From e32302c1a62f5b0736826bc3e241a705097ba223 Mon Sep 17 00:00:00 2001 From: Olivier Ortigues Date: Wed, 22 Apr 2020 13:57:54 +0200 Subject: [PATCH 0001/3533] esp8266/esppwm: Fix PWM glitch when setting duty on different channel. The PWM driver uses a double buffer for the PWM timing array, one in current use and the other one to update when changing duty parameters. The issue was that once the duty parameters were changed the updated buffer was applied immediately without synchronising to the start of the PWM period. By moving the buffer toggling/swapping to the interrupt when the cycle is done there are no more glitches. --- ports/esp8266/esppwm.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/ports/esp8266/esppwm.c b/ports/esp8266/esppwm.c index 6116a468957fa..d5bcea9acd8cc 100644 --- a/ports/esp8266/esppwm.c +++ b/ports/esp8266/esppwm.c @@ -57,6 +57,7 @@ STATIC uint8_t pwm_timer_down = 1; STATIC uint8_t pwm_current_channel = 0; STATIC uint16_t pwm_gpio = 0; STATIC uint8_t pwm_channel_num = 0; +STATIC volatile uint8_t pwm_toggle_request = 0; // XXX: 0xffffffff/(80000000/16)=35A #define US_TO_RTC_TIMER_TICKS(t) \ @@ -126,6 +127,9 @@ pwm_start(void) { LOCK_PWM(critical); // enter critical + // if a toggle is pending, we reset it since we're changing the settings again + pwm_toggle_request = 0; + struct pwm_single_param *local_single = pwm_single_toggle[pwm_toggle ^ 0x01]; uint8 *local_channel = &pwm_channel_toggle[pwm_toggle ^ 0x01]; @@ -188,14 +192,14 @@ pwm_start(void) { // start gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0); + // do the first toggle because timer has to have a valid set to do it's job + pwm_toggle ^= 0x01; + pwm_timer_down = 0; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time); - } - - if (pwm_toggle == 1) { - pwm_toggle = 0; } else { - pwm_toggle = 1; + // request pwm_tim1_intr_handler to swap the timing buffers + pwm_toggle_request = 1; } UNLOCK_PWM(critical); // leave critical @@ -297,12 +301,18 @@ pwm_get_freq(uint8 channel) { STATIC void ICACHE_RAM_ATTR pwm_tim1_intr_handler(void *dummy) { (void)dummy; - uint8 local_toggle = pwm_toggle; // pwm_toggle may change outside + RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK); if (pwm_current_channel >= (*pwm_channel - 1)) { // *pwm_channel may change outside - pwm_single = pwm_single_toggle[local_toggle]; - pwm_channel = &pwm_channel_toggle[local_toggle]; + + if (pwm_toggle_request != 0) { + pwm_toggle ^= 1; + pwm_toggle_request = 0; + } + + pwm_single = pwm_single_toggle[pwm_toggle]; + pwm_channel = &pwm_channel_toggle[pwm_toggle]; gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set, pwm_single[*pwm_channel - 1].gpio_clear, From b3bc9808f2d1396a946ae899b7d81d043eac1ddd Mon Sep 17 00:00:00 2001 From: Albort Xue Date: Thu, 7 May 2020 16:47:48 +0800 Subject: [PATCH 0002/3533] mimxrt/boards: Add MIMXRT1060_EVK board. --- .../evkmimxrt1060_flexspi_nor_config.h | 268 ++++++++++++++++++ .../boards/MIMXRT1060_EVK/flash_config.c | 48 ++++ .../boards/MIMXRT1060_EVK/mpconfigboard.h | 8 + .../boards/MIMXRT1060_EVK/mpconfigboard.mk | 7 + 4 files changed, 331 insertions(+) create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/evkmimxrt1060_flexspi_nor_config.h create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/flash_config.c create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/evkmimxrt1060_flexspi_nor_config.h b/ports/mimxrt/boards/MIMXRT1060_EVK/evkmimxrt1060_flexspi_nor_config.h new file mode 100644 index 0000000000000..8bb771fd8ad44 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/evkmimxrt1060_flexspi_nor_config.h @@ -0,0 +1,268 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __EVKMIMXRT1060_FLEXSPI_NOR_CONFIG__ +#define __EVKMIMXRT1060_FLEXSPI_NOR_CONFIG__ + +#include +#include +#include "fsl_common.h" + +/*! @name Driver version */ +/*@{*/ +/*! @brief XIP_BOARD driver version 2.0.0. */ +#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) +/*@}*/ + +/* FLEXSPI memory config block related defintions */ +#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian +#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0 +#define FLEXSPI_CFG_BLK_SIZE (512) + +/* FLEXSPI Feature related definitions */ +#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1 + +/* Lookup table related defintions */ +#define CMD_INDEX_READ 0 +#define CMD_INDEX_READSTATUS 1 +#define CMD_INDEX_WRITEENABLE 2 +#define CMD_INDEX_WRITE 4 + +#define CMD_LUT_SEQ_IDX_READ 0 +#define CMD_LUT_SEQ_IDX_READSTATUS 1 +#define CMD_LUT_SEQ_IDX_WRITEENABLE 3 +#define CMD_LUT_SEQ_IDX_WRITE 9 + +#define CMD_SDR 0x01 +#define CMD_DDR 0x21 +#define RADDR_SDR 0x02 +#define RADDR_DDR 0x22 +#define CADDR_SDR 0x03 +#define CADDR_DDR 0x23 +#define MODE1_SDR 0x04 +#define MODE1_DDR 0x24 +#define MODE2_SDR 0x05 +#define MODE2_DDR 0x25 +#define MODE4_SDR 0x06 +#define MODE4_DDR 0x26 +#define MODE8_SDR 0x07 +#define MODE8_DDR 0x27 +#define WRITE_SDR 0x08 +#define WRITE_DDR 0x28 +#define READ_SDR 0x09 +#define READ_DDR 0x29 +#define LEARN_SDR 0x0A +#define LEARN_DDR 0x2A +#define DATSZ_SDR 0x0B +#define DATSZ_DDR 0x2B +#define DUMMY_SDR 0x0C +#define DUMMY_DDR 0x2C +#define DUMMY_RWDS_SDR 0x0D +#define DUMMY_RWDS_DDR 0x2D +#define JMP_ON_CS 0x1F +#define STOP 0 + +#define FLEXSPI_1PAD 0 +#define FLEXSPI_2PAD 1 +#define FLEXSPI_4PAD 2 +#define FLEXSPI_8PAD 3 + +#define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \ + (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \ + FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1)) + +//!@brief Definitions for FlexSPI Serial Clock Frequency +typedef enum _FlexSpiSerialClockFreq +{ + kFlexSpiSerialClk_30MHz = 1, + kFlexSpiSerialClk_50MHz = 2, + kFlexSpiSerialClk_60MHz = 3, + kFlexSpiSerialClk_75MHz = 4, + kFlexSpiSerialClk_80MHz = 5, + kFlexSpiSerialClk_100MHz = 6, + kFlexSpiSerialClk_120MHz = 7, + kFlexSpiSerialClk_133MHz = 8, + kFlexSpiSerialClk_166MHz = 9, +} flexspi_serial_clk_freq_t; + +//!@brief FlexSPI clock configuration type +enum +{ + kFlexSpiClk_SDR, //!< Clock configure for SDR mode + kFlexSpiClk_DDR, //!< Clock configurat for DDR mode +}; + +//!@brief FlexSPI Read Sample Clock Source definition +typedef enum _FlashReadSampleClkSource +{ + kFlexSPIReadSampleClk_LoopbackInternally = 0, + kFlexSPIReadSampleClk_LoopbackFromDqsPad = 1, + kFlexSPIReadSampleClk_LoopbackFromSckPad = 2, + kFlexSPIReadSampleClk_ExternalInputFromDqsPad = 3, +} flexspi_read_sample_clk_t; + +//!@brief Misc feature bit definitions +enum +{ + kFlexSpiMiscOffset_DiffClkEnable = 0, //!< Bit for Differential clock enable + kFlexSpiMiscOffset_Ck2Enable = 1, //!< Bit for CK2 enable + kFlexSpiMiscOffset_ParallelEnable = 2, //!< Bit for Parallel mode enable + kFlexSpiMiscOffset_WordAddressableEnable = 3, //!< Bit for Word Addressable enable + kFlexSpiMiscOffset_SafeConfigFreqEnable = 4, //!< Bit for Safe Configuration Frequency enable + kFlexSpiMiscOffset_PadSettingOverrideEnable = 5, //!< Bit for Pad setting override enable + kFlexSpiMiscOffset_DdrModeEnable = 6, //!< Bit for DDR clock confiuration indication. +}; + +//!@brief Flash Type Definition +enum +{ + kFlexSpiDeviceType_SerialNOR = 1, //!< Flash devices are Serial NOR + kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND + kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH + kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND + kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs +}; + +//!@brief Flash Pad Definitions +enum +{ + kSerialFlash_1Pad = 1, + kSerialFlash_2Pads = 2, + kSerialFlash_4Pads = 4, + kSerialFlash_8Pads = 8, +}; + +//!@brief FlexSPI LUT Sequence structure +typedef struct _lut_sequence +{ + uint8_t seqNum; //!< Sequence Number, valid number: 1-16 + uint8_t seqId; //!< Sequence Index, valid number: 0-15 + uint16_t reserved; +} flexspi_lut_seq_t; + +//!@brief Flash Configuration Command Type +enum +{ + kDeviceConfigCmdType_Generic, //!< Generic command, for example: configure dummy cycles, drive strength, etc + kDeviceConfigCmdType_QuadEnable, //!< Quad Enable command + kDeviceConfigCmdType_Spi2Xpi, //!< Switch from SPI to DPI/QPI/OPI mode + kDeviceConfigCmdType_Xpi2Spi, //!< Switch from DPI/QPI/OPI to SPI mode + kDeviceConfigCmdType_Spi2NoCmd, //!< Switch to 0-4-4/0-8-8 mode + kDeviceConfigCmdType_Reset, //!< Reset device command +}; + +//!@brief FlexSPI Memory Configuration Block +typedef struct _FlexSPIConfig +{ + uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL + uint32_t version; //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix + uint32_t reserved0; //!< [0x008-0x00b] Reserved for future use + uint8_t readSampleClkSrc; //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3 + uint8_t csHoldTime; //!< [0x00d-0x00d] CS hold time, default value: 3 + uint8_t csSetupTime; //!< [0x00e-0x00e] CS setup time, default value: 3 + uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For + //! Serial NAND, need to refer to datasheet + uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable + uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch, + //! Generic configuration, etc. + uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for + //! DPI/QPI/OPI switch or reset command + flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt + //! sequence number, [31:16] Reserved + uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration + uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable + uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe + flexspi_lut_seq_t + configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq + uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use + uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands + uint32_t reserved2; //!< [0x03c-0x03f] Reserved for future use + uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more + //! details + uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details + uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal + uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot + //! Chapter for more details + uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot + //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH + uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use + uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1 + uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2 + uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1 + uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2 + uint32_t csPadSettingOverride; //!< [0x060-0x063] CS pad setting override value + uint32_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value + uint32_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value + uint32_t dqsPadSettingOverride; //!< [0x06c-0x06f] DQS pad setting override value + uint32_t timeoutInMs; //!< [0x070-0x073] Timeout threshold for read status command + uint32_t commandInterval; //!< [0x074-0x077] CS deselect interval between two commands + uint16_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B, in terms of 0.1ns + uint16_t busyOffset; //!< [0x07c-0x07d] Busy offset, valid value: 0-31 + uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 - + //! busy flag is 0 when flash device is busy + uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences + flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences + uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use +} flexspi_mem_config_t; + +/* */ +#define NOR_CMD_INDEX_READ CMD_INDEX_READ //!< 0 +#define NOR_CMD_INDEX_READSTATUS CMD_INDEX_READSTATUS //!< 1 +#define NOR_CMD_INDEX_WRITEENABLE CMD_INDEX_WRITEENABLE //!< 2 +#define NOR_CMD_INDEX_ERASESECTOR 3 //!< 3 +#define NOR_CMD_INDEX_PAGEPROGRAM CMD_INDEX_WRITE //!< 4 +#define NOR_CMD_INDEX_CHIPERASE 5 //!< 5 +#define NOR_CMD_INDEX_DUMMY 6 //!< 6 +#define NOR_CMD_INDEX_ERASEBLOCK 7 //!< 7 + +#define NOR_CMD_LUT_SEQ_IDX_READ CMD_LUT_SEQ_IDX_READ //!< 0 READ LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS \ + CMD_LUT_SEQ_IDX_READSTATUS //!< 1 Read Status LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS_XPI \ + 2 //!< 2 Read status DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE \ + CMD_LUT_SEQ_IDX_WRITEENABLE //!< 3 Write Enable sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE_XPI \ + 4 //!< 4 Write Enable DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 //!< 5 Erase Sector sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASEBLOCK 8 //!< 8 Erase Block sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM \ + CMD_LUT_SEQ_IDX_WRITE //!< 9 Program sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_CHIPERASE 11 //!< 11 Chip Erase sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READ_SFDP 13 //!< 13 Read SFDP sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_RESTORE_NOCMD \ + 14 //!< 14 Restore 0-4-4/0-8-8 mode sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_EXIT_NOCMD \ + 15 //!< 15 Exit 0-4-4/0-8-8 mode sequence id in lookupTable stored in config blobk + +/* + * Serial NOR configuration block + */ +typedef struct _flexspi_nor_config +{ + flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI + uint32_t pageSize; //!< Page size of Serial NOR + uint32_t sectorSize; //!< Sector size of Serial NOR + uint8_t ipcmdSerialClkFreq; //!< Clock frequency for IP command + uint8_t isUniformBlockSize; //!< Sector/Block size is the same + uint8_t reserved0[2]; //!< Reserved for future use + uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3 + uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command + uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false + uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution + uint32_t blockSize; //!< Block size + uint32_t reserve2[11]; //!< Reserved for future use +} flexspi_nor_config_t; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif /* __EVKMIMXRT1060_FLEXSPI_NOR_CONFIG__ */ diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/flash_config.c b/ports/mimxrt/boards/MIMXRT1060_EVK/flash_config.c new file mode 100644 index 0000000000000..095a922e9efe1 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/flash_config.c @@ -0,0 +1,48 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "evkmimxrt1060_flexspi_nor_config.h" + +/* Component ID definition, used by tools. */ +#ifndef FSL_COMPONENT_ID +#define FSL_COMPONENT_ID "platform.drivers.xip_board" +#endif + +/******************************************************************************* + * Code + ******************************************************************************/ +#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1) +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.conf"))) +#elif defined(__ICCARM__) +#pragma location = ".boot_hdr.conf" +#endif + +const flexspi_nor_config_t qspiflash_config = { + .memConfig = + { + .tag = FLEXSPI_CFG_BLK_TAG, + .version = FLEXSPI_CFG_BLK_VERSION, + .readSampleClkSrc = kFlexSPIReadSampleClk_LoopbackFromDqsPad, + .csHoldTime = 3u, + .csSetupTime = 3u, + .sflashPadType = kSerialFlash_4Pads, + .serialClkFreq = kFlexSpiSerialClk_100MHz, + .sflashA1Size = 8u * 1024u * 1024u, + .lookupTable = + { + // Read LUTs + FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0xEB, RADDR_SDR, FLEXSPI_4PAD, 0x18), + FLEXSPI_LUT_SEQ(DUMMY_SDR, FLEXSPI_4PAD, 0x06, READ_SDR, FLEXSPI_4PAD, 0x04), + }, + }, + .pageSize = 256u, + .sectorSize = 4u * 1024u, + .blockSize = 64u * 1024u, + .isUniformBlockSize = false, +}; +#endif /* XIP_BOOT_HEADER_ENABLE */ diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h new file mode 100644 index 0000000000000..714f987a54c9d --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h @@ -0,0 +1,8 @@ +#define MICROPY_HW_BOARD_NAME "i.MX RT1060 EVK" +#define MICROPY_HW_MCU_NAME "MIMXRT1062DVJ6A" + +#define BOARD_FLASH_SIZE (8 * 1024 * 1024) + +#define MICROPY_HW_LED_PINMUX IOMUXC_GPIO_AD_B0_09_GPIO1_IO09 +#define MICROPY_HW_LED_PORT GPIO1 +#define MICROPY_HW_LED_PIN 9 diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk new file mode 100644 index 0000000000000..b34169801b900 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk @@ -0,0 +1,7 @@ +MCU_SERIES = MIMXRT1062 +MCU_VARIANT = MIMXRT1062DVJ6A + +JLINK_PATH ?= /media/RT1060-EVK/ + +deploy: $(BUILD)/firmware.bin + cp $< $(JLINK_PATH) From 25bc42e754e76f7dd45048bcfb139fdba22e3e29 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 13:08:35 +0930 Subject: [PATCH 0003/3533] powerpc: Fix Makefile rule when linking. The linker script was included in the "$^" inputs, causing the build to fail: LINK build/firmware.elf powerpc64le-linux-gnu-ld: error: linker script file 'powerpc.lds' appears multiple times As a fix the linker script is left as a dependency of the elf, but only the object files are linked. --- ports/powerpc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index 7aa4f1449c508..2e4dd0fd0261c 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -52,7 +52,7 @@ $(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h $(BUILD)/firmware.elf: $(OBJ) powerpc.lds $(ECHO) "LINK $@" - $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) + $(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS) $(Q)$(SIZE) $@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf From b65482ffa8715f3eeeb593e26d96d80bd77e0dfd Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 13:07:09 +0930 Subject: [PATCH 0004/3533] powerpc: Set better default compiler. Most developers use a compiler which is called powerpc64le-linux-gnu-gcc. --- ports/powerpc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index 2e4dd0fd0261c..5f17a93557e5c 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -9,7 +9,7 @@ include $(TOP)/py/py.mk ARCH = $(shell uname -m) ifneq ("$(ARCH)", "ppc64") ifneq ("$(ARCH)", "ppc64le") - CROSS_COMPILE ?= powerpc64le-linux- + CROSS_COMPILE ?= powerpc64le-linux-gnu- endif endif From a9d96499b8f7310b9e43b4a8cda2f6e17cbd4248 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 13:24:19 +0930 Subject: [PATCH 0005/3533] travis: Run apt commands once, to slightly speed up the CI. --- .travis.yml | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5460bf601f484..84f58625640bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -64,8 +64,7 @@ jobs: # need newer gcc version for Cortex-M7 support - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa - sudo apt-get update -qq || true - - sudo apt-get install gcc-arm-embedded - - sudo apt-get install libnewlib-arm-none-eabi + - sudo apt-get install gcc-arm-embedded libnewlib-arm-none-eabi - arm-none-eabi-gcc --version script: - make ${MAKEOPTS} -C mpy-cross @@ -86,9 +85,7 @@ jobs: dist: bionic # needed for more recent version of qemu-system-arm with mps2-an385 target env: NAME="qemu-arm port build and tests" install: - - sudo apt-get install gcc-arm-none-eabi - - sudo apt-get install libnewlib-arm-none-eabi - - sudo apt-get install qemu-system + - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi qemu-system - arm-none-eabi-gcc --version - qemu-system-arm --version script: @@ -304,9 +301,7 @@ jobs: - stage: test env: NAME="bare-arm and minimal ports build and size-diff check" install: - - sudo apt-get install gcc-multilib libffi-dev:i386 - - sudo apt-get install gcc-arm-none-eabi - - sudo apt-get install libnewlib-arm-none-eabi + - sudo apt-get install gcc-multilib libffi-dev:i386 gcc-arm-none-eabi libnewlib-arm-none-eabi - gcc --version - arm-none-eabi-gcc --version script: @@ -332,8 +327,7 @@ jobs: - stage: test env: NAME="cc3200 port build" install: - - sudo apt-get install gcc-arm-none-eabi - - sudo apt-get install libnewlib-arm-none-eabi + - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: - make ${MAKEOPTS} -C ports/cc3200 BTARGET=application BTYPE=release - make ${MAKEOPTS} -C ports/cc3200 BTARGET=bootloader BTYPE=release @@ -342,8 +336,7 @@ jobs: - stage: test env: NAME="samd port build" install: - - sudo apt-get install gcc-arm-none-eabi - - sudo apt-get install libnewlib-arm-none-eabi + - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: - make ${MAKEOPTS} -C ports/samd submodules - make ${MAKEOPTS} -C ports/samd @@ -352,8 +345,7 @@ jobs: - stage: test env: NAME="teensy port build" install: - - sudo apt-get install gcc-arm-none-eabi - - sudo apt-get install libnewlib-arm-none-eabi + - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: - make ${MAKEOPTS} -C ports/teensy @@ -361,7 +353,6 @@ jobs: - stage: test env: NAME="powerpc port build" install: - - sudo apt-get install gcc-powerpc64le-linux-gnu - - sudo apt-get install libc6-dev-ppc64el-cross + - sudo apt-get install gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross script: - make ${MAKEOPTS} -C ports/powerpc CROSS_COMPILE=powerpc64le-linux-gnu- From d6803067c0a15c03af0639aa02b68898a3f9cc81 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 13:27:33 +0930 Subject: [PATCH 0006/3533] travis: Set build name so it appears in the web interfaces. The env NAME="foo" syntax doesn't appear to set the name in the Travis web interface. Instead use the syntax from the docs: https://docs.travis-ci.com/user/build-stages/#naming-your-jobs-within-build-stages --- .travis.yml | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 84f58625640bc..133035949d484 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ jobs: - stage: test os: linux dist: bionic - env: NAME="code formatting" + name: "code formatting" before_install: - sudo apt-add-repository --yes --update ppa:pybricks/ppa install: @@ -41,8 +41,8 @@ jobs: - stage: test os: osx osx_image: xcode11.3 + name: "unix port build with clang on OSX" env: - - NAME="unix port build with clang on OSX" - PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig script: - make ${MAKEOPTS} -C mpy-cross @@ -59,7 +59,7 @@ jobs: # stm32 port - stage: test - env: NAME="stm32 port build" + name: "stm32 port build" install: # need newer gcc version for Cortex-M7 support - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa @@ -83,7 +83,7 @@ jobs: # qemu-arm port - stage: test dist: bionic # needed for more recent version of qemu-system-arm with mps2-an385 target - env: NAME="qemu-arm port build and tests" + name: "qemu-arm port build and tests" install: - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi qemu-system - arm-none-eabi-gcc --version @@ -96,7 +96,7 @@ jobs: # unix coverage - stage: test - env: NAME="unix coverage build and tests" + name: "unix coverage build and tests" install: - sudo apt-get install python3-pip - sudo pip install cpp-coveralls @@ -131,7 +131,7 @@ jobs: # unix coverage 32-bit - stage: test - env: NAME="unix coverage 32-bit build and tests" + name: "unix coverage 32-bit build and tests" install: - sudo apt-get install gcc-multilib libffi-dev:i386 - sudo apt-get install python3-pip @@ -163,7 +163,7 @@ jobs: # standard unix port - stage: test - env: NAME="unix port build and tests" + name: "unix port build and tests" script: - make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/unix submodules @@ -176,7 +176,7 @@ jobs: # unix nanbox/float (and using Python 2 to check it can run the build scripts) - stage: test - env: NAME="unix nanbox/float port build and tests" + name: "unix nanbox/float port build and tests" install: - sudo apt-get install gcc-multilib libffi-dev:i386 script: @@ -193,7 +193,7 @@ jobs: # unix stackless/float with clang - stage: test - env: NAME="unix stackless/float port build and tests with clang" + name: "unix stackless/float port build and tests with clang" install: - sudo apt-get install clang script: @@ -209,7 +209,7 @@ jobs: # unix with sys.settrace - stage: test - env: NAME="unix port with sys.settrace build and tests" + name: "unix port with sys.settrace build and tests" script: - make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/unix MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-DMICROPY_PY_SYS_SETTRACE=1" test || travis_terminate 1 @@ -220,7 +220,7 @@ jobs: # minimal unix port with tests - stage: test - env: NAME="minimal unix port build and tests" + name: "minimal unix port build and tests" script: - make ${MAKEOPTS} -C ports/unix VARIANT=minimal - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests -e exception_chain -e self_type_check -e subclass_native_init -d basics) @@ -229,7 +229,7 @@ jobs: # windows port via mingw - stage: test - env: NAME="windows port build via mingw" + name: "windows port build via mingw" install: - sudo apt-get install gcc-mingw-w64 script: @@ -238,7 +238,7 @@ jobs: # esp32 w/ESP-IDFv3 port - stage: test - env: NAME="esp32 ESP-IDFv3 port build" + name: "esp32 ESP-IDFv3 port build" install: - sudo apt-get install python3-pip - sudo pip3 install 'pyparsing<2.4' @@ -255,7 +255,7 @@ jobs: # esp32 w/ESP-IDFv4 port - stage: test - env: NAME="esp32 ESP-IDFv4 port build" + name: "esp32 ESP-IDFv4 port build" install: - sudo apt-get install python3-pip - sudo pip3 install 'pyparsing<2.4' @@ -272,7 +272,7 @@ jobs: # esp8266 port - stage: test - env: NAME="esp8266 port build" + name: "esp8266 port build" install: - wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz - zcat xtensa-lx106-elf-standalone.tar.gz | tar x @@ -286,7 +286,7 @@ jobs: # nrf port - stage: test - env: NAME="nrf port build" + name: "nrf port build" install: - sudo apt-get install gcc-arm-none-eabi - sudo apt-get install libnewlib-arm-none-eabi @@ -299,7 +299,7 @@ jobs: # bare-arm and minimal ports, with size-diff check - stage: test - env: NAME="bare-arm and minimal ports build and size-diff check" + name: "bare-arm and minimal ports build and size-diff check" install: - sudo apt-get install gcc-multilib libffi-dev:i386 gcc-arm-none-eabi libnewlib-arm-none-eabi - gcc --version @@ -325,7 +325,7 @@ jobs: # cc3200 port - stage: test - env: NAME="cc3200 port build" + name: "cc3200 port build" install: - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: @@ -334,7 +334,7 @@ jobs: # samd port - stage: test - env: NAME="samd port build" + name: "samd port build" install: - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: @@ -343,7 +343,7 @@ jobs: # teensy port - stage: test - env: NAME="teensy port build" + name: "teensy port build" install: - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi script: @@ -351,7 +351,7 @@ jobs: # powerpc port - stage: test - env: NAME="powerpc port build" + name: "powerpc port build" install: - sudo apt-get install gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross script: From 4bbba3060d451d4dfa147ebc793acd1cc7364e5a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 27 May 2020 17:53:31 +1000 Subject: [PATCH 0007/3533] lib/utils: Lock the scheduler when executing hard callback functions. Otherwise scheduled functions may execute during the hard callback and then fail if they try to allocate heap memory. --- lib/utils/mpirq.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/utils/mpirq.c b/lib/utils/mpirq.c index d04fab68bbb52..8de13b0b6a3c8 100644 --- a/lib/utils/mpirq.c +++ b/lib/utils/mpirq.c @@ -62,8 +62,10 @@ mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent) { void mp_irq_handler(mp_irq_obj_t *self) { if (self->handler != mp_const_none) { if (self->ishard) { - // When executing code within a handler we must lock the GC to prevent - // any memory allocations. + // When executing code within a handler we must lock the scheduler to + // prevent any scheduled callbacks from running, and lock the GC to + // prevent any memory allocations. + mp_sched_lock(); gc_lock(); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { @@ -77,6 +79,7 @@ void mp_irq_handler(mp_irq_obj_t *self) { mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); } gc_unlock(); + mp_sched_unlock(); } else { // Schedule call to user function mp_sched_schedule(self->handler, self->parent); From a902b69dd51de0e3fe3bb6955296591d6a93abab Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 May 2020 11:27:09 +1000 Subject: [PATCH 0008/3533] py/py.mk: Use additional CFLAGS to compile string0.c. Otherwise functions like memset might get optimised to call themselves (eg with gcc 10). And provide CFLAGS_BUILTIN so these options can be changed by a port if needed. Fixes issue #6053. --- py/py.mk | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/py/py.mk b/py/py.mk index 0a98ccc00ca0f..d864a7ed3d843 100644 --- a/py/py.mk +++ b/py/py.mk @@ -266,6 +266,11 @@ $(HEADER_BUILD)/moduledefs.h: $(SRC_QSTR) $(QSTR_GLOBAL_DEPENDENCIES) | $(HEADER SRC_QSTR += $(HEADER_BUILD)/moduledefs.h +# Standard C functions like memset need to be compiled with special flags so +# the compiler does not optimise these functions in terms of themselves. +CFLAGS_BUILTIN ?= -ffreestanding -fno-builtin -fno-lto +$(BUILD)/lib/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN) + # Force nlr code to always be compiled with space-saving optimisation so # that the function preludes are of a minimal and predictable form. $(PY_BUILD)/nlr%.o: CFLAGS += -Os From 81db22f693d06468d45571a29fc0648a8f5664ce Mon Sep 17 00:00:00 2001 From: stijn Date: Sun, 17 May 2020 12:29:25 +0200 Subject: [PATCH 0009/3533] py/modmath: Work around msvc float bugs in atan2, fmod and modf. Older implementations deal with infinity/negative zero incorrectly. This commit adds generic fixes that can be enabled by any port that needs them, along with new tests cases. --- ports/windows/mpconfigport.h | 8 ++++++++ py/modmath.c | 27 ++++++++++++++++++++++++++- py/mpconfig.h | 15 +++++++++++++++ tests/float/math_domain.py | 4 ++-- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 84c196e11fa9c..fa09dda7527e0 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -227,6 +227,14 @@ extern const struct _mp_obj_module_t mp_module_time; #define MP_SSIZE_MAX _I32_MAX #endif +// VC++ 12.0 fixes +#if (_MSC_VER <= 1800) +#define MICROPY_PY_MATH_ATAN2_FIX_INFNAN (1) +#define MICROPY_PY_MATH_FMOD_FIX_INFNAN (1) +#ifdef _WIN64 +#define MICROPY_PY_MATH_MODF_FIX_NEGZERO (1) +#endif +#endif // CL specific definitions diff --git a/py/modmath.c b/py/modmath.c index 5ff892ba112c8..b312eeb3d2c01 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -34,6 +34,8 @@ // M_PI is not part of the math.h standard and may not be defined // And by defining our own we can ensure it uses the correct const format. #define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846) +#define MP_PI_4 MICROPY_FLOAT_CONST(0.78539816339744830962) +#define MP_3_PI_4 MICROPY_FLOAT_CONST(2.35619449019234492885) STATIC NORETURN void math_error(void) { mp_raise_ValueError(MP_ERROR_TEXT("math domain error")); @@ -132,7 +134,17 @@ MATH_FUN_1(asin, asin) // atan(x) MATH_FUN_1(atan, atan) // atan2(y, x) +#if MICROPY_PY_MATH_ATAN2_FIX_INFNAN +mp_float_t atan2_func(mp_float_t x, mp_float_t y) { + if (isinf(x) && isinf(y)) { + return copysign(y < 0 ? MP_3_PI_4 : MP_PI_4, x); + } + return atan2(x, y); +} +MATH_FUN_2(atan2, atan2_func) +#else MATH_FUN_2(atan2, atan2) +#endif // ceil(x) MATH_FUN_1_TO_INT(ceil, ceil) // copysign(x, y) @@ -148,7 +160,14 @@ MATH_FUN_1(fabs, fabs_func) // floor(x) MATH_FUN_1_TO_INT(floor, floor) // TODO: delegate to x.__floor__() if x is not a float // fmod(x, y) +#if MICROPY_PY_MATH_FMOD_FIX_INFNAN +mp_float_t fmod_func(mp_float_t x, mp_float_t y) { + return (!isinf(x) && isinf(y)) ? x : fmod(x, y); +} +MATH_FUN_2(fmod, fmod_func) +#else MATH_FUN_2(fmod, fmod) +#endif // isfinite(x) MATH_FUN_1_TO_BOOL(isfinite, isfinite) // isinf(x) @@ -246,7 +265,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp); // modf(x) STATIC mp_obj_t mp_math_modf(mp_obj_t x_obj) { mp_float_t int_part = 0.0; - mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part); + mp_float_t x = mp_obj_get_float(x_obj); + mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(x, &int_part); + #if MICROPY_PY_MATH_MODF_FIX_NEGZERO + if (fractional_part == MICROPY_FLOAT_CONST(0.0)) { + fractional_part = copysign(fractional_part, x); + } + #endif mp_obj_t tuple[2]; tuple[0] = mp_obj_new_float(fractional_part); tuple[1] = mp_obj_new_float(int_part); diff --git a/py/mpconfig.h b/py/mpconfig.h index f2b3af1f2a314..27df3f483a0e1 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1128,6 +1128,21 @@ typedef double mp_float_t; #define MICROPY_PY_MATH_ISCLOSE (0) #endif +// Whether to provide fix for atan2 Inf handling. +#ifndef MICROPY_PY_MATH_ATAN2_FIX_INFNAN +#define MICROPY_PY_MATH_ATAN2_FIX_INFNAN (0) +#endif + +// Whether to provide fix for fmod Inf handling. +#ifndef MICROPY_PY_MATH_FMOD_FIX_INFNAN +#define MICROPY_PY_MATH_FMOD_FIX_INFNAN (0) +#endif + +// Whether to provide fix for modf negative zero handling. +#ifndef MICROPY_PY_MATH_MODF_FIX_NEGZERO +#define MICROPY_PY_MATH_MODF_FIX_NEGZERO (0) +#endif + // Whether to provide "cmath" module #ifndef MICROPY_PY_CMATH #define MICROPY_PY_CMATH (0) diff --git a/tests/float/math_domain.py b/tests/float/math_domain.py index 2d4670f75dd48..e63628cf5078a 100644 --- a/tests/float/math_domain.py +++ b/tests/float/math_domain.py @@ -39,8 +39,8 @@ # double argument functions for name, f, args in ( ("pow", math.pow, ((0, 2), (-1, 2), (0, -1), (-1, 2.3))), - ("fmod", math.fmod, ((1.2, inf), (1.2, 0), (inf, 1.2))), - ("atan2", math.atan2, ((0, 0),)), + ("fmod", math.fmod, ((1.2, inf), (1.2, -inf), (1.2, 0), (inf, 1.2))), + ("atan2", math.atan2, ((0, 0), (-inf, inf), (-inf, -inf), (inf, -inf))), ("copysign", math.copysign, ()), ): for x in args + ((0, inf), (inf, 0), (inf, inf), (inf, nan), (nan, inf), (nan, nan)): From 97ccde0c431674113f56b4c9d421b2bc1acb1b56 Mon Sep 17 00:00:00 2001 From: stijn Date: Sun, 17 May 2020 10:57:51 +0200 Subject: [PATCH 0010/3533] py/ringbuf: Fix compilation with msvc. Older versions do not have "inline" so fetch the definition from mpconfigport.h. --- py/ringbuf.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/ringbuf.h b/py/ringbuf.h index 4d316d961b2b5..293e418306051 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -29,6 +29,10 @@ #include #include +#ifdef _MSC_VER +#include "py/mpconfig.h" // For inline. +#endif + typedef struct _ringbuf_t { uint8_t *buf; uint16_t size; From 9523ca92e03e699494560ec964bea748b291c7a0 Mon Sep 17 00:00:00 2001 From: stijn Date: Sun, 17 May 2020 09:39:59 +0200 Subject: [PATCH 0011/3533] windows: Make appveyor.yml self-contained. Add configuration which otherwise has to be set via the UI so the file is more self-contained, and remove configuration which is not needed because it's the same as the default. The major change here is that for a while now Appveyor has been using Visual Studio 2015 by default while we still want to support 2013. --- ports/windows/.appveyor.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ports/windows/.appveyor.yml b/ports/windows/.appveyor.yml index 2e0dbdea5d116..2965543b05942 100644 --- a/ports/windows/.appveyor.yml +++ b/ports/windows/.appveyor.yml @@ -1,3 +1,7 @@ +image: Visual Studio 2013 +clone_depth: 1 +skip_tags: true + environment: # Python version used MICROPY_CPYTHON3: c:/python34/python.exe @@ -71,10 +75,3 @@ after_test: if ($LASTEXITCODE -ne 0) { throw "$env:MSYSTEM mpy-cross tests exited with code $LASTEXITCODE" } - -skip_tags: true - -deploy: off - -nuget: - disable_publish_on_pr: true From 093fd807606d088cfaf874e79060a6c68484cbfa Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 22 May 2020 11:10:15 -0500 Subject: [PATCH 0012/3533] py/modsys: Use consistent naming pattern for module-level const objects. This renames a few identifiers to follow the usual naming convention of mp__. This makes them easier to find, e.g. when grep'ing. --- py/modsys.c | 10 +++++----- py/objint.h | 2 +- py/objint_longlong.c | 2 +- py/objint_mpz.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/py/modsys.c b/py/modsys.c index 8092d9799d81f..586b13e747130 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -53,7 +53,7 @@ const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adap #endif // version - Python language version that this implementation conforms to, as a string -STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0"); +STATIC const MP_DEFINE_STR_OBJ(mp_sys_version_obj, "3.4.0"); // version_info - Python language version that this implementation conforms to, as a tuple of ints #define I(n) MP_OBJ_NEW_SMALL_INT(n) @@ -105,7 +105,7 @@ STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = { #ifdef MICROPY_PY_SYS_PLATFORM // platform - the platform that MicroPython is running on -STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM); +STATIC const MP_DEFINE_STR_OBJ(mp_sys_platform_obj, MICROPY_PY_SYS_PLATFORM); #endif // exit([retval]): raise SystemExit, with optional argument given to the exception @@ -189,11 +189,11 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) }, { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) }, - { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) }, + { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_sys_version_obj) }, { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) }, { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) }, #ifdef MICROPY_PY_SYS_PLATFORM - { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) }, + { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&mp_sys_platform_obj) }, #endif #if MP_ENDIANNESS_LITTLE { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) }, @@ -210,7 +210,7 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = { // of "one" bits in sys.maxsize. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) }, #else - { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) }, + { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_sys_maxsize_obj) }, #endif #endif diff --git a/py/objint.h b/py/objint.h index c4752384e04db..5eed87705dedb 100644 --- a/py/objint.h +++ b/py/objint.h @@ -38,7 +38,7 @@ typedef struct _mp_obj_int_t { #endif } mp_obj_int_t; -extern const mp_obj_int_t mp_maxsize_obj; +extern const mp_obj_int_t mp_sys_maxsize_obj; #if MICROPY_PY_BUILTINS_FLOAT mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in); diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 619b4d2b54475..f2e88c3ea5643 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -40,7 +40,7 @@ #if MICROPY_PY_SYS_MAXSIZE // Export value for sys.maxsize -const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; +const mp_obj_int_t mp_sys_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; #endif mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) { diff --git a/py/objint_mpz.c b/py/objint_mpz.c index f2f0dbc3a51cf..6e52073a6e85b 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -66,7 +66,7 @@ STATIC const mpz_dig_t maxsize_dig[] = { #endif }; // *FORMAT-ON* -const mp_obj_int_t mp_maxsize_obj = { +const mp_obj_int_t mp_sys_maxsize_obj = { {&mp_type_int}, {.fixed_dig = 1, .len = NUM_DIG, .alloc = NUM_DIG, .dig = (mpz_dig_t *)maxsize_dig} }; From 2d1fef709664f5dc39bd7abac21bc063db90df61 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 26 May 2020 12:49:41 -0500 Subject: [PATCH 0013/3533] tools/codeformat.py: Use -q option on uncrustify to make output quiet. This suppresses the Parsing: as language C lines. This makes parsing run a bit faster and on CI it makes for less scrolling through logs (and black already uses the -q option). --- tools/codeformat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/codeformat.py b/tools/codeformat.py index 0a8bf2e0f4e8c..2fe72ef33d570 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -168,7 +168,7 @@ def batch(cmd, files, N=200): # Format C files with uncrustify. if format_c: - batch(["uncrustify", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"], lang_files(C_EXTS)) + batch(["uncrustify", "-q", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"], lang_files(C_EXTS)) for file in lang_files(C_EXTS): fixup_c(file) From 1662a0b06f2ad58aa4a632a88d0b3836c08bded2 Mon Sep 17 00:00:00 2001 From: cccc Date: Tue, 25 Feb 2020 23:09:59 +0800 Subject: [PATCH 0014/3533] esp32/machine_sdcard: Add "freq" keyword arg to SDCard constructor. To allow high speed access. --- docs/library/machine.SDCard.rst | 4 +++- ports/esp32/machine_sdcard.c | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/library/machine.SDCard.rst b/docs/library/machine.SDCard.rst index cf86b1fcb25e6..b1cf42ec0285b 100644 --- a/docs/library/machine.SDCard.rst +++ b/docs/library/machine.SDCard.rst @@ -23,7 +23,7 @@ arguments that might need to be set in order to use either a non-standard slot or a non-standard pin assignment. The exact subset of arguments supported will vary from platform to platform. -.. class:: SDCard(slot=1, width=1, cd=None, wp=None, sck=None, miso=None, mosi=None, cs=None) +.. class:: SDCard(slot=1, width=1, cd=None, wp=None, sck=None, miso=None, mosi=None, cs=None, freq=20000000) This class provides access to SD or MMC storage cards using either a dedicated SD/MMC interface hardware or through an SPI channel. @@ -50,6 +50,8 @@ vary from platform to platform. - *mosi* can be used to specify an SPI mosi pin. - *cs* can be used to specify an SPI chip select pin. + + - *freq* selects the SD/MMC interface frequency in Hz (only supported on the ESP32). Implementation-specific details ------------------------------- diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 0fd4e86211e86..40686508a1e20 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -126,6 +126,7 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args ARG_mosi, ARG_sck, ARG_cs, + ARG_freq, }; STATIC const mp_arg_t allowed_args[] = { { MP_QSTR_slot, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, @@ -137,6 +138,8 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + // freq is valid for both SPI and SDMMC interfaces + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 20000000} }, }; mp_arg_val_t arg_vals[MP_ARRAY_SIZE(allowed_args)]; mp_map_t kw_args; @@ -175,11 +178,14 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args self->flags = 0; // Note that these defaults are macros that expand to structure // constants so we can't directly assign them to fields. + int freq = arg_vals[ARG_freq].u_int; if (is_spi) { sdmmc_host_t _temp_host = SDSPI_HOST_DEFAULT(); + _temp_host.max_freq_khz = freq / 1000; self->host = _temp_host; } else { sdmmc_host_t _temp_host = SDMMC_HOST_DEFAULT(); + _temp_host.max_freq_khz = freq / 1000; self->host = _temp_host; } From 50a7ba234850183693d2b236e48ec0b76399123d Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 May 2020 01:36:53 -0700 Subject: [PATCH 0015/3533] esp32/modmachine: Fix machine.reset_cause to use IDF's esp_reset_reason. The code previously called rtc_get_reset_reason which is a "raw" reset cause. The ESP-IDF massages that for the proper reset cause available from esp_reset_reason. Fixes issue #5134. --- ports/esp32/modmachine.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 8d8df995c294b..82a02522f7b41 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -146,36 +146,30 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_deepsleep_obj, 0, machine_deepsleep); STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - switch (rtc_get_reset_reason(0)) { - case POWERON_RESET: + switch (esp_reset_reason()) { + case ESP_RST_POWERON: + case ESP_RST_BROWNOUT: return MP_OBJ_NEW_SMALL_INT(MP_PWRON_RESET); break; - case SW_RESET: - case SW_CPU_RESET: - return MP_OBJ_NEW_SMALL_INT(MP_SOFT_RESET); - break; - case OWDT_RESET: - case TG0WDT_SYS_RESET: - case TG1WDT_SYS_RESET: - case RTCWDT_SYS_RESET: - case RTCWDT_BROWN_OUT_RESET: - case RTCWDT_CPU_RESET: - case RTCWDT_RTC_RESET: - case TGWDT_CPU_RESET: + + case ESP_RST_INT_WDT: + case ESP_RST_TASK_WDT: + case ESP_RST_WDT: return MP_OBJ_NEW_SMALL_INT(MP_WDT_RESET); break; - case DEEPSLEEP_RESET: + case ESP_RST_DEEPSLEEP: return MP_OBJ_NEW_SMALL_INT(MP_DEEPSLEEP_RESET); break; - case EXT_CPU_RESET: + case ESP_RST_SW: + case ESP_RST_PANIC: + case ESP_RST_EXT: // Comment in ESP-IDF: "For ESP32, ESP_RST_EXT is never returned" return MP_OBJ_NEW_SMALL_INT(MP_HARD_RESET); break; - case NO_MEAN: - case SDIO_RESET: - case INTRUSION_RESET: + case ESP_RST_SDIO: + case ESP_RST_UNKNOWN: default: return MP_OBJ_NEW_SMALL_INT(0); break; From f03d030080ccf89c6ad677c818c949c3bd71ec37 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 12:59:55 +0930 Subject: [PATCH 0016/3533] powerpc/uart: Choose which UART to use at build time, not runtime. Microwatt may have firmware that places data in r3, which was used to detect microwatt vs powernv. This breaks the existing probing of the UART type in this powerpc port. Instead build only the appropriate UART into the firmware, selected by passing the option UART=potato or UART=lpc_serial to the Makefile. A future enhancement would be to parse the device tree and configure MicroPython based on the settings. --- ports/powerpc/Makefile | 7 ++-- ports/powerpc/README.md | 6 ++- ports/powerpc/main.c | 1 - ports/powerpc/uart_core.c | 73 --------------------------------- ports/powerpc/uart_lpc_serial.c | 16 +++++--- ports/powerpc/uart_potato.c | 31 ++++++++------ 6 files changed, 38 insertions(+), 96 deletions(-) delete mode 100644 ports/powerpc/uart_core.c diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index 5f17a93557e5c..1f5ec80d43653 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -6,6 +6,9 @@ QSTR_DEFS = qstrdefsport.h # include py core make definitions include $(TOP)/py/py.mk +# potato or lpc_serial +UART ?= potato + ARCH = $(shell uname -m) ifneq ("$(ARCH)", "ppc64") ifneq ("$(ARCH)", "ppc64le") @@ -30,9 +33,7 @@ LIBS = SRC_C = \ main.c \ - uart_core.c \ - uart_potato.c \ - uart_lpc_serial.c \ + uart_$(UART).c \ lib/utils/printf.c \ lib/utils/stdout_helpers.c \ lib/utils/pyexec.c \ diff --git a/ports/powerpc/README.md b/ports/powerpc/README.md index 862bfcd3c5002..631924c7c0afb 100644 --- a/ports/powerpc/README.md +++ b/ports/powerpc/README.md @@ -6,10 +6,14 @@ potato UART. ## Building -By default the port will be built for the host machine: +By default the port will be built with the potato uart for microwatt: $ make +To instead build for a machine with LPC serial, such as QEMU powernv: + + $ make UART=lpc_serial + ## Cross compilation for POWERPC If you need to cross compilers you'll want to grab a powerpc64le diff --git a/ports/powerpc/main.c b/ports/powerpc/main.c index 421f9be714c90..fdeec13abe0a5 100644 --- a/ports/powerpc/main.c +++ b/ports/powerpc/main.c @@ -65,7 +65,6 @@ int main(int argc, char **argv) { int stack_dummy; stack_top = (char *)&stack_dummy; - // microwatt has argc/r3 = 0 whereas QEMU has r3 set in head.S uart_init_ppc(argc); #if MICROPY_ENABLE_PYSTACK diff --git a/ports/powerpc/uart_core.c b/ports/powerpc/uart_core.c deleted file mode 100644 index b0dcd031a5ad2..0000000000000 --- a/ports/powerpc/uart_core.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019, Michael Neuling, IBM Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -#include "py/mpconfig.h" -#include "uart_potato.h" -#include "uart_lpc_serial.h" - -static int lpc_console; -static int potato_console; - -void uart_init_ppc(int lpc) { - lpc_console = lpc; - - if (!lpc_console) { - potato_console = 1; - - potato_uart_init(); - } else { - lpc_uart_init(); - } -} - -// Receive single character -int mp_hal_stdin_rx_chr(void) { - unsigned char c = 0; - if (lpc_console) { - c = lpc_uart_read(); - } else if (potato_console) { - c = potato_uart_read(); - } - return c; -} - -// Send string of given length -void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { - if (lpc_console) { - int i; - for (i = 0; i < len; i++) { - lpc_uart_write(str[i]); - } - } else if (potato_console) { - int i; - for (i = 0; i < len; i++) { - potato_uart_write(str[i]); - } - } -} diff --git a/ports/powerpc/uart_lpc_serial.c b/ports/powerpc/uart_lpc_serial.c index c15fc049b9d5a..760615041cf94 100644 --- a/ports/powerpc/uart_lpc_serial.c +++ b/ports/powerpc/uart_lpc_serial.c @@ -96,20 +96,24 @@ static int lpc_uart_rx_empty(void) { return !(lpc_uart_reg_read(REG_LSR) & LSR_DR); } -void lpc_uart_init(void) { +void uart_init_ppc(void) { lpc_uart_base = LPC_UART_BASE; } -char lpc_uart_read(void) { +int mp_hal_stdin_rx_chr(void) { while (lpc_uart_rx_empty()) { ; } return lpc_uart_reg_read(REG_THR); } -void lpc_uart_write(char c) { - while (lpc_uart_tx_full()) { - ; + +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + int i; + for (i = 0; i < len; i++) { + while (lpc_uart_tx_full()) { + ; + } + lpc_uart_reg_write(REG_RBR, str[i]); } - lpc_uart_reg_write(REG_RBR, c); } diff --git a/ports/powerpc/uart_potato.c b/ports/powerpc/uart_potato.c index 4a487f30322da..29f21934ddf44 100644 --- a/ports/powerpc/uart_potato.c +++ b/ports/powerpc/uart_potato.c @@ -34,10 +34,12 @@ #include #include "py/mpconfig.h" -#define PROC_FREQ 50000000 +#define SYSCON_BASE 0xc0000000 /* System control regs */ +#define SYS_REG_CLKINFO 0x20 + #define UART_FREQ 115200 #define POTATO_UART_BASE 0xc0002000 -uint64_t potato_uart_base; +static uint64_t potato_uart_base; #define POTATO_CONSOLE_TX 0x00 #define POTATO_CONSOLE_RX 0x08 @@ -60,7 +62,7 @@ static uint64_t potato_uart_reg_read(int offset) { return val; } -void potato_uart_reg_write(int offset, uint64_t val) { +static void potato_uart_reg_write(int offset, uint64_t val) { uint64_t addr; addr = potato_uart_base + offset; @@ -96,13 +98,16 @@ static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long return proc_freq / (uart_freq * 16) - 1; } -void potato_uart_init(void) { +void uart_init_ppc(void) { + uint64_t proc_freq; + potato_uart_base = POTATO_UART_BASE; - potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(PROC_FREQ, UART_FREQ)); + proc_freq = *(volatile uint64_t *)(SYSCON_BASE + SYS_REG_CLKINFO); + potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ)); } -char potato_uart_read(void) { +int mp_hal_stdin_rx_chr(void) { uint64_t val; while (potato_uart_rx_empty()) { @@ -113,13 +118,15 @@ char potato_uart_read(void) { return (char)(val & 0x000000ff); } -void potato_uart_write(char c) { - uint64_t val; +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + int i; - val = c; + for (i = 0; i < len; i++) { + uint64_t val = str[i]; - while (potato_uart_tx_full()) { - ; + while (potato_uart_tx_full()) { + ; + } + potato_uart_reg_write(POTATO_CONSOLE_TX, val); } - potato_uart_reg_write(POTATO_CONSOLE_TX, val); } From 5cfc09ffca3203eb474aec478b8baf1f35fd2a0b Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Tue, 26 May 2020 13:18:41 +0930 Subject: [PATCH 0017/3533] travis: For powerpc job, build both UART variants. The powerpc port can be built with two different UART drivers, so build both in CI. The default compiler is now powerpc64le-linux-gnu- so it does not need to be specified on the command line. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 133035949d484..414fe2b59d108 100644 --- a/.travis.yml +++ b/.travis.yml @@ -355,4 +355,5 @@ jobs: install: - sudo apt-get install gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross script: - - make ${MAKEOPTS} -C ports/powerpc CROSS_COMPILE=powerpc64le-linux-gnu- + - make ${MAKEOPTS} -C ports/powerpc UART=potato + - make ${MAKEOPTS} -C ports/powerpc UART=lpc_serial From 8f642677f724e725813155f74e5934b8c94fc1c4 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 27 May 2020 11:10:40 -0500 Subject: [PATCH 0018/3533] tools/codeformat.py: Add verbose option to pass to uncrustify and black. This adds a new command line option `-v` to `tools/codeformat.py` to enable verbose printing of all files that are scanned. Normally `uncrustify` and `black` are called with the `-q` option so setting verbose suppresses the `-q` option and on `black` also enables the `-v` option which makes it print out all file names matching the filter similar to how `uncrustify` does by default. --- tools/codeformat.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/codeformat.py b/tools/codeformat.py index 2fe72ef33d570..5aef4ccfbf242 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -138,6 +138,7 @@ def main(): cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files.") cmd_parser.add_argument("-c", action="store_true", help="Format C code only") cmd_parser.add_argument("-p", action="store_true", help="Format Python code only") + cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output") cmd_parser.add_argument("files", nargs="*", help="Run on specific globs") args = cmd_parser.parse_args() @@ -168,13 +169,21 @@ def batch(cmd, files, N=200): # Format C files with uncrustify. if format_c: - batch(["uncrustify", "-q", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"], lang_files(C_EXTS)) + command = ["uncrustify", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"] + if not args.v: + command.append("-q") + batch(command, lang_files(C_EXTS)) for file in lang_files(C_EXTS): fixup_c(file) # Format Python files with black. if format_py: - batch(["black", "-q", "--fast", "--line-length=99"], lang_files(PY_EXTS)) + command = ["black", "--fast", "--line-length=99"] + if args.v: + command.append("-v") + else: + command.append("-q") + batch(command, lang_files(PY_EXTS)) if __name__ == "__main__": From 22806ed5df27c10131af0cedb2f7e8b134fe6e7a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 May 2020 12:50:44 +1000 Subject: [PATCH 0019/3533] extmod/vfs: Retain previous working directory if chdir fails. Fixes issue #6069. --- extmod/vfs.c | 3 ++- tests/extmod/vfs_basic.py | 20 +++++++++++++++++++- tests/extmod/vfs_basic.py.exp | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index 79a8e8509ded1..d1291068ad8d6 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -322,7 +322,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { mp_obj_t path_out; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); - MP_STATE_VM(vfs_cur) = vfs; if (vfs == MP_VFS_ROOT) { // If we change to the root dir and a VFS is mounted at the root then // we must change that VFS's current dir to the root dir so that any @@ -334,9 +333,11 @@ mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { break; } } + vfs = MP_VFS_ROOT; } else { mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); } + MP_STATE_VM(vfs_cur) = vfs; return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir); diff --git a/tests/extmod/vfs_basic.py b/tests/extmod/vfs_basic.py index c8e203b3df882..62b2a277381d6 100644 --- a/tests/extmod/vfs_basic.py +++ b/tests/extmod/vfs_basic.py @@ -10,8 +10,9 @@ class Filesystem: - def __init__(self, id): + def __init__(self, id, fail=0): self.id = id + self.fail = fail def mount(self, readonly, mkfs): print(self.id, "mount", readonly, mkfs) @@ -25,6 +26,8 @@ def ilistdir(self, dir): def chdir(self, dir): print(self.id, "chdir", dir) + if self.fail: + raise OSError(self.fail) def getcwd(self): print(self.id, "getcwd") @@ -158,3 +161,18 @@ def open(self, file, mode): uos.umount("/") print(uos.listdir("/")) uos.umount("/mnt") + +# chdir to a non-existent mount point (current directory should remain unchanged) +try: + uos.chdir("/foo") +except OSError: + print("OSError") +print(uos.getcwd()) + +# chdir to a non-existent subdirectory in a mounted filesystem +uos.mount(Filesystem(5, 1), "/mnt") +try: + uos.chdir("/mnt/subdir") +except OSError: + print("OSError") +print(uos.getcwd()) diff --git a/tests/extmod/vfs_basic.py.exp b/tests/extmod/vfs_basic.py.exp index 0ae2c2cc975c4..ebca31030451b 100644 --- a/tests/extmod/vfs_basic.py.exp +++ b/tests/extmod/vfs_basic.py.exp @@ -58,3 +58,9 @@ OSError 3 umount ['mnt'] 4 umount +OSError +/ +5 mount False False +5 chdir /subdir +OSError +/ From 88971342b17f48fb91619620f4c4c1b5da40aaa4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:22:03 +1000 Subject: [PATCH 0020/3533] stm32/machine_uart: Retain attached-to-repl setting when init'ing UART. --- ports/stm32/machine_uart.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 232f3629ee6b9..1cf5817e010f7 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -283,11 +283,17 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const // flow control uint32_t flow = args.flow.u_int; + // Save attach_to_repl setting because uart_init will disable it. + bool attach_to_repl = self->attached_to_repl; + // init UART (if it fails, it's because the port doesn't exist) if (!uart_init(self, baudrate, bits, parity, stop, flow)) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) doesn't exist"), self->uart_id); } + // Restore attach_to_repl setting so UART still works if attached to dupterm. + uart_attach_to_repl(self, attach_to_repl); + // set timeout self->timeout = args.timeout.u_int; From 9ae50d22c90f05bb03c6b6e97df7dffa69628fe7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:22:32 +1000 Subject: [PATCH 0021/3533] stm32/machine_uart: Allow re-init'ing a static UART object. Just disallow changing the rxbuf which will be some static RAM (can't free it and soft-reset would lose any dynamically allocated buffer). --- ports/stm32/machine_uart.c | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 1cf5817e010f7..39a45a2a16060 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -237,11 +237,6 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args); - // static UARTs are used for internal purposes and shouldn't be reconfigured - if (self->is_static) { - mp_raise_ValueError(MP_ERROR_TEXT("UART is static and can't be init'd")); - } - // baudrate uint32_t baudrate = args.baudrate.u_int; @@ -306,20 +301,28 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const self->timeout_char = min_timeout_char; } - // setup the read buffer - m_del(byte, self->read_buf, self->read_buf_len << self->char_width); - if (args.rxbuf.u_int >= 0) { - // rxbuf overrides legacy read_buf_len - args.read_buf_len.u_int = args.rxbuf.u_int; - } - if (args.read_buf_len.u_int <= 0) { - // no read buffer - uart_set_rxbuf(self, 0, NULL); + if (self->is_static) { + // Static UARTs have fixed memory for the rxbuf and can't be reconfigured. + if (args.rxbuf.u_int >= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("UART is static and rxbuf can't be changed")); + } + uart_set_rxbuf(self, self->read_buf_len, self->read_buf); } else { - // read buffer using interrupts - size_t len = args.read_buf_len.u_int + 1; // +1 to adjust for usable length of buffer - uint8_t *buf = m_new(byte, len << self->char_width); - uart_set_rxbuf(self, len, buf); + // setup the read buffer + m_del(byte, self->read_buf, self->read_buf_len << self->char_width); + if (args.rxbuf.u_int >= 0) { + // rxbuf overrides legacy read_buf_len + args.read_buf_len.u_int = args.rxbuf.u_int; + } + if (args.read_buf_len.u_int <= 0) { + // no read buffer + uart_set_rxbuf(self, 0, NULL); + } else { + // read buffer using interrupts + size_t len = args.read_buf_len.u_int + 1; // +1 to adjust for usable length of buffer + uint8_t *buf = m_new(byte, len << self->char_width); + uart_set_rxbuf(self, len, buf); + } } // compute actual baudrate that was configured From 68d053c66eb14b29ea16efba89cf01cb879eca3e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:24:48 +1000 Subject: [PATCH 0022/3533] stm32/modmachine: Allow changing AHB and APB bus frequencies on STM32WB. For now SYSCLK cannot be changed and must remain at 64MHz. --- ports/stm32/modmachine.c | 7 ++++++- ports/stm32/powerctrl.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 0795d2ccf04b8..6fd2488a5ae7b 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -307,7 +307,7 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple); } else { // set - #if defined(STM32F0) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) + #if defined(STM32F0) || defined(STM32L0) || defined(STM32L4) mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); #else mp_int_t sysclk = mp_obj_get_int(args[0]); @@ -317,8 +317,13 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { ahb /= 2; } #endif + #if defined(STM32WB) + mp_int_t apb1 = ahb; + mp_int_t apb2 = ahb; + #else mp_int_t apb1 = ahb / 4; mp_int_t apb2 = ahb / 2; + #endif if (n_args > 1) { ahb = mp_obj_get_int(args[1]); if (n_args > 2) { diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index e11de8b021fd9..946185fba09d3 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -201,7 +201,7 @@ int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk #endif -#if !defined(STM32F0) && !defined(STM32L0) && !defined(STM32L4) && !defined(STM32WB) +#if !defined(STM32F0) && !defined(STM32L0) && !defined(STM32L4) STATIC uint32_t calc_ahb_div(uint32_t wanted_div) { #if defined(STM32H7) @@ -293,6 +293,8 @@ STATIC uint32_t calc_apb2_div(uint32_t wanted_div) { #endif } +#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) + int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t apb2) { // Return straightaway if the clocks are already at the desired frequency if (sysclk == HAL_RCC_GetSysClockFreq() @@ -455,8 +457,36 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t return 0; } +#elif defined(STM32WB) + +int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t apb2) { + // For now it's not supported to change SYSCLK (only bus dividers). + if (sysclk != HAL_RCC_GetSysClockFreq()) { + return -MP_EINVAL; + } + + // Return straightaway if the clocks are already at the desired frequency. + if (ahb == HAL_RCC_GetHCLKFreq() + && apb1 == HAL_RCC_GetPCLK1Freq() + && apb2 == HAL_RCC_GetPCLK2Freq()) { + return 0; + } + + // Calculate and configure the bus clock dividers. + uint32_t cfgr = RCC->CFGR; + cfgr &= ~(7 << RCC_CFGR_PPRE2_Pos | 7 << RCC_CFGR_PPRE1_Pos | 0xf << RCC_CFGR_HPRE_Pos); + cfgr |= calc_ahb_div(sysclk / ahb); + cfgr |= calc_apb1_div(ahb / apb1); + cfgr |= calc_apb2_div(ahb / apb2) << (RCC_CFGR_PPRE2_Pos - RCC_CFGR_PPRE1_Pos); + RCC->CFGR = cfgr; + + return 0; +} + #endif +#endif // !defined(STM32F0) && !defined(STM32L0) && !defined(STM32L4) + void powerctrl_enter_stop_mode(void) { // Disable IRQs so that the IRQ that wakes the device from stop mode is not // executed until after the clocks are reconfigured From 5210fc51ec251e5331cddd305ae0da012e00af3e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:29:11 +1000 Subject: [PATCH 0023/3533] stm32/adc: Add support to pyb.ADC for STM32WB MCUs. --- ports/stm32/adc.c | 37 ++++++++++++-------- ports/stm32/boards/stm32l4xx_hal_conf_base.h | 1 + 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index bdd4ec1aeb8e8..3793ef5ff28a2 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -110,14 +110,14 @@ #define ADC_CAL2 ((uint16_t *)(0x1FF1E840)) #define ADC_CAL_BITS (16) -#elif defined(STM32L4) +#elif defined(STM32L4) || defined(STM32WB) #define ADC_FIRST_GPIO_CHANNEL (1) #define ADC_LAST_GPIO_CHANNEL (16) -#define ADC_SCALE_V (3.0f) -#define ADC_CAL_ADDRESS (0x1fff75aa) -#define ADC_CAL1 ((uint16_t *)(ADC_CAL_ADDRESS - 2)) -#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 0x20)) +#define ADC_SCALE_V (VREFINT_CAL_VREF / 1000.0f) +#define ADC_CAL_ADDRESS (VREFINT_CAL_ADDR) +#define ADC_CAL1 (TEMPSENSOR_CAL1_ADDR) +#define ADC_CAL2 (TEMPSENSOR_CAL2_ADDR) #define ADC_CAL_BITS (12) #else @@ -147,7 +147,8 @@ #elif defined(STM32L432xx) || \ defined(STM32L451xx) || defined(STM32L452xx) || \ defined(STM32L462xx) || defined(STM32L475xx) || \ - defined(STM32L476xx) || defined(STM32L496xx) + defined(STM32L476xx) || defined(STM32L496xx) || \ + defined(STM32WB55xx) #define VBAT_DIV (3) #else #error Unsupported processor @@ -203,6 +204,11 @@ STATIC bool is_adcx_channel(int channel) { ADC_HandleTypeDef handle; handle.Instance = ADCx; return IS_ADC_CHANNEL(&handle, channel); + #elif defined(STM32WB) + ADC_HandleTypeDef handle; + handle.Instance = ADCx; + return __HAL_ADC_IS_CHANNEL_INTERNAL(channel) + || IS_ADC_CHANNEL(&handle, __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel)); #else #error Unsupported processor #endif @@ -212,7 +218,7 @@ STATIC void adc_wait_for_eoc_or_timeout(int32_t timeout) { uint32_t tickstart = HAL_GetTick(); #if defined(STM32F4) || defined(STM32F7) while ((ADCx->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) { - #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) + #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) while (READ_BIT(ADCx->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) { #else #error Unsupported processor @@ -229,7 +235,10 @@ STATIC void adcx_clock_enable(void) { #elif defined(STM32H7) __HAL_RCC_ADC3_CLK_ENABLE(); __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP); - #elif defined(STM32L4) + #elif defined(STM32L4) || defined(STM32WB) + if (__HAL_RCC_GET_ADC_SOURCE() == RCC_ADCCLKSOURCE_NONE) { + __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); + } __HAL_RCC_ADC_CLK_ENABLE(); #else #error Unsupported processor @@ -269,7 +278,7 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) { adch->Init.OversamplingMode = DISABLE; adch->Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; - #elif defined(STM32L4) + #elif defined(STM32L4) || defined(STM32WB) adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; adch->Init.ScanConvMode = ADC_SCAN_DISABLE; adch->Init.LowPowerAutoWait = DISABLE; @@ -286,7 +295,7 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) { #if defined(STM32H7) HAL_ADCEx_Calibration_Start(adch, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); #endif - #if defined(STM32L4) + #if defined(STM32L4) || defined(STM32WB) HAL_ADCEx_Calibration_Start(adch, ADC_SINGLE_ENDED); #endif } @@ -313,7 +322,7 @@ STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) { STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) { ADC_ChannelConfTypeDef sConfig; - #if defined(STM32H7) + #if defined(STM32H7) || defined(STM32WB) sConfig.Rank = ADC_REGULAR_RANK_1; if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel) == 0) { channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel); @@ -337,7 +346,7 @@ STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.OffsetRightShift = DISABLE; sConfig.OffsetSignedSaturation = DISABLE; - #elif defined(STM32L4) + #elif defined(STM32L4) || defined(STM32WB) if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) { sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; } else { @@ -523,7 +532,7 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_ // for subsequent samples we can just set the "start sample" bit #if defined(STM32F4) || defined(STM32F7) ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART; - #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) + #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) SET_BIT(ADCx->CR, ADC_CR_ADSTART); #else #error Unsupported processor @@ -633,7 +642,7 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i // ADC is started: set the "start sample" bit #if defined(STM32F4) || defined(STM32F7) ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART; - #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) + #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) SET_BIT(ADCx->CR, ADC_CR_ADSTART); #else #error Unsupported processor diff --git a/ports/stm32/boards/stm32l4xx_hal_conf_base.h b/ports/stm32/boards/stm32l4xx_hal_conf_base.h index 7e249138f566e..cfffcffbbe134 100644 --- a/ports/stm32/boards/stm32l4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l4xx_hal_conf_base.h @@ -50,6 +50,7 @@ #include "stm32l4xx_hal_uart.h" #include "stm32l4xx_hal_usart.h" #include "stm32l4xx_hal_wwdg.h" +#include "stm32l4xx_ll_adc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED From 0f7b5cceeaf48e0d9710d9a1b1be766018cc812f Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:31:07 +1000 Subject: [PATCH 0024/3533] stm32/machine_adc: Make setting of ADC1_COMMON->CCR clearer on STM32WB. --- ports/stm32/machine_adc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index 5ba000a2338c8..f29896d37cf26 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -132,8 +132,10 @@ STATIC void adc_config(ADC_TypeDef *adc, uint32_t bits) { #elif defined(STM32H7) ADC12_COMMON->CCR = 3 << ADC_CCR_CKMODE_Pos; ADC3_COMMON->CCR = 3 << ADC_CCR_CKMODE_Pos; - #elif defined(STM32L0) || defined(STM32WB) + #elif defined(STM32L0) ADC1_COMMON->CCR = 0; // ADCPR=PCLK/2 + #elif defined(STM32WB) + ADC1_COMMON->CCR = 0 << ADC_CCR_PRESC_Pos | 0 << ADC_CCR_CKMODE_Pos; // PRESC=1, MODE=ASYNC #endif #if defined(STM32H7) || defined(STM32L4) || defined(STM32WB) From c8985d52d3fde45cea2020573a78fecc4d0de142 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:32:03 +1000 Subject: [PATCH 0025/3533] stm32/dma: Add support for DMA on STM32WB, with SPI settings provided. --- ports/stm32/dma.c | 75 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index cbcc8542a19a2..4320315ba4e5d 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -34,18 +34,6 @@ #include "dma.h" #include "irq.h" -#if defined(STM32WB) - -// DMA is currently not implemented for this MCU - -void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, uint32_t dir, void *data) { -} - -void dma_deinit(const dma_descr_t *dma_descr) { -} - -#else - #define DMA_IDLE_ENABLED() (dma_idle.enabled != 0) #define DMA_SYSTICK_LOG2 (3) #define DMA_SYSTICK_MASK ((1 << DMA_SYSTICK_LOG2) - 1) @@ -82,7 +70,7 @@ typedef union { struct _dma_descr_t { #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) DMA_Stream_TypeDef *instance; - #elif defined(STM32F0) || defined(STM32L0) || defined(STM32L4) + #elif defined(STM32F0) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) DMA_Channel_TypeDef *instance; #else #error "Unsupported Processor" @@ -97,7 +85,7 @@ struct _dma_descr_t { static const DMA_InitTypeDef dma_init_struct_spi_i2c = { #if defined(STM32F4) || defined(STM32F7) .Channel = 0, - #elif defined(STM32H7) || defined(STM32L0) || defined(STM32L4) + #elif defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) .Request = 0, #endif .Direction = 0, @@ -120,7 +108,7 @@ static const DMA_InitTypeDef dma_init_struct_spi_i2c = { static const DMA_InitTypeDef dma_init_struct_sdio = { #if defined(STM32F4) || defined(STM32F7) .Channel = 0, - #elif defined(STM32L0) || defined(STM32L4) + #elif defined(STM32L0) || defined(STM32L4) || defined(STM32WB) .Request = 0, #endif .Direction = 0, @@ -130,7 +118,7 @@ static const DMA_InitTypeDef dma_init_struct_sdio = { .MemDataAlignment = DMA_MDATAALIGN_WORD, #if defined(STM32F4) || defined(STM32F7) .Mode = DMA_PFCTRL, - #elif defined(STM32L0) || defined(STM32L4) + #elif defined(STM32L0) || defined(STM32L4) || defined(STM32WB) .Mode = DMA_NORMAL, #endif .Priority = DMA_PRIORITY_VERY_HIGH, @@ -148,7 +136,7 @@ static const DMA_InitTypeDef dma_init_struct_sdio = { static const DMA_InitTypeDef dma_init_struct_dac = { #if defined(STM32F4) || defined(STM32F7) .Channel = 0, - #elif defined(STM32H7) || defined(STM32L0) || defined(STM32L4) + #elif defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) .Request = 0, #endif .Direction = 0, @@ -438,6 +426,40 @@ static const uint8_t dma_irqn[NSTREAM] = { DMA2_Channel7_IRQn, }; +#elif defined(STM32WB) + +#define NCONTROLLERS (2) +#define NSTREAMS_PER_CONTROLLER (7) +#define NSTREAM (NCONTROLLERS * NSTREAMS_PER_CONTROLLER) + +#define DMA_SUB_INSTANCE_AS_UINT8(dma_request) (dma_request) + +#define DMA1_ENABLE_MASK (0x007f) // Bits in dma_enable_mask corresponding to DMA1 +#define DMA2_ENABLE_MASK (0x3f80) // Bits in dma_enable_mask corresponding to DMA2 + +// DMA1 streams +const dma_descr_t dma_SPI_1_RX = { DMA1_Channel1, DMA_REQUEST_SPI1_RX, dma_id_0, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_SPI_1_TX = { DMA1_Channel2, DMA_REQUEST_SPI1_TX, dma_id_1, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_SPI_2_RX = { DMA1_Channel3, DMA_REQUEST_SPI2_RX, dma_id_2, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_SPI_2_TX = { DMA1_Channel4, DMA_REQUEST_SPI2_TX, dma_id_3, &dma_init_struct_spi_i2c }; + +static const uint8_t dma_irqn[NSTREAM] = { + DMA1_Channel1_IRQn, + DMA1_Channel2_IRQn, + DMA1_Channel3_IRQn, + DMA1_Channel4_IRQn, + DMA1_Channel5_IRQn, + DMA1_Channel6_IRQn, + DMA1_Channel7_IRQn, + DMA2_Channel1_IRQn, + DMA2_Channel2_IRQn, + DMA2_Channel3_IRQn, + DMA2_Channel4_IRQn, + DMA2_Channel5_IRQn, + DMA2_Channel6_IRQn, + DMA2_Channel7_IRQn, +}; + #elif defined(STM32H7) #define NCONTROLLERS (2) @@ -717,7 +739,7 @@ void DMA1_Channel4_5_6_7_IRQHandler(void) { IRQ_EXIT(DMA1_Channel4_5_6_7_IRQn); } -#elif defined(STM32L4) +#elif defined(STM32L4) || defined(STM32WB) void DMA1_Channel1_IRQHandler(void) { IRQ_ENTER(DMA1_Channel1_IRQn); @@ -836,6 +858,13 @@ static void dma_enable_clock(dma_id_t dma_id) { dma_enable_mask |= (1 << dma_id); MICROPY_END_ATOMIC_SECTION(irq_state); + #if defined(STM32WB) + // This MCU has a DMAMUX peripheral which needs to be enabled to multiplex the channels. + if (!__HAL_RCC_DMAMUX1_IS_CLK_ENABLED()) { + __HAL_RCC_DMAMUX1_CLK_ENABLE(); + } + #endif + if (dma_id < NSTREAMS_PER_CONTROLLER) { if (((old_enable_mask & DMA1_ENABLE_MASK) == 0) && !DMA1_IS_CLK_ENABLED()) { __HAL_RCC_DMA1_CLK_ENABLE(); @@ -877,7 +906,7 @@ void dma_init_handle(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, uint3 dma->Instance = dma_descr->instance; dma->Init = *dma_descr->init; dma->Init.Direction = dir; - #if defined(STM32L0) || defined(STM32L4) || defined(STM32H7) + #if defined(STM32L0) || defined(STM32L4) || defined(STM32H7) || defined(STM32WB) dma->Init.Request = dma_descr->sub_instance; #else #if !defined(STM32F0) @@ -904,7 +933,7 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, uint32_t dir dma_enable_clock(dma_id); - #if defined(STM32H7) || defined(STM32L0) || defined(STM32L4) + #if defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) // Always reset and configure the H7 and L0/L4 DMA peripheral // (dma->State is set to HAL_DMA_STATE_RESET by memset above) // TODO: understand how L0/L4 DMA works so this is not needed @@ -1062,6 +1091,10 @@ void dma_nohal_start(const dma_descr_t *descr, uint32_t src_addr, uint32_t dst_a dma->CCR |= DMA_CCR_EN; } +#elif defined(STM32WB) + +// These functions are currently not implemented or needed for this MCU. + #else void dma_nohal_init(const dma_descr_t *descr, uint32_t config) { @@ -1135,5 +1168,3 @@ void dma_nohal_start(const dma_descr_t *descr, uint32_t src_addr, uint32_t dst_a } #endif - -#endif // defined(STM32WB) From 246f3f640dd635f67269bfdbebe23408f77cd2b0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Jun 2020 21:38:11 +1000 Subject: [PATCH 0026/3533] stm32/boards/xxx_WB55: Enable pyb.ADC and hardware SPI on WB55 boards. These features are now supported (although machine.ADC is recommended over pyb.ADC). --- ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h | 3 --- ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h index 2992ccce71772..2061349417c64 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h @@ -10,7 +10,6 @@ #define MICROPY_HW_HAS_FLASH (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_RNG (1) -#define MICROPY_HW_ENABLE_ADC (0) #define MICROPY_HW_ENABLE_USB (1) #define MICROPY_HW_HAS_SWITCH (1) @@ -34,7 +33,6 @@ #define MICROPY_HW_I2C3_SDA (pin_C1) // Arduino A1, pin 30 on CN7 // SPI buses -#if 0 // TODO need working DMA #define MICROPY_HW_SPI1_NSS (pin_A4) // Arduino D10 pin 17 on CN10 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 @@ -43,7 +41,6 @@ #define MICROPY_HW_SPI2_SCK (pin_B13) // pin 30 on CN10 #define MICROPY_HW_SPI2_MISO (pin_B14) // pin 28 on CN10 #define MICROPY_HW_SPI2_MOSI (pin_B15) // pin 26 on CN10 -#endif // User switch; pressing the button makes the input go low #define MICROPY_HW_USRSW_PIN (pin_C4) diff --git a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h index 0ff751d613a54..fdb061900b898 100644 --- a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h +++ b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h @@ -10,7 +10,6 @@ #define MICROPY_HW_HAS_FLASH (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_RNG (1) -#define MICROPY_HW_ENABLE_ADC (0) #define MICROPY_HW_ENABLE_USB (1) #define MICROPY_HW_HAS_SWITCH (1) @@ -24,6 +23,11 @@ #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) +// SPI buses +#define MICROPY_HW_SPI1_SCK (pin_A5) // pin 8 on CN1 +#define MICROPY_HW_SPI1_MISO (pin_A6) // pin 9 on CN1 +#define MICROPY_HW_SPI1_MOSI (pin_A7) // pin 10 on CN1 + // User switch; pressing the button makes the input go low #define MICROPY_HW_USRSW_PIN (pin_A10) #define MICROPY_HW_USRSW_PULL (GPIO_PULLUP) From a4086a2f13732c88f81f535982288e6729a9f7db Mon Sep 17 00:00:00 2001 From: David Spickett Date: Fri, 29 May 2020 13:42:08 +0100 Subject: [PATCH 0027/3533] qemu-arm/README: Update link to toolchain. New releases have moved from launchpad to developer.arm.com. --- ports/qemu-arm/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/qemu-arm/README.md b/ports/qemu-arm/README.md index 4f1e79b101772..2c815c54b088a 100644 --- a/ports/qemu-arm/README.md +++ b/ports/qemu-arm/README.md @@ -15,8 +15,9 @@ The purposes of this port are to enable: - no need to use OpenOCD or anything else that might slow down the process in terms of plugging things together, pressing buttons, etc. -This port will only work with with [GCC ARM Embedded](launchpad.net/gcc-arm-embedded) -toolchain and not with CodeSourcery toolchain. You will need to modify +This port will only work with the [GNU ARM Embedded Toolchain]( +https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm) + and not with CodeSourcery toolchain. You will need to modify `LDFLAGS` if you want to use CodeSourcery's version of `arm-none-eabi`. The difference is that CodeSourcery needs `-T generic-m-hosted.ld` while ARM's version requires `--specs=nano.specs --specs=rdimon.specs` to be From da71f55e23a97102d241481e410fdb22d7c55758 Mon Sep 17 00:00:00 2001 From: stinos Date: Fri, 29 May 2020 20:46:46 +0200 Subject: [PATCH 0028/3533] stm32/Makefile: Quote libgcc path so spaces are not an issue. Fixes #3116. --- ports/stm32/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index fdea95e92f5c9..2614d4aa0fcf0 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -124,7 +124,7 @@ endif LDFLAGS = -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Map=$(@:.elf=.map) --cref LDFLAGS += --defsym=_estack_reserve=8 -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 From 203b10703e5df20939b88a2ce29bb961cce7e4b6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 29 May 2020 10:28:38 +1000 Subject: [PATCH 0029/3533] py/modbuiltins: Fix getattr to work with class raising AttributeError. Fixes issue #6089. --- py/modbuiltins.c | 6 +++++- tests/basics/builtin_getattr.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 85d30ab66d052..cfbfc5a25683e 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -558,7 +558,11 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); static inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { mp_obj_t dest[2]; // use load_method, raising or not raising exception - ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest); + if (defval == MP_OBJ_NULL) { + mp_load_method(base, attr, dest); + } else { + mp_load_method_protected(base, attr, dest, false); + } if (dest[0] == MP_OBJ_NULL) { return defval; } else if (dest[1] == MP_OBJ_NULL) { diff --git a/tests/basics/builtin_getattr.py b/tests/basics/builtin_getattr.py index 59cb7e7f7a4b1..afa4ab3293ca6 100644 --- a/tests/basics/builtin_getattr.py +++ b/tests/basics/builtin_getattr.py @@ -16,3 +16,15 @@ def meth(self, i): print(getattr(a, "_none_such", 123)) print(getattr(list, "foo", 456)) print(getattr(a, "va" + "r2")) + +# test a class that defines __getattr__ and may raise AttributeError +class B: + def __getattr__(self, attr): + if attr == "a": + return attr + else: + raise AttributeError +b = B() +print(getattr(b, "a")) +print(getattr(b, "a", "default")) +print(getattr(b, "b", "default")) From b2030e1661ccf58cb0f62cba0c2009f97ebbf455 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 30 May 2020 07:34:40 +1000 Subject: [PATCH 0030/3533] lib/utils/pyexec: Add missing MP_ERROR_TEXT when compiler disabled. --- lib/utils/pyexec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index ec20daff4b23d..2c8ca2de0cecc 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -96,7 +96,7 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); module_fun = mp_compile(&parse_tree, source_name, exec_flags & EXEC_FLAG_IS_REPL); #else - mp_raise_msg(&mp_type_RuntimeError, "script compilation not supported"); + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("script compilation not supported")); #endif } From 8e591d412a9922d71629f4fea74954d547f0c955 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 30 May 2020 07:35:19 +1000 Subject: [PATCH 0031/3533] minimal: Make build more flexible and work as 64-bit build. Changes are: - string0 is no longer built when building for host as the target, because it'll be provided by the system libc and may in some cases clash with the system one (eg on OSX). - mp_int_t/mp_uint_t are defined in terms of intptr_t/uintptr_t to support both 32-bit and 64-bit builds. - Configuration values which are the default in py/mpconfig.h are removed from mpconfigport.h to make the configuration a bit more minimal, eg as a better starting point for new ports. --- ports/minimal/Makefile | 5 ++++- ports/minimal/main.c | 2 ++ ports/minimal/mpconfigport.h | 32 ++------------------------------ 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 5801d64087b21..bce544ec176b5 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -49,10 +49,13 @@ SRC_C = \ lib/utils/printf.c \ lib/utils/stdout_helpers.c \ lib/utils/pyexec.c \ - lib/libc/string0.c \ lib/mp-readline/readline.c \ $(BUILD)/_frozen_mpy.c \ +ifeq ($(CROSS), 1) +SRC_C += lib/libc/string0.c +endif + OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) ifeq ($(CROSS), 1) diff --git a/ports/minimal/main.c b/ports/minimal/main.c index 6943ee7589f39..aa6d10ee3750c 100644 --- a/ports/minimal/main.c +++ b/ports/minimal/main.c @@ -60,6 +60,7 @@ int main(int argc, char **argv) { return 0; } +#if MICROPY_ENABLE_GC void gc_collect(void) { // WARNING: This gc_collect implementation doesn't try to get root // pointers from CPU registers, and thus may function incorrectly. @@ -69,6 +70,7 @@ void gc_collect(void) { gc_collect_end(); gc_dump_info(); } +#endif mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_raise_OSError(MP_ENOENT); diff --git a/ports/minimal/mpconfigport.h b/ports/minimal/mpconfigport.h index 2507d0124d1ca..c3bdf66c10ae2 100644 --- a/ports/minimal/mpconfigport.h +++ b/ports/minimal/mpconfigport.h @@ -11,31 +11,18 @@ #define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool #define MICROPY_ALLOC_PATH_MAX (256) #define MICROPY_ALLOC_PARSE_CHUNK_INIT (16) -#define MICROPY_EMIT_X64 (0) -#define MICROPY_EMIT_THUMB (0) -#define MICROPY_EMIT_INLINE_THUMB (0) -#define MICROPY_COMP_MODULE_CONST (0) #define MICROPY_COMP_CONST (0) #define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) -#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0) -#define MICROPY_MEM_STATS (0) -#define MICROPY_DEBUG_PRINTERS (0) #define MICROPY_ENABLE_GC (1) #define MICROPY_GC_ALLOC_THRESHOLD (0) -#define MICROPY_REPL_EVENT_DRIVEN (0) #define MICROPY_HELPER_REPL (1) -#define MICROPY_HELPER_LEXER_UNIX (0) -#define MICROPY_ENABLE_SOURCE_LINE (0) -#define MICROPY_ENABLE_DOC_STRING (0) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_BUILTINS_BYTEARRAY (0) #define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) -#define MICROPY_PY_BUILTINS_MEMORYVIEW (0) #define MICROPY_PY_BUILTINS_ENUMERATE (0) #define MICROPY_PY_BUILTINS_FILTER (0) -#define MICROPY_PY_BUILTINS_FROZENSET (0) #define MICROPY_PY_BUILTINS_REVERSED (0) #define MICROPY_PY_BUILTINS_SET (0) #define MICROPY_PY_BUILTINS_SLICE (0) @@ -48,34 +35,19 @@ #define MICROPY_PY_ARRAY (0) #define MICROPY_PY_ATTRTUPLE (0) #define MICROPY_PY_COLLECTIONS (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (0) #define MICROPY_PY_STRUCT (0) #define MICROPY_PY_SYS (0) #define MICROPY_MODULE_FROZEN_MPY (1) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_MODULE_GETATTR (0) -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) // type definitions for the specific machine -#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) - -// This port is intended to be 32-bit, but unfortunately, int32_t for -// different targets may be defined in different ways - either as int -// or as long. This requires different printf formatting specifiers -// to print such value. So, we avoid int32_t and use int directly. -#define UINT_FMT "%u" -#define INT_FMT "%d" -typedef int mp_int_t; // must be pointer size -typedef unsigned mp_uint_t; // must be pointer size - +typedef intptr_t mp_int_t; // must be pointer size +typedef uintptr_t mp_uint_t; // must be pointer size typedef long mp_off_t; -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, From e54626f4c1b5cb4de0b5dc86d820bb36c1d6499f Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 1 Jun 2020 16:16:25 +1000 Subject: [PATCH 0032/3533] docs/reference: Add note about multiple exceptions when heap is locked. --- docs/reference/isr_rules.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/reference/isr_rules.rst b/docs/reference/isr_rules.rst index 57690ac212c64..7f466ab42f41c 100644 --- a/docs/reference/isr_rules.rst +++ b/docs/reference/isr_rules.rst @@ -43,6 +43,11 @@ for the purpose. Debugging is simplified if the following code is included in an import micropython micropython.alloc_emergency_exception_buf(100) +The emergency exception buffer can only hold one exception stack trace. This means that if a second exception is +thrown during the handling of an exception while the heap is locked, that second exception's stack trace will +replace the original one - even if the second exception is cleanly handled. This can lead to confusing exception +messages if the buffer is later printed. + Simplicity ~~~~~~~~~~ From 02cc4462b70729a42cb127c840fee891dd08a0d4 Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Sun, 12 Apr 2020 22:00:06 +0200 Subject: [PATCH 0033/3533] mimxrt: Add initial impl of machine.LED class, and basic pin support. This commit implements an LED class with rudimentary parts of a pin C API to support it. The LED class does not yet support setting an intensity. This LED class is put in the machine module for the time being, until a better place is found. One LED is supported on TEENSY40 and MIMXRT1010_EVK boards. --- ports/mimxrt/Makefile | 11 +- ports/mimxrt/board_init.c | 14 --- .../boards/MIMXRT1010_EVK/mpconfigboard.h | 7 +- ports/mimxrt/boards/MIMXRT1010_EVK/pins.c | 33 ++++++ ports/mimxrt/boards/MIMXRT1010_EVK/pins.h | 30 ++++++ ports/mimxrt/boards/TEENSY40/mpconfigboard.h | 7 +- ports/mimxrt/boards/TEENSY40/pins.c | 33 ++++++ ports/mimxrt/boards/TEENSY40/pins.h | 30 ++++++ ports/mimxrt/led.c | 102 ++++++++++++++++++ ports/mimxrt/led.h | 56 ++++++++++ ports/mimxrt/machine_led.c | 91 ++++++++++++++++ ports/mimxrt/main.c | 2 + ports/mimxrt/modmachine.c | 4 + ports/mimxrt/mphalport.h | 4 + ports/mimxrt/pin.c | 37 +++++++ ports/mimxrt/pin.h | 100 +++++++++++++++++ 16 files changed, 540 insertions(+), 21 deletions(-) create mode 100644 ports/mimxrt/boards/MIMXRT1010_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1010_EVK/pins.h create mode 100644 ports/mimxrt/boards/TEENSY40/pins.c create mode 100644 ports/mimxrt/boards/TEENSY40/pins.h create mode 100644 ports/mimxrt/led.c create mode 100644 ports/mimxrt/led.h create mode 100644 ports/mimxrt/machine_led.c create mode 100644 ports/mimxrt/pin.c create mode 100644 ports/mimxrt/pin.h diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index cbd2b8595791a..c4ea70128e0ad 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -88,9 +88,13 @@ SRC_TINYUSB_IMX_C += \ SRC_C = \ main.c \ + led.c \ + pin.c \ tusb_port.c \ board_init.c \ $(BOARD_DIR)/flash_config.c \ + $(BOARD_DIR)/pins.c \ + machine_led.c \ modutime.c \ modmachine.c \ mphalport.c \ @@ -108,7 +112,12 @@ SRC_SS = $(MCU_DIR)/gcc/startup_$(MCU_SERIES).S SRC_S = lib/utils/gchelper_m3.s \ # List of sources for qstr extraction -SRC_QSTR += modutime.c modmachine.c +SRC_QSTR += \ + machine_led.c \ + modutime.c \ + modmachine.c \ + pin.c \ + $(BOARD_DIR)/pins.c \ OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c index a3c27b045bba5..6f5235d3b635b 100644 --- a/ports/mimxrt/board_init.c +++ b/ports/mimxrt/board_init.c @@ -45,8 +45,6 @@ volatile uint32_t systick_ms = 0; const uint8_t dcd_data[] = { 0x00 }; -void board_led_write(bool state); - void board_init(void) { // Init clock BOARD_BootClockRUN(); @@ -58,14 +56,6 @@ void board_init(void) { // 1ms tick timer SysTick_Config(SystemCoreClock / 1000); - // LED - IOMUXC_SetPinMux(MICROPY_HW_LED_PINMUX, 0U); - IOMUXC_SetPinConfig(MICROPY_HW_LED_PINMUX, 0x10B0U); - - gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0, kGPIO_NoIntmode }; - GPIO_PinInit(MICROPY_HW_LED_PORT, MICROPY_HW_LED_PIN, &led_config); - board_led_write(true); - // ------------- USB0 ------------- // // Clock @@ -95,10 +85,6 @@ void board_init(void) { // CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U); } -void board_led_write(bool state) { - GPIO_PinWrite(MICROPY_HW_LED_PORT, MICROPY_HW_LED_PIN, state ? LED_STATE_ON : (1 - LED_STATE_ON)); -} - void SysTick_Handler(void) { systick_ms++; } diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h index 02d34fc42f052..eeddd0e0213fb 100644 --- a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h @@ -3,6 +3,7 @@ #define BOARD_FLASH_SIZE (16 * 1024 * 1024) -#define MICROPY_HW_LED_PINMUX IOMUXC_GPIO_11_GPIOMUX_IO11 -#define MICROPY_HW_LED_PORT GPIO1 -#define MICROPY_HW_LED_PIN 11 +// i.MX RT1010 EVK has 1 board LED +#define MICROPY_HW_LED1_PIN (GPIO_11) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c new file mode 100644 index 0000000000000..e0397e09edb21 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_11_af[] = { + PIN_AF(GPIOMUX_IO11, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), +}; + +pin_obj_t GPIO_11 = PIN(GPIO_11, GPIO1, 5, GPIO_11_af); diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h new file mode 100644 index 0000000000000..99534932a19d5 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_11; diff --git a/ports/mimxrt/boards/TEENSY40/mpconfigboard.h b/ports/mimxrt/boards/TEENSY40/mpconfigboard.h index 10c8b7a6d1ffa..7d564785797ad 100644 --- a/ports/mimxrt/boards/TEENSY40/mpconfigboard.h +++ b/ports/mimxrt/boards/TEENSY40/mpconfigboard.h @@ -3,6 +3,7 @@ #define BOARD_FLASH_SIZE (2 * 1024 * 1024) -#define MICROPY_HW_LED_PINMUX IOMUXC_GPIO_B0_03_GPIO2_IO03 // D13 -#define MICROPY_HW_LED_PORT GPIO2 -#define MICROPY_HW_LED_PIN 3 +// Teensy 4.0 has 1 board LED +#define MICROPY_HW_LED1_PIN (GPIO_B0_03) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/TEENSY40/pins.c b/ports/mimxrt/boards/TEENSY40/pins.c new file mode 100644 index 0000000000000..c7bfc102d4350 --- /dev/null +++ b/ports/mimxrt/boards/TEENSY40/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_B0_03_af[] = { + PIN_AF(GPIO2_IO03, PIN_AF_MODE_ALT5, GPIO2, 0x10B0U), +}; + +pin_obj_t GPIO_B0_03 = PIN(GPIO_B0_03, GPIO2, 3, GPIO_B0_03_af); diff --git a/ports/mimxrt/boards/TEENSY40/pins.h b/ports/mimxrt/boards/TEENSY40/pins.h new file mode 100644 index 0000000000000..c0b6dcdfa9576 --- /dev/null +++ b/ports/mimxrt/boards/TEENSY40/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_B0_03; diff --git a/ports/mimxrt/led.c b/ports/mimxrt/led.c new file mode 100644 index 0000000000000..bec97dd8f7cc4 --- /dev/null +++ b/ports/mimxrt/led.c @@ -0,0 +1,102 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "fsl_gpio.h" +#include "fsl_iomuxc.h" + +#include "py/runtime.h" +#include "py/mphal.h" +#include "led.h" + +#if NUM_LEDS + +const machine_led_obj_t machine_led_obj[NUM_LEDS] = { + { + .base = {&machine_led_type}, + .led_id = 1U, + .led_pin = &MICROPY_HW_LED1_PIN, + } +}; + +void led_init(void) { + // Turn off LEDs and initialize + for (mp_int_t led = 0; led < NUM_LEDS; led++) { + const pin_obj_t *led_pin = machine_led_obj[led].led_pin; + + gpio_pin_config_t pin_config = { + .outputLogic = 1U, + .direction = kGPIO_DigitalOutput, + .interruptMode = kGPIO_NoIntmode, + }; + + GPIO_PinInit(led_pin->gpio, led_pin->pin, &pin_config); + + // ALT mode for GPIO is always 5 + IOMUXC_SetPinMux(led_pin->muxRegister, 5U, 0, 0, led_pin->configRegister, + 1U); // Software Input On Field: Input Path is determined by functionality + IOMUXC_SetPinConfig(led_pin->muxRegister, 5U, 0, 0, led_pin->configRegister, 0x10B0U); + MICROPY_HW_LED_OFF(led_pin); + } +} + +void led_state(machine_led_t led, int state) { + if (led < 1 || led > NUM_LEDS) { + return; + } + + const pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; + + if (state == 0) { + // turn LED off + MICROPY_HW_LED_OFF(led_pin); + } else { + // turn LED on + MICROPY_HW_LED_ON(led_pin); + } +} + +void led_toggle(machine_led_t led) { + if (led < 1 || led > NUM_LEDS) { + return; + } + + const pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; + mp_hal_pin_toggle(led_pin); +} + +void led_debug(int value, int delay) { + for (mp_int_t i = 0; i < NUM_LEDS; i++) { + led_state(i + 1, (value & (1 << i))); + } + mp_hal_delay_ms(delay); +} + +#else + +void led_init(void) { +} + +#endif diff --git a/ports/mimxrt/led.h b/ports/mimxrt/led.h new file mode 100644 index 0000000000000..35418321d173d --- /dev/null +++ b/ports/mimxrt/led.h @@ -0,0 +1,56 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_MIMXRT_LED_H +#define MICROPY_INCLUDED_MIMXRT_LED_H + +#include "pin.h" + +#if defined(MICROPY_HW_LED1_PIN) +#define NUM_LEDS (1) +#else +#define NUM_LEDS (0) +#endif + +typedef enum { + MACHINE_BOARD_LED = 1, +} machine_led_t; + +typedef struct _machine_led_obj_t { + mp_obj_base_t base; + mp_uint_t led_id; + const pin_obj_t *led_pin; +} machine_led_obj_t; + +void led_init(void); +void led_state(machine_led_t led, int state); +void led_toggle(machine_led_t led); +void led_debug(int value, int delay); + +extern const mp_obj_type_t machine_led_type; +extern const machine_led_obj_t machine_led_obj[NUM_LEDS]; + +#endif // MICROPY_INCLUDED_MIMXRT_LED_H diff --git a/ports/mimxrt/machine_led.c b/ports/mimxrt/machine_led.c new file mode 100644 index 0000000000000..07c7b180a10e6 --- /dev/null +++ b/ports/mimxrt/machine_led.c @@ -0,0 +1,91 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "led.h" + +#if NUM_LEDS + +STATIC void led_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + (void)kind; + machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "LED(%u)", self->led_id); +} + +STATIC mp_obj_t led_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // Extract arguments + mp_int_t led_id = mp_obj_get_int(args[0]); + + // Check led id is in range + if (!(1 <= led_id && led_id <= NUM_LEDS)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED(%d) doesn't exist", led_id)); + } + + // Return reference to static object + return MP_OBJ_FROM_PTR(&machine_led_obj[led_id - 1]); +} + +STATIC mp_obj_t led_obj_on(mp_obj_t self_in) { + machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); + MICROPY_HW_LED_ON(self->led_pin); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on); + +STATIC mp_obj_t led_obj_off(mp_obj_t self_in) { + machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); + MICROPY_HW_LED_OFF(self->led_pin); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off); + +STATIC mp_obj_t led_obj_toggle(mp_obj_t self_in) { + machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_hal_pin_toggle(self->led_pin); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle); + +STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { + {MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&led_obj_on_obj)}, + {MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&led_obj_off_obj)}, + {MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&led_obj_toggle_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); + +const mp_obj_type_t machine_led_type = { + {&mp_type_type}, + .name = MP_QSTR_LED, + .print = led_obj_print, + .make_new = led_obj_make_new, + .locals_dict = (mp_obj_dict_t *)&led_locals_dict, +}; + +#endif diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c index d94bf24412452..e06283a8ab37b 100644 --- a/ports/mimxrt/main.c +++ b/ports/mimxrt/main.c @@ -33,6 +33,7 @@ #include "lib/utils/gchelper.h" #include "lib/utils/pyexec.h" #include "tusb.h" +#include "led.h" extern uint8_t _sstack, _estack, _gc_heap_start, _gc_heap_end; @@ -41,6 +42,7 @@ void board_init(void); int main(void) { board_init(); tusb_init(); + led_init(); mp_stack_set_top(&_estack); mp_stack_set_limit(&_estack - &_sstack - 1024); diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index 747b7bc27f56d..a6aab7b4aba53 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -27,6 +27,7 @@ #include "py/runtime.h" #include "extmod/machine_mem.h" +#include "led.h" #include CPU_HEADER_H @@ -48,6 +49,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, + #if NUM_LEDS + { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index e1cf84f6c2cc7..2b9e49432cc0f 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -29,6 +29,10 @@ #include +#define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U)) +#define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U)) +#define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin))) + extern volatile uint32_t systick_ms; void mp_hal_set_interrupt_char(int c); diff --git a/ports/mimxrt/pin.c b/ports/mimxrt/pin.c new file mode 100644 index 0000000000000..b30f4be41ceae --- /dev/null +++ b/ports/mimxrt/pin.c @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +const mp_obj_type_t pin_type = { + .base = {&mp_type_type}, + .name = MP_QSTR_Pin, +}; + +const mp_obj_type_t pin_af_type = { + {&mp_type_type}, + .name = MP_QSTR_PinAF, +}; diff --git a/ports/mimxrt/pin.h b/ports/mimxrt/pin.h new file mode 100644 index 0000000000000..cfb1e6b572502 --- /dev/null +++ b/ports/mimxrt/pin.h @@ -0,0 +1,100 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_MIMXRT_PIN_H +#define MICROPY_INCLUDED_MIMXRT_PIN_H + +#include "fsl_gpio.h" +#include "py/obj.h" + +enum { + PIN_MODE_IN = 0, + PIN_MODE_OUT, + PIN_MODE_ALT, +}; + +enum { + PIN_AF_MODE_ALT1 = 1, + PIN_AF_MODE_ALT2, + PIN_AF_MODE_ALT3, + PIN_AF_MODE_ALT4, + PIN_AF_MODE_ALT5, + PIN_AF_MODE_ALT6, + PIN_AF_MODE_ALT7, + PIN_AF_MODE_ALT8, +}; + +typedef struct { + mp_obj_base_t base; + qstr name; // port name + uint32_t af_mode; // alternate function + void *instance; // pointer to peripheral instance for alternate function + uint32_t pad_config; // pad configuration for alternate function +} pin_af_obj_t; + +typedef struct { + mp_obj_base_t base; + qstr name; // pad name + GPIO_Type *gpio; // gpio instance for pin + uint32_t pin; // pin number + uint32_t muxRegister; + uint32_t configRegister; + uint32_t mode; // current pin mode + uint32_t af_mode; // current alternate function mode + size_t af_list_len; // length of available alternate functions list + const pin_af_obj_t *af_list; // pointer tolist with alternate functions +} pin_obj_t; + +extern const mp_obj_type_t pin_type; +extern const mp_obj_type_t pin_af_type; + +#define PIN_AF(_name, _af_mode, _instance, _pad_config) \ + { \ + .base = { &pin_af_type }, \ + .name = MP_QSTR_##_name, \ + .af_mode = (uint32_t)(_af_mode), \ + .instance = (void *)(_instance), \ + .pad_config = (uint32_t)(_pad_config), \ + } \ + +#define PIN(_name, _gpio, _pin, _af_list) \ + { \ + .base = { &pin_type }, \ + .name = MP_QSTR_##_name, \ + .gpio = (_gpio), \ + .pin = (uint32_t)(_pin), \ + .muxRegister = (uint32_t)&(IOMUXC->SW_MUX_CTL_PAD[kIOMUXC_SW_MUX_CTL_PAD_##_name]), \ + .configRegister = (uint32_t)&(IOMUXC->SW_PAD_CTL_PAD[kIOMUXC_SW_PAD_CTL_PAD_##_name]), \ + .mode = PIN_MODE_IN, \ + .af_mode = PIN_AF_MODE_ALT5, \ + .af_list_len = (size_t)(sizeof((_af_list)) / sizeof(pin_af_obj_t)), \ + .af_list = (_af_list), \ + } \ + +// Include board specific pins +#include "pins.h" + +#endif // MICROPY_INCLUDED_MIMXRT_PIN_H From e6881f08292d03f089185718c131f543d095089b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 11 May 2020 21:20:07 +1000 Subject: [PATCH 0034/3533] extmod/modbluetooth: Make modbluetooth event not a bitfield. There doesn't appear to be any use for only triggering on specific events, so it's just easier to number them sequentially. This makes them smaller values so they take up only 1 byte in the ringbuf, only 1 byte for the opcode in the bytecode, and makes room for more events. Also add a couple of new event types that need to be implemented (to avoid re-numbering later). And rename _COMPLETE and _STATUS to _DONE for consistency. In the future the "trigger" keyword argument can be reinstated by requiring the user to compute the bitmask, eg: ble.irq(handler, 1 << _IRQ_SCAN_RESULT | 1 << _IRQ_SCAN_DONE) --- examples/bluetooth/ble_temperature.py | 4 +- examples/bluetooth/ble_temperature_central.py | 37 +++++---- examples/bluetooth/ble_uart_peripheral.py | 6 +- extmod/modbluetooth.c | 37 ++++----- extmod/modbluetooth.h | 80 ++++++++++--------- extmod/nimble/modbluetooth_nimble.c | 4 +- py/ringbuf.h | 7 ++ 7 files changed, 92 insertions(+), 83 deletions(-) diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py index 01d2f7441fa17..fd24e74d7498a 100644 --- a/examples/bluetooth/ble_temperature.py +++ b/examples/bluetooth/ble_temperature.py @@ -11,8 +11,8 @@ from micropython import const -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) # org.bluetooth.service.environmental_sensing _ENV_SENSE_UUID = bluetooth.UUID(0x181A) diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 04fed0a299078..8b679b9b807be 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -10,22 +10,25 @@ from micropython import const -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_GATTS_WRITE = const(1 << 2) -_IRQ_GATTS_READ_REQUEST = const(1 << 3) -_IRQ_SCAN_RESULT = const(1 << 4) -_IRQ_SCAN_COMPLETE = const(1 << 5) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_SERVICE_RESULT = const(1 << 8) -_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) -_IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10) -_IRQ_GATTC_READ_RESULT = const(1 << 11) -_IRQ_GATTC_WRITE_STATUS = const(1 << 12) -_IRQ_GATTC_NOTIFY = const(1 << 13) -_IRQ_GATTC_INDICATE = const(1 << 14) -_IRQ_ALL = const(0xFFFF) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_SCAN_RESULT = const(5) +_IRQ_SCAN_DONE = const(6) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_DESCRIPTOR_RESULT = const(13) +_IRQ_GATTC_DESCRIPTOR_DONE = const(14) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) +_IRQ_GATTC_INDICATE = const(19) _ADV_IND = const(0x00) _ADV_DIRECT_IND = const(0x01) @@ -93,7 +96,7 @@ def _irq(self, event, data): self._name = decode_name(adv_data) or "?" self._ble.gap_scan(None) - elif event == _IRQ_SCAN_COMPLETE: + elif event == _IRQ_SCAN_DONE: if self._scan_callback: if self._addr: # Found a device during the scan (and the scan was explicitly stopped). diff --git a/examples/bluetooth/ble_uart_peripheral.py b/examples/bluetooth/ble_uart_peripheral.py index c013d96ec9ef1..cc8d589b0e35f 100644 --- a/examples/bluetooth/ble_uart_peripheral.py +++ b/examples/bluetooth/ble_uart_peripheral.py @@ -5,9 +5,9 @@ from micropython import const -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_GATTS_WRITE = const(1 << 2) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) _UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") _UART_TX = ( diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 7e6abb54909ca..b43bec0178df8 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2018 Ayke van Laethem - * Copyright (c) 2019 Jim Mussared + * Copyright (c) 2019-2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -57,7 +57,6 @@ STATIC const mp_obj_type_t bluetooth_uuid_type; typedef struct { mp_obj_base_t base; mp_obj_t irq_handler; - uint16_t irq_trigger; bool irq_scheduled; mp_obj_t irq_data_tuple; uint8_t irq_data_addr_bytes[6]; @@ -249,7 +248,6 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, o->base.type = &bluetooth_ble_type; o->irq_handler = mp_const_none; - o->irq_trigger = 0; // Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler. o->irq_data_tuple = mp_obj_new_tuple(MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN, NULL); @@ -372,10 +370,9 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_config_obj, 1, bluetooth_ble_config); STATIC mp_obj_t bluetooth_ble_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_handler, ARG_trigger }; + enum { ARG_handler }; static const mp_arg_t allowed_args[] = { { MP_QSTR_handler, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, - { MP_QSTR_trigger, MP_ARG_INT, {.u_int = MP_BLUETOOTH_IRQ_ALL} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -388,7 +385,6 @@ STATIC mp_obj_t bluetooth_ble_irq(size_t n_args, const mp_obj_t *pos_args, mp_ma MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); o->irq_handler = callback; - o->irq_trigger = args[ARG_trigger].u_int; MICROPY_PY_BLUETOOTH_EXIT return mp_const_none; @@ -854,7 +850,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { for (;;) { MICROPY_PY_BLUETOOTH_ENTER - mp_int_t event = event = ringbuf_get16(&o->ringbuf); + mp_int_t event = ringbuf_get(&o->ringbuf); if (event < 0) { // Nothing available in ringbuf. MICROPY_PY_BLUETOOTH_EXIT @@ -878,7 +874,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) { // addr_type, addr, adv_type, rssi, adv_data ringbuf_extract(&o->ringbuf, data_tuple, 0, 1, &o->irq_data_addr, 2, NULL, &o->irq_data_data); - } else if (event == MP_BLUETOOTH_IRQ_SCAN_COMPLETE) { + } else if (event == MP_BLUETOOTH_IRQ_SCAN_DONE) { // No params required. data_tuple->len = 0; } else if (event == MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT) { @@ -893,7 +889,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) { // conn_handle, value_handle, data ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data); - } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS) { + } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { // conn_handle, value_handle, status ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE @@ -915,23 +911,24 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_ble_invoke_irq_obj, bluetooth_ble_inv // Callbacks are called in interrupt context (i.e. can't allocate), so we need to push the data // into the ringbuf and schedule the callback via mp_sched_schedule. -STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint16_t event) { - if (!o || !(o->irq_trigger & event) || o->irq_handler == mp_const_none) { +STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint8_t event) { + if (!o || o->irq_handler == mp_const_none) { return false; } - if (ringbuf_free(&o->ringbuf) < len + 2) { + // Check if there is enough room for . + if (ringbuf_free(&o->ringbuf) < len + 1) { // Ringbuffer doesn't have room (and is therefore non-empty). // If this is another scan result, or the front of the ringbuffer isn't a scan result, then nothing to do. - if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT || ringbuf_peek16(&o->ringbuf) != MP_BLUETOOTH_IRQ_SCAN_RESULT) { + if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT || ringbuf_peek(&o->ringbuf) != MP_BLUETOOTH_IRQ_SCAN_RESULT) { return false; } // Front of the queue is a scan result, remove it. // event, addr_type, addr, adv_type, rssi - int n = 2 + 1 + 6 + 1 + 1; + int n = 1 + 1 + 6 + 1 + 1; for (int i = 0; i < n; ++i) { ringbuf_get(&o->ringbuf); } @@ -943,7 +940,7 @@ STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint16_t event) { } // Append this event, the caller will then append the arguments. - ringbuf_put16(&o->ringbuf, event); + ringbuf_put(&o->ringbuf, event); return true; } @@ -959,7 +956,7 @@ STATIC void schedule_ringbuf(mp_uint_t atomic_state) { } } -void mp_bluetooth_gap_on_connected_disconnected(uint16_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr) { +void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); if (enqueue_irq(o, 2 + 1 + 6, event)) { @@ -986,7 +983,7 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { void mp_bluetooth_gap_on_scan_complete(void) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (enqueue_irq(o, 0, MP_BLUETOOTH_IRQ_SCAN_COMPLETE)) { + if (enqueue_irq(o, 0, MP_BLUETOOTH_IRQ_SCAN_DONE)) { } schedule_ringbuf(atomic_state); } @@ -1048,7 +1045,7 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand schedule_ringbuf(atomic_state); } -size_t mp_bluetooth_gattc_on_data_available_start(uint16_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out) { +size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out) { MICROPY_PY_BLUETOOTH_ENTER *atomic_state_out = atomic_state; mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); @@ -1077,7 +1074,7 @@ void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) { void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (enqueue_irq(o, 2 + 2 + 2, MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS)) { + if (enqueue_irq(o, 2 + 2 + 2, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE)) { ringbuf_put16(&o->ringbuf, conn_handle); ringbuf_put16(&o->ringbuf, value_handle); ringbuf_put16(&o->ringbuf, status); @@ -1092,7 +1089,7 @@ void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_han // On ESP32, for example, this is not the case. bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if ((o->irq_trigger & MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST) && o->irq_handler != mp_const_none) { + if (o->irq_handler != mp_const_none) { // Use pre-allocated tuple because this is a hard IRQ. mp_obj_tuple_t *data = MP_OBJ_TO_PTR(o->irq_data_tuple); data->items[0] = MP_OBJ_NEW_SMALL_INT(conn_handle); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 4e658a7a0a426..091a62c845ceb 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2018 Ayke van Laethem - * Copyright (c) 2019 Jim Mussared + * Copyright (c) 2019-2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -88,48 +88,50 @@ #define MP_BLUETOOTH_ADDR_RANDOM_PRIVATE_NON_RESOLVABLE (0x13) // Random private non-resolvable address. (NRF SD 0x03) // Event codes for the IRQ handler. -// Can also be combined to pass to the trigger param to select which events you -// are interested in. -// Note this is currently stored in a uint16_t (in irq_trigger, and the event -// arg to the irq handler), so one spare value remaining. -#define MP_BLUETOOTH_IRQ_CENTRAL_CONNECT (1 << 0) -#define MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT (1 << 1) -#define MP_BLUETOOTH_IRQ_GATTS_WRITE (1 << 2) -#define MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST (1 << 3) -#define MP_BLUETOOTH_IRQ_SCAN_RESULT (1 << 4) -#define MP_BLUETOOTH_IRQ_SCAN_COMPLETE (1 << 5) -#define MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT (1 << 6) -#define MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT (1 << 7) -#define MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT (1 << 8) -#define MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT (1 << 9) -#define MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT (1 << 10) -#define MP_BLUETOOTH_IRQ_GATTC_READ_RESULT (1 << 11) -#define MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS (1 << 12) -#define MP_BLUETOOTH_IRQ_GATTC_NOTIFY (1 << 13) -#define MP_BLUETOOTH_IRQ_GATTC_INDICATE (1 << 14) -#define MP_BLUETOOTH_IRQ_ALL (0xffff) +#define MP_BLUETOOTH_IRQ_CENTRAL_CONNECT (1) +#define MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT (2) +#define MP_BLUETOOTH_IRQ_GATTS_WRITE (3) +#define MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST (4) +#define MP_BLUETOOTH_IRQ_SCAN_RESULT (5) +#define MP_BLUETOOTH_IRQ_SCAN_DONE (6) +#define MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT (7) +#define MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT (8) +#define MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT (9) +#define MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE (10) +#define MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT (11) +#define MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE (12) +#define MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT (13) +#define MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE (14) +#define MP_BLUETOOTH_IRQ_GATTC_READ_RESULT (15) +#define MP_BLUETOOTH_IRQ_GATTC_READ_DONE (16) +#define MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE (17) +#define MP_BLUETOOTH_IRQ_GATTC_NOTIFY (18) +#define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19) /* These aren't included in the module for space reasons, but can be used in your Python code if necessary. from micropython import const -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_GATTS_WRITE = const(1 << 2) -_IRQ_GATTS_READ_REQUEST = const(1 << 3) -_IRQ_SCAN_RESULT = const(1 << 4) -_IRQ_SCAN_COMPLETE = const(1 << 5) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_SERVICE_RESULT = const(1 << 8) -_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) -_IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10) -_IRQ_GATTC_READ_RESULT = const(1 << 11) -_IRQ_GATTC_WRITE_STATUS = const(1 << 12) -_IRQ_GATTC_NOTIFY = const(1 << 13) -_IRQ_GATTC_INDICATE = const(1 << 14) -_IRQ_ALL = const(0xffff) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_SCAN_RESULT = const(5) +_IRQ_SCAN_DONE = const(6) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_DESCRIPTOR_RESULT = const(13) +_IRQ_GATTC_DESCRIPTOR_DONE = const(14) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) +_IRQ_GATTC_INDICATE = const(19) */ // Common UUID type. @@ -238,7 +240,7 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const // API implemented by modbluetooth (called by port-specific implementations): // Notify modbluetooth that a connection/disconnection event has occurred. -void mp_bluetooth_gap_on_connected_disconnected(uint16_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr); +void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr); // Call this when a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); @@ -267,7 +269,7 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand // Notify modbluetooth that a read has completed with data (or notify/indicate data available, use `event` to disambiguate). // Note: these functions are to be called in a group protected by MICROPY_PY_BLUETOOTH_ENTER/EXIT. // _start returns the number of bytes to submit to the calls to _chunk, followed by a call to _end. -size_t mp_bluetooth_gattc_on_data_available_start(uint16_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out); +size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out); void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len); void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e2e95aa74eb6b..656b99a564006 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2019 Damien P. George - * Copyright (c) 2019 Jim Mussared + * Copyright (c) 2019-2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -612,7 +612,7 @@ int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -STATIC void gattc_on_data_available(uint16_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { +STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { size_t len = OS_MBUF_PKTLEN(om); mp_uint_t atomic_state; len = mp_bluetooth_gattc_on_data_available_start(event, conn_handle, value_handle, len, &atomic_state); diff --git a/py/ringbuf.h b/py/ringbuf.h index 293e418306051..4685848961c52 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -63,6 +63,13 @@ static inline int ringbuf_get(ringbuf_t *r) { return v; } +static inline int ringbuf_peek(ringbuf_t *r) { + if (r->iget == r->iput) { + return -1; + } + return r->buf[r->iget]; +} + static inline int ringbuf_put(ringbuf_t *r, uint8_t v) { uint32_t iput_new = r->iput + 1; if (iput_new >= r->size) { From 6a3c89d584db4f14c7e2432b7c5ad87951e6c2d5 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 11 May 2020 21:46:56 +1000 Subject: [PATCH 0035/3533] extmod/modbluetooth: Add discover complete events for svc/char/desc. Without this it's difficult to implement a state machine correctly if the desired services are not found. --- examples/bluetooth/ble_temperature_central.py | 17 +++- extmod/btstack/modbluetooth_btstack.c | 89 +++++++++++++------ extmod/modbluetooth.c | 13 +++ extmod/modbluetooth.h | 3 + extmod/nimble/modbluetooth_nimble.c | 6 ++ 5 files changed, 100 insertions(+), 28 deletions(-) diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 8b679b9b807be..19dc93409e291 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -80,6 +80,8 @@ def _reset(self): # Connected device. self._conn_handle = None + self._start_handle = None + self._end_handle = None self._value_handle = None def _irq(self, event, data): @@ -124,18 +126,31 @@ def _irq(self, event, data): # Connected device returned a service. conn_handle, start_handle, end_handle, uuid = data if conn_handle == self._conn_handle and uuid == _ENV_SENSE_UUID: + self._start_handle, self._end_handle = start_handle, end_handle + + elif event == _IRQ_GATTC_SERVICES_DONE: + # Service query complete. + if self._start_handle and self._end_handle: self._ble.gattc_discover_characteristics( - self._conn_handle, start_handle, end_handle + self._conn_handle, self._start_handle, self._end_handle ) + else: + print("Failed to find environmental sensing service.") elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: # Connected device returned a characteristic. conn_handle, def_handle, value_handle, properties, uuid = data if conn_handle == self._conn_handle and uuid == _TEMP_UUID: self._value_handle = value_handle + + elif event == _IRQ_GATTC_CHARACTERISTICS_DONE: + # Characteristic query complete. + if self._value_handle: # We've finished connecting and discovering device, fire the connect callback. if self._conn_callback: self._conn_callback() + else: + print("Failed to find temperature characteristic.") elif event == _IRQ_GATTC_READ_RESULT: # A read completed successfully. diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 772fe4bed1054..625ccf4c4ad99 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -56,10 +56,17 @@ volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; STATIC btstack_packet_callback_registration_t hci_event_callback_registration; STATIC int btstack_error_to_errno(int err) { + DEBUG_EVENT_printf(" --> btstack error: %d\n", err); if (err == ERROR_CODE_SUCCESS) { return 0; - } else if (err == BTSTACK_ACL_BUFFERS_FULL) { + } else if (err == BTSTACK_ACL_BUFFERS_FULL || err == BTSTACK_MEMORY_ALLOC_FAILED) { return MP_ENOMEM; + } else if (err == GATT_CLIENT_IN_WRONG_STATE) { + return MP_EALREADY; + } else if (err == GATT_CLIENT_BUSY) { + return MP_EBUSY; + } else if (err == GATT_CLIENT_NOT_CONNECTED) { + return MP_ENOTCONN; } else { return MP_EINVAL; } @@ -78,10 +85,8 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uu return result; } -STATIC void btstack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { - (void)channel; - (void)size; - DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, channel=%u, packet=%p, size=%u)\n", packet_type, channel, packet, size); +STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) { + DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); if (packet_type != HCI_EVENT_PACKET) { return; } @@ -157,6 +162,18 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_ #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { DEBUG_EVENT_printf(" --> gatt query complete\n"); + uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); + uint16_t status = gatt_event_query_complete_get_att_status(packet); + if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { + // TODO there is no value_handle available to pass here + mp_bluetooth_gattc_on_read_write_status(irq, conn_handle, 0, status); + } else if (irq == MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE || + irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE || + irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) { + mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status); + } else { + // Note that query complete is fired for other operations like query value too. + } } else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) { DEBUG_EVENT_printf(" --> gatt service query result\n"); uint16_t conn_handle = gatt_event_service_query_result_get_handle(packet); @@ -208,31 +225,50 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_ len = mp_bluetooth_gattc_on_data_available_start(MP_BLUETOOTH_IRQ_GATTC_INDICATE, conn_handle, value_handle, len, &atomic_state); mp_bluetooth_gattc_on_data_available_chunk(data, len); mp_bluetooth_gattc_on_data_available_end(atomic_state); - #endif + #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else { DEBUG_EVENT_printf(" --> hci event type: unknown (0x%02x)\n", event_type); } } +// Because the packet handler callbacks don't support an argument, we use a specific +// handler when we need to provide additional state to the handler (in the "irq" parameter). +// This is the generic handler for when you don't need extra state. +STATIC void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { + (void)channel; + (void)size; + btstack_packet_handler(packet_type, packet, 0); +} + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { +// For when the handler is being used for service discovery. +STATIC void btstack_packet_handler_discover_services(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { (void)channel; (void)size; - DEBUG_EVENT_printf("btstack_packet_handler_write_with_response(packet_type=%u, channel=%u, packet=%p, size=%u)\n", packet_type, channel, packet, size); - if (packet_type != HCI_EVENT_PACKET) { - return; - } + btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE); +} - uint8_t event_type = hci_event_packet_get_type(packet); - if (event_type == GATT_EVENT_QUERY_COMPLETE) { - DEBUG_EVENT_printf(" --> gatt query complete\n"); - uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); - uint8_t status = gatt_event_query_complete_get_att_status(packet); - // TODO there is no value_handle to pass here - mp_bluetooth_gattc_on_write_status(conn_handle, 0, status); - } +// For when the handler is being used for characteristic discovery. +STATIC void btstack_packet_handler_discover_characteristics(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { + (void)channel; + (void)size; + btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE); } -#endif + +// For when the handler is being used for descriptor discovery. +STATIC void btstack_packet_handler_discover_descriptors(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { + (void)channel; + (void)size; + btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE); +} + +// For when the handler is being used for write-with-response. +STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { + (void)channel; + (void)size; + btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE); +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC btstack_timer_source_t btstack_init_deinit_timeout; @@ -285,7 +321,7 @@ int mp_bluetooth_init(void) { #endif // Register for HCI events. - hci_event_callback_registration.callback = &btstack_packet_handler; + hci_event_callback_registration.callback = &btstack_packet_handler_generic; hci_add_event_handler(&hci_event_callback_registration); // Set a timeout for HCI initialisation. @@ -314,7 +350,7 @@ int mp_bluetooth_init(void) { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles. - gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler, GATT_CLIENT_ANY_CONNECTION, NULL); + gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler_generic, GATT_CLIENT_ANY_CONNECTION, NULL); #endif return 0; @@ -643,12 +679,11 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle) { DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_primary_services\n"); - return btstack_error_to_errno(gatt_client_discover_primary_services(&btstack_packet_handler, conn_handle)); + return btstack_error_to_errno(gatt_client_discover_primary_services(&btstack_packet_handler_discover_services, conn_handle)); } int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_characteristics\n"); - gatt_client_service_t service = { // Only start/end handles needed for gatt_client_discover_characteristics_for_service. .start_group_handle = start_handle, @@ -656,7 +691,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s .uuid16 = 0, .uuid128 = {0}, }; - return btstack_error_to_errno(gatt_client_discover_characteristics_for_service(&btstack_packet_handler, conn_handle, &service)); + return btstack_error_to_errno(gatt_client_discover_characteristics_for_service(&btstack_packet_handler_discover_characteristics, conn_handle, &service)); } int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { @@ -670,12 +705,12 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start .uuid16 = 0, .uuid128 = {0}, }; - return btstack_error_to_errno(gatt_client_discover_characteristic_descriptors(&btstack_packet_handler, conn_handle, &characteristic)); + return btstack_error_to_errno(gatt_client_discover_characteristic_descriptors(&btstack_packet_handler_discover_descriptors, conn_handle, &characteristic)); } int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n"); - return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler, conn_handle, value_handle)); + return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_generic, conn_handle, value_handle)); } int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) { diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index b43bec0178df8..7664e182951f2 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -886,6 +886,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT) { // conn_handle, handle, uuid ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, &o->irq_data_uuid, NULL); + } else if (event == MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE || event == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE || event == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) { + // conn_handle, status + ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL); } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) { // conn_handle, value_handle, data ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data); @@ -1045,6 +1048,16 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand schedule_ringbuf(atomic_state); } +void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status) { + MICROPY_PY_BLUETOOTH_ENTER + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (enqueue_irq(o, 2 + 2, event)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, status); + } + schedule_ringbuf(atomic_state); +} + size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out) { MICROPY_PY_BLUETOOTH_ENTER *atomic_state_out = atomic_state; diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 091a62c845ceb..235622389801b 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -266,6 +266,9 @@ void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t // Notify modbluetooth that a descriptor was found. void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t handle, mp_obj_bluetooth_uuid_t *descriptor_uuid); +// Notify modbluetooth that service, characteristic or descriptor discovery has finished. +void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status); + // Notify modbluetooth that a read has completed with data (or notify/indicate data available, use `event` to disambiguate). // Note: these functions are to be called in a group protected by MICROPY_PY_BLUETOOTH_ENTER/EXIT. // _start returns the number of bytes to submit to the calls to _chunk, followed by a call to _end. diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 656b99a564006..abe38d1f182a8 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -763,6 +763,8 @@ STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble if (error->status == 0) { mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(&service->uuid); mp_bluetooth_gattc_on_primary_service_result(conn_handle, service->start_handle, service->end_handle, &service_uuid); + } else { + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE, conn_handle, error->status); } return 0; } @@ -783,6 +785,8 @@ STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gat if (error->status == 0) { mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(&characteristic->uuid); mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic->def_handle, characteristic->val_handle, characteristic->properties, &characteristic_uuid); + } else { + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE, conn_handle, error->status); } return 0; } @@ -803,6 +807,8 @@ STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_er if (error->status == 0) { mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(&descriptor->uuid); mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor->handle, &descriptor_uuid); + } else { + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE, conn_handle, error->status); } return 0; } From 919d640aec637004110d9f4df9814523a3bfd847 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 12 May 2020 23:55:49 +1000 Subject: [PATCH 0036/3533] extmod/modbluetooth: Allow discovery of svc/char by uuid. In most situations this is a more efficient way of going straight to the service and characteristic you need. --- extmod/btstack/modbluetooth_btstack.c | 38 ++++++++++++++++++++++++--- extmod/modbluetooth.c | 22 ++++++++++------ extmod/modbluetooth.h | 4 +-- extmod/nimble/modbluetooth_nimble.c | 36 +++++++++++++++++-------- extmod/nimble/nimble/npl_os.c | 6 +++++ 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 625ccf4c4ad99..112c11a65eac6 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -677,12 +677,27 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, return btstack_error_to_errno(gap_connect(btstack_addr, addr_type)); } -int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle) { +int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) { DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_primary_services\n"); - return btstack_error_to_errno(gatt_client_discover_primary_services(&btstack_packet_handler_discover_services, conn_handle)); + uint8_t err; + if (uuid) { + if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { + err = gatt_client_discover_primary_services_by_uuid16(&btstack_packet_handler_discover_services, conn_handle, get_uuid16(uuid)); + } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) { + uint8_t buffer[16]; + reverse_128(uuid->data, buffer); + err = gatt_client_discover_primary_services_by_uuid128(&btstack_packet_handler_discover_services, conn_handle, buffer); + } else { + DEBUG_EVENT_printf(" --> unknown UUID size\n"); + return MP_EINVAL; + } + } else { + err = gatt_client_discover_primary_services(&btstack_packet_handler_discover_services, conn_handle); + } + return btstack_error_to_errno(err); } -int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { +int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) { DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_characteristics\n"); gatt_client_service_t service = { // Only start/end handles needed for gatt_client_discover_characteristics_for_service. @@ -691,7 +706,22 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s .uuid16 = 0, .uuid128 = {0}, }; - return btstack_error_to_errno(gatt_client_discover_characteristics_for_service(&btstack_packet_handler_discover_characteristics, conn_handle, &service)); + uint8_t err; + if (uuid) { + if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { + err = gatt_client_discover_characteristics_for_service_by_uuid16(&btstack_packet_handler_discover_characteristics, conn_handle, &service, get_uuid16(uuid)); + } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) { + uint8_t buffer[16]; + reverse_128(uuid->data, buffer); + err = gatt_client_discover_characteristics_for_service_by_uuid128(&btstack_packet_handler_discover_characteristics, conn_handle, &service, buffer); + } else { + DEBUG_EVENT_printf(" --> unknown UUID size\n"); + return MP_EINVAL; + } + } else { + err = gatt_client_discover_characteristics_for_service(&btstack_packet_handler_discover_characteristics, conn_handle, &service); + } + return btstack_error_to_errno(err); } int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 7664e182951f2..3d66bc84c3564 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -688,21 +688,27 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_set_buffer_obj, 3 #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -STATIC mp_obj_t bluetooth_ble_gattc_discover_services(mp_obj_t self_in, mp_obj_t conn_handle_in) { - (void)self_in; - mp_int_t conn_handle = mp_obj_get_int(conn_handle_in); - return bluetooth_handle_errno(mp_bluetooth_gattc_discover_primary_services(conn_handle)); +STATIC mp_obj_t bluetooth_ble_gattc_discover_services(size_t n_args, const mp_obj_t *args) { + mp_int_t conn_handle = mp_obj_get_int(args[1]); + mp_obj_bluetooth_uuid_t *uuid = NULL; + if (n_args == 3) { + uuid = MP_OBJ_TO_PTR(args[2]); + } + return bluetooth_handle_errno(mp_bluetooth_gattc_discover_primary_services(conn_handle, uuid)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_discover_services_obj, bluetooth_ble_gattc_discover_services); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_services_obj, 2, 3, bluetooth_ble_gattc_discover_services); STATIC mp_obj_t bluetooth_ble_gattc_discover_characteristics(size_t n_args, const mp_obj_t *args) { - (void)n_args; mp_int_t conn_handle = mp_obj_get_int(args[1]); mp_int_t start_handle = mp_obj_get_int(args[2]); mp_int_t end_handle = mp_obj_get_int(args[3]); - return bluetooth_handle_errno(mp_bluetooth_gattc_discover_characteristics(conn_handle, start_handle, end_handle)); + mp_obj_bluetooth_uuid_t *uuid = NULL; + if (n_args == 3) { + uuid = MP_OBJ_TO_PTR(args[4]); + } + return bluetooth_handle_errno(mp_bluetooth_gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_characteristics_obj, 4, 4, bluetooth_ble_gattc_discover_characteristics); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_characteristics_obj, 4, 5, bluetooth_ble_gattc_discover_characteristics); STATIC mp_obj_t bluetooth_ble_gattc_discover_descriptors(size_t n_args, const mp_obj_t *args) { (void)n_args; diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 235622389801b..5d02d96e3c4c5 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -221,10 +221,10 @@ int mp_bluetooth_gap_scan_stop(void); int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms); // Find all primary services on the connected peripheral. -int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle); +int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid); // Find all characteristics on the specified service on a connected peripheral. -int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle); +int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid); // Find all descriptors on the specified characteristic on a connected peripheral. int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index abe38d1f182a8..4b0f258481c23 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -74,19 +74,19 @@ STATIC int ble_hs_err_to_errno(int err) { } // Note: modbluetooth UUIDs store their data in LE. -STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid) { +STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid, ble_uuid_any_t *storage) { if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { - ble_uuid16_t *result = m_new(ble_uuid16_t, 1); + ble_uuid16_t *result = storage ? &storage->u16 : m_new(ble_uuid16_t, 1); result->u.type = BLE_UUID_TYPE_16; result->value = (uuid->data[1] << 8) | uuid->data[0]; return (ble_uuid_t *)result; } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_32) { - ble_uuid32_t *result = m_new(ble_uuid32_t, 1); + ble_uuid32_t *result = storage ? &storage->u32 : m_new(ble_uuid32_t, 1); result->u.type = BLE_UUID_TYPE_32; result->value = (uuid->data[1] << 24) | (uuid->data[1] << 16) | (uuid->data[1] << 8) | uuid->data[0]; return (ble_uuid_t *)result; } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) { - ble_uuid128_t *result = m_new(ble_uuid128_t, 1); + ble_uuid128_t *result = storage ? &storage->u128 : m_new(ble_uuid128_t, 1); result->u.type = BLE_UUID_TYPE_128; memcpy(result->value, uuid->data, 16); return (ble_uuid_t *)result; @@ -498,7 +498,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m struct ble_gatt_chr_def *characteristics = m_new(struct ble_gatt_chr_def, num_characteristics + 1); for (size_t i = 0; i < num_characteristics; ++i) { - characteristics[i].uuid = create_nimble_uuid(characteristic_uuids[i]); + characteristics[i].uuid = create_nimble_uuid(characteristic_uuids[i], NULL); characteristics[i].access_cb = characteristic_access_cb; characteristics[i].arg = NULL; characteristics[i].flags = characteristic_flags[i]; @@ -512,7 +512,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m struct ble_gatt_dsc_def *descriptors = m_new(struct ble_gatt_dsc_def, num_descriptors[i] + 1); for (size_t j = 0; j < num_descriptors[i]; ++j) { - descriptors[j].uuid = create_nimble_uuid(descriptor_uuids[descriptor_index]); + descriptors[j].uuid = create_nimble_uuid(descriptor_uuids[descriptor_index], NULL); descriptors[j].access_cb = characteristic_access_cb; descriptors[j].att_flags = descriptor_flags[descriptor_index]; descriptors[j].min_key_size = 0; @@ -530,7 +530,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m struct ble_gatt_svc_def *service = m_new(struct ble_gatt_svc_def, 2); service[0].type = BLE_GATT_SVC_TYPE_PRIMARY; - service[0].uuid = create_nimble_uuid(service_uuid); + service[0].uuid = create_nimble_uuid(service_uuid, NULL); service[0].includes = NULL; service[0].characteristics = characteristics; service[1].type = 0; // no more services @@ -769,11 +769,18 @@ STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble return 0; } -int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle) { +int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - int err = ble_gattc_disc_all_svcs(conn_handle, &peripheral_discover_service_cb, NULL); + int err; + if (uuid) { + ble_uuid_any_t nimble_uuid; + create_nimble_uuid(uuid, &nimble_uuid); + err = ble_gattc_disc_svc_by_uuid(conn_handle, &nimble_uuid.u, &peripheral_discover_service_cb, NULL); + } else { + err = ble_gattc_disc_all_svcs(conn_handle, &peripheral_discover_service_cb, NULL); + } return ble_hs_err_to_errno(err); } @@ -791,11 +798,18 @@ STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gat return 0; } -int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { +int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - int err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gatt_characteristic_cb, NULL); + int err; + if (uuid) { + ble_uuid_any_t nimble_uuid; + create_nimble_uuid(uuid, &nimble_uuid); + err = ble_gattc_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, &nimble_uuid.u, &ble_gatt_characteristic_cb, NULL); + } else { + err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gatt_characteristic_cb, NULL); + } return ble_hs_err_to_errno(err); } diff --git a/extmod/nimble/nimble/npl_os.c b/extmod/nimble/nimble/npl_os.c index 620dcb0aec13c..20c260405444f 100644 --- a/extmod/nimble/nimble/npl_os.c +++ b/extmod/nimble/nimble/npl_os.c @@ -127,6 +127,12 @@ void *nimble_realloc(void *ptr, size_t size) { return ptr2; } +// No-op implementation (only used by NimBLE logging). +int nimble_sprintf(char *str, const char *fmt, ...) { + str[0] = 0; + return 0; +} + /******************************************************************************/ // EVENTQ From c07ea3e4c2cfd9fe75a8b207a39d8bb0295fcc3b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 2 Jun 2020 14:22:47 +1000 Subject: [PATCH 0037/3533] extmod/modbluetooth: Implement read done event. On btstack there's no status associated with the read result, it comes through as a separate event. This allows you to detect read failures or timeouts. --- examples/bluetooth/ble_temperature_central.py | 4 ++++ extmod/btstack/modbluetooth_btstack.c | 11 ++++++++--- extmod/modbluetooth.c | 6 +++--- extmod/modbluetooth.h | 4 ++-- extmod/nimble/modbluetooth_nimble.c | 3 ++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 19dc93409e291..6195784622093 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -161,6 +161,10 @@ def _irq(self, event, data): self._read_callback(self._value) self._read_callback = None + elif event == _IRQ_GATTC_READ_DONE: + # Read completed. + conn_handle, value_handle, status = data + elif event == _IRQ_GATTC_NOTIFY: # The ble_temperature.py demo periodically notifies its value. conn_handle, value_handle, notify_data = data diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 112c11a65eac6..31b2825758434 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -171,8 +171,6 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) { mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status); - } else { - // Note that query complete is fired for other operations like query value too. } } else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) { DEBUG_EVENT_printf(" --> gatt service query result\n"); @@ -262,6 +260,13 @@ STATIC void btstack_packet_handler_discover_descriptors(uint8_t packet_type, uin btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE); } +// For when the handler is being used for a read query. +STATIC void btstack_packet_handler_read(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { + (void)channel; + (void)size; + btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_READ_DONE); +} + // For when the handler is being used for write-with-response. STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { (void)channel; @@ -740,7 +745,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n"); - return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_generic, conn_handle, value_handle)); + return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle)); } int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) { diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 3d66bc84c3564..34d18b0b74776 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -898,7 +898,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) { // conn_handle, value_handle, data ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data); - } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { + } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { // conn_handle, value_handle, status ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE @@ -1090,10 +1090,10 @@ void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) { schedule_ringbuf(atomic_state); } -void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status) { +void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (enqueue_irq(o, 2 + 2 + 2, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE)) { + if (enqueue_irq(o, 2 + 2 + 2, event)) { ringbuf_put16(&o->ringbuf, conn_handle); ringbuf_put16(&o->ringbuf, value_handle); ringbuf_put16(&o->ringbuf, status); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 5d02d96e3c4c5..c8b8dc63f2a67 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -276,8 +276,8 @@ size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_h void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len); void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state); -// Notify modbluetooth that a write has completed. -void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_handle, uint16_t status); +// Notify modbluetooth that a read or write operation has completed. +void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status); #endif // For stacks that don't manage attribute value data (currently all of them), helpers diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 4b0f258481c23..9b95d05f32890 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -844,6 +844,7 @@ STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_err if (error->status == 0) { gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om); } + mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr->handle, error->status); return 0; } @@ -861,7 +862,7 @@ STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_er if (!mp_bluetooth_is_active()) { return 0; } - mp_bluetooth_gattc_on_write_status(conn_handle, attr->handle, error->status); + mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE, conn_handle, attr->handle, error->status); return 0; } From 9902ce12eb59e0503072d9d980d5712e32b78fd2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 13 May 2020 14:35:32 +1000 Subject: [PATCH 0038/3533] tests/multi_bluetooth: Update to work with new BLE events. Updates the tests to use non-bitmask events, event renames, as well as some of the new completion events to improve reliability of the tests. --- tests/multi_bluetooth/ble_characteristic.py | 72 +++++++++------- .../multi_bluetooth/ble_characteristic.py.exp | 10 ++- tests/multi_bluetooth/ble_gap_advertise.py | 6 +- tests/multi_bluetooth/ble_gap_connect.py | 55 ++++++------ tests/multi_bluetooth/ble_gap_device_name.py | 53 ++++++------ .../multi_bluetooth/ble_gatt_data_transfer.py | 86 +++++++++++-------- .../ble_gatt_data_transfer.py.exp | 13 +-- .../ble_gattc_discover_services.py | 45 ++++++---- tests/run-multitests.py | 2 +- 9 files changed, 197 insertions(+), 145 deletions(-) diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index b5dfefc840dd6..33d92b823bbb2 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -5,15 +5,17 @@ TIMEOUT_MS = 5000 -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_GATTS_WRITE = const(1 << 2) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) -_IRQ_GATTC_READ_RESULT = const(1 << 11) -_IRQ_GATTC_WRITE_STATUS = const(1 << 12) -_IRQ_GATTC_NOTIFY = const(1 << 13) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") @@ -27,15 +29,13 @@ ) SERVICES = (SERVICE,) -last_event = None -last_data = None +waiting_event = None +waiting_data = None value_handle = 0 def irq(event, data): - global last_event, last_data, value_handle - last_event = event - last_data = data + global waiting_event, waiting_data, value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") elif event == _IRQ_CENTRAL_DISCONNECT: @@ -53,21 +53,32 @@ def irq(event, data): value_handle = data[2] elif event == _IRQ_GATTC_READ_RESULT: print("_IRQ_GATTC_READ_RESULT", data[-1]) - elif event == _IRQ_GATTC_WRITE_STATUS: - print("_IRQ_GATTC_WRITE_STATUS", data[-1]) + elif event == _IRQ_GATTC_READ_DONE: + print("_IRQ_GATTC_READ_DONE", data[-1]) + elif event == _IRQ_GATTC_WRITE_DONE: + print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: print("_IRQ_GATTC_NOTIFY", data[-1]) + if waiting_event is not None: + if (isinstance(waiting_event, int) and event == waiting_event) or ( + not isinstance(waiting_event, int) and waiting_event(event, data) + ): + waiting_event = None + waiting_data = data + def wait_for_event(event, timeout_ms): + global waiting_event, waiting_data + waiting_event = event + waiting_data = None + t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if isinstance(event, int): - if last_event == event: - break - elif event(): - break + if waiting_data: + return True machine.idle() + return False # Acting in peripheral role. @@ -82,17 +93,18 @@ def instance0(): ble.gatts_write(char_handle, "periph0") # Wait for central to connect to us. - wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_CENTRAL_CONNECT: + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data # Wait for a write to the characteristic from the central. wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) # Wait a bit, then write the characteristic and notify it. time.sleep_ms(1000) + print("gatts_write") ble.gatts_write(char_handle, "periph1") + print("gatts_notify") ble.gatts_notify(conn_handle, char_handle) # Wait for a write to the characteristic from the central. @@ -100,6 +112,7 @@ def instance0(): # Wait a bit, then notify a new value on the characteristic. time.sleep_ms(1000) + print("gatts_notify") ble.gatts_notify(conn_handle, char_handle, "periph2") # Wait for the central to disconnect. @@ -115,14 +128,13 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data # Discover characteristics. ble.gattc_discover_characteristics(conn_handle, 1, 65535) - wait_for_event(lambda: value_handle, TIMEOUT_MS) + wait_for_event(lambda event, data: value_handle, TIMEOUT_MS) # Issue read of characteristic, should get initial value. print("gattc_read") @@ -132,7 +144,7 @@ def instance1(): # Write to the characteristic, and ask for a response. print("gattc_write") ble.gattc_write(conn_handle, value_handle, "central0", 1) - wait_for_event(_IRQ_GATTC_WRITE_STATUS, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) # Wait for a notify, then read new value. wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) @@ -143,7 +155,7 @@ def instance1(): # Write to the characteristic, and ask for a response. print("gattc_write") ble.gattc_write(conn_handle, value_handle, "central1", 1) - wait_for_event(_IRQ_GATTC_WRITE_STATUS, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) # Wait for a notify (should have new data), then read old value (should be unchanged). wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp index 1532574d9882c..d89842ef7b097 100644 --- a/tests/multi_bluetooth/ble_characteristic.py.exp +++ b/tests/multi_bluetooth/ble_characteristic.py.exp @@ -2,7 +2,10 @@ gap_advertise _IRQ_CENTRAL_CONNECT _IRQ_GATTS_WRITE b'central0' +gatts_write +gatts_notify _IRQ_GATTS_WRITE b'central1' +gatts_notify _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect @@ -10,15 +13,18 @@ _IRQ_PERIPHERAL_CONNECT _IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000000-1111-2222-3333-444444444444') gattc_read _IRQ_GATTC_READ_RESULT b'periph0' +_IRQ_GATTC_READ_DONE 0 gattc_write -_IRQ_GATTC_WRITE_STATUS 0 +_IRQ_GATTC_WRITE_DONE 0 _IRQ_GATTC_NOTIFY b'periph1' gattc_read _IRQ_GATTC_READ_RESULT b'periph1' +_IRQ_GATTC_READ_DONE 0 gattc_write -_IRQ_GATTC_WRITE_STATUS 0 +_IRQ_GATTC_WRITE_DONE 0 _IRQ_GATTC_NOTIFY b'periph2' gattc_read _IRQ_GATTC_READ_RESULT b'central1' +_IRQ_GATTC_READ_DONE 0 gap_disconnect: True _IRQ_PERIPHERAL_DISCONNECT diff --git a/tests/multi_bluetooth/ble_gap_advertise.py b/tests/multi_bluetooth/ble_gap_advertise.py index 644b1fe28d3e8..71a5b58a56f7a 100644 --- a/tests/multi_bluetooth/ble_gap_advertise.py +++ b/tests/multi_bluetooth/ble_gap_advertise.py @@ -3,8 +3,8 @@ from micropython import const import time, machine, bluetooth -_IRQ_SCAN_RESULT = const(1 << 4) -_IRQ_SCAN_COMPLETE = const(1 << 5) +_IRQ_SCAN_RESULT = const(5) +_IRQ_SCAN_DONE = const(6) ADV_TIME_S = 3 @@ -43,7 +43,7 @@ def irq(ev, data): else: if adv_data != data[4]: adv_data = b"MISMATCH" - elif ev == _IRQ_SCAN_COMPLETE: + elif ev == _IRQ_SCAN_DONE: finished = True ble.config(rxbuf=2000) diff --git a/tests/multi_bluetooth/ble_gap_connect.py b/tests/multi_bluetooth/ble_gap_connect.py index def256f2c8f51..8e3ed8b816b3a 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py +++ b/tests/multi_bluetooth/ble_gap_connect.py @@ -5,19 +5,17 @@ TIMEOUT_MS = 4000 -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) -last_event = None -last_data = None +waiting_event = None +waiting_data = None def irq(event, data): - global last_event, last_data - last_event = event - last_data = data + global waiting_event, waiting_data if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") elif event == _IRQ_CENTRAL_DISCONNECT: @@ -27,11 +25,23 @@ def irq(event, data): elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") + if waiting_event is not None: + if event == waiting_event: + waiting_event = None + waiting_data = data + def wait_for_event(event, timeout_ms): + global waiting_event, waiting_data + waiting_event = event + waiting_data = None + t0 = time.ticks_ms() - while last_event != event and time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if waiting_data: + return True machine.idle() + return False # Acting in peripheral role. @@ -42,20 +52,20 @@ def instance0(): multitest.next() try: # Wait for central to connect, then wait for it to disconnect. - wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) - if last_event != _IRQ_CENTRAL_DISCONNECT: + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + return + if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): return # Start advertising again. ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") # Wait for central to connect, then disconnect it. - wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_CENTRAL_CONNECT: + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + return + print("gap_disconnect:", ble.gap_disconnect(waiting_data[0])) + if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): return - print("gap_disconnect:", ble.gap_disconnect(last_data[0])) - wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) @@ -67,12 +77,10 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return - print("gap_disconnect:", ble.gap_disconnect(last_data[0])) - wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_DISCONNECT: + print("gap_disconnect:", ble.gap_disconnect(waiting_data[0])) + if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): return # Wait for peripheral to start advertising again. @@ -81,8 +89,7 @@ def instance1(): # Connect to peripheral and then let the peripheral disconnect us. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) finally: diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py index ee4a7a544042c..dafa367128b49 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py +++ b/tests/multi_bluetooth/ble_gap_device_name.py @@ -5,24 +5,24 @@ TIMEOUT_MS = 5000 -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) -_IRQ_GATTC_READ_RESULT = const(1 << 11) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) GAP_DEVICE_NAME_UUID = bluetooth.UUID(0x2A00) -last_event = None -last_data = None +waiting_event = None +waiting_data = None value_handle = 0 def irq(event, data): - global last_event, last_data, value_handle - last_event = event - last_data = data + global waiting_event, waiting_data, value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") elif event == _IRQ_CENTRAL_DISCONNECT: @@ -38,16 +38,23 @@ def irq(event, data): elif event == _IRQ_GATTC_READ_RESULT: print("_IRQ_GATTC_READ_RESULT", data[-1]) + if waiting_event is not None: + if isinstance(waiting_event, int) and event == waiting_event: + waiting_event = None + waiting_data = data + def wait_for_event(event, timeout_ms): + global waiting_event, waiting_data + waiting_event = event + waiting_data = None + t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if isinstance(event, int): - if last_event == event: - break - elif event(): - break + if waiting_data: + return True machine.idle() + return False # Acting in peripheral role. @@ -72,9 +79,9 @@ def instance0(): ble.gap_advertise(20_000) # Wait for central to connect, then wait for it to disconnect. - wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS) - if last_event != _IRQ_CENTRAL_DISCONNECT: + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + return + if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS): return finally: ble.active(0) @@ -91,10 +98,9 @@ def instance1(): # Connect to peripheral. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data if iteration == 0: print("gattc_discover_characteristics") @@ -111,8 +117,7 @@ def instance1(): # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) - wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_DISCONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): return finally: ble.active(0) diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py b/tests/multi_bluetooth/ble_gatt_data_transfer.py index 1be557f9b938a..dba0c333be672 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py @@ -5,16 +5,19 @@ TIMEOUT_MS = 5000 -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_GATTS_WRITE = const(1 << 2) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_SERVICE_RESULT = const(1 << 8) -_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) -_IRQ_GATTC_READ_RESULT = const(1 << 11) -_IRQ_GATTC_WRITE_STATUS = const(1 << 12) -_IRQ_GATTC_NOTIFY = const(1 << 13) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) SERVICE_UUID = bluetooth.UUID("00000001-1111-2222-3333-444444444444") CHAR_CTRL_UUID = bluetooth.UUID("00000002-1111-2222-3333-444444444444") @@ -26,17 +29,15 @@ SERVICE = (SERVICE_UUID, (CHAR_CTRL, CHAR_RX, CHAR_TX)) SERVICES = (SERVICE,) -last_event = None -last_data = None +waiting_event = None +waiting_data = None ctrl_value_handle = 0 rx_value_handle = 0 tx_value_handle = 0 def irq(event, data): - global last_event, last_data, ctrl_value_handle, rx_value_handle, tx_value_handle - last_event = event - last_data = data + global waiting_event, waiting_data, ctrl_value_handle, rx_value_handle, tx_value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") elif event == _IRQ_CENTRAL_DISCONNECT: @@ -49,31 +50,44 @@ def irq(event, data): print("_IRQ_PERIPHERAL_DISCONNECT") elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: if data[-1] == CHAR_CTRL_UUID: - print("_IRQ_GATTC_SERVICE_RESULT", data[-1]) + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) ctrl_value_handle = data[2] elif data[-1] == CHAR_RX_UUID: - print("_IRQ_GATTC_SERVICE_RESULT", data[-1]) + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) rx_value_handle = data[2] elif data[-1] == CHAR_TX_UUID: - print("_IRQ_GATTC_SERVICE_RESULT", data[-1]) + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) tx_value_handle = data[2] + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") elif event == _IRQ_GATTC_READ_RESULT: print("_IRQ_GATTC_READ_RESULT", data[-1]) - elif event == _IRQ_GATTC_WRITE_STATUS: - print("_IRQ_GATTC_WRITE_STATUS", data[-1]) + elif event == _IRQ_GATTC_READ_DONE: + print("_IRQ_GATTC_READ_DONE", data[-1]) + elif event == _IRQ_GATTC_WRITE_DONE: + print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: print("_IRQ_GATTC_NOTIFY", data[-1]) + if waiting_event is not None: + if (isinstance(waiting_event, int) and event == waiting_event) or ( + not isinstance(waiting_event, int) and waiting_event(event, data) + ): + waiting_event = None + waiting_data = data + def wait_for_event(event, timeout_ms): + global waiting_event, waiting_data + waiting_event = event + waiting_data = None + t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if isinstance(event, int): - if last_event == event: - break - elif event(): - break + if waiting_data: + return True machine.idle() + return False # Acting in peripheral role. @@ -89,14 +103,13 @@ def instance0(): multitest.next() try: # Wait for central to connect to us. - wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_CENTRAL_CONNECT: + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data # Wait for the central to signal that it's done with its part of the test. wait_for_event( - lambda: last_event == _IRQ_GATTS_WRITE and last_data[1] == char_ctrl_handle, + lambda event, data: event == _IRQ_GATTS_WRITE and data[1] == char_ctrl_handle, 2 * TIMEOUT_MS, ) @@ -125,32 +138,33 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data # Discover characteristics. ble.gattc_discover_characteristics(conn_handle, 1, 65535) wait_for_event( - lambda: ctrl_value_handle and rx_value_handle and tx_value_handle, TIMEOUT_MS + lambda event, data: ctrl_value_handle and rx_value_handle and tx_value_handle, + TIMEOUT_MS, ) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) # Write to the characteristic a few times, with and without response. for i in range(4): print("gattc_write") ble.gattc_write(conn_handle, rx_value_handle, "central{}".format(i), i & 1) if i & 1: - wait_for_event(_IRQ_GATTC_WRITE_STATUS, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) time.sleep_ms(400) # Write to say that we are done with our part of the test. ble.gattc_write(conn_handle, ctrl_value_handle, "OK", 1) - wait_for_event(_IRQ_GATTC_WRITE_STATUS, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) # Wait for notification that peripheral is done with its part of the test. wait_for_event( - lambda: last_event == _IRQ_GATTC_NOTIFY and last_data[1] == ctrl_value_handle, + lambda event, data: event == _IRQ_GATTC_NOTIFY and data[1] == ctrl_value_handle, TIMEOUT_MS, ) diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp b/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp index 6ecd48225f5fe..a1b3cbcd07374 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp @@ -11,16 +11,17 @@ _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect _IRQ_PERIPHERAL_CONNECT -_IRQ_GATTC_SERVICE_RESULT UUID128('00000002-1111-2222-3333-444444444444') -_IRQ_GATTC_SERVICE_RESULT UUID128('00000003-1111-2222-3333-444444444444') -_IRQ_GATTC_SERVICE_RESULT UUID128('00000004-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000002-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000003-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000004-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE gattc_write gattc_write -_IRQ_GATTC_WRITE_STATUS 0 +_IRQ_GATTC_WRITE_DONE 0 gattc_write gattc_write -_IRQ_GATTC_WRITE_STATUS 0 -_IRQ_GATTC_WRITE_STATUS 0 +_IRQ_GATTC_WRITE_DONE 0 +_IRQ_GATTC_WRITE_DONE 0 _IRQ_GATTC_NOTIFY b'message0' _IRQ_GATTC_NOTIFY b'message1' _IRQ_GATTC_NOTIFY b'message2' diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py b/tests/multi_bluetooth/ble_gattc_discover_services.py index b01a890c72038..e746b87458e1b 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py @@ -5,11 +5,12 @@ TIMEOUT_MS = 5000 -_IRQ_CENTRAL_CONNECT = const(1 << 0) -_IRQ_CENTRAL_DISCONNECT = const(1 << 1) -_IRQ_PERIPHERAL_CONNECT = const(1 << 6) -_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) -_IRQ_GATTC_SERVICE_RESULT = const(1 << 8) +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) UUID_A = bluetooth.UUID(0x180D) UUID_B = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") @@ -23,15 +24,13 @@ ) SERVICES = (SERVICE_A, SERVICE_B) -last_event = None -last_data = None +waiting_event = None +waiting_data = None num_service_result = 0 def irq(event, data): - global last_event, last_data, num_service_result - last_event = event - last_data = data + global waiting_event, waiting_data, num_service_result if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") elif event == _IRQ_CENTRAL_DISCONNECT: @@ -45,16 +44,25 @@ def irq(event, data): print("_IRQ_GATTC_SERVICE_RESULT", data[3]) num_service_result += 1 + if waiting_event is not None: + if (isinstance(waiting_event, int) and event == waiting_event) or ( + not isinstance(waiting_event, int) and waiting_event(event, data) + ): + waiting_event = None + waiting_data = data + def wait_for_event(event, timeout_ms): + global waiting_event, waiting_data + waiting_event = event + waiting_data = None + t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if isinstance(event, int): - if last_event == event: - break - elif event(): - break + if waiting_data: + return True machine.idle() + return False # Acting in peripheral role. @@ -78,14 +86,13 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(0, BDADDR) - wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) - if last_event != _IRQ_PERIPHERAL_CONNECT: + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return - conn_handle, _, _ = last_data + conn_handle, _, _ = waiting_data # Discover services. ble.gattc_discover_services(conn_handle) - wait_for_event(lambda: num_service_result == 2, TIMEOUT_MS) + wait_for_event(lambda event, data: num_service_result == 2, TIMEOUT_MS) # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index c83895fb65f37..567e9b5ffa388 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -289,7 +289,7 @@ def run_test_on_instances(test_file, num_instances, instances): continue num_output += 1 last_read_time[idx] = time.time() - if out is not None: + if out is not None and not any(m in out for m in IGNORE_OUTPUT_MATCHES): trace_instance_output(idx, out) output[idx].append(out) if err is not None: From 9708fe8788eebb87bf3b0d6232dbed2012cf7358 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 13 May 2020 16:49:57 +1000 Subject: [PATCH 0039/3533] docs/library: Update ubluetooth for new events and discover by uuid. --- docs/library/ubluetooth.rst | 84 ++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index fd2efe87d913f..ee8fd75ade227 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -65,15 +65,12 @@ Configuration Event Handling -------------- -.. method:: BLE.irq(handler, trigger=0xffff) +.. method:: BLE.irq(handler) Registers a callback for events from the BLE stack. The *handler* takes two arguments, ``event`` (which will be one of the codes below) and ``data`` (which is an event-specific tuple of values). - The optional *trigger* parameter allows you to set a mask of events that - your program is interested in. The default is all events. - Note: the ``addr``, ``adv_data``, ``char_data``, ``notify_data``, and ``uuid`` entries in the tuples are references to data managed by the :mod:`ubluetooth` module (i.e. the same @@ -101,7 +98,7 @@ Event Handling elif event == _IRQ_SCAN_RESULT: # A single scan result. addr_type, addr, adv_type, rssi, adv_data = data - elif event == _IRQ_SCAN_COMPLETE: + elif event == _IRQ_SCAN_DONE: # Scan duration finished or manually stopped. pass elif event == _IRQ_PERIPHERAL_CONNECT: @@ -113,17 +110,31 @@ Event Handling elif event == _IRQ_GATTC_SERVICE_RESULT: # Called for each service found by gattc_discover_services(). conn_handle, start_handle, end_handle, uuid = data + elif event == _IRQ_GATTC_SERVICE_DONE: + # Called once service discovery is complete. + conn_handle, status = data elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: # Called for each characteristic found by gattc_discover_services(). conn_handle, def_handle, value_handle, properties, uuid = data + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + # Called once service discovery is complete. + conn_handle, status = data elif event == _IRQ_GATTC_DESCRIPTOR_RESULT: # Called for each descriptor found by gattc_discover_descriptors(). conn_handle, dsc_handle, uuid = data + elif event == _IRQ_GATTC_DESCRIPTOR_DONE: + # Called once service discovery is complete. + conn_handle, status = data elif event == _IRQ_GATTC_READ_RESULT: # A gattc_read() has completed. conn_handle, value_handle, char_data = data - elif event == _IRQ_GATTC_WRITE_STATUS: + elif event == _IRQ_GATTC_READ_DONE: + # A gattc_read() has completed. + # Note: The value_handle will be zero on btstack (but present on NimBLE). + conn_handle, value_handle, status = data + elif event == _IRQ_GATTC_WRITE_DONE: # A gattc_write() has completed. + # Note: The value_handle will be zero on btstack (but present on NimBLE). conn_handle, value_handle, status = data elif event == _IRQ_GATTC_NOTIFY: # A peripheral has sent a notify request. @@ -135,21 +146,25 @@ Event Handling The event codes are:: from micropython import const - _IRQ_CENTRAL_CONNECT = const(1 << 0) - _IRQ_CENTRAL_DISCONNECT = const(1 << 1) - _IRQ_GATTS_WRITE = const(1 << 2) - _IRQ_GATTS_READ_REQUEST = const(1 << 3) - _IRQ_SCAN_RESULT = const(1 << 4) - _IRQ_SCAN_COMPLETE = const(1 << 5) - _IRQ_PERIPHERAL_CONNECT = const(1 << 6) - _IRQ_PERIPHERAL_DISCONNECT = const(1 << 7) - _IRQ_GATTC_SERVICE_RESULT = const(1 << 8) - _IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9) - _IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10) - _IRQ_GATTC_READ_RESULT = const(1 << 11) - _IRQ_GATTC_WRITE_STATUS = const(1 << 12) - _IRQ_GATTC_NOTIFY = const(1 << 13) - _IRQ_GATTC_INDICATE = const(1 << 14) + _IRQ_CENTRAL_CONNECT = const(1) + _IRQ_CENTRAL_DISCONNECT = const(2) + _IRQ_GATTS_WRITE = const(3) + _IRQ_GATTS_READ_REQUEST = const(4) + _IRQ_SCAN_RESULT = const(5) + _IRQ_SCAN_DONE = const(6) + _IRQ_PERIPHERAL_CONNECT = const(7) + _IRQ_PERIPHERAL_DISCONNECT = const(8) + _IRQ_GATTC_SERVICE_RESULT = const(9) + _IRQ_GATTC_SERVICE_DONE = const(10) + _IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) + _IRQ_GATTC_CHARACTERISTIC_DONE = const(12) + _IRQ_GATTC_DESCRIPTOR_RESULT = const(13) + _IRQ_GATTC_DESCRIPTOR_DONE = const(14) + _IRQ_GATTC_READ_RESULT = const(15) + _IRQ_GATTC_READ_DONE = const(16) + _IRQ_GATTC_WRITE_DONE = const(17) + _IRQ_GATTC_NOTIFY = const(18) + _IRQ_GATTC_INDICATE = const(19) In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your @@ -203,7 +218,7 @@ Observer Role (Scanner) * 0x04 - SCAN_RSP - scan response When scanning is stopped (either due to the duration finishing or when - explicitly stopped), the ``_IRQ_SCAN_COMPLETE`` event will be raised. + explicitly stopped), the ``_IRQ_SCAN_DONE`` event will be raised. Peripheral Role (GATT Server) @@ -313,33 +328,42 @@ Central Role (GATT Client) Returns ``False`` if the connection handle wasn't connected, and ``True`` otherwise. -.. method:: BLE.gattc_discover_services(conn_handle) +.. method:: BLE.gattc_discover_services(conn_handle, [uuid]) Query a connected peripheral for its services. - For each service discovered, the ``_IRQ_GATTC_SERVICE_RESULT`` event will be - raised. + Optionally specify a service *uuid* to query for that service only. -.. method:: BLE.gattc_discover_characteristics(conn_handle, start_handle, end_handle) + For each service discovered, the ``_IRQ_GATTC_SERVICE_RESULT`` event will + be raised, followed by ``_IRQ_GATTC_SERVICE_DONE`` on completion. + +.. method:: BLE.gattc_discover_characteristics(conn_handle, start_handle, end_handle, [uuid]) Query a connected peripheral for characteristics in the specified range. + Optionally specify a characteristic *uuid* to query for that + characteristic only. + + You can use ``start_handle=1``, ``end_handle=0xffff`` to search for a + characteristic in any service. + For each characteristic discovered, the ``_IRQ_GATTC_CHARACTERISTIC_RESULT`` - event will be raised. + event will be raised, followed by ``_IRQ_GATTC_CHARACTERISTIC_DONE`` on completion. .. method:: BLE.gattc_discover_descriptors(conn_handle, start_handle, end_handle) Query a connected peripheral for descriptors in the specified range. For each descriptor discovered, the ``_IRQ_GATTC_DESCRIPTOR_RESULT`` event - will be raised. + will be raised, followed by ``_IRQ_GATTC_DESCRIPTOR_DONE`` on completion. .. method:: BLE.gattc_read(conn_handle, value_handle) Issue a remote read to a connected peripheral for the specified characteristic or descriptor handle. - On success, the ``_IRQ_GATTC_READ_RESULT`` event will be raised. + When a value is available, the ``_IRQ_GATTC_READ_RESULT`` event will be + raised. Additionally, the ``_IRQ_GATTC_READ_DONE`` will be raised. .. method:: BLE.gattc_write(conn_handle, value_handle, data, mode=0, /) @@ -357,7 +381,7 @@ Central Role (GATT Client) data. If a response is received from the remote peripheral the - ``_IRQ_GATTC_WRITE_STATUS`` event will be raised. + ``_IRQ_GATTC_WRITE_DONE`` event will be raised. class UUID From 1cad63c0bcf63b12436a1438aa8512cd05ad4da0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 19 May 2020 16:00:23 +1000 Subject: [PATCH 0040/3533] extmod/modbluetooth: Ensure status=0 always on success. This commit makes sure that all discovery complete and read/write status events set the status to zero on success. The status value will be implementation-dependent on non-success cases. --- docs/library/ubluetooth.rst | 5 +++++ examples/bluetooth/ble_temperature_central.py | 2 +- extmod/nimble/modbluetooth_nimble.c | 7 +++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index ee8fd75ade227..f96a7caaf828e 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -112,18 +112,21 @@ Event Handling conn_handle, start_handle, end_handle, uuid = data elif event == _IRQ_GATTC_SERVICE_DONE: # Called once service discovery is complete. + # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, status = data elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: # Called for each characteristic found by gattc_discover_services(). conn_handle, def_handle, value_handle, properties, uuid = data elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: # Called once service discovery is complete. + # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, status = data elif event == _IRQ_GATTC_DESCRIPTOR_RESULT: # Called for each descriptor found by gattc_discover_descriptors(). conn_handle, dsc_handle, uuid = data elif event == _IRQ_GATTC_DESCRIPTOR_DONE: # Called once service discovery is complete. + # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, status = data elif event == _IRQ_GATTC_READ_RESULT: # A gattc_read() has completed. @@ -131,10 +134,12 @@ Event Handling elif event == _IRQ_GATTC_READ_DONE: # A gattc_read() has completed. # Note: The value_handle will be zero on btstack (but present on NimBLE). + # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, value_handle, status = data elif event == _IRQ_GATTC_WRITE_DONE: # A gattc_write() has completed. # Note: The value_handle will be zero on btstack (but present on NimBLE). + # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, value_handle, status = data elif event == _IRQ_GATTC_NOTIFY: # A peripheral has sent a notify request. diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 6195784622093..1ce99728b37d3 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -162,7 +162,7 @@ def _irq(self, event, data): self._read_callback = None elif event == _IRQ_GATTC_READ_DONE: - # Read completed. + # Read completed (no-op). conn_handle, value_handle, status = data elif event == _IRQ_GATTC_NOTIFY: diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 9b95d05f32890..05ec6117791da 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -764,7 +764,7 @@ STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(&service->uuid); mp_bluetooth_gattc_on_primary_service_result(conn_handle, service->start_handle, service->end_handle, &service_uuid); } else { - mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE, conn_handle, error->status); + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE, conn_handle, error->status == BLE_HS_EDONE ? 0 : error->status); } return 0; } @@ -793,7 +793,7 @@ STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gat mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(&characteristic->uuid); mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic->def_handle, characteristic->val_handle, characteristic->properties, &characteristic_uuid); } else { - mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE, conn_handle, error->status); + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE, conn_handle, error->status == BLE_HS_EDONE ? 0 : error->status); } return 0; } @@ -822,7 +822,7 @@ STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_er mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(&descriptor->uuid); mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor->handle, &descriptor_uuid); } else { - mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE, conn_handle, error->status); + mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE, conn_handle, error->status == BLE_HS_EDONE ? 0 : error->status); } return 0; } @@ -840,7 +840,6 @@ STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_err if (!mp_bluetooth_is_active()) { return 0; } - // TODO: Maybe send NULL if error->status non-zero. if (error->status == 0) { gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om); } From 8b7ae4e099c18d839d1999c49dd8a1795bf9d1ae Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 28 May 2020 10:51:45 +1000 Subject: [PATCH 0041/3533] extmod/modbluetooth: Support bigger characteristic values. The ring buffer previously used a single unsigned byte field to save the length, meaning that it would overflow for large characteristic value responses. With this commit it now use a 16-bit length instead and has code to explicitly truncate at UINT16_MAX (although this should be impossible to achieve in practice). --- extmod/modbluetooth.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 34d18b0b74776..da40a1548541d 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -835,7 +835,7 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size // put more than bt->irq_data_data_alloc bytes into the ringbuf, because // that's what's available here in bt->irq_data_bytes. if (bytes_data) { - bytes_data->len = ringbuf_get(ringbuf); + bytes_data->len = ringbuf_get16(ringbuf); for (size_t i = 0; i < bytes_data->len; ++i) { // cast away const, this is actually bt->irq_data_bytes. ((uint8_t *)bytes_data->data)[i] = ringbuf_get(ringbuf); @@ -1001,7 +1001,7 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uin MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); data_len = MIN(o->irq_data_data_alloc, data_len); - if (enqueue_irq(o, 1 + 6 + 1 + 1 + 1 + data_len, MP_BLUETOOTH_IRQ_SCAN_RESULT)) { + if (enqueue_irq(o, 1 + 6 + 1 + 1 + 2 + data_len, MP_BLUETOOTH_IRQ_SCAN_RESULT)) { ringbuf_put(&o->ringbuf, addr_type); for (int i = 0; i < 6; ++i) { ringbuf_put(&o->ringbuf, addr[i]); @@ -1010,7 +1010,9 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uin ringbuf_put(&o->ringbuf, adv_type); // Note conversion of int8_t rssi to uint8_t. Must un-convert on the way out. ringbuf_put(&o->ringbuf, (uint8_t)rssi); - ringbuf_put(&o->ringbuf, data_len); + // Length field is 16-bit. + data_len = MIN(UINT16_MAX, data_len); + ringbuf_put16(&o->ringbuf, data_len); for (size_t i = 0; i < data_len; ++i) { ringbuf_put(&o->ringbuf, data[i]); } @@ -1069,10 +1071,12 @@ size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_h *atomic_state_out = atomic_state; mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); data_len = MIN(o->irq_data_data_alloc, data_len); - if (enqueue_irq(o, 2 + 2 + 1 + data_len, event)) { + if (enqueue_irq(o, 2 + 2 + 2 + data_len, event)) { ringbuf_put16(&o->ringbuf, conn_handle); ringbuf_put16(&o->ringbuf, value_handle); - ringbuf_put(&o->ringbuf, data_len); + // Length field is 16-bit. + data_len = MIN(UINT16_MAX, data_len); + ringbuf_put16(&o->ringbuf, data_len); return data_len; } else { return 0; From 834b482e67ebb1b9ad250d62ddfa1275824a7e19 Mon Sep 17 00:00:00 2001 From: jxltom Date: Sat, 9 May 2020 14:34:15 +0800 Subject: [PATCH 0042/3533] examples/bluetooth: Fix incorrect value of BR/EDR flag in advertising. According to Supplement to the Bluetooth Core Specification v8 Part A 1.3.1, to support BR/EDR the code should set the fifth bit (Simultaneous LE and BR/EDR to Same Device Capable (Controller)) and fourth bit (Simultaneous LE and BR/EDR to Same Device Capable (Host)) of the flag. --- examples/bluetooth/ble_advertising.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/bluetooth/ble_advertising.py b/examples/bluetooth/ble_advertising.py index 3fb1281f636ee..a496119967a5d 100644 --- a/examples/bluetooth/ble_advertising.py +++ b/examples/bluetooth/ble_advertising.py @@ -30,7 +30,7 @@ def _append(adv_type, value): _append( _ADV_TYPE_FLAGS, - struct.pack("B", (0x01 if limited_disc else 0x02) + (0x00 if br_edr else 0x04)), + struct.pack("B", (0x01 if limited_disc else 0x02) + (0x18 if br_edr else 0x04)), ) if name: From 596fb73927df4f9202729ff9d92a1db479a95ed7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Jun 2020 16:24:53 +1000 Subject: [PATCH 0043/3533] qemu-arm: Support building in debug mode with DEBUG=1. Fixes issue #6095. --- ports/qemu-arm/Makefile | 4 ++-- ports/qemu-arm/startup.c | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ports/qemu-arm/Makefile b/ports/qemu-arm/Makefile index 430740ccf73f7..e7b6523f88c2f 100644 --- a/ports/qemu-arm/Makefile +++ b/ports/qemu-arm/Makefile @@ -43,9 +43,9 @@ INC += -I$(BUILD) CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 $(COPT) \ -ffunction-sections -fdata-sections -#Debugging/Optimization +# Debugging/Optimization ifeq ($(DEBUG), 1) -CFLAGS += -g -DPENDSV_DEBUG +CFLAGS += -g COPT = -O0 else COPT += -Os -DNDEBUG diff --git a/ports/qemu-arm/startup.c b/ports/qemu-arm/startup.c index 002d5ef62418a..58bdf7af9e98e 100644 --- a/ports/qemu-arm/startup.c +++ b/ports/qemu-arm/startup.c @@ -1,4 +1,5 @@ #include +#include #include #include "uart.h" @@ -73,6 +74,14 @@ __attribute__((naked)) void exit(int status) { } } +#ifndef NDEBUG +void __assert_func(const char *file, int line, const char *func, const char *expr) { + (void)func; + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); + exit(1); +} +#endif + // The following are needed for tinytest #include From 621f40b12c272788533dcafcef62ed2a3449bb3b Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Jun 2020 16:42:37 +1000 Subject: [PATCH 0044/3533] esp32/mpthreadport: Fix calculation of thread stack size. With this commit the code should work correctly regardless of the size of StackType_t (it's actually 1 byte in size for the esp32's custom FreeRTOS). Fixes issue #6072. --- ports/esp32/main.c | 5 ++--- ports/esp32/mpthreadport.c | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 81e4a64346180..6f9ab82d0512a 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -65,7 +65,6 @@ // MicroPython runs as a task under FreeRTOS #define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1) #define MP_TASK_STACK_SIZE (16 * 1024) -#define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t)) int vprintf_null(const char *format, va_list ap) { // do nothing: this is used as a log target during raw repl mode @@ -75,7 +74,7 @@ int vprintf_null(const char *format, va_list ap) { void mp_task(void *pvParameter) { volatile uint32_t sp = (uint32_t)get_sp(); #if MICROPY_PY_THREAD - mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_LEN); + mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif uart_init(); @@ -169,7 +168,7 @@ void app_main(void) { nvs_flash_erase(); nvs_flash_init(); } - xTaskCreatePinnedToCore(mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, &mp_main_task_handle, MP_TASK_COREID); + xTaskCreatePinnedToCore(mp_task, "mp_task", MP_TASK_STACK_SIZE / sizeof(StackType_t), NULL, MP_TASK_PRIORITY, &mp_main_task_handle, MP_TASK_COREID); } void nlr_jump_fail(void *val) { diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index f39c99b68d41a..d294c92727bf9 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -83,7 +83,7 @@ void mp_thread_gc_others(void) { if (!th->ready) { continue; } - gc_collect_root(th->stack, th->stack_len); // probably not needed + gc_collect_root(th->stack, th->stack_len); } mp_thread_mutex_unlock(&thread_mutex); } @@ -140,17 +140,17 @@ void mp_thread_create_ex(void *(*entry)(void *), void *arg, size_t *stack_size, mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("can't create thread")); } - // adjust the stack_size to provide room to recover from hitting the limit - *stack_size -= 1024; - // add thread to linked list of all threads th->ready = 0; th->arg = arg; th->stack = pxTaskGetStackStart(th->id); - th->stack_len = *stack_size / sizeof(StackType_t); + th->stack_len = *stack_size / sizeof(uintptr_t); th->next = thread; thread = th; + // adjust the stack_size to provide room to recover from hitting the limit + *stack_size -= 1024; + mp_thread_mutex_unlock(&thread_mutex); } From eeca2c3cbe84975ffabd7fda932b2f5674e8b0fc Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 3 Jun 2020 18:44:27 -0500 Subject: [PATCH 0045/3533] github: Add GitHub action to build docs. This builds docs, but only on pull requests that change a file in the docs/ directory. --- .github/workflows/docs.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/docs.yml diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000000000..ec6b4c7f1962a --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,18 @@ +name: Build docs + +on: + pull_request: + paths: + - docs/** + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v1 + - name: Install Python packages + run: pip install Sphinx + - name: Build docs + run: make -C docs/ html From 1e6d18c915ccea0b6a19ffec9710d33dd7e5f866 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 3 Jun 2020 20:38:45 -0500 Subject: [PATCH 0046/3533] docs: Fix Sphinx 3.x warnings, and enable warnings-as-errors on build. This enables warnings as errors and fixes all current errors, namely: - reference to terms in the glossary must now be explicit (:term:) - method overloads must not be declared as a separate method or must use :noindex: - 2 cases where `` should have been used instead of ` --- docs/Makefile | 2 +- docs/library/esp32.rst | 4 ++-- docs/library/index.rst | 4 ++-- docs/library/micropython.rst | 2 +- docs/library/network.WLAN.rst | 4 ++-- docs/library/network.rst | 2 +- docs/library/pyb.Flash.rst | 5 +++-- docs/library/uerrno.rst | 4 ++-- docs/library/uio.rst | 2 ++ docs/library/uos.rst | 4 ++-- docs/library/ure.rst | 10 +++++----- docs/library/usocket.rst | 20 ++++++++++---------- docs/library/ussl.rst | 2 +- docs/make.bat | 1 + docs/reference/packages.rst | 34 +++++++++++++++++----------------- docs/reference/pyboard.py.rst | 2 +- docs/templates/replace.inc | 2 +- 17 files changed, 54 insertions(+), 50 deletions(-) diff --git a/docs/Makefile b/docs/Makefile index e9c128e900788..05709617c35b9 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -3,7 +3,7 @@ # You can set these variables from the command line. PYTHON = python3 -SPHINXOPTS = +SPHINXOPTS = -W --keep-going SPHINXBUILD = sphinx-build PAPER = BUILDDIR = build/$(MICROPY_PORT) diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index f3be3692e3267..33b779a214336 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -84,9 +84,9 @@ methods to enable over-the-air (OTA) updates. Returns a 6-tuple ``(type, subtype, addr, size, label, encrypted)``. .. method:: Partition.readblocks(block_num, buf) -.. method:: Partition.readblocks(block_num, buf, offset) + Partition.readblocks(block_num, buf, offset) .. method:: Partition.writeblocks(block_num, buf) -.. method:: Partition.writeblocks(block_num, buf, offset) + Partition.writeblocks(block_num, buf, offset) .. method:: Partition.ioctl(cmd, arg) These methods implement the simple and :ref:`extended diff --git a/docs/library/index.rst b/docs/library/index.rst index 7b1636ce61b33..952e67d43ccec 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -23,7 +23,7 @@ into MicroPython. There are a few categories of such modules: * Modules which implement a subset of Python functionality, with a provision for extension by the user (via Python code). * Modules which implement MicroPython extensions to the Python standard libraries. -* Modules specific to a particular `MicroPython port` and thus not portable. +* Modules specific to a particular :term:`MicroPython port` and thus not portable. Note about the availability of the modules and their contents: This documentation in general aspires to describe all modules and functions/classes which are @@ -38,7 +38,7 @@ in a module (or even the entire module) described in this documentation **may be unavailable** in a particular build of MicroPython on a particular system. The best place to find general information of the availability/non-availability of a particular feature is the "General Information" section which contains -information pertaining to a specific `MicroPython port`. +information pertaining to a specific :term:`MicroPython port`. On some ports you are able to discover the available, built-in libraries that can be imported by entering the following at the REPL:: diff --git a/docs/library/micropython.rst b/docs/library/micropython.rst index ded52deb0d268..7106a1a2ff16d 100644 --- a/docs/library/micropython.rst +++ b/docs/library/micropython.rst @@ -100,7 +100,7 @@ Functions unlocked. Note: `heap_locked()` is not enabled on most ports by default, - requires `MICROPY_PY_MICROPYTHON_HEAP_LOCKED`. + requires ``MICROPY_PY_MICROPYTHON_HEAP_LOCKED``. .. function:: kbd_intr(chr) diff --git a/docs/library/network.WLAN.rst b/docs/library/network.WLAN.rst index 46a27a8fe462d..885a4460c56d4 100644 --- a/docs/library/network.WLAN.rst +++ b/docs/library/network.WLAN.rst @@ -101,7 +101,7 @@ Methods nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')) .. method:: WLAN.config('param') -.. method:: WLAN.config(param=value, ...) + WLAN.config(param=value, ...) Get or set general network interface parameters. These methods allow to work with additional parameters beyond standard IP configuration (as dealt with by @@ -117,7 +117,7 @@ Methods print(ap.config('channel')) Following are commonly supported parameters (availability of a specific parameter - depends on network technology type, driver, and `MicroPython port`). + depends on network technology type, driver, and :term:`MicroPython port`). ============= =========== Parameter Description diff --git a/docs/library/network.rst b/docs/library/network.rst index 01e6f61369a40..208c58f852e6b 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -39,7 +39,7 @@ Common network adapter interface ================================ This section describes an (implied) abstract base class for all network -interface classes implemented by `MicroPython ports ` +interface classes implemented by :term:`MicroPython ports ` for different hardware. This means that MicroPython does not actually provide ``AbstractNIC`` class, but any actual NIC class, as described in the following sections, implements methods as described here. diff --git a/docs/library/pyb.Flash.rst b/docs/library/pyb.Flash.rst index 2d2f83da1c6ad..13f2097871913 100644 --- a/docs/library/pyb.Flash.rst +++ b/docs/library/pyb.Flash.rst @@ -26,6 +26,7 @@ Constructors This constructor is deprecated and will be removed in a future version of MicroPython. .. class:: pyb.Flash(\*, start=-1, len=-1) + :noindex: Create and return a block device that accesses the flash at the specified offset. The length defaults to the remaining size of the device. @@ -35,9 +36,9 @@ Methods ------- .. method:: Flash.readblocks(block_num, buf) -.. method:: Flash.readblocks(block_num, buf, offset) + Flash.readblocks(block_num, buf, offset) .. method:: Flash.writeblocks(block_num, buf) -.. method:: Flash.writeblocks(block_num, buf, offset) + Flash.writeblocks(block_num, buf, offset) .. method:: Flash.ioctl(cmd, arg) These methods implement the simple and :ref:`extended diff --git a/docs/library/uerrno.rst b/docs/library/uerrno.rst index e336eb5c5c85c..def01362f1387 100644 --- a/docs/library/uerrno.rst +++ b/docs/library/uerrno.rst @@ -7,7 +7,7 @@ |see_cpython_module| :mod:`python:errno`. This module provides access to symbolic error codes for `OSError` exception. -A particular inventory of codes depends on `MicroPython port`. +A particular inventory of codes depends on :term:`MicroPython port`. Constants --------- @@ -16,7 +16,7 @@ Constants Error codes, based on ANSI C/POSIX standard. All error codes start with "E". As mentioned above, inventory of the codes depends on - `MicroPython port`. Errors are usually accessible as ``exc.args[0]`` + :term:`MicroPython port`. Errors are usually accessible as ``exc.args[0]`` where ``exc`` is an instance of `OSError`. Usage example:: try: diff --git a/docs/library/uio.rst b/docs/library/uio.rst index 1a64b36582e52..dddb83a170750 100644 --- a/docs/library/uio.rst +++ b/docs/library/uio.rst @@ -114,7 +114,9 @@ Classes Get the current contents of the underlying buffer which holds data. .. class:: StringIO(alloc_size) + :noindex: .. class:: BytesIO(alloc_size) + :noindex: Create an empty `StringIO`/`BytesIO` object, preallocated to hold up to *alloc_size* number of bytes. That means that writing that amount diff --git a/docs/library/uos.rst b/docs/library/uos.rst index c49b13a5f5bca..ad10d9129713d 100644 --- a/docs/library/uos.rst +++ b/docs/library/uos.rst @@ -252,7 +252,7 @@ that the block device supports the extended interface. dependent on the specific block device. .. method:: readblocks(block_num, buf) - .. method:: readblocks(block_num, buf, offset) + readblocks(block_num, buf, offset) The first form reads aligned, multiples of blocks. Starting at the block given by the index *block_num*, read blocks from @@ -267,7 +267,7 @@ that the block device supports the extended interface. The number of bytes to read is given by the length of *buf*. .. method:: writeblocks(block_num, buf) - .. method:: writeblocks(block_num, buf, offset) + writeblocks(block_num, buf, offset) The first form writes aligned, multiples of blocks, and requires that the blocks that are written to be first erased (if necessary) by this method. diff --git a/docs/library/ure.rst b/docs/library/ure.rst index ca5f35b70302c..e94d286175cef 100644 --- a/docs/library/ure.rst +++ b/docs/library/ure.rst @@ -138,12 +138,12 @@ Functions If *count* is specified and non-zero then substitution will stop after this many substitutions are made. The *flags* argument is ignored. - Note: availability of this function depends on `MicroPython port`. + Note: availability of this function depends on :term:`MicroPython port`. .. data:: DEBUG Flag value, display debug information about compiled expression. - (Availability depends on `MicroPython port`.) + (Availability depends on :term:`MicroPython port`.) .. _regex: @@ -184,7 +184,7 @@ to the replacement function in `sub()`. Return a tuple containing all the substrings of the groups of the match. - Note: availability of this method depends on `MicroPython port`. + Note: availability of this method depends on :term:`MicroPython port`. .. method:: match.start([index]) match.end([index]) @@ -193,10 +193,10 @@ to the replacement function in `sub()`. substring group that was matched. *index* defaults to the entire group, otherwise it will select a group. - Note: availability of these methods depends on `MicroPython port`. + Note: availability of these methods depends on :term:`MicroPython port`. .. method:: match.span([index]) Returns the 2-tuple ``(match.start(index), match.end(index))``. - Note: availability of this method depends on `MicroPython port`. + Note: availability of this method depends on :term:`MicroPython port`. diff --git a/docs/library/usocket.rst b/docs/library/usocket.rst index e3b9d279a051e..bc4b4b6d5a952 100644 --- a/docs/library/usocket.rst +++ b/docs/library/usocket.rst @@ -37,8 +37,8 @@ power) and portable way to work with addresses. However, ``socket`` module (note the difference with native MicroPython ``usocket`` module described here) provides CPython-compatible way to specify addresses using tuples, as described below. Note that depending on a -`MicroPython port`, ``socket`` module can be builtin or need to be -installed from `micropython-lib` (as in the case of `MicroPython Unix port`), +:term:`MicroPython port`, ``socket`` module can be builtin or need to be +installed from `micropython-lib` (as in the case of :term:`MicroPython Unix port`), and some ports still accept only numeric addresses in the tuple format, and require to use `getaddrinfo` function to resolve domain names. @@ -61,7 +61,7 @@ Tuple address format for ``socket`` module: must be 0. *scopeid* is the interface scope identifier for link-local addresses. Note the domain names are not accepted as *ipv6_address*, they should be resolved first using `usocket.getaddrinfo()`. Availability - of IPv6 support depends on a `MicroPython port`. + of IPv6 support depends on a :term:`MicroPython port`. Functions --------- @@ -81,7 +81,7 @@ Functions .. function:: getaddrinfo(host, port, af=0, type=0, proto=0, flags=0, /) - Translate the host/port argument into a sequence of 5-tuples that contain all the + Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. Arguments *af*, *type*, and *proto* (which have the same meaning as for the `socket()` function) can be used to filter which kind of addresses are returned. If a parameter is not @@ -141,7 +141,7 @@ Constants .. data:: AF_INET AF_INET6 - Address family types. Availability depends on a particular `MicroPython port`. + Address family types. Availability depends on a particular :term:`MicroPython port`. .. data:: SOCK_STREAM SOCK_DGRAM @@ -151,7 +151,7 @@ Constants .. data:: IPPROTO_UDP IPPROTO_TCP - IP protocol numbers. Availability depends on a particular `MicroPython port`. + IP protocol numbers. Availability depends on a particular :term:`MicroPython port`. Note that you don't need to specify these in a call to `usocket.socket()`, because `SOCK_STREAM` socket type automatically selects `IPPROTO_TCP`, and `SOCK_DGRAM` - `IPPROTO_UDP`. Thus, the only real use of these constants @@ -160,12 +160,12 @@ Constants .. data:: usocket.SOL_* Socket option levels (an argument to `setsockopt()`). The exact - inventory depends on a `MicroPython port`. + inventory depends on a :term:`MicroPython port`. .. data:: usocket.SO_* Socket options (an argument to `setsockopt()`). The exact - inventory depends on a `MicroPython port`. + inventory depends on a :term:`MicroPython port`. Constants specific to WiPy: @@ -185,7 +185,7 @@ Methods on the socket object will fail. The remote end will receive EOF indication if supported by protocol. - Sockets are automatically closed when they are garbage-collected, but it is recommended + Sockets are automatically closed when they are garbage-collected, but it is recommended to `close()` them explicitly as soon you finished working with them. .. method:: socket.bind(address) @@ -259,7 +259,7 @@ Methods completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket is put in blocking mode. - Not every `MicroPython port` supports this method. A more portable and + Not every :term:`MicroPython port` supports this method. A more portable and generic solution is to use `uselect.poll` object. This allows to wait on multiple objects at the same time (and not just on sockets, but on generic `stream` objects which support polling). Example:: diff --git a/docs/library/ussl.rst b/docs/library/ussl.rst index be84dc0545003..ffe146331ca08 100644 --- a/docs/library/ussl.rst +++ b/docs/library/ussl.rst @@ -24,7 +24,7 @@ Functions :meth:`~usocket.socket.accept()` on a non-SSL listening server socket. Depending on the underlying module implementation in a particular - `MicroPython port`, some or all keyword arguments above may be not supported. + :term:`MicroPython port`, some or all keyword arguments above may be not supported. .. warning:: diff --git a/docs/make.bat b/docs/make.bat index 44f9682790ef5..c09487fb741ef 100644 --- a/docs/make.bat +++ b/docs/make.bat @@ -6,6 +6,7 @@ if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build +set SPHINXOPTS=-W --keep-going set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( diff --git a/docs/reference/packages.rst b/docs/reference/packages.rst index 43217493e52a4..2854df23a0240 100644 --- a/docs/reference/packages.rst +++ b/docs/reference/packages.rst @@ -14,8 +14,8 @@ packages: 1. Python modules and packages are turned into distribution package archives, and published at the Python Package Index (PyPI). -2. `upip` package manager can be used to install a distribution package - on a `MicroPython port` with networking capabilities (for example, +2. :term:`upip` package manager can be used to install a distribution package + on a :term:`MicroPython port` with networking capabilities (for example, on the Unix port). 3. For ports without networking capabilities, an "installation image" can be prepared on the Unix port, and transferred to a device by @@ -51,14 +51,14 @@ even by the smallest devices. Besides the small compression dictionary size, MicroPython distribution packages also have other optimizations, like removing any files from the archive which aren't used by the installation process. In particular, -`upip` package manager doesn't execute ``setup.py`` during installation +:term:`upip` package manager doesn't execute ``setup.py`` during installation (see below), and thus that file is not included in the archive. At the same time, these optimizations make MicroPython distribution -packages not compatible with `CPython`'s package manager, ``pip``. +packages not compatible with :term:`CPython`'s package manager, ``pip``. This isn't considered a big problem, because: -1. Packages can be installed with `upip`, and then can be used with +1. Packages can be installed with :term:`upip`, and then can be used with CPython (if they are compatible with it). 2. In the other direction, majority of CPython packages would be incompatible with MicroPython by various reasons, first of all, @@ -73,12 +73,12 @@ resource constrained devices. ------------------------ MicroPython distribution packages are intended to be installed using -the `upip` package manager. `upip` is a Python application which is +the :term:`upip` package manager. :term:`upip` is a Python application which is usually distributed (as frozen bytecode) with network-enabled -`MicroPython ports `. At the very least, -`upip` is available in the `MicroPython Unix port`. +:term:`MicroPython ports `. At the very least, +:term:`upip` is available in the :term:`MicroPython Unix port`. -On any `MicroPython port` providing `upip`, it can be accessed as +On any :term:`MicroPython port` providing :term:`upip`, it can be accessed as following:: import upip @@ -123,12 +123,12 @@ commands which corresponds to the example above are:: Cross-installing packages ------------------------- -For `MicroPython ports ` without native networking +For :term:`MicroPython ports ` without native networking capabilities, the recommend process is "cross-installing" them into a -"directory image" using the `MicroPython Unix port`, and then +"directory image" using the :term:`MicroPython Unix port`, and then transferring this image to a device by suitable means. -Installing to a directory image involves using ``-p`` switch to `upip`:: +Installing to a directory image involves using ``-p`` switch to :term:`upip`:: micropython -m upip install -p install_dir micropython-pystone_lowmem @@ -137,13 +137,13 @@ packages) will be available in the ``install_dir/`` subdirectory. You would need to transfer contents of this directory (without the ``install_dir/`` prefix) to the device, at the suitable location, where it can be found by the Python ``import`` statement (see discussion of -the `upip` installation path above). +the :term:`upip` installation path above). Cross-installing packages with freezing --------------------------------------- -For the low-memory `MicroPython ports `, the process +For the low-memory :term:`MicroPython ports `, the process described in the previous section does not provide the most efficient resource usage,because the packages are installed in the source form, so need to be compiled to the bytecome on each import. This compilation @@ -160,7 +160,7 @@ mentioned above: * Filesystem is not required for frozen packages. Using frozen bytecode requires building the executable (firmware) -for a given `MicroPython port` from the C source code. Consequently, +for a given :term:`MicroPython port` from the C source code. Consequently, the process is: 1. Follow the instructions for a particular port on setting up a @@ -168,7 +168,7 @@ the process is: study instructions in ``ports/esp8266/README.md`` and follow them. Make sure you can build the port and deploy the resulting executable/firmware successfully before proceeding to the next steps. -2. Build `MicroPython Unix port` and make sure it is in your PATH and +2. Build :term:`MicroPython Unix port` and make sure it is in your PATH and you can execute ``micropython``. 3. Change to port's directory (e.g. ``ports/esp8266/`` for ESP8266). 4. Run ``make clean-frozen``. This step cleans up any previous @@ -281,7 +281,7 @@ following calls:: pkg_resources.resource_stream(__name__, "data/page.html") pkg_resources.resource_stream(__name__, "data/image.png") -You can develop and debug using the `MicroPython Unix port` as usual. +You can develop and debug using the :term:`MicroPython Unix port` as usual. When time comes to make a distribution package out of it, just use overridden "sdist" command from sdist_upip.py module as described in the previous section. diff --git a/docs/reference/pyboard.py.rst b/docs/reference/pyboard.py.rst index d404c738f1d26..30230eebc3bf4 100644 --- a/docs/reference/pyboard.py.rst +++ b/docs/reference/pyboard.py.rst @@ -68,7 +68,7 @@ example:: $ pyboard.py -c 'print(1+1)' Similarly, the ``PYBOARD_BAUDRATE`` environment variable can be used -to set the default for the `--baudrate` option. +to set the default for the ``--baudrate`` option. Running a script on the device ------------------------------ diff --git a/docs/templates/replace.inc b/docs/templates/replace.inc index 319c53735fc28..14f1875eeec6a 100644 --- a/docs/templates/replace.inc +++ b/docs/templates/replace.inc @@ -4,6 +4,6 @@ .. |see_cpython_module| replace:: - *This module implements a subset of the corresponding* `CPython` *module, + *This module implements a subset of the corresponding* :term:`CPython` *module, as described below. For more information, refer to the original CPython documentation:* From 6ac05af8e1408a1f5cd27d57c8581a9f4197a2f5 Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Sun, 7 Jun 2020 18:23:36 +0200 Subject: [PATCH 0047/3533] mimxrt/tusb_config.h: Preliminary fix for TinyUSB HS endpoint overflow. Sending more than 64 bytes to the USB CDC endpoint in HS mode will lead to a hard crash. This commit fixes the issue, although there may be a better fix from upstream TinyUSB in the future. --- ports/mimxrt/tusb_config.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/mimxrt/tusb_config.h b/ports/mimxrt/tusb_config.h index f2367c2044384..c7ec05e632323 100644 --- a/ports/mimxrt/tusb_config.h +++ b/ports/mimxrt/tusb_config.h @@ -30,7 +30,8 @@ #define CFG_TUSB_OS (OPT_OS_NONE) #define CFG_TUD_CDC (1) -#define CFG_TUD_CDC_RX_BUFSIZE (256) -#define CFG_TUD_CDC_TX_BUFSIZE (256) +#define CFG_TUD_CDC_RX_BUFSIZE (512) +#define CFG_TUD_CDC_TX_BUFSIZE (512) +#define CFG_TUD_CDC_EPSIZE (512) #endif // MICROPY_INCLUDED_MIMXRT_TUSB_CONFIG_H From bd06c698f05b7b53070263653e74eb078b5278a9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 8 Jun 2020 14:31:35 +1000 Subject: [PATCH 0048/3533] py/dynruntime.h: Make mp_obj_str_get_str raise if arg not a str/bytes. --- py/dynruntime.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/py/dynruntime.h b/py/dynruntime.h index 9f2803c53520f..696746f7510ff 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -114,7 +114,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) { #define mp_obj_cast_to_native_base(o, t) (mp_obj_cast_to_native_base_dyn((o), (t))) #define mp_obj_get_int(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_INT)) #define mp_obj_get_int_truncated(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_UINT)) -#define mp_obj_str_get_str(s) ((void *)mp_fun_table.native_from_obj(s, MP_NATIVE_TYPE_PTR)) +#define mp_obj_str_get_str(s) (mp_obj_str_get_data_dyn((s), NULL)) #define mp_obj_str_get_data(o, len) (mp_obj_str_get_data_dyn((o), (len))) #define mp_get_buffer_raise(o, bufinfo, fl) (mp_fun_table.get_buffer_raise((o), (bufinfo), (fl))) #define mp_get_stream_raise(s, flags) (mp_fun_table.get_stream_raise((s), (flags))) @@ -149,7 +149,9 @@ static inline mp_obj_t mp_obj_cast_to_native_base_dyn(mp_obj_t self_in, mp_const static inline void *mp_obj_str_get_data_dyn(mp_obj_t o, size_t *l) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(o, &bufinfo, MP_BUFFER_READ); - *l = bufinfo.len; + if (l != NULL) { + *l = bufinfo.len; + } return bufinfo.buf; } From 51fd6c97773a7e0d76bb61e220b35b98f392e27e Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 3 Jun 2020 10:18:49 +0200 Subject: [PATCH 0049/3533] extmod/ure: Use single function for match/search/sub. Saves about 500 bytes on unix x64 and enables CPython-conform usage of passing a re object to these functions. --- examples/natmod/ure/ure.c | 4 +-- extmod/modure.c | 58 ++++++++++++++------------------------- tests/extmod/ure1.py | 11 ++++++++ tests/extmod/ure_sub.py | 9 ++++++ 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/examples/natmod/ure/ure.c b/examples/natmod/ure/ure.c index 00ca2e9eb4c71..175b93e395ef9 100644 --- a/examples/natmod/ure/ure.c +++ b/examples/natmod/ure/ure.c @@ -71,8 +71,8 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a re_type.locals_dict = (void*)&re_locals_dict; mp_store_global(MP_QSTR_compile, MP_OBJ_FROM_PTR(&mod_re_compile_obj)); - mp_store_global(MP_QSTR_match, MP_OBJ_FROM_PTR(&mod_re_match_obj)); - mp_store_global(MP_QSTR_search, MP_OBJ_FROM_PTR(&mod_re_search_obj)); + mp_store_global(MP_QSTR_match, MP_OBJ_FROM_PTR(&re_match_obj)); + mp_store_global(MP_QSTR_search, MP_OBJ_FROM_PTR(&re_search_obj)); MP_DYNRUNTIME_INIT_EXIT } diff --git a/extmod/modure.c b/extmod/modure.c index 1847ec288075d..328c897d8890b 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -53,6 +53,10 @@ typedef struct _mp_obj_match_t { const char *caps[0]; } mp_obj_match_t; +STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args); +#if !MICROPY_ENABLE_DYNRUNTIME +STATIC const mp_obj_type_t re_type; +#endif STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; @@ -175,7 +179,12 @@ STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { (void)n_args; - mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]); + mp_obj_re_t *self; + if (mp_obj_is_type(args[0], &re_type)) { + self = MP_OBJ_TO_PTR(args[0]); + } else { + self = MP_OBJ_TO_PTR(mod_re_compile(1, args)); + } Subject subj; size_t len; subj.begin = mp_obj_str_get_data(args[1], &len); @@ -253,8 +262,13 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split); #if MICROPY_PY_URE_SUB -STATIC mp_obj_t re_sub_helper(mp_obj_t self_in, size_t n_args, const mp_obj_t *args) { - mp_obj_re_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { + mp_obj_re_t *self; + if (mp_obj_is_type(args[0], &re_type)) { + self = MP_OBJ_TO_PTR(args[0]); + } else { + self = MP_OBJ_TO_PTR(mod_re_compile(1, args)); + } mp_obj_t replace = args[1]; mp_obj_t where = args[2]; mp_int_t count = 0; @@ -358,10 +372,7 @@ STATIC mp_obj_t re_sub_helper(mp_obj_t self_in, size_t n_args, const mp_obj_t *a return mp_obj_new_str_from_vstr(mp_obj_get_type(where), &vstr_return); } -STATIC mp_obj_t re_sub(size_t n_args, const mp_obj_t *args) { - return re_sub_helper(args[0], n_args, args); -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_sub_obj, 3, 5, re_sub); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_sub_obj, 3, 5, re_sub_helper); #endif @@ -414,41 +425,14 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_compile_obj, 1, 2, mod_re_compile); -STATIC mp_obj_t mod_re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { - (void)n_args; - mp_obj_t self = mod_re_compile(1, args); - - const mp_obj_t args2[] = {self, args[1]}; - mp_obj_t match = ure_exec(is_anchored, 2, args2); - return match; -} - -STATIC mp_obj_t mod_re_match(size_t n_args, const mp_obj_t *args) { - return mod_re_exec(true, n_args, args); -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_match_obj, 2, 4, mod_re_match); - -STATIC mp_obj_t mod_re_search(size_t n_args, const mp_obj_t *args) { - return mod_re_exec(false, n_args, args); -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_search_obj, 2, 4, mod_re_search); - -#if MICROPY_PY_URE_SUB -STATIC mp_obj_t mod_re_sub(size_t n_args, const mp_obj_t *args) { - mp_obj_t self = mod_re_compile(1, args); - return re_sub_helper(self, n_args, args); -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_sub_obj, 3, 5, mod_re_sub); -#endif - #if !MICROPY_ENABLE_DYNRUNTIME STATIC const mp_rom_map_elem_t mp_module_re_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ure) }, { MP_ROM_QSTR(MP_QSTR_compile), MP_ROM_PTR(&mod_re_compile_obj) }, - { MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&mod_re_match_obj) }, - { MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&mod_re_search_obj) }, + { MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&re_match_obj) }, + { MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&re_search_obj) }, #if MICROPY_PY_URE_SUB - { MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&mod_re_sub_obj) }, + { MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&re_sub_obj) }, #endif #if MICROPY_PY_URE_DEBUG { MP_ROM_QSTR(MP_QSTR_DEBUG), MP_ROM_INT(FLAG_DEBUG) }, diff --git a/tests/extmod/ure1.py b/tests/extmod/ure1.py index 9e1be5fc796bc..2bdf2d0cfb4f3 100644 --- a/tests/extmod/ure1.py +++ b/tests/extmod/ure1.py @@ -125,3 +125,14 @@ print(re.compile(r"[a\-x]").split("foo-bar")) print(re.compile(r"[\-ax]").split("foo-bar")) print("===") + +# Module functions take str/bytes/re. +for f in (re.match, re.search): + print(f(".", "foo").group(0)) + print(f(b".", b"foo").group(0)) + print(f(re.compile("."), "foo").group(0)) + try: + f(123, "a") + except TypeError: + print("TypeError") +print("===") diff --git a/tests/extmod/ure_sub.py b/tests/extmod/ure_sub.py index 6bb332077dd29..953e7bf62a478 100644 --- a/tests/extmod/ure_sub.py +++ b/tests/extmod/ure_sub.py @@ -60,3 +60,12 @@ def A(): re.sub("(a)", "b\\199999999999999999999999999999999999999", "a") except: print("invalid group") + +# Module function takes str/bytes/re. +print(re.sub("a", "a", "a")) +print(re.sub(b".", b"a", b"a")) +print(re.sub(re.compile("a"), "a", "a")) +try: + re.sub(123, "a", "a") +except TypeError: + print("TypeError") From 8e8dcdd34b39be58c4d9b64ee23ad40acb2a6b1d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 1 Apr 2020 15:16:03 +1100 Subject: [PATCH 0050/3533] esp32: Update IDF v4.0 supported hash to v4.0.1. The main fix relevant to MicroPython is https://github.com/espressif/esp-idf/issues/4196 Release notes here https://github.com/espressif/esp-idf/releases/tag/v4.0.1 --- ports/esp32/Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index bd89e8b0c5610..0f6a1969a6208 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -54,9 +54,9 @@ SDKCONFIG_COMBINED = $(BUILD)/sdkconfig.combined SDKCONFIG_H = $(BUILD)/sdkconfig.h # The git hash of the currently supported ESP IDF version. -# These correspond to v3.3.2 and v4.0. +# These correspond to v3.3.2 and v4.0.1. ESPIDF_SUPHASH_V3 := 9e70825d1e1cbf7988cf36981774300066580ea7 -ESPIDF_SUPHASH_V4 := 463a9d8b7f9af8205222b80707f9bdbba7c530e1 +ESPIDF_SUPHASH_V4 := 4c81978a3e2220674a432a588292a4c860eef27b define print_supported_git_hash $(info Supported git hash (v3.3): $(ESPIDF_SUPHASH_V3)) @@ -515,7 +515,7 @@ ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\ ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c)) ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing -I$(ESPCOMP)/wpa_supplicant/src -Wno-implicit-function-declaration +$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DCONFIG_WPA3_SAE -DCONFIG_IEEE80211W -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing -I$(ESPCOMP)/wpa_supplicant/src -Wno-implicit-function-declaration else $(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DEMBEDDED_SUPP -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_MSCHAPv2 -DEAP_TTLS -DEAP_TLS -DEAP_PEAP -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DALLOW_EVEN_MOD -D__ets__ -Wno-strict-aliasing endif @@ -555,6 +555,7 @@ ESPIDF_BT_NIMBLE_O = $(patsubst %.c,%.o,\ $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/src/*.c) \ $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/src/*.c) \ $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/src/*.c) \ + $(wildcard $(ESPCOMP)/bt/host/nimble/port/src/*.c) \ ) endif From e0d539f79d4ec020636ba7b043fac08030234112 Mon Sep 17 00:00:00 2001 From: Albort Xue Date: Sun, 7 Jun 2020 11:20:19 +0800 Subject: [PATCH 0051/3533] mimxrt/boards: Enable LED class for MIMXRT1060_EVK board. --- .../boards/MIMXRT1060_EVK/mpconfigboard.h | 7 ++-- ports/mimxrt/boards/MIMXRT1060_EVK/pins.c | 33 +++++++++++++++++++ ports/mimxrt/boards/MIMXRT1060_EVK/pins.h | 30 +++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/pins.h diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h index 714f987a54c9d..8bd2a579174aa 100644 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h @@ -3,6 +3,7 @@ #define BOARD_FLASH_SIZE (8 * 1024 * 1024) -#define MICROPY_HW_LED_PINMUX IOMUXC_GPIO_AD_B0_09_GPIO1_IO09 -#define MICROPY_HW_LED_PORT GPIO1 -#define MICROPY_HW_LED_PIN 9 +// MIMXRT1060_EVK has 1 user LED +#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c new file mode 100644 index 0000000000000..d5da9c6f99275 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_AD_B0_09_af[] = { + PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), +}; + +pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h new file mode 100644 index 0000000000000..baef51c6c8612 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_AD_B0_09; From 29e258611ace8e4de51d810d5ff14364a5fbb950 Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Tue, 14 Apr 2020 21:08:59 +0200 Subject: [PATCH 0052/3533] mimxrt/boards: Integrate support for MIMXRT1020_EVK board. --- .../evkmimxrt1020_flexspi_nor_config.h | 270 ++++++++++++++++++ .../boards/MIMXRT1020_EVK/flash_config.c | 51 ++++ .../boards/MIMXRT1020_EVK/mpconfigboard.h | 9 + .../boards/MIMXRT1020_EVK/mpconfigboard.mk | 7 + ports/mimxrt/boards/MIMXRT1020_EVK/pins.c | 33 +++ ports/mimxrt/boards/MIMXRT1020_EVK/pins.h | 30 ++ ports/mimxrt/boards/MIMXRT1021.ld | 8 + 7 files changed, 408 insertions(+) create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/evkmimxrt1020_flexspi_nor_config.h create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/flash_config.c create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1021.ld diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/evkmimxrt1020_flexspi_nor_config.h b/ports/mimxrt/boards/MIMXRT1020_EVK/evkmimxrt1020_flexspi_nor_config.h new file mode 100644 index 0000000000000..c0c6ea243672c --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/evkmimxrt1020_flexspi_nor_config.h @@ -0,0 +1,270 @@ +/* + * Copyright 2019 NXP. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +// Based on tinyusb/hw/bsp/teensy_40/evkmimxrt1020_flexspi_nor_config.h + +#ifndef __EVKMIMXRT1020_FLEXSPI_NOR_CONFIG__ +#define __EVKMIMXRT1020_FLEXSPI_NOR_CONFIG__ + +#include +#include +#include "fsl_common.h" + +/*! @name Driver version */ +/*@{*/ +/*! @brief XIP_BOARD driver version 2.0.0. */ +#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) +/*@}*/ + +/* FLEXSPI memory config block related defintions */ +#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian +#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0 +#define FLEXSPI_CFG_BLK_SIZE (512) + +/* FLEXSPI Feature related definitions */ +#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1 + +/* Lookup table related defintions */ +#define CMD_INDEX_READ 0 +#define CMD_INDEX_READSTATUS 1 +#define CMD_INDEX_WRITEENABLE 2 +#define CMD_INDEX_WRITE 4 + +#define CMD_LUT_SEQ_IDX_READ 0 +#define CMD_LUT_SEQ_IDX_READSTATUS 1 +#define CMD_LUT_SEQ_IDX_WRITEENABLE 3 +#define CMD_LUT_SEQ_IDX_WRITE 9 + +#define CMD_SDR 0x01 +#define CMD_DDR 0x21 +#define RADDR_SDR 0x02 +#define RADDR_DDR 0x22 +#define CADDR_SDR 0x03 +#define CADDR_DDR 0x23 +#define MODE1_SDR 0x04 +#define MODE1_DDR 0x24 +#define MODE2_SDR 0x05 +#define MODE2_DDR 0x25 +#define MODE4_SDR 0x06 +#define MODE4_DDR 0x26 +#define MODE8_SDR 0x07 +#define MODE8_DDR 0x27 +#define WRITE_SDR 0x08 +#define WRITE_DDR 0x28 +#define READ_SDR 0x09 +#define READ_DDR 0x29 +#define LEARN_SDR 0x0A +#define LEARN_DDR 0x2A +#define DATSZ_SDR 0x0B +#define DATSZ_DDR 0x2B +#define DUMMY_SDR 0x0C +#define DUMMY_DDR 0x2C +#define DUMMY_RWDS_SDR 0x0D +#define DUMMY_RWDS_DDR 0x2D +#define JMP_ON_CS 0x1F +#define STOP 0 + +#define FLEXSPI_1PAD 0 +#define FLEXSPI_2PAD 1 +#define FLEXSPI_4PAD 2 +#define FLEXSPI_8PAD 3 + +#define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \ + (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \ + FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1)) + +//!@brief Definitions for FlexSPI Serial Clock Frequency +typedef enum _FlexSpiSerialClockFreq +{ + kFlexSpiSerialClk_30MHz = 1, + kFlexSpiSerialClk_50MHz = 2, + kFlexSpiSerialClk_60MHz = 3, + kFlexSpiSerialClk_75MHz = 4, + kFlexSpiSerialClk_80MHz = 5, + kFlexSpiSerialClk_100MHz = 6, + kFlexSpiSerialClk_133MHz = 7, + kFlexSpiSerialClk_166MHz = 8, + kFlexSpiSerialClk_200MHz = 9, +} flexspi_serial_clk_freq_t; + +//!@brief FlexSPI clock configuration type +enum +{ + kFlexSpiClk_SDR, //!< Clock configure for SDR mode + kFlexSpiClk_DDR, //!< Clock configurat for DDR mode +}; + +//!@brief FlexSPI Read Sample Clock Source definition +typedef enum _FlashReadSampleClkSource +{ + kFlexSPIReadSampleClk_LoopbackInternally = 0, + kFlexSPIReadSampleClk_LoopbackFromDqsPad = 1, + kFlexSPIReadSampleClk_LoopbackFromSckPad = 2, + kFlexSPIReadSampleClk_ExternalInputFromDqsPad = 3, +} flexspi_read_sample_clk_t; + +//!@brief Misc feature bit definitions +enum +{ + kFlexSpiMiscOffset_DiffClkEnable = 0, //!< Bit for Differential clock enable + kFlexSpiMiscOffset_Ck2Enable = 1, //!< Bit for CK2 enable + kFlexSpiMiscOffset_ParallelEnable = 2, //!< Bit for Parallel mode enable + kFlexSpiMiscOffset_WordAddressableEnable = 3, //!< Bit for Word Addressable enable + kFlexSpiMiscOffset_SafeConfigFreqEnable = 4, //!< Bit for Safe Configuration Frequency enable + kFlexSpiMiscOffset_PadSettingOverrideEnable = 5, //!< Bit for Pad setting override enable + kFlexSpiMiscOffset_DdrModeEnable = 6, //!< Bit for DDR clock confiuration indication. +}; + +//!@brief Flash Type Definition +enum +{ + kFlexSpiDeviceType_SerialNOR = 1, //!< Flash devices are Serial NOR + kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND + kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH + kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND + kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs +}; + +//!@brief Flash Pad Definitions +enum +{ + kSerialFlash_1Pad = 1, + kSerialFlash_2Pads = 2, + kSerialFlash_4Pads = 4, + kSerialFlash_8Pads = 8, +}; + +//!@brief FlexSPI LUT Sequence structure +typedef struct _lut_sequence +{ + uint8_t seqNum; //!< Sequence Number, valid number: 1-16 + uint8_t seqId; //!< Sequence Index, valid number: 0-15 + uint16_t reserved; +} flexspi_lut_seq_t; + +//!@brief Flash Configuration Command Type +enum +{ + kDeviceConfigCmdType_Generic, //!< Generic command, for example: configure dummy cycles, drive strength, etc + kDeviceConfigCmdType_QuadEnable, //!< Quad Enable command + kDeviceConfigCmdType_Spi2Xpi, //!< Switch from SPI to DPI/QPI/OPI mode + kDeviceConfigCmdType_Xpi2Spi, //!< Switch from DPI/QPI/OPI to SPI mode + kDeviceConfigCmdType_Spi2NoCmd, //!< Switch to 0-4-4/0-8-8 mode + kDeviceConfigCmdType_Reset, //!< Reset device command +}; + +//!@brief FlexSPI Memory Configuration Block +typedef struct _FlexSPIConfig +{ + uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL + uint32_t version; //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix + uint32_t reserved0; //!< [0x008-0x00b] Reserved for future use + uint8_t readSampleClkSrc; //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3 + uint8_t csHoldTime; //!< [0x00d-0x00d] CS hold time, default value: 3 + uint8_t csSetupTime; //!< [0x00e-0x00e] CS setup time, default value: 3 + uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For + //! Serial NAND, need to refer to datasheet + uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable + uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch, + //! Generic configuration, etc. + uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for + //! DPI/QPI/OPI switch or reset command + flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt + //! sequence number, [31:16] Reserved + uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration + uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable + uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe + flexspi_lut_seq_t + configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq + uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use + uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands + uint32_t reserved2; //!< [0x03c-0x03f] Reserved for future use + uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more + //! details + uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details + uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal + uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot + //! Chapter for more details + uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot + //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH + uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use + uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1 + uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2 + uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1 + uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2 + uint32_t csPadSettingOverride; //!< [0x060-0x063] CS pad setting override value + uint32_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value + uint32_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value + uint32_t dqsPadSettingOverride; //!< [0x06c-0x06f] DQS pad setting override value + uint32_t timeoutInMs; //!< [0x070-0x073] Timeout threshold for read status command + uint32_t commandInterval; //!< [0x074-0x077] CS deselect interval between two commands + uint16_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B, in terms of 0.1ns + uint16_t busyOffset; //!< [0x07c-0x07d] Busy offset, valid value: 0-31 + uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 - + //! busy flag is 0 when flash device is busy + uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences + flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences + uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use +} flexspi_mem_config_t; + +/* */ +#define NOR_CMD_INDEX_READ CMD_INDEX_READ //!< 0 +#define NOR_CMD_INDEX_READSTATUS CMD_INDEX_READSTATUS //!< 1 +#define NOR_CMD_INDEX_WRITEENABLE CMD_INDEX_WRITEENABLE //!< 2 +#define NOR_CMD_INDEX_ERASESECTOR 3 //!< 3 +#define NOR_CMD_INDEX_PAGEPROGRAM CMD_INDEX_WRITE //!< 4 +#define NOR_CMD_INDEX_CHIPERASE 5 //!< 5 +#define NOR_CMD_INDEX_DUMMY 6 //!< 6 +#define NOR_CMD_INDEX_ERASEBLOCK 7 //!< 7 + +#define NOR_CMD_LUT_SEQ_IDX_READ CMD_LUT_SEQ_IDX_READ //!< 0 READ LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS \ + CMD_LUT_SEQ_IDX_READSTATUS //!< 1 Read Status LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS_XPI \ + 2 //!< 2 Read status DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE \ + CMD_LUT_SEQ_IDX_WRITEENABLE //!< 3 Write Enable sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE_XPI \ + 4 //!< 4 Write Enable DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 //!< 5 Erase Sector sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASEBLOCK 8 //!< 8 Erase Block sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM \ + CMD_LUT_SEQ_IDX_WRITE //!< 9 Program sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_CHIPERASE 11 //!< 11 Chip Erase sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READ_SFDP 13 //!< 13 Read SFDP sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_RESTORE_NOCMD \ + 14 //!< 14 Restore 0-4-4/0-8-8 mode sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_EXIT_NOCMD \ + 15 //!< 15 Exit 0-4-4/0-8-8 mode sequence id in lookupTable stored in config blobk + +/* + * Serial NOR configuration block + */ +typedef struct _flexspi_nor_config +{ + flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI + uint32_t pageSize; //!< Page size of Serial NOR + uint32_t sectorSize; //!< Sector size of Serial NOR + uint8_t ipcmdSerialClkFreq; //!< Clock frequency for IP command + uint8_t isUniformBlockSize; //!< Sector/Block size is the same + uint8_t reserved0[2]; //!< Reserved for future use + uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3 + uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command + uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false + uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution + uint32_t blockSize; //!< Block size + uint32_t reserve2[11]; //!< Reserved for future use +} flexspi_nor_config_t; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif /* __EVKMIMXRT1020_FLEXSPI_NOR_CONFIG__ */ diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/flash_config.c b/ports/mimxrt/boards/MIMXRT1020_EVK/flash_config.c new file mode 100644 index 0000000000000..56007931a90d7 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/flash_config.c @@ -0,0 +1,51 @@ +/* + * Copyright 2019 NXP. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +// Based on tinyusb/hw/bsp/teensy_40/evkmimxrt1010_flexspi_nor_config.c + +#include "evkmimxrt1020_flexspi_nor_config.h" + +/* Component ID definition, used by tools. */ +#ifndef FSL_COMPONENT_ID +#define FSL_COMPONENT_ID "platform.drivers.xip_board" +#endif + +/******************************************************************************* + * Code + ******************************************************************************/ +#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1) +#if defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.conf"))) +#elif defined(__ICCARM__) +#pragma location = ".boot_hdr.conf" +#endif + +const flexspi_nor_config_t qspiflash_config = { + .memConfig = + { + .tag = FLEXSPI_CFG_BLK_TAG, + .version = FLEXSPI_CFG_BLK_VERSION, + .readSampleClkSrc = kFlexSPIReadSampleClk_LoopbackFromDqsPad, + .csHoldTime = 3u, + .csSetupTime = 3u, + // Enable DDR mode, Wordaddassable, Safe configuration, Differential clock + .sflashPadType = kSerialFlash_4Pads, + .serialClkFreq = kFlexSpiSerialClk_100MHz, + .sflashA1Size = 8u * 1024u * 1024u, + .lookupTable = + { + // Read LUTs + FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0xEB, RADDR_SDR, FLEXSPI_4PAD, 0x18), + FLEXSPI_LUT_SEQ(DUMMY_SDR, FLEXSPI_4PAD, 0x06, READ_SDR, FLEXSPI_4PAD, 0x04), + }, + }, + .pageSize = 256u, + .sectorSize = 4u * 1024u, + .blockSize = 256u * 1024u, + .isUniformBlockSize = false, +}; +#endif /* XIP_BOOT_HEADER_ENABLE */ diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h new file mode 100644 index 0000000000000..8598f76bf1833 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h @@ -0,0 +1,9 @@ +#define MICROPY_HW_BOARD_NAME "i.MX RT1020 EVK" +#define MICROPY_HW_MCU_NAME "MIMXRT1021DAG5A" + +#define BOARD_FLASH_SIZE (8 * 1024 * 1024) + +// i.MX RT1020 EVK has 1 board LED +#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_05) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk new file mode 100644 index 0000000000000..f3b1689524e29 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk @@ -0,0 +1,7 @@ +MCU_SERIES = MIMXRT1021 +MCU_VARIANT = MIMXRT1021DAG5A + +JLINK_PATH ?= /media/RT1020-EVK/ + +deploy: $(BUILD)/firmware.bin + cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c new file mode 100644 index 0000000000000..946b6efca8b27 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_AD_B0_05_af[] = { + PIN_AF(GPIO1_IO05, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), +}; + +pin_obj_t GPIO_AD_B0_05 = PIN(GPIO_AD_B0_05, GPIO1, 5, GPIO_AD_B0_05_af); diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h new file mode 100644 index 0000000000000..158929911c7a5 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_AD_B0_05; diff --git a/ports/mimxrt/boards/MIMXRT1021.ld b/ports/mimxrt/boards/MIMXRT1021.ld new file mode 100644 index 0000000000000..6b59649fcaf7e --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1021.ld @@ -0,0 +1,8 @@ +/* 24kiB stack. */ +__stack_size__ = 0x6000; +_estack = __StackTop; +_sstack = __StackLimit; + +/* Use second OCRAM bank for GC heap. */ +_gc_heap_start = ORIGIN(m_data2); +_gc_heap_end = ORIGIN(m_data2) + LENGTH(m_data2); From 28370c04509a6255cd3d6b9424443a5b57bb7467 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 10 Jun 2020 10:45:24 +1000 Subject: [PATCH 0053/3533] py/objtype: Add __dict__ attribute for class objects. The behavior mirrors the instance object dict attribute where a copy of the local attributes are provided (unless the dict is read-only, then that dict itself is returned, as an optimisation). MicroPython does not support modifying this dict because the changes will not be reflected in the class. The feature is only enabled if MICROPY_CPYTHON_COMPAT is set, the same as the instance version. --- py/obj.h | 1 + py/objdict.c | 4 ++-- py/objtype.c | 15 +++++++++++++++ tests/basics/class_dict.py | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/basics/class_dict.py diff --git a/py/obj.h b/py/obj.h index 125acf118f08d..468125eb75699 100644 --- a/py/obj.h +++ b/py/obj.h @@ -895,6 +895,7 @@ size_t mp_obj_dict_len(mp_obj_t self_in); mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index); mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value); mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key); +mp_obj_t mp_obj_dict_copy(mp_obj_t self_in); static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) { return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map; } diff --git a/py/objdict.c b/py/objdict.c index 7690eeab29af1..69eda99738eec 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -227,7 +227,7 @@ STATIC mp_obj_t dict_clear(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear); -STATIC mp_obj_t dict_copy(mp_obj_t self_in) { +mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) { mp_check_self(mp_obj_is_dict_type(self_in)); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t other_out = mp_obj_new_dict(self->map.alloc); @@ -240,7 +240,7 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) { memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t)); return other_out; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy); #if MICROPY_PY_BUILTINS_DICT_FROMKEYS // this is a classmethod diff --git a/py/objtype.c b/py/objtype.c index d08c69e284b3e..cb0fb267c8165 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1013,6 +1013,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { dest[0] = MP_OBJ_NEW_QSTR(self->name); return; } + #if MICROPY_CPYTHON_COMPAT + if (attr == MP_QSTR___dict__) { + // Returns a read-only dict of the class attributes. + // If the internal locals is not fixed, a copy will be created. + mp_obj_dict_t *dict = self->locals_dict; + if (dict->map.is_fixed) { + dest[0] = MP_OBJ_FROM_PTR(dict); + } else { + dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(dict)); + dict = MP_OBJ_TO_PTR(dest[0]); + dict->map.is_fixed = 1; + } + return; + } + #endif if (attr == MP_QSTR___bases__) { if (self == &mp_type_object) { dest[0] = mp_const_empty_tuple; diff --git a/tests/basics/class_dict.py b/tests/basics/class_dict.py new file mode 100644 index 0000000000000..f80ded678b0f8 --- /dev/null +++ b/tests/basics/class_dict.py @@ -0,0 +1,19 @@ +# test __dict__ attribute of a class + +if not hasattr(int, "__dict__"): + print("SKIP") + raise SystemExit + + +# dict of a built-in type +print("from_bytes" in int.__dict__) + + +# dict of a user class +class Foo: + a = 1 + b = "bar" + + +d = Foo.__dict__ +print(d["a"], d["b"]) From 95cbe6b65ec84793f0f5ac2fd89c1375969a3bea Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 10 Jun 2020 10:46:38 +1000 Subject: [PATCH 0054/3533] py/objtype: Use mp_obj_dict_copy() for creating obj.__dict__ attribute. The resulting dict is now marked as read-only (is_fixed=1) to enforce the fact that changes to this dict will not be reflected in the class instance. This commit reduces code size by about 20 bytes, and should be more efficient because it creates a direct copy of the dict rather than reinserting all elements. --- py/objtype.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index cb0fb267c8165..a539fcab29414 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -588,16 +588,13 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des #if MICROPY_CPYTHON_COMPAT if (attr == MP_QSTR___dict__) { // Create a new dict with a copy of the instance's map items. - // This creates, unlike CPython, a 'read-only' __dict__: modifying - // it will not result in modifications to the actual instance members. - mp_map_t *map = &self->members; - mp_obj_t attr_dict = mp_obj_new_dict(map->used); - for (size_t i = 0; i < map->alloc; ++i) { - if (mp_map_slot_is_filled(map, i)) { - mp_obj_dict_store(attr_dict, map->table[i].key, map->table[i].value); - } - } - dest[0] = attr_dict; + // This creates, unlike CPython, a read-only __dict__ that can't be modified. + mp_obj_dict_t dict; + dict.base.type = &mp_type_dict; + dict.map = self->members; + dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(&dict)); + mp_obj_dict_t *dest_dict = MP_OBJ_TO_PTR(dest[0]); + dest_dict->map.is_fixed = 1; return; } #endif From f3062b5cbdeea5900486a38902e3155117ebf820 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 9 Jun 2020 14:46:57 +1000 Subject: [PATCH 0055/3533] py/obj.h: Clarify comments about mp_map_t is_fixed and is_ordered. Long ago, prior to 0ef01d0a75b8b2f48a72f0041e048a390b9e75b6, fixed and ordered maps were the same setting with the "table_is_fixed_array" member of mp_map_t. But these settings are actually independent, and it is possible to have is_fixed=1, is_ordered=0 (although this can currently only be done by tools/cc1). So update the comments to reflect this. --- py/obj.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/obj.h b/py/obj.h index 468125eb75699..f80f00031dc77 100644 --- a/py/obj.h +++ b/py/obj.h @@ -407,8 +407,8 @@ typedef struct _mp_rom_map_elem_t { typedef struct _mp_map_t { size_t all_keys_are_qstrs : 1; - size_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered - size_t is_ordered : 1; // an ordered array + size_t is_fixed : 1; // if set, table is fixed/read-only and can't be modified + size_t is_ordered : 1; // if set, table is an ordered array, not a hash map size_t used : (8 * sizeof(size_t) - 3); size_t alloc; mp_map_elem_t *table; From a4c96fb3b0c5e3bf83238a0edd0fbcbfd96208c8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Jun 2020 21:26:27 +1000 Subject: [PATCH 0056/3533] extmod/uasyncio: Add asyncio.wait_for_ms function. Fixes issue #6107. --- docs/library/uasyncio.rst | 6 ++++ extmod/uasyncio/__init__.py | 1 + extmod/uasyncio/funcs.py | 12 +++++--- tests/extmod/uasyncio_micropython.py | 37 ++++++++++++++++++++++++ tests/extmod/uasyncio_micropython.py.exp | 7 +++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/extmod/uasyncio_micropython.py create mode 100644 tests/extmod/uasyncio_micropython.py.exp diff --git a/docs/library/uasyncio.rst b/docs/library/uasyncio.rst index 0f363a076ea3b..31b38a4e05f2f 100644 --- a/docs/library/uasyncio.rst +++ b/docs/library/uasyncio.rst @@ -74,6 +74,12 @@ Additional functions This is a coroutine. +.. function:: wait_for_ms(awaitable, timeout) + + Similar to `wait_for` but *timeout* is an integer in milliseconds. + + This is a coroutine, and a MicroPython extension. + .. function:: gather(\*awaitables, return_exceptions=False) Run all *awaitables* concurrently. Any *awaitables* that are not tasks are diff --git a/extmod/uasyncio/__init__.py b/extmod/uasyncio/__init__.py index da8b58061e6ca..08f924cf29b9b 100644 --- a/extmod/uasyncio/__init__.py +++ b/extmod/uasyncio/__init__.py @@ -7,6 +7,7 @@ _attrs = { "wait_for": "funcs", + "wait_for_ms": "funcs", "gather": "funcs", "Event": "event", "Lock": "lock", diff --git a/extmod/uasyncio/funcs.py b/extmod/uasyncio/funcs.py index 7a4bddf25695a..6e1305c94f418 100644 --- a/extmod/uasyncio/funcs.py +++ b/extmod/uasyncio/funcs.py @@ -4,16 +4,16 @@ from . import core -async def wait_for(aw, timeout): +async def wait_for(aw, timeout, sleep=core.sleep): aw = core._promote_to_task(aw) if timeout is None: return await aw - def cancel(aw, timeout): - await core.sleep(timeout) + def cancel(aw, timeout, sleep): + await sleep(timeout) aw.cancel() - cancel_task = core.create_task(cancel(aw, timeout)) + cancel_task = core.create_task(cancel(aw, timeout, sleep)) try: ret = await aw except core.CancelledError: @@ -29,6 +29,10 @@ def cancel(aw, timeout): return ret +def wait_for_ms(aw, timeout): + return wait_for(aw, timeout, core.sleep_ms) + + async def gather(*aws, return_exceptions=False): ts = [core._promote_to_task(aw) for aw in aws] for i in range(len(ts)): diff --git a/tests/extmod/uasyncio_micropython.py b/tests/extmod/uasyncio_micropython.py new file mode 100644 index 0000000000000..69e5fa3224df7 --- /dev/null +++ b/tests/extmod/uasyncio_micropython.py @@ -0,0 +1,37 @@ +# Test MicroPython extensions on CPython asyncio: +# - sleep_ms +# - wait_for_ms + +try: + import utime, uasyncio +except ImportError: + print("SKIP") + raise SystemExit + + +async def task(id, t): + print("task start", id) + await uasyncio.sleep_ms(t) + print("task end", id) + return id * 2 + + +async def main(): + # Simple sleep_ms + t0 = utime.ticks_ms() + await uasyncio.sleep_ms(1) + print(utime.ticks_diff(utime.ticks_ms(), t0) < 100) + + # When task finished before the timeout + print(await uasyncio.wait_for_ms(task(1, 5), 50)) + + # When timeout passes and task is cancelled + try: + print(await uasyncio.wait_for_ms(task(2, 50), 5)) + except uasyncio.TimeoutError: + print("timeout") + + print("finish") + + +uasyncio.run(main()) diff --git a/tests/extmod/uasyncio_micropython.py.exp b/tests/extmod/uasyncio_micropython.py.exp new file mode 100644 index 0000000000000..f5be1dc75a254 --- /dev/null +++ b/tests/extmod/uasyncio_micropython.py.exp @@ -0,0 +1,7 @@ +True +task start 1 +task end 1 +2 +task start 2 +timeout +finish From 3705bc418cadd9d6ad87f2bcd5443ba82f730e15 Mon Sep 17 00:00:00 2001 From: jp-96 Date: Mon, 8 Jun 2020 12:19:56 +0900 Subject: [PATCH 0057/3533] extmod/modbluetooth: Register default GATT service and fix esp32 init. This is for the NimBLE bindings, to make sure the default GATT service appears and that the esp32 initialises NimBLE correctly (it now matches stm32). --- extmod/nimble/modbluetooth_nimble.c | 7 +++++-- ports/esp32/nimble.c | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 05ec6117791da..035ec3d26fc6d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -39,6 +39,7 @@ #include "nimble/ble.h" #include "nimble/nimble_port.h" #include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" @@ -271,8 +272,9 @@ int mp_bluetooth_init(void) { nimble_port_init(); mp_bluetooth_nimble_port_postinit(); - // By default, just register the default gap service. + // By default, just register the default gap/gatt service. ble_svc_gap_init(); + ble_svc_gatt_init(); mp_bluetooth_nimble_port_start(); @@ -466,8 +468,9 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { // Reset the gatt characteristic value db. mp_bluetooth_gatts_db_reset(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db); - // By default, just register the default gap service. + // By default, just register the default gap/gatt service. ble_svc_gap_init(); + ble_svc_gatt_init(); if (!append) { // Unref any previous service definitions. diff --git a/ports/esp32/nimble.c b/ports/esp32/nimble.c index e60e08badfbe1..16829732c30e5 100644 --- a/ports/esp32/nimble.c +++ b/ports/esp32/nimble.c @@ -44,7 +44,6 @@ void mp_bluetooth_nimble_port_preinit(void) { } void mp_bluetooth_nimble_port_postinit(void) { - nimble_port_freertos_init(ble_host_task); } void mp_bluetooth_nimble_port_deinit(void) { @@ -52,6 +51,7 @@ void mp_bluetooth_nimble_port_deinit(void) { } void mp_bluetooth_nimble_port_start(void) { + nimble_port_freertos_init(ble_host_task); } #endif From c6fd6a0d728fea590e7f731e41a33754111770bd Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 5 Jun 2020 15:40:29 +1000 Subject: [PATCH 0058/3533] examples/bluetooth: Fix event code in ble_temperature_central.py. --- examples/bluetooth/ble_temperature_central.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 1ce99728b37d3..f9db225c16942 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -128,7 +128,7 @@ def _irq(self, event, data): if conn_handle == self._conn_handle and uuid == _ENV_SENSE_UUID: self._start_handle, self._end_handle = start_handle, end_handle - elif event == _IRQ_GATTC_SERVICES_DONE: + elif event == _IRQ_GATTC_SERVICE_DONE: # Service query complete. if self._start_handle and self._end_handle: self._ble.gattc_discover_characteristics( @@ -143,7 +143,7 @@ def _irq(self, event, data): if conn_handle == self._conn_handle and uuid == _TEMP_UUID: self._value_handle = value_handle - elif event == _IRQ_GATTC_CHARACTERISTICS_DONE: + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: # Characteristic query complete. if self._value_handle: # We've finished connecting and discovering device, fire the connect callback. From 3f77f2c60ca80ea9189f85193dbd746e9a6034a9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 5 Jun 2020 15:41:08 +1000 Subject: [PATCH 0059/3533] unix/btstack_usb: Allow choosing adaptor via environment variable. This allows running (for example): env MICROPYBTUSB=2-2 ./micropython-dev ../../examples/bluetooth/ble_temperature_central.py --- ports/unix/btstack_usb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ports/unix/btstack_usb.c b/ports/unix/btstack_usb.c index da9d72fe15258..76f32d6d27cfa 100644 --- a/ports/unix/btstack_usb.c +++ b/ports/unix/btstack_usb.c @@ -110,7 +110,23 @@ void mp_bluetooth_btstack_port_init(void) { btstack_run_loop_embedded_get_instance()->init(); } - // TODO: allow setting USB device path via cmdline/env var. + // MICROPYBTUSB can be a ':'' or '-' separated port list. + char *path = getenv("MICROPYBTUSB"); + if (path != NULL) { + uint8_t usb_path[7] = {0}; + size_t usb_path_len = 0; + + while (usb_path_len < MP_ARRAY_SIZE(usb_path)) { + char *delimiter; + usb_path[usb_path_len++] = strtol(path, &delimiter, 16); + if (!delimiter || (*delimiter != ':' && *delimiter != '-')) { + break; + } + path = delimiter + 1; + } + + hci_transport_usb_set_path(usb_path_len, usb_path); + } // hci_dump_open(NULL, HCI_DUMP_STDOUT); hci_init(hci_transport_usb_instance(), NULL); From 00c3e2156a7b5447966019ad46a1c9763f0890ee Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 5 Jun 2020 15:41:40 +1000 Subject: [PATCH 0060/3533] tests/run-multitests.py: Allow passing unique env vars to each instance. For example, to run the BLE multitests entirely with the unix port: env MICROPY_MICROPYTHON=../ports/unix/micropython-dev ./run-multitests.py \ -i micropython,MICROPYBTUSB=01 \ -i micropython,MICROPYBTUSB=02:02 \ multi_bluetooth/ble_*.py --- tests/run-multitests.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index 567e9b5ffa388..7ab4e85c57d79 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -92,20 +92,25 @@ def start_file(self, filename, prepend="", append=""): class PyInstanceSubProcess(PyInstance): - def __init__(self, cmd): - self.cmd = cmd + def __init__(self, argv, env=None): + self.argv = argv + self.env = {n: v for n, v in (i.split("=") for i in env)} if env else None self.popen = None self.finished = True def __str__(self): - return self.cmd[0].rsplit("/")[-1] + return self.argv[0].rsplit("/")[-1] def run_script(self, script): output = b"" err = None try: p = subprocess.run( - self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script + self.argv, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + input=script, + env=self.env, ) output = p.stdout except subprocess.CalledProcessError as er: @@ -114,7 +119,11 @@ def run_script(self, script): def start_script(self, script): self.popen = subprocess.Popen( - self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + self.argv, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + env=self.env, ) self.popen.stdin.write(script) self.popen.stdin.close() @@ -404,16 +413,20 @@ def main(): instances_test = [] for i in cmd_args.instance: - if i.startswith("exec:"): - instances_test.append(PyInstanceSubProcess([i[len("exec:") :]])) - elif i == "micropython": - instances_test.append(PyInstanceSubProcess([MICROPYTHON])) - elif i == "cpython": - instances_test.append(PyInstanceSubProcess([CPYTHON3])) - elif i.startswith("pyb:"): - instances_test.append(PyInstancePyboard(i[len("pyb:") :])) + # Each instance arg is ,ENV=VAR,ENV=VAR... + i = i.split(",") + cmd = i[0] + env = i[1:] + if cmd.startswith("exec:"): + instances_test.append(PyInstanceSubProcess([cmd[len("exec:") :]], env)) + elif cmd == "micropython": + instances_test.append(PyInstanceSubProcess([MICROPYTHON], env)) + elif cmd == "cpython": + instances_test.append(PyInstanceSubProcess([CPYTHON3], env)) + elif cmd.startswith("pyb:"): + instances_test.append(PyInstancePyboard(cmd[len("pyb:") :])) else: - print("unknown instance string: {}".format(i), file=sys.stderr) + print("unknown instance string: {}".format(cmd), file=sys.stderr) sys.exit(1) for _ in range(max_instances - len(instances_test)): From 2934e41df0938b26cffc8eb1ea9f24a077990b61 Mon Sep 17 00:00:00 2001 From: Nick Crabtree Date: Sun, 7 Jun 2020 11:42:54 +0100 Subject: [PATCH 0061/3533] docs/esp8266: Add quickref documentation for UART on esp8266. This patch adds quickref documentation for the change in commit afd0701bf7a9dcb50c5ab46b0ae88b303fec6ed3. This commit added the ability to disable the REPL and hence use UART0 for serial communication on the esp8266, but was not previously documented anywhere. The text is largely taken from the commit message, with generic information on using the UART duplicated from the Wipy quickref document. --- docs/esp8266/quickref.rst | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index 9c7db3205a7e6..c884d93fc9940 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -138,6 +138,45 @@ Also note that Pin(16) is a special pin (used for wakeup from deepsleep mode) and may be not available for use with higher-level classes like ``Neopixel``. +UART (serial bus) +----------------- + +See :ref:`machine.UART `. :: + + from machine import UART + uart = UART(0, baudrate=9600) + uart.write('hello') + uart.read(5) # read up to 5 bytes + +Two UARTs are available. UART0 is on Pins 1 (TX) and 3 (RX). UART0 is +bidirectional, and by default is used for the REPL. UART1 is on Pins 2 +(TX) and 8 (RX) however Pin 8 is used to connect the flash chip, so +UART1 is TX only. + +When UART0 is attached to the REPL, all incoming chars on UART(0) go +straight to stdin so uart.read() will always return None. Use +sys.stdin.read() if it's needed to read characters from the UART(0) +while it's also used for the REPL (or detach, read, then reattach). +When detached the UART(0) can be used for other purposes. + +If there are no objects in any of the dupterm slots when the REPL is +started (on hard or soft reset) then UART(0) is automatically attached. +Without this, the only way to recover a board without a REPL would be to +completely erase and reflash (which would install the default boot.py which +attaches the REPL). + +To detach the REPL from UART0, use:: + + import uos + uos.dupterm(None, 1) + +The REPL is attached by default. If you have detached it, to reattach +it use:: + + import uos, machine + uart = machine.UART(0, 115200) + uos.dupterm(uart, 1) + PWM (pulse width modulation) ---------------------------- From 05e5d411b53dcba9529b6ac1e9388bab65f8c11b Mon Sep 17 00:00:00 2001 From: Albort Xue Date: Wed, 10 Jun 2020 17:11:30 +0800 Subject: [PATCH 0062/3533] mimxrt/boards: Set __heap_size__ to 0 in MIMXRT1011.ld. Do not use the traditional C heap in order to save memory, because the traditional C heap is unused in MicroPython. --- ports/mimxrt/boards/MIMXRT1011.ld | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/mimxrt/boards/MIMXRT1011.ld b/ports/mimxrt/boards/MIMXRT1011.ld index 6b59649fcaf7e..0512c48a7f2d9 100644 --- a/ports/mimxrt/boards/MIMXRT1011.ld +++ b/ports/mimxrt/boards/MIMXRT1011.ld @@ -3,6 +3,9 @@ __stack_size__ = 0x6000; _estack = __StackTop; _sstack = __StackLimit; +/* Do not use the traditional C heap. */ +__heap_size__ = 0; + /* Use second OCRAM bank for GC heap. */ _gc_heap_start = ORIGIN(m_data2); _gc_heap_end = ORIGIN(m_data2) + LENGTH(m_data2); From 38b4f1569e7755f78b991352ae5d53055ad67790 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sat, 2 May 2020 11:21:48 -0500 Subject: [PATCH 0063/3533] zephyr: Fix and rename stacks_analyze function in zephyr module. Zephyr deprecated and then removed its stack_analyze function because it was unsafe. Use the new zephyr thread analyzer instead and rename the MicroPython function to zephyr.thread_analyze() to be more consistent with the implementation. Tested on mimxrt1050_evk. The output now looks like this: >>> zephyr.thread_analyze() Thread analyze: 80004ff4 : unused 400 usage 112 / 512 (21 %) rx_workq : unused 1320 usage 180 / 1500 (12 %) tx_workq : unused 992 usage 208 / 1200 (17 %) net_mgmt : unused 656 usage 112 / 768 (14 %) sysworkq : unused 564 usage 460 / 1024 (44 %) idle : unused 256 usage 64 / 320 (20 %) main : unused 2952 usage 1784 / 4736 (37 %) --- ports/zephyr/modzephyr.c | 29 +++++++---------------------- ports/zephyr/prj.conf | 5 ++--- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/ports/zephyr/modzephyr.c b/ports/zephyr/modzephyr.c index d4ee610b204c6..71b44d7df1611 100644 --- a/ports/zephyr/modzephyr.c +++ b/ports/zephyr/modzephyr.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include "modzephyr.h" #include "py/runtime.h" @@ -45,27 +45,12 @@ STATIC mp_obj_t mod_current_tid(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_current_tid_obj, mod_current_tid); -#ifdef CONFIG_THREAD_STACK_INFO -extern k_tid_t const _main_thread; -extern k_tid_t const _idle_thread; - -static void thread_stack_dump(const struct k_thread *thread, void *user_data) { - const char *th_name = k_thread_name_get((k_tid_t)thread); - - if (th_name == NULL) { - static char tid[9]; - snprintf(tid, sizeof(tid), "%08x", (int)thread); - th_name = tid; - } - - stack_analyze(th_name, (char *)thread->stack_info.start, thread->stack_info.size); -} - -STATIC mp_obj_t mod_stacks_analyze(void) { - k_thread_foreach(thread_stack_dump, NULL); +#ifdef CONFIG_THREAD_ANALYZER +STATIC mp_obj_t mod_thread_analyze(void) { + thread_analyzer_print(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_stacks_analyze_obj, mod_stacks_analyze); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_analyze_obj, mod_thread_analyze); #endif #ifdef CONFIG_NET_SHELL @@ -84,8 +69,8 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr) }, { MP_ROM_QSTR(MP_QSTR_is_preempt_thread), MP_ROM_PTR(&mod_is_preempt_thread_obj) }, { MP_ROM_QSTR(MP_QSTR_current_tid), MP_ROM_PTR(&mod_current_tid_obj) }, - #ifdef CONFIG_THREAD_STACK_INFO - { MP_ROM_QSTR(MP_QSTR_stacks_analyze), MP_ROM_PTR(&mod_stacks_analyze_obj) }, + #ifdef CONFIG_THREAD_ANALYZER + { MP_ROM_QSTR(MP_QSTR_thread_analyze), MP_ROM_PTR(&mod_thread_analyze_obj) }, #endif #ifdef CONFIG_NET_SHELL diff --git a/ports/zephyr/prj.conf b/ports/zephyr/prj.conf index 993dfdc26f9ec..e6ccdadcf063b 100644 --- a/ports/zephyr/prj.conf +++ b/ports/zephyr/prj.conf @@ -50,10 +50,9 @@ CONFIG_NET_DHCPV4=y # Diagnostics and debugging # Required for zephyr.stack_analyze() -CONFIG_INIT_STACKS=y -CONFIG_THREAD_MONITOR=y +CONFIG_THREAD_ANALYZER=y +CONFIG_THREAD_ANALYZER_USE_PRINTK=y CONFIG_THREAD_NAME=y -CONFIG_THREAD_STACK_INFO=y # Required for usocket.pkt_get_info() CONFIG_NET_BUF_POOL_USAGE=y From 8b061f2d799d120f06d68023eb17e5b5e24d50cc Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sat, 2 May 2020 11:38:58 -0500 Subject: [PATCH 0064/3533] zephyr: Fix floating point configuration. Zephyr renamed CONFIG_FLOAT to CONFIG_FPU to better reflect its semantics of enabling the hardware floating point unit (FPU) rather than enabling toolchain-level floating point support (i.e., software floating point for FPU-less socs). --- ports/zephyr/prj.conf | 2 +- ports/zephyr/prj_minimal.conf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/zephyr/prj.conf b/ports/zephyr/prj.conf index e6ccdadcf063b..50cfa005025dc 100644 --- a/ports/zephyr/prj.conf +++ b/ports/zephyr/prj.conf @@ -11,7 +11,7 @@ CONFIG_CONSOLE_GETCHAR_BUFSIZE=128 CONFIG_CONSOLE_PUTCHAR_BUFSIZE=128 CONFIG_NEWLIB_LIBC=y -CONFIG_FLOAT=y +CONFIG_FPU=y CONFIG_MAIN_STACK_SIZE=4736 # Enable sensor subsystem (doesn't add code if not used). diff --git a/ports/zephyr/prj_minimal.conf b/ports/zephyr/prj_minimal.conf index d9c2894e6f2de..8b2104925ab67 100644 --- a/ports/zephyr/prj_minimal.conf +++ b/ports/zephyr/prj_minimal.conf @@ -1,5 +1,5 @@ CONFIG_NEWLIB_LIBC=y -CONFIG_FLOAT=y +CONFIG_FPU=y CONFIG_MAIN_STACK_SIZE=4096 CONFIG_UART_INTERRUPT_DRIVEN=y From db02cb061d8ecd4c7dec90f37e314197a6030ee8 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Thu, 14 May 2020 08:07:49 -0500 Subject: [PATCH 0065/3533] zephyr: Update for refactored zephyr device structures. Updates the zephyr port to use refactored device structures introduced in zephyr 2.3. --- ports/zephyr/machine_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index f00c1d34225ac..4b29f41d1ef71 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -48,7 +48,7 @@ typedef struct _machine_hard_i2c_obj_t { STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_hard_i2c_obj_t *self = self_in; - mp_printf(print, "%s", self->dev->config->name); + mp_printf(print, "%s", self->dev->name); } mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { From 6aff27ac3c2115734e5b0181956de0c04cfbb651 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Thu, 14 May 2020 08:32:32 -0500 Subject: [PATCH 0066/3533] zephyr: Update to new zephyr timeout API. Updates the zephyr port to use the new timeout api introduced in zephyr 2.3. --- ports/zephyr/mphalport.h | 2 +- ports/zephyr/uart_core.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h index 594f6a1f69830..8434a388b0a5e 100644 --- a/ports/zephyr/mphalport.h +++ b/ports/zephyr/mphalport.h @@ -21,7 +21,7 @@ static inline void mp_hal_delay_us(mp_uint_t delay) { } static inline void mp_hal_delay_ms(mp_uint_t delay) { - k_sleep(delay); + k_msleep(delay); } #define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us)) diff --git a/ports/zephyr/uart_core.c b/ports/zephyr/uart_core.c index fef9f00ab1625..63ecc8289e3c9 100644 --- a/ports/zephyr/uart_core.c +++ b/ports/zephyr/uart_core.c @@ -49,7 +49,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { while (len--) { char c = *str++; while (console_putchar(c) == -1) { - k_sleep(1); + k_msleep(1); } } #else From 4837b1caa2b7a149769a8e3302350acc4f4bd4d9 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Tue, 9 Jun 2020 18:10:40 -0500 Subject: [PATCH 0067/3533] zephyr: Convert DT_FLASH_AREA usages to new dts macros. Converts DT_FLASH_AREA usages in the zephyr port to new device tree macros introduced in zephyr 2.3. Tested with littlefs on the reel_board. --- ports/zephyr/main.c | 8 ++++++-- ports/zephyr/zephyr_storage.c | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index e304dfd25824d..104954f595c0a 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -38,6 +38,10 @@ #include #endif +#ifdef CONFIG_FLASH_MAP +#include +#endif + #include "py/mperrno.h" #include "py/compile.h" #include "py/runtime.h" @@ -100,8 +104,8 @@ STATIC void vfs_init(void) { mp_obj_t args[] = { mp_obj_new_str(CONFIG_DISK_SDHC_VOLUME_NAME, strlen(CONFIG_DISK_SDHC_VOLUME_NAME)) }; bdev = zephyr_disk_access_type.make_new(&zephyr_disk_access_type, ARRAY_SIZE(args), 0, args); mount_point_str = "/sd"; - #elif defined(CONFIG_FLASH_MAP) && defined(DT_FLASH_AREA_STORAGE_ID) - mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(DT_FLASH_AREA_STORAGE_ID), MP_OBJ_NEW_SMALL_INT(4096) }; + #elif defined(CONFIG_FLASH_MAP) && FLASH_AREA_LABEL_EXISTS(storage) + mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(FLASH_AREA_ID(storage)), MP_OBJ_NEW_SMALL_INT(4096) }; bdev = zephyr_flash_area_type.make_new(&zephyr_flash_area_type, ARRAY_SIZE(args), 0, args); mount_point_str = "/flash"; #endif diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c index c533cb8fd721b..83f19a8feeadd 100644 --- a/ports/zephyr/zephyr_storage.c +++ b/ports/zephyr/zephyr_storage.c @@ -245,8 +245,8 @@ STATIC const mp_rom_map_elem_t zephyr_flash_area_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&zephyr_flash_area_readblocks_obj) }, { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&zephyr_flash_area_writeblocks_obj) }, { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&zephyr_flash_area_ioctl_obj) }, - #ifdef DT_FLASH_AREA_STORAGE_ID - { MP_ROM_QSTR(MP_QSTR_STORAGE), MP_ROM_INT(DT_FLASH_AREA_STORAGE_ID) }, + #if FLASH_AREA_LABEL_EXISTS(storage) + { MP_ROM_QSTR(MP_QSTR_STORAGE), MP_ROM_INT(FLASH_AREA_ID(storage)) }, #endif }; STATIC MP_DEFINE_CONST_DICT(zephyr_flash_area_locals_dict, zephyr_flash_area_locals_dict_table); From b1651ff092e86099b39cba6fe5abfb3ad7514daf Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Tue, 9 Jun 2020 18:22:04 -0500 Subject: [PATCH 0068/3533] zephyr: Increase minimum required cmake version to 3.13.1. The minimum required cmake version has been 3.13.1 since zephyr 1.14.0. --- ports/zephyr/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 017b0689cef58..2cc19a93f1a4d 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.13.1) include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) project(NONE) From 1ae861819dbe8a57cdfda882b093d8d564d8f1c9 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Tue, 9 Jun 2020 18:28:09 -0500 Subject: [PATCH 0069/3533] zephyr: Use cmake find_package to locate zephyr. Updates the zephyr port to use the ZEPHYR_BASE environment variable only to locate the zephyr cmake package, allowing cmake to cache the variable. --- ports/zephyr/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 2cc19a93f1a4d..50cd031d4dd08 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13.1) -include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(NONE) target_sources(app PRIVATE src/zephyr_start.c src/zephyr_getchar.c) From 2b9900380ad2d0ab232e5e3a93c64aaddf90c55a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Jun 2020 10:15:25 +1000 Subject: [PATCH 0070/3533] stm32/boards/STM32F769DISC: Use macro instead of const for flash size. So that the flash size can be changed in just one place. Also remove the duplicate cache entry. --- ports/stm32/boards/STM32F769DISC/board_init.c | 1 - ports/stm32/boards/STM32F769DISC/mpconfigboard.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/stm32/boards/STM32F769DISC/board_init.c b/ports/stm32/boards/STM32F769DISC/board_init.c index 67fad407a0e28..6bb3dfb1f6178 100644 --- a/ports/stm32/boards/STM32F769DISC/board_init.c +++ b/ports/stm32/boards/STM32F769DISC/board_init.c @@ -9,7 +9,6 @@ const mp_spiflash_config_t spiflash_config = { .bus_kind = MP_SPIFLASH_BUS_QSPI, .bus.u_qspi.data = NULL, .bus.u_qspi.proto = &qspi_proto, - .cache = NULL, .cache = &spi_bdev_cache, }; diff --git a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h index 8dbac2d01574a..e1fe93bb0ed44 100644 --- a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h @@ -41,7 +41,7 @@ extern struct _spi_bdev_t spi_bdev; #if !USE_QSPI_XIP #define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) #define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ - (op) == BDEV_IOCTL_NUM_BLOCKS ? (64 * 1024 * 1024 / FLASH_BLOCK_SIZE) : \ + (op) == BDEV_IOCTL_NUM_BLOCKS ? ((1 << MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2) / 8 / FLASH_BLOCK_SIZE) : \ (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ spi_bdev_ioctl(&spi_bdev, (op), (arg)) \ ) From 5093597542de86714a1eb5a8a4463d4234c2f2cd Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Jun 2020 11:38:24 +1000 Subject: [PATCH 0071/3533] top: Update contribution and commit guide to include optional sign-off. MicroPython already requires contributors to implicitly sign-off on a set of points, which are listed in CODECONVENTIONS.md. This commit adjusts this wording to allow explicit sign-off using the git "Signed-off-by:" feature. There is no reference made to https://developercertificate.org/ because the project already has its own version of this. Signed-off-by: Damien George --- CODECONVENTIONS.md | 16 +++++++++++----- CONTRIBUTING.md | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CODECONVENTIONS.md b/CODECONVENTIONS.md index b18c9818ade25..78fb912a6ab75 100644 --- a/CODECONVENTIONS.md +++ b/CODECONVENTIONS.md @@ -27,9 +27,12 @@ change beyond 5 lines would likely require such detailed description. To get good practical examples of good commits and their messages, browse the `git log` of the project. -MicroPython doesn't require explicit sign-off for patches ("Signed-off-by" -lines and similar). Instead, the commit message, and your name and email -address on it construes your sign-off of the following: +When committing you are encouraged to sign-off your commit by adding +"Signed-off-by" lines and similar, eg using "git commit -s". If you don't +explicitly sign-off in this way then the commit message, which includes your +name and email address in the "Author" line, implies your sign-off. In either +case, of explicit or implicit sign-off, you are certifying and signing off +against the following: * That you wrote the change yourself, or took it from a project with a compatible license (in the latter case the commit message, and possibly @@ -43,8 +46,11 @@ address on it construes your sign-off of the following: copyright for your changes (for smaller changes, the commit message conveys your copyright; if you make significant changes to a particular source module, you're welcome to add your name to the file header). -* Your signature for all of the above, which is the 'Author' line in - the commit message, and which should include your full real name and +* Your contribution including commit message will be publicly and + indefinitely available for anyone to access, including redistribution + under the terms of the project's license. +* Your signature for all of the above, which is the "Signed-off-by" line + or the "Author" line in the commit message, includes your full real name and a valid and active email address by which you can be contacted in the foreseeable future. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06f970607b9bb..73004ac5181ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,6 +3,6 @@ make sure that you are acquainted with Contributor Guidelines: https://github.com/micropython/micropython/wiki/ContributorGuidelines -and Code Conventions: +as well as the Code Conventions, which includes details of how to commit: https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md From bd7c92e17d7544ad840038ef74b1998854060090 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 13 Jun 2020 13:03:00 -0500 Subject: [PATCH 0072/3533] tools/uncrustify.cfg: Remove deprecated sp_word_brace option. This option was removed in uncrustify v0.71. Signed-off-by: David Lechner --- tools/uncrustify.cfg | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tools/uncrustify.cfg b/tools/uncrustify.cfg index b0b1239b1a1fb..da422ceb0f4d7 100644 --- a/tools/uncrustify.cfg +++ b/tools/uncrustify.cfg @@ -1,4 +1,4 @@ -# Uncrustify-0.70.1 +# Uncrustify-0.71.0 # # General options @@ -669,12 +669,6 @@ sp_try_brace = ignore # ignore/add/remove/force # Add or remove space between get/set and '{' if on the same line. sp_getset_brace = ignore # ignore/add/remove/force -# Add or remove space between a variable and '{' for C++ uniform -# initialization. -# -# Default: add -sp_word_brace = add # ignore/add/remove/force - # Add or remove space between a variable and '{' for a namespace. # # Default: add From ecd782631608636f300d6b885b4f310e546a2b3f Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 13 Jun 2020 13:05:48 -0500 Subject: [PATCH 0073/3533] tools/codeformat.py: Remove sizeof fixup. Formatting for `* sizeof` was fixed in uncrustify v0.71, so we no longer need the fixups for it. Also, there was one file where the updated uncrustify caught a problem that the regex didn't pick up, which is updated in this commit. Signed-off-by: David Lechner --- py/objfun.c | 4 ++-- tools/codeformat.py | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/py/objfun.c b/py/objfun.c index 3a63d8f439d2b..f9a6b4ebc2727 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -197,8 +197,8 @@ STATIC void dump_args(const mp_obj_t *a, size_t sz) { MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state_out_var, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args); \ \ /* state size in bytes */ \ - state_size_out_var = n_state_out_var *sizeof(mp_obj_t) \ - + n_exc_stack *sizeof(mp_exc_stack_t); \ + state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \ + + n_exc_stack * sizeof(mp_exc_stack_t); \ } #define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \ diff --git a/tools/codeformat.py b/tools/codeformat.py index 5aef4ccfbf242..364037d2fac86 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -76,9 +76,6 @@ PY_EXTS = (".py",) -FIXUP_REPLACEMENTS = ((re.compile("sizeof\(([a-z_]+)\) \*\(([a-z_]+)\)"), r"sizeof(\1) * (\2)"),) - - def list_files(paths, exclusions=None, prefix=""): files = set() for pattern in paths: @@ -124,10 +121,6 @@ def fixup_c(filename): if directive == "endif": dedent_stack.pop() - # Apply general regex-based fixups. - for regex, replacement in FIXUP_REPLACEMENTS: - l = regex.sub(replacement, l) - # Write out line. f.write(l) From b4d0d7bf03a787237740b80d2ecdeb0a3ce1d4ff Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sun, 14 Jun 2020 11:02:19 -0500 Subject: [PATCH 0074/3533] tools/uncrustify: Update config for v0.71.0. This is the result of running... uncrustify -c tools/uncrustify.cfg --update-config-with-doc -o tools/uncrustify.cfg ...with some manual fixups to correct places where it changed things it should not have. Essentially it just adds new config parameters introduced in v0.71.0 with their default values. Signed-off-by: David Lechner --- tools/uncrustify.cfg | 118 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/tools/uncrustify.cfg b/tools/uncrustify.cfg index da422ceb0f4d7..96b471742cc51 100644 --- a/tools/uncrustify.cfg +++ b/tools/uncrustify.cfg @@ -1,4 +1,4 @@ -# Uncrustify-0.71.0 +# Uncrustify-0.71.0_f # # General options @@ -37,13 +37,18 @@ string_replace_tab_chars = false # true/false # Improvements to template detection may make this option obsolete. tok_split_gte = false # true/false +# Disable formatting of NL_CONT ('\\n') ended lines (e.g. multiline macros) +disable_processing_nl_cont = false # true/false + # Specify the marker used in comments to disable processing of part of the # file. +# The comment should be used alone in one line. # # Default: *INDENT-OFF* disable_processing_cmt = " *FORMAT-OFF*" # string # Specify the marker used in comments to (re)enable processing in a file. +# The comment should be used alone in one line. # # Default: *INDENT-ON* enable_processing_cmt = " *FORMAT-ON*" # string @@ -604,6 +609,10 @@ sp_catch_paren = ignore # ignore/add/remove/force # in '@catch (something) { }'. If set to ignore, sp_catch_paren is used. sp_oc_catch_paren = ignore # ignore/add/remove/force +# (OC) Add or remove space before Objective-C protocol list +# as in '@protocol Protocol' or '@interface MyClass : NSObject'. +sp_before_oc_proto_list = ignore # ignore/add/remove/force + # (OC) Add or remove space between class name and '(' # in '@interface className(categoryName):BaseClass' sp_oc_classname_paren = ignore # ignore/add/remove/force @@ -669,6 +678,10 @@ sp_try_brace = ignore # ignore/add/remove/force # Add or remove space between get/set and '{' if on the same line. sp_getset_brace = ignore # ignore/add/remove/force +# Add or remove space between a variable and '{' for C++ uniform +# initialization. +sp_word_brace_init_lst = ignore # ignore/add/remove/force + # Add or remove space between a variable and '{' for a namespace. # # Default: add @@ -1107,6 +1120,10 @@ indent_member_single = false # true/false # Spaces to indent single line ('//') comments on lines before code. indent_sing_line_comments = 0 # unsigned number +# When opening a paren for a control statement (if, for, while, etc), increase +# the indent level by this value. Negative values decrease the indent level. +indent_sparen_extra = 0 # number + # Whether to indent trailing single line ('//') comments relative to the code # instead of trying to keep the same absolute column. indent_relative_single_line_comments = false # true/false @@ -1213,12 +1230,19 @@ indent_preserve_sql = false # true/false # Default: true indent_align_assign = false # true/false +# If true, the indentation of the chunks after a '=' sequence will be set at +# LHS token indentation column before '='. +indent_off_after_assign = false # true/false + # Whether to align continued statements at the '('. If false or the '(' is # followed by a newline, the next line indent is one tab. # # Default: true indent_align_paren = false # true/false +# (OC) Whether to indent Objective-C code inside message selectors. +indent_oc_inside_msg_sel = false # true/false + # (OC) Whether to indent Objective-C blocks at brace level instead of usual # rules. indent_oc_block = false # true/false @@ -1278,6 +1302,15 @@ indent_token_after_brace = true # true/false # Whether to indent the body of a C++11 lambda. indent_cpp_lambda_body = false # true/false +# How to indent compound literals that are being returned. +# true: add both the indent from return & the compound literal open brace (ie: +# 2 indent levels) +# false: only indent 1 level, don't add the indent for the open brace, only add +# the indent for the return. +# +# Default: true +indent_compound_literal_return = true # true/false + # (C#) Whether to indent a 'using' block if no braces are used. # # Default: true @@ -1290,6 +1323,12 @@ indent_using_block = true # true/false # 2: When the `:` is a continuation, indent it under `?` indent_ternary_operator = 0 # unsigned number +# Whether to indent the statments inside ternary operator. +indent_inside_ternary_operator = false # true/false + +# If true, the indentation of the chunks after a `return` sequence will be set at return indentation column. +indent_off_after_return = false # true/false + # If true, the indentation of the chunks after a `return new` sequence will be set at return indentation column. indent_off_after_return_new = false # true/false @@ -1323,7 +1362,7 @@ nl_getset_leave_one_liners = false # true/false nl_cs_property_leave_one_liners = false # true/false # Don't split one-line function definitions, as in 'int foo() { return 0; }'. -# night modify nl_func_type_name +# might modify nl_func_type_name nl_func_leave_one_liners = false # true/false # Don't split one-line C++11 lambdas, as in '[]() { return 0; }'. @@ -1430,6 +1469,9 @@ nl_else_brace = remove # ignore/add/remove/force # Add or remove newline between 'else' and 'if'. nl_else_if = ignore # ignore/add/remove/force +# Add or remove newline before '{' opening brace +nl_before_opening_brace_func_class_def = ignore # ignore/add/remove/force + # Add or remove newline before 'if'/'else if' closing parenthesis. nl_before_if_closing_paren = ignore # ignore/add/remove/force @@ -1678,6 +1720,9 @@ nl_func_decl_args = ignore # ignore/add/remove/force # Add or remove newline after each ',' in a function definition. nl_func_def_args = ignore # ignore/add/remove/force +# Add or remove newline after each ',' in a function call. +nl_func_call_args = ignore # ignore/add/remove/force + # Whether to add a newline after each ',' in a function declaration if '(' # and ')' are in different lines. If false, nl_func_decl_args is used instead. nl_func_decl_args_multi_line = false # true/false @@ -1719,6 +1764,9 @@ nl_func_call_empty = ignore # ignore/add/remove/force # has preference over nl_func_call_start_multi_line. nl_func_call_start = ignore # ignore/add/remove/force +# Whether to add a newline before ')' in a function call. +nl_func_call_end = ignore # ignore/add/remove/force + # Whether to add a newline after '(' in a function call if '(' and ')' are in # different lines. nl_func_call_start_multi_line = false # true/false @@ -1731,6 +1779,9 @@ nl_func_call_args_multi_line = false # true/false # different lines. nl_func_call_end_multi_line = false # true/false +# Whether to respect nl_func_call_XXX option incase of closure args. +nl_func_call_args_multi_line_ignore_closures = false # true/false + # Whether to add a newline after '<' of a template parameter list. nl_template_start = false # true/false @@ -1865,7 +1916,7 @@ nl_before_return = false # true/false # close brace. nl_after_return = false # true/false -# (Java) Whether to put a blank line before a member '.' or '->' operators. +# Whether to put a blank line before a member '.' or '->' operators. nl_before_member = ignore # ignore/add/remove/force # (Java) Whether to put a blank line after a member '.' or '->' operators. @@ -2128,6 +2179,26 @@ nl_after_annotation = ignore # ignore/add/remove/force # (Java) Add or remove newline between two annotations. nl_between_annotation = ignore # ignore/add/remove/force +# The number of newlines before a whole-file #ifdef. +# +# 0: No change (default). +nl_before_whole_file_ifdef = 0 # unsigned number + +# The number of newlines after a whole-file #ifdef. +# +# 0: No change (default). +nl_after_whole_file_ifdef = 0 # unsigned number + +# The number of newlines before a whole-file #endif. +# +# 0: No change (default). +nl_before_whole_file_endif = 0 # unsigned number + +# The number of newlines after a whole-file #endif. +# +# 0: No change (default). +nl_after_whole_file_endif = 0 # unsigned number + # # Positioning options # @@ -2497,6 +2568,11 @@ align_oc_msg_colon_first = false # true/false # on the ':'. align_oc_decl_colon = false # true/false +# (OC) Whether to not align parameters in an Objectve-C message call if first +# colon is not on next line of the message call (the same way Xcode does +# aligment) +align_oc_msg_colon_xcode_like = false # true/false + # # Comment modification options # @@ -2728,6 +2804,25 @@ mod_sort_using = false # true/false # break your code if your includes/imports have ordering dependencies. mod_sort_include = false # true/false +# Whether to prioritize '#include' and '#import' statements that contain +# filename without extension when sorting is enabled. +mod_sort_incl_import_prioritize_filename = false # true/false + +# Whether to prioritize '#include' and '#import' statements that does not +# contain extensions when sorting is enabled. +mod_sort_incl_import_prioritize_extensionless = false # true/false + +# Whether to prioritize '#include' and '#import' statements that contain +# angle over quotes when sorting is enabled. +mod_sort_incl_import_prioritize_angle_over_quotes = false # true/false + +# Whether to ignore file extension in '#include' and '#import' statements +# for sorting comparison. +mod_sort_incl_import_ignore_extension = false # true/false + +# Whether to group '#include' and '#import' statements when sorting is enabled. +mod_sort_incl_import_grouping_enabled = false # true/false + # Whether to move a 'break' that appears after a fully braced 'case' before # the close brace, as in 'case X: { ... } break;' => 'case X: { ... break; }'. mod_move_case_break = false # true/false @@ -2914,6 +3009,11 @@ use_sp_after_angle_always = false # true/false # Default: true use_options_overriding_for_qt_macros = true # true/false +# If true: the form feed character is removed from the list +# of whitespace characters. +# See https://en.cppreference.com/w/cpp/string/byte/isspace +use_form_feed_no_more_as_whitespace_character = false # true/false + # # Warn levels - 1: error, 2: warning (default), 3: note # @@ -2924,6 +3024,16 @@ use_options_overriding_for_qt_macros = true # true/false # Default: 2 warn_level_tabs_found_in_verbatim_string_literals = 2 # unsigned number +# Limit the number of loops. +# Used by uncrustify.cpp to exit from infinite loop. +# 0: no limit. +debug_max_number_of_loops = 0 # number + +# Set the number of the line to protocol; +# Used in the function prot_the_line if the 2. parameter is zero. +# 0: nothing protocol. +debug_line_number_to_protocol = 0 # number + # Meaning of the settings: # Ignore - do not do any changes # Add - makes sure there is 1 or more space/brace/newline/etc @@ -2976,7 +3086,7 @@ warn_level_tabs_found_in_verbatim_string_literals = 2 # unsigned number # `macro-close END_MESSAGE_MAP` # # -# option(s) with 'not default' value: 0 +# option(s) with 'not default' value: 67 # # Custom types for MicroPython From 0fd91e39b1711772c88cfe4e0aaf817fe3387ba6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 21:42:37 +1000 Subject: [PATCH 0075/3533] py/compile: Convert scope test to SCOPE_IS_COMP_LIKE macro. This macro can be used elsewhere. --- py/compile.c | 2 +- py/scope.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/py/compile.c b/py/compile.c index 22f47ee0d295f..8835ec2f8d06d 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3083,7 +3083,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); } EMIT(return_value); - } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { + } else if (SCOPE_IS_COMP_LIKE(scope->kind)) { // a bit of a hack at the moment assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); diff --git a/py/scope.h b/py/scope.h index ba07c39498b5a..b52d98ea1c72d 100644 --- a/py/scope.h +++ b/py/scope.h @@ -55,6 +55,7 @@ typedef struct _id_info_t { } id_info_t; #define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA) +#define SCOPE_IS_COMP_LIKE(s) (SCOPE_LIST_COMP <= (s) && (s) <= SCOPE_GEN_EXPR) // scope is a "block" in Python parlance typedef enum { From 178395031157f61af5426add2de1d76d91440b21 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 21:42:44 +1000 Subject: [PATCH 0076/3533] py/compile: Implement PEP 572, assignment expressions with := operator. The syntax matches CPython and the semantics are equivalent except that, unlike CPython, MicroPython allows using := to assign to comprehension iteration variables, because disallowing this would take a lot of code to check for it. The new compile-time option MICROPY_PY_ASSIGN_EXPR selects this feature and is enabled by default, following MICROPY_PY_ASYNC_AWAIT. --- py/compile.c | 29 ++++++++++++++++++++++++++++- py/grammar.h | 21 ++++++++++++++++----- py/lexer.c | 6 ++++-- py/lexer.h | 1 + py/mpconfig.h | 5 +++++ tests/cmdline/cmd_parsetree.py.exp | 2 +- 6 files changed, 55 insertions(+), 9 deletions(-) diff --git a/py/compile.c b/py/compile.c index 8835ec2f8d06d..da5c9190a3ef4 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2015 Damien P. George + * Copyright (c) 2013-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -2108,6 +2108,27 @@ STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) { compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist); } +#if MICROPY_PY_ASSIGN_EXPR +STATIC void compile_namedexpr_helper(compiler_t *comp, mp_parse_node_t pn_name, mp_parse_node_t pn_expr) { + if (!MP_PARSE_NODE_IS_ID(pn_name)) { + compile_syntax_error(comp, (mp_parse_node_t)pn_name, MP_ERROR_TEXT("can't assign to expression")); + } + compile_node(comp, pn_expr); + EMIT(dup_top); + scope_t *old_scope = comp->scope_cur; + if (SCOPE_IS_COMP_LIKE(comp->scope_cur->kind)) { + // Use parent's scope for assigned value so it can "escape" + comp->scope_cur = comp->scope_cur->parent; + } + compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pn_name)); + comp->scope_cur = old_scope; +} + +STATIC void compile_namedexpr(compiler_t *comp, mp_parse_node_struct_t *pns) { + compile_namedexpr_helper(comp, pns->nodes[0], pns->nodes[1]); +} +#endif + STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) { bool cond = MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test; uint l_end = comp_next_label(comp); @@ -2353,6 +2374,12 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar star_flags |= MP_EMIT_STAR_FLAG_DOUBLE; dblstar_args_node = pns_arg; } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) { + #if MICROPY_PY_ASSIGN_EXPR + if (MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_argument_4)) { + compile_namedexpr_helper(comp, pns_arg->nodes[0], ((mp_parse_node_struct_t *)pns_arg->nodes[1])->nodes[0]); + n_positional++; + } else + #endif if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) { if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) { compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("LHS of keyword arg must be an id")); diff --git a/py/grammar.h b/py/grammar.h index c3d30cdf7c467..f5f1ff666d9e3 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2015 Damien P. George + * Copyright (c) 2013-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -184,10 +184,10 @@ DEF_RULE_NC(async_stmt_2, or(3), rule(funcdef), rule(with_stmt), rule(for_stmt)) #else DEF_RULE_NC(compound_stmt, or(8), rule(if_stmt), rule(while_stmt), rule(for_stmt), rule(try_stmt), rule(with_stmt), rule(funcdef), rule(classdef), rule(decorated)) #endif -DEF_RULE(if_stmt, c(if_stmt), and(6), tok(KW_IF), rule(test), tok(DEL_COLON), rule(suite), opt_rule(if_stmt_elif_list), opt_rule(else_stmt)) +DEF_RULE(if_stmt, c(if_stmt), and(6), tok(KW_IF), rule(namedexpr_test), tok(DEL_COLON), rule(suite), opt_rule(if_stmt_elif_list), opt_rule(else_stmt)) DEF_RULE_NC(if_stmt_elif_list, one_or_more, rule(if_stmt_elif)) -DEF_RULE_NC(if_stmt_elif, and(4), tok(KW_ELIF), rule(test), tok(DEL_COLON), rule(suite)) -DEF_RULE(while_stmt, c(while_stmt), and(5), tok(KW_WHILE), rule(test), tok(DEL_COLON), rule(suite), opt_rule(else_stmt)) +DEF_RULE_NC(if_stmt_elif, and(4), tok(KW_ELIF), rule(namedexpr_test), tok(DEL_COLON), rule(suite)) +DEF_RULE(while_stmt, c(while_stmt), and(5), tok(KW_WHILE), rule(namedexpr_test), tok(DEL_COLON), rule(suite), opt_rule(else_stmt)) DEF_RULE(for_stmt, c(for_stmt), and(7), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(testlist), tok(DEL_COLON), rule(suite), opt_rule(else_stmt)) DEF_RULE(try_stmt, c(try_stmt), and(4), tok(KW_TRY), tok(DEL_COLON), rule(suite), rule(try_stmt_2)) DEF_RULE_NC(try_stmt_2, or(2), rule(try_stmt_except_and_more), rule(try_stmt_finally)) @@ -210,6 +210,12 @@ DEF_RULE(suite_block_stmts, c(generic_all_nodes), one_or_more, rule(stmt)) // lambdef: 'lambda' [varargslist] ':' test // lambdef_nocond: 'lambda' [varargslist] ':' test_nocond +#if MICROPY_PY_ASSIGN_EXPR +DEF_RULE(namedexpr_test, c(namedexpr), and_ident(2), rule(test), opt_rule(namedexpr_test_2)) +DEF_RULE_NC(namedexpr_test_2, and_ident(2), tok(OP_ASSIGN), rule(test)) +#else +DEF_RULE_NC(namedexpr_test, or(1), rule(test)) +#endif DEF_RULE_NC(test, or(2), rule(lambdef), rule(test_if_expr)) DEF_RULE(test_if_expr, c(test_if_expr), and_ident(2), rule(or_test), opt_rule(test_if_else)) DEF_RULE_NC(test_if_else, and(4), tok(KW_IF), rule(or_test), tok(KW_ELSE), rule(test)) @@ -276,7 +282,7 @@ DEF_RULE_NC(atom_2b, or(2), rule(yield_expr), rule(testlist_comp)) DEF_RULE(atom_bracket, c(atom_bracket), and(3), tok(DEL_BRACKET_OPEN), opt_rule(testlist_comp), tok(DEL_BRACKET_CLOSE)) DEF_RULE(atom_brace, c(atom_brace), and(3), tok(DEL_BRACE_OPEN), opt_rule(dictorsetmaker), tok(DEL_BRACE_CLOSE)) DEF_RULE_NC(testlist_comp, and_ident(2), rule(testlist_comp_2), opt_rule(testlist_comp_3)) -DEF_RULE_NC(testlist_comp_2, or(2), rule(star_expr), rule(test)) +DEF_RULE_NC(testlist_comp_2, or(2), rule(star_expr), rule(namedexpr_test)) DEF_RULE_NC(testlist_comp_3, or(2), rule(comp_for), rule(testlist_comp_3b)) DEF_RULE_NC(testlist_comp_3b, and_ident(2), tok(DEL_COMMA), opt_rule(testlist_comp_3c)) DEF_RULE_NC(testlist_comp_3c, list_with_end, rule(testlist_comp_2), tok(DEL_COMMA)) @@ -342,7 +348,12 @@ DEF_RULE_NC(arglist_dbl_star, and(2), tok(OP_DBL_STAR), rule(test)) // comp_if: 'if' test_nocond [comp_iter] DEF_RULE_NC(argument, and_ident(2), rule(test), opt_rule(argument_2)) +#if MICROPY_PY_ASSIGN_EXPR +DEF_RULE_NC(argument_2, or(3), rule(comp_for), rule(argument_3), rule(argument_4)) +DEF_RULE_NC(argument_4, and(2), tok(OP_ASSIGN), rule(test)) +#else DEF_RULE_NC(argument_2, or(2), rule(comp_for), rule(argument_3)) +#endif DEF_RULE_NC(argument_3, and_ident(2), tok(DEL_EQUAL), rule(test)) DEF_RULE_NC(comp_iter, or(2), rule(comp_for), rule(comp_if)) DEF_RULE_NC(comp_for, and_blank(5), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(or_test), opt_rule(comp_iter)) diff --git a/py/lexer.c b/py/lexer.c index 10bb999af6b70..7d2a251d41d75 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -174,7 +174,8 @@ STATIC void indent_pop(mp_lexer_t *lex) { // this means if the start of two ops are the same then they are equal til the last char STATIC const char *const tok_enc = - "()[]{},:;~" // singles + "()[]{},;~" // singles + ":e=" // : := " >= >> >>= "*e=c*e=" // * *= ** **= @@ -194,8 +195,9 @@ STATIC const uint8_t tok_enc_kind[] = { MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE, MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE, MP_TOKEN_DEL_BRACE_OPEN, MP_TOKEN_DEL_BRACE_CLOSE, - MP_TOKEN_DEL_COMMA, MP_TOKEN_DEL_COLON, MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_OP_TILDE, + MP_TOKEN_DEL_COMMA, MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_OP_TILDE, + MP_TOKEN_DEL_COLON, MP_TOKEN_OP_ASSIGN, MP_TOKEN_OP_LESS, MP_TOKEN_OP_LESS_EQUAL, MP_TOKEN_OP_DBL_LESS, MP_TOKEN_DEL_DBL_LESS_EQUAL, MP_TOKEN_OP_MORE, MP_TOKEN_OP_MORE_EQUAL, MP_TOKEN_OP_DBL_MORE, MP_TOKEN_DEL_DBL_MORE_EQUAL, MP_TOKEN_OP_STAR, MP_TOKEN_DEL_STAR_EQUAL, MP_TOKEN_OP_DBL_STAR, MP_TOKEN_DEL_DBL_STAR_EQUAL, diff --git a/py/lexer.h b/py/lexer.h index b9f97013a1bd9..91767a44bf991 100644 --- a/py/lexer.h +++ b/py/lexer.h @@ -96,6 +96,7 @@ typedef enum _mp_token_kind_t { MP_TOKEN_KW_WITH, MP_TOKEN_KW_YIELD, + MP_TOKEN_OP_ASSIGN, MP_TOKEN_OP_TILDE, // Order of these 6 matches corresponding mp_binary_op_t operator diff --git a/py/mpconfig.h b/py/mpconfig.h index 27df3f483a0e1..287b15aaef32f 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -835,6 +835,11 @@ typedef double mp_float_t; #define MICROPY_PY_ASYNC_AWAIT (1) #endif +// Support for assignment expressions with := (see PEP 572, Python 3.8+) +#ifndef MICROPY_PY_ASSIGN_EXPR +#define MICROPY_PY_ASSIGN_EXPR (1) +#endif + // Non-standard .pend_throw() method for generators, allowing for // Future-like behavior with respect to exception handling: an // exception set with .pend_throw() will activate on the next call diff --git a/tests/cmdline/cmd_parsetree.py.exp b/tests/cmdline/cmd_parsetree.py.exp index 18986318a02b4..42a8228fb77e1 100644 --- a/tests/cmdline/cmd_parsetree.py.exp +++ b/tests/cmdline/cmd_parsetree.py.exp @@ -3,7 +3,7 @@ tok(4) [ 4] rule(22) (n=4) id(i) -[ 4] rule(44) (n=1) +[ 4] rule(45) (n=1) NULL [ 5] rule(8) (n=0) NULL From 2c5993c59e083d11ba8b85e82eeea9c5020ac553 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 21:42:48 +1000 Subject: [PATCH 0077/3533] ports: Disable MICROPY_PY_ASSIGN_EXPR in bare-arm and minimal ports. To keep these ports as minimal as possible. --- ports/bare-arm/mpconfigport.h | 1 + ports/minimal/mpconfigport.h | 1 + 2 files changed, 2 insertions(+) diff --git a/ports/bare-arm/mpconfigport.h b/ports/bare-arm/mpconfigport.h index 4567676580324..7fd236bfba9e4 100644 --- a/ports/bare-arm/mpconfigport.h +++ b/ports/bare-arm/mpconfigport.h @@ -21,6 +21,7 @@ #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) #define MICROPY_PY_ASYNC_AWAIT (0) +#define MICROPY_PY_ASSIGN_EXPR (0) #define MICROPY_PY_BUILTINS_BYTEARRAY (0) #define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) #define MICROPY_PY_BUILTINS_MEMORYVIEW (0) diff --git a/ports/minimal/mpconfigport.h b/ports/minimal/mpconfigport.h index c3bdf66c10ae2..b34217f68019a 100644 --- a/ports/minimal/mpconfigport.h +++ b/ports/minimal/mpconfigport.h @@ -19,6 +19,7 @@ #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) #define MICROPY_PY_ASYNC_AWAIT (0) +#define MICROPY_PY_ASSIGN_EXPR (0) #define MICROPY_PY_BUILTINS_BYTEARRAY (0) #define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) #define MICROPY_PY_BUILTINS_ENUMERATE (0) From e0fe8ea644b54406ca82cefdc73c98cc2e9cbe9f Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 21:42:50 +1000 Subject: [PATCH 0078/3533] tests/basics: Add tests for assignment operator :=. --- tests/basics/assign_expr.py | 29 +++++++++++++++++++++ tests/basics/assign_expr.py.exp | 14 ++++++++++ tests/basics/assign_expr_syntaxerror.py | 16 ++++++++++++ tests/basics/assign_expr_syntaxerror.py.exp | 6 +++++ 4 files changed, 65 insertions(+) create mode 100644 tests/basics/assign_expr.py create mode 100644 tests/basics/assign_expr.py.exp create mode 100644 tests/basics/assign_expr_syntaxerror.py create mode 100644 tests/basics/assign_expr_syntaxerror.py.exp diff --git a/tests/basics/assign_expr.py b/tests/basics/assign_expr.py new file mode 100644 index 0000000000000..f243905dc2a63 --- /dev/null +++ b/tests/basics/assign_expr.py @@ -0,0 +1,29 @@ +(x := 4) +print(x) + +if x := 2: + print(True) +print(x) + +print(4, x := 5) +print(x) + +x = 1 +print(x, x := 5, x) +print(x) + + +def foo(): + print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) + return hit + + +hit = 123 +print(foo()) +print(hit) # shouldn't be changed by foo + +print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) +print(hit) # should be changed by above + +print([((m := k + 1), k * m) for k in range(4)]) +print(m) diff --git a/tests/basics/assign_expr.py.exp b/tests/basics/assign_expr.py.exp new file mode 100644 index 0000000000000..e38e1ae7a626c --- /dev/null +++ b/tests/basics/assign_expr.py.exp @@ -0,0 +1,14 @@ +4 +True +2 +4 5 +5 +1 5 5 +5 +any True +8 +123 +any True +8 +[(1, 0), (2, 2), (3, 6), (4, 12)] +4 diff --git a/tests/basics/assign_expr_syntaxerror.py b/tests/basics/assign_expr_syntaxerror.py new file mode 100644 index 0000000000000..11b3501292546 --- /dev/null +++ b/tests/basics/assign_expr_syntaxerror.py @@ -0,0 +1,16 @@ +# test SyntaxError with := operator + +def test(code): + try: + print(eval(code)) + except SyntaxError: + print('SyntaxError') + +test("x := 1") +test("((x, y) := 1)") + +# these are currently all allowed in MicroPython, but not in CPython +test("([i := i + 1 for i in range(4)])") +test("([i := -1 for i, j in [(1, 2)]])") +test("([[(i := j) for i in range(2)] for j in range(2)])") +test("([[(j := i) for i in range(2)] for j in range(2)])") diff --git a/tests/basics/assign_expr_syntaxerror.py.exp b/tests/basics/assign_expr_syntaxerror.py.exp new file mode 100644 index 0000000000000..2ba7d7df869eb --- /dev/null +++ b/tests/basics/assign_expr_syntaxerror.py.exp @@ -0,0 +1,6 @@ +SyntaxError +SyntaxError +[1, 2, 3, 4] +[-1] +[[0, 0], [1, 1]] +[[0, 1], [0, 1]] From a3c89cf907a3c2b7235ea86e9a229335212b9020 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 21:42:52 +1000 Subject: [PATCH 0079/3533] tests/cpydiff: Add CPy diff test for assignment expression behaviour. --- tests/cpydiff/syntax_assign_expr.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/cpydiff/syntax_assign_expr.py diff --git a/tests/cpydiff/syntax_assign_expr.py b/tests/cpydiff/syntax_assign_expr.py new file mode 100644 index 0000000000000..d4ed063b39ae7 --- /dev/null +++ b/tests/cpydiff/syntax_assign_expr.py @@ -0,0 +1,7 @@ +""" +categories: Syntax,Operators +description: MicroPython allows using := to assign to the variable of a comprehension, CPython raises a SyntaxError. +cause: MicroPython is optimised for code size and doesn't check this case. +workaround: Do not rely on this behaviour if writing CPython compatible code. +""" +print([i := -1 for i in range(4)]) From 131b0de70a47861c266d2ac7c26ae643f679d2f7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 22:48:46 +1000 Subject: [PATCH 0080/3533] py/grammar.h: Consolidate duplicate sub-rules for :test and =test. --- py/compile.c | 2 +- py/grammar.h | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/py/compile.c b/py/compile.c index da5c9190a3ef4..614e851a3d8ac 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2375,7 +2375,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar dblstar_args_node = pns_arg; } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) { #if MICROPY_PY_ASSIGN_EXPR - if (MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_argument_4)) { + if (MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_argument_3)) { compile_namedexpr_helper(comp, pns_arg->nodes[0], ((mp_parse_node_struct_t *)pns_arg->nodes[1])->nodes[0]); n_positional++; } else diff --git a/py/grammar.h b/py/grammar.h index f5f1ff666d9e3..ff8e61828e862 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -30,6 +30,11 @@ // - zero_or_more is implemented using opt_rule around a one_or_more rule // - don't put opt_rule in arguments of or rule; instead, wrap the call to this or rule in opt_rule +// Generic sub-rules used by multiple rules below. + +DEF_RULE_NC(generic_colon_test, and_ident(2), tok(DEL_COLON), rule(test)) +DEF_RULE_NC(generic_equal_test, and_ident(2), tok(DEL_EQUAL), rule(test)) + // # Start symbols for the grammar: // # single_input is a single interactive statement; // # file_input is a module or sequence of commands read from an input file; @@ -71,19 +76,16 @@ DEF_RULE_NC(funcdefrettype, and_ident(2), tok(DEL_MINUS_MORE), rule(test)) // note: typedargslist lets through more than is allowed, compiler does further checks DEF_RULE_NC(typedargslist, list_with_end, rule(typedargslist_item), tok(DEL_COMMA)) DEF_RULE_NC(typedargslist_item, or(3), rule(typedargslist_name), rule(typedargslist_star), rule(typedargslist_dbl_star)) -DEF_RULE_NC(typedargslist_name, and_ident(3), tok(NAME), opt_rule(typedargslist_colon), opt_rule(typedargslist_equal)) +DEF_RULE_NC(typedargslist_name, and_ident(3), tok(NAME), opt_rule(generic_colon_test), opt_rule(generic_equal_test)) DEF_RULE_NC(typedargslist_star, and(2), tok(OP_STAR), opt_rule(tfpdef)) -DEF_RULE_NC(typedargslist_dbl_star, and(3), tok(OP_DBL_STAR), tok(NAME), opt_rule(typedargslist_colon)) -DEF_RULE_NC(typedargslist_colon, and_ident(2), tok(DEL_COLON), rule(test)) -DEF_RULE_NC(typedargslist_equal, and_ident(2), tok(DEL_EQUAL), rule(test)) -DEF_RULE_NC(tfpdef, and(2), tok(NAME), opt_rule(typedargslist_colon)) +DEF_RULE_NC(typedargslist_dbl_star, and(3), tok(OP_DBL_STAR), tok(NAME), opt_rule(generic_colon_test)) +DEF_RULE_NC(tfpdef, and(2), tok(NAME), opt_rule(generic_colon_test)) // note: varargslist lets through more than is allowed, compiler does further checks DEF_RULE_NC(varargslist, list_with_end, rule(varargslist_item), tok(DEL_COMMA)) DEF_RULE_NC(varargslist_item, or(3), rule(varargslist_name), rule(varargslist_star), rule(varargslist_dbl_star)) -DEF_RULE_NC(varargslist_name, and_ident(2), tok(NAME), opt_rule(varargslist_equal)) +DEF_RULE_NC(varargslist_name, and_ident(2), tok(NAME), opt_rule(generic_equal_test)) DEF_RULE_NC(varargslist_star, and(2), tok(OP_STAR), opt_rule(vfpdef)) DEF_RULE_NC(varargslist_dbl_star, and(2), tok(OP_DBL_STAR), tok(NAME)) -DEF_RULE_NC(varargslist_equal, and_ident(2), tok(DEL_EQUAL), rule(test)) DEF_RULE_NC(vfpdef, and_ident(1), tok(NAME)) // stmt: compound_stmt | simple_stmt @@ -318,8 +320,7 @@ DEF_RULE(testlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA)) // TODO dictorsetmaker lets through more than is allowed DEF_RULE_NC(dictorsetmaker, and_ident(2), rule(dictorsetmaker_item), opt_rule(dictorsetmaker_tail)) #if MICROPY_PY_BUILTINS_SET -DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and_ident(2), rule(test), opt_rule(dictorsetmaker_colon)) -DEF_RULE_NC(dictorsetmaker_colon, and_ident(2), tok(DEL_COLON), rule(test)) +DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and_ident(2), rule(test), opt_rule(generic_colon_test)) #else DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(3), rule(test), tok(DEL_COLON), rule(test)) #endif @@ -349,12 +350,11 @@ DEF_RULE_NC(arglist_dbl_star, and(2), tok(OP_DBL_STAR), rule(test)) DEF_RULE_NC(argument, and_ident(2), rule(test), opt_rule(argument_2)) #if MICROPY_PY_ASSIGN_EXPR -DEF_RULE_NC(argument_2, or(3), rule(comp_for), rule(argument_3), rule(argument_4)) -DEF_RULE_NC(argument_4, and(2), tok(OP_ASSIGN), rule(test)) +DEF_RULE_NC(argument_2, or(3), rule(comp_for), rule(generic_equal_test), rule(argument_3)) +DEF_RULE_NC(argument_3, and(2), tok(OP_ASSIGN), rule(test)) #else -DEF_RULE_NC(argument_2, or(2), rule(comp_for), rule(argument_3)) +DEF_RULE_NC(argument_2, or(2), rule(comp_for), rule(generic_equal_test)) #endif -DEF_RULE_NC(argument_3, and_ident(2), tok(DEL_EQUAL), rule(test)) DEF_RULE_NC(comp_iter, or(2), rule(comp_for), rule(comp_if)) DEF_RULE_NC(comp_for, and_blank(5), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(or_test), opt_rule(comp_iter)) DEF_RULE_NC(comp_if, and(3), tok(KW_IF), rule(test_nocond), opt_rule(comp_iter)) From f2e267da68bf85f3f12fa77155e469352bdc6c09 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 22:49:25 +1000 Subject: [PATCH 0081/3533] py/compile: Implement PEP 526, syntax for variable annotations. This addition to the grammar was introduced in Python 3.6. It allows annotating the type of a varilable, like: x: int = 123 s: str The implementation in this commit is quite simple and just ignores the annotation (the int and str bits above). The reason to implement this is to allow Python 3.6+ code that uses this feature to compile under MicroPython without change, and for users to use type checkers. In the future viper could use this syntax as a way to give types to variables, which is currently done in a bit of an ad-hoc way, eg x = int(123). And this syntax could potentially be used in the inline assembler to define labels in an way that's easier to read. --- py/compile.c | 31 ++++++++++++++++++++++++------- py/grammar.h | 8 +++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/py/compile.c b/py/compile.c index 614e851a3d8ac..53108b7062b78 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1981,7 +1981,8 @@ STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { #endif STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { - if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { + mp_parse_node_t pn_rhs = pns->nodes[1]; + if (MP_PARSE_NODE_IS_NULL(pn_rhs)) { if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) { // for REPL, evaluate then print the expression compile_load_id(comp, MP_QSTR___repl_print__); @@ -1999,10 +2000,26 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack } } - } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { - mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1]; + } else if (MP_PARSE_NODE_IS_STRUCT(pn_rhs)) { + mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pn_rhs; int kind = MP_PARSE_NODE_STRUCT_KIND(pns1); - if (kind == PN_expr_stmt_augassign) { + if (kind == PN_annassign) { + // the annotation is in pns1->nodes[0] and is ignored + if (MP_PARSE_NODE_IS_NULL(pns1->nodes[1])) { + // an annotation of the form "x: y" + // inside a function this declares "x" as a local + if (comp->scope_cur->kind == SCOPE_FUNCTION) { + if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) { + qstr lhs = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); + scope_find_or_add_id(comp->scope_cur, lhs, ID_INFO_KIND_LOCAL); + } + } + } else { + // an assigned annotation of the form "x: y = z" + pn_rhs = pns1->nodes[1]; + goto plain_assign; + } + } else if (kind == PN_expr_stmt_augassign) { c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign compile_node(comp, pns1->nodes[1]); // rhs assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0])); @@ -2027,10 +2044,10 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { } else { plain_assign: #if MICROPY_COMP_DOUBLE_TUPLE_ASSIGN - if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr) + if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_rhs, PN_testlist_star_expr) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)) { mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns->nodes[0]; - pns1 = (mp_parse_node_struct_t *)pns->nodes[1]; + pns1 = (mp_parse_node_struct_t *)pn_rhs; uint32_t n_pns0 = MP_PARSE_NODE_STRUCT_NUM_NODES(pns0); // Can only optimise a tuple-to-tuple assignment when all of the following hold: // - equal number of items in LHS and RHS tuples @@ -2070,7 +2087,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { } #endif - compile_node(comp, pns->nodes[1]); // rhs + compile_node(comp, pn_rhs); // rhs c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store } } else { diff --git a/py/grammar.h b/py/grammar.h index ff8e61828e862..285fbded2fdae 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -98,20 +98,22 @@ DEF_RULE_NC(simple_stmt, and_ident(2), rule(simple_stmt_2), tok(NEWLINE)) DEF_RULE(simple_stmt_2, c(generic_all_nodes), list_with_end, rule(small_stmt), tok(DEL_SEMICOLON)) // small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt -// expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*) +// expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*) // testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] +// annassign: ':' test ['=' (yield_expr|testlist_star_expr)] // augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//=' -// # For normal assignments, additional restrictions enforced by the interpreter +// # For normal and annotated assignments, additional restrictions enforced by the interpreter DEF_RULE_NC(small_stmt, or(8), rule(del_stmt), rule(pass_stmt), rule(flow_stmt), rule(import_stmt), rule(global_stmt), rule(nonlocal_stmt), rule(assert_stmt), rule(expr_stmt)) DEF_RULE(expr_stmt, c(expr_stmt), and(2), rule(testlist_star_expr), opt_rule(expr_stmt_2)) -DEF_RULE_NC(expr_stmt_2, or(2), rule(expr_stmt_augassign), rule(expr_stmt_assign_list)) +DEF_RULE_NC(expr_stmt_2, or(3), rule(annassign), rule(expr_stmt_augassign), rule(expr_stmt_assign_list)) DEF_RULE_NC(expr_stmt_augassign, and_ident(2), rule(augassign), rule(expr_stmt_6)) DEF_RULE_NC(expr_stmt_assign_list, one_or_more, rule(expr_stmt_assign)) DEF_RULE_NC(expr_stmt_assign, and_ident(2), tok(DEL_EQUAL), rule(expr_stmt_6)) DEF_RULE_NC(expr_stmt_6, or(2), rule(yield_expr), rule(testlist_star_expr)) DEF_RULE(testlist_star_expr, c(generic_tuple), list_with_end, rule(testlist_star_expr_2), tok(DEL_COMMA)) DEF_RULE_NC(testlist_star_expr_2, or(2), rule(star_expr), rule(test)) +DEF_RULE_NC(annassign, and(3), tok(DEL_COLON), rule(test), opt_rule(expr_stmt_assign)) DEF_RULE_NC(augassign, or(13), tok(DEL_PLUS_EQUAL), tok(DEL_MINUS_EQUAL), tok(DEL_STAR_EQUAL), tok(DEL_AT_EQUAL), tok(DEL_SLASH_EQUAL), tok(DEL_PERCENT_EQUAL), tok(DEL_AMPERSAND_EQUAL), tok(DEL_PIPE_EQUAL), tok(DEL_CARET_EQUAL), tok(DEL_DBL_LESS_EQUAL), tok(DEL_DBL_MORE_EQUAL), tok(DEL_DBL_STAR_EQUAL), tok(DEL_DBL_SLASH_EQUAL)) // del_stmt: 'del' exprlist From a51eef4471e01ee60d53ee184bc4feabf6ebd855 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 22:49:38 +1000 Subject: [PATCH 0082/3533] tests/basics: Add tests for variable annotations. --- tests/basics/annotate_var.py | 25 +++++++++++++++++++++++++ tests/basics/annotate_var.py.exp | 5 +++++ tests/run-tests | 1 + 3 files changed, 31 insertions(+) create mode 100644 tests/basics/annotate_var.py create mode 100644 tests/basics/annotate_var.py.exp diff --git a/tests/basics/annotate_var.py b/tests/basics/annotate_var.py new file mode 100644 index 0000000000000..3f767e4a73505 --- /dev/null +++ b/tests/basics/annotate_var.py @@ -0,0 +1,25 @@ +# test PEP 526, varible annotations + +x: int +print("x" in globals()) + +x: int = 1 +print(x) + +t: tuple = 1, 2 +print(t) + +# a pure annotation in a function makes that variable local +def f(): + x: int + try: + print(x) + except NameError: + print("NameError") +f() + +# here, "x" should remain a global +def f(): + x.y: int + print(x) +f() diff --git a/tests/basics/annotate_var.py.exp b/tests/basics/annotate_var.py.exp new file mode 100644 index 0000000000000..9b6536e966b0e --- /dev/null +++ b/tests/basics/annotate_var.py.exp @@ -0,0 +1,5 @@ +False +1 +(1, 2) +NameError +1 diff --git a/tests/run-tests b/tests/run-tests index 74e2f71ac6480..f9e4de4b34a20 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -414,6 +414,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from_close generator_name'.split()}) # require raise_varargs, generator name skip_tests.update({'basics/async_%s.py' % t for t in 'with with2 with_break with_return'.split()}) # require async_with skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs + skip_tests.add('basics/annotate_var.py') # requires checking for unbound local skip_tests.add('basics/del_deref.py') # requires checking for unbound local skip_tests.add('basics/del_local.py') # requires checking for unbound local skip_tests.add('basics/exception_chain.py') # raise from is not supported From 1678f417445703750e187b1b8a83c8c3e0e3796f Mon Sep 17 00:00:00 2001 From: Jon Rob Date: Mon, 8 Jun 2020 06:29:43 +0000 Subject: [PATCH 0083/3533] esp32/esp32_rmt: Extend RMT to support carrier feature. The ESP32 RMT peripheral has hardware support for a carrier frequency, and this commit exposes it to Python with the keyword arguments carrier_freq and carrier_duty_percent in the constructor. Example usage: r = esp32.RMT(0, pin=Pin(2), clock_div=80, carrier_freq=38000, carrier_duty_percent=50) --- docs/library/esp32.rst | 17 ++++++++++++++--- ports/esp32/esp32_rmt.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index 33b779a214336..715afdddeb169 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -151,6 +151,11 @@ used to transmit or receive many other types of digital signals:: r = esp32.RMT(0, pin=Pin(18), clock_div=8) r # RMT(channel=0, pin=18, source_freq=80000000, clock_div=8) + + # To use carrier frequency + r = esp32.RMT(0, pin=Pin(18), clock_div=8, carrier_freq=38000) + r # RMT(channel=0, pin=18, source_freq=80000000, clock_div=8, carrier_freq=38000, carrier_duty_percent=50) + # The channel resolution is 100ns (1/(source_freq/clock_div)). r.write_pulses((1, 20, 2, 40), start=0) # Send 0 for 100ns, 1 for 2000ns, 0 for 200ns, 1 for 4000ns @@ -164,6 +169,9 @@ define the pulses. multiplying the resolution by a 15-bit (0-32,768) number. There are eight channels (0-7) and each can have a different clock divider. +To enable the carrier frequency feature of the esp32 hardware, specify the +``carrier_freq`` as something like 38000, a typical IR carrier frequency. + So, in the example above, the 80MHz clock is divided by 8. Thus the resolution is (1/(80Mhz/8)) 100ns. Since the ``start`` level is 0 and toggles with each number, the bitstream is ``0101`` with durations of [100ns, 2000ns, @@ -174,17 +182,20 @@ For more details see Espressif's `ESP-IDF RMT documentation. .. Warning:: The current MicroPython RMT implementation lacks some features, most notably - receiving pulses and carrier transmit. RMT should be considered a + receiving pulses. RMT should be considered a *beta feature* and the interface may change in the future. -.. class:: RMT(channel, \*, pin=None, clock_div=8) +.. class:: RMT(channel, \*, pin=None, clock_div=8, carrier_freq=0, carrier_duty_percent=50) This class provides access to one of the eight RMT channels. *channel* is required and identifies which RMT channel (0-7) will be configured. *pin*, also required, configures which Pin is bound to the RMT channel. *clock_div* is an 8-bit clock divider that divides the source clock (80MHz) to the RMT - channel allowing the resolution to be specified. + channel allowing the resolution to be specified. *carrier_freq* is used to + enable the carrier feature and specify its frequency, default value is ``0`` + (not enabled). To enable, specify a positive integer. *carrier_duty_percent* + defaults to 50. .. method:: RMT.source_freq() diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 25b7b3808981c..846c80d478f0b 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -52,6 +52,8 @@ typedef struct _esp32_rmt_obj_t { uint8_t channel_id; gpio_num_t pin; uint8_t clock_div; + uint16_t carrier_duty_percent; + uint32_t carrier_freq; mp_uint_t num_items; rmt_item32_t *items; } esp32_rmt_obj_t; @@ -61,6 +63,8 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_clock_div, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, // 100ns resolution + { MP_QSTR_carrier_duty_percent, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 50} }, + { MP_QSTR_carrier_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -68,6 +72,16 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz gpio_num_t pin_id = machine_pin_get_id(args[1].u_obj); mp_uint_t clock_div = args[2].u_int; + bool carrier_en = false; + mp_uint_t carrier_duty_percent = 0; + mp_uint_t carrier_freq = 0; + + if (args[4].u_int > 0) { + carrier_en = true; + carrier_duty_percent = args[3].u_int; + carrier_freq = args[4].u_int; + } + if (clock_div < 1 || clock_div > 255) { mp_raise_ValueError(MP_ERROR_TEXT("clock_div must be between 1 and 255")); } @@ -77,6 +91,8 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz self->channel_id = channel_id; self->pin = pin_id; self->clock_div = clock_div; + self->carrier_duty_percent = carrier_duty_percent; + self->carrier_freq = carrier_freq; rmt_config_t config; config.rmt_mode = RMT_MODE_TX; @@ -85,11 +101,11 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz config.mem_block_num = 1; config.tx_config.loop_en = 0; - config.tx_config.carrier_en = 0; + config.tx_config.carrier_en = carrier_en; config.tx_config.idle_output_en = 1; config.tx_config.idle_level = 0; - config.tx_config.carrier_duty_percent = 0; - config.tx_config.carrier_freq_hz = 0; + config.tx_config.carrier_duty_percent = self->carrier_duty_percent; + config.tx_config.carrier_freq_hz = self->carrier_freq; config.tx_config.carrier_level = 1; config.clk_div = self->clock_div; @@ -103,8 +119,14 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz STATIC void esp32_rmt_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); if (self->pin != -1) { - mp_printf(print, "RMT(channel=%u, pin=%u, source_freq=%u, clock_div=%u)", + mp_printf(print, "RMT(channel=%u, pin=%u, source_freq=%u, clock_div=%u", self->channel_id, self->pin, APB_CLK_FREQ, self->clock_div); + if (self->carrier_freq > 0) { + mp_printf(print, ", carrier_freq=%u, carrier_duty_percent=%u)", + self->carrier_freq, self->carrier_duty_percent); + } else { + mp_printf(print, ")"); + } } else { mp_printf(print, "RMT()"); } From da99e0f97911864d1803b804249b38344385de11 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 12:23:29 +1000 Subject: [PATCH 0084/3533] stm32/factoryreset: Provide empty create-FS function when FAT disabled. Signed-off-by: Damien George --- ports/stm32/factoryreset.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ports/stm32/factoryreset.c b/ports/stm32/factoryreset.c index 04591101e87df..725ecd12a871c 100644 --- a/ports/stm32/factoryreset.c +++ b/ports/stm32/factoryreset.c @@ -34,6 +34,8 @@ #if MICROPY_HW_ENABLE_STORAGE +#if MICROPY_VFS_FAT + static const char fresh_boot_py[] = "# boot.py -- run on boot-up\r\n" "# can run arbitrary Python, but best to keep it minimal\r\n" @@ -128,4 +130,13 @@ MP_WEAK int factory_reset_create_filesystem(void) { return 0; // success } +#else + +// If FAT is not enabled then it's up to the board to create a fresh filesystem. +MP_WEAK int factory_reset_create_filesystem(void) { + return 0; // success +} + +#endif + #endif // MICROPY_HW_ENABLE_STORAGE From 289be6b352e30e97363c1db98a582eb84c9dd12e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 12:25:41 +1000 Subject: [PATCH 0085/3533] stm32/usb: Add support for 2xVCP on L0, L432 and WB MCUs. There are a maximum of 8 USB endpoints and each has 2 buffer slots (in/out). This commit add support for up to 8 endpoints and adds FIFO configuration for USB profiles with 2xVCP on MCUs that have device-only USB peripherals. Tested on NUCLEO_WB55 in 2xVCP, 2xVCP+MSC and 2xVCP+MSC+HID mode. Signed-off-by: Damien George --- ports/stm32/usb.c | 25 +++++++++++++++++++++++-- ports/stm32/usbd_conf.h | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index cd5238172188e..657d4cb6e7402 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -93,8 +93,29 @@ pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE; // Units of FIFO size arrays below are 4x 16-bit words = 8 bytes // There are 512x 16-bit words it total to use here (when using PCD_SNG_BUF) -// EP0(out), EP0(in), MSC/HID(out), MSC/HID(in), unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) -STATIC const uint8_t usbd_fifo_size_cdc1[] = {16, 16, 16, 16, 0, 16, 16, 16}; +STATIC const uint8_t usbd_fifo_size_cdc1[USBD_PMA_NUM_FIFO] = { + 16, 16, 16, 16, // EP0(out), EP0(in), MSC/HID(out), MSC/HID(in) + 0, 16, 16, 16, // unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) + 0, 0, 0, 0, 0, 0, 0, 0, // 8x unused +}; + +#if MICROPY_HW_USB_CDC_NUM >= 2 +STATIC const uint8_t usbd_fifo_size_cdc2[USBD_PMA_NUM_FIFO] = { + 8, 8, 16, 16, // EP0(out), EP0(in), MSC/HID(out), MSC/HID(in) + 0, 8, 12, 12, // unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) + 0, 8, 12, 12, // unused, CDC2_CMD(in), CDC2_DATA(out), CDC2_DATA(in) + 0, 0, 0, 0, // 4x unused +}; + +// RX; EP0(in), MSC/HID, CDC_CMD, CDC_DATA, CDC2_CMD/HID, CDC2_DATA, HID +STATIC const uint8_t usbd_fifo_size_cdc2_msc_hid[USBD_PMA_NUM_FIFO] = { + 8, 8, 16, 16, // EP0(out), EP0(in), MSC/HID(out), MSC/HID(in) + 0, 8, 8, 8, // unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) + 0, 8, 8, 8, // unused, CDC2_CMD(in), CDC2_DATA(out), CDC2_DATA(in) + 8, 8, // HID(out), HID(in) + 0, 0, // 2x unused +}; +#endif #else diff --git a/ports/stm32/usbd_conf.h b/ports/stm32/usbd_conf.h index 5237ba3a96e72..83805a6e4d801 100644 --- a/ports/stm32/usbd_conf.h +++ b/ports/stm32/usbd_conf.h @@ -51,7 +51,7 @@ // For MCUs with a device-only USB peripheral #define USBD_PMA_RESERVE (64) -#define USBD_PMA_NUM_FIFO (8) +#define USBD_PMA_NUM_FIFO (16) // Maximum 8 endpoints, 2 FIFOs each // For MCUs with multiple OTG USB peripherals #define USBD_FS_NUM_TX_FIFO (6) From 4b5dd012e0fe615001bbd1edf2f85ea30371a1d6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 12:29:02 +1000 Subject: [PATCH 0086/3533] stm32/rfcore: Leave txpower level as default when initialising rfcore. And provide a convenient API function to change it (currently unused). Fixes issue #5985. Signed-off-by: Damien George --- ports/stm32/rfcore.c | 7 ++++++- ports/stm32/rfcore.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 602ef974b262b..54b3393435e1e 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -416,8 +416,13 @@ void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env) SWAP_UINT8(buf[3], buf[6]); SWAP_UINT8(buf[4], buf[5]); tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_WRITE_CONFIG), 8, buf); // set BDADDR - tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), 2, (const uint8_t *)"\x00\x06"); // 0 dBm } } +// "level" is 0x00-0x1f, ranging from -40 dBm to +6 dBm (not linear). +void rfcore_ble_set_txpower(uint8_t level) { + uint8_t buf[2] = { 0x00, level }; + tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), 2, buf); +} + #endif // defined(STM32WB) diff --git a/ports/stm32/rfcore.h b/ports/stm32/rfcore.h index 138c438f12eae..fbe111e1ebdcd 100644 --- a/ports/stm32/rfcore.h +++ b/ports/stm32/rfcore.h @@ -33,5 +33,6 @@ void rfcore_init(void); void rfcore_ble_init(void); void rfcore_ble_hci_cmd(size_t len, const uint8_t *src); void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env); +void rfcore_ble_set_txpower(uint8_t level); #endif // MICROPY_INCLUDED_STM32_RFCORE_H From 8d71cc2e7da7a77aeca66bc8fac0bb0e28690344 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 11 Jun 2020 20:40:04 +0200 Subject: [PATCH 0087/3533] nrf/bluetooth: Use MP_ERROR_TEXT for all error messages. This follows up on commit def76fe4d9bbc2c342594dc05861b24d7165d274. Fixes issue #6152. --- ports/nrf/drivers/bluetooth/ble_drv.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c index 7619dc0399455..47f7f98885a91 100644 --- a/ports/nrf/drivers/bluetooth/ble_drv.c +++ b/ports/nrf/drivers/bluetooth/ble_drv.c @@ -607,12 +607,12 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { #if (BLUETOOTH_SD == 110) if ((err_code = sd_ble_gap_adv_data_set(adv_data, byte_pos, NULL, 0)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not apply advertisment data. status: 0x" HEX2_FMT), (uint16_t)err_code); } #else if ((err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not apply advertisment data. status: 0x" HEX2_FMT), (uint16_t)err_code); } #endif BLE_DRIVER_LOG("Set Adv data size: " UINT_FMT "\n", byte_pos); @@ -627,7 +627,7 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { #endif if (err_code != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not start advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not start advertisment. status: 0x" HEX2_FMT), (uint16_t)err_code); } m_adv_in_progress = true; @@ -642,12 +642,12 @@ void ble_drv_advertise_stop(void) { #if (BLUETOOTH_SD == 110) if ((err_code = sd_ble_gap_adv_stop()) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not stop advertisment. status: 0x" HEX2_FMT), (uint16_t)err_code); } #else if ((err_code = sd_ble_gap_adv_stop(m_adv_handle)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not stop advertisment. status: 0x" HEX2_FMT), (uint16_t)err_code); } #endif } @@ -667,7 +667,7 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui &gatts_value); if (err_code != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not read attribute value. status: 0x" HEX2_FMT), (uint16_t)err_code); } } @@ -684,7 +684,7 @@ void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u if (err_code != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not write attribute value. status: 0x" HEX2_FMT), (uint16_t)err_code); } } @@ -708,7 +708,7 @@ void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len, uint32_t err_code; if ((err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not notify attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not notify attribute value. status: 0x" HEX2_FMT), (uint16_t)err_code); } m_tx_in_progress++; BLE_DRIVER_LOG("Queued TX, m_tx_in_progress: %u\n", m_tx_in_progress); @@ -747,7 +747,7 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl 0); if (err_code != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not read attribute value. status: 0x" HEX2_FMT), (uint16_t)err_code); } while (gattc_char_data_handle != NULL) { @@ -777,7 +777,7 @@ void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u if (err_code != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not write attribute value. status: 0x" HEX2_FMT), (uint16_t)err_code); } while (m_write_done != true) { @@ -808,7 +808,7 @@ void ble_drv_scan_start(bool cont) { } if ((err_code = sd_ble_gap_scan_start(p_scan_params, &scan_buffer)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not start scanning. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not start scanning. status: 0x" HEX2_FMT), (uint16_t)err_code); } } @@ -854,7 +854,7 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) { &conn_params, conn_tag)) != 0) { mp_raise_msg_varg(&mp_type_OSError, - "Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code); + MP_ERROR_TEXT("Can not connect. status: 0x" HEX2_FMT), (uint16_t)err_code); } } From cba3e25cb33007e4c0b1a0c476ecce7166fb089f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Jun 2020 11:39:52 +1000 Subject: [PATCH 0088/3533] travis: Change nrf pca10056 board to build with soft-device enabled. To test building with SD which enables a lot of additional code. Signed-off-by: Damien George --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 414fe2b59d108..1cb5575c73636 100644 --- a/.travis.yml +++ b/.travis.yml @@ -292,10 +292,11 @@ jobs: - sudo apt-get install libnewlib-arm-none-eabi - arm-none-eabi-gcc --version script: + - ports/nrf/drivers/bluetooth/download_ble_stack.sh s132_nrf52_6_1_1 - make ${MAKEOPTS} -C ports/nrf submodules - make ${MAKEOPTS} -C ports/nrf BOARD=pca10040 - make ${MAKEOPTS} -C ports/nrf BOARD=microbit - - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 + - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s132 # bare-arm and minimal ports, with size-diff check - stage: test From c521c178e9d113a27c50f6e0265c75d87fd7c99c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Jun 2020 23:48:33 +1000 Subject: [PATCH 0089/3533] github: Add FUNDING.yml file pointing to micropython GitHub sponsorship. Signed-off-by: Damien George --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000000..15186fd5c1916 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: micropython From ce02d5e3486ca0665e175e9690be910a06392edf Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 10:48:24 +1000 Subject: [PATCH 0090/3533] stm32/boards/NUCLEO_WB55: Add more CPU pins and aliases to SW1/2/3. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_WB55/pins.csv | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ports/stm32/boards/NUCLEO_WB55/pins.csv b/ports/stm32/boards/NUCLEO_WB55/pins.csv index 49fdab0c28c58..d7e0babf36d25 100644 --- a/ports/stm32/boards/NUCLEO_WB55/pins.csv +++ b/ports/stm32/boards/NUCLEO_WB55/pins.csv @@ -6,6 +6,14 @@ ,PA5 ,PA6 ,PA7 +,PA8 +,PA9 +,PA10 +,PA11 +,PA12 +,PA13 +,PA14 +,PA15 ,PB0 ,PB1 ,PB2 @@ -26,7 +34,20 @@ ,PC1 ,PC2 ,PC3 +,PC4 +,PC5 +,PC6 +,PC10 +,PC11 +,PC12 +,PC13 +,PD0 +,PD1 +,PE4 SW,PC4 +SW1,PC4 +SW2,PD0 +SW3,PD1 LED_GREEN,PB0 LED_RED,PB1 LED_BLUE,PB5 From 026fda605e03113d6e753290d65fed774418bc53 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 22:20:20 +1000 Subject: [PATCH 0091/3533] tools/codeformat.py: Include extmod/{btstack,nimble} in code formatting. Signed-off-by: Damien George --- extmod/btstack/modbluetooth_btstack.c | 2 +- extmod/nimble/modbluetooth_nimble.c | 2 +- tools/codeformat.py | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 31b2825758434..206bc98f9ff71 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -35,7 +35,7 @@ #include "lib/btstack/src/btstack.h" -#define DEBUG_EVENT_printf(...) //printf(__VA_ARGS__) +#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__) #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK" diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 035ec3d26fc6d..4e0ca88efa468 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -45,7 +45,7 @@ #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" #endif -#define DEBUG_EVENT_printf(...) //printf(__VA_ARGS__) +#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__) #define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV diff --git a/tools/codeformat.py b/tools/codeformat.py index 364037d2fac86..653529460c079 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -36,6 +36,8 @@ PATHS = [ # C "extmod/*.[ch]", + "extmod/btstack/*.[ch]", + "extmod/nimble/*.[ch]", "lib/netutils/*.[ch]", "lib/timeutils/*.[ch]", "lib/utils/*.[ch]", From 77ed6f69ac35c1663a5633a8ee1d8a2446542204 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 16 Jun 2020 09:34:51 -0500 Subject: [PATCH 0092/3533] tools/uncrustify: Enable more opts to remove space between func and '('. With only `sp_func_proto_paren = remove` set there are some cases where uncrustify misses removing a space between the function name and the opening '('. This sets all of the related options to `force` as well. --- extmod/vfs_fat_diskio.c | 6 +++--- lib/utils/printf.c | 2 +- ports/cc3200/main.c | 14 +++++++------- ports/cc3200/mptask.c | 18 +++++++++--------- ports/cc3200/serverstask.c | 30 +++++++++++++++--------------- tools/uncrustify.cfg | 20 ++++++++++---------- 6 files changed, 45 insertions(+), 45 deletions(-) diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index ae1bef57d5e07..1bcd471f2162e 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -52,7 +52,7 @@ STATIC fs_user_mount_t *disk_get_device(void *bdev) { /* Read Sector(s) */ /*-----------------------------------------------------------------------*/ -DRESULT disk_read ( +DRESULT disk_read( bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address (LBA) */ @@ -72,7 +72,7 @@ DRESULT disk_read ( /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ -DRESULT disk_write ( +DRESULT disk_write( bdev_t pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ @@ -98,7 +98,7 @@ DRESULT disk_write ( /* Miscellaneous Functions */ /*-----------------------------------------------------------------------*/ -DRESULT disk_ioctl ( +DRESULT disk_ioctl( bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ diff --git a/lib/utils/printf.c b/lib/utils/printf.c index 6266ab66d3cdc..e8db2b999c123 100644 --- a/lib/utils/printf.c +++ b/lib/utils/printf.c @@ -104,7 +104,7 @@ STATIC void strn_print_strn(void *data, const char *str, size_t len) { // when linkings against it statically. // GCC 9 gives a warning about missing attributes so it's excluded until // uClibc+GCC9 support is needed. -int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias ("vsnprintf"))); +int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias("vsnprintf"))); #endif int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) { diff --git a/ports/cc3200/main.c b/ports/cc3200/main.c index b9f67194b6fc0..bdd73e7bf9ba7 100644 --- a/ports/cc3200/main.c +++ b/ports/cc3200/main.c @@ -51,8 +51,8 @@ ******************************************************************************/ // This is the static memory (TCB and stack) for the idle task -static StaticTask_t xIdleTaskTCB __attribute__ ((section (".rtos_heap"))); -static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8))); +static StaticTask_t xIdleTaskTCB __attribute__ ((section(".rtos_heap"))); +static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE] __attribute__ ((section(".rtos_heap"))) __attribute__((aligned(8))); /****************************************************************************** DECLARE PUBLIC DATA @@ -62,18 +62,18 @@ OsiTaskHandle mpTaskHandle; #endif // This is the FreeRTOS heap, defined here so we can put it in a special segment -uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8))); +uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ((section(".rtos_heap"))) __attribute__((aligned(8))); // This is the static memory (TCB and stack) for the main MicroPython task -StaticTask_t mpTaskTCB __attribute__ ((section (".rtos_heap"))); -StackType_t mpTaskStack[MICROPY_TASK_STACK_LEN] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8))); +StaticTask_t mpTaskTCB __attribute__ ((section(".rtos_heap"))); +StackType_t mpTaskStack[MICROPY_TASK_STACK_LEN] __attribute__ ((section(".rtos_heap"))) __attribute__((aligned(8))); /****************************************************************************** DEFINE PUBLIC FUNCTIONS ******************************************************************************/ -__attribute__ ((section (".boot"))) -int main (void) { +__attribute__ ((section(".boot"))) +int main(void) { // Initialize the clocks and the interrupt system HAL_SystemInit(); diff --git a/ports/cc3200/mptask.c b/ports/cc3200/mptask.c index f06a502776087..71b650ff858c8 100644 --- a/ports/cc3200/mptask.c +++ b/ports/cc3200/mptask.c @@ -113,7 +113,7 @@ static const char fresh_boot_py[] = "# boot.py -- run on boot-up\r\n" uintptr_t cortex_m3_get_sp(void); -void TASK_MicroPython (void *pvParameters) { +void TASK_MicroPython(void *pvParameters) { // get the top of the stack to initialize the garbage collector uint32_t sp = cortex_m3_get_sp(); @@ -262,16 +262,16 @@ void TASK_MicroPython (void *pvParameters) { /****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ -__attribute__ ((section (".boot"))) -STATIC void mptask_pre_init (void) { +__attribute__ ((section(".boot"))) +STATIC void mptask_pre_init(void) { // this one only makes sense after a poweron reset pyb_rtc_pre_init(); // Create the simple link spawn task - ASSERT (OSI_OK == VStartSimpleLinkSpawnTask(SIMPLELINK_SPAWN_TASK_PRIORITY)); + ASSERT(OSI_OK == VStartSimpleLinkSpawnTask(SIMPLELINK_SPAWN_TASK_PRIORITY)); // Allocate memory for the flash file system - ASSERT ((sflash_vfs_fat = mem_Malloc(sizeof(*sflash_vfs_fat))) != NULL); + ASSERT((sflash_vfs_fat = mem_Malloc(sizeof(*sflash_vfs_fat))) != NULL); // this one allocates memory for the nvic vault pyb_sleep_pre_init(); @@ -295,7 +295,7 @@ STATIC void mptask_pre_init (void) { ASSERT(svTaskHandle != NULL); } -STATIC void mptask_init_sflash_filesystem (void) { +STATIC void mptask_init_sflash_filesystem(void) { FILINFO fno; // Initialise the local flash filesystem. @@ -378,16 +378,16 @@ STATIC void mptask_init_sflash_filesystem (void) { } } -STATIC void mptask_enter_ap_mode (void) { +STATIC void mptask_enter_ap_mode(void) { // append the mac only if it's not the first boot bool add_mac = !PRCMGetSpecialBit(PRCM_FIRST_BOOT_BIT); // enable simplelink in ap mode (use the MAC address to make the ssid unique) - wlan_sl_init (ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID), + wlan_sl_init(ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID), MICROPY_PORT_WLAN_AP_SECURITY, MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY), MICROPY_PORT_WLAN_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, add_mac); } -STATIC void mptask_create_main_py (void) { +STATIC void mptask_create_main_py(void) { // create empty main.py FIL fp; f_open(&sflash_vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS); diff --git a/ports/cc3200/serverstask.c b/ports/cc3200/serverstask.c index 517a7a2281bdb..03eed8eeb0cf4 100644 --- a/ports/cc3200/serverstask.c +++ b/ports/cc3200/serverstask.c @@ -68,8 +68,8 @@ static volatile bool sleep_sockets = false; ******************************************************************************/ // This is the static memory (TCB and stack) for the servers task -StaticTask_t svTaskTCB __attribute__ ((section (".rtos_heap"))); -StackType_t svTaskStack[SERVERS_STACK_LEN] __attribute__ ((section (".rtos_heap"))) __attribute__((aligned (8))); +StaticTask_t svTaskTCB __attribute__ ((section(".rtos_heap"))); +StackType_t svTaskStack[SERVERS_STACK_LEN] __attribute__ ((section(".rtos_heap"))) __attribute__((aligned(8))); char servers_user[SERVERS_USER_PASS_LEN_MAX + 1]; char servers_pass[SERVERS_USER_PASS_LEN_MAX + 1]; @@ -77,12 +77,12 @@ char servers_pass[SERVERS_USER_PASS_LEN_MAX + 1]; /****************************************************************************** DECLARE PUBLIC FUNCTIONS ******************************************************************************/ -void TASK_Servers (void *pvParameters) { +void TASK_Servers(void *pvParameters) { bool cycle = false; - strcpy (servers_user, SERVERS_DEF_USER); - strcpy (servers_pass, SERVERS_DEF_PASS); + strcpy(servers_user, SERVERS_DEF_USER); + strcpy(servers_pass, SERVERS_DEF_PASS); telnet_init(); ftp_init(); @@ -143,12 +143,12 @@ void TASK_Servers (void *pvParameters) { } } -void servers_start (void) { +void servers_start(void) { servers_data.do_enable = true; mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3); } -void servers_stop (void) { +void servers_stop(void) { servers_data.do_disable = true; do { mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS); @@ -156,24 +156,24 @@ void servers_stop (void) { mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS * 3); } -void servers_reset (void) { +void servers_reset(void) { servers_data.do_reset = true; } -void servers_wlan_cycle_power (void) { +void servers_wlan_cycle_power(void) { servers_data.do_wlan_cycle_power = true; } -bool servers_are_enabled (void) { +bool servers_are_enabled(void) { return servers_data.enabled; } -void server_sleep_sockets (void) { +void server_sleep_sockets(void) { sleep_sockets = true; mp_hal_delay_ms(SERVERS_CYCLE_TIME_MS + 1); } -void servers_close_socket (int16_t *sd) { +void servers_close_socket(int16_t *sd) { if (*sd > 0) { modusocket_socket_delete(*sd); sl_Close(*sd); @@ -181,7 +181,7 @@ void servers_close_socket (int16_t *sd) { } } -void servers_set_login (char *user, char *pass) { +void servers_set_login(char *user, char *pass) { if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) { mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value")); } @@ -189,7 +189,7 @@ void servers_set_login (char *user, char *pass) { memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX); } -void servers_set_timeout (uint32_t timeout) { +void servers_set_timeout(uint32_t timeout) { if (timeout < SERVERS_MIN_TIMEOUT_MS) { // timeout is too low mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value")); @@ -197,7 +197,7 @@ void servers_set_timeout (uint32_t timeout) { servers_data.timeout = timeout; } -uint32_t servers_get_timeout (void) { +uint32_t servers_get_timeout(void) { return servers_data.timeout; } diff --git a/tools/uncrustify.cfg b/tools/uncrustify.cfg index 96b471742cc51..80542b903e79e 100644 --- a/tools/uncrustify.cfg +++ b/tools/uncrustify.cfg @@ -512,35 +512,35 @@ sp_func_proto_paren = remove # ignore/add/remove/force # Add or remove space between function name and '()' on function declaration # without parameters. -sp_func_proto_paren_empty = ignore # ignore/add/remove/force +sp_func_proto_paren_empty = remove # ignore/add/remove/force # Add or remove space between function name and '(' with a typedef specifier. -sp_func_type_paren = ignore # ignore/add/remove/force +sp_func_type_paren = remove # ignore/add/remove/force # Add or remove space between alias name and '(' of a non-pointer function type typedef. -sp_func_def_paren = ignore # ignore/add/remove/force +sp_func_def_paren = remove # ignore/add/remove/force # Add or remove space between function name and '()' on function definition # without parameters. -sp_func_def_paren_empty = ignore # ignore/add/remove/force +sp_func_def_paren_empty = remove # ignore/add/remove/force # Add or remove space inside empty function '()'. # Overrides sp_after_angle unless use_sp_after_angle_always is set to true. -sp_inside_fparens = ignore # ignore/add/remove/force +sp_inside_fparens = remove # ignore/add/remove/force # Add or remove space inside function '(' and ')'. sp_inside_fparen = remove # ignore/add/remove/force # Add or remove space inside the first parentheses in a function type, as in # 'void (*x)(...)'. -sp_inside_tparen = ignore # ignore/add/remove/force +sp_inside_tparen = remove # ignore/add/remove/force # Add or remove space between the ')' and '(' in a function type, as in # 'void (*x)(...)'. -sp_after_tparen_close = ignore # ignore/add/remove/force +sp_after_tparen_close = remove # ignore/add/remove/force # Add or remove space between ']' and '(' when part of a function call. -sp_square_fparen = ignore # ignore/add/remove/force +sp_square_fparen = remove # ignore/add/remove/force # Add or remove space between ')' and '{' of function. sp_fparen_brace = force # ignore/add/remove/force @@ -555,11 +555,11 @@ sp_fparen_brace_initializer = ignore # ignore/add/remove/force sp_fparen_dbrace = ignore # ignore/add/remove/force # Add or remove space between function name and '(' on function calls. -sp_func_call_paren = ignore # ignore/add/remove/force +sp_func_call_paren = remove # ignore/add/remove/force # Add or remove space between function name and '()' on function calls without # parameters. If set to ignore (the default), sp_func_call_paren is used. -sp_func_call_paren_empty = ignore # ignore/add/remove/force +sp_func_call_paren_empty = remove # ignore/add/remove/force # Add or remove space between the user function name and '(' on function # calls. You need to set a keyword to be a user function in the config file, From 3a9d948032e27f690e1fb09084c36bd47b1a75a0 Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Thu, 18 Jun 2020 12:44:54 +0100 Subject: [PATCH 0093/3533] esp32/esp32_rmt: Call rmt_driver_install before rmt_config. Otherwise the RMT will repeat pulses when using loop(True). This repeating is due to a bug in the IDF which will be fixed in an upcoming release, but for now the accepted workaround is to swap these calls, which should still work in the fixed version of the IDF. Fixes issue #6167. --- ports/esp32/esp32_rmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 846c80d478f0b..2ed4c9f6968c4 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -110,8 +110,8 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz config.clk_div = self->clock_div; - check_esp_err(rmt_config(&config)); check_esp_err(rmt_driver_install(config.channel, 0, 0)); + check_esp_err(rmt_config(&config)); return MP_OBJ_FROM_PTR(self); } From 6164c7e666fdd0aba232191768a66ab4434a7222 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 10:58:51 +1000 Subject: [PATCH 0094/3533] py/misc.h: Add missing semi-colon in mp_float_union_t for big-endian. Fixes issue #6161. Signed-off-by: Damien George --- py/misc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/misc.h b/py/misc.h index c7060ddf9d223..2dd20365c934c 100644 --- a/py/misc.h +++ b/py/misc.h @@ -247,7 +247,7 @@ typedef union _mp_float_union_t { } p; #else struct { - mp_float_uint_t sgn : 1 + mp_float_uint_t sgn : 1; mp_float_uint_t exp : MP_FLOAT_EXP_BITS; mp_float_uint_t frc : MP_FLOAT_FRAC_BITS; } p; From f1ba2c9d88078df8758ee9072b02d48e70c0b305 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 10:59:29 +1000 Subject: [PATCH 0095/3533] qemu-arm/Makefile: Add CFLAGS_EXTRA to CFLAGS. Signed-off-by: Damien George --- ports/qemu-arm/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/qemu-arm/Makefile b/ports/qemu-arm/Makefile index e7b6523f88c2f..aebc5afaeb773 100644 --- a/ports/qemu-arm/Makefile +++ b/ports/qemu-arm/Makefile @@ -42,6 +42,7 @@ INC += -I$(BUILD) CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 $(COPT) \ -ffunction-sections -fdata-sections +CFLAGS += $(CFLAGS_EXTRA) # Debugging/Optimization ifeq ($(DEBUG), 1) From ac15be9365808919dc18c6fbfee55e074671c2aa Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 10:59:53 +1000 Subject: [PATCH 0096/3533] travis: Build qemu-arm with MP_ENDIANNESS_BIG=1 to test bigendian build. Eventually it would be good to run the full test suite on a big-endian system, but for now this will help to catch build errors with the big-endian configuration. Signed-off-by: Damien George --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1cb5575c73636..09da9598e2018 100644 --- a/.travis.yml +++ b/.travis.yml @@ -90,6 +90,8 @@ jobs: - qemu-system-arm --version script: - make ${MAKEOPTS} -C mpy-cross + - make ${MAKEOPTS} -C ports/qemu-arm CFLAGS_EXTRA=-DMP_ENDIANNESS_BIG=1 + - make ${MAKEOPTS} -C ports/qemu-arm clean - make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test after_failure: - grep --text "FAIL" ports/qemu-arm/build/console.out From 5f3c2f1fa8e7a41ff85aaaea98bfb3d98459e136 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 12:19:04 +1000 Subject: [PATCH 0097/3533] stm32/irq: Clean up irq.h so it does not depend on core uPy defines. The irq.h file now just provides low-level IRQ definitions and priorities. All Python binding definitions are moved to modmachine.h, with some renaming of pyb -> machine, and also the machine_idle definition (was pyb_wfi) is moved to modmachine.c. The cc3200 and teensy ports are updated to build with these changes. Signed-off-by: Damien George --- ports/cc3200/mods/modmachine.c | 8 ++++--- ports/stm32/irq.c | 38 ++++++++++------------------------ ports/stm32/irq.h | 7 +------ ports/stm32/modmachine.c | 15 +++++++++++--- ports/stm32/modmachine.h | 7 +++++++ ports/stm32/modpyb.c | 6 +++--- ports/teensy/modpyb.c | 12 ++++++++--- 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/ports/cc3200/mods/modmachine.c b/ports/cc3200/mods/modmachine.c index f70b399c07ebd..ddecd47f2b013 100644 --- a/ports/cc3200/mods/modmachine.c +++ b/ports/cc3200/mods/modmachine.c @@ -29,7 +29,6 @@ #include "py/runtime.h" #include "py/mphal.h" -#include "irq.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" @@ -69,6 +68,9 @@ extern OsiTaskHandle xSimpleLinkSpawnTaskHndl; /// \module machine - functions related to the SoC /// +MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj); + /******************************************************************************/ // MicroPython bindings; @@ -176,8 +178,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) }, diff --git a/ports/stm32/irq.c b/ports/stm32/irq.c index 0100899733098..fdaf2385cc9d1 100644 --- a/ports/stm32/irq.c +++ b/ports/stm32/irq.c @@ -27,44 +27,28 @@ #include "py/obj.h" #include "py/mphal.h" #include "irq.h" - -/// \moduleref pyb +#include "modmachine.h" #if IRQ_ENABLE_STATS uint32_t irq_stats[IRQ_STATS_MAX] = {0}; #endif -/// \function wfi() -/// Wait for an interrupt. -/// This executies a `wfi` instruction which reduces power consumption -/// of the MCU until an interrupt occurs, at which point execution continues. -STATIC mp_obj_t pyb_wfi(void) { - __WFI(); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi); - -/// \function disable_irq() -/// Disable interrupt requests. -/// Returns the previous IRQ state: `False`/`True` for disabled/enabled IRQs -/// respectively. This return value can be passed to enable_irq to restore -/// the IRQ to its original state. -STATIC mp_obj_t pyb_disable_irq(void) { +// disable_irq() +// Disable interrupt requests. +// Returns the previous IRQ state which can be passed to enable_irq. +STATIC mp_obj_t machine_disable_irq(void) { return mp_obj_new_bool(disable_irq() == IRQ_STATE_ENABLED); } -MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq); +MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq); -/// \function enable_irq(state=True) -/// Enable interrupt requests. -/// If `state` is `True` (the default value) then IRQs are enabled. -/// If `state` is `False` then IRQs are disabled. The most common use of -/// this function is to pass it the value returned by `disable_irq` to -/// exit a critical section. -STATIC mp_obj_t pyb_enable_irq(uint n_args, const mp_obj_t *arg) { +// enable_irq(state=True) +// Enable interrupt requests, based on the argument, which is usually the +// value returned by a previous call to disable_irq. +STATIC mp_obj_t machine_enable_irq(uint n_args, const mp_obj_t *arg) { enable_irq((n_args == 0 || mp_obj_is_true(arg[0])) ? IRQ_STATE_ENABLED : IRQ_STATE_DISABLED); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj, 0, 1, pyb_enable_irq); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj, 0, 1, machine_enable_irq); #if IRQ_ENABLE_STATS // return a memoryview of the irq statistics array diff --git a/ports/stm32/irq.h b/ports/stm32/irq.h index 4b1251666c202..6c10f5e1b07f5 100644 --- a/ports/stm32/irq.h +++ b/ports/stm32/irq.h @@ -52,7 +52,7 @@ extern uint32_t irq_stats[IRQ_STATS_MAX]; #define IRQ_EXIT(irq) #endif -static inline mp_uint_t query_irq(void) { +static inline uint32_t query_irq(void) { return __get_PRIMASK(); } @@ -92,11 +92,6 @@ static inline void restore_irq_pri(uint32_t state) { #endif -MP_DECLARE_CONST_FUN_OBJ_0(pyb_wfi_obj); -MP_DECLARE_CONST_FUN_OBJ_0(pyb_disable_irq_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj); -MP_DECLARE_CONST_FUN_OBJ_0(pyb_irq_stats_obj); - // IRQ priority definitions. // // Lower number implies higher interrupt priority. diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 6fd2488a5ae7b..ac4d8712397e4 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -346,6 +346,15 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq); +// idle() +// This executies a wfi machine instruction which reduces power consumption +// of the MCU until an interrupt occurs, at which point execution continues. +STATIC mp_obj_t machine_idle(void) { + __WFI(); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); + STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_obj_t args2[2] = {MP_OBJ_NULL, args[0]}; @@ -382,7 +391,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if MICROPY_HW_ENABLE_RNG { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) }, #endif - { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&pyb_wfi_obj) }, + { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) }, { MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, @@ -391,8 +400,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) }, #endif - { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index f04bfb486bb81..e4ccf638855e8 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -39,7 +39,14 @@ MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj); MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj); + +MP_DECLARE_CONST_FUN_OBJ_0(machine_idle_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj); +MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj); + +MP_DECLARE_CONST_FUN_OBJ_0(pyb_irq_stats_obj); + #endif // MICROPY_INCLUDED_STM32_MODMACHINE_H diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 321fb62e9a162..c92090699108e 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -146,9 +146,9 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_repl_info), MP_ROM_PTR(&pyb_set_repl_info_obj) }, #endif - { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&machine_idle_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, #if IRQ_ENABLE_STATS { MP_ROM_QSTR(MP_QSTR_irq_stats), MP_ROM_PTR(&pyb_irq_stats_obj) }, #endif diff --git a/ports/teensy/modpyb.c b/ports/teensy/modpyb.c index 26e3f4c43a0dc..f4384a885426e 100644 --- a/ports/teensy/modpyb.c +++ b/ports/teensy/modpyb.c @@ -36,7 +36,6 @@ #include "lib/utils/pyexec.h" #include "gccollect.h" -#include "irq.h" #include "systick.h" #include "led.h" #include "pin.h" @@ -53,6 +52,7 @@ #include "dac.h" #include "usb.h" #include "portmodules.h" +#include "modmachine.h" /// \module pyb - functions related to the pyboard /// @@ -230,6 +230,12 @@ STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay); +STATIC mp_obj_t pyb_wfi(void) { + __WFI(); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi); + STATIC mp_obj_t pyb_stop(void) { printf("stop not currently implemented\n"); return mp_const_none; @@ -285,8 +291,8 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&pyb_stop_obj) }, { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&pyb_standby_obj) }, From ce326699d7bb4e618600475c856f2ee1fe333442 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 13:33:31 +1000 Subject: [PATCH 0098/3533] stm32/powerctrlboot: Include irq.h to get definitions of IRQ priorities. irq.h is included by py/mphal.h but it's better to be explicit, eg if mboot uses powerctrlboot.c. Signed-off-by: Damien George --- ports/stm32/powerctrlboot.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/stm32/powerctrlboot.c b/ports/stm32/powerctrlboot.c index 749cb5505caaa..206b19b75067a 100644 --- a/ports/stm32/powerctrlboot.c +++ b/ports/stm32/powerctrlboot.c @@ -25,6 +25,7 @@ */ #include "py/mphal.h" +#include "irq.h" #include "powerctrl.h" static inline void powerctrl_config_systick(void) { From afd47d58ac25b292bb7ff5ba3b1c53dbf3750d81 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 13:35:53 +1000 Subject: [PATCH 0099/3533] stm32/flash: Make flash C-API reusable, and funcs return an error code. This commit makes the low-level flash C functions usable by code other than flashbdev.c (eg by mboot). Changes in this commit are: - flash_erase() and flash_write() now return an errno error code, a negative value on error. - flash_erase() now automatically locks the flash, as well as unlocking it. - flash_write() now automatically unlocks the flash, as well as locking it. - flashbdev.c is modified for the above changes. Signed-off-by: Damien George --- ports/stm32/flash.c | 68 +++++++++++++++++++++-------------------- ports/stm32/flash.h | 4 +-- ports/stm32/mphalport.h | 4 +++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/ports/stm32/flash.c b/ports/stm32/flash.c index 99c95f7d993ed..7c7d2f7b6e0dd 100644 --- a/ports/stm32/flash.c +++ b/ports/stm32/flash.c @@ -26,6 +26,7 @@ #include "py/mpconfig.h" #include "py/misc.h" +#include "py/mphal.h" #include "flash.h" typedef struct { @@ -162,17 +163,18 @@ uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *si return 0; } -void flash_erase(uint32_t flash_dest, uint32_t num_word32) { +int flash_erase(uint32_t flash_dest, uint32_t num_word32) { // check there is something to write if (num_word32 == 0) { - return; + return 0; } - // unlock + // Unlock the flash for erase. HAL_FLASH_Unlock(); - FLASH_EraseInitTypeDef EraseInitStruct; + // Clear pending flags (if any) and set up EraseInitStruct. + FLASH_EraseInitTypeDef EraseInitStruct; #if defined(STM32F0) __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; @@ -190,17 +192,14 @@ void flash_erase(uint32_t flash_dest, uint32_t num_word32) { EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE; #elif defined(STM32L4) __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); - - // erase the sector(s) // The sector returned by flash_get_sector_info can not be used // as the flash has on each bank 0/1 pages 0..255 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.Banks = get_bank(flash_dest); EraseInitStruct.Page = get_page(flash_dest); EraseInitStruct.NbPages = get_page(flash_dest + 4 * num_word32 - 1) - EraseInitStruct.Page + 1; - ; #else - // Clear pending flags (if any) + #if defined(STM32H7) __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2); #else @@ -208,7 +207,6 @@ void flash_erase(uint32_t flash_dest, uint32_t num_word32) { FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); #endif - // erase the sector(s) EraseInitStruct.TypeErase = TYPEERASE_SECTORS; EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V #if defined(STM32H7) @@ -216,14 +214,17 @@ void flash_erase(uint32_t flash_dest, uint32_t num_word32) { #endif EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL); EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1; + #endif + // Erase the sectors. uint32_t SectorError = 0; - if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { - // error occurred during sector erase - HAL_FLASH_Lock(); // lock the flash - return; - } + HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError); + + // Lock the flash after erase. + HAL_FLASH_Lock(); + + return mp_hal_status_to_neg_errno(status); } /* @@ -255,16 +256,21 @@ void flash_erase_it(uint32_t flash_dest, uint32_t num_word32) { } */ -void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { +int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { + // Unlock the flash for write. + HAL_FLASH_Unlock(); + + HAL_StatusTypeDef status = HAL_OK; + #if defined(STM32L4) || defined(STM32WB) // program the flash uint64 by uint64 for (int i = 0; i < num_word32 / 2; i++) { uint64_t val = *(uint64_t *)src; - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) { - // error occurred during flash write - HAL_FLASH_Lock(); // lock the flash - return; + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val); + if (status != HAL_OK) { + num_word32 = 0; // don't write any odd word after this loop + break; } flash_dest += 8; src += 2; @@ -272,21 +278,16 @@ void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) if ((num_word32 & 0x01) == 1) { uint64_t val = *(uint64_t *)flash_dest; val = (val & 0xffffffff00000000uL) | (*src); - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) { - // error occurred during flash write - HAL_FLASH_Lock(); // lock the flash - return; - } + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val); } #elif defined(STM32H7) // program the flash 256 bits at a time for (int i = 0; i < num_word32 / 8; i++) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_dest, (uint64_t)(uint32_t)src) != HAL_OK) { - // error occurred during flash write - HAL_FLASH_Lock(); // lock the flash - return; + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_dest, (uint64_t)(uint32_t)src); + if (status != HAL_OK) { + break; } flash_dest += 32; src += 8; @@ -296,10 +297,9 @@ void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) // program the flash word by word for (int i = 0; i < num_word32; i++) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) { - // error occurred during flash write - HAL_FLASH_Lock(); // lock the flash - return; + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_dest, *src); + if (status != HAL_OK) { + break; } flash_dest += 4; src += 1; @@ -307,8 +307,10 @@ void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) #endif - // lock the flash + // Lock the flash after write. HAL_FLASH_Lock(); + + return mp_hal_status_to_neg_errno(status); } /* diff --git a/ports/stm32/flash.h b/ports/stm32/flash.h index b9edf610610cb..048914e501e22 100644 --- a/ports/stm32/flash.h +++ b/ports/stm32/flash.h @@ -27,7 +27,7 @@ #define MICROPY_INCLUDED_STM32_FLASH_H uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size); -void flash_erase(uint32_t flash_dest, uint32_t num_word32); -void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32); +int flash_erase(uint32_t flash_dest, uint32_t num_word32); +int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32); #endif // MICROPY_INCLUDED_STM32_FLASH_H diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index fe9378a1c7252..a76945db5fea7 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -4,6 +4,10 @@ extern const unsigned char mp_hal_status_to_errno_table[4]; +static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) { + return -mp_hal_status_to_errno_table[status]; +} + NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable From 4c8a68df6f25ed98d097b0defd3180739c813654 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 13:40:30 +1000 Subject: [PATCH 0100/3533] stm32/i2cslave: Add support for WB MCUs. Signed-off-by: Damien George --- ports/stm32/i2cslave.c | 2 +- ports/stm32/i2cslave.h | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ports/stm32/i2cslave.c b/ports/stm32/i2cslave.c index cacc00e0c4141..149e105ee8207 100644 --- a/ports/stm32/i2cslave.c +++ b/ports/stm32/i2cslave.c @@ -60,7 +60,7 @@ void i2c_slave_ev_irq_handler(i2c_slave_t *i2c) { } } -#elif defined(STM32F7) || defined(STM32H7) +#elif defined(STM32F7) || defined(STM32H7) || defined(STM32WB) void i2c_slave_init_helper(i2c_slave_t *i2c, int addr) { i2c->CR1 = I2C_CR1_STOPIE | I2C_CR1_ADDRIE | I2C_CR1_RXIE | I2C_CR1_TXIE; diff --git a/ports/stm32/i2cslave.h b/ports/stm32/i2cslave.h index 55882acd8eb2e..f335c9291226c 100644 --- a/ports/stm32/i2cslave.h +++ b/ports/stm32/i2cslave.h @@ -28,6 +28,11 @@ #include STM32_HAL_H +#if !defined(I2C2_BASE) +// This MCU doesn't have I2C2_BASE, define it so that the i2c_idx calculation works. +#define I2C2_BASE (I2C1_BASE + ((I2C3_BASE - I2C1_BASE) / 2)) +#endif + typedef I2C_TypeDef i2c_slave_t; void i2c_slave_init_helper(i2c_slave_t *i2c, int addr); @@ -42,6 +47,10 @@ static inline void i2c_slave_init(i2c_slave_t *i2c, int irqn, int irq_pri, int a RCC->APB1LENR |= 1 << (RCC_APB1LENR_I2C1EN_Pos + i2c_idx); volatile uint32_t tmp = RCC->APB1LENR; // Delay after enabling clock (void)tmp; + #elif defined(STM32WB) + RCC->APB1ENR1 |= 1 << (RCC_APB1ENR1_I2C1EN_Pos + i2c_idx); + volatile uint32_t tmp = RCC->APB1ENR1; // Delay after enabling clock + (void)tmp; #endif i2c_slave_init_helper(i2c, addr); From 736daebfc8c7dce2fab10ee311519034d702c8b6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 15:12:25 +1000 Subject: [PATCH 0101/3533] stm32/flash: Add flash_is_valid_addr, and extend sectors for 2MB F7. Signed-off-by: Damien George --- ports/stm32/flash.c | 12 ++++++++++++ ports/stm32/flash.h | 1 + 2 files changed, 13 insertions(+) diff --git a/ports/stm32/flash.c b/ports/stm32/flash.c index 7c7d2f7b6e0dd..5f96696a9ffd7 100644 --- a/ports/stm32/flash.c +++ b/ports/stm32/flash.c @@ -70,10 +70,15 @@ static const flash_layout_t flash_layout[] = { { 0x08020000, 0x20000, 3 }, }; #else +// This is for dual-bank mode disabled static const flash_layout_t flash_layout[] = { { 0x08000000, 0x08000, 4 }, { 0x08020000, 0x20000, 1 }, + #if FLASH_SECTOR_TOTAL == 8 { 0x08040000, 0x40000, 3 }, + #else + { 0x08040000, 0x40000, 7 }, + #endif }; #endif @@ -139,6 +144,13 @@ static uint32_t get_page(uint32_t addr) { #endif +bool flash_is_valid_addr(uint32_t addr) { + uint8_t last = MP_ARRAY_SIZE(flash_layout) - 1; + uint32_t end_of_flash = flash_layout[last].base_address + + flash_layout[last].sector_count * flash_layout[last].sector_size; + return flash_layout[0].base_address <= addr && addr < end_of_flash; +} + uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) { if (addr >= flash_layout[0].base_address) { uint32_t sector_index = 0; diff --git a/ports/stm32/flash.h b/ports/stm32/flash.h index 048914e501e22..12cdaca55aab9 100644 --- a/ports/stm32/flash.h +++ b/ports/stm32/flash.h @@ -26,6 +26,7 @@ #ifndef MICROPY_INCLUDED_STM32_FLASH_H #define MICROPY_INCLUDED_STM32_FLASH_H +bool flash_is_valid_addr(uint32_t addr); uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size); int flash_erase(uint32_t flash_dest, uint32_t num_word32); int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32); From a8778c8dc8849dfba14fb17ba148e4d0f78f0467 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 17:27:52 +1000 Subject: [PATCH 0102/3533] stm32/mboot: Use flash routines from main stm32 code rather than custom. The flash functions in ports/stm32/flash.c are almost identical to those in ports/stm32/mboot/main.c, so remove the duplicated code in mboot and use instead the main stm32 code. This also allows supporting other MCU series. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 3 +- ports/stm32/mboot/main.c | 162 ++++------------------------------ ports/stm32/mboot/mphalport.h | 5 ++ 3 files changed, 25 insertions(+), 145 deletions(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 43aae2a6785f3..a5db1666dd313 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -99,9 +99,10 @@ SRC_C = \ drivers/bus/softspi.c \ drivers/bus/softqspi.c \ drivers/memory/spiflash.c \ + ports/stm32/flash.c \ + ports/stm32/flashbdev.c \ ports/stm32/i2cslave.c \ ports/stm32/qspi.c \ - ports/stm32/flashbdev.c \ ports/stm32/spibdev.c \ ports/stm32/usbd_conf.c \ $(wildcard $(BOARD_DIR)/*.c) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 4ae575f3ddf41..c53920ccf0244 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -31,6 +31,7 @@ #include "extmod/crypto-algorithms/sha256.c" #include "usbd_core.h" #include "storage.h" +#include "flash.h" #include "i2cslave.h" #include "mboot.h" #include "dfu.h" @@ -500,115 +501,26 @@ static int usrbtn_state(void) { #define MBOOT_SPIFLASH2_LAYOUT "" #endif -typedef struct { - uint32_t base_address; - uint32_t sector_size; - uint32_t sector_count; -} flash_layout_t; - -#if defined(STM32F7) -// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to -// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F7 -#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR -#endif - #if defined(STM32F4) \ || defined(STM32F722xx) \ || defined(STM32F723xx) \ || defined(STM32F732xx) \ || defined(STM32F733xx) - #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT - -static const flash_layout_t flash_layout[] = { - { 0x08000000, 0x04000, 4 }, - { 0x08010000, 0x10000, 1 }, - { 0x08020000, 0x20000, 3 }, - #if defined(FLASH_SECTOR_8) - { 0x08080000, 0x20000, 4 }, - #endif - #if defined(FLASH_SECTOR_12) - { 0x08100000, 0x04000, 4 }, - { 0x08110000, 0x10000, 1 }, - { 0x08120000, 0x20000, 7 }, - #endif -}; - #elif defined(STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) - #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/04*032Kg,01*128Kg,07*256Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT - -// This is for dual-bank mode disabled -static const flash_layout_t flash_layout[] = { - { 0x08000000, 0x08000, 4 }, - { 0x08020000, 0x20000, 1 }, - { 0x08040000, 0x40000, 7 }, -}; - #elif defined(STM32H743xx) - #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/16*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT - -static const flash_layout_t flash_layout[] = { - { 0x08000000, 0x20000, 16 }, -}; - -#endif - -static inline bool flash_is_valid_addr(uint32_t addr) { - uint8_t last = MP_ARRAY_SIZE(flash_layout) - 1; - uint32_t end_of_flash = flash_layout[last].base_address + - flash_layout[last].sector_count * flash_layout[last].sector_size; - return flash_layout[0].base_address <= addr && addr < end_of_flash; -} - -static uint32_t flash_get_sector_index(uint32_t addr, uint32_t *sector_size) { - if (addr >= flash_layout[0].base_address) { - uint32_t sector_index = 0; - for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) { - for (int j = 0; j < flash_layout[i].sector_count; ++j) { - uint32_t sector_start_next = flash_layout[i].base_address - + (j + 1) * flash_layout[i].sector_size; - if (addr < sector_start_next) { - *sector_size = flash_layout[i].sector_size; - return sector_index; - } - ++sector_index; - } - } - } - return 0; -} - -#if defined(STM32H7) -// get the bank of a given flash address -static uint32_t get_bank(uint32_t addr) { - if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_SWAP_BANK) == 0) { - // no bank swap - if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) { - return FLASH_BANK_1; - } else { - return FLASH_BANK_2; - } - } else { - // bank swap - if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) { - return FLASH_BANK_2; - } else { - return FLASH_BANK_1; - } - } -} #endif -static int flash_mass_erase(void) { +static int mboot_flash_mass_erase(void) { // TODO return -1; } -static int flash_page_erase(uint32_t addr, uint32_t *next_addr) { +static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) { uint32_t sector_size = 0; - uint32_t sector = flash_get_sector_index(addr, §or_size); + uint32_t sector = flash_get_sector_info(addr, NULL, §or_size); if (sector == 0) { // Don't allow to erase the sector with this bootloader in it dfu_context.status = DFU_STATUS_ERROR_ADDRESS; @@ -618,30 +530,10 @@ static int flash_page_erase(uint32_t addr, uint32_t *next_addr) { *next_addr = addr + sector_size; - HAL_FLASH_Unlock(); - - // Clear pending flags (if any) - #if defined(STM32H7) - __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2); - #else - __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | - FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); - #endif - - // erase the sector(s) - FLASH_EraseInitTypeDef EraseInitStruct; - EraseInitStruct.TypeErase = TYPEERASE_SECTORS; - EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V - #if defined(STM32H7) - EraseInitStruct.Banks = get_bank(addr); - #endif - EraseInitStruct.Sector = sector; - EraseInitStruct.NbSectors = 1; - - uint32_t SectorError = 0; - if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { - // error occurred during sector erase - return -1; + // Erase the flash page. + int ret = flash_erase(addr, sector_size / sizeof(uint32_t)); + if (ret != 0) { + return ret; } // Check the erase set bits to 1, at least for the first 256 bytes @@ -654,8 +546,9 @@ static int flash_page_erase(uint32_t addr, uint32_t *next_addr) { return 0; } -static int flash_write(uint32_t addr, const uint8_t *src8, size_t len) { - if (addr >= flash_layout[0].base_address && addr < flash_layout[0].base_address + flash_layout[0].sector_size) { +static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) { + uint32_t sector = flash_get_sector_info(addr, NULL, NULL); + if (sector == 0) { // Don't allow to write the sector with this bootloader in it dfu_context.status = DFU_STATUS_ERROR_ADDRESS; dfu_context.error = MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX; @@ -664,32 +557,13 @@ static int flash_write(uint32_t addr, const uint8_t *src8, size_t len) { const uint32_t *src = (const uint32_t*)src8; size_t num_word32 = (len + 3) / 4; - HAL_FLASH_Unlock(); - #if defined(STM32H7) - - // program the flash 256 bits at a time - for (int i = 0; i < num_word32 / 8; ++i) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, addr, (uint64_t)(uint32_t)src) != HAL_OK) { - return - 1; - } - addr += 32; - src += 8; + // Write the data to flash. + int ret = flash_write(addr, src, num_word32); + if (ret != 0) { + return ret; } - #else - - // program the flash word by word - for (size_t i = 0; i < num_word32; i++) { - if (HAL_FLASH_Program(TYPEPROGRAM_WORD, addr, *src) != HAL_OK) { - return -1; - } - addr += 4; - src += 1; - } - - #endif - // TODO verify data return 0; @@ -700,7 +574,7 @@ static int flash_write(uint32_t addr, const uint8_t *src8, size_t len) { static int do_mass_erase(void) { // TODO - return flash_mass_erase(); + return mboot_flash_mass_erase(); } #if defined(MBOOT_SPIFLASH_ADDR) || defined(MBOOT_SPIFLASH2_ADDR) @@ -735,7 +609,7 @@ int do_page_erase(uint32_t addr, uint32_t *next_addr) { } else #endif { - ret = flash_page_erase(addr, next_addr); + ret = mboot_flash_page_erase(addr, next_addr); } led0_state((ret == 0) ? LED0_STATE_SLOW_FLASH : LED0_STATE_SLOW_INVERTED_FLASH); @@ -775,7 +649,7 @@ int do_write(uint32_t addr, const uint8_t *src8, size_t len) { } else #endif if (flash_is_valid_addr(addr)) { - ret = flash_write(addr, src8, len); + ret = mboot_flash_write(addr, src8, len); } else { dfu_context.status = DFU_STATUS_ERROR_ADDRESS; dfu_context.error = MBOOT_ERROR_STR_INVALID_ADDRESS_IDX; diff --git a/ports/stm32/mboot/mphalport.h b/ports/stm32/mboot/mphalport.h index 0c8cb91a6883a..56d18a9b05d1d 100644 --- a/ports/stm32/mboot/mphalport.h +++ b/ports/stm32/mboot/mphalport.h @@ -28,6 +28,11 @@ #include "genhdr/pins.h" +// For simplicity just convert all HAL errors to one errno. +static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) { + return status == HAL_OK ? 0 : -1; +} + #define mp_hal_delay_us_fast(us) mp_hal_delay_us(us) #define MP_HAL_PIN_MODE_INPUT (0) From 8675858465f3d83aab709170eab6dc141570acf4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 18:18:30 +1000 Subject: [PATCH 0103/3533] stm32/mboot: Use CMSIS system source code for SystemInit function. There's no need to duplicate this functionality in mboot, the code provided in stm32lib/CMSIS does the same thing and makes it easier to support other MCU series. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 2 + ports/stm32/mboot/main.c | 87 +++++++------------------------------- 2 files changed, 18 insertions(+), 71 deletions(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index a5db1666dd313..a504c2314d3f9 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -34,6 +34,7 @@ STFLASH ?= st-flash OPENOCD ?= openocd OPENOCD_CONFIG ?= boards/openocd_stm32f4.cfg STARTUP_FILE ?= lib/stm32lib/CMSIS/STM32$(MCU_SERIES_UPPER)xx/Source/Templates/gcc/startup_$(CMSIS_MCU_LOWER).o +SYSTEM_FILE ?= lib/stm32lib/CMSIS/STM32$(MCU_SERIES_UPPER)xx/Source/Templates/system_stm32$(MCU_SERIES)xx.o CROSS_COMPILE ?= arm-none-eabi- @@ -109,6 +110,7 @@ SRC_C = \ SRC_O = \ $(STARTUP_FILE) \ + $(SYSTEM_FILE) \ ports/stm32/resethandler.o \ $(BUILD)/$(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_ll_usb.o: CFLAGS += -Wno-attributes diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index c53920ccf0244..c8cd7ba60e673 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -140,72 +140,6 @@ static void __fatal_error(const char *msg) { /******************************************************************************/ // CLOCK -#if defined(STM32F4) || defined(STM32F7) - -#define CONFIG_RCC_CR_1ST (RCC_CR_HSION) -#define CONFIG_RCC_CR_2ND (RCC_CR_HSEON || RCC_CR_CSSON || RCC_CR_PLLON) -#define CONFIG_RCC_PLLCFGR (0x24003010) - -#elif defined(STM32H7) - -#define CONFIG_RCC_CR_1ST (RCC_CR_HSION) -#define CONFIG_RCC_CR_2ND (RCC_CR_PLL3ON | RCC_CR_PLL2ON | RCC_CR_PLL1ON | RCC_CR_CSSHSEON \ - | RCC_CR_HSEON | RCC_CR_HSI48ON | RCC_CR_CSIKERON | RCC_CR_CSION) -#define CONFIG_RCC_PLLCFGR (0x00000000) - -#else -#error Unknown processor -#endif - -void SystemInit(void) { - #if defined(STM32H7) - // Configure write-once power options, and wait for voltage levels to be ready - PWR->CR3 = PWR_CR3_LDOEN; - while (!(PWR->CSR1 & PWR_CSR1_ACTVOSRDY)) { - } - #endif - - // Set HSION bit - RCC->CR |= CONFIG_RCC_CR_1ST; - - // Reset CFGR register - RCC->CFGR = 0x00000000; - - // Reset HSEON, CSSON and PLLON bits - RCC->CR &= ~CONFIG_RCC_CR_2ND; - - // Reset PLLCFGR register - RCC->PLLCFGR = CONFIG_RCC_PLLCFGR; - - #if defined(STM32H7) - // Reset PLL and clock configuration registers - RCC->D1CFGR = 0x00000000; - RCC->D2CFGR = 0x00000000; - RCC->D3CFGR = 0x00000000; - RCC->PLLCKSELR = 0x00000000; - RCC->D1CCIPR = 0x00000000; - RCC->D2CCIP1R = 0x00000000; - RCC->D2CCIP2R = 0x00000000; - RCC->D3CCIPR = 0x00000000; - #endif - - // Reset HSEBYP bit - RCC->CR &= (uint32_t)0xFFFBFFFF; - - // Disable all interrupts - #if defined(STM32F4) || defined(STM32F7) - RCC->CIR = 0x00000000; - #elif defined(STM32H7) - RCC->CIER = 0x00000000; - #endif - - // Set location of vector table - SCB->VTOR = FLASH_BASE; - - // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI - SCB->CCR |= SCB_CCR_STKALIGN_Msk; -} - void systick_init(void) { // Configure SysTick as 1ms ticker SysTick_Config(SystemCoreClock / 1000); @@ -1311,12 +1245,26 @@ static void do_reset(void) { NVIC_SystemReset(); } -uint32_t SystemCoreClock; - extern PCD_HandleTypeDef pcd_fs_handle; extern PCD_HandleTypeDef pcd_hs_handle; void stm32_main(int initial_r0) { + #if defined(STM32H7) + // Configure write-once power options, and wait for voltage levels to be ready + PWR->CR3 = PWR_CR3_LDOEN; + while (!(PWR->CSR1 & PWR_CSR1_ACTVOSRDY)) { + } + + // Reset the kernel clock configuration registers for all domains. + RCC->D1CCIPR = 0x00000000; + RCC->D2CCIP1R = 0x00000000; + RCC->D2CCIP2R = 0x00000000; + RCC->D3CCIPR = 0x00000000; + #endif + + // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI + SCB->CCR |= SCB_CCR_STKALIGN_Msk; + #if defined(STM32F4) #if INSTRUCTION_CACHE_ENABLE __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); @@ -1355,9 +1303,6 @@ void stm32_main(int initial_r0) { goto enter_bootloader; } - // MCU starts up with HSI - SystemCoreClock = HSI_VALUE; - int reset_mode = get_reset_mode(); uint32_t msp = *(volatile uint32_t*)APPLICATION_ADDR; if (reset_mode != 4 && (msp & APP_VALIDITY_BITS) == 0) { From 705728369d7422905825e8e4d4c8001524b990e7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 19:35:52 +1000 Subject: [PATCH 0104/3533] stm32/mboot: Add support for using mboot with WB MCUs. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 2 ++ ports/stm32/mboot/main.c | 44 +++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index a504c2314d3f9..79aa3316f9b65 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -55,6 +55,7 @@ CFLAGS_MCU_f4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_f7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 CFLAGS_MCU_h7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 CFLAGS_MCU_l4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 +CFLAGS_MCU_wb = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS = $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -D$(CMSIS_MCU) @@ -103,6 +104,7 @@ SRC_C = \ ports/stm32/flash.c \ ports/stm32/flashbdev.c \ ports/stm32/i2cslave.c \ + ports/stm32/powerctrlboot.c \ ports/stm32/qspi.c \ ports/stm32/spibdev.c \ ports/stm32/usbd_conf.c \ diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index c8cd7ba60e673..b3af495261a14 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -33,29 +33,36 @@ #include "storage.h" #include "flash.h" #include "i2cslave.h" +#include "irq.h" #include "mboot.h" +#include "powerctrl.h" #include "dfu.h" // Using polling is about 10% faster than not using it (and using IRQ instead) // This DFU code with polling runs in about 70% of the time of the ST bootloader +// With STM32WB MCUs only non-polling/IRQ mode is supported. +#if defined(STM32WB) +#define USE_USB_POLLING (0) +#else #define USE_USB_POLLING (1) +#endif // Using cache probably won't make it faster because we run at a low frequency, and best // to keep the MCU config as minimal as possible. #define USE_CACHE (0) // IRQ priorities (encoded values suitable for NVIC_SetPriority) -#define IRQ_PRI_SYSTICK (NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 0, 0)) +// Most values are defined in irq.h. #define IRQ_PRI_I2C (NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 1, 0)) // Configure PLL to give the desired CPU freq #undef MICROPY_HW_FLASH_LATENCY -#if defined(STM32H7) -#define CORE_PLL_FREQ (96000000) -#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_2 -#else +#if defined(STM32F4) || defined(STM32F7) #define CORE_PLL_FREQ (48000000) #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_1 +#elif defined(STM32H7) +#define CORE_PLL_FREQ (96000000) +#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_2 #endif #undef MICROPY_HW_CLK_PLLM #undef MICROPY_HW_CLK_PLLN @@ -93,7 +100,11 @@ uint32_t get_le32(const uint8_t *b) { void mp_hal_delay_us(mp_uint_t usec) { // use a busy loop for the delay // sys freq is always a multiple of 2MHz, so division here won't lose precision + #if defined(CORE_PLL_FREQ) const uint32_t ucount = CORE_PLL_FREQ / 2000000 * usec / 2; + #else + const uint32_t ucount = SystemCoreClock / 2000000 * usec / 2; + #endif for (uint32_t count = 0; ++count <= ucount;) { } } @@ -313,6 +324,9 @@ uint32_t HAL_RCC_GetHCLKFreq(void) { #elif defined(STM32H7) #define AHBxENR AHB4ENR #define AHBxENR_GPIOAEN_Pos RCC_AHB4ENR_GPIOAEN_Pos +#elif defined(STM32WB) +#define AHBxENR AHB2ENR +#define AHBxENR_GPIOAEN_Pos RCC_AHB2ENR_GPIOAEN_Pos #endif void mp_hal_pin_config(mp_hal_pin_obj_t port_pin, uint32_t mode, uint32_t pull, uint32_t alt) { @@ -445,6 +459,8 @@ static int usrbtn_state(void) { #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/04*032Kg,01*128Kg,07*256Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #elif defined(STM32H743xx) #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/16*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT +#elif defined(STM32WB) +#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*04Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #endif static int mboot_flash_mass_erase(void) { @@ -924,12 +940,19 @@ typedef struct _pyb_usbdd_obj_t { #define MBOOT_USB_PID BOOTLOADER_DFU_USB_PID #endif +#if !MICROPY_HW_USB_IS_MULTI_OTG +STATIC const uint8_t usbd_fifo_size[USBD_PMA_NUM_FIFO] = { + 32, 32, // EP0(out), EP0(in) + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x unused +}; +#else static const uint8_t usbd_fifo_size[] = { 32, 8, 16, 8, 16, 0, 0, // FS: RX, EP0(in), 5x IN endpoints #if MICROPY_HW_USB_HS 116, 8, 64, 4, 64, 0, 0, 0, 0, 0, // HS: RX, EP0(in), 8x IN endpoints #endif }; +#endif __ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { USB_LEN_LANGID_STR_DESC, @@ -1455,6 +1478,15 @@ void I2Cx_EV_IRQHandler(void) { #endif #if !USE_USB_POLLING + +#if defined(STM32WB) + +void USB_LP_IRQHandler(void) { + HAL_PCD_IRQHandler(&pcd_fs_handle); +} + +#else + #if MBOOT_USB_AUTODETECT_PORT || MICROPY_HW_USB_MAIN_DEV == USB_PHY_FS_ID void OTG_FS_IRQHandler(void) { HAL_PCD_IRQHandler(&pcd_fs_handle); @@ -1467,3 +1499,5 @@ void OTG_HS_IRQHandler(void) { } #endif #endif + +#endif From 6f40e6e131b666b373ff3245a095c7e710578c16 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 19:36:16 +1000 Subject: [PATCH 0105/3533] stm32/boards: Add build-time option for NUCLEO_WB55 to use mboot. As an example of how to use mboot on a WB series MCU. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk | 11 ++++++++++- ports/stm32/boards/stm32wb55xg.ld | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk index 416364df9ef6d..dcec788ed443f 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk @@ -1,9 +1,18 @@ MCU_SERIES = wb CMSIS_MCU = STM32WB55xx AF_FILE = boards/stm32wb55_af.csv -LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld STARTUP_FILE = lib/stm32lib/CMSIS/STM32WBxx/Source/Templates/gcc/startup_stm32wb55xx_cm4.o +ifeq ($(USE_MBOOT),1) +# When using Mboot all the text goes together after the bootloader +LD_FILES = boards/stm32wb55xg.ld boards/common_bl.ld +TEXT0_ADDR = 0x08004000 +else +# When not using Mboot the text goes at the start of flash +LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld +TEXT0_ADDR = 0x08000000 +endif + # MicroPython settings MICROPY_PY_BLUETOOTH = 1 MICROPY_BLUETOOTH_NIMBLE = 1 diff --git a/ports/stm32/boards/stm32wb55xg.ld b/ports/stm32/boards/stm32wb55xg.ld index 77596d7759689..dbeccc1895775 100644 --- a/ports/stm32/boards/stm32wb55xg.ld +++ b/ports/stm32/boards/stm32wb55xg.ld @@ -6,6 +6,7 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* sectors 0-127 */ + FLASH_APP (rx) : ORIGIN = 0x08004000, LENGTH = 496K /* sectors 4-127 */ FLASH_FS (r) : ORIGIN = 0x08080000, LENGTH = 256K /* sectors 128-191 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K /* SRAM1 */ RAM2A (xrw) : ORIGIN = 0x20030020, LENGTH = 8K /* SRAM2A */ From 13ad1a4f06f2d114c821279f58fa7462e8bd0d41 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Jun 2020 19:59:54 +1000 Subject: [PATCH 0106/3533] travis: In stm32 job, build mboot for NUCLEO_WB55. Signed-off-by: Damien George --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 09da9598e2018..b7aac0bd0d0fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -79,6 +79,7 @@ jobs: - make ${MAKEOPTS} -C ports/stm32 BOARD=STM32L476DISC - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_WB55 - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBD_SF6 + - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=NUCLEO_WB55 # qemu-arm port - stage: test From 81a7293ed63bb39e05e4303f7ad297807748c7ea Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 13:56:20 +1000 Subject: [PATCH 0107/3533] stm32/mboot: Set VTOR on start up to ensure it has the correct value. Commit 8675858465f3d83aab709170eab6dc141570acf4 switched to using the CMSIS provided SystemInit function which sets VTOR to 0x00000000 (previously it was 0x08000000). A VTOR of 0x00000000 will be correct on some MCUs but not on others where the built-in bootloader is remapped to this address, via __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(). To make sure mboot has the correct vector table, this commit explicitly sets VTOR to the correct value of 0x08000000. Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index b3af495261a14..b3e63ccc26626 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -1285,6 +1285,9 @@ void stm32_main(int initial_r0) { RCC->D3CCIPR = 0x00000000; #endif + // Make sure IRQ vector table points to flash where this bootloader lives. + SCB->VTOR = FLASH_BASE; + // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI SCB->CCR |= SCB_CCR_STKALIGN_Msk; From b4dc4c5b9a1e6df620a14d85f18721a0bcb17d83 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 14:00:16 +1000 Subject: [PATCH 0108/3533] stm32/mboot: Use additional CFLAGS to compile string0.c. This is the same as a902b69dd51de0e3fe3bb6955296591d6a93abab but applied to mboot's Makefile. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 79aa3316f9b65..a72b752ac9430 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -57,6 +57,10 @@ CFLAGS_MCU_h7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 CFLAGS_MCU_l4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_wb = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 +# Standard C functions like memset need to be compiled with special flags so +# the compiler does not optimise these functions in terms of themselves. +CFLAGS_BUILTIN ?= -ffreestanding -fno-builtin -fno-lto + CFLAGS = $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -D$(CMSIS_MCU) CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) @@ -84,6 +88,7 @@ else COPT += -Os -DNDEBUG endif +$(BUILD)/lib/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN) LIB_SRC_C = \ lib/libc/string0.c \ lib/oofatfs/ff.c \ From 456a3abe8dbcd8ee822d81d6780db72d55e89d61 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Jun 2020 17:10:20 +1000 Subject: [PATCH 0109/3533] py/obj.h: Add public mp_obj_is_dict_or_ordereddict() helper macro. And use it in py/objdict.c instead of mp_obj_is_dict_type. Signed-off-by: Damien George --- py/obj.h | 2 ++ py/objdict.c | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/py/obj.h b/py/obj.h index f80f00031dc77..590b9c4b6a4a1 100644 --- a/py/obj.h +++ b/py/obj.h @@ -708,6 +708,7 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj; #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int)) #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str)) #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op)) +#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new) #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function)) mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict); @@ -890,6 +891,7 @@ typedef struct _mp_obj_dict_t { mp_obj_base_t base; mp_map_t map; } mp_obj_dict_t; +mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args); size_t mp_obj_dict_len(mp_obj_t self_in); mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index); diff --git a/py/objdict.c b/py/objdict.c index 69eda99738eec..4fa59f4634cb0 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -33,8 +33,6 @@ #include "py/objtype.h" #include "py/objstr.h" -#define mp_obj_is_dict_type(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == dict_make_new) - STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); // This is a helper function to iterate through a dictionary. The state of @@ -90,7 +88,7 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_ } } -STATIC mp_obj_t dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_obj_t dict_out = mp_obj_new_dict(0); mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out); dict->base.type = type; @@ -217,7 +215,7 @@ STATIC void mp_ensure_not_fixed(const mp_obj_dict_t *dict) { } STATIC mp_obj_t dict_clear(mp_obj_t self_in) { - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_ensure_not_fixed(self); @@ -228,7 +226,7 @@ STATIC mp_obj_t dict_clear(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear); mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) { - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t other_out = mp_obj_new_dict(self->map.alloc); mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out); @@ -275,7 +273,7 @@ STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromk #endif STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_lookup_kind_t lookup_kind) { - mp_check_self(mp_obj_is_dict_type(args[0])); + mp_check_self(mp_obj_is_dict_or_ordereddict(args[0])); mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]); if (lookup_kind != MP_MAP_LOOKUP) { mp_ensure_not_fixed(self); @@ -320,7 +318,7 @@ STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault); STATIC mp_obj_t dict_popitem(mp_obj_t self_in) { - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_ensure_not_fixed(self); if (self->map.used == 0) { @@ -345,7 +343,7 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem); STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - mp_check_self(mp_obj_is_dict_type(args[0])); + mp_check_self(mp_obj_is_dict_or_ordereddict(args[0])); mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]); mp_ensure_not_fixed(self); @@ -354,7 +352,7 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg if (n_args == 2) { // given a positional argument - if (mp_obj_is_dict_type(args[1])) { + if (mp_obj_is_dict_or_ordereddict(args[1])) { // update from other dictionary (make sure other is not self) if (args[1] != args[0]) { size_t cur = 0; @@ -512,7 +510,7 @@ STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) { } STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) { - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); return mp_obj_new_dict_view(self_in, kind); } @@ -536,7 +534,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values); STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t)); - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf; o->base.type = &dict_view_it_type; o->kind = MP_DICT_VIEW_KEYS; @@ -573,7 +571,7 @@ const mp_obj_type_t mp_type_dict = { { &mp_type_type }, .name = MP_QSTR_dict, .print = dict_print, - .make_new = dict_make_new, + .make_new = mp_obj_dict_make_new, .unary_op = dict_unary_op, .binary_op = dict_binary_op, .subscr = dict_subscr, @@ -586,7 +584,7 @@ const mp_obj_type_t mp_type_ordereddict = { { &mp_type_type }, .name = MP_QSTR_OrderedDict, .print = dict_print, - .make_new = dict_make_new, + .make_new = mp_obj_dict_make_new, .unary_op = dict_unary_op, .binary_op = dict_binary_op, .subscr = dict_subscr, @@ -613,7 +611,7 @@ size_t mp_obj_dict_len(mp_obj_t self_in) { } mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) { - mp_check_self(mp_obj_is_dict_type(self_in)); + mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_ensure_not_fixed(self); mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value; From 7dd480ad5505a40bb33ee52d01c5fa78b1273083 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Jun 2020 17:14:18 +1000 Subject: [PATCH 0110/3533] extmod/moductypes: Use mp_obj_is_dict_or_ordereddict to simplify code. Signed-off-by: Damien George --- extmod/moductypes.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index f7d2be1bc1487..811258424a3bf 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -137,11 +137,7 @@ STATIC void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_p (void)kind; mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in); const char *typen = "unk"; - if (mp_obj_is_type(self->desc, &mp_type_dict) - #if MICROPY_PY_COLLECTIONS_ORDEREDDICT - || mp_obj_is_type(self->desc, &mp_type_ordereddict) - #endif - ) { + if (mp_obj_is_dict_or_ordereddict(self->desc)) { typen = "STRUCT"; } else if (mp_obj_is_type(self->desc, &mp_type_tuple)) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc); @@ -214,11 +210,7 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_ } STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_t *max_field_size) { - if (!mp_obj_is_type(desc_in, &mp_type_dict) - #if MICROPY_PY_COLLECTIONS_ORDEREDDICT - && !mp_obj_is_type(desc_in, &mp_type_ordereddict) - #endif - ) { + if (!mp_obj_is_dict_or_ordereddict(desc_in)) { if (mp_obj_is_type(desc_in, &mp_type_tuple)) { return uctypes_struct_agg_size((mp_obj_tuple_t *)MP_OBJ_TO_PTR(desc_in), layout_type, max_field_size); } else if (mp_obj_is_small_int(desc_in)) { @@ -418,11 +410,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) { STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) { mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in); - if (!mp_obj_is_type(self->desc, &mp_type_dict) - #if MICROPY_PY_COLLECTIONS_ORDEREDDICT - && !mp_obj_is_type(self->desc, &mp_type_ordereddict) - #endif - ) { + if (!mp_obj_is_dict_or_ordereddict(self->desc)) { mp_raise_TypeError(MP_ERROR_TEXT("struct: no fields")); } From 457fdf61c3bca550b4ad3fb2c6822c838984dac0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Jun 2020 17:25:44 +1000 Subject: [PATCH 0111/3533] py/objtype: Support passing in an OrderedDict to type() as the locals. An OrderedDict can now be used for the locals when creating a type explicitly via type(name, bases, locals). Signed-off-by: Damien George --- py/objtype.c | 6 +++--- tests/basics/class_ordereddict.py | 18 ++++++++++++++++++ tests/basics/class_ordereddict.py.exp | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/basics/class_ordereddict.py create mode 100644 tests/basics/class_ordereddict.py.exp diff --git a/py/objtype.c b/py/objtype.c index a539fcab29414..40900dc050358 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -153,7 +153,7 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_t if (type->locals_dict != NULL) { // search locals_dict (the set of methods/attributes) - assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now + assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(type->locals_dict))); // MicroPython restriction, for now mp_map_t *locals_map = &type->locals_dict->map; mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(lookup->attr), MP_MAP_LOOKUP); if (elem != NULL) { @@ -1053,7 +1053,7 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // delete/store attribute if (self->locals_dict != NULL) { - assert(self->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now + assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(self->locals_dict))); // MicroPython restriction, for now mp_map_t *locals_map = &self->locals_dict->map; if (locals_map->is_fixed) { // can't apply delete/store to a fixed map @@ -1103,7 +1103,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) if (!mp_obj_is_type(bases_tuple, &mp_type_tuple)) { mp_raise_TypeError(NULL); } - if (!mp_obj_is_type(locals_dict, &mp_type_dict)) { + if (!mp_obj_is_dict_or_ordereddict(locals_dict)) { mp_raise_TypeError(NULL); } diff --git a/tests/basics/class_ordereddict.py b/tests/basics/class_ordereddict.py new file mode 100644 index 0000000000000..4dd25eb6e15b4 --- /dev/null +++ b/tests/basics/class_ordereddict.py @@ -0,0 +1,18 @@ +# test using an OrderedDict as the locals to construct a class + +try: + from ucollections import OrderedDict +except ImportError: + try: + from collections import OrderedDict + except ImportError: + print("SKIP") + raise SystemExit + +if not hasattr(int, "__dict__"): + print("SKIP") + raise SystemExit + + +A = type("A", (), OrderedDict(a=1, b=2, c=3, d=4, e=5)) +print([k for k in A.__dict__.keys() if not k.startswith("_")]) diff --git a/tests/basics/class_ordereddict.py.exp b/tests/basics/class_ordereddict.py.exp new file mode 100644 index 0000000000000..b723e327515c7 --- /dev/null +++ b/tests/basics/class_ordereddict.py.exp @@ -0,0 +1 @@ +['a', 'b', 'c', 'd', 'e'] From 76faeed098ec4ee70202202cb11e64b78a77f14e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 17:20:59 +1000 Subject: [PATCH 0112/3533] tools/makemanifest.py: Support freezing a subdirectory recursively. This adds support for freezing an entire directory while keeping the directory as part of the import path. For example freeze("path/to/library", "module") will recursively freeze all scripts in "path/to/library/module" and have them importable as "from module import ...". Signed-off-by: Damien George --- tools/makemanifest.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 2cee6aebc796a..babce7fa73f4b 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -76,9 +76,10 @@ def freeze(path, script=None, opt=0): If `script` is an iterable then freeze() is called on all items of the iterable (with the same `path` and `opt` passed through). - If `script` is a string then it specifies the filename to freeze, and - can include extra directories before the file. The file will be - searched for in `path`. + If `script` is a string then it specifies the file or directory to + freeze, and can include extra directories before the file or last + directory. The file or directory will be searched for in `path`. If + `script` is a directory then all files in that directory will be frozen. `opt` is the optimisation level to pass to mpy-cross when compiling .py to .mpy. @@ -182,14 +183,22 @@ def freeze_internal(kind, path, script, opt): if any(f[0] == KIND_AS_STR for f in manifest_list): raise FreezeError("can only freeze one str directory") manifest_list.append((KIND_AS_STR, path, script, opt)) - elif script is None: - for dirpath, dirnames, filenames in os.walk(path, followlinks=True): + elif script is None or isinstance(script, str) and script.find(".") == -1: + # Recursively search `path` for files to freeze, optionally restricted + # to a subdirectory specified by `script` + if script is None: + subdir = "" + else: + subdir = "/" + script + for dirpath, dirnames, filenames in os.walk(path + subdir, followlinks=True): for f in filenames: freeze_internal(kind, path, (dirpath + "/" + f)[len(path) + 1 :], opt) elif not isinstance(script, str): + # `script` is an iterable of items to freeze for s in script: freeze_internal(kind, path, s, opt) else: + # `script` should specify an individual file to be frozen extension_kind = {KIND_AS_MPY: ".py", KIND_MPY: ".mpy"} if kind == KIND_AUTO: for k, ext in extension_kind.items(): From 0c77668d11a0b8db7af57311828169e3fdc9678f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 Jun 2020 16:31:33 +1000 Subject: [PATCH 0113/3533] extmod/vfs_lfs: Fix littlefs bindings to build in nan-box mode. Signed-off-by: Damien George --- extmod/vfs_lfsx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index c6240967822aa..24816433be015 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -442,7 +442,7 @@ STATIC mp_import_stat_t MP_VFS_LFSx(import_stat)(void *self_in, const char *path MP_OBJ_VFS_LFSx *self = self_in; struct LFSx_API (info) info; mp_obj_str_t path_obj = { { &mp_type_str }, 0, 0, (const byte *)path }; - path = MP_VFS_LFSx(make_path)(self, &path_obj); + path = MP_VFS_LFSx(make_path)(self, MP_OBJ_FROM_PTR(&path_obj)); int ret = LFSx_API(stat)(&self->lfs, path, &info); if (ret == 0) { if (info.type == LFSx_MACRO(_TYPE_REG)) { From eb9850ef6cbb1d5a05d18a1e050f7a78b106c502 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 Jun 2020 16:08:05 +1000 Subject: [PATCH 0114/3533] stm32/mpconfigport.h: Enable PY_IO_FILEIO when any VFS is enabled. Previously, if FAT was not enabled but LFS1/2 was then MICROPY_PY_IO_FILEIO would be disabled and file binary-mode was not supported. Signed-off-by: Damien George --- ports/stm32/mpconfigport.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 13dce6e96d334..6f38d90e8d800 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -120,7 +120,7 @@ #define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO (1) #define MICROPY_PY_IO_IOBASE (1) -#define MICROPY_PY_IO_FILEIO (MICROPY_VFS_FAT) // because mp_type_fileio/textio point to fatfs impl +#define MICROPY_PY_IO_FILEIO (MICROPY_VFS_FAT || MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_STDFILES (1) @@ -210,9 +210,17 @@ #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MULTI_PARTITION (1) -// TODO these should be generic, not bound to fatfs +// TODO these should be generic, not bound to a particular FS implementation +#if MICROPY_VFS_FAT #define mp_type_fileio mp_type_vfs_fat_fileio #define mp_type_textio mp_type_vfs_fat_textio +#elif MICROPY_VFS_LFS1 +#define mp_type_fileio mp_type_vfs_lfs1_fileio +#define mp_type_textio mp_type_vfs_lfs1_textio +#elif MICROPY_VFS_LFS2 +#define mp_type_fileio mp_type_vfs_lfs2_fileio +#define mp_type_textio mp_type_vfs_lfs2_textio +#endif // use vfs's functions for import stat and builtin open #define mp_import_stat mp_vfs_import_stat From c5af3217d985f517287c6b85bda16c5b7f9fa677 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 Jun 2020 16:10:00 +1000 Subject: [PATCH 0115/3533] stm32/timer: Support TIM1 on WB MCUs. Signed-off-by: Damien George --- ports/stm32/stm32_it.c | 2 +- ports/stm32/timer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/stm32_it.c b/ports/stm32/stm32_it.c index b84f4adfaeb81..8e96da177b583 100644 --- a/ports/stm32/stm32_it.c +++ b/ports/stm32/stm32_it.c @@ -602,7 +602,7 @@ void TIM1_UP_TIM10_IRQHandler(void) { IRQ_EXIT(TIM1_UP_TIM10_IRQn); } -#if defined(STM32L4) +#if defined(STM32L4) || defined(STM32WB) void TIM1_UP_TIM16_IRQHandler(void) { IRQ_ENTER(TIM1_UP_TIM16_IRQn); timer_irq_handler(1); diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 491221ec2a7de..c6f3d21f8671e 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -805,7 +805,7 @@ STATIC const uint32_t tim_instance_table[MICROPY_HW_MAX_TIMER] = { TIM_ENTRY(1, TIM1_UP_TIM10_IRQn), #elif defined(STM32H7) TIM_ENTRY(1, TIM1_UP_IRQn), - #elif defined(STM32L4) + #elif defined(STM32L4) || defined(STM32WB) TIM_ENTRY(1, TIM1_UP_TIM16_IRQn), #endif #endif From 717b5073aa7295164f3c06a889b648071b435bc3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 Jun 2020 16:11:48 +1000 Subject: [PATCH 0116/3533] stm32/boards: Enable LFS2 on PYBD_SF3 and PYBD_SF6. This was missed in commit 120368ba1ab444b2f1c17d1eb69bc6f09072ec5d Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF3/mpconfigboard.mk | 1 + ports/stm32/boards/PYBD_SF6/mpconfigboard.mk | 1 + 2 files changed, 2 insertions(+) diff --git a/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk b/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk index 119ec8f825b42..abe3dcd862373 100644 --- a/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk +++ b/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk @@ -17,6 +17,7 @@ MICROPY_PY_LWIP = 1 MICROPY_PY_NETWORK_CYW43 = 1 MICROPY_PY_USSL = 1 MICROPY_SSL_MBEDTLS = 1 +MICROPY_VFS_LFS2 = 1 # PYBD-specific frozen modules FROZEN_MANIFEST = boards/PYBD_SF2/manifest.py diff --git a/ports/stm32/boards/PYBD_SF6/mpconfigboard.mk b/ports/stm32/boards/PYBD_SF6/mpconfigboard.mk index ddc176e9cd422..bac516ab7949f 100644 --- a/ports/stm32/boards/PYBD_SF6/mpconfigboard.mk +++ b/ports/stm32/boards/PYBD_SF6/mpconfigboard.mk @@ -14,6 +14,7 @@ MICROPY_PY_LWIP = 1 MICROPY_PY_NETWORK_CYW43 = 1 MICROPY_PY_USSL = 1 MICROPY_SSL_MBEDTLS = 1 +MICROPY_VFS_LFS2 = 1 # PYBD-specific frozen modules FROZEN_MANIFEST = boards/PYBD_SF2/manifest.py From 763bd448a4348b36db08cbae41337507e47cb892 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 16:16:21 +1000 Subject: [PATCH 0117/3533] stm32/mboot: Don't search for firmware on FS, just attempt to open it. There's no need to do a directory listing to search for the given firmware filename, it just takes extra time and code size. Instead this commit changes it so that the requested firmware file is opened immediately and will abort if the file couldn't be opened. This also allows to specify files in a directory. Signed-off-by: Damien George --- ports/stm32/mboot/fsload.c | 43 ++++++++++++-------------------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index 64eb2a3a5ba72..12f39c65b360d 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -204,7 +204,7 @@ static int fsload_program_file(FATFS *fatfs, const char *filename, bool write_to return 0; } -static int fsload_process_fatfs(uint32_t base_addr, uint32_t byte_len, size_t fname_len, const char *fname) { +static int fsload_process_fatfs(uint32_t base_addr, uint32_t byte_len, const char *fname) { fsload_bdev_t bdev = {base_addr, byte_len}; FATFS fatfs; fatfs.drv = &bdev; @@ -213,34 +213,14 @@ static int fsload_process_fatfs(uint32_t base_addr, uint32_t byte_len, size_t fn return -1; } - FF_DIR dp; - res = f_opendir(&fatfs, &dp, "/"); - if (res != FR_OK) { - return -1; - } + // Validate firmware + led_state_all(2); + int r = fsload_program_file(&fatfs, fname, false); - // Search for firmware file with correct name - int r; - for (;;) { - FILINFO fno; - res = f_readdir(&dp, &fno); - char *fn = fno.fname; - if (res != FR_OK || fn[0] == 0) { - // Finished listing dir, no firmware found - r = -1; - break; - } - if (memcmp(fn, fname, fname_len) == 0 && fn[fname_len] == '\0') { - // Found firmware - led_state_all(2); - r = fsload_program_file(&fatfs, fn, false); - if (r == 0) { - // Firmware is valid, program it - led_state_all(4); - r = fsload_program_file(&fatfs, fn, true); - } - break; - } + if (r == 0) { + // Firmware is valid, program it + led_state_all(4); + r = fsload_program_file(&fatfs, fname, true); } return r; @@ -252,9 +232,12 @@ int fsload_process(void) { return -1; } + // Get mount point id and create null-terminated filename uint8_t mount_point = elem[0]; uint8_t fname_len = elem[-1] - 1; - const char *fname = (const char*)&elem[1]; + char fname[256]; + memcpy(fname, &elem[1], fname_len); + fname[fname_len] = '\0'; elem = ELEM_DATA_START; for (;;) { @@ -267,7 +250,7 @@ int fsload_process(void) { uint32_t base_addr = get_le32(&elem[2]); uint32_t byte_len = get_le32(&elem[6]); if (elem[1] == ELEM_MOUNT_FAT) { - int ret = fsload_process_fatfs(base_addr, byte_len, fname_len, fname); + int ret = fsload_process_fatfs(base_addr, byte_len, fname); // Flash LEDs based on success/failure of update for (int i = 0; i < 4; ++i) { if (ret == 0) { From 390f32922d81b56495198d3d93b7258f450c03b6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 22:57:16 +1000 Subject: [PATCH 0118/3533] stm32/mboot: Decouple stream, filesystem and top-level loading code. This commit factors the code for files and streaming to separate source files (vfs_fat.c and gzstream.c respectively) and introduces an abstract gzstream interface to make it easier to plug in different filesystems. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 3 +- ports/stm32/mboot/fsload.c | 110 +++++----------------- ports/stm32/mboot/gzstream.c | 94 ++++++++++++++++++ ports/stm32/mboot/gzstream.h | 45 +++++++++ ports/stm32/mboot/mboot.h | 5 - ports/stm32/mboot/vfs.h | 43 +++++++++ ports/stm32/mboot/{diskio.c => vfs_fat.c} | 54 +++++++++-- 7 files changed, 256 insertions(+), 98 deletions(-) create mode 100644 ports/stm32/mboot/gzstream.c create mode 100644 ports/stm32/mboot/gzstream.h create mode 100644 ports/stm32/mboot/vfs.h rename ports/stm32/mboot/{diskio.c => vfs_fat.c} (60%) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index a72b752ac9430..9ab186412ea93 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -102,7 +102,8 @@ SRC_C = \ main.c \ elem.c \ fsload.c \ - diskio.c \ + gzstream.c \ + vfs_fat.c \ drivers/bus/softspi.c \ drivers/bus/softqspi.c \ drivers/memory/spiflash.c \ diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index 12f39c65b360d..dc40a28ea02d4 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -27,81 +27,18 @@ #include #include "py/mphal.h" -#include "lib/oofatfs/ff.h" -#include "extmod/uzlib/uzlib.h" #include "mboot.h" +#include "vfs.h" #if MBOOT_FSLOAD -#define DICT_SIZE (1 << 15) - -typedef struct _gz_stream_t { - FIL fp; - TINF_DATA tinf; - uint8_t buf[512]; - uint8_t dict[DICT_SIZE]; -} gz_stream_t; - -static gz_stream_t gz_stream SECTION_NOZERO_BSS; - -static int gz_stream_read_src(TINF_DATA *tinf) { - UINT n; - FRESULT res = f_read(&gz_stream.fp, gz_stream.buf, sizeof(gz_stream.buf), &n); - if (res != FR_OK) { - return -1; - } - if (n == 0) { - return -1; - } - tinf->source = gz_stream.buf + 1; - tinf->source_limit = gz_stream.buf + n; - return gz_stream.buf[0]; -} - -static int gz_stream_open(FATFS *fatfs, const char *filename) { - FRESULT res = f_open(fatfs, &gz_stream.fp, filename, FA_READ); - if (res != FR_OK) { - return -1; - } - memset(&gz_stream.tinf, 0, sizeof(gz_stream.tinf)); - gz_stream.tinf.readSource = gz_stream_read_src; - - int st = uzlib_gzip_parse_header(&gz_stream.tinf); - if (st != TINF_OK) { - f_close(&gz_stream.fp); - return -1; - } - - uzlib_uncompress_init(&gz_stream.tinf, gz_stream.dict, DICT_SIZE); - - return 0; -} - -static int gz_stream_read(size_t len, uint8_t *buf) { - gz_stream.tinf.dest = buf; - gz_stream.tinf.dest_limit = buf + len; - int st = uzlib_uncompress_chksum(&gz_stream.tinf); - if (st == TINF_DONE) { - return 0; - } - if (st < 0) { - return st; - } - return gz_stream.tinf.dest - buf; -} - -static int fsload_program_file(FATFS *fatfs, const char *filename, bool write_to_flash) { - int res = gz_stream_open(fatfs, filename); - if (res != 0) { - return res; - } - +static int fsload_program_file(bool write_to_flash) { // Parse DFU uint8_t buf[512]; size_t file_offset; // Read file header, <5sBIB - res = gz_stream_read(11, buf); + int res = gz_stream_read(11, buf); if (res != 11) { return -1; } @@ -204,26 +141,23 @@ static int fsload_program_file(FATFS *fatfs, const char *filename, bool write_to return 0; } -static int fsload_process_fatfs(uint32_t base_addr, uint32_t byte_len, const char *fname) { - fsload_bdev_t bdev = {base_addr, byte_len}; - FATFS fatfs; - fatfs.drv = &bdev; - FRESULT res = f_mount(&fatfs); - if (res != FR_OK) { - return -1; - } - - // Validate firmware - led_state_all(2); - int r = fsload_program_file(&fatfs, fname, false); - - if (r == 0) { - // Firmware is valid, program it - led_state_all(4); - r = fsload_program_file(&fatfs, fname, true); +static int fsload_validate_and_program_file(void *stream, const stream_methods_t *meth, const char *fname) { + // First pass verifies the file, second pass programs it + for (unsigned int pass = 0; pass <= 1; ++pass) { + led_state_all(pass == 0 ? 2 : 4); + int res = meth->open(stream, fname); + if (res == 0) { + res = gz_stream_init(stream, meth->read); + if (res == 0) { + res = fsload_program_file(pass == 0 ? false : true); + } + } + meth->close(stream); + if (res != 0) { + return res; + } } - - return r; + return 0; } int fsload_process(void) { @@ -250,7 +184,11 @@ int fsload_process(void) { uint32_t base_addr = get_le32(&elem[2]); uint32_t byte_len = get_le32(&elem[6]); if (elem[1] == ELEM_MOUNT_FAT) { - int ret = fsload_process_fatfs(base_addr, byte_len, fname); + vfs_fat_context_t ctx; + int ret = vfs_fat_mount(&ctx, base_addr, byte_len); + if (ret == 0) { + ret = fsload_validate_and_program_file(&ctx, &vfs_fat_stream_methods, fname); + } // Flash LEDs based on success/failure of update for (int i = 0; i < 4; ++i) { if (ret == 0) { diff --git a/ports/stm32/mboot/gzstream.c b/ports/stm32/mboot/gzstream.c new file mode 100644 index 0000000000000..20ba33045f8d8 --- /dev/null +++ b/ports/stm32/mboot/gzstream.c @@ -0,0 +1,94 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019-2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/mphal.h" +#include "extmod/uzlib/uzlib.h" +#include "gzstream.h" +#include "mboot.h" + +#if MBOOT_FSLOAD + +#define DICT_SIZE (1 << 15) + +typedef struct _gz_stream_t { + void *stream_data; + stream_read_t stream_read; + TINF_DATA tinf; + uint8_t buf[512]; + uint8_t dict[DICT_SIZE]; +} gz_stream_t; + +static gz_stream_t gz_stream SECTION_NOZERO_BSS; + +static int gz_stream_read_src(TINF_DATA *tinf) { + int n = gz_stream.stream_read(gz_stream.stream_data, gz_stream.buf, sizeof(gz_stream.buf)); + if (n < 0) { + // Stream error + return -1; + } + if (n == 0) { + // No data / EOF + return -1; + } + + tinf->source = gz_stream.buf + 1; + tinf->source_limit = gz_stream.buf + n; + return gz_stream.buf[0]; +} + +int gz_stream_init(void *stream_data, stream_read_t stream_read) { + gz_stream.stream_data = stream_data; + gz_stream.stream_read = stream_read; + + memset(&gz_stream.tinf, 0, sizeof(gz_stream.tinf)); + gz_stream.tinf.readSource = gz_stream_read_src; + + int st = uzlib_gzip_parse_header(&gz_stream.tinf); + if (st != TINF_OK) { + return -1; + } + + uzlib_uncompress_init(&gz_stream.tinf, gz_stream.dict, DICT_SIZE); + + return 0; +} + +int gz_stream_read(size_t len, uint8_t *buf) { + gz_stream.tinf.dest = buf; + gz_stream.tinf.dest_limit = buf + len; + int st = uzlib_uncompress_chksum(&gz_stream.tinf); + if (st == TINF_DONE) { + return 0; + } + if (st < 0) { + return st; + } + return gz_stream.tinf.dest - buf; +} + +#endif // MBOOT_FSLOAD diff --git a/ports/stm32/mboot/gzstream.h b/ports/stm32/mboot/gzstream.h new file mode 100644 index 0000000000000..ec11ba79bbb02 --- /dev/null +++ b/ports/stm32/mboot/gzstream.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019-2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_STM32_MBOOT_GZSTREAM_H +#define MICROPY_INCLUDED_STM32_MBOOT_GZSTREAM_H + +#include +#include + +typedef int (*stream_open_t)(void *stream, const char *fname); +typedef void (*stream_close_t)(void *stream); +typedef int (*stream_read_t)(void *stream, uint8_t *buf, size_t len); + +typedef struct _stream_methods_t { + stream_open_t open; + stream_close_t close; + stream_read_t read; +} stream_methods_t; + +int gz_stream_init(void *stream_data, stream_read_t stream_read); +int gz_stream_read(size_t len, uint8_t *buf); + +#endif // MICROPY_INCLUDED_STM32_MBOOT_GZSTREAM_H diff --git a/ports/stm32/mboot/mboot.h b/ports/stm32/mboot/mboot.h index 7dc1ada0c32f6..ef9af7854b894 100644 --- a/ports/stm32/mboot/mboot.h +++ b/ports/stm32/mboot/mboot.h @@ -44,11 +44,6 @@ enum { ELEM_MOUNT_FAT = 1, }; -typedef struct _fsload_bdev_t { - uint32_t base_addr; - uint32_t byte_len; -} fsload_bdev_t; - extern uint8_t _estack[ELEM_DATA_SIZE]; uint32_t get_le32(const uint8_t *b); diff --git a/ports/stm32/mboot/vfs.h b/ports/stm32/mboot/vfs.h new file mode 100644 index 0000000000000..9ee1c5ff3cd42 --- /dev/null +++ b/ports/stm32/mboot/vfs.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019-2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_STM32_MBOOT_VFS_H +#define MICROPY_INCLUDED_STM32_MBOOT_VFS_H + +#include "lib/oofatfs/ff.h" +#include "gzstream.h" + +typedef struct _vfs_fat_context_t { + uint32_t bdev_base_addr; + uint32_t bdev_byte_len; + FATFS fatfs; + FIL fp; +} vfs_fat_context_t; + +extern const stream_methods_t vfs_fat_stream_methods; + +int vfs_fat_mount(vfs_fat_context_t *ctx, uint32_t base_addr, uint32_t byte_len); + +#endif // MICROPY_INCLUDED_STM32_MBOOT_VFS_H diff --git a/ports/stm32/mboot/diskio.c b/ports/stm32/mboot/vfs_fat.c similarity index 60% rename from ports/stm32/mboot/diskio.c rename to ports/stm32/mboot/vfs_fat.c index 5f68f26a8e669..20994c8b4c00d 100644 --- a/ports/stm32/mboot/diskio.c +++ b/ports/stm32/mboot/vfs_fat.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2019-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,6 +28,7 @@ #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "mboot.h" +#include "vfs.h" #if MBOOT_FSLOAD @@ -38,10 +39,10 @@ #endif DRESULT disk_read(void *pdrv, BYTE *buf, DWORD sector, UINT count) { - fsload_bdev_t *bdev = pdrv; + vfs_fat_context_t *ctx = pdrv; - if (0 <= sector && sector < bdev->byte_len / 512) { - do_read(bdev->base_addr + sector * SECSIZE, count * SECSIZE, buf); + if (0 <= sector && sector < ctx->bdev_byte_len / 512) { + do_read(ctx->bdev_base_addr + sector * SECSIZE, count * SECSIZE, buf); return RES_OK; } @@ -49,14 +50,14 @@ DRESULT disk_read(void *pdrv, BYTE *buf, DWORD sector, UINT count) { } DRESULT disk_ioctl(void *pdrv, BYTE cmd, void *buf) { - fsload_bdev_t *bdev = pdrv; + vfs_fat_context_t *ctx = pdrv; switch (cmd) { case CTRL_SYNC: return RES_OK; case GET_SECTOR_COUNT: - *((DWORD*)buf) = bdev->byte_len / SECSIZE; + *((DWORD*)buf) = ctx->bdev_byte_len / SECSIZE; return RES_OK; case GET_SECTOR_SIZE: @@ -77,4 +78,45 @@ DRESULT disk_ioctl(void *pdrv, BYTE cmd, void *buf) { } } +int vfs_fat_mount(vfs_fat_context_t *ctx, uint32_t base_addr, uint32_t byte_len) { + ctx->bdev_base_addr = base_addr; + ctx->bdev_byte_len = byte_len; + ctx->fatfs.drv = ctx; + FRESULT res = f_mount(&ctx->fatfs); + if (res != FR_OK) { + return -1; + } + return 0; +} + +static int vfs_fat_stream_open(void *stream_in, const char *fname) { + vfs_fat_context_t *stream = stream_in; + FRESULT res = f_open(&stream->fatfs, &stream->fp, fname, FA_READ); + if (res != FR_OK) { + return -1; + } + return 0; +} + +static void vfs_fat_stream_close(void *stream_in) { + vfs_fat_context_t *stream = stream_in; + f_close(&stream->fp); +} + +static int vfs_fat_stream_read(void *stream_in, uint8_t *buf, size_t len) { + vfs_fat_context_t *stream = stream_in; + UINT n; + FRESULT res = f_read(&stream->fp, buf, len, &n); + if (res != FR_OK) { + return -1; + } + return n; +} + +const stream_methods_t vfs_fat_stream_methods = { + vfs_fat_stream_open, + vfs_fat_stream_close, + vfs_fat_stream_read, +}; + #endif // MBOOT_FSLOAD From 67fd58bbd212badff9cd66d0f5f215834844e6aa Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Jun 2020 23:58:26 +1000 Subject: [PATCH 0119/3533] stm32/mboot: Add support for littlefs. Mboot now supports FAT, LFS1 and LFS2 filesystems, to load firmware from. The filesystem needed by the board must be explicitly enabled by the configuration variables MBOOT_VFS_FAT, MBOOT_VFS_LFS1 and MBOOT_VFS_LFS2. Boards that previously used FAT implicitly (with MBOOT_FSLOAD enabled) must now add the following config to mpconfigboard.h: #define MBOOT_VFS_FAT (1) Signed-off-by: Damien George --- .../MIKROE_CLICKER2_STM32/mpconfigboard.h | 1 + ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 1 + ports/stm32/mboot/Makefile | 7 + ports/stm32/mboot/fsload.c | 71 +++++-- ports/stm32/mboot/fwupdate.py | 13 +- ports/stm32/mboot/mboot.h | 8 +- ports/stm32/mboot/vfs.h | 55 +++++- ports/stm32/mboot/vfs_fat.c | 4 +- ports/stm32/mboot/vfs_lfs.c | 177 ++++++++++++++++++ 9 files changed, 309 insertions(+), 28 deletions(-) create mode 100644 ports/stm32/mboot/vfs_lfs.c diff --git a/ports/stm32/boards/MIKROE_CLICKER2_STM32/mpconfigboard.h b/ports/stm32/boards/MIKROE_CLICKER2_STM32/mpconfigboard.h index eb622cd296a84..71af3ce03823d 100644 --- a/ports/stm32/boards/MIKROE_CLICKER2_STM32/mpconfigboard.h +++ b/ports/stm32/boards/MIKROE_CLICKER2_STM32/mpconfigboard.h @@ -83,3 +83,4 @@ #define MBOOT_BOOTPIN_PULL (MP_HAL_PIN_PULL_NONE) #define MBOOT_BOOTPIN_ACTIVE (0) #define MBOOT_FSLOAD (1) +#define MBOOT_VFS_FAT (1) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 757dacf9a7c18..f6e130eb57eaf 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -188,6 +188,7 @@ extern struct _spi_bdev_t spi_bdev2; #define MBOOT_USB_AUTODETECT_PORT (1) #define MBOOT_FSLOAD (1) +#define MBOOT_VFS_FAT (1) #define MBOOT_I2C_PERIPH_ID 1 #define MBOOT_I2C_SCL (pin_B8) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 9ab186412ea93..c901dfb3344a4 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -70,6 +70,8 @@ CFLAGS += -DSTM32_HAL_H='' CFLAGS += -DBOARD_$(BOARD) CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR) CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" +CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT +CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT CFLAGS += -DBUILDING_MBOOT=1 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) @@ -91,6 +93,10 @@ endif $(BUILD)/lib/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN) LIB_SRC_C = \ lib/libc/string0.c \ + lib/littlefs/lfs1.c \ + lib/littlefs/lfs1_util.c \ + lib/littlefs/lfs2.c \ + lib/littlefs/lfs2_util.c \ lib/oofatfs/ff.c \ lib/oofatfs/ffunicode.c \ extmod/uzlib/crc32.c \ @@ -104,6 +110,7 @@ SRC_C = \ fsload.c \ gzstream.c \ vfs_fat.c \ + vfs_lfs.c \ drivers/bus/softspi.c \ drivers/bus/softqspi.c \ drivers/memory/spiflash.c \ diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index dc40a28ea02d4..1e1ad7a04d015 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2019-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,6 +32,10 @@ #if MBOOT_FSLOAD +#if !(MBOOT_VFS_FAT || MBOOT_VFS_LFS1 || MBOOT_VFS_LFS2) +#error Must enable at least one VFS component +#endif + static int fsload_program_file(bool write_to_flash) { // Parse DFU uint8_t buf[512]; @@ -183,27 +187,58 @@ int fsload_process(void) { if (elem[0] == mount_point) { uint32_t base_addr = get_le32(&elem[2]); uint32_t byte_len = get_le32(&elem[6]); + int ret; + union { + #if MBOOT_VFS_FAT + vfs_fat_context_t fat; + #endif + #if MBOOT_VFS_LFS1 + vfs_lfs1_context_t lfs1; + #endif + #if MBOOT_VFS_LFS2 + vfs_lfs2_context_t lfs2; + #endif + } ctx; + const stream_methods_t *methods; + #if MBOOT_VFS_FAT if (elem[1] == ELEM_MOUNT_FAT) { - vfs_fat_context_t ctx; - int ret = vfs_fat_mount(&ctx, base_addr, byte_len); + ret = vfs_fat_mount(&ctx.fat, base_addr, byte_len); + methods = &vfs_fat_stream_methods; + } else + #endif + #if MBOOT_VFS_LFS1 + if (elem[1] == ELEM_MOUNT_LFS1) { + ret = vfs_lfs1_mount(&ctx.lfs1, base_addr, byte_len); + methods = &vfs_lfs1_stream_methods; + } else + #endif + #if MBOOT_VFS_LFS2 + if (elem[1] == ELEM_MOUNT_LFS2) { + ret = vfs_lfs2_mount(&ctx.lfs2, base_addr, byte_len); + methods = &vfs_lfs2_stream_methods; + } else + #endif + { + // Unknown filesystem type + return -1; + } + + if (ret == 0) { + ret = fsload_validate_and_program_file(&ctx, methods, fname); + } + + // Flash LEDs based on success/failure of update + for (int i = 0; i < 4; ++i) { if (ret == 0) { - ret = fsload_validate_and_program_file(&ctx, &vfs_fat_stream_methods, fname); + led_state_all(7); + } else { + led_state_all(1); } - // Flash LEDs based on success/failure of update - for (int i = 0; i < 4; ++i) { - if (ret == 0) { - led_state_all(7); - } else { - led_state_all(1); - } - mp_hal_delay_ms(100); - led_state_all(0); - mp_hal_delay_ms(100); - } - return ret; + mp_hal_delay_ms(100); + led_state_all(0); + mp_hal_delay_ms(100); } - // Unknown filesystem type - return -1; + return ret; } elem += elem[-1]; } diff --git a/ports/stm32/mboot/fwupdate.py b/ports/stm32/mboot/fwupdate.py index b44ed772c3047..dab5fa6632e99 100644 --- a/ports/stm32/mboot/fwupdate.py +++ b/ports/stm32/mboot/fwupdate.py @@ -1,9 +1,13 @@ # Update Mboot or MicroPython from a .dfu.gz file on the board's filesystem -# MIT license; Copyright (c) 2019 Damien P. George +# MIT license; Copyright (c) 2019-2020 Damien P. George import struct, time import uzlib, machine, stm +# Constants to be used with update_mpy +VFS_FAT = 1 +VFS_LFS1 = 2 +VFS_LFS2 = 3 FLASH_KEY1 = 0x45670123 FLASH_KEY2 = 0xCDEF89AB @@ -152,7 +156,7 @@ def update_mboot(filename): print("Programming finished, can now reset or turn off.") -def update_mpy(filename, fs_base, fs_len): +def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT): # Check firmware is of .dfu.gz type try: with open(filename, "rb") as f: @@ -166,11 +170,8 @@ def update_mpy(filename, fs_base, fs_len): ELEM_TYPE_END = 1 ELEM_TYPE_MOUNT = 2 ELEM_TYPE_FSLOAD = 3 - ELEM_MOUNT_FAT = 1 mount_point = 1 - mount = struct.pack( - " #include @@ -42,6 +44,8 @@ enum { enum { ELEM_MOUNT_FAT = 1, + ELEM_MOUNT_LFS1, + ELEM_MOUNT_LFS2, }; extern uint8_t _estack[ELEM_DATA_SIZE]; @@ -55,3 +59,5 @@ int do_write(uint32_t addr, const uint8_t *src8, size_t len); const uint8_t *elem_search(const uint8_t *elem, uint8_t elem_id); int fsload_process(void); + +#endif // MICROPY_INCLUDED_STM32_MBOOT_MBOOT_H diff --git a/ports/stm32/mboot/vfs.h b/ports/stm32/mboot/vfs.h index 9ee1c5ff3cd42..6cf883a13975a 100644 --- a/ports/stm32/mboot/vfs.h +++ b/ports/stm32/mboot/vfs.h @@ -26,8 +26,12 @@ #ifndef MICROPY_INCLUDED_STM32_MBOOT_VFS_H #define MICROPY_INCLUDED_STM32_MBOOT_VFS_H -#include "lib/oofatfs/ff.h" #include "gzstream.h" +#include "mboot.h" + +#if MBOOT_VFS_FAT + +#include "lib/oofatfs/ff.h" typedef struct _vfs_fat_context_t { uint32_t bdev_base_addr; @@ -40,4 +44,53 @@ extern const stream_methods_t vfs_fat_stream_methods; int vfs_fat_mount(vfs_fat_context_t *ctx, uint32_t base_addr, uint32_t byte_len); +#endif + +#if MBOOT_VFS_LFS1 + +#include "lib/littlefs/lfs1.h" + +#define LFS_READ_SIZE (32) +#define LFS_PROG_SIZE (32) +#define LFS_LOOKAHEAD_SIZE (32) + +typedef struct _vfs_lfs1_context_t { + uint32_t bdev_base_addr; + struct lfs1_config config; + lfs1_t lfs; + struct lfs1_file_config filecfg; + uint8_t filebuf[LFS_PROG_SIZE]; + lfs1_file_t file; +} vfs_lfs1_context_t; + +extern const stream_methods_t vfs_lfs1_stream_methods; + +int vfs_lfs1_mount(vfs_lfs1_context_t *ctx, uint32_t base_addr, uint32_t byte_len); + +#endif + +#if MBOOT_VFS_LFS2 + +#include "lib/littlefs/lfs2.h" + +#define LFS_READ_SIZE (32) +#define LFS_PROG_SIZE (32) +#define LFS_CACHE_SIZE (4 * LFS_READ_SIZE) +#define LFS_LOOKAHEAD_SIZE (32) + +typedef struct _vfs_lfs2_context_t { + uint32_t bdev_base_addr; + struct lfs2_config config; + lfs2_t lfs; + struct lfs2_file_config filecfg; + uint8_t filebuf[LFS_CACHE_SIZE]; // lfs2 specific + lfs2_file_t file; +} vfs_lfs2_context_t; + +extern const stream_methods_t vfs_lfs2_stream_methods; + +int vfs_lfs2_mount(vfs_lfs2_context_t *ctx, uint32_t base_addr, uint32_t byte_len); + +#endif + #endif // MICROPY_INCLUDED_STM32_MBOOT_VFS_H diff --git a/ports/stm32/mboot/vfs_fat.c b/ports/stm32/mboot/vfs_fat.c index 20994c8b4c00d..20de074f0dcf0 100644 --- a/ports/stm32/mboot/vfs_fat.c +++ b/ports/stm32/mboot/vfs_fat.c @@ -30,7 +30,7 @@ #include "mboot.h" #include "vfs.h" -#if MBOOT_FSLOAD +#if MBOOT_FSLOAD && MBOOT_VFS_FAT #if FF_MAX_SS == FF_MIN_SS #define SECSIZE (FF_MIN_SS) @@ -119,4 +119,4 @@ const stream_methods_t vfs_fat_stream_methods = { vfs_fat_stream_read, }; -#endif // MBOOT_FSLOAD +#endif // MBOOT_FSLOAD && MBOOT_VFS_FAT diff --git a/ports/stm32/mboot/vfs_lfs.c b/ports/stm32/mboot/vfs_lfs.c new file mode 100644 index 0000000000000..dec7c015fc087 --- /dev/null +++ b/ports/stm32/mboot/vfs_lfs.c @@ -0,0 +1,177 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/mphal.h" +#include "mboot.h" +#include "vfs.h" + +#if MBOOT_FSLOAD && (MBOOT_VFS_LFS1 || MBOOT_VFS_LFS2) + +#if MBOOT_VFS_LFS1 +#if MBOOT_VFS_LFS2 +#error Unsupported +#endif + +#define LFSx_MACRO(s) LFS1##s +#define LFSx_API(x) lfs1_ ## x +#define VFS_LFSx_CONTEXT_T vfs_lfs1_context_t +#define VFS_LFSx_MOUNT vfs_lfs1_mount +#define VFS_LFSx_STREAM_METHODS vfs_lfs1_stream_methods + +#define SUPERBLOCK_MAGIC_OFFSET (40) +#define SUPERBLOCK_BLOCK_SIZE_OFFSET (28) +#define SUPERBLOCK_BLOCK_COUNT_OFFSET (32) + +static uint8_t lfs_read_buffer[LFS_READ_SIZE]; +static uint8_t lfs_prog_buffer[LFS_PROG_SIZE]; +static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE / 8]; + +#else + +#define LFSx_MACRO(s) LFS2##s +#define LFSx_API(x) lfs2_ ## x +#define VFS_LFSx_CONTEXT_T vfs_lfs2_context_t +#define VFS_LFSx_MOUNT vfs_lfs2_mount +#define VFS_LFSx_STREAM_METHODS vfs_lfs2_stream_methods + +#define SUPERBLOCK_MAGIC_OFFSET (8) +#define SUPERBLOCK_BLOCK_SIZE_OFFSET (24) +#define SUPERBLOCK_BLOCK_COUNT_OFFSET (28) + +static uint8_t lfs_read_buffer[LFS_CACHE_SIZE]; +static uint8_t lfs_prog_buffer[LFS_CACHE_SIZE]; +static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE]; + +#endif + +static int dev_read(const struct LFSx_API (config) * c, LFSx_API(block_t) block, LFSx_API(off_t) off, void *buffer, LFSx_API(size_t) size) { + VFS_LFSx_CONTEXT_T *ctx = c->context; + if (0 <= block && block < ctx->config.block_count) { + do_read(ctx->bdev_base_addr + block * ctx->config.block_size + off, size, buffer); + return LFSx_MACRO(_ERR_OK); + } + return LFSx_MACRO(_ERR_IO); +} + +static int dev_prog(const struct LFSx_API (config) * c, LFSx_API(block_t) block, LFSx_API(off_t) off, const void *buffer, LFSx_API(size_t) size) { + return LFSx_MACRO(_ERR_IO); +} + +static int dev_erase(const struct LFSx_API (config) * c, LFSx_API(block_t) block) { + return LFSx_MACRO(_ERR_IO); +} + +static int dev_sync(const struct LFSx_API (config) * c) { + return LFSx_MACRO(_ERR_OK); +} + +int VFS_LFSx_MOUNT(VFS_LFSx_CONTEXT_T *ctx, uint32_t base_addr, uint32_t byte_len) { + // Read start of superblock. + uint8_t buf[48]; + do_read(base_addr, sizeof(buf), buf); + + // Verify littlefs and detect block size. + if (memcmp(&buf[SUPERBLOCK_MAGIC_OFFSET], "littlefs", 8) != 0) { + return -1; + } + uint32_t block_size = get_le32(&buf[SUPERBLOCK_BLOCK_SIZE_OFFSET]); + uint32_t block_count = get_le32(&buf[SUPERBLOCK_BLOCK_COUNT_OFFSET]); + + // Verify size of volume. + if (block_size * block_count != byte_len) { + return -1; + } + + ctx->bdev_base_addr = base_addr; + + struct LFSx_API (config) *config = &ctx->config; + memset(config, 0, sizeof(*config)); + + config->context = ctx; + + config->read = dev_read; + config->prog = dev_prog; + config->erase = dev_erase; + config->sync = dev_sync; + + config->read_size = LFS_READ_SIZE; + config->prog_size = LFS_PROG_SIZE; + config->block_size = block_size; + config->block_count = byte_len / block_size; + + #if MBOOT_VFS_LFS1 + config->lookahead = LFS_LOOKAHEAD_SIZE; + config->read_buffer = lfs_read_buffer; + config->prog_buffer = lfs_prog_buffer; + config->lookahead_buffer = lfs_lookahead_buffer; + #else + config->block_cycles = 100; + config->cache_size = LFS_CACHE_SIZE; + config->lookahead_size = LFS_LOOKAHEAD_SIZE; + config->read_buffer = lfs_read_buffer; + config->prog_buffer = lfs_prog_buffer; + config->lookahead_buffer = lfs_lookahead_buffer; + #endif + + int ret = LFSx_API(mount)(&ctx->lfs, &ctx->config); + if (ret < 0) { + return -1; + } + return 0; +} + +static int vfs_lfs_stream_open(void *stream_in, const char *fname) { + VFS_LFSx_CONTEXT_T *ctx = stream_in; + memset(&ctx->file, 0, sizeof(ctx->file)); + memset(&ctx->filecfg, 0, sizeof(ctx->filecfg)); + ctx->filecfg.buffer = &ctx->filebuf[0]; + LFSx_API(file_opencfg)(&ctx->lfs, &ctx->file, fname, LFSx_MACRO(_O_RDONLY), &ctx->filecfg); + return 0; +} + +static void vfs_lfs_stream_close(void *stream_in) { + VFS_LFSx_CONTEXT_T *ctx = stream_in; + LFSx_API(file_close)(&ctx->lfs, &ctx->file); +} + +static int vfs_lfs_stream_read(void *stream_in, uint8_t *buf, size_t len) { + VFS_LFSx_CONTEXT_T *ctx = stream_in; + LFSx_API(ssize_t) sz = LFSx_API(file_read)(&ctx->lfs, &ctx->file, buf, len); + if (sz < 0) { + return -1; + } + return sz; +} + +const stream_methods_t VFS_LFSx_STREAM_METHODS = { + vfs_lfs_stream_open, + vfs_lfs_stream_close, + vfs_lfs_stream_read, +}; + +#endif // MBOOT_FSLOAD && (MBOOT_VFS_LFS1 || MBOOT_VFS_LFS2) From 0a8ce0d56899b02a18ee4260446d31e732fe71e8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Jun 2020 21:26:06 +1000 Subject: [PATCH 0120/3533] stm32/mboot: Update README to describe WB and littlefs support. Signed-off-by: Damien George --- ports/stm32/mboot/README.md | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ports/stm32/mboot/README.md b/ports/stm32/mboot/README.md index 52bbf55675414..d8aa6d456f19e 100644 --- a/ports/stm32/mboot/README.md +++ b/ports/stm32/mboot/README.md @@ -2,11 +2,11 @@ Mboot - MicroPython boot loader =============================== Mboot is a custom bootloader for STM32 MCUs, and currently supports the -STM32F4xx and STM32F7xx families. It can provide a standard USB DFU interface -on either the FS or HS peripherals, as well as a sophisticated, custom I2C +STM32F4xx, STM32F7xx and STM32WBxx families. It can provide a standard USB DFU +interface on either the FS or HS peripherals, as well as a sophisticated, custom I2C interface. It can also load and program firmware in .dfu.gz format from a -filesystem. It can fit in 16k of flash space, but all features enabled requires -32k. +filesystem, either FAT, littlefs 1 or littlfs 2. +It can fit in 16k of flash space, but all features enabled requires 32k. How to use ---------- @@ -63,6 +63,15 @@ How to use #define MBOOT_FSLOAD (1) + and then enable one or more of the following depending on what filesystem + support is required in Mboot (note that the FAT driver is read-only and + quite compact, but littlefs supports both read and write so is rather + large): + + #define MBOOT_VFS_FAT (1) + #define MBOOT_VFS_LFS1 (1) + #define MBOOT_VFS_LFS2 (1) + 2. Build the board's main application firmware as usual. 3. Build mboot via: @@ -133,10 +142,18 @@ are located and what filename to program. The elements to use are: `u32` means unsigned 32-bit little-endian integer. The firmware to load must be a gzip'd DfuSe file (.dfu.gz) and stored within a -FAT formatted partition. +FAT or littlefs formatted partition. The provided fwupdate.py script contains helper functions to call into Mboot -with the correct data, and also to update Mboot itself. +with the correct data, and also to update Mboot itself. For example on PYBD +the following will update the main MicroPython firmware from the file +firmware.dfu.gz stored on the default FAT filesystem: + + import fwupdate + fwupdate.update_mpy('firmware.dfu.gz', 0x80000000, 2 * 1024 * 1024) + +The 0x80000000 value is the address understood by Mboot as the location of +the external SPI flash, configured via `MBOOT_SPIFLASH_ADDR`. Example: Mboot on PYBv1.x ------------------------- From 6475cdb7d06cc1a7bf273e66d0bbfc9aef890622 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Jun 2020 21:29:32 +1000 Subject: [PATCH 0121/3533] travis: Build mboot for PYBV10 with LFS2 enabled in stm32 job. Signed-off-by: Damien George --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b7aac0bd0d0fc..f6fe3e1e4e39f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -78,6 +78,7 @@ jobs: - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L073RZ - make ${MAKEOPTS} -C ports/stm32 BOARD=STM32L476DISC - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_WB55 + - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBD_SF6 - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=NUCLEO_WB55 From 137df817575e06b7bd765fb230a99d108f1d4f61 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Jun 2020 23:56:45 +1000 Subject: [PATCH 0122/3533] stm32/i2cslave: Pass I2C instance to callbacks to support multi I2Cs. By passing through the I2C instance to the application callbacks, the application can implement multiple I2C slave devices on different peripherals (eg I2C1 and I2C2). This commit also adds a proper rw argument to i2c_slave_process_addr_match for F7/H7/WB MCUs, and enables the i2c_slave_process_tx_end callback. Mboot is also updated for these changes. Signed-off-by: Damien George --- ports/stm32/i2cslave.c | 18 +++++++++--------- ports/stm32/i2cslave.h | 9 +++++---- ports/stm32/mboot/main.c | 11 +++++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/ports/stm32/i2cslave.c b/ports/stm32/i2cslave.c index 149e105ee8207..a575c53085168 100644 --- a/ports/stm32/i2cslave.c +++ b/ports/stm32/i2cslave.c @@ -42,20 +42,20 @@ void i2c_slave_ev_irq_handler(i2c_slave_t *i2c) { // Read of SR1, SR2 needed to clear ADDR bit sr1 = i2c->SR1; uint32_t sr2 = i2c->SR2; - i2c_slave_process_addr_match((sr2 >> I2C_SR2_TRA_Pos) & 1); + i2c_slave_process_addr_match(i2c, (sr2 >> I2C_SR2_TRA_Pos) & 1); } if (sr1 & I2C_SR1_TXE) { - i2c->DR = i2c_slave_process_tx_byte(); + i2c->DR = i2c_slave_process_tx_byte(i2c); } if (sr1 & I2C_SR1_RXNE) { - i2c_slave_process_rx_byte(i2c->DR); + i2c_slave_process_rx_byte(i2c, i2c->DR); } if (sr1 & I2C_SR1_STOPF) { // STOPF only set at end of RX mode (in TX mode AF is set on NACK) // Read of SR1, write CR1 needed to clear STOPF bit sr1 = i2c->SR1; i2c->CR1 &= ~I2C_CR1_ACK; - i2c_slave_process_rx_end(); + i2c_slave_process_rx_end(i2c); i2c->CR1 |= I2C_CR1_ACK; } } @@ -77,22 +77,22 @@ void i2c_slave_ev_irq_handler(i2c_slave_t *i2c) { // Set TXE so that TXDR is flushed and ready for the first byte i2c->ISR = I2C_ISR_TXE; i2c->ICR = I2C_ICR_ADDRCF; - i2c_slave_process_addr_match(0); + i2c_slave_process_addr_match(i2c, (i2c->ISR >> I2C_ISR_DIR_Pos) & 1); } if (isr & I2C_ISR_TXIS) { - i2c->TXDR = i2c_slave_process_tx_byte(); + i2c->TXDR = i2c_slave_process_tx_byte(i2c); } if (isr & I2C_ISR_RXNE) { - i2c_slave_process_rx_byte(i2c->RXDR); + i2c_slave_process_rx_byte(i2c, i2c->RXDR); } if (isr & I2C_ISR_STOPF) { // STOPF only set for STOP condition, not a repeated START i2c->ICR = I2C_ICR_STOPCF; i2c->OAR1 &= ~I2C_OAR1_OA1EN; if (i2c->ISR & I2C_ISR_DIR) { - // i2c_slave_process_tx_end(); + i2c_slave_process_tx_end(i2c); } else { - i2c_slave_process_rx_end(); + i2c_slave_process_rx_end(i2c); } i2c->OAR1 |= I2C_OAR1_OA1EN; } diff --git a/ports/stm32/i2cslave.h b/ports/stm32/i2cslave.h index f335c9291226c..4a51bf378e924 100644 --- a/ports/stm32/i2cslave.h +++ b/ports/stm32/i2cslave.h @@ -67,9 +67,10 @@ static inline void i2c_slave_shutdown(i2c_slave_t *i2c, int irqn) { void i2c_slave_ev_irq_handler(i2c_slave_t *i2c); // These should be provided externally -int i2c_slave_process_addr_match(int rw); -int i2c_slave_process_rx_byte(uint8_t val); -void i2c_slave_process_rx_end(void); -uint8_t i2c_slave_process_tx_byte(void); +int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw); +int i2c_slave_process_rx_byte(i2c_slave_t *i2c, uint8_t val); +void i2c_slave_process_rx_end(i2c_slave_t *i2c); +uint8_t i2c_slave_process_tx_byte(i2c_slave_t *i2c); +void i2c_slave_process_tx_end(i2c_slave_t *i2c); #endif // MICROPY_INCLUDED_STM32_I2CSLAVE_H diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index b3e63ccc26626..87618e7f410ad 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -664,7 +664,7 @@ void i2c_init(int addr) { i2c_slave_init(MBOOT_I2Cx, I2Cx_EV_IRQn, IRQ_PRI_I2C, addr); } -int i2c_slave_process_addr_match(int rw) { +int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw) { if (i2c_obj.cmd_arg_sent) { i2c_obj.cmd_send_arg = false; } @@ -672,14 +672,14 @@ int i2c_slave_process_addr_match(int rw) { return 0; // ACK } -int i2c_slave_process_rx_byte(uint8_t val) { +int i2c_slave_process_rx_byte(i2c_slave_t *i2c, uint8_t val) { if (i2c_obj.cmd_buf_pos < sizeof(i2c_obj.cmd_buf)) { i2c_obj.cmd_buf[i2c_obj.cmd_buf_pos++] = val; } return 0; // ACK } -void i2c_slave_process_rx_end(void) { +void i2c_slave_process_rx_end(i2c_slave_t *i2c) { if (i2c_obj.cmd_buf_pos == 0) { return; } @@ -764,7 +764,7 @@ void i2c_slave_process_rx_end(void) { i2c_obj.cmd_arg_sent = false; } -uint8_t i2c_slave_process_tx_byte(void) { +uint8_t i2c_slave_process_tx_byte(i2c_slave_t *i2c) { if (i2c_obj.cmd_send_arg) { i2c_obj.cmd_arg_sent = true; return i2c_obj.cmd_arg; @@ -775,6 +775,9 @@ uint8_t i2c_slave_process_tx_byte(void) { } } +void i2c_slave_process_tx_end(i2c_slave_t *i2c) { +} + #endif // defined(MBOOT_I2C_SCL) /******************************************************************************/ From aa26fe62d8728c95d447475ced9b4a03380025ea Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 Jun 2020 13:09:07 +1000 Subject: [PATCH 0123/3533] py/asm: Add funcs/macros to emit machine code for logical-shift-right. Signed-off-by: Damien George --- py/asmarm.c | 5 +++++ py/asmarm.h | 2 ++ py/asmthumb.h | 1 + py/asmx64.c | 5 +++++ py/asmx64.h | 2 ++ py/asmx86.c | 5 +++++ py/asmx86.h | 2 ++ py/asmxtensa.h | 9 +++++++++ 8 files changed, 31 insertions(+) diff --git a/py/asmarm.c b/py/asmarm.c index 72b37f73a02f5..e91421578b274 100644 --- a/py/asmarm.c +++ b/py/asmarm.c @@ -303,6 +303,11 @@ void asm_arm_lsl_reg_reg(asm_arm_t *as, uint rd, uint rs) { emit_al(as, 0x1a00010 | (rd << 12) | (rs << 8) | rd); } +void asm_arm_lsr_reg_reg(asm_arm_t *as, uint rd, uint rs) { + // mov rd, rd, lsr rs + emit_al(as, 0x1a00030 | (rd << 12) | (rs << 8) | rd); +} + void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs) { // mov rd, rd, asr rs emit_al(as, 0x1a00050 | (rd << 12) | (rs << 8) | rd); diff --git a/py/asmarm.h b/py/asmarm.h index 825fd884005ff..46da661faa9ba 100644 --- a/py/asmarm.h +++ b/py/asmarm.h @@ -101,6 +101,7 @@ void asm_arm_orr_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm); void asm_arm_mov_reg_local_addr(asm_arm_t *as, uint rd, int local_num); void asm_arm_mov_reg_pcrel(asm_arm_t *as, uint reg_dest, uint label); void asm_arm_lsl_reg_reg(asm_arm_t *as, uint rd, uint rs); +void asm_arm_lsr_reg_reg(asm_arm_t *as, uint rd, uint rs); void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs); // memory @@ -187,6 +188,7 @@ void asm_arm_bx_reg(asm_arm_t *as, uint reg_src); #define ASM_MOV_REG_PCREL(as, reg_dest, label) asm_arm_mov_reg_pcrel((as), (reg_dest), (label)) #define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift)) +#define ASM_LSR_REG_REG(as, reg_dest, reg_shift) asm_arm_lsr_reg_reg((as), (reg_dest), (reg_shift)) #define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_arm_asr_reg_reg((as), (reg_dest), (reg_shift)) #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src)) #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src)) diff --git a/py/asmthumb.h b/py/asmthumb.h index 6d0ee4b76e50e..9e6f242ccaeb3 100644 --- a/py/asmthumb.h +++ b/py/asmthumb.h @@ -345,6 +345,7 @@ void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp); // convenien #define ASM_MOV_REG_PCREL(as, rlo_dest, label) asm_thumb_mov_reg_pcrel((as), (rlo_dest), (label)) #define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift)) +#define ASM_LSR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSR, (reg_dest), (reg_shift)) #define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ASR, (reg_dest), (reg_shift)) #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src)) #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src)) diff --git a/py/asmx64.c b/py/asmx64.c index 723671d5a3558..fd64eaf98b1c1 100644 --- a/py/asmx64.c +++ b/py/asmx64.c @@ -67,6 +67,7 @@ // #define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */ // #define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */ #define OPCODE_SHL_RM64_CL (0xd3) /* /4 */ +#define OPCODE_SHR_RM64_CL (0xd3) /* /5 */ #define OPCODE_SAR_RM64_CL (0xd3) /* /7 */ // #define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */ // #define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */ @@ -382,6 +383,10 @@ void asm_x64_shl_r64_cl(asm_x64_t *as, int dest_r64) { asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL); } +void asm_x64_shr_r64_cl(asm_x64_t *as, int dest_r64) { + asm_x64_generic_r64_r64(as, dest_r64, 5, OPCODE_SHR_RM64_CL); +} + void asm_x64_sar_r64_cl(asm_x64_t *as, int dest_r64) { asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL); } diff --git a/py/asmx64.h b/py/asmx64.h index 28b1bd255fd21..e73e3c5c57b87 100644 --- a/py/asmx64.h +++ b/py/asmx64.h @@ -98,6 +98,7 @@ void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64); void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64); void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64); void asm_x64_shl_r64_cl(asm_x64_t *as, int dest_r64); +void asm_x64_shr_r64_cl(asm_x64_t *as, int dest_r64); void asm_x64_sar_r64_cl(asm_x64_t *as, int dest_r64); void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64); void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64); @@ -190,6 +191,7 @@ void asm_x64_call_ind(asm_x64_t *as, size_t fun_id, int temp_r32); #define ASM_MOV_REG_PCREL(as, reg_dest, label) asm_x64_mov_reg_pcrel((as), (reg_dest), (label)) #define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg)) +#define ASM_LSR_REG(as, reg) asm_x64_shr_r64_cl((as), (reg)) #define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg)) #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src)) #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src)) diff --git a/py/asmx86.c b/py/asmx86.c index 96de372aec0c2..4b0f8047f6eb7 100644 --- a/py/asmx86.c +++ b/py/asmx86.c @@ -67,6 +67,7 @@ // #define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */ // #define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */ #define OPCODE_SHL_RM32_CL (0xd3) /* /4 */ +#define OPCODE_SHR_RM32_CL (0xd3) /* /5 */ #define OPCODE_SAR_RM32_CL (0xd3) /* /7 */ // #define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */ // #define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */ @@ -259,6 +260,10 @@ void asm_x86_shl_r32_cl(asm_x86_t *as, int dest_r32) { asm_x86_generic_r32_r32(as, dest_r32, 4, OPCODE_SHL_RM32_CL); } +void asm_x86_shr_r32_cl(asm_x86_t *as, int dest_r32) { + asm_x86_generic_r32_r32(as, dest_r32, 5, OPCODE_SHR_RM32_CL); +} + void asm_x86_sar_r32_cl(asm_x86_t *as, int dest_r32) { asm_x86_generic_r32_r32(as, dest_r32, 7, OPCODE_SAR_RM32_CL); } diff --git a/py/asmx86.h b/py/asmx86.h index 4855cd7ee4e8a..f28040abf444c 100644 --- a/py/asmx86.h +++ b/py/asmx86.h @@ -93,6 +93,7 @@ void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); void asm_x86_shl_r32_cl(asm_x86_t *as, int dest_r32); +void asm_x86_shr_r32_cl(asm_x86_t *as, int dest_r32); void asm_x86_sar_r32_cl(asm_x86_t *as, int dest_r32); void asm_x86_add_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); void asm_x86_sub_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); @@ -185,6 +186,7 @@ void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r #define ASM_MOV_REG_PCREL(as, reg_dest, label) asm_x86_mov_reg_pcrel((as), (reg_dest), (label)) #define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg)) +#define ASM_LSR_REG(as, reg) asm_x86_shr_r32_cl((as), (reg)) #define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg)) #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src)) #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src)) diff --git a/py/asmxtensa.h b/py/asmxtensa.h index 5eb40daf7887c..43f1b608edd1c 100644 --- a/py/asmxtensa.h +++ b/py/asmxtensa.h @@ -243,6 +243,10 @@ static inline void asm_xtensa_op_sll(asm_xtensa_t *as, uint reg_dest, uint reg_s asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 1, 10, reg_dest, reg_src, 0)); } +static inline void asm_xtensa_op_srl(asm_xtensa_t *as, uint reg_dest, uint reg_src) { + asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 1, 9, reg_dest, 0, reg_src)); +} + static inline void asm_xtensa_op_sra(asm_xtensa_t *as, uint reg_dest, uint reg_src) { asm_xtensa_op24(as, ASM_XTENSA_ENCODE_RRR(0, 1, 11, reg_dest, 0, reg_src)); } @@ -372,6 +376,11 @@ void asm_xtensa_call_ind_win(asm_xtensa_t *as, uint idx); asm_xtensa_op_ssl((as), (reg_shift)); \ asm_xtensa_op_sll((as), (reg_dest), (reg_dest)); \ } while (0) +#define ASM_LSR_REG_REG(as, reg_dest, reg_shift) \ + do { \ + asm_xtensa_op_ssr((as), (reg_shift)); \ + asm_xtensa_op_srl((as), (reg_dest), (reg_dest)); \ + } while (0) #define ASM_ASR_REG_REG(as, reg_dest, reg_shift) \ do { \ asm_xtensa_op_ssr((as), (reg_shift)); \ From b3b8706d27cffbfc4cdd447b204ae7083283d13c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Jun 2020 18:26:01 +1000 Subject: [PATCH 0124/3533] py/asm: Add condition codes for signed comparisons. Signed-off-by: Damien George --- py/asmthumb.h | 7 +++++++ py/asmx64.h | 3 +++ py/asmx86.h | 3 +++ 3 files changed, 13 insertions(+) diff --git a/py/asmthumb.h b/py/asmthumb.h index 9e6f242ccaeb3..17b694a74dfdd 100644 --- a/py/asmthumb.h +++ b/py/asmthumb.h @@ -80,12 +80,19 @@ void asm_thumb_exit(asm_thumb_t *as); #define ASM_THUMB_OP_IT (0xbf00) #define ASM_THUMB_OP_ITE_EQ (0xbf0c) +#define ASM_THUMB_OP_ITE_NE (0xbf14) #define ASM_THUMB_OP_ITE_CS (0xbf2c) +#define ASM_THUMB_OP_ITE_CC (0xbf34) #define ASM_THUMB_OP_ITE_MI (0xbf4c) +#define ASM_THUMB_OP_ITE_PL (0xbf54) #define ASM_THUMB_OP_ITE_VS (0xbf6c) +#define ASM_THUMB_OP_ITE_VC (0xbf74) #define ASM_THUMB_OP_ITE_HI (0xbf8c) +#define ASM_THUMB_OP_ITE_LS (0xbf94) #define ASM_THUMB_OP_ITE_GE (0xbfac) +#define ASM_THUMB_OP_ITE_LT (0xbfb4) #define ASM_THUMB_OP_ITE_GT (0xbfcc) +#define ASM_THUMB_OP_ITE_LE (0xbfd4) #define ASM_THUMB_OP_NOP (0xbf00) #define ASM_THUMB_OP_WFI (0xbf30) diff --git a/py/asmx64.h b/py/asmx64.h index e73e3c5c57b87..1a4987f5cbbaf 100644 --- a/py/asmx64.h +++ b/py/asmx64.h @@ -61,10 +61,13 @@ // condition codes, used for jcc and setcc (despite their j-name!) #define ASM_X64_CC_JB (0x2) // below, unsigned +#define ASM_X64_CC_JAE (0x3) // above or equal, unsigned #define ASM_X64_CC_JZ (0x4) #define ASM_X64_CC_JE (0x4) #define ASM_X64_CC_JNZ (0x5) #define ASM_X64_CC_JNE (0x5) +#define ASM_X64_CC_JBE (0x6) // below or equal, unsigned +#define ASM_X64_CC_JA (0x7) // above, unsigned #define ASM_X64_CC_JL (0xc) // less, signed #define ASM_X64_CC_JGE (0xd) // greater or equal, signed #define ASM_X64_CC_JLE (0xe) // less or equal, signed diff --git a/py/asmx86.h b/py/asmx86.h index f28040abf444c..8f1b06d220e83 100644 --- a/py/asmx86.h +++ b/py/asmx86.h @@ -63,10 +63,13 @@ // condition codes, used for jcc and setcc (despite their j-name!) #define ASM_X86_CC_JB (0x2) // below, unsigned +#define ASM_X86_CC_JAE (0x3) // above or equal, unsigned #define ASM_X86_CC_JZ (0x4) #define ASM_X86_CC_JE (0x4) #define ASM_X86_CC_JNZ (0x5) #define ASM_X86_CC_JNE (0x5) +#define ASM_X86_CC_JBE (0x6) // below or equal, unsigned +#define ASM_X86_CC_JA (0x7) // above, unsigned #define ASM_X86_CC_JL (0xc) // less, signed #define ASM_X86_CC_JGE (0xd) // greater or equal, signed #define ASM_X86_CC_JLE (0xe) // less or equal, signed From 41fa8b5482089bdd7fa5478fe24f32913b23967c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Jun 2020 18:26:39 +1000 Subject: [PATCH 0125/3533] py/emitnative: Implement binary operations for viper uint operands. uint types in viper mode can now be used for all binary operators except floor-divide and modulo. Fixes issue #1847 and issue #6177. Signed-off-by: Damien George --- py/emitnative.c | 113 +++++++++++++----- tests/micropython/viper_binop_arith_uint.py | 32 +++++ .../micropython/viper_binop_arith_uint.py.exp | 10 ++ tests/micropython/viper_binop_bitwise_uint.py | 58 +++++++++ .../viper_binop_bitwise_uint.py.exp | 22 ++++ tests/micropython/viper_binop_comp_uint.py | 31 +++++ .../micropython/viper_binop_comp_uint.py.exp | 6 + tests/micropython/viper_error.py | 3 + tests/micropython/viper_error.py.exp | 3 + 9 files changed, 251 insertions(+), 27 deletions(-) create mode 100644 tests/micropython/viper_binop_arith_uint.py create mode 100644 tests/micropython/viper_binop_arith_uint.py.exp create mode 100644 tests/micropython/viper_binop_bitwise_uint.py create mode 100644 tests/micropython/viper_binop_bitwise_uint.py.exp create mode 100644 tests/micropython/viper_binop_comp_uint.py create mode 100644 tests/micropython/viper_binop_comp_uint.py.exp diff --git a/py/emitnative.c b/py/emitnative.c index 6c8e9feebaee9..2a657b6964daa 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -2289,7 +2289,8 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { DEBUG_printf("binary_op(" UINT_FMT ")\n", op); vtype_kind_t vtype_lhs = peek_vtype(emit, 1); vtype_kind_t vtype_rhs = peek_vtype(emit, 0); - if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) { + if ((vtype_lhs == VTYPE_INT || vtype_lhs == VTYPE_UINT) + && (vtype_rhs == VTYPE_INT || vtype_rhs == VTYPE_UINT)) { // for integers, inplace and normal ops are equivalent, so use just normal ops if (MP_BINARY_OP_INPLACE_OR <= op && op <= MP_BINARY_OP_INPLACE_POWER) { op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR; @@ -2306,9 +2307,13 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { if (op == MP_BINARY_OP_LSHIFT) { ASM_LSL_REG(emit->as, REG_RET); } else { - ASM_ASR_REG(emit->as, REG_RET); + if (vtype_lhs == VTYPE_UINT) { + ASM_LSR_REG(emit->as, REG_RET); + } else { + ASM_ASR_REG(emit->as, REG_RET); + } } - emit_post_push_reg(emit, VTYPE_INT, REG_RET); + emit_post_push_reg(emit, vtype_lhs, REG_RET); return; } #endif @@ -2316,6 +2321,10 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { // special cases for floor-divide and module because we dispatch to helper functions if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_MODULO) { emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1); + if (vtype_lhs != VTYPE_INT) { + EMIT_NATIVE_VIPER_TYPE_ERROR(emit, + MP_ERROR_TEXT("div/mod not implemented for uint"), mp_binary_op_method_name[op]); + } if (op == MP_BINARY_OP_FLOOR_DIVIDE) { emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE); } else { @@ -2334,31 +2343,35 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { if (op == MP_BINARY_OP_LSHIFT) { ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs); } else { - ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs); + if (vtype_lhs == VTYPE_UINT) { + ASM_LSR_REG_REG(emit->as, REG_ARG_2, reg_rhs); + } else { + ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs); + } } - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); return; } #endif if (op == MP_BINARY_OP_OR) { ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (op == MP_BINARY_OP_XOR) { ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (op == MP_BINARY_OP_AND) { ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (op == MP_BINARY_OP_ADD) { ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (op == MP_BINARY_OP_SUBTRACT) { ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (op == MP_BINARY_OP_MULTIPLY) { ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs); - emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); + emit_post_push_reg(emit, vtype_lhs, REG_ARG_2); } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) { // comparison ops are (in enum order): // MP_BINARY_OP_LESS @@ -2367,11 +2380,26 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { // MP_BINARY_OP_LESS_EQUAL // MP_BINARY_OP_MORE_EQUAL // MP_BINARY_OP_NOT_EQUAL + + if (vtype_lhs != vtype_rhs) { + EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("comparison of int and uint")); + } + + size_t op_idx = op - MP_BINARY_OP_LESS + (vtype_lhs == VTYPE_UINT ? 0 : 6); + need_reg_single(emit, REG_RET, 0); #if N_X64 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET); asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2); - static byte ops[6] = { + static byte ops[6 + 6] = { + // unsigned + ASM_X64_CC_JB, + ASM_X64_CC_JA, + ASM_X64_CC_JE, + ASM_X64_CC_JBE, + ASM_X64_CC_JAE, + ASM_X64_CC_JNE, + // signed ASM_X64_CC_JL, ASM_X64_CC_JG, ASM_X64_CC_JE, @@ -2379,11 +2407,19 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { ASM_X64_CC_JGE, ASM_X64_CC_JNE, }; - asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET); + asm_x64_setcc_r8(emit->as, ops[op_idx], REG_RET); #elif N_X86 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET); asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2); - static byte ops[6] = { + static byte ops[6 + 6] = { + // unsigned + ASM_X86_CC_JB, + ASM_X86_CC_JA, + ASM_X86_CC_JE, + ASM_X86_CC_JBE, + ASM_X86_CC_JAE, + ASM_X86_CC_JNE, + // signed ASM_X86_CC_JL, ASM_X86_CC_JG, ASM_X86_CC_JE, @@ -2391,24 +2427,39 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { ASM_X86_CC_JGE, ASM_X86_CC_JNE, }; - asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET); + asm_x86_setcc_r8(emit->as, ops[op_idx], REG_RET); #elif N_THUMB asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs); - static uint16_t ops[6] = { - ASM_THUMB_OP_ITE_GE, - ASM_THUMB_OP_ITE_GT, + static uint16_t ops[6 + 6] = { + // unsigned + ASM_THUMB_OP_ITE_CC, + ASM_THUMB_OP_ITE_HI, ASM_THUMB_OP_ITE_EQ, + ASM_THUMB_OP_ITE_LS, + ASM_THUMB_OP_ITE_CS, + ASM_THUMB_OP_ITE_NE, + // signed + ASM_THUMB_OP_ITE_LT, ASM_THUMB_OP_ITE_GT, - ASM_THUMB_OP_ITE_GE, ASM_THUMB_OP_ITE_EQ, + ASM_THUMB_OP_ITE_LE, + ASM_THUMB_OP_ITE_GE, + ASM_THUMB_OP_ITE_NE, }; - static byte ret[6] = { 0, 1, 1, 0, 1, 0, }; - asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]); - asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]); - asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1); + asm_thumb_op16(emit->as, ops[op_idx]); + asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); + asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); #elif N_ARM asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs); - static uint ccs[6] = { + static uint ccs[6 + 6] = { + // unsigned + ASM_ARM_CC_CC, + ASM_ARM_CC_HI, + ASM_ARM_CC_EQ, + ASM_ARM_CC_LS, + ASM_ARM_CC_CS, + ASM_ARM_CC_NE, + // signed ASM_ARM_CC_LT, ASM_ARM_CC_GT, ASM_ARM_CC_EQ, @@ -2416,9 +2467,17 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { ASM_ARM_CC_GE, ASM_ARM_CC_NE, }; - asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]); + asm_arm_setcc_reg(emit->as, REG_RET, ccs[op_idx]); #elif N_XTENSA || N_XTENSAWIN - static uint8_t ccs[6] = { + static uint8_t ccs[6 + 6] = { + // unsigned + ASM_XTENSA_CC_LTU, + 0x80 | ASM_XTENSA_CC_LTU, // for GTU we'll swap args + ASM_XTENSA_CC_EQ, + 0x80 | ASM_XTENSA_CC_GEU, // for LEU we'll swap args + ASM_XTENSA_CC_GEU, + ASM_XTENSA_CC_NE, + // signed ASM_XTENSA_CC_LT, 0x80 | ASM_XTENSA_CC_LT, // for GT we'll swap args ASM_XTENSA_CC_EQ, @@ -2426,7 +2485,7 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { ASM_XTENSA_CC_GE, ASM_XTENSA_CC_NE, }; - uint8_t cc = ccs[op - MP_BINARY_OP_LESS]; + uint8_t cc = ccs[op_idx]; if ((cc & 0x80) == 0) { asm_xtensa_setcc_reg_reg_reg(emit->as, cc, REG_RET, REG_ARG_2, reg_rhs); } else { diff --git a/tests/micropython/viper_binop_arith_uint.py b/tests/micropython/viper_binop_arith_uint.py new file mode 100644 index 0000000000000..e4270a10a79a9 --- /dev/null +++ b/tests/micropython/viper_binop_arith_uint.py @@ -0,0 +1,32 @@ +# test arithmetic operators with uint type + + +@micropython.viper +def add(x: uint, y: uint): + return x + y, y + x + + +print("add") +print(*add(1, 2)) +print(*(x & 0xFFFFFFFF for x in add(-1, -2))) + + +@micropython.viper +def sub(x: uint, y: uint): + return x - y, y - x + + +print("sub") +print(*(x & 0xFFFFFFFF for x in sub(1, 2))) +print(*(x & 0xFFFFFFFF for x in sub(-1, -2))) + + +@micropython.viper +def mul(x: uint, y: uint): + return x * y, y * x + + +print("mul") +print(*mul(2, 3)) +print(*(x & 0xFFFFFFFF for x in mul(2, -3))) +print(*mul(-2, -3)) diff --git a/tests/micropython/viper_binop_arith_uint.py.exp b/tests/micropython/viper_binop_arith_uint.py.exp new file mode 100644 index 0000000000000..72f84b716a4dd --- /dev/null +++ b/tests/micropython/viper_binop_arith_uint.py.exp @@ -0,0 +1,10 @@ +add +3 3 +4294967293 4294967293 +sub +4294967295 1 +1 4294967295 +mul +6 6 +4294967290 4294967290 +6 6 diff --git a/tests/micropython/viper_binop_bitwise_uint.py b/tests/micropython/viper_binop_bitwise_uint.py new file mode 100644 index 0000000000000..3bc7ba8d1113f --- /dev/null +++ b/tests/micropython/viper_binop_bitwise_uint.py @@ -0,0 +1,58 @@ +# test bitwise operators on uint type + + +@micropython.viper +def shl(x: uint, y: uint) -> uint: + return x << y + + +print("shl") +print(shl(1, 0)) +print(shl(1, 30)) +print(shl(-1, 10) & 0xFFFFFFFF) + + +@micropython.viper +def shr(x: uint, y: uint) -> uint: + return x >> y + + +print("shr") +print(shr(1, 0)) +print(shr(16, 3)) +print(shr(-1, 1) in (0x7FFFFFFF, 0x7FFFFFFF_FFFFFFFF)) + + +@micropython.viper +def and_(x: uint, y: uint): + return x & y, y & x + + +print("and") +print(*and_(1, 0)) +print(*and_(1, 3)) +print(*and_(-1, 2)) +print(*(x & 0xFFFFFFFF for x in and_(-1, -2))) + + +@micropython.viper +def or_(x: uint, y: uint): + return x | y, y | x + + +print("or") +print(*or_(1, 0)) +print(*or_(1, 2)) +print(*(x & 0xFFFFFFFF for x in or_(-1, 2))) + + +@micropython.viper +def xor(x: uint, y: uint): + return x ^ y, y ^ x + + +print("xor") +print(*xor(1, 0)) +print(*xor(1, 3)) +print(*(x & 0xFFFFFFFF for x in xor(-1, 3))) +print(*xor(-1, -3)) diff --git a/tests/micropython/viper_binop_bitwise_uint.py.exp b/tests/micropython/viper_binop_bitwise_uint.py.exp new file mode 100644 index 0000000000000..3ad6469a2fc78 --- /dev/null +++ b/tests/micropython/viper_binop_bitwise_uint.py.exp @@ -0,0 +1,22 @@ +shl +1 +1073741824 +4294966272 +shr +1 +2 +True +and +0 0 +1 1 +2 2 +4294967294 4294967294 +or +1 1 +3 3 +4294967295 4294967295 +xor +1 1 +2 2 +4294967292 4294967292 +2 2 diff --git a/tests/micropython/viper_binop_comp_uint.py b/tests/micropython/viper_binop_comp_uint.py new file mode 100644 index 0000000000000..85aa32c78c733 --- /dev/null +++ b/tests/micropython/viper_binop_comp_uint.py @@ -0,0 +1,31 @@ +# test comparison operators with uint type + + +@micropython.viper +def f(x: uint, y: uint): + if x < y: + print(" <", end="") + if x > y: + print(" >", end="") + if x == y: + print(" ==", end="") + if x <= y: + print(" <=", end="") + if x >= y: + print(" >=", end="") + if x != y: + print(" !=", end="") + + +def test(a, b): + print(a, b, end="") + f(a, b) + print() + + +test(1, 1) +test(2, 1) +test(1, 2) +test(2, -1) +test(-2, 1) +test(-2, -1) diff --git a/tests/micropython/viper_binop_comp_uint.py.exp b/tests/micropython/viper_binop_comp_uint.py.exp new file mode 100644 index 0000000000000..cacce62b6e380 --- /dev/null +++ b/tests/micropython/viper_binop_comp_uint.py.exp @@ -0,0 +1,6 @@ +1 1 == <= >= +2 1 > >= != +1 2 < <= != +2 -1 < <= != +-2 1 > >= != +-2 -1 < <= != diff --git a/tests/micropython/viper_error.py b/tests/micropython/viper_error.py index 790f3d75c4f0e..80617af0c1f29 100644 --- a/tests/micropython/viper_error.py +++ b/tests/micropython/viper_error.py @@ -52,6 +52,7 @@ def f(): # can't do binary op between incompatible types test("@micropython.viper\ndef f(): 1 + []") +test("@micropython.viper\ndef f(x:int, y:uint): x < y") # can't load test("@micropython.viper\ndef f(): 1[0]") @@ -73,6 +74,8 @@ def f(): test("@micropython.viper\ndef f(x:int): ~x") # binary op not implemented +test("@micropython.viper\ndef f(x:uint, y:uint): res = x // y") +test("@micropython.viper\ndef f(x:uint, y:uint): res = x % y") test("@micropython.viper\ndef f(x:int): res = x in x") # yield (from) not implemented diff --git a/tests/micropython/viper_error.py.exp b/tests/micropython/viper_error.py.exp index da9a0ca93ee34..31c85b1d872af 100644 --- a/tests/micropython/viper_error.py.exp +++ b/tests/micropython/viper_error.py.exp @@ -6,6 +6,7 @@ ViperTypeError("local 'x' has type 'int' but source is 'object'",) ViperTypeError("can't implicitly convert 'ptr' to 'bool'",) ViperTypeError("return expected 'int' but got 'object'",) ViperTypeError("can't do binary op between 'int' and 'object'",) +ViperTypeError('comparison of int and uint',) ViperTypeError("can't load from 'int'",) ViperTypeError("can't load from 'int'",) ViperTypeError("can't store to 'int'",) @@ -17,6 +18,8 @@ ViperTypeError('must raise an object',) ViperTypeError('unary op __pos__ not implemented',) ViperTypeError('unary op __neg__ not implemented',) ViperTypeError('unary op __invert__ not implemented',) +ViperTypeError('div/mod not implemented for uint',) +ViperTypeError('div/mod not implemented for uint',) ViperTypeError('binary op not implemented',) NotImplementedError('native yield',) NotImplementedError('native yield',) From 9f911d822ee97edaa94873823db5eb10c31f5f77 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 22 Jun 2020 10:21:02 +1000 Subject: [PATCH 0126/3533] py/objcomplex: Add mp_obj_get_complex_maybe for use in complex bin-op. This allows complex binary operations to fail gracefully with unsupported operation rather than raising an exception, so that special methods work correctly. Signed-off-by: Damien George --- py/obj.c | 9 ++++++++- py/obj.h | 1 + py/objcomplex.c | 5 ++++- tests/float/cmath_fun.py | 6 ++++++ tests/float/complex_special_mehods.py | 15 +++++++++++++++ tests/run-tests | 1 + 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/float/complex_special_mehods.py diff --git a/py/obj.c b/py/obj.c index 07b1612552548..ed047acc39f76 100644 --- a/py/obj.c +++ b/py/obj.c @@ -371,7 +371,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) { } #if MICROPY_PY_BUILTINS_COMPLEX -void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { +bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { if (arg == mp_const_false) { *real = 0; *imag = 0; @@ -392,6 +392,13 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { } else if (mp_obj_is_type(arg, &mp_type_complex)) { mp_obj_complex_get(arg, real, imag); } else { + return false; + } + return true; +} + +void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { + if (!mp_obj_get_complex_maybe(arg, real, imag)) { #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex")); #else diff --git a/py/obj.h b/py/obj.h index 590b9c4b6a4a1..1fa24eb18c903 100644 --- a/py/obj.h +++ b/py/obj.h @@ -778,6 +778,7 @@ bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value); mp_float_t mp_obj_get_float(mp_obj_t self_in); bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value); void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); +bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); #endif void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items); // *items may point inside a GC block diff --git a/py/objcomplex.c b/py/objcomplex.c index 91e4402309d88..f4c4aeffcb91d 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -178,7 +178,10 @@ void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) { mp_float_t rhs_real, rhs_imag; - mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible) + if (!mp_obj_get_complex_maybe(rhs_in, &rhs_real, &rhs_imag)) { + return MP_OBJ_NULL; // op not supported + } + switch (op) { case MP_BINARY_OP_ADD: case MP_BINARY_OP_INPLACE_ADD: diff --git a/tests/float/cmath_fun.py b/tests/float/cmath_fun.py index 7b5e692452765..15b72e7a6203b 100644 --- a/tests/float/cmath_fun.py +++ b/tests/float/cmath_fun.py @@ -57,3 +57,9 @@ if abs(real) < 1e-6: real = 0.0 print("complex(%.5g, %.5g)" % (real, ret.imag)) + +# test invalid type passed to cmath function +try: + log([]) +except TypeError: + print("TypeError") diff --git a/tests/float/complex_special_mehods.py b/tests/float/complex_special_mehods.py new file mode 100644 index 0000000000000..6789013fa6e0d --- /dev/null +++ b/tests/float/complex_special_mehods.py @@ -0,0 +1,15 @@ +# test complex interacting with special methods + + +class A: + def __add__(self, x): + print("__add__") + return 1 + + def __radd__(self, x): + print("__radd__") + return 2 + + +print(A() + 1j) +print(1j + A()) diff --git a/tests/run-tests b/tests/run-tests index f9e4de4b34a20..102b0f7790e14 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -355,6 +355,7 @@ def run_tests(pyb, tests, args, base_path="."): if not has_complex: skip_tests.add('float/complex1.py') skip_tests.add('float/complex1_intbig.py') + skip_tests.add('float/complex_special_mehods.py') skip_tests.add('float/int_big_float.py') skip_tests.add('float/true_value.py') skip_tests.add('float/types.py') From e4fcd216e02eef0b389c84ecd67be3114aac0a5d Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 26 Jun 2020 10:20:27 +1000 Subject: [PATCH 0127/3533] stm32/usbd_cdc_interface: Remove full==size-1 limitation on tx ringbuf. Before this commit the USB VCP TX ring-buffer used the basic implementation where it can only be filled to a maximum of buffer size-1. For a 1024 size buffer this means the largest packet that can be sent is 1023. Once a packet of this size is sent the next byte copied in goes to the final byte in the buffer, so must be sent as a 1 byte packet before the read pointer can be wrapped around to the beginning. So in large streaming transfers, watching the USB sniffer you basically get alternating 1023 byte packets then 1 byte packets. This commit changes the ring-buffer implementation to a scheme that doesn't have the full-size limitation, and the USB VCP driver can now achieve a constant stream of full-sized packets. This scheme introduces a restriction on the size of the buffer: it must be a power of 2, and the maximum size is half of the size of the index (in this case the index is 16-bit, so the maximum size would be 32767 bytes rounded to 16384 for a power-of-2). But this is not a big limitation because the size of the ring-buffer prior to this commit was restricted to powers of 2 because it was using a mask-based method to wrap the indices. For an explanation of the new scheme see https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/ The RX buffer could likely do with a similar change, though as it's not read from in chunks like the TX buffer it doesn't present the same issue, all that's lost is one byte capacity of the buffer. USB VCP TX throughput is improved by this change, potentially doubling the speed in certain cases. --- ports/stm32/usbd_cdc_interface.c | 72 ++++++++++++++++++++------------ ports/stm32/usbd_cdc_interface.h | 4 +- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index a0e19de9750aa..7a09128527cec 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -171,32 +171,56 @@ int8_t usbd_cdc_control(usbd_cdc_state_t *cdc_in, uint8_t cmd, uint8_t *pbuf, ui return USBD_OK; } +static inline uint16_t usbd_cdc_tx_buffer_mask(uint16_t val) { + return val & (USBD_CDC_TX_DATA_SIZE - 1); +} + +static inline uint16_t usbd_cdc_tx_buffer_size(usbd_cdc_itf_t *cdc) { + return cdc->tx_buf_ptr_in - cdc->tx_buf_ptr_out; +} + +static inline bool usbd_cdc_tx_buffer_empty(usbd_cdc_itf_t *cdc) { + return cdc->tx_buf_ptr_out == cdc->tx_buf_ptr_in; +} + +static inline bool usbd_cdc_tx_buffer_will_be_empty(usbd_cdc_itf_t *cdc) { + return cdc->tx_buf_ptr_out_next == cdc->tx_buf_ptr_in; +} + +static inline bool usbd_cdc_tx_buffer_full(usbd_cdc_itf_t *cdc) { + return usbd_cdc_tx_buffer_size(cdc) == USBD_CDC_TX_DATA_SIZE; +} + +static uint16_t usbd_cdc_tx_send_length(usbd_cdc_itf_t *cdc) { + uint16_t to_end = USBD_CDC_TX_DATA_SIZE - usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_out); + return MIN(usbd_cdc_tx_buffer_size(cdc), to_end); +} + +static void usbd_cdc_tx_buffer_put(usbd_cdc_itf_t *cdc, uint8_t data) { + cdc->tx_buf[usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_in)] = data; + cdc->tx_buf_ptr_in++; +} + +static uint8_t *usbd_cdc_tx_buffer_getp(usbd_cdc_itf_t *cdc, uint16_t len) { + cdc->tx_buf_ptr_out_next += len; + return &cdc->tx_buf[usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_out)]; +} + // Called when the USB IN endpoint is ready to receive more data // (cdc.base.tx_in_progress must be 0) void usbd_cdc_tx_ready(usbd_cdc_state_t *cdc_in) { usbd_cdc_itf_t *cdc = (usbd_cdc_itf_t *)cdc_in; - cdc->tx_buf_ptr_out = cdc->tx_buf_ptr_out_shadow; + cdc->tx_buf_ptr_out = cdc->tx_buf_ptr_out_next; - if (cdc->tx_buf_ptr_out == cdc->tx_buf_ptr_in && !cdc->tx_need_empty_packet) { + if (usbd_cdc_tx_buffer_empty(cdc) && !cdc->tx_need_empty_packet) { // No outstanding data to send return; } - - uint32_t len; - if (cdc->tx_buf_ptr_out > cdc->tx_buf_ptr_in) { // rollback - len = USBD_CDC_TX_DATA_SIZE - cdc->tx_buf_ptr_out; - } else { - len = cdc->tx_buf_ptr_in - cdc->tx_buf_ptr_out; - } - + uint16_t len = usbd_cdc_tx_send_length(cdc); // Should always succeed because cdc.base.tx_in_progress==0 - USBD_CDC_TransmitPacket(&cdc->base, len, &cdc->tx_buf[cdc->tx_buf_ptr_out]); + USBD_CDC_TransmitPacket(&cdc->base, len, usbd_cdc_tx_buffer_getp(cdc, len)); - cdc->tx_buf_ptr_out_shadow += len; - if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) { - cdc->tx_buf_ptr_out_shadow = 0; - } // According to the USB specification, a packet size of 64 bytes (CDC_DATA_FS_MAX_PACKET_SIZE) // gets held at the USB host until the next packet is sent. This is because a @@ -204,7 +228,7 @@ void usbd_cdc_tx_ready(usbd_cdc_state_t *cdc_in) { // the host waits for all data to arrive (ie, waits for a packet < max packet size). // To flush a packet of exactly max packet size, we need to send a zero-size packet. // See eg http://www.cypress.com/?id=4&rID=92719 - cdc->tx_need_empty_packet = (len > 0 && len % usbd_cdc_max_packet(cdc->base.usbd->pdev) == 0 && cdc->tx_buf_ptr_out_shadow == cdc->tx_buf_ptr_in); + cdc->tx_need_empty_packet = (len > 0 && len % usbd_cdc_max_packet(cdc->base.usbd->pdev) == 0 && usbd_cdc_tx_buffer_will_be_empty(cdc)); } // Attempt to queue data on the USB IN endpoint @@ -291,10 +315,7 @@ int8_t usbd_cdc_receive(usbd_cdc_state_t *cdc_in, size_t len) { } int usbd_cdc_tx_half_empty(usbd_cdc_itf_t *cdc) { - int32_t tx_waiting = (int32_t)cdc->tx_buf_ptr_in - (int32_t)cdc->tx_buf_ptr_out; - if (tx_waiting < 0) { - tx_waiting += USBD_CDC_TX_DATA_SIZE; - } + int32_t tx_waiting = usbd_cdc_tx_buffer_size(cdc); return tx_waiting <= USBD_CDC_TX_DATA_SIZE / 2; } @@ -317,8 +338,7 @@ int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t for (uint32_t i = 0; i < len; i++) { // Wait until the device is connected and the buffer has space, with a given timeout uint32_t start = HAL_GetTick(); - while (cdc->connect_state == USBD_CDC_CONNECT_STATE_DISCONNECTED - || ((cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1)) == cdc->tx_buf_ptr_out) { + while (cdc->connect_state == USBD_CDC_CONNECT_STATE_DISCONNECTED || usbd_cdc_tx_buffer_full(cdc)) { usbd_cdc_try_tx(cdc); // Wraparound of tick is taken care of by 2's complement arithmetic. if (HAL_GetTick() - start >= timeout) { @@ -333,8 +353,7 @@ int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t } // Write data to device buffer - cdc->tx_buf[cdc->tx_buf_ptr_in] = buf[i]; - cdc->tx_buf_ptr_in = (cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1); + usbd_cdc_tx_buffer_put(cdc, buf[i]); } usbd_cdc_try_tx(cdc); @@ -357,7 +376,7 @@ void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { // If the buffer is full, wait until it gets drained, with a timeout of 500ms // (wraparound of tick is taken care of by 2's complement arithmetic). uint32_t start = HAL_GetTick(); - while (((cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1)) == cdc->tx_buf_ptr_out && HAL_GetTick() - start <= 500) { + while (usbd_cdc_tx_buffer_full(cdc) && HAL_GetTick() - start <= 500) { usbd_cdc_try_tx(cdc); if (query_irq() == IRQ_STATE_DISABLED) { // IRQs disabled so buffer will never be drained; exit loop @@ -367,8 +386,7 @@ void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { } } - cdc->tx_buf[cdc->tx_buf_ptr_in] = buf[i]; - cdc->tx_buf_ptr_in = (cdc->tx_buf_ptr_in + 1) & (USBD_CDC_TX_DATA_SIZE - 1); + usbd_cdc_tx_buffer_put(cdc, buf[i]); } usbd_cdc_try_tx(cdc); } diff --git a/ports/stm32/usbd_cdc_interface.h b/ports/stm32/usbd_cdc_interface.h index 1847b3a6b0eb1..d0509b09f9a99 100644 --- a/ports/stm32/usbd_cdc_interface.h +++ b/ports/stm32/usbd_cdc_interface.h @@ -35,7 +35,7 @@ #define USBD_CDC_RX_DATA_SIZE (1024) // this must be 2 or greater, and a power of 2 #endif #ifndef USBD_CDC_TX_DATA_SIZE -#define USBD_CDC_TX_DATA_SIZE (1024) // I think this can be any value (was 2048) +#define USBD_CDC_TX_DATA_SIZE (1024) // This must be a power of 2 and no greater than 16384 #endif // Values for connect_state @@ -60,7 +60,7 @@ typedef struct _usbd_cdc_itf_t { uint8_t tx_buf[USBD_CDC_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer uint16_t tx_buf_ptr_in; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when new data is available volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when data is drained - uint16_t tx_buf_ptr_out_shadow; // shadow of above + uint16_t tx_buf_ptr_out_next; // next position of above once transmission finished uint8_t tx_need_empty_packet; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size volatile uint8_t connect_state; // indicates if we are connected From 048a1d675dc92ff49b1a00adedba0a0dea8d5ea0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 29 Jun 2020 16:06:30 +1000 Subject: [PATCH 0128/3533] stm32/timer: Properly initialise timer deadtime/brk on WB MCUs. Signed-off-by: Damien George --- ports/stm32/timer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index c6f3d21f8671e..9b8c14c0da552 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -477,7 +477,7 @@ STATIC void config_deadtime(pyb_timer_obj_t *self, mp_int_t ticks, mp_int_t brk) deadTimeConfig.DeadTime = compute_dtg_from_ticks(ticks); deadTimeConfig.BreakState = brk == BRK_OFF ? TIM_BREAK_DISABLE : TIM_BREAK_ENABLE; deadTimeConfig.BreakPolarity = brk == BRK_LOW ? TIM_BREAKPOLARITY_LOW : TIM_BREAKPOLARITY_HIGH; - #if defined(STM32F7) || defined(STM32H7) | defined(STM32L4) + #if defined(STM32F7) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) deadTimeConfig.BreakFilter = 0; deadTimeConfig.Break2State = TIM_BREAK_DISABLE; deadTimeConfig.Break2Polarity = TIM_BREAKPOLARITY_LOW; @@ -769,14 +769,14 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons HAL_TIM_Base_Init(&self->tim); #if !defined(STM32L0) #if defined(IS_TIM_ADVANCED_INSTANCE) - if (IS_TIM_ADVANCED_INSTANCE(self->tim.Instance)) { + if (IS_TIM_ADVANCED_INSTANCE(self->tim.Instance)) #elif defined(IS_TIM_BREAK_INSTANCE) - if (IS_TIM_BREAK_INSTANCE(self->tim.Instance)) { + if (IS_TIM_BREAK_INSTANCE(self->tim.Instance)) #else - if (0) { - #endif + if (0) + #endif + { config_deadtime(self, args[ARG_deadtime].u_int, args[ARG_brk].u_int); - } #endif From 4d6f60d4286b3e6ea8c9fb0141ba13e4978882b6 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Thu, 5 Mar 2020 15:51:20 +1100 Subject: [PATCH 0129/3533] tools/pydfu.py: Respect longer timeouts requested by DFU device/mboot. --- tools/pydfu.py | 52 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index 030f56bf852e3..8b8e338981b49 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -22,6 +22,8 @@ import usb.core import usb.util import zlib +import time +import math # VID/PID __VID = 0x0483 @@ -29,6 +31,8 @@ # USB request __TIMEOUT __TIMEOUT = 4000 +__NEXT_TIMEOUT = 0 +__STATUS_TIMEOUT = 20000 # DFU commands __DFU_DETACH = 0 @@ -95,6 +99,13 @@ def get_string(dev, index): return usb.util.get_string(dev, index) +def timeout(): + global __NEXT_TIMEOUT + t = max(__TIMEOUT, __NEXT_TIMEOUT) + __NEXT_TIMEOUT = 0 + return int(math.ceil(t)) + + def find_dfu_cfg_descr(descr): if len(descr) == 9 and descr[0] == 9 and descr[1] == _DFU_DESCRIPTOR_TYPE: nt = collections.namedtuple( @@ -150,17 +161,32 @@ def init(): def abort_request(): """Sends an abort request.""" - __dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, __TIMEOUT) + __dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, timeout()) def clr_status(): """Clears any error status (perhaps left over from a previous session).""" - __dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, __TIMEOUT) + __dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, timeout()) def get_status(): """Get the status of the last operation.""" - stat = __dev.ctrl_transfer(0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, 20000) + _timeout = time.time() + max(__STATUS_TIMEOUT, timeout()) + stat = None + while time.time() < _timeout: + try: + stat = __dev.ctrl_transfer( + 0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, int(_timeout - time.time()) + ) + break + except usb.core.USBError as ex: + # If the firmware is blocked the transfer can timeout much quicker than + # the supplied timeout. If so, retry until the overall timeout is used up. + if "Operation timed out" not in str(ex): + raise + + if stat is None: + raise SystemExit("DFU: get_status timed out") # firmware can provide an optional string for any error if stat[5]: @@ -168,6 +194,12 @@ def get_status(): if message: print(message) + # firmware can send a longer timeout request while it's performing slow operation eg. erase + timeout_ms = stat[1] << 16 | stat[2] << 8 | stat[3] + if timeout_ms: + global __NEXT_TIMEOUT + __NEXT_TIMEOUT = __TIMEOUT + timeout_ms + return stat[4] @@ -180,9 +212,9 @@ def check_status(stage, expected): def mass_erase(): """Performs a MASS erase (i.e. erases the entire device).""" # Send DNLOAD with first byte=0x41 - __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", __TIMEOUT) + __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", timeout()) - # Execute last command + # Execute erase and wait until complete check_status("erase", __DFU_STATE_DFU_DOWNLOAD_BUSY) # Check command state @@ -196,7 +228,7 @@ def page_erase(addr): # Send DNLOAD with first byte=0x41 and page address buf = struct.pack(" Date: Thu, 5 Mar 2020 16:05:36 +1100 Subject: [PATCH 0130/3533] stm32/mboot: Implement DFU mass erase. The implementation internally uses sector erase to wipe everything except the sector(s) that mboot lives in (by erasing starting from APPLICATION_ADDR). The erase command can take some time (eg an STM32F765 with 2MB of flash takes 8 to 10 seconds). This time is normally enough to make pydfu.py fail with a timeout. The DFU standard includes a mechanism for the DFU device to request a longer timeout as part of the get-status response just before starting an operation. This timeout functionality has been implemented here. --- ports/stm32/mboot/dfu.h | 6 +++++ ports/stm32/mboot/main.c | 57 ++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/ports/stm32/mboot/dfu.h b/ports/stm32/mboot/dfu.h index e826e217f032f..a1d4d10d0ef90 100644 --- a/ports/stm32/mboot/dfu.h +++ b/ports/stm32/mboot/dfu.h @@ -67,6 +67,12 @@ typedef enum { DFU_CMD_DNLOAD = 8, } dfu_cmd_t; +enum { + DFU_CMD_DNLOAD_SET_ADDRESS = 0x21, + DFU_CMD_DNLOAD_ERASE = 0x41, + DFU_CMD_DNLOAD_READ_UNPROTECT = 0x92, +}; + // Error status flags typedef enum { DFU_STATUS_OK = 0x00, // No error condition is present. diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 87618e7f410ad..d4527874120ad 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -441,6 +441,11 @@ static int usrbtn_state(void) { /******************************************************************************/ // FLASH +#if defined(STM32WB) +#define FLASH_END FLASH_END_ADDR +#endif +#define APPLICATION_FLASH_LENGTH (FLASH_END + 1 - APPLICATION_ADDR) + #ifndef MBOOT_SPIFLASH_LAYOUT #define MBOOT_SPIFLASH_LAYOUT "" #endif @@ -464,8 +469,9 @@ static int usrbtn_state(void) { #endif static int mboot_flash_mass_erase(void) { - // TODO - return -1; + // Erase all flash pages after mboot. + int ret = flash_erase(APPLICATION_ADDR, APPLICATION_FLASH_LENGTH / sizeof(uint32_t)); + return ret; } static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) { @@ -523,7 +529,7 @@ static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) { // Writable address space interface static int do_mass_erase(void) { - // TODO + // TODO spiflash erase ? return mboot_flash_mass_erase(); } @@ -791,20 +797,45 @@ static void dfu_init(void) { dfu_context.addr = 0x08000000; } +// The DFU_GETSTATUS response before dfu_process_dnload is run should include the needed timeout adjustments +static size_t get_timeout_ms(void) { + if (dfu_context.wBlockNum == 0) { + // download control commands + if (dfu_context.wLength >= 1 && dfu_context.buf[0] == DFU_CMD_DNLOAD_ERASE) { + if (dfu_context.wLength == 1) { + // mass erase command + // It takes 10-12 seconds to erase a 2MB stm part. Extrapolate a suitable timeout from this. + return APPLICATION_FLASH_LENGTH / 170; + + } else if (dfu_context.wLength == 5) { + // erase page command + return 500; + } + } + } else if (dfu_context.wBlockNum > 1) { + // write data to memory command + return 500; + } + return 0; +} + static int dfu_process_dnload(void) { int ret = -1; if (dfu_context.wBlockNum == 0) { // download control commands - if (dfu_context.wLength >= 1 && dfu_context.buf[0] == 0x41) { + if (dfu_context.wLength >= 1 && dfu_context.buf[0] == DFU_CMD_DNLOAD_ERASE) { if (dfu_context.wLength == 1) { // mass erase ret = do_mass_erase(); + if (ret != 0) { + dfu_context.cmd = DFU_CMD_NONE; + } } else if (dfu_context.wLength == 5) { // erase page uint32_t next_addr; ret = do_page_erase(get_le32(&dfu_context.buf[1]), &next_addr); } - } else if (dfu_context.wLength >= 1 && dfu_context.buf[0] == 0x21) { + } else if (dfu_context.wLength >= 1 && dfu_context.buf[0] == DFU_CMD_DNLOAD_SET_ADDRESS) { if (dfu_context.wLength == 5) { // set address dfu_context.addr = get_le32(&dfu_context.buf[1]); @@ -888,12 +919,16 @@ static int dfu_handle_tx(int cmd, int arg, int len, uint8_t *buf, int max_len) { default: dfu_context.state = DFU_STATE_BUSY; } - buf[0] = dfu_context.status; // bStatus - buf[1] = 0; // bwPollTimeout (ms) - buf[2] = 0; // bwPollTimeout (ms) - buf[3] = 0; // bwPollTimeout (ms) - buf[4] = dfu_context.state; // bState - buf[5] = dfu_context.error; // iString + size_t timeout_ms = get_timeout_ms(); + buf[0] = dfu_context.status; // bStatus + buf[1] = (timeout_ms >> 16) & 0xFF; // bwPollTimeout (ms) + buf[2] = (timeout_ms >> 8) & 0xFF; // bwPollTimeout (ms) + buf[3] = timeout_ms & 0xFF; // bwPollTimeout (ms) + buf[4] = dfu_context.state; // bState + buf[5] = dfu_context.error; // iString + // Clear errors now they've been sent + dfu_context.status = DFU_STATUS_OK; + dfu_context.error = 0; return 6; } return -1; From 40006813c3e053e4cf952bfdd32866ce9496b0f8 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 30 Jun 2020 16:33:32 +1000 Subject: [PATCH 0131/3533] stm32/flash: Update flash_get_sector_info to return -1 on invalid addr. So the caller can tell when an invalid address is used and can take appropriate action. --- ports/stm32/flash.c | 4 ++-- ports/stm32/flash.h | 2 +- ports/stm32/flashbdev.c | 4 ++-- ports/stm32/mboot/main.c | 23 +++++++++++++---------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ports/stm32/flash.c b/ports/stm32/flash.c index 5f96696a9ffd7..499129a6f3e5d 100644 --- a/ports/stm32/flash.c +++ b/ports/stm32/flash.c @@ -151,7 +151,7 @@ bool flash_is_valid_addr(uint32_t addr) { return flash_layout[0].base_address <= addr && addr < end_of_flash; } -uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) { +int32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) { if (addr >= flash_layout[0].base_address) { uint32_t sector_index = 0; for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) { @@ -172,7 +172,7 @@ uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *si } } } - return 0; + return -1; } int flash_erase(uint32_t flash_dest, uint32_t num_word32) { diff --git a/ports/stm32/flash.h b/ports/stm32/flash.h index 12cdaca55aab9..ecda923db7826 100644 --- a/ports/stm32/flash.h +++ b/ports/stm32/flash.h @@ -27,7 +27,7 @@ #define MICROPY_INCLUDED_STM32_FLASH_H bool flash_is_valid_addr(uint32_t addr); -uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size); +int32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size); int flash_erase(uint32_t flash_dest, uint32_t num_word32); int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32); diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index e105bd35381b9..4153a713c8636 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -181,7 +181,7 @@ int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg) { static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) { uint32_t flash_sector_start; uint32_t flash_sector_size; - uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); + int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); if (flash_sector_size > FLASH_SECTOR_SIZE_MAX) { flash_sector_size = FLASH_SECTOR_SIZE_MAX; } @@ -201,7 +201,7 @@ static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) { static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) { uint32_t flash_sector_start; uint32_t flash_sector_size; - uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); + int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size); if (flash_cache_sector_id == flash_sector_id) { // in cache, copy from there return (uint8_t *)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start; diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index d4527874120ad..8cc14c2ac99cb 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -476,25 +476,27 @@ static int mboot_flash_mass_erase(void) { static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) { uint32_t sector_size = 0; - uint32_t sector = flash_get_sector_info(addr, NULL, §or_size); - if (sector == 0) { - // Don't allow to erase the sector with this bootloader in it + uint32_t sector_start = 0; + int32_t sector = flash_get_sector_info(addr, §or_start, §or_size); + if (sector <= 0) { + // Don't allow to erase the sector with this bootloader in it, or invalid sectors dfu_context.status = DFU_STATUS_ERROR_ADDRESS; - dfu_context.error = MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX; + dfu_context.error = (sector == 0) ? MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX + : MBOOT_ERROR_STR_INVALID_ADDRESS_IDX; return -1; } - *next_addr = addr + sector_size; + *next_addr = sector_start + sector_size; // Erase the flash page. - int ret = flash_erase(addr, sector_size / sizeof(uint32_t)); + int ret = flash_erase(sector_start, sector_size / sizeof(uint32_t)); if (ret != 0) { return ret; } // Check the erase set bits to 1, at least for the first 256 bytes for (int i = 0; i < 64; ++i) { - if (((volatile uint32_t*)addr)[i] != 0xffffffff) { + if (((volatile uint32_t*)sector_start)[i] != 0xffffffff) { return -2; } } @@ -503,11 +505,12 @@ static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) { } static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) { - uint32_t sector = flash_get_sector_info(addr, NULL, NULL); - if (sector == 0) { + int32_t sector = flash_get_sector_info(addr, NULL, NULL); + if (sector <= 0) { // Don't allow to write the sector with this bootloader in it dfu_context.status = DFU_STATUS_ERROR_ADDRESS; - dfu_context.error = MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX; + dfu_context.error = (sector == 0) ? MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX + : MBOOT_ERROR_STR_INVALID_ADDRESS_IDX; return -1; } From 65a7e00078fae27521dd9df78e328c6fa1e3b288 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 30 Jun 2020 14:23:50 +1000 Subject: [PATCH 0132/3533] stm32/mboot: Add DFU logic to respond to DFU_GETSTATE request. This is required for some DFU programmers, eg ST's DfuSe demo PC app. Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 8cc14c2ac99cb..ba0c35b8bfe4c 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -933,6 +933,9 @@ static int dfu_handle_tx(int cmd, int arg, int len, uint8_t *buf, int max_len) { dfu_context.status = DFU_STATUS_OK; dfu_context.error = 0; return 6; + } else if (cmd == DFU_GETSTATE && len == 1) { + buf[0] = dfu_context.state; // bState + return 1; } return -1; } From cb9aafbf8f0ee912e5f4d0a32d5f9765d9e8f30c Mon Sep 17 00:00:00 2001 From: victor Date: Sun, 28 Jun 2020 23:41:04 +0200 Subject: [PATCH 0133/3533] docs/library: Clarify that the arg to esp.deepsleep is in microseconds. --- docs/library/esp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/esp.rst b/docs/library/esp.rst index cb2bc7af8d538..b9ae57bd9757f 100644 --- a/docs/library/esp.rst +++ b/docs/library/esp.rst @@ -31,7 +31,7 @@ Functions The system enters the set sleep mode automatically when possible. -.. function:: deepsleep(time=0, /) +.. function:: deepsleep(time_us=0, /) **Note**: ESP8266 only - use `machine.deepsleep()` on ESP32 From b572aa5721e3516367c56fba9e0d09ac0edbbd2c Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 Date: Thu, 25 Jun 2020 15:35:58 +0200 Subject: [PATCH 0134/3533] docs/esp32: Add info about PWM duty cycle range to esp32 quickref. See related #4581. --- docs/esp32/quickref.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 8861ca4ac87ed..b4f443a910009 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -175,6 +175,7 @@ range from 1Hz to 40MHz but there is a tradeoff; as the base frequency *increases* the duty resolution *decreases*. See `LED Control `_ for more details. +Currently the duty cycle has to be in the range of 0-1023. Use the ``machine.PWM`` class:: From 9d5edb35596b0ff5943bf1cb756dc3c42e8566d9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Jun 2020 16:22:29 +1000 Subject: [PATCH 0135/3533] lib/utils: Protect all of mpirq.c with MICROPY_ENABLE_SCHEDULER. So it can be unconditionally included in a port's build even if certain configurations in that port do not use its features, to simplify the Makefile. Signed-off-by: Damien George --- lib/utils/mpirq.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/utils/mpirq.c b/lib/utils/mpirq.c index 8de13b0b6a3c8..663be182248cc 100644 --- a/lib/utils/mpirq.c +++ b/lib/utils/mpirq.c @@ -31,6 +31,8 @@ #include "py/gc.h" #include "lib/utils/mpirq.h" +#if MICROPY_ENABLE_SCHEDULER + /****************************************************************************** DECLARE PUBLIC DATA ******************************************************************************/ @@ -125,3 +127,5 @@ const mp_obj_type_t mp_irq_type = { .call = mp_irq_call, .locals_dict = (mp_obj_dict_t *)&mp_irq_locals_dict, }; + +#endif // MICROPY_ENABLE_SCHEDULER From f84145bea1bc962b5aecf24bef6ea64202d905ee Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Jun 2020 16:22:56 +1000 Subject: [PATCH 0136/3533] zephyr: Implement machine.Pin.irq() for setting callbacks on pin change. Supports hard and soft interrupts. In the current implementation, soft interrupt callbacks will only be called when the VM is executing, ie they will not be called during a blocking kernel call like k_msleep. And the behaviour of hard interrupt callbacks will depend on the underlying device, as well as the amount of ISR stack space. Soft and hard interrupts tested on frdm_k64f and nucleo_f767zi boards. Signed-off-by: Damien George --- ports/zephyr/Makefile | 1 + ports/zephyr/machine_pin.c | 133 ++++++++++++++++++++++++++++++++++++ ports/zephyr/main.c | 6 ++ ports/zephyr/modmachine.h | 3 + ports/zephyr/mpconfigport.h | 7 +- 5 files changed, 149 insertions(+), 1 deletion(-) diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile index 4fe6552846aab..169aebc26772e 100644 --- a/ports/zephyr/Makefile +++ b/ports/zephyr/Makefile @@ -52,6 +52,7 @@ SRC_C = main.c \ uart_core.c \ zephyr_storage.c \ lib/timeutils/timeutils.c \ + lib/utils/mpirq.c \ lib/utils/stdout_helpers.c \ lib/utils/printf.c \ lib/utils/pyexec.c \ diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index ad5f54baafb92..79d759eb28bf3 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -35,10 +35,50 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" +#include "lib/utils/mpirq.h" #include "modmachine.h" +#if MICROPY_PY_MACHINE + +typedef struct _machine_pin_irq_obj_t { + mp_irq_obj_t base; + struct _machine_pin_irq_obj_t *next; + struct gpio_callback callback; +} machine_pin_irq_obj_t; + +STATIC const mp_irq_methods_t machine_pin_irq_methods; const mp_obj_base_t machine_pin_obj_template = {&machine_pin_type}; +void machine_pin_deinit(void) { + for (machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_list); irq != NULL; irq = irq->next) { + machine_pin_obj_t *pin = MP_OBJ_TO_PTR(irq->base.parent); + gpio_pin_interrupt_configure(pin->port, pin->pin, GPIO_INT_DISABLE); + gpio_remove_callback(pin->port, &irq->callback); + } + MP_STATE_PORT(machine_pin_irq_list) = NULL; +} + +STATIC void gpio_callback_handler(struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { + machine_pin_irq_obj_t *irq = CONTAINER_OF(cb, machine_pin_irq_obj_t, callback); + + #if MICROPY_STACK_CHECK + // This callback executes in an ISR context so the stack-limit check must be changed to + // use the ISR stack for the duration of this function (so that hard IRQ callbacks work). + char *orig_stack_top = MP_STATE_THREAD(stack_top); + size_t orig_stack_limit = MP_STATE_THREAD(stack_limit); + MP_STATE_THREAD(stack_top) = (void *)&irq; + MP_STATE_THREAD(stack_limit) = CONFIG_ISR_STACK_SIZE - 512; + #endif + + mp_irq_handler(&irq->base); + + #if MICROPY_STACK_CHECK + // Restore original stack-limit checking values. + MP_STATE_THREAD(stack_top) = orig_stack_top; + MP_STATE_THREAD(stack_limit) = orig_stack_limit; + #endif +} + STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pin_obj_t *self = self_in; mp_printf(print, "", self->port, self->pin); @@ -151,6 +191,66 @@ STATIC mp_obj_t machine_pin_on(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on); +// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False) +STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_handler, ARG_trigger, ARG_hard }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_INT_EDGE_BOTH} }, + { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, + }; + machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (self->irq == NULL) { + machine_pin_irq_obj_t *irq; + for (irq = MP_STATE_PORT(machine_pin_irq_list); irq != NULL; irq = irq->next) { + machine_pin_obj_t *irq_pin = MP_OBJ_TO_PTR(irq->base.parent); + if (irq_pin->port == self->port && irq_pin->pin == self->pin) { + break; + } + } + if (irq == NULL) { + irq = m_new_obj(machine_pin_irq_obj_t); + irq->base.base.type = &mp_irq_type; + irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods; + irq->base.parent = MP_OBJ_FROM_PTR(self); + irq->base.handler = mp_const_none; + irq->base.ishard = false; + irq->next = MP_STATE_PORT(machine_pin_irq_list); + gpio_init_callback(&irq->callback, gpio_callback_handler, BIT(self->pin)); + int ret = gpio_add_callback(self->port, &irq->callback); + if (ret != 0) { + mp_raise_OSError(-ret); + } + MP_STATE_PORT(machine_pin_irq_list) = irq; + } + self->irq = irq; + } + + if (n_args > 1 || kw_args->used != 0) { + // configure irq + int ret = gpio_pin_interrupt_configure(self->port, self->pin, GPIO_INT_DISABLE); + if (ret != 0) { + mp_raise_OSError(-ret); + } + + self->irq->base.handler = args[ARG_handler].u_obj; + self->irq->base.ishard = args[ARG_hard].u_bool; + + if (args[ARG_handler].u_obj != mp_const_none) { + ret = gpio_pin_interrupt_configure(self->port, self->pin, args[ARG_trigger].u_int); + if (ret != 0) { + mp_raise_OSError(-ret); + } + } + } + + return MP_OBJ_FROM_PTR(self->irq); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq); + STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { (void)errcode; machine_pin_obj_t *self = self_in; @@ -172,12 +272,15 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) }, { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) }, { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) }, + { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, // class constants { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_INPUT) }, { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_OUTPUT) }, { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) }, { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_INT_EDGE_RISING) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_INT_EDGE_FALLING) }, }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); @@ -195,3 +298,33 @@ const mp_obj_type_t machine_pin_type = { .protocol = &machine_pin_pin_p, .locals_dict = (mp_obj_t)&machine_pin_locals_dict, }; + +STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (new_trigger == 0) { + new_trigger = GPIO_INT_DISABLE; + } + int ret = gpio_pin_interrupt_configure(self->port, self->pin, new_trigger); + if (ret != 0) { + mp_raise_OSError(-ret); + } + return 0; +} + +STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (info_type == MP_IRQ_INFO_FLAGS) { + return gpio_get_pending_int(self->port); + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return 0; // TODO + } + return 0; +} + +STATIC const mp_irq_methods_t machine_pin_irq_methods = { + .init = machine_pin_irq, + .trigger = machine_pin_irq_trigger, + .info = machine_pin_irq_info, +}; + +#endif // MICROPY_PY_MACHINE diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 104954f595c0a..78b947d7af9b9 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -55,6 +55,7 @@ #include "extmod/vfs.h" #endif +#include "modmachine.h" #include "modzephyr.h" #ifdef TEST @@ -166,6 +167,11 @@ int real_main(void) { } printf("soft reboot\n"); + + #if MICROPY_PY_MACHINE + machine_pin_deinit(); + #endif + goto soft_reset; return 0; diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index 84e4d10a885ae..766a9d7ea3874 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -11,6 +11,9 @@ typedef struct _machine_pin_obj_t { mp_obj_base_t base; struct device *port; uint32_t pin; + struct _machine_pin_irq_obj_t *irq; } machine_pin_obj_t; +void machine_pin_deinit(void); + #endif // MICROPY_INCLUDED_ZEPHYR_MODMACHINE_H diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 7044c175d030c..f94ee72707d47 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -81,6 +81,7 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_BUILTINS_COMPLEX (0) +#define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_VFS (1) #define MICROPY_READER_VFS (MICROPY_VFS) @@ -119,7 +120,8 @@ typedef long mp_off_t; #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; + const char *readline_hist[8]; \ + void *machine_pin_irq_list; /* Linked list of pin irq objects */ extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_time; @@ -169,3 +171,6 @@ extern const struct _mp_obj_module_t mp_module_zsensor; // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, + +#define MICROPY_BEGIN_ATOMIC_SECTION irq_lock +#define MICROPY_END_ATOMIC_SECTION irq_unlock From 41b7734c464cbdcd0d447a4de697ce9b3fcd9e04 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Jun 2020 16:23:09 +1000 Subject: [PATCH 0137/3533] zephyr/make-minimal: Disable FAT and LFS2 options to make it build. Signed-off-by: Damien George --- ports/zephyr/make-minimal | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/zephyr/make-minimal b/ports/zephyr/make-minimal index 1fc143e4d6af0..d7dddc33427df 100755 --- a/ports/zephyr/make-minimal +++ b/ports/zephyr/make-minimal @@ -11,6 +11,8 @@ make \ CONF_FILE=prj_minimal.conf \ CFLAGS_EXTRA='-DMP_CONFIGFILE=""' \ + MICROPY_VFS_FAT=0 \ + MICROPY_VFS_LFS2=0 \ FROZEN_DIR= \ QEMU_NET=0 \ "$@" From d06ae1d2b11d03e45b6d12a41b9435ae0ca1690c Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 30 Jun 2020 23:52:35 +1000 Subject: [PATCH 0138/3533] py/obj.h: Make existing MP_TYPE_FLAG_xxx macros sequential. There's no reason to have them non-sequential, this was likely a typo from commit 9ec1caf42e7733b5141b7aecf1b6e58834a94bf7. Signed-off-by: Damien George --- py/obj.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/obj.h b/py/obj.h index 1fa24eb18c903..6bec57c35293c 100644 --- a/py/obj.h +++ b/py/obj.h @@ -475,8 +475,8 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); // operator and not the __ne__ operator. If it's set then __ne__ may be implemented. #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001) #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002) -#define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0040) -#define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0080) +#define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004) +#define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008) #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010) typedef enum { From 332d83343fb3ef5d2b94b4f058aa53fd0493779e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 28 Jun 2020 09:39:20 +1000 Subject: [PATCH 0139/3533] py: Rework mp_convert_member_lookup to properly handle built-ins. This commit fixes lookups of class members to make it so that built-in functions that are used as methods/functions of a class work correctly. The mp_convert_member_lookup() function is pretty much completely changed by this commit, but for the most part it's just reorganised and the indenting changed. The functional changes are: - staticmethod and classmethod checks moved to later in the if-logic, because they are less common and so should be checked after the more common cases. - The explicit mp_obj_is_type(member, &mp_type_type) check is removed because it's now subsumed by other, more general tests in this function. - MP_TYPE_FLAG_BINDS_SELF and MP_TYPE_FLAG_BUILTIN_FUN type flags added to make the checks in this function much simpler (now they just test this bit in type->flags). - An extra check is made for mp_obj_is_instance_type(type) to fix lookup of built-in functions. Fixes #1326 and #6198. Signed-off-by: Damien George --- py/obj.h | 4 ++ py/objclosure.c | 1 + py/objfun.c | 8 ++++ py/objgenerator.c | 2 + py/runtime.c | 82 ++++++++++++++++++--------------- tests/basics/class_bind_self.py | 9 +++- 6 files changed, 67 insertions(+), 39 deletions(-) diff --git a/py/obj.h b/py/obj.h index 6bec57c35293c..f621b1dadd492 100644 --- a/py/obj.h +++ b/py/obj.h @@ -473,11 +473,15 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); // then the type may check for equality against a different type. // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__ // operator and not the __ne__ operator. If it's set then __ne__ may be implemented. +// If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg. +// If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type. #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001) #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002) #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004) #define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008) #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010) +#define MP_TYPE_FLAG_BINDS_SELF (0x0020) +#define MP_TYPE_FLAG_BUILTIN_FUN (0x0040) typedef enum { PRINT_STR = 0, diff --git a/py/objclosure.c b/py/objclosure.c index f5038ffc46513..9a8c7631c2bea 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -80,6 +80,7 @@ STATIC void closure_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ const mp_obj_type_t closure_type = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_closure, #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED .print = closure_print, diff --git a/py/objfun.c b/py/objfun.c index f9a6b4ebc2727..052f4b1cedfd2 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -58,6 +58,7 @@ STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_type_t mp_type_fun_builtin_0 = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, .name = MP_QSTR_function, .call = fun_builtin_0_call, .unary_op = mp_generic_unary_op, @@ -72,6 +73,7 @@ STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_type_t mp_type_fun_builtin_1 = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, .name = MP_QSTR_function, .call = fun_builtin_1_call, .unary_op = mp_generic_unary_op, @@ -86,6 +88,7 @@ STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_type_t mp_type_fun_builtin_2 = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, .name = MP_QSTR_function, .call = fun_builtin_2_call, .unary_op = mp_generic_unary_op, @@ -100,6 +103,7 @@ STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_type_t mp_type_fun_builtin_3 = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, .name = MP_QSTR_function, .call = fun_builtin_3_call, .unary_op = mp_generic_unary_op, @@ -130,6 +134,7 @@ STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_k const mp_obj_type_t mp_type_fun_builtin_var = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, .name = MP_QSTR_function, .call = fun_builtin_var_call, .unary_op = mp_generic_unary_op, @@ -355,6 +360,7 @@ void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { const mp_obj_type_t mp_type_fun_bc = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_function, #if MICROPY_CPYTHON_COMPAT .print = fun_bc_print, @@ -406,6 +412,7 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, co STATIC const mp_obj_type_t mp_type_fun_native = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_function, .call = fun_native_call, .unary_op = mp_generic_unary_op, @@ -513,6 +520,7 @@ STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const STATIC const mp_obj_type_t mp_type_fun_asm = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_function, .call = fun_asm_call, .unary_op = mp_generic_unary_op, diff --git a/py/objgenerator.c b/py/objgenerator.c index 5e140ba234474..543685fac7860 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -73,6 +73,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons const mp_obj_type_t mp_type_gen_wrap = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_generator, .call = gen_wrap_call, .unary_op = mp_generic_unary_op, @@ -126,6 +127,7 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k const mp_obj_type_t mp_type_native_gen_wrap = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_generator, .call = native_gen_wrap_call, .unary_op = mp_generic_unary_op, diff --git a/py/runtime.c b/py/runtime.c index 157bc74a8dfb4..78da386919532 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -993,6 +993,7 @@ STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c STATIC const mp_obj_type_t mp_type_checked_fun = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_function, .call = checked_fun_call, }; @@ -1011,49 +1012,54 @@ STATIC mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) // and put the result in the dest[] array for a possible method call. // Conversion means dealing with static/class methods, callables, and values. // see http://docs.python.org/3/howto/descriptor.html +// and also https://mail.python.org/pipermail/python-dev/2015-March/138950.html void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest) { - if (mp_obj_is_type(member, &mp_type_staticmethod)) { - // return just the function - dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun; - } else if (mp_obj_is_type(member, &mp_type_classmethod)) { - // return a bound method, with self being the type of this object - // this type should be the type of the original instance, not the base - // type (which is what is passed in the 'type' argument to this function) - if (self != MP_OBJ_NULL) { - type = mp_obj_get_type(self); - } - dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun; - dest[1] = MP_OBJ_FROM_PTR(type); - } else if (mp_obj_is_type(member, &mp_type_type)) { - // Don't try to bind types (even though they're callable) - dest[0] = member; - } else if (mp_obj_is_fun(member) - || (mp_obj_is_obj(member) - && (((mp_obj_base_t *)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_closure - || ((mp_obj_base_t *)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_generator))) { - // only functions, closures and generators objects can be bound to self - #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG + if (mp_obj_is_obj(member)) { const mp_obj_type_t *m_type = ((mp_obj_base_t *)MP_OBJ_TO_PTR(member))->type; - if (self == MP_OBJ_NULL - && (m_type == &mp_type_fun_builtin_0 - || m_type == &mp_type_fun_builtin_1 - || m_type == &mp_type_fun_builtin_2 - || m_type == &mp_type_fun_builtin_3 - || m_type == &mp_type_fun_builtin_var) - && type != &mp_type_object) { - // we extracted a builtin method without a first argument, so we must - // wrap this function in a type checker - // Note that object will do its own checking so shouldn't be wrapped. - dest[0] = mp_obj_new_checked_fun(type, member); - } else - #endif - { - // return a bound method, with self being this object + if (m_type->flags & MP_TYPE_FLAG_BINDS_SELF) { + // `member` is a function that binds self as its first argument. + if (m_type->flags & MP_TYPE_FLAG_BUILTIN_FUN) { + // `member` is a built-in function, which has special behaviour. + if (mp_obj_is_instance_type(type)) { + // Built-in functions on user types always behave like a staticmethod. + dest[0] = member; + } + #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG + else if (self == MP_OBJ_NULL && type != &mp_type_object) { + // `member` is a built-in method without a first argument, so wrap + // it in a type checker that will check self when it's supplied. + // Note that object will do its own checking so shouldn't be wrapped. + dest[0] = mp_obj_new_checked_fun(type, member); + } + #endif + else { + // Return a (built-in) bound method, with self being this object. + dest[0] = member; + dest[1] = self; + } + } else { + // Return a bound method, with self being this object. + dest[0] = member; + dest[1] = self; + } + } else if (m_type == &mp_type_staticmethod) { + // `member` is a staticmethod, return the function that it wraps. + dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun; + } else if (m_type == &mp_type_classmethod) { + // `member` is a classmethod, return a bound method with self being the type of + // this object. This type should be the type of the original instance, not the + // base type (which is what is passed in the `type` argument to this function). + if (self != MP_OBJ_NULL) { + type = mp_obj_get_type(self); + } + dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun; + dest[1] = MP_OBJ_FROM_PTR(type); + } else { + // `member` is a value, so just return that value. dest[0] = member; - dest[1] = self; } } else { - // class member is a value, so just return that value + // `member` is a value, so just return that value. dest[0] = member; } } diff --git a/tests/basics/class_bind_self.py b/tests/basics/class_bind_self.py index 16e4f5ac95ade..813f876446ef2 100644 --- a/tests/basics/class_bind_self.py +++ b/tests/basics/class_bind_self.py @@ -40,11 +40,18 @@ def f4(self, arg): # generator print(c.f3()) print(next(c.f4(4))) print(c.f5(5)) -#print(c.f6(-6)) not working in uPy +print(c.f6(-6)) print(c.f7(7)) print(c.f8(8)) print(c.f9(9)) +# test calling the functions accessed via the class itself +print(C.f5(10)) +print(C.f6(-11)) +print(C.f7(12)) +print(C.f8(13)) +print(C.f9(14)) + # not working in uPy #class C(list): # # this acts like a method and binds self From 95ec0debec1064695b9b0ecdbfa2146f17f898bb Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 1 Jul 2020 15:05:03 +1000 Subject: [PATCH 0140/3533] stm32/mboot: Remove the use of timeout in DFU_GETSTATUS. This is treated more like a "delay before continuing" in the spec and official tools and does not appear to be really needed. In particular, downloading firmware is much slower with non-zero timeouts because the host must pause by the timeout between sending each DFU_GETSTATUS to poll for download/erase complete. --- ports/stm32/mboot/main.c | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index ba0c35b8bfe4c..03620e6a3bdb2 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -800,28 +800,6 @@ static void dfu_init(void) { dfu_context.addr = 0x08000000; } -// The DFU_GETSTATUS response before dfu_process_dnload is run should include the needed timeout adjustments -static size_t get_timeout_ms(void) { - if (dfu_context.wBlockNum == 0) { - // download control commands - if (dfu_context.wLength >= 1 && dfu_context.buf[0] == DFU_CMD_DNLOAD_ERASE) { - if (dfu_context.wLength == 1) { - // mass erase command - // It takes 10-12 seconds to erase a 2MB stm part. Extrapolate a suitable timeout from this. - return APPLICATION_FLASH_LENGTH / 170; - - } else if (dfu_context.wLength == 5) { - // erase page command - return 500; - } - } - } else if (dfu_context.wBlockNum > 1) { - // write data to memory command - return 500; - } - return 0; -} - static int dfu_process_dnload(void) { int ret = -1; if (dfu_context.wBlockNum == 0) { @@ -922,11 +900,10 @@ static int dfu_handle_tx(int cmd, int arg, int len, uint8_t *buf, int max_len) { default: dfu_context.state = DFU_STATE_BUSY; } - size_t timeout_ms = get_timeout_ms(); buf[0] = dfu_context.status; // bStatus - buf[1] = (timeout_ms >> 16) & 0xFF; // bwPollTimeout (ms) - buf[2] = (timeout_ms >> 8) & 0xFF; // bwPollTimeout (ms) - buf[3] = timeout_ms & 0xFF; // bwPollTimeout (ms) + buf[1] = 0; // bwPollTimeout_lsb (ms) + buf[2] = 0; // bwPollTimeout (ms) + buf[3] = 0; // bwPollTimeout_msb (ms) buf[4] = dfu_context.state; // bState buf[5] = dfu_context.error; // iString // Clear errors now they've been sent From 494bcad8aba37966297470419b3c690a362495bb Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 1 Jul 2020 15:06:14 +1000 Subject: [PATCH 0141/3533] stm32/mboot: Disable polling mode by default and use IRQ mode instead. Polling mode will cause failures with the mass-erase command due to USB timeouts, because the USB IRQs are not being serviced. Swiching from polling to IRQ mode fixes this because the USB IRQs can be serviced between page erases. Note that when the flash is being programmed or erased the MCU is halted and cannot respond to USB IRQs, because mboot runs from flash, as opposed to the built-in bootloader which is in system ROM. But the maximum delay in responding to an IRQ is the time taken to erase a single page, about 100ms for large pages, and that is short enough that the USB does not timeout on the host side. Recent tests have shown that in the current mboot code IRQ mode is pretty much the same speed as polling mode (within timing error), code size is slightly reduced in IRQ mode, and IRQ mode idles at about half of the power consumption as polling mode. --- ports/stm32/mboot/main.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 03620e6a3bdb2..99187e3ef8406 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -38,14 +38,12 @@ #include "powerctrl.h" #include "dfu.h" -// Using polling is about 10% faster than not using it (and using IRQ instead) -// This DFU code with polling runs in about 70% of the time of the ST bootloader +// This option selects whether to use explicit polling or IRQs for USB events. +// In some test cases polling mode can run slightly faster, but it uses more power. +// Polling mode will also cause failures with the mass-erase command because USB +// events will not be serviced for the duration of the mass erase. // With STM32WB MCUs only non-polling/IRQ mode is supported. -#if defined(STM32WB) #define USE_USB_POLLING (0) -#else -#define USE_USB_POLLING (1) -#endif // Using cache probably won't make it faster because we run at a low frequency, and best // to keep the MCU config as minimal as possible. From 07f181a216dc2fe323f0511cf3a5f9dc37545c22 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 1 Jul 2020 15:01:16 +1000 Subject: [PATCH 0142/3533] Revert "tools/pydfu.py: Respect longer timeouts requested by DFU dev..." This reverts commit 4d6f60d4286b3e6ea8c9fb0141ba13e4978882b6. This implementation used the timeout as a maximum amount of time needed for the operation, when actually the spec and other tools suggest that it's the minumum delay needed between subsequent USB transfers. --- tools/pydfu.py | 52 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index 8b8e338981b49..030f56bf852e3 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -22,8 +22,6 @@ import usb.core import usb.util import zlib -import time -import math # VID/PID __VID = 0x0483 @@ -31,8 +29,6 @@ # USB request __TIMEOUT __TIMEOUT = 4000 -__NEXT_TIMEOUT = 0 -__STATUS_TIMEOUT = 20000 # DFU commands __DFU_DETACH = 0 @@ -99,13 +95,6 @@ def get_string(dev, index): return usb.util.get_string(dev, index) -def timeout(): - global __NEXT_TIMEOUT - t = max(__TIMEOUT, __NEXT_TIMEOUT) - __NEXT_TIMEOUT = 0 - return int(math.ceil(t)) - - def find_dfu_cfg_descr(descr): if len(descr) == 9 and descr[0] == 9 and descr[1] == _DFU_DESCRIPTOR_TYPE: nt = collections.namedtuple( @@ -161,32 +150,17 @@ def init(): def abort_request(): """Sends an abort request.""" - __dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, timeout()) + __dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, __TIMEOUT) def clr_status(): """Clears any error status (perhaps left over from a previous session).""" - __dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, timeout()) + __dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, __TIMEOUT) def get_status(): """Get the status of the last operation.""" - _timeout = time.time() + max(__STATUS_TIMEOUT, timeout()) - stat = None - while time.time() < _timeout: - try: - stat = __dev.ctrl_transfer( - 0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, int(_timeout - time.time()) - ) - break - except usb.core.USBError as ex: - # If the firmware is blocked the transfer can timeout much quicker than - # the supplied timeout. If so, retry until the overall timeout is used up. - if "Operation timed out" not in str(ex): - raise - - if stat is None: - raise SystemExit("DFU: get_status timed out") + stat = __dev.ctrl_transfer(0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, 20000) # firmware can provide an optional string for any error if stat[5]: @@ -194,12 +168,6 @@ def get_status(): if message: print(message) - # firmware can send a longer timeout request while it's performing slow operation eg. erase - timeout_ms = stat[1] << 16 | stat[2] << 8 | stat[3] - if timeout_ms: - global __NEXT_TIMEOUT - __NEXT_TIMEOUT = __TIMEOUT + timeout_ms - return stat[4] @@ -212,9 +180,9 @@ def check_status(stage, expected): def mass_erase(): """Performs a MASS erase (i.e. erases the entire device).""" # Send DNLOAD with first byte=0x41 - __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", timeout()) + __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", __TIMEOUT) - # Execute erase and wait until complete + # Execute last command check_status("erase", __DFU_STATE_DFU_DOWNLOAD_BUSY) # Check command state @@ -228,7 +196,7 @@ def page_erase(addr): # Send DNLOAD with first byte=0x41 and page address buf = struct.pack(" Date: Tue, 9 Jun 2020 16:45:17 +1000 Subject: [PATCH 0143/3533] unix: Make manifest selection match other ports. Changes are: - The default manifest.py is moved to the variants directory (it's in "boards" in other ports). - The coverage variant now uses a custom manifest in its variant directory to add frzmpy/frzstr. - The frzmpy/frzstr tests are moved to variants/coverage/. --- ports/unix/Makefile | 2 +- ports/unix/manifest_coverage.py | 2 -- .../{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy1.py | 0 .../{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy2.py | 0 .../coverage/frzmpy}/frzmpy_pkg1/__init__.py | 0 .../coverage/frzmpy}/frzmpy_pkg2/mod.py | 0 .../{coverage-frzmpy => variants/coverage/frzmpy}/frzqstr.py | 0 .../{coverage-frzstr => variants/coverage/frzstr}/frzstr1.py | 0 .../coverage/frzstr}/frzstr_pkg1/__init__.py | 0 .../coverage/frzstr}/frzstr_pkg2/mod.py | 0 ports/unix/variants/coverage/manifest.py | 2 ++ ports/unix/variants/coverage/mpconfigvariant.mk | 2 +- ports/unix/{ => variants}/manifest.py | 0 13 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 ports/unix/manifest_coverage.py rename ports/unix/{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy1.py (100%) rename ports/unix/{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy2.py (100%) rename ports/unix/{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy_pkg1/__init__.py (100%) rename ports/unix/{coverage-frzmpy => variants/coverage/frzmpy}/frzmpy_pkg2/mod.py (100%) rename ports/unix/{coverage-frzmpy => variants/coverage/frzmpy}/frzqstr.py (100%) rename ports/unix/{coverage-frzstr => variants/coverage/frzstr}/frzstr1.py (100%) rename ports/unix/{coverage-frzstr => variants/coverage/frzstr}/frzstr_pkg1/__init__.py (100%) rename ports/unix/{coverage-frzstr => variants/coverage/frzstr}/frzstr_pkg2/mod.py (100%) create mode 100644 ports/unix/variants/coverage/manifest.py rename ports/unix/{ => variants}/manifest.py (100%) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index ec14166149ba8..3e2fa63a16c3b 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -14,7 +14,7 @@ include ../../py/mkenv.mk include $(VARIANT_DIR)/mpconfigvariant.mk # use FROZEN_MANIFEST for new projects, others are legacy -FROZEN_MANIFEST ?= manifest.py +FROZEN_MANIFEST ?= variants/manifest.py FROZEN_DIR = FROZEN_MPY_DIR = diff --git a/ports/unix/manifest_coverage.py b/ports/unix/manifest_coverage.py deleted file mode 100644 index 0c32d08578e94..0000000000000 --- a/ports/unix/manifest_coverage.py +++ /dev/null @@ -1,2 +0,0 @@ -freeze_as_str('coverage-frzstr') -freeze_as_mpy('coverage-frzmpy') diff --git a/ports/unix/coverage-frzmpy/frzmpy1.py b/ports/unix/variants/coverage/frzmpy/frzmpy1.py similarity index 100% rename from ports/unix/coverage-frzmpy/frzmpy1.py rename to ports/unix/variants/coverage/frzmpy/frzmpy1.py diff --git a/ports/unix/coverage-frzmpy/frzmpy2.py b/ports/unix/variants/coverage/frzmpy/frzmpy2.py similarity index 100% rename from ports/unix/coverage-frzmpy/frzmpy2.py rename to ports/unix/variants/coverage/frzmpy/frzmpy2.py diff --git a/ports/unix/coverage-frzmpy/frzmpy_pkg1/__init__.py b/ports/unix/variants/coverage/frzmpy/frzmpy_pkg1/__init__.py similarity index 100% rename from ports/unix/coverage-frzmpy/frzmpy_pkg1/__init__.py rename to ports/unix/variants/coverage/frzmpy/frzmpy_pkg1/__init__.py diff --git a/ports/unix/coverage-frzmpy/frzmpy_pkg2/mod.py b/ports/unix/variants/coverage/frzmpy/frzmpy_pkg2/mod.py similarity index 100% rename from ports/unix/coverage-frzmpy/frzmpy_pkg2/mod.py rename to ports/unix/variants/coverage/frzmpy/frzmpy_pkg2/mod.py diff --git a/ports/unix/coverage-frzmpy/frzqstr.py b/ports/unix/variants/coverage/frzmpy/frzqstr.py similarity index 100% rename from ports/unix/coverage-frzmpy/frzqstr.py rename to ports/unix/variants/coverage/frzmpy/frzqstr.py diff --git a/ports/unix/coverage-frzstr/frzstr1.py b/ports/unix/variants/coverage/frzstr/frzstr1.py similarity index 100% rename from ports/unix/coverage-frzstr/frzstr1.py rename to ports/unix/variants/coverage/frzstr/frzstr1.py diff --git a/ports/unix/coverage-frzstr/frzstr_pkg1/__init__.py b/ports/unix/variants/coverage/frzstr/frzstr_pkg1/__init__.py similarity index 100% rename from ports/unix/coverage-frzstr/frzstr_pkg1/__init__.py rename to ports/unix/variants/coverage/frzstr/frzstr_pkg1/__init__.py diff --git a/ports/unix/coverage-frzstr/frzstr_pkg2/mod.py b/ports/unix/variants/coverage/frzstr/frzstr_pkg2/mod.py similarity index 100% rename from ports/unix/coverage-frzstr/frzstr_pkg2/mod.py rename to ports/unix/variants/coverage/frzstr/frzstr_pkg2/mod.py diff --git a/ports/unix/variants/coverage/manifest.py b/ports/unix/variants/coverage/manifest.py new file mode 100644 index 0000000000000..6111050884222 --- /dev/null +++ b/ports/unix/variants/coverage/manifest.py @@ -0,0 +1,2 @@ +freeze_as_str("frzstr") +freeze_as_mpy("frzmpy") diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index ddb5027a97ef7..66e694e0a9ea0 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -11,7 +11,7 @@ CFLAGS += \ LDFLAGS += -fprofile-arcs -ftest-coverage -FROZEN_MANIFEST = manifest_coverage.py +FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_VFS_FAT = 1 diff --git a/ports/unix/manifest.py b/ports/unix/variants/manifest.py similarity index 100% rename from ports/unix/manifest.py rename to ports/unix/variants/manifest.py From 4050281311744ae5946aa1faec8aa75339bcab2b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 9 Jun 2020 16:45:30 +1000 Subject: [PATCH 0144/3533] unix: Enable uasyncio on dev variant. --- ports/unix/variants/dev/manifest.py | 3 +++ ports/unix/variants/dev/mpconfigvariant.h | 4 ++++ ports/unix/variants/dev/mpconfigvariant.mk | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 ports/unix/variants/dev/manifest.py diff --git a/ports/unix/variants/dev/manifest.py b/ports/unix/variants/dev/manifest.py new file mode 100644 index 0000000000000..92a681116a687 --- /dev/null +++ b/ports/unix/variants/dev/manifest.py @@ -0,0 +1,3 @@ +include("$(PORT_DIR)/variants/manifest.py") + +include("$(MPY_DIR)/extmod/uasyncio/manifest.py") diff --git a/ports/unix/variants/dev/mpconfigvariant.h b/ports/unix/variants/dev/mpconfigvariant.h index eb65134717c6d..c2b959572abaa 100644 --- a/ports/unix/variants/dev/mpconfigvariant.h +++ b/ports/unix/variants/dev/mpconfigvariant.h @@ -30,3 +30,7 @@ #define MICROPY_PY_SYS_SETTRACE (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) + +#ifndef MICROPY_PY_UASYNCIO +#define MICROPY_PY_UASYNCIO (1) +#endif diff --git a/ports/unix/variants/dev/mpconfigvariant.mk b/ports/unix/variants/dev/mpconfigvariant.mk index bbd30b6238248..8a63b233d814e 100644 --- a/ports/unix/variants/dev/mpconfigvariant.mk +++ b/ports/unix/variants/dev/mpconfigvariant.mk @@ -1,5 +1,7 @@ PROG ?= micropython-dev +FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py + MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_PY_BLUETOOTH = 1 From 9dfb4ae6aacd5eb4317f7a474b9da00c1cec9088 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 25 Jun 2020 22:55:40 +0200 Subject: [PATCH 0145/3533] nrf/bluetooth/ble_uart: Fix implicit declaration of function. mp_keyboard_interrupt() triggers a compiler error because the function is implicitly declared. This commit adds "py/runtime.h" to the includes. Fixes issue #5732. --- ports/nrf/drivers/bluetooth/ble_uart.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index b94780e26011b..f6bc7f719c63c 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -31,6 +31,7 @@ #include "ringbuffer.h" #include "mphalport.h" #include "lib/utils/interrupt_char.h" +#include "py/runtime.h" #if MICROPY_PY_BLE_NUS From fc1f22a097ac2c6eb418605f36b53936e5138451 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 25 Jun 2020 23:06:00 +0200 Subject: [PATCH 0146/3533] nrf/bluetooth: Handle data length update request. The Bluetooth link gets disconnected when connecting from a PC after 30-40 seconds. This commit adds handling of the data length update request. The data length parameter pointer is set to NULL in the reply, letting the SoftDevice automatically set values and use them in the data length update procedure. --- ports/nrf/drivers/bluetooth/ble_drv.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c index 47f7f98885a91..1a64cdfbd24b9 100644 --- a/ports/nrf/drivers/bluetooth/ble_drv.c +++ b/ports/nrf/drivers/bluetooth/ble_drv.c @@ -1131,6 +1131,12 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { BLE_DRIVER_LOG("GATTS EVT EXCHANGE MTU REQUEST\n"); (void)sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, 23); // MAX MTU size break; + + case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST: + BLE_DRIVER_LOG("BLE GAP EVT DATA LENGTH UPDATE REQUEST\n"); + sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, NULL, NULL); + break; + #endif // (BLUETOOTH_SD == 132) || (BLUETOOTH_SD == 140) default: From ab0c14dba014d0ac27b8a5472bd795809a9be885 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 25 Jun 2020 23:32:28 +0200 Subject: [PATCH 0147/3533] nrf/bluetooth/ble_uart: Add mp_hal_stdio_poll function. This adds support for enabling MICROPY_PY_SYS_STDFILES when running UART over Bluetooth (NUS). --- ports/nrf/drivers/bluetooth/ble_uart.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index f6bc7f719c63c..86cd084746cd0 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -33,6 +33,10 @@ #include "lib/utils/interrupt_char.h" #include "py/runtime.h" +#if MICROPY_PY_SYS_STDFILES +#include "py/stream.h" +#endif + #if MICROPY_PY_BLE_NUS static ubluepy_uuid_obj_t uuid_obj_service = { @@ -136,6 +140,17 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { mp_hal_stdout_tx_strn(str, len); } +#if MICROPY_PY_SYS_STDFILES +uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { + uintptr_t ret = 0; + if ((poll_flags & MP_STREAM_POLL_RD) && ble_uart_enabled() + && !isBufferEmpty(mp_rx_ring_buffer)) { + ret |= MP_STREAM_POLL_RD; + } + return ret; +} +#endif + STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) { ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in); From 5996bf72f134dd8f1b72ecd20757dcb1c98cfc89 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 25 Jun 2020 23:41:35 +0200 Subject: [PATCH 0148/3533] nrf/bluetooth/ble_uart: Fix random advertisement name. The storage space of the advertisement name is not declared static, leading to a random advertisement name. This commit fixes the issue by declaring it static. --- ports/nrf/drivers/bluetooth/ble_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index 86cd084746cd0..b2e79ba44d65a 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -228,7 +228,7 @@ void ble_uart_init0(void) { ble_uart_peripheral.conn_handle = 0xFFFF; - char device_name[] = "mpus"; + static char device_name[] = "mpus"; mp_obj_t service_list = mp_obj_new_list(0, NULL); mp_obj_list_append(service_list, MP_OBJ_FROM_PTR(&ble_uart_service)); From f22f7b285e69aa05ce2dcf1e26de626dd1b1599d Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 25 Jun 2020 23:49:12 +0200 Subject: [PATCH 0149/3533] nrf/bluetooth/ble_uart: Swap end character on cooked strings. Changing line ending character of cooked strings makes rshell/pyboard.py work correctly over Bluetooth socat/pts devices. --- ports/nrf/drivers/bluetooth/ble_uart.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index b2e79ba44d65a..64e3a05f93192 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -136,8 +136,25 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { } } +void ble_uart_tx_char(char c) { + // Not connected: drop output + if (!ble_uart_enabled()) return; + + ubluepy_characteristic_obj_t * p_char = &ble_uart_char_tx; + + ble_drv_attr_s_notify(p_char->p_service->p_periph->conn_handle, + p_char->handle, + 1, + (uint8_t *)&c); +} + void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { - mp_hal_stdout_tx_strn(str, len); + for (const char *top = str + len; str < top; str++) { + if (*str == '\n') { + ble_uart_tx_char('\r'); + } + ble_uart_tx_char(*str); + } } #if MICROPY_PY_SYS_STDFILES From c2317a3a8d5f184de2f816078d91be699274b94e Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sat, 27 Jun 2020 13:07:39 +0200 Subject: [PATCH 0150/3533] nrf/Makefile: Disable ROM text compression when compiling for debug. When compiling for debug (-O0) the .text segment cannot fit the flash region when MICROPY_ROM_TEXT_COMPRESSION=1, because the compiler does not optimise away the large if-else chain used to select the correct compressed string. This commit enforces MICROPY_ROM_TEXT_COMPRESSION=0 when compiling for debug (DEBUG=1). --- ports/nrf/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 23d5cd20d0d00..8f73336e0b1e8 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -39,7 +39,9 @@ endif QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h # MicroPython feature configurations +ifeq ($(DEBUG), 0) MICROPY_ROM_TEXT_COMPRESSION ?= 1 +endif # include py core make definitions include ../../py/py.mk From 15574cd665a8e66157aa97447e3869513da81d45 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Thu, 2 Jul 2020 22:19:00 +0200 Subject: [PATCH 0151/3533] nrf: Add support for time.ticks_xxx functions using RTC1. This commit adds time.ticks_ms/us support using RTC1 as the timebase. It also adds the time.ticks_add/diff helper functions. This feature can be enabled using MICROPY_PY_TIME_TICKS. If disabled the system uses the legacy sleep methods and does not have any ticks functions. In addition support for MICROPY_EVENT_POLL_HOOK was added to the time.sleep_ms(x) function, making this function more power efficient and allows support for select.poll/asyncio. To support this, the RTC's CCR0 was used to schedule a ~1msec event to wakeup the CPU. Some important notes about the RTC timebase: - Since the granularity of RTC1's ticks are approx 30usec, time.ticks_us is not perfect, does not have 1us resolution, but is otherwise quite usable. For tighter measurments the ticker's 1MHz counter should be used. - time.ticks_ms(x) should *not* be called in an IRQ with higher prio than the RTC overflow irq (3). If so it introduces a race condition and possibly leads to wrong tick calculations. See #6171 and #6202. --- ports/nrf/main.c | 5 + ports/nrf/modules/machine/rtcounter.c | 7 ++ ports/nrf/modules/utime/modutime.c | 4 + ports/nrf/mpconfigport.h | 10 ++ ports/nrf/mphalport.c | 143 +++++++++++++++++++++++++- ports/nrf/mphalport.h | 13 ++- 6 files changed, 174 insertions(+), 8 deletions(-) diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 3c5d0a05d87f1..670c88e7cad9d 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -52,6 +52,8 @@ #include "i2c.h" #include "adc.h" #include "rtcounter.h" +#include "mphalport.h" + #if MICROPY_PY_MACHINE_HW_PWM #include "pwm.h" #endif @@ -101,6 +103,9 @@ int main(int argc, char **argv) { soft_reset: + #if MICROPY_PY_TIME_TICKS + rtc1_init_time_ticks(); + #endif led_init(); diff --git a/ports/nrf/modules/machine/rtcounter.c b/ports/nrf/modules/machine/rtcounter.c index 5fb28557d8230..c9f907774e6a3 100644 --- a/ports/nrf/modules/machine/rtcounter.c +++ b/ports/nrf/modules/machine/rtcounter.c @@ -153,6 +153,13 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s int rtc_id = rtc_find(args[ARG_id].u_obj); + #if MICROPY_PY_TIME_TICKS + if (rtc_id == 1) { + // time module uses RTC1, prevent using it + mp_raise_ValueError(MP_ERROR_TEXT("RTC1 reserved by time module")); + } + #endif + // const and non-const part of the RTC object. const machine_rtc_obj_t * self = &machine_rtc_obj[rtc_id]; machine_rtc_config_t *config = self->config; diff --git a/ports/nrf/modules/utime/modutime.c b/ports/nrf/modules/utime/modutime.c index 60cdbe4f33140..bb29141011eab 100644 --- a/ports/nrf/modules/utime/modutime.c +++ b/ports/nrf/modules/utime/modutime.c @@ -42,6 +42,10 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 28076114fe42a..fc34a7cf268df 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -174,6 +174,9 @@ #define MICROPY_PY_MACHINE_RTCOUNTER (0) #endif +#ifndef MICROPY_PY_TIME_TICKS +#define MICROPY_PY_TIME_TICKS (0) +#endif #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0) @@ -317,6 +320,13 @@ extern const struct _mp_obj_module_t ble_module; /* micro:bit root pointers */ \ void *async_data[2]; \ +#define MICROPY_EVENT_POLL_HOOK \ + do { \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ + __WFI(); \ + } while (0); + #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) // We need to provide a declaration/definition of alloca() diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 4469adc80d84f..b8e4c2e4d8b55 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -35,6 +35,121 @@ #include "nrfx_errors.h" #include "nrfx_config.h" +#if MICROPY_PY_TIME_TICKS +#include "nrfx_rtc.h" +#include "nrf_clock.h" +#endif + +#if MICROPY_PY_TIME_TICKS + +// Use RTC1 for time ticks generation (ms and us) with 32kHz tick resolution +// and overflow handling in RTC IRQ. + +#define RTC_TICK_INCREASE_MSEC (33) + +#define RTC_RESCHEDULE_CC(rtc, cc_nr, ticks) \ + do { \ + nrfx_rtc_cc_set(&rtc, cc_nr, nrfx_rtc_counter_get(&rtc) + ticks, true); \ + } while (0); + +// RTC overflow irq handling notes: +// - If has_overflowed is set it could be before or after COUNTER is read. +// If before then an adjustment must be made, if after then no adjustment is necessary. +// - The before case is when COUNTER is very small (because it just overflowed and was set to zero), +// the after case is when COUNTER is very large (because it's just about to overflow +// but we read it right before it overflows). +// - The extra check for counter is to distinguish these cases. 1<<23 because it's halfway +// between min and max values of COUNTER. +#define RTC1_GET_TICKS_ATOMIC(rtc, overflows, counter) \ + do { \ + rtc.p_reg->INTENCLR = RTC_INTENCLR_OVRFLW_Msk; \ + overflows = rtc_overflows; \ + counter = rtc.p_reg->COUNTER; \ + uint32_t has_overflowed = rtc.p_reg->EVENTS_OVRFLW; \ + if (has_overflowed && counter < (1 << 23)) { \ + overflows += 1; \ + } \ + rtc.p_reg->INTENSET = RTC_INTENSET_OVRFLW_Msk; \ + } while (0); + +nrfx_rtc_t rtc1 = NRFX_RTC_INSTANCE(1); +volatile mp_uint_t rtc_overflows = 0; + +const nrfx_rtc_config_t rtc_config_time_ticks = { + .prescaler = 0, + .reliable = 0, + .tick_latency = 0, + #ifdef NRF51 + .interrupt_priority = 1, + #else + .interrupt_priority = 3, + #endif +}; + +STATIC void rtc_irq_time(nrfx_rtc_int_type_t event) { + // irq handler for overflow + if (event == NRFX_RTC_INT_OVERFLOW) { + rtc_overflows += 1; + } + // irq handler for wakeup from WFI (~1msec) + if (event == NRFX_RTC_INT_COMPARE0) { + RTC_RESCHEDULE_CC(rtc1, 0, RTC_TICK_INCREASE_MSEC) + } +} + +void rtc1_init_time_ticks(void) { + // Start the low-frequency clock (if it hasn't been started already) + if (!nrf_clock_lf_is_running(NRF_CLOCK)) { + nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART); + } + // Uninitialize first, then set overflow IRQ and first CC event + nrfx_rtc_uninit(&rtc1); + nrfx_rtc_init(&rtc1, &rtc_config_time_ticks, rtc_irq_time); + nrfx_rtc_overflow_enable(&rtc1, true); + RTC_RESCHEDULE_CC(rtc1, 0, RTC_TICK_INCREASE_MSEC) + nrfx_rtc_enable(&rtc1); +} + +mp_uint_t mp_hal_ticks_ms(void) { + // Compute: (rtc_overflows << 24 + COUNTER) * 1000 / 32768 + // + // Note that COUNTER * 1000 / 32768 would overflow during calculation, so use + // the less obvious * 125 / 4096 calculation (overflow secure). + // + // Make sure not to call this function within an irq with higher prio than the + // RTC's irq. This would introduce the danger of preempting the RTC irq and + // calling mp_hal_ticks_ms() at that time would return a false result. + uint32_t overflows; + uint32_t counter; + // guard against overflow irq + RTC1_GET_TICKS_ATOMIC(rtc1, overflows, counter) + return (overflows << 9) * 1000 + (counter * 125 / 4096); +} + +mp_uint_t mp_hal_ticks_us(void) { + // Compute: ticks_us = (overflows << 24 + counter) * 1000000 / 32768 + // = (overflows << 15 * 15625) + (counter * 15625 / 512) + // Since this function is likely to be called in a poll loop it must + // be fast, using an optimized 64bit mult/divide. + uint32_t overflows; + uint32_t counter; + // guard against overflow irq + RTC1_GET_TICKS_ATOMIC(rtc1, overflows, counter) + // first compute counter * 15625 + uint32_t counter_lo = (counter & 0xffff) * 15625; + uint32_t counter_hi = (counter >> 16) * 15625; + // actual value is counter_hi << 16 + counter_lo + return ((overflows << 15) * 15625) + ((counter_hi << 7) + (counter_lo >> 9)); +} + +#else + +mp_uint_t mp_hal_ticks_ms(void) { + return 0; +} + +#endif + // this table converts from HAL_StatusTypeDef to POSIX errno const byte mp_hal_status_to_errno_table[4] = { [HAL_OK] = 0, @@ -70,7 +185,7 @@ int mp_hal_stdin_rx_chr(void) { if (MP_STATE_PORT(board_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(board_stdio_uart))) { return uart_rx_char(MP_STATE_PORT(board_stdio_uart)); } - __WFI(); + MICROPY_EVENT_POLL_HOOK } return 0; @@ -93,6 +208,31 @@ void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } +#if MICROPY_PY_TIME_TICKS + +void mp_hal_delay_us(mp_uint_t us) { + uint32_t now; + if (us == 0) { + return; + } + now = mp_hal_ticks_us(); + while (mp_hal_ticks_us() - now < us) { + } +} + +void mp_hal_delay_ms(mp_uint_t ms) { + uint32_t now; + if (ms == 0) { + return; + } + now = mp_hal_ticks_ms(); + while (mp_hal_ticks_ms() - now < ms) { + MICROPY_EVENT_POLL_HOOK + } +} + +#else + void mp_hal_delay_us(mp_uint_t us) { if (us == 0) { return; @@ -175,6 +315,7 @@ void mp_hal_delay_ms(mp_uint_t ms) { mp_hal_delay_us(999); } } +#endif #if defined(NRFX_LOG_ENABLED) && (NRFX_LOG_ENABLED == 1) diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 5614be29fa29f..15b37b7ef22d2 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -41,12 +41,6 @@ typedef enum HAL_TIMEOUT = 0x03 } HAL_StatusTypeDef; -static inline uint32_t hal_tick_fake(void) { - return 0; -} - -#define mp_hal_ticks_ms hal_tick_fake // TODO: implement. Right now, return 0 always - extern const unsigned char mp_hal_status_to_errno_table[4]; NORETURN void mp_hal_raise(HAL_StatusTypeDef status); @@ -70,10 +64,15 @@ const char *nrfx_error_code_lookup(uint32_t err_code); #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_open_drain(p) nrf_gpio_cfg_input(p->pin, NRF_GPIO_PIN_NOPULL) +#if MICROPY_PY_TIME_TICKS +void rtc1_init_time_ticks(); +#else +mp_uint_t mp_hal_ticks_ms(void); +#define mp_hal_ticks_us() (0) +#endif // TODO: empty implementation for now. Used by machine_spi.c:69 #define mp_hal_delay_us_fast(p) -#define mp_hal_ticks_us() (0) #define mp_hal_ticks_cpu() (0) #endif From 59ed3bdd9fa8dc6e0677cf8d4478a5a32fee4500 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Thu, 2 Jul 2020 22:19:11 +0200 Subject: [PATCH 0152/3533] nrf: Enable nrf tick support on all boards by default. Having time.ticks_ms/us/add/diff is very useful and used by many drivers, libraries and components. --- ports/nrf/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index fc34a7cf268df..c5304b20d0d3e 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -175,7 +175,7 @@ #endif #ifndef MICROPY_PY_TIME_TICKS -#define MICROPY_PY_TIME_TICKS (0) +#define MICROPY_PY_TIME_TICKS (1) #endif #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) From f5dd46b4791249c5989859b07c4886c69d8b98b3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 2 Jul 2020 16:12:59 +1000 Subject: [PATCH 0153/3533] unix/variants: Enable VFS and all supported filesystems on dev variant. So that micropython-dev can be used to test VFS code, and inspect and build filesystem images that are compatible with bare-metal systems. Signed-off-by: Damien George --- ports/unix/variants/dev/mpconfigvariant.h | 21 +++++++++++++++------ ports/unix/variants/dev/mpconfigvariant.mk | 4 +++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ports/unix/variants/dev/mpconfigvariant.h b/ports/unix/variants/dev/mpconfigvariant.h index c2b959572abaa..7c3e84cc44e11 100644 --- a/ports/unix/variants/dev/mpconfigvariant.h +++ b/ports/unix/variants/dev/mpconfigvariant.h @@ -24,13 +24,22 @@ * THE SOFTWARE. */ -#define MICROPY_REPL_EMACS_WORDS_MOVE (1) -#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (1) -#define MICROPY_ENABLE_SCHEDULER (1) +#define MICROPY_READER_VFS (1) +#define MICROPY_REPL_EMACS_WORDS_MOVE (1) +#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (1) +#define MICROPY_ENABLE_SCHEDULER (1) +#define MICROPY_VFS (1) +#define MICROPY_VFS_POSIX (1) -#define MICROPY_PY_SYS_SETTRACE (1) -#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) +#define MICROPY_PY_SYS_SETTRACE (1) +#define MICROPY_PY_UOS_VFS (1) +#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #ifndef MICROPY_PY_UASYNCIO -#define MICROPY_PY_UASYNCIO (1) +#define MICROPY_PY_UASYNCIO (1) #endif + +// Use vfs's functions for import stat and builtin open. +#define mp_import_stat mp_vfs_import_stat +#define mp_builtin_open mp_vfs_open +#define mp_builtin_open_obj mp_vfs_open_obj diff --git a/ports/unix/variants/dev/mpconfigvariant.mk b/ports/unix/variants/dev/mpconfigvariant.mk index 8a63b233d814e..1f8611b6fc30b 100644 --- a/ports/unix/variants/dev/mpconfigvariant.mk +++ b/ports/unix/variants/dev/mpconfigvariant.mk @@ -3,5 +3,7 @@ PROG ?= micropython-dev FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py MICROPY_ROM_TEXT_COMPRESSION = 1 - +MICROPY_VFS_FAT = 1 +MICROPY_VFS_LFS1 = 1 +MICROPY_VFS_LFS2 = 1 MICROPY_PY_BLUETOOTH = 1 From 8594389fe75efc3903970db319f07f89fdb8c8cc Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 30 Jun 2020 22:59:28 +0200 Subject: [PATCH 0154/3533] stm32/fdcan: Use the right FIFO to calc element address in can_receive. --- ports/stm32/fdcan.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ports/stm32/fdcan.c b/ports/stm32/fdcan.c index b3b1da998c79d..bafc7e3f5ad7f 100644 --- a/ports/stm32/fdcan.c +++ b/ports/stm32/fdcan.c @@ -218,8 +218,14 @@ int can_receive(FDCAN_HandleTypeDef *can, int fifo, FDCAN_RxHeaderTypeDef *hdr, } // Get pointer to incoming message - uint32_t index = (can->Instance->RXF0S & FDCAN_RXF0S_F0GI) >> 8; - uint32_t *address = (uint32_t *)(can->msgRam.RxFIFO0SA + (index * can->Init.RxFifo0ElmtSize * 4)); + uint32_t index, *address; + if (fifo == FDCAN_RX_FIFO0) { + index = (*rxf & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos; + address = (uint32_t *)(can->msgRam.RxFIFO0SA + (index * can->Init.RxFifo0ElmtSize * 4)); + } else { + index = (*rxf & FDCAN_RXF1S_F1GI) >> FDCAN_RXF1S_F1GI_Pos; + address = (uint32_t *)(can->msgRam.RxFIFO1SA + (index * can->Init.RxFifo1ElmtSize * 4)); + } // Parse header of message hdr->IdType = *address & FDCAN_ELEMENT_MASK_XTD; From 63b2eb27d44a9beb9f66abd77d3b6526fdb2482d Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 30 Jun 2020 23:04:57 +0200 Subject: [PATCH 0155/3533] stm32/fdcan: Use FDCAN_RXFxS_FxFL instead of hard-coded value. --- ports/stm32/fdcan.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/stm32/fdcan.c b/ports/stm32/fdcan.c index bafc7e3f5ad7f..3ab72cdace1a8 100644 --- a/ports/stm32/fdcan.c +++ b/ports/stm32/fdcan.c @@ -200,17 +200,21 @@ void can_clearfilter(pyb_can_obj_t *self, uint32_t f, uint8_t bank) { int can_receive(FDCAN_HandleTypeDef *can, int fifo, FDCAN_RxHeaderTypeDef *hdr, uint8_t *data, uint32_t timeout_ms) { volatile uint32_t *rxf, *rxa; + uint32_t fl; + if (fifo == FDCAN_RX_FIFO0) { rxf = &can->Instance->RXF0S; rxa = &can->Instance->RXF0A; + fl = FDCAN_RXF0S_F0FL; } else { rxf = &can->Instance->RXF1S; rxa = &can->Instance->RXF1A; + fl = FDCAN_RXF1S_F1FL; } // Wait for a message to become available, with timeout uint32_t start = HAL_GetTick(); - while ((*rxf & 7) == 0) { + while ((*rxf & fl) == 0) { MICROPY_EVENT_POLL_HOOK if (HAL_GetTick() - start >= timeout_ms) { return -MP_ETIMEDOUT; From d07073f4e2f37d9614340f7544985dfee834532e Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 30 Jun 2020 23:08:20 +0200 Subject: [PATCH 0156/3533] stm32/fdcan: Support maximum timeout of HAL_MAX_DELAY in can_receive. --- ports/stm32/fdcan.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ports/stm32/fdcan.c b/ports/stm32/fdcan.c index 3ab72cdace1a8..2892572f40a96 100644 --- a/ports/stm32/fdcan.c +++ b/ports/stm32/fdcan.c @@ -215,10 +215,12 @@ int can_receive(FDCAN_HandleTypeDef *can, int fifo, FDCAN_RxHeaderTypeDef *hdr, // Wait for a message to become available, with timeout uint32_t start = HAL_GetTick(); while ((*rxf & fl) == 0) { - MICROPY_EVENT_POLL_HOOK - if (HAL_GetTick() - start >= timeout_ms) { - return -MP_ETIMEDOUT; + if (timeout_ms != HAL_MAX_DELAY) { + if (HAL_GetTick() - start >= timeout_ms) { + return -MP_ETIMEDOUT; + } } + MICROPY_EVENT_POLL_HOOK } // Get pointer to incoming message From c299cc94e334a5a9576e15e0f046e24810c4c75a Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 30 Jun 2020 23:13:57 +0200 Subject: [PATCH 0157/3533] stm32/pyb_can: Handle timeout arg for FDCAN in pyb_can_send. Following the documented pyb can_send behavior in pyb.CAN docs. --- ports/stm32/pyb_can.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index ad2efbef4be1a..224b8f28b0eab 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -432,6 +432,20 @@ STATIC mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t * HAL_StatusTypeDef status; #if MICROPY_HW_ENABLE_FDCAN + uint32_t timeout_ms = args[ARG_timeout].u_int; + uint32_t start = HAL_GetTick(); + while (HAL_FDCAN_GetTxFifoFreeLevel(&self->can) == 0) { + if (timeout_ms == 0) { + mp_raise_OSError(MP_ETIMEDOUT); + } + // Check for the Timeout + if (timeout_ms != HAL_MAX_DELAY) { + if (HAL_GetTick() - start >= timeout_ms) { + mp_raise_OSError(MP_ETIMEDOUT); + } + } + MICROPY_EVENT_POLL_HOOK + } status = HAL_FDCAN_AddMessageToTxFifoQ(&self->can, &tx_msg, tx_data); #else self->can.pTxMsg = &tx_msg; From b6146ca1a11d9db6fa2276920f2c3c32d9bc4457 Mon Sep 17 00:00:00 2001 From: Thomas Friebel Date: Sun, 5 Jul 2020 16:48:27 +0200 Subject: [PATCH 0158/3533] extmod/nimble: Fix attr NULL ptr dereference in ble_gatt_attr_read_cb. In case of error, NimBLE calls the read callback with attr = NULL. --- extmod/nimble/modbluetooth_nimble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 4e0ca88efa468..be7d13d9e6f5d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -846,7 +846,7 @@ STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_err if (error->status == 0) { gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om); } - mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr->handle, error->status); + mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr ? attr->handle : -1, error->status); return 0; } From f743bd3d2581596ecc06973ffc52b429931b19fe Mon Sep 17 00:00:00 2001 From: Alex Tsamakos Date: Tue, 23 Jun 2020 02:17:17 +0200 Subject: [PATCH 0159/3533] nrf/boards: Add initial support for Actinius Icarus. Example make command: make BOARD=actinius_icarus --- ports/nrf/README.md | 2 + .../boards/actinius_icarus/mpconfigboard.h | 81 +++++++++++++++++++ .../boards/actinius_icarus/mpconfigboard.mk | 6 ++ ports/nrf/boards/actinius_icarus/pins.csv | 32 ++++++++ 4 files changed, 121 insertions(+) create mode 100644 ports/nrf/boards/actinius_icarus/mpconfigboard.h create mode 100644 ports/nrf/boards/actinius_icarus/mpconfigboard.mk create mode 100644 ports/nrf/boards/actinius_icarus/pins.csv diff --git a/ports/nrf/README.md b/ports/nrf/README.md index b5f39267ebf2f..a02548afe1f9a 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -43,6 +43,7 @@ This is a port of MicroPython to the Nordic Semiconductor nRF series of chips. * [Particle Xenon](https://docs.particle.io/xenon/) * nRF9160 * [PCA10090](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF9160-DK) + * [Actinius Icarus](https://www.actinius.com/icarus) ## Compile and Flash @@ -135,6 +136,7 @@ pca10056 | s140 | Peripheral and Central | [Segge pca10059 | s140 | Peripheral and Central | Manual, SWDIO and SWCLK solder points on the sides. particle_xenon | s140 | Peripheral and Central | [Black Magic Probe](#black-magic-probe-targets) pca10090 | None (bsdlib.a) | None (LTE/GNSS) | [Segger](#segger-targets) +actinius_icarus | None (bsdlib.a) | None (LTE/GNSS) | [Segger](#segger-targets) ## IDAP-M/IDAP-Link Targets diff --git a/ports/nrf/boards/actinius_icarus/mpconfigboard.h b/ports/nrf/boards/actinius_icarus/mpconfigboard.h new file mode 100644 index 0000000000000..e344d2d6d0968 --- /dev/null +++ b/ports/nrf/boards/actinius_icarus/mpconfigboard.h @@ -0,0 +1,81 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Actinius + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#define ACTINIUS_ICARUS + +#define MICROPY_HW_BOARD_NAME "Actinius Icarus" +#define MICROPY_HW_MCU_NAME "NRF9160" +#define MICROPY_PY_SYS_PLATFORM "nrf9160" + +#define MICROPY_PY_MACHINE_UART (1) +#define MICROPY_PY_MACHINE_HW_PWM (0) +#define MICROPY_PY_MACHINE_HW_SPI (1) +#define MICROPY_PY_MACHINE_TIMER (1) +#define MICROPY_PY_MACHINE_RTCOUNTER (0) +#define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_ADC (0) +#define MICROPY_PY_MACHINE_TEMP (0) +#define MICROPY_PY_RANDOM_HW_RNG (0) + +#define MICROPY_MBFS (0) +#define MICROPY_VFS (1) + +#define MICROPY_HW_HAS_LED (1) +#define MICROPY_HW_HAS_SWITCH (1) +#define MICROPY_HW_HAS_FLASH (0) +#define MICROPY_HW_HAS_SDCARD (0) +#define MICROPY_HW_HAS_MMA7660 (0) +#define MICROPY_HW_HAS_LIS3DSH (0) +#define MICROPY_HW_HAS_LCD (0) +#define MICROPY_HW_ENABLE_RNG (0) +#define MICROPY_HW_ENABLE_RTC (0) +#define MICROPY_HW_ENABLE_TIMER (0) +#define MICROPY_HW_ENABLE_SERVO (0) +#define MICROPY_HW_ENABLE_DAC (0) +#define MICROPY_HW_ENABLE_CAN (0) + +#define MICROPY_HW_LED_TRICOLOR (1) +#define MICROPY_HW_LED_PULLUP (1) + +#define MICROPY_HW_LED_RED (10) // LED1 / RED +#define MICROPY_HW_LED_GREEN (11) // LED2 / GREEN +#define MICROPY_HW_LED_BLUE (12) // LED3 / BLUE + +// UART config +#define MICROPY_HW_UART1_RX (6) +#define MICROPY_HW_UART1_TX (9) +#define MICROPY_HW_UART1_CTS (25) +#define MICROPY_HW_UART1_RTS (7) +#define MICROPY_HW_UART1_HWFC (1) + +// SPI0 config +#define MICROPY_HW_SPI0_NAME "SPI0" + +#define MICROPY_HW_SPI0_SCK (20) +#define MICROPY_HW_SPI0_MOSI (21) +#define MICROPY_HW_SPI0_MISO (22) + +#define HELP_TEXT_BOARD_LED "1,2,3" diff --git a/ports/nrf/boards/actinius_icarus/mpconfigboard.mk b/ports/nrf/boards/actinius_icarus/mpconfigboard.mk new file mode 100644 index 0000000000000..c1e05fd306272 --- /dev/null +++ b/ports/nrf/boards/actinius_icarus/mpconfigboard.mk @@ -0,0 +1,6 @@ +MCU_SERIES = m33 +MCU_VARIANT = nrf91 +MCU_SUB_VARIANT = nrf9160 +LD_FILES += boards/nrf9160_1M_256k.ld + +NRF_DEFINES += -DNRF9160_XXAA -DNRF_TRUSTZONE_NONSECURE diff --git a/ports/nrf/boards/actinius_icarus/pins.csv b/ports/nrf/boards/actinius_icarus/pins.csv new file mode 100644 index 0000000000000..50c284dfdc5cc --- /dev/null +++ b/ports/nrf/boards/actinius_icarus/pins.csv @@ -0,0 +1,32 @@ +P0,P0 +P1,P1 +P2,P2 +P3,P3 +P4,P4 +BUTTON,P5 +UART_RX,P6 +UART_RTS,P7 +SIM_SELECT,P8 +UART_TX,P9 +LED1,P10 +LED2,P11 +LED3,P12 +BAT_SENSE,P13 +A1,P14 +A2,P15 +A3,P16 +A4,P17 +A5,P18 +A6,P19 +SPI_SCK,P20 +SPI_MOSI,P21 +SPI_MISO,P22 +P23,P23 +P24,P24 +UART_CTS,P25 +I2C_SDA,P26 +I2C_SCL,P27 +ACCEL_INT1,P28 +ACCEL_INT2,P29 +P30,P30 +P31,P31 From b776fe69692e92497c7aae0f38dfcbe14b2ca024 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 9 Jul 2020 20:41:11 +0200 Subject: [PATCH 0160/3533] nrf/nrfx_config: Disable RTC2 for nRF9160 targets. nRF9160 does not have any RTC2. Disable the configuration in case of NRF9160_XXAA. --- ports/nrf/nrfx_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h index 19c744510feb5..beb6b34ab368d 100644 --- a/ports/nrf/nrfx_config.h +++ b/ports/nrf/nrfx_config.h @@ -130,7 +130,7 @@ #define NRFX_RTC_ENABLED (MICROPY_PY_MACHINE_RTCOUNTER) #define NRFX_RTC0_ENABLED 1 #define NRFX_RTC1_ENABLED 1 -#define NRFX_RTC2_ENABLED (!NRF51) +#define NRFX_RTC2_ENABLED (!NRF51) && (!NRF9160_XXAA) #define NRFX_TIMER_ENABLED (MICROPY_PY_MACHINE_TIMER) #define NRFX_TIMER0_ENABLED 1 From 95d0d1c48675c946a3c3dd1c2f9ab293f21bb943 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 9 Jul 2020 21:03:09 +0200 Subject: [PATCH 0161/3533] nrf/boards: Enable RTCounter machine module for nrf9160 boards. Resolves dependencies for MICROPY_PY_TIME_TICKS which requires to link against nrfx_rtc.c functions by setting MICROPY_PY_MACHINE_RTCOUNTER to 1. --- ports/nrf/boards/actinius_icarus/mpconfigboard.h | 2 +- ports/nrf/boards/pca10090/mpconfigboard.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/nrf/boards/actinius_icarus/mpconfigboard.h b/ports/nrf/boards/actinius_icarus/mpconfigboard.h index e344d2d6d0968..e99d731df941d 100644 --- a/ports/nrf/boards/actinius_icarus/mpconfigboard.h +++ b/ports/nrf/boards/actinius_icarus/mpconfigboard.h @@ -34,7 +34,7 @@ #define MICROPY_PY_MACHINE_HW_PWM (0) #define MICROPY_PY_MACHINE_HW_SPI (1) #define MICROPY_PY_MACHINE_TIMER (1) -#define MICROPY_PY_MACHINE_RTCOUNTER (0) +#define MICROPY_PY_MACHINE_RTCOUNTER (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_ADC (0) #define MICROPY_PY_MACHINE_TEMP (0) diff --git a/ports/nrf/boards/pca10090/mpconfigboard.h b/ports/nrf/boards/pca10090/mpconfigboard.h index e4e514290949b..28381c40d1e6f 100644 --- a/ports/nrf/boards/pca10090/mpconfigboard.h +++ b/ports/nrf/boards/pca10090/mpconfigboard.h @@ -34,7 +34,7 @@ #define MICROPY_PY_MACHINE_HW_PWM (0) #define MICROPY_PY_MACHINE_HW_SPI (1) #define MICROPY_PY_MACHINE_TIMER (0) -#define MICROPY_PY_MACHINE_RTCOUNTER (0) +#define MICROPY_PY_MACHINE_RTCOUNTER (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_ADC (0) #define MICROPY_PY_MACHINE_TEMP (0) From 411e1157e7ea9ee65db5e78bdd5d2ee577d92cc2 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Fri, 10 Jul 2020 19:55:35 +0200 Subject: [PATCH 0162/3533] travis: Install newer toolchain for nrf job. Update toolchain to GNU Arm Embedded Toolchain. --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f6fe3e1e4e39f..2f86345ae8bcb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -292,7 +292,10 @@ jobs: - stage: test name: "nrf port build" install: - - sudo apt-get install gcc-arm-none-eabi + # need newer gcc version for Cortex-M33 support + - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa + - sudo apt-get update -qq || true + - sudo apt-get install gcc-arm-embedded - sudo apt-get install libnewlib-arm-none-eabi - arm-none-eabi-gcc --version script: From d9e8edc8bcff7102d655b63c672ccb44adeca688 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Fri, 10 Jul 2020 09:19:34 +0200 Subject: [PATCH 0163/3533] travis: Add pca10090 build to nrf job. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2f86345ae8bcb..8eb5ef6bae514 100644 --- a/.travis.yml +++ b/.travis.yml @@ -304,6 +304,7 @@ jobs: - make ${MAKEOPTS} -C ports/nrf BOARD=pca10040 - make ${MAKEOPTS} -C ports/nrf BOARD=microbit - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s132 + - make ${MAKEOPTS} -C ports/nrf BOARD=pca10090 # bare-arm and minimal ports, with size-diff check - stage: test From 486cb6dd4a1a17ff2567f721855d4626fdf69412 Mon Sep 17 00:00:00 2001 From: Matt Trentini Date: Mon, 18 May 2020 14:45:46 +1000 Subject: [PATCH 0164/3533] nrf: Add board definition for nRF52840-MDK-USB-Dongle. --- ports/nrf/README.md | 1 + ports/nrf/boards/memory.ld | 7 +- .../boards/nrf52840-mdk-usb-dongle/README.md | 49 +++++++++++++ .../nrf52840-mdk-usb-dongle/mpconfigboard.h | 71 +++++++++++++++++++ .../nrf52840-mdk-usb-dongle/mpconfigboard.mk | 7 ++ .../nrf52840_open_bootloader.ld | 2 + .../boards/nrf52840-mdk-usb-dongle/pins.csv | 17 +++++ 7 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md create mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.h create mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk create mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld create mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/pins.csv diff --git a/ports/nrf/README.md b/ports/nrf/README.md index a02548afe1f9a..b08a034561e0c 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -41,6 +41,7 @@ This is a port of MicroPython to the Nordic Semiconductor nRF series of chips. * [PCA10056](http://www.nordicsemi.com/eng/Products/nRF52840-Preview-DK) * [PCA10059](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-Dongle) * [Particle Xenon](https://docs.particle.io/xenon/) + * [nRF52840 MDK USB Dongle](boards/nrf52840-mdk-usb-dongle/README.md) * nRF9160 * [PCA10090](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF9160-DK) * [Actinius Icarus](https://www.actinius.com/icarus) diff --git a/ports/nrf/boards/memory.ld b/ports/nrf/boards/memory.ld index c95daf3d94fa0..f1f9a2a4c48b8 100644 --- a/ports/nrf/boards/memory.ld +++ b/ports/nrf/boards/memory.ld @@ -1,14 +1,17 @@ /* Flash layout: softdevice | application | filesystem */ /* RAM layout: softdevice RAM | application RAM */ -_sd_size = DEFINED(_sd_size) ? _sd_size : 0; + +_ram_start = DEFINED(_ram_start) ? _ram_start : 0x20000000; +_flash_start = DEFINED(_flash_start) ? _flash_start : 0; +_sd_size = DEFINED(_sd_size) ? _sd_size : _flash_start; _sd_ram = DEFINED(_sd_ram) ? _sd_ram : 0; _fs_size = DEFINED(_fs_size) ? _fs_size : 64K; /* TODO: set to 0 if not using the filesystem */ _app_size = _flash_size - _sd_size - _fs_size; _app_start = _sd_size; _fs_start = _sd_size + _app_size; _fs_end = _fs_start + _fs_size; -_app_ram_start = 0x20000000 + _sd_ram; +_app_ram_start = _ram_start + _sd_ram; _app_ram_size = _ram_size - _sd_ram; _heap_start = _ebss; _heap_end = _ram_end - _stack_size; diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md b/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md new file mode 100644 index 0000000000000..c39a800d776cf --- /dev/null +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md @@ -0,0 +1,49 @@ +nRF52840 MDK USB Dongle +======================= + +The *[nRF52840 MDK USB +Dongle](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle)* is a small, +low-cost development board in a USB dongle form-factor powered by an nRF52840 +with 1MB flash and 256KB RAM. + +This device is pre-installed with [Open +Bootloader](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/programming/), +allowing DFU upgrades over USB using Nordic [nRF +Connect](https://www.nordicsemi.com/Software-and-tools/Development-Tools/nRF-Connect-for-desktop) +or [nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil/). To support +Open Bootloader, the flash and memory layout must be adjusted slightly (details +[here](https://devzone.nordicsemi.com/nordic/short-range-guides/b/getting-started/posts/nrf52840-dongle-programming-tutorial)) +from the typical nRF build; this board definition ensure the appropriate build +configuration is used for MicroPython. + + +Pinout +------ + +The [pinout +diagram](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/#pinout-diagram) +provides an overview of the available pins and their capabilities. All pins are +available in MicroPython, using the pin numbers labelled in the diagram +(excluding the leading port number, *P0*). + +The three LEDs are available either through the usual `Pin` mechanism - pins +22-24 - or by `board.LED(n)` where n can be 1, 2 or 3. + + +Build instructions +------------------ + +Follow the standard [nRF Port build instructions](../../README.md); but use +`nrf52840-mdk-usb-dongle` as the value for `BOARD`: + + make BOARD=nrf52840-mdk-usb-dongle + +The build artifacts will be created in `build-nrf52840-mdk-usb-dongle`. Once +built, the easiest way to deploy to the device is to open `firmware.hex` using +*nRF Connect* and select *Write*. Detailed instructions can be found on the +[developer +wiki](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/programming/). + +**Note** that the regular method of deployment for the MicroPython nRF port +(using `make deploy`) will *not* operate correctly and will overwrite the +bootloader. diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.h b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.h new file mode 100644 index 0000000000000..f048c39e0efdd --- /dev/null +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.h @@ -0,0 +1,71 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Matt Trentini + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#define MICROPY_HW_BOARD_NAME "MDK-USB-DONGLE" +#define MICROPY_HW_MCU_NAME "NRF52840" +#define MICROPY_PY_SYS_PLATFORM "nrf52840-MDK-USB-Dongle" + +#define MICROPY_PY_MACHINE_UART (1) +#define MICROPY_PY_MACHINE_HW_PWM (1) +#define MICROPY_PY_MACHINE_HW_SPI (1) +#define MICROPY_PY_MACHINE_TIMER (1) +#define MICROPY_PY_MACHINE_RTCOUNTER (1) +#define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_TEMP (1) + +#define MICROPY_HW_ENABLE_RNG (1) + +#define MICROPY_HW_USB_CDC (1) + +#define MICROPY_HW_HAS_LED (1) +#define MICROPY_HW_LED_COUNT (3) +#define MICROPY_HW_LED_PULLUP (1) + +#define MICROPY_HW_LED1 (22) // LED1 GREEN +#define MICROPY_HW_LED2 (23) // LED2 RED +#define MICROPY_HW_LED3 (24) // LED3 BLUE + +// UART config +#define MICROPY_HW_UART1_RX (7) +#define MICROPY_HW_UART1_TX (8) +#define MICROPY_HW_UART1_CTS (9) +#define MICROPY_HW_UART1_RTS (10) +#define MICROPY_HW_UART1_HWFC (1) + +// SPI0 config +#define MICROPY_HW_SPI0_NAME "SPI0" + +#define MICROPY_HW_SPI0_SCK (19) +#define MICROPY_HW_SPI0_MOSI (20) +#define MICROPY_HW_SPI0_MISO (21) + +#define MICROPY_HW_PWM0_NAME "PWM0" +#define MICROPY_HW_PWM1_NAME "PWM1" +#define MICROPY_HW_PWM2_NAME "PWM2" +#define MICROPY_HW_PWM3_NAME "PWM3" + +#define HELP_TEXT_BOARD_LED "1,2,3" diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk new file mode 100644 index 0000000000000..f98d5c88a9076 --- /dev/null +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk @@ -0,0 +1,7 @@ +MCU_SERIES = m4 +MCU_VARIANT = nrf52 +MCU_SUB_VARIANT = nrf52840 +SOFTDEV_VERSION = 6.1.1 +LD_FILES += boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld boards/nrf52840_1M_256k.ld + +NRF_DEFINES += -DNRF52840_XXAA diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld b/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld new file mode 100644 index 0000000000000..d6c6e743a4daf --- /dev/null +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld @@ -0,0 +1,2 @@ +_ram_start = 0x20000008; +_flash_start = 0x1000; diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/pins.csv b/ports/nrf/boards/nrf52840-mdk-usb-dongle/pins.csv new file mode 100644 index 0000000000000..57162863778eb --- /dev/null +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/pins.csv @@ -0,0 +1,17 @@ +P2,P2,ADC1_CH0 +P3,P3,ADC1_CH1 +P4,P4,ADC1_CH2 +P5,P5,ADC1_CH3 +P6,P6 +P7,P7 +P8,P8 +P9,P9 +P10,P10 +USER,P18,P18 +P19,P19 +P20,P20 +P21,P21 +LED_G,P22,P22 +LED_R,P23,P23 +LED_B,P24,P24 +P25,P25 From 342800c9a24e4a6b6453d3e0a7b96ddfb2ed33db Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 16 Jul 2020 12:10:45 +0200 Subject: [PATCH 0165/3533] travis: Change nrf pca10056 board to build with s140 SoftDevice. It might compile fine with S132 as SoftDevice for nRF52840. However, there might be different hardware on the SoC which in turn could make it fail. SoftDevice S140 is correct BLE stack for nRF52840 SoC and would provide a more accurate test build. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8eb5ef6bae514..48ce39f60ad89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -299,11 +299,11 @@ jobs: - sudo apt-get install libnewlib-arm-none-eabi - arm-none-eabi-gcc --version script: - - ports/nrf/drivers/bluetooth/download_ble_stack.sh s132_nrf52_6_1_1 + - ports/nrf/drivers/bluetooth/download_ble_stack.sh s140_nrf52_6_1_1 - make ${MAKEOPTS} -C ports/nrf submodules - make ${MAKEOPTS} -C ports/nrf BOARD=pca10040 - make ${MAKEOPTS} -C ports/nrf BOARD=microbit - - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s132 + - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s140 - make ${MAKEOPTS} -C ports/nrf BOARD=pca10090 # bare-arm and minimal ports, with size-diff check From 5d0be97bd9d8e9511852d2917fe0c6560232e85e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 17 Jun 2020 15:10:55 +1000 Subject: [PATCH 0166/3533] unix: Make the MICROPY_xxx_ATOMIC_SECTION mutex recursive. This mutex is used to make the unix port behave more like bare metal, i.e. it allows "IRQ handlers" to run exclusively by making the mutex recursive. --- ports/unix/btstack_usb.c | 3 +++ ports/unix/mpthreadport.c | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/unix/btstack_usb.c b/ports/unix/btstack_usb.c index 76f32d6d27cfa..ab6a49f39ae73 100644 --- a/ports/unix/btstack_usb.c +++ b/ports/unix/btstack_usb.c @@ -156,7 +156,10 @@ STATIC void *btstack_thread(void *arg) { // Or, if a timeout results in it being set to TIMEOUT. while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { + // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); btstack_run_loop_embedded_execute_once(); + MICROPY_END_ATOMIC_SECTION(atomic_state); // The USB transport schedules events to the run loop at 1ms intervals, // and the implementation currently polls rather than selects. diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c index 711cf2a6bbffa..de0f5923bafe9 100644 --- a/ports/unix/mpthreadport.c +++ b/ports/unix/mpthreadport.c @@ -65,7 +65,7 @@ STATIC pthread_key_t tls_key; // The mutex is used for any code in this port that needs to be thread safe. // Specifically for thread management, access to the linked list is one example. // But also, e.g. scheduler state. -STATIC pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; +STATIC pthread_mutex_t thread_mutex; STATIC thread_t *thread; // this is used to synchronise the signal handler of the thread @@ -111,6 +111,13 @@ void mp_thread_init(void) { pthread_key_create(&tls_key, NULL); pthread_setspecific(tls_key, &mp_state_ctx.thread); + // Needs to be a recursive mutex to emulate the behavior of + // BEGIN_ATOMIC_SECTION on bare metal. + pthread_mutexattr_t thread_mutex_attr; + pthread_mutexattr_init(&thread_mutex_attr); + pthread_mutexattr_settype(&thread_mutex_attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&thread_mutex, &thread_mutex_attr); + // create first entry in linked list of all threads thread = malloc(sizeof(thread_t)); thread->id = pthread_self(); From 07aec4681f6ea5173315e660c6ba39cd12a2aa58 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 17 Jun 2020 13:54:31 +1000 Subject: [PATCH 0167/3533] examples/bluetooth: In ble_advertising.py, skip appearance if not set. --- examples/bluetooth/ble_advertising.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/bluetooth/ble_advertising.py b/examples/bluetooth/ble_advertising.py index a496119967a5d..eed527f55d210 100644 --- a/examples/bluetooth/ble_advertising.py +++ b/examples/bluetooth/ble_advertising.py @@ -47,7 +47,8 @@ def _append(adv_type, value): _append(_ADV_TYPE_UUID128_COMPLETE, b) # See org.bluetooth.characteristic.gap.appearance.xml - _append(_ADV_TYPE_APPEARANCE, struct.pack(" Date: Tue, 16 Jun 2020 21:53:22 +1000 Subject: [PATCH 0168/3533] extmod/btstack: Schedule notify/indicate/write ops for bg completion. The goal of this commit is to allow using ble.gatts_notify() at any time, even if the stack is not ready to send the notification right now. It also addresses the same issue for ble.gatts_indicate() and ble.gattc_write() (without response). In addition this commit fixes the case where the buffer passed to write-with-response wasn't copied, meaning it could be modified by the caller, affecting the in-progress write. The changes are: - gatts_notify/indicate will now run in the background if the ACL buffer is currently full, meaning that notify/indicate can be called at any time. - gattc_write(mode=0) (no response) will now allow for one outstanding write. - gattc_write(mode=1) (with response) will now copy the buffer so that it can't be modified by the caller while the write is in progress. All four paths also now track the buffer while the operation is in progress, which prevents the GC free'ing the buffer while it's still needed. --- extmod/btstack/modbluetooth_btstack.c | 284 ++++++++++++++++-- extmod/btstack/modbluetooth_btstack.h | 3 + extmod/modbluetooth.c | 85 ++++-- extmod/modbluetooth.h | 2 +- extmod/nimble/modbluetooth_nimble.c | 6 +- tests/multi_bluetooth/ble_characteristic.py | 24 ++ .../multi_bluetooth/ble_characteristic.py.exp | 9 + 7 files changed, 353 insertions(+), 60 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 206bc98f9ff71..5b2aec67da247 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -53,8 +53,6 @@ STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3; volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; -STATIC btstack_packet_callback_registration_t hci_event_callback_registration; - STATIC int btstack_error_to_errno(int err) { DEBUG_EVENT_printf(" --> btstack error: %d\n", err); if (err == ERROR_CODE_SUCCESS) { @@ -85,6 +83,159 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uu return result; } +// Notes on supporting background ops (e.g. an attempt to gatts_notify while +// an existing notification is in progress): + +// GATTS Notify/Indicate (att_server_notify/indicate) +// * When available, copies buffer immediately. +// * Otherwise fails with BTSTACK_ACL_BUFFERS_FULL +// * Use att_server_request_to_send_notification/indication to get callback +// * Takes btstack_context_callback_registration_t (and takes ownership) and conn_handle. +// * Callback is invoked with just the context member of the btstack_context_callback_registration_t + +// GATTC Write without response (gatt_client_write_value_of_characteristic_without_response) +// * When available, copies buffer immediately. +// * Otherwise, fails with GATT_CLIENT_BUSY. +// * Use gatt_client_request_can_write_without_response_event to get callback +// * Takes btstack_packet_handler_t (function pointer) and conn_handle +// * Callback is invoked, use gatt_event_can_write_without_response_get_handle to get the conn_handle (no other context) +// * There can only be one pending gatt_client_request_can_write_without_response_event (otherwise we fail with EALREADY). + +// GATTC Write with response (gatt_client_write_value_of_characteristic) +// * When peripheral is available, takes ownership of buffer. +// * Otherwise, fails with GATT_CLIENT_IN_WRONG_STATE (we fail the operation). +// * Raises GATT_EVENT_QUERY_COMPLETE to the supplied packet handler. + +// For notify/indicate/write-without-response that proceed immediately, nothing extra required. +// For all other cases, buffer needs to be copied and protected from GC. +// For notify/indicate: +// * btstack_context_callback_registration_t: +// * needs to be malloc'ed +// * needs to be protected from GC +// * context arg needs to point back to the callback registration so it can be freed and un-protected +// For write-without-response +// * only the conn_handle is available in the callback +// * so we need a queue of conn_handle->(value_handle, copied buffer) + +// Pending operation types. +enum { + // Queued for sending when possible. + MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, // Waiting for context callback + MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, // Waiting for context callback + MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, // Waiting for conn handle + // Hold buffer pointer until complete. + MP_BLUETOOTH_BTSTACK_PENDING_WRITE, // Waiting for write done event +}; + +// Pending operation: +// - Holds a GC reference to the copied outgoing buffer. +// - Provides enough information for the callback handler to execute the desired operation. +struct _mp_btstack_pending_op_t { + btstack_linked_item_t *next; // Must be first field to match btstack_linked_item. + + // See enum above. + uint16_t op_type; + + // For all op types. + uint16_t conn_handle; + uint16_t value_handle; + + // For notify/indicate only. + // context_registration.context will point back to this struct. + btstack_context_callback_registration_t context_registration; + + // For notify/indicate/write-without-response, this is the actual buffer to send. + // For write-with-response, just holding onto the buffer for GC ref. + size_t len; + uint8_t buf[]; +}; + +// Must hold MICROPY_PY_BLUETOOTH_ENTER. +STATIC void btstack_remove_pending_operation(mp_btstack_pending_op_t *pending_op, bool del) { + bool removed = btstack_linked_list_remove(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops, (btstack_linked_item_t *)pending_op); + assert(removed); + (void)removed; + if (del) { + m_del_var(mp_btstack_pending_op_t, uint8_t, pending_op->len, pending_op); + } +} + +// Called in response to a gatts_notify/indicate being unable to complete, which then calls +// att_server_request_to_send_notification. +// We now have an opportunity to re-try the operation with an empty ACL buffer. +STATIC void btstack_notify_indicate_ready_handler(void *context) { + MICROPY_PY_BLUETOOTH_ENTER + mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)context; + DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%lu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len); + if (pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY) { + int err = att_server_notify(pending_op->conn_handle, pending_op->value_handle, pending_op->buf, pending_op->len); + DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err); + assert(err == ERROR_CODE_SUCCESS); + (void)err; + } else { + assert(pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE); + int err = att_server_indicate(pending_op->conn_handle, pending_op->value_handle, NULL, 0); + DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err); + assert(err == ERROR_CODE_SUCCESS); + (void)err; + } + // Can't free the pending op as we're in IRQ context. Leave it for the GC. + btstack_remove_pending_operation(pending_op, false /* del */); + MICROPY_PY_BLUETOOTH_EXIT +} + +// Register a pending background operation -- copies the buffer, and makes it known to the GC. +STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, const uint8_t *buf, size_t len) { + DEBUG_EVENT_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%lu\n", op_type, conn_handle, value_handle, len); + mp_btstack_pending_op_t *pending_op = m_new_obj_var(mp_btstack_pending_op_t, uint8_t, len); + pending_op->op_type = op_type; + pending_op->conn_handle = conn_handle; + pending_op->value_handle = value_handle; + pending_op->len = len; + memcpy(pending_op->buf, buf, len); + + if (op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY || op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE) { + pending_op->context_registration.callback = &btstack_notify_indicate_ready_handler; + pending_op->context_registration.context = pending_op; + } + + MICROPY_PY_BLUETOOTH_ENTER + bool added = btstack_linked_list_add(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops, (btstack_linked_item_t *)pending_op); + assert(added); + (void)added; + MICROPY_PY_BLUETOOTH_EXIT + + return pending_op; +} + +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +// Cleans up a pending op of the specified type for this conn_handle (and if specified, value_handle). +// Used by MP_BLUETOOTH_BTSTACK_PENDING_WRITE and MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE. +// At the moment, both will set value_handle=0xffff as the events do not know their value_handle. +// TODO: Can we make btstack give us the value_handle for regular write (with response) so that we +// know for sure that we're using the correct entry. +STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, bool del) { + MICROPY_PY_BLUETOOTH_ENTER + DEBUG_EVENT_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle); + btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, &MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops); + while (btstack_linked_list_iterator_has_next(&it)) { + mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)btstack_linked_list_iterator_next(&it); + + if (pending_op->op_type == op_type && pending_op->conn_handle == conn_handle && (value_handle == 0xffff || pending_op->value_handle == value_handle)) { + DEBUG_EVENT_printf("btstack_finish_pending_operation: found value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len); + btstack_remove_pending_operation(pending_op, del); + MICROPY_PY_BLUETOOTH_EXIT + return del ? NULL : pending_op; + } + } + DEBUG_EVENT_printf("btstack_finish_pending_operation: not found\n"); + MICROPY_PY_BLUETOOTH_EXIT + return NULL; +} +#endif + STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) { DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); if (packet_type != HCI_EVENT_PACKET) { @@ -161,12 +312,17 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { - DEBUG_EVENT_printf(" --> gatt query complete\n"); uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet); + DEBUG_EVENT_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status); if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { - // TODO there is no value_handle available to pass here - mp_bluetooth_gattc_on_read_write_status(irq, conn_handle, 0, status); + // TODO there is no value_handle available to pass here. + // TODO try and get this implemented in btstack. + mp_bluetooth_gattc_on_read_write_status(irq, conn_handle, 0xffff, status); + // Unref the saved buffer for the write operation on this conn_handle. + if (irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { + btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE, conn_handle, 0xffff, false /* del */); + } } else if (irq == MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) { @@ -223,6 +379,16 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t len = mp_bluetooth_gattc_on_data_available_start(MP_BLUETOOTH_IRQ_GATTC_INDICATE, conn_handle, value_handle, len, &atomic_state); mp_bluetooth_gattc_on_data_available_chunk(data, len); mp_bluetooth_gattc_on_data_available_end(atomic_state); + } else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) { + uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet); + DEBUG_EVENT_printf(" --> gatt can write without response %d\n", conn_handle); + mp_btstack_pending_op_t *pending_op = btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, 0xffff, false /* !del */); + if (pending_op) { + DEBUG_EVENT_printf(" --> ready for value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len); + gatt_client_write_value_of_characteristic_without_response(pending_op->conn_handle, pending_op->value_handle, pending_op->len, (uint8_t *)pending_op->buf); + // Note: Can't "del" the pending_op from IRQ context. Leave it for the GC. + } + #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else { DEBUG_EVENT_printf(" --> hci event type: unknown (0x%02x)\n", event_type); @@ -238,6 +404,10 @@ STATIC void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel btstack_packet_handler(packet_type, packet, 0); } +STATIC btstack_packet_callback_registration_t hci_event_callback_registration = { + .callback = &btstack_packet_handler_generic +}; + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // For when the handler is being used for service discovery. STATIC void btstack_packet_handler_discover_services(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { @@ -326,7 +496,6 @@ int mp_bluetooth_init(void) { #endif // Register for HCI events. - hci_event_callback_registration.callback = &btstack_packet_handler_generic; hci_add_event_handler(&hci_event_callback_registration); // Set a timeout for HCI initialisation. @@ -410,8 +579,7 @@ size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) { } int mp_bluetooth_gap_set_device_name(const uint8_t *buf, size_t len) { - mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, BTSTACK_GAP_DEVICE_NAME_HANDLE, buf, len); - return 0; + return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, BTSTACK_GAP_DEVICE_NAME_HANDLE, buf, len); } int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) { @@ -556,7 +724,10 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m if (props & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)) { // btstack creates the CCCB as the next handle. mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index] + 1, MP_BLUETOOTH_CCCB_LEN); - mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index] + 1, cccb_buf, sizeof(cccb_buf)); + int ret = mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index] + 1, cccb_buf, sizeof(cccb_buf)); + if (ret) { + return ret; + } } DEBUG_EVENT_printf("Registered char with handle %u\n", handles[handle_index]); ++handle_index; @@ -607,19 +778,63 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) { uint8_t *data = NULL; size_t len = 0; mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len); - return mp_bluetooth_gatts_notify_send(conn_handle, value_handle, data, &len); + return mp_bluetooth_gatts_notify_send(conn_handle, value_handle, data, len); } -int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len) { +int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) { DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send\n"); - // TODO: We need to use att_server_request_to_send_notification here as the stack may not be ready to send a notification. - int err = att_server_notify(conn_handle, value_handle, value, *value_len); - return btstack_error_to_errno(err); + + // Attempt to send immediately. If it succeeds, btstack will copy the buffer. + MICROPY_PY_BLUETOOTH_ENTER + int err = att_server_notify(conn_handle, value_handle, value, value_len); + MICROPY_PY_BLUETOOTH_EXIT + + if (err == BTSTACK_ACL_BUFFERS_FULL) { + DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n"); + // Schedule callback, making a copy of the buffer. + mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, conn_handle, value_handle, value, value_len); + + err = att_server_request_to_send_notification(&pending_op->context_registration, conn_handle); + + if (err != ERROR_CODE_SUCCESS) { + // Failure. Unref and free the pending operation. + btstack_remove_pending_operation(pending_op, true /* del */); + } + + return 0; + } else { + return btstack_error_to_errno(err); + } } int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate\n"); - return btstack_error_to_errno(att_server_indicate(conn_handle, value_handle, NULL, 0)); + + uint8_t *data = NULL; + size_t len = 0; + mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len); + + // Attempt to send immediately, will copy buffer. + MICROPY_PY_BLUETOOTH_ENTER + int err = att_server_indicate(conn_handle, value_handle, data, len); + MICROPY_PY_BLUETOOTH_EXIT + + if (err == BTSTACK_ACL_BUFFERS_FULL) { + DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n"); + // Schedule callback, making a copy of the buffer. + mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, conn_handle, value_handle, data, len); + + err = att_server_request_to_send_indication(&pending_op->context_registration, conn_handle); + + if (err != ERROR_CODE_SUCCESS) { + // Failure. Unref and free the pending operation. + btstack_remove_pending_operation(pending_op, true /* del */); + } + + return 0; + } else { + return btstack_error_to_errno(err); + } } int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) { @@ -751,19 +966,42 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) { DEBUG_EVENT_printf("mp_bluetooth_gattc_write\n"); - // TODO the below gatt_client functions do not copy the data and require it to be valid - // until the write is done, so there should be some kind of buffering done here. + // We should be distinguishing between gatt_client_write_value_of_characteristic vs + // gatt_client_write_characteristic_descriptor_using_descriptor_handle. + // However both are implemented using send_gatt_write_attribute_value_request under the hood, + // and we get the exact same event to the packet handler. + // Same story for the "without response" version. + + int err; + mp_btstack_pending_op_t *pending_op = NULL; if (mode == MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE) { - // TODO need to call gatt_client_request_can_write_without_response_event then do - // the actual write on the callback from that. - return btstack_error_to_errno(gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value)); + // If possible, this will send immediately, copying the buffer directly to the ACL buffer. + err = gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value); + if (err == GATT_CLIENT_BUSY) { + DEBUG_EVENT_printf("mp_bluetooth_gattc_write: client busy\n"); + // Can't send right now, need to take a copy of the buffer and add it to the queue. + pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, value_handle, value, *value_len); + // Notify when this conn_handle can write. + err = gatt_client_request_can_write_without_response_event(&btstack_packet_handler_generic, conn_handle); + } else { + DEBUG_EVENT_printf("mp_bluetooth_gattc_write: other failure: %d\n", err); + } + } else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) { + // Pending operation copies the value buffer and keeps a GC reference + // until the response comes back (there is always a response). + pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE, conn_handle, value_handle, value, *value_len); + err = gatt_client_write_value_of_characteristic(&btstack_packet_handler_write_with_response, conn_handle, value_handle, pending_op->len, pending_op->buf); + } else { + return MP_EINVAL; } - if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) { - return btstack_error_to_errno(gatt_client_write_value_of_characteristic(&btstack_packet_handler_write_with_response, conn_handle, value_handle, *value_len, (uint8_t *)value)); + + if (pending_op && err != ERROR_CODE_SUCCESS) { + // Failure. Unref and free the pending operation. + btstack_remove_pending_operation(pending_op, true /* del */); } - return MP_EINVAL; + return btstack_error_to_errno(err); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE diff --git a/extmod/btstack/modbluetooth_btstack.h b/extmod/btstack/modbluetooth_btstack.h index 3bdb5f271e5dd..c943b6d72d548 100644 --- a/extmod/btstack/modbluetooth_btstack.h +++ b/extmod/btstack/modbluetooth_btstack.h @@ -33,6 +33,8 @@ #include "lib/btstack/src/btstack.h" +typedef struct _mp_btstack_pending_op_t mp_btstack_pending_op_t; + typedef struct _mp_bluetooth_btstack_root_pointers_t { // This stores both the advertising data and the scan response data, concatenated together. uint8_t *adv_data; @@ -45,6 +47,7 @@ typedef struct _mp_bluetooth_btstack_root_pointers_t { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Registration for notify/indicate events. gatt_client_notification_t notification; + btstack_linked_list_t pending_ops; #endif } mp_bluetooth_btstack_root_pointers_t; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index da40a1548541d..d94e5aec91ef2 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -321,8 +321,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map case MP_QSTR_gap_name: { mp_buffer_info_t bufinfo; mp_get_buffer_raise(e->value, &bufinfo, MP_BUFFER_READ); - int ret = mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len); - bluetooth_handle_errno(ret); + bluetooth_handle_errno(mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len)); break; } case MP_QSTR_rxbuf: { @@ -663,10 +662,9 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) if (n_args == 4) { mp_buffer_info_t bufinfo = {0}; mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); - size_t len = bufinfo.len; - int err = mp_bluetooth_gatts_notify_send(conn_handle, value_handle, bufinfo.buf, &len); + int err = mp_bluetooth_gatts_notify_send(conn_handle, value_handle, bufinfo.buf, bufinfo.len); bluetooth_handle_errno(err); - return MP_OBJ_NEW_SMALL_INT(len); + return mp_const_none; } else { int err = mp_bluetooth_gatts_notify(conn_handle, value_handle); return bluetooth_handle_errno(err); @@ -674,6 +672,15 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_notify_obj, 3, 4, bluetooth_ble_gatts_notify); +STATIC mp_obj_t bluetooth_ble_gatts_indicate(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t value_handle_in) { + mp_int_t conn_handle = mp_obj_get_int(conn_handle_in); + mp_int_t value_handle = mp_obj_get_int(value_handle_in); + + int err = mp_bluetooth_gatts_indicate(conn_handle, value_handle); + return bluetooth_handle_errno(err); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_gatts_indicate_obj, bluetooth_ble_gatts_indicate); + STATIC mp_obj_t bluetooth_ble_gatts_set_buffer(size_t n_args, const mp_obj_t *args) { mp_int_t value_handle = mp_obj_get_int(args[1]); mp_int_t len = mp_obj_get_int(args[2]); @@ -765,6 +772,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_write), MP_ROM_PTR(&bluetooth_ble_gatts_write_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) }, + { MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) }, #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // GATT Client (i.e. central/scanner role) @@ -1147,47 +1155,58 @@ mp_bluetooth_gatts_db_entry_t *mp_bluetooth_gatts_db_lookup(mp_gatts_db_t db, ui } int mp_bluetooth_gatts_db_read(mp_gatts_db_t db, uint16_t handle, uint8_t **value, size_t *value_len) { + MICROPY_PY_BLUETOOTH_ENTER mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle); - if (!entry) { - return MP_EINVAL; - } - - *value = entry->data; - *value_len = entry->data_len; - if (entry->append) { - entry->data_len = 0; + if (entry) { + *value = entry->data; + *value_len = entry->data_len; + if (entry->append) { + entry->data_len = 0; + } } - - return 0; + MICROPY_PY_BLUETOOTH_EXIT + return entry ? 0 : MP_EINVAL; } int mp_bluetooth_gatts_db_write(mp_gatts_db_t db, uint16_t handle, const uint8_t *value, size_t value_len) { + MICROPY_PY_BLUETOOTH_ENTER mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle); - if (!entry) { - return MP_EINVAL; - } + if (entry) { + if (value_len > entry->data_alloc) { + uint8_t *data = m_new_maybe(uint8_t, value_len); + if (data) { + entry->data = data; + entry->data_alloc = value_len; + } else { + MICROPY_PY_BLUETOOTH_EXIT + return MP_ENOMEM; + } + } - if (value_len > entry->data_alloc) { - entry->data = m_new(uint8_t, value_len); - entry->data_alloc = value_len; + memcpy(entry->data, value, value_len); + entry->data_len = value_len; } - - memcpy(entry->data, value, value_len); - entry->data_len = value_len; - - return 0; + MICROPY_PY_BLUETOOTH_EXIT + return entry ? 0 : MP_EINVAL; } int mp_bluetooth_gatts_db_resize(mp_gatts_db_t db, uint16_t handle, size_t len, bool append) { + MICROPY_PY_BLUETOOTH_ENTER mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(db, handle); - if (!entry) { - return MP_EINVAL; + if (entry) { + uint8_t *data = m_renew_maybe(uint8_t, entry->data, entry->data_alloc, len, true); + if (data) { + entry->data = data; + entry->data_alloc = len; + entry->data_len = 0; + entry->append = append; + } else { + MICROPY_PY_BLUETOOTH_EXIT + return MP_ENOMEM; + } } - entry->data = m_renew(uint8_t, entry->data, entry->data_alloc, len); - entry->data_alloc = len; - entry->data_len = 0; - entry->append = append; - return 0; + MICROPY_PY_BLUETOOTH_EXIT + return entry ? 0 : MP_EINVAL; } #endif // MICROPY_PY_BLUETOOTH diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index c8b8dc63f2a67..a232ee2d1d095 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -199,7 +199,7 @@ int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t // Notify the central that it should do a read. int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle); // Notify the central, including a data payload. (Note: does not set the gatts db value). -int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len); +int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len); // Indicate the central. int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index be7d13d9e6f5d..2e94685131d87 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -587,13 +587,13 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) { return ble_hs_err_to_errno(ble_gattc_notify(conn_handle, value_handle)); } -int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len) { +int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - struct os_mbuf *om = ble_hs_mbuf_from_flat(value, *value_len); + struct os_mbuf *om = ble_hs_mbuf_from_flat(value, value_len); if (om == NULL) { - return -1; + return MP_ENOMEM; } // TODO: check that notify_custom takes ownership of om, if not os_mbuf_free_chain(om). return ble_hs_err_to_errno(ble_gattc_notify_custom(conn_handle, value_handle, om)); diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index 33d92b823bbb2..fd6fd467248de 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -16,6 +16,7 @@ _IRQ_GATTC_READ_DONE = const(16) _IRQ_GATTC_WRITE_DONE = const(17) _IRQ_GATTC_NOTIFY = const(18) +_IRQ_GATTC_INDICATE = const(19) SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") @@ -59,6 +60,8 @@ def irq(event, data): print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: print("_IRQ_GATTC_NOTIFY", data[-1]) + elif event == _IRQ_GATTC_INDICATE: + print("_IRQ_GATTC_INDICATE", data[-1]) if waiting_event is not None: if (isinstance(waiting_event, int) and event == waiting_event) or ( @@ -115,6 +118,16 @@ def instance0(): print("gatts_notify") ble.gatts_notify(conn_handle, char_handle, "periph2") + # Wait for a write to the characteristic from the central. + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) + + # Wait a bit, then notify a new value on the characteristic. + time.sleep_ms(1000) + print("gatts_write") + ble.gatts_write(char_handle, "periph3") + print("gatts_indicate") + ble.gatts_indicate(conn_handle, char_handle) + # Wait for the central to disconnect. wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) finally: @@ -163,6 +176,17 @@ def instance1(): ble.gattc_read(conn_handle, value_handle) wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) + # Write to the characteristic, and ask for a response. + print("gattc_write") + ble.gattc_write(conn_handle, value_handle, "central2", 1) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) + + # Wait for a indicate (should have new data), then read new value. + wait_for_event(_IRQ_GATTC_INDICATE, TIMEOUT_MS) + print("gattc_read") + ble.gattc_read(conn_handle, value_handle) + wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) + # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp index d89842ef7b097..7f66a4d907455 100644 --- a/tests/multi_bluetooth/ble_characteristic.py.exp +++ b/tests/multi_bluetooth/ble_characteristic.py.exp @@ -6,6 +6,9 @@ gatts_write gatts_notify _IRQ_GATTS_WRITE b'central1' gatts_notify +_IRQ_GATTS_WRITE b'central2' +gatts_write +gatts_indicate _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect @@ -26,5 +29,11 @@ _IRQ_GATTC_NOTIFY b'periph2' gattc_read _IRQ_GATTC_READ_RESULT b'central1' _IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +_IRQ_GATTC_INDICATE b'periph3' +gattc_read +_IRQ_GATTC_READ_RESULT b'periph3' +_IRQ_GATTC_READ_DONE 0 gap_disconnect: True _IRQ_PERIPHERAL_DISCONNECT From 89a95b7c85cd90967020b80a0a4649b2f745bea8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 17 Jun 2020 14:57:52 +1000 Subject: [PATCH 0169/3533] examples/bluetooth: Add simple UART demo with central and peripheral. --- examples/bluetooth/ble_simple_central.py | 245 ++++++++++++++++++++ examples/bluetooth/ble_simple_peripheral.py | 98 ++++++++ 2 files changed, 343 insertions(+) create mode 100644 examples/bluetooth/ble_simple_central.py create mode 100644 examples/bluetooth/ble_simple_peripheral.py diff --git a/examples/bluetooth/ble_simple_central.py b/examples/bluetooth/ble_simple_central.py new file mode 100644 index 0000000000000..f6996366e3da1 --- /dev/null +++ b/examples/bluetooth/ble_simple_central.py @@ -0,0 +1,245 @@ +# This example finds and connects to a peripheral running the +# UART service (e.g. ble_simple_peripheral.py). + +import bluetooth +import random +import struct +import time +import micropython + +from ble_advertising import decode_services, decode_name + +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_SCAN_RESULT = const(5) +_IRQ_SCAN_DONE = const(6) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_DESCRIPTOR_RESULT = const(13) +_IRQ_GATTC_DESCRIPTOR_DONE = const(14) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) +_IRQ_GATTC_INDICATE = const(19) + +_ADV_IND = const(0x00) +_ADV_DIRECT_IND = const(0x01) +_ADV_SCAN_IND = const(0x02) +_ADV_NONCONN_IND = const(0x03) + +_UART_SERVICE_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") +_UART_RX_CHAR_UUID = bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E") +_UART_TX_CHAR_UUID = bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E") + + +class BLESimpleCentral: + def __init__(self, ble): + self._ble = ble + self._ble.active(True) + self._ble.irq(handler=self._irq) + + self._reset() + + def _reset(self): + # Cached name and address from a successful scan. + self._name = None + self._addr_type = None + self._addr = None + + # Callbacks for completion of various operations. + # These reset back to None after being invoked. + self._scan_callback = None + self._conn_callback = None + self._read_callback = None + + # Persistent callback for when new data is notified from the device. + self._notify_callback = None + + # Connected device. + self._conn_handle = None + self._start_handle = None + self._end_handle = None + self._tx_handle = None + self._rx_handle = None + + def _irq(self, event, data): + if event == _IRQ_SCAN_RESULT: + addr_type, addr, adv_type, rssi, adv_data = data + if adv_type in (_ADV_IND, _ADV_DIRECT_IND,) and _UART_SERVICE_UUID in decode_services( + adv_data + ): + # Found a potential device, remember it and stop scanning. + self._addr_type = addr_type + self._addr = bytes( + addr + ) # Note: addr buffer is owned by caller so need to copy it. + self._name = decode_name(adv_data) or "?" + self._ble.gap_scan(None) + + elif event == _IRQ_SCAN_DONE: + if self._scan_callback: + if self._addr: + # Found a device during the scan (and the scan was explicitly stopped). + self._scan_callback(self._addr_type, self._addr, self._name) + self._scan_callback = None + else: + # Scan timed out. + self._scan_callback(None, None, None) + + elif event == _IRQ_PERIPHERAL_CONNECT: + # Connect successful. + conn_handle, addr_type, addr, = data + if addr_type == self._addr_type and addr == self._addr: + self._conn_handle = conn_handle + self._ble.gattc_discover_services(self._conn_handle) + + elif event == _IRQ_PERIPHERAL_DISCONNECT: + # Disconnect (either initiated by us or the remote end). + conn_handle, _, _, = data + if conn_handle == self._conn_handle: + # If it was initiated by us, it'll already be reset. + self._reset() + + elif event == _IRQ_GATTC_SERVICE_RESULT: + # Connected device returned a service. + conn_handle, start_handle, end_handle, uuid = data + print("service", data) + if conn_handle == self._conn_handle and uuid == _UART_SERVICE_UUID: + self._start_handle, self._end_handle = start_handle, end_handle + + elif event == _IRQ_GATTC_SERVICE_DONE: + # Service query complete. + if self._start_handle and self._end_handle: + self._ble.gattc_discover_characteristics( + self._conn_handle, self._start_handle, self._end_handle + ) + else: + print("Failed to find uart service.") + + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + # Connected device returned a characteristic. + conn_handle, def_handle, value_handle, properties, uuid = data + if conn_handle == self._conn_handle and uuid == _UART_RX_CHAR_UUID: + self._rx_handle = value_handle + if conn_handle == self._conn_handle and uuid == _UART_TX_CHAR_UUID: + self._tx_handle = value_handle + + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + # Characteristic query complete. + if self._tx_handle is not None and self._rx_handle is not None: + # We've finished connecting and discovering device, fire the connect callback. + if self._conn_callback: + self._conn_callback() + else: + print("Failed to find uart rx characteristic.") + + elif event == _IRQ_GATTC_WRITE_DONE: + conn_handle, value_handle, status = data + print("TX complete") + + elif event == _IRQ_GATTC_NOTIFY: + conn_handle, value_handle, notify_data = data + if conn_handle == self._conn_handle and value_handle == self._tx_handle: + if self._notify_callback: + self._notify_callback(notify_data) + + # Returns true if we've successfully connected and discovered characteristics. + def is_connected(self): + return ( + self._conn_handle is not None + and self._tx_handle is not None + and self._rx_handle is not None + ) + + # Find a device advertising the environmental sensor service. + def scan(self, callback=None): + self._addr_type = None + self._addr = None + self._scan_callback = callback + self._ble.gap_scan(2000, 30000, 30000) + + # Connect to the specified device (otherwise use cached address from a scan). + def connect(self, addr_type=None, addr=None, callback=None): + self._addr_type = addr_type or self._addr_type + self._addr = addr or self._addr + self._conn_callback = callback + if self._addr_type is None or self._addr is None: + return False + self._ble.gap_connect(self._addr_type, self._addr) + return True + + # Disconnect from current device. + def disconnect(self): + if not self._conn_handle: + return + self._ble.gap_disconnect(self._conn_handle) + self._reset() + + # Send data over the UART + def write(self, v, response=False): + if not self.is_connected(): + return + self._ble.gattc_write(self._conn_handle, self._rx_handle, v, 1 if response else 0) + + # Set handler for when data is received over the UART. + def on_notify(self, callback): + self._notify_callback = callback + + +def demo(): + ble = bluetooth.BLE() + central = BLESimpleCentral(ble) + + not_found = False + + def on_scan(addr_type, addr, name): + if addr_type is not None: + print("Found peripheral:", addr_type, addr, name) + central.connect() + else: + nonlocal not_found + not_found = True + print("No peripheral found.") + + central.scan(callback=on_scan) + + # Wait for connection... + while not central.is_connected(): + time.sleep_ms(100) + if not_found: + return + + print("Connected") + + def on_rx(v): + print("RX", v) + + central.on_notify(on_rx) + + with_response = False + + i = 0 + while central.is_connected(): + try: + v = str(i) + "_" + print("TX", v) + central.write(v, with_response) + except: + print("TX failed") + i += 1 + time.sleep_ms(400 if with_response else 30) + + print("Disconnected") + + +if __name__ == "__main__": + demo() diff --git a/examples/bluetooth/ble_simple_peripheral.py b/examples/bluetooth/ble_simple_peripheral.py new file mode 100644 index 0000000000000..08cd7fa98d69c --- /dev/null +++ b/examples/bluetooth/ble_simple_peripheral.py @@ -0,0 +1,98 @@ +# This example demonstrates a UART periperhal. + +import bluetooth +import random +import struct +import time +from ble_advertising import advertising_payload + +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) + +_UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") +_UART_TX = ( + bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"), + bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY, +) +_UART_RX = ( + bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"), + bluetooth.FLAG_WRITE | bluetooth.FLAG_WRITE_NO_RESPONSE, +) +_UART_SERVICE = ( + _UART_UUID, + (_UART_TX, _UART_RX,), +) + + +class BLESimplePeripheral: + def __init__(self, ble, name="mpy-uart"): + self._ble = ble + self._ble.active(True) + self._ble.irq(handler=self._irq) + ((self._handle_tx, self._handle_rx,),) = self._ble.gatts_register_services( + (_UART_SERVICE,) + ) + self._connections = set() + self._write_callback = None + self._payload = advertising_payload(name=name, services=[_UART_UUID],) + self._advertise() + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _, = data + print("New connection", conn_handle) + self._connections.add(conn_handle) + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _, = data + print("Disconnected", conn_handle) + self._connections.remove(conn_handle) + # Start advertising again to allow a new connection. + self._advertise() + elif event == _IRQ_GATTS_WRITE: + conn_handle, value_handle = data + value = self._ble.gatts_read(value_handle) + if value_handle == self._handle_rx and self._write_callback: + self._write_callback(value) + + def send(self, data): + for conn_handle in self._connections: + self._ble.gatts_notify(conn_handle, self._handle_tx, data) + + def is_connected(self): + return len(self._connections) > 0 + + def _advertise(self, interval_us=500000): + print("Starting advertising") + self._ble.gap_advertise(interval_us, adv_data=self._payload) + + def on_write(self, callback): + self._write_callback = callback + + +def demo(): + ble = bluetooth.BLE() + p = BLESimplePeripheral(ble) + + def on_rx(v): + print("RX", v) + + p.on_write(on_rx) + + i = 0 + while True: + if p.is_connected(): + # Short burst of queued notifications. + for _ in range(3): + data = str(i) + "_" + print("TX", data) + p.send(data) + i += 1 + time.sleep_ms(100) + + +if __name__ == "__main__": + demo() From b7698841b20cd20a6729ba9d9f19a93f4fd33a5c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 17 Jul 2020 15:18:32 +1000 Subject: [PATCH 0170/3533] docs/library: Add gatts_indicate() doc to ubluetooth.rst. Also clarify behavior of `gatts_notify` and add some TODOs about adding an event for indication acknowledgement. --- docs/library/ubluetooth.rst | 43 +++++++++++++++++---------- extmod/btstack/modbluetooth_btstack.c | 2 ++ extmod/nimble/modbluetooth_nimble.c | 2 ++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index f96a7caaf828e..ed2ffefe750ad 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -48,7 +48,8 @@ Configuration - ``'mac'``: Returns the device MAC address. If a device has a fixed address (e.g. PYBD) then it will be returned. Otherwise (e.g. ESP32) a random address will be generated when the BLE interface is made active. - Note: on some ports, accessing this value requires that the interface is + + **Note:** on some ports, accessing this value requires that the interface is active (so that the MAC address can be queried from the controller). - ``'gap_name'``: Get/set the GAP device name used by service 0x1800, @@ -71,12 +72,12 @@ Event Handling arguments, ``event`` (which will be one of the codes below) and ``data`` (which is an event-specific tuple of values). - Note: the ``addr``, ``adv_data``, ``char_data``, ``notify_data``, and - ``uuid`` entries in the tuples are - references to data managed by the :mod:`ubluetooth` module (i.e. the same - instance will be re-used across multiple calls to the event handler). If - your program wants to use this data outside of the handler, then it must - copy them first, e.g. by using ``bytes(addr)`` or ``bluetooth.UUID(uuid)``. + **Note:** the ``addr``, ``adv_data``, ``char_data``, ``notify_data``, and + ``uuid`` entries in the tuples are references to data managed by the + :mod:`ubluetooth` module (i.e. the same instance will be re-used across + multiple calls to the event handler). If your program wants to use this + data outside of the handler, then it must copy them first, e.g. by using + ``bytes(addr)`` or ``bluetooth.UUID(uuid)``. An event handler showing all possible events:: @@ -189,7 +190,7 @@ Broadcaster Role (Advertiser) protocol (e.g. ``bytes``, ``bytearray``, ``str``). *adv_data* is included in all broadcasts, and *resp_data* is send in reply to an active scan. - Note: if *adv_data* (or *resp_data*) is ``None``, then the data passed + **Note:** if *adv_data* (or *resp_data*) is ``None``, then the data passed to the previous call to ``gap_advertise`` will be re-used. This allows a broadcaster to resume advertising with just ``gap_advertise(interval_us)``. To clear the advertising payload pass an empty ``bytes``, i.e. ``b''``. @@ -280,8 +281,8 @@ writes from a central to a given characteristic, use ( (hr,), (tx, rx,), ) = bt.gatts_register_services(SERVICES) The three value handles (``hr``, ``tx``, ``rx``) can be used with - :meth:`gatts_read `, :meth:`gatts_write `, - and :meth:`gatts_notify `. + :meth:`gatts_read `, :meth:`gatts_write `, :meth:`gatts_notify `, and + :meth:`gatts_indicate `. **Note:** Advertising must be stopped before registering services. @@ -296,12 +297,24 @@ writes from a central to a given characteristic, use .. method:: BLE.gatts_notify(conn_handle, value_handle, [data]) - Notifies a connected central that this value has changed and that it should - issue a read of the current value from this peripheral. + Sends a notification request to a connected central. + + If *data* is specified, then that value is sent to the central as part of + the notification. The local value will not be modified. + + Otherwise, if *data* is not specified, then the current local value (as + set with :meth:`gatts_write `) will be sent. + +.. method:: BLE.gatts_indicate(conn_handle, value_handle) + + Sends an indication request to a connected central. + + **Note:** This does not currently support sending a custom value, it will + always send the current local value (as set with :meth:`gatts_write + `). - If *data* is specified, then the that value is sent to the central as part - of the notification, avoiding the need for a separate read request. Note - that this will not update the local value stored. + **Note:** This does not currently support generating an event for sucessful + acknowledgment of the indication. .. method:: BLE.gatts_set_buffer(value_handle, len, append=False, /) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 5b2aec67da247..b114275b6e87c 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -814,6 +814,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { size_t len = 0; mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len); + // TODO: Handle ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE to generate acknowledgment event. + // Attempt to send immediately, will copy buffer. MICROPY_PY_BLUETOOTH_ENTER int err = att_server_indicate(conn_handle, value_handle, data, len); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 2e94685131d87..b859213aabd3d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -603,6 +603,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } + // TODO: catch BLE_GAP_EVENT_NOTIFY_TX to raise an event for completed + // indication transaction. return ble_hs_err_to_errno(ble_gattc_indicate(conn_handle, value_handle)); } From 43ceadac5503ac6854dd0d400bd13b93f36dd6a6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 20 Jul 2020 11:29:19 +1000 Subject: [PATCH 0171/3533] extmod/modbluetooth: Ignore unused self_in in ble_gatts_indicate. --- extmod/modbluetooth.c | 1 + 1 file changed, 1 insertion(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index d94e5aec91ef2..ced67365a68ad 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -673,6 +673,7 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_notify_obj, 3, 4, bluetooth_ble_gatts_notify); STATIC mp_obj_t bluetooth_ble_gatts_indicate(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t value_handle_in) { + (void)self_in; mp_int_t conn_handle = mp_obj_get_int(conn_handle_in); mp_int_t value_handle = mp_obj_get_int(value_handle_in); From 3c7ca2004c78ec386e136b947ed5e05a39b61aaf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 20 Jul 2020 11:46:55 +1000 Subject: [PATCH 0172/3533] extmod/modbluetooth: Fix so it builds in peripheral-only mode. --- extmod/btstack/modbluetooth_btstack.c | 22 ++++++++++++---------- extmod/btstack/modbluetooth_btstack.h | 3 ++- extmod/modbluetooth.c | 2 ++ extmod/nimble/modbluetooth_nimble.c | 14 +++++++------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index b114275b6e87c..d25b01d815591 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -70,6 +70,7 @@ STATIC int btstack_error_to_errno(int err) { } } +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uuid128) { mp_obj_bluetooth_uuid_t result; if (uuid16 != 0) { @@ -82,6 +83,7 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uu } return result; } +#endif // Notes on supporting background ops (e.g. an attempt to gatts_notify while // an existing notification is in progress): @@ -286,16 +288,6 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_EVENT_printf(" --> btstack # conns changed\n"); } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) { DEBUG_EVENT_printf(" --> hci vendor specific\n"); - } else if (event_type == GAP_EVENT_ADVERTISING_REPORT) { - DEBUG_EVENT_printf(" --> gap advertising report\n"); - bd_addr_t address; - gap_event_advertising_report_get_address(packet, address); - uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet); - uint8_t address_type = gap_event_advertising_report_get_address_type(packet); - int8_t rssi = gap_event_advertising_report_get_rssi(packet); - uint8_t length = gap_event_advertising_report_get_data_length(packet); - const uint8_t *data = gap_event_advertising_report_get_data(packet); - mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length); } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { DEBUG_EVENT_printf(" --> hci disconnect complete\n"); uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet); @@ -311,6 +303,16 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t uint8_t addr[6] = {0}; mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + } else if (event_type == GAP_EVENT_ADVERTISING_REPORT) { + DEBUG_EVENT_printf(" --> gap advertising report\n"); + bd_addr_t address; + gap_event_advertising_report_get_address(packet, address); + uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet); + uint8_t address_type = gap_event_advertising_report_get_address_type(packet); + int8_t rssi = gap_event_advertising_report_get_rssi(packet); + uint8_t length = gap_event_advertising_report_get_data_length(packet); + const uint8_t *data = gap_event_advertising_report_get_data(packet); + mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length); } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet); diff --git a/extmod/btstack/modbluetooth_btstack.h b/extmod/btstack/modbluetooth_btstack.h index c943b6d72d548..2fad86f22605e 100644 --- a/extmod/btstack/modbluetooth_btstack.h +++ b/extmod/btstack/modbluetooth_btstack.h @@ -44,10 +44,11 @@ typedef struct _mp_bluetooth_btstack_root_pointers_t { // Characteristic (and descriptor) value storage. mp_gatts_db_t gatts_db; + btstack_linked_list_t pending_ops; + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Registration for notify/indicate events. gatt_client_notification_t notification; - btstack_linked_list_t pending_ops; #endif } mp_bluetooth_btstack_root_pointers_t; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index ced67365a68ad..fb5c6ac8c99ab 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -836,10 +836,12 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size // Note the int8_t got packed into the ringbuf as a uint8_t. data_tuple->items[j++] = MP_OBJ_NEW_SMALL_INT((int8_t)ringbuf_get(ringbuf)); } + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE if (uuid) { ringbuf_get_uuid(ringbuf, uuid); data_tuple->items[j++] = MP_OBJ_FROM_PTR(uuid); } + #endif // The code that enqueues into the ringbuf should ensure that it doesn't // put more than bt->irq_data_data_alloc bytes into the ringbuf, because // that's what's available here in bt->irq_data_bytes. diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index b859213aabd3d..d60e23ecb4b9b 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -96,6 +96,13 @@ STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid, ble_u } } +// modbluetooth (and the layers above it) work in BE for addresses, Nimble works in LE. +STATIC void reverse_addr_byte_order(uint8_t *addr_out, const uint8_t *addr_in) { + for (int i = 0; i < 6; ++i) { + addr_out[i] = addr_in[5 - i]; + } +} + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(const ble_uuid_any_t *uuid) { @@ -123,13 +130,6 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(const ble_uuid_any_t *uuid) { return result; } -// modbluetooth (and the layers above it) work in BE for addresses, Nimble works in LE. -STATIC void reverse_addr_byte_order(uint8_t *addr_out, const uint8_t *addr_in) { - for (int i = 0; i < 6; ++i) { - addr_out[i] = addr_in[5 - i]; - } -} - STATIC ble_addr_t create_nimble_addr(uint8_t addr_type, const uint8_t *addr) { ble_addr_t addr_nimble; addr_nimble.type = addr_type; From 9d823a5d9a6730edde8e1df1e5ff4add1ad17094 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 20 Jul 2020 16:58:10 +1000 Subject: [PATCH 0173/3533] extmod/modbluetooth: Add event for "indicate acknowledgement". This commit adds the IRQ_GATTS_INDICATE_DONE BLE event which will be raised with the status of gatts_indicate (unlike notify, indications require acknowledgement). An example of its use is added to ble_temperature.py, and to the multitests in ble_characteristic.py. Implemented for btstack and nimble bindings, tested in both directions between unix/btstack and pybd/nimble. --- examples/bluetooth/ble_temperature.py | 19 +++++--- extmod/btstack/modbluetooth_btstack.c | 44 ++++++++++++++++--- extmod/modbluetooth.c | 15 +++++++ extmod/modbluetooth.h | 6 +++ extmod/nimble/modbluetooth_nimble.c | 13 +++++- tests/multi_bluetooth/ble_characteristic.py | 8 +++- .../multi_bluetooth/ble_characteristic.py.exp | 1 + 7 files changed, 92 insertions(+), 14 deletions(-) diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py index fd24e74d7498a..0e2da2239d50a 100644 --- a/examples/bluetooth/ble_temperature.py +++ b/examples/bluetooth/ble_temperature.py @@ -13,13 +13,14 @@ _IRQ_CENTRAL_CONNECT = const(1) _IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_INDICATE_DONE = const(20) # org.bluetooth.service.environmental_sensing _ENV_SENSE_UUID = bluetooth.UUID(0x181A) # org.bluetooth.characteristic.temperature _TEMP_CHAR = ( bluetooth.UUID(0x2A6E), - bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY, + bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE, ) _ENV_SENSE_SERVICE = ( _ENV_SENSE_UUID, @@ -52,15 +53,21 @@ def _irq(self, event, data): self._connections.remove(conn_handle) # Start advertising again to allow a new connection. self._advertise() + elif event == _IRQ_GATTS_INDICATE_DONE: + conn_handle, value_handle, status, = data - def set_temperature(self, temp_deg_c, notify=False): + def set_temperature(self, temp_deg_c, notify=False, indicate=False): # Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius. # Write the local value, ready for a central to read. self._ble.gatts_write(self._handle, struct.pack(" att connected\n"); + // The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which. + // So we use the HCI_EVENT_LE_META event directly in the main packet handler. } else if (event_type == ATT_EVENT_DISCONNECTED) { DEBUG_EVENT_printf(" --> att disconnected\n"); - } else if (event_type == HCI_EVENT_LE_META) { + } else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) { + DEBUG_EVENT_printf(" --> att indication complete\n"); + uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet); + uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet); + uint8_t status = att_event_handle_value_indication_complete_get_status(packet); + mp_bluetooth_gatts_on_indicate_complete(conn_handle, value_handle, status); + } else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { + // Ignore, duplicated by att_server.c. + } else { + DEBUG_EVENT_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type); + } +} + +STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) { + DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); + if (packet_type != HCI_EVENT_PACKET) { + return; + } + + uint8_t event_type = hci_event_packet_get_type(packet); + + if (event_type == HCI_EVENT_LE_META) { DEBUG_EVENT_printf(" --> hci le meta\n"); if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) { uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); @@ -277,7 +305,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; } } else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) { - DEBUG_EVENT_printf(" --> hci transport packet set\n"); + DEBUG_EVENT_printf(" --> hci transport packet sent\n"); } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { DEBUG_EVENT_printf(" --> hci command complete\n"); } else if (event_type == HCI_EVENT_COMMAND_STATUS) { @@ -288,6 +316,8 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_EVENT_printf(" --> btstack # conns changed\n"); } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) { DEBUG_EVENT_printf(" --> hci vendor specific\n"); + } else if (event_type == GATT_EVENT_MTU) { + DEBUG_EVENT_printf(" --> hci MTU\n"); } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { DEBUG_EVENT_printf(" --> hci disconnect complete\n"); uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet); @@ -500,6 +530,9 @@ int mp_bluetooth_init(void) { // Register for HCI events. hci_add_event_handler(&hci_event_callback_registration); + // Register for ATT server events. + att_server_register_packet_handler(&btstack_packet_handler_att_server); + // Set a timeout for HCI initialisation. btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS); btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler); @@ -816,7 +849,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { size_t len = 0; mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len); - // TODO: Handle ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE to generate acknowledgment event. + // Indicate will raise ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE when + // acknowledged (or timeout/error). // Attempt to send immediately, will copy buffer. MICROPY_PY_BLUETOOTH_ENTER diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index fb5c6ac8c99ab..7bfb774782048 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -800,6 +800,7 @@ STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_FLAG_READ), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ) }, { MP_ROM_QSTR(MP_QSTR_FLAG_WRITE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE) }, { MP_ROM_QSTR(MP_QSTR_FLAG_NOTIFY), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) }, + { MP_ROM_QSTR(MP_QSTR_FLAG_INDICATE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE) }, { MP_ROM_QSTR(MP_QSTR_FLAG_WRITE_NO_RESPONSE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE) }, }; @@ -887,6 +888,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTS_WRITE) { // conn_handle, value_handle ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL); + } else if (event == MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE) { + // conn_handle, value_handle, status + ringbuf_extract(&o->ringbuf, data_tuple, 2, 1, NULL, 0, NULL, NULL); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) { // addr_type, addr, adv_type, rssi, adv_data @@ -999,6 +1003,17 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { schedule_ringbuf(atomic_state); } +void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status) { + MICROPY_PY_BLUETOOTH_ENTER + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (enqueue_irq(o, 2 + 2 + 1, MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, value_handle); + ringbuf_put(&o->ringbuf, status); + } + schedule_ringbuf(atomic_state); +} + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE void mp_bluetooth_gap_on_scan_complete(void) { MICROPY_PY_BLUETOOTH_ENTER diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index a232ee2d1d095..cdb86e5e63c44 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -68,6 +68,7 @@ #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE (1 << 2) #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE (1 << 3) #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY (1 << 4) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE (1 << 5) // For mp_bluetooth_gattc_write, the mode parameter #define MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE (0) @@ -107,6 +108,7 @@ #define MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE (17) #define MP_BLUETOOTH_IRQ_GATTC_NOTIFY (18) #define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19) +#define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20) /* These aren't included in the module for space reasons, but can be used @@ -132,6 +134,7 @@ _IRQ_GATTC_READ_DONE = const(16) _IRQ_GATTC_WRITE_DONE = const(17) _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) +_IRQ_GATTS_INDICATE_DONE = const(20) */ // Common UUID type. @@ -245,6 +248,9 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han // Call this when a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); +// Call this when an acknowledgment is received for an indication. +void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status); + #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK // Call this when a characteristic is read from. Return false to deny the read. bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index d60e23ecb4b9b..843989b2b8986 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -248,6 +248,15 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val); mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr); break; + + case BLE_GAP_EVENT_NOTIFY_TX: { + DEBUG_EVENT_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status); + // This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0). + if (event->notify_tx.indication && event->notify_tx.status != 0) { + // Map "done/ack" to 0, otherwise pass the status directly. + mp_bluetooth_gatts_on_indicate_complete(event->notify_tx.conn_handle, event->notify_tx.attr_handle, event->notify_tx.status == BLE_HS_EDONE ? 0 : event->notify_tx.status); + } + } } return 0; @@ -603,8 +612,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - // TODO: catch BLE_GAP_EVENT_NOTIFY_TX to raise an event for completed - // indication transaction. + // This will raise BLE_GAP_EVENT_NOTIFY_TX with a status when it is + // acknowledged (or timeout/error). return ble_hs_err_to_errno(ble_gattc_indicate(conn_handle, value_handle)); } diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index fd6fd467248de..0d72c181fdec6 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -17,12 +17,13 @@ _IRQ_GATTC_WRITE_DONE = const(17) _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) +_IRQ_GATTS_INDICATE_DONE = const(20) SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") CHAR = ( CHAR_UUID, - bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY, + bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE, ) SERVICE = ( SERVICE_UUID, @@ -62,6 +63,8 @@ def irq(event, data): print("_IRQ_GATTC_NOTIFY", data[-1]) elif event == _IRQ_GATTC_INDICATE: print("_IRQ_GATTC_INDICATE", data[-1]) + elif event == _IRQ_GATTS_INDICATE_DONE: + print("_IRQ_GATTS_INDICATE_DONE", data[-1]) if waiting_event is not None: if (isinstance(waiting_event, int) and event == waiting_event) or ( @@ -128,6 +131,9 @@ def instance0(): print("gatts_indicate") ble.gatts_indicate(conn_handle, char_handle) + # Wait for the indicate ack. + wait_for_event(_IRQ_GATTS_INDICATE_DONE, TIMEOUT_MS) + # Wait for the central to disconnect. wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) finally: diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp index 7f66a4d907455..25b5544efc459 100644 --- a/tests/multi_bluetooth/ble_characteristic.py.exp +++ b/tests/multi_bluetooth/ble_characteristic.py.exp @@ -9,6 +9,7 @@ gatts_notify _IRQ_GATTS_WRITE b'central2' gatts_write gatts_indicate +_IRQ_GATTS_INDICATE_DONE 0 _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect From c7f7c0214c433d52204fe73613bd6162507a0482 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 20 Jul 2020 17:33:12 +1000 Subject: [PATCH 0174/3533] docs/library: For ubluetooth, add docs for _IRQ_GATTS_INDICATE_DONE. --- docs/library/ubluetooth.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index ed2ffefe750ad..e103932efd2f6 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -148,6 +148,10 @@ Event Handling elif event == _IRQ_GATTC_INDICATE: # A peripheral has sent an indicate request. conn_handle, value_handle, notify_data = data + elif event == _IRQ_GATTS_INDICATE_DONE: + # A central has acknowledged the indication. + # Note: Status will be zero on successful acknowledgment, implementation-specific value otherwise. + conn_handle, value_handle, status = data The event codes are:: @@ -171,6 +175,7 @@ The event codes are:: _IRQ_GATTC_WRITE_DONE = const(17) _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) + _IRQ_GATTS_INDICATE_DONE = const(20) In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your @@ -313,8 +318,8 @@ writes from a central to a given characteristic, use always send the current local value (as set with :meth:`gatts_write `). - **Note:** This does not currently support generating an event for sucessful - acknowledgment of the indication. + On acknowledgment (or failure, e.g. timeout), the + ``_IRQ_GATTS_INDICATE_DONE`` event will be raised. .. method:: BLE.gatts_set_buffer(value_handle, len, append=False, /) From 9aa214077e6d1e0fba1a775431fedea4c8d76558 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 26 Mar 2020 23:17:35 -0700 Subject: [PATCH 0175/3533] extmod/modussl: Improve exception error messages. This commit adds human readable error messages when mbedtls or axtls raise an exception. Currently often just an EIO error is raised so the user is lost and can't tell whether it's a cert error, buffer overrun, connecting to a non-ssl port, etc. The axtls and mbedtls error raising in the ussl module is modified to raise: OSError(-err_num, "error string") For axtls a small error table of strings is added and used for the second argument of the OSErrer. For mbedtls the code uses mbedtls' built-in strerror function, and if there is an out of memory condition it just produces OSError(-err_num). Producing the error string for mbedtls is conditional on them being included in the mbedtls build, via MBEDTLS_ERROR_C. --- extmod/modussl_axtls.c | 55 ++++++++++++++++++++++++++-- extmod/modussl_mbedtls.c | 43 ++++++++++++++++++++-- tests/extmod/ussl_basic.py | 2 +- tests/extmod/ussl_basic.py.exp | 3 +- tests/net_inet/tls_num_errors.py | 44 ++++++++++++++++++++++ tests/net_inet/tls_num_errors.py.exp | 2 + tests/net_inet/tls_text_errors.py | 33 +++++++++++++++++ 7 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 tests/net_inet/tls_num_errors.py create mode 100644 tests/net_inet/tls_num_errors.py.exp create mode 100644 tests/net_inet/tls_text_errors.py diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 7b0e3cbcbc327..0b0ce35fc8711 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -29,6 +29,7 @@ #include "py/runtime.h" #include "py/stream.h" +#include "py/objstr.h" #if MICROPY_PY_USSL && MICROPY_SSL_AXTLS @@ -54,6 +55,56 @@ struct ssl_args { STATIC const mp_obj_type_t ussl_socket_type; +// Table of errors +struct ssl_errs { + int16_t errnum; + const char *errstr; +}; +STATIC const struct ssl_errs ssl_error_tab[] = { + { SSL_NOT_OK, "NOT_OK" }, + { SSL_ERROR_DEAD, "DEAD" }, + { SSL_CLOSE_NOTIFY, "CLOSE_NOTIFY" }, + { SSL_EAGAIN, "EAGAIN" }, + { SSL_ERROR_CONN_LOST, "CONN_LOST" }, + { SSL_ERROR_RECORD_OVERFLOW, "RECORD_OVERFLOW" }, + { SSL_ERROR_SOCK_SETUP_FAILURE, "SOCK_SETUP_FAILURE" }, + { SSL_ERROR_INVALID_HANDSHAKE, "INVALID_HANDSHAKE" }, + { SSL_ERROR_INVALID_PROT_MSG, "INVALID_PROT_MSG" }, + { SSL_ERROR_INVALID_HMAC, "INVALID_HMAC" }, + { SSL_ERROR_INVALID_VERSION, "INVALID_VERSION" }, + { SSL_ERROR_UNSUPPORTED_EXTENSION, "UNSUPPORTED_EXTENSION" }, + { SSL_ERROR_INVALID_SESSION, "INVALID_SESSION" }, + { SSL_ERROR_NO_CIPHER, "NO_CIPHER" }, + { SSL_ERROR_INVALID_CERT_HASH_ALG, "INVALID_CERT_HASH_ALG" }, + { SSL_ERROR_BAD_CERTIFICATE, "BAD_CERTIFICATE" }, + { SSL_ERROR_INVALID_KEY, "INVALID_KEY" }, + { SSL_ERROR_FINISHED_INVALID, "FINISHED_INVALID" }, + { SSL_ERROR_NO_CERT_DEFINED, "NO_CERT_DEFINED" }, + { SSL_ERROR_NO_CLIENT_RENOG, "NO_CLIENT_RENOG" }, + { SSL_ERROR_NOT_SUPPORTED, "NOT_SUPPORTED" }, +}; + +STATIC NORETURN void ussl_raise_error(int err) { + for (size_t i = 0; i < MP_ARRAY_SIZE(ssl_error_tab); i++) { + if (ssl_error_tab[i].errnum == err) { + // construct string object + mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); + if (o_str == NULL) { + break; + } + o_str->base.type = &mp_type_str; + o_str->data = (const byte *)ssl_error_tab[i].errstr; + o_str->len = strlen((char *)o_str->data); + o_str->hash = qstr_compute_hash(o_str->data, o_str->len); + // raise + mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)}; + nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); + } + } + mp_raise_OSError(err); +} + + STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args) { #if MICROPY_PY_USSL_FINALISER mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t); @@ -107,9 +158,7 @@ STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args int res = ssl_handshake_status(o->ssl_sock); if (res != SSL_OK) { - printf("ssl_handshake_status: %d\n", res); - ssl_display_error(res); - mp_raise_OSError(MP_EIO); + ussl_raise_error(res); } } diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 9e117c82ccf34..94061ddc8b946 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -34,6 +34,7 @@ #include "py/runtime.h" #include "py/stream.h" +#include "py/objstr.h" // mbedtls_time_t #include "mbedtls/platform.h" @@ -43,6 +44,7 @@ #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/debug.h" +#include "mbedtls/error.h" typedef struct _mp_obj_ssl_socket_t { mp_obj_base_t base; @@ -74,6 +76,42 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons } #endif +STATIC NORETURN void mbedtls_raise_error(int err) { + #if defined(MBEDTLS_ERROR_C) + // Including mbedtls_strerror takes about 16KB on the esp32 due to all the strings. + // MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror. + // It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile. + // "small" negative integer error codes come from underlying stream/sockets, not mbedtls + if (err < 0 && err > -256) { + mp_raise_OSError(-err); + } + + // Try to allocate memory for the message + #define ERR_STR_MAX 100 // mbedtls_strerror truncates if it doesn't fit + mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); + byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX); + if (o_str == NULL || o_str_buf == NULL) { + mp_raise_OSError(err); + } + + // print the error message into the allocated buffer + mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX); + size_t len = strnlen((char *)o_str_buf, ERR_STR_MAX); + + // Put the exception object together + o_str->base.type = &mp_type_str; + o_str->data = o_str_buf; + o_str->len = len; + o_str->hash = qstr_compute_hash(o_str->data, o_str->len); + // raise + mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)}; + nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); + #else + // mbedtls is compiled without error strings so we simply return the err number + mp_raise_OSError(err); // typ. err is negative + #endif +} + STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { mp_obj_t sock = *(mp_obj_t *)ctx; @@ -85,7 +123,7 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { if (mp_is_nonblocking_error(err)) { return MBEDTLS_ERR_SSL_WANT_WRITE; } - return -err; + return -err; // convert an MP_ERRNO to something mbedtls passes through as error } else { return out_sz; } @@ -197,7 +235,6 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { if (args->do_handshake.u_bool) { while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { - printf("mbedtls_ssl_handshake error: -%x\n", -ret); goto cleanup; } } @@ -221,7 +258,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { } else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) { mp_raise_ValueError(MP_ERROR_TEXT("invalid cert")); } else { - mp_raise_OSError(MP_EIO); + mbedtls_raise_error(ret); } } diff --git a/tests/extmod/ussl_basic.py b/tests/extmod/ussl_basic.py index b4e21c7dce660..9e1821dca9981 100644 --- a/tests/extmod/ussl_basic.py +++ b/tests/extmod/ussl_basic.py @@ -9,7 +9,7 @@ # create in client mode try: - ss = ssl.wrap_socket(io.BytesIO()) + ss = ssl.wrap_socket(io.BytesIO(), server_hostname="test.example.com") except OSError as er: print("wrap_socket:", repr(er)) diff --git a/tests/extmod/ussl_basic.py.exp b/tests/extmod/ussl_basic.py.exp index 5282338319efb..eb7df855aa099 100644 --- a/tests/extmod/ussl_basic.py.exp +++ b/tests/extmod/ussl_basic.py.exp @@ -1,5 +1,4 @@ -ssl_handshake_status: -256 -wrap_socket: OSError(5,) +wrap_socket: OSError(-256, 'CONN_LOST') <_SSLSocket 4 b'' diff --git a/tests/net_inet/tls_num_errors.py b/tests/net_inet/tls_num_errors.py new file mode 100644 index 0000000000000..dd7f714e6ef7e --- /dev/null +++ b/tests/net_inet/tls_num_errors.py @@ -0,0 +1,44 @@ +# test that modtls produces a numerical error message when out of heap + +try: + import usocket as socket, ussl as ssl, sys +except: + import socket, ssl, sys +try: + from micropython import alloc_emergency_exception_buf, heap_lock, heap_unlock +except: + print("SKIP") + raise SystemExit + + +# test with heap locked to see it switch to number-only error message +def test(addr): + alloc_emergency_exception_buf(256) + s = socket.socket() + s.connect(addr) + try: + s.setblocking(False) + s = ssl.wrap_socket(s, do_handshake=False) + heap_lock() + print("heap is locked") + while True: + ret = s.write("foo") + if ret: + break + heap_unlock() + print("wrap: no exception") + except OSError as e: + heap_unlock() + # mbedtls produces "-29184" + # axtls produces "RECORD_OVERFLOW" + ok = "-29184" in str(e) or "RECORD_OVERFLOW" in str(e) + print("wrap:", ok) + if not ok: + print("got exception:", e) + s.close() + + +if __name__ == "__main__": + # connect to plain HTTP port, oops! + addr = socket.getaddrinfo("micropython.org", 80)[0][-1] + test(addr) diff --git a/tests/net_inet/tls_num_errors.py.exp b/tests/net_inet/tls_num_errors.py.exp new file mode 100644 index 0000000000000..e6a15634d00ae --- /dev/null +++ b/tests/net_inet/tls_num_errors.py.exp @@ -0,0 +1,2 @@ +heap is locked +wrap: True diff --git a/tests/net_inet/tls_text_errors.py b/tests/net_inet/tls_text_errors.py new file mode 100644 index 0000000000000..2ba167b868ef8 --- /dev/null +++ b/tests/net_inet/tls_text_errors.py @@ -0,0 +1,33 @@ +# test that modtls produces a text error message + +try: + import usocket as socket, ussl as ssl, sys +except: + import socket, ssl, sys + + +def test(addr): + s = socket.socket() + s.connect(addr) + try: + s = ssl.wrap_socket(s) + print("wrap: no exception") + except OSError as e: + # mbedtls produces "mbedtls -0x7200: SSL - An invalid SSL record was received" + # axtls produces "RECORD_OVERFLOW" + # CPython produces "[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)" + ok = ( + "invalid SSL record" in str(e) + or "RECORD_OVERFLOW" in str(e) + or "wrong version" in str(e) + ) + print("wrap:", ok) + if not ok: + print("got exception:", e) + s.close() + + +if __name__ == "__main__": + # connect to plain HTTP port, oops! + addr = socket.getaddrinfo("micropython.org", 80)[0][-1] + test(addr) From 98e583430fb7b793119db27bad9f98119e81579f Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 2 Jul 2020 12:48:16 -0700 Subject: [PATCH 0176/3533] lib/libc: Add implementation of strncpy. --- lib/libc/string0.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/libc/string0.c b/lib/libc/string0.c index 8c86bf65f7b40..5f40a71e3e284 100644 --- a/lib/libc/string0.c +++ b/lib/libc/string0.c @@ -169,6 +169,23 @@ char *strcpy(char *dest, const char *src) { return dest; } +// Public Domain implementation of strncpy from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strncpy_function +char *strncpy(char *s1, const char *s2, size_t n) { + char *dst = s1; + const char *src = s2; + /* Copy bytes, one at a time. */ + while (n > 0) { + n--; + if ((*dst++ = *src++) == '\0') { + /* If we get here, we found a null character at the end of s2 */ + *dst = '\0'; + break; + } + } + return s1; + } + // needed because gcc optimises strcpy + strcat to this char *stpcpy(char *dest, const char *src) { while (*src) { From 3e758ef235793502061edd122cd5cd91172faf51 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 29 May 2020 13:05:47 -0700 Subject: [PATCH 0177/3533] lib/mbedtls_errors: Add code to patch mbedtls for shortened error strs. The file `mbedtls_errors/mp_mbedtls_errors.c` can be used instead of `mbedtls/library/error.c` to give shorter error strings, reducing the build size of the error strings from about 12-16kB down to about 2-5kB. --- lib/mbedtls_errors/README.md | 42 ++ lib/mbedtls_errors/do-esp32.sh | 7 + lib/mbedtls_errors/do-mp.sh | 4 + lib/mbedtls_errors/do-test.sh | 4 + lib/mbedtls_errors/error.fmt | 165 ++++++ lib/mbedtls_errors/generate_errors.diff | 22 + lib/mbedtls_errors/mp_mbedtls_errors.c | 705 ++++++++++++++++++++++++ lib/mbedtls_errors/tester.c | 58 ++ tools/codeformat.py | 1 + 9 files changed, 1008 insertions(+) create mode 100644 lib/mbedtls_errors/README.md create mode 100755 lib/mbedtls_errors/do-esp32.sh create mode 100755 lib/mbedtls_errors/do-mp.sh create mode 100755 lib/mbedtls_errors/do-test.sh create mode 100644 lib/mbedtls_errors/error.fmt create mode 100644 lib/mbedtls_errors/generate_errors.diff create mode 100644 lib/mbedtls_errors/mp_mbedtls_errors.c create mode 100644 lib/mbedtls_errors/tester.c diff --git a/lib/mbedtls_errors/README.md b/lib/mbedtls_errors/README.md new file mode 100644 index 0000000000000..0e13021eb1910 --- /dev/null +++ b/lib/mbedtls_errors/README.md @@ -0,0 +1,42 @@ +MBEDTLS Error Strings for MicroPython +===================================== + +This directory contains source code and tools to rework the Mbedtls error strings for +micropython to use less space. In short, instead of storing and printing something like +"SSL - Our own certificate(s) is/are too large to send in an SSL message" it prints +the name of the error #define, which would be "MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE" in +this case, and only stores `SSL_CERTIFICATE_TOO_LARGE` in flash. The exact Mbedtls error +defines are used because they're easy to search for to find more detailed information. + +Mbedtls defines a specific format for error value #defines and +includes a Perl script to gather all `MBEDTLS_ERR` defines from includes files together with +english error text. From that the Perl script generates `mbedtls_strerror()`. The files in this +directory modify this process to produce a more space efficient error lookup table with +shorter error strings. + +The files are as follows: +- `generate_errors.diff` - diff for original mbedtls perl script +- `error.fmt` - modified code template for MicroPython +- `mp_mbedtls_errors.c` - source file with `mbedtls_strerror` this is built using the include + files in `../mbedtls` +- `do-mp.sh` - shell script to produce `mp_mbedtls_errors.c` +- `tester.c` - simple C main to test `mp_mbedtls_errors.c` locally on a dev box +- `do-test.sh` - shell script to produce `mp_mbedtls_errors.c` and compile the `tester` app +- `do-esp32.sh` - shell script to produce `esp32_mbedtls_errors.c` -- see below + +In order not to store multiple copies of `mbedtls_errors.c` +([https://github.com/micropython/micropython/pull/5819#discussion_r445528006](see)) +it is assumed that all ports use the same version of mbedtls with the same error #defines. +This is true as of MP v1.13, and ESP-IDF versions 3.3.2 and 4.0.1. If anything changes in the +future the `do-esp32.sh` script can be used to generate an esp32-specific version. + +### How-to + +- To build MicroPython all that is needed is to include the `mp_mbedtls_errors.c` into the build + (the Makefiles do this automatically). Note that Perl is not needed for routine MicroPython + builds. +- When a new version of Mbedtls is pulled-in the `do-mp.sh` script should be run to + re-generate `mp_mbedtls_errors.c`. +- The `tester` app should be run if changes to the string handling in `error.fmt` are made: + it tests that there is not an off-by-one error in the string copying/appending, etc. +- To include `mbedtls_strerror` error strings define `MBEDTLS_ERROR_C` in the build. diff --git a/lib/mbedtls_errors/do-esp32.sh b/lib/mbedtls_errors/do-esp32.sh new file mode 100755 index 0000000000000..6fd4682415c3f --- /dev/null +++ b/lib/mbedtls_errors/do-esp32.sh @@ -0,0 +1,7 @@ +#! /bin/bash -e +# Generate esp32_mbedtls_errors.c for use in the Esp32 port, with the ESP-IDF version of mbedtls +# The IDF_PATH env var must be set to the top-level dir of ESPIDF +echo "IDF_PATH=$IDF_PATH" +MBEDTLS=$IDF_PATH/components/mbedtls/mbedtls +patch -o esp32_generate_errors.pl $MBEDTLS/scripts/generate_errors.pl +#endif + +#if defined(MBEDTLS_PLATFORM_C) +#include "mbedtls/platform.h" +#else +#define mbedtls_snprintf snprintf +#define mbedtls_time_t time_t +#endif + +#if defined(MBEDTLS_ERROR_C) + +#include + +HEADER_INCLUDED + +// Error code table type +struct ssl_errs { + int16_t errnum; + const char *errstr; +}; + +// Table of high level error codes +static const struct ssl_errs mbedtls_high_level_error_tab[] = { +// BEGIN generated code +HIGH_LEVEL_CODE_CHECKS +// END generated code +}; + +static const struct ssl_errs mbedtls_low_level_error_tab[] = { +// Low level error codes +// +// BEGIN generated code +LOW_LEVEL_CODE_CHECKS +// END generated code +}; + +static const char *mbedtls_err_prefix = "MBEDTLS_ERR_"; +#define MBEDTLS_ERR_PREFIX_LEN ( sizeof("MBEDTLS_ERR_")-1 ) + +// copy error text into buffer, ensure null termination, return strlen of result +static size_t mbedtls_err_to_str(int err, const struct ssl_errs tab[], int tab_len, char *buf, size_t buflen) { + if (buflen == 0) return 0; + + // prefix for all error names + strncpy(buf, mbedtls_err_prefix, buflen); + if (buflen <= MBEDTLS_ERR_PREFIX_LEN+1) { + buf[buflen-1] = 0; + return buflen-1; + } + + // append error name from table + for (int i = 0; i < tab_len; i++) { + if (tab[i].errnum == err) { + strncpy(buf+MBEDTLS_ERR_PREFIX_LEN, tab[i].errstr, buflen-MBEDTLS_ERR_PREFIX_LEN); + buf[buflen-1] = 0; + return strlen(buf); + } + } + + mbedtls_snprintf(buf+MBEDTLS_ERR_PREFIX_LEN, buflen-MBEDTLS_ERR_PREFIX_LEN, "UNKNOWN (0x%04X)", + err); + return strlen(buf); +} + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + +void mbedtls_strerror(int ret, char *buf, size_t buflen) { + int use_ret; + + if (buflen == 0) return; + + buf[buflen-1] = 0; + + if (ret < 0) ret = -ret; + + // + // High-level error codes + // + uint8_t got_hl = (ret & 0xFF80) != 0; + if (got_hl) { + use_ret = ret & 0xFF80; + + // special case +#if defined(MBEDTLS_SSL_TLS_C) + if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) { + strncpy(buf, "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE", buflen); + buf[buflen-1] = 0; + return; + } +#endif + + size_t len = mbedtls_err_to_str(use_ret, mbedtls_high_level_error_tab, + ARRAY_SIZE(mbedtls_high_level_error_tab), buf, buflen); + + buf += len; + buflen -= len; + if (buflen == 0) return; + } + + // + // Low-level error codes + // + use_ret = ret & ~0xFF80; + + if (use_ret == 0) return; + + // If high level code is present, make a concatenation between both error strings. + if (got_hl) { + if (buflen < 2) return; + *buf++ = '+'; + buflen--; + } + + mbedtls_err_to_str(use_ret, mbedtls_low_level_error_tab, + ARRAY_SIZE(mbedtls_low_level_error_tab), buf, buflen); +} + +#else /* MBEDTLS_ERROR_C */ + +#if defined(MBEDTLS_ERROR_STRERROR_DUMMY) + +/* + * Provide an non-function in case MBEDTLS_ERROR_C is not defined + */ +void mbedtls_strerror( int ret, char *buf, size_t buflen ) +{ + ((void) ret); + + if( buflen > 0 ) + buf[0] = '\0'; +} + +#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */ + +#endif /* MBEDTLS_ERROR_C */ diff --git a/lib/mbedtls_errors/generate_errors.diff b/lib/mbedtls_errors/generate_errors.diff new file mode 100644 index 0000000000000..ad24c372faed7 --- /dev/null +++ b/lib/mbedtls_errors/generate_errors.diff @@ -0,0 +1,22 @@ +--- generate_errors_orig.pl 2020-06-20 08:40:38.819060379 -0700 ++++ generate_errors.pl 2020-06-20 08:47:26.511163591 -0700 +@@ -162,16 +162,12 @@ + + if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE") + { +- ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". +- "${white_space}\{\n". +- "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n". +- "${white_space} return;\n". +- "${white_space}}\n" ++ # no-op, this case is hard-coded in error.fmt + } + else + { +- ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". +- "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n" ++ my $error_text = $error_name =~ s/^MBEDTLS_ERR_//r; ++ ${$code_check} .= "${white_space}{ -($error_name), \"$error_text\" },\n" + } + }; + diff --git a/lib/mbedtls_errors/mp_mbedtls_errors.c b/lib/mbedtls_errors/mp_mbedtls_errors.c new file mode 100644 index 0000000000000..03a91f0dc9495 --- /dev/null +++ b/lib/mbedtls_errors/mp_mbedtls_errors.c @@ -0,0 +1,705 @@ +/* + * Error message information + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is part of mbed TLS (https://tls.mbed.org) + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) +#include "mbedtls/error.h" +#include +#endif + +#if defined(MBEDTLS_PLATFORM_C) +#include "mbedtls/platform.h" +#else +#define mbedtls_snprintf snprintf +#define mbedtls_time_t time_t +#endif + +#if defined(MBEDTLS_ERROR_C) + +#include + +#if defined(MBEDTLS_AES_C) +#include "mbedtls/aes.h" +#endif + +#if defined(MBEDTLS_ARC4_C) +#include "mbedtls/arc4.h" +#endif + +#if defined(MBEDTLS_ARIA_C) +#include "mbedtls/aria.h" +#endif + +#if defined(MBEDTLS_BASE64_C) +#include "mbedtls/base64.h" +#endif + +#if defined(MBEDTLS_BIGNUM_C) +#include "mbedtls/bignum.h" +#endif + +#if defined(MBEDTLS_BLOWFISH_C) +#include "mbedtls/blowfish.h" +#endif + +#if defined(MBEDTLS_CAMELLIA_C) +#include "mbedtls/camellia.h" +#endif + +#if defined(MBEDTLS_CCM_C) +#include "mbedtls/ccm.h" +#endif + +#if defined(MBEDTLS_CHACHA20_C) +#include "mbedtls/chacha20.h" +#endif + +#if defined(MBEDTLS_CHACHAPOLY_C) +#include "mbedtls/chachapoly.h" +#endif + +#if defined(MBEDTLS_CIPHER_C) +#include "mbedtls/cipher.h" +#endif + +#if defined(MBEDTLS_CMAC_C) +#include "mbedtls/cmac.h" +#endif + +#if defined(MBEDTLS_CTR_DRBG_C) +#include "mbedtls/ctr_drbg.h" +#endif + +#if defined(MBEDTLS_DES_C) +#include "mbedtls/des.h" +#endif + +#if defined(MBEDTLS_DHM_C) +#include "mbedtls/dhm.h" +#endif + +#if defined(MBEDTLS_ECP_C) +#include "mbedtls/ecp.h" +#endif + +#if defined(MBEDTLS_ENTROPY_C) +#include "mbedtls/entropy.h" +#endif + +#if defined(MBEDTLS_GCM_C) +#include "mbedtls/gcm.h" +#endif + +#if defined(MBEDTLS_HKDF_C) +#include "mbedtls/hkdf.h" +#endif + +#if defined(MBEDTLS_HMAC_DRBG_C) +#include "mbedtls/hmac_drbg.h" +#endif + +#if defined(MBEDTLS_MD_C) +#include "mbedtls/md.h" +#endif + +#if defined(MBEDTLS_MD2_C) +#include "mbedtls/md2.h" +#endif + +#if defined(MBEDTLS_MD4_C) +#include "mbedtls/md4.h" +#endif + +#if defined(MBEDTLS_MD5_C) +#include "mbedtls/md5.h" +#endif + +#if defined(MBEDTLS_NET_C) +#include "mbedtls/net_sockets.h" +#endif + +#if defined(MBEDTLS_OID_C) +#include "mbedtls/oid.h" +#endif + +#if defined(MBEDTLS_PADLOCK_C) +#include "mbedtls/padlock.h" +#endif + +#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C) +#include "mbedtls/pem.h" +#endif + +#if defined(MBEDTLS_PK_C) +#include "mbedtls/pk.h" +#endif + +#if defined(MBEDTLS_PKCS12_C) +#include "mbedtls/pkcs12.h" +#endif + +#if defined(MBEDTLS_PKCS5_C) +#include "mbedtls/pkcs5.h" +#endif + +#if defined(MBEDTLS_PLATFORM_C) +#include "mbedtls/platform.h" +#endif + +#if defined(MBEDTLS_POLY1305_C) +#include "mbedtls/poly1305.h" +#endif + +#if defined(MBEDTLS_RIPEMD160_C) +#include "mbedtls/ripemd160.h" +#endif + +#if defined(MBEDTLS_RSA_C) +#include "mbedtls/rsa.h" +#endif + +#if defined(MBEDTLS_SHA1_C) +#include "mbedtls/sha1.h" +#endif + +#if defined(MBEDTLS_SHA256_C) +#include "mbedtls/sha256.h" +#endif + +#if defined(MBEDTLS_SHA512_C) +#include "mbedtls/sha512.h" +#endif + +#if defined(MBEDTLS_SSL_TLS_C) +#include "mbedtls/ssl.h" +#endif + +#if defined(MBEDTLS_THREADING_C) +#include "mbedtls/threading.h" +#endif + +#if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C) +#include "mbedtls/x509.h" +#endif + +#if defined(MBEDTLS_XTEA_C) +#include "mbedtls/xtea.h" +#endif + + +// Error code table type +struct ssl_errs { + int16_t errnum; + const char *errstr; +}; + +// Table of high level error codes +static const struct ssl_errs mbedtls_high_level_error_tab[] = { +// BEGIN generated code +#if defined(MBEDTLS_CIPHER_C) + { -(MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE), "CIPHER_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA), "CIPHER_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_CIPHER_ALLOC_FAILED), "CIPHER_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_CIPHER_INVALID_PADDING), "CIPHER_INVALID_PADDING" }, + { -(MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED), "CIPHER_FULL_BLOCK_EXPECTED" }, + { -(MBEDTLS_ERR_CIPHER_AUTH_FAILED), "CIPHER_AUTH_FAILED" }, + { -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT), "CIPHER_INVALID_CONTEXT" }, + { -(MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED), "CIPHER_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_CIPHER_C */ + +#if defined(MBEDTLS_DHM_C) + { -(MBEDTLS_ERR_DHM_BAD_INPUT_DATA), "DHM_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED), "DHM_READ_PARAMS_FAILED" }, + { -(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED), "DHM_MAKE_PARAMS_FAILED" }, + { -(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED), "DHM_READ_PUBLIC_FAILED" }, + { -(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED), "DHM_MAKE_PUBLIC_FAILED" }, + { -(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED), "DHM_CALC_SECRET_FAILED" }, + { -(MBEDTLS_ERR_DHM_INVALID_FORMAT), "DHM_INVALID_FORMAT" }, + { -(MBEDTLS_ERR_DHM_ALLOC_FAILED), "DHM_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_DHM_FILE_IO_ERROR), "DHM_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_DHM_HW_ACCEL_FAILED), "DHM_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_DHM_SET_GROUP_FAILED), "DHM_SET_GROUP_FAILED" }, +#endif /* MBEDTLS_DHM_C */ + +#if defined(MBEDTLS_ECP_C) + { -(MBEDTLS_ERR_ECP_BAD_INPUT_DATA), "ECP_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL), "ECP_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE), "ECP_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_ECP_VERIFY_FAILED), "ECP_VERIFY_FAILED" }, + { -(MBEDTLS_ERR_ECP_ALLOC_FAILED), "ECP_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_ECP_RANDOM_FAILED), "ECP_RANDOM_FAILED" }, + { -(MBEDTLS_ERR_ECP_INVALID_KEY), "ECP_INVALID_KEY" }, + { -(MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH), "ECP_SIG_LEN_MISMATCH" }, + { -(MBEDTLS_ERR_ECP_HW_ACCEL_FAILED), "ECP_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_ECP_IN_PROGRESS), "ECP_IN_PROGRESS" }, +#endif /* MBEDTLS_ECP_C */ + +#if defined(MBEDTLS_MD_C) + { -(MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE), "MD_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_MD_BAD_INPUT_DATA), "MD_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_MD_ALLOC_FAILED), "MD_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_MD_FILE_IO_ERROR), "MD_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_MD_HW_ACCEL_FAILED), "MD_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_MD_C */ + +#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C) + { -(MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT), "PEM_NO_HEADER_FOOTER_PRESENT" }, + { -(MBEDTLS_ERR_PEM_INVALID_DATA), "PEM_INVALID_DATA" }, + { -(MBEDTLS_ERR_PEM_ALLOC_FAILED), "PEM_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_PEM_INVALID_ENC_IV), "PEM_INVALID_ENC_IV" }, + { -(MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG), "PEM_UNKNOWN_ENC_ALG" }, + { -(MBEDTLS_ERR_PEM_PASSWORD_REQUIRED), "PEM_PASSWORD_REQUIRED" }, + { -(MBEDTLS_ERR_PEM_PASSWORD_MISMATCH), "PEM_PASSWORD_MISMATCH" }, + { -(MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE), "PEM_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_PEM_BAD_INPUT_DATA), "PEM_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */ + +#if defined(MBEDTLS_PK_C) + { -(MBEDTLS_ERR_PK_ALLOC_FAILED), "PK_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_PK_TYPE_MISMATCH), "PK_TYPE_MISMATCH" }, + { -(MBEDTLS_ERR_PK_BAD_INPUT_DATA), "PK_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_PK_FILE_IO_ERROR), "PK_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_PK_KEY_INVALID_VERSION), "PK_KEY_INVALID_VERSION" }, + { -(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT), "PK_KEY_INVALID_FORMAT" }, + { -(MBEDTLS_ERR_PK_UNKNOWN_PK_ALG), "PK_UNKNOWN_PK_ALG" }, + { -(MBEDTLS_ERR_PK_PASSWORD_REQUIRED), "PK_PASSWORD_REQUIRED" }, + { -(MBEDTLS_ERR_PK_PASSWORD_MISMATCH), "PK_PASSWORD_MISMATCH" }, + { -(MBEDTLS_ERR_PK_INVALID_PUBKEY), "PK_INVALID_PUBKEY" }, + { -(MBEDTLS_ERR_PK_INVALID_ALG), "PK_INVALID_ALG" }, + { -(MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE), "PK_UNKNOWN_NAMED_CURVE" }, + { -(MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE), "PK_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_PK_SIG_LEN_MISMATCH), "PK_SIG_LEN_MISMATCH" }, + { -(MBEDTLS_ERR_PK_HW_ACCEL_FAILED), "PK_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_PK_C */ + +#if defined(MBEDTLS_PKCS12_C) + { -(MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA), "PKCS12_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE), "PKCS12_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT), "PKCS12_PBE_INVALID_FORMAT" }, + { -(MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH), "PKCS12_PASSWORD_MISMATCH" }, +#endif /* MBEDTLS_PKCS12_C */ + +#if defined(MBEDTLS_PKCS5_C) + { -(MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA), "PKCS5_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_PKCS5_INVALID_FORMAT), "PKCS5_INVALID_FORMAT" }, + { -(MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE), "PKCS5_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH), "PKCS5_PASSWORD_MISMATCH" }, +#endif /* MBEDTLS_PKCS5_C */ + +#if defined(MBEDTLS_RSA_C) + { -(MBEDTLS_ERR_RSA_BAD_INPUT_DATA), "RSA_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_RSA_INVALID_PADDING), "RSA_INVALID_PADDING" }, + { -(MBEDTLS_ERR_RSA_KEY_GEN_FAILED), "RSA_KEY_GEN_FAILED" }, + { -(MBEDTLS_ERR_RSA_KEY_CHECK_FAILED), "RSA_KEY_CHECK_FAILED" }, + { -(MBEDTLS_ERR_RSA_PUBLIC_FAILED), "RSA_PUBLIC_FAILED" }, + { -(MBEDTLS_ERR_RSA_PRIVATE_FAILED), "RSA_PRIVATE_FAILED" }, + { -(MBEDTLS_ERR_RSA_VERIFY_FAILED), "RSA_VERIFY_FAILED" }, + { -(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE), "RSA_OUTPUT_TOO_LARGE" }, + { -(MBEDTLS_ERR_RSA_RNG_FAILED), "RSA_RNG_FAILED" }, + { -(MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION), "RSA_UNSUPPORTED_OPERATION" }, + { -(MBEDTLS_ERR_RSA_HW_ACCEL_FAILED), "RSA_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_RSA_C */ + +#if defined(MBEDTLS_SSL_TLS_C) + { -(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE), "SSL_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_SSL_BAD_INPUT_DATA), "SSL_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_SSL_INVALID_MAC), "SSL_INVALID_MAC" }, + { -(MBEDTLS_ERR_SSL_INVALID_RECORD), "SSL_INVALID_RECORD" }, + { -(MBEDTLS_ERR_SSL_CONN_EOF), "SSL_CONN_EOF" }, + { -(MBEDTLS_ERR_SSL_UNKNOWN_CIPHER), "SSL_UNKNOWN_CIPHER" }, + { -(MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN), "SSL_NO_CIPHER_CHOSEN" }, + { -(MBEDTLS_ERR_SSL_NO_RNG), "SSL_NO_RNG" }, + { -(MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE), "SSL_NO_CLIENT_CERTIFICATE" }, + { -(MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE), "SSL_CERTIFICATE_TOO_LARGE" }, + { -(MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED), "SSL_CERTIFICATE_REQUIRED" }, + { -(MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED), "SSL_PRIVATE_KEY_REQUIRED" }, + { -(MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED), "SSL_CA_CHAIN_REQUIRED" }, + { -(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE), "SSL_UNEXPECTED_MESSAGE" }, + { -(MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED), "SSL_PEER_VERIFY_FAILED" }, + { -(MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY), "SSL_PEER_CLOSE_NOTIFY" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO), "SSL_BAD_HS_CLIENT_HELLO" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO), "SSL_BAD_HS_SERVER_HELLO" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE), "SSL_BAD_HS_CERTIFICATE" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST), "SSL_BAD_HS_CERTIFICATE_REQUEST" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE), "SSL_BAD_HS_SERVER_KEY_EXCHANGE" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE), "SSL_BAD_HS_SERVER_HELLO_DONE" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE), "SSL_BAD_HS_CLIENT_KEY_EXCHANGE" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP), "SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS), "SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY), "SSL_BAD_HS_CERTIFICATE_VERIFY" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC), "SSL_BAD_HS_CHANGE_CIPHER_SPEC" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_FINISHED), "SSL_BAD_HS_FINISHED" }, + { -(MBEDTLS_ERR_SSL_ALLOC_FAILED), "SSL_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED), "SSL_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH), "SSL_HW_ACCEL_FALLTHROUGH" }, + { -(MBEDTLS_ERR_SSL_COMPRESSION_FAILED), "SSL_COMPRESSION_FAILED" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION), "SSL_BAD_HS_PROTOCOL_VERSION" }, + { -(MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET), "SSL_BAD_HS_NEW_SESSION_TICKET" }, + { -(MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED), "SSL_SESSION_TICKET_EXPIRED" }, + { -(MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH), "SSL_PK_TYPE_MISMATCH" }, + { -(MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY), "SSL_UNKNOWN_IDENTITY" }, + { -(MBEDTLS_ERR_SSL_INTERNAL_ERROR), "SSL_INTERNAL_ERROR" }, + { -(MBEDTLS_ERR_SSL_COUNTER_WRAPPING), "SSL_COUNTER_WRAPPING" }, + { -(MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO), "SSL_WAITING_SERVER_HELLO_RENEGO" }, + { -(MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED), "SSL_HELLO_VERIFY_REQUIRED" }, + { -(MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL), "SSL_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE), "SSL_NO_USABLE_CIPHERSUITE" }, + { -(MBEDTLS_ERR_SSL_WANT_READ), "SSL_WANT_READ" }, + { -(MBEDTLS_ERR_SSL_WANT_WRITE), "SSL_WANT_WRITE" }, + { -(MBEDTLS_ERR_SSL_TIMEOUT), "SSL_TIMEOUT" }, + { -(MBEDTLS_ERR_SSL_CLIENT_RECONNECT), "SSL_CLIENT_RECONNECT" }, + { -(MBEDTLS_ERR_SSL_UNEXPECTED_RECORD), "SSL_UNEXPECTED_RECORD" }, + { -(MBEDTLS_ERR_SSL_NON_FATAL), "SSL_NON_FATAL" }, + { -(MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH), "SSL_INVALID_VERIFY_HASH" }, + { -(MBEDTLS_ERR_SSL_CONTINUE_PROCESSING), "SSL_CONTINUE_PROCESSING" }, + { -(MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS), "SSL_ASYNC_IN_PROGRESS" }, + { -(MBEDTLS_ERR_SSL_EARLY_MESSAGE), "SSL_EARLY_MESSAGE" }, + { -(MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS), "SSL_CRYPTO_IN_PROGRESS" }, +#endif /* MBEDTLS_SSL_TLS_C */ + +#if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C) + { -(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE), "X509_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_X509_UNKNOWN_OID), "X509_UNKNOWN_OID" }, + { -(MBEDTLS_ERR_X509_INVALID_FORMAT), "X509_INVALID_FORMAT" }, + { -(MBEDTLS_ERR_X509_INVALID_VERSION), "X509_INVALID_VERSION" }, + { -(MBEDTLS_ERR_X509_INVALID_SERIAL), "X509_INVALID_SERIAL" }, + { -(MBEDTLS_ERR_X509_INVALID_ALG), "X509_INVALID_ALG" }, + { -(MBEDTLS_ERR_X509_INVALID_NAME), "X509_INVALID_NAME" }, + { -(MBEDTLS_ERR_X509_INVALID_DATE), "X509_INVALID_DATE" }, + { -(MBEDTLS_ERR_X509_INVALID_SIGNATURE), "X509_INVALID_SIGNATURE" }, + { -(MBEDTLS_ERR_X509_INVALID_EXTENSIONS), "X509_INVALID_EXTENSIONS" }, + { -(MBEDTLS_ERR_X509_UNKNOWN_VERSION), "X509_UNKNOWN_VERSION" }, + { -(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG), "X509_UNKNOWN_SIG_ALG" }, + { -(MBEDTLS_ERR_X509_SIG_MISMATCH), "X509_SIG_MISMATCH" }, + { -(MBEDTLS_ERR_X509_CERT_VERIFY_FAILED), "X509_CERT_VERIFY_FAILED" }, + { -(MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT), "X509_CERT_UNKNOWN_FORMAT" }, + { -(MBEDTLS_ERR_X509_BAD_INPUT_DATA), "X509_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_X509_ALLOC_FAILED), "X509_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_X509_FILE_IO_ERROR), "X509_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL), "X509_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_X509_FATAL_ERROR), "X509_FATAL_ERROR" }, +#endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */ +// END generated code +}; + +static const struct ssl_errs mbedtls_low_level_error_tab[] = { +// Low level error codes +// +// BEGIN generated code +#if defined(MBEDTLS_AES_C) + { -(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH), "AES_INVALID_KEY_LENGTH" }, + { -(MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH), "AES_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_AES_BAD_INPUT_DATA), "AES_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE), "AES_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_AES_HW_ACCEL_FAILED), "AES_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_AES_C */ + +#if defined(MBEDTLS_ARC4_C) + { -(MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED), "ARC4_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_ARC4_C */ + +#if defined(MBEDTLS_ARIA_C) + { -(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA), "ARIA_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH), "ARIA_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE), "ARIA_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED), "ARIA_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_ARIA_C */ + +#if defined(MBEDTLS_ASN1_PARSE_C) + { -(MBEDTLS_ERR_ASN1_OUT_OF_DATA), "ASN1_OUT_OF_DATA" }, + { -(MBEDTLS_ERR_ASN1_UNEXPECTED_TAG), "ASN1_UNEXPECTED_TAG" }, + { -(MBEDTLS_ERR_ASN1_INVALID_LENGTH), "ASN1_INVALID_LENGTH" }, + { -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH), "ASN1_LENGTH_MISMATCH" }, + { -(MBEDTLS_ERR_ASN1_INVALID_DATA), "ASN1_INVALID_DATA" }, + { -(MBEDTLS_ERR_ASN1_ALLOC_FAILED), "ASN1_ALLOC_FAILED" }, + { -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL), "ASN1_BUF_TOO_SMALL" }, +#endif /* MBEDTLS_ASN1_PARSE_C */ + +#if defined(MBEDTLS_BASE64_C) + { -(MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL), "BASE64_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_BASE64_INVALID_CHARACTER), "BASE64_INVALID_CHARACTER" }, +#endif /* MBEDTLS_BASE64_C */ + +#if defined(MBEDTLS_BIGNUM_C) + { -(MBEDTLS_ERR_MPI_FILE_IO_ERROR), "MPI_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_MPI_BAD_INPUT_DATA), "MPI_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_MPI_INVALID_CHARACTER), "MPI_INVALID_CHARACTER" }, + { -(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL), "MPI_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_MPI_NEGATIVE_VALUE), "MPI_NEGATIVE_VALUE" }, + { -(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO), "MPI_DIVISION_BY_ZERO" }, + { -(MBEDTLS_ERR_MPI_NOT_ACCEPTABLE), "MPI_NOT_ACCEPTABLE" }, + { -(MBEDTLS_ERR_MPI_ALLOC_FAILED), "MPI_ALLOC_FAILED" }, +#endif /* MBEDTLS_BIGNUM_C */ + +#if defined(MBEDTLS_BLOWFISH_C) + { -(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA), "BLOWFISH_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH), "BLOWFISH_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED), "BLOWFISH_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_BLOWFISH_C */ + +#if defined(MBEDTLS_CAMELLIA_C) + { -(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA), "CAMELLIA_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH), "CAMELLIA_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED), "CAMELLIA_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_CAMELLIA_C */ + +#if defined(MBEDTLS_CCM_C) + { -(MBEDTLS_ERR_CCM_BAD_INPUT), "CCM_BAD_INPUT" }, + { -(MBEDTLS_ERR_CCM_AUTH_FAILED), "CCM_AUTH_FAILED" }, + { -(MBEDTLS_ERR_CCM_HW_ACCEL_FAILED), "CCM_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_CCM_C */ + +#if defined(MBEDTLS_CHACHA20_C) + { -(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA), "CHACHA20_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE), "CHACHA20_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED), "CHACHA20_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_CHACHA20_C */ + +#if defined(MBEDTLS_CHACHAPOLY_C) + { -(MBEDTLS_ERR_CHACHAPOLY_BAD_STATE), "CHACHAPOLY_BAD_STATE" }, + { -(MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED), "CHACHAPOLY_AUTH_FAILED" }, +#endif /* MBEDTLS_CHACHAPOLY_C */ + +#if defined(MBEDTLS_CMAC_C) + { -(MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED), "CMAC_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_CMAC_C */ + +#if defined(MBEDTLS_CTR_DRBG_C) + { -(MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED), "CTR_DRBG_ENTROPY_SOURCE_FAILED" }, + { -(MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG), "CTR_DRBG_REQUEST_TOO_BIG" }, + { -(MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG), "CTR_DRBG_INPUT_TOO_BIG" }, + { -(MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR), "CTR_DRBG_FILE_IO_ERROR" }, +#endif /* MBEDTLS_CTR_DRBG_C */ + +#if defined(MBEDTLS_DES_C) + { -(MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH), "DES_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_DES_HW_ACCEL_FAILED), "DES_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_DES_C */ + +#if defined(MBEDTLS_ENTROPY_C) + { -(MBEDTLS_ERR_ENTROPY_SOURCE_FAILED), "ENTROPY_SOURCE_FAILED" }, + { -(MBEDTLS_ERR_ENTROPY_MAX_SOURCES), "ENTROPY_MAX_SOURCES" }, + { -(MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED), "ENTROPY_NO_SOURCES_DEFINED" }, + { -(MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE), "ENTROPY_NO_STRONG_SOURCE" }, + { -(MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR), "ENTROPY_FILE_IO_ERROR" }, +#endif /* MBEDTLS_ENTROPY_C */ + +#if defined(MBEDTLS_GCM_C) + { -(MBEDTLS_ERR_GCM_AUTH_FAILED), "GCM_AUTH_FAILED" }, + { -(MBEDTLS_ERR_GCM_HW_ACCEL_FAILED), "GCM_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_GCM_BAD_INPUT), "GCM_BAD_INPUT" }, +#endif /* MBEDTLS_GCM_C */ + +#if defined(MBEDTLS_HKDF_C) + { -(MBEDTLS_ERR_HKDF_BAD_INPUT_DATA), "HKDF_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_HKDF_C */ + +#if defined(MBEDTLS_HMAC_DRBG_C) + { -(MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG), "HMAC_DRBG_REQUEST_TOO_BIG" }, + { -(MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG), "HMAC_DRBG_INPUT_TOO_BIG" }, + { -(MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR), "HMAC_DRBG_FILE_IO_ERROR" }, + { -(MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED), "HMAC_DRBG_ENTROPY_SOURCE_FAILED" }, +#endif /* MBEDTLS_HMAC_DRBG_C */ + +#if defined(MBEDTLS_MD2_C) + { -(MBEDTLS_ERR_MD2_HW_ACCEL_FAILED), "MD2_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_MD2_C */ + +#if defined(MBEDTLS_MD4_C) + { -(MBEDTLS_ERR_MD4_HW_ACCEL_FAILED), "MD4_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_MD4_C */ + +#if defined(MBEDTLS_MD5_C) + { -(MBEDTLS_ERR_MD5_HW_ACCEL_FAILED), "MD5_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_MD5_C */ + +#if defined(MBEDTLS_NET_C) + { -(MBEDTLS_ERR_NET_SOCKET_FAILED), "NET_SOCKET_FAILED" }, + { -(MBEDTLS_ERR_NET_CONNECT_FAILED), "NET_CONNECT_FAILED" }, + { -(MBEDTLS_ERR_NET_BIND_FAILED), "NET_BIND_FAILED" }, + { -(MBEDTLS_ERR_NET_LISTEN_FAILED), "NET_LISTEN_FAILED" }, + { -(MBEDTLS_ERR_NET_ACCEPT_FAILED), "NET_ACCEPT_FAILED" }, + { -(MBEDTLS_ERR_NET_RECV_FAILED), "NET_RECV_FAILED" }, + { -(MBEDTLS_ERR_NET_SEND_FAILED), "NET_SEND_FAILED" }, + { -(MBEDTLS_ERR_NET_CONN_RESET), "NET_CONN_RESET" }, + { -(MBEDTLS_ERR_NET_UNKNOWN_HOST), "NET_UNKNOWN_HOST" }, + { -(MBEDTLS_ERR_NET_BUFFER_TOO_SMALL), "NET_BUFFER_TOO_SMALL" }, + { -(MBEDTLS_ERR_NET_INVALID_CONTEXT), "NET_INVALID_CONTEXT" }, + { -(MBEDTLS_ERR_NET_POLL_FAILED), "NET_POLL_FAILED" }, + { -(MBEDTLS_ERR_NET_BAD_INPUT_DATA), "NET_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_NET_C */ + +#if defined(MBEDTLS_OID_C) + { -(MBEDTLS_ERR_OID_NOT_FOUND), "OID_NOT_FOUND" }, + { -(MBEDTLS_ERR_OID_BUF_TOO_SMALL), "OID_BUF_TOO_SMALL" }, +#endif /* MBEDTLS_OID_C */ + +#if defined(MBEDTLS_PADLOCK_C) + { -(MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED), "PADLOCK_DATA_MISALIGNED" }, +#endif /* MBEDTLS_PADLOCK_C */ + +#if defined(MBEDTLS_PLATFORM_C) + { -(MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED), "PLATFORM_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED), "PLATFORM_FEATURE_UNSUPPORTED" }, +#endif /* MBEDTLS_PLATFORM_C */ + +#if defined(MBEDTLS_POLY1305_C) + { -(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA), "POLY1305_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE), "POLY1305_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED), "POLY1305_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_POLY1305_C */ + +#if defined(MBEDTLS_RIPEMD160_C) + { -(MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED), "RIPEMD160_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_RIPEMD160_C */ + +#if defined(MBEDTLS_SHA1_C) + { -(MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED), "SHA1_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_SHA1_BAD_INPUT_DATA), "SHA1_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_SHA1_C */ + +#if defined(MBEDTLS_SHA256_C) + { -(MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED), "SHA256_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA), "SHA256_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_SHA256_C */ + +#if defined(MBEDTLS_SHA512_C) + { -(MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED), "SHA512_HW_ACCEL_FAILED" }, + { -(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA), "SHA512_BAD_INPUT_DATA" }, +#endif /* MBEDTLS_SHA512_C */ + +#if defined(MBEDTLS_THREADING_C) + { -(MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE), "THREADING_FEATURE_UNAVAILABLE" }, + { -(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA), "THREADING_BAD_INPUT_DATA" }, + { -(MBEDTLS_ERR_THREADING_MUTEX_ERROR), "THREADING_MUTEX_ERROR" }, +#endif /* MBEDTLS_THREADING_C */ + +#if defined(MBEDTLS_XTEA_C) + { -(MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH), "XTEA_INVALID_INPUT_LENGTH" }, + { -(MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED), "XTEA_HW_ACCEL_FAILED" }, +#endif /* MBEDTLS_XTEA_C */ +// END generated code +}; + +static const char *mbedtls_err_prefix = "MBEDTLS_ERR_"; +#define MBEDTLS_ERR_PREFIX_LEN ( sizeof("MBEDTLS_ERR_")-1 ) + +// copy error text into buffer, ensure null termination, return strlen of result +static size_t mbedtls_err_to_str(int err, const struct ssl_errs tab[], int tab_len, char *buf, size_t buflen) { + if (buflen == 0) return 0; + + // prefix for all error names + strncpy(buf, mbedtls_err_prefix, buflen); + if (buflen <= MBEDTLS_ERR_PREFIX_LEN+1) { + buf[buflen-1] = 0; + return buflen-1; + } + + // append error name from table + for (int i = 0; i < tab_len; i++) { + if (tab[i].errnum == err) { + strncpy(buf+MBEDTLS_ERR_PREFIX_LEN, tab[i].errstr, buflen-MBEDTLS_ERR_PREFIX_LEN); + buf[buflen-1] = 0; + return strlen(buf); + } + } + + mbedtls_snprintf(buf+MBEDTLS_ERR_PREFIX_LEN, buflen-MBEDTLS_ERR_PREFIX_LEN, "UNKNOWN (0x%04X)", + err); + return strlen(buf); +} + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + +void mbedtls_strerror(int ret, char *buf, size_t buflen) { + int use_ret; + + if (buflen == 0) return; + + buf[buflen-1] = 0; + + if (ret < 0) ret = -ret; + + // + // High-level error codes + // + uint8_t got_hl = (ret & 0xFF80) != 0; + if (got_hl) { + use_ret = ret & 0xFF80; + + // special case +#if defined(MBEDTLS_SSL_TLS_C) + if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) { + strncpy(buf, "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE", buflen); + buf[buflen-1] = 0; + return; + } +#endif + + size_t len = mbedtls_err_to_str(use_ret, mbedtls_high_level_error_tab, + ARRAY_SIZE(mbedtls_high_level_error_tab), buf, buflen); + + buf += len; + buflen -= len; + if (buflen == 0) return; + } + + // + // Low-level error codes + // + use_ret = ret & ~0xFF80; + + if (use_ret == 0) return; + + // If high level code is present, make a concatenation between both error strings. + if (got_hl) { + if (buflen < 2) return; + *buf++ = '+'; + buflen--; + } + + mbedtls_err_to_str(use_ret, mbedtls_low_level_error_tab, + ARRAY_SIZE(mbedtls_low_level_error_tab), buf, buflen); +} + +#else /* MBEDTLS_ERROR_C */ + +#if defined(MBEDTLS_ERROR_STRERROR_DUMMY) + +/* + * Provide an non-function in case MBEDTLS_ERROR_C is not defined + */ +void mbedtls_strerror( int ret, char *buf, size_t buflen ) +{ + ((void) ret); + + if( buflen > 0 ) + buf[0] = '\0'; +} + +#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */ + +#endif /* MBEDTLS_ERROR_C */ diff --git a/lib/mbedtls_errors/tester.c b/lib/mbedtls_errors/tester.c new file mode 100644 index 0000000000000..6f1c788f50ad8 --- /dev/null +++ b/lib/mbedtls_errors/tester.c @@ -0,0 +1,58 @@ +#include "mbedtls/error.h" +#include +#include + +// test_code checks that the provided code results in the provided error string for any size +// buffer. It calls mbedtls_strerror() to fill a buffer that is from 1 to 100 bytes in length +// and then checks that the buffer contents is OK and that a few guard bytes before and after +// the buffer were not overwritten. +int test_code(int code, char *str) { + char buf[100]; + int ok = 1; + int res; + + // test zero-length buffer + memset(buf, -3, 100); + mbedtls_strerror(code, buf + 4, 0); + for (int i = 0; i < 10; i++) { + if (buf[i] != -3) { + printf("Error: guard overwritten buflen=0 i=%d buf[i]=%d\n", i, buf[i]); + ok = 0; + } + } + + // test + for (size_t buflen = 1; buflen < 90; buflen++) { + memset(buf, -3, 100); + mbedtls_strerror(code, buf + 4, buflen); + for (int i = 0; i < 4; i++) { + if (buf[i] != -3) { + printf("Error: pre-guard overwritten buflen=%d i=%d buf[i]=%d\n", buflen, i, buf[i]); + ok = 0; + } + } + for (int i = 4 + buflen; i < 100; i++) { + if (buf[i] != -3) { + printf("Error: post-guard overwritten buflen=%d i=%d buf[i]=%d\n", buflen, i, buf[i]); + ok = 0; + } + } + char exp[100]; + strncpy(exp, str, buflen); + exp[buflen - 1] = 0; + if (strcmp(buf + 4, exp) != 0) { + printf("Error: expected %s, got %s\n", exp, buf); + ok = 0; + } + } + + printf("Test %x -> %s is %s\n", code, str, ok?"OK":"*** BAD ***"); +} + +int main() { + test_code(0x7200, "MBEDTLS_ERR_SSL_INVALID_RECORD"); + test_code(0x7780, "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE"); + test_code(0x0074, "MBEDTLS_ERR_SHA256_BAD_INPUT_DATA"); + test_code(0x6600 | 0x0074, "MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH+MBEDTLS_ERR_SHA256_BAD_INPUT_DATA"); + test_code(103, "MBEDTLS_ERR_UNKNOWN (0x0067)"); +} diff --git a/tools/codeformat.py b/tools/codeformat.py index 653529460c079..81a3cdcf8e5a2 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -38,6 +38,7 @@ "extmod/*.[ch]", "extmod/btstack/*.[ch]", "extmod/nimble/*.[ch]", + "lib/mbedtls_errors/tester.c", "lib/netutils/*.[ch]", "lib/timeutils/*.[ch]", "lib/utils/*.[ch]", From 5264478007c78b7737972404bda18ab39d792e17 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 2 Jul 2020 12:34:36 -0700 Subject: [PATCH 0178/3533] extmod/modussl_mbedtls: Integrate shorter error strings. The stm32 and esp32 ports now use shorter error strings for mbedtls errors. Also, MBEDTLS_ERROR_C is enabled on stm32 by default to get these strings. --- extmod/modussl_mbedtls.c | 20 ++++++++++++-------- ports/esp32/Makefile | 6 ++++-- ports/stm32/Makefile | 3 +++ ports/stm32/mbedtls/mbedtls_config.h | 1 + tests/net_inet/test_tls_sites.py | 2 +- tests/net_inet/tls_text_errors.py | 4 ++-- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 94061ddc8b946..1677dc6e1ca70 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -77,17 +77,21 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons #endif STATIC NORETURN void mbedtls_raise_error(int err) { - #if defined(MBEDTLS_ERROR_C) - // Including mbedtls_strerror takes about 16KB on the esp32 due to all the strings. - // MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror. - // It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile. - // "small" negative integer error codes come from underlying stream/sockets, not mbedtls + // _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the + // underlying socket into negative codes to pass them through mbedtls. Here we turn them + // positive again so they get interpreted as the OSError they really are. The + // cut-off of -256 is a bit hacky, sigh. if (err < 0 && err > -256) { mp_raise_OSError(-err); } + #if defined(MBEDTLS_ERROR_C) + // Including mbedtls_strerror takes about 1.5KB due to the error strings. + // MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror. + // It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile. + // Try to allocate memory for the message - #define ERR_STR_MAX 100 // mbedtls_strerror truncates if it doesn't fit + #define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX); if (o_str == NULL || o_str_buf == NULL) { @@ -96,7 +100,7 @@ STATIC NORETURN void mbedtls_raise_error(int err) { // print the error message into the allocated buffer mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX); - size_t len = strnlen((char *)o_str_buf, ERR_STR_MAX); + size_t len = strlen((char *)o_str_buf); // Put the exception object together o_str->base.type = &mp_type_str; @@ -108,7 +112,7 @@ STATIC NORETURN void mbedtls_raise_error(int err) { nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); #else // mbedtls is compiled without error strings so we simply return the err number - mp_raise_OSError(err); // typ. err is negative + mp_raise_OSError(err); // err is typically a large negative number #endif } diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 0f6a1969a6208..2cbe9f6be57f9 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -362,6 +362,7 @@ EXTMOD_SRC_C += $(addprefix extmod/,\ ) LIB_SRC_C = $(addprefix lib/,\ + mbedtls_errors/mp_mbedtls_errors.c \ mp-readline/readline.c \ netutils/netutils.c \ timeutils/timeutils.c \ @@ -506,11 +507,12 @@ ESPIDF_LWIP_O = $(patsubst %.c,%.o,\ $(wildcard $(ESPCOMP)/lwip/port/esp32/*/*.c) \ ) -ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\ +# Mbedtls source files, exclude error.c in favor of lib/mbedtls_errors/mp_mbedtls_errors.c +ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o, $(filter-out %/error.c,\ $(wildcard $(ESPCOMP)/mbedtls/mbedtls/library/*.c) \ $(wildcard $(ESPCOMP)/mbedtls/port/*.c) \ $(wildcard $(ESPCOMP)/mbedtls/port/esp32/*.c) \ - ) + )) ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c)) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 2614d4aa0fcf0..fe8f0b8711f0f 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -472,6 +472,9 @@ endif ifeq ($(MICROPY_SSL_MBEDTLS),1) CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' SRC_MOD += mbedtls/mbedtls_port.c +# replace mbedtls' error.c by ours +SRC_MOD := $(filter-out %/mbedtls/library/error.c, $(SRC_MOD)) +LIB_SRC_C += lib/mbedtls_errors/mp_mbedtls_errors.c endif ifeq ($(MICROPY_PY_BLUETOOTH),1) diff --git a/ports/stm32/mbedtls/mbedtls_config.h b/ports/stm32/mbedtls/mbedtls_config.h index 338c8b354146b..56fbbf3aaf086 100644 --- a/ports/stm32/mbedtls/mbedtls_config.h +++ b/ports/stm32/mbedtls/mbedtls_config.h @@ -67,6 +67,7 @@ #define MBEDTLS_CTR_DRBG_C //#define MBEDTLS_ECP_C #define MBEDTLS_ENTROPY_C +#define MBEDTLS_ERROR_C #define MBEDTLS_MD_C #define MBEDTLS_MD5_C #define MBEDTLS_OID_C diff --git a/tests/net_inet/test_tls_sites.py b/tests/net_inet/test_tls_sites.py index 876343acfc1d3..d2cb928c8d5b9 100644 --- a/tests/net_inet/test_tls_sites.py +++ b/tests/net_inet/test_tls_sites.py @@ -54,7 +54,7 @@ def main(): test_one(site, opts) print(site, "ok") except Exception as e: - print(site, repr(e)) + print(site, e) main() diff --git a/tests/net_inet/tls_text_errors.py b/tests/net_inet/tls_text_errors.py index 2ba167b868ef8..9e8ccfaf9ec77 100644 --- a/tests/net_inet/tls_text_errors.py +++ b/tests/net_inet/tls_text_errors.py @@ -14,10 +14,10 @@ def test(addr): print("wrap: no exception") except OSError as e: # mbedtls produces "mbedtls -0x7200: SSL - An invalid SSL record was received" - # axtls produces "RECORD_OVERFLOW" + # axtls produces "RECORD_OVERFLOW" but also prints "TLS buffer overflow,..." # CPython produces "[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)" ok = ( - "invalid SSL record" in str(e) + "SSL_INVALID_RECORD" in str(e) or "RECORD_OVERFLOW" in str(e) or "wrong version" in str(e) ) From 7dbef5377cd86a4f20a16acd3ad7798c4160aabc Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Fri, 26 Jun 2020 14:55:07 +0100 Subject: [PATCH 0179/3533] esp32/esp32_rmt: Properly fix looping behaviour of RMT. A previous commit 3a9d948032e27f690e1fb09084c36bd47b1a75a0 can cause lock-ups of the RMT driver, so this commit reverses that, adds a loop_en flag, and explicitly controls the TX interrupt in write_pulses(). This provides correct looping, non-blocking writes and sensible behaviour for wait_done(). See also #6167. --- ports/esp32/esp32_rmt.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 2ed4c9f6968c4..7971ca5d1c5af 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -56,6 +56,7 @@ typedef struct _esp32_rmt_obj_t { uint32_t carrier_freq; mp_uint_t num_items; rmt_item32_t *items; + bool loop_en; } esp32_rmt_obj_t; STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { @@ -93,6 +94,7 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz self->clock_div = clock_div; self->carrier_duty_percent = carrier_duty_percent; self->carrier_freq = carrier_freq; + self->loop_en = false; rmt_config_t config; config.rmt_mode = RMT_MODE_TX; @@ -110,8 +112,8 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz config.clk_div = self->clock_div; - check_esp_err(rmt_driver_install(config.channel, 0, 0)); check_esp_err(rmt_config(&config)); + check_esp_err(rmt_driver_install(config.channel, 0, 0)); return MP_OBJ_FROM_PTR(self); } @@ -180,7 +182,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_rmt_wait_done_obj, 1, esp32_rmt_wait_don STATIC mp_obj_t esp32_rmt_loop(mp_obj_t self_in, mp_obj_t loop) { esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_esp_err(rmt_set_tx_loop_mode(self->channel_id, mp_obj_get_int(loop))); + self->loop_en = mp_obj_get_int(loop); + if (!self->loop_en) { + bool loop_en; + check_esp_err(rmt_get_tx_loop_mode(self->channel_id, &loop_en)); + if (loop_en) { + check_esp_err(rmt_set_tx_loop_mode(self->channel_id, false)); + check_esp_err(rmt_set_tx_intr_en(self->channel_id, true)); + } + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_rmt_loop_obj, esp32_rmt_loop); @@ -222,8 +232,24 @@ STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *pos_args, self->items[item_index].level1 = start++; } } + + if (self->loop_en) { + bool loop_en; + check_esp_err(rmt_get_tx_loop_mode(self->channel_id, &loop_en)); + if (loop_en) { + check_esp_err(rmt_set_tx_intr_en(self->channel_id, true)); + check_esp_err(rmt_set_tx_loop_mode(self->channel_id, false)); + } + check_esp_err(rmt_wait_tx_done(self->channel_id, portMAX_DELAY)); + check_esp_err(rmt_set_tx_intr_en(self->channel_id, false)); + } + check_esp_err(rmt_write_items(self->channel_id, self->items, num_items, false /* non-blocking */)); + if (self->loop_en) { + check_esp_err(rmt_set_tx_loop_mode(self->channel_id, true)); + } + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_rmt_write_pulses_obj, 2, esp32_rmt_write_pulses); From 5f0e9d1bace44715e906cd0dbcb30f455b042ec2 Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Fri, 3 Jul 2020 14:14:33 +0100 Subject: [PATCH 0180/3533] docs/library: Update documentation of esp32's RMT. This explains how looping now works, and removes the warning about calling wait_done(). --- docs/library/esp32.rst | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index 715afdddeb169..dfde840a2141a 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -209,19 +209,21 @@ For more details see Espressif's `ESP-IDF RMT documentation. .. method:: RMT.wait_done(timeout=0) - Returns True if `RMT.write_pulses` has completed. + Returns ``True`` if the channel is currently transmitting a stream of pulses + started with a call to `RMT.write_pulses`. If *timeout* (defined in ticks of ``source_freq / clock_div``) is specified - the method will wait for *timeout* or until `RMT.write_pulses` is complete, - returning ``False`` if the channel continues to transmit. - -.. Warning:: - Avoid using ``wait_done()`` if looping is enabled. + the method will wait for *timeout* or until transmission is complete, + returning ``False`` if the channel continues to transmit. If looping is + enabled with `RMT.loop` and a stream has started, then this method will + always (wait and) return ``False``. .. method:: RMT.loop(enable_loop) - Configure looping on the channel, allowing a stream of pulses to be - indefinitely repeated. *enable_loop* is bool, set to True to enable looping. + Configure looping on the channel. *enable_loop* is bool, set to ``True`` to + enable looping on the *next* call to `RMT.write_pulses`. If called with + ``False`` while a looping stream is currently being transmitted then the + current set of pulses will be completed before transmission stops. .. method:: RMT.write_pulses(pulses, start) @@ -230,6 +232,15 @@ For more details see Espressif's `ESP-IDF RMT documentation. resolution ``(1 / (source_freq / clock_div))``. *start* defines whether the stream starts at 0 or 1. + If transmission of a stream is currently in progress then this method will + block until transmission of that stream has ended before beginning sending + *pulses*. + + If looping is enabled with `RMT.loop`, the stream of pulses will be repeated + indefinitely. Further calls to `RMT.write_pulses` will end the previous + stream - blocking until the last set of pulses has been transmitted - + before starting the next stream. + Ultra-Low-Power co-processor ---------------------------- From 76fefad18bb3f29d747d6f20adc722f7c8e44f84 Mon Sep 17 00:00:00 2001 From: Kenneth Ryerson Date: Mon, 20 Apr 2020 09:51:11 -0500 Subject: [PATCH 0181/3533] esp32/network_lan: Add support for IP101 PHY. Signed-off-by: Kenneth Ryerson --- ports/esp32/modnetwork.c | 1 + ports/esp32/modnetwork.h | 2 +- ports/esp32/network_lan.c | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 975029121ed0b..325b27f74b262 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -776,6 +776,7 @@ STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PHY_LAN8720), MP_ROM_INT(PHY_LAN8720) }, { MP_ROM_QSTR(MP_QSTR_PHY_TLK110), MP_ROM_INT(PHY_TLK110) }, + { MP_ROM_QSTR(MP_QSTR_PHY_IP101), MP_ROM_INT(PHY_IP101) }, // ETH Clock modes from ESP-IDF #if !MICROPY_ESP_IDF_4 diff --git a/ports/esp32/modnetwork.h b/ports/esp32/modnetwork.h index 64d2da018c056..db1b3d1300932 100644 --- a/ports/esp32/modnetwork.h +++ b/ports/esp32/modnetwork.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_ESP32_MODNETWORK_H #define MICROPY_INCLUDED_ESP32_MODNETWORK_H -enum { PHY_LAN8720, PHY_TLK110 }; +enum { PHY_LAN8720, PHY_TLK110, PHY_IP101 }; MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj); MP_DECLARE_CONST_FUN_OBJ_1(ppp_make_new_obj); diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index 1f9a733a7aef1..7a4ad49e01ceb 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -33,6 +33,7 @@ #include "eth_phy/phy.h" #include "eth_phy/phy_tlk110.h" #include "eth_phy/phy_lan8720.h" +#include "eth_phy/phy_ip101.h" #include "tcpip_adapter.h" #include "modnetwork.h" @@ -123,7 +124,9 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address")); } - if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) { + if (args[ARG_phy_type].u_int != PHY_LAN8720 && + args[ARG_phy_type].u_int != PHY_TLK110 && + args[ARG_phy_type].u_int != PHY_IP101) { mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type")); } @@ -145,6 +148,9 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar case PHY_LAN8720: config = phy_lan8720_default_ethernet_config; break; + case PHY_IP101: + config = phy_ip101_default_ethernet_config; + break; } self->link_func = config.phy_check_link; From f80b1d853599787597abe6ead9e04ef8e775818b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 19 Jul 2020 14:45:47 +1000 Subject: [PATCH 0182/3533] lib/stm32lib: Update library for H7 v1.6.0 and WB v1.6.0. Changes in this new library version are: - Update H7 HAL to v1.6.0. - Update WB HAL to v1.6.0. - Add patches to fix F4 ll_uart clock selection for UART9/UART10. Signed-off-by: Damien George --- lib/stm32lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stm32lib b/lib/stm32lib index 668d7a9e54aea..58fee7c92bd57 160000 --- a/lib/stm32lib +++ b/lib/stm32lib @@ -1 +1 @@ -Subproject commit 668d7a9e54aea98f8fe8a858eac1d3daa80fa824 +Subproject commit 58fee7c92bd576814d3f2afd92fbc62990270ecc From 895b1dbdda132b115bb0b5102962f4f46adb7adb Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 16 Jul 2020 23:20:30 +1000 Subject: [PATCH 0183/3533] tests/basics: Split out memoryview slice-assign tests to separate file. Signed-off-by: Damien George --- tests/basics/memoryview1.py | 48 ------------------- tests/basics/memoryview_slice_assign.py | 63 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 48 deletions(-) create mode 100644 tests/basics/memoryview_slice_assign.py diff --git a/tests/basics/memoryview1.py b/tests/basics/memoryview1.py index 1bfeabdfdc824..4c20c91f4947f 100644 --- a/tests/basics/memoryview1.py +++ b/tests/basics/memoryview1.py @@ -54,54 +54,6 @@ m[2] = 6 print(a) -# test slice assignment between memoryviews -b1 = bytearray(b'1234') -b2 = bytearray(b'5678') -b3 = bytearray(b'5678') -m1 = memoryview(b1) -m2 = memoryview(b2) -m3 = memoryview(b3) -m2[1:3] = m1[0:2] -print(b2) -b3[1:3] = m1[0:2] -print(b3) -m1[2:4] = b3[1:3] -print(b1) - -try: - m2[1:3] = b1[0:4] -except ValueError: - print("ValueError") - -try: - m2[1:3] = m1[0:4] -except ValueError: - print("ValueError") - -try: - m2[0:4] = m1[1:3] -except ValueError: - print("ValueError") - -# test memoryview of arrays with items sized larger than 1 -a1 = array.array('i', [0]*5) -m4 = memoryview(a1) -a2 = array.array('i', [3]*5) -m5 = memoryview(a2) -m4[1:3] = m5[1:3] -print(a1) - -try: - m4[1:3] = m2[1:3] -except ValueError: - print("ValueError") - -# invalid assignment on RHS -try: - memoryview(array.array('i'))[0:2] = b'1234' -except ValueError: - print('ValueError') - # invalid attribute try: memoryview(b'a').noexist diff --git a/tests/basics/memoryview_slice_assign.py b/tests/basics/memoryview_slice_assign.py new file mode 100644 index 0000000000000..b730dceba6e5d --- /dev/null +++ b/tests/basics/memoryview_slice_assign.py @@ -0,0 +1,63 @@ +# test slice assignment to memoryview + +try: + memoryview(bytearray(1))[:] = memoryview(bytearray(1)) +except (NameError, TypeError): + print("SKIP") + raise SystemExit + +try: + import uarray as array +except ImportError: + try: + import array + except ImportError: + print("SKIP") + raise SystemExit + +# test slice assignment between memoryviews +b1 = bytearray(b'1234') +b2 = bytearray(b'5678') +b3 = bytearray(b'5678') +m1 = memoryview(b1) +m2 = memoryview(b2) +m3 = memoryview(b3) +m2[1:3] = m1[0:2] +print(b2) +b3[1:3] = m1[0:2] +print(b3) +m1[2:4] = b3[1:3] +print(b1) + +# invalid slice assignments +try: + m2[1:3] = b1[0:4] +except ValueError: + print("ValueError") +try: + m2[1:3] = m1[0:4] +except ValueError: + print("ValueError") +try: + m2[0:4] = m1[1:3] +except ValueError: + print("ValueError") + +# test memoryview of arrays with items sized larger than 1 +a1 = array.array('i', [0]*5) +m4 = memoryview(a1) +a2 = array.array('i', [3]*5) +m5 = memoryview(a2) +m4[1:3] = m5[1:3] +print(a1) + +try: + m4[1:3] = m2[1:3] +except ValueError: + print("ValueError") + +# invalid assignment on RHS +try: + memoryview(array.array('i'))[0:2] = b'1234' +except ValueError: + print('ValueError') From a853fff838c1f995b9eeb0ebef5f117cfa3d5f0a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 16 Jul 2020 23:23:42 +1000 Subject: [PATCH 0184/3533] py/obj.h: Fix mp_seq_replace_slice_no_grow to use memmove not memcpy. Because the argument arrays may overlap, as show by the new tests in this commit. Also remove the debugging comments for these macros, add a new comment about overlapping regions, and separate the macros by blank lines to make them easier to read. Fixes issue #6244. Signed-off-by: Damien George --- py/obj.h | 8 ++++---- tests/basics/memoryview_slice_assign.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/py/obj.h b/py/obj.h index f621b1dadd492..31542a7f9bd7c 100644 --- a/py/obj.h +++ b/py/obj.h @@ -992,17 +992,17 @@ bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args); mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value); mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes); + // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz)) + +// Note: dest and slice regions may overlap #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \ - /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * (item_sz));*/ \ - memcpy(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \ - /*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * (item_sz));*/ \ + memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \ memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz)); // Note: dest and slice regions may overlap #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \ - /*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * (item_sz));*/ \ memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \ memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); diff --git a/tests/basics/memoryview_slice_assign.py b/tests/basics/memoryview_slice_assign.py index b730dceba6e5d..74f6fae6f788d 100644 --- a/tests/basics/memoryview_slice_assign.py +++ b/tests/basics/memoryview_slice_assign.py @@ -61,3 +61,27 @@ memoryview(array.array('i'))[0:2] = b'1234' except ValueError: print('ValueError') + +# test shift left of bytearray +b = bytearray(range(10)) +mv = memoryview(b) +mv[1:] = mv[:-1] +print(b) + +# test shift right of bytearray +b = bytearray(range(10)) +mv = memoryview(b) +mv[:-1] = mv[1:] +print(b) + +# test shift left of array +a = array.array('I', range(10)) +mv = memoryview(a) +mv[1:] = mv[:-1] +print(a) + +# test shift right of array +a = array.array('I', range(10)) +mv = memoryview(a) +mv[:-1] = mv[1:] +print(a) From 27767aafa21754e6cdbf52a719e7ce395f0fe672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20V=C3=B6r=C3=B6s?= Date: Sat, 15 Feb 2020 19:28:56 +0100 Subject: [PATCH 0185/3533] lib/libm_dbl: Add round.c source code. This code is imported from musl, to match existing code in libm_dbl. The file is also added to the build in stm32/Makefile. It's not needed by the core code but, similar to c5cc64175be32cb1e4f3f1a249667bc9f5a12613, allows round() to be used by user C modules or board extensions. --- lib/libm_dbl/round.c | 35 +++++++++++++++++++++++++++++++++++ ports/stm32/Makefile | 1 + 2 files changed, 36 insertions(+) create mode 100644 lib/libm_dbl/round.c diff --git a/lib/libm_dbl/round.c b/lib/libm_dbl/round.c new file mode 100644 index 0000000000000..130d58d2571e7 --- /dev/null +++ b/lib/libm_dbl/round.c @@ -0,0 +1,35 @@ +#include "libm.h" + +#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 +#define EPS DBL_EPSILON +#elif FLT_EVAL_METHOD==2 +#define EPS LDBL_EPSILON +#endif +static const double_t toint = 1/EPS; + +double round(double x) +{ + union {double f; uint64_t i;} u = {x}; + int e = u.i >> 52 & 0x7ff; + double_t y; + + if (e >= 0x3ff+52) + return x; + if (u.i >> 63) + x = -x; + if (e < 0x3ff-1) { + /* raise inexact if x!=0 */ + FORCE_EVAL(x + toint); + return 0*u.f; + } + y = x + toint - toint - x; + if (y > 0.5) + y = y + x - 1; + else if (y <= -0.5) + y = y + x + 1; + else + y = y + x; + if (u.i >> 63) + y = -y; + return y; +} diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index fe8f0b8711f0f..e7d2e2abc2caf 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -191,6 +191,7 @@ LIBM_SRC_C = $(addprefix lib/libm_dbl/,\ nearbyint.c \ pow.c \ rint.c \ + round.c \ scalbn.c \ sin.c \ sinh.c \ From d9b726102429f555aec12275bb740f315ae9a795 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Jul 2020 16:28:46 +1000 Subject: [PATCH 0186/3533] lib/libc: Fix string0's implementation of strncpy. Fixing 98e583430fb7b793119db27bad9f98119e81579f, the semantics of strncpy require that the remainder of dst be filled with null bytes. Signed-off-by: Damien George --- lib/libc/string0.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/libc/string0.c b/lib/libc/string0.c index 5f40a71e3e284..19ad14d0f7a77 100644 --- a/lib/libc/string0.c +++ b/lib/libc/string0.c @@ -178,8 +178,10 @@ char *strncpy(char *s1, const char *s2, size_t n) { while (n > 0) { n--; if ((*dst++ = *src++) == '\0') { - /* If we get here, we found a null character at the end of s2 */ - *dst = '\0'; + /* If we get here, we found a null character at the end + of s2, so use memset to put null bytes at the end of + s1. */ + memset(dst, '\0', n); break; } } From 0a79e183984fabb3d33b7879a2d0dc79948dd0de Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sun, 19 Jul 2020 12:42:09 +0200 Subject: [PATCH 0187/3533] nrf: Split mpconfigport.h into multiple files. Splitting mpconfigport.h into multiple device specific files in order to facilitate variations between devices. Due to the fact that the devices might have variations in features and also variations in flash size it makes sense that some devices offers more functionality than others without being limited by restricted devices. For example more micropython features can be activated for nrf52840 with 1MB flash, compared to nrf51 with 256KB. --- ports/nrf/mpconfigdevice_nrf51822.h | 37 +++++++++++++++++++++++++++++ ports/nrf/mpconfigdevice_nrf52832.h | 37 +++++++++++++++++++++++++++++ ports/nrf/mpconfigdevice_nrf52840.h | 37 +++++++++++++++++++++++++++++ ports/nrf/mpconfigdevice_nrf9160.h | 37 +++++++++++++++++++++++++++++ ports/nrf/mpconfigport.h | 12 ++++++++++ 5 files changed, 160 insertions(+) create mode 100644 ports/nrf/mpconfigdevice_nrf51822.h create mode 100644 ports/nrf/mpconfigdevice_nrf52832.h create mode 100644 ports/nrf/mpconfigdevice_nrf52840.h create mode 100644 ports/nrf/mpconfigdevice_nrf9160.h diff --git a/ports/nrf/mpconfigdevice_nrf51822.h b/ports/nrf/mpconfigdevice_nrf51822.h new file mode 100644 index 0000000000000..55d540be98b90 --- /dev/null +++ b/ports/nrf/mpconfigdevice_nrf51822.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Board overridable build configuration. + +#ifndef MICROPY_MBFS +#define MICROPY_MBFS (1) +#endif + +#ifndef MICROPY_VFS +#define MICROPY_VFS (0) +#endif + +// Board overridable feature configuration. diff --git a/ports/nrf/mpconfigdevice_nrf52832.h b/ports/nrf/mpconfigdevice_nrf52832.h new file mode 100644 index 0000000000000..55d540be98b90 --- /dev/null +++ b/ports/nrf/mpconfigdevice_nrf52832.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Board overridable build configuration. + +#ifndef MICROPY_MBFS +#define MICROPY_MBFS (1) +#endif + +#ifndef MICROPY_VFS +#define MICROPY_VFS (0) +#endif + +// Board overridable feature configuration. diff --git a/ports/nrf/mpconfigdevice_nrf52840.h b/ports/nrf/mpconfigdevice_nrf52840.h new file mode 100644 index 0000000000000..55d540be98b90 --- /dev/null +++ b/ports/nrf/mpconfigdevice_nrf52840.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Board overridable build configuration. + +#ifndef MICROPY_MBFS +#define MICROPY_MBFS (1) +#endif + +#ifndef MICROPY_VFS +#define MICROPY_VFS (0) +#endif + +// Board overridable feature configuration. diff --git a/ports/nrf/mpconfigdevice_nrf9160.h b/ports/nrf/mpconfigdevice_nrf9160.h new file mode 100644 index 0000000000000..55d540be98b90 --- /dev/null +++ b/ports/nrf/mpconfigdevice_nrf9160.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Board overridable build configuration. + +#ifndef MICROPY_MBFS +#define MICROPY_MBFS (1) +#endif + +#ifndef MICROPY_VFS +#define MICROPY_VFS (0) +#endif + +// Board overridable feature configuration. diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index c5304b20d0d3e..e84709c49dda8 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -29,6 +29,18 @@ #include +#if defined(NRF51822) + #include "mpconfigdevice_nrf51822.h" +#elif defined(NRF52832) + #include "mpconfigdevice_nrf52832.h" +#elif defined(NRF52840) + #include "mpconfigdevice_nrf52840.h" +#elif defined(NRF9160) + #include "mpconfigdevice_nrf9160.h" +#else + #pragma error "Device not defined" +#endif + // options to control how MicroPython is built #ifndef MICROPY_VFS #define MICROPY_VFS (0) From caaaa2b1f4645b72929a957cf5ceed182e619f4c Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sun, 19 Jul 2020 12:49:30 +0200 Subject: [PATCH 0188/3533] nrf: Enable more features for all targets. Enabling the following features for all targets, except for nrf51 targets compiled to be used with SoftDevice: - MICROPY_PY_ARRAY_SLICE_ASSIGN - MICROPY_PY_SYS_STDFILES - MICROPY_PY_UBINASCII --- ports/nrf/mpconfigdevice_nrf51822.h | 24 ++++++++++++++++++++++++ ports/nrf/mpconfigdevice_nrf52832.h | 12 ++++++++++++ ports/nrf/mpconfigdevice_nrf52840.h | 12 ++++++++++++ ports/nrf/mpconfigdevice_nrf9160.h | 12 ++++++++++++ ports/nrf/mpconfigport.h | 3 --- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/ports/nrf/mpconfigdevice_nrf51822.h b/ports/nrf/mpconfigdevice_nrf51822.h index 55d540be98b90..2f85c9f4c5468 100644 --- a/ports/nrf/mpconfigdevice_nrf51822.h +++ b/ports/nrf/mpconfigdevice_nrf51822.h @@ -35,3 +35,27 @@ #endif // Board overridable feature configuration. + +#ifndef MICROPY_PY_ARRAY_SLICE_ASSIGN +#if defined(BLUETOOTH_SD) +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0) +#else +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#endif +#endif + +#ifndef MICROPY_PY_SYS_STDFILES +#if defined(BLUETOOTH_SD) +#define MICROPY_PY_SYS_STDFILES (0) +#else +#define MICROPY_PY_SYS_STDFILES (1) +#endif +#endif + +#ifndef MICROPY_PY_UBINASCII +#if defined(BLUETOOTH_SD) +#define MICROPY_PY_UBINASCII (0) +#else +#define MICROPY_PY_UBINASCII (1) +#endif +#endif diff --git a/ports/nrf/mpconfigdevice_nrf52832.h b/ports/nrf/mpconfigdevice_nrf52832.h index 55d540be98b90..2bfd047ca1456 100644 --- a/ports/nrf/mpconfigdevice_nrf52832.h +++ b/ports/nrf/mpconfigdevice_nrf52832.h @@ -35,3 +35,15 @@ #endif // Board overridable feature configuration. + +#ifndef MICROPY_PY_ARRAY_SLICE_ASSIGN +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#endif + +#ifndef MICROPY_PY_SYS_STDFILES +#define MICROPY_PY_SYS_STDFILES (1) +#endif + +#ifndef MICROPY_PY_UBINASCII +#define MICROPY_PY_UBINASCII (1) +#endif diff --git a/ports/nrf/mpconfigdevice_nrf52840.h b/ports/nrf/mpconfigdevice_nrf52840.h index 55d540be98b90..2bfd047ca1456 100644 --- a/ports/nrf/mpconfigdevice_nrf52840.h +++ b/ports/nrf/mpconfigdevice_nrf52840.h @@ -35,3 +35,15 @@ #endif // Board overridable feature configuration. + +#ifndef MICROPY_PY_ARRAY_SLICE_ASSIGN +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#endif + +#ifndef MICROPY_PY_SYS_STDFILES +#define MICROPY_PY_SYS_STDFILES (1) +#endif + +#ifndef MICROPY_PY_UBINASCII +#define MICROPY_PY_UBINASCII (1) +#endif diff --git a/ports/nrf/mpconfigdevice_nrf9160.h b/ports/nrf/mpconfigdevice_nrf9160.h index 55d540be98b90..2bfd047ca1456 100644 --- a/ports/nrf/mpconfigdevice_nrf9160.h +++ b/ports/nrf/mpconfigdevice_nrf9160.h @@ -35,3 +35,15 @@ #endif // Board overridable feature configuration. + +#ifndef MICROPY_PY_ARRAY_SLICE_ASSIGN +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#endif + +#ifndef MICROPY_PY_SYS_STDFILES +#define MICROPY_PY_SYS_STDFILES (1) +#endif + +#ifndef MICROPY_PY_UBINASCII +#define MICROPY_PY_UBINASCII (1) +#endif diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index e84709c49dda8..1197df016cce9 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -117,11 +117,9 @@ #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) -#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0) #define MICROPY_PY_BUILTINS_SLICE_ATTRS (0) #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_MAXSIZE (1) -#define MICROPY_PY_SYS_STDFILES (0) #define MICROPY_PY_SYS_STDIO_BUFFER (0) #define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0) #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (0) @@ -129,7 +127,6 @@ #define MICROPY_PY_IO (0) #define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_UERRNO (0) -#define MICROPY_PY_UBINASCII (0) #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #define MICROPY_PY_UCTYPES (0) From cf9be201d780eedcdb8a6c05f5ea7c796f892f07 Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Thu, 2 Jul 2020 11:08:25 +1000 Subject: [PATCH 0189/3533] docs/library: Update pyb.SPI init method to add descr about "ti" arg. --- docs/library/pyb.SPI.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/library/pyb.SPI.rst b/docs/library/pyb.SPI.rst index a1910be49b7fb..181bdd3b6ec5d 100644 --- a/docs/library/pyb.SPI.rst +++ b/docs/library/pyb.SPI.rst @@ -64,6 +64,7 @@ Methods respectively. - ``bits`` can be 8 or 16, and is the number of bits in each transferred word. - ``firstbit`` can be ``SPI.MSB`` or ``SPI.LSB``. + - ``ti`` True indicates Texas Instruments, as opposed to Motorola, signal conventions. - ``crc`` can be None for no CRC, or a polynomial specifier. Note that the SPI clock frequency will not always be the requested baudrate. From 4a7c2731c5190d3bb7781f91a24a273d52acb289 Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Thu, 2 Jul 2020 12:34:45 +1000 Subject: [PATCH 0190/3533] docs/library: Update pyb.Timer to add missing args and defaults to init. --- docs/library/pyb.Timer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/pyb.Timer.rst b/docs/library/pyb.Timer.rst index bb7e7e52d5648..532b64da8e93c 100644 --- a/docs/library/pyb.Timer.rst +++ b/docs/library/pyb.Timer.rst @@ -62,7 +62,7 @@ Constructors Methods ------- -.. method:: Timer.init(\*, freq, prescaler, period) +.. method:: Timer.init(\*, freq, prescaler, period, mode=Timer.UP, div=1, callback=None, deadtime=0) Initialise the timer. Initialisation must be either by frequency (in Hz) or by prescaler and period:: From 47289f4bc94c02acd49144ba15d91e58342807ee Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Fri, 3 Jul 2020 15:17:50 +1000 Subject: [PATCH 0191/3533] docs/library: Update pyb.UART to correct pyboard UART availability. On original pyboard UART 5 isn't available; added pyboard D availability. --- docs/library/pyb.UART.rst | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/library/pyb.UART.rst b/docs/library/pyb.UART.rst index ab7ab2fb882a4..0a611a72566da 100644 --- a/docs/library/pyb.UART.rst +++ b/docs/library/pyb.UART.rst @@ -48,13 +48,16 @@ Constructors .. class:: pyb.UART(bus, ...) - Construct a UART object on the given bus. ``bus`` can be 1-6, or 'XA', 'XB', 'YA', or 'YB'. + Construct a UART object on the given bus. + For Pyboard ``bus`` can be 1-4, 6, 'XA', 'XB', 'YA', or 'YB'. + For Pyboard Lite ``bus`` can be 1, 2, 6, 'XB', or 'YA'. + For Pyboard D ``bus`` can be 1-4, 'XA', 'YA' or 'YB'. With no additional parameters, the UART object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. See ``init`` for parameters of initialisation. - The physical pins of the UART busses are: + The physical pins of the UART busses on Pyboard are: - ``UART(4)`` is on ``XA``: ``(TX, RX) = (X1, X2) = (PA0, PA1)`` - ``UART(1)`` is on ``XB``: ``(TX, RX) = (X9, X10) = (PB6, PB7)`` @@ -62,10 +65,22 @@ Constructors - ``UART(3)`` is on ``YB``: ``(TX, RX) = (Y9, Y10) = (PB10, PB11)`` - ``UART(2)`` is on: ``(TX, RX) = (X3, X4) = (PA2, PA3)`` - The Pyboard Lite supports UART(1), UART(2) and UART(6) only. Pins are as above except: + The Pyboard Lite supports UART(1), UART(2) and UART(6) only, pins are: + - ``UART(1)`` is on ``XB``: ``(TX, RX) = (X9, X10) = (PB6, PB7)`` + - ``UART(6)`` is on ``YA``: ``(TX, RX) = (Y1, Y2) = (PC6, PC7)`` - ``UART(2)`` is on: ``(TX, RX) = (X1, X2) = (PA2, PA3)`` + The Pyboard D supports UART(1), UART(2), UART(3) and UART(4) only, pins are: + + - ``UART(4)`` is on ``XA``: ``(TX, RX) = (X1, X2) = (PA0, PA1)`` + - ``UART(1)`` is on ``YA``: ``(TX, RX) = (Y1, Y2) = (PA9, PA10)`` + - ``UART(3)`` is on ``YB``: ``(TX, RX) = (Y9, Y10) = (PB10, PB11)`` + - ``UART(2)`` is on: ``(TX, RX) = (X3, X4) = (PA2, PA3)`` + + *Note:* Pyboard D has ``UART(1)`` on ``YA``, unlike Pyboard and Pyboard Lite that both + have ``UART(1)`` on ``XB`` and ``UART(6)`` on ``YA``. + Methods ------- From fe7d47971f7de0cc094b28ae34e49f40b41d456d Mon Sep 17 00:00:00 2001 From: Josh Lloyd Date: Mon, 20 Jul 2020 11:06:12 +1200 Subject: [PATCH 0192/3533] docs/esp32: Fix machine.Timer quickref to specify HW timers. Also remove trailing spaces on other lines. --- docs/esp32/quickref.rst | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index b4f443a910009..d5c222f3a1eef 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -118,17 +118,21 @@ Use the :mod:`time ` module:: Timers ------ -Virtual (RTOS-based) timers are supported. Use the :ref:`machine.Timer ` class -with timer ID of -1:: +The ESP32 port has four hardware timers. Use the :ref:`machine.Timer ` class +with a timer ID from 0 to 3 (inclusive):: from machine import Timer - tim = Timer(-1) - tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1)) - tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(2)) + tim0 = Timer(0) + tim0.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(0)) + + tim1 = Timer(1) + tim1.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(1)) The period is in milliseconds. +Virtual timers are not currently supported on this port. + .. _Pins_and_GPIO: Pins and GPIO @@ -172,7 +176,7 @@ PWM (pulse width modulation) PWM can be enabled on all output-enabled pins. The base frequency can range from 1Hz to 40MHz but there is a tradeoff; as the base frequency -*increases* the duty resolution *decreases*. See +*increases* the duty resolution *decreases*. See `LED Control `_ for more details. Currently the duty cycle has to be in the range of 0-1023. @@ -273,7 +277,7 @@ class:: .. Warning:: Currently *all* of ``sck``, ``mosi`` and ``miso`` *must* be specified when - initialising Software SPI. + initialising Software SPI. Hardware SPI bus ---------------- @@ -445,11 +449,11 @@ Use the ``TouchPad`` class in the ``machine`` module:: from machine import TouchPad, Pin t = TouchPad(Pin(14)) - t.read() # Returns a smaller number when touched + t.read() # Returns a smaller number when touched ``TouchPad.read`` returns a value relative to the capacitive variation. Small numbers (typically in -the *tens*) are common when a pin is touched, larger numbers (above *one thousand*) when -no touch is present. However the values are *relative* and can vary depending on the board +the *tens*) are common when a pin is touched, larger numbers (above *one thousand*) when +no touch is present. However the values are *relative* and can vary depending on the board and surrounding composition so some calibration may be required. There are ten capacitive touch-enabled pins that can be used on the ESP32: 0, 2, 4, 12, 13 From 37e1b5c891f9964bb6c95228bc2d718511507a69 Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Tue, 21 Jul 2020 18:47:28 +0100 Subject: [PATCH 0193/3533] py/compile: Don't await __aiter__ special method in async-for. MicroPython's original implementation of __aiter__ was correct for an earlier (provisional) version of PEP492 (CPython 3.5), where __aiter__ was an async-def function. But that changed in the final version of PEP492 (in CPython 3.5.2) where the function was changed to a normal one. See https://www.python.org/dev/peps/pep-0492/#why-aiter-does-not-return-an-awaitable See also the note at the end of this subsection in the docs: https://docs.python.org/3.5/reference/datamodel.html#asynchronous-iterators And for completeness the BPO: https://bugs.python.org/issue27243 To be consistent with the Python spec as it stands today (and now that PEP492 is final) this commit changes MicroPython's behaviour to match CPython: __aiter__ should return an async-iterable object, but is not itself awaitable. The relevant tests are updated to match. See #6267. --- py/compile.c | 3 ++- tests/basics/async_for.py | 2 +- tests/basics/async_for2.py | 5 ++--- tests/basics/async_for2.py.exp | 4 ---- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/py/compile.c b/py/compile.c index 53108b7062b78..d1a4d65c8bb51 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1798,7 +1798,8 @@ STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns uint try_finally_label = comp_next_label(comp); compile_node(comp, pns->nodes[1]); // iterator - compile_await_object_method(comp, MP_QSTR___aiter__); + EMIT_ARG(load_method, MP_QSTR___aiter__, false); + EMIT_ARG(call_method, 0, 0, 0); compile_store_id(comp, context); START_BREAK_CONTINUE_BLOCK diff --git a/tests/basics/async_for.py b/tests/basics/async_for.py index 6b4e136d593d4..5fd0540828afe 100644 --- a/tests/basics/async_for.py +++ b/tests/basics/async_for.py @@ -6,7 +6,7 @@ def __init__(self, obj): print('init') self._it = iter(obj) - async def __aiter__(self): + def __aiter__(self): print('aiter') return self diff --git a/tests/basics/async_for2.py b/tests/basics/async_for2.py index 89584fcb100f3..aad23a3e5ad8c 100644 --- a/tests/basics/async_for2.py +++ b/tests/basics/async_for2.py @@ -1,4 +1,4 @@ -# test waiting within "async for" aiter/anext functions +# test waiting within "async for" __anext__ function import sys if sys.implementation.name == 'micropython': @@ -21,9 +21,8 @@ def __init__(self, high): self.cur = 0 self.high = high - async def __aiter__(self): + def __aiter__(self): print('aiter') - print('f returned:', await f(10)) return self async def __anext__(self): diff --git a/tests/basics/async_for2.py.exp b/tests/basics/async_for2.py.exp index 886232f7bab73..52bbe90c85376 100644 --- a/tests/basics/async_for2.py.exp +++ b/tests/basics/async_for2.py.exp @@ -1,9 +1,5 @@ init aiter -f start: 10 -coro yielded: 11 -coro yielded: 12 -f returned: 13 anext f start: 20 coro yielded: 21 From fd2ff867a08fd174f0bbf7a03aa59547634e91ac Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Jul 2020 23:45:08 +1000 Subject: [PATCH 0194/3533] stm32/usbdev: Fix calculation of SCSI LUN size with multiple LUNs. The SCSI driver calls GetCapacity to get the block size and number of blocks of the underlying block-device/LUN. It caches these values and uses them later on to verify that reads/writes are within the bounds of the LUN. But, prior to this commit, there was only one set of cached values for all LUNs, so the bounds checking for a LUN could use incorrect values, values from one of the other LUNs that most recently updated the cached values. This would lead to failed SCSI requests. This commit fixes this issue by having separate cached values for each LUN. Signed-off-by: Damien George --- ports/stm32/usbd_msc_interface.h | 2 -- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 7 ++-- ports/stm32/usbdev/class/src/usbd_msc_scsi.c | 34 ++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/ports/stm32/usbd_msc_interface.h b/ports/stm32/usbd_msc_interface.h index 411c707cab7df..9d25a72a3a4ba 100644 --- a/ports/stm32/usbd_msc_interface.h +++ b/ports/stm32/usbd_msc_interface.h @@ -26,8 +26,6 @@ #ifndef MICROPY_INCLUDED_STM32_USBD_MSC_INTERFACE_H #define MICROPY_INCLUDED_STM32_USBD_MSC_INTERFACE_H -#define USBD_MSC_MAX_LUN (2) - extern const USBD_StorageTypeDef usbd_msc_fops; void usbd_msc_init_lu(size_t lu_n, const void *lu_data); diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index d934f4676c4df..d6c38bbd07e34 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -34,6 +34,9 @@ #define MSC_MEDIA_PACKET (2048) // was 8192; how low can it go whilst still working? #define HID_DATA_FS_MAX_PACKET_SIZE (64) // endpoint IN & OUT packet size +// Maximum number of LUN that can be exposed on the MSC interface +#define USBD_MSC_MAX_LUN (2) + // Need to define here for BOT and SCSI layers #define MSC_IN_EP (0x81) #define MSC_OUT_EP (0x01) @@ -78,8 +81,8 @@ typedef struct { uint8_t scsi_sense_head; uint8_t scsi_sense_tail; - uint16_t scsi_blk_size; - uint32_t scsi_blk_nbr; + uint16_t scsi_blk_size[USBD_MSC_MAX_LUN]; + uint32_t scsi_blk_nbr[USBD_MSC_MAX_LUN]; uint32_t scsi_blk_addr_in_blks; uint32_t scsi_blk_len; diff --git a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c index d0413b758a5d6..2eb716ccde438 100644 --- a/ports/stm32/usbdev/class/src/usbd_msc_scsi.c +++ b/ports/stm32/usbdev/class/src/usbd_msc_scsi.c @@ -247,7 +247,7 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ { USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; - if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) + if(hmsc->bdev_ops->GetCapacity(lun, &hmsc->scsi_blk_nbr[lun], &hmsc->scsi_blk_size[lun]) != 0) { SCSI_SenseCode(pdev, lun, @@ -258,15 +258,17 @@ static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_ else { - hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 24); - hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 16); - hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1) >> 8); - hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1); + uint32_t blk_nbr = hmsc->scsi_blk_nbr[lun]; + hmsc->bot_data[0] = (uint8_t)((blk_nbr - 1) >> 24); + hmsc->bot_data[1] = (uint8_t)((blk_nbr - 1) >> 16); + hmsc->bot_data[2] = (uint8_t)((blk_nbr - 1) >> 8); + hmsc->bot_data[3] = (uint8_t)(blk_nbr - 1); - hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24); - hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16); - hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8); - hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size); + uint32_t blk_size = hmsc->scsi_blk_size[lun]; + hmsc->bot_data[4] = (uint8_t)(blk_size >> 24); + hmsc->bot_data[5] = (uint8_t)(blk_size >> 16); + hmsc->bot_data[6] = (uint8_t)(blk_size >> 8); + hmsc->bot_data[7] = (uint8_t)(blk_size); hmsc->bot_data_length = 8; return 0; @@ -516,7 +518,7 @@ static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *para } hmsc->bot_state = USBD_BOT_DATA_IN; - hmsc->scsi_blk_len *= hmsc->scsi_blk_size; + hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun]; /* cases 4,5 : Hi <> Dn */ if (hmsc->cbw.dDataLength != hmsc->scsi_blk_len) @@ -596,7 +598,7 @@ static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *pa return -1; /* error */ } - hmsc->scsi_blk_len *= hmsc->scsi_blk_size; + hmsc->scsi_blk_len *= hmsc->scsi_blk_size[lun]; /* cases 3,11,13 : Hn,Ho <> D0 */ if (hmsc->cbw.dDataLength != hmsc->scsi_blk_len) @@ -670,7 +672,7 @@ static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun , u { USBD_MSC_BOT_HandleTypeDef *hmsc = &((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->MSC_BOT_ClassData; - if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr ) + if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr[lun]) { SCSI_SenseCode(pdev, lun, @@ -697,7 +699,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) if( hmsc->bdev_ops->Read(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, - len / hmsc->scsi_blk_size) < 0) + len / hmsc->scsi_blk_size[lun]) < 0) { SCSI_SenseCode(pdev, @@ -714,7 +716,7 @@ static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) len); - hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size; + hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun]; hmsc->scsi_blk_len -= len; /* case 6 : Hi = Di */ @@ -744,7 +746,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) if(hmsc->bdev_ops->Write(lun , hmsc->bot_data, hmsc->scsi_blk_addr_in_blks, - len / hmsc->scsi_blk_size) < 0) + len / hmsc->scsi_blk_size[lun]) < 0) { SCSI_SenseCode(pdev, lun, @@ -754,7 +756,7 @@ static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) } - hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size; + hmsc->scsi_blk_addr_in_blks += len / hmsc->scsi_blk_size[lun]; hmsc->scsi_blk_len -= len; /* case 12 : Ho = Do */ From 441460d81ff2b1faee7d044d859896f754361b93 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 25 Jul 2020 23:05:41 +1000 Subject: [PATCH 0195/3533] extmod/uasyncio: Add StreamReader.readexactly(n) method. It raises on EOFError instead of an IncompleteReadError (which is what CPython does). But the latter is derived from EOFError so code compatible with MicroPython and CPython can be written by catching EOFError (eg see included test). Fixes issue #6156. Signed-off-by: Damien George --- extmod/uasyncio/stream.py | 12 ++++ tests/multi_net/uasyncio_tcp_readexactly.py | 68 +++++++++++++++++++ .../multi_net/uasyncio_tcp_readexactly.py.exp | 10 +++ 3 files changed, 90 insertions(+) create mode 100644 tests/multi_net/uasyncio_tcp_readexactly.py create mode 100644 tests/multi_net/uasyncio_tcp_readexactly.py.exp diff --git a/extmod/uasyncio/stream.py b/extmod/uasyncio/stream.py index 2a1efd1a17bfc..b6d787e4f0336 100644 --- a/extmod/uasyncio/stream.py +++ b/extmod/uasyncio/stream.py @@ -30,6 +30,18 @@ async def read(self, n): yield core._io_queue.queue_read(self.s) return self.s.read(n) + async def readexactly(self, n): + r = b"" + while n: + yield core._io_queue.queue_read(self.s) + r2 = self.s.read(n) + if r2 is not None: + if not len(r2): + raise EOFError + r += r2 + n -= len(r2) + return r + async def readline(self): l = b"" while True: diff --git a/tests/multi_net/uasyncio_tcp_readexactly.py b/tests/multi_net/uasyncio_tcp_readexactly.py new file mode 100644 index 0000000000000..71d8c6d0e9323 --- /dev/null +++ b/tests/multi_net/uasyncio_tcp_readexactly.py @@ -0,0 +1,68 @@ +# Test uasyncio stream readexactly() method using TCP server/client + +try: + import uasyncio as asyncio +except ImportError: + try: + import asyncio + except ImportError: + print("SKIP") + raise SystemExit + +PORT = 8000 + + +async def handle_connection(reader, writer): + writer.write(b"a") + await writer.drain() + + # Split the first 2 bytes up so the client must wait for the second one + await asyncio.sleep(0.1) + + writer.write(b"b") + await writer.drain() + + writer.write(b"c") + await writer.drain() + + writer.write(b"d") + await writer.drain() + + print("close") + writer.close() + await writer.wait_closed() + + print("done") + ev.set() + + +async def tcp_server(): + global ev + ev = asyncio.Event() + server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT) + print("server running") + multitest.next() + async with server: + await asyncio.wait_for(ev.wait(), 10) + + +async def tcp_client(): + reader, writer = await asyncio.open_connection(IP, PORT) + print(await reader.readexactly(2)) + print(await reader.readexactly(0)) + print(await reader.readexactly(1)) + try: + print(await reader.readexactly(2)) + except EOFError as er: + print("EOFError") + print(await reader.readexactly(0)) + + +def instance0(): + multitest.globals(IP=multitest.get_network_ip()) + asyncio.run(tcp_server()) + + +def instance1(): + multitest.next() + asyncio.run(tcp_client()) diff --git a/tests/multi_net/uasyncio_tcp_readexactly.py.exp b/tests/multi_net/uasyncio_tcp_readexactly.py.exp new file mode 100644 index 0000000000000..65ce6d628dd4f --- /dev/null +++ b/tests/multi_net/uasyncio_tcp_readexactly.py.exp @@ -0,0 +1,10 @@ +--- instance0 --- +server running +close +done +--- instance1 --- +b'ab' +b'' +b'c' +EOFError +b'' From 952de5cb77208f519a88752cd5de43337686f7f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Jul 2020 10:56:24 +1000 Subject: [PATCH 0196/3533] tools/makemanifest.py: Use errno.EEXIST instead of number 17. To make this code more portable, across different platforms. Signed-off-by: Damien George --- tools/makemanifest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index babce7fa73f4b..6779198c47c00 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -25,6 +25,7 @@ # THE SOFTWARE. from __future__ import print_function +import errno import sys import os import subprocess @@ -171,7 +172,7 @@ def mkdir(path): try: os.mkdir(cur_path) except OSError as er: - if er.args[0] == 17: # file exists + if er.args[0] == errno.EEXIST: pass else: raise er From 0c0cef9870e8215d67c79fefa6582849842001f9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Jul 2020 14:30:56 +1000 Subject: [PATCH 0197/3533] tests: Move .mpy import tests from import/ to micropython/ dir. These tests are specific to MicroPython so have a better home in the micropython/ test subdir, and putting them here allows them to be run by all targets, not just those that have access to the local filesystem (eg the unix port). Signed-off-by: Damien George --- .../{import/mpy_invalid.py => micropython/import_mpy_invalid.py} | 0 .../mpy_invalid.py.exp => micropython/import_mpy_invalid.py.exp} | 0 .../mpy_native.py => micropython/import_mpy_native_x64.py} | 0 .../import_mpy_native_x64.py.exp} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename tests/{import/mpy_invalid.py => micropython/import_mpy_invalid.py} (100%) rename tests/{import/mpy_invalid.py.exp => micropython/import_mpy_invalid.py.exp} (100%) rename tests/{import/mpy_native.py => micropython/import_mpy_native_x64.py} (100%) rename tests/{import/mpy_native.py.exp => micropython/import_mpy_native_x64.py.exp} (100%) diff --git a/tests/import/mpy_invalid.py b/tests/micropython/import_mpy_invalid.py similarity index 100% rename from tests/import/mpy_invalid.py rename to tests/micropython/import_mpy_invalid.py diff --git a/tests/import/mpy_invalid.py.exp b/tests/micropython/import_mpy_invalid.py.exp similarity index 100% rename from tests/import/mpy_invalid.py.exp rename to tests/micropython/import_mpy_invalid.py.exp diff --git a/tests/import/mpy_native.py b/tests/micropython/import_mpy_native_x64.py similarity index 100% rename from tests/import/mpy_native.py rename to tests/micropython/import_mpy_native_x64.py diff --git a/tests/import/mpy_native.py.exp b/tests/micropython/import_mpy_native_x64.py.exp similarity index 100% rename from tests/import/mpy_native.py.exp rename to tests/micropython/import_mpy_native_x64.py.exp From 8da40baa47ee9fe7aac228af2c0addd1f4ce3646 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Jul 2020 14:43:13 +1000 Subject: [PATCH 0198/3533] tests/micropython: Improve .mpy import tests to run on more targets. All imports are now tested to see if the test should be skipped, UserFile.read is removed, and UserFile.readinto is made more efficient. Signed-off-by: Damien George --- tests/micropython/import_mpy_invalid.py | 19 ++++++------------- tests/micropython/import_mpy_native_x64.py | 19 ++++++------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/tests/micropython/import_mpy_invalid.py b/tests/micropython/import_mpy_invalid.py index d6d01e7f1fc0c..00973fe3f9f12 100644 --- a/tests/micropython/import_mpy_invalid.py +++ b/tests/micropython/import_mpy_invalid.py @@ -1,11 +1,9 @@ # test importing of invalid .mpy files -import sys, uio - try: - uio.IOBase - import uos + import sys, uio, uos + uio.IOBase uos.mount except (ImportError, AttributeError): print("SKIP") @@ -14,18 +12,13 @@ class UserFile(uio.IOBase): def __init__(self, data): - self.data = data + self.data = memoryview(data) self.pos = 0 - def read(self): - return self.data - def readinto(self, buf): - n = 0 - while n < len(buf) and self.pos < len(self.data): - buf[n] = self.data[self.pos] - n += 1 - self.pos += 1 + n = min(len(buf), len(self.data) - self.pos) + buf[:n] = self.data[self.pos : self.pos + n] + self.pos += n return n def ioctl(self, req, arg): diff --git a/tests/micropython/import_mpy_native_x64.py b/tests/micropython/import_mpy_native_x64.py index 5d7bdab4a2aaa..d0de507d44234 100644 --- a/tests/micropython/import_mpy_native_x64.py +++ b/tests/micropython/import_mpy_native_x64.py @@ -1,11 +1,9 @@ # test importing of .mpy files with native code (x64 only) -import sys, uio - try: - uio.IOBase - import uos + import sys, uio, uos + uio.IOBase uos.mount except (ImportError, AttributeError): print("SKIP") @@ -18,18 +16,13 @@ class UserFile(uio.IOBase): def __init__(self, data): - self.data = data + self.data = memoryview(data) self.pos = 0 - def read(self): - return self.data - def readinto(self, buf): - n = 0 - while n < len(buf) and self.pos < len(self.data): - buf[n] = self.data[self.pos] - n += 1 - self.pos += 1 + n = min(len(buf), len(self.data) - self.pos) + buf[:n] = self.data[self.pos : self.pos + n] + self.pos += n return n def ioctl(self, req, arg): From 9883d8e818feba112935676eb5aa4ce211d7779c Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 27 Jul 2020 23:52:38 +1000 Subject: [PATCH 0199/3533] py/persistentcode: Maintain root ptr list of imported native .mpy code. On ports where normal heap memory can contain executable code (eg ARM-based ports such as stm32), native code loaded from an .mpy file may be reclaimed by the GC because there's no reference to the very start of the native machine code block that is reachable from root pointers (only pointers to internal parts of the machine code block are reachable, but that doesn't help the GC find the memory). This commit fixes this issue by maintaining an explicit list of root pointers pointing to native code that is loaded from an .mpy file. This is not needed for all ports so is selectable by the new configuration option MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE. It's enabled by default if a port does not specify any special functions to allocate or commit executable memory. A test is included to test that native code loaded from an .mpy file does not get reclaimed by the GC. Fixes #6045. Signed-off-by: Damien George --- py/mpconfig.h | 12 +++ py/mpstate.h | 5 + py/persistentcode.c | 14 ++- py/runtime.c | 4 + tests/micropython/import_mpy_native_gc.py | 91 +++++++++++++++++++ tests/micropython/import_mpy_native_gc.py.exp | 1 + 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 tests/micropython/import_mpy_native_gc.py create mode 100644 tests/micropython/import_mpy_native_gc.py.exp diff --git a/py/mpconfig.h b/py/mpconfig.h index 287b15aaef32f..3d9ce8bb55c95 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -351,6 +351,18 @@ // Convenience definition for whether any native or inline assembler emitter is enabled #define MICROPY_EMIT_MACHINE_CODE (MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM) +// Whether native relocatable code loaded from .mpy files is explicitly tracked +// so that the GC cannot reclaim it. Needed on architectures that allocate +// executable memory on the MicroPython heap and don't explicitly track this +// data some other way. +#ifndef MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE +#if !MICROPY_EMIT_MACHINE_CODE || defined(MP_PLAT_ALLOC_EXEC) || defined(MP_PLAT_COMMIT_EXEC) +#define MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE (0) +#else +#define MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE (1) +#endif +#endif + /*****************************************************************************/ /* Compiler configuration */ diff --git a/py/mpstate.h b/py/mpstate.h index 5f6cf55936a32..2519c77e2d647 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -167,6 +167,11 @@ typedef struct _mp_state_vm_t { mp_obj_dict_t *mp_module_builtins_override_dict; #endif + #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE + // An mp_obj_list_t that tracks relocated native code to prevent the GC from reclaiming them. + mp_obj_t track_reloc_code_list; + #endif + // include any root pointers defined by a port MICROPY_PORT_ROOT_POINTERS diff --git a/py/persistentcode.c b/py/persistentcode.c index 386ea49477f6b..da3234a5fe9cd 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2016 Damien P. George + * Copyright (c) 2013-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -516,6 +516,18 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { fun_data = MP_PLAT_COMMIT_EXEC(fun_data, fun_data_len, opt_ri); #else if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERRELOC) { + #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE + // If native code needs relocations then it's not guaranteed that a pointer to + // the head of `buf` (containing the machine code) will be retained for the GC + // to trace. This is because native functions can start inside `buf` and so + // it's possible that the only GC-reachable pointers are pointers inside `buf`. + // So put this `buf` on a list of reachable root pointers. + if (MP_STATE_PORT(track_reloc_code_list) == MP_OBJ_NULL) { + MP_STATE_PORT(track_reloc_code_list) = mp_obj_new_list(0, NULL); + } + mp_obj_list_append(MP_STATE_PORT(track_reloc_code_list), MP_OBJ_FROM_PTR(fun_data)); + #endif + // Do the relocations. mp_native_relocate(&ri, fun_data, (uintptr_t)fun_data); } #endif diff --git a/py/runtime.c b/py/runtime.c index 78da386919532..38da2f4538e97 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -106,6 +106,10 @@ void mp_init(void) { MP_STATE_VM(mp_module_builtins_override_dict) = NULL; #endif + #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE + MP_STATE_VM(track_reloc_code_list) = MP_OBJ_NULL; + #endif + #if MICROPY_PY_OS_DUPTERM for (size_t i = 0; i < MICROPY_PY_OS_DUPTERM; ++i) { MP_STATE_VM(dupterm_objs[i]) = MP_OBJ_NULL; diff --git a/tests/micropython/import_mpy_native_gc.py b/tests/micropython/import_mpy_native_gc.py new file mode 100644 index 0000000000000..e8fac8f179371 --- /dev/null +++ b/tests/micropython/import_mpy_native_gc.py @@ -0,0 +1,91 @@ +# Test that native code loaded from a .mpy file is retained after a GC. + +try: + import gc, sys, uio, uos + + sys.implementation.mpy + uio.IOBase + uos.mount +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class UserFile(uio.IOBase): + def __init__(self, data): + self.data = memoryview(data) + self.pos = 0 + + def readinto(self, buf): + n = min(len(buf), len(self.data) - self.pos) + buf[:n] = self.data[self.pos : self.pos + n] + self.pos += n + return n + + def ioctl(self, req, arg): + return 0 + + +class UserFS: + def __init__(self, files): + self.files = files + + def mount(self, readonly, mksfs): + pass + + def umount(self): + pass + + def stat(self, path): + if path in self.files: + return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) + raise OSError + + def open(self, path, mode): + return UserFile(self.files[path]) + + +# Pre-compiled examples/natmod/features0 example for various architectures, keyed +# by the required value of sys.implementation.mpy. +features0_file_contents = { + # -march=x64 -mcache-lookup-bc + 0xB05: b'M\x05\x0b\x1f \x84b\xe9/\x00\x00\x00SH\x8b\x1ds\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dA\x00\x00\x00H\x8b\x7f\x08L\x8bc(A\xff\xd4H\x8d5\x1f\x00\x00\x00H\x89\xc5H\x8b\x05-\x00\x00\x00\x0f\xb78\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x84@\x12factorial\x10\x00\x00\r \x01"\x9f\x1c\x01\x1e\xff', + # -march=armv7m + 0x1605: b"M\x05\x16\x1f \x84\x12\x1a\xe0\x00\x00\x13\xb5\nK\nJ{D\x9cX\x02!\xe3h\x98G\x03F\x01 3\xb9\x02!#i\x01\x93\x02\xb0\xbd\xe8\x10@\x18GXC\x01;\xf4\xe7\x00\xbfj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\tN\tK~D\xf4X@hgi\xb8G\x05F\x07K\x07I\xf2XyD\x10\x88ck\x98G(F\xb8G h\xf8\xbd6\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1c\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x01\x84\x00\x12factorial\x10\x00\x00\r<\x01>\x9f8\x01:\xff", +} + +# Populate other armv7m-derived archs based on armv7m. +for arch in (0x1A05, 0x1E05, 0x2205): + features0_file_contents[arch] = features0_file_contents[0x1605] + +if sys.implementation.mpy not in features0_file_contents: + print("SKIP") + raise SystemExit + +# These are the test .mpy files. +user_files = {"/features0.mpy": features0_file_contents[sys.implementation.mpy]} + +# Create and mount a user filesystem. +uos.mount(UserFS(user_files), "/userfs") +sys.path.append("/userfs") + +# Import the native function. +gc.collect() +from features0 import factorial + +# Free the module that contained the function. +del sys.modules["features0"] + +# Run a GC cycle which should reclaim the module but not the function. +gc.collect() + +# Allocate lots of fragmented memory to overwrite anything that was just freed by the GC. +for i in range(1000): + [] + +# Run the native function, it should not have been freed or overwritten. +print(factorial(10)) + +# Unmount and undo path addition. +uos.umount("/userfs") +sys.path.pop() diff --git a/tests/micropython/import_mpy_native_gc.py.exp b/tests/micropython/import_mpy_native_gc.py.exp new file mode 100644 index 0000000000000..3fbd4a8698fa3 --- /dev/null +++ b/tests/micropython/import_mpy_native_gc.py.exp @@ -0,0 +1 @@ +3628800 From b731bd0ce6f05796dd642804bf2469646d7a063c Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 Aug 2020 14:46:05 +1000 Subject: [PATCH 0200/3533] tools/makemanifest.py: Print nicely formatted errors from mpy-cross. If mpy-cross exits with an error be sure to print that error in a way that is readable, instead of a long bytes object. Signed-off-by: Damien George --- tools/makemanifest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 6779198c47c00..b7d4a4d4a68af 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -288,7 +288,8 @@ def main(): + ["-o", outfile, "-s", script, "-O{}".format(opt), infile] ) if res != 0: - print("error compiling {}: {}".format(infile, out)) + print("error compiling {}:".format(infile)) + sys.stdout.buffer.write(out) raise SystemExit(1) ts_outfile = get_timestamp(outfile) mpy_files.append(outfile) From 60f5b941e096aba270a7d4af8faa33169de8364f Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 Aug 2020 15:39:56 +1000 Subject: [PATCH 0201/3533] extmod/vfs_reader: Fix mp_reader_new_file to open file in "rb" mode. mp_reader_new_file() is used to read in files for importing, either .py or .mpy files, for the lexer and persistent code loader respectively. In both cases the file should be opened in raw bytes mode: the lexer handles unicode characters itself, and .mpy files contain 8-bit bytes by nature. Before this commit importing was working correctly because, although the file was opened in text mode, all native filesystem implementations (POSIX, FAT, LFS) would access the file in raw bytes mode via mp_stream_rw() calling mp_stream_p_t.read(). So it was only an issue for non-native filesystems, such as those implemented in Python. For Python-based filesystem implementations, a call to mp_stream_rw() would go via IOBase and then to readinto() at the Python level, and readinto() is only defined on files opened in raw bytes mode. Signed-off-by: Damien George --- extmod/vfs_reader.c | 7 +++++-- tests/extmod/vfs_userfs.py | 14 ++++++++++---- tests/extmod/vfs_userfs.py.exp | 6 +++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/extmod/vfs_reader.c b/extmod/vfs_reader.c index db13ce3c33141..d3904c5c50693 100644 --- a/extmod/vfs_reader.c +++ b/extmod/vfs_reader.c @@ -71,8 +71,11 @@ STATIC void mp_reader_vfs_close(void *data) { void mp_reader_new_file(mp_reader_t *reader, const char *filename) { mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t); - mp_obj_t arg = mp_obj_new_str(filename, strlen(filename)); - rf->file = mp_vfs_open(1, &arg, (mp_map_t *)&mp_const_empty_map); + mp_obj_t args[2] = { + mp_obj_new_str(filename, strlen(filename)), + MP_OBJ_NEW_QSTR(MP_QSTR_rb), + }; + rf->file = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map); int errcode; rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE); if (errcode != 0) { diff --git a/tests/extmod/vfs_userfs.py b/tests/extmod/vfs_userfs.py index 06e546b081a97..3cdfe82eea8dc 100644 --- a/tests/extmod/vfs_userfs.py +++ b/tests/extmod/vfs_userfs.py @@ -16,14 +16,20 @@ class UserFile(uio.IOBase): - def __init__(self, data): + def __init__(self, mode, data): + assert isinstance(data, bytes) + self.is_text = mode.find("b") == -1 self.data = data self.pos = 0 def read(self): - return self.data + if self.is_text: + return str(self.data, "utf8") + else: + return self.data def readinto(self, buf): + assert not self.is_text n = 0 while n < len(buf) and self.pos < len(self.data): buf[n] = self.data[self.pos] @@ -54,12 +60,12 @@ def stat(self, path): def open(self, path, mode): print("open", path, mode) - return UserFile(self.files[path]) + return UserFile(mode, self.files[path]) # create and mount a user filesystem user_files = { - "/data.txt": b"some data in a text file\n", + "/data.txt": b"some data in a text file", "/usermod1.py": b"print('in usermod1')\nimport usermod2", "/usermod2.py": b"print('in usermod2')", } diff --git a/tests/extmod/vfs_userfs.py.exp b/tests/extmod/vfs_userfs.py.exp index 6a4d925b91e8b..00ddd95fca508 100644 --- a/tests/extmod/vfs_userfs.py.exp +++ b/tests/extmod/vfs_userfs.py.exp @@ -1,12 +1,12 @@ open /data.txt r -b'some data in a text file\n' +some data in a text file stat /usermod1 stat /usermod1.py -open /usermod1.py r +open /usermod1.py rb ioctl 4 0 in usermod1 stat /usermod2 stat /usermod2.py -open /usermod2.py r +open /usermod2.py rb ioctl 4 0 in usermod2 From ac94e06f0ba818157490587af1e4d0fb4cf3e962 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Fri, 7 Aug 2020 14:39:13 -0500 Subject: [PATCH 0202/3533] zephyr: Include storage/flash_map.h unconditionally. Include storage/flash_map.h unconditionally so we always have access to the FLASH_AREA_LABEL_EXISTS macro, even if CONFIG_FLASH_MAP is not defined. This fixes a build error for the qemu_x86 board: main.c:108:63: error: missing binary operator before token "(" 108 | #elif defined(CONFIG_FLASH_MAP) && FLASH_AREA_LABEL_EXISTS(storage) | ^ ../../py/mkrules.mk:88: recipe for target 'build/genhdr/qstr.i.last' failed Signed-off-by: Maureen Helm --- ports/zephyr/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 78b947d7af9b9..74785cc830420 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -38,9 +38,7 @@ #include #endif -#ifdef CONFIG_FLASH_MAP #include -#endif #include "py/mperrno.h" #include "py/compile.h" From 17689a71f66bcf96c09f8700fa3e0f5aa5be225c Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Mon, 27 Jul 2020 12:58:02 -0500 Subject: [PATCH 0203/3533] travis: Add zephyr build to CI. Adds a job to build the zephyr port in CI using the same docker container that the zephyr project uses for its own CI. Always make clean zephyr builds to ensure we don't just rebuild C code, but we also rebuild Kconfig and dts. This is required when switching between boards, which have different Kconfigs and device trees. Uses the tagged zephyr 2.3.0 release. Signed-off-by: Maureen Helm --- .travis.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.travis.yml b/.travis.yml index 48ce39f60ad89..ebf9b55899dc9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,33 @@ jobs: - tools/codeformat.py - git diff --exit-code + # zephyr port + - stage: test + name: "zephyr port build" + services: + - docker + before_install: + - docker pull zephyrprojectrtos/ci:v0.11.8 + - > + docker run --name zephyr-ci -d -it + -v "$(pwd)":/micropython + -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.11.3 + -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr + -w /micropython/ports/zephyr + zephyrprojectrtos/ci:v0.11.8 + - docker ps -a + install: + - docker exec zephyr-ci west init --mr v2.3.0 /zephyrproject + - docker exec -w /zephyrproject zephyr-ci west update + - docker exec -w /zephyrproject zephyr-ci west zephyr-export + script: + - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS}" + - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS} BOARD=frdm_k64f" + - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS}" + - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=frdm_k64f" + - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=mimxrt1050_evk" + - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=reel_board" + # unix port on OSX (first in list because the build VM takes a long time to start) - stage: test os: osx From e76c7466b62c940150cf1b5000e18f60a5efa2e3 Mon Sep 17 00:00:00 2001 From: Zenix27 <30363000+Zenix27@users.noreply.github.com> Date: Sat, 11 Jul 2020 12:23:26 +0530 Subject: [PATCH 0204/3533] docs: Change `\*` to `*` in argument lists. Latest versions of Sphinx (at least 3.1.0) do not need the `*` escaped and will render the `\` in the output if it is there, so remove it. Fixes issue #6209. --- docs/library/btree.rst | 2 +- docs/library/esp32.rst | 2 +- docs/library/lcd160cr.rst | 2 +- docs/library/machine.ADCWiPy.rst | 4 ++-- docs/library/machine.I2C.rst | 10 +++++----- docs/library/machine.Pin.rst | 6 +++--- docs/library/machine.RTC.rst | 4 ++-- docs/library/machine.SPI.rst | 2 +- docs/library/machine.Signal.rst | 2 +- docs/library/machine.Timer.rst | 2 +- docs/library/machine.TimerWiPy.rst | 6 +++--- docs/library/machine.UART.rst | 2 +- docs/library/network.CC3K.rst | 2 +- docs/library/network.WLAN.rst | 2 +- docs/library/network.WLANWiPy.rst | 6 +++--- docs/library/network.rst | 4 ++-- docs/library/pyb.CAN.rst | 8 ++++---- docs/library/pyb.DAC.rst | 6 +++--- docs/library/pyb.Flash.rst | 2 +- docs/library/pyb.I2C.rst | 10 +++++----- docs/library/pyb.SPI.rst | 8 ++++---- docs/library/pyb.Timer.rst | 2 +- docs/library/pyb.UART.rst | 2 +- docs/library/pyb.USB_HID.rst | 2 +- docs/library/pyb.USB_VCP.rst | 6 +++--- docs/library/pyb.rst | 2 +- docs/library/uasyncio.rst | 2 +- docs/library/uos.rst | 2 +- 28 files changed, 55 insertions(+), 55 deletions(-) diff --git a/docs/library/btree.rst b/docs/library/btree.rst index a1f7a5420f15e..de52ef7e7e44e 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -76,7 +76,7 @@ Example:: Functions --------- -.. function:: open(stream, \*, flags=0, pagesize=0, cachesize=0, minkeypage=0) +.. function:: open(stream, *, flags=0, pagesize=0, cachesize=0, minkeypage=0) Open a database from a random-access `stream` (like an open file). All other parameters are optional and keyword-only, and allow to tweak advanced diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index dfde840a2141a..c6777b8a7da33 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -186,7 +186,7 @@ For more details see Espressif's `ESP-IDF RMT documentation. *beta feature* and the interface may change in the future. -.. class:: RMT(channel, \*, pin=None, clock_div=8, carrier_freq=0, carrier_duty_percent=50) +.. class:: RMT(channel, *, pin=None, clock_div=8, carrier_freq=0, carrier_duty_percent=50) This class provides access to one of the eight RMT channels. *channel* is required and identifies which RMT channel (0-7) will be configured. *pin*, diff --git a/docs/library/lcd160cr.rst b/docs/library/lcd160cr.rst index e31ed94651ca5..85e4b8f07a1f6 100644 --- a/docs/library/lcd160cr.rst +++ b/docs/library/lcd160cr.rst @@ -37,7 +37,7 @@ For example:: Constructors ------------ -.. class:: LCD160CR(connect=None, \*, pwr=None, i2c=None, spi=None, i2c_addr=98) +.. class:: LCD160CR(connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98) Construct an LCD160CR object. The parameters are: diff --git a/docs/library/machine.ADCWiPy.rst b/docs/library/machine.ADCWiPy.rst index 4a4f0524c8f0b..e500d00890f3a 100644 --- a/docs/library/machine.ADCWiPy.rst +++ b/docs/library/machine.ADCWiPy.rst @@ -22,7 +22,7 @@ Usage:: Constructors ------------ -.. class:: ADCWiPy(id=0, \*, bits=12) +.. class:: ADCWiPy(id=0, *, bits=12) Create an ADC object associated with the given pin. This allows you to then read analog values on that pin. @@ -39,7 +39,7 @@ Constructors Methods ------- -.. method:: ADCWiPy.channel(id, \*, pin) +.. method:: ADCWiPy.channel(id, *, pin) Create an analog pin. If only channel ID is given, the correct pin will be selected. Alternatively, only the pin can be passed and the correct diff --git a/docs/library/machine.I2C.rst b/docs/library/machine.I2C.rst index 2cbfec1ba83bf..743554bc76650 100644 --- a/docs/library/machine.I2C.rst +++ b/docs/library/machine.I2C.rst @@ -33,7 +33,7 @@ Example usage:: Constructors ------------ -.. class:: I2C(id=-1, \*, scl, sda, freq=400000) +.. class:: I2C(id=-1, *, scl, sda, freq=400000) Construct and return a new I2C object using the following parameters: @@ -52,7 +52,7 @@ Constructors General Methods --------------- -.. method:: I2C.init(scl, sda, \*, freq=400000) +.. method:: I2C.init(scl, sda, *, freq=400000) Initialise the I2C bus with the given arguments: @@ -153,14 +153,14 @@ from and written to. In this case there are two addresses associated with an I2C transaction: the slave address and the memory address. The following methods are convenience functions to communicate with such devices. -.. method:: I2C.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8) +.. method:: I2C.readfrom_mem(addr, memaddr, nbytes, *, addrsize=8) Read *nbytes* from the slave specified by *addr* starting from the memory address specified by *memaddr*. The argument *addrsize* specifies the address size in bits. Returns a `bytes` object with the data read. -.. method:: I2C.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8) +.. method:: I2C.readfrom_mem_into(addr, memaddr, buf, *, addrsize=8) Read into *buf* from the slave specified by *addr* starting from the memory address specified by *memaddr*. The number of bytes read is the @@ -170,7 +170,7 @@ methods are convenience functions to communicate with such devices. The method returns ``None``. -.. method:: I2C.writeto_mem(addr, memaddr, buf, \*, addrsize=8) +.. method:: I2C.writeto_mem(addr, memaddr, buf, *, addrsize=8) Write *buf* to the slave specified by *addr* starting from the memory address specified by *memaddr*. diff --git a/docs/library/machine.Pin.rst b/docs/library/machine.Pin.rst index 3055491ebbba8..095240fe1499a 100644 --- a/docs/library/machine.Pin.rst +++ b/docs/library/machine.Pin.rst @@ -42,7 +42,7 @@ Usage Model:: Constructors ------------ -.. class:: Pin(id, mode=-1, pull=-1, \*, value, drive, alt) +.. class:: Pin(id, mode=-1, pull=-1, *, value, drive, alt) Access the pin peripheral (GPIO pin) associated with the given ``id``. If additional arguments are given in the constructor then they are used to initialise @@ -106,7 +106,7 @@ Constructors Methods ------- -.. method:: Pin.init(mode=-1, pull=-1, \*, value, drive, alt) +.. method:: Pin.init(mode=-1, pull=-1, *, value, drive, alt) Re-initialise the pin using the given parameters. Only those arguments that are specified will be set. The rest of the pin peripheral state will remain @@ -179,7 +179,7 @@ Methods Availability: WiPy. -.. method:: Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), \*, priority=1, wake=None, hard=False) +.. method:: Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), *, priority=1, wake=None, hard=False) Configure an interrupt handler to be called when the trigger source of the pin is active. If the pin mode is ``Pin.IN`` then the trigger source is diff --git a/docs/library/machine.RTC.rst b/docs/library/machine.RTC.rst index 548ad007e0dc3..d5a4f390b512b 100644 --- a/docs/library/machine.RTC.rst +++ b/docs/library/machine.RTC.rst @@ -38,7 +38,7 @@ Methods Resets the RTC to the time of January 1, 2015 and starts running it again. -.. method:: RTC.alarm(id, time, \*, repeat=False) +.. method:: RTC.alarm(id, time, *, repeat=False) Set the RTC alarm. Time might be either a millisecond value to program the alarm to current time + time_in_ms in the future, or a datetimetuple. If the time passed is in @@ -52,7 +52,7 @@ Methods Cancel a running alarm. -.. method:: RTC.irq(\*, trigger, handler=None, wake=machine.IDLE) +.. method:: RTC.irq(*, trigger, handler=None, wake=machine.IDLE) Create an irq object triggered by a real time clock alarm. diff --git a/docs/library/machine.SPI.rst b/docs/library/machine.SPI.rst index a9fcc719c8956..ea460a7b81282 100644 --- a/docs/library/machine.SPI.rst +++ b/docs/library/machine.SPI.rst @@ -29,7 +29,7 @@ Constructors Methods ------- -.. method:: SPI.init(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO)) +.. method:: SPI.init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO)) Initialise the SPI bus with the given parameters: diff --git a/docs/library/machine.Signal.rst b/docs/library/machine.Signal.rst index a1a29164b2cfd..651c8c8497fd8 100644 --- a/docs/library/machine.Signal.rst +++ b/docs/library/machine.Signal.rst @@ -75,7 +75,7 @@ Constructors ------------ .. class:: Signal(pin_obj, invert=False) - Signal(pin_arguments..., \*, invert=False) + Signal(pin_arguments..., *, invert=False) Create a Signal object. There're two ways to create it: diff --git a/docs/library/machine.Timer.rst b/docs/library/machine.Timer.rst index b16ad52d597f5..2d1287f325780 100644 --- a/docs/library/machine.Timer.rst +++ b/docs/library/machine.Timer.rst @@ -35,7 +35,7 @@ Constructors Methods ------- -.. method:: Timer.init(\*, mode=Timer.PERIODIC, period=-1, callback=None) +.. method:: Timer.init(*, mode=Timer.PERIODIC, period=-1, callback=None) Initialise the timer. Example:: diff --git a/docs/library/machine.TimerWiPy.rst b/docs/library/machine.TimerWiPy.rst index 904a958c69b16..f5b748c62e1d5 100644 --- a/docs/library/machine.TimerWiPy.rst +++ b/docs/library/machine.TimerWiPy.rst @@ -39,7 +39,7 @@ Constructors Methods ------- -.. method:: TimerWiPy.init(mode, \*, width=16) +.. method:: TimerWiPy.init(mode, *, width=16) Initialise the timer. Example:: @@ -64,7 +64,7 @@ Methods Deinitialises the timer. Stops the timer, and disables the timer peripheral. -.. method:: TimerWiPy.channel(channel, \**, freq, period, polarity=TimerWiPy.POSITIVE, duty_cycle=0) +.. method:: TimerWiPy.channel(channel, **, freq, period, polarity=TimerWiPy.POSITIVE, duty_cycle=0) If only a channel identifier passed, then a previously initialized channel object is returned (or ``None`` if there is no previous channel). @@ -113,7 +113,7 @@ TimerChannel objects are created using the Timer.channel() method. Methods ------- -.. method:: timerchannel.irq(\*, trigger, priority=1, handler=None) +.. method:: timerchannel.irq(*, trigger, priority=1, handler=None) The behavior of this callback is heavily dependent on the operating mode of the timer channel: diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index 70984dfb2fff1..957d58ca18256 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -43,7 +43,7 @@ Constructors Methods ------- -.. method:: UART.init(baudrate=9600, bits=8, parity=None, stop=1, \*, ...) +.. method:: UART.init(baudrate=9600, bits=8, parity=None, stop=1, *, ...) Initialise the UART bus with the given parameters: diff --git a/docs/library/network.CC3K.rst b/docs/library/network.CC3K.rst index 212a1e3bd771f..18210e2d26d59 100644 --- a/docs/library/network.CC3K.rst +++ b/docs/library/network.CC3K.rst @@ -51,7 +51,7 @@ Constructors Methods ------- -.. method:: CC3K.connect(ssid, key=None, \*, security=WPA2, bssid=None) +.. method:: CC3K.connect(ssid, key=None, *, security=WPA2, bssid=None) Connect to a WiFi access point using the given SSID, and other security parameters. diff --git a/docs/library/network.WLAN.rst b/docs/library/network.WLAN.rst index 885a4460c56d4..fcdaa41b36345 100644 --- a/docs/library/network.WLAN.rst +++ b/docs/library/network.WLAN.rst @@ -32,7 +32,7 @@ Methods argument is passed. Otherwise, query current state if no argument is provided. Most other methods require active interface. -.. method:: WLAN.connect(ssid=None, password=None, \*, bssid=None) +.. method:: WLAN.connect(ssid=None, password=None, *, bssid=None) Connect to the specified wireless network, using the specified password. If *bssid* is given then the connection will be restricted to the diff --git a/docs/library/network.WLANWiPy.rst b/docs/library/network.WLANWiPy.rst index b6e3279597ac9..2a5ba118454bc 100644 --- a/docs/library/network.WLANWiPy.rst +++ b/docs/library/network.WLANWiPy.rst @@ -43,7 +43,7 @@ Constructors Methods ------- -.. method:: WLANWiPy.init(mode, \*, ssid, auth, channel, antenna) +.. method:: WLANWiPy.init(mode, *, ssid, auth, channel, antenna) Set or get the WiFi network processor configuration. @@ -69,7 +69,7 @@ Methods # configure as an station wlan.init(mode=WLAN.STA) -.. method:: WLANWiPy.connect(ssid, \*, auth=None, bssid=None, timeout=None) +.. method:: WLANWiPy.connect(ssid, *, auth=None, bssid=None, timeout=None) Connect to a WiFi access point using the given SSID, and other security parameters. @@ -131,7 +131,7 @@ Methods Get or set a 6-byte long bytes object with the MAC address. -.. method:: WLANWiPy.irq(\*, handler, wake) +.. method:: WLANWiPy.irq(*, handler, wake) Create a callback to be triggered when a WLAN event occurs during ``machine.SLEEP`` mode. Events are triggered by socket activity or by WLAN connection/disconnection. diff --git a/docs/library/network.rst b/docs/library/network.rst index 208c58f852e6b..bd3bc6f34ba81 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -58,7 +58,7 @@ parameter should be `id`. interface (behavior of calling them on inactive interface is undefined). -.. method:: AbstractNIC.connect([service_id, key=None, \*, ...]) +.. method:: AbstractNIC.connect([service_id, key=None, *, ...]) Connect the interface to a network. This method is optional, and available only for interfaces which are not "always connected". @@ -82,7 +82,7 @@ parameter should be `id`. Returns ``True`` if connected to network, otherwise returns ``False``. -.. method:: AbstractNIC.scan(\*, ...) +.. method:: AbstractNIC.scan(*, ...) Scan for the available network services/connections. Returns a list of tuples with discovered service parameters. For various diff --git a/docs/library/pyb.CAN.rst b/docs/library/pyb.CAN.rst index ba935abfd5c4e..8078e29e0cc8d 100644 --- a/docs/library/pyb.CAN.rst +++ b/docs/library/pyb.CAN.rst @@ -49,7 +49,7 @@ Class Methods Methods ------- -.. method:: CAN.init(mode, extframe=False, prescaler=100, \*, sjw=1, bs1=6, bs2=8, auto_restart=False) +.. method:: CAN.init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8, auto_restart=False) Initialise the CAN bus with the given parameters: @@ -135,7 +135,7 @@ Methods - number of pending RX messages on fifo 0 - number of pending RX messages on fifo 1 -.. method:: CAN.setfilter(bank, mode, fifo, params, \*, rtr) +.. method:: CAN.setfilter(bank, mode, fifo, params, *, rtr) Configure a filter bank: @@ -187,7 +187,7 @@ Methods Return ``True`` if any message waiting on the FIFO, else ``False``. -.. method:: CAN.recv(fifo, list=None, \*, timeout=5000) +.. method:: CAN.recv(fifo, list=None, *, timeout=5000) Receive data on the bus: @@ -220,7 +220,7 @@ Methods # No heap memory is allocated in the following call can.recv(0, lst) -.. method:: CAN.send(data, id, \*, timeout=0, rtr=False) +.. method:: CAN.send(data, id, *, timeout=0, rtr=False) Send a message on the bus: diff --git a/docs/library/pyb.DAC.rst b/docs/library/pyb.DAC.rst index 9a465b9ce25d8..bf07119ada166 100644 --- a/docs/library/pyb.DAC.rst +++ b/docs/library/pyb.DAC.rst @@ -49,7 +49,7 @@ To output a continuous sine-wave at 12-bit resolution:: Constructors ------------ -.. class:: pyb.DAC(port, bits=8, \*, buffering=None) +.. class:: pyb.DAC(port, bits=8, *, buffering=None) Construct a new DAC object. @@ -76,7 +76,7 @@ Constructors Methods ------- -.. method:: DAC.init(bits=8, \*, buffering=None) +.. method:: DAC.init(bits=8, *, buffering=None) Reinitialise the DAC. *bits* can be 8 or 12. *buffering* can be ``None``, ``False`` or ``True``; see above constructor for the meaning @@ -103,7 +103,7 @@ Methods value is 2\*\*``bits``-1, where ``bits`` is set when creating the DAC object or by using the ``init`` method. -.. method:: DAC.write_timed(data, freq, \*, mode=DAC.NORMAL) +.. method:: DAC.write_timed(data, freq, *, mode=DAC.NORMAL) Initiates a burst of RAM to DAC using a DMA transfer. The input data is treated as an array of bytes in 8-bit mode, and diff --git a/docs/library/pyb.Flash.rst b/docs/library/pyb.Flash.rst index 13f2097871913..4b0c2ce2aaa2f 100644 --- a/docs/library/pyb.Flash.rst +++ b/docs/library/pyb.Flash.rst @@ -25,7 +25,7 @@ Constructors This constructor is deprecated and will be removed in a future version of MicroPython. -.. class:: pyb.Flash(\*, start=-1, len=-1) +.. class:: pyb.Flash(*, start=-1, len=-1) :noindex: Create and return a block device that accesses the flash at the specified offset. The length defaults to the remaining size of the device. diff --git a/docs/library/pyb.I2C.rst b/docs/library/pyb.I2C.rst index d549c1a81207d..641dcb8815873 100644 --- a/docs/library/pyb.I2C.rst +++ b/docs/library/pyb.I2C.rst @@ -84,7 +84,7 @@ Methods Turn off the I2C bus. -.. method:: I2C.init(mode, \*, addr=0x12, baudrate=400000, gencall=False, dma=False) +.. method:: I2C.init(mode, *, addr=0x12, baudrate=400000, gencall=False, dma=False) Initialise the I2C bus with the given parameters: @@ -100,7 +100,7 @@ Methods Check if an I2C device responds to the given address. Only valid when in master mode. -.. method:: I2C.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8) +.. method:: I2C.mem_read(data, addr, memaddr, *, timeout=5000, addr_size=8) Read from the memory of an I2C device: @@ -113,7 +113,7 @@ Methods Returns the read data. This is only valid in master mode. -.. method:: I2C.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size=8) +.. method:: I2C.mem_write(data, addr, memaddr, *, timeout=5000, addr_size=8) Write to the memory of an I2C device: @@ -126,7 +126,7 @@ Methods Returns ``None``. This is only valid in master mode. -.. method:: I2C.recv(recv, addr=0x00, \*, timeout=5000) +.. method:: I2C.recv(recv, addr=0x00, *, timeout=5000) Receive data on the bus: @@ -138,7 +138,7 @@ Methods Return value: if ``recv`` is an integer then a new buffer of the bytes received, otherwise the same buffer that was passed in to ``recv``. -.. method:: I2C.send(send, addr=0x00, \*, timeout=5000) +.. method:: I2C.send(send, addr=0x00, *, timeout=5000) Send data on the bus: diff --git a/docs/library/pyb.SPI.rst b/docs/library/pyb.SPI.rst index 181bdd3b6ec5d..24e2ec5a73d2c 100644 --- a/docs/library/pyb.SPI.rst +++ b/docs/library/pyb.SPI.rst @@ -51,7 +51,7 @@ Methods Turn off the SPI bus. -.. method:: SPI.init(mode, baudrate=328125, \*, prescaler, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None) +.. method:: SPI.init(mode, baudrate=328125, *, prescaler, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None) Initialise the SPI bus with the given parameters: @@ -77,7 +77,7 @@ Methods Printing the SPI object will show you the computed baudrate and the chosen prescaler. -.. method:: SPI.recv(recv, \*, timeout=5000) +.. method:: SPI.recv(recv, *, timeout=5000) Receive data on the bus: @@ -88,7 +88,7 @@ Methods Return value: if ``recv`` is an integer then a new buffer of the bytes received, otherwise the same buffer that was passed in to ``recv``. -.. method:: SPI.send(send, \*, timeout=5000) +.. method:: SPI.send(send, *, timeout=5000) Send data on the bus: @@ -97,7 +97,7 @@ Methods Return value: ``None``. -.. method:: SPI.send_recv(send, recv=None, \*, timeout=5000) +.. method:: SPI.send_recv(send, recv=None, *, timeout=5000) Send and receive data on the bus at the same time: diff --git a/docs/library/pyb.Timer.rst b/docs/library/pyb.Timer.rst index 532b64da8e93c..34fe71155fc15 100644 --- a/docs/library/pyb.Timer.rst +++ b/docs/library/pyb.Timer.rst @@ -62,7 +62,7 @@ Constructors Methods ------- -.. method:: Timer.init(\*, freq, prescaler, period, mode=Timer.UP, div=1, callback=None, deadtime=0) +.. method:: Timer.init(*, freq, prescaler, period, mode=Timer.UP, div=1, callback=None, deadtime=0) Initialise the timer. Initialisation must be either by frequency (in Hz) or by prescaler and period:: diff --git a/docs/library/pyb.UART.rst b/docs/library/pyb.UART.rst index 0a611a72566da..a1d6e59002551 100644 --- a/docs/library/pyb.UART.rst +++ b/docs/library/pyb.UART.rst @@ -84,7 +84,7 @@ Constructors Methods ------- -.. method:: UART.init(baudrate, bits=8, parity=None, stop=1, \*, timeout=0, flow=0, timeout_char=0, read_buf_len=64) +.. method:: UART.init(baudrate, bits=8, parity=None, stop=1, *, timeout=0, flow=0, timeout_char=0, read_buf_len=64) Initialise the UART bus with the given parameters: diff --git a/docs/library/pyb.USB_HID.rst b/docs/library/pyb.USB_HID.rst index 649dc3df4a605..7e23d1313d0da 100644 --- a/docs/library/pyb.USB_HID.rst +++ b/docs/library/pyb.USB_HID.rst @@ -21,7 +21,7 @@ Constructors Methods ------- -.. method:: USB_HID.recv(data, \*, timeout=5000) +.. method:: USB_HID.recv(data, *, timeout=5000) Receive data on the bus: diff --git a/docs/library/pyb.USB_VCP.rst b/docs/library/pyb.USB_VCP.rst index b16924cf60b2e..bbcbc0701b3b4 100644 --- a/docs/library/pyb.USB_VCP.rst +++ b/docs/library/pyb.USB_VCP.rst @@ -21,7 +21,7 @@ Constructors Methods ------- -.. method:: USB_VCP.init(\*, flow=-1) +.. method:: USB_VCP.init(*, flow=-1) Configure the USB VCP port. If the *flow* argument is not -1 then the value sets the flow control, which can be a bitwise-or of ``USB_VCP.RTS`` and ``USB_VCP.CTS``. @@ -89,7 +89,7 @@ Methods Returns the number of bytes written. -.. method:: USB_VCP.recv(data, \*, timeout=5000) +.. method:: USB_VCP.recv(data, *, timeout=5000) Receive data on the bus: @@ -100,7 +100,7 @@ Methods Return value: if ``data`` is an integer then a new buffer of the bytes received, otherwise the number of bytes read into ``data`` is returned. -.. method:: USB_VCP.send(data, \*, timeout=5000) +.. method:: USB_VCP.send(data, *, timeout=5000) Send data over the USB VCP: diff --git a/docs/library/pyb.rst b/docs/library/pyb.rst index 34103195ddd1c..addcd20a91aeb 100644 --- a/docs/library/pyb.rst +++ b/docs/library/pyb.rst @@ -210,7 +210,7 @@ Miscellaneous functions It only makes sense to call this function from within boot.py. -.. function:: mount(device, mountpoint, \*, readonly=False, mkfs=False) +.. function:: mount(device, mountpoint, *, readonly=False, mkfs=False) .. note:: This function is deprecated. Mounting and unmounting devices should be performed by :meth:`uos.mount` and :meth:`uos.umount` instead. diff --git a/docs/library/uasyncio.rst b/docs/library/uasyncio.rst index 31b38a4e05f2f..a81e532d7fd25 100644 --- a/docs/library/uasyncio.rst +++ b/docs/library/uasyncio.rst @@ -80,7 +80,7 @@ Additional functions This is a coroutine, and a MicroPython extension. -.. function:: gather(\*awaitables, return_exceptions=False) +.. function:: gather(*awaitables, return_exceptions=False) Run all *awaitables* concurrently. Any *awaitables* that are not tasks are promoted to tasks. diff --git a/docs/library/uos.rst b/docs/library/uos.rst index ad10d9129713d..051247b717419 100644 --- a/docs/library/uos.rst +++ b/docs/library/uos.rst @@ -144,7 +144,7 @@ programs. Ports that have this functionality provide the :func:`mount` and :func:`umount` functions, and possibly various filesystem implementations represented by VFS classes. -.. function:: mount(fsobj, mount_point, \*, readonly) +.. function:: mount(fsobj, mount_point, *, readonly) Mount the filesystem object *fsobj* at the location in the VFS given by the *mount_point* string. *fsobj* can be a a VFS object that has a ``mount()`` From 8727c4e2eca9026494c173061c5905d54fa5f73d Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Tue, 11 Aug 2020 12:44:51 -0700 Subject: [PATCH 0205/3533] stm32/pin_defs_stm32: Fix pin printing to show IN mode correctly. Prior to this commit, if you configure a pin as an output type (I2C in this example) and then later configure it back as an input, then it will report the type incorrectly. Example: >>> import machine >>> b6 = machine.Pin('B6') >>> b6 Pin(Pin.cpu.B6, mode=Pin.IN) >>> machine.I2C(1) I2C(1, scl=B6, sda=B7, freq=420000) >>> b6 Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=Pin.AF4_I2C1) >>> b6.init(machine.Pin.IN) >>> b6 Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, af=Pin.AF4_I2C1) With this commit the last print now works: >>> b6 Pin(Pin.cpu.B6, mode=Pin.IN) --- ports/stm32/pin_defs_stm32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/pin_defs_stm32.c b/ports/stm32/pin_defs_stm32.c index 0aef5f95fa294..a3f9d039d6320 100644 --- a/ports/stm32/pin_defs_stm32.c +++ b/ports/stm32/pin_defs_stm32.c @@ -8,9 +8,9 @@ uint32_t pin_get_mode(const pin_obj_t *pin) { GPIO_TypeDef *gpio = pin->gpio; uint32_t mode = (gpio->MODER >> (pin->pin * 2)) & 3; - if (mode != GPIO_MODE_ANALOG) { + if (mode == GPIO_MODE_OUTPUT_PP || mode == GPIO_MODE_AF_PP) { if (gpio->OTYPER & pin->pin_mask) { - mode |= 1 << 4; + mode |= 1 << 4; // Converts from xxx_PP to xxx_OD } } return mode; From 60cf2c0959ed2668c01df664a2e7c3cd1f5c6d89 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Wed, 12 Aug 2020 21:30:33 +0200 Subject: [PATCH 0206/3533] tools/pyboard.py: Replace eval() of received data with alternative. Prior to this commit, pyboard.py used eval() to "parse" file data received from the board. Using eval() on received data from a device is dangerous, because a malicious device may inject arbitrary code execution on the PC that is doing the operation. Consider the following scenario: Eve may write a malicious script to Bob's board in his absence. On return Bob notices that something is wrong with the board, because it doesn't work as expected anymore. He wants to read out boot.py (or any other file) to see what is wrong. What he gets is a remote code execution on his PC. Proof of concept: Eve: $ cat boot.py _print = print print = lambda *x, **y: _print("os.system('ls /; echo Pwned!')", end="\r\n\x04") $ ./pyboard.py -f cp boot.py : cp boot.py :boot.py Bob: $ ./pyboard.py -f cp :boot.py /tmp/foo cp :boot.py /tmp/foo bin chroot dev home lib32 media opt root sbin sys usr boot config etc lib lib64 mnt proc run srv tmp var Pwned! There's also the possibility that the device is malfunctioning and sends random and possibly dangerous data back to the PC, to be eval'd. Fix this problem by using ast.literal_eval() to parse the received bytes, instead of eval(). Signed-off-by: Michael Buesch --- tools/pyboard.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 6376b13c7e333..c97ddbe487be3 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -70,6 +70,7 @@ import sys import time import os +import ast try: stdout = sys.stdout.buffer @@ -426,7 +427,12 @@ def fs_get(self, src, dest, chunk_size=256): data = bytearray() self.exec_("print(r(%u))" % chunk_size, data_consumer=lambda d: data.extend(d)) assert data.endswith(b"\r\n\x04") - data = eval(str(data[:-3], "ascii")) + try: + data = ast.literal_eval(str(data[:-3], "ascii")) + if not isinstance(data, bytes): + raise ValueError("Not bytes") + except (UnicodeError, ValueError) as e: + raise PyboardError("fs_get: Could not interpret received data: %s" % str(e)) if not data: break f.write(data) From 492cf34fd860330f44a74732d89e9db8bc02a4e8 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Thu, 13 Aug 2020 15:20:08 +0200 Subject: [PATCH 0207/3533] tools/mpy-tool.py: Fix offset of line number info. Signed-off-by: Martin Milata --- tools/mpy-tool.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 43d35503eb564..1ac6c93d7d8b2 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -253,6 +253,7 @@ def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes): self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset) self.simple_name = self._unpack_qstr(self.ip2) self.source_file = self._unpack_qstr(self.ip2 + 2) + self.line_info_offset = self.ip2 + 4 def _unpack_qstr(self, ip): qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8 @@ -404,7 +405,10 @@ def freeze_module(self, qstr_links=(), type_sig=0): print(" .n_def_pos_args = %u," % self.prelude[5]) print(" .qstr_block_name = %s," % self.simple_name.qstr_id) print(" .qstr_source_file = %s," % self.source_file.qstr_id) - print(" .line_info = fun_data_%s + %u," % (self.escaped_name, 0)) # TODO + print( + " .line_info = fun_data_%s + %u," + % (self.escaped_name, self.line_info_offset) + ) print(" .opcodes = fun_data_%s + %u," % (self.escaped_name, self.ip)) print(" },") print(" .line_of_definition = %u," % 0) # TODO From 448319a7450714efd72ae527e184a247a82ee39d Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 22 Aug 2020 01:07:57 +1000 Subject: [PATCH 0208/3533] tools/makemanifest.py: Use os.makedirs to make path for generated files. The existing implementation of mkdir() in this file is not sophisticated enough to work correctly on all operating systems (eg Mac can raise EISDIR). Using the standard os.makedirs() function handles all cases correctly. Signed-off-by: Damien George --- tools/makemanifest.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index b7d4a4d4a68af..377f245596fe9 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -25,7 +25,6 @@ # THE SOFTWARE. from __future__ import print_function -import errno import sys import os import subprocess @@ -165,17 +164,10 @@ def get_timestamp_newest(path): return ts_newest -def mkdir(path): - cur_path = "" - for p in path.split("/")[:-1]: - cur_path += p + "/" - try: - os.mkdir(cur_path) - except OSError as er: - if er.args[0] == errno.EEXIST: - pass - else: - raise er +def mkdir(filename): + path = os.path.dirname(filename) + if not os.path.isdir(path): + os.makedirs(path) def freeze_internal(kind, path, script, opt): From 5f9b105244bb9f605f3ca157cd421967c665bd6e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 21 Aug 2020 10:30:09 +1000 Subject: [PATCH 0209/3533] py/runtime: Fix builtin compile() in "single" mode so it prints exprs. As per CPython behaviour, compile(stmt, "file", "single") should create code which prints to stdout (via __repl_print__) the results of any expressions in stmt. Signed-off-by: Damien George --- py/runtime.c | 2 +- tests/basics/builtin_compile.py | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/py/runtime.c b/py/runtime.c index 38da2f4538e97..c12271f4e2cf5 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1473,7 +1473,7 @@ mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_i if (nlr_push(&nlr) == 0) { qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind); - mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false); + mp_obj_t module_fun = mp_compile(&parse_tree, source_name, parse_input_kind == MP_PARSE_SINGLE_INPUT); mp_obj_t ret; if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) { diff --git a/tests/basics/builtin_compile.py b/tests/basics/builtin_compile.py index bf272aca15d43..a2f2cbe550131 100644 --- a/tests/basics/builtin_compile.py +++ b/tests/basics/builtin_compile.py @@ -1,11 +1,10 @@ # test compile builtin -def have_compile(): - try: - compile - return True - except NameError: - return False +try: + compile +except NameError: + print("SKIP") + raise SystemExit def test(): global x @@ -26,8 +25,9 @@ def test(): exec(c, {}, {"x":3}) # single/eval mode - exec(compile('print(1 + 1)', 'file', 'single')) - print(eval(compile('1 + 1', 'file', 'eval'))) + exec(compile("if 1: 10 + 1\n", "file", "single")) + exec(compile("print(10 + 2)", "file", "single")) + print(eval(compile("10 + 3", "file", "eval"))) # bad mode try: @@ -42,8 +42,4 @@ def test(): print("NameError") print(x) # check 'x' still exists as a global -if have_compile(): - test() -else: - print("SKIP") - raise SystemExit +test() From 20948a3d54ff560a3d029b8bfc9761dcbbad9312 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 20 Aug 2020 23:11:44 +1000 Subject: [PATCH 0210/3533] tests/extmod: Add test for uasyncio.sleep of a negative time. It should take 0 time to await on a negative sleep. Signed-off-by: Damien George --- tests/extmod/uasyncio_basic.py | 12 ++++++++++-- tests/extmod/uasyncio_basic.py.exp | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/extmod/uasyncio_basic.py b/tests/extmod/uasyncio_basic.py index f6685fa674676..c88908d99b320 100644 --- a/tests/extmod/uasyncio_basic.py +++ b/tests/extmod/uasyncio_basic.py @@ -36,8 +36,16 @@ async def main(): t1 = ticks() await delay_print(0.04, "long") t2 = ticks() - - print("took {} {}".format(round(ticks_diff(t1, t0), -1), round(ticks_diff(t2, t1), -1))) + await delay_print(-1, "negative") + t3 = ticks() + + print( + "took {} {} {}".format( + round(ticks_diff(t1, t0), -1), + round(ticks_diff(t2, t1), -1), + round(ticks_diff(t3, t2), -1), + ) + ) asyncio.run(main()) diff --git a/tests/extmod/uasyncio_basic.py.exp b/tests/extmod/uasyncio_basic.py.exp index 447d05d5e0984..6673978769fbf 100644 --- a/tests/extmod/uasyncio_basic.py.exp +++ b/tests/extmod/uasyncio_basic.py.exp @@ -2,4 +2,5 @@ start after sleep short long -took 20 40 +negative +took 20 40 0 From 55c76eaac12af4e93f8de11bb25c835e8b65c623 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 20 Aug 2020 23:13:25 +1000 Subject: [PATCH 0211/3533] extmod/uasyncio: Truncate negative sleeps to 0. Otherwise a task that continuously awaits on a large negative sleep can monopolise the scheduler (because its wake time is always less than everything else in the pairing heap). Signed-off-by: Damien George --- extmod/uasyncio/core.py | 2 +- tests/extmod/uasyncio_fair.py | 1 + tests/extmod/uasyncio_fair.py.exp | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/extmod/uasyncio/core.py b/extmod/uasyncio/core.py index 689487d36a6fd..045b4cd139182 100644 --- a/extmod/uasyncio/core.py +++ b/extmod/uasyncio/core.py @@ -53,7 +53,7 @@ def __next__(self): # Use a SingletonGenerator to do it without allocating on the heap def sleep_ms(t, sgen=SingletonGenerator()): assert sgen.state is None - sgen.state = ticks_add(ticks(), t) + sgen.state = ticks_add(ticks(), max(0, t)) return sgen diff --git a/tests/extmod/uasyncio_fair.py b/tests/extmod/uasyncio_fair.py index 9b04454bc1f50..e0ee811a9b046 100644 --- a/tests/extmod/uasyncio_fair.py +++ b/tests/extmod/uasyncio_fair.py @@ -22,6 +22,7 @@ async def main(): t1 = asyncio.create_task(task(1, -0.01)) t2 = asyncio.create_task(task(2, 0.1)) t3 = asyncio.create_task(task(3, 0.2)) + t3 = asyncio.create_task(task(4, -100)) await asyncio.sleep(0.5) t1.cancel() t2.cancel() diff --git a/tests/extmod/uasyncio_fair.py.exp b/tests/extmod/uasyncio_fair.py.exp index 4428943f46287..b4b6481db019f 100644 --- a/tests/extmod/uasyncio_fair.py.exp +++ b/tests/extmod/uasyncio_fair.py.exp @@ -3,6 +3,7 @@ task start 2 task work 2 task start 3 task work 3 +task start 4 task work 2 task work 3 task work 2 From 92899354d96a5b5463f3002b2476a73c11ebe626 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 29 Jul 2020 01:01:31 +1000 Subject: [PATCH 0212/3533] unix/fatfs_port: Implement get_fattime. Signed-off-by: Damien George --- ports/unix/fatfs_port.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ports/unix/fatfs_port.c b/ports/unix/fatfs_port.c index 30f1959f52063..4ec5b919ab54b 100644 --- a/ports/unix/fatfs_port.c +++ b/ports/unix/fatfs_port.c @@ -1,5 +1,13 @@ +#include #include "lib/oofatfs/ff.h" DWORD get_fattime(void) { - return 0; + time_t now = time(NULL); + struct tm *tm = localtime(&now); + return ((1900 + tm->tm_year - 1980) << 25) + | (tm->tm_mon << 21) + | (tm->tm_mday << 16) + | (tm->tm_hour << 11) + | (tm->tm_min << 5) + | (tm->tm_sec / 2); } From badd351150df70bea6644db98f48fbc3a1e5ffea Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 2 Aug 2020 00:32:38 +1000 Subject: [PATCH 0213/3533] lib/timeutils: Add helper functions to deal with nanosecs since 1970. Signed-off-by: Damien George --- lib/timeutils/timeutils.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/timeutils/timeutils.h b/lib/timeutils/timeutils.h index cb7a72123a119..08b0dc2e8559a 100644 --- a/lib/timeutils/timeutils.h +++ b/lib/timeutils/timeutils.h @@ -27,6 +27,10 @@ #ifndef MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H #define MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H +// The number of seconds between 1970/1/1 and 2000/1/1 is calculated using: +// time.mktime((2000,1,1,0,0,0,0,0,0)) - time.mktime((1970,1,1,0,0,0,0,0,0)) +#define TIMEUTILS_SECONDS_1970_TO_2000 (946684800ULL) + typedef struct _timeutils_struct_time_t { uint16_t tm_year; // i.e. 2014 uint8_t tm_mon; // 1..12 @@ -38,6 +42,14 @@ typedef struct _timeutils_struct_time_t { uint16_t tm_yday; // 1..366 } timeutils_struct_time_t; +static inline uint64_t timeutils_seconds_since_2000_to_nanoseconds_since_1970(mp_uint_t s) { + return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL; +} + +static inline mp_uint_t timeutils_seconds_since_2000_from_nanoseconds_since_1970(uint64_t ns) { + return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000; +} + bool timeutils_is_leap_year(mp_uint_t year); mp_uint_t timeutils_days_in_month(mp_uint_t year, mp_uint_t month); mp_uint_t timeutils_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date); @@ -51,4 +63,10 @@ mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds); +static inline uint64_t timeutils_nanoseconds_since_1970(mp_uint_t year, mp_uint_t month, + mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) { + return timeutils_seconds_since_2000_to_nanoseconds_since_1970( + timeutils_seconds_since_2000(year, month, date, hour, minute, second)); +} + #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H From ee50a6effebf315c38d4a129dc9f65ee722eb5b6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 1 Aug 2020 23:50:23 +1000 Subject: [PATCH 0214/3533] py/mphal.h: Introduce mp_hal_time_ns and implement on various ports. This should return a 64-bit value being the number of nanoseconds since 1970/1/1. Signed-off-by: Damien George --- ports/esp32/mphalport.c | 7 +++++++ ports/esp8266/esp_mphal.c | 4 ++++ ports/stm32/rtc.c | 18 ++++++++++++++++++ ports/unix/unix_mphal.c | 5 +++++ ports/zephyr/mphalport.h | 5 +++++ py/mphal.h | 6 ++++++ 6 files changed, 45 insertions(+) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 99548ad627be5..5e526df6673cb 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -28,6 +28,7 @@ #include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -195,6 +196,12 @@ void mp_hal_delay_us(uint32_t us) { } } +uint64_t mp_hal_time_ns(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; +} + // Wake up the main task if it is sleeping void mp_hal_wake_main_task_from_isr(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index c467ab72db7ec..9e1b72e438ad7 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -138,6 +138,10 @@ void MP_FASTCODE(mp_hal_delay_ms)(uint32_t delay) { mp_hal_delay_us(delay * 1000); } +uint64_t mp_hal_time_ns(void) { + return pyb_rtc_get_us_since_2000() * 1000ULL; +} + void ets_event_poll(void) { ets_loop_iter(); mp_handle_pending(true); diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index de26352004771..4f759c4bc74f0 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -27,6 +27,7 @@ #include #include "py/runtime.h" +#include "lib/timeutils/timeutils.h" #include "extint.h" #include "rtc.h" #include "irq.h" @@ -442,6 +443,23 @@ STATIC void RTC_CalendarConfig(void) { } } +uint64_t mp_hal_time_ns(void) { + uint64_t ns = 0; + #if MICROPY_HW_ENABLE_RTC + // Get current according to the RTC. + rtc_init_finalise(); + RTC_TimeTypeDef time; + RTC_DateTypeDef date; + HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); + HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); + ns = timeutils_nanoseconds_since_1970( + 2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds); + uint32_t usec = ((RTC_SYNCH_PREDIV - time.SubSeconds) * (1000000 / 64)) / ((RTC_SYNCH_PREDIV + 1) / 64); + ns += usec * 1000; + #endif + return ns; +} + /******************************************************************************/ // MicroPython bindings diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 42c22f7b50482..f43067f646ba9 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -214,3 +214,8 @@ mp_uint_t mp_hal_ticks_us(void) { return tv.tv_sec * 1000000 + tv.tv_usec; #endif } + +uint64_t mp_hal_time_ns(void) { + time_t now = time(NULL); + return (uint64_t)now * 1000000000ULL; +} diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h index 8434a388b0a5e..9ef213ddb0809 100644 --- a/ports/zephyr/mphalport.h +++ b/ports/zephyr/mphalport.h @@ -24,6 +24,11 @@ static inline void mp_hal_delay_ms(mp_uint_t delay) { k_msleep(delay); } +static inline uint64_t mp_hal_time_ns(void) { + // Not currently implemented. + return 0; +} + #define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us)) #define mp_hal_pin_od_low(p) (mp_raise_NotImplementedError("mp_hal_pin_od_low")) #define mp_hal_pin_od_high(p) (mp_raise_NotImplementedError("mp_hal_pin_od_high")) diff --git a/py/mphal.h b/py/mphal.h index 66d80705a60ee..6d11f6ddc0836 100644 --- a/py/mphal.h +++ b/py/mphal.h @@ -26,6 +26,7 @@ #ifndef MICROPY_INCLUDED_PY_MPHAL_H #define MICROPY_INCLUDED_PY_MPHAL_H +#include #include "py/mpconfig.h" #ifdef MICROPY_MPHALPORT_H @@ -74,6 +75,11 @@ mp_uint_t mp_hal_ticks_us(void); mp_uint_t mp_hal_ticks_cpu(void); #endif +#ifndef mp_hal_time_ns +// Nanoseconds since 1970/1/1. +uint64_t mp_hal_time_ns(void); +#endif + // If port HAL didn't define its own pin API, use generic // "virtual pin" API from the core. #ifndef mp_hal_pin_obj_t From 2acc087880de39d7e17abc9344b8d2faba3478dd Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 29 Jul 2020 01:01:48 +1000 Subject: [PATCH 0215/3533] extmod/vfs_lfs: Add mtime support to littlefs files. This commit adds support for modification time of files on littlefs v2 filesystems, using file attributes. For some background see issue #6114. Features/properties of this implementation: - Only supported on littlefs2 (not littlefs1). - Uses littlefs2's general file attributes to store the timestamp. - The timestamp is 64-bits and stores nanoseconds since 1970/1/1 (if the range to the year 2554 is not enough then additional bits can be added to this timestamp by adding another file attribute). - mtime is enabled by default but can be disabled in the constructor, eg: uos.mount(uos.VfsLfs2(bdev, mtime=False), '/flash') - It's fully backwards compatible, existing littlefs2 filesystems will work without reformatting and timestamps will be added transparently to existing files (once they are opened for writing). - Files without timestamps will open correctly, and stat will just return 0 for their timestamp. - mtime can be disabled or enabled each mount time and timestamps will only be updated if mtime is enabled (otherwise they will be untouched). Signed-off-by: Damien George --- docs/library/uos.rst | 16 +++-- extmod/vfs_lfs.c | 21 ++++++- extmod/vfs_lfsx.c | 25 ++++++-- extmod/vfs_lfsx_file.c | 18 +++++- tests/extmod/vfs_lfs.py | 13 ++-- tests/extmod/vfs_lfs.py.exp | 16 ++--- tests/extmod/vfs_lfs_mtime.py | 98 +++++++++++++++++++++++++++++++ tests/extmod/vfs_lfs_mtime.py.exp | 13 ++++ 8 files changed, 197 insertions(+), 23 deletions(-) create mode 100644 tests/extmod/vfs_lfs_mtime.py create mode 100644 tests/extmod/vfs_lfs_mtime.py.exp diff --git a/docs/library/uos.rst b/docs/library/uos.rst index 051247b717419..edc94556b1722 100644 --- a/docs/library/uos.rst +++ b/docs/library/uos.rst @@ -178,7 +178,7 @@ represented by VFS classes. Build a FAT filesystem on *block_dev*. -.. class:: VfsLfs1(block_dev) +.. class:: VfsLfs1(block_dev, readsize=32, progsize=32, lookahead=32) Create a filesystem object that uses the `littlefs v1 filesystem format`_. Storage of the littlefs filesystem is provided by *block_dev*, which must @@ -187,23 +187,31 @@ represented by VFS classes. See :ref:`filesystem` for more information. - .. staticmethod:: mkfs(block_dev) + .. staticmethod:: mkfs(block_dev, readsize=32, progsize=32, lookahead=32) Build a Lfs1 filesystem on *block_dev*. .. note:: There are reports of littlefs v1 failing in certain situations, for details see `littlefs issue 347`_. -.. class:: VfsLfs2(block_dev) +.. class:: VfsLfs2(block_dev, readsize=32, progsize=32, lookahead=32, mtime=True) Create a filesystem object that uses the `littlefs v2 filesystem format`_. Storage of the littlefs filesystem is provided by *block_dev*, which must support the :ref:`extended interface `. Objects created by this constructor can be mounted using :func:`mount`. + The *mtime* argument enables modification timestamps for files, stored using + littlefs attributes. This option can be disabled or enabled differently each + mount time and timestamps will only be added or updated if *mtime* is enabled, + otherwise the timestamps will remain untouched. Littlefs v2 filesystems without + timestamps will work without reformatting and timestamps will be added + transparently to existing files once they are opened for writing. When *mtime* + is enabled `uos.stat` on files without timestamps will return 0 for the timestamp. + See :ref:`filesystem` for more information. - .. staticmethod:: mkfs(block_dev) + .. staticmethod:: mkfs(block_dev, readsize=32, progsize=32, lookahead=32) Build a Lfs2 filesystem on *block_dev*. diff --git a/extmod/vfs_lfs.c b/extmod/vfs_lfs.c index 90a1996f9ce4a..a53f66f2d63c8 100644 --- a/extmod/vfs_lfs.c +++ b/extmod/vfs_lfs.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2019-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,18 +25,20 @@ */ #include "py/runtime.h" +#include "py/mphal.h" #include "extmod/vfs.h" #include "extmod/vfs_lfs.h" #if MICROPY_VFS && (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) -enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead }; +enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead, LFS_MAKE_ARG_mtime }; static const mp_arg_t lfs_make_allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, + { MP_QSTR_mtime, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; #if MICROPY_VFS_LFS1 @@ -98,9 +100,13 @@ mp_obj_t mp_vfs_lfs1_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode #define MP_TYPE_VFS_LFSx mp_type_vfs_lfs2 #define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs2##s +// Attribute ids for lfs2_attr.type. +#define LFS_ATTR_MTIME (1) // 64-bit little endian, nanoseconds since 1970/1/1 + typedef struct _mp_obj_vfs_lfs2_t { mp_obj_base_t base; mp_vfs_blockdev_t blockdev; + bool enable_mtime; vstr_t cur_dir; struct lfs2_config config; lfs2_t lfs; @@ -109,14 +115,25 @@ typedef struct _mp_obj_vfs_lfs2_t { typedef struct _mp_obj_vfs_lfs2_file_t { mp_obj_base_t base; mp_obj_vfs_lfs2_t *vfs; + uint8_t mtime[8]; lfs2_file_t file; struct lfs2_file_config cfg; + struct lfs2_attr attrs[1]; uint8_t file_buffer[0]; } mp_obj_vfs_lfs2_file_t; const char *mp_vfs_lfs2_make_path(mp_obj_vfs_lfs2_t *self, mp_obj_t path_in); mp_obj_t mp_vfs_lfs2_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in); +STATIC void lfs_get_mtime(uint8_t buf[8]) { + uint64_t ns = mp_hal_time_ns(); + // Store "ns" to "buf" in little-endian format (essentially htole64). + for (size_t i = 0; i < 8; ++i) { + buf[i] = ns; + ns >>= 8; + } +} + #include "extmod/vfs_lfsx.c" #include "extmod/vfs_lfsx_file.c" diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 24816433be015..511b741b0dec1 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2019-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,6 +34,7 @@ #include "py/objstr.h" #include "py/mperrno.h" #include "extmod/vfs.h" +#include "lib/timeutils/timeutils.h" STATIC int MP_VFS_LFSx(dev_ioctl)(const struct LFSx_API (config) * c, int cmd, int arg, bool must_return_int) { mp_obj_t ret = mp_vfs_blockdev_ioctl(c->context, cmd, arg); @@ -120,6 +121,9 @@ STATIC mp_obj_t MP_VFS_LFSx(make_new)(const mp_obj_type_t * type, size_t n_args, self->base.type = type; vstr_init(&self->cur_dir, 16); vstr_add_byte(&self->cur_dir, '/'); + #if LFS_BUILD_VERSION == 2 + self->enable_mtime = args[LFS_MAKE_ARG_mtime].u_bool; + #endif MP_VFS_LFSx(init_config)(self, args[LFS_MAKE_ARG_bdev].u_obj, args[LFS_MAKE_ARG_readsize].u_int, args[LFS_MAKE_ARG_progsize].u_int, args[LFS_MAKE_ARG_lookahead].u_int); int ret = LFSx_API(mount)(&self->lfs, &self->config); @@ -352,6 +356,19 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) { mp_raise_OSError(-ret); } + mp_uint_t mtime = 0; + #if LFS_BUILD_VERSION == 2 + uint8_t mtime_buf[8]; + lfs2_ssize_t sz = lfs2_getattr(&self->lfs, path, LFS_ATTR_MTIME, &mtime_buf, sizeof(mtime_buf)); + if (sz == sizeof(mtime_buf)) { + uint64_t ns = 0; + for (size_t i = sizeof(mtime_buf); i > 0; --i) { + ns = ns << 8 | mtime_buf[i - 1]; + } + mtime = timeutils_seconds_since_2000_from_nanoseconds_since_1970(ns); + } + #endif + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); t->items[0] = MP_OBJ_NEW_SMALL_INT(info.type == LFSx_MACRO(_TYPE_REG) ? MP_S_IFREG : MP_S_IFDIR); // st_mode t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino @@ -360,9 +377,9 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) { t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid t->items[6] = mp_obj_new_int_from_uint(info.size); // st_size - t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // st_atime - t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // st_mtime - t->items[9] = MP_OBJ_NEW_SMALL_INT(0); // st_ctime + t->items[7] = MP_OBJ_NEW_SMALL_INT(mtime); // st_atime + t->items[8] = MP_OBJ_NEW_SMALL_INT(mtime); // st_mtime + t->items[9] = MP_OBJ_NEW_SMALL_INT(mtime); // st_ctime return MP_OBJ_FROM_PTR(t); } diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index f74b41837d32d..bc1a37b90bdb8 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2019-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -101,6 +101,17 @@ mp_obj_t MP_VFS_LFSx(file_open)(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mod #endif o->cfg.buffer = &o->file_buffer[0]; + #if LFS_BUILD_VERSION == 2 + if (self->enable_mtime) { + lfs_get_mtime(&o->mtime[0]); + o->attrs[0].type = LFS_ATTR_MTIME; + o->attrs[0].buffer = &o->mtime[0]; + o->attrs[0].size = sizeof(o->mtime); + o->cfg.attrs = &o->attrs[0]; + o->cfg.attr_count = MP_ARRAY_SIZE(o->attrs); + } + #endif + const char *path = MP_VFS_LFSx(make_path)(self, path_in); int ret = LFSx_API(file_opencfg)(&self->lfs, &o->file, path, flags, &o->cfg); if (ret < 0) { @@ -131,6 +142,11 @@ STATIC mp_uint_t MP_VFS_LFSx(file_read)(mp_obj_t self_in, void *buf, mp_uint_t s STATIC mp_uint_t MP_VFS_LFSx(file_write)(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in); MP_VFS_LFSx(check_open)(self); + #if LFS_BUILD_VERSION == 2 + if (self->vfs->enable_mtime) { + lfs_get_mtime(&self->mtime[0]); + } + #endif LFSx_API(ssize_t) sz = LFSx_API(file_write)(&self->vfs->lfs, &self->file, buf, size); if (sz < 0) { *errcode = -sz; diff --git a/tests/extmod/vfs_lfs.py b/tests/extmod/vfs_lfs.py index 609d9f949e17d..8e56400df3c2b 100644 --- a/tests/extmod/vfs_lfs.py +++ b/tests/extmod/vfs_lfs.py @@ -35,6 +35,11 @@ def ioctl(self, op, arg): return 0 +def print_stat(st, print_size=True): + # don't print times (just check that they have the correct type) + print(st[:6], st[6] if print_size else -1, type(st[7]), type(st[8]), type(st[9])) + + def test(bdev, vfs_class): print("test", vfs_class) @@ -69,10 +74,10 @@ def test(bdev, vfs_class): vfs.mkdir("testdir") # stat a file - print(vfs.stat("test")) + print_stat(vfs.stat("test")) # stat a dir (size seems to vary on LFS2 so don't print that) - print(vfs.stat("testdir")[:6]) + print_stat(vfs.stat("testdir"), False) # read with vfs.open("test", "r") as f: @@ -112,8 +117,8 @@ def test(bdev, vfs_class): # create file in directory to make sure paths are relative vfs.open("test2", "w").close() - print(vfs.stat("test2")) - print(vfs.stat("/testdir/test2")) + print_stat(vfs.stat("test2")) + print_stat(vfs.stat("/testdir/test2")) vfs.remove("test2") # chdir back to root and remove testdir diff --git a/tests/extmod/vfs_lfs.py.exp b/tests/extmod/vfs_lfs.py.exp index a7025577449d0..a0450c84b79f8 100644 --- a/tests/extmod/vfs_lfs.py.exp +++ b/tests/extmod/vfs_lfs.py.exp @@ -7,8 +7,8 @@ test [('test', 32768, 0, 8), ('testdir', 16384, 0, 0)] [] [('test', 32768, 0, 8)] -(32768, 0, 0, 0, 0, 0, 8, 0, 0, 0) -(16384, 0, 0, 0, 0, 0) +(32768, 0, 0, 0, 0, 0) 8 +(16384, 0, 0, 0, 0, 0) -1 littlefs data length: 4096 write 0 @@ -22,8 +22,8 @@ write 3 [('test', 32768, 0, 8), ('testdir', 16384, 0, 0)] / /testdir -(32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) -(32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) +(32768, 0, 0, 0, 0, 0) 0 +(32768, 0, 0, 0, 0, 0) 0 / /testdir / @@ -44,8 +44,8 @@ test [('testdir', 16384, 0, 0), ('test', 32768, 0, 8)] [] [('test', 32768, 0, 8)] -(32768, 0, 0, 0, 0, 0, 8, 0, 0, 0) -(16384, 0, 0, 0, 0, 0) +(32768, 0, 0, 0, 0, 0) 8 +(16384, 0, 0, 0, 0, 0) -1 littlefs data length: 4096 write 0 @@ -59,8 +59,8 @@ write 3 [('test', 32768, 0, 8), ('testdir', 16384, 0, 0)] / /testdir -(32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) -(32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) +(32768, 0, 0, 0, 0, 0) 0 +(32768, 0, 0, 0, 0, 0) 0 / /testdir / diff --git a/tests/extmod/vfs_lfs_mtime.py b/tests/extmod/vfs_lfs_mtime.py new file mode 100644 index 0000000000000..0108076884055 --- /dev/null +++ b/tests/extmod/vfs_lfs_mtime.py @@ -0,0 +1,98 @@ +# Test for VfsLfs using a RAM device, mtime feature + +try: + import utime, uos + + utime.sleep + uos.VfsLfs2 +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMBlockDevice: + ERASE_BLOCK_SIZE = 1024 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) + + def readblocks(self, block, buf, off): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + buf[i] = self.data[addr + i] + + def writeblocks(self, block, buf, off): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + self.data[addr + i] = buf[i] + + def ioctl(self, op, arg): + if op == 4: # block count + return len(self.data) // self.ERASE_BLOCK_SIZE + if op == 5: # block size + return self.ERASE_BLOCK_SIZE + if op == 6: # erase block + return 0 + + +def test(bdev, vfs_class): + print("test", vfs_class) + + # Initial format of block device. + vfs_class.mkfs(bdev) + + # construction + print("mtime=True") + vfs = vfs_class(bdev, mtime=True) + + # Create an empty file, should have a timestamp. + vfs.open("test1", "wt").close() + + # Wait 1 second so mtime will increase by at least 1. + utime.sleep(1) + + # Create another empty file, should have a timestamp. + vfs.open("test2", "wt").close() + + # Stat the files and check that test1 is older than test2. + stat1 = vfs.stat("test1") + stat2 = vfs.stat("test2") + print(stat1[8] != 0, stat2[8] != 0) + print(stat1[8] < stat2[8]) + + # Wait 1 second so mtime will increase by at least 1. + utime.sleep(1) + + # Open test1 for reading and ensure mtime did not change. + vfs.open("test1", "rt").close() + print(vfs.stat("test1") == stat1) + + # Open test1 for writing and ensure mtime increased from the previous value. + vfs.open("test1", "wt").close() + stat1_old = stat1 + stat1 = vfs.stat("test1") + print(stat1_old[8] < stat1[8]) + + # Unmount. + vfs.umount() + + # Check that remounting with mtime=False can read the timestamps. + print("mtime=False") + vfs = vfs_class(bdev, mtime=False) + print(vfs.stat("test1") == stat1) + print(vfs.stat("test2") == stat2) + f = vfs.open("test1", "wt") + f.close() + print(vfs.stat("test1") == stat1) + vfs.umount() + + # Check that remounting with mtime=True still has the timestamps. + print("mtime=True") + vfs = vfs_class(bdev, mtime=True) + print(vfs.stat("test1") == stat1) + print(vfs.stat("test2") == stat2) + vfs.umount() + + +bdev = RAMBlockDevice(30) +test(bdev, uos.VfsLfs2) diff --git a/tests/extmod/vfs_lfs_mtime.py.exp b/tests/extmod/vfs_lfs_mtime.py.exp new file mode 100644 index 0000000000000..2e3d7491bcbd8 --- /dev/null +++ b/tests/extmod/vfs_lfs_mtime.py.exp @@ -0,0 +1,13 @@ +test +mtime=True +True True +True +True +True +mtime=False +True +True +True +mtime=True +True +True From 3d9a7ed02f4e50ee2eb541ac2f4d7f6f5b5d316e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:43:37 +1000 Subject: [PATCH 0216/3533] extmod/btstack: Implement GAP scan duration_ms parameter. This commit makes scanning work when duration_ms is set to zero. Prior to this it would not work with duration_ms set to zero. --- extmod/btstack/modbluetooth_btstack.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index c7269d33ed5ca..89e396dfd08e7 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -889,7 +889,7 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle) { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC btstack_timer_source_t scan_duration_timeout; -STATIC void hci_initialization_timeout_handler(btstack_timer_source_t *ds) { +STATIC void scan_duration_timeout_handler(btstack_timer_source_t *ds) { (void)ds; mp_bluetooth_gap_scan_stop(); } @@ -897,9 +897,11 @@ STATIC void hci_initialization_timeout_handler(btstack_timer_source_t *ds) { int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us) { DEBUG_EVENT_printf("mp_bluetooth_gap_scan_start\n"); - btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms); - btstack_run_loop_set_timer_handler(&scan_duration_timeout, hci_initialization_timeout_handler); - btstack_run_loop_add_timer(&scan_duration_timeout); + if (duration_ms > 0) { + btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms); + btstack_run_loop_set_timer_handler(&scan_duration_timeout, scan_duration_timeout_handler); + btstack_run_loop_add_timer(&scan_duration_timeout); + } // 0 = passive scan (we don't handle scan response). gap_set_scan_parameters(0, interval_us / 625, window_us / 625); From 0bc2c1c1057d7f5c1e4987139062386a8f9fe5f2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 17 Aug 2020 10:21:27 +1000 Subject: [PATCH 0217/3533] extmod/modbluetooth: Fix race between READ_REQUEST and other IRQs. The READ_REQUEST callback is handled as a hard interrupt (because the BLE stack needs an immediate response from it so it can continue) and so calls to Python require extra protection: - the caller-owned tuple passed into the callback must be separate from the tuple used by other callback events (which are soft interrupts); - the GC and scheduler must be locked during callback execution. --- extmod/modbluetooth.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 7bfb774782048..730c288ee7885 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -26,14 +26,15 @@ */ #include "py/binary.h" +#include "py/gc.h" #include "py/misc.h" #include "py/mperrno.h" +#include "py/mphal.h" #include "py/obj.h" -#include "py/objstr.h" #include "py/objarray.h" +#include "py/objstr.h" #include "py/qstr.h" #include "py/runtime.h" -#include "py/mphal.h" #include "extmod/modbluetooth.h" #include @@ -66,6 +67,9 @@ typedef struct { mp_obj_str_t irq_data_data; mp_obj_bluetooth_uuid_t irq_data_uuid; ringbuf_t ringbuf; + #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK + mp_obj_t irq_read_request_data_tuple; + #endif } mp_obj_bluetooth_ble_t; // TODO: this seems like it could be generic? @@ -252,6 +256,11 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, // Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler. o->irq_data_tuple = mp_obj_new_tuple(MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN, NULL); + #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK + // Pre-allocate a separate data tuple for the read request "hard" irq. + o->irq_read_request_data_tuple = mp_obj_new_tuple(2, NULL); + #endif + // Pre-allocated buffers for address, payload and uuid. o->irq_data_addr.base.type = &mp_type_bytes; o->irq_data_addr.data = o->irq_data_addr_bytes; @@ -1139,12 +1148,22 @@ void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); if (o->irq_handler != mp_const_none) { - // Use pre-allocated tuple because this is a hard IRQ. - mp_obj_tuple_t *data = MP_OBJ_TO_PTR(o->irq_data_tuple); + // When executing code within a handler we must lock the scheduler to + // prevent any scheduled callbacks from running, and lock the GC to + // prevent any memory allocations. + mp_sched_lock(); + gc_lock(); + + // Use pre-allocated tuple distinct to the one used by the "soft" IRQs. + mp_obj_tuple_t *data = MP_OBJ_TO_PTR(o->irq_read_request_data_tuple); data->items[0] = MP_OBJ_NEW_SMALL_INT(conn_handle); data->items[1] = MP_OBJ_NEW_SMALL_INT(value_handle); data->len = 2; - mp_obj_t irq_ret = mp_call_function_2_protected(o->irq_handler, MP_OBJ_NEW_SMALL_INT(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST), o->irq_data_tuple); + mp_obj_t irq_ret = mp_call_function_2_protected(o->irq_handler, MP_OBJ_NEW_SMALL_INT(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST), o->irq_read_request_data_tuple); + + gc_unlock(); + mp_sched_unlock(); + // If the IRQ handler explicitly returned false, then deny the read. Otherwise if it returns None/True, allow it. return irq_ret != MP_OBJ_NULL && (irq_ret == mp_const_none || mp_obj_is_true(irq_ret)); } else { From a80a146858b3c9a7fbae4030a524666ab19f7a47 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 14 Aug 2020 11:49:41 +1000 Subject: [PATCH 0218/3533] extmod/bluetooth: Support active scanning in BLE.gap_scan(). This adds an additional optional parameter to gap_scan() to select active scanning, where scan responses are returned as well as normal scan results. This parameter is False by default which retains the existing behaviour. --- docs/library/ubluetooth.rst | 4 +++- extmod/btstack/modbluetooth_btstack.c | 5 ++--- extmod/modbluetooth.c | 8 ++++++-- extmod/modbluetooth.h | 2 +- extmod/nimble/modbluetooth_nimble.c | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index e103932efd2f6..0cac16f5f4ad9 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -204,7 +204,7 @@ Broadcaster Role (Advertiser) Observer Role (Scanner) ----------------------- -.. method:: BLE.gap_scan(duration_ms, [interval_us], [window_us]) +.. method:: BLE.gap_scan(duration_ms, [interval_us], [window_us], [active]) Run a scan operation lasting for the specified duration (in **milli**\ seconds). @@ -228,6 +228,8 @@ Observer Role (Scanner) * 0x03 - ADV_NONCONN_IND - non-connectable undirected advertising * 0x04 - SCAN_RSP - scan response + ``active`` can be set ``True`` if you want to receive scan responses in the results. + When scanning is stopped (either due to the duration finishing or when explicitly stopped), the ``_IRQ_SCAN_DONE`` event will be raised. diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 89e396dfd08e7..8f0c82974c316 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -894,7 +894,7 @@ STATIC void scan_duration_timeout_handler(btstack_timer_source_t *ds) { mp_bluetooth_gap_scan_stop(); } -int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us) { +int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) { DEBUG_EVENT_printf("mp_bluetooth_gap_scan_start\n"); if (duration_ms > 0) { @@ -903,8 +903,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ btstack_run_loop_add_timer(&scan_duration_timeout); } - // 0 = passive scan (we don't handle scan response). - gap_set_scan_parameters(0, interval_us / 625, window_us / 625); + gap_set_scan_parameters(active_scan ? 1 : 0, interval_us / 625, window_us / 625); gap_start_scan(); return 0; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 730c288ee7885..82efe49385377 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -609,6 +609,7 @@ STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) { mp_int_t duration_ms = 0; mp_int_t interval_us = 1280000; mp_int_t window_us = 11250; + bool active_scan = false; if (n_args > 1) { if (args[1] == mp_const_none) { // scan(None) --> stop scan. @@ -619,12 +620,15 @@ STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) { interval_us = mp_obj_get_int(args[2]); if (n_args > 3) { window_us = mp_obj_get_int(args[3]); + if (n_args > 4) { + active_scan = mp_obj_is_true(args[4]); + } } } } - return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us)); + return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us, active_scan)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 4, bluetooth_ble_gap_scan); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 5, bluetooth_ble_gap_scan); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in) { diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index cdb86e5e63c44..2c07683124232 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -215,7 +215,7 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Start a discovery (scan). Set duration to zero to run continuously. -int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us); +int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan); // Stop discovery (if currently active). int mp_bluetooth_gap_scan_stop(void); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 843989b2b8986..e51f2747ec5c8 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -661,7 +661,7 @@ STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) { return 0; } -int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us) { +int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } @@ -673,7 +673,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ .window = MAX(BLE_HCI_SCAN_WINDOW_MIN, MIN(BLE_HCI_SCAN_WINDOW_MAX, window_us / BLE_HCI_SCAN_ITVL)), .filter_policy = BLE_HCI_CONN_FILT_NO_WL, .limited = 0, - .passive = 1, // TODO: Handle BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP in gap_scan_cb above. + .passive = active_scan ? 0 : 1, .filter_duplicates = 0, }; int err = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, duration_ms, &discover_params, gap_scan_cb, NULL); From 5fb276de33c238d0a56696c701b8274d070684d8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 26 Aug 2020 16:02:39 +1000 Subject: [PATCH 0219/3533] tests/extmod: Make uasyncio_fair test more reliable by adjusting sleeps. With sleep(0.2) a multiple of sleep(0.1), the order of task 2 and 3 execution is not well defined, and depends on the precision of the system clock and how fast the rest of the code runs. So change 0.2 to 0.18 to make the test more reliable. Also fix a typo of t3/t4, and cancel t4 at the end. Signed-off-by: Damien George --- tests/extmod/uasyncio_fair.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/extmod/uasyncio_fair.py b/tests/extmod/uasyncio_fair.py index e0ee811a9b046..e2257060548bb 100644 --- a/tests/extmod/uasyncio_fair.py +++ b/tests/extmod/uasyncio_fair.py @@ -21,12 +21,13 @@ async def task(id, t): async def main(): t1 = asyncio.create_task(task(1, -0.01)) t2 = asyncio.create_task(task(2, 0.1)) - t3 = asyncio.create_task(task(3, 0.2)) - t3 = asyncio.create_task(task(4, -100)) + t3 = asyncio.create_task(task(3, 0.18)) + t4 = asyncio.create_task(task(4, -100)) await asyncio.sleep(0.5) t1.cancel() t2.cancel() t3.cancel() + t4.cancel() print("finish") From 91c5d168c061ec45a548876a76a5540316d02594 Mon Sep 17 00:00:00 2001 From: Roberto Colistete Jr Date: Sat, 15 Aug 2020 02:35:02 -0300 Subject: [PATCH 0220/3533] nrf/Makefile: Improve user C modules support. Add CFLAGS_EXTRA to CFLAGS. Include LDFLAGS_MOD to the compilation. And, add SRC_MOD to SRC_QSTR. --- ports/nrf/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 8f73336e0b1e8..e5a1b471a35d7 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -113,7 +113,7 @@ endif CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) -CFLAGS += $(INC) -Wall -Werror -g -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) +CFLAGS += $(INC) -Wall -Werror -g -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -fno-strict-aliasing CFLAGS += -Iboards/$(BOARD) CFLAGS += -DNRF5_HAL_H='<$(MCU_VARIANT)_hal.h>' @@ -445,11 +445,11 @@ flash: deploy $(BUILD)/$(OUTPUT_FILENAME).elf: $(OBJ) $(ECHO) "LINK $@" - $(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIBS) + $(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LDFLAGS_MOD) $(LIBS) $(Q)$(SIZE) $@ # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_LIB) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) +SRC_QSTR += $(SRC_C) $(SRC_LIB) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) $(SRC_MOD) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR From 405893afc6654ed2ace9a6475bfd2095133e614a Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 8 Apr 2020 08:01:58 +0200 Subject: [PATCH 0221/3533] tests/run-tests: Use absolute paths where possible. Replace some usages of paths relative to the current working directory with absolute paths relative to the tests directory. Fixes and resulting changes: - default values of MICROPYTHON and MPYCROSS are absolute paths and always correct - likewise, the correct full paths for tools and extmod directories are appended to sys.path - printing/cleaning failures works properly since it expects the .exp and .out files in the tests directory which is also where they are written to now, plus no more need for changing directories This fixes #5872 and allows running custom tests which use run-tests without having to cd to the tests directory first, and the test output still is in the tests/ directory instead of the current working directory. Discovery of tests and all skip test logic based on paths relative to the current working directory remains unchanged which essentially means that for running most of MicroPython's own tests, run-tests must still be ran from within it's directory, so document that. --- tests/run-tests | 54 ++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/tests/run-tests b/tests/run-tests index 102b0f7790e14..c2831614a3599 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -5,21 +5,29 @@ import subprocess import sys import platform import argparse +import inspect import re from glob import glob +# See stackoverflow.com/questions/2632199: __file__ nor sys.argv[0] +# are guaranteed to always work, this one should though. +BASEPATH = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda: None))) + +def base_path(*p): + return os.path.abspath(os.path.join(BASEPATH, *p)).replace('\\', '/') + # Tests require at least CPython 3.3. If your default python3 executable # is of lower version, you can point MICROPY_CPYTHON3 environment var # to the correct executable. if os.name == 'nt': CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3.exe') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/windows/micropython.exe') + MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/windows/micropython.exe')) else: CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/unix/micropython') + MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/unix/micropython')) # mpy-cross is only needed if --via-mpy command-line arg is passed -MPYCROSS = os.getenv('MICROPY_MPYCROSS', '../mpy-cross/mpy-cross') +MPYCROSS = os.getenv('MICROPY_MPYCROSS', base_path('../mpy-cross/mpy-cross')) # For diff'ing test output DIFF = os.getenv('MICROPY_DIFF', 'diff -u') @@ -61,7 +69,7 @@ def run_micropython(pyb, args, test_file, is_special=False): had_crash = False if pyb is None: # run on PC - if test_file.startswith(('cmdline/', 'feature_check/')) or test_file in special_tests: + if test_file.startswith(('cmdline/', base_path('feature_check/'))) or test_file in special_tests: # special handling for tests of the unix cmdline program is_special = True @@ -215,10 +223,10 @@ def run_feature_check(pyb, args, base_path, test_file): if pyb is not None and test_file.startswith("repl_"): # REPL feature tests will not run via pyboard because they require prompt interactivity return b"" - return run_micropython(pyb, args, base_path + "/feature_check/" + test_file, is_special=True) + return run_micropython(pyb, args, base_path("feature_check", test_file), is_special=True) -def run_tests(pyb, tests, args, base_path="."): +def run_tests(pyb, tests, args): test_count = 0 testcase_count = 0 passed_count = 0 @@ -244,6 +252,10 @@ def run_tests(pyb, tests, args, base_path="."): # If we're asked to --list-tests, we can't assume that there's a # connection to target, so we can't run feature checks usefully. if not (args.list_tests or args.write_exp): + # Even if we run completely different tests in a different directory, + # we need to access feature_checks from the same directory as the + # run-tests script itself so use base_path. + # Check if micropython.native is supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, 'native_check.py') if output == b'CRASH': @@ -307,7 +319,7 @@ def run_tests(pyb, tests, args, base_path="."): upy_float_precision = int(upy_float_precision) has_complex = run_feature_check(pyb, args, base_path, 'complex.py') == b'complex\n' has_coverage = run_feature_check(pyb, args, base_path, 'coverage.py') == b'coverage\n' - cpy_byteorder = subprocess.check_output([CPYTHON3, base_path + '/feature_check/byteorder.py']) + cpy_byteorder = subprocess.check_output([CPYTHON3, base_path('feature_check/byteorder.py')]) skip_endian = (upy_byteorder != cpy_byteorder) # These tests don't test slice explicitly but rather use it to perform the test @@ -509,8 +521,8 @@ def run_tests(pyb, tests, args, base_path="."): testcase_count += len(output_expected.splitlines()) - filename_expected = test_basename + ".exp" - filename_mupy = test_basename + ".out" + filename_expected = base_path(test_basename + ".exp") + filename_mupy = base_path(test_basename + ".out") if output_expected == output_mupy: print("pass ", test_file) @@ -563,6 +575,10 @@ def main(): formatter_class=argparse.RawDescriptionHelpFormatter, description='''Run and manage tests for MicroPython. +Tests are discovered by scanning test directories for .py files or using the +specified test files. If test files nor directories are specified, the script +expects to be ran in the tests directory (where this file is located) and the +builtin tests suitable for the target platform are ran. When running tests, run-tests compares the MicroPython output of the test with the output produced by running the test through CPython unless a .exp file is found, in which case it is used as comparison. @@ -598,10 +614,8 @@ the last matching regex is used: args = cmd_parser.parse_args() if args.print_failures: - os.chdir(os.path.abspath(os.path.dirname(__file__))) - - for exp in glob("*.exp"): - testbase = os.path.basename(exp)[:-4] + for exp in glob(base_path("*.exp")): + testbase = exp[:-4] print() print("FAILURE {0}".format(testbase)) os.system("{0} {1}.exp {1}.out".format(DIFF, testbase)) @@ -609,9 +623,7 @@ the last matching regex is used: sys.exit(0) if args.clean_failures: - os.chdir(os.path.abspath(os.path.dirname(__file__))) - - for f in glob("*.exp") + glob("*.out"): + for f in glob(base_path("*.exp")) + glob(base_path("*.out")): os.remove(f) sys.exit(0) @@ -622,7 +634,7 @@ the last matching regex is used: pyb = None elif args.target in EXTERNAL_TARGETS: global pyboard - sys.path.append('../tools') + sys.path.append(base_path('../tools')) import pyboard pyb = pyboard.Pyboard(args.device, args.baudrate, args.user, args.password) pyb.enter_raw_repl() @@ -659,14 +671,10 @@ the last matching regex is used: if not args.keep_path: # clear search path to make sure tests use only builtin modules and those in extmod - os.environ['MICROPYPATH'] = os.pathsep + '../extmod' + os.environ['MICROPYPATH'] = os.pathsep + base_path('../extmod') - # Even if we run completely different tests in a different directory, - # we need to access feature_check's from the same directory as the - # run-tests script itself. - base_path = os.path.dirname(sys.argv[0]) or "." try: - res = run_tests(pyb, tests, args, base_path) + res = run_tests(pyb, tests, args) finally: if pyb: pyb.close() From 0c3f9d58a5dcf0480460548a416f27d9c8350f3c Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 7 Apr 2020 16:19:49 +0200 Subject: [PATCH 0222/3533] tests/run-tests: Make test output directory configurable. A configurable result directory is advantageous because it enables using a dedicated location, eventually outside of the source tree, instead of forcing the output files into a fixed directory which might also contain other files already. For that reason the default output directory also has been changed to tests/results/. --- .gitignore | 3 +-- tests/run-tests | 16 +++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 50bd30e877e87..c52f59eec74e6 100644 --- a/.gitignore +++ b/.gitignore @@ -27,8 +27,7 @@ build-*/ # Test failure outputs ###################### -tests/*.exp -tests/*.out +tests/results/* # Python cache files ###################### diff --git a/tests/run-tests b/tests/run-tests index c2831614a3599..49811d9b78845 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -226,7 +226,7 @@ def run_feature_check(pyb, args, base_path, test_file): return run_micropython(pyb, args, base_path("feature_check", test_file), is_special=True) -def run_tests(pyb, tests, args): +def run_tests(pyb, tests, args, result_dir): test_count = 0 testcase_count = 0 passed_count = 0 @@ -521,8 +521,8 @@ def run_tests(pyb, tests, args): testcase_count += len(output_expected.splitlines()) - filename_expected = base_path(test_basename + ".exp") - filename_mupy = base_path(test_basename + ".out") + filename_expected = os.path.join(result_dir, test_basename + ".exp") + filename_mupy = os.path.join(result_dir, test_basename + ".out") if output_expected == output_mupy: print("pass ", test_file) @@ -582,7 +582,7 @@ builtin tests suitable for the target platform are ran. When running tests, run-tests compares the MicroPython output of the test with the output produced by running the test through CPython unless a .exp file is found, in which case it is used as comparison. -If a test fails, run-tests produces a pair of .out and .exp files in the current +If a test fails, run-tests produces a pair of .out and .exp files in the result directory with the MicroPython output and the expectations, respectively. ''', epilog='''\ @@ -599,6 +599,7 @@ the last matching regex is used: cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username') cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password') cmd_parser.add_argument('-d', '--test-dirs', nargs='*', help='input test directories (if no files given)') + cmd_parser.add_argument('-r', '--result-dir', default=base_path('results'), help='directory for test results') cmd_parser.add_argument('-e', '--exclude', action=append_filter, metavar='REGEX', dest='filters', help='exclude test by regex on path/name.py') cmd_parser.add_argument('-i', '--include', action=append_filter, metavar='REGEX', dest='filters', help='include test by regex on path/name.py') cmd_parser.add_argument('--write-exp', action='store_true', help='use CPython to generate .exp files to run tests w/o CPython') @@ -614,7 +615,7 @@ the last matching regex is used: args = cmd_parser.parse_args() if args.print_failures: - for exp in glob(base_path("*.exp")): + for exp in glob(os.path.join(args.result_dir, "*.exp")): testbase = exp[:-4] print() print("FAILURE {0}".format(testbase)) @@ -623,7 +624,7 @@ the last matching regex is used: sys.exit(0) if args.clean_failures: - for f in glob(base_path("*.exp")) + glob(base_path("*.out")): + for f in glob(os.path.join(args.result_dir, "*.exp")) + glob(os.path.join(args.result_dir, "*.out")): os.remove(f) sys.exit(0) @@ -674,7 +675,8 @@ the last matching regex is used: os.environ['MICROPYPATH'] = os.pathsep + base_path('../extmod') try: - res = run_tests(pyb, tests, args) + os.makedirs(args.result_dir, exist_ok=True) + res = run_tests(pyb, tests, args, args.result_dir) finally: if pyb: pyb.close() From cef678b2dbab528a54bc36bd6c3be84a38857629 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sun, 23 Aug 2020 15:16:00 +0200 Subject: [PATCH 0223/3533] extmod/machine_i2c: Fix buffer overrun if 'addrsize' is bigger than 32. The memory operation functions read_mem() and write_mem() create a temporary buffer on the local C stack for the address bytes with the size of 4 bytes. This buffer is filled in a loop from the user supplied address and address length. If the user supplied 'addrsize' is bigger than 32, the local buffer is overrun. Fix this by raising an exception for invalid 'addrsize' values. Signed-off-by: Michael Buesch --- extmod/machine_i2c.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 14cba962365e1..c804cf570385d 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -526,13 +526,24 @@ STATIC mp_obj_t machine_i2c_writevto(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_writevto_obj, 3, 4, machine_i2c_writevto); -STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, uint8_t *buf, size_t len) { - mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); - uint8_t memaddr_buf[4]; +STATIC size_t fill_memaddr_buf(uint8_t *memaddr_buf, uint32_t memaddr, uint8_t addrsize) { size_t memaddr_len = 0; + if ((addrsize & 7) != 0 || addrsize > 32) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid addrsize")); + } for (int16_t i = addrsize - 8; i >= 0; i -= 8) { memaddr_buf[memaddr_len++] = memaddr >> i; } + return memaddr_len; +} + +STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, uint8_t *buf, size_t len) { + mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); + + // Create buffer with memory address + uint8_t memaddr_buf[4]; + size_t memaddr_len = fill_memaddr_buf(&memaddr_buf[0], memaddr, addrsize); + int ret = mp_machine_i2c_writeto(self, addr, memaddr_buf, memaddr_len, false); if (ret != memaddr_len) { // must generate STOP @@ -546,11 +557,8 @@ STATIC int write_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); // Create buffer with memory address - size_t memaddr_len = 0; uint8_t memaddr_buf[4]; - for (int16_t i = addrsize - 8; i >= 0; i -= 8) { - memaddr_buf[memaddr_len++] = memaddr >> i; - } + size_t memaddr_len = fill_memaddr_buf(&memaddr_buf[0], memaddr, addrsize); // Create partial write buffers mp_machine_i2c_buf_t bufs[2] = { From a93a378e933abc294de6ae26fb86a421e858fbcf Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 28 Aug 2020 16:42:32 +1000 Subject: [PATCH 0224/3533] zephyr/README: Update required Zephyr version and mention new features. Signed-off-by: Damien George --- ports/zephyr/README.md | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/ports/zephyr/README.md b/ports/zephyr/README.md index 6bcbccd8b36a3..a2fb393d90d0f 100644 --- a/ports/zephyr/README.md +++ b/ports/zephyr/README.md @@ -1,10 +1,11 @@ MicroPython port to Zephyr RTOS =============================== -This is an work-in-progress port of MicroPython to Zephyr RTOS +This is a work-in-progress port of MicroPython to Zephyr RTOS (http://zephyrproject.org). -This port requires Zephyr version 1.8 or higher. All boards supported +This port requires Zephyr version 2.3.0, and may also work on higher +versions. All boards supported by Zephyr (with standard level of features support, like UART console) should work with MicroPython (but not all were tested). @@ -12,12 +13,14 @@ Features supported at this time: * REPL (interactive prompt) over Zephyr UART console. * `utime` module for time measurements and delays. -* `machine.Pin` class for GPIO control. +* `machine.Pin` class for GPIO control, with IRQ support. * `machine.I2C` class for I2C control. * `usocket` module for networking (IPv4/IPv6). * "Frozen modules" support to allow to bundle Python modules together with firmware. Including complete applications, including with run-on-boot capability. +* virtual filesystem with FAT and littlefs formats, backed by either + DiskAccess or FlashArea (flash map). Over time, bindings for various Zephyr subsystems may be added. @@ -28,17 +31,35 @@ Building Follow to Zephyr web site for Getting Started instruction of installing Zephyr SDK, getting Zephyr source code, and setting up development environment. (Direct link: -https://www.zephyrproject.org/doc/getting_started/getting_started.html). +https://docs.zephyrproject.org/latest/getting_started/index.html). You may want to build Zephyr's own sample applications to make sure your setup is correct. -To build MicroPython port, in the port subdirectory (zephyr/), run: +If you already have Zephyr installed but are having issues building the +MicroPython port then try installing the correct version of Zephyr via: - make BOARD= + $ west init zephyrproject -m https://github.com/zephyrproject-rtos/zephyr --mr v2.3.0 + +Alternatively, you don't have to redo the Zephyr installation to just +switch from master to a tagged release, you can instead do: + + $ cd zephyrproject/zephyr + $ git checkout v2.3.0 + $ west update + +With Zephyr installed you may then need to configure your environment, +for example by sourcing `zephyrproject/zephyr/zephyr-env.sh`. + +Once Zephyr is ready to use you can build the MicroPython port. +In the port subdirectory `ports/zephyr/` run: + + $ make BOARD= If you don't specify BOARD, the default is `qemu_x86` (x86 target running -in QEMU emulator). Consult Zephyr documentation above for the list of -supported boards. +in QEMU emulator). Consult the Zephyr documentation above for the list of +supported boards. Board configuration files appearing in `ports/zephyr/boards/` +correspond to boards that have been tested with MicroPython and may have +additional options enabled, like filesystem support. Running @@ -66,6 +87,10 @@ cf. for example QEMU networking requirements above; real hardware boards generally should not have any special requirements, unless there're known issues). +For example, to deploy firmware on the FRDM-K64F board run: + + $ make BOARD=frdm_k64f flash + Quick example ------------- @@ -89,6 +114,19 @@ reference materials). To execute the above sample, copy it to clipboard, in MicroPython REPL enter "paste mode" using Ctrl+E, paste clipboard, press Ctrl+D to finish paste mode and start execution. +To respond to Pin change IRQs, on a FRDM-K64F board run: + + from machine import Pin + + SW2 = Pin(("GPIO_2", 6), Pin.IN) + SW3 = Pin(("GPIO_0", 4), Pin.IN) + + SW2.irq(lambda t: print("SW2 changed")) + SW3.irq(lambda t: print("SW3 changed")) + + while True: + pass + Example of using I2C to scan for I2C slaves: from machine import I2C From 338b12d3c840ea56504c83d9c51e4d0f628684f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 Aug 2020 13:58:20 +1000 Subject: [PATCH 0225/3533] LICENSE,docs: Update copyright year range to include 2020. Signed-off-by: Damien George --- LICENSE | 2 +- docs/conf.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index e6a54cf269947..8c5e4fe4c359a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013-2019 Damien P. George +Copyright (c) 2013-2020 Damien P. George Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/docs/conf.py b/docs/conf.py index 36f99cd2c441b..7dc039d6ca599 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -66,7 +66,7 @@ # General information about the project. project = 'MicroPython' -copyright = '2014-2019, Damien P. George, Paul Sokolovsky, and contributors' +copyright = '2014-2020, Damien P. George, Paul Sokolovsky, and contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the From 40d174ac7d1cc5c548756bb75b4f36b9b450b8bf Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 Aug 2020 14:00:24 +1000 Subject: [PATCH 0226/3533] stm32/powerctrl.h: Include stdbool.h to get definition of bool. Signed-off-by: Damien George --- ports/stm32/powerctrl.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/stm32/powerctrl.h b/ports/stm32/powerctrl.h index 6e5f899a4cd1b..9f223e794a984 100644 --- a/ports/stm32/powerctrl.h +++ b/ports/stm32/powerctrl.h @@ -26,6 +26,7 @@ #ifndef MICROPY_INCLUDED_STM32_POWERCTRL_H #define MICROPY_INCLUDED_STM32_POWERCTRL_H +#include #include void SystemClock_Config(void); From 0c7354afaf91da3dac2c5ec471603c9e7acc8eac Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 Aug 2020 14:04:59 +1000 Subject: [PATCH 0227/3533] tests: Split out complex reverse-op tests to separate test file. So they can be skipped if __rOP__'s are not supported on the target. Also fix the typo in the complex_special_methods.py filename. Signed-off-by: Damien George --- tests/float/complex_reverse_op.py | 10 ++++++++++ ...ex_special_mehods.py => complex_special_methods.py} | 5 ----- tests/run-tests | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 tests/float/complex_reverse_op.py rename tests/float/{complex_special_mehods.py => complex_special_methods.py} (62%) diff --git a/tests/float/complex_reverse_op.py b/tests/float/complex_reverse_op.py new file mode 100644 index 0000000000000..a7351949d0cea --- /dev/null +++ b/tests/float/complex_reverse_op.py @@ -0,0 +1,10 @@ +# test complex interacting with special reverse methods + + +class A: + def __radd__(self, x): + print("__radd__") + return 2 + + +print(1j + A()) diff --git a/tests/float/complex_special_mehods.py b/tests/float/complex_special_methods.py similarity index 62% rename from tests/float/complex_special_mehods.py rename to tests/float/complex_special_methods.py index 6789013fa6e0d..7e54905e495ad 100644 --- a/tests/float/complex_special_mehods.py +++ b/tests/float/complex_special_methods.py @@ -6,10 +6,5 @@ def __add__(self, x): print("__add__") return 1 - def __radd__(self, x): - print("__radd__") - return 2 - print(A() + 1j) -print(1j + A()) diff --git a/tests/run-tests b/tests/run-tests index 49811d9b78845..a7b88ecdd3265 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -367,7 +367,7 @@ def run_tests(pyb, tests, args, result_dir): if not has_complex: skip_tests.add('float/complex1.py') skip_tests.add('float/complex1_intbig.py') - skip_tests.add('float/complex_special_mehods.py') + skip_tests.add('float/complex_special_methods.py') skip_tests.add('float/int_big_float.py') skip_tests.add('float/true_value.py') skip_tests.add('float/types.py') @@ -476,7 +476,7 @@ def run_tests(pyb, tests, args, result_dir): skip_it |= skip_slice and is_slice skip_it |= skip_async and is_async skip_it |= skip_const and is_const - skip_it |= skip_revops and test_name.startswith("class_reverse_op") + skip_it |= skip_revops and "reverse_op" in test_name skip_it |= skip_io_module and is_io_module if args.list_tests: From 06659077a81b85882254cf0953c33b27614e018e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 Aug 2020 15:14:29 +1000 Subject: [PATCH 0228/3533] all: Update Python code to conform to latest black formatting. Updating to Black v20.8b1 there are two changes that affect the code in this repository: - If there is a trailing comma in a list (eg [], () or function call) then that list is now written out with one line per element. So remove such trailing commas where the list should stay on one line. - Spaces at the start of """ doc strings are removed. Signed-off-by: Damien George --- examples/bluetooth/ble_simple_central.py | 6 +++--- examples/bluetooth/ble_simple_peripheral.py | 12 +++++------- examples/bluetooth/ble_temperature.py | 6 +++--- examples/bluetooth/ble_temperature_central.py | 6 +++--- examples/bluetooth/ble_uart_peripheral.py | 12 +++++------- extmod/webrepl/manifest.py | 2 +- ports/cc3200/boards/make-pins.py | 5 ++++- ports/nrf/boards/make-pins.py | 5 ++++- ports/stm32/boards/make-pins.py | 5 ++++- ports/teensy/make-pins.py | 5 ++++- py/makemoduledefs.py | 6 +++--- tests/extmod/uasyncio_gather.py | 2 +- tests/extmod/uctypes_le.py | 2 +- tests/extmod/uctypes_native_le.py | 2 +- tests/extmod/uctypes_sizeof_native.py | 2 +- tests/float/cmath_fun.py | 2 +- tests/float/math_fun.py | 8 ++++---- tests/float/math_fun_special.py | 2 +- tools/mpy_ld.py | 2 +- 19 files changed, 50 insertions(+), 42 deletions(-) diff --git a/examples/bluetooth/ble_simple_central.py b/examples/bluetooth/ble_simple_central.py index f6996366e3da1..f0a3297d322e1 100644 --- a/examples/bluetooth/ble_simple_central.py +++ b/examples/bluetooth/ble_simple_central.py @@ -74,7 +74,7 @@ def _reset(self): def _irq(self, event, data): if event == _IRQ_SCAN_RESULT: addr_type, addr, adv_type, rssi, adv_data = data - if adv_type in (_ADV_IND, _ADV_DIRECT_IND,) and _UART_SERVICE_UUID in decode_services( + if adv_type in (_ADV_IND, _ADV_DIRECT_IND) and _UART_SERVICE_UUID in decode_services( adv_data ): # Found a potential device, remember it and stop scanning. @@ -97,14 +97,14 @@ def _irq(self, event, data): elif event == _IRQ_PERIPHERAL_CONNECT: # Connect successful. - conn_handle, addr_type, addr, = data + conn_handle, addr_type, addr = data if addr_type == self._addr_type and addr == self._addr: self._conn_handle = conn_handle self._ble.gattc_discover_services(self._conn_handle) elif event == _IRQ_PERIPHERAL_DISCONNECT: # Disconnect (either initiated by us or the remote end). - conn_handle, _, _, = data + conn_handle, _, _ = data if conn_handle == self._conn_handle: # If it was initiated by us, it'll already be reset. self._reset() diff --git a/examples/bluetooth/ble_simple_peripheral.py b/examples/bluetooth/ble_simple_peripheral.py index 08cd7fa98d69c..f5e7661926ba8 100644 --- a/examples/bluetooth/ble_simple_peripheral.py +++ b/examples/bluetooth/ble_simple_peripheral.py @@ -23,7 +23,7 @@ ) _UART_SERVICE = ( _UART_UUID, - (_UART_TX, _UART_RX,), + (_UART_TX, _UART_RX), ) @@ -32,22 +32,20 @@ def __init__(self, ble, name="mpy-uart"): self._ble = ble self._ble.active(True) self._ble.irq(handler=self._irq) - ((self._handle_tx, self._handle_rx,),) = self._ble.gatts_register_services( - (_UART_SERVICE,) - ) + ((self._handle_tx, self._handle_rx),) = self._ble.gatts_register_services((_UART_SERVICE,)) self._connections = set() self._write_callback = None - self._payload = advertising_payload(name=name, services=[_UART_UUID],) + self._payload = advertising_payload(name=name, services=[_UART_UUID]) self._advertise() def _irq(self, event, data): # Track connections so we can send notifications. if event == _IRQ_CENTRAL_CONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data print("New connection", conn_handle) self._connections.add(conn_handle) elif event == _IRQ_CENTRAL_DISCONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data print("Disconnected", conn_handle) self._connections.remove(conn_handle) # Start advertising again to allow a new connection. diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py index 0e2da2239d50a..001a26b114a0a 100644 --- a/examples/bluetooth/ble_temperature.py +++ b/examples/bluetooth/ble_temperature.py @@ -46,15 +46,15 @@ def __init__(self, ble, name="mpy-temp"): def _irq(self, event, data): # Track connections so we can send notifications. if event == _IRQ_CENTRAL_CONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data self._connections.add(conn_handle) elif event == _IRQ_CENTRAL_DISCONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data self._connections.remove(conn_handle) # Start advertising again to allow a new connection. self._advertise() elif event == _IRQ_GATTS_INDICATE_DONE: - conn_handle, value_handle, status, = data + conn_handle, value_handle, status = data def set_temperature(self, temp_deg_c, notify=False, indicate=False): # Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius. diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index f9db225c16942..79830341789d4 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -87,7 +87,7 @@ def _reset(self): def _irq(self, event, data): if event == _IRQ_SCAN_RESULT: addr_type, addr, adv_type, rssi, adv_data = data - if adv_type in (_ADV_IND, _ADV_DIRECT_IND,) and _ENV_SENSE_UUID in decode_services( + if adv_type in (_ADV_IND, _ADV_DIRECT_IND) and _ENV_SENSE_UUID in decode_services( adv_data ): # Found a potential device, remember it and stop scanning. @@ -110,14 +110,14 @@ def _irq(self, event, data): elif event == _IRQ_PERIPHERAL_CONNECT: # Connect successful. - conn_handle, addr_type, addr, = data + conn_handle, addr_type, addr = data if addr_type == self._addr_type and addr == self._addr: self._conn_handle = conn_handle self._ble.gattc_discover_services(self._conn_handle) elif event == _IRQ_PERIPHERAL_DISCONNECT: # Disconnect (either initiated by us or the remote end). - conn_handle, _, _, = data + conn_handle, _, _ = data if conn_handle == self._conn_handle: # If it was initiated by us, it'll already be reset. self._reset() diff --git a/examples/bluetooth/ble_uart_peripheral.py b/examples/bluetooth/ble_uart_peripheral.py index cc8d589b0e35f..59b35f7e6ad5e 100644 --- a/examples/bluetooth/ble_uart_peripheral.py +++ b/examples/bluetooth/ble_uart_peripheral.py @@ -20,7 +20,7 @@ ) _UART_SERVICE = ( _UART_UUID, - (_UART_TX, _UART_RX,), + (_UART_TX, _UART_RX), ) # org.bluetooth.characteristic.gap.appearance.xml @@ -32,9 +32,7 @@ def __init__(self, ble, name="mpy-uart", rxbuf=100): self._ble = ble self._ble.active(True) self._ble.irq(handler=self._irq) - ((self._tx_handle, self._rx_handle,),) = self._ble.gatts_register_services( - (_UART_SERVICE,) - ) + ((self._tx_handle, self._rx_handle),) = self._ble.gatts_register_services((_UART_SERVICE,)) # Increase the size of the rx buffer and enable append mode. self._ble.gatts_set_buffer(self._rx_handle, rxbuf, True) self._connections = set() @@ -50,16 +48,16 @@ def irq(self, handler): def _irq(self, event, data): # Track connections so we can send notifications. if event == _IRQ_CENTRAL_CONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data self._connections.add(conn_handle) elif event == _IRQ_CENTRAL_DISCONNECT: - conn_handle, _, _, = data + conn_handle, _, _ = data if conn_handle in self._connections: self._connections.remove(conn_handle) # Start advertising again to allow a new connection. self._advertise() elif event == _IRQ_GATTS_WRITE: - conn_handle, value_handle, = data + conn_handle, value_handle = data if conn_handle in self._connections and value_handle == self._rx_handle: self._rx_buffer += self._ble.gatts_read(self._rx_handle) if self._handler: diff --git a/extmod/webrepl/manifest.py b/extmod/webrepl/manifest.py index 6ce7d85466c06..6eceb3eeb6c56 100644 --- a/extmod/webrepl/manifest.py +++ b/extmod/webrepl/manifest.py @@ -1 +1 @@ -freeze(".", ("webrepl.py", "webrepl_setup.py", "websocket_helper.py",)) +freeze(".", ("webrepl.py", "webrepl_setup.py", "websocket_helper.py")) diff --git a/ports/cc3200/boards/make-pins.py b/ports/cc3200/boards/make-pins.py index a204561cfbc32..0cf0d565606f2 100644 --- a/ports/cc3200/boards/make-pins.py +++ b/ports/cc3200/boards/make-pins.py @@ -216,7 +216,10 @@ def main(): default="cc3200_af.csv", ) parser.add_argument( - "-b", "--board", dest="board_filename", help="Specifies the board file", + "-b", + "--board", + dest="board_filename", + help="Specifies the board file", ) parser.add_argument( "-p", diff --git a/ports/nrf/boards/make-pins.py b/ports/nrf/boards/make-pins.py index 56c56ae5f42f3..347ed15b21b31 100644 --- a/ports/nrf/boards/make-pins.py +++ b/ports/nrf/boards/make-pins.py @@ -381,7 +381,10 @@ def main(): default="build/pins_af.py", ) parser.add_argument( - "-b", "--board", dest="board_filename", help="Specifies the board file", + "-b", + "--board", + dest="board_filename", + help="Specifies the board file", ) parser.add_argument( "-p", diff --git a/ports/stm32/boards/make-pins.py b/ports/stm32/boards/make-pins.py index 692387f404ea5..a91ed8a2c9150 100755 --- a/ports/stm32/boards/make-pins.py +++ b/ports/stm32/boards/make-pins.py @@ -515,7 +515,10 @@ def main(): action="store_true", ) parser.add_argument( - "-b", "--board", dest="board_filename", help="Specifies the board file", + "-b", + "--board", + dest="board_filename", + help="Specifies the board file", ) parser.add_argument( "-p", diff --git a/ports/teensy/make-pins.py b/ports/teensy/make-pins.py index ddefae8521c50..4e46a8c244969 100755 --- a/ports/teensy/make-pins.py +++ b/ports/teensy/make-pins.py @@ -370,7 +370,10 @@ def main(): default="build/pins_af.py", ) parser.add_argument( - "-b", "--board", dest="board_filename", help="Specifies the board file", + "-b", + "--board", + dest="board_filename", + help="Specifies the board file", ) parser.add_argument( "-p", diff --git a/py/makemoduledefs.py b/py/makemoduledefs.py index d718da6e02513..612f3d29a3660 100644 --- a/py/makemoduledefs.py +++ b/py/makemoduledefs.py @@ -17,7 +17,7 @@ def find_c_file(obj_file, vpath): - """ Search vpaths for the c file that matches the provided object_file. + """Search vpaths for the c file that matches the provided object_file. :param str obj_file: object file to find the matching c file for :param List[str] vpath: List of base paths, similar to gcc vpath @@ -36,7 +36,7 @@ def find_c_file(obj_file, vpath): def find_module_registrations(c_file): - """ Find any MP_REGISTER_MODULE definitions in the provided c file. + """Find any MP_REGISTER_MODULE definitions in the provided c file. :param str c_file: path to c file to check :return: List[(module_name, obj_module, enabled_define)] @@ -52,7 +52,7 @@ def find_module_registrations(c_file): def generate_module_table_header(modules): - """ Generate header with module table entries for builtin modules. + """Generate header with module table entries for builtin modules. :param List[(module_name, obj_module, enabled_define)] modules: module defs :return: None diff --git a/tests/extmod/uasyncio_gather.py b/tests/extmod/uasyncio_gather.py index 2697a6278b08c..0e2948b07caa4 100644 --- a/tests/extmod/uasyncio_gather.py +++ b/tests/extmod/uasyncio_gather.py @@ -34,7 +34,7 @@ async def gather_task(): async def main(): # Simple gather with return values - print(await asyncio.gather(factorial("A", 2), factorial("B", 3), factorial("C", 4),)) + print(await asyncio.gather(factorial("A", 2), factorial("B", 3), factorial("C", 4))) # Cancel a multi gather # TODO doesn't work, Task should not forward cancellation from gather to sub-task diff --git a/tests/extmod/uctypes_le.py b/tests/extmod/uctypes_le.py index 23f2af982a1a9..f69da30b61ea7 100644 --- a/tests/extmod/uctypes_le.py +++ b/tests/extmod/uctypes_le.py @@ -6,7 +6,7 @@ desc = { "s0": uctypes.UINT16 | 0, - "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}), + "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1}), "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN, diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py index c161539c624f8..7958e5c22ada9 100644 --- a/tests/extmod/uctypes_native_le.py +++ b/tests/extmod/uctypes_native_le.py @@ -16,7 +16,7 @@ desc = { "s0": uctypes.UINT16 | 0, - "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}), + "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1}), "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN, diff --git a/tests/extmod/uctypes_sizeof_native.py b/tests/extmod/uctypes_sizeof_native.py index 352c191e0d8a6..157e554b09d47 100644 --- a/tests/extmod/uctypes_sizeof_native.py +++ b/tests/extmod/uctypes_sizeof_native.py @@ -28,7 +28,7 @@ "b": uctypes.UINT32 | 4, "c": uctypes.UINT8 | 8, "d": uctypes.UINT32 | 0, - "sub": (4, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}), + "sub": (4, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1}), } assert uctypes.sizeof(S5) == 12 diff --git a/tests/float/cmath_fun.py b/tests/float/cmath_fun.py index 15b72e7a6203b..39011733b02b4 100644 --- a/tests/float/cmath_fun.py +++ b/tests/float/cmath_fun.py @@ -22,7 +22,7 @@ test_values_non_zero.append(complex(r, -i)) if r != 0.0 and i != 0.0: test_values_non_zero.append(complex(-r, -i)) -test_values = [complex(0.0, 0.0),] + test_values_non_zero +test_values = [complex(0.0, 0.0)] + test_values_non_zero print(test_values) functions = [ diff --git a/tests/float/math_fun.py b/tests/float/math_fun.py index 7b6bb86489b7d..0d443a4754521 100644 --- a/tests/float/math_fun.py +++ b/tests/float/math_fun.py @@ -62,10 +62,10 @@ copysign, [(23.0, 42.0), (-23.0, 42.0), (23.0, -42.0), (-23.0, -42.0), (1.0, 0.0), (1.0, -0.0)], ), - ("pow", pow, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0),)), - ("atan2", atan2, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0),)), - ("fmod", fmod, ((1.0, 1.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0),)), - ("ldexp", ldexp, ((1.0, 0), (0.0, 1), (2.0, 2), (3.0, -2), (-3.0, -4),)), + ("pow", pow, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))), + ("atan2", atan2, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))), + ("fmod", fmod, ((1.0, 1.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))), + ("ldexp", ldexp, ((1.0, 0), (0.0, 1), (2.0, 2), (3.0, -2), (-3.0, -4))), ( "log", log, diff --git a/tests/float/math_fun_special.py b/tests/float/math_fun_special.py index c101a7e5019db..614470c0f15dd 100644 --- a/tests/float/math_fun_special.py +++ b/tests/float/math_fun_special.py @@ -40,7 +40,7 @@ ("erf", erf, test_values), ("erfc", erfc, test_values), ("gamma", gamma, pos_test_values), - ("lgamma", lgamma, pos_test_values + [50.0, 100.0,]), + ("lgamma", lgamma, pos_test_values + [50.0, 100.0]), ] for function_name, function, test_vals in functions: diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index 32bc176cb71c4..774966a7f68a0 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -908,7 +908,7 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs): # MPY: header out.write_bytes( bytearray( - [ord("M"), MPY_VERSION, env.arch.mpy_feature, MP_SMALL_INT_BITS, QSTR_WINDOW_SIZE,] + [ord("M"), MPY_VERSION, env.arch.mpy_feature, MP_SMALL_INT_BITS, QSTR_WINDOW_SIZE] ) ) From d1995e50ebda074f8151609bb4c95d4c31072513 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 30 Aug 2020 13:20:51 +1000 Subject: [PATCH 0229/3533] extmod/modlwip: Fix error return for TCP recv when not connected. This commit fixes the cases when a TCP socket is in STATE_NEW, STATE_LISTENING or STATE_CONNECTING and recv() is called on it. It now raises ENOTCONN instead of a random error code due to it previously indexing beyond the start of error_lookup_table[]. Signed-off-by: Damien George --- extmod/modlwip.c | 7 +++++-- tests/extmod/usocket_tcp_basic.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/extmod/usocket_tcp_basic.py diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 216e81749d1c3..1d557a6a84b5a 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -757,8 +757,11 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_ return 0; } } else if (socket->state != STATE_CONNECTED) { - assert(socket->state < 0); - *_errno = error_lookup_table[-socket->state]; + if (socket->state >= STATE_NEW) { + *_errno = MP_ENOTCONN; + } else { + *_errno = error_lookup_table[-socket->state]; + } return -1; } } diff --git a/tests/extmod/usocket_tcp_basic.py b/tests/extmod/usocket_tcp_basic.py new file mode 100644 index 0000000000000..368dfe3c98f4a --- /dev/null +++ b/tests/extmod/usocket_tcp_basic.py @@ -0,0 +1,17 @@ +# Test basic, stand-alone TCP socket functionality + +try: + import usocket as socket, uerrno as errno +except ImportError: + try: + import socket, errno + except ImportError: + print("SKIP") + raise SystemExit + +# recv() on a fresh socket should raise ENOTCONN +s = socket.socket() +try: + s.recv(1) +except OSError as er: + print("ENOTCONN:", er.args[0] == errno.ENOTCONN) From 836bca9956d9f02b9c0f6f396dc76cbd1586de10 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 30 Aug 2020 13:48:26 +1000 Subject: [PATCH 0230/3533] unix/variants: Fix fast and freedos variants so they build again. This regressed in bd2fff66875ed5adab8ce163a7f2bdafbd0332f9 Signed-off-by: Damien George --- ports/unix/variants/fast/mpconfigvariant.h | 2 -- ports/unix/variants/fast/mpconfigvariant.mk | 2 +- ports/unix/variants/freedos/mpconfigvariant.h | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/ports/unix/variants/fast/mpconfigvariant.h b/ports/unix/variants/fast/mpconfigvariant.h index 6d73275fdebfc..8a531b056ab71 100644 --- a/ports/unix/variants/fast/mpconfigvariant.h +++ b/ports/unix/variants/fast/mpconfigvariant.h @@ -32,5 +32,3 @@ // 91 is a magic number proposed by @dpgeorge, which make pystone run ~ at tie // with CPython 3.4. #define MICROPY_MODULE_DICT_SIZE (91) - -#include "variants/DEV/mpconfigvariant.h" diff --git a/ports/unix/variants/fast/mpconfigvariant.mk b/ports/unix/variants/fast/mpconfigvariant.mk index d67f7c8f38388..595e5756457c9 100644 --- a/ports/unix/variants/fast/mpconfigvariant.mk +++ b/ports/unix/variants/fast/mpconfigvariant.mk @@ -1,6 +1,6 @@ # build synthetically fast interpreter for benchmarking -COPT += "-fno-crossjumping -O2" +COPT += -fno-crossjumping -O2 PROG = micropython-fast diff --git a/ports/unix/variants/freedos/mpconfigvariant.h b/ports/unix/variants/freedos/mpconfigvariant.h index 338b34492de7f..562c783ca35fb 100644 --- a/ports/unix/variants/freedos/mpconfigvariant.h +++ b/ports/unix/variants/freedos/mpconfigvariant.h @@ -36,5 +36,3 @@ #undef _DIRENT_HAVE_D_INO #define MICROPY_USE_INTERNAL_ERRNO (1) - -#include "variants/DEV/mpconfigvariant.h" From 40153b800a8324f6cf3e47dd71cafcf90c3c4718 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 00:49:19 +1000 Subject: [PATCH 0231/3533] esp32/mphalport: Fix mp_hal_time_ns offset. gettimeofday returns seconds since 2000/1/1 so needs to be adjusted to seconds since 1970/1/1 to give the correct return value of mp_hal_time_ns. Signed-off-by: Damien George --- ports/esp32/mphalport.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 5e526df6673cb..3a46edf1f75c1 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -45,6 +45,7 @@ #include "py/mpstate.h" #include "py/mphal.h" #include "extmod/misc.h" +#include "lib/timeutils/timeutils.h" #include "lib/utils/pyexec.h" #include "mphalport.h" @@ -199,7 +200,10 @@ void mp_hal_delay_us(uint32_t us) { uint64_t mp_hal_time_ns(void) { struct timeval tv; gettimeofday(&tv, NULL); - return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; + // gettimeofday returns seconds since 2000/1/1 + uint64_t ns = timeutils_seconds_since_2000_to_nanoseconds_since_1970(tv.tv_sec); + ns += (uint64_t)tv.tv_usec * 1000ULL; + return ns; } // Wake up the main task if it is sleeping From c70e59965977df97d1134f0dcadb9cd4ed58139f Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 14:25:20 +1000 Subject: [PATCH 0232/3533] extmod/vfs: Support larger integer range in VFS stat time fields. On ports like unix where the Epoch is 1970/1/1 and atime/mtime/ctime are in seconds since the Epoch, this value will overflow a small-int on 32-bit systems. So far this is only an issue on 32-bit unix builds that use the VFS layer (eg dev and coverage unix variants) but the fix (using mp_obj_new_int_from_uint instead of MP_OBJ_NEW_SMALL_INT) is there for all ports so as to not complicate the code, and because they will need the range one day. Also apply a similar fix to other fields in VfsPosix.stat because they may also be large. Signed-off-by: Damien George --- extmod/vfs_fat.c | 6 +++--- extmod/vfs_lfsx.c | 6 +++--- extmod/vfs_posix.c | 18 +++++++++--------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index e7af4057b7433..79b0fc119b2fc 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -326,9 +326,9 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size - t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime - t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime - t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime + t->items[7] = mp_obj_new_int_from_uint(seconds); // st_atime + t->items[8] = mp_obj_new_int_from_uint(seconds); // st_mtime + t->items[9] = mp_obj_new_int_from_uint(seconds); // st_ctime return MP_OBJ_FROM_PTR(t); } diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 511b741b0dec1..9f5d9ce6db7f7 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -377,9 +377,9 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) { t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid t->items[6] = mp_obj_new_int_from_uint(info.size); // st_size - t->items[7] = MP_OBJ_NEW_SMALL_INT(mtime); // st_atime - t->items[8] = MP_OBJ_NEW_SMALL_INT(mtime); // st_mtime - t->items[9] = MP_OBJ_NEW_SMALL_INT(mtime); // st_ctime + t->items[7] = mp_obj_new_int_from_uint(mtime); // st_atime + t->items[8] = mp_obj_new_int_from_uint(mtime); // st_mtime + t->items[9] = mp_obj_new_int_from_uint(mtime); // st_ctime return MP_OBJ_FROM_PTR(t); } diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index f14f56e81ec82..719afe28fe87f 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -295,15 +295,15 @@ STATIC mp_obj_t vfs_posix_stat(mp_obj_t self_in, mp_obj_t path_in) { MP_HAL_RETRY_SYSCALL(ret, stat(path, &sb), mp_raise_OSError(err)); mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode); - t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.st_ino); - t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.st_dev); - t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.st_nlink); - t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.st_uid); - t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.st_gid); - t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.st_size); - t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.st_atime); - t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.st_mtime); - t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime); + t->items[1] = mp_obj_new_int_from_uint(sb.st_ino); + t->items[2] = mp_obj_new_int_from_uint(sb.st_dev); + t->items[3] = mp_obj_new_int_from_uint(sb.st_nlink); + t->items[4] = mp_obj_new_int_from_uint(sb.st_uid); + t->items[5] = mp_obj_new_int_from_uint(sb.st_gid); + t->items[6] = mp_obj_new_int_from_uint(sb.st_size); + t->items[7] = mp_obj_new_int_from_uint(sb.st_atime); + t->items[8] = mp_obj_new_int_from_uint(sb.st_mtime); + t->items[9] = mp_obj_new_int_from_uint(sb.st_ctime); return MP_OBJ_FROM_PTR(t); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_stat_obj, vfs_posix_stat); From 0385b21597a3e5dc3d1a84cd37848fc2ac2a0e20 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 14:39:02 +1000 Subject: [PATCH 0233/3533] unix/modos: Support larger integer range in uos.stat fields. On 32-bit builds these stat fields will overflow a small-int, so use mp_obj_new_int_from_uint to construct the int object. Signed-off-by: Damien George --- ports/unix/modos.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/unix/modos.c b/ports/unix/modos.c index 82b1b11425679..5e719c5736c1c 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -58,15 +58,15 @@ STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode); - t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.st_ino); - t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.st_dev); - t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.st_nlink); - t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.st_uid); - t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.st_gid); + t->items[1] = mp_obj_new_int_from_uint(sb.st_ino); + t->items[2] = mp_obj_new_int_from_uint(sb.st_dev); + t->items[3] = mp_obj_new_int_from_uint(sb.st_nlink); + t->items[4] = mp_obj_new_int_from_uint(sb.st_uid); + t->items[5] = mp_obj_new_int_from_uint(sb.st_gid); t->items[6] = mp_obj_new_int_from_uint(sb.st_size); - t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.st_atime); - t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.st_mtime); - t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime); + t->items[7] = mp_obj_new_int_from_uint(sb.st_atime); + t->items[8] = mp_obj_new_int_from_uint(sb.st_mtime); + t->items[9] = mp_obj_new_int_from_uint(sb.st_ctime); return MP_OBJ_FROM_PTR(t); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat); From 2a72e90ab8969b69304d84aa7070720d63c60d06 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 14:55:54 +1000 Subject: [PATCH 0234/3533] extmod/vfs: Add option to use 1970 as Epoch. By setting MICROPY_EPOCH_IS_1970 a port can opt to use 1970/1/1 as the Epoch for timestamps returned by stat(). And this setting is enabled on the unix and windows ports because that's what they use. Signed-off-by: Damien George --- extmod/vfs_fat.c | 3 +++ extmod/vfs_lfsx.c | 3 +++ ports/unix/mpconfigport.h | 3 +++ ports/windows/mpconfigport.h | 3 +++ 4 files changed, 12 insertions(+) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 79b0fc119b2fc..ace5ba5b6547f 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -319,6 +319,9 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { (fno.ftime >> 5) & 0x3f, 2 * (fno.ftime & 0x1f) ); + #if MICROPY_EPOCH_IS_1970 + seconds += TIMEUTILS_SECONDS_1970_TO_2000; + #endif t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 9f5d9ce6db7f7..d00df53104f6f 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -366,6 +366,9 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) { ns = ns << 8 | mtime_buf[i - 1]; } mtime = timeutils_seconds_since_2000_from_nanoseconds_since_1970(ns); + #if MICROPY_EPOCH_IS_1970 + mtime += TIMEUTILS_SECONDS_1970_TO_2000; + #endif } #endif diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index f3c61c18f100d..08605842fd5bd 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -176,6 +176,9 @@ #define MICROPY_ERROR_PRINTER (&mp_stderr_print) #define MICROPY_PY_STR_BYTES_CMP_WARN (1) +// VFS stat functions should return time values relative to 1970/1/1 +#define MICROPY_EPOCH_IS_1970 (1) + extern const struct _mp_print_t mp_stderr_print; #if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__)) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index fa09dda7527e0..30389c700eac3 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -123,6 +123,9 @@ #define MICROPY_WARNINGS (1) #define MICROPY_PY_STR_BYTES_CMP_WARN (1) +// VFS stat functions should return time values relative to 1970/1/1 +#define MICROPY_EPOCH_IS_1970 (1) + extern const struct _mp_print_t mp_stderr_print; #ifdef _MSC_VER From a909c215879d94e7e76046f71f38e881949fe7dc Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 14:59:10 +1000 Subject: [PATCH 0235/3533] unix/fatfs_port: Fix month offset in timestamp calculation. Signed-off-by: Damien George --- ports/unix/fatfs_port.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/fatfs_port.c b/ports/unix/fatfs_port.c index 4ec5b919ab54b..9e0f444ce2576 100644 --- a/ports/unix/fatfs_port.c +++ b/ports/unix/fatfs_port.c @@ -5,7 +5,7 @@ DWORD get_fattime(void) { time_t now = time(NULL); struct tm *tm = localtime(&now); return ((1900 + tm->tm_year - 1980) << 25) - | (tm->tm_mon << 21) + | ((tm->tm_mon + 1) << 21) | (tm->tm_mday << 16) | (tm->tm_hour << 11) | (tm->tm_min << 5) From 0e6ef403598a7a5fbc44b28e43c264c24aa02416 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 31 Aug 2020 15:01:10 +1000 Subject: [PATCH 0236/3533] tests/extmod: Add tests for verifying FAT and littlefs mtime values. Verifies mtime timestamps on files match the value returned by time.time(). Also update vfs_fat_ramdisk.py so it doesn't check FAT timestamp of the root, because that may change across runs/ports. Signed-off-by: Damien George --- tests/extmod/vfs_fat_mtime.py | 74 +++++++++++++++++++++++++++++ tests/extmod/vfs_fat_mtime.py.exp | 3 ++ tests/extmod/vfs_fat_ramdisk.py | 2 +- tests/extmod/vfs_fat_ramdisk.py.exp | 2 +- tests/extmod/vfs_lfs_mtime.py | 9 +++- tests/extmod/vfs_lfs_mtime.py.exp | 1 + 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 tests/extmod/vfs_fat_mtime.py create mode 100644 tests/extmod/vfs_fat_mtime.py.exp diff --git a/tests/extmod/vfs_fat_mtime.py b/tests/extmod/vfs_fat_mtime.py new file mode 100644 index 0000000000000..d8fd66b75f769 --- /dev/null +++ b/tests/extmod/vfs_fat_mtime.py @@ -0,0 +1,74 @@ +# Test for VfsFat using a RAM device, mtime feature + +try: + import utime, uos + + utime.time + utime.sleep + uos.VfsFat +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMBlockDevice: + ERASE_BLOCK_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) + + def readblocks(self, block, buf): + addr = block * self.ERASE_BLOCK_SIZE + for i in range(len(buf)): + buf[i] = self.data[addr + i] + + def writeblocks(self, block, buf): + addr = block * self.ERASE_BLOCK_SIZE + for i in range(len(buf)): + self.data[addr + i] = buf[i] + + def ioctl(self, op, arg): + if op == 4: # block count + return len(self.data) // self.ERASE_BLOCK_SIZE + if op == 5: # block size + return self.ERASE_BLOCK_SIZE + + +def test(bdev, vfs_class): + print("test", vfs_class) + + # Initial format of block device. + vfs_class.mkfs(bdev) + + # construction + vfs = vfs_class(bdev) + + # Create an empty file, should have a timestamp. + current_time = int(utime.time()) + vfs.open("test1", "wt").close() + + # Wait 2 seconds so mtime will increase (FAT has 2 second resolution). + utime.sleep(2) + + # Create another empty file, should have a timestamp. + vfs.open("test2", "wt").close() + + # Stat the files and check mtime is non-zero. + stat1 = vfs.stat("test1") + stat2 = vfs.stat("test2") + print(stat1[8] != 0, stat2[8] != 0) + + # Check that test1 has mtime which matches time.time() at point of creation. + # TODO this currently fails on the unix port because FAT stores timestamps + # in localtime and stat() is UTC based. + # print(current_time - 1 <= stat1[8] <= current_time + 1) + + # Check that test1 is older than test2. + print(stat1[8] < stat2[8]) + + # Unmount. + vfs.umount() + + +bdev = RAMBlockDevice(50) +test(bdev, uos.VfsFat) diff --git a/tests/extmod/vfs_fat_mtime.py.exp b/tests/extmod/vfs_fat_mtime.py.exp new file mode 100644 index 0000000000000..55550b9834e5b --- /dev/null +++ b/tests/extmod/vfs_fat_mtime.py.exp @@ -0,0 +1,3 @@ +test +True True +True diff --git a/tests/extmod/vfs_fat_ramdisk.py b/tests/extmod/vfs_fat_ramdisk.py index 5f758bf89c87a..9a68d94fedefa 100644 --- a/tests/extmod/vfs_fat_ramdisk.py +++ b/tests/extmod/vfs_fat_ramdisk.py @@ -63,7 +63,7 @@ def ioctl(self, op, arg): f.write("hello!") print(list(vfs.ilistdir())) -print("stat root:", vfs.stat("/")) +print("stat root:", vfs.stat("/")[:-3]) # timestamps differ across runs print("stat file:", vfs.stat("foo_file.txt")[:-3]) # timestamps differ across runs print(b"FOO_FILETXT" in bdev.data) diff --git a/tests/extmod/vfs_fat_ramdisk.py.exp b/tests/extmod/vfs_fat_ramdisk.py.exp index ef6cf1e72b62f..384fa64c76510 100644 --- a/tests/extmod/vfs_fat_ramdisk.py.exp +++ b/tests/extmod/vfs_fat_ramdisk.py.exp @@ -4,7 +4,7 @@ statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255) getcwd: / True [('foo_file.txt', 32768, 0, 6)] -stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +stat root: (16384, 0, 0, 0, 0, 0, 0) stat file: (32768, 0, 0, 0, 0, 0, 6) True True diff --git a/tests/extmod/vfs_lfs_mtime.py b/tests/extmod/vfs_lfs_mtime.py index 0108076884055..bacdd2246c6d6 100644 --- a/tests/extmod/vfs_lfs_mtime.py +++ b/tests/extmod/vfs_lfs_mtime.py @@ -3,6 +3,7 @@ try: import utime, uos + utime.time utime.sleep uos.VfsLfs2 except (ImportError, AttributeError): @@ -46,6 +47,7 @@ def test(bdev, vfs_class): vfs = vfs_class(bdev, mtime=True) # Create an empty file, should have a timestamp. + current_time = int(utime.time()) vfs.open("test1", "wt").close() # Wait 1 second so mtime will increase by at least 1. @@ -54,10 +56,15 @@ def test(bdev, vfs_class): # Create another empty file, should have a timestamp. vfs.open("test2", "wt").close() - # Stat the files and check that test1 is older than test2. + # Stat the files and check mtime is non-zero. stat1 = vfs.stat("test1") stat2 = vfs.stat("test2") print(stat1[8] != 0, stat2[8] != 0) + + # Check that test1 has mtime which matches time.time() at point of creation. + print(current_time <= stat1[8] <= current_time + 1) + + # Check that test1 is older than test2. print(stat1[8] < stat2[8]) # Wait 1 second so mtime will increase by at least 1. diff --git a/tests/extmod/vfs_lfs_mtime.py.exp b/tests/extmod/vfs_lfs_mtime.py.exp index 2e3d7491bcbd8..cf6455c04015b 100644 --- a/tests/extmod/vfs_lfs_mtime.py.exp +++ b/tests/extmod/vfs_lfs_mtime.py.exp @@ -4,6 +4,7 @@ True True True True True +True mtime=False True True From b0932fcf2e2f9a81abf7737ed4b2573bd9ad4a49 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 2 Sep 2020 12:01:26 +1000 Subject: [PATCH 0237/3533] all: Bump version to 1.13. Signed-off-by: Damien George --- docs/conf.py | 2 +- py/mpconfig.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 7dc039d6ca599..460886b4da2e8 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -74,7 +74,7 @@ # # We don't follow "The short X.Y version" vs "The full version, including alpha/beta/rc tags" # breakdown, so use the same version identifier for both to avoid confusion. -version = release = '1.12' +version = release = '1.13' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/py/mpconfig.h b/py/mpconfig.h index 3d9ce8bb55c95..ae4cabfdcf0f2 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -28,7 +28,7 @@ // Current version of MicroPython #define MICROPY_VERSION_MAJOR 1 -#define MICROPY_VERSION_MINOR 12 +#define MICROPY_VERSION_MINOR 13 #define MICROPY_VERSION_MICRO 0 // Combined version as a 32-bit number for convenience From 40ad8f1666b265dafc7844d765f45cfae4b6299f Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 18 Jun 2020 11:19:14 +0200 Subject: [PATCH 0238/3533] all: Rename "sys" module to "usys". This is consistent with the other 'micro' modules and allows implementing additional features in Python via e.g. micropython-lib's sys. Note this is a breaking change (not backwards compatible) for ports which do not enable weak links, as "import sys" must now be replaced with "import usys". --- docs/library/index.rst | 2 +- docs/library/{sys.rst => usys.rst} | 14 +++++++------- drivers/nrf24l01/nrf24l01test.py | 10 +++++----- extmod/webrepl/websocket_helper.py | 5 ++++- py/objmodule.c | 2 +- tests/basics/async_await2.py | 5 ++++- tests/basics/async_for2.py | 5 ++++- tests/basics/async_with2.py | 5 ++++- tests/basics/attrtuple1.py | 5 ++++- tests/basics/builtin_callable.py | 5 ++++- tests/basics/builtin_dir.py | 5 ++++- tests/basics/int_big1.py | 5 ++++- tests/basics/module2.py | 4 ++-- tests/basics/python34.py | 6 +++--- tests/basics/string_compare.py | 5 ++++- tests/basics/sys1.py | 5 ++++- tests/basics/sys_exit.py | 5 ++++- tests/basics/sys_getsizeof.py | 5 ++++- tests/extmod/uctypes_array_assign_native_le.py | 4 ++-- .../uctypes_array_assign_native_le_intbig.py | 4 ++-- tests/extmod/uctypes_native_le.py | 4 ++-- tests/extmod/uctypes_ptr_le.py | 4 ++-- tests/extmod/uctypes_ptr_native_le.py | 4 ++-- tests/extmod/vfs_fat_more.py | 6 +++--- tests/extmod/vfs_lfs_mount.py | 10 +++++----- tests/extmod/vfs_userfs.py | 6 +++--- tests/feature_check/byteorder.py | 5 ++++- tests/float/float2int_doubleprec_intbig.py | 4 ++-- tests/float/float2int_fp30_intbig.py | 4 ++-- tests/float/float2int_intbig.py | 4 ++-- tests/io/argv.py | 5 ++++- tests/io/builtin_print_file.py | 5 ++++- tests/io/file_stdio.py | 5 ++++- tests/io/resource_stream.py | 4 ++-- tests/micropython/emg_exc.py | 4 ++-- tests/micropython/heapalloc_traceback.py | 4 ++-- tests/micropython/import_mpy_invalid.py | 6 +++--- tests/micropython/import_mpy_native_x64.py | 8 ++++---- tests/micropython/opt_level_lineno.py | 2 +- tests/micropython/viper_misc_intbig.py | 4 ++-- tests/misc/print_exception.py | 4 ++-- tests/misc/sys_atexit.py | 6 +++--- tests/misc/sys_exc_info.py | 5 ++++- tests/run-natmodtests.py | 4 ++-- tests/run-tests-exp.py | 2 +- tests/thread/thread_stacksize1.py | 6 ++++-- 46 files changed, 142 insertions(+), 89 deletions(-) rename docs/library/{sys.rst => usys.rst} (93%) diff --git a/docs/library/index.rst b/docs/library/index.rst index 952e67d43ccec..43d9e87f3cda7 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -77,7 +77,6 @@ it will fallback to loading the built-in ``ujson`` module. cmath.rst gc.rst math.rst - sys.rst uarray.rst uasyncio.rst ubinascii.rst @@ -93,6 +92,7 @@ it will fallback to loading the built-in ``ujson`` module. usocket.rst ussl.rst ustruct.rst + usys.rst utime.rst uzlib.rst _thread.rst diff --git a/docs/library/sys.rst b/docs/library/usys.rst similarity index 93% rename from docs/library/sys.rst rename to docs/library/usys.rst index 24f9e353bb006..96016438507cf 100644 --- a/docs/library/sys.rst +++ b/docs/library/usys.rst @@ -1,7 +1,7 @@ -:mod:`sys` -- system specific functions -======================================= +:mod:`usys` -- system specific functions +======================================== -.. module:: sys +.. module:: usys :synopsis: system specific functions |see_cpython_module| :mod:`python:sys`. @@ -28,10 +28,10 @@ Functions This function is a MicroPython extension intended to provide similar functionality to the :mod:`atexit` module in CPython. -.. function:: print_exception(exc, file=sys.stdout, /) +.. function:: print_exception(exc, file=usys.stdout, /) Print exception with a traceback to a file-like object *file* (or - `sys.stdout` by default). + `usys.stdout` by default). .. admonition:: Difference to CPython :class: attention @@ -84,7 +84,7 @@ Constants value directly, but instead count number of bits in it:: bits = 0 - v = sys.maxsize + v = usys.maxsize while v: bits += 1 v >>= 1 @@ -113,7 +113,7 @@ Constants is an identifier of a board, e.g. ``"pyboard"`` for the original MicroPython reference board. It thus can be used to distinguish one board from another. If you need to check whether your program runs on MicroPython (vs other - Python implementation), use `sys.implementation` instead. + Python implementation), use `usys.implementation` instead. .. data:: stderr diff --git a/drivers/nrf24l01/nrf24l01test.py b/drivers/nrf24l01/nrf24l01test.py index 14efbffd2aba1..56bdb6e26eb9c 100644 --- a/drivers/nrf24l01/nrf24l01test.py +++ b/drivers/nrf24l01/nrf24l01test.py @@ -1,6 +1,6 @@ """Test for nrf24l01 module. Portable between MicroPython targets.""" -import sys +import usys import ustruct as struct import utime from machine import Pin, SPI @@ -14,14 +14,14 @@ # master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266. _SLAVE_SEND_DELAY = const(10) -if sys.platform == "pyboard": +if usys.platform == "pyboard": cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"} -elif sys.platform == "esp8266": # Hardware SPI +elif usys.platform == "esp8266": # Hardware SPI cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5} -elif sys.platform == "esp32": # Software SPI +elif usys.platform == "esp32": # Software SPI cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27} else: - raise ValueError("Unsupported platform {}".format(sys.platform)) + raise ValueError("Unsupported platform {}".format(usys.platform)) # Addresses are in little-endian format. They correspond to big-endian # 0xf0f0f0f0e1, 0xf0f0f0f0d2 diff --git a/extmod/webrepl/websocket_helper.py b/extmod/webrepl/websocket_helper.py index 5ca80534eb33f..3260acc52f377 100644 --- a/extmod/webrepl/websocket_helper.py +++ b/extmod/webrepl/websocket_helper.py @@ -1,4 +1,7 @@ -import sys +try: + import usys as sys +except ImportError: + import sys try: import ubinascii as binascii diff --git a/py/objmodule.c b/py/objmodule.c index 060e1bc1e6c2d..a1f9d9d7f146a 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -159,7 +159,7 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = { #endif #endif #if MICROPY_PY_SYS - { MP_ROM_QSTR(MP_QSTR_sys), MP_ROM_PTR(&mp_module_sys) }, + { MP_ROM_QSTR(MP_QSTR_usys), MP_ROM_PTR(&mp_module_sys) }, #endif #if MICROPY_PY_GC && MICROPY_ENABLE_GC { MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&mp_module_gc) }, diff --git a/tests/basics/async_await2.py b/tests/basics/async_await2.py index 1e3164df93372..56f77604ac1be 100644 --- a/tests/basics/async_await2.py +++ b/tests/basics/async_await2.py @@ -1,6 +1,9 @@ # test await expression -import sys +try: + import usys as sys +except ImportError: + import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f diff --git a/tests/basics/async_for2.py b/tests/basics/async_for2.py index aad23a3e5ad8c..4af3be4c6df32 100644 --- a/tests/basics/async_for2.py +++ b/tests/basics/async_for2.py @@ -1,6 +1,9 @@ # test waiting within "async for" __anext__ function -import sys +try: + import usys as sys +except ImportError: + import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f diff --git a/tests/basics/async_with2.py b/tests/basics/async_with2.py index 44421ae91798e..4dd1386240f2c 100644 --- a/tests/basics/async_with2.py +++ b/tests/basics/async_with2.py @@ -1,6 +1,9 @@ # test waiting within async with enter/exit functions -import sys +try: + import usys as sys +except ImportError: + import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f diff --git a/tests/basics/attrtuple1.py b/tests/basics/attrtuple1.py index 78a0fbed1b73a..249c030bb4fdb 100644 --- a/tests/basics/attrtuple1.py +++ b/tests/basics/attrtuple1.py @@ -1,7 +1,10 @@ # test attrtuple # we can't test this type directly so we use sys.implementation object -import sys +try: + import usys as sys +except ImportError: + import sys t = sys.implementation # It can be just a normal tuple on small ports diff --git a/tests/basics/builtin_callable.py b/tests/basics/builtin_callable.py index 3ae49f004d18a..c0a9d0c473a5a 100644 --- a/tests/basics/builtin_callable.py +++ b/tests/basics/builtin_callable.py @@ -7,7 +7,10 @@ print(callable("dfsd")) # modules should not be callabe -import sys +try: + import usys as sys +except ImportError: + import sys print(callable(sys)) # builtins should be callable diff --git a/tests/basics/builtin_dir.py b/tests/basics/builtin_dir.py index 1eecbd044b7b0..1f2b498d77f77 100644 --- a/tests/basics/builtin_dir.py +++ b/tests/basics/builtin_dir.py @@ -4,7 +4,10 @@ print('__name__' in dir()) # dir of module -import sys +try: + import usys as sys +except ImportError: + import sys print('version' in dir(sys)) # dir of type diff --git a/tests/basics/int_big1.py b/tests/basics/int_big1.py index 40d16c455bf38..108e3ee5c9626 100644 --- a/tests/basics/int_big1.py +++ b/tests/basics/int_big1.py @@ -102,7 +102,10 @@ x = -4611686018427387904 # big # sys.maxsize is a constant mpz, so test it's compatible with dynamic ones -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.maxsize + 1 - 1 == sys.maxsize) # test extraction of big int value via mp_obj_get_int_maybe diff --git a/tests/basics/module2.py b/tests/basics/module2.py index a135601579cd7..5923a27e08d42 100644 --- a/tests/basics/module2.py +++ b/tests/basics/module2.py @@ -1,6 +1,6 @@ # uPy behaviour only: builtin modules are read-only -import sys +import usys try: - sys.x = 1 + usys.x = 1 except AttributeError: print("AttributeError") diff --git a/tests/basics/python34.py b/tests/basics/python34.py index 4030db143c4d7..0f6e4bafd0b9e 100644 --- a/tests/basics/python34.py +++ b/tests/basics/python34.py @@ -33,9 +33,9 @@ def test_syntax(code): # from basics/sys1.py # uPy prints version 3.4 -import sys -print(sys.version[:3]) -print(sys.version_info[0], sys.version_info[1]) +import usys +print(usys.version[:3]) +print(usys.version_info[0], usys.version_info[1]) # from basics/exception1.py # in 3.7 no comma is printed if there is only 1 arg (in 3.4-3.6 one is printed) diff --git a/tests/basics/string_compare.py b/tests/basics/string_compare.py index 6515809b3642d..f34879df25bcb 100644 --- a/tests/basics/string_compare.py +++ b/tests/basics/string_compare.py @@ -51,7 +51,10 @@ # this tests an internal string that doesn't have a hash with a string # that does have a hash, but the lengths of the two strings are different -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.version == 'a long string that has a hash') # this special string would have a hash of 0 but is incremented to 1 diff --git a/tests/basics/sys1.py b/tests/basics/sys1.py index 095824afaf1fc..0947ea1964bf9 100644 --- a/tests/basics/sys1.py +++ b/tests/basics/sys1.py @@ -1,6 +1,9 @@ # test sys module -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.__name__) print(type(sys.path)) diff --git a/tests/basics/sys_exit.py b/tests/basics/sys_exit.py index b1f71549db1c5..4d640ae60e4e9 100644 --- a/tests/basics/sys_exit.py +++ b/tests/basics/sys_exit.py @@ -1,6 +1,9 @@ # test sys module's exit function -import sys +try: + import usys as sys +except ImportError: + import sys try: sys.exit diff --git a/tests/basics/sys_getsizeof.py b/tests/basics/sys_getsizeof.py index fe1b403e04bbf..4dc919848ca4c 100644 --- a/tests/basics/sys_getsizeof.py +++ b/tests/basics/sys_getsizeof.py @@ -1,6 +1,9 @@ # test sys.getsizeof() function -import sys +try: + import usys as sys +except ImportError: + import sys try: sys.getsizeof except AttributeError: diff --git a/tests/extmod/uctypes_array_assign_native_le.py b/tests/extmod/uctypes_array_assign_native_le.py index d4c27fc4b3f5a..5bddfcdf2f601 100644 --- a/tests/extmod/uctypes_array_assign_native_le.py +++ b/tests/extmod/uctypes_array_assign_native_le.py @@ -1,4 +1,4 @@ -import sys +import usys try: import uctypes @@ -6,7 +6,7 @@ print("SKIP") raise SystemExit -if sys.byteorder != "little": +if usys.byteorder != "little": print("SKIP") raise SystemExit diff --git a/tests/extmod/uctypes_array_assign_native_le_intbig.py b/tests/extmod/uctypes_array_assign_native_le_intbig.py index f33c63b4ef959..42583b8afefef 100644 --- a/tests/extmod/uctypes_array_assign_native_le_intbig.py +++ b/tests/extmod/uctypes_array_assign_native_le_intbig.py @@ -1,4 +1,4 @@ -import sys +import usys try: import uctypes @@ -6,7 +6,7 @@ print("SKIP") raise SystemExit -if sys.byteorder != "little": +if usys.byteorder != "little": print("SKIP") raise SystemExit diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py index 7958e5c22ada9..9889b98e9d00f 100644 --- a/tests/extmod/uctypes_native_le.py +++ b/tests/extmod/uctypes_native_le.py @@ -1,7 +1,7 @@ # This test is exactly like uctypes_le.py, but uses native structure layout. # Codepaths for packed vs native structures are different. This test only works # on little-endian machine (no matter if 32 or 64 bit). -import sys +import usys try: import uctypes @@ -9,7 +9,7 @@ print("SKIP") raise SystemExit -if sys.byteorder != "little": +if usys.byteorder != "little": print("SKIP") raise SystemExit diff --git a/tests/extmod/uctypes_ptr_le.py b/tests/extmod/uctypes_ptr_le.py index 5d8094ee48a79..f475465ae80f8 100644 --- a/tests/extmod/uctypes_ptr_le.py +++ b/tests/extmod/uctypes_ptr_le.py @@ -1,4 +1,4 @@ -import sys +import usys try: import uctypes @@ -6,7 +6,7 @@ print("SKIP") raise SystemExit -if sys.byteorder != "little": +if usys.byteorder != "little": print("SKIP") raise SystemExit diff --git a/tests/extmod/uctypes_ptr_native_le.py b/tests/extmod/uctypes_ptr_native_le.py index 8ca4d2c55cf9c..ca2b316c54a5d 100644 --- a/tests/extmod/uctypes_ptr_native_le.py +++ b/tests/extmod/uctypes_ptr_native_le.py @@ -1,4 +1,4 @@ -import sys +import usys try: import uctypes @@ -6,7 +6,7 @@ print("SKIP") raise SystemExit -if sys.byteorder != "little": +if usys.byteorder != "little": print("SKIP") raise SystemExit diff --git a/tests/extmod/vfs_fat_more.py b/tests/extmod/vfs_fat_more.py index d8449829deacb..076802f6ad5eb 100644 --- a/tests/extmod/vfs_fat_more.py +++ b/tests/extmod/vfs_fat_more.py @@ -111,10 +111,10 @@ def ioctl(self, op, arg): print(uos.listdir("sys")) # test importing a file from a mounted FS -import sys +import usys -sys.path.clear() -sys.path.append("/sys") +usys.path.clear() +usys.path.append("/sys") with open("sys/test_module.py", "w") as f: f.write('print("test_module!")') import test_module diff --git a/tests/extmod/vfs_lfs_mount.py b/tests/extmod/vfs_lfs_mount.py index 9207f4a8c6d05..2c40b2989796f 100644 --- a/tests/extmod/vfs_lfs_mount.py +++ b/tests/extmod/vfs_lfs_mount.py @@ -68,17 +68,17 @@ def test(bdev, vfs_class): uos.umount("/lfs") # clear imported modules - sys.modules.clear() + usys.modules.clear() bdev = RAMBlockDevice(30) # initialise path -import sys +import usys -sys.path.clear() -sys.path.append("/lfs") -sys.path.append("") +usys.path.clear() +usys.path.append("/lfs") +usys.path.append("") # run tests test(bdev, uos.VfsLfs1) diff --git a/tests/extmod/vfs_userfs.py b/tests/extmod/vfs_userfs.py index 3cdfe82eea8dc..570b8335341e1 100644 --- a/tests/extmod/vfs_userfs.py +++ b/tests/extmod/vfs_userfs.py @@ -1,7 +1,7 @@ # test VFS functionality with a user-defined filesystem # also tests parts of uio.IOBase implementation -import sys +import usys try: import uio @@ -76,9 +76,9 @@ def open(self, path, mode): print(f.read()) # import files from the user filesystem -sys.path.append("/userfs") +usys.path.append("/userfs") import usermod1 # unmount and undo path addition uos.umount("/userfs") -sys.path.pop() +usys.path.pop() diff --git a/tests/feature_check/byteorder.py b/tests/feature_check/byteorder.py index c82a41a24b1c0..509bd8b1b51de 100644 --- a/tests/feature_check/byteorder.py +++ b/tests/feature_check/byteorder.py @@ -1,3 +1,6 @@ -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.byteorder) diff --git a/tests/float/float2int_doubleprec_intbig.py b/tests/float/float2int_doubleprec_intbig.py index 84fa2d25360e1..418eddb60781a 100644 --- a/tests/float/float2int_doubleprec_intbig.py +++ b/tests/float/float2int_doubleprec_intbig.py @@ -2,10 +2,10 @@ try: import ustruct as struct + import usys as sys except: import struct - -import sys + import sys maxsize_bits = 0 maxsize = sys.maxsize diff --git a/tests/float/float2int_fp30_intbig.py b/tests/float/float2int_fp30_intbig.py index 75ff52f39dd49..fcbe2e309f0be 100644 --- a/tests/float/float2int_fp30_intbig.py +++ b/tests/float/float2int_fp30_intbig.py @@ -2,10 +2,10 @@ try: import ustruct as struct + import usys as sys except: import struct - -import sys + import sys maxsize_bits = 0 maxsize = sys.maxsize diff --git a/tests/float/float2int_intbig.py b/tests/float/float2int_intbig.py index 62aca39634968..865aeea7b9171 100644 --- a/tests/float/float2int_intbig.py +++ b/tests/float/float2int_intbig.py @@ -2,10 +2,10 @@ try: import ustruct as struct + import usys as sys except: import struct - -import sys + import sys maxsize_bits = 0 maxsize = sys.maxsize diff --git a/tests/io/argv.py b/tests/io/argv.py index 53254da11906b..834292504d05d 100644 --- a/tests/io/argv.py +++ b/tests/io/argv.py @@ -1,3 +1,6 @@ -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.argv) diff --git a/tests/io/builtin_print_file.py b/tests/io/builtin_print_file.py index 822356a6cc305..e5c20b64e4bec 100644 --- a/tests/io/builtin_print_file.py +++ b/tests/io/builtin_print_file.py @@ -1,6 +1,9 @@ # test builtin print function, using file= argument -import sys +try: + import usys as sys +except ImportError: + import sys try: sys.stdout diff --git a/tests/io/file_stdio.py b/tests/io/file_stdio.py index cbdb070163c31..6c08f35d78784 100644 --- a/tests/io/file_stdio.py +++ b/tests/io/file_stdio.py @@ -1,4 +1,7 @@ -import sys +try: + import usys as sys +except ImportError: + import sys print(sys.stdin.fileno()) print(sys.stdout.fileno()) diff --git a/tests/io/resource_stream.py b/tests/io/resource_stream.py index 5656205b692d6..b589ff99bf2ba 100644 --- a/tests/io/resource_stream.py +++ b/tests/io/resource_stream.py @@ -1,5 +1,5 @@ import uio -import sys +import usys try: uio.resource_stream @@ -11,5 +11,5 @@ print(buf.read()) # resource_stream(None, ...) look ups from current dir, hence sys.path[0] hack -buf = uio.resource_stream(None, sys.path[0] + "/data/file2") +buf = uio.resource_stream(None, usys.path[0] + "/data/file2") print(buf.read()) diff --git a/tests/micropython/emg_exc.py b/tests/micropython/emg_exc.py index bca4d2d9fbaca..b8df94b071af8 100644 --- a/tests/micropython/emg_exc.py +++ b/tests/micropython/emg_exc.py @@ -1,7 +1,7 @@ # test that emergency exceptions work import micropython -import sys +import usys try: import uio @@ -26,7 +26,7 @@ def f(): # print the exception buf = uio.StringIO() - sys.print_exception(exc, buf) + usys.print_exception(exc, buf) for l in buf.getvalue().split("\n"): if l.startswith(" File "): print(l.split('"')[2]) diff --git a/tests/micropython/heapalloc_traceback.py b/tests/micropython/heapalloc_traceback.py index eabd09388b90b..09a2ad2c14d6e 100644 --- a/tests/micropython/heapalloc_traceback.py +++ b/tests/micropython/heapalloc_traceback.py @@ -1,7 +1,7 @@ # test that we can generate a traceback without allocating import micropython -import sys +import usys try: import uio @@ -33,7 +33,7 @@ def test(): # print the exception that was raised buf = uio.StringIO() -sys.print_exception(global_exc, buf) +usys.print_exception(global_exc, buf) for l in buf.getvalue().split("\n"): # uPy on pyboard prints as file, so remove filename. if l.startswith(" File "): diff --git a/tests/micropython/import_mpy_invalid.py b/tests/micropython/import_mpy_invalid.py index 00973fe3f9f12..02fd4b1254971 100644 --- a/tests/micropython/import_mpy_invalid.py +++ b/tests/micropython/import_mpy_invalid.py @@ -1,7 +1,7 @@ # test importing of invalid .mpy files try: - import sys, uio, uos + import usys, uio, uos uio.IOBase uos.mount @@ -54,7 +54,7 @@ def open(self, path, mode): # create and mount a user filesystem uos.mount(UserFS(user_files), "/userfs") -sys.path.append("/userfs") +usys.path.append("/userfs") # import .mpy files from the user filesystem for i in range(len(user_files)): @@ -66,4 +66,4 @@ def open(self, path, mode): # unmount and undo path addition uos.umount("/userfs") -sys.path.pop() +usys.path.pop() diff --git a/tests/micropython/import_mpy_native_x64.py b/tests/micropython/import_mpy_native_x64.py index d0de507d44234..3e7b2058eb812 100644 --- a/tests/micropython/import_mpy_native_x64.py +++ b/tests/micropython/import_mpy_native_x64.py @@ -1,7 +1,7 @@ # test importing of .mpy files with native code (x64 only) try: - import sys, uio, uos + import usys, uio, uos uio.IOBase uos.mount @@ -9,7 +9,7 @@ print("SKIP") raise SystemExit -if not (sys.platform == "linux" and sys.maxsize > 2 ** 32): +if not (usys.platform == "linux" and usys.maxsize > 2 ** 32): print("SKIP") raise SystemExit @@ -101,7 +101,7 @@ def open(self, path, mode): # create and mount a user filesystem uos.mount(UserFS(user_files), "/userfs") -sys.path.append("/userfs") +usys.path.append("/userfs") # import .mpy files from the user filesystem for i in range(len(user_files)): @@ -114,4 +114,4 @@ def open(self, path, mode): # unmount and undo path addition uos.umount("/userfs") -sys.path.pop() +usys.path.pop() diff --git a/tests/micropython/opt_level_lineno.py b/tests/micropython/opt_level_lineno.py index d8253e54b41f0..1cbf2fb1a8548 100644 --- a/tests/micropython/opt_level_lineno.py +++ b/tests/micropython/opt_level_lineno.py @@ -3,4 +3,4 @@ # check that level 3 doesn't store line numbers # the expected output is that any line is printed as "line 1" micropython.opt_level(3) -exec("try:\n xyz\nexcept NameError as er:\n import sys\n sys.print_exception(er)") +exec("try:\n xyz\nexcept NameError as er:\n import usys\n usys.print_exception(er)") diff --git a/tests/micropython/viper_misc_intbig.py b/tests/micropython/viper_misc_intbig.py index 055c08d8e53dc..eda873ca63dee 100644 --- a/tests/micropython/viper_misc_intbig.py +++ b/tests/micropython/viper_misc_intbig.py @@ -6,6 +6,6 @@ def viper_uint() -> uint: return uint(-1) -import sys +import usys -print(viper_uint() == (sys.maxsize << 1 | 1)) +print(viper_uint() == (usys.maxsize << 1 | 1)) diff --git a/tests/misc/print_exception.py b/tests/misc/print_exception.py index f26c1fd5ac867..9b3467b0a0f84 100644 --- a/tests/misc/print_exception.py +++ b/tests/misc/print_exception.py @@ -1,10 +1,10 @@ -import sys - try: try: import uio as io + import usys as sys except ImportError: import io + import sys except ImportError: print("SKIP") raise SystemExit diff --git a/tests/misc/sys_atexit.py b/tests/misc/sys_atexit.py index e9c5693f975e3..141b24cc9f62b 100644 --- a/tests/misc/sys_atexit.py +++ b/tests/misc/sys_atexit.py @@ -1,9 +1,9 @@ # test sys.atexit() function -import sys +import usys try: - sys.atexit + usys.atexit except AttributeError: print("SKIP") raise SystemExit @@ -15,7 +15,7 @@ def do_at_exit(): print("done at exit:", some_var) -sys.atexit(do_at_exit) +usys.atexit(do_at_exit) some_var = "ok" print("done before exit") diff --git a/tests/misc/sys_exc_info.py b/tests/misc/sys_exc_info.py index d7e8a2d943b5e..3a8c4a6c8d3ed 100644 --- a/tests/misc/sys_exc_info.py +++ b/tests/misc/sys_exc_info.py @@ -1,4 +1,7 @@ -import sys +try: + import usys as sys +except ImportError: + import sys try: sys.exc_info diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index f88ca0b788971..8eb27169c4ca1 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -30,7 +30,7 @@ # Code to allow a target MicroPython to import an .mpy from RAM injected_import_hook_code = """\ -import sys, uos, uio +import usys, uos, uio class __File(uio.IOBase): def __init__(self): self.off = 0 @@ -54,7 +54,7 @@ def open(self, path, mode): return __File() uos.mount(__FS(), '/__remote') uos.chdir('/__remote') -sys.modules['{}'] = __import__('__injected') +usys.modules['{}'] = __import__('__injected') """ diff --git a/tests/run-tests-exp.py b/tests/run-tests-exp.py index 34c6f650f83b6..ac32fe9869c36 100644 --- a/tests/run-tests-exp.py +++ b/tests/run-tests-exp.py @@ -5,7 +5,7 @@ # This script is intended to be run by the same interpreter executable # which is to be tested, so should use minimal language functionality. # -import sys +import usys as sys import uos as os diff --git a/tests/thread/thread_stacksize1.py b/tests/thread/thread_stacksize1.py index 5827237a175eb..5d25509b763a0 100644 --- a/tests/thread/thread_stacksize1.py +++ b/tests/thread/thread_stacksize1.py @@ -1,8 +1,10 @@ # test setting the thread stack size # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd - -import sys +try: + import usys as sys +except ImportError: + import sys import _thread # different implementations have different minimum sizes From 38959ed8f11970d47f4e07b720a590e65e80dc35 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 00:32:39 +1000 Subject: [PATCH 0239/3533] lib/libm: Reduce size of static two_over_pi array. Thanks to Jeff Epler for the idea. Signed-off-by: Damien George --- lib/libm/ef_rem_pio2.c | 4 ++-- lib/libm/fdlibm.h | 2 +- lib/libm/kf_rem_pio2.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/libm/ef_rem_pio2.c b/lib/libm/ef_rem_pio2.c index 8111ca197f0e4..fcdfb18287e67 100644 --- a/lib/libm/ef_rem_pio2.c +++ b/lib/libm/ef_rem_pio2.c @@ -35,9 +35,9 @@ * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi */ #ifdef __STDC__ -static const __int32_t two_over_pi[] = { +static const __uint8_t two_over_pi[] = { #else -static __int32_t two_over_pi[] = { +static __uint8_t two_over_pi[] = { #endif 0xA2, 0xF9, 0x83, 0x6E, 0x4E, 0x44, 0x15, 0x29, 0xFC, 0x27, 0x57, 0xD1, 0xF5, 0x34, 0xDD, 0xC0, 0xDB, 0x62, diff --git a/lib/libm/fdlibm.h b/lib/libm/fdlibm.h index ace3b2da22f94..22cd8f16b4c12 100644 --- a/lib/libm/fdlibm.h +++ b/lib/libm/fdlibm.h @@ -188,7 +188,7 @@ extern float __ieee754_scalbf __P((float,float)); extern float __kernel_sinf __P((float,float,int)); extern float __kernel_cosf __P((float,float)); extern float __kernel_tanf __P((float,float,int)); -extern int __kernel_rem_pio2f __P((float*,float*,int,int,int,const __int32_t*)); +extern int __kernel_rem_pio2f __P((float*,float*,int,int,int,const __uint8_t*)); /* A union which permits us to convert between a float and a 32 bit int. */ diff --git a/lib/libm/kf_rem_pio2.c b/lib/libm/kf_rem_pio2.c index 6bd4d287e6905..43e4b4687f506 100644 --- a/lib/libm/kf_rem_pio2.c +++ b/lib/libm/kf_rem_pio2.c @@ -62,10 +62,10 @@ two8 = 2.5600000000e+02f, /* 0x43800000 */ twon8 = 3.9062500000e-03f; /* 0x3b800000 */ #ifdef __STDC__ - int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const __int32_t *ipio2) + int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const __uint8_t *ipio2) #else int __kernel_rem_pio2f(x,y,e0,nx,prec,ipio2) - float x[], y[]; int e0,nx,prec; __int32_t ipio2[]; + float x[], y[]; int e0,nx,prec; __uint8_t ipio2[]; #endif { __int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; From 5e69926ea06cc035e831fcb657e756764682e0b5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 01:04:08 +1000 Subject: [PATCH 0240/3533] stm32/mpconfigport.h: Enable MICROPY_PY_REVERSE_SPECIAL_METHODS. It's a useful core feature. Signed-off-by: Damien George --- ports/stm32/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 6f38d90e8d800..14a05898951ba 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -100,6 +100,7 @@ #define MICROPY_PY_BUILTINS_SLICE_INDICES (1) #define MICROPY_PY_BUILTINS_ROUND_INT (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_BUILTINS_COMPILE (MICROPY_ENABLE_COMPILER) #define MICROPY_PY_BUILTINS_EXECFILE (MICROPY_ENABLE_COMPILER) #define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) From 3ff70792770e4591fec22fa6a1b50f492236fcde Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 12:40:38 +1000 Subject: [PATCH 0241/3533] lib/utils/mpirq: Add mp_irq_init func, and clean up unused init method. mp_irq_init() is useful when the IRQ object is allocated by the caller. The mp_irq_methods_t.init method is not used anywhere so has been removed. Signed-off-by: Damien George --- lib/utils/mpirq.c | 6 +++++- lib/utils/mpirq.h | 14 +++++++------- ports/stm32/machine_uart.c | 1 - ports/zephyr/machine_pin.c | 7 +------ 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/utils/mpirq.c b/lib/utils/mpirq.c index 663be182248cc..02139f24dc5fc 100644 --- a/lib/utils/mpirq.c +++ b/lib/utils/mpirq.c @@ -53,12 +53,16 @@ const mp_arg_t mp_irq_init_args[] = { mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent) { mp_irq_obj_t *self = m_new0(mp_irq_obj_t, 1); + mp_irq_init(self, methods, parent); + return self; +} + +void mp_irq_init(mp_irq_obj_t *self, const mp_irq_methods_t *methods, mp_obj_t parent) { self->base.type = &mp_irq_type; self->methods = (mp_irq_methods_t *)methods; self->parent = parent; self->handler = mp_const_none; self->ishard = false; - return self; } void mp_irq_handler(mp_irq_obj_t *self) { diff --git a/lib/utils/mpirq.h b/lib/utils/mpirq.h index 548185b531147..186c9e1b0b92b 100644 --- a/lib/utils/mpirq.h +++ b/lib/utils/mpirq.h @@ -26,6 +26,8 @@ #ifndef MICROPY_INCLUDED_LIB_UTILS_MPIRQ_H #define MICROPY_INCLUDED_LIB_UTILS_MPIRQ_H +#include "py/obj.h" + /****************************************************************************** DEFINE CONSTANTS ******************************************************************************/ @@ -41,20 +43,17 @@ enum { DEFINE TYPES ******************************************************************************/ -typedef mp_obj_t (*mp_irq_init_t)(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); -typedef mp_uint_t (*mp_irq_uint_method_one_uint_para_t)(mp_obj_t self, mp_uint_t trigger); -typedef mp_uint_t (*mp_irq_int_method_one_para_t)(mp_obj_t self, mp_uint_t info_type); +typedef mp_uint_t (*mp_irq_trigger_fun_t)(mp_obj_t self, mp_uint_t trigger); +typedef mp_uint_t (*mp_irq_info_fun_t)(mp_obj_t self, mp_uint_t info_type); enum { MP_IRQ_INFO_FLAGS, MP_IRQ_INFO_TRIGGERS, - MP_IRQ_INFO_CNT }; typedef struct _mp_irq_methods_t { - mp_irq_init_t init; - mp_irq_uint_method_one_uint_para_t trigger; - mp_irq_int_method_one_para_t info; + mp_irq_trigger_fun_t trigger; + mp_irq_info_fun_t info; } mp_irq_methods_t; typedef struct _mp_irq_obj_t { @@ -77,6 +76,7 @@ extern const mp_obj_type_t mp_irq_type; ******************************************************************************/ mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent); +void mp_irq_init(mp_irq_obj_t *self, const mp_irq_methods_t *methods, mp_obj_t parent); void mp_irq_handler(mp_irq_obj_t *self); #endif // MICROPY_INCLUDED_LIB_UTILS_MPIRQ_H diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 39a45a2a16060..631f46e81f6e1 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -139,7 +139,6 @@ STATIC mp_uint_t pyb_uart_irq_info(mp_obj_t self_in, mp_uint_t info_type) { } STATIC const mp_irq_methods_t pyb_uart_irq_methods = { - .init = pyb_uart_irq, .trigger = pyb_uart_irq_trigger, .info = pyb_uart_irq_info, }; diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index 79d759eb28bf3..c9c6a8c8936e0 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -213,11 +213,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ } if (irq == NULL) { irq = m_new_obj(machine_pin_irq_obj_t); - irq->base.base.type = &mp_irq_type; - irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods; - irq->base.parent = MP_OBJ_FROM_PTR(self); - irq->base.handler = mp_const_none; - irq->base.ishard = false; + mp_irq_init(&irq->base, &machine_pin_irq_methods, MP_OBJ_FROM_PTR(self)); irq->next = MP_STATE_PORT(machine_pin_irq_list); gpio_init_callback(&irq->callback, gpio_callback_handler, BIT(self->pin)); int ret = gpio_add_callback(self->port, &irq->callback); @@ -322,7 +318,6 @@ STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { } STATIC const mp_irq_methods_t machine_pin_irq_methods = { - .init = machine_pin_irq, .trigger = machine_pin_irq_trigger, .info = machine_pin_irq_info, }; From 23109988c29447067b15f1943f72c0b486819c6c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:21:23 +1000 Subject: [PATCH 0242/3533] stm32/uart: Allow static IRQ handler registration. This will allow the HCI UART to use a non-heap mp_irq_obj_t, which avoids needing to make a root pointer for it. --- lib/utils/mpirq.h | 2 +- ports/stm32/machine_uart.c | 78 ++------------------------------------ ports/stm32/uart.c | 62 ++++++++++++++++++++++++++++++ ports/stm32/uart.h | 12 +++++- 4 files changed, 77 insertions(+), 77 deletions(-) diff --git a/lib/utils/mpirq.h b/lib/utils/mpirq.h index 186c9e1b0b92b..dd423c0101137 100644 --- a/lib/utils/mpirq.h +++ b/lib/utils/mpirq.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_LIB_UTILS_MPIRQ_H #define MICROPY_INCLUDED_LIB_UTILS_MPIRQ_H -#include "py/obj.h" +#include "py/runtime.h" /****************************************************************************** DEFINE CONSTANTS diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 631f46e81f6e1..2862f4c0e7b15 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -73,76 +73,6 @@ /// /// uart.any() # returns True if any characters waiting -typedef struct _pyb_uart_irq_map_t { - uint16_t irq_en; - uint16_t flag; -} pyb_uart_irq_map_t; - -STATIC const pyb_uart_irq_map_t mp_irq_map[] = { - { USART_CR1_IDLEIE, UART_FLAG_IDLE}, // RX idle - { USART_CR1_PEIE, UART_FLAG_PE}, // parity error - { USART_CR1_TXEIE, UART_FLAG_TXE}, // TX register empty - { USART_CR1_TCIE, UART_FLAG_TC}, // TX complete - { USART_CR1_RXNEIE, UART_FLAG_RXNE}, // RX register not empty - #if 0 - // For now only IRQs selected by CR1 are supported - #if defined(STM32F4) - { USART_CR2_LBDIE, UART_FLAG_LBD}, // LIN break detection - #else - { USART_CR2_LBDIE, UART_FLAG_LBDF}, // LIN break detection - #endif - { USART_CR3_CTSIE, UART_FLAG_CTS}, // CTS - #endif -}; - -// OR-ed IRQ flags which should not be touched by the user -STATIC const uint32_t mp_irq_reserved = UART_FLAG_RXNE; - -// OR-ed IRQ flags which are allowed to be used by the user -STATIC const uint32_t mp_irq_allowed = UART_FLAG_IDLE; - -STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); - -STATIC void pyb_uart_irq_config(pyb_uart_obj_t *self, bool enable) { - if (self->mp_irq_trigger) { - for (size_t entry = 0; entry < MP_ARRAY_SIZE(mp_irq_map); ++entry) { - if (mp_irq_map[entry].flag & mp_irq_reserved) { - continue; - } - if (mp_irq_map[entry].flag & self->mp_irq_trigger) { - if (enable) { - self->uartx->CR1 |= mp_irq_map[entry].irq_en; - } else { - self->uartx->CR1 &= ~mp_irq_map[entry].irq_en; - } - } - } - } -} - -STATIC mp_uint_t pyb_uart_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { - pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - pyb_uart_irq_config(self, false); - self->mp_irq_trigger = new_trigger; - pyb_uart_irq_config(self, true); - return 0; -} - -STATIC mp_uint_t pyb_uart_irq_info(mp_obj_t self_in, mp_uint_t info_type) { - pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (info_type == MP_IRQ_INFO_FLAGS) { - return self->mp_irq_flags; - } else if (info_type == MP_IRQ_INFO_TRIGGERS) { - return self->mp_irq_trigger; - } - return 0; -} - -STATIC const mp_irq_methods_t pyb_uart_irq_methods = { - .trigger = pyb_uart_irq_trigger, - .info = pyb_uart_irq_info, -}; - STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); if (!self->is_enabled) { @@ -518,7 +448,7 @@ STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t * if (self->mp_irq_obj == NULL) { self->mp_irq_trigger = 0; - self->mp_irq_obj = mp_irq_new(&pyb_uart_irq_methods, MP_OBJ_FROM_PTR(self)); + self->mp_irq_obj = mp_irq_new(&uart_irq_methods, MP_OBJ_FROM_PTR(self)); } if (n_args > 1 || kw_args->used != 0) { @@ -530,17 +460,17 @@ STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t * // Check the trigger mp_uint_t trigger = args[MP_IRQ_ARG_INIT_trigger].u_int; - mp_uint_t not_supported = trigger & ~mp_irq_allowed; + mp_uint_t not_supported = trigger & ~MP_UART_ALLOWED_FLAGS; if (trigger != 0 && not_supported) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("trigger 0x%08x unsupported"), not_supported); } // Reconfigure user IRQs - pyb_uart_irq_config(self, false); + uart_irq_config(self, false); self->mp_irq_obj->handler = handler; self->mp_irq_obj->ishard = args[MP_IRQ_ARG_INIT_hard].u_bool; self->mp_irq_trigger = trigger; - pyb_uart_irq_config(self, true); + uart_irq_config(self, true); } return MP_OBJ_FROM_PTR(self->mp_irq_obj); diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 8693813fa4413..b14a18c6ddfa6 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -93,6 +93,28 @@ extern void NORETURN __fatal_error(const char *msg); +typedef struct _pyb_uart_irq_map_t { + uint16_t irq_en; + uint16_t flag; +} pyb_uart_irq_map_t; + +STATIC const pyb_uart_irq_map_t mp_uart_irq_map[] = { + { USART_CR1_IDLEIE, UART_FLAG_IDLE}, // RX idle + { USART_CR1_PEIE, UART_FLAG_PE}, // parity error + { USART_CR1_TXEIE, UART_FLAG_TXE}, // TX register empty + { USART_CR1_TCIE, UART_FLAG_TC}, // TX complete + { USART_CR1_RXNEIE, UART_FLAG_RXNE}, // RX register not empty + #if 0 + // For now only IRQs selected by CR1 are supported + #if defined(STM32F4) + { USART_CR2_LBDIE, UART_FLAG_LBD}, // LIN break detection + #else + { USART_CR2_LBDIE, UART_FLAG_LBDF}, // LIN break detection + #endif + { USART_CR3_CTSIE, UART_FLAG_CTS}, // CTS + #endif +}; + void uart_init0(void) { #if defined(STM32H7) RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit = {0}; @@ -444,6 +466,23 @@ bool uart_init(pyb_uart_obj_t *uart_obj, return true; } +void uart_irq_config(pyb_uart_obj_t *self, bool enable) { + if (self->mp_irq_trigger) { + for (size_t entry = 0; entry < MP_ARRAY_SIZE(mp_uart_irq_map); ++entry) { + if (mp_uart_irq_map[entry].flag & MP_UART_RESERVED_FLAGS) { + continue; + } + if (mp_uart_irq_map[entry].flag & self->mp_irq_trigger) { + if (enable) { + self->uartx->CR1 |= mp_uart_irq_map[entry].irq_en; + } else { + self->uartx->CR1 &= ~mp_uart_irq_map[entry].irq_en; + } + } + } + } +} + void uart_set_rxbuf(pyb_uart_obj_t *self, size_t len, void *buf) { self->read_buf_head = 0; self->read_buf_tail = 0; @@ -864,3 +903,26 @@ void uart_irq_handler(mp_uint_t uart_id) { mp_irq_handler(self->mp_irq_obj); } } + +STATIC mp_uint_t uart_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uart_irq_config(self, false); + self->mp_irq_trigger = new_trigger; + uart_irq_config(self, true); + return 0; +} + +STATIC mp_uint_t uart_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (info_type == MP_IRQ_INFO_FLAGS) { + return self->mp_irq_flags; + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return self->mp_irq_trigger; + } + return 0; +} + +const mp_irq_methods_t uart_irq_methods = { + .trigger = uart_irq_trigger, + .info = uart_irq_info, +}; diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index 62676fb91be51..9a38db593d4fc 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_STM32_UART_H #define MICROPY_INCLUDED_STM32_UART_H -struct _mp_irq_obj_t; +#include "lib/utils/mpirq.h" typedef enum { PYB_UART_NONE = 0, @@ -45,6 +45,12 @@ typedef enum { #define CHAR_WIDTH_8BIT (0) #define CHAR_WIDTH_9BIT (1) +// OR-ed IRQ flags which are allowed to be used by the user +#define MP_UART_ALLOWED_FLAGS UART_FLAG_IDLE + +// OR-ed IRQ flags which should not be touched by the user +#define MP_UART_RESERVED_FLAGS UART_FLAG_RXNE + typedef struct _pyb_uart_obj_t { mp_obj_base_t base; USART_TypeDef *uartx; @@ -62,16 +68,18 @@ typedef struct _pyb_uart_obj_t { byte *read_buf; // byte or uint16_t, depending on char size uint16_t mp_irq_trigger; // user IRQ trigger mask uint16_t mp_irq_flags; // user IRQ active IRQ flags - struct _mp_irq_obj_t *mp_irq_obj; // user IRQ object + mp_irq_obj_t *mp_irq_obj; // user IRQ object } pyb_uart_obj_t; extern const mp_obj_type_t pyb_uart_type; +extern const mp_irq_methods_t uart_irq_methods; void uart_init0(void); void uart_deinit_all(void); bool uart_exists(int uart_id); bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow); +void uart_irq_config(pyb_uart_obj_t *self, bool enable); void uart_set_rxbuf(pyb_uart_obj_t *self, size_t len, void *buf); void uart_deinit(pyb_uart_obj_t *uart_obj); void uart_irq_handler(mp_uint_t uart_id); From 5ff265a3db998fd86a45d83ccca98fda153991c4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 16:35:25 +1000 Subject: [PATCH 0243/3533] stm32/modbluetooth_hci: Use a static mp_irq_obj_t for BT HCI UART IRQ. So that the IRQ handler does not need to be traced by the GC. --- ports/stm32/modbluetooth_hci.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ports/stm32/modbluetooth_hci.c b/ports/stm32/modbluetooth_hci.c index 4e016eae8451c..54c60adc01181 100644 --- a/ports/stm32/modbluetooth_hci.c +++ b/ports/stm32/modbluetooth_hci.c @@ -135,6 +135,7 @@ int mp_bluetooth_hci_uart_readchar(void) { #include "uart.h" pyb_uart_obj_t mp_bluetooth_hci_uart_obj; +mp_irq_obj_t mp_bluetooth_hci_uart_irq_obj; static uint8_t hci_uart_rxbuf[512]; @@ -163,15 +164,14 @@ int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { } int mp_bluetooth_hci_uart_activate(void) { - // Interrupt on RX chunk received (idle) - // Trigger stack poll when this happens - mp_obj_t uart_irq_fn = mp_load_attr(MP_OBJ_FROM_PTR(&mp_bluetooth_hci_uart_obj), MP_QSTR_irq); - mp_obj_t uargs[] = { - MP_OBJ_FROM_PTR(&mp_uart_interrupt_obj), - MP_OBJ_NEW_SMALL_INT(UART_FLAG_IDLE), - mp_const_true, - }; - mp_call_function_n_kw(uart_irq_fn, 3, 0, uargs); + // Add IRQ handler for IDLE (i.e. packet finished). + uart_irq_config(&mp_bluetooth_hci_uart_obj, false); + mp_irq_init(&mp_bluetooth_hci_uart_irq_obj, &uart_irq_methods, MP_OBJ_FROM_PTR(&mp_bluetooth_hci_uart_obj)); + mp_bluetooth_hci_uart_obj.mp_irq_obj = &mp_bluetooth_hci_uart_irq_obj; + mp_bluetooth_hci_uart_obj.mp_irq_trigger = UART_FLAG_IDLE; + mp_bluetooth_hci_uart_irq_obj.handler = MP_OBJ_FROM_PTR(&mp_uart_interrupt_obj); + mp_bluetooth_hci_uart_irq_obj.ishard = true; + uart_irq_config(&mp_bluetooth_hci_uart_obj, true); mp_bluetooth_hci_controller_init(); mp_bluetooth_hci_controller_activate(); From e46aac24bacdafcb5323fbfc702a3bd597f66072 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 16:04:31 +1000 Subject: [PATCH 0244/3533] extmod/modbluetooth: Rename logging macro to be just DEBUG_printf. And prefix the debug message with "btstack:" or "nimble:", depending on the context. Also use correct format specifier for %zu. --- extmod/btstack/modbluetooth_btstack.c | 146 ++++++++++++++------------ extmod/nimble/modbluetooth_nimble.c | 44 ++++---- 2 files changed, 100 insertions(+), 90 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 8f0c82974c316..9aef7e6879fb0 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -35,7 +35,7 @@ #include "lib/btstack/src/btstack.h" -#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__) +#define DEBUG_printf(...) // printf("btstack: " __VA_ARGS__) #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK" @@ -54,7 +54,7 @@ STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3; volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; STATIC int btstack_error_to_errno(int err) { - DEBUG_EVENT_printf(" --> btstack error: %d\n", err); + DEBUG_printf(" --> btstack error: %d\n", err); if (err == ERROR_CODE_SUCCESS) { return 0; } else if (err == BTSTACK_ACL_BUFFERS_FULL || err == BTSTACK_MEMORY_ALLOC_FAILED) { @@ -168,16 +168,16 @@ STATIC void btstack_remove_pending_operation(mp_btstack_pending_op_t *pending_op STATIC void btstack_notify_indicate_ready_handler(void *context) { MICROPY_PY_BLUETOOTH_ENTER mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)context; - DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%lu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len); + DEBUG_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%zu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len); if (pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY) { int err = att_server_notify(pending_op->conn_handle, pending_op->value_handle, pending_op->buf, pending_op->len); - DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err); + DEBUG_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err); assert(err == ERROR_CODE_SUCCESS); (void)err; } else { assert(pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE); int err = att_server_indicate(pending_op->conn_handle, pending_op->value_handle, NULL, 0); - DEBUG_EVENT_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err); + DEBUG_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err); assert(err == ERROR_CODE_SUCCESS); (void)err; } @@ -188,7 +188,7 @@ STATIC void btstack_notify_indicate_ready_handler(void *context) { // Register a pending background operation -- copies the buffer, and makes it known to the GC. STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, const uint8_t *buf, size_t len) { - DEBUG_EVENT_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%lu\n", op_type, conn_handle, value_handle, len); + DEBUG_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%zu\n", op_type, conn_handle, value_handle, len); mp_btstack_pending_op_t *pending_op = m_new_obj_var(mp_btstack_pending_op_t, uint8_t, len); pending_op->op_type = op_type; pending_op->conn_handle = conn_handle; @@ -219,20 +219,20 @@ STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_ty // know for sure that we're using the correct entry. STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, bool del) { MICROPY_PY_BLUETOOTH_ENTER - DEBUG_EVENT_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle); + DEBUG_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle); btstack_linked_list_iterator_t it; btstack_linked_list_iterator_init(&it, &MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops); while (btstack_linked_list_iterator_has_next(&it)) { mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)btstack_linked_list_iterator_next(&it); if (pending_op->op_type == op_type && pending_op->conn_handle == conn_handle && (value_handle == 0xffff || pending_op->value_handle == value_handle)) { - DEBUG_EVENT_printf("btstack_finish_pending_operation: found value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len); + DEBUG_printf("btstack_finish_pending_operation: found value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len); btstack_remove_pending_operation(pending_op, del); MICROPY_PY_BLUETOOTH_EXIT return del ? NULL : pending_op; } } - DEBUG_EVENT_printf("btstack_finish_pending_operation: not found\n"); + DEBUG_printf("btstack_finish_pending_operation: not found\n"); MICROPY_PY_BLUETOOTH_EXIT return NULL; } @@ -243,7 +243,7 @@ STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_typ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { (void)channel; (void)size; - DEBUG_EVENT_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet); + DEBUG_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet); if (packet_type != HCI_EVENT_PACKET) { return; } @@ -251,13 +251,13 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan uint8_t event_type = hci_event_packet_get_type(packet); if (event_type == ATT_EVENT_CONNECTED) { - DEBUG_EVENT_printf(" --> att connected\n"); + DEBUG_printf(" --> att connected\n"); // The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which. // So we use the HCI_EVENT_LE_META event directly in the main packet handler. } else if (event_type == ATT_EVENT_DISCONNECTED) { - DEBUG_EVENT_printf(" --> att disconnected\n"); + DEBUG_printf(" --> att disconnected\n"); } else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) { - DEBUG_EVENT_printf(" --> att indication complete\n"); + DEBUG_printf(" --> att indication complete\n"); uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet); uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet); uint8_t status = att_event_handle_value_indication_complete_get_status(packet); @@ -265,12 +265,12 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan } else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { // Ignore, duplicated by att_server.c. } else { - DEBUG_EVENT_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type); + DEBUG_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type); } } STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) { - DEBUG_EVENT_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); + DEBUG_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); if (packet_type != HCI_EVENT_PACKET) { return; } @@ -278,7 +278,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t uint8_t event_type = hci_event_packet_get_type(packet); if (event_type == HCI_EVENT_LE_META) { - DEBUG_EVENT_printf(" --> hci le meta\n"); + DEBUG_printf(" --> hci le meta\n"); if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) { uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet); @@ -296,7 +296,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t } } else if (event_type == BTSTACK_EVENT_STATE) { uint8_t state = btstack_event_state_get_state(packet); - DEBUG_EVENT_printf(" --> btstack event state 0x%02x\n", state); + DEBUG_printf(" --> btstack event state 0x%02x\n", state); if (state == HCI_STATE_WORKING) { // Signal that initialisation has completed. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE; @@ -305,21 +305,21 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; } } else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) { - DEBUG_EVENT_printf(" --> hci transport packet sent\n"); + DEBUG_printf(" --> hci transport packet sent\n"); } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { - DEBUG_EVENT_printf(" --> hci command complete\n"); + DEBUG_printf(" --> hci command complete\n"); } else if (event_type == HCI_EVENT_COMMAND_STATUS) { - DEBUG_EVENT_printf(" --> hci command status\n"); + DEBUG_printf(" --> hci command status\n"); } else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) { - DEBUG_EVENT_printf(" --> hci number of completed packets\n"); + DEBUG_printf(" --> hci number of completed packets\n"); } else if (event_type == BTSTACK_EVENT_NR_CONNECTIONS_CHANGED) { - DEBUG_EVENT_printf(" --> btstack # conns changed\n"); + DEBUG_printf(" --> btstack # conns changed\n"); } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) { - DEBUG_EVENT_printf(" --> hci vendor specific\n"); + DEBUG_printf(" --> hci vendor specific\n"); } else if (event_type == GATT_EVENT_MTU) { - DEBUG_EVENT_printf(" --> hci MTU\n"); + DEBUG_printf(" --> hci MTU\n"); } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { - DEBUG_EVENT_printf(" --> hci disconnect complete\n"); + DEBUG_printf(" --> hci disconnect complete\n"); uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet); const hci_connection_t *conn = hci_connection_for_handle(conn_handle); uint16_t irq_event; @@ -334,7 +334,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else if (event_type == GAP_EVENT_ADVERTISING_REPORT) { - DEBUG_EVENT_printf(" --> gap advertising report\n"); + DEBUG_printf(" --> gap advertising report\n"); bd_addr_t address; gap_event_advertising_report_get_address(packet, address); uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet); @@ -346,7 +346,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet); - DEBUG_EVENT_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status); + DEBUG_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status); if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { // TODO there is no value_handle available to pass here. // TODO try and get this implemented in btstack. @@ -361,28 +361,28 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status); } } else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) { - DEBUG_EVENT_printf(" --> gatt service query result\n"); + DEBUG_printf(" --> gatt service query result\n"); uint16_t conn_handle = gatt_event_service_query_result_get_handle(packet); gatt_client_service_t service; gatt_event_service_query_result_get_service(packet, &service); mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(service.uuid16, service.uuid128); mp_bluetooth_gattc_on_primary_service_result(conn_handle, service.start_group_handle, service.end_group_handle, &service_uuid); } else if (event_type == GATT_EVENT_CHARACTERISTIC_QUERY_RESULT) { - DEBUG_EVENT_printf(" --> gatt characteristic query result\n"); + DEBUG_printf(" --> gatt characteristic query result\n"); uint16_t conn_handle = gatt_event_characteristic_query_result_get_handle(packet); gatt_client_characteristic_t characteristic; gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(characteristic.uuid16, characteristic.uuid128); mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.start_handle, characteristic.value_handle, characteristic.properties, &characteristic_uuid); } else if (event_type == GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT) { - DEBUG_EVENT_printf(" --> gatt descriptor query result\n"); + DEBUG_printf(" --> gatt descriptor query result\n"); uint16_t conn_handle = gatt_event_all_characteristic_descriptors_query_result_get_handle(packet); gatt_client_characteristic_descriptor_t descriptor; gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &descriptor); mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(descriptor.uuid16, descriptor.uuid128); mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor.handle, &descriptor_uuid); } else if (event_type == GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) { - DEBUG_EVENT_printf(" --> gatt characteristic value query result\n"); + DEBUG_printf(" --> gatt characteristic value query result\n"); uint16_t conn_handle = gatt_event_characteristic_value_query_result_get_handle(packet); uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet); uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet); @@ -392,7 +392,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gattc_on_data_available_chunk(data, len); mp_bluetooth_gattc_on_data_available_end(atomic_state); } else if (event_type == GATT_EVENT_NOTIFICATION) { - DEBUG_EVENT_printf(" --> gatt notification\n"); + DEBUG_printf(" --> gatt notification\n"); uint16_t conn_handle = gatt_event_notification_get_handle(packet); uint16_t value_handle = gatt_event_notification_get_value_handle(packet); uint16_t len = gatt_event_notification_get_value_length(packet); @@ -402,7 +402,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gattc_on_data_available_chunk(data, len); mp_bluetooth_gattc_on_data_available_end(atomic_state); } else if (event_type == GATT_EVENT_INDICATION) { - DEBUG_EVENT_printf(" --> gatt indication\n"); + DEBUG_printf(" --> gatt indication\n"); uint16_t conn_handle = gatt_event_indication_get_handle(packet); uint16_t value_handle = gatt_event_indication_get_value_handle(packet); uint16_t len = gatt_event_indication_get_value_length(packet); @@ -413,17 +413,17 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gattc_on_data_available_end(atomic_state); } else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) { uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet); - DEBUG_EVENT_printf(" --> gatt can write without response %d\n", conn_handle); + DEBUG_printf(" --> gatt can write without response %d\n", conn_handle); mp_btstack_pending_op_t *pending_op = btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, 0xffff, false /* !del */); if (pending_op) { - DEBUG_EVENT_printf(" --> ready for value_handle=%d len=%lu\n", pending_op->value_handle, pending_op->len); + DEBUG_printf(" --> ready for value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len); gatt_client_write_value_of_characteristic_without_response(pending_op->conn_handle, pending_op->value_handle, pending_op->len, (uint8_t *)pending_op->buf); // Note: Can't "del" the pending_op from IRQ context. Leave it for the GC. } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else { - DEBUG_EVENT_printf(" --> hci event type: unknown (0x%02x)\n", event_type); + DEBUG_printf(" --> hci event type: unknown (0x%02x)\n", event_type); } } @@ -489,7 +489,7 @@ STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) { } int mp_bluetooth_init(void) { - DEBUG_EVENT_printf("mp_bluetooth_init\n"); + DEBUG_printf("mp_bluetooth_init\n"); if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { return 0; @@ -538,6 +538,8 @@ int mp_bluetooth_init(void) { btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler); btstack_run_loop_add_timer(&btstack_init_deinit_timeout); + DEBUG_printf("mp_bluetooth_init: waiting for stack startup\n"); + // Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT. mp_bluetooth_btstack_port_start(); while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) { @@ -547,6 +549,8 @@ int mp_bluetooth_init(void) { // Check for timeout. if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { + DEBUG_printf("mp_bluetooth_init: stack startup timed out\n"); + // Required to stop the polling loop. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; // Attempt a shutdown (may not do anything). @@ -566,7 +570,7 @@ int mp_bluetooth_init(void) { } void mp_bluetooth_deinit(void) { - DEBUG_EVENT_printf("mp_bluetooth_deinit\n"); + DEBUG_printf("mp_bluetooth_deinit\n"); // Nothing to do if not initialised. if (!MP_STATE_PORT(bluetooth_btstack_root_pointers)) { @@ -595,6 +599,8 @@ void mp_bluetooth_deinit(void) { mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; + + DEBUG_printf("mp_bluetooth_deinit: complete\n"); } bool mp_bluetooth_is_active(void) { @@ -618,7 +624,7 @@ int mp_bluetooth_gap_set_device_name(const uint8_t *buf, size_t len) { } int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) { - DEBUG_EVENT_printf("mp_bluetooth_gap_advertise_start\n"); + DEBUG_printf("mp_bluetooth_gap_advertise_start\n"); uint16_t adv_int_min = interval_us / 625; uint16_t adv_int_max = interval_us / 625; uint8_t adv_type = connectable ? 0 : 2; @@ -654,14 +660,14 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons } void mp_bluetooth_gap_advertise_stop(void) { - DEBUG_EVENT_printf("mp_bluetooth_gap_advertise_stop\n"); + DEBUG_printf("mp_bluetooth_gap_advertise_stop\n"); gap_advertisements_enable(false); MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc = 0; MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data = NULL; } int mp_bluetooth_gatts_register_service_begin(bool append) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service_begin\n"); + DEBUG_printf("mp_bluetooth_gatts_register_service_begin\n"); if (!append) { // This will reset the DB. // Becase the DB is statically allocated, there's no problem with just re-initing it. @@ -682,10 +688,10 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) { (void)connection_handle; - DEBUG_EVENT_printf("btstack: att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size); + DEBUG_printf("btstack: att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size); mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle); if (!entry) { - DEBUG_EVENT_printf("btstack: att_read_callback handle not found\n"); + DEBUG_printf("btstack: att_read_callback handle not found\n"); return 0; // TODO: Find status code for not-found. } @@ -695,10 +701,10 @@ STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t a STATIC int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) { (void)offset; (void)transaction_mode; - DEBUG_EVENT_printf("btstack: att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size); + DEBUG_printf("btstack: att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size); mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle); if (!entry) { - DEBUG_EVENT_printf("btstack: att_write_callback handle not found\n"); + DEBUG_printf("btstack: att_write_callback handle not found\n"); return 0; // TODO: Find status code for not-found. } @@ -720,7 +726,7 @@ STATIC inline uint16_t get_uuid16(const mp_obj_bluetooth_uuid_t *uuid) { } int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service\n"); + DEBUG_printf("mp_bluetooth_gatts_register_service\n"); // Note: btstack expects BE UUIDs (which it immediately convertes to LE). // So we have to convert all our modbluetooth LE UUIDs to BE just for the att_db_util_add_* methods (using get_uuid16 above, and reverse_128 from btstackutil.h). @@ -764,7 +770,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m return ret; } } - DEBUG_EVENT_printf("Registered char with handle %u\n", handles[handle_index]); + DEBUG_printf("mp_bluetooth_gatts_register_service: Registered char with handle %u\n", handles[handle_index]); ++handle_index; for (size_t j = 0; j < num_descriptors[i]; ++j) { @@ -782,7 +788,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m return MP_EINVAL; } mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index], MP_BLUETOOTH_DEFAULT_ATTR_LEN); - DEBUG_EVENT_printf("Registered desc with handle %u\n", handles[handle_index]); + DEBUG_printf("mp_bluetooth_gatts_register_service: Registered desc with handle %u\n", handles[handle_index]); ++descriptor_index; ++handle_index; } @@ -792,23 +798,23 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m } int mp_bluetooth_gatts_register_service_end(void) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_register_service_end\n"); + DEBUG_printf("mp_bluetooth_gatts_register_service_end\n"); att_server_init(att_db_util_get_address(), &att_read_callback, &att_write_callback); return 0; } int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *value_len) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_read\n"); + DEBUG_printf("mp_bluetooth_gatts_read\n"); return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len); } int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_write\n"); + DEBUG_printf("mp_bluetooth_gatts_write\n"); return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len); } int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_notify\n"); + DEBUG_printf("mp_bluetooth_gatts_notify\n"); // Note: btstack doesn't appear to support sending a notification without a value, so include the stored value. uint8_t *data = NULL; size_t len = 0; @@ -817,7 +823,7 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) { } int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send\n"); + DEBUG_printf("mp_bluetooth_gatts_notify_send\n"); // Attempt to send immediately. If it succeeds, btstack will copy the buffer. MICROPY_PY_BLUETOOTH_ENTER @@ -825,7 +831,7 @@ int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, MICROPY_PY_BLUETOOTH_EXIT if (err == BTSTACK_ACL_BUFFERS_FULL) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n"); + DEBUG_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n"); // Schedule callback, making a copy of the buffer. mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, conn_handle, value_handle, value, value_len); @@ -843,7 +849,7 @@ int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, } int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate\n"); + DEBUG_printf("mp_bluetooth_gatts_indicate\n"); uint8_t *data = NULL; size_t len = 0; @@ -858,7 +864,7 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { MICROPY_PY_BLUETOOTH_EXIT if (err == BTSTACK_ACL_BUFFERS_FULL) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n"); + DEBUG_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n"); // Schedule callback, making a copy of the buffer. mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, conn_handle, value_handle, data, len); @@ -876,12 +882,12 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) { } int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) { - DEBUG_EVENT_printf("mp_bluetooth_gatts_set_buffer\n"); + DEBUG_printf("mp_bluetooth_gatts_set_buffer\n"); return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, len, append); } int mp_bluetooth_gap_disconnect(uint16_t conn_handle) { - DEBUG_EVENT_printf("mp_bluetooth_gap_disconnect\n"); + DEBUG_printf("mp_bluetooth_gap_disconnect\n"); gap_disconnect(conn_handle); return 0; } @@ -895,7 +901,7 @@ STATIC void scan_duration_timeout_handler(btstack_timer_source_t *ds) { } int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) { - DEBUG_EVENT_printf("mp_bluetooth_gap_scan_start\n"); + DEBUG_printf("mp_bluetooth_gap_scan_start\n"); if (duration_ms > 0) { btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms); @@ -910,7 +916,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ } int mp_bluetooth_gap_scan_stop(void) { - DEBUG_EVENT_printf("mp_bluetooth_gap_scan_stop\n"); + DEBUG_printf("mp_bluetooth_gap_scan_stop\n"); btstack_run_loop_remove_timer(&scan_duration_timeout); gap_stop_scan(); mp_bluetooth_gap_on_scan_complete(); @@ -918,7 +924,7 @@ int mp_bluetooth_gap_scan_stop(void) { } int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) { - DEBUG_EVENT_printf("mp_bluetooth_gap_peripheral_connect\n"); + DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n"); uint16_t conn_scan_interval = 60000 / 625; uint16_t conn_scan_window = 30000 / 625; @@ -937,7 +943,7 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, } int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_primary_services\n"); + DEBUG_printf("mp_bluetooth_gattc_discover_primary_services\n"); uint8_t err; if (uuid) { if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { @@ -947,7 +953,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_ reverse_128(uuid->data, buffer); err = gatt_client_discover_primary_services_by_uuid128(&btstack_packet_handler_discover_services, conn_handle, buffer); } else { - DEBUG_EVENT_printf(" --> unknown UUID size\n"); + DEBUG_printf(" --> unknown UUID size\n"); return MP_EINVAL; } } else { @@ -957,7 +963,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_ } int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_characteristics\n"); + DEBUG_printf("mp_bluetooth_gattc_discover_characteristics\n"); gatt_client_service_t service = { // Only start/end handles needed for gatt_client_discover_characteristics_for_service. .start_group_handle = start_handle, @@ -974,7 +980,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s reverse_128(uuid->data, buffer); err = gatt_client_discover_characteristics_for_service_by_uuid128(&btstack_packet_handler_discover_characteristics, conn_handle, &service, buffer); } else { - DEBUG_EVENT_printf(" --> unknown UUID size\n"); + DEBUG_printf(" --> unknown UUID size\n"); return MP_EINVAL; } } else { @@ -984,7 +990,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s } int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_discover_descriptors\n"); + DEBUG_printf("mp_bluetooth_gattc_discover_descriptors\n"); gatt_client_characteristic_t characteristic = { // Only start/end handles needed for gatt_client_discover_characteristic_descriptors. .start_handle = start_handle, @@ -998,12 +1004,12 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start } int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_read\n"); + DEBUG_printf("mp_bluetooth_gattc_read\n"); return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle)); } int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_write\n"); + DEBUG_printf("mp_bluetooth_gattc_write\n"); // We should be distinguishing between gatt_client_write_value_of_characteristic vs // gatt_client_write_characteristic_descriptor_using_descriptor_handle. @@ -1018,13 +1024,13 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const // If possible, this will send immediately, copying the buffer directly to the ACL buffer. err = gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value); if (err == GATT_CLIENT_BUSY) { - DEBUG_EVENT_printf("mp_bluetooth_gattc_write: client busy\n"); + DEBUG_printf("mp_bluetooth_gattc_write: client busy\n"); // Can't send right now, need to take a copy of the buffer and add it to the queue. pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, value_handle, value, *value_len); // Notify when this conn_handle can write. err = gatt_client_request_can_write_without_response_event(&btstack_packet_handler_generic, conn_handle); } else { - DEBUG_EVENT_printf("mp_bluetooth_gattc_write: other failure: %d\n", err); + DEBUG_printf("mp_bluetooth_gattc_write: other failure: %d\n", err); } } else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) { // Pending operation copies the value buffer and keeps a GC reference diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e51f2747ec5c8..36bfa6cccfa61 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -45,7 +45,7 @@ #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" #endif -#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__) +#define DEBUG_printf(...) // printf("nimble: " __VA_ARGS__) #define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV @@ -172,10 +172,12 @@ STATIC void sync_cb(void) { } if (MP_BLUETOOTH_DEFAULT_ATTR_LEN > 20) { + DEBUG_printf("sync_cb: Setting MTU\n"); rc = ble_att_set_preferred_mtu(MP_BLUETOOTH_DEFAULT_ATTR_LEN + 3); assert(rc == 0); } + DEBUG_printf("sync_cb: Setting device name\n"); ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME); mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE; @@ -188,12 +190,12 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { switch (ctxt->op) { case BLE_GATT_REGISTER_OP_SVC: // Called when a service is successfully registered. - DEBUG_EVENT_printf("gatts_register_cb: svc uuid=%p handle=%d\n", &ctxt->svc.svc_def->uuid, ctxt->svc.handle); + DEBUG_printf("gatts_register_cb: svc uuid=%p handle=%d\n", &ctxt->svc.svc_def->uuid, ctxt->svc.handle); break; case BLE_GATT_REGISTER_OP_CHR: // Called when a characteristic is successfully registered. - DEBUG_EVENT_printf("gatts_register_cb: chr uuid=%p def_handle=%d val_handle=%d\n", &ctxt->chr.chr_def->uuid, ctxt->chr.def_handle, ctxt->chr.val_handle); + DEBUG_printf("gatts_register_cb: chr uuid=%p def_handle=%d val_handle=%d\n", &ctxt->chr.chr_def->uuid, ctxt->chr.def_handle, ctxt->chr.val_handle); // Note: We will get this event for the default GAP Service, meaning that we allocate storage for the // "device name" and "appearance" characteristics, even though we never see the reads for them. @@ -207,7 +209,7 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { case BLE_GATT_REGISTER_OP_DSC: // Called when a descriptor is successfully registered. // Note: This is event is not called for the CCCD. - DEBUG_EVENT_printf("gatts_register_cb: dsc uuid=%p handle=%d\n", &ctxt->dsc.dsc_def->uuid, ctxt->dsc.handle); + DEBUG_printf("gatts_register_cb: dsc uuid=%p handle=%d\n", &ctxt->dsc.dsc_def->uuid, ctxt->dsc.handle); // See above, safe to alloc. mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, ctxt->dsc.handle, MP_BLUETOOTH_DEFAULT_ATTR_LEN); @@ -217,13 +219,13 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { break; default: - DEBUG_EVENT_printf("gatts_register_cb: unknown op %d\n", ctxt->op); + DEBUG_printf("gatts_register_cb: unknown op %d\n", ctxt->op); break; } } STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { - DEBUG_EVENT_printf("gap_event_cb: type=%d\n", event->type); + DEBUG_printf("gap_event_cb: type=%d\n", event->type); if (!mp_bluetooth_is_active()) { return 0; } @@ -250,7 +252,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { break; case BLE_GAP_EVENT_NOTIFY_TX: { - DEBUG_EVENT_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status); + DEBUG_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status); // This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0). if (event->notify_tx.indication && event->notify_tx.status != 0) { // Map "done/ack" to 0, otherwise pass the status directly. @@ -263,7 +265,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { } int mp_bluetooth_init(void) { - DEBUG_EVENT_printf("mp_bluetooth_init\n"); + DEBUG_printf("mp_bluetooth_init\n"); // Clean up if necessary. mp_bluetooth_deinit(); @@ -291,7 +293,7 @@ int mp_bluetooth_init(void) { while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { MICROPY_EVENT_POLL_HOOK } - DEBUG_EVENT_printf("mp_bluetooth_init: ready\n"); + DEBUG_printf("mp_bluetooth_init: ready\n"); return 0; } @@ -304,7 +306,7 @@ STATIC void ble_hs_shutdown_stop_cb(int status, void *arg) { STATIC struct ble_hs_stop_listener ble_hs_shutdown_stop_listener; void mp_bluetooth_deinit(void) { - DEBUG_EVENT_printf("mp_bluetooth_deinit\n"); + DEBUG_printf("mp_bluetooth_deinit\n"); if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { return; } @@ -325,7 +327,7 @@ void mp_bluetooth_deinit(void) { mp_bluetooth_nimble_port_deinit(); MP_STATE_PORT(bluetooth_nimble_root_pointers) = NULL; - DEBUG_EVENT_printf("mp_bluetooth_deinit: shut down\n"); + DEBUG_printf("mp_bluetooth_deinit: shut down\n"); } bool mp_bluetooth_is_active(void) { @@ -409,7 +411,7 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons if (ret == 0) { return 0; } - DEBUG_EVENT_printf("ble_gap_adv_start: %d\n", ret); + DEBUG_printf("ble_gap_adv_start: %d\n", ret); return ble_hs_err_to_errno(ret); } @@ -421,7 +423,7 @@ void mp_bluetooth_gap_advertise_stop(void) { } static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - DEBUG_EVENT_printf("characteristic_access_cb: conn_handle=%u value_handle=%u op=%u\n", conn_handle, value_handle, ctxt->op); + DEBUG_printf("characteristic_access_cb: conn_handle=%u value_handle=%u op=%u\n", conn_handle, value_handle, ctxt->op); if (!mp_bluetooth_is_active()) { return 0; } @@ -640,7 +642,7 @@ STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_ } STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) { - DEBUG_EVENT_printf("gap_scan_cb: event=%d type=%d\n", event->type, event->type == BLE_GAP_EVENT_DISC ? event->disc.event_type : -1); + DEBUG_printf("gap_scan_cb: event=%d type=%d\n", event->type, event->type == BLE_GAP_EVENT_DISC ? event->disc.event_type : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -681,6 +683,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ } int mp_bluetooth_gap_scan_stop(void) { + DEBUG_printf("mp_bluetooth_gap_scan_stop\n"); if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } @@ -697,7 +700,7 @@ int mp_bluetooth_gap_scan_stop(void) { // Central role: GAP events for a connected peripheral. STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { - DEBUG_EVENT_printf("peripheral_gap_event_cb: event=%d\n", event->type); + DEBUG_printf("peripheral_gap_event_cb: event=%d\n", event->type); if (!mp_bluetooth_is_active()) { return 0; } @@ -745,6 +748,7 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { } int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) { + DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n"); if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } @@ -770,7 +774,7 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, } STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) { - DEBUG_EVENT_printf("peripheral_discover_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1); + DEBUG_printf("peripheral_discover_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -799,7 +803,7 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_ } STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) { - DEBUG_EVENT_printf("ble_gatt_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1); + DEBUG_printf("ble_gatt_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -828,7 +832,7 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s } STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg) { - DEBUG_EVENT_printf("ble_gatt_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1); + DEBUG_printf("ble_gatt_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -850,7 +854,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start } STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - DEBUG_EVENT_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); + DEBUG_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -871,7 +875,7 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { } STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - DEBUG_EVENT_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); + DEBUG_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); if (!mp_bluetooth_is_active()) { return 0; } From ed14435a8e6199b845c3404a9052f9ff4213292c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:43:09 +1000 Subject: [PATCH 0245/3533] extmod/modbluetooth: Refactor stack/hci/driver/port bindings. Previously the interaction between the different layers of the Bluetooth stack was different on each port and each stack. This commit defines common interfaces between them and implements them for cyw43, btstack, nimble, stm32, unix. --- drivers/cyw43/cywbt.c | 14 +- extmod/btstack/btstack.mk | 11 ++ extmod/btstack/btstack_config.h | 1 + .../btstack/btstack_hci_uart.c | 94 ++++---------- .../btstack_hci_uart.h} | 21 ++- extmod/btstack/modbluetooth_btstack.c | 5 + extmod/btstack/modbluetooth_btstack.h | 1 + extmod/mpbthci.c | 38 ++++++ extmod/{modbluetooth_hci.h => mpbthci.h} | 25 ++-- extmod/nimble/hal/hal_uart.c | 25 ++-- extmod/nimble/hal/hal_uart.h | 31 ++++- extmod/nimble/logcfg/logcfg.h | 45 +++++++ extmod/nimble/modbluetooth_nimble.c | 122 ++++++++++++++---- extmod/nimble/modbluetooth_nimble.h | 18 ++- extmod/nimble/nimble.mk | 13 +- .../nimble/{npl_os.c => nimble_npl_os.c} | 64 ++++++--- extmod/nimble/nimble/nimble_npl_os.h | 19 ++- extmod/nimble/syscfg/syscfg.h | 5 +- ports/esp32/Makefile | 2 +- ports/esp32/{nimble.c => mpnimbleport.c} | 30 ++++- ports/stm32/Makefile | 7 +- .../{modbluetooth_hci.c => mpbthciport.c} | 97 +++++++++----- ports/stm32/mpbtstackport.c | 107 +++++++++++++++ ports/stm32/mpbtstackport.h | 34 +++++ ports/stm32/mpconfigport.h | 21 ++- ports/stm32/{nimble.c => mpnimbleport.c} | 61 ++++----- ports/stm32/mpnimbleport.h | 34 +++++ ports/unix/Makefile | 9 +- ports/unix/mpbtstackport.h | 39 ++++++ ports/unix/mpbtstackport_common.c | 92 +++++++++++++ .../{btstack_usb.c => mpbtstackport_usb.c} | 101 ++------------- ports/unix/mpconfigport.h | 4 +- 32 files changed, 849 insertions(+), 341 deletions(-) rename ports/stm32/btstack.c => extmod/btstack/btstack_hci_uart.c (67%) rename extmod/{nimble/nimble/nimble_hci_uart.h => btstack/btstack_hci_uart.h} (66%) create mode 100644 extmod/mpbthci.c rename extmod/{modbluetooth_hci.h => mpbthci.h} (71%) create mode 100644 extmod/nimble/logcfg/logcfg.h rename extmod/nimble/nimble/{npl_os.c => nimble_npl_os.c} (85%) rename ports/esp32/{nimble.c => mpnimbleport.c} (66%) rename ports/stm32/{modbluetooth_hci.c => mpbthciport.c} (75%) create mode 100644 ports/stm32/mpbtstackport.c create mode 100644 ports/stm32/mpbtstackport.h rename ports/stm32/{nimble.c => mpnimbleport.c} (56%) create mode 100644 ports/stm32/mpnimbleport.h create mode 100644 ports/unix/mpbtstackport.h create mode 100644 ports/unix/mpbtstackport_common.c rename ports/unix/{btstack_usb.c => mpbtstackport_usb.c} (54%) diff --git a/drivers/cyw43/cywbt.c b/drivers/cyw43/cywbt.c index 79318f4483ba4..defe670683ffd 100644 --- a/drivers/cyw43/cywbt.c +++ b/drivers/cyw43/cywbt.c @@ -31,13 +31,19 @@ #include "py/mphal.h" #include "pin_static_af.h" #include "uart.h" -#include "extmod/modbluetooth_hci.h" +#include "extmod/mpbthci.h" #if MICROPY_PY_NETWORK_CYW43 extern const char fw_4343WA1_7_45_98_50_start; #define CYWBT_FW_ADDR (&fw_4343WA1_7_45_98_50_start + 749 * 512 + 29 * 256) +// Provided by the port. +extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj; + +// Provided by the port, and also possibly shared with the stack. +extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; + /******************************************************************************/ // CYW BT HCI low-level driver @@ -168,10 +174,6 @@ int mp_bluetooth_hci_controller_init(void) { mp_hal_pin_config(pyb_pin_WL_GPIO_4, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0); // RF-switch power mp_hal_pin_high(pyb_pin_WL_GPIO_4); // Turn the RF-switch on - return 0; -} - -int mp_bluetooth_hci_controller_activate(void) { uint8_t buf[256]; mp_hal_pin_low(pyb_pin_BT_REG_ON); @@ -219,7 +221,7 @@ int mp_bluetooth_hci_controller_activate(void) { return 0; } -int mp_bluetooth_hci_controller_deactivate(void) { +int mp_bluetooth_hci_controller_deinit(void) { mp_hal_pin_low(pyb_pin_BT_REG_ON); return 0; diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index dd96e6337943e..3084601b85415 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -38,6 +38,17 @@ CFLAGS += $(shell pkg-config libusb-1.0 --cflags) LDFLAGS += $(shell pkg-config libusb-1.0 --libs) endif +ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) +SRC_BTSTACK += \ + lib/btstack/src/hci_transport_h4.c \ + lib/btstack/chipset/zephyr/btstack_chipset_zephyr.c + +EXTMOD_SRC_C += \ + extmod/btstack/btstack_hci_uart.c \ + +CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK_H4=1 +endif + ifeq ($(MICROPY_BLUETOOTH_BTSTACK_ENABLE_CLASSIC),1) include $(BTSTACK_DIR)/src/classic/Makefile.inc SRC_BTSTACK += \ diff --git a/extmod/btstack/btstack_config.h b/extmod/btstack/btstack_config.h index f420f47a5bee5..e56a84f94a16d 100644 --- a/extmod/btstack/btstack_config.h +++ b/extmod/btstack/btstack_config.h @@ -8,6 +8,7 @@ // #define ENABLE_CLASSIC #define ENABLE_LE_DATA_CHANNELS // #define ENABLE_LOG_INFO +// #define ENABLE_LOG_DEBUG #define ENABLE_LOG_ERROR // BTstack configuration. buffers, sizes, ... diff --git a/ports/stm32/btstack.c b/extmod/btstack/btstack_hci_uart.c similarity index 67% rename from ports/stm32/btstack.c rename to extmod/btstack/btstack_hci_uart.c index cbb15a86cb972..5c96e02dc0fed 100644 --- a/ports/stm32/btstack.c +++ b/extmod/btstack/btstack_hci_uart.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2020 Damien P. George + * Copyright (c) 2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,12 +32,14 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK #include "lib/btstack/src/btstack.h" -#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" -#include "lib/btstack/platform/embedded/hal_cpu.h" -#include "lib/btstack/platform/embedded/hal_time_ms.h" -#include "extmod/modbluetooth_hci.h" -#include "extmod/btstack/modbluetooth_btstack.h" +#include "extmod/mpbthci.h" +#include "extmod/btstack/btstack_hci_uart.h" + +#include "mpbtstackport.h" + +// Implements a btstack btstack_uart_block_t on top of the mphciuart.h +// interface to an HCI UART provided by the port. // We pass the bytes directly to the UART during a send, but then notify btstack in the next poll. STATIC bool send_done; @@ -48,42 +51,29 @@ STATIC size_t recv_len; STATIC size_t recv_idx; STATIC void (*recv_handler)(void); -// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the -// following three functions are empty. - -void hal_cpu_disable_irqs(void) { -} - -void hal_cpu_enable_irqs(void) { -} - -void hal_cpu_enable_irqs_and_sleep(void) { -} - -uint32_t hal_time_ms(void) { - return mp_hal_ticks_ms(); -} - STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) { + (void)uart_config; + send_done = false; recv_len = 0; recv_idx = 0; recv_handler = NULL; send_handler = NULL; - // Set up the UART periperhal. - mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID); + // Set up the UART peripheral, attach IRQ and power up the HCI controller. + // We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate. + mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0); + mp_bluetooth_hci_controller_init(); return 0; } STATIC int btstack_uart_open(void) { - // Attach IRQ and power up the HCI controller. - mp_bluetooth_hci_uart_activate(); return 0; } STATIC int btstack_uart_close(void) { + mp_bluetooth_hci_controller_deinit(); return 0; } @@ -101,10 +91,12 @@ STATIC int btstack_uart_set_baudrate(uint32_t baudrate) { } STATIC int btstack_uart_set_parity(int parity) { + (void)parity; return 0; } STATIC int btstack_uart_set_flowcontrol(int flowcontrol) { + (void)flowcontrol; return 0; } @@ -123,14 +115,16 @@ STATIC int btstack_uart_get_supported_sleep_modes(void) { } STATIC void btstack_uart_set_sleep(btstack_uart_sleep_mode_t sleep_mode) { + (void)sleep_mode; // printf("btstack_uart_set_sleep %u\n", sleep_mode); } STATIC void btstack_uart_set_wakeup_handler(void (*wakeup_handler)(void)) { + (void)wakeup_handler; // printf("btstack_uart_set_wakeup_handler\n"); } -STATIC const btstack_uart_block_t btstack_uart_block = { +const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = { &btstack_uart_init, &btstack_uart_open, &btstack_uart_close, @@ -146,15 +140,9 @@ STATIC const btstack_uart_block_t btstack_uart_block = { &btstack_uart_set_wakeup_handler, }; -STATIC const hci_transport_config_uart_t hci_transport_config_uart = { - HCI_TRANSPORT_CONFIG_UART, - MICROPY_HW_BLE_UART_BAUDRATE, - 3000000, - 0, - NULL, -}; +void mp_bluetooth_btstack_hci_uart_process(void) { + bool host_wake = mp_bluetooth_hci_controller_woken(); -STATIC void btstack_uart_process(void) { if (send_done) { // If we'd done a TX in the last interval, notify btstack that it's complete. send_done = false; @@ -176,48 +164,10 @@ STATIC void btstack_uart_process(void) { } } } -} - -void mp_bluetooth_hci_poll(void) { - if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_OFF) { - return; - } - // Process uart data. - bool host_wake = mp_bluetooth_hci_controller_woken(); - btstack_uart_process(); if (host_wake) { mp_bluetooth_hci_controller_sleep_maybe(); } - - // Call the BTstack run loop. - btstack_run_loop_embedded_execute_once(); -} - -void mp_bluetooth_btstack_port_init(void) { - static bool run_loop_init = false; - if (!run_loop_init) { - run_loop_init = true; - btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); - } else { - btstack_run_loop_embedded_get_instance()->init(); - } - - // hci_dump_open(NULL, HCI_DUMP_STDOUT); - const hci_transport_t *transport = hci_transport_h4_instance(&btstack_uart_block); - hci_init(transport, &hci_transport_config_uart); - - // TODO: Probably not necessary for BCM (we have our own firmware loader), - // but might be worth investigating for other controllers in the future. - // hci_set_chipset(btstack_chipset_bcm_instance()); -} - -void mp_bluetooth_btstack_port_deinit(void) { - hci_power_control(HCI_POWER_OFF); -} - -void mp_bluetooth_btstack_port_start(void) { - hci_power_control(HCI_POWER_ON); } #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK diff --git a/extmod/nimble/nimble/nimble_hci_uart.h b/extmod/btstack/btstack_hci_uart.h similarity index 66% rename from extmod/nimble/nimble/nimble_hci_uart.h rename to extmod/btstack/btstack_hci_uart.h index 646a12dac1460..8011e587dee3f 100644 --- a/extmod/nimble/nimble/nimble_hci_uart.h +++ b/extmod/btstack/btstack_hci_uart.h @@ -3,7 +3,8 @@ * * The MIT License (MIT) * - * Copyright (c) 2019 Damien P. George + * Copyright (c) 2020 Damien P. George + * Copyright (c) 2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,18 +24,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H -#define MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H -// Extensions to extmod/modbluetooth_hci.h specific to NimBLE. +#ifndef MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H +#define MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H -#include "extmod/nimble/hal/hal_uart.h" +#include "lib/btstack/src/btstack.h" -// Helpers called from ports. -void mp_bluetooth_nimble_hci_uart_process(void); +// --- Used by the port to create the HCI transport --------------------------- +extern const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block; -// Must be provided by the port. -void mp_bluetooth_nimble_hci_uart_rx(hal_uart_rx_cb_t rx_cb, void *rx_arg); -void mp_bluetooth_nimble_hci_uart_tx_strn(const char *str, uint len); +// --- Called by the MicroPython port when UART data is available ------------- +void mp_bluetooth_btstack_hci_uart_process(void); -#endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H +#endif // MICROPY_INCLUDED_EXTMOD_BTSTACK_MODBLUETOOTH_BTSTACK_H diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 9aef7e6879fb0..03ec12b734346 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -53,6 +53,8 @@ STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3; volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; +#define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV + STATIC int btstack_error_to_errno(int err) { DEBUG_printf(" --> btstack error: %d\n", err); if (err == ERROR_CODE_SUCCESS) { @@ -300,6 +302,9 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t if (state == HCI_STATE_WORKING) { // Signal that initialisation has completed. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE; + } else if (state == HCI_STATE_HALTING) { + // Signal that de-initialisation has begun. + mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_HALTING; } else if (state == HCI_STATE_OFF) { // Signal that de-initialisation has completed. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; diff --git a/extmod/btstack/modbluetooth_btstack.h b/extmod/btstack/modbluetooth_btstack.h index 2fad86f22605e..7890bbfae2c4a 100644 --- a/extmod/btstack/modbluetooth_btstack.h +++ b/extmod/btstack/modbluetooth_btstack.h @@ -56,6 +56,7 @@ enum { MP_BLUETOOTH_BTSTACK_STATE_OFF, MP_BLUETOOTH_BTSTACK_STATE_STARTING, MP_BLUETOOTH_BTSTACK_STATE_ACTIVE, + MP_BLUETOOTH_BTSTACK_STATE_HALTING, MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT, }; diff --git a/extmod/mpbthci.c b/extmod/mpbthci.c new file mode 100644 index 0000000000000..79a865424233d --- /dev/null +++ b/extmod/mpbthci.c @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Default definitions in case a controller doesn't implement this. + +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH + +#define DEBUG_printf(...) // printf(__VA_ARGS__) + +#include "extmod/mpbthci.h" + + +#endif // MICROPY_PY_BLUETOOTH diff --git a/extmod/modbluetooth_hci.h b/extmod/mpbthci.h similarity index 71% rename from extmod/modbluetooth_hci.h rename to extmod/mpbthci.h index d6b432d224710..acb5b832bab11 100644 --- a/extmod/modbluetooth_hci.h +++ b/extmod/mpbthci.h @@ -24,15 +24,15 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H -#define MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H +#ifndef MICROPY_INCLUDED_EXTMOD_MPBTHCI_H +#define MICROPY_INCLUDED_EXTMOD_MPBTHCI_H -#include "uart.h" +// --- Optionally can be implemented by the driver. --------------------------- -// Optionally can be implemented by the driver. +// Start/stop the HCI controller. +// Requires the UART to this HCI controller is available. int mp_bluetooth_hci_controller_init(void); -int mp_bluetooth_hci_controller_activate(void); -int mp_bluetooth_hci_controller_deactivate(void); +int mp_bluetooth_hci_controller_deinit(void); // Tell the controller to go to sleep (e.g. on RX if we don't think we're expecting anything more). int mp_bluetooth_hci_controller_sleep_maybe(void); @@ -41,16 +41,11 @@ bool mp_bluetooth_hci_controller_woken(void); // Wake up the controller (e.g. we're about to TX). int mp_bluetooth_hci_controller_wakeup(void); -// Storage and bindings that need to be implemented by the port. -// These are used by the stack bindings (e.g. nimble/hal_uart.c) -// as well as potentially the driver (e.g. cywbt.c). -extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; -extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj; - -int mp_bluetooth_hci_uart_init(uint32_t port); -int mp_bluetooth_hci_uart_activate(void); +// --- Bindings that need to be implemented by the port. ---------------------- +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate); +int mp_bluetooth_hci_uart_deinit(void); int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate); int mp_bluetooth_hci_uart_readchar(void); int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len); -#endif // MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H +#endif // MICROPY_INCLUDED_EXTMOD_MPBTHCI_H diff --git a/extmod/nimble/hal/hal_uart.c b/extmod/nimble/hal/hal_uart.c index fba82b830438a..c6d0850fea993 100644 --- a/extmod/nimble/hal/hal_uart.c +++ b/extmod/nimble/hal/hal_uart.c @@ -26,11 +26,9 @@ #include "py/runtime.h" #include "py/mphal.h" -#include "pin_static_af.h" #include "nimble/ble.h" #include "extmod/nimble/hal/hal_uart.h" -#include "extmod/modbluetooth_hci.h" -#include "extmod/nimble/nimble/nimble_hci_uart.h" +#include "extmod/mpbthci.h" #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE @@ -39,6 +37,9 @@ static void *hal_uart_tx_arg; static hal_uart_rx_cb_t hal_uart_rx_cb; static void *hal_uart_rx_arg; +// Provided by the port, and also possibly shared with the driver. +extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; + int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_uart_rx_cb_t rx_cb, void *rx_arg) { hal_uart_tx_cb = tx_cb; hal_uart_tx_arg = tx_arg; @@ -48,9 +49,7 @@ int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_u } int hal_uart_config(uint32_t port, uint32_t baudrate, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow) { - mp_bluetooth_hci_uart_init(port); - mp_bluetooth_hci_uart_set_baudrate(baudrate); - return mp_bluetooth_hci_uart_activate(); + return mp_bluetooth_hci_uart_init(port, baudrate); } void hal_uart_start_tx(uint32_t port) { @@ -71,7 +70,7 @@ void hal_uart_start_tx(uint32_t port) { printf("\n"); #endif - mp_bluetooth_nimble_hci_uart_tx_strn((void*)mp_bluetooth_hci_cmd_buf, len); + mp_bluetooth_hci_uart_write(mp_bluetooth_hci_cmd_buf, len); } int hal_uart_close(uint32_t port) { @@ -79,7 +78,17 @@ int hal_uart_close(uint32_t port) { } void mp_bluetooth_nimble_hci_uart_process(void) { - mp_bluetooth_nimble_hci_uart_rx(hal_uart_rx_cb, hal_uart_rx_arg); + bool host_wake = mp_bluetooth_hci_controller_woken(); + + int chr; + while ((chr = mp_bluetooth_hci_uart_readchar()) >= 0) { + // printf("UART RX: %02x\n", data); + hal_uart_rx_cb(hal_uart_rx_arg, chr); + } + + if (host_wake) { + mp_bluetooth_hci_controller_sleep_maybe(); + } } #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/extmod/nimble/hal/hal_uart.h b/extmod/nimble/hal/hal_uart.h index 0ef04bc81e026..1ff1c17436dae 100644 --- a/extmod/nimble/hal/hal_uart.h +++ b/extmod/nimble/hal/hal_uart.h @@ -1,3 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_HAL_HAL_UART_H #define MICROPY_INCLUDED_EXTMOD_NIMBLE_HAL_HAL_UART_H @@ -10,10 +36,13 @@ typedef int (*hal_uart_tx_cb_t)(void *arg); typedef int (*hal_uart_rx_cb_t)(void *arg, uint8_t data); -// Called by NimBLE, implemented in hal_uart.c. +// --- Called by NimBLE, implemented in hal_uart.c. --------------------------- int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_uart_rx_cb_t rx_cb, void *rx_arg); int hal_uart_config(uint32_t port, uint32_t baud, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow); void hal_uart_start_tx(uint32_t port); int hal_uart_close(uint32_t port); +// --- Called by the MicroPython port when UART data is available ------------- +void mp_bluetooth_nimble_hci_uart_process(void); + #endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_HAL_HAL_UART_H diff --git a/extmod/nimble/logcfg/logcfg.h b/extmod/nimble/logcfg/logcfg.h new file mode 100644 index 0000000000000..e9023dae65b5d --- /dev/null +++ b/extmod/nimble/logcfg/logcfg.h @@ -0,0 +1,45 @@ +/** + * This file was generated by Apache newt version: 1.8.0-dev + */ + +#ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_LOGCFG_LOGCFG_H +#define MICROPY_INCLUDED_EXTMOD_NIMBLE_LOGCFG_LOGCFG_H + +#include "py/mphal.h" +#include "modlog/modlog.h" +#include "log_common/log_common.h" + +#define MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING (1) + +#if MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING +#define DFLT_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__) +#else +#define DFLT_LOG_DEBUG(...) IGNORE(__VA_ARGS__) +#endif + +#if MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING > 1 +#define BLE_HS_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__) +#else +#define BLE_HS_LOG_DEBUG(...) IGNORE(__VA_ARGS__) +#endif + +#define BLE_HS_LOG_INFO(...) MODLOG_INFO(4, __VA_ARGS__) +#define BLE_HS_LOG_WARN(...) MODLOG_WARN(4, __VA_ARGS__) +#define BLE_HS_LOG_ERROR(...) MODLOG_ERROR(4, __VA_ARGS__) +#define BLE_HS_LOG_CRITICAL(...) MODLOG_CRITICAL(4, __VA_ARGS__) +#define BLE_HS_LOG_DISABLED(...) MODLOG_DISABLED(4, __VA_ARGS__) + +#define DFLT_LOG_INFO(...) MODLOG_INFO(0, __VA_ARGS__) +#define DFLT_LOG_WARN(...) MODLOG_WARN(0, __VA_ARGS__) +#define DFLT_LOG_ERROR(...) MODLOG_ERROR(0, __VA_ARGS__) +#define DFLT_LOG_CRITICAL(...) MODLOG_CRITICAL(0, __VA_ARGS__) +#define DFLT_LOG_DISABLED(...) MODLOG_DISABLED(0, __VA_ARGS__) + +#define MFG_LOG_DEBUG(...) IGNORE(__VA_ARGS__) +#define MFG_LOG_INFO(...) IGNORE(__VA_ARGS__) +#define MFG_LOG_WARN(...) IGNORE(__VA_ARGS__) +#define MFG_LOG_ERROR(...) IGNORE(__VA_ARGS__) +#define MFG_LOG_CRITICAL(...) IGNORE(__VA_ARGS__) +#define MFG_LOG_DISABLED(...) MODLOG_DISABLED(128, __VA_ARGS__) + +#endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_LOGCFG_LOGCFG_H diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 36bfa6cccfa61..61ba3a3aba696 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -33,6 +33,7 @@ #include "extmod/nimble/modbluetooth_nimble.h" #include "extmod/modbluetooth.h" +#include "extmod/mpbthci.h" #include "host/ble_hs.h" #include "host/util/util.h" @@ -64,10 +65,11 @@ STATIC int8_t ble_hs_err_to_errno_table[] = { }; STATIC int ble_hs_err_to_errno(int err) { + DEBUG_printf("ble_hs_err_to_errno: %d\n", err); if (!err) { return 0; } - if (0 <= err && err < MP_ARRAY_SIZE(ble_hs_err_to_errno_table) && ble_hs_err_to_errno_table[err]) { + if (err >= 0 && (unsigned)err < MP_ARRAY_SIZE(ble_hs_err_to_errno_table) && ble_hs_err_to_errno_table[err]) { return ble_hs_err_to_errno_table[err]; } else { return MP_EIO; @@ -150,6 +152,12 @@ STATIC void sync_cb(void) { int rc; ble_addr_t addr; + DEBUG_printf("sync_cb: state=%d\n", mp_bluetooth_nimble_ble_state); + + if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { + return; + } + rc = ble_hs_util_ensure_addr(0); // prefer public address if (rc != 0) { // https://mynewt.apache.org/latest/tutorials/ble/eddystone.html#configure-the-nimble-stack-with-an-address @@ -264,11 +272,67 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { return 0; } +#if !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY + +// On ports such as ESP32 where we only implement the bindings, then +// the port must provide these functions. +// But for STM32 / Unix-H4, we provide a default implementation of the +// port-specific functionality. +// TODO: In the future if a port ever needs to customise these functions +// then investigate using MP_WEAK or splitting them out to another .c file. + +#include "transport/uart/ble_hci_uart.h" + +void mp_bluetooth_nimble_port_hci_init(void) { + DEBUG_printf("mp_bluetooth_nimble_port_hci_init (nimble default)\n"); + // This calls mp_bluetooth_hci_uart_init (via ble_hci_uart_init --> hal_uart_config --> mp_bluetooth_hci_uart_init). + ble_hci_uart_init(); + mp_bluetooth_hci_controller_init(); +} + +void mp_bluetooth_nimble_port_hci_deinit(void) { + DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit (nimble default)\n"); + mp_bluetooth_hci_controller_deinit(); + mp_bluetooth_hci_uart_deinit(); +} + +void mp_bluetooth_nimble_port_start(void) { + DEBUG_printf("mp_bluetooth_nimble_port_start (nimble default)\n"); + // By default, assume port is already running its own background task (e.g. SysTick on STM32). + // ESP32 runs a FreeRTOS task, Unix has a thread. +} + +// Called when the host stop procedure has completed. +STATIC void ble_hs_shutdown_stop_cb(int status, void *arg) { + (void)status; + (void)arg; + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; +} + +STATIC struct ble_hs_stop_listener ble_hs_shutdown_stop_listener; + +void mp_bluetooth_nimble_port_shutdown(void) { + DEBUG_printf("mp_bluetooth_nimble_port_shutdown (nimble default)\n"); + // By default, just call ble_hs_stop directly and wait for the stack to stop. + + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STOPPING; + + ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb, NULL); + + while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { + MICROPY_EVENT_POLL_HOOK + } +} + +#endif // !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY + int mp_bluetooth_init(void) { DEBUG_printf("mp_bluetooth_init\n"); // Clean up if necessary. mp_bluetooth_deinit(); + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STARTING; + ble_hs_cfg.reset_cb = reset_cb; ble_hs_cfg.sync_cb = sync_cb; ble_hs_cfg.gatts_register_cb = gatts_register_cb; @@ -277,56 +341,70 @@ int mp_bluetooth_init(void) { MP_STATE_PORT(bluetooth_nimble_root_pointers) = m_new0(mp_bluetooth_nimble_root_pointers_t, 1); mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db); - mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STARTING; + #if !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY + // Dereference any previous NimBLE mallocs. + MP_STATE_PORT(bluetooth_nimble_memory) = NULL; + #endif - mp_bluetooth_nimble_port_preinit(); + // Allow port (ESP32) to override NimBLE's HCI init. + // Otherwise default implementation above calls ble_hci_uart_init(). + mp_bluetooth_nimble_port_hci_init(); + + // Initialise NimBLE memory and data structures. nimble_port_init(); - mp_bluetooth_nimble_port_postinit(); // By default, just register the default gap/gatt service. ble_svc_gap_init(); ble_svc_gatt_init(); + // Make sure that the HCI UART and event handling task is running. mp_bluetooth_nimble_port_start(); + // Static initialization is complete, can start processing events. + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC; + // Wait for sync callback while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { MICROPY_EVENT_POLL_HOOK } + DEBUG_printf("mp_bluetooth_init: ready\n"); return 0; } -// Called when the host stop procedure has completed. -STATIC void ble_hs_shutdown_stop_cb(int status, void *arg) { - mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; -} - -STATIC struct ble_hs_stop_listener ble_hs_shutdown_stop_listener; - void mp_bluetooth_deinit(void) { DEBUG_printf("mp_bluetooth_deinit\n"); if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { return; } - mp_bluetooth_gap_advertise_stop(); - #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE - mp_bluetooth_gap_scan_stop(); - #endif - - mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STOPPING; + // Must call ble_hs_stop() in a port-specific way to stop the background + // task. Default implementation provided above. + if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { + mp_bluetooth_gap_advertise_stop(); + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + mp_bluetooth_gap_scan_stop(); + #endif - ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb, NULL); + DEBUG_printf("mp_bluetooth_deinit: starting port shutdown\n"); - while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { - MICROPY_EVENT_POLL_HOOK + mp_bluetooth_nimble_port_shutdown(); + assert(mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF); + } else { + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; } - mp_bluetooth_nimble_port_deinit(); + // Shutdown the HCI controller. + mp_bluetooth_nimble_port_hci_deinit(); MP_STATE_PORT(bluetooth_nimble_root_pointers) = NULL; + + #if !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY + // Dereference any previous NimBLE mallocs. + MP_STATE_PORT(bluetooth_nimble_memory) = NULL; + #endif + DEBUG_printf("mp_bluetooth_deinit: shut down\n"); } @@ -485,7 +563,7 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { if (!append) { // Unref any previous service definitions. - for (int i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) { + for (size_t i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) { MP_STATE_PORT(bluetooth_nimble_root_pointers)->services[i] = NULL; } MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0; diff --git a/extmod/nimble/modbluetooth_nimble.h b/extmod/nimble/modbluetooth_nimble.h index f44e1d69dca52..7e401781e7f9c 100644 --- a/extmod/nimble/modbluetooth_nimble.h +++ b/extmod/nimble/modbluetooth_nimble.h @@ -43,15 +43,27 @@ typedef struct _mp_bluetooth_nimble_root_pointers_t { enum { MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF, MP_BLUETOOTH_NIMBLE_BLE_STATE_STARTING, + MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC, MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE, MP_BLUETOOTH_NIMBLE_BLE_STATE_STOPPING, }; extern volatile int mp_bluetooth_nimble_ble_state; -void mp_bluetooth_nimble_port_preinit(void); -void mp_bluetooth_nimble_port_postinit(void); -void mp_bluetooth_nimble_port_deinit(void); +// --- Optionally provided by the MicroPython port. --------------------------- +// (default implementations provided by modbluetooth_nimble.c) + +// Tell the port to init the UART and start the HCI controller. +void mp_bluetooth_nimble_port_hci_init(void); + +// Tell the port to deinit the UART and shutdown the HCI controller. +void mp_bluetooth_nimble_port_hci_deinit(void); + +// Tell the port to run its background task (i.e. poll the UART and pump events). void mp_bluetooth_nimble_port_start(void); +// Tell the port to stop its background task. +void mp_bluetooth_nimble_port_shutdown(void); + + #endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_MODBLUETOOTH_NIMBLE_H diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index 4e1642c98a998..f8a68bc6f52b6 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -2,16 +2,19 @@ ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) -EXTMOD_SRC_C += extmod/nimble/modbluetooth_nimble.c +EXTMOD_DIR = extmod +NIMBLE_EXTMOD_DIR = $(EXTMOD_DIR)/nimble -CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1 +EXTMOD_SRC_C += $(NIMBLE_EXTMOD_DIR)/modbluetooth_nimble.c -NIMBLE_EXTMOD_DIR = extmod/nimble +CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1 # Use NimBLE from the submodule in lib/mynewt-nimble by default, # allowing a port to use their own system version (e.g. ESP32). MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY ?= 0 +CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=$(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY) + ifeq ($(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY),0) NIMBLE_LIB_DIR = lib/mynewt-nimble @@ -82,7 +85,7 @@ LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ ) EXTMOD_SRC_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \ - nimble/npl_os.c \ + nimble/nimble_npl_os.c \ hal/hal_uart.c \ ) @@ -98,7 +101,7 @@ INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/include INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/transport/uart/include INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/porting/nimble/include -$(BUILD)/$(NIMBLE_LIB_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format +$(BUILD)/$(NIMBLE_LIB_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format -Wno-sign-compare endif diff --git a/extmod/nimble/nimble/npl_os.c b/extmod/nimble/nimble/nimble_npl_os.c similarity index 85% rename from extmod/nimble/nimble/npl_os.c rename to extmod/nimble/nimble/nimble_npl_os.c index 20c260405444f..ba3031a8ff694 100644 --- a/extmod/nimble/nimble/npl_os.c +++ b/extmod/nimble/nimble/nimble_npl_os.c @@ -29,16 +29,18 @@ #include "py/runtime.h" #include "nimble/ble.h" #include "nimble/nimble_npl.h" -#include "nimble/nimble_hci_uart.h" +#include "extmod/nimble/hal/hal_uart.h" -#define DEBUG_OS_printf(...) //printf(__VA_ARGS__) -#define DEBUG_MALLOC_printf(...) //printf(__VA_ARGS__) -#define DEBUG_EVENT_printf(...) //printf(__VA_ARGS__) -#define DEBUG_MUTEX_printf(...) //printf(__VA_ARGS__) -#define DEBUG_SEM_printf(...) //printf(__VA_ARGS__) -#define DEBUG_CALLOUT_printf(...) //printf(__VA_ARGS__) -#define DEBUG_TIME_printf(...) //printf(__VA_ARGS__) -#define DEBUG_CRIT_printf(...) //printf(__VA_ARGS__) +#include "extmod/modbluetooth.h" + +#define DEBUG_OS_printf(...) // printf(__VA_ARGS__) +#define DEBUG_MALLOC_printf(...) // printf(__VA_ARGS__) +#define DEBUG_EVENT_printf(...) // printf(__VA_ARGS__) +#define DEBUG_MUTEX_printf(...) // printf(__VA_ARGS__) +#define DEBUG_SEM_printf(...) // printf(__VA_ARGS__) +#define DEBUG_CALLOUT_printf(...) // printf(__VA_ARGS__) +#define DEBUG_TIME_printf(...) // printf(__VA_ARGS__) +#define DEBUG_CRIT_printf(...) // printf(__VA_ARGS__) bool ble_npl_os_started(void) { DEBUG_OS_printf("ble_npl_os_started\n"); @@ -138,7 +140,7 @@ int nimble_sprintf(char *str, const char *fmt, ...) { struct ble_npl_eventq *global_eventq = NULL; -void os_eventq_run_all(void) { +void mp_bluetooth_nimble_os_eventq_run_all(void) { for (struct ble_npl_eventq *evq = global_eventq; evq != NULL; evq = evq->nextq) { while (evq->head != NULL) { struct ble_npl_event *ev = evq->head; @@ -238,23 +240,39 @@ ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens) { ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout) { DEBUG_SEM_printf("ble_npl_sem_pend(%p, %u) count=%u\n", sem, (uint)timeout, (uint)sem->count); + + // This is called by NimBLE to synchronously wait for an HCI ACK. The + // corresponding ble_npl_sem_release is called directly by the UART rx + // handler (i.e. hal_uart_rx_cb in extmod/nimble/hal/hal_uart.c). + + // So this implementation just polls the UART until either the semaphore + // is released, or the timeout occurs. + if (sem->count == 0) { uint32_t t0 = mp_hal_ticks_ms(); while (sem->count == 0 && mp_hal_ticks_ms() - t0 < timeout) { - // This function may be called at thread-level, so execute - // mp_bluetooth_nimble_hci_uart_process at raised priority. + // This can be called either from code running in NimBLE's "task" + // (i.e. PENDSV) or directly by application code, so for the + // latter case, prevent the "task" from running while we poll the + // UART directly. MICROPY_PY_BLUETOOTH_ENTER mp_bluetooth_nimble_hci_uart_process(); MICROPY_PY_BLUETOOTH_EXIT + if (sem->count != 0) { break; } - __WFI(); + + // Because we're polling, might as well wait for a UART IRQ indicating + // more data available. + mp_bluetooth_nimble_hci_uart_wfi(); } + if (sem->count == 0) { - printf("timeout\n"); + printf("NimBLE: HCI ACK timeout\n"); return BLE_NPL_TIMEOUT; } + DEBUG_SEM_printf("got response in %u ms\n", (int)(mp_hal_ticks_ms() - t0)); } sem->count -= 1; @@ -277,7 +295,7 @@ uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem) { static struct ble_npl_callout *global_callout = NULL; -void os_callout_process(void) { +void mp_bluetooth_nimble_os_callout_process(void) { uint32_t tnow = mp_hal_ticks_ms(); for (struct ble_npl_callout *c = global_callout; c != NULL; c = c->nextc) { if (!c->active) { @@ -386,12 +404,24 @@ void ble_npl_time_delay(ble_npl_time_t ticks) { /******************************************************************************/ // CRITICAL +// This is used anywhere NimBLE modifies global data structures. +// We need to protect between: +// - A MicroPython VM thread. +// - The NimBLE "task" (e.g. PENDSV on STM32, pthread on Unix). +// On STM32, by disabling PENDSV, we ensure that either: +// - If we're in the NimBLE task, we're exclusive anyway. +// - If we're in a VM thread, we can't be interrupted by the NimBLE task, or switched to another thread. +// On Unix, there's a global mutex. + +// TODO: Both ports currently use MICROPY_PY_BLUETOOTH_ENTER in their implementation, +// maybe this doesn't need to be port-specific? + uint32_t ble_npl_hw_enter_critical(void) { DEBUG_CRIT_printf("ble_npl_hw_enter_critical()\n"); - return raise_irq_pri(15); + return mp_bluetooth_nimble_hci_uart_enter_critical(); } void ble_npl_hw_exit_critical(uint32_t ctx) { DEBUG_CRIT_printf("ble_npl_hw_exit_critical(%u)\n", (uint)ctx); - restore_irq_pri(ctx); + mp_bluetooth_nimble_hci_uart_exit_critical(ctx); } diff --git a/extmod/nimble/nimble/nimble_npl_os.h b/extmod/nimble/nimble/nimble_npl_os.h index 3d886fedb58fd..55425206612ae 100644 --- a/extmod/nimble/nimble/nimble_npl_os.h +++ b/extmod/nimble/nimble/nimble_npl_os.h @@ -24,11 +24,15 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H -#define MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H +#ifndef MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NIMBLE_NPL_OS_H +#define MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NIMBLE_NPL_OS_H + +// This is included by nimble/nimble_npl.h -- include that rather than this file directly. #include +// --- Configuration of NimBLE data structures -------------------------------- + #define BLE_NPL_OS_ALIGNMENT (4) #define BLE_NPL_TIME_FOREVER (0xffffffff) @@ -63,4 +67,15 @@ struct ble_npl_sem { volatile uint16_t count; }; +// --- Called by the MicroPython port ----------------------------------------- + +void mp_bluetooth_nimble_os_eventq_run_all(void); +void mp_bluetooth_nimble_os_callout_process(void); + +// --- Must be provided by the MicroPython port ------------------------------- + +void mp_bluetooth_nimble_hci_uart_wfi(void); +uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void); +void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state); + #endif // MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H diff --git a/extmod/nimble/syscfg/syscfg.h b/extmod/nimble/syscfg/syscfg.h index 6ac420f35e550..8a6f2338be2ab 100644 --- a/extmod/nimble/syscfg/syscfg.h +++ b/extmod/nimble/syscfg/syscfg.h @@ -2,12 +2,14 @@ #define MICROPY_INCLUDED_EXTMOD_NIMBLE_SYSCFG_H #include "py/mphal.h" -#include "uart.h" + +#include "mpnimbleport.h" void *nimble_malloc(size_t size); void nimble_free(void *ptr); void *nimble_realloc(void *ptr, size_t size); +// Redirect NimBLE malloc to the GC heap. #define malloc(size) nimble_malloc(size) #define free(ptr) nimble_free(ptr) #define realloc(ptr, size) nimble_realloc(ptr, size) @@ -88,6 +90,7 @@ int nimble_sprintf(char *str, const char *fmt, ...); #define MYNEWT_VAL_BLE_GATT_WRITE_NO_RSP (MYNEWT_VAL_BLE_ROLE_CENTRAL) #define MYNEWT_VAL_BLE_GATT_WRITE_RELIABLE (MYNEWT_VAL_BLE_ROLE_CENTRAL) #define MYNEWT_VAL_BLE_HOST (1) +#define MYNEWT_VAL_BLE_HS_AUTO_START (1) #define MYNEWT_VAL_BLE_HS_DEBUG (0) #define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0) #define MYNEWT_VAL_BLE_HS_FLOW_CTRL_ITVL (1000) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 2cbe9f6be57f9..dcf4110cfc913 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -341,7 +341,7 @@ SRC_C = \ modnetwork.c \ network_lan.c \ network_ppp.c \ - nimble.c \ + mpnimbleport.c \ modsocket.c \ modesp.c \ esp32_partition.c \ diff --git a/ports/esp32/nimble.c b/ports/esp32/mpnimbleport.c similarity index 66% rename from ports/esp32/nimble.c rename to ports/esp32/mpnimbleport.c index 16829732c30e5..a58fcbdbf49f4 100644 --- a/ports/esp32/nimble.c +++ b/ports/esp32/mpnimbleport.c @@ -30,28 +30,48 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE +#define DEBUG_printf(...) // printf("nimble (esp32): " __VA_ARGS__) + #include "esp_nimble_hci.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" +#include "extmod/nimble/modbluetooth_nimble.h" + STATIC void ble_host_task(void *param) { + DEBUG_printf("ble_host_task\n"); nimble_port_run(); // This function will return only when nimble_port_stop() is executed. nimble_port_freertos_deinit(); } -void mp_bluetooth_nimble_port_preinit(void) { +void mp_bluetooth_nimble_port_hci_init(void) { + DEBUG_printf("mp_bluetooth_nimble_port_hci_init\n"); esp_nimble_hci_and_controller_init(); } -void mp_bluetooth_nimble_port_postinit(void) { -} +void mp_bluetooth_nimble_port_hci_deinit(void) { + DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit\n"); -void mp_bluetooth_nimble_port_deinit(void) { - nimble_port_stop(); + esp_nimble_hci_and_controller_deinit(); } void mp_bluetooth_nimble_port_start(void) { + DEBUG_printf("mp_bluetooth_nimble_port_start\n"); nimble_port_freertos_init(ble_host_task); } +void mp_bluetooth_nimble_port_shutdown(void) { + DEBUG_printf("mp_bluetooth_nimble_port_shutdown\n"); + + // Despite the name, these is an ESP32-specific (no other NimBLE ports have these functions). + // Calls ble_hs_stop() and waits for stack shutdown. + nimble_port_stop(); + + // Shuts down the event queue. + nimble_port_deinit(); + + // Mark stack as shutdown. + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; +} + #endif diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index e7d2e2abc2caf..7b68299e04f70 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -480,7 +480,7 @@ endif ifeq ($(MICROPY_PY_BLUETOOTH),1) -SRC_C += modbluetooth_hci.c +SRC_C += mpbthciport.c ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) @@ -490,12 +490,13 @@ endif ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) include $(TOP)/extmod/nimble/nimble.mk -SRC_C += nimble.c +SRC_C += mpnimbleport.c endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 include $(TOP)/extmod/btstack/btstack.mk -SRC_C += btstack.c +SRC_C += mpbtstackport.c endif ifeq ($(MICROPY_PY_NETWORK_CYW43),1) diff --git a/ports/stm32/modbluetooth_hci.c b/ports/stm32/mpbthciport.c similarity index 75% rename from ports/stm32/modbluetooth_hci.c rename to ports/stm32/mpbthciport.c index 54c60adc01181..71a9e4fc7f433 100644 --- a/ports/stm32/modbluetooth_hci.c +++ b/ports/stm32/mpbthciport.c @@ -26,17 +26,20 @@ #include "py/runtime.h" #include "py/mphal.h" -#include "extmod/modbluetooth_hci.h" +#include "extmod/mpbthci.h" #include "systick.h" #include "pendsv.h" +#include "lib/utils/mpirq.h" #include "py/obj.h" #if MICROPY_PY_BLUETOOTH +#define DEBUG_printf(...) // printf(__VA_ARGS__) + uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; -// Must be provided by the stack bindings. +// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c). extern void mp_bluetooth_hci_poll(void); // Hook for pendsv poller to run this periodically every 128ms @@ -61,34 +64,19 @@ STATIC uint16_t hci_uart_rx_buf_cur; STATIC uint16_t hci_uart_rx_buf_len; STATIC uint8_t hci_uart_rx_buf_data[256]; -int mp_bluetooth_hci_controller_deactivate(void) { - return 0; -} - -int mp_bluetooth_hci_controller_sleep_maybe(void) { - return 0; -} - -bool mp_bluetooth_hci_controller_woken(void) { - return true; -} - -int mp_bluetooth_hci_controller_wakeup(void) { - return 0; -} - -int mp_bluetooth_hci_uart_init(uint32_t port) { +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { (void)port; - return 0; -} - -int mp_bluetooth_hci_uart_activate(void) { + (void)baudrate; rfcore_ble_init(); hci_uart_rx_buf_cur = 0; hci_uart_rx_buf_len = 0; return 0; } +int mp_bluetooth_hci_uart_deinit(void) { + return 0; +} + int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { (void)baudrate; return 0; @@ -140,13 +128,16 @@ mp_irq_obj_t mp_bluetooth_hci_uart_irq_obj; static uint8_t hci_uart_rxbuf[512]; mp_obj_t mp_uart_interrupt(mp_obj_t self_in) { - // New HCI data, schedule mp_bluetooth_hci_poll to make the stack handle it. + // DEBUG_printf("mp_uart_interrupt\n"); + // New HCI data, schedule mp_bluetooth_hci_poll via PENDSV to make the stack handle it. mp_bluetooth_hci_poll_wrapper(0); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_interrupt_obj, mp_uart_interrupt); -int mp_bluetooth_hci_uart_init(uint32_t port) { +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { + DEBUG_printf("mp_bluetooth_hci_uart_init (stm32)\n"); + // bits (8), stop (1), parity (none) and flow (rts/cts) are assumed to match MYNEWT_VAL_BLE_HCI_UART_ constants in syscfg.h. mp_bluetooth_hci_uart_obj.base.type = &pyb_uart_type; mp_bluetooth_hci_uart_obj.uart_id = port; @@ -154,16 +145,29 @@ int mp_bluetooth_hci_uart_init(uint32_t port) { mp_bluetooth_hci_uart_obj.timeout = 2; mp_bluetooth_hci_uart_obj.timeout_char = 2; MP_STATE_PORT(pyb_uart_obj_all)[mp_bluetooth_hci_uart_obj.uart_id - 1] = &mp_bluetooth_hci_uart_obj; + + // This also initialises the UART. + mp_bluetooth_hci_uart_set_baudrate(baudrate); + + return 0; +} + +int mp_bluetooth_hci_uart_deinit(void) { + DEBUG_printf("mp_bluetooth_hci_uart_deinit (stm32)\n"); + // TODO: deinit mp_bluetooth_hci_uart_obj + return 0; } int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { + DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate(%lu) (stm32)\n", baudrate); + if (!baudrate) { + return -1; + } + uart_init(&mp_bluetooth_hci_uart_obj, baudrate, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); uart_set_rxbuf(&mp_bluetooth_hci_uart_obj, sizeof(hci_uart_rxbuf), hci_uart_rxbuf); - return 0; -} -int mp_bluetooth_hci_uart_activate(void) { // Add IRQ handler for IDLE (i.e. packet finished). uart_irq_config(&mp_bluetooth_hci_uart_obj, false); mp_irq_init(&mp_bluetooth_hci_uart_irq_obj, &uart_irq_methods, MP_OBJ_FROM_PTR(&mp_bluetooth_hci_uart_obj)); @@ -173,13 +177,12 @@ int mp_bluetooth_hci_uart_activate(void) { mp_bluetooth_hci_uart_irq_obj.ishard = true; uart_irq_config(&mp_bluetooth_hci_uart_obj, true); - mp_bluetooth_hci_controller_init(); - mp_bluetooth_hci_controller_activate(); - return 0; } int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { + // DEBUG_printf("mp_bluetooth_hci_uart_write (stm32)\n"); + mp_bluetooth_hci_controller_wakeup(); uart_tx_strn(&mp_bluetooth_hci_uart_obj, (void *)buf, len); return 0; @@ -188,7 +191,10 @@ int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { // This function expects the controller to be in the wake state via a previous call // to mp_bluetooth_hci_controller_woken. int mp_bluetooth_hci_uart_readchar(void) { + // DEBUG_printf("mp_bluetooth_hci_uart_readchar (stm32)\n"); + if (uart_rx_any(&mp_bluetooth_hci_uart_obj)) { + // DEBUG_printf("... available\n"); return uart_rx_char(&mp_bluetooth_hci_uart_obj); } else { return -1; @@ -197,4 +203,33 @@ int mp_bluetooth_hci_uart_readchar(void) { #endif // defined(STM32WB) +// Default (weak) implementation of the HCI controller interface. +// A driver (e.g. cywbt43.c) can override these for controller-specific +// functionality (i.e. power management). + +MP_WEAK int mp_bluetooth_hci_controller_init(void) { + DEBUG_printf("mp_bluetooth_hci_controller_init (default)\n"); + return 0; +} + +MP_WEAK int mp_bluetooth_hci_controller_deinit(void) { + DEBUG_printf("mp_bluetooth_hci_controller_deinit (default)\n"); + return 0; +} + +MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) { + DEBUG_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n"); + return 0; +} + +MP_WEAK bool mp_bluetooth_hci_controller_woken(void) { + DEBUG_printf("mp_bluetooth_hci_controller_woken (default)\n"); + return true; +} + +MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) { + DEBUG_printf("mp_bluetooth_hci_controller_wakeup (default)\n"); + return 0; +} + #endif // MICROPY_PY_BLUETOOTH diff --git a/ports/stm32/mpbtstackport.c b/ports/stm32/mpbtstackport.c new file mode 100644 index 0000000000000..a031a6a151aff --- /dev/null +++ b/ports/stm32/mpbtstackport.c @@ -0,0 +1,107 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK + +#include "lib/btstack/src/btstack.h" +#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" +#include "lib/btstack/platform/embedded/hal_cpu.h" +#include "lib/btstack/platform/embedded/hal_time_ms.h" + +#include "extmod/mpbthci.h" +#include "extmod/btstack/btstack_hci_uart.h" +#include "extmod/btstack/modbluetooth_btstack.h" + +// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the +// following three functions are empty. + +void hal_cpu_disable_irqs(void) { +} + +void hal_cpu_enable_irqs(void) { +} + +void hal_cpu_enable_irqs_and_sleep(void) { +} + +uint32_t hal_time_ms(void) { + return mp_hal_ticks_ms(); +} + +STATIC const hci_transport_config_uart_t hci_transport_config_uart = { + HCI_TRANSPORT_CONFIG_UART, + MICROPY_HW_BLE_UART_BAUDRATE, + 3000000, + 0, + NULL, +}; + +void mp_bluetooth_hci_poll(void) { + if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_OFF) { + return; + } + + // Process uart data. + if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_HALTING) { + mp_bluetooth_btstack_hci_uart_process(); + } + + // Call the BTstack run loop. + btstack_run_loop_embedded_execute_once(); +} + +void mp_bluetooth_btstack_port_init(void) { + static bool run_loop_init = false; + if (!run_loop_init) { + run_loop_init = true; + btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); + } else { + btstack_run_loop_embedded_get_instance()->init(); + } + + // hci_dump_open(NULL, HCI_DUMP_STDOUT); + const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); + hci_init(transport, &hci_transport_config_uart); + + // TODO: Probably not necessary for BCM (we have our own firmware loader in the cyw43 driver), + // but might be worth investigating for other controllers in the future. + // hci_set_chipset(btstack_chipset_bcm_instance()); +} + +void mp_bluetooth_btstack_port_deinit(void) { + hci_power_control(HCI_POWER_OFF); + hci_close(); +} + +void mp_bluetooth_btstack_port_start(void) { + hci_power_control(HCI_POWER_ON); +} + +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK diff --git a/ports/stm32/mpbtstackport.h b/ports/stm32/mpbtstackport.h new file mode 100644 index 0000000000000..110f921d9a9f8 --- /dev/null +++ b/ports/stm32/mpbtstackport.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_STM32_BTSTACK_PORT_H +#define MICROPY_INCLUDED_STM32_BTSTACK_PORT_H + +// To allow MICROPY_HW_BLE_UART_ID to be resolved. + +#include "uart.h" + +#endif // MICROPY_INCLUDED_STM32_BTSTACK_PORT_H diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 14a05898951ba..e1e1491d64c6d 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -298,6 +298,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; #if MICROPY_BLUETOOTH_NIMBLE struct _mp_bluetooth_nimble_root_pointers_t; +struct _mp_bluetooth_nimble_malloc_t; #define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE void **bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers; #else #define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE @@ -410,14 +411,20 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_THREAD_YIELD() #endif -// The LwIP interface must run at a raised IRQ priority -#define MICROPY_PY_LWIP_ENTER uint32_t atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); -#define MICROPY_PY_LWIP_REENTER atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); -#define MICROPY_PY_LWIP_EXIT restore_irq_pri(atomic_state); +// For regular code that wants to prevent "background tasks" from running. +// These background tasks (LWIP, Bluetooth) run in PENDSV context. +#define MICROPY_PY_PENDSV_ENTER uint32_t atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); +#define MICROPY_PY_PENDSV_REENTER atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); +#define MICROPY_PY_PENDSV_EXIT restore_irq_pri(atomic_state); -// Bluetooth calls must run at a raised IRQ priority -#define MICROPY_PY_BLUETOOTH_ENTER MICROPY_PY_LWIP_ENTER -#define MICROPY_PY_BLUETOOTH_EXIT MICROPY_PY_LWIP_EXIT +// Prevent the "LWIP task" from running. +#define MICROPY_PY_LWIP_ENTER MICROPY_PY_PENDSV_ENTER +#define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER +#define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT + +// Prevent the "Bluetooth task" from running (either NimBLE or btstack). +#define MICROPY_PY_BLUETOOTH_ENTER MICROPY_PY_PENDSV_ENTER +#define MICROPY_PY_BLUETOOTH_EXIT MICROPY_PY_PENDSV_EXIT // We need an implementation of the log2 function which is not a macro #define MP_NEED_LOG2 (1) diff --git a/ports/stm32/nimble.c b/ports/stm32/mpnimbleport.c similarity index 56% rename from ports/stm32/nimble.c rename to ports/stm32/mpnimbleport.c index 0d349585fb1ae..1d7c095139cde 100644 --- a/ports/stm32/nimble.c +++ b/ports/stm32/mpnimbleport.c @@ -31,58 +31,41 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE -#include "transport/uart/ble_hci_uart.h" #include "host/ble_hs.h" +#include "nimble/nimble_npl.h" -#include "extmod/modbluetooth_hci.h" +#include "extmod/mpbthci.h" #include "extmod/nimble/modbluetooth_nimble.h" -#include "extmod/nimble/nimble/nimble_hci_uart.h" - -extern void os_eventq_run_all(void); -extern void os_callout_process(void); +#include "extmod/nimble/hal/hal_uart.h" +// This implements the Nimble "background task". It's called at PENDSV +// priority, either every 128ms or whenever there's UART data available. +// Because it's called via PENDSV, you can implicitly consider that it +// is surrounded by MICROPY_PY_BLUETOOTH_ENTER / MICROPY_PY_BLUETOOTH_EXIT. void mp_bluetooth_hci_poll(void) { - if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { - return; - } - - mp_bluetooth_nimble_hci_uart_process(); - os_callout_process(); - os_eventq_run_all(); -} - -void mp_bluetooth_nimble_port_preinit(void) { - MP_STATE_PORT(bluetooth_nimble_memory) = NULL; - ble_hci_uart_init(); -} + if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { + // Ask NimBLE to process UART data. + mp_bluetooth_nimble_hci_uart_process(); -void mp_bluetooth_nimble_port_postinit(void) { + // Run pending background operations and events, but only after HCI sync. + mp_bluetooth_nimble_os_callout_process(); + mp_bluetooth_nimble_os_eventq_run_all(); + } } -void mp_bluetooth_nimble_port_deinit(void) { - mp_bluetooth_hci_controller_deactivate(); -} +// --- Port-specific helpers for the generic NimBLE bindings. ----------------- -void mp_bluetooth_nimble_port_start(void) { - ble_hs_start(); +void mp_bluetooth_nimble_hci_uart_wfi(void) { + __WFI(); } -void mp_bluetooth_nimble_hci_uart_rx(hal_uart_rx_cb_t rx_cb, void *rx_arg) { - bool host_wake = mp_bluetooth_hci_controller_woken(); - - int chr; - while ((chr = mp_bluetooth_hci_uart_readchar()) >= 0) { - // printf("UART RX: %02x\n", data); - rx_cb(rx_arg, chr); - } - - if (host_wake) { - mp_bluetooth_hci_controller_sleep_maybe(); - } +uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) { + MICROPY_PY_BLUETOOTH_ENTER + return atomic_state; } -void mp_bluetooth_nimble_hci_uart_tx_strn(const char *str, uint len) { - mp_bluetooth_hci_uart_write((const uint8_t *)str, len); +void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) { + MICROPY_PY_BLUETOOTH_EXIT } #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/ports/stm32/mpnimbleport.h b/ports/stm32/mpnimbleport.h new file mode 100644 index 0000000000000..f3ee597183a04 --- /dev/null +++ b/ports/stm32/mpnimbleport.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_STM32_NIMBLE_PORT_H +#define MICROPY_INCLUDED_STM32_NIMBLE_PORT_H + +// To allow MICROPY_HW_BLE_UART_ID to be resolved. + +#include "uart.h" + +#endif // MICROPY_INCLUDED_STM32_NIMBLE_PORT_H diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 3e2fa63a16c3b..a9ba5edfad9af 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -140,18 +140,19 @@ ifeq ($(HAVE_LIBUSB),1) CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=1 +# Runs in a thread, cannot make calls into the VM. +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=0 MICROPY_BLUETOOTH_BTSTACK ?= 1 MICROPY_BLUETOOTH_BTSTACK_USB ?= 1 ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) GIT_SUBMODULES += lib/btstack - include $(TOP)/extmod/btstack/btstack.mk endif endif + endif ifeq ($(MICROPY_PY_FFI),1) @@ -198,7 +199,9 @@ SRC_C = \ alloc.c \ coverage.c \ fatfs_port.c \ - btstack_usb.c \ + mpbtstackport_common.c \ + mpbtstackport_usb.c \ + mpnimbleport.c \ $(SRC_MOD) \ $(wildcard $(VARIANT_DIR)/*.c) diff --git a/ports/unix/mpbtstackport.h b/ports/unix/mpbtstackport.h new file mode 100644 index 0000000000000..29433cc7fb006 --- /dev/null +++ b/ports/unix/mpbtstackport.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H +#define MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H + +#define MICROPY_HW_BLE_UART_ID (0) +#define MICROPY_HW_BLE_UART_BAUDRATE (1000000) + +bool mp_bluetooth_hci_poll(void); + +#if MICROPY_BLUETOOTH_BTSTACK_USB +void mp_bluetooth_btstack_port_init_usb(void); +#endif + +#endif // MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c new file mode 100644 index 0000000000000..402cf95283441 --- /dev/null +++ b/ports/unix/mpbtstackport_common.c @@ -0,0 +1,92 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK + +#include "lib/btstack/src/btstack.h" + +#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" +#include "lib/btstack/platform/embedded/hal_cpu.h" +#include "lib/btstack/platform/embedded/hal_time_ms.h" + +#include "extmod/btstack/modbluetooth_btstack.h" + +#include "mpbtstackport.h" + +// Called by the UART polling thread in mpbthciport.c, or by the USB polling thread in mpbtstackport_usb.c. +bool mp_bluetooth_hci_poll(void) { + if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_HALTING) { + // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); + btstack_run_loop_embedded_execute_once(); + MICROPY_END_ATOMIC_SECTION(atomic_state); + + return true; + } + + return false; +} + +// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the +// following three functions are empty. + +void hal_cpu_disable_irqs(void) { +} + +void hal_cpu_enable_irqs(void) { +} + +void hal_cpu_enable_irqs_and_sleep(void) { +} + +uint32_t hal_time_ms(void) { + return mp_hal_ticks_ms(); +} + +void mp_bluetooth_btstack_port_init(void) { + static bool run_loop_init = false; + if (!run_loop_init) { + run_loop_init = true; + btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); + } else { + btstack_run_loop_embedded_get_instance()->init(); + } + + // hci_dump_open(NULL, HCI_DUMP_STDOUT); + + #if MICROPY_BLUETOOTH_BTSTACK_USB + mp_bluetooth_btstack_port_init_usb(); + #endif +} + +void mp_hal_get_mac(int idx, uint8_t buf[6]) { +} + +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK diff --git a/ports/unix/btstack_usb.c b/ports/unix/mpbtstackport_usb.c similarity index 54% rename from ports/unix/btstack_usb.c rename to ports/unix/mpbtstackport_usb.c index ab6a49f39ae73..28d2c8c543f71 100644 --- a/ports/unix/btstack_usb.c +++ b/ports/unix/mpbtstackport_usb.c @@ -31,7 +31,7 @@ #include "py/mperrno.h" #include "py/mphal.h" -#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB #include "lib/btstack/src/btstack.h" #include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" @@ -40,76 +40,15 @@ #include "extmod/btstack/modbluetooth_btstack.h" +#include "mpbtstackport.h" + #if !MICROPY_PY_THREAD #error Unix btstack requires MICROPY_PY_THREAD #endif STATIC const useconds_t USB_POLL_INTERVAL_US = 1000; -STATIC const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; - -STATIC uint8_t local_addr[6] = {0}; -STATIC uint8_t static_address[6] = {0}; -STATIC volatile bool have_addr = false; -STATIC bool using_static_address = false; - -STATIC btstack_packet_callback_registration_t hci_event_callback_registration; - -STATIC void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { - (void)channel; - (void)size; - if (packet_type != HCI_EVENT_PACKET) { - return; - } - switch (hci_event_packet_get_type(packet)) { - case BTSTACK_EVENT_STATE: - if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) { - return; - } - gap_local_bd_addr(local_addr); - if (using_static_address) { - memcpy(local_addr, static_address, sizeof(local_addr)); - } - have_addr = true; - break; - case HCI_EVENT_COMMAND_COMPLETE: - if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) { - reverse_48(&packet[7], static_address); - gap_random_address_set(static_address); - using_static_address = true; - have_addr = true; - } - break; - default: - break; - } -} - -// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the -// following three functions are empty. - -void hal_cpu_disable_irqs(void) { -} - -void hal_cpu_enable_irqs(void) { -} - -void hal_cpu_enable_irqs_and_sleep(void) { -} - -uint32_t hal_time_ms(void) { - return mp_hal_ticks_ms(); -} - -void mp_bluetooth_btstack_port_init(void) { - static bool run_loop_init = false; - if (!run_loop_init) { - run_loop_init = true; - btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); - } else { - btstack_run_loop_embedded_get_instance()->init(); - } - +void mp_bluetooth_btstack_port_init_usb(void) { // MICROPYBTUSB can be a ':'' or '-' separated port list. char *path = getenv("MICROPYBTUSB"); if (path != NULL) { @@ -128,11 +67,7 @@ void mp_bluetooth_btstack_port_init(void) { hci_transport_usb_set_path(usb_path_len, usb_path); } - // hci_dump_open(NULL, HCI_DUMP_STDOUT); hci_init(hci_transport_usb_instance(), NULL); - - hci_event_callback_registration.callback = &packet_handler; - hci_add_event_handler(&hci_event_callback_registration); } STATIC pthread_t bstack_thread_id; @@ -142,9 +77,12 @@ void mp_bluetooth_btstack_port_deinit(void) { // Wait for the poll loop to terminate when the state is set to OFF. pthread_join(bstack_thread_id, NULL); - have_addr = false; } + +// Provided by mpbstackport_common.c. +extern bool mp_bluetooth_hci_poll(void); + STATIC void *btstack_thread(void *arg) { (void)arg; hci_power_control(HCI_POWER_ON); @@ -155,19 +93,15 @@ STATIC void *btstack_thread(void *arg) { // in modbluetooth_btstack.c setting the state back to OFF. // Or, if a timeout results in it being set to TIMEOUT. - while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { - // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). - mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); - btstack_run_loop_embedded_execute_once(); - MICROPY_END_ATOMIC_SECTION(atomic_state); + while (true) { + if (!mp_bluetooth_hci_poll()) { + break; + } // The USB transport schedules events to the run loop at 1ms intervals, // and the implementation currently polls rather than selects. usleep(USB_POLL_INTERVAL_US); } - - hci_close(); - return NULL; } @@ -179,13 +113,4 @@ void mp_bluetooth_btstack_port_start(void) { pthread_create(&bstack_thread_id, &attr, &btstack_thread, NULL); } -void mp_hal_get_mac(int idx, uint8_t buf[6]) { - if (idx == MP_HAL_MAC_BDADDR) { - if (!have_addr) { - mp_raise_OSError(MP_ENODEV); - } - memcpy(buf, local_addr, sizeof(local_addr)); - } -} - -#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 08605842fd5bd..9e24ab10cd263 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -309,9 +309,11 @@ void mp_unix_mark_exec(void); #define MP_STATE_PORT MP_STATE_VM -#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK +#if MICROPY_PY_BLUETOOTH +#if MICROPY_BLUETOOTH_BTSTACK struct _mp_bluetooth_btstack_root_pointers_t; #define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers; +#endif #else #define MICROPY_BLUETOOTH_ROOT_POINTERS #endif From 5b08676d6a644487c4ec6f63aef02888f8da8b19 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 17 Aug 2020 18:12:20 +1000 Subject: [PATCH 0246/3533] extmod/nimble: Set struct alignment correctly on 64-bit arch. --- extmod/nimble/nimble/nimble_npl_os.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/nimble/nimble/nimble_npl_os.h b/extmod/nimble/nimble/nimble_npl_os.h index 55425206612ae..bfa35d0952f25 100644 --- a/extmod/nimble/nimble/nimble_npl_os.h +++ b/extmod/nimble/nimble/nimble_npl_os.h @@ -33,7 +33,7 @@ // --- Configuration of NimBLE data structures -------------------------------- -#define BLE_NPL_OS_ALIGNMENT (4) +#define BLE_NPL_OS_ALIGNMENT (sizeof(uintptr_t)) #define BLE_NPL_TIME_FOREVER (0xffffffff) typedef uint32_t ble_npl_time_t; From f3f31ac9599fcf1c94dca20046da69773cc3418a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 16:28:55 +1000 Subject: [PATCH 0247/3533] extmod/nimble: Make nimble_malloc work with allocated size. --- extmod/nimble/nimble/nimble_npl_os.c | 104 ++++++++++++++++++--------- ports/stm32/mpconfigport.h | 2 +- 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/extmod/nimble/nimble/nimble_npl_os.c b/extmod/nimble/nimble/nimble_npl_os.c index ba3031a8ff694..500f7c0a51e0b 100644 --- a/extmod/nimble/nimble/nimble_npl_os.c +++ b/extmod/nimble/nimble/nimble_npl_os.c @@ -58,44 +58,58 @@ void *ble_npl_get_current_task_id(void) { // Maintain a linked list of heap memory that we've passed to Nimble, // discoverable via the bluetooth_nimble_memory root pointer. -// MP_STATE_PORT(bluetooth_nimble_memory) is a pointer to [next, prev, data...]. +typedef struct _mp_bluetooth_nimble_malloc_t { + struct _mp_bluetooth_nimble_malloc_t *prev; + struct _mp_bluetooth_nimble_malloc_t *next; + size_t size; + uint8_t data[]; +} mp_bluetooth_nimble_malloc_t; // TODO: This is duplicated from mbedtls. Perhaps make this a generic feature? -void *m_malloc_bluetooth(size_t size) { - void **ptr = m_malloc0(size + 2 * sizeof(uintptr_t)); - if (MP_STATE_PORT(bluetooth_nimble_memory) != NULL) { - MP_STATE_PORT(bluetooth_nimble_memory)[0] = ptr; +STATIC void *m_malloc_bluetooth(size_t size) { + size += sizeof(mp_bluetooth_nimble_malloc_t); + mp_bluetooth_nimble_malloc_t *alloc = m_malloc0(size); + alloc->size = size; + alloc->next = MP_STATE_PORT(bluetooth_nimble_memory); + if (alloc->next) { + alloc->next->prev = alloc; } - ptr[0] = NULL; - ptr[1] = MP_STATE_PORT(bluetooth_nimble_memory); - MP_STATE_PORT(bluetooth_nimble_memory) = ptr; - return &ptr[2]; + MP_STATE_PORT(bluetooth_nimble_memory) = alloc; + return alloc->data; } -void m_free_bluetooth(void *ptr_in) { - void **ptr = &((void**)ptr_in)[-2]; - if (ptr[1] != NULL) { - ((void**)ptr[1])[0] = ptr[0]; +STATIC mp_bluetooth_nimble_malloc_t* get_nimble_malloc(void *ptr) { + return (mp_bluetooth_nimble_malloc_t*)((uintptr_t)ptr - sizeof(mp_bluetooth_nimble_malloc_t)); +} + +STATIC void m_free_bluetooth(void *ptr) { + mp_bluetooth_nimble_malloc_t *alloc = get_nimble_malloc(ptr); + if (alloc->next) { + alloc->next->prev = alloc->prev; } - if (ptr[0] != NULL) { - ((void**)ptr[0])[1] = ptr[1]; + if (alloc->prev) { + alloc->prev->next = alloc->next; } else { - MP_STATE_PORT(bluetooth_nimble_memory) = ptr[1]; + MP_STATE_PORT(bluetooth_nimble_memory) = NULL; } - m_free(ptr); + m_free(alloc + #if MICROPY_MALLOC_USES_ALLOCATED_SIZE + , alloc->size + #endif + ); } // Check if a nimble ptr is tracked. // If it isn't, that means that it's from a previous soft-reset cycle. STATIC bool is_valid_nimble_malloc(void *ptr) { DEBUG_MALLOC_printf("NIMBLE is_valid_nimble_malloc(%p)\n", ptr); - void** search = MP_STATE_PORT(bluetooth_nimble_memory); - while (search) { - if (&search[2] == ptr) { + mp_bluetooth_nimble_malloc_t *alloc = MP_STATE_PORT(bluetooth_nimble_memory); + while (alloc) { + DEBUG_MALLOC_printf("NIMBLE checking: %p\n", alloc->data); + if (alloc->data == ptr) { return true; } - - search = (void**)search[1]; + alloc = alloc->next; } return false; } @@ -110,22 +124,46 @@ void *nimble_malloc(size_t size) { // Only free if it's still a valid pointer. void nimble_free(void *ptr) { DEBUG_MALLOC_printf("NIMBLE free(%p)\n", ptr); - if (ptr && is_valid_nimble_malloc(ptr)) { - m_free_bluetooth(ptr); + + if (ptr) { + // After a stack re-init, NimBLE has variables in BSS that might be + // still pointing to old allocations from a previous init. We can't do + // anything about this (e.g. ble_gatts_free_mem is private). But we + // can identify that this is a non-null, invalid alloc because it + // won't be in our list, so ignore it because it is effectively free'd + // anyway (it's not referenced by anything the GC can find). + if (is_valid_nimble_malloc(ptr)) { + m_free_bluetooth(ptr); + } } } // Only realloc if it's still a valid pointer. Otherwise just malloc. -void *nimble_realloc(void *ptr, size_t size) { - // This is only used by ble_gatts.c to grow the queue of pending services to be registered. - DEBUG_MALLOC_printf("NIMBLE realloc(%p, %u)\n", ptr, (uint)size); - void *ptr2 = nimble_malloc(size); - if (ptr && is_valid_nimble_malloc(ptr)) { - // If it's a realloc and we still have the old data, then copy it. - // This will happen as we add services. - memcpy(ptr2, ptr, size); - m_free_bluetooth(ptr); +void *nimble_realloc(void *ptr, size_t new_size) { + DEBUG_MALLOC_printf("NIMBLE realloc(%p, %u)\n", ptr, (uint)new_size); + + if (!ptr) { + return nimble_malloc(new_size); } + + assert(is_valid_nimble_malloc(ptr)); + + // Existing alloc is big enough. + mp_bluetooth_nimble_malloc_t *alloc = get_nimble_malloc(ptr); + size_t old_size = alloc->size - sizeof(mp_bluetooth_nimble_malloc_t); + if (old_size >= new_size) { + return ptr; + } + + // Allocate a new, larger region. + void *ptr2 = m_malloc_bluetooth(new_size); + + // Copy old, smaller region into new region. + memcpy(ptr2, ptr, old_size); + m_free_bluetooth(ptr); + + DEBUG_MALLOC_printf(" --> %p\n", ptr2); + return ptr2; } diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index e1e1491d64c6d..95b539603fb95 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -299,7 +299,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; #if MICROPY_BLUETOOTH_NIMBLE struct _mp_bluetooth_nimble_root_pointers_t; struct _mp_bluetooth_nimble_malloc_t; -#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE void **bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers; +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE struct _mp_bluetooth_nimble_malloc_t *bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers; #else #define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE #endif From aa18ab7db2a055440cb4798487c12809c74935c5 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 16:29:22 +1000 Subject: [PATCH 0248/3533] extmod/nimble: Implement NimBLE mutex. --- extmod/nimble/nimble/nimble_npl_os.c | 49 +++++++++++++++++++++++++--- extmod/nimble/nimble/nimble_npl_os.h | 1 + 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/extmod/nimble/nimble/nimble_npl_os.c b/extmod/nimble/nimble/nimble_npl_os.c index 500f7c0a51e0b..46f7a0b291303 100644 --- a/extmod/nimble/nimble/nimble_npl_os.c +++ b/extmod/nimble/nimble/nimble_npl_os.c @@ -249,21 +249,62 @@ void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg) { /******************************************************************************/ // MUTEX +// This is what MICROPY_BEGIN_ATOMIC_SECTION returns on Unix (i.e. we don't +// need to preserve the atomic state to unlock). +#define ATOMIC_STATE_MUTEX_NOT_HELD 0xffffffff + ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu) { DEBUG_MUTEX_printf("ble_npl_mutex_init(%p)\n", mu); mu->locked = 0; + mu->atomic_state = ATOMIC_STATE_MUTEX_NOT_HELD; return BLE_NPL_OK; } ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout) { - DEBUG_MUTEX_printf("ble_npl_mutex_pend(%p, %u) locked=%u\n", mu, (uint)timeout, (uint)mu->locked); - mu->locked = 1; + DEBUG_MUTEX_printf("ble_npl_mutex_pend(%p, %u) locked=%u irq=%d\n", mu, (uint)timeout, (uint)mu->locked); + + // This is a recursive mutex which we implement on top of the IRQ priority + // scheme. Unfortunately we have a single piece of global storage, where + // enter/exit critical needs an "atomic state". + + // There are two different acquirers, either running in a VM thread (i.e. + // a direct Python call into NimBLE), or in the NimBLE task (i.e. polling + // or UART RX). + + // On STM32 the NimBLE task runs in PENDSV, so cannot be interrupted by a VM thread. + // Therefore we only need to ensure that a VM thread that acquires a currently-unlocked mutex + // now raises the priority (thus preventing context switches to other VM threads and + // the PENDSV irq). If the mutex is already locked, then it must have been acquired + // by us. + + // On Unix, the critical section is completely recursive and doesn't require us to manage + // state so we just acquire and release every time. + + // TODO: The "volatile" on locked/atomic_state isn't enough to protect against memory re-ordering. + + // First acquirer of this mutex always enters the critical section, unless + // we're on Unix where it happens every time. + if (mu->atomic_state == ATOMIC_STATE_MUTEX_NOT_HELD) { + mu->atomic_state = mp_bluetooth_nimble_hci_uart_enter_critical(); + } + + ++mu->locked; + return BLE_NPL_OK; } ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu) { - DEBUG_MUTEX_printf("ble_npl_mutex_release(%p) locked=%u\n", mu, (uint)mu->locked); - mu->locked = 0; + DEBUG_MUTEX_printf("ble_npl_mutex_release(%p) locked=%u irq=%d\n", mu, (uint)mu->locked); + assert(mu->locked > 0); + + --mu->locked; + + // Only exit the critical section for the final release, unless we're on Unix. + if (mu->locked == 0 || mu->atomic_state == ATOMIC_STATE_MUTEX_NOT_HELD) { + mp_bluetooth_nimble_hci_uart_exit_critical(mu->atomic_state); + mu->atomic_state = ATOMIC_STATE_MUTEX_NOT_HELD; + } + return BLE_NPL_OK; } diff --git a/extmod/nimble/nimble/nimble_npl_os.h b/extmod/nimble/nimble/nimble_npl_os.h index bfa35d0952f25..3ef07aa9ccd12 100644 --- a/extmod/nimble/nimble/nimble_npl_os.h +++ b/extmod/nimble/nimble/nimble_npl_os.h @@ -61,6 +61,7 @@ struct ble_npl_callout { struct ble_npl_mutex { volatile uint8_t locked; + volatile uint32_t atomic_state; }; struct ble_npl_sem { From feed69aa5c4e9fda037497a1b63c05a27b164920 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 16:40:55 +1000 Subject: [PATCH 0249/3533] unix/Makefile: Always enable -f*-sections regardless of DEBUG setting. --- ports/unix/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index a9ba5edfad9af..090b58d8ec4e2 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -47,10 +47,12 @@ ifdef DEBUG COPT ?= -O0 else COPT ?= -Os -COPT += -fdata-sections -ffunction-sections COPT += -DNDEBUG endif +# Remove unused sections. +COPT += -fdata-sections -ffunction-sections + # Always enable symbols -- They're occasionally useful, and don't make it into the # final .bin/.hex/.dfu so the extra size doesn't matter. CFLAGS += -g From 1b1b22905e1aa7676c51078f75b1bd73d8383a4a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:46:53 +1000 Subject: [PATCH 0250/3533] unix: Implement BLE H4 HCI UART for btstack/nimble. This commit adds support for using Bluetooth on the unix port via a H4 serial interface (distinct from a USB dongle), with both BTstack and NimBLE Bluetooth stacks. Note that MICROPY_PY_BLUETOOTH is now disabled for the coverage variant. Prior to this commit Bluetooth was anyway not being built on Travis because libusb was not detected. But now that bluetooth works in H4 mode it will be built, and will lead to a large decrease in coverage because Bluetooth tests cannot be run on Travis. --- extmod/btstack/btstack.mk | 8 + ports/unix/Makefile | 45 +++- ports/unix/mpbthciport.c | 219 ++++++++++++++++++ ports/unix/mpbtstackport.h | 5 + ports/unix/mpbtstackport_common.c | 7 + ports/unix/mpbtstackport_h4.c | 80 +++++++ ports/unix/mpconfigport.h | 7 +- ports/unix/mpnimbleport.c | 84 +++++++ ports/unix/mpnimbleport.h | 33 +++ .../unix/variants/coverage/mpconfigvariant.mk | 1 - ports/unix/variants/dev/mpconfigvariant.mk | 3 +- 11 files changed, 484 insertions(+), 8 deletions(-) create mode 100644 ports/unix/mpbthciport.c create mode 100644 ports/unix/mpbtstackport_h4.c create mode 100644 ports/unix/mpnimbleport.c create mode 100644 ports/unix/mpnimbleport.h diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index 3084601b85415..7e5d2f646d14c 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -30,10 +30,18 @@ SRC_BTSTACK = \ $(addprefix lib/btstack/src/ble/, $(filter-out %_tlv.c, $(SRC_BLE_FILES))) \ lib/btstack/platform/embedded/btstack_run_loop_embedded.c +ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) +ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) + $(error Cannot specifiy both MICROPY_BLUETOOTH_BTSTACK_USB and MICROPY_BLUETOOTH_BTSTACK_H4) +endif +endif + ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) SRC_BTSTACK += \ lib/btstack/platform/libusb/hci_transport_h2_libusb.c +CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK_USB=1 + CFLAGS += $(shell pkg-config libusb-1.0 --cflags) LDFLAGS += $(shell pkg-config libusb-1.0 --libs) endif diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 090b58d8ec4e2..ff5f355022bc5 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -134,24 +134,57 @@ CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0 LDFLAGS_MOD += $(LIBPTHREAD) endif -# If the variant enables it and we have libusb, enable BTStack support for USB adaptors. +# If the variant enables it, enable modbluetooth. ifeq ($(MICROPY_PY_BLUETOOTH),1) HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1') -ifeq ($(HAVE_LIBUSB),1) + +# Only one stack can be enabled. +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +$(error Cannot enable both NimBLE and BTstack at the same time) +endif +endif + +# Default to btstack, but a variant (or make command line) can set NimBLE +# explicitly (which is always via H4 UART). +ifneq ($(MICROPY_BLUETOOTH_NIMBLE),1) +ifneq ($(MICROPY_BLUETOOTH_BTSTACK),1) +MICROPY_BLUETOOTH_BTSTACK ?= 1 +endif +endif CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 # Runs in a thread, cannot make calls into the VM. CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=0 -MICROPY_BLUETOOTH_BTSTACK ?= 1 +ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) + +# Figure out which BTstack transport to use. +ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) +ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) +$(error Cannot enable BTstack support for USB and H4 UART at the same time) +endif +else +ifeq ($(HAVE_LIBUSB),1) +# Default to btstack-over-usb. MICROPY_BLUETOOTH_BTSTACK_USB ?= 1 +else +# Fallback to HCI controller via a H4 UART (e.g. Zephyr on nRF) over a /dev/tty serial port. +MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 +endif +endif -ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +# BTstack is enabled. GIT_SUBMODULES += lib/btstack include $(TOP)/extmod/btstack/btstack.mk -endif + +else + +# NimBLE is enabled. +GIT_SUBMODULES += lib/mynewt-nimble +include $(TOP)/extmod/nimble/nimble.mk endif @@ -201,7 +234,9 @@ SRC_C = \ alloc.c \ coverage.c \ fatfs_port.c \ + mpbthciport.c \ mpbtstackport_common.c \ + mpbtstackport_h4.c \ mpbtstackport_usb.c \ mpnimbleport.c \ $(SRC_MOD) \ diff --git a/ports/unix/mpbthciport.c b/ports/unix/mpbthciport.c new file mode 100644 index 0000000000000..5a5c1d4a4e79d --- /dev/null +++ b/ports/unix/mpbthciport.c @@ -0,0 +1,219 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH && (MICROPY_BLUETOOTH_NIMBLE || (MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4)) + +#if !MICROPY_PY_THREAD +#error Unix HCI UART requires MICROPY_PY_THREAD +#endif + +#include "extmod/modbluetooth.h" +#include "extmod/mpbthci.h" + +#include +#include + +#include +#include +#include +#include + +#define DEBUG_printf(...) // printf(__VA_ARGS__) +#define DEBUG_HCI_DUMP (0) + +uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; + +// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c). +extern bool mp_bluetooth_hci_poll(void); + +STATIC const useconds_t UART_POLL_INTERVAL_US = 1000; + +STATIC int uart_fd = -1; +STATIC pthread_t hci_poll_thread_id; + +STATIC void *hci_poll_thread(void *arg) { + (void)arg; + + // This will return false when the stack is shutdown. + while (mp_bluetooth_hci_poll()) { + usleep(UART_POLL_INTERVAL_US); + } + + return NULL; +} + +STATIC int configure_uart(void) { + struct termios toptions; + + // Get existing config. + if (tcgetattr(uart_fd, &toptions) < 0) { + DEBUG_printf("Couldn't get term attributes"); + return -1; + } + + // Raw mode (disable all processing). + cfmakeraw(&toptions); + + // 8N1, no parity. + toptions.c_cflag &= ~CSTOPB; + toptions.c_cflag |= CS8; + toptions.c_cflag &= ~PARENB; + + // Enable receiver, ignore modem control lines + toptions.c_cflag |= CREAD | CLOCAL; + + // Blocking, single-byte reads. + toptions.c_cc[VMIN] = 1; + toptions.c_cc[VTIME] = 0; + + // Enable HW RTS/CTS flow control. + toptions.c_iflag &= ~(IXON | IXOFF | IXANY); + toptions.c_cflag |= CRTSCTS; + + // 1Mbit (TODO: make this configurable). + speed_t brate = B1000000; + cfsetospeed(&toptions, brate); + cfsetispeed(&toptions, brate); + + // Apply immediately. + if (tcsetattr(uart_fd, TCSANOW, &toptions) < 0) { + DEBUG_printf("Couldn't set term attributes"); + return -1; + } + + return 0; +} + +// HCI UART bindings. +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { + (void)port; + (void)baudrate; + + DEBUG_printf("mp_bluetooth_hci_uart_init (unix)\n"); + + char uart_device_name[256] = "/dev/ttyUSB0"; + + char *path = getenv("MICROPYBTUART"); + if (path != NULL) { + strcpy(uart_device_name, path); + } + DEBUG_printf("Using HCI UART: %s\n", uart_device_name); + + int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; + uart_fd = open(uart_device_name, flags); + if (uart_fd == -1) { + DEBUG_printf("Unable to open port %s", uart_device_name); + return -1; + } + + if (configure_uart()) { + return -1; + } + + // Create a thread to run the polling loop. + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_create(&hci_poll_thread_id, &attr, &hci_poll_thread, NULL); + + return 0; +} + +int mp_bluetooth_hci_uart_deinit(void) { + DEBUG_printf("mp_bluetooth_hci_uart_deinit\n"); + + // Wait for the poll loop to terminate when the state is set to OFF. + pthread_join(hci_poll_thread_id, NULL); + + // Close the UART. + close(uart_fd); + uart_fd = -1; + + return 0; +} + +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { + (void)baudrate; + DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate\n"); + return 0; +} + +int mp_bluetooth_hci_uart_readchar(void) { + // DEBUG_printf("mp_bluetooth_hci_uart_readchar\n"); + + uint8_t c; + ssize_t bytes_read = read(uart_fd, &c, 1); + + if (bytes_read == 1) { + #if DEBUG_HCI_DUMP + printf("[% 8ld] RX: %02x\n", mp_hal_ticks_ms(), c); + #endif + return c; + } else { + return -1; + } +} + +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { + // DEBUG_printf("mp_bluetooth_hci_uart_write\n"); + + #if DEBUG_HCI_DUMP + printf("[% 8ld] TX: %02x", mp_hal_ticks_ms(), buf[0]); + for (size_t i = 1; i < len; ++i) { + printf(":%02x", buf[i]); + } + printf("\n"); + #endif + + return write(uart_fd, buf, len); +} + +// No-op implementations of HCI controller interface. +int mp_bluetooth_hci_controller_init(void) { + return 0; +} + +int mp_bluetooth_hci_controller_deinit(void) { + return 0; +} + +int mp_bluetooth_hci_controller_sleep_maybe(void) { + return 0; +} + +bool mp_bluetooth_hci_controller_woken(void) { + return true; +} + +int mp_bluetooth_hci_controller_wakeup(void) { + return 0; +} + +#endif // MICROPY_PY_BLUETOOTH && (MICROPY_BLUETOOTH_NIMBLE || (MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4)) diff --git a/ports/unix/mpbtstackport.h b/ports/unix/mpbtstackport.h index 29433cc7fb006..c82e8bd812d7a 100644 --- a/ports/unix/mpbtstackport.h +++ b/ports/unix/mpbtstackport.h @@ -32,6 +32,11 @@ bool mp_bluetooth_hci_poll(void); +#if MICROPY_BLUETOOTH_BTSTACK_H4 +void mp_bluetooth_hci_poll_h4(void); +void mp_bluetooth_btstack_port_init_h4(void); +#endif + #if MICROPY_BLUETOOTH_BTSTACK_USB void mp_bluetooth_btstack_port_init_usb(void); #endif diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c index 402cf95283441..eb1b1c9c8042b 100644 --- a/ports/unix/mpbtstackport_common.c +++ b/ports/unix/mpbtstackport_common.c @@ -45,6 +45,9 @@ bool mp_bluetooth_hci_poll(void) { if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_HALTING) { // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); + #if MICROPY_BLUETOOTH_BTSTACK_H4 + mp_bluetooth_hci_poll_h4(); + #endif btstack_run_loop_embedded_execute_once(); MICROPY_END_ATOMIC_SECTION(atomic_state); @@ -81,6 +84,10 @@ void mp_bluetooth_btstack_port_init(void) { // hci_dump_open(NULL, HCI_DUMP_STDOUT); + #if MICROPY_BLUETOOTH_BTSTACK_H4 + mp_bluetooth_btstack_port_init_h4(); + #endif + #if MICROPY_BLUETOOTH_BTSTACK_USB mp_bluetooth_btstack_port_init_usb(); #endif diff --git a/ports/unix/mpbtstackport_h4.c b/ports/unix/mpbtstackport_h4.c new file mode 100644 index 0000000000000..4fdc20c22b69b --- /dev/null +++ b/ports/unix/mpbtstackport_h4.c @@ -0,0 +1,80 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4 + +#include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h" + +#include "extmod/btstack/btstack_hci_uart.h" +#include "extmod/btstack/modbluetooth_btstack.h" + +#include "mpbtstackport.h" + +#define DEBUG_printf(...) // printf(__VA_ARGS__) + +STATIC hci_transport_config_uart_t hci_transport_config_uart = { + HCI_TRANSPORT_CONFIG_UART, + 1000000, // initial baudrate + 0, // main baudrate + 1, // flow control + NULL, // device name +}; + +void mp_bluetooth_hci_poll_h4(void) { + if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { + mp_bluetooth_btstack_hci_uart_process(); + } +} + +void mp_bluetooth_btstack_port_init_h4(void) { + DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n"); + + const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); + hci_init(transport, &hci_transport_config_uart); + + hci_set_chipset(btstack_chipset_zephyr_instance()); +} + +void mp_bluetooth_btstack_port_deinit(void) { + DEBUG_printf("mp_bluetooth_btstack_port_deinit\n"); + + hci_power_control(HCI_POWER_OFF); + hci_close(); +} + +void mp_bluetooth_btstack_port_start(void) { + DEBUG_printf("mp_bluetooth_btstack_port_start\n"); + + hci_power_control(HCI_POWER_ON); +} + +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4 diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 9e24ab10cd263..c74d2fd84aa5f 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -314,6 +314,11 @@ void mp_unix_mark_exec(void); struct _mp_bluetooth_btstack_root_pointers_t; #define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers; #endif +#if MICROPY_BLUETOOTH_NIMBLE +struct _mp_bluetooth_nimble_root_pointers_t; +struct _mp_bluetooth_nimble_malloc_t; +#define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_nimble_malloc_t *bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers; +#endif #else #define MICROPY_BLUETOOTH_ROOT_POINTERS #endif @@ -353,7 +358,7 @@ struct _mp_bluetooth_btstack_root_pointers_t; #endif #if MICROPY_PY_THREAD -#define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0) +#define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0xffffffff) #define MICROPY_END_ATOMIC_SECTION(x) (void)x; mp_thread_unix_end_atomic_section() #endif diff --git a/ports/unix/mpnimbleport.c b/ports/unix/mpnimbleport.c new file mode 100644 index 0000000000000..8961910098c42 --- /dev/null +++ b/ports/unix/mpnimbleport.c @@ -0,0 +1,84 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" + +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE + +#include "nimble/nimble_npl.h" + +#include "extmod/nimble/modbluetooth_nimble.h" +#include "extmod/nimble/hal/hal_uart.h" + +#define DEBUG_printf(...) // printf(__VA_ARGS__) + +// Called by the UART polling thread in mpbthciport.c. +bool mp_bluetooth_hci_poll(void) { + // DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) %d\n", mp_bluetooth_nimble_ble_state); + + if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { + DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) -- shutdown\n"); + return false; + } + + if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { + + // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); + + // Ask NimBLE to process UART data. + mp_bluetooth_nimble_hci_uart_process(); + + // Run pending background operations and events, but only after HCI sync. + mp_bluetooth_nimble_os_callout_process(); + mp_bluetooth_nimble_os_eventq_run_all(); + + MICROPY_END_ATOMIC_SECTION(atomic_state); + } + + return true; +} + +// Extra port-specific helpers. +void mp_bluetooth_nimble_hci_uart_wfi(void) { + // DEBUG_printf("mp_bluetooth_nimble_hci_uart_wfi\n"); + // TODO: this should do a select() on the uart_fd. +} + +uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) { + // DEBUG_printf("mp_bluetooth_nimble_hci_uart_enter_critical\n"); + MICROPY_PY_BLUETOOTH_ENTER + return atomic_state; // Always 0xffffffff +} + +void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) { + MICROPY_PY_BLUETOOTH_EXIT + // DEBUG_printf("mp_bluetooth_nimble_hci_uart_exit_critical\n"); +} + +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/ports/unix/mpnimbleport.h b/ports/unix/mpnimbleport.h new file mode 100644 index 0000000000000..a2935e6fdf44a --- /dev/null +++ b/ports/unix/mpnimbleport.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_UNIX_NIMBLE_PORT_H +#define MICROPY_INCLUDED_UNIX_NIMBLE_PORT_H + +#define MICROPY_HW_BLE_UART_ID (0) +#define MICROPY_HW_BLE_UART_BAUDRATE (1000000) + +#endif // MICROPY_INCLUDED_UNIX_NIMBLE_PORT_H diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 66e694e0a9ea0..f11d0b0d28f75 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -17,4 +17,3 @@ MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_VFS_FAT = 1 MICROPY_VFS_LFS1 = 1 MICROPY_VFS_LFS2 = 1 -MICROPY_PY_BLUETOOTH = 1 diff --git a/ports/unix/variants/dev/mpconfigvariant.mk b/ports/unix/variants/dev/mpconfigvariant.mk index 1f8611b6fc30b..91bd28da9bd85 100644 --- a/ports/unix/variants/dev/mpconfigvariant.mk +++ b/ports/unix/variants/dev/mpconfigvariant.mk @@ -6,4 +6,5 @@ MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_VFS_FAT = 1 MICROPY_VFS_LFS1 = 1 MICROPY_VFS_LFS2 = 1 -MICROPY_PY_BLUETOOTH = 1 + +MICROPY_PY_BLUETOOTH ?= 1 From c4af714d58213bb23b81f3736f785d897e60ac77 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:47:21 +1000 Subject: [PATCH 0251/3533] extmod/modbluetooth: Implement configuration of address modes. Changes `BLE.config('mac')` to return a tuple (addr_mode, addr). Adds `BLE.config(addr_mode=...)` to set the addressing mode. --- extmod/btstack/modbluetooth_btstack.c | 122 ++++++++++++++++++++++++- extmod/modbluetooth.c | 11 ++- extmod/modbluetooth.h | 21 ++--- extmod/nimble/modbluetooth_nimble.c | 127 +++++++++++++++++--------- ports/unix/mpbtstackport_common.c | 3 - 5 files changed, 222 insertions(+), 62 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 03ec12b734346..b028a5d1ac94c 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -271,6 +271,14 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan } } +#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS +// During startup, the controller (e.g. Zephyr) might give us a static address that we can use. +STATIC uint8_t controller_static_addr[6] = {0}; +STATIC bool controller_static_addr_available = false; + +STATIC const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; +#endif + STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) { DEBUG_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet); if (packet_type != HCI_EVENT_PACKET) { @@ -313,6 +321,13 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_printf(" --> hci transport packet sent\n"); } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { DEBUG_printf(" --> hci command complete\n"); + #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) { + DEBUG_printf(" --> static address available\n"); + reverse_48(&packet[7], controller_static_addr); + controller_static_addr_available = true; + } + #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS } else if (event_type == HCI_EVENT_COMMAND_STATUS) { DEBUG_printf(" --> hci command status\n"); } else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) { @@ -493,6 +508,60 @@ STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) { mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT; } +STATIC void btstack_static_address_ready(void *arg) { + DEBUG_printf("btstack_static_address_ready.\n"); + *(volatile bool *)arg = true; +} + +STATIC bool set_public_address(void) { + bd_addr_t local_addr; + gap_local_bd_addr(local_addr); + bd_addr_t null_addr = {0}; + if (memcmp(local_addr, null_addr, 6) == 0) { + DEBUG_printf("set_public_address: No public address available.\n"); + return false; + } + DEBUG_printf("set_public_address: Using controller's public address.\n"); + gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_OFF); + return true; +} + +STATIC void set_random_address(void) { + #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + if (controller_static_addr_available) { + DEBUG_printf("set_random_address: Using static address supplied by controller.\n"); + gap_random_address_set(controller_static_addr); + } else + #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + { + DEBUG_printf("set_random_address: Generating random static address.\n"); + btstack_crypto_random_t sm_crypto_random_request; + bd_addr_t static_addr; + volatile bool ready = false; + btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready); + while (!ready) { + MICROPY_EVENT_POLL_HOOK + } + DEBUG_printf("set_random_address: Address generated.\n"); + gap_random_address_set(static_addr); + } + + // Wait for the controller to accept this address. + while (true) { + uint8_t addr_type; + bd_addr_t addr; + gap_le_get_own_address(&addr_type, addr); + + bd_addr_t null_addr = {0}; + if (memcmp(addr, null_addr, 6) != 0) { + break; + } + + MICROPY_EVENT_POLL_HOOK + } + DEBUG_printf("set_random_address: Address loaded by controller\n"); +} + int mp_bluetooth_init(void) { DEBUG_printf("mp_bluetooth_init\n"); @@ -505,6 +574,10 @@ int mp_bluetooth_init(void) { btstack_memory_init(); + #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + controller_static_addr_available = false; + #endif + MP_STATE_PORT(bluetooth_btstack_root_pointers) = m_new0(mp_bluetooth_btstack_root_pointers_t, 1); mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db); @@ -563,9 +636,23 @@ int mp_bluetooth_init(void) { // Clean up. MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; + return MP_ETIMEDOUT; } + DEBUG_printf("mp_bluetooth_init: stack startup complete\n"); + + // At this point if the controller has its own public address, btstack will know this. + // However, if this is not available, then attempt to get a static address: + // - For a Zephyr controller on nRF, a static address will be available during startup. + // - Otherwise we ask the controller to generate a static address for us. + // In either case, calling gap_random_address_set will set the mode to STATIC, and then + // immediately set the address on the controller. We then wait until this address becomes available. + + if (!set_public_address()) { + set_random_address(); + } + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles. gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler_generic, GATT_CLIENT_ANY_CONNECTION, NULL); @@ -612,8 +699,39 @@ bool mp_bluetooth_is_active(void) { return mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE; } -void mp_bluetooth_get_device_addr(uint8_t *addr) { - mp_hal_get_mac(MP_HAL_MAC_BDADDR, addr); +void mp_bluetooth_get_current_address(uint8_t *addr_type, uint8_t *addr) { + if (!mp_bluetooth_is_active()) { + mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE); + } + + DEBUG_printf("mp_bluetooth_get_current_address\n"); + gap_le_get_own_address(addr_type, addr); +} + +void mp_bluetooth_set_address_mode(uint8_t addr_mode) { + if (!mp_bluetooth_is_active()) { + mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE); + } + + switch (addr_mode) { + case MP_BLUETOOTH_ADDRESS_MODE_PUBLIC: { + DEBUG_printf("mp_bluetooth_set_address_mode: public\n"); + if (!set_public_address()) { + // No public address available. + mp_raise_OSError(MP_EINVAL); + } + break; + } + case MP_BLUETOOTH_ADDRESS_MODE_RANDOM: { + DEBUG_printf("mp_bluetooth_set_address_mode: random\n"); + set_random_address(); + break; + } + case MP_BLUETOOTH_ADDRESS_MODE_RPA: + case MP_BLUETOOTH_ADDRESS_MODE_NRPA: + // Not yet supported. + mp_raise_OSError(MP_EINVAL); + } } size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) { diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 82efe49385377..a8a762ce32f24 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -308,9 +308,11 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map return mp_obj_new_bytes(buf, len); } case MP_QSTR_mac: { + uint8_t addr_type; uint8_t addr[6]; - mp_bluetooth_get_device_addr(addr); - return mp_obj_new_bytes(addr, MP_ARRAY_SIZE(addr)); + mp_bluetooth_get_current_address(&addr_type, addr); + mp_obj_t items[] = { MP_OBJ_NEW_SMALL_INT(addr_type), mp_obj_new_bytes(addr, MP_ARRAY_SIZE(addr)) }; + return mp_obj_new_tuple(2, items); } case MP_QSTR_rxbuf: return mp_obj_new_int(self->ringbuf.size); @@ -366,6 +368,11 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map m_del(uint8_t, old_irq_data_buf, old_irq_data_alloc); break; } + case MP_QSTR_addr_mode: { + mp_int_t addr_mode = mp_obj_get_int(e->value); + mp_bluetooth_set_address_mode(addr_mode); + break; + } default: mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); } diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 2c07683124232..23ff97bc61806 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -79,15 +79,6 @@ #define MP_BLUETOOTH_UUID_TYPE_32 (4) #define MP_BLUETOOTH_UUID_TYPE_128 (16) -// Address types (for the addr_type params). -// Ports will need to map these to their own values. -#define MP_BLUETOOTH_ADDR_PUBLIC (0x00) // Public (identity) address. (Same as NimBLE and NRF SD) -#define MP_BLUETOOTH_ADDR_RANDOM_STATIC (0x01) // Random static (identity) address. (Same as NimBLE and NRF SD) -#define MP_BLUETOOTH_ADDR_PUBLIC_ID (0x02) // (Same as NimBLE) -#define MP_BLUETOOTH_ADDR_RANDOM_ID (0x03) // (Same as NimBLE) -#define MP_BLUETOOTH_ADDR_RANDOM_PRIVATE_RESOLVABLE (0x12) // Random private resolvable address. (NRF SD 0x02) -#define MP_BLUETOOTH_ADDR_RANDOM_PRIVATE_NON_RESOLVABLE (0x13) // Random private non-resolvable address. (NRF SD 0x03) - // Event codes for the IRQ handler. #define MP_BLUETOOTH_IRQ_CENTRAL_CONNECT (1) #define MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT (2) @@ -110,6 +101,11 @@ #define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19) #define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20) +#define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) +#define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) +#define MP_BLUETOOTH_ADDRESS_MODE_RPA (2) +#define MP_BLUETOOTH_ADDRESS_MODE_NRPA (3) + /* These aren't included in the module for space reasons, but can be used in your Python code if necessary. @@ -173,8 +169,11 @@ void mp_bluetooth_deinit(void); // Returns true when the Bluetooth stack is active. bool mp_bluetooth_is_active(void); -// Gets the MAC addr of this device in big-endian format. -void mp_bluetooth_get_device_addr(uint8_t *addr); +// Gets the current address of this device in big-endian format. +void mp_bluetooth_get_current_address(uint8_t *addr_type, uint8_t *addr); + +// Sets the addressing mode to use. +void mp_bluetooth_set_address_mode(uint8_t addr_mode); // Get or set the GAP device name that will be used by service 0x1800, characteristic 0x2a00. size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 61ba3a3aba696..1c782943ab485 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -50,6 +50,8 @@ #define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV +STATIC uint8_t nimble_address_mode = BLE_OWN_ADDR_RANDOM; + // Any BLE_HS_xxx code not in this table will default to MP_EIO. STATIC int8_t ble_hs_err_to_errno_table[] = { [BLE_HS_EAGAIN] = MP_EAGAIN, @@ -148,9 +150,26 @@ STATIC void reset_cb(int reason) { (void)reason; } -STATIC void sync_cb(void) { +STATIC bool has_public_address(void) { + return ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, NULL, NULL) == 0; +} + +STATIC void set_random_address(bool nrpa) { int rc; + (void)rc; + DEBUG_printf("sync_cb: Generating random static address\n"); ble_addr_t addr; + rc = ble_hs_id_gen_rnd(nrpa ? 1 : 0, &addr); + assert(rc == 0); + rc = ble_hs_id_set_rnd(addr.val); + assert(rc == 0); + rc = ble_hs_util_ensure_addr(1); + assert(rc == 0); +} + +STATIC void sync_cb(void) { + int rc; + (void)rc; DEBUG_printf("sync_cb: state=%d\n", mp_bluetooth_nimble_ble_state); @@ -158,25 +177,11 @@ STATIC void sync_cb(void) { return; } - rc = ble_hs_util_ensure_addr(0); // prefer public address - if (rc != 0) { - // https://mynewt.apache.org/latest/tutorials/ble/eddystone.html#configure-the-nimble-stack-with-an-address - #if MICROPY_PY_BLUETOOTH_RANDOM_ADDR - rc = ble_hs_id_gen_rnd(1, &addr); - assert(rc == 0); - rc = ble_hs_id_set_rnd(addr.val); - assert(rc == 0); - #else - uint8_t addr_be[6]; - mp_hal_get_mac(MP_HAL_MAC_BDADDR, addr_be); - reverse_addr_byte_order(addr.val, addr_be); - // ble_hs_id_set_pub(addr.val); - rc = ble_hs_id_set_rnd(addr.val); - assert(rc == 0); - #endif - - rc = ble_hs_util_ensure_addr(0); // prefer public address - assert(rc == 0); + if (has_public_address()) { + nimble_address_mode = BLE_OWN_ADDR_PUBLIC; + } else { + nimble_address_mode = BLE_OWN_ADDR_RANDOM; + set_random_address(false); } if (MP_BLUETOOTH_DEFAULT_ATTR_LEN > 20) { @@ -412,19 +417,65 @@ bool mp_bluetooth_is_active(void) { return mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE; } -void mp_bluetooth_get_device_addr(uint8_t *addr) { - #if MICROPY_PY_BLUETOOTH_RANDOM_ADDR +void mp_bluetooth_get_current_address(uint8_t *addr_type, uint8_t *addr) { + if (!mp_bluetooth_is_active()) { + mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE); + } + uint8_t addr_le[6]; - int rc = ble_hs_id_copy_addr(BLE_ADDR_RANDOM, addr_le, NULL); + + switch (nimble_address_mode) { + case BLE_OWN_ADDR_PUBLIC: + *addr_type = BLE_ADDR_PUBLIC; + break; + case BLE_OWN_ADDR_RANDOM: + *addr_type = BLE_ADDR_RANDOM; + break; + case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT: + case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT: + default: + // TODO: If RPA/NRPA in use, get the current value. + // Is this even possible in NimBLE? + mp_raise_OSError(MP_EINVAL); + } + + int rc = ble_hs_id_copy_addr(*addr_type, addr_le, NULL); if (rc != 0) { - // Even with MICROPY_PY_BLUETOOTH_RANDOM_ADDR enabled the public address may - // be used instead, in which case there is no random address. - ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr_le, NULL); + mp_raise_OSError(MP_EINVAL); } reverse_addr_byte_order(addr, addr_le); - #else - mp_hal_get_mac(MP_HAL_MAC_BDADDR, addr); - #endif +} + +void mp_bluetooth_set_address_mode(uint8_t addr_mode) { + switch (addr_mode) { + case MP_BLUETOOTH_ADDRESS_MODE_PUBLIC: + if (!has_public_address()) { + // No public address available. + mp_raise_OSError(MP_EINVAL); + } + nimble_address_mode = BLE_OWN_ADDR_PUBLIC; + break; + case MP_BLUETOOTH_ADDRESS_MODE_RANDOM: + // Generate an static random address. + set_random_address(false); + nimble_address_mode = BLE_OWN_ADDR_RANDOM; + break; + case MP_BLUETOOTH_ADDRESS_MODE_RPA: + if (has_public_address()) { + nimble_address_mode = BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT; + } else { + // Generate an static random address to use as the identity address. + set_random_address(false); + nimble_address_mode = BLE_OWN_ADDR_RPA_RANDOM_DEFAULT; + } + break; + case MP_BLUETOOTH_ADDRESS_MODE_NRPA: + // Generate an NRPA. + set_random_address(true); + // In NimBLE, NRPA is treated like a static random address that happens to be an NRPA. + nimble_address_mode = BLE_OWN_ADDR_RANDOM; + break; + } } size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) { @@ -473,19 +524,7 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons .channel_map = 7, // all 3 channels. }; - ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); - if (ret == 0) { - return 0; - } - ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); - if (ret == 0) { - return 0; - } - ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_RANDOM_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); - if (ret == 0) { - return 0; - } - ret = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); + ret = ble_gap_adv_start(nimble_address_mode, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); if (ret == 0) { return 0; } @@ -756,7 +795,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ .passive = active_scan ? 0 : 1, .filter_duplicates = 0, }; - int err = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, duration_ms, &discover_params, gap_scan_cb, NULL); + int err = ble_gap_disc(nimble_address_mode, duration_ms, &discover_params, gap_scan_cb, NULL); return ble_hs_err_to_errno(err); } @@ -847,7 +886,7 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, }; ble_addr_t addr_nimble = create_nimble_addr(addr_type, addr); - int err = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &addr_nimble, duration_ms, ¶ms, &peripheral_gap_event_cb, NULL); + int err = ble_gap_connect(nimble_address_mode, &addr_nimble, duration_ms, ¶ms, &peripheral_gap_event_cb, NULL); return ble_hs_err_to_errno(err); } diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c index eb1b1c9c8042b..621e661f9eba3 100644 --- a/ports/unix/mpbtstackport_common.c +++ b/ports/unix/mpbtstackport_common.c @@ -93,7 +93,4 @@ void mp_bluetooth_btstack_port_init(void) { #endif } -void mp_hal_get_mac(int idx, uint8_t buf[6]) { -} - #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK From 26b66804e96b78395cddc37e195fc36862837d97 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:16:29 +1000 Subject: [PATCH 0252/3533] tests/multi_bluetooth: Update to new config('mac') behaviour. --- tests/multi_bluetooth/ble_characteristic.py | 2 +- tests/multi_bluetooth/ble_gap_advertise.py | 2 +- tests/multi_bluetooth/ble_gap_connect.py | 4 ++-- tests/multi_bluetooth/ble_gap_device_name.py | 2 +- tests/multi_bluetooth/ble_gatt_data_transfer.py | 2 +- tests/multi_bluetooth/ble_gattc_discover_services.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index 0d72c181fdec6..d918d9aefcf03 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -146,7 +146,7 @@ def instance1(): try: # Connect to peripheral and then disconnect. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return conn_handle, _, _ = waiting_data diff --git a/tests/multi_bluetooth/ble_gap_advertise.py b/tests/multi_bluetooth/ble_gap_advertise.py index 71a5b58a56f7a..bb6ff8e425461 100644 --- a/tests/multi_bluetooth/ble_gap_advertise.py +++ b/tests/multi_bluetooth/ble_gap_advertise.py @@ -36,7 +36,7 @@ def instance1(): def irq(ev, data): nonlocal finished, adv_types, adv_data if ev == _IRQ_SCAN_RESULT: - if data[1] == BDADDR: + if data[0] == BDADDR[0] and data[1] == BDADDR[1]: adv_types.add(data[2]) if adv_data is None: adv_data = bytes(data[4]) diff --git a/tests/multi_bluetooth/ble_gap_connect.py b/tests/multi_bluetooth/ble_gap_connect.py index 8e3ed8b816b3a..ba9c28230d7da 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py +++ b/tests/multi_bluetooth/ble_gap_connect.py @@ -76,7 +76,7 @@ def instance1(): try: # Connect to peripheral and then disconnect. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return print("gap_disconnect:", ble.gap_disconnect(waiting_data[0])) @@ -88,7 +88,7 @@ def instance1(): # Connect to peripheral and then let the peripheral disconnect us. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py index dafa367128b49..92ea94278a647 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py +++ b/tests/multi_bluetooth/ble_gap_device_name.py @@ -97,7 +97,7 @@ def instance1(): # Connect to peripheral. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return conn_handle, _, _ = waiting_data diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py b/tests/multi_bluetooth/ble_gatt_data_transfer.py index dba0c333be672..240f048607c92 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py @@ -137,7 +137,7 @@ def instance1(): try: # Connect to peripheral and then disconnect. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return conn_handle, _, _ = waiting_data diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py b/tests/multi_bluetooth/ble_gattc_discover_services.py index e746b87458e1b..57f95bf0125e7 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py @@ -85,7 +85,7 @@ def instance1(): try: # Connect to peripheral and then disconnect. print("gap_connect") - ble.gap_connect(0, BDADDR) + ble.gap_connect(*BDADDR) if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): return conn_handle, _, _ = waiting_data From 67d8139e2b4cbaba5c722feef4e5478ae647344d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 14 Aug 2020 15:17:14 +1000 Subject: [PATCH 0253/3533] docs/library/ubluetooth.rst: Document BLE address modes. --- docs/library/ubluetooth.rst | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 0cac16f5f4ad9..f8dad7b8d1ed2 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -45,12 +45,22 @@ Configuration Currently supported values are: - - ``'mac'``: Returns the device MAC address. If a device has a fixed address - (e.g. PYBD) then it will be returned. Otherwise (e.g. ESP32) a random - address will be generated when the BLE interface is made active. + - ``'mac'``: The current address in use, depending on the current address mode. + This returns a tuple of ``(addr_type, addr)``. - **Note:** on some ports, accessing this value requires that the interface is - active (so that the MAC address can be queried from the controller). + See :meth:`gatts_write ` for details about address type. + + This may only be queried while the interface is currently active. + + - ``'addr_mode'``: Sets the address mode. Values can be: + + * 0x00 - PUBLIC - Use the controller's public address. + * 0x01 - RANDOM - Use a generated static address. + * 0x02 - RPA - Use resolvable private addresses. + * 0x03 - NRPA - Use non-resolvable private addresses. + + By default the interface mode will use a PUBLIC address if available, otherwise + it will use a RANDOM address. - ``'gap_name'``: Get/set the GAP device name used by service 0x1800, characteristic 0x2a00. This can be set at any time and changed multiple @@ -219,8 +229,13 @@ Observer Role (Scanner) (background scanning). For each scan result the ``_IRQ_SCAN_RESULT`` event will be raised, with event - data ``(addr_type, addr, adv_type, rssi, adv_data)``. ``adv_type`` values correspond - to the Bluetooth Specification: + data ``(addr_type, addr, adv_type, rssi, adv_data)``. + + ``addr_type`` values indicate public or random addresses: + * 0x00 - PUBLIC + * 0x01 - RANDOM (either static, RPA, or NRPA, the type is encoded in the address itself) + + ``adv_type`` values correspond to the Bluetooth Specification: * 0x00 - ADV_IND - connectable and scannable undirected advertising * 0x01 - ADV_DIRECT_IND - connectable directed advertising @@ -342,6 +357,8 @@ Central Role (GATT Client) Connect to a peripheral. + See :meth:`gatts_write ` for details about address types. + On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised. .. method:: BLE.gap_disconnect(conn_handle) From 52a2ce45de353267776aaa25856c9d144653f142 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 17 Aug 2020 10:53:00 +1000 Subject: [PATCH 0254/3533] extmod/modbluetooth: Allow using mp_hal_get_mac as a static address. Generally a controller should either have its own public address hardcoded, or loaded by the driver (e.g. cywbt43). However, for a controller that has no public address where you still want a long-term stable address, this allows you to use a static address generated by the port. Typically on STM32 this will be an LAA, but a board might override this. --- extmod/btstack/modbluetooth_btstack.c | 17 ++++++++++++++++- extmod/nimble/modbluetooth_nimble.c | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index b028a5d1ac94c..773234b9b2600 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -508,10 +508,12 @@ STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) { mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT; } +#if !MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS STATIC void btstack_static_address_ready(void *arg) { DEBUG_printf("btstack_static_address_ready.\n"); *(volatile bool *)arg = true; } +#endif STATIC bool set_public_address(void) { bd_addr_t local_addr; @@ -534,14 +536,27 @@ STATIC void set_random_address(void) { } else #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS { + bd_addr_t static_addr; + + #if MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + + DEBUG_printf("set_random_address: Generating static address using mp_hal_get_mac\n"); + mp_hal_get_mac(MP_HAL_MAC_BDADDR, static_addr); + // Mark it as STATIC (not RPA or NRPA). + static_addr[0] |= 0xc0; + + #else + DEBUG_printf("set_random_address: Generating random static address.\n"); btstack_crypto_random_t sm_crypto_random_request; - bd_addr_t static_addr; volatile bool ready = false; btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready); while (!ready) { MICROPY_EVENT_POLL_HOOK } + + #endif // MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + DEBUG_printf("set_random_address: Address generated.\n"); gap_random_address_set(static_addr); } diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 1c782943ab485..4222f58fa8812 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -157,10 +157,22 @@ STATIC bool has_public_address(void) { STATIC void set_random_address(bool nrpa) { int rc; (void)rc; - DEBUG_printf("sync_cb: Generating random static address\n"); ble_addr_t addr; - rc = ble_hs_id_gen_rnd(nrpa ? 1 : 0, &addr); - assert(rc == 0); + #if MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + if (!nrpa) { + DEBUG_printf("set_random_address: Generating static address using mp_hal_get_mac\n"); + uint8_t hal_mac_addr[6]; + mp_hal_get_mac(MP_HAL_MAC_BDADDR, hal_mac_addr); + addr = create_nimble_addr(BLE_ADDR_RANDOM, hal_mac_addr); + // Mark it as STATIC (not RPA or NRPA). + addr.val[5] |= 0xc0; + } else + #endif + { + DEBUG_printf("set_random_address: Generating random static address\n"); + rc = ble_hs_id_gen_rnd(nrpa ? 1 : 0, &addr); + assert(rc == 0); + } rc = ble_hs_id_set_rnd(addr.val); assert(rc == 0); rc = ble_hs_util_ensure_addr(1); From 8b00aeab8fd20f8b8920cf322b5cfb7021ef1504 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Sat, 15 Aug 2020 14:06:19 +1000 Subject: [PATCH 0255/3533] extmod/btstack: Add btstack support for _IRQ_GATTS_READ_REQUEST. --- extmod/btstack/modbluetooth_btstack.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 773234b9b2600..2e1bd47cfa462 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -825,24 +825,37 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { } STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) { + // Should return data length, 0 for error, or -1 for delayed response. + // For more details search "*att_read_callback*" in micropython/lib/btstack/doc/manual/docs/profiles.md (void)connection_handle; - DEBUG_printf("btstack: att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size); + DEBUG_printf("att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size); mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle); if (!entry) { - DEBUG_printf("btstack: att_read_callback handle not found\n"); - return 0; // TODO: Find status code for not-found. + DEBUG_printf("att_read_callback handle not found\n"); + return 0; + } + + #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK + // Allow Python code to override value (by using gatts_write), or deny (by returning false) the read. + if ((buffer == NULL) && (buffer_size == 0)) { + if (!mp_bluetooth_gatts_on_read_request(connection_handle, att_handle)) { + DEBUG_printf("att_read_callback: read request denied\n"); + return 0; + } } + #endif - return att_read_callback_handle_blob(entry->data, entry->data_len, offset, buffer, buffer_size); + uint16_t ret = att_read_callback_handle_blob(entry->data, entry->data_len, offset, buffer, buffer_size); + return ret; } STATIC int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) { (void)offset; (void)transaction_mode; - DEBUG_printf("btstack: att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size); + DEBUG_printf("att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size); mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle); if (!entry) { - DEBUG_printf("btstack: att_write_callback handle not found\n"); + DEBUG_printf("att_write_callback handle not found\n"); return 0; // TODO: Find status code for not-found. } From 6077c63a450d7c2ab5e31e5845ecb5f1a4634b26 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Sun, 16 Aug 2020 08:30:19 +1000 Subject: [PATCH 0256/3533] stm32/mpbthciport: Increase char timeout of BT HCI UART. The 2ms used previously was not long enough and it could lose HCI sync. Also print error on tx failure to make this more obvious in the future. --- ports/stm32/mpbthciport.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index 71a9e4fc7f433..a5977ff12c210 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -142,8 +142,9 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { mp_bluetooth_hci_uart_obj.base.type = &pyb_uart_type; mp_bluetooth_hci_uart_obj.uart_id = port; mp_bluetooth_hci_uart_obj.is_static = true; - mp_bluetooth_hci_uart_obj.timeout = 2; - mp_bluetooth_hci_uart_obj.timeout_char = 2; + // We don't want to block indefinitely, but expect flow control is doing its job. + mp_bluetooth_hci_uart_obj.timeout = 200; + mp_bluetooth_hci_uart_obj.timeout_char = 200; MP_STATE_PORT(pyb_uart_obj_all)[mp_bluetooth_hci_uart_obj.uart_id - 1] = &mp_bluetooth_hci_uart_obj; // This also initialises the UART. @@ -184,7 +185,11 @@ int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { // DEBUG_printf("mp_bluetooth_hci_uart_write (stm32)\n"); mp_bluetooth_hci_controller_wakeup(); - uart_tx_strn(&mp_bluetooth_hci_uart_obj, (void *)buf, len); + int errcode; + uart_tx_data(&mp_bluetooth_hci_uart_obj, (void *)buf, len, &errcode); + if (errcode != 0) { + printf("\nmp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); + } return 0; } From 99a29ec705b463290d5a2ac1eabc46fb7f2a83b0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 18 Aug 2020 11:05:34 +1000 Subject: [PATCH 0257/3533] extmod/btstack: Detect HCI UART init failure. --- extmod/btstack/btstack_hci_uart.c | 14 +++++++++++--- extmod/btstack/modbluetooth_btstack.c | 7 ++++++- ports/unix/mpbthciport.c | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/extmod/btstack/btstack_hci_uart.c b/extmod/btstack/btstack_hci_uart.c index 5c96e02dc0fed..ae18628a9cf0e 100644 --- a/extmod/btstack/btstack_hci_uart.c +++ b/extmod/btstack/btstack_hci_uart.c @@ -50,6 +50,7 @@ STATIC uint8_t *recv_buf; STATIC size_t recv_len; STATIC size_t recv_idx; STATIC void (*recv_handler)(void); +STATIC bool init_success = false; STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) { (void)uart_config; @@ -62,14 +63,21 @@ STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) { // Set up the UART peripheral, attach IRQ and power up the HCI controller. // We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate. - mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0); - mp_bluetooth_hci_controller_init(); + if (mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0)) { + init_success = false; + return -1; + } + if (mp_bluetooth_hci_controller_init()) { + init_success = false; + return -1; + } + init_success = true; return 0; } STATIC int btstack_uart_open(void) { - return 0; + return init_success ? 0 : 1; } STATIC int btstack_uart_close(void) { diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 2e1bd47cfa462..cde802a61f5f4 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -317,6 +317,9 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t // Signal that de-initialisation has completed. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; } + } else if (event_type == BTSTACK_EVENT_POWERON_FAILED) { + // Signal that initialisation has failed. + mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; } else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) { DEBUG_printf(" --> hci transport packet sent\n"); } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { @@ -644,6 +647,8 @@ int mp_bluetooth_init(void) { if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { DEBUG_printf("mp_bluetooth_init: stack startup timed out\n"); + bool timeout = mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT; + // Required to stop the polling loop. mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; // Attempt a shutdown (may not do anything). @@ -652,7 +657,7 @@ int mp_bluetooth_init(void) { // Clean up. MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; - return MP_ETIMEDOUT; + return timeout ? MP_ETIMEDOUT : MP_EINVAL; } DEBUG_printf("mp_bluetooth_init: stack startup complete\n"); diff --git a/ports/unix/mpbthciport.c b/ports/unix/mpbthciport.c index 5a5c1d4a4e79d..dbfb5e0d0e5f7 100644 --- a/ports/unix/mpbthciport.c +++ b/ports/unix/mpbthciport.c @@ -124,12 +124,12 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { if (path != NULL) { strcpy(uart_device_name, path); } - DEBUG_printf("Using HCI UART: %s\n", uart_device_name); + DEBUG_printf("mp_bluetooth_hci_uart_init: Using HCI UART: %s\n", uart_device_name); int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; uart_fd = open(uart_device_name, flags); if (uart_fd == -1) { - DEBUG_printf("Unable to open port %s", uart_device_name); + printf("mp_bluetooth_hci_uart_init: Unable to open port %s\n", uart_device_name); return -1; } From 311b8519af75be9fe4215b5ee8f77e91d089d5df Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 19 Aug 2020 10:46:09 +1000 Subject: [PATCH 0258/3533] esp32: Pin MicroPython and NimBLE tasks to core 0. MicroPython and NimBLE must be on the same core, for synchronisation of the BLE ringbuf and the MicroPython scheduler. However, in the current IDF versions (3.3 and 4.0) there are issues (see e.g. #5489) with running NimBLE on core 1. This change - pinning both tasks to core 0 - makes it possible to reliably run the BLE multitests on esp32 boards. --- ports/esp32/boards/sdkconfig.ble | 9 ++++++--- ports/esp32/mphalport.h | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ports/esp32/boards/sdkconfig.ble b/ports/esp32/boards/sdkconfig.ble index cdbb621a63112..f714ce4629b32 100644 --- a/ports/esp32/boards/sdkconfig.ble +++ b/ports/esp32/boards/sdkconfig.ble @@ -9,9 +9,12 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4 # Pin to the same core as MP. -CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=n -CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=y -CONFIG_BT_NIMBLE_PINNED_TO_CORE=1 +# Until we move to IDF 4.2+, we need NimBLE on core 0, and for synchronisation +# with the ringbuffer and scheduler MP needs to be on the same core. +# See https://github.com/micropython/micropython/issues/5489 +CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=y +CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=n +CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 # v3.3-only (renamed in 4.0) CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY=y diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index 1f78d820a3f17..60cc308d68275 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -35,8 +35,11 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -// The core that the MicroPython task(s) are pinned to -#define MP_TASK_COREID (1) +// The core that the MicroPython task(s) are pinned to. +// Until we move to IDF 4.2+, we need NimBLE on core 0, and for synchronisation +// with the ringbuffer and scheduler MP needs to be on the same core. +// See https://github.com/micropython/micropython/issues/5489 +#define MP_TASK_COREID (0) extern TaskHandle_t mp_main_task_handle; From 126f972c34f25bffc07d0d1cb3e536921fec0979 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 19 Aug 2020 12:55:38 +1000 Subject: [PATCH 0259/3533] extmod/nimble: Add timeout for HCI sync on startup. This allows `ble.active(1)` to fail correctly if the HCI controller is unavailable. It also avoids an infine loop in the NimBLE event handler where NimBLE doesn't correctly detect that the HCI controller is unavailable and keeps trying to reset. Furthermore, it fixes an issue where GATT service registrations were left allocated, which led to a bad realloc if the stack was activated multiple times. --- extmod/nimble/modbluetooth_nimble.c | 28 +++++++++++++++++++++------- extmod/nimble/nimble/nimble_npl_os.c | 15 ++++++++++++--- ports/unix/mpbthciport.c | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 4222f58fa8812..9ca90b602476f 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -52,6 +52,9 @@ STATIC uint8_t nimble_address_mode = BLE_OWN_ADDR_RANDOM; +#define NIMBLE_STARTUP_TIMEOUT 2000 +STATIC struct ble_npl_sem startup_sem; + // Any BLE_HS_xxx code not in this table will default to MP_EIO. STATIC int8_t ble_hs_err_to_errno_table[] = { [BLE_HS_EAGAIN] = MP_EAGAIN, @@ -206,6 +209,8 @@ STATIC void sync_cb(void) { ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME); mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE; + + ble_npl_sem_release(&startup_sem); } STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { @@ -355,6 +360,8 @@ int mp_bluetooth_init(void) { ble_hs_cfg.gatts_register_cb = gatts_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + ble_npl_sem_init(&startup_sem, 0); + MP_STATE_PORT(bluetooth_nimble_root_pointers) = m_new0(mp_bluetooth_nimble_root_pointers_t, 1); mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db); @@ -370,21 +377,28 @@ int mp_bluetooth_init(void) { // Initialise NimBLE memory and data structures. nimble_port_init(); - // By default, just register the default gap/gatt service. - ble_svc_gap_init(); - ble_svc_gatt_init(); - // Make sure that the HCI UART and event handling task is running. mp_bluetooth_nimble_port_start(); // Static initialization is complete, can start processing events. mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC; - // Wait for sync callback - while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { - MICROPY_EVENT_POLL_HOOK + ble_npl_sem_pend(&startup_sem, NIMBLE_STARTUP_TIMEOUT); + + if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { + mp_bluetooth_deinit(); + return MP_ETIMEDOUT; } + // By default, just register the default gap/gatt service. + ble_svc_gap_init(); + ble_svc_gatt_init(); + // The preceeding two calls allocate service definitions on the heap, + // then we must now call gatts_start to register those services + // and free the heap memory. + // Otherwise it will be realloc'ed on the next stack startup. + ble_gatts_start(); + DEBUG_printf("mp_bluetooth_init: ready\n"); return 0; diff --git a/extmod/nimble/nimble/nimble_npl_os.c b/extmod/nimble/nimble/nimble_npl_os.c index 46f7a0b291303..2ec012940f763 100644 --- a/extmod/nimble/nimble/nimble_npl_os.c +++ b/extmod/nimble/nimble/nimble_npl_os.c @@ -32,6 +32,7 @@ #include "extmod/nimble/hal/hal_uart.h" #include "extmod/modbluetooth.h" +#include "extmod/nimble/modbluetooth_nimble.h" #define DEBUG_OS_printf(...) // printf(__VA_ARGS__) #define DEBUG_MALLOC_printf(...) // printf(__VA_ARGS__) @@ -180,7 +181,8 @@ struct ble_npl_eventq *global_eventq = NULL; void mp_bluetooth_nimble_os_eventq_run_all(void) { for (struct ble_npl_eventq *evq = global_eventq; evq != NULL; evq = evq->nextq) { - while (evq->head != NULL) { + int n = 0; + while (evq->head != NULL && mp_bluetooth_nimble_ble_state > MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { struct ble_npl_event *ev = evq->head; evq->head = ev->next; if (ev->next) { @@ -191,6 +193,13 @@ void mp_bluetooth_nimble_os_eventq_run_all(void) { DEBUG_EVENT_printf("event_run(%p)\n", ev); ev->fn(ev); DEBUG_EVENT_printf("event_run(%p) done\n", ev); + + if (++n > 3) { + // Limit to running 3 tasks per queue. + // Some tasks (such as reset) can enqueue themselves + // making this an infinite loop (while in PENDSV). + break; + } } } } @@ -348,11 +357,11 @@ ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout } if (sem->count == 0) { - printf("NimBLE: HCI ACK timeout\n"); + DEBUG_SEM_printf("ble_npl_sem_pend: semaphore timeout\n"); return BLE_NPL_TIMEOUT; } - DEBUG_SEM_printf("got response in %u ms\n", (int)(mp_hal_ticks_ms() - t0)); + DEBUG_SEM_printf("ble_npl_sem_pend: acquired in %u ms\n", (int)(mp_hal_ticks_ms() - t0)); } sem->count -= 1; return BLE_NPL_OK; diff --git a/ports/unix/mpbthciport.c b/ports/unix/mpbthciport.c index dbfb5e0d0e5f7..316a8831fe97b 100644 --- a/ports/unix/mpbthciport.c +++ b/ports/unix/mpbthciport.c @@ -105,6 +105,10 @@ STATIC int configure_uart(void) { // Apply immediately. if (tcsetattr(uart_fd, TCSANOW, &toptions) < 0) { DEBUG_printf("Couldn't set term attributes"); + + close(uart_fd); + uart_fd = -1; + return -1; } @@ -149,6 +153,10 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { int mp_bluetooth_hci_uart_deinit(void) { DEBUG_printf("mp_bluetooth_hci_uart_deinit\n"); + if (uart_fd == -1) { + return 0; + } + // Wait for the poll loop to terminate when the state is set to OFF. pthread_join(hci_poll_thread_id, NULL); @@ -168,6 +176,10 @@ int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { int mp_bluetooth_hci_uart_readchar(void) { // DEBUG_printf("mp_bluetooth_hci_uart_readchar\n"); + if (uart_fd == -1) { + return -1; + } + uint8_t c; ssize_t bytes_read = read(uart_fd, &c, 1); @@ -184,6 +196,10 @@ int mp_bluetooth_hci_uart_readchar(void) { int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { // DEBUG_printf("mp_bluetooth_hci_uart_write\n"); + if (uart_fd == -1) { + return 0; + } + #if DEBUG_HCI_DUMP printf("[% 8ld] TX: %02x", mp_hal_ticks_ms(), buf[0]); for (size_t i = 1; i < len; ++i) { From b27edb8073409a3caee921dd4e2c1b182bd4c0fb Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 20 Aug 2020 23:21:06 +1000 Subject: [PATCH 0260/3533] stm32/make-stmconst.py: Add support for WB55 header files. --- ports/stm32/make-stmconst.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ports/stm32/make-stmconst.py b/ports/stm32/make-stmconst.py index 1b3cc08d951f3..ac5c56f5c79f5 100644 --- a/ports/stm32/make-stmconst.py +++ b/ports/stm32/make-stmconst.py @@ -46,7 +46,7 @@ def __init__(self, line): class Lexer: re_io_reg = r"__IO uint(?P8|16|32)_t +(?P[A-Z0-9]+)" - re_comment = r"(?P[A-Za-z0-9 \-/_()&]+)" + re_comment = r"(?P[A-Za-z0-9 \-/_()&:]+)" re_addr_offset = r"Address offset: (?P0x[0-9A-Z]{2,3})" regexs = ( ( @@ -78,16 +78,16 @@ class Lexer: ( "IO reg", re.compile( - re_io_reg + r"; */\*!< *" + re_comment + r", +" + re_addr_offset + r" *\*/" + re_io_reg + r" *; */\*!< *" + re_comment + r",? +" + re_addr_offset + r" *\*/" ), ), ( "IO reg array", re.compile( re_io_reg - + r"\[(?P[2-8])\]; */\*!< *" + + r"\[(?P[2-8])\] *; */\*!< *" + re_comment - + r", +" + + r",? +" + re_addr_offset + r"-(0x[0-9A-Z]{2,3}) *\*/" ), @@ -160,7 +160,11 @@ def parse_file(filename): if m[0] == "}": pass elif m[0] == "} TypeDef": - reg_defs[m[1].groupdict()["id"]] = regs + d = m[1].groupdict() + n = d["id"] + g = d["global"] + if n not in reg_defs or not g: + reg_defs[n] = regs else: raise LexerError(lexer.line_number) @@ -298,6 +302,7 @@ def main(): "USART", "WWDG", "RNG", + "IPCC", ): if reg in reg_defs: print_regs(reg, reg_defs[reg], needed_qstrs, needed_mpzs) From 30e8162ac49d333b00bae4a7f719a1f24692b062 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 20 Aug 2020 18:12:44 +1000 Subject: [PATCH 0261/3533] stm32/rfcore: Update rfcore.c to match how ST examples work. - Split tables and buffers into SRAM2A/2B. - Use structs rather than word offsets to access tables. - Use FLASH_IPCCDBA register value rather than option bytes directly. --- ports/stm32/boards/stm32wb55xg.ld | 13 ++- ports/stm32/rfcore.c | 178 ++++++++++++++++++++++-------- 2 files changed, 141 insertions(+), 50 deletions(-) diff --git a/ports/stm32/boards/stm32wb55xg.ld b/ports/stm32/boards/stm32wb55xg.ld index dbeccc1895775..c3dc5f5197a00 100644 --- a/ports/stm32/boards/stm32wb55xg.ld +++ b/ports/stm32/boards/stm32wb55xg.ld @@ -9,7 +9,8 @@ MEMORY FLASH_APP (rx) : ORIGIN = 0x08004000, LENGTH = 496K /* sectors 4-127 */ FLASH_FS (r) : ORIGIN = 0x08080000, LENGTH = 256K /* sectors 128-191 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K /* SRAM1 */ - RAM2A (xrw) : ORIGIN = 0x20030020, LENGTH = 8K /* SRAM2A */ + RAM2A (xrw) : ORIGIN = 0x20030000, LENGTH = 10K /* SRAM2A */ + RAM2B (xrw) : ORIGIN = 0x20038000, LENGTH = 10K /* SRAM2B */ } /* produce a link error if there is not this amount of RAM for these sections */ @@ -36,10 +37,20 @@ _flash_fs_end = ORIGIN(FLASH_FS) + LENGTH(FLASH_FS); SECTIONS { + /* Put all IPCC tables into SRAM2A. */ .ram2a_bss : { . = ALIGN(4); + . = . + 64; /* Leave room for the mb_ref_table_t (assuming IPCCDBA==0). */ *rfcore.o(.bss.ipcc_mem_*) . = ALIGN(4); } >RAM2A + + /* Put all IPCC buffers into SRAM2B. */ + .ram2b_bss : + { + . = ALIGN(4); + *rfcore.o(.bss.ipcc_membuf_*) + . = ALIGN(4); + } >RAM2B } diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 54b3393435e1e..3565c03898444 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2019 Damien P. George + * Copyright (c) 2020 Jim Mussared * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,6 +35,10 @@ #if defined(STM32WB) +#include "stm32wbxx_ll_ipcc.h" + +#define DEBUG_printf(...) // printf("rfcore: " __VA_ARGS__) + // Define to 1 to print traces of HCI packets #define HCI_TRACE (0) @@ -61,23 +66,88 @@ typedef struct _parse_hci_info_t { bool was_hci_reset_evt; } parse_hci_info_t; -static volatile uint32_t ipcc_mem_dev_info_tab[8]; -static volatile uint32_t ipcc_mem_ble_tab[4]; -static volatile uint32_t ipcc_mem_sys_tab[2]; -static volatile uint32_t ipcc_mem_memmgr_tab[7]; - -static volatile uint32_t ipcc_mem_sys_cmd_buf[272 / 4]; -static volatile tl_list_node_t ipcc_mem_sys_queue; - -static volatile tl_list_node_t ipcc_mem_memmgr_free_buf_queue; -static volatile uint32_t ipcc_mem_memmgr_ble_spare_evt_buf[272 / 4]; -static volatile uint32_t ipcc_mem_memmgr_sys_spare_evt_buf[272 / 4]; -static volatile uint32_t ipcc_mem_memmgr_evt_pool[6 * 272 / 4]; - -static volatile uint32_t ipcc_mem_ble_cmd_buf[272 / 4]; -static volatile uint32_t ipcc_mem_ble_cs_buf[272 / 4]; -static volatile tl_list_node_t ipcc_mem_ble_evt_queue; -static volatile uint32_t ipcc_mem_ble_hci_acl_data_buf[272 / 4]; +// Version +// [0:3] = Build - 0: Untracked - 15:Released - x: Tracked version +// [4:7] = branch - 0: Mass Market - x: ... +// [8:15] = Subversion +// [16:23] = Version minor +// [24:31] = Version major + +// Memory Size +// [0:7] = Flash (Number of 4k sectors) +// [8:15] = Reserved (Shall be set to 0 - may be used as flash extension) +// [16:23] = SRAM2b (Number of 1k sectors) +// [24:31] = SRAM2a (Number of 1k sectors) + +typedef struct __attribute__((packed)) _ipcc_device_info_table_t { + uint32_t safeboot_version; + uint32_t fus_version; + uint32_t fus_memorysize; + uint32_t fus_info; + uint32_t fw_version; + uint32_t fw_memorysize; + uint32_t fw_infostack; + uint32_t fw_reserved; +} ipcc_device_info_table_t; + +typedef struct __attribute__((packed)) _ipcc_ble_table_t { + uint8_t *pcmd_buffer; + uint8_t *pcs_buffer; + tl_list_node_t *pevt_queue; + uint8_t *phci_acl_data_buffer; +} ipcc_ble_table_t; + +// msg +// [0:7] = cmd/evt +// [8:31] = Reserved +typedef struct __attribute__((packed)) _ipcc_sys_table_t { + uint8_t *pcmd_buffer; + tl_list_node_t *sys_queue; +} ipcc_sys_table_t; + +typedef struct __attribute__((packed)) _ipcc_mem_manager_table_t { + uint8_t *spare_ble_buffer; + uint8_t *spare_sys_buffer; + uint8_t *blepool; + uint32_t blepoolsize; + tl_list_node_t *pevt_free_buffer_queue; + uint8_t *traces_evt_pool; + uint32_t tracespoolsize; +} ipcc_mem_manager_table_t; + +typedef struct __attribute__((packed)) _ipcc_ref_table_t { + ipcc_device_info_table_t *p_device_info_table; + ipcc_ble_table_t *p_ble_table; + void *p_thread_table; + ipcc_sys_table_t *p_sys_table; + ipcc_mem_manager_table_t *p_mem_manager_table; + void *p_traces_table; + void *p_mac_802_15_4_table; + void *p_zigbee_table; + void *p_lld_tests_table; + void *p_lld_ble_table; +} ipcc_ref_table_t; + +// The stm32wb55xg.ld script puts .bss.ipcc_mem_* into SRAM2A and .bss_ipcc_membuf_* into SRAM2B. +// It also leaves 64 bytes at the start of SRAM2A for the ref table. + +STATIC ipcc_device_info_table_t ipcc_mem_dev_info_tab; // mem1 +STATIC ipcc_ble_table_t ipcc_mem_ble_tab; // mem1 +STATIC ipcc_sys_table_t ipcc_mem_sys_tab; // mem1 +STATIC ipcc_mem_manager_table_t ipcc_mem_memmgr_tab; // mem1 + +STATIC uint8_t ipcc_membuf_sys_cmd_buf[272]; // mem2 +STATIC tl_list_node_t ipcc_mem_sys_queue; // mem1 + +STATIC tl_list_node_t ipcc_mem_memmgr_free_buf_queue; // mem1 +STATIC uint8_t ipcc_membuf_memmgr_ble_spare_evt_buf[272]; // mem2 +STATIC uint8_t ipcc_membuf_memmgr_sys_spare_evt_buf[272]; // mem2 +STATIC uint8_t ipcc_membuf_memmgr_evt_pool[6 * 272]; // mem2 + +STATIC uint8_t ipcc_membuf_ble_cmd_buf[272]; // mem2 +STATIC uint8_t ipcc_membuf_ble_cs_buf[272]; // mem2 +STATIC tl_list_node_t ipcc_mem_ble_evt_queue; // mem1 +STATIC uint8_t ipcc_membuf_ble_hci_acl_data_buf[272]; // mem2 /******************************************************************************/ // Transport layer linked list @@ -105,21 +175,21 @@ STATIC void tl_list_append(volatile tl_list_node_t *head, volatile tl_list_node_ /******************************************************************************/ // IPCC interface -STATIC uint32_t get_ipccdba(void) { - return *(uint32_t *)(OPTION_BYTE_BASE + 0x68) & 0x3fff; -} - -STATIC volatile void **get_buffer_table(void) { - return (volatile void **)(SRAM2A_BASE + get_ipccdba()); +STATIC volatile ipcc_ref_table_t *get_buffer_table(void) { + // The IPCCDBA option bytes must not be changed without + // making a corresponding change to the linker script. + return (volatile ipcc_ref_table_t *)(SRAM2A_BASE + LL_FLASH_GetIPCCBufferAddr() * 4); } void ipcc_init(uint32_t irq_pri) { + DEBUG_printf("ipcc_init\n"); + // Setup buffer table pointers - volatile void **tab = get_buffer_table(); - tab[0] = &ipcc_mem_dev_info_tab[0]; - tab[1] = &ipcc_mem_ble_tab[0]; - tab[3] = &ipcc_mem_sys_tab[0]; - tab[4] = &ipcc_mem_memmgr_tab[0]; + volatile ipcc_ref_table_t *tab = get_buffer_table(); + tab->p_device_info_table = &ipcc_mem_dev_info_tab; + tab->p_ble_table = &ipcc_mem_ble_tab; + tab->p_sys_table = &ipcc_mem_sys_tab; + tab->p_mem_manager_table = &ipcc_mem_memmgr_tab; // Start IPCC peripheral __HAL_RCC_IPCC_CLK_ENABLE(); @@ -130,32 +200,34 @@ void ipcc_init(uint32_t irq_pri) { NVIC_SetPriority(IPCC_C1_RX_IRQn, irq_pri); HAL_NVIC_EnableIRQ(IPCC_C1_RX_IRQn); - // Device info table will be populated by FUS/WS + // Device info table will be populated by FUS/WS on CPU2 boot. // Populate system table tl_list_init(&ipcc_mem_sys_queue); - ipcc_mem_sys_tab[0] = (uint32_t)&ipcc_mem_sys_cmd_buf[0]; - ipcc_mem_sys_tab[1] = (uint32_t)&ipcc_mem_sys_queue; + ipcc_mem_sys_tab.pcmd_buffer = ipcc_membuf_sys_cmd_buf; + ipcc_mem_sys_tab.sys_queue = &ipcc_mem_sys_queue; // Populate memory manager table tl_list_init(&ipcc_mem_memmgr_free_buf_queue); - ipcc_mem_memmgr_tab[0] = (uint32_t)&ipcc_mem_memmgr_ble_spare_evt_buf[0]; - ipcc_mem_memmgr_tab[1] = (uint32_t)&ipcc_mem_memmgr_sys_spare_evt_buf[0]; - ipcc_mem_memmgr_tab[2] = (uint32_t)&ipcc_mem_memmgr_evt_pool[0]; - ipcc_mem_memmgr_tab[3] = sizeof(ipcc_mem_memmgr_evt_pool); - ipcc_mem_memmgr_tab[4] = (uint32_t)&ipcc_mem_memmgr_free_buf_queue; - ipcc_mem_memmgr_tab[5] = 0; - ipcc_mem_memmgr_tab[6] = 0; + ipcc_mem_memmgr_tab.spare_ble_buffer = ipcc_membuf_memmgr_ble_spare_evt_buf; + ipcc_mem_memmgr_tab.spare_sys_buffer = ipcc_membuf_memmgr_sys_spare_evt_buf; + ipcc_mem_memmgr_tab.blepool = ipcc_membuf_memmgr_evt_pool; + ipcc_mem_memmgr_tab.blepoolsize = sizeof(ipcc_membuf_memmgr_evt_pool); + ipcc_mem_memmgr_tab.pevt_free_buffer_queue = &ipcc_mem_memmgr_free_buf_queue; + ipcc_mem_memmgr_tab.traces_evt_pool = NULL; + ipcc_mem_memmgr_tab.tracespoolsize = 0; // Populate BLE table tl_list_init(&ipcc_mem_ble_evt_queue); - ipcc_mem_ble_tab[0] = (uint32_t)&ipcc_mem_ble_cmd_buf[0]; - ipcc_mem_ble_tab[1] = (uint32_t)&ipcc_mem_ble_cs_buf[0]; - ipcc_mem_ble_tab[2] = (uint32_t)&ipcc_mem_ble_evt_queue; - ipcc_mem_ble_tab[3] = (uint32_t)&ipcc_mem_ble_hci_acl_data_buf[0]; + ipcc_mem_ble_tab.pcmd_buffer = ipcc_membuf_ble_cmd_buf; + ipcc_mem_ble_tab.pcs_buffer = ipcc_membuf_ble_cs_buf; + ipcc_mem_ble_tab.pevt_queue = &ipcc_mem_ble_evt_queue; + ipcc_mem_ble_tab.phci_acl_data_buffer = ipcc_membuf_ble_hci_acl_data_buf; } STATIC int ipcc_wait_ack(unsigned int ch, uint32_t timeout_ms) { + DEBUG_printf("ipcc_wait_ack\n"); + uint32_t t0 = mp_hal_ticks_ms(); while (IPCC->C1TOC2SR & ch) { if (mp_hal_ticks_ms() - t0 > timeout_ms) { @@ -168,6 +240,8 @@ STATIC int ipcc_wait_ack(unsigned int ch, uint32_t timeout_ms) { } STATIC int ipcc_wait_msg(unsigned int ch, uint32_t timeout_ms) { + DEBUG_printf("ipcc_wait_msg\n"); + uint32_t t0 = mp_hal_ticks_ms(); while (!(IPCC->C2TOC1SR & ch)) { if (mp_hal_ticks_ms() - t0 > timeout_ms) { @@ -254,8 +328,8 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h while (cur != head) { tl_parse_hci_msg((uint8_t *)cur->body, parse); volatile tl_list_node_t *next = tl_list_unlink(cur); - if ((void *)&ipcc_mem_memmgr_evt_pool[0] <= (void *)cur - && (void *)cur < (void *)&ipcc_mem_memmgr_evt_pool[MP_ARRAY_SIZE(ipcc_mem_memmgr_evt_pool)]) { + if ((void *)&ipcc_membuf_memmgr_evt_pool[0] <= (void *)cur + && (void *)cur < (void *)&ipcc_membuf_memmgr_evt_pool[MP_ARRAY_SIZE(ipcc_membuf_memmgr_evt_pool)]) { // Place memory back in free pool tl_list_append(&ipcc_mem_memmgr_free_buf_queue, cur); free = true; @@ -291,12 +365,12 @@ STATIC void tl_sys_wait_resp(const uint8_t *buf, unsigned int ch) { } STATIC void tl_sys_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd((uint8_t *)&ipcc_mem_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, len, buf); - tl_sys_wait_resp((uint8_t *)&ipcc_mem_sys_cmd_buf, IPCC_CH_SYS); + tl_hci_cmd((uint8_t *)&ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, len, buf); + tl_sys_wait_resp((uint8_t *)&ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS); } STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd((uint8_t *)&ipcc_mem_ble_cmd_buf[0], IPCC_CH_BLE, 0x01, opcode, len, buf); + tl_hci_cmd((uint8_t *)&ipcc_membuf_ble_cmd_buf[0], IPCC_CH_BLE, 0x01, opcode, len, buf); ipcc_wait_msg(IPCC_CH_BLE, 250); tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); } @@ -305,6 +379,8 @@ STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) // RF core interface void rfcore_init(void) { + DEBUG_printf("rfcore_init\n"); + // Ensure LSE is running rtc_init_finalise(); @@ -361,6 +437,8 @@ static const struct { }; void rfcore_ble_init(void) { + DEBUG_printf("rfcore_ble_init\n"); + // Clear any outstanding messages from ipcc_init tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL); tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); @@ -371,6 +449,8 @@ void rfcore_ble_init(void) { } void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { + DEBUG_printf("rfcore_ble_hci_cmd\n"); + #if HCI_TRACE printf("[% 8d] HCI_CMD(%02x", mp_hal_ticks_ms(), src[0]); for (int i = 1; i < len; ++i) { @@ -382,10 +462,10 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { tl_list_node_t *n; uint32_t ch; if (src[0] == 0x01) { - n = (tl_list_node_t *)&ipcc_mem_ble_cmd_buf[0]; + n = (tl_list_node_t *)&ipcc_membuf_ble_cmd_buf[0]; ch = IPCC_CH_BLE; } else if (src[0] == 0x02) { - n = (tl_list_node_t *)&ipcc_mem_ble_hci_acl_data_buf[0]; + n = (tl_list_node_t *)&ipcc_membuf_ble_hci_acl_data_buf[0]; ch = IPCC_CH_HCI_ACL; } else { printf("** UNEXPECTED HCI HDR: 0x%02x **\n", src[0]); From 9c9cc7a02f6f998d0141989040d68219d6ad0625 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 8 Sep 2020 17:00:41 +1000 Subject: [PATCH 0262/3533] stm32/boards/USBDONGLE_WB55: Add USE_MBOOT support. --- ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk index 416364df9ef6d..dcec788ed443f 100644 --- a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk +++ b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk @@ -1,9 +1,18 @@ MCU_SERIES = wb CMSIS_MCU = STM32WB55xx AF_FILE = boards/stm32wb55_af.csv -LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld STARTUP_FILE = lib/stm32lib/CMSIS/STM32WBxx/Source/Templates/gcc/startup_stm32wb55xx_cm4.o +ifeq ($(USE_MBOOT),1) +# When using Mboot all the text goes together after the bootloader +LD_FILES = boards/stm32wb55xg.ld boards/common_bl.ld +TEXT0_ADDR = 0x08004000 +else +# When not using Mboot the text goes at the start of flash +LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld +TEXT0_ADDR = 0x08000000 +endif + # MicroPython settings MICROPY_PY_BLUETOOTH = 1 MICROPY_BLUETOOTH_NIMBLE = 1 From 0f28020a68b190d973dc5ef2049c76eb28acddee Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 28 Aug 2020 14:59:24 +1000 Subject: [PATCH 0263/3533] stm32/powerctrlboot: Acquire HSEM5 on STM32WB during SystemClock_Config. This is required to allow using WS firmware newer than 1.1.1 concurrently with USB (e.g. USB VCP). It prevents CPU2 from modifying the CLK48 config on boot. Tested on WS=1.8 FUS=1.1. See AN5289 and https://github.com/micropython/micropython/issues/6316 --- ports/stm32/powerctrlboot.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ports/stm32/powerctrlboot.c b/ports/stm32/powerctrlboot.c index 206b19b75067a..880e43e04c2a2 100644 --- a/ports/stm32/powerctrlboot.c +++ b/ports/stm32/powerctrlboot.c @@ -154,12 +154,23 @@ void SystemClock_Config(void) { #elif defined(STM32WB) +#include "stm32wbxx_ll_hsem.h" + +// This semaphore protected access to the CLK48 configuration. +// CPU1 should hold this semaphore while the USB peripheral is in use. +// See AN5289 and https://github.com/micropython/micropython/issues/6316. +#define CLK48_SEMID (5) + void SystemClock_Config(void) { // Enable the 32MHz external oscillator RCC->CR |= RCC_CR_HSEON; while (!(RCC->CR & RCC_CR_HSERDY)) { } + // Prevent CPU2 from disabling CLK48. + while (LL_HSEM_1StepLock(HSEM, CLK48_SEMID)) { + } + // Use HSE and the PLL to get a 64MHz SYSCLK #define PLLM (HSE_VALUE / 8000000) // VCO input is 8MHz #define PLLN (24) // 24*8MHz = 192MHz From 5eda362e0a7ed0eda6a4becdebf76a53cf69f944 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 31 Aug 2020 15:54:20 +1000 Subject: [PATCH 0264/3533] tests/multi_bluetooth: Make ble_gap_connect robust against event timing. --- tests/multi_bluetooth/ble_gap_connect.py | 61 ++++++++++++-------- tests/multi_bluetooth/ble_gap_connect.py.exp | 1 + 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/tests/multi_bluetooth/ble_gap_connect.py b/tests/multi_bluetooth/ble_gap_connect.py index ba9c28230d7da..8b40a29163119 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py +++ b/tests/multi_bluetooth/ble_gap_connect.py @@ -10,35 +10,36 @@ _IRQ_PERIPHERAL_CONNECT = const(7) _IRQ_PERIPHERAL_DISCONNECT = const(8) -waiting_event = None -waiting_data = None +central_connected = False +central_disconnected = False +peripheral_connected = False +peripheral_disconnected = False +conn_handle = None def irq(event, data): - global waiting_event, waiting_data + global central_connected, central_disconnected, peripheral_connected, peripheral_disconnected, conn_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") + central_connected = True + conn_handle = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") + central_disconnected = True elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") + peripheral_connected = True + conn_handle = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") + peripheral_disconnected = True + remote_addr = data[0] - if waiting_event is not None: - if event == waiting_event: - waiting_event = None - waiting_data = data - - -def wait_for_event(event, timeout_ms): - global waiting_event, waiting_data - waiting_event = event - waiting_data = None +def wait_for(fn, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if waiting_data: + if fn(): return True machine.idle() return False @@ -46,25 +47,31 @@ def wait_for_event(event, timeout_ms): # Acting in peripheral role. def instance0(): + global central_connected, central_disconnected + multitest.globals(BDADDR=ble.config("mac")) print("gap_advertise") ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") multitest.next() try: # Wait for central to connect, then wait for it to disconnect. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + if not wait_for(lambda: central_connected, TIMEOUT_MS): return - if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): + if not wait_for(lambda: central_disconnected, TIMEOUT_MS): return + central_connected = False + central_disconnected = False + # Start advertising again. + print("gap_advertise") ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") # Wait for central to connect, then disconnect it. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + if not wait_for(lambda: central_connected, TIMEOUT_MS): return - print("gap_disconnect:", ble.gap_disconnect(waiting_data[0])) - if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + if not wait_for(lambda: central_disconnected, TIMEOUT_MS): return finally: ble.active(0) @@ -72,26 +79,32 @@ def instance0(): # Acting in central role. def instance1(): + global peripheral_connected, peripheral_disconnected + multitest.next() try: # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): + if not wait_for(lambda: peripheral_connected, TIMEOUT_MS): return - print("gap_disconnect:", ble.gap_disconnect(waiting_data[0])) - if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + if not wait_for(lambda: peripheral_disconnected, TIMEOUT_MS): return + peripheral_connected = False + peripheral_disconnected = False + # Wait for peripheral to start advertising again. time.sleep_ms(100) # Connect to peripheral and then let the peripheral disconnect us. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): + if not wait_for(lambda: peripheral_connected, TIMEOUT_MS): + return + if not wait_for(lambda: peripheral_disconnected, TIMEOUT_MS): return - wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) diff --git a/tests/multi_bluetooth/ble_gap_connect.py.exp b/tests/multi_bluetooth/ble_gap_connect.py.exp index 1c96f1d48ac07..d0dc020703214 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py.exp +++ b/tests/multi_bluetooth/ble_gap_connect.py.exp @@ -2,6 +2,7 @@ gap_advertise _IRQ_CENTRAL_CONNECT _IRQ_CENTRAL_DISCONNECT +gap_advertise _IRQ_CENTRAL_CONNECT gap_disconnect: True _IRQ_CENTRAL_DISCONNECT From 01f2d776141d94b8d15d91aa1e0f0cef4244d5f7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 31 Aug 2020 15:55:09 +1000 Subject: [PATCH 0265/3533] stm32/rfcore: Fix length matching in HCI parser. --- ports/stm32/rfcore.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 3565c03898444..99bb405630ba7 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -258,11 +258,13 @@ STATIC int ipcc_wait_msg(unsigned int ch, uint32_t timeout_ms) { STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { const char *kind; - size_t len = 3 + buf[2]; + size_t len = 0; switch (buf[0]) { case 0x02: { // Standard BT HCI ACL packet kind = "HCI_ACL"; + // + len = 5 + buf[3] + (buf[4] << 8); if (parse != NULL) { parse->cb_fun(parse->cb_env, buf, len); } @@ -271,6 +273,8 @@ STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { case 0x04: { // Standard BT HCI event packet kind = "HCI_EVT"; + // + len = 3 + buf[2]; if (parse != NULL) { bool fix = false; if (buf[1] == 0x0e && len == 7 && buf[3] == 0x01 && buf[4] == 0x63 && buf[5] == 0x0c && buf[6] == 0x01) { From 8b4ebd716613ba981c6714510a7adf5c860d7bdf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 1 Sep 2020 14:13:44 +1000 Subject: [PATCH 0266/3533] stm32/rfcore: Refactor some helper funcs, and remove some magic numbers. Also explain what the payload fixup code is doing. --- ports/stm32/rfcore.c | 262 +++++++++++++++++++++++++------------------ 1 file changed, 153 insertions(+), 109 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 99bb405630ba7..5630ea7132db8 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -42,18 +42,33 @@ // Define to 1 to print traces of HCI packets #define HCI_TRACE (0) -#define IPCC_CH_BLE (0x01) // BLE HCI command and response -#define IPCC_CH_SYS (0x02) // system HCI command and response -#define IPCC_CH_MM (0x08) // release buffer -#define IPCC_CH_HCI_ACL (0x20) // HCI ACL outgoing data +#define IPCC_CH_BLE (LL_IPCC_CHANNEL_1) // BLE HCI command and response +#define IPCC_CH_SYS (LL_IPCC_CHANNEL_2) // system HCI command and response +#define IPCC_CH_MM (LL_IPCC_CHANNEL_4) // release buffer +#define IPCC_CH_HCI_ACL (LL_IPCC_CHANNEL_6) // HCI ACL outgoing data -#define OGF_VENDOR (0x3f) -#define OCF_WRITE_CONFIG (0x0c) -#define OCF_SET_TX_POWER (0x0f) -#define OCF_BLE_INIT (0x66) +#define OGF_CTLR_BASEBAND (0x03) +#define OCF_CB_RESET (0x03) +#define OCF_CB_SET_EVENT_MASK2 (0x63) + +#define OGF_VENDOR (0x3f) +#define OCF_WRITE_CONFIG (0x0c) +#define OCF_SET_TX_POWER (0x0f) +#define OCF_BLE_INIT (0x66) #define HCI_OPCODE(ogf, ocf) ((ogf) << 10 | (ocf)) +#define HCI_KIND_BT_CMD (0x01) // ...? +#define HCI_KIND_BT_ACL (0x02) // +#define HCI_KIND_BT_EVENT (0x04) // +#define HCI_KIND_VENDOR_RESPONSE (0x11) +#define HCI_KIND_VENDOR_EVENT (0x12) + +#define HCI_EVENT_COMMAND_COMPLETE (0x0E) // + +#define SYS_ACK_TIMEOUT_MS (250) +#define BLE_ACK_TIMEOUT_MS (250) + typedef struct _tl_list_node_t { volatile struct _tl_list_node_t *next; volatile struct _tl_list_node_t *prev; @@ -194,12 +209,6 @@ void ipcc_init(uint32_t irq_pri) { // Start IPCC peripheral __HAL_RCC_IPCC_CLK_ENABLE(); - // Enable wanted IRQs - IPCC->C1CR = 0; // IPCC_C1CR_RXOIE; - IPCC->C1MR = 0xffffffff; - NVIC_SetPriority(IPCC_C1_RX_IRQn, irq_pri); - HAL_NVIC_EnableIRQ(IPCC_C1_RX_IRQn); - // Device info table will be populated by FUS/WS on CPU2 boot. // Populate system table @@ -225,127 +234,136 @@ void ipcc_init(uint32_t irq_pri) { ipcc_mem_ble_tab.phci_acl_data_buffer = ipcc_membuf_ble_hci_acl_data_buf; } -STATIC int ipcc_wait_ack(unsigned int ch, uint32_t timeout_ms) { - DEBUG_printf("ipcc_wait_ack\n"); - - uint32_t t0 = mp_hal_ticks_ms(); - while (IPCC->C1TOC2SR & ch) { - if (mp_hal_ticks_ms() - t0 > timeout_ms) { - printf("ipcc_wait_ack: timeout\n"); - return -MP_ETIMEDOUT; - } - } - // C2 cleared IPCC flag - return 0; -} - -STATIC int ipcc_wait_msg(unsigned int ch, uint32_t timeout_ms) { - DEBUG_printf("ipcc_wait_msg\n"); - - uint32_t t0 = mp_hal_ticks_ms(); - while (!(IPCC->C2TOC1SR & ch)) { - if (mp_hal_ticks_ms() - t0 > timeout_ms) { - printf("ipcc_wait_msg: timeout\n"); - return -MP_ETIMEDOUT; - } - } - // C2 set IPCC flag - return 0; -} - /******************************************************************************/ // Transport layer HCI interface STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { - const char *kind; + const char *info; size_t len = 0; + bool applied_set_event_event_mask2_fix = false; switch (buf[0]) { - case 0x02: { - // Standard BT HCI ACL packet - kind = "HCI_ACL"; - // + case HCI_KIND_BT_ACL: { + info = "HCI_ACL"; + len = 5 + buf[3] + (buf[4] << 8); if (parse != NULL) { parse->cb_fun(parse->cb_env, buf, len); } break; } - case 0x04: { - // Standard BT HCI event packet - kind = "HCI_EVT"; - // + case HCI_KIND_BT_EVENT: { + info = "HCI_EVT"; + len = 3 + buf[2]; if (parse != NULL) { - bool fix = false; - if (buf[1] == 0x0e && len == 7 && buf[3] == 0x01 && buf[4] == 0x63 && buf[5] == 0x0c && buf[6] == 0x01) { - len -= 1; - fix = true; + + if (buf[1] == HCI_EVENT_COMMAND_COMPLETE && len == 7) { + uint16_t opcode = (buf[5] << 8) | buf[4]; + uint8_t status = buf[6]; + + if (opcode == HCI_OPCODE(OGF_CTLR_BASEBAND, OCF_CB_SET_EVENT_MASK2) && status != 0) { + // The WB doesn't support this command (despite being in CS 4.1), so pretend like + // it succeeded by replacing the final byte (status) with a zero. + applied_set_event_event_mask2_fix = true; + len -= 1; + } + + if (opcode == HCI_OPCODE(OGF_CTLR_BASEBAND, OCF_CB_RESET) && status == 0) { + // Controller acknowledged reset command. + // This will trigger setting the MAC address. + parse->was_hci_reset_evt = true; + } } + parse->cb_fun(parse->cb_env, buf, len); - if (fix) { - len += 1; - uint8_t data = 0x00; // success + + if (applied_set_event_event_mask2_fix) { + // Inject the zero status. + uint8_t data = 0; parse->cb_fun(parse->cb_env, &data, 1); + // Restore the length for the HCI tracing below. + len += 1; } - // Check for successful HCI_Reset event - parse->was_hci_reset_evt = buf[1] == 0x0e && buf[2] == 0x04 && buf[3] == 0x01 - && buf[4] == 0x03 && buf[5] == 0x0c && buf[6] == 0x00; } break; } - case 0x11: { - // Response packet + case HCI_KIND_VENDOR_RESPONSE: { // assert(buf[1] == 0x0e); - kind = "VEND_RESP"; + info = "VEND_RESP"; + len = 3 + buf[2]; // ??? // uint16_t cmd = buf[4] | buf[5] << 8; // uint8_t status = buf[6]; break; } - case 0x12: { - // Event packet + case HCI_KIND_VENDOR_EVENT: { // assert(buf[1] == 0xff); - kind = "VEND_EVT"; + info = "VEND_EVT"; + len = 3 + buf[2]; // ??? // uint16_t evt = buf[3] | buf[4] << 8; break; } default: - kind = "HCI_UNKNOWN"; + info = "HCI_UNKNOWN"; break; } #if HCI_TRACE - printf("[% 8d] %s(%02x", mp_hal_ticks_ms(), kind, buf[0]); + printf("[% 8d] <%s(%02x", mp_hal_ticks_ms(), info, buf[0]); for (int i = 1; i < len; ++i) { printf(":%02x", buf[i]); } - printf(")\n"); + printf(")"); + if (parse && parse->was_hci_reset_evt) { + printf(" (reset)"); + } + if (applied_set_event_event_mask2_fix) { + printf(" (mask2 fix)"); + } + printf("\n"); + #else - (void)kind; + (void)info; #endif } -STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { - if (IPCC->C2TOC1SR & ch) { - // Message available on CH2 - volatile tl_list_node_t *cur = head->next; - bool free = false; - while (cur != head) { - tl_parse_hci_msg((uint8_t *)cur->body, parse); - volatile tl_list_node_t *next = tl_list_unlink(cur); - if ((void *)&ipcc_membuf_memmgr_evt_pool[0] <= (void *)cur - && (void *)cur < (void *)&ipcc_membuf_memmgr_evt_pool[MP_ARRAY_SIZE(ipcc_membuf_memmgr_evt_pool)]) { - // Place memory back in free pool - tl_list_append(&ipcc_mem_memmgr_free_buf_queue, cur); - free = true; - } - cur = next; - } - if (free) { - // Notify change in free pool - IPCC->C1SCR = IPCC_CH_MM << 16; +STATIC void tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { + volatile tl_list_node_t *cur = head->next; + bool added_to_free_queue = false; + while (cur != head) { + tl_parse_hci_msg((uint8_t *)cur->body, parse); + + volatile tl_list_node_t *next = tl_list_unlink(cur); + + // If this node is allocated from the memmgr event pool, then place it into the free buffer. + if ((uint8_t *)cur >= ipcc_membuf_memmgr_evt_pool && (uint8_t *)cur < ipcc_membuf_memmgr_evt_pool + sizeof(ipcc_membuf_memmgr_evt_pool)) { + // Place memory back in free pool. + tl_list_append(&ipcc_mem_memmgr_free_buf_queue, cur); + added_to_free_queue = true; } - // Clear receive channel - IPCC->C1SCR = ch; + + cur = next; + } + + if (added_to_free_queue) { + // Notify change in free pool. + LL_C1_IPCC_SetFlag_CHx(IPCC, IPCC_CH_MM); + } +} + +STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { + if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, ch)) { + tl_process_msg(head, ch, parse); + + // Clear receive channel. + LL_C1_IPCC_ClearFlag_CHx(IPCC, ch); + } +} + +STATIC void tl_check_msg_ble(volatile tl_list_node_t *head, parse_hci_info_t *parse) { + if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { + tl_process_msg(head, IPCC_CH_BLE, parse); + + LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); } } @@ -358,25 +376,51 @@ STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opco cmd[10] = opcode >> 8; cmd[11] = len; memcpy(&cmd[12], buf, len); - // IPCC indicate - IPCC->C1SCR = ch << 16; + + // Indicate that this channel is ready. + LL_C1_IPCC_SetFlag_CHx(IPCC, ch); } -STATIC void tl_sys_wait_resp(const uint8_t *buf, unsigned int ch) { - if (ipcc_wait_ack(ch, 250) == 0) { - tl_parse_hci_msg(buf, NULL); +STATIC int tl_sys_wait_ack(const uint8_t *buf) { + uint32_t t0 = mp_hal_ticks_ms(); + + // C2 will clear this bit to acknowledge the request. + while (LL_C1_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_SYS)) { + if (mp_hal_ticks_ms() - t0 > SYS_ACK_TIMEOUT_MS) { + printf("tl_sys_wait_ack: timeout\n"); + return -MP_ETIMEDOUT; + } } + + // C1-to-C2 bit cleared, so process (but ignore) the response. + tl_parse_hci_msg(buf, NULL); + return 0; } STATIC void tl_sys_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd((uint8_t *)&ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, len, buf); - tl_sys_wait_resp((uint8_t *)&ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS); + tl_hci_cmd(ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, len, buf); + tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf); +} + +STATIC int tl_ble_wait_resp(void) { + uint32_t t0 = mp_hal_ticks_ms(); + while (!LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { + if (mp_hal_ticks_ms() - t0 > BLE_ACK_TIMEOUT_MS) { + printf("tl_ble_wait_resp: timeout\n"); + return -MP_ETIMEDOUT; + } + } + + // C2 set IPCC flag. + tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); + return 0; } +// Synchronously send a BLE command. STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd((uint8_t *)&ipcc_membuf_ble_cmd_buf[0], IPCC_CH_BLE, 0x01, opcode, len, buf); - ipcc_wait_msg(IPCC_CH_BLE, 250); - tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); + tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, len, buf); + tl_ble_wait_resp(); + } /******************************************************************************/ @@ -445,7 +489,7 @@ void rfcore_ble_init(void) { // Clear any outstanding messages from ipcc_init tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL); - tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); + tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); // Configure and reset the BLE controller tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), sizeof(ble_init_params), (const uint8_t *)&ble_init_params); @@ -456,7 +500,7 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { DEBUG_printf("rfcore_ble_hci_cmd\n"); #if HCI_TRACE - printf("[% 8d] HCI_CMD(%02x", mp_hal_ticks_ms(), src[0]); + printf("[% 8d] >HCI_CMD(%02x", mp_hal_ticks_ms(), src[0]); for (int i = 1; i < len; ++i) { printf(":%02x", src[i]); } @@ -465,10 +509,10 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { tl_list_node_t *n; uint32_t ch; - if (src[0] == 0x01) { + if (src[0] == HCI_KIND_BT_CMD) { n = (tl_list_node_t *)&ipcc_membuf_ble_cmd_buf[0]; ch = IPCC_CH_BLE; - } else if (src[0] == 0x02) { + } else if (src[0] == HCI_KIND_BT_ACL) { n = (tl_list_node_t *)&ipcc_membuf_ble_hci_acl_data_buf[0]; ch = IPCC_CH_HCI_ACL; } else { @@ -480,13 +524,13 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { n->prev = n; memcpy(n->body, src, len); - // IPCC indicate - IPCC->C1SCR = ch << 16; + // IPCC indicate. + LL_C1_IPCC_SetFlag_CHx(IPCC, ch); } void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env) { parse_hci_info_t parse = { cb, env, false }; - tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, &parse); + tl_check_msg_ble(&ipcc_mem_ble_evt_queue, &parse); // Intercept HCI_Reset events and reconfigure the controller following the reset if (parse.was_hci_reset_evt) { From e2390d5a2f4f18912878596965b0c794ac153cb4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 1 Sep 2020 14:13:44 +1000 Subject: [PATCH 0267/3533] stm32/rfcore: Enable RX IRQ on BLE IPCC channel for better performance. Before this change there was up to a 128ms delay on incoming payloads from CPU2 as it was polled by SysTick. Now the RX IRQ immediately schedules the PendSV. --- ports/stm32/rfcore.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 5630ea7132db8..ca2ad747c4bb8 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -164,6 +164,9 @@ STATIC uint8_t ipcc_membuf_ble_cs_buf[272]; // mem2 STATIC tl_list_node_t ipcc_mem_ble_evt_queue; // mem1 STATIC uint8_t ipcc_membuf_ble_hci_acl_data_buf[272]; // mem2 +// Set by the RX IRQ handler on incoming HCI payload. +STATIC volatile bool had_ble_irq = false; + /******************************************************************************/ // Transport layer linked list @@ -209,6 +212,13 @@ void ipcc_init(uint32_t irq_pri) { // Start IPCC peripheral __HAL_RCC_IPCC_CLK_ENABLE(); + // Enable receive IRQ on the BLE channel. + LL_C1_IPCC_EnableIT_RXO(IPCC); + LL_C1_IPCC_DisableReceiveChannel(IPCC, LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4 | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6); + LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE); + NVIC_SetPriority(IPCC_C1_RX_IRQn, irq_pri); + HAL_NVIC_EnableIRQ(IPCC_C1_RX_IRQn); + // Device info table will be populated by FUS/WS on CPU2 boot. // Populate system table @@ -360,10 +370,10 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h } STATIC void tl_check_msg_ble(volatile tl_list_node_t *head, parse_hci_info_t *parse) { - if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { + if (had_ble_irq) { tl_process_msg(head, IPCC_CH_BLE, parse); - LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); + had_ble_irq = false; } } @@ -404,7 +414,7 @@ STATIC void tl_sys_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) STATIC int tl_ble_wait_resp(void) { uint32_t t0 = mp_hal_ticks_ms(); - while (!LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { + while (!had_ble_irq) { if (mp_hal_ticks_ms() - t0 > BLE_ACK_TIMEOUT_MS) { printf("tl_ble_wait_resp: timeout\n"); return -MP_ETIMEDOUT; @@ -553,4 +563,26 @@ void rfcore_ble_set_txpower(uint8_t level) { tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), 2, buf); } +// IPCC IRQ Handlers +void IPCC_C1_TX_IRQHandler(void) { + IRQ_ENTER(IPCC_C1_TX_IRQn); + IRQ_EXIT(IPCC_C1_TX_IRQn); +} + +void IPCC_C1_RX_IRQHandler(void) { + IRQ_ENTER(IPCC_C1_RX_IRQn); + + if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { + had_ble_irq = true; + + LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); + + // Schedule PENDSV to process incoming HCI payload. + extern void mp_bluetooth_hci_poll_wrapper(uint32_t ticks_ms); + mp_bluetooth_hci_poll_wrapper(0); + } + + IRQ_EXIT(IPCC_C1_RX_IRQn); +} + #endif // defined(STM32WB) From 632e3b7acc51b3b28f310ef5e23b0983c8e8b700 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 28 Aug 2020 15:51:15 +1000 Subject: [PATCH 0268/3533] stm32/boards/NUCLEO_WB55: Add Python helper code for rfcore. This allows prototyping rfcore.c improvements from Python. This was mostly written by @dpgeorge with small modifications to work after rfcore_init() by @jimmo. --- ports/stm32/boards/NUCLEO_WB55/rfcore.py | 347 +++++++++++++++++++++++ 1 file changed, 347 insertions(+) create mode 100644 ports/stm32/boards/NUCLEO_WB55/rfcore.py diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore.py b/ports/stm32/boards/NUCLEO_WB55/rfcore.py new file mode 100644 index 0000000000000..e612499a7829f --- /dev/null +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore.py @@ -0,0 +1,347 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2020 Damien P. George +# Copyright (c) 2020 Jim Mussared +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# This script provides some helpers to allow Python code to access the IPCC +# mechanism in the WB55, and works with the memory layout configured in +# ports/stm32/rfcore.c -- i.e. it expects that rfcore_init() has been run. + +# At this stage this is useful for debugging, but can be extended to support +# FUS/WS firmware updates. +# e.g. +# ../../tools/pyboard.py --device /dev/ttyACM0 boards/NUCLEO_WB55/rfcore.py +# to print out SRAM2A, register state and FUS/WS info. + +from machine import mem8, mem16, mem32 +import time, struct, uctypes +import stm + + +class Flash: + FLASH_KEY1 = 0x45670123 + FLASH_KEY2 = 0xCDEF89AB + + def wait_not_busy(self): + while mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16: + machine.idle() + + def unlock(self): + mem32[stm.FLASH + stm.FLASH_KEYR] = Flash.FLASH_KEY1 + mem32[stm.FLASH + stm.FLASH_KEYR] = Flash.FLASH_KEY2 + + def lock(self): + mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK + + def erase_page(self, page): + print("erase", page) + assert 0 <= page <= 255 # 1MiB range (4k page) + self.wait_not_busy() + cr = page << 3 | 1 << 1 # PNB # PER + mem32[stm.FLASH + stm.FLASH_CR] = cr + mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT + self.wait_not_busy() + mem32[stm.FLASH + stm.FLASH_CR] = 0 + + def write(self, addr, buf): + assert len(buf) % 4 == 0 + self.wait_not_busy() + cr = 1 << 0 # PG + mem32[stm.FLASH + stm.FLASH_CR] = cr + buf_addr = uctypes.addressof(buf) + off = 0 + while off < len(buf): + mem32[addr + off] = mem32[buf_addr + off] + off += 4 + if off % 8 == 0: + self.wait_not_busy() + if off % 8: + mem32[addr + off] = 0 + self.wait_not_busy() + mem32[stm.FLASH + stm.FLASH_CR] = 0 + + +def copy_file_to_flash(filename, addr): + flash = Flash() + flash.unlock() + try: + with open(filename, "rb") as f: + buf = bytearray(4096) + while 1: + sz = f.readinto(buf) + if sz == 0: + break + print("write", hex(addr), sz) + flash.erase_page((addr - 0x08000000) // 4096) + print("done e") + flash.write(addr, buf) + print("done") + addr += 4096 + finally: + flash.lock() + + +SRAM2A_BASE = const(0x2003_0000) + +# for vendor OGF +OGF_VENDOR = const(0x3F) +OCF_FUS_GET_STATE = const(0x52) +OCF_FUS_FW_UPGRADE = const(0x54) +OCF_FUS_FW_DELETE = const(0x55) +OCF_FUS_START_WS = const(0x5A) +OCF_BLE_INIT = const(0x66) + + +@micropython.asm_thumb +def asm_sev_wfe(): + data(2, 0xBF40) # sev + data(2, 0xBF20) # wfe + + +TABLE_DEVICE_INFO = const(0) +TABLE_BLE = const(1) +TABLE_SYS = const(3) +TABLE_MEM_MANAGER = const(4) + +CHANNEL_BLE = const(1) +CHANNEL_SYS = const(2) +CHANNEL_TRACES = const(4) +CHANNEL_ACL = const(6) + +INDICATOR_HCI_COMMAND = const(0x01) +INDICATOR_HCI_EVENT = const(0x04) +INDICATOR_FUS_COMMAND = const(0x10) +INDICATOR_FUS_RESPONSE = const(0x11) +INDICATOR_FUS_EVENT = const(0x12) + +MAGIC_FUS_ACTIVE = const(0xA94656B9) + + +def get_ipccdba(): + return mem32[stm.FLASH + stm.FLASH_IPCCBR] & 0x3FFF + + +def get_ipcc_table(table): + return mem32[SRAM2A_BASE + get_ipccdba() + table * 4] + + +def get_ipcc_table_word(table, offset): + return mem32[get_ipcc_table(table) + offset * 4] & 0xFFFFFFFF + + +def get_ipcc_table_byte(table, offset): + return mem8[get_ipcc_table(table) + offset] & 0xFF + + +def sram2a_dump(num_words=64, width=8): + print("SRAM2A @%08x" % SRAM2A_BASE) + for i in range((num_words + width - 1) // width): + print(" %04x " % (i * 4 * width), end="") + for j in range(width): + print(" %08x" % (mem32[SRAM2A_BASE + (i * width + j) * 4] & 0xFFFFFFFF), end="") + print() + + +SYS_CMD_BUF = 0 # next*,prev*,type8,...; 272 bytes +SYS_SYS_QUEUE = 0 # next*,prev* + +MM_BLE_SPARE_EVT_BUF = 0 # next*,prev*; 272 bytes +MM_SYS_SPARE_EVT_BUF = 0 # next*,prev*; 272 bytes +MM_BLE_POOL = 0 # ? +MM_BLE_POOL_SIZE = 0 # ? +MM_FREE_BUF_QUEUE = 0 # next*,prev* +MM_EV_POOL = 0 # ? +MM_EV_POOL_SIZE = 0 # ? + +BLE_CMD_BUF = 0 +BLE_CS_BUF = 0 +BLE_EVT_QUEUE = 0 +BLE_HCI_ACL_DATA_BUF = 0 + + +def ipcc_init(): + global SYS_CMD_BUF, SYS_SYS_QUEUE + SYS_CMD_BUF = get_ipcc_table_word(TABLE_SYS, 0) + SYS_SYS_QUEUE = get_ipcc_table_word(TABLE_SYS, 1) + + global MM_BLE_SPARE_EVT_BUF, MM_SYS_SPARE_EVT_BUF, MM_BLE_POOL, MM_BLE_POOL_SIZE, MM_FREE_BUF_QUEUE, MM_EV_POOL, MM_EV_POOL_SIZE + MM_BLE_SPARE_EVT_BUF = get_ipcc_table_word(TABLE_MEM_MANAGER, 0) + MM_SYS_SPARE_EVT_BUF = get_ipcc_table_word(TABLE_MEM_MANAGER, 1) + MM_BLE_POOL = get_ipcc_table_word(TABLE_MEM_MANAGER, 2) + MM_BLE_POOL_SIZE = get_ipcc_table_word(TABLE_MEM_MANAGER, 3) + MM_FREE_BUF_QUEUE = get_ipcc_table_word(TABLE_MEM_MANAGER, 4) + MM_EV_POOL = get_ipcc_table_word(TABLE_MEM_MANAGER, 5) + MM_EV_POOL_SIZE = get_ipcc_table_word(TABLE_MEM_MANAGER, 6) + + global BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE, BLE_HCI_ACL_DATA_BUF + BLE_CMD_BUF = get_ipcc_table_word(TABLE_BLE, 0) + BLE_CS_BUF = get_ipcc_table_word(TABLE_BLE, 1) + BLE_EVT_QUEUE = get_ipcc_table_word(TABLE_BLE, 2) + BLE_HCI_ACL_DATA_BUF = get_ipcc_table_word(TABLE_BLE, 3) + + print("IPCC initialised") + print("SYS: 0x%08x 0x%08x" % (SYS_CMD_BUF, SYS_SYS_QUEUE)) + print("BLE: 0x%08x 0x%08x 0x%08x" % (BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE)) + + +def tl_list_init(addr): + mem32[addr] = addr # next + mem32[addr + 4] = addr # prev + + +def tl_list_append(head, n): + sram2a_dump(1024) + print("Appending 0x%08x to 0x%08x" % (head, n)) + # item->next = head + mem32[n] = head + # item->prev = head->prev + mem32[n + 4] = mem32[head + 4] + # head->prev->next = item + mem32[mem32[head + 4]] = n + # head->prev = item + mem32[head + 4] = n + + +def tl_list_unlink(n): + # next = item->next + next = mem32[n] + # prev = item->prev + prev = mem32[n + 4] + # prev->next = item->next + mem32[prev] = next + # item->next->prev = prev + mem32[next + 4] = prev + + return next + + +def tl_list_dump(head): + print( + "list(%08x, %08x, %08x):" % (head, mem32[head] & 0xFFFFFFFF, mem32[head + 4] & 0xFFFFFFFF), + end="", + ) + cur = mem32[head] + while cur != head: + print(" %08x" % (cur & 0xFFFFFFFF), end="") + cur = mem32[cur] + print() + + +def fus_active(): + return get_ipcc_table_word(TABLE_DEVICE_INFO, 0) == MAGIC_FUS_ACTIVE + + +def info(): + sfr = mem32[stm.FLASH + stm.FLASH_SFR] + srrvr = mem32[stm.FLASH + stm.FLASH_SRRVR] + + print("IPCCDBA : 0x%08x" % (get_ipccdba() & 0x3FFF)) + print("DDS : %r" % bool(sfr & (1 << 12))) + print("FSD : %r" % bool(sfr & (1 << 8))) + print("SFSA : 0x%08x" % (sfr & 0xFF)) + print("C2OPT : %r" % bool(srrvr & (1 << 31))) + print("NBRSD : %r" % bool(srrvr & (1 << 30))) + print("SNBRSA : 0x%08x" % ((srrvr >> 25) & 0x1F)) + print("BRSD : %r" % bool(srrvr & (1 << 23))) + print("SBRSA : 0x%08x" % ((srrvr >> 18) & 0x1F)) + print("SBRV : 0x%08x" % (srrvr & 0x3FFFF)) + + +def dev_info(): + def dump_version(offset): + x = get_ipcc_table_word(TABLE_DEVICE_INFO, offset) + print( + "0x%08x (%u.%u.%u.%u.%u)" + % (x, x >> 24, x >> 16 & 0xFF, x >> 8 & 0xFF, x >> 4 & 0xF, x & 0xF) + ) + + def dump_memory_size(offset): + x = get_ipcc_table_word(TABLE_DEVICE_INFO, offset) + print( + "0x%08x (SRAM2b=%uk SRAM2a=%uk flash=%uk)" + % (x, x >> 24, x >> 16 & 0xFF, (x & 0xFF) * 4) + ) + + print("Device information table @%08x:" % get_ipcc_table(TABLE_DEVICE_INFO)) + if fus_active(): + # layout when running FUS + print("FUS is active") + print("state : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 0)) + print("last FUS active state : 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 5)) + print("last wireless stack state: 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 6)) + print("cur wireless stack type : 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 7)) + print("safe boot version : ", end="") + dump_version(2) + print("FUS version : ", end="") + dump_version(3) + print("FUS memory size : ", end="") + dump_memory_size(4) + print("wireless stack version : ", end="") + dump_version(5) + print("wireless stack mem size : ", end="") + dump_memory_size(6) + print("wireless FW-BLE info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) + print("wireless FW-thread info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 8)) + print( + "UID64 : 0x%08x 0x%08x" + % ( + get_ipcc_table_word(TABLE_DEVICE_INFO, 9), + get_ipcc_table_word(TABLE_DEVICE_INFO, 10), + ) + ) + print("device ID : 0x%04x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 11)) + else: + # layout when running WS + print("WS is active") + print("safe boot version : ", end="") + dump_version(0) + print("FUS version : ", end="") + dump_version(1) + print("FUS memory size : ", end="") + dump_memory_size(2) + print("FUS info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 3)) + print("wireless stack version : ", end="") + dump_version(4) + print("wireless stack mem size : ", end="") + dump_memory_size(5) + print("wireless stack info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) + print("wireless reserved : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) + + +def ipcc_state(): + print("IPCC:") + print(" C1CR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1CR] & 0xFFFFFFFF), end="") + print(" C2CR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2CR] & 0xFFFFFFFF)) + print(" C1MR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1MR] & 0xFFFFFFFF), end="") + print(" C2MR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2MR] & 0xFFFFFFFF)) + # these always read 0 + # print(' C1SCR: 0x%08x' % (mem32[stm.IPCC + stm.IPCC_C1SCR] & 0xffffffff), end='') + # print(' C2SCR: 0x%08x' % (mem32[stm.IPCC + stm.IPCC_C2SCR] & 0xffffffff)) + print(" C1TOC2SR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1TOC2SR] & 0xFFFFFFFF), end="") + print(" C2TOC1SR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2TOC1SR] & 0xFFFFFFFF)) + + +sram2a_dump(264) +ipcc_init() +info() +dev_info() From 5f50568b1f7ab4f917b8d782d643ac55612910dd Mon Sep 17 00:00:00 2001 From: Albort Xue Date: Tue, 7 Apr 2020 15:54:01 +0800 Subject: [PATCH 0269/3533] mimxrt/boards: Add MIMXRT1064_EVK board. --- ports/mimxrt/Makefile | 2 +- ports/mimxrt/boards/MIMXRT1064.ld | 8 + .../evkmimxrt1064_flexspi_nor_config.h | 268 ++++++++++++++++++ .../boards/MIMXRT1064_EVK/flash_config.c | 49 ++++ .../boards/MIMXRT1064_EVK/mpconfigboard.h | 8 + .../boards/MIMXRT1064_EVK/mpconfigboard.mk | 9 + ports/mimxrt/boards/MIMXRT1064_EVK/pins.c | 33 +++ ports/mimxrt/boards/MIMXRT1064_EVK/pins.h | 30 ++ 8 files changed, 406 insertions(+), 1 deletion(-) create mode 100644 ports/mimxrt/boards/MIMXRT1064.ld create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/evkmimxrt1064_flexspi_nor_config.h create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/flash_config.c create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.mk create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/pins.h diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index c4ea70128e0ad..85e77b978e9f0 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -40,7 +40,7 @@ INC += -I$(TOP)/lib/tinyusb/hw INC += -I$(TOP)/lib/tinyusb/hw/bsp/teensy_40 CFLAGS_MCU = -mtune=cortex-m7 -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -CFLAGS = $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=c99 -nostdlib -mthumb $(CFLAGS_MCU) +CFLAGS += $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=c99 -nostdlib -mthumb $(CFLAGS_MCU) CFLAGS += -DCPU_$(MCU_SERIES) -DCPU_$(MCU_VARIANT) CFLAGS += -DXIP_EXTERNAL_FLASH=1 \ -DXIP_BOOT_HEADER_ENABLE=1 \ diff --git a/ports/mimxrt/boards/MIMXRT1064.ld b/ports/mimxrt/boards/MIMXRT1064.ld new file mode 100644 index 0000000000000..bf37c21802581 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064.ld @@ -0,0 +1,8 @@ +/* 24kiB stack. */ +__stack_size__ = 0x6000; +_estack = __StackTop; +_sstack = __StackLimit; + +/* Use second OCRAM bank for GC heap. */ +_gc_heap_start = ORIGIN(m_data2); +_gc_heap_end = ORIGIN(m_data2) + LENGTH(m_data2); \ No newline at end of file diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/evkmimxrt1064_flexspi_nor_config.h b/ports/mimxrt/boards/MIMXRT1064_EVK/evkmimxrt1064_flexspi_nor_config.h new file mode 100644 index 0000000000000..efdfe583fb82c --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/evkmimxrt1064_flexspi_nor_config.h @@ -0,0 +1,268 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __EVKMIMXRT1064_FLEXSPI_NOR_CONFIG__ +#define __EVKMIMXRT1064_FLEXSPI_NOR_CONFIG__ + +#include +#include +#include "fsl_common.h" + +/*! @name Driver version */ +/*@{*/ +/*! @brief XIP_BOARD driver version 2.0.0. */ +#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) +/*@}*/ + +/* FLEXSPI memory config block related defintions */ +#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian +#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0 +#define FLEXSPI_CFG_BLK_SIZE (512) + +/* FLEXSPI Feature related definitions */ +#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1 + +/* Lookup table related defintions */ +#define CMD_INDEX_READ 0 +#define CMD_INDEX_READSTATUS 1 +#define CMD_INDEX_WRITEENABLE 2 +#define CMD_INDEX_WRITE 4 + +#define CMD_LUT_SEQ_IDX_READ 0 +#define CMD_LUT_SEQ_IDX_READSTATUS 1 +#define CMD_LUT_SEQ_IDX_WRITEENABLE 3 +#define CMD_LUT_SEQ_IDX_WRITE 9 + +#define CMD_SDR 0x01 +#define CMD_DDR 0x21 +#define RADDR_SDR 0x02 +#define RADDR_DDR 0x22 +#define CADDR_SDR 0x03 +#define CADDR_DDR 0x23 +#define MODE1_SDR 0x04 +#define MODE1_DDR 0x24 +#define MODE2_SDR 0x05 +#define MODE2_DDR 0x25 +#define MODE4_SDR 0x06 +#define MODE4_DDR 0x26 +#define MODE8_SDR 0x07 +#define MODE8_DDR 0x27 +#define WRITE_SDR 0x08 +#define WRITE_DDR 0x28 +#define READ_SDR 0x09 +#define READ_DDR 0x29 +#define LEARN_SDR 0x0A +#define LEARN_DDR 0x2A +#define DATSZ_SDR 0x0B +#define DATSZ_DDR 0x2B +#define DUMMY_SDR 0x0C +#define DUMMY_DDR 0x2C +#define DUMMY_RWDS_SDR 0x0D +#define DUMMY_RWDS_DDR 0x2D +#define JMP_ON_CS 0x1F +#define STOP 0 + +#define FLEXSPI_1PAD 0 +#define FLEXSPI_2PAD 1 +#define FLEXSPI_4PAD 2 +#define FLEXSPI_8PAD 3 + +#define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \ + (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \ + FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1)) + +//!@brief Definitions for FlexSPI Serial Clock Frequency +typedef enum _FlexSpiSerialClockFreq +{ + kFlexSpiSerialClk_30MHz = 1, + kFlexSpiSerialClk_50MHz = 2, + kFlexSpiSerialClk_60MHz = 3, + kFlexSpiSerialClk_75MHz = 4, + kFlexSpiSerialClk_80MHz = 5, + kFlexSpiSerialClk_100MHz = 6, + kFlexSpiSerialClk_120MHz = 7, + kFlexSpiSerialClk_133MHz = 8, + kFlexSpiSerialClk_166MHz = 9, +} flexspi_serial_clk_freq_t; + +//!@brief FlexSPI clock configuration type +enum +{ + kFlexSpiClk_SDR, //!< Clock configure for SDR mode + kFlexSpiClk_DDR, //!< Clock configurat for DDR mode +}; + +//!@brief FlexSPI Read Sample Clock Source definition +typedef enum _FlashReadSampleClkSource +{ + kFlexSPIReadSampleClk_LoopbackInternally = 0, + kFlexSPIReadSampleClk_LoopbackFromDqsPad = 1, + kFlexSPIReadSampleClk_LoopbackFromSckPad = 2, + kFlexSPIReadSampleClk_ExternalInputFromDqsPad = 3, +} flexspi_read_sample_clk_t; + +//!@brief Misc feature bit definitions +enum +{ + kFlexSpiMiscOffset_DiffClkEnable = 0, //!< Bit for Differential clock enable + kFlexSpiMiscOffset_Ck2Enable = 1, //!< Bit for CK2 enable + kFlexSpiMiscOffset_ParallelEnable = 2, //!< Bit for Parallel mode enable + kFlexSpiMiscOffset_WordAddressableEnable = 3, //!< Bit for Word Addressable enable + kFlexSpiMiscOffset_SafeConfigFreqEnable = 4, //!< Bit for Safe Configuration Frequency enable + kFlexSpiMiscOffset_PadSettingOverrideEnable = 5, //!< Bit for Pad setting override enable + kFlexSpiMiscOffset_DdrModeEnable = 6, //!< Bit for DDR clock confiuration indication. +}; + +//!@brief Flash Type Definition +enum +{ + kFlexSpiDeviceType_SerialNOR = 1, //!< Flash devices are Serial NOR + kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND + kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH + kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND + kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs +}; + +//!@brief Flash Pad Definitions +enum +{ + kSerialFlash_1Pad = 1, + kSerialFlash_2Pads = 2, + kSerialFlash_4Pads = 4, + kSerialFlash_8Pads = 8, +}; + +//!@brief FlexSPI LUT Sequence structure +typedef struct _lut_sequence +{ + uint8_t seqNum; //!< Sequence Number, valid number: 1-16 + uint8_t seqId; //!< Sequence Index, valid number: 0-15 + uint16_t reserved; +} flexspi_lut_seq_t; + +//!@brief Flash Configuration Command Type +enum +{ + kDeviceConfigCmdType_Generic, //!< Generic command, for example: configure dummy cycles, drive strength, etc + kDeviceConfigCmdType_QuadEnable, //!< Quad Enable command + kDeviceConfigCmdType_Spi2Xpi, //!< Switch from SPI to DPI/QPI/OPI mode + kDeviceConfigCmdType_Xpi2Spi, //!< Switch from DPI/QPI/OPI to SPI mode + kDeviceConfigCmdType_Spi2NoCmd, //!< Switch to 0-4-4/0-8-8 mode + kDeviceConfigCmdType_Reset, //!< Reset device command +}; + +//!@brief FlexSPI Memory Configuration Block +typedef struct _FlexSPIConfig +{ + uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL + uint32_t version; //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix + uint32_t reserved0; //!< [0x008-0x00b] Reserved for future use + uint8_t readSampleClkSrc; //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3 + uint8_t csHoldTime; //!< [0x00d-0x00d] CS hold time, default value: 3 + uint8_t csSetupTime; //!< [0x00e-0x00e] CS setup time, default value: 3 + uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For + //! Serial NAND, need to refer to datasheet + uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable + uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch, + //! Generic configuration, etc. + uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for + //! DPI/QPI/OPI switch or reset command + flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt + //! sequence number, [31:16] Reserved + uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration + uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable + uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe + flexspi_lut_seq_t + configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq + uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use + uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands + uint32_t reserved2; //!< [0x03c-0x03f] Reserved for future use + uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more + //! details + uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details + uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal + uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot + //! Chapter for more details + uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot + //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH + uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use + uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1 + uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2 + uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1 + uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2 + uint32_t csPadSettingOverride; //!< [0x060-0x063] CS pad setting override value + uint32_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value + uint32_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value + uint32_t dqsPadSettingOverride; //!< [0x06c-0x06f] DQS pad setting override value + uint32_t timeoutInMs; //!< [0x070-0x073] Timeout threshold for read status command + uint32_t commandInterval; //!< [0x074-0x077] CS deselect interval between two commands + uint16_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B, in terms of 0.1ns + uint16_t busyOffset; //!< [0x07c-0x07d] Busy offset, valid value: 0-31 + uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 - + //! busy flag is 0 when flash device is busy + uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences + flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences + uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use +} flexspi_mem_config_t; + +/* */ +#define NOR_CMD_INDEX_READ CMD_INDEX_READ //!< 0 +#define NOR_CMD_INDEX_READSTATUS CMD_INDEX_READSTATUS //!< 1 +#define NOR_CMD_INDEX_WRITEENABLE CMD_INDEX_WRITEENABLE //!< 2 +#define NOR_CMD_INDEX_ERASESECTOR 3 //!< 3 +#define NOR_CMD_INDEX_PAGEPROGRAM CMD_INDEX_WRITE //!< 4 +#define NOR_CMD_INDEX_CHIPERASE 5 //!< 5 +#define NOR_CMD_INDEX_DUMMY 6 //!< 6 +#define NOR_CMD_INDEX_ERASEBLOCK 7 //!< 7 + +#define NOR_CMD_LUT_SEQ_IDX_READ CMD_LUT_SEQ_IDX_READ //!< 0 READ LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS \ + CMD_LUT_SEQ_IDX_READSTATUS //!< 1 Read Status LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS_XPI \ + 2 //!< 2 Read status DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE \ + CMD_LUT_SEQ_IDX_WRITEENABLE //!< 3 Write Enable sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE_XPI \ + 4 //!< 4 Write Enable DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 //!< 5 Erase Sector sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASEBLOCK 8 //!< 8 Erase Block sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM \ + CMD_LUT_SEQ_IDX_WRITE //!< 9 Program sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_CHIPERASE 11 //!< 11 Chip Erase sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READ_SFDP 13 //!< 13 Read SFDP sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_RESTORE_NOCMD \ + 14 //!< 14 Restore 0-4-4/0-8-8 mode sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_EXIT_NOCMD \ + 15 //!< 15 Exit 0-4-4/0-8-8 mode sequence id in lookupTable stored in config blobk + +/* + * Serial NOR configuration block + */ +typedef struct _flexspi_nor_config +{ + flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI + uint32_t pageSize; //!< Page size of Serial NOR + uint32_t sectorSize; //!< Sector size of Serial NOR + uint8_t ipcmdSerialClkFreq; //!< Clock frequency for IP command + uint8_t isUniformBlockSize; //!< Sector/Block size is the same + uint8_t reserved0[2]; //!< Reserved for future use + uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3 + uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command + uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false + uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution + uint32_t blockSize; //!< Block size + uint32_t reserve2[11]; //!< Reserved for future use +} flexspi_nor_config_t; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif /* __EVKMIMXRT1064_FLEXSPI_NOR_CONFIG__ */ diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/flash_config.c b/ports/mimxrt/boards/MIMXRT1064_EVK/flash_config.c new file mode 100644 index 0000000000000..bfb1c2d59aa25 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/flash_config.c @@ -0,0 +1,49 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "evkmimxrt1064_flexspi_nor_config.h" + +/* Component ID definition, used by tools. */ +#ifndef FSL_COMPONENT_ID +#define FSL_COMPONENT_ID "platform.drivers.xip_board" +#endif + +/******************************************************************************* + * Code + ******************************************************************************/ +#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1) +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.conf"))) +#elif defined(__ICCARM__) +#pragma location = ".boot_hdr.conf" +#endif + +const flexspi_nor_config_t qspiflash_config = { + .memConfig = + { + .tag = FLEXSPI_CFG_BLK_TAG, + .version = FLEXSPI_CFG_BLK_VERSION, + .readSampleClkSrc = kFlexSPIReadSampleClk_LoopbackFromDqsPad, + .csHoldTime = 3u, + .csSetupTime = 3u, + // Enable DDR mode, Wordaddassable, Safe configuration, Differential clock + .sflashPadType = kSerialFlash_4Pads, + .serialClkFreq = kFlexSpiSerialClk_100MHz, + .sflashA1Size = 8u * 1024u * 1024u, + .lookupTable = + { + // Read LUTs + FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0xEB, RADDR_SDR, FLEXSPI_4PAD, 0x18), + FLEXSPI_LUT_SEQ(DUMMY_SDR, FLEXSPI_4PAD, 0x06, READ_SDR, FLEXSPI_4PAD, 0x04), + }, + }, + .pageSize = 256u, + .sectorSize = 4u * 1024u, + .blockSize = 256u * 1024u, + .isUniformBlockSize = false, +}; +#endif /* XIP_BOOT_HEADER_ENABLE */ diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h new file mode 100644 index 0000000000000..e05c8824d26ce --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h @@ -0,0 +1,8 @@ +#define MICROPY_HW_BOARD_NAME "i.MX RT1064 EVK" +#define MICROPY_HW_MCU_NAME "MIMXRT1064DVL6A" + + +// MIMXRT1064_EVK has 1 user LED +#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.mk new file mode 100644 index 0000000000000..700988919146d --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.mk @@ -0,0 +1,9 @@ +MCU_SERIES = MIMXRT1064 +MCU_VARIANT = MIMXRT1064DVL6A + +JLINK_PATH ?= /media/RT1064-EVK/ + +CFLAGS += -DBOARD_FLASH_SIZE=0x400000 + +deploy: $(BUILD)/firmware.bin + cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c new file mode 100644 index 0000000000000..d5da9c6f99275 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_AD_B0_09_af[] = { + PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), +}; + +pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h new file mode 100644 index 0000000000000..baef51c6c8612 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_AD_B0_09; From 4f2fe346239b6b3f720708021c868a88d97bb16d Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 16:12:09 +1000 Subject: [PATCH 0270/3533] tools/mpy-tool.py: Fix merge of multiple mpy files to POP_TOP correctly. MP_BC_CALL_FUNCTION will leave the result on the Python stack, so that result must be discarded by MP_BC_POP_TOP. Signed-off-by: Damien George --- tools/mpy-tool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 1ac6c93d7d8b2..de7cfe5d631c7 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -938,7 +938,7 @@ def merge_mpy(raw_codes, output_file): merged_mpy.extend(header) bytecode = bytearray() - bytecode_len = 6 + len(raw_codes) * 4 + 2 + bytecode_len = 6 + len(raw_codes) * 5 + 2 bytecode.append(bytecode_len << 2) # kind and length bytecode.append(0b00000000) # signature prelude bytecode.append(0b00001000) # size prelude @@ -947,7 +947,7 @@ def merge_mpy(raw_codes, output_file): for idx in range(len(raw_codes)): bytecode.append(0x32) # MP_BC_MAKE_FUNCTION bytecode.append(idx) # index raw code - bytecode.extend(b"\x34\x00") # MP_BC_CALL_FUNCTION, 0 args + bytecode.extend(b"\x34\x00\x59") # MP_BC_CALL_FUNCTION, 0 args, MP_BC_POP_TOP bytecode.extend(b"\x51\x63") # MP_BC_LOAD_NONE, MP_BC_RETURN_VALUE bytecode.append(0) # n_obj From 75344af4cada48b8b044ae35d0e62c38c601d899 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 8 Sep 2020 13:52:13 +1000 Subject: [PATCH 0271/3533] nrf/main: Make mp_builtin_open signature match that in py/builtin.h. Signed-off-by: Damien George --- ports/nrf/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 670c88e7cad9d..86ba83342b9f3 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -280,10 +280,10 @@ mp_import_stat_t mp_import_stat(const char *path) { return uos_mbfs_import_stat(path); } -STATIC mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args) { +mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { return uos_mbfs_open(n_args, args); } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open); +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); #else // use dummy functions - no filesystem available @@ -295,7 +295,7 @@ mp_import_stat_t mp_import_stat(const char *path) { return MP_IMPORT_STAT_NO_EXIST; } -STATIC mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { +mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { mp_raise_OSError(MP_EPERM); } MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); From 27e117307d7d5b63955940866e6094bedb7d299e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 8 Sep 2020 13:52:56 +1000 Subject: [PATCH 0272/3533] nrf: Remove unnecessary includes of mpconfigport.h and its header guard. The mpconfigport.h file is an internal header and should only ever be included once by mpconfig.h. Signed-off-by: Damien George --- ports/nrf/drivers/bluetooth/ble_drv.c | 1 - ports/nrf/modules/uos/microbitfs.c | 1 - ports/nrf/mpconfigport.h | 5 ----- 3 files changed, 7 deletions(-) diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c index 1a64cdfbd24b9..3a15025cb4978 100644 --- a/ports/nrf/drivers/bluetooth/ble_drv.c +++ b/ports/nrf/drivers/bluetooth/ble_drv.c @@ -32,7 +32,6 @@ #include "py/runtime.h" #include "ble_drv.h" -#include "mpconfigport.h" #include "nrf_sdm.h" #include "ble_gap.h" #include "ble.h" // sd_ble_uuid_encode diff --git a/ports/nrf/modules/uos/microbitfs.c b/ports/nrf/modules/uos/microbitfs.c index 7f16372cf2224..e8628f4945c20 100644 --- a/ports/nrf/modules/uos/microbitfs.c +++ b/ports/nrf/modules/uos/microbitfs.c @@ -36,7 +36,6 @@ #include "py/stream.h" #include "py/runtime.h" #include "extmod/vfs.h" -#include "mpconfigport.h" #if MICROPY_MBFS diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 1197df016cce9..76bb6d7bdf0df 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -24,9 +24,6 @@ * THE SOFTWARE. */ -#ifndef NRF5_MPCONFIGPORT_H__ -#define NRF5_MPCONFIGPORT_H__ - #include #if defined(NRF51822) @@ -342,5 +339,3 @@ extern const struct _mp_obj_module_t ble_module; #include #define MICROPY_PIN_DEFS_PORT_H "pin_defs_nrf5.h" - -#endif From 547688c58cce3436082cc9338929c2699410c0e1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 10 Sep 2020 20:43:07 +1000 Subject: [PATCH 0273/3533] stm32/usb: Don't nul pyb_hid_report_desc if MICROPY_HW_USB_HID disabled. So this code can be used if pyb_hid_report_desc is not included in the port's root pointer list. Signed-off-by: Damien George --- ports/stm32/usb.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 657d4cb6e7402..968b2999ced37 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -221,7 +221,9 @@ void pyb_usb_init0(void) { for (int i = 0; i < MICROPY_HW_USB_CDC_NUM; ++i) { usb_device.usbd_cdc_itf[i].attached_to_repl = false; } + #if MICROPY_HW_USB_HID MP_STATE_PORT(pyb_hid_report_desc) = MP_OBJ_NULL; + #endif pyb_usb_vcp_init0(); } From 709398daae14b3630de7050e08b9e77ea1232716 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 10 Sep 2020 20:46:40 +1000 Subject: [PATCH 0274/3533] stm32/rtc.h: Include py/obj.h to make header self contained. Signed-off-by: Damien George --- ports/stm32/rtc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/rtc.h b/ports/stm32/rtc.h index d3840c1b73592..5c454b3e3937f 100644 --- a/ports/stm32/rtc.h +++ b/ports/stm32/rtc.h @@ -26,6 +26,8 @@ #ifndef MICROPY_INCLUDED_STM32_RTC_H #define MICROPY_INCLUDED_STM32_RTC_H +#include "py/obj.h" + extern RTC_HandleTypeDef RTCHandle; extern const mp_obj_type_t pyb_rtc_type; From 8d5a40c86e384bf3cddb2f687374e0bb1ae6df7d Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 11:04:46 +1000 Subject: [PATCH 0275/3533] py/objfloat: Fix handling of negative float to power of nan. Prior to this commit, pow(-2, float('nan')) would return (nan+nanj), or raise an exception on targets that don't support complex numbers. This is fixed to return simply nan, as CPython does. Signed-off-by: Damien George --- py/objfloat.c | 2 +- tests/float/inf_nan_arith.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/float/inf_nan_arith.py diff --git a/py/objfloat.c b/py/objfloat.c index 09b73c8cda525..f1e401eccecfe 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -293,7 +293,7 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t if (lhs_val == 0 && rhs_val < 0 && !isinf(rhs_val)) { goto zero_division_error; } - if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(floor)(rhs_val)) { + if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(floor)(rhs_val) && !isnan(rhs_val)) { #if MICROPY_PY_BUILTINS_COMPLEX return mp_obj_complex_binary_op(MP_BINARY_OP_POWER, lhs_val, 0, rhs_in); #else diff --git a/tests/float/inf_nan_arith.py b/tests/float/inf_nan_arith.py new file mode 100644 index 0000000000000..c27e38bc520be --- /dev/null +++ b/tests/float/inf_nan_arith.py @@ -0,0 +1,20 @@ +# Test behaviour of inf and nan in basic float operations + +inf = float("inf") +nan = float("nan") + +values = (-2, -1, 0, 1, 2, inf, nan) + +for x in values: + for y in values: + print(x, y) + print(" + - *", x + y, x - y, x * y) + try: + print(" /", x / y) + except ZeroDivisionError: + print(" / ZeroDivisionError") + try: + print(" ** pow", x ** y, pow(x, y)) + except ZeroDivisionError: + print(" ** pow ZeroDivisionError") + print(" == != < <= > >=", x == y, x != y, x < y, x <= y, x > y, x >= y) From 2e54d9d146b34d7ad00e4394c9767f4319244cdf Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 8 Sep 2020 15:22:34 +0200 Subject: [PATCH 0276/3533] py: Fix handling of NaN in certain pow implementations. Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly. --- ports/windows/mpconfigport.h | 2 ++ py/modmath.c | 12 ++++++++++++ py/mpconfig.h | 5 +++++ py/objfloat.c | 6 ++++++ tests/float/math_domain.py | 2 +- 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 30389c700eac3..faf10752ee203 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -236,6 +236,8 @@ extern const struct _mp_obj_module_t mp_module_time; #define MICROPY_PY_MATH_FMOD_FIX_INFNAN (1) #ifdef _WIN64 #define MICROPY_PY_MATH_MODF_FIX_NEGZERO (1) +#else +#define MICROPY_PY_MATH_POW_FIX_NAN (1) #endif #endif diff --git a/py/modmath.c b/py/modmath.c index b312eeb3d2c01..b7948f39e7c91 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -98,7 +98,19 @@ mp_float_t MICROPY_FLOAT_C_FUN(log2)(mp_float_t x) { // sqrt(x): returns the square root of x MATH_FUN_1(sqrt, sqrt) // pow(x, y): returns x to the power of y +#if MICROPY_PY_MATH_POW_FIX_NAN +mp_float_t pow_func(mp_float_t x, mp_float_t y) { + // pow(base, 0) returns 1 for any base, even when base is NaN + // pow(+1, exponent) returns 1 for any exponent, even when exponent is NaN + if (x == MICROPY_FLOAT_CONST(1.0) || y == MICROPY_FLOAT_CONST(0.0)) { + return MICROPY_FLOAT_CONST(1.0); + } + return MICROPY_FLOAT_C_FUN(pow)(x, y); +} +MATH_FUN_2(pow, pow_func) +#else MATH_FUN_2(pow, pow) +#endif // exp(x) MATH_FUN_1(exp, exp) #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS diff --git a/py/mpconfig.h b/py/mpconfig.h index ae4cabfdcf0f2..12517316c6c80 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1160,6 +1160,11 @@ typedef double mp_float_t; #define MICROPY_PY_MATH_MODF_FIX_NEGZERO (0) #endif +// Whether to provide fix for pow(1, NaN) and pow(NaN, 0), which both should be 1 not NaN. +#ifndef MICROPY_PY_MATH_POW_FIX_NAN +#define MICROPY_PY_MATH_POW_FIX_NAN (0) +#endif + // Whether to provide "cmath" module #ifndef MICROPY_PY_CMATH #define MICROPY_PY_CMATH (0) diff --git a/py/objfloat.c b/py/objfloat.c index f1e401eccecfe..451609492e5c1 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -300,6 +300,12 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t mp_raise_ValueError(MP_ERROR_TEXT("complex values not supported")); #endif } + #if MICROPY_PY_MATH_POW_FIX_NAN // Also see modmath.c. + if (lhs_val == MICROPY_FLOAT_CONST(1.0) || rhs_val == MICROPY_FLOAT_CONST(0.0)) { + lhs_val = MICROPY_FLOAT_CONST(1.0); + break; + } + #endif lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break; case MP_BINARY_OP_DIVMOD: { diff --git a/tests/float/math_domain.py b/tests/float/math_domain.py index e63628cf5078a..0c25dc08b6649 100644 --- a/tests/float/math_domain.py +++ b/tests/float/math_domain.py @@ -38,7 +38,7 @@ # double argument functions for name, f, args in ( - ("pow", math.pow, ((0, 2), (-1, 2), (0, -1), (-1, 2.3))), + ("pow", math.pow, ((0, 2), (-1, 2), (0, -1), (-1, 2.3), (nan, 0), (1, nan))), ("fmod", math.fmod, ((1.2, inf), (1.2, -inf), (1.2, 0), (inf, 1.2))), ("atan2", math.atan2, ((0, 0), (-inf, inf), (-inf, -inf), (inf, -inf))), ("copysign", math.copysign, ()), From 373b400632bb5371f1e259983850814d2384e667 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Sep 2020 00:53:18 +1000 Subject: [PATCH 0277/3533] extmod/modussl_axtls: Reduce size of code that makes exception. Change in code size (for ports that use axtls) is: unix x64: -152 -0.030% [incl -160(data)] unix nanbox: -112 -0.025% [incl -96(data)] esp8266: -64 -0.009% GENERIC Signed-off-by: Damien George --- extmod/modussl_axtls.c | 96 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 0b0ce35fc8711..da5941a55b33e 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -55,53 +55,65 @@ struct ssl_args { STATIC const mp_obj_type_t ussl_socket_type; -// Table of errors -struct ssl_errs { - int16_t errnum; - const char *errstr; +// Table of error strings corresponding to SSL_xxx error codes. +STATIC const char *const ssl_error_tab1[] = { + "NOT_OK", + "DEAD", + "CLOSE_NOTIFY", + "EAGAIN", }; -STATIC const struct ssl_errs ssl_error_tab[] = { - { SSL_NOT_OK, "NOT_OK" }, - { SSL_ERROR_DEAD, "DEAD" }, - { SSL_CLOSE_NOTIFY, "CLOSE_NOTIFY" }, - { SSL_EAGAIN, "EAGAIN" }, - { SSL_ERROR_CONN_LOST, "CONN_LOST" }, - { SSL_ERROR_RECORD_OVERFLOW, "RECORD_OVERFLOW" }, - { SSL_ERROR_SOCK_SETUP_FAILURE, "SOCK_SETUP_FAILURE" }, - { SSL_ERROR_INVALID_HANDSHAKE, "INVALID_HANDSHAKE" }, - { SSL_ERROR_INVALID_PROT_MSG, "INVALID_PROT_MSG" }, - { SSL_ERROR_INVALID_HMAC, "INVALID_HMAC" }, - { SSL_ERROR_INVALID_VERSION, "INVALID_VERSION" }, - { SSL_ERROR_UNSUPPORTED_EXTENSION, "UNSUPPORTED_EXTENSION" }, - { SSL_ERROR_INVALID_SESSION, "INVALID_SESSION" }, - { SSL_ERROR_NO_CIPHER, "NO_CIPHER" }, - { SSL_ERROR_INVALID_CERT_HASH_ALG, "INVALID_CERT_HASH_ALG" }, - { SSL_ERROR_BAD_CERTIFICATE, "BAD_CERTIFICATE" }, - { SSL_ERROR_INVALID_KEY, "INVALID_KEY" }, - { SSL_ERROR_FINISHED_INVALID, "FINISHED_INVALID" }, - { SSL_ERROR_NO_CERT_DEFINED, "NO_CERT_DEFINED" }, - { SSL_ERROR_NO_CLIENT_RENOG, "NO_CLIENT_RENOG" }, - { SSL_ERROR_NOT_SUPPORTED, "NOT_SUPPORTED" }, +STATIC const char *const ssl_error_tab2[] = { + "CONN_LOST", + "RECORD_OVERFLOW", + "SOCK_SETUP_FAILURE", + NULL, + "INVALID_HANDSHAKE", + "INVALID_PROT_MSG", + "INVALID_HMAC", + "INVALID_VERSION", + "UNSUPPORTED_EXTENSION", + "INVALID_SESSION", + "NO_CIPHER", + "INVALID_CERT_HASH_ALG", + "BAD_CERTIFICATE", + "INVALID_KEY", + NULL, + "FINISHED_INVALID", + "NO_CERT_DEFINED", + "NO_CLIENT_RENOG", + "NOT_SUPPORTED", }; STATIC NORETURN void ussl_raise_error(int err) { - for (size_t i = 0; i < MP_ARRAY_SIZE(ssl_error_tab); i++) { - if (ssl_error_tab[i].errnum == err) { - // construct string object - mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); - if (o_str == NULL) { - break; - } - o_str->base.type = &mp_type_str; - o_str->data = (const byte *)ssl_error_tab[i].errstr; - o_str->len = strlen((char *)o_str->data); - o_str->hash = qstr_compute_hash(o_str->data, o_str->len); - // raise - mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)}; - nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); - } + MP_STATIC_ASSERT(SSL_NOT_OK - 3 == SSL_EAGAIN); + MP_STATIC_ASSERT(SSL_ERROR_CONN_LOST - 18 == SSL_ERROR_NOT_SUPPORTED); + + // Check if err corresponds to something in one of the error string tables. + const char *errstr = NULL; + if (SSL_NOT_OK >= err && err >= SSL_EAGAIN) { + errstr = ssl_error_tab1[SSL_NOT_OK - err]; + } else if (SSL_ERROR_CONN_LOST >= err && err >= SSL_ERROR_NOT_SUPPORTED) { + errstr = ssl_error_tab2[SSL_ERROR_CONN_LOST - err]; + } + + // Unknown error, just raise the error code. + if (errstr == NULL) { + mp_raise_OSError(err); + } + + // Construct string object. + mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); + if (o_str == NULL) { + mp_raise_OSError(err); } - mp_raise_OSError(err); + o_str->base.type = &mp_type_str; + o_str->data = (const byte *)errstr; + o_str->len = strlen((char *)o_str->data); + o_str->hash = qstr_compute_hash(o_str->data, o_str->len); + + // Raise OSError(err, str). + mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)}; + nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); } From 4b35aa5730cf26992453e3ce7cf74ced4128fe20 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 10 Sep 2020 12:22:49 +0200 Subject: [PATCH 0278/3533] tools: Write msvc-compatible frozen content. The msvc compiler doesn't accept zero-sized arrays so let the freezing process generate compatible C code in case no modules are found and the involved arrays are all empty. This doesn't affect the functionality in any way because those arrays only get accessed when mp_frozen_mpy_names contains names, i.e. when modules are actually found. --- tools/make-frozen.py | 4 ++-- tools/makemanifest.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/make-frozen.py b/tools/make-frozen.py index a8706bf784fdc..bc35d383429d6 100755 --- a/tools/make-frozen.py +++ b/tools/make-frozen.py @@ -50,7 +50,7 @@ def module_name(f): for f, st in modules: print("%d," % st.st_size) -print("};") +print("0};") print("const char mp_frozen_str_content[] = {") for f, st in modules: @@ -82,4 +82,4 @@ def module_name(f): chrs.append('\\0"') print("".join(chrs)) -print("};") +print('"\\0"};') diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 377f245596fe9..9ef03682607dc 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -327,7 +327,7 @@ def main(): b" (qstr_pool_t*)&mp_qstr_const_pool, MP_QSTRnumber_of, 0, 0\n" b"};\n" b'const char mp_frozen_mpy_names[1] = {"\\0"};\n' - b"const mp_raw_code_t *const mp_frozen_mpy_content[0] = {};\n" + b"const mp_raw_code_t *const mp_frozen_mpy_content[1] = {NULL};\n" ) # Generate output From 2a9ea69fa93b1b940b802f981f8835f6decdb085 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 10 Sep 2020 12:11:58 +0200 Subject: [PATCH 0279/3533] windows/msvc: Support freezing modules. Support freezing modules via manifest.py for consistency with the other ports. In essence this comes down to calling makemanifest.py and adding the resulting .c file to the build. Note the file with preprocessed qstrs has been renamed to match what makemanifest.py expects and which is also the name all other ports use. --- ports/windows/micropython.vcxproj | 2 +- ports/windows/msvc/genhdr.targets | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ports/windows/micropython.vcxproj b/ports/windows/micropython.vcxproj index f70fe96158d07..73a837a8408ab 100644 --- a/ports/windows/micropython.vcxproj +++ b/ports/windows/micropython.vcxproj @@ -104,7 +104,7 @@ - + diff --git a/ports/windows/msvc/genhdr.targets b/ports/windows/msvc/genhdr.targets index 327f922e4a162..3af0ea263699b 100644 --- a/ports/windows/msvc/genhdr.targets +++ b/ports/windows/msvc/genhdr.targets @@ -55,7 +55,7 @@ using(var outFile = System.IO.File.CreateText(OutputFile)) { - + $([System.String]::new('%(FullPath)').Replace('$(PyBaseDir)', '$(DestDir)qstr\')) @@ -101,8 +101,8 @@ using(var outFile = System.IO.File.CreateText(OutputFile)) { $(QstrGen).tmp - - + + @@ -115,6 +115,17 @@ using(var outFile = System.IO.File.CreateText(OutputFile)) { + + + + + MICROPY_MODULE_FROZEN_MPY=1;MICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool;%(PreprocessorDefinitions) + + + + + + From 5b94c610971334019e7301026e0fbbf5950e094d Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 10 Sep 2020 14:13:25 +0200 Subject: [PATCH 0280/3533] windows/Makefile: Support freezing modules. Alter the build flags as needed to support freezing modules with a manifest. This makes freezing works just like it does for e.g. the unix port. --- ports/windows/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/windows/Makefile b/ports/windows/Makefile index b2d11872ed3a6..3bfbc18304818 100644 --- a/ports/windows/Makefile +++ b/ports/windows/Makefile @@ -58,4 +58,9 @@ SRC_QSTR += $(SRC_C) # SRC_QSTR SRC_QSTR_AUTO_DEPS += +ifneq ($(FROZEN_MANIFEST),) +CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool -DMICROPY_MODULE_FROZEN_MPY=1 -DMPZ_DIG_SIZE=16 +MPY_CROSS_FLAGS += -mcache-lookup-bc +endif + include $(TOP)/py/mkrules.mk From 70bec41089eef26122d6c2cbd06bf359bc0ab253 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 10 Sep 2020 13:21:21 +0200 Subject: [PATCH 0281/3533] windows: Show test failures in the Appveyor builds. --- ports/windows/.appveyor.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ports/windows/.appveyor.yml b/ports/windows/.appveyor.yml index 2965543b05942..4d2d6bd11dc87 100644 --- a/ports/windows/.appveyor.yml +++ b/ports/windows/.appveyor.yml @@ -38,7 +38,15 @@ test_script: - ps: | cd (Join-Path $env:APPVEYOR_BUILD_FOLDER 'tests') & $env:MICROPY_CPYTHON3 run-tests + if ($LASTEXITCODE -ne 0) { + & $env:MICROPY_CPYTHON3 run-tests --print-failures + throw "Test failure" + } & $env:MICROPY_CPYTHON3 run-tests --via-mpy -d basics float micropython + if ($LASTEXITCODE -ne 0) { + & $env:MICROPY_CPYTHON3 run-tests --print-failures + throw "Test failure" + } # After the build/test phase for the MSVC build completes, # build and test with mingw-w64, release versions only. @@ -69,9 +77,11 @@ after_test: } & $env:MICROPY_CPYTHON3 $testArgs if ($LASTEXITCODE -ne 0) { - throw "$env:MSYSTEM tests exited with code $LASTEXITCODE" + & $env:MICROPY_CPYTHON3 run-tests --print-failures + throw "Test failure" } & $env:MICROPY_CPYTHON3 ($testArgs + @('--via-mpy', '-d', 'basics', 'float', 'micropython')) if ($LASTEXITCODE -ne 0) { - throw "$env:MSYSTEM mpy-cross tests exited with code $LASTEXITCODE" + & $env:MICROPY_CPYTHON3 run-tests --print-failures + throw "Test failure" } From 50efce81740901fee6f5191db36a48ada13ef6df Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 10 Sep 2020 11:31:01 +1000 Subject: [PATCH 0282/3533] esp32/mpconfigport.h: Remove duplicate uhashlib registration. --- ports/esp32/mpconfigport.h | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 7c09bdbd9218c..663fed3f683a4 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -200,7 +200,6 @@ extern const struct _mp_obj_module_t mp_module_onewire; { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \ { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \ #define MP_STATE_PORT MP_STATE_VM From 85f2b239d8ff8d29155c63799717e3f88b2f33f3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 11 Sep 2020 17:22:28 +1000 Subject: [PATCH 0283/3533] py/showbc: Pass in an mp_print_t struct to all bytecode-print functions. So the output can be redirected if needed. Signed-off-by: Damien George --- py/bc.h | 8 +- py/emitglue.c | 2 +- py/showbc.c | 209 +++++++++++++++++++++++++------------------------- py/vm.c | 2 +- 4 files changed, 109 insertions(+), 112 deletions(-) diff --git a/py/bc.h b/py/bc.h index 0c19c516595ac..16f314e19936f 100644 --- a/py/bc.h +++ b/py/bc.h @@ -226,10 +226,10 @@ const byte *mp_decode_uint_skip(const byte *ptr); mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc); mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args); void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args); -void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table); -void mp_bytecode_print2(const byte *code, size_t len, const mp_uint_t *const_table); -const byte *mp_bytecode_print_str(const byte *ip); -#define mp_bytecode_print_inst(code, const_table) mp_bytecode_print2(code, 1, const_table) +void mp_bytecode_print(const mp_print_t *print, const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table); +void mp_bytecode_print2(const mp_print_t *print, const byte *code, size_t len, const mp_uint_t *const_table); +const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip); +#define mp_bytecode_print_inst(print, code, const_table) mp_bytecode_print2(print, code, 1, const_table) // Helper macros to access pointer with least significant bits holding flags #define MP_TAGPTR_PTR(x) ((void *)((uintptr_t)(x) & ~((uintptr_t)3))) diff --git a/py/emitglue.c b/py/emitglue.c index 98320c426bc63..0ef708a3f3f56 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -92,7 +92,7 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, #endif #if MICROPY_DEBUG_PRINTERS if (mp_verbose_flag >= 2) { - mp_bytecode_print(rc, code, len, const_table); + mp_bytecode_print(&mp_plat_print, rc, code, len, const_table); } #endif } diff --git a/py/showbc.c b/py/showbc.c index 4d6527fae2e45..8941dd7544487 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -32,9 +32,6 @@ #if MICROPY_DEBUG_PRINTERS -// redirect all printfs in this file to the platform print stream -#define printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) - #define DECODE_UINT { \ unum = 0; \ do { \ @@ -80,7 +77,7 @@ const byte *mp_showbc_code_start; const mp_uint_t *mp_showbc_const_table; -void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const mp_uint_t *const_table) { +void mp_bytecode_print(const mp_print_t *print, const void *descr, const byte *ip, mp_uint_t len, const mp_uint_t *const_table) { mp_showbc_code_start = ip; // Decode prelude @@ -96,30 +93,30 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m qstr block_name = mp_decode_uint(&code_info); qstr source_file = mp_decode_uint(&code_info); #endif - printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n", + mp_printf(print, "File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n", qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len); // raw bytecode dump size_t prelude_size = ip - mp_showbc_code_start + n_info + n_cell; - printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", + mp_printf(print, "Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", prelude_size, len - prelude_size); for (mp_uint_t i = 0; i < len; i++) { if (i > 0 && i % 16 == 0) { - printf("\n"); + mp_printf(print, "\n"); } - printf(" %02x", mp_showbc_code_start[i]); + mp_printf(print, " %02x", mp_showbc_code_start[i]); } - printf("\n"); + mp_printf(print, "\n"); // bytecode prelude: arg names (as qstr objects) - printf("arg names:"); + mp_printf(print, "arg names:"); for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) { - printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i]))); + mp_printf(print, " %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i]))); } - printf("\n"); + mp_printf(print, "\n"); - printf("(N_STATE %u)\n", (unsigned)n_state); - printf("(N_EXC_STACK %u)\n", (unsigned)n_exc_stack); + mp_printf(print, "(N_STATE %u)\n", (unsigned)n_state); + mp_printf(print, "(N_EXC_STACK %u)\n", (unsigned)n_exc_stack); // skip over code_info ip += n_info; @@ -127,14 +124,14 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m // bytecode prelude: initialise closed over variables for (size_t i = 0; i < n_cell; ++i) { uint local_num = *ip++; - printf("(INIT_CELL %u)\n", local_num); + mp_printf(print, "(INIT_CELL %u)\n", local_num); } // print out line number info { mp_int_t bc = 0; mp_uint_t source_line = 1; - printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); + mp_printf(print, " bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); for (const byte *ci = code_info; *ci;) { if ((ci[0] & 0x80) == 0) { // 0b0LLBBBBB encoding @@ -147,27 +144,27 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m source_line += ((ci[0] << 4) & 0x700) | ci[1]; ci += 2; } - printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); + mp_printf(print, " bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); } } - mp_bytecode_print2(ip, len - prelude_size, const_table); + mp_bytecode_print2(print, ip, len - prelude_size, const_table); } -const byte *mp_bytecode_print_str(const byte *ip) { +const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip) { mp_uint_t unum; qstr qst; switch (*ip++) { case MP_BC_LOAD_CONST_FALSE: - printf("LOAD_CONST_FALSE"); + mp_printf(print, "LOAD_CONST_FALSE"); break; case MP_BC_LOAD_CONST_NONE: - printf("LOAD_CONST_NONE"); + mp_printf(print, "LOAD_CONST_NONE"); break; case MP_BC_LOAD_CONST_TRUE: - printf("LOAD_CONST_TRUE"); + mp_printf(print, "LOAD_CONST_TRUE"); break; case MP_BC_LOAD_CONST_SMALL_INT: { @@ -179,197 +176,197 @@ const byte *mp_bytecode_print_str(const byte *ip) { do { num = (num << 7) | (*ip & 0x7f); } while ((*ip++ & 0x80) != 0); - printf("LOAD_CONST_SMALL_INT " INT_FMT, num); + mp_printf(print, "LOAD_CONST_SMALL_INT " INT_FMT, num); break; } case MP_BC_LOAD_CONST_STRING: DECODE_QSTR; - printf("LOAD_CONST_STRING '%s'", qstr_str(qst)); + mp_printf(print, "LOAD_CONST_STRING '%s'", qstr_str(qst)); break; case MP_BC_LOAD_CONST_OBJ: DECODE_OBJ; - printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum)); - mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR); + mp_printf(print, "LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum)); + mp_obj_print_helper(print, (mp_obj_t)unum, PRINT_REPR); break; case MP_BC_LOAD_NULL: - printf("LOAD_NULL"); + mp_printf(print, "LOAD_NULL"); break; case MP_BC_LOAD_FAST_N: DECODE_UINT; - printf("LOAD_FAST_N " UINT_FMT, unum); + mp_printf(print, "LOAD_FAST_N " UINT_FMT, unum); break; case MP_BC_LOAD_DEREF: DECODE_UINT; - printf("LOAD_DEREF " UINT_FMT, unum); + mp_printf(print, "LOAD_DEREF " UINT_FMT, unum); break; case MP_BC_LOAD_NAME: DECODE_QSTR; - printf("LOAD_NAME %s", qstr_str(qst)); + mp_printf(print, "LOAD_NAME %s", qstr_str(qst)); if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) { - printf(" (cache=%u)", *ip++); + mp_printf(print, " (cache=%u)", *ip++); } break; case MP_BC_LOAD_GLOBAL: DECODE_QSTR; - printf("LOAD_GLOBAL %s", qstr_str(qst)); + mp_printf(print, "LOAD_GLOBAL %s", qstr_str(qst)); if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) { - printf(" (cache=%u)", *ip++); + mp_printf(print, " (cache=%u)", *ip++); } break; case MP_BC_LOAD_ATTR: DECODE_QSTR; - printf("LOAD_ATTR %s", qstr_str(qst)); + mp_printf(print, "LOAD_ATTR %s", qstr_str(qst)); if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) { - printf(" (cache=%u)", *ip++); + mp_printf(print, " (cache=%u)", *ip++); } break; case MP_BC_LOAD_METHOD: DECODE_QSTR; - printf("LOAD_METHOD %s", qstr_str(qst)); + mp_printf(print, "LOAD_METHOD %s", qstr_str(qst)); break; case MP_BC_LOAD_SUPER_METHOD: DECODE_QSTR; - printf("LOAD_SUPER_METHOD %s", qstr_str(qst)); + mp_printf(print, "LOAD_SUPER_METHOD %s", qstr_str(qst)); break; case MP_BC_LOAD_BUILD_CLASS: - printf("LOAD_BUILD_CLASS"); + mp_printf(print, "LOAD_BUILD_CLASS"); break; case MP_BC_LOAD_SUBSCR: - printf("LOAD_SUBSCR"); + mp_printf(print, "LOAD_SUBSCR"); break; case MP_BC_STORE_FAST_N: DECODE_UINT; - printf("STORE_FAST_N " UINT_FMT, unum); + mp_printf(print, "STORE_FAST_N " UINT_FMT, unum); break; case MP_BC_STORE_DEREF: DECODE_UINT; - printf("STORE_DEREF " UINT_FMT, unum); + mp_printf(print, "STORE_DEREF " UINT_FMT, unum); break; case MP_BC_STORE_NAME: DECODE_QSTR; - printf("STORE_NAME %s", qstr_str(qst)); + mp_printf(print, "STORE_NAME %s", qstr_str(qst)); break; case MP_BC_STORE_GLOBAL: DECODE_QSTR; - printf("STORE_GLOBAL %s", qstr_str(qst)); + mp_printf(print, "STORE_GLOBAL %s", qstr_str(qst)); break; case MP_BC_STORE_ATTR: DECODE_QSTR; - printf("STORE_ATTR %s", qstr_str(qst)); + mp_printf(print, "STORE_ATTR %s", qstr_str(qst)); if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) { - printf(" (cache=%u)", *ip++); + mp_printf(print, " (cache=%u)", *ip++); } break; case MP_BC_STORE_SUBSCR: - printf("STORE_SUBSCR"); + mp_printf(print, "STORE_SUBSCR"); break; case MP_BC_DELETE_FAST: DECODE_UINT; - printf("DELETE_FAST " UINT_FMT, unum); + mp_printf(print, "DELETE_FAST " UINT_FMT, unum); break; case MP_BC_DELETE_DEREF: DECODE_UINT; - printf("DELETE_DEREF " UINT_FMT, unum); + mp_printf(print, "DELETE_DEREF " UINT_FMT, unum); break; case MP_BC_DELETE_NAME: DECODE_QSTR; - printf("DELETE_NAME %s", qstr_str(qst)); + mp_printf(print, "DELETE_NAME %s", qstr_str(qst)); break; case MP_BC_DELETE_GLOBAL: DECODE_QSTR; - printf("DELETE_GLOBAL %s", qstr_str(qst)); + mp_printf(print, "DELETE_GLOBAL %s", qstr_str(qst)); break; case MP_BC_DUP_TOP: - printf("DUP_TOP"); + mp_printf(print, "DUP_TOP"); break; case MP_BC_DUP_TOP_TWO: - printf("DUP_TOP_TWO"); + mp_printf(print, "DUP_TOP_TWO"); break; case MP_BC_POP_TOP: - printf("POP_TOP"); + mp_printf(print, "POP_TOP"); break; case MP_BC_ROT_TWO: - printf("ROT_TWO"); + mp_printf(print, "ROT_TWO"); break; case MP_BC_ROT_THREE: - printf("ROT_THREE"); + mp_printf(print, "ROT_THREE"); break; case MP_BC_JUMP: DECODE_SLABEL; - printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_POP_JUMP_IF_TRUE: DECODE_SLABEL; - printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_POP_JUMP_IF_FALSE: DECODE_SLABEL; - printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_JUMP_IF_TRUE_OR_POP: DECODE_SLABEL; - printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_JUMP_IF_FALSE_OR_POP: DECODE_SLABEL; - printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_SETUP_WITH: DECODE_ULABEL; // loop-like labels are always forward - printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_WITH_CLEANUP: - printf("WITH_CLEANUP"); + mp_printf(print, "WITH_CLEANUP"); break; case MP_BC_UNWIND_JUMP: DECODE_SLABEL; - printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip); + mp_printf(print, "UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip); ip += 1; break; case MP_BC_SETUP_EXCEPT: DECODE_ULABEL; // except labels are always forward - printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_SETUP_FINALLY: DECODE_ULABEL; // except labels are always forward - printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_END_FINALLY: @@ -377,169 +374,169 @@ const byte *mp_bytecode_print_str(const byte *ip) { // if TOS is an integer, does something else // if TOS is None, just pops it and continues // else error - printf("END_FINALLY"); + mp_printf(print, "END_FINALLY"); break; case MP_BC_GET_ITER: - printf("GET_ITER"); + mp_printf(print, "GET_ITER"); break; case MP_BC_GET_ITER_STACK: - printf("GET_ITER_STACK"); + mp_printf(print, "GET_ITER_STACK"); break; case MP_BC_FOR_ITER: DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward - printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_POP_EXCEPT_JUMP: DECODE_ULABEL; // these labels are always forward - printf("POP_EXCEPT_JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); + mp_printf(print, "POP_EXCEPT_JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start)); break; case MP_BC_BUILD_TUPLE: DECODE_UINT; - printf("BUILD_TUPLE " UINT_FMT, unum); + mp_printf(print, "BUILD_TUPLE " UINT_FMT, unum); break; case MP_BC_BUILD_LIST: DECODE_UINT; - printf("BUILD_LIST " UINT_FMT, unum); + mp_printf(print, "BUILD_LIST " UINT_FMT, unum); break; case MP_BC_BUILD_MAP: DECODE_UINT; - printf("BUILD_MAP " UINT_FMT, unum); + mp_printf(print, "BUILD_MAP " UINT_FMT, unum); break; case MP_BC_STORE_MAP: - printf("STORE_MAP"); + mp_printf(print, "STORE_MAP"); break; case MP_BC_BUILD_SET: DECODE_UINT; - printf("BUILD_SET " UINT_FMT, unum); + mp_printf(print, "BUILD_SET " UINT_FMT, unum); break; #if MICROPY_PY_BUILTINS_SLICE case MP_BC_BUILD_SLICE: DECODE_UINT; - printf("BUILD_SLICE " UINT_FMT, unum); + mp_printf(print, "BUILD_SLICE " UINT_FMT, unum); break; #endif case MP_BC_STORE_COMP: DECODE_UINT; - printf("STORE_COMP " UINT_FMT, unum); + mp_printf(print, "STORE_COMP " UINT_FMT, unum); break; case MP_BC_UNPACK_SEQUENCE: DECODE_UINT; - printf("UNPACK_SEQUENCE " UINT_FMT, unum); + mp_printf(print, "UNPACK_SEQUENCE " UINT_FMT, unum); break; case MP_BC_UNPACK_EX: DECODE_UINT; - printf("UNPACK_EX " UINT_FMT, unum); + mp_printf(print, "UNPACK_EX " UINT_FMT, unum); break; case MP_BC_MAKE_FUNCTION: DECODE_PTR; - printf("MAKE_FUNCTION %p", (void *)(uintptr_t)unum); + mp_printf(print, "MAKE_FUNCTION %p", (void *)(uintptr_t)unum); break; case MP_BC_MAKE_FUNCTION_DEFARGS: DECODE_PTR; - printf("MAKE_FUNCTION_DEFARGS %p", (void *)(uintptr_t)unum); + mp_printf(print, "MAKE_FUNCTION_DEFARGS %p", (void *)(uintptr_t)unum); break; case MP_BC_MAKE_CLOSURE: { DECODE_PTR; mp_uint_t n_closed_over = *ip++; - printf("MAKE_CLOSURE %p " UINT_FMT, (void *)(uintptr_t)unum, n_closed_over); + mp_printf(print, "MAKE_CLOSURE %p " UINT_FMT, (void *)(uintptr_t)unum, n_closed_over); break; } case MP_BC_MAKE_CLOSURE_DEFARGS: { DECODE_PTR; mp_uint_t n_closed_over = *ip++; - printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void *)(uintptr_t)unum, n_closed_over); + mp_printf(print, "MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void *)(uintptr_t)unum, n_closed_over); break; } case MP_BC_CALL_FUNCTION: DECODE_UINT; - printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); + mp_printf(print, "CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); break; case MP_BC_CALL_FUNCTION_VAR_KW: DECODE_UINT; - printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); + mp_printf(print, "CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); break; case MP_BC_CALL_METHOD: DECODE_UINT; - printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); + mp_printf(print, "CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); break; case MP_BC_CALL_METHOD_VAR_KW: DECODE_UINT; - printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); + mp_printf(print, "CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff); break; case MP_BC_RETURN_VALUE: - printf("RETURN_VALUE"); + mp_printf(print, "RETURN_VALUE"); break; case MP_BC_RAISE_LAST: - printf("RAISE_LAST"); + mp_printf(print, "RAISE_LAST"); break; case MP_BC_RAISE_OBJ: - printf("RAISE_OBJ"); + mp_printf(print, "RAISE_OBJ"); break; case MP_BC_RAISE_FROM: - printf("RAISE_FROM"); + mp_printf(print, "RAISE_FROM"); break; case MP_BC_YIELD_VALUE: - printf("YIELD_VALUE"); + mp_printf(print, "YIELD_VALUE"); break; case MP_BC_YIELD_FROM: - printf("YIELD_FROM"); + mp_printf(print, "YIELD_FROM"); break; case MP_BC_IMPORT_NAME: DECODE_QSTR; - printf("IMPORT_NAME '%s'", qstr_str(qst)); + mp_printf(print, "IMPORT_NAME '%s'", qstr_str(qst)); break; case MP_BC_IMPORT_FROM: DECODE_QSTR; - printf("IMPORT_FROM '%s'", qstr_str(qst)); + mp_printf(print, "IMPORT_FROM '%s'", qstr_str(qst)); break; case MP_BC_IMPORT_STAR: - printf("IMPORT_STAR"); + mp_printf(print, "IMPORT_STAR"); break; default: if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) { - printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16); + mp_printf(print, "LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16); } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) { - printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI); + mp_printf(print, "LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI); } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) { - printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI); + mp_printf(print, "STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI); } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) { - printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI); + mp_printf(print, "UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI); } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) { mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI; - printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op])); + mp_printf(print, "BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op])); } else { - printf("code %p, byte code 0x%02x not implemented\n", ip - 1, ip[-1]); + mp_printf(print, "code %p, byte code 0x%02x not implemented\n", ip - 1, ip[-1]); assert(0); return ip; } @@ -549,13 +546,13 @@ const byte *mp_bytecode_print_str(const byte *ip) { return ip; } -void mp_bytecode_print2(const byte *ip, size_t len, const mp_uint_t *const_table) { +void mp_bytecode_print2(const mp_print_t *print, const byte *ip, size_t len, const mp_uint_t *const_table) { mp_showbc_code_start = ip; mp_showbc_const_table = const_table; while (ip < len + mp_showbc_code_start) { - printf("%02u ", (uint)(ip - mp_showbc_code_start)); - ip = mp_bytecode_print_str(ip); - printf("\n"); + mp_printf(print, "%02u ", (uint)(ip - mp_showbc_code_start)); + ip = mp_bytecode_print_str(print, ip); + mp_printf(print, "\n"); } } diff --git a/py/vm.c b/py/vm.c index f30aed8ff1e36..393a2bbbd6513 100644 --- a/py/vm.c +++ b/py/vm.c @@ -39,7 +39,7 @@ // *FORMAT-OFF* #if 0 -#define TRACE(ip) printf("sp=%d ", (int)(sp - &code_state->state[0] + 1)); mp_bytecode_print2(ip, 1, code_state->fun_bc->const_table); +#define TRACE(ip) printf("sp=%d ", (int)(sp - &code_state->state[0] + 1)); mp_bytecode_print2(&mp_plat_print, ip, 1, code_state->fun_bc->const_table); #else #define TRACE(ip) #endif From acdb0608b7622cc7af0bdbfc89b2696583c948ae Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 11 Sep 2020 23:00:03 +1000 Subject: [PATCH 0284/3533] py/parse: Pass in an mp_print_t to mp_parse_node_print. So the output can be redirected if needed. Signed-off-by: Damien George --- ports/unix/main.c | 2 +- py/parse.c | 36 ++++++++++++++++++------------------ py/parse.h | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index c38b7b0c287a9..0fe492a554cbd 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -133,7 +133,7 @@ STATIC int execute_from_lexer(int source_kind, const void *source, mp_parse_inpu // allow to print the parse tree in the coverage build if (mp_verbose_flag >= 3) { printf("----------------\n"); - mp_parse_node_print(parse_tree.root, 0); + mp_parse_node_print(&mp_plat_print, parse_tree.root, 0); printf("----------------\n"); } #endif diff --git a/py/parse.c b/py/parse.c index b93282165f88a..38cd215a9ff71 100644 --- a/py/parse.c +++ b/py/parse.c @@ -368,35 +368,35 @@ size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_ } #if MICROPY_DEBUG_PRINTERS -void mp_parse_node_print(mp_parse_node_t pn, size_t indent) { +void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t indent) { if (MP_PARSE_NODE_IS_STRUCT(pn)) { - printf("[% 4d] ", (int)((mp_parse_node_struct_t *)pn)->source_line); + mp_printf(print, "[% 4d] ", (int)((mp_parse_node_struct_t *)pn)->source_line); } else { - printf(" "); + mp_printf(print, " "); } for (size_t i = 0; i < indent; i++) { - printf(" "); + mp_printf(print, " "); } if (MP_PARSE_NODE_IS_NULL(pn)) { - printf("NULL\n"); + mp_printf(print, "NULL\n"); } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn); - printf("int(" INT_FMT ")\n", arg); + mp_printf(print, "int(" INT_FMT ")\n", arg); } else if (MP_PARSE_NODE_IS_LEAF(pn)) { uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn); switch (MP_PARSE_NODE_LEAF_KIND(pn)) { case MP_PARSE_NODE_ID: - printf("id(%s)\n", qstr_str(arg)); + mp_printf(print, "id(%s)\n", qstr_str(arg)); break; case MP_PARSE_NODE_STRING: - printf("str(%s)\n", qstr_str(arg)); + mp_printf(print, "str(%s)\n", qstr_str(arg)); break; case MP_PARSE_NODE_BYTES: - printf("bytes(%s)\n", qstr_str(arg)); + mp_printf(print, "bytes(%s)\n", qstr_str(arg)); break; default: assert(MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN); - printf("tok(%u)\n", (uint)arg); + mp_printf(print, "tok(%u)\n", (uint)arg); break; } } else { @@ -404,19 +404,19 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) { mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn; if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) { #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D - printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32)); + mp_printf(print, "literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32)); #else - printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]); + mp_printf(print, "literal const(%p)\n", (mp_obj_t)pns->nodes[0]); #endif } else { size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); #if USE_RULE_NAME - printf("%s(%u) (n=%u)\n", rule_name_table[MP_PARSE_NODE_STRUCT_KIND(pns)], (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); + mp_printf(print, "%s(%u) (n=%u)\n", rule_name_table[MP_PARSE_NODE_STRUCT_KIND(pns)], (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); #else - printf("rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); + mp_printf(print, "rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); #endif for (size_t i = 0; i < n; i++) { - mp_parse_node_print(pns->nodes[i], indent + 2); + mp_parse_node_print(print, pns->nodes[i], indent + 2); } } } @@ -424,10 +424,10 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) { #endif // MICROPY_DEBUG_PRINTERS /* -STATIC void result_stack_show(parser_t *parser) { - printf("result stack, most recent first\n"); +STATIC void result_stack_show(const mp_print_t *print, parser_t *parser) { + mp_printf(print, "result stack, most recent first\n"); for (ssize_t i = parser->result_stack_top - 1; i >= 0; i--) { - mp_parse_node_print(parser->result_stack[i], 0); + mp_parse_node_print(print, parser->result_stack[i], 0); } } */ diff --git a/py/parse.h b/py/parse.h index 4ed8a7ad12324..a6eb380047dc3 100644 --- a/py/parse.h +++ b/py/parse.h @@ -86,7 +86,7 @@ bool mp_parse_node_is_const_false(mp_parse_node_t pn); bool mp_parse_node_is_const_true(mp_parse_node_t pn); bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o); size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes); -void mp_parse_node_print(mp_parse_node_t pn, size_t indent); +void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t indent); typedef enum { MP_PARSE_SINGLE_INPUT, From b31cb21a3936b97af4a52b082e1f40c2061aeab7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 12 Sep 2020 13:47:59 +1000 Subject: [PATCH 0285/3533] stm32/servo: Fix angle and speed methods to work again with -ve args. Fixes a regression introduced by 70affd9ba22e7f62666a9a2fafc2a3c0be9ef95a Fixes issue #6403 Signed-off-by: Damien George --- ports/stm32/servo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/servo.c b/ports/stm32/servo.c index a347c6bbd06d4..17084224637ec 100644 --- a/ports/stm32/servo.c +++ b/ports/stm32/servo.c @@ -278,7 +278,7 @@ STATIC mp_obj_t pyb_servo_angle(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90); } else { #if MICROPY_PY_BUILTINS_FLOAT - self->pulse_dest = self->pulse_centre + (uint16_t)((mp_float_t)self->pulse_angle_90 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(90.0)); + self->pulse_dest = self->pulse_centre + (int16_t)((mp_float_t)self->pulse_angle_90 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(90.0)); #else self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90; #endif @@ -308,7 +308,7 @@ STATIC mp_obj_t pyb_servo_speed(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100); } else { #if MICROPY_PY_BUILTINS_FLOAT - self->pulse_dest = self->pulse_centre + (uint16_t)((mp_float_t)self->pulse_speed_100 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(100.0)); + self->pulse_dest = self->pulse_centre + (int16_t)((mp_float_t)self->pulse_speed_100 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(100.0)); #else self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100; #endif From 504522bd02f9d9f9848d5a6e38427df3d1e4a6e3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 10:53:09 +1000 Subject: [PATCH 0286/3533] extmod/modbluetooth: Fix handling of optional data/uuid args. For the following 3 functions, previously the code relied on whether the arg was passed at all, to make it optional. Now it allows them to be explicitly `None` to indicate they are not used: - gatts_notify(..., [data]) - gattc_discover_services(..., [uuid]) - gattc_discover_characteristics(..., [uuid]) Also ensure that the uuid arguments are actually instances of the uuid type, and fix handling of the 5th arg in gattc_discover_characteristics(). --- extmod/modbluetooth.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index a8a762ce32f24..14d0921c84f4d 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -679,7 +679,7 @@ STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) mp_int_t conn_handle = mp_obj_get_int(args[1]); mp_int_t value_handle = mp_obj_get_int(args[2]); - if (n_args == 4) { + if (n_args == 4 && args[3] != mp_const_none) { mp_buffer_info_t bufinfo = {0}; mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); int err = mp_bluetooth_gatts_notify_send(conn_handle, value_handle, bufinfo.buf, bufinfo.len); @@ -719,7 +719,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_set_buffer_obj, 3 STATIC mp_obj_t bluetooth_ble_gattc_discover_services(size_t n_args, const mp_obj_t *args) { mp_int_t conn_handle = mp_obj_get_int(args[1]); mp_obj_bluetooth_uuid_t *uuid = NULL; - if (n_args == 3) { + if (n_args == 3 && args[2] != mp_const_none) { + if (!mp_obj_is_type(args[2], &bluetooth_uuid_type)) { + mp_raise_TypeError(MP_ERROR_TEXT("UUID")); + } uuid = MP_OBJ_TO_PTR(args[2]); } return bluetooth_handle_errno(mp_bluetooth_gattc_discover_primary_services(conn_handle, uuid)); @@ -731,7 +734,10 @@ STATIC mp_obj_t bluetooth_ble_gattc_discover_characteristics(size_t n_args, cons mp_int_t start_handle = mp_obj_get_int(args[2]); mp_int_t end_handle = mp_obj_get_int(args[3]); mp_obj_bluetooth_uuid_t *uuid = NULL; - if (n_args == 3) { + if (n_args == 5 && args[4] != mp_const_none) { + if (!mp_obj_is_type(args[4], &bluetooth_uuid_type)) { + mp_raise_TypeError(MP_ERROR_TEXT("UUID")); + } uuid = MP_OBJ_TO_PTR(args[4]); } return bluetooth_handle_errno(mp_bluetooth_gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid)); From 6a6a5f9e151473bdcc1d14725d680691ff665a82 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 11:43:53 +1000 Subject: [PATCH 0287/3533] extmod/modbluetooth: Make BLE.irq() method positional only. Simplifcation now that the trigger arg has been removed. --- extmod/modbluetooth.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 14d0921c84f4d..64cd383525665 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -384,27 +384,21 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_config_obj, 1, bluetooth_ble_config); -STATIC mp_obj_t bluetooth_ble_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_handler }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_handler, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mp_obj_t callback = args[ARG_handler].u_obj; - if (callback != mp_const_none && !mp_obj_is_callable(callback)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid callback")); +STATIC mp_obj_t bluetooth_ble_irq(mp_obj_t self_in, mp_obj_t handler_in) { + (void)self_in; + if (handler_in != mp_const_none && !mp_obj_is_callable(handler_in)) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid handler")); } // Update the callback. MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - o->irq_handler = callback; + o->irq_handler = handler_in; MICROPY_PY_BLUETOOTH_EXIT return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_irq_obj, 1, bluetooth_ble_irq); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_irq_obj, bluetooth_ble_irq); // ---------------------------------------------------------------------------- // Bluetooth object: GAP From 19faf550904c4e93427450994b7babe367eb9add Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 11:45:09 +1000 Subject: [PATCH 0288/3533] docs/library/ubluetooth.rst: Clarify position/kw arguments. Almost all ubluetooth functions are positional-only, but some have optional args. Make this clearer and show what the defaults are. --- docs/library/ubluetooth.rst | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index f8dad7b8d1ed2..5635f4d69a763 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -28,15 +28,15 @@ Constructor Configuration ------------- -.. method:: BLE.active([active]) +.. method:: BLE.active([active], /) Optionally changes the active state of the BLE radio, and returns the current state. The radio must be made active before using any other methods on this class. -.. method:: BLE.config('param') - BLE.config(param=value, ...) +.. method:: BLE.config('param', /) + BLE.config(*, param=value, ...) Get or set configuration values of the BLE interface. To get a value the parameter name should be quoted as a string, and just one parameter is @@ -76,7 +76,7 @@ Configuration Event Handling -------------- -.. method:: BLE.irq(handler) +.. method:: BLE.irq(handler, /) Registers a callback for events from the BLE stack. The *handler* takes two arguments, ``event`` (which will be one of the codes below) and ``data`` @@ -195,7 +195,7 @@ program. Broadcaster Role (Advertiser) ----------------------------- -.. method:: BLE.gap_advertise(interval_us, adv_data=None, resp_data=None, connectable=True) +.. method:: BLE.gap_advertise(interval_us, adv_data=None, *, resp_data=None, connectable=True) Starts advertising at the specified interval (in **micro**\ seconds). This interval will be rounded down to the nearest 625us. To stop advertising, set @@ -214,7 +214,7 @@ Broadcaster Role (Advertiser) Observer Role (Scanner) ----------------------- -.. method:: BLE.gap_scan(duration_ms, [interval_us], [window_us], [active]) +.. method:: BLE.gap_scan(duration_ms, interval_us=1280000, window_us=11250, active=False, /) Run a scan operation lasting for the specified duration (in **milli**\ seconds). @@ -244,7 +244,7 @@ Observer Role (Scanner) * 0x04 - SCAN_RSP - scan response ``active`` can be set ``True`` if you want to receive scan responses in the results. - + When scanning is stopped (either due to the duration finishing or when explicitly stopped), the ``_IRQ_SCAN_DONE`` event will be raised. @@ -268,7 +268,7 @@ writes from a central to a given characteristic, use :meth:`gatts_write` after registration. e.g. ``gatts_write(char_handle, bytes(100))``. -.. method:: BLE.gatts_register_services(services_definition) +.. method:: BLE.gatts_register_services(services_definition, /) Configures the peripheral with the specified services, replacing any existing services. @@ -308,26 +308,26 @@ writes from a central to a given characteristic, use **Note:** Advertising must be stopped before registering services. -.. method:: BLE.gatts_read(value_handle) +.. method:: BLE.gatts_read(value_handle, /) Reads the local value for this handle (which has either been written by :meth:`gatts_write ` or by a remote central). -.. method:: BLE.gatts_write(value_handle, data) +.. method:: BLE.gatts_write(value_handle, data, /) Writes the local value for this handle, which can be read by a central. -.. method:: BLE.gatts_notify(conn_handle, value_handle, [data]) +.. method:: BLE.gatts_notify(conn_handle, value_handle, data=None, /) Sends a notification request to a connected central. - If *data* is specified, then that value is sent to the central as part of + If *data* is not ``None``, then that value is sent to the central as part of the notification. The local value will not be modified. - Otherwise, if *data* is not specified, then the current local value (as + Otherwise, if *data* is ``None``, then the current local value (as set with :meth:`gatts_write `) will be sent. -.. method:: BLE.gatts_indicate(conn_handle, value_handle) +.. method:: BLE.gatts_indicate(conn_handle, value_handle, /) Sends an indication request to a connected central. @@ -361,7 +361,7 @@ Central Role (GATT Client) On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised. -.. method:: BLE.gap_disconnect(conn_handle) +.. method:: BLE.gap_disconnect(conn_handle, /) Disconnect the specified connection handle. @@ -370,7 +370,7 @@ Central Role (GATT Client) Returns ``False`` if the connection handle wasn't connected, and ``True`` otherwise. -.. method:: BLE.gattc_discover_services(conn_handle, [uuid]) +.. method:: BLE.gattc_discover_services(conn_handle, uuid=None, /) Query a connected peripheral for its services. @@ -379,7 +379,7 @@ Central Role (GATT Client) For each service discovered, the ``_IRQ_GATTC_SERVICE_RESULT`` event will be raised, followed by ``_IRQ_GATTC_SERVICE_DONE`` on completion. -.. method:: BLE.gattc_discover_characteristics(conn_handle, start_handle, end_handle, [uuid]) +.. method:: BLE.gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid=None, /) Query a connected peripheral for characteristics in the specified range. @@ -392,14 +392,14 @@ Central Role (GATT Client) For each characteristic discovered, the ``_IRQ_GATTC_CHARACTERISTIC_RESULT`` event will be raised, followed by ``_IRQ_GATTC_CHARACTERISTIC_DONE`` on completion. -.. method:: BLE.gattc_discover_descriptors(conn_handle, start_handle, end_handle) +.. method:: BLE.gattc_discover_descriptors(conn_handle, start_handle, end_handle, /) Query a connected peripheral for descriptors in the specified range. For each descriptor discovered, the ``_IRQ_GATTC_DESCRIPTOR_RESULT`` event will be raised, followed by ``_IRQ_GATTC_DESCRIPTOR_DONE`` on completion. -.. method:: BLE.gattc_read(conn_handle, value_handle) +.. method:: BLE.gattc_read(conn_handle, value_handle, /) Issue a remote read to a connected peripheral for the specified characteristic or descriptor handle. @@ -433,7 +433,7 @@ class UUID Constructor ----------- -.. class:: UUID(value) +.. class:: UUID(value, /) Creates a UUID instance with the specified **value**. From 5be3bfcb7e065bd95eb8460d2069524b934bcd0f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 15 Sep 2020 11:38:58 +1000 Subject: [PATCH 0289/3533] extmod/modbluetooth: Print UUIDs correctly. In particular, the printed string can now be re-evaluated to construct the same UUID object. --- extmod/modbluetooth.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 64cd383525665..f77c7eb63cf22 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -181,7 +181,7 @@ STATIC void bluetooth_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_p (void)kind; mp_obj_bluetooth_uuid_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "UUID%u(%s", self->type * 8, self->type <= 4 ? "0x" : "'"); + mp_printf(print, "UUID(%s", self->type <= 4 ? "0x" : "'"); for (int i = 0; i < self->type; ++i) { if (i == 4 || i == 6 || i == 8 || i == 10) { mp_printf(print, "-"); From c200759290bae8ceb74c91d29c0a521292d925f4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 12:26:09 +1000 Subject: [PATCH 0290/3533] docs/library/ubluetooth.rst: Clarify peripheral/central vs server/client Previously this documentation assumed peripheral=server and central=client, but this is not accurate or true to the implementation. --- docs/library/ubluetooth.rst | 130 +++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 48 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 5635f4d69a763..e4471f8054a82 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -6,8 +6,8 @@ This module provides an interface to a Bluetooth controller on a board. Currently this supports Bluetooth Low Energy (BLE) in Central, Peripheral, -Broadcaster, and Observer roles, and a device may operate in multiple -roles concurrently. +Broadcaster, and Observer roles, as well as GATT Server and Client. A device +may operate in multiple roles concurrently. This API is intended to match the low-level Bluetooth protocol and provide building-blocks for higher-level abstractions such as specific device types. @@ -70,8 +70,7 @@ Configuration incoming events. This buffer is global to the entire BLE driver and so handles incoming data for all events, including all characteristics. Increasing this allows better handling of bursty incoming data (for - example scan results) and the ability for a central role to receive - larger characteristic values. + example scan results) and the ability to receive larger characteristic values. Event Handling -------------- @@ -99,10 +98,10 @@ Event Handling # A central has disconnected from this peripheral. conn_handle, addr_type, addr = data elif event == _IRQ_GATTS_WRITE: - # A central has written to this characteristic or descriptor. + # A client has written to this characteristic or descriptor. conn_handle, attr_handle = data elif event == _IRQ_GATTS_READ_REQUEST: - # A central has issued a read. Note: this is a hard IRQ. + # A client has issued a read. Note: this is a hard IRQ. # Return None to deny the read. # Note: This event is not supported on ESP32. conn_handle, attr_handle = data @@ -153,13 +152,13 @@ Event Handling # Note: Status will be zero on success, implementation-specific value otherwise. conn_handle, value_handle, status = data elif event == _IRQ_GATTC_NOTIFY: - # A peripheral has sent a notify request. + # A server has sent a notify request. conn_handle, value_handle, notify_data = data elif event == _IRQ_GATTC_INDICATE: - # A peripheral has sent an indicate request. + # A server has sent an indicate request. conn_handle, value_handle, notify_data = data elif event == _IRQ_GATTS_INDICATE_DONE: - # A central has acknowledged the indication. + # A client has acknowledged the indication. # Note: Status will be zero on successful acknowledgment, implementation-specific value otherwise. conn_handle, value_handle, status = data @@ -249,28 +248,74 @@ Observer Role (Scanner) explicitly stopped), the ``_IRQ_SCAN_DONE`` event will be raised. -Peripheral Role (GATT Server) ------------------------------ +Central Role +------------ + +A central device can connect to peripherals that it has discovered using the observer role (see :meth:`gap_scan`) or with a known address. + +.. method:: BLE.gap_connect(addr_type, addr, scan_duration_ms=2000, /) + + Connect to a peripheral. + + See :meth:`gap_scan ` for details about address types. + + On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised. + + +Peripheral Role +--------------- + +A peripheral device is expected to send connectable advertisements (see +:meth:`gap_advertise`). It will usually be acting as a GATT +server, having first registered services and characteristics using +:meth:`gatts_register_services`. + +When a central connects, the ``_IRQ_CENTRAL_CONNECT`` event will be raised. + + +Central & Peripheral Roles +-------------------------- + +.. method:: BLE.gap_disconnect(conn_handle, /) + + Disconnect the specified connection handle. This can either be a + central that has connected to this device (if acting as a peripheral) + or a peripheral that was previously connected to by this device (if acting + as a central). + + On success, the ``_IRQ_PERIPHERAL_DISCONNECT`` or ``_IRQ_CENTRAL_DISCONNECT`` + event will be raised. + + Returns ``False`` if the connection handle wasn't connected, and ``True`` + otherwise. + -A BLE peripheral has a set of registered services. Each service may contain +GATT Server +----------- + +A GATT server has a set of registered services. Each service may contain characteristics, which each have a value. Characteristics can also contain descriptors, which themselves have values. These values are stored locally, and are accessed by their "value handle" which is generated during service registration. They can also be read from or written -to by a remote central device. Additionally, a peripheral can "notify" a -characteristic to a connected central via a connection handle. +to by a remote client device. Additionally, a server can "notify" a +characteristic to a connected client via a connection handle. + +A device in either central or peripheral roles may function as a GATT server, +however in most cases it will be more common for a peripheral device to act +as the server. Characteristics and descriptors have a default maximum size of 20 bytes. -Anything written to them by a central will be truncated to this length. However, +Anything written to them by a client will be truncated to this length. However, any local write will increase the maximum size, so if you want to allow larger -writes from a central to a given characteristic, use +writes from a client to a given characteristic, use :meth:`gatts_write` after registration. e.g. ``gatts_write(char_handle, bytes(100))``. .. method:: BLE.gatts_register_services(services_definition, /) - Configures the peripheral with the specified services, replacing any + Configures the server with the specified services, replacing any existing services. *services_definition* is a list of **services**, where each **service** is a @@ -311,17 +356,17 @@ writes from a central to a given characteristic, use .. method:: BLE.gatts_read(value_handle, /) Reads the local value for this handle (which has either been written by - :meth:`gatts_write ` or by a remote central). + :meth:`gatts_write ` or by a remote client). .. method:: BLE.gatts_write(value_handle, data, /) - Writes the local value for this handle, which can be read by a central. + Writes the local value for this handle, which can be read by a client. .. method:: BLE.gatts_notify(conn_handle, value_handle, data=None, /) - Sends a notification request to a connected central. + Sends a notification request to a connected client. - If *data* is not ``None``, then that value is sent to the central as part of + If *data* is not ``None``, then that value is sent to the client as part of the notification. The local value will not be modified. Otherwise, if *data* is ``None``, then the current local value (as @@ -329,7 +374,7 @@ writes from a central to a given characteristic, use .. method:: BLE.gatts_indicate(conn_handle, value_handle, /) - Sends an indication request to a connected central. + Sends an indication request to a connected client. **Note:** This does not currently support sending a custom value, it will always send the current local value (as set with :meth:`gatts_write @@ -349,30 +394,19 @@ writes from a central to a given characteristic, use be cleared after reading. This feature is useful when implementing something like the Nordic UART Service. +GATT Client +----------- -Central Role (GATT Client) --------------------------- - -.. method:: BLE.gap_connect(addr_type, addr, scan_duration_ms=2000, /) - - Connect to a peripheral. - - See :meth:`gatts_write ` for details about address types. - - On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised. - -.. method:: BLE.gap_disconnect(conn_handle, /) - - Disconnect the specified connection handle. - - On success, the ``_IRQ_PERIPHERAL_DISCONNECT`` event will be raised. +A GATT client can discover and read/write characteristics on a remote GATT server. - Returns ``False`` if the connection handle wasn't connected, and ``True`` - otherwise. +It is more common for a central role device to act as the GATT client, however +it's also possible for a peripheral to act as a client in order to discover +information about the central that has connected to it (e.g. to read the +device name from the device information service). .. method:: BLE.gattc_discover_services(conn_handle, uuid=None, /) - Query a connected peripheral for its services. + Query a connected server for its services. Optionally specify a service *uuid* to query for that service only. @@ -381,7 +415,7 @@ Central Role (GATT Client) .. method:: BLE.gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid=None, /) - Query a connected peripheral for characteristics in the specified range. + Query a connected server for characteristics in the specified range. Optionally specify a characteristic *uuid* to query for that characteristic only. @@ -394,14 +428,14 @@ Central Role (GATT Client) .. method:: BLE.gattc_discover_descriptors(conn_handle, start_handle, end_handle, /) - Query a connected peripheral for descriptors in the specified range. + Query a connected server for descriptors in the specified range. For each descriptor discovered, the ``_IRQ_GATTC_DESCRIPTOR_RESULT`` event will be raised, followed by ``_IRQ_GATTC_DESCRIPTOR_DONE`` on completion. .. method:: BLE.gattc_read(conn_handle, value_handle, /) - Issue a remote read to a connected peripheral for the specified + Issue a remote read to a connected server for the specified characteristic or descriptor handle. When a value is available, the ``_IRQ_GATTC_READ_RESULT`` event will be @@ -409,20 +443,20 @@ Central Role (GATT Client) .. method:: BLE.gattc_write(conn_handle, value_handle, data, mode=0, /) - Issue a remote write to a connected peripheral for the specified + Issue a remote write to a connected server for the specified characteristic or descriptor handle. The argument *mode* specifies the write behaviour, with the currently supported values being: * ``mode=0`` (default) is a write-without-response: the write will - be sent to the remote peripheral but no confirmation will be + be sent to the remote server but no confirmation will be returned, and no event will be raised. - * ``mode=1`` is a write-with-response: the remote peripheral is + * ``mode=1`` is a write-with-response: the remote server is requested to send a response/acknowledgement that it received the data. - If a response is received from the remote peripheral the + If a response is received from the remote server the ``_IRQ_GATTC_WRITE_DONE`` event will be raised. From fe642ced43809f932eaa8e3396c8bd0f8265de09 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 18 Sep 2020 12:50:05 +1000 Subject: [PATCH 0291/3533] tests/multi_bluetooth: Update UUID format in .exp files. --- tests/multi_bluetooth/ble_characteristic.py.exp | 2 +- tests/multi_bluetooth/ble_gap_device_name.py.exp | 2 +- tests/multi_bluetooth/ble_gatt_data_transfer.py.exp | 6 +++--- tests/multi_bluetooth/ble_gattc_discover_services.py.exp | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp index 25b5544efc459..90dc46c06fd57 100644 --- a/tests/multi_bluetooth/ble_characteristic.py.exp +++ b/tests/multi_bluetooth/ble_characteristic.py.exp @@ -14,7 +14,7 @@ _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect _IRQ_PERIPHERAL_CONNECT -_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') gattc_read _IRQ_GATTC_READ_RESULT b'periph0' _IRQ_GATTC_READ_DONE 0 diff --git a/tests/multi_bluetooth/ble_gap_device_name.py.exp b/tests/multi_bluetooth/ble_gap_device_name.py.exp index 377df90dee356..0f4ee6e3dc576 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py.exp +++ b/tests/multi_bluetooth/ble_gap_device_name.py.exp @@ -11,7 +11,7 @@ _IRQ_CENTRAL_DISCONNECT gap_connect _IRQ_PERIPHERAL_CONNECT gattc_discover_characteristics -_IRQ_GATTC_CHARACTERISTIC_RESULT UUID16(0x2a00) +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID(0x2a00) gattc_read _IRQ_GATTC_READ_RESULT b'GAP_NAME0' gap_disconnect: True diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp b/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp index a1b3cbcd07374..94a0718108c1e 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py.exp @@ -11,9 +11,9 @@ _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect _IRQ_PERIPHERAL_CONNECT -_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000002-1111-2222-3333-444444444444') -_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000003-1111-2222-3333-444444444444') -_IRQ_GATTC_CHARACTERISTIC_RESULT UUID128('00000004-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000002-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000003-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000004-1111-2222-3333-444444444444') _IRQ_GATTC_CHARACTERISTIC_DONE gattc_write gattc_write diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py.exp b/tests/multi_bluetooth/ble_gattc_discover_services.py.exp index 06c36b249368a..7f4d8da81ad8c 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py.exp +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py.exp @@ -5,7 +5,7 @@ _IRQ_CENTRAL_DISCONNECT --- instance1 --- gap_connect _IRQ_PERIPHERAL_CONNECT -_IRQ_GATTC_SERVICE_RESULT UUID16(0x180d) -_IRQ_GATTC_SERVICE_RESULT UUID128('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a') +_IRQ_GATTC_SERVICE_RESULT UUID(0x180d) +_IRQ_GATTC_SERVICE_RESULT UUID('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a') gap_disconnect: True _IRQ_PERIPHERAL_DISCONNECT From f271b96b5c8d6a4abedc81459a1c5710eb187abb Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 13:24:12 +1000 Subject: [PATCH 0292/3533] docs/library/ubluetooth.rst: Add docs for MTU API. --- docs/library/ubluetooth.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index e4471f8054a82..03d03583a1cce 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -72,6 +72,13 @@ Configuration Increasing this allows better handling of bursty incoming data (for example scan results) and the ability to receive larger characteristic values. + - ``'mtu'``: Get/set the MTU that will be used during an MTU exchange. The + resulting MTU will be the minimum of this and the remote device's MTU. + MTU exchange will not happen automatically (unless the remote device initiates + it), and must be manually initiated with + :meth:`gattc_exchange_mtu`. + Use the ``_IRQ_MTU_EXCHANGED`` event to discover the MTU for a given connection. + Event Handling -------------- @@ -161,6 +168,9 @@ Event Handling # A client has acknowledged the indication. # Note: Status will be zero on successful acknowledgment, implementation-specific value otherwise. conn_handle, value_handle, status = data + elif event == _IRQ_MTU_EXCHANGED: + # MTU exchange complete (either initiated by us or the remote device). + conn_handle, mtu = data The event codes are:: @@ -185,6 +195,7 @@ The event codes are:: _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) _IRQ_GATTS_INDICATE_DONE = const(20) + _IRQ_MTU_EXCHANGED = const(21) In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your @@ -459,6 +470,18 @@ device name from the device information service). If a response is received from the remote server the ``_IRQ_GATTC_WRITE_DONE`` event will be raised. +.. method:: BLE.gattc_exchange_mtu(conn_handle, /) + + Initiate MTU exchange with a connected server, using the preferred MTU + set using ``BLE.config(mtu=value)``. + + The ``_IRQ_MTU_EXCHANGED`` event will be raised when MTU exchange + completes. + + **Note:** MTU exchange is typically initiated by the central. When using + the BlueKitchen stack in the central role, it does not support a remote + peripheral initiating the MTU exchange. NimBLE works for both roles. + class UUID ---------- From 857e2c8fd54bc1d5ec3d3c0a38e4ac989e91413a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 11 Sep 2020 18:12:36 +1000 Subject: [PATCH 0293/3533] extmod/modbluetooth: Implement MTU. --- extmod/btstack/modbluetooth_btstack.c | 36 ++++++++++++++++++++-- extmod/modbluetooth.c | 28 +++++++++++++++++ extmod/modbluetooth.h | 12 ++++++++ extmod/nimble/modbluetooth_nimble.c | 44 +++++++++++++++++++++++++-- 4 files changed, 116 insertions(+), 4 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index cde802a61f5f4..ae96035868877 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -264,6 +264,11 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet); uint8_t status = att_event_handle_value_indication_complete_get_status(packet); mp_bluetooth_gatts_on_indicate_complete(conn_handle, value_handle, status); + } else if (event_type == ATT_EVENT_MTU_EXCHANGE_COMPLETE) { + // This is triggered in peripheral mode, when exchange initiated by us or remote. + uint16_t conn_handle = att_event_mtu_exchange_complete_get_handle(packet); + uint16_t mtu = att_event_mtu_exchange_complete_get_MTU(packet); + mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu); } else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { // Ignore, duplicated by att_server.c. } else { @@ -339,8 +344,6 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_printf(" --> btstack # conns changed\n"); } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) { DEBUG_printf(" --> hci vendor specific\n"); - } else if (event_type == GATT_EVENT_MTU) { - DEBUG_printf(" --> hci MTU\n"); } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { DEBUG_printf(" --> hci disconnect complete\n"); uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet); @@ -621,6 +624,9 @@ int mp_bluetooth_init(void) { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE gatt_client_init(); + + // We always require explicitly exchanging MTU with ble.gattc_exchange_mtu(). + gatt_client_mtu_enable_auto_negotiation(false); #endif // Register for HCI events. @@ -1042,6 +1048,24 @@ int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, len, append); } +int mp_bluetooth_get_preferred_mtu(void) { + if (!mp_bluetooth_is_active()) { + mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE); + } + return l2cap_max_le_mtu(); +} + +int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { + if (!mp_bluetooth_is_active()) { + return ERRNO_BLUETOOTH_NOT_ACTIVE; + } + l2cap_set_max_le_mtu(mtu); + if (l2cap_max_le_mtu() != mtu) { + return MP_EINVAL; + } + return 0; +} + int mp_bluetooth_gap_disconnect(uint16_t conn_handle) { DEBUG_printf("mp_bluetooth_gap_disconnect\n"); gap_disconnect(conn_handle); @@ -1204,6 +1228,14 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const return btstack_error_to_errno(err); } + +int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { + DEBUG_printf("mp_bluetooth_exchange_mtu: conn_handle=%d mtu=%d\n", conn_handle, l2cap_max_le_mtu()); + + gatt_client_send_mtu_negotiation(&btstack_packet_handler_att_server, conn_handle); + + return 0; +} #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index f77c7eb63cf22..daf9cd0d1984b 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -316,6 +316,8 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map } case MP_QSTR_rxbuf: return mp_obj_new_int(self->ringbuf.size); + case MP_QSTR_mtu: + return mp_obj_new_int(mp_bluetooth_get_preferred_mtu()); default: mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); } @@ -368,6 +370,11 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map m_del(uint8_t, old_irq_data_buf, old_irq_data_alloc); break; } + case MP_QSTR_mtu: { + mp_int_t mtu = mp_obj_get_int(e->value); + bluetooth_handle_errno(mp_bluetooth_set_preferred_mtu(mtu)); + break; + } case MP_QSTR_addr_mode: { mp_int_t addr_mode = mp_obj_get_int(e->value); mp_bluetooth_set_address_mode(addr_mode); @@ -770,6 +777,13 @@ STATIC mp_obj_t bluetooth_ble_gattc_write(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_write_obj, 4, 5, bluetooth_ble_gattc_write); +STATIC mp_obj_t bluetooth_ble_gattc_exchange_mtu(mp_obj_t self_in, mp_obj_t conn_handle_in) { + (void)self_in; + uint16_t conn_handle = mp_obj_get_int(conn_handle_in); + return bluetooth_handle_errno(mp_bluetooth_gattc_exchange_mtu(conn_handle)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_exchange_mtu_obj, bluetooth_ble_gattc_exchange_mtu); + #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // ---------------------------------------------------------------------------- @@ -802,6 +816,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gattc_discover_descriptors), MP_ROM_PTR(&bluetooth_ble_gattc_discover_descriptors_obj) }, { MP_ROM_QSTR(MP_QSTR_gattc_read), MP_ROM_PTR(&bluetooth_ble_gattc_read_obj) }, { MP_ROM_QSTR(MP_QSTR_gattc_write), MP_ROM_PTR(&bluetooth_ble_gattc_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_gattc_exchange_mtu), MP_ROM_PTR(&bluetooth_ble_gattc_exchange_mtu_obj) }, #endif }; STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table); @@ -911,6 +926,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE) { // conn_handle, value_handle, status ringbuf_extract(&o->ringbuf, data_tuple, 2, 1, NULL, 0, NULL, NULL); + } else if (event == MP_BLUETOOTH_IRQ_MTU_EXCHANGED) { + // conn_handle, mtu + ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE } else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) { // addr_type, addr, adv_type, rssi, adv_data @@ -1184,6 +1202,16 @@ bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_han } #endif +void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { + MICROPY_PY_BLUETOOTH_ENTER + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (enqueue_irq(o, 2 + 2, MP_BLUETOOTH_IRQ_MTU_EXCHANGED)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, value); + } + schedule_ringbuf(atomic_state); +} + void mp_bluetooth_gatts_db_create_entry(mp_gatts_db_t db, uint16_t handle, size_t len) { mp_map_elem_t *elem = mp_map_lookup(db, MP_OBJ_NEW_SMALL_INT(handle), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); mp_bluetooth_gatts_db_entry_t *entry = m_new(mp_bluetooth_gatts_db_entry_t, 1); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 23ff97bc61806..2df4c3c25f17b 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -100,6 +100,7 @@ #define MP_BLUETOOTH_IRQ_GATTC_NOTIFY (18) #define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19) #define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20) +#define MP_BLUETOOTH_IRQ_MTU_EXCHANGED (21) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -131,6 +132,7 @@ _IRQ_GATTC_WRITE_DONE = const(17) _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) _IRQ_GATTS_INDICATE_DONE = const(20) +_IRQ_MTU_EXCHANGED = const(21) */ // Common UUID type. @@ -212,6 +214,10 @@ int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append // Disconnect from a central or peripheral. int mp_bluetooth_gap_disconnect(uint16_t conn_handle); +// Set/get the MTU that we will respond to a MTU exchange with. +int mp_bluetooth_get_preferred_mtu(void); +int mp_bluetooth_set_preferred_mtu(uint16_t mtu); + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Start a discovery (scan). Set duration to zero to run continuously. int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan); @@ -236,6 +242,9 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle); // Write the value to the remote peripheral. int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode); + +// Initiate MTU exchange for a specific connection using the preferred MTU. +int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle); #endif ///////////////////////////////////////////////////////////////////////////// @@ -255,6 +264,9 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle); #endif +// Call this when an MTU exchange completes. +void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value); + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Notify modbluetooth that scan has finished, either timeout, manually, or by some other action (e.g. connecting). void mp_bluetooth_gap_on_scan_complete(void); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 9ca90b602476f..c0d6d64cfc1f0 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -288,9 +288,17 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { // Map "done/ack" to 0, otherwise pass the status directly. mp_bluetooth_gatts_on_indicate_complete(event->notify_tx.conn_handle, event->notify_tx.attr_handle, event->notify_tx.status == BLE_HS_EDONE ? 0 : event->notify_tx.status); } + break; } - } + case BLE_GAP_EVENT_MTU: { + if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { + DEBUG_printf("gap_event_cb: mtu update: conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); + mp_bluetooth_gatts_on_mtu_exchanged(event->mtu.conn_handle, event->mtu.value); + } + break; + } + } return 0; } @@ -637,7 +645,7 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { return 0; } -int mp_bluetooth_gatts_register_service_end() { +int mp_bluetooth_gatts_register_service_end(void) { int ret = ble_gatts_start(); if (ret != 0) { return ble_hs_err_to_errno(ret); @@ -769,6 +777,23 @@ int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle, len, append); } +int mp_bluetooth_get_preferred_mtu(void) { + if (!mp_bluetooth_is_active()) { + mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE); + } + return ble_att_preferred_mtu(); +} + +int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { + if (!mp_bluetooth_is_active()) { + return ERRNO_BLUETOOTH_NOT_ACTIVE; + } + if (ble_att_set_preferred_mtu(mtu)) { + return MP_EINVAL; + } + return 0; +} + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { @@ -884,6 +909,14 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { // TODO break; + case BLE_GAP_EVENT_MTU: { + if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { + DEBUG_printf("peripheral_gap_event_cb: mtu update: conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); + mp_bluetooth_gatts_on_mtu_exchanged(event->mtu.conn_handle, event->mtu.value); + } + break; + } + default: break; } @@ -1042,6 +1075,13 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const return ble_hs_err_to_errno(err); } +int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { + DEBUG_printf("mp_bluetooth_exchange_mtu: conn_handle=%d mtu=%d\n", conn_handle, ble_att_preferred_mtu()); + + // Using NULL callback (we'll get notified in gap_event_cb instead). + return ble_hs_err_to_errno(ble_gattc_exchange_mtu(conn_handle, NULL, NULL)); +} + #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE From 06dda48144be94ff6bef92b5bbf3fa87dea32cfa Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 14 Sep 2020 18:11:19 +1000 Subject: [PATCH 0294/3533] tests/run-multitests.py: Show test/truth diff. --- tests/run-multitests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index 7ab4e85c57d79..ad032df38b238 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -7,6 +7,7 @@ import sys, os, time, re, select import argparse import subprocess +import tempfile sys.path.append("../tools") import pyboard @@ -18,6 +19,9 @@ CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython") +# For diff'ing test output +DIFF = os.getenv("MICROPY_DIFF", "diff -u") + PYTHON_TRUTH = CPYTHON3 INSTANCE_READ_TIMEOUT_S = 10 @@ -323,6 +327,18 @@ def run_test_on_instances(test_file, num_instances, instances): return error, skip, output_str +def print_diff(a, b): + a_fd, a_path = tempfile.mkstemp(text=True) + b_fd, b_path = tempfile.mkstemp(text=True) + os.write(a_fd, a.encode()) + os.write(b_fd, b.encode()) + os.close(a_fd) + os.close(b_fd) + subprocess.run(DIFF.split(" ") + [a_path, b_path]) + os.unlink(a_path) + os.unlink(b_path) + + def run_tests(test_files, instances_truth, instances_test): skipped_tests = [] passed_tests = [] @@ -372,6 +388,8 @@ def run_tests(test_files, instances_truth, instances_test): print(output_test, end="") print("### TRUTH ###") print(output_truth, end="") + print("### DIFF ###") + print_diff(output_test, output_truth) if cmd_args.show_output: print() From 3086d35e1637ba2bdc05ae676034d029ddbf302f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 14 Sep 2020 18:11:46 +1000 Subject: [PATCH 0295/3533] tests/multi_bluetooth/ble_mtu.py: Add multitest for BLE MTU. --- tests/multi_bluetooth/ble_mtu.py | 208 +++++++++++++++++++++++++++ tests/multi_bluetooth/ble_mtu.py.exp | 143 ++++++++++++++++++ 2 files changed, 351 insertions(+) create mode 100644 tests/multi_bluetooth/ble_mtu.py create mode 100644 tests/multi_bluetooth/ble_mtu.py.exp diff --git a/tests/multi_bluetooth/ble_mtu.py b/tests/multi_bluetooth/ble_mtu.py new file mode 100644 index 0000000000000..ef3199c5bbb04 --- /dev/null +++ b/tests/multi_bluetooth/ble_mtu.py @@ -0,0 +1,208 @@ +# Test MTU exchange (initiated by both central and peripheral) and the effect on +# notify and write size. + +# Seven connections are made (four central->peripheral, three peripheral->central). +# +# Test | Requested | Preferred | Result | Notes +# 0 | 300 (C) | 256 (P) | 256 | +# 1 | 300 (C) | 200 (P) | 200 | +# 2 | 300 (C) | 400 (P) | 300 | +# 3 | 300 (C) | 50 (P) | 50 | Shorter than 64 so the notification is truncated. +# 4 | 290 (P) | 256 (C) | 256 | +# 5 | 290 (P) | 190 (C) | 190 | +# 6 | 290 (P) | 350 (C) | 290 | +# +# For each connection a notification is sent by the server (peripheral) and a characteristic +# is written by the client (central) to ensure that the expected size is transmitted. +# +# Note: This currently fails on btstack for two reasons: +# - btstack doesn't truncate writes to the MTU (it fails instead) +# - btstack (in central mode) doesn't handle the peripheral initiating the MTU exchange + +from micropython import const +import time, machine, bluetooth + +TIMEOUT_MS = 5000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_GATTC_NOTIFY = const(18) +_IRQ_MTU_EXCHANGED = const(21) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = ( + CHAR_UUID, + bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY, +) +SERVICE = ( + SERVICE_UUID, + (CHAR,), +) +SERVICES = (SERVICE,) + +waiting_events = {} + + +def irq(event, data): + global value_handle + if event == _IRQ_CENTRAL_CONNECT: + print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_CENTRAL_DISCONNECT: + print("_IRQ_CENTRAL_DISCONNECT") + elif event == _IRQ_GATTS_WRITE: + print("_IRQ_GATTS_WRITE") + elif event == _IRQ_PERIPHERAL_CONNECT: + print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_DISCONNECT: + print("_IRQ_PERIPHERAL_DISCONNECT") + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + if data[-1] == CHAR_UUID: + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) + waiting_events[event] = data[2] + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") + elif event == _IRQ_GATTC_WRITE_DONE: + print("_IRQ_GATTC_WRITE_DONE") + elif event == _IRQ_GATTC_NOTIFY: + print("_IRQ_GATTC_NOTIFY", len(data[-1]), chr(data[-1][0])) + elif event == _IRQ_MTU_EXCHANGED: + print("_IRQ_MTU_EXCHANGED", data[-1]) + waiting_events[event] = data[-1] + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return True + machine.idle() + return False + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services(SERVICES) + ble.gatts_set_buffer(char_handle, 500, False) + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + for i in range(7): + waiting_events.clear() + + if i == 1: + ble.config(mtu=200) + elif i == 2: + ble.config(mtu=400) + elif i == 3: + ble.config(mtu=50) + elif i >= 4: + ble.config(mtu=290) + else: + # This is the NimBLE default. + ble.config(mtu=256) + + # Wait for central to connect to us. + if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): + return + conn_handle = waiting_events[_IRQ_CENTRAL_CONNECT] + + if i >= 4: + print("gattc_exchange_mtu") + ble.gattc_exchange_mtu(conn_handle) + + if not wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS): + return + mtu = waiting_events[_IRQ_MTU_EXCHANGED] + + print("gatts_notify") + ble.gatts_notify(conn_handle, char_handle, str(i) * 64) + + if not wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS): + return + + print("gatts_read") + data = ble.gatts_read(char_handle) + print("characteristic len:", len(data), chr(data[0])) + + # Wait for the central to disconnect. + if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): + return + + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + multitest.next() + try: + for i in range(7): + waiting_events.clear() + + if i < 4: + ble.config(mtu=300) + elif i == 5: + ble.config(mtu=190) + elif i == 6: + ble.config(mtu=350) + else: + ble.config(mtu=256) + + # Connect to peripheral and then disconnect. + print("gap_connect") + ble.gap_connect(*BDADDR) + if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): + return + conn_handle = waiting_events[_IRQ_PERIPHERAL_CONNECT] + + if i < 4: + print("gattc_exchange_mtu") + ble.gattc_exchange_mtu(conn_handle) + + if not wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS): + return + mtu = waiting_events[_IRQ_MTU_EXCHANGED] + + if not wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS): + return + + print("gattc_discover_characteristics") + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + if not wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS): + return + value_handle = waiting_events[_IRQ_GATTC_CHARACTERISTIC_RESULT] + + # Write 20 more than the MTU to test truncation. + print("gattc_write") + ble.gattc_write(conn_handle, value_handle, chr(ord("a") + i) * (mtu + 20), 1) + if not wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS): + return + + # Disconnect from peripheral. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): + return + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/ble_mtu.py.exp b/tests/multi_bluetooth/ble_mtu.py.exp new file mode 100644 index 0000000000000..1039a5da13c22 --- /dev/null +++ b/tests/multi_bluetooth/ble_mtu.py.exp @@ -0,0 +1,143 @@ +--- instance0 --- +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_MTU_EXCHANGED 256 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 253 a +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_MTU_EXCHANGED 200 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 197 b +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_MTU_EXCHANGED 300 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 297 c +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_MTU_EXCHANGED 50 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 47 d +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 256 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 253 e +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 190 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 187 f +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 290 +gatts_notify +_IRQ_GATTS_WRITE +gatts_read +characteristic len: 287 g +_IRQ_CENTRAL_DISCONNECT +gap_advertise +--- instance1 --- +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 256 +_IRQ_GATTC_NOTIFY 64 0 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 200 +_IRQ_GATTC_NOTIFY 64 1 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 300 +_IRQ_GATTC_NOTIFY 64 2 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_exchange_mtu +_IRQ_MTU_EXCHANGED 50 +_IRQ_GATTC_NOTIFY 47 3 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +_IRQ_MTU_EXCHANGED 256 +_IRQ_GATTC_NOTIFY 64 4 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +_IRQ_MTU_EXCHANGED 190 +_IRQ_GATTC_NOTIFY 64 5 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +_IRQ_MTU_EXCHANGED 290 +_IRQ_GATTC_NOTIFY 64 6 +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_write +_IRQ_GATTC_WRITE_DONE +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT From 5c503de52166f6ebd4ef59b58b130a730782823d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Sep 2020 16:34:09 +1000 Subject: [PATCH 0296/3533] travis: Install setuptools for black code formatting. Signed-off-by: Damien George --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ebf9b55899dc9..45b67dc9fedc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,7 @@ jobs: install: - sudo apt-get install uncrustify python3-pip - uncrustify --version + - pip3 install --user setuptools - pip3 install --user black - black --version script: From 52d6eeb409e0d4ff3a1fd6811f75b4953912eb38 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 10 Sep 2020 11:13:13 +1000 Subject: [PATCH 0297/3533] esp32/boards/sdkconfig.base: Set default IDF log level to ERROR. This commit changes the default logging level on all esp32 boards to ERROR. The esp32 port is now stable enough that it makes sense to remove the info logs to make the output cleaner, and to match other ports. More verbose logging can always be reenabled via esp.osdebug(). This also fixes issue #6354, error messages from NimBLE: the problem is that ble.active(True) will cause the IDF's NimBLE port to reset the "NimBLE" tag back to the default level (which was INFO prior to this commit). Even if the user had previously called esp.osdebug(None), because the IDF is setting the "NimBLE" tag back to the default (INFO), the messages will continue to be shown. The one quirk is that if the user does want to see the additional logging, then they must call esp.osdebug(0, 3) after ble.active(True) to undo the IDF setting the level back to the default (now ERROR). This means that it's impossible (via Python/esp.osdebug) to see stack-startup logging, you'd have to recompile with the default level changed back to INFO. --- ports/esp32/boards/sdkconfig.base | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index f44ec4e173e9b..67e2424a1246d 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -11,6 +11,11 @@ CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR=y # Bootloader config CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y +# Change default log level to "ERROR" (instead of "INFO") +CONFIG_LOG_DEFAULT_LEVEL_INFO=n +CONFIG_LOG_DEFAULT_LEVEL_ERROR=y +CONFIG_LOG_DEFAULT_LEVEL=1 + # ESP32-specific CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n From ecb36d243915958e118a2c1d6b4bef8b490f0bc0 Mon Sep 17 00:00:00 2001 From: Mirko Vogt Date: Wed, 16 Sep 2020 23:57:40 +0000 Subject: [PATCH 0298/3533] esp32/modnetwork: Re-enable PPP support for IDF-SDK >=v4. PPP support was disabled in 96008ff59a8af9883af17d01b951029d9d02eec9 - marked as "unsupported" due to an early IDF v4 release. With the currently supported IDF v4.x version - 4c81978a - it appears to be working just fine. --- ports/esp32/modnetwork.c | 2 +- ports/esp32/network_ppp.c | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 325b27f74b262..029a8d143127c 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -755,8 +755,8 @@ STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&get_wlan_obj) }, #if !MICROPY_ESP_IDF_4 { MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&get_lan_obj) }, - { MP_ROM_QSTR(MP_QSTR_PPP), MP_ROM_PTR(&ppp_make_new_obj) }, #endif + { MP_ROM_QSTR(MP_QSTR_PPP), MP_ROM_PTR(&ppp_make_new_obj) }, { MP_ROM_QSTR(MP_QSTR_phy_mode), MP_ROM_PTR(&esp_phy_mode_obj) }, #if MODNETWORK_INCLUDE_CONSTANTS diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index c3046c9fc77b9..df57b817251d8 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -26,7 +26,6 @@ * THE SOFTWARE. */ -#if !MICROPY_ESP_IDF_4 #include "py/runtime.h" #include "py/mphal.h" #include "py/objtype.h" @@ -284,5 +283,3 @@ const mp_obj_type_t ppp_if_type = { .name = MP_QSTR_PPP, .locals_dict = (mp_obj_dict_t *)&ppp_if_locals_dict, }; - -#endif // !MICROPY_ESP_IDF_4 From b28758054b586fe41f56e462856d89f28c5e3f6a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 11 Sep 2020 16:47:29 +1000 Subject: [PATCH 0299/3533] esp8266: Remove release-specific manifest, disable osdebug by default. This commit removes release-specific builds for the esp8266 and makes the normal build of the GENERIC board more like the release build. This makes esp8266 like all the other ports, for which there is no difference between a daily build and a release build, making things less confusing. Release builds were previously defined by UART_OS=-1 (disable OS messages) and using manifest_release.py to include more frozen modules. The changes in this commit are: - Remove manifest_release.py. - Add existing modules from manifest_release.py (except example code) to the GENERIC board's manifest.py file. - Change UART_OS default to -1 to disable OS messages by default. Signed-off-by: Damien George --- ports/esp8266/Makefile | 2 +- ports/esp8266/boards/GENERIC/manifest.py | 19 +++++++++++++++++++ ports/esp8266/boards/manifest_release.py | 23 ----------------------- 3 files changed, 20 insertions(+), 24 deletions(-) delete mode 100644 ports/esp8266/boards/manifest_release.py diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 38b91a545dea3..02ea05f76e064 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -50,7 +50,7 @@ INC += -I$(ESP_SDK)/include # UART for "os" messages. 0 is normal UART as used by MicroPython REPL, # 1 is debug UART (tx only), -1 to disable. -UART_OS = 0 +UART_OS = -1 CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ -D__ets__ -DICACHE_FLASH \ diff --git a/ports/esp8266/boards/GENERIC/manifest.py b/ports/esp8266/boards/GENERIC/manifest.py index 4e65b256f92c8..46f3b837be07f 100644 --- a/ports/esp8266/boards/GENERIC/manifest.py +++ b/ports/esp8266/boards/GENERIC/manifest.py @@ -1,2 +1,21 @@ +# base modules include("$(PORT_DIR)/boards/manifest.py") + +# uasyncio include("$(MPY_DIR)/extmod/uasyncio/manifest.py") + +# drivers +freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") + +# Libraries from micropython-lib, include only if the library directory exists +if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): + # file utilities + freeze("$(MPY_LIB_DIR)/upysh", "upysh.py") + + # requests + freeze("$(MPY_LIB_DIR)/urequests", "urequests.py") + freeze("$(MPY_LIB_DIR)/urllib.urequest", "urllib/urequest.py") + + # umqtt + freeze("$(MPY_LIB_DIR)/umqtt.simple", "umqtt/simple.py") + freeze("$(MPY_LIB_DIR)/umqtt.robust", "umqtt/robust.py") diff --git a/ports/esp8266/boards/manifest_release.py b/ports/esp8266/boards/manifest_release.py deleted file mode 100644 index 5a3194ae9bd45..0000000000000 --- a/ports/esp8266/boards/manifest_release.py +++ /dev/null @@ -1,23 +0,0 @@ -include("manifest.py") - -# drivers -freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") - -# file utilities -freeze("$(MPY_LIB_DIR)/upysh", "upysh.py") - -# requests -freeze("$(MPY_LIB_DIR)/urequests", "urequests.py") -freeze("$(MPY_LIB_DIR)/urllib.urequest", "urllib/urequest.py") - -# umqtt with examples -freeze("$(MPY_LIB_DIR)/umqtt.simple", "umqtt/simple.py") -freeze("$(MPY_LIB_DIR)/umqtt.robust", "umqtt/robust.py") -freeze("$(MPY_LIB_DIR)/umqtt.simple", "example_pub_button.py") -freeze("$(MPY_LIB_DIR)/umqtt.simple", "example_sub_led.py") - -# HTTP examples -freeze("$(MPY_DIR)/examples/network", "http_client.py") -freeze("$(MPY_DIR)/examples/network", "http_client_ssl.py") -freeze("$(MPY_DIR)/examples/network", "http_server.py") -freeze("$(MPY_DIR)/examples/network", "http_server_ssl.py") From bd7af6151d605d3fc8f70cb9ddf45b2fd7881f08 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 14 Sep 2020 00:07:12 +1000 Subject: [PATCH 0300/3533] ports: Add utime.gmtime() function. To portably get the Epoch. This is simply aliased to localtime() on ports that are not timezone aware. Signed-off-by: Damien George --- docs/library/utime.rst | 14 ++++++++++---- ports/cc3200/mods/modutime.c | 1 + ports/esp32/modutime.c | 1 + ports/esp8266/modules/ntptime.py | 5 ++--- ports/esp8266/modutime.c | 1 + ports/stm32/modutime.c | 1 + ports/unix/modtime.c | 14 ++++++++++++-- 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/docs/library/utime.rst b/docs/library/utime.rst index 7fe83f5abe0cd..8c222ede5f80b 100644 --- a/docs/library/utime.rst +++ b/docs/library/utime.rst @@ -36,11 +36,17 @@ behave not as expected. Functions --------- -.. function:: localtime([secs]) +.. function:: gmtime([secs]) + localtime([secs]) - Convert a time expressed in seconds since the Epoch (see above) into an 8-tuple which - contains: (year, month, mday, hour, minute, second, weekday, yearday) - If secs is not provided or None, then the current time from the RTC is used. + Convert the time *secs* expressed in seconds since the Epoch (see above) into an + 8-tuple which contains: ``(year, month, mday, hour, minute, second, weekday, yearday)`` + If *secs* is not provided or None, then the current time from the RTC is used. + + The `gmtime()` function returns a date-time tuple in UTC, and `localtime()` returns a + date-time tuple in local time. + + The format of the entries in the 8-tuple are: * year includes the century (for example 2014). * month is 1-12 diff --git a/ports/cc3200/mods/modutime.c b/ports/cc3200/mods/modutime.c index a729d62f9cbb2..e77065ef450db 100644 --- a/ports/cc3200/mods/modutime.c +++ b/ports/cc3200/mods/modutime.c @@ -133,6 +133,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, diff --git a/ports/esp32/modutime.c b/ports/esp32/modutime.c index 0325bd469391d..1f93ce6c30c36 100644 --- a/ports/esp32/modutime.c +++ b/ports/esp32/modutime.c @@ -85,6 +85,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, diff --git a/ports/esp8266/modules/ntptime.py b/ports/esp8266/modules/ntptime.py index 92ae6ab077cfb..dd07e46f1d3b4 100644 --- a/ports/esp8266/modules/ntptime.py +++ b/ports/esp8266/modules/ntptime.py @@ -29,12 +29,11 @@ def time(): return val - NTP_DELTA -# There's currently no timezone support in MicroPython, so -# utime.localtime() will return UTC time (as if it was .gmtime()) +# There's currently no timezone support in MicroPython, and the RTC is set in UTC time. def settime(): t = time() import machine import utime - tm = utime.localtime(t) + tm = utime.gmtime(t) machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) diff --git a/ports/esp8266/modutime.c b/ports/esp8266/modutime.c index 9e924121bbc78..a951f8f8d2ccc 100644 --- a/ports/esp8266/modutime.c +++ b/ports/esp8266/modutime.c @@ -108,6 +108,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, diff --git a/ports/stm32/modutime.c b/ports/stm32/modutime.c index 77ec7468c99b6..9641ddb4733bf 100644 --- a/ports/stm32/modutime.c +++ b/ports/stm32/modutime.c @@ -132,6 +132,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index 84c83da268627..5343e479c7d92 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -132,7 +132,7 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep); -STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t mod_time_gm_local_time(size_t n_args, const mp_obj_t *args, struct tm *(*time_func)(const time_t *timep)) { time_t t; if (n_args == 0) { t = time(NULL); @@ -144,7 +144,7 @@ STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) { t = mp_obj_get_int(args[0]); #endif } - struct tm *tm = localtime(&t); + struct tm *tm = time_func(&t); mp_obj_t ret = mp_obj_new_tuple(9, NULL); @@ -165,6 +165,15 @@ STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) { return ret; } + +STATIC mp_obj_t mod_time_gmtime(size_t n_args, const mp_obj_t *args) { + return mod_time_gm_local_time(n_args, args, gmtime); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_gmtime_obj, 0, 1, mod_time_gmtime); + +STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) { + return mod_time_gm_local_time(n_args, args, localtime); +} STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_localtime_obj, 0, 1, mod_time_localtime); STATIC mp_obj_t mod_time_mktime(mp_obj_t tuple) { @@ -210,6 +219,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) }, }; From 8f20cdc353a9d1c1e5d43484b58e1eaf81ec52e0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 14 Sep 2020 12:15:03 +1000 Subject: [PATCH 0301/3533] all: Rename absolute time-based functions to include "epoch". For time-based functions that work with absolute time there is the need for an Epoch, to set the zero-point at which the absolute time starts counting. Such functions include time.time() and filesystem stat return values. And different ports may use a different Epoch. To make it clearer what functions use the Epoch (whatever it may be), and make the ports more consistent with their use of the Epoch, this commit renames all Epoch related functions to include the word "epoch" in their name (and remove references to "2000"). Along with this rename, the following things have changed: - mp_hal_time_ns() is now specified to return the number of nanoseconds since the Epoch, rather than since 1970 (but since this is an internal function it doesn't change anything for the user). - littlefs timestamps on the esp8266 have been fixed (they were previously off by 30 years in nanoseconds). Otherwise, there is no functional change made by this commit. Signed-off-by: Damien George --- extmod/vfs_fat.c | 5 +---- extmod/vfs_lfs.c | 4 +++- extmod/vfs_lfsx.c | 6 ++---- lib/timeutils/timeutils.h | 43 +++++++++++++++++++++++++++---------- ports/esp32/fatfs_port.c | 2 +- ports/esp32/machine_rtc.c | 4 ++-- ports/esp32/modutime.c | 2 +- ports/esp32/mphalport.c | 3 +-- ports/esp8266/esp_mphal.c | 2 +- ports/esp8266/fatfs_port.c | 4 ++-- ports/esp8266/machine_rtc.c | 18 ++++++++-------- ports/esp8266/modmachine.c | 2 +- ports/esp8266/modmachine.h | 4 ++-- ports/esp8266/modutime.c | 8 +++---- ports/stm32/modutime.c | 4 ++-- ports/stm32/rtc.c | 4 ++-- py/mphal.h | 2 +- 17 files changed, 67 insertions(+), 50 deletions(-) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index ace5ba5b6547f..95b7ad9944116 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -311,7 +311,7 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { } else { mode |= MP_S_IFREG; } - mp_int_t seconds = timeutils_seconds_since_2000( + mp_int_t seconds = timeutils_seconds_since_epoch( 1980 + ((fno.fdate >> 9) & 0x7f), (fno.fdate >> 5) & 0x0f, fno.fdate & 0x1f, @@ -319,9 +319,6 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { (fno.ftime >> 5) & 0x3f, 2 * (fno.ftime & 0x1f) ); - #if MICROPY_EPOCH_IS_1970 - seconds += TIMEUTILS_SECONDS_1970_TO_2000; - #endif t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev diff --git a/extmod/vfs_lfs.c b/extmod/vfs_lfs.c index a53f66f2d63c8..9cf3eb11083cf 100644 --- a/extmod/vfs_lfs.c +++ b/extmod/vfs_lfs.c @@ -26,6 +26,7 @@ #include "py/runtime.h" #include "py/mphal.h" +#include "lib/timeutils/timeutils.h" #include "extmod/vfs.h" #include "extmod/vfs_lfs.h" @@ -126,7 +127,8 @@ const char *mp_vfs_lfs2_make_path(mp_obj_vfs_lfs2_t *self, mp_obj_t path_in); mp_obj_t mp_vfs_lfs2_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in); STATIC void lfs_get_mtime(uint8_t buf[8]) { - uint64_t ns = mp_hal_time_ns(); + // On-disk storage of timestamps uses 1970 as the Epoch, so convert from host's Epoch. + uint64_t ns = timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(mp_hal_time_ns()); // Store "ns" to "buf" in little-endian format (essentially htole64). for (size_t i = 0; i < 8; ++i) { buf[i] = ns; diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index d00df53104f6f..35d5f03c59a2c 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -365,10 +365,8 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) { for (size_t i = sizeof(mtime_buf); i > 0; --i) { ns = ns << 8 | mtime_buf[i - 1]; } - mtime = timeutils_seconds_since_2000_from_nanoseconds_since_1970(ns); - #if MICROPY_EPOCH_IS_1970 - mtime += TIMEUTILS_SECONDS_1970_TO_2000; - #endif + // On-disk storage of timestamps uses 1970 as the Epoch, so convert to host's Epoch. + mtime = timeutils_seconds_since_epoch_from_nanoseconds_since_1970(ns); } #endif diff --git a/lib/timeutils/timeutils.h b/lib/timeutils/timeutils.h index 08b0dc2e8559a..14da831dc8b58 100644 --- a/lib/timeutils/timeutils.h +++ b/lib/timeutils/timeutils.h @@ -42,14 +42,6 @@ typedef struct _timeutils_struct_time_t { uint16_t tm_yday; // 1..366 } timeutils_struct_time_t; -static inline uint64_t timeutils_seconds_since_2000_to_nanoseconds_since_1970(mp_uint_t s) { - return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL; -} - -static inline mp_uint_t timeutils_seconds_since_2000_from_nanoseconds_since_1970(uint64_t ns) { - return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000; -} - bool timeutils_is_leap_year(mp_uint_t year); mp_uint_t timeutils_days_in_month(mp_uint_t year, mp_uint_t month); mp_uint_t timeutils_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date); @@ -63,10 +55,39 @@ mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds); -static inline uint64_t timeutils_nanoseconds_since_1970(mp_uint_t year, mp_uint_t month, +// Select the Epoch used by the port. +#if MICROPY_EPOCH_IS_1970 + +static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) { - return timeutils_seconds_since_2000_to_nanoseconds_since_1970( - timeutils_seconds_since_2000(year, month, date, hour, minute, second)); + return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000; +} + +static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) { + return ns / 1000000000ULL; +} + +static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns) { + return ns; } +#else // Epoch is 2000 + +#define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time +#define timeutils_seconds_since_epoch timeutils_seconds_since_2000 + +static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) { + return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL; +} + +static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) { + return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000; +} + +static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns) { + return ns + TIMEUTILS_SECONDS_1970_TO_2000 * 1000000000ULL; +} + +#endif + #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H diff --git a/ports/esp32/fatfs_port.c b/ports/esp32/fatfs_port.c index 779ba1c88accc..cfc52853d08a9 100644 --- a/ports/esp32/fatfs_port.c +++ b/ports/esp32/fatfs_port.c @@ -34,7 +34,7 @@ DWORD get_fattime(void) { struct timeval tv; gettimeofday(&tv, NULL); timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, &tm); + timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm); return ((DWORD)(tm.tm_year - 1980) << 25) | ((DWORD)tm.tm_mon << 21) | ((DWORD)tm.tm_mday << 16) | ((DWORD)tm.tm_hour << 11) | ((DWORD)tm.tm_min << 5) | ((DWORD)tm.tm_sec >> 1); diff --git a/ports/esp32/machine_rtc.c b/ports/esp32/machine_rtc.c index c776eaa8d3d1c..6902ce85fd2c7 100644 --- a/ports/esp32/machine_rtc.c +++ b/ports/esp32/machine_rtc.c @@ -93,7 +93,7 @@ STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *ar gettimeofday(&tv, NULL); timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, &tm); + timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm); mp_obj_t tuple[8] = { mp_obj_new_int(tm.tm_year), @@ -114,7 +114,7 @@ STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *ar mp_obj_get_array_fixed_n(args[1], 8, &items); struct timeval tv = {0}; - tv.tv_sec = timeutils_seconds_since_2000(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6])); + tv.tv_sec = timeutils_seconds_since_epoch(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6])); tv.tv_usec = mp_obj_get_int(items[7]); settimeofday(&tv, NULL); diff --git a/ports/esp32/modutime.c b/ports/esp32/modutime.c index 1f93ce6c30c36..ac3edb4e9b76f 100644 --- a/ports/esp32/modutime.c +++ b/ports/esp32/modutime.c @@ -44,7 +44,7 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { } else { seconds = mp_obj_get_int(args[0]); } - timeutils_seconds_since_2000_to_struct_time(seconds, &tm); + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); mp_obj_t tuple[8] = { tuple[0] = mp_obj_new_int(tm.tm_year), tuple[1] = mp_obj_new_int(tm.tm_mon), diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 3a46edf1f75c1..ad571bf961886 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -200,8 +200,7 @@ void mp_hal_delay_us(uint32_t us) { uint64_t mp_hal_time_ns(void) { struct timeval tv; gettimeofday(&tv, NULL); - // gettimeofday returns seconds since 2000/1/1 - uint64_t ns = timeutils_seconds_since_2000_to_nanoseconds_since_1970(tv.tv_sec); + uint64_t ns = tv.tv_sec * 1000000000ULL; ns += (uint64_t)tv.tv_usec * 1000ULL; return ns; } diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 9e1b72e438ad7..54f9611e56233 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -139,7 +139,7 @@ void MP_FASTCODE(mp_hal_delay_ms)(uint32_t delay) { } uint64_t mp_hal_time_ns(void) { - return pyb_rtc_get_us_since_2000() * 1000ULL; + return pyb_rtc_get_us_since_epoch() * 1000ULL; } void ets_event_poll(void) { diff --git a/ports/esp8266/fatfs_port.c b/ports/esp8266/fatfs_port.c index 8cef0acec3c62..bbd10519359a0 100644 --- a/ports/esp8266/fatfs_port.c +++ b/ports/esp8266/fatfs_port.c @@ -33,10 +33,10 @@ DWORD get_fattime(void) { // TODO: Optimize division (there's no HW division support on ESP8266, // so it's expensive). - uint32_t secs = (uint32_t)(pyb_rtc_get_us_since_2000() / 1000000); + uint32_t secs = (uint32_t)(pyb_rtc_get_us_since_epoch() / 1000000); timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(secs, &tm); + timeutils_seconds_since_epoch_to_struct_time(secs, &tm); return ((DWORD)(tm.tm_year - 1980) << 25) | ((DWORD)tm.tm_mon << 21) | ((DWORD)tm.tm_mday << 16) | ((DWORD)tm.tm_hour << 11) | ((DWORD)tm.tm_min << 5) | ((DWORD)tm.tm_sec >> 1); diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index 1aa73f9b317f8..e7b750fd92c8d 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -84,7 +84,7 @@ STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_ return (mp_obj_t)&pyb_rtc_obj; } -void pyb_rtc_set_us_since_2000(uint64_t nowus) { +void pyb_rtc_set_us_since_epoch(uint64_t nowus) { uint32_t cal = system_rtc_clock_cali_proc(); // Save RTC ticks for overflow detection. rtc_last_ticks = system_get_rtc_time(); @@ -96,7 +96,7 @@ void pyb_rtc_set_us_since_2000(uint64_t nowus) { system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta)); }; -uint64_t pyb_rtc_get_us_since_2000() { +uint64_t pyb_rtc_get_us_since_epoch() { uint32_t cal; int64_t delta; uint32_t rtc_ticks; @@ -120,17 +120,17 @@ uint64_t pyb_rtc_get_us_since_2000() { void rtc_prepare_deepsleep(uint64_t sleep_us) { // RTC time will reset at wake up. Let's be preared for this. - int64_t delta = pyb_rtc_get_us_since_2000() + sleep_us; + int64_t delta = pyb_rtc_get_us_since_epoch() + sleep_us; system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta)); } STATIC mp_obj_t pyb_rtc_datetime(size_t n_args, const mp_obj_t *args) { if (n_args == 1) { // Get time - uint64_t msecs = pyb_rtc_get_us_since_2000() / 1000; + uint64_t msecs = pyb_rtc_get_us_since_epoch() / 1000; timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(msecs / 1000, &tm); + timeutils_seconds_since_epoch_to_struct_time(msecs / 1000, &tm); mp_obj_t tuple[8] = { mp_obj_new_int(tm.tm_year), @@ -149,8 +149,8 @@ STATIC mp_obj_t pyb_rtc_datetime(size_t n_args, const mp_obj_t *args) { mp_obj_t *items; mp_obj_get_array_fixed_n(args[1], 8, &items); - pyb_rtc_set_us_since_2000( - ((uint64_t)timeutils_seconds_since_2000( + pyb_rtc_set_us_since_epoch( + ((uint64_t)timeutils_seconds_since_epoch( mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), @@ -209,7 +209,7 @@ STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time } // set expiry time (in microseconds) - pyb_rtc_alarm0_expiry = pyb_rtc_get_us_since_2000() + (uint64_t)mp_obj_get_int(time_in) * 1000; + pyb_rtc_alarm0_expiry = pyb_rtc_get_us_since_epoch() + (uint64_t)mp_obj_get_int(time_in) * 1000; return mp_const_none; @@ -222,7 +222,7 @@ STATIC mp_obj_t pyb_rtc_alarm_left(size_t n_args, const mp_obj_t *args) { mp_raise_ValueError(MP_ERROR_TEXT("invalid alarm")); } - uint64_t now = pyb_rtc_get_us_since_2000(); + uint64_t now = pyb_rtc_get_us_since_epoch(); if (pyb_rtc_alarm0_expiry <= now) { return MP_OBJ_NEW_SMALL_INT(0); } else { diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index bc838b420645d..cb28d5b144827 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -132,7 +132,7 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { // see if RTC.ALARM0 should wake the device if (pyb_rtc_alarm0_wake & MACHINE_WAKE_DEEPSLEEP) { - uint64_t t = pyb_rtc_get_us_since_2000(); + uint64_t t = pyb_rtc_get_us_since_epoch(); if (pyb_rtc_alarm0_expiry <= t) { sleep_us = 1; // alarm already expired so wake immediately } else { diff --git a/ports/esp8266/modmachine.h b/ports/esp8266/modmachine.h index f5cfc0fa2cd52..be4debd335a64 100644 --- a/ports/esp8266/modmachine.h +++ b/ports/esp8266/modmachine.h @@ -32,8 +32,8 @@ void pin_set(uint pin, int value); extern uint32_t pyb_rtc_alarm0_wake; extern uint64_t pyb_rtc_alarm0_expiry; -void pyb_rtc_set_us_since_2000(uint64_t nowus); -uint64_t pyb_rtc_get_us_since_2000(); +void pyb_rtc_set_us_since_epoch(uint64_t nowus); +uint64_t pyb_rtc_get_us_since_epoch(); void rtc_prepare_deepsleep(uint64_t sleep_us); #endif // MICROPY_INCLUDED_ESP8266_MODMACHINE_H diff --git a/ports/esp8266/modutime.c b/ports/esp8266/modutime.c index a951f8f8d2ccc..86534722c792f 100644 --- a/ports/esp8266/modutime.c +++ b/ports/esp8266/modutime.c @@ -58,11 +58,11 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { timeutils_struct_time_t tm; mp_int_t seconds; if (n_args == 0 || args[0] == mp_const_none) { - seconds = pyb_rtc_get_us_since_2000() / 1000 / 1000; + seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000; } else { seconds = mp_obj_get_int(args[0]); } - timeutils_seconds_since_2000_to_struct_time(seconds, &tm); + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); mp_obj_t tuple[8] = { tuple[0] = mp_obj_new_int(tm.tm_year), tuple[1] = mp_obj_new_int(tm.tm_mon), @@ -98,10 +98,10 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) { MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); /// \function time() -/// Returns the number of seconds, as an integer, since 1/1/2000. +/// Returns the number of seconds, as an integer, since the Epoch. STATIC mp_obj_t time_time(void) { // get date and time - return mp_obj_new_int(pyb_rtc_get_us_since_2000() / 1000 / 1000); + return mp_obj_new_int(pyb_rtc_get_us_since_epoch() / 1000 / 1000); } MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); diff --git a/ports/stm32/modutime.c b/ports/stm32/modutime.c index 9641ddb4733bf..2a37a130df2bb 100644 --- a/ports/stm32/modutime.c +++ b/ports/stm32/modutime.c @@ -76,7 +76,7 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { } else { mp_int_t seconds = mp_obj_get_int(args[0]); timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(seconds, &tm); + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); mp_obj_t tuple[8] = { tuple[0] = mp_obj_new_int(tm.tm_year), tuple[1] = mp_obj_new_int(tm.tm_mon), @@ -125,7 +125,7 @@ STATIC mp_obj_t time_time(void) { RTC_TimeTypeDef time; HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); - return mp_obj_new_int(timeutils_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds)); + return mp_obj_new_int(timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds)); } MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index 4f759c4bc74f0..18ecf7750513b 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -452,8 +452,8 @@ uint64_t mp_hal_time_ns(void) { RTC_DateTypeDef date; HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); - ns = timeutils_nanoseconds_since_1970( - 2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds); + ns = timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds); + ns *= 1000000000ULL; uint32_t usec = ((RTC_SYNCH_PREDIV - time.SubSeconds) * (1000000 / 64)) / ((RTC_SYNCH_PREDIV + 1) / 64); ns += usec * 1000; #endif diff --git a/py/mphal.h b/py/mphal.h index 6d11f6ddc0836..0d4b1224e5cec 100644 --- a/py/mphal.h +++ b/py/mphal.h @@ -76,7 +76,7 @@ mp_uint_t mp_hal_ticks_cpu(void); #endif #ifndef mp_hal_time_ns -// Nanoseconds since 1970/1/1. +// Nanoseconds since the Epoch. uint64_t mp_hal_time_ns(void); #endif From 9d1983f078150b0f1da7bfb2e55c0ac823c328b6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 16 Sep 2020 12:11:26 +1000 Subject: [PATCH 0302/3533] py/dynruntime.h: Add mp_import_* and mp_load/store_*. These functions already exist in the fun table, and this commit just adds convenience macros for them. --- py/dynruntime.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/py/dynruntime.h b/py/dynruntime.h index 696746f7510ff..eb8301284e1fc 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -163,9 +163,15 @@ static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) { /******************************************************************************/ // General runtime functions -#define mp_load_name(qst) (mp_fun_table.load_name(qst)) -#define mp_load_global(qst) (mp_fun_table.load_global(qst)) -#define mp_store_global(qst, obj) (mp_fun_table.store_global((qst), (obj))) +#define mp_load_name(qst) (mp_fun_table.load_name((qst))) +#define mp_load_global(qst) (mp_fun_table.load_global((qst))) +#define mp_load_attr(base, attr) (mp_fun_table.load_attr((base), (attr))) +#define mp_load_method(base, attr, dest) (mp_fun_table.load_method((base), (attr), (dest))) +#define mp_load_super_method(attr, dest) (mp_fun_table.load_super_method((attr), (dest))) +#define mp_store_name(qst, obj) (mp_fun_table.store_name((qst), (obj))) +#define mp_store_global(qst, obj) (mp_fun_table.store_global((qst), (obj))) +#define mp_store_attr(base, attr, val) (mp_fun_table.store_attr((base), (attr), (val))) + #define mp_unary_op(op, obj) (mp_fun_table.unary_op((op), (obj))) #define mp_binary_op(op, lhs, rhs) (mp_fun_table.binary_op((op), (lhs), (rhs))) @@ -193,6 +199,13 @@ static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) { #define MP_DYNRUNTIME_MAKE_FUNCTION(f) \ (mp_make_function_from_raw_code((rc.fun_data = (f), &rc), MP_OBJ_NULL, MP_OBJ_NULL)) +#define mp_import_name(name, fromlist, level) \ + (mp_fun_table.import_name((name), (fromlist), (level))) +#define mp_import_from(module, name) \ + (mp_fun_table.import_from((module), (name))) +#define mp_import_all(module) \ + (mp_fun_table.import_all((module)) + /******************************************************************************/ // Exceptions From 8af9796b1642189f590520062b1a00e965d5985e Mon Sep 17 00:00:00 2001 From: Tweako Date: Wed, 16 Sep 2020 10:29:33 +0200 Subject: [PATCH 0303/3533] stm32/led: Support PWM output without TIM3. For example, the STM32WB55 doesn't have TIM3 but can still drive LEDs using PWM on other timers. --- ports/stm32/led.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/led.c b/ports/stm32/led.c index 923171884629f..adcb240cc0cc6 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -141,9 +141,11 @@ STATIC void led_pwm_init(int led) { case 2: __TIM2_CLK_ENABLE(); break; + #if defined(TIM3) case 3: __TIM3_CLK_ENABLE(); break; + #endif default: assert(0); } From c410a86814abde8ce05aaf46383f38ef9f981a8c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Sep 2020 13:26:24 +1000 Subject: [PATCH 0304/3533] tests/basics: Enable == and != special-method tests now that they work. These work since 3aab54bf434e7f025a91ea05052f1bac439fad8c Signed-off-by: Damien George --- tests/basics/special_methods.py | 4 ++++ tests/basics/special_methods2.py | 9 --------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/basics/special_methods.py b/tests/basics/special_methods.py index b56bc1c9c479d..d8af8e07979f2 100644 --- a/tests/basics/special_methods.py +++ b/tests/basics/special_methods.py @@ -100,6 +100,10 @@ def __int__(self): cud2 = Cud() str(cud1) +cud1 == cud1 +cud1 == cud2 +cud1 != cud1 +cud1 != cud2 cud1 < cud2 cud1 <= cud2 cud1 == cud2 diff --git a/tests/basics/special_methods2.py b/tests/basics/special_methods2.py index 09e43fff29973..31f330ab42b9d 100644 --- a/tests/basics/special_methods2.py +++ b/tests/basics/special_methods2.py @@ -129,12 +129,3 @@ def __dir__(self): # test that dir() does not delegate to __dir__ for the type print('a' in dir(Cud)) - -# TODO: the following operations are not supported on every ports -# -# ne is not supported, !(eq) is called instead -#cud1 != cud2 -# -# in the following test, cpython still calls __eq__ -# cud3=cud1 -# cud3==cud1 From 42342fa3cb30e2eac56ceb1d21b4eb60a0f406f3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Sep 2020 13:37:31 +1000 Subject: [PATCH 0305/3533] tests/basics: Add test for MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS ops. And enable this feature on unix, the coverage variant. The .exp test file is needed so the test can run on CPython versions prior to "@=" operator support. Signed-off-by: Damien George --- .../unix/variants/coverage/mpconfigvariant.h | 1 + tests/basics/class_inplace_op2.py | 73 +++++++++++++++++++ tests/basics/class_inplace_op2.py.exp | 12 +++ 3 files changed, 86 insertions(+) create mode 100644 tests/basics/class_inplace_op2.py create mode 100644 tests/basics/class_inplace_op2.py.exp diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 802c2fe5f7203..3de5294322502 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -39,6 +39,7 @@ #define MICROPY_WARNINGS_CATEGORY (1) #define MICROPY_MODULE_GETATTR (1) #define MICROPY_PY_DELATTR_SETATTR (1) +#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (1) #define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE (1) #define MICROPY_PY_BUILTINS_NEXT2 (1) diff --git a/tests/basics/class_inplace_op2.py b/tests/basics/class_inplace_op2.py new file mode 100644 index 0000000000000..0e3f2add412c2 --- /dev/null +++ b/tests/basics/class_inplace_op2.py @@ -0,0 +1,73 @@ +# Test inplace special methods enabled by MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS + + +class A: + def __imul__(self, other): + print("__imul__") + return self + + def __imatmul__(self, other): + print("__imatmul__") + return self + + def __ifloordiv__(self, other): + print("__ifloordiv__") + return self + + def __itruediv__(self, other): + print("__itruediv__") + return self + + def __imod__(self, other): + print("__imod__") + return self + + def __ipow__(self, other): + print("__ipow__") + return self + + def __ior__(self, other): + print("__ior__") + return self + + def __ixor__(self, other): + print("__ixor__") + return self + + def __iand__(self, other): + print("__iand__") + return self + + def __ilshift__(self, other): + print("__ilshift__") + return self + + def __irshift__(self, other): + print("__irshift__") + return self + + +a = A() + +try: + a *= None +except TypeError: + print("SKIP") + raise SystemExit + +a @= None +a //= None +a /= None +a %= None +a **= None +a |= None +a ^= None +a &= None +a <<= None +a >>= None + +# Normal operator should not fallback to inplace operator +try: + a * None +except TypeError: + print("TypeError") diff --git a/tests/basics/class_inplace_op2.py.exp b/tests/basics/class_inplace_op2.py.exp new file mode 100644 index 0000000000000..8c323b5178ef4 --- /dev/null +++ b/tests/basics/class_inplace_op2.py.exp @@ -0,0 +1,12 @@ +__imul__ +__imatmul__ +__ifloordiv__ +__itruediv__ +__imod__ +__ipow__ +__ior__ +__ixor__ +__iand__ +__ilshift__ +__irshift__ +TypeError From 3e16763201d879cfdd58fdf269fbe03a3d326675 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 22 Sep 2020 14:08:22 +1000 Subject: [PATCH 0306/3533] stm32/rfcore: Fix FUS layout and size of ipcc_device_info_table_t. The device info table has a different layout when core 2 is in FUS mode. In particular it's larger than the 32 bytes used when in WS mode and if the correct amount of space is not allocated then the end of the table may be overwritten with other data (eg with FUS version 0.5.3). So update the structure to fix this. Also update rfcore.py to disable IRQs (which are enabled by rfcore.c), to not depend on uctypes, and to not require the asm_thumb emitter. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_WB55/rfcore.py | 19 ++++++------ ports/stm32/rfcore.c | 38 ++++++++++++++++++------ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore.py b/ports/stm32/boards/NUCLEO_WB55/rfcore.py index e612499a7829f..cfe315605e541 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore.py @@ -34,10 +34,15 @@ # to print out SRAM2A, register state and FUS/WS info. from machine import mem8, mem16, mem32 -import time, struct, uctypes +import time, struct import stm +def addressof(buf): + assert type(buf) is bytearray + return mem32[id(buf) + 12] + + class Flash: FLASH_KEY1 = 0x45670123 FLASH_KEY2 = 0xCDEF89AB @@ -68,7 +73,7 @@ def write(self, addr, buf): self.wait_not_busy() cr = 1 << 0 # PG mem32[stm.FLASH + stm.FLASH_CR] = cr - buf_addr = uctypes.addressof(buf) + buf_addr = addressof(buf) off = 0 while off < len(buf): mem32[addr + off] = mem32[buf_addr + off] @@ -111,13 +116,6 @@ def copy_file_to_flash(filename, addr): OCF_FUS_START_WS = const(0x5A) OCF_BLE_INIT = const(0x66) - -@micropython.asm_thumb -def asm_sev_wfe(): - data(2, 0xBF40) # sev - data(2, 0xBF20) # wfe - - TABLE_DEVICE_INFO = const(0) TABLE_BLE = const(1) TABLE_SYS = const(3) @@ -199,6 +197,9 @@ def ipcc_init(): BLE_EVT_QUEUE = get_ipcc_table_word(TABLE_BLE, 2) BLE_HCI_ACL_DATA_BUF = get_ipcc_table_word(TABLE_BLE, 3) + # Disable interrupts, the code here uses polling + mem32[stm.IPCC + stm.IPCC_C1CR] = 0 + print("IPCC initialised") print("SYS: 0x%08x 0x%08x" % (SYS_CMD_BUF, SYS_SYS_QUEUE)) print("BLE: 0x%08x 0x%08x 0x%08x" % (BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE)) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index ca2ad747c4bb8..dc23bbe0b2a69 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -94,15 +94,35 @@ typedef struct _parse_hci_info_t { // [16:23] = SRAM2b (Number of 1k sectors) // [24:31] = SRAM2a (Number of 1k sectors) -typedef struct __attribute__((packed)) _ipcc_device_info_table_t { - uint32_t safeboot_version; - uint32_t fus_version; - uint32_t fus_memorysize; - uint32_t fus_info; - uint32_t fw_version; - uint32_t fw_memorysize; - uint32_t fw_infostack; - uint32_t fw_reserved; +typedef union __attribute__((packed)) _ipcc_device_info_table_t { + struct { + uint32_t table_state; + uint8_t reserved0; + uint8_t last_fus_state; + uint8_t last_ws_state; + uint8_t ws_type; + uint32_t safeboot_version; + uint32_t fus_version; + uint32_t fus_memorysize; + uint32_t ws_version; + uint32_t ws_memorysize; + uint32_t ws_ble_info; + uint32_t ws_thread_info; + uint32_t reserved1; + uint64_t uid64; + uint16_t device_id; + uint16_t pad; + } fus; + struct { + uint32_t safeboot_version; + uint32_t fus_version; + uint32_t fus_memorysize; + uint32_t fus_info; + uint32_t fw_version; + uint32_t fw_memorysize; + uint32_t fw_infostack; + uint32_t fw_reserved; + } ws; } ipcc_device_info_table_t; typedef struct __attribute__((packed)) _ipcc_ble_table_t { From 71adf506ce43b55b859f81f191ac0826928bbdd5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 22 Sep 2020 15:05:00 +1000 Subject: [PATCH 0307/3533] extmod/vfs: Fix lookup of entry in root dir so it fails correctly. Prior to this commit, uos.chdir('/') followed by uos.stat('noexist') would succeed that stat even though the entry did not exist (some other functions like listdir would have similar issues). This is because, if the current directory was the root and the path was relative, mp_vfs_lookup_path would return success for bad paths. Signed-off-by: Damien George --- extmod/vfs.c | 8 ++------ tests/extmod/vfs_basic.py | 8 ++++++++ tests/extmod/vfs_basic.py.exp | 12 ++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index d1291068ad8d6..3cb7af1b434f9 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -83,12 +83,8 @@ mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) { } } - // if we get here then there's nothing mounted on / - - if (is_abs) { - // path began with / and was not found - return MP_VFS_NONE; - } + // if we get here then there's nothing mounted on /, so the path doesn't exist + return MP_VFS_NONE; } // a relative path within a mounted device diff --git a/tests/extmod/vfs_basic.py b/tests/extmod/vfs_basic.py index 62b2a277381d6..9a9ef2ca61be3 100644 --- a/tests/extmod/vfs_basic.py +++ b/tests/extmod/vfs_basic.py @@ -74,6 +74,14 @@ def open(self, file, mode): # getcwd when in root dir print(uos.getcwd()) +# test operations on the root directory with nothing mounted, they should all fail +for func in ("chdir", "listdir", "mkdir", "remove", "rmdir", "stat"): + for arg in ("x", "/x"): + try: + getattr(uos, func)(arg) + except OSError: + print(func, arg, "OSError") + # basic mounting and listdir uos.mount(Filesystem(1), "/test_mnt") print(uos.listdir()) diff --git a/tests/extmod/vfs_basic.py.exp b/tests/extmod/vfs_basic.py.exp index ebca31030451b..536bb4c805d30 100644 --- a/tests/extmod/vfs_basic.py.exp +++ b/tests/extmod/vfs_basic.py.exp @@ -1,6 +1,18 @@ (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) True / +chdir x OSError +chdir /x OSError +listdir x OSError +listdir /x OSError +mkdir x OSError +mkdir /x OSError +remove x OSError +remove /x OSError +rmdir x OSError +rmdir /x OSError +stat x OSError +stat /x OSError 1 mount False False ['test_mnt'] ('test_mnt', 16384, 0) From bada8c923195622aee80faf7acf003bc7852459d Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 22 Sep 2020 13:59:47 +0200 Subject: [PATCH 0308/3533] windows: Update build instructions in README. Make the instructions more complete by documenting all needed steps for starting from scratch. Also add a section for MSYS2 since the Travis build uses it as well and it's a good alternative for Cygwin. Remove the mingw32 reference since it's not readily available anymore in most Linux distros nor compiles successfully. --- ports/windows/README.md | 60 ++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/ports/windows/README.md b/ports/windows/README.md index f1bd405513b99..8d907d1b7243a 100644 --- a/ports/windows/README.md +++ b/ports/windows/README.md @@ -3,46 +3,73 @@ It is based on Unix port, and expected to remain so. The port requires additional testing, debugging, and patches. Please consider to contribute. +All gcc-based builds use the gcc compiler from [Mingw-w64](mingw-w64.org), +which is the advancement of the original mingw project. The latter is +getting obsolete and is not actively supported by MicroPython. + +Build instruction assume you're in the ports/windows directory. Building on Debian/Ubuntu Linux system --------------------------------------- - sudo apt-get install gcc-mingw-w64 + sudo apt-get install python3 build-essential gcc-mingw-w64 + make -C ../../mpy-cross make CROSS_COMPILE=i686-w64-mingw32- -If for some reason the mingw-w64 crosscompiler is not available, you can try -mingw32 instead, but it comes with a really old gcc which may produce some -spurious errors (you may need to disable -Werror): - - sudo apt-get install mingw32 mingw32-binutils mingw32-runtime - make CROSS_COMPILE=i586-mingw32msvc- - Building under Cygwin --------------------- -Install following packages using cygwin's setup.exe: +Install Cygwin, then install following packages using Cygwin's setup.exe: * mingw64-i686-gcc-core * mingw64-x86_64-gcc-core * make +Also install the python3 package, or install Python globally for Windows (see below). + Build using: + make -C ../../mpy-cross CROSS_COMPILE=i686-w64-mingw32- make CROSS_COMPILE=i686-w64-mingw32- Or for 64bit: + make -C ../../mpy-cross CROSS_COMPILE=x86_64-w64-mingw32- make CROSS_COMPILE=x86_64-w64-mingw32- +Building under MSYS2 +-------------------- + +Install MSYS2 from http://repo.msys2.org/distrib, start the msys2.exe shell and +install the build tools: + + pacman -Syuu + pacman -S make mingw-w64-x86_64-gcc pkg-config python3 + +Start the mingw64.exe shell and build: + + make -C ../../mpy-cross STRIP=echo SIZE=echo + make + + Building using MS Visual Studio 2013 (or higher) ------------------------------------------------ -In the IDE, open `micropython.vcxproj` and build. +Install Python. There are several ways to do this, for example: download and install the +latest Python 3 release from https://www.python.org/downloads/windows or from +https://docs.conda.io/en/latest/miniconda.html, +or open the Microsoft Store app and search for Python and install it. + +Install Visual Studio and the C++ toolset (for recent versions: install +the free Visual Studio Community edition and the *Desktop development with C++* workload). + +In the IDE, open `micropython-cross.vcxproj` and `micropython.vcxproj` and build. To build from the command line: + msbuild ../../mpy-cross/mpy-cross.vcxproj msbuild micropython.vcxproj __Stack usage__ @@ -57,6 +84,19 @@ There are several ways to deal with this: See [issue 2927](https://github.com/micropython/micropython/issues/2927) for more information. +Running the tests +----------------- + +This is similar for all ports: + + cd ../../tests + python3 ./run-tests + +Depending on the combination of platform and Python version used it might be +needed to first set the MICROPY_MICROPYTHON environment variable to +the full path of micropython.exe. + + Running on Linux using Wine --------------------------- From ca017841d65b5f9da5eb4e6e3ca66b009bc54fc4 Mon Sep 17 00:00:00 2001 From: Iyassou Shimels Date: Wed, 23 Sep 2020 22:45:22 +0300 Subject: [PATCH 0309/3533] py/objstr: Make bytes(bytes_obj) return bytes_obj. Calling the bytes constructor on a bytes object returns the original bytes object. This saves allocating a new instance, and matches CPython. Signed-off-by: Iyassou Shimels --- py/objstr.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index a276a255e516f..84728e6f2d919 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -205,6 +205,10 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size return mp_const_empty_bytes; } + if (mp_obj_is_type(args[0], &mp_type_bytes)) { + return args[0]; + } + if (mp_obj_is_str(args[0])) { if (n_args < 2 || n_args > 3) { goto wrong_args; From 50e34f979c90584273a67ecd9189640417a60960 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 23:17:56 +1000 Subject: [PATCH 0310/3533] py/objarray.h: Add mp_obj_memoryview_init() helper function. Signed-off-by: Damien George --- py/objarray.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/py/objarray.h b/py/objarray.h index 2fb6e2c9158f4..94c31c969386b 100644 --- a/py/objarray.h +++ b/py/objarray.h @@ -49,4 +49,14 @@ typedef struct _mp_obj_array_t { void *items; } mp_obj_array_t; +#if MICROPY_PY_BUILTINS_MEMORYVIEW +static inline void mp_obj_memoryview_init(mp_obj_array_t *self, size_t typecode, size_t offset, size_t len, void *items) { + self->base.type = &mp_type_memoryview; + self->typecode = typecode; + self->free = offset; + self->len = len; + self->items = items; +} +#endif + #endif // MICROPY_INCLUDED_PY_OBJARRAY_H From 81f2162ca0e926c9a4a1787a3863d94d86be0b36 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 23:18:16 +1000 Subject: [PATCH 0311/3533] extmod/modbluetooth: Change module-owned bytes objects to memoryview. A read-only memoryview object is a better representation of the data, which is owned by the ubluetooth module and may change between calls to the user's irq callback function. Signed-off-by: Damien George --- docs/library/ubluetooth.rst | 22 +++++++++++----- extmod/modbluetooth.c | 26 +++++++------------ tests/multi_bluetooth/ble_characteristic.py | 6 ++--- tests/multi_bluetooth/ble_gap_device_name.py | 2 +- .../multi_bluetooth/ble_gatt_data_transfer.py | 2 +- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 03d03583a1cce..f94ad3a612c07 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -88,12 +88,22 @@ Event Handling arguments, ``event`` (which will be one of the codes below) and ``data`` (which is an event-specific tuple of values). - **Note:** the ``addr``, ``adv_data``, ``char_data``, ``notify_data``, and - ``uuid`` entries in the tuples are references to data managed by the - :mod:`ubluetooth` module (i.e. the same instance will be re-used across - multiple calls to the event handler). If your program wants to use this - data outside of the handler, then it must copy them first, e.g. by using - ``bytes(addr)`` or ``bluetooth.UUID(uuid)``. + **Note:** As an optimisation to prevent unnecessary allocations, the ``addr``, + ``adv_data``, ``char_data``, ``notify_data``, and ``uuid`` entries in the + tuples are read-only memoryview instances pointing to ubluetooth's internal + ringbuffer, and are only valid during the invocation of the IRQ handler + function. If your program needs to save one of these values to access after + the IRQ handler has returned (e.g. by saving it in a class instance or global + variable), then it needs to take a copy of the data, either by using ``bytes()`` + or ``bluetooth.UUID()``, like this:: + + connected_addr = bytes(addr) # equivalently: adv_data, char_data, or notify_data + matched_uuid = bluetooth.UUID(uuid) + + For example, the IRQ handler for a scan result might inspect the ``adv_data`` + to decide if it's the correct device, and only then copy the address data to be + used elsewhere in the program. And to print data from within the IRQ handler, + ``print(bytes(addr))`` will be needed. An event handler showing all possible events:: diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index daf9cd0d1984b..57f69433a1a97 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -32,7 +32,6 @@ #include "py/mphal.h" #include "py/obj.h" #include "py/objarray.h" -#include "py/objstr.h" #include "py/qstr.h" #include "py/runtime.h" #include "extmod/modbluetooth.h" @@ -62,9 +61,8 @@ typedef struct { mp_obj_t irq_data_tuple; uint8_t irq_data_addr_bytes[6]; uint16_t irq_data_data_alloc; - uint8_t *irq_data_data_bytes; - mp_obj_str_t irq_data_addr; - mp_obj_str_t irq_data_data; + mp_obj_array_t irq_data_addr; + mp_obj_array_t irq_data_data; mp_obj_bluetooth_uuid_t irq_data_uuid; ringbuf_t ringbuf; #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK @@ -262,11 +260,9 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, #endif // Pre-allocated buffers for address, payload and uuid. - o->irq_data_addr.base.type = &mp_type_bytes; - o->irq_data_addr.data = o->irq_data_addr_bytes; + mp_obj_memoryview_init(&o->irq_data_addr, 'B', 0, 0, o->irq_data_addr_bytes); o->irq_data_data_alloc = MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(MICROPY_PY_BLUETOOTH_RINGBUF_SIZE); - o->irq_data_data.base.type = &mp_type_bytes; - o->irq_data_data.data = m_new(uint8_t, o->irq_data_data_alloc); + mp_obj_memoryview_init(&o->irq_data_data, 'B', 0, 0, m_new(uint8_t, o->irq_data_data_alloc)); o->irq_data_uuid.base.type = &bluetooth_uuid_type; // Allocate the default ringbuf. @@ -352,7 +348,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map // Get old buffer sizes and pointers uint8_t *old_ringbuf_buf = self->ringbuf.buf; size_t old_ringbuf_alloc = self->ringbuf.size; - uint8_t *old_irq_data_buf = (uint8_t *)self->irq_data_data.data; + uint8_t *old_irq_data_buf = (uint8_t *)self->irq_data_data.items; size_t old_irq_data_alloc = self->irq_data_data_alloc; // Atomically update the ringbuf and irq data @@ -362,7 +358,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map self->ringbuf.iget = 0; self->ringbuf.iput = 0; self->irq_data_data_alloc = irq_data_alloc; - self->irq_data_data.data = irq_data; + self->irq_data_data.items = irq_data; MICROPY_PY_BLUETOOTH_EXIT // Free old buffers @@ -850,7 +846,7 @@ const mp_obj_module_t mp_module_ubluetooth = { #include -STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size_t n_u16, size_t n_u8, mp_obj_str_t *bytes_addr, size_t n_i8, mp_obj_bluetooth_uuid_t *uuid, mp_obj_str_t *bytes_data) { +STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size_t n_u16, size_t n_u8, mp_obj_array_t *bytes_addr, size_t n_i8, mp_obj_bluetooth_uuid_t *uuid, mp_obj_array_t *bytes_data) { assert(ringbuf_avail(ringbuf) >= n_u16 * 2 + n_u8 + (bytes_addr ? 6 : 0) + n_i8 + (uuid ? 1 : 0) + (bytes_data ? 1 : 0)); size_t j = 0; @@ -863,8 +859,7 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size if (bytes_addr) { bytes_addr->len = 6; for (size_t i = 0; i < bytes_addr->len; ++i) { - // cast away const, this is actually bt->irq_addr_bytes. - ((uint8_t *)bytes_addr->data)[i] = ringbuf_get(ringbuf); + ((uint8_t *)bytes_addr->items)[i] = ringbuf_get(ringbuf); } data_tuple->items[j++] = MP_OBJ_FROM_PTR(bytes_addr); } @@ -880,12 +875,11 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size #endif // The code that enqueues into the ringbuf should ensure that it doesn't // put more than bt->irq_data_data_alloc bytes into the ringbuf, because - // that's what's available here in bt->irq_data_bytes. + // that's what's available here. if (bytes_data) { bytes_data->len = ringbuf_get16(ringbuf); for (size_t i = 0; i < bytes_data->len; ++i) { - // cast away const, this is actually bt->irq_data_bytes. - ((uint8_t *)bytes_data->data)[i] = ringbuf_get(ringbuf); + ((uint8_t *)bytes_data->items)[i] = ringbuf_get(ringbuf); } data_tuple->items[j++] = MP_OBJ_FROM_PTR(bytes_data); } diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index d918d9aefcf03..026b9d551c2b9 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -54,15 +54,15 @@ def irq(event, data): print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) value_handle = data[2] elif event == _IRQ_GATTC_READ_RESULT: - print("_IRQ_GATTC_READ_RESULT", data[-1]) + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) elif event == _IRQ_GATTC_READ_DONE: print("_IRQ_GATTC_READ_DONE", data[-1]) elif event == _IRQ_GATTC_WRITE_DONE: print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: - print("_IRQ_GATTC_NOTIFY", data[-1]) + print("_IRQ_GATTC_NOTIFY", bytes(data[-1])) elif event == _IRQ_GATTC_INDICATE: - print("_IRQ_GATTC_INDICATE", data[-1]) + print("_IRQ_GATTC_INDICATE", bytes(data[-1])) elif event == _IRQ_GATTS_INDICATE_DONE: print("_IRQ_GATTS_INDICATE_DONE", data[-1]) diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py index 92ea94278a647..fbc9d80baeb08 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py +++ b/tests/multi_bluetooth/ble_gap_device_name.py @@ -36,7 +36,7 @@ def irq(event, data): print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) value_handle = data[2] elif event == _IRQ_GATTC_READ_RESULT: - print("_IRQ_GATTC_READ_RESULT", data[-1]) + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) if waiting_event is not None: if isinstance(waiting_event, int) and event == waiting_event: diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py b/tests/multi_bluetooth/ble_gatt_data_transfer.py index 240f048607c92..944c9e2d2a55f 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py @@ -67,7 +67,7 @@ def irq(event, data): elif event == _IRQ_GATTC_WRITE_DONE: print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: - print("_IRQ_GATTC_NOTIFY", data[-1]) + print("_IRQ_GATTC_NOTIFY", bytes(data[-1])) if waiting_event is not None: if (isinstance(waiting_event, int) and event == waiting_event) or ( From c8ade2bd7f5d0e089098bcc07eb77a770f3da726 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 22:47:25 +1000 Subject: [PATCH 0312/3533] docs/develop: Add notes on prerequisite tools for building native .mpy. Signed-off-by: Damien George --- docs/develop/natmod.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/develop/natmod.rst b/docs/develop/natmod.rst index dc9f82e773874..a4c188719a4d9 100644 --- a/docs/develop/natmod.rst +++ b/docs/develop/natmod.rst @@ -21,7 +21,8 @@ language which can be compiled to stand-alone machine code can be put into a A native .mpy module is built using the ``mpy_ld.py`` tool, which is found in the ``tools/`` directory of the project. This tool takes a set of object files -(.o files) and links them together to create a native .mpy files. +(.o files) and links them together to create a native .mpy files. It requires +CPython 3 and the library pyelftools v0.25 or greater. Supported features and limitations ---------------------------------- @@ -179,6 +180,14 @@ The file ``Makefile`` contains: Compiling the module -------------------- +The prerequisite tools needed to build a native .mpy file are: + +* The MicroPython repository (at least the ``py/`` and ``tools/`` directories). +* CPython 3, and the library pyelftools (eg ``pip install 'pyelftools>=0.25'``). +* GNU make. +* A C compiler for the target architecture (if C source is used). +* Optionally ``mpy-cross``, built from the MicroPython repository (if .py source is used). + Be sure to select the correct ``ARCH`` for the target you are going to run on. Then build with:: From 9123b67d641ba708a4ea3e5cd50665f0a73c6c8a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 24 Sep 2020 12:03:30 +1000 Subject: [PATCH 0313/3533] tests/run-tests: Use -BS flags when running CPython. The use of -S ensures that only the CPython standard library is accessible, which makes tests run the same regardless of any site-packages that are installed. It also improves start-up time of CPython, reducing the overall time spent running the test suite. tests/basics/containment.py is updated to work around issue with old Python versions not being able to str-format a dict-keys object, which becomes apparent when -S is used. Signed-off-by: Damien George --- tests/basics/containment.py | 4 ++-- tests/run-tests | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/basics/containment.py b/tests/basics/containment.py index 4c94a9bae4c81..24317ddd233b9 100644 --- a/tests/basics/containment.py +++ b/tests/basics/containment.py @@ -1,8 +1,8 @@ # sets, see set_containment for i in 1, 2: for o in {1:2}, {1:2}.keys(): - print("{} in {}: {}".format(i, o, i in o)) - print("{} not in {}: {}".format(i, o, i not in o)) + print("{} in {}: {}".format(i, str(o), i in o)) + print("{} not in {}: {}".format(i, str(o), i not in o)) haystack = "supercalifragilistc" for needle in [haystack[i:] for i in range(len(haystack))]: diff --git a/tests/run-tests b/tests/run-tests index a7b88ecdd3265..eebc8c4252700 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -26,6 +26,10 @@ else: CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/unix/micropython')) +# Use CPython options to not save .pyc files, to only access the core standard library +# (not site packages which may clash with u-module names), and improve start up time. +CPYTHON3_CMD = [CPYTHON3, "-BS"] + # mpy-cross is only needed if --via-mpy command-line arg is passed MPYCROSS = os.getenv('MICROPY_MPYCROSS', base_path('../mpy-cross/mpy-cross')) @@ -319,7 +323,7 @@ def run_tests(pyb, tests, args, result_dir): upy_float_precision = int(upy_float_precision) has_complex = run_feature_check(pyb, args, base_path, 'complex.py') == b'complex\n' has_coverage = run_feature_check(pyb, args, base_path, 'coverage.py') == b'coverage\n' - cpy_byteorder = subprocess.check_output([CPYTHON3, base_path('feature_check/byteorder.py')]) + cpy_byteorder = subprocess.check_output(CPYTHON3_CMD + [base_path('feature_check/byteorder.py')]) skip_endian = (upy_byteorder != cpy_byteorder) # These tests don't test slice explicitly but rather use it to perform the test @@ -498,7 +502,7 @@ def run_tests(pyb, tests, args, result_dir): else: # run CPython to work out expected output try: - output_expected = subprocess.check_output([CPYTHON3, '-B', test_file]) + output_expected = subprocess.check_output(CPYTHON3_CMD + [test_file]) if args.write_exp: with open(test_file_expected, 'wb') as f: f.write(output_expected) From 0fd0eb00aa5b9d311046d48d73a8cfabb30d7dd6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 25 Sep 2020 03:15:22 +1000 Subject: [PATCH 0314/3533] examples/bluetooth: Update to use positional-only args to irq(). To match 6a6a5f9e151473bdcc1d14725d680691ff665a82. --- examples/bluetooth/ble_simple_central.py | 2 +- examples/bluetooth/ble_simple_peripheral.py | 2 +- examples/bluetooth/ble_temperature.py | 2 +- examples/bluetooth/ble_temperature_central.py | 2 +- examples/bluetooth/ble_uart_peripheral.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/bluetooth/ble_simple_central.py b/examples/bluetooth/ble_simple_central.py index f0a3297d322e1..a6bbdc6ee0795 100644 --- a/examples/bluetooth/ble_simple_central.py +++ b/examples/bluetooth/ble_simple_central.py @@ -45,7 +45,7 @@ class BLESimpleCentral: def __init__(self, ble): self._ble = ble self._ble.active(True) - self._ble.irq(handler=self._irq) + self._ble.irq(self._irq) self._reset() diff --git a/examples/bluetooth/ble_simple_peripheral.py b/examples/bluetooth/ble_simple_peripheral.py index f5e7661926ba8..d2b134e7149e9 100644 --- a/examples/bluetooth/ble_simple_peripheral.py +++ b/examples/bluetooth/ble_simple_peripheral.py @@ -31,7 +31,7 @@ class BLESimplePeripheral: def __init__(self, ble, name="mpy-uart"): self._ble = ble self._ble.active(True) - self._ble.irq(handler=self._irq) + self._ble.irq(self._irq) ((self._handle_tx, self._handle_rx),) = self._ble.gatts_register_services((_UART_SERVICE,)) self._connections = set() self._write_callback = None diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py index 001a26b114a0a..d375a62ffb8ac 100644 --- a/examples/bluetooth/ble_temperature.py +++ b/examples/bluetooth/ble_temperature.py @@ -35,7 +35,7 @@ class BLETemperature: def __init__(self, ble, name="mpy-temp"): self._ble = ble self._ble.active(True) - self._ble.irq(handler=self._irq) + self._ble.irq(self._irq) ((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,)) self._connections = set() self._payload = advertising_payload( diff --git a/examples/bluetooth/ble_temperature_central.py b/examples/bluetooth/ble_temperature_central.py index 79830341789d4..96c4aad1445a1 100644 --- a/examples/bluetooth/ble_temperature_central.py +++ b/examples/bluetooth/ble_temperature_central.py @@ -56,7 +56,7 @@ class BLETemperatureCentral: def __init__(self, ble): self._ble = ble self._ble.active(True) - self._ble.irq(handler=self._irq) + self._ble.irq(self._irq) self._reset() diff --git a/examples/bluetooth/ble_uart_peripheral.py b/examples/bluetooth/ble_uart_peripheral.py index 59b35f7e6ad5e..6d167a871a75e 100644 --- a/examples/bluetooth/ble_uart_peripheral.py +++ b/examples/bluetooth/ble_uart_peripheral.py @@ -31,7 +31,7 @@ class BLEUART: def __init__(self, ble, name="mpy-uart", rxbuf=100): self._ble = ble self._ble.active(True) - self._ble.irq(handler=self._irq) + self._ble.irq(self._irq) ((self._tx_handle, self._rx_handle),) = self._ble.gatts_register_services((_UART_SERVICE,)) # Increase the size of the rx buffer and enable append mode. self._ble.gatts_set_buffer(self._rx_handle, rxbuf, True) From 319437d4bd7c97ab18275d3eaf1bc190554f9df7 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 25 Sep 2020 12:27:02 +1000 Subject: [PATCH 0315/3533] extmod/modure: Allow \\ in re.sub replacements. --- extmod/modure.c | 3 +++ tests/extmod/ure_sub.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/extmod/modure.c b/extmod/modure.c index 328c897d8890b..220587b42f9a3 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -343,6 +343,9 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { const char *end_match = match->caps[match_no * 2 + 1]; vstr_add_strn(&vstr_return, start_match, end_match - start_match); } + } else if (*repl == '\\') { + // Add the \ character + vstr_add_byte(&vstr_return, *repl++); } } else { // Just add the current byte from the replacement string diff --git a/tests/extmod/ure_sub.py b/tests/extmod/ure_sub.py index 953e7bf62a478..ae6ad28d621f4 100644 --- a/tests/extmod/ure_sub.py +++ b/tests/extmod/ure_sub.py @@ -43,6 +43,9 @@ def A(): ) ) +# \g immediately followed by another \g +print(re.sub("(abc)", r"\g<1>\g<1>", "abc")) + # no matches at all print(re.sub("a", "b", "c")) @@ -69,3 +72,6 @@ def A(): re.sub(123, "a", "a") except TypeError: print("TypeError") + +# Include \ in the sub replacement +print(re.sub("b", "\\\\b", "abc")) From ce49be43b1ac7582edcfec20c56928781a512a7d Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sat, 12 Sep 2020 14:46:05 -0500 Subject: [PATCH 0316/3533] zephyr: Replace zephyr integer types with C99 types. Zephyr v2.4.0 stopped using custom integer types in favor of C99 types instead. Signed-off-by: Maureen Helm --- ports/zephyr/machine_i2c.c | 2 +- ports/zephyr/zephyr_storage.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 4b29f41d1ef71..576cb197777ed 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -97,7 +97,7 @@ STATIC int machine_hard_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t add struct i2c_msg msg; int ret; - msg.buf = (u8_t *)buf; + msg.buf = (uint8_t *)buf; msg.len = len; msg.flags = 0; diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c index 83f19a8feeadd..1c25b32771e66 100644 --- a/ports/zephyr/zephyr_storage.c +++ b/ports/zephyr/zephyr_storage.c @@ -146,7 +146,7 @@ typedef struct _zephyr_flash_area_obj_t { const struct flash_area *area; int block_size; int block_count; - u8_t id; + uint8_t id; } zephyr_flash_area_obj_t; STATIC void zephyr_flash_area_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { From f842e32155d38038f3a2657238be57724b1e0bf1 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sat, 12 Sep 2020 14:50:21 -0500 Subject: [PATCH 0317/3533] zephyr: Const-ify struct device instance pointers. Zephyr v2.4.0 added a const qualifier to usages of struct device to allow storing device driver instances exclusively in flash and thereby reduce ram footprint. Signed-off-by: Maureen Helm --- ports/zephyr/machine_i2c.c | 4 ++-- ports/zephyr/machine_pin.c | 4 ++-- ports/zephyr/modmachine.h | 2 +- ports/zephyr/modzsensor.c | 2 +- ports/zephyr/uart_core.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 576cb197777ed..0efcd331fc4d1 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -42,7 +42,7 @@ STATIC const mp_obj_type_t machine_hard_i2c_type; typedef struct _machine_hard_i2c_obj_t { mp_obj_base_t base; - struct device *dev; + const struct device *dev; bool restart; } machine_hard_i2c_obj_t; @@ -65,7 +65,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj); - struct device *dev = device_get_binding(dev_name); + const struct device *dev = device_get_binding(dev_name); if (dev == NULL) { mp_raise_ValueError(MP_ERROR_TEXT("device not found")); diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index c9c6a8c8936e0..34c822c2f05a5 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -58,7 +58,7 @@ void machine_pin_deinit(void) { MP_STATE_PORT(machine_pin_irq_list) = NULL; } -STATIC void gpio_callback_handler(struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { +STATIC void gpio_callback_handler(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { machine_pin_irq_obj_t *irq = CONTAINER_OF(cb, machine_pin_irq_obj_t, callback); #if MICROPY_STACK_CHECK @@ -132,7 +132,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_obj_get_array_fixed_n(args[0], 2, &items); const char *drv_name = mp_obj_str_get_str(items[0]); int wanted_pin = mp_obj_get_int(items[1]); - struct device *wanted_port = device_get_binding(drv_name); + const struct device *wanted_port = device_get_binding(drv_name); if (!wanted_port) { mp_raise_ValueError(MP_ERROR_TEXT("invalid port")); } diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index 766a9d7ea3874..5d7b2e1ed847a 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -9,7 +9,7 @@ MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj); typedef struct _machine_pin_obj_t { mp_obj_base_t base; - struct device *port; + const struct device *port; uint32_t pin; struct _machine_pin_irq_obj_t *irq; } machine_pin_obj_t; diff --git a/ports/zephyr/modzsensor.c b/ports/zephyr/modzsensor.c index b01ce2693c232..01f05aacd64a0 100644 --- a/ports/zephyr/modzsensor.c +++ b/ports/zephyr/modzsensor.c @@ -35,7 +35,7 @@ typedef struct _mp_obj_sensor_t { mp_obj_base_t base; - struct device *dev; + const struct device *dev; } mp_obj_sensor_t; STATIC mp_obj_t sensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/zephyr/uart_core.c b/ports/zephyr/uart_core.c index 63ecc8289e3c9..44bdeb5c2784c 100644 --- a/ports/zephyr/uart_core.c +++ b/ports/zephyr/uart_core.c @@ -53,7 +53,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { } } #else - static struct device *uart_console_dev; + static const struct device *uart_console_dev; if (uart_console_dev == NULL) { uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); } From c2a7aac906a31b0d361b7177fae7fc88736a7705 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sat, 12 Sep 2020 15:35:29 -0500 Subject: [PATCH 0318/3533] travis: Update zephyr build to v2.4.0. Updates CI to use the latest zephyr release tag. Signed-off-by: Maureen Helm --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 45b67dc9fedc2..c9fcc21336efd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,7 +54,7 @@ jobs: zephyrprojectrtos/ci:v0.11.8 - docker ps -a install: - - docker exec zephyr-ci west init --mr v2.3.0 /zephyrproject + - docker exec zephyr-ci west init --mr v2.4.0 /zephyrproject - docker exec -w /zephyrproject zephyr-ci west update - docker exec -w /zephyrproject zephyr-ci west zephyr-export script: From 997ec9e8ccaf4f1d8a950ef025e098e6b033c66c Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Mon, 28 Sep 2020 09:23:10 -0500 Subject: [PATCH 0319/3533] zephyr: Update build instructions to v2.4.0. Updates the zephyr port build instructions to use the latest zephyr release tag. Signed-off-by: Maureen Helm --- ports/zephyr/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/zephyr/README.md b/ports/zephyr/README.md index a2fb393d90d0f..d69030dc2c712 100644 --- a/ports/zephyr/README.md +++ b/ports/zephyr/README.md @@ -4,7 +4,7 @@ MicroPython port to Zephyr RTOS This is a work-in-progress port of MicroPython to Zephyr RTOS (http://zephyrproject.org). -This port requires Zephyr version 2.3.0, and may also work on higher +This port requires Zephyr version 2.4.0, and may also work on higher versions. All boards supported by Zephyr (with standard level of features support, like UART console) should work with MicroPython (but not all were tested). @@ -38,13 +38,13 @@ setup is correct. If you already have Zephyr installed but are having issues building the MicroPython port then try installing the correct version of Zephyr via: - $ west init zephyrproject -m https://github.com/zephyrproject-rtos/zephyr --mr v2.3.0 + $ west init zephyrproject -m https://github.com/zephyrproject-rtos/zephyr --mr v2.4.0 Alternatively, you don't have to redo the Zephyr installation to just switch from master to a tagged release, you can instead do: $ cd zephyrproject/zephyr - $ git checkout v2.3.0 + $ git checkout v2.4.0 $ west update With Zephyr installed you may then need to configure your environment, From ee7568ca8d4f1a5d09a123cf5f7a1e430b8f4c9d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 25 Sep 2020 10:47:59 -0500 Subject: [PATCH 0320/3533] docs/reference/packages.rst: Fix typo, remove duplicate "port". Fixes #6485. --- docs/reference/packages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/packages.rst b/docs/reference/packages.rst index 2854df23a0240..e9adfc176ea73 100644 --- a/docs/reference/packages.rst +++ b/docs/reference/packages.rst @@ -188,7 +188,7 @@ Few notes: 1. Step 5 in the sequence above assumes that the distribution package is available from PyPI. If that is not the case, you would need to copy Python source files manually to ``modules/`` subdirectory - of the port port directory. (Note that upip does not support + of the port directory. (Note that upip does not support installing from e.g. version control repositories). 2. The firmware for baremetal devices usually has size restrictions, so adding too many frozen modules may overflow it. Usually, you From c711c0049e5f12cae048d2b0e77bc70e68804ea5 Mon Sep 17 00:00:00 2001 From: Mike Wadsten Date: Tue, 22 Sep 2020 11:07:20 -0500 Subject: [PATCH 0321/3533] py/makeversionhdr.py: Match only git tags which look like versions. Some downstream projects may use tags in their repositories for more than just designating MicroPython releases. In those cases, the makeversionhdr.py script would end up using a different tag than intended. So tell `git describe` to only match tags that look like a MicroPython version tag, such as `v1.12` or `v2.0`. --- py/makeversionhdr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/makeversionhdr.py b/py/makeversionhdr.py index 567b3b8322f9f..970a86e6ca3ab 100644 --- a/py/makeversionhdr.py +++ b/py/makeversionhdr.py @@ -23,7 +23,7 @@ def get_version_info_from_git(): # Note: git describe doesn't work if no tag is available try: git_tag = subprocess.check_output( - ["git", "describe", "--dirty", "--always"], + ["git", "describe", "--dirty", "--always", "--match", "v[1-9].*"], stderr=subprocess.STDOUT, universal_newlines=True, ).strip() From c35deb2625efc877b3a0d03d5654e27232b2d101 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Sep 2020 13:30:48 +1000 Subject: [PATCH 0322/3533] extmod/machine_i2c: Rename type to SoftI2C and add custom print method. Also rename machine_i2c_type to mp_machine_soft_i2c_type. These changes make it clear that it's a soft-I2C implementation, and match SoftSPI. Signed-off-by: Damien George --- extmod/machine_i2c.c | 17 ++++++++++++----- extmod/machine_i2c.h | 3 ++- ports/esp32/modmachine.c | 2 +- ports/esp8266/modmachine.c | 2 +- ports/nrf/modules/machine/i2c.h | 2 +- ports/nrf/modules/machine/modmachine.c | 2 +- ports/nrf/mphalport.h | 2 ++ ports/stm32/modmachine.c | 2 +- ports/zephyr/modmachine.c | 2 +- 9 files changed, 22 insertions(+), 12 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index c804cf570385d..8aad001f1e42f 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -302,6 +302,12 @@ STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint /******************************************************************************/ // MicroPython bindings for I2C +STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + mp_machine_soft_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "SoftI2C(scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ", freq=%u)", + mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda), 500000 / self->us_delay); +} + STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { @@ -318,7 +324,7 @@ STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, mp_hal_i2c_init(self, args[ARG_freq].u_int); } -STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { // check the id argument, if given if (n_args > 0) { if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) { @@ -336,7 +342,7 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s // create new soft I2C object machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t); - self->base.type = &machine_i2c_type; + self->base.type = &mp_machine_soft_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); machine_i2c_obj_init_helper(self, n_args, args, &kw_args); @@ -700,10 +706,11 @@ STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = { .transfer = mp_machine_soft_i2c_transfer, }; -const mp_obj_type_t machine_i2c_type = { +const mp_obj_type_t mp_machine_soft_i2c_type = { { &mp_type_type }, - .name = MP_QSTR_I2C, - .make_new = machine_i2c_make_new, + .name = MP_QSTR_SoftI2C, + .print = mp_machine_soft_i2c_print, + .make_new = mp_machine_soft_i2c_make_new, .protocol = &mp_machine_soft_i2c_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict, }; diff --git a/extmod/machine_i2c.h b/extmod/machine_i2c.h index f951c1f214973..9723069d56c99 100644 --- a/extmod/machine_i2c.h +++ b/extmod/machine_i2c.h @@ -27,6 +27,7 @@ #define MICROPY_INCLUDED_EXTMOD_MACHINE_I2C_H #include "py/obj.h" +#include "py/mphal.h" #define MP_MACHINE_I2C_FLAG_READ (0x01) // if not set then it's a write #define MP_MACHINE_I2C_FLAG_STOP (0x02) @@ -56,7 +57,7 @@ typedef struct _mp_machine_soft_i2c_obj_t { mp_hal_pin_obj_t sda; } mp_machine_soft_i2c_obj_t; -extern const mp_obj_type_t machine_i2c_type; +extern const mp_obj_type_t mp_machine_soft_i2c_type; extern const mp_obj_dict_t mp_machine_soft_i2c_locals_dict; int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags); diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 82a02522f7b41..d56f6fcef12ec 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -255,7 +255,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index cb28d5b144827..4917ed9b904fe 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -421,7 +421,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_SPI { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) }, diff --git a/ports/nrf/modules/machine/i2c.h b/ports/nrf/modules/machine/i2c.h index 92194ce75763f..3c4fde983a62a 100644 --- a/ports/nrf/modules/machine/i2c.h +++ b/ports/nrf/modules/machine/i2c.h @@ -27,7 +27,7 @@ #ifndef I2C_H__ #define I2C_H__ -extern const mp_obj_type_t machine_i2c_type; +#include "extmod/machine_i2c.h" void i2c_init0(void); diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index 48730c849f6bf..786abac7c1ea9 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -208,7 +208,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, #endif #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 15b37b7ef22d2..5900559b5dd76 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -54,8 +54,10 @@ void mp_hal_delay_us(mp_uint_t us); const char *nrfx_error_code_lookup(uint32_t err_code); +#define MP_HAL_PIN_FMT "%q" #define mp_hal_pin_obj_t const pin_obj_t * #define mp_hal_get_pin_obj(o) pin_find(o) +#define mp_hal_pin_name(p) ((p)->name) #define mp_hal_pin_high(p) nrf_gpio_pin_set(p->pin) #define mp_hal_pin_low(p) nrf_gpio_pin_clear(p->pin) #define mp_hal_pin_read(p) (nrf_gpio_pin_dir_get(p->pin) == NRF_GPIO_PIN_DIR_OUTPUT) ? nrf_gpio_pin_out_read(p->pin) : nrf_gpio_pin_read(p->pin) diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index ac4d8712397e4..ff3b842c870f1 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -415,7 +415,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 72078cf9deabc..fc4d3b3ca8706 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -60,7 +60,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, From aaed33896b0fec67a0e2ec7daf3fe908253d8cf7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Sep 2020 13:37:31 +1000 Subject: [PATCH 0323/3533] extmod/machine_i2c: Remove "id" arg in SoftI2C constructor. The SoftI2C constructor is now used soley to create SoftI2C instances, it can no longer delegate to create a hardware-based I2C instance. Signed-off-by: Damien George --- extmod/machine_i2c.c | 15 --------------- ports/esp32/mpconfigport.h | 1 - ports/nrf/mpconfigport.h | 1 - ports/stm32/mpconfigport.h | 3 --- ports/zephyr/mpconfigport.h | 1 - 5 files changed, 21 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 8aad001f1e42f..9203f16f6d785 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -325,21 +325,6 @@ STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, } STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check the id argument, if given - if (n_args > 0) { - if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) { - #if defined(MICROPY_PY_MACHINE_I2C_MAKE_NEW) - // dispatch to port-specific constructor - extern mp_obj_t MICROPY_PY_MACHINE_I2C_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args); - return MICROPY_PY_MACHINE_I2C_MAKE_NEW(type, n_args, n_kw, args); - #else - mp_raise_ValueError(MP_ERROR_TEXT("invalid I2C peripheral")); - #endif - } - --n_args; - ++args; - } - // create new soft I2C object machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t); self->base.type = &mp_machine_soft_i2c_type; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 663fed3f683a4..00abab3afd8bd 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -145,7 +145,6 @@ #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) -#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hw_i2c_make_new #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 76bb6d7bdf0df..a025feb2ec65a 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -135,7 +135,6 @@ #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PULSE (0) -#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new #define MICROPY_PY_MACHINE_SPI (0) #define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0) #define MICROPY_PY_FRAMEBUF (0) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 95b539603fb95..9ff4765ac25cc 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -183,9 +183,6 @@ #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_I2C (1) -#if MICROPY_HW_ENABLE_HW_I2C -#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new -#endif #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index f94ee72707d47..6bfd9ff884f1b 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -61,7 +61,6 @@ #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_I2C (1) -#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_PY_STRUCT (0) From 9e0533b9e158a455be9284b70011d0515096a5f6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Sep 2020 14:07:39 +1000 Subject: [PATCH 0324/3533] extmod/machine_spi: Remove "id" arg in SoftSPI constructor. The SoftSPI constructor is now used soley to create SoftSPI instances, it can no longer delegate to create a hardware-based SPI instance. Signed-off-by: Damien George --- extmod/machine_spi.c | 24 +----------------------- ports/esp32/mpconfigport.h | 1 - ports/esp8266/machine_hspi.c | 2 +- ports/esp8266/mpconfigport.h | 1 - ports/stm32/machine_spi.c | 4 ++-- ports/stm32/mpconfigport.h | 1 - 6 files changed, 4 insertions(+), 29 deletions(-) diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index a7f96573a720e..c951a5137c5ce 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -41,28 +41,6 @@ /******************************************************************************/ // MicroPython bindings for generic machine.SPI -STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args); - -mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check the id argument, if given - if (n_args > 0) { - if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) { - #if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW) - // dispatch to port-specific constructor - extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args); - return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args); - #else - mp_raise_ValueError(MP_ERROR_TEXT("invalid SPI peripheral")); - #endif - } - --n_args; - ++args; - } - - // software SPI - return mp_machine_soft_spi_make_new(type, n_args, n_kw, args); -} - STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol; @@ -275,7 +253,7 @@ const mp_obj_type_t mp_machine_soft_spi_type = { { &mp_type_type }, .name = MP_QSTR_SoftSPI, .print = mp_machine_soft_spi_print, - .make_new = mp_machine_spi_make_new, // delegate to master constructor + .make_new = mp_machine_soft_spi_make_new, .protocol = &mp_machine_soft_spi_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, }; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 00abab3afd8bd..5bf0676b238e3 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -148,7 +148,6 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) -#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hw_spi_make_new #define MICROPY_HW_ENABLE_SDCARD (1) #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index 7319194d763d3..55bbcf9f58b46 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -178,7 +178,7 @@ const mp_obj_type_t machine_hspi_type = { { &mp_type_type }, .name = MP_QSTR_HSPI, .print = machine_hspi_print, - .make_new = mp_machine_spi_make_new, // delegate to master constructor + .make_new = machine_hspi_make_new, .protocol = &machine_hspi_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, }; diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 5344a98d6a548..974310f84da6e 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -80,7 +80,6 @@ #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SPI (1) -#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hspi_make_new #define MICROPY_PY_UWEBSOCKET (1) #define MICROPY_PY_WEBREPL (1) #define MICROPY_PY_WEBREPL_DELAY (20) diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index cf6e96ab67bf6..edbd500b3c073 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -48,7 +48,7 @@ STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} }, + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} }, { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, @@ -137,7 +137,7 @@ const mp_obj_type_t machine_hard_spi_type = { { &mp_type_type }, .name = MP_QSTR_SPI, .print = machine_hard_spi_print, - .make_new = mp_machine_spi_make_new, // delegate to master constructor + .make_new = machine_hard_spi_make_new, .protocol = &machine_hard_spi_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, }; diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 9ff4765ac25cc..7ea41bb6f4655 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -186,7 +186,6 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) -#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hard_spi_make_new #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48) #define MICROPY_PY_UWEBSOCKET (MICROPY_PY_LWIP) From 39d50d129ce428858332523548f0594503d0f45b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Sep 2020 14:09:49 +1000 Subject: [PATCH 0325/3533] ports: Add SoftI2C and SoftSPI to machine module where appropriate. Previous commits removed the ability for one I2C/SPI constructor to construct both software- or hardware-based peripheral instances. Such construction is now split to explicit soft and non-soft types. This commit makes both types available in all ports that previously could create both software and hardware peripherals: machine.I2C and machine.SPI construct hardware instances, while machine.SoftI2C and machine.SoftSPI create software instances. This is a breaking change for use of software-based I2C and SPI. Code that constructed I2C/SPI peripherals in the following way will need to be changed: machine.I2C(-1, ...) -> machine.SoftI2C(...) machine.I2C(scl=scl, sda=sda) -> machine.SoftI2C(scl=scl, sda=sda) machine.SPI(-1, ...) -> machine.SoftSPI(...) machine.SPI(sck=sck, mosi=mosi, miso=miso) -> machine.SoftSPI(sck=sck, mosi=mosi, miso=miso) Code which uses machine.I2C and machine.SPI classes to access hardware peripherals does not need to change. Signed-off-by: Damien George --- ports/esp32/machine_i2c.c | 4 ++-- ports/esp32/modmachine.c | 6 ++++-- ports/esp32/modmachine.h | 1 + ports/esp8266/modmachine.c | 3 +++ ports/nrf/modules/machine/i2c.c | 4 +--- ports/nrf/modules/machine/i2c.h | 2 ++ ports/nrf/modules/machine/modmachine.c | 3 ++- ports/stm32/machine_i2c.c | 5 ++--- ports/stm32/modmachine.c | 7 +++++++ ports/stm32/modmachine.h | 1 + ports/stm32/spi.h | 1 - ports/zephyr/machine_i2c.c | 5 ++--- ports/zephyr/modmachine.c | 2 +- ports/zephyr/modmachine.h | 1 + ports/zephyr/mphalport.h | 4 ++++ 15 files changed, 33 insertions(+), 16 deletions(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 8362ed5e7be5a..075d0ded6c475 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -28,6 +28,7 @@ #include "py/mphal.h" #include "py/mperrno.h" #include "extmod/machine_i2c.h" +#include "modmachine.h" #include "driver/i2c.h" @@ -45,7 +46,6 @@ typedef struct _machine_hw_i2c_obj_t { gpio_num_t sda : 8; } machine_hw_i2c_obj_t; -STATIC const mp_obj_type_t machine_hw_i2c_type; STATIC machine_hw_i2c_obj_t machine_hw_i2c_obj[I2C_NUM_MAX]; STATIC void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, uint32_t freq, uint32_t timeout_us, bool first_init) { @@ -169,7 +169,7 @@ STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = { .transfer = machine_hw_i2c_transfer, }; -STATIC const mp_obj_type_t machine_hw_i2c_type = { +const mp_obj_type_t machine_hw_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, .print = machine_hw_i2c_print, diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index d56f6fcef12ec..303d25ee8b2d1 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -255,10 +255,12 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hw_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, // Reset reasons diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h index fbb43250ff42e..98b36e32b0231 100644 --- a/ports/esp32/modmachine.h +++ b/ports/esp32/modmachine.h @@ -16,6 +16,7 @@ extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_pwm_type; +extern const mp_obj_type_t machine_hw_i2c_type; extern const mp_obj_type_t machine_hw_spi_type; extern const mp_obj_type_t machine_uart_type; extern const mp_obj_type_t machine_rtc_type; diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index 4917ed9b904fe..86c1d728d5369 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -39,6 +39,7 @@ #include "extmod/machine_signal.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" +#include "extmod/machine_spi.h" #include "modmachine.h" #include "xtirq.h" @@ -422,9 +423,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, #if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_SPI { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, #endif // wake abilities diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index afa906c3598d7..704d8615a810d 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -63,8 +63,6 @@ #endif -STATIC const mp_obj_type_t machine_hard_i2c_type; - typedef struct _machine_hard_i2c_obj_t { mp_obj_base_t base; nrfx_twi_t p_twi; // Driver instance @@ -161,7 +159,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer_single = machine_hard_i2c_transfer_single, }; -STATIC const mp_obj_type_t machine_hard_i2c_type = { +const mp_obj_type_t machine_hard_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, .print = machine_hard_i2c_print, diff --git a/ports/nrf/modules/machine/i2c.h b/ports/nrf/modules/machine/i2c.h index 3c4fde983a62a..1dfb1f077a446 100644 --- a/ports/nrf/modules/machine/i2c.h +++ b/ports/nrf/modules/machine/i2c.h @@ -29,6 +29,8 @@ #include "extmod/machine_i2c.h" +extern const mp_obj_type_t machine_hard_i2c_type; + void i2c_init0(void); #endif // I2C_H__ diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index 786abac7c1ea9..3330349cd5963 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -208,7 +208,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, #endif #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 14dd88b78f4dc..31defcb17a117 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -32,11 +32,10 @@ #include "py/mperrno.h" #include "extmod/machine_i2c.h" #include "i2c.h" +#include "modmachine.h" #if MICROPY_HW_ENABLE_HW_I2C -STATIC const mp_obj_type_t machine_hard_i2c_type; - #define I2C_POLL_DEFAULT_TIMEOUT_US (50000) // 50ms #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) @@ -266,7 +265,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer = machine_hard_i2c_transfer, }; -STATIC const mp_obj_type_t machine_hard_i2c_type = { +const mp_obj_type_t machine_hard_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, .print = machine_hard_i2c_print, diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index ff3b842c870f1..9fa00a915186f 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -37,6 +37,7 @@ #include "extmod/machine_signal.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" +#include "extmod/machine_spi.h" #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" @@ -415,9 +416,15 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, #if MICROPY_PY_MACHINE_I2C + #if MICROPY_HW_ENABLE_HW_I2C + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + #else { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index e4ccf638855e8..e846208540f72 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -30,6 +30,7 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_timer_type; +extern const mp_obj_type_t machine_hard_i2c_type; void machine_init(void); void machine_deinit(void); diff --git a/ports/stm32/spi.h b/ports/stm32/spi.h index 885fb0bd6fc8c..17f1bf6c4ab90 100644 --- a/ports/stm32/spi.h +++ b/ports/stm32/spi.h @@ -65,7 +65,6 @@ extern const spi_t spi_obj[6]; extern const mp_spi_proto_t spi_proto; extern const mp_obj_type_t pyb_spi_type; -extern const mp_obj_type_t machine_soft_spi_type; extern const mp_obj_type_t machine_hard_spi_type; // A transfer of "len" bytes should take len*8*1000/baudrate milliseconds. diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 0efcd331fc4d1..2d1c7d5c54e8b 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -37,8 +37,7 @@ #include "py/mphal.h" #include "py/mperrno.h" #include "extmod/machine_i2c.h" - -STATIC const mp_obj_type_t machine_hard_i2c_type; +#include "modmachine.h" typedef struct _machine_hard_i2c_obj_t { mp_obj_base_t base; @@ -127,7 +126,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer_single = machine_hard_i2c_transfer_single, }; -STATIC const mp_obj_type_t machine_hard_i2c_type = { +const mp_obj_type_t machine_hard_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, .print = machine_hard_i2c_print, diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index fc4d3b3ca8706..29e6c889c4cef 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -60,7 +60,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index 5d7b2e1ed847a..b67da55338cd7 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -4,6 +4,7 @@ #include "py/obj.h" extern const mp_obj_type_t machine_pin_type; +extern const mp_obj_type_t machine_hard_i2c_type; MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj); diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h index 9ef213ddb0809..b3154b649bbda 100644 --- a/ports/zephyr/mphalport.h +++ b/ports/zephyr/mphalport.h @@ -30,6 +30,10 @@ static inline uint64_t mp_hal_time_ns(void) { } #define mp_hal_delay_us_fast(us) (mp_hal_delay_us(us)) + +// C-level pin API is not currently implemented +#define MP_HAL_PIN_FMT "%d" +#define mp_hal_pin_name(p) (0) #define mp_hal_pin_od_low(p) (mp_raise_NotImplementedError("mp_hal_pin_od_low")) #define mp_hal_pin_od_high(p) (mp_raise_NotImplementedError("mp_hal_pin_od_high")) #define mp_hal_pin_open_drain(p) (mp_raise_NotImplementedError("mp_hal_pin_open_drain")) From 71f3ade770fa7f2637d94f5ba5840b64a16a95db Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 25 Sep 2020 14:47:34 +1000 Subject: [PATCH 0326/3533] ports: Support legacy soft I2C/SPI construction via id=-1 arg. With a warning that this way of constructing software I2C/SPI is deprecated. The check and warning will be removed in a future release. This should help existing code to migrate to the new SoftI2C/SoftSPI types. Signed-off-by: Damien George --- extmod/machine_i2c.h | 13 +++++++++++++ extmod/machine_spi.h | 13 +++++++++++++ ports/esp32/machine_hw_spi.c | 2 ++ ports/esp32/machine_i2c.c | 2 ++ ports/esp8266/machine_hspi.c | 2 ++ ports/nrf/modules/machine/i2c.c | 2 ++ ports/stm32/machine_i2c.c | 2 ++ ports/stm32/machine_spi.c | 2 ++ ports/zephyr/machine_i2c.c | 2 ++ 9 files changed, 40 insertions(+) diff --git a/extmod/machine_i2c.h b/extmod/machine_i2c.h index 9723069d56c99..e3a87e282a0fe 100644 --- a/extmod/machine_i2c.h +++ b/extmod/machine_i2c.h @@ -29,6 +29,19 @@ #include "py/obj.h" #include "py/mphal.h" +// Temporary support for legacy construction of SoftI2C via I2C type. +#define MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args) \ + do { \ + if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \ + mp_print_str(MICROPY_ERROR_PRINTER, "Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead\n"); \ + if (n_args != 0) { \ + --n_args; \ + ++all_args; \ + } \ + return mp_machine_soft_i2c_type.make_new(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \ + } \ + } while (0) + #define MP_MACHINE_I2C_FLAG_READ (0x01) // if not set then it's a write #define MP_MACHINE_I2C_FLAG_STOP (0x02) diff --git a/extmod/machine_spi.h b/extmod/machine_spi.h index db21e1cd31919..ca92c719a8b5a 100644 --- a/extmod/machine_spi.h +++ b/extmod/machine_spi.h @@ -30,6 +30,19 @@ #include "py/mphal.h" #include "drivers/bus/spi.h" +// Temporary support for legacy construction of SoftSPI via SPI type. +#define MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args) \ + do { \ + if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \ + mp_print_str(MICROPY_ERROR_PRINTER, "Warning: SPI(-1, ...) is deprecated, use SoftSPI(...) instead\n"); \ + if (n_args != 0) { \ + --n_args; \ + ++all_args; \ + } \ + return mp_machine_soft_spi_type.make_new(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \ + } \ + } while (0) + // SPI protocol typedef struct _mp_machine_spi_p_t { void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 3962e26b78610..3790b4e0cf142 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -351,6 +351,8 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ } mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args); + enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} }, diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 075d0ded6c475..a1d0ad0f4d93c 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -115,6 +115,8 @@ STATIC void machine_hw_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_p } mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args); + // Parse args enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index 55bbcf9f58b46..ff3ba17255358 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -151,6 +151,8 @@ STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ob } mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, args); + // args[0] holds the id of the peripheral if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) { // FlashROM is on SPI0, so far we don't support its usage diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index 704d8615a810d..ab4d516621bc7 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -94,6 +94,8 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp /* MicroPython bindings for machine API */ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args); + enum { ARG_id, ARG_scl, ARG_sda }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 31defcb17a117..e0c408c6d07c2 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -193,6 +193,8 @@ STATIC void machine_hard_i2c_init(machine_hard_i2c_obj_t *self, uint32_t freq, u #endif mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args); + // parse args enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout, ARG_timingr }; static const mp_arg_t allowed_args[] = { diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index edbd500b3c073..37c026cefc33a 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -46,6 +46,8 @@ STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp } mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args); + enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 2d1c7d5c54e8b..ec4b8620a5014 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -51,6 +51,8 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp } mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args); + enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, From 98182a97c5a9229938406beb722966eacceeb823 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 29 Sep 2020 16:50:23 +1000 Subject: [PATCH 0327/3533] docs: Update I2C and SPI docs to add reference to SoftI2C and SoftSPI. Signed-off-by: Damien George --- docs/esp32/quickref.rst | 62 ++++++++++++++++++++++-------------- docs/esp8266/quickref.rst | 9 +++--- docs/library/machine.I2C.rst | 38 ++++++++++++++++------ docs/library/machine.SPI.rst | 20 ++++++++++-- 4 files changed, 89 insertions(+), 40 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index d5c222f3a1eef..79e61a10b6dfd 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -249,16 +249,15 @@ ESP32 specific ADC class method reference: Software SPI bus ---------------- -There are two SPI drivers. One is implemented in software (bit-banging) -and works on all pins, and is accessed via the :ref:`machine.SPI ` -class:: +Software SPI (using bit-banging) works on all pins, and is accessed via the +:ref:`machine.SoftSPI ` class:: - from machine import Pin, SPI + from machine import Pin, SoftSPI - # construct an SPI bus on the given pins + # construct a SoftSPI bus on the given pins # polarity is the idle state of SCK # phase=0 means sample on the first edge of SCK, phase=1 means the second - spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4)) + spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4)) spi.init(baudrate=200000) # set the baudrate @@ -298,39 +297,54 @@ mosi 13 23 miso 12 19 ===== =========== ============ -Hardware SPI has the same methods as Software SPI above:: +Hardware SPI is accessed via the :ref:`machine.SPI ` class and +has the same methods as software SPI above:: from machine import Pin, SPI hspi = SPI(1, 10000000, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) vspi = SPI(2, baudrate=80000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) +Software I2C bus +---------------- -I2C bus -------- - -The I2C driver has both software and hardware implementations, and the two -hardware peripherals have identifiers 0 and 1. Any available output-capable -pins can be used for SCL and SDA. The driver is accessed via the -:ref:`machine.I2C ` class:: - - from machine import Pin, I2C +Software I2C (using bit-banging) works on all output-capable pins, and is +accessed via the :ref:`machine.SoftI2C ` class:: - # construct a software I2C bus - i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) + from machine import Pin, SoftI2C - # construct a hardware I2C bus - i2c = I2C(0) - i2c = I2C(1, scl=Pin(5), sda=Pin(4), freq=400000) + i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100000) - i2c.scan() # scan for slave devices + i2c.scan() # scan for devices - i2c.readfrom(0x3a, 4) # read 4 bytes from slave device with address 0x3a - i2c.writeto(0x3a, '12') # write '12' to slave device with address 0x3a + i2c.readfrom(0x3a, 4) # read 4 bytes from device with address 0x3a + i2c.writeto(0x3a, '12') # write '12' to device with address 0x3a buf = bytearray(10) # create a buffer with 10 bytes i2c.writeto(0x3a, buf) # write the given buffer to the slave +Hardware I2C bus +---------------- + +There are two hardware I2C peripherals with identifiers 0 and 1. Any available +output-capable pins can be used for SCL and SDA but the defaults are given +below. + +===== =========== ============ +\ I2C(0) I2C(1) +===== =========== ============ +scl 18 25 +sda 19 26 +===== =========== ============ + +The driver is accessed via the :ref:`machine.I2C ` class and +has the same methods as software I2C above:: + + from machine import Pin, I2C + + i2c = I2C(0) + i2c = I2C(1, scl=Pin(5), sda=Pin(4), freq=400000) + Real time clock (RTC) --------------------- diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index c884d93fc9940..1a22bd50a5ac2 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -214,15 +214,15 @@ Software SPI bus ---------------- There are two SPI drivers. One is implemented in software (bit-banging) -and works on all pins, and is accessed via the :ref:`machine.SPI ` +and works on all pins, and is accessed via the :ref:`machine.SoftSPI ` class:: - from machine import Pin, SPI + from machine import Pin, SoftSPI # construct an SPI bus on the given pins # polarity is the idle state of SCK # phase=0 means sample on the first edge of SCK, phase=1 means the second - spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4)) + spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4)) spi.init(baudrate=200000) # set the baudrate @@ -258,7 +258,8 @@ I2C bus ------- The I2C driver is implemented in software and works on all pins, -and is accessed via the :ref:`machine.I2C ` class:: +and is accessed via the :ref:`machine.I2C ` class (which is an +alias of :ref:`machine.SoftI2C `):: from machine import Pin, I2C diff --git a/docs/library/machine.I2C.rst b/docs/library/machine.I2C.rst index 743554bc76650..f9b951564611b 100644 --- a/docs/library/machine.I2C.rst +++ b/docs/library/machine.I2C.rst @@ -12,6 +12,14 @@ when created, or initialised later on. Printing the I2C object gives you information about its configuration. +Both hardware and software I2C implementations exist via the +:ref:`machine.I2C ` and `machine.SoftI2C` classes. Hardware I2C uses +underlying hardware support of the system to perform the reads/writes and is +usually efficient and fast but may have restrictions on which pins can be used. +Software I2C is implemented by bit-banging and can be used on any pin but is not +as efficient. These classes have the same methods available and differ primarily +in the way they are constructed. + Example usage:: from machine import I2C @@ -33,21 +41,33 @@ Example usage:: Constructors ------------ -.. class:: I2C(id=-1, *, scl, sda, freq=400000) +.. class:: I2C(id, *, scl, sda, freq=400000) Construct and return a new I2C object using the following parameters: - - *id* identifies a particular I2C peripheral. The default - value of -1 selects a software implementation of I2C which can - work (in most cases) with arbitrary pins for SCL and SDA. - If *id* is -1 then *scl* and *sda* must be specified. Other - allowed values for *id* depend on the particular port/board, - and specifying *scl* and *sda* may or may not be required or - allowed in this case. + - *id* identifies a particular I2C peripheral. Allowed values for + depend on the particular port/board + - *scl* should be a pin object specifying the pin to use for SCL. + - *sda* should be a pin object specifying the pin to use for SDA. + - *freq* should be an integer which sets the maximum frequency + for SCL. + + Note that some ports/boards will have default values of *scl* and *sda* + that can be changed in this constructor. Others will have fixed values + of *scl* and *sda* that cannot be changed. + +.. _machine.SoftI2C: +.. class:: SoftI2C(scl, sda, *, freq=400000, timeout=255) + + Construct a new software I2C object. The parameters are: + - *scl* should be a pin object specifying the pin to use for SCL. - *sda* should be a pin object specifying the pin to use for SDA. - *freq* should be an integer which sets the maximum frequency for SCL. + - *timeout* is the maximum time in microseconds to wait for clock + stretching (SCL held low by another device on the bus), after + which an ``OSError(ETIMEDOUT)`` exception is raised. General Methods --------------- @@ -79,7 +99,7 @@ The following methods implement the primitive I2C master bus operations and can be combined to make any I2C transaction. They are provided if you need more control over the bus, otherwise the standard methods (see below) can be used. -These methods are available on software I2C only. +These methods are only available on the `machine.SoftI2C` class. .. method:: I2C.start() diff --git a/docs/library/machine.SPI.rst b/docs/library/machine.SPI.rst index ea460a7b81282..7565241eb1cd2 100644 --- a/docs/library/machine.SPI.rst +++ b/docs/library/machine.SPI.rst @@ -11,21 +11,35 @@ SS (Slave Select), to select a particular device on a bus with which communication takes place. Management of an SS signal should happen in user code (via machine.Pin class). +Both hardware and software SPI implementations exist via the +:ref:`machine.SPI ` and `machine.SoftSPI` classes. Hardware SPI uses underlying +hardware support of the system to perform the reads/writes and is usually +efficient and fast but may have restrictions on which pins can be used. +Software SPI is implemented by bit-banging and can be used on any pin but +is not as efficient. These classes have the same methods available and +differ primarily in the way they are constructed. + Constructors ------------ .. class:: SPI(id, ...) - Construct an SPI object on the given bus, ``id``. Values of ``id`` depend + Construct an SPI object on the given bus, *id*. Values of *id* depend on a particular port and its hardware. Values 0, 1, etc. are commonly used - to select hardware SPI block #0, #1, etc. Value -1 can be used for - bitbanging (software) implementation of SPI (if supported by a port). + to select hardware SPI block #0, #1, etc. With no additional parameters, the SPI object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. See ``init`` for parameters of initialisation. +.. _machine.SoftSPI: +.. class:: SoftSPI(baudrate=500000, *, polarity=0, phase=0, bits=8, firstbit=MSB, sck=None, mosi=None, miso=None) + + Construct a new software SPI object. Additional parameters must be + given, usually at least *sck*, *mosi* and *miso*, and these are used + to initialise the bus. See `SPI.init` for a description of the parameters. + Methods ------- From 905a18aafefbe04aa2beceb84885c29aa156b975 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 24 Sep 2020 14:37:01 +1000 Subject: [PATCH 0328/3533] unix,windows: Implement mp_hal_time_ns using gettimeofday. This provides microsecond accuracy. Signed-off-by: Damien George --- ports/unix/unix_mphal.c | 5 +++-- ports/windows/windows_mphal.c | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index f43067f646ba9..3fe2fa9fec21d 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -216,6 +216,7 @@ mp_uint_t mp_hal_ticks_us(void) { } uint64_t mp_hal_time_ns(void) { - time_t now = time(NULL); - return (uint64_t)now * 1000000000ULL; + struct timeval tv; + gettimeofday(&tv, NULL); + return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 8ed087358f567..b442b59147e4e 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -255,3 +255,9 @@ mp_uint_t mp_hal_ticks_cpu(void) { return value.LowPart; #endif } + +uint64_t mp_hal_time_ns(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; +} From d4b61b00172ccc231307e3ef33f66f28cb6b051f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 24 Sep 2020 12:37:02 +1000 Subject: [PATCH 0329/3533] extmod/utime_mphal: Add generic utime.time_ns() function. It requires mp_hal_time_ns() to be provided by a port. This function allows very accurate absolute timestamps. Enabled on unix, windows, stm32, esp8266 and esp32. Signed-off-by: Damien George --- docs/library/utime.rst | 10 ++++++++-- extmod/utime_mphal.c | 6 ++++++ extmod/utime_mphal.h | 1 + ports/esp32/modutime.c | 1 + ports/esp8266/modutime.c | 1 + ports/stm32/modutime.c | 1 + ports/unix/modtime.c | 1 + tests/extmod/utime_time_ns.py | 24 ++++++++++++++++++++++++ tests/extmod/utime_time_ns.py.exp | 2 ++ 9 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/extmod/utime_time_ns.py create mode 100644 tests/extmod/utime_time_ns.py.exp diff --git a/docs/library/utime.rst b/docs/library/utime.rst index 8c222ede5f80b..86fd27b3a5ac8 100644 --- a/docs/library/utime.rst +++ b/docs/library/utime.rst @@ -216,8 +216,9 @@ Functions function returns number of seconds since a port-specific reference point in time (for embedded boards without a battery-backed RTC, usually since power up or reset). If you want to develop portable MicroPython application, you should not rely on this function - to provide higher than second precision. If you need higher precision, use - `ticks_ms()` and `ticks_us()` functions, if you need calendar time, + to provide higher than second precision. If you need higher precision, absolute + timestamps, use `time_ns()`. If relative times are acceptable then use the + `ticks_ms()` and `ticks_us()` functions. If you need calendar time, `gmtime()` or `localtime()` without an argument is a better choice. .. admonition:: Difference to CPython @@ -233,3 +234,8 @@ Functions hardware also lacks battery-powered RTC, so returns number of seconds since last power-up or from other relative, hardware-specific point (e.g. reset). + +.. function:: time_ns() + + Similar to `time()` but returns nanoseconds since the Epoch, as an integer (usually + a big integer, so will allocate on the heap). diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 6aff2cac725c2..d053cf128b871 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -99,4 +99,10 @@ STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) { } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add); +// Returns the number of nanoseconds since the Epoch, as an integer. +STATIC mp_obj_t time_time_ns(void) { + return mp_obj_new_int_from_ull(mp_hal_time_ns()); +} +MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_time_ns_obj, time_time_ns); + #endif // MICROPY_PY_UTIME_MP_HAL diff --git a/extmod/utime_mphal.h b/extmod/utime_mphal.h index 88a9ed4d3756d..57fc348832812 100644 --- a/extmod/utime_mphal.h +++ b/extmod/utime_mphal.h @@ -37,5 +37,6 @@ MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj); MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj); MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj); MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj); +MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_time_ns_obj); #endif // MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H diff --git a/ports/esp32/modutime.c b/ports/esp32/modutime.c index ac3edb4e9b76f..cf7178e0b1c2d 100644 --- a/ports/esp32/modutime.c +++ b/ports/esp32/modutime.c @@ -97,6 +97,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/ports/esp8266/modutime.c b/ports/esp8266/modutime.c index 86534722c792f..bcfbf7baf2c66 100644 --- a/ports/esp8266/modutime.c +++ b/ports/esp8266/modutime.c @@ -120,6 +120,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/ports/stm32/modutime.c b/ports/stm32/modutime.c index 2a37a130df2bb..4bce45eb33315 100644 --- a/ports/stm32/modutime.c +++ b/ports/stm32/modutime.c @@ -144,6 +144,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index 5343e479c7d92..e08409e20e3e9 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -219,6 +219,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) }, diff --git a/tests/extmod/utime_time_ns.py b/tests/extmod/utime_time_ns.py new file mode 100644 index 0000000000000..8f3890f1cb85e --- /dev/null +++ b/tests/extmod/utime_time_ns.py @@ -0,0 +1,24 @@ +# test utime.time_ns() + +try: + import utime + + utime.sleep_us + utime.time_ns +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +t0 = utime.time_ns() +utime.sleep_us(1000) +t1 = utime.time_ns() + +# Check that time_ns increases. +print(t0 < t1) + +# Check that time_ns counts correctly, but be very lenient with the upper bound (50ms). +if 950000 < t1 - t0 < 50000000: + print(True) +else: + print(t0, t1, t1 - t0) diff --git a/tests/extmod/utime_time_ns.py.exp b/tests/extmod/utime_time_ns.py.exp new file mode 100644 index 0000000000000..dbde422651c9a --- /dev/null +++ b/tests/extmod/utime_time_ns.py.exp @@ -0,0 +1,2 @@ +True +True From 843dcd4f8515c62b32b4b25dc3b01fba107622e2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 30 Sep 2020 23:34:42 +1000 Subject: [PATCH 0330/3533] py/parse: Expose rule-name printing as MICROPY_DEBUG_PARSE_RULE_NAME. So it can be enabled without modifying the source. Signed-off-by: Damien George --- py/mpconfig.h | 5 +++++ py/parse.c | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 12517316c6c80..cc83f3850d695 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -454,6 +454,11 @@ #define MICROPY_DEBUG_MP_OBJ_SENTINELS (0) #endif +// Whether to print parse rule names (rather than integers) in mp_parse_node_print +#ifndef MICROPY_DEBUG_PARSE_RULE_NAME +#define MICROPY_DEBUG_PARSE_RULE_NAME (0) +#endif + // Whether to enable a simple VM stack overflow check #ifndef MICROPY_DEBUG_VM_STACK_OVERFLOW #define MICROPY_DEBUG_VM_STACK_OVERFLOW (0) diff --git a/py/parse.c b/py/parse.c index 38cd215a9ff71..da2f5e796d9be 100644 --- a/py/parse.c +++ b/py/parse.c @@ -55,9 +55,6 @@ #define RULE_ARG_RULE (0x2000) #define RULE_ARG_OPT_RULE (0x3000) -// (un)comment to use rule names; for debugging -// #define USE_RULE_NAME (1) - // *FORMAT-OFF* enum { @@ -192,7 +189,7 @@ static const size_t FIRST_RULE_WITH_OFFSET_ABOVE_255 = #undef DEF_RULE_NC 0; -#if USE_RULE_NAME +#if MICROPY_DEBUG_PARSE_RULE_NAME // Define an array of rule names corresponding to each rule STATIC const char *const rule_name_table[] = { #define DEF_RULE(rule, comp, kind, ...) #rule, @@ -410,7 +407,7 @@ void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t ind #endif } else { size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); - #if USE_RULE_NAME + #if MICROPY_DEBUG_PARSE_RULE_NAME mp_printf(print, "%s(%u) (n=%u)\n", rule_name_table[MP_PARSE_NODE_STRUCT_KIND(pns)], (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); #else mp_printf(print, "rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); From 817b80a102a413b6458ea391bb2c463aff99672e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 30 Sep 2020 23:35:55 +1000 Subject: [PATCH 0331/3533] unix/variants: Enable MICROPY_DEBUG_PARSE_RULE_NAME on coverage build. Signed-off-by: Damien George --- .../unix/variants/coverage/mpconfigvariant.h | 1 + tests/cmdline/cmd_parsetree.py.exp | 22 +++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 3de5294322502..942117608fab1 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -30,6 +30,7 @@ #define MICROPY_VFS (1) #define MICROPY_PY_UOS_VFS (1) +#define MICROPY_DEBUG_PARSE_RULE_NAME (1) #define MICROPY_OPT_MATH_FACTORIAL (1) #define MICROPY_FLOAT_HIGH_QUALITY_HASH (1) #define MICROPY_ENABLE_SCHEDULER (1) diff --git a/tests/cmdline/cmd_parsetree.py.exp b/tests/cmdline/cmd_parsetree.py.exp index 42a8228fb77e1..e64f4f782951a 100644 --- a/tests/cmdline/cmd_parsetree.py.exp +++ b/tests/cmdline/cmd_parsetree.py.exp @@ -1,31 +1,31 @@ ---------------- -[ 4] rule(1) (n=9) +[ 4] \(rule\|file_input_2\)(1) (n=9) tok(4) -[ 4] rule(22) (n=4) +[ 4] \(rule\|for_stmt\)(22) (n=4) id(i) -[ 4] rule(45) (n=1) +[ 4] \(rule\|atom_paren\)(45) (n=1) NULL -[ 5] rule(8) (n=0) +[ 5] \(rule\|pass_stmt\)(8) (n=0) NULL -[ 6] rule(5) (n=2) +[ 6] \(rule\|expr_stmt\)(5) (n=2) id(a) tok(14) -[ 7] rule(5) (n=2) +[ 7] \(rule\|expr_stmt\)(5) (n=2) id(b) str(str) -[ 8] rule(5) (n=2) +[ 8] \(rule\|expr_stmt\)(5) (n=2) id(c) [ 8] literal \.\+ -[ 9] rule(5) (n=2) +[ 9] \(rule\|expr_stmt\)(5) (n=2) id(d) bytes(bytes) -[ 10] rule(5) (n=2) +[ 10] \(rule\|expr_stmt\)(5) (n=2) id(e) [ 10] literal \.\+ -[ 11] rule(5) (n=2) +[ 11] \(rule\|expr_stmt\)(5) (n=2) id(f) [ 11] literal \.\+ -[ 12] rule(5) (n=2) +[ 12] \(rule\|expr_stmt\)(5) (n=2) id(g) int(123) ---------------- From 0fff2e03fe07471997a6df6f92c6960cfd225dc0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Oct 2020 15:46:13 +1000 Subject: [PATCH 0332/3533] stm32/Makefile: Allow boards to extend SRC_C, SRC_O and OBJ variables. Signed-off-by: Damien George --- ports/stm32/Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 7b68299e04f70..cf3a589ca9087 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -259,7 +259,7 @@ DRIVERS_SRC_C = $(addprefix drivers/,\ dht/dht.c \ ) -SRC_C = \ +SRC_C += \ main.c \ stm32_it.c \ usbd_conf.c \ @@ -330,7 +330,7 @@ SRC_C = \ adc.c \ $(wildcard $(BOARD_DIR)/*.c) -SRC_O = \ +SRC_O += \ $(STARTUP_FILE) \ $(SYSTEM_FILE) @@ -505,7 +505,6 @@ endif endif -OBJ = OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(LIBM_O) From 1dc64359dab733b3c77f07d9d79589010f6fd8e0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 15:54:58 +1000 Subject: [PATCH 0333/3533] esp32: Use path relative to root for netutils/timeutils headers. Signed-off-by: Damien George --- ports/esp32/Makefile | 3 --- ports/esp32/fatfs_port.c | 2 +- ports/esp32/machine_rtc.c | 2 +- ports/esp32/modesp32.c | 2 +- ports/esp32/modnetwork.c | 2 +- ports/esp32/network_ppp.c | 2 +- 6 files changed, 5 insertions(+), 8 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index dcf4110cfc913..be282577229c4 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -136,9 +136,6 @@ include $(SDKCONFIG) INC += -I. INC += -I$(TOP) -INC += -I$(TOP)/lib/mp-readline -INC += -I$(TOP)/lib/netutils -INC += -I$(TOP)/lib/timeutils INC += -I$(BUILD) INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include diff --git a/ports/esp32/fatfs_port.c b/ports/esp32/fatfs_port.c index cfc52853d08a9..7fce654c0997c 100644 --- a/ports/esp32/fatfs_port.c +++ b/ports/esp32/fatfs_port.c @@ -28,7 +28,7 @@ #include #include "lib/oofatfs/ff.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" DWORD get_fattime(void) { struct timeval tv; diff --git a/ports/esp32/machine_rtc.c b/ports/esp32/machine_rtc.c index 6902ce85fd2c7..1b6a71b5b4181 100644 --- a/ports/esp32/machine_rtc.c +++ b/ports/esp32/machine_rtc.c @@ -36,7 +36,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mphal.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" #include "modmachine.h" #include "machine_rtc.h" diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 8e248370ffb6d..28d1762d240de 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -41,7 +41,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mphal.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" #include "modmachine.h" #include "machine_rtc.h" #include "modesp32.h" diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 029a8d143127c..44263350409fc 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -40,7 +40,7 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/mperrno.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "esp_eth.h" #include "esp_wifi.h" #include "esp_log.h" diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index df57b817251d8..db2292ca03f7e 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -30,7 +30,7 @@ #include "py/mphal.h" #include "py/objtype.h" #include "py/stream.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modmachine.h" #include "netif/ppp/ppp.h" From 7497d891a7cfade68d8c7d26b64080c56e0579e5 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Fri, 2 Oct 2020 21:23:09 +0200 Subject: [PATCH 0334/3533] stm32/sdio: Don't change any DMA2 settings on H7 MCUs. DMA2 clock and registers should be left in their current state in the H7 build. --- ports/stm32/sdio.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/stm32/sdio.c b/ports/stm32/sdio.c index 79cc9c9a323a9..87f550e3a6f5d 100644 --- a/ports/stm32/sdio.c +++ b/ports/stm32/sdio.c @@ -76,7 +76,9 @@ void sdio_init(uint32_t irq_pri) { #endif mp_hal_delay_us(10); + #if defined(STM32F7) __HAL_RCC_DMA2_CLK_ENABLE(); // enable DMA2 peripheral + #endif NVIC_SetPriority(SDMMC1_IRQn, irq_pri); @@ -216,7 +218,9 @@ int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) { } #endif + #if defined(STM32F7) DMA2_Stream3->CR = 0; // ensure DMA is reset + #endif SDMMC1->ICR = SDMMC_STATIC_FLAGS; // clear interrupts SDMMC1->ARG = arg; SDMMC1->CMD = cmd | SDMMC_CMD_WAITRESP_0 | SDMMC_CMD_CPSMEN; @@ -296,7 +300,9 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le SDMMC1->DTIMER = 0x2000000; // about 700ms running at 48MHz SDMMC1->DLEN = (len + block_size - 1) & ~(block_size - 1); + #if defined(STM32F7) DMA2_Stream3->CR = 0; + #endif if (dma) { // prepare DMA so it's ready when the DPSM starts its transfer From 9855b9cd82f9d2e9108fd54ab7d4602134ebae1d Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Fri, 2 Oct 2020 21:44:15 +0200 Subject: [PATCH 0335/3533] stm32/sdcard: Fix H7 build when using SDMMC2. Changes are: - Fix missing IRQ handler when SDMMC2 is used instead of SDMMC1 with H7 MCUs. - Removed outdated H7 series compatibility macros. - Defined common IRQ handler macro for F4 series. --- ports/stm32/sdcard.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 9d77722302c99..7d79e9f47b1a8 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -48,12 +48,14 @@ #if defined(MICROPY_HW_SDMMC2_CK) #define SDIO SDMMC2 +#define SDMMC_IRQHandler SDMMC2_IRQHandler #define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC2_CLK_ENABLE() #define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC2_CLK_DISABLE() #define SDMMC_IRQn SDMMC2_IRQn #define SDMMC_DMA dma_SDMMC_2 #else #define SDIO SDMMC1 +#define SDMMC_IRQHandler SDMMC1_IRQHandler #define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC1_CLK_ENABLE() #define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC1_CLK_DISABLE() #define SDMMC_IRQn SDMMC1_IRQn @@ -86,8 +88,6 @@ #define SDIO_HARDWARE_FLOW_CONTROL_ENABLE SDMMC_HARDWARE_FLOW_CONTROL_ENABLE #if defined(STM32H7) -#define GPIO_AF12_SDIO GPIO_AF12_SDIO1 -#define SDIO_IRQHandler SDMMC1_IRQHandler #define SDIO_TRANSFER_CLK_DIV SDMMC_NSpeed_CLK_DIV #define SDIO_USE_GPDMA 0 #else @@ -102,6 +102,7 @@ #define SDMMC_CLK_ENABLE() __SDIO_CLK_ENABLE() #define SDMMC_CLK_DISABLE() __SDIO_CLK_DISABLE() #define SDMMC_IRQn SDIO_IRQn +#define SDMMC_IRQHandler SDIO_IRQHandler #define SDMMC_DMA dma_SDIO_0 #define SDIO_USE_GPDMA 1 #define STATIC_AF_SDMMC_CK STATIC_AF_SDIO_CK @@ -398,21 +399,11 @@ STATIC void sdmmc_irq_handler(void) { } } -#if !defined(MICROPY_HW_SDMMC2_CK) -void SDIO_IRQHandler(void) { - IRQ_ENTER(SDIO_IRQn); +void SDMMC_IRQHandler(void) { + IRQ_ENTER(SDMMC_IRQn); sdmmc_irq_handler(); - IRQ_EXIT(SDIO_IRQn); + IRQ_EXIT(SDMMC_IRQn); } -#endif - -#if defined(STM32F7) -void SDMMC2_IRQHandler(void) { - IRQ_ENTER(SDMMC2_IRQn); - sdmmc_irq_handler(); - IRQ_EXIT(SDMMC2_IRQn); -} -#endif STATIC void sdcard_reset_periph(void) { // Fully reset the SDMMC peripheral before calling HAL SD DMA functions. From 7c76a2dfcffa853e73464434861af185b3e5a9f4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 1 Oct 2020 14:47:00 +1000 Subject: [PATCH 0336/3533] stm32/rfcore: Add Python API for basic rfcore operations. The new functions provide FUS/WS status, version and SYS HCI commands: - stm.rfcore_status() - stm.rfcore_fw_version(fw_id) - stm.rfcore_sys_hci(ogf, ocf, cmd) --- ports/stm32/modstm.c | 7 +++ ports/stm32/rfcore.c | 103 +++++++++++++++++++++++++++++++++++-------- ports/stm32/rfcore.h | 4 ++ 3 files changed, 96 insertions(+), 18 deletions(-) diff --git a/ports/stm32/modstm.c b/ports/stm32/modstm.c index 418b8bde2a14e..3f4f33979afd5 100644 --- a/ports/stm32/modstm.c +++ b/ports/stm32/modstm.c @@ -30,6 +30,7 @@ #include "py/obj.h" #include "py/objint.h" #include "extmod/machine_mem.h" +#include "rfcore.h" #include "portmodules.h" #if MICROPY_PY_STM @@ -44,6 +45,12 @@ STATIC const mp_rom_map_elem_t stm_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, #include "genhdr/modstm_const.h" + + #if defined(STM32WB) + { MP_ROM_QSTR(MP_QSTR_rfcore_status), MP_ROM_PTR(&rfcore_status_obj) }, + { MP_ROM_QSTR(MP_QSTR_rfcore_fw_version), MP_ROM_PTR(&rfcore_fw_version_obj) }, + { MP_ROM_QSTR(MP_QSTR_rfcore_sys_hci), MP_ROM_PTR(&rfcore_sys_hci_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(stm_module_globals, stm_module_globals_table); diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index dc23bbe0b2a69..436042cc2ecc6 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -30,6 +30,7 @@ #include "py/mperrno.h" #include "py/mphal.h" +#include "py/runtime.h" #include "rtc.h" #include "rfcore.h" @@ -66,9 +67,16 @@ #define HCI_EVENT_COMMAND_COMPLETE (0x0E) // -#define SYS_ACK_TIMEOUT_MS (250) +// There can be quite long delays during firmware update. +#define SYS_ACK_TIMEOUT_MS (1000) + #define BLE_ACK_TIMEOUT_MS (250) +// AN5185 +#define MAGIC_FUS_ACTIVE 0xA94656B9 +// AN5289 +#define MAGIC_IPCC_MEM_INCORRECT 0x3DE96F61 + typedef struct _tl_list_node_t { volatile struct _tl_list_node_t *next; volatile struct _tl_list_node_t *prev; @@ -267,10 +275,10 @@ void ipcc_init(uint32_t irq_pri) { /******************************************************************************/ // Transport layer HCI interface -STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { +STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { const char *info; - size_t len = 0; bool applied_set_event_event_mask2_fix = false; + size_t len; switch (buf[0]) { case HCI_KIND_BT_ACL: { info = "HCI_ACL"; @@ -334,6 +342,7 @@ STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { } default: info = "HCI_UNKNOWN"; + len = 0; break; } @@ -354,6 +363,8 @@ STATIC void tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { #else (void)info; #endif + + return len; } STATIC void tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { @@ -397,7 +408,7 @@ STATIC void tl_check_msg_ble(volatile tl_list_node_t *head, parse_hci_info_t *pa } } -STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opcode, size_t len, const uint8_t *buf) { +STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opcode, const uint8_t *buf, size_t len) { tl_list_node_t *n = (tl_list_node_t *)cmd; n->next = n; n->prev = n; @@ -407,11 +418,19 @@ STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opco cmd[11] = len; memcpy(&cmd[12], buf, len); + #if HCI_TRACE + printf("[% 8d] >HCI(", mp_hal_ticks_ms()); + for (int i = 0; i < len + 4; ++i) { + printf(":%02x", cmd[i + 8]); + } + printf(")\n"); + #endif + // Indicate that this channel is ready. LL_C1_IPCC_SetFlag_CHx(IPCC, ch); } -STATIC int tl_sys_wait_ack(const uint8_t *buf) { +STATIC ssize_t tl_sys_wait_ack(const uint8_t *buf) { uint32_t t0 = mp_hal_ticks_ms(); // C2 will clear this bit to acknowledge the request. @@ -422,14 +441,14 @@ STATIC int tl_sys_wait_ack(const uint8_t *buf) { } } - // C1-to-C2 bit cleared, so process (but ignore) the response. - tl_parse_hci_msg(buf, NULL); - return 0; + // C1-to-C2 bit cleared, so process the response (just get the length, do + // not parse any further). + return (ssize_t)tl_parse_hci_msg(buf, NULL); } -STATIC void tl_sys_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd(ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, len, buf); - tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf); +STATIC ssize_t tl_sys_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) { + tl_hci_cmd(ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, buf, len); + return tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf); } STATIC int tl_ble_wait_resp(void) { @@ -447,10 +466,9 @@ STATIC int tl_ble_wait_resp(void) { } // Synchronously send a BLE command. -STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, size_t len, const uint8_t *buf) { - tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, len, buf); +STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) { + tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, buf, len); tl_ble_wait_resp(); - } /******************************************************************************/ @@ -522,8 +540,8 @@ void rfcore_ble_init(void) { tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); // Configure and reset the BLE controller - tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), sizeof(ble_init_params), (const uint8_t *)&ble_init_params); - tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), 0, NULL); + tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params)); + tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0); } void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { @@ -573,14 +591,14 @@ void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env) SWAP_UINT8(buf[2], buf[7]); SWAP_UINT8(buf[3], buf[6]); SWAP_UINT8(buf[4], buf[5]); - tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_WRITE_CONFIG), 8, buf); // set BDADDR + tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_WRITE_CONFIG), buf, 8); // set BDADDR } } // "level" is 0x00-0x1f, ranging from -40 dBm to +6 dBm (not linear). void rfcore_ble_set_txpower(uint8_t level) { uint8_t buf[2] = { 0x00, level }; - tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), 2, buf); + tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), buf, 2); } // IPCC IRQ Handlers @@ -605,4 +623,53 @@ void IPCC_C1_RX_IRQHandler(void) { IRQ_EXIT(IPCC_C1_RX_IRQn); } +/******************************************************************************/ +// MicroPython bindings + +STATIC mp_obj_t rfcore_status(void) { + return mp_obj_new_int_from_uint(ipcc_mem_dev_info_tab.fus.table_state); +} +MP_DEFINE_CONST_FUN_OBJ_0(rfcore_status_obj, rfcore_status); + +STATIC mp_obj_t get_version_tuple(uint32_t data) { + mp_obj_t items[] = { + MP_OBJ_NEW_SMALL_INT(data >> 24), MP_OBJ_NEW_SMALL_INT(data >> 16 & 0xFF), MP_OBJ_NEW_SMALL_INT(data >> 8 & 0xFF), MP_OBJ_NEW_SMALL_INT(data >> 4 & 0xF), MP_OBJ_NEW_SMALL_INT(data & 0xF) + }; + return mp_obj_new_tuple(5, items); +} + +STATIC mp_obj_t rfcore_fw_version(mp_obj_t fw_id_in) { + if (ipcc_mem_dev_info_tab.fus.table_state == MAGIC_IPCC_MEM_INCORRECT) { + mp_raise_OSError(MP_EINVAL); + } + mp_int_t fw_id = mp_obj_get_int(fw_id_in); + bool fus_active = ipcc_mem_dev_info_tab.fus.table_state == MAGIC_FUS_ACTIVE; + uint32_t v; + if (fw_id == 0) { + // FUS + v = fus_active ? ipcc_mem_dev_info_tab.fus.fus_version : ipcc_mem_dev_info_tab.ws.fus_version; + } else { + // WS + v = fus_active ? ipcc_mem_dev_info_tab.fus.ws_version : ipcc_mem_dev_info_tab.ws.fw_version; + } + return get_version_tuple(v); +} +MP_DEFINE_CONST_FUN_OBJ_1(rfcore_fw_version_obj, rfcore_fw_version); + +STATIC mp_obj_t rfcore_sys_hci(mp_obj_t ogf_in, mp_obj_t ocf_in, mp_obj_t cmd_in) { + if (ipcc_mem_dev_info_tab.fus.table_state == MAGIC_IPCC_MEM_INCORRECT) { + mp_raise_OSError(MP_EINVAL); + } + mp_int_t ogf = mp_obj_get_int(ogf_in); + mp_int_t ocf = mp_obj_get_int(ocf_in); + mp_buffer_info_t bufinfo = {0}; + mp_get_buffer_raise(cmd_in, &bufinfo, MP_BUFFER_READ); + ssize_t len = tl_sys_hci_cmd_resp(HCI_OPCODE(ogf, ocf), bufinfo.buf, bufinfo.len); + if (len < 0) { + mp_raise_OSError(-len); + } + return mp_obj_new_bytes(ipcc_membuf_sys_cmd_buf, len); +} +MP_DEFINE_CONST_FUN_OBJ_3(rfcore_sys_hci_obj, rfcore_sys_hci); + #endif // defined(STM32WB) diff --git a/ports/stm32/rfcore.h b/ports/stm32/rfcore.h index fbe111e1ebdcd..39f6220a32d26 100644 --- a/ports/stm32/rfcore.h +++ b/ports/stm32/rfcore.h @@ -35,4 +35,8 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src); void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env); void rfcore_ble_set_txpower(uint8_t level); +MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj); +MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj); +MP_DECLARE_CONST_FUN_OBJ_3(rfcore_sys_hci_obj); + #endif // MICROPY_INCLUDED_STM32_RFCORE_H From 222ec1a4a86cdcfa9546c30243e4e244bec9697f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 29 Sep 2020 18:37:45 +1000 Subject: [PATCH 0337/3533] stm32/boards/NUCLEO_WB55: Add standalone WB55 FUS/WS firmware updater. This commit adds a script that can be run on-device to install FUS and WS binaries from the filesystem. Instructions for use are provided in the rfcore_firmware.py file. The commit also removes unneeded functionality from the existing rfcore.py debug script (and renames it rfcore_debug.py). --- .../{rfcore.py => rfcore_debug.py} | 119 +--- .../boards/NUCLEO_WB55/rfcore_firmware.py | 547 ++++++++++++++++++ .../boards/NUCLEO_WB55/rfcore_makefirmware.py | 79 +++ 3 files changed, 630 insertions(+), 115 deletions(-) rename ports/stm32/boards/NUCLEO_WB55/{rfcore.py => rfcore_debug.py} (74%) create mode 100644 ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py create mode 100755 ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_debug.py similarity index 74% rename from ports/stm32/boards/NUCLEO_WB55/rfcore.py rename to ports/stm32/boards/NUCLEO_WB55/rfcore_debug.py index cfe315605e541..4dbead0acc63d 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_debug.py @@ -27,85 +27,16 @@ # mechanism in the WB55, and works with the memory layout configured in # ports/stm32/rfcore.c -- i.e. it expects that rfcore_init() has been run. -# At this stage this is useful for debugging, but can be extended to support -# FUS/WS firmware updates. # e.g. # ../../tools/pyboard.py --device /dev/ttyACM0 boards/NUCLEO_WB55/rfcore.py # to print out SRAM2A, register state and FUS/WS info. +# +# The `stm` module provides some helper functions to access rfcore functionality. +# See rfcore_firmware.py for more information. from machine import mem8, mem16, mem32 -import time, struct import stm - -def addressof(buf): - assert type(buf) is bytearray - return mem32[id(buf) + 12] - - -class Flash: - FLASH_KEY1 = 0x45670123 - FLASH_KEY2 = 0xCDEF89AB - - def wait_not_busy(self): - while mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16: - machine.idle() - - def unlock(self): - mem32[stm.FLASH + stm.FLASH_KEYR] = Flash.FLASH_KEY1 - mem32[stm.FLASH + stm.FLASH_KEYR] = Flash.FLASH_KEY2 - - def lock(self): - mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK - - def erase_page(self, page): - print("erase", page) - assert 0 <= page <= 255 # 1MiB range (4k page) - self.wait_not_busy() - cr = page << 3 | 1 << 1 # PNB # PER - mem32[stm.FLASH + stm.FLASH_CR] = cr - mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT - self.wait_not_busy() - mem32[stm.FLASH + stm.FLASH_CR] = 0 - - def write(self, addr, buf): - assert len(buf) % 4 == 0 - self.wait_not_busy() - cr = 1 << 0 # PG - mem32[stm.FLASH + stm.FLASH_CR] = cr - buf_addr = addressof(buf) - off = 0 - while off < len(buf): - mem32[addr + off] = mem32[buf_addr + off] - off += 4 - if off % 8 == 0: - self.wait_not_busy() - if off % 8: - mem32[addr + off] = 0 - self.wait_not_busy() - mem32[stm.FLASH + stm.FLASH_CR] = 0 - - -def copy_file_to_flash(filename, addr): - flash = Flash() - flash.unlock() - try: - with open(filename, "rb") as f: - buf = bytearray(4096) - while 1: - sz = f.readinto(buf) - if sz == 0: - break - print("write", hex(addr), sz) - flash.erase_page((addr - 0x08000000) // 4096) - print("done e") - flash.write(addr, buf) - print("done") - addr += 4096 - finally: - flash.lock() - - SRAM2A_BASE = const(0x2003_0000) # for vendor OGF @@ -205,49 +136,6 @@ def ipcc_init(): print("BLE: 0x%08x 0x%08x 0x%08x" % (BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE)) -def tl_list_init(addr): - mem32[addr] = addr # next - mem32[addr + 4] = addr # prev - - -def tl_list_append(head, n): - sram2a_dump(1024) - print("Appending 0x%08x to 0x%08x" % (head, n)) - # item->next = head - mem32[n] = head - # item->prev = head->prev - mem32[n + 4] = mem32[head + 4] - # head->prev->next = item - mem32[mem32[head + 4]] = n - # head->prev = item - mem32[head + 4] = n - - -def tl_list_unlink(n): - # next = item->next - next = mem32[n] - # prev = item->prev - prev = mem32[n + 4] - # prev->next = item->next - mem32[prev] = next - # item->next->prev = prev - mem32[next + 4] = prev - - return next - - -def tl_list_dump(head): - print( - "list(%08x, %08x, %08x):" % (head, mem32[head] & 0xFFFFFFFF, mem32[head + 4] & 0xFFFFFFFF), - end="", - ) - cur = mem32[head] - while cur != head: - print(" %08x" % (cur & 0xFFFFFFFF), end="") - cur = mem32[cur] - print() - - def fus_active(): return get_ipcc_table_word(TABLE_DEVICE_INFO, 0) == MAGIC_FUS_ACTIVE @@ -346,3 +234,4 @@ def ipcc_state(): ipcc_init() info() dev_info() +ipcc_state() diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py new file mode 100644 index 0000000000000..53c96a552a34c --- /dev/null +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -0,0 +1,547 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2020 Damien P. George +# Copyright (c) 2020 Jim Mussared +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# This script provides helpers for working with the FUS/WS firmware on the WB55. +# It can be frozen into the MicroPython firmware (via manifest.py) +# +# The current FUS and WS firmware version and state can be queried via the +# `stm` module, e.g. +# stm.rfcore_status() (returns the first word of the device info table) +# stm.rfcore_fw_version(id) (returns a 5-tuple indicating fw version; id is: 0=FUS, 1=WS) +# stm.rfcore_sys_hci(ogf, ocf, cmd_buf) (synchronously execute HCI command on SYS channel) +# +# To perform a firmware update: +# +# 1. Generate "obfuscated" binary images using rfcore_makefirmware.py +# ./boards/NUCLEO_WB55/rfcore_makefirmware.py ~/src/github.com/STMicroelectronics/STM32CubeWB/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x/ /tmp +# This will generate /tmp/{fus_102,fus_110,ws_ble_hci}.bin +# +# 2. Copy required files to the device filesystem. +# In general, it's always safe to copy all three files and the updater will +# figure out what needs to be done. This is the recommended option. +# However, if you already have the latest FUS (1.1.0) installed, then just the +# WS firmware is required. +# If a FUS binary is present, then the existing WS will be removed so it's a good +# idea to always include the WS binary if updating FUS. +# Note that a WS binary will not be installed unless FUS 1.1.0 is installed. +# +# 3. Ensure boot.py calls `rfcore_firmware.resume()`. +# The WB55 will reset several times during the firmware update process, so this +# script manages the update state using RTC backup registers. +# `rfcore_firmware.resume()` will continue the update operation on startup to +# resume any in-progress update operation, and either trigger another reset, or +# return 0 to indicate that the operation completed successfully, or a reason +# code (see REASON_* below) to indicate failure. +# +# 4. Call rfcore_firmware.check_for_updates() to start the update process. +# The device will then immediately reboot and when the firmware update completes, +# the status will be returned from rfcore_firmware.resume(). See the REASON_ codes below. +# You can use the built-in stm.rfcore_fw_version() to query the installed version +# from your application code. + +import struct, os +import machine, stm +from micropython import const + +_OGF_VENDOR = const(0x3F) + +_OCF_FUS_GET_STATE = const(0x52) +_OCF_FUS_FW_UPGRADE = const(0x54) +_OCF_FUS_FW_DELETE = const(0x55) +_OCF_FUS_START_WS = const(0x5A) +_OCF_BLE_INIT = const(0x66) + +_HCI_KIND_VENDOR_RESPONSE = const(0x11) + + +# The firmware updater will search all of flash for the image to install, so +# it's important that the file doesn't exist anywhere on the filesystem and +# that the updater only finds the version that we copy into the reserved area. +# Otherwise it will find matching headers/footers in the flash filesystem and +# get confused leading to either "FUS_STATE_IMG_NOT_AUTHENTIC" or (worse) +# corrupting the FUS. +# See footnote [1] referenced by Table 9 in AN5185 - Rev 4 -- the address +# passed to FUS_FW_UPGRADE is ignored (implying that it must be searching the +# flash). This requires that the firmware files have been pre-processed by +# rfcore_makefirmware.py and this key must match the one there. +_OBFUSCATION_KEY = const(0x0573B55AA) + +# On boards using the internal flash filesystem, this must match the +# `_flash_fs_end` symbol defined by the linker script (boards/stm32wb55xg.ld). +# We erase everything from here until the start of the secure area (defined by +# SFSA) just to ensure that no other fragments of firmware files are left +# behind. On boards with external flash, this just needs to ensure that it +# includes any regions that may contain partial firmware data. +# This is non-const so it can be override. +STAGING_AREA_START = 0x80C0000 + +# First word of device info table indicating FUS state (returned by `stm.rfcore_status()`). +_MAGIC_FUS_ACTIVE = const(0xA94656B9) # AN5185 +_MAGIC_IPCC_MEM_INCORRECT = const(0x3DE96F61) # # AN5185 + +# Argument to `stm.rfcore_fw_version()`. +_FW_VERSION_FUS = const(0) +_FW_VERSION_WS = const(1) + +# No firmware update in progress. Boot normally. +_STATE_IDLE = const(0) + +# A previous firmware update failed. Will return reason code from resume(). +_STATE_FAILED = const(1) + +# Trying to get into the FUS. Keep issuing GET_STATE until the FUS is active. +_STATE_WAITING_FOR_FUS = const(2) + +# Trying to get into the WS. Keep issuing START_WS until the WS is active (or fails). +_STATE_WAITING_FOR_WS = const(3) + +# FW_DELETE has been issued. Waiting for the WS version to report zero. +_STATE_DELETING_WS = const(4) + +# Flash copy has started for FUS/WS. If a reboot occurs, then fail. +_STATE_COPYING_FUS = const(5) +_STATE_COPYING_WS = const(6) + +# Flash write fully completed, ready for install. +_STATE_COPIED_FUS = const(7) +_STATE_COPIED_WS = const(8) + +# Check for next update to perform. +# Either we've just gotten into the FUS, or the first update in a sequence +# has completed. (e.g. FUS done, now do WS). +_STATE_CHECK_UPDATES = const(9) + +# Installation has started, keep polling GET_STATE. +_STATE_INSTALLING_WS = const(10) +_STATE_INSTALLING_FUS = const(11) + +# Update completed successfully. +REASON_OK = const(0) +# The device reset during flash copy. Possibly WS still installed. +REASON_FLASH_COPY_FAILED = const(1) +# Unable to start the WS after firmware update. +REASON_NO_WS = const(2) +# Copying FUS image to staging area caused FUS to fail. +REASON_FLASH_FUS_BAD_STATE = const(3) +# Copying WS image to staging area caused FUS to fail. +REASON_FLASH_WS_BAD_STATE = const(4) +# Cannot get into the FUS. Perhaps rfcore misconfigured. +REASON_FUS_NOT_RESPONDING = const(5) +# After a FUS install, unable to get back to the FUS. +REASON_FUS_NOT_RESPONDING_AFTER_FUS = const(6) +# After a WS install, unable to get back to the FUS. +REASON_FUS_NOT_RESPONDING_AFTER_WS = const(7) +# Unable to query rfcore version/active. +REASON_RFCORE_NOT_CONFIGURED = const(8) +# The WS deletion didn't have any effect. +REASON_WS_STILL_PRESENT = const(9) +# FUS refused to delete the WS. +REASON_WS_DELETION_FAILED = const(10) +# FUS returned a specific code for a FUS update. +# See AN5185 Rev 4, Table 12. Reason between 0x00-0x11 will be added. +REASON_FUS_VENDOR = const(0x10) +# FUS returned a specific code for a WS update. Values as for the FUS update. +REASON_WS_VENDOR = const(0x30) + +# FUS 1.0.2 must be installed before FUS 1.1.0 can be installed. +# A factory Nucleo board has FUS (0, 5, 3, 0, 0) and WS (0, 5, 1, 0, 0). +_FUS_VERSION_102 = (1, 0, 2, 0, 0) +_FUS_VERSION_110 = (1, 1, 0, 0, 0) +_PATH_FUS_102 = "fus_102.bin" +_PATH_FUS_110 = "fus_110.bin" +_PATH_WS_BLE_HCI = "ws_ble_hci.bin" + +# This address is correct for versions up to v1.8 (assuming existing firmware deleted). +# Note any address from the end of the filesystem to the SFSA would be fine, but if +# the FUS is fixed in the future to use the specified address then these are the "correct" +# ones. +_ADDR_FUS = 0x080EC000 +_ADDR_WS_BLE_HCI = 0x080DC000 + + +def log(msg, *args, **kwargs): + print("[rfcore update]", msg.format(*args, **kwargs)) + + +class _Flash: + _FLASH_KEY1 = 0x45670123 + _FLASH_KEY2 = 0xCDEF89AB + + def wait_not_busy(self): + while machine.mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16: + machine.idle() + + def unlock(self): + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 + + def lock(self): + machine.mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK + + def erase_page(self, page): + assert 0 <= page <= 255 # 1MiB range (4k page) + self.wait_not_busy() + cr = page << 3 | 1 << 1 # PNB # PER + machine.mem32[stm.FLASH + stm.FLASH_CR] = cr + machine.mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT + self.wait_not_busy() + machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 + + def write(self, addr, buf, sz, key=0): + assert sz % 4 == 0 + self.wait_not_busy() + cr = 1 << 0 # PG + machine.mem32[stm.FLASH + stm.FLASH_CR] = cr + off = 0 + while off < sz: + v = (buf[off]) | (buf[off + 1] << 8) | (buf[off + 2] << 16) | (buf[off + 3] << 24) + machine.mem32[addr + off] = v ^ key + off += 4 + if off % 8 == 0: + self.wait_not_busy() + if off % 8: + machine.mem32[addr + off] = 0 + self.wait_not_busy() + machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 + + +def _copy_file_to_flash(filename, addr): + flash = _Flash() + flash.unlock() + try: + # Erase the entire staging area in flash. + erase_addr = STAGING_AREA_START + sfr_sfsa = machine.mem32[stm.FLASH + stm.FLASH_SFR] & 0xFF + erase_limit = 0x08000000 + sfr_sfsa * 4096 + while erase_addr < erase_limit: + flash.erase_page((erase_addr - 0x08000000) // 4096) + erase_addr += 4096 + + # Write the contents of the firmware (note flash.write will apply the + # XOR de-obfuscation). + with open(filename, "rb") as f: + buf = bytearray(4096) + + while 1: + sz = f.readinto(buf) + if sz == 0: + break + flash.write(addr, buf, sz, _OBFUSCATION_KEY) + addr += 4096 + + finally: + flash.lock() + + +def _parse_vendor_response(data): + assert len(data) >= 7 + assert data[0] == _HCI_KIND_VENDOR_RESPONSE + assert data[1] == 0x0E + # assert data[3] == 0xff # "Num HCI" -- docs say 0xff, but we see 0x01 + op = (data[5] << 8) | data[4] + return (op >> 10, op & 0x3FF, data[6], data[7] if len(data) > 7 else 0) + + +def _run_sys_hci_cmd(ogf, ocf, buf=b""): + try: + ogf_out, ocf_out, status, result = _parse_vendor_response( + stm.rfcore_sys_hci(ogf, ocf, buf) + ) + except OSError: + # Timeout or FUS not active. + return (0xFF, 0xFF) + assert ogf_out == ogf + assert ocf_out == ocf + return (status, result) + + +def fus_get_state(): + return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_GET_STATE) + + +def fus_is_idle(): + return fus_get_state() == (0, 0) + + +def fus_start_ws(): + return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_START_WS) + + +def _fus_fwdelete(): + return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_FW_DELETE) + + +def _fus_run_fwupgrade(addr): + # Note: Address is ignored by the FUS (see comments above). + return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_FW_UPGRADE, struct.pack(" FUS 1.1.0 -> WS (depending on what's available). + elif _STATE_id == _STATE_CHECK_UPDATES: + log("Checking for updates") + fus_version = stm.rfcore_fw_version(_FW_VERSION_FUS) + log("FUS version {}", fus_version) + if fus_version < _FUS_VERSION_102: + log("Factory FUS detected") + if _stat_and_start_copy( + _PATH_FUS_102, _ADDR_FUS, _STATE_COPYING_FUS, _STATE_COPIED_FUS + ): + continue + elif fus_version >= _FUS_VERSION_102 and fus_version < _FUS_VERSION_110: + log("FUS 1.0.2 detected") + if _stat_and_start_copy( + _PATH_FUS_110, _ADDR_FUS, _STATE_COPYING_FUS, _STATE_COPIED_FUS + ): + continue + else: + log("FUS is up-to-date") + + if fus_version >= _FUS_VERSION_110: + if _stat_and_start_copy( + _PATH_WS_BLE_HCI, _ADDR_WS_BLE_HCI, _STATE_COPYING_WS, _STATE_COPIED_WS + ): + continue + else: + log("No WS updates available") + else: + # Don't attempt to install WS if we're running an old FUS. + log("Need latest FUS to install WS") + + # Attempt to go back to WS. + # Either this will fail (because WS was removed due to FUS install), or + # this whole thing was a no-op and we should be fine to restart WS. + _write_state(_STATE_WAITING_FOR_WS) + + # This shouldn't happen - the flash write should always complete and + # move straight onto the COPIED state. Failure here indicates that + # the rfcore is misconfigured or the WS firmware was not deleted first. + elif _STATE_id == _STATE_COPYING_FUS or _STATE_id == _STATE_COPYING_WS: + log("Flash copy failed mid-write") + _write_failure_state(REASON_FLASH_COPY_FAILED) + + # Flash write completed, we should immediately see GET_STATE return 0,0 + # so we can start the FUS install. + elif _STATE_id == _STATE_COPIED_FUS: + if fus_is_idle(): + log("FUS copy complete, installing") + _write_state(_STATE_INSTALLING_FUS) + _fus_run_fwupgrade(_ADDR_FUS) + else: + log("FUS copy bad state") + _write_failure_state(REASON_FLASH_FUS_BAD_STATE) + + # Keep polling the state until we see a 0,0 (success) or non-transient + # error. In general we should expect to see (16,0) several times, + # followed by a (255,0), followed by (0, 0). + elif _STATE_id == _STATE_INSTALLING_FUS: + log("Installing FUS...") + status, result = fus_get_state() + log("FUS state: {} {}", status, result) + if 0x20 <= status <= 0x2F and result == 0: + # FUS_STATE_FUS_UPGRD_ONGOING + log("FUS still in progress...") + elif 0x10 <= status <= 0x1F and result == 0x11: + # FUS_STATE_FW_UPGRD_ONGOING and FUS_FW_ROLLBACK_ERROR + # Confusingly this is a "FW_UPGRD" (0x10) not "FUS_UPRD" (0x20). + log("Attempted to install same FUS version... re-querying FUS state to resume.") + elif status == 0: + log("FUS update successful") + _write_state(_STATE_CHECK_UPDATES) + # Need to force a reset after FUS install otherwise a subsequent flash copy will fail. + machine.reset() + elif result == 0: + # See below (for equivalent path for WS install -- we + # sometimes see (255,0) right at the end). + log("Re-querying FUS state...") + elif result == 0xFF: + _write_failure_state(REASON_FUS_NOT_RESPONDING_AFTER_FUS) + else: + _write_failure_state(REASON_FUS_VENDOR + result) + + # Keep polling the state until we see 0,0 or failure (1,0). Any other + # result means retry (but the docs say that 0 and 1 are the only + # status values). + elif _STATE_id == _STATE_DELETING_WS: + log("Deleting WS...") + status, result = fus_get_state() + log("FUS state: {} {}", status, result) + if status == 0: + if sum(stm.rfcore_fw_version(_FW_VERSION_WS)) == 0: + log("WS deletion complete") + _write_state(_STATE_CHECK_UPDATES) + else: + log("WS deletion no effect") + _write_failure_state(REASON_WS_STILL_PRESENT) + elif status == 1: + log("WS deletion failed") + _write_failure_state(REASON_WS_DELETION_FAILED) + + # As for _STATE_COPIED_FUS above. We should immediately see 0,0. + elif _STATE_id == _STATE_COPIED_WS: + if fus_is_idle(): + log("WS copy complete, installing") + _write_state(_STATE_INSTALLING_WS) + _fus_run_fwupgrade(_ADDR_WS_BLE_HCI) + else: + log("WS copy bad state") + _write_failure_state(REASON_FLASH_WS_BAD_STATE) + + # As for _STATE_INSTALLING_FUS above. + elif _STATE_id == _STATE_INSTALLING_WS: + log("Installing WS...") + status, result = fus_get_state() + log("FUS state: {} {}", status, result) + if 0x10 <= status <= 0x1F and result == 0: + # FUS_STATE_FW_UPGRD_ONGOING + log("WS still in progress...") + elif 0x10 <= status <= 0x1F and result == 0x11: + # FUS_FW_ROLLBACK_ERROR + log("Attempted to install same WS version... re-querying FUS state to resume.") + elif status == 0: + log("WS update successful") + _write_state(_STATE_WAITING_FOR_WS) + elif result == 0: + # We get a error response with no payload sometimes at the end + # of the update (this is not in AN5185). Re-try the GET_STATE. + # The same thing happens transitioning from WS to FUS mode. + # The actual HCI response has no payload, the result=0 comes from + # _parse_vendor_response above when len=7. + log("Re-querying FUS state...") + elif result == 0xFF: + # This is specifically a failure sending the HCI command. + _write_failure_state(REASON_FUS_NOT_RESPONDING_AFTER_WS) + else: + _write_failure_state(REASON_WS_VENDOR + result) + + +# Start a firmware update. +# This will immediately trigger a reset and start the update process on boot. +def check_for_updates(): + log("Starting firmware update") + _write_state(_STATE_WAITING_FOR_FUS) + machine.reset() diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py new file mode 100755 index 0000000000000..23f3d20f0c9ce --- /dev/null +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2020 Jim Mussared +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# This script obfuscates the ST wireless binaries so they can be safely copied +# to the flash filesystem and not be accidentally discovered by the FUS during +# an update. See more information (and the corresponding de-obfuscation) in +# rfcore_firmware.py as well as instructions on how to use. + +import os +import struct +import sys + +# Must match rfcore_firmware.py. +_OBFUSCATION_KEY = 0x0573B55AA + +_FIRMWARE_FILES = { + "stm32wb5x_FUS_fw_1_0_2.bin": "fus_102.bin", + "stm32wb5x_FUS_fw.bin": "fus_110.bin", + "stm32wb5x_BLE_HCILayer_fw.bin": "ws_ble_hci.bin", +} + + +def main(src_path, dest_path): + for src_file, dest_file in _FIRMWARE_FILES.items(): + src_file = os.path.join(src_path, src_file) + dest_file = os.path.join(dest_path, dest_file) + if not os.path.exists(src_file): + print("Unable to find: {}".format(src_file)) + continue + sz = 0 + with open(src_file, "rb") as src: + with open(dest_file, "wb") as dest: + while True: + b = src.read(4) + if not b: + break + (v,) = struct.unpack(" Date: Fri, 9 Oct 2020 16:51:47 +1100 Subject: [PATCH 0338/3533] stm32/rfcore: Update to support WS=1.9.0.0.4. This WS update to 1.9.0.0.4 broke the workaround used in rfcore for OCF_CB_SET_EVENT_MASK2, so fix it to support WS 1.8 and 1.9. --- ports/stm32/rfcore.c | 45 +++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 436042cc2ecc6..3850a17dae642 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -66,6 +66,7 @@ #define HCI_KIND_VENDOR_EVENT (0x12) #define HCI_EVENT_COMMAND_COMPLETE (0x0E) // +#define HCI_EVENT_COMMAND_STATUS (0x0F) // // There can be quite long delays during firmware update. #define SYS_ACK_TIMEOUT_MS (1000) @@ -275,9 +276,19 @@ void ipcc_init(uint32_t irq_pri) { /******************************************************************************/ // Transport layer HCI interface +// The WS firmware doesn't support OCF_CB_SET_EVENT_MASK2, and fails with: +// v1.8.0.0.4 (and below): HCI_EVENT_COMMAND_COMPLETE with a non-zero status +// v1.9.0.0.4 (and above): HCI_EVENT_COMMAND_STATUS with a non-zero status +// In either case we detect the failure response and inject this response +// instead (which is HCI_EVENT_COMMAND_COMPLETE for OCF_CB_SET_EVENT_MASK2 +// with status=0). +STATIC const uint8_t set_event_event_mask2_fix_payload[] = { 0x04, 0x0e, 0x04, 0x01, 0x63, 0x0c, 0x00 }; + STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { const char *info; - bool applied_set_event_event_mask2_fix = false; + #if HCI_TRACE + int applied_set_event_event_mask2_fix = 0; + #endif size_t len; switch (buf[0]) { case HCI_KIND_BT_ACL: { @@ -300,10 +311,12 @@ STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { uint8_t status = buf[6]; if (opcode == HCI_OPCODE(OGF_CTLR_BASEBAND, OCF_CB_SET_EVENT_MASK2) && status != 0) { - // The WB doesn't support this command (despite being in CS 4.1), so pretend like - // it succeeded by replacing the final byte (status) with a zero. - applied_set_event_event_mask2_fix = true; - len -= 1; + // For WS firmware v1.8.0.0.4 and below. Reply with the "everything OK" payload. + parse->cb_fun(parse->cb_env, set_event_event_mask2_fix_payload, sizeof(set_event_event_mask2_fix_payload)); + #if HCI_TRACE + applied_set_event_event_mask2_fix = 18; + #endif + break; // Don't send the original payload. } if (opcode == HCI_OPCODE(OGF_CTLR_BASEBAND, OCF_CB_RESET) && status == 0) { @@ -313,15 +326,21 @@ STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { } } - parse->cb_fun(parse->cb_env, buf, len); + if (buf[1] == HCI_EVENT_COMMAND_STATUS && len == 7) { + uint16_t opcode = (buf[6] << 8) | buf[5]; + uint8_t status = buf[3]; - if (applied_set_event_event_mask2_fix) { - // Inject the zero status. - uint8_t data = 0; - parse->cb_fun(parse->cb_env, &data, 1); - // Restore the length for the HCI tracing below. - len += 1; + if (opcode == HCI_OPCODE(OGF_CTLR_BASEBAND, OCF_CB_SET_EVENT_MASK2) && status != 0) { + // For WS firmware v1.9.0.0.4 and higher. Reply with the "everything OK" payload. + parse->cb_fun(parse->cb_env, set_event_event_mask2_fix_payload, sizeof(set_event_event_mask2_fix_payload)); + #if HCI_TRACE + applied_set_event_event_mask2_fix = 19; + #endif + break; // Don't send the original payload. + } } + + parse->cb_fun(parse->cb_env, buf, len); } break; } @@ -356,7 +375,7 @@ STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { printf(" (reset)"); } if (applied_set_event_event_mask2_fix) { - printf(" (mask2 fix)"); + printf(" (mask2 fix %d)", applied_set_event_event_mask2_fix); } printf("\n"); From 880875bea1d825b8fa0d1c4a779ff767377f7655 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 9 Oct 2020 17:10:29 +1100 Subject: [PATCH 0339/3533] py/objdict: Add mp_const_empty_dict_obj, use it for mp_const_empty_map. --- py/map.c | 11 ----------- py/obj.h | 9 ++++++--- py/objdict.c | 12 ++++++++++++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/py/map.c b/py/map.c index 676c364da76a1..54f4b0204b87f 100644 --- a/py/map.c +++ b/py/map.c @@ -40,17 +40,6 @@ #define DEBUG_printf(...) (void)0 #endif -// Fixed empty map. Useful when need to call kw-receiving functions -// without any keywords from C, etc. -const mp_map_t mp_const_empty_map = { - .all_keys_are_qstrs = 0, - .is_fixed = 1, - .is_ordered = 1, - .used = 0, - .alloc = 0, - .table = NULL, -}; - // This table of sizes is used to control the growth of hash tables. // The first set of sizes are chosen so the allocation fits exactly in a // 4-word GC block, and it's not so important for these small values to be diff --git a/py/obj.h b/py/obj.h index 31542a7f9bd7c..6a040b77739c2 100644 --- a/py/obj.h +++ b/py/obj.h @@ -422,8 +422,6 @@ typedef enum _mp_map_lookup_kind_t { MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup } mp_map_lookup_kind_t; -extern const mp_map_t mp_const_empty_map; - static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) { assert(pos < map->alloc); return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL; @@ -685,17 +683,22 @@ extern const struct _mp_obj_bool_t mp_const_false_obj; extern const struct _mp_obj_bool_t mp_const_true_obj; #endif -// Constant objects, globally accessible: b'', (), Ellipsis, NotImplemented, GeneratorExit() +// Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit() // The below macros are for convenience only. #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj)) #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj)) #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj)) extern const struct _mp_obj_str_t mp_const_empty_bytes_obj; extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj; +extern const struct _mp_obj_dict_t mp_const_empty_dict_obj; extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj; extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj; extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj; +// Fixed empty map. Useful when calling keyword-receiving functions +// without any keywords from C, etc. +#define mp_const_empty_map (mp_const_empty_dict_obj.map) + // General API for objects // These macros are derived from more primitive ones and are used to diff --git a/py/objdict.c b/py/objdict.c index 4fa59f4634cb0..4e51f259e7ff4 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -33,6 +33,18 @@ #include "py/objtype.h" #include "py/objstr.h" +const mp_obj_dict_t mp_const_empty_dict_obj = { + .base = { .type = &mp_type_dict }, + .map = { + .all_keys_are_qstrs = 0, + .is_fixed = 1, + .is_ordered = 1, + .used = 0, + .alloc = 0, + .table = NULL, + } +}; + STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); // This is a helper function to iterate through a dictionary. The state of From b137d064e9e0bfebd2a59a9b312935031252e742 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 4 Aug 2020 14:28:06 +1000 Subject: [PATCH 0340/3533] py/objtype: Handle __dict__ attribute when type has no locals. --- py/objtype.c | 9 ++++++--- tests/basics/class_dict.py | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index 40900dc050358..7f75232941046 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1014,13 +1014,16 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (attr == MP_QSTR___dict__) { // Returns a read-only dict of the class attributes. // If the internal locals is not fixed, a copy will be created. - mp_obj_dict_t *dict = self->locals_dict; + const mp_obj_dict_t *dict = self->locals_dict; + if (!dict) { + dict = &mp_const_empty_dict_obj; + } if (dict->map.is_fixed) { dest[0] = MP_OBJ_FROM_PTR(dict); } else { dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(dict)); - dict = MP_OBJ_TO_PTR(dest[0]); - dict->map.is_fixed = 1; + mp_obj_dict_t *dict_copy = MP_OBJ_TO_PTR(dest[0]); + dict_copy->map.is_fixed = 1; } return; } diff --git a/tests/basics/class_dict.py b/tests/basics/class_dict.py index f80ded678b0f8..508ae5e2e543e 100644 --- a/tests/basics/class_dict.py +++ b/tests/basics/class_dict.py @@ -17,3 +17,8 @@ class Foo: d = Foo.__dict__ print(d["a"], d["b"]) + + +# dict of a class that has no locals_dict (return empty dict). +d = type(type('')).__dict__ +print(d is not None) From 520bb88d70893287b2c2728259cd020ddc710eaf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Oct 2020 20:09:04 +1100 Subject: [PATCH 0341/3533] stm32/boards/NUCLEO_WB55/rfcore_firmware.py: Fix flash unlock. The flash can sometimes be in an already-unlocked state, and attempting to unlock it again will cause an immediate reset. So make _Flash.unlock() check FLASH_CR_LOCK to get the current state. Also fix some magic numbers for FLASH_CR_LOCK AND FLASH_CR_STRT. The machine.reset() could be removed because it no longer crashes now that the flash unlock is fixed. Signed-off-by: Jim Mussared --- .../boards/NUCLEO_WB55/rfcore_firmware.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py index 53c96a552a34c..de98d97430445 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -189,23 +189,31 @@ class _Flash: _FLASH_KEY1 = 0x45670123 _FLASH_KEY2 = 0xCDEF89AB + _FLASH_CR_STRT_MASK = 1 << 16 + _FLASH_CR_LOCK_MASK = 1 << 31 + _FLASH_SR_BSY_MASK = 1 << 16 + def wait_not_busy(self): - while machine.mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16: + while machine.mem32[stm.FLASH + stm.FLASH_SR] & _Flash._FLASH_SR_BSY_MASK: machine.idle() def unlock(self): - machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 - machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 + if machine.mem32[stm.FLASH + stm.FLASH_CR] & _Flash._FLASH_CR_LOCK_MASK: + # Only unlock if already locked (i.e. FLASH_CR_LOCK is set). + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY1 + machine.mem32[stm.FLASH + stm.FLASH_KEYR] = _Flash._FLASH_KEY2 + else: + log("Flash was already unlocked.") def lock(self): - machine.mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK + machine.mem32[stm.FLASH + stm.FLASH_CR] = _Flash._FLASH_CR_LOCK_MASK def erase_page(self, page): assert 0 <= page <= 255 # 1MiB range (4k page) self.wait_not_busy() cr = page << 3 | 1 << 1 # PNB # PER machine.mem32[stm.FLASH + stm.FLASH_CR] = cr - machine.mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT + machine.mem32[stm.FLASH + stm.FLASH_CR] = cr | _Flash._FLASH_CR_STRT_MASK self.wait_not_busy() machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 @@ -472,8 +480,6 @@ def resume(): elif status == 0: log("FUS update successful") _write_state(_STATE_CHECK_UPDATES) - # Need to force a reset after FUS install otherwise a subsequent flash copy will fail. - machine.reset() elif result == 0: # See below (for equivalent path for WS install -- we # sometimes see (255,0) right at the end). From dfb63b56134dc054b2e0023d9a404dea749d1fee Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Oct 2020 20:11:43 +1100 Subject: [PATCH 0342/3533] stm32/boards/NUCLEO_WB55/rfcore_firmware.py: Fix bad variable name. Signed-off-by: Jim Mussared --- .../boards/NUCLEO_WB55/rfcore_firmware.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py index de98d97430445..3358b246d064c 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -370,18 +370,18 @@ def resume(): return _write_failure_state(REASON_RFCORE_NOT_CONFIGURED) while True: - _STATE_id = _read_state() + state = _read_state() - if _STATE_id == _STATE_IDLE: + if state == _STATE_IDLE: log("Firmware update complete") return 0 - elif _STATE_id == _STATE_FAILED: + elif state == _STATE_FAILED: log("Firmware update failed") return _read_failure_reason() # Keep calling GET_STATE until error or FUS. - elif _STATE_id == _STATE_WAITING_FOR_FUS: + elif state == _STATE_WAITING_FOR_FUS: log("Querying FUS state") status, result = fus_get_state() log("FUS state: {} {}", status, result) @@ -395,7 +395,7 @@ def resume(): _write_state(_STATE_CHECK_UPDATES) # Keep trying to start the WS until !fus_active() (or error). - elif _STATE_id == _STATE_WAITING_FOR_WS: + elif state == _STATE_WAITING_FOR_WS: if stm.rfcore_status() != _MAGIC_FUS_ACTIVE: log("WS active") _write_state(_STATE_IDLE) @@ -410,7 +410,7 @@ def resume(): _write_failure_state(REASON_NO_WS) # Sequence the FUS 1.0.2 -> FUS 1.1.0 -> WS (depending on what's available). - elif _STATE_id == _STATE_CHECK_UPDATES: + elif state == _STATE_CHECK_UPDATES: log("Checking for updates") fus_version = stm.rfcore_fw_version(_FW_VERSION_FUS) log("FUS version {}", fus_version) @@ -448,13 +448,13 @@ def resume(): # This shouldn't happen - the flash write should always complete and # move straight onto the COPIED state. Failure here indicates that # the rfcore is misconfigured or the WS firmware was not deleted first. - elif _STATE_id == _STATE_COPYING_FUS or _STATE_id == _STATE_COPYING_WS: + elif state == _STATE_COPYING_FUS or state == _STATE_COPYING_WS: log("Flash copy failed mid-write") _write_failure_state(REASON_FLASH_COPY_FAILED) # Flash write completed, we should immediately see GET_STATE return 0,0 # so we can start the FUS install. - elif _STATE_id == _STATE_COPIED_FUS: + elif state == _STATE_COPIED_FUS: if fus_is_idle(): log("FUS copy complete, installing") _write_state(_STATE_INSTALLING_FUS) @@ -466,7 +466,7 @@ def resume(): # Keep polling the state until we see a 0,0 (success) or non-transient # error. In general we should expect to see (16,0) several times, # followed by a (255,0), followed by (0, 0). - elif _STATE_id == _STATE_INSTALLING_FUS: + elif state == _STATE_INSTALLING_FUS: log("Installing FUS...") status, result = fus_get_state() log("FUS state: {} {}", status, result) @@ -492,7 +492,7 @@ def resume(): # Keep polling the state until we see 0,0 or failure (1,0). Any other # result means retry (but the docs say that 0 and 1 are the only # status values). - elif _STATE_id == _STATE_DELETING_WS: + elif state == _STATE_DELETING_WS: log("Deleting WS...") status, result = fus_get_state() log("FUS state: {} {}", status, result) @@ -508,7 +508,7 @@ def resume(): _write_failure_state(REASON_WS_DELETION_FAILED) # As for _STATE_COPIED_FUS above. We should immediately see 0,0. - elif _STATE_id == _STATE_COPIED_WS: + elif state == _STATE_COPIED_WS: if fus_is_idle(): log("WS copy complete, installing") _write_state(_STATE_INSTALLING_WS) @@ -518,7 +518,7 @@ def resume(): _write_failure_state(REASON_FLASH_WS_BAD_STATE) # As for _STATE_INSTALLING_FUS above. - elif _STATE_id == _STATE_INSTALLING_WS: + elif state == _STATE_INSTALLING_WS: log("Installing WS...") status, result = fus_get_state() log("FUS state: {} {}", status, result) From 893f75546c4e65ca5b72bc7ef9b91003372c4705 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Oct 2020 20:35:11 +1100 Subject: [PATCH 0343/3533] stm32/boards/NUCLEO_WB55/rfcore_firmware.py: Increase GET_STATE timeout. When installing WS firmware, the very first GET_STATE can take several seconds to respond (especially with the larger binaries like BLE_stack_full). Allows stm.rfcore_sys_hci to take an optional timeout, defaulting to SYS_ACK_TIMEOUT_MS (which is 250ms). Signed-off-by: Jim Mussared --- .../boards/NUCLEO_WB55/rfcore_firmware.py | 18 +++++++---- ports/stm32/rfcore.c | 32 +++++++++++-------- ports/stm32/rfcore.h | 2 +- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py index 3358b246d064c..b5f1d0072e349 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -180,6 +180,12 @@ _ADDR_FUS = 0x080EC000 _ADDR_WS_BLE_HCI = 0x080DC000 +# When installing the FUS/WS it can take a long time to return to the first +# GET_STATE HCI command. +# e.g. Installing stm32wb5x_BLE_Stack_full_fw.bin takes 3600ms to respond. +_INSTALLING_FUS_GET_STATE_TIMEOUT = const(1000) +_INSTALLING_WS_GET_STATE_TIMEOUT = const(6000) + def log(msg, *args, **kwargs): print("[rfcore update]", msg.format(*args, **kwargs)) @@ -272,10 +278,10 @@ def _parse_vendor_response(data): return (op >> 10, op & 0x3FF, data[6], data[7] if len(data) > 7 else 0) -def _run_sys_hci_cmd(ogf, ocf, buf=b""): +def _run_sys_hci_cmd(ogf, ocf, buf=b"", timeout=0): try: ogf_out, ocf_out, status, result = _parse_vendor_response( - stm.rfcore_sys_hci(ogf, ocf, buf) + stm.rfcore_sys_hci(ogf, ocf, buf, timeout) ) except OSError: # Timeout or FUS not active. @@ -285,8 +291,8 @@ def _run_sys_hci_cmd(ogf, ocf, buf=b""): return (status, result) -def fus_get_state(): - return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_GET_STATE) +def fus_get_state(timeout=0): + return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_GET_STATE, timeout=timeout) def fus_is_idle(): @@ -468,7 +474,7 @@ def resume(): # followed by a (255,0), followed by (0, 0). elif state == _STATE_INSTALLING_FUS: log("Installing FUS...") - status, result = fus_get_state() + status, result = fus_get_state(_INSTALLING_FUS_GET_STATE_TIMEOUT) log("FUS state: {} {}", status, result) if 0x20 <= status <= 0x2F and result == 0: # FUS_STATE_FUS_UPGRD_ONGOING @@ -520,7 +526,7 @@ def resume(): # As for _STATE_INSTALLING_FUS above. elif state == _STATE_INSTALLING_WS: log("Installing WS...") - status, result = fus_get_state() + status, result = fus_get_state(_INSTALLING_WS_GET_STATE_TIMEOUT) log("FUS state: {} {}", status, result) if 0x10 <= status <= 0x1F and result == 0: # FUS_STATE_FW_UPGRD_ONGOING diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 3850a17dae642..1fc0c9531d03b 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -68,9 +68,7 @@ #define HCI_EVENT_COMMAND_COMPLETE (0x0E) // #define HCI_EVENT_COMMAND_STATUS (0x0F) // -// There can be quite long delays during firmware update. -#define SYS_ACK_TIMEOUT_MS (1000) - +#define SYS_ACK_TIMEOUT_MS (250) #define BLE_ACK_TIMEOUT_MS (250) // AN5185 @@ -449,12 +447,14 @@ STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opco LL_C1_IPCC_SetFlag_CHx(IPCC, ch); } -STATIC ssize_t tl_sys_wait_ack(const uint8_t *buf) { +STATIC ssize_t tl_sys_wait_ack(const uint8_t *buf, mp_int_t timeout_ms) { uint32_t t0 = mp_hal_ticks_ms(); + timeout_ms = MAX(SYS_ACK_TIMEOUT_MS, timeout_ms); + // C2 will clear this bit to acknowledge the request. while (LL_C1_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_SYS)) { - if (mp_hal_ticks_ms() - t0 > SYS_ACK_TIMEOUT_MS) { + if (mp_hal_ticks_ms() - t0 > timeout_ms) { printf("tl_sys_wait_ack: timeout\n"); return -MP_ETIMEDOUT; } @@ -465,9 +465,9 @@ STATIC ssize_t tl_sys_wait_ack(const uint8_t *buf) { return (ssize_t)tl_parse_hci_msg(buf, NULL); } -STATIC ssize_t tl_sys_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) { +STATIC ssize_t tl_sys_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len, mp_int_t timeout_ms) { tl_hci_cmd(ipcc_membuf_sys_cmd_buf, IPCC_CH_SYS, 0x10, opcode, buf, len); - return tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf); + return tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf, timeout_ms); } STATIC int tl_ble_wait_resp(void) { @@ -559,7 +559,7 @@ void rfcore_ble_init(void) { tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); // Configure and reset the BLE controller - tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params)); + tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params), 0); tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0); } @@ -675,20 +675,24 @@ STATIC mp_obj_t rfcore_fw_version(mp_obj_t fw_id_in) { } MP_DEFINE_CONST_FUN_OBJ_1(rfcore_fw_version_obj, rfcore_fw_version); -STATIC mp_obj_t rfcore_sys_hci(mp_obj_t ogf_in, mp_obj_t ocf_in, mp_obj_t cmd_in) { +STATIC mp_obj_t rfcore_sys_hci(size_t n_args, const mp_obj_t *args) { if (ipcc_mem_dev_info_tab.fus.table_state == MAGIC_IPCC_MEM_INCORRECT) { mp_raise_OSError(MP_EINVAL); } - mp_int_t ogf = mp_obj_get_int(ogf_in); - mp_int_t ocf = mp_obj_get_int(ocf_in); + mp_int_t ogf = mp_obj_get_int(args[0]); + mp_int_t ocf = mp_obj_get_int(args[1]); mp_buffer_info_t bufinfo = {0}; - mp_get_buffer_raise(cmd_in, &bufinfo, MP_BUFFER_READ); - ssize_t len = tl_sys_hci_cmd_resp(HCI_OPCODE(ogf, ocf), bufinfo.buf, bufinfo.len); + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ); + mp_int_t timeout_ms = 0; + if (n_args >= 4) { + timeout_ms = mp_obj_get_int(args[3]); + } + ssize_t len = tl_sys_hci_cmd_resp(HCI_OPCODE(ogf, ocf), bufinfo.buf, bufinfo.len, timeout_ms); if (len < 0) { mp_raise_OSError(-len); } return mp_obj_new_bytes(ipcc_membuf_sys_cmd_buf, len); } -MP_DEFINE_CONST_FUN_OBJ_3(rfcore_sys_hci_obj, rfcore_sys_hci); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj, 3, 4, rfcore_sys_hci); #endif // defined(STM32WB) diff --git a/ports/stm32/rfcore.h b/ports/stm32/rfcore.h index 39f6220a32d26..6a3c85f67dfe8 100644 --- a/ports/stm32/rfcore.h +++ b/ports/stm32/rfcore.h @@ -37,6 +37,6 @@ void rfcore_ble_set_txpower(uint8_t level); MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj); MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj); -MP_DECLARE_CONST_FUN_OBJ_3(rfcore_sys_hci_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj); #endif // MICROPY_INCLUDED_STM32_RFCORE_H From 18518e26a7a92345fdcf8ad79e4c8b3a753f2d06 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Oct 2020 11:06:49 +1100 Subject: [PATCH 0344/3533] ports: Use correct in/out endpoint size in TUD_CDC_DESCRIPTOR. The last argument of TUD_CDC_DESCRIPTOR() is the endpoint size (or wMaxPacketSize), not the CDC RX buffer size (which can be larger than the endpoint size). Signed-off-by: Damien George --- ports/mimxrt/tusb_port.c | 3 ++- ports/nrf/drivers/usb/usb_descriptors.c | 3 ++- ports/samd/tusb_port.c | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ports/mimxrt/tusb_port.c b/ports/mimxrt/tusb_port.c index 70f8ef527cd0d..f6b09a362d18a 100644 --- a/ports/mimxrt/tusb_port.c +++ b/ports/mimxrt/tusb_port.c @@ -39,6 +39,7 @@ #define USBD_CDC_EP_OUT (0x02) #define USBD_CDC_EP_IN (0x82) #define USBD_CDC_CMD_MAX_SIZE (8) +#define USBD_CDC_IN_OUT_MAX_SIZE (512) #define USBD_STR_0 (0x00) #define USBD_STR_MANUF (0x01) @@ -70,7 +71,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, - USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, CFG_TUD_CDC_RX_BUFSIZE), + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), }; static const char *const usbd_desc_str[] = { diff --git a/ports/nrf/drivers/usb/usb_descriptors.c b/ports/nrf/drivers/usb/usb_descriptors.c index 3704e5d0dd180..f6724c1bc078c 100644 --- a/ports/nrf/drivers/usb/usb_descriptors.c +++ b/ports/nrf/drivers/usb/usb_descriptors.c @@ -39,6 +39,7 @@ #define USBD_CDC_EP_OUT (0x02) #define USBD_CDC_EP_IN (0x82) #define USBD_CDC_CMD_MAX_SIZE (8) +#define USBD_CDC_IN_OUT_MAX_SIZE (64) #define USBD_STR_0 (0x00) #define USBD_STR_MANUF (0x01) @@ -70,7 +71,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, - USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, CFG_TUD_CDC_RX_BUFSIZE), + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), }; static const char *const usbd_desc_str[] = { diff --git a/ports/samd/tusb_port.c b/ports/samd/tusb_port.c index 019e3a891c262..f3d417f1a182e 100644 --- a/ports/samd/tusb_port.c +++ b/ports/samd/tusb_port.c @@ -40,6 +40,7 @@ #define USBD_CDC_EP_OUT (0x02) #define USBD_CDC_EP_IN (0x82) #define USBD_CDC_CMD_MAX_SIZE (8) +#define USBD_CDC_IN_OUT_MAX_SIZE (64) #define USBD_STR_0 (0x00) #define USBD_STR_MANUF (0x01) @@ -71,7 +72,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, - USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, CFG_TUD_CDC_RX_BUFSIZE), + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), }; static const char *const usbd_desc_str[] = { From 56e0932485af51ec175c8f43432eee67d657b334 Mon Sep 17 00:00:00 2001 From: awachtler Date: Tue, 6 Oct 2020 22:19:05 +0200 Subject: [PATCH 0345/3533] tools/upip.py: Support explicit port number in host. Adding a port number other then 443 to a PyPI URL may be needed if a local server like devpi is used. --- tools/upip.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/upip.py b/tools/upip.py index 0036eac25db7b..aa8aecedfc23f 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -129,7 +129,11 @@ def url_open(url): proto, _, host, urlpath = url.split("/", 3) try: - ai = usocket.getaddrinfo(host, 443, 0, usocket.SOCK_STREAM) + port = 443 + if ":" in host: + host, port = host.split(":") + port = int(port) + ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) except OSError as e: fatal("Unable to resolve %s (no Internet?)" % host, e) # print("Address infos:", ai) @@ -147,7 +151,7 @@ def url_open(url): warn_ussl = False # MicroPython rawsocket module supports file interface directly - s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host)) + s.write("GET /%s HTTP/1.0\r\nHost: %s:%s\r\n\r\n" % (urlpath, host, port)) l = s.readline() protover, status, msg = l.split(None, 2) if status != b"200": From 3bc0ecbcd974716920b860904a47ee7f69afb717 Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Fri, 9 Oct 2020 20:40:17 +1100 Subject: [PATCH 0346/3533] docs/library/btree.rst: Correct method typo: __detitem__ to __delitem__. --- docs/library/btree.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/btree.rst b/docs/library/btree.rst index de52ef7e7e44e..ba910852102d1 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -118,7 +118,7 @@ Methods .. method:: btree.__getitem__(key) btree.get(key, default=None, /) btree.__setitem__(key, val) - btree.__detitem__(key) + btree.__delitem__(key) btree.__contains__(key) Standard dictionary methods. From 23f9439f441173dae961de4d2fe73986c166ff8f Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Sat, 10 Oct 2020 08:14:38 +1100 Subject: [PATCH 0347/3533] docs/library/machine.rst: Correct minor typo: timout to timeout. --- docs/library/machine.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/machine.rst b/docs/library/machine.rst index b580353d6b564..18dc6f2afaa59 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -79,7 +79,7 @@ Power related functions If *time_ms* is specified then this will be the maximum time in milliseconds that the sleep will last for. Otherwise the sleep can last indefinitely. - With or without a timout, execution may resume at any time if there are events + With or without a timeout, execution may resume at any time if there are events that require processing. Such events, or wake sources, should be configured before sleeping, like `Pin` change or `RTC` timeout. From cf6845b1cf4680bb2eade175aaab00428bedf8ba Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Sat, 10 Oct 2020 08:18:24 +1100 Subject: [PATCH 0348/3533] docs/library/machine.Signal.rst: Correct typo: usecases to use cases. --- docs/library/machine.Signal.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/machine.Signal.rst b/docs/library/machine.Signal.rst index 651c8c8497fd8..1e1fcb5483a86 100644 --- a/docs/library/machine.Signal.rst +++ b/docs/library/machine.Signal.rst @@ -55,7 +55,7 @@ Following is the guide when Signal vs Pin should be used: * Use Pin: If you implement a higher-level protocol or bus to communicate with more complex devices. -The split between Pin and Signal come from the usecases above and the +The split between Pin and Signal come from the use cases above and the architecture of MicroPython: Pin offers the lowest overhead, which may be important when bit-banging protocols. But Signal adds additional flexibility on top of Pin, at the cost of minor overhead (much smaller From 4842060366a88d8f50155538ce5fc3a12c8c709a Mon Sep 17 00:00:00 2001 From: Howard Lovatt Date: Sat, 10 Oct 2020 08:31:00 +1100 Subject: [PATCH 0349/3533] docs/library/machine.Timer.rst: Add mention of constructor arguments. --- docs/library/machine.Timer.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/library/machine.Timer.rst b/docs/library/machine.Timer.rst index 2d1287f325780..9991d3aebc2fc 100644 --- a/docs/library/machine.Timer.rst +++ b/docs/library/machine.Timer.rst @@ -31,6 +31,8 @@ Constructors Construct a new timer object of the given id. Id of -1 constructs a virtual timer (if supported by a board). + + See ``init`` for parameters of initialisation. Methods ------- From 32c99174e143b45d056c83a33f8de7502a82370c Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Thu, 10 Sep 2020 17:46:25 +1000 Subject: [PATCH 0350/3533] unix/mpconfigport.h: Enable MICROPY_PY_DELATTR_SETATTR. This is a generally useful feature and because it's part of the object model it cannot be added at runtime by some loadable Python code, so enable it on the standard unix build. --- ports/unix/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index c74d2fd84aa5f..17f4895573ed5 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -87,6 +87,7 @@ #define MICROPY_VFS_POSIX_FILE (1) #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_DESCRIPTORS (1) +#define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) From 97108fce5730f2342903e55d533ef2c30ebdfc13 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 21 Oct 2020 08:54:46 +1100 Subject: [PATCH 0351/3533] esp32/mpconfigport.h: Enable MICROPY_PY_DELATTR_SETATTR. To align with unix and stm32 ports. --- ports/esp32/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 5bf0676b238e3..b63d1f89558c0 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -63,6 +63,7 @@ // control over Python builtins #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_DESCRIPTORS (1) +#define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_STR_BYTES_CMP_WARN (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) From a93d9b8c2d4f7b020a906007702e01a1485e8043 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Fri, 9 Oct 2020 22:02:46 +0200 Subject: [PATCH 0352/3533] stm32: Fix broken build when FAT FS multi-partition is disabled. --- ports/stm32/sdcard.c | 2 ++ ports/stm32/storage.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 7d79e9f47b1a8..b255ee82ca9be 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -866,7 +866,9 @@ void sdcard_init_vfs(fs_user_mount_t *vfs, int part) { vfs->base.type = &mp_fat_vfs_type; vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NATIVE | MP_BLOCKDEV_FLAG_HAVE_IOCTL; vfs->fatfs.drv = vfs; + #if MICROPY_FATFS_MULTI_PARTITION vfs->fatfs.part = part; + #endif vfs->blockdev.readblocks[0] = MP_OBJ_FROM_PTR(&pyb_sdcard_readblocks_obj); vfs->blockdev.readblocks[1] = MP_OBJ_FROM_PTR(&pyb_sdcard_obj); vfs->blockdev.readblocks[2] = MP_OBJ_FROM_PTR(sdcard_read_blocks); // native version diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 0fefcbab9063c..c8805d682986b 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -453,7 +453,9 @@ void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NATIVE | MP_BLOCKDEV_FLAG_HAVE_IOCTL; vfs->fatfs.drv = vfs; + #if MICROPY_FATFS_MULTI_PARTITION vfs->fatfs.part = 1; // flash filesystem lives on first partition + #endif vfs->blockdev.readblocks[0] = MP_OBJ_FROM_PTR(&pyb_flash_readblocks_obj); vfs->blockdev.readblocks[1] = MP_OBJ_FROM_PTR(&pyb_flash_obj); vfs->blockdev.readblocks[2] = MP_OBJ_FROM_PTR(storage_read_blocks); // native version From 581d43b7742628907e5a77115350e9eadd59092a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Oct 2020 11:54:38 +1100 Subject: [PATCH 0353/3533] stm32/usbd_cdc_interface: Check and handle CDC TX wrap-overflow. If the device is not connected over USB CDC to a host then all output to the CDC (eg initial boot messages) is written to the CDC TX buffer with wrapping, so that the most recent data is retained when the USB CDC is eventually connected (eg so the REPL banner is displayed upon connection). This commit fixes a bug in this behaviour, which was likely introduced in e4fcd216e02eef0b389c84ecd67be3114aac0a5d, where the initial data in the CDC TX buffer is repeated multiple times on first connection of the device to the host. Signed-off-by: Damien George --- ports/stm32/usbd_cdc_interface.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 7a09128527cec..a8261a958aa09 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -196,9 +196,13 @@ static uint16_t usbd_cdc_tx_send_length(usbd_cdc_itf_t *cdc) { return MIN(usbd_cdc_tx_buffer_size(cdc), to_end); } -static void usbd_cdc_tx_buffer_put(usbd_cdc_itf_t *cdc, uint8_t data) { +static void usbd_cdc_tx_buffer_put(usbd_cdc_itf_t *cdc, uint8_t data, bool check_overflow) { cdc->tx_buf[usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_in)] = data; cdc->tx_buf_ptr_in++; + if (check_overflow && usbd_cdc_tx_buffer_size(cdc) > USBD_CDC_TX_DATA_SIZE) { + cdc->tx_buf_ptr_out++; + cdc->tx_buf_ptr_out_next = cdc->tx_buf_ptr_out; + } } static uint8_t *usbd_cdc_tx_buffer_getp(usbd_cdc_itf_t *cdc, uint16_t len) { @@ -353,7 +357,7 @@ int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t } // Write data to device buffer - usbd_cdc_tx_buffer_put(cdc, buf[i]); + usbd_cdc_tx_buffer_put(cdc, buf[i], false); } usbd_cdc_try_tx(cdc); @@ -386,7 +390,7 @@ void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { } } - usbd_cdc_tx_buffer_put(cdc, buf[i]); + usbd_cdc_tx_buffer_put(cdc, buf[i], true); } usbd_cdc_try_tx(cdc); } From 6eebdbc495a52d1965d7e9255816db310041f96a Mon Sep 17 00:00:00 2001 From: Kevin Thomas Date: Sat, 3 Oct 2020 08:35:41 -0400 Subject: [PATCH 0354/3533] docs/reference/glossary.rst: Fix minor grammar error, An -> A. --- docs/reference/glossary.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/glossary.rst b/docs/reference/glossary.rst index d63f372298f9e..27a66aa76c075 100644 --- a/docs/reference/glossary.rst +++ b/docs/reference/glossary.rst @@ -177,7 +177,7 @@ Glossary typically accessible on a host PC via USB. stream - Also known as a "file-like object". An Python object which provides + Also known as a "file-like object". A Python object which provides sequential read-write access to the underlying data. A stream object implements a corresponding interface, which consists of methods like ``read()``, ``write()``, ``readinto()``, ``seek()``, ``flush()``, From 6324c3e05499a31c5a80ad58f030e693f459f294 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 10:42:57 +0200 Subject: [PATCH 0355/3533] py/scope: Name and use id_kind_type_t. The function scope_find_or_add_id used to take a scope_kind_t enum and save it in an uint8_t. Saving an enum in a uint8_t is fine, but everywhere this function is called it is not actually given a scope_kind_t but an anonymous enum instead. Let's give this enum a name and use that as the argument type. This doesn't change the generated code, but is a C type mismatch that unfortunately doesn't show up unless you enable -Wenum-conversion. --- py/scope.c | 2 +- py/scope.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/py/scope.c b/py/scope.c index 073c2a38c2c21..98e02fb53fe31 100644 --- a/py/scope.c +++ b/py/scope.c @@ -72,7 +72,7 @@ void scope_free(scope_t *scope) { m_del(scope_t, scope, 1); } -id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, scope_kind_t kind) { +id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, id_info_kind_t kind) { id_info_t *id_info = scope_find(scope, qst); if (id_info != NULL) { return id_info; diff --git a/py/scope.h b/py/scope.h index b52d98ea1c72d..edf164c4adace 100644 --- a/py/scope.h +++ b/py/scope.h @@ -29,14 +29,14 @@ #include "py/parse.h" #include "py/emitglue.h" -enum { +typedef enum { ID_INFO_KIND_UNDECIDED, ID_INFO_KIND_GLOBAL_IMPLICIT, ID_INFO_KIND_GLOBAL_EXPLICIT, ID_INFO_KIND_LOCAL, // in a function f, written and only referenced by f ID_INFO_KIND_CELL, // in a function f, read/written by children of f ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f -}; +} id_info_kind_t; enum { ID_FLAG_IS_PARAM = 0x01, @@ -92,7 +92,7 @@ typedef struct _scope_t { scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options); void scope_free(scope_t *scope); -id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, scope_kind_t kind); +id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, id_info_kind_t kind); id_info_t *scope_find(scope_t *scope, qstr qstr); id_info_t *scope_find_global(scope_t *scope, qstr qstr); void scope_check_to_close_over(scope_t *scope, id_info_t *id); From 6d3aa16443c3eeef3945bf3d31429a655f690e0c Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Fri, 13 Dec 2019 08:24:18 +0100 Subject: [PATCH 0356/3533] py/objexcept: Compare mp_emergency_exception_buf_size signed. mp_emergency_exception_buf_size is signed, so let's make sure we compare it as such. --- py/objexcept.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/objexcept.c b/py/objexcept.c index 517427ed71365..885032c3e3eed 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -208,7 +208,7 @@ mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, siz // reserved room (after the traceback data) for a tuple with 1 element. // Otherwise we are free to use the whole buffer after the traceback data. if (o_tuple == NULL && mp_emergency_exception_buf_size >= - EMG_BUF_TUPLE_OFFSET + EMG_BUF_TUPLE_SIZE(n_args)) { + (mp_int_t)(EMG_BUF_TUPLE_OFFSET + EMG_BUF_TUPLE_SIZE(n_args))) { o_tuple = (mp_obj_tuple_t *) ((uint8_t *)MP_STATE_VM(mp_emergency_exception_buf) + EMG_BUF_TUPLE_OFFSET); } @@ -383,7 +383,7 @@ mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_te // that buffer to store the string object, reserving room at the start for the // traceback and 1-tuple. if (o_str == NULL - && mp_emergency_exception_buf_size >= EMG_BUF_STR_OFFSET + sizeof(mp_obj_str_t)) { + && mp_emergency_exception_buf_size >= (mp_int_t)(EMG_BUF_STR_OFFSET + sizeof(mp_obj_str_t))) { o_str = (mp_obj_str_t *)((uint8_t *)MP_STATE_VM(mp_emergency_exception_buf) + EMG_BUF_STR_OFFSET); } @@ -465,7 +465,7 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_er // that buffer to store the string object and its data (at least 16 bytes for // the string data), reserving room at the start for the traceback and 1-tuple. if ((o_str == NULL || o_str_buf == NULL) - && mp_emergency_exception_buf_size >= EMG_BUF_STR_OFFSET + sizeof(mp_obj_str_t) + 16) { + && mp_emergency_exception_buf_size >= (mp_int_t)(EMG_BUF_STR_OFFSET + sizeof(mp_obj_str_t) + 16)) { used_emg_buf = true; o_str = (mp_obj_str_t *)((uint8_t *)MP_STATE_VM(mp_emergency_exception_buf) + EMG_BUF_STR_OFFSET); o_str_buf = (byte *)((uint8_t *)MP_STATE_VM(mp_emergency_exception_buf) + EMG_BUF_STR_BUF_OFFSET); @@ -573,7 +573,7 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qs self->traceback_data = m_new_maybe(size_t, TRACEBACK_ENTRY_LEN); if (self->traceback_data == NULL) { #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF - if (mp_emergency_exception_buf_size >= EMG_BUF_TRACEBACK_OFFSET + EMG_BUF_TRACEBACK_SIZE) { + if (mp_emergency_exception_buf_size >= (mp_int_t)(EMG_BUF_TRACEBACK_OFFSET + EMG_BUF_TRACEBACK_SIZE)) { // There is room in the emergency buffer for traceback data size_t *tb = (size_t *)((uint8_t *)MP_STATE_VM(mp_emergency_exception_buf) + EMG_BUF_TRACEBACK_OFFSET); From fdd6fa389ed68a5d0761f7cb71c94db5e927d741 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 10:51:59 +0200 Subject: [PATCH 0357/3533] py: Use unsigned comparison of chars. On x86 chars are signed, but we're comparing a char to '0' + unsigned int, which is promoted to an unsigned int. Let's promote the char to unsigned before doing the comparison to avoid weird corner cases. --- py/emitinlinethumb.c | 2 +- py/emitinlinextensa.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c index 2853da26c5465..cffaa4bb89950 100644 --- a/py/emitinlinethumb.c +++ b/py/emitinlinethumb.c @@ -108,7 +108,7 @@ STATIC mp_uint_t emit_inline_thumb_count_params(emit_inline_asm_t *emit, mp_uint return 0; } const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i])); - if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) { + if (!(strlen(p) == 2 && p[0] == 'r' && (mp_uint_t)p[1] == '0' + i)) { emit_inline_thumb_error_msg(emit, MP_ERROR_TEXT("parameters must be registers in sequence r0 to r3")); return 0; } diff --git a/py/emitinlinextensa.c b/py/emitinlinextensa.c index 6cc2e4d34b6a8..5dac2ae3907f1 100644 --- a/py/emitinlinextensa.c +++ b/py/emitinlinextensa.c @@ -92,7 +92,7 @@ STATIC mp_uint_t emit_inline_xtensa_count_params(emit_inline_asm_t *emit, mp_uin return 0; } const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i])); - if (!(strlen(p) == 2 && p[0] == 'a' && p[1] == '2' + i)) { + if (!(strlen(p) == 2 && p[0] == 'a' && (mp_uint_t)p[1] == '2' + i)) { emit_inline_xtensa_error_msg(emit, MP_ERROR_TEXT("parameters must be registers in sequence a2 to a5")); return 0; } From 9aa58cf8bac353297ff5e7b4f3331e5618046095 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 11:23:12 +0200 Subject: [PATCH 0358/3533] py, extmod: Add explicit initializers for default values. When compiling with -Wextra which includes -Wmissing-field-initializers GCC will warn that the defval field of mp_arg_val_t is not initialized. This is just a warning as it is defined to be zero initialized, but since it is a union it makes sense to be explicit about which member we're going to use, so add the explicit initializers and get rid of the warning. --- extmod/machine_i2c.c | 4 ++-- extmod/vfs_lfs.c | 2 +- py/modmath.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 9203f16f6d785..12c9abbcbaece 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -311,8 +311,8 @@ STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} }, }; diff --git a/extmod/vfs_lfs.c b/extmod/vfs_lfs.c index 9cf3eb11083cf..dd78269a460ab 100644 --- a/extmod/vfs_lfs.c +++ b/extmod/vfs_lfs.c @@ -35,7 +35,7 @@ enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead, LFS_MAKE_ARG_mtime }; static const mp_arg_t lfs_make_allowed_args[] = { - { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, diff --git a/py/modmath.c b/py/modmath.c index b7948f39e7c91..3ab3ff334c41b 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -206,8 +206,8 @@ MATH_FUN_1(lgamma, lgamma) STATIC mp_obj_t mp_math_isclose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_a, ARG_b, ARG_rel_tol, ARG_abs_tol }; static const mp_arg_t allowed_args[] = { - {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ}, - {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ}, + {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_rel_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_abs_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(0)}}, }; From f1f6ef7b17dc97f784a4cdb33154800129dc6d26 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Thu, 28 Nov 2019 12:50:08 +0100 Subject: [PATCH 0359/3533] py/vmentrytable: Ignore GCC -Woverride-init. Like Clang, GCC warns about this file, but only with -Woverride-init which is enabled by -Wextra. Disable the warnings for this file just like we do for Clang to make -Wextra happy. --- py/vmentrytable.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/py/vmentrytable.h b/py/vmentrytable.h index 068214bf916cf..7912270872759 100644 --- a/py/vmentrytable.h +++ b/py/vmentrytable.h @@ -30,6 +30,10 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Winitializer-overrides" #endif // __clang__ +#if __GNUC__ >= 5 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Woverride-init" +#endif // __GNUC__ >= 5 static const void *const entry_table[256] = { [0 ... 255] = &&entry_default, @@ -119,3 +123,6 @@ static const void *const entry_table[256] = { #if __clang__ #pragma clang diagnostic pop #endif // __clang__ +#if __GNUC__ >= 5 +#pragma GCC diagnostic pop +#endif // __GNUC__ >= 5 From dde3db21fcd8d810bb59e0c56dfa5fd9208e1544 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 12:19:48 +0200 Subject: [PATCH 0360/3533] extmod: Disable -Wmissing-field-initializers for lfs2. --- extmod/extmod.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extmod/extmod.mk b/extmod/extmod.mk index e312acba86db4..b000b058d7bd4 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -37,6 +37,8 @@ SRC_MOD += $(addprefix $(LITTLEFS_DIR)/,\ lfs2.c \ lfs2_util.c \ ) + +$(BUILD)/$(LITTLEFS_DIR)/lfs2.o: CFLAGS += -Wno-missing-field-initializers endif ################################################################################ From ccd92335a11f03597f94da2ac937811ff3fa50cf Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Thu, 28 Nov 2019 12:47:21 +0100 Subject: [PATCH 0361/3533] py, extmod: Introduce and use MP_FALLTHROUGH macro. Newer GCC versions are able to warn about switch cases that fall through. This is usually a sign of a forgotten break statement, but in the few cases where a fall through is intended we annotate it with this macro to avoid the warning. --- extmod/moductypes.c | 3 ++- extmod/re1.5/compilecode.c | 1 + extmod/re1.5/recursiveloop.c | 1 + py/gc.c | 3 ++- py/lexer.c | 3 ++- py/mpconfig.h | 7 +++++++ py/objset.c | 1 + 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 811258424a3bf..c5fbf12e42b9d 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -506,6 +506,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set return mp_obj_new_bytearray_by_ref(uctypes_struct_agg_size(sub, self->flags, &dummy), self->addr + offset); } // Fall thru to return uctypes struct object + MP_FALLTHROUGH } case PTR: { mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t); @@ -627,7 +628,7 @@ STATIC mp_obj_t uctypes_struct_unary_op(mp_unary_op_t op, mp_obj_t self_in) { return mp_obj_new_int((mp_int_t)(uintptr_t)p); } } - /* fallthru */ + MP_FALLTHROUGH default: return MP_OBJ_NULL; // op not supported diff --git a/extmod/re1.5/compilecode.c b/extmod/re1.5/compilecode.c index 3f54b3993fd36..c4d12af87a32e 100644 --- a/extmod/re1.5/compilecode.c +++ b/extmod/re1.5/compilecode.c @@ -29,6 +29,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) prog->len++; break; } + MP_FALLTHROUGH default: term = PC; EMIT(PC++, Char); diff --git a/extmod/re1.5/recursiveloop.c b/extmod/re1.5/recursiveloop.c index bb337decfbc95..f8cb92629200a 100644 --- a/extmod/re1.5/recursiveloop.c +++ b/extmod/re1.5/recursiveloop.c @@ -22,6 +22,7 @@ recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int n case Char: if(*sp != *pc++) return 0; + MP_FALLTHROUGH case Any: sp++; continue; diff --git a/py/gc.c b/py/gc.c index 9c6336852a0cd..767f1b81c4dfa 100644 --- a/py/gc.c +++ b/py/gc.c @@ -298,7 +298,8 @@ STATIC void gc_sweep(void) { #if MICROPY_PY_GC_COLLECT_RETVAL MP_STATE_MEM(gc_collected)++; #endif - // fall through to free the head + // fall through to free the head + MP_FALLTHROUGH case AT_TAIL: if (free_tail) { diff --git a/py/lexer.c b/py/lexer.c index 7d2a251d41d75..07ea2b96ab578 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -346,7 +346,8 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw) { vstr_add_char(&lex->vstr, '\\'); break; } - // Otherwise fall through. + // Otherwise fall through. + MP_FALLTHROUGH case 'x': { mp_uint_t num = 0; if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) { diff --git a/py/mpconfig.h b/py/mpconfig.h index cc83f3850d695..854188b66b585 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1643,6 +1643,13 @@ typedef double mp_float_t; #endif #endif +// Explicitly annotate switch case fall throughs +#if defined(__GNUC__) && __GNUC__ >= 7 +#define MP_FALLTHROUGH __attribute__((fallthrough)); +#else +#define MP_FALLTHROUGH +#endif + #ifndef MP_HTOBE16 #if MP_ENDIANNESS_LITTLE #define MP_HTOBE16(x) ((uint16_t)((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))) diff --git a/py/objset.c b/py/objset.c index f31a901a7008a..dac9b1138291e 100644 --- a/py/objset.c +++ b/py/objset.c @@ -445,6 +445,7 @@ STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } return MP_OBJ_NEW_SMALL_INT(hash); } + MP_FALLTHROUGH #endif default: return MP_OBJ_NULL; // op not supported From bef412789ea93c521bd9c2dddc22b9b3484da574 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 11:29:16 +0200 Subject: [PATCH 0362/3533] mpy-cross: Enable more warnings. --- mpy-cross/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpy-cross/Makefile b/mpy-cross/Makefile index f80ee761b7bcf..971f2f81aa593 100644 --- a/mpy-cross/Makefile +++ b/mpy-cross/Makefile @@ -18,7 +18,7 @@ INC += -I$(TOP) # compiler settings CWARN = -Wall -Werror -CWARN += -Wpointer-arith -Wuninitialized +CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables From 05f95682e7ddfb08c317e83826df9a1d636676f3 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sat, 3 Oct 2020 11:31:13 +0200 Subject: [PATCH 0363/3533] unix: Enable more warnings. --- ports/unix/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index ff5f355022bc5..7380e5e41259c 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -39,7 +39,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror -CWARN += -Wpointer-arith -Wuninitialized -Wdouble-promotion -Wsign-compare -Wfloat-conversion +CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith -Wdouble-promotion -Wfloat-conversion CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) -I$(VARIANT_DIR) $(CFLAGS_EXTRA) # Debugging/Optimization From 368c1a09611f2a139c0e401eeb4359f9cc2a7c57 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Oct 2020 00:54:30 +1100 Subject: [PATCH 0364/3533] tests/thread/stress_schedule.py: Assign globals before running test. When threading is enabled without the GIL then there can be races between the threads accessing the globals dict. Avoid this issue by making sure all globals variables are allocated before starting the threads. Signed-off-by: Damien George --- tests/thread/stress_schedule.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/thread/stress_schedule.py b/tests/thread/stress_schedule.py index c5a402b3a3c36..8be7f2d737874 100644 --- a/tests/thread/stress_schedule.py +++ b/tests/thread/stress_schedule.py @@ -14,7 +14,11 @@ gc.disable() +_NUM_TASKS = 10000 +_TIMEOUT_MS = 10000 + n = 0 # How many times the task successfully ran. +t = None # Start time of test, assigned here to preallocate entry in globals dict. def task(x): @@ -34,9 +38,6 @@ def thread(): for i in range(8): _thread.start_new_thread(thread, ()) -_NUM_TASKS = const(10000) -_TIMEOUT_MS = const(10000) - # Wait up to 10 seconds for 10000 tasks to be scheduled. t = utime.ticks_ms() while n < _NUM_TASKS and utime.ticks_diff(utime.ticks_ms(), t) < _TIMEOUT_MS: From 0118c07916c24a6ccb6dbd0ea904312f01798b40 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Oct 2020 17:19:52 +1100 Subject: [PATCH 0365/3533] stm32/machine_adc: Fix ADC auto-calibration to run when ADC not enabled. Prior to this commit, the ADC calibration code was never executing because ADVREGEN bit was set making the CR register always non-zero. This commit changes the logic so that ADC calibration is always run when the ADC is disabled and an ADC channel is initialised. It also uses the LL API functions to do the calibration, to make sure it is done correctly on each MCU variant. Signed-off-by: Damien George --- ports/stm32/boards/stm32f0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32h7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32l0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32wbxx_hal_conf_base.h | 1 + ports/stm32/machine_adc.c | 14 ++++++++++---- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/stm32f0xx_hal_conf_base.h b/ports/stm32/boards/stm32f0xx_hal_conf_base.h index 9cb7761ac2ca4..faceda2f4fa90 100644 --- a/ports/stm32/boards/stm32f0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f0xx_hal_conf_base.h @@ -47,6 +47,7 @@ #include "stm32f0xx_hal_uart.h" #include "stm32f0xx_hal_usart.h" #include "stm32f0xx_hal_wwdg.h" +#include "stm32f0xx_ll_adc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32h7xx_hal_conf_base.h b/ports/stm32/boards/stm32h7xx_hal_conf_base.h index 5c97e2c44b720..9f43da40308f7 100644 --- a/ports/stm32/boards/stm32h7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32h7xx_hal_conf_base.h @@ -53,6 +53,7 @@ #include "stm32h7xx_hal_uart.h" #include "stm32h7xx_hal_usart.h" #include "stm32h7xx_hal_wwdg.h" +#include "stm32h7xx_ll_adc.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32l0xx_hal_conf_base.h b/ports/stm32/boards/stm32l0xx_hal_conf_base.h index ed524fecca9c3..b100daaa98eb3 100644 --- a/ports/stm32/boards/stm32l0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l0xx_hal_conf_base.h @@ -46,6 +46,7 @@ #include "stm32l0xx_hal_uart.h" #include "stm32l0xx_hal_usart.h" #include "stm32l0xx_hal_wwdg.h" +#include "stm32l0xx_ll_adc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32wbxx_hal_conf_base.h b/ports/stm32/boards/stm32wbxx_hal_conf_base.h index 8dbc9ecea7f12..83d07ad5b1c3b 100644 --- a/ports/stm32/boards/stm32wbxx_hal_conf_base.h +++ b/ports/stm32/boards/stm32wbxx_hal_conf_base.h @@ -41,6 +41,7 @@ #include "stm32wbxx_hal_tim.h" #include "stm32wbxx_hal_uart.h" #include "stm32wbxx_hal_usart.h" +#include "stm32wbxx_ll_adc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index f29896d37cf26..9c20f0f954989 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -156,10 +156,16 @@ STATIC void adc_config(ADC_TypeDef *adc, uint32_t bits) { #endif #if ADC_V2 - if (adc->CR == 0) { - // ADC hasn't been enabled so calibrate it - adc->CR |= ADC_CR_ADCAL; - while (adc->CR & ADC_CR_ADCAL) { + if (!(adc->CR & ADC_CR_ADEN)) { + // ADC isn't enabled so calibrate it now + #if defined(STM32F0) || defined(STM32L0) + LL_ADC_StartCalibration(adc); + #elif defined(STM32L4) || defined(STM32WB) + LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED); + #else + LL_ADC_StartCalibration(adc, LL_ADC_CALIB_OFFSET_LINEARITY, LL_ADC_SINGLE_ENDED); + #endif + while (LL_ADC_IsCalibrationOnGoing(adc)) { } } From 03a1f94ea16a532bd4219092edb06e251d9a0ca5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 11:31:53 +1100 Subject: [PATCH 0366/3533] extmod/vfs_lfs: Support mounting LFS filesystems in read-only mode. Signed-off-by: Damien George --- extmod/vfs_lfsx.c | 12 +++++++++--- tests/extmod/vfs_lfs_mount.py | 17 +++++++++++++++++ tests/extmod/vfs_lfs_mount.py.exp | 6 ++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 35d5f03c59a2c..f865e46060234 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -423,10 +423,16 @@ STATIC mp_obj_t MP_VFS_LFSx(statvfs)(mp_obj_t self_in, mp_obj_t path_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(MP_VFS_LFSx(statvfs_obj), MP_VFS_LFSx(statvfs)); STATIC mp_obj_t MP_VFS_LFSx(mount)(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) { - (void)self_in; - (void)readonly; + MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(self_in); (void)mkfs; - // already called LFSx_API(mount) in MP_VFS_LFSx(make_new) + + // Make block device read-only if requested. + if (mp_obj_is_true(readonly)) { + self->blockdev.writeblocks[0] = MP_OBJ_NULL; + } + + // Already called LFSx_API(mount) in MP_VFS_LFSx(make_new) so the filesystem is ready. + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_3(MP_VFS_LFSx(mount_obj), MP_VFS_LFSx(mount)); diff --git a/tests/extmod/vfs_lfs_mount.py b/tests/extmod/vfs_lfs_mount.py index 2c40b2989796f..3d8cec6075c86 100644 --- a/tests/extmod/vfs_lfs_mount.py +++ b/tests/extmod/vfs_lfs_mount.py @@ -67,6 +67,23 @@ def test(bdev, vfs_class): # umount uos.umount("/lfs") + # mount read-only + vfs = vfs_class(bdev) + uos.mount(vfs, "/lfs", readonly=True) + + # test reading works + with open("/lfs/subdir/lfsmod2.py") as f: + print("lfsmod2.py:", f.read()) + + # test writing fails + try: + open("/lfs/test_write", "w") + except OSError as er: + print(repr(er)) + + # umount + uos.umount("/lfs") + # clear imported modules usys.modules.clear() diff --git a/tests/extmod/vfs_lfs_mount.py.exp b/tests/extmod/vfs_lfs_mount.py.exp index b5c52153141a2..aa654ebe05f12 100644 --- a/tests/extmod/vfs_lfs_mount.py.exp +++ b/tests/extmod/vfs_lfs_mount.py.exp @@ -2,7 +2,13 @@ test hello from lfs package hello from lfs +lfsmod2.py: print("hello from lfs") + +OSError(30,) test hello from lfs package hello from lfs +lfsmod2.py: print("hello from lfs") + +OSError(36,) From b4062894df8e17e11179dcb5f5b28b27eed33aff Mon Sep 17 00:00:00 2001 From: robert Date: Sat, 22 Aug 2020 20:56:26 +0200 Subject: [PATCH 0367/3533] esp32/mpconfigport.h: Seed the urandom module on import. For seeding, the RNG function of the ESP-IDF is used, which is told to be a true RNG, at least when WiFi or Bluetooth is enabled. Seeding on import is as per CPython. To obtain a reproducible sequence of pseudo-random numbers one must explicitly seed with a known value. --- ports/esp32/mpconfigport.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index b63d1f89558c0..f170d70708b79 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -6,6 +6,7 @@ #include #include +#include "esp_system.h" #if !MICROPY_ESP_IDF_4 #include "rom/ets_sys.h" @@ -141,6 +142,7 @@ #define MICROPY_PY_UBINASCII_CRC32 (1) #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (esp_random()) #define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new From 057193e855ca0b5873191c327184052765781ddf Mon Sep 17 00:00:00 2001 From: robert Date: Sun, 23 Aug 2020 12:12:11 +0200 Subject: [PATCH 0368/3533] esp8266/mpconfigport.h: Seed the urandom module on import. For seeding, the hardware RNG of the esp8266 is used. --- ports/esp8266/mpconfigport.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 974310f84da6e..99cf2ade4dec3 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -26,6 +26,7 @@ #define MICROPY_HELPER_REPL (1) #define MICROPY_HELPER_LEXER_UNIX (0) #define MICROPY_ENABLE_SOURCE_LINE (1) +#define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_USE_INTERNAL_ERRNO (1) @@ -69,6 +70,7 @@ #define MICROPY_PY_UTIMEQ (1) #define MICROPY_PY_UJSON (1) #define MICROPY_PY_URANDOM (1) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (*WDEV_HWRNG) #define MICROPY_PY_URE (1) #define MICROPY_PY_USELECT (1) #define MICROPY_PY_UTIME_MP_HAL (1) @@ -197,4 +199,6 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_SCHED_SCHEDULE(f) MP_FASTCODE(f) +#define WDEV_HWRNG ((volatile uint32_t *)0x3ff20e44) + #define _assert(expr) ((expr) ? (void)0 : __assert_func(__FILE__, __LINE__, __func__, #expr)) From 59019d7f759c78dedd8d353d24c8d64a7a9981c7 Mon Sep 17 00:00:00 2001 From: robert Date: Sun, 23 Aug 2020 12:14:23 +0200 Subject: [PATCH 0369/3533] stm32/mpconfigport.h: Seed the urandom module on import. For seeding the rng_get function is used, which is also the heart of uos.urandom and pyb.rng, and is a hardware RNG where available. --- ports/stm32/mpconfigport.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 7ea41bb6f4655..5f8e7ec2de1c1 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -79,6 +79,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #endif #define MICROPY_STREAMS_NON_BLOCK (1) +#define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_USE_INTERNAL_ERRNO (1) @@ -167,6 +168,7 @@ #endif #ifndef MICROPY_PY_URANDOM #define MICROPY_PY_URANDOM (1) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rng_get()) #endif #ifndef MICROPY_PY_URANDOM_EXTRA_FUNCS #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) @@ -427,3 +429,6 @@ static inline mp_uint_t disable_irq(void) { // We need to provide a declaration/definition of alloca() #include + +// Needed for MICROPY_PY_URANDOM_SEED_INIT_FUNC. +uint32_t rng_get(void); From 3e455e9792b5851a21ed2d94d518b21557d2a361 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Oct 2020 14:17:46 +1100 Subject: [PATCH 0370/3533] stm32/rng: Use SysTick+RTC+unique-id to seed pRNG for MCUs without RNG. The same seed will only occur if the board is the same, the RTC has the same time (eg freshly powered up) and the first call to this function (eg via an "import random") is done at exactly the same time since reset. Signed-off-by: Damien George --- ports/stm32/rng.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ports/stm32/rng.c b/ports/stm32/rng.c index b23941998a475..eea02f72657de 100644 --- a/ports/stm32/rng.c +++ b/ports/stm32/rng.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "rtc.h" #include "rng.h" #if MICROPY_HW_ENABLE_RNG @@ -63,16 +64,26 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get); #else // MICROPY_HW_ENABLE_RNG // For MCUs that don't have an RNG we still need to provide a rng_get() function, -// eg for lwIP. A pseudo-RNG is not really ideal but we go with it for now. We +// eg for lwIP and random.seed(). A pseudo-RNG is not really ideal but we go with +// it for now, seeding with numbers which will be somewhat different each time. We // don't want to use urandom's pRNG because then the user won't see a reproducible // random stream. // Yasmarang random number generator by Ilya Levin // http://www.literatecode.com/yasmarang STATIC uint32_t pyb_rng_yasmarang(void) { - static uint32_t pad = 0xeda4baba, n = 69, d = 233; + static bool seeded = false; + static uint32_t pad = 0, n = 0, d = 0; static uint8_t dat = 0; + if (!seeded) { + seeded = true; + rtc_init_finalise(); + pad = *(uint32_t *)MP_HAL_UNIQUE_ID_ADDRESS ^ SysTick->VAL; + n = RTC->TR; + d = RTC->SSR; + } + pad += dat + d * n; pad = (pad << 3) + (pad >> 29); n = pad | 2; From 6f34800884f0a9a0c7116a7c5b94c6db38c4b417 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Oct 2020 14:20:00 +1100 Subject: [PATCH 0371/3533] extmod/modurandom: Support urandom.seed() without an argument. If a port provides MICROPY_PY_URANDOM_SEED_INIT_FUNC as a source of randomness then this will be used when urandom.seed() is called without an argument (or with None as the argument) to seed the pRNG. Other related changes in this commit: - mod_urandom___init__ is changed to call seed() without arguments, instead of explicitly passing in the result of MICROPY_PY_URANDOM_SEED_INIT_FUNC. - mod_urandom___init__ will only ever seed the pRNG once (before it could seed it again if imported by, eg, random and then urandom). - The Yasmarang state is moved to the BSS for builds where the state is guaranteed to be initialised on import of the (u)random module. Signed-off-by: Damien George --- extmod/modurandom.c | 41 ++++++++++++++++++++++++---- tests/extmod/urandom_seed_default.py | 30 ++++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 tests/extmod/urandom_seed_default.py diff --git a/extmod/modurandom.c b/extmod/modurandom.c index ab83b0f701234..5a736c1ebc628 100644 --- a/extmod/modurandom.c +++ b/extmod/modurandom.c @@ -31,15 +31,29 @@ #if MICROPY_PY_URANDOM +// Work out if the seed will be set on import or not. +#if MICROPY_MODULE_BUILTIN_INIT && defined(MICROPY_PY_URANDOM_SEED_INIT_FUNC) +#define SEED_ON_IMPORT (1) +#else +#define SEED_ON_IMPORT (0) +#endif + // Yasmarang random number generator // by Ilya Levin // http://www.literatecode.com/yasmarang // Public Domain #if !MICROPY_ENABLE_DYNRUNTIME +#if SEED_ON_IMPORT +// If the state is seeded on import then keep these variables in the BSS. +STATIC uint32_t yasmarang_pad, yasmarang_n, yasmarang_d; +STATIC uint8_t yasmarang_dat; +#else +// Without seed-on-import these variables must be initialised via the data section. STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233; STATIC uint8_t yasmarang_dat = 0; #endif +#endif STATIC uint32_t yasmarang(void) { yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n; @@ -83,15 +97,24 @@ STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_getrandbits_obj, mod_urandom_getrandbits); -STATIC mp_obj_t mod_urandom_seed(mp_obj_t seed_in) { - mp_uint_t seed = mp_obj_get_int_truncated(seed_in); +STATIC mp_obj_t mod_urandom_seed(size_t n_args, const mp_obj_t *args) { + mp_uint_t seed; + if (n_args == 0 || args[0] == mp_const_none) { + #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC + seed = MICROPY_PY_URANDOM_SEED_INIT_FUNC; + #else + mp_raise_ValueError(MP_ERROR_TEXT("no default seed")); + #endif + } else { + seed = mp_obj_get_int_truncated(args[0]); + } yasmarang_pad = seed; yasmarang_n = 69; yasmarang_d = 233; yasmarang_dat = 0; return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_seed_obj, mod_urandom_seed); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_seed_obj, 0, 1, mod_urandom_seed); #if MICROPY_PY_URANDOM_EXTRA_FUNCS @@ -189,9 +212,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_uniform_obj, mod_urandom_uniform); #endif // MICROPY_PY_URANDOM_EXTRA_FUNCS -#ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC +#if SEED_ON_IMPORT STATIC mp_obj_t mod_urandom___init__() { - mod_urandom_seed(MP_OBJ_NEW_SMALL_INT(MICROPY_PY_URANDOM_SEED_INIT_FUNC)); + // This module may be imported by more than one name so need to ensure + // that it's only ever seeded once. + static bool seeded = false; + if (!seeded) { + seeded = true; + mod_urandom_seed(0, NULL); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__); @@ -200,7 +229,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__) #if !MICROPY_ENABLE_DYNRUNTIME STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) }, - #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC + #if SEED_ON_IMPORT { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&mod_urandom___init___obj) }, #endif { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_urandom_getrandbits_obj) }, diff --git a/tests/extmod/urandom_seed_default.py b/tests/extmod/urandom_seed_default.py new file mode 100644 index 0000000000000..a032b9362b701 --- /dev/null +++ b/tests/extmod/urandom_seed_default.py @@ -0,0 +1,30 @@ +# test urandom.seed() without any arguments + +try: + import urandom as random +except ImportError: + try: + import random + except ImportError: + print("SKIP") + raise SystemExit + +try: + random.seed() +except ValueError: + # no default seed on this platform + print("SKIP") + raise SystemExit + + +def rng_seq(): + return [random.getrandbits(16) for _ in range(10)] + + +# seed with default and check that doesn't produce the same RNG sequence +random.seed() +seq = rng_seq() +random.seed() +print(seq == rng_seq()) +random.seed(None) +print(seq == rng_seq()) From 1b723937e3fe4012ac878ded833ff6a39455e0b8 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 26 Aug 2020 11:23:10 +0200 Subject: [PATCH 0372/3533] py/makeqstrdefs.py: Fix beaviour when scanning non-C preprocessed files. When process_file() is passed a preprocessed C++ file for instance it won't find any lines containing .c files and the last_fname variable remains None, so handle that gracefully. --- py/makeqstrdefs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py/makeqstrdefs.py b/py/makeqstrdefs.py index 9449a46ee924a..47afe0adb9775 100644 --- a/py/makeqstrdefs.py +++ b/py/makeqstrdefs.py @@ -58,7 +58,8 @@ def process_file(f): elif args.mode == _MODE_COMPRESS: output.append(match) - write_out(last_fname, output) + if last_fname: + write_out(last_fname, output) return "" From 2b9f0586e7a37a673aa2c8ef298751c8d30b2667 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 22 Oct 2020 14:09:33 +0200 Subject: [PATCH 0373/3533] py/makeqstrdefs.py: Process C++ files as well. Preprocessed C++ code isn't different from C code when it comes to QSTR instances so process it as well. --- py/makeqstrdefs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/makeqstrdefs.py b/py/makeqstrdefs.py index 47afe0adb9775..1db00d963356f 100644 --- a/py/makeqstrdefs.py +++ b/py/makeqstrdefs.py @@ -44,7 +44,7 @@ def process_file(f): m = re_line.match(line) assert m is not None fname = m.group(1) - if not fname.endswith(".c"): + if os.path.splitext(fname)[1] not in [".c", ".cpp"]: continue if fname != last_fname: write_out(last_fname, output) From f1666419a8aaee846f7175ccdb8799ab9deea376 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 8 Oct 2020 16:19:26 +0200 Subject: [PATCH 0374/3533] py/mkrules.mk: Add target for compiling C++ files. Move the target from the ESP32 Makefile since that does what is needed already, but also include files from user C modules as is done for the C files. --- ports/esp32/Makefile | 16 ---------------- py/mkrules.mk | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index be282577229c4..94374eb1c4f98 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -781,22 +781,6 @@ $(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.pro $(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS) $(Q)$(SIZE) $@ -define compile_cxx -$(ECHO) "CXX $<" -$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< -@# The following fixes the dependency file. -@# See http://make.paulandlesley.org/autodep.html for details. -@# Regex adjusted from the above to play better with Windows paths, etc. -@$(CP) $(@:.o=.d) $(@:.o=.P); \ - $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ - -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ - $(RM) -f $(@:.o=.d) -endef - -vpath %.cpp . $(TOP) -$(BUILD)/%.o: %.cpp - $(call compile_cxx) - ################################################################################ # Declarations to build the bootloader diff --git a/py/mkrules.mk b/py/mkrules.mk index c37c25db4bd2f..f003174ea5449 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -58,12 +58,28 @@ $(Q)$(CC) $(CFLAGS) -c -MD -o $@ $< $(RM) -f $(@:.o=.d) endef +define compile_cxx +$(ECHO) "CXX $<" +$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< +@# The following fixes the dependency file. +@# See http://make.paulandlesley.org/autodep.html for details. +@# Regex adjusted from the above to play better with Windows paths, etc. +@$(CP) $(@:.o=.d) $(@:.o=.P); \ + $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ + $(RM) -f $(@:.o=.d) +endef + vpath %.c . $(TOP) $(USER_C_MODULES) $(BUILD)/%.o: %.c $(call compile_c) vpath %.c . $(TOP) $(USER_C_MODULES) +vpath %.cpp . $(TOP) $(USER_C_MODULES) +$(BUILD)/%.o: %.cpp + $(call compile_cxx) + $(BUILD)/%.pp: %.c $(ECHO) "PreProcess $<" $(Q)$(CPP) $(CFLAGS) -Wp,-C,-dD,-dI -o $@ $< From 8e94fa0d2eb94483f387b1ae2e081d1998575a7f Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 8 Oct 2020 16:40:17 +0200 Subject: [PATCH 0375/3533] py/makeqstrdefs.py: Support preprocessing C++ files for QSTR generation. When SCR_QSTR contains C++ files they should be preprocessed with the same compiler flags (CXXFLAGS) as they will be compiled with, to make sure code scanned for QSTR occurrences is effectively the code used in the rest of the build. The 'split SCR_QSTR in .c and .cpp files and process each with different flags' logic isn't trivial to express in a Makefile and the existing principle for deciding which files to preprocess was already rather complicated, so the actual preprocessing is moved into makeqstrdefs.py completely. --- py/makeqstrdefs.py | 59 +++++++++++++++++++++++++++++++++++++++++++++- py/mkrules.mk | 10 ++++---- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/py/makeqstrdefs.py b/py/makeqstrdefs.py index 1db00d963356f..f514ae0c1052f 100644 --- a/py/makeqstrdefs.py +++ b/py/makeqstrdefs.py @@ -8,6 +8,7 @@ from __future__ import print_function import re +import subprocess import sys import io import os @@ -20,6 +21,31 @@ _MODE_COMPRESS = "compress" +def preprocess(): + if any(src in args.dependencies for src in args.changed_sources): + sources = args.sources + elif any(args.changed_sources): + sources = args.changed_sources + else: + sources = args.sources + csources = [] + cxxsources = [] + for source in sources: + if source.endswith(".cpp"): + cxxsources.append(source) + else: + csources.append(source) + try: + os.makedirs(os.path.dirname(args.output[0])) + except OSError: + pass + with open(args.output[0], "w") as out_file: + if csources: + subprocess.check_call(args.pp + args.cflags + csources, stdout=out_file) + if cxxsources: + subprocess.check_call(args.pp + args.cxxflags + cxxsources, stdout=out_file) + + def write_out(fname, output): if output: for m, r in [("/", "__"), ("\\", "__"), (":", "@"), ("..", "@@")]: @@ -105,7 +131,7 @@ def cat_together(): if __name__ == "__main__": - if len(sys.argv) != 6: + if len(sys.argv) < 6: print("usage: %s command mode input_filename output_dir output_file" % sys.argv[0]) sys.exit(2) @@ -114,6 +140,37 @@ class Args: args = Args() args.command = sys.argv[1] + + if args.command == "pp": + named_args = { + s: [] + for s in [ + "pp", + "output", + "cflags", + "cxxflags", + "sources", + "changed_sources", + "dependencies", + ] + } + + for arg in sys.argv[1:]: + if arg in named_args: + current_tok = arg + else: + named_args[current_tok].append(arg) + + if not named_args["pp"] or len(named_args["output"]) != 1: + print("usage: %s %s ..." % (sys.argv[0], " ... ".join(named_args))) + sys.exit(2) + + for k, v in named_args.items(): + setattr(args, k, v) + + preprocess() + sys.exit(0) + args.mode = sys.argv[2] args.input_filename = sys.argv[3] # Unused for command=cat args.output_dir = sys.argv[4] diff --git a/py/mkrules.mk b/py/mkrules.mk index f003174ea5449..3bfe64d7536a1 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -15,10 +15,12 @@ CFLAGS += -DMICROPY_ROM_TEXT_COMPRESSION=1 endif # QSTR generation uses the same CFLAGS, with these modifications. +QSTR_GEN_FLAGS = -DNO_QSTR -I$(BUILD)/tmp # Note: := to force evalulation immediately. QSTR_GEN_CFLAGS := $(CFLAGS) -QSTR_GEN_CFLAGS += -DNO_QSTR -QSTR_GEN_CFLAGS += -I$(BUILD)/tmp +QSTR_GEN_CFLAGS += $(QSTR_GEN_FLAGS) +QSTR_GEN_CXXFLAGS := $(CXXFLAGS) +QSTR_GEN_CXXFLAGS += $(QSTR_GEN_FLAGS) # This file expects that OBJ contains a list of all of the object files. # The directory portion of each object file is used to locate the source @@ -95,14 +97,14 @@ $(BUILD)/%.pp: %.c # to get built before we try to compile any of them. $(OBJ): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/mpversion.h $(OBJ_EXTRA_ORDER_DEPS) -# The logic for qstr regeneration is: +# The logic for qstr regeneration (applied by makeqstrdefs.py) is: # - if anything in QSTR_GLOBAL_DEPENDENCIES is newer, then process all source files ($^) # - else, if list of newer prerequisites ($?) is not empty, then process just these ($?) # - else, process all source files ($^) [this covers "make -B" which can set $? to empty] # See more information about this process in docs/develop/qstr.rst. $(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) $(QSTR_GLOBAL_DEPENDENCIES) | $(QSTR_GLOBAL_REQUIREMENTS) $(ECHO) "GEN $@" - $(Q)$(CPP) $(QSTR_GEN_CFLAGS) $(if $(filter $?,$(QSTR_GLOBAL_DEPENDENCIES)),$^,$(if $?,$?,$^)) >$(HEADER_BUILD)/qstr.i.last + $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py pp $(CPP) output $(HEADER_BUILD)/qstr.i.last cflags $(QSTR_GEN_CFLAGS) cxxflags $(QSTR_GEN_CXXFLAGS) sources $^ dependencies $(QSTR_GLOBAL_DEPENDENCIES) changed_sources $? $(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last $(ECHO) "GEN $@" From e498a8bd13a0ecdf1cc50f57eb6d3630e8c0c078 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 14 Oct 2020 15:42:18 +0200 Subject: [PATCH 0376/3533] py: Workaround clang error when building misc.h with C++ compiler. --- py/misc.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/misc.h b/py/misc.h index 2dd20365c934c..aac90724460cf 100644 --- a/py/misc.h +++ b/py/misc.h @@ -272,7 +272,12 @@ typedef union _mp_float_union_t { // Map MP_COMPRESSED_ROM_TEXT to the compressed strings. // Force usage of the MP_ERROR_TEXT macro by requiring an opaque type. -typedef struct {} *mp_rom_error_text_t; +typedef struct { + #ifdef __clang__ + // Fix "error: empty struct has size 0 in C, size 1 in C++". + char dummy; + #endif +} *mp_rom_error_text_t; #include From 78c8b55067b2a3da8a2237fe8acd351d188902cb Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 8 Oct 2020 16:39:33 +0200 Subject: [PATCH 0377/3533] docs: Fix reference to QSTR_GEN_CFLAGS Makefile flag. --- docs/develop/qstr.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/develop/qstr.rst b/docs/develop/qstr.rst index 1b3b9f903bc91..3550a8bd425f2 100644 --- a/docs/develop/qstr.rst +++ b/docs/develop/qstr.rst @@ -51,7 +51,7 @@ Processing happens in the following stages: through the C pre-processor. This means that any conditionally disabled code will be removed, and macros expanded. This means we don't add strings to the pool that won't be used in the final firmware. Because at this stage (thanks - to the ``NO_QSTR`` macro added by ``QSTR_GEN_EXTRA_CFLAGS``) there is no + to the ``NO_QSTR`` macro added by ``QSTR_GEN_CFLAGS``) there is no definition for ``MP_QSTR_Foo`` it passes through this stage unaffected. This file also includes comments from the preprocessor that include line number information. Note that this step only uses files that have changed, which From 0153148fd26308e4ce921a4287ac4a26af15a9fe Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 8 Oct 2020 16:44:55 +0200 Subject: [PATCH 0378/3533] py/py.mk: Support C++ code for user C modules. Support C++ code in .cpp files by providing CXX counterparts of the _USERMOD_ flags we have for C already. This merely enables the Makefile of user C modules to use variables specific to C++ compilation, it is still up to each port's main Makefile to also include these in the build. --- docs/develop/cmodules.rst | 9 +++++---- py/py.mk | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index e616adad03353..849d0e60a2ea2 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -8,7 +8,8 @@ limitations with the Python environment, often due to an inability to access certain hardware resources or Python speed limitations. If your limitations can't be resolved with suggestions in :ref:`speed_python`, -writing some or all of your module in C is a viable option. +writing some or all of your module in C (and/or C++ if implemented for your port) +is a viable option. If your module is designed to access or work with commonly available hardware or libraries please consider implementing it inside the MicroPython @@ -29,7 +30,7 @@ Structure of an external C module A MicroPython user C module is a directory with the following files: -* ``*.c`` and/or ``*.h`` source code files for your module. +* ``*.c`` / ``*.cpp`` / ``*.h`` source code files for your module. These will typically include the low level functionality being implemented and the MicroPython binding functions to expose the functions and module(s). @@ -44,12 +45,12 @@ A MicroPython user C module is a directory with the following files: in your ``micropython.mk`` to a local make variable, eg ``EXAMPLE_MOD_DIR := $(USERMOD_DIR)`` - Your ``micropython.mk`` must add your modules C files relative to your + Your ``micropython.mk`` must add your modules source files relative to your expanded copy of ``$(USERMOD_DIR)`` to ``SRC_USERMOD``, eg ``SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c`` If you have custom ``CFLAGS`` settings or include folders to define, these - should be added to ``CFLAGS_USERMOD``. + should be added to ``CFLAGS_USERMOD``, or ``CXXFLAGS_USERMOD``. See below for full usage example. diff --git a/py/py.mk b/py/py.mk index d864a7ed3d843..bac38f07412f9 100644 --- a/py/py.mk +++ b/py/py.mk @@ -33,7 +33,9 @@ ifneq ($(USER_C_MODULES),) # pre-define USERMOD variables as expanded so that variables are immediate # expanded as they're added to them SRC_USERMOD := +SRC_USERMOD_CXX := CFLAGS_USERMOD := +CXXFLAGS_USERMOD := LDFLAGS_USERMOD := $(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \ $(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\ @@ -42,7 +44,9 @@ $(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \ ) SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD)) +SRC_MOD_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX)) CFLAGS_MOD += $(CFLAGS_USERMOD) +CXXFLAGS_MOD += $(CXXFLAGS_USERMOD) LDFLAGS_MOD += $(LDFLAGS_USERMOD) endif From fad4079778f46bc21dd19a674b31b4c3c7eb6a91 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 8 Oct 2020 16:52:25 +0200 Subject: [PATCH 0379/3533] esp32,unix: Support building C++ code. Support building .cpp files and linking them into the micropython executable in a way similar to how it is done for .c files. The main incentive here is to enable user C modules to use C++ files (which are put in SRC_MOD_CXX by py.mk) since the core itself does not utilize C++. However, to verify build functionality a unix overage test is added. The esp32 port already has CXXFLAGS so just add the user modules' flags to it. For the unix port use a copy of the CFLAGS but strip the ones which are not usable for C++. --- .travis.yml | 2 +- ports/esp32/Makefile | 8 ++++++-- ports/unix/Makefile | 15 ++++++++++++++- ports/unix/coveragecpp.cpp | 23 +++++++++++++++++++++++ ports/unix/main.c | 2 ++ tests/unix/extra_coverage.py | 3 +++ tests/unix/extra_coverage.py.exp | 1 + 7 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 ports/unix/coveragecpp.cpp diff --git a/.travis.yml b/.travis.yml index c9fcc21336efd..3b399804e37a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -165,7 +165,7 @@ jobs: - stage: test name: "unix coverage 32-bit build and tests" install: - - sudo apt-get install gcc-multilib libffi-dev:i386 + - sudo apt-get install gcc-multilib g++-multilib libffi-dev:i386 - sudo apt-get install python3-pip - sudo pip3 install setuptools - sudo pip3 install pyelftools diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 94374eb1c4f98..756bc8f8940c5 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -268,7 +268,7 @@ CFLAGS += -DMICROPY_ESP_IDF_4=1 endif # this is what ESPIDF uses for c++ compilation -CXXFLAGS = -std=gnu++11 $(CFLAGS_COMMON) $(INC) $(INC_ESPCOMP) +CXXFLAGS = -std=gnu++11 $(CFLAGS_COMMON) $(INC) $(INC_ESPCOMP) $(CXXFLAGS_MOD) LDFLAGS = -nostdlib -Map=$(@:.elf=.map) --cref LDFLAGS += --gc-sections -static -EL @@ -354,6 +354,9 @@ SRC_C = \ $(wildcard $(BOARD_DIR)/*.c) \ $(SRC_MOD) +SRC_CXX += \ + $(SRC_MOD_CXX) + EXTMOD_SRC_C += $(addprefix extmod/,\ modonewire.c \ ) @@ -376,6 +379,7 @@ DRIVERS_SRC_C = $(addprefix drivers/,\ OBJ_MP = OBJ_MP += $(PY_O) OBJ_MP += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ_MP += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ_MP += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ_MP += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) @@ -384,7 +388,7 @@ OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) $(OBJ_MP): CFLAGS += -Wdouble-promotion -Wfloat-conversion # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C) # Append any auto-generated sources that are needed by sources listed in SRC_QSTR SRC_QSTR_AUTO_DEPS += diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 7380e5e41259c..3388d67a2d6f8 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -248,13 +248,18 @@ LIB_SRC_C += $(addprefix lib/,\ utils/gchelper_generic.c \ ) +SRC_CXX += \ + coveragecpp.cpp \ + $(SRC_MOD_CXX) + OBJ = $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(LIB_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(LIB_SRC_C) $(EXTMOD_SRC_C) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR SRC_QSTR_AUTO_DEPS += @@ -272,6 +277,14 @@ ifneq ($(FROZEN_MANIFEST)$(FROZEN_DIR),) CFLAGS += -DMICROPY_MODULE_FROZEN_STR endif +HASCPP17 = $(shell expr `$(CC) -dumpversion | cut -f1 -d.` \>= 7) +ifeq ($(HASCPP17), 1) + CXXFLAGS += -std=c++17 +else + CXXFLAGS += -std=c++11 +endif +CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS) $(CXXFLAGS_MOD)) + ifeq ($(MICROPY_FORCE_32BIT),1) RUN_TESTS_MPY_CROSS_FLAGS = --mpy-cross-flags='-mcache-lookup-bc -march=x86' else diff --git a/ports/unix/coveragecpp.cpp b/ports/unix/coveragecpp.cpp new file mode 100644 index 0000000000000..ea7418e1dd46a --- /dev/null +++ b/ports/unix/coveragecpp.cpp @@ -0,0 +1,23 @@ +extern "C" { +#include "py/obj.h" +} + +#if defined(MICROPY_UNIX_COVERAGE) + +// Just to test building of C++ code. +STATIC mp_obj_t extra_cpp_coverage_impl() { + return mp_const_none; +} + +extern "C" { +mp_obj_t extra_cpp_coverage(void); +mp_obj_t extra_cpp_coverage(void) { + return extra_cpp_coverage_impl(); +} + +// This is extern to avoid name mangling. +extern const mp_obj_fun_builtin_fixed_t extra_cpp_coverage_obj = {{&mp_type_fun_builtin_0}, {extra_cpp_coverage}}; + +} + +#endif diff --git a/ports/unix/main.c b/ports/unix/main.c index 0fe492a554cbd..6f85cbf8d021d 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -531,7 +531,9 @@ MP_NOINLINE int main_(int argc, char **argv) { #if defined(MICROPY_UNIX_COVERAGE) { MP_DECLARE_CONST_FUN_OBJ_0(extra_coverage_obj); + MP_DECLARE_CONST_FUN_OBJ_0(extra_cpp_coverage_obj); mp_store_global(QSTR_FROM_STR_STATIC("extra_coverage"), MP_OBJ_FROM_PTR(&extra_coverage_obj)); + mp_store_global(QSTR_FROM_STR_STATIC("extra_cpp_coverage"), MP_OBJ_FROM_PTR(&extra_cpp_coverage_obj)); } #endif diff --git a/tests/unix/extra_coverage.py b/tests/unix/extra_coverage.py index 36105f6bad18c..1c028506e3fcc 100644 --- a/tests/unix/extra_coverage.py +++ b/tests/unix/extra_coverage.py @@ -46,6 +46,9 @@ buf = uio.BufferedWriter(stream, 8) print(buf.write(bytearray(16))) +# function defined in C++ code +print("cpp", extra_cpp_coverage()) + # test basic import of frozen scripts import frzstr1 diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 7d7b7dd9f92f2..514ff9437bfee 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -144,6 +144,7 @@ OSError 0 None None +cpp None frzstr1 frzstr1.py frzmpy1 From 25c4563f26c2270cde01b01dca0e8b801c9c8282 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 21 Oct 2020 11:13:47 +0200 Subject: [PATCH 0380/3533] examples: Add example code for user C modules, both C and C++. Add working example code to provide a starting point for users with files that they can just copy, and include the modules in the coverage test to verify the complete user C module build functionality. The cexample module uses the code originally found in cmodules.rst, which has been updated to reflect this and partially rewritten with more complete information. --- docs/develop/cmodules.rst | 160 ++++++++---------- examples/usercmodule/cexample/examplemodule.c | 34 ++++ examples/usercmodule/cexample/micropython.mk | 9 + examples/usercmodule/cppexample/example.cpp | 17 ++ .../usercmodule/cppexample/examplemodule.c | 25 +++ .../usercmodule/cppexample/examplemodule.h | 5 + .../usercmodule/cppexample/micropython.mk | 12 ++ .../unix/variants/coverage/mpconfigvariant.mk | 4 +- tests/unix/extra_coverage.py | 10 ++ tests/unix/extra_coverage.py.exp | 2 + 10 files changed, 192 insertions(+), 86 deletions(-) create mode 100644 examples/usercmodule/cexample/examplemodule.c create mode 100644 examples/usercmodule/cexample/micropython.mk create mode 100644 examples/usercmodule/cppexample/example.cpp create mode 100644 examples/usercmodule/cppexample/examplemodule.c create mode 100644 examples/usercmodule/cppexample/examplemodule.h create mode 100644 examples/usercmodule/cppexample/micropython.mk diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 849d0e60a2ea2..2d08df82820d9 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -49,8 +49,9 @@ A MicroPython user C module is a directory with the following files: expanded copy of ``$(USERMOD_DIR)`` to ``SRC_USERMOD``, eg ``SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c`` - If you have custom ``CFLAGS`` settings or include folders to define, these - should be added to ``CFLAGS_USERMOD``, or ``CXXFLAGS_USERMOD``. + If you have custom compiler options (like ``-I`` to add directories to search + for header files), these should be added to ``CFLAGS_USERMOD`` for C code + and to ``CXXFLAGS_USERMOD`` for C++ code. See below for full usage example. @@ -58,124 +59,113 @@ A MicroPython user C module is a directory with the following files: Basic example ------------- -This simple module named ``example`` provides a single function -``example.add_ints(a, b)`` which adds the two integer args together and returns -the result. +This simple module named ``cexample`` provides a single function +``cexample.add_ints(a, b)`` which adds the two integer args together and returns +the result. It can be found in the MicroPython source tree and has +a source file and a Makefile fragment with content as descibed above:: -Directory:: + micropython/ + └──examples/ + └──usercmodule/ + └──cexample/ + ├── examplemodule.c + └── micropython.mk - example/ - ├── example.c - └── micropython.mk +Refer to the comments in these 2 files for additional explanation. +Next to the ``cexample`` module there's also ``cppexample`` which +works in the same way but shows one way of mixing C and C++ code +in MicroPython. -``example.c`` - -.. code-block:: c - - // Include required definitions first. - #include "py/obj.h" - #include "py/runtime.h" - #include "py/builtin.h" - - // This is the function which will be called from Python as example.add_ints(a, b). - STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) { - // Extract the ints from the micropython input objects - int a = mp_obj_get_int(a_obj); - int b = mp_obj_get_int(b_obj); +Compiling the cmodule into MicroPython +-------------------------------------- - // Calculate the addition and convert to MicroPython object. - return mp_obj_new_int(a + b); - } - // Define a Python reference to the function above - STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints); +To build such a module, compile MicroPython (see `getting started +`_), +applying 2 modifications: - // Define all properties of the example module. - // Table entries are key/value pairs of the attribute name (a string) - // and the MicroPython object reference. - // All identifiers and strings are written as MP_QSTR_xxx and will be - // optimized to word-sized integers by the build system (interned strings). - STATIC const mp_rom_map_elem_t example_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_example) }, - { MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) }, - }; - STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table); +- an extra ``make`` flag named ``USER_C_MODULES`` set to the directory + containing all modules you want included (not to the module itself). + For building the example modules which come with MicroPython, + set ``USER_C_MODULES`` to the ``examples/usercmodule`` directory. + For your own projects it's more convenient to keep custom code out of + the main source tree so a typical project directory structure will look + like this:: - // Define module object. - const mp_obj_module_t example_user_cmodule = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&example_module_globals, - }; + my_project/ + ├── modules/ + │ └──example1/ + │ ├──example1.c + │ └──micropython.mk + │ └──example2/ + │ ├──example2.c + │ └──micropython.mk + └── micropython/ + ├──ports/ + ... ├──stm32/ + ... - // Register the module to make it available in Python - MP_REGISTER_MODULE(MP_QSTR_example, example_user_cmodule, MODULE_EXAMPLE_ENABLED); + with ``USER_C_MODULES`` set to the ``my_project/modules`` directory. -``micropython.mk`` +- all modules found in this directory will be compiled, but only those + which are explicitly enabled will be availabe for importing. Enabling a + module is done by setting the preprocessor define from its module + registration to 1. For example if the source code defines the module with -.. code-block:: make + .. code-block:: c - EXAMPLE_MOD_DIR := $(USERMOD_DIR) + MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); - # Add all C files to SRC_USERMOD. - SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c - # We can add our module folder to include paths if needed - # This is not actually needed in this example. - CFLAGS_USERMOD += -I$(EXAMPLE_MOD_DIR) + then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available. + This can be done by adding ``CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1`` to + the ``make`` command, or editing ``mpconfigport.h`` or ``mpconfigboard.h`` + to add -Finally you will need to define ``MODULE_EXAMPLE_ENABLED`` to 1. This -can be done by adding ``CFLAGS_EXTRA=-DMODULE_EXAMPLE_ENABLED=1`` to -the ``make`` command, or editing ``mpconfigport.h`` or -``mpconfigboard.h`` to add + .. code-block:: c -.. code-block:: c + #define MODULE_CEXAMPLE_ENABLED (1) - #define MODULE_EXAMPLE_ENABLED (1) -Note that the exact method depends on the port as they have different -structures. If not done correctly it will compile but importing will -fail to find the module. + Note that the exact method depends on the port as they have different + structures. If not done correctly it will compile but importing will + fail to find the module. +To sum up, here's how the ``cexample`` module from the ``examples/usercmodule`` +directory can be built for the unix port: -Compiling the cmodule into MicroPython --------------------------------------- +.. code-block:: bash -To build such a module, compile MicroPython (see `getting started -`_) with an -extra ``make`` flag named ``USER_C_MODULES`` set to the directory containing -all modules you want included (not to the module itself). For example: + cd micropython/ports/unix + make USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1 all +The build output will show the modules found:: -Directory:: + ... + Including User C Module from ../../examples/usercmodule/cexample + Including User C Module from ../../examples/usercmodule/cppexample + ... - my_project/ - ├── modules/ - │ └──example/ - │ ├──example.c - │ └──micropython.mk - └── micropython/ - ├──ports/ - ... ├──stm32/ - ... -Building for stm32 port: +Or for your own project with a directory structure as shown above, +including both modules and building the stm32 port for example: .. code-block:: bash cd my_project/micropython/ports/stm32 - make USER_C_MODULES=../../../modules CFLAGS_EXTRA=-DMODULE_EXAMPLE_ENABLED=1 all + make USER_C_MODULES=../../../modules \ + CFLAGS_EXTRA="-DMODULE_EXAMPLE1_ENABLED=1 -DMODULE_EXAMPLE2_ENABLED=1" all Module usage in MicroPython --------------------------- -Once built into your copy of MicroPython, the module implemented -in ``example.c`` above can now be accessed in Python just -like any other builtin module, eg +Once built into your copy of MicroPython, the module +can now be accessed in Python just like any other builtin module, e.g. .. code-block:: python - import example - print(example.add_ints(1, 3)) + import cexample + print(cexample.add_ints(1, 3)) # should display 4 diff --git a/examples/usercmodule/cexample/examplemodule.c b/examples/usercmodule/cexample/examplemodule.c new file mode 100644 index 0000000000000..f608823c9e51c --- /dev/null +++ b/examples/usercmodule/cexample/examplemodule.c @@ -0,0 +1,34 @@ +// Include MicroPython API. +#include "py/runtime.h" + +// This is the function which will be called from Python as cexample.add_ints(a, b). +STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) { + // Extract the ints from the micropython input objects. + int a = mp_obj_get_int(a_obj); + int b = mp_obj_get_int(b_obj); + + // Calculate the addition and convert to MicroPython object. + return mp_obj_new_int(a + b); +} +// Define a Python reference to the function above. +STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints); + +// Define all properties of the module. +// Table entries are key/value pairs of the attribute name (a string) +// and the MicroPython object reference. +// All identifiers and strings are written as MP_QSTR_xxx and will be +// optimized to word-sized integers by the build system (interned strings). +STATIC const mp_rom_map_elem_t example_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample) }, + { MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table); + +// Define module object. +const mp_obj_module_t example_user_cmodule = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&example_module_globals, +}; + +// Register the module to make it available in Python. +MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); diff --git a/examples/usercmodule/cexample/micropython.mk b/examples/usercmodule/cexample/micropython.mk new file mode 100644 index 0000000000000..dbfe3c5cbdb1f --- /dev/null +++ b/examples/usercmodule/cexample/micropython.mk @@ -0,0 +1,9 @@ +EXAMPLE_MOD_DIR := $(USERMOD_DIR) + +# Add all C files to SRC_USERMOD. +SRC_USERMOD += $(EXAMPLE_MOD_DIR)/examplemodule.c + +# We can add our module folder to include paths if needed +# This is not actually needed in this example. +CFLAGS_USERMOD += -I$(EXAMPLE_MOD_DIR) +CEXAMPLE_MOD_DIR := $(USERMOD_DIR) diff --git a/examples/usercmodule/cppexample/example.cpp b/examples/usercmodule/cppexample/example.cpp new file mode 100644 index 0000000000000..06809732a4d7b --- /dev/null +++ b/examples/usercmodule/cppexample/example.cpp @@ -0,0 +1,17 @@ +extern "C" { +#include + +// Here we implement the function using C++ code, but since it's +// declaration has to be compatible with C everything goes in extern "C" scope. +mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) { + // Prove we have (at least) C++11 features. + const auto a = mp_obj_get_int(a_obj); + const auto b = mp_obj_get_int(b_obj); + const auto sum = [&]() { + return mp_obj_new_int(a + b); + } (); + // Prove we're being scanned for QSTRs. + mp_obj_t tup[] = {sum, MP_ROM_QSTR(MP_QSTR_hellocpp)}; + return mp_obj_new_tuple(2, tup); +} +} diff --git a/examples/usercmodule/cppexample/examplemodule.c b/examples/usercmodule/cppexample/examplemodule.c new file mode 100644 index 0000000000000..ceb588bef6b30 --- /dev/null +++ b/examples/usercmodule/cppexample/examplemodule.c @@ -0,0 +1,25 @@ +#include + +// Define a Python reference to the function we'll make available. +// See example.cpp for the definition. +STATIC MP_DEFINE_CONST_FUN_OBJ_2(cppfunc_obj, cppfunc); + +// Define all properties of the module. +// Table entries are key/value pairs of the attribute name (a string) +// and the MicroPython object reference. +// All identifiers and strings are written as MP_QSTR_xxx and will be +// optimized to word-sized integers by the build system (interned strings). +STATIC const mp_rom_map_elem_t cppexample_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cppexample) }, + { MP_ROM_QSTR(MP_QSTR_cppfunc), MP_ROM_PTR(&cppfunc_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(cppexample_module_globals, cppexample_module_globals_table); + +// Define module object. +const mp_obj_module_t cppexample_user_cmodule = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&cppexample_module_globals, +}; + +// Register the module to make it available in Python. +MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, MODULE_CPPEXAMPLE_ENABLED); diff --git a/examples/usercmodule/cppexample/examplemodule.h b/examples/usercmodule/cppexample/examplemodule.h new file mode 100644 index 0000000000000..d89384a630060 --- /dev/null +++ b/examples/usercmodule/cppexample/examplemodule.h @@ -0,0 +1,5 @@ +// Include MicroPython API. +#include "py/runtime.h" + +// Declare the function we'll make available in Python as cppexample.cppfunc(). +extern mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj); diff --git a/examples/usercmodule/cppexample/micropython.mk b/examples/usercmodule/cppexample/micropython.mk new file mode 100644 index 0000000000000..e10d965a0011e --- /dev/null +++ b/examples/usercmodule/cppexample/micropython.mk @@ -0,0 +1,12 @@ +CPPEXAMPLE_MOD_DIR := $(USERMOD_DIR) + +# Add our source files to the respective variables. +SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/examplemodule.c +SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/example.cpp + +# Add our module directory to the include path. +CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR) +CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR) + +# We use C++ features so have to link against the standard library. +LDFLAGS_USERMOD += -lstdc++ diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index f11d0b0d28f75..55399831aaa0a 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -7,11 +7,13 @@ CFLAGS += \ -fprofile-arcs -ftest-coverage \ -Wformat -Wmissing-declarations -Wmissing-prototypes \ -Wold-style-definition -Wpointer-arith -Wshadow -Wuninitialized -Wunused-parameter \ - -DMICROPY_UNIX_COVERAGE + -DMICROPY_UNIX_COVERAGE \ + -DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1 LDFLAGS += -fprofile-arcs -ftest-coverage FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py +USER_C_MODULES = $(TOP)/examples/usercmodule MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_VFS_FAT = 1 diff --git a/tests/unix/extra_coverage.py b/tests/unix/extra_coverage.py index 1c028506e3fcc..b4808993a760e 100644 --- a/tests/unix/extra_coverage.py +++ b/tests/unix/extra_coverage.py @@ -49,6 +49,16 @@ # function defined in C++ code print("cpp", extra_cpp_coverage()) +# test user C module +import cexample + +print(cexample.add_ints(3, 2)) + +# test user C module mixed with C++ code +import cppexample + +print(cppexample.cppfunc(1, 2)) + # test basic import of frozen scripts import frzstr1 diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 514ff9437bfee..257224108a4c5 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -145,6 +145,8 @@ OSError None None cpp None +5 +(3, 'hellocpp') frzstr1 frzstr1.py frzmpy1 From dbb13104ca612d1b2b289adc51c028a1bd1f7d1d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 17:32:39 +1100 Subject: [PATCH 0381/3533] docs/develop/cmodules.rst: Add link to source code for user C example. Signed-off-by: Damien George --- docs/develop/cmodules.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 2d08df82820d9..2db1f65f2e488 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -61,8 +61,9 @@ Basic example This simple module named ``cexample`` provides a single function ``cexample.add_ints(a, b)`` which adds the two integer args together and returns -the result. It can be found in the MicroPython source tree and has -a source file and a Makefile fragment with content as descibed above:: +the result. It can be found in the MicroPython source tree +`in the examples directory `_ +and has a source file and a Makefile fragment with content as descibed above:: micropython/ └──examples/ From a866f868f87e50b9aa86a2f2bec8cfc9fb687437 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 17:33:34 +1100 Subject: [PATCH 0382/3533] unix/Makefile: Move coverage.c and coveragecpp.cpp to coverage variant. So that g++ is not needed to build a non-coverage unix variant. Signed-off-by: Damien George --- ports/unix/Makefile | 4 +--- ports/unix/variants/coverage/mpconfigvariant.mk | 3 +++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 3388d67a2d6f8..048ef97f77531 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -220,7 +220,7 @@ SRC_MOD += modjni.c endif # source files -SRC_C = \ +SRC_C += \ main.c \ gccollect.c \ unix_mphal.c \ @@ -232,7 +232,6 @@ SRC_C = \ modtime.c \ moduselect.c \ alloc.c \ - coverage.c \ fatfs_port.c \ mpbthciport.c \ mpbtstackport_common.c \ @@ -249,7 +248,6 @@ LIB_SRC_C += $(addprefix lib/,\ ) SRC_CXX += \ - coveragecpp.cpp \ $(SRC_MOD_CXX) OBJ = $(PY_O) diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 55399831aaa0a..ef81975d9da8c 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -19,3 +19,6 @@ MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_VFS_FAT = 1 MICROPY_VFS_LFS1 = 1 MICROPY_VFS_LFS2 = 1 + +SRC_C += coverage.c +SRC_CXX += coveragecpp.cpp From df3b466d6c44af494e404e54861b4c25cf4d54c8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 23:07:51 +1100 Subject: [PATCH 0383/3533] stm32/boards: Factor out common data/bss/heap/stack linker sections. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/f722_qspi.ld | 37 +------------- ports/stm32/boards/PYBD_SF6/f767.ld | 36 +------------- ports/stm32/boards/STM32F769DISC/f769_qspi.ld | 41 +--------------- ports/stm32/boards/common_basic.ld | 48 +------------------ ports/stm32/boards/common_bl.ld | 48 +------------------ ports/stm32/boards/common_blifs.ld | 48 +------------------ ports/stm32/boards/common_bss_heap_stack.ld | 28 +++++++++++ .../boards/common_extratext_data_in_flash.ld | 14 ++++++ .../common_extratext_data_in_flash_app.ld | 14 ++++++ .../common_extratext_data_in_flash_text.ld | 14 ++++++ ports/stm32/boards/common_ifs.ld | 48 +------------------ 11 files changed, 84 insertions(+), 292 deletions(-) create mode 100644 ports/stm32/boards/common_bss_heap_stack.ld create mode 100644 ports/stm32/boards/common_extratext_data_in_flash.ld create mode 100644 ports/stm32/boards/common_extratext_data_in_flash_app.ld create mode 100644 ports/stm32/boards/common_extratext_data_in_flash_text.ld diff --git a/ports/stm32/boards/PYBD_SF2/f722_qspi.ld b/ports/stm32/boards/PYBD_SF2/f722_qspi.ld index 3b2e45d9b6a11..c9199b341bfd4 100644 --- a/ports/stm32/boards/PYBD_SF2/f722_qspi.ld +++ b/ports/stm32/boards/PYBD_SF2/f722_qspi.ld @@ -72,39 +72,6 @@ SECTIONS _etext = .; } >FLASH_APP - _sidata = LOADADDR(.data); - - .data : - { - . = ALIGN(4); - _sdata = .; - *(.data*) - - . = ALIGN(4); - _edata = .; - } >RAM AT> FLASH_APP - - .bss : - { - . = ALIGN(4); - _sbss = .; - *(.bss*) - *(COMMON) - . = ALIGN(4); - _ebss = .; - } >RAM - - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM + INCLUDE common_extratext_data_in_flash_app.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/PYBD_SF6/f767.ld b/ports/stm32/boards/PYBD_SF6/f767.ld index 1dd4c11ed9898..5866f0b5cc2d5 100644 --- a/ports/stm32/boards/PYBD_SF6/f767.ld +++ b/ports/stm32/boards/PYBD_SF6/f767.ld @@ -62,38 +62,6 @@ SECTIONS _etext = .; } >FLASH_APP - _sidata = LOADADDR(.data); - - .data : - { - . = ALIGN(4); - _sdata = .; - *(.data*) - . = ALIGN(4); - _edata = .; - } >RAM AT> FLASH_APP - - .bss : - { - . = ALIGN(4); - _sbss = .; - *(.bss*) - *(COMMON) - . = ALIGN(4); - _ebss = .; - } >RAM - - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM + INCLUDE common_extratext_data_in_flash_app.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/STM32F769DISC/f769_qspi.ld b/ports/stm32/boards/STM32F769DISC/f769_qspi.ld index 5f920b417fdde..b6957a321332c 100644 --- a/ports/stm32/boards/STM32F769DISC/f769_qspi.ld +++ b/ports/stm32/boards/STM32F769DISC/f769_qspi.ld @@ -68,43 +68,6 @@ SECTIONS _etext = .; } >FLASH_APP - /* Used by the startup to initialize data */ - _sidata = LOADADDR(.data); - - /* The initialized data section */ - .data : - { - . = ALIGN(4); - _sdata = .; - *(.data*) - . = ALIGN(4); - _edata = .; - } >RAM AT> FLASH_APP - - /* The uninitialized (zeroed) data section */ - .bss : - { - . = ALIGN(4); - _sbss = .; - *(.bss*) - *(COMMON) - . = ALIGN(4); - _ebss = .; - } >RAM - - /* Define the start of the heap, and make sure we have a minimum size */ - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - /* Just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM + INCLUDE common_extratext_data_in_flash_app.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/common_basic.ld b/ports/stm32/boards/common_basic.ld index 2e428aa62c573..dbda1b8b68ba4 100644 --- a/ports/stm32/boards/common_basic.ld +++ b/ports/stm32/boards/common_basic.ld @@ -37,50 +37,6 @@ SECTIONS _etext = .; /* define a global symbol at end of code */ } >FLASH - /* used by the startup to initialize data */ - _sidata = LOADADDR(.data); - - /* This is the initialized data section - The program executes knowing that the data is in the RAM - but the loader puts the initial values in the FLASH (inidata). - It is one task of the startup to copy the initial values from FLASH to RAM. */ - .data : - { - . = ALIGN(4); - _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ - *(.data*) /* .data* sections */ - - . = ALIGN(4); - _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ - } >RAM AT> FLASH - - /* Uninitialized data section */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* define a global symbol at bss start; used by startup code */ - *(.bss*) - *(COMMON) - - . = ALIGN(4); - _ebss = .; /* define a global symbol at bss end; used by startup code and GC */ - } >RAM - - /* this is to define the start of the heap, and make sure we have a minimum size */ - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - /* this just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM - - .ARM.attributes 0 : { *(.ARM.attributes) } + INCLUDE common_extratext_data_in_flash.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/common_bl.ld b/ports/stm32/boards/common_bl.ld index 52b2a677d706d..21d809a3d2339 100644 --- a/ports/stm32/boards/common_bl.ld +++ b/ports/stm32/boards/common_bl.ld @@ -37,50 +37,6 @@ SECTIONS _etext = .; /* define a global symbol at end of code */ } >FLASH_APP - /* used by the startup to initialize data */ - _sidata = LOADADDR(.data); - - /* This is the initialized data section - The program executes knowing that the data is in the RAM - but the loader puts the initial values in the FLASH (inidata). - It is one task of the startup to copy the initial values from FLASH to RAM. */ - .data : - { - . = ALIGN(4); - _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ - *(.data*) /* .data* sections */ - - . = ALIGN(4); - _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ - } >RAM AT> FLASH_APP - - /* Uninitialized data section */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* define a global symbol at bss start; used by startup code */ - *(.bss*) - *(COMMON) - - . = ALIGN(4); - _ebss = .; /* define a global symbol at bss end; used by startup code and GC */ - } >RAM - - /* this is to define the start of the heap, and make sure we have a minimum size */ - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - /* this just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM - - .ARM.attributes 0 : { *(.ARM.attributes) } + INCLUDE common_extratext_data_in_flash_app.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/common_blifs.ld b/ports/stm32/boards/common_blifs.ld index 65722f2e57e8d..5517a2d09c69c 100644 --- a/ports/stm32/boards/common_blifs.ld +++ b/ports/stm32/boards/common_blifs.ld @@ -37,50 +37,6 @@ SECTIONS _etext = .; /* define a global symbol at end of code */ } >FLASH_TEXT - /* used by the startup to initialize data */ - _sidata = LOADADDR(.data); - - /* This is the initialized data section - The program executes knowing that the data is in the RAM - but the loader puts the initial values in the FLASH (inidata). - It is one task of the startup to copy the initial values from FLASH to RAM. */ - .data : - { - . = ALIGN(4); - _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ - *(.data*) /* .data* sections */ - - . = ALIGN(4); - _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ - } >RAM AT> FLASH_TEXT - - /* Uninitialized data section */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* define a global symbol at bss start; used by startup code */ - *(.bss*) - *(COMMON) - - . = ALIGN(4); - _ebss = .; /* define a global symbol at bss end; used by startup code and GC */ - } >RAM - - /* this is to define the start of the heap, and make sure we have a minimum size */ - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - /* this just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM - - .ARM.attributes 0 : { *(.ARM.attributes) } + INCLUDE common_extratext_data_in_flash_text.ld + INCLUDE common_bss_heap_stack.ld } diff --git a/ports/stm32/boards/common_bss_heap_stack.ld b/ports/stm32/boards/common_bss_heap_stack.ld new file mode 100644 index 0000000000000..1bb2249e96c9b --- /dev/null +++ b/ports/stm32/boards/common_bss_heap_stack.ld @@ -0,0 +1,28 @@ +/* This linker script fragment is intended to be included in SECTIONS. */ + +/* Zeroed-out data section */ +.bss : +{ + . = ALIGN(4); + _sbss = .; + *(.bss*) + *(COMMON) + . = ALIGN(4); + _ebss = .; +} >RAM + +/* This is to define the start of the heap, and make sure there is a minimum size */ +.heap : +{ + . = ALIGN(4); + . = . + _minimum_heap_size; + . = ALIGN(4); +} >RAM + +/* This checks there is enough RAM for the stack */ +.stack : +{ + . = ALIGN(4); + . = . + _minimum_stack_size; + . = ALIGN(4); +} >RAM diff --git a/ports/stm32/boards/common_extratext_data_in_flash.ld b/ports/stm32/boards/common_extratext_data_in_flash.ld new file mode 100644 index 0000000000000..e5f25a3c6f649 --- /dev/null +++ b/ports/stm32/boards/common_extratext_data_in_flash.ld @@ -0,0 +1,14 @@ +/* This linker script fragment is intended to be included in SECTIONS. */ + +/* Used by the start-up code to initialise data */ +_sidata = LOADADDR(.data); + +/* Initialised data section, start-up code will copy it from flash to RAM */ +.data : +{ + . = ALIGN(4); + _sdata = .; + *(.data*) + . = ALIGN(4); + _edata = .; +} >RAM AT> FLASH diff --git a/ports/stm32/boards/common_extratext_data_in_flash_app.ld b/ports/stm32/boards/common_extratext_data_in_flash_app.ld new file mode 100644 index 0000000000000..8230f8f9b2c38 --- /dev/null +++ b/ports/stm32/boards/common_extratext_data_in_flash_app.ld @@ -0,0 +1,14 @@ +/* This linker script fragment is intended to be included in SECTIONS. */ + +/* Used by the start-up code to initialise data */ +_sidata = LOADADDR(.data); + +/* Initialised data section, start-up code will copy it from flash to RAM */ +.data : +{ + . = ALIGN(4); + _sdata = .; + *(.data*) + . = ALIGN(4); + _edata = .; +} >RAM AT> FLASH_APP diff --git a/ports/stm32/boards/common_extratext_data_in_flash_text.ld b/ports/stm32/boards/common_extratext_data_in_flash_text.ld new file mode 100644 index 0000000000000..526d2519f7bff --- /dev/null +++ b/ports/stm32/boards/common_extratext_data_in_flash_text.ld @@ -0,0 +1,14 @@ +/* This linker script fragment is intended to be included in SECTIONS. */ + +/* Used by the start-up code to initialise data */ +_sidata = LOADADDR(.data); + +/* Initialised data section, start-up code will copy it from flash to RAM */ +.data : +{ + . = ALIGN(4); + _sdata = .; + *(.data*) + . = ALIGN(4); + _edata = .; +} >RAM AT> FLASH_TEXT diff --git a/ports/stm32/boards/common_ifs.ld b/ports/stm32/boards/common_ifs.ld index 74b2ffb419406..733ca12f675bf 100644 --- a/ports/stm32/boards/common_ifs.ld +++ b/ports/stm32/boards/common_ifs.ld @@ -54,50 +54,6 @@ SECTIONS _etext = .; /* define a global symbol at end of code */ } >FLASH_TEXT - /* used by the startup to initialize data */ - _sidata = LOADADDR(.data); - - /* This is the initialized data section - The program executes knowing that the data is in the RAM - but the loader puts the initial values in the FLASH (inidata). - It is one task of the startup to copy the initial values from FLASH to RAM. */ - .data : - { - . = ALIGN(4); - _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ - *(.data*) /* .data* sections */ - - . = ALIGN(4); - _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ - } >RAM AT> FLASH_TEXT - - /* Uninitialized data section */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* define a global symbol at bss start; used by startup code */ - *(.bss*) - *(COMMON) - - . = ALIGN(4); - _ebss = .; /* define a global symbol at bss end; used by startup code and GC */ - } >RAM - - /* this is to define the start of the heap, and make sure we have a minimum size */ - .heap : - { - . = ALIGN(4); - . = . + _minimum_heap_size; - . = ALIGN(4); - } >RAM - - /* this just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - . = . + _minimum_stack_size; - . = ALIGN(4); - } >RAM - - .ARM.attributes 0 : { *(.ARM.attributes) } + INCLUDE common_extratext_data_in_flash_text.ld + INCLUDE common_bss_heap_stack.ld } From 97960dc7deb7a0e691fca5944402cd03386b744b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 17:34:38 +1100 Subject: [PATCH 0384/3533] stm32: Support C++ code and user C modules written in C++. Also build user C modules as part of the stm32 CI. Signed-off-by: Damien George --- .travis.yml | 2 +- ports/stm32/Makefile | 13 ++++++++++++- .../stm32/boards/common_extratext_data_in_flash.ld | 8 ++++++++ .../boards/common_extratext_data_in_flash_app.ld | 8 ++++++++ .../boards/common_extratext_data_in_flash_text.ld | 8 ++++++++ ports/stm32/main.c | 4 ++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3b399804e37a3..14595ddeb1d39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -99,7 +99,7 @@ jobs: - make ${MAKEOPTS} -C ports/stm32 submodules - git submodule update --init lib/btstack - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_F091RC - - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 + - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA="-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1" - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_H743ZI CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index cf3a589ca9087..61da9cc9807aa 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -138,6 +138,13 @@ else COPT += -Os -DNDEBUG endif +# Flags for optional C++ source code +CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS)) +CXXFLAGS += $(CXXFLAGS_MOD) +ifneq ($(SRC_CXX)$(SRC_MOD_CXX),) +LDFLAGS += -L$(dir $(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)) +endif + # Options for mpy-cross MPY_CROSS_FLAGS += -march=armv7m @@ -330,6 +337,9 @@ SRC_C += \ adc.c \ $(wildcard $(BOARD_DIR)/*.c) +SRC_CXX += \ + $(SRC_MOD_CXX) + SRC_O += \ $(STARTUP_FILE) \ $(SYSTEM_FILE) @@ -511,6 +521,7 @@ OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o)) @@ -644,7 +655,7 @@ GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_MOD) $(LIB_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SRC_MOD) $(LIB_SRC_C) $(EXTMOD_SRC_C) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR SRC_QSTR_AUTO_DEPS += $(GEN_CDCINF_HEADER) diff --git a/ports/stm32/boards/common_extratext_data_in_flash.ld b/ports/stm32/boards/common_extratext_data_in_flash.ld index e5f25a3c6f649..eb9b86f49d8e0 100644 --- a/ports/stm32/boards/common_extratext_data_in_flash.ld +++ b/ports/stm32/boards/common_extratext_data_in_flash.ld @@ -1,5 +1,13 @@ /* This linker script fragment is intended to be included in SECTIONS. */ +/* For C++ exception handling */ +.ARM : +{ + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; +} >FLASH + /* Used by the start-up code to initialise data */ _sidata = LOADADDR(.data); diff --git a/ports/stm32/boards/common_extratext_data_in_flash_app.ld b/ports/stm32/boards/common_extratext_data_in_flash_app.ld index 8230f8f9b2c38..aba6bf57c88af 100644 --- a/ports/stm32/boards/common_extratext_data_in_flash_app.ld +++ b/ports/stm32/boards/common_extratext_data_in_flash_app.ld @@ -1,5 +1,13 @@ /* This linker script fragment is intended to be included in SECTIONS. */ +/* For C++ exception handling */ +.ARM : +{ + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; +} >FLASH_APP + /* Used by the start-up code to initialise data */ _sidata = LOADADDR(.data); diff --git a/ports/stm32/boards/common_extratext_data_in_flash_text.ld b/ports/stm32/boards/common_extratext_data_in_flash_text.ld index 526d2519f7bff..5a29e47307a6b 100644 --- a/ports/stm32/boards/common_extratext_data_in_flash_text.ld +++ b/ports/stm32/boards/common_extratext_data_in_flash_text.ld @@ -1,5 +1,13 @@ /* This linker script fragment is intended to be included in SECTIONS. */ +/* For C++ exception handling */ +.ARM : +{ + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; +} >FLASH_TEXT + /* Used by the start-up code to initialise data */ _sidata = LOADADDR(.data); diff --git a/ports/stm32/main.c b/ports/stm32/main.c index b5dbfa50fd5cc..f19dac0e7947b 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -133,6 +133,10 @@ void nlr_jump_fail(void *val) { __fatal_error(""); } +void abort(void) { + __fatal_error("abort"); +} + #ifndef NDEBUG void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { (void)func; From ed7ddd4dd436fb84e602fee4dbdc4882eca642ab Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Oct 2020 17:36:04 +1100 Subject: [PATCH 0385/3533] tests/micropython/extreme_exc.py: Unlink alloc'd lists earlier in chain. To help the GC collect this memory that's no longer needed after the test. Signed-off-by: Damien George --- tests/micropython/extreme_exc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/micropython/extreme_exc.py b/tests/micropython/extreme_exc.py index dae5b151860fc..9d2f24745f933 100644 --- a/tests/micropython/extreme_exc.py +++ b/tests/micropython/extreme_exc.py @@ -126,7 +126,7 @@ def f(): ) except Exception as er: e = er - lst[0] = None + lst[0][0] = None lst = None print(repr(e)[:10]) From 2ae3c890bd923b4c39bba3d2e2f2d75eca5dcc06 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Oct 2020 15:47:20 +1100 Subject: [PATCH 0386/3533] extmod/btstack/btstack.mk: Add -Wimplicit-fallthrough=0. This is needed since -Wextra was added to the build in bef412789ea93c521bd9c2dddc22b9b3484da574 Signed-off-by: Jim Mussared --- extmod/btstack/btstack.mk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index 7e5d2f646d14c..e3309b61db7a4 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -66,11 +66,12 @@ endif LIB_SRC_C += $(SRC_BTSTACK) # Suppress some warnings. -BTSTACK_WARNING_CFLAGS = -Wno-old-style-definition -Wno-unused-variable -Wno-unused-parameter +BTSTACK_WARNING_CFLAGS = -Wno-old-style-definition -Wno-unused-variable -Wno-unused-parameter -Wimplicit-fallthrough=0 ifneq ($(CC),clang) BTSTACK_WARNING_CFLAGS += -Wno-format endif $(BUILD)/lib/btstack/src/%.o: CFLAGS += $(BTSTACK_WARNING_CFLAGS) +$(BUILD)/lib/btstack/platform/%.o: CFLAGS += $(BTSTACK_WARNING_CFLAGS) endif endif From b7883ce74c5a9b9689d812d134117d625fd42e73 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 4 Nov 2020 10:04:26 +1100 Subject: [PATCH 0387/3533] extmod/nimble/nimble.mk: Add -Wno-old-style-declaration. This is needed since -Wextra was added to the build in bef412789ea93c521bd9c2dddc22b9b3484da574 Signed-off-by: Jim Mussared --- extmod/nimble/nimble.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index f8a68bc6f52b6..fbd031b3e3e16 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -101,7 +101,7 @@ INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/include INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/transport/uart/include INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/porting/nimble/include -$(BUILD)/$(NIMBLE_LIB_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format -Wno-sign-compare +$(BUILD)/$(NIMBLE_LIB_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format -Wno-sign-compare -Wno-old-style-declaration endif From 1e297c88989592258965b69cb740039e26c7636c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Oct 2020 17:38:56 +1000 Subject: [PATCH 0388/3533] stm32/main: Move update_reset_mode to outside the soft-reset loop. Running the update inside the soft-reset loop will mean that (on boards like PYBD that use a bootloader) the same reset mode is used each reset loop, eg factory reset occurs each time. Signed-off-by: Damien George --- ports/stm32/main.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index f19dac0e7947b..7f8ee84fd4d6b 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -552,6 +552,11 @@ void stm32_main(uint32_t reset_mode) { MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL - 1] = &pyb_uart_repl_obj; #endif + #if !MICROPY_HW_USES_BOOTLOADER + // check if user switch held to select the reset mode + reset_mode = update_reset_mode(1); + #endif + soft_reset: #if defined(MICROPY_HW_LED2) @@ -564,11 +569,6 @@ void stm32_main(uint32_t reset_mode) { led_state(3, 0); led_state(4, 0); - #if !MICROPY_HW_USES_BOOTLOADER - // check if user switch held to select the reset mode - reset_mode = update_reset_mode(1); - #endif - // Python threading init #if MICROPY_PY_THREAD mp_thread_init(); @@ -776,5 +776,8 @@ void stm32_main(uint32_t reset_mode) { gc_sweep_all(); + // Set reset_mode to normal boot. + reset_mode = 1; + goto soft_reset; } From 4c3976bbcaf58266fdcaab264fee5b7a94a682e5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Oct 2020 00:44:18 +1100 Subject: [PATCH 0389/3533] stm32: Add MICROPY_BOARD calls in various places in stm32_main. For a board to have full configurability of the soft reset loop. Signed-off-by: Damien George --- ports/stm32/Makefile | 1 + ports/stm32/boardctrl.c | 183 ++++++++++++++++++++++++++++++++++++++++ ports/stm32/boardctrl.h | 80 ++++++++++++++++++ ports/stm32/main.c | 164 ++++++++--------------------------- 4 files changed, 298 insertions(+), 130 deletions(-) create mode 100644 ports/stm32/boardctrl.c create mode 100644 ports/stm32/boardctrl.h diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 61da9cc9807aa..fa9998a8e078b 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -267,6 +267,7 @@ DRIVERS_SRC_C = $(addprefix drivers/,\ ) SRC_C += \ + boardctrl.c \ main.c \ stm32_it.c \ usbd_conf.c \ diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c new file mode 100644 index 0000000000000..188068d705a2a --- /dev/null +++ b/ports/stm32/boardctrl.c @@ -0,0 +1,183 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mphal.h" +#include "lib/utils/pyexec.h" +#include "boardctrl.h" +#include "led.h" +#include "usrsw.h" + +STATIC void flash_error(int n) { + for (int i = 0; i < n; i++) { + led_state(PYB_LED_RED, 1); + led_state(PYB_LED_GREEN, 0); + mp_hal_delay_ms(250); + led_state(PYB_LED_RED, 0); + led_state(PYB_LED_GREEN, 1); + mp_hal_delay_ms(250); + } + led_state(PYB_LED_GREEN, 0); +} + +#if !MICROPY_HW_USES_BOOTLOADER +STATIC uint update_reset_mode(uint reset_mode) { + #if MICROPY_HW_HAS_SWITCH + if (switch_get()) { + + // The original method used on the pyboard is appropriate if you have 2 + // or more LEDs. + #if defined(MICROPY_HW_LED2) + for (uint i = 0; i < 3000; i++) { + if (!switch_get()) { + break; + } + mp_hal_delay_ms(20); + if (i % 30 == 29) { + if (++reset_mode > 3) { + reset_mode = 1; + } + led_state(2, reset_mode & 1); + led_state(3, reset_mode & 2); + led_state(4, reset_mode & 4); + } + } + // flash the selected reset mode + for (uint i = 0; i < 6; i++) { + led_state(2, 0); + led_state(3, 0); + led_state(4, 0); + mp_hal_delay_ms(50); + led_state(2, reset_mode & 1); + led_state(3, reset_mode & 2); + led_state(4, reset_mode & 4); + mp_hal_delay_ms(50); + } + mp_hal_delay_ms(400); + + #elif defined(MICROPY_HW_LED1) + + // For boards with only a single LED, we'll flash that LED the + // appropriate number of times, with a pause between each one + for (uint i = 0; i < 10; i++) { + led_state(1, 0); + for (uint j = 0; j < reset_mode; j++) { + if (!switch_get()) { + break; + } + led_state(1, 1); + mp_hal_delay_ms(100); + led_state(1, 0); + mp_hal_delay_ms(200); + } + mp_hal_delay_ms(400); + if (!switch_get()) { + break; + } + if (++reset_mode > 3) { + reset_mode = 1; + } + } + // Flash the selected reset mode + for (uint i = 0; i < 2; i++) { + for (uint j = 0; j < reset_mode; j++) { + led_state(1, 1); + mp_hal_delay_ms(100); + led_state(1, 0); + mp_hal_delay_ms(200); + } + mp_hal_delay_ms(400); + } + #else + #error Need a reset mode update method + #endif + } + #endif + return reset_mode; +} +#endif + +void boardctrl_before_soft_reset_loop(boardctrl_state_t *state) { + #if !MICROPY_HW_USES_BOOTLOADER + // Update the reset_mode via the default + // method which uses the board switch/button and LEDs. + state->reset_mode = update_reset_mode(1); + #endif +} + +void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { + // Turn on a single LED to indicate start up. + #if defined(MICROPY_HW_LED2) + led_state(1, 0); + led_state(2, 1); + #else + led_state(1, 1); + led_state(2, 0); + #endif + led_state(3, 0); + led_state(4, 0); +} + +void boardctrl_before_boot_py(boardctrl_state_t *state) { + state->run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; +} + +void boardctrl_after_boot_py(boardctrl_state_t *state) { + if (state->run_boot_py && !state->last_ret) { + flash_error(4); + } + + // Turn boot-up LEDs off + + #if !defined(MICROPY_HW_LED2) + // If there is only one LED on the board then it's used to signal boot-up + // and so we turn it off here. Otherwise LED(1) is used to indicate dirty + // flash cache and so we shouldn't change its state. + led_state(1, 0); + #endif + led_state(2, 0); + led_state(3, 0); + led_state(4, 0); +} + +void boardctrl_before_main_py(boardctrl_state_t *state) { + state->run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) + && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; +} + +void boardctrl_after_main_py(boardctrl_state_t *state) { + if (state->run_main_py && !state->last_ret) { + flash_error(3); + } +} + +void boardctrl_start_soft_reset(boardctrl_state_t *state) { + state->log_soft_reset = true; +} + +void boardctrl_end_soft_reset(boardctrl_state_t *state) { + // Set reset_mode to normal boot. + state->reset_mode = 1; +} diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h new file mode 100644 index 0000000000000..05bade305c254 --- /dev/null +++ b/ports/stm32/boardctrl.h @@ -0,0 +1,80 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_STM32_BOARDCTRL_H +#define MICROPY_INCLUDED_STM32_BOARDCTRL_H + +#include "py/mpconfig.h" + +#ifndef MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP +#define MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP boardctrl_before_soft_reset_loop +#endif + +#ifndef MICROPY_BOARD_TOP_SOFT_RESET_LOOP +#define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop +#endif + +#ifndef MICROPY_BOARD_BEFORE_BOOT_PY +#define MICROPY_BOARD_BEFORE_BOOT_PY boardctrl_before_boot_py +#endif + +#ifndef MICROPY_BOARD_AFTER_BOOT_PY +#define MICROPY_BOARD_AFTER_BOOT_PY boardctrl_after_boot_py +#endif + +#ifndef MICROPY_BOARD_BEFORE_MAIN_PY +#define MICROPY_BOARD_BEFORE_MAIN_PY boardctrl_before_main_py +#endif + +#ifndef MICROPY_BOARD_AFTER_MAIN_PY +#define MICROPY_BOARD_AFTER_MAIN_PY boardctrl_after_main_py +#endif + +#ifndef MICROPY_BOARD_START_SOFT_RESET +#define MICROPY_BOARD_START_SOFT_RESET boardctrl_start_soft_reset +#endif + +#ifndef MICROPY_BOARD_END_SOFT_RESET +#define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset +#endif + +typedef struct _boardctrl_state_t { + uint8_t reset_mode; + bool run_boot_py; + bool run_main_py; + bool log_soft_reset; + int last_ret; +} boardctrl_state_t; + +void boardctrl_before_soft_reset_loop(boardctrl_state_t *state); +void boardctrl_top_soft_reset_loop(boardctrl_state_t *state); +void boardctrl_before_boot_py(boardctrl_state_t *state); +void boardctrl_after_boot_py(boardctrl_state_t *state); +void boardctrl_before_main_py(boardctrl_state_t *state); +void boardctrl_after_main_py(boardctrl_state_t *state); +void boardctrl_start_soft_reset(boardctrl_state_t *state); +void boardctrl_end_soft_reset(boardctrl_state_t *state); + +#endif // MICROPY_INCLUDED_STM32_BOARDCTRL_H diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 7f8ee84fd4d6b..c749cac978ba8 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2018 Damien P. George + * Copyright (c) 2013-2020 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -53,6 +53,7 @@ #include "extmod/modbluetooth.h" #endif +#include "boardctrl.h" #include "mpu.h" #include "rfcore.h" #include "systick.h" @@ -95,18 +96,6 @@ STATIC pyb_uart_obj_t pyb_uart_repl_obj; STATIC uint8_t pyb_uart_repl_rxbuf[MICROPY_HW_UART_REPL_RXBUF]; #endif -void flash_error(int n) { - for (int i = 0; i < n; i++) { - led_state(PYB_LED_RED, 1); - led_state(PYB_LED_GREEN, 0); - mp_hal_delay_ms(250); - led_state(PYB_LED_RED, 0); - led_state(PYB_LED_GREEN, 1); - mp_hal_delay_ms(250); - } - led_state(PYB_LED_GREEN, 0); -} - void NORETURN __fatal_error(const char *msg) { for (volatile uint delay = 0; delay < 10000000; delay++) { } @@ -310,83 +299,6 @@ STATIC bool init_sdcard_fs(void) { } #endif -#if !MICROPY_HW_USES_BOOTLOADER -STATIC uint update_reset_mode(uint reset_mode) { - #if MICROPY_HW_HAS_SWITCH - if (switch_get()) { - - // The original method used on the pyboard is appropriate if you have 2 - // or more LEDs. - #if defined(MICROPY_HW_LED2) - for (uint i = 0; i < 3000; i++) { - if (!switch_get()) { - break; - } - mp_hal_delay_ms(20); - if (i % 30 == 29) { - if (++reset_mode > 3) { - reset_mode = 1; - } - led_state(2, reset_mode & 1); - led_state(3, reset_mode & 2); - led_state(4, reset_mode & 4); - } - } - // flash the selected reset mode - for (uint i = 0; i < 6; i++) { - led_state(2, 0); - led_state(3, 0); - led_state(4, 0); - mp_hal_delay_ms(50); - led_state(2, reset_mode & 1); - led_state(3, reset_mode & 2); - led_state(4, reset_mode & 4); - mp_hal_delay_ms(50); - } - mp_hal_delay_ms(400); - - #elif defined(MICROPY_HW_LED1) - - // For boards with only a single LED, we'll flash that LED the - // appropriate number of times, with a pause between each one - for (uint i = 0; i < 10; i++) { - led_state(1, 0); - for (uint j = 0; j < reset_mode; j++) { - if (!switch_get()) { - break; - } - led_state(1, 1); - mp_hal_delay_ms(100); - led_state(1, 0); - mp_hal_delay_ms(200); - } - mp_hal_delay_ms(400); - if (!switch_get()) { - break; - } - if (++reset_mode > 3) { - reset_mode = 1; - } - } - // Flash the selected reset mode - for (uint i = 0; i < 2; i++) { - for (uint j = 0; j < reset_mode; j++) { - led_state(1, 1); - mp_hal_delay_ms(100); - led_state(1, 0); - mp_hal_delay_ms(200); - } - mp_hal_delay_ms(400); - } - #else - #error Need a reset mode update method - #endif - } - #endif - return reset_mode; -} -#endif - void stm32_main(uint32_t reset_mode) { #if !defined(STM32F0) && defined(MICROPY_HW_VTOR) // Change IRQ vector table if configured differently @@ -552,22 +464,17 @@ void stm32_main(uint32_t reset_mode) { MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL - 1] = &pyb_uart_repl_obj; #endif - #if !MICROPY_HW_USES_BOOTLOADER - // check if user switch held to select the reset mode - reset_mode = update_reset_mode(1); - #endif + boardctrl_state_t state; + state.reset_mode = reset_mode; + state.run_boot_py = false; + state.run_main_py = false; + state.last_ret = 0; + + MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state); soft_reset: - #if defined(MICROPY_HW_LED2) - led_state(1, 0); - led_state(2, 1); - #else - led_state(1, 1); - led_state(2, 0); - #endif - led_state(3, 0); - led_state(4, 0); + MICROPY_BOARD_TOP_SOFT_RESET_LOOP(&state); // Python threading init #if MICROPY_PY_THREAD @@ -656,29 +563,19 @@ void stm32_main(uint32_t reset_mode) { // reset config variables; they should be set by boot.py MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL; + MICROPY_BOARD_BEFORE_BOOT_PY(&state); + // run boot.py, if it exists // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py - if (reset_mode == 1 || reset_mode == 3) { + if (state.run_boot_py) { const char *boot_py = "boot.py"; - int ret = pyexec_file_if_exists(boot_py); - if (ret & PYEXEC_FORCED_EXIT) { + state.last_ret = pyexec_file_if_exists(boot_py); + if (state.last_ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } - if (!ret) { - flash_error(4); - } } - // turn boot-up LEDs off - #if !defined(MICROPY_HW_LED2) - // If there is only one LED on the board then it's used to signal boot-up - // and so we turn it off here. Otherwise LED(1) is used to indicate dirty - // flash cache and so we shouldn't change its state. - led_state(1, 0); - #endif - led_state(2, 0); - led_state(3, 0); - led_state(4, 0); + MICROPY_BOARD_AFTER_BOOT_PY(&state); // Now we initialise sub-systems that need configuration from boot.py, // or whose initialisation can be safely deferred until after running @@ -713,23 +610,24 @@ void stm32_main(uint32_t reset_mode) { // At this point everything is fully configured and initialised. + MICROPY_BOARD_BEFORE_MAIN_PY(&state); + // Run the main script from the current directory. - if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { + if (state.run_main_py) { const char *main_py; if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) { main_py = "main.py"; } else { main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); } - int ret = pyexec_file_if_exists(main_py); - if (ret & PYEXEC_FORCED_EXIT) { + state.last_ret = pyexec_file_if_exists(main_py); + if (state.last_ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } - if (!ret) { - flash_error(3); - } } + MICROPY_BOARD_AFTER_MAIN_PY(&state); + #if MICROPY_ENABLE_COMPILER // Main script is finished, so now go into REPL mode. // The REPL mode can change, or it can request a soft reset. @@ -750,12 +648,19 @@ void stm32_main(uint32_t reset_mode) { // soft reset + MICROPY_BOARD_START_SOFT_RESET(&state); + #if MICROPY_HW_ENABLE_STORAGE - printf("MPY: sync filesystems\n"); + if (state.log_soft_reset) { + mp_printf(&mp_plat_print, "MPY: sync filesystems\n"); + } storage_flush(); #endif - printf("MPY: soft reboot\n"); + if (state.log_soft_reset) { + mp_printf(&mp_plat_print, "MPY: soft reboot\n"); + } + #if MICROPY_PY_BLUETOOTH mp_bluetooth_deinit(); #endif @@ -774,10 +679,9 @@ void stm32_main(uint32_t reset_mode) { pyb_thread_deinit(); #endif - gc_sweep_all(); + MICROPY_BOARD_END_SOFT_RESET(&state); - // Set reset_mode to normal boot. - reset_mode = 1; + gc_sweep_all(); goto soft_reset; } From b99300b53ecc8db1ced8d96953ba4eaf2026f069 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Nov 2020 18:14:50 +1100 Subject: [PATCH 0390/3533] stm32/boardctrl: Define MICROPY_BOARD_EARLY_INIT alongside others. Signed-off-by: Damien George --- ports/stm32/boardctrl.h | 4 ++++ ports/stm32/main.c | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index 05bade305c254..6f79dfb890707 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -28,6 +28,10 @@ #include "py/mpconfig.h" +#ifndef MICROPY_BOARD_EARLY_INIT +#define MICROPY_BOARD_EARLY_INIT() +#endif + #ifndef MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP #define MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP boardctrl_before_soft_reset_loop #endif diff --git a/ports/stm32/main.c b/ports/stm32/main.c index c749cac978ba8..db8222479b0f6 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -385,10 +385,7 @@ void stm32_main(uint32_t reset_mode) { __HAL_RCC_D2SRAM3_CLK_ENABLE(); #endif - - #if defined(MICROPY_BOARD_EARLY_INIT) MICROPY_BOARD_EARLY_INIT(); - #endif // basic sub-system init #if defined(STM32WB) From 7789cd5f16b068f032e58be8e5e60bf17cb03d40 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 31 Oct 2020 00:22:11 +1100 Subject: [PATCH 0391/3533] lib/utils/pyexec: Add MICROPY_BOARD hooks before/after executing code. Signed-off-by: Damien George --- lib/utils/pyexec.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 2c8ca2de0cecc..2b86af3bbaadd 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -68,10 +68,15 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input uint32_t start = 0; #endif + #ifdef MICROPY_BOARD_BEFORE_PYTHON_EXEC + MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags); + #endif + // by default a SystemExit exception returns 0 pyexec_system_exit = 0; nlr_buf_t nlr; + nlr.ret_val = NULL; if (nlr_push(&nlr) == 0) { mp_obj_t module_fun; #if MICROPY_MODULE_FROZEN_MPY @@ -157,6 +162,10 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input mp_hal_stdout_tx_strn("\x04", 1); } + #ifdef MICROPY_BOARD_AFTER_PYTHON_EXEC + MICROPY_BOARD_AFTER_PYTHON_EXEC(input_kind, exec_flags, nlr.ret_val, &ret); + #endif + return ret; } From 1fef5662ab96a27c2b279082607103bbe5da9de5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Nov 2020 22:35:02 +1100 Subject: [PATCH 0392/3533] py/mpz: Do sign extension in mpz_as_bytes for negative values. Signed-off-by: Damien George --- py/mpz.c | 10 +++++++++- py/objint_mpz.c | 1 - tests/basics/struct1_intbig.py | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/py/mpz.c b/py/mpz.c index b3d88067929fd..75159305382a1 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -1609,7 +1609,6 @@ bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) { return true; } -// writes at most len bytes to buf (so buf should be zeroed before calling) void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) { byte *b = buf; if (big_endian) { @@ -1641,6 +1640,15 @@ void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) { } } } + + // fill remainder of buf with zero/sign extension of the integer + if (big_endian) { + len = b - buf; + } else { + len = buf + len - b; + buf = b; + } + memset(buf, z->neg ? 0xff : 0x00, len); } #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 6e52073a6e85b..ef3e017967857 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -116,7 +116,6 @@ mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) { assert(mp_obj_is_type(self_in, &mp_type_int)); mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); - memset(buf, 0, len); mpz_as_bytes(&self->mpz, big_endian, len, buf); } diff --git a/tests/basics/struct1_intbig.py b/tests/basics/struct1_intbig.py index 380293f36cea7..24541c8a42509 100644 --- a/tests/basics/struct1_intbig.py +++ b/tests/basics/struct1_intbig.py @@ -36,3 +36,11 @@ # check small int overflow print(struct.unpack("": + for type_ in "bhiq": + fmt = endian + type_ + b = struct.pack(fmt, -2 + bigzero) + print(fmt, b, struct.unpack(fmt, b)) From bdfb584b294bf1379921b08ec020386b8ff6257b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Nov 2020 22:39:54 +1100 Subject: [PATCH 0393/3533] extmod/moductypes: Fix storing to (U)INT64 arrays on 32-bit archs. Fixes issue #6583. Signed-off-by: Damien George --- extmod/moductypes.c | 2 +- tests/extmod/uctypes_array_load_store.py | 19 ++++++++++++++++ tests/extmod/uctypes_array_load_store.py.exp | 24 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/extmod/uctypes_array_load_store.py create mode 100644 tests/extmod/uctypes_array_load_store.py.exp diff --git a/extmod/moductypes.c b/extmod/moductypes.c index c5fbf12e42b9d..79a49d5c329c2 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -399,7 +399,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) { ((uint64_t *)p)[index] = (uint64_t)v; } else { // TODO: Doesn't offer atomic store semantics, but should at least try - set_unaligned(val_type, p, MP_ENDIANNESS_BIG, val); + set_unaligned(val_type, (void *)&((uint64_t *)p)[index], MP_ENDIANNESS_BIG, val); } return; default: diff --git a/tests/extmod/uctypes_array_load_store.py b/tests/extmod/uctypes_array_load_store.py new file mode 100644 index 0000000000000..709b9f5c29b18 --- /dev/null +++ b/tests/extmod/uctypes_array_load_store.py @@ -0,0 +1,19 @@ +# Test uctypes array, load and store, with array size > 1 + +try: + import uctypes +except ImportError: + print("SKIP") + raise SystemExit + +N = 3 + +for endian in ("NATIVE", "LITTLE_ENDIAN", "BIG_ENDIAN"): + for type_ in ("INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32", "INT64", "UINT64"): + desc = {"arr": (uctypes.ARRAY | 0, getattr(uctypes, type_) | N)} + sz = uctypes.sizeof(desc) + data = bytearray(sz) + s = uctypes.struct(uctypes.addressof(data), desc, getattr(uctypes, endian)) + for i in range(N): + s.arr[i] = i + print(endian, type_, sz, *(s.arr[i] for i in range(N))) diff --git a/tests/extmod/uctypes_array_load_store.py.exp b/tests/extmod/uctypes_array_load_store.py.exp new file mode 100644 index 0000000000000..f6f7bc96ef1a0 --- /dev/null +++ b/tests/extmod/uctypes_array_load_store.py.exp @@ -0,0 +1,24 @@ +NATIVE INT8 3 0 1 2 +NATIVE UINT8 3 0 1 2 +NATIVE INT16 6 0 1 2 +NATIVE UINT16 6 0 1 2 +NATIVE INT32 12 0 1 2 +NATIVE UINT32 12 0 1 2 +NATIVE INT64 24 0 1 2 +NATIVE UINT64 24 0 1 2 +LITTLE_ENDIAN INT8 3 0 1 2 +LITTLE_ENDIAN UINT8 3 0 1 2 +LITTLE_ENDIAN INT16 6 0 1 2 +LITTLE_ENDIAN UINT16 6 0 1 2 +LITTLE_ENDIAN INT32 12 0 1 2 +LITTLE_ENDIAN UINT32 12 0 1 2 +LITTLE_ENDIAN INT64 24 0 1 2 +LITTLE_ENDIAN UINT64 24 0 1 2 +BIG_ENDIAN INT8 3 0 1 2 +BIG_ENDIAN UINT8 3 0 1 2 +BIG_ENDIAN INT16 6 0 1 2 +BIG_ENDIAN UINT16 6 0 1 2 +BIG_ENDIAN INT32 12 0 1 2 +BIG_ENDIAN UINT32 12 0 1 2 +BIG_ENDIAN INT64 24 0 1 2 +BIG_ENDIAN UINT64 24 0 1 2 From d7e1526593151b33ab52af445647c6d1315a96dc Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Nov 2020 22:42:28 +1100 Subject: [PATCH 0394/3533] py/binary: Fix sign extension setting wide integer on 32-bit archs. Signed-off-by: Damien George --- py/binary.c | 2 +- tests/extmod/uctypes_array_load_store.py | 4 +- tests/extmod/uctypes_array_load_store.py.exp | 48 ++++++++++---------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/py/binary.c b/py/binary.c index d0f72ec23c49c..1847894b71068 100644 --- a/py/binary.c +++ b/py/binary.c @@ -343,7 +343,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p val = mp_obj_get_int(val_in); // zero/sign extend if needed if (BYTES_PER_WORD < 8 && size > sizeof(val)) { - int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00; + int c = (mp_int_t)val < 0 ? 0xff : 0x00; memset(p, c, size); if (struct_type == '>') { p += size - sizeof(val); diff --git a/tests/extmod/uctypes_array_load_store.py b/tests/extmod/uctypes_array_load_store.py index 709b9f5c29b18..3b9bb6d7308ca 100644 --- a/tests/extmod/uctypes_array_load_store.py +++ b/tests/extmod/uctypes_array_load_store.py @@ -6,7 +6,7 @@ print("SKIP") raise SystemExit -N = 3 +N = 5 for endian in ("NATIVE", "LITTLE_ENDIAN", "BIG_ENDIAN"): for type_ in ("INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32", "INT64", "UINT64"): @@ -15,5 +15,5 @@ data = bytearray(sz) s = uctypes.struct(uctypes.addressof(data), desc, getattr(uctypes, endian)) for i in range(N): - s.arr[i] = i + s.arr[i] = i - 2 print(endian, type_, sz, *(s.arr[i] for i in range(N))) diff --git a/tests/extmod/uctypes_array_load_store.py.exp b/tests/extmod/uctypes_array_load_store.py.exp index f6f7bc96ef1a0..10de8046454ba 100644 --- a/tests/extmod/uctypes_array_load_store.py.exp +++ b/tests/extmod/uctypes_array_load_store.py.exp @@ -1,24 +1,24 @@ -NATIVE INT8 3 0 1 2 -NATIVE UINT8 3 0 1 2 -NATIVE INT16 6 0 1 2 -NATIVE UINT16 6 0 1 2 -NATIVE INT32 12 0 1 2 -NATIVE UINT32 12 0 1 2 -NATIVE INT64 24 0 1 2 -NATIVE UINT64 24 0 1 2 -LITTLE_ENDIAN INT8 3 0 1 2 -LITTLE_ENDIAN UINT8 3 0 1 2 -LITTLE_ENDIAN INT16 6 0 1 2 -LITTLE_ENDIAN UINT16 6 0 1 2 -LITTLE_ENDIAN INT32 12 0 1 2 -LITTLE_ENDIAN UINT32 12 0 1 2 -LITTLE_ENDIAN INT64 24 0 1 2 -LITTLE_ENDIAN UINT64 24 0 1 2 -BIG_ENDIAN INT8 3 0 1 2 -BIG_ENDIAN UINT8 3 0 1 2 -BIG_ENDIAN INT16 6 0 1 2 -BIG_ENDIAN UINT16 6 0 1 2 -BIG_ENDIAN INT32 12 0 1 2 -BIG_ENDIAN UINT32 12 0 1 2 -BIG_ENDIAN INT64 24 0 1 2 -BIG_ENDIAN UINT64 24 0 1 2 +NATIVE INT8 5 -2 -1 0 1 2 +NATIVE UINT8 5 254 255 0 1 2 +NATIVE INT16 10 -2 -1 0 1 2 +NATIVE UINT16 10 65534 65535 0 1 2 +NATIVE INT32 20 -2 -1 0 1 2 +NATIVE UINT32 20 4294967294 4294967295 0 1 2 +NATIVE INT64 40 -2 -1 0 1 2 +NATIVE UINT64 40 18446744073709551614 18446744073709551615 0 1 2 +LITTLE_ENDIAN INT8 5 -2 -1 0 1 2 +LITTLE_ENDIAN UINT8 5 254 255 0 1 2 +LITTLE_ENDIAN INT16 10 -2 -1 0 1 2 +LITTLE_ENDIAN UINT16 10 65534 65535 0 1 2 +LITTLE_ENDIAN INT32 20 -2 -1 0 1 2 +LITTLE_ENDIAN UINT32 20 4294967294 4294967295 0 1 2 +LITTLE_ENDIAN INT64 40 -2 -1 0 1 2 +LITTLE_ENDIAN UINT64 40 18446744073709551614 18446744073709551615 0 1 2 +BIG_ENDIAN INT8 5 -2 -1 0 1 2 +BIG_ENDIAN UINT8 5 254 255 0 1 2 +BIG_ENDIAN INT16 10 -2 -1 0 1 2 +BIG_ENDIAN UINT16 10 65534 65535 0 1 2 +BIG_ENDIAN INT32 20 -2 -1 0 1 2 +BIG_ENDIAN UINT32 20 4294967294 4294967295 0 1 2 +BIG_ENDIAN INT64 40 -2 -1 0 1 2 +BIG_ENDIAN UINT64 40 18446744073709551614 18446744073709551615 0 1 2 From a7932ae4e66bd1c354eee357f18393bd83144144 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Oct 2020 16:38:13 +1100 Subject: [PATCH 0395/3533] tools/makeqstrdefs.py: Run qstr preprocessing in parallel. This gives a substantial speedup of the preprocessing step, i.e. the generation of qstr.i.last. For example on a clean build, making qstr.i.last: 21s -> 4s on STM32 (WB55) 8.9 -> 1.8s on Unix (dev). Done in collaboration with @stinos. Signed-off-by: Jim Mussared --- py/makeqstrdefs.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/py/makeqstrdefs.py b/py/makeqstrdefs.py index f514ae0c1052f..ad4f22d5e4cf3 100644 --- a/py/makeqstrdefs.py +++ b/py/makeqstrdefs.py @@ -7,11 +7,12 @@ from __future__ import print_function +import io +import os import re import subprocess import sys -import io -import os +import multiprocessing, multiprocessing.dummy # Extract MP_QSTR_FOO macros. @@ -39,11 +40,27 @@ def preprocess(): os.makedirs(os.path.dirname(args.output[0])) except OSError: pass - with open(args.output[0], "w") as out_file: - if csources: - subprocess.check_call(args.pp + args.cflags + csources, stdout=out_file) - if cxxsources: - subprocess.check_call(args.pp + args.cxxflags + cxxsources, stdout=out_file) + + def pp(flags): + def run(files): + return subprocess.check_output(args.pp + flags + files) + + return run + + try: + cpus = multiprocessing.cpu_count() + except NotImplementedError: + cpus = 1 + p = multiprocessing.dummy.Pool(cpus) + with open(args.output[0], "wb") as out_file: + for flags, sources in ( + (args.cflags, csources), + (args.cxxflags, cxxsources), + ): + batch_size = (len(sources) + cpus - 1) // cpus + chunks = [sources[i : i + batch_size] for i in range(0, len(sources), batch_size or 1)] + for output in p.imap(pp(flags), chunks): + out_file.write(output) def write_out(fname, output): From b04240cb77c5305a63b4ca7b36229d410eca2072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Wed, 4 Nov 2020 14:26:56 +0200 Subject: [PATCH 0396/3533] stm32/Makefile: Make the generation of `firmware.bin` explicit. The file `$(BUILD)/firmware.bin` was used by the target `deploy-stlink` and `deploy-openocd` but it was generated indirectly by the target `firmware.dfu`. As this file could be used to program boards directly by a Mass Storage copy, it's better to make it explicitly generated. Additionally, some target are refactored to remove redundancy and be more explicit on dependencies. --- ports/stm32/Makefile | 112 +++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index fa9998a8e078b..8f7cdd93ca5fe 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -564,15 +564,56 @@ CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool CFLAGS += -DMICROPY_MODULE_FROZEN_MPY endif -.PHONY: deploy +define RUN_DFU + $(ECHO) "Writing $(1) to the board" + $(if $(filter $(USE_PYDFU),1),\ + $(Q)$(PYTHON) $(PYDFU) --vid $(BOOTLOADER_DFU_USB_VID) --pid $(BOOTLOADER_DFU_USB_PID) -u $(1), + $(Q)$(DFU_UTIL) -a 0 -d $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) -D $(1)) +endef + +define RUN_STLINK + $(ECHO) "Writing $(1) to the board via ST-LINK" + $(Q)$(STFLASH) write $(1) $(2) +endef + +define RUN_OPENOCD + $(ECHO) "Writing $(1) to the board via ST-LINK using OpenOCD" + $(Q)$(OPENOCD) -f $(OPENOCD_CONFIG) -c "stm_flash $(1) $(2) $(3) $(4)" +endef + +define GENERATE_ELF + $(ECHO) "LINK $(1)" + $(Q)$(LD) $(LDFLAGS) -o $(1) $(2) $(LDFLAGS_MOD) $(LIBS) + $(Q)$(SIZE) $(1) + $(if $(filter-out $(TEXT0_ADDR),0x08000000), \ + $(ECHO) "INFO: this build requires mboot to be installed first") + $(if $(filter $(TEXT1_ADDR),0x90000000), \ + $(ECHO) "INFO: this build places firmware in external QSPI flash") +endef + +define GENERATE_BIN + $(ECHO) "GEN $(1)" + $(Q)$(OBJCOPY) -O binary $(addprefix -j ,$(3)) $(2) $(1) +endef + +define GENERATE_DFU + $(ECHO) "GEN $(1)" + $(Q)$(PYTHON) $(DFU) \ + -D $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) \ + $(if $(2),$(addprefix -b ,$(3):$(2))) \ + $(if $(4),$(addprefix -b ,$(5):$(4))) \ + $(1) +endef + +define GENERATE_HEX + $(ECHO) "GEN $(1)" + $(Q)$(OBJCOPY) -O ihex $(2) $(1) +endef + +.PHONY: deploy deploy-stlink deploy-openocd deploy: $(BUILD)/firmware.dfu - $(ECHO) "Writing $< to the board" -ifeq ($(USE_PYDFU),1) - $(Q)$(PYTHON) $(PYDFU) --vid $(BOOTLOADER_DFU_USB_VID) --pid $(BOOTLOADER_DFU_USB_PID) -u $< -else - $(Q)$(DFU_UTIL) -a 0 -d $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) -D $< -endif + $(call RUN_DFU,$^) # A board should specify TEXT0_ADDR if to use a different location than the # default for the firmware memory location. A board can also optionally define @@ -584,18 +625,17 @@ ifeq ($(TEXT1_ADDR),) TEXT0_SECTIONS ?= .isr_vector .text .data -deploy-stlink: $(BUILD)/firmware.dfu - $(ECHO) "Writing $(BUILD)/firmware.bin to the board via ST-LINK" - $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(TEXT0_ADDR) +deploy-stlink: $(BUILD)/firmware.bin + $(call RUN_STLINK,$^,$(TEXT0_ADDR)) -deploy-openocd: $(BUILD)/firmware.dfu - $(ECHO) "Writing $(BUILD)/firmware.bin to the board via ST-LINK using OpenOCD" - $(Q)$(OPENOCD) -f $(OPENOCD_CONFIG) -c "stm_flash $(BUILD)/firmware.bin $(TEXT0_ADDR)" +deploy-openocd: $(BUILD)/firmware.bin + $(call RUN_OPENOCD,$^,$(TEXT0_ADDR)) -$(BUILD)/firmware.dfu: $(BUILD)/firmware.elf - $(ECHO) "Create $@" - $(Q)$(OBJCOPY) -O binary $(addprefix -j ,$(TEXT0_SECTIONS)) $^ $(BUILD)/firmware.bin - $(Q)$(PYTHON) $(DFU) -D $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) -b $(TEXT0_ADDR):$(BUILD)/firmware.bin $@ +$(BUILD)/firmware.bin: $(BUILD)/firmware.elf + $(call GENERATE_BIN,$@,$^,$(TEXT0_SECTIONS)) + +$(BUILD)/firmware.dfu: $(BUILD)/firmware.bin + $(call GENERATE_DFU,$@,$^,$(TEXT0_ADDR)) else # TEXT0_ADDR and TEXT1_ADDR are specified so split firmware between these locations @@ -603,38 +643,28 @@ else TEXT0_SECTIONS ?= .isr_vector TEXT1_SECTIONS ?= .text .data -deploy-stlink: $(BUILD)/firmware.dfu - $(ECHO) "Writing $(BUILD)/firmware0.bin to the board via ST-LINK" - $(Q)$(STFLASH) write $(BUILD)/firmware0.bin $(TEXT0_ADDR) - $(ECHO) "Writing $(BUILD)/firmware1.bin to the board via ST-LINK" - $(Q)$(STFLASH) --reset write $(BUILD)/firmware1.bin $(TEXT1_ADDR) +deploy-stlink: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin + $(call RUN_STLINK,$(word 1,$^),$(TEXT0_ADDR)) + $(call RUN_STLINK,$(word 2,$^),$(TEXT1_ADDR)) -deploy-openocd: $(BUILD)/firmware.dfu - $(ECHO) "Writing $(BUILD)/firmware{0,1}.bin to the board via ST-LINK using OpenOCD" - $(Q)$(OPENOCD) -f $(OPENOCD_CONFIG) -c "stm_flash $(BUILD)/firmware0.bin $(TEXT0_ADDR) $(BUILD)/firmware1.bin $(TEXT1_ADDR)" +deploy-openocd: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin + $(call RUN_OPENOCD,$(word 1,$^),$(TEXT0_ADDR),$(word 2,$^),$(TEXT1_ADDR)) -$(BUILD)/firmware.dfu: $(BUILD)/firmware.elf - $(ECHO) "GEN $@" - $(Q)$(OBJCOPY) -O binary $(addprefix -j ,$(TEXT0_SECTIONS)) $^ $(BUILD)/firmware0.bin - $(Q)$(OBJCOPY) -O binary $(addprefix -j ,$(TEXT1_SECTIONS)) $^ $(BUILD)/firmware1.bin - $(Q)$(PYTHON) $(DFU) -D $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) -b $(TEXT0_ADDR):$(BUILD)/firmware0.bin -b $(TEXT1_ADDR):$(BUILD)/firmware1.bin $@ +$(BUILD)/firmware0.bin: $(BUILD)/firmware.elf + $(call GENERATE_BIN,$@,$^,$(TEXT0_SECTIONS)) +$(BUILD)/firmware1.bin: $(BUILD)/firmware.elf + $(call GENERATE_BIN,$@,$^,$(TEXT1_SECTIONS)) + +$(BUILD)/firmware.dfu: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin + $(call GENERATE_DFU,$@,$(word 1,$^),$(TEXT0_ADDR),$(word 2,$^),$(TEXT1_ADDR)) endif $(BUILD)/firmware.hex: $(BUILD)/firmware.elf - $(ECHO) "GEN $@" - $(Q)$(OBJCOPY) -O ihex $< $@ + $(call GENERATE_HEX,$@,$^) $(BUILD)/firmware.elf: $(OBJ) - $(ECHO) "LINK $@" - $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LDFLAGS_MOD) $(LIBS) - $(Q)$(SIZE) $@ -ifneq ($(TEXT0_ADDR),0x08000000) - $(ECHO) "INFO: this build requires mboot to be installed first" -endif -ifeq ($(TEXT1_ADDR),0x90000000) - $(ECHO) "INFO: this build places firmware in external QSPI flash" -endif + $(call GENERATE_ELF,$@,$^) PLLVALUES = boards/pllvalues.py MAKE_PINS = boards/make-pins.py From 8a917ad2529ea3df5f47e2be5b4edf362d2d03f6 Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Mon, 9 Nov 2020 13:11:52 +0000 Subject: [PATCH 0397/3533] esp32/machine_pin: Reset pin if init sets mode. This will forcibly grab the pin back from the ADC if it has previously been associated with it. Fixes #5771. --- ports/esp32/machine_pin.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index a55cc1f4abaca..dcdd53be92378 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -150,6 +150,11 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + // reset the pin first if this is a mode-setting init (grab it back from ADC) + if (args[ARG_mode].u_obj != mp_const_none) { + gpio_reset_pin(self->id); + } + // configure the pin for gpio gpio_pad_select_gpio(self->id); From 922f81dfd1b94e23f23f05aac0ecafac8645ce46 Mon Sep 17 00:00:00 2001 From: Arrowana Date: Sat, 31 Oct 2020 13:34:58 +1100 Subject: [PATCH 0398/3533] extmod/machine_mem: Only allow integers in machine.memX subscript. Prior to this change machine.mem32['foo'] (or using any other non-integer subscript) could result in a fault due to 'foo' being interpreted as an integer. And when writing code it's hard to tell if the fault is due to a bad subscript type, or an integer subscript that specifies an invalid memory address. The type of the object used in the subscript is now tested to be an integer by using mp_obj_get_int_truncated instead of mp_obj_int_get_truncated. The performance hit of this change is minimal, and machine.memX objects are more for convenience than performance (there are many other ways to read/write memory in a faster way), Fixes issue #6588. --- extmod/machine_mem.c | 2 +- ports/unix/modmachine.c | 2 +- tests/extmod/machine1.py | 20 ++++++++++++++++++++ tests/extmod/machine1.py.exp | 4 ++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/extmod/machine_mem.c b/extmod/machine_mem.c index a1494bd51ad17..73e2f7fd1fcb0 100644 --- a/extmod/machine_mem.c +++ b/extmod/machine_mem.c @@ -40,7 +40,7 @@ #if !defined(MICROPY_MACHINE_MEM_GET_READ_ADDR) || !defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR) STATIC uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) { - uintptr_t addr = mp_obj_int_get_truncated(addr_o); + uintptr_t addr = mp_obj_get_int_truncated(addr_o); if ((addr & (align - 1)) != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align); } diff --git a/ports/unix/modmachine.c b/ports/unix/modmachine.c index a6eb8c01edd05..5b462a3b1534b 100644 --- a/ports/unix/modmachine.c +++ b/ports/unix/modmachine.c @@ -47,7 +47,7 @@ #if MICROPY_PY_MACHINE uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) { - uintptr_t addr = mp_obj_int_get_truncated(addr_o); + uintptr_t addr = mp_obj_get_int_truncated(addr_o); if ((addr & (align - 1)) != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align); } diff --git a/tests/extmod/machine1.py b/tests/extmod/machine1.py index 6ff38cc05179d..0c7f8122f4ae4 100644 --- a/tests/extmod/machine1.py +++ b/tests/extmod/machine1.py @@ -26,3 +26,23 @@ del machine.mem8[0] except TypeError: print("TypeError") + +try: + machine.mem8[0:1] +except TypeError: + print("TypeError") + +try: + machine.mem8[0:1] = 10 +except TypeError: + print("TypeError") + +try: + machine.mem8["hello"] +except TypeError: + print("TypeError") + +try: + machine.mem8["hello"] = 10 +except TypeError: + print("TypeError") diff --git a/tests/extmod/machine1.py.exp b/tests/extmod/machine1.py.exp index bb421ea5cfa42..250485969086e 100644 --- a/tests/extmod/machine1.py.exp +++ b/tests/extmod/machine1.py.exp @@ -2,3 +2,7 @@ ValueError ValueError TypeError +TypeError +TypeError +TypeError +TypeError From a0623a081c0dde50aa8d87b464232f2d7866f951 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Nov 2020 12:04:56 +1100 Subject: [PATCH 0399/3533] stm32/Makefile: Allow boards to extend all SRC variables. And rename SRC_HAL -> HAL_SRC_C and SRC_USBDEV -> USBDEV_SRC_C for consistency with other source variables. Follow on from 0fff2e03fe07471997a6df6f92c6960cfd225dc0 Signed-off-by: Damien George --- ports/stm32/Makefile | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 8f7cdd93ca5fe..18f7e8a1a10d8 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -148,7 +148,7 @@ endif # Options for mpy-cross MPY_CROSS_FLAGS += -march=armv7m -LIB_SRC_C = $(addprefix lib/,\ +LIB_SRC_C += $(addprefix lib/,\ libc/string0.c \ mp-readline/readline.c \ netutils/netutils.c \ @@ -163,7 +163,7 @@ LIB_SRC_C = $(addprefix lib/,\ ) ifeq ($(MICROPY_FLOAT_IMPL),double) -LIBM_SRC_C = $(addprefix lib/libm_dbl/,\ +LIBM_SRC_C += $(addprefix lib/libm_dbl/,\ __cos.c \ __expo2.c \ __fpclassify.c \ @@ -213,7 +213,7 @@ else LIBM_SRC_C += lib/libm_dbl/sqrt.c endif else -LIBM_SRC_C = $(addprefix lib/libm/,\ +LIBM_SRC_C += $(addprefix lib/libm/,\ math.c \ acoshf.c \ asinfacosf.c \ @@ -255,11 +255,11 @@ ifeq ($(MICROPY_FLOAT_IMPL),double) $(LIBM_O): CFLAGS := $(filter-out -Wdouble-promotion -Wfloat-conversion, $(CFLAGS)) endif -EXTMOD_SRC_C = $(addprefix extmod/,\ +EXTMOD_SRC_C += $(addprefix extmod/,\ modonewire.c \ ) -DRIVERS_SRC_C = $(addprefix drivers/,\ +DRIVERS_SRC_C += $(addprefix drivers/,\ bus/softspi.c \ bus/softqspi.c \ memory/spiflash.c \ @@ -363,7 +363,7 @@ SRC_O += \ endif endif -SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ +HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ hal.c \ hal_adc.c \ hal_adc_ex.c \ @@ -388,13 +388,13 @@ SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ ) ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 h7 l0 l4 wb)) -SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ +HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ ll_usb.c \ ) endif ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 h7 l4)) -SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ +HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ hal_sd.c \ ll_sdmmc.c \ ll_fmc.c \ @@ -402,7 +402,7 @@ SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ endif ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 h7)) -SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ +HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ hal_mmc.c \ hal_sdram.c \ hal_dma_ex.c \ @@ -411,14 +411,14 @@ SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ endif ifeq ($(CMSIS_MCU),$(filter $(CMSIS_MCU),STM32H743xx)) - SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_, hal_fdcan.c) + HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_, hal_fdcan.c) else ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 f4 f7 h7 l4)) - SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_, hal_can.c) + HAL_SRC_C += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_, hal_can.c) endif endif -SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\ +USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\ core/src/usbd_core.c \ core/src/usbd_ctlreq.c \ core/src/usbd_ioreq.c \ @@ -521,11 +521,11 @@ OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) -OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) OBJ += $(BUILD)/pins_$(BOARD).o From cc2a35b7b241e7eda031db424bf9b3afb8b6204b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Nov 2020 14:35:43 +1100 Subject: [PATCH 0400/3533] stm32/rtc: Validate the RTC prescaler on boot and change if incorrect. Devices with RTC backup-batteries have been shown (very rarely) to have incorrect RTC prescaler values. Such incorrect values mean the RTC counts fast or slow, and will be wrong forever if the power/backup-battery is always present. This commit detects such a state at start up (hard reset) and corrects it by reconfiguring the RTC prescaler values. Signed-off-by: Damien George --- ports/stm32/boards/stm32f0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32h7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32l0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32l4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32wbxx_hal_conf_base.h | 1 + ports/stm32/rtc.c | 40 ++++++++++++++++++-- 8 files changed, 44 insertions(+), 3 deletions(-) diff --git a/ports/stm32/boards/stm32f0xx_hal_conf_base.h b/ports/stm32/boards/stm32f0xx_hal_conf_base.h index faceda2f4fa90..5c6f31d1dd81b 100644 --- a/ports/stm32/boards/stm32f0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f0xx_hal_conf_base.h @@ -48,6 +48,7 @@ #include "stm32f0xx_hal_usart.h" #include "stm32f0xx_hal_wwdg.h" #include "stm32f0xx_ll_adc.h" +#include "stm32f0xx_ll_rtc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32f4xx_hal_conf_base.h b/ports/stm32/boards/stm32f4xx_hal_conf_base.h index cdae0c5629e20..8d8bb8f4eeccc 100644 --- a/ports/stm32/boards/stm32f4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f4xx_hal_conf_base.h @@ -53,6 +53,7 @@ #include "stm32f4xx_hal_uart.h" #include "stm32f4xx_hal_usart.h" #include "stm32f4xx_hal_wwdg.h" +#include "stm32f4xx_ll_rtc.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32f7xx_hal_conf_base.h b/ports/stm32/boards/stm32f7xx_hal_conf_base.h index 05ab10fea08ad..83a144f8fe7f0 100644 --- a/ports/stm32/boards/stm32f7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f7xx_hal_conf_base.h @@ -53,6 +53,7 @@ #include "stm32f7xx_hal_uart.h" #include "stm32f7xx_hal_usart.h" #include "stm32f7xx_hal_wwdg.h" +#include "stm32f7xx_ll_rtc.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32h7xx_hal_conf_base.h b/ports/stm32/boards/stm32h7xx_hal_conf_base.h index 9f43da40308f7..231f1ac7f4d28 100644 --- a/ports/stm32/boards/stm32h7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32h7xx_hal_conf_base.h @@ -54,6 +54,7 @@ #include "stm32h7xx_hal_usart.h" #include "stm32h7xx_hal_wwdg.h" #include "stm32h7xx_ll_adc.h" +#include "stm32h7xx_ll_rtc.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32l0xx_hal_conf_base.h b/ports/stm32/boards/stm32l0xx_hal_conf_base.h index b100daaa98eb3..6b5ece766a83c 100644 --- a/ports/stm32/boards/stm32l0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l0xx_hal_conf_base.h @@ -47,6 +47,7 @@ #include "stm32l0xx_hal_usart.h" #include "stm32l0xx_hal_wwdg.h" #include "stm32l0xx_ll_adc.h" +#include "stm32l0xx_ll_rtc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32l4xx_hal_conf_base.h b/ports/stm32/boards/stm32l4xx_hal_conf_base.h index cfffcffbbe134..215e798b92f2e 100644 --- a/ports/stm32/boards/stm32l4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l4xx_hal_conf_base.h @@ -51,6 +51,7 @@ #include "stm32l4xx_hal_usart.h" #include "stm32l4xx_hal_wwdg.h" #include "stm32l4xx_ll_adc.h" +#include "stm32l4xx_ll_rtc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32wbxx_hal_conf_base.h b/ports/stm32/boards/stm32wbxx_hal_conf_base.h index 83d07ad5b1c3b..91309e286fa1e 100644 --- a/ports/stm32/boards/stm32wbxx_hal_conf_base.h +++ b/ports/stm32/boards/stm32wbxx_hal_conf_base.h @@ -42,6 +42,7 @@ #include "stm32wbxx_hal_uart.h" #include "stm32wbxx_hal_usart.h" #include "stm32wbxx_ll_adc.h" +#include "stm32wbxx_ll_rtc.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index 18ecf7750513b..bd898d4558570 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -114,20 +114,22 @@ void rtc_init_start(bool force_init) { rtc_need_init_finalise = false; if (!force_init) { + bool rtc_running = false; uint32_t bdcr = RCC->BDCR; if ((bdcr & (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL | RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_0 | RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) { // LSE is enabled & ready --> no need to (re-)init RTC + rtc_running = true; // remove Backup Domain write protection HAL_PWR_EnableBkUpAccess(); // Clear source Reset Flag __HAL_RCC_CLEAR_RESET_FLAGS(); // provide some status information - rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8; - return; + rtc_info |= 0x40000; } else if ((bdcr & (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL)) == (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_1)) { // LSI configured as the RTC clock source --> no need to (re-)init RTC + rtc_running = true; // remove Backup Domain write protection HAL_PWR_EnableBkUpAccess(); // Clear source Reset Flag @@ -135,7 +137,39 @@ void rtc_init_start(bool force_init) { // Turn the LSI on (it may need this even if the RTC is running) RCC->CSR |= RCC_CSR_LSION; // provide some status information - rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8; + rtc_info |= 0x80000; + } + + if (rtc_running) { + // Provide information about the registers that indicated the RTC is running. + rtc_info |= (RCC->BDCR & 7) | (RCC->CSR & 3) << 8; + + // Check that the sync and async prescaler values are correct. If the RTC + // gets into a state where they are wrong then it will run slow or fast and + // never be corrected. In such a situation, attempt to reconfigure the values + // without changing the data/time. + if (LL_RTC_GetSynchPrescaler(RTC) != RTC_SYNCH_PREDIV + || LL_RTC_GetAsynchPrescaler(RTC) != RTC_ASYNCH_PREDIV) { + // Values are wrong, attempt to enter RTC init mode and change them. + LL_RTC_DisableWriteProtection(RTC); + LL_RTC_EnableInitMode(RTC); + uint32_t ticks_ms = HAL_GetTick(); + while (HAL_GetTick() - ticks_ms < RTC_TIMEOUT_VALUE) { + if (LL_RTC_IsActiveFlag_INIT(RTC)) { + // Reconfigure the RTC prescaler register PRER. + LL_RTC_SetSynchPrescaler(RTC, RTC_SYNCH_PREDIV); + LL_RTC_SetAsynchPrescaler(RTC, RTC_ASYNCH_PREDIV); + LL_RTC_DisableInitMode(RTC); + break; + } + } + LL_RTC_EnableWriteProtection(RTC); + + // Provide information that the prescaler was changed. + rtc_info |= 0x100000; + } + + // The RTC is up and running, so return without any further configuration. return; } } From 309fb822e6f24de54dbd107d4573a3bedac0bf9e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 17:38:09 +1100 Subject: [PATCH 0401/3533] tests/run-multitests.py: Fix diff order, show changes relative to truth. Signed-off-by: Jim Mussared --- tests/run-multitests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index ad032df38b238..5b8e70e921a7a 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -389,7 +389,7 @@ def run_tests(test_files, instances_truth, instances_test): print("### TRUTH ###") print(output_truth, end="") print("### DIFF ###") - print_diff(output_test, output_truth) + print_diff(output_truth, output_test) if cmd_args.show_output: print() From ccfd535af48f2ffc00f7306648b60624894d3004 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 17:41:28 +1100 Subject: [PATCH 0402/3533] tests/multi_bluetooth: Improve reliability of event waiting. Use the same `wait_for_event` in all tests that doesn't hold a reference to the event data tuple and handles repeat events. Also fix a few misc reliability issues around timeouts and sequencing. Signed-off-by: Jim Mussared --- tests/multi_bluetooth/ble_characteristic.py | 91 ++++++++----------- .../multi_bluetooth/ble_characteristic.py.exp | 1 + tests/multi_bluetooth/ble_gap_advertise.py | 5 +- tests/multi_bluetooth/ble_gap_connect.py | 70 +++++--------- tests/multi_bluetooth/ble_gap_device_name.py | 53 +++++------ .../ble_gap_device_name.py.exp | 1 + .../multi_bluetooth/ble_gatt_data_transfer.py | 62 +++++-------- .../ble_gattc_discover_services.py | 37 ++++---- .../ble_gattc_discover_services.py.exp | 2 + tests/multi_bluetooth/ble_mtu.py | 54 +++++------ 10 files changed, 155 insertions(+), 221 deletions(-) diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index 026b9d551c2b9..0f22daff88f0a 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -31,28 +31,31 @@ ) SERVICES = (SERVICE,) -waiting_event = None -waiting_data = None -value_handle = 0 +waiting_events = {} def irq(event, data): - global waiting_event, waiting_data, value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") elif event == _IRQ_GATTS_WRITE: print("_IRQ_GATTS_WRITE", ble.gatts_read(data[-1])) elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: # conn_handle, def_handle, value_handle, properties, uuid = data if data[-1] == CHAR_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) - value_handle = data[2] + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") elif event == _IRQ_GATTC_READ_RESULT: print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) elif event == _IRQ_GATTC_READ_DONE: @@ -66,25 +69,19 @@ def irq(event, data): elif event == _IRQ_GATTS_INDICATE_DONE: print("_IRQ_GATTS_INDICATE_DONE", data[-1]) - if waiting_event is not None: - if (isinstance(waiting_event, int) and event == waiting_event) or ( - not isinstance(waiting_event, int) and waiting_event(event, data) - ): - waiting_event = None - waiting_data = data + if event not in waiting_events: + waiting_events[event] = None def wait_for_event(event, timeout_ms): - global waiting_event, waiting_data - waiting_event = event - waiting_data = None - t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if waiting_data: - return True + if event in waiting_events: + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. @@ -99,39 +96,35 @@ def instance0(): ble.gatts_write(char_handle, "periph0") # Wait for central to connect to us. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) - # Wait for a write to the characteristic from the central. - wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) + # A - # Wait a bit, then write the characteristic and notify it. - time.sleep_ms(1000) + # Wait for a write to the characteristic from the central, + # then reply with a notification. + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) print("gatts_write") ble.gatts_write(char_handle, "periph1") print("gatts_notify") ble.gatts_notify(conn_handle, char_handle) - # Wait for a write to the characteristic from the central. - wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) + # B - # Wait a bit, then notify a new value on the characteristic. - time.sleep_ms(1000) + # Wait for a write to the characteristic from the central, + # then reply with value-included notification. + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) print("gatts_notify") ble.gatts_notify(conn_handle, char_handle, "periph2") - # Wait for a write to the characteristic from the central. - wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) + # C - # Wait a bit, then notify a new value on the characteristic. - time.sleep_ms(1000) + # Wait for a write to the characteristic from the central, + # then reply with an indication. + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) print("gatts_write") ble.gatts_write(char_handle, "periph3") print("gatts_indicate") ble.gatts_indicate(conn_handle, char_handle) - - # Wait for the indicate ack. wait_for_event(_IRQ_GATTS_INDICATE_DONE, TIMEOUT_MS) # Wait for the central to disconnect. @@ -147,49 +140,45 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) # Discover characteristics. ble.gattc_discover_characteristics(conn_handle, 1, 65535) - wait_for_event(lambda event, data: value_handle, TIMEOUT_MS) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) # Issue read of characteristic, should get initial value. print("gattc_read") ble.gattc_read(conn_handle, value_handle) wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) - # Write to the characteristic, and ask for a response. + # Write to the characteristic, which will trigger a notification. print("gattc_write") ble.gattc_write(conn_handle, value_handle, "central0", 1) wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) - - # Wait for a notify, then read new value. + # A wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) - print("gattc_read") + print("gattc_read") # Read the new value set immediately before notification. ble.gattc_read(conn_handle, value_handle) wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) - # Write to the characteristic, and ask for a response. + # Write to the characteristic, which will trigger a value-included notification. print("gattc_write") ble.gattc_write(conn_handle, value_handle, "central1", 1) wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) - - # Wait for a notify (should have new data), then read old value (should be unchanged). + # B wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) - print("gattc_read") + print("gattc_read") # Read value should be unchanged. ble.gattc_read(conn_handle, value_handle) wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) - # Write to the characteristic, and ask for a response. + # Write to the characteristic, which will trigger an indication. print("gattc_write") ble.gattc_write(conn_handle, value_handle, "central2", 1) wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) - - # Wait for a indicate (should have new data), then read new value. + # C wait_for_event(_IRQ_GATTC_INDICATE, TIMEOUT_MS) - print("gattc_read") + print("gattc_read") # Read the new value set immediately before indication. ble.gattc_read(conn_handle, value_handle) wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) diff --git a/tests/multi_bluetooth/ble_characteristic.py.exp b/tests/multi_bluetooth/ble_characteristic.py.exp index 90dc46c06fd57..31667415eebf8 100644 --- a/tests/multi_bluetooth/ble_characteristic.py.exp +++ b/tests/multi_bluetooth/ble_characteristic.py.exp @@ -15,6 +15,7 @@ _IRQ_CENTRAL_DISCONNECT gap_connect _IRQ_PERIPHERAL_CONNECT _IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE gattc_read _IRQ_GATTC_READ_RESULT b'periph0' _IRQ_GATTC_READ_DONE 0 diff --git a/tests/multi_bluetooth/ble_gap_advertise.py b/tests/multi_bluetooth/ble_gap_advertise.py index bb6ff8e425461..bb1ef94a71e7b 100644 --- a/tests/multi_bluetooth/ble_gap_advertise.py +++ b/tests/multi_bluetooth/ble_gap_advertise.py @@ -46,7 +46,10 @@ def irq(ev, data): elif ev == _IRQ_SCAN_DONE: finished = True - ble.config(rxbuf=2000) + try: + ble.config(rxbuf=2000) + except: + pass ble.irq(irq) ble.gap_scan(2 * ADV_TIME_S * 1000, 10000, 10000) while not finished: diff --git a/tests/multi_bluetooth/ble_gap_connect.py b/tests/multi_bluetooth/ble_gap_connect.py index 8b40a29163119..2c1d2cbbc55dc 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py +++ b/tests/multi_bluetooth/ble_gap_connect.py @@ -10,101 +10,77 @@ _IRQ_PERIPHERAL_CONNECT = const(7) _IRQ_PERIPHERAL_DISCONNECT = const(8) -central_connected = False -central_disconnected = False -peripheral_connected = False -peripheral_disconnected = False -conn_handle = None +waiting_events = {} def irq(event, data): - global central_connected, central_disconnected, peripheral_connected, peripheral_disconnected, conn_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") - central_connected = True - conn_handle = data[0] + waiting_events[event] = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") - central_disconnected = True elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") - peripheral_connected = True - conn_handle = data[0] + waiting_events[event] = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") - peripheral_disconnected = True - remote_addr = data[0] + if event not in waiting_events: + waiting_events[event] = None -def wait_for(fn, timeout_ms): + +def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if fn(): - return True + if event in waiting_events: + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. def instance0(): - global central_connected, central_disconnected - multitest.globals(BDADDR=ble.config("mac")) print("gap_advertise") ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") multitest.next() try: # Wait for central to connect, then wait for it to disconnect. - if not wait_for(lambda: central_connected, TIMEOUT_MS): - return - if not wait_for(lambda: central_disconnected, TIMEOUT_MS): - return - - central_connected = False - central_disconnected = False + wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) # Start advertising again. print("gap_advertise") ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") # Wait for central to connect, then disconnect it. - if not wait_for(lambda: central_connected, TIMEOUT_MS): - return + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) print("gap_disconnect:", ble.gap_disconnect(conn_handle)) - if not wait_for(lambda: central_disconnected, TIMEOUT_MS): - return + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) # Acting in central role. def instance1(): - global peripheral_connected, peripheral_disconnected - multitest.next() try: # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for(lambda: peripheral_connected, TIMEOUT_MS): - return + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) print("gap_disconnect:", ble.gap_disconnect(conn_handle)) - if not wait_for(lambda: peripheral_disconnected, TIMEOUT_MS): - return - - peripheral_connected = False - peripheral_disconnected = False - - # Wait for peripheral to start advertising again. - time.sleep_ms(100) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) # Connect to peripheral and then let the peripheral disconnect us. + # Extra scan timeout allows for the peripheral to receive the disconnect + # event and start advertising again. print("gap_connect") - ble.gap_connect(*BDADDR) - if not wait_for(lambda: peripheral_connected, TIMEOUT_MS): - return - if not wait_for(lambda: peripheral_disconnected, TIMEOUT_MS): - return + ble.gap_connect(BDADDR[0], BDADDR[1], 5000) + wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py index fbc9d80baeb08..e7202170bd8a4 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py +++ b/tests/multi_bluetooth/ble_gap_device_name.py @@ -16,45 +16,44 @@ GAP_DEVICE_NAME_UUID = bluetooth.UUID(0x2A00) -waiting_event = None -waiting_data = None -value_handle = 0 +waiting_events = {} def irq(event, data): - global waiting_event, waiting_data, value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: if data[-1] == GAP_DEVICE_NAME_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) - value_handle = data[2] + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") elif event == _IRQ_GATTC_READ_RESULT: print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) - if waiting_event is not None: - if isinstance(waiting_event, int) and event == waiting_event: - waiting_event = None - waiting_data = data + if event not in waiting_events: + waiting_events[event] = None def wait_for_event(event, timeout_ms): - global waiting_event, waiting_data - waiting_event = event - waiting_data = None - t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if waiting_data: - return True + if event in waiting_events: + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. @@ -79,10 +78,8 @@ def instance0(): ble.gap_advertise(20_000) # Wait for central to connect, then wait for it to disconnect. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): - return - if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS): - return + wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + wait_for_event(_IRQ_CENTRAL_DISCONNECT, 4 * TIMEOUT_MS) finally: ble.active(0) @@ -91,6 +88,7 @@ def instance0(): def instance1(): multitest.next() try: + value_handle = None for iteration in range(2): # Wait for peripheral to start advertising. time.sleep_ms(500) @@ -98,17 +96,15 @@ def instance1(): # Connect to peripheral. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) if iteration == 0: + # Only do characteristic discovery on the first iteration, + # assume value_handle is unchanged on the second. print("gattc_discover_characteristics") ble.gattc_discover_characteristics(conn_handle, 1, 65535) - wait_for_event(lambda: value_handle, TIMEOUT_MS) - - # Wait for all characteristic results to come in (there should be an event for it). - time.sleep_ms(500) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) # Read the peripheral's GAP device name. print("gattc_read") @@ -117,8 +113,7 @@ def instance1(): # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) - if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): - return + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) diff --git a/tests/multi_bluetooth/ble_gap_device_name.py.exp b/tests/multi_bluetooth/ble_gap_device_name.py.exp index 0f4ee6e3dc576..a7a7aa367016a 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py.exp +++ b/tests/multi_bluetooth/ble_gap_device_name.py.exp @@ -12,6 +12,7 @@ gap_connect _IRQ_PERIPHERAL_CONNECT gattc_discover_characteristics _IRQ_GATTC_CHARACTERISTIC_RESULT UUID(0x2a00) +_IRQ_GATTC_CHARACTERISTIC_DONE gattc_read _IRQ_GATTC_READ_RESULT b'GAP_NAME0' gap_disconnect: True diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py b/tests/multi_bluetooth/ble_gatt_data_transfer.py index 944c9e2d2a55f..e8249b3da8962 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py @@ -29,35 +29,33 @@ SERVICE = (SERVICE_UUID, (CHAR_CTRL, CHAR_RX, CHAR_TX)) SERVICES = (SERVICE,) -waiting_event = None -waiting_data = None -ctrl_value_handle = 0 -rx_value_handle = 0 -tx_value_handle = 0 +waiting_events = {} def irq(event, data): - global waiting_event, waiting_data, ctrl_value_handle, rx_value_handle, tx_value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") elif event == _IRQ_GATTS_WRITE: print("_IRQ_GATTS_WRITE") + waiting_events[(event, data[1])] = None elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: if data[-1] == CHAR_CTRL_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) - ctrl_value_handle = data[2] + waiting_events[(event, CHAR_CTRL_UUID)] = data[2] elif data[-1] == CHAR_RX_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) - rx_value_handle = data[2] + waiting_events[(event, CHAR_RX_UUID)] = data[2] elif data[-1] == CHAR_TX_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) - tx_value_handle = data[2] + waiting_events[(event, CHAR_TX_UUID)] = data[2] elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: print("_IRQ_GATTC_CHARACTERISTIC_DONE") elif event == _IRQ_GATTC_READ_RESULT: @@ -68,26 +66,21 @@ def irq(event, data): print("_IRQ_GATTC_WRITE_DONE", data[-1]) elif event == _IRQ_GATTC_NOTIFY: print("_IRQ_GATTC_NOTIFY", bytes(data[-1])) + waiting_events[(event, data[1])] = None - if waiting_event is not None: - if (isinstance(waiting_event, int) and event == waiting_event) or ( - not isinstance(waiting_event, int) and waiting_event(event, data) - ): - waiting_event = None - waiting_data = data + if event not in waiting_events: + waiting_events[event] = None def wait_for_event(event, timeout_ms): - global waiting_event, waiting_data - waiting_event = event - waiting_data = None - t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if waiting_data: - return True + if event in waiting_events: + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. @@ -103,15 +96,10 @@ def instance0(): multitest.next() try: # Wait for central to connect to us. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) # Wait for the central to signal that it's done with its part of the test. - wait_for_event( - lambda event, data: event == _IRQ_GATTS_WRITE and data[1] == char_ctrl_handle, - 2 * TIMEOUT_MS, - ) + wait_for_event((_IRQ_GATTS_WRITE, char_ctrl_handle), 2 * TIMEOUT_MS) # Read all accumulated data from the central. print("gatts_read:", ble.gatts_read(char_rx_handle)) @@ -138,17 +126,14 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) # Discover characteristics. ble.gattc_discover_characteristics(conn_handle, 1, 65535) - wait_for_event( - lambda event, data: ctrl_value_handle and rx_value_handle and tx_value_handle, - TIMEOUT_MS, - ) wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + ctrl_value_handle = waiting_events[(_IRQ_GATTC_CHARACTERISTIC_RESULT, CHAR_CTRL_UUID)] + rx_value_handle = waiting_events[(_IRQ_GATTC_CHARACTERISTIC_RESULT, CHAR_RX_UUID)] + tx_value_handle = waiting_events[(_IRQ_GATTC_CHARACTERISTIC_RESULT, CHAR_TX_UUID)] # Write to the characteristic a few times, with and without response. for i in range(4): @@ -163,10 +148,7 @@ def instance1(): wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) # Wait for notification that peripheral is done with its part of the test. - wait_for_event( - lambda event, data: event == _IRQ_GATTC_NOTIFY and data[1] == ctrl_value_handle, - TIMEOUT_MS, - ) + wait_for_event((_IRQ_GATTC_NOTIFY, ctrl_value_handle), TIMEOUT_MS) # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py b/tests/multi_bluetooth/ble_gattc_discover_services.py index 57f95bf0125e7..00bb94bba0ba8 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py @@ -24,45 +24,42 @@ ) SERVICES = (SERVICE_A, SERVICE_B) -waiting_event = None -waiting_data = None +waiting_events = {} num_service_result = 0 def irq(event, data): - global waiting_event, waiting_data, num_service_result + global num_service_result if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_CENTRAL_DISCONNECT: print("_IRQ_CENTRAL_DISCONNECT") elif event == _IRQ_PERIPHERAL_CONNECT: print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] elif event == _IRQ_PERIPHERAL_DISCONNECT: print("_IRQ_PERIPHERAL_DISCONNECT") elif event == _IRQ_GATTC_SERVICE_RESULT: if data[3] == UUID_A or data[3] == UUID_B: print("_IRQ_GATTC_SERVICE_RESULT", data[3]) num_service_result += 1 + elif event == _IRQ_GATTC_SERVICE_DONE: + print("_IRQ_GATTC_SERVICE_DONE") - if waiting_event is not None: - if (isinstance(waiting_event, int) and event == waiting_event) or ( - not isinstance(waiting_event, int) and waiting_event(event, data) - ): - waiting_event = None - waiting_data = data + if event not in waiting_events: + waiting_events[event] = None def wait_for_event(event, timeout_ms): - global waiting_event, waiting_data - waiting_event = event - waiting_data = None - t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: - if waiting_data: - return True + if event in waiting_events: + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. @@ -86,13 +83,13 @@ def instance1(): # Connect to peripheral and then disconnect. print("gap_connect") ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): - return - conn_handle, _, _ = waiting_data + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) # Discover services. ble.gattc_discover_services(conn_handle) - wait_for_event(lambda event, data: num_service_result == 2, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_SERVICE_DONE, TIMEOUT_MS) + + print("discovered:", num_service_result) # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py.exp b/tests/multi_bluetooth/ble_gattc_discover_services.py.exp index 7f4d8da81ad8c..d14f802377ca1 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py.exp +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py.exp @@ -7,5 +7,7 @@ gap_connect _IRQ_PERIPHERAL_CONNECT _IRQ_GATTC_SERVICE_RESULT UUID(0x180d) _IRQ_GATTC_SERVICE_RESULT UUID('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a') +_IRQ_GATTC_SERVICE_DONE +discovered: 2 gap_disconnect: True _IRQ_PERIPHERAL_DISCONNECT diff --git a/tests/multi_bluetooth/ble_mtu.py b/tests/multi_bluetooth/ble_mtu.py index ef3199c5bbb04..73c77e2460c65 100644 --- a/tests/multi_bluetooth/ble_mtu.py +++ b/tests/multi_bluetooth/ble_mtu.py @@ -51,7 +51,6 @@ def irq(event, data): - global value_handle if event == _IRQ_CENTRAL_CONNECT: print("_IRQ_CENTRAL_CONNECT") waiting_events[event] = data[0] @@ -68,6 +67,8 @@ def irq(event, data): if data[-1] == CHAR_UUID: print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) waiting_events[event] = data[2] + else: + return elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: print("_IRQ_GATTC_CHARACTERISTIC_DONE") elif event == _IRQ_GATTC_WRITE_DONE: @@ -86,9 +87,11 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - return True + result = waiting_events[event] + del waiting_events[event] + return result machine.idle() - return False + raise ValueError("Timeout waiting for {}".format(event)) # Acting in peripheral role. @@ -101,8 +104,6 @@ def instance0(): multitest.next() try: for i in range(7): - waiting_events.clear() - if i == 1: ble.config(mtu=200) elif i == 2: @@ -116,31 +117,26 @@ def instance0(): ble.config(mtu=256) # Wait for central to connect to us. - if not wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS): - return - conn_handle = waiting_events[_IRQ_CENTRAL_CONNECT] + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) if i >= 4: print("gattc_exchange_mtu") ble.gattc_exchange_mtu(conn_handle) - if not wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS): - return - mtu = waiting_events[_IRQ_MTU_EXCHANGED] + mtu = wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS) print("gatts_notify") ble.gatts_notify(conn_handle, char_handle, str(i) * 64) - if not wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS): - return + # Extra timeout while client does service discovery. + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS * 2) print("gatts_read") data = ble.gatts_read(char_handle) print("characteristic len:", len(data), chr(data[0])) # Wait for the central to disconnect. - if not wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS): - return + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) print("gap_advertise") ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") @@ -154,8 +150,6 @@ def instance1(): multitest.next() try: for i in range(7): - waiting_events.clear() - if i < 4: ble.config(mtu=300) elif i == 5: @@ -166,39 +160,33 @@ def instance1(): ble.config(mtu=256) # Connect to peripheral and then disconnect. + # Extra scan timeout allows for the peripheral to receive the previous disconnect + # event and start advertising again. print("gap_connect") - ble.gap_connect(*BDADDR) - if not wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS): - return - conn_handle = waiting_events[_IRQ_PERIPHERAL_CONNECT] + ble.gap_connect(BDADDR[0], BDADDR[1], 5000) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) if i < 4: print("gattc_exchange_mtu") ble.gattc_exchange_mtu(conn_handle) - if not wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS): - return - mtu = waiting_events[_IRQ_MTU_EXCHANGED] + mtu = wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS) - if not wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS): - return + wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) print("gattc_discover_characteristics") ble.gattc_discover_characteristics(conn_handle, 1, 65535) - if not wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS): - return - value_handle = waiting_events[_IRQ_GATTC_CHARACTERISTIC_RESULT] + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) # Write 20 more than the MTU to test truncation. print("gattc_write") ble.gattc_write(conn_handle, value_handle, chr(ord("a") + i) * (mtu + 20), 1) - if not wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS): - return + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) # Disconnect from peripheral. print("gap_disconnect:", ble.gap_disconnect(conn_handle)) - if not wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS): - return + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) finally: ble.active(0) From c75ce379100993f41a7502986146e735367cf8aa Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 17:41:58 +1100 Subject: [PATCH 0403/3533] tests/run-multitests.py: Add a -p flag to run permutations of instances. Signed-off-by: Jim Mussared --- tests/run-multitests.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index 5b8e70e921a7a..cdb2730edabbd 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -6,6 +6,7 @@ import sys, os, time, re, select import argparse +import itertools import subprocess import tempfile @@ -418,6 +419,13 @@ def main(): cmd_parser.add_argument( "-i", "--instance", action="append", default=[], help="instance(s) to run the tests on" ) + cmd_parser.add_argument( + "-p", + "--permutations", + type=int, + default=1, + help="repeat the test with this many permutations of the instance order", + ) cmd_parser.add_argument("files", nargs="+", help="input test files") cmd_args = cmd_parser.parse_args() @@ -450,8 +458,14 @@ def main(): for _ in range(max_instances - len(instances_test)): instances_test.append(PyInstanceSubProcess([MICROPYTHON])) + all_pass = True try: - all_pass = run_tests(test_files, instances_truth, instances_test) + for i, instances_test_permutation in enumerate(itertools.permutations(instances_test)): + if i >= cmd_args.permutations: + break + + all_pass &= run_tests(test_files, instances_truth, instances_test_permutation) + finally: for i in instances_truth: i.close() From 3d890e7ab4b3d85a6fa9e57a9a596a23eeaa6aa7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Oct 2020 13:17:23 +1100 Subject: [PATCH 0404/3533] extmod/modbluetooth: Make UUID type accessible outside modbluetooth.c. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 1 + extmod/modbluetooth.c | 33 ++++++++++++++------------- extmod/modbluetooth.h | 4 +++- extmod/nimble/modbluetooth_nimble.c | 1 + 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index ae96035868877..fd5d343637b5a 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -75,6 +75,7 @@ STATIC int btstack_error_to_errno(int err) { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uuid128) { mp_obj_bluetooth_uuid_t result; + result.base.type = &mp_type_bluetooth_uuid; if (uuid16 != 0) { result.data[0] = uuid16 & 0xff; result.data[1] = (uuid16 >> 8) & 0xff; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 57f69433a1a97..90ed495462b28 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -51,9 +51,8 @@ // while still leaving room for a couple of normal (small, fixed size) events. #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(ringbuf_size) (MAX((int)((ringbuf_size) / 2), (int)(ringbuf_size) - 64)) -STATIC const mp_obj_type_t bluetooth_ble_type; -STATIC const mp_obj_type_t bluetooth_uuid_type; - +// bluetooth.BLE type. This is currently a singleton, however in the future +// this could allow having multiple BLE interfaces on different UARTs. typedef struct { mp_obj_base_t base; mp_obj_t irq_handler; @@ -70,6 +69,8 @@ typedef struct { #endif } mp_obj_bluetooth_ble_t; +STATIC const mp_obj_type_t mp_type_bluetooth_ble; + // TODO: this seems like it could be generic? STATIC mp_obj_t bluetooth_handle_errno(int err) { if (err != 0) { @@ -88,7 +89,7 @@ STATIC mp_obj_t bluetooth_uuid_make_new(const mp_obj_type_t *type, size_t n_args mp_arg_check_num(n_args, n_kw, 1, 1, false); mp_obj_bluetooth_uuid_t *self = m_new_obj(mp_obj_bluetooth_uuid_t); - self->base.type = &bluetooth_uuid_type; + self->base.type = &mp_type_bluetooth_uuid; if (mp_obj_is_int(all_args[0])) { self->type = MP_BLUETOOTH_UUID_TYPE_16; @@ -152,7 +153,7 @@ STATIC mp_obj_t bluetooth_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } STATIC mp_obj_t bluetooth_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { - if (!mp_obj_is_type(rhs_in, &bluetooth_uuid_type)) { + if (!mp_obj_is_type(rhs_in, &mp_type_bluetooth_uuid)) { return MP_OBJ_NULL; } @@ -225,7 +226,7 @@ STATIC void ringbuf_get_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -STATIC const mp_obj_type_t bluetooth_uuid_type = { +const mp_obj_type_t mp_type_bluetooth_uuid = { { &mp_type_type }, .name = MP_QSTR_UUID, .make_new = bluetooth_uuid_make_new, @@ -247,7 +248,7 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, (void)all_args; if (MP_STATE_VM(bluetooth) == MP_OBJ_NULL) { mp_obj_bluetooth_ble_t *o = m_new0(mp_obj_bluetooth_ble_t, 1); - o->base.type = &bluetooth_ble_type; + o->base.type = &mp_type_bluetooth_ble; o->irq_handler = mp_const_none; @@ -263,7 +264,7 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, mp_obj_memoryview_init(&o->irq_data_addr, 'B', 0, 0, o->irq_data_addr_bytes); o->irq_data_data_alloc = MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(MICROPY_PY_BLUETOOTH_RINGBUF_SIZE); mp_obj_memoryview_init(&o->irq_data_data, 'B', 0, 0, m_new(uint8_t, o->irq_data_data_alloc)); - o->irq_data_uuid.base.type = &bluetooth_uuid_type; + o->irq_data_uuid.base.type = &mp_type_bluetooth_uuid; // Allocate the default ringbuf. ringbuf_alloc(&o->ringbuf, MICROPY_PY_BLUETOOTH_RINGBUF_SIZE); @@ -441,7 +442,7 @@ STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_a STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_gap_advertise_obj, 1, bluetooth_ble_gap_advertise); STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t characteristics_in, uint16_t **handles, size_t *num_handles) { - if (!mp_obj_is_type(uuid_in, &bluetooth_uuid_type)) { + if (!mp_obj_is_type(uuid_in, &mp_type_bluetooth_uuid)) { mp_raise_ValueError(MP_ERROR_TEXT("invalid service UUID")); } mp_obj_bluetooth_uuid_t *service_uuid = MP_OBJ_TO_PTR(uuid_in); @@ -482,7 +483,7 @@ STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t character mp_raise_ValueError(MP_ERROR_TEXT("invalid characteristic tuple")); } mp_obj_t uuid_obj = characteristic_items[0]; - if (!mp_obj_is_type(uuid_obj, &bluetooth_uuid_type)) { + if (!mp_obj_is_type(uuid_obj, &mp_type_bluetooth_uuid)) { mp_raise_ValueError(MP_ERROR_TEXT("invalid characteristic UUID")); } @@ -514,7 +515,7 @@ STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t character mp_obj_t *descriptor_items; mp_obj_get_array_fixed_n(descriptor_obj, 2, &descriptor_items); mp_obj_t desc_uuid_obj = descriptor_items[0]; - if (!mp_obj_is_type(desc_uuid_obj, &bluetooth_uuid_type)) { + if (!mp_obj_is_type(desc_uuid_obj, &mp_type_bluetooth_uuid)) { mp_raise_ValueError(MP_ERROR_TEXT("invalid descriptor UUID")); } @@ -717,7 +718,7 @@ STATIC mp_obj_t bluetooth_ble_gattc_discover_services(size_t n_args, const mp_ob mp_int_t conn_handle = mp_obj_get_int(args[1]); mp_obj_bluetooth_uuid_t *uuid = NULL; if (n_args == 3 && args[2] != mp_const_none) { - if (!mp_obj_is_type(args[2], &bluetooth_uuid_type)) { + if (!mp_obj_is_type(args[2], &mp_type_bluetooth_uuid)) { mp_raise_TypeError(MP_ERROR_TEXT("UUID")); } uuid = MP_OBJ_TO_PTR(args[2]); @@ -732,7 +733,7 @@ STATIC mp_obj_t bluetooth_ble_gattc_discover_characteristics(size_t n_args, cons mp_int_t end_handle = mp_obj_get_int(args[3]); mp_obj_bluetooth_uuid_t *uuid = NULL; if (n_args == 5 && args[4] != mp_const_none) { - if (!mp_obj_is_type(args[4], &bluetooth_uuid_type)) { + if (!mp_obj_is_type(args[4], &mp_type_bluetooth_uuid)) { mp_raise_TypeError(MP_ERROR_TEXT("UUID")); } uuid = MP_OBJ_TO_PTR(args[4]); @@ -817,7 +818,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table); -STATIC const mp_obj_type_t bluetooth_ble_type = { +STATIC const mp_obj_type_t mp_type_bluetooth_ble = { { &mp_type_type }, .name = MP_QSTR_BLE, .make_new = bluetooth_ble_make_new, @@ -826,8 +827,8 @@ STATIC const mp_obj_type_t bluetooth_ble_type = { STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluetooth) }, - { MP_ROM_QSTR(MP_QSTR_BLE), MP_ROM_PTR(&bluetooth_ble_type) }, - { MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bluetooth_uuid_type) }, + { MP_ROM_QSTR(MP_QSTR_BLE), MP_ROM_PTR(&mp_type_bluetooth_ble) }, + { MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&mp_type_bluetooth_uuid) }, { MP_ROM_QSTR(MP_QSTR_FLAG_READ), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ) }, { MP_ROM_QSTR(MP_QSTR_FLAG_WRITE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE) }, { MP_ROM_QSTR(MP_QSTR_FLAG_NOTIFY), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) }, diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 2df4c3c25f17b..618939ab18f0e 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -135,7 +135,7 @@ _IRQ_GATTS_INDICATE_DONE = const(20) _IRQ_MTU_EXCHANGED = const(21) */ -// Common UUID type. +// bluetooth.UUID type. // Ports are expected to map this to their own internal UUID types. // Internally the UUID data is little-endian, but the user should only // ever see this if they use the buffer protocol, e.g. in order to @@ -147,6 +147,8 @@ typedef struct { uint8_t data[16]; } mp_obj_bluetooth_uuid_t; +extern const mp_obj_type_t mp_type_bluetooth_uuid; + ////////////////////////////////////////////////////////////// // API implemented by ports (i.e. called from modbluetooth.c): diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index c0d6d64cfc1f0..acb4c03dc32f5 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -114,6 +114,7 @@ STATIC void reverse_addr_byte_order(uint8_t *addr_out, const uint8_t *addr_in) { STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(const ble_uuid_any_t *uuid) { mp_obj_bluetooth_uuid_t result; + result.base.type = &mp_type_bluetooth_uuid; switch (uuid->u.type) { case BLE_UUID_TYPE_16: result.type = MP_BLUETOOTH_UUID_TYPE_16; From de60aa7d6bef3dc25559ae88e36bd05283e927e5 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 16 Oct 2020 10:10:26 +1100 Subject: [PATCH 0405/3533] unix: Handle pending events/scheduler in MICROPY_EVENT_POLL_HOOK. Signed-off-by: Jim Mussared --- ports/unix/mpconfigport.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 17f4895573ed5..d838f42b3f3dc 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -363,7 +363,12 @@ struct _mp_bluetooth_nimble_malloc_t; #define MICROPY_END_ATOMIC_SECTION(x) (void)x; mp_thread_unix_end_atomic_section() #endif -#define MICROPY_EVENT_POLL_HOOK mp_hal_delay_us(500); +#define MICROPY_EVENT_POLL_HOOK \ + do { \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ + mp_hal_delay_us(500); \ + } while (0); #include #define MICROPY_UNIX_MACHINE_IDLE sched_yield(); From 4559bcb4679e04e0a5e24030675676ff6a9803f2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 23:21:18 +1100 Subject: [PATCH 0406/3533] unix: Make mp_hal_delay_ms run MICROPY_EVENT_POLL_HOOK. Signed-off-by: Jim Mussared --- ports/unix/mphalport.h | 5 ----- ports/unix/unix_mphal.c | 14 ++++++++++++++ ports/windows/windows_mphal.c | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ports/unix/mphalport.h b/ports/unix/mphalport.h index 95ad63221ef95..89d23ca5d22e9 100644 --- a/ports/unix/mphalport.h +++ b/ports/unix/mphalport.h @@ -64,11 +64,6 @@ static inline int mp_hal_readline(vstr_t *vstr, const char *p) { #endif -// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: -// "The useconds argument shall be less than one million." -static inline void mp_hal_delay_ms(mp_uint_t ms) { - usleep((ms) * 1000); -} static inline void mp_hal_delay_us(mp_uint_t us) { usleep(us); } diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 3fe2fa9fec21d..28a4ca2c4a8e9 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -220,3 +220,17 @@ uint64_t mp_hal_time_ns(void) { gettimeofday(&tv, NULL); return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } + +void mp_hal_delay_ms(mp_uint_t ms) { + #ifdef MICROPY_EVENT_POLL_HOOK + mp_uint_t start = mp_hal_ticks_ms(); + while (mp_hal_ticks_ms() - start < ms) { + // MICROPY_EVENT_POLL_HOOK does mp_hal_delay_us(500) (i.e. usleep(500)). + MICROPY_EVENT_POLL_HOOK + } + #else + // TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: + // "The useconds argument shall be less than one million." + usleep(ms * 1000); + #endif +} diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index b442b59147e4e..49daa0542868f 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -261,3 +261,9 @@ uint64_t mp_hal_time_ns(void) { gettimeofday(&tv, NULL); return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } + +// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: +// "The useconds argument shall be less than one million." +void mp_hal_delay_ms(mp_uint_t ms) { + usleep((ms) * 1000); +} From c398e46b29f5c780b8016f2e88afe4c6984c54d8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 17:46:11 +1100 Subject: [PATCH 0407/3533] extmod/modbluetooth: Combine gattc-data-available callbacks into one. Instead of having the stack indicate a "start", "data"..., "end", pass through the data in one callback as an array of chunks of data. This is because the upcoming non-ringbuffer modbluetooth implementation cannot buffer the data in the ringbuffer and requires instead a single callback with all the data, to pass to the Python callback. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 15 ++-------- extmod/modbluetooth.c | 41 ++++++++++++++------------- extmod/modbluetooth.h | 6 +--- extmod/nimble/modbluetooth_nimble.c | 35 +++++++++++++++++------ 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index fd5d343637b5a..a4cc601746b1e 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -414,30 +414,21 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet); uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet); const uint8_t *data = gatt_event_characteristic_value_query_result_get_value(packet); - mp_uint_t atomic_state; - len = mp_bluetooth_gattc_on_data_available_start(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, value_handle, len, &atomic_state); - mp_bluetooth_gattc_on_data_available_chunk(data, len); - mp_bluetooth_gattc_on_data_available_end(atomic_state); + mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, value_handle, &data, &len, 1); } else if (event_type == GATT_EVENT_NOTIFICATION) { DEBUG_printf(" --> gatt notification\n"); uint16_t conn_handle = gatt_event_notification_get_handle(packet); uint16_t value_handle = gatt_event_notification_get_value_handle(packet); uint16_t len = gatt_event_notification_get_value_length(packet); const uint8_t *data = gatt_event_notification_get_value(packet); - mp_uint_t atomic_state; - len = mp_bluetooth_gattc_on_data_available_start(MP_BLUETOOTH_IRQ_GATTC_NOTIFY, conn_handle, value_handle, len, &atomic_state); - mp_bluetooth_gattc_on_data_available_chunk(data, len); - mp_bluetooth_gattc_on_data_available_end(atomic_state); + mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_NOTIFY, conn_handle, value_handle, &data, &len, 1); } else if (event_type == GATT_EVENT_INDICATION) { DEBUG_printf(" --> gatt indication\n"); uint16_t conn_handle = gatt_event_indication_get_handle(packet); uint16_t value_handle = gatt_event_indication_get_value_handle(packet); uint16_t len = gatt_event_indication_get_value_length(packet); const uint8_t *data = gatt_event_indication_get_value(packet); - mp_uint_t atomic_state; - len = mp_bluetooth_gattc_on_data_available_start(MP_BLUETOOTH_IRQ_GATTC_INDICATE, conn_handle, value_handle, len, &atomic_state); - mp_bluetooth_gattc_on_data_available_chunk(data, len); - mp_bluetooth_gattc_on_data_available_end(atomic_state); + mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_INDICATE, conn_handle, value_handle, &data, &len, 1); } else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) { uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet); DEBUG_printf(" --> gatt can write without response %d\n", conn_handle); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 90ed495462b28..6bad134959271 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1125,31 +1125,34 @@ void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle schedule_ringbuf(atomic_state); } -size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out) { +void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { MICROPY_PY_BLUETOOTH_ENTER - *atomic_state_out = atomic_state; mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - data_len = MIN(o->irq_data_data_alloc, data_len); - if (enqueue_irq(o, 2 + 2 + 2 + data_len, event)) { + + // Get the total length of the fragmented buffers. + uint16_t total_len = 0; + for (size_t i = 0; i < num; ++i) { + total_len += data_len[i]; + } + + // Truncate the data at what we'll be able to pass to Python. + total_len = MIN(o->irq_data_data_alloc, total_len); + + if (enqueue_irq(o, 2 + 2 + 2 + total_len, event)) { ringbuf_put16(&o->ringbuf, conn_handle); ringbuf_put16(&o->ringbuf, value_handle); - // Length field is 16-bit. - data_len = MIN(UINT16_MAX, data_len); - ringbuf_put16(&o->ringbuf, data_len); - return data_len; - } else { - return 0; - } -} -void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len) { - mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - for (size_t i = 0; i < data_len; ++i) { - ringbuf_put(&o->ringbuf, data[i]); - } -} + ringbuf_put16(&o->ringbuf, total_len); -void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) { + // Copy total_len from the fragments to the ringbuffer. + uint16_t copied_bytes = 0; + for (size_t i = 0; i < num; ++i) { + for (size_t j = 0; i < data_len[i] && copied_bytes < total_len; ++j) { + ringbuf_put(&o->ringbuf, data[i][j]); + ++copied_bytes; + } + } + } schedule_ringbuf(atomic_state); } diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 618939ab18f0e..dee7186a2db3f 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -289,11 +289,7 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status); // Notify modbluetooth that a read has completed with data (or notify/indicate data available, use `event` to disambiguate). -// Note: these functions are to be called in a group protected by MICROPY_PY_BLUETOOTH_ENTER/EXIT. -// _start returns the number of bytes to submit to the calls to _chunk, followed by a call to _end. -size_t mp_bluetooth_gattc_on_data_available_start(uint8_t event, uint16_t conn_handle, uint16_t value_handle, size_t data_len, mp_uint_t *atomic_state_out); -void mp_bluetooth_gattc_on_data_available_chunk(const uint8_t *data, size_t data_len); -void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state); +void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num); // Notify modbluetooth that a read or write operation has completed. void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index acb4c03dc32f5..621278e0f15c8 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -798,16 +798,35 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { - size_t len = OS_MBUF_PKTLEN(om); - mp_uint_t atomic_state; - len = mp_bluetooth_gattc_on_data_available_start(event, conn_handle, value_handle, len, &atomic_state); - while (len > 0 && om != NULL) { - size_t n = MIN(om->om_len, len); - mp_bluetooth_gattc_on_data_available_chunk(OS_MBUF_DATA(om, const uint8_t *), n); - len -= n; + // When the HCI data for an ATT payload arrives, the L2CAP channel will + // buffer it into its receive buffer. We set BLE_L2CAP_JOIN_RX_FRAGS=1 in + // syscfg.h so it should be rare that the mbuf is fragmented, but we do need + // to be able to handle it. We pass all the fragments up to modbluetooth.c + // which will create a temporary buffer on the MicroPython heap if necessary + // to re-assemble them. + + // Count how many links are in the mbuf chain. + size_t n = 0; + const struct os_mbuf *elem = om; + while (elem) { + n += 1; + elem = SLIST_NEXT(elem, om_next); + } + + // Grab data pointers and lengths for each of the links. + const uint8_t **data = mp_local_alloc(sizeof(uint8_t *) * n); + uint16_t *data_len = mp_local_alloc(sizeof(uint16_t) * n); + for (size_t i = 0; i < n; ++i) { + data[i] = OS_MBUF_DATA(om, const uint8_t *); + data_len[i] = om->om_len; om = SLIST_NEXT(om, om_next); } - mp_bluetooth_gattc_on_data_available_end(atomic_state); + + // Pass all the fragments together. + mp_bluetooth_gattc_on_data_available(event, conn_handle, value_handle, data, data_len, n); + + mp_local_free(data_len); + mp_local_free(data); } STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) { From 6d9fdff8d07f3fa2a05eddb05e1a55754ae3542f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 17:48:55 +1100 Subject: [PATCH 0408/3533] extmod/nimble: Poll startup directly rather than using NimBLE sem. Using a semaphore (the previous approach) will only run the UART, whereas for startup we need to also run the event queue. This change makes it run the full scheduler hook. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 621278e0f15c8..b387e6ae1f979 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -53,7 +53,6 @@ STATIC uint8_t nimble_address_mode = BLE_OWN_ADDR_RANDOM; #define NIMBLE_STARTUP_TIMEOUT 2000 -STATIC struct ble_npl_sem startup_sem; // Any BLE_HS_xxx code not in this table will default to MP_EIO. STATIC int8_t ble_hs_err_to_errno_table[] = { @@ -210,8 +209,6 @@ STATIC void sync_cb(void) { ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME); mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE; - - ble_npl_sem_release(&startup_sem); } STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { @@ -369,8 +366,6 @@ int mp_bluetooth_init(void) { ble_hs_cfg.gatts_register_cb = gatts_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; - ble_npl_sem_init(&startup_sem, 0); - MP_STATE_PORT(bluetooth_nimble_root_pointers) = m_new0(mp_bluetooth_nimble_root_pointers_t, 1); mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db); @@ -383,16 +378,24 @@ int mp_bluetooth_init(void) { // Otherwise default implementation above calls ble_hci_uart_init(). mp_bluetooth_nimble_port_hci_init(); + // Static initialization is complete, can start processing events. + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC; + // Initialise NimBLE memory and data structures. nimble_port_init(); // Make sure that the HCI UART and event handling task is running. mp_bluetooth_nimble_port_start(); - // Static initialization is complete, can start processing events. - mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC; - - ble_npl_sem_pend(&startup_sem, NIMBLE_STARTUP_TIMEOUT); + // Run the scheduler while we wait for stack startup. + // On non-ringbuffer builds (NimBLE on STM32/Unix) this will also poll the UART and run the event queue. + mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms(); + while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { + if (mp_hal_ticks_ms() - timeout_start_ticks_ms > NIMBLE_STARTUP_TIMEOUT) { + break; + } + MICROPY_EVENT_POLL_HOOK + } if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { mp_bluetooth_deinit(); From 81e92d3d6e1a605a6115821ac24dcbc2546ba0f9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Oct 2020 13:06:45 +1100 Subject: [PATCH 0409/3533] extmod/modbluetooth: Re-instate optional no-ringbuf modbluetooth. This requires that the event handlers are called from non-interrupt context (i.e. the MicroPython scheduler). This will allow the BLE stack (e.g. NimBLE) to run from the scheduler rather than an IRQ like PENDSV, and therefore be able to invoke Python callbacks directly/synchronously. This allows writing Python BLE handlers for events that require immediate response such as _IRQ_READ_REQUEST (which was previous a hard IRQ) and future events relating to pairing/bonding. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 4 +- extmod/modbluetooth.c | 224 +++++++++++++++++++++----- extmod/modbluetooth.h | 8 +- extmod/nimble/modbluetooth_nimble.c | 4 +- ports/stm32/Makefile | 1 - ports/unix/Makefile | 2 - 6 files changed, 189 insertions(+), 54 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index a4cc601746b1e..97dd2cbb53f5c 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -838,15 +838,15 @@ STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t a return 0; } - #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK // Allow Python code to override value (by using gatts_write), or deny (by returning false) the read. + // Note this will be a no-op if the ringbuffer implementation is being used, as the Python callback cannot + // be executed synchronously. This is currently always the case for btstack. if ((buffer == NULL) && (buffer_size == 0)) { if (!mp_bluetooth_gatts_on_read_request(connection_handle, att_handle)) { DEBUG_printf("att_read_callback: read request denied\n"); return 0; } } - #endif uint16_t ret = att_read_callback_handle_blob(entry->data, entry->data_len, offset, buffer, buffer_size); return ret; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 6bad134959271..d8068df594538 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -47,15 +47,18 @@ #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN 5 +#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // This formula is intended to allow queuing the data of a large characteristic // while still leaving room for a couple of normal (small, fixed size) events. #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(ringbuf_size) (MAX((int)((ringbuf_size) / 2), (int)(ringbuf_size) - 64)) +#endif // bluetooth.BLE type. This is currently a singleton, however in the future // this could allow having multiple BLE interfaces on different UARTs. typedef struct { mp_obj_base_t base; mp_obj_t irq_handler; + #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS bool irq_scheduled; mp_obj_t irq_data_tuple; uint8_t irq_data_addr_bytes[6]; @@ -64,8 +67,6 @@ typedef struct { mp_obj_array_t irq_data_data; mp_obj_bluetooth_uuid_t irq_data_uuid; ringbuf_t ringbuf; - #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK - mp_obj_t irq_read_request_data_tuple; #endif } mp_obj_bluetooth_ble_t; @@ -206,8 +207,7 @@ STATIC mp_int_t bluetooth_uuid_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bu return 0; } -#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE - +#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS && MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void ringbuf_put_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) { assert(ringbuf_free(ringbuf) >= (size_t)uuid->type + 1); ringbuf_put(ringbuf, uuid->type); @@ -252,14 +252,10 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, o->irq_handler = mp_const_none; + #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler. o->irq_data_tuple = mp_obj_new_tuple(MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN, NULL); - #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK - // Pre-allocate a separate data tuple for the read request "hard" irq. - o->irq_read_request_data_tuple = mp_obj_new_tuple(2, NULL); - #endif - // Pre-allocated buffers for address, payload and uuid. mp_obj_memoryview_init(&o->irq_data_addr, 'B', 0, 0, o->irq_data_addr_bytes); o->irq_data_data_alloc = MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(MICROPY_PY_BLUETOOTH_RINGBUF_SIZE); @@ -268,6 +264,7 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, // Allocate the default ringbuf. ringbuf_alloc(&o->ringbuf, MICROPY_PY_BLUETOOTH_RINGBUF_SIZE); + #endif MP_STATE_VM(bluetooth) = MP_OBJ_FROM_PTR(o); } @@ -290,8 +287,6 @@ STATIC mp_obj_t bluetooth_ble_active(size_t n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_active_obj, 1, 2, bluetooth_ble_active); STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - mp_obj_bluetooth_ble_t *self = MP_OBJ_TO_PTR(args[0]); - if (kwargs->used == 0) { // Get config value if (n_args != 2) { @@ -311,8 +306,12 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map mp_obj_t items[] = { MP_OBJ_NEW_SMALL_INT(addr_type), mp_obj_new_bytes(addr, MP_ARRAY_SIZE(addr)) }; return mp_obj_new_tuple(2, items); } - case MP_QSTR_rxbuf: + #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + case MP_QSTR_rxbuf: { + mp_obj_bluetooth_ble_t *self = MP_OBJ_TO_PTR(args[0]); return mp_obj_new_int(self->ringbuf.size); + } + #endif case MP_QSTR_mtu: return mp_obj_new_int(mp_bluetooth_get_preferred_mtu()); default: @@ -334,6 +333,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map bluetooth_handle_errno(mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len)); break; } + #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS case MP_QSTR_rxbuf: { // Determine new buffer sizes mp_int_t ringbuf_alloc = mp_obj_get_int(e->value); @@ -347,6 +347,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map uint8_t *irq_data = m_new(uint8_t, irq_data_alloc); // Get old buffer sizes and pointers + mp_obj_bluetooth_ble_t *self = MP_OBJ_TO_PTR(args[0]); uint8_t *old_ringbuf_buf = self->ringbuf.buf; size_t old_ringbuf_alloc = self->ringbuf.size; uint8_t *old_irq_data_buf = (uint8_t *)self->irq_data_data.items; @@ -367,6 +368,7 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map m_del(uint8_t, old_irq_data_buf, old_irq_data_alloc); break; } + #endif case MP_QSTR_mtu: { mp_int_t mtu = mp_obj_get_int(e->value); bluetooth_handle_errno(mp_bluetooth_set_preferred_mtu(mtu)); @@ -845,8 +847,7 @@ const mp_obj_module_t mp_module_ubluetooth = { // Helpers -#include - +#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size_t n_u16, size_t n_u8, mp_obj_array_t *bytes_addr, size_t n_i8, mp_obj_bluetooth_uuid_t *uuid, mp_obj_array_t *bytes_data) { assert(ringbuf_avail(ringbuf) >= n_u16 * 2 + n_u8 + (bytes_addr ? 6 : 0) + n_i8 + (uuid ? 1 : 0) + (bytes_data ? 1 : 0)); size_t j = 0; @@ -854,7 +855,7 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size for (size_t i = 0; i < n_u16; ++i) { data_tuple->items[j++] = MP_OBJ_NEW_SMALL_INT(ringbuf_get16(ringbuf)); } - if (n_u8) { + for (size_t i = 0; i < n_u8; ++i) { data_tuple->items[j++] = MP_OBJ_NEW_SMALL_INT(ringbuf_get(ringbuf)); } if (bytes_addr) { @@ -960,11 +961,166 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_ble_invoke_irq_obj, bluetooth_ble_invoke_irq); +#endif // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // ---------------------------------------------------------------------------- // Port API // ---------------------------------------------------------------------------- +#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + +STATIC mp_obj_t invoke_irq_handler(uint16_t event, + const uint16_t *u16, size_t n_u16, + const uint8_t *u8, size_t n_u8, + const uint8_t *addr, + const int8_t *i8, size_t n_i8, + const mp_obj_bluetooth_uuid_t *uuid, + const uint8_t *data, size_t data_len) { + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (o->irq_handler == mp_const_none) { + return mp_const_none; + } + + mp_obj_array_t mv_addr; + mp_obj_array_t mv_data; + + mp_obj_tuple_t *data_tuple = mp_local_alloc(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t) * MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); + data_tuple->base.type = &mp_type_tuple; + data_tuple->len = 0; + + for (size_t i = 0; i < n_u16; ++i) { + data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(u16[i]); + } + for (size_t i = 0; i < n_u8; ++i) { + data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(u8[i]); + } + if (addr) { + mp_obj_memoryview_init(&mv_addr, 'B', 0, 6, (void *)addr); + data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(&mv_addr); + } + for (size_t i = 0; i < n_i8; ++i) { + data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(i8[i]); + } + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + if (uuid) { + data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(uuid); + } + #endif + if (data) { + mp_obj_memoryview_init(&mv_data, 'B', 0, data_len, (void *)data); + data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(&mv_data); + } + assert(data_tuple->len <= MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); + + mp_obj_t result = mp_call_function_2(o->irq_handler, MP_OBJ_NEW_SMALL_INT(event), MP_OBJ_FROM_PTR(data_tuple)); + + mp_local_free(data_tuple); + + return result; +} + +#define NULL_U16 NULL +#define NULL_U8 NULL +#define NULL_ADDR NULL +#define NULL_I8 NULL +#define NULL_UUID NULL +#define NULL_DATA NULL + +void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr) { + invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { + uint16_t args[] = {conn_handle, value_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status) { + uint16_t args[] = {conn_handle, value_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { + uint16_t args[] = {conn_handle, value_handle}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + return result == mp_const_none || mp_obj_is_true(result); +} + +void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { + uint16_t args[] = {conn_handle, value}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +void mp_bluetooth_gap_on_scan_complete(void) { + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_U16, 0, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) { + int8_t args[] = {adv_type, rssi}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, NULL_U16, 0, &addr_type, 1, addr, args, 2, NULL_UUID, data, data_len); +} + +void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid) { + uint16_t args[] = {conn_handle, start_handle, end_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, service_uuid, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { + uint16_t args[] = {conn_handle, def_handle, value_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 3, &properties, 1, NULL_ADDR, NULL_I8, 0, characteristic_uuid, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t handle, mp_obj_bluetooth_uuid_t *descriptor_uuid) { + uint16_t args[] = {conn_handle, handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, descriptor_uuid, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status) { + uint16_t args[] = {conn_handle, status}; + invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { + const uint8_t *combined_data; + uint16_t total_len; + + if (num > 1) { + // Fragmented buffer, need to combine into a new heap-allocated buffer + // in order to pass to Python. + total_len = 0; + for (size_t i = 0; i < num; ++i) { + total_len += data_len[i]; + } + uint8_t *buf = m_new(uint8_t, total_len); + uint8_t *p = buf; + for (size_t i = 0; i < num; ++i) { + memcpy(p, data[i], data_len[i]); + p += data_len[i]; + } + combined_data = buf; + } else { + // Single buffer, use directly. + combined_data = *data; + total_len = *data_len; + } + + uint16_t args[] = {conn_handle, value_handle}; + invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, combined_data, total_len); + + if (num > 1) { + m_del(uint8_t, (uint8_t *)combined_data, total_len); + } +} + +void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) { + uint16_t args[] = {conn_handle, value_handle, status}; + invoke_irq_handler(event, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +#else // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // Callbacks are called in interrupt context (i.e. can't allocate), so we need to push the data // into the ringbuf and schedule the callback via mp_sched_schedule. @@ -1169,36 +1325,12 @@ void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -#if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK -// This can only be enabled when the thread invoking this is a MicroPython thread. -// On ESP32, for example, this is not the case. bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { - mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (o->irq_handler != mp_const_none) { - // When executing code within a handler we must lock the scheduler to - // prevent any scheduled callbacks from running, and lock the GC to - // prevent any memory allocations. - mp_sched_lock(); - gc_lock(); - - // Use pre-allocated tuple distinct to the one used by the "soft" IRQs. - mp_obj_tuple_t *data = MP_OBJ_TO_PTR(o->irq_read_request_data_tuple); - data->items[0] = MP_OBJ_NEW_SMALL_INT(conn_handle); - data->items[1] = MP_OBJ_NEW_SMALL_INT(value_handle); - data->len = 2; - mp_obj_t irq_ret = mp_call_function_2_protected(o->irq_handler, MP_OBJ_NEW_SMALL_INT(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST), o->irq_read_request_data_tuple); - - gc_unlock(); - mp_sched_unlock(); - - // If the IRQ handler explicitly returned false, then deny the read. Otherwise if it returns None/True, allow it. - return irq_ret != MP_OBJ_NULL && (irq_ret == mp_const_none || mp_obj_is_true(irq_ret)); - } else { - // No IRQ handler, allow the read. - return true; - } + (void)conn_handle; + (void)value_handle; + // This must be handled synchronously and therefore cannot implemented with the ringbuffer. + return true; } -#endif void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { MICROPY_PY_BLUETOOTH_ENTER @@ -1210,6 +1342,12 @@ void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { schedule_ringbuf(atomic_state); } +#endif // MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + +// ---------------------------------------------------------------------------- +// GATTS DB +// ---------------------------------------------------------------------------- + void mp_bluetooth_gatts_db_create_entry(mp_gatts_db_t db, uint16_t handle, size_t len) { mp_map_elem_t *elem = mp_map_lookup(db, MP_OBJ_NEW_SMALL_INT(handle), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); mp_bluetooth_gatts_db_entry_t *entry = m_new(mp_bluetooth_gatts_db_entry_t, 1); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index dee7186a2db3f..6ed086d553f05 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -43,8 +43,10 @@ #define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (0) #endif -#ifndef MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK -#define MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK (0) +#ifndef MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +// This can be enabled if the BLE stack runs entirely in scheduler context +// and therefore is able to call directly into the VM to run Python callbacks. +#define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS (0) #endif // This is used to protect the ringbuffer. @@ -261,10 +263,8 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); // Call this when an acknowledgment is received for an indication. void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status); -#if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK // Call this when a characteristic is read from. Return false to deny the read. bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle); -#endif // Call this when an MTU exchange completes. void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index b387e6ae1f979..7ee6ae8677e8b 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -586,12 +586,12 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: case BLE_GATT_ACCESS_OP_READ_DSC: - #if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK // Allow Python code to override (by using gatts_write), or deny (by returning false) the read. + // Note this will be a no-op if the ringbuffer implementation is being used (i.e. the stack isn't + // run in the scheduler). The ringbuffer is not used on STM32 and Unix-H4 only. if (!mp_bluetooth_gatts_on_read_request(conn_handle, value_handle)) { return BLE_ATT_ERR_READ_NOT_PERMITTED; } - #endif entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle); if (!entry) { diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 18f7e8a1a10d8..75090a077e2bf 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -430,7 +430,6 @@ USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\ ifeq ($(MICROPY_PY_BLUETOOTH),1) CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=1 endif ifeq ($(MICROPY_PY_NETWORK_CYW43),1) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 048ef97f77531..f72c05f1add78 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -156,8 +156,6 @@ endif CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 -# Runs in a thread, cannot make calls into the VM. -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=0 ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) From 61d1e4b01b1bf77e5ca478e18065f0691ae274a7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 23:27:47 +1100 Subject: [PATCH 0410/3533] extmod/nimble: Make stm32 and unix NimBLE ports use synchronous events. This changes stm32 from using PENDSV to run NimBLE to use the MicroPython scheduler instead. This allows Python BLE callbacks to be invoked directly (and therefore synchronously) rather than via the ringbuffer. The NimBLE UART HCI and event processing now happens in a scheduled task every 128ms. When RX IRQ idle events arrive, it will also schedule this task to improve latency. There is a similar change for the unix port where the background thread now queues the scheduled task. Signed-off-by: Jim Mussared --- extmod/modbluetooth.h | 1 + extmod/nimble/hal/hal_uart.c | 23 +++- extmod/nimble/hal/hal_uart.h | 2 +- extmod/nimble/modbluetooth_nimble.c | 5 +- extmod/nimble/nimble.mk | 7 + extmod/nimble/nimble/nimble_npl_os.c | 189 ++++++++++++++------------- extmod/nimble/nimble/nimble_npl_os.h | 4 +- ports/stm32/main.c | 4 +- ports/stm32/mpbthciport.c | 65 ++++++++- ports/stm32/mpconfigport.h | 9 +- ports/stm32/mpnimbleport.c | 30 ++--- ports/stm32/pendsv.h | 2 +- ports/stm32/rfcore.c | 6 +- ports/unix/mpbthciport.c | 54 +++++++- ports/unix/mpnimbleport.c | 34 ++--- 15 files changed, 279 insertions(+), 156 deletions(-) diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 6ed086d553f05..52e3446ff3b94 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -50,6 +50,7 @@ #endif // This is used to protect the ringbuffer. +// A port may no-op this if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS is enabled. #ifndef MICROPY_PY_BLUETOOTH_ENTER #define MICROPY_PY_BLUETOOTH_ENTER mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); #define MICROPY_PY_BLUETOOTH_EXIT MICROPY_END_ATOMIC_SECTION(atomic_state); diff --git a/extmod/nimble/hal/hal_uart.c b/extmod/nimble/hal/hal_uart.c index c6d0850fea993..230970b089c55 100644 --- a/extmod/nimble/hal/hal_uart.c +++ b/extmod/nimble/hal/hal_uart.c @@ -28,10 +28,13 @@ #include "py/mphal.h" #include "nimble/ble.h" #include "extmod/nimble/hal/hal_uart.h" +#include "extmod/nimble/nimble/nimble_npl_os.h" #include "extmod/mpbthci.h" #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE +#define HCI_TRACE (0) + static hal_uart_tx_cb_t hal_uart_tx_cb; static void *hal_uart_tx_arg; static hal_uart_rx_cb_t hal_uart_rx_cb; @@ -62,10 +65,10 @@ void hal_uart_start_tx(uint32_t port) { mp_bluetooth_hci_cmd_buf[len++] = data; } - #if 0 - printf("[% 8d] BTUTX: %02x", mp_hal_ticks_ms(), hci_cmd_buf[0]); - for (int i = 1; i < len; ++i) { - printf(":%02x", hci_cmd_buf[i]); + #if HCI_TRACE + printf("< [% 8d] %02x", mp_hal_ticks_ms(), mp_bluetooth_hci_cmd_buf[0]); + for (size_t i = 1; i < len; ++i) { + printf(":%02x", mp_bluetooth_hci_cmd_buf[i]); } printf("\n"); #endif @@ -77,13 +80,21 @@ int hal_uart_close(uint32_t port) { return 0; // success } -void mp_bluetooth_nimble_hci_uart_process(void) { +void mp_bluetooth_nimble_hci_uart_process(bool run_events) { bool host_wake = mp_bluetooth_hci_controller_woken(); int chr; while ((chr = mp_bluetooth_hci_uart_readchar()) >= 0) { - // printf("UART RX: %02x\n", data); + #if HCI_TRACE + printf("> %02x (%d)\n", chr); + #endif hal_uart_rx_cb(hal_uart_rx_arg, chr); + + // Incoming data may result in events being enqueued. If we're in + // scheduler context then we can run those events immediately. + if (run_events) { + mp_bluetooth_nimble_os_eventq_run_all(); + } } if (host_wake) { diff --git a/extmod/nimble/hal/hal_uart.h b/extmod/nimble/hal/hal_uart.h index 1ff1c17436dae..647e8ab4772e2 100644 --- a/extmod/nimble/hal/hal_uart.h +++ b/extmod/nimble/hal/hal_uart.h @@ -43,6 +43,6 @@ void hal_uart_start_tx(uint32_t port); int hal_uart_close(uint32_t port); // --- Called by the MicroPython port when UART data is available ------------- -void mp_bluetooth_nimble_hci_uart_process(void); +void mp_bluetooth_nimble_hci_uart_process(bool run_events); #endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_HAL_HAL_UART_H diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 7ee6ae8677e8b..c961aee3262c9 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -382,6 +382,7 @@ int mp_bluetooth_init(void) { mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC; // Initialise NimBLE memory and data structures. + DEBUG_printf("mp_bluetooth_init: nimble_port_init\n"); nimble_port_init(); // Make sure that the HCI UART and event handling task is running. @@ -402,6 +403,8 @@ int mp_bluetooth_init(void) { return MP_ETIMEDOUT; } + DEBUG_printf("mp_bluetooth_init: starting services\n"); + // By default, just register the default gap/gatt service. ble_svc_gap_init(); ble_svc_gatt_init(); @@ -417,7 +420,7 @@ int mp_bluetooth_init(void) { } void mp_bluetooth_deinit(void) { - DEBUG_printf("mp_bluetooth_deinit\n"); + DEBUG_printf("mp_bluetooth_deinit %d\n", mp_bluetooth_nimble_ble_state); if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { return; } diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index fbd031b3e3e16..00a244d6eac77 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -17,6 +17,13 @@ CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=$(MICROPY_BLUETOOTH_NIMBL ifeq ($(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY),0) +# On all ports where we provide the full implementation (i.e. not just +# bindings like on ESP32), then we don't need to use the ringbuffer. In this +# case, all NimBLE events are run by the MicroPython scheduler. On Unix, the +# scheduler is also responsible for polling the UART, whereas on STM32 the +# UART is also polled by the RX IRQ. +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 + NIMBLE_LIB_DIR = lib/mynewt-nimble LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ diff --git a/extmod/nimble/nimble/nimble_npl_os.c b/extmod/nimble/nimble/nimble_npl_os.c index 2ec012940f763..b68957fabf385 100644 --- a/extmod/nimble/nimble/nimble_npl_os.c +++ b/extmod/nimble/nimble/nimble_npl_os.c @@ -179,63 +179,100 @@ int nimble_sprintf(char *str, const char *fmt, ...) { struct ble_npl_eventq *global_eventq = NULL; +// This must not be called recursively or concurrently with the UART handler. void mp_bluetooth_nimble_os_eventq_run_all(void) { - for (struct ble_npl_eventq *evq = global_eventq; evq != NULL; evq = evq->nextq) { - int n = 0; - while (evq->head != NULL && mp_bluetooth_nimble_ble_state > MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { - struct ble_npl_event *ev = evq->head; - evq->head = ev->next; - if (ev->next) { - ev->next->prev = NULL; - ev->next = NULL; - } - ev->prev = NULL; - DEBUG_EVENT_printf("event_run(%p)\n", ev); - ev->fn(ev); - DEBUG_EVENT_printf("event_run(%p) done\n", ev); - - if (++n > 3) { - // Limit to running 3 tasks per queue. - // Some tasks (such as reset) can enqueue themselves - // making this an infinite loop (while in PENDSV). + if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { + return; + } + + // Keep running while there are pending events. + while (true) { + struct ble_npl_event *ev = NULL; + + os_sr_t sr; + OS_ENTER_CRITICAL(sr); + // Search all queues for an event. + for (struct ble_npl_eventq *evq = global_eventq; evq != NULL; evq = evq->nextq) { + ev = evq->head; + if (ev) { + // Remove this event from the queue. + evq->head = ev->next; + if (ev->next) { + ev->next->prev = NULL; + ev->next = NULL; + } + ev->prev = NULL; + + ev->pending = false; + + // Stop searching and execute this event. break; } } + OS_EXIT_CRITICAL(sr); + + if (!ev) { + break; + } + + // Run the event handler. + DEBUG_EVENT_printf("event_run(%p)\n", ev); + ev->fn(ev); + DEBUG_EVENT_printf("event_run(%p) done\n", ev); + + if (ev->pending) { + // If this event has been re-enqueued while it was running, then + // stop running further events. This prevents an infinite loop + // where the reset event re-enqueues itself on HCI timeout. + break; + } } } void ble_npl_eventq_init(struct ble_npl_eventq *evq) { DEBUG_EVENT_printf("ble_npl_eventq_init(%p)\n", evq); + os_sr_t sr; + OS_ENTER_CRITICAL(sr); evq->head = NULL; struct ble_npl_eventq **evq2; for (evq2 = &global_eventq; *evq2 != NULL; evq2 = &(*evq2)->nextq) { } *evq2 = evq; evq->nextq = NULL; + OS_EXIT_CRITICAL(sr); } void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev) { DEBUG_EVENT_printf("ble_npl_eventq_put(%p, %p (%p, %p))\n", evq, ev, ev->fn, ev->arg); + os_sr_t sr; + OS_ENTER_CRITICAL(sr); ev->next = NULL; + ev->pending = true; if (evq->head == NULL) { + // Empty list, make this the first item. evq->head = ev; ev->prev = NULL; } else { - struct ble_npl_event *ev2 = evq->head; + // Find the tail of this list. + struct ble_npl_event *tail = evq->head; while (true) { - if (ev2 == ev) { + if (tail == ev) { DEBUG_EVENT_printf(" --> already in queue\n"); - return; + // Already in the list (e.g. a fragmented ACL will enqueue an + // event to process it for each fragment). + break; } - if (ev2->next == NULL) { + if (tail->next == NULL) { + // Found the end of the list, add this event as the tail. + tail->next = ev; + ev->prev = tail; break; } - DEBUG_EVENT_printf(" --> %p\n", ev2->next); - ev2 = ev2->next; + DEBUG_EVENT_printf(" --> %p\n", tail->next); + tail = tail->next; } - ev2->next = ev; - ev->prev = ev2; } + OS_EXIT_CRITICAL(sr); } void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg) { @@ -243,6 +280,7 @@ void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *ar ev->fn = fn; ev->arg = arg; ev->next = NULL; + ev->pending = false; } void *ble_npl_event_get_arg(struct ble_npl_event *ev) { @@ -258,44 +296,17 @@ void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg) { /******************************************************************************/ // MUTEX -// This is what MICROPY_BEGIN_ATOMIC_SECTION returns on Unix (i.e. we don't -// need to preserve the atomic state to unlock). -#define ATOMIC_STATE_MUTEX_NOT_HELD 0xffffffff - ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu) { DEBUG_MUTEX_printf("ble_npl_mutex_init(%p)\n", mu); mu->locked = 0; - mu->atomic_state = ATOMIC_STATE_MUTEX_NOT_HELD; return BLE_NPL_OK; } ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout) { - DEBUG_MUTEX_printf("ble_npl_mutex_pend(%p, %u) locked=%u irq=%d\n", mu, (uint)timeout, (uint)mu->locked); + DEBUG_MUTEX_printf("ble_npl_mutex_pend(%p, %u) locked=%u\n", mu, (uint)timeout, (uint)mu->locked); - // This is a recursive mutex which we implement on top of the IRQ priority - // scheme. Unfortunately we have a single piece of global storage, where - // enter/exit critical needs an "atomic state". - - // There are two different acquirers, either running in a VM thread (i.e. - // a direct Python call into NimBLE), or in the NimBLE task (i.e. polling - // or UART RX). - - // On STM32 the NimBLE task runs in PENDSV, so cannot be interrupted by a VM thread. - // Therefore we only need to ensure that a VM thread that acquires a currently-unlocked mutex - // now raises the priority (thus preventing context switches to other VM threads and - // the PENDSV irq). If the mutex is already locked, then it must have been acquired - // by us. - - // On Unix, the critical section is completely recursive and doesn't require us to manage - // state so we just acquire and release every time. - - // TODO: The "volatile" on locked/atomic_state isn't enough to protect against memory re-ordering. - - // First acquirer of this mutex always enters the critical section, unless - // we're on Unix where it happens every time. - if (mu->atomic_state == ATOMIC_STATE_MUTEX_NOT_HELD) { - mu->atomic_state = mp_bluetooth_nimble_hci_uart_enter_critical(); - } + // All NimBLE code is executed by the scheduler (and is therefore + // implicitly mutexed) so this mutex implementation is a no-op. ++mu->locked; @@ -303,17 +314,11 @@ ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t time } ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu) { - DEBUG_MUTEX_printf("ble_npl_mutex_release(%p) locked=%u irq=%d\n", mu, (uint)mu->locked); + DEBUG_MUTEX_printf("ble_npl_mutex_release(%p) locked=%u\n", mu, (uint)mu->locked); assert(mu->locked > 0); --mu->locked; - // Only exit the critical section for the final release, unless we're on Unix. - if (mu->locked == 0 || mu->atomic_state == ATOMIC_STATE_MUTEX_NOT_HELD) { - mp_bluetooth_nimble_hci_uart_exit_critical(mu->atomic_state); - mu->atomic_state = ATOMIC_STATE_MUTEX_NOT_HELD; - } - return BLE_NPL_OK; } @@ -329,30 +334,19 @@ ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens) { ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout) { DEBUG_SEM_printf("ble_npl_sem_pend(%p, %u) count=%u\n", sem, (uint)timeout, (uint)sem->count); - // This is called by NimBLE to synchronously wait for an HCI ACK. The - // corresponding ble_npl_sem_release is called directly by the UART rx - // handler (i.e. hal_uart_rx_cb in extmod/nimble/hal/hal_uart.c). - - // So this implementation just polls the UART until either the semaphore - // is released, or the timeout occurs. + // This is only called by NimBLE in ble_hs_hci_cmd_tx to synchronously + // wait for an HCI ACK. The corresponding ble_npl_sem_release is called + // directly by the UART rx handler (i.e. hal_uart_rx_cb in + // extmod/nimble/hal/hal_uart.c). So this loop needs to run only the HCI + // UART processing but not run any events. if (sem->count == 0) { uint32_t t0 = mp_hal_ticks_ms(); while (sem->count == 0 && mp_hal_ticks_ms() - t0 < timeout) { - // This can be called either from code running in NimBLE's "task" - // (i.e. PENDSV) or directly by application code, so for the - // latter case, prevent the "task" from running while we poll the - // UART directly. - MICROPY_PY_BLUETOOTH_ENTER - mp_bluetooth_nimble_hci_uart_process(); - MICROPY_PY_BLUETOOTH_EXIT - if (sem->count != 0) { break; } - // Because we're polling, might as well wait for a UART IRQ indicating - // more data available. mp_bluetooth_nimble_hci_uart_wfi(); } @@ -384,6 +378,8 @@ uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem) { static struct ble_npl_callout *global_callout = NULL; void mp_bluetooth_nimble_os_callout_process(void) { + os_sr_t sr; + OS_ENTER_CRITICAL(sr); uint32_t tnow = mp_hal_ticks_ms(); for (struct ble_npl_callout *c = global_callout; c != NULL; c = c->nextc) { if (!c->active) { @@ -393,17 +389,24 @@ void mp_bluetooth_nimble_os_callout_process(void) { DEBUG_CALLOUT_printf("callout_run(%p) tnow=%u ticks=%u evq=%p\n", c, (uint)tnow, (uint)c->ticks, c->evq); c->active = false; if (c->evq) { + // Enqueue this callout for execution in the event queue. ble_npl_eventq_put(c->evq, &c->ev); } else { + // Execute this callout directly. + OS_EXIT_CRITICAL(sr); c->ev.fn(&c->ev); + OS_ENTER_CRITICAL(sr); } DEBUG_CALLOUT_printf("callout_run(%p) done\n", c); } } + OS_EXIT_CRITICAL(sr); } void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq, ble_npl_event_fn *ev_cb, void *ev_arg) { DEBUG_CALLOUT_printf("ble_npl_callout_init(%p, %p, %p, %p)\n", c, evq, ev_cb, ev_arg); + os_sr_t sr; + OS_ENTER_CRITICAL(sr); c->active = false; c->ticks = 0; c->evq = evq; @@ -413,17 +416,22 @@ void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq, for (c2 = &global_callout; *c2 != NULL; c2 = &(*c2)->nextc) { if (c == *c2) { // callout already in linked list so don't link it in again + OS_EXIT_CRITICAL(sr); return; } } *c2 = c; c->nextc = NULL; + OS_EXIT_CRITICAL(sr); } ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks) { DEBUG_CALLOUT_printf("ble_npl_callout_reset(%p, %u) tnow=%u\n", c, (uint)ticks, (uint)mp_hal_ticks_ms()); + os_sr_t sr; + OS_ENTER_CRITICAL(sr); c->active = true; c->ticks = ble_npl_time_get() + ticks; + OS_EXIT_CRITICAL(sr); return BLE_NPL_OK; } @@ -493,23 +501,20 @@ void ble_npl_time_delay(ble_npl_time_t ticks) { // CRITICAL // This is used anywhere NimBLE modifies global data structures. -// We need to protect between: -// - A MicroPython VM thread. -// - The NimBLE "task" (e.g. PENDSV on STM32, pthread on Unix). -// On STM32, by disabling PENDSV, we ensure that either: -// - If we're in the NimBLE task, we're exclusive anyway. -// - If we're in a VM thread, we can't be interrupted by the NimBLE task, or switched to another thread. -// On Unix, there's a global mutex. -// TODO: Both ports currently use MICROPY_PY_BLUETOOTH_ENTER in their implementation, -// maybe this doesn't need to be port-specific? +// Currently all NimBLE code is invoked by the scheduler so there should be no +// races, so on STM32 MICROPY_PY_BLUETOOTH_ENTER/MICROPY_PY_BLUETOOTH_EXIT are +// no-ops. However, in the future we may wish to make HCI UART processing +// happen asynchronously (e.g. on RX IRQ), so the port can implement these +// macros accordingly. uint32_t ble_npl_hw_enter_critical(void) { DEBUG_CRIT_printf("ble_npl_hw_enter_critical()\n"); - return mp_bluetooth_nimble_hci_uart_enter_critical(); + MICROPY_PY_BLUETOOTH_ENTER + return atomic_state; } -void ble_npl_hw_exit_critical(uint32_t ctx) { - DEBUG_CRIT_printf("ble_npl_hw_exit_critical(%u)\n", (uint)ctx); - mp_bluetooth_nimble_hci_uart_exit_critical(ctx); +void ble_npl_hw_exit_critical(uint32_t atomic_state) { + MICROPY_PY_BLUETOOTH_EXIT + DEBUG_CRIT_printf("ble_npl_hw_exit_critical(%u)\n", (uint)atomic_state); } diff --git a/extmod/nimble/nimble/nimble_npl_os.h b/extmod/nimble/nimble/nimble_npl_os.h index 3ef07aa9ccd12..bfabe56e895c0 100644 --- a/extmod/nimble/nimble/nimble_npl_os.h +++ b/extmod/nimble/nimble/nimble_npl_os.h @@ -42,6 +42,7 @@ typedef int32_t ble_npl_stime_t; struct ble_npl_event { ble_npl_event_fn *fn; void *arg; + bool pending; struct ble_npl_event *prev; struct ble_npl_event *next; }; @@ -61,7 +62,6 @@ struct ble_npl_callout { struct ble_npl_mutex { volatile uint8_t locked; - volatile uint32_t atomic_state; }; struct ble_npl_sem { @@ -76,7 +76,5 @@ void mp_bluetooth_nimble_os_callout_process(void); // --- Must be provided by the MicroPython port ------------------------------- void mp_bluetooth_nimble_hci_uart_wfi(void); -uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void); -void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state); #endif // MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H diff --git a/ports/stm32/main.c b/ports/stm32/main.c index db8222479b0f6..d00c2ec713f61 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -433,8 +433,8 @@ void stm32_main(uint32_t reset_mode) { systick_enable_dispatch(SYSTICK_DISPATCH_LWIP, mod_network_lwip_poll_wrapper); #endif #if MICROPY_PY_BLUETOOTH - extern void mp_bluetooth_hci_poll_wrapper(uint32_t ticks_ms); - systick_enable_dispatch(SYSTICK_DISPATCH_BLUETOOTH_HCI, mp_bluetooth_hci_poll_wrapper); + extern void mp_bluetooth_hci_systick(uint32_t ticks_ms); + systick_enable_dispatch(SYSTICK_DISPATCH_BLUETOOTH_HCI, mp_bluetooth_hci_systick); #endif #if MICROPY_PY_NETWORK_CYW43 diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index a5977ff12c210..ee9f4e31eb2ba 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -27,6 +27,7 @@ #include "py/runtime.h" #include "py/mphal.h" #include "extmod/mpbthci.h" +#include "extmod/modbluetooth.h" #include "systick.h" #include "pendsv.h" #include "lib/utils/mpirq.h" @@ -35,23 +36,58 @@ #if MICROPY_PY_BLUETOOTH -#define DEBUG_printf(...) // printf(__VA_ARGS__) +#define DEBUG_printf(...) // printf("mpbthciport.c: " __VA_ARGS__) uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; // Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c). +// Request new data from the uart and pass to the stack, and run pending events/callouts. extern void mp_bluetooth_hci_poll(void); // Hook for pendsv poller to run this periodically every 128ms #define BLUETOOTH_HCI_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & 0x7f) == 0) +#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + +// For synchronous mode, we run all BLE stack code inside a scheduled task. +// This task is scheduled periodically (every 128ms) via SysTick, or +// immediately on HCI UART RXIDLE. + +// Prevent double-enqueuing of the scheduled task. +STATIC volatile bool events_task_is_scheduled = false; + +STATIC mp_obj_t run_events_scheduled_task(mp_obj_t none_in) { + (void)none_in; + events_task_is_scheduled = false; + // This will process all buffered HCI UART data, and run any callouts or events. + mp_bluetooth_hci_poll(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(run_events_scheduled_task_obj, run_events_scheduled_task); + +// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to +// request that processing happens ASAP in the scheduler. +void mp_bluetooth_hci_systick(uint32_t ticks_ms) { + if (events_task_is_scheduled) { + return; + } + + if (ticks_ms == 0 || BLUETOOTH_HCI_TICK(ticks_ms)) { + events_task_is_scheduled = mp_sched_schedule(MP_OBJ_FROM_PTR(&run_events_scheduled_task_obj), mp_const_none); + } +} + +#else // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + // Called periodically (systick) or directly (e.g. uart irq). -void mp_bluetooth_hci_poll_wrapper(uint32_t ticks_ms) { +void mp_bluetooth_hci_systick(uint32_t ticks_ms) { if (ticks_ms == 0 || BLUETOOTH_HCI_TICK(ticks_ms)) { pendsv_schedule_dispatch(PENDSV_DISPATCH_BLUETOOTH_HCI, mp_bluetooth_hci_poll); } } +#endif + #if defined(STM32WB) /******************************************************************************/ @@ -67,13 +103,23 @@ STATIC uint8_t hci_uart_rx_buf_data[256]; int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { (void)port; (void)baudrate; + + DEBUG_printf("mp_bluetooth_hci_uart_init (stm32 rfcore)\n"); + + #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + events_task_is_scheduled = false; + #endif + rfcore_ble_init(); hci_uart_rx_buf_cur = 0; hci_uart_rx_buf_len = 0; + return 0; } int mp_bluetooth_hci_uart_deinit(void) { + DEBUG_printf("mp_bluetooth_hci_uart_deinit (stm32 rfcore)\n"); + return 0; } @@ -125,12 +171,12 @@ int mp_bluetooth_hci_uart_readchar(void) { pyb_uart_obj_t mp_bluetooth_hci_uart_obj; mp_irq_obj_t mp_bluetooth_hci_uart_irq_obj; -static uint8_t hci_uart_rxbuf[512]; +static uint8_t hci_uart_rxbuf[768]; mp_obj_t mp_uart_interrupt(mp_obj_t self_in) { - // DEBUG_printf("mp_uart_interrupt\n"); - // New HCI data, schedule mp_bluetooth_hci_poll via PENDSV to make the stack handle it. - mp_bluetooth_hci_poll_wrapper(0); + // Queue up the scheduler to run the HCI UART and event processing ASAP. + mp_bluetooth_hci_systick(0); + return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_interrupt_obj, mp_uart_interrupt); @@ -138,6 +184,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_interrupt_obj, mp_uart_interrupt); int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { DEBUG_printf("mp_bluetooth_hci_uart_init (stm32)\n"); + #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + events_task_is_scheduled = false; + #endif + // bits (8), stop (1), parity (none) and flow (rts/cts) are assumed to match MYNEWT_VAL_BLE_HCI_UART_ constants in syscfg.h. mp_bluetooth_hci_uart_obj.base.type = &pyb_uart_type; mp_bluetooth_hci_uart_obj.uart_id = port; @@ -147,7 +197,7 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { mp_bluetooth_hci_uart_obj.timeout_char = 200; MP_STATE_PORT(pyb_uart_obj_all)[mp_bluetooth_hci_uart_obj.uart_id - 1] = &mp_bluetooth_hci_uart_obj; - // This also initialises the UART. + // This also initialises the UART and adds the RXIDLE IRQ handler. mp_bluetooth_hci_uart_set_baudrate(baudrate); return 0; @@ -155,6 +205,7 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { int mp_bluetooth_hci_uart_deinit(void) { DEBUG_printf("mp_bluetooth_hci_uart_deinit (stm32)\n"); + // TODO: deinit mp_bluetooth_hci_uart_obj return 0; diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 5f8e7ec2de1c1..b6906ef998c2e 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -420,9 +420,16 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER #define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT -// Prevent the "Bluetooth task" from running (either NimBLE or btstack). +#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +// Bluetooth code only runs in the scheduler, no locking/mutex required. +#define MICROPY_PY_BLUETOOTH_ENTER uint32_t atomic_state = 0; +#define MICROPY_PY_BLUETOOTH_EXIT (void)atomic_state; +#else +// When async events are enabled, need to prevent PendSV execution racing with +// scheduler execution. #define MICROPY_PY_BLUETOOTH_ENTER MICROPY_PY_PENDSV_ENTER #define MICROPY_PY_BLUETOOTH_EXIT MICROPY_PY_PENDSV_EXIT +#endif // We need an implementation of the log2 function which is not a macro #define MP_NEED_LOG2 (1) diff --git a/ports/stm32/mpnimbleport.c b/ports/stm32/mpnimbleport.c index 1d7c095139cde..0ba76fb277ea2 100644 --- a/ports/stm32/mpnimbleport.c +++ b/ports/stm32/mpnimbleport.c @@ -31,24 +31,29 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE +#define DEBUG_printf(...) // printf("mpnimbleport.c: " __VA_ARGS__) + #include "host/ble_hs.h" #include "nimble/nimble_npl.h" #include "extmod/mpbthci.h" +#include "extmod/modbluetooth.h" #include "extmod/nimble/modbluetooth_nimble.h" #include "extmod/nimble/hal/hal_uart.h" -// This implements the Nimble "background task". It's called at PENDSV -// priority, either every 128ms or whenever there's UART data available. -// Because it's called via PENDSV, you can implicitly consider that it -// is surrounded by MICROPY_PY_BLUETOOTH_ENTER / MICROPY_PY_BLUETOOTH_EXIT. +// Get any pending data from the UART and send it to NimBLE's HCI buffers. +// Any further processing by NimBLE will be run via its event queue. void mp_bluetooth_hci_poll(void) { if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { - // Ask NimBLE to process UART data. - mp_bluetooth_nimble_hci_uart_process(); + // DEBUG_printf("mp_bluetooth_hci_poll_uart %d\n", mp_bluetooth_nimble_ble_state); - // Run pending background operations and events, but only after HCI sync. + // Run any timers. mp_bluetooth_nimble_os_callout_process(); + + // Process incoming UART data, and run events as they are generated. + mp_bluetooth_nimble_hci_uart_process(true); + + // Run any remaining events (e.g. if there was no UART data). mp_bluetooth_nimble_os_eventq_run_all(); } } @@ -57,15 +62,10 @@ void mp_bluetooth_hci_poll(void) { void mp_bluetooth_nimble_hci_uart_wfi(void) { __WFI(); -} - -uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) { - MICROPY_PY_BLUETOOTH_ENTER - return atomic_state; -} -void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) { - MICROPY_PY_BLUETOOTH_EXIT + // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK. + // Do not need to run events here (it must not invoke Python code), only processing incoming HCI data. + mp_bluetooth_nimble_hci_uart_process(false); } #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/ports/stm32/pendsv.h b/ports/stm32/pendsv.h index 585f81e8bd441..aa8f90e3e428f 100644 --- a/ports/stm32/pendsv.h +++ b/ports/stm32/pendsv.h @@ -34,7 +34,7 @@ enum { PENDSV_DISPATCH_CYW43, #endif #endif - #if MICROPY_PY_BLUETOOTH + #if MICROPY_PY_BLUETOOTH && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS PENDSV_DISPATCH_BLUETOOTH_HCI, #endif PENDSV_DISPATCH_MAX diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 1fc0c9531d03b..08338fcdc25c5 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -634,9 +634,9 @@ void IPCC_C1_RX_IRQHandler(void) { LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); - // Schedule PENDSV to process incoming HCI payload. - extern void mp_bluetooth_hci_poll_wrapper(uint32_t ticks_ms); - mp_bluetooth_hci_poll_wrapper(0); + // Queue up the scheduler to process UART data and run events. + extern void mp_bluetooth_hci_systick(uint32_t ticks_ms); + mp_bluetooth_hci_systick(0); } IRQ_EXIT(IPCC_C1_RX_IRQn); diff --git a/ports/unix/mpbthciport.c b/ports/unix/mpbthciport.c index 316a8831fe97b..14afbebcd7ec5 100644 --- a/ports/unix/mpbthciport.c +++ b/ports/unix/mpbthciport.c @@ -50,22 +50,67 @@ uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; +STATIC int uart_fd = -1; + // Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c). extern bool mp_bluetooth_hci_poll(void); -STATIC const useconds_t UART_POLL_INTERVAL_US = 1000; +#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS -STATIC int uart_fd = -1; +// For synchronous mode, we run all BLE stack code inside a scheduled task. +// This task is scheduled periodically (every 1ms) by a background thread. + +// Allows the stack to tell us that we should stop trying to schedule. +extern bool mp_bluetooth_hci_active(void); + +// Prevent double-enqueuing of the scheduled task. +STATIC volatile bool events_task_is_scheduled = false; + +STATIC mp_obj_t run_events_scheduled_task(mp_obj_t none_in) { + (void)none_in; + MICROPY_PY_BLUETOOTH_ENTER + events_task_is_scheduled = false; + MICROPY_PY_BLUETOOTH_EXIT + mp_bluetooth_hci_poll(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(run_events_scheduled_task_obj, run_events_scheduled_task); + +#endif // MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + +STATIC const useconds_t UART_POLL_INTERVAL_US = 1000; STATIC pthread_t hci_poll_thread_id; STATIC void *hci_poll_thread(void *arg) { (void)arg; + DEBUG_printf("hci_poll_thread: starting\n"); + + #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS + + events_task_is_scheduled = false; + + while (mp_bluetooth_hci_active()) { + MICROPY_PY_BLUETOOTH_ENTER + if (!events_task_is_scheduled) { + events_task_is_scheduled = mp_sched_schedule(MP_OBJ_FROM_PTR(&run_events_scheduled_task_obj), mp_const_none); + } + MICROPY_PY_BLUETOOTH_EXIT + usleep(UART_POLL_INTERVAL_US); + } + + #else + + // In asynchronous (i.e. ringbuffer) mode, we run the BLE stack directly from the thread. // This will return false when the stack is shutdown. while (mp_bluetooth_hci_poll()) { usleep(UART_POLL_INTERVAL_US); } + #endif + + DEBUG_printf("hci_poll_thread: stopped\n"); + return NULL; } @@ -122,6 +167,11 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { DEBUG_printf("mp_bluetooth_hci_uart_init (unix)\n"); + if (uart_fd != -1) { + DEBUG_printf("mp_bluetooth_hci_uart_init: already active\n"); + return 0; + } + char uart_device_name[256] = "/dev/ttyUSB0"; char *path = getenv("MICROPYBTUART"); diff --git a/ports/unix/mpnimbleport.c b/ports/unix/mpnimbleport.c index 8961910098c42..29f558f74db51 100644 --- a/ports/unix/mpnimbleport.c +++ b/ports/unix/mpnimbleport.c @@ -47,38 +47,28 @@ bool mp_bluetooth_hci_poll(void) { } if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { + // Run any timers. + mp_bluetooth_nimble_os_callout_process(); - // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time). - mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); - - // Ask NimBLE to process UART data. - mp_bluetooth_nimble_hci_uart_process(); + // Process incoming UART data, and run events as they are generated. + mp_bluetooth_nimble_hci_uart_process(true); - // Run pending background operations and events, but only after HCI sync. - mp_bluetooth_nimble_os_callout_process(); + // Run any remaining events (e.g. if there was no UART data). mp_bluetooth_nimble_os_eventq_run_all(); - - MICROPY_END_ATOMIC_SECTION(atomic_state); } return true; } -// Extra port-specific helpers. -void mp_bluetooth_nimble_hci_uart_wfi(void) { - // DEBUG_printf("mp_bluetooth_nimble_hci_uart_wfi\n"); - // TODO: this should do a select() on the uart_fd. -} - -uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) { - // DEBUG_printf("mp_bluetooth_nimble_hci_uart_enter_critical\n"); - MICROPY_PY_BLUETOOTH_ENTER - return atomic_state; // Always 0xffffffff +bool mp_bluetooth_hci_active(void) { + return mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; } -void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) { - MICROPY_PY_BLUETOOTH_EXIT - // DEBUG_printf("mp_bluetooth_nimble_hci_uart_exit_critical\n"); +// Extra port-specific helpers. +void mp_bluetooth_nimble_hci_uart_wfi(void) { + // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK. + // Do not need to run events here, only processing incoming HCI data. + mp_bluetooth_nimble_hci_uart_process(false); } #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE From 7e75245d549d01ac4f01ee615c193c00cf3269a7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 12 Nov 2020 23:05:47 +1100 Subject: [PATCH 0411/3533] tests/multi_bluetooth: Change dict index-and-del to pop, to clear event. Signed-off-by: Jim Mussared --- tests/multi_bluetooth/ble_characteristic.py | 4 +--- tests/multi_bluetooth/ble_gap_connect.py | 4 +--- tests/multi_bluetooth/ble_gap_device_name.py | 4 +--- tests/multi_bluetooth/ble_gatt_data_transfer.py | 4 +--- tests/multi_bluetooth/ble_gattc_discover_services.py | 4 +--- tests/multi_bluetooth/ble_mtu.py | 4 +--- 6 files changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/multi_bluetooth/ble_characteristic.py b/tests/multi_bluetooth/ble_characteristic.py index 0f22daff88f0a..327822a01ee15 100644 --- a/tests/multi_bluetooth/ble_characteristic.py +++ b/tests/multi_bluetooth/ble_characteristic.py @@ -77,9 +77,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) diff --git a/tests/multi_bluetooth/ble_gap_connect.py b/tests/multi_bluetooth/ble_gap_connect.py index 2c1d2cbbc55dc..95d1be7baa2e4 100644 --- a/tests/multi_bluetooth/ble_gap_connect.py +++ b/tests/multi_bluetooth/ble_gap_connect.py @@ -33,9 +33,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) diff --git a/tests/multi_bluetooth/ble_gap_device_name.py b/tests/multi_bluetooth/ble_gap_device_name.py index e7202170bd8a4..556068114bbbc 100644 --- a/tests/multi_bluetooth/ble_gap_device_name.py +++ b/tests/multi_bluetooth/ble_gap_device_name.py @@ -49,9 +49,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) diff --git a/tests/multi_bluetooth/ble_gatt_data_transfer.py b/tests/multi_bluetooth/ble_gatt_data_transfer.py index e8249b3da8962..861cb49fc34f2 100644 --- a/tests/multi_bluetooth/ble_gatt_data_transfer.py +++ b/tests/multi_bluetooth/ble_gatt_data_transfer.py @@ -76,9 +76,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) diff --git a/tests/multi_bluetooth/ble_gattc_discover_services.py b/tests/multi_bluetooth/ble_gattc_discover_services.py index 00bb94bba0ba8..f6ee5fa00e1fa 100644 --- a/tests/multi_bluetooth/ble_gattc_discover_services.py +++ b/tests/multi_bluetooth/ble_gattc_discover_services.py @@ -55,9 +55,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) diff --git a/tests/multi_bluetooth/ble_mtu.py b/tests/multi_bluetooth/ble_mtu.py index 73c77e2460c65..f202ec764de9b 100644 --- a/tests/multi_bluetooth/ble_mtu.py +++ b/tests/multi_bluetooth/ble_mtu.py @@ -87,9 +87,7 @@ def wait_for_event(event, timeout_ms): t0 = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: if event in waiting_events: - result = waiting_events[event] - del waiting_events[event] - return result + return waiting_events.pop(event) machine.idle() raise ValueError("Timeout waiting for {}".format(event)) From efc0800132f7c85a55718ba71f5d2729d244f85f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 4 Nov 2020 12:54:46 +1100 Subject: [PATCH 0412/3533] tests/multi_bluetooth: Add a test for WB55 concurrent flash access. This test currently passes on Unix/PYBD, but fails on WB55 because it lacks synchronisation of the internal flash. Signed-off-by: Jim Mussared --- .../multi_bluetooth/stress_log_filesystem.py | 188 ++++++++++++++++++ .../stress_log_filesystem.py.exp | 84 ++++++++ 2 files changed, 272 insertions(+) create mode 100644 tests/multi_bluetooth/stress_log_filesystem.py create mode 100644 tests/multi_bluetooth/stress_log_filesystem.py.exp diff --git a/tests/multi_bluetooth/stress_log_filesystem.py b/tests/multi_bluetooth/stress_log_filesystem.py new file mode 100644 index 0000000000000..83df555ce4d8f --- /dev/null +++ b/tests/multi_bluetooth/stress_log_filesystem.py @@ -0,0 +1,188 @@ +# Test concurrency between filesystem access and BLE host. This is +# particularly relevant on STM32WB where the second core is stalled while +# flash operations are in progress. + +from micropython import const +import time, machine, bluetooth, os + +TIMEOUT_MS = 10000 + +LOG_PATH_INSTANCE0 = "stress_log_filesystem_0.log" +LOG_PATH_INSTANCE1 = "stress_log_filesystem_1.log" + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_SERVICE_RESULT = const(9) +_IRQ_GATTC_SERVICE_DONE = const(10) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_GATTC_READ_DONE = const(16) +_IRQ_GATTC_WRITE_DONE = const(17) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = ( + CHAR_UUID, + bluetooth.FLAG_READ | bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE, +) +SERVICE = ( + SERVICE_UUID, + (CHAR,), +) +SERVICES = (SERVICE,) + + +waiting_events = {} +log_file = None + + +def write_log(*args): + if log_file: + print(*args, file=log_file) + log_file.flush() + + +last_file_write = 0 + + +def periodic_log_write(): + global last_file_write + t = time.ticks_ms() + if time.ticks_diff(t, last_file_write) > 50: + write_log("tick") + last_file_write = t + + +def irq(event, data): + write_log("event", event) + + if event == _IRQ_CENTRAL_CONNECT: + print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_CENTRAL_DISCONNECT: + print("_IRQ_CENTRAL_DISCONNECT") + elif event == _IRQ_PERIPHERAL_CONNECT: + print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_DISCONNECT: + print("_IRQ_PERIPHERAL_DISCONNECT") + elif event == _IRQ_GATTC_SERVICE_RESULT: + # conn_handle, start_handle, end_handle, uuid = data + if data[-1] == SERVICE_UUID: + print("_IRQ_GATTC_SERVICE_RESULT", data[3]) + waiting_events[event] = (data[1], data[2]) + else: + return + elif event == _IRQ_GATTC_SERVICE_DONE: + print("_IRQ_GATTC_SERVICE_DONE") + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + # conn_handle, def_handle, value_handle, properties, uuid = data + if data[-1] == CHAR_UUID: + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") + elif event == _IRQ_GATTC_READ_RESULT: + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) + elif event == _IRQ_GATTC_READ_DONE: + print("_IRQ_GATTC_READ_DONE", data[-1]) + elif event == _IRQ_GATTC_WRITE_DONE: + print("_IRQ_GATTC_WRITE_DONE", data[-1]) + elif event == _IRQ_GATTS_WRITE: + print("_IRQ_GATTS_WRITE") + elif event == _IRQ_GATTS_READ_REQUEST: + print("_IRQ_GATTS_READ_REQUEST") + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + periodic_log_write() + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +# Acting in peripheral role. +def instance0(): + global log_file + log_file = open(LOG_PATH_INSTANCE0, "w") + write_log("start") + ble.active(1) + ble.irq(irq) + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services(SERVICES) + multitest.next() + try: + for repeat in range(2): + print("gap_advertise") + ble.gap_advertise(50_000, b"\x02\x01\x06\x04\xffMPY") + # Wait for central to connect, do a sequence of read/write, then disconnect. + wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + for op in range(4): + wait_for_event(_IRQ_GATTS_READ_REQUEST, TIMEOUT_MS) + wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS) + wait_for_event(_IRQ_CENTRAL_DISCONNECT, 2 * TIMEOUT_MS) + finally: + ble.active(0) + log_file.close() + os.unlink(LOG_PATH_INSTANCE0) + + +# Acting in central role. +def instance1(): + global log_file + log_file = open(LOG_PATH_INSTANCE1, "w") + write_log("start") + ble.active(1) + ble.irq(irq) + multitest.next() + try: + for repeat in range(2): + # Connect to peripheral and then disconnect. + print("gap_connect") + ble.gap_connect(BDADDR[0], BDADDR[1], 5000) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + # Discover services. + print("gattc_discover_services") + ble.gattc_discover_services(conn_handle) + start_handle, end_handle = wait_for_event(_IRQ_GATTC_SERVICE_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_SERVICE_DONE, TIMEOUT_MS) + + # Discover characteristics. + print("gattc_discover_characteristics") + ble.gattc_discover_characteristics(conn_handle, start_handle, end_handle) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + for op in range(4): + print("gattc_read") + ble.gattc_read(conn_handle, value_handle) + wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_READ_DONE, TIMEOUT_MS) + print("gattc_write") + ble.gattc_write(conn_handle, value_handle, "{}".format(op), 1) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) + + # Disconnect. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, 2 * TIMEOUT_MS) + finally: + ble.active(0) + log_file.close() + os.unlink(LOG_PATH_INSTANCE1) + + +ble = bluetooth.BLE() diff --git a/tests/multi_bluetooth/stress_log_filesystem.py.exp b/tests/multi_bluetooth/stress_log_filesystem.py.exp new file mode 100644 index 0000000000000..8e1144b78748e --- /dev/null +++ b/tests/multi_bluetooth/stress_log_filesystem.py.exp @@ -0,0 +1,84 @@ +--- instance0 --- +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_CENTRAL_DISCONNECT +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_GATTS_READ_REQUEST +_IRQ_GATTS_WRITE +_IRQ_CENTRAL_DISCONNECT +--- instance1 --- +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_discover_services +_IRQ_GATTC_SERVICE_RESULT UUID('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a') +_IRQ_GATTC_SERVICE_DONE +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_read +_IRQ_GATTC_READ_RESULT b'' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'0' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'1' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'2' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT +gap_connect +_IRQ_PERIPHERAL_CONNECT +gattc_discover_services +_IRQ_GATTC_SERVICE_RESULT UUID('a5a5a5a5-ffff-9999-1111-5a5a5a5a5a5a') +_IRQ_GATTC_SERVICE_DONE +gattc_discover_characteristics +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gattc_read +_IRQ_GATTC_READ_RESULT b'3' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'0' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'1' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'2' +_IRQ_GATTC_READ_DONE 0 +gattc_write +_IRQ_GATTC_WRITE_DONE 0 +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT From a64121b0d48ccd2d7212b6ff996d730eb85bfaad Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 4 Nov 2020 14:37:16 +1100 Subject: [PATCH 0413/3533] stm32/rfcore: Make RX IRQ schedule the NimBLE handler. This commit switches the STM32WB HCI interface (between the two CPUs) to require the use of MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS, and as a consequence to require NimBLE. IPCC RX IRQs now schedule the NimBLE handler to run via mp_sched_schedule. Signed-off-by: Jim Mussared --- ports/stm32/rfcore.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 08338fcdc25c5..550e5323e8526 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -31,6 +31,7 @@ #include "py/mperrno.h" #include "py/mphal.h" #include "py/runtime.h" +#include "extmod/modbluetooth.h" #include "rtc.h" #include "rfcore.h" @@ -40,6 +41,10 @@ #define DEBUG_printf(...) // printf("rfcore: " __VA_ARGS__) +#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +#error "STM32WB must use synchronous events in the BLE implementation." +#endif + // Define to 1 to print traces of HCI packets #define HCI_TRACE (0) @@ -191,9 +196,6 @@ STATIC uint8_t ipcc_membuf_ble_cs_buf[272]; // mem2 STATIC tl_list_node_t ipcc_mem_ble_evt_queue; // mem1 STATIC uint8_t ipcc_membuf_ble_hci_acl_data_buf[272]; // mem2 -// Set by the RX IRQ handler on incoming HCI payload. -STATIC volatile bool had_ble_irq = false; - /******************************************************************************/ // Transport layer linked list @@ -408,6 +410,7 @@ STATIC void tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, parse } } +// Only call this when IRQs are disabled on this channel. STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, ch)) { tl_process_msg(head, ch, parse); @@ -417,14 +420,6 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h } } -STATIC void tl_check_msg_ble(volatile tl_list_node_t *head, parse_hci_info_t *parse) { - if (had_ble_irq) { - tl_process_msg(head, IPCC_CH_BLE, parse); - - had_ble_irq = false; - } -} - STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opcode, const uint8_t *buf, size_t len) { tl_list_node_t *n = (tl_list_node_t *)cmd; n->next = n; @@ -472,7 +467,7 @@ STATIC ssize_t tl_sys_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t l STATIC int tl_ble_wait_resp(void) { uint32_t t0 = mp_hal_ticks_ms(); - while (!had_ble_irq) { + while (!LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { if (mp_hal_ticks_ms() - t0 > BLE_ACK_TIMEOUT_MS) { printf("tl_ble_wait_resp: timeout\n"); return -MP_ETIMEDOUT; @@ -480,14 +475,16 @@ STATIC int tl_ble_wait_resp(void) { } // C2 set IPCC flag. - tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); + tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); return 0; } // Synchronously send a BLE command. STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) { + LL_C1_IPCC_DisableReceiveChannel(IPCC, IPCC_CH_BLE); tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, buf, len); tl_ble_wait_resp(); + LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE); } /******************************************************************************/ @@ -554,11 +551,10 @@ static const struct { void rfcore_ble_init(void) { DEBUG_printf("rfcore_ble_init\n"); - // Clear any outstanding messages from ipcc_init + // Clear any outstanding messages from ipcc_init. tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL); - tl_check_msg_ble(&ipcc_mem_ble_evt_queue, NULL); - // Configure and reset the BLE controller + // Configure and reset the BLE controller. tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params), 0); tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0); } @@ -597,7 +593,7 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env) { parse_hci_info_t parse = { cb, env, false }; - tl_check_msg_ble(&ipcc_mem_ble_evt_queue, &parse); + tl_process_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, &parse); // Intercept HCI_Reset events and reconfigure the controller following the reset if (parse.was_hci_reset_evt) { @@ -629,9 +625,9 @@ void IPCC_C1_TX_IRQHandler(void) { void IPCC_C1_RX_IRQHandler(void) { IRQ_ENTER(IPCC_C1_RX_IRQn); - if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { - had_ble_irq = true; + DEBUG_printf("IPCC_C1_RX_IRQHandler\n"); + if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); // Queue up the scheduler to process UART data and run events. From 119c88ef17c390e43828b825c3df99ffc1dc39b9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 4 Nov 2020 15:29:31 +1100 Subject: [PATCH 0414/3533] stm32/flash: Implement WB55 flash locking. This is needed to moderate concurrent access to the internal flash, as while an erase/write is in progress execution will stall on the wireless core due to the bus being locked. This implements Figure 10 from AN5289 Rev 3. Signed-off-by: Jim Mussared --- ports/stm32/flash.c | 93 ++++++++++++++++++++++++++++++ ports/stm32/mboot/Makefile | 1 + ports/stm32/mpconfigboard_common.h | 4 ++ ports/stm32/rfcore.c | 24 ++++++-- ports/stm32/rfcore.h | 3 + 5 files changed, 121 insertions(+), 4 deletions(-) diff --git a/ports/stm32/flash.c b/ports/stm32/flash.c index 499129a6f3e5d..d399ece866e99 100644 --- a/ports/stm32/flash.c +++ b/ports/stm32/flash.c @@ -29,6 +29,21 @@ #include "py/mphal.h" #include "flash.h" +#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION +// See WB55 specific documentation in AN5289 Rev 3, and in particular, Figure 10. + +#include "rfcore.h" +#include "stm32wbxx_ll_hsem.h" + +// Protects all flash registers. +#define SEMID_FLASH_REGISTERS (2) +// Used by CPU1 to prevent CPU2 from writing/erasing data in flash memory. +#define SEMID_FLASH_CPU1 (6) +// Used by CPU2 to prevent CPU1 from writing/erasing data in flash memory. +#define SEMID_FLASH_CPU2 (7) + +#endif + typedef struct { uint32_t base_address; uint32_t sector_size; @@ -181,9 +196,27 @@ int flash_erase(uint32_t flash_dest, uint32_t num_word32) { return 0; } + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Acquire lock on the flash peripheral. + while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_REGISTERS)) { + } + #endif + // Unlock the flash for erase. HAL_FLASH_Unlock(); + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Tell the HCI controller stack we're starting an erase, so it + // avoids radio activity for a while. + rfcore_start_flash_erase(); + // Wait for PES. + while (LL_FLASH_IsActiveFlag_OperationSuspended()) { + } + // Wait for flash lock. + while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) { + } + #endif + // Clear pending flags (if any) and set up EraseInitStruct. FLASH_EraseInitTypeDef EraseInitStruct; @@ -233,9 +266,23 @@ int flash_erase(uint32_t flash_dest, uint32_t num_word32) { uint32_t SectorError = 0; HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError); + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Release flash lock. + while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) { + } + LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0); + // Tell HCI controller that erase is over. + rfcore_end_flash_erase(); + #endif + // Lock the flash after erase. HAL_FLASH_Lock(); + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Release lock on the flash peripheral. + LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_REGISTERS, 0); + #endif + return mp_hal_status_to_neg_errno(status); } @@ -269,9 +316,21 @@ void flash_erase_it(uint32_t flash_dest, uint32_t num_word32) { */ int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Acquire lock on the flash peripheral. + while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_REGISTERS)) { + } + #endif + // Unlock the flash for write. HAL_FLASH_Unlock(); + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Wait for PES. + while (LL_FLASH_IsActiveFlag_OperationSuspended()) { + } + #endif + HAL_StatusTypeDef status = HAL_OK; #if defined(STM32L4) || defined(STM32WB) @@ -279,7 +338,22 @@ int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { // program the flash uint64 by uint64 for (int i = 0; i < num_word32 / 2; i++) { uint64_t val = *(uint64_t *)src; + + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Wait for flash lock. + while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) { + } + #endif + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val); + + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Release flash lock. + LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0); + while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) { + } + #endif + if (status != HAL_OK) { num_word32 = 0; // don't write any odd word after this loop break; @@ -290,7 +364,21 @@ int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { if ((num_word32 & 0x01) == 1) { uint64_t val = *(uint64_t *)flash_dest; val = (val & 0xffffffff00000000uL) | (*src); + + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Wait for flash lock. + while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) { + } + #endif + status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val); + + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Release flash lock. + LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0); + while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) { + } + #endif } #elif defined(STM32H7) @@ -322,6 +410,11 @@ int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) { // Lock the flash after write. HAL_FLASH_Lock(); + #if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION + // Release lock on the flash peripheral. + LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_REGISTERS, 0); + #endif + return mp_hal_status_to_neg_errno(status); } diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index c901dfb3344a4..3d041e1c141ea 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -73,6 +73,7 @@ CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT CFLAGS += -DBUILDING_MBOOT=1 +CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) LDFLAGS = -nostdlib -L . -T stm32_generic.ld -Map=$(@:.elf=.map) --cref diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 127696b19374a..a73a26b1627b4 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -216,6 +216,10 @@ #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (1) +#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION +#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1) +#endif + #else #error Unsupported MCU series #endif diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 550e5323e8526..6133720958093 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -57,10 +57,12 @@ #define OCF_CB_RESET (0x03) #define OCF_CB_SET_EVENT_MASK2 (0x63) -#define OGF_VENDOR (0x3f) -#define OCF_WRITE_CONFIG (0x0c) -#define OCF_SET_TX_POWER (0x0f) -#define OCF_BLE_INIT (0x66) +#define OGF_VENDOR (0x3f) +#define OCF_WRITE_CONFIG (0x0c) +#define OCF_SET_TX_POWER (0x0f) +#define OCF_BLE_INIT (0x66) +#define OCF_C2_FLASH_ERASE_ACTIVITY (0x69) +#define OCF_C2_SET_FLASH_ACTIVITY_CONTROL (0x73) #define HCI_OPCODE(ogf, ocf) ((ogf) << 10 | (ocf)) @@ -557,6 +559,10 @@ void rfcore_ble_init(void) { // Configure and reset the BLE controller. tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params), 0); tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0); + + // Enable PES rather than SEM7 to moderate flash access between the cores. + uint8_t buf = 0; // FLASH_ACTIVITY_CONTROL_PES + tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_C2_SET_FLASH_ACTIVITY_CONTROL), &buf, 1, 0); } void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { @@ -616,6 +622,16 @@ void rfcore_ble_set_txpower(uint8_t level) { tl_ble_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_SET_TX_POWER), buf, 2); } +void rfcore_start_flash_erase(void) { + uint8_t buf = 1; // ERASE_ACTIVITY_ON + tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_C2_FLASH_ERASE_ACTIVITY), &buf, 1, 0); +} + +void rfcore_end_flash_erase(void) { + uint8_t buf = 0; // ERASE_ACTIVITY_OFF + tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_C2_FLASH_ERASE_ACTIVITY), &buf, 1, 0); +} + // IPCC IRQ Handlers void IPCC_C1_TX_IRQHandler(void) { IRQ_ENTER(IPCC_C1_TX_IRQn); diff --git a/ports/stm32/rfcore.h b/ports/stm32/rfcore.h index 6a3c85f67dfe8..fe29ac612ccc6 100644 --- a/ports/stm32/rfcore.h +++ b/ports/stm32/rfcore.h @@ -35,6 +35,9 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src); void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env); void rfcore_ble_set_txpower(uint8_t level); +void rfcore_start_flash_erase(void); +void rfcore_end_flash_erase(void); + MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj); MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj); From 21c293fbcd1f9d5bafac300b5da265b4efd2be52 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 10 Nov 2020 12:55:24 +1100 Subject: [PATCH 0415/3533] stm32/rfcore: Don't send HCI ACL cmds while another is pending. And, for TX, the next/prev entries ane unused so set them to NULL to indicate this. Signed-off-by: Jim Mussared --- ports/stm32/rfcore.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 6133720958093..da77be6117d38 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -32,6 +32,7 @@ #include "py/mphal.h" #include "py/runtime.h" #include "extmod/modbluetooth.h" +#include "nimble/nimble_npl.h" #include "rtc.h" #include "rfcore.h" @@ -72,8 +73,9 @@ #define HCI_KIND_VENDOR_RESPONSE (0x11) #define HCI_KIND_VENDOR_EVENT (0x12) -#define HCI_EVENT_COMMAND_COMPLETE (0x0E) // -#define HCI_EVENT_COMMAND_STATUS (0x0F) // +#define HCI_EVENT_COMMAND_COMPLETE (0x0E) // +#define HCI_EVENT_COMMAND_STATUS (0x0F) // +#define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS (0x13) // ()* #define SYS_ACK_TIMEOUT_MS (250) #define BLE_ACK_TIMEOUT_MS (250) @@ -83,6 +85,8 @@ // AN5289 #define MAGIC_IPCC_MEM_INCORRECT 0x3DE96F61 +volatile bool hci_acl_cmd_pending = false; + typedef struct _tl_list_node_t { volatile struct _tl_list_node_t *next; volatile struct _tl_list_node_t *prev; @@ -305,6 +309,11 @@ STATIC size_t tl_parse_hci_msg(const uint8_t *buf, parse_hci_info_t *parse) { case HCI_KIND_BT_EVENT: { info = "HCI_EVT"; + // Acknowledgment of a pending ACL request, allow another one to be sent. + if (buf[1] == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) { + hci_acl_cmd_pending = false; + } + len = 3 + buf[2]; if (parse != NULL) { @@ -424,8 +433,8 @@ STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_h STATIC void tl_hci_cmd(uint8_t *cmd, unsigned int ch, uint8_t hdr, uint16_t opcode, const uint8_t *buf, size_t len) { tl_list_node_t *n = (tl_list_node_t *)cmd; - n->next = n; - n->prev = n; + n->next = NULL; + n->prev = NULL; cmd[8] = hdr; cmd[9] = opcode; cmd[10] = opcode >> 8; @@ -584,13 +593,25 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { } else if (src[0] == HCI_KIND_BT_ACL) { n = (tl_list_node_t *)&ipcc_membuf_ble_hci_acl_data_buf[0]; ch = IPCC_CH_HCI_ACL; + + // Give the previous ACL command up to 100ms to complete. + mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms(); + while (hci_acl_cmd_pending) { + if (mp_hal_ticks_ms() - timeout_start_ticks_ms > 100) { + break; + } + mp_bluetooth_nimble_hci_uart_wfi(); + } + + // Prevent sending another command until this one returns with HCI_EVENT_COMMAND_{COMPLETE,STATUS}. + hci_acl_cmd_pending = true; } else { printf("** UNEXPECTED HCI HDR: 0x%02x **\n", src[0]); return; } - n->next = n; - n->prev = n; + n->next = NULL; + n->prev = NULL; memcpy(n->body, src, len); // IPCC indicate. From 240b3de8bc0c0bf5ad04b92e4faae85c0f3a929c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 18 Nov 2020 13:32:41 +1100 Subject: [PATCH 0416/3533] stm32/rfcore: Depend on NimBLE only when BLE enabled. This fixes the build for non-STM32WB based boards when the NimBLE submodule has not been fetched, and also allows STM32WB boards to build with BLE disabled. Signed-off-by: Jim Mussared --- ports/stm32/rfcore.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index da77be6117d38..7d0a8520a6fc4 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -32,7 +32,6 @@ #include "py/mphal.h" #include "py/runtime.h" #include "extmod/modbluetooth.h" -#include "nimble/nimble_npl.h" #include "rtc.h" #include "rfcore.h" @@ -40,12 +39,23 @@ #include "stm32wbxx_ll_ipcc.h" -#define DEBUG_printf(...) // printf("rfcore: " __VA_ARGS__) +#if MICROPY_PY_BLUETOOTH + +#if MICROPY_BLUETOOTH_NIMBLE +// For mp_bluetooth_nimble_hci_uart_wfi +#include "nimble/nimble_npl.h" +#else +#error "STM32WB must use NimBLE." +#endif #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS -#error "STM32WB must use synchronous events in the BLE implementation." +#error "STM32WB must use synchronous BLE events." #endif +#endif + +#define DEBUG_printf(...) // printf("rfcore: " __VA_ARGS__) + // Define to 1 to print traces of HCI packets #define HCI_TRACE (0) @@ -600,7 +610,9 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { if (mp_hal_ticks_ms() - timeout_start_ticks_ms > 100) { break; } + #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE mp_bluetooth_nimble_hci_uart_wfi(); + #endif } // Prevent sending another command until this one returns with HCI_EVENT_COMMAND_{COMPLETE,STATUS}. @@ -653,7 +665,9 @@ void rfcore_end_flash_erase(void) { tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_C2_FLASH_ERASE_ACTIVITY), &buf, 1, 0); } +/******************************************************************************/ // IPCC IRQ Handlers + void IPCC_C1_TX_IRQHandler(void) { IRQ_ENTER(IPCC_C1_TX_IRQn); IRQ_EXIT(IPCC_C1_TX_IRQn); @@ -667,9 +681,11 @@ void IPCC_C1_RX_IRQHandler(void) { if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); + #if MICROPY_PY_BLUETOOTH // Queue up the scheduler to process UART data and run events. extern void mp_bluetooth_hci_systick(uint32_t ticks_ms); mp_bluetooth_hci_systick(0); + #endif } IRQ_EXIT(IPCC_C1_RX_IRQn); From 5af3c046c7bfc1108094e53aea8b612c2ff86fd4 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 16 Nov 2020 20:51:37 +0100 Subject: [PATCH 0417/3533] esp32,esp8266: Remove "FAT" from warning message in inisetup.py. Because FAT is not any more the only filesystem used. --- ports/esp32/modules/inisetup.py | 2 +- ports/esp8266/modules/inisetup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/esp32/modules/inisetup.py b/ports/esp32/modules/inisetup.py index 9995d6bc07dd2..426a47a6b45e5 100644 --- a/ports/esp32/modules/inisetup.py +++ b/ports/esp32/modules/inisetup.py @@ -21,7 +21,7 @@ def fs_corrupted(): while 1: print( """\ -FAT filesystem appears to be corrupted. If you had important data there, you +The filesystem appears to be corrupted. If you had important data there, you may want to make a flash snapshot to try to recover it. Otherwise, perform factory reprogramming of MicroPython firmware (completely erase flash, followed by firmware programming). diff --git a/ports/esp8266/modules/inisetup.py b/ports/esp8266/modules/inisetup.py index 79576e9d62aa7..e711a57d4b609 100644 --- a/ports/esp8266/modules/inisetup.py +++ b/ports/esp8266/modules/inisetup.py @@ -30,7 +30,7 @@ def fs_corrupted(): while 1: print( """\ -The FAT filesystem starting at sector %d with size %d sectors appears to +The filesystem starting at sector %d with size %d sectors appears to be corrupted. If you had important data there, you may want to make a flash snapshot to try to recover it. Otherwise, perform factory reprogramming of MicroPython firmware (completely erase flash, followed by firmware From 3dcb551d8981bfb370bfe8f467fcde59dd7a916a Mon Sep 17 00:00:00 2001 From: JPFrancoia Date: Thu, 1 Oct 2020 09:51:58 +0200 Subject: [PATCH 0418/3533] nrf/README: Describe Pin numbering scheme for nRF52840. Clarify that the nRF52840's GPIO 1.00 to 1.15 maps to Pin(32-47) in MicroPython. --- ports/nrf/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ports/nrf/README.md b/ports/nrf/README.md index b08a034561e0c..5fceb6705f6d2 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -187,3 +187,12 @@ Other: * nRF UART application for IPhone/Android WebBluetooth mode can also be configured by editing `bluetooth_conf.h` and set `BLUETOOTH_WEBBLUETOOTH_REPL` to 1. This will alternate advertisement between Eddystone URL and regular connectable advertisement. The Eddystone URL will point the phone or PC to download [WebBluetooth REPL](https://aykevl.nl/apps/nus/) (experimental), which subsequently can be used to connect to the Bluetooth REPL from the PC or Phone browser. + + +## Pin numbering scheme for nrf52840-based boards + +Software Pins 0-31 correspond to physical pins 0.x and software Pins 32-47 +correspond to physical pins 1.x. + +Example: +`Pin(47)` would be 1.15 on the PCA10059 From 64180f0742a1926162b784c68bafa49b9b58596c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 18 Nov 2020 14:50:43 +1100 Subject: [PATCH 0419/3533] extmod/machine_i2c: Add init protocol method for generic I2C bindings. Hardware I2C implementations must provide a .init() protocol method if they want to support reconfiguration. Otherwise the default is that i2c.init() raises an OSError (currently the case for all ports). mp_machine_soft_i2c_locals_dict is renamed to mp_machine_i2c_locals_dict to match the generic SPI bindings. Fixes issue #6623 (where calling .init() on a HW I2C would crash). Signed-off-by: Damien George --- extmod/machine_i2c.c | 87 +++++++++++++++++++-------------- extmod/machine_i2c.h | 9 ++-- ports/esp32/machine_i2c.c | 2 +- ports/nrf/modules/machine/i2c.c | 2 +- ports/stm32/machine_i2c.c | 2 +- ports/zephyr/machine_i2c.c | 2 +- 6 files changed, 59 insertions(+), 45 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 12c9abbcbaece..44161fbbb967a 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -300,45 +300,18 @@ STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint } /******************************************************************************/ -// MicroPython bindings for I2C +// MicroPython bindings for generic machine.I2C -STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - mp_machine_soft_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "SoftI2C(scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ", freq=%u)", - mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda), 500000 / self->us_delay); -} - -STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, - { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); - self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); - self->us_timeout = args[ARG_timeout].u_int; - mp_hal_i2c_init(self, args[ARG_freq].u_int); -} - -STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // create new soft I2C object - machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t); - self->base.type = &mp_machine_soft_i2c_type; - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - machine_i2c_obj_init_helper(self, n_args, args, &kw_args); - return MP_OBJ_FROM_PTR(self); -} - -STATIC mp_obj_t machine_i2c_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { - machine_i2c_obj_init_helper(MP_OBJ_TO_PTR(args[0]), n_args - 1, args + 1, kw_args); +STATIC mp_obj_t machine_i2c_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + if (i2c_p->init == NULL) { + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); + } + i2c_p->init(self, n_args - 1, args + 1, kw_args); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_init_obj, 1, machine_i2c_obj_init); +MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_init_obj, 1, machine_i2c_init); STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) { mp_obj_base_t *self = MP_OBJ_TO_PTR(self_in); @@ -653,8 +626,45 @@ STATIC const mp_rom_map_elem_t machine_i2c_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readfrom_mem_into), MP_ROM_PTR(&machine_i2c_readfrom_mem_into_obj) }, { MP_ROM_QSTR(MP_QSTR_writeto_mem), MP_ROM_PTR(&machine_i2c_writeto_mem_obj) }, }; +MP_DEFINE_CONST_DICT(mp_machine_i2c_locals_dict, machine_i2c_locals_dict_table); -MP_DEFINE_CONST_DICT(mp_machine_soft_i2c_locals_dict, machine_i2c_locals_dict_table); +/******************************************************************************/ +// Implementation of soft I2C + +STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + mp_machine_soft_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "SoftI2C(scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ", freq=%u)", + mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda), 500000 / self->us_delay); +} + +STATIC void mp_machine_soft_i2c_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} }, + }; + + mp_machine_soft_i2c_obj_t *self = (mp_machine_soft_i2c_obj_t *)self_in; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); + self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); + self->us_timeout = args[ARG_timeout].u_int; + mp_hal_i2c_init(self, args[ARG_freq].u_int); +} + +STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + // create new soft I2C object + machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t); + self->base.type = &mp_machine_soft_i2c_type; + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + mp_machine_soft_i2c_init(&self->base, n_args, args, &kw_args); + return MP_OBJ_FROM_PTR(self); +} int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len, bool nack) { machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in; @@ -684,6 +694,7 @@ int mp_machine_soft_i2c_write(mp_obj_base_t *self_in, const uint8_t *src, size_t } STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = { + .init = mp_machine_soft_i2c_init, .start = (int (*)(mp_obj_base_t *))mp_hal_i2c_start, .stop = (int (*)(mp_obj_base_t *))mp_hal_i2c_stop, .read = mp_machine_soft_i2c_read, @@ -697,7 +708,7 @@ const mp_obj_type_t mp_machine_soft_i2c_type = { .print = mp_machine_soft_i2c_print, .make_new = mp_machine_soft_i2c_make_new, .protocol = &mp_machine_soft_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; #endif // MICROPY_PY_MACHINE_I2C diff --git a/extmod/machine_i2c.h b/extmod/machine_i2c.h index e3a87e282a0fe..9e43747323840 100644 --- a/extmod/machine_i2c.h +++ b/extmod/machine_i2c.h @@ -51,9 +51,12 @@ typedef struct _mp_machine_i2c_buf_t { } mp_machine_i2c_buf_t; // I2C protocol -// the first 4 methods can be NULL, meaning operation is not supported -// transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor +// - init must be non-NULL +// - start/stop/read/write can be NULL, meaning operation is not supported +// - transfer must be non-NULL +// - transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor typedef struct _mp_machine_i2c_p_t { + void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); int (*start)(mp_obj_base_t *obj); int (*stop)(mp_obj_base_t *obj); int (*read)(mp_obj_base_t *obj, uint8_t *dest, size_t len, bool nack); @@ -71,7 +74,7 @@ typedef struct _mp_machine_soft_i2c_obj_t { } mp_machine_soft_i2c_obj_t; extern const mp_obj_type_t mp_machine_soft_i2c_type; -extern const mp_obj_dict_t mp_machine_soft_i2c_locals_dict; +extern const mp_obj_dict_t mp_machine_i2c_locals_dict; int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags); int mp_machine_soft_i2c_transfer(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags); diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index a1d0ad0f4d93c..87b08fc9bc6e6 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -177,5 +177,5 @@ const mp_obj_type_t machine_hw_i2c_type = { .print = machine_hw_i2c_print, .make_new = machine_hw_i2c_make_new, .protocol = &machine_hw_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index ab4d516621bc7..aac9320873207 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -167,7 +167,7 @@ const mp_obj_type_t machine_hard_i2c_type = { .print = machine_hard_i2c_print, .make_new = machine_hard_i2c_make_new, .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict, + .locals_dict = (mp_obj_dict_t*)&mp_machine_i2c_locals_dict, }; #endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index e0c408c6d07c2..cfa00b86d830b 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -273,7 +273,7 @@ const mp_obj_type_t machine_hard_i2c_type = { .print = machine_hard_i2c_print, .make_new = machine_hard_i2c_make_new, .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; #endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index ec4b8620a5014..efd4bdcb21b58 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -134,5 +134,5 @@ const mp_obj_type_t machine_hard_i2c_type = { .print = machine_hard_i2c_print, .make_new = machine_hard_i2c_make_new, .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; From 0e8af2b3708ab3e499491836d481b10e030408f8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 16 Oct 2020 00:15:16 +1100 Subject: [PATCH 0420/3533] extmod/modbluetooth: Add API for L2CAP channels. Also known as L2CAP "connection oriented channels". This provides a socket-like data transfer mechanism for BLE. Currently only implemented for NimBLE on STM32 / Unix. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 129 +++++++++-- extmod/modbluetooth.h | 35 ++- extmod/nimble/modbluetooth_nimble.c | 332 +++++++++++++++++++++++++++ extmod/nimble/modbluetooth_nimble.h | 6 + extmod/nimble/nimble/nimble_npl_os.h | 13 ++ extmod/nimble/syscfg/syscfg.h | 6 +- ports/stm32/Makefile | 1 + ports/unix/Makefile | 1 + py/misc.h | 3 + 9 files changed, 504 insertions(+), 22 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index d8068df594538..2cff386f125d5 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -43,6 +43,10 @@ #error modbluetooth requires MICROPY_ENABLE_SCHEDULER #endif +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +#error l2cap channels require synchronous modbluetooth events +#endif + #define MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS 2000 #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN 5 @@ -785,6 +789,58 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_exchange_mtu_obj, bluetooth #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + +STATIC mp_obj_t bluetooth_ble_l2cap_listen(mp_obj_t self_in, mp_obj_t psm_in, mp_obj_t mtu_in) { + (void)self_in; + mp_int_t psm = mp_obj_get_int(psm_in); + mp_int_t mtu = mp_obj_get_int(mtu_in); + return bluetooth_handle_errno(mp_bluetooth_l2cap_listen(psm, mtu)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_l2cap_listen_obj, bluetooth_ble_l2cap_listen); + +STATIC mp_obj_t bluetooth_ble_l2cap_connect(size_t n_args, const mp_obj_t *args) { + mp_int_t conn_handle = mp_obj_get_int(args[1]); + mp_int_t psm = mp_obj_get_int(args[2]); + mp_int_t mtu = mp_obj_get_int(args[3]); + return bluetooth_handle_errno(mp_bluetooth_l2cap_connect(conn_handle, psm, mtu)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_connect_obj, 4, 4, bluetooth_ble_l2cap_connect); + +STATIC mp_obj_t bluetooth_ble_l2cap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t cid_in) { + (void)self_in; + mp_int_t conn_handle = mp_obj_get_int(conn_handle_in); + mp_int_t cid = mp_obj_get_int(cid_in); + return bluetooth_handle_errno(mp_bluetooth_l2cap_disconnect(conn_handle, cid)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_l2cap_disconnect_obj, bluetooth_ble_l2cap_disconnect); + +STATIC mp_obj_t bluetooth_ble_l2cap_send(size_t n_args, const mp_obj_t *args) { + mp_int_t conn_handle = mp_obj_get_int(args[1]); + mp_int_t cid = mp_obj_get_int(args[2]); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); + bool stalled = false; + bluetooth_handle_errno(mp_bluetooth_l2cap_send(conn_handle, cid, bufinfo.buf, bufinfo.len, &stalled)); + // Return True if the channel is still ready to send. + return mp_obj_new_bool(!stalled); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_send_obj, 4, 4, bluetooth_ble_l2cap_send); + +STATIC mp_obj_t bluetooth_ble_l2cap_recvinto(size_t n_args, const mp_obj_t *args) { + mp_int_t conn_handle = mp_obj_get_int(args[1]); + mp_int_t cid = mp_obj_get_int(args[2]); + mp_buffer_info_t bufinfo = {0}; + if (args[3] != mp_const_none) { + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_WRITE); + } + bluetooth_handle_errno(mp_bluetooth_l2cap_recvinto(conn_handle, cid, bufinfo.buf, &bufinfo.len)); + return MP_OBJ_NEW_SMALL_INT(bufinfo.len); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_recvinto_obj, 4, 4, bluetooth_ble_l2cap_recvinto); + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + // ---------------------------------------------------------------------------- // Bluetooth object: Definition // ---------------------------------------------------------------------------- @@ -817,6 +873,13 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gattc_write), MP_ROM_PTR(&bluetooth_ble_gattc_write_obj) }, { MP_ROM_QSTR(MP_QSTR_gattc_exchange_mtu), MP_ROM_PTR(&bluetooth_ble_gattc_exchange_mtu_obj) }, #endif + #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + { MP_ROM_QSTR(MP_QSTR_l2cap_listen), MP_ROM_PTR(&bluetooth_ble_l2cap_listen_obj) }, + { MP_ROM_QSTR(MP_QSTR_l2cap_connect), MP_ROM_PTR(&bluetooth_ble_l2cap_connect_obj) }, + { MP_ROM_QSTR(MP_QSTR_l2cap_disconnect), MP_ROM_PTR(&bluetooth_ble_l2cap_disconnect_obj) }, + { MP_ROM_QSTR(MP_QSTR_l2cap_send), MP_ROM_PTR(&bluetooth_ble_l2cap_send_obj) }, + { MP_ROM_QSTR(MP_QSTR_l2cap_recvinto), MP_ROM_PTR(&bluetooth_ble_l2cap_recvinto_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table); @@ -1051,6 +1114,37 @@ void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); } +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { + uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + // Return non-zero from IRQ handler to fail the accept. + mp_int_t ret = 0; + mp_obj_get_int_maybe(result, &ret); + return ret; +} + +void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { + uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status) { + uint16_t args[] = {conn_handle, cid, psm, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status) { + uint16_t args[] = {conn_handle, cid}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + +void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid) { + uint16_t args[] = {conn_handle, cid}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE void mp_bluetooth_gap_on_scan_complete(void) { invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_U16, 0, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); @@ -1203,6 +1297,23 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu schedule_ringbuf(atomic_state); } +bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { + (void)conn_handle; + (void)value_handle; + // This must be handled synchronously and therefore cannot implemented with the ringbuffer. + return true; +} + +void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { + MICROPY_PY_BLUETOOTH_ENTER + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (enqueue_irq(o, 2 + 2, MP_BLUETOOTH_IRQ_MTU_EXCHANGED)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, value); + } + schedule_ringbuf(atomic_state); +} + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE void mp_bluetooth_gap_on_scan_complete(void) { MICROPY_PY_BLUETOOTH_ENTER @@ -1322,26 +1433,8 @@ void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle } schedule_ringbuf(atomic_state); } - #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { - (void)conn_handle; - (void)value_handle; - // This must be handled synchronously and therefore cannot implemented with the ringbuffer. - return true; -} - -void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { - MICROPY_PY_BLUETOOTH_ENTER - mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (enqueue_irq(o, 2 + 2, MP_BLUETOOTH_IRQ_MTU_EXCHANGED)) { - ringbuf_put16(&o->ringbuf, conn_handle); - ringbuf_put16(&o->ringbuf, value); - } - schedule_ringbuf(atomic_state); -} - #endif // MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // ---------------------------------------------------------------------------- diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 52e3446ff3b94..9caddb0f3f89e 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -49,6 +49,11 @@ #define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS (0) #endif +// A port can optionally enable support for L2CAP "Connection Oriented Channels". +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (0) +#endif + // This is used to protect the ringbuffer. // A port may no-op this if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS is enabled. #ifndef MICROPY_PY_BLUETOOTH_ENTER @@ -104,6 +109,11 @@ #define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19) #define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20) #define MP_BLUETOOTH_IRQ_MTU_EXCHANGED (21) +#define MP_BLUETOOTH_IRQ_L2CAP_ACCEPT (22) +#define MP_BLUETOOTH_IRQ_L2CAP_CONNECT (23) +#define MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT (24) +#define MP_BLUETOOTH_IRQ_L2CAP_RECV (25) +#define MP_BLUETOOTH_IRQ_L2CAP_SEND_READY (26) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -136,6 +146,11 @@ _IRQ_GATTC_NOTIFY = const(18) _IRQ_GATTC_INDICATE = const(19) _IRQ_GATTS_INDICATE_DONE = const(20) _IRQ_MTU_EXCHANGED = const(21) +_IRQ_L2CAP_ACCEPT = const(22) +_IRQ_L2CAP_CONNECT = const(23) +_IRQ_L2CAP_DISCONNECT = const(24) +_IRQ_L2CAP_RECV = const(25) +_IRQ_L2CAP_SEND_READY = const(26) */ // bluetooth.UUID type. @@ -250,7 +265,15 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const // Initiate MTU exchange for a specific connection using the preferred MTU. int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle); -#endif +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +int mp_bluetooth_l2cap_listen(uint16_t psm, uint16_t mtu); +int mp_bluetooth_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu); +int mp_bluetooth_l2cap_disconnect(uint16_t conn_handle, uint16_t cid); +int mp_bluetooth_l2cap_send(uint16_t conn_handle, uint16_t cid, const uint8_t *buf, size_t len, bool *stalled); +int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf, size_t *len); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS ///////////////////////////////////////////////////////////////////////////// // API implemented by modbluetooth (called by port-specific implementations): @@ -294,7 +317,15 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u // Notify modbluetooth that a read or write operation has completed. void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status); -#endif +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); +void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); +void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status); +void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status); +void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS // For stacks that don't manage attribute value data (currently all of them), helpers // to store this in a map, keyed by value handle. diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index c961aee3262c9..fa416ebc3f529 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -42,6 +42,10 @@ #include "services/gap/ble_svc_gap.h" #include "services/gatt/ble_svc_gatt.h" +// We need the definition of "struct ble_l2cap_chan". +// See l2cap_channel_event() for details. +#include "nimble/host/src/ble_l2cap_priv.h" + #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" #endif @@ -66,6 +70,7 @@ STATIC int8_t ble_hs_err_to_errno_table[] = { [BLE_HS_ETIMEOUT] = MP_ETIMEDOUT, [BLE_HS_EDONE] = MP_EIO, // TODO: Maybe should be MP_EISCONN (connect uses this for "already connected"). [BLE_HS_EBUSY] = MP_EBUSY, + [BLE_HS_EBADDATA] = MP_EINVAL, }; STATIC int ble_hs_err_to_errno(int err) { @@ -1110,4 +1115,331 @@ int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + +// Fortunately NimBLE uses mbuf chains correctly with L2CAP COC (rather than +// accessing the mbuf internals directly), so we can use a small block size to +// avoid excessive fragmentation and rely on them chaining together for larger +// payloads. +#define L2CAP_BUF_BLOCK_SIZE (128) + +// This gives us enough room to have one MTU-size transmit buffer and two +// MTU-sized receive buffers. Note that we use the local MTU to calculate +// the buffer size. This means that if the peer MTU is larger, then +// there might not be enough space in the pool to send a full peer-MTU +// sized payload and mp_bluetooth_l2cap_send will return ENOMEM. +#define L2CAP_BUF_SIZE_MTUS_PER_CHANNEL (3) + +typedef struct _mp_bluetooth_nimble_l2cap_channel_t { + struct ble_l2cap_chan *chan; + struct os_mbuf_pool sdu_mbuf_pool; + struct os_mempool sdu_mempool; + struct os_mbuf *rx_pending; + uint16_t mtu; + os_membuf_t sdu_mem[]; +} mp_bluetooth_nimble_l2cap_channel_t; + +STATIC void destroy_l2cap_channel() { + // Only free the l2cap channel if we're the one that initiated the connection. + // Listeners continue listening on the same channel. + if (!MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_listening) { + MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_chan = NULL; + } +} + +STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { + DEBUG_printf("l2cap_channel_event: type=%d\n", event->type); + mp_bluetooth_nimble_l2cap_channel_t *chan = (mp_bluetooth_nimble_l2cap_channel_t *)arg; + struct ble_l2cap_chan_info info; + + switch (event->type) { + case BLE_L2CAP_EVENT_COC_CONNECTED: { + DEBUG_printf("l2cap_channel_event: connect: conn_handle=%d status=%d\n", event->connect.conn_handle, event->connect.status); + chan->chan = event->connect.chan; + + ble_l2cap_get_chan_info(event->connect.chan, &info); + if (event->connect.status == 0) { + mp_bluetooth_gattc_on_l2cap_connect(event->connect.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); + } else { + mp_bluetooth_gattc_on_l2cap_disconnect(event->connect.conn_handle, info.scid, info.psm, event->connect.status); + destroy_l2cap_channel(); + } + break; + } + case BLE_L2CAP_EVENT_COC_DISCONNECTED: { + DEBUG_printf("l2cap_channel_event: disconnect: conn_handle=%d\n", event->disconnect.conn_handle); + ble_l2cap_get_chan_info(event->disconnect.chan, &info); + mp_bluetooth_gattc_on_l2cap_disconnect(event->disconnect.conn_handle, info.scid, info.psm, 0); + destroy_l2cap_channel(); + break; + } + case BLE_L2CAP_EVENT_COC_ACCEPT: { + DEBUG_printf("l2cap_channel_event: accept: conn_handle=%d peer_sdu_size=%d\n", event->accept.conn_handle, event->accept.peer_sdu_size); + chan->chan = event->accept.chan; + ble_l2cap_get_chan_info(event->accept.chan, &info); + int ret = mp_bluetooth_gattc_on_l2cap_accept(event->accept.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); + if (ret != 0) { + return ret; + } + struct os_mbuf *sdu_rx = os_mbuf_get_pkthdr(&chan->sdu_mbuf_pool, 0); + assert(sdu_rx); + return ble_l2cap_recv_ready(chan->chan, sdu_rx); + } + case BLE_L2CAP_EVENT_COC_DATA_RECEIVED: { + DEBUG_printf("l2cap_channel_event: receive: conn_handle=%d len=%d\n", event->receive.conn_handle, OS_MBUF_PKTLEN(event->receive.sdu_rx)); + + if (chan->rx_pending) { + // Ideally this doesn't happen, as the sender should not get + // any more credits to send more data until we call + // ble_l2cap_recv_ready. However there might be multiple + // in-flight packets if the sender was able to send more than + // one before stalling. + DEBUG_printf("l2cap_channel_event: receive: appending to rx pending\n"); + // Note: os_mbuf_concat will just join the two together, so + // sdu_rx is now "owned" by rx_pending. + os_mbuf_concat(chan->rx_pending, event->receive.sdu_rx); + } else { + // Normal case is when the first payload arrives since calling + // ble_l2cap_recv_ready. + DEBUG_printf("l2cap_event: receive: new payload\n"); + // Take ownership of sdu_rx. + chan->rx_pending = event->receive.sdu_rx; + } + + struct os_mbuf *sdu_rx = os_mbuf_get_pkthdr(&chan->sdu_mbuf_pool, 0); + assert(sdu_rx); + + // ble_l2cap_coc_rx_fn invokes this event handler when a complete payload arrives. + // However, it NULLs chan->chan->coc_rx.sdu before doing so, expecting that + // ble_l2cap_recv_ready will be called to give it a new mbuf. + // This means that if another payload arrives before we call ble_l2cap_recv_ready + // then ble_l2cap_coc_rx_fn will NULL-deref coc_rx.sdu. + + // Because we're not yet ready to grant new credits to the channel, we can't call + // ble_l2cap_recv_ready yet, so instead we just give it a new mbuf. This requires + // ble_l2cap_priv.h for the definition of chan->chan (i.e. struct ble_l2cap_chan). + chan->chan->coc_rx.sdu = sdu_rx; + + ble_l2cap_get_chan_info(event->receive.chan, &info); + mp_bluetooth_gattc_on_l2cap_recv(event->receive.conn_handle, info.scid); + break; + } + case BLE_L2CAP_EVENT_COC_TX_UNSTALLED: { + DEBUG_printf("l2cap_channel_event: tx_unstalled: conn_handle=%d status=%d\n", event->tx_unstalled.conn_handle, event->tx_unstalled.status); + ble_l2cap_get_chan_info(event->receive.chan, &info); + // Map status to {0,1} (i.e. "sent everything", or "partial send"). + mp_bluetooth_gattc_on_l2cap_send_ready(event->tx_unstalled.conn_handle, info.scid, event->tx_unstalled.status == 0 ? 0 : 1); + break; + } + case BLE_L2CAP_EVENT_COC_RECONFIG_COMPLETED: { + DEBUG_printf("l2cap_channel_event: reconfig_completed: conn_handle=%d\n", event->reconfigured.conn_handle); + break; + } + case BLE_L2CAP_EVENT_COC_PEER_RECONFIGURED: { + DEBUG_printf("l2cap_channel_event: peer_reconfigured: conn_handle=%d\n", event->reconfigured.conn_handle); + break; + } + default: { + DEBUG_printf("l2cap_channel_event: unknown event\n"); + break; + } + } + + return 0; +} + +STATIC mp_bluetooth_nimble_l2cap_channel_t *get_l2cap_channel_for_conn_cid(uint16_t conn_handle, uint16_t cid) { + // TODO: Support more than one concurrent L2CAP channel. At the moment we + // just verify that the cid refers to the current channel. + mp_bluetooth_nimble_l2cap_channel_t *chan = MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_chan; + + if (!chan) { + return NULL; + } + + struct ble_l2cap_chan_info info; + ble_l2cap_get_chan_info(chan->chan, &info); + + if (info.scid != cid || ble_l2cap_get_conn_handle(chan->chan) != conn_handle) { + return NULL; + } + + return chan; +} + +STATIC int create_l2cap_channel(uint16_t mtu, mp_bluetooth_nimble_l2cap_channel_t **out) { + if (MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_chan) { + // Only one L2CAP channel allowed. + // Additionally, if we're listening, then no connections may be initiated. + DEBUG_printf("create_l2cap_channel: channel already in use\n"); + return MP_EALREADY; + } + + // We want the TX and RX buffers to share a pool that is some multiple of + // the MTU size. Figure out how many blocks per MTU (rounding up), then + // multiply that by the "MTUs per channel" (set to 3 above). + const size_t buf_blocks = MP_CEIL_DIVIDE(mtu, L2CAP_BUF_BLOCK_SIZE) * L2CAP_BUF_SIZE_MTUS_PER_CHANNEL; + + mp_bluetooth_nimble_l2cap_channel_t *chan = m_new_obj_var(mp_bluetooth_nimble_l2cap_channel_t, uint8_t, OS_MEMPOOL_SIZE(buf_blocks, L2CAP_BUF_BLOCK_SIZE) * sizeof(os_membuf_t)); + MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_chan = chan; + + // Will be set in BLE_L2CAP_EVENT_COC_CONNECTED or BLE_L2CAP_EVENT_COC_ACCEPT. + chan->chan = NULL; + + chan->mtu = mtu; + chan->rx_pending = NULL; + + int err = os_mempool_init(&chan->sdu_mempool, buf_blocks, L2CAP_BUF_BLOCK_SIZE, chan->sdu_mem, "l2cap_sdu_pool"); + if (err != 0) { + DEBUG_printf("mp_bluetooth_l2cap_connect: os_mempool_init failed %d\n", err); + return MP_ENOMEM; + } + + err = os_mbuf_pool_init(&chan->sdu_mbuf_pool, &chan->sdu_mempool, L2CAP_BUF_BLOCK_SIZE, buf_blocks); + if (err != 0) { + DEBUG_printf("mp_bluetooth_l2cap_connect: os_mbuf_pool_init failed %d\n", err); + return MP_ENOMEM; + } + + *out = chan; + return 0; +} + +int mp_bluetooth_l2cap_listen(uint16_t psm, uint16_t mtu) { + DEBUG_printf("mp_bluetooth_l2cap_listen: psm=%d, mtu=%d\n", psm, mtu); + + mp_bluetooth_nimble_l2cap_channel_t *chan; + int err = create_l2cap_channel(mtu, &chan); + if (err != 0) { + return err; + } + + MP_STATE_PORT(bluetooth_nimble_root_pointers)->l2cap_listening = true; + + return ble_hs_err_to_errno(ble_l2cap_create_server(psm, mtu, &l2cap_channel_event, chan)); +} + +int mp_bluetooth_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu) { + DEBUG_printf("mp_bluetooth_l2cap_connect: conn_handle=%d, psm=%d, mtu=%d\n", conn_handle, psm, mtu); + + mp_bluetooth_nimble_l2cap_channel_t *chan; + int err = create_l2cap_channel(mtu, &chan); + if (err != 0) { + return err; + } + + struct os_mbuf *sdu_rx = os_mbuf_get_pkthdr(&chan->sdu_mbuf_pool, 0); + assert(sdu_rx); + return ble_hs_err_to_errno(ble_l2cap_connect(conn_handle, psm, mtu, sdu_rx, &l2cap_channel_event, chan)); +} + +int mp_bluetooth_l2cap_disconnect(uint16_t conn_handle, uint16_t cid) { + DEBUG_printf("mp_bluetooth_l2cap_disconnect: conn_handle=%d, cid=%d\n", conn_handle, cid); + mp_bluetooth_nimble_l2cap_channel_t *chan = get_l2cap_channel_for_conn_cid(conn_handle, cid); + if (!chan) { + return MP_EINVAL; + } + return ble_hs_err_to_errno(ble_l2cap_disconnect(chan->chan)); +} + +int mp_bluetooth_l2cap_send(uint16_t conn_handle, uint16_t cid, const uint8_t *buf, size_t len, bool *stalled) { + DEBUG_printf("mp_bluetooth_l2cap_send: conn_handle=%d, cid=%d, len=%d\n", conn_handle, cid, (int)len); + + mp_bluetooth_nimble_l2cap_channel_t *chan = get_l2cap_channel_for_conn_cid(conn_handle, cid); + if (!chan) { + return MP_EINVAL; + } + + struct ble_l2cap_chan_info info; + ble_l2cap_get_chan_info(chan->chan, &info); + if (len > info.peer_coc_mtu) { + // This is verified by ble_l2cap_send anyway, but this lets us + // avoid copying a too-large buffer into an mbuf. + return MP_EINVAL; + } + + if (len > (L2CAP_BUF_SIZE_MTUS_PER_CHANNEL - 1) * info.our_coc_mtu) { + // Always ensure there's at least one local MTU of space left in the buffer + // for the RX buffer. + return MP_EINVAL; + } + + // Grab an mbuf from the pool, and append the incoming buffer to it. + struct os_mbuf *sdu_tx = os_mbuf_get_pkthdr(&chan->sdu_mbuf_pool, 0); + if (sdu_tx == NULL) { + return MP_ENOMEM; + } + int err = os_mbuf_append(sdu_tx, buf, len); + if (err) { + os_mbuf_free_chain(sdu_tx); + return MP_ENOMEM; + } + + err = ble_l2cap_send(chan->chan, sdu_tx); + if (err == BLE_HS_ESTALLED) { + // Stalled means that this one will still send but any future ones + // will fail until we receive an unstalled event. + *stalled = true; + err = 0; + } else { + *stalled = false; + } + + // Other error codes such as BLE_HS_EBUSY (we're stalled) or BLE_HS_EBADDATA (bigger than MTU). + return ble_hs_err_to_errno(err); +} + +int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf, size_t *len) { + mp_bluetooth_nimble_l2cap_channel_t *chan = get_l2cap_channel_for_conn_cid(conn_handle, cid); + if (!chan) { + return MP_EINVAL; + } + + MICROPY_PY_BLUETOOTH_ENTER + if (chan->rx_pending) { + size_t avail = OS_MBUF_PKTLEN(chan->rx_pending); + + if (buf == NULL) { + // Can use this to implement a poll - just find out how much is available. + *len = avail; + } else { + // Have dest buffer and data available. + // Figure out how much we should copy. + *len = min(*len, avail); + + // Extract the required number of bytes. + os_mbuf_copydata(chan->rx_pending, 0, *len, buf); + + if (*len == avail) { + // That's all that's available -- free this mbuf and re-enable receiving. + os_mbuf_free_chain(chan->rx_pending); + chan->rx_pending = NULL; + + // We've already given the channel a new mbuf in l2cap_channel_event above, so + // re-use that mbuf in the call to ble_l2cap_recv_ready. This will just + // give the channel more credits. + struct os_mbuf *sdu_rx = chan->chan->coc_rx.sdu; + assert(sdu_rx); + if (sdu_rx) { + ble_l2cap_recv_ready(chan->chan, sdu_rx); + } + } else { + // Trim the used bytes from the start of the mbuf. + // Positive argument means "trim this many from head". + os_mbuf_adj(chan->rx_pending, *len); + // Clean up any empty mbufs at the head. + chan->rx_pending = os_mbuf_trim_front(chan->rx_pending); + } + } + } else { + // No pending data. + *len = 0; + } + + MICROPY_PY_BLUETOOTH_EXIT + return 0; +} + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/extmod/nimble/modbluetooth_nimble.h b/extmod/nimble/modbluetooth_nimble.h index 7e401781e7f9c..9ed64368b2d5f 100644 --- a/extmod/nimble/modbluetooth_nimble.h +++ b/extmod/nimble/modbluetooth_nimble.h @@ -38,6 +38,12 @@ typedef struct _mp_bluetooth_nimble_root_pointers_t { // Pending service definitions. size_t n_services; struct ble_gatt_svc_def *services[MP_BLUETOOTH_NIMBLE_MAX_SERVICES]; + + #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + // L2CAP channels. + struct _mp_bluetooth_nimble_l2cap_channel_t *l2cap_chan; + bool l2cap_listening; + #endif } mp_bluetooth_nimble_root_pointers_t; enum { diff --git a/extmod/nimble/nimble/nimble_npl_os.h b/extmod/nimble/nimble/nimble_npl_os.h index bfabe56e895c0..d0803f7e2e81d 100644 --- a/extmod/nimble/nimble/nimble_npl_os.h +++ b/extmod/nimble/nimble/nimble_npl_os.h @@ -30,12 +30,25 @@ // This is included by nimble/nimble_npl.h -- include that rather than this file directly. #include +#include // --- Configuration of NimBLE data structures -------------------------------- +// This is used at runtime to align allocations correctly. #define BLE_NPL_OS_ALIGNMENT (sizeof(uintptr_t)) #define BLE_NPL_TIME_FOREVER (0xffffffff) +// This is used at compile time to force struct member alignment. See +// os_mempool.h for where this is used (none of these three macros are defined +// by default). +#define OS_CFG_ALIGN_4 (4) +#define OS_CFG_ALIGN_8 (8) +#if (ULONG_MAX == 0xffffffffffffffff) +#define OS_CFG_ALIGNMENT (OS_CFG_ALIGN_8) +#else +#define OS_CFG_ALIGNMENT (OS_CFG_ALIGN_4) +#endif + typedef uint32_t ble_npl_time_t; typedef int32_t ble_npl_stime_t; diff --git a/extmod/nimble/syscfg/syscfg.h b/extmod/nimble/syscfg/syscfg.h index 8a6f2338be2ab..bef6b3b9210d4 100644 --- a/extmod/nimble/syscfg/syscfg.h +++ b/extmod/nimble/syscfg/syscfg.h @@ -99,9 +99,11 @@ int nimble_sprintf(char *str, const char *fmt, ...); #define MYNEWT_VAL_BLE_HS_PHONY_HCI_ACKS (0) #define MYNEWT_VAL_BLE_HS_REQUIRE_OS (1) #define MYNEWT_VAL_BLE_HS_STOP_ON_SHUTDOWN_TIMEOUT (2000) -#define MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM (0) +#define MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM (1) +#define MYNEWT_VAL_BLE_L2CAP_COC_MPS (MYNEWT_VAL_MSYS_1_BLOCK_SIZE - 8) +#define MYNEWT_VAL_BLE_L2CAP_ENHANCED_COC (0) #define MYNEWT_VAL_BLE_L2CAP_JOIN_RX_FRAGS (1) -#define MYNEWT_VAL_BLE_L2CAP_MAX_CHANS (3*MYNEWT_VAL_BLE_MAX_CONNECTIONS) +#define MYNEWT_VAL_BLE_L2CAP_MAX_CHANS (3 * MYNEWT_VAL_BLE_MAX_CONNECTIONS) #define MYNEWT_VAL_BLE_L2CAP_RX_FRAG_TIMEOUT (30000) #define MYNEWT_VAL_BLE_L2CAP_SIG_MAX_PROCS (1) #define MYNEWT_VAL_BLE_MONITOR_CONSOLE_BUFFER_SIZE (128) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 75090a077e2bf..ced851ca38e27 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -499,6 +499,7 @@ endif endif ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 include $(TOP)/extmod/nimble/nimble.mk SRC_C += mpnimbleport.c endif diff --git a/ports/unix/Makefile b/ports/unix/Makefile index f72c05f1add78..6a936a242545c 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -182,6 +182,7 @@ else # NimBLE is enabled. GIT_SUBMODULES += lib/mynewt-nimble +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 include $(TOP)/extmod/nimble/nimble.mk endif diff --git a/py/misc.h b/py/misc.h index aac90724460cf..fe2b3b8afaa75 100644 --- a/py/misc.h +++ b/py/misc.h @@ -53,6 +53,9 @@ typedef unsigned int uint; // Static assertion macro #define MP_STATIC_ASSERT(cond) ((void)sizeof(char[1 - 2 * !(cond)])) +// Round-up integer division +#define MP_CEIL_DIVIDE(a, b) (((a) + (b) - 1) / (b)) + /** memory allocation ******************************************/ // TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element) From 3795c71271aa51902854911a20b020db6b2d274b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 17 Nov 2020 23:30:06 +1100 Subject: [PATCH 0421/3533] docs/library/ubluetooth.rst: Add docs for L2CAP channels. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 116 ++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index f94ad3a612c07..35ac13ad56fa9 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -6,8 +6,9 @@ This module provides an interface to a Bluetooth controller on a board. Currently this supports Bluetooth Low Energy (BLE) in Central, Peripheral, -Broadcaster, and Observer roles, as well as GATT Server and Client. A device -may operate in multiple roles concurrently. +Broadcaster, and Observer roles, as well as GATT Server and Client and L2CAP +connection-oriented-channels. A device may operate in multiple roles +concurrently. This API is intended to match the low-level Bluetooth protocol and provide building-blocks for higher-level abstractions such as specific device types. @@ -72,9 +73,9 @@ Configuration Increasing this allows better handling of bursty incoming data (for example scan results) and the ability to receive larger characteristic values. - - ``'mtu'``: Get/set the MTU that will be used during an MTU exchange. The + - ``'mtu'``: Get/set the MTU that will be used during a ATT MTU exchange. The resulting MTU will be the minimum of this and the remote device's MTU. - MTU exchange will not happen automatically (unless the remote device initiates + ATT MTU exchange will not happen automatically (unless the remote device initiates it), and must be manually initiated with :meth:`gattc_exchange_mtu`. Use the ``_IRQ_MTU_EXCHANGED`` event to discover the MTU for a given connection. @@ -179,8 +180,25 @@ Event Handling # Note: Status will be zero on successful acknowledgment, implementation-specific value otherwise. conn_handle, value_handle, status = data elif event == _IRQ_MTU_EXCHANGED: - # MTU exchange complete (either initiated by us or the remote device). + # ATT MTU exchange complete (either initiated by us or the remote device). conn_handle, mtu = data + elif event == _IRQ_L2CAP_ACCEPT: + # A new channel has been accepted. + # Return a non-zero integer to reject the connection, or zero (or None) to accept. + conn_handle, cid, psm, our_mtu, peer_mtu = data + elif event == _IRQ_L2CAP_CONNECT: + # A new channel is now connected (either as a result of connecting or accepting). + conn_handle, cid, psm, our_mtu, peer_mtu = data + elif event == _IRQ_L2CAP_DISCONNECT: + # Existing channel has disconnected (status is zero), or a connection attempt failed (non-zero status). + conn_handle, cid, psm, status = data + elif event == _IRQ_L2CAP_RECV: + # New data is available on the channel. Use l2cap_recvinto to read. + conn_handle, cid = data + elif event == _IRQ_L2CAP_SEND_READY: + # A previous l2cap_send that returned False has now completed and the channel is ready to send again. + # If status is non-zero, then the transmit buffer overflowed and the application should re-send the data. + conn_handle, cid, status = data The event codes are:: @@ -206,6 +224,11 @@ The event codes are:: _IRQ_GATTC_INDICATE = const(19) _IRQ_GATTS_INDICATE_DONE = const(20) _IRQ_MTU_EXCHANGED = const(21) + _IRQ_L2CAP_ACCEPT = const(22) + _IRQ_L2CAP_CONNECT = const(23) + _IRQ_L2CAP_DISCONNECT = const(24) + _IRQ_L2CAP_RECV = const(25) + _IRQ_L2CAP_SEND_READY = const(26) In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your @@ -493,6 +516,89 @@ device name from the device information service). peripheral initiating the MTU exchange. NimBLE works for both roles. +L2CAP connection-oriented-channels +---------------------------------- + + This feature allows for socket-like data exchange between two BLE devices. + Once the devices are connected via GAP, either device can listen for the + other to connect on a numeric PSM (Protocol/Service Multiplexer). + + **Note:** This is currently only supported when using the NimBLE stack on + STM32 and Unix (not ESP32). Only one L2CAP channel may be active at a given + time (i.e. you cannot connect while listening). + + Active L2CAP channels are identified by the connection handle that they were + established on and a CID (channel ID). + + Connection-oriented channels have built-in credit-based flow control. Unlike + ATT, where devices negotiate a shared MTU, both the listening and connecting + devices each set an independent MTU which limits the maximum amount of + outstanding data that the remote device can send before it is fully consumed + in :meth:`l2cap_recvinto `. + +.. method:: BLE.l2cap_listen(psm, mtu, /) + + Start listening for incoming L2CAP channel requests on the specified *psm* + with the local MTU set to *mtu*. + + When a remote device initiates a connection, the ``_IRQ_L2CAP_ACCEPT`` + event will be raised, which gives the listening server a chance to reject + the incoming connection (by returning a non-zero integer). + + Once the connection is accepted, the ``_IRQ_L2CAP_CONNECT`` event will be + raised, allowing the server to obtain the channel id (CID) and the local and + remote MTU. + + **Note:** It is not currently possible to stop listening. + +.. method:: BLE.l2cap_connect(conn_handle, psm, mtu, /) + + Connect to a listening peer on the specified *psm* with local MTU set to *mtu*. + + On successful connection, the the ``_IRQ_L2CAP_CONNECT`` event will be + raised, allowing the client to obtain the CID and the local and remote (peer) MTU. + + An unsuccessful connection will raise the ``_IRQ_L2CAP_DISCONNECT`` event + with a non-zero status. + +.. method:: BLE.l2cap_disconnect(conn_handle, cid, /) + + Disconnect an active L2CAP channel with the specified *conn_handle* and + *cid*. + +.. method:: BLE.l2cap_send(conn_handle, cid, buf, /) + + Send the specified *buf* (which must support the buffer protocol) on the + L2CAP channel identified by *conn_handle* and *cid*. + + The specified buffer cannot be larger than the remote (peer) MTU, and no + more than twice the size of the local MTU. + + This will return ``False`` if the channel is now "stalled", which means that + :meth:`l2cap_send ` must not be called again until the + ``_IRQ_L2CAP_SEND_READY`` event is received (which will happen when the + remote device grants more credits, typically after it has received and + processed the data). + +.. method:: BLE.l2cap_recvinto(conn_handle, cid, buf, /) + + Receive data from the specified *conn_handle* and *cid* into the provided + *buf* (which must support the buffer protocol, e.g. bytearray or + memoryview). + + Returns the number of bytes read from the channel. + + If *buf* is None, then returns the number of bytes available. + + **Note:** After receiving the ``_IRQ_L2CAP_RECV`` event, the application should + continue calling :meth:`l2cap_recvinto ` until no more + bytes are available in the receive buffer (typically up to the size of the + remote (peer) MTU). + + Until the receive buffer is empty, the remote device will not be granted + more channel credits and will be unable to send any more data. + + class UUID ---------- From 23fad2526db2bbc8b6b07bfd67cc6feba1770249 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 17 Nov 2020 23:30:23 +1100 Subject: [PATCH 0422/3533] tests/multi_bluetooth: Add L2CAP channels multi-test. Signed-off-by: Jim Mussared --- tests/multi_bluetooth/ble_l2cap.py | 175 +++++++++++++++++++++++++ tests/multi_bluetooth/ble_l2cap.py.exp | 23 ++++ 2 files changed, 198 insertions(+) create mode 100644 tests/multi_bluetooth/ble_l2cap.py create mode 100644 tests/multi_bluetooth/ble_l2cap.py.exp diff --git a/tests/multi_bluetooth/ble_l2cap.py b/tests/multi_bluetooth/ble_l2cap.py new file mode 100644 index 0000000000000..a26f59b3ef0f9 --- /dev/null +++ b/tests/multi_bluetooth/ble_l2cap.py @@ -0,0 +1,175 @@ +# Test L2CAP COC send/recv. + +# Sends a sequence of varying-sized payloads from central->peripheral, and +# verifies that the other device sees the same data, then does the same thing +# peripheral->central. + +from micropython import const +import time, machine, bluetooth, random + +TIMEOUT_MS = 1000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_L2CAP_ACCEPT = const(22) +_IRQ_L2CAP_CONNECT = const(23) +_IRQ_L2CAP_DISCONNECT = const(24) +_IRQ_L2CAP_RECV = const(25) +_IRQ_L2CAP_SEND_READY = const(26) + +_L2CAP_MTU = const(450) +_L2CAP_PSM = const(22) + +_PAYLOAD_LEN = const(_L2CAP_MTU - 50) +_PAYLOAD_LEN_STEP = -23 +_NUM_PAYLOADS = const(16) + +_RANDOM_SEED = 22 + + +waiting_events = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, addr_type, addr = data + print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = conn_handle + elif event == _IRQ_CENTRAL_DISCONNECT: + print("_IRQ_CENTRAL_DISCONNECT") + elif event == _IRQ_PERIPHERAL_CONNECT: + conn_handle, addr_type, addr = data + print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = conn_handle + elif event == _IRQ_PERIPHERAL_DISCONNECT: + print("_IRQ_PERIPHERAL_DISCONNECT") + elif event == _IRQ_L2CAP_ACCEPT: + conn_handle, cid, psm, our_mtu, peer_mtu = data + print("_IRQ_L2CAP_ACCEPT", psm, our_mtu, peer_mtu) + waiting_events[event] = (conn_handle, cid, psm) + elif event == _IRQ_L2CAP_CONNECT: + conn_handle, cid, psm, our_mtu, peer_mtu = data + print("_IRQ_L2CAP_CONNECT", psm, our_mtu, peer_mtu) + waiting_events[event] = (conn_handle, cid, psm, our_mtu, peer_mtu) + elif event == _IRQ_L2CAP_DISCONNECT: + conn_handle, cid, psm, status = data + print("_IRQ_L2CAP_DISCONNECT", psm, status) + elif event == _IRQ_L2CAP_RECV: + conn_handle, cid = data + elif event == _IRQ_L2CAP_SEND_READY: + conn_handle, cid, status = data + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +def send_data(ble, conn_handle, cid): + buf = bytearray(_PAYLOAD_LEN) + mv = memoryview(buf) + print("l2cap_send", _NUM_PAYLOADS, _PAYLOAD_LEN) + for i in range(_NUM_PAYLOADS): + n = _PAYLOAD_LEN + i * _PAYLOAD_LEN_STEP + for j in range(n): + buf[j] = random.randint(0, 255) + if not ble.l2cap_send(conn_handle, cid, mv[:n]): + wait_for_event(_IRQ_L2CAP_SEND_READY, TIMEOUT_MS) + + +def recv_data(ble, conn_handle, cid): + buf = bytearray(_PAYLOAD_LEN) + recv_bytes = 0 + recv_correct = 0 + expected_bytes = ( + _PAYLOAD_LEN * _NUM_PAYLOADS + _PAYLOAD_LEN_STEP * _NUM_PAYLOADS * (_NUM_PAYLOADS - 1) // 2 + ) + print("l2cap_recvinto", expected_bytes) + while recv_bytes < expected_bytes: + wait_for_event(_IRQ_L2CAP_RECV, TIMEOUT_MS) + while True: + n = ble.l2cap_recvinto(conn_handle, cid, buf) + if n == 0: + break + recv_bytes += n + for i in range(n): + if buf[i] == random.randint(0, 255): + recv_correct += 1 + return recv_bytes, recv_correct + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect to us. + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + + print("l2cap_listen") + ble.l2cap_listen(_L2CAP_PSM, _L2CAP_MTU) + + conn_handle, cid, psm = wait_for_event(_IRQ_L2CAP_ACCEPT, TIMEOUT_MS) + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) + + random.seed(_RANDOM_SEED) + + recv_bytes, recv_correct = recv_data(ble, conn_handle, cid) + send_data(ble, conn_handle, cid) + + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) + + print("received", recv_bytes, recv_correct) + + # Wait for the central to disconnect. + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + multitest.next() + try: + # Connect to peripheral and then disconnect. + print("gap_connect") + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + print("l2cap_connect") + ble.l2cap_connect(conn_handle, _L2CAP_PSM, _L2CAP_MTU) + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) + + random.seed(_RANDOM_SEED) + + send_data(ble, conn_handle, cid) + recv_bytes, recv_correct = recv_data(ble, conn_handle, cid) + + # Disconnect channel. + print("l2cap_disconnect") + ble.l2cap_disconnect(conn_handle, cid) + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) + + print("received", recv_bytes, recv_correct) + + # Disconnect from peripheral. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/ble_l2cap.py.exp b/tests/multi_bluetooth/ble_l2cap.py.exp new file mode 100644 index 0000000000000..0c572d99fefb1 --- /dev/null +++ b/tests/multi_bluetooth/ble_l2cap.py.exp @@ -0,0 +1,23 @@ +--- instance0 --- +gap_advertise +_IRQ_CENTRAL_CONNECT +l2cap_listen +_IRQ_L2CAP_ACCEPT 22 450 450 +_IRQ_L2CAP_CONNECT 22 450 450 +l2cap_recvinto 3640 +l2cap_send 16 400 +_IRQ_L2CAP_DISCONNECT 22 0 +received 3640 3640 +_IRQ_CENTRAL_DISCONNECT +--- instance1 --- +gap_connect +_IRQ_PERIPHERAL_CONNECT +l2cap_connect +_IRQ_L2CAP_CONNECT 22 450 450 +l2cap_send 16 400 +l2cap_recvinto 3640 +l2cap_disconnect +_IRQ_L2CAP_DISCONNECT 22 0 +received 3640 3640 +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT From 5a7027915c3b9ec2dffc3af9530dad90b56b8cc0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 07:56:34 +1100 Subject: [PATCH 0423/3533] extmod/nimble/modbluetooth_nimble: Fix build when l2cap unavailable. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index fa416ebc3f529..bbff26b5354e9 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -42,9 +42,11 @@ #include "services/gap/ble_svc_gap.h" #include "services/gatt/ble_svc_gatt.h" +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS // We need the definition of "struct ble_l2cap_chan". // See l2cap_channel_event() for details. #include "nimble/host/src/ble_l2cap_priv.h" +#endif #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" From 6a3d70db9642e29b017f111abaf7fb6238a9b5a6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 28 Nov 2020 12:21:40 +1100 Subject: [PATCH 0424/3533] tests/extmod: Add vfs_posix.py test for uos.VfsPosix class. Signed-off-by: Damien George --- tests/extmod/vfs_posix.py | 23 +++++++++++++++++++++++ tests/extmod/vfs_posix.py.exp | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 tests/extmod/vfs_posix.py create mode 100644 tests/extmod/vfs_posix.py.exp diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py new file mode 100644 index 0000000000000..3bea99365d970 --- /dev/null +++ b/tests/extmod/vfs_posix.py @@ -0,0 +1,23 @@ +# Test for VfsPosix + +try: + import uos + + uos.VfsPosix +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +# getcwd and chdir +curdir = uos.getcwd() +uos.chdir("/") +print(uos.getcwd()) +uos.chdir(curdir) +print(uos.getcwd() == curdir) + +# stat +print(type(uos.stat("/"))) + +# listdir and ilistdir +print(type(uos.listdir("/"))) diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp new file mode 100644 index 0000000000000..1b1f59b43e5da --- /dev/null +++ b/tests/extmod/vfs_posix.py.exp @@ -0,0 +1,4 @@ +/ +True + + From be24e6a53faf31926a65c51426591b21f3f09c54 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 28 Nov 2020 17:31:13 +1100 Subject: [PATCH 0425/3533] py/mpprint: Prevent case fall-through when assert is disabled. Signed-off-by: Damien George --- py/mpprint.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/py/mpprint.c b/py/mpprint.c index c550c1d952f0a..31cf73bb58af9 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -557,11 +557,9 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { case 'l': { unsigned long long int arg_value = va_arg(args, unsigned long long int); ++fmt; - if (*fmt == 'u' || *fmt == 'd') { - chrs += mp_print_int(print, arg_value, *fmt == 'd', 10, 'a', flags, fill, width); - break; - } - assert(!"unsupported fmt char"); + assert(*fmt == 'u' || *fmt == 'd' || !"unsupported fmt char"); + chrs += mp_print_int(print, arg_value, *fmt == 'd', 10, 'a', flags, fill, width); + break; } #endif default: From 547e8a9fe70dee4de92a35b7a98a10bf26a20321 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Nov 2020 18:04:35 +1100 Subject: [PATCH 0426/3533] tools/ci.sh: Add helper script to run CI tasks. The aim is for this script to be used on any CI platform, as well as run locally. Signed-off-by: Damien George --- tools/ci.sh | 461 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100755 tools/ci.sh diff --git a/tools/ci.sh b/tools/ci.sh new file mode 100755 index 0000000000000..3b906b449c112 --- /dev/null +++ b/tools/ci.sh @@ -0,0 +1,461 @@ +#!/bin/bash + +if which nproc > /dev/null; then + MAKEOPTS="-j$(nproc)" +else + MAKEOPTS="-j$(sysctl -n hw.ncpu)" +fi + +######################################################################################## +# general helper functions + +function ci_gcc_arm_setup { + sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi + arm-none-eabi-gcc --version +} + +######################################################################################## +# code formatting + +function ci_code_formatting_setup { + sudo apt-add-repository --yes --update ppa:pybricks/ppa + sudo apt-get install uncrustify + pip3 install black + uncrustify --version + black --version +} + +function ci_code_formatting_run { + tools/codeformat.py -v +} + +######################################################################################## +# code size + +function ci_code_size_setup { + sudo apt-get install gcc-multilib + gcc --version + ci_gcc_arm_setup +} + +function ci_code_size_build { + # starts off at either the ref/pull/N/merge FETCH_HEAD, or the current branch HEAD + git checkout -b pull_request # save the current location + git remote add upstream https://github.com/micropython/micropython.git + git fetch --depth=100 upstream + # build reference, save to size0 + # ignore any errors with this build, in case master is failing + git checkout `git merge-base --fork-point upstream/master pull_request` + git show -s + tools/metrics.py clean bm + tools/metrics.py build bm | tee ~/size0 || true + # build PR/branch, save to size1 + git checkout pull_request + git log upstream/master..HEAD + tools/metrics.py clean bm + tools/metrics.py build bm | tee ~/size1 +} + +######################################################################################## +# ports/cc3200 + +function ci_cc3200_setup { + ci_gcc_arm_setup +} + +function ci_cc3200_build { + make ${MAKEOPTS} -C ports/cc3200 BTARGET=application BTYPE=release + make ${MAKEOPTS} -C ports/cc3200 BTARGET=bootloader BTYPE=release +} + +######################################################################################## +# ports/esp32 + +function ci_esp32_idf3_setup { + sudo pip3 install pyserial 'pyparsing<2.4' + curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar zxf - + git clone https://github.com/espressif/esp-idf.git + echo $(pwd)/xtensa-esp32-elf/bin +} + +function ci_esp32_idf3_build { + make ${MAKEOPTS} -C mpy-cross + git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V3 :=" ports/esp32/Makefile | cut -d " " -f 3) + git -C esp-idf submodule update --init components/json/cJSON components/esp32/lib components/esptool_py/esptool components/expat/expat components/lwip/lwip components/mbedtls/mbedtls components/micro-ecc/micro-ecc components/nghttp/nghttp2 components/nimble components/bt + make ${MAKEOPTS} -C ports/esp32 submodules + make ${MAKEOPTS} -C ports/esp32 +} + +function ci_esp32_idf4_setup { + sudo pip3 install pyserial 'pyparsing<2.4' + curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp-2019r2-linux-amd64.tar.gz | tar zxf - + git clone https://github.com/espressif/esp-idf.git + echo $(pwd)/xtensa-esp32-elf/bin +} + +function ci_esp32_idf4_build { + make ${MAKEOPTS} -C mpy-cross + git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V4 :=" ports/esp32/Makefile | cut -d " " -f 3) + git -C esp-idf submodule update --init components/bt/controller/lib components/bt/host/nimble/nimble components/esp_wifi/lib_esp32 components/esptool_py/esptool components/lwip/lwip components/mbedtls/mbedtls + make ${MAKEOPTS} -C ports/esp32 submodules + make ${MAKEOPTS} -C ports/esp32 +} + +######################################################################################## +# ports/esp8266 + +function ci_esp8266_setup { + sudo pip install pyserial + wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz + zcat xtensa-lx106-elf-standalone.tar.gz | tar x + echo $(pwd)/xtensa-lx106-elf/bin +} + +function ci_esp8266_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/esp8266 submodules + make ${MAKEOPTS} -C ports/esp8266 + make ${MAKEOPTS} -C ports/esp8266 BOARD=GENERIC_512K + make ${MAKEOPTS} -C ports/esp8266 BOARD=GENERIC_1M +} + +######################################################################################## +# ports/nrf + +function ci_nrf_setup { + ci_gcc_arm_setup +} + +function ci_nrf_build { + ports/nrf/drivers/bluetooth/download_ble_stack.sh s140_nrf52_6_1_1 + make ${MAKEOPTS} -C ports/nrf submodules + make ${MAKEOPTS} -C ports/nrf BOARD=pca10040 + make ${MAKEOPTS} -C ports/nrf BOARD=microbit + make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s140 + make ${MAKEOPTS} -C ports/nrf BOARD=pca10090 +} + +######################################################################################## +# ports/powerpc + +function ci_powerpc_setup { + sudo apt-get install gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross +} + +function ci_powerpc_build { + make ${MAKEOPTS} -C ports/powerpc UART=potato + make ${MAKEOPTS} -C ports/powerpc UART=lpc_serial +} + +######################################################################################## +# ports/qemu-arm + +function ci_qemu_arm_setup { + ci_gcc_arm_setup + sudo apt-get update + sudo apt-get install qemu-system + qemu-system-arm --version +} + +function ci_qemu_arm_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/qemu-arm CFLAGS_EXTRA=-DMP_ENDIANNESS_BIG=1 + make ${MAKEOPTS} -C ports/qemu-arm clean + make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test +} + +######################################################################################## +# ports/samd + +function ci_samd_setup { + ci_gcc_arm_setup +} + +function ci_samd_build { + make ${MAKEOPTS} -C ports/samd submodules + make ${MAKEOPTS} -C ports/samd +} + +######################################################################################## +# ports/stm32 + +function ci_stm32_setup { + ci_gcc_arm_setup +} + +function ci_stm32_pyb_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/stm32 submodules + git submodule update --init lib/btstack + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA="-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1" + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 + make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' + make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBD_SF6 +} + +function ci_stm32_nucleo_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/stm32 submodules + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_F091RC + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_H743ZI CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L073RZ + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L476RG + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_WB55 + make ${MAKEOPTS} -C ports/stm32/mboot BOARD=NUCLEO_WB55 +} + +######################################################################################## +# ports/teensy + +function ci_teensy_setup { + ci_gcc_arm_setup +} + +function ci_teensy_build { + make ${MAKEOPTS} -C ports/teensy +} + +######################################################################################## +# ports/unix + +CI_UNIX_OPTS_SYS_SETTRACE=( + MICROPY_PY_BTREE=0 + MICROPY_PY_FFI=0 + MICROPY_PY_USSL=0 + CFLAGS_EXTRA="-DMICROPY_PY_SYS_SETTRACE=1" +) + +CI_UNIX_OPTS_SYS_SETTRACE_STACKLESS=( + MICROPY_PY_BTREE=0 + MICROPY_PY_FFI=0 + MICROPY_PY_USSL=0 + CFLAGS_EXTRA="-DMICROPY_STACKLESS=1 -DMICROPY_STACKLESS_STRICT=1 -DMICROPY_PY_SYS_SETTRACE=1" +) + +function ci_unix_build_helper { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix "$@" submodules + make ${MAKEOPTS} -C ports/unix "$@" deplibs + make ${MAKEOPTS} -C ports/unix "$@" +} + +function ci_unix_run_tests_helper { + make -C ports/unix "$@" test +} + +function ci_unix_run_tests_full_helper { + variant=$1 + shift + if [ $variant = standard ]; then + micropython=micropython + else + micropython=micropython-$variant + fi + make -C ports/unix VARIANT=$variant "$@" test_full + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/$micropython ./run-multitests.py multi_net/*.py) +} + +function ci_native_mpy_modules_build { + if [ "$1" = "" ]; then + arch=x64 + else + arch=$1 + fi + make -C examples/natmod/features1 ARCH=$arch + make -C examples/natmod/features2 ARCH=$arch + make -C examples/natmod/btree ARCH=$arch + make -C examples/natmod/framebuf ARCH=$arch + make -C examples/natmod/uheapq ARCH=$arch + make -C examples/natmod/urandom ARCH=$arch + make -C examples/natmod/ure ARCH=$arch + make -C examples/natmod/uzlib ARCH=$arch +} + +function ci_native_mpy_modules_32bit_build { + ci_native_mpy_modules_build x86 +} + +function ci_unix_minimal_build { + make ${MAKEOPTS} -C ports/unix VARIANT=minimal +} + +function ci_unix_minimal_run_tests { + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests -e exception_chain -e self_type_check -e subclass_native_init -d basics) +} + +function ci_unix_standard_build { + ci_unix_build_helper VARIANT=standard +} + +function ci_unix_standard_run_tests { + ci_unix_run_tests_full_helper standard +} + +function ci_unix_standard_run_perfbench { + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython ./run-perfbench.py 1000 1000) +} + +function ci_unix_coverage_setup { + sudo apt-get install lcov + sudo pip3 install setuptools + sudo pip3 install pyelftools + gcc --version + python3 --version +} + +function ci_unix_coverage_build { + ci_unix_build_helper VARIANT=coverage +} + +function ci_unix_coverage_run_tests { + ci_unix_run_tests_full_helper coverage +} + +function ci_unix_coverage_run_native_mpy_tests { + MICROPYPATH=examples/natmod/features2 ./ports/unix/micropython-coverage -m features2 + (cd tests && ./run-natmodtests.py "$@" extmod/{btree*,framebuf*,uheapq*,ure*,uzlib*}.py) +} + +function ci_unix_32bit_setup { + sudo dpkg --add-architecture i386 + sudo apt-get update + sudo apt-get install gcc-multilib g++-multilib libffi-dev:i386 + sudo pip3 install setuptools + sudo pip3 install pyelftools + gcc --version + python2 --version + python3 --version +} + +function ci_unix_coverage_32bit_build { + ci_unix_build_helper VARIANT=coverage MICROPY_FORCE_32BIT=1 +} + +function ci_unix_coverage_32bit_run_tests { + ci_unix_run_tests_full_helper coverage MICROPY_FORCE_32BIT=1 +} + +function ci_unix_coverage_32bit_run_native_mpy_tests { + ci_unix_coverage_run_native_mpy_tests --arch x86 +} + +function ci_unix_nanbox_build { + # Use Python 2 to check that it can run the build scripts + ci_unix_build_helper PYTHON=python2 VARIANT=nanbox +} + +function ci_unix_nanbox_run_tests { + ci_unix_run_tests_full_helper nanbox PYTHON=python2 +} + +function ci_unix_float_build { + ci_unix_build_helper VARIANT=standard CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" +} + +function ci_unix_float_run_tests { + # TODO get this working: ci_unix_run_tests_full_helper standard CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" + ci_unix_run_tests_helper CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" +} + +function ci_unix_clang_setup { + sudo apt-get install clang + clang --version +} + +function ci_unix_stackless_clang_build { + make ${MAKEOPTS} -C mpy-cross CC=clang + make ${MAKEOPTS} -C ports/unix submodules + make ${MAKEOPTS} -C ports/unix CC=clang CFLAGS_EXTRA="-DMICROPY_STACKLESS=1 -DMICROPY_STACKLESS_STRICT=1" +} + +function ci_unix_stackless_clang_run_tests { + ci_unix_run_tests_helper CC=clang +} + +function ci_unix_float_clang_build { + make ${MAKEOPTS} -C mpy-cross CC=clang + make ${MAKEOPTS} -C ports/unix submodules + make ${MAKEOPTS} -C ports/unix CC=clang CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" +} + +function ci_unix_float_clang_run_tests { + ci_unix_run_tests_helper CC=clang +} + +function ci_unix_settrace_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SYS_SETTRACE[@]}" +} + +function ci_unix_settrace_run_tests { + ci_unix_run_tests_helper "${CI_UNIX_OPTS_SYS_SETTRACE[@]}" +} + +function ci_unix_settrace_stackless_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SYS_SETTRACE_STACKLESS[@]}" +} + +function ci_unix_settrace_stackless_run_tests { + ci_unix_run_tests_helper "${CI_UNIX_OPTS_SYS_SETTRACE_STACKLESS[@]}" +} + +function ci_unix_macos_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix submodules + #make ${MAKEOPTS} -C ports/unix deplibs + make ${MAKEOPTS} -C ports/unix + # check for additional compiler errors/warnings + make ${MAKEOPTS} -C ports/unix VARIANT=coverage submodules + make ${MAKEOPTS} -C ports/unix VARIANT=coverage +} + +function ci_unix_macos_run_tests { + # Issues with macOS tests: + # - OSX has poor time resolution and these uasyncio tests do not have correct output + # - import_pkg7 has a problem with relative imports + # - urandom_basic has a problem with getrandbits(0) + (cd tests && ./run-tests --exclude 'uasyncio_(basic|heaplock|lock|wait_task)' --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') +} + +######################################################################################## +# ports/windows + +function ci_windows_setup { + sudo apt-get install gcc-mingw-w64 +} + +function ci_windows_build { + make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/windows CROSS_COMPILE=i686-w64-mingw32- +} + +######################################################################################## +# ports/zephyr + +function ci_zephyr_setup { + docker pull zephyrprojectrtos/ci:v0.11.8 + docker run --name zephyr-ci -d -it \ + -v "$(pwd)":/micropython \ + -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.11.3 \ + -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr \ + -w /micropython/ports/zephyr \ + zephyrprojectrtos/ci:v0.11.8 + docker ps -a +} + +function ci_zephyr_install { + docker exec zephyr-ci west init --mr v2.4.0 /zephyrproject + docker exec -w /zephyrproject zephyr-ci west update + docker exec -w /zephyrproject zephyr-ci west zephyr-export +} + +function ci_zephyr_build { + docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS}" + docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS} BOARD=frdm_k64f" + docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS}" + docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=frdm_k64f" + docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=mimxrt1050_evk" + docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=reel_board" +} From a598ae5b4dc4054f90156480454689c79f7c41a8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Nov 2020 18:05:45 +1100 Subject: [PATCH 0427/3533] github/workflows: Add workflows for all CI tasks, builds and tests. Signed-off-by: Damien George --- .github/workflows/code_formatting.yml | 16 +++ .github/workflows/code_size.yml | 25 ++++ .github/workflows/ports_cc3200.yml | 23 ++++ .github/workflows/ports_esp32.yml | 36 ++++++ .github/workflows/ports_esp8266.yml | 23 ++++ .github/workflows/ports_nrf.yml | 23 ++++ .github/workflows/ports_powerpc.yml | 23 ++++ .github/workflows/ports_qemu-arm.yml | 26 ++++ .github/workflows/ports_samd.yml | 23 ++++ .github/workflows/ports_stm32.yml | 32 +++++ .github/workflows/ports_teensy.yml | 23 ++++ .github/workflows/ports_unix.yml | 176 ++++++++++++++++++++++++++ .github/workflows/ports_windows.yml | 23 ++++ .github/workflows/ports_zephyr.yml | 24 ++++ 14 files changed, 496 insertions(+) create mode 100644 .github/workflows/code_formatting.yml create mode 100644 .github/workflows/code_size.yml create mode 100644 .github/workflows/ports_cc3200.yml create mode 100644 .github/workflows/ports_esp32.yml create mode 100644 .github/workflows/ports_esp8266.yml create mode 100644 .github/workflows/ports_nrf.yml create mode 100644 .github/workflows/ports_powerpc.yml create mode 100644 .github/workflows/ports_qemu-arm.yml create mode 100644 .github/workflows/ports_samd.yml create mode 100644 .github/workflows/ports_stm32.yml create mode 100644 .github/workflows/ports_teensy.yml create mode 100644 .github/workflows/ports_unix.yml create mode 100644 .github/workflows/ports_windows.yml create mode 100644 .github/workflows/ports_zephyr.yml diff --git a/.github/workflows/code_formatting.yml b/.github/workflows/code_formatting.yml new file mode 100644 index 0000000000000..aab347d78e97d --- /dev/null +++ b/.github/workflows/code_formatting.yml @@ -0,0 +1,16 @@ +name: Check code formatting + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v1 + - name: Install packages + run: source tools/ci.sh && ci_code_formatting_setup + - name: Run code formatting + run: source tools/ci.sh && ci_code_formatting_run + - name: Check code formatting + run: git diff --exit-code diff --git a/.github/workflows/code_size.yml b/.github/workflows/code_size.yml new file mode 100644 index 0000000000000..dd759a4f3ff56 --- /dev/null +++ b/.github/workflows/code_size.yml @@ -0,0 +1,25 @@ +name: Check code size + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'ports/bare-arm/**' + - 'ports/minimal/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_code_size_setup + - name: Build + run: source tools/ci.sh && ci_code_size_build + - name: Compute code size difference + run: tools/metrics.py diff --error-threshold 0 ~/size0 ~/size1 diff --git a/.github/workflows/ports_cc3200.yml b/.github/workflows/ports_cc3200.yml new file mode 100644 index 0000000000000..0eaa36da3796e --- /dev/null +++ b/.github/workflows/ports_cc3200.yml @@ -0,0 +1,23 @@ +name: cc3200 port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/cc3200/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_cc3200_setup + - name: Build + run: source tools/ci.sh && ci_cc3200_build diff --git a/.github/workflows/ports_esp32.yml b/.github/workflows/ports_esp32.yml new file mode 100644 index 0000000000000..b89e462e049fc --- /dev/null +++ b/.github/workflows/ports_esp32.yml @@ -0,0 +1,36 @@ +name: esp32 port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/esp32/**' + +jobs: + idf3_build: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_esp32_idf3_setup >> $GITHUB_PATH + - name: Build + env: + IDF_PATH: ${{ github.workspace }}/esp-idf + run: source tools/ci.sh && ci_esp32_idf3_build + + idf4_build: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_esp32_idf4_setup >> $GITHUB_PATH + - name: Build + env: + IDF_PATH: ${{ github.workspace }}/esp-idf + run: source tools/ci.sh && ci_esp32_idf4_build diff --git a/.github/workflows/ports_esp8266.yml b/.github/workflows/ports_esp8266.yml new file mode 100644 index 0000000000000..f6f2fcaf4a80e --- /dev/null +++ b/.github/workflows/ports_esp8266.yml @@ -0,0 +1,23 @@ +name: esp8266 port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/esp8266/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_esp8266_setup >> $GITHUB_PATH + - name: Build + run: source tools/ci.sh && ci_esp8266_build diff --git a/.github/workflows/ports_nrf.yml b/.github/workflows/ports_nrf.yml new file mode 100644 index 0000000000000..1ba3b0ce61e96 --- /dev/null +++ b/.github/workflows/ports_nrf.yml @@ -0,0 +1,23 @@ +name: nrf port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/nrf/**' + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_nrf_setup + - name: Build + run: source tools/ci.sh && ci_nrf_build diff --git a/.github/workflows/ports_powerpc.yml b/.github/workflows/ports_powerpc.yml new file mode 100644 index 0000000000000..88fa59767b756 --- /dev/null +++ b/.github/workflows/ports_powerpc.yml @@ -0,0 +1,23 @@ +name: powerpc port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/powerpc/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_powerpc_setup + - name: Build + run: source tools/ci.sh && ci_powerpc_build diff --git a/.github/workflows/ports_qemu-arm.yml b/.github/workflows/ports_qemu-arm.yml new file mode 100644 index 0000000000000..f456dfd74aff6 --- /dev/null +++ b/.github/workflows/ports_qemu-arm.yml @@ -0,0 +1,26 @@ +name: qemu-arm port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/qemu-arm/**' + +jobs: + build_and_test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_qemu_arm_setup + - name: Build + run: source tools/ci.sh && ci_qemu_arm_build + - name: Print failures + if: failure() + run: grep --text "FAIL" ports/qemu-arm/build/console.out diff --git a/.github/workflows/ports_samd.yml b/.github/workflows/ports_samd.yml new file mode 100644 index 0000000000000..bde4ed965faa0 --- /dev/null +++ b/.github/workflows/ports_samd.yml @@ -0,0 +1,23 @@ +name: samd port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/samd/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_samd_setup + - name: Build + run: source tools/ci.sh && ci_samd_build diff --git a/.github/workflows/ports_stm32.yml b/.github/workflows/ports_stm32.yml new file mode 100644 index 0000000000000..4634339c96a6a --- /dev/null +++ b/.github/workflows/ports_stm32.yml @@ -0,0 +1,32 @@ +name: stm32 port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/stm32/**' + +jobs: + build_pyb: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_stm32_setup + - name: Build + run: source tools/ci.sh && ci_stm32_pyb_build + + build_nucleo: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_stm32_setup + - name: Build + run: source tools/ci.sh && ci_stm32_nucleo_build diff --git a/.github/workflows/ports_teensy.yml b/.github/workflows/ports_teensy.yml new file mode 100644 index 0000000000000..7ae77c80af604 --- /dev/null +++ b/.github/workflows/ports_teensy.yml @@ -0,0 +1,23 @@ +name: teensy port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/teensy/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_teensy_setup + - name: Build + run: source tools/ci.sh && ci_teensy_build diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml new file mode 100644 index 0000000000000..385b2c008a7c8 --- /dev/null +++ b/.github/workflows/ports_unix.yml @@ -0,0 +1,176 @@ +name: unix port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'examples/**' + - 'ports/unix/**' + +jobs: + minimal: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: source tools/ci.sh && ci_unix_minimal_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_minimal_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + standard: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: source tools/ci.sh && ci_unix_standard_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_standard_run_tests + - name: Run performance benchmarks + run: source tools/ci.sh && ci_unix_standard_run_perfbench + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + coverage: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_coverage_setup + - name: Build + run: source tools/ci.sh && ci_unix_coverage_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_coverage_run_tests + - name: Build native mpy modules + run: source tools/ci.sh && ci_native_mpy_modules_build + - name: Test importing .mpy generated by mpy_ld.py + run: source tools/ci.sh && ci_unix_coverage_run_native_mpy_tests + - name: Run lcov coverage analysis + run: | + mkdir -p coverage + lcov --rc lcov_branch_coverage=1 --directory ports/unix/build-coverage --capture --output-file coverage/lcov.info.all + lcov --remove coverage/lcov.info.all '*/lib/*' '*/ports/unix/*' '*/utils/*' --output-file coverage/lcov.info + - name: Send to coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + coverage_32bit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_32bit_setup + - name: Build + run: source tools/ci.sh && ci_unix_coverage_32bit_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_coverage_32bit_run_tests + - name: Build native mpy modules + run: source tools/ci.sh && ci_native_mpy_modules_32bit_build + - name: Test importing .mpy generated by mpy_ld.py + run: source tools/ci.sh && ci_unix_coverage_32bit_run_native_mpy_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + nanbox: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_32bit_setup + - name: Build + run: source tools/ci.sh && ci_unix_nanbox_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_nanbox_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + float: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: source tools/ci.sh && ci_unix_float_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_float_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + stackless_clang: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_clang_setup + - name: Build + run: source tools/ci.sh && ci_unix_stackless_clang_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_stackless_clang_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + float_clang: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_clang_setup + - name: Build + run: source tools/ci.sh && ci_unix_float_clang_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_float_clang_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + settrace: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: source tools/ci.sh && ci_unix_settrace_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_settrace_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + settrace_stackless: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: source tools/ci.sh && ci_unix_settrace_stackless_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_settrace_stackless_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures + + macos: + runs-on: macos-11.0 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v1 + - name: Build + run: source tools/ci.sh && ci_unix_macos_build + - name: Run tests + run: source tools/ci.sh && ci_unix_macos_run_tests + - name: Print failures + if: failure() + run: tests/run-tests --print-failures diff --git a/.github/workflows/ports_windows.yml b/.github/workflows/ports_windows.yml new file mode 100644 index 0000000000000..1bfe40c7fded2 --- /dev/null +++ b/.github/workflows/ports_windows.yml @@ -0,0 +1,23 @@ +name: windows port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'ports/unix/**' + - 'ports/windows/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_windows_setup + - name: Build + run: source tools/ci.sh && ci_windows_build diff --git a/.github/workflows/ports_zephyr.yml b/.github/workflows/ports_zephyr.yml new file mode 100644 index 0000000000000..d9ae2b8c55a2a --- /dev/null +++ b/.github/workflows/ports_zephyr.yml @@ -0,0 +1,24 @@ +name: zephyr port + +on: + push: + pull_request: + paths: + - '.github/workflows/ports_zephyr.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'ports/zephyr/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_zephyr_setup + - name: Install Zephyr + run: source tools/ci.sh && ci_zephyr_install + - name: Build + run: source tools/ci.sh && ci_zephyr_build From ee3706f4bdcb06c60eef16eff38dd388509021d8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Nov 2020 18:06:57 +1100 Subject: [PATCH 0428/3533] travis: Stop using Travis for CI. Travis now limits the amount of free minutes for open-source projects, and it does not provide enough for this project. So stop using it and instead use on GitHub Actions. Signed-off-by: Damien George --- .travis.yml | 396 ---------------------------------------------------- 1 file changed, 396 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 14595ddeb1d39..0000000000000 --- a/.travis.yml +++ /dev/null @@ -1,396 +0,0 @@ -# global options -dist: xenial -language: - - c -compiler: - - gcc -cache: - directories: - - "${HOME}/persist" -env: - global: - - MAKEOPTS="-j4" -git: - submodules: false - -# define the successive stages -stages: - - name: test - -# define the jobs for the stages -# approx order of the jobs has longest running first to optimise total time -jobs: - include: - # check code formatting - - stage: test - os: linux - dist: bionic - name: "code formatting" - before_install: - - sudo apt-add-repository --yes --update ppa:pybricks/ppa - install: - - sudo apt-get install uncrustify python3-pip - - uncrustify --version - - pip3 install --user setuptools - - pip3 install --user black - - black --version - script: - - tools/codeformat.py - - git diff --exit-code - - # zephyr port - - stage: test - name: "zephyr port build" - services: - - docker - before_install: - - docker pull zephyrprojectrtos/ci:v0.11.8 - - > - docker run --name zephyr-ci -d -it - -v "$(pwd)":/micropython - -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.11.3 - -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr - -w /micropython/ports/zephyr - zephyrprojectrtos/ci:v0.11.8 - - docker ps -a - install: - - docker exec zephyr-ci west init --mr v2.4.0 /zephyrproject - - docker exec -w /zephyrproject zephyr-ci west update - - docker exec -w /zephyrproject zephyr-ci west zephyr-export - script: - - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS}" - - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS} BOARD=frdm_k64f" - - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS}" - - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=frdm_k64f" - - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=mimxrt1050_evk" - - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=reel_board" - - # unix port on OSX (first in list because the build VM takes a long time to start) - - stage: test - os: osx - osx_image: xcode11.3 - name: "unix port build with clang on OSX" - env: - - PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/unix submodules - - make ${MAKEOPTS} -C ports/unix deplibs - - make ${MAKEOPTS} -C ports/unix - # OSX has poor time resolution and the following tests do not have the correct output - - (cd tests && ./run-tests --exclude 'uasyncio_(basic|heaplock|lock|wait_task)') - # check for additional compiler errors/warnings - - make ${MAKEOPTS} -C ports/unix VARIANT=coverage submodules - - make ${MAKEOPTS} -C ports/unix VARIANT=coverage - after_failure: - - tests/run-tests --print-failures - - # stm32 port - - stage: test - name: "stm32 port build" - install: - # need newer gcc version for Cortex-M7 support - - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa - - sudo apt-get update -qq || true - - sudo apt-get install gcc-arm-embedded libnewlib-arm-none-eabi - - arm-none-eabi-gcc --version - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/stm32 submodules - - git submodule update --init lib/btstack - - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_F091RC - - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA="-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1" - - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 - - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 - - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_H743ZI CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' - - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L073RZ - - make ${MAKEOPTS} -C ports/stm32 BOARD=STM32L476DISC - - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_WB55 - - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' - - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBD_SF6 - - make ${MAKEOPTS} -C ports/stm32/mboot BOARD=NUCLEO_WB55 - - # qemu-arm port - - stage: test - dist: bionic # needed for more recent version of qemu-system-arm with mps2-an385 target - name: "qemu-arm port build and tests" - install: - - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi qemu-system - - arm-none-eabi-gcc --version - - qemu-system-arm --version - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/qemu-arm CFLAGS_EXTRA=-DMP_ENDIANNESS_BIG=1 - - make ${MAKEOPTS} -C ports/qemu-arm clean - - make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test - after_failure: - - grep --text "FAIL" ports/qemu-arm/build/console.out - - # unix coverage - - stage: test - name: "unix coverage build and tests" - install: - - sudo apt-get install python3-pip - - sudo pip install cpp-coveralls - - sudo pip3 install setuptools - - sudo pip3 install pyelftools - - gcc --version - - python3 --version - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/unix VARIANT=coverage submodules - - make ${MAKEOPTS} -C ports/unix VARIANT=coverage deplibs - - make ${MAKEOPTS} -C ports/unix VARIANT=coverage - # run the main test suite - - make -C ports/unix VARIANT=coverage test_full - - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-coverage ./run-multitests.py multi_net/*.py) || travis_terminate 1 - # test building native mpy modules - - make -C examples/natmod/features1 ARCH=x64 - - make -C examples/natmod/features2 ARCH=x64 - - make -C examples/natmod/btree ARCH=x64 - - make -C examples/natmod/framebuf ARCH=x64 - - make -C examples/natmod/uheapq ARCH=x64 - - make -C examples/natmod/urandom ARCH=x64 - - make -C examples/natmod/ure ARCH=x64 - - make -C examples/natmod/uzlib ARCH=x64 - # test importing .mpy generated by mpy_ld.py - - MICROPYPATH=examples/natmod/features2 ./ports/unix/micropython-coverage -m features2 - - (cd tests && ./run-natmodtests.py extmod/{btree*,framebuf*,uheapq*,ure*,uzlib*}.py) - # run coveralls coverage analysis (try to, even if some builds/tests failed) - - (cd ports/unix && coveralls --root ../.. --build-root . --gcov $(which gcov) --gcov-options '\-o build-coverage/' --include py --include extmod) - after_failure: - - tests/run-tests --print-failures - - # unix coverage 32-bit - - stage: test - name: "unix coverage 32-bit build and tests" - install: - - sudo apt-get install gcc-multilib g++-multilib libffi-dev:i386 - - sudo apt-get install python3-pip - - sudo pip3 install setuptools - - sudo pip3 install pyelftools - - gcc --version - - python3 --version - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/unix MICROPY_FORCE_32BIT=1 VARIANT=coverage submodules - - make ${MAKEOPTS} -C ports/unix MICROPY_FORCE_32BIT=1 VARIANT=coverage deplibs - - make ${MAKEOPTS} -C ports/unix MICROPY_FORCE_32BIT=1 VARIANT=coverage - # run the main test suite - - make -C ports/unix MICROPY_FORCE_32BIT=1 VARIANT=coverage test_full || travis_terminate 1 - # test building native mpy modules - - make -C examples/natmod/features1 ARCH=x86 - - make -C examples/natmod/features2 ARCH=x86 - - make -C examples/natmod/btree ARCH=x86 - - make -C examples/natmod/framebuf ARCH=x86 - - make -C examples/natmod/uheapq ARCH=x86 - - make -C examples/natmod/urandom ARCH=x86 - - make -C examples/natmod/ure ARCH=x86 - - make -C examples/natmod/uzlib ARCH=x86 - # test importing .mpy generated by mpy_ld.py - - MICROPYPATH=examples/natmod/features2 ./ports/unix/micropython-coverage -m features2 - - (cd tests && ./run-natmodtests.py --arch x86 extmod/{btree*,framebuf*,uheapq*,ure*,uzlib*}.py) - after_failure: - - tests/run-tests --print-failures - - # standard unix port - - stage: test - name: "unix port build and tests" - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/unix submodules - - make ${MAKEOPTS} -C ports/unix deplibs - - make ${MAKEOPTS} -C ports/unix - - make ${MAKEOPTS} -C ports/unix test - - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython ./run-perfbench.py 1000 1000) - after_failure: - - tests/run-tests --print-failures - - # unix nanbox/float (and using Python 2 to check it can run the build scripts) - - stage: test - name: "unix nanbox/float port build and tests" - install: - - sudo apt-get install gcc-multilib libffi-dev:i386 - script: - - make ${MAKEOPTS} -C mpy-cross PYTHON=python2 - - make ${MAKEOPTS} -C ports/unix VARIANT=nanbox submodules - - make ${MAKEOPTS} -C ports/unix PYTHON=python2 VARIANT=nanbox deplibs - - make ${MAKEOPTS} -C ports/unix PYTHON=python2 VARIANT=nanbox - - make ${MAKEOPTS} -C ports/unix PYTHON=python2 VARIANT=nanbox test_full || travis_terminate 1 - - make ${MAKEOPTS} -C ports/unix clean - - make ${MAKEOPTS} -C ports/unix CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" - - make ${MAKEOPTS} -C ports/unix test - after_failure: - - tests/run-tests --print-failures - - # unix stackless/float with clang - - stage: test - name: "unix stackless/float port build and tests with clang" - install: - - sudo apt-get install clang - script: - - make ${MAKEOPTS} -C mpy-cross CC=clang - - make ${MAKEOPTS} -C ports/unix submodules - - make ${MAKEOPTS} -C ports/unix CC=clang CFLAGS_EXTRA="-DMICROPY_STACKLESS=1 -DMICROPY_STACKLESS_STRICT=1" - - make ${MAKEOPTS} -C ports/unix CC=clang test || travis_terminate 1 - - make ${MAKEOPTS} -C ports/unix clean - - make ${MAKEOPTS} -C ports/unix CC=clang CFLAGS_EXTRA="-DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT" - - make ${MAKEOPTS} -C ports/unix CC=clang test - after_failure: - - tests/run-tests --print-failures - - # unix with sys.settrace - - stage: test - name: "unix port with sys.settrace build and tests" - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/unix MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-DMICROPY_PY_SYS_SETTRACE=1" test || travis_terminate 1 - - make ${MAKEOPTS} -C ports/unix clean - - make ${MAKEOPTS} -C ports/unix MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-DMICROPY_STACKLESS=1 -DMICROPY_STACKLESS_STRICT=1 -DMICROPY_PY_SYS_SETTRACE=1" test - after_failure: - - tests/run-tests --print-failures - - # minimal unix port with tests - - stage: test - name: "minimal unix port build and tests" - script: - - make ${MAKEOPTS} -C ports/unix VARIANT=minimal - - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests -e exception_chain -e self_type_check -e subclass_native_init -d basics) - after_failure: - - tests/run-tests --print-failures - - # windows port via mingw - - stage: test - name: "windows port build via mingw" - install: - - sudo apt-get install gcc-mingw-w64 - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/windows CROSS_COMPILE=i686-w64-mingw32- - - # esp32 w/ESP-IDFv3 port - - stage: test - name: "esp32 ESP-IDFv3 port build" - install: - - sudo apt-get install python3-pip - - sudo pip3 install 'pyparsing<2.4' - - curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar zxf - - - export PATH=$(pwd)/xtensa-esp32-elf/bin:$PATH - - git clone https://github.com/espressif/esp-idf.git - - export IDF_PATH=$(pwd)/esp-idf - script: - - make ${MAKEOPTS} -C mpy-cross - - git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V3 :=" ports/esp32/Makefile | cut -d " " -f 3) - - git -C esp-idf submodule update --init components/json/cJSON components/esp32/lib components/esptool_py/esptool components/expat/expat components/lwip/lwip components/mbedtls/mbedtls components/micro-ecc/micro-ecc components/nghttp/nghttp2 components/nimble components/bt - - make ${MAKEOPTS} -C ports/esp32 submodules - - make ${MAKEOPTS} -C ports/esp32 - - # esp32 w/ESP-IDFv4 port - - stage: test - name: "esp32 ESP-IDFv4 port build" - install: - - sudo apt-get install python3-pip - - sudo pip3 install 'pyparsing<2.4' - - curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp-2019r2-linux-amd64.tar.gz | tar zxf - - - export PATH=$(pwd)/xtensa-esp32-elf/bin:$PATH - - git clone https://github.com/espressif/esp-idf.git - - export IDF_PATH=$(pwd)/esp-idf - script: - - make ${MAKEOPTS} -C mpy-cross - - git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V4 :=" ports/esp32/Makefile | cut -d " " -f 3) - - git -C esp-idf submodule update --init components/bt/controller/lib components/bt/host/nimble/nimble components/esp_wifi/lib_esp32 components/esptool_py/esptool components/lwip/lwip components/mbedtls/mbedtls - - make ${MAKEOPTS} -C ports/esp32 submodules - - make ${MAKEOPTS} -C ports/esp32 - - # esp8266 port - - stage: test - name: "esp8266 port build" - install: - - wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz - - zcat xtensa-lx106-elf-standalone.tar.gz | tar x - - export PATH=$(pwd)/xtensa-lx106-elf/bin:$PATH - script: - - make ${MAKEOPTS} -C mpy-cross - - make ${MAKEOPTS} -C ports/esp8266 submodules - - make ${MAKEOPTS} -C ports/esp8266 - - make ${MAKEOPTS} -C ports/esp8266 BOARD=GENERIC_512K - - make ${MAKEOPTS} -C ports/esp8266 BOARD=GENERIC_1M - - # nrf port - - stage: test - name: "nrf port build" - install: - # need newer gcc version for Cortex-M33 support - - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa - - sudo apt-get update -qq || true - - sudo apt-get install gcc-arm-embedded - - sudo apt-get install libnewlib-arm-none-eabi - - arm-none-eabi-gcc --version - script: - - ports/nrf/drivers/bluetooth/download_ble_stack.sh s140_nrf52_6_1_1 - - make ${MAKEOPTS} -C ports/nrf submodules - - make ${MAKEOPTS} -C ports/nrf BOARD=pca10040 - - make ${MAKEOPTS} -C ports/nrf BOARD=microbit - - make ${MAKEOPTS} -C ports/nrf BOARD=pca10056 SD=s140 - - make ${MAKEOPTS} -C ports/nrf BOARD=pca10090 - - # bare-arm and minimal ports, with size-diff check - - stage: test - name: "bare-arm and minimal ports build and size-diff check" - install: - - sudo apt-get install gcc-multilib libffi-dev:i386 gcc-arm-none-eabi libnewlib-arm-none-eabi - - gcc --version - - arm-none-eabi-gcc --version - script: - # starts off at either the ref/pull/N/merge FETCH_HEAD, or the current branch HEAD - - git checkout -b pull_request # save the current location - - git remote add upstream https://github.com/micropython/micropython.git - - git fetch --depth=100 upstream - # build reference, save to size0 - # ignore any errors with this build, in case master is failing - - git checkout `git merge-base --fork-point upstream/master pull_request` - - git show -s - - tools/metrics.py clean bm - - tools/metrics.py build bm | tee ~/size0 || true - # build PR/branch, save to size1 - - git checkout pull_request - - git log upstream/master..HEAD - - tools/metrics.py clean bm - - tools/metrics.py build bm | tee ~/size1 || travis_terminate 1 - # compute diff of the code sizes - - tools/metrics.py diff --error-threshold 0 ~/size0 ~/size1 - - # cc3200 port - - stage: test - name: "cc3200 port build" - install: - - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi - script: - - make ${MAKEOPTS} -C ports/cc3200 BTARGET=application BTYPE=release - - make ${MAKEOPTS} -C ports/cc3200 BTARGET=bootloader BTYPE=release - - # samd port - - stage: test - name: "samd port build" - install: - - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi - script: - - make ${MAKEOPTS} -C ports/samd submodules - - make ${MAKEOPTS} -C ports/samd - - # teensy port - - stage: test - name: "teensy port build" - install: - - sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi - script: - - make ${MAKEOPTS} -C ports/teensy - - # powerpc port - - stage: test - name: "powerpc port build" - install: - - sudo apt-get install gcc-powerpc64le-linux-gnu libc6-dev-ppc64el-cross - script: - - make ${MAKEOPTS} -C ports/powerpc UART=potato - - make ${MAKEOPTS} -C ports/powerpc UART=lpc_serial From 02b44a0154aee3cfd2675b4ff180061b2b5bc40d Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 10:28:48 +1100 Subject: [PATCH 0429/3533] tests/run-tests: Update skipped tests on CI for GitHub Actions. Signed-off-by: Damien George --- tests/run-tests | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/run-tests b/tests/run-tests index eebc8c4252700..23c30d3c3cb55 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -342,13 +342,9 @@ def run_tests(pyb, tests, args, result_dir): 'struct_endian', ) - # Some tests shouldn't be run under Travis CI - if os.getenv('TRAVIS') == 'true': - skip_tests.add('basics/memoryerror.py') - skip_tests.add('thread/thread_gc1.py') # has reliability issues - skip_tests.add('thread/thread_lock4.py') # has reliability issues - skip_tests.add('thread/stress_heap.py') # has reliability issues - skip_tests.add('thread/stress_recurse.py') # has reliability issues + # Some tests shouldn't be run on GitHub Actions + if os.getenv('GITHUB_ACTIONS') == 'true': + skip_tests.add('thread/stress_schedule.py') # has reliability issues if upy_float_precision == 0: skip_tests.add('extmod/uctypes_le_float.py') From 2f723d83c0b30a272d0ef41e8b7eda729de1e0e0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 10:29:20 +1100 Subject: [PATCH 0430/3533] README: Update badges for new GitHub Actions workflows. Signed-off-by: Damien George --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57daebf87263b..f75590a5ba7d0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.com/micropython/micropython.png?branch=master)](https://travis-ci.com/micropython/micropython) [![Coverage Status](https://coveralls.io/repos/micropython/micropython/badge.png?branch=master)](https://coveralls.io/r/micropython/micropython?branch=master) +[![CI badge](https://github.com/micropython/micropython/workflows/unix%20port/badge.svg)](https://github.com/micropython/micropython/actions?query=branch%3Amaster+event%3Apush) [![Coverage badge](https://coveralls.io/repos/micropython/micropython/badge.png?branch=master)](https://coveralls.io/r/micropython/micropython?branch=master) The MicroPython project ======================= From f7225d1c9527c9839d80b8f73641ddb4ebbe6105 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 10:33:46 +1100 Subject: [PATCH 0431/3533] github/workflows: Run unix and qemu-arm workflows when tests change. Signed-off-by: Damien George --- .github/workflows/ports_qemu-arm.yml | 3 ++- .github/workflows/ports_unix.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ports_qemu-arm.yml b/.github/workflows/ports_qemu-arm.yml index f456dfd74aff6..8d144ca3cb7aa 100644 --- a/.github/workflows/ports_qemu-arm.yml +++ b/.github/workflows/ports_qemu-arm.yml @@ -11,6 +11,7 @@ on: - 'lib/**' - 'drivers/**' - 'ports/qemu-arm/**' + - 'tests/**' jobs: build_and_test: @@ -19,7 +20,7 @@ jobs: - uses: actions/checkout@v2 - name: Install packages run: source tools/ci.sh && ci_qemu_arm_setup - - name: Build + - name: Build and run test suite run: source tools/ci.sh && ci_qemu_arm_build - name: Print failures if: failure() diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 385b2c008a7c8..552fd72d8440e 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -11,6 +11,7 @@ on: - 'lib/**' - 'examples/**' - 'ports/unix/**' + - 'tests/**' jobs: minimal: From bb24c69b909e82cc1b6e82a28f14fa732f596b9f Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Oct 2020 12:11:10 +1100 Subject: [PATCH 0432/3533] lib/utils/pyexec: Add stdin-reader on raw REPL with flow control. Background: the friendly/normal REPL is intended for human use whereas the raw REPL is for computer use/automation. Raw REPL is used for things like pyboard.py script_to_run.py. The normal REPL has built-in flow control because it echos back the characters. That's not so with raw REPL and flow control is just implemented by rate limiting the amount of data that goes in. Currently it's fixed at 256 byte chunks every 10ms. This is sometimes too fast for slow MCUs or systems with small stdin buffers. It's also too slow for a lot of higher-end MCUs, ie it could be a lot faster. This commit adds a new raw REPL mode which includes flow control: the device will echo back a character after a certain number of bytes are sent to the host, and the host can use this to regulate the data going out to the device. The amount of characters is controlled by the device and sent to the host before communication starts. This flow control allows getting the maximum speed out of a serial link, regardless of the link or the device at the other end. Also, this new raw REPL mode parses and compiles the incoming data as it comes in. It does this by creating a "stdin reader" object which is then passed to the lexer. The lexer requests bytes from this "stdin reader" which retrieves bytes from the host, and does flow control. What this means is that no memory is used to store the script (in the existing raw REPL mode the device needs a big buffer to read in the script before it can pass it on to the lexer/parser/compiler). The only memory needed on the device is enough to parse and compile. Finally, it would be possible to extend this new raw REPL to allow bytecode (.mpy files) to be sent as well as text mode scripts (but that's not done in this commit). Some results follow. The test was to send a large 33k script that contains mostly comments and then prints out the heap, run via pyboard.py large.py. On PYBD-SF6, prior to this PR: $ ./pyboard.py large.py stack: 524 out of 23552 GC: total: 392192, used: 34464, free: 357728 No. of 1-blocks: 12, 2-blocks: 2, max blk sz: 2075, max free sz: 22345 GC memory layout; from 2001a3f0: 00000: h=hhhh=======================================hhBShShh==h=======h 00400: =====hh=B........h==h=========================================== 00800: ================================================================ 00c00: ================================================================ 01000: ================================================================ 01400: ================================================================ 01800: ================================================================ 01c00: ================================================================ 02000: ================================================================ 02400: ================================================================ 02800: ================================================================ 02c00: ================================================================ 03000: ================================================================ 03400: ================================================================ 03800: ================================================================ 03c00: ================================================================ 04000: ================================================================ 04400: ================================================================ 04800: ================================================================ 04c00: ================================================================ 05000: ================================================================ 05400: ================================================================ 05800: ================================================================ 05c00: ================================================================ 06000: ================================================================ 06400: ================================================================ 06800: ================================================================ 06c00: ================================================================ 07000: ================================================================ 07400: ================================================================ 07800: ================================================================ 07c00: ================================================================ 08000: ================================================================ 08400: ===============================================.....h==......... (349 lines all free) (the big blob of used memory is the large script). Same but with this PR: $ ./pyboard.py large.py stack: 524 out of 23552 GC: total: 392192, used: 1296, free: 390896 No. of 1-blocks: 12, 2-blocks: 3, max blk sz: 40, max free sz: 24420 GC memory layout; from 2001a3f0: 00000: h=hhhh=======================================hhBShShh==h=======h 00400: =====hh=h=B......h==.....h==.................................... (381 lines all free) The only thing in RAM is the compiled script (and some other unrelated items). Time to download before this PR: 1438ms, data rate: 230,799 bits/sec. Time to download with this PR: 119ms, data rate: 2,788,991 bits/sec. So it's more than 10 times faster, and uses significantly less RAM. Results are similar on other boards. On an stm32 board that connects via UART only at 115200 baud, the data rate goes from 80kbit/sec to 113kbit/sec, so gets close to saturating the UART link without loss of data. The new raw REPL mode also supports a single ctrl-C to break out of this flow-control mode, so that a ctrl-C can always get back to a known state. It's also backwards compatible with the original raw REPL mode, which is still supported with the same sequence of commands. The new raw REPL mode is activated by ctrl-E, which gives an error on devices that do not support the new mode. Signed-off-by: Damien George --- lib/utils/pyexec.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 2b86af3bbaadd..86321ac12909f 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -56,6 +56,7 @@ STATIC bool repl_display_debugging_info = 0; #define EXEC_FLAG_SOURCE_IS_RAW_CODE (8) #define EXEC_FLAG_SOURCE_IS_VSTR (16) #define EXEC_FLAG_SOURCE_IS_FILENAME (32) +#define EXEC_FLAG_SOURCE_IS_READER (64) // parses, compiles and executes the code in the lexer // frees the lexer before returning @@ -91,6 +92,8 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input if (exec_flags & EXEC_FLAG_SOURCE_IS_VSTR) { const vstr_t *vstr = source; lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, 0); + } else if (exec_flags & EXEC_FLAG_SOURCE_IS_READER) { + lex = mp_lexer_new(MP_QSTR__lt_stdin_gt_, *(mp_reader_t *)source); } else if (exec_flags & EXEC_FLAG_SOURCE_IS_FILENAME) { lex = mp_lexer_new_from_file(source); } else { @@ -122,6 +125,12 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input // uncaught exception mp_hal_set_interrupt_char(-1); // disable interrupt mp_handle_pending(false); // clear any pending exceptions (and run any callbacks) + + if (exec_flags & EXEC_FLAG_SOURCE_IS_READER) { + const mp_reader_t *reader = source; + reader->close(reader->data); + } + // print EOF after normal output if (exec_flags & EXEC_FLAG_PRINT_EOF) { mp_hal_stdout_tx_strn("\x04", 1); @@ -170,6 +179,99 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input } #if MICROPY_ENABLE_COMPILER + +// This can be configured by a port (and even configured to a function to be +// computed dynamically) to indicate the maximum number of bytes that can be +// held in the stdin buffer. +#ifndef MICROPY_REPL_STDIN_BUFFER_MAX +#define MICROPY_REPL_STDIN_BUFFER_MAX (256) +#endif + +typedef struct _mp_reader_stdin_t { + bool eof; + uint16_t window_max; + uint16_t window_remain; +} mp_reader_stdin_t; + +STATIC mp_uint_t mp_reader_stdin_readbyte(void *data) { + mp_reader_stdin_t *reader = (mp_reader_stdin_t *)data; + + if (reader->eof) { + return MP_READER_EOF; + } + + int c = mp_hal_stdin_rx_chr(); + + if (c == CHAR_CTRL_C || c == CHAR_CTRL_D) { + reader->eof = true; + mp_hal_stdout_tx_strn("\x04", 1); // indicate end to host + if (c == CHAR_CTRL_C) { + #if MICROPY_KBD_EXCEPTION + MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; + nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); + #else + mp_raise_type(&mp_type_KeyboardInterrupt); + #endif + } else { + return MP_READER_EOF; + } + } + + if (--reader->window_remain == 0) { + mp_hal_stdout_tx_strn("\x01", 1); // indicate window available to host + reader->window_remain = reader->window_max; + } + + return c; +} + +STATIC void mp_reader_stdin_close(void *data) { + mp_reader_stdin_t *reader = (mp_reader_stdin_t *)data; + if (!reader->eof) { + reader->eof = true; + mp_hal_stdout_tx_strn("\x04", 1); // indicate end to host + for (;;) { + int c = mp_hal_stdin_rx_chr(); + if (c == CHAR_CTRL_C || c == CHAR_CTRL_D) { + break; + } + } + } +} + +STATIC void mp_reader_new_stdin(mp_reader_t *reader, mp_reader_stdin_t *reader_stdin, uint16_t buf_max) { + // Make flow-control window half the buffer size, and indicate to the host that 2x windows are + // free (sending the window size implicitly indicates that a window is free, and then the 0x01 + // indicates that another window is free). + size_t window = buf_max / 2; + char reply[3] = { window & 0xff, window >> 8, 0x01 }; + mp_hal_stdout_tx_strn(reply, sizeof(reply)); + + reader_stdin->eof = false; + reader_stdin->window_max = window; + reader_stdin->window_remain = window; + reader->data = reader_stdin; + reader->readbyte = mp_reader_stdin_readbyte; + reader->close = mp_reader_stdin_close; +} + +STATIC int do_reader_stdin(int c) { + if (c != 'A') { + // Unsupported command. + mp_hal_stdout_tx_strn("R\x00", 2); + return 0; + } + + // Indicate reception of command. + mp_hal_stdout_tx_strn("R\x01", 2); + + mp_reader_t reader; + mp_reader_stdin_t reader_stdin; + mp_reader_new_stdin(&reader, &reader_stdin, MICROPY_REPL_STDIN_BUFFER_MAX); + int exec_flags = EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_READER; + return parse_compile_execute(&reader, MP_PARSE_FILE_INPUT, exec_flags); +} + #if MICROPY_REPL_EVENT_DRIVEN typedef struct _repl_t { @@ -203,6 +305,13 @@ void pyexec_event_repl_init(void) { STATIC int pyexec_raw_repl_process_char(int c) { if (c == CHAR_CTRL_A) { // reset raw REPL + if (vstr_len(MP_STATE_VM(repl_line)) == 2 && vstr_str(MP_STATE_VM(repl_line))[0] == CHAR_CTRL_E) { + int ret = do_reader_stdin(vstr_str(MP_STATE_VM(repl_line))[1]); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; + } + goto reset; + } mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n"); goto reset; } else if (c == CHAR_CTRL_B) { @@ -388,6 +497,15 @@ int pyexec_raw_repl(void) { int c = mp_hal_stdin_rx_chr(); if (c == CHAR_CTRL_A) { // reset raw REPL + if (vstr_len(&line) == 2 && vstr_str(&line)[0] == CHAR_CTRL_E) { + int ret = do_reader_stdin(vstr_str(&line)[1]); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; + } + vstr_reset(&line); + mp_hal_stdout_tx_str(">"); + continue; + } goto raw_repl_reset; } else if (c == CHAR_CTRL_B) { // change to friendly REPL From a59282b9bfb6928cd68b696258c0dd2280244eb3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Oct 2020 23:58:02 +1100 Subject: [PATCH 0433/3533] tools/pyboard.py: Add fast raw-paste mode. This commit adds support to pyboard.py for the new raw REPL paste mode. Note that this new pyboard.py is fully backwards compatible with old devices (it detects if the device supports the new raw REPL paste mode). Signed-off-by: Damien George --- tools/pyboard.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index c97ddbe487be3..3ecdf03f156f7 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -253,6 +253,7 @@ def inWaiting(self): class Pyboard: def __init__(self, device, baudrate=115200, user="micro", password="python", wait=0): + self.use_raw_paste = True if device.startswith("exec:"): self.serial = ProcessToSerial(device[len("exec:") :]) elif device.startswith("execpty:"): @@ -359,6 +360,41 @@ def follow(self, timeout, data_consumer=None): # return normal and error output return data, data_err + def raw_paste_write(self, command_bytes): + # Read initial header, with window size. + data = self.serial.read(2) + window_size = data[0] | data[1] << 8 + window_remain = window_size + + # Write out the command_bytes data. + i = 0 + while i < len(command_bytes): + while window_remain == 0 or self.serial.inWaiting(): + data = self.serial.read(1) + if data == b"\x01": + # Device indicated that a new window of data can be sent. + window_remain += window_size + elif data == b"\x04": + # Device indicated abrupt end. Acknowledge it and finish. + self.serial.write(b"\x04") + return + else: + # Unexpected data from device. + raise PyboardError("unexpected read during raw paste: {}".format(data)) + # Send out as much data as possible that fits within the allowed window. + b = command_bytes[i : min(i + window_remain, len(command_bytes))] + self.serial.write(b) + window_remain -= len(b) + i += len(b) + + # Indicate end of data. + self.serial.write(b"\x04") + + # Wait for device to acknowledge end of data. + data = self.read_until(1, b"\x04") + if not data.endswith(b"\x04"): + raise PyboardError("could not complete raw paste: {}".format(data)) + def exec_raw_no_follow(self, command): if isinstance(command, bytes): command_bytes = command @@ -370,7 +406,26 @@ def exec_raw_no_follow(self, command): if not data.endswith(b">"): raise PyboardError("could not enter raw repl") - # write command + if self.use_raw_paste: + # Try to enter raw-paste mode. + self.serial.write(b"\x05A\x01") + data = self.serial.read(2) + if data == b"R\x00": + # Device understood raw-paste command but doesn't support it. + pass + elif data == b"R\x01": + # Device supports raw-paste mode, write out the command using this mode. + return self.raw_paste_write(command_bytes) + else: + # Device doesn't support raw-paste, fall back to normal raw REPL. + data = self.read_until(1, b"w REPL; CTRL-B to exit\r\n>") + if not data.endswith(b"w REPL; CTRL-B to exit\r\n>"): + print(data) + raise PyboardError("could not enter raw repl") + # Don't try to use raw-paste mode again for this connection. + self.use_raw_paste = False + + # Write command using standard raw REPL, 256 bytes every 10ms. for i in range(0, len(command_bytes), 256): self.serial.write(command_bytes[i : min(i + 256, len(command_bytes))]) time.sleep(0.01) From a14ca31e8579a07f263bca0dd4b0dd03f43befa2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 11:34:23 +1100 Subject: [PATCH 0434/3533] docs/reference/repl.rst: Add information about new raw-paste mode. Signed-off-by: Damien George --- docs/reference/repl.rst | 100 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/docs/reference/repl.rst b/docs/reference/repl.rst index 06ba918117f33..55f76ee1a03b9 100644 --- a/docs/reference/repl.rst +++ b/docs/reference/repl.rst @@ -196,17 +196,105 @@ So you can use the underscore to save the result in a variable. For example: 15 >>> -Raw mode --------- +Raw mode and raw-paste mode +--------------------------- -Raw mode is not something that a person would normally use. It is intended for -programmatic use. It essentially behaves like paste mode with echo turned off. +Raw mode (also called raw REPL) is not something that a person would normally use. +It is intended for programmatic use and essentially behaves like paste mode with +echo turned off, and with optional flow control. Raw mode is entered using Ctrl-A. You then send your python code, followed by a Ctrl-D. The Ctrl-D will be acknowledged by 'OK' and then the python code will be compiled and executed. Any output (or errors) will be sent back. Entering Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL. -The ``tools/pyboard.py`` program uses the raw REPL to execute python files on the -MicroPython board. +Raw-paste mode is an additional mode within the raw REPL that includes flow control, +and which compiles code as it receives it. This makes it more robust for high-speed +transfer of code into the device, and it also uses less RAM when receiving because +it does not need to store a verbatim copy of the code before compiling (unlike +standard raw mode). +Raw-paste mode uses the following protocol: + +#. Enter raw REPL as usual via ctrl-A. + +#. Write 3 bytes: ``b"\x05A\x01"`` (ie ctrl-E then "A" then ctrl-A). + +#. Read 2 bytes to determine if the device entered raw-paste mode: + + * If the result is ``b"R\x00"`` then the device understands the command but + doesn't support raw paste. + + * If the result is ``b"R\x01"`` then the device does support raw paste and + has entered this mode. + + * Otherwise the result should be ``b"ra"`` and the device doesn't support raw + paste and the string ``b"w REPL; CTRL-B to exit\r\n>"`` should be read and + discarded. + +#. If the device is in raw-paste mode then continue, otherwise fallback to + standard raw mode. + +#. Read 2 bytes, this is the flow control window-size-increment (in bytes) + stored as a 16-bit unsigned little endian integer. The initial value for the + remaining-window-size variable should be set to this number. + +#. Write out the code to the device: + + * While there are bytes to send, write up to the remaining-window-size worth + of bytes, and decrease the remaining-window-size by the number of bytes + written. + + * If the remaining-window-size is 0, or there is a byte waiting to read, read + 1 byte. If this byte is ``b"\x01"`` then increase the remaining-window-size + by the window-size-increment from step 5. If this byte is ``b"\x04"`` then + the device wants to end the data reception, and ``b"\x04"`` should be + written to the device and no more code sent after that. (Note: if there is + a byte waiting to be read from the device then it does not need to be read + and acted upon immediately, the device will continue to consume incoming + bytes as long as reamining-window-size is greater than 0.) + +#. When all code has been written to the device, write ``b"\x04"`` to indicate + end-of-data. + +#. Read from the device until ``b"\x04"`` is received. At this point the device + has received and compiled all of the code that was sent and is executing it. + +#. The device outputs any characters produced by the executing code. When (if) + the code finishes ``b"\x04"`` will be output, followed by any exception that + was uncaught, followed again by ``b"\x04"``. It then goes back to the + standard raw REPL and outputs ``b">"``. + +For example, starting at a new line at the normal (friendly) REPL, if you write:: + + b"\x01\x05A\x01print(123)\x04" + +Then the device will respond with something like:: + + b"\r\nraw REPL; CTRL-B to exit\r\n>R\x01\x80\x00\x01\x04123\r\n\x04\x04>" + +Broken down over time this looks like:: + + # Step 1: enter raw REPL + write: b"\x01" + read: b"\r\nraw REPL; CTRL-B to exit\r\n>" + + # Step 2-5: enter raw-paste mode + write: b"\x05A\x01" + read: b"R\x01\x80\x00\x01" + + # Step 6-8: write out code + write: b"print(123)\x04" + read: b"\x04" + + # Step 9: code executes and result is read + read: b"123\r\n\x04\x04>" + +In this case the flow control window-size-increment is 128 and there are two +windows worth of data immediately available at the start, one from the initial +window-size-increment value and one from the explicit ``b"\x01"`` value that +is sent. So this means up to 256 bytes can be written to begin with before +waiting or checking for more incoming flow-control characters. + +The ``tools/pyboard.py`` program uses the raw REPL, including raw-paste mode, to +execute Python code on a MicroPython-enabled board. From ca40eb0fdadd5a963b9a065999b092f029a598d5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 17:38:19 +1100 Subject: [PATCH 0435/3533] extmod/uasyncio: Delay calling Loop.call_exception_handler by 1 loop. When a tasks raises an exception which is uncaught, and no other task await's on that task, then an error message is printed (or a user function called) via a call to Loop.call_exception_handler. In CPython this call is made when the Task object is freed (eg via reference counting) because it's at that point that it is known that the exception that was raised will never be handled. MicroPython does not have reference counting and the current behaviour is to deal with uncaught exceptions as early as possible, ie as soon as they terminate the task. But this can be undesirable because in certain cases a task can start and raise an exception immediately (before any await is executed in that task's coro) and before any other task gets a chance to await on it to catch the exception. This commit changes the behaviour so that tasks which end due to an uncaught exception are scheduled one more time for execution, and if they are not await'ed on by the next scheduling loop, then the exception handler is called (eg the exception is printed out). Signed-off-by: Damien George --- extmod/moduasyncio.c | 39 +++++++++++++++++-- extmod/uasyncio/core.py | 16 ++++---- extmod/uasyncio/funcs.py | 4 +- extmod/uasyncio/task.py | 19 +++++++-- .../extmod/uasyncio_set_exception_handler.py | 12 +++++- .../uasyncio_set_exception_handler.py.exp | 1 + 6 files changed, 73 insertions(+), 18 deletions(-) diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index 0b15c9e0790a5..e8822c069741b 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -146,6 +146,9 @@ STATIC const mp_obj_type_t task_queue_type = { /******************************************************************************/ // Task class +// For efficiency, the task object is stored to the coro entry when the task is done. +#define TASK_IS_DONE(task) ((task)->coro == MP_OBJ_FROM_PTR(task)) + // This is the core uasyncio context with cur_task, _task_queue and CancelledError. STATIC mp_obj_t uasyncio_context = MP_OBJ_NULL; @@ -167,7 +170,7 @@ STATIC mp_obj_t task_make_new(const mp_obj_type_t *type, size_t n_args, size_t n STATIC mp_obj_t task_cancel(mp_obj_t self_in) { mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); // Check if task is already finished. - if (self->coro == mp_const_none) { + if (TASK_IS_DONE(self)) { return mp_const_false; } // Can't cancel self (not supported yet). @@ -209,6 +212,24 @@ STATIC mp_obj_t task_cancel(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_cancel_obj, task_cancel); +STATIC mp_obj_t task_throw(mp_obj_t self_in, mp_obj_t value_in) { + // This task raised an exception which was uncaught; handle that now. + mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); + // Set the data because it was cleared by the main scheduling loop. + self->data = value_in; + if (self->waiting == mp_const_none) { + // Nothing await'ed on the task so call the exception handler. + mp_obj_t _exc_context = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR__exc_context)); + mp_obj_dict_store(_exc_context, MP_OBJ_NEW_QSTR(MP_QSTR_exception), value_in); + mp_obj_dict_store(_exc_context, MP_OBJ_NEW_QSTR(MP_QSTR_future), self_in); + mp_obj_t Loop = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_Loop)); + mp_obj_t call_exception_handler = mp_load_attr(Loop, MP_QSTR_call_exception_handler); + mp_call_function_1(call_exception_handler, _exc_context); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(task_throw_obj, task_throw); + STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); if (dest[0] == MP_OBJ_NULL) { @@ -218,12 +239,15 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } else if (attr == MP_QSTR_data) { dest[0] = self->data; } else if (attr == MP_QSTR_waiting) { - if (self->waiting != mp_const_none) { + if (self->waiting != mp_const_none && self->waiting != mp_const_false) { dest[0] = self->waiting; } } else if (attr == MP_QSTR_cancel) { dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj); dest[1] = self_in; + } else if (attr == MP_QSTR_throw) { + dest[0] = MP_OBJ_FROM_PTR(&task_throw_obj); + dest[1] = self_in; } else if (attr == MP_QSTR_ph_key) { dest[0] = self->ph_key; } @@ -246,14 +270,21 @@ STATIC mp_obj_t task_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { (void)iter_buf; mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); if (self->waiting == mp_const_none) { - self->waiting = task_queue_make_new(&task_queue_type, 0, 0, NULL); + // The is the first access of the "waiting" entry. + if (TASK_IS_DONE(self)) { + // Signal that the completed-task has been await'ed on. + self->waiting = mp_const_false; + } else { + // Lazily allocate the waiting queue. + self->waiting = task_queue_make_new(&task_queue_type, 0, 0, NULL); + } } return self_in; } STATIC mp_obj_t task_iternext(mp_obj_t self_in) { mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); - if (self->coro == mp_const_none) { + if (TASK_IS_DONE(self)) { // Task finished, raise return value to caller so it can continue. nlr_raise(self->data); } else { diff --git a/extmod/uasyncio/core.py b/extmod/uasyncio/core.py index 045b4cd139182..6a84b0982c41f 100644 --- a/extmod/uasyncio/core.py +++ b/extmod/uasyncio/core.py @@ -185,8 +185,6 @@ def run_until_complete(main_task=None): if isinstance(er, StopIteration): return er.value raise er - # Save return value of coro to pass up to caller - t.data = er # Schedule any other tasks waiting on the completion of this task waiting = False if hasattr(t, "waiting"): @@ -194,13 +192,15 @@ def run_until_complete(main_task=None): _task_queue.push_head(t.waiting.pop_head()) waiting = True t.waiting = None # Free waiting queue head - # Print out exception for detached tasks if not waiting and not isinstance(er, excs_stop): - _exc_context["exception"] = er - _exc_context["future"] = t - Loop.call_exception_handler(_exc_context) - # Indicate task is done - t.coro = None + # An exception ended this detached task, so queue it for later + # execution to handle the uncaught exception if no other task retrieves + # the exception in the meantime (this is handled by Task.throw). + _task_queue.push_head(t) + # Indicate task is done by setting coro to the task object itself + t.coro = t + # Save return value of coro to pass up to caller + t.data = er # Create a new task from a coroutine and run it until it finishes diff --git a/extmod/uasyncio/funcs.py b/extmod/uasyncio/funcs.py index 6e1305c94f418..d306752312c6e 100644 --- a/extmod/uasyncio/funcs.py +++ b/extmod/uasyncio/funcs.py @@ -21,9 +21,9 @@ def cancel(aw, timeout, sleep): pass finally: # Cancel the "cancel" task if it's still active (optimisation instead of cancel_task.cancel()) - if cancel_task.coro is not None: + if cancel_task.coro is not cancel_task: core._task_queue.remove(cancel_task) - if cancel_task.coro is None: + if cancel_task.coro is cancel_task: # Cancel task ran to completion, ie there was a timeout raise core.TimeoutError return ret diff --git a/extmod/uasyncio/task.py b/extmod/uasyncio/task.py index 1788cf0ed0486..2420ab7193159 100644 --- a/extmod/uasyncio/task.py +++ b/extmod/uasyncio/task.py @@ -130,13 +130,16 @@ def __init__(self, coro, globals=None): self.ph_rightmost_parent = None # Paring heap def __iter__(self): - if not hasattr(self, "waiting"): + if self.coro is self: + # Signal that the completed-task has been await'ed on. + self.waiting = None + elif not hasattr(self, "waiting"): # Lazily allocated head of linked list of Tasks waiting on completion of this task. self.waiting = TaskQueue() return self def __next__(self): - if not self.coro: + if self.coro is self: # Task finished, raise return value to caller so it can continue. raise self.data else: @@ -147,7 +150,7 @@ def __next__(self): def cancel(self): # Check if task is already finished. - if self.coro is None: + if self.coro is self: return False # Can't cancel self (not supported yet). if self is core.cur_task: @@ -166,3 +169,13 @@ def cancel(self): core._task_queue.push_head(self) self.data = core.CancelledError return True + + def throw(self, value): + # This task raised an exception which was uncaught; handle that now. + # Set the data because it was cleared by the main scheduling loop. + self.data = value + if not hasattr(self, "waiting"): + # Nothing await'ed on the task so call the exception handler. + core._exc_context["exception"] = value + core._exc_context["future"] = self + core.Loop.call_exception_handler(core._exc_context) diff --git a/tests/extmod/uasyncio_set_exception_handler.py b/tests/extmod/uasyncio_set_exception_handler.py index ad62a79b7b7a8..fe7b83eb4d81a 100644 --- a/tests/extmod/uasyncio_set_exception_handler.py +++ b/tests/extmod/uasyncio_set_exception_handler.py @@ -32,13 +32,23 @@ async def main(): # Create a task that raises and uses the custom exception handler asyncio.create_task(task(0)) print("sleep") - await asyncio.sleep(0) + for _ in range(2): + await asyncio.sleep(0) # Create 2 tasks to test order of printing exception asyncio.create_task(task(1)) asyncio.create_task(task(2)) print("sleep") + for _ in range(2): + await asyncio.sleep(0) + + # Create a task, let it run, then await it (no exception should be printed) + t = asyncio.create_task(task(3)) await asyncio.sleep(0) + try: + await t + except ValueError as er: + print(repr(er)) print("done") diff --git a/tests/extmod/uasyncio_set_exception_handler.py.exp b/tests/extmod/uasyncio_set_exception_handler.py.exp index 4744641e54c3c..fb4711469dc5b 100644 --- a/tests/extmod/uasyncio_set_exception_handler.py.exp +++ b/tests/extmod/uasyncio_set_exception_handler.py.exp @@ -5,4 +5,5 @@ custom_handler ValueError(0, 1) sleep custom_handler ValueError(1, 2) custom_handler ValueError(2, 3) +ValueError(3, 4) done From 309dfe39e07d3c3c8ba873ed09116cea9f6f52c0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Nov 2020 17:38:30 +1100 Subject: [PATCH 0436/3533] extmod/uasyncio: Add Task.done() method. This is added because task.coro==None is no longer the way to detect if a task is finished. Providing a (CPython compatible) function for this allows the implementation to be abstracted away. Signed-off-by: Damien George --- extmod/moduasyncio.c | 9 ++++ extmod/uasyncio/task.py | 3 ++ tests/extmod/uasyncio_task_done.py | 66 ++++++++++++++++++++++++++ tests/extmod/uasyncio_task_done.py.exp | 24 ++++++++++ 4 files changed, 102 insertions(+) create mode 100644 tests/extmod/uasyncio_task_done.py create mode 100644 tests/extmod/uasyncio_task_done.py.exp diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index e8822c069741b..fe0f748cac986 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -167,6 +167,12 @@ STATIC mp_obj_t task_make_new(const mp_obj_type_t *type, size_t n_args, size_t n return MP_OBJ_FROM_PTR(self); } +STATIC mp_obj_t task_done(mp_obj_t self_in) { + mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(TASK_IS_DONE(self)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_done_obj, task_done); + STATIC mp_obj_t task_cancel(mp_obj_t self_in) { mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); // Check if task is already finished. @@ -242,6 +248,9 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (self->waiting != mp_const_none && self->waiting != mp_const_false) { dest[0] = self->waiting; } + } else if (attr == MP_QSTR_done) { + dest[0] = MP_OBJ_FROM_PTR(&task_done_obj); + dest[1] = self_in; } else if (attr == MP_QSTR_cancel) { dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj); dest[1] = self_in; diff --git a/extmod/uasyncio/task.py b/extmod/uasyncio/task.py index 2420ab7193159..68ddf496f08a2 100644 --- a/extmod/uasyncio/task.py +++ b/extmod/uasyncio/task.py @@ -148,6 +148,9 @@ def __next__(self): # Set calling task's data to this task that it waits on, to double-link it. core.cur_task.data = self + def done(self): + return self.coro is self + def cancel(self): # Check if task is already finished. if self.coro is self: diff --git a/tests/extmod/uasyncio_task_done.py b/tests/extmod/uasyncio_task_done.py new file mode 100644 index 0000000000000..2700da8c34168 --- /dev/null +++ b/tests/extmod/uasyncio_task_done.py @@ -0,0 +1,66 @@ +# Test the Task.done() method + +try: + import uasyncio as asyncio +except ImportError: + try: + import asyncio + except ImportError: + print("SKIP") + raise SystemExit + + +async def task(t, exc=None): + print("task start") + if t >= 0: + await asyncio.sleep(t) + if exc: + raise exc + print("task done") + + +async def main(): + # Task that finishes immediately. + print("=" * 10) + t = asyncio.create_task(task(-1)) + print(t.done()) + await asyncio.sleep(0) + print(t.done()) + await t + print(t.done()) + + # Task that starts, runs and finishes. + print("=" * 10) + t = asyncio.create_task(task(0.01)) + print(t.done()) + await asyncio.sleep(0) + print(t.done()) + await t + print(t.done()) + + # Task that raises immediately. + print("=" * 10) + t = asyncio.create_task(task(-1, ValueError)) + print(t.done()) + await asyncio.sleep(0) + print(t.done()) + try: + await t + except ValueError as er: + print(repr(er)) + print(t.done()) + + # Task that raises after a delay. + print("=" * 10) + t = asyncio.create_task(task(0.01, ValueError)) + print(t.done()) + await asyncio.sleep(0) + print(t.done()) + try: + await t + except ValueError as er: + print(repr(er)) + print(t.done()) + + +asyncio.run(main()) diff --git a/tests/extmod/uasyncio_task_done.py.exp b/tests/extmod/uasyncio_task_done.py.exp new file mode 100644 index 0000000000000..ddda04c5ec43b --- /dev/null +++ b/tests/extmod/uasyncio_task_done.py.exp @@ -0,0 +1,24 @@ +========== +False +task start +task done +True +True +========== +False +task start +False +task done +True +========== +False +task start +True +ValueError() +True +========== +False +task start +False +ValueError() +True From b505971069333a373d9f0c12138b64dab83fed72 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 1 Dec 2020 14:22:16 +1100 Subject: [PATCH 0437/3533] extmod/uasyncio: Fix cancellation handling of wait_for. This commit switches the roles of the helper task from a cancellation task to a runner task, to get the correct semantics for cancellation of wait_for. Some uasyncio tests are now disabled for the native emitter due to issues with native code generation of generators and yield-from. Fixes #5797. Signed-off-by: Damien George --- extmod/uasyncio/funcs.py | 52 ++++++++++++++------ tests/extmod/uasyncio_wait_for.py | 55 +++++++++++++++++++++ tests/extmod/uasyncio_wait_for.py.exp | 20 ++++++++ tests/extmod/uasyncio_wait_for_fwd.py | 60 +++++++++++++++++++++++ tests/extmod/uasyncio_wait_for_fwd.py.exp | 26 ++++++++++ tests/run-tests | 3 ++ 6 files changed, 200 insertions(+), 16 deletions(-) create mode 100644 tests/extmod/uasyncio_wait_for_fwd.py create mode 100644 tests/extmod/uasyncio_wait_for_fwd.py.exp diff --git a/extmod/uasyncio/funcs.py b/extmod/uasyncio/funcs.py index d306752312c6e..93f4fd256cafe 100644 --- a/extmod/uasyncio/funcs.py +++ b/extmod/uasyncio/funcs.py @@ -9,24 +9,44 @@ async def wait_for(aw, timeout, sleep=core.sleep): if timeout is None: return await aw - def cancel(aw, timeout, sleep): - await sleep(timeout) - aw.cancel() + def runner(waiter, aw): + nonlocal status, result + try: + result = await aw + s = True + except BaseException as er: + s = er + if status is None: + # The waiter is still waiting, set status for it and cancel it. + status = s + waiter.cancel() + + # Run aw in a separate runner task that manages its exceptions. + status = None + result = None + runner_task = core.create_task(runner(core.cur_task, aw)) - cancel_task = core.create_task(cancel(aw, timeout, sleep)) try: - ret = await aw - except core.CancelledError: - # Ignore CancelledError from aw, it's probably due to timeout - pass - finally: - # Cancel the "cancel" task if it's still active (optimisation instead of cancel_task.cancel()) - if cancel_task.coro is not cancel_task: - core._task_queue.remove(cancel_task) - if cancel_task.coro is cancel_task: - # Cancel task ran to completion, ie there was a timeout - raise core.TimeoutError - return ret + # Wait for the timeout to elapse. + await sleep(timeout) + except core.CancelledError as er: + if status is True: + # aw completed successfully and cancelled the sleep, so return aw's result. + return result + elif status is None: + # This wait_for was cancelled externally, so cancel aw and re-raise. + status = True + runner_task.cancel() + raise er + else: + # aw raised an exception, propagate it out to the caller. + raise status + + # The sleep finished before aw, so cancel aw and raise TimeoutError. + status = True + runner_task.cancel() + await runner_task + raise core.TimeoutError def wait_for_ms(aw, timeout): diff --git a/tests/extmod/uasyncio_wait_for.py b/tests/extmod/uasyncio_wait_for.py index 92fd174b846db..9612d16204043 100644 --- a/tests/extmod/uasyncio_wait_for.py +++ b/tests/extmod/uasyncio_wait_for.py @@ -31,30 +31,85 @@ async def task_raise(): raise ValueError +async def task_cancel_other(t, other): + print("task_cancel_other start") + await asyncio.sleep(t) + print("task_cancel_other cancel") + other.cancel() + + +async def task_wait_for_cancel(id, t, t_wait): + print("task_wait_for_cancel start") + try: + await asyncio.wait_for(task(id, t), t_wait) + except asyncio.CancelledError as er: + print("task_wait_for_cancel cancelled") + raise er + + +async def task_wait_for_cancel_ignore(t_wait): + print("task_wait_for_cancel_ignore start") + try: + await asyncio.wait_for(task_catch(), t_wait) + except asyncio.CancelledError as er: + print("task_wait_for_cancel_ignore cancelled") + raise er + + async def main(): + sep = "-" * 10 + # When task finished before the timeout print(await asyncio.wait_for(task(1, 0.01), 10)) + print(sep) # When timeout passes and task is cancelled try: print(await asyncio.wait_for(task(2, 10), 0.01)) except asyncio.TimeoutError: print("timeout") + print(sep) # When timeout passes and task is cancelled, but task ignores the cancellation request try: print(await asyncio.wait_for(task_catch(), 0.1)) except asyncio.TimeoutError: print("TimeoutError") + print(sep) # When task raises an exception try: print(await asyncio.wait_for(task_raise(), 1)) except ValueError: print("ValueError") + print(sep) # Timeout of None means wait forever print(await asyncio.wait_for(task(3, 0.1), None)) + print(sep) + + # When task is cancelled by another task + t = asyncio.create_task(task(4, 10)) + asyncio.create_task(task_cancel_other(0.01, t)) + try: + print(await asyncio.wait_for(t, 1)) + except asyncio.CancelledError as er: + print(repr(er)) + print(sep) + + # When wait_for gets cancelled + t = asyncio.create_task(task_wait_for_cancel(4, 1, 2)) + await asyncio.sleep(0.01) + t.cancel() + await asyncio.sleep(0.01) + print(sep) + + # When wait_for gets cancelled and awaited task ignores the cancellation request + t = asyncio.create_task(task_wait_for_cancel_ignore(2)) + await asyncio.sleep(0.01) + t.cancel() + await asyncio.sleep(0.01) + print(sep) print("finish") diff --git a/tests/extmod/uasyncio_wait_for.py.exp b/tests/extmod/uasyncio_wait_for.py.exp index 41a5f2481e908..a4201d31ffdb8 100644 --- a/tests/extmod/uasyncio_wait_for.py.exp +++ b/tests/extmod/uasyncio_wait_for.py.exp @@ -1,15 +1,35 @@ task start 1 task end 1 2 +---------- task start 2 timeout +---------- task_catch start ignore cancel task_catch done TimeoutError +---------- task start ValueError +---------- task start 3 task end 3 6 +---------- +task start 4 +task_cancel_other start +task_cancel_other cancel +CancelledError() +---------- +task_wait_for_cancel start +task start 4 +task_wait_for_cancel cancelled +---------- +task_wait_for_cancel_ignore start +task_catch start +task_wait_for_cancel_ignore cancelled +ignore cancel +task_catch done +---------- finish diff --git a/tests/extmod/uasyncio_wait_for_fwd.py b/tests/extmod/uasyncio_wait_for_fwd.py new file mode 100644 index 0000000000000..33738085ce4a5 --- /dev/null +++ b/tests/extmod/uasyncio_wait_for_fwd.py @@ -0,0 +1,60 @@ +# Test asyncio.wait_for, with forwarding cancellation + +try: + import uasyncio as asyncio +except ImportError: + try: + import asyncio + except ImportError: + print("SKIP") + raise SystemExit + + +async def awaiting(t, return_if_fail): + try: + print("awaiting started") + await asyncio.sleep(t) + except asyncio.CancelledError as er: + # CPython wait_for raises CancelledError inside task but TimeoutError in wait_for + print("awaiting canceled") + if return_if_fail: + return False # return has no effect if Cancelled + else: + raise er + except Exception as er: + print("caught exception", er) + raise er + + +async def test_cancellation_forwarded(catch, catch_inside): + print("----------") + + async def wait(): + try: + await asyncio.wait_for(awaiting(2, catch_inside), 1) + except asyncio.TimeoutError as er: + print("Got timeout error") + raise er + except asyncio.CancelledError as er: + print("Got canceled") + if not catch: + raise er + + async def cancel(t): + print("cancel started") + await asyncio.sleep(0.01) + print("cancel wait()") + t.cancel() + + t = asyncio.create_task(wait()) + k = asyncio.create_task(cancel(t)) + try: + await t + except asyncio.CancelledError: + print("waiting got cancelled") + + +asyncio.run(test_cancellation_forwarded(False, False)) +asyncio.run(test_cancellation_forwarded(False, True)) +asyncio.run(test_cancellation_forwarded(True, True)) +asyncio.run(test_cancellation_forwarded(True, False)) diff --git a/tests/extmod/uasyncio_wait_for_fwd.py.exp b/tests/extmod/uasyncio_wait_for_fwd.py.exp new file mode 100644 index 0000000000000..9f22f1a7d1e88 --- /dev/null +++ b/tests/extmod/uasyncio_wait_for_fwd.py.exp @@ -0,0 +1,26 @@ +---------- +cancel started +awaiting started +cancel wait() +Got canceled +awaiting canceled +waiting got cancelled +---------- +cancel started +awaiting started +cancel wait() +Got canceled +awaiting canceled +waiting got cancelled +---------- +cancel started +awaiting started +cancel wait() +Got canceled +awaiting canceled +---------- +cancel started +awaiting started +cancel wait() +Got canceled +awaiting canceled diff --git a/tests/run-tests b/tests/run-tests index 23c30d3c3cb55..cb5b5cd0a5676 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -434,7 +434,10 @@ def run_tests(pyb, tests, args, result_dir): skip_tests.add('basics/scope_implicit.py') # requires checking for unbound local skip_tests.add('basics/try_finally_return2.py') # requires raise_varargs skip_tests.add('basics/unboundlocal.py') # requires checking for unbound local + skip_tests.add('extmod/uasyncio_event.py') # unknown issue skip_tests.add('extmod/uasyncio_lock.py') # requires async with + skip_tests.add('extmod/uasyncio_micropython.py') # unknown issue + skip_tests.add('extmod/uasyncio_wait_for.py') # unknown issue skip_tests.add('misc/features.py') # requires raise_varargs skip_tests.add('misc/print_exception.py') # because native doesn't have proper traceback info skip_tests.add('misc/sys_exc_info.py') # sys.exc_info() is not supported for native From f2a9a0ac413cc337ccd78bbb26cd67cee7b1e210 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 25 Nov 2020 09:28:41 +1100 Subject: [PATCH 0438/3533] extmod/nimble: Fail read if the characteristic is too big. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index bbff26b5354e9..e4a048e3d3df4 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -608,7 +608,9 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, return BLE_ATT_ERR_ATTR_NOT_FOUND; } - os_mbuf_append(ctxt->om, entry->data, entry->data_len); + if (os_mbuf_append(ctxt->om, entry->data, entry->data_len)) { + return BLE_ATT_ERR_INSUFFICIENT_RES; + } return 0; case BLE_GATT_ACCESS_OP_WRITE_CHR: @@ -625,6 +627,8 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, entry->data_len = MIN(entry->data_alloc, OS_MBUF_PKTLEN(ctxt->om) + offset); os_mbuf_copydata(ctxt->om, 0, entry->data_len - offset, entry->data + offset); + // TODO: Consider failing with BLE_ATT_ERR_INSUFFICIENT_RES if the buffer is full. + mp_bluetooth_gatts_on_write(conn_handle, value_handle); return 0; From c70665fb0bc1b4d755abda422d221cc0a72e91da Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Sat, 14 Nov 2020 19:40:25 +1100 Subject: [PATCH 0439/3533] extmod/modbluetooth: Add _IRQ_CONNECTION_UPDATE event. This allows the application to be notified of changes to the connection interval, connection latency and supervision timeout. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 40 ++++++++++++++++++--------- extmod/modbluetooth.c | 21 ++++++++++++++ extmod/modbluetooth.h | 5 ++++ extmod/nimble/modbluetooth_nimble.c | 25 +++++++++++++---- 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 97dd2cbb53f5c..9faae26e46929 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -295,20 +295,34 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t if (event_type == HCI_EVENT_LE_META) { DEBUG_printf(" --> hci le meta\n"); - if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) { - uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); - uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet); - bd_addr_t addr; - hci_subevent_le_connection_complete_get_peer_address(packet, addr); - uint16_t irq_event; - if (hci_subevent_le_connection_complete_get_role(packet) == 0) { - // Master role. - irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT; - } else { - // Slave role. - irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT; + switch (hci_event_le_meta_get_subevent_code(packet)) { + case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: { + uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); + uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet); + bd_addr_t addr; + hci_subevent_le_connection_complete_get_peer_address(packet, addr); + uint16_t irq_event; + if (hci_subevent_le_connection_complete_get_role(packet) == 0) { + // Master role. + irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT; + } else { + // Slave role. + irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT; + } + mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, addr_type, addr); + break; + } + case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: { + uint8_t status = hci_subevent_le_connection_update_complete_get_status(packet); + uint16_t conn_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); + uint16_t conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); + uint16_t conn_latency = hci_subevent_le_connection_update_complete_get_conn_latency(packet); + uint16_t supervision_timeout = hci_subevent_le_connection_update_complete_get_supervision_timeout(packet); + DEBUG_printf("- LE Connection %04x: connection update - connection interval %u.%02u ms, latency %u, timeout %u\n", + conn_handle, conn_interval * 125 / 100, 25 * (conn_interval & 3), conn_latency, supervision_timeout); + mp_bluetooth_gap_on_connection_update(conn_handle, conn_interval, conn_latency, supervision_timeout, status); + break; } - mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, addr_type, addr); } } else if (event_type == BTSTACK_EVENT_STATE) { uint8_t state = btstack_event_state_get_state(packet); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 2cff386f125d5..dc1084acc13dc 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -979,6 +979,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { if (event == MP_BLUETOOTH_IRQ_CENTRAL_CONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT || event == MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT) { // conn_handle, addr_type, addr ringbuf_extract(&o->ringbuf, data_tuple, 1, 1, &o->irq_data_addr, 0, NULL, NULL); + } else if (event == MP_BLUETOOTH_IRQ_CONNECTION_UPDATE) { + // conn_handle, conn_interval, conn_latency, supervision_timeout, status + ringbuf_extract(&o->ringbuf, data_tuple, 5, 0, NULL, 0, NULL, NULL); } else if (event == MP_BLUETOOTH_IRQ_GATTS_WRITE) { // conn_handle, value_handle ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL); @@ -1093,6 +1096,11 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); } +void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status) { + uint16_t args[] = {conn_handle, conn_interval, conn_latency, supervision_timeout, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); @@ -1276,6 +1284,19 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han schedule_ringbuf(atomic_state); } +void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status) { + MICROPY_PY_BLUETOOTH_ENTER + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (enqueue_irq(o, 2 + 2 + 2 + 2 + 2, MP_BLUETOOTH_IRQ_CONNECTION_UPDATE)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, conn_interval); + ringbuf_put16(&o->ringbuf, conn_latency); + ringbuf_put16(&o->ringbuf, supervision_timeout); + ringbuf_put16(&o->ringbuf, status); + } + schedule_ringbuf(atomic_state); +} + void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 9caddb0f3f89e..2619e71894d7c 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -114,6 +114,7 @@ #define MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT (24) #define MP_BLUETOOTH_IRQ_L2CAP_RECV (25) #define MP_BLUETOOTH_IRQ_L2CAP_SEND_READY (26) +#define MP_BLUETOOTH_IRQ_CONNECTION_UPDATE (27) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -151,6 +152,7 @@ _IRQ_L2CAP_CONNECT = const(23) _IRQ_L2CAP_DISCONNECT = const(24) _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) +_IRQ_GATTS_CONN_UPDATE = const(27) */ // bluetooth.UUID type. @@ -281,6 +283,9 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf // Notify modbluetooth that a connection/disconnection event has occurred. void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr); +// Call this when any connection parameters have been changed. +void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status); + // Call this when a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e4a048e3d3df4..3983d1869082b 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -303,6 +303,19 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { } break; } + + case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: + DEBUG_printf("gap_event_cb: phy update: %d\n", event->phy_updated.tx_phy); + break; + + case BLE_GAP_EVENT_CONN_UPDATE: { + DEBUG_printf("gap_event_cb: connection update: status=%d\n", event->conn_update.status); + struct ble_gap_conn_desc desc; + if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { + mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); + } + break; + } } return 0; } @@ -938,13 +951,13 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { break; } - case BLE_GAP_EVENT_CONN_UPDATE: - // TODO - break; - - case BLE_GAP_EVENT_CONN_UPDATE_REQ: - // TODO + case BLE_GAP_EVENT_CONN_UPDATE: { + DEBUG_printf("peripheral_gap_event_cb: connection update: status=%d\n", event->conn_update.status); + if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { + mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); + } break; + } case BLE_GAP_EVENT_MTU: { if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { From 7a9aa49595e1c523a5765397db135ff8f00515b1 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 23:23:51 +1100 Subject: [PATCH 0440/3533] docs/library/ubluetooth.rst: Add _IRQ_CONNECTION_UDPATE docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 35ac13ad56fa9..73547c356aa25 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -199,6 +199,9 @@ Event Handling # A previous l2cap_send that returned False has now completed and the channel is ready to send again. # If status is non-zero, then the transmit buffer overflowed and the application should re-send the data. conn_handle, cid, status = data + elif event == _IRQ_CONNECTION_UPDATE: + # The remote device has updated connection parameters. + conn_handle, conn_interval, conn_latency, supervision_timeout, status = data The event codes are:: @@ -229,6 +232,7 @@ The event codes are:: _IRQ_L2CAP_DISCONNECT = const(24) _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) + _IRQ_CONNECTION_UPDATE = const(27) In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your From 1697ff335db523ff0809051d42871beb9c86012d Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Thu, 27 Aug 2020 09:13:25 +1000 Subject: [PATCH 0441/3533] extmod/modbluetooth: Allow setting char/desc enc/auth options. This widens the characteristic/descriptor flags to 16-bit, to allow setting encryption/authentication requirements. Sets the required flags for NimBLE and btstack implementations. The BLE.FLAG_* constants will eventually be deprecated in favour of copy and paste Python constants (like the IRQs). Signed-off-by: Jim Mussared --- examples/bluetooth/ble_simple_peripheral.py | 9 ++++- examples/bluetooth/ble_temperature.py | 6 ++- examples/bluetooth/ble_uart_peripheral.py | 7 +++- extmod/btstack/modbluetooth_btstack.c | 36 +++++++++++++---- extmod/modbluetooth.c | 8 ++-- extmod/modbluetooth.h | 44 ++++++++++++++++++--- extmod/nimble/modbluetooth_nimble.c | 6 ++- 7 files changed, 93 insertions(+), 23 deletions(-) diff --git a/examples/bluetooth/ble_simple_peripheral.py b/examples/bluetooth/ble_simple_peripheral.py index d2b134e7149e9..0ebe431764c93 100644 --- a/examples/bluetooth/ble_simple_peripheral.py +++ b/examples/bluetooth/ble_simple_peripheral.py @@ -12,14 +12,19 @@ _IRQ_CENTRAL_DISCONNECT = const(2) _IRQ_GATTS_WRITE = const(3) +_FLAG_READ = const(0x0002) +_FLAG_WRITE_NO_RESPONSE = const(0x0004) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) + _UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") _UART_TX = ( bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"), - bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY, + _FLAG_READ | _FLAG_NOTIFY, ) _UART_RX = ( bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"), - bluetooth.FLAG_WRITE | bluetooth.FLAG_WRITE_NO_RESPONSE, + _FLAG_WRITE | _FLAG_WRITE_NO_RESPONSE, ) _UART_SERVICE = ( _UART_UUID, diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py index d375a62ffb8ac..e6378ebee7964 100644 --- a/examples/bluetooth/ble_temperature.py +++ b/examples/bluetooth/ble_temperature.py @@ -15,12 +15,16 @@ _IRQ_CENTRAL_DISCONNECT = const(2) _IRQ_GATTS_INDICATE_DONE = const(20) +_FLAG_READ = const(0x0002) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + # org.bluetooth.service.environmental_sensing _ENV_SENSE_UUID = bluetooth.UUID(0x181A) # org.bluetooth.characteristic.temperature _TEMP_CHAR = ( bluetooth.UUID(0x2A6E), - bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE, + _FLAG_READ | _FLAG_NOTIFY | _FLAG_INDICATE, ) _ENV_SENSE_SERVICE = ( _ENV_SENSE_UUID, diff --git a/examples/bluetooth/ble_uart_peripheral.py b/examples/bluetooth/ble_uart_peripheral.py index 6d167a871a75e..b4e352be9f6dd 100644 --- a/examples/bluetooth/ble_uart_peripheral.py +++ b/examples/bluetooth/ble_uart_peripheral.py @@ -9,14 +9,17 @@ _IRQ_CENTRAL_DISCONNECT = const(2) _IRQ_GATTS_WRITE = const(3) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) + _UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") _UART_TX = ( bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"), - bluetooth.FLAG_NOTIFY, + _FLAG_NOTIFY, ) _UART_RX = ( bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"), - bluetooth.FLAG_WRITE, + _FLAG_WRITE, ) _UART_SERVICE = ( _UART_UUID, diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 9faae26e46929..da9a6f732bf4c 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -893,7 +893,30 @@ STATIC inline uint16_t get_uuid16(const mp_obj_bluetooth_uuid_t *uuid) { return (uuid->data[1] << 8) | uuid->data[0]; } -int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) { +// Map MP_BLUETOOTH_CHARACTERISTIC_FLAG_ values to btstack read/write permission values. +STATIC void get_characteristic_permissions(uint16_t flags, uint16_t *read_permission, uint16_t *write_permission) { + if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_ENCRYPTED) { + *read_permission = ATT_SECURITY_ENCRYPTED; + } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHENTICATED) { + *read_permission = ATT_SECURITY_AUTHENTICATED; + } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHORIZED) { + *read_permission = ATT_SECURITY_AUTHORIZED; + } else { + *read_permission = ATT_SECURITY_NONE; + } + + if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_ENCRYPTED) { + *write_permission = ATT_SECURITY_ENCRYPTED; + } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHENTICATED) { + *write_permission = ATT_SECURITY_AUTHENTICATED; + } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHORIZED) { + *write_permission = ATT_SECURITY_AUTHORIZED; + } else { + *write_permission = ATT_SECURITY_NONE; + } +} + +int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint16_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint16_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) { DEBUG_printf("mp_bluetooth_gatts_register_service\n"); // Note: btstack expects BE UUIDs (which it immediately convertes to LE). // So we have to convert all our modbluetooth LE UUIDs to BE just for the att_db_util_add_* methods (using get_uuid16 above, and reverse_128 from btstackutil.h). @@ -916,9 +939,9 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m static uint8_t cccb_buf[2] = {0}; for (size_t i = 0; i < num_characteristics; ++i) { - uint16_t props = characteristic_flags[i] | ATT_PROPERTY_DYNAMIC; - uint16_t read_permission = ATT_SECURITY_NONE; - uint16_t write_permission = ATT_SECURITY_NONE; + uint16_t props = (characteristic_flags[i] & 0x7f) | ATT_PROPERTY_DYNAMIC; + uint16_t read_permission, write_permission; + get_characteristic_permissions(characteristic_flags[i], &read_permission, &write_permission); if (characteristic_uuids[i]->type == MP_BLUETOOTH_UUID_TYPE_16) { handles[handle_index] = att_db_util_add_characteristic_uuid16(get_uuid16(characteristic_uuids[i]), props, read_permission, write_permission, NULL, 0); } else if (characteristic_uuids[i]->type == MP_BLUETOOTH_UUID_TYPE_128) { @@ -942,9 +965,8 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m ++handle_index; for (size_t j = 0; j < num_descriptors[i]; ++j) { - props = descriptor_flags[descriptor_index] | ATT_PROPERTY_DYNAMIC; - read_permission = ATT_SECURITY_NONE; - write_permission = ATT_SECURITY_NONE; + props = (descriptor_flags[descriptor_index] & 0x7f) | ATT_PROPERTY_DYNAMIC; + get_characteristic_permissions(descriptor_flags[descriptor_index], &read_permission, &write_permission); if (descriptor_uuids[descriptor_index]->type == MP_BLUETOOTH_UUID_TYPE_16) { handles[handle_index] = att_db_util_add_descriptor_uuid16(get_uuid16(descriptor_uuids[descriptor_index]), props, read_permission, write_permission, NULL, 0); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index dc1084acc13dc..1e67c8ce32749 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -461,11 +461,11 @@ STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t character // Lists of characteristic uuids and flags. mp_obj_bluetooth_uuid_t **characteristic_uuids = m_new(mp_obj_bluetooth_uuid_t *, len); - uint8_t *characteristic_flags = m_new(uint8_t, len); + uint16_t *characteristic_flags = m_new(uint16_t, len); // Flattened list of descriptor uuids and flags. Grows (realloc) as more descriptors are encountered. mp_obj_bluetooth_uuid_t **descriptor_uuids = NULL; - uint8_t *descriptor_flags = NULL; + uint16_t *descriptor_flags = NULL; // How many descriptors in the flattened list per characteristic. uint8_t *num_descriptors = m_new(uint8_t, len); @@ -506,7 +506,7 @@ STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t character // Grow the flattened uuids and flags arrays with this many more descriptors. descriptor_uuids = m_renew(mp_obj_bluetooth_uuid_t *, descriptor_uuids, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); - descriptor_flags = m_renew(uint8_t, descriptor_flags, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); + descriptor_flags = m_renew(uint16_t, descriptor_flags, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); // Also grow the handles array. *handles = m_renew(uint16_t, *handles, *num_handles, *num_handles + num_descriptors[characteristic_index]); @@ -894,6 +894,8 @@ STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluetooth) }, { MP_ROM_QSTR(MP_QSTR_BLE), MP_ROM_PTR(&mp_type_bluetooth_ble) }, { MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&mp_type_bluetooth_uuid) }, + + // TODO: Deprecate these flags (recommend copying the constants from modbluetooth.h instead). { MP_ROM_QSTR(MP_QSTR_FLAG_READ), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ) }, { MP_ROM_QSTR(MP_QSTR_FLAG_WRITE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE) }, { MP_ROM_QSTR(MP_QSTR_FLAG_NOTIFY), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) }, diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 2619e71894d7c..0d4296626b589 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -71,12 +71,28 @@ // Advertisement packet lengths #define MP_BLUETOOTH_GAP_ADV_MAX_LEN (32) +// Basic characteristic/descriptor flags. // These match the spec values for these flags so can be passed directly to the stack. -#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ (1 << 1) -#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE (1 << 2) -#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE (1 << 3) -#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY (1 << 4) -#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE (1 << 5) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_BROADCAST (0x0001) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ (0x0002) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE (0x0004) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE (0x0008) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY (0x0010) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE (0x0020) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_AUTHENTICATED_SIGNED_WRITE (0x0040) + +// TODO: NimBLE and BlueKitchen disagree on this one. +// #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_RELIABLE_WRITE (0x0080) + +// Extended flags for security and privacy. +// These match NimBLE but might require mapping in the bindings for other stacks. +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_AUX_WRITE (0x0100) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_ENCRYPTED (0x0200) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHENTICATED (0x0400) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHORIZED (0x0800) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_ENCRYPTED (0x1000) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHENTICATED (0x2000) +#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHORIZED (0x4000) // For mp_bluetooth_gattc_write, the mode parameter #define MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE (0) @@ -153,6 +169,22 @@ _IRQ_L2CAP_DISCONNECT = const(24) _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) _IRQ_GATTS_CONN_UPDATE = const(27) + +_FLAG_BROADCAST = const(0x0001) +_FLAG_READ = const(0x0002) +_FLAG_WRITE_NO_RESPONSE = const(0x0004) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) +_FLAG_AUTHENTICATED_SIGNED_WRITE = const(0x0040) + +_FLAG_AUX_WRITE = const(0x0100) +_FLAG_READ_ENCRYPTED = const(0x0200) +_FLAG_READ_AUTHENTICATED = const(0x0400) +_FLAG_READ_AUTHORIZED = const(0x0800) +_FLAG_WRITE_ENCRYPTED = const(0x1000) +_FLAG_WRITE_AUTHENTICATED = const(0x2000) +_FLAG_WRITE_AUTHORIZED = const(0x4000) */ // bluetooth.UUID type. @@ -214,7 +246,7 @@ void mp_bluetooth_gap_advertise_stop(void); int mp_bluetooth_gatts_register_service_begin(bool append); // Add a service with the given list of characteristics to the queue to be registered. // The value_handles won't be valid until after mp_bluetooth_register_service_end is called. -int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics); +int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint16_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint16_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics); // Register any queued services. int mp_bluetooth_gatts_register_service_end(void); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 3983d1869082b..4484df937fc9e 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -685,7 +685,7 @@ int mp_bluetooth_gatts_register_service_end(void) { return 0; } -int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) { +int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint16_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint16_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) { if (MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services == MP_BLUETOOTH_NIMBLE_MAX_SERVICES) { return MP_E2BIG; } @@ -697,6 +697,7 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m characteristics[i].uuid = create_nimble_uuid(characteristic_uuids[i], NULL); characteristics[i].access_cb = characteristic_access_cb; characteristics[i].arg = NULL; + // NimBLE flags match the MP_BLUETOOTH_CHARACTERISTIC_FLAG_ ones exactly (including the security/privacy options). characteristics[i].flags = characteristic_flags[i]; characteristics[i].min_key_size = 0; characteristics[i].val_handle = &handles[handle_index]; @@ -710,7 +711,8 @@ int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, m for (size_t j = 0; j < num_descriptors[i]; ++j) { descriptors[j].uuid = create_nimble_uuid(descriptor_uuids[descriptor_index], NULL); descriptors[j].access_cb = characteristic_access_cb; - descriptors[j].att_flags = descriptor_flags[descriptor_index]; + // NimBLE doesn't support security/privacy options on descriptors. + descriptors[j].att_flags = (uint8_t)descriptor_flags[descriptor_index]; descriptors[j].min_key_size = 0; // Unlike characteristic, Nimble doesn't provide an automatic way to remember the handle, so use the arg. descriptors[j].arg = &handles[handle_index]; From 89553997b8496aca96b46274c81fa6413d58f6bd Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 22:40:14 +1100 Subject: [PATCH 0442/3533] docs/library/ubluetooth.rst: Update char/desc flags. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 73547c356aa25..434759b43250a 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -375,9 +375,9 @@ writes from a client to a given characteristic, use Each **descriptor** is a two-element tuple containing a UUID and a **flags** value. - The **flags** are a bitwise-OR combination of the - :data:`ubluetooth.FLAG_READ`, :data:`ubluetooth.FLAG_WRITE` and - :data:`ubluetooth.FLAG_NOTIFY` values defined below. + The **flags** are a bitwise-OR combination of the flags defined below. These + set both the behaviour of the characteristic (or descriptor) as well as the + security and privacy requirements. The return value is a list (one element per service) of tuples (each element is a value handle). Characteristics and descriptor handles are flattened @@ -401,6 +401,25 @@ writes from a client to a given characteristic, use **Note:** Advertising must be stopped before registering services. + Available flags for characteristics and descriptors are:: + + from micropython import const + _FLAG_BROADCAST = const(0x0001) + _FLAG_READ = const(0x0002) + _FLAG_WRITE_NO_RESPONSE = const(0x0004) + _FLAG_WRITE = const(0x0008) + _FLAG_NOTIFY = const(0x0010) + _FLAG_INDICATE = const(0x0020) + _FLAG_AUTHENTICATED_SIGNED_WRITE = const(0x0040) + + _FLAG_AUX_WRITE = const(0x0100) + _FLAG_READ_ENCRYPTED = const(0x0200) + _FLAG_READ_AUTHENTICATED = const(0x0400) + _FLAG_READ_AUTHORIZED = const(0x0800) + _FLAG_WRITE_ENCRYPTED = const(0x1000) + _FLAG_WRITE_AUTHENTICATED = const(0x2000) + _FLAG_WRITE_AUTHORIZED = const(0x4000) + .. method:: BLE.gatts_read(value_handle, /) Reads the local value for this handle (which has either been written by @@ -618,11 +637,3 @@ Constructor - A 16-bit integer. e.g. ``0x2908``. - A 128-bit UUID string. e.g. ``'6E400001-B5A3-F393-E0A9-E50E24DCCA9E'``. - - -Constants ---------- - -.. data:: ubluetooth.FLAG_READ - ubluetooth.FLAG_WRITE - ubluetooth.FLAG_NOTIFY From 60830bcba46dd649e396b605708274a2208c398d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 23:04:58 +1100 Subject: [PATCH 0443/3533] extmod/modbluetooth: Allow user-specified reason in read request IRQ. Instead of returning None/bool from the IRQ, return None/int (where a zero value means success). This mirrors how the L2CAP_ACCEPT return value works. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 11 +++++++---- extmod/modbluetooth.h | 20 ++++++++++++++++++-- extmod/nimble/modbluetooth_nimble.c | 8 +++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 1e67c8ce32749..ed983b7946107 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1113,10 +1113,13 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); } -bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { +mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); - return result == mp_const_none || mp_obj_is_true(result); + // Return non-zero from IRQ handler to fail the read. + mp_int_t ret = 0; + mp_obj_get_int_maybe(result, &ret); + return ret; } void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { @@ -1320,11 +1323,11 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu schedule_ringbuf(atomic_state); } -bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { +mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { (void)conn_handle; (void)value_handle; // This must be handled synchronously and therefore cannot implemented with the ringbuffer. - return true; + return MP_BLUETOOTH_GATTS_NO_ERROR; } void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 0d4296626b589..9856d4a689407 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -94,6 +94,14 @@ #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHENTICATED (0x2000) #define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHORIZED (0x4000) +// Return values from _IRQ_GATTS_READ_REQUEST. +#define MP_BLUETOOTH_GATTS_NO_ERROR (0x00) +#define MP_BLUETOOTH_GATTS_ERROR_READ_NOT_PERMITTED (0x02) +#define MP_BLUETOOTH_GATTS_ERROR_WRITE_NOT_PERMITTED (0x03) +#define MP_BLUETOOTH_GATTS_ERROR_INSUFFICIENT_AUTHENTICATION (0x05) +#define MP_BLUETOOTH_GATTS_ERROR_INSUFFICIENT_AUTHORIZATION (0x08) +#define MP_BLUETOOTH_GATTS_ERROR_INSUFFICIENT_ENCRYPTION (0x0f) + // For mp_bluetooth_gattc_write, the mode parameter #define MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE (0) #define MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE (1) @@ -185,6 +193,13 @@ _FLAG_READ_AUTHORIZED = const(0x0800) _FLAG_WRITE_ENCRYPTED = const(0x1000) _FLAG_WRITE_AUTHENTICATED = const(0x2000) _FLAG_WRITE_AUTHORIZED = const(0x4000) + +_GATTS_NO_ERROR = const(0x00) +_GATTS_ERROR_READ_NOT_PERMITTED = const(0x02) +_GATTS_ERROR_WRITE_NOT_PERMITTED = const(0x03) +_GATTS_ERROR_INSUFFICIENT_AUTHENTICATION = const(0x05) +_GATTS_ERROR_INSUFFICIENT_AUTHORIZATION = const(0x08) +_GATTS_ERROR_INSUFFICIENT_ENCRYPTION = const(0x0f) */ // bluetooth.UUID type. @@ -324,8 +339,9 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); // Call this when an acknowledgment is received for an indication. void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status); -// Call this when a characteristic is read from. Return false to deny the read. -bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle); +// Call this when a characteristic is read from (giving the handler a chance to update the stored value). +// Return 0 to allow the read, otherwise a non-zero rejection reason (see MP_BLUETOOTH_GATTS_ERROR_*). +mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle); // Call this when an MTU exchange completes. void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 4484df937fc9e..e79dc2d4cb09c 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -608,12 +608,13 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, mp_bluetooth_gatts_db_entry_t *entry; switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: - case BLE_GATT_ACCESS_OP_READ_DSC: + case BLE_GATT_ACCESS_OP_READ_DSC: { // Allow Python code to override (by using gatts_write), or deny (by returning false) the read. // Note this will be a no-op if the ringbuffer implementation is being used (i.e. the stack isn't // run in the scheduler). The ringbuffer is not used on STM32 and Unix-H4 only. - if (!mp_bluetooth_gatts_on_read_request(conn_handle, value_handle)) { - return BLE_ATT_ERR_READ_NOT_PERMITTED; + int req = mp_bluetooth_gatts_on_read_request(conn_handle, value_handle); + if (req) { + return req; } entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle); @@ -626,6 +627,7 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle, } return 0; + } case BLE_GATT_ACCESS_OP_WRITE_CHR: case BLE_GATT_ACCESS_OP_WRITE_DSC: entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle); From 5e20f689ada17dcd750d516c957115fbb36c9435 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 23:09:10 +1100 Subject: [PATCH 0444/3533] docs/library/ubluetooth.rst: Update read request IRQ docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 434759b43250a..f65020d2aed6a 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -119,9 +119,9 @@ Event Handling # A client has written to this characteristic or descriptor. conn_handle, attr_handle = data elif event == _IRQ_GATTS_READ_REQUEST: - # A client has issued a read. Note: this is a hard IRQ. - # Return None to deny the read. - # Note: This event is not supported on ESP32. + # A client has issued a read. Note: this is only supported on STM32. + # Return a non-zero integer to deny the read (see below), or zero (or None) + # to accept the read. conn_handle, attr_handle = data elif event == _IRQ_SCAN_RESULT: # A single scan result. @@ -234,6 +234,15 @@ The event codes are:: _IRQ_L2CAP_SEND_READY = const(26) _IRQ_CONNECTION_UPDATE = const(27) +For the ``_IRQ_GATTS_READ_REQUEST`` event, the available return codes are:: + + _GATTS_NO_ERROR = const(0x00) + _GATTS_ERROR_READ_NOT_PERMITTED = const(0x02) + _GATTS_ERROR_WRITE_NOT_PERMITTED = const(0x03) + _GATTS_ERROR_INSUFFICIENT_AUTHENTICATION = const(0x05) + _GATTS_ERROR_INSUFFICIENT_AUTHORIZATION = const(0x08) + _GATTS_ERROR_INSUFFICIENT_ENCRYPTION = const(0x0f) + In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your program. @@ -420,6 +429,8 @@ writes from a client to a given characteristic, use _FLAG_WRITE_AUTHENTICATED = const(0x2000) _FLAG_WRITE_AUTHORIZED = const(0x4000) + As for the IRQs above, any required constants should be added to your Python code. + .. method:: BLE.gatts_read(value_handle, /) Reads the local value for this handle (which has either been written by From ac89267fef8b2396cf46c6ba19ccbe3d10acff9a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 25 Nov 2020 19:26:56 +1100 Subject: [PATCH 0445/3533] extmod/modbluetooth: Add compile-config flag to enable pairing/bonding. Enable it on STM32/Unix NimBLE only (pairing/bonding requires synchronous events and full bindings). Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 4 ++++ extmod/modbluetooth.h | 6 ++++++ extmod/nimble/nimble.mk | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index ed983b7946107..b9b7c712481f8 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -47,6 +47,10 @@ #error l2cap channels require synchronous modbluetooth events #endif +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +#error pairing and bonding require synchronous modbluetooth events +#endif + #define MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS 2000 #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN 5 diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 9856d4a689407..2cb2c709c5345 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -54,6 +54,12 @@ #define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (0) #endif +// A port can optionally enable support for pairing and bonding. +// Requires MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS. +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +#define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (0) +#endif + // This is used to protect the ringbuffer. // A port may no-op this if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS is enabled. #ifndef MICROPY_PY_BLUETOOTH_ENTER diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index 00a244d6eac77..ba094f16f871a 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -24,6 +24,11 @@ ifeq ($(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY),0) # UART is also polled by the RX IRQ. CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 +# Without the ringbuffer, and with the full implementation, we can also +# enable pairing and bonding. This requires both synchronous events and +# some customisation of the key store. +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 + NIMBLE_LIB_DIR = lib/mynewt-nimble LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ From 05fef8c6a4113bc05dd09ddd8d0bf7d136d59f39 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 4 Nov 2020 18:21:59 +1100 Subject: [PATCH 0446/3533] extmod/modbluetooth: Add _IRQ_ENCRYPTION_UPDATE event. This allows the application to be notified if any of encrypted, authenticated and bonded state change, as well as the encryption key size. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 4 ++++ extmod/btstack/modbluetooth_btstack.c | 29 +++++++++++++++++++++++++++ extmod/modbluetooth.c | 7 +++++++ extmod/modbluetooth.h | 9 ++++++++- extmod/nimble/modbluetooth_nimble.c | 24 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index f65020d2aed6a..d402c6a9ebb25 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -202,6 +202,9 @@ Event Handling elif event == _IRQ_CONNECTION_UPDATE: # The remote device has updated connection parameters. conn_handle, conn_interval, conn_latency, supervision_timeout, status = data + elif event == _IRQ_ENCRYPTION_UPDATE: + # The encryption state has changed (likely as a result of pairing or bonding). + conn_handle, encrypted, authenticated, bonded, key_size = data The event codes are:: @@ -233,6 +236,7 @@ The event codes are:: _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) _IRQ_CONNECTION_UPDATE = const(27) + _IRQ_ENCRYPTION_UPDATE = const(28) For the ``_IRQ_GATTS_READ_REQUEST`` event, the available return codes are:: diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index da9a6f732bf4c..825a9ab7b178e 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -359,6 +359,35 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_printf(" --> btstack # conns changed\n"); } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) { DEBUG_printf(" --> hci vendor specific\n"); + } else if (event_type == SM_EVENT_AUTHORIZATION_RESULT || + event_type == SM_EVENT_PAIRING_COMPLETE || + // event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle + event_type == HCI_EVENT_ENCRYPTION_CHANGE) { + DEBUG_printf(" --> enc/auth/pair/bond change\n", ); + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + uint16_t conn_handle; + switch (event_type) { + case SM_EVENT_AUTHORIZATION_RESULT: + conn_handle = sm_event_authorization_result_get_handle(packet); + break; + case SM_EVENT_PAIRING_COMPLETE: + conn_handle = sm_event_pairing_complete_get_handle(packet); + break; + case HCI_EVENT_ENCRYPTION_CHANGE: + conn_handle = hci_event_encryption_change_get_connection_handle(packet); + break; + default: + return; + } + + hci_connection_t *hci_con = hci_connection_for_handle(conn_handle); + sm_connection_t *desc = &hci_con->sm_connection; + mp_bluetooth_gatts_on_encryption_update(conn_handle, + desc->sm_connection_encrypted, + desc->sm_connection_authenticated, + desc->sm_le_db_index != -1, + desc->sm_actual_encryption_key_size); + #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) { DEBUG_printf(" --> hci disconnect complete\n"); uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index b9b7c712481f8..5d7de7b97fe06 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1107,6 +1107,13 @@ void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_i invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) { + uint8_t args[] = {encrypted, authenticated, bonded, key_size}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 2cb2c709c5345..62ff6f2f9e325 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -145,6 +145,7 @@ #define MP_BLUETOOTH_IRQ_L2CAP_RECV (25) #define MP_BLUETOOTH_IRQ_L2CAP_SEND_READY (26) #define MP_BLUETOOTH_IRQ_CONNECTION_UPDATE (27) +#define MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE (28) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -182,7 +183,8 @@ _IRQ_L2CAP_CONNECT = const(23) _IRQ_L2CAP_DISCONNECT = const(24) _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) -_IRQ_GATTS_CONN_UPDATE = const(27) +_IRQ_CONNECTION_UPDATE = const(27) +_IRQ_ENCRYPTION_UPDATE = const(28) _FLAG_BROADCAST = const(0x0001) _FLAG_READ = const(0x0002) @@ -339,6 +341,11 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han // Call this when any connection parameters have been changed. void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status); +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +// Call this when any connection encryption has been changed (e.g. during pairing). +void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size); +#endif + // Call this when a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e79dc2d4cb09c..312a564263fa2 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -316,6 +316,19 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { } break; } + + case BLE_GAP_EVENT_ENC_CHANGE: { + DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status); + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + struct ble_gap_conn_desc desc; + if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { + mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, + desc.sec_state.encrypted, desc.sec_state.authenticated, + desc.sec_state.bonded, desc.sec_state.key_size); + } + #endif + break; + } } return 0; } @@ -971,6 +984,17 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { break; } + case BLE_GAP_EVENT_ENC_CHANGE: { + DEBUG_printf("peripheral_gap_event_cb: enc change: status=%d\n", event->enc_change.status); + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { + mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, + desc.sec_state.encrypted, desc.sec_state.authenticated, + desc.sec_state.bonded, desc.sec_state.key_size); + } + #endif + break; + } default: break; } From a1fcf301217b34ae74fbb937a9488be5bc6e61a5 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 23:54:46 +1100 Subject: [PATCH 0447/3533] extmod/modbluetooth: Allow configuration of pairing/bonding parameters. This allows setting the security and MITM-protection requirements. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 38 +++++++++++++++++++++++++++ extmod/modbluetooth.c | 22 ++++++++++++++++ extmod/modbluetooth.h | 24 +++++++++++++++++ extmod/nimble/modbluetooth_nimble.c | 18 +++++++++++++ extmod/nimble/syscfg/syscfg.h | 13 ++++----- 5 files changed, 109 insertions(+), 6 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 825a9ab7b178e..ae4bac00940fa 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -53,6 +53,11 @@ STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3; volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; +// sm_set_authentication_requirements is set-only, so cache current value. +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +STATIC uint8_t mp_bluetooth_btstack_sm_auth_req = 0; +#endif + #define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV STATIC int btstack_error_to_errno(int err) { @@ -795,6 +800,39 @@ void mp_bluetooth_set_address_mode(uint8_t addr_mode) { } } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +void mp_bluetooth_set_bonding(bool enabled) { + if (enabled) { + mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_BONDING; + } else { + mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_BONDING; + } + sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req); +} + +void mp_bluetooth_set_mitm_protection(bool enabled) { + if (enabled) { + mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_MITM_PROTECTION; + } else { + mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_MITM_PROTECTION; + } + sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req); +} + +void mp_bluetooth_set_le_secure(bool enabled) { + if (enabled) { + mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_SECURE_CONNECTION; + } else { + mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_SECURE_CONNECTION; + } + sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req); +} + +void mp_bluetooth_set_io_capability(uint8_t capability) { + sm_set_io_capabilities(capability); +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) { uint8_t *value = NULL; size_t value_len = 0; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 5d7de7b97fe06..06e340c42d20a 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -387,6 +387,28 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map mp_bluetooth_set_address_mode(addr_mode); break; } + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + case MP_QSTR_bond: { + bool bonding_enabled = mp_obj_is_true(e->value); + mp_bluetooth_set_bonding(bonding_enabled); + break; + } + case MP_QSTR_mitm: { + bool mitm_protection = mp_obj_is_true(e->value); + mp_bluetooth_set_mitm_protection(mitm_protection); + break; + } + case MP_QSTR_io: { + mp_int_t io_capability = mp_obj_get_int(e->value); + mp_bluetooth_set_io_capability(io_capability); + break; + } + case MP_QSTR_le_secure: { + bool le_secure_required = mp_obj_is_true(e->value); + mp_bluetooth_set_le_secure(le_secure_required); + break; + } + #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING default: mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); } diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 62ff6f2f9e325..977453becd486 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -152,6 +152,13 @@ #define MP_BLUETOOTH_ADDRESS_MODE_RPA (2) #define MP_BLUETOOTH_ADDRESS_MODE_NRPA (3) +// These match the spec values, can be used directly by the stack. +#define MP_BLUETOOTH_IO_CAPABILITY_DISPLAY_ONLY (0) +#define MP_BLUETOOTH_IO_CAPABILITY_DISPLAY_YESNO (1) +#define MP_BLUETOOTH_IO_CAPABILITY_KEYBOARD_ONLY (2) +#define MP_BLUETOOTH_IO_CAPABILITY_NO_INPUT_OUTPUT (3) +#define MP_BLUETOOTH_IO_CAPABILITY_KEYBOARD_DISPLAY (4) + /* These aren't included in the module for space reasons, but can be used in your Python code if necessary. @@ -208,6 +215,12 @@ _GATTS_ERROR_WRITE_NOT_PERMITTED = const(0x03) _GATTS_ERROR_INSUFFICIENT_AUTHENTICATION = const(0x05) _GATTS_ERROR_INSUFFICIENT_AUTHORIZATION = const(0x08) _GATTS_ERROR_INSUFFICIENT_ENCRYPTION = const(0x0f) + +_IO_CAPABILITY_DISPLAY_ONLY = const(0) +_IO_CAPABILITY_DISPLAY_YESNO = const(1) +_IO_CAPABILITY_KEYBOARD_ONLY = const(2) +_IO_CAPABILITY_NO_INPUT_OUTPUT = const(3) +_IO_CAPABILITY_KEYBOARD_DISPLAY = const(4) */ // bluetooth.UUID type. @@ -254,6 +267,17 @@ void mp_bluetooth_get_current_address(uint8_t *addr_type, uint8_t *addr); // Sets the addressing mode to use. void mp_bluetooth_set_address_mode(uint8_t addr_mode); +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +// Set bonding flag in pairing requests (i.e. persist security keys). +void mp_bluetooth_set_bonding(bool enabled); +// Require MITM protection. +void mp_bluetooth_set_mitm_protection(bool enabled); +// Require LE Secure pairing (rather than Legacy Pairing) +void mp_bluetooth_set_le_secure(bool enabled); +// I/O capabilities for authentication (see MP_BLUETOOTH_IO_CAPABILITY_*). +void mp_bluetooth_set_io_capability(uint8_t capability); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + // Get or set the GAP device name that will be used by service 0x1800, characteristic 0x2a00. size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf); int mp_bluetooth_gap_set_device_name(const uint8_t *buf, size_t len); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 312a564263fa2..852b9eac021c2 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -552,6 +552,24 @@ void mp_bluetooth_set_address_mode(uint8_t addr_mode) { } } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +void mp_bluetooth_set_bonding(bool enabled) { + ble_hs_cfg.sm_bonding = enabled; +} + +void mp_bluetooth_set_mitm_protection(bool enabled) { + ble_hs_cfg.sm_mitm = enabled; +} + +void mp_bluetooth_set_le_secure(bool enabled) { + ble_hs_cfg.sm_sc = enabled; +} + +void mp_bluetooth_set_io_capability(uint8_t capability) { + ble_hs_cfg.sm_io_cap = capability; +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) { const char *name = ble_svc_gap_device_name(); *buf = (const uint8_t *)name; diff --git a/extmod/nimble/syscfg/syscfg.h b/extmod/nimble/syscfg/syscfg.h index bef6b3b9210d4..a051a8fefd069 100644 --- a/extmod/nimble/syscfg/syscfg.h +++ b/extmod/nimble/syscfg/syscfg.h @@ -116,18 +116,19 @@ int nimble_sprintf(char *str, const char *fmt, ...); #define MYNEWT_VAL_BLE_MONITOR_UART_BUFFER_SIZE (64) #define MYNEWT_VAL_BLE_MONITOR_UART_DEV ("uart0") #define MYNEWT_VAL_BLE_RPA_TIMEOUT (300) -#define MYNEWT_VAL_BLE_SM_BONDING (0) -#define MYNEWT_VAL_BLE_SM_IO_CAP (BLE_HS_IO_NO_INPUT_OUTPUT) #define MYNEWT_VAL_BLE_SM_KEYPRESS (0) #define MYNEWT_VAL_BLE_SM_LEGACY (1) #define MYNEWT_VAL_BLE_SM_MAX_PROCS (1) -#define MYNEWT_VAL_BLE_SM_MITM (0) #define MYNEWT_VAL_BLE_SM_OOB_DATA_FLAG (0) -#define MYNEWT_VAL_BLE_SM_OUR_KEY_DIST (0) -#define MYNEWT_VAL_BLE_SM_SC (1) -#define MYNEWT_VAL_BLE_SM_THEIR_KEY_DIST (0) +#define MYNEWT_VAL_BLE_SM_OUR_KEY_DIST (7) +#define MYNEWT_VAL_BLE_SM_THEIR_KEY_DIST (7) #define MYNEWT_VAL_BLE_STORE_MAX_BONDS (3) #define MYNEWT_VAL_BLE_STORE_MAX_CCCDS (8) +// These can be overridden at runtime with ble.config(le_secure, mitm, bond, io). +#define MYNEWT_VAL_BLE_SM_SC (1) +#define MYNEWT_VAL_BLE_SM_MITM (0) +#define MYNEWT_VAL_BLE_SM_BONDING (0) +#define MYNEWT_VAL_BLE_SM_IO_CAP (BLE_HS_IO_NO_INPUT_OUTPUT) /*** nimble/host/services/gap */ #define MYNEWT_VAL_BLE_SVC_GAP_APPEARANCE (0) From f822557cbb79decb6947e1e35b8cddce18f4c06f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 24 Nov 2020 23:59:06 +1100 Subject: [PATCH 0448/3533] docs/library/ubluetooth.rst: Add pairing/bonding config docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index d402c6a9ebb25..29d3044ebf4d7 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -80,6 +80,24 @@ Configuration :meth:`gattc_exchange_mtu`. Use the ``_IRQ_MTU_EXCHANGED`` event to discover the MTU for a given connection. + - ``'bond'``: Sets whether bonding will be enabled during pairing. When + enabled, pairing requests will set the "bond" flag and the keys will be stored + by both devices. + + - ``'mitm'``: Sets whether MITM-protection is required for pairing. + + - ``'io'``: Sets the I/O capabilities of this device. + + Available options are:: + + _IO_CAPABILITY_DISPLAY_ONLY = const(0) + _IO_CAPABILITY_DISPLAY_YESNO = const(1) + _IO_CAPABILITY_KEYBOARD_ONLY = const(2) + _IO_CAPABILITY_NO_INPUT_OUTPUT = const(3) + _IO_CAPABILITY_KEYBOARD_DISPLAY = const(4) + + - ``'le_secure'``: Sets whether "LE Secure" pairing is required. Default is "Legacy Pairing". + Event Handling -------------- From 801e8ffacff0d2ed15e82136db2a8a45afbfab7b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 25 Nov 2020 00:06:16 +1100 Subject: [PATCH 0449/3533] extmod/modbluetooth: Add gap_pair(conn_handle) func to intiate pairing. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 8 ++++++++ extmod/modbluetooth.c | 12 ++++++++++++ extmod/modbluetooth.h | 5 +++++ extmod/nimble/modbluetooth_nimble.c | 7 +++++++ 4 files changed, 32 insertions(+) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index ae4bac00940fa..f6af664a4e9b5 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -1167,6 +1167,14 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle) { return 0; } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +int mp_bluetooth_gap_pair(uint16_t conn_handle) { + DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle); + sm_request_pairing(conn_handle); + return 0; +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC btstack_timer_source_t scan_duration_timeout; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 06e340c42d20a..0d33130db7430 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -682,6 +682,15 @@ STATIC mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_han } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_disconnect_obj, bluetooth_ble_gap_disconnect); +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +STATIC mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in) { + (void)self_in; + uint16_t conn_handle = mp_obj_get_int(conn_handle_in); + return bluetooth_handle_errno(mp_bluetooth_gap_pair(conn_handle)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + // ---------------------------------------------------------------------------- // Bluetooth object: GATTS (Peripheral/Advertiser role) // ---------------------------------------------------------------------------- @@ -883,6 +892,9 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gap_scan), MP_ROM_PTR(&bluetooth_ble_gap_scan_obj) }, #endif { MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) }, + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + { MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) }, + #endif // GATT Server (i.e. peripheral/advertiser role) { MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) }, diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 977453becd486..48e75d432efa4 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -319,6 +319,11 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle); int mp_bluetooth_get_preferred_mtu(void); int mp_bluetooth_set_preferred_mtu(uint16_t mtu); +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +// Initiate pairing on the specified connection. +int mp_bluetooth_gap_pair(uint16_t conn_handle); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Start a discovery (scan). Set duration to zero to run continuously. int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 852b9eac021c2..b1f13ee98b2b0 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -860,6 +860,13 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { return 0; } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +int mp_bluetooth_gap_pair(uint16_t conn_handle) { + DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle); + return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle)); +} +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { From fff634e03134b8f1724759f477da61b698c9cdf4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 25 Nov 2020 00:10:17 +1100 Subject: [PATCH 0450/3533] docs/library/ubluetooth.rst: Add gap_pair() docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 29d3044ebf4d7..594d73c87824b 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -368,6 +368,15 @@ Central & Peripheral Roles Returns ``False`` if the connection handle wasn't connected, and ``True`` otherwise. +.. method:: BLE.gap_pair(conn_handle, /) + + Initiate pairing with the remote device. + + Before calling this, ensure that the ``io``, ``mitm``, ``le_secure``, and + ``bond`` configuration options are set (via :meth:`config`). + + On successful pairing, the ``_IRQ_ENCRYPTION_UPDATED`` event will be raised. + GATT Server ----------- From c4d08aa4e3641a1d05d35051454d4bce8ef14fcf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 25 Nov 2020 17:28:04 +1100 Subject: [PATCH 0451/3533] extmod/modbluetooth: Add support for bonding (key persistence). This adds `_IRQ_GET_SECRET` and `_IRQ_SET_SECRET` events to allow the BT stack to request the Python code retrive/store/delete secret key data. The actual keys and values are opaque to Python and stack-specific. Only NimBLE is implemented (pending moving btstack to sync events). The secret store is designed to be compatible with BlueKitchen's TLV store API. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 78 ++++++++----- extmod/modbluetooth.h | 18 ++- extmod/nimble/modbluetooth_nimble.c | 172 +++++++++++++++++++++++++++- extmod/nimble/nimble.mk | 2 +- 4 files changed, 240 insertions(+), 30 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 0d33130db7430..a69e8418a761d 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1081,14 +1081,15 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, const uint8_t *addr, const int8_t *i8, size_t n_i8, const mp_obj_bluetooth_uuid_t *uuid, - const uint8_t *data, size_t data_len) { + const uint8_t **data, size_t *data_len, size_t n_data) { mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); if (o->irq_handler == mp_const_none) { return mp_const_none; } mp_obj_array_t mv_addr; - mp_obj_array_t mv_data; + mp_obj_array_t mv_data[2]; + assert(n_data <= 2); mp_obj_tuple_t *data_tuple = mp_local_alloc(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t) * MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); data_tuple->base.type = &mp_type_tuple; @@ -1112,9 +1113,13 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(uuid); } #endif - if (data) { - mp_obj_memoryview_init(&mv_data, 'B', 0, data_len, (void *)data); - data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(&mv_data); + for (size_t i = 0; i < n_data; ++i) { + if (data[i]) { + mp_obj_memoryview_init(&mv_data[i], 'B', 0, data_len[i], (void *)data[i]); + data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(&mv_data[i]); + } else { + data_tuple->items[data_tuple->len++] = mp_const_none; + } } assert(data_tuple->len <= MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); @@ -1131,36 +1136,57 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, #define NULL_I8 NULL #define NULL_UUID NULL #define NULL_DATA NULL +#define NULL_DATA_LEN NULL void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr) { - invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status) { uint16_t args[] = {conn_handle, conn_interval, conn_latency, supervision_timeout, status}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) { uint8_t args[] = {encrypted, authenticated, bonded, key_size}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); +} + +bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len) { + uint8_t args[] = {type, index}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GET_SECRET, NULL_U16, 0, args, 2, NULL_ADDR, NULL_I8, 0, NULL_UUID, &key, &key_len, 1); + if (result == mp_const_none) { + return false; + } + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(result, &bufinfo, MP_BUFFER_READ); + *value = bufinfo.buf; + *value_len = bufinfo.len; + return true; +} + +bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len) { + const uint8_t *data[] = {key, value}; + size_t data_len[] = {key_len, value_len}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_SET_SECRET, NULL_U16, 0, &type, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, data, data_len, 2); + return mp_obj_is_true(result); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status) { uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); // Return non-zero from IRQ handler to fail the read. mp_int_t ret = 0; mp_obj_get_int_maybe(result, &ret); @@ -1169,13 +1195,13 @@ mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { uint16_t args[] = {conn_handle, value}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); // Return non-zero from IRQ handler to fail the accept. mp_int_t ret = 0; mp_obj_get_int_maybe(result, &ret); @@ -1184,58 +1210,58 @@ mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status) { uint16_t args[] = {conn_handle, cid, psm, status}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status) { uint16_t args[] = {conn_handle, cid}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid) { uint16_t args[] = {conn_handle, cid}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE void mp_bluetooth_gap_on_scan_complete(void) { - invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_U16, 0, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_U16, 0, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) { int8_t args[] = {adv_type, rssi}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, NULL_U16, 0, &addr_type, 1, addr, args, 2, NULL_UUID, data, data_len); + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, NULL_U16, 0, &addr_type, 1, addr, args, 2, NULL_UUID, &data, &data_len, 1); } void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid) { uint16_t args[] = {conn_handle, start_handle, end_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, service_uuid, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, service_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { uint16_t args[] = {conn_handle, def_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 3, &properties, 1, NULL_ADDR, NULL_I8, 0, characteristic_uuid, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 3, &properties, 1, NULL_ADDR, NULL_I8, 0, characteristic_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t handle, mp_obj_bluetooth_uuid_t *descriptor_uuid) { uint16_t args[] = {conn_handle, handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, descriptor_uuid, NULL_DATA, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, descriptor_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status) { uint16_t args[] = {conn_handle, status}; - invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { const uint8_t *combined_data; - uint16_t total_len; + size_t total_len; if (num > 1) { // Fragmented buffer, need to combine into a new heap-allocated buffer @@ -1258,7 +1284,7 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u } uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, combined_data, total_len); + invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, &combined_data, &total_len, 1); if (num > 1) { m_del(uint8_t, (uint8_t *)combined_data, total_len); @@ -1267,7 +1293,7 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) { uint16_t args[] = {conn_handle, value_handle, status}; - invoke_irq_handler(event, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); + invoke_irq_handler(event, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 48e75d432efa4..d79b7f56b9828 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -146,6 +146,8 @@ #define MP_BLUETOOTH_IRQ_L2CAP_SEND_READY (26) #define MP_BLUETOOTH_IRQ_CONNECTION_UPDATE (27) #define MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE (28) +#define MP_BLUETOOTH_IRQ_GET_SECRET (29) +#define MP_BLUETOOTH_IRQ_SET_SECRET (30) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -159,6 +161,12 @@ #define MP_BLUETOOTH_IO_CAPABILITY_NO_INPUT_OUTPUT (3) #define MP_BLUETOOTH_IO_CAPABILITY_KEYBOARD_DISPLAY (4) +// These match NimBLE BLE_SM_IOACT_. +#define MP_BLUETOOTH_PASSKEY_ACTION_NONE (0) +#define MP_BLUETOOTH_PASSKEY_ACTION_INPUT (2) +#define MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY (3) +#define MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON (4) + /* These aren't included in the module for space reasons, but can be used in your Python code if necessary. @@ -192,6 +200,8 @@ _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) _IRQ_CONNECTION_UPDATE = const(27) _IRQ_ENCRYPTION_UPDATE = const(28) +_IRQ_GET_SECRET = const(29) +_IRQ_SET_SECRET = const(30) _FLAG_BROADCAST = const(0x0001) _FLAG_READ = const(0x0002) @@ -373,7 +383,13 @@ void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_i #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING // Call this when any connection encryption has been changed (e.g. during pairing). void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size); -#endif + +// Call this when you need the application to manage persistent key data. +// For get, if key is NULL, then the implementation must return the index'th matching key. Otherwise it should return a specific key. +// For set, if value is NULL, then delete. +bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len); +bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING // Call this when a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index b1f13ee98b2b0..2b273c6bc7962 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -269,6 +269,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { switch (event->type) { case BLE_GAP_EVENT_CONNECT: + DEBUG_printf("gap_event_cb: connect: status=%d\n", event->connect.status); if (event->connect.status == 0) { // Connection established. ble_gap_conn_find(event->connect.conn_handle, &desc); @@ -282,6 +283,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { case BLE_GAP_EVENT_DISCONNECT: // Disconnect. + DEBUG_printf("gap_event_cb: disconnect: reason=%d\n", event->disconnect.reason); reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val); mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr); break; @@ -310,7 +312,6 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { case BLE_GAP_EVENT_CONN_UPDATE: { DEBUG_printf("gap_event_cb: connection update: status=%d\n", event->conn_update.status); - struct ble_gap_conn_desc desc; if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); } @@ -320,7 +321,6 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { case BLE_GAP_EVENT_ENC_CHANGE: { DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status); #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING - struct ble_gap_conn_desc desc; if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, desc.sec_state.encrypted, desc.sec_state.authenticated, @@ -329,6 +329,27 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { #endif break; } + + case BLE_GAP_EVENT_REPEAT_PAIRING: { + // We recognized this peer but the peer doesn't recognize us. + DEBUG_printf("gap_event_cb: repeat pairing: conn_handle=%d\n", event->repeat_pairing.conn_handle); + + // TODO: Consider returning BLE_GAP_REPEAT_PAIRING_IGNORE (and + // possibly an API to configure this). + + // Delete the old bond. + int rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + if (rc == 0) { + ble_store_util_delete_peer(&desc.peer_id_addr); + } + + // Allow re-pairing. + return BLE_GAP_REPEAT_PAIRING_RETRY; + } + + default: + DEBUG_printf("gap_event_cb: unknown type %d\n", event->type); + break; } return 0; } @@ -969,6 +990,7 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { switch (event->type) { case BLE_GAP_EVENT_CONNECT: + DEBUG_printf("peripheral_gap_event_cb: status=%d\n", event->connect.status); if (event->connect.status == 0) { // Connection established. ble_gap_conn_find(event->connect.conn_handle, &desc); @@ -982,6 +1004,7 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { case BLE_GAP_EVENT_DISCONNECT: // Disconnect. + DEBUG_printf("peripheral_gap_event_cb: reason=%d\n", event->disconnect.reason); reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val); mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr); @@ -1020,9 +1043,12 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { #endif break; } + default: + DEBUG_printf("peripheral_gap_event_cb: unknown type %d\n", event->type); break; } + return 0; } @@ -1514,4 +1540,146 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + +STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, union ble_store_value *value) { + DEBUG_printf("ble_store_ram_read: %d\n", obj_type); + const uint8_t *key_data; + size_t key_data_len; + + switch (obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: { + if (ble_addr_cmp(&key->sec.peer_addr, BLE_ADDR_ANY)) { + // (single) + // Find the entry for this specific peer. + assert(key->sec.idx == 0); + assert(!key->sec.ediv_rand_present); + key_data = (const uint8_t *)&key->sec.peer_addr; + key_data_len = sizeof(ble_addr_t); + } else { + // (with index) + // Iterate all known peers. + assert(!key->sec.ediv_rand_present); + key_data = NULL; + key_data_len = 0; + } + break; + } + case BLE_STORE_OBJ_TYPE_OUR_SEC: { + // + // Find our secret for this remote device, matching this ediv/rand key. + assert(ble_addr_cmp(&key->sec.peer_addr, BLE_ADDR_ANY)); // Must have address. + assert(key->sec.idx == 0); + assert(key->sec.ediv_rand_present); + key_data = (const uint8_t *)&key->sec.peer_addr; + key_data_len = sizeof(ble_addr_t); + break; + } + case BLE_STORE_OBJ_TYPE_CCCD: { + // TODO: Implement CCCD persistence. + DEBUG_printf("ble_store_ram_read: CCCD not supported.\n"); + return -1; + } + default: + return BLE_HS_ENOTSUP; + } + + const uint8_t *value_data; + size_t value_data_len; + if (!mp_bluetooth_gap_on_get_secret(obj_type, key->sec.idx, key_data, key_data_len, &value_data, &value_data_len)) { + DEBUG_printf("ble_store_ram_read: Key not found: type=%d, index=%u, key=0x%p, len=" UINT_FMT "\n", obj_type, key->sec.idx, key_data, key_data_len); + return BLE_HS_ENOENT; + } + + if (value_data_len != sizeof(struct ble_store_value_sec)) { + DEBUG_printf("ble_store_ram_read: Invalid key data: actual=" UINT_FMT " expected=" UINT_FMT "\n", value_data_len, sizeof(struct ble_store_value_sec)); + return BLE_HS_ENOENT; + } + + memcpy((uint8_t *)&value->sec, value_data, sizeof(struct ble_store_value_sec)); + + DEBUG_printf("ble_store_ram_read: found secret\n"); + + if (obj_type == BLE_STORE_OBJ_TYPE_OUR_SEC) { + // TODO: Verify ediv_rand matches. + } + + return 0; +} + +STATIC int ble_store_ram_write(int obj_type, const union ble_store_value *val) { + DEBUG_printf("ble_store_ram_write: %d\n", obj_type); + switch (obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_OUR_SEC: { + // + + struct ble_store_key_sec key_sec; + const struct ble_store_value_sec *value_sec = &val->sec; + ble_store_key_from_value_sec(&key_sec, value_sec); + + assert(ble_addr_cmp(&key_sec.peer_addr, BLE_ADDR_ANY)); // Must have address. + assert(key_sec.ediv_rand_present); + + if (!mp_bluetooth_gap_on_set_secret(obj_type, (const uint8_t *)&key_sec.peer_addr, sizeof(ble_addr_t), (const uint8_t *)value_sec, sizeof(struct ble_store_value_sec))) { + DEBUG_printf("Failed to write key: type=%d\n", obj_type); + return BLE_HS_ESTORE_CAP; + } + + DEBUG_printf("ble_store_ram_write: wrote secret\n"); + + return 0; + } + case BLE_STORE_OBJ_TYPE_CCCD: { + // TODO: Implement CCCD persistence. + DEBUG_printf("ble_store_ram_write: CCCD not supported.\n"); + // Just pretend we wrote it. + return 0; + } + default: + return BLE_HS_ENOTSUP; + } +} + +STATIC int ble_store_ram_delete(int obj_type, const union ble_store_key *key) { + DEBUG_printf("ble_store_ram_delete: %d\n", obj_type); + switch (obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_OUR_SEC: { + // + + assert(ble_addr_cmp(&key->sec.peer_addr, BLE_ADDR_ANY)); // Must have address. + // ediv_rand is optional (will not be present for delete). + + if (!mp_bluetooth_gap_on_set_secret(obj_type, (const uint8_t *)&key->sec.peer_addr, sizeof(ble_addr_t), NULL, 0)) { + DEBUG_printf("Failed to delete key: type=%d\n", obj_type); + return BLE_HS_ENOENT; + } + + DEBUG_printf("ble_store_ram_delete: deleted secret\n"); + + return 0; + } + case BLE_STORE_OBJ_TYPE_CCCD: { + // TODO: Implement CCCD persistence. + DEBUG_printf("ble_store_ram_delete: CCCD not supported.\n"); + // Just pretend it wasn't there. + return BLE_HS_ENOENT; + } + default: + return BLE_HS_ENOTSUP; + } +} + +// nimble_port_init always calls ble_store_ram_init. We provide this alternative +// implementation rather than the one in nimble/store/ram/src/ble_store_ram.c. +// TODO: Consider re-implementing nimble_port_init instead. +void ble_store_ram_init(void) { + ble_hs_cfg.store_read_cb = ble_store_ram_read; + ble_hs_cfg.store_write_cb = ble_store_ram_write; + ble_hs_cfg.store_delete_cb = ble_store_ram_delete; +} + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index ba094f16f871a..806630074eb87 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -83,7 +83,6 @@ LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ ble_store_util.c \ ble_uuid.c \ ) \ - nimble/host/store/ram/src/ble_store_ram.c \ nimble/host/util/src/addr.c \ nimble/transport/uart/src/ble_hci_uart.c \ $(addprefix porting/nimble/src/, \ @@ -95,6 +94,7 @@ LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ os_msys_init.c \ ) \ ) + # nimble/host/store/ram/src/ble_store_ram.c \ EXTMOD_SRC_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \ nimble/nimble_npl_os.c \ From b799fe142117f5c503c9c73fbe5f1cb4479d6f7b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 27 Nov 2020 00:17:56 +1100 Subject: [PATCH 0452/3533] docs/library/ubluetooth.rst: Add bonding docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 594d73c87824b..9ce400e7eb047 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -8,7 +8,7 @@ This module provides an interface to a Bluetooth controller on a board. Currently this supports Bluetooth Low Energy (BLE) in Central, Peripheral, Broadcaster, and Observer roles, as well as GATT Server and Client and L2CAP connection-oriented-channels. A device may operate in multiple roles -concurrently. +concurrently. Pairing (and bonding) is supported on some ports. This API is intended to match the low-level Bluetooth protocol and provide building-blocks for higher-level abstractions such as specific device types. @@ -96,7 +96,8 @@ Configuration _IO_CAPABILITY_NO_INPUT_OUTPUT = const(3) _IO_CAPABILITY_KEYBOARD_DISPLAY = const(4) - - ``'le_secure'``: Sets whether "LE Secure" pairing is required. Default is "Legacy Pairing". + - ``'le_secure'``: Sets whether "LE Secure" pairing is required. Default is + false (i.e. allow "Legacy Pairing"). Event Handling -------------- @@ -223,6 +224,16 @@ Event Handling elif event == _IRQ_ENCRYPTION_UPDATE: # The encryption state has changed (likely as a result of pairing or bonding). conn_handle, encrypted, authenticated, bonded, key_size = data + elif event == _IRQ_GET_SECRET: + # Return a stored secret. + # If key is None, return the index'th value of this sec_type. + # Otherwise return the corresponding value for this sec_type and key. + sec_type, index, key = data + return value + elif event == _IRQ_SET_SECRET: + # Save a secret to the store for this sec_type and key. + sec_type, key, value = data + return True The event codes are:: @@ -255,6 +266,8 @@ The event codes are:: _IRQ_L2CAP_SEND_READY = const(26) _IRQ_CONNECTION_UPDATE = const(27) _IRQ_ENCRYPTION_UPDATE = const(28) + _IRQ_GET_SECRET = const(29) + _IRQ_SET_SECRET = const(30) For the ``_IRQ_GATTS_READ_REQUEST`` event, the available return codes are:: @@ -368,15 +381,6 @@ Central & Peripheral Roles Returns ``False`` if the connection handle wasn't connected, and ``True`` otherwise. -.. method:: BLE.gap_pair(conn_handle, /) - - Initiate pairing with the remote device. - - Before calling this, ensure that the ``io``, ``mitm``, ``le_secure``, and - ``bond`` configuration options are set (via :meth:`config`). - - On successful pairing, the ``_IRQ_ENCRYPTION_UPDATED`` event will be raised. - GATT Server ----------- @@ -664,6 +668,31 @@ L2CAP connection-oriented-channels more channel credits and will be unable to send any more data. +Pairing and bonding +------------------- + + Pairing allows a connection to be encrypted and authenticated via exchange + of secrets (with optional MITM protection via passkey authentication). + + Bonding is the process of storing those secrets into non-volatile storage. + When bonded, a device is able to resolve a resolvable private address (RPA) + from another device based on the stored identity resolving key (IRK). + To support bonding, an application must implement the ``_IRQ_GET_SECRET`` + and ``_IRQ_SET_SECRET`` events. + + **Note:** This is currently only supported when using the NimBLE stack on + STM32 and Unix (not ESP32). + +.. method:: BLE.gap_pair(conn_handle, /) + + Initiate pairing with the remote device. + + Before calling this, ensure that the ``io``, ``mitm``, ``le_secure``, and + ``bond`` configuration options are set (via :meth:`config`). + + On successful pairing, the ``_IRQ_ENCRYPTION_UPDATE`` event will be raised. + + class UUID ---------- From 4bcbbfdb6cde6293085f4123090890c889cad50d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 26 Nov 2020 23:48:34 +1100 Subject: [PATCH 0453/3533] extmod/modbluetooth: Simplify synchronous invoke_irq_handler signature. Rather than dealing with the different int types, just pass them all as a single array of mp_int_t with n_unsigned (before addr) and n_signed (after addr). Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 103 ++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index a69e8418a761d..ba5333e70e90d 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1076,10 +1076,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_ble_invoke_irq_obj, bluetooth_ble_inv #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS STATIC mp_obj_t invoke_irq_handler(uint16_t event, - const uint16_t *u16, size_t n_u16, - const uint8_t *u8, size_t n_u8, + const mp_int_t *numeric, size_t n_unsigned, size_t n_signed, const uint8_t *addr, - const int8_t *i8, size_t n_i8, const mp_obj_bluetooth_uuid_t *uuid, const uint8_t **data, size_t *data_len, size_t n_data) { mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); @@ -1095,18 +1093,15 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, data_tuple->base.type = &mp_type_tuple; data_tuple->len = 0; - for (size_t i = 0; i < n_u16; ++i) { - data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(u16[i]); - } - for (size_t i = 0; i < n_u8; ++i) { - data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(u8[i]); + for (size_t i = 0; i < n_unsigned; ++i) { + data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(numeric[i]); } if (addr) { mp_obj_memoryview_init(&mv_addr, 'B', 0, 6, (void *)addr); data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(&mv_addr); } - for (size_t i = 0; i < n_i8; ++i) { - data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(i8[i]); + for (size_t i = 0; i < n_signed; ++i) { + data_tuple->items[data_tuple->len++] = MP_OBJ_NEW_SMALL_INT(numeric[i + n_unsigned]); } #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE if (uuid) { @@ -1130,32 +1125,31 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, return result; } -#define NULL_U16 NULL -#define NULL_U8 NULL +#define NULL_NUMERIC NULL #define NULL_ADDR NULL -#define NULL_I8 NULL #define NULL_UUID NULL #define NULL_DATA NULL #define NULL_DATA_LEN NULL void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr) { - invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, addr_type}; + invoke_irq_handler(event, args, 2, 0, addr, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status) { - uint16_t args[] = {conn_handle, conn_interval, conn_latency, supervision_timeout, status}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, conn_interval, conn_latency, supervision_timeout, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) { - uint8_t args[] = {encrypted, authenticated, bonded, key_size}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, encrypted, authenticated, bonded, key_size}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len) { - uint8_t args[] = {type, index}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GET_SECRET, NULL_U16, 0, args, 2, NULL_ADDR, NULL_I8, 0, NULL_UUID, &key, &key_len, 1); + mp_int_t args[] = {type, index}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GET_SECRET, args, 2, 0, NULL_ADDR, NULL_UUID, &key, &key_len, 1); if (result == mp_const_none) { return false; } @@ -1167,26 +1161,27 @@ bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t * } bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len) { + mp_int_t args[] = { type }; const uint8_t *data[] = {key, value}; size_t data_len[] = {key_len, value_len}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_SET_SECRET, NULL_U16, 0, &type, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, data, data_len, 2); + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_SET_SECRET, args, 1, 0, NULL_ADDR, NULL_UUID, data, data_len, 2); return mp_obj_is_true(result); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { - uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, value_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status) { - uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, value_handle, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 3, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) { - uint16_t args[] = {conn_handle, value_handle}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, value_handle}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); // Return non-zero from IRQ handler to fail the read. mp_int_t ret = 0; mp_obj_get_int_maybe(result, &ret); @@ -1194,14 +1189,14 @@ mp_int_t mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value } void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { - uint16_t args[] = {conn_handle, value}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, value}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_MTU_EXCHANGED, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { - uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; - mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; + mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); // Return non-zero from IRQ handler to fail the accept. mp_int_t ret = 0; mp_obj_get_int_maybe(result, &ret); @@ -1209,54 +1204,54 @@ mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, } void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { - uint16_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status) { - uint16_t args[] = {conn_handle, cid, psm, status}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, cid, psm, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status) { - uint16_t args[] = {conn_handle, cid}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, cid, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 3, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid) { - uint16_t args[] = {conn_handle, cid}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, cid}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE void mp_bluetooth_gap_on_scan_complete(void) { - invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_U16, 0, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_NUMERIC, 0, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) { - int8_t args[] = {adv_type, rssi}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, NULL_U16, 0, &addr_type, 1, addr, args, 2, NULL_UUID, &data, &data_len, 1); + mp_int_t args[] = {addr_type, adv_type, rssi}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, args, 1, 2, addr, NULL_UUID, &data, &data_len, 1); } void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid) { - uint16_t args[] = {conn_handle, start_handle, end_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, service_uuid, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, start_handle, end_handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, 0, NULL_ADDR, service_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { - uint16_t args[] = {conn_handle, def_handle, value_handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 3, &properties, 1, NULL_ADDR, NULL_I8, 0, characteristic_uuid, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, def_handle, value_handle, properties}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 4, 0, NULL_ADDR, characteristic_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t handle, mp_obj_bluetooth_uuid_t *descriptor_uuid) { - uint16_t args[] = {conn_handle, handle}; - invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, descriptor_uuid, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, handle}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT, args, 2, 0, NULL_ADDR, descriptor_uuid, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle, uint16_t status) { - uint16_t args[] = {conn_handle, status}; - invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, status}; + invoke_irq_handler(event, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { @@ -1283,8 +1278,8 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u total_len = *data_len; } - uint16_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(event, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, &combined_data, &total_len, 1); + mp_int_t args[] = {conn_handle, value_handle}; + invoke_irq_handler(event, args, 2, 0, NULL_ADDR, NULL_UUID, &combined_data, &total_len, 1); if (num > 1) { m_del(uint8_t, (uint8_t *)combined_data, total_len); @@ -1292,8 +1287,8 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u } void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) { - uint16_t args[] = {conn_handle, value_handle, status}; - invoke_irq_handler(event, args, 3, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); + mp_int_t args[] = {conn_handle, value_handle, status}; + invoke_irq_handler(event, args, 3, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE From e4f27cbee767d24f8e05678a484a584ec6fbe974 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 26 Nov 2020 23:49:45 +1100 Subject: [PATCH 0454/3533] extmod/modbluetooth: Add support for passkey authentication. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 14 +++++++++++ extmod/modbluetooth.h | 20 ++++++++++++++++ extmod/nimble/modbluetooth_nimble.c | 37 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index ba5333e70e90d..caaf872e7ddf4 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -689,6 +689,14 @@ STATIC mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in return bluetooth_handle_errno(mp_bluetooth_gap_pair(conn_handle)); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair); + +STATIC mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) { + uint16_t conn_handle = mp_obj_get_int(args[1]); + uint8_t action = mp_obj_get_int(args[2]); + mp_int_t passkey = mp_obj_get_int(args[3]); + return bluetooth_handle_errno(mp_bluetooth_gap_passkey(conn_handle, action, passkey)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_passkey_obj, 4, 4, bluetooth_ble_gap_passkey); #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING // ---------------------------------------------------------------------------- @@ -894,6 +902,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) }, #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING { MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) }, + { MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) }, #endif // GATT Server (i.e. peripheral/advertiser role) { MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) }, @@ -1167,6 +1176,11 @@ bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_SET_SECRET, args, 1, 0, NULL_ADDR, NULL_UUID, data, data_len, 2); return mp_obj_is_true(result); } + +void mp_bluetooth_gap_on_passkey_action(uint16_t conn_handle, uint8_t action, mp_int_t passkey) { + mp_int_t args[] = { conn_handle, action, passkey }; + invoke_irq_handler(MP_BLUETOOTH_IRQ_PASSKEY_ACTION, args, 2, 1, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); +} #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index d79b7f56b9828..f14033de302e2 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -148,6 +148,7 @@ #define MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE (28) #define MP_BLUETOOTH_IRQ_GET_SECRET (29) #define MP_BLUETOOTH_IRQ_SET_SECRET (30) +#define MP_BLUETOOTH_IRQ_PASSKEY_ACTION (31) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -167,6 +168,12 @@ #define MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY (3) #define MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON (4) +// These match NimBLE BLE_SM_IOACT_. +#define MP_BLUETOOTH_PASSKEY_ACTION_NONE (0) +#define MP_BLUETOOTH_PASSKEY_ACTION_INPUT (2) +#define MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY (3) +#define MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON (4) + /* These aren't included in the module for space reasons, but can be used in your Python code if necessary. @@ -202,6 +209,7 @@ _IRQ_CONNECTION_UPDATE = const(27) _IRQ_ENCRYPTION_UPDATE = const(28) _IRQ_GET_SECRET = const(29) _IRQ_SET_SECRET = const(30) +_IRQ_PASSKEY_ACTION = const(31) _FLAG_BROADCAST = const(0x0001) _FLAG_READ = const(0x0002) @@ -231,6 +239,11 @@ _IO_CAPABILITY_DISPLAY_YESNO = const(1) _IO_CAPABILITY_KEYBOARD_ONLY = const(2) _IO_CAPABILITY_NO_INPUT_OUTPUT = const(3) _IO_CAPABILITY_KEYBOARD_DISPLAY = const(4) + +_PASSKEY_ACTION_NONE = const(0) +_PASSKEY_ACTION_INPUT = const(2) +_PASSKEY_ACTION_DISPLAY = const(3) +_PASSKEY_ACTION_NUMERIC_COMPARISON = const(4) */ // bluetooth.UUID type. @@ -332,6 +345,9 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu); #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING // Initiate pairing on the specified connection. int mp_bluetooth_gap_pair(uint16_t conn_handle); + +// Respond to a pairing request. +int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey); #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE @@ -387,8 +403,12 @@ void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypte // Call this when you need the application to manage persistent key data. // For get, if key is NULL, then the implementation must return the index'th matching key. Otherwise it should return a specific key. // For set, if value is NULL, then delete. +// The "type" is stack-specific, but could also be used to implement versioning. bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len); bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len); + +// Call this when a passkey verification needs to be processed. +void mp_bluetooth_gap_on_passkey_action(uint16_t conn_handle, uint8_t action, mp_int_t passkey); #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING // Call this when a characteristic is written to. diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 2b273c6bc7962..ae727086eaeda 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -347,6 +347,16 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { return BLE_GAP_REPEAT_PAIRING_RETRY; } + case BLE_GAP_EVENT_PASSKEY_ACTION: { + DEBUG_printf("gap_event_cb: passkey action: conn_handle=%d action=%d num=" UINT_FMT "\n", event->passkey.conn_handle, event->passkey.params.action, (mp_uint_t)event->passkey.params.numcmp); + + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + mp_bluetooth_gap_on_passkey_action(event->passkey.conn_handle, event->passkey.params.action, event->passkey.params.numcmp); + #endif + + return 0; + } + default: DEBUG_printf("gap_event_cb: unknown type %d\n", event->type); break; @@ -886,6 +896,33 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) { DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle); return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle)); } + +int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) { + struct ble_sm_io io = {0}; + + switch (action) { + case MP_BLUETOOTH_PASSKEY_ACTION_INPUT: { + io.passkey = passkey; + break; + } + case MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY: { + io.passkey = passkey; + break; + } + case MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON: { + io.numcmp_accept = passkey != 0; + break; + } + default: { + return MP_EINVAL; + } + } + + io.action = action; + + DEBUG_printf("mp_bluetooth_gap_passkey: injecting IO: conn_handle=%d, action=%d, passkey=" UINT_FMT ", numcmp_accept=%d\n", conn_handle, io.action, (mp_uint_t)io.passkey, io.numcmp_accept); + return ble_hs_err_to_errno(ble_sm_inject_io(conn_handle, &io)); +} #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE From f6fd46c4024c28c827ccffd13dbe02b9ea74cfb8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 26 Nov 2020 23:54:56 +1100 Subject: [PATCH 0455/3533] examples/bluetooth: Add bonding/passkey demo. Signed-off-by: Jim Mussared --- examples/bluetooth/ble_bonding_peripheral.py | 194 +++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 examples/bluetooth/ble_bonding_peripheral.py diff --git a/examples/bluetooth/ble_bonding_peripheral.py b/examples/bluetooth/ble_bonding_peripheral.py new file mode 100644 index 0000000000000..bd7596dbcb347 --- /dev/null +++ b/examples/bluetooth/ble_bonding_peripheral.py @@ -0,0 +1,194 @@ +# This example demonstrates a simple temperature sensor peripheral. +# +# The sensor's local value updates every second, and it will notify +# any connected central every 10 seconds. +# +# Work-in-progress demo of implementing bonding and passkey auth. + +import bluetooth +import random +import struct +import time +import json +import binascii +from ble_advertising import advertising_payload + +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_INDICATE_DONE = const(20) + +_IRQ_ENCRYPTION_UPDATE = const(28) +_IRQ_PASSKEY_ACTION = const(31) + +_IRQ_GET_SECRET = const(29) +_IRQ_SET_SECRET = const(30) + +_FLAG_READ = const(0x0002) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + +_FLAG_READ_ENCRYPTED = const(0x0200) + +# org.bluetooth.service.environmental_sensing +_ENV_SENSE_UUID = bluetooth.UUID(0x181A) +# org.bluetooth.characteristic.temperature +_TEMP_CHAR = ( + bluetooth.UUID(0x2A6E), + _FLAG_READ | _FLAG_NOTIFY | _FLAG_INDICATE | _FLAG_READ_ENCRYPTED, +) +_ENV_SENSE_SERVICE = ( + _ENV_SENSE_UUID, + (_TEMP_CHAR,), +) + +# org.bluetooth.characteristic.gap.appearance.xml +_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) + +_IO_CAPABILITY_DISPLAY_ONLY = const(0) +_IO_CAPABILITY_DISPLAY_YESNO = const(1) +_IO_CAPABILITY_KEYBOARD_ONLY = const(2) +_IO_CAPABILITY_NO_INPUT_OUTPUT = const(3) +_IO_CAPABILITY_KEYBOARD_DISPLAY = const(4) + +_PASSKEY_ACTION_INPUT = const(2) +_PASSKEY_ACTION_DISP = const(3) +_PASSKEY_ACTION_NUMCMP = const(4) + + +class BLETemperature: + def __init__(self, ble, name="mpy-temp"): + self._ble = ble + self._load_secrets() + self._ble.irq(self._irq) + self._ble.config(bond=True) + self._ble.config(le_secure=True) + self._ble.config(mitm=True) + self._ble.config(io=_IO_CAPABILITY_DISPLAY_YESNO) + self._ble.active(True) + self._ble.config(addr_mode=2) + ((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,)) + self._connections = set() + self._payload = advertising_payload( + name=name, services=[_ENV_SENSE_UUID], appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER + ) + self._advertise() + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _ = data + self._connections.add(conn_handle) + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _ = data + self._connections.remove(conn_handle) + self._save_secrets() + # Start advertising again to allow a new connection. + self._advertise() + elif event == _IRQ_ENCRYPTION_UPDATE: + conn_handle, encrypted, authenticated, bonded, key_size = data + print("encryption update", conn_handle, encrypted, authenticated, bonded, key_size) + elif event == _IRQ_PASSKEY_ACTION: + conn_handle, action, passkey = data + print("passkey action", conn_handle, action, passkey) + if action == _PASSKEY_ACTION_NUMCMP: + accept = int(input("accept? ")) + self._ble.gap_passkey(conn_handle, action, accept) + elif action == _PASSKEY_ACTION_DISP: + print("displaying 123456") + self._ble.gap_passkey(conn_handle, action, 123456) + elif action == _PASSKEY_ACTION_INPUT: + print("prompting for passkey") + passkey = int(input("passkey? ")) + self._ble.gap_passkey(conn_handle, action, passkey) + else: + print("unknown action") + elif event == _IRQ_GATTS_INDICATE_DONE: + conn_handle, value_handle, status = data + elif event == _IRQ_SET_SECRET: + sec_type, key, value = data + key = sec_type, bytes(key) + value = bytes(value) if value else None + print("set secret:", key, value) + if value is None: + if key in self._secrets: + del self._secrets[key] + return True + else: + return False + else: + self._secrets[key] = value + return True + elif event == _IRQ_GET_SECRET: + sec_type, index, key = data + print("get secret:", sec_type, index, bytes(key) if key else None) + if key is None: + i = 0 + for (t, _key), value in self._secrets.items(): + if t == sec_type: + if i == index: + return value + i += 1 + return None + else: + key = sec_type, bytes(key) + return self._secrets.get(key, None) + + def set_temperature(self, temp_deg_c, notify=False, indicate=False): + # Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius. + # Write the local value, ready for a central to read. + self._ble.gatts_write(self._handle, struct.pack(" Date: Fri, 27 Nov 2020 00:32:26 +1100 Subject: [PATCH 0456/3533] docs/library/ubluetooth.rst: Add passkey docs. Signed-off-by: Jim Mussared --- docs/library/ubluetooth.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index 9ce400e7eb047..3d435a7beda5a 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -234,6 +234,13 @@ Event Handling # Save a secret to the store for this sec_type and key. sec_type, key, value = data return True + elif event == _IRQ_PASSKEY_ACTION: + # Respond to a passkey request during pairing. + # See gap_passkey() for details. + # action will be an action that is compatible with the configured "io" config. + # passkey will be non-zero if action is "numeric comparison". + conn_handle, action, passkey = data + The event codes are:: @@ -278,6 +285,13 @@ For the ``_IRQ_GATTS_READ_REQUEST`` event, the available return codes are:: _GATTS_ERROR_INSUFFICIENT_AUTHORIZATION = const(0x08) _GATTS_ERROR_INSUFFICIENT_ENCRYPTION = const(0x0f) +For the ``_IRQ_PASSKEY_ACTION`` event, the available actions are:: + + _PASSKEY_ACTION_NONE = const(0) + _PASSKEY_ACTION_INPUT = const(2) + _PASSKEY_ACTION_DISPLAY = const(3) + _PASSKEY_ACTION_NUMERIC_COMPARISON = const(4) + In order to save space in the firmware, these constants are not included on the :mod:`ubluetooth` module. Add the ones that you need from the list above to your program. @@ -692,6 +706,22 @@ Pairing and bonding On successful pairing, the ``_IRQ_ENCRYPTION_UPDATE`` event will be raised. +.. method:: BLE.gap_passkey(conn_handle, action, passkey, /) + + Respond to a ``_IRQ_PASSKEY_ACTION`` event for the specified *conn_handle* + and *action*. + + The *passkey* is a numeric value and will depend on on the + *action* (which will depend on what I/O capability has been set): + + * When the *action* is ``_PASSKEY_ACTION_INPUT``, then the application should + prompt the user to enter the passkey that is shown on the remote device. + * When the *action* is ``_PASSKEY_ACTION_DISPLAY``, then the application should + generate a random 6-digit passkey and show it to the user. + * When the *action* is ``_PASSKEY_ACTION_NUMERIC_COMPARISON``, then the application + should show the passkey that was provided in the ``_IRQ_PASSKEY_ACTION`` event + and then respond with either ``0`` (cancel pairing), or ``1`` (accept pairing). + class UUID ---------- From d79b9c6c7cb42c69c15d1b7869a04e889d812b82 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 27 Nov 2020 17:08:22 +1100 Subject: [PATCH 0457/3533] extmod/nimble: Generate and persist a unique IRK. This provides a workaround for https://github.com/apache/mynewt-nimble/issues/887. Without this, all devices would share a fixed default IRK. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index ae727086eaeda..613e325a953f5 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -189,6 +189,59 @@ STATIC void set_random_address(bool nrpa) { assert(rc == 0); } +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +// For ble_hs_pvcy_set_our_irk +#include "nimble/host/src/ble_hs_pvcy_priv.h" +// For ble_hs_hci_util_rand +#include "nimble/host/src/ble_hs_hci_priv.h" +// For ble_hs_misc_restore_irks +#include "nimble/host/src/ble_hs_priv.h" + +// Must be distinct to BLE_STORE_OBJ_TYPE_ in ble_store.h. +#define SECRET_TYPE_OUR_IRK 10 + +STATIC int load_irk(void) { + // NimBLE unconditionally loads a fixed IRK on startup. + // See https://github.com/apache/mynewt-nimble/issues/887 + + // Dummy key to use for the store. + // Technically the secret type is enough as there will only be + // one IRK so the key doesn't matter, but a NULL (None) key means "search". + const uint8_t key[3] = {'i', 'r', 'k'}; + + int rc; + const uint8_t *irk; + size_t irk_len; + if (mp_bluetooth_gap_on_get_secret(SECRET_TYPE_OUR_IRK, 0, key, sizeof(key), &irk, &irk_len) && irk_len == 16) { + DEBUG_printf("load_irk: Applying IRK from store.\n"); + rc = ble_hs_pvcy_set_our_irk(irk); + if (rc) { + return rc; + } + } else { + DEBUG_printf("load_irk: Generating new IRK.\n"); + uint8_t rand_irk[16]; + rc = ble_hs_hci_util_rand(rand_irk, 16); + if (rc) { + return rc; + } + DEBUG_printf("load_irk: Saving new IRK.\n"); + if (!mp_bluetooth_gap_on_set_secret(SECRET_TYPE_OUR_IRK, key, sizeof(key), rand_irk, 16)) { + return BLE_HS_EINVAL; + } + DEBUG_printf("load_irk: Applying new IRK.\n"); + rc = ble_hs_pvcy_set_our_irk(rand_irk); + if (rc) { + return rc; + } + } + + // Loading an IRK will clear all peer IRKs, so reload them from the store. + rc = ble_hs_misc_restore_irks(); + return rc; +} +#endif + STATIC void sync_cb(void) { int rc; (void)rc; @@ -199,6 +252,11 @@ STATIC void sync_cb(void) { return; } + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + rc = load_irk(); + assert(rc == 0); + #endif + if (has_public_address()) { nimble_address_mode = BLE_OWN_ADDR_PUBLIC; } else { From c8b055717805500494a14870a4200bbc933fe337 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 1 Dec 2020 22:00:02 +1100 Subject: [PATCH 0458/3533] tests/multi_bluetooth: Add multitests for BLE pairing and bonding. Signed-off-by: Damien George --- tests/multi_bluetooth/ble_gap_pair.py | 129 +++++++++++++++++ tests/multi_bluetooth/ble_gap_pair.py.exp | 17 +++ tests/multi_bluetooth/ble_gap_pair_bond.py | 134 ++++++++++++++++++ .../multi_bluetooth/ble_gap_pair_bond.py.exp | 17 +++ 4 files changed, 297 insertions(+) create mode 100644 tests/multi_bluetooth/ble_gap_pair.py create mode 100644 tests/multi_bluetooth/ble_gap_pair.py.exp create mode 100644 tests/multi_bluetooth/ble_gap_pair_bond.py create mode 100644 tests/multi_bluetooth/ble_gap_pair_bond.py.exp diff --git a/tests/multi_bluetooth/ble_gap_pair.py b/tests/multi_bluetooth/ble_gap_pair.py new file mode 100644 index 0000000000000..728513405ee79 --- /dev/null +++ b/tests/multi_bluetooth/ble_gap_pair.py @@ -0,0 +1,129 @@ +# Test BLE GAP connect/disconnect with pairing, and read an encrypted characteristic +# TODO: add gap_passkey testing + +from micropython import const +import time, machine, bluetooth + +TIMEOUT_MS = 4000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_ENCRYPTION_UPDATE = const(28) + +_FLAG_READ = const(0x0002) +_FLAG_READ_ENCRYPTED = const(0x0200) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = (CHAR_UUID, _FLAG_READ | _FLAG_READ_ENCRYPTED) +SERVICE = (SERVICE_UUID, (CHAR,)) + +waiting_events = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_CENTRAL_DISCONNECT: + print("_IRQ_CENTRAL_DISCONNECT") + elif event == _IRQ_GATTS_READ_REQUEST: + print("_IRQ_GATTS_READ_REQUEST") + elif event == _IRQ_PERIPHERAL_CONNECT: + print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_DISCONNECT: + print("_IRQ_PERIPHERAL_DISCONNECT") + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + if data[-1] == CHAR_UUID: + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") + elif event == _IRQ_GATTC_READ_RESULT: + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) + elif event == _IRQ_ENCRYPTION_UPDATE: + print("_IRQ_ENCRYPTION_UPDATE", data[1], data[2], data[3]) + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services((SERVICE,)) + ble.gatts_write(char_handle, "encrypted") + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect. + wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + + # Wait for pairing event. + wait_for_event(_IRQ_ENCRYPTION_UPDATE, TIMEOUT_MS) + + # Wait for GATTS read request. + wait_for_event(_IRQ_GATTS_READ_REQUEST, TIMEOUT_MS) + + # Wait for central to disconnect. + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + multitest.next() + try: + # Connect to peripheral. + print("gap_connect") + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + # Discover characteristics (before pairing, doesn't need to be encrypted). + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + # Pair with the peripheral. + print("gap_pair") + ble.gap_pair(conn_handle) + + # Wait for the pairing event. + wait_for_event(_IRQ_ENCRYPTION_UPDATE, TIMEOUT_MS) + + # Read the peripheral's characteristic, should be encrypted. + print("gattc_read") + ble.gattc_read(conn_handle, value_handle) + wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) + + # Disconnect from the peripheral. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.config(mitm=True, le_secure=True, bond=False) +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/ble_gap_pair.py.exp b/tests/multi_bluetooth/ble_gap_pair.py.exp new file mode 100644 index 0000000000000..9efe0fb22b2b4 --- /dev/null +++ b/tests/multi_bluetooth/ble_gap_pair.py.exp @@ -0,0 +1,17 @@ +--- instance0 --- +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_ENCRYPTION_UPDATE 1 0 0 +_IRQ_GATTS_READ_REQUEST +_IRQ_CENTRAL_DISCONNECT +--- instance1 --- +gap_connect +_IRQ_PERIPHERAL_CONNECT +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gap_pair +_IRQ_ENCRYPTION_UPDATE 1 0 0 +gattc_read +_IRQ_GATTC_READ_RESULT b'encrypted' +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT diff --git a/tests/multi_bluetooth/ble_gap_pair_bond.py b/tests/multi_bluetooth/ble_gap_pair_bond.py new file mode 100644 index 0000000000000..a29c217887b45 --- /dev/null +++ b/tests/multi_bluetooth/ble_gap_pair_bond.py @@ -0,0 +1,134 @@ +# Test BLE GAP connect/disconnect with pairing and bonding, and read an encrypted +# characteristic +# TODO: reconnect after bonding to test that the secrets persist + +from micropython import const +import time, machine, bluetooth + +TIMEOUT_MS = 4000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_READ_REQUEST = const(4) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_READ_RESULT = const(15) +_IRQ_ENCRYPTION_UPDATE = const(28) +_IRQ_SET_SECRET = const(30) + +_FLAG_READ = const(0x0002) +_FLAG_READ_ENCRYPTED = const(0x0200) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = (CHAR_UUID, _FLAG_READ | _FLAG_READ_ENCRYPTED) +SERVICE = (SERVICE_UUID, (CHAR,)) + +waiting_events = {} +secrets = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + print("_IRQ_CENTRAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_CENTRAL_DISCONNECT: + print("_IRQ_CENTRAL_DISCONNECT") + elif event == _IRQ_GATTS_READ_REQUEST: + print("_IRQ_GATTS_READ_REQUEST") + elif event == _IRQ_PERIPHERAL_CONNECT: + print("_IRQ_PERIPHERAL_CONNECT") + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_DISCONNECT: + print("_IRQ_PERIPHERAL_DISCONNECT") + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + if data[-1] == CHAR_UUID: + print("_IRQ_GATTC_CHARACTERISTIC_RESULT", data[-1]) + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: + print("_IRQ_GATTC_CHARACTERISTIC_DONE") + elif event == _IRQ_GATTC_READ_RESULT: + print("_IRQ_GATTC_READ_RESULT", bytes(data[-1])) + elif event == _IRQ_ENCRYPTION_UPDATE: + print("_IRQ_ENCRYPTION_UPDATE", data[1], data[2], data[3]) + elif event == _IRQ_SET_SECRET: + return True + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services((SERVICE,)) + ble.gatts_write(char_handle, "encrypted") + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect. + wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + + # Wait for pairing event. + wait_for_event(_IRQ_ENCRYPTION_UPDATE, TIMEOUT_MS) + + # Wait for GATTS read request. + wait_for_event(_IRQ_GATTS_READ_REQUEST, TIMEOUT_MS) + + # Wait for central to disconnect. + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + multitest.next() + try: + # Connect to peripheral. + print("gap_connect") + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + # Discover characteristics (before pairing, doesn't need to be encrypted). + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + # Pair with the peripheral. + print("gap_pair") + ble.gap_pair(conn_handle) + + # Wait for the pairing event. + wait_for_event(_IRQ_ENCRYPTION_UPDATE, TIMEOUT_MS) + + # Read the peripheral's characteristic, should be encrypted. + print("gattc_read") + ble.gattc_read(conn_handle, value_handle) + wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS) + + # Disconnect from the peripheral. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.config(mitm=True, le_secure=True, bond=True) +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/ble_gap_pair_bond.py.exp b/tests/multi_bluetooth/ble_gap_pair_bond.py.exp new file mode 100644 index 0000000000000..271403a71a8c7 --- /dev/null +++ b/tests/multi_bluetooth/ble_gap_pair_bond.py.exp @@ -0,0 +1,17 @@ +--- instance0 --- +gap_advertise +_IRQ_CENTRAL_CONNECT +_IRQ_ENCRYPTION_UPDATE 1 0 1 +_IRQ_GATTS_READ_REQUEST +_IRQ_CENTRAL_DISCONNECT +--- instance1 --- +gap_connect +_IRQ_PERIPHERAL_CONNECT +_IRQ_GATTC_CHARACTERISTIC_RESULT UUID('00000000-1111-2222-3333-444444444444') +_IRQ_GATTC_CHARACTERISTIC_DONE +gap_pair +_IRQ_ENCRYPTION_UPDATE 1 0 1 +gattc_read +_IRQ_GATTC_READ_RESULT b'encrypted' +gap_disconnect: True +_IRQ_PERIPHERAL_DISCONNECT From 849748873cd838be0d91555b47f1447f4476175a Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 25 Nov 2020 15:15:49 +0200 Subject: [PATCH 0459/3533] stm32/modmachine: Add device and revision ids to machine.info(). --- ports/stm32/modmachine.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 9fa00a915186f..c5f49b6ef4395 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -155,6 +155,8 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) { printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]); } + printf("DEVID=0x%04x\nREVID=0x%04x\n", (unsigned int)HAL_GetDEVID(), (unsigned int)HAL_GetREVID()); + // get and print clock speeds // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz { From ce9197eb209bacd1453ae2ef83523aa0fa1f9b45 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Fri, 27 Nov 2020 02:38:00 +0200 Subject: [PATCH 0460/3533] stm32/Makefile: Disable text compression in debug builds. Otherwise the flash overflows. Fixes issue #6653. --- ports/stm32/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index ced851ca38e27..c3b7bbba98c61 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -134,6 +134,8 @@ LDFLAGS += --gc-sections ifeq ($(DEBUG), 1) CFLAGS += -g -DPENDSV_DEBUG COPT = -O0 +# Disable text compression in debug builds +MICROPY_ROM_TEXT_COMPRESSION = 0 else COPT += -Os -DNDEBUG endif From e9e619fa24cca3aa6788931d6fee561436576f83 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Dec 2020 23:16:10 +0200 Subject: [PATCH 0461/3533] stm32/powerctrl: Define RCC_SR_SFTRSTF flag for H747. --- ports/stm32/powerctrl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 946185fba09d3..01ad1834184b4 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -32,7 +32,11 @@ #if defined(STM32H7) #define RCC_SR RSR +#if defined(STM32H743xx) #define RCC_SR_SFTRSTF RCC_RSR_SFTRSTF +#elif defined(STM32H747xx) +#define RCC_SR_SFTRSTF RCC_RSR_SFT2RSTF +#endif #define RCC_SR_RMVF RCC_RSR_RMVF #else #define RCC_SR CSR From 3e5dd2dbcc87710f9f3f3bf4cd732ecdcc922b1f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Dec 2020 23:31:56 +0200 Subject: [PATCH 0462/3533] stm32/powerctrl: Fix STOP mode voltage scaling on H7 REV V devices. --- ports/stm32/powerctrl.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 01ad1834184b4..7d368169790d1 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -38,6 +38,12 @@ #define RCC_SR_SFTRSTF RCC_RSR_SFT2RSTF #endif #define RCC_SR_RMVF RCC_RSR_RMVF +// This macro returns the actual voltage scaling level factoring in the power overdrive bit. +// If the current voltage scale is VOLTAGE_SCALE1 and PWER_ODEN bit is set return VOLTAGE_SCALE0 +// otherwise the current voltage scaling (level VOS1 to VOS3) set in PWER_CSR is returned instead. +#define POWERCTRL_GET_VOLTAGE_SCALING() \ + (((PWR->CSR1 & PWR_CSR1_ACTVOS) && (SYSCFG->PWRCR & SYSCFG_PWRCR_ODEN)) ? \ + PWR_REGULATOR_VOLTAGE_SCALE0 : (PWR->CSR1 & PWR_CSR1_ACTVOS)) #else #define RCC_SR CSR #define RCC_SR_SFTRSTF RCC_CSR_SFTRSTF @@ -510,6 +516,19 @@ void powerctrl_enter_stop_mode(void) { HAL_PWREx_EnableFlashPowerDown(); #endif + #if defined(STM32H7) + // Save the current voltage scaling level to restore after exiting low power mode. + uint32_t vscaling = POWERCTRL_GET_VOLTAGE_SCALING(); + + // If the current voltage scaling level is 0, switch to level 1 before entering low power mode. + if (vscaling == PWR_REGULATOR_VOLTAGE_SCALE0) { + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + // Wait for PWR_FLAG_VOSRDY + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) { + } + } + #endif + #if defined(STM32F7) HAL_PWR_EnterSTOPMode((PWR_CR1_LPDS | PWR_CR1_LPUDS | PWR_CR1_FPDS | PWR_CR1_UDEN), PWR_STOPENTRY_WFI); #else @@ -532,6 +551,17 @@ void powerctrl_enter_stop_mode(void) { #else + #if defined(STM32H7) + // When exiting from Stop or Standby modes, the Run mode voltage scaling is reset to + // the default VOS3 value. Restore the voltage scaling to the previous voltage scale. + if (vscaling != POWERCTRL_GET_VOLTAGE_SCALING()) { + __HAL_PWR_VOLTAGESCALING_CONFIG(vscaling); + // Wait for PWR_FLAG_VOSRDY + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) { + } + } + #endif + #if !defined(STM32L4) // enable clock __HAL_RCC_HSE_CONFIG(MICROPY_HW_RCC_HSE_STATE); From 463a275bc4e99a1d37d523a120fe45976ea021fe Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Dec 2020 23:33:56 +0200 Subject: [PATCH 0463/3533] stm32/powerctrl: On H7, re-enable disabled OSCs/PLLs on exit from STOP. This commit saves OSCs/PLLs state before STOP mode and restores them on exit. Some boards use HSI48 for USB for example, others have PLL2/3 enabled, etc. --- ports/stm32/powerctrl.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 7d368169790d1..c36351e311987 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -517,6 +517,9 @@ void powerctrl_enter_stop_mode(void) { #endif #if defined(STM32H7) + // Save RCC CR to re-enable OSCs and PLLs after wake up from low power mode. + uint32_t rcc_cr = RCC->CR; + // Save the current voltage scaling level to restore after exiting low power mode. uint32_t vscaling = POWERCTRL_GET_VOLTAGE_SCALING(); @@ -607,9 +610,39 @@ void powerctrl_enter_stop_mode(void) { #endif #if defined(STM32H7) - // Enable PLL3 for USB - RCC->CR |= RCC_CR_PLL3ON; - while (!(RCC->CR & RCC_CR_PLL3RDY)) { + // Enable HSI + if (rcc_cr & RCC_CR_HSION) { + RCC->CR |= RCC_CR_HSION; + while (!(RCC->CR & RCC_CR_HSIRDY)) { + } + } + + // Enable CSI + if (rcc_cr & RCC_CR_CSION) { + RCC->CR |= RCC_CR_CSION; + while (!(RCC->CR & RCC_CR_CSIRDY)) { + } + } + + // Enable HSI48 + if (rcc_cr & RCC_CR_HSI48ON) { + RCC->CR |= RCC_CR_HSI48ON; + while (!(RCC->CR & RCC_CR_HSI48RDY)) { + } + } + + // Enable PLL2 + if (rcc_cr & RCC_CR_PLL2ON) { + RCC->CR |= RCC_CR_PLL2ON; + while (!(RCC->CR & RCC_CR_PLL2RDY)) { + } + } + + // Enable PLL3 + if (rcc_cr & RCC_CR_PLL3ON) { + RCC->CR |= RCC_CR_PLL3ON; + while (!(RCC->CR & RCC_CR_PLL3RDY)) { + } } #endif From 8add94e94e906ecf6c46d49127a8a0590be308b8 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Dec 2020 23:35:58 +0200 Subject: [PATCH 0464/3533] stm32/powerctrl: Disable RTC write protection before changing flags. --- ports/stm32/powerctrl.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index c36351e311987..ce2f6e1905c5d 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -689,6 +689,10 @@ void powerctrl_enter_standby_mode(void) { // save RTC interrupts uint32_t save_irq_bits = RTC->CR & CR_BITS; + // disable register write protection + RTC->WPR = 0xca; + RTC->WPR = 0x53; + // disable RTC interrupts RTC->CR &= ~CR_BITS; @@ -714,6 +718,9 @@ void powerctrl_enter_standby_mode(void) { // enable previously-enabled RTC interrupts RTC->CR |= save_irq_bits; + // enable register write protection + RTC->WPR = 0xff; + #if defined(STM32F7) // Enable the internal (eg RTC) wakeup sources // See Errata 2.2.2 "Wakeup from Standby mode when the back-up SRAM regulator is enabled" From 7b9b6d080a2d7de6db73fe4caaca29f54a1d716d Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Dec 2020 23:36:47 +0200 Subject: [PATCH 0465/3533] stm32/powerctrl: Set H7 RTC wakeup flags. --- ports/stm32/powerctrl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index ce2f6e1905c5d..076215ba9f287 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -705,7 +705,8 @@ void powerctrl_enter_standby_mode(void) { // clear global wake-up flag PWR->CR2 |= PWR_CR2_CWUPF6 | PWR_CR2_CWUPF5 | PWR_CR2_CWUPF4 | PWR_CR2_CWUPF3 | PWR_CR2_CWUPF2 | PWR_CR2_CWUPF1; #elif defined(STM32H7) - // TODO + EXTI_D1->PR1 = 0x3fffff; + PWR->WKUPCR |= PWR_WAKEUP_FLAG1 | PWR_WAKEUP_FLAG2 | PWR_WAKEUP_FLAG3 | PWR_WAKEUP_FLAG4 | PWR_WAKEUP_FLAG5 | PWR_WAKEUP_FLAG6; #elif defined(STM32L4) || defined(STM32WB) // clear all wake-up flags PWR->SCR |= PWR_SCR_CWUF5 | PWR_SCR_CWUF4 | PWR_SCR_CWUF3 | PWR_SCR_CWUF2 | PWR_SCR_CWUF1; From 7dc2f4ed384f45eb943e0b7dc39a539af7058b80 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 3 Dec 2020 19:46:07 +0200 Subject: [PATCH 0466/3533] stm32/powerctrl: Ensure SysTick is disabled on STOP mode entry for H7. Even though IRQs are disabled this seems to be required on H7 Rev Y, otherwise Systick interrupt triggers and the MCU leaves the stop mode immediately. --- ports/stm32/powerctrl.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 076215ba9f287..bf8be647f4d76 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -502,6 +502,13 @@ void powerctrl_enter_stop_mode(void) { // executed until after the clocks are reconfigured uint32_t irq_state = disable_irq(); + #if defined(STM32H7) + // Disable SysTick Interrupt + // Note: This seems to be required at least on the H7 REV Y, + // otherwise the MCU will leave stop mode immediately on entry. + SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; + #endif + #if defined(MICROPY_BOARD_ENTER_STOP) MICROPY_BOARD_ENTER_STOP #endif @@ -659,6 +666,11 @@ void powerctrl_enter_stop_mode(void) { MICROPY_BOARD_LEAVE_STOP #endif + #if defined(STM32H7) + // Enable SysTick Interrupt + SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; + #endif + // Enable IRQs now that all clocks are reconfigured enable_irq(irq_state); } From 4ce6427bd71129b1ab3b9eeaf2c1182d623cd7b9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 3 Dec 2020 12:58:48 +1100 Subject: [PATCH 0467/3533] stm32/i2c: Factor I2C finding code to i2c_find_peripheral function. Signed-off-by: Damien George --- ports/stm32/i2c.c | 50 +++++++++++++++++++++++++++++++++++++++ ports/stm32/i2c.h | 2 ++ ports/stm32/machine_i2c.c | 33 +------------------------- ports/stm32/pyb_i2c.c | 33 +------------------------- 4 files changed, 54 insertions(+), 64 deletions(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index 5981df11c5bd8..d67201b714418 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -26,6 +26,7 @@ #include "py/mperrno.h" #include "py/mphal.h" +#include "py/runtime.h" #include "i2c.h" #if MICROPY_HW_ENABLE_HW_I2C @@ -487,4 +488,53 @@ int i2c_writeto(i2c_t *i2c, uint16_t addr, const uint8_t *src, size_t len, bool #endif +STATIC const uint8_t i2c_available = + 0 + #if defined(MICROPY_HW_I2C1_SCL) + | 1 << 1 + #endif + #if defined(MICROPY_HW_I2C2_SCL) + | 1 << 2 + #endif + #if defined(MICROPY_HW_I2C3_SCL) + | 1 << 3 + #endif + #if defined(MICROPY_HW_I2C4_SCL) + | 1 << 4 + #endif +; + +int i2c_find_peripheral(mp_obj_t id) { + int i2c_id = 0; + if (mp_obj_is_str(id)) { + const char *port = mp_obj_str_get_str(id); + if (0) { + #ifdef MICROPY_HW_I2C1_NAME + } else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) { + i2c_id = 1; + #endif + #ifdef MICROPY_HW_I2C2_NAME + } else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) { + i2c_id = 2; + #endif + #ifdef MICROPY_HW_I2C3_NAME + } else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) { + i2c_id = 3; + #endif + #ifdef MICROPY_HW_I2C4_NAME + } else if (strcmp(port, MICROPY_HW_I2C4_NAME) == 0) { + i2c_id = 4; + #endif + } else { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%s) doesn't exist"), port); + } + } else { + i2c_id = mp_obj_get_int(id); + if (i2c_id < 1 || i2c_id >= 8 * sizeof(i2c_available) || !(i2c_available & (1 << i2c_id))) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); + } + } + return i2c_id; +} + #endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/i2c.h b/ports/stm32/i2c.h index 4846f1cf3ab52..d494ad66d57f5 100644 --- a/ports/stm32/i2c.h +++ b/ports/stm32/i2c.h @@ -62,4 +62,6 @@ int i2c_write(i2c_t *i2c, const uint8_t *src, size_t len, size_t next_len); int i2c_readfrom(i2c_t *i2c, uint16_t addr, uint8_t *dest, size_t len, bool stop); int i2c_writeto(i2c_t *i2c, uint16_t addr, const uint8_t *src, size_t len, bool stop); +int i2c_find_peripheral(mp_obj_t id); + #endif // MICROPY_INCLUDED_STM32_I2C_H diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index cfa00b86d830b..41e65cf05cb37 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -210,39 +210,8 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - // work out i2c bus - int i2c_id = 0; - if (mp_obj_is_str(args[ARG_id].u_obj)) { - const char *port = mp_obj_str_get_str(args[ARG_id].u_obj); - if (0) { - #ifdef MICROPY_HW_I2C1_NAME - } else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) { - i2c_id = 1; - #endif - #ifdef MICROPY_HW_I2C2_NAME - } else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) { - i2c_id = 2; - #endif - #ifdef MICROPY_HW_I2C3_NAME - } else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) { - i2c_id = 3; - #endif - #ifdef MICROPY_HW_I2C4_NAME - } else if (strcmp(port, MICROPY_HW_I2C4_NAME) == 0) { - i2c_id = 4; - #endif - } else { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%s) doesn't exist"), port); - } - } else { - i2c_id = mp_obj_get_int(args[ARG_id].u_obj); - if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(machine_hard_i2c_obj) - || machine_hard_i2c_obj[i2c_id - 1].base.type == NULL) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); - } - } - // get static peripheral object + int i2c_id = i2c_find_peripheral(args[ARG_id].u_obj); machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)&machine_hard_i2c_obj[i2c_id - 1]; // here we would check the scl/sda pins and configure them, but it's not implemented diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index 4f8f7a0e3741c..ee8b498a1359c 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -668,39 +668,8 @@ STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_ // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - // work out i2c bus - int i2c_id = 0; - if (mp_obj_is_str(args[0])) { - const char *port = mp_obj_str_get_str(args[0]); - if (0) { - #ifdef MICROPY_HW_I2C1_NAME - } else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) { - i2c_id = 1; - #endif - #ifdef MICROPY_HW_I2C2_NAME - } else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) { - i2c_id = 2; - #endif - #ifdef MICROPY_HW_I2C3_NAME - } else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) { - i2c_id = 3; - #endif - #ifdef MICROPY_HW_I2C4_NAME - } else if (strcmp(port, MICROPY_HW_I2C4_NAME) == 0) { - i2c_id = 4; - #endif - } else { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%s) doesn't exist"), port); - } - } else { - i2c_id = mp_obj_get_int(args[0]); - if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(pyb_i2c_obj) - || pyb_i2c_obj[i2c_id - 1].i2c == NULL) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); - } - } - // get I2C object + int i2c_id = i2c_find_peripheral(args[0]); const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id - 1]; if (n_args > 1 || n_kw > 0) { From 1e4e2644ecfd2cab5154a2ad95f3eb5aca545ba7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 3 Dec 2020 13:07:37 +1100 Subject: [PATCH 0468/3533] stm32: Add support for a board to reserve certain peripherals. Allows reserving CAN, I2C, SPI, Timer and UART peripherals. If reserved the peripheral cannot be accessed from Python. Signed-off-by: Damien George --- ports/stm32/i2c.c | 6 ++++++ ports/stm32/machine_uart.c | 5 +++++ ports/stm32/mpconfigboard_common.h | 25 ++++++++++++++++++++++++ ports/stm32/pyb_can.c | 5 +++++ ports/stm32/spi.c | 31 ++++++++++++++++++------------ ports/stm32/timer.c | 5 +++++ 6 files changed, 65 insertions(+), 12 deletions(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index d67201b714418..0b7105344cd28 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -534,6 +534,12 @@ int i2c_find_peripheral(mp_obj_t id) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); } } + + // check if the I2C is reserved for system use or not + if (MICROPY_HW_I2C_IS_RESERVED(i2c_id)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) is reserved"), i2c_id); + } + return i2c_id; } diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 2862f4c0e7b15..31e6a1789e4a3 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -345,6 +345,11 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size } } + // check if the UART is reserved for system use or not + if (MICROPY_HW_UART_IS_RESERVED(uart_id)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) is reserved"), uart_id); + } + pyb_uart_obj_t *self; if (MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] == NULL) { // create new UART object diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index a73a26b1627b4..8890abc59bbe1 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -127,6 +127,31 @@ #define MICROPY_HW_FLASH_FS_LABEL "pybflash" #endif +// Function to determine if the given can_id is reserved for system use or not. +#ifndef MICROPY_HW_CAN_IS_RESERVED +#define MICROPY_HW_CAN_IS_RESERVED(can_id) (false) +#endif + +// Function to determine if the given i2c_id is reserved for system use or not. +#ifndef MICROPY_HW_I2C_IS_RESERVED +#define MICROPY_HW_I2C_IS_RESERVED(i2c_id) (false) +#endif + +// Function to determine if the given spi_id is reserved for system use or not. +#ifndef MICROPY_HW_SPI_IS_RESERVED +#define MICROPY_HW_SPI_IS_RESERVED(spi_id) (false) +#endif + +// Function to determine if the given tim_id is reserved for system use or not. +#ifndef MICROPY_HW_TIM_IS_RESERVED +#define MICROPY_HW_TIM_IS_RESERVED(tim_id) (false) +#endif + +// Function to determine if the given uart_id is reserved for system use or not. +#ifndef MICROPY_HW_UART_IS_RESERVED +#define MICROPY_HW_UART_IS_RESERVED(uart_id) (false) +#endif + /*****************************************************************************/ // General configuration diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index 224b8f28b0eab..def66481cbdc8 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -203,6 +203,11 @@ STATIC mp_obj_t pyb_can_make_new(const mp_obj_type_t *type, size_t n_args, size_ mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) doesn't exist"), can_idx); } + // check if the CAN is reserved for system use or not + if (MICROPY_HW_CAN_IS_RESERVED(can_idx)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) is reserved"), can_idx); + } + pyb_can_obj_t *self; if (MP_STATE_PORT(pyb_can_obj_all)[can_idx - 1] == NULL) { self = m_new_obj(pyb_can_obj_t); diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 5f9b1c1a28d7f..07374d7a38eef 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -167,45 +167,52 @@ void spi_init0(void) { } int spi_find_index(mp_obj_t id) { + int spi_id; if (mp_obj_is_str(id)) { // given a string id const char *port = mp_obj_str_get_str(id); if (0) { #ifdef MICROPY_HW_SPI1_NAME } else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) { - return 1; + spi_id = 1; #endif #ifdef MICROPY_HW_SPI2_NAME } else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) { - return 2; + spi_id = 2; #endif #ifdef MICROPY_HW_SPI3_NAME } else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) { - return 3; + spi_id = 3; #endif #ifdef MICROPY_HW_SPI4_NAME } else if (strcmp(port, MICROPY_HW_SPI4_NAME) == 0) { - return 4; + spi_id = 4; #endif #ifdef MICROPY_HW_SPI5_NAME } else if (strcmp(port, MICROPY_HW_SPI5_NAME) == 0) { - return 5; + spi_id = 5; #endif #ifdef MICROPY_HW_SPI6_NAME } else if (strcmp(port, MICROPY_HW_SPI6_NAME) == 0) { - return 6; + spi_id = 6; #endif + } else { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%s) doesn't exist"), port); } - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%s) doesn't exist"), port); } else { // given an integer id - int spi_id = mp_obj_get_int(id); - if (spi_id >= 1 && spi_id <= MP_ARRAY_SIZE(spi_obj) - && spi_obj[spi_id - 1].spi != NULL) { - return spi_id; + spi_id = mp_obj_get_int(id); + if (spi_id < 1 || spi_id > MP_ARRAY_SIZE(spi_obj) || spi_obj[spi_id - 1].spi == NULL) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); } - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); } + + // check if the SPI is reserved for system use or not + if (MICROPY_HW_SPI_IS_RESERVED(spi_id)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) is reserved"), spi_id); + } + + return spi_id; } STATIC uint32_t spi_get_source_freq(SPI_HandleTypeDef *spi) { diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 9b8c14c0da552..a34d2984da979 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -896,6 +896,11 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Timer(%d) doesn't exist"), tim_id); } + // check if the timer is reserved for system use or not + if (MICROPY_HW_TIM_IS_RESERVED(tim_id)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Timer(%d) is reserved"), tim_id); + } + pyb_timer_obj_t *tim; if (MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] == NULL) { // create new Timer object From cb1bb7592e18a50f33d22f84614c5ee9f45ce21e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 7 Dec 2020 17:11:49 +1100 Subject: [PATCH 0469/3533] stm32/Makefile: Change -O0 to -Og for DEBUG=1 builds. The -Og optimisation level produces a more realistic build, gives a better debugging experience, and generates smaller code than -O0, allowing debug builds to fit in flash. This commit also assigns variables in can.c to prevent warnings when -Og is used, and builds a board in CI with DEBUG=1 enabled. Signed-off-by: Damien George --- ports/stm32/Makefile | 2 +- ports/stm32/can.c | 4 ++-- tools/ci.sh | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index c3b7bbba98c61..a9dac03d2c021 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -133,7 +133,7 @@ LDFLAGS += --gc-sections # Debugging/Optimization ifeq ($(DEBUG), 1) CFLAGS += -g -DPENDSV_DEBUG -COPT = -O0 +COPT = -Og # Disable text compression in debug builds MICROPY_ROM_TEXT_COMPRESSION = 0 else diff --git a/ports/stm32/can.c b/ports/stm32/can.c index e5fc55e409bbc..bcc6983594967 100644 --- a/ports/stm32/can.c +++ b/ports/stm32/can.c @@ -226,8 +226,8 @@ int can_receive(CAN_HandleTypeDef *can, int fifo, CanRxMsgTypeDef *msg, uint8_t HAL_StatusTypeDef CAN_Transmit(CAN_HandleTypeDef *hcan, uint32_t Timeout) { uint32_t transmitmailbox; uint32_t tickstart; - uint32_t rqcpflag; - uint32_t txokflag; + uint32_t rqcpflag = 0; + uint32_t txokflag = 0; // Check the parameters assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE)); diff --git a/tools/ci.sh b/tools/ci.sh index 3b906b449c112..ded9fab922d50 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -200,7 +200,7 @@ function ci_stm32_nucleo_build { make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_F091RC make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_H743ZI CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L073RZ - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L476RG + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_L476RG DEBUG=1 make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_WB55 make ${MAKEOPTS} -C ports/stm32/mboot BOARD=NUCLEO_WB55 } From 92a5ee6ac1c50084f8c29d4accda67cca91b6846 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Mon, 30 Nov 2020 10:45:05 -0600 Subject: [PATCH 0470/3533] zephyr: Replace broken shell_net_iface() with more general shell_exec(). The zephyr function net_shell_cmd_iface() was removed in zephyr v1.14.0, therefore the MicroPython zephyr port did not build with newer zephyr versions when CONFIG_NET_SHELL=y. Replace with a more general shell_exec() function that can execute any zephyr shell command. For example: >>> zephyr.shell_exec("net") Subcommands: allocs :Print network memory allocations. arp :Print information about IPv4 ARP cache. conn :Print information about network connections. dns :Show how DNS is configured. events :Monitor network management events. gptp :Print information about gPTP support. iface :Print information about network interfaces. ipv6 :Print information about IPv6 specific information and configuration. mem :Print information about network memory usage. nbr :Print neighbor information. ping :Ping a network host. pkt :net_pkt information. ppp :PPP information. resume :Resume a network interface route :Show network route. stacks :Show network stacks information. stats :Show network statistics. suspend :Suspend a network interface tcp :Connect/send/close TCP connection. vlan :Show VLAN information. websocket :Print information about WebSocket connections. >>> zephyr.shell_exec("kernel") kernel - Kernel commands Subcommands: cycles :Kernel cycles. reboot :Reboot. stacks :List threads stack usage. threads :List kernel threads. uptime :Kernel uptime. version :Kernel version. Signed-off-by: Maureen Helm --- ports/zephyr/modzephyr.c | 22 ++++++++++------------ ports/zephyr/prj.conf | 6 +++++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ports/zephyr/modzephyr.c b/ports/zephyr/modzephyr.c index 71b44d7df1611..52449ea381c42 100644 --- a/ports/zephyr/modzephyr.c +++ b/ports/zephyr/modzephyr.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include "modzephyr.h" #include "py/runtime.h" @@ -53,17 +55,14 @@ STATIC mp_obj_t mod_thread_analyze(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_analyze_obj, mod_thread_analyze); #endif -#ifdef CONFIG_NET_SHELL - -// int net_shell_cmd_iface(int argc, char *argv[]); - -STATIC mp_obj_t mod_shell_net_iface(void) { - net_shell_cmd_iface(0, NULL); +#ifdef CONFIG_SHELL_BACKEND_SERIAL +STATIC mp_obj_t mod_shell_exec(mp_obj_t cmd_in) { + const char *cmd = mp_obj_str_get_str(cmd_in); + shell_execute_cmd(shell_backend_uart_get_ptr(), cmd); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_shell_net_iface_obj, mod_shell_net_iface); - -#endif // CONFIG_NET_SHELL +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_shell_exec_obj, mod_shell_exec); +#endif // CONFIG_SHELL_BACKEND_SERIAL STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr) }, @@ -72,9 +71,8 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { #ifdef CONFIG_THREAD_ANALYZER { MP_ROM_QSTR(MP_QSTR_thread_analyze), MP_ROM_PTR(&mod_thread_analyze_obj) }, #endif - - #ifdef CONFIG_NET_SHELL - { MP_ROM_QSTR(MP_QSTR_shell_net_iface), MP_ROM_PTR(&mod_shell_net_iface_obj) }, + #ifdef CONFIG_SHELL_BACKEND_SERIAL + { MP_ROM_QSTR(MP_QSTR_shell_exec), MP_ROM_PTR(&mod_shell_exec_obj) }, #endif #ifdef CONFIG_DISK_ACCESS { MP_ROM_QSTR(MP_QSTR_DiskAccess), MP_ROM_PTR(&zephyr_disk_access_type) }, diff --git a/ports/zephyr/prj.conf b/ports/zephyr/prj.conf index 50cfa005025dc..9824f48e3362f 100644 --- a/ports/zephyr/prj.conf +++ b/ports/zephyr/prj.conf @@ -57,7 +57,11 @@ CONFIG_THREAD_NAME=y # Required for usocket.pkt_get_info() CONFIG_NET_BUF_POOL_USAGE=y -# Required for usocket.shell_*() +# Required for zephyr.shell_exec() +#CONFIG_SHELL=y +#CONFIG_SHELL_BACKEND_SERIAL_INTERRUPT_DRIVEN=n + +# Required for zephyr.shell_exec("net iface") #CONFIG_NET_SHELL=y # Uncomment to enable "INFO" level net_buf logging From dde0735ac1629aca4f7d41334f25b75dd8d35010 Mon Sep 17 00:00:00 2001 From: Jonathan Bruchim Date: Sun, 8 Nov 2020 20:31:35 +0200 Subject: [PATCH 0471/3533] zephyr: Guard I2C code with appropriate ifdef config. To reduce binary code size when not using I2C. Signed-off-by: Jonathan Bruchim --- ports/zephyr/machine_i2c.c | 4 ++++ ports/zephyr/modmachine.c | 2 ++ 2 files changed, 6 insertions(+) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index efd4bdcb21b58..aa8823392dff0 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -39,6 +39,8 @@ #include "extmod/machine_i2c.h" #include "modmachine.h" +#if MICROPY_PY_MACHINE_I2C + typedef struct _machine_hard_i2c_obj_t { mp_obj_base_t base; const struct device *dev; @@ -136,3 +138,5 @@ const mp_obj_type_t machine_hard_i2c_type = { .protocol = &machine_hard_i2c_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; + +#endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 29e6c889c4cef..968f758b93ed3 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -60,7 +60,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, + #if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, From 5020b14d5419065f1a5ef5aed1be7badee28c9bf Mon Sep 17 00:00:00 2001 From: Joris Peeraer Date: Thu, 22 Oct 2020 10:38:03 +0200 Subject: [PATCH 0472/3533] py/mpprint: Fix length calculation for strings with precision-modifier. Two issues are tackled: 1. The calculation of the correct length to print is fixed to treat the precision as a maximum length instead as the exact length. This is done for both qstr (%q) and for regular str (%s). 2. Fix the incorrect use of mp_printf("%.*s") to mp_print_strn(). Because of the fix of above issue, some testcases that would print an embedded null-byte (^@ in test-output) would now fail. The bug here is that "%s" was used to print null-bytes. Instead, mp_print_strn is used to make sure all bytes are outputted and the exact length is respected. Test-cases are added for both %s and %q with a combination of precision and padding specifiers. --- ports/unix/coverage.c | 2 +- py/mpprint.c | 13 +++++++------ py/objstr.c | 4 ++-- py/objstrunicode.c | 2 +- tests/unix/extra_coverage.py.exp | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 7169c7793bd93..ef66c4fb59440 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -186,7 +186,7 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%ld\n", 123); // long mp_printf(&mp_plat_print, "%lx\n", 0x123); // long hex mp_printf(&mp_plat_print, "%X\n", 0x1abcdef); // capital hex - mp_printf(&mp_plat_print, "%.2s %.3s\n", "abc", "abc"); // fixed string precision + mp_printf(&mp_plat_print, "%.2s %.3s '%4.4s' '%5.5q' '%.3q'\n", "abc", "abc", "abc", MP_QSTR_True, MP_QSTR_True); // fixed string precision mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools #ifndef NDEBUG diff --git a/py/mpprint.c b/py/mpprint.c index 31cf73bb58af9..3218bd2f4dcf8 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -484,10 +484,10 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { qstr qst = va_arg(args, qstr); size_t len; const char *str = (const char *)qstr_data(qst, &len); - if (prec < 0) { - prec = len; + if (prec >= 0 && (size_t)prec < len) { + len = prec; } - chrs += mp_print_strn(print, str, prec, flags, fill, width); + chrs += mp_print_strn(print, str, len, flags, fill, width); break; } case 's': { @@ -499,10 +499,11 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { break; } #endif - if (prec < 0) { - prec = strlen(str); + size_t len = strlen(str); + if (prec >= 0 && (size_t)prec < len) { + len = prec; } - chrs += mp_print_strn(print, str, prec, flags, fill, width); + chrs += mp_print_strn(print, str, len, flags, fill, width); break; } case 'd': { diff --git a/py/objstr.c b/py/objstr.c index 84728e6f2d919..9f8da873d66c0 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -123,10 +123,10 @@ STATIC void str_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t bool is_bytes = true; #endif if (kind == PRINT_RAW || (!MICROPY_PY_BUILTINS_STR_UNICODE && kind == PRINT_STR && !is_bytes)) { - mp_printf(print, "%.*s", str_len, str_data); + print->print_strn(print->data, (const char *)str_data, str_len); } else { if (is_bytes) { - mp_print_str(print, "b"); + print->print_strn(print->data, "b", 1); } mp_str_print_quoted(print, str_data, str_len, is_bytes); } diff --git a/py/objstrunicode.c b/py/objstrunicode.c index b21df22a3bc0a..ed79ad68a93c6 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -92,7 +92,7 @@ STATIC void uni_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t } #endif if (kind == PRINT_STR) { - mp_printf(print, "%.*s", str_len, str_data); + print->print_strn(print->data, (const char *)str_data, str_len); } else { uni_print_quoted(print, str_data, str_len); } diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 257224108a4c5..d97de2c0ef8b3 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -4,7 +4,7 @@ 123 123 1ABCDEF -ab abc +ab abc ' abc' ' True' 'Tru' false true (null) From 248968863595fb285b1209927e27c37b06faf857 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 16 Jul 2020 22:19:19 +0200 Subject: [PATCH 0473/3533] nrf/boards: Update memory.ld to include bootloader offsets. Adding variables that can be set from other linker scripts: - _bootloader_head_size: Bootloader flash offset in front of the application. - _bootloader_tail_size: Bootloader offset from the tail of the flash. In case the bootloader is located at the end. - _bootloader_head_ram_size: Bootloader RAM usage in front of the application. Updated calculations of application flash and RAM. --- ports/nrf/boards/memory.ld | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/ports/nrf/boards/memory.ld b/ports/nrf/boards/memory.ld index f1f9a2a4c48b8..9c4c9c8bd1267 100644 --- a/ports/nrf/boards/memory.ld +++ b/ports/nrf/boards/memory.ld @@ -1,18 +1,20 @@ -/* Flash layout: softdevice | application | filesystem */ -/* RAM layout: softdevice RAM | application RAM */ - -_ram_start = DEFINED(_ram_start) ? _ram_start : 0x20000000; -_flash_start = DEFINED(_flash_start) ? _flash_start : 0; -_sd_size = DEFINED(_sd_size) ? _sd_size : _flash_start; +/* Flash layout: bootloader_head | softdevice | application | filesystem | bootloader_tail */ +/* RAM layout: bootloader RAM | softdevice RAM | application RAM */ +_bootloader_head_size = DEFINED(_bootloader_head_size) ? _bootloader_head_size : 0; +_bootloader_tail_size = DEFINED(_bootloader_tail_size) ? _bootloader_tail_size : 0; +_bootloader_head_ram_size = DEFINED(_bootloader_head_ram_size) ? _bootloader_head_ram_size : 0; +_head_size = DEFINED(_sd_size) ? _sd_size : _bootloader_head_size; +_head_ram = DEFINED(_sd_ram) ? _sd_ram : _bootloader_head_ram_size; +_sd_size = DEFINED(_sd_size) ? _sd_size : 0; _sd_ram = DEFINED(_sd_ram) ? _sd_ram : 0; _fs_size = DEFINED(_fs_size) ? _fs_size : 64K; /* TODO: set to 0 if not using the filesystem */ -_app_size = _flash_size - _sd_size - _fs_size; -_app_start = _sd_size; -_fs_start = _sd_size + _app_size; +_app_size = _flash_size - _head_size - _fs_size - _bootloader_tail_size; +_app_start = _head_size; +_fs_start = _head_size + _app_size; _fs_end = _fs_start + _fs_size; -_app_ram_start = _ram_start + _sd_ram; -_app_ram_size = _ram_size - _sd_ram; +_app_ram_start = 0x20000000 + _head_ram; +_app_ram_size = _ram_size - _head_ram; _heap_start = _ebss; _heap_end = _ram_end - _stack_size; _heap_size = _heap_end - _heap_start; From 718397a37d7ec2071b5f5d3668c31e6b8f69f3b9 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 16 Jul 2020 23:04:02 +0200 Subject: [PATCH 0474/3533] nrf/Makefile: Add bootloader specific section. Add the option for "mpconfigboard.mk" to define whether the board hosts a bootloader or not. The BOOTLOADER make variable must be set to the name of the bootloader. When the BOOTLOADER name is set it is also required to supply the BOOTLOADER_VERSION_MAJOR and the BOOTLOADER_VERSION_MINOR from the "mpconfigboards.mk". These will be used to resolve which bootloader linker script that should be passed to the linker. The BOOTLOADER section also supplies the C-compiler with BOOTLOADER_= as a compiler define. This is for future use in case a bootloader needs to do modification to the startup files or similar (like setting the VTOR specific to a version of a bootloader). --- ports/nrf/Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index e5a1b471a35d7..4ee9384ec28ff 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -26,6 +26,13 @@ else include drivers/bluetooth/bluetooth_common.mk endif +ifneq ($(BOOTLOADER),) + BOOTLOADER_UPPER = $(shell echo $(BOOTLOADER) | tr '[:lower:]' '[:upper:]') + # Use additional bootloader linker script + LD_FILES += boards/$(MCU_SUB_VARIANT)_$(BOOTLOADER)_$(BOOTLOADER_VERSION_MAJOR).$(BOOTLOADER_VERSION_MINOR).x.ld + CFLAGS += -DBOOTLOADER_$(BOOTLOADER_UPPER)_VERSION=$(BOOTLOADER_MAJOR)$(BOOTLOADER_MINOR) +endif + LD_FILES += boards/memory.ld boards/common.ld ifneq ($(LD_FILE),) From 634f6df32485457dd1c236633cffd49177aaf826 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 16 Jul 2020 23:17:28 +0200 Subject: [PATCH 0475/3533] nrf/Makefile: Add support for flashing with nrfutil. An additional Makefile parameter NRFUTIL_PORT can be set in order to define the serial port to used for the DFU (Default: /dev/ttyACM0). The "nrfutil" that is used as flasher towards OpenBootloader is available for installation through Python "pip". In case of SD=s140, SoftDevice ID 0xB6 is passed to nrfutil's package generation which corresponds to SoftDevice s140 v6.1.1. --- ports/nrf/Makefile | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 4ee9384ec28ff..86809508042c1 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -446,6 +446,31 @@ deploy: $(BUILD)/$(OUTPUT_FILENAME).hex sd: $(BUILD)/$(OUTPUT_FILENAME).hex $(Q)$(OPENOCD) -f interface/cmsis-dap.cfg -f $(OPENOCD_TARGET) -c "init" -c "program $(SOFTDEV_HEX) verify reset" -c "exit" +else ifeq ($(FLASHER), nrfutil) + +NRFUTIL_PORT ?= /dev/ttyACM0 +NRFUTIL_SD_REQ ?= 0x00 + +ifeq ($(SD), s140) +NRFUTIL_SD_REQ = 0xB6 +endif + +.PHONY: nrfutil_dfu_sd nrfutil_dfu_deploy +.NOTPARALLEL: nrfutil_dfu_sd nrfutil_dfu_deploy + +# DFU both SD and app in case of target "sd" +sd: nrfutil_dfu_sd nrfutil_dfu_deploy + +nrfutil_dfu_sd: $(BUILD)/$(OUTPUT_FILENAME).hex + $(Q)hexmerge.py -o $(BUILD)/stripped_sd.hex --range=0x1000: $(SOFTDEV_HEX) + $(Q)nrfutil pkg generate --hw-version 52 --sd-req 0x00 --sd-id 0x00 --softdevice $(BUILD)/stripped_sd.hex $(BUILD)/stripped_sd.zip + $(Q)nrfutil dfu usb-serial -pkg $(BUILD)/stripped_sd.zip -p $(NRFUTIL_PORT) -t 0 + +nrfutil_dfu_deploy: $(BUILD)/$(OUTPUT_FILENAME).hex + $(Q)nrfutil pkg generate --hw-version 52 --sd-req $(NRFUTIL_SD_REQ) --application-version 1 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/$(OUTPUT_FILENAME)_dfu.zip + $(Q)nrfutil dfu usb-serial -pkg $(BUILD)/$(OUTPUT_FILENAME)_dfu.zip -p $(NRFUTIL_PORT) -t 0 + +deploy: nrfutil_dfu_deploy endif flash: deploy From 7f405236a3355467a37cb16762939d6362352089 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 16 Jul 2020 23:23:41 +0200 Subject: [PATCH 0476/3533] nrf/boards: Add linker script for nrf52840 Open Bootloader 1.2.0. --- ports/nrf/boards/nrf52840_open_bootloader_1.2.x.ld | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ports/nrf/boards/nrf52840_open_bootloader_1.2.x.ld diff --git a/ports/nrf/boards/nrf52840_open_bootloader_1.2.x.ld b/ports/nrf/boards/nrf52840_open_bootloader_1.2.x.ld new file mode 100644 index 0000000000000..c23a2dbda411f --- /dev/null +++ b/ports/nrf/boards/nrf52840_open_bootloader_1.2.x.ld @@ -0,0 +1,5 @@ +/* GNU linker script for nrf52840 Open Bootloader version 1.2.x */ + +_bootloader_head_size = 0x1000; /* MBR */ +_bootloader_tail_size = 0x20000; /* Bootloader start address 0x000E0000 */ +_bootloader_head_ram_size = 0x8; From d0b8554df477ca1b9daf634d01604e3f760909ca Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 3 Dec 2020 21:22:27 +0100 Subject: [PATCH 0477/3533] nrf: Change selected boards to utilize pre-flashed bootloader. The nrf52840-mdk-usb-dongle and pca10050 comes with a pre-flashed bootloader (OpenBootloader). This commit updates the boards "mpconfigboard.mk" to use DFU as default flashing method and set the corresponding BOOTLOADER settings such that nrf52840_open_bootloader_1.2.x.ld linker script is used. The default DFU flashing method can be disabled by issuing "DFU=0" when invoking make. This will lead to "segger" being used as default flashing tool. When using "DFU=0", the linker scripts will not compensate for any MBR and Bootloader region being present, and might overwrite them if they were present. The commit also removes the custom linker script specific to nrf52840-mdk-usb-dongle as it now points to a generic. Updated nrf52840-mdk-usb-dongle's README.md to be more clear on how to deploy the built firmware. The port README.md has also been updated. In the list of target boards a new column has been added to indicate which bootloader is present on the target board. And for consistency, changed all examples in the README.md to use "deploy" instead of "flash". --- ports/nrf/README.md | 70 +++++++++++++------ .../boards/nrf52840-mdk-usb-dongle/README.md | 9 ++- .../nrf52840-mdk-usb-dongle/mpconfigboard.mk | 14 +++- .../nrf52840_open_bootloader.ld | 2 - ports/nrf/boards/pca10059/mpconfigboard.mk | 12 ++++ 5 files changed, 77 insertions(+), 30 deletions(-) delete mode 100644 ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld diff --git a/ports/nrf/README.md b/ports/nrf/README.md index 5fceb6705f6d2..22bb4af514a39 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -58,12 +58,12 @@ By default, the PCA10040 (nrf52832) is used as compile target. To build and flas make submodules make - make flash + make deploy Alternatively the target board could be defined: make BOARD=pca10040 - make BOARD=pca10040 flash + make BOARD=pca10040 deploy ## Compile without LTO enabled @@ -118,26 +118,27 @@ For example: ## Target Boards and Make Flags -Target Board (BOARD) | Bluetooth Stack (SD) | Bluetooth Support | Flash Util ----------------------|-------------------------|------------------------|------------------------------- -microbit | s110 | Peripheral | [PyOCD](#pyocdopenocd-targets) -pca10000 | s110 | Peripheral | [Segger](#segger-targets) -pca10001 | s110 | Peripheral | [Segger](#segger-targets) -pca10028 | s110 | Peripheral | [Segger](#segger-targets) -pca10031 | s110 | Peripheral | [Segger](#segger-targets) -wt51822_s4at | s110 | Peripheral | Manual, see [datasheet](https://4tronix.co.uk/picobot2/WT51822-S4AT.pdf) for pinout -pca10040 | s132 | Peripheral and Central | [Segger](#segger-targets) -feather52 | s132 | Peripheral and Central | Manual, SWDIO and SWCLK solder points on the bottom side of the board -arduino_primo | s132 | Peripheral and Central | [PyOCD](#pyocdopenocd-targets) -ibk_blyst_nano | s132 | Peripheral and Central | [IDAP](#idap-midap-link-targets) -idk_blyst_nano | s132 | Peripheral and Central | [IDAP](#idap-midap-link-targets) -blueio_tag_evim | s132 | Peripheral and Central | [IDAP](#idap-midap-link-targets) -evk_nina_b1 | s132 | Peripheral and Central | [Segger](#segger-targets) -pca10056 | s140 | Peripheral and Central | [Segger](#segger-targets) -pca10059 | s140 | Peripheral and Central | Manual, SWDIO and SWCLK solder points on the sides. -particle_xenon | s140 | Peripheral and Central | [Black Magic Probe](#black-magic-probe-targets) -pca10090 | None (bsdlib.a) | None (LTE/GNSS) | [Segger](#segger-targets) -actinius_icarus | None (bsdlib.a) | None (LTE/GNSS) | [Segger](#segger-targets) +Target Board (BOARD) | Bluetooth Stack (SD) | Bluetooth Support | Bootloader | Default Flash Util +---------------------|-------------------------|------------------------|----------------|------------------- +microbit | s110 | Peripheral | | [PyOCD](#pyocdopenocd-targets) +pca10000 | s110 | Peripheral | | [Segger](#segger-targets) +pca10001 | s110 | Peripheral | | [Segger](#segger-targets) +pca10028 | s110 | Peripheral | | [Segger](#segger-targets) +pca10031 | s110 | Peripheral | | [Segger](#segger-targets) +wt51822_s4at | s110 | Peripheral | | Manual, see [datasheet](https://4tronix.co.uk/picobot2/WT51822-S4AT.pdf) for pinout +pca10040 | s132 | Peripheral and Central | | [Segger](#segger-targets) +feather52 | s132 | Peripheral and Central | | Manual, SWDIO and SWCLK solder points on the bottom side of the board +arduino_primo | s132 | Peripheral and Central | | [PyOCD](#pyocdopenocd-targets) +ibk_blyst_nano | s132 | Peripheral and Central | | [IDAP](#idap-midap-link-targets) +idk_blyst_nano | s132 | Peripheral and Central | | [IDAP](#idap-midap-link-targets) +blueio_tag_evim | s132 | Peripheral and Central | | [IDAP](#idap-midap-link-targets) +evk_nina_b1 | s132 | Peripheral and Central | | [Segger](#segger-targets) +pca10056 | s140 | Peripheral and Central | | [Segger](#segger-targets) +pca10059 | s140 | Peripheral and Central | OpenBootloader | [nrfutil](#nrfutil-targets) +particle_xenon | s140 | Peripheral and Central | | [Black Magic Probe](#black-magic-probe-targets) +nrf52840-mdk-usb-dongle | s140 | Peripheral and Central | OpenBootloader | [nrfutil](#nrfutil-targets) +pca10090 | None (bsdlib.a) | None (LTE/GNSS) | | [Segger](#segger-targets) +actinius_icarus | None (bsdlib.a) | None (LTE/GNSS) | | [Segger](#segger-targets) ## IDAP-M/IDAP-Link Targets @@ -173,6 +174,31 @@ This requires no further dependencies other than `arm-none-eabi-gdb`. [this guide](https://github.com/blacksphere/blackmagic/wiki/Useful-GDB-commands) for more tips about using the BMP with GDB. +## nRFUtil Targets + +Install the necessary Python packages that will be used for flashing using the bootloader: + + sudo pip install nrfutil + sudo pip install intelhex + +The `intelhex` provides the `hexmerge.py` utility which is used by the Makefile +to trim of the MBR in case SoftDevice flashing is requested. + +`nrfutil` as flashing backend also requires a serial port paramter to be defined +in addition to the `deploy` target of make. For example: + + make BOARD=nrf52840-mdk-usb-dongle NRFUTIL_PORT=/dev/ttyACM0 deploy + +If the target device is connected to `/dev/ttyACM0` serial port, the +`NRFUTIL_PORT` parameter to make can be elided as it is the default serial +port set by the Makefile. + +When enabling Bluetooth LE, as with the other flash utils, the SoftDevice +needs to be flashed in the first firmware update. This can be done by issuing +the `sd` target instead of `deploy`. For example: + + make BOARD=nrf52840-mdk-usb-dongle SD=s140 NRFUTIL_PORT=/dev/ttyACM0 sd + ## Bluetooth LE REPL The port also implements a BLE REPL driver. This feature is disabled by default, as it will deactivate the UART REPL when activated. As some of the nRF devices only have one UART, using the BLE REPL free's the UART instance such that it can be used as a general UART peripheral not bound to REPL. diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md b/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md index c39a800d776cf..f993c7dc53623 100644 --- a/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/README.md @@ -39,11 +39,10 @@ Follow the standard [nRF Port build instructions](../../README.md); but use make BOARD=nrf52840-mdk-usb-dongle The build artifacts will be created in `build-nrf52840-mdk-usb-dongle`. Once -built, the easiest way to deploy to the device is to open `firmware.hex` using +built, the target can be deployed to the device as described in +[nRFUtil targets](../../README.md#nrfutil-targets). + +An alternative way to deploy to the device, is to open `firmware.hex` using *nRF Connect* and select *Write*. Detailed instructions can be found on the [developer wiki](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/programming/). - -**Note** that the regular method of deployment for the MicroPython nRF port -(using `make deploy`) will *not* operate correctly and will overwrite the -bootloader. diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk index f98d5c88a9076..ca437418d5254 100644 --- a/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk +++ b/ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk @@ -2,6 +2,18 @@ MCU_SERIES = m4 MCU_VARIANT = nrf52 MCU_SUB_VARIANT = nrf52840 SOFTDEV_VERSION = 6.1.1 -LD_FILES += boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld boards/nrf52840_1M_256k.ld + +DFU ?= 1 + +ifeq ($(DFU),1) +BOOTLOADER=open_bootloader +BOOTLOADER_VERSION_MAJOR=1 +BOOTLOADER_VERSION_MINOR=2 +FLASHER=nrfutil +else +FLASHER=segger +endif + +LD_FILES += boards/nrf52840_1M_256k.ld NRF_DEFINES += -DNRF52840_XXAA diff --git a/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld b/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld deleted file mode 100644 index d6c6e743a4daf..0000000000000 --- a/ports/nrf/boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld +++ /dev/null @@ -1,2 +0,0 @@ -_ram_start = 0x20000008; -_flash_start = 0x1000; diff --git a/ports/nrf/boards/pca10059/mpconfigboard.mk b/ports/nrf/boards/pca10059/mpconfigboard.mk index ca555d3932aa4..ca437418d5254 100644 --- a/ports/nrf/boards/pca10059/mpconfigboard.mk +++ b/ports/nrf/boards/pca10059/mpconfigboard.mk @@ -2,6 +2,18 @@ MCU_SERIES = m4 MCU_VARIANT = nrf52 MCU_SUB_VARIANT = nrf52840 SOFTDEV_VERSION = 6.1.1 + +DFU ?= 1 + +ifeq ($(DFU),1) +BOOTLOADER=open_bootloader +BOOTLOADER_VERSION_MAJOR=1 +BOOTLOADER_VERSION_MINOR=2 +FLASHER=nrfutil +else +FLASHER=segger +endif + LD_FILES += boards/nrf52840_1M_256k.ld NRF_DEFINES += -DNRF52840_XXAA From cd61fc8e44202feb20a4c52b56f75740aa72d075 Mon Sep 17 00:00:00 2001 From: Reinhard Feger Date: Fri, 24 Jul 2020 22:53:43 +0200 Subject: [PATCH 0478/3533] stm32/boards/stm32h743.ld: Enable D2 RAM and add eth-buffer section. --- ports/stm32/boards/stm32h743.ld | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/stm32/boards/stm32h743.ld b/ports/stm32/boards/stm32h743.ld index 69738ab8b913a..8cf8a4e591a5f 100644 --- a/ports/stm32/boards/stm32h743.ld +++ b/ports/stm32/boards/stm32h743.ld @@ -10,7 +10,8 @@ MEMORY FLASH_FS (r) : ORIGIN = 0x08020000, LENGTH = 128K /* sector 1, 128K */ FLASH_TEXT (rx) : ORIGIN = 0x08040000, LENGTH = 1792K /* sectors 6*128 + 8*128 */ DTCM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K /* Used for storage cache */ - RAM (xrw) : ORIGIN = 0x24000000, LENGTH = 512K /* AXI SRAM */ + RAM (xrw) : ORIGIN = 0x24000000, LENGTH = 512K /* AXI SRAM */ + RAM_D2 (xrw) : ORIGIN = 0x30000000, LENGTH = 288K } /* produce a link error if there is not this amount of RAM for these sections */ @@ -27,3 +28,12 @@ _ram_start = ORIGIN(RAM); _ram_end = ORIGIN(RAM) + LENGTH(RAM); _heap_start = _ebss; /* heap starts just after statically allocated memory */ _heap_end = _sstack; + +/* Define output sections */ +SECTIONS +{ + .eth_buffers (NOLOAD) : { + . = ABSOLUTE(0x30040000); + *eth.o*(.bss.eth_dma) + } >RAM_D2 +} From d986b2012289cecba1f249c4e29446a0bfe680a2 Mon Sep 17 00:00:00 2001 From: Reinhard Feger Date: Fri, 24 Jul 2020 23:08:41 +0200 Subject: [PATCH 0479/3533] stm32/eth: Add support for H7 processors. --- ports/stm32/eth.c | 231 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 226 insertions(+), 5 deletions(-) diff --git a/ports/stm32/eth.c b/ports/stm32/eth.c index 33d20707e21c0..7a08e8c28c34d 100644 --- a/ports/stm32/eth.c +++ b/ports/stm32/eth.c @@ -61,7 +61,19 @@ #define PHY_SCSR_SPEED_100FULL (6 << PHY_SCSR_SPEED_Pos) // ETH DMA RX and TX descriptor definitions - +#if defined(STM32H7) +#define RX_DESCR_3_OWN_Pos (31) +#define RX_DESCR_3_IOC_Pos (30) +#define RX_DESCR_3_BUF1V_Pos (24) +#define RX_DESCR_3_PL_Msk (0x7fff) + +#define TX_DESCR_3_OWN_Pos (31) +#define TX_DESCR_3_LD_Pos (29) +#define TX_DESCR_3_FD_Pos (28) +#define TX_DESCR_3_CIC_Pos (16) +#define TX_DESCR_2_B1L_Pos (0) +#define TX_DESCR_2_B1L_Msk (0x3fff << TX_DESCR_2_B1L_Pos) +#else #define RX_DESCR_0_OWN_Pos (31) #define RX_DESCR_0_FL_Pos (16) #define RX_DESCR_0_FL_Msk (0x3fff << RX_DESCR_0_FL_Pos) @@ -78,6 +90,7 @@ #define TX_DESCR_0_TER_Pos (21) #define TX_DESCR_0_TCH_Pos (20) #define TX_DESCR_1_TBS1_Pos (0) +#endif // Configuration values @@ -121,6 +134,20 @@ STATIC void eth_mac_deinit(eth_t *self); STATIC void eth_process_frame(eth_t *self, size_t len, const uint8_t *buf); STATIC void eth_phy_write(uint32_t reg, uint32_t val) { + #if defined(STM32H7) + while (ETH->MACMDIOAR & ETH_MACMDIOAR_MB) { + } + uint32_t ar = ETH->MACMDIOAR; + ar &= ~ETH_MACMDIOAR_RDA_Msk; + ar |= reg << ETH_MACMDIOAR_RDA_Pos; + ar &= ~ETH_MACMDIOAR_MOC_Msk; + ar |= ETH_MACMDIOAR_MOC_WR; + ar |= ETH_MACMDIOAR_MB; + ETH->MACMDIODR = val; + ETH->MACMDIOAR = ar; + while (ETH->MACMDIOAR & ETH_MACMDIOAR_MB) { + } + #else while (ETH->MACMIIAR & ETH_MACMIIAR_MB) { } ETH->MACMIIDR = val; @@ -129,9 +156,24 @@ STATIC void eth_phy_write(uint32_t reg, uint32_t val) { ETH->MACMIIAR = ar; while (ETH->MACMIIAR & ETH_MACMIIAR_MB) { } + #endif } STATIC uint32_t eth_phy_read(uint32_t reg) { + #if defined(STM32H7) + while (ETH->MACMDIOAR & ETH_MACMDIOAR_MB) { + } + uint32_t ar = ETH->MACMDIOAR; + ar &= ~ETH_MACMDIOAR_RDA_Msk; + ar |= reg << ETH_MACMDIOAR_RDA_Pos; + ar &= ~ETH_MACMDIOAR_MOC_Msk; + ar |= ETH_MACMDIOAR_MOC_RD; + ar |= ETH_MACMDIOAR_MB; + ETH->MACMDIOAR = ar; + while (ETH->MACMDIOAR & ETH_MACMDIOAR_MB) { + } + return ETH->MACMDIODR; + #else while (ETH->MACMIIAR & ETH_MACMIIAR_MB) { } uint32_t ar = ETH->MACMIIAR; @@ -140,6 +182,7 @@ STATIC uint32_t eth_phy_read(uint32_t reg) { while (ETH->MACMIIAR & ETH_MACMIIAR_MB) { } return ETH->MACMIIDR; + #endif } void eth_init(eth_t *self, int mac_idx) { @@ -160,7 +203,7 @@ STATIC int eth_mac_init(eth_t *self) { // Configure GPIO mp_hal_pin_config_alt_static(MICROPY_HW_ETH_MDC, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_MDC); mp_hal_pin_config_alt_static(MICROPY_HW_ETH_MDIO, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_MDIO); - mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_REF_CLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_REF_CLK); + mp_hal_pin_config_alt_static_speed(MICROPY_HW_ETH_RMII_REF_CLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_MEDIUM, STATIC_AF_ETH_RMII_REF_CLK); mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_CRS_DV, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_CRS_DV); mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_RXD0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_RXD0); mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_RXD1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_RXD1); @@ -168,26 +211,53 @@ STATIC int eth_mac_init(eth_t *self) { mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_TXD0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_TXD0); mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_TXD1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_TXD1); + #if defined(STM32H7) + __HAL_RCC_ETH1MAC_CLK_ENABLE(); + __HAL_RCC_ETH1TX_CLK_ENABLE(); + __HAL_RCC_ETH1RX_CLK_ENABLE(); + __HAL_RCC_ETH1MAC_FORCE_RESET(); + #else __HAL_RCC_ETH_CLK_ENABLE(); __HAL_RCC_ETHMAC_FORCE_RESET(); + #endif // Select RMII interface + #if defined(STM32H7) + SYSCFG->PMCR = (SYSCFG->PMCR & ~SYSCFG_PMCR_EPIS_SEL_Msk) | SYSCFG_PMCR_EPIS_SEL_2; + #else __HAL_RCC_SYSCFG_CLK_ENABLE(); SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL; + #endif + + #if defined(STM32H7) + __HAL_RCC_ETH1MAC_RELEASE_RESET(); + __HAL_RCC_ETH1MAC_CLK_SLEEP_ENABLE(); + __HAL_RCC_ETH1TX_CLK_SLEEP_ENABLE(); + __HAL_RCC_ETH1RX_CLK_SLEEP_ENABLE(); + #else __HAL_RCC_ETHMAC_RELEASE_RESET(); __HAL_RCC_ETHMAC_CLK_SLEEP_ENABLE(); __HAL_RCC_ETHMACTX_CLK_SLEEP_ENABLE(); __HAL_RCC_ETHMACRX_CLK_SLEEP_ENABLE(); + #endif // Do a soft reset of the MAC core - ETH->DMABMR = ETH_DMABMR_SR; + #if defined(STM32H7) + #define ETH_SOFT_RESET(eth) do { eth->DMAMR = ETH_DMAMR_SWR; } while (0) + #define ETH_IS_RESET(eth) (eth->DMAMR & ETH_DMAMR_SWR) + #else + #define ETH_SOFT_RESET(eth) do { eth->DMABMR = ETH_DMABMR_SR; } while (0) + #define ETH_IS_RESET(eth) (eth->DMABMR & ETH_DMABMR_SR) + #endif + + ETH_SOFT_RESET(ETH); mp_hal_delay_ms(2); // Wait for soft reset to finish uint32_t t0 = mp_hal_ticks_ms(); - while (ETH->DMABMR & ETH_DMABMR_SR) { + while (ETH_IS_RESET(ETH)) { if (mp_hal_ticks_ms() - t0 > 1000) { return -MP_ETIMEDOUT; } @@ -196,6 +266,21 @@ STATIC int eth_mac_init(eth_t *self) { // Set MII clock range uint32_t hclk = HAL_RCC_GetHCLKFreq(); uint32_t cr_div; + #if defined(STM32H7) + cr_div = ETH->MACMDIOAR & ~ETH_MACMDIOAR_CR; + if (hclk < 35000000) { + cr_div |= ETH_MACMDIOAR_CR_DIV16; + } else if (hclk < 60000000) { + cr_div |= ETH_MACMDIOAR_CR_DIV26; + } else if (hclk < 100000000) { + cr_div |= ETH_MACMDIOAR_CR_DIV42; + } else if (hclk < 150000000) { + cr_div |= ETH_MACMDIOAR_CR_DIV62; + } else { + cr_div |= ETH_MACMDIOAR_CR_DIV102; + } + ETH->MACMDIOAR = cr_div; + #else if (hclk < 35000000) { cr_div = ETH_MACMIIAR_CR_Div16; } else if (hclk < 60000000) { @@ -208,6 +293,12 @@ STATIC int eth_mac_init(eth_t *self) { cr_div = ETH_MACMIIAR_CR_Div102; } ETH->MACMIIAR = cr_div; + #endif + + #if defined(STM32H7) + // don't skip 32bit words since our desriptors are continuous in memory + ETH->DMACCR &= ~(ETH_DMACCR_DSL_Msk); + #endif // Reset the PHY eth_phy_write(PHY_BCR, PHY_BCR_SOFT_RESET); @@ -249,17 +340,36 @@ STATIC int eth_mac_init(eth_t *self) { uint16_t phy_scsr = eth_phy_read(PHY_SCSR); // Burst mode configuration + #if defined(STM32H7) + ETH->DMASBMR = ETH->DMASBMR & ~ETH_DMASBMR_AAL & ~ETH_DMASBMR_FB; + #else ETH->DMABMR = 0; + #endif mp_hal_delay_ms(2); // Select DMA interrupts + #if defined(STM32H7) + ETH->DMACIER = ETH->DMACIER + | ETH_DMACIER_NIE // enable normal interrupts + | ETH_DMACIER_RIE // enable RX interrupt + ; + #else ETH->DMAIER = ETH_DMAIER_NISE // enable normal interrupts | ETH_DMAIER_RIE // enable RX interrupt ; + #endif // Configure RX descriptor lists for (size_t i = 0; i < RX_BUF_NUM; ++i) { + #if defined(STM32H7) + eth_dma.rx_descr[i].rdes3 = + 1 << RX_DESCR_3_OWN_Pos + | (1 << RX_DESCR_3_BUF1V_Pos) // buf1 address valid + | (1 << RX_DESCR_3_IOC_Pos) // Interrupt Enabled on Completion + ; + eth_dma.rx_descr[i].rdes0 = (uint32_t)ð_dma.rx_buf[i * RX_BUF_SIZE]; // buf 1 address + #else eth_dma.rx_descr[i].rdes0 = 1 << RX_DESCR_0_OWN_Pos; eth_dma.rx_descr[i].rdes1 = 1 << RX_DESCR_1_RCH_Pos // chained @@ -267,31 +377,64 @@ STATIC int eth_mac_init(eth_t *self) { ; eth_dma.rx_descr[i].rdes2 = (uint32_t)ð_dma.rx_buf[i * RX_BUF_SIZE]; eth_dma.rx_descr[i].rdes3 = (uint32_t)ð_dma.rx_descr[(i + 1) % RX_BUF_NUM]; + #endif } + + #if defined(STM32H7) + ETH->DMACRDLAR = (uint32_t)ð_dma.rx_descr[0]; + #else ETH->DMARDLAR = (uint32_t)ð_dma.rx_descr[0]; + #endif eth_dma.rx_descr_idx = 0; // Configure TX descriptor lists for (size_t i = 0; i < TX_BUF_NUM; ++i) { + #if defined(STM32H7) + eth_dma.tx_descr[i].tdes0 = 0; + eth_dma.tx_descr[i].tdes1 = 0; + eth_dma.tx_descr[i].tdes2 = TX_BUF_SIZE & TX_DESCR_2_B1L_Msk; + eth_dma.tx_descr[i].tdes3 = 0; + #else eth_dma.tx_descr[i].tdes0 = 1 << TX_DESCR_0_TCH_Pos; eth_dma.tx_descr[i].tdes1 = 0; eth_dma.tx_descr[i].tdes2 = 0; eth_dma.tx_descr[i].tdes3 = (uint32_t)ð_dma.tx_descr[(i + 1) % TX_BUF_NUM]; + #endif } + + #if defined(STM32H7) + // set number of descriptors and buffers + ETH->DMACTDRLR = TX_BUF_NUM - 1; + ETH->DMACRDRLR = RX_BUF_NUM - 1; + + ETH->DMACTDLAR = (uint32_t)ð_dma.tx_descr[0]; + #else ETH->DMATDLAR = (uint32_t)ð_dma.tx_descr[0]; + #endif eth_dma.tx_descr_idx = 0; // Configure DMA + #if defined(STM32H7) + // read from RX FIFO only after a full frame is written + ETH->MTLRQOMR = ETH_MTLRQOMR_RSF; + // transmission starts when a full packet resides in the Tx queue + ETH->MTLTQOMR = ETH_MTLTQOMR_TSF; + #else ETH->DMAOMR = ETH_DMAOMR_RSF // read from RX FIFO after a full frame is written | ETH_DMAOMR_TSF // transmit when a full frame is in TX FIFO (needed by errata) ; + #endif mp_hal_delay_ms(2); // Select MAC filtering options + #if defined(STM32H7) + ETH->MACPFR = ETH_MACPFR_RA; // pass all frames up + #else ETH->MACFFR = ETH_MACFFR_RA // pass all frames up ; + #endif mp_hal_delay_ms(2); // Set MAC address @@ -318,10 +461,15 @@ STATIC int eth_mac_init(eth_t *self) { mp_hal_delay_ms(2); // Start DMA layer + #if defined(STM32H7) + ETH->DMACRCR |= ETH_DMACRCR_SR; // start RX + ETH->DMACTCR |= ETH_DMACTCR_ST; // start TX + #else ETH->DMAOMR |= ETH_DMAOMR_ST // start TX | ETH_DMAOMR_SR // start RX ; + #endif mp_hal_delay_ms(2); // Enable interrupts @@ -334,9 +482,15 @@ STATIC int eth_mac_init(eth_t *self) { STATIC void eth_mac_deinit(eth_t *self) { (void)self; HAL_NVIC_DisableIRQ(ETH_IRQn); + #if defined(STM32H7) + __HAL_RCC_ETH1MAC_FORCE_RESET(); + __HAL_RCC_ETH1MAC_RELEASE_RESET(); + __HAL_RCC_ETH1MAC_CLK_DISABLE(); + #else __HAL_RCC_ETHMAC_FORCE_RESET(); __HAL_RCC_ETHMAC_RELEASE_RESET(); __HAL_RCC_ETH_CLK_DISABLE(); + #endif } STATIC int eth_tx_buf_get(size_t len, uint8_t **buf) { @@ -348,19 +502,32 @@ STATIC int eth_tx_buf_get(size_t len, uint8_t **buf) { eth_dma_tx_descr_t *tx_descr = ð_dma.tx_descr[eth_dma.tx_descr_idx]; uint32_t t0 = mp_hal_ticks_ms(); for (;;) { + #if defined(STM32H7) + if (!(tx_descr->tdes3 & (1 << TX_DESCR_3_OWN_Pos))) { + break; + } + #else if (!(tx_descr->tdes0 & (1 << TX_DESCR_0_OWN_Pos))) { break; } + #endif if (mp_hal_ticks_ms() - t0 > 1000) { return -MP_ETIMEDOUT; } } + #if defined(STM32H7) + // Update TX descriptor with length and buffer pointer + *buf = ð_dma.tx_buf[eth_dma.tx_descr_idx * TX_BUF_SIZE]; + tx_descr->tdes2 = len & TX_DESCR_2_B1L_Msk; + tx_descr->tdes0 = (uint32_t)*buf; + #else // Update TX descriptor with length, buffer pointer and linked list pointer *buf = ð_dma.tx_buf[eth_dma.tx_descr_idx * TX_BUF_SIZE]; tx_descr->tdes1 = len << TX_DESCR_1_TBS1_Pos; tx_descr->tdes2 = (uint32_t)*buf; tx_descr->tdes3 = (uint32_t)ð_dma.tx_descr[(eth_dma.tx_descr_idx + 1) % TX_BUF_NUM]; + #endif return 0; } @@ -371,6 +538,14 @@ STATIC int eth_tx_buf_send(void) { eth_dma.tx_descr_idx = (eth_dma.tx_descr_idx + 1) % TX_BUF_NUM; // Schedule to send next outgoing frame + #if defined(STM32H7) + tx_descr->tdes3 = + 1 << TX_DESCR_3_OWN_Pos // owned by DMA + | 1 << TX_DESCR_3_LD_Pos // last segment + | 1 << TX_DESCR_3_FD_Pos // first segment + | 3 << TX_DESCR_3_CIC_Pos // enable all checksums inserted by hardware + ; + #else tx_descr->tdes0 = 1 << TX_DESCR_0_OWN_Pos // owned by DMA | 1 << TX_DESCR_0_LS_Pos // last segment @@ -378,13 +553,21 @@ STATIC int eth_tx_buf_send(void) { | 3 << TX_DESCR_0_CIC_Pos // enable all checksums inserted by hardware | 1 << TX_DESCR_0_TCH_Pos // TX descriptor is chained ; + #endif // Notify ETH DMA that there is a new TX descriptor for sending __DMB(); + #if defined(STM32H7) + if (ETH->DMACSR & ETH_DMACSR_TBU) { + ETH->DMACSR = ETH_DMACSR_TBU; + } + ETH->DMACTDTPR = (uint32_t)ð_dma.tx_descr[eth_dma.tx_descr_idx]; + #else if (ETH->DMASR & ETH_DMASR_TBUS) { ETH->DMASR = ETH_DMASR_TBUS; ETH->DMATPDR = 0; } + #endif return 0; } @@ -396,6 +579,12 @@ STATIC void eth_dma_rx_free(void) { eth_dma.rx_descr_idx = (eth_dma.rx_descr_idx + 1) % RX_BUF_NUM; // Schedule to get next incoming frame + #if defined(STM32H7) + rx_descr->rdes0 = (uint32_t)buf; + rx_descr->rdes3 = 1 << RX_DESCR_3_OWN_Pos; // owned by DMA + rx_descr->rdes3 |= 1 << RX_DESCR_3_BUF1V_Pos; // buf 1 address valid + rx_descr->rdes3 |= 1 << RX_DESCR_3_IOC_Pos; // Interrupt Enabled on Completion + #else rx_descr->rdes1 = 1 << RX_DESCR_1_RCH_Pos // RX descriptor is chained | RX_BUF_SIZE << RX_DESCR_1_RBS1_Pos // maximum buffer length @@ -403,28 +592,60 @@ STATIC void eth_dma_rx_free(void) { rx_descr->rdes2 = (uint32_t)buf; rx_descr->rdes3 = (uint32_t)ð_dma.rx_descr[eth_dma.rx_descr_idx]; rx_descr->rdes0 = 1 << RX_DESCR_0_OWN_Pos; // owned by DMA + #endif // Notify ETH DMA that there is a new RX descriptor available __DMB(); + #if defined(STM32H7) + ETH->DMACRDTPR = (uint32_t)&rx_descr[eth_dma.rx_descr_idx]; + #else ETH->DMARPDR = 0; + #endif } void ETH_IRQHandler(void) { + #if defined(STM32H7) + uint32_t sr = ETH->DMACSR; + ETH->DMACSR = ETH_DMACSR_NIS; + uint32_t rx_interrupt = sr & ETH_DMACSR_RI; + #else uint32_t sr = ETH->DMASR; ETH->DMASR = ETH_DMASR_NIS; - if (sr & ETH_DMASR_RS) { + uint32_t rx_interrupt = sr & ETH_DMASR_RS; + #endif + if (rx_interrupt) { + #if defined(STM32H7) + ETH->DMACSR = ETH_DMACSR_RI; + #else ETH->DMASR = ETH_DMASR_RS; + #endif for (;;) { + #if defined(STM32H7) + eth_dma_rx_descr_t *rx_descr_l = ð_dma.rx_descr[eth_dma.rx_descr_idx]; + if (rx_descr_l->rdes3 & (1 << RX_DESCR_3_OWN_Pos)) { + // No more RX descriptors ready to read + break; + } + #else eth_dma_rx_descr_t *rx_descr = ð_dma.rx_descr[eth_dma.rx_descr_idx]; if (rx_descr->rdes0 & (1 << RX_DESCR_0_OWN_Pos)) { // No more RX descriptors ready to read break; } + #endif // Get RX buffer containing new frame + #if defined(STM32H7) + size_t len = (rx_descr_l->rdes3 & RX_DESCR_3_PL_Msk); + #else size_t len = (rx_descr->rdes0 & RX_DESCR_0_FL_Msk) >> RX_DESCR_0_FL_Pos; + #endif len -= 4; // discard CRC at end + #if defined(STM32H7) + uint8_t *buf = ð_dma.rx_buf[eth_dma.rx_descr_idx * RX_BUF_SIZE]; + #else uint8_t *buf = (uint8_t *)rx_descr->rdes2; + #endif // Process frame eth_process_frame(ð_instance, len, buf); From 032e0956200d602dcd1dbee8b1894f2e3227e779 Mon Sep 17 00:00:00 2001 From: Reinhard Feger Date: Fri, 24 Jul 2020 23:13:17 +0200 Subject: [PATCH 0480/3533] stm32/boards/NUCLEO_H743ZI: Enable ethernet peripheral. --- ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h b/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h index 4e6b162269a8a..324f2b03ce0d0 100644 --- a/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h @@ -91,8 +91,8 @@ void NUCLEO_H743ZI_board_early_init(void); #define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) #define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET) -// Ethernet via RMII (MDC define disabled for now until eth.c supports H7) -//#define MICROPY_HW_ETH_MDC (pin_C1) +// Ethernet via RMII +#define MICROPY_HW_ETH_MDC (pin_C1) #define MICROPY_HW_ETH_MDIO (pin_A2) #define MICROPY_HW_ETH_RMII_REF_CLK (pin_A1) #define MICROPY_HW_ETH_RMII_CRS_DV (pin_A7) From d9d761b0572b9b99e97f1b5dd39226173f2e239e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 9 Dec 2020 11:01:24 +1100 Subject: [PATCH 0481/3533] lib/littlefs: Update littlefs2 to v2.3.0. At commit 1a59954ec64ca168828a15242cc6de94ac75f9d1 Signed-off-by: Damien George --- lib/littlefs/README.md | 4 +- lib/littlefs/lfs2.c | 1169 ++++++++++++++++++++++++++++------------ lib/littlefs/lfs2.h | 40 +- 3 files changed, 862 insertions(+), 351 deletions(-) diff --git a/lib/littlefs/README.md b/lib/littlefs/README.md index 1f0aacbf9840f..ca21a05f42891 100644 --- a/lib/littlefs/README.md +++ b/lib/littlefs/README.md @@ -2,7 +2,7 @@ littlefs library ================ The upstream source for the files in this directory is -https://github.com/ARMmbed/littlefs +https://github.com/littlefs-project/littlefs To generate the separate files with lfs1 and lfs2 prefixes run the following commands in the top-level directory of the littlefs repository (replace the @@ -13,7 +13,7 @@ version tags with the latest/desired ones, and set `$MPY_DIR`): cp lfs1*.[ch] $MPY_DIR/lib/littlefs git reset --hard HEAD - git checkout v2.1.3 + git checkout v2.3.0 python2 ./scripts/prefix.py lfs2 cp lfs2*.[ch] $MPY_DIR/lib/littlefs git reset --hard HEAD diff --git a/lib/littlefs/lfs2.c b/lib/littlefs/lfs2.c index 6bae806ceb41c..31f3c603e7b87 100644 --- a/lib/littlefs/lfs2.c +++ b/lib/littlefs/lfs2.c @@ -118,24 +118,29 @@ static int lfs2_bd_cmp(lfs2_t *lfs2, lfs2_block_t block, lfs2_off_t off, const void *buffer, lfs2_size_t size) { const uint8_t *data = buffer; + lfs2_size_t diff = 0; - for (lfs2_off_t i = 0; i < size; i++) { - uint8_t dat; - int err = lfs2_bd_read(lfs2, + for (lfs2_off_t i = 0; i < size; i += diff) { + uint8_t dat[8]; + + diff = lfs2_min(size-i, sizeof(dat)); + int res = lfs2_bd_read(lfs2, pcache, rcache, hint-i, - block, off+i, &dat, 1); - if (err) { - return err; + block, off+i, &dat, diff); + if (res) { + return res; } - if (dat != data[i]) { - return (dat < data[i]) ? LFS2_CMP_LT : LFS2_CMP_GT; + res = memcmp(dat, data + i, diff); + if (res) { + return res < 0 ? LFS2_CMP_LT : LFS2_CMP_GT; } } return LFS2_CMP_EQ; } +#ifndef LFS2_READONLY static int lfs2_bd_flush(lfs2_t *lfs2, lfs2_cache_t *pcache, lfs2_cache_t *rcache, bool validate) { if (pcache->block != LFS2_BLOCK_NULL && pcache->block != LFS2_BLOCK_INLINE) { @@ -168,7 +173,9 @@ static int lfs2_bd_flush(lfs2_t *lfs2, return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_bd_sync(lfs2_t *lfs2, lfs2_cache_t *pcache, lfs2_cache_t *rcache, bool validate) { lfs2_cache_drop(lfs2, rcache); @@ -182,7 +189,9 @@ static int lfs2_bd_sync(lfs2_t *lfs2, LFS2_ASSERT(err <= 0); return err; } +#endif +#ifndef LFS2_READONLY static int lfs2_bd_prog(lfs2_t *lfs2, lfs2_cache_t *pcache, lfs2_cache_t *rcache, bool validate, lfs2_block_t block, lfs2_off_t off, @@ -228,13 +237,16 @@ static int lfs2_bd_prog(lfs2_t *lfs2, return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_bd_erase(lfs2_t *lfs2, lfs2_block_t block) { LFS2_ASSERT(block < lfs2->cfg->block_count); int err = lfs2->cfg->erase(lfs2->cfg, block); LFS2_ASSERT(err <= 0); return err; } +#endif /// Small type-level utilities /// @@ -388,10 +400,12 @@ static void lfs2_ctz_fromle32(struct lfs2_ctz *ctz) { ctz->size = lfs2_fromle32(ctz->size); } +#ifndef LFS2_READONLY static void lfs2_ctz_tole32(struct lfs2_ctz *ctz) { ctz->head = lfs2_tole32(ctz->head); ctz->size = lfs2_tole32(ctz->size); } +#endif static inline void lfs2_superblock_fromle32(lfs2_superblock_t *superblock) { superblock->version = lfs2_fromle32(superblock->version); @@ -411,15 +425,46 @@ static inline void lfs2_superblock_tole32(lfs2_superblock_t *superblock) { superblock->attr_max = lfs2_tole32(superblock->attr_max); } +static inline bool lfs2_mlist_isopen(struct lfs2_mlist *head, + struct lfs2_mlist *node) { + for (struct lfs2_mlist **p = &head; *p; p = &(*p)->next) { + if (*p == (struct lfs2_mlist*)node) { + return true; + } + } + + return false; +} + +static inline void lfs2_mlist_remove(lfs2_t *lfs2, struct lfs2_mlist *mlist) { + for (struct lfs2_mlist **p = &lfs2->mlist; *p; p = &(*p)->next) { + if (*p == mlist) { + *p = (*p)->next; + break; + } + } +} + +static inline void lfs2_mlist_append(lfs2_t *lfs2, struct lfs2_mlist *mlist) { + mlist->next = lfs2->mlist; + lfs2->mlist = mlist; +} + /// Internal operations predeclared here /// +#ifndef LFS2_READONLY static int lfs2_dir_commit(lfs2_t *lfs2, lfs2_mdir_t *dir, const struct lfs2_mattr *attrs, int attrcount); static int lfs2_dir_compact(lfs2_t *lfs2, lfs2_mdir_t *dir, const struct lfs2_mattr *attrs, int attrcount, lfs2_mdir_t *source, uint16_t begin, uint16_t end); + +static lfs2_ssize_t lfs2_file_rawwrite(lfs2_t *lfs2, lfs2_file_t *file, + const void *buffer, lfs2_size_t size); +static int lfs2_file_rawsync(lfs2_t *lfs2, lfs2_file_t *file); static int lfs2_file_outline(lfs2_t *lfs2, lfs2_file_t *file); static int lfs2_file_flush(lfs2_t *lfs2, lfs2_file_t *file); + static void lfs2_fs_preporphans(lfs2_t *lfs2, int8_t orphans); static void lfs2_fs_prepmove(lfs2_t *lfs2, uint16_t id, const lfs2_block_t pair[2]); @@ -429,17 +474,32 @@ static lfs2_stag_t lfs2_fs_parent(lfs2_t *lfs2, const lfs2_block_t dir[2], lfs2_mdir_t *parent); static int lfs2_fs_relocate(lfs2_t *lfs2, const lfs2_block_t oldpair[2], lfs2_block_t newpair[2]); -int lfs2_fs_traverseraw(lfs2_t *lfs2, - int (*cb)(void *data, lfs2_block_t block), void *data, - bool includeorphans); static int lfs2_fs_forceconsistency(lfs2_t *lfs2); -static int lfs2_deinit(lfs2_t *lfs2); +#endif + #ifdef LFS2_MIGRATE static int lfs21_traverse(lfs2_t *lfs2, int (*cb)(void*, lfs2_block_t), void *data); #endif +static int lfs2_dir_rawrewind(lfs2_t *lfs2, lfs2_dir_t *dir); + +static lfs2_ssize_t lfs2_file_rawread(lfs2_t *lfs2, lfs2_file_t *file, + void *buffer, lfs2_size_t size); +static int lfs2_file_rawclose(lfs2_t *lfs2, lfs2_file_t *file); +static lfs2_soff_t lfs2_file_rawsize(lfs2_t *lfs2, lfs2_file_t *file); + +static lfs2_ssize_t lfs2_fs_rawsize(lfs2_t *lfs2); +static int lfs2_fs_rawtraverse(lfs2_t *lfs2, + int (*cb)(void *data, lfs2_block_t block), void *data, + bool includeorphans); + +static int lfs2_deinit(lfs2_t *lfs2); +static int lfs2_rawunmount(lfs2_t *lfs2); + + /// Block allocator /// +#ifndef LFS2_READONLY static int lfs2_alloc_lookahead(void *p, lfs2_block_t block) { lfs2_t *lfs2 = (lfs2_t*)p; lfs2_block_t off = ((block - lfs2->free.off) @@ -451,20 +511,24 @@ static int lfs2_alloc_lookahead(void *p, lfs2_block_t block) { return 0; } +#endif +// indicate allocated blocks have been committed into the filesystem, this +// is to prevent blocks from being garbage collected in the middle of a +// commit operation static void lfs2_alloc_ack(lfs2_t *lfs2) { lfs2->free.ack = lfs2->cfg->block_count; } -// Invalidate the lookahead buffer. This is done during mounting and -// failed traversals -static void lfs2_alloc_reset(lfs2_t *lfs2) { - lfs2->free.off = lfs2->seed % lfs2->cfg->block_size; +// drop the lookahead buffer, this is done during mounting and failed +// traversals in order to avoid invalid lookahead state +static void lfs2_alloc_drop(lfs2_t *lfs2) { lfs2->free.size = 0; lfs2->free.i = 0; lfs2_alloc_ack(lfs2); } +#ifndef LFS2_READONLY static int lfs2_alloc(lfs2_t *lfs2, lfs2_block_t *block) { while (true) { while (lfs2->free.i != lfs2->free.size) { @@ -503,13 +567,14 @@ static int lfs2_alloc(lfs2_t *lfs2, lfs2_block_t *block) { // find mask of free blocks from tree memset(lfs2->free.buffer, 0, lfs2->cfg->lookahead_size); - int err = lfs2_fs_traverseraw(lfs2, lfs2_alloc_lookahead, lfs2, true); + int err = lfs2_fs_rawtraverse(lfs2, lfs2_alloc_lookahead, lfs2, true); if (err) { - lfs2_alloc_reset(lfs2); + lfs2_alloc_drop(lfs2); return err; } } } +#endif /// Metadata pair and directory operations /// static lfs2_stag_t lfs2_dir_getslice(lfs2_t *lfs2, const lfs2_mdir_t *dir, @@ -642,6 +707,7 @@ static int lfs2_dir_getread(lfs2_t *lfs2, const lfs2_mdir_t *dir, return 0; } +#ifndef LFS2_READONLY static int lfs2_dir_traverse_filter(void *p, lfs2_tag_t tag, const void *buffer) { lfs2_tag_t *filtertag = p; @@ -669,7 +735,9 @@ static int lfs2_dir_traverse_filter(void *p, return false; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_traverse(lfs2_t *lfs2, const lfs2_mdir_t *dir, lfs2_off_t off, lfs2_tag_t ptag, const struct lfs2_mattr *attrs, int attrcount, @@ -763,6 +831,7 @@ static int lfs2_dir_traverse(lfs2_t *lfs2, } } } +#endif static lfs2_stag_t lfs2_dir_fetchmatch(lfs2_t *lfs2, lfs2_mdir_t *dir, const lfs2_block_t pair[2], @@ -870,8 +939,10 @@ static lfs2_stag_t lfs2_dir_fetchmatch(lfs2_t *lfs2, ptag ^= (lfs2_tag_t)(lfs2_tag_chunk(tag) & 1U) << 31; // toss our crc into the filesystem seed for - // pseudorandom numbers - lfs2->seed ^= crc; + // pseudorandom numbers, note we use another crc here + // as a collection function because it is sufficiently + // random and convenient + lfs2->seed = lfs2_crc(lfs2->seed, &crc, sizeof(crc)); // update with what's found so far besttag = tempbesttag; @@ -1200,6 +1271,7 @@ struct lfs2_commit { lfs2_off_t end; }; +#ifndef LFS2_READONLY static int lfs2_dir_commitprog(lfs2_t *lfs2, struct lfs2_commit *commit, const void *buffer, lfs2_size_t size) { int err = lfs2_bd_prog(lfs2, @@ -1214,7 +1286,9 @@ static int lfs2_dir_commitprog(lfs2_t *lfs2, struct lfs2_commit *commit, commit->off += size; return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_commitattr(lfs2_t *lfs2, struct lfs2_commit *commit, lfs2_tag_t tag, const void *buffer) { // check if we fit @@ -1259,14 +1333,17 @@ static int lfs2_dir_commitattr(lfs2_t *lfs2, struct lfs2_commit *commit, commit->ptag = tag & 0x7fffffff; return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_commitcrc(lfs2_t *lfs2, struct lfs2_commit *commit) { - const lfs2_off_t off1 = commit->off; - const uint32_t crc1 = commit->crc; // align to program units - const lfs2_off_t end = lfs2_alignup(off1 + 2*sizeof(uint32_t), + const lfs2_off_t end = lfs2_alignup(commit->off + 2*sizeof(uint32_t), lfs2->cfg->prog_size); + lfs2_off_t off1 = 0; + uint32_t crc1 = 0; + // create crc tags to fill up remainder of commit, note that // padding is not crced, which lets fetches skip padding but // makes committing a bit more complicated @@ -1302,6 +1379,12 @@ static int lfs2_dir_commitcrc(lfs2_t *lfs2, struct lfs2_commit *commit) { return err; } + // keep track of non-padding checksum to verify + if (off1 == 0) { + off1 = commit->off + sizeof(uint32_t); + crc1 = commit->crc; + } + commit->off += sizeof(tag)+lfs2_tag_size(tag); commit->ptag = tag ^ ((lfs2_tag_t)reset << 31); commit->crc = 0xffffffff; // reset crc for next "commit" @@ -1315,7 +1398,7 @@ static int lfs2_dir_commitcrc(lfs2_t *lfs2, struct lfs2_commit *commit) { // successful commit, check checksums to make sure lfs2_off_t off = commit->begin; - lfs2_off_t noff = off1 + sizeof(uint32_t); + lfs2_off_t noff = off1; while (off < end) { uint32_t crc = 0xffffffff; for (lfs2_off_t i = off; i < noff+sizeof(uint32_t); i++) { @@ -1352,7 +1435,9 @@ static int lfs2_dir_commitcrc(lfs2_t *lfs2, struct lfs2_commit *commit) { return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_alloc(lfs2_t *lfs2, lfs2_mdir_t *dir) { // allocate pair of dir blocks (backwards, so we write block 1 first) for (int i = 0; i < 2; i++) { @@ -1375,8 +1460,12 @@ static int lfs2_dir_alloc(lfs2_t *lfs2, lfs2_mdir_t *dir) { return err; } - // make sure we don't immediately evict - dir->rev += dir->rev & 1; + // to make sure we don't immediately evict, align the new revision count + // to our block_cycles modulus, see lfs2_dir_compact for why our modulus + // is tweaked this way + if (lfs2->cfg->block_cycles > 0) { + dir->rev = lfs2_alignup(dir->rev, ((lfs2->cfg->block_cycles+1)|1)); + } // set defaults dir->off = sizeof(dir->rev); @@ -1390,7 +1479,9 @@ static int lfs2_dir_alloc(lfs2_t *lfs2, lfs2_mdir_t *dir) { // don't write out yet, let caller take care of that return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_drop(lfs2_t *lfs2, lfs2_mdir_t *dir, lfs2_mdir_t *tail) { // steal state int err = lfs2_dir_getgstate(lfs2, tail, &lfs2->gdelta); @@ -1409,7 +1500,9 @@ static int lfs2_dir_drop(lfs2_t *lfs2, lfs2_mdir_t *dir, lfs2_mdir_t *tail) { return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_split(lfs2_t *lfs2, lfs2_mdir_t *dir, const struct lfs2_mattr *attrs, int attrcount, lfs2_mdir_t *source, uint16_t split, uint16_t end) { @@ -1442,7 +1535,9 @@ static int lfs2_dir_split(lfs2_t *lfs2, return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_commit_size(void *p, lfs2_tag_t tag, const void *buffer) { lfs2_size_t *size = p; (void)buffer; @@ -1450,17 +1545,23 @@ static int lfs2_dir_commit_size(void *p, lfs2_tag_t tag, const void *buffer) { *size += lfs2_tag_dsize(tag); return 0; } +#endif +#ifndef LFS2_READONLY struct lfs2_dir_commit_commit { lfs2_t *lfs2; struct lfs2_commit *commit; }; +#endif +#ifndef LFS2_READONLY static int lfs2_dir_commit_commit(void *p, lfs2_tag_t tag, const void *buffer) { struct lfs2_dir_commit_commit *commit = p; return lfs2_dir_commitattr(commit->lfs2, commit->commit, tag, buffer); } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_compact(lfs2_t *lfs2, lfs2_mdir_t *dir, const struct lfs2_mattr *attrs, int attrcount, lfs2_mdir_t *source, uint16_t begin, uint16_t end) { @@ -1525,7 +1626,7 @@ static int lfs2_dir_compact(lfs2_t *lfs2, if (lfs2_pair_cmp(dir->pair, (const lfs2_block_t[2]){0, 1}) == 0) { // oh no! we're writing too much to the superblock, // should we expand? - lfs2_ssize_t res = lfs2_fs_size(lfs2); + lfs2_ssize_t res = lfs2_fs_rawsize(lfs2); if (res < 0) { return res; } @@ -1715,7 +1816,9 @@ static int lfs2_dir_compact(lfs2_t *lfs2, return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_dir_commit(lfs2_t *lfs2, lfs2_mdir_t *dir, const struct lfs2_mattr *attrs, int attrcount) { // check for any inline files that aren't RAM backed and @@ -1903,15 +2006,15 @@ static int lfs2_dir_commit(lfs2_t *lfs2, lfs2_mdir_t *dir, return 0; } +#endif /// Top level directory operations /// -int lfs2_mkdir(lfs2_t *lfs2, const char *path) { - LFS2_TRACE("lfs2_mkdir(%p, \"%s\")", (void*)lfs2, path); +#ifndef LFS2_READONLY +static int lfs2_rawmkdir(lfs2_t *lfs2, const char *path) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs2_fs_forceconsistency(lfs2); if (err) { - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } @@ -1920,14 +2023,12 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { uint16_t id; err = lfs2_dir_find(lfs2, &cwd.m, &path, &id); if (!(err == LFS2_ERR_NOENT && id != 0x3ff)) { - LFS2_TRACE("lfs2_mkdir -> %d", (err < 0) ? err : LFS2_ERR_EXIST); return (err < 0) ? err : LFS2_ERR_EXIST; } // check that name fits lfs2_size_t nlen = strlen(path); if (nlen > lfs2->name_max) { - LFS2_TRACE("lfs2_mkdir -> %d", LFS2_ERR_NAMETOOLONG); return LFS2_ERR_NAMETOOLONG; } @@ -1936,7 +2037,6 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { lfs2_mdir_t dir; err = lfs2_dir_alloc(lfs2, &dir); if (err) { - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } @@ -1945,7 +2045,6 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { while (pred.split) { err = lfs2_dir_fetch(lfs2, &pred, pred.tail); if (err) { - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } } @@ -1956,7 +2055,6 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { {LFS2_MKTAG(LFS2_TYPE_SOFTTAIL, 0x3ff, 8), pred.tail})); lfs2_pair_fromle32(pred.tail); if (err) { - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } @@ -1979,7 +2077,6 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { lfs2_pair_fromle32(dir.pair); if (err) { lfs2->mlist = cwd.next; - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } @@ -1997,24 +2094,20 @@ int lfs2_mkdir(lfs2_t *lfs2, const char *path) { LFS2_TYPE_SOFTTAIL, 0x3ff, 8), dir.pair})); lfs2_pair_fromle32(dir.pair); if (err) { - LFS2_TRACE("lfs2_mkdir -> %d", err); return err; } - LFS2_TRACE("lfs2_mkdir -> %d", 0); return 0; } +#endif -int lfs2_dir_open(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { - LFS2_TRACE("lfs2_dir_open(%p, %p, \"%s\")", (void*)lfs2, (void*)dir, path); +static int lfs2_dir_rawopen(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { lfs2_stag_t tag = lfs2_dir_find(lfs2, &dir->m, &path, NULL); if (tag < 0) { - LFS2_TRACE("lfs2_dir_open -> %"PRId32, tag); return tag; } if (lfs2_tag_type3(tag) != LFS2_TYPE_DIR) { - LFS2_TRACE("lfs2_dir_open -> %d", LFS2_ERR_NOTDIR); return LFS2_ERR_NOTDIR; } @@ -2028,7 +2121,6 @@ int lfs2_dir_open(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { lfs2_stag_t res = lfs2_dir_get(lfs2, &dir->m, LFS2_MKTAG(0x700, 0x3ff, 0), LFS2_MKTAG(LFS2_TYPE_STRUCT, lfs2_tag_id(tag), 8), pair); if (res < 0) { - LFS2_TRACE("lfs2_dir_open -> %"PRId32, res); return res; } lfs2_pair_fromle32(pair); @@ -2037,7 +2129,6 @@ int lfs2_dir_open(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { // fetch first pair int err = lfs2_dir_fetch(lfs2, &dir->m, pair); if (err) { - LFS2_TRACE("lfs2_dir_open -> %d", err); return err; } @@ -2049,30 +2140,19 @@ int lfs2_dir_open(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { // add to list of mdirs dir->type = LFS2_TYPE_DIR; - dir->next = (lfs2_dir_t*)lfs2->mlist; - lfs2->mlist = (struct lfs2_mlist*)dir; + lfs2_mlist_append(lfs2, (struct lfs2_mlist *)dir); - LFS2_TRACE("lfs2_dir_open -> %d", 0); return 0; } -int lfs2_dir_close(lfs2_t *lfs2, lfs2_dir_t *dir) { - LFS2_TRACE("lfs2_dir_close(%p, %p)", (void*)lfs2, (void*)dir); +static int lfs2_dir_rawclose(lfs2_t *lfs2, lfs2_dir_t *dir) { // remove from list of mdirs - for (struct lfs2_mlist **p = &lfs2->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs2_mlist*)dir) { - *p = (*p)->next; - break; - } - } + lfs2_mlist_remove(lfs2, (struct lfs2_mlist *)dir); - LFS2_TRACE("lfs2_dir_close -> %d", 0); return 0; } -int lfs2_dir_read(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { - LFS2_TRACE("lfs2_dir_read(%p, %p, %p)", - (void*)lfs2, (void*)dir, (void*)info); +static int lfs2_dir_rawread(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { memset(info, 0, sizeof(*info)); // special offset for '.' and '..' @@ -2080,26 +2160,22 @@ int lfs2_dir_read(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { info->type = LFS2_TYPE_DIR; strcpy(info->name, "."); dir->pos += 1; - LFS2_TRACE("lfs2_dir_read -> %d", true); return true; } else if (dir->pos == 1) { info->type = LFS2_TYPE_DIR; strcpy(info->name, ".."); dir->pos += 1; - LFS2_TRACE("lfs2_dir_read -> %d", true); return true; } while (true) { if (dir->id == dir->m.count) { if (!dir->m.split) { - LFS2_TRACE("lfs2_dir_read -> %d", false); return false; } int err = lfs2_dir_fetch(lfs2, &dir->m, dir->m.tail); if (err) { - LFS2_TRACE("lfs2_dir_read -> %d", err); return err; } @@ -2108,7 +2184,6 @@ int lfs2_dir_read(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { int err = lfs2_dir_getinfo(lfs2, &dir->m, dir->id, info); if (err && err != LFS2_ERR_NOENT) { - LFS2_TRACE("lfs2_dir_read -> %d", err); return err; } @@ -2119,17 +2194,13 @@ int lfs2_dir_read(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { } dir->pos += 1; - LFS2_TRACE("lfs2_dir_read -> %d", true); return true; } -int lfs2_dir_seek(lfs2_t *lfs2, lfs2_dir_t *dir, lfs2_off_t off) { - LFS2_TRACE("lfs2_dir_seek(%p, %p, %"PRIu32")", - (void*)lfs2, (void*)dir, off); +static int lfs2_dir_rawseek(lfs2_t *lfs2, lfs2_dir_t *dir, lfs2_off_t off) { // simply walk from head dir - int err = lfs2_dir_rewind(lfs2, dir); + int err = lfs2_dir_rawrewind(lfs2, dir); if (err) { - LFS2_TRACE("lfs2_dir_seek -> %d", err); return err; } @@ -2148,13 +2219,11 @@ int lfs2_dir_seek(lfs2_t *lfs2, lfs2_dir_t *dir, lfs2_off_t off) { if (dir->id == dir->m.count) { if (!dir->m.split) { - LFS2_TRACE("lfs2_dir_seek -> %d", LFS2_ERR_INVAL); return LFS2_ERR_INVAL; } err = lfs2_dir_fetch(lfs2, &dir->m, dir->m.tail); if (err) { - LFS2_TRACE("lfs2_dir_seek -> %d", err); return err; } @@ -2162,29 +2231,23 @@ int lfs2_dir_seek(lfs2_t *lfs2, lfs2_dir_t *dir, lfs2_off_t off) { } } - LFS2_TRACE("lfs2_dir_seek -> %d", 0); return 0; } -lfs2_soff_t lfs2_dir_tell(lfs2_t *lfs2, lfs2_dir_t *dir) { - LFS2_TRACE("lfs2_dir_tell(%p, %p)", (void*)lfs2, (void*)dir); +static lfs2_soff_t lfs2_dir_rawtell(lfs2_t *lfs2, lfs2_dir_t *dir) { (void)lfs2; - LFS2_TRACE("lfs2_dir_tell -> %"PRId32, dir->pos); return dir->pos; } -int lfs2_dir_rewind(lfs2_t *lfs2, lfs2_dir_t *dir) { - LFS2_TRACE("lfs2_dir_rewind(%p, %p)", (void*)lfs2, (void*)dir); +static int lfs2_dir_rawrewind(lfs2_t *lfs2, lfs2_dir_t *dir) { // reload the head dir int err = lfs2_dir_fetch(lfs2, &dir->m, dir->head); if (err) { - LFS2_TRACE("lfs2_dir_rewind -> %d", err); return err; } dir->id = 0; dir->pos = 0; - LFS2_TRACE("lfs2_dir_rewind -> %d", 0); return 0; } @@ -2237,6 +2300,7 @@ static int lfs2_ctz_find(lfs2_t *lfs2, return 0; } +#ifndef LFS2_READONLY static int lfs2_ctz_extend(lfs2_t *lfs2, lfs2_cache_t *pcache, lfs2_cache_t *rcache, lfs2_block_t head, lfs2_size_t size, @@ -2334,6 +2398,7 @@ static int lfs2_ctz_extend(lfs2_t *lfs2, lfs2_cache_drop(lfs2, pcache); } } +#endif static int lfs2_ctz_traverse(lfs2_t *lfs2, const lfs2_cache_t *pcache, lfs2_cache_t *rcache, @@ -2380,27 +2445,25 @@ static int lfs2_ctz_traverse(lfs2_t *lfs2, /// Top level file operations /// -int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, +static int lfs2_file_rawopencfg(lfs2_t *lfs2, lfs2_file_t *file, const char *path, int flags, const struct lfs2_file_config *cfg) { - LFS2_TRACE("lfs2_file_opencfg(%p, %p, \"%s\", %x, %p {" - ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", - (void*)lfs2, (void*)file, path, flags, - (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); - +#ifndef LFS2_READONLY // deorphan if we haven't yet, needed at most once after poweron - if ((flags & 3) != LFS2_O_RDONLY) { + if ((flags & LFS2_O_WRONLY) == LFS2_O_WRONLY) { int err = lfs2_fs_forceconsistency(lfs2); if (err) { - LFS2_TRACE("lfs2_file_opencfg -> %d", err); return err; } } +#else + LFS2_ASSERT((flags & LFS2_O_RDONLY) == LFS2_O_RDONLY); +#endif // setup simple file details int err; file->cfg = cfg; - file->flags = flags | LFS2_F_OPENED; + file->flags = flags; file->pos = 0; file->off = 0; file->cache.buffer = NULL; @@ -2414,9 +2477,13 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, // get id, add to list of mdirs to catch update changes file->type = LFS2_TYPE_REG; - file->next = (lfs2_file_t*)lfs2->mlist; - lfs2->mlist = (struct lfs2_mlist*)file; + lfs2_mlist_append(lfs2, (struct lfs2_mlist *)file); +#ifdef LFS2_READONLY + if (tag == LFS2_ERR_NOENT) { + err = LFS2_ERR_NOENT; + goto cleanup; +#else if (tag == LFS2_ERR_NOENT) { if (!(flags & LFS2_O_CREAT)) { err = LFS2_ERR_NOENT; @@ -2432,9 +2499,9 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, // get next slot and create entry to remember name err = lfs2_dir_commit(lfs2, &file->m, LFS2_MKATTRS( - {LFS2_MKTAG(LFS2_TYPE_CREATE, file->id, 0)}, + {LFS2_MKTAG(LFS2_TYPE_CREATE, file->id, 0), NULL}, {LFS2_MKTAG(LFS2_TYPE_REG, file->id, nlen), path}, - {LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, file->id, 0)})); + {LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, file->id, 0), NULL})); if (err) { err = LFS2_ERR_NAMETOOLONG; goto cleanup; @@ -2444,13 +2511,16 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, } else if (flags & LFS2_O_EXCL) { err = LFS2_ERR_EXIST; goto cleanup; +#endif } else if (lfs2_tag_type3(tag) != LFS2_TYPE_REG) { err = LFS2_ERR_ISDIR; goto cleanup; +#ifndef LFS2_READONLY } else if (flags & LFS2_O_TRUNC) { // truncate if requested tag = LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, file->id, 0); file->flags |= LFS2_F_DIRTY; +#endif } else { // try to load what's on disk, if it's inlined we'll fix it later tag = lfs2_dir_get(lfs2, &file->m, LFS2_MKTAG(0x700, 0x3ff, 0), @@ -2464,7 +2534,8 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, // fetch attrs for (unsigned i = 0; i < file->cfg->attr_count; i++) { - if ((file->flags & 3) != LFS2_O_WRONLY) { + // if opened for read / read-write operations + if ((file->flags & LFS2_O_RDONLY) == LFS2_O_RDONLY) { lfs2_stag_t res = lfs2_dir_get(lfs2, &file->m, LFS2_MKTAG(0x7ff, 0x3ff, 0), LFS2_MKTAG(LFS2_TYPE_USERATTR + file->cfg->attrs[i].type, @@ -2476,7 +2547,9 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, } } - if ((file->flags & 3) != LFS2_O_RDONLY) { +#ifndef LFS2_READONLY + // if opened for write / read-write operations + if ((file->flags & LFS2_O_WRONLY) == LFS2_O_WRONLY) { if (file->cfg->attrs[i].size > lfs2->attr_max) { err = LFS2_ERR_NOSPC; goto cleanup; @@ -2484,6 +2557,7 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, file->flags |= LFS2_F_DIRTY; } +#endif } // allocate buffer if needed @@ -2523,54 +2597,45 @@ int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, } } - LFS2_TRACE("lfs2_file_opencfg -> %d", 0); return 0; cleanup: // clean up lingering resources +#ifndef LFS2_READONLY file->flags |= LFS2_F_ERRED; - lfs2_file_close(lfs2, file); - LFS2_TRACE("lfs2_file_opencfg -> %d", err); +#endif + lfs2_file_rawclose(lfs2, file); return err; } -int lfs2_file_open(lfs2_t *lfs2, lfs2_file_t *file, +static int lfs2_file_rawopen(lfs2_t *lfs2, lfs2_file_t *file, const char *path, int flags) { - LFS2_TRACE("lfs2_file_open(%p, %p, \"%s\", %x)", - (void*)lfs2, (void*)file, path, flags); static const struct lfs2_file_config defaults = {0}; - int err = lfs2_file_opencfg(lfs2, file, path, flags, &defaults); - LFS2_TRACE("lfs2_file_open -> %d", err); + int err = lfs2_file_rawopencfg(lfs2, file, path, flags, &defaults); return err; } -int lfs2_file_close(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_TRACE("lfs2_file_close(%p, %p)", (void*)lfs2, (void*)file); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - - int err = lfs2_file_sync(lfs2, file); +static int lfs2_file_rawclose(lfs2_t *lfs2, lfs2_file_t *file) { +#ifndef LFS2_READONLY + int err = lfs2_file_rawsync(lfs2, file); +#else + int err = 0; +#endif // remove from list of mdirs - for (struct lfs2_mlist **p = &lfs2->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs2_mlist*)file) { - *p = (*p)->next; - break; - } - } + lfs2_mlist_remove(lfs2, (struct lfs2_mlist*)file); // clean up memory if (!file->cfg->buffer) { lfs2_free(file->cache.buffer); } - file->flags &= ~LFS2_F_OPENED; - LFS2_TRACE("lfs2_file_close -> %d", err); return err; } -static int lfs2_file_relocate(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_ASSERT(file->flags & LFS2_F_OPENED); +#ifndef LFS2_READONLY +static int lfs2_file_relocate(lfs2_t *lfs2, lfs2_file_t *file) { while (true) { // just relocate what exists into new block lfs2_block_t nblock; @@ -2638,7 +2703,9 @@ static int lfs2_file_relocate(lfs2_t *lfs2, lfs2_file_t *file) { lfs2_cache_drop(lfs2, &lfs2->pcache); } } +#endif +#ifndef LFS2_READONLY static int lfs2_file_outline(lfs2_t *lfs2, lfs2_file_t *file) { file->off = file->pos; lfs2_alloc_ack(lfs2); @@ -2650,10 +2717,10 @@ static int lfs2_file_outline(lfs2_t *lfs2, lfs2_file_t *file) { file->flags &= ~LFS2_F_INLINE; return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_file_flush(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - if (file->flags & LFS2_F_READING) { if (!(file->flags & LFS2_F_INLINE)) { lfs2_cache_drop(lfs2, &file->cache); @@ -2669,7 +2736,7 @@ static int lfs2_file_flush(lfs2_t *lfs2, lfs2_file_t *file) { lfs2_file_t orig = { .ctz.head = file->ctz.head, .ctz.size = file->ctz.size, - .flags = LFS2_O_RDONLY | LFS2_F_OPENED, + .flags = LFS2_O_RDONLY, .pos = file->pos, .cache = lfs2->rcache, }; @@ -2679,12 +2746,12 @@ static int lfs2_file_flush(lfs2_t *lfs2, lfs2_file_t *file) { // copy over a byte at a time, leave it up to caching // to make this efficient uint8_t data; - lfs2_ssize_t res = lfs2_file_read(lfs2, &orig, &data, 1); + lfs2_ssize_t res = lfs2_file_rawread(lfs2, &orig, &data, 1); if (res < 0) { return res; } - res = lfs2_file_write(lfs2, file, &data, 1); + res = lfs2_file_rawwrite(lfs2, file, &data, 1); if (res < 0) { return res; } @@ -2730,24 +2797,22 @@ static int lfs2_file_flush(lfs2_t *lfs2, lfs2_file_t *file) { return 0; } +#endif -int lfs2_file_sync(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_TRACE("lfs2_file_sync(%p, %p)", (void*)lfs2, (void*)file); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - +#ifndef LFS2_READONLY +static int lfs2_file_rawsync(lfs2_t *lfs2, lfs2_file_t *file) { if (file->flags & LFS2_F_ERRED) { // it's not safe to do anything if our file errored - LFS2_TRACE("lfs2_file_sync -> %d", 0); return 0; } int err = lfs2_file_flush(lfs2, file); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_sync -> %d", err); return err; } + if ((file->flags & LFS2_F_DIRTY) && !lfs2_pair_isnull(file->m.pair)) { // update dir entry @@ -2777,39 +2842,35 @@ int lfs2_file_sync(lfs2_t *lfs2, lfs2_file_t *file) { file->cfg->attr_count), file->cfg->attrs})); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_sync -> %d", err); return err; } file->flags &= ~LFS2_F_DIRTY; } - LFS2_TRACE("lfs2_file_sync -> %d", 0); return 0; } +#endif -lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, +static lfs2_ssize_t lfs2_file_rawread(lfs2_t *lfs2, lfs2_file_t *file, void *buffer, lfs2_size_t size) { - LFS2_TRACE("lfs2_file_read(%p, %p, %p, %"PRIu32")", - (void*)lfs2, (void*)file, buffer, size); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - LFS2_ASSERT((file->flags & 3) != LFS2_O_WRONLY); + LFS2_ASSERT((file->flags & LFS2_O_RDONLY) == LFS2_O_RDONLY); uint8_t *data = buffer; lfs2_size_t nsize = size; +#ifndef LFS2_READONLY if (file->flags & LFS2_F_WRITING) { // flush out any writes int err = lfs2_file_flush(lfs2, file); if (err) { - LFS2_TRACE("lfs2_file_read -> %d", err); return err; } } +#endif if (file->pos >= file->ctz.size) { // eof if past end - LFS2_TRACE("lfs2_file_read -> %d", 0); return 0; } @@ -2825,7 +2886,6 @@ lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, file->ctz.head, file->ctz.size, file->pos, &file->block, &file->off); if (err) { - LFS2_TRACE("lfs2_file_read -> %d", err); return err; } } else { @@ -2845,7 +2905,6 @@ lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, file->id, 0), file->off, data, diff); if (err) { - LFS2_TRACE("lfs2_file_read -> %d", err); return err; } } else { @@ -2853,7 +2912,6 @@ lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, NULL, &file->cache, lfs2->cfg->block_size, file->block, file->off, data, diff); if (err) { - LFS2_TRACE("lfs2_file_read -> %d", err); return err; } } @@ -2864,16 +2922,13 @@ lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, nsize -= diff; } - LFS2_TRACE("lfs2_file_read -> %"PRId32, size); return size; } -lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, +#ifndef LFS2_READONLY +static lfs2_ssize_t lfs2_file_rawwrite(lfs2_t *lfs2, lfs2_file_t *file, const void *buffer, lfs2_size_t size) { - LFS2_TRACE("lfs2_file_write(%p, %p, %p, %"PRIu32")", - (void*)lfs2, (void*)file, buffer, size); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - LFS2_ASSERT((file->flags & 3) != LFS2_O_RDONLY); + LFS2_ASSERT((file->flags & LFS2_O_WRONLY) == LFS2_O_WRONLY); const uint8_t *data = buffer; lfs2_size_t nsize = size; @@ -2882,7 +2937,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, // drop any reads int err = lfs2_file_flush(lfs2, file); if (err) { - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } } @@ -2893,7 +2947,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, if (file->pos + size > lfs2->file_max) { // Larger than file limit? - LFS2_TRACE("lfs2_file_write -> %d", LFS2_ERR_FBIG); return LFS2_ERR_FBIG; } @@ -2903,9 +2956,8 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, file->pos = file->ctz.size; while (file->pos < pos) { - lfs2_ssize_t res = lfs2_file_write(lfs2, file, &(uint8_t){0}, 1); + lfs2_ssize_t res = lfs2_file_rawwrite(lfs2, file, &(uint8_t){0}, 1); if (res < 0) { - LFS2_TRACE("lfs2_file_write -> %"PRId32, res); return res; } } @@ -2919,7 +2971,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, int err = lfs2_file_outline(lfs2, file); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } } @@ -2936,7 +2987,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, file->pos-1, &file->block, &file->off); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } @@ -2951,7 +3001,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, &file->block, &file->off); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } } else { @@ -2972,7 +3021,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, goto relocate; } file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } @@ -2981,7 +3029,6 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, err = lfs2_file_relocate(lfs2, file); if (err) { file->flags |= LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %d", err); return err; } } @@ -2995,22 +3042,19 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, } file->flags &= ~LFS2_F_ERRED; - LFS2_TRACE("lfs2_file_write -> %"PRId32, size); return size; } +#endif -lfs2_soff_t lfs2_file_seek(lfs2_t *lfs2, lfs2_file_t *file, +static lfs2_soff_t lfs2_file_rawseek(lfs2_t *lfs2, lfs2_file_t *file, lfs2_soff_t off, int whence) { - LFS2_TRACE("lfs2_file_seek(%p, %p, %"PRId32", %d)", - (void*)lfs2, (void*)file, off, whence); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - +#ifndef LFS2_READONLY // write out everything beforehand, may be noop if rdonly int err = lfs2_file_flush(lfs2, file); if (err) { - LFS2_TRACE("lfs2_file_seek -> %d", err); return err; } +#endif // find new pos lfs2_off_t npos = file->pos; @@ -3024,34 +3068,28 @@ lfs2_soff_t lfs2_file_seek(lfs2_t *lfs2, lfs2_file_t *file, if (npos > lfs2->file_max) { // file position out of range - LFS2_TRACE("lfs2_file_seek -> %d", LFS2_ERR_INVAL); return LFS2_ERR_INVAL; } // update pos file->pos = npos; - LFS2_TRACE("lfs2_file_seek -> %"PRId32, npos); return npos; } -int lfs2_file_truncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size) { - LFS2_TRACE("lfs2_file_truncate(%p, %p, %"PRIu32")", - (void*)lfs2, (void*)file, size); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); - LFS2_ASSERT((file->flags & 3) != LFS2_O_RDONLY); +#ifndef LFS2_READONLY +static int lfs2_file_rawtruncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size) { + LFS2_ASSERT((file->flags & LFS2_O_WRONLY) == LFS2_O_WRONLY); if (size > LFS2_FILE_MAX) { - LFS2_TRACE("lfs2_file_truncate -> %d", LFS2_ERR_INVAL); return LFS2_ERR_INVAL; } lfs2_off_t pos = file->pos; - lfs2_off_t oldsize = lfs2_file_size(lfs2, file); + lfs2_off_t oldsize = lfs2_file_rawsize(lfs2, file); if (size < oldsize) { // need to flush since directly changing metadata int err = lfs2_file_flush(lfs2, file); if (err) { - LFS2_TRACE("lfs2_file_truncate -> %d", err); return err; } @@ -3060,7 +3098,6 @@ int lfs2_file_truncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size) { file->ctz.head, file->ctz.size, size, &file->block, &file->off); if (err) { - LFS2_TRACE("lfs2_file_truncate -> %d", err); return err; } @@ -3070,97 +3107,80 @@ int lfs2_file_truncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - lfs2_soff_t res = lfs2_file_seek(lfs2, file, 0, LFS2_SEEK_END); + lfs2_soff_t res = lfs2_file_rawseek(lfs2, file, 0, LFS2_SEEK_END); if (res < 0) { - LFS2_TRACE("lfs2_file_truncate -> %"PRId32, res); return (int)res; } } // fill with zeros while (file->pos < size) { - lfs2_ssize_t res = lfs2_file_write(lfs2, file, &(uint8_t){0}, 1); + lfs2_ssize_t res = lfs2_file_rawwrite(lfs2, file, &(uint8_t){0}, 1); if (res < 0) { - LFS2_TRACE("lfs2_file_truncate -> %"PRId32, res); return (int)res; } } } // restore pos - lfs2_soff_t res = lfs2_file_seek(lfs2, file, pos, LFS2_SEEK_SET); + lfs2_soff_t res = lfs2_file_rawseek(lfs2, file, pos, LFS2_SEEK_SET); if (res < 0) { - LFS2_TRACE("lfs2_file_truncate -> %"PRId32, res); return (int)res; } - LFS2_TRACE("lfs2_file_truncate -> %d", 0); return 0; } +#endif -lfs2_soff_t lfs2_file_tell(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_TRACE("lfs2_file_tell(%p, %p)", (void*)lfs2, (void*)file); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); +static lfs2_soff_t lfs2_file_rawtell(lfs2_t *lfs2, lfs2_file_t *file) { (void)lfs2; - LFS2_TRACE("lfs2_file_tell -> %"PRId32, file->pos); return file->pos; } -int lfs2_file_rewind(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_TRACE("lfs2_file_rewind(%p, %p)", (void*)lfs2, (void*)file); - lfs2_soff_t res = lfs2_file_seek(lfs2, file, 0, LFS2_SEEK_SET); +static int lfs2_file_rawrewind(lfs2_t *lfs2, lfs2_file_t *file) { + lfs2_soff_t res = lfs2_file_rawseek(lfs2, file, 0, LFS2_SEEK_SET); if (res < 0) { - LFS2_TRACE("lfs2_file_rewind -> %"PRId32, res); return (int)res; } - LFS2_TRACE("lfs2_file_rewind -> %d", 0); return 0; } -lfs2_soff_t lfs2_file_size(lfs2_t *lfs2, lfs2_file_t *file) { - LFS2_TRACE("lfs2_file_size(%p, %p)", (void*)lfs2, (void*)file); - LFS2_ASSERT(file->flags & LFS2_F_OPENED); +static lfs2_soff_t lfs2_file_rawsize(lfs2_t *lfs2, lfs2_file_t *file) { (void)lfs2; + +#ifndef LFS2_READONLY if (file->flags & LFS2_F_WRITING) { - LFS2_TRACE("lfs2_file_size -> %"PRId32, - lfs2_max(file->pos, file->ctz.size)); return lfs2_max(file->pos, file->ctz.size); - } else { - LFS2_TRACE("lfs2_file_size -> %"PRId32, file->ctz.size); - return file->ctz.size; } +#endif + + return file->ctz.size; } /// General fs operations /// -int lfs2_stat(lfs2_t *lfs2, const char *path, struct lfs2_info *info) { - LFS2_TRACE("lfs2_stat(%p, \"%s\", %p)", (void*)lfs2, path, (void*)info); +static int lfs2_rawstat(lfs2_t *lfs2, const char *path, struct lfs2_info *info) { lfs2_mdir_t cwd; lfs2_stag_t tag = lfs2_dir_find(lfs2, &cwd, &path, NULL); if (tag < 0) { - LFS2_TRACE("lfs2_stat -> %"PRId32, tag); return (int)tag; } - int err = lfs2_dir_getinfo(lfs2, &cwd, lfs2_tag_id(tag), info); - LFS2_TRACE("lfs2_stat -> %d", err); - return err; + return lfs2_dir_getinfo(lfs2, &cwd, lfs2_tag_id(tag), info); } -int lfs2_remove(lfs2_t *lfs2, const char *path) { - LFS2_TRACE("lfs2_remove(%p, \"%s\")", (void*)lfs2, path); +#ifndef LFS2_READONLY +static int lfs2_rawremove(lfs2_t *lfs2, const char *path) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs2_fs_forceconsistency(lfs2); if (err) { - LFS2_TRACE("lfs2_remove -> %d", err); return err; } lfs2_mdir_t cwd; lfs2_stag_t tag = lfs2_dir_find(lfs2, &cwd, &path, NULL); if (tag < 0 || lfs2_tag_id(tag) == 0x3ff) { - LFS2_TRACE("lfs2_remove -> %"PRId32, (tag < 0) ? tag : LFS2_ERR_INVAL); return (tag < 0) ? (int)tag : LFS2_ERR_INVAL; } @@ -3172,19 +3192,16 @@ int lfs2_remove(lfs2_t *lfs2, const char *path) { lfs2_stag_t res = lfs2_dir_get(lfs2, &cwd, LFS2_MKTAG(0x700, 0x3ff, 0), LFS2_MKTAG(LFS2_TYPE_STRUCT, lfs2_tag_id(tag), 8), pair); if (res < 0) { - LFS2_TRACE("lfs2_remove -> %"PRId32, res); return (int)res; } lfs2_pair_fromle32(pair); err = lfs2_dir_fetch(lfs2, &dir.m, pair); if (err) { - LFS2_TRACE("lfs2_remove -> %d", err); return err; } if (dir.m.count > 0 || dir.m.split) { - LFS2_TRACE("lfs2_remove -> %d", LFS2_ERR_NOTEMPTY); return LFS2_ERR_NOTEMPTY; } @@ -3200,10 +3217,9 @@ int lfs2_remove(lfs2_t *lfs2, const char *path) { // delete the entry err = lfs2_dir_commit(lfs2, &cwd, LFS2_MKATTRS( - {LFS2_MKTAG(LFS2_TYPE_DELETE, lfs2_tag_id(tag), 0)})); + {LFS2_MKTAG(LFS2_TYPE_DELETE, lfs2_tag_id(tag), 0), NULL})); if (err) { lfs2->mlist = dir.next; - LFS2_TRACE("lfs2_remove -> %d", err); return err; } @@ -3214,28 +3230,24 @@ int lfs2_remove(lfs2_t *lfs2, const char *path) { err = lfs2_fs_pred(lfs2, dir.m.pair, &cwd); if (err) { - LFS2_TRACE("lfs2_remove -> %d", err); return err; } err = lfs2_dir_drop(lfs2, &cwd, &dir.m); if (err) { - LFS2_TRACE("lfs2_remove -> %d", err); return err; } } - LFS2_TRACE("lfs2_remove -> %d", 0); return 0; } +#endif -int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { - LFS2_TRACE("lfs2_rename(%p, \"%s\", \"%s\")", (void*)lfs2, oldpath, newpath); - +#ifndef LFS2_READONLY +static int lfs2_rawrename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs2_fs_forceconsistency(lfs2); if (err) { - LFS2_TRACE("lfs2_rename -> %d", err); return err; } @@ -3243,8 +3255,6 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { lfs2_mdir_t oldcwd; lfs2_stag_t oldtag = lfs2_dir_find(lfs2, &oldcwd, &oldpath, NULL); if (oldtag < 0 || lfs2_tag_id(oldtag) == 0x3ff) { - LFS2_TRACE("lfs2_rename -> %"PRId32, - (oldtag < 0) ? oldtag : LFS2_ERR_INVAL); return (oldtag < 0) ? (int)oldtag : LFS2_ERR_INVAL; } @@ -3254,8 +3264,6 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { lfs2_stag_t prevtag = lfs2_dir_find(lfs2, &newcwd, &newpath, &newid); if ((prevtag < 0 || lfs2_tag_id(prevtag) == 0x3ff) && !(prevtag == LFS2_ERR_NOENT && newid != 0x3ff)) { - LFS2_TRACE("lfs2_rename -> %"PRId32, - (prevtag < 0) ? prevtag : LFS2_ERR_INVAL); return (prevtag < 0) ? (int)prevtag : LFS2_ERR_INVAL; } @@ -3269,7 +3277,6 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { // check that name fits lfs2_size_t nlen = strlen(newpath); if (nlen > lfs2->name_max) { - LFS2_TRACE("lfs2_rename -> %d", LFS2_ERR_NAMETOOLONG); return LFS2_ERR_NAMETOOLONG; } @@ -3280,11 +3287,9 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { newoldid += 1; } } else if (lfs2_tag_type3(prevtag) != lfs2_tag_type3(oldtag)) { - LFS2_TRACE("lfs2_rename -> %d", LFS2_ERR_ISDIR); return LFS2_ERR_ISDIR; } else if (samepair && newid == newoldid) { // we're renaming to ourselves?? - LFS2_TRACE("lfs2_rename -> %d", 0); return 0; } else if (lfs2_tag_type3(prevtag) == LFS2_TYPE_DIR) { // must be empty before removal @@ -3292,7 +3297,6 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { lfs2_stag_t res = lfs2_dir_get(lfs2, &newcwd, LFS2_MKTAG(0x700, 0x3ff, 0), LFS2_MKTAG(LFS2_TYPE_STRUCT, newid, 8), prevpair); if (res < 0) { - LFS2_TRACE("lfs2_rename -> %"PRId32, res); return (int)res; } lfs2_pair_fromle32(prevpair); @@ -3300,12 +3304,10 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { // must be empty before removal err = lfs2_dir_fetch(lfs2, &prevdir.m, prevpair); if (err) { - LFS2_TRACE("lfs2_rename -> %d", err); return err; } if (prevdir.m.count > 0 || prevdir.m.split) { - LFS2_TRACE("lfs2_rename -> %d", LFS2_ERR_NOTEMPTY); return LFS2_ERR_NOTEMPTY; } @@ -3326,15 +3328,14 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { // move over all attributes err = lfs2_dir_commit(lfs2, &newcwd, LFS2_MKATTRS( {LFS2_MKTAG_IF(prevtag != LFS2_ERR_NOENT, - LFS2_TYPE_DELETE, newid, 0)}, - {LFS2_MKTAG(LFS2_TYPE_CREATE, newid, 0)}, + LFS2_TYPE_DELETE, newid, 0), NULL}, + {LFS2_MKTAG(LFS2_TYPE_CREATE, newid, 0), NULL}, {LFS2_MKTAG(lfs2_tag_type3(oldtag), newid, strlen(newpath)), newpath}, {LFS2_MKTAG(LFS2_FROM_MOVE, newid, lfs2_tag_id(oldtag)), &oldcwd}, {LFS2_MKTAG_IF(samepair, - LFS2_TYPE_DELETE, newoldid, 0)})); + LFS2_TYPE_DELETE, newoldid, 0), NULL})); if (err) { lfs2->mlist = prevdir.next; - LFS2_TRACE("lfs2_rename -> %d", err); return err; } @@ -3344,10 +3345,9 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { // prep gstate and delete move id lfs2_fs_prepmove(lfs2, 0x3ff, NULL); err = lfs2_dir_commit(lfs2, &oldcwd, LFS2_MKATTRS( - {LFS2_MKTAG(LFS2_TYPE_DELETE, lfs2_tag_id(oldtag), 0)})); + {LFS2_MKTAG(LFS2_TYPE_DELETE, lfs2_tag_id(oldtag), 0), NULL})); if (err) { lfs2->mlist = prevdir.next; - LFS2_TRACE("lfs2_rename -> %d", err); return err; } } @@ -3359,29 +3359,24 @@ int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { err = lfs2_fs_pred(lfs2, prevdir.m.pair, &newcwd); if (err) { - LFS2_TRACE("lfs2_rename -> %d", err); return err; } err = lfs2_dir_drop(lfs2, &newcwd, &prevdir.m); if (err) { - LFS2_TRACE("lfs2_rename -> %d", err); return err; } } - LFS2_TRACE("lfs2_rename -> %d", 0); return 0; } +#endif -lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, +static lfs2_ssize_t lfs2_rawgetattr(lfs2_t *lfs2, const char *path, uint8_t type, void *buffer, lfs2_size_t size) { - LFS2_TRACE("lfs2_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", - (void*)lfs2, path, type, buffer, size); lfs2_mdir_t cwd; lfs2_stag_t tag = lfs2_dir_find(lfs2, &cwd, &path, NULL); if (tag < 0) { - LFS2_TRACE("lfs2_getattr -> %"PRId32, tag); return tag; } @@ -3391,7 +3386,6 @@ lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, id = 0; int err = lfs2_dir_fetch(lfs2, &cwd, lfs2->root); if (err) { - LFS2_TRACE("lfs2_getattr -> %d", err); return err; } } @@ -3402,19 +3396,16 @@ lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, buffer); if (tag < 0) { if (tag == LFS2_ERR_NOENT) { - LFS2_TRACE("lfs2_getattr -> %d", LFS2_ERR_NOATTR); return LFS2_ERR_NOATTR; } - LFS2_TRACE("lfs2_getattr -> %"PRId32, tag); return tag; } - size = lfs2_tag_size(tag); - LFS2_TRACE("lfs2_getattr -> %"PRId32, size); - return size; + return lfs2_tag_size(tag); } +#ifndef LFS2_READONLY static int lfs2_commitattr(lfs2_t *lfs2, const char *path, uint8_t type, const void *buffer, lfs2_size_t size) { lfs2_mdir_t cwd; @@ -3436,27 +3427,24 @@ static int lfs2_commitattr(lfs2_t *lfs2, const char *path, return lfs2_dir_commit(lfs2, &cwd, LFS2_MKATTRS( {LFS2_MKTAG(LFS2_TYPE_USERATTR + type, id, size), buffer})); } +#endif -int lfs2_setattr(lfs2_t *lfs2, const char *path, +#ifndef LFS2_READONLY +static int lfs2_rawsetattr(lfs2_t *lfs2, const char *path, uint8_t type, const void *buffer, lfs2_size_t size) { - LFS2_TRACE("lfs2_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", - (void*)lfs2, path, type, buffer, size); if (size > lfs2->attr_max) { - LFS2_TRACE("lfs2_setattr -> %d", LFS2_ERR_NOSPC); return LFS2_ERR_NOSPC; } - int err = lfs2_commitattr(lfs2, path, type, buffer, size); - LFS2_TRACE("lfs2_setattr -> %d", err); - return err; + return lfs2_commitattr(lfs2, path, type, buffer, size); } +#endif -int lfs2_removeattr(lfs2_t *lfs2, const char *path, uint8_t type) { - LFS2_TRACE("lfs2_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs2, path, type); - int err = lfs2_commitattr(lfs2, path, type, NULL, 0x3ff); - LFS2_TRACE("lfs2_removeattr -> %d", err); - return err; +#ifndef LFS2_READONLY +static int lfs2_rawremoveattr(lfs2_t *lfs2, const char *path, uint8_t type) { + return lfs2_commitattr(lfs2, path, type, NULL, 0x3ff); } +#endif /// Filesystem operations /// @@ -3584,28 +3572,12 @@ static int lfs2_deinit(lfs2_t *lfs2) { return 0; } -int lfs2_format(lfs2_t *lfs2, const struct lfs2_config *cfg) { - LFS2_TRACE("lfs2_format(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs2, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); +#ifndef LFS2_READONLY +static int lfs2_rawformat(lfs2_t *lfs2, const struct lfs2_config *cfg) { int err = 0; { err = lfs2_init(lfs2, cfg); if (err) { - LFS2_TRACE("lfs2_format -> %d", err); return err; } @@ -3636,7 +3608,7 @@ int lfs2_format(lfs2_t *lfs2, const struct lfs2_config *cfg) { lfs2_superblock_tole32(&superblock); err = lfs2_dir_commit(lfs2, &root, LFS2_MKATTRS( - {LFS2_MKTAG(LFS2_TYPE_CREATE, 0, 0)}, + {LFS2_MKTAG(LFS2_TYPE_CREATE, 0, 0), NULL}, {LFS2_MKTAG(LFS2_TYPE_SUPERBLOCK, 0, 8), "littlefs"}, {LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, 0, sizeof(superblock)), &superblock})); @@ -3661,30 +3633,14 @@ int lfs2_format(lfs2_t *lfs2, const struct lfs2_config *cfg) { cleanup: lfs2_deinit(lfs2); - LFS2_TRACE("lfs2_format -> %d", err); return err; + } +#endif -int lfs2_mount(lfs2_t *lfs2, const struct lfs2_config *cfg) { - LFS2_TRACE("lfs2_mount(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs2, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); +static int lfs2_rawmount(lfs2_t *lfs2, const struct lfs2_config *cfg) { int err = lfs2_init(lfs2, cfg); if (err) { - LFS2_TRACE("lfs2_mount -> %d", err); return err; } @@ -3797,28 +3753,25 @@ int lfs2_mount(lfs2_t *lfs2, const struct lfs2_config *cfg) { lfs2->gstate.tag += !lfs2_tag_isvalid(lfs2->gstate.tag); lfs2->gdisk = lfs2->gstate; - // setup free lookahead - lfs2_alloc_reset(lfs2); + // setup free lookahead, to distribute allocations uniformly across + // boots, we start the allocator at a random location + lfs2->free.off = lfs2->seed % lfs2->cfg->block_count; + lfs2_alloc_drop(lfs2); - LFS2_TRACE("lfs2_mount -> %d", 0); return 0; cleanup: - lfs2_unmount(lfs2); - LFS2_TRACE("lfs2_mount -> %d", err); + lfs2_rawunmount(lfs2); return err; } -int lfs2_unmount(lfs2_t *lfs2) { - LFS2_TRACE("lfs2_unmount(%p)", (void*)lfs2); - int err = lfs2_deinit(lfs2); - LFS2_TRACE("lfs2_unmount -> %d", err); - return err; +static int lfs2_rawunmount(lfs2_t *lfs2) { + return lfs2_deinit(lfs2); } /// Filesystem filesystem operations /// -int lfs2_fs_traverseraw(lfs2_t *lfs2, +int lfs2_fs_rawtraverse(lfs2_t *lfs2, int (*cb)(void *data, lfs2_block_t block), void *data, bool includeorphans) { // iterate over metadata pairs @@ -3888,6 +3841,7 @@ int lfs2_fs_traverseraw(lfs2_t *lfs2, } } +#ifndef LFS2_READONLY // iterate over any open files for (lfs2_file_t *f = (lfs2_file_t*)lfs2->mlist; f; f = f->next) { if (f->type != LFS2_TYPE_REG) { @@ -3910,19 +3864,12 @@ int lfs2_fs_traverseraw(lfs2_t *lfs2, } } } +#endif return 0; } -int lfs2_fs_traverse(lfs2_t *lfs2, - int (*cb)(void *data, lfs2_block_t block), void *data) { - LFS2_TRACE("lfs2_fs_traverse(%p, %p, %p)", - (void*)lfs2, (void*)(uintptr_t)cb, data); - int err = lfs2_fs_traverseraw(lfs2, cb, data, true); - LFS2_TRACE("lfs2_fs_traverse -> %d", 0); - return err; -} - +#ifndef LFS2_READONLY static int lfs2_fs_pred(lfs2_t *lfs2, const lfs2_block_t pair[2], lfs2_mdir_t *pdir) { // iterate over all directory directory entries @@ -3948,12 +3895,16 @@ static int lfs2_fs_pred(lfs2_t *lfs2, return LFS2_ERR_NOENT; } +#endif +#ifndef LFS2_READONLY struct lfs2_fs_parent_match { lfs2_t *lfs2; const lfs2_block_t pair[2]; }; +#endif +#ifndef LFS2_READONLY static int lfs2_fs_parent_match(void *data, lfs2_tag_t tag, const void *buffer) { struct lfs2_fs_parent_match *find = data; @@ -3972,7 +3923,9 @@ static int lfs2_fs_parent_match(void *data, lfs2_pair_fromle32(child); return (lfs2_pair_cmp(child, find->pair) == 0) ? LFS2_CMP_EQ : LFS2_CMP_LT; } +#endif +#ifndef LFS2_READONLY static lfs2_stag_t lfs2_fs_parent(lfs2_t *lfs2, const lfs2_block_t pair[2], lfs2_mdir_t *parent) { // use fetchmatch with callback to find pairs @@ -3999,7 +3952,9 @@ static lfs2_stag_t lfs2_fs_parent(lfs2_t *lfs2, const lfs2_block_t pair[2], return LFS2_ERR_NOENT; } +#endif +#ifndef LFS2_READONLY static int lfs2_fs_relocate(lfs2_t *lfs2, const lfs2_block_t oldpair[2], lfs2_block_t newpair[2]) { // update internal root @@ -4050,7 +4005,7 @@ static int lfs2_fs_relocate(lfs2_t *lfs2, lfs2_pair_tole32(newpair); int err = lfs2_dir_commit(lfs2, &parent, LFS2_MKATTRS( {LFS2_MKTAG_IF(moveid != 0x3ff, - LFS2_TYPE_DELETE, moveid, 0)}, + LFS2_TYPE_DELETE, moveid, 0), NULL}, {tag, newpair})); lfs2_pair_fromle32(newpair); if (err) { @@ -4084,7 +4039,7 @@ static int lfs2_fs_relocate(lfs2_t *lfs2, lfs2_pair_tole32(newpair); err = lfs2_dir_commit(lfs2, &parent, LFS2_MKATTRS( {LFS2_MKTAG_IF(moveid != 0x3ff, - LFS2_TYPE_DELETE, moveid, 0)}, + LFS2_TYPE_DELETE, moveid, 0), NULL}, {LFS2_MKTAG(LFS2_TYPE_TAIL + parent.split, 0x3ff, 8), newpair})); lfs2_pair_fromle32(newpair); if (err) { @@ -4094,14 +4049,18 @@ static int lfs2_fs_relocate(lfs2_t *lfs2, return 0; } +#endif +#ifndef LFS2_READONLY static void lfs2_fs_preporphans(lfs2_t *lfs2, int8_t orphans) { LFS2_ASSERT(lfs2_tag_size(lfs2->gstate.tag) > 0 || orphans >= 0); lfs2->gstate.tag += orphans; lfs2->gstate.tag = ((lfs2->gstate.tag & ~LFS2_MKTAG(0x800, 0, 0)) | ((uint32_t)lfs2_gstate_hasorphans(&lfs2->gstate) << 31)); } +#endif +#ifndef LFS2_READONLY static void lfs2_fs_prepmove(lfs2_t *lfs2, uint16_t id, const lfs2_block_t pair[2]) { lfs2->gstate.tag = ((lfs2->gstate.tag & ~LFS2_MKTAG(0x7ff, 0x3ff, 0)) | @@ -4109,7 +4068,9 @@ static void lfs2_fs_prepmove(lfs2_t *lfs2, lfs2->gstate.pair[0] = (id != 0x3ff) ? pair[0] : 0; lfs2->gstate.pair[1] = (id != 0x3ff) ? pair[1] : 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_fs_demove(lfs2_t *lfs2) { if (!lfs2_gstate_hasmove(&lfs2->gdisk)) { return 0; @@ -4132,14 +4093,16 @@ static int lfs2_fs_demove(lfs2_t *lfs2) { uint16_t moveid = lfs2_tag_id(lfs2->gdisk.tag); lfs2_fs_prepmove(lfs2, 0x3ff, NULL); err = lfs2_dir_commit(lfs2, &movedir, LFS2_MKATTRS( - {LFS2_MKTAG(LFS2_TYPE_DELETE, moveid, 0)})); + {LFS2_MKTAG(LFS2_TYPE_DELETE, moveid, 0), NULL})); if (err) { return err; } return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_fs_deorphan(lfs2_t *lfs2) { if (!lfs2_gstate_hasorphans(&lfs2->gstate)) { return 0; @@ -4213,7 +4176,9 @@ static int lfs2_fs_deorphan(lfs2_t *lfs2) { lfs2_fs_preporphans(lfs2, -lfs2_gstate_getorphans(&lfs2->gstate)); return 0; } +#endif +#ifndef LFS2_READONLY static int lfs2_fs_forceconsistency(lfs2_t *lfs2) { int err = lfs2_fs_demove(lfs2); if (err) { @@ -4227,6 +4192,7 @@ static int lfs2_fs_forceconsistency(lfs2_t *lfs2) { return 0; } +#endif static int lfs2_fs_size_count(void *p, lfs2_block_t block) { (void)block; @@ -4235,16 +4201,13 @@ static int lfs2_fs_size_count(void *p, lfs2_block_t block) { return 0; } -lfs2_ssize_t lfs2_fs_size(lfs2_t *lfs2) { - LFS2_TRACE("lfs2_fs_size(%p)", (void*)lfs2); +static lfs2_ssize_t lfs2_fs_rawsize(lfs2_t *lfs2) { lfs2_size_t size = 0; - int err = lfs2_fs_traverseraw(lfs2, lfs2_fs_size_count, &size, false); + int err = lfs2_fs_rawtraverse(lfs2, lfs2_fs_size_count, &size, false); if (err) { - LFS2_TRACE("lfs2_fs_size -> %d", err); return err; } - LFS2_TRACE("lfs2_fs_size -> %d", err); return size; } @@ -4669,27 +4632,10 @@ static int lfs21_unmount(lfs2_t *lfs2) { } /// v1 migration /// -int lfs2_migrate(lfs2_t *lfs2, const struct lfs2_config *cfg) { - LFS2_TRACE("lfs2_migrate(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs2, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); +static int lfs2_rawmigrate(lfs2_t *lfs2, const struct lfs2_config *cfg) { struct lfs21 lfs21; int err = lfs21_mount(lfs2, &lfs21, cfg); if (err) { - LFS2_TRACE("lfs2_migrate -> %d", err); return err; } @@ -4906,8 +4852,539 @@ int lfs2_migrate(lfs2_t *lfs2, const struct lfs2_config *cfg) { cleanup: lfs21_unmount(lfs2); - LFS2_TRACE("lfs2_migrate -> %d", err); return err; } #endif + + +/// Public API wrappers /// + +// Here we can add tracing/thread safety easily + +// Thread-safe wrappers if enabled +#ifdef LFS2_THREADSAFE +#define LFS2_LOCK(cfg) cfg->lock(cfg) +#define LFS2_UNLOCK(cfg) cfg->unlock(cfg) +#else +#define LFS2_LOCK(cfg) ((void)cfg, 0) +#define LFS2_UNLOCK(cfg) ((void)cfg) +#endif + +// Public API +#ifndef LFS2_READONLY +int lfs2_format(lfs2_t *lfs2, const struct lfs2_config *cfg) { + int err = LFS2_LOCK(cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_format(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs2, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); + + err = lfs2_rawformat(lfs2, cfg); + + LFS2_TRACE("lfs2_format -> %d", err); + LFS2_UNLOCK(cfg); + return err; +} +#endif + +int lfs2_mount(lfs2_t *lfs2, const struct lfs2_config *cfg) { + int err = LFS2_LOCK(cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_mount(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs2, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); + + err = lfs2_rawmount(lfs2, cfg); + + LFS2_TRACE("lfs2_mount -> %d", err); + LFS2_UNLOCK(cfg); + return err; +} + +int lfs2_unmount(lfs2_t *lfs2) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_unmount(%p)", (void*)lfs2); + + err = lfs2_rawunmount(lfs2); + + LFS2_TRACE("lfs2_unmount -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +#ifndef LFS2_READONLY +int lfs2_remove(lfs2_t *lfs2, const char *path) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_remove(%p, \"%s\")", (void*)lfs2, path); + + err = lfs2_rawremove(lfs2, path); + + LFS2_TRACE("lfs2_remove -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +#ifndef LFS2_READONLY +int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_rename(%p, \"%s\", \"%s\")", (void*)lfs2, oldpath, newpath); + + err = lfs2_rawrename(lfs2, oldpath, newpath); + + LFS2_TRACE("lfs2_rename -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +int lfs2_stat(lfs2_t *lfs2, const char *path, struct lfs2_info *info) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_stat(%p, \"%s\", %p)", (void*)lfs2, path, (void*)info); + + err = lfs2_rawstat(lfs2, path, info); + + LFS2_TRACE("lfs2_stat -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, + uint8_t type, void *buffer, lfs2_size_t size) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", + (void*)lfs2, path, type, buffer, size); + + lfs2_ssize_t res = lfs2_rawgetattr(lfs2, path, type, buffer, size); + + LFS2_TRACE("lfs2_getattr -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +#ifndef LFS2_READONLY +int lfs2_setattr(lfs2_t *lfs2, const char *path, + uint8_t type, const void *buffer, lfs2_size_t size) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", + (void*)lfs2, path, type, buffer, size); + + err = lfs2_rawsetattr(lfs2, path, type, buffer, size); + + LFS2_TRACE("lfs2_setattr -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +#ifndef LFS2_READONLY +int lfs2_removeattr(lfs2_t *lfs2, const char *path, uint8_t type) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs2, path, type); + + err = lfs2_rawremoveattr(lfs2, path, type); + + LFS2_TRACE("lfs2_removeattr -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +int lfs2_file_open(lfs2_t *lfs2, lfs2_file_t *file, const char *path, int flags) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_open(%p, %p, \"%s\", %x)", + (void*)lfs2, (void*)file, path, flags); + LFS2_ASSERT(!lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + err = lfs2_file_rawopen(lfs2, file, path, flags); + + LFS2_TRACE("lfs2_file_open -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +int lfs2_file_opencfg(lfs2_t *lfs2, lfs2_file_t *file, + const char *path, int flags, + const struct lfs2_file_config *cfg) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_opencfg(%p, %p, \"%s\", %x, %p {" + ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", + (void*)lfs2, (void*)file, path, flags, + (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); + LFS2_ASSERT(!lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + err = lfs2_file_rawopencfg(lfs2, file, path, flags, cfg); + + LFS2_TRACE("lfs2_file_opencfg -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +int lfs2_file_close(lfs2_t *lfs2, lfs2_file_t *file) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_close(%p, %p)", (void*)lfs2, (void*)file); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + err = lfs2_file_rawclose(lfs2, file); + + LFS2_TRACE("lfs2_file_close -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +#ifndef LFS2_READONLY +int lfs2_file_sync(lfs2_t *lfs2, lfs2_file_t *file) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_sync(%p, %p)", (void*)lfs2, (void*)file); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + err = lfs2_file_rawsync(lfs2, file); + + LFS2_TRACE("lfs2_file_sync -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, + void *buffer, lfs2_size_t size) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_read(%p, %p, %p, %"PRIu32")", + (void*)lfs2, (void*)file, buffer, size); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + lfs2_ssize_t res = lfs2_file_rawread(lfs2, file, buffer, size); + + LFS2_TRACE("lfs2_file_read -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +#ifndef LFS2_READONLY +lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, + const void *buffer, lfs2_size_t size) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_write(%p, %p, %p, %"PRIu32")", + (void*)lfs2, (void*)file, buffer, size); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + lfs2_ssize_t res = lfs2_file_rawwrite(lfs2, file, buffer, size); + + LFS2_TRACE("lfs2_file_write -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} +#endif + +lfs2_soff_t lfs2_file_seek(lfs2_t *lfs2, lfs2_file_t *file, + lfs2_soff_t off, int whence) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_seek(%p, %p, %"PRId32", %d)", + (void*)lfs2, (void*)file, off, whence); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + lfs2_soff_t res = lfs2_file_rawseek(lfs2, file, off, whence); + + LFS2_TRACE("lfs2_file_seek -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +#ifndef LFS2_READONLY +int lfs2_file_truncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_truncate(%p, %p, %"PRIu32")", + (void*)lfs2, (void*)file, size); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + err = lfs2_file_rawtruncate(lfs2, file, size); + + LFS2_TRACE("lfs2_file_truncate -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +lfs2_soff_t lfs2_file_tell(lfs2_t *lfs2, lfs2_file_t *file) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_tell(%p, %p)", (void*)lfs2, (void*)file); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + lfs2_soff_t res = lfs2_file_rawtell(lfs2, file); + + LFS2_TRACE("lfs2_file_tell -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +int lfs2_file_rewind(lfs2_t *lfs2, lfs2_file_t *file) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_rewind(%p, %p)", (void*)lfs2, (void*)file); + + err = lfs2_file_rawrewind(lfs2, file); + + LFS2_TRACE("lfs2_file_rewind -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +lfs2_soff_t lfs2_file_size(lfs2_t *lfs2, lfs2_file_t *file) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_file_size(%p, %p)", (void*)lfs2, (void*)file); + LFS2_ASSERT(lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)file)); + + lfs2_soff_t res = lfs2_file_rawsize(lfs2, file); + + LFS2_TRACE("lfs2_file_size -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +#ifndef LFS2_READONLY +int lfs2_mkdir(lfs2_t *lfs2, const char *path) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_mkdir(%p, \"%s\")", (void*)lfs2, path); + + err = lfs2_rawmkdir(lfs2, path); + + LFS2_TRACE("lfs2_mkdir -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} +#endif + +int lfs2_dir_open(lfs2_t *lfs2, lfs2_dir_t *dir, const char *path) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_open(%p, %p, \"%s\")", (void*)lfs2, (void*)dir, path); + LFS2_ASSERT(!lfs2_mlist_isopen(lfs2->mlist, (struct lfs2_mlist*)dir)); + + err = lfs2_dir_rawopen(lfs2, dir, path); + + LFS2_TRACE("lfs2_dir_open -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +int lfs2_dir_close(lfs2_t *lfs2, lfs2_dir_t *dir) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_close(%p, %p)", (void*)lfs2, (void*)dir); + + err = lfs2_dir_rawclose(lfs2, dir); + + LFS2_TRACE("lfs2_dir_close -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +int lfs2_dir_read(lfs2_t *lfs2, lfs2_dir_t *dir, struct lfs2_info *info) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_read(%p, %p, %p)", + (void*)lfs2, (void*)dir, (void*)info); + + err = lfs2_dir_rawread(lfs2, dir, info); + + LFS2_TRACE("lfs2_dir_read -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +int lfs2_dir_seek(lfs2_t *lfs2, lfs2_dir_t *dir, lfs2_off_t off) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_seek(%p, %p, %"PRIu32")", + (void*)lfs2, (void*)dir, off); + + err = lfs2_dir_rawseek(lfs2, dir, off); + + LFS2_TRACE("lfs2_dir_seek -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +lfs2_soff_t lfs2_dir_tell(lfs2_t *lfs2, lfs2_dir_t *dir) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_tell(%p, %p)", (void*)lfs2, (void*)dir); + + lfs2_soff_t res = lfs2_dir_rawtell(lfs2, dir); + + LFS2_TRACE("lfs2_dir_tell -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +int lfs2_dir_rewind(lfs2_t *lfs2, lfs2_dir_t *dir) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_dir_rewind(%p, %p)", (void*)lfs2, (void*)dir); + + err = lfs2_dir_rawrewind(lfs2, dir); + + LFS2_TRACE("lfs2_dir_rewind -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +lfs2_ssize_t lfs2_fs_size(lfs2_t *lfs2) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_fs_size(%p)", (void*)lfs2); + + lfs2_ssize_t res = lfs2_fs_rawsize(lfs2); + + LFS2_TRACE("lfs2_fs_size -> %"PRId32, res); + LFS2_UNLOCK(lfs2->cfg); + return res; +} + +int lfs2_fs_traverse(lfs2_t *lfs2, int (*cb)(void *, lfs2_block_t), void *data) { + int err = LFS2_LOCK(lfs2->cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_fs_traverse(%p, %p, %p)", + (void*)lfs2, (void*)(uintptr_t)cb, data); + + err = lfs2_fs_rawtraverse(lfs2, cb, data, true); + + LFS2_TRACE("lfs2_fs_traverse -> %d", err); + LFS2_UNLOCK(lfs2->cfg); + return err; +} + +#ifdef LFS2_MIGRATE +int lfs2_migrate(lfs2_t *lfs2, const struct lfs2_config *cfg) { + int err = LFS2_LOCK(cfg); + if (err) { + return err; + } + LFS2_TRACE("lfs2_migrate(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs2, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); + + err = lfs2_rawmigrate(lfs2, cfg); + + LFS2_TRACE("lfs2_migrate -> %d", err); + LFS2_UNLOCK(cfg); + return err; +} +#endif + diff --git a/lib/littlefs/lfs2.h b/lib/littlefs/lfs2.h index c89af79cabddd..e2afc7792ce4f 100644 --- a/lib/littlefs/lfs2.h +++ b/lib/littlefs/lfs2.h @@ -9,6 +9,7 @@ #include #include +#include "lfs2_util.h" #ifdef __cplusplus extern "C" @@ -21,7 +22,7 @@ extern "C" // Software library version // Major (top-nibble), incremented on backwards incompatible changes // Minor (bottom-nibble), incremented on feature additions -#define LFS2_VERSION 0x00020002 +#define LFS2_VERSION 0x00020003 #define LFS2_VERSION_MAJOR (0xffff & (LFS2_VERSION >> 16)) #define LFS2_VERSION_MINOR (0xffff & (LFS2_VERSION >> 0)) @@ -123,20 +124,25 @@ enum lfs2_type { enum lfs2_open_flags { // open flags LFS2_O_RDONLY = 1, // Open a file as read only +#ifndef LFS2_READONLY LFS2_O_WRONLY = 2, // Open a file as write only LFS2_O_RDWR = 3, // Open a file as read and write LFS2_O_CREAT = 0x0100, // Create a file if it does not exist LFS2_O_EXCL = 0x0200, // Fail if a file already exists LFS2_O_TRUNC = 0x0400, // Truncate the existing file to zero size LFS2_O_APPEND = 0x0800, // Move to end of file on every write +#endif // internally used flags +#ifndef LFS2_READONLY LFS2_F_DIRTY = 0x010000, // File does not match storage LFS2_F_WRITING = 0x020000, // File has been written since last flush +#endif LFS2_F_READING = 0x040000, // File has been read since last flush - LFS2_F_ERRED = 0x080000, // An error occured during write +#ifndef LFS2_READONLY + LFS2_F_ERRED = 0x080000, // An error occurred during write +#endif LFS2_F_INLINE = 0x100000, // Currently inlined in directory entry - LFS2_F_OPENED = 0x200000, // File has been opened }; // File seek flags @@ -174,6 +180,16 @@ struct lfs2_config { // are propogated to the user. int (*sync)(const struct lfs2_config *c); +#ifdef LFS2_THREADSAFE + // Lock the underlying block device. Negative error codes + // are propogated to the user. + int (*lock)(const struct lfs2_config *c); + + // Unlock the underlying block device. Negative error codes + // are propogated to the user. + int (*unlock)(const struct lfs2_config *c); +#endif + // Minimum size of a block read. All read operations will be a // multiple of this value. lfs2_size_t read_size; @@ -399,6 +415,7 @@ typedef struct lfs2 { /// Filesystem functions /// +#ifndef LFS2_READONLY // Format a block device with the littlefs // // Requires a littlefs object and config struct. This clobbers the littlefs @@ -407,6 +424,7 @@ typedef struct lfs2 { // // Returns a negative error code on failure. int lfs2_format(lfs2_t *lfs2, const struct lfs2_config *config); +#endif // Mounts a littlefs // @@ -426,12 +444,15 @@ int lfs2_unmount(lfs2_t *lfs2); /// General operations /// +#ifndef LFS2_READONLY // Removes a file or directory // // If removing a directory, the directory must be empty. // Returns a negative error code on failure. int lfs2_remove(lfs2_t *lfs2, const char *path); +#endif +#ifndef LFS2_READONLY // Rename or move a file or directory // // If the destination exists, it must match the source in type. @@ -439,6 +460,7 @@ int lfs2_remove(lfs2_t *lfs2, const char *path); // // Returns a negative error code on failure. int lfs2_rename(lfs2_t *lfs2, const char *oldpath, const char *newpath); +#endif // Find info about a file or directory // @@ -461,6 +483,7 @@ int lfs2_stat(lfs2_t *lfs2, const char *path, struct lfs2_info *info); lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, uint8_t type, void *buffer, lfs2_size_t size); +#ifndef LFS2_READONLY // Set custom attributes // // Custom attributes are uniquely identified by an 8-bit type and limited @@ -470,13 +493,16 @@ lfs2_ssize_t lfs2_getattr(lfs2_t *lfs2, const char *path, // Returns a negative error code on failure. int lfs2_setattr(lfs2_t *lfs2, const char *path, uint8_t type, const void *buffer, lfs2_size_t size); +#endif +#ifndef LFS2_READONLY // Removes a custom attribute // // If an attribute is not found, nothing happens. // // Returns a negative error code on failure. int lfs2_removeattr(lfs2_t *lfs2, const char *path, uint8_t type); +#endif /// File operations /// @@ -525,6 +551,7 @@ int lfs2_file_sync(lfs2_t *lfs2, lfs2_file_t *file); lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, void *buffer, lfs2_size_t size); +#ifndef LFS2_READONLY // Write data to file // // Takes a buffer and size indicating the data to write. The file will not @@ -533,6 +560,7 @@ lfs2_ssize_t lfs2_file_read(lfs2_t *lfs2, lfs2_file_t *file, // Returns the number of bytes written, or a negative error code on failure. lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, const void *buffer, lfs2_size_t size); +#endif // Change the position of the file // @@ -541,10 +569,12 @@ lfs2_ssize_t lfs2_file_write(lfs2_t *lfs2, lfs2_file_t *file, lfs2_soff_t lfs2_file_seek(lfs2_t *lfs2, lfs2_file_t *file, lfs2_soff_t off, int whence); +#ifndef LFS2_READONLY // Truncates the size of the file to the specified size // // Returns a negative error code on failure. int lfs2_file_truncate(lfs2_t *lfs2, lfs2_file_t *file, lfs2_off_t size); +#endif // Return the position of the file // @@ -567,10 +597,12 @@ lfs2_soff_t lfs2_file_size(lfs2_t *lfs2, lfs2_file_t *file); /// Directory operations /// +#ifndef LFS2_READONLY // Create a directory // // Returns a negative error code on failure. int lfs2_mkdir(lfs2_t *lfs2, const char *path); +#endif // Open a directory // @@ -632,6 +664,7 @@ lfs2_ssize_t lfs2_fs_size(lfs2_t *lfs2); // Returns a negative error code on failure. int lfs2_fs_traverse(lfs2_t *lfs2, int (*cb)(void*, lfs2_block_t), void *data); +#ifndef LFS2_READONLY #ifdef LFS2_MIGRATE // Attempts to migrate a previous version of littlefs // @@ -646,6 +679,7 @@ int lfs2_fs_traverse(lfs2_t *lfs2, int (*cb)(void*, lfs2_block_t), void *data); // Returns a negative error code on failure. int lfs2_migrate(lfs2_t *lfs2, const struct lfs2_config *cfg); #endif +#endif #ifdef __cplusplus From 8a29319a157a69c1755d515e466b3e1c7ed8b76e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 9 Dec 2020 13:18:22 +1100 Subject: [PATCH 0482/3533] lib/littlefs: Guard lfs2_mlist_isopen with LFS2_NO_ASSERT. To prevent warnings about this function being unused when assertions are disabled. Signed-off-by: Damien George --- lib/littlefs/lfs2.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/littlefs/lfs2.c b/lib/littlefs/lfs2.c index 31f3c603e7b87..1f8c9d2d93d63 100644 --- a/lib/littlefs/lfs2.c +++ b/lib/littlefs/lfs2.c @@ -425,6 +425,7 @@ static inline void lfs2_superblock_tole32(lfs2_superblock_t *superblock) { superblock->attr_max = lfs2_tole32(superblock->attr_max); } +#ifndef LFS2_NO_ASSERT static inline bool lfs2_mlist_isopen(struct lfs2_mlist *head, struct lfs2_mlist *node) { for (struct lfs2_mlist **p = &head; *p; p = &(*p)->next) { @@ -435,6 +436,7 @@ static inline bool lfs2_mlist_isopen(struct lfs2_mlist *head, return false; } +#endif static inline void lfs2_mlist_remove(lfs2_t *lfs2, struct lfs2_mlist *mlist) { for (struct lfs2_mlist **p = &lfs2->mlist; *p; p = &(*p)->next) { From 460a181b77c875608c27dcb955560cff3a51f839 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 9 Dec 2020 11:02:47 +1100 Subject: [PATCH 0483/3533] stm32/mboot: Enable LFS2_READONLY for mboot builds with littlefs. To reduce code size, since mboot does not modify the filesystem. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 3d041e1c141ea..44372b0e8a4ae 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -71,7 +71,7 @@ CFLAGS += -DBOARD_$(BOARD) CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR) CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT -CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT +CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT -DLFS2_READONLY CFLAGS += -DBUILDING_MBOOT=1 CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) From f694a6fa20652e327151ac537b79180930c54ca0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 7 Dec 2020 16:18:33 +1100 Subject: [PATCH 0484/3533] lib/stm32lib: Update library for WB v1.10.0. Changes in this new library version are: - Update WB HAL to v1.10.0. Signed-off-by: Damien George --- lib/stm32lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stm32lib b/lib/stm32lib index 58fee7c92bd57..302c52794d2f5 160000 --- a/lib/stm32lib +++ b/lib/stm32lib @@ -1 +1 @@ -Subproject commit 58fee7c92bd576814d3f2afd92fbc62990270ecc +Subproject commit 302c52794d2f579903f4e49cbad1f5d3a7f401ad From f305c62a5fccb376db773fde6273e64e1fa9948f Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 7 Dec 2020 16:19:12 +1100 Subject: [PATCH 0485/3533] stm32/usb: Allocate 128 bytes to CDC data out EPs on non-multi-OTG MCUs. This much buffer space is required for CDC data out endpoints to avoid any buffer overflows when the USB CDC is saturated with data. Signed-off-by: Damien George --- ports/stm32/usb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 968b2999ced37..5003bb27cc235 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -102,8 +102,8 @@ STATIC const uint8_t usbd_fifo_size_cdc1[USBD_PMA_NUM_FIFO] = { #if MICROPY_HW_USB_CDC_NUM >= 2 STATIC const uint8_t usbd_fifo_size_cdc2[USBD_PMA_NUM_FIFO] = { 8, 8, 16, 16, // EP0(out), EP0(in), MSC/HID(out), MSC/HID(in) - 0, 8, 12, 12, // unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) - 0, 8, 12, 12, // unused, CDC2_CMD(in), CDC2_DATA(out), CDC2_DATA(in) + 0, 8, 16, 8, // unused, CDC_CMD(in), CDC_DATA(out), CDC_DATA(in) + 0, 8, 16, 8, // unused, CDC2_CMD(in), CDC2_DATA(out), CDC2_DATA(in) 0, 0, 0, 0, // 4x unused }; From e0bb7a53c3244ffa720fcdb64be5784379c5f596 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 14 Dec 2020 10:29:57 +1100 Subject: [PATCH 0486/3533] tests/misc/sys_settrace_features.py: Ignore CPython zipimport traces. Signed-off-by: Damien George --- tests/misc/sys_settrace_features.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/misc/sys_settrace_features.py b/tests/misc/sys_settrace_features.py index a12304489231f..d315ea69730c0 100644 --- a/tests/misc/sys_settrace_features.py +++ b/tests/misc/sys_settrace_features.py @@ -60,7 +60,8 @@ def trace_tick_handler_bob(frame, event, arg): def trace_tick_handler(frame, event, arg): # Ignore CPython specific helpers. - if frame.f_globals["__name__"].find("importlib") != -1: + frame_name = frame.f_globals["__name__"] + if frame_name.find("importlib") != -1 or frame_name.find("zipimport") != -1: return print("### trace_handler::main event:", event) From 69262a11dcaae6db7ecbd1f4b6179a2ed6195a10 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 14 Dec 2020 13:05:43 +1100 Subject: [PATCH 0487/3533] tools/ci.sh: Put echo of CI path in a separate function. Because the setup functions may print other information which should not be added to the path. Signed-off-by: Damien George --- .github/workflows/ports_esp32.yml | 4 ++-- .github/workflows/ports_esp8266.yml | 2 +- tools/ci.sh | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ports_esp32.yml b/.github/workflows/ports_esp32.yml index b89e462e049fc..041074557e616 100644 --- a/.github/workflows/ports_esp32.yml +++ b/.github/workflows/ports_esp32.yml @@ -18,7 +18,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_esp32_idf3_setup >> $GITHUB_PATH + run: source tools/ci.sh && ci_esp32_idf3_setup && ci_esp32_idf3_path >> $GITHUB_PATH - name: Build env: IDF_PATH: ${{ github.workspace }}/esp-idf @@ -29,7 +29,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_esp32_idf4_setup >> $GITHUB_PATH + run: source tools/ci.sh && ci_esp32_idf4_setup && ci_esp32_idf4_path >> $GITHUB_PATH - name: Build env: IDF_PATH: ${{ github.workspace }}/esp-idf diff --git a/.github/workflows/ports_esp8266.yml b/.github/workflows/ports_esp8266.yml index f6f2fcaf4a80e..f4ce1f8212d87 100644 --- a/.github/workflows/ports_esp8266.yml +++ b/.github/workflows/ports_esp8266.yml @@ -18,6 +18,6 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_esp8266_setup >> $GITHUB_PATH + run: source tools/ci.sh && ci_esp8266_setup && ci_esp8266_path >> $GITHUB_PATH - name: Build run: source tools/ci.sh && ci_esp8266_build diff --git a/tools/ci.sh b/tools/ci.sh index ded9fab922d50..32fccf071d5cb 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -75,6 +75,9 @@ function ci_esp32_idf3_setup { sudo pip3 install pyserial 'pyparsing<2.4' curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar zxf - git clone https://github.com/espressif/esp-idf.git +} + +function ci_esp32_idf3_path { echo $(pwd)/xtensa-esp32-elf/bin } @@ -90,6 +93,9 @@ function ci_esp32_idf4_setup { sudo pip3 install pyserial 'pyparsing<2.4' curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp-2019r2-linux-amd64.tar.gz | tar zxf - git clone https://github.com/espressif/esp-idf.git +} + +function ci_esp32_idf4_path { echo $(pwd)/xtensa-esp32-elf/bin } @@ -108,6 +114,9 @@ function ci_esp8266_setup { sudo pip install pyserial wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz zcat xtensa-lx106-elf-standalone.tar.gz | tar x +} + +function ci_esp8266_path { echo $(pwd)/xtensa-lx106-elf/bin } From ee52f89224ac7fde3794bcac7efa1ee4ee2bff81 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 14 Dec 2020 13:07:00 +1100 Subject: [PATCH 0488/3533] tools/ci.sh: Use pip-install to get latest version of esptool.py. Because the version included in xtensa-lx106-elf-standalone.tar.gz needs Python 2 (and pyserial for Python 2). Signed-off-by: Damien George --- tools/ci.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/ci.sh b/tools/ci.sh index 32fccf071d5cb..1a413ec991920 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -111,9 +111,11 @@ function ci_esp32_idf4_build { # ports/esp8266 function ci_esp8266_setup { - sudo pip install pyserial + sudo pip install pyserial esptool wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz zcat xtensa-lx106-elf-standalone.tar.gz | tar x + # Remove this esptool.py so pip version is used instead + rm xtensa-lx106-elf/bin/esptool.py } function ci_esp8266_path { From 0091041f5a1bbf0a2d9ab7ad3de7169f8cc07cf1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 13 Dec 2020 16:00:39 +1100 Subject: [PATCH 0489/3533] py/modmath: Simplify handling of positional args to reduce code size. As a general pattern, required positional arguments that are not named do not need to be parsed using mp_arg_parse_all(). Signed-off-by: Damien George --- py/modmath.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/py/modmath.c b/py/modmath.c index 3ab3ff334c41b..ac9e0bbc449c6 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -204,17 +204,15 @@ MATH_FUN_1(lgamma, lgamma) #if MICROPY_PY_MATH_ISCLOSE STATIC mp_obj_t mp_math_isclose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_a, ARG_b, ARG_rel_tol, ARG_abs_tol }; + enum { ARG_rel_tol, ARG_abs_tol }; static const mp_arg_t allowed_args[] = { - {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_rel_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_abs_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(0)}}, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - const mp_float_t a = mp_obj_get_float(args[ARG_a].u_obj); - const mp_float_t b = mp_obj_get_float(args[ARG_b].u_obj); + mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + const mp_float_t a = mp_obj_get_float(pos_args[0]); + const mp_float_t b = mp_obj_get_float(pos_args[1]); const mp_float_t rel_tol = args[ARG_rel_tol].u_obj == MP_OBJ_NULL ? (mp_float_t)1e-9 : mp_obj_get_float(args[ARG_rel_tol].u_obj); const mp_float_t abs_tol = mp_obj_get_float(args[ARG_abs_tol].u_obj); From 246b2e016ab4a226cc0ac015f5cc340418f81ca1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 13 Dec 2020 16:15:04 +1100 Subject: [PATCH 0490/3533] py/mkrules.mk: Remove stray vpath and unused -Itmp, add $(Q) for $(AR). Signed-off-by: Damien George --- py/mkrules.mk | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/py/mkrules.mk b/py/mkrules.mk index 3bfe64d7536a1..eb8477cbb1d8b 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -15,7 +15,7 @@ CFLAGS += -DMICROPY_ROM_TEXT_COMPRESSION=1 endif # QSTR generation uses the same CFLAGS, with these modifications. -QSTR_GEN_FLAGS = -DNO_QSTR -I$(BUILD)/tmp +QSTR_GEN_FLAGS = -DNO_QSTR # Note: := to force evalulation immediately. QSTR_GEN_CFLAGS := $(CFLAGS) QSTR_GEN_CFLAGS += $(QSTR_GEN_FLAGS) @@ -76,8 +76,6 @@ vpath %.c . $(TOP) $(USER_C_MODULES) $(BUILD)/%.o: %.c $(call compile_c) -vpath %.c . $(TOP) $(USER_C_MODULES) - vpath %.cpp . $(TOP) $(USER_C_MODULES) $(BUILD)/%.o: %.cpp $(call compile_cxx) @@ -215,7 +213,7 @@ LIBMICROPYTHON = libmicropython.a # tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some # other file to cause needed effect, e.g. relinking with new lib. lib $(LIBMICROPYTHON): $(OBJ) - $(AR) rcs $(LIBMICROPYTHON) $^ + $(Q)$(AR) rcs $(LIBMICROPYTHON) $^ $(LIBMICROPYTHON_EXTRA_CMD) clean: From 1719459c28138be009c8dd41f0e6cb3b942eb2dd Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 13 Dec 2020 16:41:12 +1100 Subject: [PATCH 0491/3533] extmod/modubinascii: Update code, docs for hexlify now CPython has sep. Since CPython 3.8 the optional "sep" argument to hexlify is officially supported, so update comments in the code and the docs to reflect this. Signed-off-by: Damien George --- docs/library/ubinascii.rst | 10 ++++------ extmod/modubinascii.c | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/library/ubinascii.rst b/docs/library/ubinascii.rst index 192d34514b3d9..721b80508e31a 100644 --- a/docs/library/ubinascii.rst +++ b/docs/library/ubinascii.rst @@ -14,13 +14,11 @@ Functions .. function:: hexlify(data, [sep]) - Convert binary data to hexadecimal representation. Returns bytes string. - - .. admonition:: Difference to CPython - :class: attention + Convert the bytes in the *data* object to a hexadecimal representation. + Returns a bytes object. - If additional argument, *sep* is supplied, it is used as a separator - between hexadecimal values. + If the additional argument *sep* is supplied it is used as a separator + between hexadecimal values. .. function:: unhexlify(data) diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index 1d4c72b24b45a..9e4f86fbd2356 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -34,8 +34,8 @@ #if MICROPY_PY_UBINASCII STATIC mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) { - // Second argument is for an extension to allow a separator to be used - // between values. + // First argument is the data to convert. + // Second argument is an optional separator to be used between values. const char *sep = NULL; mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); From dc1fd4df7360d101952426dd0d1574d3971e50f5 Mon Sep 17 00:00:00 2001 From: Oliver Joos Date: Fri, 27 Nov 2020 11:05:39 +0100 Subject: [PATCH 0492/3533] tests/extmod: Add test to try and mount a block device directly. Mounting a bdev directly tries to auto-detect the filesystem and if none is found an OSError(19,) should be raised. The fourth parameter of readblocks() and writeblocks() must be optional to support ports with MICROPY_VFS_FAT=1. Otherwise mounting a bdev may fail because looking for a FATFS will call readblocks() with only 3 parameters. --- tests/extmod/vfs_lfs_mount.py | 26 +++++++++++++++++++------- tests/extmod/vfs_lfs_mount.py.exp | 2 ++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/extmod/vfs_lfs_mount.py b/tests/extmod/vfs_lfs_mount.py index 3d8cec6075c86..bea8a2723ac5b 100644 --- a/tests/extmod/vfs_lfs_mount.py +++ b/tests/extmod/vfs_lfs_mount.py @@ -16,12 +16,12 @@ class RAMBlockDevice: def __init__(self, blocks): self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) - def readblocks(self, block, buf, off): + def readblocks(self, block, buf, off=0): addr = block * self.ERASE_BLOCK_SIZE + off for i in range(len(buf)): buf[i] = self.data[addr + i] - def writeblocks(self, block, buf, off): + def writeblocks(self, block, buf, off=0): addr = block * self.ERASE_BLOCK_SIZE + off for i in range(len(buf)): self.data[addr + i] = buf[i] @@ -35,9 +35,17 @@ def ioctl(self, op, arg): return 0 -def test(bdev, vfs_class): +def test(vfs_class): print("test", vfs_class) + bdev = RAMBlockDevice(30) + + # mount bdev unformatted + try: + uos.mount(bdev, "/lfs") + except Exception as er: + print(repr(er)) + # mkfs vfs_class.mkfs(bdev) @@ -84,12 +92,16 @@ def test(bdev, vfs_class): # umount uos.umount("/lfs") + # mount bdev again + uos.mount(bdev, "/lfs") + + # umount + uos.umount("/lfs") + # clear imported modules usys.modules.clear() -bdev = RAMBlockDevice(30) - # initialise path import usys @@ -98,5 +110,5 @@ def test(bdev, vfs_class): usys.path.append("") # run tests -test(bdev, uos.VfsLfs1) -test(bdev, uos.VfsLfs2) +test(uos.VfsLfs1) +test(uos.VfsLfs2) diff --git a/tests/extmod/vfs_lfs_mount.py.exp b/tests/extmod/vfs_lfs_mount.py.exp index aa654ebe05f12..68561b4807350 100644 --- a/tests/extmod/vfs_lfs_mount.py.exp +++ b/tests/extmod/vfs_lfs_mount.py.exp @@ -1,4 +1,5 @@ test +OSError(19,) hello from lfs package hello from lfs @@ -6,6 +7,7 @@ lfsmod2.py: print("hello from lfs") OSError(30,) test +OSError(19,) hello from lfs package hello from lfs From a13d1b50c93802b9ce6be8dac0fec53545656ef7 Mon Sep 17 00:00:00 2001 From: Oliver Joos Date: Fri, 27 Nov 2020 11:09:16 +0100 Subject: [PATCH 0493/3533] extmod/vfs: Raise OSError(ENODEV) if mounting bdev without a filesystem. This commit prevents uos.mount() from raising an AttributeError. vfs_autodetect() is supposed to return an object that has a "mount" method, so if no filesystem is found it should raise an OSError(ENODEV) and not return the bdev itself which has no "mount" method. --- extmod/vfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index 3cb7af1b434f9..7dca59d351b03 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -191,7 +191,8 @@ STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) { return mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &bdev_obj); #endif - return bdev_obj; + // no filesystem found + mp_raise_OSError(MP_ENODEV); } mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { From 20f8ce19825d1a976f8d61e79e661e67509681bd Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sun, 6 Dec 2020 20:28:21 +0200 Subject: [PATCH 0494/3533] stm32/pyb_can: Add ability to calculate CAN bit timing from baudrate. Calculate the bit timing from baudrate if provided, allowing sample point override. This makes it a lot easier to make CAN work between different MCUs with different clocks, prescalers etc. Tested on F4, F7 and H7 Y/V variants. --- docs/library/pyb.CAN.rst | 7 ++++- ports/stm32/pyb_can.c | 62 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/docs/library/pyb.CAN.rst b/docs/library/pyb.CAN.rst index 8078e29e0cc8d..649bcda108fcc 100644 --- a/docs/library/pyb.CAN.rst +++ b/docs/library/pyb.CAN.rst @@ -49,7 +49,7 @@ Class Methods Methods ------- -.. method:: CAN.init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8, auto_restart=False) +.. method:: CAN.init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8, auto_restart=False, baudrate=0, sample_point=75) Initialise the CAN bus with the given parameters: @@ -67,6 +67,11 @@ Methods - *auto_restart* sets whether the controller will automatically try and restart communications after entering the bus-off state; if this is disabled then :meth:`~CAN.restart()` can be used to leave the bus-off state + - *baudrate* if a baudrate other than 0 is provided, this function will try to automatically + calculate a CAN bit-timing (overriding *prescaler*, *bs1* and *bs2*) that satisfies both + the baudrate and the desired *sample_point*. + - *sample_point* given in a percentage of the bit time, the *sample_point* specifies the position + of the last bit sample with respect to the whole bit time. The default *sample_point* is 75%. The time quanta tq is the basic unit of time for the CAN bus. tq is the CAN prescaler value divided by PCLK1 (the frequency of internal peripheral bus 1); diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index def66481cbdc8..3e55069ab9ac3 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -140,9 +140,41 @@ STATIC void pyb_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki } } +STATIC uint32_t pyb_can_get_source_freq() { + uint32_t can_kern_clk = 0; + + // Find CAN kernel clock + #if defined(STM32H7) + switch (__HAL_RCC_GET_FDCAN_SOURCE()) { + case RCC_FDCANCLKSOURCE_HSE: + can_kern_clk = HSE_VALUE; + break; + case RCC_FDCANCLKSOURCE_PLL: { + PLL1_ClocksTypeDef pll1_clocks; + HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks); + can_kern_clk = pll1_clocks.PLL1_Q_Frequency; + break; + } + case RCC_FDCANCLKSOURCE_PLL2: { + PLL2_ClocksTypeDef pll2_clocks; + HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks); + can_kern_clk = pll2_clocks.PLL2_Q_Frequency; + break; + } + } + #else // F4 and F7 and assume other MCUs too. + // CAN1/CAN2/CAN3 on APB1 use GetPCLK1Freq, alternatively use the following: + // can_kern_clk = ((HSE_VALUE / osc_config.PLL.PLLM ) * osc_config.PLL.PLLN) / + // (osc_config.PLL.PLLQ * clk_init.AHBCLKDivider * clk_init.APB1CLKDivider); + can_kern_clk = HAL_RCC_GetPCLK1Freq(); + #endif + + return can_kern_clk; +} + // init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8) STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart }; + enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart, ARG_baudrate, ARG_sample_point }; static const mp_arg_t allowed_args[] = { { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} }, { MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} }, @@ -151,6 +183,8 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp { MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS1} }, { MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_BS2} }, { MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_sample_point, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 75} }, // 75% sampling point }; // parse args @@ -162,6 +196,32 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp // set the CAN configuration values memset(&self->can, 0, sizeof(self->can)); + // Calculate CAN bit timing from baudrate if provided + if (args[ARG_baudrate].u_int != 0) { + uint32_t baudrate = args[ARG_baudrate].u_int; + uint32_t sampoint = args[ARG_sample_point].u_int; + uint32_t can_kern_clk = pyb_can_get_source_freq(); + bool timing_found = false; + + // The following max values work on all MCUs for classical CAN. + for (int brp = 1; brp < 512 && !timing_found; brp++) { + for (int bs1 = 1; bs1 < 16 && !timing_found; bs1++) { + for (int bs2 = 1; bs2 < 8 && !timing_found; bs2++) { + if ((baudrate == (can_kern_clk / (brp * (1 + bs1 + bs2)))) && + ((sampoint * 10) == (((1 + bs1) * 1000) / (1 + bs1 + bs2)))) { + args[ARG_bs1].u_int = bs1; + args[ARG_bs2].u_int = bs2; + args[ARG_prescaler].u_int = brp; + timing_found = true; + } + } + } + } + if (!timing_found) { + mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("couldn't match baudrate and sample point")); + } + } + // init CAN (if it fails, it's because the port doesn't exist) if (!can_init(self, args[ARG_mode].u_int, args[ARG_prescaler].u_int, args[ARG_sjw].u_int, args[ARG_bs1].u_int, args[ARG_bs2].u_int, args[ARG_auto_restart].u_bool)) { From 32d76e5de66237eba456975095f998434413467a Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 8 Dec 2020 00:22:17 +0200 Subject: [PATCH 0495/3533] stm32/system_stm32: Enable DBGMCU in low-power modes for debug builds. --- ports/stm32/system_stm32.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index 00b7213869eae..2160e0ac994c6 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -425,6 +425,11 @@ void SystemClock_Config(void) { HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, TICK_INT_PRIORITY, 0)); #endif + + #if defined(STM32H7) && !defined(NDEBUG) + // Enable the Debug Module in low-power modes. + DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEPD1 | DBGMCU_CR_DBG_STOPD1 | DBGMCU_CR_DBG_STANDBYD1); + #endif } #endif From b603066bc2d272d05033705922e515d50318e735 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 8 Dec 2020 01:55:50 +0200 Subject: [PATCH 0496/3533] stm32/sdram: Add SDRAM enter/leave self-refresh mode functions. These functions enable SDRAM data retention in stop mode. Example usage, in mpconfigboard.h: #define MICROPY_BOARD_ENTER_STOP sdram_enter_low_power(); #define MICROPY_BOARD_LEAVE_STOP sdram_leave_low_power(); --- ports/stm32/sdram.c | 22 ++++++++++++++++++++-- ports/stm32/sdram.h | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ports/stm32/sdram.c b/ports/stm32/sdram.c index 0aae74e116b16..e8892b57463a4 100644 --- a/ports/stm32/sdram.c +++ b/ports/stm32/sdram.c @@ -49,8 +49,7 @@ #ifdef FMC_SDRAM_BANK -static void sdram_init_seq(SDRAM_HandleTypeDef - *hsdram, FMC_SDRAM_CommandTypeDef *command); +static void sdram_init_seq(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *command); extern void __fatal_error(const char *msg); bool sdram_init(void) { @@ -254,6 +253,25 @@ static void sdram_init_seq(SDRAM_HandleTypeDef #endif } +void sdram_enter_low_power(void) { + // Enter self-refresh mode. + // In self-refresh mode the SDRAM retains data with external clocking. + FMC_SDRAM_DEVICE->SDCMR |= (FMC_SDRAM_CMD_SELFREFRESH_MODE | // Command Mode + FMC_SDRAM_CMD_TARGET_BANK | // Command Target + (0 << 5U) | // Auto Refresh Number -1 + (0 << 9U)); // Mode Register Definition +} + +void sdram_leave_low_power(void) { + // Exit self-refresh mode. + // Self-refresh mode is exited when the device is accessed or the mode bits are + // set to Normal mode, so technically it's not necessary to call this functions. + FMC_SDRAM_DEVICE->SDCMR |= (FMC_SDRAM_CMD_NORMAL_MODE | // Command Mode + FMC_SDRAM_CMD_TARGET_BANK | // Command Target + (0 << 5U) | // Auto Refresh Number - 1 + (0 << 9U)); // Mode Register Definition +} + bool sdram_test(bool fast) { uint8_t const pattern = 0xaa; uint8_t const antipattern = 0x55; diff --git a/ports/stm32/sdram.h b/ports/stm32/sdram.h index 9b4b4fb83e202..773a30802f87f 100644 --- a/ports/stm32/sdram.h +++ b/ports/stm32/sdram.h @@ -11,5 +11,7 @@ bool sdram_init(void); void *sdram_start(void); void *sdram_end(void); +void sdram_enter_low_power(void); +void sdram_leave_low_power(void); bool sdram_test(bool fast); #endif // __SDRAM_H__ From 80883a82c0dd40178933eb8a46b2877cb72725b2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Dec 2020 12:23:09 +1100 Subject: [PATCH 0497/3533] stm32/adc: Deselect VBAT after reading to prevent battery drain. Signed-off-by: Damien George --- ports/stm32/adc.c | 8 ++--- ports/stm32/adc.h | 31 ++++++++++++++++++++ ports/stm32/boards/stm32f4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f7xx_hal_conf_base.h | 1 + ports/stm32/machine_adc.c | 8 ++++- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 3793ef5ff28a2..5fa22d7c78d3a 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -380,16 +380,14 @@ STATIC uint32_t adc_config_and_read_channel(ADC_HandleTypeDef *adcHandle, uint32 adc_config_channel(adcHandle, channel); uint32_t raw_value = adc_read_channel(adcHandle); - #if defined(STM32F4) || defined(STM32F7) // ST docs say that (at least on STM32F42x and STM32F43x), VBATE must // be disabled when TSVREFE is enabled for TEMPSENSOR and VREFINT // conversions to work. VBATE is enabled by the above call to read // the channel, and here we disable VBATE so a subsequent call for // TEMPSENSOR or VREFINT works correctly. - if (channel == ADC_CHANNEL_VBAT) { - ADC->CCR &= ~ADC_CCR_VBATE; - } - #endif + // It's also good to disable the VBAT switch to prevent battery drain, + // so disable it for all MCUs. + adc_deselect_vbat(adcHandle->Instance, channel); return raw_value; } diff --git a/ports/stm32/adc.h b/ports/stm32/adc.h index 4ae6022bc3362..6f2e61e099449 100644 --- a/ports/stm32/adc.h +++ b/ports/stm32/adc.h @@ -29,4 +29,35 @@ extern const mp_obj_type_t pyb_adc_type; extern const mp_obj_type_t pyb_adc_all_type; +#if defined(ADC_CHANNEL_VBAT) + +static inline void adc_deselect_vbat(ADC_TypeDef *adc, uint32_t channel) { + (void)adc; + + if (channel == ADC_CHANNEL_VBAT) { + ADC_Common_TypeDef *adc_common; + + #if defined(STM32F0) || defined(STM32WB) + adc_common = ADC1_COMMON; + #elif defined(STM32F4) || defined(STM32L4) + adc_common = ADC_COMMON_REGISTER(0); + #elif defined(STM32F7) + adc_common = ADC123_COMMON; + #elif defined(STM32H7) + adc_common = adc == ADC3 ? ADC3_COMMON : ADC12_COMMON; + #endif + + adc_common->CCR &= ~LL_ADC_PATH_INTERNAL_VBAT; + } +} + +#else + +static inline void adc_deselect_vbat(ADC_TypeDef *adc, uint32_t channel) { + (void)adc; + (void)channel; +} + +#endif + #endif // MICROPY_INCLUDED_STM32_ADC_H diff --git a/ports/stm32/boards/stm32f4xx_hal_conf_base.h b/ports/stm32/boards/stm32f4xx_hal_conf_base.h index 8d8bb8f4eeccc..91f064835e03d 100644 --- a/ports/stm32/boards/stm32f4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f4xx_hal_conf_base.h @@ -53,6 +53,7 @@ #include "stm32f4xx_hal_uart.h" #include "stm32f4xx_hal_usart.h" #include "stm32f4xx_hal_wwdg.h" +#include "stm32f4xx_ll_adc.h" #include "stm32f4xx_ll_rtc.h" // Enable various HAL modules diff --git a/ports/stm32/boards/stm32f7xx_hal_conf_base.h b/ports/stm32/boards/stm32f7xx_hal_conf_base.h index 83a144f8fe7f0..1a3fca3ac86a8 100644 --- a/ports/stm32/boards/stm32f7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f7xx_hal_conf_base.h @@ -53,6 +53,7 @@ #include "stm32f7xx_hal_uart.h" #include "stm32f7xx_hal_usart.h" #include "stm32f7xx_hal_wwdg.h" +#include "stm32f7xx_ll_adc.h" #include "stm32f7xx_ll_rtc.h" // Enable various HAL modules diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index 9c20f0f954989..d9e5a64da5533 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -26,6 +26,7 @@ #include "py/runtime.h" #include "py/mphal.h" +#include "adc.h" #if defined(STM32F0) || defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) #define ADC_V2 (1) @@ -335,10 +336,15 @@ STATIC uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint return 0xffff; } + // Select, configure and read the channel. adc_config_channel(adc, channel, sample_time); uint32_t raw = adc_read_channel(adc); + + // If VBAT was sampled then deselect it to prevent battery drain. + adc_deselect_vbat(adc, channel); + + // Scale raw reading to 16 bit value using a Taylor expansion (for bits <= 16). uint32_t bits = adc_get_bits(adc); - // Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16) #if defined(STM32H7) if (bits < 8) { // For 6 and 7 bits From 061cb1a73a4ecbf69a4e036053664b4f84754b34 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Dec 2020 15:09:53 +1100 Subject: [PATCH 0498/3533] stm32/main: Do extended readblocks call when auto-detecting littlefs. When littlefs is enabled extended reading must be supported, and using this function to read the first block for auto-detection is more efficient (a smaller read) and does not require a cached SPI-flash read. Signed-off-by: Damien George --- ports/stm32/main.c | 13 +++++++------ ports/stm32/storage.c | 7 +++++++ ports/stm32/storage.h | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index d00c2ec713f61..fb10b96501e1d 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -164,17 +164,18 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { // Default block device to entire flash storage mp_obj_t bdev = MP_OBJ_FROM_PTR(&pyb_flash_obj); + int ret; + #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 // Try to detect the block device used for the main filesystem, based on the first block - uint8_t buf[FLASH_BLOCK_SIZE]; - storage_read_blocks(buf, FLASH_PART1_START_BLOCK, 1); - + uint8_t buf[64]; + ret = storage_readblocks_ext(buf, 0, 0, sizeof(buf)); mp_int_t len = -1; #if MICROPY_VFS_LFS1 - if (memcmp(&buf[40], "littlefs", 8) == 0) { + if (ret == 0 && memcmp(&buf[40], "littlefs", 8) == 0) { // LFS1 lfs1_superblock_t *superblock = (void *)&buf[12]; uint32_t block_size = lfs1_fromle32(superblock->d.block_size); @@ -184,7 +185,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { #endif #if MICROPY_VFS_LFS2 - if (memcmp(&buf[8], "littlefs", 8) == 0) { + if (ret == 0 && memcmp(&buf[8], "littlefs", 8) == 0) { // LFS2 lfs2_superblock_t *superblock = (void *)&buf[20]; uint32_t block_size = lfs2_fromle32(superblock->block_size); @@ -203,7 +204,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { // Try to mount the flash on "/flash" and chdir to it for the boot-up directory. mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); - int ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point); + ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point); if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) { // No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs), diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index c8805d682986b..6581860ff3ffb 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -250,6 +250,13 @@ int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl #define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE) #endif +#if defined(MICROPY_HW_BDEV_READBLOCKS_EXT) +// Size of blocks is PYB_FLASH_NATIVE_BLOCK_SIZE +int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) { + return MICROPY_HW_BDEV_READBLOCKS_EXT(dest, block, offset, len); +} +#endif + typedef struct _pyb_flash_obj_t { mp_obj_base_t base; uint32_t start; // in bytes diff --git a/ports/stm32/storage.h b/ports/stm32/storage.h index 490fc4a09b8ac..73058aad6b393 100644 --- a/ports/stm32/storage.h +++ b/ports/stm32/storage.h @@ -50,6 +50,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block); // these return 0 on success, negative errno on error int storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks); int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks); +int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len); int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg); bool flash_bdev_readblock(uint8_t *dest, uint32_t block); From e43a74a4db484ac1bfef191a8aa19b58b519efc6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Dec 2020 16:59:54 +1100 Subject: [PATCH 0499/3533] drivers/memory/spiflash: Add MICROPY_HW_SPIFLASH_ENABLE_CACHE option. This only needs to be enabled if a board uses FAT FS on external SPI flash. When disabled (and using external SPI flash) 4k of RAM can be saved. Signed-off-by: Damien George --- drivers/memory/spiflash.c | 4 ++++ drivers/memory/spiflash.h | 6 ++++++ ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 1 + ports/stm32/boards/STM32F769DISC/mpconfigboard.h | 1 + ports/stm32/boards/STM32L476DISC/mpconfigboard.h | 1 + ports/stm32/mpconfigboard_common.h | 8 ++++++++ ports/stm32/spibdev.c | 6 ++++++ 7 files changed, 27 insertions(+) diff --git a/drivers/memory/spiflash.c b/drivers/memory/spiflash.c index e870d39f5f9cb..bb15bd6252519 100644 --- a/drivers/memory/spiflash.c +++ b/drivers/memory/spiflash.c @@ -287,6 +287,8 @@ int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint /******************************************************************************/ // Interface functions that use the cache +#if MICROPY_HW_SPIFLASH_ENABLE_CACHE + void mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { if (len == 0) { return; @@ -509,3 +511,5 @@ int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, con mp_spiflash_release_bus(self); return 0; } + +#endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE diff --git a/drivers/memory/spiflash.h b/drivers/memory/spiflash.h index 96dfdeeab66bf..c4162ff21cdc6 100644 --- a/drivers/memory/spiflash.h +++ b/drivers/memory/spiflash.h @@ -38,6 +38,7 @@ enum { struct _mp_spiflash_t; +#if MICROPY_HW_SPIFLASH_ENABLE_CACHE // A cache must be provided by the user in the config struct. The same cache // struct can be shared by multiple SPI flash instances. typedef struct _mp_spiflash_cache_t { @@ -45,6 +46,7 @@ typedef struct _mp_spiflash_cache_t { struct _mp_spiflash_t *user; // current user of buf, for shared use uint32_t block; // current block stored in buf; 0xffffffff if invalid } mp_spiflash_cache_t; +#endif typedef struct _mp_spiflash_config_t { uint32_t bus_kind; @@ -59,7 +61,9 @@ typedef struct _mp_spiflash_config_t { const mp_qspi_proto_t *proto; } u_qspi; } bus; + #if MICROPY_HW_SPIFLASH_ENABLE_CACHE mp_spiflash_cache_t *cache; // can be NULL if cache functions not used + #endif } mp_spiflash_config_t; typedef struct _mp_spiflash_t { @@ -75,9 +79,11 @@ int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr); void mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest); int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src); +#if MICROPY_HW_SPIFLASH_ENABLE_CACHE // These functions use the cache (which must already be configured) void mp_spiflash_cache_flush(mp_spiflash_t *self); void mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest); int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src); +#endif #endif // MICROPY_INCLUDED_DRIVERS_MEMORY_SPIFLASH_H diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index f6e130eb57eaf..29164ac11c909 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -77,6 +77,7 @@ void board_sleep(int value); // SPI flash #1, block device config extern const struct _mp_spiflash_config_t spiflash_config; extern struct _spi_bdev_t spi_bdev; +#define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) #define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ diff --git a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h index e1fe93bb0ed44..843b987cee714 100644 --- a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h @@ -40,6 +40,7 @@ extern const struct _mp_spiflash_config_t spiflash_config; extern struct _spi_bdev_t spi_bdev; #if !USE_QSPI_XIP #define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) +#define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) #define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ (op) == BDEV_IOCTL_NUM_BLOCKS ? ((1 << MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2) / 8 / FLASH_BLOCK_SIZE) : \ (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ diff --git a/ports/stm32/boards/STM32L476DISC/mpconfigboard.h b/ports/stm32/boards/STM32L476DISC/mpconfigboard.h index 2831187531178..ad62f761e55f3 100644 --- a/ports/stm32/boards/STM32L476DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32L476DISC/mpconfigboard.h @@ -22,6 +22,7 @@ void STM32L476DISC_board_early_init(void); // block device config for SPI flash extern const struct _mp_spiflash_config_t spiflash_config; extern struct _spi_bdev_t spi_bdev; +#define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) #define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 8890abc59bbe1..60fbc35fcbae0 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -286,6 +286,14 @@ #define MICROPY_HW_BDEV_WRITEBLOCK flash_bdev_writeblock #endif +// Whether to enable caching for external SPI flash, to allow block writes that are +// smaller than the native page-erase size of the SPI flash, eg when FAT FS is used. +// Enabling this enables spi_bdev_readblocks() and spi_bdev_writeblocks() functions, +// and requires a valid mp_spiflash_config_t.cache pointer. +#ifndef MICROPY_HW_SPIFLASH_ENABLE_CACHE +#define MICROPY_HW_SPIFLASH_ENABLE_CACHE (0) +#endif + // Enable the storage sub-system if a block device is defined #if defined(MICROPY_HW_BDEV_IOCTL) #define MICROPY_HW_ENABLE_STORAGE (1) diff --git a/ports/stm32/spibdev.c b/ports/stm32/spibdev.c index 05c8819a7737f..5090b43b1cb92 100644 --- a/ports/stm32/spibdev.c +++ b/ports/stm32/spibdev.c @@ -41,19 +41,23 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) { return 0; case BDEV_IOCTL_IRQ_HANDLER: + #if MICROPY_HW_SPIFLASH_ENABLE_CACHE if ((bdev->spiflash.flags & 1) && HAL_GetTick() - bdev->flash_tick_counter_last_write >= 1000) { mp_spiflash_cache_flush(&bdev->spiflash); led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off } + #endif return 0; case BDEV_IOCTL_SYNC: + #if MICROPY_HW_SPIFLASH_ENABLE_CACHE if (bdev->spiflash.flags & 1) { uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access mp_spiflash_cache_flush(&bdev->spiflash); led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off restore_irq_pri(basepri); } + #endif return 0; case BDEV_IOCTL_BLOCK_ERASE: { @@ -66,6 +70,7 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) { return -MP_EINVAL; } +#if MICROPY_HW_SPIFLASH_ENABLE_CACHE int spi_bdev_readblocks(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access mp_spiflash_cached_read(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, dest); @@ -85,6 +90,7 @@ int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_nu return ret; } +#endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE int spi_bdev_readblocks_raw(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes) { uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access From e715a8fb9bf9c572b805aca4682048183c6eb97c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Dec 2020 21:59:58 +1100 Subject: [PATCH 0500/3533] stm32/boards/PYBD_SF2: Disable SPIFLASH_ENABLE_CACHE for mboot builds. Mboot builds do not use the external SPI flash in caching mode, and explicitly disabling it saves RAM and a small bit of flash. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/bdev.c | 6 ++++++ ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/ports/stm32/boards/PYBD_SF2/bdev.c b/ports/stm32/boards/PYBD_SF2/bdev.c index 6c5ff721ec707..d9bec93c27e40 100644 --- a/ports/stm32/boards/PYBD_SF2/bdev.c +++ b/ports/stm32/boards/PYBD_SF2/bdev.c @@ -27,8 +27,10 @@ #include "storage.h" #include "qspi.h" +#if MICROPY_HW_SPIFLASH_ENABLE_CACHE // Shared cache for first and second SPI block devices STATIC mp_spiflash_cache_t spi_bdev_cache; +#endif // First external SPI flash uses software QSPI interface @@ -45,7 +47,9 @@ const mp_spiflash_config_t spiflash_config = { .bus_kind = MP_SPIFLASH_BUS_QSPI, .bus.u_qspi.data = (void*)&soft_qspi_bus, .bus.u_qspi.proto = &mp_soft_qspi_proto, + #if MICROPY_HW_SPIFLASH_ENABLE_CACHE .cache = &spi_bdev_cache, + #endif }; spi_bdev_t spi_bdev; @@ -56,7 +60,9 @@ const mp_spiflash_config_t spiflash2_config = { .bus_kind = MP_SPIFLASH_BUS_QSPI, .bus.u_qspi.data = NULL, .bus.u_qspi.proto = &qspi_proto, + #if MICROPY_HW_SPIFLASH_ENABLE_CACHE .cache = &spi_bdev_cache, + #endif }; spi_bdev_t spi_bdev2; diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 29164ac11c909..45d968f353609 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -77,7 +77,9 @@ void board_sleep(int value); // SPI flash #1, block device config extern const struct _mp_spiflash_config_t spiflash_config; extern struct _spi_bdev_t spi_bdev; +#if !BUILDING_MBOOT #define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) +#endif #define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ From 505a1853b9490246fbb95850ad8dae6d1b49d002 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 18 Dec 2020 13:48:26 +1100 Subject: [PATCH 0501/3533] teensy: Fix build errors and warnings and enable -Werror. Changes are: - Remove include of stm32's adc.h because it was recently changed and is no longer compatible with teensy (and not used anyway). - Remove define of __disable_irq in mpconfigport.h because it was clashing with an equivalent definition in core/mk20dx128.h. - Add -Werror to CFLAGS, and change -std=gnu99 to -std=c99. Signed-off-by: Damien George --- ports/teensy/Makefile | 2 +- ports/teensy/modpyb.c | 1 - ports/teensy/mpconfigport.h | 6 +----- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index f2623eabd3dc3..d3978718e0095 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -38,7 +38,7 @@ INC += -I$(TOP)/ports/stm32 INC += -I$(BUILD) INC += -Icore -CFLAGS = $(INC) -Wall -Wpointer-arith -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) LDFLAGS = -nostdlib -T mk20dx256.ld -msoft-float -mfloat-abi=soft ifeq ($(USE_ARDUINO_TOOLCHAIN),1) diff --git a/ports/teensy/modpyb.c b/ports/teensy/modpyb.c index f4384a885426e..6671b3abdce0e 100644 --- a/ports/teensy/modpyb.c +++ b/ports/teensy/modpyb.c @@ -44,7 +44,6 @@ #include "usrsw.h" #include "rng.h" #include "uart.h" -#include "adc.h" #include "storage.h" #include "sdcard.h" #include "accel.h" diff --git a/ports/teensy/mpconfigport.h b/ports/teensy/mpconfigport.h index 24269c3b981e6..c00da5ed9e498 100644 --- a/ports/teensy/mpconfigport.h +++ b/ports/teensy/mpconfigport.h @@ -72,10 +72,6 @@ typedef long mp_off_t; // value from disable_irq back to enable_irq. If you really need // to know the machine-specific values, see irq.h. -#ifndef __disable_irq -#define __disable_irq() __asm__ volatile ("CPSID i"); -#endif - __attribute__((always_inline)) static inline uint32_t __get_PRIMASK(void) { uint32_t result; __asm volatile ("MRS %0, primask" : "=r" (result)); @@ -92,7 +88,7 @@ __attribute__((always_inline)) static inline void enable_irq(mp_uint_t state) { __attribute__((always_inline)) static inline mp_uint_t disable_irq(void) { mp_uint_t state = __get_PRIMASK(); - __disable_irq(); + __asm__ volatile ("CPSID i"); return state; } From 108183fcc01f722d17e373f91f392a2a60ac787a Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 15 Dec 2020 11:54:34 +0100 Subject: [PATCH 0502/3533] tests/misc/sys_settrace: Make test output independent of invoked path. The original logic of reducing a full path to a relative one assumes "tests/misc" is in the filename which is limited in usage: it never works for CPython on Windows since that will use a backslash as path separator, and also won't work when the filename is a path not relative to the tests directory which happens for example in the common case of running "./run-tests -d misc". Fix all cases by printing only the bare filename, which requires them all to start with sys_settrace_ hence the renaming. --- tests/misc/sys_settrace_features.py | 8 +- tests/misc/sys_settrace_generator.py | 4 +- tests/misc/sys_settrace_generator.py.exp | 276 +++++++++--------- tests/misc/sys_settrace_loop.py | 4 +- tests/misc/sys_settrace_loop.py.exp | 92 +++--- ...ace_generic.py => sys_settrace_generic.py} | 6 +- ...e_importme.py => sys_settrace_importme.py} | 0 7 files changed, 195 insertions(+), 195 deletions(-) rename tests/misc/sys_settrace_subdir/{trace_generic.py => sys_settrace_generic.py} (91%) rename tests/misc/sys_settrace_subdir/{trace_importme.py => sys_settrace_importme.py} (100%) diff --git a/tests/misc/sys_settrace_features.py b/tests/misc/sys_settrace_features.py index d315ea69730c0..e1b1a059dad6f 100644 --- a/tests/misc/sys_settrace_features.py +++ b/tests/misc/sys_settrace_features.py @@ -20,8 +20,8 @@ def print_stacktrace(frame, level=0): " ", frame.f_globals["__name__"], frame.f_code.co_name, - # reduce full path to some pseudo-relative - "misc" + "".join(frame.f_code.co_filename.split("tests/misc")[-1:]), + # Keep just the filename. + "sys_settrace_" + frame.f_code.co_filename.split("sys_settrace_")[-1], frame.f_lineno, ) ) @@ -95,9 +95,9 @@ def do_tests(): print("Who loves the sun?") print("Not every-", factorial(3)) - from sys_settrace_subdir import trace_generic + from sys_settrace_subdir import sys_settrace_generic - trace_generic.run_tests() + sys_settrace_generic.run_tests() return diff --git a/tests/misc/sys_settrace_generator.py b/tests/misc/sys_settrace_generator.py index 4ace0f50e81b5..43065df4ae9be 100644 --- a/tests/misc/sys_settrace_generator.py +++ b/tests/misc/sys_settrace_generator.py @@ -17,8 +17,8 @@ def print_stacktrace(frame, level=0): " ", frame.f_globals["__name__"], frame.f_code.co_name, - # reduce full path to some pseudo-relative - "misc" + "".join(frame.f_code.co_filename.split("tests/misc")[-1:]), + # Keep just the filename. + "sys_settrace_" + frame.f_code.co_filename.split("sys_settrace_")[-1], frame.f_lineno, ) ) diff --git a/tests/misc/sys_settrace_generator.py.exp b/tests/misc/sys_settrace_generator.py.exp index de9d0bf1c36a6..a83450afe380c 100644 --- a/tests/misc/sys_settrace_generator.py.exp +++ b/tests/misc/sys_settrace_generator.py.exp @@ -1,195 +1,195 @@ ### trace_handler::main event: call - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:41 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:41 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:42 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:42 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:48 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:49 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:49 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:50 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:50 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:52 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:52 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:42 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:52 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:42 + 1: @__main__:test_generator => sys_settrace_generator.py:52 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:52 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:52 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:52 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:52 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:56 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:46 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:46 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:46 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:46 + 1: @__main__:test_generator => sys_settrace_generator.py:56 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: exception - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:56 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:58 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:58 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:59 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:59 + 1: @__main__: => sys_settrace_generator.py:69 test_generator 7 8 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:61 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:61 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:62 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:62 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:42 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:42 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:64 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:64 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:43 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:43 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:64 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:64 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:44 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:44 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:64 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:64 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:63 + 1: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: call - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:45 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:45 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:46 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:46 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: return - 0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:46 - 1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:63 - 2: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:make_gen => sys_settrace_generator.py:46 + 1: @__main__:test_generator => sys_settrace_generator.py:63 + 2: @__main__: => sys_settrace_generator.py:69 ### trace_handler::main event: line - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:65 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:65 + 1: @__main__: => sys_settrace_generator.py:69 7 ### trace_handler::main event: return - 0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:65 - 1: @__main__: => miscmisc/sys_settrace_generator.py:69 + 0: @__main__:test_generator => sys_settrace_generator.py:65 + 1: @__main__: => sys_settrace_generator.py:69 Total traces executed: 54 diff --git a/tests/misc/sys_settrace_loop.py b/tests/misc/sys_settrace_loop.py index 06d0dc17bf77b..1186bd91a0726 100644 --- a/tests/misc/sys_settrace_loop.py +++ b/tests/misc/sys_settrace_loop.py @@ -17,8 +17,8 @@ def print_stacktrace(frame, level=0): " ", frame.f_globals["__name__"], frame.f_code.co_name, - # reduce full path to some pseudo-relative - "misc" + "".join(frame.f_code.co_filename.split("tests/misc")[-1:]), + # Keep just the filename. + "sys_settrace_" + frame.f_code.co_filename.split("sys_settrace_")[-1], frame.f_lineno, ) ) diff --git a/tests/misc/sys_settrace_loop.py.exp b/tests/misc/sys_settrace_loop.py.exp index f56f98fae01f3..ff9ef577c550c 100644 --- a/tests/misc/sys_settrace_loop.py.exp +++ b/tests/misc/sys_settrace_loop.py.exp @@ -1,72 +1,72 @@ ### trace_handler::main event: call - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:41 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:41 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:43 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:43 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:44 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:45 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:44 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:45 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:44 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:45 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:44 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:45 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:46 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:46 + 1: @__main__: => sys_settrace_loop.py:58 test_for_loop 3 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:49 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:49 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:50 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:50 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:51 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:51 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:53 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:53 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:52 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:52 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:53 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:53 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:52 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:52 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:53 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:53 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:52 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:52 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:53 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:53 + 1: @__main__: => sys_settrace_loop.py:58 ### trace_handler::main event: line - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:54 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:54 + 1: @__main__: => sys_settrace_loop.py:58 test_while_loop 3 ### trace_handler::main event: return - 0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:54 - 1: @__main__: => miscmisc/sys_settrace_loop.py:58 + 0: @__main__:test_loop => sys_settrace_loop.py:54 + 1: @__main__: => sys_settrace_loop.py:58 Total traces executed: 23 diff --git a/tests/misc/sys_settrace_subdir/trace_generic.py b/tests/misc/sys_settrace_subdir/sys_settrace_generic.py similarity index 91% rename from tests/misc/sys_settrace_subdir/trace_generic.py rename to tests/misc/sys_settrace_subdir/sys_settrace_generic.py index 111a9d19ff30d..a60ca955d7c7c 100644 --- a/tests/misc/sys_settrace_subdir/trace_generic.py +++ b/tests/misc/sys_settrace_subdir/sys_settrace_generic.py @@ -41,10 +41,10 @@ def test_lambda(): # import def test_import(): - from sys_settrace_subdir import trace_importme + from sys_settrace_subdir import sys_settrace_importme - trace_importme.dummy() - trace_importme.saysomething() + sys_settrace_importme.dummy() + sys_settrace_importme.saysomething() # class diff --git a/tests/misc/sys_settrace_subdir/trace_importme.py b/tests/misc/sys_settrace_subdir/sys_settrace_importme.py similarity index 100% rename from tests/misc/sys_settrace_subdir/trace_importme.py rename to tests/misc/sys_settrace_subdir/sys_settrace_importme.py From 069557edef84a4f37ad52ae93838129da8e5fe77 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 15 Dec 2020 12:03:42 +0100 Subject: [PATCH 0503/3533] tests/misc/sys_settrace_features.py: Fix running with non-dflt encoding. Notably git-cmd which comes with git installations on Windows alters the encoding resulting in CPython tracing encodings/cp1252.py calls. --- tests/misc/sys_settrace_features.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/misc/sys_settrace_features.py b/tests/misc/sys_settrace_features.py index e1b1a059dad6f..8a38b7534f7df 100644 --- a/tests/misc/sys_settrace_features.py +++ b/tests/misc/sys_settrace_features.py @@ -60,8 +60,9 @@ def trace_tick_handler_bob(frame, event, arg): def trace_tick_handler(frame, event, arg): # Ignore CPython specific helpers. + to_ignore = ["importlib", "zipimport", "encodings"] frame_name = frame.f_globals["__name__"] - if frame_name.find("importlib") != -1 or frame_name.find("zipimport") != -1: + if any(name in frame_name for name in to_ignore): return print("### trace_handler::main event:", event) From f42a190247661e1e5aa18e0ce93c627fd7676d8e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 21 Dec 2020 17:31:12 +1100 Subject: [PATCH 0504/3533] extmod/nimble: Reset NimBLE BSS in mp_bluetooth_init. Without this fix, each time service registration happened it would do an increasingly large malloc() for service state. See https://github.com/apache/mynewt-nimble/issues/896. --- extmod/nimble/modbluetooth_nimble.c | 35 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 613e325a953f5..843886d009d12 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -476,11 +476,27 @@ void mp_bluetooth_nimble_port_shutdown(void) { #endif // !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY +void nimble_reset_gatts_bss(void) { + // NimBLE assumes that service registration only ever happens once, so + // we need to reset service registration state from a previous stack startup. + // These variables are defined in ble_hs.c and are only ever incremented + // (during service registration) and never reset. + // See https://github.com/apache/mynewt-nimble/issues/896 + extern uint16_t ble_hs_max_attrs; + extern uint16_t ble_hs_max_services; + extern uint16_t ble_hs_max_client_configs; + ble_hs_max_attrs = 0; + ble_hs_max_services = 0; + ble_hs_max_client_configs = 0; +} + int mp_bluetooth_init(void) { DEBUG_printf("mp_bluetooth_init\n"); // Clean up if necessary. mp_bluetooth_deinit(); + nimble_reset_gatts_bss(); + mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STARTING; ble_hs_cfg.reset_cb = reset_cb; @@ -775,6 +791,15 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } + + if (append) { + // Don't support append yet (modbluetooth.c doesn't support it yet anyway). + // TODO: This should be possible with NimBLE. + return MP_EOPNOTSUPP; + } + + nimble_reset_gatts_bss(); + int ret = ble_gatts_reset(); if (ret != 0) { return ble_hs_err_to_errno(ret); @@ -787,13 +812,11 @@ int mp_bluetooth_gatts_register_service_begin(bool append) { ble_svc_gap_init(); ble_svc_gatt_init(); - if (!append) { - // Unref any previous service definitions. - for (size_t i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) { - MP_STATE_PORT(bluetooth_nimble_root_pointers)->services[i] = NULL; - } - MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0; + // Unref any previous service definitions. + for (size_t i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) { + MP_STATE_PORT(bluetooth_nimble_root_pointers)->services[i] = NULL; } + MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0; return 0; } From f7aafc0628f2008d015b32b0c0253a13f748d436 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 22 Dec 2020 13:06:16 +1100 Subject: [PATCH 0505/3533] extmod/nimble: Don't assert on save-IRK failure. --- extmod/nimble/modbluetooth_nimble.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 843886d009d12..9ceeab7d7b6c6 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -227,7 +227,9 @@ STATIC int load_irk(void) { } DEBUG_printf("load_irk: Saving new IRK.\n"); if (!mp_bluetooth_gap_on_set_secret(SECRET_TYPE_OUR_IRK, key, sizeof(key), rand_irk, 16)) { - return BLE_HS_EINVAL; + // Code that doesn't implement pairing/bonding won't support set/get secret. + // So they'll just get the default fixed IRK. + return 0; } DEBUG_printf("load_irk: Applying new IRK.\n"); rc = ble_hs_pvcy_set_our_irk(rand_irk); From 0ce69486534eca545e5a86f9696047956b5c874e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jan 2021 14:56:57 +1100 Subject: [PATCH 0506/3533] lib/libhydrogen: Add new libhydrogen submodule. This library is a small and easy-to-use cryptographic library which is well suited to embedded systems. Signed-off-by: Damien George --- .gitmodules | 3 +++ lib/libhydrogen | 1 + 2 files changed, 4 insertions(+) create mode 160000 lib/libhydrogen diff --git a/.gitmodules b/.gitmodules index c152e9e7747fa..f4785955af41b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -36,3 +36,6 @@ [submodule "lib/nxp_driver"] path = lib/nxp_driver url = https://github.com/hathach/nxp_driver.git +[submodule "lib/libhydrogen"] + path = lib/libhydrogen + url = https://github.com/jedisct1/libhydrogen.git diff --git a/lib/libhydrogen b/lib/libhydrogen new file mode 160000 index 0000000000000..5c5d513093075 --- /dev/null +++ b/lib/libhydrogen @@ -0,0 +1 @@ +Subproject commit 5c5d513093075f7245ea522101b17c50aa579af2 From 09e67de32739c032bf357e253ae5a783bd41dd7b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jan 2021 02:55:40 +1100 Subject: [PATCH 0507/3533] stm32/mboot/gzstream: Fix lost data decompressing final part of file. Prior to this fix, the final piece of data in a compressed file may have been lost when decompressing. Signed-off-by: Damien George --- ports/stm32/mboot/gzstream.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ports/stm32/mboot/gzstream.c b/ports/stm32/mboot/gzstream.c index 20ba33045f8d8..4f8a4a40cc2dc 100644 --- a/ports/stm32/mboot/gzstream.c +++ b/ports/stm32/mboot/gzstream.c @@ -38,7 +38,7 @@ typedef struct _gz_stream_t { void *stream_data; stream_read_t stream_read; - TINF_DATA tinf; + struct uzlib_uncomp tinf; uint8_t buf[512]; uint8_t dict[DICT_SIZE]; } gz_stream_t; @@ -66,7 +66,7 @@ int gz_stream_init(void *stream_data, stream_read_t stream_read) { gz_stream.stream_read = stream_read; memset(&gz_stream.tinf, 0, sizeof(gz_stream.tinf)); - gz_stream.tinf.readSource = gz_stream_read_src; + gz_stream.tinf.source_read_cb = gz_stream_read_src; int st = uzlib_gzip_parse_header(&gz_stream.tinf); if (st != TINF_OK) { @@ -79,15 +79,21 @@ int gz_stream_init(void *stream_data, stream_read_t stream_read) { } int gz_stream_read(size_t len, uint8_t *buf) { + if (gz_stream.tinf.source == NULL && gz_stream.tinf.source_read_cb == NULL) { + // End of stream. + return 0; + } gz_stream.tinf.dest = buf; gz_stream.tinf.dest_limit = buf + len; int st = uzlib_uncompress_chksum(&gz_stream.tinf); - if (st == TINF_DONE) { - return 0; - } if (st < 0) { return st; } + if (st == TINF_DONE) { + // Indicate end-of-stream for subsequent calls. + gz_stream.tinf.source = NULL; + gz_stream.tinf.source_read_cb = NULL; + } return gz_stream.tinf.dest - buf; } From c6f334272a34e007462347f067013944e57bfe33 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jan 2021 03:00:40 +1100 Subject: [PATCH 0508/3533] stm32/mboot: Add support for signed and encrypted firmware updates. This commit adds support to stm32's mboot for signe, encrypted and compressed DFU updates. It is based on inital work done by Andrew Leech. The feature is enabled by setting MBOOT_ENABLE_PACKING to 1 in the board's mpconfigboard.mk file, and by providing a header file in the board folder (usually called mboot_keys.h) with a set of signing and encryption keys (which can be generated by mboot_pack_dfu.py). The signing and encryption is provided by libhydrogen. Compression is provided by uzlib. Enabling packing costs about 3k of flash. The included mboot_pack_dfu.py script converts a .dfu file to a .pack.dfu file which can be subsequently deployed to a board with mboot in packing mode. This .pack.dfu file is created as follows: - the firmware from the original .dfu is split into chunks (so the decryption can fit in RAM) - each chunk is compressed, encrypted, a header added, then signed - a special final chunk is added with a signature of the entire firmware - all chunks are concatenated to make the final .pack.dfu file The .pack.dfu file can be deployed over USB or from the internal filesystem on the device (if MBOOT_FSLOAD is enabled). See #5267 and #5309 for additional discussion. Signed-off-by: Damien George --- ports/stm32/Makefile | 24 ++- ports/stm32/mboot/Makefile | 16 ++ ports/stm32/mboot/Particle.h | 10 + ports/stm32/mboot/README.md | 28 +++ ports/stm32/mboot/dfu.h | 10 +- ports/stm32/mboot/fsload.c | 45 ++++- ports/stm32/mboot/fwupdate.py | 7 +- ports/stm32/mboot/gzstream.c | 16 +- ports/stm32/mboot/gzstream.h | 3 +- ports/stm32/mboot/main.c | 68 ++++++- ports/stm32/mboot/mboot.h | 7 + ports/stm32/mboot/mboot_pack_dfu.py | 260 +++++++++++++++++++++++++ ports/stm32/mboot/pack.c | 282 ++++++++++++++++++++++++++++ ports/stm32/mboot/pack.h | 82 ++++++++ ports/stm32/mboot/stm32_generic.ld | 4 +- ports/stm32/mboot/vfs_fat.c | 2 +- ports/stm32/mboot/vfs_lfs.c | 4 +- ports/stm32/mpconfigport.mk | 7 +- 18 files changed, 844 insertions(+), 31 deletions(-) create mode 100644 ports/stm32/mboot/Particle.h create mode 100644 ports/stm32/mboot/mboot_pack_dfu.py create mode 100644 ports/stm32/mboot/pack.c create mode 100644 ports/stm32/mboot/pack.h diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index a9dac03d2c021..d8c9772b26eda 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -30,7 +30,7 @@ FROZEN_MANIFEST ?= boards/manifest.py # include py core make definitions include $(TOP)/py/py.mk -GIT_SUBMODULES = lib/lwip lib/mbedtls lib/mynewt-nimble lib/stm32lib +GIT_SUBMODULES = lib/libhydrogen lib/lwip lib/mbedtls lib/mynewt-nimble lib/stm32lib MCU_SERIES_UPPER = $(shell echo $(MCU_SERIES) | tr '[:lower:]' '[:upper:]') CMSIS_MCU_LOWER = $(shell echo $(CMSIS_MCU) | tr '[:upper:]' '[:lower:]') @@ -41,6 +41,7 @@ HAL_DIR=lib/stm32lib/STM32$(MCU_SERIES_UPPER)xx_HAL_Driver USBDEV_DIR=usbdev #USBHOST_DIR=usbhost DFU=$(TOP)/tools/dfu.py +MBOOT_PACK_DFU = mboot/mboot_pack_dfu.py # may need to prefix dfu-util with sudo USE_PYDFU ?= 1 PYDFU ?= $(TOP)/tools/pydfu.py @@ -546,7 +547,13 @@ $(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 all_main $(BUILD)/firmware.hex + +ifeq ($(MBOOT_ENABLE_PACKING),1) +all_main: $(BUILD)/firmware.pack.dfu +else +all_main: $(BUILD)/firmware.dfu +endif # For convenience, automatically fetch required submodules if they don't exist $(TOP)/lib/stm32lib/README.md: @@ -607,6 +614,11 @@ define GENERATE_DFU $(1) endef +define GENERATE_PACK_DFU + $(ECHO) "GEN $(1)" + $(Q)$(PYTHON) $(MBOOT_PACK_DFU) --keys $(MBOOT_PACK_KEYS_FILE) pack-dfu --gzip $(MBOOT_PACK_CHUNKSIZE) $(2) $(1) +endef + define GENERATE_HEX $(ECHO) "GEN $(1)" $(Q)$(OBJCOPY) -O ihex $(2) $(1) @@ -614,8 +626,13 @@ endef .PHONY: deploy deploy-stlink deploy-openocd +ifeq ($(MBOOT_ENABLE_PACKING),1) +deploy: $(BUILD)/firmware.pack.dfu + $(call RUN_DFU,$^) +else deploy: $(BUILD)/firmware.dfu $(call RUN_DFU,$^) +endif # A board should specify TEXT0_ADDR if to use a different location than the # default for the firmware memory location. A board can also optionally define @@ -662,6 +679,9 @@ $(BUILD)/firmware.dfu: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin $(call GENERATE_DFU,$@,$(word 1,$^),$(TEXT0_ADDR),$(word 2,$^),$(TEXT1_ADDR)) endif +$(BUILD)/firmware.pack.dfu: $(BUILD)/firmware.dfu $(BOARD_DIR)/mboot_keys.h + $(call GENERATE_PACK_DFU,$@,$<) + $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(call GENERATE_HEX,$@,$^) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 44372b0e8a4ae..ad2fda3eee508 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -12,6 +12,12 @@ BOARD_DIR ?= $(abspath ../boards/$(BOARD)) # that can be built with or without mboot. USE_MBOOT ?= 1 +# Set MBOOT_ENABLE_PACKING to 1 to enable DFU packing with encryption and signing. +# Ensure the MBOOT_PACK_xxx values match stm32/Makefile, to build matching application firmware. +MBOOT_ENABLE_PACKING ?= 0 +MBOOT_PACK_CHUNKSIZE ?= 16384 +MBOOT_PACK_KEYS_FILE ?= $(BOARD_DIR)/mboot_keys.h + # Sanity check that the board configuration directory exists ifeq ($(wildcard $(BOARD_DIR)/.),) $(error Invalid BOARD specified: $(BOARD_DIR)) @@ -110,6 +116,7 @@ SRC_C = \ elem.c \ fsload.c \ gzstream.c \ + pack.c \ vfs_fat.c \ vfs_lfs.c \ drivers/bus/softspi.c \ @@ -129,6 +136,15 @@ SRC_O = \ $(SYSTEM_FILE) \ ports/stm32/resethandler.o \ +ifeq ($(MBOOT_ENABLE_PACKING), 1) + +SRC_C += lib/libhydrogen/hydrogen.c + +CFLAGS += -DMBOOT_ENABLE_PACKING=1 -DPARTICLE -DPLATFORM_ID=3 +CFLAGS += -DMBOOT_PACK_CHUNKSIZE=$(MBOOT_PACK_CHUNKSIZE) +CFLAGS += -DMBOOT_PACK_KEYS_FILE=\"$(MBOOT_PACK_KEYS_FILE)\" +endif + $(BUILD)/$(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_ll_usb.o: CFLAGS += -Wno-attributes SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ hal_cortex.c \ diff --git a/ports/stm32/mboot/Particle.h b/ports/stm32/mboot/Particle.h new file mode 100644 index 0000000000000..5e0953739a7af --- /dev/null +++ b/ports/stm32/mboot/Particle.h @@ -0,0 +1,10 @@ +// Header for libhydrogen use only. Act like a Particle board for the random +// implementation. This code is not actually called when just decrypting and +// verifying a signature, but a correct implementation is provided anyway. + +#include "py/mphal.h" +#include "rng.h" + +static inline uint32_t HAL_RNG_GetRandomNumber(void) { + return rng_get(); +} diff --git a/ports/stm32/mboot/README.md b/ports/stm32/mboot/README.md index d8aa6d456f19e..59213eb615141 100644 --- a/ports/stm32/mboot/README.md +++ b/ports/stm32/mboot/README.md @@ -155,6 +155,34 @@ firmware.dfu.gz stored on the default FAT filesystem: The 0x80000000 value is the address understood by Mboot as the location of the external SPI flash, configured via `MBOOT_SPIFLASH_ADDR`. +Signed and encrypted DFU support +-------------------------------- + +Mboot optionally supports signing and encrypting the binary firmware in the DFU file. +In general this is refered to as a packed DFU file. This requires additional settings +in the board config and requires the `pyhy` Python module to be installed for `python3` +to be used when building packed firmware, eg: + + $ pip3 install pyhy + +In addition to the changes made to mpconfigboard.mk earlier, for encrypted +support you also need to add: + + MBOOT_ENABLE_PACKING = 1 + +You will also need to generate signing and encryption keys which will be built into +mboot and used for all subsequent installations of firmware. This can be done via: + + $ python3 ports/stm32/mboot/mboot_pack_dfu.py generate-keys + +This command generates a `mboot_keys.h` file which should be stored in the board +definition folder (next to mpconfigboard.mk). + +Once you build the firmware, the `firmware.pack.dfu` file will contain the encrypted +and signed firmware, and can be deployed via USB DFU, or by copying it to the device's +internal filesystem (if `MBOOT_FSLOAD` is enabled). `firmware.dfu` is still unencrypted +and can be directly flashed with jtag etc. + Example: Mboot on PYBv1.x ------------------------- diff --git a/ports/stm32/mboot/dfu.h b/ports/stm32/mboot/dfu.h index a1d4d10d0ef90..73db3545d8d55 100644 --- a/ports/stm32/mboot/dfu.h +++ b/ports/stm32/mboot/dfu.h @@ -39,6 +39,14 @@ #define MBOOT_ERROR_STR_INVALID_ADDRESS_IDX 0x11 #define MBOOT_ERROR_STR_INVALID_ADDRESS "Address out of range" +#if MBOOT_ENABLE_PACKING +#define MBOOT_ERROR_STR_INVALID_SIG_IDX 0x12 +#define MBOOT_ERROR_STR_INVALID_SIG "Invalid signature in file" + +#define MBOOT_ERROR_STR_INVALID_READ_IDX 0x13 +#define MBOOT_ERROR_STR_INVALID_READ "Read support disabled on encrypted bootloader" +#endif + // DFU class requests enum { DFU_DETACH = 0, @@ -104,6 +112,6 @@ typedef struct _dfu_state_t { uint8_t buf[DFU_XFER_SIZE] __attribute__((aligned(4))); } dfu_context_t; -static dfu_context_t dfu_context SECTION_NOZERO_BSS; +extern dfu_context_t dfu_context; #endif // MICROPY_INCLUDED_STM32_MBOOT_DFU_H diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index 1e1ad7a04d015..591b670aa00c2 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -28,6 +28,7 @@ #include "py/mphal.h" #include "mboot.h" +#include "pack.h" #include "vfs.h" #if MBOOT_FSLOAD @@ -36,13 +37,43 @@ #error Must enable at least one VFS component #endif +#if MBOOT_ENABLE_PACKING +// Packed DFU files are gzip'd internally, not on the outside, so reads of the file +// just read the file directly. + +static void *input_stream_data; +static stream_read_t input_stream_read_meth; + +static inline int input_stream_init(void *stream_data, stream_read_t stream_read) { + input_stream_data = stream_data; + input_stream_read_meth = stream_read; + return 0; +} + +static inline int input_stream_read(size_t len, uint8_t *buf) { + return input_stream_read_meth(input_stream_data, buf, len); +} + +#else +// Standard (non-packed) DFU files must be gzip'd externally / on the outside, so +// reads of the file go through gz_stream. + +static inline int input_stream_init(void *stream_data, stream_read_t stream_read) { + return gz_stream_init_from_stream(stream_data, stream_read); +} + +static inline int input_stream_read(size_t len, uint8_t *buf) { + return gz_stream_read(len, buf); +} +#endif + static int fsload_program_file(bool write_to_flash) { // Parse DFU uint8_t buf[512]; size_t file_offset; // Read file header, <5sBIB - int res = gz_stream_read(11, buf); + int res = input_stream_read(11, buf); if (res != 11) { return -1; } @@ -62,7 +93,7 @@ static int fsload_program_file(bool write_to_flash) { uint32_t total_size = get_le32(buf + 6); // Read target header, <6sBi255sII - res = gz_stream_read(274, buf); + res = input_stream_read(274, buf); if (res != 274) { return -1; } @@ -82,7 +113,7 @@ static int fsload_program_file(bool write_to_flash) { // Parse each element for (size_t elem = 0; elem < num_elems; ++elem) { // Read element header, sizeof(buf)) { l = sizeof(buf); } - res = gz_stream_read(l, buf); + res = input_stream_read(l, buf); if (res != l) { return -1; } @@ -135,7 +168,7 @@ static int fsload_program_file(bool write_to_flash) { } // Read trailing info - res = gz_stream_read(16, buf); + res = input_stream_read(16, buf); if (res != 16) { return -1; } @@ -151,7 +184,7 @@ static int fsload_validate_and_program_file(void *stream, const stream_methods_t led_state_all(pass == 0 ? 2 : 4); int res = meth->open(stream, fname); if (res == 0) { - res = gz_stream_init(stream, meth->read); + res = input_stream_init(stream, meth->read); if (res == 0) { res = fsload_program_file(pass == 0 ? false : true); } diff --git a/ports/stm32/mboot/fwupdate.py b/ports/stm32/mboot/fwupdate.py index dab5fa6632e99..65284a6ac0461 100644 --- a/ports/stm32/mboot/fwupdate.py +++ b/ports/stm32/mboot/fwupdate.py @@ -157,14 +157,15 @@ def update_mboot(filename): def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT): - # Check firmware is of .dfu.gz type + # Check firmware is of .dfu or .dfu.gz type try: with open(filename, "rb") as f: hdr = uzlib.DecompIO(f, 16 + 15).read(6) except Exception: - hdr = None + with open(filename, "rb") as f: + hdr = f.read(6) if hdr != b"DfuSe\x01": - print("Firmware must be a .dfu.gz file.") + print("Firmware must be a .dfu(.gz) file.") return ELEM_TYPE_END = 1 diff --git a/ports/stm32/mboot/gzstream.c b/ports/stm32/mboot/gzstream.c index 4f8a4a40cc2dc..6530539f40b1a 100644 --- a/ports/stm32/mboot/gzstream.c +++ b/ports/stm32/mboot/gzstream.c @@ -31,7 +31,7 @@ #include "gzstream.h" #include "mboot.h" -#if MBOOT_FSLOAD +#if MBOOT_FSLOAD || MBOOT_ENABLE_PACKING #define DICT_SIZE (1 << 15) @@ -61,7 +61,17 @@ static int gz_stream_read_src(TINF_DATA *tinf) { return gz_stream.buf[0]; } -int gz_stream_init(void *stream_data, stream_read_t stream_read) { +int gz_stream_init_from_raw_data(const uint8_t *data, size_t len) { + memset(&gz_stream.tinf, 0, sizeof(gz_stream.tinf)); + gz_stream.tinf.source = data; + gz_stream.tinf.source_limit = data + len; + + uzlib_uncompress_init(&gz_stream.tinf, gz_stream.dict, DICT_SIZE); + + return 0; +} + +int gz_stream_init_from_stream(void *stream_data, stream_read_t stream_read) { gz_stream.stream_data = stream_data; gz_stream.stream_read = stream_read; @@ -97,4 +107,4 @@ int gz_stream_read(size_t len, uint8_t *buf) { return gz_stream.tinf.dest - buf; } -#endif // MBOOT_FSLOAD +#endif // MBOOT_FSLOAD || MBOOT_ENABLE_PACKING diff --git a/ports/stm32/mboot/gzstream.h b/ports/stm32/mboot/gzstream.h index ec11ba79bbb02..d3d93aa657312 100644 --- a/ports/stm32/mboot/gzstream.h +++ b/ports/stm32/mboot/gzstream.h @@ -39,7 +39,8 @@ typedef struct _stream_methods_t { stream_read_t read; } stream_methods_t; -int gz_stream_init(void *stream_data, stream_read_t stream_read); +int gz_stream_init_from_raw_data(const uint8_t *data, size_t len); +int gz_stream_init_from_stream(void *stream_data, stream_read_t stream_read); int gz_stream_read(size_t len, uint8_t *buf); #endif // MICROPY_INCLUDED_STM32_MBOOT_GZSTREAM_H diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 99187e3ef8406..0846d97cf365a 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -37,6 +37,7 @@ #include "mboot.h" #include "powerctrl.h" #include "dfu.h" +#include "pack.h" // This option selects whether to use explicit polling or IRQs for USB events. // In some test cases polling mode can run slightly faster, but it uses more power. @@ -56,11 +57,17 @@ // Configure PLL to give the desired CPU freq #undef MICROPY_HW_FLASH_LATENCY #if defined(STM32F4) || defined(STM32F7) -#define CORE_PLL_FREQ (48000000) -#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_1 + #if MBOOT_ENABLE_PACKING + // With encryption/signing/compression, a faster CPU makes processing much faster. + #define CORE_PLL_FREQ (96000000) + #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_3 + #else + #define CORE_PLL_FREQ (48000000) + #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_1 + #endif #elif defined(STM32H7) -#define CORE_PLL_FREQ (96000000) -#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_2 + #define CORE_PLL_FREQ (96000000) + #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_2 #endif #undef MICROPY_HW_CLK_PLLM #undef MICROPY_HW_CLK_PLLN @@ -87,7 +94,8 @@ // These bits are used to detect valid application firmware at APPLICATION_ADDR #define APP_VALIDITY_BITS (0x00000003) -#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +// Global dfu state +dfu_context_t dfu_context SECTION_NOZERO_BSS; static void do_reset(void); @@ -140,7 +148,7 @@ void HAL_Delay(uint32_t ms) { mp_hal_delay_ms(ms); } -static void __fatal_error(const char *msg) { +NORETURN static void __fatal_error(const char *msg) { NVIC_SystemReset(); for (;;) { } @@ -547,7 +555,7 @@ static int spiflash_page_erase(mp_spiflash_t *spif, uint32_t addr, uint32_t n_bl } #endif -int do_page_erase(uint32_t addr, uint32_t *next_addr) { +int hw_page_erase(uint32_t addr, uint32_t *next_addr) { int ret = -1; led0_state(LED0_STATE_ON); @@ -573,7 +581,7 @@ int do_page_erase(uint32_t addr, uint32_t *next_addr) { return ret; } -void do_read(uint32_t addr, int len, uint8_t *buf) { +void hw_read(uint32_t addr, int len, uint8_t *buf) { led0_state(LED0_STATE_FAST_FLASH); #if defined(MBOOT_SPIFLASH_ADDR) if (MBOOT_SPIFLASH_ADDR <= addr && addr < MBOOT_SPIFLASH_ADDR + MBOOT_SPIFLASH_BYTE_SIZE) { @@ -592,7 +600,7 @@ void do_read(uint32_t addr, int len, uint8_t *buf) { led0_state(LED0_STATE_SLOW_FLASH); } -int do_write(uint32_t addr, const uint8_t *src8, size_t len) { +int hw_write(uint32_t addr, const uint8_t *src8, size_t len) { int ret = -1; led0_state(LED0_STATE_FAST_FLASH); #if defined(MBOOT_SPIFLASH_ADDR) @@ -616,6 +624,34 @@ int do_write(uint32_t addr, const uint8_t *src8, size_t len) { return ret; } +int do_page_erase(uint32_t addr, uint32_t *next_addr) { + #if MBOOT_ENABLE_PACKING + // Erase handled automatically for packed mode. + return 0; + #else + return hw_page_erase(addr, next_addr); + #endif +} + +void do_read(uint32_t addr, int len, uint8_t *buf) { + #if MBOOT_ENABLE_PACKING + // Read disabled on packed (encrypted) mode. + dfu_context.status = DFU_STATUS_ERROR_FILE; + dfu_context.error = MBOOT_ERROR_STR_INVALID_READ_IDX; + led0_state(LED0_STATE_SLOW_INVERTED_FLASH); + #else + hw_read(addr, len, buf); + #endif +} + +int do_write(uint32_t addr, const uint8_t *src8, size_t len) { + #if MBOOT_ENABLE_PACKING + return mboot_pack_write(addr, src8, len); + #else + return hw_write(addr, src8, len); + #endif +} + /******************************************************************************/ // I2C slave interface @@ -1068,6 +1104,16 @@ static uint8_t *pyb_usbdd_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, u USBD_GetString((uint8_t*)MBOOT_ERROR_STR_INVALID_ADDRESS, str_desc, length); return str_desc; + #if MBOOT_ENABLE_PACKING + case MBOOT_ERROR_STR_INVALID_SIG_IDX: + USBD_GetString((uint8_t*)MBOOT_ERROR_STR_INVALID_SIG, str_desc, length); + return str_desc; + + case MBOOT_ERROR_STR_INVALID_READ_IDX: + USBD_GetString((uint8_t*)MBOOT_ERROR_STR_INVALID_READ, str_desc, length); + return str_desc; + #endif + default: return NULL; } @@ -1388,6 +1434,10 @@ void stm32_main(int initial_r0) { mp_spiflash_init(MBOOT_SPIFLASH2_SPIFLASH); #endif + #if MBOOT_ENABLE_PACKING + mboot_pack_init(); + #endif + #if MBOOT_FSLOAD if ((initial_r0 & 0xffffff80) == 0x70ad0080) { // Application passed through elements, validate then process them diff --git a/ports/stm32/mboot/mboot.h b/ports/stm32/mboot/mboot.h index 37929665ebe95..e4ed3cecc7f50 100644 --- a/ports/stm32/mboot/mboot.h +++ b/ports/stm32/mboot/mboot.h @@ -36,6 +36,9 @@ #define ELEM_DATA_START (&_estack[0]) #define ELEM_DATA_MAX (&_estack[ELEM_DATA_SIZE]) +#define NORETURN __attribute__((noreturn)) +#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + enum { ELEM_TYPE_END = 1, ELEM_TYPE_MOUNT, @@ -53,6 +56,10 @@ extern uint8_t _estack[ELEM_DATA_SIZE]; uint32_t get_le32(const uint8_t *b); void led_state_all(unsigned int mask); +int hw_page_erase(uint32_t addr, uint32_t *next_addr); +void hw_read(uint32_t addr, int len, uint8_t *buf); +int hw_write(uint32_t addr, const uint8_t *src8, size_t len); + int do_page_erase(uint32_t addr, uint32_t *next_addr); void do_read(uint32_t addr, int len, uint8_t *buf); int do_write(uint32_t addr, const uint8_t *src8, size_t len); diff --git a/ports/stm32/mboot/mboot_pack_dfu.py b/ports/stm32/mboot/mboot_pack_dfu.py new file mode 100644 index 0000000000000..47382f5910187 --- /dev/null +++ b/ports/stm32/mboot/mboot_pack_dfu.py @@ -0,0 +1,260 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2020-2021 Damien P. George +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +Utility to create compressed, encrypted and signed DFU files. +""" + +import argparse +import os +import re +import struct +import sys +import zlib + +sys.path.append(os.path.dirname(__file__) + "/../../../tools") +import dfu + +try: + import pyhy +except ImportError: + raise SystemExit( + "ERROR: pyhy not found. Please install python pyhy for encrypted mboot support: pip3 install pyhy" + ) + + +# Currenty supported version of a packed DFU file. +MBOOT_PACK_HEADER_VERSION = 1 + +# Must match MBOOT_PACK_HYDRO_CONTEXT in mboot/pack.h +MBOOT_PACK_HYDRO_CONTEXT = "mbootenc" + +# Must match enum in mboot/pack.h. +MBOOT_PACK_CHUNK_META = 0 +MBOOT_PACK_CHUNK_FULL_SIG = 1 +MBOOT_PACK_CHUNK_FW_RAW = 2 +MBOOT_PACK_CHUNK_FW_GZIP = 3 + + +class Keys: + def __init__(self, filename): + self.filename = filename + + def generate(self): + kp = pyhy.hydro_sign_keygen() + self.sign_sk = kp.sk + self.sign_pk = kp.pk + self.secretbox = pyhy.hydro_secretbox_keygen() + + def _save_data(self, name, data, file_, hide=False): + prefix = "//" if hide else "" + data = ",".join("0x{:02x}".format(b) for b in data) + file_.write("{}const uint8_t {}[] = {{{}}};\n".format(prefix, name, data)) + + def _load_data(self, name, line): + line = line.split(name + "[] = ") + if len(line) != 2: + raise Exception("malformed input keys: {}".format(line)) + data = line[1].strip() + return bytes(int(value, 16) for value in data[1:-2].split(",")) + + def save(self): + with open(self.filename, "w") as f: + self._save_data("mboot_pack_sign_secret_key", self.sign_sk, f, hide=True) + self._save_data("mboot_pack_sign_public_key", self.sign_pk, f) + self._save_data("mboot_pack_secretbox_key", self.secretbox, f) + + def load(self): + with open(self.filename) as f: + self.sign_sk = self._load_data("mboot_pack_sign_secret_key", f.readline()) + self.sign_pk = self._load_data("mboot_pack_sign_public_key", f.readline()) + self.secretbox = self._load_data("mboot_pack_secretbox_key", f.readline()) + + +def dfu_read(filename): + elems = [] + + with open(filename, "rb") as f: + hdr = f.read(11) + sig, ver, size, num_targ = struct.unpack("<5sBIB", hdr) + file_offset = 11 + + for i in range(num_targ): + hdr = f.read(274) + sig, alt, has_name, name, t_size, num_elem = struct.unpack("<6sBi255sII", hdr) + + file_offset += 274 + file_offset_t = file_offset + for j in range(num_elem): + hdr = f.read(8) + addr, e_size = struct.unpack(" + +#include "dfu.h" +#include "gzstream.h" +#include "mboot.h" +#include "pack.h" + +#if MBOOT_ENABLE_PACKING + +// Keys provided externally by the board, will be built into mboot flash. +#include MBOOT_PACK_KEYS_FILE + +// Encrypted dfu files using gzip require a decompress buffer. Larger can be faster. +// This setting is independent to the incoming encrypted/signed/compressed DFU file. +#ifndef MBOOT_PACK_GZIP_BUFFER_SIZE +#define MBOOT_PACK_GZIP_BUFFER_SIZE (2048) +#endif + +// State to manage automatic flash erasure. +static uint32_t erased_base_addr; +static uint32_t erased_top_addr; + +// DFU chunk buffer, used to cache incoming blocks of data from USB. +static uint32_t firmware_chunk_base_addr; +static mboot_pack_chunk_buf_t firmware_chunk_buf; + +// Temporary buffer for decrypted data. +static uint8_t decrypted_buf[MBOOT_PACK_DFU_CHUNK_BUF_SIZE] __attribute__((aligned(8))); + +// Temporary buffer for uncompressing. +static uint8_t uncompressed_buf[MBOOT_PACK_GZIP_BUFFER_SIZE] __attribute__((aligned(8))); + +// Buffer to hold the start of the firmware, which is only written once the +// entire firmware is validated. This is 8 bytes due to STM32WB MCUs requiring +// that a double-word write to flash can only be done once (due to ECC). +static uint8_t firmware_head[8]; + +void mboot_pack_init(void) { + erased_base_addr = 0; + erased_top_addr = 0; + firmware_chunk_base_addr = 0; +} + +// In encrypted mode the erase is automatically managed. +// Note: this scheme requires blocks be written in sequence, which is the case. +static int mboot_pack_erase(uint32_t addr, size_t len) { + while (!(erased_base_addr <= addr && addr + len <= erased_top_addr)) { + uint32_t erase; + if (erased_base_addr <= addr && addr < erased_top_addr) { + erase = erased_top_addr; + } else { + erase = addr; + erased_base_addr = addr; + } + uint32_t next_addr; + int ret = hw_page_erase(erase, &next_addr); + if (ret != 0) { + return ret; + } + erased_top_addr = next_addr; + } + return 0; +} + +// Commit an unencrypted and uncompressed chunk of firmware to the flash. +static int mboot_pack_commit_chunk(uint32_t addr, uint8_t *data, size_t len) { + // Erase any required sectors before writing. + int ret = mboot_pack_erase(addr, len); + if (ret != 0) { + return ret; + } + + if (addr == APPLICATION_ADDR) { + // Don't write the very start of the firmware, just copy it into a temporary buffer. + // It will be written only if the full firmware passes the checksum/signature. + memcpy(firmware_head, data, sizeof(firmware_head)); + addr += sizeof(firmware_head); + data += sizeof(firmware_head); + len -= sizeof(firmware_head); + } + + // Commit this piece of the firmware. + return hw_write(addr, data, len); +} + +// Handle a chunk with the full firmware signature. +static int mboot_pack_handle_full_sig(void) { + if (firmware_chunk_buf.header.length < hydro_sign_BYTES) { + return -1; + } + + uint8_t *full_sig = &firmware_chunk_buf.data[firmware_chunk_buf.header.length - hydro_sign_BYTES]; + uint32_t *region_data = (uint32_t *)&firmware_chunk_buf.data[0]; + size_t num_regions = (full_sig - (uint8_t *)region_data) / sizeof(uint32_t) / 2; + + uint8_t *buf = decrypted_buf; + const size_t buf_alloc = sizeof(decrypted_buf); + + // Compute the signature of the full firmware. + hydro_sign_state sign_state; + hydro_sign_init(&sign_state, MBOOT_PACK_HYDRO_CONTEXT); + for (size_t region = 0; region < num_regions; ++region) { + uint32_t addr = region_data[2 * region]; + uint32_t len = region_data[2 * region + 1]; + while (len) { + uint32_t l = len <= buf_alloc ? len : buf_alloc; + hw_read(addr, l, buf); + if (addr == APPLICATION_ADDR) { + // The start of the firmware was not yet written to flash so copy + // it out of the temporary buffer to compute the full signature. + memcpy(buf, firmware_head, sizeof(firmware_head)); + } + int ret = hydro_sign_update(&sign_state, buf, l); + if (ret != 0) { + return -1; + } + addr += l; + len -= l; + } + } + + // Verify the signature of the full firmware. + int ret = hydro_sign_final_verify(&sign_state, full_sig, mboot_pack_sign_public_key); + if (ret != 0) { + dfu_context.status = DFU_STATUS_ERROR_VERIFY; + dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; + return -1; + } + + // Full firmware passed the signature check. + // Write the start of the firmware so it boots. + return hw_write(APPLICATION_ADDR, firmware_head, sizeof(firmware_head)); +} + +// Handle a chunk with firmware data. +static int mboot_pack_handle_firmware(void) { + const uint8_t *fw_data = &firmware_chunk_buf.data[0]; + const size_t fw_len = firmware_chunk_buf.header.length; + + // Decrypt the chunk. + if (hydro_secretbox_decrypt(decrypted_buf, fw_data, fw_len, 0, MBOOT_PACK_HYDRO_CONTEXT, mboot_pack_secretbox_key) != 0) { + dfu_context.status = DFU_STATUS_ERROR_VERIFY; + dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; + return -1; + } + + // Use the decrypted message contents going formward. + size_t len = fw_len - hydro_secretbox_HEADERBYTES; + uint32_t addr = firmware_chunk_buf.header.address; + + if (firmware_chunk_buf.header.format == MBOOT_PACK_CHUNK_FW_GZIP) { + // Decompress chunk data. + gz_stream_init_from_raw_data(decrypted_buf, len); + for (;;) { + int read = gz_stream_read(sizeof(uncompressed_buf), uncompressed_buf); + if (read == 0) { + return 0; // finished decompressing + } else if (read < 0) { + return -1; // error reading + } + int ret = mboot_pack_commit_chunk(addr, uncompressed_buf, read); + if (ret != 0) { + return ret; + } + addr += read; + } + } else { + // Commit chunk data directly. + return mboot_pack_commit_chunk(addr, decrypted_buf, len); + } +} + +int mboot_pack_write(uint32_t addr, const uint8_t *src8, size_t len) { + if (addr == APPLICATION_ADDR) { + // Base address of main firmware, reset any previous state + firmware_chunk_base_addr = 0; + } + + if (firmware_chunk_base_addr == 0) { + // First piece of data starting a new chunk, so set the base address. + firmware_chunk_base_addr = addr; + } + + if (addr < firmware_chunk_base_addr) { + // Address out of range. + firmware_chunk_base_addr = 0; + return -1; + } + + size_t offset = addr - firmware_chunk_base_addr; + if (offset + len > sizeof(firmware_chunk_buf)) { + // Address/length out of range. + firmware_chunk_base_addr = 0; + return -1; + } + + // Copy in the new data piece into the chunk buffer. + memcpy((uint8_t *)&firmware_chunk_buf + offset, src8, len); + + if (offset + len < sizeof(firmware_chunk_buf.header)) { + // Don't have the header yet. + return 0; + } + + if (firmware_chunk_buf.header.header_vers != MBOOT_PACK_HEADER_VERSION) { + // Chunk header has the wrong version. + dfu_context.status = DFU_STATUS_ERROR_FILE; + dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; + return -1; + } + + if (firmware_chunk_buf.header.address != firmware_chunk_base_addr) { + // Chunk address doesn't agree with dfu address, abort. + dfu_context.status = DFU_STATUS_ERROR_ADDRESS; + dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; + return -1; + } + + if (offset + len < sizeof(firmware_chunk_buf.header) + firmware_chunk_buf.header.length + sizeof(firmware_chunk_buf.signature)) { + // Don't have the full chunk yet. + return 0; + } + + // Have the full chunk in firmware_chunk_buf, process it now. + + // Reset the chunk base address for the next chunk that comes in. + firmware_chunk_base_addr = 0; + + // Verify the signature of the chunk. + const size_t fw_len = firmware_chunk_buf.header.length; + const uint8_t *sig = &firmware_chunk_buf.data[0] + fw_len; + if (hydro_sign_verify(sig, &firmware_chunk_buf, sizeof(firmware_chunk_buf.header) + fw_len, + MBOOT_PACK_HYDRO_CONTEXT, mboot_pack_sign_public_key) != 0) { + // Signature failed + dfu_context.status = DFU_STATUS_ERROR_VERIFY; + dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; + return -1; + } + + // Signature passed, we have valid chunk. + + if (firmware_chunk_buf.header.format == MBOOT_PACK_CHUNK_META) { + // Ignore META chunks. + return 0; + } else if (firmware_chunk_buf.header.format == MBOOT_PACK_CHUNK_FULL_SIG) { + return mboot_pack_handle_full_sig(); + } else if (firmware_chunk_buf.header.format == MBOOT_PACK_CHUNK_FW_RAW + || firmware_chunk_buf.header.format == MBOOT_PACK_CHUNK_FW_GZIP) { + return mboot_pack_handle_firmware(); + } else { + // Unsupported contents. + return -1; + } +} + +#endif // MBOOT_ENABLE_PACKING diff --git a/ports/stm32/mboot/pack.h b/ports/stm32/mboot/pack.h new file mode 100644 index 0000000000000..195f297ca177d --- /dev/null +++ b/ports/stm32/mboot/pack.h @@ -0,0 +1,82 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_STM32_MBOOT_PACK_H +#define MICROPY_INCLUDED_STM32_MBOOT_PACK_H + +#include +#include "py/mphal.h" + +#if MBOOT_ENABLE_PACKING + +#include "lib/libhydrogen/hydrogen.h" + +// Encrypted & signed bootloader support + +/******************************************************************************/ +// Interface + +#define MBOOT_PACK_HEADER_VERSION (1) + +// Used by libhydrogen for signing and secretbox context. +#define MBOOT_PACK_HYDRO_CONTEXT "mbootenc" + +// Maximum size of the firmware payload. +#define MBOOT_PACK_DFU_CHUNK_BUF_SIZE (MBOOT_PACK_CHUNKSIZE + hydro_secretbox_HEADERBYTES) + +enum mboot_pack_chunk_format { + MBOOT_PACK_CHUNK_META = 0, + MBOOT_PACK_CHUNK_FULL_SIG = 1, + MBOOT_PACK_CHUNK_FW_RAW = 2, + MBOOT_PACK_CHUNK_FW_GZIP = 3, +}; + +// Each DFU chunk transfered has this header to validate it. + +typedef struct _mboot_pack_chunk_buf_t { + struct { + uint8_t header_vers; + uint8_t format; // enum mboot_pack_chunk_format + uint8_t _pad[2]; + uint32_t address; + uint32_t length; // number of bytes in following "data" payload, excluding "signature" + } header; + uint8_t data[MBOOT_PACK_DFU_CHUNK_BUF_SIZE]; + uint8_t signature[hydro_sign_BYTES]; +} mboot_pack_chunk_buf_t; + +// Signing and encryption keys, stored in mboot flash, provided externally. +extern const uint8_t mboot_pack_sign_public_key[hydro_sign_PUBLICKEYBYTES]; +extern const uint8_t mboot_pack_secretbox_key[hydro_secretbox_KEYBYTES]; + +/******************************************************************************/ +// Implementation + +void mboot_pack_init(void); +int mboot_pack_write(uint32_t addr, const uint8_t *src8, size_t len); + +#endif // MBOOT_ENABLE_PACKING + +#endif // MICROPY_INCLUDED_STM32_MBOOT_PACK_H diff --git a/ports/stm32/mboot/stm32_generic.ld b/ports/stm32/mboot/stm32_generic.ld index 0504d6d6aef91..ade43496745eb 100644 --- a/ports/stm32/mboot/stm32_generic.ld +++ b/ports/stm32/mboot/stm32_generic.ld @@ -6,11 +6,11 @@ MEMORY { FLASH_BL (rx) : ORIGIN = 0x08000000, LENGTH = 32K /* sector 0 (can be 32K) */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 120K } /* produce a link error if there is not this amount of RAM for these sections */ -_minimum_stack_size = 2K; +_minimum_stack_size = 8K; /* Define tho top end of the stack. The stack is full descending so begins just above last byte of RAM. Note that EABI requires the stack to be 8-byte diff --git a/ports/stm32/mboot/vfs_fat.c b/ports/stm32/mboot/vfs_fat.c index 20de074f0dcf0..5120bdb1044e6 100644 --- a/ports/stm32/mboot/vfs_fat.c +++ b/ports/stm32/mboot/vfs_fat.c @@ -42,7 +42,7 @@ DRESULT disk_read(void *pdrv, BYTE *buf, DWORD sector, UINT count) { vfs_fat_context_t *ctx = pdrv; if (0 <= sector && sector < ctx->bdev_byte_len / 512) { - do_read(ctx->bdev_base_addr + sector * SECSIZE, count * SECSIZE, buf); + hw_read(ctx->bdev_base_addr + sector * SECSIZE, count * SECSIZE, buf); return RES_OK; } diff --git a/ports/stm32/mboot/vfs_lfs.c b/ports/stm32/mboot/vfs_lfs.c index dec7c015fc087..e4dc511db56f6 100644 --- a/ports/stm32/mboot/vfs_lfs.c +++ b/ports/stm32/mboot/vfs_lfs.c @@ -72,7 +72,7 @@ static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE]; static int dev_read(const struct LFSx_API (config) * c, LFSx_API(block_t) block, LFSx_API(off_t) off, void *buffer, LFSx_API(size_t) size) { VFS_LFSx_CONTEXT_T *ctx = c->context; if (0 <= block && block < ctx->config.block_count) { - do_read(ctx->bdev_base_addr + block * ctx->config.block_size + off, size, buffer); + hw_read(ctx->bdev_base_addr + block * ctx->config.block_size + off, size, buffer); return LFSx_MACRO(_ERR_OK); } return LFSx_MACRO(_ERR_IO); @@ -93,7 +93,7 @@ static int dev_sync(const struct LFSx_API (config) * c) { int VFS_LFSx_MOUNT(VFS_LFSx_CONTEXT_T *ctx, uint32_t base_addr, uint32_t byte_len) { // Read start of superblock. uint8_t buf[48]; - do_read(base_addr, sizeof(buf), buf); + hw_read(base_addr, sizeof(buf), buf); // Verify littlefs and detect block size. if (memcmp(&buf[SUPERBLOCK_MAGIC_OFFSET], "littlefs", 8) != 0) { diff --git a/ports/stm32/mpconfigport.mk b/ports/stm32/mpconfigport.mk index c6b3ddc74f4e0..98ae93d20b997 100644 --- a/ports/stm32/mpconfigport.mk +++ b/ports/stm32/mpconfigport.mk @@ -1,4 +1,4 @@ -# Enable/disable extra modules +# Enable/disable extra modules and features # wiznet5k module for ethernet support; valid values are: # 0 : no Wiznet support @@ -11,3 +11,8 @@ MICROPY_PY_CC3K ?= 0 # VFS FAT FS support MICROPY_VFS_FAT ?= 1 + +# Encrypted/signed bootloader support (ensure the MBOOT_PACK_xxx values match stm32/mboot/Makefile) +MBOOT_ENABLE_PACKING ?= 0 +MBOOT_PACK_CHUNKSIZE ?= 16384 +MBOOT_PACK_KEYS_FILE ?= $(BOARD_DIR)/mboot_keys.h From f6e6ef69e07d8779df6431e491012f49fb9afb44 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jan 2021 03:01:36 +1100 Subject: [PATCH 0509/3533] stm32/boards/NUCLEO_WB55: Enable MBOOT with packing mode. To have at least one board configured with MBOOT_ENABLE_PACKING, for CI testing purposes and demonstration of the feature. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_WB55/mboot_keys.h | 5 +++++ ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 ports/stm32/boards/NUCLEO_WB55/mboot_keys.h diff --git a/ports/stm32/boards/NUCLEO_WB55/mboot_keys.h b/ports/stm32/boards/NUCLEO_WB55/mboot_keys.h new file mode 100644 index 0000000000000..773813079a0ec --- /dev/null +++ b/ports/stm32/boards/NUCLEO_WB55/mboot_keys.h @@ -0,0 +1,5 @@ +//const uint8_t mboot_pack_sign_secret_key[] = {0xf1,0xd8,0x66,0x08,0xbc,0x78,0x39,0x65,0x6a,0xf4,0x88,0xf4,0x43,0x4d,0x10,0xfe,0x4f,0x79,0xb9,0xc3,0x77,0x36,0x23,0xc8,0xcf,0x62,0xa1,0x90,0xf1,0xdc,0xd9,0xbc,0xb2,0x2e,0x59,0x1e,0x53,0x04,0x54,0xd5,0xd1,0x3a,0x6d,0x2e,0x79,0x3e,0xb5,0x70,0xb4,0x9f,0x33,0xff,0x90,0x1d,0xc3,0x54,0x90,0x12,0x96,0x79,0xf4,0xed,0x56,0x75}; +const uint8_t mboot_pack_sign_public_key[] = {0xb2,0x2e,0x59,0x1e,0x53,0x04,0x54,0xd5,0xd1,0x3a,0x6d,0x2e,0x79,0x3e,0xb5,0x70,0xb4,0x9f,0x33,0xff,0x90,0x1d,0xc3,0x54,0x90,0x12,0x96,0x79,0xf4,0xed,0x56,0x75}; +const uint8_t mboot_pack_secretbox_key[] = {0x4a,0xbe,0x9b,0xed,0x55,0x9f,0x74,0xeb,0x1e,0x70,0xde,0xf5,0x19,0x0a,0xeb,0xa5,0x68,0xea,0xb0,0x88,0xe6,0xfe,0x4d,0x10,0x69,0x23,0x06,0x7f,0xdd,0x83,0xf0,0xbf}; + +// The above keys are for demonstration purposes only, do not use them in production! diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk index dcec788ed443f..4cb047c95e074 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk @@ -1,3 +1,7 @@ +# By default this board is configured to use mboot with packing (signing and encryption) +# enabled. Mboot must be deployed first. +USE_MBOOT ?= 1 + MCU_SERIES = wb CMSIS_MCU = STM32WB55xx AF_FILE = boards/stm32wb55_af.csv @@ -17,3 +21,6 @@ endif MICROPY_PY_BLUETOOTH = 1 MICROPY_BLUETOOTH_NIMBLE = 1 MICROPY_VFS_LFS2 = 1 + +# Mboot settings +MBOOT_ENABLE_PACKING = 1 From de2374cdc6889401effba91287b0978a2a59089e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jan 2021 15:17:12 +1100 Subject: [PATCH 0510/3533] tools/ci.sh: Pip install pyhy for stm32 builds. Signed-off-by: Damien George --- tools/ci.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/ci.sh b/tools/ci.sh index 1a413ec991920..a68e6c113db6d 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -192,6 +192,7 @@ function ci_samd_build { function ci_stm32_setup { ci_gcc_arm_setup + pip3 install pyhy } function ci_stm32_pyb_build { From 49dd9ba1a5e50c400a3cfd604dd884bf9fea628c Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 13:52:47 +1100 Subject: [PATCH 0511/3533] stm32/Makefile: Use MBOOT_PACK_KEYS_FILE as depedency of .pack.dfu. To match the definition of GENERATE_PACK_DFU, so a board can customise the location/name of this file if needed. Signed-off-by: Damien George --- ports/stm32/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index d8c9772b26eda..6e5fc15363509 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -679,7 +679,7 @@ $(BUILD)/firmware.dfu: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin $(call GENERATE_DFU,$@,$(word 1,$^),$(TEXT0_ADDR),$(word 2,$^),$(TEXT1_ADDR)) endif -$(BUILD)/firmware.pack.dfu: $(BUILD)/firmware.dfu $(BOARD_DIR)/mboot_keys.h +$(BUILD)/firmware.pack.dfu: $(BUILD)/firmware.dfu $(MBOOT_PACK_KEYS_FILE) $(call GENERATE_PACK_DFU,$@,$<) $(BUILD)/firmware.hex: $(BUILD)/firmware.elf From aa136b4d78c7f81e63bde20ae17ab69bbbf456db Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 22 Jan 2021 17:30:25 +1100 Subject: [PATCH 0512/3533] extmod/modbluetooth: Add ble.hci_cmd(ogf, ocf, req, resp) function. This allows sending arbitrary HCI commands and getting the response. The return value of the function is the status of the command. This is intended for debugging and not to be a part of the public API, and must be enabled via mpconfigboard.h. It's currently only implemented for NimBLE bindings. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 20 ++++++++++++++++++++ extmod/modbluetooth.h | 10 ++++++++++ extmod/nimble/modbluetooth_nimble.c | 23 ++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index caaf872e7ddf4..8870a21c4d520 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -884,6 +884,23 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_recvinto_obj, 4, #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + +STATIC mp_obj_t bluetooth_ble_hci_cmd(size_t n_args, const mp_obj_t *args) { + mp_int_t ogf = mp_obj_get_int(args[1]); + mp_int_t ocf = mp_obj_get_int(args[2]); + mp_buffer_info_t bufinfo_request = {0}; + mp_buffer_info_t bufinfo_response = {0}; + mp_get_buffer_raise(args[3], &bufinfo_request, MP_BUFFER_READ); + mp_get_buffer_raise(args[4], &bufinfo_response, MP_BUFFER_WRITE); + uint8_t status = 0; + bluetooth_handle_errno(mp_bluetooth_hci_cmd(ogf, ocf, bufinfo_request.buf, bufinfo_request.len, bufinfo_response.buf, bufinfo_response.len, &status)); + return MP_OBJ_NEW_SMALL_INT(status); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_hci_cmd_obj, 5, 5, bluetooth_ble_hci_cmd); + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + // ---------------------------------------------------------------------------- // Bluetooth object: Definition // ---------------------------------------------------------------------------- @@ -927,6 +944,9 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_l2cap_send), MP_ROM_PTR(&bluetooth_ble_l2cap_send_obj) }, { MP_ROM_QSTR(MP_QSTR_l2cap_recvinto), MP_ROM_PTR(&bluetooth_ble_l2cap_recvinto_obj) }, #endif + #if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + { MP_ROM_QSTR(MP_QSTR_hci_cmd), MP_ROM_PTR(&bluetooth_ble_hci_cmd_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index f14033de302e2..c96427fcb4a1e 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -60,6 +60,12 @@ #define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (0) #endif +// Optionally enable support for the `hci_cmd` function allowing +// Python to directly low-level HCI commands. +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD +#define MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD (0) +#endif + // This is used to protect the ringbuffer. // A port may no-op this if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS is enabled. #ifndef MICROPY_PY_BLUETOOTH_ENTER @@ -387,6 +393,10 @@ int mp_bluetooth_l2cap_send(uint16_t conn_handle, uint16_t cid, const uint8_t *b int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf, size_t *len); #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD +int mp_bluetooth_hci_cmd(uint16_t ogf, uint16_t ocf, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_len, uint8_t *status); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + ///////////////////////////////////////////////////////////////////////////// // API implemented by modbluetooth (called by port-specific implementations): diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 9ceeab7d7b6c6..8bead890d8373 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -81,9 +81,11 @@ STATIC int ble_hs_err_to_errno(int err) { return 0; } if (err >= 0 && (unsigned)err < MP_ARRAY_SIZE(ble_hs_err_to_errno_table) && ble_hs_err_to_errno_table[err]) { + // Return an MP_Exxx error code. return ble_hs_err_to_errno_table[err]; } else { - return MP_EIO; + // Pass through the BLE error code. + return -err; } } @@ -1660,6 +1662,25 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + +// For ble_hs_hci_cmd_tx +#include "nimble/host/src/ble_hs_hci_priv.h" + +int mp_bluetooth_hci_cmd(uint16_t ogf, uint16_t ocf, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_len, uint8_t *status) { + int rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(ogf, ocf), req, req_len, resp, resp_len); + if (rc < BLE_HS_ERR_HCI_BASE || rc >= BLE_HS_ERR_HCI_BASE + 0x100) { + // The controller didn't handle the command (e.g. HCI timeout). + return ble_hs_err_to_errno(rc); + } else { + // The command executed, but had an error (i.e. invalid parameter). + *status = rc - BLE_HS_ERR_HCI_BASE; + return 0; + } +} + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, union ble_store_value *value) { From 063d7cc0e2f076bd4299ab487563d398c3d212b6 Mon Sep 17 00:00:00 2001 From: Yonatan Schachter Date: Tue, 24 Nov 2020 16:59:04 +0200 Subject: [PATCH 0513/3533] zephyr: Add basic UART functionality to machine module. Currently supports only polling read and write. Signed-off-by: Yonatan Schachter --- ports/zephyr/Makefile | 1 + ports/zephyr/machine_uart.c | 168 ++++++++++++++++++++++++++++++++++++ ports/zephyr/modmachine.c | 1 + ports/zephyr/modmachine.h | 1 + 4 files changed, 171 insertions(+) create mode 100644 ports/zephyr/machine_uart.c diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile index 169aebc26772e..c200062a3ff85 100644 --- a/ports/zephyr/Makefile +++ b/ports/zephyr/Makefile @@ -49,6 +49,7 @@ SRC_C = main.c \ modmachine.c \ machine_i2c.c \ machine_pin.c \ + machine_uart.c \ uart_core.c \ zephyr_storage.c \ lib/timeutils/timeutils.c \ diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c new file mode 100644 index 0000000000000..6ae8707d7f130 --- /dev/null +++ b/ports/zephyr/machine_uart.c @@ -0,0 +1,168 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * Copyright (c) 2020 Yonatan Schachter + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include + +#include "py/runtime.h" +#include "py/stream.h" +#include "py/mperrno.h" +#include "py/objstr.h" +#include "modmachine.h" + +typedef struct _machine_uart_obj_t { + mp_obj_base_t base; + const struct device *dev; + uint16_t timeout; // timeout waiting for first char (in ms) + uint16_t timeout_char; // timeout waiting between chars (in ms) +} machine_uart_obj_t; + +STATIC const char *_parity_name[] = {"None", "Odd", "Even", "Mark", "Space"}; +STATIC const char *_stop_bits_name[] = {"0.5", "1", "1.5", "2"}; +STATIC const char *_data_bits_name[] = {"5", "6", "7", "8", "9"}; +STATIC const char *_flow_control_name[] = {"None", "RTS/CTS", "DTR/DSR"}; + +STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + struct uart_config config; + uart_config_get(self->dev, &config); + mp_printf(print, "UART(\"%s\", baudrate=%u, data_bits=%s, parity_bits=%s, stop_bits=%s, flow_control=%s, timeout=%u, timeout_char=%u)", + self->dev->name, config.baudrate, _data_bits_name[config.data_bits], + _parity_name[config.parity], _stop_bits_name[config.stop_bits], _flow_control_name[config.flow_ctrl], + self->timeout, self->timeout_char); +} + +STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_timeout, ARG_timeout_char }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + self->timeout = args[ARG_timeout].u_int; + self->timeout_char = args[ARG_timeout_char].u_int; +} + +STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + GET_STR_DATA_LEN(args[0], name, name_len); + + machine_uart_obj_t *self = m_new_obj(machine_uart_obj_t); + self->base.type = &machine_uart_type; + self->dev = device_get_binding(name); + if (!self->dev) { + mp_raise_ValueError(MP_ERROR_TEXT("Bad device name")); + } + + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args); + + return MP_OBJ_FROM_PTR(self); +} + +STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); + +STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint8_t *buffer = (uint8_t *)buf_in; + uint8_t data; + mp_uint_t bytes_read = 0; + size_t elapsed_ms = 0; + size_t time_to_wait = self->timeout; + + while ((elapsed_ms < time_to_wait) && (bytes_read < size)) { + if (!uart_poll_in(self->dev, &data)) { + buffer[bytes_read++] = data; + elapsed_ms = 0; + time_to_wait = self->timeout_char; + } else { + k_msleep(1); + elapsed_ms++; + } + } + return bytes_read; +} + +STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint8_t *buffer = (uint8_t *)buf_in; + + for (mp_uint_t i = 0; i < size; i++) { + uart_poll_out(self->dev, buffer[i]); + } + + return size; +} + +STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { + mp_uint_t ret; + + if (request == MP_STREAM_POLL) { + ret = 0; + // read is always blocking + + if (arg & MP_STREAM_POLL_WR) { + ret |= MP_STREAM_POLL_WR; + } + return ret; + } else { + *errcode = MP_EINVAL; + ret = MP_STREAM_ERROR; + } + return ret; +} + +STATIC const mp_stream_p_t uart_stream_p = { + .read = machine_uart_read, + .write = machine_uart_write, + .ioctl = machine_uart_ioctl, + .is_text = false, +}; + +const mp_obj_type_t machine_uart_type = { + { &mp_type_type }, + .name = MP_QSTR_UART, + .print = machine_uart_print, + .make_new = machine_uart_make_new, + .getiter = mp_identity_getiter, + .iternext = mp_stream_unbuffered_iter, + .protocol = &uart_stream_p, + .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, +}; diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 968f758b93ed3..107895bea0e42 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -63,6 +63,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, #endif + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index b67da55338cd7..957f2dd4f9709 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -5,6 +5,7 @@ extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_hard_i2c_type; +extern const mp_obj_type_t machine_uart_type; MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj); From 769e822f190b2ae3c67a6612b7141a54920a19bf Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Fri, 11 Dec 2020 16:59:40 -0800 Subject: [PATCH 0514/3533] esp32/modnetwork: Synchronize WiFi AUTH_xxx constants with IDF values. --- ports/esp32/modnetwork.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 44263350409fc..64f1c91dc7922 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -772,6 +772,11 @@ STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_AUTH_WPA_PSK), MP_ROM_INT(WIFI_AUTH_WPA_PSK) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_PSK), MP_ROM_INT(WIFI_AUTH_WPA2_PSK) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WPA_WPA2_PSK), MP_ROM_INT(WIFI_AUTH_WPA_WPA2_PSK) }, + { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_ENTERPRISE), MP_ROM_INT(WIFI_AUTH_WPA2_ENTERPRISE) }, + #if 0 // TODO: Remove this #if/#endif when lastest ISP IDF will be used + { MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA3_PSK) }, + { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_PSK) }, + #endif { MP_ROM_QSTR(MP_QSTR_AUTH_MAX), MP_ROM_INT(WIFI_AUTH_MAX) }, { MP_ROM_QSTR(MP_QSTR_PHY_LAN8720), MP_ROM_INT(PHY_LAN8720) }, From 419134bea47953888344d325220c376117f6b2b4 Mon Sep 17 00:00:00 2001 From: Oliver Joos Date: Wed, 16 Dec 2020 21:25:18 +0100 Subject: [PATCH 0515/3533] tests/extmod: Add test for the precision of utime functions. According to documentation time() has a precision of at least 1 second. This test runs for 2.5 seconds and calls all utime functions every 100ms. Then it checks if they returned enough different results. All functions with sub-second precision will return ~25 results. This test passes with 15 results or more. Functions that do not exist are skipped silently. --- tests/extmod/utime_res.py | 65 +++++++++++++++++++++++++++++++++++ tests/extmod/utime_res.py.exp | 9 +++++ 2 files changed, 74 insertions(+) create mode 100644 tests/extmod/utime_res.py create mode 100644 tests/extmod/utime_res.py.exp diff --git a/tests/extmod/utime_res.py b/tests/extmod/utime_res.py new file mode 100644 index 0000000000000..4b624334836f1 --- /dev/null +++ b/tests/extmod/utime_res.py @@ -0,0 +1,65 @@ +# test utime resolutions + +try: + import utime +except ImportError: + print("SKIP") + raise SystemExit + + +def gmtime_time(): + return utime.gmtime(utime.time()) + + +def localtime_time(): + return utime.localtime(utime.time()) + + +def test(): + TEST_TIME = 2500 + EXPECTED_MAP = ( + # (function name, min. number of results in 2.5 sec) + ("time", 3), + ("gmtime", 3), + ("localtime", 3), + ("gmtime_time", 3), + ("localtime_time", 3), + ("ticks_ms", 15), + ("ticks_us", 15), + ("ticks_ns", 15), + ("ticks_cpu", 15), + ) + + # call time functions + results_map = {} + end_time = utime.ticks_ms() + TEST_TIME + while utime.ticks_diff(end_time, utime.ticks_ms()) > 0: + utime.sleep_ms(100) + for func_name, _ in EXPECTED_MAP: + try: + time_func = getattr(utime, func_name, None) or globals()[func_name] + now = time_func() # may raise AttributeError + except (KeyError, AttributeError): + continue + try: + results_map[func_name].add(now) + except KeyError: + results_map[func_name] = {now} + + # check results + for func_name, min_len in EXPECTED_MAP: + print("Testing %s" % func_name) + results = results_map.get(func_name) + if results is None: + pass + elif func_name == "ticks_cpu" and results == {0}: + # ticks_cpu() returns 0 on some ports (e.g. unix) + pass + elif len(results) < min_len: + print( + "%s() returns %s result%s in %s ms, expecting >= %s" + % (func_name, len(results), "s"[: len(results) != 1], TEST_TIME, min_len) + ) + + +test() diff --git a/tests/extmod/utime_res.py.exp b/tests/extmod/utime_res.py.exp new file mode 100644 index 0000000000000..08c2d82950603 --- /dev/null +++ b/tests/extmod/utime_res.py.exp @@ -0,0 +1,9 @@ +Testing time +Testing gmtime +Testing localtime +Testing gmtime_time +Testing localtime_time +Testing ticks_ms +Testing ticks_us +Testing ticks_ns +Testing ticks_cpu From 290dc1d5eeba005bdc604a9eb2d781a922081c29 Mon Sep 17 00:00:00 2001 From: Oliver Joos Date: Wed, 16 Dec 2020 22:35:52 +0100 Subject: [PATCH 0516/3533] unix/modtime: Fix time() precision on unix ports with non-double floats. With MICROPY_FLOAT_IMPL_FLOAT the results of utime.time(), gmtime() and localtime() change only every 129 seconds. As one consequence tests/extmod/vfs_lfs_mtime.py will fail on a unix port with LFS support. With this patch these functions only return floats if MICROPY_FLOAT_IMPL_DOUBLE is used. Otherwise they return integers. --- ports/unix/modtime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index e08409e20e3e9..91c0a19413092 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -67,7 +67,7 @@ static inline int msec_sleep_tv(struct timeval *tv) { #endif STATIC mp_obj_t mod_time_time(void) { - #if MICROPY_PY_BUILTINS_FLOAT + #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE struct timeval tv; gettimeofday(&tv, NULL); mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000; @@ -137,7 +137,7 @@ STATIC mp_obj_t mod_time_gm_local_time(size_t n_args, const mp_obj_t *args, stru if (n_args == 0) { t = time(NULL); } else { - #if MICROPY_PY_BUILTINS_FLOAT + #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE mp_float_t val = mp_obj_get_float(args[0]); t = (time_t)MICROPY_FLOAT_C_FUN(trunc)(val); #else From 342dc617842c7ebdc2b93dcbbfe9f0f9bb4789d3 Mon Sep 17 00:00:00 2001 From: Vincent Duvert Date: Mon, 21 Dec 2020 18:48:53 +0100 Subject: [PATCH 0517/3533] cc3200/ftp: Add quotes to PWD response and allow FEAT prior to login. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit improves some FTP implementation details for better compatibility with FTP clients: * The PWD command now puts quotes around the directory name before returning it. This fixes BBEdit’s FTP client, which performs a PWD after each CWD and gets confused if the returned directory path is not surrounded by quotes. * The FEAT command is now allowed before logging in. This fixes the lftp client, which send FEAT first and gets confused (tries to use TLS) if the server responds with 332. --- ports/cc3200/ftp/ftp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/cc3200/ftp/ftp.c b/ports/cc3200/ftp/ftp.c index ee80e51f5269e..37680bc939dde 100644 --- a/ports/cc3200/ftp/ftp.c +++ b/ports/cc3200/ftp/ftp.c @@ -684,7 +684,7 @@ static void ftp_process_cmd (void) { if (E_FTP_RESULT_OK == (result = ftp_recv_non_blocking(ftp_data.c_sd, ftp_cmd_buffer, FTP_MAX_PARAM_SIZE + FTP_CMD_SIZE_MAX, &len))) { // bufptr is moved as commands are being popped ftp_cmd_index_t cmd = ftp_pop_command(&bufptr); - if (!ftp_data.loggin.passvalid && (cmd != E_FTP_CMD_USER && cmd != E_FTP_CMD_PASS && cmd != E_FTP_CMD_QUIT)) { + if (!ftp_data.loggin.passvalid && (cmd != E_FTP_CMD_USER && cmd != E_FTP_CMD_PASS && cmd != E_FTP_CMD_QUIT && cmd != E_FTP_CMD_FEAT)) { ftp_send_reply(332, NULL); return; } @@ -718,7 +718,8 @@ static void ftp_process_cmd (void) { break; case E_FTP_CMD_PWD: case E_FTP_CMD_XPWD: - ftp_send_reply(257, ftp_path); + snprintf((char *)ftp_data.dBuffer, FTP_BUFFER_SIZE, "\"%s\"", ftp_path); + ftp_send_reply(257, (char *)ftp_data.dBuffer); break; case E_FTP_CMD_SIZE: { From 45f0b6ab6300e75a17f6427ec45d812e072a9522 Mon Sep 17 00:00:00 2001 From: Vincent Duvert Date: Wed, 23 Dec 2020 13:41:56 +0100 Subject: [PATCH 0518/3533] cc3200: Fix debug build. * Fix a typo in the Makefile that prevented the debug build to be actually enabled when BTYPE=debug is used. * Add a missing header in modmachine.c that is used when a debug build is created. --- ports/cc3200/application.mk | 2 +- ports/cc3200/mods/modmachine.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/cc3200/application.mk b/ports/cc3200/application.mk index b216ca16dd873..2125274ae138f 100644 --- a/ports/cc3200/application.mk +++ b/ports/cc3200/application.mk @@ -182,7 +182,7 @@ ifeq ($(BTYPE), release) CFLAGS += -DNDEBUG else ifeq ($(BTYPE), debug) -CFLAGS += -DNDEBUG +CFLAGS += -DDEBUG else $(error Invalid BTYPE specified) endif diff --git a/ports/cc3200/mods/modmachine.c b/ports/cc3200/mods/modmachine.c index ddecd47f2b013..89800810c7d1b 100644 --- a/ports/cc3200/mods/modmachine.c +++ b/ports/cc3200/mods/modmachine.c @@ -26,6 +26,7 @@ */ #include +#include #include "py/runtime.h" #include "py/mphal.h" From 0a079155e46d588095e4419b1095fd4ae691ab50 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 23 Jan 2021 17:15:18 +1100 Subject: [PATCH 0519/3533] github/workflows: Fix code-size CI workflow. Changes are: - Use ubuntu-20.04 so that gcc-multilib installs without error. - Use "fetch-depth: 100" to get history prior to pull request. Signed-off-by: Damien George --- .github/workflows/code_size.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/code_size.yml b/.github/workflows/code_size.yml index dd759a4f3ff56..7570261e7d23b 100644 --- a/.github/workflows/code_size.yml +++ b/.github/workflows/code_size.yml @@ -14,9 +14,11 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 + with: + fetch-depth: 100 - name: Install packages run: source tools/ci.sh && ci_code_size_setup - name: Build From 203e1d2a65273db3f6ff063ba1124a89c3482c0f Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 24 Jan 2021 15:02:20 +1100 Subject: [PATCH 0520/3533] tools/ci.sh: For code size build, fetch history of master branch only. It's not necessary to fetch all branches. Signed-off-by: Damien George --- tools/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci.sh b/tools/ci.sh index a68e6c113db6d..86c17bb691e3a 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -42,7 +42,7 @@ function ci_code_size_build { # starts off at either the ref/pull/N/merge FETCH_HEAD, or the current branch HEAD git checkout -b pull_request # save the current location git remote add upstream https://github.com/micropython/micropython.git - git fetch --depth=100 upstream + git fetch --depth=100 upstream master # build reference, save to size0 # ignore any errors with this build, in case master is failing git checkout `git merge-base --fork-point upstream/master pull_request` From 4eaebc1988699db6ebfd35fbe56a3e8d4cd0b373 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Mon, 12 Oct 2020 17:25:05 -0300 Subject: [PATCH 0521/3533] docs/develop: Add MicroPython Internals chapter. This commit adds many new sections to the existing "Developing and building MicroPython" chapter to make it all about the internals of MicroPython. This work was done as part of Google's Season of Docs 2020. --- docs/develop/compiler.rst | 317 +++++++++++++++++++++++++ docs/develop/extendingmicropython.rst | 19 ++ docs/develop/gettingstarted.rst | 324 ++++++++++++++++++++++++++ docs/develop/img/bitmap.png | Bin 0 -> 6388 bytes docs/develop/img/collision.png | Bin 0 -> 3464 bytes docs/develop/img/linprob.png | Bin 0 -> 3655 bytes docs/develop/index.rst | 31 ++- docs/develop/library.rst | 86 +++++++ docs/develop/maps.rst | 63 +++++ docs/develop/memorymgt.rst | 141 +++++++++++ docs/develop/optimizations.rst | 72 ++++++ docs/develop/porting.rst | 310 ++++++++++++++++++++++++ docs/develop/publiccapi.rst | 25 ++ docs/develop/qstr.rst | 7 +- docs/develop/writingtests.rst | 70 ++++++ 15 files changed, 1454 insertions(+), 11 deletions(-) create mode 100644 docs/develop/compiler.rst create mode 100644 docs/develop/extendingmicropython.rst create mode 100644 docs/develop/gettingstarted.rst create mode 100644 docs/develop/img/bitmap.png create mode 100644 docs/develop/img/collision.png create mode 100644 docs/develop/img/linprob.png create mode 100644 docs/develop/library.rst create mode 100644 docs/develop/maps.rst create mode 100644 docs/develop/memorymgt.rst create mode 100644 docs/develop/optimizations.rst create mode 100644 docs/develop/porting.rst create mode 100644 docs/develop/publiccapi.rst create mode 100644 docs/develop/writingtests.rst diff --git a/docs/develop/compiler.rst b/docs/develop/compiler.rst new file mode 100644 index 0000000000000..2007657490f55 --- /dev/null +++ b/docs/develop/compiler.rst @@ -0,0 +1,317 @@ +.. _compiler: + +The Compiler +============ + +The compilation process in MicroPython involves the following steps: + +* The lexer converts the stream of text that makes up a MicroPython program into tokens. +* The parser then converts the tokens into an abstract syntax (parse tree). +* Then bytecode or native code is emitted based on the parse tree. + +For purposes of this discussion we are going to add a simple language feature ``add1`` +that can be use in Python as: + +.. code-block:: bash + + >>> add1 3 + 4 + >>> + +The ``add1`` statement takes an integer as argument and adds ``1`` to it. + +Adding a grammar rule +---------------------- + +MicroPython's grammar is based on the `CPython grammar `_ +and is defined in `py/grammar.h `_. +This grammar is what is used to parse MicroPython source files. + +There are two macros you need to know to define a grammar rule: ``DEF_RULE`` and ``DEF_RULE_NC``. +``DEF_RULE`` allows you to define a rule with an associated compile function, +while ``DEF_RULE_NC`` has no compile (NC) function for it. + +A simple grammar definition with a compile function for our new ``add1`` statement +looks like the following: + +.. code-block:: c + + DEF_RULE(add1_stmt, c(add1_stmt), and(2), tok(KW_ADD1), rule(testlist)) + +The second argument ``c(add1_stmt)`` is the corresponding compile function that should be implemented +in ``py/compile.c`` to turn this rule into executable code. + +The third required argument can be ``or`` or ``and``. This specifies the number of nodes associated +with a statement. For example, in this case, our ``add1`` statement is similar to ADD1 in assembly +language. It takes one numeric argument. Therefore, the ``add1_stmt`` has two nodes associated with it. +One node is for the statement itself, i.e the literal ``add1`` corresponding to ``KW_ADD1``, +and the other for its argument, a ``testlist`` rule which is the top-level expression rule. + +.. note:: + The ``add1`` rule here is just an example and not part of the standard + MicroPython grammar. + +The fourth argument in this example is the token associated with the rule, ``KW_ADD1``. This token should be +defined in the lexer by editing ``py/lexer.h``. + +Defining the same rule without a compile function is achieved by using the ``DEF_RULE_NC`` macro +and omitting the compile function argument: + +.. code-block:: c + + DEF_RULE_NC(add1_stmt, and(2), tok(KW_ADD1), rule(testlist)) + +The remaining arguments take on the same meaning. A rule without a compile function must +be handled explicitly by all rules that may have this rule as a node. Such NC-rules are usually +used to express sub-parts of a complicated grammar structure that cannot be expressed in a +single rule. + +.. note:: + The macros ``DEF_RULE`` and ``DEF_RULE_NC`` take other arguments. For an in-depth understanding of + supported parameters, see `py/grammar.h `_. + +Adding a lexical token +---------------------- + +Every rule defined in the grammar should have a token associated with it that is defined in ``py/lexer.h``. +Add this token by editing the ``_mp_token_kind_t`` enum: + +.. code-block:: c + :emphasize-lines: 12 + + typedef enum _mp_token_kind_t { + ... + MP_TOKEN_KW_OR, + MP_TOKEN_KW_PASS, + MP_TOKEN_KW_RAISE, + MP_TOKEN_KW_RETURN, + MP_TOKEN_KW_TRY, + MP_TOKEN_KW_WHILE, + MP_TOKEN_KW_WITH, + MP_TOKEN_KW_YIELD, + MP_TOKEN_KW_ADD1, + ... + } mp_token_kind_t; + +Then also edit ``py/lexer.c`` to add the new keyword literal text: + +.. code-block:: c + :emphasize-lines: 12 + + STATIC const char *const tok_kw[] = { + ... + "or", + "pass", + "raise", + "return", + "try", + "while", + "with", + "yield", + "add1", + ... + }; + +Notice the keyword is named depending on what you want it to be. For consistency, maintain the +naming standard accordingly. + +.. note:: + The order of these keywords in ``py/lexer.c`` must match the order of tokens in the enum + defined in ``py/lexer.h``. + +Parsing +------- + +In the parsing stage the parser takes the tokens produced by the lexer and converts them to an abstract syntax tree (AST) or +*parse tree*. The implementation for the parser is defined in `py/parse.c `_. + +The parser also maintains a table of constants for use in different aspects of parsing, similar to what a +`symbol table `_ +does. + +Several optimizations like `constant folding `_ +on integers for most operations e.g. logical, binary, unary, etc, and optimizing enhancements on parenthesis +around expressions are performed during this phase, along with some optimizations on strings. + +It's worth noting that *docstrings* are discarded and not accessible to the compiler. +Even optimizations like `string interning `_ are +not applied to *docstrings*. + +Compiler passes +--------------- + +Like many compilers, MicroPython compiles all code to MicroPython bytecode or native code. The functionality +that achieves this is implemented in `py/compile.c `_. +The most relevant method you should know about is this: + +.. code-block:: c + + mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) { + // Compile the input parse_tree to a raw-code structure. + mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, is_repl); + // Create and return a function object that executes the outer module. + return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); + } + +The compiler compiles the code in four passes: scope, stack size, code size and emit. +Each pass runs the same C code over the same AST data structure, with different things +being computed each time based on the results of the previous pass. + +First pass +~~~~~~~~~~ + +In the first pass, the compiler learns about the known identifiers (variables) and +their scope, being global, local, closed over, etc. In the same pass the emitter +(bytecode or native code) also computes the number of labels needed for the emitted +code. + +.. code-block:: c + + // Compile pass 1. + comp->emit = emit_bc; + comp->emit_method_table = &emit_bc_method_table; + + uint max_num_labels = 0; + for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { + if (s->emit_options == MP_EMIT_OPT_ASM) { + compile_scope_inline_asm(comp, s, MP_PASS_SCOPE); + } else { + compile_scope(comp, s, MP_PASS_SCOPE); + + // Check if any implicitly declared variables should be closed over. + for (size_t i = 0; i < s->id_info_len; ++i) { + id_info_t *id = &s->id_info[i]; + if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { + scope_check_to_close_over(s, id); + } + } + } + ... + } + +Second and third passes +~~~~~~~~~~~~~~~~~~~~~~~ + +The second and third passes involve computing the Python stack size and code size +for the bytecode or native code. After the third pass the code size cannot change, +otherwise jump labels will be incorrect. + +.. code-block:: c + + for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { + ... + + // Pass 2: Compute the Python stack size. + compile_scope(comp, s, MP_PASS_STACK_SIZE); + + // Pass 3: Compute the code size. + if (comp->compile_error == MP_OBJ_NULL) { + compile_scope(comp, s, MP_PASS_CODE_SIZE); + } + + ... + } + +Just before pass two there is a selection for the type of code to be emitted, which can +either be native or bytecode. + +.. code-block:: c + + // Choose the emitter type. + switch (s->emit_options) { + case MP_EMIT_OPT_NATIVE_PYTHON: + case MP_EMIT_OPT_VIPER: + if (emit_native == NULL) { + emit_native = NATIVE_EMITTER(new)(&comp->compile_error, &comp->next_label, max_num_labels); + } + comp->emit_method_table = NATIVE_EMITTER_TABLE; + comp->emit = emit_native; + break; + + default: + comp->emit = emit_bc; + comp->emit_method_table = &emit_bc_method_table; + break; + } + +The bytecode option is the default but something unique to note for the native +code option is that there is another option via ``VIPER``. See the +:ref:`Emitting native code ` section for more details on +viper annotations. + +There is also support for *inline assembly code*, where assembly instructions are +written as Python function calls but are emitted directly as the corresponding +machine code. This assembler has only three passes (scope, code size, emit) +and uses a different implementation, not the ``compile_scope`` function. +See the `inline assembler tutorial `_ +for more details. + +Fourth pass +~~~~~~~~~~~ + +The fourth pass emits the final code that can be executed, either bytecode in +the virtual machine, or native code directly by the CPU. + +.. code-block:: c + + for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { + ... + + // Pass 4: Emit the compiled bytecode or native code. + if (comp->compile_error == MP_OBJ_NULL) { + compile_scope(comp, s, MP_PASS_EMIT); + } + } + +Emitting bytecode +----------------- + +Statements in Python code usually correspond to emitted bytecode, for example ``a + b`` +generates "push a" then "push b" then "binary op add". Some statements do not emit +anything but instead affect other things like the scope of variables, for example +``global a``. + +The implementation of a function that emits bytecode looks similar to this: + +.. code-block:: c + + void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) { + emit_write_bytecode_byte(emit, 0, MP_BC_UNARY_OP_MULTI + op); + } + +We use the unary operator expressions for an example here but the implementation +details are similar for other statements/expressions. The method ``emit_write_bytecode_byte()`` +is a wrapper around the main function ``emit_get_cur_to_write_bytecode()`` that all +functions must call to emit bytecode. + +.. _emitting_native_code: + +Emitting native code +--------------------- + +Similar to how bytecode is generated, there should be a corresponding function in ``py/emitnative.c`` for each +code statement: + +.. code-block:: c + + STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) { + vtype_kind_t vtype; + emit_pre_pop_reg(emit, &vtype, REG_ARG_2); + if (vtype == VTYPE_PYOBJ) { + emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1); + emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); + } else { + adjust_stack(emit, 1); + EMIT_NATIVE_VIPER_TYPE_ERROR(emit, + MP_ERROR_TEXT("unary op %q not implemented"), mp_unary_op_method_name[op]); + } + } + +The difference here is that we have to handle *viper typing*. Viper annotations allow +us to handle more than one type of variable. By default all variables are Python objects, +but with viper a variable can also be declared as a machine-typed variable like a native +integer or pointer. Viper can be thought of as a superset of Python, where normal Python +objects are handled as usual, while native machine variables are handled in an optimised +way by using direct machine instructions for the operations. Viper typing may break +Python equivalence because, for example, integers become native integers and can overflow +(unlike Python integers which extend automatically to arbitrary precision). diff --git a/docs/develop/extendingmicropython.rst b/docs/develop/extendingmicropython.rst new file mode 100644 index 0000000000000..7fb1ae47a0e4e --- /dev/null +++ b/docs/develop/extendingmicropython.rst @@ -0,0 +1,19 @@ +.. _extendingmicropython: + +Extending MicroPython in C +========================== + +This chapter describes options for implementing additional functionality in C, but from code +written outside of the main MicroPython repository. The first approach is useful for building +your own custom firmware with some project-specific additional modules or functions that can +be accessed from Python. The second approach is for building modules that can be loaded at runtime. + +Please see the :ref:`library section ` for more information on building core modules that +live in the main MicroPython repository. + +.. toctree:: + :maxdepth: 3 + + cmodules.rst + natmod.rst + \ No newline at end of file diff --git a/docs/develop/gettingstarted.rst b/docs/develop/gettingstarted.rst new file mode 100644 index 0000000000000..3dd00a579a2b7 --- /dev/null +++ b/docs/develop/gettingstarted.rst @@ -0,0 +1,324 @@ +.. _gettingstarted: + +Getting Started +=============== + +This guide covers a step-by-step process on setting up version control, obtaining and building +a copy of the source code for a port, building the documentation, running tests, and a description of the +directory structure of the MicroPython code base. + +Source control with git +----------------------- + +MicroPython is hosted on `GitHub `_ and uses +`Git `_ for source control. The workflow is such that +code is pulled and pushed to and from the main repository. Install the respective version +of Git for your operating system to follow through the rest of the steps. + +.. note:: + For a reference on the installation instructions, please refer to + the `Git installation instructions `_. + Learn about the basic git commands in this `Git Handbook `_ + or any other sources on the internet. + +Get the code +------------ + +It is recommended that you maintain a fork of the MicroPython repository for your development purposes. +The process of obtaining the source code includes the following: + +#. Fork the repository https://github.com/micropython/micropython +#. You will now have a fork at /micropython>. +#. Clone the forked repository using the following command: + +.. code-block:: bash + + $ git clone https://github.com//micropython + +Then, `configure the remote repositories `_ to be able to +collaborate on the MicroPython project. + +Configure remote upstream: + +.. code-block:: bash + + $ cd micropython + $ git remote add upstream https://github.com/micropython/micropython + +It is common to configure ``upstream`` and ``origin`` on a forked repository +to assist with sharing code changes. You can maintain your own mapping but +it is recommended that ``origin`` maps to your fork and ``upstream`` to the main +MicroPython repository. + +After the above configuration, your setup should be similar to this: + +.. code-block:: bash + + $ git remote -v + origin https://github.com//micropython (fetch) + origin https://github.com//micropython (push) + upstream https://github.com/micropython/micropython (fetch) + upstream https://github.com/micropython/micropython (push) + +You should now have a copy of the source code. By default, you are pointing +to the master branch. To prepare for further development, it is recommended +to work on a development branch. + +.. code-block:: bash + + $ git checkout -b dev-branch + +You can give it any name. You will have to compile MicroPython whenever you change +to a different branch. + +Compile and build the code +-------------------------- + +When compiling MicroPython, you compile a specific :term:`port`, usually +targeting a specific :ref:`board `. Start by installing the required dependencies. +Then build the MicroPython cross-compiler before you can successfully compile and build. +This applies specifically when using Linux to compile. +The Windows instructions are provided in a later section. + +.. _required_dependencies: + +Required dependencies +~~~~~~~~~~~~~~~~~~~~~ + +Install the required dependencies for Linux: + +.. code-block:: bash + + $ sudo apt-get install build-essential libffi-dev git pkg-config + +For the stm32 port, the ARM cross-compiler is required: + +.. code-block:: bash + + $ sudo apt-get install arm-none-eabi-gcc arm-none-eabi-binutils arm-none-eabi-newlib + +See the `ARM GCC +toolchain `_ +for the latest details. + +Python is also required. Python 2 is supported for now, but we recommend using Python 3. +Check that you have Python available on your system: + +.. code-block:: bash + + $ python3 + Python 3.5.0 (default, Jul 17 2020, 14:04:10) + [GCC 5.4.0 20160609] on linux + Type "help", "copyright", "credits" or "license" for more information. + >>> + +All supported ports have different dependency requirements, see their respective +`readme files `_. + +Building the MicroPython cross-compiler +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Almost all ports require building ``mpy-cross`` first to perform pre-compilation +of Python code that will be included in the port firmware: + +.. code-block:: bash + + $ cd mpy-cross + $ make + +.. note:: + Note that, ``mpy-cross`` must be built for the host architecture + and not the target architecture. + +If it built successfully, you should see a message similar to this: + +.. code-block:: bash + + LINK mpy-cross + text data bss dec hex filename + 279328 776 880 280984 44998 mpy-cross + +.. note:: + + Use ``make -C mpy-cross`` to build the cross-compiler in one statement + without moving to the ``mpy-cross`` directory otherwise, you will need + to do ``cd ..`` for the next steps. + +Building the Unix port of MicroPython +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The Unix port is a version of MicroPython that runs on Linux, macOS, and other Unix-like operating systems. +It's extremely useful for developing MicroPython as it avoids having to deploy your code to a device to test it. +In many ways, it works a lot like CPython's python binary. + +To build for the Unix port, make sure all Linux related dependencies are installed as detailed in the +required dependencies section. See the :ref:`required_dependencies` +to make sure that all dependencies are installed for this port. Also, make sure you have a working +environment for ``gcc`` and ``GNU make``. Ubuntu 20.04 has been used for the example +below but other unixes ought to work with little modification: + +.. code-block:: bash + + $ gcc --version + gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0 + Copyright (C) 2019 Free Software Foundation, Inc. + This is free software; see the source for copying conditions. There is NO + warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.then build: + +.. code-block:: bash + + $ cd ports/unix + $ make submodules + $ make + +If MicroPython built correctly, you should see the following: + +.. code-block:: bash + + LINK micropython + text data bss dec hex filename + 412033 5680 2496 420209 66971 micropython + +Now run it: + +.. code-block:: bash + + $ ./micropython + MicroPython v1.13-38-gc67012d-dirty on 2020-09-13; linux version + Use Ctrl-D to exit, Ctrl-E for paste mode + >>> print("hello world") + hello world + >>> + +Building the Windows port +~~~~~~~~~~~~~~~~~~~~~~~~~ + +The Windows port includes a Visual Studio project file micropython.vcxproj that you can use to build micropython.exe. +It can be opened in Visual Studio or built from the command line using msbuild. Alternatively, it can be built using mingw, +either in Windows with Cygwin, or on Linux. +See `windows port documentation `_ for more information. + +Building the STM32 port +~~~~~~~~~~~~~~~~~~~~~~~ + +Like the Unix port, you need to install some required dependencies +as detailed in the :ref:`required_dependencies` section, then build: + +.. code-block:: bash + + $ cd ports/stm32 + $ make submodules + $ make + +Please refer to the `stm32 documentation `_ +for more details on flashing the firmware. + +.. note:: + See the :ref:`required_dependencies` to make sure that all dependencies are installed for this port. + The cross-compiler is needed. ``arm-none-eabi-gcc`` should also be in the $PATH or specified manually + via CROSS_COMPILE, either by setting the environment variable or in the ``make`` command line arguments. + +You can also specify which board to use: + +.. code-block:: bash + + $ cd ports/stm32 + $ make submodules + $ make BOARD= + +See `ports/stm32/boards `_ +for the available boards. e.g. "PYBV11" or "NUCLEO_WB55". + +Building the documentation +-------------------------- + +MicroPython documentation is created using ``Sphinx``. If you have already +installed Python, then install ``Sphinx`` using ``pip``. It is recommended +that you use a virtual environment: + +.. code-block:: bash + + $ python3 -m venv env + $ source env/bin/activate + $ pip install sphinx + +Navigate to the ``docs`` directory: + +.. code-block:: bash + + $ cd docs + +Build the docs: + +.. code-block:: bash + + $ make html + +Open ``docs/build/html/index.html`` in your browser to view the docs locally. Refer to the +documentation on `importing your documentation +`_ to use Read the Docs. + +Running the tests +----------------- + +To run all tests in the test suite on the Unix port use: + +.. code-block:: bash + + $ cd ports/unix + $ make test + +To run a selection of tests on a board/device connected over USB use: + +.. code-block:: bash + + $ cd tests + $ ./run-tests --target minimal --device /dev/ttyACM0 + +See also :ref:`writingtests`. + +Folder structure +---------------- + +There are a couple of directories to take note of in terms of where certain implementation details +are. The following is a break down of the top-level folders in the source code. + +py + + Contains the compiler, runtime, and core library implementation. + +mpy-cross + + Has the MicroPython cross-compiler which pre-compiles the Python scripts to bytecode. + +ports + + Code for all the versions of MicroPython for the supported ports. + +lib + + Low-level C libraries used by any port which are mostly 3rd-party libraries. + +drivers + + Has drivers for specific hardware and intended to work across multiple ports. + +extmod + + Contains a C implementation of more non-core modules. + +docs + + Has the standard documentation found at https://docs.micropython.org/. + +tests + + An implementation of the test suite. + +tools + + Contains helper tools including the ``upip`` and the ``pyboard.py`` module. + +examples + + Example code for building MicroPython as a library as well as native modules. diff --git a/docs/develop/img/bitmap.png b/docs/develop/img/bitmap.png new file mode 100644 index 0000000000000000000000000000000000000000..87de81d769431782a9cb77c0bc7e11f6e3c07044 GIT binary patch literal 6388 zcmcIpbx>Ptn}>2+thoD4inK^+OK~XDP^?ICDNu?#0fL4UD8=rD6bZ$-Sc6mCf>T_I z6n6~??hOPscXwy!+kbXv_WSnCoH^%tW}bKEJ@5Pe=Z;qL{#c(AUz@? zVrIfEN_K~kp)m^xCOk;ImDLT%$jIhbbyf%=n~#c#kG_Y!kDs-d9g%~(hnt;v%T9wimKSp9E5wfYkoH{}5`)Lk~ym{uy+`k-^j40(xDEHRbQTHAtIZE*n zFJvevwkOwhlBnNnJ81K1@NK`kO1?s)4gu0gx74Es@^`mW~vC?`}*j%2HHFPXg;P@Ci#PuZf4cd zx>s(o==-VU)++o&ZUs$g&CUC>^8{7OTYtOWVw#6Qx5h|Fv!RJBwMW~lkY)bTaGwB< zS29 z)eEcrL#I@~HlsmPvwBW_5J{QJ94R{&a?I4U4qCOEwVHmbi%3W1L@y*$JwJlWy$ZNr zo|>AvDk147(jmvyDb1f@rSw8OnnuCFnc-VEBntRbqicI)i40t$4 zMK!Q6^V~cTV&I@!7737w?b`>yD%i-_(I-ENaE!0(oL_anNf1#>_&4nh9|3yXIC4;M zR5kcCEul`@|KZqha_YB<;m+!zS26hJI}0La$s!CVKsumWbhB6v_lkPPmAj z_!XZ7y?=O^k_xg7H-D<^0NIRaUlo0fJ_on!a5hv;%)AT{2xE}`r;nw&aqA`3k39ks zJUgc~8&W@55E1%Jn-bGciT_jnHJ$qR zxaJb@V4xFca`TFof}Ri#u7gfEG+<101n+`*t0M}xwmc4zQb{eBji74D2%!f8-LKEX zG$mVGeI6Vn`ibZW&w_EV`tqBtobVJ6%elbg@z_Lj{)GUAS74;MF-=RtH+tvqR1_(P z33W}l%5a})xN8Ph+I0Vy^E<_iHI0*}`8ep>wJyB><}pC?#l!I$t84cu!BIBZ5e3Ke zabxDBD*GizA)wG~{+o~cZ`&qpLxPE|861_Hu&Gv1A&#?S@4c@Go){gJ*nWHeZs+KE zgu+at@Vd;Kv-s`{4NKU;b^pS|ff?6q!_`{eC+xua5I%)jpatvpvo2!$)c}btCmr2l zqi#N!jjsznyi!kwmCu!J>pnoR0yFoOcp{z5zffT`Ptl5REc`8Z=r~stLAFn%D;F18p1EZ<#8B^|8*;ygI+pT(-1A2v8GEX|aNwCXTY0F>p<(^68 z(9qDIO~L*^!pQ_L9c)`%I&^&Eu(FcDOSok+WdHPKKVKou?3=6F)Eb>P1Ir8tFMhnD zN;b+(W-<31R!upmu0v*BFC9ypm*=@HNV7`{tX}P0O%G;c9qWj9Vsnswf&v0aq@?w` zPK`pU?I$lBQRoAQK{KQYexx|()x@!rsySY<*YqZ?x{BEe=Ggz*?*f;31p;LZ&IiF; zi_~ZHFJ~brOd?p**PAkqcaTeFm(t`@c=(}Qc2bh+@x1FzZ!{g30)zqFyN*-_l$b|rlJD)7MKQ72cG{#cSfO{n$ zS=%?$h*5j+^WK*cyZW%fotVc+!VG<~7}|GUI|JcXd79OF)5=kTEKikeT3m~FQHHRk zpD(*-=1QkSZUV=GvfTe3?p|Yn)R6Il*vg1Jm;G?s_Y;&ha>BW`3?MEc!5;M1!eMH5 zV%cyTc$6edBIRl4Fg{}uv0T1dVeQGu!p@Fa^s9=#{_5+{w)=Z&9xbb~j#cHi+FG|I z+Fr2SXm4-t!EAMJL-UZb2}`Zg;j?GYYRp7>!S?p{U@-XU)2BV$bV9tp-K%vR%kAmu zxxBozq}354BoL{o(%?0a4LnyC0C-YpwS%OzYLd9CM)vws^xJssuBN97^?%X-|CPeW zcSK>++}9V{b4v^_L$ldkc8OW1^nDJ(%gpP2ucTH4a`WTz{Zrc$edG8{PBwlX1$A!L)C|bWX_0m2WE4s+n5O#g1!n%o1r8ckRwSp3 zcRVf5xSl}jWRJ{=FFsRvB%(S|Rw8<9+qIuYj*ZKB0pT6n%yu%b&XRiVugAv5W>!`w zo`u8VQc_ss)ogdckj%faQ5~u(`=Yd{$VH*@7b`t0J2N|bqtcrt=>4Kc3$>TAMWLag z9;JN`VWm=r+O8%bvnwa&@`))BsjL~jsmxWwx$$CQfMZB-K$EapO<`qar8u_@4U&WG zIl$ukm+gVbfF^2J$-SP+jm2W}bU2T+{{~5;{fF^*irlo_vi{}RCC`=1BZc9E>`YYs zC9*HK9g2l5$t=>*PQOlO@7aEXpY8p^;kGu< zRv4K1wKHbCg(NAg`FPn|_(EwpOvrxQD2I1n>4a2TIV^SY^+0ud3VwjsCO z<<^n*XIacVyi8vo_84iR0_r~Vqs~|<#Wk@`GY~jM9AIH+tsUR9l(YA`wC&2Vx2iOQ zLk6f9oazmkahwj#sBW$n6vbvM&=*lEylp!6lqT%1kZxG_5F#`UZ_ZTg%W@x7d|k8A zA}z=juMLs8E$)NO5WPHLA&VnOEB43yT8HYuH$3ru%4>2?n*)mEZ&U_{IwsbDhFBYW z?tsEP;w*3DyR!^q`Diq@-=%AV9dp20<5ec~)567c0frk`sbCi1xwu}bO{SL4{th14 zD2rF>KeGjZZ@SVDSzK8d;u@Q&S%&@kWf+G}qgV(UjfN}S%pA^(i(yaAbInU-Se$@u zMVZfijggkXNrJv%2qk!x{{*Y(Z{>YT6^22F%d$?*m8wc0kd17;6#X52YYOAQyu3Ww zEBcJS|6P#1)0LK%?%`IJe5w3S*T0H{h(T_aID|ZFm#8J9GHbW}1Z6ETx97_5^LI1~?TwS_9sjIH!0QpC4LPf0N5A}{qqr?CPn{>rn{}F)H{%Pp z6CdBMElkYwYjHVjitYcB=D5IXPIGldSZe?c~{{xm(~D zb(YofSLm5_nqxQ*o423cfWv^hh5V@oK{+r9oSktLloUaQ0T!md1NlWtqdHW0d`9e5 ztILas$qcnWVA2mA(Ip9Qe^%TS8x>d)WS=A4Z_UOt^0Y1uqtCY90S|-pzmxpmf zIE0a2%QVpff_rUv^NaLqIXC#t$?=?am-A1vrSV*Xb~~$It-&7cxjt$1S6w#U3`0<}~X2d_E))7aGeY6Ljth-#5_K)`piaABH1z zIbc!CuA*<=jQN%6cS{(VK`aK5To$orz4J3suEHwL5Ka5JAdP~EsHhLCW%^EHlR^6D z_+1%GyPKXLUAd6?X&sJEws)dg%Ku2OuL{YX4uTx{1t4qmjZr0rCf5}8ZqpSgZ z&(dJM!nQP~plU&X`C2W%73DQn48KtU@g}R;X|M9Uc2m>1F_^sxQyMN(nJ;-D`8a!g zxjpEFakkFQybXpY(C^UjaFD7RVHd5=;Es%JP-Re5K&wm*`qnAoW?b8}IPjg9T$Hl9x1Im2nUteo;}d_Cl- zZCPr(ygah9<$yKAU@&<+7%dH^F; zdynLJczD_zM!DDb#VM^VChv6&_VxX6uI20{c|rHF!=j+5FMn}IFU$0zWuo?j;* z5T-LYhCVTxoIKQN6}ck=Eq>Bc6h%|{Sp(#pohf^Mg^XWwhLqIZbjSIh)+bhTJya`I6x zd5^d4y-y-&YQw~)jl?`YUt%w~FM~)J&4w_f@-uR|csR|a_k!U!}SF@KGrw7gHvG0YG@wAlYuoj+S- zJ^C`f{F&Y(4~BTH!~JJ=TY4VkplLJA_GAL2ENM0GoDtM)rZnFS5$sCmllPoJ+pxQ+Di!2_;MKF^+*R~;9J|U zs6v9!`-S#E6l)~RQ0)&e6Iv6jZeyUo3UftadJ9UB{kH%)z;YjIvOEQ$nWQ9OV1BsdQcmyfUD?&ZH!H&8h8duKeBTohGyI(% z8BIA}QI{cFBI-(h;Y(|tmQLe&nD)g|PZg9e&-^%9L~Z`5Lq=p`7cJ@Zx0$@)C?{v_4Dep%Y41aJE zHj&Aod0|p9%}G_A_*nkdF$Y%~)#3Zbick=Yzo@N^6-lJbEs&UiPhry;5)KS&U$@<~ zI?T%uXT7$v;knCpKfdnTB+Wldzw+oUuTSz5rT#c{o&UYckc0{N0qsDunGPJ_*=R&Q*SpkCVARku$vN| zTx?KmUc9R|>rp|v(tlbWes3_t-_FSt3;&}AQU4Q^-)f+1F90WtKLUXe1;+DujdC;7 zb9B9`LA~K~6JQ5`OeW8dulp)ei$zG{yS=evk-9~d(88dYMh-vh+vL}#{1tN%aYN>> z!iG>$07B#EbwXCz~ny}3A|Eo%Zy|Gk%vLZ-lv4}`kiD)l+2vden!U3psj5jvSu z^NwQ;pX{D$nT>hGthrgFMLF)L5@7MiS)hDNLX;~>r(Mhn%lECSs4y1&3F8LVAQr`U z;kbG7#q|@PD%a?>UWY#`GG22T~{7)KqhRYZ0EiZLbuUBpkUcR<)2mlImbA^a`+QT6bn5Uzg z7nz}5@q+8Nwwf}?FLQeV`67jq#rVtcIxC-yl-U($ZX@1?n3Gh7j@bvVbFH35K2-2x zXAb7p{f>rR2{{t&Tu$n=y_EOJNb2TC+1qSS=>~b*#l!0TLqI%^O&*e?;(5dLtV6Jx zYx08~?#TZ{2rl`cZQETu$E-&CeEZYEpiz6f8wW=HdaC?ppF!P-i@HOJULxyql9e8gMNCkdDm#;~Q${;~zsiJ@STg0^on~U~KRae~`hqe9NvY|F12UHrrrsWUmsd<=MEL6lKG%`h zYdw&c#lj+S5e`o9cpVLaOZnu*VmC)3XfK{FeNZ1tSfkHr>P5T~5n(o7y2nA^(0hqz zheu8Pi+Jt^X~45nAsSB)k_?V8jA;6XlLXxBN)}I+TU+Vy*H`*yJmpeZLItU%)Qp&AQIx_XcXP|8eg?!5(od^2D&cxE2m7dd zB(7TOamt8tUnjx%`~h2%%#6p*SfYr77|IlgUa{eqA(@#Tp;Ih?>}f-V{fo&NAuU9( z5#HhGIH+|li~yH2y7a#839O;;1d*_7ScyZi4e+_fIE>J|)paWIZX1c`MC^DW7Xn5G(*mzd2rOP1#Kr)|5LM8r37_ zE>c+7*RvWAcp2}Ecnk(QAL3abLKX1L~d1RP}nqFqUt*XudgVmP54<}!^r)|*S$7w%6D+^ zURD&9!Bkm3%^+s@ruU6Pm_Ah*c4p59tE?Im)02mhKpt{M@m9=>UU>Vk`~9{YRFJ|U zzWx5bm~uzCTv|}Eiju)o1^mk;+dY;w4?ukBlPlxjLvwQ(RaILHZksrzE0q`=)&+_u zogt2U0=bSo&>p9lJ%8m}gV1!rbCMsG#zi{Q%->i!-XG??1ta@n&9G9lR0sHxNZOFl z-`>?uKalUbr-XDDE-BxI3n;UNRd>!?qBF+w*B&5N7{eRieZ%3*73Gt;#VIK|3J^L; zS@Nx;eDOzK;PssG{_WFxgdY*=sZ=E|zr7X7ZH=j|MYiP6HtC2Qq=dJD=9~y9npfMC z*z;r_fa#V$gbtv4^&cY9?gpAemJ>c*{w3BxRmz}V@;Gffh`fARewBD~I3u|Rq|B!b ziZP2a|8Ju5f9TGvhg_<6$(JOH+dbUz(F%~a=pZm+L@=1p+7oBB33-_`$niANy9cAJ z8erztSqMc-QMW}(o1yjUmDI!a3kcVMz9>fx|GN3X@cmIH$-$J2lf#q=);R%GU#Xj@ zS)=S@8`DQ?einOO4e$MaR`Agh4kQ{D22xz&xJ z3~8}fMbP?T{vP)tpSMOsHDa6Iw0g0n6VQCbLPKal0RhoCp5sQf+m@*O!C7D+#gvJ8 zm1!e;LfEBES0WK_>UZAPm_JF{-OCAiRUO5*S$PuB_jjQfJo@)DO`lDH6-DL|lBw%inR(V=p{Q8YhT7iWg3(7(;YN%PY*DZX_+JDAB7jaNwvNSPE z`jqZ7$7(l1B}7`ptI5~8S66A2S>$|Jv(4yp>QCUn zcn{?au2mhogq_XOidkh2ptk84_VCe??|c*z<=zm1Xh{o{4c$4i5w0 zzn509jxsY?tI#qh>#IGrb#ZL5HFmjb_S+ITtAqJHC?U#ZifP&wjqca-bbDCEWA;dD z9JzJCqazzYT4>%uM+J|4C}js~LYt?bl*^c!fIY-ksS%rCh9pddOA!vcyxpucaq2t* zGS*m_&QTFF-Z3$~#&Cm=aBxyAP;aAixZ-n<{T3f$v&~Ymy|G$thkUs4wX4Xmnm52D zvW=(0APM7Jupc66Z~U(8JR8+z>B{dm0cj1ohdAe34?%eP&f=~lgy>s-1D zi$!$DI#PPo-D~A{)85b5?1;pZ!86o|SY-GzuL&ECy3q(jNWSCS8r4_J7gmVUGQ~jV z2NJwa!9ZY%oo;bh)Fn5!*`ptQarI^U;J>$63&Q$3RCz1@+}NQWqc$!MmDX+dc&S-7 zYs_^1d)95di8;tgpv*falTTtX^AbsTRFPZR#tymtNJ;aTg`dJTX_T>!jku}A7w;xN zGMsSc8x}VXVYhj6YcA%j1mR+%4I4x2H?;#rC@b%OBKxklr6cqAI!;VY9TrIKu9)BG zU9EiT>OBvD+nv-3$zIp}NP}i(THXntGFkZBD0}oL$o|U*>{1X@&-Hy?m?<% z-|V1Dj`*~I$@9LAkVJ`6+avRrb``Ib-@)SEXZyw-OdpPY8df%9gkpeyX`dOqZ9gc8 z#({O+KXis>Ds*xXrdq7>Zs)SO&EEkqR>n+^ttpik?^5mof&EWL4F1&LM@l=Se}>*4 z;_c0PW7PP|3Odh`eK;XuyrW{|#@~OG82)1vIxri&Hkgxh(&~|_rJ?4q`gLMoiDDjB ztPb_{WPE4W-F5hhrY}axFyEYjLx^(i^R8lp*)-xsI0H@Dro z7?zc}TS>-|J>$}aQBeqB%7^UILkvG=d*HLmt$`Nr=aHH%k7gEUq?Ogcf%axo$w=>7 z5Qf=WFNssq|I2aDuWQl1ixsUm%CEt>IR{c{?Il~aY-V1QC`KUqW2)RtJ!5lhU_@J+ z>%L90jEt-3(PujT{cqNMyLeAE)z%x(Y+o{OwiF>7FCG}jrQqjU_zlK5k zv)*=VXBXBu?S_D#=poUe`{7wp?~4}EdQ-!v_FMtLs>JbzP9k%MtHli+uU7)*6($5= zZ6+tAzG#das<56_7+pDMrLVTOOfe5K2lp!~ZF6FdUF0%RMCEVC28PYDx&f4L5=Ij_ z>iob#Ud`};m0o^$TG_jkYC5BJ;;zZ++3eM{(sD ze2PS=i-VaBg3GOIh1QX$v;+2s{^h4-hvhC zk?oLCwUKQ_ZFJeG=^aDi{j9Kvii$JW`C0OhEA7>(^Ast*PoU;MGGVjfGKKd&xKf0X zBXuUS?Cir{9vTJb-8jlrlsM=S1egG&ugs*ChVCcz8{`(a@$gW}b};+yMXd*GnIV)_^e6a~C?^r;?(Lkjv}+bgXItPWju2aw233?9VGz$)1407n zVlRTT4agjpp!POpM8w#0b#*9@w5U{z;QFs-&G>S(a7X7}Zu&B^1ZT{9LIQE_d=MY2 z5Y8ue%OUhaT|{abl@T(9tb~N|?u7lCgm>{-S@%_dK%vi%0#HqnTj&?6x;I&z!@#(3 zwOz`(a7SxcbZUC$OsM@5FEKnbKQ~9C8Oa%7`0Lxk zJma3MlN{h3QLWyL<`qJm`I|#czd+}cb1bh9HhR7Cx_l}i{GFhhcTJ&>-XnYtjI2Ez z5iSrZmz;pT733g&?r5R;gq%7bQ@98^>T$8+39(yr8~HgoMfj7n*k)euR&99_OD}ka zS9{bA$x5^$;@B%M-9n(ln?e+ zyl*M&y0*-KZfu6?V~q9asMq5XV<|r#3fJ4=47FQd!itJ~bP@Z{VlAEsTL!tBm%}Fq z(|lM$)X&+rDM(pa6MmR%dP%KlX>>X1_QPeLp`SJJ=SKy@&c^v;svl`}I&LZwXno+a zztq07nNgAM4$UR@EWEtr%0)Gl@ZDQ}#&&*65$VKU$v_@SbnElG`~BILdhik%r8m;( z>Ph!?_OFkJ*dofpby{W}El!XVFk>qU zM4ryZrQ8m1txR8jslmOxCYv(6=-qe0E6sFn634w=kExG23dsic`tNFE~BeI1EU% z1p&SZunzq{pmmqhy>L$ZWh>USOE(RK09Vu{=l>Mxw!-~CWtXYIo zPmZ1Vq!^agP2P)-@kGr@uDMQB-KN2-y@S!saI!NDAr6$PcBpRi!aN8h-Sv;_9DUyy z__irkYqR1$VEU_1HY+d^_9 zqk#aC?5lciP&f-#XP#rf6TClomt?UqQtMr;uSD~Hs;OFCpU}|EZ0ffS$!(g((=WA+ z+uM$wx0ERp(G9GZD@3mkh^F{bBAy0h%oArGD-d`~lX*5#Rha9vz}eq?TtcG@@dvhx z*n%Y|ttoS@Ha9@0f^_Qa-8{fl1f>dIueRUr(UpXE?tAN*1PA*J^bjQ>u|s>adQjq~ zXZ6HIW2UqRI1d?j=gnZ@7Z_>MH{9hd&1c|j;Aw&#cI*3uwzHG$H8)k~Ku#A@r3vE7`l)m%2eWyi89#WBvyq>t7X7AF$m10q!9!DxzW)?Fmhwef$n`4zLrn?1 z%IL~ump&ma71V+x#j${~?@Xj_WdET8!3Oj>g&4Yvf_!S(ai<_57VD0)KCa!TV{$nS zm~)tbrd=IgO_&6NA>W>tneK)unx#&yJJm-2F+JO@rlq0oJzw0^ z?N>It7pvwQ`EByj2B0Da+Simw*jpD-sQtu$xZ?5MRhaYmcFo zqZf311H&GNROQ4jL~K*%8v7V}ev8G-8T(#sP0i=Pu4;wp4Zl*4S@3TZMX)%xIOld( zqXy2rRR(hJ`VhGvM|=hWZP@$)4huAeFYZ;g8(bFK7Z^Uvo0bzr+q=l z-vA8WF!8E!y}!d{$(>IDs>P!53$S zLPdZ{i>Sw#w`ZgV?_JNd_;D*pgA#GBJ8qK5HbB9q`>ETuJwaM33W##0>))UL>?HYYBBwnv-U?+=+-0Ix_c5?7sCIr## zs`2sZ*RA!(ZaREck(3|6OQ+4Ro{R7xunw_RPd`);g%P_-||R?b(-$HoNh#)O@tsygw5S@3pVK< zzt#7re_7R!7(O+#WMyNOZ)SGtl(nV@J6Q{2CK~m=hn-Mt^q7+3vVxs4Zf?0rx(vee z^Ixd>Hq>tf1sCJdwfMCz*{Te7<^?I5UIjV7ymY_Yc&x|5EYI8Q)9yAk7k~X9jyHl+ z_}uP7oP}00v?Szm;d|Zw-VrXvpvreg)-bJRzGGFqHUs3e@)s=ijCTQqYhe?3&Sykg zj)G0Uv!wv}2 z4=eu7o)U}WRNSQtJMihcVDQx+dq!IF7Kh+VgMm2ATW)2`&j+y87NyxFA^akG82HLa zbK!60x~KXhm86GX7C6a^oNV59@APqh_GoH4aoXl^L56XsK_x6h=VoM~JHj#K9fR&h zjdX<^bIDVw8kTq(CH+Fe_6w~es4>Fw!)tjJyX_8C zJ=$JF8$zjsbei_{I+&BShvN0@XXIbi-lgoNF~R<3aTx!bj=yi4J*06Nb1xScy>o)Q zjP1Di8W6uf0hI)YeZ7;Or0xdz#?CbYJOeHr0f_&fp$ELzXwt*=lfg#W( m?$!Q}V*Iab{O2Km{}8-$!#4BCvxS3K5CAc^HY4A-ANk)54-ZcO literal 0 HcmV?d00001 diff --git a/docs/develop/index.rst b/docs/develop/index.rst index f1fd0692ec9e1..7a6a6be67c5b9 100644 --- a/docs/develop/index.rst +++ b/docs/develop/index.rst @@ -1,14 +1,27 @@ -Developing and building MicroPython -=================================== +MicroPython Internals +===================== -This chapter describes some options for extending MicroPython in C. Note -that it doesn't aim to be a complete guide for developing with MicroPython. -See the `getting started guide -`_ for further information. +This chapter covers a tour of MicroPython from the perspective of a developer, contributing +to MicroPython. It acts as a comprehensive resource on the implementation details of MicroPython +for both novice and expert contributors. + +Development around MicroPython usually involves modifying the core runtime, porting or +maintaining a new library. This guide describes at great depth, the implementation +details of MicroPython including a getting started guide, compiler internals, porting +MicroPython to a new platform and implementing a core MicroPython library. .. toctree:: - :maxdepth: 1 + :maxdepth: 3 - cmodules.rst + gettingstarted.rst + writingtests.rst + compiler.rst + memorymgt.rst + library.rst + optimizations.rst qstr.rst - natmod.rst + maps.rst + publiccapi.rst + extendingmicropython.rst + porting.rst + \ No newline at end of file diff --git a/docs/develop/library.rst b/docs/develop/library.rst new file mode 100644 index 0000000000000..bebddcc8a3605 --- /dev/null +++ b/docs/develop/library.rst @@ -0,0 +1,86 @@ +.. _internals_library: + +Implementing a Module +===================== + +This chapter details how to implement a core module in MicroPython. +MicroPython modules can be one of the following: + +- Built-in module: A general module that is be part of the MicroPython repository. +- User module: A module that is useful for your specific project that you maintain + in your own repository or private codebase. +- Dynamic module: A module that can be deployed and imported at runtime to your device. + +A module in MicroPython can be implemented in one of the following locations: + +- py/: A core library that mirrors core CPython functionality. +- extmod/: A CPython or MicroPython-specific module that is shared across multiple ports. +- ports//: A port-specific module. + +.. note:: + This chapter describes modules implemented in ``py/`` or core modules. + See :ref:`extendingmicropython` for details on implementing an external module. + For details on port-specific modules, see :ref:`porting_to_a_board`. + +Implementing a core module +-------------------------- + +Like CPython, MicroPython has core builtin modules that can be accessed through import statements. +An example is the ``gc`` module discussed in :ref:`memorymanagement`. + +.. code-block:: bash + + >>> import gc + >>> gc.enable() + >>> + +MicroPython has several other builtin standard/core modules like ``io``, ``uarray`` etc. +Adding a new core module involves several modifications. + +First, create the ``C`` file in the ``py/`` directory. In this example we are adding a +hypothetical new module ``subsystem`` in the file ``modsubsystem.c``: + +.. code-block:: c + + #include "py/builtin.h" + #include "py/runtime.h" + + #if MICROPY_PY_SUBSYSTEM + + // info() + STATIC mp_obj_t py_subsystem_info(void) { + return MP_OBJ_NEW_SMALL_INT(42); + } + MP_DEFINE_CONST_FUN_OBJ_0(subsystem_info_obj, py_subsystem_info); + + STATIC const mp_rom_map_elem_t mp_module_subsystem_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_subsystem) }, + { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&subsystem_info_obj) }, + }; + STATIC MP_DEFINE_CONST_DICT(mp_module_subsystem_globals, mp_module_subsystem_globals_table); + + const mp_obj_module_t mp_module_subsystem = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&mp_module_subsystem_globals, + }; + + MP_REGISTER_MODULE(MP_QSTR_subsystem, mp_module_subsystem, MICROPY_PY_SUBSYSTEM); + + #endif + +The implementation includes a definition of all functions related to the module and adds the +functions to the module's global table in ``mp_module_subsystem_globals_table``. It also +creates the module object with ``mp_module_subsystem``. The module is then registered with +the wider system via the ``MP_REGISTER_MODULE`` macro. + +After building and running the modified MicroPython, the module should now be importable: + +.. code-block:: bash + + >>> import subsystem + >>> subsystem.info() + 42 + >>> + +Our ``info()`` function currently returns just a single number but can be extended +to do anything. Similarly, more functions can be added to this new module. diff --git a/docs/develop/maps.rst b/docs/develop/maps.rst new file mode 100644 index 0000000000000..8f899fa1d3462 --- /dev/null +++ b/docs/develop/maps.rst @@ -0,0 +1,63 @@ +.. _maps: + +Maps and Dictionaries +===================== + +MicroPython dictionaries and maps use techniques called open addressing and linear probing. +This chapter details both of these methods. + +Open addressing +--------------- + +`Open addressing `_ is used to resolve collisions. +Collisions are very common occurrences and happen when two items happen to hash to the same +slot or location. For example, given a hash setup as this: + +.. image:: img/collision.png + +If there is a request to fill slot ``0`` with ``70``, since the slot ``0`` is not empty, open addressing +finds the next available slot in the dictionary to service this request. This sequential search for an alternate +location is called *probing*. There are several sequence probing algorithms but MicroPython uses +linear probing that is described in the next section. + +Linear probing +-------------- + +Linear probing is one of the methods for finding an available address or slot in a dictionary. In MicroPython, +it is used with open addressing. To service the request described above, unlike other probing algorithms, +linear probing assumes a fixed interval of ``1`` between probes. The request will therefore be serviced by +placing the item in the next free slot which is slot ``4`` in our example: + +.. image:: img/linprob.png + +The same methods i.e open addressing and linear probing are used to search for an item in a dictionary. +Assume we want to search for the data item ``33``. The computed hash value will be 2. Looking at slot 2 +reveals ``33``, at this point, we return ``True``. Searching for ``70`` is quite different as there was a +collision at the time of insertion. Therefore computing the hash value is ``0`` which is currently +holding ``44``. Instead of simply returning ``False``, we perform a sequential search starting at point +``1`` until the item ``70`` is found or we encounter a free slot. This is the general way of performing +look-ups in hashes: + +.. code-block:: c + + // not yet found, keep searching in this table + pos = (pos + 1) % set->alloc; + + if (pos == start_pos) { + // search got back to starting position, so index is not in table + if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { + if (avail_slot != NULL) { + // there was an available slot, so use that + set->used++; + *avail_slot = index; + return index; + } else { + // not enough room in table, rehash it + mp_set_rehash(set); + // restart the search for the new element + start_pos = pos = hash % set->alloc; + } + } + } else { + return MP_OBJ_NULL; + } diff --git a/docs/develop/memorymgt.rst b/docs/develop/memorymgt.rst new file mode 100644 index 0000000000000..5b1690cc827b3 --- /dev/null +++ b/docs/develop/memorymgt.rst @@ -0,0 +1,141 @@ +.. _memorymanagement: + +Memory Management +================= + +Unlike programming languages such as C/C++, MicroPython hides memory management +details from the developer by supporting automatic memory management. +Automatic memory management is a technique used by operating systems or applications to automatically manage +the allocation and deallocation of memory. This eliminates challenges such as forgetting to +free the memory allocated to an object. Automatic memory management also avoids the critical issue of using memory +that is already released. Automatic memory management takes many forms, one of them being +garbage collection (GC). + +The garbage collector usually has two responsibilities; + +#. Allocate new objects in available memory. +#. Free unused memory. + +There are many GC algorithms but MicroPython uses the +`Mark and Sweep `_ +policy for managing memory. This algorithm has a mark phase that traverses the heap marking all +live objects while the sweep phase goes through the heap reclaiming all unmarked objects. + +Garbage collection functionality in MicroPython is available through the ``gc`` built-in +module: + +.. code-block:: bash + + >>> x = 5 + >>> x + 5 + >>> import gc + >>> gc.enable() + >>> gc.mem_alloc() + 1312 + >>> gc.mem_free() + 2071392 + >>> gc.collect() + 19 + >>> gc.disable() + >>> + +Even when ``gc.disable()`` is invoked, collection can be triggered with ``gc.collect()``. + +The object model +---------------- + +All MicroPython objects are referred to by the ``mp_obj_t`` data type. +This is usually word-sized (i.e. the same size as a pointer on the target architecture), +and can be typically 32-bit (STM32, nRF, ESP32, Unix x86) or 64-bit (Unix x64). +It can also be greater than a word-size for certain object representations, for +example ``OBJ_REPR_D`` has a 64-bit sized ``mp_obj_t`` on a 32-bit architecture. + +An ``mp_obj_t`` represents a MicroPython object, for example an integer, float, type, dict or +class instance. Some objects, like booleans and small integers, have their value stored directly +in the ``mp_obj_t`` value and do not require additional memory. Other objects have their value +store elsewhere in memory (for example on the garbage-collected heap) and their ``mp_obj_t`` contains +a pointer to that memory. A portion of ``mp_obj_t`` is the tag which tells what type of object it is. + +See ``py/mpconfig.h`` for the specific details of the available representations. + +**Pointer tagging** + +Because pointers are word-aligned, when they are stored in an ``mp_obj_t`` the +lower bits of this object handle will be zero. For example on a 32-bit architecture +the lower 2 bits will be zero: + +``********|********|********|******00`` + +These bits are reserved for purposes of storing a tag. The tag stores extra information as +opposed to introducing a new field to store that information in the object, which may be +inefficient. In MicroPython the tag tells if we are dealing with a small integer, interned +(small) string or a concrete object, and different semantics apply to each of these. + +For small integers the mapping is this: + +``********|********|********|*******1`` + +Where the asterisks hold the actual integer value. For an interned string or an immediate +object (e.g. ``True``) the layout of the ``mp_obj_t`` value is, respectively: + +``********|********|********|*****010`` + +``********|********|********|*****110`` + +While a concrete object that is none of the above takes the form: + +``********|********|********|******00`` + +The stars here correspond to the address of the concrete object in memory. + +Allocation of objects +---------------------- + +The value of a small integer is stored directly in the ``mp_obj_t`` and will be +allocated in-place, not on the heap or elsewhere. As such, creation of small +integers does not affect the heap. Similarly for interned strings that already have +their textual data stored elsewhere, and immediate values like ``None``, ``False`` +and ``True``. + +Everything else which is a concrete object is allocated on the heap and its object structure is such that +a field is reserved in the object header to store the type of the object. + +.. code-block:: bash + + +++++++++++ + + + + + type + object header + + + + +++++++++++ + + + object items + + + + + + + +++++++++++ + +The heap's smallest unit of allocation is a block, which is four machine words in +size (16 bytes on a 32-bit machine, 32 bytes on a 64-bit machine). +Another structure also allocated on the heap tracks the allocation of +objects in each block. This structure is called a *bitmap*. + +.. image:: img/bitmap.png + +The bitmap tracks whether a block is "free" or "in use" and use two bits to track this state +for each block. + +The mark-sweep garbage collector manages the objects allocated on the heap, and also +utilises the bitmap to mark objects that are still in use. +See `py/gc.c `_ +for the full implementation of these details. + +**Allocation: heap layout** + +The heap is arranged such that it consists of blocks in pools. A block +can have different properties: + +- *ATB(allocation table byte):* If set, then the block is a normal block +- *FREE:* Free block +- *HEAD:* Head of a chain of blocks +- *TAIL:* In the tail of a chain of blocks +- *MARK :* Marked head block +- *FTB(finaliser table byte):* If set, then the block has a finaliser diff --git a/docs/develop/optimizations.rst b/docs/develop/optimizations.rst new file mode 100644 index 0000000000000..d972cde66616a --- /dev/null +++ b/docs/develop/optimizations.rst @@ -0,0 +1,72 @@ +.. _optimizations: + +Optimizations +============= + +MicroPython uses several optimizations to save RAM but also ensure the efficient +execution of programs. This chapter discusses some of these optimizations. + +.. note:: + :ref:`qstr` and :ref:`maps` details other optimizations on strings and + dictionaries. + +Frozen bytecode +--------------- + +When MicroPython loads Python code from the filesystem, it first has to parse the file into +a temporary in-memory representation, and then generate bytecode for execution, both of which +are stored in the heap (in RAM). This can lead to significant amounts of memory being used. +The MicroPython cross compiler can be used to generate +a ``.mpy`` file, containing the pre-compiled bytecode for a Python module. This will still +be loaded into RAM, but it avoids the additional overhead of the parsing stage. + +As a further optimisation, the pre-compiled bytecode from a ``.mpy`` file can be "frozen" +into the firmware image as part of the main firmware compilation process, which means that +the bytecode will be executed from ROM. This can lead to a significant memory saving, and +reduce heap fragmentation. + +Variables +--------- + +MicroPython processes local and global variables differently. Global variables +are stored and looked up from a global dictionary that is allocated on the heap +(note that each module has its own separate dict, so separate namespace). +Local variables on the other hand are are stored on the Python value stack, which may +live on the C stack or on the heap. They are accessed directly by their offset +within the Python stack, which is more efficient than a global lookup in a dict. + +The length of global variable names also affects how much RAM is used as identifiers +are stored in RAM. The shorter the identifier, the less memory is used. + +The other aspect is that ``const`` variables that start with an underscore are treated as +proper constants and are not allocated or added in a dictionary, hence saving some memory. +These variables use ``const()`` from the MicroPython library. Therefore: + +.. code-block:: python + + from micropython import const + + X = const(1) + _Y = const(2) + foo(X, _Y) + +Compiles to: + +.. code-block:: python + + X = 1 + foo(1, 2) + +Allocation of memory +-------------------- + +Most of the common MicroPython constructs are not allocated on the heap. +However the following are: + +- Dynamic data structures like lists, mappings, etc; +- Functions, classes and object instances; +- imports; and +- First-time assignment of global variables (to create the slot in the global dict). + +For a detailed discussion on a more user-centric perspective on optimization, +see `Maximising MicroPython speed `_ diff --git a/docs/develop/porting.rst b/docs/develop/porting.rst new file mode 100644 index 0000000000000..59dd570008bcd --- /dev/null +++ b/docs/develop/porting.rst @@ -0,0 +1,310 @@ +.. _porting_to_a_board: + +Porting MicroPython +=================== + +The MicroPython project contains several ports to different microcontroller families and +architectures. The project repository has a `ports `_ +directory containing a subdirectory for each supported port. + +A port will typically contain definitions for multiple "boards", each of which is a specific piece of +hardware that that port can run on, e.g. a development kit or device. + +The `minimal port `_ is +available as a simplified reference implementation of a MicroPython port. It can run on both the +host system and an STM32F4xx MCU. + +In general, starting a port requires: + +- Setting up the toolchain (configuring Makefiles, etc). +- Implementing boot configuration and CPU initialization. +- Initialising basic drivers required for development and debugging (e.g. GPIO, UART). +- Performing the board-specific configurations. +- Implementing the port-specific modules. + +Minimal MicroPython firmware +---------------------------- + +The best way to start porting MicroPython to a new board is by integrating a minimal +MicroPython interpreter. For this walkthrough, create a subdirectory for the new +port in the ``ports`` directory: + +.. code-block:: bash + + $ cd ports + $ mkdir example_port + +The basic MicroPython firmware is implemented in the main port file, e.g ``main.c``: + +.. code-block:: c + + #include "py/compile.h" + #include "py/gc.h" + #include "py/mperrno.h" + #include "py/stackctrl.h" + #include "lib/utils/gchelper.h" + #include "lib/utils/pyexec.h" + + // Allocate memory for the MicroPython GC heap. + static char heap[4096]; + + int main(int argc, char **argv) { + // Initialise the MicroPython runtime. + mp_stack_ctrl_init(); + gc_init(heap, heap + sizeof(heap)); + mp_init(); + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0); + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); + + // Start a normal REPL; will exit when ctrl-D is entered on a blank line. + pyexec_friendly_repl(); + + // Deinitialise the runtime. + gc_sweep_all(); + mp_deinit(); + return 0; + } + + // Handle uncaught exceptions (should never be reached in a correct C implementation). + void nlr_jump_fail(void *val) { + for (;;) { + } + } + + // Do a garbage collection cycle. + void gc_collect(void) { + gc_collect_start(); + gc_helper_collect_regs_and_stack(); + gc_collect_end(); + } + + // There is no filesystem so stat'ing returns nothing. + mp_import_stat_t mp_import_stat(const char *path) { + return MP_IMPORT_STAT_NO_EXIST; + } + + // There is no filesystem so opening a file raises an exception. + mp_lexer_t *mp_lexer_new_from_file(const char *filename) { + mp_raise_OSError(MP_ENOENT); + } + +We also need a Makefile at this point for the port: + +.. code-block:: Makefile + + # Include the core environment definitions; this will set $(TOP). + include ../../py/mkenv.mk + + # Include py core make definitions. + include $(TOP)/py/py.mk + + # Set CFLAGS and libraries. + CFLAGS = -I. -I$(BUILD) -I$(TOP) + LIBS = -lm + + # Define the required source files. + SRC_C = \ + main.c \ + mphalport.c \ + lib/mp-readline/readline.c \ + lib/utils/gchelper_generic.c \ + lib/utils/pyexec.c \ + lib/utils/stdout_helpers.c \ + + # Define the required object files. + OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) + + # Define the top-level target, the main firmware. + all: $(BUILD)/firmware.elf + + # Define how to build the firmware. + $(BUILD)/firmware.elf: $(OBJ) + $(ECHO) "LINK $@" + $(Q)$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) + $(Q)$(SIZE) $@ + + # Include remaining core make rules. + include $(TOP)/py/mkrules.mk + +Remember to use proper tabs to indent the Makefile. + +MicroPython Configurations +-------------------------- + +After integrating the minimal code above, the next step is to create the MicroPython +configuration files for the port. The compile-time configurations are specified in +``mpconfigport.h`` and additional hardware-abstraction functions, such as time keeping, +in ``mphalport.h``. + +The following is an example of an ``mpconfigport.h`` file: + +.. code-block:: c + + #include + + // Python internal features. + #define MICROPY_ENABLE_GC (1) + #define MICROPY_HELPER_REPL (1) + #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) + #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) + + // Fine control over Python builtins, classes, modules, etc. + #define MICROPY_PY_ASYNC_AWAIT (0) + #define MICROPY_PY_BUILTINS_SET (0) + #define MICROPY_PY_ATTRTUPLE (0) + #define MICROPY_PY_COLLECTIONS (0) + #define MICROPY_PY_MATH (0) + #define MICROPY_PY_IO (0) + #define MICROPY_PY_STRUCT (0) + + // Type definitions for the specific machine. + + typedef intptr_t mp_int_t; // must be pointer size + typedef uintptr_t mp_uint_t; // must be pointer size + typedef long mp_off_t; + + // We need to provide a declaration/definition of alloca(). + #include + + // Define the port's name and hardware. + #define MICROPY_HW_BOARD_NAME "example-board" + #define MICROPY_HW_MCU_NAME "unknown-cpu" + + #define MP_STATE_PORT MP_STATE_VM + + #define MICROPY_PORT_ROOT_POINTERS \ + const char *readline_hist[8]; + +This configuration file contains machine-specific configurations including aspects like if different +MicroPython features should be enabled e.g. ``#define MICROPY_ENABLE_GC (1)``. Making this Setting +``(0)`` disables the feature. + +Other configurations include type definitions, root pointers, board name, microcontroller name +etc. + +Similarly, an minimal example ``mphalport.h`` file looks like this: + +.. code-block:: c + + static inline void mp_hal_set_interrupt_char(char c) {} + +Support for standard input/output +--------------------------------- + +MicroPython requires at least a way to output characters, and to have a REPL it also +requires a way to input characters. Functions for this can be implemented in the file +``mphalport.c``, for example: + +.. code-block:: c + + #include + #include "py/mpconfig.h" + + // Receive single character, blocking until one is available. + int mp_hal_stdin_rx_chr(void) { + unsigned char c = 0; + int r = read(STDIN_FILENO, &c, 1); + (void)r; + return c; + } + + // Send the string of given length. + void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + int r = write(STDOUT_FILENO, str, len); + (void)r; + } + +These input and output functions have to be modified depending on the +specific board API. This example uses the standard input/output stream. + +Building and running +-------------------- + +At this stage the directory of the new port should contain:: + + ports/example_port/ + ├── main.c + ├── Makefile + ├── mpconfigport.h + ├── mphalport.c + └── mphalport.h + +The port can now be built by running ``make`` (or otherwise, depending on your system). + +If you are using the default compiler settings in the Makefile given above then this +will create an executable called ``build/firmware.elf`` which can be executed directly. +To get a functional REPL you may need to first configure the terminal to raw mode: + +.. code-block:: bash + + $ stty raw opost -echo + $ ./build/firmware.elf + +That should give a MicroPython REPL. You can then run commands like: + +.. code-block:: bash + + MicroPython v1.13 on 2021-01-01; example-board with unknown-cpu + >>> import usys + >>> usys.implementation + ('micropython', (1, 13, 0)) + >>> + +Use Ctrl-D to exit, and then run ``reset`` to reset the terminal. + +Adding a module to the port +--------------------------- + +To add a custom module like ``myport``, first add the module definition in a file +``modmyport.c``: + +.. code-block:: c + + #include "py/runtime.h" + + STATIC mp_obj_t myport_info(void) { + mp_printf(&mp_plat_print, "info about my port\n"); + return mp_const_none; + } + STATIC MP_DEFINE_CONST_FUN_OBJ_0(myport_info_obj, myport_info); + + STATIC const mp_rom_map_elem_t myport_module_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_myport) }, + { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&myport_info_obj) }, + }; + STATIC MP_DEFINE_CONST_DICT(myport_module_globals, myport_module_globals_table); + + const mp_obj_module_t myport_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&myport_module_globals, + }; + + MP_REGISTER_MODULE(MP_QSTR_myport, myport_module, 1); + +Note: the "1" as the third argument in ``MP_REGISTER_MODULE`` enables this new module +unconditionally. To allow it to be conditionally enabled, replace the "1" by +``MICROPY_PY_MYPORT`` and then add ``#define MICROPY_PY_MYPORT (1)`` in ``mpconfigport.h`` +accordingly. + +You will also need to edit the Makefile to add ``modmyport.c`` to the ``SRC_C`` list, and +a new line adding the same file to ``SRC_QSTR`` (so qstrs are searched for in this new file), +like this: + +.. code-block:: Makefile + + SRC_C = \ + main.c \ + modmyport.c \ + mphalport.c \ + ... + + SRC_QSTR += modport.c + +If all went correctly then, after rebuilding, you should be able to import the new module: + +.. code-block:: bash + + >>> import myport + >>> myport.info() + info about my port + >>> diff --git a/docs/develop/publiccapi.rst b/docs/develop/publiccapi.rst new file mode 100644 index 0000000000000..132c7b136bc14 --- /dev/null +++ b/docs/develop/publiccapi.rst @@ -0,0 +1,25 @@ +.. _publiccapi: + +The public C API +================ + +The public C-API comprises functions defined in all C header files in the ``py/`` +directory. Most of the important core runtime C APIs are exposed in ``runtime.h`` and +``obj.h``. + +The following is an example of public API functions from ``obj.h``: + +.. code-block:: c + + mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items); + mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg); + mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value); + void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items); + +At its core, any functions and macros in header files make up the public +API and can be used to access very low-level details of MicroPython. Static +inline functions in header files are fine too, such functions will be +inlined in the code when used. + +Header files in the ``ports`` directory are only exposed to the functionality +specific to a given port. diff --git a/docs/develop/qstr.rst b/docs/develop/qstr.rst index 3550a8bd425f2..cd1fc47862cf0 100644 --- a/docs/develop/qstr.rst +++ b/docs/develop/qstr.rst @@ -1,3 +1,5 @@ +.. _qstr: + MicroPython string interning ============================ @@ -57,6 +59,7 @@ Processing happens in the following stages: information. Note that this step only uses files that have changed, which means that ``qstr.i.last`` will only contain data from files that have changed since the last compile. + 2. ``qstr.split`` is an empty file created after running ``makeqstrdefs.py split`` on qstr.i.last. It's just used as a dependency to indicate that the step ran. This script outputs one file per input C file, ``genhdr/qstr/...file.c.qstr``, @@ -71,8 +74,8 @@ Processing happens in the following stages: data is written to another file (``qstrdefs.collected.h.hash``) which allows it to track changes across builds. -4. ``qstrdefs.preprocessed.h`` adds in the QSTRs from qstrdefs*. It - concatenates ``qstrdefs.collected.h`` with ``qstrdefs*.h``, then it transforms +4. Generate an enumeration, each entry of which maps a ``MP_QSTR_Foo`` to it's corresponding index. + It concatenates ``qstrdefs.collected.h`` with ``qstrdefs*.h``, then it transforms each line from ``Q(Foo)`` to ``"Q(Foo)"`` so they pass through the preprocessor unchanged. Then the preprocessor is used to deal with any conditional compilation in ``qstrdefs*.h``. Then the transformation is undone back to diff --git a/docs/develop/writingtests.rst b/docs/develop/writingtests.rst new file mode 100644 index 0000000000000..4bdf4dd7a620e --- /dev/null +++ b/docs/develop/writingtests.rst @@ -0,0 +1,70 @@ +.. _writingtests: + +Writing tests +============= + +Tests in MicroPython are located at the path ``tests/``. The following is a listing of +key directories and the run-tests runner script: + +.. code-block:: bash + + . + ├── basics + ├── extmod + ├── float + ├── micropython + ├── run-tests + ... + +There are subfolders maintained to categorize the tests. Add a test by creating a new file in one of the +existing folders or in a new folder. It's also possible to make custom tests outside this tests folder, +which would be recommended for a custom port. + +For example, add the following code in a file ``print.py`` in the ``tests/unix/`` subdirectory: + +.. code-block:: python + + def print_one(): + print(1) + + print_one() + +If you run your tests, this test should appear in the test output: + +.. code-block:: bash + + $ cd ports/unix + $ make tests + skip unix/extra_coverage.py + pass unix/ffi_callback.py + pass unix/ffi_float.py + pass unix/ffi_float2.py + pass unix/print.py + pass unix/time.py + pass unix/time2.py + +Tests are run by comparing the output from the test target against the output from CPython. +So any test should use print statements to indicate test results. + +For tests that can't be compared to CPython (i.e. micropython-specific functionality), +you can provide a ``.py.exp`` file which will be used as the truth for comparison. + +The other way to run tests, which is useful when running on targets other than the Unix port, is: + +.. code-block:: bash + + $ cd tests + $ ./run-tests + +Then to run on a board: + +.. code-block:: bash + + $ ./run-tests --target minimal --device /dev/ttyACM0 + +And to run only a certain set of tests (eg a directory): + +.. code-block:: bash + + $ ./run-tests -d basics + $ ./run-tests float/builtin*.py From 71ea438561b737de7d4bb6bc60412a7e409cf298 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Jan 2021 13:09:44 +1100 Subject: [PATCH 0522/3533] extmod/vfs: Check block 0 and 1 when auto-detecting littlefs. The superblock for littlefs is in block 0 and 1, but block 0 may be erased or partially written, so block 1 must be checked if block 0 does not have a valid littlefs superblock in it. Prior to this commit, the mount of a block device which auto-detected the filysystem type would fail for littlefs if block 0 did not contain a valid superblock. That is now fixed. Signed-off-by: Damien George --- extmod/vfs.c | 37 ++++++++++---------- tests/extmod/vfs_lfs_superblock.py | 47 ++++++++++++++++++++++++++ tests/extmod/vfs_lfs_superblock.py.exp | 2 ++ 3 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 tests/extmod/vfs_lfs_superblock.py create mode 100644 tests/extmod/vfs_lfs_superblock.py.exp diff --git a/extmod/vfs.c b/extmod/vfs.c index 7dca59d351b03..6dcdcfa22d3e6 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -160,27 +160,30 @@ STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) { #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { - mp_obj_t vfs = MP_OBJ_NULL; + // The superblock for littlefs is in both block 0 and 1, but block 0 may be erased + // or partially written, so search both blocks 0 and 1 for the littlefs signature. mp_vfs_blockdev_t blockdev; mp_vfs_blockdev_init(&blockdev, bdev_obj); uint8_t buf[44]; - mp_vfs_blockdev_read_ext(&blockdev, 0, 8, sizeof(buf), buf); - #if MICROPY_VFS_LFS1 - if (memcmp(&buf[32], "littlefs", 8) == 0) { - // LFS1 - vfs = mp_type_vfs_lfs1.make_new(&mp_type_vfs_lfs1, 1, 0, &bdev_obj); - nlr_pop(); - return vfs; - } - #endif - #if MICROPY_VFS_LFS2 - if (memcmp(&buf[0], "littlefs", 8) == 0) { - // LFS2 - vfs = mp_type_vfs_lfs2.make_new(&mp_type_vfs_lfs2, 1, 0, &bdev_obj); - nlr_pop(); - return vfs; + for (size_t block_num = 0; block_num <= 1; ++block_num) { + mp_vfs_blockdev_read_ext(&blockdev, block_num, 8, sizeof(buf), buf); + #if MICROPY_VFS_LFS1 + if (memcmp(&buf[32], "littlefs", 8) == 0) { + // LFS1 + mp_obj_t vfs = mp_type_vfs_lfs1.make_new(&mp_type_vfs_lfs1, 1, 0, &bdev_obj); + nlr_pop(); + return vfs; + } + #endif + #if MICROPY_VFS_LFS2 + if (memcmp(&buf[0], "littlefs", 8) == 0) { + // LFS2 + mp_obj_t vfs = mp_type_vfs_lfs2.make_new(&mp_type_vfs_lfs2, 1, 0, &bdev_obj); + nlr_pop(); + return vfs; + } + #endif } - #endif nlr_pop(); } else { // Ignore exception (eg block device doesn't support extended readblocks) diff --git a/tests/extmod/vfs_lfs_superblock.py b/tests/extmod/vfs_lfs_superblock.py new file mode 100644 index 0000000000000..1ac5675554b46 --- /dev/null +++ b/tests/extmod/vfs_lfs_superblock.py @@ -0,0 +1,47 @@ +# Test for VfsLfs using a RAM device, when the first superblock does not exist + +try: + import uos + + uos.VfsLfs2 +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMBlockDevice: + def __init__(self, block_size, data): + self.block_size = block_size + self.data = data + + def readblocks(self, block, buf, off): + addr = block * self.block_size + off + for i in range(len(buf)): + buf[i] = self.data[addr + i] + + def ioctl(self, op, arg): + if op == 4: # block count + return len(self.data) // self.block_size + if op == 5: # block size + return self.block_size + if op == 6: # erase block + return 0 + + +# This is a valid littlefs2 filesystem with a block size of 64 bytes. +# The first block (where the first superblock is stored) is fully erased. +lfs2_data = b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02\x00\x00\x00\xf0\x0f\xff\xf7littlefs/\xe0\x00\x10\x00\x00\x02\x00@\x00\x00\x00\x04\x00\x00\x00\xff\x00\x00\x00\xff\xff\xff\x7f\xfe\x03\x00\x00p\x1f\xfc\x08\x1b\xb4\x14\xa7\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01\x00\x00\x00\xff\xef\xff\xf7test.txt \x00\x00\x08p\x1f\xfc\x08\x83\xf1u\xba\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + +# Create the block device from the static data (it will be read-only). +bdev = RAMBlockDevice(64, lfs2_data) + +# Create the VFS explicitly, no auto-detection is needed for this. +vfs = uos.VfsLfs2(bdev) +print(list(vfs.ilistdir())) + +# Mount the block device directly; this relies on auto-detection. +uos.mount(bdev, "/userfs") +print(uos.listdir("/userfs")) + +# Clean up. +uos.umount("/userfs") diff --git a/tests/extmod/vfs_lfs_superblock.py.exp b/tests/extmod/vfs_lfs_superblock.py.exp new file mode 100644 index 0000000000000..c71bf50e82fd4 --- /dev/null +++ b/tests/extmod/vfs_lfs_superblock.py.exp @@ -0,0 +1,2 @@ +[] +[] From d1945cc2b56bb2159224fa7027d0aadb2362430c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Jan 2021 13:13:09 +1100 Subject: [PATCH 0523/3533] stm32/main: Check block 0 and 1 when auto-detecting littlefs. The superblock for littlefs is in block 0 and 1, but block 0 may be erased or partially written, so block 1 must be checked if block 0 does not have a valid littlefs superblock in it. Prior to this commit, if block 0 did not contain a valid littlefs superblock (but block 1 did) then the auto-detection would fail, mounting a FAT filesystem would also fail, and the system would reformat the flash, even though it may have contained a valid littlefs filesystem. This is now fixed. Signed-off-by: Damien George --- ports/stm32/main.c | 48 +++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index fb10b96501e1d..f0a10fa93359e 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -168,31 +168,35 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 - // Try to detect the block device used for the main filesystem, based on the first block - - uint8_t buf[64]; - ret = storage_readblocks_ext(buf, 0, 0, sizeof(buf)); + // Try to detect the block device used for the main filesystem based on the + // contents of the superblock, which can be the first or second block. mp_int_t len = -1; + uint8_t buf[64]; + for (size_t block_num = 0; block_num <= 1; ++block_num) { + ret = storage_readblocks_ext(buf, block_num, 0, sizeof(buf)); + + #if MICROPY_VFS_LFS1 + if (ret == 0 && memcmp(&buf[40], "littlefs", 8) == 0) { + // LFS1 + lfs1_superblock_t *superblock = (void *)&buf[12]; + uint32_t block_size = lfs1_fromle32(superblock->d.block_size); + uint32_t block_count = lfs1_fromle32(superblock->d.block_count); + len = block_count * block_size; + break; + } + #endif - #if MICROPY_VFS_LFS1 - if (ret == 0 && memcmp(&buf[40], "littlefs", 8) == 0) { - // LFS1 - lfs1_superblock_t *superblock = (void *)&buf[12]; - uint32_t block_size = lfs1_fromle32(superblock->d.block_size); - uint32_t block_count = lfs1_fromle32(superblock->d.block_count); - len = block_count * block_size; - } - #endif - - #if MICROPY_VFS_LFS2 - if (ret == 0 && memcmp(&buf[8], "littlefs", 8) == 0) { - // LFS2 - lfs2_superblock_t *superblock = (void *)&buf[20]; - uint32_t block_size = lfs2_fromle32(superblock->block_size); - uint32_t block_count = lfs2_fromle32(superblock->block_count); - len = block_count * block_size; + #if MICROPY_VFS_LFS2 + if (ret == 0 && memcmp(&buf[8], "littlefs", 8) == 0) { + // LFS2 + lfs2_superblock_t *superblock = (void *)&buf[20]; + uint32_t block_size = lfs2_fromle32(superblock->block_size); + uint32_t block_count = lfs2_fromle32(superblock->block_count); + len = block_count * block_size; + break; + } + #endif } - #endif if (len != -1) { // Detected a littlefs filesystem so create correct block device for it From c1eb2929279fdcf7f89adeb013290e7c45e24fb7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Jan 2021 15:37:00 +1100 Subject: [PATCH 0524/3533] stm32/mboot: Don't auto-detect littlefs block size. Instead it is now passed in as an optional parameter to the ELEM_MOUNT element, with a compile-time configurable default. Signed-off-by: Damien George --- ports/stm32/mboot/fsload.c | 25 +++++++++++++++++++++---- ports/stm32/mboot/vfs.h | 4 ++-- ports/stm32/mboot/vfs_lfs.c | 26 +------------------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index 591b670aa00c2..fe98426bed9cf 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -31,6 +31,11 @@ #include "pack.h" #include "vfs.h" +// Default block size used for mount operations if none given. +#ifndef MBOOT_FSLOAD_DEFAULT_BLOCK_SIZE +#define MBOOT_FSLOAD_DEFAULT_BLOCK_SIZE (4096) +#endif + #if MBOOT_FSLOAD #if !(MBOOT_VFS_FAT || MBOOT_VFS_LFS1 || MBOOT_VFS_LFS2) @@ -213,8 +218,19 @@ int fsload_process(void) { elem = ELEM_DATA_START; for (;;) { elem = elem_search(elem, ELEM_TYPE_MOUNT); - if (elem == NULL || elem[-1] != 10) { - // End of elements, or invalid MOUNT element + if (elem == NULL) { + // End of elements. + return -1; + } + uint32_t block_size; + if (elem[-1] == 10) { + // No block size given, use default. + block_size = MBOOT_FSLOAD_DEFAULT_BLOCK_SIZE; + } else if (elem[-1] == 14) { + // Block size given, extract it. + block_size = get_le32(&elem[10]); + } else { + // Invalid MOUNT element. return -1; } if (elem[0] == mount_point) { @@ -235,19 +251,20 @@ int fsload_process(void) { const stream_methods_t *methods; #if MBOOT_VFS_FAT if (elem[1] == ELEM_MOUNT_FAT) { + (void)block_size; ret = vfs_fat_mount(&ctx.fat, base_addr, byte_len); methods = &vfs_fat_stream_methods; } else #endif #if MBOOT_VFS_LFS1 if (elem[1] == ELEM_MOUNT_LFS1) { - ret = vfs_lfs1_mount(&ctx.lfs1, base_addr, byte_len); + ret = vfs_lfs1_mount(&ctx.lfs1, base_addr, byte_len, block_size); methods = &vfs_lfs1_stream_methods; } else #endif #if MBOOT_VFS_LFS2 if (elem[1] == ELEM_MOUNT_LFS2) { - ret = vfs_lfs2_mount(&ctx.lfs2, base_addr, byte_len); + ret = vfs_lfs2_mount(&ctx.lfs2, base_addr, byte_len, block_size); methods = &vfs_lfs2_stream_methods; } else #endif diff --git a/ports/stm32/mboot/vfs.h b/ports/stm32/mboot/vfs.h index 6cf883a13975a..22bb98936790e 100644 --- a/ports/stm32/mboot/vfs.h +++ b/ports/stm32/mboot/vfs.h @@ -65,7 +65,7 @@ typedef struct _vfs_lfs1_context_t { extern const stream_methods_t vfs_lfs1_stream_methods; -int vfs_lfs1_mount(vfs_lfs1_context_t *ctx, uint32_t base_addr, uint32_t byte_len); +int vfs_lfs1_mount(vfs_lfs1_context_t *ctx, uint32_t base_addr, uint32_t byte_len, uint32_t block_size); #endif @@ -89,7 +89,7 @@ typedef struct _vfs_lfs2_context_t { extern const stream_methods_t vfs_lfs2_stream_methods; -int vfs_lfs2_mount(vfs_lfs2_context_t *ctx, uint32_t base_addr, uint32_t byte_len); +int vfs_lfs2_mount(vfs_lfs2_context_t *ctx, uint32_t base_addr, uint32_t byte_len, uint32_t block_size); #endif diff --git a/ports/stm32/mboot/vfs_lfs.c b/ports/stm32/mboot/vfs_lfs.c index e4dc511db56f6..3e4c9caf16d87 100644 --- a/ports/stm32/mboot/vfs_lfs.c +++ b/ports/stm32/mboot/vfs_lfs.c @@ -43,10 +43,6 @@ #define VFS_LFSx_MOUNT vfs_lfs1_mount #define VFS_LFSx_STREAM_METHODS vfs_lfs1_stream_methods -#define SUPERBLOCK_MAGIC_OFFSET (40) -#define SUPERBLOCK_BLOCK_SIZE_OFFSET (28) -#define SUPERBLOCK_BLOCK_COUNT_OFFSET (32) - static uint8_t lfs_read_buffer[LFS_READ_SIZE]; static uint8_t lfs_prog_buffer[LFS_PROG_SIZE]; static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE / 8]; @@ -59,10 +55,6 @@ static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE / 8]; #define VFS_LFSx_MOUNT vfs_lfs2_mount #define VFS_LFSx_STREAM_METHODS vfs_lfs2_stream_methods -#define SUPERBLOCK_MAGIC_OFFSET (8) -#define SUPERBLOCK_BLOCK_SIZE_OFFSET (24) -#define SUPERBLOCK_BLOCK_COUNT_OFFSET (28) - static uint8_t lfs_read_buffer[LFS_CACHE_SIZE]; static uint8_t lfs_prog_buffer[LFS_CACHE_SIZE]; static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE]; @@ -90,23 +82,7 @@ static int dev_sync(const struct LFSx_API (config) * c) { return LFSx_MACRO(_ERR_OK); } -int VFS_LFSx_MOUNT(VFS_LFSx_CONTEXT_T *ctx, uint32_t base_addr, uint32_t byte_len) { - // Read start of superblock. - uint8_t buf[48]; - hw_read(base_addr, sizeof(buf), buf); - - // Verify littlefs and detect block size. - if (memcmp(&buf[SUPERBLOCK_MAGIC_OFFSET], "littlefs", 8) != 0) { - return -1; - } - uint32_t block_size = get_le32(&buf[SUPERBLOCK_BLOCK_SIZE_OFFSET]); - uint32_t block_count = get_le32(&buf[SUPERBLOCK_BLOCK_COUNT_OFFSET]); - - // Verify size of volume. - if (block_size * block_count != byte_len) { - return -1; - } - +int VFS_LFSx_MOUNT(VFS_LFSx_CONTEXT_T *ctx, uint32_t base_addr, uint32_t byte_len, uint32_t block_size) { ctx->bdev_base_addr = base_addr; struct LFSx_API (config) *config = &ctx->config; From 0efa0b54374e3ea0cbaf455c41945800479b5bc8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Jan 2021 15:46:02 +1100 Subject: [PATCH 0525/3533] stm32/mboot: Add ELEM_TYPE_STATUS element so application can get status. This new element takes the form: (ELEM_TYPE_STATUS, 4,
). If this element is present in the mboot command then mboot will store to the given address the result of the filesystem firmware update process. The address can for example be an RTC backup register. Signed-off-by: Damien George --- ports/stm32/boards/stm32f4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32h7xx_hal_conf_base.h | 1 + ports/stm32/mboot/main.c | 9 ++++++++- ports/stm32/mboot/mboot.h | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ports/stm32/boards/stm32f4xx_hal_conf_base.h b/ports/stm32/boards/stm32f4xx_hal_conf_base.h index 91f064835e03d..057a9e81e4cac 100644 --- a/ports/stm32/boards/stm32f4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f4xx_hal_conf_base.h @@ -54,6 +54,7 @@ #include "stm32f4xx_hal_usart.h" #include "stm32f4xx_hal_wwdg.h" #include "stm32f4xx_ll_adc.h" +#include "stm32f4xx_ll_pwr.h" #include "stm32f4xx_ll_rtc.h" // Enable various HAL modules diff --git a/ports/stm32/boards/stm32f7xx_hal_conf_base.h b/ports/stm32/boards/stm32f7xx_hal_conf_base.h index 1a3fca3ac86a8..6e7dff3042ff3 100644 --- a/ports/stm32/boards/stm32f7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f7xx_hal_conf_base.h @@ -54,6 +54,7 @@ #include "stm32f7xx_hal_usart.h" #include "stm32f7xx_hal_wwdg.h" #include "stm32f7xx_ll_adc.h" +#include "stm32f7xx_ll_pwr.h" #include "stm32f7xx_ll_rtc.h" // Enable various HAL modules diff --git a/ports/stm32/boards/stm32h7xx_hal_conf_base.h b/ports/stm32/boards/stm32h7xx_hal_conf_base.h index 231f1ac7f4d28..a451cfde76162 100644 --- a/ports/stm32/boards/stm32h7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32h7xx_hal_conf_base.h @@ -54,6 +54,7 @@ #include "stm32h7xx_hal_usart.h" #include "stm32h7xx_hal_wwdg.h" #include "stm32h7xx_ll_adc.h" +#include "stm32h7xx_ll_pwr.h" #include "stm32h7xx_ll_rtc.h" // Enable various HAL modules diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 0846d97cf365a..1395949f2e012 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -1443,7 +1443,14 @@ void stm32_main(int initial_r0) { // Application passed through elements, validate then process them const uint8_t *elem_end = elem_search(ELEM_DATA_START, ELEM_TYPE_END); if (elem_end != NULL && elem_end[-1] == 0) { - fsload_process(); + int ret = fsload_process(); + // If there is a valid ELEM_TYPE_STATUS element then store the status in the given location. + const uint8_t *elem_status = elem_search(ELEM_DATA_START, ELEM_TYPE_STATUS); + if (elem_status != NULL && elem_status[-1] == 4) { + uint32_t *status_ptr = (uint32_t *)get_le32(&elem_status[0]); + LL_PWR_EnableBkUpAccess(); // In case status_ptr points to backup registers + *status_ptr = ret; + } } // Always reset because the application is expecting to resume led_state_all(0); diff --git a/ports/stm32/mboot/mboot.h b/ports/stm32/mboot/mboot.h index e4ed3cecc7f50..853a86968ea61 100644 --- a/ports/stm32/mboot/mboot.h +++ b/ports/stm32/mboot/mboot.h @@ -43,6 +43,7 @@ enum { ELEM_TYPE_END = 1, ELEM_TYPE_MOUNT, ELEM_TYPE_FSLOAD, + ELEM_TYPE_STATUS, }; enum { From bd7110a3d50d2bcfaa5527fdb5d7305badba1b82 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 29 Jan 2021 00:40:49 +1100 Subject: [PATCH 0526/3533] stm32/mboot: Introduce MBOOT_ERRNO_xxx constants and use them. So that a failed update via fsload can be more easily diagnosed. Signed-off-by: Damien George --- ports/stm32/mboot/fsload.c | 30 +++++++++++++++--------------- ports/stm32/mboot/gzstream.c | 2 +- ports/stm32/mboot/main.c | 6 +++--- ports/stm32/mboot/mboot.h | 31 +++++++++++++++++++++++++++++++ ports/stm32/mboot/pack.c | 22 +++++++++++----------- ports/stm32/mboot/vfs_fat.c | 4 ++-- ports/stm32/mboot/vfs_lfs.c | 13 +++++++++++-- 7 files changed, 74 insertions(+), 34 deletions(-) diff --git a/ports/stm32/mboot/fsload.c b/ports/stm32/mboot/fsload.c index fe98426bed9cf..9ecc25b0be925 100644 --- a/ports/stm32/mboot/fsload.c +++ b/ports/stm32/mboot/fsload.c @@ -80,18 +80,18 @@ static int fsload_program_file(bool write_to_flash) { // Read file header, <5sBIB int res = input_stream_read(11, buf); if (res != 11) { - return -1; + return -MBOOT_ERRNO_DFU_READ_ERROR; } file_offset = 11; // Validate header, version 1 if (memcmp(buf, "DfuSe\x01", 6) != 0) { - return -1; + return -MBOOT_ERRNO_DFU_INVALID_HEADER; } // Must have only 1 target if (buf[10] != 1) { - return -2; + return -MBOOT_ERRNO_DFU_TOO_MANY_TARGETS; } // Get total size @@ -100,13 +100,13 @@ static int fsload_program_file(bool write_to_flash) { // Read target header, <6sBi255sII res = input_stream_read(274, buf); if (res != 274) { - return -1; + return -MBOOT_ERRNO_DFU_READ_ERROR; } file_offset += 274; // Validate target header, with alt being 0 if (memcmp(buf, "Target\x00", 7) != 0) { - return -1; + return -MBOOT_ERRNO_DFU_INVALID_TARGET; } // Get target size and number of elements @@ -120,7 +120,7 @@ static int fsload_program_file(bool write_to_flash) { // Read element header, sizeof(firmware_chunk_buf)) { // Address/length out of range. firmware_chunk_base_addr = 0; - return -1; + return -MBOOT_ERRNO_PACK_INVALID_ADDR; } // Copy in the new data piece into the chunk buffer. @@ -232,14 +232,14 @@ int mboot_pack_write(uint32_t addr, const uint8_t *src8, size_t len) { // Chunk header has the wrong version. dfu_context.status = DFU_STATUS_ERROR_FILE; dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; - return -1; + return -MBOOT_ERRNO_PACK_INVALID_VERSION; } if (firmware_chunk_buf.header.address != firmware_chunk_base_addr) { // Chunk address doesn't agree with dfu address, abort. dfu_context.status = DFU_STATUS_ERROR_ADDRESS; dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; - return -1; + return -MBOOT_ERRNO_PACK_INVALID_ADDR; } if (offset + len < sizeof(firmware_chunk_buf.header) + firmware_chunk_buf.header.length + sizeof(firmware_chunk_buf.signature)) { @@ -260,7 +260,7 @@ int mboot_pack_write(uint32_t addr, const uint8_t *src8, size_t len) { // Signature failed dfu_context.status = DFU_STATUS_ERROR_VERIFY; dfu_context.error = MBOOT_ERROR_STR_INVALID_SIG_IDX; - return -1; + return -MBOOT_ERRNO_PACK_SIGN_FAILED; } // Signature passed, we have valid chunk. @@ -275,7 +275,7 @@ int mboot_pack_write(uint32_t addr, const uint8_t *src8, size_t len) { return mboot_pack_handle_firmware(); } else { // Unsupported contents. - return -1; + return -MBOOT_ERRNO_PACK_INVALID_CHUNK; } } diff --git a/ports/stm32/mboot/vfs_fat.c b/ports/stm32/mboot/vfs_fat.c index 5120bdb1044e6..cfa30fb12cc27 100644 --- a/ports/stm32/mboot/vfs_fat.c +++ b/ports/stm32/mboot/vfs_fat.c @@ -84,7 +84,7 @@ int vfs_fat_mount(vfs_fat_context_t *ctx, uint32_t base_addr, uint32_t byte_len) ctx->fatfs.drv = ctx; FRESULT res = f_mount(&ctx->fatfs); if (res != FR_OK) { - return -1; + return -MBOOT_ERRNO_VFS_FAT_MOUNT_FAILED; } return 0; } @@ -93,7 +93,7 @@ static int vfs_fat_stream_open(void *stream_in, const char *fname) { vfs_fat_context_t *stream = stream_in; FRESULT res = f_open(&stream->fatfs, &stream->fp, fname, FA_READ); if (res != FR_OK) { - return -1; + return -MBOOT_ERRNO_VFS_FAT_OPEN_FAILED; } return 0; } diff --git a/ports/stm32/mboot/vfs_lfs.c b/ports/stm32/mboot/vfs_lfs.c index 3e4c9caf16d87..e7fd8ce63c07e 100644 --- a/ports/stm32/mboot/vfs_lfs.c +++ b/ports/stm32/mboot/vfs_lfs.c @@ -37,6 +37,9 @@ #error Unsupported #endif +#define MBOOT_ERRNO_VFS_LFS_MOUNT_FAILED MBOOT_ERRNO_VFS_LFS1_MOUNT_FAILED +#define MBOOT_ERRNO_VFS_LFS_OPEN_FAILED MBOOT_ERRNO_VFS_LFS1_OPEN_FAILED + #define LFSx_MACRO(s) LFS1##s #define LFSx_API(x) lfs1_ ## x #define VFS_LFSx_CONTEXT_T vfs_lfs1_context_t @@ -49,6 +52,9 @@ static uint8_t lfs_lookahead_buffer[LFS_LOOKAHEAD_SIZE / 8]; #else +#define MBOOT_ERRNO_VFS_LFS_MOUNT_FAILED MBOOT_ERRNO_VFS_LFS2_MOUNT_FAILED +#define MBOOT_ERRNO_VFS_LFS_OPEN_FAILED MBOOT_ERRNO_VFS_LFS2_OPEN_FAILED + #define LFSx_MACRO(s) LFS2##s #define LFSx_API(x) lfs2_ ## x #define VFS_LFSx_CONTEXT_T vfs_lfs2_context_t @@ -116,7 +122,7 @@ int VFS_LFSx_MOUNT(VFS_LFSx_CONTEXT_T *ctx, uint32_t base_addr, uint32_t byte_le int ret = LFSx_API(mount)(&ctx->lfs, &ctx->config); if (ret < 0) { - return -1; + return -MBOOT_ERRNO_VFS_LFS_MOUNT_FAILED; } return 0; } @@ -126,7 +132,10 @@ static int vfs_lfs_stream_open(void *stream_in, const char *fname) { memset(&ctx->file, 0, sizeof(ctx->file)); memset(&ctx->filecfg, 0, sizeof(ctx->filecfg)); ctx->filecfg.buffer = &ctx->filebuf[0]; - LFSx_API(file_opencfg)(&ctx->lfs, &ctx->file, fname, LFSx_MACRO(_O_RDONLY), &ctx->filecfg); + int ret = LFSx_API(file_opencfg)(&ctx->lfs, &ctx->file, fname, LFSx_MACRO(_O_RDONLY), &ctx->filecfg); + if (ret < 0) { + return -MBOOT_ERRNO_VFS_LFS_OPEN_FAILED; + } return 0; } From 8f211df360f324c6f8e0fee3fe00bc0b4a9ac390 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 29 Jan 2021 11:16:00 +1100 Subject: [PATCH 0527/3533] stm32/mboot/fwupdate.py: Refactor update_mpy with support for STATUS. Changes are: - refactor to use new _create_element function - support extended version of MOUNT element with block size - support STATUS element Signed-off-by: Damien George --- ports/stm32/mboot/fwupdate.py | 36 ++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/ports/stm32/mboot/fwupdate.py b/ports/stm32/mboot/fwupdate.py index 65284a6ac0461..d7c8f46db4c53 100644 --- a/ports/stm32/mboot/fwupdate.py +++ b/ports/stm32/mboot/fwupdate.py @@ -1,6 +1,7 @@ # Update Mboot or MicroPython from a .dfu.gz file on the board's filesystem # MIT license; Copyright (c) 2019-2020 Damien P. George +from micropython import const import struct, time import uzlib, machine, stm @@ -9,6 +10,12 @@ VFS_LFS1 = 2 VFS_LFS2 = 3 +# Constants for creating mboot elements. +_ELEM_TYPE_END = const(1) +_ELEM_TYPE_MOUNT = const(2) +_ELEM_TYPE_FSLOAD = const(3) +_ELEM_TYPE_STATUS = const(4) + FLASH_KEY1 = 0x45670123 FLASH_KEY2 = 0xCDEF89AB @@ -156,7 +163,11 @@ def update_mboot(filename): print("Programming finished, can now reset or turn off.") -def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT): +def _create_element(kind, body): + return bytes([kind, len(body)]) + body + + +def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT, fs_blocksize=0, status_addr=None): # Check firmware is of .dfu or .dfu.gz type try: with open(filename, "rb") as f: @@ -168,13 +179,20 @@ def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT): print("Firmware must be a .dfu(.gz) file.") return - ELEM_TYPE_END = 1 - ELEM_TYPE_MOUNT = 2 - ELEM_TYPE_FSLOAD = 3 + if fs_type in (VFS_LFS1, VFS_LFS2) and not fs_blocksize: + raise Exception("littlefs requires fs_blocksize parameter") + mount_point = 1 - mount = struct.pack(" Date: Fri, 29 Jan 2021 11:59:15 +1100 Subject: [PATCH 0528/3533] stm32/main: Introduce MICROPY_HW_FLASH_MOUNT_AT_BOOT config option. It's enabled by default to retain the existing behaviour. A board can disable this option if it manages mounting the filesystem itself, for example in frozen code. Signed-off-by: Damien George --- ports/stm32/main.c | 4 ++-- ports/stm32/mpconfigboard_common.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index f0a10fa93359e..38710e2651fb3 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -153,7 +153,7 @@ STATIC mp_obj_t pyb_main(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a } MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main); -#if MICROPY_HW_ENABLE_STORAGE +#if MICROPY_HW_FLASH_MOUNT_AT_BOOT // avoid inlining to avoid stack usage within main() MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { if (reset_mode == 3) { @@ -530,7 +530,7 @@ void stm32_main(uint32_t reset_mode) { // Initialise the local flash filesystem. // Create it if needed, mount in on /flash, and set it as current dir. bool mounted_flash = false; - #if MICROPY_HW_ENABLE_STORAGE + #if MICROPY_HW_FLASH_MOUNT_AT_BOOT mounted_flash = init_flash_fs(reset_mode); #endif diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 60fbc35fcbae0..f493bb5d4a644 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -122,6 +122,11 @@ #define MICROPY_HW_HAS_LCD (0) #endif +// Whether to automatically mount (and boot from) the flash filesystem +#ifndef MICROPY_HW_FLASH_MOUNT_AT_BOOT +#define MICROPY_HW_FLASH_MOUNT_AT_BOOT (MICROPY_HW_ENABLE_STORAGE) +#endif + // The volume label used when creating the flash filesystem #ifndef MICROPY_HW_FLASH_FS_LABEL #define MICROPY_HW_FLASH_FS_LABEL "pybflash" From 5d68b5e22ca361676903c68a5c004bdff40dd88b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 29 Jan 2021 23:30:02 +1100 Subject: [PATCH 0529/3533] tools/ci.sh: For ci_code_size_setup, update apt to install gcc-multilib. Signed-off-by: Damien George --- tools/ci.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/ci.sh b/tools/ci.sh index 86c17bb691e3a..986ec6756eac7 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -33,6 +33,7 @@ function ci_code_formatting_run { # code size function ci_code_size_setup { + sudo apt-get update sudo apt-get install gcc-multilib gcc --version ci_gcc_arm_setup From 925bd67cfb1607264fae79a4fdf5e79ab1ae46aa Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 26 Sep 2020 01:17:11 +1000 Subject: [PATCH 0530/3533] py/objfun: Support fun.__globals__ attribute. This returns a reference to the globals dict associated with the function, ie the global scope that the function was defined in. This attribute is read-only but the dict itself is modifiable, per CPython behaviour. Signed-off-by: Damien George --- py/objfun.c | 4 ++++ tests/basics/fun_globals.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/basics/fun_globals.py diff --git a/py/objfun.c b/py/objfun.c index 052f4b1cedfd2..178f834431cf1 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -355,6 +355,10 @@ void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (attr == MP_QSTR___name__) { dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in)); } + if (attr == MP_QSTR___globals__) { + mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in); + dest[0] = MP_OBJ_FROM_PTR(self->globals); + } } #endif diff --git a/tests/basics/fun_globals.py b/tests/basics/fun_globals.py new file mode 100644 index 0000000000000..3f32e8bdb05b5 --- /dev/null +++ b/tests/basics/fun_globals.py @@ -0,0 +1,21 @@ +# test the __globals__ attribute of a function + + +def foo(): + pass + + +if not hasattr(foo, "__globals__"): + print("SKIP") + raise SystemExit + +print(type(foo.__globals__)) +print(foo.__globals__ is globals()) + +foo.__globals__["bar"] = 123 +print(bar) + +try: + foo.__globals__ = None +except AttributeError: + print("AttributeError") From fe16e785fe76ca2c84b82512c427c7fa57176abe Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 16 Jan 2021 02:01:26 +1100 Subject: [PATCH 0531/3533] tools/mpy-tool.py: List frozen modules in MICROPY_FROZEN_LIST_ITEM. Signed-off-by: Damien George --- tools/mpy-tool.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index de7cfe5d631c7..ea756d3ee2a5c 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -916,6 +916,17 @@ def freeze_mpy(base_qstrs, raw_codes): print(" &raw_code_%s," % rc.escaped_name) print("};") + # If a port defines MICROPY_FROZEN_LIST_ITEM then list all modules wrapped in that macro. + print("#ifdef MICROPY_FROZEN_LIST_ITEM") + for rc in raw_codes: + module_name = rc.source_file.str + if module_name.endswith("/__init__.py"): + short_name = module_name[: -len("/__init__.py")] + else: + short_name = module_name[: -len(".py")] + print('MICROPY_FROZEN_LIST_ITEM("%s", "%s")' % (short_name, module_name)) + print("#endif") + def merge_mpy(raw_codes, output_file): assert len(raw_codes) <= 31 # so var-uints all fit in 1 byte From 40d2010882409e71080f519312488f5bb951238b Mon Sep 17 00:00:00 2001 From: graham sanderson Date: Sat, 12 Dec 2020 13:04:10 -0600 Subject: [PATCH 0532/3533] py/asmthumb: Add support for ARMv6M in native emitter. Adds a new compile-time option MICROPY_EMIT_THUMB_ARMV7M which is enabled by default (to get existing behaviour) and which should be disabled (set to 0) when building native emitter support (@micropython.native) on ARMv6M targets. --- py/asmthumb.c | 177 +++++++++++++++++++++++++++++++++++++++++-- py/asmthumb.h | 45 +++++++++++ py/emitinlinethumb.c | 6 +- py/mpconfig.h | 5 ++ 4 files changed, 225 insertions(+), 8 deletions(-) diff --git a/py/asmthumb.c b/py/asmthumb.c index bb10935fdaad3..e3558b2cf2568 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -47,6 +47,7 @@ #define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800) #define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000) +#if MICROPY_EMIT_THUMB_ARMV7M // Note: these actually take an imm12 but the high-bit is not encoded here #define OP_ADD_W_RRI_HI(reg_src) (0xf200 | (reg_src)) #define OP_ADD_W_RRI_LO(reg_dest, imm11) ((imm11 << 4 & 0x7000) | reg_dest << 8 | (imm11 & 0xff)) @@ -55,6 +56,7 @@ #define OP_LDR_W_HI(reg_base) (0xf8d0 | (reg_base)) #define OP_LDR_W_LO(reg_dest, imm12) ((reg_dest) << 12 | (imm12)) +#endif static inline byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int n) { return mp_asm_base_get_cur_to_write_bytes(&as->base, n); @@ -122,7 +124,7 @@ void asm_thumb_entry(asm_thumb_t *as, int num_locals) { // If this Thumb machine code is run from ARM state then add a prelude // to switch to Thumb state for the duration of the function. - #if MICROPY_DYNAMIC_COMPILER || MICROPY_EMIT_ARM || (defined(__arm__) && !defined(__thumb2__)) + #if MICROPY_DYNAMIC_COMPILER || MICROPY_EMIT_ARM || (defined(__arm__) && !defined(__thumb2__) && !defined(__thumb__)) #if MICROPY_DYNAMIC_COMPILER if (mp_dynamic_compiler.native_arch == MP_NATIVE_ARCH_ARMV6) #endif @@ -171,11 +173,21 @@ void asm_thumb_entry(asm_thumb_t *as, int num_locals) { } asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist)); if (stack_adjust > 0) { + #if MICROPY_EMIT_THUMB_ARMV7M if (UNSIGNED_FIT7(stack_adjust)) { asm_thumb_op16(as, OP_SUB_SP(stack_adjust)); } else { asm_thumb_op32(as, OP_SUB_W_RRI_HI(ASM_THUMB_REG_SP), OP_SUB_W_RRI_LO(ASM_THUMB_REG_SP, stack_adjust * 4)); } + #else + int adj = stack_adjust; + // we don't expect the stack_adjust to be massive + while (!UNSIGNED_FIT7(adj)) { + asm_thumb_op16(as, OP_SUB_SP(127)); + adj -= 127; + } + asm_thumb_op16(as, OP_SUB_SP(adj)); + #endif } as->push_reglist = reglist; as->stack_adjust = stack_adjust; @@ -183,11 +195,21 @@ void asm_thumb_entry(asm_thumb_t *as, int num_locals) { void asm_thumb_exit(asm_thumb_t *as) { if (as->stack_adjust > 0) { + #if MICROPY_EMIT_THUMB_ARMV7M if (UNSIGNED_FIT7(as->stack_adjust)) { asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust)); } else { asm_thumb_op32(as, OP_ADD_W_RRI_HI(ASM_THUMB_REG_SP), OP_ADD_W_RRI_LO(ASM_THUMB_REG_SP, as->stack_adjust * 4)); } + #else + int adj = as->stack_adjust; + // we don't expect the stack_adjust to be massive + while (!UNSIGNED_FIT7(adj)) { + asm_thumb_op16(as, OP_ADD_SP(127)); + adj -= 127; + } + asm_thumb_op16(as, OP_ADD_SP(adj)); + #endif } asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist)); } @@ -241,6 +263,8 @@ void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) { asm_thumb_op16(as, 0x4600 | op_lo); } +#if MICROPY_EMIT_THUMB_ARMV7M + // if loading lo half with movw, the i16 value will be zero extended into the r32 register! size_t asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) { assert(reg_dest < ASM_THUMB_REG_R15); @@ -250,6 +274,16 @@ size_t asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i1 return loc; } +#else + +void asm_thumb_mov_rlo_i16(asm_thumb_t *as, uint rlo_dest, int i16_src) { + asm_thumb_mov_rlo_i8(as, rlo_dest, (i16_src >> 8) & 0xff); + asm_thumb_lsl_rlo_rlo_i5(as, rlo_dest, rlo_dest, 8); + asm_thumb_add_rlo_i8(as, rlo_dest, i16_src & 0xff); +} + +#endif + #define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff)) bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) { @@ -274,8 +308,13 @@ bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) { asm_thumb_op16(as, OP_BCC_N(cond, rel)); return as->base.pass != MP_ASM_PASS_EMIT || SIGNED_FIT9(rel); } else { + #if MICROPY_EMIT_THUMB_ARMV7M asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel)); return true; + #else + // this method should not be called for ARMV6M + return false; + #endif } } @@ -296,8 +335,30 @@ size_t asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) { size_t loc = mp_asm_base_get_code_pos(&as->base); + #if MICROPY_EMIT_THUMB_ARMV7M asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32); asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16); + #else + // should only be called with lo reg for ARMV6M + assert(reg_dest < ASM_THUMB_REG_R8); + + // sanity check that generated code is aligned + assert(!as->base.code_base || !(3u & (uintptr_t)as->base.code_base)); + + // basically: + // (nop) + // ldr reg_dest, _data + // b 1f + // _data: .word i32 + // 1: + if (as->base.code_offset & 2u) { + asm_thumb_op16(as, ASM_THUMB_OP_NOP); + } + asm_thumb_ldr_rlo_pcrel_i8(as, reg_dest, 0); + asm_thumb_op16(as, OP_B_N(2)); + asm_thumb_op16(as, i32 & 0xffff); + asm_thumb_op16(as, i32 >> 16); + #endif return loc; } @@ -305,27 +366,68 @@ size_t asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) { void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) { if (reg_dest < 8 && UNSIGNED_FIT8(i32)) { asm_thumb_mov_rlo_i8(as, reg_dest, i32); - } else if (UNSIGNED_FIT16(i32)) { - asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32); } else { - asm_thumb_mov_reg_i32(as, reg_dest, i32); + #if MICROPY_EMIT_THUMB_ARMV7M + if (UNSIGNED_FIT16(i32)) { + asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32); + } else { + asm_thumb_mov_reg_i32(as, reg_dest, i32); + } + #else + uint rlo_dest = reg_dest; + assert(rlo_dest < ASM_THUMB_REG_R8); // should never be called for ARMV6M + + bool negate = i32 < 0 && ((i32 + i32) & 0xffffffffu); // don't negate 0x80000000 + if (negate) { + i32 = -i32; + } + + uint clz = __builtin_clz(i32); + uint ctz = i32 ? __builtin_ctz(i32) : 0; + assert(clz + ctz <= 32); + if (clz + ctz >= 24) { + asm_thumb_mov_rlo_i8(as, rlo_dest, (i32 >> ctz) & 0xff); + asm_thumb_lsl_rlo_rlo_i5(as, rlo_dest, rlo_dest, ctz); + } else if (UNSIGNED_FIT16(i32)) { + asm_thumb_mov_rlo_i16(as, rlo_dest, i32); + } else { + if (negate) { + // no point in negating if we're storing in 32 bit anyway + negate = false; + i32 = -i32; + } + asm_thumb_mov_reg_i32(as, rlo_dest, i32); + } + if (negate) { + asm_thumb_neg_rlo_rlo(as, rlo_dest, rlo_dest); + } + #endif } } #define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff)) #define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff)) +static void asm_thumb_mov_local_check(asm_thumb_t *as, int word_offset) { + if (as->base.pass >= MP_ASM_PASS_EMIT) { + assert(word_offset >= 0); + if (!UNSIGNED_FIT8(word_offset)) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("too many locals for native method")); + } + } +} + void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) { assert(rlo_src < ASM_THUMB_REG_R8); int word_offset = local_num; - assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0); + asm_thumb_mov_local_check(as, word_offset); asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset)); } void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) { assert(rlo_dest < ASM_THUMB_REG_R8); int word_offset = local_num; - assert(as->base.pass < MP_ASM_PASS_EMIT || word_offset >= 0); + asm_thumb_mov_local_check(as, word_offset); asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset)); } @@ -341,21 +443,63 @@ void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) void asm_thumb_mov_reg_pcrel(asm_thumb_t *as, uint rlo_dest, uint label) { mp_uint_t dest = get_label_dest(as, label); mp_int_t rel = dest - as->base.code_offset; - rel -= 4 + 4; // adjust for mov_reg_i16 and then PC+4 prefetch of add_reg_reg rel |= 1; // to stay in Thumb state when jumping to this address + #if MICROPY_EMIT_THUMB_ARMV7M + rel -= 4 + 4; // adjust for mov_reg_i16 and then PC+4 prefetch of add_reg_reg asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, rlo_dest, rel); // 4 bytes + #else + rel -= 8 + 4; // adjust for four instructions and then PC+4 prefetch of add_reg_reg + // 6 bytes + asm_thumb_mov_rlo_i16(as, rlo_dest, rel); + // 2 bytes - not always needed, but we want to keep the size the same + asm_thumb_sxth_rlo_rlo(as, rlo_dest, rlo_dest); + #endif asm_thumb_add_reg_reg(as, rlo_dest, ASM_THUMB_REG_R15); // 2 bytes } +#if MICROPY_EMIT_THUMB_ARMV7M static inline void asm_thumb_ldr_reg_reg_i12(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) { asm_thumb_op32(as, OP_LDR_W_HI(reg_base), OP_LDR_W_LO(reg_dest, word_offset * 4)); } +#endif void asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint reg_base, uint word_offset) { if (reg_dest < ASM_THUMB_REG_R8 && reg_base < ASM_THUMB_REG_R8 && UNSIGNED_FIT5(word_offset)) { asm_thumb_ldr_rlo_rlo_i5(as, reg_dest, reg_base, word_offset); } else { + #if MICROPY_EMIT_THUMB_ARMV7M asm_thumb_ldr_reg_reg_i12(as, reg_dest, reg_base, word_offset); + #else + word_offset -= 31; + if (reg_dest < ASM_THUMB_REG_R8 && reg_base < ASM_THUMB_REG_R8) { + if (UNSIGNED_FIT8(word_offset) && (word_offset < 64 || reg_dest != reg_base)) { + if (word_offset < 64) { + if (reg_dest != reg_base) { + asm_thumb_mov_reg_reg(as, reg_dest, reg_base); + } + asm_thumb_add_rlo_i8(as, reg_dest, word_offset * 4); + } else { + asm_thumb_mov_rlo_i8(as, reg_dest, word_offset); + asm_thumb_lsl_rlo_rlo_i5(as, reg_dest, reg_dest, 2); + asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_base); + } + } else { + if (reg_dest != reg_base) { + asm_thumb_mov_rlo_i16(as, reg_dest, word_offset * 4); + asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_dest); + } else { + uint reg_other = reg_dest ^ 7; + asm_thumb_op16(as, OP_PUSH_RLIST((1 << reg_other))); + asm_thumb_mov_rlo_i16(as, reg_other, word_offset * 4); + asm_thumb_add_rlo_rlo_rlo(as, reg_dest, reg_dest, reg_other); + asm_thumb_op16(as, OP_POP_RLIST((1 << reg_other))); + } + } + } else { + assert(0); // should never be called for ARMV6M + } + asm_thumb_ldr_rlo_rlo_i5(as, reg_dest, reg_dest, 31); + #endif } } @@ -378,7 +522,20 @@ void asm_thumb_b_label(asm_thumb_t *as, uint label) { } else { // is a forwards jump, so need to assume it's large large_jump: + #if MICROPY_EMIT_THUMB_ARMV7M asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel)); + #else + if (SIGNED_FIT12(rel)) { + // this code path has to be the same number of instructions irrespective of rel + asm_thumb_op16(as, OP_B_N(rel)); + } else { + asm_thumb_op16(as, ASM_THUMB_OP_NOP); + if (dest != (mp_uint_t)-1) { + // we have an actual branch > 12 bits; this is not handled yet + mp_raise_NotImplementedError(MP_ERROR_TEXT("native method too big")); + } + } + #endif } } @@ -397,7 +554,13 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) { } else { // is a forwards jump, so need to assume it's large large_jump: + #if MICROPY_EMIT_THUMB_ARMV7M asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel)); + #else + // reverse the sense of the branch to jump over a longer branch + asm_thumb_op16(as, OP_BCC_N(cond ^ 1, 0)); + asm_thumb_b_label(as, label); + #endif } } diff --git a/py/asmthumb.h b/py/asmthumb.h index 17b694a74dfdd..3c45336186655 100644 --- a/py/asmthumb.h +++ b/py/asmthumb.h @@ -157,6 +157,7 @@ static inline void asm_thumb_sub_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint #define ASM_THUMB_FORMAT_3_CMP (0x2800) #define ASM_THUMB_FORMAT_3_ADD (0x3000) #define ASM_THUMB_FORMAT_3_SUB (0x3800) +#define ASM_THUMB_FORMAT_3_LDR (0x4800) #define ASM_THUMB_FORMAT_3_ENCODE(op, rlo, i8) ((op) | ((rlo) << 8) | (i8)) @@ -177,6 +178,9 @@ static inline void asm_thumb_add_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { static inline void asm_thumb_sub_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_SUB, rlo, i8); } +static inline void asm_thumb_ldr_rlo_pcrel_i8(asm_thumb_t *as, uint rlo, uint i8) { + asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_LDR, rlo, i8); +} // FORMAT 4: ALU operations @@ -202,6 +206,12 @@ void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src); static inline void asm_thumb_cmp_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) { asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_CMP, rlo_dest, rlo_src); } +static inline void asm_thumb_mvn_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) { + asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_MVN, rlo_dest, rlo_src); +} +static inline void asm_thumb_neg_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) { + asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_NEG, rlo_dest, rlo_src); +} // FORMAT 5: hi register operations (add, cmp, mov, bx) // For add/cmp/mov, at least one of the args must be a high register @@ -263,6 +273,32 @@ static inline void asm_thumb_ldrb_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uin static inline void asm_thumb_ldrh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint byte_offset) { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_10_LDRH, rlo_dest, rlo_base, byte_offset); } +static inline void asm_thumb_lsl_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_src, uint shift) { + asm_thumb_format_1(as, ASM_THUMB_FORMAT_1_LSL, rlo_dest, rlo_src, shift); +} +static inline void asm_thumb_asr_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_src, uint shift) { + asm_thumb_format_1(as, ASM_THUMB_FORMAT_1_ASR, rlo_dest, rlo_src, shift); +} + +// FORMAT 11: sign/zero extend + +#define ASM_THUMB_FORMAT_11_ENCODE(op, rlo_dest, rlo_src) \ + ((op) | ((rlo_src) << 3) | (rlo_dest)) + +#define ASM_THUMB_FORMAT_11_SXTH (0xb200) +#define ASM_THUMB_FORMAT_11_SXTB (0xb240) +#define ASM_THUMB_FORMAT_11_UXTH (0xb280) +#define ASM_THUMB_FORMAT_11_UXTB (0xb2c0) + +static inline void asm_thumb_format_11(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) { + assert(rlo_dest < ASM_THUMB_REG_R8); + assert(rlo_src < ASM_THUMB_REG_R8); + asm_thumb_op16(as, ASM_THUMB_FORMAT_11_ENCODE(op, rlo_dest, rlo_src)); +} + +static inline void asm_thumb_sxth_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) { + asm_thumb_format_11(as, ASM_THUMB_FORMAT_11_SXTH, rlo_dest, rlo_src); +} // TODO convert these to above format style @@ -270,7 +306,12 @@ static inline void asm_thumb_ldrh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uin #define ASM_THUMB_OP_MOVT (0xf2c0) void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src); + +#if MICROPY_EMIT_THUMB_ARMV7M size_t asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src); +#else +void asm_thumb_mov_rlo_i16(asm_thumb_t *as, uint rlo_dest, int i16_src); +#endif // these return true if the destination is in range, false otherwise bool asm_thumb_b_n_label(asm_thumb_t *as, uint label); @@ -344,7 +385,11 @@ void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp); // convenien #define ASM_MOV_LOCAL_REG(as, local_num, reg) asm_thumb_mov_local_reg((as), (local_num), (reg)) #define ASM_MOV_REG_IMM(as, reg_dest, imm) asm_thumb_mov_reg_i32_optimised((as), (reg_dest), (imm)) +#if MICROPY_EMIT_THUMB_ARMV7M #define ASM_MOV_REG_IMM_FIX_U16(as, reg_dest, imm) asm_thumb_mov_reg_i16((as), ASM_THUMB_OP_MOVW, (reg_dest), (imm)) +#else +#define ASM_MOV_REG_IMM_FIX_U16(as, reg_dest, imm) asm_thumb_mov_rlo_i16((as), (reg_dest), (imm)) +#endif #define ASM_MOV_REG_IMM_FIX_WORD(as, reg_dest, imm) asm_thumb_mov_reg_i32((as), (reg_dest), (imm)) #define ASM_MOV_REG_LOCAL(as, reg_dest, local_num) asm_thumb_mov_reg_local((as), (reg_dest), (local_num)) #define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_thumb_mov_reg_reg((as), (reg_dest), (reg_src)) diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c index cffaa4bb89950..073b88da71f6e 100644 --- a/py/emitinlinethumb.c +++ b/py/emitinlinethumb.c @@ -573,7 +573,11 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a goto unknown_op; } int label_num = get_arg_label(emit, op_str, pn_args[0]); - if (!asm_thumb_bcc_nw_label(&emit->as, cc, label_num, op_len == 5 && op_str[4] == 'w')) { + bool wide = op_len == 5 && op_str[4] == 'w'; + if (wide && !ARMV7M) { + goto unknown_op; + } + if (!asm_thumb_bcc_nw_label(&emit->as, cc, label_num, wide)) { goto branch_not_in_range; } } else if (ARMV7M && op_str[0] == 'i' && op_str[1] == 't') { diff --git a/py/mpconfig.h b/py/mpconfig.h index 854188b66b585..1df2e8fe918ef 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -304,6 +304,11 @@ #define MICROPY_EMIT_THUMB (0) #endif +// Whether to emit ARMv7-M instruction support in thumb native code +#ifndef MICROPY_EMIT_THUMB_ARMV7M +#define MICROPY_EMIT_THUMB_ARMV7M (1) +#endif + // Whether to enable the thumb inline assembler #ifndef MICROPY_EMIT_INLINE_THUMB #define MICROPY_EMIT_INLINE_THUMB (0) From c9f4c5acd6278a7a8f3376867d2927efd00aefa6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 00:08:59 +1100 Subject: [PATCH 0533/3533] py/emitnative: Ensure encoding to load prelude_offset doesn't change sz. Based on change made by Graham Sanderson. Signed-off-by: Damien George --- py/emitnative.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/py/emitnative.c b/py/emitnative.c index 2a657b6964daa..8cb5de1b4af9f 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -211,6 +211,7 @@ struct _emit_t { int pass; bool do_viper_types; + bool prelude_offset_uses_u16_encoding; mp_uint_t local_vtype_alloc; vtype_kind_t *local_vtype; @@ -339,6 +340,18 @@ STATIC void emit_native_mov_reg_qstr_obj(emit_t *emit, int reg_dest, qstr qst) { emit_native_mov_state_reg((emit), (local_num), (reg_temp)); \ } while (false) +#define emit_native_mov_state_imm_fix_u16_via(emit, local_num, imm, reg_temp) \ + do { \ + ASM_MOV_REG_IMM_FIX_U16((emit)->as, (reg_temp), (imm)); \ + emit_native_mov_state_reg((emit), (local_num), (reg_temp)); \ + } while (false) + +#define emit_native_mov_state_imm_fix_word_via(emit, local_num, imm, reg_temp) \ + do { \ + ASM_MOV_REG_IMM_FIX_WORD((emit)->as, (reg_temp), (imm)); \ + emit_native_mov_state_reg((emit), (local_num), (reg_temp)); \ + } while (false) + STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) { DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope); @@ -549,16 +562,27 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_FUN_OBJ(emit), REG_PARENT_ARG_1); // Set code_state.ip (offset from start of this function to prelude info) + int code_state_ip_local = emit->code_state_start + OFFSETOF_CODE_STATE_IP; #if N_PRELUDE_AS_BYTES_OBJ // Prelude is a bytes object in const_table; store ip = prelude->data - fun_bc->bytecode ASM_LOAD_REG_REG_OFFSET(emit->as, REG_LOCAL_3, REG_LOCAL_3, emit->scope->num_pos_args + emit->scope->num_kwonly_args + 1); ASM_LOAD_REG_REG_OFFSET(emit->as, REG_LOCAL_3, REG_LOCAL_3, offsetof(mp_obj_str_t, data) / sizeof(uintptr_t)); ASM_LOAD_REG_REG_OFFSET(emit->as, REG_PARENT_ARG_1, REG_PARENT_ARG_1, OFFSETOF_OBJ_FUN_BC_BYTECODE); ASM_SUB_REG_REG(emit->as, REG_LOCAL_3, REG_PARENT_ARG_1); - emit_native_mov_state_reg(emit, emit->code_state_start + OFFSETOF_CODE_STATE_IP, REG_LOCAL_3); + emit_native_mov_state_reg(emit, code_state_ip_local, REG_LOCAL_3); #else - // TODO this encoding may change size in the final pass, need to make it fixed - emit_native_mov_state_imm_via(emit, emit->code_state_start + OFFSETOF_CODE_STATE_IP, emit->prelude_offset, REG_PARENT_ARG_1); + if (emit->pass == MP_PASS_CODE_SIZE) { + // Commit to the encoding size based on the value of prelude_offset in this pass. + // By using 32768 as the cut-off it is highly unlikely that prelude_offset will + // grow beyond 65535 by the end of thiss pass, and so require the larger encoding. + emit->prelude_offset_uses_u16_encoding = emit->prelude_offset < 32768; + } + if (emit->prelude_offset_uses_u16_encoding) { + assert(emit->prelude_offset <= 65535); + emit_native_mov_state_imm_fix_u16_via(emit, code_state_ip_local, emit->prelude_offset, REG_PARENT_ARG_1); + } else { + emit_native_mov_state_imm_fix_word_via(emit, code_state_ip_local, emit->prelude_offset, REG_PARENT_ARG_1); + } #endif // Set code_state.n_state (only works on little endian targets due to n_state being uint16_t) From 7a97e4351b4d78f64c3d1fbecd497481e649a83f Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 00:29:18 +1100 Subject: [PATCH 0534/3533] tests: Move native for test from pybnative to micropython. And make it generic so it can be run on any target. Signed-off-by: Damien George --- tests/{pybnative/for.py => micropython/native_for.py} | 2 +- tests/{pybnative/for.py.exp => micropython/native_for.py.exp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/{pybnative/for.py => micropython/native_for.py} (85%) rename tests/{pybnative/for.py.exp => micropython/native_for.py.exp} (100%) diff --git a/tests/pybnative/for.py b/tests/micropython/native_for.py similarity index 85% rename from tests/pybnative/for.py rename to tests/micropython/native_for.py index 50177a9ba4854..c640a8d08b576 100644 --- a/tests/pybnative/for.py +++ b/tests/micropython/native_for.py @@ -1,4 +1,4 @@ -import pyb +# test for native for loops @micropython.native diff --git a/tests/pybnative/for.py.exp b/tests/micropython/native_for.py.exp similarity index 100% rename from tests/pybnative/for.py.exp rename to tests/micropython/native_for.py.exp From 75fea330bffe9c916def8bf1521d7d9143eceac2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 00:59:47 +1100 Subject: [PATCH 0535/3533] py/emitinlinethumb: Exclude code using #if when ARMV7M disabled. So there are no references to undeclared asm_thumb_mov_reg_i16(). Signed-off-by: Damien George --- py/emitinlinethumb.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c index 073b88da71f6e..1a35e25ad35b4 100644 --- a/py/emitinlinethumb.c +++ b/py/emitinlinethumb.c @@ -705,23 +705,24 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a } else if (op == MP_QSTR_sub) { op_code = ASM_THUMB_FORMAT_3_SUB; goto op_format_3; - } else if (ARMV7M && op == MP_QSTR_movw) { + #if ARMV7M + } else if (op == MP_QSTR_movw) { op_code = ASM_THUMB_OP_MOVW; mp_uint_t reg_dest; op_movw_movt: reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); int i_src = get_arg_i(emit, op_str, pn_args[1], 0xffff); asm_thumb_mov_reg_i16(&emit->as, op_code, reg_dest, i_src); - } else if (ARMV7M && op == MP_QSTR_movt) { + } else if (op == MP_QSTR_movt) { op_code = ASM_THUMB_OP_MOVT; goto op_movw_movt; - } else if (ARMV7M && op == MP_QSTR_movwt) { + } else if (op == MP_QSTR_movwt) { // this is a convenience instruction mp_uint_t reg_dest = get_arg_reg(emit, op_str, pn_args[0], 15); uint32_t i_src = get_arg_i(emit, op_str, pn_args[1], 0xffffffff); asm_thumb_mov_reg_i16(&emit->as, ASM_THUMB_OP_MOVW, reg_dest, i_src & 0xffff); asm_thumb_mov_reg_i16(&emit->as, ASM_THUMB_OP_MOVT, reg_dest, (i_src >> 16) & 0xffff); - } else if (ARMV7M && op == MP_QSTR_ldrex) { + } else if (op == MP_QSTR_ldrex) { mp_uint_t r_dest = get_arg_reg(emit, op_str, pn_args[0], 15); mp_parse_node_t pn_base, pn_offset; if (get_arg_addr(emit, op_str, pn_args[1], &pn_base, &pn_offset)) { @@ -729,6 +730,7 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a mp_uint_t i8 = get_arg_i(emit, op_str, pn_offset, 0xff) >> 2; asm_thumb_op32(&emit->as, 0xe850 | r_base, 0x0f00 | (r_dest << 12) | i8); } + #endif } else { // search table for ldr/str instructions for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(format_9_10_op_table); i++) { From 33f10381d6cf47a6c6e1ce44ce3debb44ebbaa9d Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 02:54:18 +1100 Subject: [PATCH 0536/3533] lib/timeutils: Provide simple impl of extra funcs when Epoch is 1970. Dates/times must be post 2000/1/1 to work correctly with these simple implementations. Signed-off-by: Damien George --- lib/timeutils/timeutils.c | 2 +- lib/timeutils/timeutils.h | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index fc8b5e7fc824f..af210d9943bee 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -158,7 +158,7 @@ mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month, + (year - 2000) * 31536000; } -mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, +mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds) { // Normalize the tuple. This allows things like: diff --git a/lib/timeutils/timeutils.h b/lib/timeutils/timeutils.h index 14da831dc8b58..2d40f773cc53e 100644 --- a/lib/timeutils/timeutils.h +++ b/lib/timeutils/timeutils.h @@ -52,12 +52,21 @@ void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t, mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second); -mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, +mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds); // Select the Epoch used by the port. #if MICROPY_EPOCH_IS_1970 +static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) { + // TODO this will give incorrect results for dates before 2000/1/1 + return timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm); +} + +static inline uint64_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds) { + return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000; +} + static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) { return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000; @@ -75,6 +84,7 @@ static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_19 #define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time #define timeutils_seconds_since_epoch timeutils_seconds_since_2000 +#define timeutils_mktime timeutils_mktime_2000 static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) { return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL; From 15ac5a3df9b90eade2b4743e08e23ce219cb4f7f Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Jan 2021 11:38:27 +1100 Subject: [PATCH 0537/3533] extmod/modframebuf: Change int to unsigned int in format methods args. These args are already bounds checked and clipped, and using unsigned ints can be more efficient. It also eliminates possible issues and compiler warnings with shifting of signed integers. Signed-off-by: Damien George --- extmod/modframebuf.c | 70 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 2753acc2712b6..18b170078de14 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -45,9 +45,9 @@ typedef struct _mp_obj_framebuf_t { STATIC const mp_obj_type_t mp_type_framebuf; #endif -typedef void (*setpixel_t)(const mp_obj_framebuf_t *, int, int, uint32_t); -typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t *, int, int); -typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, int, int, int, int, uint32_t); +typedef void (*setpixel_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int, uint32_t); +typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int); +typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int, unsigned int, unsigned int, uint32_t); typedef struct _mp_framebuf_p_t { setpixel_t setpixel; @@ -66,25 +66,25 @@ typedef struct _mp_framebuf_p_t { // Functions for MHLSB and MHMSB -STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { size_t index = (x + y * fb->stride) >> 3; - int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); + unsigned int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); ((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset); } -STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { size_t index = (x + y * fb->stride) >> 3; - int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); + unsigned int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); return (((uint8_t *)fb->buf)[index] >> (offset)) & 0x01; } -STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { - int reverse = fb->format == FRAMEBUF_MHMSB; - int advance = fb->stride >> 3; +STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { + unsigned int reverse = fb->format == FRAMEBUF_MHMSB; + unsigned int advance = fb->stride >> 3; while (w--) { uint8_t *b = &((uint8_t *)fb->buf)[(x >> 3) + y * advance]; - int offset = reverse ? x & 7 : 7 - (x & 7); - for (int hh = h; hh; --hh) { + unsigned int offset = reverse ? x & 7 : 7 - (x & 7); + for (unsigned int hh = h; hh; --hh) { *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset); b += advance; } @@ -94,21 +94,21 @@ STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int // Functions for MVLSB format -STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { size_t index = (y >> 3) * fb->stride + x; uint8_t offset = y & 0x07; ((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset); } -STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { return (((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x] >> (y & 0x07)) & 0x01; } -STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { +STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { while (h--) { uint8_t *b = &((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x]; uint8_t offset = y & 0x07; - for (int ww = w; ww; --ww) { + for (unsigned int ww = w; ww; --ww) { *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset); ++b; } @@ -118,18 +118,18 @@ STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, in // Functions for RGB565 format -STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { ((uint16_t *)fb->buf)[x + y * fb->stride] = col; } -STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { return ((uint16_t *)fb->buf)[x + y * fb->stride]; } -STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { +STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { uint16_t *b = &((uint16_t *)fb->buf)[x + y * fb->stride]; while (h--) { - for (int ww = w; ww; --ww) { + for (unsigned int ww = w; ww; --ww) { *b++ = col; } b += fb->stride - w; @@ -138,7 +138,7 @@ STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, i // Functions for GS2_HMSB format -STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2]; uint8_t shift = (x & 0x3) << 1; uint8_t mask = 0x3 << shift; @@ -146,15 +146,15 @@ STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_ *pixel = color | (*pixel & (~mask)); } -STATIC uint32_t gs2_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t gs2_hmsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { uint8_t pixel = ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2]; uint8_t shift = (x & 0x3) << 1; return (pixel >> shift) & 0x3; } -STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { - for (int xx = x; xx < x + w; xx++) { - for (int yy = y; yy < y + h; yy++) { +STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { + for (unsigned int xx = x; xx < x + w; xx++) { + for (unsigned int yy = y; yy < y + h; yy++) { gs2_hmsb_setpixel(fb, xx, yy, col); } } @@ -162,7 +162,7 @@ STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, // Functions for GS4_HMSB format -STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1]; if (x % 2) { @@ -172,7 +172,7 @@ STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_ } } -STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { if (x % 2) { return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f; } @@ -180,16 +180,16 @@ STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] >> 4; } -STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { +STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { col &= 0x0f; uint8_t *pixel_pair = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1]; uint8_t col_shifted_left = col << 4; uint8_t col_pixel_pair = col_shifted_left | col; - int pixel_count_till_next_line = (fb->stride - w) >> 1; + unsigned int pixel_count_till_next_line = (fb->stride - w) >> 1; bool odd_x = (x % 2 == 1); while (h--) { - int ww = w; + unsigned int ww = w; if (odd_x && ww > 0) { *pixel_pair = (*pixel_pair & 0xf0) | col; @@ -213,16 +213,16 @@ STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, // Functions for GS8 format -STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)]; *pixel = col & 0xff; } -STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { return ((uint8_t *)fb->buf)[(x + y * fb->stride)]; } -STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { +STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) { uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)]; while (h--) { memset(pixel, col, w); @@ -240,11 +240,11 @@ STATIC mp_framebuf_p_t formats[] = { [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect}, }; -static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { +static inline void setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { formats[fb->format].setpixel(fb, x, y, col); } -static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, int x, int y) { +static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { return formats[fb->format].getpixel(fb, x, y); } From 794df0f1d502bf715449ed6d36f16cf52b095d0b Mon Sep 17 00:00:00 2001 From: graham sanderson Date: Sun, 17 Jan 2021 21:56:34 -0600 Subject: [PATCH 0538/3533] py/emitnative: Support binary ops on ARMv6M without use of ite instr. --- py/asmthumb.c | 12 ++++++++++++ py/asmthumb.h | 2 ++ py/emitnative.c | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/py/asmthumb.c b/py/asmthumb.c index e3558b2cf2568..f7ac87fa0f934 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -564,6 +564,18 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) { } } +void asm_thumb_bcc_rel9(asm_thumb_t *as, int cond, int rel) { + rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction + assert(SIGNED_FIT9(rel)); + asm_thumb_op16(as, OP_BCC_N(cond, rel)); +} + +void asm_thumb_b_rel12(asm_thumb_t *as, int rel) { + rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction + assert(SIGNED_FIT12(rel)); + asm_thumb_op16(as, OP_B_N(rel)); +} + #define OP_BLX(reg) (0x4780 | ((reg) << 3)) #define OP_SVC(arg) (0xdf00 | (arg)) diff --git a/py/asmthumb.h b/py/asmthumb.h index 3c45336186655..17a0cca98c583 100644 --- a/py/asmthumb.h +++ b/py/asmthumb.h @@ -330,6 +330,8 @@ void asm_thumb_ldr_reg_reg_i12_optimised(asm_thumb_t *as, uint reg_dest, uint re void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience: picks narrow or wide branch void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch void asm_thumb_bl_ind(asm_thumb_t *as, uint fun_id, uint reg_temp); // convenience +void asm_thumb_bcc_rel9(asm_thumb_t *as, int cc, int rel); +void asm_thumb_b_rel12(asm_thumb_t *as, int rel); // Holds a pointer to mp_fun_table #define ASM_THUMB_REG_FUN_TABLE ASM_THUMB_REG_R7 diff --git a/py/emitnative.c b/py/emitnative.c index 8cb5de1b4af9f..052a505911d2a 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -2454,6 +2454,7 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { asm_x86_setcc_r8(emit->as, ops[op_idx], REG_RET); #elif N_THUMB asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs); + #if MICROPY_EMIT_THUMB_ARMV7M static uint16_t ops[6 + 6] = { // unsigned ASM_THUMB_OP_ITE_CC, @@ -2473,6 +2474,28 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) { asm_thumb_op16(emit->as, ops[op_idx]); asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); + #else + static uint16_t ops[6 + 6] = { + // unsigned + ASM_THUMB_CC_CC, + ASM_THUMB_CC_HI, + ASM_THUMB_CC_EQ, + ASM_THUMB_CC_LS, + ASM_THUMB_CC_CS, + ASM_THUMB_CC_NE, + // signed + ASM_THUMB_CC_LT, + ASM_THUMB_CC_GT, + ASM_THUMB_CC_EQ, + ASM_THUMB_CC_LE, + ASM_THUMB_CC_GE, + ASM_THUMB_CC_NE, + }; + asm_thumb_bcc_rel9(emit->as, ops[op_idx], 6); + asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); + asm_thumb_b_rel12(emit->as, 4); + asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); + #endif #elif N_ARM asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs); static uint ccs[6 + 6] = { From ec0503bd0ce3f851f15d79c69b660a6cf411ea78 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Jan 2021 01:19:46 +1100 Subject: [PATCH 0539/3533] extmod/modonewire: Use pin_od_high/pin_od_low instead of pin_write. The pin is configured in open-drain mode so these od_high/od_low methods should be used. Signed-off-by: Damien George --- extmod/modonewire.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/extmod/modonewire.c b/extmod/modonewire.c index 210bee366ee9d..6abe3dfad9bff 100644 --- a/extmod/modonewire.c +++ b/extmod/modonewire.c @@ -44,10 +44,10 @@ #define TIMING_WRITE3 (10) STATIC int onewire_bus_reset(mp_hal_pin_obj_t pin) { - mp_hal_pin_write(pin, 0); + mp_hal_pin_od_low(pin); mp_hal_delay_us(TIMING_RESET1); uint32_t i = mp_hal_quiet_timing_enter(); - mp_hal_pin_write(pin, 1); + mp_hal_pin_od_high(pin); mp_hal_delay_us_fast(TIMING_RESET2); int status = !mp_hal_pin_read(pin); mp_hal_quiet_timing_exit(i); @@ -56,11 +56,11 @@ STATIC int onewire_bus_reset(mp_hal_pin_obj_t pin) { } STATIC int onewire_bus_readbit(mp_hal_pin_obj_t pin) { - mp_hal_pin_write(pin, 1); + mp_hal_pin_od_high(pin); uint32_t i = mp_hal_quiet_timing_enter(); - mp_hal_pin_write(pin, 0); + mp_hal_pin_od_low(pin); mp_hal_delay_us_fast(TIMING_READ1); - mp_hal_pin_write(pin, 1); + mp_hal_pin_od_high(pin); mp_hal_delay_us_fast(TIMING_READ2); int value = mp_hal_pin_read(pin); mp_hal_quiet_timing_exit(i); @@ -70,13 +70,13 @@ STATIC int onewire_bus_readbit(mp_hal_pin_obj_t pin) { STATIC void onewire_bus_writebit(mp_hal_pin_obj_t pin, int value) { uint32_t i = mp_hal_quiet_timing_enter(); - mp_hal_pin_write(pin, 0); + mp_hal_pin_od_low(pin); mp_hal_delay_us_fast(TIMING_WRITE1); if (value) { - mp_hal_pin_write(pin, 1); + mp_hal_pin_od_high(pin); } mp_hal_delay_us_fast(TIMING_WRITE2); - mp_hal_pin_write(pin, 1); + mp_hal_pin_od_high(pin); mp_hal_delay_us_fast(TIMING_WRITE3); mp_hal_quiet_timing_exit(i); } From ef3ee7aa1005cd1f15c2144d4b46feb792ab3185 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Jan 2021 18:04:39 +1100 Subject: [PATCH 0540/3533] lib/pico-sdk: Add new pico-sdk submodule, for the rp2 port. Signed-off-by: Damien George --- .gitmodules | 3 +++ lib/pico-sdk | 1 + 2 files changed, 4 insertions(+) create mode 160000 lib/pico-sdk diff --git a/.gitmodules b/.gitmodules index f4785955af41b..ceaa5342b412c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -39,3 +39,6 @@ [submodule "lib/libhydrogen"] path = lib/libhydrogen url = https://github.com/jedisct1/libhydrogen.git +[submodule "lib/pico-sdk"] + path = lib/pico-sdk + url = https://github.com/raspberrypi/pico-sdk.git diff --git a/lib/pico-sdk b/lib/pico-sdk new file mode 160000 index 0000000000000..493ed000ddfc2 --- /dev/null +++ b/lib/pico-sdk @@ -0,0 +1 @@ +Subproject commit 493ed000ddfc21aa1db933f5477dba52ac841aac From 469345e7285128739e2934e7934e107ffda79fc1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Jan 2021 00:34:08 +1100 Subject: [PATCH 0541/3533] rp2: Add new port to Raspberry Pi RP2 microcontroller. This commit adds a new port "rp2" which targets the new Raspberry Pi RP2040 microcontroller. The build system uses pure cmake (with a small Makefile wrapper for convenience). The USB driver is TinyUSB, and there is a machine module with most of the standard classes implemented. Some examples are provided in the examples/rp2/ directory. Work done in collaboration with Graham Sanderson. Signed-off-by: Damien George --- examples/rp2/pio_1hz.py | 35 ++ examples/rp2/pio_exec.py | 27 ++ examples/rp2/pio_pinchange.py | 46 ++ examples/rp2/pio_pwm.py | 45 ++ examples/rp2/pio_uart_tx.py | 44 ++ examples/rp2/pio_ws2812.py | 59 +++ examples/rp2/pwm_fade.py | 25 ++ ports/rp2/CMakeLists.txt | 162 +++++++ ports/rp2/Makefile | 14 + ports/rp2/README.md | 100 +++++ ports/rp2/machine_adc.c | 120 +++++ ports/rp2/machine_i2c.c | 161 +++++++ ports/rp2/machine_pin.c | 449 +++++++++++++++++++ ports/rp2/machine_pwm.c | 197 +++++++++ ports/rp2/machine_spi.c | 278 ++++++++++++ ports/rp2/machine_timer.c | 165 +++++++ ports/rp2/machine_uart.c | 246 +++++++++++ ports/rp2/machine_wdt.c | 78 ++++ ports/rp2/main.c | 208 +++++++++ ports/rp2/manifest.py | 3 + ports/rp2/memmap_mp.ld | 252 +++++++++++ ports/rp2/micropy_extmod.cmake | 40 ++ ports/rp2/micropy_py.cmake | 134 ++++++ ports/rp2/micropy_rules.cmake | 91 ++++ ports/rp2/modmachine.c | 101 +++++ ports/rp2/modmachine.h | 18 + ports/rp2/modrp2.c | 41 ++ ports/rp2/modrp2.h | 38 ++ ports/rp2/modules/_boot.py | 15 + ports/rp2/modules/rp2.py | 294 +++++++++++++ ports/rp2/moduos.c | 103 +++++ ports/rp2/modutime.c | 127 ++++++ ports/rp2/mpconfigport.h | 196 +++++++++ ports/rp2/mphalport.c | 142 ++++++ ports/rp2/mphalport.h | 113 +++++ ports/rp2/mpthreadport.c | 105 +++++ ports/rp2/mpthreadport.h | 64 +++ ports/rp2/qstrdefsport.h | 3 + ports/rp2/rp2_flash.c | 146 ++++++ ports/rp2/rp2_pio.c | 780 +++++++++++++++++++++++++++++++++ ports/rp2/tusb_config.h | 34 ++ ports/rp2/tusb_port.c | 115 +++++ ports/rp2/uart.c | 63 +++ ports/rp2/uart.h | 32 ++ 44 files changed, 5509 insertions(+) create mode 100644 examples/rp2/pio_1hz.py create mode 100644 examples/rp2/pio_exec.py create mode 100644 examples/rp2/pio_pinchange.py create mode 100644 examples/rp2/pio_pwm.py create mode 100644 examples/rp2/pio_uart_tx.py create mode 100644 examples/rp2/pio_ws2812.py create mode 100644 examples/rp2/pwm_fade.py create mode 100644 ports/rp2/CMakeLists.txt create mode 100644 ports/rp2/Makefile create mode 100644 ports/rp2/README.md create mode 100644 ports/rp2/machine_adc.c create mode 100644 ports/rp2/machine_i2c.c create mode 100644 ports/rp2/machine_pin.c create mode 100644 ports/rp2/machine_pwm.c create mode 100644 ports/rp2/machine_spi.c create mode 100644 ports/rp2/machine_timer.c create mode 100644 ports/rp2/machine_uart.c create mode 100644 ports/rp2/machine_wdt.c create mode 100644 ports/rp2/main.c create mode 100644 ports/rp2/manifest.py create mode 100644 ports/rp2/memmap_mp.ld create mode 100644 ports/rp2/micropy_extmod.cmake create mode 100644 ports/rp2/micropy_py.cmake create mode 100644 ports/rp2/micropy_rules.cmake create mode 100644 ports/rp2/modmachine.c create mode 100644 ports/rp2/modmachine.h create mode 100644 ports/rp2/modrp2.c create mode 100644 ports/rp2/modrp2.h create mode 100644 ports/rp2/modules/_boot.py create mode 100644 ports/rp2/modules/rp2.py create mode 100644 ports/rp2/moduos.c create mode 100644 ports/rp2/modutime.c create mode 100644 ports/rp2/mpconfigport.h create mode 100644 ports/rp2/mphalport.c create mode 100644 ports/rp2/mphalport.h create mode 100644 ports/rp2/mpthreadport.c create mode 100644 ports/rp2/mpthreadport.h create mode 100644 ports/rp2/qstrdefsport.h create mode 100644 ports/rp2/rp2_flash.c create mode 100644 ports/rp2/rp2_pio.c create mode 100644 ports/rp2/tusb_config.h create mode 100644 ports/rp2/tusb_port.c create mode 100644 ports/rp2/uart.c create mode 100644 ports/rp2/uart.h diff --git a/examples/rp2/pio_1hz.py b/examples/rp2/pio_1hz.py new file mode 100644 index 0000000000000..c18aa22fc0ece --- /dev/null +++ b/examples/rp2/pio_1hz.py @@ -0,0 +1,35 @@ +# Example using PIO to blink an LED and raise an IRQ at 1Hz. + +import time +from machine import Pin +import rp2 + + +@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) +def blink_1hz(): + # fmt: off + # Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000 + irq(rel(0)) + set(pins, 1) + set(x, 31) [5] + label("delay_high") + nop() [29] + jmp(x_dec, "delay_high") + + # Cycles: 1 + 7 + 32 * (30 + 1) = 1000 + set(pins, 0) + set(x, 31) [6] + label("delay_low") + nop() [29] + jmp(x_dec, "delay_low") + # fmt: on + + +# Create the StateMachine with the blink_1hz program, outputting on Pin(25). +sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25)) + +# Set the IRQ handler to print the millisecond timestamp. +sm.irq(lambda p: print(time.ticks_ms())) + +# Start the StateMachine. +sm.active(1) diff --git a/examples/rp2/pio_exec.py b/examples/rp2/pio_exec.py new file mode 100644 index 0000000000000..d8cbc33ef015a --- /dev/null +++ b/examples/rp2/pio_exec.py @@ -0,0 +1,27 @@ +# Example using PIO to turn on an LED via an explicit exec. +# +# Demonstrates: +# - using set_init and set_base +# - using StateMachine.exec + +import time +from machine import Pin +import rp2 + +# Define an empty program that uses a single set pin. +@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) +def prog(): + pass + + +# Construct the StateMachine, binding Pin(25) to the set pin. +sm = rp2.StateMachine(0, prog, set_base=Pin(25)) + +# Turn on the set pin via an exec instruction. +sm.exec("set(pins, 1)") + +# Sleep for 500ms. +time.sleep(0.5) + +# Turn off the set pin via an exec instruction. +sm.exec("set(pins, 0)") diff --git a/examples/rp2/pio_pinchange.py b/examples/rp2/pio_pinchange.py new file mode 100644 index 0000000000000..767c8e78c2eef --- /dev/null +++ b/examples/rp2/pio_pinchange.py @@ -0,0 +1,46 @@ +# Example using PIO to wait for a pin change and raise an IRQ. +# +# Demonstrates: +# - PIO wrapping +# - PIO wait instruction, waiting on an input pin +# - PIO irq instruction, in blocking mode with relative IRQ number +# - setting the in_base pin for a StateMachine +# - setting an irq handler for a StateMachine +# - instantiating 2x StateMachine's with the same program and different pins + +import time +from machine import Pin +import rp2 + + +@rp2.asm_pio() +def wait_pin_low(): + wrap_target() + + wait(0, pin, 0) + irq(block, rel(0)) + wait(1, pin, 0) + + wrap() + + +def handler(sm): + # Print a (wrapping) timestamp, and the state machine object. + print(time.ticks_ms(), sm) + + +# Instantiate StateMachine(0) with wait_pin_low program on Pin(16). +pin16 = Pin(16, Pin.IN, Pin.PULL_UP) +sm0 = rp2.StateMachine(0, wait_pin_low, in_base=pin16) +sm0.irq(handler) + +# Instantiate StateMachine(1) with wait_pin_low program on Pin(17). +pin17 = Pin(17, Pin.IN, Pin.PULL_UP) +sm1 = rp2.StateMachine(1, wait_pin_low, in_base=pin17) +sm1.irq(handler) + +# Start the StateMachine's running. +sm0.active(1) +sm1.active(1) + +# Now, when Pin(16) or Pin(17) is pulled low a message will be printed to the REPL. diff --git a/examples/rp2/pio_pwm.py b/examples/rp2/pio_pwm.py new file mode 100644 index 0000000000000..8e87448ca8029 --- /dev/null +++ b/examples/rp2/pio_pwm.py @@ -0,0 +1,45 @@ +# Example of using PIO for PWM, and fading the brightness of an LED + +from machine import Pin +from rp2 import PIO, StateMachine, asm_pio +from time import sleep + + +@asm_pio(sideset_init=PIO.OUT_LOW) +def pwm_prog(): + # fmt: off + pull(noblock) .side(0) + mov(x, osr) # Keep most recent pull data stashed in X, for recycling by noblock + mov(y, isr) # ISR must be preloaded with PWM count max + label("pwmloop") + jmp(x_not_y, "skip") + nop() .side(1) + label("skip") + jmp(y_dec, "pwmloop") + # fmt: on + + +class PIOPWM: + def __init__(self, sm_id, pin, max_count, count_freq): + self._sm = StateMachine(sm_id, pwm_prog, freq=2 * count_freq, sideset_base=Pin(pin)) + # Use exec() to load max count into ISR + self._sm.put(max_count) + self._sm.exec("pull()") + self._sm.exec("mov(isr, osr)") + self._sm.active(1) + self._max_count = max_count + + def set(self, value): + # Minimum value is -1 (completely turn off), 0 actually still produces narrow pulse + value = max(value, -1) + value = min(value, self._max_count) + self._sm.put(value) + + +# Pin 25 is LED on Pico boards +pwm = PIOPWM(0, 25, max_count=(1 << 16) - 1, count_freq=10_000_000) + +while True: + for i in range(256): + pwm.set(i ** 2) + sleep(0.01) diff --git a/examples/rp2/pio_uart_tx.py b/examples/rp2/pio_uart_tx.py new file mode 100644 index 0000000000000..0f8c1260bd7da --- /dev/null +++ b/examples/rp2/pio_uart_tx.py @@ -0,0 +1,44 @@ +# Example using PIO to create a UART TX interface + +from machine import Pin +from rp2 import PIO, StateMachine, asm_pio + +UART_BAUD = 115200 +PIN_BASE = 10 +NUM_UARTS = 8 + + +@asm_pio(sideset_init=PIO.OUT_HIGH, out_init=PIO.OUT_HIGH, out_shiftdir=PIO.SHIFT_RIGHT) +def uart_tx(): + # fmt: off + # Block with TX deasserted until data available + pull() + # Initialise bit counter, assert start bit for 8 cycles + set(x, 7) .side(0) [7] + # Shift out 8 data bits, 8 execution cycles per bit + label("bitloop") + out(pins, 1) [6] + jmp(x_dec, "bitloop") + # Assert stop bit for 8 cycles total (incl 1 for pull()) + nop() .side(1) [6] + # fmt: on + + +# Now we add 8 UART TXs, on pins 10 to 17. Use the same baud rate for all of them. +uarts = [] +for i in range(NUM_UARTS): + sm = StateMachine( + i, uart_tx, freq=8 * UART_BAUD, sideset_base=Pin(PIN_BASE + i), out_base=Pin(PIN_BASE + i) + ) + sm.active(1) + uarts.append(sm) + +# We can print characters from each UART by pushing them to the TX FIFO +def pio_uart_print(sm, s): + for c in s: + sm.put(ord(c)) + + +# Print a different message from each UART +for i, u in enumerate(uarts): + pio_uart_print(u, "Hello from UART {}!\n".format(i)) diff --git a/examples/rp2/pio_ws2812.py b/examples/rp2/pio_ws2812.py new file mode 100644 index 0000000000000..dd021a9d5642d --- /dev/null +++ b/examples/rp2/pio_ws2812.py @@ -0,0 +1,59 @@ +# Example using PIO to drive a set of WS2812 LEDs. + +import array, time +from machine import Pin +import rp2 + +# Configure the number of WS2812 LEDs. +NUM_LEDS = 8 + + +@rp2.asm_pio( + sideset_init=rp2.PIO.OUT_LOW, + out_shiftdir=rp2.PIO.SHIFT_LEFT, + autopull=True, + pull_thresh=24, +) +def ws2812(): + # fmt: off + T1 = 2 + T2 = 5 + T3 = 3 + wrap_target() + label("bitloop") + out(x, 1) .side(0) [T3 - 1] + jmp(not_x, "do_zero") .side(1) [T1 - 1] + jmp("bitloop") .side(1) [T2 - 1] + label("do_zero") + nop() .side(0) [T2 - 1] + wrap() + # fmt: on + + +# Create the StateMachine with the ws2812 program, outputting on Pin(22). +sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(22)) + +# Start the StateMachine, it will wait for data on its FIFO. +sm.active(1) + +# Display a pattern on the LEDs via an array of LED RGB values. +ar = array.array("I", [0 for _ in range(NUM_LEDS)]) + +# Cycle colours. +for i in range(4 * NUM_LEDS): + for j in range(NUM_LEDS): + r = j * 100 // (NUM_LEDS - 1) + b = 100 - j * 100 // (NUM_LEDS - 1) + if j != i % NUM_LEDS: + r >>= 3 + b >>= 3 + ar[j] = r << 16 | b + sm.put(ar, 8) + time.sleep_ms(50) + +# Fade out. +for i in range(24): + for j in range(NUM_LEDS): + ar[j] = ar[j] >> 1 & 0x7F7F7F + sm.put(ar, 8) + time.sleep_ms(50) diff --git a/examples/rp2/pwm_fade.py b/examples/rp2/pwm_fade.py new file mode 100644 index 0000000000000..7264edaa293a1 --- /dev/null +++ b/examples/rp2/pwm_fade.py @@ -0,0 +1,25 @@ +# Example using PWM to fade an LED. + +import time +from machine import Pin, PWM + + +# Construct PWM object, with LED on Pin(25). +pwm = PWM(Pin(25)) + +# Set the PWM frequency. +pwm.freq(1000) + +# Fade the LED in and out a few times. +duty = 0 +direction = 1 +for _ in range(8 * 256): + duty += direction + if duty > 255: + duty = 255 + direction = -1 + elif duty < 0: + duty = 0 + direction = 1 + pwm.duty_u16(duty * duty) + time.sleep(0.001) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt new file mode 100644 index 0000000000000..400cb8480e849 --- /dev/null +++ b/ports/rp2/CMakeLists.txt @@ -0,0 +1,162 @@ +cmake_minimum_required(VERSION 3.12) + +# Set build type to reduce firmware size +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE MinSizeRel) +endif() + +# Set main target and component locations +set(MICROPYTHON_TARGET firmware) +get_filename_component(MPY_DIR "../.." ABSOLUTE) +if (PICO_SDK_PATH_OVERRIDE) + set(PICO_SDK_PATH ${PICO_SDK_PATH_OVERRIDE}) +else() + set(PICO_SDK_PATH ../../lib/pico-sdk) +endif() + +# Include component cmake fragments +include(micropy_py.cmake) +include(micropy_extmod.cmake) +include(${PICO_SDK_PATH}/pico_sdk_init.cmake) + +# Define the top-level project +project(${MICROPYTHON_TARGET}) + +pico_sdk_init() + +add_executable(${MICROPYTHON_TARGET}) + +set(SOURCE_LIB + ${MPY_DIR}/lib/littlefs/lfs1.c + ${MPY_DIR}/lib/littlefs/lfs1_util.c + ${MPY_DIR}/lib/littlefs/lfs2.c + ${MPY_DIR}/lib/littlefs/lfs2_util.c + ${MPY_DIR}/lib/mp-readline/readline.c + ${MPY_DIR}/lib/oofatfs/ff.c + ${MPY_DIR}/lib/oofatfs/ffunicode.c + ${MPY_DIR}/lib/timeutils/timeutils.c + ${MPY_DIR}/lib/utils/gchelper_m0.s + ${MPY_DIR}/lib/utils/gchelper_native.c + ${MPY_DIR}/lib/utils/mpirq.c + ${MPY_DIR}/lib/utils/stdout_helpers.c + ${MPY_DIR}/lib/utils/sys_stdio_mphal.c + ${MPY_DIR}/lib/utils/pyexec.c +) + +set(SOURCE_DRIVERS + ${MPY_DIR}/drivers/bus/softspi.c +) + +set(SOURCE_PORT + machine_adc.c + machine_i2c.c + machine_pin.c + machine_pwm.c + machine_spi.c + machine_timer.c + machine_uart.c + machine_wdt.c + main.c + modmachine.c + modrp2.c + moduos.c + modutime.c + mphalport.c + mpthreadport.c + rp2_flash.c + rp2_pio.c + tusb_port.c + uart.c +) + +set(SOURCE_QSTR + ${SOURCE_PY} + ${SOURCE_EXTMOD} + ${MPY_DIR}/lib/utils/mpirq.c + ${MPY_DIR}/lib/utils/sys_stdio_mphal.c + ${PROJECT_SOURCE_DIR}/machine_adc.c + ${PROJECT_SOURCE_DIR}/machine_i2c.c + ${PROJECT_SOURCE_DIR}/machine_pin.c + ${PROJECT_SOURCE_DIR}/machine_pwm.c + ${PROJECT_SOURCE_DIR}/machine_spi.c + ${PROJECT_SOURCE_DIR}/machine_timer.c + ${PROJECT_SOURCE_DIR}/machine_uart.c + ${PROJECT_SOURCE_DIR}/machine_wdt.c + ${PROJECT_SOURCE_DIR}/modmachine.c + ${PROJECT_SOURCE_DIR}/modrp2.c + ${PROJECT_SOURCE_DIR}/moduos.c + ${PROJECT_SOURCE_DIR}/modutime.c + ${PROJECT_SOURCE_DIR}/rp2_flash.c + ${PROJECT_SOURCE_DIR}/rp2_pio.c +) + +set(MPY_QSTR_DEFS ${PROJECT_SOURCE_DIR}/qstrdefsport.h) + +# Define mpy-cross flags and frozen manifest +set(MPY_CROSS_FLAGS -march=armv7m) +set(FROZEN_MANIFEST ${PROJECT_SOURCE_DIR}/manifest.py) + +include(micropy_rules.cmake) + +target_sources(${MICROPYTHON_TARGET} PRIVATE + ${SOURCE_PY} + ${SOURCE_EXTMOD} + ${SOURCE_LIB} + ${SOURCE_DRIVERS} + ${SOURCE_PORT} +) + +target_include_directories(${MICROPYTHON_TARGET} PRIVATE + "${PROJECT_SOURCE_DIR}" + "${MPY_DIR}" + "${CMAKE_BINARY_DIR}" + ) + +target_compile_options(${MICROPYTHON_TARGET} PRIVATE + -Wall + #-Werror + -DFFCONF_H=\"${MPY_DIR}/lib/oofatfs/ffconf.h\" + -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT + -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT +) + +target_compile_definitions(${MICROPYTHON_TARGET} PRIVATE + PICO_FLOAT_PROPAGATE_NANS=1 + PICO_STACK_SIZE=0x2000 + PICO_CORE1_STACK_SIZE=0 + PICO_PROGRAM_NAME="MicroPython" + PICO_NO_PROGRAM_VERSION_STRING=1 # do it ourselves in main.c + MICROPY_BUILD_TYPE="${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} ${CMAKE_BUILD_TYPE}" + PICO_NO_BI_STDIO_UART=1 # we call it UART REPL +) + +target_link_libraries(${MICROPYTHON_TARGET} + hardware_adc + hardware_dma + hardware_flash + hardware_i2c + hardware_pio + hardware_pwm + hardware_rtc + hardware_spi + hardware_sync + pico_multicore + pico_stdlib_headers + pico_stdlib + tinyusb_device +) + +# todo this is a bit brittle, but we want to move a few source files into RAM (which requires +# a linker script modification) until we explicitly add macro calls around the function +# defs to move them into RAM. +if (PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM) + pico_set_linker_script(${MICROPYTHON_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp.ld) +endif() + +pico_add_extra_outputs(${MICROPYTHON_TARGET}) + +add_custom_command(TARGET ${MICROPYTHON_TARGET} + POST_BUILD + COMMAND arm-none-eabi-size --format=berkeley ${PROJECT_BINARY_DIR}/${MICROPYTHON_TARGET}.elf + VERBATIM +) diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile new file mode 100644 index 0000000000000..08cd53dcca68c --- /dev/null +++ b/ports/rp2/Makefile @@ -0,0 +1,14 @@ +# Makefile for micropython on Raspberry Pi RP2 +# +# This is a simple wrapper around cmake + +BUILD = build + +$(VERBOSE)MAKESILENT = -s + +all: + [ -d $(BUILD) ] || cmake -S . -B $(BUILD) -DPICO_BUILD_DOCS=0 + $(MAKE) $(MAKESILENT) -C $(BUILD) + +clean: + $(RM) -rf $(BUILD) diff --git a/ports/rp2/README.md b/ports/rp2/README.md new file mode 100644 index 0000000000000..d49550c5c4bd2 --- /dev/null +++ b/ports/rp2/README.md @@ -0,0 +1,100 @@ +# The RP2 port + +This is a port of MicroPython to the Raspberry Pi RP2 series of microcontrollers. +Currently supported features are: + +- REPL over USB VCP, and optionally over UART (on GP0/GP1). +- Filesystem on the internal flash, using littlefs2. +- Support for native code generation and inline assembler. +- `utime` module with sleep, time and ticks functions. +- `uos` module with VFS support. +- `machine` module with the following classes: `Pin`, `ADC`, `PWM`, `I2C`, `SPI`, + `SoftI2C`, `SoftSPI`, `Timer`, `UART`, `WDT`. +- `rp2` module with programmable IO (PIO) support. + +See the `examples/rp2/` directory for some example code. + +## Building + +The MicroPython cross-compiler must be built first, which will be used to +pre-compile (freeze) built-in Python code. This cross-compiler is built and +run on the host machine using: + + $ make -C mpy-cross + +This command should be executed from the root directory of this repository. +All other commands below should be executed from the ports/rp2/ directory. + +Building of the RP2 firmware is done entirely using CMake, although a simple +Makefile is also provided as a convenience. To build the firmware run (from +this directory): + + $ make clean + $ make + +You can also build the standard CMake way. The final firmware is found in +the top-level of the CMake build directory (`build` by default) and is +called `firmware.uf2`. + +## Deploying firmware to the device + +Firmware can be deployed to the device by putting it into bootloader mode +(hold down BOOTSEL while powering on or resetting) and then copying +`firmware.uf2` to the USB mass storage device that appears. + +If MicroPython is already installed then the bootloader can be entered by +executing `import machine; machine.bootloader()` at the REPL. + +## Sample code + +The following samples can be easily run on the board by entering paste mode +with Ctrl-E at the REPL, then cut-and-pasting the sample code to the REPL, then +executing the code with Ctrl-D. + +### Blinky + +This blinks the on-board LED on the Pico board at 1.25Hz, using a Timer object +with a callback. + +```python +from machine import Pin, Timer +led = Pin(25, Pin.OUT) +tim = Timer() +def tick(timer): + global led + led.toggle() + +tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick) +``` + +### PIO blinky + +This blinks the on-board LED on the Pico board at 1Hz, using a PIO peripheral and +PIO assembler to directly toggle the LED at the required rate. + +```python +from machine import Pin +import rp2 + +@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) +def blink_1hz(): + # Turn on the LED and delay, taking 1000 cycles. + set(pins, 1) + set(x, 31) [6] + label("delay_high") + nop() [29] + jmp(x_dec, "delay_high") + + # Turn off the LED and delay, taking 1000 cycles. + set(pins, 0) + set(x, 31) [6] + label("delay_low") + nop() [29] + jmp(x_dec, "delay_low") + +# Create StateMachine(0) with the blink_1hz program, outputting on Pin(25). +sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25)) +sm.active(1) +``` + +See the `examples/rp2/` directory for further example code. diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c new file mode 100644 index 0000000000000..f0f367151cab5 --- /dev/null +++ b/ports/rp2/machine_adc.c @@ -0,0 +1,120 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "hardware/adc.h" + +#define ADC_IS_VALID_GPIO(gpio) ((gpio) >= 26 && (gpio) <= 29) +#define ADC_CHANNEL_FROM_GPIO(gpio) ((gpio) - 26) +#define ADC_CHANNEL_TEMPSENSOR (4) + +STATIC uint16_t adc_config_and_read_u16(uint32_t channel) { + adc_select_input(channel); + uint32_t raw = adc_read(); + const uint32_t bits = 12; + // Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16) + return raw << (16 - bits) | raw >> (2 * bits - 16); +} + +/******************************************************************************/ +// MicroPython bindings for machine.ADC + +const mp_obj_type_t machine_adc_type; + +typedef struct _machine_adc_obj_t { + mp_obj_base_t base; + uint32_t channel; +} machine_adc_obj_t; + +STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "", self->channel); +} + +// ADC(id) +STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // Check number of arguments + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + mp_obj_t source = all_args[0]; + + uint32_t channel; + if (mp_obj_is_int(source)) { + // Get and validate channel number. + channel = mp_obj_get_int(source); + if (!((channel >= 0 && channel <= ADC_CHANNEL_TEMPSENSOR) || ADC_IS_VALID_GPIO(channel))) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid channel")); + } + + } else { + // Get GPIO and check it has ADC capabilities. + channel = mp_hal_get_pin_obj(source); + if (!ADC_IS_VALID_GPIO(channel)) { + mp_raise_ValueError(MP_ERROR_TEXT("Pin doesn't have ADC capabilities")); + } + } + + adc_init(); + + if (ADC_IS_VALID_GPIO(channel)) { + // Configure the GPIO pin in ADC mode. + adc_gpio_init(channel); + channel = ADC_CHANNEL_FROM_GPIO(channel); + } else if (channel == ADC_CHANNEL_TEMPSENSOR) { + // Enable temperature sensor. + adc_set_temp_sensor_enabled(1); + } + + // Create ADC object. + machine_adc_obj_t *o = m_new_obj(machine_adc_obj_t); + o->base.type = &machine_adc_type; + o->channel = channel; + + return MP_OBJ_FROM_PTR(o); +} + +// read_u16() +STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(adc_config_and_read_u16(self->channel)); +} +MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); + +STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, + + { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); + +const mp_obj_type_t machine_adc_type = { + { &mp_type_type }, + .name = MP_QSTR_ADC, + .print = machine_adc_print, + .make_new = machine_adc_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, +}; diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c new file mode 100644 index 0000000000000..0852cc19938e1 --- /dev/null +++ b/ports/rp2/machine_i2c.c @@ -0,0 +1,161 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/mperrno.h" +#include "extmod/machine_i2c.h" +#include "modmachine.h" + +#include "hardware/i2c.h" + +#define DEFAULT_I2C_FREQ (400000) +#define DEFAULT_I2C0_SCL (9) +#define DEFAULT_I2C0_SDA (8) +#define DEFAULT_I2C1_SCL (7) +#define DEFAULT_I2C1_SDA (6) + +// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. +#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) +#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) + +typedef struct _machine_i2c_obj_t { + mp_obj_base_t base; + i2c_inst_t *const i2c_inst; + uint8_t i2c_id; + uint8_t scl; + uint8_t sda; + uint32_t freq; +} machine_i2c_obj_t; + +STATIC machine_i2c_obj_t machine_i2c_obj[] = { + {{&machine_hw_i2c_type}, i2c0, 0, DEFAULT_I2C0_SCL, DEFAULT_I2C0_SDA, 0}, + {{&machine_hw_i2c_type}, i2c1, 1, DEFAULT_I2C1_SCL, DEFAULT_I2C1_SDA, 0}, +}; + +STATIC void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "I2C(%u, freq=%u, scl=%u, sda=%u)", + self->i2c_id, self->freq, self->scl, self->sda); +} + +mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_freq, ARG_scl, ARG_sda }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} }, + { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse args. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get I2C bus. + int i2c_id = mp_obj_get_int(args[ARG_id].u_obj); + if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_obj)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); + } + + // Get static peripheral object. + machine_i2c_obj_t *self = (machine_i2c_obj_t *)&machine_i2c_obj[i2c_id]; + + // Set SCL/SDA pins if configured. + if (args[ARG_scl].u_obj != mp_const_none) { + int scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); + if (!IS_VALID_SCL(self->i2c_id, scl)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin")); + } + self->scl = scl; + } + if (args[ARG_sda].u_obj != mp_const_none) { + int sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); + if (!IS_VALID_SDA(self->i2c_id, sda)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin")); + } + self->sda = sda; + } + + // Initialise the I2C peripheral if any arguments given, or it was not initialised previously. + if (n_args > 1 || n_kw > 0 || self->freq == 0) { + self->freq = args[ARG_freq].u_int; + i2c_init(self->i2c_inst, self->freq); + self->freq = i2c_set_baudrate(self->i2c_inst, self->freq); + gpio_set_function(self->scl, GPIO_FUNC_I2C); + gpio_set_function(self->sda, GPIO_FUNC_I2C); + gpio_set_pulls(self->scl, true, 0); + gpio_set_pulls(self->sda, true, 0); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) { + machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in; + int ret; + bool nostop = !(flags & MP_MACHINE_I2C_FLAG_STOP); + if (flags & MP_MACHINE_I2C_FLAG_READ) { + ret = i2c_read_blocking(self->i2c_inst, addr, buf, len, nostop); + } else { + if (len <= 2) { + // Workaround issue with hardware I2C not accepting short writes. + mp_machine_soft_i2c_obj_t soft_i2c = { + .base = { &mp_machine_soft_i2c_type }, + .us_delay = 500000 / self->freq + 1, + .us_timeout = 255, + .scl = self->scl, + .sda = self->sda, + }; + mp_machine_i2c_buf_t bufs = { + .len = len, + .buf = buf, + }; + mp_hal_pin_open_drain(self->scl); + mp_hal_pin_open_drain(self->sda); + ret = mp_machine_soft_i2c_transfer(&soft_i2c.base, addr, 1, &bufs, flags); + gpio_set_function(self->scl, GPIO_FUNC_I2C); + gpio_set_function(self->sda, GPIO_FUNC_I2C); + } else { + ret = i2c_write_blocking(self->i2c_inst, addr, buf, len, nostop); + } + } + return (ret < 0) ? -MP_EIO : ret; +} + +STATIC const mp_machine_i2c_p_t machine_i2c_p = { + .transfer = mp_machine_i2c_transfer_adaptor, + .transfer_single = machine_i2c_transfer_single, +}; + +const mp_obj_type_t machine_hw_i2c_type = { + { &mp_type_type }, + .name = MP_QSTR_I2C, + .print = machine_i2c_print, + .make_new = machine_i2c_make_new, + .protocol = &machine_i2c_p, + .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, +}; diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c new file mode 100644 index 0000000000000..f798c10fdf7ac --- /dev/null +++ b/ports/rp2/machine_pin.c @@ -0,0 +1,449 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "py/runtime.h" +#include "py/mphal.h" +#include "lib/utils/mpirq.h" +#include "modmachine.h" +#include "extmod/virtpin.h" + +#include "hardware/irq.h" +#include "hardware/regs/intctrl.h" +#include "hardware/structs/iobank0.h" +#include "hardware/structs/padsbank0.h" + +#define GPIO_MODE_IN (0) +#define GPIO_MODE_OUT (1) +#define GPIO_MODE_OPEN_DRAIN (2) +#define GPIO_MODE_ALT (3) + +// These can be or'd together. +#define GPIO_PULL_UP (1) +#define GPIO_PULL_DOWN (2) + +#define GPIO_IRQ_ALL (0xf) + +// Macros to access the state of the hardware. +#define GPIO_GET_FUNCSEL(id) ((iobank0_hw->io[(id)].ctrl & IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS) >> IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB) +#define GPIO_IS_OUT(id) (sio_hw->gpio_oe & (1 << (id))) +#define GPIO_IS_PULL_UP(id) (padsbank0_hw->io[(id)] & PADS_BANK0_GPIO0_PUE_BITS) +#define GPIO_IS_PULL_DOWN(id) (padsbank0_hw->io[(id)] & PADS_BANK0_GPIO0_PDE_BITS) + +// Open drain behaviour is simulated. +#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask & (1 << (id))) + +typedef struct _machine_pin_obj_t { + mp_obj_base_t base; + uint32_t id; +} machine_pin_obj_t; + +typedef struct _machine_pin_irq_obj_t { + mp_irq_obj_t base; + uint32_t flags; + uint32_t trigger; +} machine_pin_irq_obj_t; + +STATIC const mp_irq_methods_t machine_pin_irq_methods; + +STATIC const machine_pin_obj_t machine_pin_obj[N_GPIOS] = { + {{&machine_pin_type}, 0}, + {{&machine_pin_type}, 1}, + {{&machine_pin_type}, 2}, + {{&machine_pin_type}, 3}, + {{&machine_pin_type}, 4}, + {{&machine_pin_type}, 5}, + {{&machine_pin_type}, 6}, + {{&machine_pin_type}, 7}, + {{&machine_pin_type}, 8}, + {{&machine_pin_type}, 9}, + {{&machine_pin_type}, 10}, + {{&machine_pin_type}, 11}, + {{&machine_pin_type}, 12}, + {{&machine_pin_type}, 13}, + {{&machine_pin_type}, 14}, + {{&machine_pin_type}, 15}, + {{&machine_pin_type}, 16}, + {{&machine_pin_type}, 17}, + {{&machine_pin_type}, 18}, + {{&machine_pin_type}, 19}, + {{&machine_pin_type}, 20}, + {{&machine_pin_type}, 21}, + {{&machine_pin_type}, 22}, + {{&machine_pin_type}, 23}, + {{&machine_pin_type}, 24}, + {{&machine_pin_type}, 25}, + {{&machine_pin_type}, 26}, + {{&machine_pin_type}, 27}, + {{&machine_pin_type}, 28}, + {{&machine_pin_type}, 29}, +}; + +// Mask with "1" indicating that the corresponding pin is in simulated open-drain mode. +uint32_t machine_pin_open_drain_mask; + +STATIC void gpio_irq(void) { + for (int i = 0; i < 4; ++i) { + uint32_t intr = iobank0_hw->intr[i]; + if (intr) { + for (int j = 0; j < 8; ++j) { + if (intr & 0xf) { + uint32_t gpio = 8 * i + j; + gpio_acknowledge_irq(gpio, intr & 0xf); + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[gpio]); + if (irq != NULL && (intr & irq->trigger)) { + irq->flags = intr & irq->trigger; + mp_irq_handler(&irq->base); + } + } + intr >>= 4; + } + } + } +} + +void machine_pin_init(void) { + memset(MP_STATE_PORT(machine_pin_irq_obj), 0, sizeof(MP_STATE_PORT(machine_pin_irq_obj))); + irq_set_exclusive_handler(IO_IRQ_BANK0, gpio_irq); + irq_set_enabled(IO_IRQ_BANK0, true); +} + +void machine_pin_deinit(void) { + for (int i = 0; i < N_GPIOS; ++i) { + gpio_set_irq_enabled(i, GPIO_IRQ_ALL, false); + } + irq_set_enabled(IO_IRQ_BANK0, false); + irq_remove_handler(IO_IRQ_BANK0, gpio_irq); +} + +STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_pin_obj_t *self = self_in; + uint funcsel = GPIO_GET_FUNCSEL(self->id); + qstr mode_qst; + if (funcsel == GPIO_FUNC_SIO) { + if (GPIO_IS_OPEN_DRAIN(self->id)) { + mode_qst = MP_QSTR_OPEN_DRAIN; + } else if (GPIO_IS_OUT(self->id)) { + mode_qst = MP_QSTR_OUT; + } else { + mode_qst = MP_QSTR_IN; + } + } else { + mode_qst = MP_QSTR_ALT; + } + mp_printf(print, "Pin(%u, mode=%q", self->id, mode_qst); + bool pull_up = false; + if (GPIO_IS_PULL_UP(self->id)) { + mp_printf(print, ", pull=%q", MP_QSTR_PULL_UP); + pull_up = true; + } + if (GPIO_IS_PULL_DOWN(self->id)) { + if (pull_up) { + mp_printf(print, "|%q", MP_QSTR_PULL_DOWN); + } else { + mp_printf(print, ", pull=%q", MP_QSTR_PULL_DOWN); + } + } + if (funcsel != GPIO_FUNC_SIO) { + mp_printf(print, ", alt=%u", funcsel); + } + mp_printf(print, ")"); +} + +// pin.init(mode, pull=None, *, value=None, alt=FUNC_SIO) +STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_mode, ARG_pull, ARG_value, ARG_alt }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = GPIO_FUNC_SIO}}, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // set initial value (do this before configuring mode/pull) + if (args[ARG_value].u_obj != mp_const_none) { + gpio_put(self->id, mp_obj_is_true(args[ARG_value].u_obj)); + } + + // configure mode + if (args[ARG_mode].u_obj != mp_const_none) { + mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); + if (mode == GPIO_MODE_IN) { + mp_hal_pin_input(self->id); + } else if (mode == GPIO_MODE_OUT) { + mp_hal_pin_output(self->id); + } else if (mode == GPIO_MODE_OPEN_DRAIN) { + mp_hal_pin_open_drain(self->id); + } else { + // Alternate function. + gpio_set_function(self->id, args[ARG_alt].u_int); + machine_pin_open_drain_mask &= ~(1 << self->id); + } + } + + // configure pull (unconditionally because None means no-pull) + uint32_t pull = 0; + if (args[ARG_pull].u_obj != mp_const_none) { + pull = mp_obj_get_int(args[ARG_pull].u_obj); + } + gpio_set_pulls(self->id, pull & GPIO_PULL_UP, pull & GPIO_PULL_DOWN); + + return mp_const_none; +} + +// constructor(id, ...) +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + // get the wanted pin object + int wanted_pin = mp_obj_get_int(args[0]); + if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj))) { + mp_raise_ValueError("invalid pin"); + } + const machine_pin_obj_t *self = &machine_pin_obj[wanted_pin]; + + if (n_args > 1 || n_kw > 0) { + // pin mode given, so configure this GPIO + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_pin_obj_init_helper(self, n_args - 1, args + 1, &kw_args); + } + + return MP_OBJ_FROM_PTR(self); +} + +// fast method for getting/setting pin value +STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); + machine_pin_obj_t *self = self_in; + if (n_args == 0) { + // get pin + return MP_OBJ_NEW_SMALL_INT(gpio_get(self->id)); + } else { + // set pin + bool value = mp_obj_is_true(args[0]); + if (GPIO_IS_OPEN_DRAIN(self->id)) { + MP_STATIC_ASSERT(GPIO_IN == 0 && GPIO_OUT == 1); + gpio_set_dir(self->id, 1 - value); + } else { + gpio_put(self->id, value); + } + return mp_const_none; + } +} + +// pin.init(mode, pull) +STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args); +} +MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init); + +// pin.value([value]) +STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { + return machine_pin_call(args[0], n_args - 1, 0, args + 1); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); + +// pin.low() +STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (GPIO_IS_OPEN_DRAIN(self->id)) { + gpio_set_dir(self->id, GPIO_OUT); + } else { + gpio_clr_mask(1u << self->id); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); + +// pin.high() +STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (GPIO_IS_OPEN_DRAIN(self->id)) { + gpio_set_dir(self->id, GPIO_IN); + } else { + gpio_set_mask(1u << self->id); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); + +// pin.toggle() +STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (GPIO_IS_OPEN_DRAIN(self->id)) { + if (GPIO_IS_OUT(self->id)) { + gpio_set_dir(self->id, GPIO_IN); + } else { + gpio_set_dir(self->id, GPIO_OUT); + } + } else { + gpio_xor_mask(1u << self->id); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); + +// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False) +STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_handler, ARG_trigger, ARG_hard }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE} }, + { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, + }; + machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the IRQ object. + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]); + + // Allocate the IRQ object if it doesn't already exist. + if (irq == NULL) { + irq = m_new_obj(machine_pin_irq_obj_t); + irq->base.base.type = &mp_irq_type; + irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods; + irq->base.parent = MP_OBJ_FROM_PTR(self); + irq->base.handler = mp_const_none; + irq->base.ishard = false; + MP_STATE_PORT(machine_pin_irq_obj[self->id]) = irq; + } + + if (n_args > 1 || kw_args->used != 0) { + // Configure IRQ. + + // Disable all IRQs while data is updated. + gpio_set_irq_enabled(self->id, GPIO_IRQ_ALL, false); + + // Update IRQ data. + irq->base.handler = args[ARG_handler].u_obj; + irq->base.ishard = args[ARG_hard].u_bool; + irq->flags = 0; + irq->trigger = args[ARG_trigger].u_int; + + // Enable IRQ if a handler is given. + if (args[ARG_handler].u_obj != mp_const_none) { + gpio_set_irq_enabled(self->id, args[ARG_trigger].u_int, true); + } + } + + return MP_OBJ_FROM_PTR(irq); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq); + +STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { + // instance methods + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_pin_low_obj) }, + { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_pin_high_obj) }, + { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_low_obj) }, + { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_high_obj) }, + { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, + { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, + + // class constants + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUT) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OPEN_DRAIN) }, + { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_ALT) }, + { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) }, + { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_IRQ_EDGE_RISE) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_IRQ_EDGE_FALL) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); + +STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { + (void)errcode; + machine_pin_obj_t *self = self_in; + + switch (request) { + case MP_PIN_READ: { + return gpio_get(self->id); + } + case MP_PIN_WRITE: { + gpio_put(self->id, arg); + return 0; + } + } + return -1; +} + +STATIC const mp_pin_p_t pin_pin_p = { + .ioctl = pin_ioctl, +}; + +const mp_obj_type_t machine_pin_type = { + { &mp_type_type }, + .name = MP_QSTR_Pin, + .print = machine_pin_print, + .make_new = mp_pin_make_new, + .call = machine_pin_call, + .protocol = &pin_pin_p, + .locals_dict = (mp_obj_t)&machine_pin_locals_dict, +}; + +STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]); + gpio_set_irq_enabled(self->id, GPIO_IRQ_ALL, false); + irq->flags = 0; + irq->trigger = new_trigger; + gpio_set_irq_enabled(self->id, new_trigger, true); + return 0; +} + +STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]); + if (info_type == MP_IRQ_INFO_FLAGS) { + return irq->flags; + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return irq->trigger; + } + return 0; +} + +STATIC const mp_irq_methods_t machine_pin_irq_methods = { + .trigger = machine_pin_irq_trigger, + .info = machine_pin_irq_info, +}; + +mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { + if (!mp_obj_is_type(obj, &machine_pin_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin")); + } + machine_pin_obj_t *pin = MP_OBJ_TO_PTR(obj); + return pin->id; +} diff --git a/ports/rp2/machine_pwm.c b/ports/rp2/machine_pwm.c new file mode 100644 index 0000000000000..ff40c5503f42b --- /dev/null +++ b/ports/rp2/machine_pwm.c @@ -0,0 +1,197 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "modmachine.h" + +#include "hardware/clocks.h" +#include "hardware/pwm.h" + +/******************************************************************************/ +// MicroPython bindings for machine.PWM + +const mp_obj_type_t machine_pwm_type; + +typedef struct _machine_pwm_obj_t { + mp_obj_base_t base; + uint8_t slice; + uint8_t channel; +} machine_pwm_obj_t; + +STATIC machine_pwm_obj_t machine_pwm_obj[] = { + {{&machine_pwm_type}, 0, PWM_CHAN_A}, + {{&machine_pwm_type}, 0, PWM_CHAN_B}, + {{&machine_pwm_type}, 1, PWM_CHAN_A}, + {{&machine_pwm_type}, 1, PWM_CHAN_B}, + {{&machine_pwm_type}, 2, PWM_CHAN_A}, + {{&machine_pwm_type}, 2, PWM_CHAN_B}, + {{&machine_pwm_type}, 3, PWM_CHAN_A}, + {{&machine_pwm_type}, 3, PWM_CHAN_B}, + {{&machine_pwm_type}, 4, PWM_CHAN_A}, + {{&machine_pwm_type}, 4, PWM_CHAN_B}, + {{&machine_pwm_type}, 5, PWM_CHAN_A}, + {{&machine_pwm_type}, 5, PWM_CHAN_B}, + {{&machine_pwm_type}, 6, PWM_CHAN_A}, + {{&machine_pwm_type}, 6, PWM_CHAN_B}, + {{&machine_pwm_type}, 7, PWM_CHAN_A}, + {{&machine_pwm_type}, 7, PWM_CHAN_B}, +}; + +STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "", self->slice, self->channel); +} + +// PWM(pin) +STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // Check number of arguments + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // Get GPIO to connect to PWM. + uint32_t gpio = mp_hal_get_pin_obj(all_args[0]); + + // Get static peripheral object. + uint slice = pwm_gpio_to_slice_num(gpio); + uint8_t channel = pwm_gpio_to_channel(gpio); + const machine_pwm_obj_t *self = &machine_pwm_obj[slice * 2 + channel]; + + // Select PWM function for given GPIO. + gpio_set_function(gpio, GPIO_FUNC_PWM); + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t machine_pwm_deinit(mp_obj_t self_in) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); + pwm_set_enabled(self->slice, false); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pwm_deinit_obj, machine_pwm_deinit); + +// PWM.freq([value]) +STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *args) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t source_hz = clock_get_hz(clk_sys); + if (n_args == 1) { + // Get frequency. + uint32_t div16 = pwm_hw->slice[self->slice].div; + uint32_t top = pwm_hw->slice[self->slice].top; + uint32_t pwm_freq = 16 * source_hz / div16 / top; + return MP_OBJ_NEW_SMALL_INT(pwm_freq); + } else { + // Set the frequency, making "top" as large as possible for maximum resolution. + // Maximum "top" is set at 65534 to be able to achieve 100% duty with 65535. + #define TOP_MAX 65534 + mp_int_t freq = mp_obj_get_int(args[1]); + uint32_t div16_top = 16 * source_hz / freq; + uint32_t top = 1; + for (;;) { + // Try a few small prime factors to get close to the desired frequency. + if (div16_top >= 16 * 5 && div16_top % 5 == 0 && top * 5 <= TOP_MAX) { + div16_top /= 5; + top *= 5; + } else if (div16_top >= 16 * 3 && div16_top % 3 == 0 && top * 3 <= TOP_MAX) { + div16_top /= 3; + top *= 3; + } else if (div16_top >= 16 * 2 && top * 2 <= TOP_MAX) { + div16_top /= 2; + top *= 2; + } else { + break; + } + } + if (div16_top < 16) { + mp_raise_ValueError(MP_ERROR_TEXT("freq too large")); + } else if (div16_top >= 256 * 16) { + mp_raise_ValueError(MP_ERROR_TEXT("freq too small")); + } + pwm_hw->slice[self->slice].div = div16_top; + pwm_hw->slice[self->slice].top = top; + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_freq_obj, 1, 2, machine_pwm_freq); + +// PWM.duty_u16([value]) +STATIC mp_obj_t machine_pwm_duty_u16(size_t n_args, const mp_obj_t *args) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t top = pwm_hw->slice[self->slice].top; + if (n_args == 1) { + // Get duty cycle. + uint32_t cc = pwm_hw->slice[self->slice].cc; + cc = (cc >> (self->channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB)) & 0xffff; + return MP_OBJ_NEW_SMALL_INT(cc * 65535 / (top + 1)); + } else { + // Set duty cycle. + mp_int_t duty_u16 = mp_obj_get_int(args[1]); + uint32_t cc = duty_u16 * (top + 1) / 65535; + pwm_set_chan_level(self->slice, self->channel, cc); + pwm_set_enabled(self->slice, true); + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_duty_u16_obj, 1, 2, machine_pwm_duty_u16); + +// PWM.duty_ns([value]) +STATIC mp_obj_t machine_pwm_duty_ns(size_t n_args, const mp_obj_t *args) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t source_hz = clock_get_hz(clk_sys); + uint32_t slice_hz = 16 * source_hz / pwm_hw->slice[self->slice].div; + if (n_args == 1) { + // Get duty cycle. + uint32_t cc = pwm_hw->slice[self->slice].cc; + cc = (cc >> (self->channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB)) & 0xffff; + return MP_OBJ_NEW_SMALL_INT((uint64_t)cc * 1000000000ULL / slice_hz); + } else { + // Set duty cycle. + mp_int_t duty_ns = mp_obj_get_int(args[1]); + uint32_t cc = (uint64_t)duty_ns * slice_hz / 1000000000ULL; + if (cc > 65535) { + mp_raise_ValueError(MP_ERROR_TEXT("duty larger than period")); + } + pwm_set_chan_level(self->slice, self->channel, cc); + pwm_set_enabled(self->slice, true); + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_duty_ns_obj, 1, 2, machine_pwm_duty_ns); + +STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_pwm_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_pwm_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_duty_u16), MP_ROM_PTR(&machine_pwm_duty_u16_obj) }, + { MP_ROM_QSTR(MP_QSTR_duty_ns), MP_ROM_PTR(&machine_pwm_duty_ns_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict, machine_pwm_locals_dict_table); + +const mp_obj_type_t machine_pwm_type = { + { &mp_type_type }, + .name = MP_QSTR_PWM, + .print = machine_pwm_print, + .make_new = machine_pwm_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_pwm_locals_dict, +}; diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c new file mode 100644 index 0000000000000..478c0614557c2 --- /dev/null +++ b/ports/rp2/machine_spi.c @@ -0,0 +1,278 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/mperrno.h" +#include "extmod/machine_spi.h" +#include "modmachine.h" + +#include "hardware/spi.h" +#include "hardware/dma.h" + +#define DEFAULT_SPI_BAUDRATE (1000000) +#define DEFAULT_SPI_POLARITY (0) +#define DEFAULT_SPI_PHASE (0) +#define DEFAULT_SPI_BITS (8) +#define DEFAULT_SPI_FIRSTBIT (SPI_MSB_FIRST) +#define DEFAULT_SPI0_SCK (6) +#define DEFAULT_SPI0_MOSI (7) +#define DEFAULT_SPI0_MISO (4) +#define DEFAULT_SPI1_SCK (10) +#define DEFAULT_SPI1_MOSI (11) +#define DEFAULT_SPI1_MISO (8) + +#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) +#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) +#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) +#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) + +typedef struct _machine_spi_obj_t { + mp_obj_base_t base; + spi_inst_t *const spi_inst; + uint8_t spi_id; + uint8_t polarity; + uint8_t phase; + uint8_t bits; + uint8_t firstbit; + uint8_t sck; + uint8_t mosi; + uint8_t miso; + uint32_t baudrate; +} machine_spi_obj_t; + +STATIC machine_spi_obj_t machine_spi_obj[] = { + { + {&machine_spi_type}, spi0, 0, + DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT, + DEFAULT_SPI0_SCK, DEFAULT_SPI0_MOSI, DEFAULT_SPI0_MISO, + 0, + }, + { + {&machine_spi_type}, spi1, 1, + DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT, + DEFAULT_SPI1_SCK, DEFAULT_SPI1_MOSI, DEFAULT_SPI1_MISO, + 0, + }, +}; + +STATIC void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=%u)", + self->spi_id, self->baudrate, self->polarity, self->phase, self->bits, + self->sck, self->mosi, self->miso); +} + +mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = DEFAULT_SPI_BAUDRATE} }, + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_POLARITY} }, + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE} }, + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_BITS} }, + { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} }, + { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the SPI bus id. + int spi_id = mp_obj_get_int(args[ARG_id].u_obj); + if (spi_id < 0 || spi_id >= MP_ARRAY_SIZE(machine_spi_obj)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); + } + + // Get static peripheral object. + machine_spi_obj_t *self = (machine_spi_obj_t *)&machine_spi_obj[spi_id]; + + // Set SCK/MOSI/MISO pins if configured. + if (args[ARG_sck].u_obj != mp_const_none) { + int sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj); + if (!IS_VALID_SCK(self->spi_id, sck)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad SCK pin")); + } + self->sck = sck; + } + if (args[ARG_mosi].u_obj != mp_const_none) { + int mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj); + if (!IS_VALID_MOSI(self->spi_id, mosi)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad MOSI pin")); + } + self->mosi = mosi; + } + if (args[ARG_miso].u_obj != mp_const_none) { + int miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj); + if (!IS_VALID_MISO(self->spi_id, miso)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad MISO pin")); + } + self->miso = miso; + } + + // Initialise the SPI peripheral if any arguments given, or it was not initialised previously. + if (n_args > 1 || n_kw > 0 || self->baudrate == 0) { + self->baudrate = args[ARG_baudrate].u_int; + self->polarity = args[ARG_polarity].u_int; + self->phase = args[ARG_phase].u_int; + self->bits = args[ARG_bits].u_int; + self->firstbit = args[ARG_firstbit].u_int; + if (self->firstbit == SPI_LSB_FIRST) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB")); + } + + spi_init(self->spi_inst, self->baudrate); + self->baudrate = spi_set_baudrate(self->spi_inst, self->baudrate); + spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit); + gpio_set_function(self->sck, GPIO_FUNC_SPI); + gpio_set_function(self->miso, GPIO_FUNC_SPI); + gpio_set_function(self->mosi, GPIO_FUNC_SPI); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + }; + + // Parse the arguments. + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Reconfigure the baudrate if requested. + if (args[ARG_baudrate].u_int != -1) { + self->baudrate = spi_set_baudrate(self->spi_inst, args[ARG_baudrate].u_int); + } + + // Reconfigure the format if requested. + bool set_format = false; + if (args[ARG_polarity].u_int != -1) { + self->polarity = args[ARG_polarity].u_int; + set_format = true; + } + if (args[ARG_phase].u_int != -1) { + self->phase = args[ARG_phase].u_int; + set_format = true; + } + if (args[ARG_bits].u_int != -1) { + self->bits = args[ARG_bits].u_int; + set_format = true; + } + if (args[ARG_firstbit].u_int != -1) { + self->firstbit = args[ARG_firstbit].u_int; + if (self->firstbit == SPI_LSB_FIRST) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB")); + } + } + if (set_format) { + spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit); + } +} + +STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; + // Use DMA for large transfers if channels are available + const size_t dma_min_size_threshold = 32; + int chan_tx = -1; + int chan_rx = -1; + if (len >= dma_min_size_threshold) { + // Use two DMA channels to service the two FIFOs + chan_tx = dma_claim_unused_channel(false); + chan_rx = dma_claim_unused_channel(false); + } + bool use_dma = chan_rx >= 0 && chan_tx >= 0; + // note src is guaranteed to be non-NULL + bool write_only = dest == NULL; + + if (use_dma) { + uint8_t dev_null; + dma_channel_config c = dma_channel_get_default_config(chan_tx); + channel_config_set_transfer_data_size(&c, DMA_SIZE_8); + channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_TX : DREQ_SPI0_TX); + dma_channel_configure(chan_tx, &c, + &spi_get_hw(self->spi_inst)->dr, + src, + len, + false); + + c = dma_channel_get_default_config(chan_rx); + channel_config_set_transfer_data_size(&c, DMA_SIZE_8); + channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_RX : DREQ_SPI0_RX); + channel_config_set_read_increment(&c, false); + channel_config_set_write_increment(&c, !write_only); + dma_channel_configure(chan_rx, &c, + write_only ? &dev_null : dest, + &spi_get_hw(self->spi_inst)->dr, + len, + false); + + dma_start_channel_mask((1u << chan_rx) | (1u << chan_tx)); + dma_channel_wait_for_finish_blocking(chan_rx); + dma_channel_wait_for_finish_blocking(chan_tx); + } + + // If we have claimed only one channel successfully, we should release immediately + if (chan_rx >= 0) { + dma_channel_unclaim(chan_rx); + } + if (chan_tx >= 0) { + dma_channel_unclaim(chan_tx); + } + + if (!use_dma) { + // Use software for small transfers, or if couldn't claim two DMA channels + if (write_only) { + spi_write_blocking(self->spi_inst, src, len); + } else { + spi_write_read_blocking(self->spi_inst, src, dest, len); + } + } +} + +STATIC const mp_machine_spi_p_t machine_spi_p = { + .init = machine_spi_init, + .transfer = machine_spi_transfer, +}; + +const mp_obj_type_t machine_spi_type = { + { &mp_type_type }, + .name = MP_QSTR_SPI, + .print = machine_spi_print, + .make_new = machine_spi_make_new, + .protocol = &machine_spi_p, + .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, +}; diff --git a/ports/rp2/machine_timer.c b/ports/rp2/machine_timer.c new file mode 100644 index 0000000000000..e7e8f02d5517c --- /dev/null +++ b/ports/rp2/machine_timer.c @@ -0,0 +1,165 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" +#include "pico/time.h" + +#define ALARM_ID_INVALID (-1) +#define TIMER_MODE_ONE_SHOT (0) +#define TIMER_MODE_PERIODIC (1) + +typedef struct _machine_timer_obj_t { + mp_obj_base_t base; + struct alarm_pool *pool; + alarm_id_t alarm_id; + uint32_t mode; + uint64_t delta_us; // for periodic mode + mp_obj_t callback; +} machine_timer_obj_t; + +const mp_obj_type_t machine_timer_type; + +STATIC int64_t alarm_callback(alarm_id_t id, void *user_data) { + machine_timer_obj_t *self = user_data; + mp_sched_schedule(self->callback, MP_OBJ_FROM_PTR(self)); + if (self->mode == TIMER_MODE_ONE_SHOT) { + return 0; + } else { + return -self->delta_us; + } +} + +STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); + qstr mode = self->mode == TIMER_MODE_ONE_SHOT ? MP_QSTR_ONE_SHOT : MP_QSTR_PERIODIC; + mp_printf(print, "Timer(mode=%q, period=%u, tick_hz=1000000)", mode, self->delta_us); +} + +STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIMER_MODE_PERIODIC} }, + { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, + { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + self->mode = args[ARG_mode].u_int; + if (args[ARG_freq].u_obj != mp_const_none) { + // Frequency specified in Hz + #if MICROPY_PY_BUILTINS_FLOAT + self->delta_us = (uint64_t)(MICROPY_FLOAT_CONST(1000000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); + #else + self->delta_us = 1000000 / mp_obj_get_int(args[ARG_freq].u_obj); + #endif + } else { + // Period specified + self->delta_us = (uint64_t)args[ARG_period].u_int * 1000000 / args[ARG_tick_hz].u_int; + } + if (self->delta_us < 1) { + self->delta_us = 1; + } + + self->callback = args[ARG_callback].u_obj; + self->alarm_id = alarm_pool_add_alarm_in_us(self->pool, self->delta_us, alarm_callback, self, true); + if (self->alarm_id == -1) { + mp_raise_OSError(MP_ENOMEM); + } + + return mp_const_none; +} + +STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + machine_timer_obj_t *self = m_new_obj_with_finaliser(machine_timer_obj_t); + self->base.type = &machine_timer_type; + self->pool = alarm_pool_get_default(); + self->alarm_id = ALARM_ID_INVALID; + + // Get timer id (only soft timer (-1) supported at the moment) + mp_int_t id = -1; + if (n_args > 0) { + id = mp_obj_get_int(args[0]); + --n_args; + ++args; + } + if (id != -1) { + mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist")); + } + + if (n_args > 0 || n_kw > 0) { + // Start the timer + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_timer_init_helper(self, n_args, args, &kw_args); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]); + if (self->alarm_id != ALARM_ID_INVALID) { + alarm_pool_cancel_alarm(self->pool, self->alarm_id); + self->alarm_id = ALARM_ID_INVALID; + } + return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init); + +STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->alarm_id != ALARM_ID_INVALID) { + alarm_pool_cancel_alarm(self->pool, self->alarm_id); + self->alarm_id = ALARM_ID_INVALID; + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit); + +STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(TIMER_MODE_ONE_SHOT) }, + { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(TIMER_MODE_PERIODIC) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); + +const mp_obj_type_t machine_timer_type = { + { &mp_type_type }, + .name = MP_QSTR_Timer, + .print = machine_timer_print, + .make_new = machine_timer_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_timer_locals_dict, +}; diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c new file mode 100644 index 0000000000000..30446e688de34 --- /dev/null +++ b/ports/rp2/machine_uart.c @@ -0,0 +1,246 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/stream.h" +#include "py/mphal.h" +#include "py/mperrno.h" +#include "modmachine.h" + +#include "hardware/uart.h" + +#define DEFAULT_UART_BAUDRATE (115200) +#define DEFAULT_UART_BITS (8) +#define DEFAULT_UART_STOP (1) +#define DEFAULT_UART0_TX (0) +#define DEFAULT_UART0_RX (1) +#define DEFAULT_UART1_TX (4) +#define DEFAULT_UART1_RX (5) + +#define IS_VALID_PERIPH(uart, pin) (((((pin) + 4) & 8) >> 3) == (uart)) +#define IS_VALID_TX(uart, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(uart, pin)) +#define IS_VALID_RX(uart, pin) (((pin) & 3) == 1 && IS_VALID_PERIPH(uart, pin)) + +typedef struct _machine_uart_obj_t { + mp_obj_base_t base; + uart_inst_t *const uart; + uint8_t uart_id; + uint32_t baudrate; + uint8_t bits; + uart_parity_t parity; + uint8_t stop; + uint8_t tx; + uint8_t rx; +} machine_uart_obj_t; + +STATIC machine_uart_obj_t machine_uart_obj[] = { + {{&machine_uart_type}, uart0, 0, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART0_TX, DEFAULT_UART0_RX}, + {{&machine_uart_type}, uart1, 1, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART1_TX, DEFAULT_UART1_RX}, +}; + +STATIC const char *_parity_name[] = {"None", "0", "1"}; + +/******************************************************************************/ +// MicroPython bindings for UART + +STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d)", + self->uart_id, self->baudrate, self->bits, _parity_name[self->parity], + self->stop, self->tx, self->rx); +} + +STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_bits, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_parity, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} }, + { MP_QSTR_stop, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse args. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get UART bus. + int uart_id = mp_obj_get_int(args[ARG_id].u_obj); + if (uart_id < 0 || uart_id >= MP_ARRAY_SIZE(machine_uart_obj)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) doesn't exist"), uart_id); + } + + // Get static peripheral object. + machine_uart_obj_t *self = (machine_uart_obj_t *)&machine_uart_obj[uart_id]; + + // Set baudrate if configured. + if (args[ARG_baudrate].u_int > 0) { + self->baudrate = args[ARG_baudrate].u_int; + } + + // Set bits if configured. + if (args[ARG_bits].u_int > 0) { + self->bits = args[ARG_bits].u_int; + } + + // Set parity if configured. + if (args[ARG_parity].u_obj != MP_OBJ_NEW_SMALL_INT(-1)) { + if (args[ARG_parity].u_obj == mp_const_none) { + self->parity = UART_PARITY_NONE; + } else if (mp_obj_get_int(args[ARG_parity].u_obj) & 1) { + self->parity = UART_PARITY_ODD; + } else { + self->parity = UART_PARITY_EVEN; + } + } + + // Set stop bits if configured. + if (args[ARG_stop].u_int > 0) { + self->stop = args[ARG_stop].u_int; + } + + // Set TX/RX pins if configured. + if (args[ARG_tx].u_obj != mp_const_none) { + int tx = mp_hal_get_pin_obj(args[ARG_tx].u_obj); + if (!IS_VALID_TX(self->uart_id, tx)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad TX pin")); + } + self->tx = tx; + } + if (args[ARG_rx].u_obj != mp_const_none) { + int rx = mp_hal_get_pin_obj(args[ARG_rx].u_obj); + if (!IS_VALID_RX(self->uart_id, rx)) { + mp_raise_ValueError(MP_ERROR_TEXT("bad RX pin")); + } + self->rx = rx; + } + + // Initialise the UART peripheral if any arguments given, or it was not initialised previously. + if (n_args > 1 || n_kw > 0 || self->baudrate == 0) { + if (self->baudrate == 0) { + self->baudrate = DEFAULT_UART_BAUDRATE; + } + uart_init(self->uart, self->baudrate); + uart_set_format(self->uart, self->bits, self->stop, self->parity); + uart_set_fifo_enabled(self->uart, true); + gpio_set_function(self->tx, GPIO_FUNC_UART); + gpio_set_function(self->rx, GPIO_FUNC_UART); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(uart_is_readable(self->uart)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any); + +STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uart_set_break(self->uart, true); + mp_hal_delay_us(13000000 / self->baudrate + 1); + uart_set_break(self->uart, false); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); + +STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, + + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, + + { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); + +STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + // TODO support timeout + uint8_t *dest = buf_in; + for (size_t i = 0; i < size; ++i) { + while (!uart_is_readable(self->uart)) { + MICROPY_EVENT_POLL_HOOK + } + *dest++ = uart_get_hw(self->uart)->dr; + } + return size; +} + +STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + // TODO support timeout + const uint8_t *src = buf_in; + for (size_t i = 0; i < size; ++i) { + while (!uart_is_writable(self->uart)) { + MICROPY_EVENT_POLL_HOOK + } + uart_get_hw(self->uart)->dr = *src++; + } + return size; +} + +STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { + machine_uart_obj_t *self = self_in; + mp_uint_t ret; + if (request == MP_STREAM_POLL) { + uintptr_t flags = arg; + ret = 0; + if ((flags & MP_STREAM_POLL_RD) && uart_is_readable(self->uart)) { + ret |= MP_STREAM_POLL_RD; + } + if ((flags & MP_STREAM_POLL_WR) && uart_is_writable(self->uart)) { + ret |= MP_STREAM_POLL_WR; + } + } else { + *errcode = MP_EINVAL; + ret = MP_STREAM_ERROR; + } + return ret; +} + +STATIC const mp_stream_p_t uart_stream_p = { + .read = machine_uart_read, + .write = machine_uart_write, + .ioctl = machine_uart_ioctl, + .is_text = false, +}; + +const mp_obj_type_t machine_uart_type = { + { &mp_type_type }, + .name = MP_QSTR_UART, + .print = machine_uart_print, + .make_new = machine_uart_make_new, + .getiter = mp_identity_getiter, + .iternext = mp_stream_unbuffered_iter, + .protocol = &uart_stream_p, + .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, +}; diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c new file mode 100644 index 0000000000000..38e0597018d64 --- /dev/null +++ b/ports/rp2/machine_wdt.c @@ -0,0 +1,78 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "modmachine.h" + +#include "hardware/watchdog.h" + +typedef struct _machine_wdt_obj_t { + mp_obj_base_t base; +} machine_wdt_obj_t; + +STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; + +STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Verify the WDT id. + mp_int_t id = args[ARG_id].u_int; + if (id != 0) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); + } + + // Start the watchdog (timeout is in milliseconds). + watchdog_enable(args[ARG_timeout].u_int, false); + + return MP_OBJ_FROM_PTR(&machine_wdt); +} + +STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { + (void)self_in; + watchdog_update(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); + +STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); + +const mp_obj_type_t machine_wdt_type = { + { &mp_type_type }, + .name = MP_QSTR_WDT, + .make_new = machine_wdt_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_wdt_locals_dict, +}; diff --git a/ports/rp2/main.c b/ports/rp2/main.c new file mode 100644 index 0000000000000..3e956ae78c4ad --- /dev/null +++ b/ports/rp2/main.c @@ -0,0 +1,208 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/compile.h" +#include "py/runtime.h" +#include "py/gc.h" +#include "py/mperrno.h" +#include "py/stackctrl.h" +#include "lib/mp-readline/readline.h" +#include "lib/utils/gchelper.h" +#include "lib/utils/pyexec.h" +#include "tusb.h" +#include "uart.h" +#include "modmachine.h" +#include "modrp2.h" +#include "genhdr/mpversion.h" + +#include "pico/stdlib.h" +#include "pico/binary_info.h" +#include "hardware/rtc.h" +#include "hardware/structs/rosc.h" + +extern uint8_t __StackTop, __StackBottom; +static char gc_heap[192 * 1024]; + +// Embed version info in the binary in machine readable form +bi_decl(bi_program_version_string(MICROPY_GIT_TAG)); + +// Add a section to the picotool output similar to program features, but for frozen modules +// (it will aggregate BINARY_INFO_ID_MP_FROZEN binary info) +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)); + +int main(int argc, char **argv) { + #if MICROPY_HW_ENABLE_UART_REPL + bi_decl(bi_program_feature("UART REPL")) + setup_default_uart(); + mp_uart_init(); + #endif + + #if MICROPY_HW_ENABLE_USBDEV + bi_decl(bi_program_feature("USB REPL")) + tusb_init(); + #endif + + #if MICROPY_PY_THREAD + bi_decl(bi_program_feature("thread support")) + mp_thread_init(); + #endif + + // Start and initialise the RTC + datetime_t t = { + .year = 2021, + .month = 1, + .day = 1, + .dotw = 5, // 0 is Sunday, so 5 is Friday + .hour = 0, + .min = 0, + .sec = 0, + }; + rtc_init(); + rtc_set_datetime(&t); + + // Initialise stack extents and GC heap. + mp_stack_set_top(&__StackTop); + mp_stack_set_limit(&__StackTop - &__StackBottom - 256); + gc_init(&gc_heap[0], &gc_heap[MP_ARRAY_SIZE(gc_heap)]); + + for (;;) { + + // Initialise MicroPython runtime. + mp_init(); + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); + + // Initialise sub-systems. + readline_init0(); + machine_pin_init(); + rp2_pio_init(); + + // Execute _boot.py to set up the filesystem. + pyexec_frozen_module("_boot.py"); + + // Execute user scripts. + pyexec_file_if_exists("boot.py"); + if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { + pyexec_file_if_exists("main.py"); + } + + for (;;) { + if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { + if (pyexec_raw_repl() != 0) { + break; + } + } else { + if (pyexec_friendly_repl() != 0) { + break; + } + } + } + + mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); + rp2_pio_deinit(); + machine_pin_deinit(); + gc_sweep_all(); + mp_deinit(); + } + + return 0; +} + +void gc_collect(void) { + gc_collect_start(); + gc_helper_collect_regs_and_stack(); + #if MICROPY_PY_THREAD + mp_thread_gc_others(); + #endif + gc_collect_end(); +} + +void nlr_jump_fail(void *val) { + printf("FATAL: uncaught exception %p\n", val); + mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(val)); + for (;;) { + __breakpoint(); + } +} + +#ifndef NDEBUG +void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); + panic("Assertion failed"); +} +#endif + +uint32_t rosc_random_u32(void) { + uint32_t value = 0; + for (size_t i = 0; i < 32; ++i) { + value = value << 1 | rosc_hw->randombit; + } + return value; +} + +const char rp2_help_text[] = + "Welcome to MicroPython!\n" + "\n" + "For online help please visit https://micropython.org/help/.\n" + "\n" + "For access to the hardware use the 'machine' module. RP2 specific commands\n" + "are in the 'rp2' module.\n" + "\n" + "Quick overview of some objects:\n" + " machine.Pin(pin) -- get a pin, eg machine.Pin(0)\n" + " machine.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p\n" + " methods: init(..), value([v]), high(), low(), irq(handler)\n" + " machine.ADC(pin) -- make an analog object from a pin\n" + " methods: read_u16()\n" + " machine.PWM(pin) -- make a PWM object from a pin\n" + " methods: deinit(), freq([f]), duty_u16([d]), duty_ns([d])\n" + " machine.I2C(id) -- create an I2C object (id=0,1)\n" + " methods: readfrom(addr, buf, stop=True), writeto(addr, buf, stop=True)\n" + " readfrom_mem(addr, memaddr, arg), writeto_mem(addr, memaddr, arg)\n" + " machine.SPI(id, baudrate=1000000) -- create an SPI object (id=0,1)\n" + " methods: read(nbytes, write=0x00), write(buf), write_readinto(wr_buf, rd_buf)\n" + " machine.Timer(freq, callback) -- create a software timer object\n" + " eg: machine.Timer(freq=1, callback=lambda t:print(t))\n" + "\n" + "Pins are numbered 0-29, and 26-29 have ADC capabilities\n" + "Pin IO modes are: Pin.IN, Pin.OUT, Pin.ALT\n" + "Pin pull modes are: Pin.PULL_UP, Pin.PULL_DOWN\n" + "\n" + "Useful control commands:\n" + " CTRL-C -- interrupt a running program\n" + " CTRL-D -- on a blank line, do a soft reset of the board\n" + " CTRL-E -- on a blank line, enter paste mode\n" + "\n" + "For further help on a specific object, type help(obj)\n" + "For a list of available modules, type help('modules')\n" +; + diff --git a/ports/rp2/manifest.py b/ports/rp2/manifest.py new file mode 100644 index 0000000000000..a56d4b1b0923b --- /dev/null +++ b/ports/rp2/manifest.py @@ -0,0 +1,3 @@ +freeze("modules") +freeze("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/extmod/uasyncio/manifest.py") diff --git a/ports/rp2/memmap_mp.ld b/ports/rp2/memmap_mp.ld new file mode 100644 index 0000000000000..5cebd33466a49 --- /dev/null +++ b/ports/rp2/memmap_mp.ld @@ -0,0 +1,252 @@ +/* Based on GCC ARM embedded samples. + Defines the following symbols for use by code: + __exidx_start + __exidx_end + __etext + __data_start__ + __preinit_array_start + __preinit_array_end + __init_array_start + __init_array_end + __fini_array_start + __fini_array_end + __data_end__ + __bss_start__ + __bss_end__ + __end__ + end + __HeapLimit + __StackLimit + __StackTop + __stack (== StackTop) +*/ + +MEMORY +{ + FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k + RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k + SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k + SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k +} + +ENTRY(_entry_point) + +SECTIONS +{ + /* Second stage bootloader is prepended to the image. It must be 256 bytes big + and checksummed. It is usually built by the boot_stage2 target + in the Pico SDK + */ + + .flash_begin : { + __flash_binary_start = .; + } > FLASH + + .boot2 : { + __boot2_start__ = .; + KEEP (*(.boot2)) + __boot2_end__ = .; + } > FLASH + + ASSERT(__boot2_end__ - __boot2_start__ == 256, + "ERROR: Pico second stage bootloader must be 256 bytes in size") + + /* The second stage will always enter the image at the start of .text. + The debugger will use the ELF entry point, which is the _entry_point + symbol if present, otherwise defaults to start of .text. + This can be used to transfer control back to the bootrom on debugger + launches only, to perform proper flash setup. + */ + + .text : { + __reset_start = .; + KEEP (*(.reset)) + . = ALIGN(256); + __reset_end = .; + ASSERT(__reset_end - __reset_start == 256, "ERROR: reset section should only be 256 bytes"); + KEEP (*(.vectors)) + /* TODO revisit this now memset/memcpy/float in ROM */ + /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from + * FLASH ... we will include any thing excluded here in .data below by default */ + *(.init) + /* Change for MicroPython... excluse gc.c, parse.c, vm.c from flash */ + *(EXCLUDE_FILE(*libgcc.a: *libc.a: *lib_a-mem*.o *libm.a: *gc.c.obj *vm.c.obj *parse.c.obj) .text*) + *(.fini) + /* Pull all c'tors into .text */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + /* Followed by destructors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(.eh_frame*) + . = ALIGN(4); + } > FLASH + + .rodata : { + *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*) + . = ALIGN(4); + *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*))) + . = ALIGN(4); + } > FLASH + + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } > FLASH + + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } > FLASH + __exidx_end = .; + + /* Machine inspectable binary information */ + . = ALIGN(4); + __binary_info_start = .; + .binary_info : + { + KEEP(*(.binary_info.keep.*)) + *(.binary_info.*) + } > FLASH + __binary_info_end = .; + . = ALIGN(4); + + /* End of .text-like segments */ + __etext = .; + + .ram_vector_table (COPY): { + *(.ram_vector_table) + } > RAM + + .data : { + __data_start__ = .; + *(vtable) + + *(.time_critical*) + + /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */ + *(.text*) + . = ALIGN(4); + *(.rodata*) + . = ALIGN(4); + + *(.data*) + + . = ALIGN(4); + *(.after_data.*) + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__mutex_array_start = .); + KEEP(*(SORT(.mutex_array.*))) + KEEP(*(.mutex_array)) + PROVIDE_HIDDEN (__mutex_array_end = .); + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(SORT(.preinit_array.*))) + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + *(SORT(.fini_array.*)) + *(.fini_array) + PROVIDE_HIDDEN (__fini_array_end = .); + + *(.jcr) + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + } > RAM AT> FLASH + + .uninitialized_data (COPY): { + . = ALIGN(4); + *(.uninitialized_data*) + } > RAM + + /* Start and end symbols must be word-aligned */ + .scratch_x : { + __scratch_x_start__ = .; + *(.scratch_x.*) + . = ALIGN(4); + __scratch_x_end__ = .; + } > SCRATCH_X AT > FLASH + __scratch_x_source__ = LOADADDR(.scratch_x); + + .scratch_y : { + __scratch_y_start__ = .; + *(.scratch_y.*) + . = ALIGN(4); + __scratch_y_end__ = .; + } > SCRATCH_Y AT > FLASH + __scratch_y_source__ = LOADADDR(.scratch_y); + + .bss : { + . = ALIGN(4); + __bss_start__ = .; + *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*))) + *(COMMON) + . = ALIGN(4); + __bss_end__ = .; + } > RAM + + .heap (COPY): + { + __end__ = .; + end = __end__; + *(.heap*) + __HeapLimit = .; + } > RAM + + /* .stack*_dummy section doesn't contains any symbols. It is only + * used for linker to calculate size of stack sections, and assign + * values to stack symbols later + * + * stack1 section may be empty/missing if platform_launch_core1 is not used */ + + /* by default we put core 0 stack at the end of scratch Y, so that if core 1 + * stack is not used then all of SCRATCH_X is free. + */ + .stack1_dummy (COPY): + { + *(.stack1*) + } > SCRATCH_X + .stack_dummy (COPY): + { + *(.stack*) + } > SCRATCH_Y + + .flash_end : { + __flash_binary_end = .; + } > FLASH + + /* stack limit is poorly named, but historically is maximum heap ptr */ + __StackLimit = ORIGIN(RAM) + LENGTH(RAM); + __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X); + __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y); + __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy); + __StackBottom = __StackTop - SIZEOF(.stack_dummy); + PROVIDE(__stack = __StackTop); + + /* Check if data + heap + stack exceeds RAM limit */ + ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed") + /* todo assert on extra code */ +} + diff --git a/ports/rp2/micropy_extmod.cmake b/ports/rp2/micropy_extmod.cmake new file mode 100644 index 0000000000000..1e968bef637fd --- /dev/null +++ b/ports/rp2/micropy_extmod.cmake @@ -0,0 +1,40 @@ +# CMake fragment for MicroPython extmod component + +set(SOURCE_EXTMOD + ${MPY_DIR}/extmod/machine_i2c.c + ${MPY_DIR}/extmod/machine_mem.c + ${MPY_DIR}/extmod/machine_pulse.c + ${MPY_DIR}/extmod/machine_signal.c + ${MPY_DIR}/extmod/machine_spi.c + ${MPY_DIR}/extmod/modbtree.c + ${MPY_DIR}/extmod/modframebuf.c + ${MPY_DIR}/extmod/modonewire.c + ${MPY_DIR}/extmod/moduasyncio.c + ${MPY_DIR}/extmod/modubinascii.c + ${MPY_DIR}/extmod/moducryptolib.c + ${MPY_DIR}/extmod/moductypes.c + ${MPY_DIR}/extmod/moduhashlib.c + ${MPY_DIR}/extmod/moduheapq.c + ${MPY_DIR}/extmod/modujson.c + ${MPY_DIR}/extmod/modurandom.c + ${MPY_DIR}/extmod/modure.c + ${MPY_DIR}/extmod/moduselect.c + ${MPY_DIR}/extmod/modussl_axtls.c + ${MPY_DIR}/extmod/modussl_mbedtls.c + ${MPY_DIR}/extmod/modutimeq.c + ${MPY_DIR}/extmod/moduwebsocket.c + ${MPY_DIR}/extmod/moduzlib.c + ${MPY_DIR}/extmod/modwebrepl.c + ${MPY_DIR}/extmod/uos_dupterm.c + ${MPY_DIR}/extmod/utime_mphal.c + ${MPY_DIR}/extmod/vfs.c + ${MPY_DIR}/extmod/vfs_blockdev.c + ${MPY_DIR}/extmod/vfs_fat.c + ${MPY_DIR}/extmod/vfs_fat_diskio.c + ${MPY_DIR}/extmod/vfs_fat_file.c + ${MPY_DIR}/extmod/vfs_lfs.c + ${MPY_DIR}/extmod/vfs_posix.c + ${MPY_DIR}/extmod/vfs_posix_file.c + ${MPY_DIR}/extmod/vfs_reader.c + ${MPY_DIR}/extmod/virtpin.c +) diff --git a/ports/rp2/micropy_py.cmake b/ports/rp2/micropy_py.cmake new file mode 100644 index 0000000000000..aeb7e2b9b5aa7 --- /dev/null +++ b/ports/rp2/micropy_py.cmake @@ -0,0 +1,134 @@ +# CMake fragment for MicroPython core py component + +set(MPY_PY_DIR "${MPY_DIR}/py") +set(MPY_PY_QSTRDEFS "${MPY_PY_DIR}/qstrdefs.h") +set(MPY_GENHDR_DIR "${CMAKE_BINARY_DIR}/genhdr") +set(MPY_MPVERSION "${MPY_GENHDR_DIR}/mpversion.h") +set(MPY_MODULEDEFS "${MPY_GENHDR_DIR}/moduledefs.h") +set(MPY_QSTR_DEFS_LAST "${MPY_GENHDR_DIR}/qstr.i.last") +set(MPY_QSTR_DEFS_SPLIT "${MPY_GENHDR_DIR}/qstr.split") +set(MPY_QSTR_DEFS_COLLECTED "${MPY_GENHDR_DIR}/qstrdefs.collected.h") +set(MPY_QSTR_DEFS_PREPROCESSED "${MPY_GENHDR_DIR}/qstrdefs.preprocessed.h") +set(MPY_QSTR_DEFS_GENERATED "${MPY_GENHDR_DIR}/qstrdefs.generated.h") +set(MPY_FROZEN_CONTENT "${CMAKE_BINARY_DIR}/frozen_content.c") + +# All py/ source files +set(SOURCE_PY + ${MPY_PY_DIR}/argcheck.c + ${MPY_PY_DIR}/asmarm.c + ${MPY_PY_DIR}/asmbase.c + ${MPY_PY_DIR}/asmthumb.c + ${MPY_PY_DIR}/asmx64.c + ${MPY_PY_DIR}/asmx86.c + ${MPY_PY_DIR}/asmxtensa.c + ${MPY_PY_DIR}/bc.c + ${MPY_PY_DIR}/binary.c + ${MPY_PY_DIR}/builtinevex.c + ${MPY_PY_DIR}/builtinhelp.c + ${MPY_PY_DIR}/builtinimport.c + ${MPY_PY_DIR}/compile.c + ${MPY_PY_DIR}/emitbc.c + ${MPY_PY_DIR}/emitcommon.c + ${MPY_PY_DIR}/emitglue.c + ${MPY_PY_DIR}/emitinlinethumb.c + ${MPY_PY_DIR}/emitinlinextensa.c + ${MPY_PY_DIR}/emitnarm.c + ${MPY_PY_DIR}/emitnthumb.c + ${MPY_PY_DIR}/emitnx64.c + ${MPY_PY_DIR}/emitnx86.c + ${MPY_PY_DIR}/emitnxtensa.c + ${MPY_PY_DIR}/emitnxtensawin.c + ${MPY_PY_DIR}/formatfloat.c + ${MPY_PY_DIR}/frozenmod.c + ${MPY_PY_DIR}/gc.c + ${MPY_PY_DIR}/lexer.c + ${MPY_PY_DIR}/malloc.c + ${MPY_PY_DIR}/map.c + ${MPY_PY_DIR}/modarray.c + ${MPY_PY_DIR}/modbuiltins.c + ${MPY_PY_DIR}/modcmath.c + ${MPY_PY_DIR}/modcollections.c + ${MPY_PY_DIR}/modgc.c + ${MPY_PY_DIR}/modio.c + ${MPY_PY_DIR}/modmath.c + ${MPY_PY_DIR}/modmicropython.c + ${MPY_PY_DIR}/modstruct.c + ${MPY_PY_DIR}/modsys.c + ${MPY_PY_DIR}/modthread.c + ${MPY_PY_DIR}/moduerrno.c + ${MPY_PY_DIR}/mpprint.c + ${MPY_PY_DIR}/mpstate.c + ${MPY_PY_DIR}/mpz.c + ${MPY_PY_DIR}/nativeglue.c + ${MPY_PY_DIR}/nlr.c + ${MPY_PY_DIR}/nlrpowerpc.c + ${MPY_PY_DIR}/nlrsetjmp.c + ${MPY_PY_DIR}/nlrthumb.c + ${MPY_PY_DIR}/nlrx64.c + ${MPY_PY_DIR}/nlrx86.c + ${MPY_PY_DIR}/nlrxtensa.c + ${MPY_PY_DIR}/obj.c + ${MPY_PY_DIR}/objarray.c + ${MPY_PY_DIR}/objattrtuple.c + ${MPY_PY_DIR}/objbool.c + ${MPY_PY_DIR}/objboundmeth.c + ${MPY_PY_DIR}/objcell.c + ${MPY_PY_DIR}/objclosure.c + ${MPY_PY_DIR}/objcomplex.c + ${MPY_PY_DIR}/objdeque.c + ${MPY_PY_DIR}/objdict.c + ${MPY_PY_DIR}/objenumerate.c + ${MPY_PY_DIR}/objexcept.c + ${MPY_PY_DIR}/objfilter.c + ${MPY_PY_DIR}/objfloat.c + ${MPY_PY_DIR}/objfun.c + ${MPY_PY_DIR}/objgenerator.c + ${MPY_PY_DIR}/objgetitemiter.c + ${MPY_PY_DIR}/objint.c + ${MPY_PY_DIR}/objint_longlong.c + ${MPY_PY_DIR}/objint_mpz.c + ${MPY_PY_DIR}/objlist.c + ${MPY_PY_DIR}/objmap.c + ${MPY_PY_DIR}/objmodule.c + ${MPY_PY_DIR}/objnamedtuple.c + ${MPY_PY_DIR}/objnone.c + ${MPY_PY_DIR}/objobject.c + ${MPY_PY_DIR}/objpolyiter.c + ${MPY_PY_DIR}/objproperty.c + ${MPY_PY_DIR}/objrange.c + ${MPY_PY_DIR}/objreversed.c + ${MPY_PY_DIR}/objset.c + ${MPY_PY_DIR}/objsingleton.c + ${MPY_PY_DIR}/objslice.c + ${MPY_PY_DIR}/objstr.c + ${MPY_PY_DIR}/objstringio.c + ${MPY_PY_DIR}/objstrunicode.c + ${MPY_PY_DIR}/objtuple.c + ${MPY_PY_DIR}/objtype.c + ${MPY_PY_DIR}/objzip.c + ${MPY_PY_DIR}/opmethods.c + ${MPY_PY_DIR}/pairheap.c + ${MPY_PY_DIR}/parse.c + ${MPY_PY_DIR}/parsenum.c + ${MPY_PY_DIR}/parsenumbase.c + ${MPY_PY_DIR}/persistentcode.c + ${MPY_PY_DIR}/profile.c + ${MPY_PY_DIR}/pystack.c + ${MPY_PY_DIR}/qstr.c + ${MPY_PY_DIR}/reader.c + ${MPY_PY_DIR}/repl.c + ${MPY_PY_DIR}/ringbuf.c + ${MPY_PY_DIR}/runtime.c + ${MPY_PY_DIR}/runtime_utils.c + ${MPY_PY_DIR}/scheduler.c + ${MPY_PY_DIR}/scope.c + ${MPY_PY_DIR}/sequence.c + ${MPY_PY_DIR}/showbc.c + ${MPY_PY_DIR}/smallint.c + ${MPY_PY_DIR}/stackctrl.c + ${MPY_PY_DIR}/stream.c + ${MPY_PY_DIR}/unicode.c + ${MPY_PY_DIR}/vm.c + ${MPY_PY_DIR}/vstr.c + ${MPY_PY_DIR}/warning.c +) diff --git a/ports/rp2/micropy_rules.cmake b/ports/rp2/micropy_rules.cmake new file mode 100644 index 0000000000000..ade9b8c92d5cc --- /dev/null +++ b/ports/rp2/micropy_rules.cmake @@ -0,0 +1,91 @@ +# CMake fragment for MicroPython rules + +target_sources(${MICROPYTHON_TARGET} PRIVATE + ${MPY_MPVERSION} + ${MPY_QSTR_DEFS_GENERATED} + ${MPY_FROZEN_CONTENT} +) + +# Command to force the build of another command + +add_custom_command( + OUTPUT FORCE_BUILD + COMMENT "" + COMMAND echo -n +) + +# Generate mpversion.h + +add_custom_command( + OUTPUT ${MPY_MPVERSION} + COMMAND ${CMAKE_COMMAND} -E make_directory ${MPY_GENHDR_DIR} + COMMAND python3 ${MPY_DIR}/py/makeversionhdr.py ${MPY_MPVERSION} + DEPENDS FORCE_BUILD +) + +# Generate moduledefs.h +# This is currently hard-coded to support modarray.c only, because makemoduledefs.py doesn't support absolute paths + +add_custom_command( + OUTPUT ${MPY_MODULEDEFS} + COMMAND python3 ${MPY_PY_DIR}/makemoduledefs.py --vpath="." ../../../py/modarray.c > ${MPY_MODULEDEFS} + DEPENDS ${MPY_MPVERSION} + ${SOURCE_QSTR} +) + +# Generate qstrs + +# If any of the dependencies in this rule change then the C-preprocessor step must be run. +# It only needs to be passed the list of SOURCE_QSTR files that have changed since it was +# last run, but it looks like it's not possible to specify that with cmake. +add_custom_command( + OUTPUT ${MPY_QSTR_DEFS_LAST} + COMMAND ${CMAKE_C_COMPILER} -E \$\(C_INCLUDES\) \$\(C_FLAGS\) -DNO_QSTR ${SOURCE_QSTR} > ${MPY_GENHDR_DIR}/qstr.i.last + DEPENDS ${MPY_MODULEDEFS} + ${SOURCE_QSTR} + VERBATIM +) + +add_custom_command( + OUTPUT ${MPY_QSTR_DEFS_SPLIT} + COMMAND python3 ${MPY_DIR}/py/makeqstrdefs.py split qstr ${MPY_GENHDR_DIR}/qstr.i.last ${MPY_GENHDR_DIR}/qstr _ + COMMAND touch ${MPY_QSTR_DEFS_SPLIT} + DEPENDS ${MPY_QSTR_DEFS_LAST} + VERBATIM +) + +add_custom_command( + OUTPUT ${MPY_QSTR_DEFS_COLLECTED} + COMMAND python3 ${MPY_DIR}/py/makeqstrdefs.py cat qstr _ ${MPY_GENHDR_DIR}/qstr ${MPY_QSTR_DEFS_COLLECTED} + DEPENDS ${MPY_QSTR_DEFS_SPLIT} + VERBATIM +) + +add_custom_command( + OUTPUT ${MPY_QSTR_DEFS_PREPROCESSED} + COMMAND cat ${MPY_PY_QSTRDEFS} ${MPY_QSTR_DEFS} ${MPY_QSTR_DEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E \$\(C_INCLUDES\) \$\(C_FLAGS\) - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MPY_QSTR_DEFS_PREPROCESSED} + DEPENDS ${MPY_PY_QSTRDEFS} ${MPY_QSTR_DEFS} ${MPY_QSTR_DEFS_COLLECTED} + VERBATIM +) + +add_custom_command( + OUTPUT ${MPY_QSTR_DEFS_GENERATED} + COMMAND python3 ${MPY_PY_DIR}/makeqstrdata.py ${MPY_QSTR_DEFS_PREPROCESSED} > ${MPY_QSTR_DEFS_GENERATED} + DEPENDS ${MPY_QSTR_DEFS_PREPROCESSED} + VERBATIM +) + +# Build frozen code + +target_compile_options(${MICROPYTHON_TARGET} PUBLIC + -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool + -DMICROPY_MODULE_FROZEN_MPY=\(1\) +) + +add_custom_command( + OUTPUT ${MPY_FROZEN_CONTENT} + COMMAND python3 ${MPY_DIR}/tools/makemanifest.py -o ${MPY_FROZEN_CONTENT} -v "MPY_DIR=${MPY_DIR}" -v "PORT_DIR=${PROJECT_SOURCE_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MPY_CROSS_FLAGS} ${FROZEN_MANIFEST} + DEPENDS FORCE_BUILD + ${MPY_QSTR_DEFS_GENERATED} + VERBATIM +) diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c new file mode 100644 index 0000000000000..228e9543c1936 --- /dev/null +++ b/ports/rp2/modmachine.c @@ -0,0 +1,101 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "extmod/machine_i2c.h" +#include "extmod/machine_mem.h" +#include "extmod/machine_spi.h" + +#include "modmachine.h" +#include "hardware/clocks.h" +#include "hardware/watchdog.h" +#include "pico/bootrom.h" + +#define RP2_RESET_PWRON (1) +#define RP2_RESET_WDT (3) + +STATIC mp_obj_t machine_reset(void) { + watchdog_reboot(0, SRAM_END, 0); + for (;;) { + __wfi(); + } + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); + +STATIC mp_obj_t machine_reset_cause(void) { + int reset_cause; + if (watchdog_caused_reboot()) { + reset_cause = RP2_RESET_WDT; + } else { + reset_cause = RP2_RESET_PWRON; + } + return MP_OBJ_NEW_SMALL_INT(reset_cause); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); + +STATIC mp_obj_t machine_bootloader(void) { + reset_usb_boot(0, 0); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader); + +STATIC mp_obj_t machine_freq(void) { + return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys)); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); + +STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, + { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, + { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + + { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(RP2_RESET_PWRON) }, + { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(RP2_RESET_WDT) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); + +const mp_obj_module_t mp_module_machine = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&machine_module_globals, +}; diff --git a/ports/rp2/modmachine.h b/ports/rp2/modmachine.h new file mode 100644 index 0000000000000..d09c83aee2c9e --- /dev/null +++ b/ports/rp2/modmachine.h @@ -0,0 +1,18 @@ +#ifndef MICROPY_INCLUDED_RP2_MODMACHINE_H +#define MICROPY_INCLUDED_RP2_MODMACHINE_H + +#include "py/obj.h" + +extern const mp_obj_type_t machine_adc_type; +extern const mp_obj_type_t machine_hw_i2c_type; +extern const mp_obj_type_t machine_pin_type; +extern const mp_obj_type_t machine_pwm_type; +extern const mp_obj_type_t machine_spi_type; +extern const mp_obj_type_t machine_timer_type; +extern const mp_obj_type_t machine_uart_type; +extern const mp_obj_type_t machine_wdt_type; + +void machine_pin_init(void); +void machine_pin_deinit(void); + +#endif // MICROPY_INCLUDED_RP2_MODMACHINE_H diff --git a/ports/rp2/modrp2.c b/ports/rp2/modrp2.c new file mode 100644 index 0000000000000..8009fa33f8749 --- /dev/null +++ b/ports/rp2/modrp2.c @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "modrp2.h" + +STATIC const mp_rom_map_elem_t rp2_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rp2) }, + { MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&rp2_flash_type) }, + { MP_ROM_QSTR(MP_QSTR_PIO), MP_ROM_PTR(&rp2_pio_type) }, + { MP_ROM_QSTR(MP_QSTR_StateMachine), MP_ROM_PTR(&rp2_state_machine_type) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2_module_globals, rp2_module_globals_table); + +const mp_obj_module_t mp_module_rp2 = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&rp2_module_globals, +}; diff --git a/ports/rp2/modrp2.h b/ports/rp2/modrp2.h new file mode 100644 index 0000000000000..805c785f2d8cd --- /dev/null +++ b/ports/rp2/modrp2.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_RP2_MODRP2_H +#define MICROPY_INCLUDED_RP2_MODRP2_H + +#include "py/obj.h" + +extern const mp_obj_type_t rp2_flash_type; +extern const mp_obj_type_t rp2_pio_type; +extern const mp_obj_type_t rp2_state_machine_type; + +void rp2_pio_init(void); +void rp2_pio_deinit(void); + +#endif // MICROPY_INCLUDED_RP2_MODRP2_H diff --git a/ports/rp2/modules/_boot.py b/ports/rp2/modules/_boot.py new file mode 100644 index 0000000000000..099e5aba05907 --- /dev/null +++ b/ports/rp2/modules/_boot.py @@ -0,0 +1,15 @@ +import os +import machine, rp2 + + +# Try to mount the filesystem, and format the flash if it doesn't exist. +# Note: the flash requires the programming size to be aligned to 256 bytes. +bdev = rp2.Flash() +try: + vfs = os.VfsLfs2(bdev, progsize=256) +except: + os.VfsLfs2.mkfs(bdev, progsize=256) + vfs = os.VfsLfs2(bdev, progsize=256) +os.mount(vfs, "/") + +del os, bdev, vfs diff --git a/ports/rp2/modules/rp2.py b/ports/rp2/modules/rp2.py new file mode 100644 index 0000000000000..a3adcdc51274d --- /dev/null +++ b/ports/rp2/modules/rp2.py @@ -0,0 +1,294 @@ +# rp2 module: uses C code from _rp2, plus asm_pio decorator implemented in Python. +# MIT license; Copyright (c) 2020-2021 Damien P. George + +from _rp2 import * +from micropython import const + +_PROG_DATA = const(0) +_PROG_OFFSET_PIO0 = const(1) +_PROG_OFFSET_PIO1 = const(2) +_PROG_EXECCTRL = const(3) +_PROG_SHIFTCTRL = const(4) +_PROG_OUT_PINS = const(5) +_PROG_SET_PINS = const(6) +_PROG_SIDESET_PINS = const(7) +_PROG_MAX_FIELDS = const(8) + + +class PIOASMError(Exception): + pass + + +class PIOASMEmit: + def __init__( + self, + *, + out_init=None, + set_init=None, + sideset_init=None, + in_shiftdir=0, + out_shiftdir=0, + autopush=False, + autopull=False, + push_thresh=32, + pull_thresh=32 + ): + from array import array + + self.labels = {} + execctrl = 0 + shiftctrl = ( + (pull_thresh & 0x1F) << 25 + | (push_thresh & 0x1F) << 20 + | out_shiftdir << 19 + | in_shiftdir << 18 + | autopull << 17 + | autopush << 16 + ) + self.prog = [array("H"), -1, -1, execctrl, shiftctrl, out_init, set_init, sideset_init] + + self.wrap_used = False + + if sideset_init is None: + self.sideset_count = 0 + elif isinstance(sideset_init, int): + self.sideset_count = 1 + else: + self.sideset_count = len(sideset_init) + + def start_pass(self, pass_): + if pass_ == 1: + if not self.wrap_used and self.num_instr: + self.wrap() + self.delay_max = 31 + if self.sideset_count: + self.sideset_opt = self.num_sideset != self.num_instr + if self.sideset_opt: + self.prog[_PROG_EXECCTRL] |= 1 << 30 + self.sideset_count += 1 + self.delay_max >>= self.sideset_count + self.pass_ = pass_ + self.num_instr = 0 + self.num_sideset = 0 + + def __getitem__(self, key): + return self.delay(key) + + def delay(self, delay): + if self.pass_ > 0: + if delay > self.delay_max: + raise PIOASMError("delay too large") + self.prog[_PROG_DATA][-1] |= delay << 8 + return self + + def side(self, value): + self.num_sideset += 1 + if self.pass_ > 0: + set_bit = 13 - self.sideset_count + self.prog[_PROG_DATA][-1] |= self.sideset_opt << 12 | value << set_bit + return self + + def wrap_target(self): + self.prog[_PROG_EXECCTRL] |= self.num_instr << 7 + + def wrap(self): + assert self.num_instr + self.prog[_PROG_EXECCTRL] |= (self.num_instr - 1) << 12 + self.wrap_used = True + + def label(self, label): + if self.pass_ == 0: + if label in self.labels: + raise PIOASMError("duplicate label {}".format(label)) + self.labels[label] = self.num_instr + + def word(self, instr, label=None): + self.num_instr += 1 + if self.pass_ > 0: + if label is None: + label = 0 + else: + if not label in self.labels: + raise PIOASMError("unknown label {}".format(label)) + label = self.labels[label] + self.prog[_PROG_DATA].append(instr | label) + return self + + def nop(self): + return self.word(0xA042) + + def jmp(self, cond, label=None): + if label is None: + label = cond + cond = 0 # always + return self.word(0x0000 | cond << 5, label) + + def wait(self, polarity, src, index): + if src == 6: + src = 1 # "pin" + elif src != 0: + src = 2 # "irq" + return self.word(0x2000 | polarity << 7 | src << 5 | index) + + def in_(self, src, data): + if not 0 < data <= 32: + raise PIOASMError("invalid bit count {}".format(data)) + return self.word(0x4000 | src << 5 | data & 0x1F) + + def out(self, dest, data): + if dest == 8: + dest = 7 # exec + if not 0 < data <= 32: + raise PIOASMError("invalid bit count {}".format(data)) + return self.word(0x6000 | dest << 5 | data & 0x1F) + + def push(self, value=0, value2=0): + value |= value2 + if not value & 1: + value |= 0x20 # block by default + return self.word(0x8000 | (value & 0x60)) + + def pull(self, value=0, value2=0): + value |= value2 + if not value & 1: + value |= 0x20 # block by default + return self.word(0x8080 | (value & 0x60)) + + def mov(self, dest, src): + if dest == 8: + dest = 4 # exec + return self.word(0xA000 | dest << 5 | src) + + def irq(self, mod, index=None): + if index is None: + index = mod + mod = 0 # no modifiers + return self.word(0xC000 | (mod & 0x60) | index) + + def set(self, dest, data): + return self.word(0xE000 | dest << 5 | data) + + +_pio_funcs = { + # source constants for wait + "gpio": 0, + # "pin": see below, translated to 1 + # "irq": see below function, translated to 2 + # source/dest constants for in_, out, mov, set + "pins": 0, + "x": 1, + "y": 2, + "null": 3, + "pindirs": 4, + "pc": 5, + "status": 5, + "isr": 6, + "osr": 7, + "exec": 8, # translated to 4 for mov, 7 for out + # operation functions for mov's src + "invert": lambda x: x | 0x08, + "reverse": lambda x: x | 0x10, + # jmp condition constants + "not_x": 1, + "x_dec": 2, + "not_y": 3, + "y_dec": 4, + "x_not_y": 5, + "pin": 6, + "not_osre": 7, + # constants for push, pull + "noblock": 0x01, + "block": 0x21, + "iffull": 0x40, + "ifempty": 0x40, + # constants and modifiers for irq + # "noblock": see above + # "block": see above + "clear": 0x40, + "rel": lambda x: x | 0x10, + # functions + "wrap_target": None, + "wrap": None, + "label": None, + "word": None, + "nop": None, + "jmp": None, + "wait": None, + "in_": None, + "out": None, + "push": None, + "pull": None, + "mov": None, + "irq": None, + "set": None, +} + + +def asm_pio(**kw): + emit = PIOASMEmit(**kw) + + def dec(f): + nonlocal emit + + gl = _pio_funcs + gl["wrap_target"] = emit.wrap_target + gl["wrap"] = emit.wrap + gl["label"] = emit.label + gl["word"] = emit.word + gl["nop"] = emit.nop + gl["jmp"] = emit.jmp + gl["wait"] = emit.wait + gl["in_"] = emit.in_ + gl["out"] = emit.out + gl["push"] = emit.push + gl["pull"] = emit.pull + gl["mov"] = emit.mov + gl["irq"] = emit.irq + gl["set"] = emit.set + + old_gl = f.__globals__.copy() + f.__globals__.clear() + f.__globals__.update(gl) + + emit.start_pass(0) + f() + + emit.start_pass(1) + f() + + f.__globals__.clear() + f.__globals__.update(old_gl) + + return emit.prog + + return dec + + +# sideset_count is inclusive of enable bit +def asm_pio_encode(instr, sideset_count): + emit = PIOASMEmit() + emit.delay_max = 31 + emit.sideset_count = sideset_count + if emit.sideset_count: + emit.delay_max >>= emit.sideset_count + emit.pass_ = 1 + emit.num_instr = 0 + emit.num_sideset = 0 + + gl = _pio_funcs + gl["nop"] = emit.nop + # gl["jmp"] = emit.jmp currently not supported + gl["wait"] = emit.wait + gl["in_"] = emit.in_ + gl["out"] = emit.out + gl["push"] = emit.push + gl["pull"] = emit.pull + gl["mov"] = emit.mov + gl["irq"] = emit.irq + gl["set"] = emit.set + + exec(instr, gl) + + if len(emit.prog[_PROG_DATA]) != 1: + raise PIOASMError("expecting exactly 1 instruction") + return emit.prog[_PROG_DATA][0] diff --git a/ports/rp2/moduos.c b/ports/rp2/moduos.c new file mode 100644 index 0000000000000..7ee662b5a4518 --- /dev/null +++ b/ports/rp2/moduos.c @@ -0,0 +1,103 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/objstr.h" +#include "py/runtime.h" +#include "extmod/vfs.h" +#include "extmod/vfs_fat.h" +#include "extmod/vfs_lfs.h" +#include "genhdr/mpversion.h" + +STATIC const qstr os_uname_info_fields[] = { + MP_QSTR_sysname, MP_QSTR_nodename, + MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine +}; +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, MICROPY_PY_SYS_PLATFORM); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_PY_SYS_PLATFORM); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE " (" MICROPY_BUILD_TYPE ")"); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME); + +STATIC MP_DEFINE_ATTRTUPLE( + os_uname_info_obj, + os_uname_info_fields, + 5, + (mp_obj_t)&os_uname_info_sysname_obj, + (mp_obj_t)&os_uname_info_nodename_obj, + (mp_obj_t)&os_uname_info_release_obj, + (mp_obj_t)&os_uname_info_version_obj, + (mp_obj_t)&os_uname_info_machine_obj + ); + +STATIC mp_obj_t os_uname(void) { + return (mp_obj_t)&os_uname_info_obj; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); + +STATIC const mp_rom_map_elem_t os_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) }, + + { MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) }, + + #if MICROPY_VFS + { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) }, + { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) }, + { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) }, + { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) }, + { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) }, + #endif + + // The following are MicroPython extensions. + + #if MICROPY_PY_OS_DUPTERM + { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) }, + #endif + + #if MICROPY_VFS + { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mp_vfs_ilistdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) }, + { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) }, + #if MICROPY_VFS_FAT + { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) }, + #endif + #if MICROPY_VFS_LFS1 + { MP_ROM_QSTR(MP_QSTR_VfsLfs1), MP_ROM_PTR(&mp_type_vfs_lfs1) }, + #endif + #if MICROPY_VFS_LFS2 + { MP_ROM_QSTR(MP_QSTR_VfsLfs2), MP_ROM_PTR(&mp_type_vfs_lfs2) }, + #endif + #endif +}; +STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); + +const mp_obj_module_t mp_module_uos = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&os_module_globals, +}; diff --git a/ports/rp2/modutime.c b/ports/rp2/modutime.c new file mode 100644 index 0000000000000..4835a0ee11e63 --- /dev/null +++ b/ports/rp2/modutime.c @@ -0,0 +1,127 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "lib/timeutils/timeutils.h" +#include "extmod/utime_mphal.h" +#include "hardware/rtc.h" + +// localtime([secs]) +// Convert a time expressed in seconds since the Epoch into an 8-tuple which +// contains: (year, month, mday, hour, minute, second, weekday, yearday) +// If secs is not provided or None, then the current time from is used. +STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { + if (n_args == 0 || args[0] == mp_const_none) { + // Get current date and time. + datetime_t t; + rtc_get_datetime(&t); + mp_obj_t tuple[8] = { + mp_obj_new_int(t.year), + mp_obj_new_int(t.month), + mp_obj_new_int(t.day), + mp_obj_new_int(t.hour), + mp_obj_new_int(t.min), + mp_obj_new_int(t.sec), + mp_obj_new_int((t.dotw + 6) % 7), // convert 0=Sunday to 6=Sunday + mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)), + }; + return mp_obj_new_tuple(8, tuple); + } else { + // Convert given seconds to tuple. + mp_int_t seconds = mp_obj_get_int(args[0]); + timeutils_struct_time_t tm; + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); + mp_obj_t tuple[8] = { + tuple[0] = mp_obj_new_int(tm.tm_year), + tuple[1] = mp_obj_new_int(tm.tm_mon), + tuple[2] = mp_obj_new_int(tm.tm_mday), + tuple[3] = mp_obj_new_int(tm.tm_hour), + tuple[4] = mp_obj_new_int(tm.tm_min), + tuple[5] = mp_obj_new_int(tm.tm_sec), + tuple[6] = mp_obj_new_int(tm.tm_wday), + tuple[7] = mp_obj_new_int(tm.tm_yday), + }; + return mp_obj_new_tuple(8, tuple); + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime); + +// mktime() +// This is inverse function of localtime. It's argument is a full 8-tuple +// which expresses a time as per localtime. It returns an integer which is +// the number of seconds since the Epoch. +STATIC mp_obj_t time_mktime(mp_obj_t tuple) { + size_t len; + mp_obj_t *elem; + mp_obj_get_array(tuple, &len, &elem); + + // localtime generates a tuple of len 8. CPython uses 9, so we accept both. + if (len < 8 || len > 9) { + mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9")); + } + + return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), + mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]), + mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); +} +MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); + +// time() +// Return the number of seconds since the Epoch. +STATIC mp_obj_t time_time(void) { + datetime_t t; + rtc_get_datetime(&t); + return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); + +STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, + + { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, + + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table); + +const mp_obj_module_t mp_module_utime = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&mp_module_time_globals, +}; diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h new file mode 100644 index 0000000000000..d409ccfd4d926 --- /dev/null +++ b/ports/rp2/mpconfigport.h @@ -0,0 +1,196 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Options controlling how MicroPython is built, overriding defaults in py/mpconfig.h + +#include +#include "hardware/spi.h" +#include "hardware/sync.h" +#include "pico/binary_info.h" + +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" +#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) + +// Memory allocation policies +#define MICROPY_GC_STACK_ENTRY_TYPE uint16_t +#define MICROPY_ALLOC_PATH_MAX (128) +#define MICROPY_QSTR_BYTES_IN_HASH (1) + +// MicroPython emitters +#define MICROPY_PERSISTENT_CODE_LOAD (1) +#define MICROPY_EMIT_THUMB (1) +#define MICROPY_EMIT_THUMB_ARMV7M (0) +#define MICROPY_EMIT_INLINE_THUMB (1) +#define MICROPY_EMIT_INLINE_THUMB_FLOAT (0) +#define MICROPY_EMIT_INLINE_THUMB_ARMV7M (0) + +// Python internal features +#define MICROPY_READER_VFS (1) +#define MICROPY_ENABLE_GC (1) +#define MICROPY_ENABLE_FINALISER (1) +#define MICROPY_STACK_CHECK (1) +#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) +#define MICROPY_KBD_EXCEPTION (1) +#define MICROPY_HELPER_REPL (1) +#define MICROPY_REPL_AUTO_INDENT (1) +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +#define MICROPY_ENABLE_SOURCE_LINE (1) +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_MODULE_BUILTIN_INIT (1) +#define MICROPY_MODULE_WEAK_LINKS (1) +#define MICROPY_CAN_OVERRIDE_BUILTINS (1) +#define MICROPY_ENABLE_SCHEDULER (1) +#define MICROPY_SCHEDULER_DEPTH (8) + +// Fine control over Python builtins, classes, modules, etc +#define MICROPY_PY_FUNCTION_ATTRS (1) +#define MICROPY_PY_BUILTINS_STR_UNICODE (1) +#define MICROPY_PY_BUILTINS_MEMORYVIEW (1) +#define MICROPY_PY_BUILTINS_ROUND_INT (1) +#define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_BUILTINS_INPUT (1) +#define MICROPY_PY_BUILTINS_HELP (1) +#define MICROPY_PY_BUILTINS_HELP_TEXT rp2_help_text +#define MICROPY_PY_BUILTINS_HELP_MODULES (1) +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#define MICROPY_PY___FILE__ (0) +#define MICROPY_PY_MICROPYTHON_MEM_INFO (1) +#define MICROPY_PY_IO_IOBASE (1) +#define MICROPY_PY_IO_FILEIO (1) +#define MICROPY_PY_SYS_MAXSIZE (1) +#define MICROPY_PY_SYS_STDFILES (1) +#define MICROPY_PY_SYS_STDIO_BUFFER (1) +#define MICROPY_PY_SYS_PLATFORM "rp2" +#define MICROPY_PY_THREAD (1) +#define MICROPY_PY_THREAD_GIL (0) + +// Extended modules +#define MICROPY_EPOCH_IS_1970 (1) +#define MICROPY_PY_UASYNCIO (1) +#define MICROPY_PY_UCTYPES (1) +#define MICROPY_PY_UZLIB (1) +#define MICROPY_PY_UJSON (1) +#define MICROPY_PY_URE (1) +#define MICROPY_PY_URE_MATCH_GROUPS (1) +#define MICROPY_PY_URE_MATCH_SPAN_START_END (1) +#define MICROPY_PY_URE_SUB (1) +#define MICROPY_PY_UHASHLIB (1) +#define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_UTIME_MP_HAL (1) +#define MICROPY_PY_URANDOM (1) +#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rosc_random_u32()) +#define MICROPY_PY_USELECT (1) +#define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new +#define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SPI (1) +#define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) +#define MICROPY_PY_MACHINE_SPI_LSB (SPI_LSB_FIRST) +#define MICROPY_PY_FRAMEBUF (1) +#define MICROPY_VFS (1) +#define MICROPY_VFS_LFS2 (1) + +// Use VfsLfs2's types for fileio/textio +#define mp_type_fileio mp_type_vfs_lfs2_fileio +#define mp_type_textio mp_type_vfs_lfs2_textio + +// Use VFS's functions for import stat and builtin open +#define mp_import_stat mp_vfs_import_stat +#define mp_builtin_open_obj mp_vfs_open_obj + +// Hooks to add builtins + +#define MICROPY_PORT_BUILTINS \ + { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, + +extern const struct _mp_obj_module_t mp_module_machine; +extern const struct _mp_obj_module_t mp_module_onewire; +extern const struct _mp_obj_module_t mp_module_rp2; +extern const struct _mp_obj_module_t mp_module_uos; +extern const struct _mp_obj_module_t mp_module_utime; + +#define MICROPY_PORT_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR__rp2), (mp_obj_t)&mp_module_rp2 }, \ + { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos) }, \ + { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \ + +#define MICROPY_PORT_ROOT_POINTERS \ + const char *readline_hist[8]; \ + void *machine_pin_irq_obj[30]; \ + void *rp2_pio_irq_obj[2]; \ + void *rp2_state_machine_irq_obj[8]; \ + +#define MP_STATE_PORT MP_STATE_VM + +// Miscellaneous settings + +// TODO need to look and see if these could/should be spinlock/mutex +#define MICROPY_BEGIN_ATOMIC_SECTION() save_and_disable_interrupts() +#define MICROPY_END_ATOMIC_SECTION(state) restore_interrupts(state) + +#if MICROPY_HW_ENABLE_USBDEV +#define MICROPY_HW_USBDEV_TASK_HOOK extern void tud_task(void); tud_task(); +#define MICROPY_VM_HOOK_COUNT (10) +#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT; +#define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \ + vm_hook_divisor = MICROPY_VM_HOOK_COUNT; \ + MICROPY_HW_USBDEV_TASK_HOOK \ +} +#define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL +#define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL +#else +#define MICROPY_HW_USBDEV_TASK_HOOK +#endif + +#define MICROPY_EVENT_POLL_HOOK \ + do { \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ + best_effort_wfe_or_timeout(make_timeout_time_ms(1)); \ + MICROPY_HW_USBDEV_TASK_HOOK \ + } while (0); + +#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) + +#define MP_SSIZE_MAX (0x7fffffff) +typedef intptr_t mp_int_t; // must be pointer size +typedef uintptr_t mp_uint_t; // must be pointer size +typedef intptr_t mp_off_t; + +// We need to provide a declaration/definition of alloca() +#include + +#define BINARY_INFO_TAG_MICROPYTHON BINARY_INFO_MAKE_TAG('M', 'P') +#define BINARY_INFO_ID_MP_FROZEN 0x4a99d719 +#define MICROPY_FROZEN_LIST_ITEM(name, file) bi_decl(bi_string(BINARY_INFO_TAG_MICROPYTHON, BINARY_INFO_ID_MP_FROZEN, name)) + +extern uint32_t rosc_random_u32(void); diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c new file mode 100644 index 0000000000000..1122afcef730e --- /dev/null +++ b/ports/rp2/mphalport.c @@ -0,0 +1,142 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/stream.h" +#include "py/mphal.h" +#include "lib/timeutils/timeutils.h" +#include "tusb.h" +#include "uart.h" +#include "hardware/rtc.h" + +#if MICROPY_HW_ENABLE_UART_REPL + +#ifndef UART_BUFFER_LEN +// reasonably big so we can paste +#define UART_BUFFER_LEN 256 +#endif + +STATIC uint8_t stdin_ringbuf_array[UART_BUFFER_LEN]; +ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) }; + +#endif + +#if MICROPY_KBD_EXCEPTION + +int mp_interrupt_char = -1; + +void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { + (void)itf; + (void)wanted_char; + tud_cdc_read_char(); // discard interrupt char + mp_keyboard_interrupt(); +} + +void mp_hal_set_interrupt_char(int c) { + mp_interrupt_char = c; + tud_cdc_set_wanted_char(c); +} + +#endif + +uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { + uintptr_t ret = 0; + #if MICROPY_HW_ENABLE_UART_REPL + if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) { + ret |= MP_STREAM_POLL_RD; + } + #endif + #if MICROPY_HW_ENABLE_USBDEV + if (tud_cdc_connected() && tud_cdc_available()) { + ret |= MP_STREAM_POLL_RD; + } + #endif + return ret; +} + +// Receive single character +int mp_hal_stdin_rx_chr(void) { + for (;;) { + #if MICROPY_HW_ENABLE_UART_REPL + int c = ringbuf_get(&stdin_ringbuf); + if (c != -1) { + return c; + } + #endif + #if MICROPY_HW_ENABLE_USBDEV + if (tud_cdc_connected() && tud_cdc_available()) { + uint8_t buf[1]; + uint32_t count = tud_cdc_read(buf, sizeof(buf)); + if (count) { + return buf[0]; + } + } + #endif + MICROPY_EVENT_POLL_HOOK + } +} + +// Send string of given length +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + #if MICROPY_HW_ENABLE_UART_REPL + mp_uart_write_strn(str, len); + #endif + + #if MICROPY_HW_ENABLE_USBDEV + if (tud_cdc_connected()) { + for (size_t i = 0; i < len;) { + uint32_t n = len - i; + if (n > CFG_TUD_CDC_EP_BUFSIZE) { + n = CFG_TUD_CDC_EP_BUFSIZE; + } + while (n > tud_cdc_write_available()) { + tud_task(); + tud_cdc_write_flush(); + } + uint32_t n2 = tud_cdc_write(str + i, n); + tud_task(); + tud_cdc_write_flush(); + i += n2; + } + } + #endif +} + +void mp_hal_delay_ms(mp_uint_t ms) { + absolute_time_t t = make_timeout_time_ms(ms); + while (!time_reached(t)) { + mp_handle_pending(true); + best_effort_wfe_or_timeout(t); + MICROPY_HW_USBDEV_TASK_HOOK + } +} + +uint64_t mp_hal_time_ns(void) { + datetime_t t; + rtc_get_datetime(&t); + uint64_t s = timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec); + return s * 1000000000ULL; +} diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h new file mode 100644 index 0000000000000..40633dcf2c12f --- /dev/null +++ b/ports/rp2/mphalport.h @@ -0,0 +1,113 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +#ifndef MICROPY_INCLUDED_RP2_MPHALPORT_H +#define MICROPY_INCLUDED_RP2_MPHALPORT_H + +#include "py/mpconfig.h" +#include "py/ringbuf.h" +#include "pico/time.h" + +extern int mp_interrupt_char; +extern ringbuf_t stdin_ringbuf; + +void mp_hal_set_interrupt_char(int c); + +static inline void mp_hal_delay_us(mp_uint_t us) { + sleep_us(us); +} + +static inline void mp_hal_delay_us_fast(mp_uint_t us) { + busy_wait_us(us); +} + +#define mp_hal_quiet_timing_enter() MICROPY_BEGIN_ATOMIC_SECTION() +#define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state) + +static inline mp_uint_t mp_hal_ticks_us(void) { + return time_us_32(); +} + +static inline mp_uint_t mp_hal_ticks_ms(void) { + return to_ms_since_boot(get_absolute_time()); +} + +static inline mp_uint_t mp_hal_ticks_cpu(void) { + // ticks_cpu() is defined as using the highest-resolution timing source + // in the system. This is usually a CPU clock, but doesn't have to be. + return time_us_32(); +} + +// C-level pin HAL + +#include "py/obj.h" +#include "hardware/gpio.h" + +#define MP_HAL_PIN_FMT "%u" +#define mp_hal_pin_obj_t uint + +extern uint32_t machine_pin_open_drain_mask; + +mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in); + +static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) { + return pin; +} + +static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) { + gpio_set_function(pin, GPIO_FUNC_SIO); + gpio_set_dir(pin, GPIO_IN); + machine_pin_open_drain_mask &= ~(1 << pin); +} + +static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) { + gpio_set_function(pin, GPIO_FUNC_SIO); + gpio_set_dir(pin, GPIO_OUT); + machine_pin_open_drain_mask &= ~(1 << pin); +} + +static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { + gpio_set_function(pin, GPIO_FUNC_SIO); + gpio_set_dir(pin, GPIO_IN); + gpio_put(pin, 0); + machine_pin_open_drain_mask |= 1 << pin; +} + +static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) { + return gpio_get(pin); +} + +static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) { + gpio_put(pin, v); +} + +static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { + gpio_set_dir(pin, GPIO_OUT); +} + +static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) { + gpio_set_dir(pin, GPIO_IN); +} + +#endif // MICROPY_INCLUDED_RP2_MPHALPORT_H diff --git a/ports/rp2/mpthreadport.c b/ports/rp2/mpthreadport.c new file mode 100644 index 0000000000000..fb4428772afc1 --- /dev/null +++ b/ports/rp2/mpthreadport.c @@ -0,0 +1,105 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/gc.h" +#include "py/mpthread.h" +#include "pico/stdlib.h" +#include "pico/multicore.h" + +#if MICROPY_PY_THREAD + +extern uint8_t __StackTop, __StackBottom; + +void *core_state[2]; + +STATIC void *(*core1_entry)(void *) = NULL; +STATIC void *core1_arg = NULL; +STATIC uint32_t *core1_stack = NULL; +STATIC size_t core1_stack_num_words = 0; + +void mp_thread_init(void) { + mp_thread_set_state(&mp_state_ctx.thread); + core1_entry = NULL; +} + +void mp_thread_gc_others(void) { + if (get_core_num() == 0) { + // GC running on core0, trace core1's stack, if it's running. + if (core1_entry != NULL) { + gc_collect_root((void **)core1_stack, core1_stack_num_words); + } + } else { + // GC running on core1, trace core0's stack. + gc_collect_root((void **)&__StackBottom, (&__StackTop - &__StackBottom) / sizeof(uintptr_t)); + } +} + +STATIC void core1_entry_wrapper(void) { + if (core1_entry) { + core1_entry(core1_arg); + } + core1_entry = NULL; + // returning from here will loop the core forever (WFI) +} + +void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) { + // Check if core1 is already in use. + if (core1_entry != NULL) { + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("core1 in use")); + } + + core1_entry = entry; + core1_arg = arg; + + if (*stack_size == 0) { + *stack_size = 4096; // default stack size + } else if (*stack_size < 2048) { + *stack_size = 2048; // minimum stack size + } + + // Round stack size to a multiple of the word size. + core1_stack_num_words = *stack_size / sizeof(uint32_t); + *stack_size = core1_stack_num_words * sizeof(uint32_t); + + // Allocate stack. + core1_stack = m_new(uint32_t, core1_stack_num_words); + + // Create thread on core1. + multicore_reset_core1(); + multicore_launch_core1_with_stack(core1_entry_wrapper, core1_stack, *stack_size); + + // Adjust stack_size to provide room to recover from hitting the limit. + *stack_size -= 512; +} + +void mp_thread_start(void) { +} + +void mp_thread_finish(void) { +} + +#endif // MICROPY_PY_THREAD diff --git a/ports/rp2/mpthreadport.h b/ports/rp2/mpthreadport.h new file mode 100644 index 0000000000000..4583f6f5391f8 --- /dev/null +++ b/ports/rp2/mpthreadport.h @@ -0,0 +1,64 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_RP2_MPTHREADPORT_H +#define MICROPY_INCLUDED_RP2_MPTHREADPORT_H + +#include "py/mpthread.h" +#include "pico/mutex.h" + +typedef struct mutex mp_thread_mutex_t; + +extern void *core_state[2]; + +void mp_thread_init(void); +void mp_thread_gc_others(void); + +static inline void mp_thread_set_state(struct _mp_state_thread_t *state) { + core_state[get_core_num()] = state; +} + +static inline struct _mp_state_thread_t *mp_thread_get_state(void) { + return core_state[get_core_num()]; +} + +static inline void mp_thread_mutex_init(mp_thread_mutex_t *m) { + mutex_init(m); +} + +static inline int mp_thread_mutex_lock(mp_thread_mutex_t *m, int wait) { + if (wait) { + mutex_enter_blocking(m); + return 1; + } else { + return mutex_try_enter(m, NULL); + } +} + +static inline void mp_thread_mutex_unlock(mp_thread_mutex_t *m) { + mutex_exit(m); +} + +#endif // MICROPY_INCLUDED_RP2_MPTHREADPORT_H diff --git a/ports/rp2/qstrdefsport.h b/ports/rp2/qstrdefsport.h new file mode 100644 index 0000000000000..472d05f4375f6 --- /dev/null +++ b/ports/rp2/qstrdefsport.h @@ -0,0 +1,3 @@ +// qstrs specific to this port +// *FORMAT-OFF* +Q(/lib) diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c new file mode 100644 index 0000000000000..cd1bc65489dfc --- /dev/null +++ b/ports/rp2/rp2_flash.c @@ -0,0 +1,146 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "extmod/vfs.h" +#include "modrp2.h" +#include "hardware/flash.h" +#include "pico/binary_info.h" + +#define BLOCK_SIZE_BYTES (FLASH_SECTOR_SIZE) + +#ifndef MICROPY_HW_FLASH_STORAGE_BYTES +#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) +#endif + +#ifndef MICROPY_HW_FLASH_STORAGE_BASE +#define MICROPY_HW_FLASH_STORAGE_BASE (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES) +#endif + +static_assert(MICROPY_HW_FLASH_STORAGE_BASE + MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big"); + +typedef struct _rp2_flash_obj_t { + mp_obj_base_t base; + uint32_t flash_base; + uint32_t flash_size; +} rp2_flash_obj_t; + +STATIC rp2_flash_obj_t rp2_flash_obj = { + .base = { &rp2_flash_type }, + .flash_base = MICROPY_HW_FLASH_STORAGE_BASE, + .flash_size = MICROPY_HW_FLASH_STORAGE_BYTES, +}; + +// Tag the flash drive in the binary as readable/writable (but not reformatable) +bi_decl(bi_block_device( + BINARY_INFO_TAG_MICROPYTHON, + "MicroPython", + XIP_BASE + MICROPY_HW_FLASH_STORAGE_BASE, + MICROPY_HW_FLASH_STORAGE_BYTES, + NULL, + BINARY_INFO_BLOCK_DEV_FLAG_READ | + BINARY_INFO_BLOCK_DEV_FLAG_WRITE | + BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN)); + +STATIC mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // Check args. + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // Return singleton object. + return MP_OBJ_FROM_PTR(&rp2_flash_obj); +} + +STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) { + rp2_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t offset = mp_obj_get_int(args[1]) * BLOCK_SIZE_BYTES; + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE); + if (n_args == 4) { + offset += mp_obj_get_int(args[3]); + } + memcpy(bufinfo.buf, (void *)(XIP_BASE + self->flash_base + offset), bufinfo.len); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_flash_readblocks_obj, 3, 4, rp2_flash_readblocks); + +STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) { + rp2_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t offset = mp_obj_get_int(args[1]) * BLOCK_SIZE_BYTES; + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ); + if (n_args == 3) { + flash_range_erase(self->flash_base + offset, bufinfo.len); + // TODO check return value + } else { + offset += mp_obj_get_int(args[3]); + } + flash_range_program(self->flash_base + offset, bufinfo.buf, bufinfo.len); + // TODO check return value + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_flash_writeblocks_obj, 3, 4, rp2_flash_writeblocks); + +STATIC mp_obj_t rp2_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) { + rp2_flash_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case MP_BLOCKDEV_IOCTL_INIT: + return MP_OBJ_NEW_SMALL_INT(0); + case MP_BLOCKDEV_IOCTL_DEINIT: + return MP_OBJ_NEW_SMALL_INT(0); + case MP_BLOCKDEV_IOCTL_SYNC: + return MP_OBJ_NEW_SMALL_INT(0); + case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: + return MP_OBJ_NEW_SMALL_INT(self->flash_size / BLOCK_SIZE_BYTES); + case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: + return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE_BYTES); + case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: { + uint32_t offset = mp_obj_get_int(arg_in) * BLOCK_SIZE_BYTES; + flash_range_erase(self->flash_base + offset, BLOCK_SIZE_BYTES); + // TODO check return value + return MP_OBJ_NEW_SMALL_INT(0); + } + default: + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(rp2_flash_ioctl_obj, rp2_flash_ioctl); + +STATIC const mp_rom_map_elem_t rp2_flash_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&rp2_flash_readblocks_obj) }, + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&rp2_flash_writeblocks_obj) }, + { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&rp2_flash_ioctl_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2_flash_locals_dict, rp2_flash_locals_dict_table); + +const mp_obj_type_t rp2_flash_type = { + { &mp_type_type }, + .name = MP_QSTR_Flash, + .make_new = rp2_flash_make_new, + .locals_dict = (mp_obj_dict_t *)&rp2_flash_locals_dict, +}; diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c new file mode 100644 index 0000000000000..c8542127c03ba --- /dev/null +++ b/ports/rp2/rp2_pio.c @@ -0,0 +1,780 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/binary.h" +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" +#include "lib/utils/mpirq.h" +#include "modrp2.h" + +#include "hardware/clocks.h" +#include "hardware/irq.h" +#include "hardware/pio.h" + +#define PIO_NUM(pio) ((pio) == pio0 ? 0 : 1) + +typedef struct _rp2_pio_obj_t { + mp_obj_base_t base; + PIO pio; + uint8_t irq; +} rp2_pio_obj_t; + +typedef struct _rp2_pio_irq_obj_t { + mp_irq_obj_t base; + uint32_t flags; + uint32_t trigger; +} rp2_pio_irq_obj_t; + +typedef struct _rp2_state_machine_obj_t { + mp_obj_base_t base; + PIO pio; + uint8_t irq; + uint8_t sm; // 0-3 + uint8_t id; // 0-7 +} rp2_state_machine_obj_t; + +typedef struct _rp2_state_machine_irq_obj_t { + mp_irq_obj_t base; + uint8_t flags; + uint8_t trigger; +} rp2_state_machine_irq_obj_t; + +STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[8]; + +STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); + +STATIC void pio_irq0(PIO pio) { + uint32_t ints = pio->ints0; + + // Acknowledge SM0-3 IRQs if they are enabled on this IRQ0. + pio->irq = ints >> 8; + + // Call handler if it is registered, for PIO irqs. + rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(pio)]); + if (irq != NULL && (ints & irq->trigger)) { + irq->flags = ints & irq->trigger; + mp_irq_handler(&irq->base); + } + + // Call handler if it is registered, for StateMachine irqs. + for (size_t i = 0; i < 4; ++i) { + rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(pio) * 4 + i]); + if (irq != NULL && ((ints >> (8 + i)) & irq->trigger)) { + irq->flags = 1; + mp_irq_handler(&irq->base); + } + } +} + +STATIC void pio0_irq0(void) { + pio_irq0(pio0); +} + +STATIC void pio1_irq0(void) { + pio_irq0(pio1); +} + +void rp2_pio_init(void) { + // Reset all PIO instruction memory. + pio_clear_instruction_memory(pio0); + pio_clear_instruction_memory(pio1); + + // Set up interrupts. + memset(MP_STATE_PORT(rp2_pio_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_pio_irq_obj))); + memset(MP_STATE_PORT(rp2_state_machine_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_state_machine_irq_obj))); + irq_set_exclusive_handler(PIO0_IRQ_0, pio0_irq0); + irq_set_exclusive_handler(PIO1_IRQ_0, pio1_irq0); +} + +void rp2_pio_deinit(void) { + // Disable and clear interrupts. + irq_set_mask_enabled((1u << PIO0_IRQ_0) | (1u << PIO0_IRQ_1), false); + irq_remove_handler(PIO0_IRQ_0, pio0_irq0); + irq_remove_handler(PIO1_IRQ_0, pio1_irq0); +} + +/******************************************************************************/ +// Helper functions to manage asm_pio data structure. + +#define ASM_PIO_CONFIG_DEFAULT { -1, 0, 0, 0 }; + +enum { + PROG_DATA, + PROG_OFFSET_PIO0, + PROG_OFFSET_PIO1, + PROG_EXECCTRL, + PROG_SHIFTCTRL, + PROG_OUT_PINS, + PROG_SET_PINS, + PROG_SIDESET_PINS, + PROG_MAX_FIELDS, +}; + +typedef struct _asm_pio_config_t { + int8_t base; + uint8_t count; + uint8_t pindirs; + uint8_t pinvals; +} asm_pio_config_t; + +STATIC void asm_pio_override_shiftctrl(mp_obj_t arg, uint32_t bits, uint32_t lsb, pio_sm_config *config) { + if (arg != mp_const_none) { + config->shiftctrl = (config->shiftctrl & ~bits) | (mp_obj_get_int(arg) << lsb); + } +} + +STATIC void asm_pio_get_pins(const char *type, mp_obj_t prog_pins, mp_obj_t arg_base, asm_pio_config_t *config) { + if (prog_pins != mp_const_none) { + // The PIO program specified pins for initialisation on out/set/sideset. + if (mp_obj_is_integer(prog_pins)) { + // A single pin specified, set its dir and value. + config->count = 1; + mp_int_t value = mp_obj_get_int(prog_pins); + config->pindirs = value >> 1; + config->pinvals = value & 1; + } else { + // An array of pins specified, set their dirs and values. + size_t count; + mp_obj_t *items; + mp_obj_get_array(prog_pins, &count, &items); + config->count = count; + for (size_t i = 0; i < config->count; ++i) { + mp_int_t value = mp_obj_get_int(items[i]); + config->pindirs |= (value >> 1) << i; + config->pinvals |= (value & 1) << i; + } + } + } + + if (arg_base != mp_const_none) { + // The instantiation of the PIO program specified a base pin. + config->base = mp_hal_get_pin_obj(arg_base); + } +} + +STATIC void asm_pio_init_gpio(PIO pio, uint32_t sm, asm_pio_config_t *config) { + uint32_t pinmask = ((1 << config->count) - 1) << config->base; + pio_sm_set_pins_with_mask(pio, sm, config->pinvals << config->base, pinmask); + pio_sm_set_pindirs_with_mask(pio, sm, config->pindirs << config->base, pinmask); + for (size_t i = 0; i < config->count; ++i) { + gpio_set_function(config->base + i, pio == pio0 ? GPIO_FUNC_PIO0 : GPIO_FUNC_PIO1); + } +} + +/******************************************************************************/ +// PIO object + +STATIC const mp_irq_methods_t rp2_pio_irq_methods; + +STATIC rp2_pio_obj_t rp2_pio_obj[] = { + { { &rp2_pio_type }, pio0, PIO0_IRQ_0 }, + { { &rp2_pio_type }, pio1, PIO1_IRQ_0 }, +}; + +STATIC void rp2_pio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "PIO(%u)", self->pio == pio0 ? 0 : 1); +} + +// constructor(id) +STATIC mp_obj_t rp2_pio_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // Get the PIO object. + int pio_id = mp_obj_get_int(args[0]); + if (!(0 <= pio_id && pio_id < MP_ARRAY_SIZE(rp2_pio_obj))) { + mp_raise_ValueError("invalid PIO"); + } + const rp2_pio_obj_t *self = &rp2_pio_obj[pio_id]; + + // Return the PIO object. + return MP_OBJ_FROM_PTR(self); +} + +// PIO.add_program(prog) +STATIC mp_obj_t rp2_pio_add_program(mp_obj_t self_in, mp_obj_t prog_in) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in); + + // Get the program data. + mp_obj_t *prog; + mp_obj_get_array_fixed_n(prog_in, PROG_MAX_FIELDS, &prog); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(prog[PROG_DATA], &bufinfo, MP_BUFFER_READ); + + // Add the program data to the PIO instruction memory. + struct pio_program pio_program = { bufinfo.buf, bufinfo.len / 2, -1 }; + if (!pio_can_add_program(self->pio, &pio_program)) { + mp_raise_OSError(MP_ENOMEM); + } + uint offset = pio_add_program(self->pio, &pio_program); + + // Store the program offset in the program object. + prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)] = MP_OBJ_NEW_SMALL_INT(offset); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(rp2_pio_add_program_obj, rp2_pio_add_program); + +// PIO.remove_program([prog]) +STATIC mp_obj_t rp2_pio_remove_program(size_t n_args, const mp_obj_t *args) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(args[0]); + + // Default to remove all programs. + uint8_t length = 32; + uint offset = 0; + + if (n_args > 1) { + // Get specific program to remove. + mp_obj_t *prog; + mp_obj_get_array_fixed_n(args[1], PROG_MAX_FIELDS, &prog); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(prog[PROG_DATA], &bufinfo, MP_BUFFER_READ); + length = bufinfo.len / 2; + offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]); + if (offset < 0) { + mp_raise_ValueError("prog not in instruction memory"); + } + // Invalidate the program offset in the program object. + prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)] = MP_OBJ_NEW_SMALL_INT(-1); + } + + // Remove the program from the instruction memory. + struct pio_program pio_program = { NULL, length, -1 }; + pio_remove_program(self->pio, &pio_program, offset); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_pio_remove_program_obj, 1, 2, rp2_pio_remove_program); + +// PIO.state_machine(id, prog, freq=-1, *, set=None) +STATIC mp_obj_t rp2_pio_state_machine(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + + // Get and verify the state machine id. + mp_int_t sm_id = mp_obj_get_int(pos_args[1]); + if (!(0 <= sm_id && sm_id < 4)) { + mp_raise_ValueError("invalide state machine"); + } + + // Return the correct StateMachine object. + const rp2_state_machine_obj_t *sm = &rp2_state_machine_obj[(self->pio == pio0 ? 0 : 4) + sm_id]; + + if (n_args > 2 || kw_args->used > 0) { + // Configuration arguments given so init this StateMachine. + rp2_state_machine_init_helper(sm, n_args - 2, pos_args + 2, kw_args); + } + + return MP_OBJ_FROM_PTR(sm); +} +MP_DEFINE_CONST_FUN_OBJ_KW(rp2_pio_state_machine_obj, 2, rp2_pio_state_machine); + +// PIO.irq(handler=None, trigger=IRQ_SM0|IRQ_SM1|IRQ_SM2|IRQ_SM3, hard=False) +STATIC mp_obj_t rp2_pio_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_handler, ARG_trigger, ARG_hard }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = 0xf00} }, + { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, + }; + + // Parse the arguments. + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the IRQ object. + rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]); + + // Allocate the IRQ object if it doesn't already exist. + if (irq == NULL) { + irq = m_new_obj(rp2_pio_irq_obj_t); + irq->base.base.type = &mp_irq_type; + irq->base.methods = (mp_irq_methods_t *)&rp2_pio_irq_methods; + irq->base.parent = MP_OBJ_FROM_PTR(self); + irq->base.handler = mp_const_none; + irq->base.ishard = false; + MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]) = irq; + } + + if (n_args > 1 || kw_args->used != 0) { + // Configure IRQ. + + // Disable all IRQs while data is updated. + irq_set_enabled(self->irq, false); + + // Update IRQ data. + irq->base.handler = args[ARG_handler].u_obj; + irq->base.ishard = args[ARG_hard].u_bool; + irq->flags = 0; + irq->trigger = args[ARG_trigger].u_int; + + // Enable IRQ if a handler is given. + if (args[ARG_handler].u_obj != mp_const_none) { + self->pio->inte0 = irq->trigger; + irq_set_enabled(self->irq, true); + } + } + + return MP_OBJ_FROM_PTR(irq); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(rp2_pio_irq_obj, 1, rp2_pio_irq); + +STATIC const mp_rom_map_elem_t rp2_pio_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_add_program), MP_ROM_PTR(&rp2_pio_add_program_obj) }, + { MP_ROM_QSTR(MP_QSTR_remove_program), MP_ROM_PTR(&rp2_pio_remove_program_obj) }, + { MP_ROM_QSTR(MP_QSTR_state_machine), MP_ROM_PTR(&rp2_pio_state_machine_obj) }, + { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&rp2_pio_irq_obj) }, + + { MP_ROM_QSTR(MP_QSTR_IN_LOW), MP_ROM_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_IN_HIGH), MP_ROM_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_OUT_LOW), MP_ROM_INT(2) }, + { MP_ROM_QSTR(MP_QSTR_OUT_HIGH), MP_ROM_INT(3) }, + + { MP_ROM_QSTR(MP_QSTR_SHIFT_LEFT), MP_ROM_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_SHIFT_RIGHT), MP_ROM_INT(1) }, + + { MP_ROM_QSTR(MP_QSTR_IRQ_SM0), MP_ROM_INT(0x100) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_SM1), MP_ROM_INT(0x200) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_SM2), MP_ROM_INT(0x400) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_SM3), MP_ROM_INT(0x800) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2_pio_locals_dict, rp2_pio_locals_dict_table); + +const mp_obj_type_t rp2_pio_type = { + { &mp_type_type }, + .name = MP_QSTR_PIO, + .print = rp2_pio_print, + .make_new = rp2_pio_make_new, + .locals_dict = (mp_obj_dict_t *)&rp2_pio_locals_dict, +}; + +STATIC mp_uint_t rp2_pio_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in); + rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]); + irq_set_enabled(self->irq, false); + irq->flags = 0; + irq->trigger = new_trigger; + irq_set_enabled(self->irq, true); + return 0; +} + +STATIC mp_uint_t rp2_pio_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in); + rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]); + if (info_type == MP_IRQ_INFO_FLAGS) { + return irq->flags; + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return irq->trigger; + } + return 0; +} + +STATIC const mp_irq_methods_t rp2_pio_irq_methods = { + .trigger = rp2_pio_irq_trigger, + .info = rp2_pio_irq_info, +}; + +/******************************************************************************/ +// StateMachine object + +STATIC const mp_irq_methods_t rp2_state_machine_irq_methods; + +STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[] = { + { { &rp2_state_machine_type }, pio0, PIO0_IRQ_0, 0, 0 }, + { { &rp2_state_machine_type }, pio0, PIO0_IRQ_0, 1, 1 }, + { { &rp2_state_machine_type }, pio0, PIO0_IRQ_0, 2, 2 }, + { { &rp2_state_machine_type }, pio0, PIO0_IRQ_0, 3, 3 }, + { { &rp2_state_machine_type }, pio1, PIO1_IRQ_0, 0, 4 }, + { { &rp2_state_machine_type }, pio1, PIO1_IRQ_0, 1, 5 }, + { { &rp2_state_machine_type }, pio1, PIO1_IRQ_0, 2, 6 }, + { { &rp2_state_machine_type }, pio1, PIO1_IRQ_0, 3, 7 }, +}; + +STATIC void rp2_state_machine_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "StateMachine(%u)", self->id); +} + +// StateMachine.init(prog, freq=-1, *, +// in_base=None, out_base=None, set_base=None, sideset_base=None, +// in_shiftdir=None, out_shiftdir=None, push_thresh=None, pull_thresh=None, +// ) +STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { + ARG_prog, ARG_freq, + ARG_in_base, ARG_out_base, ARG_set_base, ARG_sideset_base, + ARG_in_shiftdir, ARG_out_shiftdir, ARG_push_thresh, ARG_pull_thresh + }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_prog, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_freq, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_in_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_out_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_set_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_sideset_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_in_shiftdir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_out_shiftdir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_push_thresh, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_pull_thresh, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the program. + mp_obj_t *prog; + mp_obj_get_array_fixed_n(args[ARG_prog].u_obj, PROG_MAX_FIELDS, &prog); + + // Get and the program offset, and load it into memory if it's not already there. + mp_int_t offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]); + if (offset < 0) { + rp2_pio_add_program(&rp2_pio_obj[PIO_NUM(self->pio)], args[ARG_prog].u_obj); + offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]); + } + + // Compute the clock divider. + float div; + if (args[ARG_freq].u_int < 0) { + div = 1; + } else if (args[ARG_freq].u_int == 0) { + div = 0; + } else { + div = (float)clock_get_hz(clk_sys) / (float)args[ARG_freq].u_int; + } + + // Disable and reset the state machine. + pio_sm_init(self->pio, self->sm, offset, NULL); + + // Build the state machine config. + pio_sm_config config = pio_get_default_sm_config(); + sm_config_set_clkdiv(&config, div); + config.execctrl = mp_obj_get_int_truncated(prog[PROG_EXECCTRL]); + config.shiftctrl = mp_obj_get_int_truncated(prog[PROG_SHIFTCTRL]); + + // Adjust wrap top/bottom to account for location of program in instruction memory. + config.execctrl += (offset << PIO_SM0_EXECCTRL_WRAP_TOP_LSB) + + (offset << PIO_SM0_EXECCTRL_WRAP_BOTTOM_LSB); + + // Configure in pin base, if needed. + if (args[ARG_in_base].u_obj != mp_const_none) { + sm_config_set_in_pins(&config, mp_hal_get_pin_obj(args[ARG_in_base].u_obj)); + } + + // Configure out pins, if needed. + asm_pio_config_t out_config = ASM_PIO_CONFIG_DEFAULT; + asm_pio_get_pins("out", prog[PROG_OUT_PINS], args[ARG_out_base].u_obj, &out_config); + if (out_config.base >= 0) { + sm_config_set_out_pins(&config, out_config.base, out_config.count); + } + + // Configure set pin, if needed. + asm_pio_config_t set_config = ASM_PIO_CONFIG_DEFAULT; + asm_pio_get_pins("set", prog[PROG_SET_PINS], args[ARG_set_base].u_obj, &set_config); + if (set_config.base >= 0) { + sm_config_set_set_pins(&config, set_config.base, set_config.count); + } + + // Configure sideset pin, if needed. + asm_pio_config_t sideset_config = ASM_PIO_CONFIG_DEFAULT; + asm_pio_get_pins("sideset", prog[PROG_SIDESET_PINS], args[ARG_sideset_base].u_obj, &sideset_config); + if (sideset_config.base >= 0) { + uint32_t count = sideset_config.count; + if (config.execctrl & (1 << PIO_SM0_EXECCTRL_SIDE_EN_LSB)) { + // When sideset is optional, count includes the option bit. + ++count; + } + config.pinctrl |= count << PIO_SM0_PINCTRL_SIDESET_COUNT_LSB; + sm_config_set_sideset_pins(&config, sideset_config.base); + } + + // Override shift state if needed. + asm_pio_override_shiftctrl(args[ARG_in_shiftdir].u_obj, PIO_SM0_SHIFTCTRL_IN_SHIFTDIR_BITS, PIO_SM0_SHIFTCTRL_IN_SHIFTDIR_LSB, &config); + asm_pio_override_shiftctrl(args[ARG_out_shiftdir].u_obj, PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_BITS, PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_LSB, &config); + asm_pio_override_shiftctrl(args[ARG_push_thresh].u_obj, PIO_SM0_SHIFTCTRL_PUSH_THRESH_BITS, PIO_SM0_SHIFTCTRL_PUSH_THRESH_LSB, &config); + asm_pio_override_shiftctrl(args[ARG_pull_thresh].u_obj, PIO_SM0_SHIFTCTRL_PULL_THRESH_BITS, PIO_SM0_SHIFTCTRL_PULL_THRESH_LSB, &config); + + // Configure the state machine. + pio_sm_set_config(self->pio, self->sm, &config); + + // Configure the GPIO. + if (out_config.base >= 0) { + asm_pio_init_gpio(self->pio, self->sm, &out_config); + } + if (set_config.base >= 0) { + asm_pio_init_gpio(self->pio, self->sm, &set_config); + } + if (sideset_config.base >= 0) { + asm_pio_init_gpio(self->pio, self->sm, &sideset_config); + } + + return mp_const_none; +} + +// StateMachine(id, ...) +STATIC mp_obj_t rp2_state_machine_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + // Get the StateMachine object. + mp_int_t sm_id = mp_obj_get_int(args[0]); + if (!(0 <= sm_id && sm_id < MP_ARRAY_SIZE(rp2_state_machine_obj))) { + mp_raise_ValueError("invalid StateMachine"); + } + const rp2_state_machine_obj_t *self = &rp2_state_machine_obj[sm_id]; + + if (n_args > 1 || n_kw > 0) { + // Configuration arguments given so init this StateMachine. + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + rp2_state_machine_init_helper(self, n_args - 1, args + 1, &kw_args); + } + + // Return the StateMachine object. + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t rp2_state_machine_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + return rp2_state_machine_init_helper(MP_OBJ_TO_PTR(args[0]), n_args - 1, args + 1, kw_args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(rp2_state_machine_init_obj, 1, rp2_state_machine_init); + +// StateMachine.active([value]) +STATIC mp_obj_t rp2_state_machine_active(size_t n_args, const mp_obj_t *args) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(args[0]); + if (n_args > 1) { + pio_sm_set_enabled(self->pio, self->sm, mp_obj_is_true(args[1])); + } + return mp_obj_new_bool((self->pio->ctrl >> self->sm) & 1); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_active_obj, 1, 2, rp2_state_machine_active); + +// StateMachine.exec(instr) +STATIC mp_obj_t rp2_state_machine_exec(mp_obj_t self_in, mp_obj_t instr_in) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t rp2_module = mp_import_name(MP_QSTR_rp2, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)); + mp_obj_t asm_pio_encode = mp_load_attr(rp2_module, MP_QSTR_asm_pio_encode); + uint32_t sideset_count = self->pio->sm[self->sm].pinctrl >> PIO_SM0_PINCTRL_SIDESET_COUNT_LSB; + mp_obj_t encoded_obj = mp_call_function_2(asm_pio_encode, instr_in, MP_OBJ_NEW_SMALL_INT(sideset_count)); + mp_int_t encoded = mp_obj_get_int(encoded_obj); + pio_sm_exec(self->pio, self->sm, encoded); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(rp2_state_machine_exec_obj, rp2_state_machine_exec); + +// StateMachine.get(buf=None, shift=0) +STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(args[0]); + mp_buffer_info_t bufinfo; + bufinfo.buf = NULL; + uint32_t shift = 0; + if (n_args > 1) { + if (args[1] != mp_const_none) { + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); + if (bufinfo.typecode == BYTEARRAY_TYPECODE) { + bufinfo.typecode = 'b'; + } else { + bufinfo.typecode |= 0x20; // make lowercase to support upper and lower + } + } + if (n_args > 2) { + shift = mp_obj_get_int(args[2]); + } + } + uint8_t *dest = bufinfo.buf; + const uint8_t *dest_top = dest + bufinfo.len; + for (;;) { + while (pio_sm_is_rx_fifo_empty(self->pio, self->sm)) { + // This delay must be fast. + mp_handle_pending(true); + MICROPY_HW_USBDEV_TASK_HOOK + } + uint32_t value = pio_sm_get(self->pio, self->sm) >> shift; + if (dest == NULL) { + return mp_obj_new_int_from_uint(value); + } + if (dest >= dest_top) { + return args[1]; + } + if (bufinfo.typecode == 'b') { + *(uint8_t *)dest = value; + dest += sizeof(uint8_t); + } else if (bufinfo.typecode == 'h') { + *(uint16_t *)dest = value; + dest += sizeof(uint16_t); + } else if (bufinfo.typecode == 'i') { + *(uint32_t *)dest = value; + dest += sizeof(uint32_t); + } else { + mp_raise_ValueError("unsupported buffer type"); + } + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_get_obj, 1, 3, rp2_state_machine_get); + +// StateMachine.put(value, shift=0) +STATIC mp_obj_t rp2_state_machine_put(size_t n_args, const mp_obj_t *args) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t shift = 0; + if (n_args > 2) { + shift = mp_obj_get_int(args[2]); + } + uint32_t data; + mp_buffer_info_t bufinfo; + if (!mp_get_buffer(args[1], &bufinfo, MP_BUFFER_READ)) { + data = mp_obj_get_int_truncated(args[1]); + bufinfo.buf = &data; + bufinfo.len = sizeof(uint32_t); + bufinfo.typecode = 'I'; + } + const uint8_t *src = bufinfo.buf; + const uint8_t *src_top = src + bufinfo.len; + while (src < src_top) { + uint32_t value; + if (bufinfo.typecode == 'B' || bufinfo.typecode == BYTEARRAY_TYPECODE) { + value = *(uint8_t *)src; + src += sizeof(uint8_t); + } else if (bufinfo.typecode == 'H') { + value = *(uint16_t *)src; + src += sizeof(uint16_t); + } else if (bufinfo.typecode == 'I') { + value = *(uint32_t *)src; + src += sizeof(uint32_t); + } else { + mp_raise_ValueError("unsupported buffer type"); + } + while (pio_sm_is_tx_fifo_full(self->pio, self->sm)) { + // This delay must be fast. + mp_handle_pending(true); + MICROPY_HW_USBDEV_TASK_HOOK + } + pio_sm_put(self->pio, self->sm, value << shift); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_put_obj, 2, 3, rp2_state_machine_put); + +// StateMachine.irq(handler=None, trigger=0|1, hard=False) +STATIC mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_handler, ARG_trigger, ARG_hard }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = 1} }, + { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, + }; + + // Parse the arguments. + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the IRQ object. + rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[self->id]); + + // Allocate the IRQ object if it doesn't already exist. + if (irq == NULL) { + irq = m_new_obj(rp2_state_machine_irq_obj_t); + irq->base.base.type = &mp_irq_type; + irq->base.methods = (mp_irq_methods_t *)&rp2_state_machine_irq_methods; + irq->base.parent = MP_OBJ_FROM_PTR(self); + irq->base.handler = mp_const_none; + irq->base.ishard = false; + MP_STATE_PORT(rp2_state_machine_irq_obj[self->id]) = irq; + } + + if (n_args > 1 || kw_args->used != 0) { + // Configure IRQ. + + // Disable all IRQs while data is updated. + irq_set_enabled(self->irq, false); + + // Update IRQ data. + irq->base.handler = args[ARG_handler].u_obj; + irq->base.ishard = args[ARG_hard].u_bool; + irq->flags = 0; + irq->trigger = args[ARG_trigger].u_int; + + // Enable IRQ if a handler is given. + if (args[ARG_handler].u_obj == mp_const_none) { + self->pio->inte0 &= ~(1 << (8 + self->sm)); + } else { + self->pio->inte0 |= 1 << (8 + self->sm); + } + + if (self->pio->inte0) { + irq_set_enabled(self->irq, true); + } + } + + return MP_OBJ_FROM_PTR(irq); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(rp2_state_machine_irq_obj, 1, rp2_state_machine_irq); + +STATIC const mp_rom_map_elem_t rp2_state_machine_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&rp2_state_machine_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&rp2_state_machine_active_obj) }, + { MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&rp2_state_machine_exec_obj) }, + { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&rp2_state_machine_get_obj) }, + { MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&rp2_state_machine_put_obj) }, + { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&rp2_state_machine_irq_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2_state_machine_locals_dict, rp2_state_machine_locals_dict_table); + +const mp_obj_type_t rp2_state_machine_type = { + { &mp_type_type }, + .name = MP_QSTR_StateMachine, + .print = rp2_state_machine_print, + .make_new = rp2_state_machine_make_new, + .locals_dict = (mp_obj_dict_t *)&rp2_state_machine_locals_dict, +}; + +STATIC mp_uint_t rp2_state_machine_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(self->pio)]); + irq_set_enabled(self->irq, false); + irq->flags = 0; + irq->trigger = new_trigger; + irq_set_enabled(self->irq, true); + return 0; +} + +STATIC mp_uint_t rp2_state_machine_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(self->pio)]); + if (info_type == MP_IRQ_INFO_FLAGS) { + return irq->flags; + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return irq->trigger; + } + return 0; +} + +STATIC const mp_irq_methods_t rp2_state_machine_irq_methods = { + .trigger = rp2_state_machine_irq_trigger, + .info = rp2_state_machine_irq_info, +}; diff --git a/ports/rp2/tusb_config.h b/ports/rp2/tusb_config.h new file mode 100644 index 0000000000000..1402edf3795b5 --- /dev/null +++ b/ports/rp2/tusb_config.h @@ -0,0 +1,34 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H +#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H + +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) + +#define CFG_TUD_CDC (1) +#define CFG_TUD_CDC_RX_BUFSIZE (256) +#define CFG_TUD_CDC_TX_BUFSIZE (256) + +#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H diff --git a/ports/rp2/tusb_port.c b/ports/rp2/tusb_port.c new file mode 100644 index 0000000000000..afb566bdb4a2f --- /dev/null +++ b/ports/rp2/tusb_port.c @@ -0,0 +1,115 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "tusb.h" + +#define USBD_VID (0x2E8A) // Raspberry Pi +#define USBD_PID (0x0005) // RP2 MicroPython + +#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) +#define USBD_MAX_POWER_MA (250) + +#define USBD_ITF_CDC (0) // needs 2 interfaces +#define USBD_ITF_MAX (2) + +#define USBD_CDC_EP_CMD (0x81) +#define USBD_CDC_EP_OUT (0x02) +#define USBD_CDC_EP_IN (0x82) +#define USBD_CDC_CMD_MAX_SIZE (8) +#define USBD_CDC_IN_OUT_MAX_SIZE (64) + +#define USBD_STR_0 (0x00) +#define USBD_STR_MANUF (0x01) +#define USBD_STR_PRODUCT (0x02) +#define USBD_STR_SERIAL (0x03) +#define USBD_STR_CDC (0x04) + +// Note: descriptors returned from callbacks must exist long enough for transfer to complete + +static const tusb_desc_device_t usbd_desc_device = { + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .idVendor = USBD_VID, + .idProduct = USBD_PID, + .bcdDevice = 0x0100, + .iManufacturer = USBD_STR_MANUF, + .iProduct = USBD_STR_PRODUCT, + .iSerialNumber = USBD_STR_SERIAL, + .bNumConfigurations = 1, +}; + +static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, + TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), + + TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), +}; + +static const char *const usbd_desc_str[] = { + [USBD_STR_MANUF] = "MicroPython", + [USBD_STR_PRODUCT] = "Board in FS mode", + [USBD_STR_SERIAL] = "000000000000", // TODO + [USBD_STR_CDC] = "Board CDC", +}; + +const uint8_t *tud_descriptor_device_cb(void) { + return (const uint8_t *)&usbd_desc_device; +} + +const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { + (void)index; + return usbd_desc_cfg; +} + +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + #define DESC_STR_MAX (20) + static uint16_t desc_str[DESC_STR_MAX]; + + uint8_t len; + if (index == 0) { + desc_str[1] = 0x0409; // supported language is English + len = 1; + } else { + if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) { + return NULL; + } + const char *str = usbd_desc_str[index]; + for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) { + desc_str[1 + len] = str[len]; + } + } + + // first byte is length (including header), second byte is string type + desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2); + + return desc_str; +} diff --git a/ports/rp2/uart.c b/ports/rp2/uart.c new file mode 100644 index 0000000000000..b7991563af034 --- /dev/null +++ b/ports/rp2/uart.c @@ -0,0 +1,63 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/ringbuf.h" +#include "py/mphal.h" +#include "uart.h" + +#include "hardware/uart.h" +#include "hardware/irq.h" +#include "hardware/regs/uart.h" + +#if MICROPY_HW_ENABLE_UART_REPL + +void uart_irq(void) { + uart_get_hw(uart_default)->icr = UART_UARTICR_BITS; // clear interrupt flags + if (uart_is_readable(uart_default)) { + int c = uart_getc(uart_default); + #if MICROPY_KBD_EXCEPTION + if (c == mp_interrupt_char) { + mp_keyboard_interrupt(); + return; + } + #endif + ringbuf_put(&stdin_ringbuf, c); + } +} + +void mp_uart_init(void) { + uart_get_hw(uart_default)->imsc = UART_UARTIMSC_BITS; // enable mask + uint irq_num = uart_get_index(uart_default) ? UART1_IRQ : UART0_IRQ; + irq_set_exclusive_handler(irq_num, uart_irq); + irq_set_enabled(irq_num, true); // enable irq +} + +void mp_uart_write_strn(const char *str, size_t len) { + uart_write_blocking(uart_default, (const uint8_t *)str, len); +} + +#endif diff --git a/ports/rp2/uart.h b/ports/rp2/uart.h new file mode 100644 index 0000000000000..a49172f8f5781 --- /dev/null +++ b/ports/rp2/uart.h @@ -0,0 +1,32 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_RP2_UART_H +#define MICROPY_INCLUDED_RP2_UART_H + +void mp_uart_init(void); +void mp_uart_write_strn(const char *str, size_t len); + +#endif // MICROPY_INCLUDED_RP2_UART_H From b8f4c623f9002099b0910ac873ef9e3acbcf428c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Jan 2021 19:24:12 +1100 Subject: [PATCH 0542/3533] github/workflows: Add CI workflow for rp2 port. Signed-off-by: Damien George --- .github/workflows/ports_rp2.yml | 23 +++++++++++++++++++++++ tools/ci.sh | 14 ++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .github/workflows/ports_rp2.yml diff --git a/.github/workflows/ports_rp2.yml b/.github/workflows/ports_rp2.yml new file mode 100644 index 0000000000000..668b79cae256c --- /dev/null +++ b/.github/workflows/ports_rp2.yml @@ -0,0 +1,23 @@ +name: rp2 port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/rp2/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_rp2_setup + - name: Build + run: source tools/ci.sh && ci_rp2_build diff --git a/tools/ci.sh b/tools/ci.sh index 986ec6756eac7..4e4aad5600e77 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -176,6 +176,20 @@ function ci_qemu_arm_build { make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test } +######################################################################################## +# ports/rp2 + +function ci_rp2_setup { + ci_gcc_arm_setup +} + +function ci_rp2_build { + make ${MAKEOPTS} -C mpy-cross + git submodule update --init lib/pico-sdk + git -C lib/pico-sdk submodule update --init lib/tinyusb + make ${MAKEOPTS} -C ports/rp2 +} + ######################################################################################## # ports/samd From 0f9a9129da0d0ef60956dcfa36b02b62d731b1b1 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 29 Jan 2021 17:45:33 +1100 Subject: [PATCH 0543/3533] stm32/rfcore: Fix flow control for IPCC RX IRQ. Don't clear the IPCC channel flag until we've actually handled the incoming data, or else the wireless firmware may clobber the IPCC buffer if more data arrives. This requires masking the IRQ until the data is handled. Signed-off-by: Jim Mussared --- ports/stm32/rfcore.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 7d0a8520a6fc4..b11c4f2022733 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -434,10 +434,16 @@ STATIC void tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, parse // Only call this when IRQs are disabled on this channel. STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) { if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, ch)) { + // Process new data. tl_process_msg(head, ch, parse); - // Clear receive channel. + // Clear receive channel (allows RF core to send more data to us). LL_C1_IPCC_ClearFlag_CHx(IPCC, ch); + + if (ch == IPCC_CH_BLE) { + // Renable IRQs for BLE now that we've cleared the flag. + LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE); + } } } @@ -495,17 +501,17 @@ STATIC int tl_ble_wait_resp(void) { } } - // C2 set IPCC flag. + // C2 set IPCC flag -- process the data, clear the flag, and re-enable IRQs. tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL); return 0; } // Synchronously send a BLE command. STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) { + // Poll for completion rather than wait for IRQ->scheduler. LL_C1_IPCC_DisableReceiveChannel(IPCC, IPCC_CH_BLE); tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, buf, len); tl_ble_wait_resp(); - LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE); } /******************************************************************************/ @@ -632,7 +638,7 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { void rfcore_ble_check_msg(int (*cb)(void *, const uint8_t *, size_t), void *env) { parse_hci_info_t parse = { cb, env, false }; - tl_process_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, &parse); + tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, &parse); // Intercept HCI_Reset events and reconfigure the controller following the reset if (parse.was_hci_reset_evt) { @@ -679,7 +685,8 @@ void IPCC_C1_RX_IRQHandler(void) { DEBUG_printf("IPCC_C1_RX_IRQHandler\n"); if (LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) { - LL_C1_IPCC_ClearFlag_CHx(IPCC, IPCC_CH_BLE); + // Disable this IRQ until the incoming data is processed (in tl_check_msg). + LL_C1_IPCC_DisableReceiveChannel(IPCC, IPCC_CH_BLE); #if MICROPY_PY_BLUETOOTH // Queue up the scheduler to process UART data and run events. From 47d02b3104369430db9fd7a6f80f9fa47badaf65 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 29 Jan 2021 17:46:33 +1100 Subject: [PATCH 0544/3533] extmod/nimble: Improve the flow control for l2cap recv path. If the _IRQ_L2CAP_RECV handler does the actual consumption of the incoming data (i.e. via l2cap_recvinto), rather than setting a flag for non-scheduler-context to handle it later, then two things can happen: - It can starve the VM (i.e. the scheduled task never terminates). This is because calling l2cap_recvinto will empty the rx buffer, which will grant more credits to the channel (an HCI command), meaning more data can arrive. This means that the loop in hal_uart.c that keeps reading HCI data from the uart and executing NimBLE events as they are created will not terminate, preventing other VM code from running. - There's no flow control (i.e. data will arrive too quickly). The channel shouldn't be given credits until after we return from scheduler context. It's preferable that no work is done in scheduler/IRQ context. But to prevent this being a problem this commit changes l2cap_recvinto so that if it is called in IRQ context, and the Python handler empties the rx buffer, then don't grant credits until the Python handler is complete. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 36 +++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 8bead890d8373..a2eb8a588c67e 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -1355,6 +1355,7 @@ typedef struct _mp_bluetooth_nimble_l2cap_channel_t { struct os_mbuf_pool sdu_mbuf_pool; struct os_mempool sdu_mempool; struct os_mbuf *rx_pending; + bool irq_in_progress; uint16_t mtu; os_membuf_t sdu_mem[]; } mp_bluetooth_nimble_l2cap_channel_t; @@ -1441,7 +1442,23 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { chan->chan->coc_rx.sdu = sdu_rx; ble_l2cap_get_chan_info(event->receive.chan, &info); + + // Don't allow granting more credits until after the IRQ is handled. + chan->irq_in_progress = true; + mp_bluetooth_gattc_on_l2cap_recv(event->receive.conn_handle, info.scid); + chan->irq_in_progress = false; + + // If all data has been consumed by the IRQ handler, then now allow + // more credits. If the IRQ handler doesn't consume all available data + // then rx_pending will be still set. + if (!chan->rx_pending) { + struct os_mbuf *sdu_rx = chan->chan->coc_rx.sdu; + assert(sdu_rx); + if (sdu_rx) { + ble_l2cap_recv_ready(chan->chan, sdu_rx); + } + } break; } case BLE_L2CAP_EVENT_COC_TX_UNSTALLED: { @@ -1508,6 +1525,7 @@ STATIC int create_l2cap_channel(uint16_t mtu, mp_bluetooth_nimble_l2cap_channel_ chan->mtu = mtu; chan->rx_pending = NULL; + chan->irq_in_progress = false; int err = os_mempool_init(&chan->sdu_mempool, buf_blocks, L2CAP_BUF_BLOCK_SIZE, chan->sdu_mem, "l2cap_sdu_pool"); if (err != 0) { @@ -1635,13 +1653,17 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf os_mbuf_free_chain(chan->rx_pending); chan->rx_pending = NULL; - // We've already given the channel a new mbuf in l2cap_channel_event above, so - // re-use that mbuf in the call to ble_l2cap_recv_ready. This will just - // give the channel more credits. - struct os_mbuf *sdu_rx = chan->chan->coc_rx.sdu; - assert(sdu_rx); - if (sdu_rx) { - ble_l2cap_recv_ready(chan->chan, sdu_rx); + // If we're in the call stack of the l2cap_channel_event handler, then don't + // re-enable receiving yet (as we need to complete the rest of IRQ handler first). + if (!chan->irq_in_progress) { + // We've already given the channel a new mbuf in l2cap_channel_event above, so + // re-use that mbuf in the call to ble_l2cap_recv_ready. This will just + // give the channel more credits. + struct os_mbuf *sdu_rx = chan->chan->coc_rx.sdu; + assert(sdu_rx); + if (sdu_rx) { + ble_l2cap_recv_ready(chan->chan, sdu_rx); + } } } else { // Trim the used bytes from the start of the mbuf. From b9a35bebf75be53a817bf6341af14b882093e345 Mon Sep 17 00:00:00 2001 From: stijn Date: Sat, 16 Jan 2021 09:18:31 +0100 Subject: [PATCH 0545/3533] py/qstr.h: Remove QSTR_FROM_STR_STATIC macro. It practically does the same as qstr_from_str and was only used in one place, which should actually use the compile-time MP_QSTR_XXX form for consistency; qstr_from_str is for runtime strings only. --- ports/unix/main.c | 10 +++++----- py/qstr.h | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index 6f85cbf8d021d..af4328a4db759 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -532,8 +532,8 @@ MP_NOINLINE int main_(int argc, char **argv) { { MP_DECLARE_CONST_FUN_OBJ_0(extra_coverage_obj); MP_DECLARE_CONST_FUN_OBJ_0(extra_cpp_coverage_obj); - mp_store_global(QSTR_FROM_STR_STATIC("extra_coverage"), MP_OBJ_FROM_PTR(&extra_coverage_obj)); - mp_store_global(QSTR_FROM_STR_STATIC("extra_cpp_coverage"), MP_OBJ_FROM_PTR(&extra_cpp_coverage_obj)); + mp_store_global(MP_QSTR_extra_coverage, MP_OBJ_FROM_PTR(&extra_coverage_obj)); + mp_store_global(MP_QSTR_extra_cpp_coverage, MP_OBJ_FROM_PTR(&extra_cpp_coverage_obj)); } #endif @@ -546,9 +546,9 @@ MP_NOINLINE int main_(int argc, char **argv) { // test_obj.attr = 42 // // mp_obj_t test_class_type, test_class_instance; - // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0)); - // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type)); - // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42)); + // test_class_type = mp_obj_new_type(qstr_from_str("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0)); + // mp_store_name(qstr_from_str("test_obj"), test_class_instance = mp_call_function_0(test_class_type)); + // mp_store_attr(test_class_instance, qstr_from_str("attr"), mp_obj_new_int(42)); /* printf("bytes:\n"); diff --git a/py/qstr.h b/py/qstr.h index df87217ff323e..0b6fb12b084a0 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -32,7 +32,7 @@ // See qstrdefs.h for a list of qstr's that are available as constants. // Reference them as MP_QSTR_xxxx. // -// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx") +// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str("xxx") // for qstrs that are referenced this way, but you don't want to have them in ROM. // first entry in enum will be MP_QSTRnull=0, which indicates invalid/no qstr @@ -55,7 +55,6 @@ typedef struct _qstr_pool_t { const byte *qstrs[]; } qstr_pool_t; -#define QSTR_FROM_STR_STATIC(s) (qstr_from_strn((s), strlen(s))) #define QSTR_TOTAL() (MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len) void qstr_init(void); From fca2730ea01bc8457804884f4a0472ae0d938a19 Mon Sep 17 00:00:00 2001 From: stijn Date: Sat, 16 Jan 2021 09:24:22 +0100 Subject: [PATCH 0546/3533] lib/utils/pyexec: Remove obsolete LCD initialization. This was added a long time ago in 75abee206d1a575aa98a486d043c94d64df432c1 when USB host support was added to the stm (now stm32) port, and when this pyexec code was actually part of the stm port. It's unlikely to work as intended anymore. If it is needed in the future then generic hook macros can be added in pyexec. --- lib/utils/pyexec.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 86321ac12909f..d1e955d65b9a2 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -546,12 +546,6 @@ int pyexec_friendly_repl(void) { vstr_t line; vstr_init(&line, 32); - #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD - // in host mode, we enable the LCD for the repl - mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD"))); - mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true); - #endif - friendly_repl_reset: mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n"); #if MICROPY_PY_BUILTINS_HELP From d48860c7ddfb37bdc48d0f3afc3a26dcd54cca0f Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 19 Jan 2021 13:07:51 +0100 Subject: [PATCH 0547/3533] tools/verifygitlog.py: Add script for verifying commit message format. The main rules enforced are: - At most 72 characters in the subject line, with a ": " in it. - At most 75 characters per line in the body. - No "noreply" email addresses. --- tools/verifygitlog.py | 122 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100755 tools/verifygitlog.py diff --git a/tools/verifygitlog.py b/tools/verifygitlog.py new file mode 100755 index 0000000000000..0080b96bfaea9 --- /dev/null +++ b/tools/verifygitlog.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 + +import re +import subprocess +import sys + +verbosity = 0 # Show what's going on, 0 1 or 2. +suggestions = 1 # Set to 0 to not include lengthy suggestions in error messages. + + +def verbose(*args): + if verbosity: + print(*args) + + +def very_verbose(*args): + if verbosity > 1: + print(*args) + + +def git_log(pretty_format, *args): + # Delete pretty argument from user args so it doesn't interfere with what we do. + args = ["git", "log"] + [arg for arg in args if "--pretty" not in args] + args.append("--pretty=format:" + pretty_format) + very_verbose("git_log", *args) + # Generator yielding each output line. + for line in subprocess.Popen(args, stdout=subprocess.PIPE).stdout: + yield line.decode().rstrip("\r\n") + + +def verify(sha): + verbose("verify", sha) + errors = [] + warnings = [] + + def error_text(err): + return "commit " + sha + ": " + err + + def error(err): + errors.append(error_text(err)) + + def warning(err): + warnings.append(error_text(err)) + + # Author and committer email. + for line in git_log("%ae%n%ce", sha, "-n1"): + very_verbose("email", line) + if "noreply" in line: + error("Unwanted email address: " + line) + + # Message body. + raw_body = list(git_log("%B", sha, "-n1")) + if not raw_body: + error("Message is empty") + return errors, warnings + + # Subject line. + subject_line = raw_body[0] + very_verbose("subject_line", subject_line) + if not re.match(r"^[^!]+: [A-Z]+.+ .+\.$", subject_line): + error("Subject line should contain ': ' and end in '.': " + subject_line) + if len(subject_line) >= 73: + error("Subject line should be 72 or less characters: " + subject_line) + + # Second one divides subject and body. + if len(raw_body) > 1 and raw_body[1]: + error("Second message line should be empty: " + raw_body[1]) + + # Message body lines. + for line in raw_body[2:]: + if len(line) >= 76: + error("Message lines should be 75 or less characters: " + line) + + if not raw_body[-1].startswith("Signed-off-by: ") or "@" not in raw_body[-1]: + warning("Message should be signed-off") + + return errors, warnings + + +def run(args): + verbose("run", *args) + has_errors = False + has_warnings = False + for sha in git_log("%h", *args): + errors, warnings = verify(sha) + has_errors |= any(errors) + has_warnings |= any(warnings) + for err in errors: + print("error:", err) + for err in warnings: + print("warning:", err) + if has_errors or has_warnings: + if suggestions: + print("See https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md") + else: + print("ok") + if has_errors: + sys.exit(1) + + +def show_help(): + print("usage: verifygitlog.py [-v -n -h] ...") + print("-v : increase verbosity, can be speficied multiple times") + print("-n : do not print multi-line suggestions") + print("-h : print this help message and exit") + print("... : arguments passed to git log to retrieve commits to verify") + print(" see https://www.git-scm.com/docs/git-log") + print(" passing no arguments at all will verify all commits") + print("examples:") + print("verifygitlog.py -n10 # Check last 10 commits") + print("verifygitlog.py -v master..HEAD # Check commits since master") + + +if __name__ == "__main__": + args = sys.argv[1:] + verbosity = args.count("-v") + suggestions = args.count("-n") == 0 + if "-h" in args: + show_help() + else: + args = [arg for arg in args if arg not in ["-v", "-n", "-h"]] + run(args) From 37c2f507a01af06627ff93eafa4fc80b2734620e Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 19 Jan 2021 15:06:50 +0100 Subject: [PATCH 0548/3533] github/workflows: Add workflow to verify commit message format. Using the new tools/verifygitlog.py script. --- .github/workflows/commit_formatting.yml | 14 ++++++++++++++ tools/ci.sh | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .github/workflows/commit_formatting.yml diff --git a/.github/workflows/commit_formatting.yml b/.github/workflows/commit_formatting.yml new file mode 100644 index 0000000000000..5f96fbb934426 --- /dev/null +++ b/.github/workflows/commit_formatting.yml @@ -0,0 +1,14 @@ +name: Check commit message formatting + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: '100' + - uses: actions/setup-python@v1 + - name: Check commit message formatting + run: source tools/ci.sh && ci_commit_formatting_run diff --git a/tools/ci.sh b/tools/ci.sh index 4e4aad5600e77..c6b641dae96dc 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -29,6 +29,16 @@ function ci_code_formatting_run { tools/codeformat.py -v } +######################################################################################## +# commit formatting + +function ci_commit_formatting_run { + git remote add upstream https://github.com/micropython/micropython.git + git fetch --depth=100 upstream master + # For a PR, upstream/master..HEAD ends with a merge commit into master, exlude that one. + tools/verifygitlog.py -v upstream/master..HEAD --no-merges +} + ######################################################################################## # code size From 499e199addacd3777244c61cc4c3b4efdfa9b300 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Tue, 19 Jan 2021 09:22:04 +0000 Subject: [PATCH 0549/3533] docs,stm32: Fix minor typos in RTC docs, and->an. --- docs/library/machine.RTC.rst | 2 +- docs/library/pyb.RTC.rst | 2 +- ports/stm32/rtc.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/library/machine.RTC.rst b/docs/library/machine.RTC.rst index d5a4f390b512b..ae5446ef74fee 100644 --- a/docs/library/machine.RTC.rst +++ b/docs/library/machine.RTC.rst @@ -4,7 +4,7 @@ class RTC -- real time clock ============================ -The RTC is and independent clock that keeps track of the date +The RTC is an independent clock that keeps track of the date and time. Example usage:: diff --git a/docs/library/pyb.RTC.rst b/docs/library/pyb.RTC.rst index 4c736597cf8db..c477e7f035c87 100644 --- a/docs/library/pyb.RTC.rst +++ b/docs/library/pyb.RTC.rst @@ -4,7 +4,7 @@ class RTC -- real time clock ============================ -The RTC is and independent clock that keeps track of the date +The RTC is an independent clock that keeps track of the date and time. Example usage:: diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index bd898d4558570..02b0f2dbd1364 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -43,7 +43,7 @@ /// \moduleref pyb /// \class RTC - real time clock /// -/// The RTC is and independent clock that keeps track of the date +/// The RTC is an independent clock that keeps track of the date /// and time. /// /// Example usage: From 2aecf378be3190920fd0e50aff6ccdc83ecfd7e9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 21 Jan 2021 10:09:02 +1100 Subject: [PATCH 0550/3533] tools/makemanifest.py: Add check that freeze path is a directory. Avoids accidentally writing freeze("path/to/file.py") and getting unexpected results. --- tools/makemanifest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 9ef03682607dc..c07a3a6c77ab2 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -172,6 +172,8 @@ def mkdir(filename): def freeze_internal(kind, path, script, opt): path = convert_path(path) + if not os.path.isdir(path): + raise FreezeError("freeze path must be a directory") if script is None and kind == KIND_AS_STR: if any(f[0] == KIND_AS_STR for f in manifest_list): raise FreezeError("can only freeze one str directory") From 993ab6aa2c2e8eadcfdf64371bfddaa8304f26cb Mon Sep 17 00:00:00 2001 From: Chris Hemingway Date: Sun, 24 Jan 2021 14:40:44 +0000 Subject: [PATCH 0551/3533] nrf/README: Add use of "make submodules" in alternative build paragraph. Add "make submodules" to commands when building for the first time. Otherwise, on a first time build, the submodules have not been checked out and a lot of `fatal error: nrfx.h: No such file or directory` errors are printed. --- ports/nrf/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/nrf/README.md b/ports/nrf/README.md index 22bb4af514a39..0341cd81cde05 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -62,8 +62,9 @@ By default, the PCA10040 (nrf52832) is used as compile target. To build and flas Alternatively the target board could be defined: - make BOARD=pca10040 - make BOARD=pca10040 deploy + make submodules + make BOARD=pca10040 + make BOARD=pca10040 deploy ## Compile without LTO enabled From 5c37e76e4f27cf3553adb67947c0a8e0a5500591 Mon Sep 17 00:00:00 2001 From: Christopher Tse Date: Tue, 26 Jan 2021 03:28:20 -0600 Subject: [PATCH 0552/3533] esp8266/modules/neopixel.py: Add timing param to NeoPixel constructor. This matches the esp32 port. --- ports/esp8266/modules/neopixel.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/esp8266/modules/neopixel.py b/ports/esp8266/modules/neopixel.py index be56ed1b07266..501a2689e79f4 100644 --- a/ports/esp8266/modules/neopixel.py +++ b/ports/esp8266/modules/neopixel.py @@ -7,12 +7,13 @@ class NeoPixel: ORDER = (1, 0, 2, 3) - def __init__(self, pin, n, bpp=3): + def __init__(self, pin, n, bpp=3, timing=1): self.pin = pin self.n = n self.bpp = bpp self.buf = bytearray(n * bpp) self.pin.init(pin.OUT) + self.timing = timing def __setitem__(self, index, val): offset = index * self.bpp @@ -28,4 +29,4 @@ def fill(self, color): self[i] = color def write(self): - neopixel_write(self.pin, self.buf, True) + neopixel_write(self.pin, self.buf, self.timing) From ddb53c9458381f71d896ebba60234716f1b156d9 Mon Sep 17 00:00:00 2001 From: Christopher Tse Date: Tue, 26 Jan 2021 22:52:55 -0600 Subject: [PATCH 0553/3533] docs/esp8266/quickref: Add warning block about NeoPixel timing. --- docs/esp8266/quickref.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index 1a22bd50a5ac2..a478b66581d93 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -364,6 +364,13 @@ For low-level driving of a NeoPixel:: import esp esp.neopixel_write(pin, grb_buf, is800khz) +.. Warning:: + By default ``NeoPixel`` is configured to control the more popular *800kHz* + units. It is possible to use alternative timing to control other (typically + 400kHz) devices by passing ``timing=0`` when constructing the + ``NeoPixel`` object. + + APA102 driver ------------- From c2b5bfcc0c6648f549af215f3d2f47302e962c96 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 27 Jan 2021 10:14:50 +0100 Subject: [PATCH 0554/3533] tools: Remove obsolete upip bootstrap script. The upip module is frozen into ports supporting it, and it is included in the source tree, so there is no need to get it from PyPi. Moreover the PyPi package referred to is an out-of-date version of upip which is basically unrelated to our upip.py because the source is taken from a fork of micropython-lib instead of this repository. --- tools/bootstrap_upip.sh | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100755 tools/bootstrap_upip.sh diff --git a/tools/bootstrap_upip.sh b/tools/bootstrap_upip.sh deleted file mode 100755 index 2891775d7deac..0000000000000 --- a/tools/bootstrap_upip.sh +++ /dev/null @@ -1,30 +0,0 @@ -# This script performs bootstrap installation of upip package manager from PyPI -# All the other packages can be installed using it. - -saved="$PWD" - -if [ "$1" = "" ]; then - dest=~/.micropython/lib/ -else - dest="$1" -fi - -if [ -z "$TMPDIR" ]; then - cd /tmp -else - cd $TMPDIR -fi - -# Remove any stale old version -rm -rf micropython-upip-* -wget -nd -rH -l1 -D files.pythonhosted.org https://pypi.org/project/micropython-upip/ --reject=html - -tar xfz micropython-upip-*.tar.gz -tmpd="$PWD" - -cd "$saved" -mkdir -p "$dest" -cp "$tmpd"/micropython-upip-*/upip*.py "$dest" - -echo "upip is installed. To use:" -echo "micropython -m upip --help" From cb8e2f02ab34b49e5bd42012f3bc9adf61607b25 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 27 Jan 2021 15:44:53 +0100 Subject: [PATCH 0555/3533] py/gc: Fix debug printing of pointer. When DEBUG_printf is the standard printf, compilers require the value for %p to be an actual pointer instead of an integer. --- py/gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/gc.c b/py/gc.c index 767f1b81c4dfa..41bbaa1b57f3c 100644 --- a/py/gc.c +++ b/py/gc.c @@ -294,7 +294,7 @@ STATIC void gc_sweep(void) { } #endif free_tail = 1; - DEBUG_printf("gc_sweep(%p)\n", PTR_FROM_BLOCK(block)); + DEBUG_printf("gc_sweep(%p)\n", (void *)PTR_FROM_BLOCK(block)); #if MICROPY_PY_GC_COLLECT_RETVAL MP_STATE_MEM(gc_collected)++; #endif From cb30928ac8da931f905d1c14468498d21108933a Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Sat, 16 Jan 2021 20:48:19 +0000 Subject: [PATCH 0556/3533] py/persistentcode: Introduce MICROPY_PERSISTENT_CODE_SAVE_FILE option. This should be enabled when the mp_raw_code_save_file function is needed. It is enabled for mpy-cross, and a check for defined(__APPLE__) is added to cover Mac M1 systems. --- mpy-cross/mpconfigport.h | 8 ++++++++ py/mpconfig.h | 5 +++++ py/persistentcode.c | 9 ++------- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mpy-cross/mpconfigport.h b/mpy-cross/mpconfigport.h index 21d3e12ed43d7..7ff3d1b6b4b09 100644 --- a/mpy-cross/mpconfigport.h +++ b/mpy-cross/mpconfigport.h @@ -30,6 +30,14 @@ #define MICROPY_PERSISTENT_CODE_LOAD (0) #define MICROPY_PERSISTENT_CODE_SAVE (1) +#ifndef MICROPY_PERSISTENT_CODE_SAVE_FILE +#if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) || defined(__unix__) || defined(__APPLE__) +#define MICROPY_PERSISTENT_CODE_SAVE_FILE (1) +#else +#define MICROPY_PERSISTENT_CODE_SAVE_FILE (0) +#endif +#endif + #define MICROPY_EMIT_X64 (1) #define MICROPY_EMIT_X86 (1) #define MICROPY_EMIT_THUMB (1) diff --git a/py/mpconfig.h b/py/mpconfig.h index 1df2e8fe918ef..518bddd6c8972 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -283,6 +283,11 @@ #define MICROPY_PERSISTENT_CODE_SAVE (0) #endif +// Whether to support saving persistent code to a file via mp_raw_code_save_file +#ifndef MICROPY_PERSISTENT_CODE_SAVE_FILE +#define MICROPY_PERSISTENT_CODE_SAVE_FILE (0) +#endif + // Whether generated code can persist independently of the VM/runtime instance // This is enabled automatically when needed by other features #ifndef MICROPY_PERSISTENT_CODE diff --git a/py/persistentcode.c b/py/persistentcode.c index da3234a5fe9cd..084632a60fdc1 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -825,10 +825,7 @@ void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) { save_raw_code(print, rc, &qw); } -// here we define mp_raw_code_save_file depending on the port -// TODO abstract this away properly - -#if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) || defined(__unix__) +#if MICROPY_PERSISTENT_CODE_SAVE_FILE #include #include @@ -853,8 +850,6 @@ void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) { MP_THREAD_GIL_ENTER(); } -#else -#error mp_raw_code_save_file not implemented for this platform -#endif +#endif // MICROPY_PERSISTENT_CODE_SAVE_FILE #endif // MICROPY_PERSISTENT_CODE_SAVE From 407df82f813d7a8d522b5b43e39f91816dea26a6 Mon Sep 17 00:00:00 2001 From: Samuelson <59143578+TheNameIsSamSamuelson@users.noreply.github.com> Date: Sat, 9 Jan 2021 11:33:07 -0300 Subject: [PATCH 0557/3533] docs/develop/natmod: Fix a small typo, con->can. --- docs/develop/natmod.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/develop/natmod.rst b/docs/develop/natmod.rst index a4c188719a4d9..8ffe49591cc69 100644 --- a/docs/develop/natmod.rst +++ b/docs/develop/natmod.rst @@ -202,7 +202,7 @@ Module usage in MicroPython Once the module is built there should be a file called ``factorial.mpy``. Copy this so it is accessible on the filesystem of your MicroPython system and can be -found in the import path. The module con now be accessed in Python just like any +found in the import path. The module can now be accessed in Python just like any other module, for example:: import factorial From ef9fde7339b12e17596966afb2b3d641ca5fcff2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 31 Jan 2021 23:17:42 +1100 Subject: [PATCH 0558/3533] LICENSE,docs: Update copyright year range to include 2021. Signed-off-by: Damien George --- LICENSE | 2 +- docs/conf.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 8c5e4fe4c359a..3193eb8cec189 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013-2020 Damien P. George +Copyright (c) 2013-2021 Damien P. George Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/docs/conf.py b/docs/conf.py index 460886b4da2e8..4ca4d18f502e9 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -66,7 +66,7 @@ # General information about the project. project = 'MicroPython' -copyright = '2014-2020, Damien P. George, Paul Sokolovsky, and contributors' +copyright = '2014-2021, Damien P. George, Paul Sokolovsky, and contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the From 4fb5f012c32a2ed394091039a7213472001a98c9 Mon Sep 17 00:00:00 2001 From: iTitou Date: Wed, 20 Jan 2021 10:33:40 +0100 Subject: [PATCH 0559/3533] py/makeversionhdr: Honor SOURCE_DATE_EPOCH if present. This environment variable, if defined during the build process, indicates a fixed time that should be used in place of "now" when such a time is explicitely referenced. This allows for reproducible builds of micropython. See https://reproducible-builds.org/specs/source-date-epoch/ Signed-off-by: iTitou --- py/makeversionhdr.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/py/makeversionhdr.py b/py/makeversionhdr.py index 970a86e6ca3ab..2f4bc9182664e 100644 --- a/py/makeversionhdr.py +++ b/py/makeversionhdr.py @@ -80,6 +80,12 @@ def make_version_header(filename): git_tag, git_hash = info + build_date = datetime.date.today() + if "SOURCE_DATE_EPOCH" in os.environ: + build_date = datetime.datetime.utcfromtimestamp( + int(os.environ["SOURCE_DATE_EPOCH"]) + ).date() + # Generate the file with the git and version info file_data = """\ // This file was generated by py/makeversionhdr.py @@ -89,7 +95,7 @@ def make_version_header(filename): """ % ( git_tag, git_hash, - datetime.date.today().strftime("%Y-%m-%d"), + build_date.strftime("%Y-%m-%d"), ) # Check if the file contents changed from last time From b8f5f5cd85e83fd136fac5e90ed5d486c2924a42 Mon Sep 17 00:00:00 2001 From: iTitou Date: Wed, 20 Jan 2021 12:13:04 +0100 Subject: [PATCH 0560/3533] github/workflows/ports_unix.yml: Add job for a reproducible build. With a check for reproducible build date. Invocation of the test suite is not needed because it's already run in another job. Signed-off-by: iTitou --- .github/workflows/ports_unix.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 552fd72d8440e..eb1416045c03a 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -26,6 +26,17 @@ jobs: if: failure() run: tests/run-tests --print-failures + reproducible: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build with reproducible date + run: source tools/ci.sh && ci_unix_minimal_build + env: + SOURCE_DATE_EPOCH: 1234567890 + - name: Check reproducible build date + run: echo | ports/unix/micropython-minimal -i | grep 'on 2009-02-13;' + standard: runs-on: ubuntu-latest steps: From 35a6f6231e482936b7ef7e8fe3dfc790a7beb4ed Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Feb 2021 18:44:28 +1100 Subject: [PATCH 0561/3533] tests/extmod/utime_time_ns.py: Relax bounds on time_ns measurement. Some devices have lower precision than 1ms for time_ns() (eg PYBv1.x has 3.9ms resolution of the RTC) so make the test more lenient for them. Signed-off-by: Damien George --- tests/extmod/utime_time_ns.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/extmod/utime_time_ns.py b/tests/extmod/utime_time_ns.py index 8f3890f1cb85e..0d13f839d4f2d 100644 --- a/tests/extmod/utime_time_ns.py +++ b/tests/extmod/utime_time_ns.py @@ -11,14 +11,14 @@ t0 = utime.time_ns() -utime.sleep_us(1000) +utime.sleep_us(5000) t1 = utime.time_ns() # Check that time_ns increases. print(t0 < t1) -# Check that time_ns counts correctly, but be very lenient with the upper bound (50ms). -if 950000 < t1 - t0 < 50000000: +# Check that time_ns counts correctly, but be very lenient with the bounds (2ms to 50ms). +if 2000000 < t1 - t0 < 50000000: print(True) else: print(t0, t1, t1 - t0) From ffded488109230dc488b295e19eff7a0055ed0fa Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Feb 2021 22:30:50 +1100 Subject: [PATCH 0562/3533] zephyr/machine_uart: Fix arg of machine_uart_ioctl to make it uintptr_t. Signed-off-by: Damien George --- ports/zephyr/machine_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 6ae8707d7f130..23d7d59443729 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -131,7 +131,7 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin return size; } -STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { +STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { mp_uint_t ret; if (request == MP_STREAM_POLL) { From c9210a65dfce0771186916a1d43cbf63133773ba Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 1 Feb 2021 18:21:12 +0000 Subject: [PATCH 0563/3533] rp2/machine_pin: Change N_GPIOS to NUM_BANK0_GPIOS for pico-sdk compat. This fixes machine_pin.c to build against the new pico-sdk coming down the pipeline, whilst still working with the existing version. --- ports/rp2/machine_pin.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c index f798c10fdf7ac..0525e0f9f68bd 100644 --- a/ports/rp2/machine_pin.c +++ b/ports/rp2/machine_pin.c @@ -71,7 +71,7 @@ typedef struct _machine_pin_irq_obj_t { STATIC const mp_irq_methods_t machine_pin_irq_methods; -STATIC const machine_pin_obj_t machine_pin_obj[N_GPIOS] = { +STATIC const machine_pin_obj_t machine_pin_obj[NUM_BANK0_GPIOS] = { {{&machine_pin_type}, 0}, {{&machine_pin_type}, 1}, {{&machine_pin_type}, 2}, @@ -134,7 +134,7 @@ void machine_pin_init(void) { } void machine_pin_deinit(void) { - for (int i = 0; i < N_GPIOS; ++i) { + for (int i = 0; i < NUM_BANK0_GPIOS; ++i) { gpio_set_irq_enabled(i, GPIO_IRQ_ALL, false); } irq_set_enabled(IO_IRQ_BANK0, false); From 52d3ae707d7ddb8cf2c06962923226221d281206 Mon Sep 17 00:00:00 2001 From: graham sanderson Date: Mon, 1 Feb 2021 15:48:03 -0600 Subject: [PATCH 0564/3533] rp2/memmap_mp.ld: Update for latest SDK. --- ports/rp2/memmap_mp.ld | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ports/rp2/memmap_mp.ld b/ports/rp2/memmap_mp.ld index 5cebd33466a49..0dc96743ea5cb 100644 --- a/ports/rp2/memmap_mp.ld +++ b/ports/rp2/memmap_mp.ld @@ -35,7 +35,7 @@ SECTIONS { /* Second stage bootloader is prepended to the image. It must be 256 bytes big and checksummed. It is usually built by the boot_stage2 target - in the Pico SDK + in the Raspberry Pi Pico SDK */ .flash_begin : { @@ -59,12 +59,11 @@ SECTIONS */ .text : { - __reset_start = .; - KEEP (*(.reset)) - . = ALIGN(256); - __reset_end = .; - ASSERT(__reset_end - __reset_start == 256, "ERROR: reset section should only be 256 bytes"); + __logical_binary_start = .; KEEP (*(.vectors)) + KEEP (*(.binary_info_header)) + __binary_info_header_end = .; + KEEP (*(.reset)) /* TODO revisit this now memset/memcpy/float in ROM */ /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from * FLASH ... we will include any thing excluded here in .data below by default */ @@ -247,6 +246,8 @@ SECTIONS /* Check if data + heap + stack exceeds RAM limit */ ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed") + + ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary") /* todo assert on extra code */ } From 9b277d981528173f59daab196873a8c11aa17e26 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Feb 2021 10:56:45 +1100 Subject: [PATCH 0565/3533] lib/pico-sdk: Update to latest version v1.0.1. In particular it fixes GPIO19 so that it can be used as an output. Signed-off-by: Damien George --- lib/pico-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pico-sdk b/lib/pico-sdk index 493ed000ddfc2..2d5789eca8965 160000 --- a/lib/pico-sdk +++ b/lib/pico-sdk @@ -1 +1 @@ -Subproject commit 493ed000ddfc21aa1db933f5477dba52ac841aac +Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72 From 7a9027fd5d64868322b9a82b0ac47ac0eba0167d Mon Sep 17 00:00:00 2001 From: Tim Radvan Date: Sat, 30 Jan 2021 21:48:53 +0000 Subject: [PATCH 0566/3533] rp2/rp2_pio: Add JMP PIN support for PIO. PIO state machines can make a conditional jump on the state of a pin: the `JMP PIN` command. This requires the pin to be configured with `sm_config_set_jmp_pin`, but until now we didn't have a way of doing that in MicroPython. This commit adds a new `jmp_pin=None` argument to `StateMachine`. If it is not `None` then we try to interpret it as a Pin, and pass its value to `sm_config_set_jmp_pin`. Signed-off-by: Tim Radvan --- ports/rp2/rp2_pio.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index c8542127c03ba..7d8921d0c3a51 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -422,13 +422,14 @@ STATIC void rp2_state_machine_print(const mp_print_t *print, mp_obj_t self_in, m } // StateMachine.init(prog, freq=-1, *, -// in_base=None, out_base=None, set_base=None, sideset_base=None, -// in_shiftdir=None, out_shiftdir=None, push_thresh=None, pull_thresh=None, +// in_base=None, out_base=None, set_base=None, jmp_pin=None, +// sideset_base=None, in_shiftdir=None, out_shiftdir=None, +// push_thresh=None, pull_thresh=None, // ) STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_prog, ARG_freq, - ARG_in_base, ARG_out_base, ARG_set_base, ARG_sideset_base, + ARG_in_base, ARG_out_base, ARG_set_base, ARG_jmp_pin, ARG_sideset_base, ARG_in_shiftdir, ARG_out_shiftdir, ARG_push_thresh, ARG_pull_thresh }; static const mp_arg_t allowed_args[] = { @@ -437,6 +438,7 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel { MP_QSTR_in_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_out_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_set_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_jmp_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_sideset_base, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_in_shiftdir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_out_shiftdir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, @@ -501,6 +503,11 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel sm_config_set_set_pins(&config, set_config.base, set_config.count); } + // Configure jmp pin, if needed. + if (args[ARG_jmp_pin].u_obj != mp_const_none) { + sm_config_set_jmp_pin(&config, mp_hal_get_pin_obj(args[ARG_jmp_pin].u_obj)); + } + // Configure sideset pin, if needed. asm_pio_config_t sideset_config = ASM_PIO_CONFIG_DEFAULT; asm_pio_get_pins("sideset", prog[PROG_SIDESET_PINS], args[ARG_sideset_base].u_obj, &sideset_config); From 3ea05e499d52beaecf4c9c54a67510ac031fe27b Mon Sep 17 00:00:00 2001 From: Tim Radvan Date: Mon, 1 Feb 2021 20:07:19 +0000 Subject: [PATCH 0567/3533] examples/rp2: Add pio_uart_rx.py example. This was adapted from the `pio/uart_rx` example from the `pico-examples` repository: https://github.com/raspberrypi/pico-examples/blob/master/pio/uart_rx/uart_rx.pio It demonstrates the `jmp_pin` feature in action. Signed-off-by: Tim Radvan --- examples/rp2/pio_uart_rx.py | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 examples/rp2/pio_uart_rx.py diff --git a/examples/rp2/pio_uart_rx.py b/examples/rp2/pio_uart_rx.py new file mode 100644 index 0000000000000..41dfde3dad433 --- /dev/null +++ b/examples/rp2/pio_uart_rx.py @@ -0,0 +1,104 @@ +# Example using PIO to create a UART RX interface. +# +# To make it work you'll need a wire connecting GPIO4 and GPIO3. +# +# Demonstrates: +# - PIO shifting in data on a pin +# - PIO jmp(pin) instruction +# - PIO irq handler +# - using the second core via _thread + +import _thread +from machine import Pin, UART +from rp2 import PIO, StateMachine, asm_pio + +UART_BAUD = 9600 + +HARD_UART_TX_PIN = Pin(4, Pin.OUT) +PIO_RX_PIN = Pin(3, Pin.IN, Pin.PULL_UP) + + +@asm_pio( + autopush=True, + push_thresh=8, + in_shiftdir=rp2.PIO.SHIFT_RIGHT, +) +def uart_rx_mini(): + # fmt: off + # Wait for start bit + wait(0, pin, 0) + # Preload bit counter, delay until eye of first data bit + set(x, 7) [10] + # Loop 8 times + label("bitloop") + # Sample data + in_(pins, 1) + # Each iteration is 8 cycles + jmp(x_dec, "bitloop") [6] + # fmt: on + + +@asm_pio( + in_shiftdir=rp2.PIO.SHIFT_RIGHT, +) +def uart_rx(): + # fmt: off + label("start") + # Stall until start bit is asserted + wait(0, pin, 0) + # Preload bit counter, then delay until halfway through + # the first data bit (12 cycles incl wait, set). + set(x, 7) [10] + label("bitloop") + # Shift data bit into ISR + in_(pins, 1) + # Loop 8 times, each loop iteration is 8 cycles + jmp(x_dec, "bitloop") [6] + # Check stop bit (should be high) + jmp(pin, "good_stop") + # Either a framing error or a break. Set a sticky flag + # and wait for line to return to idle state. + irq(block, 4) + wait(1, pin, 0) + # Don't push data if we didn't see good framing. + jmp("start") + # No delay before returning to start; a little slack is + # important in case the TX clock is slightly too fast. + label("good_stop") + push(block) + # fmt: on + + +# The handler for a UART break detected by the PIO. +def handler(sm): + print("break", time.ticks_ms(), end=" ") + + +# Function for core1 to execute to write to the given UART. +def core1_task(uart, text): + uart.write(text) + + +# Set up the hard UART we're going to use to print characters. +uart = UART(1, UART_BAUD, tx=HARD_UART_TX_PIN) + +for pio_prog in ("uart_rx_mini", "uart_rx"): + # Set up the state machine we're going to use to receive the characters. + sm = StateMachine( + 0, + globals()[pio_prog], + freq=8 * UART_BAUD, + in_base=PIO_RX_PIN, # For WAIT, IN + jmp_pin=PIO_RX_PIN, # For JMP + ) + sm.irq(handler) + sm.active(1) + + # Tell core 1 to print some text to UART 1 + text = "Hello, world from PIO, using {}!".format(pio_prog) + _thread.start_new_thread(core1_task, (uart, text)) + + # Echo characters received from PIO to the console. + for i in range(len(text)): + print(chr(sm.get() >> 24), end="") + print() From 7be1f7790278d2c40e7a828e340c43f257c7d748 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Feb 2021 12:09:33 +1100 Subject: [PATCH 0568/3533] stm32/usbd_cdc_interface: Don't wait in usbd_cdc_tx_always if suspended. MCUs with device-only USB peripherals (eg L0, WB) do not implement (at least not in the ST HAL) the HAL_PCD_DisconnectCallback event. So if a USB cable is disconnected the USB driver does not deinitialise itself (usbd_cdc_deinit is not called) and the CDC driver can stay in the USBD_CDC_CONNECT_STATE_CONNECTED state. Then if the USB was attached to the REPL, output can become very slow waiting in usbd_cdc_tx_always for 500ms for each character. The disconnect event is not implemented on these MCUs but the suspend event is. And in the situation where the USB cable is disconnected the suspend event is raised because SOF packets are no longer received. The issue of very slow output on these MCUs is fixed in this commit (really worked around) by adding a check in usbd_cdc_tx_always to see if the USB device state is suspended, and, if so, breaking out of the 500ms wait loop. This should also help all MCUs for a real USB suspend. A proper fix for MCUs with device-only USB would be to implement or somehow synthesise the HAL_PCD_DisconnectCallback event. See issue #6672. Signed-off-by: Damien George --- ports/stm32/usbd_cdc_interface.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index a8261a958aa09..a465f608a655e 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -382,6 +382,10 @@ void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { uint32_t start = HAL_GetTick(); while (usbd_cdc_tx_buffer_full(cdc) && HAL_GetTick() - start <= 500) { usbd_cdc_try_tx(cdc); + if (cdc->base.usbd->pdev->dev_state == USBD_STATE_SUSPENDED) { + // The USB is suspended so buffer will never be drained; exit loop + break; + } if (query_irq() == IRQ_STATE_DISABLED) { // IRQs disabled so buffer will never be drained; exit loop break; From 5ef71cd16793ab254a1f0e7dd46bb9f0c5809f44 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 2 Feb 2021 15:09:15 +1100 Subject: [PATCH 0569/3533] stm32/mboot: Change debug compiler optimisation from -O0 to -Og. With mboot encrpytion and fsload enabled, the DEBUG build -O0 compiler settings result in mboot no longer fitting in the 32k sector. This commit changes this to -Og which also brings it into line with the regular stm32 build. --- ports/stm32/mboot/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index ad2fda3eee508..0c92442597723 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -92,7 +92,7 @@ LDFLAGS += --gc-sections # Debugging/Optimization ifeq ($(DEBUG), 1) CFLAGS += -g -DPENDSV_DEBUG -COPT = -O0 +COPT = -Og else COPT += -Os -DNDEBUG endif From 03974485016654de09e42737ea875bc601d5ec56 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 14 Jan 2021 13:23:15 +0100 Subject: [PATCH 0570/3533] tests/run-tests: Change default Python command used on Windows. Default to just calling python since that is most commonly available: the official installer or zipfiles from python.org, anaconda, nupkg all result in python being available but not python3. In other words: the default used so far is wrong. Note that os.name is 'posix' when running the python version which comes with Cygwin or MSys2 so they are not affected by this. However of all possible ways to get Python on Windows, only Cygwin provides no python command so update the default way for running tests in the README. --- ports/windows/README.md | 4 ++++ tests/run-tests | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/windows/README.md b/ports/windows/README.md index 8d907d1b7243a..1b904f8f5e60e 100644 --- a/ports/windows/README.md +++ b/ports/windows/README.md @@ -90,6 +90,10 @@ Running the tests This is similar for all ports: cd ../../tests + python ./run-tests + +Though when running on Cygwin and using Cygwin's Python installation you'll need: + python3 ./run-tests Depending on the combination of platform and Python version used it might be diff --git a/tests/run-tests b/tests/run-tests index cb5b5cd0a5676..b733d8a9fc582 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -20,7 +20,7 @@ def base_path(*p): # is of lower version, you can point MICROPY_CPYTHON3 environment var # to the correct executable. if os.name == 'nt': - CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3.exe') + CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python') MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/windows/micropython.exe')) else: CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') From 81a4d96aed7221ea497bb90f135b3fd14ab22c0c Mon Sep 17 00:00:00 2001 From: stijn Date: Sat, 16 Jan 2021 09:01:17 +0100 Subject: [PATCH 0571/3533] windows/msvc: Use same default python command as core. --- ports/windows/msvc/genhdr.targets | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/windows/msvc/genhdr.targets b/ports/windows/msvc/genhdr.targets index 3af0ea263699b..9ea6ed28cb4b8 100644 --- a/ports/windows/msvc/genhdr.targets +++ b/ports/windows/msvc/genhdr.targets @@ -14,6 +14,7 @@ $(PySrcDir)qstrdefs.h $(DestDir)qstrdefscollected.h $(DestDir)qstrdefs.generated.h + $(MICROPY_CPYTHON3) python cl.exe $([System.IO.Path]::Combine(`$(CLToolPath)`, `$(CLToolExe)`)) From 195e7dfa0681bcfbc67cfaa68f863c51dcac95da Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Feb 2021 12:55:11 +1100 Subject: [PATCH 0572/3533] rp2/modmachine: Implement additional functions incl unique_id and idle. Added functions in the machine module are: - unique_id (returns 8 bytes) - soft_reset - idle - lightsleep, deepsleep (not power saving at the moment) - disable_irq, enable_irq - time_pulse_us Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 1 + ports/rp2/main.c | 11 +++++-- ports/rp2/modmachine.c | 65 ++++++++++++++++++++++++++++++++++++++++ ports/rp2/mpconfigport.h | 1 + 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 400cb8480e849..160456c6b1e24 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -143,6 +143,7 @@ target_link_libraries(${MICROPYTHON_TARGET} pico_multicore pico_stdlib_headers pico_stdlib + pico_unique_id tinyusb_device ) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 3e956ae78c4ad..64218f97c5aeb 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -110,9 +110,15 @@ int main(int argc, char **argv) { pyexec_frozen_module("_boot.py"); // Execute user scripts. - pyexec_file_if_exists("boot.py"); + int ret = pyexec_file_if_exists("boot.py"); + if (ret & PYEXEC_FORCED_EXIT) { + goto soft_reset_exit; + } if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { - pyexec_file_if_exists("main.py"); + ret = pyexec_file_if_exists("main.py"); + if (ret & PYEXEC_FORCED_EXIT) { + goto soft_reset_exit; + } } for (;;) { @@ -127,6 +133,7 @@ int main(int argc, char **argv) { } } + soft_reset_exit: mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); rp2_pio_deinit(); machine_pin_deinit(); diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 228e9543c1936..dbaafabe86ede 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -26,18 +26,34 @@ #include "py/runtime.h" #include "py/mphal.h" +#include "lib/utils/pyexec.h" #include "extmod/machine_i2c.h" #include "extmod/machine_mem.h" +#include "extmod/machine_pulse.h" #include "extmod/machine_spi.h" #include "modmachine.h" #include "hardware/clocks.h" #include "hardware/watchdog.h" #include "pico/bootrom.h" +#include "pico/unique_id.h" #define RP2_RESET_PWRON (1) #define RP2_RESET_WDT (3) +STATIC mp_obj_t machine_unique_id(void) { + pico_unique_board_id_t id; + pico_get_unique_board_id(&id); + return mp_obj_new_bytes(id.id, sizeof(id.id)); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); + +STATIC mp_obj_t machine_soft_reset(void) { + pyexec_system_exit = PYEXEC_FORCED_EXIT; + mp_raise_type(&mp_type_SystemExit); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset); + STATIC mp_obj_t machine_reset(void) { watchdog_reboot(0, SRAM_END, 0); for (;;) { @@ -69,12 +85,61 @@ STATIC mp_obj_t machine_freq(void) { } MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); +STATIC mp_obj_t machine_idle(void) { + best_effort_wfe_or_timeout(make_timeout_time_ms(1)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); + +STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + for (;;) { + MICROPY_EVENT_POLL_HOOK + } + } else { + mp_hal_delay_ms(mp_obj_get_int(args[0])); + } + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep); + +STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { + machine_lightsleep(n_args, args); + return machine_reset(); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); + +STATIC mp_obj_t machine_disable_irq(void) { + uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION(); + return mp_obj_new_int(state); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq); + +STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { + uint32_t state = mp_obj_get_int(state_in); + MICROPY_END_ATOMIC_SECTION(state); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, + { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, + { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, + + { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, + { MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, + + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + + { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index d409ccfd4d926..9ba381030b150 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -109,6 +109,7 @@ #define MICROPY_PY_USELECT (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new +#define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) From 0e4458707673bdf04a812ab9cd2ae06712bcdd1a Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 1 Feb 2021 13:40:28 +1100 Subject: [PATCH 0573/3533] docs/library/machine.Pin.rst: Make it clear which methods are not core. Signed-off-by: Damien George --- docs/library/machine.Pin.rst | 58 +++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/docs/library/machine.Pin.rst b/docs/library/machine.Pin.rst index 095240fe1499a..f8e9e1054d65b 100644 --- a/docs/library/machine.Pin.rst +++ b/docs/library/machine.Pin.rst @@ -33,8 +33,8 @@ Usage Model:: # read and print the pin value print(p2.value()) - # reconfigure pin #0 in input mode - p0.mode(p0.IN) + # reconfigure pin #0 in input mode with a pull down resistor + p0.init(p0.IN, p0.PULL_DOWN) # configure an irq callback p0.irq(lambda p:print(p)) @@ -160,25 +160,6 @@ Methods Set pin to "0" output level. -.. method:: Pin.mode([mode]) - - Get or set the pin mode. - See the constructor documentation for details of the ``mode`` argument. - -.. method:: Pin.pull([pull]) - - Get or set the pin pull state. - See the constructor documentation for details of the ``pull`` argument. - -.. method:: Pin.drive([drive]) - - Get or set the pin drive strength. - See the constructor documentation for details of the ``drive`` argument. - - Not all ports implement this method. - - Availability: WiPy. - .. method:: Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), *, priority=1, wake=None, hard=False) Configure an interrupt handler to be called when the trigger source of the @@ -220,6 +201,41 @@ Methods This method returns a callback object. +The following methods are not part of the core Pin API and only implemented on certain ports. + +.. method:: Pin.low() + + Set pin to "0" output level. + + Availability: nrf, rp2, stm32 ports. + +.. method:: Pin.high() + + Set pin to "1" output level. + + Availability: nrf, rp2, stm32 ports. + +.. method:: Pin.mode([mode]) + + Get or set the pin mode. + See the constructor documentation for details of the ``mode`` argument. + + Availability: cc3200, stm32 ports. + +.. method:: Pin.pull([pull]) + + Get or set the pin pull state. + See the constructor documentation for details of the ``pull`` argument. + + Availability: cc3200, stm32 ports. + +.. method:: Pin.drive([drive]) + + Get or set the pin drive strength. + See the constructor documentation for details of the ``drive`` argument. + + Availability: cc3200 port. + Constants --------- From 78b23c3a1f064dc19bbee68930ef3aba110c781c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 00:59:07 +1100 Subject: [PATCH 0574/3533] all: Bump version to 1.14. Signed-off-by: Damien George --- docs/conf.py | 2 +- py/mpconfig.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 4ca4d18f502e9..dd6cc31fb8736 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -74,7 +74,7 @@ # # We don't follow "The short X.Y version" vs "The full version, including alpha/beta/rc tags" # breakdown, so use the same version identifier for both to avoid confusion. -version = release = '1.13' +version = release = '1.14' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/py/mpconfig.h b/py/mpconfig.h index 518bddd6c8972..d9a30cd3005f1 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -28,7 +28,7 @@ // Current version of MicroPython #define MICROPY_VERSION_MAJOR 1 -#define MICROPY_VERSION_MINOR 13 +#define MICROPY_VERSION_MINOR 14 #define MICROPY_VERSION_MICRO 0 // Combined version as a 32-bit number for convenience From 7f7b4f2bc68a06c9f47d6b99e953cac0b21c26c9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 16:29:54 +1100 Subject: [PATCH 0575/3533] rp2/machine_adc: Only initialise the ADC periph if not already enabled. Otherwise it resets the ADC peripheral each time a new ADC object is constructed, which can reset other state that has already been set up. See issue #6833. Signed-off-by: Damien George --- ports/rp2/machine_adc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index f0f367151cab5..f8925e5aa33a6 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -78,7 +78,10 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s } } - adc_init(); + // Initialise the ADC peripheral if it's not already running. + if (!(adc_hw->cs & ADC_CS_EN_BITS)) { + adc_init(); + } if (ADC_IS_VALID_GPIO(channel)) { // Configure the GPIO pin in ADC mode. From 5fdf351178df9a18df624ae0f5947d8a5a6bce40 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 11 Jan 2021 14:10:25 +0800 Subject: [PATCH 0576/3533] py/gc: Don't include mpconfig.h and misc.h in gc.h. Because gc.h doesn't reference any symbol from these header files. Signed-off-by: Xiang Xiao --- ports/esp8266/gccollect.c | 1 + py/gc.h | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ports/esp8266/gccollect.c b/ports/esp8266/gccollect.c index 3618a56644e6f..bca0e030cb0b9 100644 --- a/ports/esp8266/gccollect.c +++ b/ports/esp8266/gccollect.c @@ -26,6 +26,7 @@ #include +#include "py/mpconfig.h" #include "py/gc.h" #include "gccollect.h" diff --git a/py/gc.h b/py/gc.h index 4690d393708f2..f67fb0daad6dc 100644 --- a/py/gc.h +++ b/py/gc.h @@ -26,11 +26,9 @@ #ifndef MICROPY_INCLUDED_PY_GC_H #define MICROPY_INCLUDED_PY_GC_H +#include #include -#include "py/mpconfig.h" -#include "py/misc.h" - void gc_init(void *start, void *end); // These lock/unlock functions can be nested. From 7c4435459261f1ea93577938b1ab281239d159e7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Feb 2021 14:43:24 +1100 Subject: [PATCH 0577/3533] ports: Remove def of MP_PLAT_PRINT_STRN if it's the same as the default. To simplify config, there's no need to specify MP_PLAT_PRINT_STRN if it's the same as the default definition in py/mpconfig.h. Signed-off-by: Damien George --- ports/cc3200/mpconfigport.h | 2 -- ports/esp32/mpconfigport.h | 1 - ports/esp8266/mpconfigport.h | 1 - ports/javascript/mpconfigport.h | 2 -- ports/mimxrt/mpconfigport.h | 1 - ports/nrf/mpconfigport.h | 2 -- ports/pic16bit/mpconfigport.h | 2 -- ports/powerpc/mpconfigport.h | 2 -- ports/samd/mpconfigport.h | 1 - ports/stm32/mpconfigport.h | 2 -- ports/teensy/mpconfigport.h | 2 -- 11 files changed, 18 deletions(-) diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index 6fdb278d864b1..87689c505efc5 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -197,8 +197,6 @@ typedef int32_t mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_off_t; -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - #define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() #define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) #define MICROPY_EVENT_POLL_HOOK __WFI(); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index f170d70708b79..5ad5fa18a3045 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -223,7 +223,6 @@ struct mp_bluetooth_nimble_root_pointers_t; #define BYTES_PER_WORD (4) #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p))) -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) void *esp_native_code_commit(void *, size_t, void *); #define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc) #define MP_SSIZE_MAX (0x7fffffff) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 99cf2ade4dec3..2c56b4188fd07 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -135,7 +135,6 @@ typedef uint32_t sys_prot_t; // for modlwip // ssize_t, off_t as required by POSIX-signatured functions in stream.h #include -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) void *esp_native_code_commit(void *, size_t, void *); #define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc) diff --git a/ports/javascript/mpconfigport.h b/ports/javascript/mpconfigport.h index 0101e10c8d613..d1bfbbd6011e5 100644 --- a/ports/javascript/mpconfigport.h +++ b/ports/javascript/mpconfigport.h @@ -180,8 +180,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned mp_uint_t; // must be pointer size typedef long mp_off_t; -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 344867fb819dd..15a292486ff4e 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -98,7 +98,6 @@ extern const struct _mp_obj_module_t mp_module_utime; } while (0); #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) #define MP_SSIZE_MAX (0x7fffffff) typedef int mp_int_t; // must be pointer size diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index a025feb2ec65a..581e83ef2f8c7 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -332,8 +332,6 @@ extern const struct _mp_obj_module_t ble_module; __WFI(); \ } while (0); -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - // We need to provide a declaration/definition of alloca() #include diff --git a/ports/pic16bit/mpconfigport.h b/ports/pic16bit/mpconfigport.h index f121081e6fe8b..43300d1760283 100644 --- a/ports/pic16bit/mpconfigport.h +++ b/ports/pic16bit/mpconfigport.h @@ -84,8 +84,6 @@ typedef unsigned int mp_uint_t; // must be pointer size typedef int mp_off_t; -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - // extra builtin names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, diff --git a/ports/powerpc/mpconfigport.h b/ports/powerpc/mpconfigport.h index 7a927773626d7..0b868e3dafabf 100644 --- a/ports/powerpc/mpconfigport.h +++ b/ports/powerpc/mpconfigport.h @@ -103,8 +103,6 @@ typedef unsigned long mp_uint_t; // must be pointer size typedef long mp_off_t; -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index ffb30bf5e2188..852d9e9ee52de 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -98,7 +98,6 @@ extern const struct _mp_obj_module_t mp_module_utime; } while (0); #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) #define MP_SSIZE_MAX (0x7fffffff) typedef int mp_int_t; // must be pointer size diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index b6906ef998c2e..a47529d8b3c01 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -360,8 +360,6 @@ typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_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 // 1 machine instruction). // diff --git a/ports/teensy/mpconfigport.h b/ports/teensy/mpconfigport.h index c00da5ed9e498..3acedcf0248c9 100644 --- a/ports/teensy/mpconfigport.h +++ b/ports/teensy/mpconfigport.h @@ -62,8 +62,6 @@ typedef int32_t mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_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 // 1 machine instruction). // From 8a41ee19c22d7bb85fcbd90c9d06b9937a1b8c87 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Feb 2021 15:27:53 +1100 Subject: [PATCH 0578/3533] py: Remove BITS_PER_WORD definition. It's only used in one location, to test if << or >> will overflow when shifting mp_uint_t. For such a test it's clearer to use sizeof(lhs_val), which will be valid even if the type of lhs_val changes. Signed-off-by: Damien George --- py/mpconfig.h | 1 - py/runtime.c | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index d9a30cd3005f1..3c1ed28d806f7 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1538,7 +1538,6 @@ typedef double mp_float_t; #ifndef BITS_PER_BYTE #define BITS_PER_BYTE (8) #endif -#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) // mp_int_t value with most significant bit set #define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1)) diff --git a/py/runtime.c b/py/runtime.c index c12271f4e2cf5..2e2fa1d173351 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -387,7 +387,9 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { if (rhs_val < 0) { // negative shift not allowed mp_raise_ValueError(MP_ERROR_TEXT("negative shift count")); - } else if (rhs_val >= (mp_int_t)BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) { + } else if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * BITS_PER_BYTE) + || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) + || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) { // left-shift will overflow, so use higher precision integer lhs = mp_obj_new_int_from_ll(lhs_val); goto generic_binary_op; @@ -404,10 +406,10 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { mp_raise_ValueError(MP_ERROR_TEXT("negative shift count")); } else { // standard precision is enough for right-shift - if (rhs_val >= (mp_int_t)BITS_PER_WORD) { + if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * BITS_PER_BYTE)) { // Shifting to big amounts is underfined behavior // in C and is CPU-dependent; propagate sign bit. - rhs_val = BITS_PER_WORD - 1; + rhs_val = sizeof(lhs_val) * BITS_PER_BYTE - 1; } lhs_val >>= rhs_val; } From 7e956fae28e4e3fdea30f834d448eacfc4429de7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Feb 2021 15:32:59 +1100 Subject: [PATCH 0579/3533] py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE. To give this macro a standard MP_ prefix. Signed-off-by: Damien George --- py/gc.c | 4 ++-- py/mpconfig.h | 5 +++-- py/objfloat.c | 2 +- py/objint.c | 2 +- py/runtime.c | 6 +++--- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/py/gc.c b/py/gc.c index 41bbaa1b57f3c..be35dc2ccc458 100644 --- a/py/gc.c +++ b/py/gc.c @@ -118,9 +118,9 @@ void gc_init(void *start, void *end) { // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK) size_t total_byte_len = (byte *)end - (byte *)start; #if MICROPY_ENABLE_FINALISER - MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); + MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len * MP_BITS_PER_BYTE / (MP_BITS_PER_BYTE + MP_BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + MP_BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); #else - MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); + MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + MP_BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); #endif MP_STATE_MEM(gc_alloc_table_start) = (byte *)start; diff --git a/py/mpconfig.h b/py/mpconfig.h index 3c1ed28d806f7..3a0c5fb6e770a 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1535,8 +1535,9 @@ typedef double mp_float_t; #define BYTES_PER_WORD (sizeof(mp_uint_t)) #endif -#ifndef BITS_PER_BYTE -#define BITS_PER_BYTE (8) +// Number of bits in a byte +#ifndef MP_BITS_PER_BYTE +#define MP_BITS_PER_BYTE (8) #endif // mp_int_t value with most significant bit set #define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1)) diff --git a/py/objfloat.c b/py/objfloat.c index 451609492e5c1..5194dba51ee6d 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -77,7 +77,7 @@ mp_int_t mp_float_hash(mp_float_t src) { // number may have a fraction; xor the integer part with the fractional part val = (frc >> (MP_FLOAT_FRAC_BITS - adj_exp)) ^ (frc & (((mp_float_uint_t)1 << (MP_FLOAT_FRAC_BITS - adj_exp)) - 1)); - } else if ((unsigned int)adj_exp < BITS_PER_BYTE * sizeof(mp_int_t) - 1) { + } else if ((unsigned int)adj_exp < MP_BITS_PER_BYTE * sizeof(mp_int_t) - 1) { // the number is a (big) whole integer and will fit in val's signed-width val = (mp_int_t)frc << (adj_exp - MP_FLOAT_FRAC_BITS); } else { diff --git a/py/objint.c b/py/objint.c index 4619fb5751fed..00375d388ee9b 100644 --- a/py/objint.c +++ b/py/objint.c @@ -121,7 +121,7 @@ STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) { return MP_FP_CLASS_FIT_SMALLINT; } #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG - if (e <= (((sizeof(long long) * BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) { + if (e <= (((sizeof(long long) * MP_BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) { return MP_FP_CLASS_FIT_LONGINT; } #endif diff --git a/py/runtime.c b/py/runtime.c index 2e2fa1d173351..5d476a2764df3 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -387,7 +387,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { if (rhs_val < 0) { // negative shift not allowed mp_raise_ValueError(MP_ERROR_TEXT("negative shift count")); - } else if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * BITS_PER_BYTE) + } else if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE) || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) { // left-shift will overflow, so use higher precision integer @@ -406,10 +406,10 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { mp_raise_ValueError(MP_ERROR_TEXT("negative shift count")); } else { // standard precision is enough for right-shift - if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * BITS_PER_BYTE)) { + if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE)) { // Shifting to big amounts is underfined behavior // in C and is CPU-dependent; propagate sign bit. - rhs_val = sizeof(lhs_val) * BITS_PER_BYTE - 1; + rhs_val = sizeof(lhs_val) * MP_BITS_PER_BYTE - 1; } lhs_val >>= rhs_val; } From ad4656b861f94277bed9647ca176e662ce5119e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Feb 2021 16:39:09 +1100 Subject: [PATCH 0580/3533] all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD. The "word" referred to by BYTES_PER_WORD is actually the size of mp_obj_t which is not always the same as the size of a pointer on the target architecture. So rename this config value to better reflect what it measures, and also prefix it with MP_. For uses of BYTES_PER_WORD in setting the stack limit this has been changed to sizeof(void *), because the stack usually grows with machine-word sized values (eg an nlr_buf_t has many machine words in it). Signed-off-by: Damien George --- examples/embedding/hello-embed.c | 2 +- mpy-cross/main.c | 4 ++-- ports/esp32/mpconfigport.h | 1 - ports/nrf/mpconfigport.h | 2 -- ports/unix/main.c | 4 ++-- ports/unix/mpthreadport.c | 2 +- py/binary.c | 4 ++-- py/emitbc.c | 2 +- py/gc.c | 2 +- py/mpconfig.h | 10 +++++----- py/persistentcode.c | 2 +- 11 files changed, 16 insertions(+), 19 deletions(-) diff --git a/examples/embedding/hello-embed.c b/examples/embedding/hello-embed.c index 2000b703c11c6..9a90288cf6d77 100644 --- a/examples/embedding/hello-embed.c +++ b/examples/embedding/hello-embed.c @@ -53,7 +53,7 @@ mp_obj_t execute_from_str(const char *str) { int main() { // Initialized stack limit - mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4)); + mp_stack_set_limit(40000 * (sizeof(void *) / 4)); // Initialize heap gc_init(heap, heap + sizeof(heap)); // Initialize interpreter diff --git a/mpy-cross/main.c b/mpy-cross/main.c index a403c0504dd76..635e53a719cc9 100644 --- a/mpy-cross/main.c +++ b/mpy-cross/main.c @@ -170,7 +170,7 @@ STATIC void pre_process_options(int argc, char **argv) { heap_size *= 1024 * 1024; } if (word_adjust) { - heap_size = heap_size * BYTES_PER_WORD / 4; + heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4; } } else { exit(usage(argv)); @@ -182,7 +182,7 @@ STATIC void pre_process_options(int argc, char **argv) { } MP_NOINLINE int main_(int argc, char **argv) { - mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4)); + mp_stack_set_limit(40000 * (sizeof(void *) / 4)); pre_process_options(argc, argv); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 5ad5fa18a3045..81f8297c50019 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -221,7 +221,6 @@ struct mp_bluetooth_nimble_root_pointers_t; // type definitions for the specific machine -#define BYTES_PER_WORD (4) #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p))) void *esp_native_code_commit(void *, size_t, void *); #define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 581e83ef2f8c7..c246c53dd5aff 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -201,8 +201,6 @@ // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) #define MP_SSIZE_MAX (0x7fffffff) diff --git a/ports/unix/main.c b/ports/unix/main.c index af4328a4db759..07db8d22c614f 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -387,7 +387,7 @@ STATIC void pre_process_options(int argc, char **argv) { goto invalid_arg; } if (word_adjust) { - heap_size = heap_size * BYTES_PER_WORD / 4; + heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4; } // If requested size too small, we'll crash anyway if (heap_size < 700) { @@ -446,7 +446,7 @@ MP_NOINLINE int main_(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); #endif - mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4)); + mp_stack_set_limit(40000 * (sizeof(void *) / 4)); pre_process_options(argc, argv); diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c index de0f5923bafe9..cbc4901f69f4e 100644 --- a/ports/unix/mpthreadport.c +++ b/ports/unix/mpthreadport.c @@ -206,7 +206,7 @@ void mp_thread_start(void) { void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) { // default stack size is 8k machine-words if (*stack_size == 0) { - *stack_size = 8192 * BYTES_PER_WORD; + *stack_size = 8192 * sizeof(void *); } // minimum stack size is set by pthreads diff --git a/py/binary.c b/py/binary.c index 1847894b71068..5c098b223d6c2 100644 --- a/py/binary.c +++ b/py/binary.c @@ -321,7 +321,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p double f; } fp_dp; fp_dp.f = mp_obj_get_float_to_d(val_in); - if (BYTES_PER_WORD == 8) { + if (MP_BYTES_PER_OBJ_WORD == 8) { val = fp_dp.i64; } else { int be = struct_type == '>'; @@ -342,7 +342,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p val = mp_obj_get_int(val_in); // zero/sign extend if needed - if (BYTES_PER_WORD < 8 && size > sizeof(val)) { + if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) { int c = (mp_int_t)val < 0 ? 0xff : 0x00; memset(p, c, size); if (struct_type == '>') { diff --git a/py/emitbc.c b/py/emitbc.c index c74b7fbc4d978..d7e8e05f0fa63 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -36,7 +36,7 @@ #if MICROPY_ENABLE_COMPILER -#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) +#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7) #define DUMMY_DATA_SIZE (BYTES_FOR_INT) struct _emit_t { diff --git a/py/gc.c b/py/gc.c index be35dc2ccc458..53a0d9da4a73c 100644 --- a/py/gc.c +++ b/py/gc.c @@ -49,7 +49,7 @@ // detect untraced object still in use #define CLEAR_ON_SWEEP (0) -#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD) +#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / MP_BYTES_PER_OBJ_WORD) #define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK) // ATB = allocation table byte diff --git a/py/mpconfig.h b/py/mpconfig.h index 3a0c5fb6e770a..e87c52f2be6ff 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -126,7 +126,7 @@ // Number of bytes in memory allocation/GC block. Any size allocated will be // rounded up to be multiples of this. #ifndef MICROPY_BYTES_PER_GC_BLOCK -#define MICROPY_BYTES_PER_GC_BLOCK (4 * BYTES_PER_WORD) +#define MICROPY_BYTES_PER_GC_BLOCK (4 * MP_BYTES_PER_OBJ_WORD) #endif // Number of words allocated (in BSS) to the GC stack (minimum is 1) @@ -1530,9 +1530,9 @@ typedef double mp_float_t; #define STATIC static #endif -// Number of bytes in a word -#ifndef BYTES_PER_WORD -#define BYTES_PER_WORD (sizeof(mp_uint_t)) +// Number of bytes in an object word: mp_obj_t, mp_uint_t, mp_uint_t +#ifndef MP_BYTES_PER_OBJ_WORD +#define MP_BYTES_PER_OBJ_WORD (sizeof(mp_uint_t)) #endif // Number of bits in a byte @@ -1540,7 +1540,7 @@ typedef double mp_float_t; #define MP_BITS_PER_BYTE (8) #endif // mp_int_t value with most significant bit set -#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1)) +#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (MP_BYTES_PER_OBJ_WORD * MP_BITS_PER_BYTE - 1)) // Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are // defined and that they are the opposite of each other. diff --git a/py/persistentcode.c b/py/persistentcode.c index 084632a60fdc1..ac523990c1bcd 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -595,7 +595,7 @@ STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) { print->print_strn(print->data, (const char *)data, len); } -#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) +#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7) STATIC void mp_print_uint(mp_print_t *print, size_t n) { byte buf[BYTES_FOR_INT]; byte *p = buf + sizeof(buf); From c891190c693ec04289f5773b2b27d125f8c6059f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Feb 2021 16:39:46 +1100 Subject: [PATCH 0581/3533] py: Rename WORD_MSBIT_HIGH to MP_OBJ_WORD_MSBIT_HIGH. To make it clear it is for mp_obj_t/mp_uint_t "word" types, and to prefix this macro with MP_. Signed-off-by: Damien George --- py/mpconfig.h | 2 +- py/mpz.c | 4 ++-- py/smallint.h | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index e87c52f2be6ff..454fc12b7cacd 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1540,7 +1540,7 @@ typedef double mp_float_t; #define MP_BITS_PER_BYTE (8) #endif // mp_int_t value with most significant bit set -#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (MP_BYTES_PER_OBJ_WORD * MP_BITS_PER_BYTE - 1)) +#define MP_OBJ_WORD_MSBIT_HIGH (((mp_uint_t)1) << (MP_BYTES_PER_OBJ_WORD * MP_BITS_PER_BYTE - 1)) // Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are // defined and that they are the opposite of each other. diff --git a/py/mpz.c b/py/mpz.c index 75159305382a1..51d0c968907d8 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -1573,7 +1573,7 @@ bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) { mpz_dig_t *d = i->dig + i->len; while (d-- > i->dig) { - if (val > (~(WORD_MSBIT_HIGH) >> DIG_SIZE)) { + if (val > (~(MP_OBJ_WORD_MSBIT_HIGH) >> DIG_SIZE)) { // will overflow return false; } @@ -1598,7 +1598,7 @@ bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) { mpz_dig_t *d = i->dig + i->len; while (d-- > i->dig) { - if (val > (~(WORD_MSBIT_HIGH) >> (DIG_SIZE - 1))) { + if (val > (~(MP_OBJ_WORD_MSBIT_HIGH) >> (DIG_SIZE - 1))) { // will overflow return false; } diff --git a/py/smallint.h b/py/smallint.h index 6a3c75236c3ff..58d843e8a1e35 100644 --- a/py/smallint.h +++ b/py/smallint.h @@ -36,17 +36,17 @@ // In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C -#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 1)) -#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0) +#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)MP_OBJ_WORD_MSBIT_HIGH) >> 1)) +#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & MP_OBJ_WORD_MSBIT_HIGH) == 0) // Mask to truncate mp_int_t to positive value -#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1)) +#define MP_SMALL_INT_POSITIVE_MASK ~(MP_OBJ_WORD_MSBIT_HIGH | (MP_OBJ_WORD_MSBIT_HIGH >> 1)) #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B -#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 2)) +#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)MP_OBJ_WORD_MSBIT_HIGH) >> 2)) #define MP_SMALL_INT_FITS(n) ((((n) & MP_SMALL_INT_MIN) == 0) || (((n) & MP_SMALL_INT_MIN) == MP_SMALL_INT_MIN)) // Mask to truncate mp_int_t to positive value -#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1) | (WORD_MSBIT_HIGH >> 2)) +#define MP_SMALL_INT_POSITIVE_MASK ~(MP_OBJ_WORD_MSBIT_HIGH | (MP_OBJ_WORD_MSBIT_HIGH >> 1) | (MP_OBJ_WORD_MSBIT_HIGH >> 2)) #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D From 1f800cac3c4f56418486f24c3824418a890f759f Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Feb 2021 01:10:30 +1100 Subject: [PATCH 0582/3533] rp2/micropy_rules.cmake: Fix makemoduledefs vpath to work with abs path. In particular the firmware can now be built in a build directory that lives outside the source tree, and the py/modarray.c file will still be found. See issue #6837. Signed-off-by: Damien George --- ports/rp2/micropy_rules.cmake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/rp2/micropy_rules.cmake b/ports/rp2/micropy_rules.cmake index ade9b8c92d5cc..9eee4ac14d1b2 100644 --- a/ports/rp2/micropy_rules.cmake +++ b/ports/rp2/micropy_rules.cmake @@ -24,11 +24,10 @@ add_custom_command( ) # Generate moduledefs.h -# This is currently hard-coded to support modarray.c only, because makemoduledefs.py doesn't support absolute paths add_custom_command( OUTPUT ${MPY_MODULEDEFS} - COMMAND python3 ${MPY_PY_DIR}/makemoduledefs.py --vpath="." ../../../py/modarray.c > ${MPY_MODULEDEFS} + COMMAND python3 ${MPY_PY_DIR}/makemoduledefs.py --vpath="/" ${SOURCE_QSTR} > ${MPY_MODULEDEFS} DEPENDS ${MPY_MPVERSION} ${SOURCE_QSTR} ) From 9dedcf122d19c6b7452adb48ff5567509adfb073 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Feb 2021 15:45:38 +1100 Subject: [PATCH 0583/3533] py/gc: Change include of stdint.h to stddef.h. No std-int types are used in gc.h, but size_t is which needs stddef.h. Signed-off-by: Damien George --- ports/esp8266/gccollect.h | 2 ++ py/gc.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/esp8266/gccollect.h b/ports/esp8266/gccollect.h index 4323e95075a9f..b86d3d6e1cc7a 100644 --- a/ports/esp8266/gccollect.h +++ b/ports/esp8266/gccollect.h @@ -26,6 +26,8 @@ #ifndef MICROPY_INCLUDED_ESP8266_GCCOLLECT_H #define MICROPY_INCLUDED_ESP8266_GCCOLLECT_H +#include + extern uint32_t _text_start; extern uint32_t _text_end; extern uint32_t _irom0_text_start; diff --git a/py/gc.h b/py/gc.h index f67fb0daad6dc..5aef27c006b93 100644 --- a/py/gc.h +++ b/py/gc.h @@ -27,7 +27,7 @@ #define MICROPY_INCLUDED_PY_GC_H #include -#include +#include void gc_init(void *start, void *end); From 0a59938574502b19b3d685133084399c090d3c11 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Feb 2021 00:26:08 +1100 Subject: [PATCH 0584/3533] py/mpz: Fix overflow of borrow in mpn_div. For certain operands to mpn_div, the existing code path for `DIG_SIZE == MPZ_DBL_DIG_SIZE / 2` had a bug in it where borrow could still overflow in the `(x >= *n || *n - x <= borrow)` branch, ie `borrow + x - (mpz_dbl_dig_t)*n` overflows the borrow variable. In such cases the subsequent right-shift of borrow would not bring in the overflow bit, leading to an error in the result. An example division that had overflow when MPZ_DIG_SIZE = 16 is `(2 ** 48 - 1) ** 2 // (2 ** 48 - 1)`. This is fixed in this commit by simplifying the code and handling the low digits of borrow first, and then the upper bits (to shift down) separately. There is no longer a distinction between `DIG_SIZE < MPZ_DBL_DIG_SIZE / 2` and `DIG_SIZE == MPZ_DBL_DIG_SIZE / 2`. This commit also simplifies the second part of the calculation so that borrow does not need to be negated (instead the code just works knowing that borrow is negative and using + instead of - in calculations involving borrow). Fixes #6777. Signed-off-by: Damien George --- py/mpz.c | 55 +++++++++++-------------------------- tests/basics/int_big_div.py | 4 +++ 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/py/mpz.c b/py/mpz.c index 51d0c968907d8..e0d249c214ff5 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -531,60 +531,37 @@ STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_di quo /= lead_den_digit; // Multiply quo by den and subtract from num to get remainder. - // We have different code here to handle different compile-time - // configurations of mpz: - // - // 1. DIG_SIZE is stricly less than half the number of bits - // available in mpz_dbl_dig_t. In this case we can use a - // slightly more optimal (in time and space) routine that - // uses the extra bits in mpz_dbl_dig_signed_t to store a - // sign bit. - // - // 2. DIG_SIZE is exactly half the number of bits available in - // mpz_dbl_dig_t. In this (common) case we need to be careful - // not to overflow the borrow variable. And the shifting of - // borrow needs some special logic (it's a shift right with - // round up). - // + // Must be careful with overflow of the borrow variable. Both + // borrow and low_digs are signed values and need signed right-shift, + // but x is unsigned and may take a full-range value. const mpz_dig_t *d = den_dig; mpz_dbl_dig_t d_norm = 0; - mpz_dbl_dig_t borrow = 0; + mpz_dbl_dig_signed_t borrow = 0; for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) { + // Get the next digit in (den). d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE); + // Multiply the next digit in (quo * den). mpz_dbl_dig_t x = (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK); - #if DIG_SIZE < MPZ_DBL_DIG_SIZE / 2 - borrow += (mpz_dbl_dig_t)*n - x; // will overflow if DIG_SIZE >= MPZ_DBL_DIG_SIZE/2 - *n = borrow & DIG_MASK; - borrow = (mpz_dbl_dig_signed_t)borrow >> DIG_SIZE; - #else // DIG_SIZE == MPZ_DBL_DIG_SIZE / 2 - if (x >= *n || *n - x <= borrow) { - borrow += x - (mpz_dbl_dig_t)*n; - *n = (-borrow) & DIG_MASK; - borrow = (borrow >> DIG_SIZE) + ((borrow & DIG_MASK) == 0 ? 0 : 1); // shift-right with round-up - } else { - *n = ((mpz_dbl_dig_t)*n - x - borrow) & DIG_MASK; - borrow = 0; - } - #endif + // Compute the low DIG_MASK bits of the next digit in (num - quo * den) + mpz_dbl_dig_signed_t low_digs = (borrow & DIG_MASK) + *n - (x & DIG_MASK); + // Store the digit result for (num). + *n = low_digs & DIG_MASK; + // Compute the borrow, shifted right before summing to avoid overflow. + borrow = (borrow >> DIG_SIZE) - (x >> DIG_SIZE) + (low_digs >> DIG_SIZE); } - #if DIG_SIZE < MPZ_DBL_DIG_SIZE / 2 - // Borrow was negative in the above for-loop, make it positive for next if-block. - borrow = -borrow; - #endif - // At this point we have either: // // 1. quo was the correct value and the most-sig-digit of num is exactly - // cancelled by borrow (borrow == *num_dig). In this case there is + // cancelled by borrow (borrow + *num_dig == 0). In this case there is // nothing more to do. // // 2. quo was too large, we subtracted too many den from num, and the - // most-sig-digit of num is 1 less than borrow (borrow == *num_dig + 1). + // most-sig-digit of num is less than needed (borrow + *num_dig < 0). // In this case we must reduce quo and add back den to num until the // carry from this operation cancels out the borrow. // - borrow -= *num_dig; + borrow += *num_dig; for (; borrow != 0; --quo) { d = den_dig; d_norm = 0; @@ -595,7 +572,7 @@ STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_di *n = carry & DIG_MASK; carry >>= DIG_SIZE; } - borrow -= carry; + borrow += carry; } // store this digit of the quotient diff --git a/tests/basics/int_big_div.py b/tests/basics/int_big_div.py index 642f051d41284..29fd405970f58 100644 --- a/tests/basics/int_big_div.py +++ b/tests/basics/int_big_div.py @@ -8,3 +8,7 @@ print((x + 1) // x) x = 0x86c60128feff5330 print((x + 1) // x) + +# these check edge cases where borrow overflows +print((2 ** 48 - 1) ** 2 // (2 ** 48 - 1)) +print((2 ** 256 - 2 ** 32) ** 2 // (2 ** 256 - 2 ** 32)) From c7aaee2b2ba0cb19d3b3deb6092e0bfe55d79052 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 9 Feb 2021 10:42:19 +1100 Subject: [PATCH 0585/3533] esp8266/modules: Fix fs_corrupted() to use start_sec not START_SEC. START_SEC was changed in e0905e85a7ad2961aa9192f6130565860e531ad3. Also, update the error message to mention how to format the partition at the REPL, and make the total message shorter to save a bit of flash. Signed-off-by: Damien George --- ports/esp8266/modules/inisetup.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ports/esp8266/modules/inisetup.py b/ports/esp8266/modules/inisetup.py index e711a57d4b609..9500b8048c9d8 100644 --- a/ports/esp8266/modules/inisetup.py +++ b/ports/esp8266/modules/inisetup.py @@ -30,13 +30,12 @@ def fs_corrupted(): while 1: print( """\ -The filesystem starting at sector %d with size %d sectors appears to -be corrupted. If you had important data there, you may want to make a flash -snapshot to try to recover it. Otherwise, perform factory reprogramming -of MicroPython firmware (completely erase flash, followed by firmware -programming). +The filesystem starting at sector %d with size %d sectors looks corrupt. +You may want to make a flash snapshot and try to recover it. Otherwise, +format it with uos.VfsLfs2.mkfs(bdev), or completely erase the flash and +reprogram MicroPython. """ - % (bdev.START_SEC, bdev.blocks) + % (bdev.start_sec, bdev.blocks) ) time.sleep(3) From 26b4ef4c46520f595f39d53bbea7f247c9467370 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Feb 2021 22:53:36 +1100 Subject: [PATCH 0586/3533] extmod/vfs_posix_file: Allow closing an already closed file. Signed-off-by: Damien George --- extmod/vfs_posix_file.c | 6 +++++- tests/extmod/vfs_posix.py | 25 +++++++++++++++++++++++++ tests/extmod/vfs_posix.py.exp | 1 + tests/io/file1.py | 3 +++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 264764b4ee29d..f3eac98ce3f9f 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -163,7 +163,11 @@ STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in); - check_fd_is_open(o); + + if (request != MP_STREAM_CLOSE) { + check_fd_is_open(o); + } + switch (request) { case MP_STREAM_FLUSH: { int ret; diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py index 3bea99365d970..aea447e182e07 100644 --- a/tests/extmod/vfs_posix.py +++ b/tests/extmod/vfs_posix.py @@ -8,6 +8,15 @@ print("SKIP") raise SystemExit +# We need a file for testing that doesn't already exist. +# Skip the test if it does exist. +temp_file = "micropy_test_file.txt" +try: + uos.stat(temp_file) + print("SKIP") + raise SystemExit +except OSError: + pass # getcwd and chdir curdir = uos.getcwd() @@ -21,3 +30,19 @@ # listdir and ilistdir print(type(uos.listdir("/"))) + +# file create +f = open(temp_file, "w") +f.write("hello") +f.close() + +# close on a closed file should succeed +f.close() + +# file read +f = open(temp_file, "r") +print(f.read()) +f.close() + +# remove +uos.remove(temp_file) diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp index 1b1f59b43e5da..c0a4ed000bf8b 100644 --- a/tests/extmod/vfs_posix.py.exp +++ b/tests/extmod/vfs_posix.py.exp @@ -2,3 +2,4 @@ True +hello diff --git a/tests/io/file1.py b/tests/io/file1.py index 2a46c9c63e360..de30045d316bb 100644 --- a/tests/io/file1.py +++ b/tests/io/file1.py @@ -44,3 +44,6 @@ except OSError: print("OSError") f.close() + +# close() on a closed file +f.close() From df85e4881396d0ebb5942a111fba974ea5c80b77 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Feb 2021 23:24:39 +1100 Subject: [PATCH 0587/3533] tests/extmod/vfs_posix.py: Add more tests for VfsPosix class. Signed-off-by: Damien George --- tests/extmod/vfs_posix.py | 52 +++++++++++++++++++++++++++++++---- tests/extmod/vfs_posix.py.exp | 11 ++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py index aea447e182e07..f8c4aae40633f 100644 --- a/tests/extmod/vfs_posix.py +++ b/tests/extmod/vfs_posix.py @@ -8,11 +8,11 @@ print("SKIP") raise SystemExit -# We need a file for testing that doesn't already exist. +# We need a directory for testing that doesn't already exist. # Skip the test if it does exist. -temp_file = "micropy_test_file.txt" +temp_dir = "micropy_test_dir" try: - uos.stat(temp_file) + uos.stat(temp_dir) print("SKIP") raise SystemExit except OSError: @@ -31,18 +31,58 @@ # listdir and ilistdir print(type(uos.listdir("/"))) +# mkdir +uos.mkdir(temp_dir) + # file create -f = open(temp_file, "w") +f = open(temp_dir + "/test", "w") f.write("hello") f.close() # close on a closed file should succeed f.close() +# construct a file object using the type constructor, with a raw fileno +f = type(f)(2) +print(f) + # file read -f = open(temp_file, "r") +f = open(temp_dir + "/test", "r") print(f.read()) f.close() +# rename +uos.rename(temp_dir + "/test", temp_dir + "/test2") +print(uos.listdir(temp_dir)) + +# construct new VfsPosix with path argument +vfs = uos.VfsPosix(temp_dir) +print(list(i[0] for i in vfs.ilistdir("."))) + +# stat, statvfs +print(type(vfs.stat("."))) +print(type(vfs.statvfs("."))) + +# check types of ilistdir with str/bytes arguments +print(type(list(vfs.ilistdir("."))[0][0])) +print(type(list(vfs.ilistdir(b"."))[0][0])) + # remove -uos.remove(temp_file) +uos.remove(temp_dir + "/test2") +print(uos.listdir(temp_dir)) + +# remove with error +try: + uos.remove(temp_dir + "/test2") +except OSError: + print("remove OSError") + +# rmdir +uos.rmdir(temp_dir) +print(temp_dir in uos.listdir()) + +# rmdir with error +try: + uos.rmdir(temp_dir) +except OSError: + print("rmdir OSError") diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp index c0a4ed000bf8b..e7d68f38ec7ca 100644 --- a/tests/extmod/vfs_posix.py.exp +++ b/tests/extmod/vfs_posix.py.exp @@ -2,4 +2,15 @@ True + hello +['test2'] +['test2'] + + + + +[] +remove OSError +False +rmdir OSError From 7815dd2cc5306b72c84dbbc27638d9d3f6b32ac9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Feb 2021 21:37:10 +1100 Subject: [PATCH 0588/3533] unix/mpbtstackport_common: Implement mp_bluetooth_hci_active. So that BTSTACK can be enabled with SYNC_EVENTS. Signed-off-by: Damien George --- ports/unix/mpbtstackport_common.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c index 621e661f9eba3..ec40db65bc3a4 100644 --- a/ports/unix/mpbtstackport_common.c +++ b/ports/unix/mpbtstackport_common.c @@ -57,6 +57,11 @@ bool mp_bluetooth_hci_poll(void) { return false; } +bool mp_bluetooth_hci_active(void) { + return mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_OFF + && mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT; +} + // The IRQ functionality in btstack_run_loop_embedded.c is not used, so the // following three functions are empty. From 7535f67dfbbcd0f2f47ffbf893d6f66f96becae6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Feb 2021 16:00:27 +1100 Subject: [PATCH 0589/3533] extmod/btstack: Add HCI trace debugging option in btstack_hci_uart. Signed-off-by: Damien George --- extmod/btstack/btstack_hci_uart.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/extmod/btstack/btstack_hci_uart.c b/extmod/btstack/btstack_hci_uart.c index ae18628a9cf0e..0a137bbae442d 100644 --- a/extmod/btstack/btstack_hci_uart.c +++ b/extmod/btstack/btstack_hci_uart.c @@ -38,6 +38,11 @@ #include "mpbtstackport.h" +#define HCI_TRACE (0) +#define COL_OFF "\033[0m" +#define COL_GREEN "\033[0;32m" +#define COL_BLUE "\033[0;34m" + // Implements a btstack btstack_uart_block_t on top of the mphciuart.h // interface to an HCI UART provided by the port. @@ -114,6 +119,14 @@ STATIC void btstack_uart_receive_block(uint8_t *buf, uint16_t len) { } STATIC void btstack_uart_send_block(const uint8_t *buf, uint16_t len) { + #if HCI_TRACE + printf(COL_GREEN "< [% 8d] %02x", (int)mp_hal_ticks_ms(), buf[0]); + for (size_t i = 1; i < len; ++i) { + printf(":%02x", buf[i]); + } + printf(COL_OFF "\n"); + #endif + mp_bluetooth_hci_uart_write(buf, len); send_done = true; } @@ -165,6 +178,13 @@ void mp_bluetooth_btstack_hci_uart_process(void) { while (recv_idx < recv_len && (chr = mp_bluetooth_hci_uart_readchar()) >= 0) { recv_buf[recv_idx++] = chr; if (recv_idx == recv_len) { + #if HCI_TRACE + printf(COL_BLUE "> [% 8d] %02x", (int)mp_hal_ticks_ms(), recv_buf[0]); + for (size_t i = 1; i < recv_len; ++i) { + printf(":%02x", recv_buf[i]); + } + printf(COL_OFF "\n"); + #endif recv_idx = 0; recv_len = 0; if (recv_handler) { From 24a8a408a9f5cc6cb26f2295a87303e75a48a312 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Feb 2021 21:39:32 +1100 Subject: [PATCH 0590/3533] extmod/btstack: Add stub functions for passkey, l2cap bindings. Signed-off-by: Damien George --- extmod/btstack/modbluetooth_btstack.c | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index f6af664a4e9b5..540b0fb7a9d24 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -1168,11 +1168,18 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle) { } #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + int mp_bluetooth_gap_pair(uint16_t conn_handle) { DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle); sm_request_pairing(conn_handle); return 0; } + +int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) { + DEBUG_printf("mp_bluetooth_gap_passkey: conn_handle=%d action=%d passkey=%d\n", conn_handle, action, (int)passkey); + return MP_EOPNOTSUPP; +} + #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE @@ -1341,4 +1348,33 @@ int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { } #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + +int mp_bluetooth_l2cap_listen(uint16_t psm, uint16_t mtu) { + DEBUG_printf("mp_bluetooth_l2cap_listen: psm=%d, mtu=%d\n", psm, mtu); + return MP_EOPNOTSUPP; +} + +int mp_bluetooth_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu) { + DEBUG_printf("mp_bluetooth_l2cap_connect: conn_handle=%d, psm=%d, mtu=%d\n", conn_handle, psm, mtu); + return MP_EOPNOTSUPP; +} + +int mp_bluetooth_l2cap_disconnect(uint16_t conn_handle, uint16_t cid) { + DEBUG_printf("mp_bluetooth_l2cap_disconnect: conn_handle=%d, cid=%d\n", conn_handle, cid); + return MP_EOPNOTSUPP; +} + +int mp_bluetooth_l2cap_send(uint16_t conn_handle, uint16_t cid, const uint8_t *buf, size_t len, bool *stalled) { + DEBUG_printf("mp_bluetooth_l2cap_send: conn_handle=%d, cid=%d, len=%d\n", conn_handle, cid, (int)len); + return MP_EOPNOTSUPP; +} + +int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf, size_t *len) { + DEBUG_printf("mp_bluetooth_l2cap_recvinto: conn_handle=%d, cid=%d, len=%d\n", conn_handle, cid, (int)*len); + return MP_EOPNOTSUPP; +} + +#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS + #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK From 50615fef89787f8b55a8ba6e718298421d7e3cb0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Feb 2021 21:40:11 +1100 Subject: [PATCH 0591/3533] extmod/btstack: Enable SYNC_EVENTS, PAIRING_BONDING by default. Synchronous events work on stm32 and unix ports. Signed-off-by: Damien George --- extmod/btstack/btstack.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index e3309b61db7a4..17bbb16f88f72 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -11,6 +11,8 @@ EXTMOD_SRC_C += extmod/btstack/modbluetooth_btstack.c INC += -I$(TOP)/$(BTSTACK_EXTMOD_DIR) CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK=1 +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 BTSTACK_DIR = $(TOP)/lib/btstack From 9b7d8b87ee317ed69c8c9963d4eb27174ac33230 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 16:33:06 +1100 Subject: [PATCH 0592/3533] lib/tinyusb: Update to version 0.8.0. Includes support for RP2040. Signed-off-by: Damien George --- lib/tinyusb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tinyusb b/lib/tinyusb index a6b916ba85bef..7b62c71dd5ec4 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit a6b916ba85bef6aad50f1652532b02984dfe2484 +Subproject commit 7b62c71dd5ec42e61499d2d83902df9484842670 From 035d16126ac2e4ea9b9c1c33a15104cd952897a1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 8 Feb 2021 23:47:49 +1100 Subject: [PATCH 0593/3533] ports: Update to build with new tinyusb. Signed-off-by: Damien George --- ports/mimxrt/board_init.c | 4 ++-- ports/mimxrt/tusb_config.h | 1 - ports/mimxrt/tusb_port.c | 4 ++-- ports/nrf/drivers/usb/usb_descriptors.c | 4 ++-- ports/samd/tusb_port.c | 14 +++++++------- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c index 6f5235d3b635b..102764cac13d2 100644 --- a/ports/mimxrt/board_init.c +++ b/ports/mimxrt/board_init.c @@ -90,11 +90,11 @@ void SysTick_Handler(void) { } void USB_OTG1_IRQHandler(void) { - tud_isr(0); + tud_int_handler(0); tud_task(); } void USB_OTG2_IRQHandler(void) { - tud_isr(1); + tud_int_handler(1); tud_task(); } diff --git a/ports/mimxrt/tusb_config.h b/ports/mimxrt/tusb_config.h index c7ec05e632323..862fb6c52d2e5 100644 --- a/ports/mimxrt/tusb_config.h +++ b/ports/mimxrt/tusb_config.h @@ -32,6 +32,5 @@ #define CFG_TUD_CDC (1) #define CFG_TUD_CDC_RX_BUFSIZE (512) #define CFG_TUD_CDC_TX_BUFSIZE (512) -#define CFG_TUD_CDC_EPSIZE (512) #endif // MICROPY_INCLUDED_MIMXRT_TUSB_CONFIG_H diff --git a/ports/mimxrt/tusb_port.c b/ports/mimxrt/tusb_port.c index f6b09a362d18a..0d73b09235b64 100644 --- a/ports/mimxrt/tusb_port.c +++ b/ports/mimxrt/tusb_port.c @@ -67,7 +67,7 @@ static const tusb_desc_device_t usbd_desc_device = { }; static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { - TUD_CONFIG_DESCRIPTOR(USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, @@ -90,7 +90,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { return usbd_desc_cfg; } -const uint16_t *tud_descriptor_string_cb(uint8_t index) { +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { #define DESC_STR_MAX (20) static uint16_t desc_str[DESC_STR_MAX]; diff --git a/ports/nrf/drivers/usb/usb_descriptors.c b/ports/nrf/drivers/usb/usb_descriptors.c index f6724c1bc078c..e9436ded54fe8 100644 --- a/ports/nrf/drivers/usb/usb_descriptors.c +++ b/ports/nrf/drivers/usb/usb_descriptors.c @@ -67,7 +67,7 @@ static const tusb_desc_device_t usbd_desc_device = { }; static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { - TUD_CONFIG_DESCRIPTOR(USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, @@ -90,7 +90,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { return usbd_desc_cfg; } -const uint16_t *tud_descriptor_string_cb(uint8_t index) { +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { #define DESC_STR_MAX (20) static uint16_t desc_str[DESC_STR_MAX]; diff --git a/ports/samd/tusb_port.c b/ports/samd/tusb_port.c index f3d417f1a182e..e96f332464b5a 100644 --- a/ports/samd/tusb_port.c +++ b/ports/samd/tusb_port.c @@ -68,7 +68,7 @@ static const tusb_desc_device_t usbd_desc_device = { }; static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { - TUD_CONFIG_DESCRIPTOR(USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, @@ -91,7 +91,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { return usbd_desc_cfg; } -const uint16_t *tud_descriptor_string_cb(uint8_t index) { +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { #define DESC_STR_MAX (20) static uint16_t desc_str[DESC_STR_MAX]; @@ -118,29 +118,29 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index) { #if defined(MCU_SAMD21) void USB_Handler_wrapper(void) { - USB_Handler(); + tud_int_handler(0); tud_task(); } #elif defined(MCU_SAMD51) void USB_0_Handler_wrapper(void) { - USB_0_Handler(); + tud_int_handler(0); tud_task(); } void USB_1_Handler_wrapper(void) { - USB_1_Handler(); + tud_int_handler(0); tud_task(); } void USB_2_Handler_wrapper(void) { - USB_2_Handler(); + tud_int_handler(0); tud_task(); } void USB_3_Handler_wrapper(void) { - USB_3_Handler(); + tud_int_handler(0); tud_task(); } From c9260dda23bfdaea042d3bb94e1a830af0dbfa18 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 16:33:55 +1100 Subject: [PATCH 0594/3533] rp2: Use local tinyusb instead of the one in pico-sdk. So that all MicroPython ports that use tinyusb use the same version. Also requires fewer submodule checkouts when building rp2 along with other ports that use tinyusb. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 3 +++ tools/ci.sh | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 160456c6b1e24..bd58d8f707952 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -14,6 +14,9 @@ else() set(PICO_SDK_PATH ../../lib/pico-sdk) endif() +# Use the local tinyusb instead of the one in pico-sdk +set(PICO_TINYUSB_PATH ${MPY_DIR}/lib/tinyusb) + # Include component cmake fragments include(micropy_py.cmake) include(micropy_extmod.cmake) diff --git a/tools/ci.sh b/tools/ci.sh index c6b641dae96dc..8661f532efe97 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -195,8 +195,7 @@ function ci_rp2_setup { function ci_rp2_build { make ${MAKEOPTS} -C mpy-cross - git submodule update --init lib/pico-sdk - git -C lib/pico-sdk submodule update --init lib/tinyusb + git submodule update --init lib/pico-sdk lib/tinyusb make ${MAKEOPTS} -C ports/rp2 } From f31c6b484060eb3554aa13bb758cc9a5974cafbb Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Feb 2021 13:32:27 +1100 Subject: [PATCH 0595/3533] mimxrt: Fix USB CDC handling so it works reliably. On i.MX the SysTick IRQ cannot wake the CPU from a WFI so the CPU was blocked on WFI waiting for USB data in mp_hal_stdin_rx_chr() even though it had already arrived (because it may arrive just after calling the check tud_cdc_available()). This commit fixes this problem by using SEV/WFE to indicate that there has been a USB event. The mp_hal_stdout_tx_strn() function is also fixed so that it doesn't overflow the USB buffers. Signed-off-by: Damien George --- ports/mimxrt/board_init.c | 2 ++ ports/mimxrt/mpconfigport.h | 2 +- ports/mimxrt/mphalport.c | 17 ++++++++--------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c index 102764cac13d2..cd7dc9a7dd08e 100644 --- a/ports/mimxrt/board_init.c +++ b/ports/mimxrt/board_init.c @@ -92,9 +92,11 @@ void SysTick_Handler(void) { void USB_OTG1_IRQHandler(void) { tud_int_handler(0); tud_task(); + __SEV(); } void USB_OTG2_IRQHandler(void) { tud_int_handler(1); tud_task(); + __SEV(); } diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 15a292486ff4e..68c7a894221d8 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -94,7 +94,7 @@ extern const struct _mp_obj_module_t mp_module_utime; do { \ extern void mp_handle_pending(bool); \ mp_handle_pending(true); \ - __WFI(); \ + __WFE(); \ } while (0); #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) diff --git a/ports/mimxrt/mphalport.c b/ports/mimxrt/mphalport.c index d7642d6b6d4a4..3032464d398f4 100644 --- a/ports/mimxrt/mphalport.c +++ b/ports/mimxrt/mphalport.c @@ -75,7 +75,7 @@ int mp_hal_stdin_rx_chr(void) { return buf[0]; } } - __WFI(); + MICROPY_EVENT_POLL_HOOK } } @@ -83,17 +83,16 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { if (tud_cdc_connected()) { for (size_t i = 0; i < len;) { uint32_t n = len - i; - uint32_t n2 = tud_cdc_write(str + i, n); - if (n2 < n) { - while (!tud_cdc_write_flush()) { - __WFI(); - } + if (n > CFG_TUD_CDC_EP_BUFSIZE) { + n = CFG_TUD_CDC_EP_BUFSIZE; } + while (n > tud_cdc_write_available()) { + __WFE(); + } + uint32_t n2 = tud_cdc_write(str + i, n); + tud_cdc_write_flush(); i += n2; } - while (!tud_cdc_write_flush()) { - __WFI(); - } } // TODO // while (len--) { From ede6b86a08ea3a749e1785c5263723c875b08359 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Feb 2021 14:09:01 +1100 Subject: [PATCH 0596/3533] samd/mphalport: Fix USB CDC tx handling to work reliably. Signed-off-by: Damien George --- ports/samd/mphalport.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index 67fc2cb0d6f17..357ec93a68143 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -80,17 +80,16 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { if (tud_cdc_connected()) { for (size_t i = 0; i < len;) { uint32_t n = len - i; - uint32_t n2 = tud_cdc_write(str + i, n); - if (n2 < n) { - while (!tud_cdc_write_flush()) { - __WFI(); - } + if (n > CFG_TUD_CDC_EP_BUFSIZE) { + n = CFG_TUD_CDC_EP_BUFSIZE; + } + while (n > tud_cdc_write_available()) { + __WFI(); } + uint32_t n2 = tud_cdc_write(str + i, n); + tud_cdc_write_flush(); i += n2; } - while (!tud_cdc_write_flush()) { - __WFI(); - } } while (len--) { while (!(USARTx->USART.INTFLAG.bit.DRE)) { From 701fdcacafe017c88c8cf18f64d8c26f25987e97 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 13 Feb 2021 13:52:53 +1100 Subject: [PATCH 0597/3533] nrf/drivers/usb: Add USBD_IRQHandler which calls tud_int_handler. This is needed for TinyUSB to process USB device IRQs. Related to #6325. Signed-off-by: Damien George --- ports/nrf/drivers/usb/usb_cdc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/nrf/drivers/usb/usb_cdc.c b/ports/nrf/drivers/usb/usb_cdc.c index c90bced6bb563..9d7c832e20d32 100644 --- a/ports/nrf/drivers/usb/usb_cdc.c +++ b/ports/nrf/drivers/usb/usb_cdc.c @@ -223,4 +223,8 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { } } +void USBD_IRQHandler(void) { + tud_int_handler(0); +} + #endif // MICROPY_HW_USB_CDC From d128999938d6b44f52f4d0c6e1f5169ab3c1e7a5 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Fri, 12 Feb 2021 22:15:43 +1100 Subject: [PATCH 0598/3533] tools: Add filesystem action examples to pyboard.py help. Signed-off-by: Brianna Laugher --- docs/reference/pyboard.py.rst | 4 +++- tools/pyboard.py | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/reference/pyboard.py.rst b/docs/reference/pyboard.py.rst index 30230eebc3bf4..4fedbb7aab9a3 100644 --- a/docs/reference/pyboard.py.rst +++ b/docs/reference/pyboard.py.rst @@ -48,7 +48,9 @@ Running ``pyboard.py --help`` gives the following output: available --follow follow the output after running the scripts [default if no scripts given] - -f, --filesystem perform a filesystem action + -f, --filesystem perform a filesystem action: cp local :device | cp + :device local | cat path | ls [path] | rm path | mkdir + path | rmdir path Running a command on the device ------------------------------- diff --git a/tools/pyboard.py b/tools/pyboard.py index 3ecdf03f156f7..069f7490d0dd1 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -651,7 +651,11 @@ def main(): help="Do not follow the output after running the scripts.", ) cmd_parser.add_argument( - "-f", "--filesystem", action="store_true", help="perform a filesystem action" + "-f", + "--filesystem", + action="store_true", + help="perform a filesystem action: " + "cp local :device | cp :device local | cat path | ls [path] | rm path | mkdir path | rmdir path", ) cmd_parser.add_argument("files", nargs="*", help="input files") args = cmd_parser.parse_args() From 7ed99544e4cc1c09bd5abf9f54869c3122fa033b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 14:11:18 +1100 Subject: [PATCH 0599/3533] extmod/uasyncio: Add asyncio.current_task(). Matches CPython behavior. Fixes #6686 --- docs/library/uasyncio.rst | 4 ++++ extmod/uasyncio/core.py | 4 ++++ tests/extmod/uasyncio_current_task.py | 25 +++++++++++++++++++++++ tests/extmod/uasyncio_current_task.py.exp | 1 + 4 files changed, 34 insertions(+) create mode 100644 tests/extmod/uasyncio_current_task.py create mode 100644 tests/extmod/uasyncio_current_task.py.exp diff --git a/docs/library/uasyncio.rst b/docs/library/uasyncio.rst index a81e532d7fd25..3316f908d6364 100644 --- a/docs/library/uasyncio.rst +++ b/docs/library/uasyncio.rst @@ -40,6 +40,10 @@ Core functions Returns the corresponding `Task` object. +.. function:: current_task() + + Return the `Task` object associated with the currently running task. + .. function:: run(coro) Create a new task from the given coroutine and run it until it completes. diff --git a/extmod/uasyncio/core.py b/extmod/uasyncio/core.py index 6a84b0982c41f..d74763f6a696e 100644 --- a/extmod/uasyncio/core.py +++ b/extmod/uasyncio/core.py @@ -264,6 +264,10 @@ def get_event_loop(runq_len=0, waitq_len=0): return Loop +def current_task(): + return cur_task + + def new_event_loop(): global _task_queue, _io_queue # TaskQueue of Task instances diff --git a/tests/extmod/uasyncio_current_task.py b/tests/extmod/uasyncio_current_task.py new file mode 100644 index 0000000000000..3165a2cf16a7e --- /dev/null +++ b/tests/extmod/uasyncio_current_task.py @@ -0,0 +1,25 @@ +# Test current_task() function + +try: + import uasyncio as asyncio +except ImportError: + try: + import asyncio + except ImportError: + print("SKIP") + raise SystemExit + + +async def task(result): + result[0] = asyncio.current_task() + + +async def main(): + result = [None] + t = asyncio.create_task(task(result)) + await asyncio.sleep(0) + await asyncio.sleep(0) + print(t is result[0]) + + +asyncio.run(main()) diff --git a/tests/extmod/uasyncio_current_task.py.exp b/tests/extmod/uasyncio_current_task.py.exp new file mode 100644 index 0000000000000..0ca95142bb715 --- /dev/null +++ b/tests/extmod/uasyncio_current_task.py.exp @@ -0,0 +1 @@ +True From d2a34c62e73f63a7822c823b5523e5924c28831d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 16:18:35 +1100 Subject: [PATCH 0600/3533] stm32/uart: Add uart_set_baudrate function. This allows changing the baudrate of the UART without reinitialising it (reinitialising can lead to spurious characters sent on the TX line). Signed-off-by: Damien George --- ports/stm32/boards/stm32f0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32f7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32h7xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32l0xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32l4xx_hal_conf_base.h | 1 + ports/stm32/boards/stm32wbxx_hal_conf_base.h | 1 + ports/stm32/uart.c | 16 +++++++++++++--- ports/stm32/uart.h | 2 ++ 9 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ports/stm32/boards/stm32f0xx_hal_conf_base.h b/ports/stm32/boards/stm32f0xx_hal_conf_base.h index 5c6f31d1dd81b..70e6ccf758d38 100644 --- a/ports/stm32/boards/stm32f0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f0xx_hal_conf_base.h @@ -49,6 +49,7 @@ #include "stm32f0xx_hal_wwdg.h" #include "stm32f0xx_ll_adc.h" #include "stm32f0xx_ll_rtc.h" +#include "stm32f0xx_ll_usart.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32f4xx_hal_conf_base.h b/ports/stm32/boards/stm32f4xx_hal_conf_base.h index 057a9e81e4cac..134a30018ce36 100644 --- a/ports/stm32/boards/stm32f4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f4xx_hal_conf_base.h @@ -56,6 +56,7 @@ #include "stm32f4xx_ll_adc.h" #include "stm32f4xx_ll_pwr.h" #include "stm32f4xx_ll_rtc.h" +#include "stm32f4xx_ll_usart.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32f7xx_hal_conf_base.h b/ports/stm32/boards/stm32f7xx_hal_conf_base.h index 6e7dff3042ff3..efb15d471dcc7 100644 --- a/ports/stm32/boards/stm32f7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f7xx_hal_conf_base.h @@ -56,6 +56,7 @@ #include "stm32f7xx_ll_adc.h" #include "stm32f7xx_ll_pwr.h" #include "stm32f7xx_ll_rtc.h" +#include "stm32f7xx_ll_usart.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32h7xx_hal_conf_base.h b/ports/stm32/boards/stm32h7xx_hal_conf_base.h index a451cfde76162..c07ae93e37e04 100644 --- a/ports/stm32/boards/stm32h7xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32h7xx_hal_conf_base.h @@ -56,6 +56,7 @@ #include "stm32h7xx_ll_adc.h" #include "stm32h7xx_ll_pwr.h" #include "stm32h7xx_ll_rtc.h" +#include "stm32h7xx_ll_usart.h" // Enable various HAL modules #define HAL_ADC_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32l0xx_hal_conf_base.h b/ports/stm32/boards/stm32l0xx_hal_conf_base.h index 6b5ece766a83c..cc033666af8c3 100644 --- a/ports/stm32/boards/stm32l0xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l0xx_hal_conf_base.h @@ -48,6 +48,7 @@ #include "stm32l0xx_hal_wwdg.h" #include "stm32l0xx_ll_adc.h" #include "stm32l0xx_ll_rtc.h" +#include "stm32l0xx_ll_usart.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32l4xx_hal_conf_base.h b/ports/stm32/boards/stm32l4xx_hal_conf_base.h index 215e798b92f2e..8d77a80a7096e 100644 --- a/ports/stm32/boards/stm32l4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32l4xx_hal_conf_base.h @@ -52,6 +52,7 @@ #include "stm32l4xx_hal_wwdg.h" #include "stm32l4xx_ll_adc.h" #include "stm32l4xx_ll_rtc.h" +#include "stm32l4xx_ll_usart.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/boards/stm32wbxx_hal_conf_base.h b/ports/stm32/boards/stm32wbxx_hal_conf_base.h index 91309e286fa1e..72af0262a28b2 100644 --- a/ports/stm32/boards/stm32wbxx_hal_conf_base.h +++ b/ports/stm32/boards/stm32wbxx_hal_conf_base.h @@ -43,6 +43,7 @@ #include "stm32wbxx_hal_usart.h" #include "stm32wbxx_ll_adc.h" #include "stm32wbxx_ll_rtc.h" +#include "stm32wbxx_ll_usart.h" // Enable various HAL modules #define HAL_MODULE_ENABLED diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index b14a18c6ddfa6..8091ba05f99af 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -603,7 +603,7 @@ void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) { self->attached_to_repl = attached; } -uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { +uint32_t uart_get_source_freq(pyb_uart_obj_t *self) { uint32_t uart_clk = 0; #if defined(STM32F0) @@ -672,10 +672,20 @@ uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { } #endif + return uart_clk; +} + +uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { // This formula assumes UART_OVERSAMPLING_16 - uint32_t baudrate = uart_clk / self->uartx->BRR; + return uart_get_source_freq(self) / self->uartx->BRR; +} - return baudrate; +void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) { + LL_USART_SetBaudRate(self->uartx, uart_get_source_freq(self), + #if defined(STM32H7) || defined(STM32WB) + LL_USART_PRESCALER_DIV1, + #endif + LL_USART_OVERSAMPLING_16, baudrate); } mp_uint_t uart_rx_any(pyb_uart_obj_t *self) { diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index 9a38db593d4fc..570a79c932585 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -86,6 +86,8 @@ void uart_irq_handler(mp_uint_t uart_id); void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached); uint32_t uart_get_baudrate(pyb_uart_obj_t *self); +void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate); + mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj); bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout); int uart_rx_char(pyb_uart_obj_t *uart_obj); From bffb71f523e4bcc21b913af291deeb67091bed88 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 3 Feb 2021 16:19:47 +1100 Subject: [PATCH 0601/3533] stm32/mpbthciport: Only init the uart once, then use uart_set_baudrate. Signed-off-by: Damien George --- ports/stm32/mpbthciport.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index ee9f4e31eb2ba..5e51892e6f494 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -32,8 +32,6 @@ #include "pendsv.h" #include "lib/utils/mpirq.h" -#include "py/obj.h" - #if MICROPY_PY_BLUETOOTH #define DEBUG_printf(...) // printf("mpbthciport.c: " __VA_ARGS__) @@ -197,8 +195,18 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { mp_bluetooth_hci_uart_obj.timeout_char = 200; MP_STATE_PORT(pyb_uart_obj_all)[mp_bluetooth_hci_uart_obj.uart_id - 1] = &mp_bluetooth_hci_uart_obj; - // This also initialises the UART and adds the RXIDLE IRQ handler. - mp_bluetooth_hci_uart_set_baudrate(baudrate); + // Initialise the UART. + uart_init(&mp_bluetooth_hci_uart_obj, 115200, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); + uart_set_rxbuf(&mp_bluetooth_hci_uart_obj, sizeof(hci_uart_rxbuf), hci_uart_rxbuf); + + // Add IRQ handler for IDLE (i.e. packet finished). + uart_irq_config(&mp_bluetooth_hci_uart_obj, false); + mp_irq_init(&mp_bluetooth_hci_uart_irq_obj, &uart_irq_methods, MP_OBJ_FROM_PTR(&mp_bluetooth_hci_uart_obj)); + mp_bluetooth_hci_uart_obj.mp_irq_obj = &mp_bluetooth_hci_uart_irq_obj; + mp_bluetooth_hci_uart_obj.mp_irq_trigger = UART_FLAG_IDLE; + mp_bluetooth_hci_uart_irq_obj.handler = MP_OBJ_FROM_PTR(&mp_uart_interrupt_obj); + mp_bluetooth_hci_uart_irq_obj.ishard = true; + uart_irq_config(&mp_bluetooth_hci_uart_obj, true); return 0; } @@ -213,22 +221,7 @@ int mp_bluetooth_hci_uart_deinit(void) { int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate(%lu) (stm32)\n", baudrate); - if (!baudrate) { - return -1; - } - - uart_init(&mp_bluetooth_hci_uart_obj, baudrate, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); - uart_set_rxbuf(&mp_bluetooth_hci_uart_obj, sizeof(hci_uart_rxbuf), hci_uart_rxbuf); - - // Add IRQ handler for IDLE (i.e. packet finished). - uart_irq_config(&mp_bluetooth_hci_uart_obj, false); - mp_irq_init(&mp_bluetooth_hci_uart_irq_obj, &uart_irq_methods, MP_OBJ_FROM_PTR(&mp_bluetooth_hci_uart_obj)); - mp_bluetooth_hci_uart_obj.mp_irq_obj = &mp_bluetooth_hci_uart_irq_obj; - mp_bluetooth_hci_uart_obj.mp_irq_trigger = UART_FLAG_IDLE; - mp_bluetooth_hci_uart_irq_obj.handler = MP_OBJ_FROM_PTR(&mp_uart_interrupt_obj); - mp_bluetooth_hci_uart_irq_obj.ishard = true; - uart_irq_config(&mp_bluetooth_hci_uart_obj, true); - + uart_set_baudrate(&mp_bluetooth_hci_uart_obj, baudrate); return 0; } From 66098c09850ccc31b49b341e7eb7a5f8526e359d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 15:53:46 +1000 Subject: [PATCH 0602/3533] py,extmod: Add core cmake rule files. These allow a port to use cmake natively instead of make. Signed-off-by: Damien George --- extmod/extmod.cmake | 44 +++++++++++++++ py/mkrules.cmake | 128 ++++++++++++++++++++++++++++++++++++++++++++ py/py.cmake | 124 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 296 insertions(+) create mode 100644 extmod/extmod.cmake create mode 100644 py/mkrules.cmake create mode 100644 py/py.cmake diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake new file mode 100644 index 0000000000000..6668a2d8aa3cc --- /dev/null +++ b/extmod/extmod.cmake @@ -0,0 +1,44 @@ +# CMake fragment for MicroPython extmod component + +set(MICROPY_EXTMOD_DIR "${MICROPY_DIR}/extmod") +set(MICROPY_OOFATFS_DIR "${MICROPY_DIR}/lib/oofatfs") + +set(MICROPY_SOURCE_EXTMOD + ${MICROPY_EXTMOD_DIR}/machine_i2c.c + ${MICROPY_EXTMOD_DIR}/machine_mem.c + ${MICROPY_EXTMOD_DIR}/machine_pulse.c + ${MICROPY_EXTMOD_DIR}/machine_signal.c + ${MICROPY_EXTMOD_DIR}/machine_spi.c + ${MICROPY_EXTMOD_DIR}/modbluetooth.c + ${MICROPY_EXTMOD_DIR}/modbtree.c + ${MICROPY_EXTMOD_DIR}/modframebuf.c + ${MICROPY_EXTMOD_DIR}/moduasyncio.c + ${MICROPY_EXTMOD_DIR}/modubinascii.c + ${MICROPY_EXTMOD_DIR}/moducryptolib.c + ${MICROPY_EXTMOD_DIR}/moductypes.c + ${MICROPY_EXTMOD_DIR}/moduhashlib.c + ${MICROPY_EXTMOD_DIR}/moduheapq.c + ${MICROPY_EXTMOD_DIR}/modujson.c + ${MICROPY_EXTMOD_DIR}/modurandom.c + ${MICROPY_EXTMOD_DIR}/modure.c + ${MICROPY_EXTMOD_DIR}/moduselect.c + ${MICROPY_EXTMOD_DIR}/modussl_axtls.c + ${MICROPY_EXTMOD_DIR}/modussl_mbedtls.c + ${MICROPY_EXTMOD_DIR}/modutimeq.c + ${MICROPY_EXTMOD_DIR}/moduwebsocket.c + ${MICROPY_EXTMOD_DIR}/moduzlib.c + ${MICROPY_EXTMOD_DIR}/modwebrepl.c + ${MICROPY_EXTMOD_DIR}/uos_dupterm.c + ${MICROPY_EXTMOD_DIR}/utime_mphal.c + ${MICROPY_EXTMOD_DIR}/vfs.c + ${MICROPY_EXTMOD_DIR}/vfs_blockdev.c + ${MICROPY_EXTMOD_DIR}/vfs_fat.c + ${MICROPY_EXTMOD_DIR}/vfs_fat_diskio.c + ${MICROPY_EXTMOD_DIR}/vfs_fat_file.c + ${MICROPY_EXTMOD_DIR}/vfs_lfs.c + ${MICROPY_EXTMOD_DIR}/vfs_posix.c + ${MICROPY_EXTMOD_DIR}/vfs_posix_file.c + ${MICROPY_EXTMOD_DIR}/vfs_reader.c + ${MICROPY_EXTMOD_DIR}/virtpin.c + ${MICROPY_EXTMOD_DIR}/nimble/modbluetooth_nimble.c +) diff --git a/py/mkrules.cmake b/py/mkrules.cmake new file mode 100644 index 0000000000000..bdff385815a44 --- /dev/null +++ b/py/mkrules.cmake @@ -0,0 +1,128 @@ +# CMake fragment for MicroPython rules + +set(MICROPY_PY_QSTRDEFS "${MICROPY_PY_DIR}/qstrdefs.h") +set(MICROPY_GENHDR_DIR "${CMAKE_BINARY_DIR}/genhdr") +set(MICROPY_MPVERSION "${MICROPY_GENHDR_DIR}/mpversion.h") +set(MICROPY_MODULEDEFS "${MICROPY_GENHDR_DIR}/moduledefs.h") +set(MICROPY_QSTR_DEFS_LAST "${MICROPY_GENHDR_DIR}/qstr.i.last") +set(MICROPY_QSTR_DEFS_SPLIT "${MICROPY_GENHDR_DIR}/qstr.split") +set(MICROPY_QSTR_DEFS_COLLECTED "${MICROPY_GENHDR_DIR}/qstrdefs.collected.h") +set(MICROPY_QSTR_DEFS_PREPROCESSED "${MICROPY_GENHDR_DIR}/qstrdefs.preprocessed.h") +set(MICROPY_QSTR_DEFS_GENERATED "${MICROPY_GENHDR_DIR}/qstrdefs.generated.h") + +# Provide defaults for preprocessor flags if not already defined +if(NOT MICROPY_CPP_FLAGS) + get_target_property(MICROPY_CPP_INC ${MICROPY_TARGET} INCLUDE_DIRECTORIES) + get_target_property(MICROPY_CPP_DEF ${MICROPY_TARGET} COMPILE_DEFINITIONS) +endif() + +# Compute MICROPY_CPP_FLAGS for preprocessor +list(APPEND MICROPY_CPP_INC ${MICROPY_CPP_INC_EXTRA}) +list(APPEND MICROPY_CPP_DEF ${MICROPY_CPP_DEF_EXTRA}) +set(_prefix "-I") +foreach(_arg ${MICROPY_CPP_INC}) + list(APPEND MICROPY_CPP_FLAGS ${_prefix}${_arg}) +endforeach() +set(_prefix "-D") +foreach(_arg ${MICROPY_CPP_DEF}) + list(APPEND MICROPY_CPP_FLAGS ${_prefix}${_arg}) +endforeach() +list(APPEND MICROPY_CPP_FLAGS ${MICROPY_CPP_FLAGS_EXTRA}) + +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +target_sources(${MICROPY_TARGET} PRIVATE + ${MICROPY_MPVERSION} + ${MICROPY_QSTR_DEFS_GENERATED} +) + +# Command to force the build of another command + +add_custom_command( + OUTPUT MICROPY_FORCE_BUILD + COMMENT "" + COMMAND echo -n +) + +# Generate mpversion.h + +add_custom_command( + OUTPUT ${MICROPY_MPVERSION} + COMMAND ${CMAKE_COMMAND} -E make_directory ${MICROPY_GENHDR_DIR} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/py/makeversionhdr.py ${MICROPY_MPVERSION} + DEPENDS MICROPY_FORCE_BUILD +) + +# Generate moduledefs.h + +add_custom_command( + OUTPUT ${MICROPY_MODULEDEFS} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makemoduledefs.py --vpath="/" ${MICROPY_SOURCE_QSTR} > ${MICROPY_MODULEDEFS} + DEPENDS ${MICROPY_MPVERSION} + ${MICROPY_SOURCE_QSTR} +) + +# Generate qstrs + +# If any of the dependencies in this rule change then the C-preprocessor step must be run. +# It only needs to be passed the list of MICROPY_SOURCE_QSTR files that have changed since +# it was last run, but it looks like it's not possible to specify that with cmake. +add_custom_command( + OUTPUT ${MICROPY_QSTR_DEFS_LAST} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py pp ${CMAKE_C_COMPILER} -E output ${MICROPY_GENHDR_DIR}/qstr.i.last cflags ${MICROPY_CPP_FLAGS} -DNO_QSTR sources ${MICROPY_SOURCE_QSTR} + DEPENDS ${MICROPY_MODULEDEFS} + ${MICROPY_SOURCE_QSTR} + VERBATIM +) + +add_custom_command( + OUTPUT ${MICROPY_QSTR_DEFS_SPLIT} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py split qstr ${MICROPY_GENHDR_DIR}/qstr.i.last ${MICROPY_GENHDR_DIR}/qstr _ + COMMAND touch ${MICROPY_QSTR_DEFS_SPLIT} + DEPENDS ${MICROPY_QSTR_DEFS_LAST} + VERBATIM +) + +add_custom_command( + OUTPUT ${MICROPY_QSTR_DEFS_COLLECTED} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py cat qstr _ ${MICROPY_GENHDR_DIR}/qstr ${MICROPY_QSTR_DEFS_COLLECTED} + DEPENDS ${MICROPY_QSTR_DEFS_SPLIT} + VERBATIM +) + +add_custom_command( + OUTPUT ${MICROPY_QSTR_DEFS_PREPROCESSED} + COMMAND cat ${MICROPY_PY_QSTRDEFS} ${MICROPY_QSTR_DEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTR_DEFS_PREPROCESSED} + DEPENDS ${MICROPY_QSTR_DEFS_COLLECTED} + VERBATIM +) + +add_custom_command( + OUTPUT ${MICROPY_QSTR_DEFS_GENERATED} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdata.py ${MICROPY_QSTR_DEFS_PREPROCESSED} > ${MICROPY_QSTR_DEFS_GENERATED} + DEPENDS ${MICROPY_QSTR_DEFS_PREPROCESSED} + VERBATIM +) + +# Build frozen code if enabled + +if(MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_CONTENT "${CMAKE_BINARY_DIR}/frozen_content.c") + + target_sources(${MICROPY_TARGET} PRIVATE + ${MICROPY_FROZEN_CONTENT} + ) + + target_compile_definitions(${MICROPY_TARGET} PUBLIC + MICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool + MICROPY_MODULE_FROZEN_MPY=\(1\) + ) + + add_custom_command( + OUTPUT ${MICROPY_FROZEN_CONTENT} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/tools/makemanifest.py -o ${MICROPY_FROZEN_CONTENT} -v "MPY_DIR=${MICROPY_DIR}" -v "PORT_DIR=${MICROPY_PORT_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MICROPY_CROSS_FLAGS} ${MICROPY_FROZEN_MANIFEST} + DEPENDS MICROPY_FORCE_BUILD + ${MICROPY_QSTR_DEFS_GENERATED} + VERBATIM + ) +endif() diff --git a/py/py.cmake b/py/py.cmake new file mode 100644 index 0000000000000..52baaa8efc7fd --- /dev/null +++ b/py/py.cmake @@ -0,0 +1,124 @@ +# CMake fragment for MicroPython core py component + +set(MICROPY_PY_DIR "${MICROPY_DIR}/py") + +# All py/ source files +set(MICROPY_SOURCE_PY + ${MICROPY_PY_DIR}/argcheck.c + ${MICROPY_PY_DIR}/asmarm.c + ${MICROPY_PY_DIR}/asmbase.c + ${MICROPY_PY_DIR}/asmthumb.c + ${MICROPY_PY_DIR}/asmx64.c + ${MICROPY_PY_DIR}/asmx86.c + ${MICROPY_PY_DIR}/asmxtensa.c + ${MICROPY_PY_DIR}/bc.c + ${MICROPY_PY_DIR}/binary.c + ${MICROPY_PY_DIR}/builtinevex.c + ${MICROPY_PY_DIR}/builtinhelp.c + ${MICROPY_PY_DIR}/builtinimport.c + ${MICROPY_PY_DIR}/compile.c + ${MICROPY_PY_DIR}/emitbc.c + ${MICROPY_PY_DIR}/emitcommon.c + ${MICROPY_PY_DIR}/emitglue.c + ${MICROPY_PY_DIR}/emitinlinethumb.c + ${MICROPY_PY_DIR}/emitinlinextensa.c + ${MICROPY_PY_DIR}/emitnarm.c + ${MICROPY_PY_DIR}/emitnthumb.c + ${MICROPY_PY_DIR}/emitnx64.c + ${MICROPY_PY_DIR}/emitnx86.c + ${MICROPY_PY_DIR}/emitnxtensa.c + ${MICROPY_PY_DIR}/emitnxtensawin.c + ${MICROPY_PY_DIR}/formatfloat.c + ${MICROPY_PY_DIR}/frozenmod.c + ${MICROPY_PY_DIR}/gc.c + ${MICROPY_PY_DIR}/lexer.c + ${MICROPY_PY_DIR}/malloc.c + ${MICROPY_PY_DIR}/map.c + ${MICROPY_PY_DIR}/modarray.c + ${MICROPY_PY_DIR}/modbuiltins.c + ${MICROPY_PY_DIR}/modcmath.c + ${MICROPY_PY_DIR}/modcollections.c + ${MICROPY_PY_DIR}/modgc.c + ${MICROPY_PY_DIR}/modio.c + ${MICROPY_PY_DIR}/modmath.c + ${MICROPY_PY_DIR}/modmicropython.c + ${MICROPY_PY_DIR}/modstruct.c + ${MICROPY_PY_DIR}/modsys.c + ${MICROPY_PY_DIR}/modthread.c + ${MICROPY_PY_DIR}/moduerrno.c + ${MICROPY_PY_DIR}/mpprint.c + ${MICROPY_PY_DIR}/mpstate.c + ${MICROPY_PY_DIR}/mpz.c + ${MICROPY_PY_DIR}/nativeglue.c + ${MICROPY_PY_DIR}/nlr.c + ${MICROPY_PY_DIR}/nlrpowerpc.c + ${MICROPY_PY_DIR}/nlrsetjmp.c + ${MICROPY_PY_DIR}/nlrthumb.c + ${MICROPY_PY_DIR}/nlrx64.c + ${MICROPY_PY_DIR}/nlrx86.c + ${MICROPY_PY_DIR}/nlrxtensa.c + ${MICROPY_PY_DIR}/obj.c + ${MICROPY_PY_DIR}/objarray.c + ${MICROPY_PY_DIR}/objattrtuple.c + ${MICROPY_PY_DIR}/objbool.c + ${MICROPY_PY_DIR}/objboundmeth.c + ${MICROPY_PY_DIR}/objcell.c + ${MICROPY_PY_DIR}/objclosure.c + ${MICROPY_PY_DIR}/objcomplex.c + ${MICROPY_PY_DIR}/objdeque.c + ${MICROPY_PY_DIR}/objdict.c + ${MICROPY_PY_DIR}/objenumerate.c + ${MICROPY_PY_DIR}/objexcept.c + ${MICROPY_PY_DIR}/objfilter.c + ${MICROPY_PY_DIR}/objfloat.c + ${MICROPY_PY_DIR}/objfun.c + ${MICROPY_PY_DIR}/objgenerator.c + ${MICROPY_PY_DIR}/objgetitemiter.c + ${MICROPY_PY_DIR}/objint.c + ${MICROPY_PY_DIR}/objint_longlong.c + ${MICROPY_PY_DIR}/objint_mpz.c + ${MICROPY_PY_DIR}/objlist.c + ${MICROPY_PY_DIR}/objmap.c + ${MICROPY_PY_DIR}/objmodule.c + ${MICROPY_PY_DIR}/objnamedtuple.c + ${MICROPY_PY_DIR}/objnone.c + ${MICROPY_PY_DIR}/objobject.c + ${MICROPY_PY_DIR}/objpolyiter.c + ${MICROPY_PY_DIR}/objproperty.c + ${MICROPY_PY_DIR}/objrange.c + ${MICROPY_PY_DIR}/objreversed.c + ${MICROPY_PY_DIR}/objset.c + ${MICROPY_PY_DIR}/objsingleton.c + ${MICROPY_PY_DIR}/objslice.c + ${MICROPY_PY_DIR}/objstr.c + ${MICROPY_PY_DIR}/objstringio.c + ${MICROPY_PY_DIR}/objstrunicode.c + ${MICROPY_PY_DIR}/objtuple.c + ${MICROPY_PY_DIR}/objtype.c + ${MICROPY_PY_DIR}/objzip.c + ${MICROPY_PY_DIR}/opmethods.c + ${MICROPY_PY_DIR}/pairheap.c + ${MICROPY_PY_DIR}/parse.c + ${MICROPY_PY_DIR}/parsenum.c + ${MICROPY_PY_DIR}/parsenumbase.c + ${MICROPY_PY_DIR}/persistentcode.c + ${MICROPY_PY_DIR}/profile.c + ${MICROPY_PY_DIR}/pystack.c + ${MICROPY_PY_DIR}/qstr.c + ${MICROPY_PY_DIR}/reader.c + ${MICROPY_PY_DIR}/repl.c + ${MICROPY_PY_DIR}/ringbuf.c + ${MICROPY_PY_DIR}/runtime.c + ${MICROPY_PY_DIR}/runtime_utils.c + ${MICROPY_PY_DIR}/scheduler.c + ${MICROPY_PY_DIR}/scope.c + ${MICROPY_PY_DIR}/sequence.c + ${MICROPY_PY_DIR}/showbc.c + ${MICROPY_PY_DIR}/smallint.c + ${MICROPY_PY_DIR}/stackctrl.c + ${MICROPY_PY_DIR}/stream.c + ${MICROPY_PY_DIR}/unicode.c + ${MICROPY_PY_DIR}/vm.c + ${MICROPY_PY_DIR}/vstr.c + ${MICROPY_PY_DIR}/warning.c +) From 9b9088214687221d06f75a2932e9e0bd1c1b4103 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Sep 2020 15:55:55 +1000 Subject: [PATCH 0603/3533] esp32: Add support to build using IDF with cmake. This commit adds support for building the esp32 port with cmake, and in particular it builds MicroPython as a component within the ESP-IDF. Using cmake and the ESP-IDF build infrastructure makes it much easier to maintain the port, especially with the various new ESP32 MCUs and their required toolchains. Signed-off-by: Damien George --- ports/esp32/CMakeLists.txt | 38 ++++ .../esp32/boards/GENERIC/mpconfigboard.cmake | 2 + .../boards/GENERIC_D2WD/mpconfigboard.cmake | 6 + .../esp32/boards/GENERIC_D2WD/sdkconfig.board | 5 + .../boards/GENERIC_OTA/mpconfigboard.cmake | 6 + .../esp32/boards/GENERIC_OTA/sdkconfig.board | 2 + .../boards/GENERIC_SPIRAM/mpconfigboard.cmake | 6 + .../esp32/boards/TINYPICO/mpconfigboard.cmake | 8 + ports/esp32/boards/sdkconfig.base | 5 + ports/esp32/main/CMakeLists.txt | 168 ++++++++++++++++++ 10 files changed, 246 insertions(+) create mode 100644 ports/esp32/CMakeLists.txt create mode 100644 ports/esp32/boards/GENERIC/mpconfigboard.cmake create mode 100644 ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake create mode 100644 ports/esp32/boards/GENERIC_D2WD/sdkconfig.board create mode 100644 ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake create mode 100644 ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake create mode 100644 ports/esp32/boards/TINYPICO/mpconfigboard.cmake create mode 100644 ports/esp32/main/CMakeLists.txt diff --git a/ports/esp32/CMakeLists.txt b/ports/esp32/CMakeLists.txt new file mode 100644 index 0000000000000..fa419202f90d0 --- /dev/null +++ b/ports/esp32/CMakeLists.txt @@ -0,0 +1,38 @@ +# Top-level cmake file for building MicroPython on ESP32. + +cmake_minimum_required(VERSION 3.5) + +# Set the location of this port's directory. +set(MICROPY_PORT_DIR ${CMAKE_SOURCE_DIR}) + +# Set the board if it's not already set. +if(NOT MICROPY_BOARD) + set(MICROPY_BOARD GENERIC) +endif() + +# Set the board directory and check that it exists. +if(NOT MICROPY_BOARD_DIR) + set(MICROPY_BOARD_DIR ${MICROPY_PORT_DIR}/boards/${MICROPY_BOARD}) +endif() +if(NOT EXISTS ${MICROPY_BOARD_DIR}/mpconfigboard.cmake) + message(FATAL_ERROR "Invalid MICROPY_BOARD specified: ${MICROPY_BOARD}") +endif() + +# Define the output sdkconfig so it goes in the build directory. +set(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) + +# Include board config; this is expected to set SDKCONFIG_DEFAULTS (among other options). +include(${MICROPY_BOARD_DIR}/mpconfigboard.cmake) + +# Concatenate all sdkconfig files into a combined one for the IDF to use. +file(WRITE ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "") +foreach(SDKCONFIG_DEFAULT ${SDKCONFIG_DEFAULTS}) + file(READ ${SDKCONFIG_DEFAULT} CONTENTS) + file(APPEND ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "${CONTENTS}") +endforeach() +configure_file(${CMAKE_BINARY_DIR}/sdkconfig.combined.in ${CMAKE_BINARY_DIR}/sdkconfig.combined COPYONLY) +set(SDKCONFIG_DEFAULTS ${CMAKE_BINARY_DIR}/sdkconfig.combined) + +# Include main IDF cmake file and define the project. +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(micropython) diff --git a/ports/esp32/boards/GENERIC/mpconfigboard.cmake b/ports/esp32/boards/GENERIC/mpconfigboard.cmake new file mode 100644 index 0000000000000..8fea524555060 --- /dev/null +++ b/ports/esp32/boards/GENERIC/mpconfigboard.cmake @@ -0,0 +1,2 @@ +set(SDKCONFIG_DEFAULTS boards/sdkconfig.base) +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake new file mode 100644 index 0000000000000..4e23666f15714 --- /dev/null +++ b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake @@ -0,0 +1,6 @@ +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/GENERIC_D2WD/sdkconfig.board +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board b/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board new file mode 100644 index 0000000000000..367283ded3713 --- /dev/null +++ b/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board @@ -0,0 +1,5 @@ +CONFIG_ESPTOOLPY_FLASHMODE_DIO=y +CONFIG_ESPTOOLPY_FLASHFREQ_40M=y +CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-2MiB.csv" diff --git a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake new file mode 100644 index 0000000000000..7b1e1460054d7 --- /dev/null +++ b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake @@ -0,0 +1,6 @@ +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/GENERIC_OTA/sdkconfig.board +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/GENERIC_OTA/sdkconfig.board b/ports/esp32/boards/GENERIC_OTA/sdkconfig.board index b0ed171d811a5..ca1f4276f4560 100644 --- a/ports/esp32/boards/GENERIC_OTA/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_OTA/sdkconfig.board @@ -1,4 +1,6 @@ CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-ota.csv" # ESP-IDF v3: CONFIG_APP_ROLLBACK_ENABLE=y diff --git a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake new file mode 100644 index 0000000000000..bb441d9ebf543 --- /dev/null +++ b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake @@ -0,0 +1,6 @@ +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.spiram +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake new file mode 100644 index 0000000000000..990e3e035b9c0 --- /dev/null +++ b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake @@ -0,0 +1,8 @@ +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.240mhz + boards/sdkconfig.spiram + boards/TINYPICO/sdkconfig.board +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index 67e2424a1246d..de6e42c8f3139 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -51,3 +51,8 @@ CONFIG_PPP_SUPPORT=y CONFIG_PPP_PAP_SUPPORT=y CONFIG_PPP_CHAP_SUPPORT=y CONFIG_ULP_COPROC_ENABLED=y + +# For cmake build +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt new file mode 100644 index 0000000000000..a46e0112c9b88 --- /dev/null +++ b/ports/esp32/main/CMakeLists.txt @@ -0,0 +1,168 @@ +# Set location of base MicroPython directory. +get_filename_component(MICROPY_DIR ${PROJECT_DIR}/../.. ABSOLUTE) + +# Include core source components. +include(${MICROPY_DIR}/py/py.cmake) +include(${MICROPY_DIR}/extmod/extmod.cmake) + +set(MICROPY_SOURCE_EXTMOD_EXTRA + ${MICROPY_DIR}/extmod/modonewire.c +) + +set(MICROPY_SOURCE_LIB + ${MICROPY_DIR}/lib/littlefs/lfs1.c + ${MICROPY_DIR}/lib/littlefs/lfs1_util.c + ${MICROPY_DIR}/lib/littlefs/lfs2.c + ${MICROPY_DIR}/lib/littlefs/lfs2_util.c + ${MICROPY_DIR}/lib/mbedtls_errors/mp_mbedtls_errors.c + ${MICROPY_DIR}/lib/mp-readline/readline.c + ${MICROPY_DIR}/lib/netutils/netutils.c + ${MICROPY_DIR}/lib/oofatfs/ff.c + ${MICROPY_DIR}/lib/oofatfs/ffunicode.c + ${MICROPY_DIR}/lib/timeutils/timeutils.c + ${MICROPY_DIR}/lib/utils/interrupt_char.c + ${MICROPY_DIR}/lib/utils/stdout_helpers.c + ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c + ${MICROPY_DIR}/lib/utils/pyexec.c +) + +set(MICROPY_SOURCE_DRIVERS + ${MICROPY_DIR}/drivers/bus/softspi.c + ${MICROPY_DIR}/drivers/dht/dht.c +) + +set(MICROPY_SOURCE_PORT + ${PROJECT_DIR}/main.c + ${PROJECT_DIR}/uart.c + ${PROJECT_DIR}/gccollect.c + ${PROJECT_DIR}/mphalport.c + ${PROJECT_DIR}/fatfs_port.c + ${PROJECT_DIR}/help.c + ${PROJECT_DIR}/modutime.c + ${PROJECT_DIR}/moduos.c + ${PROJECT_DIR}/machine_timer.c + ${PROJECT_DIR}/machine_pin.c + ${PROJECT_DIR}/machine_touchpad.c + ${PROJECT_DIR}/machine_adc.c + ${PROJECT_DIR}/machine_dac.c + ${PROJECT_DIR}/machine_i2c.c + ${PROJECT_DIR}/machine_pwm.c + ${PROJECT_DIR}/machine_uart.c + ${PROJECT_DIR}/modmachine.c + ${PROJECT_DIR}/modnetwork.c + ${PROJECT_DIR}/network_lan.c + ${PROJECT_DIR}/network_ppp.c + ${PROJECT_DIR}/mpnimbleport.c + ${PROJECT_DIR}/modsocket.c + ${PROJECT_DIR}/modesp.c + ${PROJECT_DIR}/esp32_partition.c + ${PROJECT_DIR}/esp32_rmt.c + ${PROJECT_DIR}/esp32_ulp.c + ${PROJECT_DIR}/modesp32.c + ${PROJECT_DIR}/espneopixel.c + ${PROJECT_DIR}/machine_hw_spi.c + ${PROJECT_DIR}/machine_wdt.c + ${PROJECT_DIR}/mpthreadport.c + ${PROJECT_DIR}/machine_rtc.c + ${PROJECT_DIR}/machine_sdcard.c +) + +set(MICROPY_SOURCE_QSTR + ${MICROPY_SOURCE_PY} + ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_EXTMOD_EXTRA} + ${MICROPY_SOURCE_LIB} + ${MICROPY_SOURCE_PORT} +) + +set(IDF_COMPONENTS + app_update + bootloader_support + driver + esp32 + esp_common + esp_eth + esp_event + esp_ringbuf + esp_rom + esp_wifi + freertos + heap + log + lwip + mbedtls + mdns + newlib + nvs_flash + sdmmc + soc + spi_flash + tcpip_adapter + ulp + vfs + xtensa +) + +# Register the main IDF component. +idf_component_register( + SRCS + ${MICROPY_SOURCE_PY} + ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_EXTMOD_EXTRA} + ${MICROPY_SOURCE_LIB} + ${MICROPY_SOURCE_DRIVERS} + ${MICROPY_SOURCE_PORT} + INCLUDE_DIRS + ${MICROPY_DIR} + ${MICROPY_PORT_DIR} + ${MICROPY_BOARD_DIR} + ${CMAKE_BINARY_DIR} + REQUIRES + ${IDF_COMPONENTS} +) + +# Set the MicroPython target as the current (main) IDF component target. +set(MICROPY_TARGET ${COMPONENT_TARGET}) + +# Define mpy-cross flags, for use with frozen code. +set(MICROPY_CROSS_FLAGS -march=xtensawin) + +# Set compile options for this port. +target_compile_definitions(${MICROPY_TARGET} PUBLIC + MICROPY_ESP_IDF_4=1 + MICROPY_VFS_FAT=1 + MICROPY_VFS_LFS2=1 + FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\" + LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT + LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT +) + +# Disable some warnings to keep the build output clean. +target_compile_options(${MICROPY_TARGET} PUBLIC + -Wno-clobbered + -Wno-deprecated-declarations + -Wno-missing-field-initializers +) + +# Collect all of the include directories and compile definitions for the IDF components. +foreach(comp ${IDF_COMPONENTS}) + get_target_property(type __idf_${comp} TYPE) + set(_inc OFF) + set(_def OFF) + if(${type} STREQUAL STATIC_LIBRARY) + get_target_property(_inc __idf_${comp} INCLUDE_DIRECTORIES) + get_target_property(_def __idf_${comp} COMPILE_DEFINITIONS) + elseif(${type} STREQUAL INTERFACE_LIBRARY) + get_target_property(_inc __idf_${comp} INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(_def __idf_${comp} INTERFACE_COMPILE_DEFINITIONS) + endif() + if(_inc) + list(APPEND MICROPY_CPP_INC_EXTRA ${_inc}) + endif() + if(_def) + list(APPEND MICROPY_CPP_DEF_EXTRA ${_def}) + endif() +endforeach() + +# Include the main MicroPython cmake rules. +include(${MICROPY_DIR}/py/mkrules.cmake) From 9c2231f47ad1d30d3436561dfbeefd44c7a3e2f9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Oct 2020 18:06:09 +1100 Subject: [PATCH 0604/3533] esp32/esp32_rmt: Don't do unnecessary check for unsigned less than zero. Signed-off-by: Damien George --- ports/esp32/esp32_rmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 7971ca5d1c5af..b547af5539f34 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -209,7 +209,7 @@ STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *pos_args, mp_obj_t pulses = args[1].u_obj; mp_uint_t start = args[2].u_int; - if (start < 0 || start > 1) { + if (start > 1) { mp_raise_ValueError(MP_ERROR_TEXT("start must be 0 or 1")); } From 97072b72246abd69ee0d5892f8571bb4d13609a9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Oct 2020 14:28:17 +1100 Subject: [PATCH 0605/3533] esp32: Add explicit initialisers to silence compiler warnings. This makes no functional change. See similar commit 9aa58cf8bac353297ff5e7b4f3331e5618046095 Signed-off-by: Damien George --- ports/esp32/machine_i2c.c | 2 +- ports/esp32/modnetwork.c | 2 +- ports/esp32/mphalport.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 87b08fc9bc6e6..3993c7b52b9bb 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -120,7 +120,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_ // Parse args enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 64f1c91dc7922..d981b60805007 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -331,7 +331,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - wifi_config_t wifi_sta_config = {{{0}}}; + wifi_config_t wifi_sta_config = {0}; // configure any parameters that are given if (n_args > 1) { diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index ad571bf961886..54033826847ba 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -52,7 +52,7 @@ TaskHandle_t mp_main_task_handle; STATIC uint8_t stdin_ringbuf_array[256]; -ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array)}; +ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0}; // Check the ESP-IDF error code and raise an OSError if it's not ESP_OK. void check_esp_err(esp_err_t code) { From 9f035d6bb71e35ff9acd05b65dbc751887f03af2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Feb 2021 00:52:35 +1100 Subject: [PATCH 0606/3533] esp32: Remove traditional "make" capability. It's now replaced by cmake/idf.py. But a convenience Makefile is still provided with traditional targets like "all" and "deploy". Signed-off-by: Damien George --- ports/esp32/Makefile | 979 +----------------- ports/esp32/boards/GENERIC/mpconfigboard.mk | 1 - .../boards/GENERIC_D2WD/mpconfigboard.mk | 5 - .../esp32/boards/GENERIC_OTA/mpconfigboard.mk | 4 - .../boards/GENERIC_SPIRAM/mpconfigboard.mk | 2 - ports/esp32/boards/TINYPICO/mpconfigboard.mk | 8 - 6 files changed, 25 insertions(+), 974 deletions(-) delete mode 100644 ports/esp32/boards/GENERIC/mpconfigboard.mk delete mode 100644 ports/esp32/boards/GENERIC_D2WD/mpconfigboard.mk delete mode 100644 ports/esp32/boards/GENERIC_OTA/mpconfigboard.mk delete mode 100644 ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.mk delete mode 100644 ports/esp32/boards/TINYPICO/mpconfigboard.mk diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 756bc8f8940c5..1249795993614 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -1,972 +1,43 @@ -# Select the board to build for: if not given on the command line, -# then default to GENERIC. +# Makefile for MicroPython on ESP32. +# +# This is a simple, convenience wrapper around idf.py (which uses cmake). + +# Select the board to build for, defaulting to GENERIC. BOARD ?= GENERIC # If the build directory is not given, make it reflect the board name. BUILD ?= build-$(BOARD) -BOARD_DIR ?= boards/$(BOARD) -ifeq ($(wildcard $(BOARD_DIR)/.),) -$(error Invalid BOARD specified: $(BOARD_DIR)) -endif - -include ../../py/mkenv.mk - -# Optional (not currently used for ESP32) --include mpconfigport.mk - -ifneq ($(SDKCONFIG),) -$(error Use the BOARD variable instead of SDKCONFIG) -endif - -# Expected to set SDKCONFIG -include $(BOARD_DIR)/mpconfigboard.mk - -# qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h -QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h -QSTR_GLOBAL_REQUIREMENTS = $(SDKCONFIG_H) - -# MicroPython feature configurations -MICROPY_ROM_TEXT_COMPRESSION ?= 1 -MICROPY_PY_USSL = 0 -MICROPY_SSL_AXTLS = 0 -MICROPY_PY_BTREE = 1 -MICROPY_VFS_FAT = 1 -MICROPY_VFS_LFS2 = 1 - -FROZEN_MANIFEST ?= boards/manifest.py - -# include py core make definitions -include $(TOP)/py/py.mk - -GIT_SUBMODULES = lib/berkeley-db-1.xx - +# Device serial settings. PORT ?= /dev/ttyUSB0 BAUD ?= 460800 -FLASH_MODE ?= dio -FLASH_FREQ ?= 40m -FLASH_SIZE ?= 4MB -CROSS_COMPILE ?= xtensa-esp32-elf- -OBJDUMP = $(CROSS_COMPILE)objdump - -SDKCONFIG_COMBINED = $(BUILD)/sdkconfig.combined -SDKCONFIG_H = $(BUILD)/sdkconfig.h - -# The git hash of the currently supported ESP IDF version. -# These correspond to v3.3.2 and v4.0.1. -ESPIDF_SUPHASH_V3 := 9e70825d1e1cbf7988cf36981774300066580ea7 -ESPIDF_SUPHASH_V4 := 4c81978a3e2220674a432a588292a4c860eef27b - -define print_supported_git_hash -$(info Supported git hash (v3.3): $(ESPIDF_SUPHASH_V3)) -$(info Supported git hash (v4.0) (experimental): $(ESPIDF_SUPHASH_V4)) -endef - -# paths to ESP IDF and its components -ifeq ($(ESPIDF),) -ifneq ($(IDF_PATH),) -ESPIDF = $(IDF_PATH) -else -$(info The ESPIDF variable has not been set, please set it to the root of the esp-idf repository.) -$(info See README.md for installation instructions.) -dummy := $(call print_supported_git_hash) -$(error ESPIDF not set) -endif -endif - -ESPCOMP = $(ESPIDF)/components -ESPTOOL ?= $(ESPCOMP)/esptool_py/esptool/esptool.py -ESPCOMP_KCONFIGS = $(shell find $(ESPCOMP) -name Kconfig) -ESPCOMP_KCONFIGS_PROJBUILD = $(shell find $(ESPCOMP) -name Kconfig.projbuild) - -# verify the ESP IDF version -ESPIDF_CURHASH := $(shell git -C $(ESPIDF) show -s --pretty=format:'%H') - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) -$(info Building with ESP IDF v3) -else ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -$(info Building with ESP IDF v4) - -PYPARSING_VERSION = $(shell python3 -c 'import pyparsing; print(pyparsing.__version__)') -ifneq ($(PYPARSING_VERSION),2.3.1) -$(info ** ERROR **) -$(info EDP IDF requires pyparsing version less than 2.4) -$(info You will need to set up a Python virtual environment with pyparsing 2.3.1) -$(info Please see README.md for more information) -$(error Incorrect pyparsing version) -endif -else -$(info ** WARNING **) -$(info The git hash of ESP IDF does not match the supported version) -$(info The build may complete and the firmware may work but it is not guaranteed) -$(info ESP IDF path: $(ESPIDF)) -$(info Current git hash: $(ESPIDF_CURHASH)) -dummy := $(call print_supported_git_hash) -endif - -# pretty format of ESP IDF version, used internally by the IDF -IDF_VER := $(shell git -C $(ESPIDF) describe) - -ifeq ($(shell which $(CC) 2> /dev/null),) -$(info ** ERROR **) -$(info Cannot find C compiler $(CC)) -$(info Add the xtensa toolchain to your PATH. See README.md) -$(error C compiler missing) -endif - -# Support BLE by default. -# Can be explicitly disabled on the command line or board config. -MICROPY_PY_BLUETOOTH ?= 1 -ifeq ($(MICROPY_PY_BLUETOOTH),1) -SDKCONFIG += boards/sdkconfig.ble - -# Use NimBLE on ESP32. -MICROPY_BLUETOOTH_NIMBLE ?= 1 -# Use Nimble bindings, but ESP32 IDF provides the Nimble library. -MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY = 1 -include $(TOP)/extmod/nimble/nimble.mk -endif - -# include sdkconfig to get needed configuration values -include $(SDKCONFIG) - -################################################################################ -# Compiler and linker flags - -INC += -I. -INC += -I$(TOP) -INC += -I$(BUILD) - -INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include -INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include_bootloader -INC_ESPCOMP += -I$(ESPCOMP)/console -INC_ESPCOMP += -I$(ESPCOMP)/driver/include -INC_ESPCOMP += -I$(ESPCOMP)/driver/include/driver -INC_ESPCOMP += -I$(ESPCOMP)/efuse/include -INC_ESPCOMP += -I$(ESPCOMP)/efuse/esp32/include -INC_ESPCOMP += -I$(ESPCOMP)/esp32/include -INC_ESPCOMP += -I$(ESPCOMP)/espcoredump/include -INC_ESPCOMP += -I$(ESPCOMP)/soc/include -INC_ESPCOMP += -I$(ESPCOMP)/soc/esp32/include -INC_ESPCOMP += -I$(ESPCOMP)/heap/include -INC_ESPCOMP += -I$(ESPCOMP)/log/include -INC_ESPCOMP += -I$(ESPCOMP)/nvs_flash/include -INC_ESPCOMP += -I$(ESPCOMP)/freertos/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_ringbuf/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_event/include -INC_ESPCOMP += -I$(ESPCOMP)/tcpip_adapter/include -INC_ESPCOMP += -I$(ESPCOMP)/lwip/lwip/src/include -INC_ESPCOMP += -I$(ESPCOMP)/lwip/port/esp32/include -INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps -INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps/sntp -INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/mbedtls/include -INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/port/include -INC_ESPCOMP += -I$(ESPCOMP)/mdns/include -INC_ESPCOMP += -I$(ESPCOMP)/mdns/private_include -INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/include -INC_ESPCOMP += -I$(ESPCOMP)/ulp/include -INC_ESPCOMP += -I$(ESPCOMP)/vfs/include -INC_ESPCOMP += -I$(ESPCOMP)/xtensa-debug-module/include -INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include -INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/port/include -INC_ESPCOMP += -I$(ESPCOMP)/app_trace/include -INC_ESPCOMP += -I$(ESPCOMP)/app_update/include -INC_ESPCOMP += -I$(ESPCOMP)/pthread/include -INC_ESPCOMP += -I$(ESPCOMP)/smartconfig_ack/include -INC_ESPCOMP += -I$(ESPCOMP)/sdmmc/include - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -INC_ESPCOMP += -I$(ESPCOMP)/esp_common/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_eth/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_event/private_include -INC_ESPCOMP += -I$(ESPCOMP)/esp_rom/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/include -INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/esp32/include -INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps/sntp -INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/private_include -INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include/esp_supplicant -INC_ESPCOMP += -I$(ESPCOMP)/xtensa/include -INC_ESPCOMP += -I$(ESPCOMP)/xtensa/esp32/include -ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) -INC_ESPCOMP += -I$(ESPCOMP)/bt/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/common/osi/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/common/btc/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/common/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/port/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/include -INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/esp-hci/include -endif -else -INC_ESPCOMP += -I$(ESPCOMP)/ethernet/include -INC_ESPCOMP += -I$(ESPCOMP)/expat/expat/expat/lib -INC_ESPCOMP += -I$(ESPCOMP)/expat/port/include -INC_ESPCOMP += -I$(ESPCOMP)/json/include -INC_ESPCOMP += -I$(ESPCOMP)/json/port/include -INC_ESPCOMP += -I$(ESPCOMP)/micro-ecc/micro-ecc -INC_ESPCOMP += -I$(ESPCOMP)/nghttp/port/include -INC_ESPCOMP += -I$(ESPCOMP)/nghttp/nghttp2/lib/includes -ifeq ($(CONFIG_NIMBLE_ENABLED),y) -INC_ESPCOMP += -I$(ESPCOMP)/bt/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/porting/nimble/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/port/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/ans/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/bas/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/gap/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/gatt/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/ias/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/lls/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/services/tps/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/util/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/store/ram/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/nimble/host/store/config/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/porting/npl/freertos/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/nimble/ext/tinycrypt/include -INC_ESPCOMP += -I$(ESPCOMP)/nimble/esp-hci/include -endif -endif - -INC_NEWLIB += -I$(ESPCOMP)/newlib/platform_include -INC_NEWLIB += -I$(ESPCOMP)/newlib/include - -ifeq ($(MICROPY_PY_BLUETOOTH),1) -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 -endif - -# these flags are common to C and C++ compilation -CFLAGS_COMMON = -Os -ffunction-sections -fdata-sections -fstrict-volatile-bitfields \ - -mlongcalls -nostdlib \ - -Wall -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable \ - -Wno-error=unused-variable -Wno-error=deprecated-declarations \ - -DESP_PLATFORM - -CFLAGS_BASE = -std=gnu99 $(CFLAGS_COMMON) -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H -CFLAGS = $(CFLAGS_BASE) $(INC) $(INC_ESPCOMP) $(INC_NEWLIB) -CFLAGS += -DIDF_VER=\"$(IDF_VER)\" -CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) -CFLAGS += -I$(BOARD_DIR) - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -CFLAGS += -DMICROPY_ESP_IDF_4=1 -endif - -# this is what ESPIDF uses for c++ compilation -CXXFLAGS = -std=gnu++11 $(CFLAGS_COMMON) $(INC) $(INC_ESPCOMP) $(CXXFLAGS_MOD) - -LDFLAGS = -nostdlib -Map=$(@:.elf=.map) --cref -LDFLAGS += --gc-sections -static -EL -LDFLAGS += -u call_user_start_cpu0 -u uxTopUsedPriority -u ld_include_panic_highint_hdl -LDFLAGS += -u __cxa_guard_dummy # so that implementation of static guards is taken from cxx_guards.o instead of libstdc++.a -LDFLAGS += -L$(ESPCOMP)/esp32/ld -LDFLAGS += -L$(ESPCOMP)/esp_rom/esp32/ld -LDFLAGS += -T $(BUILD)/esp32_out.ld -LDFLAGS += -T $(BUILD)/esp32.project.ld -LDFLAGS += -T esp32.rom.ld -LDFLAGS += -T esp32.rom.libgcc.ld -LDFLAGS += -T esp32.peripherals.ld - -LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) -LIBSTDCXX_FILE_NAME = $(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a) - -# Debugging/Optimization -ifeq ($(DEBUG), 1) -CFLAGS += -g -COPT = -O0 -else -#CFLAGS += -fdata-sections -ffunction-sections -COPT += -Os -DNDEBUG -#LDFLAGS += --gc-sections -endif - -# Options for mpy-cross -MPY_CROSS_FLAGS += -march=xtensawin - -# Enable SPIRAM support if CONFIG_ESP32_SPIRAM_SUPPORT=y in sdkconfig -ifeq ($(CONFIG_ESP32_SPIRAM_SUPPORT),y) -CFLAGS_COMMON += -mfix-esp32-psram-cache-issue -LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc-psram-workaround.a $(ESPCOMP)/newlib/lib/libm-psram-workaround.a -else -# Additional newlib symbols that can only be used with spiram disabled. -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -LDFLAGS += -T esp32.rom.newlib-funcs.ld -LDFLAGS += -T esp32.rom.newlib-locale.ld -LDFLAGS += -T esp32.rom.newlib-data.ld -else -LDFLAGS += -T esp32.rom.spiram_incompatible_fns.ld -endif -LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc.a $(ESPCOMP)/newlib/lib/libm.a -endif - -################################################################################ -# List of MicroPython source and object files - -SRC_C = \ - main.c \ - uart.c \ - gccollect.c \ - mphalport.c \ - fatfs_port.c \ - help.c \ - modutime.c \ - moduos.c \ - machine_timer.c \ - machine_pin.c \ - machine_touchpad.c \ - machine_adc.c \ - machine_dac.c \ - machine_i2c.c \ - machine_pwm.c \ - machine_uart.c \ - modmachine.c \ - modnetwork.c \ - network_lan.c \ - network_ppp.c \ - mpnimbleport.c \ - modsocket.c \ - modesp.c \ - esp32_partition.c \ - esp32_rmt.c \ - esp32_ulp.c \ - modesp32.c \ - espneopixel.c \ - machine_hw_spi.c \ - machine_wdt.c \ - mpthreadport.c \ - machine_rtc.c \ - machine_sdcard.c \ - $(wildcard $(BOARD_DIR)/*.c) \ - $(SRC_MOD) - -SRC_CXX += \ - $(SRC_MOD_CXX) - -EXTMOD_SRC_C += $(addprefix extmod/,\ - modonewire.c \ - ) - -LIB_SRC_C = $(addprefix lib/,\ - mbedtls_errors/mp_mbedtls_errors.c \ - mp-readline/readline.c \ - netutils/netutils.c \ - timeutils/timeutils.c \ - utils/pyexec.c \ - utils/interrupt_char.c \ - utils/sys_stdio_mphal.c \ - ) - -DRIVERS_SRC_C = $(addprefix drivers/,\ - bus/softspi.c \ - dht/dht.c \ - ) - -OBJ_MP = -OBJ_MP += $(PY_O) -OBJ_MP += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ_MP += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) -OBJ_MP += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) -OBJ_MP += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) -OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) - -# Only enable this for the MicroPython source: ignore warnings from esp-idf. -$(OBJ_MP): CFLAGS += -Wdouble-promotion -Wfloat-conversion - -# List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_CXX) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C) -# Append any auto-generated sources that are needed by sources listed in SRC_QSTR -SRC_QSTR_AUTO_DEPS += - -################################################################################ -# Generate sdkconfig.h from sdkconfig - -$(SDKCONFIG_COMBINED): $(SDKCONFIG) - $(Q)$(MKDIR) -p $(dir $@) - $(Q)$(CAT) $^ > $@ - -$(SDKCONFIG_H): $(SDKCONFIG_COMBINED) - $(ECHO) "GEN $@" - $(Q)$(MKDIR) -p $(dir $@) - $(Q)$(PYTHON) $(ESPIDF)/tools/kconfig_new/confgen.py \ - --output header $@ \ - --config $< \ - --kconfig $(ESPIDF)/Kconfig \ - --env "IDF_TARGET=esp32" \ - --env "IDF_CMAKE=n" \ - --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ - --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ - --env "IDF_PATH=$(ESPIDF)" - $(Q)touch $@ - -$(HEADER_BUILD)/qstrdefs.generated.h: $(SDKCONFIG_H) $(BOARD_DIR)/mpconfigboard.h - -################################################################################ -# List of object files from the ESP32 IDF components - -ESPIDF_BOOTLOADER_SUPPORT_O = $(patsubst %.c,%.o,\ - $(filter-out $(ESPCOMP)/bootloader_support/src/bootloader_init.c,\ - $(wildcard $(ESPCOMP)/bootloader_support/src/*.c) \ - $(wildcard $(ESPCOMP)/bootloader_support/src/idf/*.c) \ - )) - -ESPIDF_DRIVER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/driver/*.c)) - -ESPIDF_EFUSE_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/efuse/esp32/*.c)\ - $(wildcard $(ESPCOMP)/efuse/src/*.c)\ - ) - -$(BUILD)/$(ESPCOMP)/esp32/dport_access.o: CFLAGS += -Wno-array-bounds -ESPIDF_ESP32_O = \ - $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/*.c)) \ - $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/hwcrypto/*.c)) \ - $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/esp32/*.S)) \ - -ESPIDF_ESP_RINGBUF_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_ringbuf/*.c)) - -ESPIDF_HEAP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/heap/*.c)) - -ESPIDF_SOC_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/soc/esp32/*.c) \ - $(wildcard $(ESPCOMP)/soc/src/*.c) \ - $(wildcard $(ESPCOMP)/soc/src/hal/*.c) \ - ) - -$(BUILD)/$(ESPCOMP)/cxx/cxx_guards.o: CXXFLAGS += -Wno-error=sign-compare -ESPIDF_CXX_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/cxx/*.cpp)) - -ESPIDF_PTHREAD_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/pthread/*.c)) - -# Assembler .S files need only basic flags, and in particular should not have -# -Os because that generates subtly different code. -# We also need custom CFLAGS for .c files because FreeRTOS has headers with -# generic names (eg queue.h) which can clash with other files in the port. -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. -I$(ESPCOMP)/xtensa/include -I$(ESPCOMP)/xtensa/esp32/include -I$(ESPCOMP)/esp_common/include -else -CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. -endif -$(BUILD)/$(ESPCOMP)/freertos/portasm.o: CFLAGS = $(CFLAGS_ASM) -$(BUILD)/$(ESPCOMP)/freertos/xtensa_context.o: CFLAGS = $(CFLAGS_ASM) -$(BUILD)/$(ESPCOMP)/freertos/xtensa_intr_asm.o: CFLAGS = $(CFLAGS_ASM) -$(BUILD)/$(ESPCOMP)/freertos/xtensa_vectors.o: CFLAGS = $(CFLAGS_ASM) -$(BUILD)/$(ESPCOMP)/freertos/xtensa_vector_defaults.o: CFLAGS = $(CFLAGS_ASM) -$(BUILD)/$(ESPCOMP)/freertos/%.o: CFLAGS = $(CFLAGS_BASE) -I. -I$(BUILD) $(INC_ESPCOMP) $(INC_NEWLIB) -I$(ESPCOMP)/freertos/include/freertos -D_ESP_FREERTOS_INTERNAL -ESPIDF_FREERTOS_O = \ - $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/freertos/*.c)) \ - $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/freertos/*.S)) \ - -ESPIDF_VFS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/vfs/*.c)) - -ESPIDF_LOG_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/log/*.c)) - -ESPIDF_XTENSA_DEBUG_MODULE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/xtensa-debug-module/*.c)) - -ESPIDF_TCPIP_ADAPTER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/tcpip_adapter/*.c)) - -ESPIDF_APP_TRACE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_trace/*.c)) -ESPIDF_APP_UPDATE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_update/*.c)) +PYTHON ?= python3 -ESPIDF_NEWLIB_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/newlib/*.c)) - -$(BUILD)/$(ESPCOMP)/nvs_flash/src/nvs_api.o: CXXFLAGS += -Wno-error=sign-compare -ESPIDF_NVS_FLASH_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/nvs_flash/src/*.cpp)) - -ESPIDF_SMARTCONFIG_ACK_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/smartconfig_ack/*.c)) - -ESPIDF_SPI_FLASH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/spi_flash/*.c)) - -ESPIDF_ULP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/ulp/*.c)) - -$(BUILD)/$(ESPCOMP)/lwip/%.o: CFLAGS += -Wno-address -Wno-unused-variable -Wno-unused-but-set-variable -ESPIDF_LWIP_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/lwip/apps/dhcpserver/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/api/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/apps/sntp/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*.c) \ - $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*/*.c) \ - $(wildcard $(ESPCOMP)/lwip/port/esp32/*.c) \ - $(wildcard $(ESPCOMP)/lwip/port/esp32/*/*.c) \ - ) - -# Mbedtls source files, exclude error.c in favor of lib/mbedtls_errors/mp_mbedtls_errors.c -ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o, $(filter-out %/error.c,\ - $(wildcard $(ESPCOMP)/mbedtls/mbedtls/library/*.c) \ - $(wildcard $(ESPCOMP)/mbedtls/port/*.c) \ - $(wildcard $(ESPCOMP)/mbedtls/port/esp32/*.c) \ - )) - -ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c)) - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DCONFIG_WPA3_SAE -DCONFIG_IEEE80211W -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing -I$(ESPCOMP)/wpa_supplicant/src -Wno-implicit-function-declaration -else -$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DEMBEDDED_SUPP -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_MSCHAPv2 -DEAP_TTLS -DEAP_TLS -DEAP_PEAP -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DALLOW_EVEN_MOD -D__ets__ -Wno-strict-aliasing -endif -ESPIDF_WPA_SUPPLICANT_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/wpa_supplicant/port/*.c) \ - $(wildcard $(ESPCOMP)/wpa_supplicant/src/*/*.c) \ - ) - -ESPIDF_SDMMC_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/sdmmc/*.c)) - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -ESPIDF_ESP_COMMON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_common/src/*.c)) - -ESPIDF_ESP_EVENT_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_event/*.c)) - -ESPIDF_ESP_WIFI_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_wifi/src/*.c)) - -ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) -ESPIDF_BT_NIMBLE_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/bt/controller/*.c) \ - $(wildcard $(ESPCOMP)/bt/common/btc/core/*.c) \ - $(wildcard $(ESPCOMP)/bt/common/osi/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/esp-hci/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_config.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_nvs.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/src/*.c) \ - $(wildcard $(ESPCOMP)/bt/host/nimble/port/src/*.c) \ - ) -endif - -$(BUILD)/$(ESPCOMP)/esp_eth/src/esp_eth_mac_dm9051.o: CFLAGS += -fno-strict-aliasing -ESPIDF_ESP_ETH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_eth/src/*.c)) - -ESPIDF_XTENSA_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/xtensa/*.c) \ - $(wildcard $(ESPCOMP)/xtensa/esp32/*.c) \ - ) -else -ESPIDF_JSON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/json/cJSON/cJSON*.c)) - -ESPIDF_ETHERNET_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/ethernet/*.c) \ - $(wildcard $(ESPCOMP)/ethernet/eth_phy/*.c) \ - ) - -ifeq ($(CONFIG_NIMBLE_ENABLED),y) -ESPIDF_BT_NIMBLE_O = $(patsubst %.c,%.o,\ - $(wildcard $(ESPCOMP)/bt/*.c) \ - $(wildcard $(ESPCOMP)/nimble/esp-hci/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/ext/tinycrypt/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/ans/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/bas/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/gap/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/gatt/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/ias/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/lls/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/services/tps/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/store/config/src/ble_store_config.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/store/config/src/ble_store_nvs.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/store/ram/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/host/util/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/nimble/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/porting/nimble/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/nimble/porting/npl/freertos/src/*.c) \ - $(wildcard $(ESPCOMP)/nimble/port/src/*.c) \ - ) -endif -endif - -OBJ_ESPIDF = -LIB_ESPIDF = -BUILD_ESPIDF_LIB = $(BUILD)/esp-idf - -define gen_espidf_lib_rule -OBJ_ESPIDF += $(addprefix $$(BUILD)/,$(2)) -LIB_ESPIDF += $(1) -$(BUILD_ESPIDF_LIB)/$(1)/lib$(1).a: $(addprefix $$(BUILD)/,$(2)) - $(ECHO) "AR $$@" - $(Q)$(AR) cru $$@ $$^ -endef - -$(eval $(call gen_espidf_lib_rule,bootloader_support,$(ESPIDF_BOOTLOADER_SUPPORT_O))) -$(eval $(call gen_espidf_lib_rule,driver,$(ESPIDF_DRIVER_O))) -$(eval $(call gen_espidf_lib_rule,efuse,$(ESPIDF_EFUSE_O))) -$(eval $(call gen_espidf_lib_rule,esp32,$(ESPIDF_ESP32_O))) -$(eval $(call gen_espidf_lib_rule,esp_ringbuf,$(ESPIDF_ESP_RINGBUF_O))) -$(eval $(call gen_espidf_lib_rule,heap,$(ESPIDF_HEAP_O))) -$(eval $(call gen_espidf_lib_rule,soc,$(ESPIDF_SOC_O))) -$(eval $(call gen_espidf_lib_rule,cxx,$(ESPIDF_CXX_O))) -$(eval $(call gen_espidf_lib_rule,pthread,$(ESPIDF_PTHREAD_O))) -$(eval $(call gen_espidf_lib_rule,freertos,$(ESPIDF_FREERTOS_O))) -$(eval $(call gen_espidf_lib_rule,vfs,$(ESPIDF_VFS_O))) -$(eval $(call gen_espidf_lib_rule,json,$(ESPIDF_JSON_O))) -$(eval $(call gen_espidf_lib_rule,log,$(ESPIDF_LOG_O))) -$(eval $(call gen_espidf_lib_rule,xtensa-debug-module,$(ESPIDF_XTENSA_DEBUG_MODULE_O))) -$(eval $(call gen_espidf_lib_rule,tcpip_adapter,$(ESPIDF_TCPIP_ADAPTER_O))) -$(eval $(call gen_espidf_lib_rule,app_trace,$(ESPIDF_APP_TRACE_O))) -$(eval $(call gen_espidf_lib_rule,app_update,$(ESPIDF_APP_UPDATE_O))) -$(eval $(call gen_espidf_lib_rule,newlib,$(ESPIDF_NEWLIB_O))) -$(eval $(call gen_espidf_lib_rule,nvs_flash,$(ESPIDF_NVS_FLASH_O))) -$(eval $(call gen_espidf_lib_rule,smartconfig_ack,$(ESPIDF_SMARTCONFIG_ACK_O))) -$(eval $(call gen_espidf_lib_rule,spi_flash,$(ESPIDF_SPI_FLASH_O))) -$(eval $(call gen_espidf_lib_rule,ulp,$(ESPIDF_ULP_O))) -$(eval $(call gen_espidf_lib_rule,lwip,$(ESPIDF_LWIP_O))) -$(eval $(call gen_espidf_lib_rule,mbedtls,$(ESPIDF_MBEDTLS_O))) -$(eval $(call gen_espidf_lib_rule,mdns,$(ESPIDF_MDNS_O))) -$(eval $(call gen_espidf_lib_rule,wpa_supplicant,$(ESPIDF_WPA_SUPPLICANT_O))) -$(eval $(call gen_espidf_lib_rule,sdmmc,$(ESPIDF_SDMMC_O))) -$(eval $(call gen_espidf_lib_rule,bt_nimble,$(ESPIDF_BT_NIMBLE_O))) - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -$(eval $(call gen_espidf_lib_rule,esp_common,$(ESPIDF_ESP_COMMON_O))) -$(eval $(call gen_espidf_lib_rule,esp_event,$(ESPIDF_ESP_EVENT_O))) -$(eval $(call gen_espidf_lib_rule,esp_wifi,$(ESPIDF_ESP_WIFI_O))) -$(eval $(call gen_espidf_lib_rule,esp_eth,$(ESPIDF_ESP_ETH_O))) -$(eval $(call gen_espidf_lib_rule,xtensa,$(ESPIDF_XTENSA_O))) -else -$(eval $(call gen_espidf_lib_rule,ethernet,$(ESPIDF_ETHERNET_O))) -endif - -# Create all destination build dirs before compiling IDF source -OBJ_ESPIDF_DIRS = $(sort $(dir $(OBJ_ESPIDF))) $(BUILD_ESPIDF_LIB) $(addprefix $(BUILD_ESPIDF_LIB)/,$(LIB_ESPIDF)) -$(OBJ_ESPIDF): | $(OBJ_ESPIDF_DIRS) -$(OBJ_ESPIDF_DIRS): - $(MKDIR) -p $@ - -# Make all IDF object files depend on sdkconfig -$(OBJ_ESPIDF): $(SDKCONFIG_H) - -# Add all IDF components to the set of libraries -LIB = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) - -################################################################################ -# ESP IDF ldgen - -LDGEN_FRAGMENTS = $(shell find $(ESPCOMP) -name "*.lf") - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) - -LDGEN_LIBRARIES=$(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) - -$(BUILD_ESPIDF_LIB)/ldgen_libraries: $(LDGEN_LIBRARIES) $(ESPIDF)/make/ldgen.mk - printf "$(foreach library,$(LDGEN_LIBRARIES),$(library)\n)" > $(BUILD_ESPIDF_LIB)/ldgen_libraries - -$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(BUILD_ESPIDF_LIB)/ldgen_libraries - $(ECHO) "GEN $@" - $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ - --input $< \ - --output $@ \ - --config $(SDKCONFIG_COMBINED) \ - --kconfig $(ESPIDF)/Kconfig \ - --fragments $(LDGEN_FRAGMENTS) \ - --libraries-file $(BUILD_ESPIDF_LIB)/ldgen_libraries \ - --env "IDF_TARGET=esp32" \ - --env "IDF_CMAKE=n" \ - --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ - --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ - --env "IDF_PATH=$(ESPIDF)" \ - --objdump $(OBJDUMP) - -else - -LDGEN_SECTIONS_INFO = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a.sections_info) -LDGEN_SECTION_INFOS = $(BUILD_ESPIDF_LIB)/ldgen.section_infos - -define gen_sections_info_rule -$(1).sections_info: $(1) - $(ECHO) "GEN $(1).sections_info" - $(Q)$(OBJDUMP) -h $(1) > $(1).sections_info -endef - -$(eval $(foreach lib,$(LIB_ESPIDF),$(eval $(call gen_sections_info_rule,$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a)))) - -$(LDGEN_SECTION_INFOS): $(LDGEN_SECTIONS_INFO) $(ESPIDF)/make/ldgen.mk - $(Q)printf "$(foreach info,$(LDGEN_SECTIONS_INFO),$(info)\n)" > $@ - -$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(LDGEN_SECTION_INFOS) - $(ECHO) "GEN $@" - $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ - --input $< \ - --output $@ \ - --config $(SDKCONFIG_COMBINED) \ - --kconfig $(ESPIDF)/Kconfig \ - --fragments $(LDGEN_FRAGMENTS) \ - --sections $(LDGEN_SECTION_INFOS) \ - --env "IDF_TARGET=esp32" \ - --env "IDF_CMAKE=n" \ - --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ - --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ - --env "IDF_PATH=$(ESPIDF)" - -endif +GIT_SUBMODULES = lib/berkeley-db-1.xx -################################################################################ -# Main targets +.PHONY: all clean deploy erase submodules FORCE -all: $(BUILD)/firmware.bin +IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -B $(BUILD) -.PHONY: idf-version deploy erase +all: + idf.py $(IDFPY_FLAGS) build + @$(PYTHON) makeimg.py \ + $(BUILD)/bootloader/bootloader.bin \ + $(BUILD)/partition_table/partition-table.bin \ + $(BUILD)/micropython.bin \ + $(BUILD)/firmware.bin -idf-version: - $(ECHO) "ESP IDF supported hash: $(ESPIDF_SUPHASH)" +$(BUILD)/bootloader/bootloader.bin $(BUILD)/partition_table/partition -table.bin $(BUILD)/micropython.bin: FORCE -$(BUILD)/firmware.bin: $(BUILD)/bootloader.bin $(BUILD)/partitions.bin $(BUILD)/application.bin - $(ECHO) "Create $@" - $(Q)$(PYTHON) makeimg.py $^ $@ +clean: + idf.py $(IDFPY_FLAGS) fullclean -deploy: $(BUILD)/firmware.bin - $(ECHO) "Writing $^ to the board" - $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) write_flash -z --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) 0x1000 $^ +deploy: + idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) flash erase: - $(ECHO) "Erasing flash" - $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) erase_flash - -################################################################################ -# Declarations to build the application - -OBJ = $(OBJ_MP) - -APP_LD_ARGS = -APP_LD_ARGS += $(LDFLAGS_MOD) -APP_LD_ARGS += $(addprefix -T,$(LD_FILES)) -APP_LD_ARGS += --start-group -APP_LD_ARGS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc -APP_LD_ARGS += -L$(dir $(LIBSTDCXX_FILE_NAME)) -lstdc++ -APP_LD_ARGS += $(LIBC_LIBM) -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -APP_LD_ARGS += -L$(ESPCOMP)/xtensa/esp32 -lhal -APP_LD_ARGS += -L$(ESPCOMP)/bt/controller/lib -lbtdm_app -APP_LD_ARGS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lsmartconfig -lcoexist -else -APP_LD_ARGS += $(ESPCOMP)/esp32/libhal.a -APP_LD_ARGS += -L$(ESPCOMP)/bt/lib -lbtdm_app -APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lwpa -lsmartconfig -lcoexist -lwps -lwpa2 -endif -APP_LD_ARGS += $(OBJ) -APP_LD_ARGS += $(LIB) -APP_LD_ARGS += --end-group - -$(BUILD)/esp32_out.ld: $(SDKCONFIG_H) - $(Q)$(CC) -I$(BUILD) -C -P -x c -E $(ESPCOMP)/esp32/ld/esp32.ld -o $@ - -$(BUILD)/application.bin: $(BUILD)/application.elf - $(ECHO) "Create $@" - $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< - -$(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.project.ld - $(ECHO) "LINK $@" - $(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS) - $(Q)$(SIZE) $@ - -################################################################################ -# Declarations to build the bootloader - -BOOTLOADER_LIB_DIR = $(BUILD)/bootloader -BOOTLOADER_LIB_ALL = - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp_rom/include -Wno-error=format \ - -I$(ESPCOMP)/esp_common/include \ - -I$(ESPCOMP)/xtensa/include \ - -I$(ESPCOMP)/xtensa/esp32/include -else -$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/micro-ecc/micro-ecc -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp32 -Wno-error=format -endif - -# libbootloader_support.a -BOOTLOADER_LIB_ALL += bootloader_support -BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - bootloader_support/src/bootloader_clock.o \ - bootloader_support/src/bootloader_common.o \ - bootloader_support/src/bootloader_flash.o \ - bootloader_support/src/bootloader_flash_config.o \ - bootloader_support/src/bootloader_init.o \ - bootloader_support/src/bootloader_random.o \ - bootloader_support/src/bootloader_utility.o \ - bootloader_support/src/flash_qio_mode.o \ - bootloader_support/src/esp_image_format.o \ - bootloader_support/src/flash_encrypt.o \ - bootloader_support/src/flash_partitions.o \ - ) - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - bootloader_support/src/esp32/bootloader_sha.o \ - bootloader_support/src/bootloader_flash_config.o \ - bootloader_support/src/esp32/secure_boot.o \ - ) -else -BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - bootloader_support/src/bootloader_sha.o \ - bootloader_support/src/secure_boot_signatures.o \ - bootloader_support/src/secure_boot.o \ - ) -endif - -$(BOOTLOADER_LIB_DIR)/libbootloader_support.a: $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ - -# liblog.a -BOOTLOADER_LIB_ALL += log -BOOTLOADER_LIB_LOG_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - log/log.o \ - ) -$(BOOTLOADER_LIB_DIR)/liblog.a: $(BOOTLOADER_LIB_LOG_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ - -# libspi_flash.a -BOOTLOADER_LIB_ALL += spi_flash -BOOTLOADER_LIB_SPI_FLASH_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - spi_flash/spi_flash_rom_patch.o \ - ) -$(BOOTLOADER_LIB_DIR)/libspi_flash.a: $(BOOTLOADER_LIB_SPI_FLASH_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ - -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) -# libmicro-ecc.a -BOOTLOADER_LIB_ALL += micro-ecc -BOOTLOADER_LIB_MICRO_ECC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - micro-ecc/micro-ecc/uECC.o \ - ) -$(BOOTLOADER_LIB_DIR)/libmicro-ecc.a: $(BOOTLOADER_LIB_MICRO_ECC_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ -endif - -# libsoc.a -$(BUILD)/bootloader/$(ESPCOMP)/soc/esp32/rtc_clk.o: CFLAGS += -fno-jump-tables -fno-tree-switch-conversion -BOOTLOADER_LIB_ALL += soc -BOOTLOADER_LIB_SOC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/soc/,\ - esp32/cpu_util.o \ - esp32/gpio_periph.o \ - esp32/rtc_clk.o \ - esp32/rtc_clk_init.o \ - esp32/rtc_init.o \ - esp32/rtc_periph.o \ - esp32/rtc_pm.o \ - esp32/rtc_sleep.o \ - esp32/rtc_time.o \ - esp32/rtc_wdt.o \ - esp32/sdio_slave_periph.o \ - esp32/sdmmc_periph.o \ - esp32/soc_memory_layout.o \ - esp32/spi_periph.o \ - src/memory_layout_utils.o \ - ) -$(BOOTLOADER_LIB_DIR)/libsoc.a: $(BOOTLOADER_LIB_SOC_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ - -# libmain.a -BOOTLOADER_LIB_ALL += main -BOOTLOADER_LIB_MAIN_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ - bootloader/subproject/main/bootloader_start.o \ - ) -$(BOOTLOADER_LIB_DIR)/libmain.a: $(BOOTLOADER_LIB_MAIN_OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) cr $@ $^ - -# all objects files -BOOTLOADER_OBJ_ALL = \ - $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) \ - $(BOOTLOADER_LIB_LOG_OBJ) \ - $(BOOTLOADER_LIB_SPI_FLASH_OBJ) \ - $(BOOTLOADER_LIB_MICRO_ECC_OBJ) \ - $(BOOTLOADER_LIB_SOC_OBJ) \ - $(BOOTLOADER_LIB_MAIN_OBJ) - -$(BOOTLOADER_OBJ_ALL): $(SDKCONFIG_H) - -BOOTLOADER_LIBS = -BOOTLOADER_LIBS += -Wl,--start-group -BOOTLOADER_LIBS += $(BOOTLOADER_OBJ) -BOOTLOADER_LIBS += -L$(BUILD)/bootloader $(addprefix -l,$(BOOTLOADER_LIB_ALL)) -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -BOOTLOADER_LIBS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lrtc -else -BOOTLOADER_LIBS += -L$(ESPCOMP)/esp32/lib -lrtc -endif -BOOTLOADER_LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc -BOOTLOADER_LIBS += -Wl,--end-group - -BOOTLOADER_LDFLAGS = -BOOTLOADER_LDFLAGS += -nostdlib -BOOTLOADER_LDFLAGS += -L$(ESPIDF)/lib -BOOTLOADER_LDFLAGS += -L$(ESPIDF)/ld -BOOTLOADER_LDFLAGS += -u call_user_start_cpu0 -BOOTLOADER_LDFLAGS += -Wl,--gc-sections -BOOTLOADER_LDFLAGS += -static -BOOTLOADER_LDFLAGS += -Wl,-EL -BOOTLOADER_LDFLAGS += -Wl,-Map=$(@:.elf=.map) -Wl,--cref -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.ld -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.rom.ld -ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.ld -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld -else -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.ld -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.spiram_incompatible_fns.ld -endif -BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.peripherals.ld - -BOOTLOADER_OBJ_DIRS = $(sort $(dir $(BOOTLOADER_OBJ_ALL))) -$(BOOTLOADER_OBJ_ALL): | $(BOOTLOADER_OBJ_DIRS) -$(BOOTLOADER_OBJ_DIRS): - $(MKDIR) -p $@ - -$(BUILD)/bootloader/%.o: %.c - $(call compile_c) - -$(BUILD)/bootloader.bin: $(BUILD)/bootloader.elf - $(ECHO) "Create $@" - $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< - -$(BUILD)/bootloader.elf: $(BOOTLOADER_OBJ) $(addprefix $(BOOTLOADER_LIB_DIR)/lib,$(addsuffix .a,$(BOOTLOADER_LIB_ALL))) - $(ECHO) "LINK $@" - $(Q)$(CC) $(BOOTLOADER_LDFLAGS) -o $@ $(BOOTLOADER_LIBS) - -################################################################################ -# Declarations to build the partitions - -PYTHON2 ?= python2 - -# Can be overriden by mkconfigboard.mk. -PART_SRC ?= partitions.csv - -$(BUILD)/partitions.bin: $(PART_SRC) - $(ECHO) "Create $@" - $(Q)$(PYTHON2) $(ESPCOMP)/partition_table/gen_esp32part.py -q $< $@ - -################################################################################ + idf.py $(IDFPY_FLAGS) erase_flash -include $(TOP)/py/mkrules.mk +submodules: + git submodule update --init $(addprefix ../../,$(GIT_SUBMODULES)) diff --git a/ports/esp32/boards/GENERIC/mpconfigboard.mk b/ports/esp32/boards/GENERIC/mpconfigboard.mk deleted file mode 100644 index fc49d2a8c256c..0000000000000 --- a/ports/esp32/boards/GENERIC/mpconfigboard.mk +++ /dev/null @@ -1 +0,0 @@ -SDKCONFIG += boards/sdkconfig.base diff --git a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.mk b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.mk deleted file mode 100644 index 65de5dcd08e55..0000000000000 --- a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.mk +++ /dev/null @@ -1,5 +0,0 @@ -SDKCONFIG += boards/sdkconfig.base -PART_SRC = partitions-2MiB.csv -FLASH_SIZE = 2MB -FLASH_MODE = dio -FLASH_FREQ = 40m diff --git a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.mk b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.mk deleted file mode 100644 index db6492cac2b84..0000000000000 --- a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.mk +++ /dev/null @@ -1,4 +0,0 @@ -SDKCONFIG += boards/sdkconfig.base -SDKCONFIG += boards/GENERIC_OTA/sdkconfig.board - -PART_SRC = partitions-ota.csv diff --git a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.mk b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.mk deleted file mode 100644 index 59aa75f8571b9..0000000000000 --- a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.mk +++ /dev/null @@ -1,2 +0,0 @@ -SDKCONFIG += boards/sdkconfig.base -SDKCONFIG += boards/sdkconfig.spiram diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.mk b/ports/esp32/boards/TINYPICO/mpconfigboard.mk deleted file mode 100644 index 5c96ec45a79e6..0000000000000 --- a/ports/esp32/boards/TINYPICO/mpconfigboard.mk +++ /dev/null @@ -1,8 +0,0 @@ -FLASH_FREQ = 80m - -SDKCONFIG += boards/sdkconfig.base -SDKCONFIG += boards/sdkconfig.240mhz -SDKCONFIG += boards/sdkconfig.spiram -SDKCONFIG += boards/TINYPICO/sdkconfig.board - -FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py From 26b17fd28a359567788b4bd2fe4a7bbac4c9a69d Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Feb 2021 00:56:58 +1100 Subject: [PATCH 0607/3533] esp32/boards: Remove old IDF v3 sdkconfig values. IDF v3 is no longer supported with the move to cmake. Signed-off-by: Damien George --- ports/esp32/boards/GENERIC_OTA/sdkconfig.board | 3 --- ports/esp32/boards/sdkconfig.base | 11 ----------- ports/esp32/boards/sdkconfig.ble | 10 ---------- ports/esp32/boards/sdkconfig.spiram | 3 --- 4 files changed, 27 deletions(-) diff --git a/ports/esp32/boards/GENERIC_OTA/sdkconfig.board b/ports/esp32/boards/GENERIC_OTA/sdkconfig.board index ca1f4276f4560..d314860cc9378 100644 --- a/ports/esp32/boards/GENERIC_OTA/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_OTA/sdkconfig.board @@ -1,6 +1,3 @@ CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-ota.csv" - -# ESP-IDF v3: -CONFIG_APP_ROLLBACK_ENABLE=y diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index de6e42c8f3139..e326831a158be 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -41,17 +41,6 @@ CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y # ULP coprocessor support CONFIG_ESP32_ULP_COPROC_ENABLED=y -# v3.3-only (renamed in 4.0) -CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y -CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=n -CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=n -CONFIG_SUPPORT_STATIC_ALLOCATION=y -CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK=y -CONFIG_PPP_SUPPORT=y -CONFIG_PPP_PAP_SUPPORT=y -CONFIG_PPP_CHAP_SUPPORT=y -CONFIG_ULP_COPROC_ENABLED=y - # For cmake build CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_PARTITION_TABLE_CUSTOM=y diff --git a/ports/esp32/boards/sdkconfig.ble b/ports/esp32/boards/sdkconfig.ble index f714ce4629b32..5565fd81a8432 100644 --- a/ports/esp32/boards/sdkconfig.ble +++ b/ports/esp32/boards/sdkconfig.ble @@ -15,13 +15,3 @@ CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4 CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=y CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=n CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 - -# v3.3-only (renamed in 4.0) -CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY=y -CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY= -CONFIG_BTDM_CONTROLLER_MODE_BTDM= -CONFIG_BLUEDROID_ENABLED=n -CONFIG_NIMBLE_ENABLED=y -CONFIG_NIMBLE_MAX_CONNECTIONS=4 -CONFIG_NIMBLE_PINNED_TO_CORE_0=n -CONFIG_NIMBLE_PINNED_TO_CORE_1=y diff --git a/ports/esp32/boards/sdkconfig.spiram b/ports/esp32/boards/sdkconfig.spiram index db1a83af8c432..5b4ce118b890e 100644 --- a/ports/esp32/boards/sdkconfig.spiram +++ b/ports/esp32/boards/sdkconfig.spiram @@ -4,6 +4,3 @@ CONFIG_ESP32_SPIRAM_SUPPORT=y CONFIG_SPIRAM_CACHE_WORKAROUND=y CONFIG_SPIRAM_IGNORE_NOTFOUND=y CONFIG_SPIRAM_USE_MEMMAP=y - -# v3.3-only (renamed in 4.0) -CONFIG_SPIRAM_SUPPORT=y From da2b5fa1c1a569faa0d139d97369cce9c0b55a00 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Feb 2021 01:36:30 +1100 Subject: [PATCH 0608/3533] esp32/boards: Enable BLE on all boards. BLE was enabled by default on all boards in the existing make build. Signed-off-by: Damien George --- ports/esp32/boards/GENERIC/mpconfigboard.cmake | 6 +++++- ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake | 1 + ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake | 1 + ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake | 1 + ports/esp32/boards/TINYPICO/mpconfigboard.cmake | 1 + ports/esp32/boards/sdkconfig.base | 4 ++++ ports/esp32/main/CMakeLists.txt | 1 + ports/esp32/mpconfigport.h | 6 ++++++ 8 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ports/esp32/boards/GENERIC/mpconfigboard.cmake b/ports/esp32/boards/GENERIC/mpconfigboard.cmake index 8fea524555060..f69b05f47b042 100644 --- a/ports/esp32/boards/GENERIC/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC/mpconfigboard.cmake @@ -1,2 +1,6 @@ -set(SDKCONFIG_DEFAULTS boards/sdkconfig.base) +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.ble +) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake index 4e23666f15714..686c78df97f23 100644 --- a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake @@ -1,5 +1,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base + boards/sdkconfig.ble boards/GENERIC_D2WD/sdkconfig.board ) diff --git a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake index 7b1e1460054d7..ca552d470c0a6 100644 --- a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake @@ -1,5 +1,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base + boards/sdkconfig.ble boards/GENERIC_OTA/sdkconfig.board ) diff --git a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake index bb441d9ebf543..2128edb592f2a 100644 --- a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake @@ -1,5 +1,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base + boards/sdkconfig.ble boards/sdkconfig.spiram ) diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake index 990e3e035b9c0..5c31f5c60065e 100644 --- a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake +++ b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake @@ -1,5 +1,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base + boards/sdkconfig.ble boards/sdkconfig.240mhz boards/sdkconfig.spiram boards/TINYPICO/sdkconfig.board diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index e326831a158be..c84dd606b2c29 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -45,3 +45,7 @@ CONFIG_ESP32_ULP_COPROC_ENABLED=y CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" + +# To reduce iRAM usage +CONFIG_ESP32_WIFI_IRAM_OPT=n +CONFIG_ESP32_WIFI_RX_IRAM_OPT=n diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index a46e0112c9b88..eedf3ae9a511f 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -78,6 +78,7 @@ set(MICROPY_SOURCE_QSTR set(IDF_COMPONENTS app_update bootloader_support + bt driver esp32 esp_common diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 81f8297c50019..34be9405e4eb5 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -126,6 +126,12 @@ #define MICROPY_PY_THREAD_GIL_VM_DIVISOR (32) // extended modules +#ifndef MICROPY_PY_BLUETOOTH +#define MICROPY_PY_BLUETOOTH (1) +#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1) +#define MICROPY_BLUETOOTH_NIMBLE (1) +#define MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY (1) +#endif #define MICROPY_PY_UASYNCIO (1) #define MICROPY_PY_UCTYPES (1) #define MICROPY_PY_UZLIB (1) From aa3d6b6aa5ec9fdf15b5c2d30a1c9f9c9064cd3c Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Feb 2021 10:28:05 +1100 Subject: [PATCH 0609/3533] tools/ci.sh: Change esp32 CI to work with idf.py and IDF v4.0.2. Signed-off-by: Damien George --- .github/workflows/ports_esp32.yml | 19 +++---------- tools/ci.sh | 44 ++++++++++--------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/.github/workflows/ports_esp32.yml b/.github/workflows/ports_esp32.yml index 041074557e616..0ad3f87a25fcb 100644 --- a/.github/workflows/ports_esp32.yml +++ b/.github/workflows/ports_esp32.yml @@ -13,24 +13,11 @@ on: - 'ports/esp32/**' jobs: - idf3_build: + build: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_esp32_idf3_setup && ci_esp32_idf3_path >> $GITHUB_PATH + run: source tools/ci.sh && ci_esp32_setup - name: Build - env: - IDF_PATH: ${{ github.workspace }}/esp-idf - run: source tools/ci.sh && ci_esp32_idf3_build - - idf4_build: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v2 - - name: Install packages - run: source tools/ci.sh && ci_esp32_idf4_setup && ci_esp32_idf4_path >> $GITHUB_PATH - - name: Build - env: - IDF_PATH: ${{ github.workspace }}/esp-idf - run: source tools/ci.sh && ci_esp32_idf4_build + run: source tools/ci.sh && ci_esp32_build diff --git a/tools/ci.sh b/tools/ci.sh index 8661f532efe97..4f99551468c59 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -82,38 +82,22 @@ function ci_cc3200_build { ######################################################################################## # ports/esp32 -function ci_esp32_idf3_setup { - sudo pip3 install pyserial 'pyparsing<2.4' - curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar zxf - +function ci_esp32_setup { git clone https://github.com/espressif/esp-idf.git -} - -function ci_esp32_idf3_path { - echo $(pwd)/xtensa-esp32-elf/bin -} - -function ci_esp32_idf3_build { - make ${MAKEOPTS} -C mpy-cross - git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V3 :=" ports/esp32/Makefile | cut -d " " -f 3) - git -C esp-idf submodule update --init components/json/cJSON components/esp32/lib components/esptool_py/esptool components/expat/expat components/lwip/lwip components/mbedtls/mbedtls components/micro-ecc/micro-ecc components/nghttp/nghttp2 components/nimble components/bt - make ${MAKEOPTS} -C ports/esp32 submodules - make ${MAKEOPTS} -C ports/esp32 -} - -function ci_esp32_idf4_setup { - sudo pip3 install pyserial 'pyparsing<2.4' - curl -L https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp-2019r2-linux-amd64.tar.gz | tar zxf - - git clone https://github.com/espressif/esp-idf.git -} - -function ci_esp32_idf4_path { - echo $(pwd)/xtensa-esp32-elf/bin -} - -function ci_esp32_idf4_build { + git -C esp-idf checkout v4.0.2 + git -C esp-idf submodule update --init \ + components/bt/controller/lib \ + components/bt/host/nimble/nimble \ + components/esp_wifi/lib_esp32 \ + components/esptool_py/esptool \ + components/lwip/lwip \ + components/mbedtls/mbedtls + ./esp-idf/install.sh +} + +function ci_esp32_build { + source esp-idf/export.sh make ${MAKEOPTS} -C mpy-cross - git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V4 :=" ports/esp32/Makefile | cut -d " " -f 3) - git -C esp-idf submodule update --init components/bt/controller/lib components/bt/host/nimble/nimble components/esp_wifi/lib_esp32 components/esptool_py/esptool components/lwip/lwip components/mbedtls/mbedtls make ${MAKEOPTS} -C ports/esp32 submodules make ${MAKEOPTS} -C ports/esp32 } From e017f276f7f95c7412b18939ba91784fdc69b734 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Feb 2021 17:51:31 +1100 Subject: [PATCH 0610/3533] esp32/README: Update based on new IDF v4 cmake build process. Signed-off-by: Damien George --- ports/esp32/README.md | 245 +++++++++++++----------------------------- 1 file changed, 76 insertions(+), 169 deletions(-) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index 54fb41cf6f390..4f63750593dc0 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -1,13 +1,13 @@ MicroPython port to the ESP32 ============================= -This is an experimental port of MicroPython to the Espressif ESP32 -microcontroller. It uses the ESP-IDF framework and MicroPython runs as +This is a port of MicroPython to the Espressif ESP32 series of +microcontrollers. It uses the ESP-IDF framework and MicroPython runs as a task under FreeRTOS. Supported features include: - REPL (Python prompt) over UART0. -- 16k stack for the MicroPython task and 96k Python heap. +- 16k stack for the MicroPython task and approximately 100k Python heap. - Many of MicroPython's features are enabled: unicode, arbitrary-precision integers, single-precision floats, complex numbers, frozen bytecode, as well as many of the internal modules. @@ -15,16 +15,24 @@ Supported features include: - The machine module with GPIO, UART, SPI, software I2C, ADC, DAC, PWM, TouchPad, WDT and Timer. - The network module with WLAN (WiFi) support. +- Bluetooth low-energy (BLE) support via the bluetooth module. -Development of this ESP32 port was sponsored in part by Microbric Pty Ltd. +Initial development of this ESP32 port was sponsored in part by Microbric Pty Ltd. -Setting up the toolchain and ESP-IDF ------------------------------------- +Setting up ESP-IDF and the build environment +-------------------------------------------- -There are two main components that are needed to build the firmware: -- the Xtensa cross-compiler that targets the CPU in the ESP32 (this is - different to the compiler used by the ESP8266) -- the Espressif IDF (IoT development framework, aka SDK) +MicroPython on ESP32 requires the Espressif IDF version 4 (IoT development +framework, aka SDK). The ESP-IDF includes the libraries and RTOS needed to +manage the ESP32 microcontroller, as well as a way to manage the required +build environment and toolchains needed to build the firmware. + +The ESP-IDF changes quickly and MicroPython only supports certain versions. +Currently MicroPython supports v4.0.2, although other IDF v4 versions may also +work. + +To install the ESP-IDF the full instructions can be found at the +[Espressif Getting Started guide](https://docs.espressif.com/projects/esp-idf/en/v4.0.2/get-started/index.html#installation-step-by-step). If you are on a Windows machine then the [Windows Subsystem for Linux](https://msdn.microsoft.com/en-au/commandline/wsl/install_guide) is the @@ -32,164 +40,31 @@ most efficient way to install the ESP32 toolchain and build the project. If you use WSL then follow the Linux instructions rather than the Windows instructions. -The ESP-IDF changes quickly and MicroPython only supports certain versions. -The git hash of these versions (one for 3.x, one for 4.x) can be found by -running `make` without a configured `ESPIDF`. Then you can fetch the -required IDF using the following command: - -```bash -$ cd ports/esp32 -$ make ESPIDF= # This will print the supported hashes, copy the one you want. -$ export ESPIDF=$HOME/src/github.com/espressif/esp-idf # Or any path you like. -$ mkdir -p $ESPIDF -$ cd $ESPIDF -$ git clone https://github.com/espressif/esp-idf.git $ESPIDF -$ git checkout -$ git submodule update --init --recursive -``` - -Note: The ESP IDF v4.x support is currently experimental. It does not -currently support PPP or wired Ethernet. - -Python dependencies -=================== - -You will also need other dependencies from the IDF, see -`$ESPIDF/requirements.txt`, but at a minimum you need `pyserial>=3.0` and -`pyparsing>=2.0.3,<2.4.0`. - -You can use Python 2 or Python 3. If you need to override the system default -add (for example) `PYTHON=python3` to any of the `make` commands below. - -It is recommended to use a Python virtual environment. Even if your system -package manager already provides these libraries, the IDF v4.x is currently -incompatible with pyparsing 2.4 and higher. - -For example, to set up a Python virtual environment from scratch: - -```bash -$ cd ports/esp32 -$ python3 -m venv build-venv -$ source build-venv/bin/activate -$ pip install --upgrade pip -$ pip install -r path/to/esp-idf/requirements.txt -``` +The Espressif instructions will guide you through using the `install.sh` +(or `install.bat`) script to download the toolchain and set up your environment. +The steps to take are summarised below. -To re-enter this virtual environment in future sessions, you only need to -source the `activate` script, i.e.: +To check out a copy of the IDF use git clone: ```bash -$ cd ports/esp32 -$ source build-venv/bin/activate +$ git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git ``` -Then, to install the toolchain (which includes the GCC compiler, linker, binutils, -etc), there are two options: - -1. Using the IDF scripts to install the toolchain (IDF 4.x only) -================================================================ - -Follow the steps at the [Espressif Getting Started guide](https://docs.espressif.com/projects/esp-idf/en/v4.0/get-started/index.html#step-3-set-up-the-tools). - -This will guide you through using the `install.sh` (or `install.bat`) script -to download the toolchain and add it to your `PATH`. The steps are summarised -below: - -After you've cloned and checked out the IDF to the correct version (see -above), run the `install.sh` script: - -```bash -$ cd $ESPIDF -$ ./install.sh # (or install.bat on Windows) -``` +(You don't need a full recursive clone; see the `ci_esp32_setup` function in +`tools/ci.sh` in this repository for more detailed set-up commands.) -Then in the `ports/esp32` directory, source the `export.sh` script to set the -`PATH`. +After you've cloned and checked out the IDF to the correct version, run the +`install.sh` script: ```bash -$ cd micropython/ports/esp32 -$ source $ESPIDF/export.sh # (or path\to\esp-idf\export.bat on Windows) -$ # Run make etc, see below. +$ cd esp-idf +$ ./install.sh # (or install.bat on Windows) +$ source export.sh # (or export.bat on Windows) ``` The `install.sh` step only needs to be done once. You will need to source `export.sh` for every new session. -Note: If you get an error about `--no-site-packages`, then modify -`$ESPIDF/tools/idf_tools.py` and make the same change as [this -commit](https://github.com/espressif/esp-idf/commit/7a18f02acd7005f7c56e62175a8d1968a1a9019d). - -2. or, Downloading pre-built toolchain manually (IDF 3.x and 4.x) -============================================================= - -Note: while this works with 4.x, if you're using the 4.x IDF, it's much -simpler to use the guide above, which will also get a more recent version of -the toolchain. - -You can follow the 3.x guide at: - - * [Linux installation](https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/linux-setup.html) - * [MacOS installation](https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/macos-setup.html) - * [Windows installation](https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/windows-setup.html) - -You will need to update your `PATH` environment variable to include the ESP32 -toolchain. For example, you can issue the following commands on (at least) -Linux: - - $ export PATH=$PATH:$HOME/esp/crosstool-NG/builds/xtensa-esp32-elf/bin - -You can put this command in your `.profile` or `.bash_login`, or do it manually. - -Configuring the MicroPython build ---------------------------------- - -You then need to set the `ESPIDF` environment/makefile variable to point to -the root of the ESP-IDF repository. The recommended way to do this is to have -a custom `makefile` in `ports/esp32` which sets any additional variables, then -includes the main `Makefile`. Note that GNU Make will preferentially run -`GNUmakefile`, then `makefile`, then `Makefile`, which is what allows this to -work. On case-insensitive filesystems, you'll need to use `GNUmakefile` rather -than `makefile`. - -Create a new file in the esp32 directory called `makefile` (or `GNUmakefile`) -and add the following lines to that file: - -``` -ESPIDF ?= -BOARD ?= GENERIC -#PORT ?= /dev/ttyUSB0 -#FLASH_MODE ?= qio -#FLASH_SIZE ?= 4MB -#CROSS_COMPILE ?= xtensa-esp32-elf- - -include Makefile -``` - -Be sure to enter the correct path to your local copy of the IDF repository -(and use `$(HOME)`, not tilde (`~`), to reference your home directory). - -If the Xtensa cross-compiler is not in your path you can use the -`CROSS_COMPILE` variable to set its location. Other options of interest are -`PORT` for the serial port of your ESP32 module, and `FLASH_MODE` (which may -need to be `dio` for some modules) and `FLASH_SIZE`. See the Makefile for -further information. - -The default ESP IDF configuration settings are provided by the `GENERIC` -board definition in the directory `boards/GENERIC`. For a custom configuration -you can define your own board directory. - -Any of these variables can also be set on the make command line, e.g. to set -the `BOARD` variable, use: - -```bash -$ make BOARD=TINYPICO -``` - -Note the use of `?=` in the `makefile` which allows them to be overridden on -the command line. There is also a `GENERIC_SPIRAM` board for for ESP32 -modules that have external SPIRAM, but prefer to use a specific board target -(or define your own as necessary). - Building the firmware --------------------- @@ -198,8 +73,7 @@ built-in scripts to bytecode. This can be done by (from the root of this repository): ```bash -$ cd mpy-cross -$ make mpy-cross +$ make -C mpy-cross ``` Then to build MicroPython for the ESP32 run: @@ -210,16 +84,14 @@ $ make submodules $ make ``` -This will produce binary firmware images in the `build/` subdirectory -(three of them: bootloader.bin, partitions.bin and application.bin). +This will produce a combined `firmware.bin` image in the `build-GENERIC/` +subdirectory (this firmware image is made up of: bootloader.bin, partitions.bin +and micropython.bin). To flash the firmware you must have your ESP32 module in the bootloader mode and connected to a serial port on your PC. Refer to the documentation -for your particular ESP32 module for how to do this. The serial port and -flash settings are set in the `Makefile`, and can be overridden in your -local `makefile`; see above for more details. - -You will also need to have user permissions to access the /dev/ttyUSB0 device. +for your particular ESP32 module for how to do this. +You will also need to have user permissions to access the `/dev/ttyUSB0` device. On Linux, you can enable this by adding your user to the `dialout` group, and rebooting or logging out and in again. (Note: on some distributions this may be the `uucp` group, run `ls -la /dev/ttyUSB0` to check.) @@ -242,8 +114,23 @@ To flash the MicroPython firmware to your ESP32 use: $ make deploy ``` -This will use the `esptool.py` script (provided by ESP-IDF) to flash the -binary images to the device. +The default ESP32 board build by the above commands is the `GENERIC` one, which +should work on most ESP32 modules. You can specify a different board by passing +`BOARD=` to the make commands, for example: + +```bash +$ make BOARD=GENERIC_SPIRAM +``` + +Note: the above "make" commands are thin wrappers for the underlying `idf.py` +build tool that is part of the ESP-IDF. You can instead use `idf.py` directly, +for example: + +```bash +$ idf.py build +$ idf.py -D MICROPY_BOARD=GENERIC_SPIRAM build +$ idf.py flash +``` Getting a Python prompt on the device ------------------------------------- @@ -262,6 +149,8 @@ or $ miniterm.py /dev/ttyUSB0 115200 ``` +You can also use `idf.py monitor`. + Configuring the WiFi and using the board ---------------------------------------- @@ -301,9 +190,27 @@ import machine antenna = machine.Pin(16, machine.Pin.OUT, value=0) ``` +Defining a custom ESP32 board +----------------------------- + +The default ESP-IDF configuration settings are provided by the `GENERIC` +board definition in the directory `boards/GENERIC`. For a custom configuration +you can define your own board directory. Start a new board configuration by +copying an existing one (like `GENERIC`) and modifying it to suit your board. + +MicroPython specific configuration values are defined in the board-specific +`mpconfigboard.h` file, which is included by `mpconfigport.h`. Additional +settings are put in `mpconfigboard.cmake`, including a list of `sdkconfig` +files that configure ESP-IDF settings. Some standard `sdkconfig` files are +provided in the `boards/` directory, like `boards/sdkconfig.ble`. You can +also define custom ones in your board directory. + +See existing board definitions for further examples of configuration. + +Configuration Troubleshooting --------------- -* Continuous reboots after programming: Ensure FLASH_MODE is correct for your - board (e.g. ESP-WROOM-32 should be DIO). Then perform a `make clean`, rebuild, - redeploy. +* Continuous reboots after programming: Ensure `CONFIG_ESPTOOLPY_FLASHMODE` is + correct for your board (e.g. ESP-WROOM-32 should be DIO). Then perform a + `make clean`, rebuild, redeploy. From d191d88cabec9bf328f6f46b11ce6642af70cfc7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 15 Feb 2021 21:00:01 +1100 Subject: [PATCH 0611/3533] esp32: Add support to build with ESP-IDF v4.1.1. ESP-IDF v4.0.2 is still supported. Signed-off-by: Damien George --- ports/esp32/README.md | 5 +++-- ports/esp32/machine_uart.c | 38 ++++++++++++++++++++++----------- ports/esp32/main/CMakeLists.txt | 4 ++++ ports/esp32/modnetwork.c | 13 +++++++---- ports/esp32/modsocket.c | 6 +++++- ports/esp32/uart.c | 1 + 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index 4f63750593dc0..d8818ccadb6c3 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -28,8 +28,8 @@ manage the ESP32 microcontroller, as well as a way to manage the required build environment and toolchains needed to build the firmware. The ESP-IDF changes quickly and MicroPython only supports certain versions. -Currently MicroPython supports v4.0.2, although other IDF v4 versions may also -work. +Currently MicroPython supports v4.0.2 and v4.1.1, +although other IDF v4 versions may also work. To install the ESP-IDF the full instructions can be found at the [Espressif Getting Started guide](https://docs.espressif.com/projects/esp-idf/en/v4.0.2/get-started/index.html#installation-step-by-step). @@ -50,6 +50,7 @@ To check out a copy of the IDF use git clone: $ git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git ``` +You can replace `v4.0.2` with `v4.1.1` or any other supported version. (You don't need a full recursive clone; see the `ci_esp32_setup` function in `tools/ci.sh` in this repository for more detailed set-up commands.) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index c334e694b21b0..7fce83f2c4c2b 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -36,6 +36,20 @@ #include "py/mperrno.h" #include "modmachine.h" +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) +#define UART_INV_TX UART_INVERSE_TXD +#define UART_INV_RX UART_INVERSE_RXD +#define UART_INV_RTS UART_INVERSE_RTS +#define UART_INV_CTS UART_INVERSE_CTS +#else +#define UART_INV_TX UART_SIGNAL_TXD_INV +#define UART_INV_RX UART_SIGNAL_RXD_INV +#define UART_INV_RTS UART_SIGNAL_RTS_INV +#define UART_INV_CTS UART_SIGNAL_CTS_INV +#endif + +#define UART_INV_MASK (UART_INV_TX | UART_INV_RX | UART_INV_RTS | UART_INV_CTS) + typedef struct _machine_uart_obj_t { mp_obj_base_t base; uart_port_t uart_num; @@ -68,28 +82,28 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri if (self->invert) { mp_printf(print, ", invert="); uint32_t invert_mask = self->invert; - if (invert_mask & UART_INVERSE_TXD) { + if (invert_mask & UART_INV_TX) { mp_printf(print, "INV_TX"); - invert_mask &= ~UART_INVERSE_TXD; + invert_mask &= ~UART_INV_TX; if (invert_mask) { mp_printf(print, "|"); } } - if (invert_mask & UART_INVERSE_RXD) { + if (invert_mask & UART_INV_RX) { mp_printf(print, "INV_RX"); - invert_mask &= ~UART_INVERSE_RXD; + invert_mask &= ~UART_INV_RX; if (invert_mask) { mp_printf(print, "|"); } } - if (invert_mask & UART_INVERSE_RTS) { + if (invert_mask & UART_INV_RTS) { mp_printf(print, "INV_RTS"); - invert_mask &= ~UART_INVERSE_RTS; + invert_mask &= ~UART_INV_RTS; if (invert_mask) { mp_printf(print, "|"); } } - if (invert_mask & UART_INVERSE_CTS) { + if (invert_mask & UART_INV_CTS) { mp_printf(print, "INV_CTS"); } } @@ -238,7 +252,7 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co } // set line inversion - if (args[ARG_invert].u_int & ~UART_LINE_INV_MASK) { + if (args[ARG_invert].u_int & ~UART_INV_MASK) { mp_raise_ValueError(MP_ERROR_TEXT("invalid inversion mask")); } self->invert = args[ARG_invert].u_int; @@ -380,10 +394,10 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, - { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERSE_TXD) }, - { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERSE_RXD) }, - { MP_ROM_QSTR(MP_QSTR_INV_RTS), MP_ROM_INT(UART_INVERSE_RTS) }, - { MP_ROM_QSTR(MP_QSTR_INV_CTS), MP_ROM_INT(UART_INVERSE_CTS) }, + { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INV_TX) }, + { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INV_RX) }, + { MP_ROM_QSTR(MP_QSTR_INV_RTS), MP_ROM_INT(UART_INV_RTS) }, + { MP_ROM_QSTR(MP_QSTR_INV_CTS), MP_ROM_INT(UART_INV_CTS) }, }; STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index eedf3ae9a511f..2455a4cdd2e94 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -104,6 +104,10 @@ set(IDF_COMPONENTS xtensa ) +if(IDF_VERSION_MINOR GREATER_EQUAL 1) + list(APPEND IDF_COMPONENTS esp_netif) +endif() + # Register the main IDF component. idf_component_register( SRCS diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index d981b60805007..f772fa2cf6420 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -45,7 +45,6 @@ #include "esp_wifi.h" #include "esp_log.h" #include "lwip/dns.h" -#include "tcpip_adapter.h" #include "mdns.h" #if !MICROPY_ESP_IDF_4 @@ -55,6 +54,12 @@ #include "modnetwork.h" +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) +#define DNS_MAIN TCPIP_ADAPTER_DNS_MAIN +#else +#define DNS_MAIN ESP_NETIF_DNS_MAIN +#endif + #define MODNETWORK_INCLUDE_CONSTANTS (1) NORETURN void _esp_exceptions(esp_err_t e) { @@ -491,7 +496,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { tcpip_adapter_ip_info_t info; tcpip_adapter_dns_info_t dns_info; tcpip_adapter_get_ip_info(self->if_id, &info); - tcpip_adapter_get_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info); + tcpip_adapter_get_dns_info(self->if_id, DNS_MAIN, &dns_info); if (n_args == 1) { // get mp_obj_t tuple[4] = { @@ -526,14 +531,14 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { _esp_exceptions(e); } ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(self->if_id, &info)); - ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); + ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, DNS_MAIN, &dns_info)); } else if (self->if_id == WIFI_IF_AP) { esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP); if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) { _esp_exceptions(e); } ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_AP, &info)); - ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info)); + ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_AP, DNS_MAIN, &dns_info)); ESP_EXCEPTIONS(tcpip_adapter_dhcps_start(WIFI_IF_AP)); } } else { diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 85433e575fc51..8a4cf3e1659af 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -46,7 +46,6 @@ #include "py/stream.h" #include "py/mperrno.h" #include "lib/netutils/netutils.h" -#include "tcpip_adapter.h" #include "mdns.h" #include "modnetwork.h" @@ -181,7 +180,12 @@ static int _socket_getaddrinfo3(const char *nodename, const char *servname, memcpy(nodename_no_local, nodename, nodename_len - local_len); nodename_no_local[nodename_len - local_len] = '\0'; + #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) struct ip4_addr addr = {0}; + #else + esp_ip4_addr_t addr = {0}; + #endif + esp_err_t err = mdns_query_a(nodename_no_local, MDNS_QUERY_TIMEOUT_MS, &addr); if (err != ESP_OK) { if (err == ESP_ERR_NOT_FOUND) { diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index 381be7f4f016c..c837c8dcfe6a4 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -29,6 +29,7 @@ #include #include "driver/uart.h" +#include "soc/uart_periph.h" #include "py/runtime.h" #include "py/mphal.h" From a91500217757d9f7dc9791f014cfb465955f7d42 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 15 Feb 2021 21:00:09 +1100 Subject: [PATCH 0612/3533] esp32: Add support to build with ESP-IDF v4.2. Signed-off-by: Damien George --- ports/esp32/README.md | 2 +- ports/esp32/main/CMakeLists.txt | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index d8818ccadb6c3..c349f44b27ef3 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -28,7 +28,7 @@ manage the ESP32 microcontroller, as well as a way to manage the required build environment and toolchains needed to build the firmware. The ESP-IDF changes quickly and MicroPython only supports certain versions. -Currently MicroPython supports v4.0.2 and v4.1.1, +Currently MicroPython supports v4.0.2, v4.1.1 and v4.2, although other IDF v4 versions may also work. To install the ESP-IDF the full instructions can be found at the diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 2455a4cdd2e94..52985383f92dd 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -108,6 +108,11 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 1) list(APPEND IDF_COMPONENTS esp_netif) endif() +if(IDF_VERSION_MINOR GREATER_EQUAL 2) + list(APPEND IDF_COMPONENTS esp_system) + list(APPEND IDF_COMPONENTS esp_timer) +endif() + # Register the main IDF component. idf_component_register( SRCS @@ -169,5 +174,12 @@ foreach(comp ${IDF_COMPONENTS}) endif() endforeach() +if(IDF_VERSION_MINOR GREATER_EQUAL 2) + # These paths cannot currently be found by the IDF_COMPONENTS search loop above, + # so add them explicitly. + list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/${IDF_TARGET}/include) + list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/include) +endif() + # Include the main MicroPython cmake rules. include(${MICROPY_DIR}/py/mkrules.cmake) From f12462ddc4cd847896a38412539a49ccb0579c94 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 15 Feb 2021 23:22:05 +1100 Subject: [PATCH 0613/3533] esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. Signed-off-by: Damien George --- ports/esp32/esp32_ulp.c | 4 ---- ports/esp32/main.c | 4 ---- ports/esp32/modesp.c | 3 --- ports/esp32/modmachine.c | 6 ------ ports/esp32/modsocket.c | 12 ------------ ports/esp32/mpconfigport.h | 4 ---- ports/esp32/mphalport.c | 5 ----- ports/esp32/mpthreadport.c | 3 --- 8 files changed, 41 deletions(-) diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 4e9d768146301..50244cdf25060 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -85,11 +85,7 @@ STATIC const mp_rom_map_elem_t esp32_ulp_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_set_wakeup_period), MP_ROM_PTR(&esp32_ulp_set_wakeup_period_obj) }, { MP_ROM_QSTR(MP_QSTR_load_binary), MP_ROM_PTR(&esp32_ulp_load_binary_obj) }, { MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&esp32_ulp_run_obj) }, - #if !MICROPY_ESP_IDF_4 - { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ULP_COPROC_RESERVE_MEM) }, - #else { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) }, - #endif }; STATIC MP_DEFINE_CONST_DICT(esp32_ulp_locals_dict, esp32_ulp_locals_dict_table); diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 6f9ab82d0512a..001f2beaeb5e9 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -37,11 +37,7 @@ #include "esp_task.h" #include "soc/cpu.h" #include "esp_log.h" -#if MICROPY_ESP_IDF_4 #include "esp32/spiram.h" -#else -#include "esp_spiram.h" -#endif #include "py/stackctrl.h" #include "py/nlr.h" diff --git a/ports/esp32/modesp.c b/ports/esp32/modesp.c index 369649c119ffd..59a261e8c27ff 100644 --- a/ports/esp32/modesp.c +++ b/ports/esp32/modesp.c @@ -29,9 +29,6 @@ #include -#if !MICROPY_ESP_IDF_4 -#include "rom/gpio.h" -#endif #include "esp_log.h" #include "esp_spi_flash.h" diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 303d25ee8b2d1..bb346ea1cd3fb 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -32,15 +32,9 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#if MICROPY_ESP_IDF_4 #include "esp32/rom/rtc.h" #include "esp32/clk.h" #include "esp_sleep.h" -#else -#include "rom/ets_sys.h" -#include "rom/rtc.h" -#include "esp_clk.h" -#endif #include "esp_pm.h" #include "driver/touch_pad.h" diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 8a4cf3e1659af..ce7a714eb233c 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -55,18 +55,6 @@ #include "lwip/igmp.h" #include "esp_log.h" -#if !MICROPY_ESP_IDF_4 -#define lwip_bind lwip_bind_r -#define lwip_listen lwip_listen_r -#define lwip_accept lwip_accept_r -#define lwip_setsockopt lwip_setsockopt_r -#define lwip_fnctl lwip_fnctl_r -#define lwip_recvfrom lwip_recvfrom_r -#define lwip_write lwip_write_r -#define lwip_sendto lwip_sendto_r -#define lwip_close lwip_close_r -#endif - #define SOCKET_POLL_US (100000) #define MDNS_QUERY_TIMEOUT_MS (5000) #define MDNS_LOCAL_SUFFIX ".local" diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 34be9405e4eb5..7b0a00250b90f 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -8,10 +8,6 @@ #include #include "esp_system.h" -#if !MICROPY_ESP_IDF_4 -#include "rom/ets_sys.h" -#endif - // object representation and NLR handling #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_A) #define MICROPY_NLR_SETJMP (1) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 54033826847ba..65fa88e4d0780 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -32,12 +32,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" - -#if MICROPY_ESP_IDF_4 #include "esp32/rom/uart.h" -#else -#include "rom/uart.h" -#endif #include "py/obj.h" #include "py/objstr.h" diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index d294c92727bf9..140a76464fe40 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -34,9 +34,6 @@ #include "mpthreadport.h" #include "esp_task.h" -#if !MICROPY_ESP_IDF_4 -#include "freertos/semphr.h" -#endif #if MICROPY_PY_THREAD From 771376a0cb26c7a74554ff421feff9c92eadceeb Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 24 Oct 2020 12:34:23 -0700 Subject: [PATCH 0614/3533] esp32/modsocket: Remove unix socket error code translation. The ESP-IDF has its own errno codes which should propagate out to the user. --- ports/esp32/modsocket.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index ce7a714eb233c..61761d819445f 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -140,16 +140,6 @@ void usocket_events_handler(void) { #endif // MICROPY_PY_USOCKET_EVENTS -NORETURN static void exception_from_errno(int _errno) { - // Here we need to convert from lwip errno values to MicroPython's standard ones - if (_errno == EADDRINUSE) { - _errno = MP_EADDRINUSE; - } else if (_errno == EINPROGRESS) { - _errno = MP_EINPROGRESS; - } - mp_raise_OSError(_errno); -} - static inline void check_for_exceptions(void) { mp_handle_pending(true); } @@ -277,7 +267,7 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz sock->fd = lwip_socket(sock->domain, sock->type, sock->proto); if (sock->fd < 0) { - exception_from_errno(errno); + mp_raise_OSError(errno); } _socket_settimeout(sock, UINT64_MAX); @@ -291,7 +281,7 @@ STATIC mp_obj_t socket_bind(const mp_obj_t arg0, const mp_obj_t arg1) { int r = lwip_bind(self->fd, res->ai_addr, res->ai_addrlen); lwip_freeaddrinfo(res); if (r < 0) { - exception_from_errno(errno); + mp_raise_OSError(errno); } return mp_const_none; } @@ -302,7 +292,7 @@ STATIC mp_obj_t socket_listen(const mp_obj_t arg0, const mp_obj_t arg1) { int backlog = mp_obj_get_int(arg1); int r = lwip_listen(self->fd, backlog); if (r < 0) { - exception_from_errno(errno); + mp_raise_OSError(errno); } return mp_const_none; } @@ -323,7 +313,7 @@ STATIC mp_obj_t socket_accept(const mp_obj_t arg0) { break; } if (errno != EAGAIN) { - exception_from_errno(errno); + mp_raise_OSError(errno); } check_for_exceptions(); } @@ -365,7 +355,7 @@ STATIC mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) { MP_THREAD_GIL_ENTER(); lwip_freeaddrinfo(res); if (r != 0) { - exception_from_errno(errno); + mp_raise_OSError(errno); } return mp_const_none; @@ -384,7 +374,7 @@ STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) { int val = mp_obj_get_int(args[3]); int ret = lwip_setsockopt(self->fd, SOL_SOCKET, opt, &val, sizeof(int)); if (ret != 0) { - exception_from_errno(errno); + mp_raise_OSError(errno); } break; } @@ -535,7 +525,7 @@ mp_obj_t _socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in, int errcode; mp_uint_t ret = _socket_read_data(self_in, vstr.buf, len, from, from_len, &errcode); if (ret == MP_STREAM_ERROR) { - exception_from_errno(errcode); + mp_raise_OSError(errcode); } vstr.len = ret; @@ -569,7 +559,7 @@ int _socket_send(socket_obj_t *sock, const char *data, size_t datalen) { int r = lwip_write(sock->fd, data + sentlen, datalen - sentlen); MP_THREAD_GIL_ENTER(); if (r < 0 && errno != EWOULDBLOCK) { - exception_from_errno(errno); + mp_raise_OSError(errno); } if (r > 0) { sentlen += r; @@ -627,7 +617,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_ return mp_obj_new_int_from_uint(ret); } if (ret == -1 && errno != EWOULDBLOCK) { - exception_from_errno(errno); + mp_raise_OSError(errno); } check_for_exceptions(); } From 902da05a180d40e62373656f8be94a01ca39eb94 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 11 Nov 2020 17:46:18 -0800 Subject: [PATCH 0615/3533] esp32: Set MICROPY_USE_INTERNAL_ERRNO=0 to use toolchain's errno.h. The underlying OS (the ESP-IDF) uses it's own internal errno codes and so it's simpler and cleaner to use those rather than trying to convert everything to the values defined in py/mperrno.h. --- ports/esp32/mpconfigport.h | 2 +- tests/net_hosted/connect_nonblock.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 7b0a00250b90f..392f8c749c240 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -51,7 +51,7 @@ #define MICROPY_MODULE_FROZEN_MPY (1) #define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool #define MICROPY_CAN_OVERRIDE_BUILTINS (1) -#define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_USE_INTERNAL_ERRNO (0) // errno.h from xtensa-esp32-elf/sys-include/sys #define MICROPY_USE_INTERNAL_PRINTF (0) // ESP32 SDK requires its own printf #define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_SCHEDULER_DEPTH (8) diff --git a/tests/net_hosted/connect_nonblock.py b/tests/net_hosted/connect_nonblock.py index 3a3eaa2ba01e4..c024b65a0a9a3 100644 --- a/tests/net_hosted/connect_nonblock.py +++ b/tests/net_hosted/connect_nonblock.py @@ -2,8 +2,9 @@ try: import usocket as socket + import uerrno as errno except: - import socket + import socket, errno def test(peer_addr): @@ -12,7 +13,7 @@ def test(peer_addr): try: s.connect(peer_addr) except OSError as er: - print(er.args[0] == 115) # 115 is EINPROGRESS + print(er.args[0] == errno.EINPROGRESS) s.close() From a1a28157993756255eca067acb23145b08151546 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 13:25:11 +1100 Subject: [PATCH 0616/3533] extmod/nimble: Ensure handle is set on read error. On error, the handle is only available on err->att_handle rather than in attr->handle used in the non-error case. Signed-off-by: Jim Mussared --- extmod/nimble/modbluetooth_nimble.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index a2eb8a588c67e..3910c599cb72b 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -1281,14 +1281,15 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start } STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - DEBUG_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); + uint16_t handle = attr ? attr->handle : (error ? error->att_handle : 0xffff); + DEBUG_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); if (!mp_bluetooth_is_active()) { return 0; } if (error->status == 0) { gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, attr->handle, attr->om); } - mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, attr ? attr->handle : -1, error->status); + mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_READ_DONE, conn_handle, handle, error->status); return 0; } @@ -1302,11 +1303,12 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { } STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - DEBUG_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, attr ? attr->handle : -1); + uint16_t handle = attr ? attr->handle : (error ? error->att_handle : 0xffff); + DEBUG_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); if (!mp_bluetooth_is_active()) { return 0; } - mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE, conn_handle, attr->handle, error->status); + mp_bluetooth_gattc_on_read_write_status(MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE, conn_handle, handle, error->status); return 0; } From fce0bd1a2af5aebd81d30d79b50210c80f768bc4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 1 Feb 2021 13:12:23 +1100 Subject: [PATCH 0617/3533] extmod/moduselect: Fix unsigned/signed comparison for timeout!=-1. Signed-off-by: Jim Mussared --- extmod/moduselect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 80beb8e09b8aa..6d8249f42758f 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -148,7 +148,7 @@ STATIC mp_obj_t select_select(size_t n_args, const mp_obj_t *args) { // poll the objects mp_uint_t n_ready = poll_map_poll(&poll_map, rwx_len); - if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) { + if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_tick >= timeout)) { // one or more objects are ready, or we had a timeout mp_obj_t list_array[3]; list_array[0] = mp_obj_new_list(rwx_len[0], NULL); @@ -250,7 +250,7 @@ STATIC mp_uint_t poll_poll_internal(uint n_args, const mp_obj_t *args) { for (;;) { // poll the objects n_ready = poll_map_poll(&self->poll_map, NULL); - if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) { + if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_tick >= timeout)) { break; } MICROPY_EVENT_POLL_HOOK From 4c54012373e6546704f534d0de3e900704ae9d11 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Feb 2021 15:49:20 +1100 Subject: [PATCH 0618/3533] unix/moduselect: Don't allow both posix and non-posix configurations. Signed-off-by: Jim Mussared --- ports/unix/moduselect.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index 6a0ee79aa6f0d..a9390a146bd05 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -29,6 +29,10 @@ #if MICROPY_PY_USELECT_POSIX +#if MICROPY_PY_USELECT +#error "Can't have both MICROPY_PY_USELECT and MICROPY_PY_USELECT_POSIX." +#endif + #include #include #include From 5e96e89999cd3b922dbc1a6ed473b44c81db92f4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 14:14:07 +1100 Subject: [PATCH 0619/3533] extmod/uasyncio: Add ThreadSafeFlag. This is a MicroPython-extension that allows for code running in IRQ (hard or soft) or scheduler context to sequence asyncio code. Signed-off-by: Jim Mussared --- extmod/uasyncio/__init__.py | 1 + extmod/uasyncio/event.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/extmod/uasyncio/__init__.py b/extmod/uasyncio/__init__.py index 08f924cf29b9b..fa64438f6b2a0 100644 --- a/extmod/uasyncio/__init__.py +++ b/extmod/uasyncio/__init__.py @@ -10,6 +10,7 @@ "wait_for_ms": "funcs", "gather": "funcs", "Event": "event", + "ThreadSafeFlag": "event", "Lock": "lock", "open_connection": "stream", "start_server": "stream", diff --git a/extmod/uasyncio/event.py b/extmod/uasyncio/event.py index 31cb00e055deb..c28ad1fb31687 100644 --- a/extmod/uasyncio/event.py +++ b/extmod/uasyncio/event.py @@ -14,6 +14,8 @@ def is_set(self): def set(self): # Event becomes set, schedule any tasks waiting on it + # Note: This must not be called from anything except the thread running + # the asyncio loop (i.e. neither hard or soft IRQ, or a different thread). while self.waiting.peek(): core._task_queue.push_head(self.waiting.pop_head()) self.state = True @@ -29,3 +31,32 @@ async def wait(self): core.cur_task.data = self.waiting yield return True + + +# MicroPython-extension: This can be set from outside the asyncio event loop, +# such as other threads, IRQs or scheduler context. Implementation is a stream +# that asyncio will poll until a flag is set. +# Note: Unlike Event, this is self-clearing. +try: + import uio + + class ThreadSafeFlag(uio.IOBase): + def __init__(self): + self._flag = 0 + + def ioctl(self, req, flags): + if req == 3: # MP_STREAM_POLL + return self._flag * flags + return None + + def set(self): + self._flag = 1 + + async def wait(self): + if not self._flag: + yield core._io_queue.queue_read(self) + self._flag = 0 + + +except ImportError: + pass From cdf9c8648f81dd4f2795a71b075aab75f01a9f1f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Feb 2021 17:28:15 +1100 Subject: [PATCH 0620/3533] docs/library/uasyncio.rst: Add docs for ThreadSafeFlag. Signed-off-by: Jim Mussared --- docs/library/uasyncio.rst | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/library/uasyncio.rst b/docs/library/uasyncio.rst index 3316f908d6364..1efee56e9688b 100644 --- a/docs/library/uasyncio.rst +++ b/docs/library/uasyncio.rst @@ -125,6 +125,9 @@ class Event Set the event. Any tasks waiting on the event will be scheduled to run. + Note: This must be called from within a task. It is not safe to call this + from an IRQ, scheduler callback, or other thread. See `ThreadSafeFlag`. + .. method:: Event.clear() Clear the event. @@ -136,6 +139,29 @@ class Event This is a coroutine. +class ThreadSafeFlag +-------------------- + +.. class:: ThreadSafeFlag() + + Create a new flag which can be used to synchronise a task with code running + outside the asyncio loop, such as other threads, IRQs, or scheduler + callbacks. Flags start in the cleared state. + +.. method:: ThreadSafeFlag.set() + + Set the flag. If there is a task waiting on the event, it will be scheduled + to run. + +.. method:: ThreadSafeFlag.wait() + + Wait for the flag to be set. If the flag is already set then it returns + immediately. + + A flag may only be waited on by a single task at a time. + + This is a coroutine. + class Lock ---------- @@ -188,7 +214,7 @@ TCP stream connections This is a coroutine. .. class:: Stream() - + This represents a TCP stream connection. To minimise code this class implements both a reader and a writer, and both ``StreamReader`` and ``StreamWriter`` alias to this class. From 83d23059ef80d0d28d1e0769a721c83665b0095f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Feb 2021 17:28:39 +1100 Subject: [PATCH 0621/3533] tests/extmod: Add test for ThreadSafeFlag. Signed-off-by: Jim Mussared --- tests/extmod/uasyncio_threadsafeflag.py | 79 +++++++++++++++++++++ tests/extmod/uasyncio_threadsafeflag.py.exp | 21 ++++++ 2 files changed, 100 insertions(+) create mode 100644 tests/extmod/uasyncio_threadsafeflag.py create mode 100644 tests/extmod/uasyncio_threadsafeflag.py.exp diff --git a/tests/extmod/uasyncio_threadsafeflag.py b/tests/extmod/uasyncio_threadsafeflag.py new file mode 100644 index 0000000000000..4e002a3d2a0b1 --- /dev/null +++ b/tests/extmod/uasyncio_threadsafeflag.py @@ -0,0 +1,79 @@ +# Test Event class + +try: + import uasyncio as asyncio +except ImportError: + print("SKIP") + raise SystemExit + + +import micropython + +try: + micropython.schedule +except AttributeError: + print("SKIP") + raise SystemExit + + +try: + # Unix port can't select/poll on user-defined types. + import uselect as select + + poller = select.poll() + poller.register(asyncio.ThreadSafeFlag()) +except TypeError: + print("SKIP") + raise SystemExit + + +async def task(id, flag): + print("task", id) + await flag.wait() + print("task", id, "done") + + +def set_from_schedule(flag): + print("schedule") + flag.set() + print("schedule done") + + +async def main(): + flag = asyncio.ThreadSafeFlag() + + # Set the flag from within the loop. + t = asyncio.create_task(task(1, flag)) + print("yield") + await asyncio.sleep(0) + print("set event") + flag.set() + print("yield") + await asyncio.sleep(0) + print("wait task") + await t + + # Set the flag from scheduler context. + print("----") + t = asyncio.create_task(task(2, flag)) + print("yield") + await asyncio.sleep(0) + print("set event") + micropython.schedule(set_from_schedule, flag) + print("yield") + await asyncio.sleep(0) + print("wait task") + await t + + # Flag already set. + print("----") + print("set event") + flag.set() + t = asyncio.create_task(task(3, flag)) + print("yield") + await asyncio.sleep(0) + print("wait task") + await t + + +asyncio.run(main()) diff --git a/tests/extmod/uasyncio_threadsafeflag.py.exp b/tests/extmod/uasyncio_threadsafeflag.py.exp new file mode 100644 index 0000000000000..aef4e479ba447 --- /dev/null +++ b/tests/extmod/uasyncio_threadsafeflag.py.exp @@ -0,0 +1,21 @@ +yield +task 1 +set event +yield +wait task +task 1 done +---- +yield +task 2 +set event +yield +schedule +schedule done +wait task +task 2 done +---- +set event +yield +task 3 +task 3 done +wait task From 566020034fc64448d33718c575809fcfe8f271db Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 16:11:38 +1100 Subject: [PATCH 0622/3533] tools/makemanifest.py: Allow passing option args to include(). This allows customising which features can be enabled in a frozen library. e.g. `include("path.py", extra_features=True)` in path.py: options.defaults(standard_features=True) if options.standard_features: # freeze standard modules. if options.extra_features: # freeze extra modules. Signed-off-by: Jim Mussared --- tools/makemanifest.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index c07a3a6c77ab2..117d1536e87d1 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -34,13 +34,27 @@ # Public functions to be used in the manifest -def include(manifest): +def include(manifest, **kwargs): """Include another manifest. The manifest argument can be a string (filename) or an iterable of strings. Relative paths are resolved with respect to the current manifest file. + + Optional kwargs can be provided which will be available to the + included script via the `options` variable. + + e.g. include("path.py", extra_features=True) + + in path.py: + options.defaults(standard_features=True) + + # freeze minimal modules. + if options.standard_features: + # freeze standard modules. + if options.extra_features: + # freeze extra modules. """ if not isinstance(manifest, str): @@ -53,7 +67,7 @@ def include(manifest): # Applies to includes and input files. prev_cwd = os.getcwd() os.chdir(os.path.dirname(manifest)) - exec(f.read()) + exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)}) os.chdir(prev_cwd) @@ -125,6 +139,18 @@ def freeze_mpy(path, script=None, opt=0): manifest_list = [] +class IncludeOptions: + def __init__(self, **kwargs): + self._kwargs = kwargs + self._defaults = {} + + def defaults(self, **kwargs): + self._defaults = kwargs + + def __getattr__(self, name): + return self._kwargs.get(name, self._defaults.get(name, None)) + + class FreezeError(Exception): pass From 2aa57931a6e23e39adddc717c25fa8a499966cb9 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Mon, 15 Feb 2021 08:51:48 -0600 Subject: [PATCH 0623/3533] zephyr: Update to zephyr v2.5.0. Updates the zephyr port build instructions and CI to use the latest zephyr release tag. Signed-off-by: Maureen Helm --- ports/zephyr/README.md | 6 +++--- tools/ci.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/zephyr/README.md b/ports/zephyr/README.md index d69030dc2c712..d1cc841977fe4 100644 --- a/ports/zephyr/README.md +++ b/ports/zephyr/README.md @@ -4,7 +4,7 @@ MicroPython port to Zephyr RTOS This is a work-in-progress port of MicroPython to Zephyr RTOS (http://zephyrproject.org). -This port requires Zephyr version 2.4.0, and may also work on higher +This port requires Zephyr version v2.5.0, and may also work on higher versions. All boards supported by Zephyr (with standard level of features support, like UART console) should work with MicroPython (but not all were tested). @@ -38,13 +38,13 @@ setup is correct. If you already have Zephyr installed but are having issues building the MicroPython port then try installing the correct version of Zephyr via: - $ west init zephyrproject -m https://github.com/zephyrproject-rtos/zephyr --mr v2.4.0 + $ west init zephyrproject -m https://github.com/zephyrproject-rtos/zephyr --mr v2.5.0 Alternatively, you don't have to redo the Zephyr installation to just switch from master to a tagged release, you can instead do: $ cd zephyrproject/zephyr - $ git checkout v2.4.0 + $ git checkout v2.5.0 $ west update With Zephyr installed you may then need to configure your environment, diff --git a/tools/ci.sh b/tools/ci.sh index 4f99551468c59..d945375562551 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -466,7 +466,7 @@ function ci_zephyr_setup { } function ci_zephyr_install { - docker exec zephyr-ci west init --mr v2.4.0 /zephyrproject + docker exec zephyr-ci west init --mr v2.5.0 /zephyrproject docker exec -w /zephyrproject zephyr-ci west update docker exec -w /zephyrproject zephyr-ci west zephyr-export } From dff6fc64d23c548dce2b43096c3f1522db303de8 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Tue, 10 Nov 2020 19:05:30 -0600 Subject: [PATCH 0624/3533] py: Expand lists in core cmake custom commands. The core cmake rules use custom commands to invoke qstr processing scripts. For the zephyr port, it's possible that list arguments to these commands may contain generator expressions, therefore we need to expand them properly. Signed-off-by: Maureen Helm --- py/mkrules.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/py/mkrules.cmake b/py/mkrules.cmake index bdff385815a44..e05dcb836a211 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -73,6 +73,7 @@ add_custom_command( DEPENDS ${MICROPY_MODULEDEFS} ${MICROPY_SOURCE_QSTR} VERBATIM + COMMAND_EXPAND_LISTS ) add_custom_command( @@ -81,6 +82,7 @@ add_custom_command( COMMAND touch ${MICROPY_QSTR_DEFS_SPLIT} DEPENDS ${MICROPY_QSTR_DEFS_LAST} VERBATIM + COMMAND_EXPAND_LISTS ) add_custom_command( @@ -88,6 +90,7 @@ add_custom_command( COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py cat qstr _ ${MICROPY_GENHDR_DIR}/qstr ${MICROPY_QSTR_DEFS_COLLECTED} DEPENDS ${MICROPY_QSTR_DEFS_SPLIT} VERBATIM + COMMAND_EXPAND_LISTS ) add_custom_command( @@ -95,6 +98,7 @@ add_custom_command( COMMAND cat ${MICROPY_PY_QSTRDEFS} ${MICROPY_QSTR_DEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTR_DEFS_PREPROCESSED} DEPENDS ${MICROPY_QSTR_DEFS_COLLECTED} VERBATIM + COMMAND_EXPAND_LISTS ) add_custom_command( @@ -102,6 +106,7 @@ add_custom_command( COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdata.py ${MICROPY_QSTR_DEFS_PREPROCESSED} > ${MICROPY_QSTR_DEFS_GENERATED} DEPENDS ${MICROPY_QSTR_DEFS_PREPROCESSED} VERBATIM + COMMAND_EXPAND_LISTS ) # Build frozen code if enabled From f49a73641ac677ea49a031ae7efc5822441f0cb5 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sun, 18 Oct 2020 14:54:38 -0500 Subject: [PATCH 0625/3533] zephyr: Disable frozen source modules. Disables frozen source modules in the zephyr port. They are deprecated in the makefile rules and not implemented in the new cmake rules. Signed-off-by: Maureen Helm --- ports/zephyr/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 6bfd9ff884f1b..966f7f7e94a1d 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -110,7 +110,7 @@ #define MICROPY_HW_MCU_NAME "unknown-cpu" #endif -#define MICROPY_MODULE_FROZEN_STR (1) +#define MICROPY_MODULE_FROZEN_STR (0) typedef int mp_int_t; // must be pointer size typedef unsigned mp_uint_t; // must be pointer size From 51fa1339f12bace6be49647336f8c9bf57302d4d Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Fri, 28 Aug 2020 14:26:31 -0500 Subject: [PATCH 0626/3533] zephyr: Remove unused build files. Removes zephyr port build files that aren't being used anymore. Signed-off-by: Maureen Helm --- ports/zephyr/Kbuild | 3 --- ports/zephyr/Makefile.zephyr | 30 ------------------------------ ports/zephyr/src/Makefile | 17 ----------------- ports/zephyr/z_config.mk | 17 ----------------- 4 files changed, 67 deletions(-) delete mode 100644 ports/zephyr/Kbuild delete mode 100644 ports/zephyr/Makefile.zephyr delete mode 100644 ports/zephyr/src/Makefile delete mode 100644 ports/zephyr/z_config.mk diff --git a/ports/zephyr/Kbuild b/ports/zephyr/Kbuild deleted file mode 100644 index 9e656d5f48a9b..0000000000000 --- a/ports/zephyr/Kbuild +++ /dev/null @@ -1,3 +0,0 @@ -#subdir-ccflags-y += -I$(SOURCE_DIR)/../mylib/include - -obj-y += src/ diff --git a/ports/zephyr/Makefile.zephyr b/ports/zephyr/Makefile.zephyr deleted file mode 100644 index 16f0a9452f02b..0000000000000 --- a/ports/zephyr/Makefile.zephyr +++ /dev/null @@ -1,30 +0,0 @@ -# -# Copyright (c) 2016 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -KERNEL_TYPE = micro -# BOARD must be passed on command line from main Makefile -#BOARD = -CONF_FILE = prj.conf -QEMU_NET = 1 - -#export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world -export LDFLAGS_zephyr += -L$(CURDIR) -export ALL_LIBS += micropython - -include ${ZEPHYR_BASE}/Makefile.inc -ifeq ($(QEMU_NET), 1) -include ${ZEPHYR_BASE}/samples/net/common/Makefile.ipstack -endif diff --git a/ports/zephyr/src/Makefile b/ports/zephyr/src/Makefile deleted file mode 100644 index 36dd8c64efe6a..0000000000000 --- a/ports/zephyr/src/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -# -# Copyright (c) 2016 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -obj-y += zephyr_start.o zephyr_getchar.o diff --git a/ports/zephyr/z_config.mk b/ports/zephyr/z_config.mk deleted file mode 100644 index 28addd8f29fc7..0000000000000 --- a/ports/zephyr/z_config.mk +++ /dev/null @@ -1,17 +0,0 @@ -srctree = $(ZEPHYR_BASE) - -include $(Z_DOTCONFIG) -override ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH)) -SOC_NAME = $(subst $(DQUOTE),,$(CONFIG_SOC)) -SOC_SERIES = $(subst $(DQUOTE),,$(CONFIG_SOC_SERIES)) -SOC_FAMILY = $(subst $(DQUOTE),,$(CONFIG_SOC_FAMILY)) -ifeq ($(SOC_SERIES),) -SOC_PATH = $(SOC_NAME) -else -SOC_PATH = $(SOC_FAMILY)/$(SOC_SERIES) -endif - -KBUILD_CFLAGS := -c -include $(ZEPHYR_BASE)/scripts/Kbuild.include - -include $(ZEPHYR_BASE)/arch/$(ARCH)/Makefile From f573e73baeddfb8fde59413e8053b538cf908f9b Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Sun, 11 Oct 2020 13:11:31 -0500 Subject: [PATCH 0627/3533] zephyr: Build MicroPython as a cmake target. Refactors the zephyr build infrastructure to build MicroPython as a cmake target, using the recently introduced core cmake rules. This change makes it possible to build the zephyr port like most other zephyr applications using west or cmake directly. It simplifies building with extra cmake arguments, such as specifying an alternate conf file or adding an Arduino shield. It also enables building the zephyr port anywhere in the host file system, which will allow regressing across multiple boards with the zephyr twister script. Signed-off-by: Maureen Helm --- ports/zephyr/.gitignore | 1 - ports/zephyr/CMakeLists.txt | 134 ++++++++++++++++++++++++++++++------ ports/zephyr/Kconfig | 46 +++++++++++++ ports/zephyr/Makefile | 116 ------------------------------- ports/zephyr/README.md | 56 +++++++-------- ports/zephyr/make-minimal | 18 ----- ports/zephyr/prj.conf | 5 ++ tools/ci.sh | 13 ++-- 8 files changed, 201 insertions(+), 188 deletions(-) delete mode 100644 ports/zephyr/.gitignore create mode 100644 ports/zephyr/Kconfig delete mode 100644 ports/zephyr/Makefile delete mode 100755 ports/zephyr/make-minimal diff --git a/ports/zephyr/.gitignore b/ports/zephyr/.gitignore deleted file mode 100644 index 00ca089d1598e..0000000000000 --- a/ports/zephyr/.gitignore +++ /dev/null @@ -1 +0,0 @@ -outdir/ diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 50cd031d4dd08..08868d716dc9e 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -1,24 +1,118 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright 2020 NXP +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + cmake_minimum_required(VERSION 3.13.1) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(NONE) - -target_sources(app PRIVATE src/zephyr_start.c src/zephyr_getchar.c) - -add_library(libmicropython STATIC IMPORTED) -set_target_properties(libmicropython PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a) -target_link_libraries(app PUBLIC libmicropython) - -zephyr_get_include_directories_for_lang_as_string(C includes) -zephyr_get_system_include_directories_for_lang_as_string(C system_includes) -zephyr_get_compile_definitions_for_lang_as_string(C definitions) -zephyr_get_compile_options_for_lang_as_string(C options) - -add_custom_target( - outputexports - COMMAND echo CC="${CMAKE_C_COMPILER}" - COMMAND echo AR="${CMAKE_AR}" - COMMAND echo Z_CFLAGS=${system_includes} ${includes} ${definitions} ${options} - VERBATIM - USES_TERMINAL +project(micropython) + +set(MICROPY_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(MICROPY_DIR ${MICROPY_PORT_DIR}/../..) +set(MICROPY_TARGET micropython) + +include(${MICROPY_DIR}/py/py.cmake) +include(${MICROPY_DIR}/extmod/extmod.cmake) + +set(MICROPY_SOURCE_PORT + main.c + help.c + machine_i2c.c + machine_pin.c + machine_uart.c + modmachine.c + moduos.c + modusocket.c + modutime.c + modzephyr.c + modzsensor.c + uart_core.c + zephyr_storage.c +) +list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/) + +set(MICROPY_SOURCE_LIB + timeutils/timeutils.c + utils/mpirq.c + utils/stdout_helpers.c + utils/printf.c + utils/pyexec.c + utils/interrupt_char.c + mp-readline/readline.c + oofatfs/ff.c + oofatfs/ffunicode.c + littlefs/lfs1.c + littlefs/lfs1_util.c + littlefs/lfs2.c + littlefs/lfs2_util.c ) +list(TRANSFORM MICROPY_SOURCE_LIB PREPEND ${MICROPY_DIR}/lib/) + +set(MICROPY_SOURCE_QSTR + ${MICROPY_SOURCE_PY} + ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_LIB} + ${MICROPY_SOURCE_PORT} +) + +zephyr_get_include_directories_for_lang(C includes) +zephyr_get_system_include_directories_for_lang(C system_includes) +zephyr_get_compile_definitions_for_lang(C definitions) +zephyr_get_compile_options_for_lang(C options) + +set(MICROPY_CPP_FLAGS_EXTRA ${includes} ${system_includes} ${definitions} ${options}) + +zephyr_library_named(${MICROPY_TARGET}) + +zephyr_library_include_directories( + ${MICROPY_DIR} + ${MICROPY_PORT_DIR} + ${CMAKE_CURRENT_BINARY_DIR} +) + +zephyr_library_compile_options( + -std=gnu99 -fomit-frame-pointer +) + +zephyr_library_compile_definitions( + NDEBUG + MP_CONFIGFILE=<${CONFIG_MICROPY_CONFIGFILE}> + MICROPY_HEAP_SIZE=${CONFIG_MICROPY_HEAP_SIZE} + FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\" + MICROPY_VFS_FAT=$ + MICROPY_VFS_LFS1=$ + MICROPY_VFS_LFS2=$ +) + +zephyr_library_sources(${MICROPY_SOURCE_QSTR}) + +add_dependencies(${MICROPY_TARGET} zephyr_generated_headers) + +include(${MICROPY_DIR}/py/mkrules.cmake) + +target_sources(app PRIVATE + src/zephyr_start.c + src/zephyr_getchar.c +) + +target_link_libraries(app PRIVATE ${MICROPY_TARGET}) diff --git a/ports/zephyr/Kconfig b/ports/zephyr/Kconfig new file mode 100644 index 0000000000000..5545cf3b15b07 --- /dev/null +++ b/ports/zephyr/Kconfig @@ -0,0 +1,46 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright 2020 NXP +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +menu "MicroPython Options" + +config MICROPY_CONFIGFILE + string "Configuration file" + default "mpconfigport_minimal.h" + +config MICROPY_HEAP_SIZE + int "Heap size" + default 16384 + +config MICROPY_VFS_FAT + bool "FatFS file system" + +config MICROPY_VFS_LFS1 + bool "LittleFs version 1 file system" + +config MICROPY_VFS_LFS2 + bool "LittleFs version 2 file system" + +endmenu # MicroPython Options + +source "Kconfig.zephyr" diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile deleted file mode 100644 index c200062a3ff85..0000000000000 --- a/ports/zephyr/Makefile +++ /dev/null @@ -1,116 +0,0 @@ -# -# This is the main Makefile, which uses MicroPython build system, -# but Zephyr arch-specific toolchain and target-specific flags. -# This Makefile builds MicroPython as a library, and then calls -# recursively Makefile.zephyr to build complete application binary -# using Zephyr build system. -# -# To build a "minimal" configuration, use "make-minimal" wrapper. - -BOARD ?= qemu_x86 -OUTDIR_PREFIX = $(BOARD) - -# Default heap size is 16KB, which is on conservative side, to let -# it build for smaller boards, but it won't be enough for larger -# applications, and will need to be increased. -MICROPY_HEAP_SIZE = 16384 -FROZEN_DIR = scripts - -MICROPY_VFS_FAT ?= 1 -MICROPY_VFS_LFS1 ?= 0 -MICROPY_VFS_LFS2 ?= 1 - -# Default target -all: - -include ../../py/mkenv.mk -include $(TOP)/py/py.mk - -# Zephyr (generated) config files - must be defined before include below -Z_EXPORTS = outdir/$(OUTDIR_PREFIX)/Makefile.export -ifneq ($(MAKECMDGOALS), clean) -include $(Z_EXPORTS) -endif - -INC += -I. -INC += -I$(TOP) -INC += -I$(BUILD) -INC += -I$(ZEPHYR_BASE)/net/ip -INC += -I$(ZEPHYR_BASE)/net/ip/contiki -INC += -I$(ZEPHYR_BASE)/net/ip/contiki/os - -SRC_C = main.c \ - help.c \ - moduos.c \ - modusocket.c \ - modutime.c \ - modzephyr.c \ - modzsensor.c \ - modmachine.c \ - machine_i2c.c \ - machine_pin.c \ - machine_uart.c \ - uart_core.c \ - zephyr_storage.c \ - lib/timeutils/timeutils.c \ - lib/utils/mpirq.c \ - lib/utils/stdout_helpers.c \ - lib/utils/printf.c \ - lib/utils/pyexec.c \ - lib/utils/interrupt_char.c \ - lib/mp-readline/readline.c \ - $(SRC_MOD) - -# List of sources for qstr extraction -SRC_QSTR += $(SRC_C) - -OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) - -CFLAGS = $(Z_CFLAGS) \ - -std=gnu99 -fomit-frame-pointer -DNDEBUG -DMICROPY_HEAP_SIZE=$(MICROPY_HEAP_SIZE) $(CFLAGS_MOD) $(CFLAGS_EXTRA) $(INC) - -include $(TOP)/py/mkrules.mk - -GENERIC_TARGETS = all zephyr run qemu qemugdb flash debug debugserver \ - ram_report rom_report -KCONFIG_TARGETS = \ - initconfig config nconfig menuconfig xconfig gconfig \ - oldconfig silentoldconfig defconfig savedefconfig \ - allnoconfig allyesconfig alldefconfig randconfig \ - listnewconfig olddefconfig -CLEAN_TARGETS = pristine mrproper - -$(GENERIC_TARGETS): $(LIBMICROPYTHON) -$(CLEAN_TARGETS): clean - -$(GENERIC_TARGETS) $(KCONFIG_TARGETS) $(CLEAN_TARGETS): - $(MAKE) -C outdir/$(BOARD) $@ - -$(LIBMICROPYTHON): | $(Z_EXPORTS) -build/genhdr/qstr.i.last: | $(Z_EXPORTS) - -# If we recreate libmicropython, also cause zephyr.bin relink -LIBMICROPYTHON_EXTRA_CMD = -$(RM) -f outdir/$(OUTDIR_PREFIX)/zephyr.lnk - -# MicroPython's global clean cleans everything, fast -CLEAN_EXTRA = outdir libmicropython.a - -# Clean Zephyr things in Zephyr way -z_clean: - $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) clean - -test: - cd $(TOP)/tests && ./run-tests --target minimal --device "execpty:make -C ../ports/zephyr run BOARD=$(BOARD) QEMU_PTY=1" - -cmake: outdir/$(BOARD)/Makefile - -ifneq ($(CONF_FILE),) -CMAKE_MOD += -DCONF_FILE=$(CONF_FILE) -endif - -outdir/$(BOARD)/Makefile: - mkdir -p outdir/$(BOARD) && cmake -DBOARD=$(BOARD) $(CMAKE_MOD) -Boutdir/$(BOARD) -H. - -$(Z_EXPORTS): outdir/$(BOARD)/Makefile - make --no-print-directory -C outdir/$(BOARD) outputexports CMAKE_COMMAND=: >$@ - make -C outdir/$(BOARD) syscall_list_h_target kobj_types_h_target diff --git a/ports/zephyr/README.md b/ports/zephyr/README.md index d1cc841977fe4..cdc3c70f0e616 100644 --- a/ports/zephyr/README.md +++ b/ports/zephyr/README.md @@ -50,47 +50,48 @@ switch from master to a tagged release, you can instead do: With Zephyr installed you may then need to configure your environment, for example by sourcing `zephyrproject/zephyr/zephyr-env.sh`. -Once Zephyr is ready to use you can build the MicroPython port. -In the port subdirectory `ports/zephyr/` run: +Once Zephyr is ready to use you can build the MicroPython port just like any +other Zephyr application. You can do this anywhere in your file system, it does +not have to be in the `ports/zephyr` directory. Assuming you have cloned the +MicroPython repository into your home directory, you can build the Zephyr port +for a frdm_k64f board like this: - $ make BOARD= + $ west build -b frdm_k64f ~/micropython/ports/zephyr -If you don't specify BOARD, the default is `qemu_x86` (x86 target running -in QEMU emulator). Consult the Zephyr documentation above for the list of +To build for QEMU instead: + + $ west build -b qemu_x86 ~/micropython/ports/zephyr + +Consult the Zephyr documentation above for the list of supported boards. Board configuration files appearing in `ports/zephyr/boards/` correspond to boards that have been tested with MicroPython and may have additional options enabled, like filesystem support. - Running ------- -To run the resulting firmware in QEMU (for BOARDs like qemu_x86, -qemu_cortex_m3): +To flash the resulting firmware to your board: - make run + $ west flash -With the default configuration, networking is now enabled, so you need to -follow instructions in https://wiki.zephyrproject.org/view/Networking-with-Qemu -to setup host side of TAP/SLIP networking. If you get error like: +Or, to flash it to your board and start a gdb debug session: - could not connect serial device to character backend 'unix:/tmp/slip.sock' + $ west debug -it's a sign that you didn't followed instructions above. If you would like -to just run it quickly without extra setup, see "minimal" build below. +To run the resulting firmware in QEMU (for BOARDs like qemu_x86, +qemu_cortex_m3): -For deploying/flashing a firmware on a real board, follow Zephyr -documentation for a given board, including known issues for that board -(if any). (Mind again that networking is enabled for the default build, -so you should know if there're any special requirements in that regard, -cf. for example QEMU networking requirements above; real hardware boards -generally should not have any special requirements, unless there're known -issues). + $ west build -t run -For example, to deploy firmware on the FRDM-K64F board run: +Networking is enabled with the default configuration, so you need to follow +instructions in +https://docs.zephyrproject.org/latest/guides/networking/qemu_setup.html#networking-with-qemu +to setup the host side of TAP/SLIP networking. If you get an error like: - $ make BOARD=frdm_k64f flash + could not connect serial device to character backend 'unix:/tmp/slip.sock' +it's a sign that you didn't follow the instructions above. If you would like +to just run it quickly without extra setup, see "minimal" build below. Quick example ------------- @@ -151,9 +152,10 @@ enabled over time. To make a minimal build: - ./make-minimal BOARD= + $ west build -b qemu_x86 ~/micropython/ports/zephyr -- -DCONF_FILE=prj_minimal.conf To run a minimal build in QEMU without requiring TAP networking setup -run the following after you built image with the previous command: +run the following after you built an image with the previous command: + + $ west build -t run - ./make-minimal BOARD= run diff --git a/ports/zephyr/make-minimal b/ports/zephyr/make-minimal deleted file mode 100755 index d7dddc33427df..0000000000000 --- a/ports/zephyr/make-minimal +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -# -# This is a wrapper for make to build a "minimal" Zephyr port. -# It should be run just like make (i.e. extra vars can be passed on the -# command line, etc.), e.g.: -# -# ./make-minimal BOARD=qemu_cortex_m3 -# ./make-minimal BOARD=qemu_cortex_m3 run -# - -make \ - CONF_FILE=prj_minimal.conf \ - CFLAGS_EXTRA='-DMP_CONFIGFILE=""' \ - MICROPY_VFS_FAT=0 \ - MICROPY_VFS_LFS2=0 \ - FROZEN_DIR= \ - QEMU_NET=0 \ - "$@" diff --git a/ports/zephyr/prj.conf b/ports/zephyr/prj.conf index 9824f48e3362f..c9c2e96da9beb 100644 --- a/ports/zephyr/prj.conf +++ b/ports/zephyr/prj.conf @@ -69,3 +69,8 @@ CONFIG_NET_BUF_POOL_USAGE=y #CONFIG_NET_DEBUG_NET_BUF=y # Change to 4 for "DEBUG" level #CONFIG_SYS_LOG_NET_LEVEL=3 + +# MicroPython options +CONFIG_MICROPY_CONFIGFILE="mpconfigport.h" +CONFIG_MICROPY_VFS_FAT=y +CONFIG_MICROPY_VFS_LFS2=y diff --git a/tools/ci.sh b/tools/ci.sh index d945375562551..616e78f1976ac 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -460,6 +460,7 @@ function ci_zephyr_setup { -v "$(pwd)":/micropython \ -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.11.3 \ -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr \ + -e ZEPHYR_BASE=/zephyrproject/zephyr \ -w /micropython/ports/zephyr \ zephyrprojectrtos/ci:v0.11.8 docker ps -a @@ -472,10 +473,10 @@ function ci_zephyr_install { } function ci_zephyr_build { - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS}" - docker exec zephyr-ci bash -c "make clean; ./make-minimal ${MAKEOPTS} BOARD=frdm_k64f" - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS}" - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=frdm_k64f" - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=mimxrt1050_evk" - docker exec zephyr-ci bash -c "make clean; make ${MAKEOPTS} BOARD=reel_board" + docker exec zephyr-ci west build -p auto -b qemu_x86 -- -DCONF_FILE=prj_minimal.conf + docker exec zephyr-ci west build -p auto -b frdm_k64f -- -DCONF_FILE=prj_minimal.conf + docker exec zephyr-ci west build -p auto -b qemu_x86 + docker exec zephyr-ci west build -p auto -b frdm_k64f + docker exec zephyr-ci west build -p auto -b mimxrt1050_evk + docker exec zephyr-ci west build -p auto -b reel_board } From 56a36899bdb472d5c2003164079bedeba42fa24d Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Tue, 16 Feb 2021 08:59:53 -0600 Subject: [PATCH 0628/3533] tools/ci.sh: Update zephyr docker image to v0.11.13. Updates the zephyr docker image to the latest, v0.11.13. This updates CI to use zephyr SDK v0.12.2 and GCC v10.2.0 for the zephyr port. Signed-off-by: Maureen Helm --- tools/ci.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/ci.sh b/tools/ci.sh index 616e78f1976ac..5e0686f301bc0 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -455,14 +455,14 @@ function ci_windows_build { # ports/zephyr function ci_zephyr_setup { - docker pull zephyrprojectrtos/ci:v0.11.8 + docker pull zephyrprojectrtos/ci:v0.11.13 docker run --name zephyr-ci -d -it \ -v "$(pwd)":/micropython \ - -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.11.3 \ + -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.12.2 \ -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr \ -e ZEPHYR_BASE=/zephyrproject/zephyr \ -w /micropython/ports/zephyr \ - zephyrprojectrtos/ci:v0.11.8 + zephyrprojectrtos/ci:v0.11.13 docker ps -a } From 6c4a5d185d7e70c22c0f72fb1cd8add251600548 Mon Sep 17 00:00:00 2001 From: PTH Date: Thu, 11 Feb 2021 15:23:00 +0100 Subject: [PATCH 0629/3533] zephyr/boards: Add support for the nucleo_h743zi board. --- ports/zephyr/boards/nucleo_h743zi.conf | 7 +++++++ ports/zephyr/boards/nucleo_h743zi.overlay | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ports/zephyr/boards/nucleo_h743zi.conf create mode 100644 ports/zephyr/boards/nucleo_h743zi.overlay diff --git a/ports/zephyr/boards/nucleo_h743zi.conf b/ports/zephyr/boards/nucleo_h743zi.conf new file mode 100644 index 0000000000000..942415b7967d8 --- /dev/null +++ b/ports/zephyr/boards/nucleo_h743zi.conf @@ -0,0 +1,7 @@ +# disable console subsys to get REPL working +CONFIG_CONSOLE_SUBSYS=n + +# flash config +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y diff --git a/ports/zephyr/boards/nucleo_h743zi.overlay b/ports/zephyr/boards/nucleo_h743zi.overlay new file mode 100644 index 0000000000000..f5e6d4f92157f --- /dev/null +++ b/ports/zephyr/boards/nucleo_h743zi.overlay @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Thomas Popp + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* delete the already defined storage partition and create a new and bigger one */ +/delete-node/ &storage_partition; + +&flash0 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + + /* storage slot: 1024 KB in Flash Memory Bank 2 */ + storage_partition: partition@100000 { + label = "storage"; + reg = <0x00100000 0x100000>; + }; + }; +}; From 5cb91afb9b3b34bbf0ee022a3f69af90c01d1d54 Mon Sep 17 00:00:00 2001 From: PTH Date: Fri, 12 Feb 2021 15:08:02 +0100 Subject: [PATCH 0630/3533] zephyr/modusocket: Fix parameter in calls to net_context_get_XXX(). The following simple usocket example throws an error EINVAL on connect import usocket s = usocket.socket() s.connect(usocket.getaddrinfo('www.micropython.org', 80)[0][-1]) Traceback (most recent call last): File "", line 1, in OSError: [Errno 22] EINVAL Fixing the context parameter in calls of net_context_get_family() and net_context_get_type(), the connect works fine. Tested on a nucleo_h743zi board. --- ports/zephyr/modusocket.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c index 6c900753c8b51..f9fc96a2b1d5b 100644 --- a/ports/zephyr/modusocket.c +++ b/ports/zephyr/modusocket.c @@ -79,7 +79,8 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka mp_obj_t *addr_items; mp_obj_get_array_fixed_n(addr_in, 2, &addr_items); - sockaddr_in->sin_family = net_context_get_family((void *)socket->ctx); + void *context = zsock_get_context_object(socket->ctx); + sockaddr_in->sin_family = net_context_get_family(context); RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr)); sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1])); } @@ -119,8 +120,8 @@ STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin if (self->ctx == -1) { mp_printf(print, ""); } else { - struct net_context *ctx = (void *)self->ctx; - mp_printf(print, "", ctx, net_context_get_type(ctx)); + void *context = zsock_get_context_object(self->ctx); + mp_printf(print, "", self->ctx, net_context_get_type(context)); } } From 236274f08fb51d36eba9cd0458483bd398bfed0a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 14:31:29 +1100 Subject: [PATCH 0631/3533] extmod/nimble/hal/hal_uart: Fix HCI_TRACE format specifiers. Makes this work consistently on unix and stm32 ports. --- extmod/nimble/hal/hal_uart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/nimble/hal/hal_uart.c b/extmod/nimble/hal/hal_uart.c index 230970b089c55..cd5e49e30b4c0 100644 --- a/extmod/nimble/hal/hal_uart.c +++ b/extmod/nimble/hal/hal_uart.c @@ -66,7 +66,7 @@ void hal_uart_start_tx(uint32_t port) { } #if HCI_TRACE - printf("< [% 8d] %02x", mp_hal_ticks_ms(), mp_bluetooth_hci_cmd_buf[0]); + printf("< [% 8d] %02x", (int)mp_hal_ticks_ms(), mp_bluetooth_hci_cmd_buf[0]); for (size_t i = 1; i < len; ++i) { printf(":%02x", mp_bluetooth_hci_cmd_buf[i]); } @@ -86,7 +86,7 @@ void mp_bluetooth_nimble_hci_uart_process(bool run_events) { int chr; while ((chr = mp_bluetooth_hci_uart_readchar()) >= 0) { #if HCI_TRACE - printf("> %02x (%d)\n", chr); + printf("> %02x\n", chr); #endif hal_uart_rx_cb(hal_uart_rx_arg, chr); From 4005138882757ae536482c1d105b28c9869ab8ce Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Feb 2021 14:33:03 +1100 Subject: [PATCH 0632/3533] extmod/modbluetooth: Allow NimBLE to use Zephyr static address. Zephyr controllers can be queried for a static address (computed from the device ID). BlueKitchen already supports this, but make them both use the same macro to enable the feature. --- extmod/btstack/modbluetooth_btstack.c | 12 ++++++------ extmod/nimble/modbluetooth_nimble.c | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 540b0fb7a9d24..c861c76d22b0b 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -282,7 +282,7 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan } } -#if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS +#if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS // During startup, the controller (e.g. Zephyr) might give us a static address that we can use. STATIC uint8_t controller_static_addr[6] = {0}; STATIC bool controller_static_addr_available = false; @@ -349,13 +349,13 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t DEBUG_printf(" --> hci transport packet sent\n"); } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { DEBUG_printf(" --> hci command complete\n"); - #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) { DEBUG_printf(" --> static address available\n"); reverse_48(&packet[7], controller_static_addr); controller_static_addr_available = true; } - #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + #endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS } else if (event_type == HCI_EVENT_COMMAND_STATUS) { DEBUG_printf(" --> hci command status\n"); } else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) { @@ -575,12 +575,12 @@ STATIC bool set_public_address(void) { } STATIC void set_random_address(void) { - #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS if (controller_static_addr_available) { DEBUG_printf("set_random_address: Using static address supplied by controller.\n"); gap_random_address_set(controller_static_addr); } else - #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + #endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS { bd_addr_t static_addr; @@ -635,7 +635,7 @@ int mp_bluetooth_init(void) { btstack_memory_init(); - #if MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS + #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS controller_static_addr_available = false; #endif diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 3910c599cb72b..a0a3998c6614d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -48,6 +48,11 @@ #include "nimble/host/src/ble_l2cap_priv.h" #endif +#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD || MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS +// For ble_hs_hci_cmd_tx +#include "nimble/host/src/ble_hs_hci_priv.h" +#endif + #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE" #endif @@ -179,6 +184,14 @@ STATIC void set_random_address(bool nrpa) { // Mark it as STATIC (not RPA or NRPA). addr.val[5] |= 0xc0; } else + #elif MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS + if (!nrpa) { + DEBUG_printf("set_random_address: Generating static address from Zephyr controller\n"); + uint8_t buf[23]; + rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_VENDOR, 0x09), NULL, 0, buf, sizeof(buf)); + assert(rc == 0); + memcpy(addr.val, buf + 1, 6); + } else #endif { DEBUG_printf("set_random_address: Generating random static address\n"); @@ -1688,9 +1701,6 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf #if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD -// For ble_hs_hci_cmd_tx -#include "nimble/host/src/ble_hs_hci_priv.h" - int mp_bluetooth_hci_cmd(uint16_t ogf, uint16_t ocf, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_len, uint8_t *status) { int rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(ogf, ocf), req, req_len, resp, resp_len); if (rc < BLE_HS_ERR_HCI_BASE || rc >= BLE_HS_ERR_HCI_BASE + 0x100) { From 2eed9780ba7074de9e464a2bc771ad14f0332a6c Mon Sep 17 00:00:00 2001 From: David Michieli Date: Tue, 16 Feb 2021 12:34:34 +1100 Subject: [PATCH 0633/3533] stm32/mboot: Add unpack-dfu command to mboot_pack_dfu.py tool. This command unpacks a previously packed DFU file, writing out a DFU which should be the same as the original (before packing). --- ports/stm32/mboot/mboot_pack_dfu.py | 26 ++++++++++++++++++++++++++ tools/ci.sh | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/ports/stm32/mboot/mboot_pack_dfu.py b/ports/stm32/mboot/mboot_pack_dfu.py index 47382f5910187..540057e06ee8c 100644 --- a/ports/stm32/mboot/mboot_pack_dfu.py +++ b/ports/stm32/mboot/mboot_pack_dfu.py @@ -211,9 +211,16 @@ def pack_dfu(keys, args): def verify_pack_dfu(keys, filename): + """Verify packed dfu file against keys. Gathers decrypted binary data.""" full_sig = pyhy.hydro_sign(MBOOT_PACK_HYDRO_CONTEXT) _, elems = dfu_read(filename) + base_addr = None + binary_data = b"" + for addr, data in elems: + if base_addr is None: + base_addr = addr + header = struct.unpack(" Date: Thu, 2 Apr 2020 10:01:16 -0700 Subject: [PATCH 0634/3533] extmod/modussl: Fix ussl read/recv/send/write errors when non-blocking. Also fix related problems with socket on esp32, improve docs for wrap_socket, and add more tests. --- docs/library/ussl.rst | 22 +++- extmod/modussl_axtls.c | 31 ++++- extmod/modussl_mbedtls.c | 3 +- ports/esp32/modsocket.c | 8 +- tests/net_hosted/accept_timeout.py | 6 +- tests/net_hosted/connect_nonblock_xfer.py | 147 ++++++++++++++++++++++ tests/net_inet/ssl_errors.py | 51 ++++++++ tests/net_inet/test_tls_nonblock.py | 116 +++++++++++++++++ tests/net_inet/test_tls_sites.py | 6 +- tests/net_inet/test_tls_sites.py.exp | 3 +- 10 files changed, 373 insertions(+), 20 deletions(-) create mode 100644 tests/net_hosted/connect_nonblock_xfer.py create mode 100644 tests/net_inet/ssl_errors.py create mode 100644 tests/net_inet/test_tls_nonblock.py diff --git a/docs/library/ussl.rst b/docs/library/ussl.rst index ffe146331ca08..14e3f3ad142ce 100644 --- a/docs/library/ussl.rst +++ b/docs/library/ussl.rst @@ -13,16 +13,23 @@ facilities for network sockets, both client-side and server-side. Functions --------- -.. function:: ussl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None) - +.. function:: ussl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None, do_handshake=True) Takes a `stream` *sock* (usually usocket.socket instance of ``SOCK_STREAM`` type), and returns an instance of ssl.SSLSocket, which wraps the underlying stream in an SSL context. Returned object has the usual `stream` interface methods like - ``read()``, ``write()``, etc. In MicroPython, the returned object does not expose - socket interface and methods like ``recv()``, ``send()``. In particular, a - server-side SSL socket should be created from a normal socket returned from + ``read()``, ``write()``, etc. + A server-side SSL socket should be created from a normal socket returned from :meth:`~usocket.socket.accept()` on a non-SSL listening server socket. + - *do_handshake* determines whether the handshake is done as part of the ``wrap_socket`` + or whether it is deferred to be done as part of the initial reads or writes + (there is no ``do_handshake`` method as in CPython). + For blocking sockets doing the handshake immediately is standard. For non-blocking + sockets (i.e. when the *sock* passed into ``wrap_socket`` is in non-blocking mode) + the handshake should generally be deferred because otherwise ``wrap_socket`` blocks + until it completes. Note that in AXTLS the handshake can be deferred until the first + read or write but it then blocks until completion. + Depending on the underlying module implementation in a particular :term:`MicroPython port`, some or all keyword arguments above may be not supported. @@ -31,6 +38,11 @@ Functions Some implementations of ``ussl`` module do NOT validate server certificates, which makes an SSL connection established prone to man-in-the-middle attacks. + CPython's ``wrap_socket`` returns an ``SSLSocket`` object which has methods typical + for sockets, such as ``send``, ``recv``, etc. MicroPython's ``wrap_socket`` + returns an object more similar to CPython's ``SSLObject`` which does not have + these socket methods. + Exceptions ---------- diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index da5941a55b33e..9d5934206749a 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -167,10 +167,15 @@ STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args o->ssl_sock = ssl_client_new(o->ssl_ctx, (long)sock, NULL, 0, ext); if (args->do_handshake.u_bool) { - int res = ssl_handshake_status(o->ssl_sock); - - if (res != SSL_OK) { - ussl_raise_error(res); + int r = ssl_handshake_status(o->ssl_sock); + + if (r != SSL_OK) { + if (r == SSL_CLOSE_NOTIFY) { // EOF + r = MP_ENOTCONN; + } else if (r == SSL_EAGAIN) { + r = MP_EAGAIN; + } + ussl_raise_error(r); } } @@ -242,8 +247,24 @@ STATIC mp_uint_t ussl_socket_write(mp_obj_t o_in, const void *buf, mp_uint_t siz return MP_STREAM_ERROR; } - mp_int_t r = ssl_write(o->ssl_sock, buf, size); + mp_int_t r; +eagain: + r = ssl_write(o->ssl_sock, buf, size); + if (r == 0) { + // see comment in ussl_socket_read above + if (o->blocking) { + goto eagain; + } else { + r = SSL_EAGAIN; + } + } if (r < 0) { + if (r == SSL_CLOSE_NOTIFY || r == SSL_ERROR_CONN_LOST) { + return 0; // EOF + } + if (r == SSL_EAGAIN) { + r = MP_EAGAIN; + } *errcode = r; return MP_STREAM_ERROR; } diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 1677dc6e1ca70..277af37c7cfd6 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -133,6 +133,7 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) { } } +// _mbedtls_ssl_recv is called by mbedtls to receive bytes from the underlying socket STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) { mp_obj_t sock = *(mp_obj_t *)ctx; @@ -171,7 +172,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { mbedtls_pk_init(&o->pkey); mbedtls_ctr_drbg_init(&o->ctr_drbg); #ifdef MBEDTLS_DEBUG_C - // Debug level (0-4) + // Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose mbedtls_debug_set_threshold(0); #endif diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 61761d819445f..5135e31631e04 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -558,7 +558,8 @@ int _socket_send(socket_obj_t *sock, const char *data, size_t datalen) { MP_THREAD_GIL_EXIT(); int r = lwip_write(sock->fd, data + sentlen, datalen - sentlen); MP_THREAD_GIL_ENTER(); - if (r < 0 && errno != EWOULDBLOCK) { + // lwip returns EINPROGRESS when trying to send right after a non-blocking connect + if (r < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) { mp_raise_OSError(errno); } if (r > 0) { @@ -567,7 +568,7 @@ int _socket_send(socket_obj_t *sock, const char *data, size_t datalen) { check_for_exceptions(); } if (sentlen == 0) { - mp_raise_OSError(MP_ETIMEDOUT); + mp_raise_OSError(sock->retries == 0 ? MP_EWOULDBLOCK : MP_ETIMEDOUT); } return sentlen; } @@ -650,7 +651,8 @@ STATIC mp_uint_t socket_stream_write(mp_obj_t self_in, const void *buf, mp_uint_ if (r > 0) { return r; } - if (r < 0 && errno != EWOULDBLOCK) { + // lwip returns MP_EINPROGRESS when trying to write right after a non-blocking connect + if (r < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) { *errcode = errno; return MP_STREAM_ERROR; } diff --git a/tests/net_hosted/accept_timeout.py b/tests/net_hosted/accept_timeout.py index ff989110ae1b6..5f528d557d8d3 100644 --- a/tests/net_hosted/accept_timeout.py +++ b/tests/net_hosted/accept_timeout.py @@ -1,9 +1,9 @@ # test that socket.accept() on a socket with timeout raises ETIMEDOUT try: - import usocket as socket + import uerrno as errno, usocket as socket except: - import socket + import errno, socket try: socket.socket.settimeout @@ -18,5 +18,5 @@ try: s.accept() except OSError as er: - print(er.args[0] in (110, "timed out")) # 110 is ETIMEDOUT; CPython uses a string + print(er.args[0] in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno s.close() diff --git a/tests/net_hosted/connect_nonblock_xfer.py b/tests/net_hosted/connect_nonblock_xfer.py new file mode 100644 index 0000000000000..feb648ea0aeff --- /dev/null +++ b/tests/net_hosted/connect_nonblock_xfer.py @@ -0,0 +1,147 @@ +# test that socket.connect() on a non-blocking socket raises EINPROGRESS +# and that an immediate write/send/read/recv does the right thing + +try: + import sys, time + import uerrno as errno, usocket as socket, ussl as ssl +except: + import socket, errno, ssl +isMP = sys.implementation.name == "micropython" + + +def dp(e): + # uncomment next line for development and testing, to print the actual exceptions + # print(repr(e)) + pass + + +# do_connect establishes the socket and wraps it if tls is True. +# If handshake is true, the initial connect (and TLS handshake) is +# allowed to be performed before returning. +def do_connect(peer_addr, tls, handshake): + s = socket.socket() + s.setblocking(False) + try: + # print("Connecting to", peer_addr) + s.connect(peer_addr) + except OSError as er: + print("connect:", er.args[0] == errno.EINPROGRESS) + if er.args[0] != errno.EINPROGRESS: + print(" got", er.args[0]) + # wrap with ssl/tls if desired + if tls: + try: + if sys.implementation.name == "micropython": + s = ssl.wrap_socket(s, do_handshake=handshake) + else: + s = ssl.wrap_socket(s, do_handshake_on_connect=handshake) + print("wrap: True") + except Exception as e: + dp(e) + print("wrap:", e) + elif handshake: + # just sleep a little bit, this allows any connect() errors to happen + time.sleep(0.2) + return s + + +# test runs the test against a specific peer address. +def test(peer_addr, tls=False, handshake=False): + # MicroPython plain sockets have read/write, but CPython's don't + # MicroPython TLS sockets and CPython's have read/write + # hasRW captures this wonderful state of affairs + hasRW = isMP or tls + + # MicroPython plain sockets and CPython's have send/recv + # MicroPython TLS sockets don't have send/recv, but CPython's do + # hasSR captures this wonderful state of affairs + hasSR = not (isMP and tls) + + # connect + send + if hasSR: + s = do_connect(peer_addr, tls, handshake) + # send -> 4 or EAGAIN + try: + ret = s.send(b"1234") + print("send:", handshake and ret == 4) + except OSError as er: + # + dp(er) + print("send:", er.args[0] in (errno.EAGAIN, errno.EINPROGRESS)) + s.close() + else: # fake it... + print("connect:", True) + if tls: + print("wrap:", True) + print("send:", True) + + # connect + write + if hasRW: + s = do_connect(peer_addr, tls, handshake) + # write -> None + try: + ret = s.write(b"1234") + print("write:", ret in (4, None)) # SSL may accept 4 into buffer + except OSError as er: + dp(er) + print("write:", False) # should not raise + except ValueError as er: # CPython + dp(er) + print("write:", er.args[0] == "Write on closed or unwrapped SSL socket.") + s.close() + else: # fake it... + print("connect:", True) + if tls: + print("wrap:", True) + print("write:", True) + + if hasSR: + # connect + recv + s = do_connect(peer_addr, tls, handshake) + # recv -> EAGAIN + try: + print("recv:", s.recv(10)) + except OSError as er: + dp(er) + print("recv:", er.args[0] == errno.EAGAIN) + s.close() + else: # fake it... + print("connect:", True) + if tls: + print("wrap:", True) + print("recv:", True) + + # connect + read + if hasRW: + s = do_connect(peer_addr, tls, handshake) + # read -> None + try: + ret = s.read(10) + print("read:", ret is None) + except OSError as er: + dp(er) + print("read:", False) # should not raise + except ValueError as er: # CPython + dp(er) + print("read:", er.args[0] == "Read on closed or unwrapped SSL socket.") + s.close() + else: # fake it... + print("connect:", True) + if tls: + print("wrap:", True) + print("read:", True) + + +if __name__ == "__main__": + # these tests use a non-existent test IP address, this way the connect takes forever and + # we can see EAGAIN/None (https://tools.ietf.org/html/rfc5737) + print("--- Plain sockets to nowhere ---") + test(socket.getaddrinfo("192.0.2.1", 80)[0][-1], False, False) + print("--- SSL sockets to nowhere ---") + # this test fails with AXTLS because do_handshake=False blocks on first read/write and + # there it times out until the connect is aborted + test(socket.getaddrinfo("192.0.2.1", 443)[0][-1], True, False) + print("--- Plain sockets ---") + test(socket.getaddrinfo("micropython.org", 80)[0][-1], False, True) + print("--- SSL sockets ---") + test(socket.getaddrinfo("micropython.org", 443)[0][-1], True, True) diff --git a/tests/net_inet/ssl_errors.py b/tests/net_inet/ssl_errors.py new file mode 100644 index 0000000000000..fd281b1c499c3 --- /dev/null +++ b/tests/net_inet/ssl_errors.py @@ -0,0 +1,51 @@ +# test that socket.connect() on a non-blocking socket raises EINPROGRESS +# and that an immediate write/send/read/recv does the right thing + +import sys + +try: + import uerrno as errno, usocket as socket, ussl as ssl +except: + import errno, socket, ssl + + +def test(addr, hostname, block=True): + print("---", hostname or addr) + s = socket.socket() + s.setblocking(block) + try: + s.connect(addr) + print("connected") + except OSError as e: + if e.args[0] != errno.EINPROGRESS: + raise + print("EINPROGRESS") + + try: + if sys.implementation.name == "micropython": + s = ssl.wrap_socket(s, do_handshake=block) + else: + s = ssl.wrap_socket(s, do_handshake_on_connect=block) + print("wrap: True") + except OSError: + print("wrap: error") + + if not block: + try: + while s.write(b"0") is None: + pass + except (ValueError, OSError): # CPython raises ValueError, MicroPython raises OSError + print("write: error") + s.close() + + +if __name__ == "__main__": + # connect to plain HTTP port, oops! + addr = socket.getaddrinfo("micropython.org", 80)[0][-1] + test(addr, None) + # connect to plain HTTP port, oops! + addr = socket.getaddrinfo("micropython.org", 80)[0][-1] + test(addr, None, False) + # connect to server with self-signed cert, oops! + addr = socket.getaddrinfo("test.mosquitto.org", 8883)[0][-1] + test(addr, "test.mosquitto.org") diff --git a/tests/net_inet/test_tls_nonblock.py b/tests/net_inet/test_tls_nonblock.py new file mode 100644 index 0000000000000..c27ead3d50fb7 --- /dev/null +++ b/tests/net_inet/test_tls_nonblock.py @@ -0,0 +1,116 @@ +try: + import usocket as socket, ussl as ssl, uerrno as errno, sys +except: + import socket, ssl, errno, sys, time, select + + +def test_one(site, opts): + ai = socket.getaddrinfo(site, 443) + addr = ai[0][-1] + print(addr) + + # Connect the raw socket + s = socket.socket() + s.setblocking(False) + try: + s.connect(addr) + raise OSError(-1, "connect blocks") + except OSError as e: + if e.args[0] != errno.EINPROGRESS: + raise + + if sys.implementation.name != "micropython": + # in CPython we have to wait, otherwise wrap_socket is not happy + select.select([], [s], []) + + try: + # Wrap with SSL + try: + if sys.implementation.name == "micropython": + s = ssl.wrap_socket(s, do_handshake=False) + else: + s = ssl.wrap_socket(s, do_handshake_on_connect=False) + except OSError as e: + if e.args[0] != errno.EINPROGRESS: + raise + print("wrapped") + + # CPython needs to be told to do the handshake + if sys.implementation.name != "micropython": + while True: + try: + s.do_handshake() + break + except ssl.SSLError as err: + if err.args[0] == ssl.SSL_ERROR_WANT_READ: + select.select([s], [], []) + elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: + select.select([], [s], []) + else: + raise + time.sleep(0.1) + # print("shook hands") + + # Write HTTP request + out = b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, "latin") + while len(out) > 0: + n = s.write(out) + if n is None: + continue + if n > 0: + out = out[n:] + elif n == 0: + raise OSError(-1, "unexpected EOF in write") + print("wrote") + + # Read response + resp = b"" + while True: + try: + b = s.read(128) + except OSError as err: + if err.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ: + continue + raise + if b is None: + continue + if len(b) > 0: + if len(resp) < 1024: + resp += b + elif len(b) == 0: + break + print("read") + + if resp[:7] != b"HTTP/1.": + raise ValueError("response doesn't start with HTTP/1.") + # print(resp) + + finally: + s.close() + + +SITES = [ + "google.com", + {"host": "www.google.com"}, + "micropython.org", + "pypi.org", + "api.telegram.org", + {"host": "api.pushbullet.com", "sni": True}, +] + + +def main(): + for site in SITES: + opts = {} + if isinstance(site, dict): + opts = site + site = opts["host"] + try: + test_one(site, opts) + print(site, "ok") + except Exception as e: + print(site, "error") + print("DONE") + + +main() diff --git a/tests/net_inet/test_tls_sites.py b/tests/net_inet/test_tls_sites.py index d2cb928c8d5b9..3f945efb83150 100644 --- a/tests/net_inet/test_tls_sites.py +++ b/tests/net_inet/test_tls_sites.py @@ -27,6 +27,8 @@ def test_one(site, opts): s.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, "latin")) resp = s.read(4096) + if resp[:7] != b"HTTP/1.": + raise ValueError("response doesn't start with HTTP/1.") # print(resp) finally: @@ -36,10 +38,10 @@ def test_one(site, opts): SITES = [ "google.com", "www.google.com", + "micropython.org", + "pypi.org", "api.telegram.org", {"host": "api.pushbullet.com", "sni": True}, - # "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", - {"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True}, ] diff --git a/tests/net_inet/test_tls_sites.py.exp b/tests/net_inet/test_tls_sites.py.exp index 2f3c113d2f9ac..bc4a8dbd115db 100644 --- a/tests/net_inet/test_tls_sites.py.exp +++ b/tests/net_inet/test_tls_sites.py.exp @@ -1,5 +1,6 @@ google.com ok www.google.com ok +micropython.org ok +pypi.org ok api.telegram.org ok api.pushbullet.com ok -w9rybpfril.execute-api.ap-southeast-2.amazonaws.com ok From caeec80a9c34f950da41ffe4d5e6f3b4adc680fd Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 12:04:38 +1100 Subject: [PATCH 0635/3533] stm32/usb: Allow a board to configure USBD_VID and all PIDs. If a board defines USBD_VID then that will be used instead of the default. And then the board must also define all USBD_PID_xxx values that it needs. Signed-off-by: Damien George --- ports/stm32/usb.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index f69af86e5b366..295038ebd4289 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -30,6 +30,7 @@ #define PYB_USB_FLAG_USB_MODE_CALLED (0x0002) +#ifndef USBD_VID // Windows needs a different PID to distinguish different device configurations #define USBD_VID (0xf055) #define USBD_PID_CDC_MSC (0x9800) @@ -43,6 +44,7 @@ #define USBD_PID_CDC_MSC_HID (0x9808) #define USBD_PID_CDC2_MSC_HID (0x9809) #define USBD_PID_CDC3_MSC_HID (0x980a) +#endif typedef enum { PYB_USB_STORAGE_MEDIUM_NONE = 0, From 9b78f3e6c6b3eee84dcce2ff75764d8e7d4c713c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 13:03:52 +1100 Subject: [PATCH 0636/3533] stm32: Make pyb, uos, utime, machine and onewire modules configurable. The default for these is to enable them, but they can now be disabled individually by a board configuration. Signed-off-by: Damien George --- ports/stm32/modmachine.c | 4 ++ ports/stm32/mpconfigboard_common.h | 5 +++ ports/stm32/mpconfigport.h | 67 ++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index c5f49b6ef4395..f9fd1d9a64d77 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -406,7 +406,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + #if MICROPY_PY_MACHINE_PULSE { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, @@ -425,8 +427,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif + #if MICROPY_PY_MACHINE_SPI { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index f493bb5d4a644..615310e51b771 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -37,6 +37,11 @@ #define MICROPY_PY_STM (1) #endif +// Whether to include the pyb module +#ifndef MICROPY_PY_PYB +#define MICROPY_PY_PYB (1) +#endif + // Whether to include legacy functions and classes in the pyb module #ifndef MICROPY_PY_PYB_LEGACY #define MICROPY_PY_PYB_LEGACY (1) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index a47529d8b3c01..407a3e6c6a403 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -166,6 +166,11 @@ #ifndef MICROPY_PY_UBINASCII #define MICROPY_PY_UBINASCII (1) #endif +#ifndef MICROPY_PY_UOS +#define MICROPY_PY_UOS (1) +#endif +#define MICROPY_PY_OS_DUPTERM (3) +#define MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM (1) #ifndef MICROPY_PY_URANDOM #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rng_get()) @@ -174,13 +179,15 @@ #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #endif #define MICROPY_PY_USELECT (1) +#ifndef MICROPY_PY_UTIME +#define MICROPY_PY_UTIME (1) +#endif +#define MICROPY_PY_UTIME_MP_HAL (MICROPY_PY_UTIME) #ifndef MICROPY_PY_UTIMEQ #define MICROPY_PY_UTIMEQ (1) #endif -#define MICROPY_PY_UTIME_MP_HAL (1) -#define MICROPY_PY_OS_DUPTERM (3) -#define MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM (1) #define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP) +#ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new @@ -188,6 +195,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) +#endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48) #define MICROPY_PY_UWEBSOCKET (MICROPY_PY_LWIP) @@ -201,6 +209,9 @@ #ifndef MICROPY_PY_NETWORK #define MICROPY_PY_NETWORK (1) #endif +#ifndef MICROPY_PY_ONEWIRE +#define MICROPY_PY_ONEWIRE (1) +#endif // fatfs configuration used in ffconf.h #define MICROPY_FATFS_ENABLE_LFN (1) @@ -246,12 +257,38 @@ extern const struct _mp_obj_module_t mp_module_usocket; extern const struct _mp_obj_module_t mp_module_network; extern const struct _mp_obj_module_t mp_module_onewire; +#if MICROPY_PY_PYB +#define PYB_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, +#else +#define PYB_BUILTIN_MODULE +#endif + #if MICROPY_PY_STM -#define STM_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_stm), MP_ROM_PTR(&stm_module) }, +#define STM_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_stm), MP_ROM_PTR(&stm_module) }, #else #define STM_BUILTIN_MODULE #endif +#if MICROPY_PY_MACHINE +#define MACHINE_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&machine_module) }, +#define MACHINE_BUILTIN_MODULE_CONSTANTS { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, +#else +#define MACHINE_BUILTIN_MODULE +#define MACHINE_BUILTIN_MODULE_CONSTANTS +#endif + +#if MICROPY_PY_UOS +#define UOS_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos) }, +#else +#define UOS_BUILTIN_MODULE +#endif + +#if MICROPY_PY_UTIME +#define UTIME_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, +#else +#define UTIME_BUILTIN_MODULE +#endif + #if MICROPY_PY_USOCKET && MICROPY_PY_LWIP // usocket implementation provided by lwIP #define SOCKET_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_usocket), MP_ROM_PTR(&mp_module_lwip) }, @@ -269,21 +306,27 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define NETWORK_BUILTIN_MODULE #endif +#if MICROPY_PY_ONEWIRE +#define ONEWIRE_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR__onewire), MP_ROM_PTR(&mp_module_onewire) }, +#else +#define ONEWIRE_BUILTIN_MODULE +#endif + #define MICROPY_PORT_BUILTIN_MODULES \ - { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&machine_module) }, \ - { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, \ + MACHINE_BUILTIN_MODULE \ + PYB_BUILTIN_MODULE \ STM_BUILTIN_MODULE \ - { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos) }, \ - { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \ + UOS_BUILTIN_MODULE \ + UTIME_BUILTIN_MODULE \ SOCKET_BUILTIN_MODULE \ NETWORK_BUILTIN_MODULE \ - { MP_ROM_QSTR(MP_QSTR__onewire), MP_ROM_PTR(&mp_module_onewire) }, \ + ONEWIRE_BUILTIN_MODULE \ // extra constants #define MICROPY_PORT_CONSTANTS \ - { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&machine_module) }, \ - { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, \ - { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, \ + MACHINE_BUILTIN_MODULE \ + MACHINE_BUILTIN_MODULE_CONSTANTS \ + PYB_BUILTIN_MODULE \ STM_BUILTIN_MODULE \ #define MP_STATE_PORT MP_STATE_VM From 5c92ff53fed97ca1f5f5b8cd427b49ee79037298 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 13:05:28 +1100 Subject: [PATCH 0637/3533] stm32/boards: Disable onewire module on boards with small flash. Signed-off-by: Damien George --- ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h | 1 + ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h | 1 + ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h | 1 + 3 files changed, 3 insertions(+) diff --git a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h index 531794de66f1b..b330e8fdc1f27 100644 --- a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h +++ b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h @@ -13,6 +13,7 @@ #define MICROPY_PY_FRAMEBUF (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) +#define MICROPY_PY_ONEWIRE (0) #define MICROPY_PY_STM (0) #define MICROPY_PY_PYB_LEGACY (0) #define MICROPY_PY_UHEAPQ (0) diff --git a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h index 2b01bd33a3f1d..a5c5dc9a0ee7b 100644 --- a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h @@ -13,6 +13,7 @@ #define MICROPY_PY_FRAMEBUF (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) +#define MICROPY_PY_ONEWIRE (0) #define MICROPY_PY_STM (0) #define MICROPY_PY_PYB_LEGACY (0) #define MICROPY_PY_UHEAPQ (0) diff --git a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h index 6a3a364dc57a9..14b3bab8a7158 100644 --- a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h @@ -7,6 +7,7 @@ #define MICROPY_PY_GENERATOR_PEND_THROW (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) +#define MICROPY_PY_ONEWIRE (0) #define MICROPY_PY_STM (0) #define MICROPY_PY_PYB_LEGACY (0) #define MICROPY_PY_UHEAPQ (0) From 629fdc366adacf345bbacd83145be2f78823ee57 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 17 Feb 2021 13:45:14 +1100 Subject: [PATCH 0638/3533] stm32/mpbthciport: Fix initial baudrate to use provided value. Fixes bug introduced in the recent bffb71f523e4bcc21b913af291deeb67091bed88 --- ports/stm32/mpbthciport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index 5e51892e6f494..2d34e5e2b79ac 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -196,7 +196,7 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { MP_STATE_PORT(pyb_uart_obj_all)[mp_bluetooth_hci_uart_obj.uart_id - 1] = &mp_bluetooth_hci_uart_obj; // Initialise the UART. - uart_init(&mp_bluetooth_hci_uart_obj, 115200, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); + uart_init(&mp_bluetooth_hci_uart_obj, baudrate, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); uart_set_rxbuf(&mp_bluetooth_hci_uart_obj, sizeof(hci_uart_rxbuf), hci_uart_rxbuf); // Add IRQ handler for IDLE (i.e. packet finished). From 89cb2c6b80ccf302fd7519bd7b3e00680fc717f3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 15:45:06 +1100 Subject: [PATCH 0639/3533] stm32/mpbthciport: Use mp_printf instead of printf for error message. Signed-off-by: Damien George --- ports/stm32/mpbthciport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index 2d34e5e2b79ac..c3cd864e2b7ad 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -232,7 +232,7 @@ int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { int errcode; uart_tx_data(&mp_bluetooth_hci_uart_obj, (void *)buf, len, &errcode); if (errcode != 0) { - printf("\nmp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); + mp_printf(&mp_plat_print, "\nmp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); } return 0; } From 301fe805caaa4ff6a566fb4ef494ef18553dfbfe Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 15:45:54 +1100 Subject: [PATCH 0640/3533] stm32/mpbtstackport: Allow chipset and secondary baudrate to be set. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 1 + ports/stm32/mpbtstackport.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 45d968f353609..dc42751fffe87 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -185,6 +185,7 @@ extern struct _spi_bdev_t spi_bdev2; // Bluetooth config #define MICROPY_HW_BLE_UART_ID (PYB_UART_6) #define MICROPY_HW_BLE_UART_BAUDRATE (115200) +#define MICROPY_HW_BLE_UART_BAUDRATE_SECONDARY (3000000) /******************************************************************************/ // Bootloader configuration diff --git a/ports/stm32/mpbtstackport.c b/ports/stm32/mpbtstackport.c index a031a6a151aff..3dfd8cb3c4a85 100644 --- a/ports/stm32/mpbtstackport.c +++ b/ports/stm32/mpbtstackport.c @@ -58,7 +58,7 @@ uint32_t hal_time_ms(void) { STATIC const hci_transport_config_uart_t hci_transport_config_uart = { HCI_TRANSPORT_CONFIG_UART, MICROPY_HW_BLE_UART_BAUDRATE, - 3000000, + MICROPY_HW_BLE_UART_BAUDRATE_SECONDARY, 0, NULL, }; @@ -90,9 +90,9 @@ void mp_bluetooth_btstack_port_init(void) { const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); hci_init(transport, &hci_transport_config_uart); - // TODO: Probably not necessary for BCM (we have our own firmware loader in the cyw43 driver), - // but might be worth investigating for other controllers in the future. - // hci_set_chipset(btstack_chipset_bcm_instance()); + #ifdef MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE + hci_set_chipset(MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE); + #endif } void mp_bluetooth_btstack_port_deinit(void) { From cf6a0158806b11c521b9c9f8a1104f577a78fe1e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Feb 2021 16:06:56 +1100 Subject: [PATCH 0641/3533] extmod/btstack: Use MICROPY_HW_BLE_UART_BAUDRATE for first UART init. Otherwise the UART may be left in a state at baudrate=0. Signed-off-by: Damien George --- extmod/btstack/btstack_hci_uart.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extmod/btstack/btstack_hci_uart.c b/extmod/btstack/btstack_hci_uart.c index 0a137bbae442d..7205dba069365 100644 --- a/extmod/btstack/btstack_hci_uart.c +++ b/extmod/btstack/btstack_hci_uart.c @@ -67,8 +67,7 @@ STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) { send_handler = NULL; // Set up the UART peripheral, attach IRQ and power up the HCI controller. - // We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate. - if (mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0)) { + if (mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, MICROPY_HW_BLE_UART_BAUDRATE)) { init_success = false; return -1; } From d4b45898f58aae66c706ca5f832f11727daef46b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Feb 2021 13:59:03 +1100 Subject: [PATCH 0642/3533] stm32/mboot: After sig verify, only write firmware-head if latter valid. So that mboot can be used to program encrypted/signed firmware to regions of flash that are not the main application, eg that are the filesystem. Signed-off-by: Damien George --- ports/stm32/mboot/pack.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ports/stm32/mboot/pack.c b/ports/stm32/mboot/pack.c index 63ab93ea30fdb..88529ec50a5e9 100644 --- a/ports/stm32/mboot/pack.c +++ b/ports/stm32/mboot/pack.c @@ -61,10 +61,14 @@ static uint8_t uncompressed_buf[MBOOT_PACK_GZIP_BUFFER_SIZE] __attribute__((alig // that a double-word write to flash can only be done once (due to ECC). static uint8_t firmware_head[8]; +// Flag to indicate that firmware_head contains valid data. +static bool firmware_head_valid; + void mboot_pack_init(void) { erased_base_addr = 0; erased_top_addr = 0; firmware_chunk_base_addr = 0; + firmware_head_valid = false; } // In encrypted mode the erase is automatically managed. @@ -103,6 +107,7 @@ static int mboot_pack_commit_chunk(uint32_t addr, uint8_t *data, size_t len) { addr += sizeof(firmware_head); data += sizeof(firmware_head); len -= sizeof(firmware_head); + firmware_head_valid = true; } // Commit this piece of the firmware. @@ -131,7 +136,7 @@ static int mboot_pack_handle_full_sig(void) { while (len) { uint32_t l = len <= buf_alloc ? len : buf_alloc; hw_read(addr, l, buf); - if (addr == APPLICATION_ADDR) { + if (addr == APPLICATION_ADDR && firmware_head_valid) { // The start of the firmware was not yet written to flash so copy // it out of the temporary buffer to compute the full signature. memcpy(buf, firmware_head, sizeof(firmware_head)); @@ -154,8 +159,13 @@ static int mboot_pack_handle_full_sig(void) { } // Full firmware passed the signature check. - // Write the start of the firmware so it boots. - return hw_write(APPLICATION_ADDR, firmware_head, sizeof(firmware_head)); + + if (firmware_head_valid) { + // Write the start of the firmware so it boots. + ret = hw_write(APPLICATION_ADDR, firmware_head, sizeof(firmware_head)); + } + + return ret; } // Handle a chunk with firmware data. From 466ad35a725742928ef7b12522cc75929083af35 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Feb 2021 11:51:01 +1100 Subject: [PATCH 0643/3533] esp32/boards: Enable size optimisation for builds. This enables -Os for compilation, but still keeps full assertion messages. With IDF v4.2, -Os changes the GENERIC firmware size from 1512176 down to 1384640, and the GENERIC_SPIRAM firmware is now 1452320 which fits in the allocated partition. Signed-off-by: Damien George --- ports/esp32/boards/sdkconfig.base | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index c84dd606b2c29..91e68c7bf6a9c 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -4,6 +4,12 @@ CONFIG_IDF_TARGET="esp32" CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000 +# Compiler options: use -Os to reduce size, but keep full assertions +# (CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is for IDF 4.0.2) +CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE=y +CONFIG_COMPILER_OPTIMIZATION_SIZE=y +CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y + # Application manager CONFIG_APP_EXCLUDE_PROJECT_VER_VAR=y CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR=y From 143372ab5e41a609b5b7d8bb60af1a5fd478e7ea Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 19 Feb 2021 10:38:01 +1100 Subject: [PATCH 0644/3533] esp32: Add support to build with ESP-IDF v4.3 pre-release. The esp32 port now builds against IDF v4.3-beta1, as well as v4.4-dev. Signed-off-by: Damien George --- ports/esp32/main/CMakeLists.txt | 6 ++++++ ports/esp32/modesp32.c | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 52985383f92dd..b6cf214bba64c 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -113,6 +113,12 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 2) list(APPEND IDF_COMPONENTS esp_timer) endif() +if(IDF_VERSION_MINOR GREATER_EQUAL 3) + list(APPEND IDF_COMPONENTS esp_hw_support) + list(APPEND IDF_COMPONENTS esp_pm) + list(APPEND IDF_COMPONENTS hal) +endif() + # Register the main IDF component. idf_component_register( SRCS diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 28d1762d240de..d7c6bf0fa344d 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -35,7 +35,6 @@ #include "driver/adc.h" #include "esp_heap_caps.h" #include "multi_heap.h" -#include "../heap_private.h" #include "py/nlr.h" #include "py/obj.h" @@ -46,6 +45,13 @@ #include "machine_rtc.h" #include "modesp32.h" +// These private includes are needed for idf_heap_info. +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0) +#define MULTI_HEAP_FREERTOS +#include "../multi_heap_platform.h" +#endif +#include "../heap_private.h" + STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) { if (machine_rtc_config.ext0_pin != -1) { From c10d431819f9f7095c3573c98f01c98526a2cb0b Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 18 Jan 2021 01:06:12 -0800 Subject: [PATCH 0645/3533] esp32: Add basic support for Non-Volatile-Storage in esp32 module. This commit implements basic NVS support for the esp32. It follows the pattern of the esp32.Partition class and exposes an NVS object per NVS namespace. The initial support provided is only for signed 32-bit integers and binary blobs. It's easy (albeit a bit tedious) to add support for more types. See discussions in: #4436, #4707, #6780 --- docs/library/esp32.rst | 48 ++++++++++ ports/esp32/esp32_nvs.c | 151 ++++++++++++++++++++++++++++++++ ports/esp32/main/CMakeLists.txt | 1 + ports/esp32/modesp32.c | 1 + ports/esp32/modesp32.h | 1 + tests/esp32/esp32_nvs.py | 67 ++++++++++++++ tests/esp32/esp32_nvs.py.exp | 14 +++ 7 files changed, 283 insertions(+) create mode 100644 ports/esp32/esp32_nvs.c create mode 100644 tests/esp32/esp32_nvs.py create mode 100644 tests/esp32/esp32_nvs.py.exp diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index c6777b8a7da33..f179a31ef65e3 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -269,3 +269,51 @@ Constants esp32.WAKEUP_ANY_HIGH Selects the wake level for pins. + +Non-Volatile Storage +-------------------- + +This class gives access to the Non-Volatile storage managed by ESP-IDF. The NVS is partitioned +into namespaces and each namespace contains typed key-value pairs. The keys are strings and the +values may be various integer types, strings, and binary blobs. The driver currently only +supports 32-bit signed integers and blobs. + +.. warning:: + + Changes to NVS need to be committed to flash by calling the commit method. Failure + to call commit results in changes being lost at the next reset. + +.. class:: NVS(namespace) + + Create an object providing access to a namespace (which is automatically created if not + present). + +.. method:: NVS.set_i32(key, value) + + Sets a 32-bit signed integer value for the specified key. Remember to call *commit*! + +.. method:: NVS.get_i32(key) + + Returns the signed integer value for the specified key. Raises an OSError if the key does not + exist or has a different type. + +.. method:: NVS.set_blob(key, value) + + Sets a binary blob value for the specified key. The value passed in must support the buffer + protocol, e.g. bytes, bytearray, str. (Note that esp-idf distinguishes blobs and strings, this + method always writes a blob even if a string is passed in as value.) + Remember to call *commit*! + +.. method:: NVS.get_blob(key, buffer) + + Reads the value of the blob for the specified key into the buffer, which must be a bytearray. + Returns the actual length read. Raises an OSError if the key does not exist, has a different + type, or if the buffer is too small. + +.. method:: NVS.erase_key(key) + + Erases a key-value pair. + +.. method:: NVS.commit() + + Commits changes made by *set_xxx* methods to flash. diff --git a/ports/esp32/esp32_nvs.c b/ports/esp32/esp32_nvs.c new file mode 100644 index 0000000000000..d13151d3ce774 --- /dev/null +++ b/ports/esp32/esp32_nvs.c @@ -0,0 +1,151 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 by Thorsten von Eicken + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "py/mperrno.h" +#include "mphalport.h" +#include "modesp32.h" +#include "nvs_flash.h" +#include "nvs.h" + +// This file implements the NVS (Non-Volatile Storage) class in the esp32 module. +// It provides simple access to the NVS feature provided by ESP-IDF. + +// NVS python object that represents an NVS namespace. +typedef struct _esp32_nvs_obj_t { + mp_obj_base_t base; + nvs_handle_t namespace; +} esp32_nvs_obj_t; + +// *esp32_nvs_new allocates a python NVS object given a handle to an esp-idf namespace C obj. +STATIC esp32_nvs_obj_t *esp32_nvs_new(nvs_handle_t namespace) { + esp32_nvs_obj_t *self = m_new_obj(esp32_nvs_obj_t); + self->base.type = &esp32_nvs_type; + self->namespace = namespace; + return self; +} + +// esp32_nvs_print prints an NVS object, unfortunately it doesn't seem possible to extract the +// namespace string or anything else from the opaque handle provided by esp-idf. +STATIC void esp32_nvs_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + // esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, ""); +} + +// esp32_nvs_make_new constructs a handle to an NVS namespace. +STATIC mp_obj_t esp32_nvs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // Check args + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // Get requested nvs namespace + const char *ns_name = mp_obj_str_get_str(all_args[0]); + nvs_handle_t namespace; + check_esp_err(nvs_open(ns_name, NVS_READWRITE, &namespace)); + return MP_OBJ_FROM_PTR(esp32_nvs_new(namespace)); +} + +// esp32_nvs_set_i32 sets a 32-bit integer value +STATIC mp_obj_t esp32_nvs_set_i32(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *key = mp_obj_str_get_str(key_in); + int32_t value = mp_obj_get_int(value_in); + check_esp_err(nvs_set_i32(self->namespace, key, value)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_set_i32_obj, esp32_nvs_set_i32); + +// esp32_nvs_get_i32 reads a 32-bit integer value +STATIC mp_obj_t esp32_nvs_get_i32(mp_obj_t self_in, mp_obj_t key_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *key = mp_obj_str_get_str(key_in); + int32_t value; + check_esp_err(nvs_get_i32(self->namespace, key, &value)); + return mp_obj_new_int(value); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_nvs_get_i32_obj, esp32_nvs_get_i32); + +// esp32_nvs_set_blob writes a buffer object into a binary blob value. +STATIC mp_obj_t esp32_nvs_set_blob(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *key = mp_obj_str_get_str(key_in); + mp_buffer_info_t value; + mp_get_buffer_raise(value_in, &value, MP_BUFFER_READ); + check_esp_err(nvs_set_blob(self->namespace, key, value.buf, value.len)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_set_blob_obj, esp32_nvs_set_blob); + +// esp32_nvs_get_blob reads a binary blob value into a bytearray. Returns actual length. +STATIC mp_obj_t esp32_nvs_get_blob(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *key = mp_obj_str_get_str(key_in); + // get buffer to be filled + mp_buffer_info_t value; + mp_get_buffer_raise(value_in, &value, MP_BUFFER_WRITE); + size_t length = value.len; + // fill the buffer with the value, will raise an esp-idf error if the length of + // the provided buffer (bytearray) is too small + check_esp_err(nvs_get_blob(self->namespace, key, value.buf, &length)); + return MP_OBJ_NEW_SMALL_INT(length); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32_nvs_get_blob_obj, esp32_nvs_get_blob); + +// esp32_nvs_erase_key erases one key. +STATIC mp_obj_t esp32_nvs_erase_key(mp_obj_t self_in, mp_obj_t key_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *key = mp_obj_str_get_str(key_in); + check_esp_err(nvs_erase_key(self->namespace, key)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_nvs_erase_key_obj, esp32_nvs_erase_key); + +// esp32_nvs_commit commits any changes to flash. +STATIC mp_obj_t esp32_nvs_commit(mp_obj_t self_in) { + esp32_nvs_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_esp_err(nvs_commit(self->namespace)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_nvs_commit_obj, esp32_nvs_commit); + +STATIC const mp_rom_map_elem_t esp32_nvs_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_get_i32), MP_ROM_PTR(&esp32_nvs_get_i32_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_i32), MP_ROM_PTR(&esp32_nvs_set_i32_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_blob), MP_ROM_PTR(&esp32_nvs_get_blob_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_blob), MP_ROM_PTR(&esp32_nvs_set_blob_obj) }, + { MP_ROM_QSTR(MP_QSTR_erase_key), MP_ROM_PTR(&esp32_nvs_erase_key_obj) }, + { MP_ROM_QSTR(MP_QSTR_commit), MP_ROM_PTR(&esp32_nvs_commit_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(esp32_nvs_locals_dict, esp32_nvs_locals_dict_table); + +const mp_obj_type_t esp32_nvs_type = { + { &mp_type_type }, + .name = MP_QSTR_NVS, + .print = esp32_nvs_print, + .make_new = esp32_nvs_make_new, + .locals_dict = (mp_obj_dict_t *)&esp32_nvs_locals_dict, +}; diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index b6cf214bba64c..9fb48a90412ef 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -55,6 +55,7 @@ set(MICROPY_SOURCE_PORT ${PROJECT_DIR}/mpnimbleport.c ${PROJECT_DIR}/modsocket.c ${PROJECT_DIR}/modesp.c + ${PROJECT_DIR}/esp32_nvs.c ${PROJECT_DIR}/esp32_partition.c ${PROJECT_DIR}/esp32_rmt.c ${PROJECT_DIR}/esp32_ulp.c diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index d7c6bf0fa344d..53ca7fdc60fcc 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -186,6 +186,7 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_hall_sensor), MP_ROM_PTR(&esp32_hall_sensor_obj) }, { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, + { MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) }, { MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) }, { MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) }, { MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) }, diff --git a/ports/esp32/modesp32.h b/ports/esp32/modesp32.h index f4c0491f7c27f..18bd62ee41640 100644 --- a/ports/esp32/modesp32.h +++ b/ports/esp32/modesp32.h @@ -26,6 +26,7 @@ #define RTC_LAST_EXT_PIN 39 #define RTC_IS_VALID_EXT_PIN(pin_id) ((1ll << (pin_id)) & RTC_VALID_EXT_PINS) +extern const mp_obj_type_t esp32_nvs_type; extern const mp_obj_type_t esp32_partition_type; extern const mp_obj_type_t esp32_rmt_type; extern const mp_obj_type_t esp32_ulp_type; diff --git a/tests/esp32/esp32_nvs.py b/tests/esp32/esp32_nvs.py new file mode 100644 index 0000000000000..fd8b152ca71d8 --- /dev/null +++ b/tests/esp32/esp32_nvs.py @@ -0,0 +1,67 @@ +# Test the esp32 NVS class - access to esp-idf's Non-Volatile-Storage + +from esp32 import NVS + +nvs = NVS("mp-test") + +# test setting and gettin an integer kv +nvs.set_i32("key1", 1234) +print(nvs.get_i32("key1")) +nvs.set_i32("key2", -503) +print(nvs.get_i32("key2")) +print(nvs.get_i32("key1")) + +# test setting and getting a blob kv using a bytearray +blob1 = "testing a string as a blob" +nvs.set_blob("blob1", blob1) +buf1 = bytearray(len(blob1)) +len1 = nvs.get_blob("blob1", buf1) +print(buf1) +print(len(blob1), len1) + +# test setting and getting a blob kv using a string +blob2 = b"testing a bytearray" +nvs.set_blob("blob2", blob2) +buf2 = bytearray(len(blob2)) +len2 = nvs.get_blob("blob2", buf2) +print(buf2) +print(len(blob2), len2) + +# test raising of error exceptions +nvs.erase_key("key1") +try: + nvs.erase_key("key1") # not found +except OSError as e: + print(e) +try: + nvs.get_i32("key1") # not found +except OSError as e: + print(e) +try: + nvs.get_i32("blob1") # not found (blob1 exists but diff type) +except OSError as e: + print(e) +try: + buf3 = bytearray(10) + nvs.get_blob("blob1", buf3) # invalid length (too short) +except OSError as e: + print(e) + +nvs.commit() # we're not verifying that this does anything, just doesn't error + +# test using a second namespace and that it doesn't interfere with first +nvs2 = NVS("mp-test2") +try: + print(nvs2.get_i32("key2")) +except OSError as e: + print(e) +nvs2.set_i32("key2", 7654) +print(nvs.get_i32("key2")) +print(nvs2.get_i32("key2")) + +# clean-up (the namespaces will remain) +nvs.erase_key("key2") +nvs.erase_key("blob1") +nvs.erase_key("blob2") +nvs2.erase_key("key2") +nvs.commit() diff --git a/tests/esp32/esp32_nvs.py.exp b/tests/esp32/esp32_nvs.py.exp new file mode 100644 index 0000000000000..33cdfd6df9c09 --- /dev/null +++ b/tests/esp32/esp32_nvs.py.exp @@ -0,0 +1,14 @@ +1234 +-503 +1234 +bytearray(b'testing a string as a blob') +26 26 +bytearray(b'testing a bytearray') +19 19 +(-4354, 'ESP_ERR_NVS_NOT_FOUND') +(-4354, 'ESP_ERR_NVS_NOT_FOUND') +(-4354, 'ESP_ERR_NVS_NOT_FOUND') +(-4364, 'ESP_ERR_NVS_INVALID_LENGTH') +(-4354, 'ESP_ERR_NVS_NOT_FOUND') +-503 +7654 From d28dbcd6c783bc45bc4661abbb53d2b6c5397102 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 3 Jun 2020 22:42:37 -0700 Subject: [PATCH 0646/3533] esp32: Make machine.soft_reset() work in main.py and reset_cause(). This commit fixes two issues on the esp32: - it enables machine.soft_reset() to be called in main.py; - it enables machine.reset_cause() to correctly identify a soft reset. The former is useful in that it enables soft resets in applications that are started at boot time. The support is patterned after the stm32 port. --- ports/esp32/main.c | 9 ++++++++- ports/esp32/modmachine.c | 14 ++++++++++++++ ports/esp32/modmachine.h | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 001f2beaeb5e9..7413798d0c302 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -73,6 +73,7 @@ void mp_task(void *pvParameter) { mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif uart_init(); + machine_init(); // TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0. #if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_SPIRAM_SUPPORT @@ -118,7 +119,10 @@ void mp_task(void *pvParameter) { pyexec_frozen_module("_boot.py"); pyexec_file_if_exists("boot.py"); if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { - pyexec_file_if_exists("main.py"); + int ret = pyexec_file_if_exists("main.py"); + if (ret & PYEXEC_FORCED_EXIT) { + goto soft_reset_exit; + } } for (;;) { @@ -135,6 +139,8 @@ void mp_task(void *pvParameter) { } } +soft_reset_exit: + #if MICROPY_BLUETOOTH_NIMBLE mp_bluetooth_deinit(); #endif @@ -151,6 +157,7 @@ void mp_task(void *pvParameter) { // deinitialise peripherals machine_pins_deinit(); + machine_deinit(); usocket_events_deinit(); mp_deinit(); diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index bb346ea1cd3fb..3925bcb64e194 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -59,6 +59,8 @@ typedef enum { MP_SOFT_RESET } reset_reason_t; +STATIC bool is_soft_reset = 0; + STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get @@ -140,6 +142,9 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_deepsleep_obj, 0, machine_deepsleep); STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + if (is_soft_reset) { + return MP_OBJ_NEW_SMALL_INT(MP_SOFT_RESET); + } switch (esp_reset_reason()) { case ESP_RST_POWERON: case ESP_RST_BROWNOUT: @@ -171,6 +176,15 @@ STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_ } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_reset_cause_obj, 0, machine_reset_cause); +void machine_init(void) { + is_soft_reset = 0; +} + +void machine_deinit(void) { + // we are doing a soft-reset so change the reset_cause + is_soft_reset = 1; +} + STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { return MP_OBJ_NEW_SMALL_INT(esp_sleep_get_wakeup_cause()); } diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h index 98b36e32b0231..3e99e11205afc 100644 --- a/ports/esp32/modmachine.h +++ b/ports/esp32/modmachine.h @@ -22,6 +22,8 @@ extern const mp_obj_type_t machine_uart_type; extern const mp_obj_type_t machine_rtc_type; extern const mp_obj_type_t machine_sdcard_type; +void machine_init(void); +void machine_deinit(void); void machine_pins_init(void); void machine_pins_deinit(void); void machine_timer_deinit_all(void); From a76604afba109d990e466cdcd5a69a82077a7f56 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 19 Feb 2021 14:47:51 +1100 Subject: [PATCH 0647/3533] extmod/modbluetooth: Separate enabling of "client" from "central". Previously, the MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE macro controlled enabling both the central mode and the GATT client functionality (because usually the two go together). This commits adds a new MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT macro that separately enables the GATT client functionality. This defaults to MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE. This also fixes a bug in the NimBLE bindings where a notification or indication would not be received by a peripheral (acting as client) as gap_event_cb wasn't handling it. Now both central_gap_event_cb and peripheral_gap_event_cb share the same common handler for these events. Signed-off-by: Jim Mussared --- extmod/btstack/modbluetooth_btstack.c | 30 ++- extmod/modbluetooth.c | 32 ++- extmod/modbluetooth.h | 24 +- extmod/nimble/modbluetooth_nimble.c | 336 +++++++++++++++----------- 4 files changed, 246 insertions(+), 176 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index c861c76d22b0b..5f047e32541f4 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -91,7 +91,7 @@ STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uu } return result; } -#endif +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Notes on supporting background ops (e.g. an attempt to gatts_notify while // an existing notification is in progress): @@ -218,7 +218,7 @@ STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_ty return pending_op; } -#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Cleans up a pending op of the specified type for this conn_handle (and if specified, value_handle). // Used by MP_BLUETOOTH_BTSTACK_PENDING_WRITE and MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE. @@ -418,6 +418,8 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t uint8_t length = gap_event_advertising_report_get_data_length(packet); const uint8_t *data = gap_event_advertising_report_get_data(packet); mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length); + #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet); @@ -487,7 +489,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t // Note: Can't "del" the pending_op from IRQ context. Leave it for the GC. } - #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT } else { DEBUG_printf(" --> hci event type: unknown (0x%02x)\n", event_type); } @@ -506,7 +508,7 @@ STATIC btstack_packet_callback_registration_t hci_event_callback_registration = .callback = &btstack_packet_handler_generic }; -#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // For when the handler is being used for service discovery. STATIC void btstack_packet_handler_discover_services(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { (void)channel; @@ -541,7 +543,7 @@ STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint (void)size; btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE); } -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT STATIC btstack_timer_source_t btstack_init_deinit_timeout; @@ -662,12 +664,12 @@ int mp_bluetooth_init(void) { sm_set_er(dummy_key); sm_set_ir(dummy_key); - #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT gatt_client_init(); // We always require explicitly exchanging MTU with ble.gattc_exchange_mtu(). gatt_client_mtu_enable_auto_negotiation(false); - #endif + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Register for HCI events. hci_add_event_handler(&hci_event_callback_registration); @@ -719,10 +721,10 @@ int mp_bluetooth_init(void) { set_random_address(); } - #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles. gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler_generic, GATT_CLIENT_ANY_CONNECTION, NULL); - #endif + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT return 0; } @@ -737,10 +739,10 @@ void mp_bluetooth_deinit(void) { mp_bluetooth_gap_advertise_stop(); - #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Remove our registration for notify/indicate. gatt_client_stop_listening_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification); - #endif + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Set a timer that will forcibly set the state to TIMEOUT, which will stop the loop below. btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS); @@ -1232,6 +1234,10 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, return btstack_error_to_errno(gap_connect(btstack_addr, addr_type)); } +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) { DEBUG_printf("mp_bluetooth_gattc_discover_primary_services\n"); uint8_t err; @@ -1346,7 +1352,7 @@ int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { return 0; } -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 8870a21c4d520..269493f0a94a3 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -761,7 +761,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_set_buffer_obj, 3 // Bluetooth object: GATTC (Central/Scanner role) // ---------------------------------------------------------------------------- -#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT STATIC mp_obj_t bluetooth_ble_gattc_discover_services(size_t n_args, const mp_obj_t *args) { mp_int_t conn_handle = mp_obj_get_int(args[1]); @@ -830,7 +830,7 @@ STATIC mp_obj_t bluetooth_ble_gattc_exchange_mtu(mp_obj_t self_in, mp_obj_t conn } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_exchange_mtu_obj, bluetooth_ble_gattc_exchange_mtu); -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS @@ -921,15 +921,15 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) }, { MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) }, #endif - // GATT Server (i.e. peripheral/advertiser role) + // GATT Server { MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_write), MP_ROM_PTR(&bluetooth_ble_gatts_write_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) }, - #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE - // GATT Client (i.e. central/scanner role) + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + // GATT Client { MP_ROM_QSTR(MP_QSTR_gattc_discover_services), MP_ROM_PTR(&bluetooth_ble_gattc_discover_services_obj) }, { MP_ROM_QSTR(MP_QSTR_gattc_discover_characteristics), MP_ROM_PTR(&bluetooth_ble_gattc_discover_characteristics_obj) }, { MP_ROM_QSTR(MP_QSTR_gattc_discover_descriptors), MP_ROM_PTR(&bluetooth_ble_gattc_discover_descriptors_obj) }, @@ -1067,6 +1067,8 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_SCAN_DONE) { // No params required. data_tuple->len = 0; + #endif + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT } else if (event == MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT) { // conn_handle, start_handle, end_handle, uuid ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, &o->irq_data_uuid, NULL); @@ -1085,7 +1087,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) { // conn_handle, value_handle, status ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL); - #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT } MICROPY_PY_BLUETOOTH_EXIT @@ -1228,7 +1230,7 @@ void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) { } #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS -mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { +mp_int_t mp_bluetooth_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { mp_int_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_ACCEPT, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); // Return non-zero from IRQ handler to fail the accept. @@ -1237,22 +1239,22 @@ mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, return ret; } -void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { +void mp_bluetooth_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu) { mp_int_t args[] = {conn_handle, cid, psm, our_mtu, peer_mtu}; invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_CONNECT, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status) { +void mp_bluetooth_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status) { mp_int_t args[] = {conn_handle, cid, psm, status}; invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT, args, 4, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status) { +void mp_bluetooth_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status) { mp_int_t args[] = {conn_handle, cid, status}; invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_SEND_READY, args, 3, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid) { +void mp_bluetooth_on_l2cap_recv(uint16_t conn_handle, uint16_t cid) { mp_int_t args[] = {conn_handle, cid}; invoke_irq_handler(MP_BLUETOOTH_IRQ_L2CAP_RECV, args, 2, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } @@ -1267,7 +1269,9 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uin mp_int_t args[] = {addr_type, adv_type, rssi}; invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, args, 1, 2, addr, NULL_UUID, &data, &data_len, 1); } +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid) { mp_int_t args[] = {conn_handle, start_handle, end_handle}; invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, 0, NULL_ADDR, service_uuid, NULL_DATA, NULL_DATA_LEN, 0); @@ -1325,7 +1329,7 @@ void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle invoke_irq_handler(event, args, 3, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #else // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // Callbacks are called in interrupt context (i.e. can't allocate), so we need to push the data @@ -1471,7 +1475,9 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uin } schedule_ringbuf(atomic_state); } +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); @@ -1559,7 +1565,7 @@ void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle } schedule_ringbuf(atomic_state); } -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #endif // MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index c96427fcb4a1e..d126ad6c11344 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -43,6 +43,12 @@ #define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (0) #endif +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT +// Enable the client by default if we're enabling central mode. It's possible +// to enable client without central though. +#define MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT (MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE) +#endif + #ifndef MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS // This can be enabled if the BLE stack runs entirely in scheduler context // and therefore is able to call directly into the VM to run Python callbacks. @@ -365,7 +371,9 @@ int mp_bluetooth_gap_scan_stop(void); // Connect to a found peripheral. int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms); +#endif +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Find all primary services on the connected peripheral. int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid); @@ -383,7 +391,7 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const // Initiate MTU exchange for a specific connection using the preferred MTU. int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle); -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS int mp_bluetooth_l2cap_listen(uint16_t psm, uint16_t mtu); @@ -440,7 +448,9 @@ void mp_bluetooth_gap_on_scan_complete(void); // Notify modbluetooth of a scan result. void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len); +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // Notify modbluetooth that a service was found (either by discover-all, or discover-by-uuid). void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid); @@ -458,14 +468,14 @@ void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, u // Notify modbluetooth that a read or write operation has completed. void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status); -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS -mp_int_t mp_bluetooth_gattc_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); -void mp_bluetooth_gattc_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); -void mp_bluetooth_gattc_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status); -void mp_bluetooth_gattc_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status); -void mp_bluetooth_gattc_on_l2cap_recv(uint16_t conn_handle, uint16_t cid); +mp_int_t mp_bluetooth_on_l2cap_accept(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); +void mp_bluetooth_on_l2cap_connect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t our_mtu, uint16_t peer_mtu); +void mp_bluetooth_on_l2cap_disconnect(uint16_t conn_handle, uint16_t cid, uint16_t psm, uint16_t status); +void mp_bluetooth_on_l2cap_send_ready(uint16_t conn_handle, uint16_t cid, uint8_t status); +void mp_bluetooth_on_l2cap_recv(uint16_t conn_handle, uint16_t cid); #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS // For stacks that don't manage attribute value data (currently all of them), helpers diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index a0a3998c6614d..e3a2f872e5ecb 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -80,6 +80,69 @@ STATIC int8_t ble_hs_err_to_errno_table[] = { [BLE_HS_EBADDATA] = MP_EINVAL, }; +STATIC int ble_hs_err_to_errno(int err); + +STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid, ble_uuid_any_t *storage); +STATIC void reverse_addr_byte_order(uint8_t *addr_out, const uint8_t *addr_in); + +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(const ble_uuid_any_t *uuid); +STATIC ble_addr_t create_nimble_addr(uint8_t addr_type, const uint8_t *addr); +#endif + +STATIC void reset_cb(int reason); + +STATIC bool has_public_address(void); +STATIC void set_random_address(bool nrpa); + +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +STATIC int load_irk(void); +#endif + +STATIC void sync_cb(void); + +#if !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY +STATIC void ble_hs_shutdown_stop_cb(int status, void *arg); +#endif + +// Successfully registered service/char/desc handles. +STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); + +// Events about a connected central (we're in peripheral role). +STATIC int central_gap_event_cb(struct ble_gap_event *event, void *arg); +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +// Events about a connected peripheral (we're in central role). +STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg); +#endif +// Used by both of the above. +STATIC int commmon_gap_event_cb(struct ble_gap_event *event, void *arg); + +#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +// Scan results. +STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg); +#endif + +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT +// Data available (either due to notify/indicate or successful read). +STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om); + +// Client discovery callbacks. +STATIC int ble_gattc_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg); +STATIC int ble_gattc_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg); +STATIC int ble_gattc_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg); + +// Client read/write handlers. +STATIC int ble_gattc_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg); +STATIC int ble_gattc_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg); +#endif + +#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING +// Bonding store. +STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, union ble_store_value *value); +STATIC int ble_store_ram_write(int obj_type, const union ble_store_value *val); +STATIC int ble_store_ram_delete(int obj_type, const union ble_store_key *key); +#endif + STATIC int ble_hs_err_to_errno(int err) { DEBUG_printf("ble_hs_err_to_errno: %d\n", err); if (!err) { @@ -334,8 +397,54 @@ STATIC void gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { } } -STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { - DEBUG_printf("gap_event_cb: type=%d\n", event->type); +STATIC int commmon_gap_event_cb(struct ble_gap_event *event, void *arg) { + struct ble_gap_conn_desc desc; + + switch (event->type) { + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + case BLE_GAP_EVENT_NOTIFY_RX: { + uint16_t ev = event->notify_rx.indication == 0 ? MP_BLUETOOTH_IRQ_GATTC_NOTIFY : MP_BLUETOOTH_IRQ_GATTC_INDICATE; + gattc_on_data_available(ev, event->notify_rx.conn_handle, event->notify_rx.attr_handle, event->notify_rx.om); + return 0; + } + #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + + case BLE_GAP_EVENT_CONN_UPDATE: { + DEBUG_printf("commmon_gap_event_cb: connection update: status=%d\n", event->conn_update.status); + if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { + mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); + } + return 0; + } + + case BLE_GAP_EVENT_MTU: { + if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { + DEBUG_printf("commmon_gap_event_cb: mtu update: conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); + mp_bluetooth_gatts_on_mtu_exchanged(event->mtu.conn_handle, event->mtu.value); + } + return 0; + } + + case BLE_GAP_EVENT_ENC_CHANGE: { + DEBUG_printf("commmon_gap_event_cb: enc change: status=%d\n", event->enc_change.status); + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { + mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, + desc.sec_state.encrypted, desc.sec_state.authenticated, + desc.sec_state.bonded, desc.sec_state.key_size); + } + #endif + return 0; + } + + default: + DEBUG_printf("commmon_gap_event_cb: unknown type %d\n", event->type); + return 0; + } +} + +STATIC int central_gap_event_cb(struct ble_gap_event *event, void *arg) { + DEBUG_printf("central_gap_event_cb: type=%d\n", event->type); if (!mp_bluetooth_is_active()) { return 0; } @@ -344,7 +453,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { switch (event->type) { case BLE_GAP_EVENT_CONNECT: - DEBUG_printf("gap_event_cb: connect: status=%d\n", event->connect.status); + DEBUG_printf("central_gap_event_cb: connect: status=%d\n", event->connect.status); if (event->connect.status == 0) { // Connection established. ble_gap_conn_find(event->connect.conn_handle, &desc); @@ -354,60 +463,32 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { // Connection failed. mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT, event->connect.conn_handle, 0xff, addr); } - break; + return 0; case BLE_GAP_EVENT_DISCONNECT: // Disconnect. - DEBUG_printf("gap_event_cb: disconnect: reason=%d\n", event->disconnect.reason); + DEBUG_printf("central_gap_event_cb: disconnect: reason=%d\n", event->disconnect.reason); reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val); mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr); - break; + return 0; case BLE_GAP_EVENT_NOTIFY_TX: { - DEBUG_printf("gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status); + DEBUG_printf("central_gap_event_cb: notify_tx: %d %d\n", event->notify_tx.indication, event->notify_tx.status); // This event corresponds to either a sent notify/indicate (status == 0), or an indication confirmation (status != 0). if (event->notify_tx.indication && event->notify_tx.status != 0) { // Map "done/ack" to 0, otherwise pass the status directly. mp_bluetooth_gatts_on_indicate_complete(event->notify_tx.conn_handle, event->notify_tx.attr_handle, event->notify_tx.status == BLE_HS_EDONE ? 0 : event->notify_tx.status); } - break; - } - - case BLE_GAP_EVENT_MTU: { - if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { - DEBUG_printf("gap_event_cb: mtu update: conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); - mp_bluetooth_gatts_on_mtu_exchanged(event->mtu.conn_handle, event->mtu.value); - } - break; + return 0; } case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: - DEBUG_printf("gap_event_cb: phy update: %d\n", event->phy_updated.tx_phy); - break; - - case BLE_GAP_EVENT_CONN_UPDATE: { - DEBUG_printf("gap_event_cb: connection update: status=%d\n", event->conn_update.status); - if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { - mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); - } - break; - } - - case BLE_GAP_EVENT_ENC_CHANGE: { - DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status); - #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING - if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { - mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, - desc.sec_state.encrypted, desc.sec_state.authenticated, - desc.sec_state.bonded, desc.sec_state.key_size); - } - #endif - break; - } + DEBUG_printf("central_gap_event_cb: phy update: %d\n", event->phy_updated.tx_phy); + return 0; case BLE_GAP_EVENT_REPEAT_PAIRING: { // We recognized this peer but the peer doesn't recognize us. - DEBUG_printf("gap_event_cb: repeat pairing: conn_handle=%d\n", event->repeat_pairing.conn_handle); + DEBUG_printf("central_gap_event_cb: repeat pairing: conn_handle=%d\n", event->repeat_pairing.conn_handle); // TODO: Consider returning BLE_GAP_REPEAT_PAIRING_IGNORE (and // possibly an API to configure this). @@ -423,7 +504,7 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { } case BLE_GAP_EVENT_PASSKEY_ACTION: { - DEBUG_printf("gap_event_cb: passkey action: conn_handle=%d action=%d num=" UINT_FMT "\n", event->passkey.conn_handle, event->passkey.params.action, (mp_uint_t)event->passkey.params.numcmp); + DEBUG_printf("central_gap_event_cb: passkey action: conn_handle=%d action=%d num=" UINT_FMT "\n", event->passkey.conn_handle, event->passkey.params.action, (mp_uint_t)event->passkey.params.numcmp); #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING mp_bluetooth_gap_on_passkey_action(event->passkey.conn_handle, event->passkey.params.action, event->passkey.params.numcmp); @@ -431,12 +512,9 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { return 0; } - - default: - DEBUG_printf("gap_event_cb: unknown type %d\n", event->type); - break; } - return 0; + + return commmon_gap_event_cb(event, arg); } #if !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY @@ -738,7 +816,7 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons .channel_map = 7, // all 3 channels. }; - ret = ble_gap_adv_start(nimble_address_mode, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); + ret = ble_gap_adv_start(nimble_address_mode, NULL, BLE_HS_FOREVER, &adv_params, central_gap_event_cb, NULL); if (ret == 0) { return 0; } @@ -1025,38 +1103,6 @@ int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t pass #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE -STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { - // When the HCI data for an ATT payload arrives, the L2CAP channel will - // buffer it into its receive buffer. We set BLE_L2CAP_JOIN_RX_FRAGS=1 in - // syscfg.h so it should be rare that the mbuf is fragmented, but we do need - // to be able to handle it. We pass all the fragments up to modbluetooth.c - // which will create a temporary buffer on the MicroPython heap if necessary - // to re-assemble them. - - // Count how many links are in the mbuf chain. - size_t n = 0; - const struct os_mbuf *elem = om; - while (elem) { - n += 1; - elem = SLIST_NEXT(elem, om_next); - } - - // Grab data pointers and lengths for each of the links. - const uint8_t **data = mp_local_alloc(sizeof(uint8_t *) * n); - uint16_t *data_len = mp_local_alloc(sizeof(uint16_t) * n); - for (size_t i = 0; i < n; ++i) { - data[i] = OS_MBUF_DATA(om, const uint8_t *); - data_len[i] = om->om_len; - om = SLIST_NEXT(om, om_next); - } - - // Pass all the fragments together. - mp_bluetooth_gattc_on_data_available(event, conn_handle, value_handle, data, data_len, n); - - mp_local_free(data_len); - mp_local_free(data); -} - STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) { DEBUG_printf("gap_scan_cb: event=%d type=%d\n", event->type, event->type == BLE_GAP_EVENT_DISC ? event->disc.event_type : -1); if (!mp_bluetooth_is_active()) { @@ -1135,56 +1181,17 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { // Connection failed. mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT, event->connect.conn_handle, 0xff, addr); } - break; + return 0; case BLE_GAP_EVENT_DISCONNECT: // Disconnect. DEBUG_printf("peripheral_gap_event_cb: reason=%d\n", event->disconnect.reason); reverse_addr_byte_order(addr, event->disconnect.conn.peer_id_addr.val); mp_bluetooth_gap_on_connected_disconnected(MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT, event->disconnect.conn.conn_handle, event->disconnect.conn.peer_id_addr.type, addr); - - break; - - case BLE_GAP_EVENT_NOTIFY_RX: { - uint16_t ev = event->notify_rx.indication == 0 ? MP_BLUETOOTH_IRQ_GATTC_NOTIFY : MP_BLUETOOTH_IRQ_GATTC_INDICATE; - gattc_on_data_available(ev, event->notify_rx.conn_handle, event->notify_rx.attr_handle, event->notify_rx.om); - break; - } - - case BLE_GAP_EVENT_CONN_UPDATE: { - DEBUG_printf("peripheral_gap_event_cb: connection update: status=%d\n", event->conn_update.status); - if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { - mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); - } - break; - } - - case BLE_GAP_EVENT_MTU: { - if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) { - DEBUG_printf("peripheral_gap_event_cb: mtu update: conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); - mp_bluetooth_gatts_on_mtu_exchanged(event->mtu.conn_handle, event->mtu.value); - } - break; - } - - case BLE_GAP_EVENT_ENC_CHANGE: { - DEBUG_printf("peripheral_gap_event_cb: enc change: status=%d\n", event->enc_change.status); - #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING - if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) { - mp_bluetooth_gatts_on_encryption_update(event->conn_update.conn_handle, - desc.sec_state.encrypted, desc.sec_state.authenticated, - desc.sec_state.bonded, desc.sec_state.key_size); - } - #endif - break; - } - - default: - DEBUG_printf("peripheral_gap_event_cb: unknown type %d\n", event->type); - break; + return 0; } - return 0; + return commmon_gap_event_cb(event, arg); } int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) { @@ -1213,8 +1220,8 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, return ble_hs_err_to_errno(err); } -STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) { - DEBUG_printf("peripheral_discover_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1); +STATIC int ble_gattc_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) { + DEBUG_printf("ble_gattc_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -1227,6 +1234,42 @@ STATIC int peripheral_discover_service_cb(uint16_t conn_handle, const struct ble return 0; } +#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE + +#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + +STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) { + // When the HCI data for an ATT payload arrives, the L2CAP channel will + // buffer it into its receive buffer. We set BLE_L2CAP_JOIN_RX_FRAGS=1 in + // syscfg.h so it should be rare that the mbuf is fragmented, but we do need + // to be able to handle it. We pass all the fragments up to modbluetooth.c + // which will create a temporary buffer on the MicroPython heap if necessary + // to re-assemble them. + + // Count how many links are in the mbuf chain. + size_t n = 0; + const struct os_mbuf *elem = om; + while (elem) { + n += 1; + elem = SLIST_NEXT(elem, om_next); + } + + // Grab data pointers and lengths for each of the links. + const uint8_t **data = mp_local_alloc(sizeof(uint8_t *) * n); + uint16_t *data_len = mp_local_alloc(sizeof(uint16_t) * n); + for (size_t i = 0; i < n; ++i) { + data[i] = OS_MBUF_DATA(om, const uint8_t *); + data_len[i] = om->om_len; + om = SLIST_NEXT(om, om_next); + } + + // Pass all the fragments together. + mp_bluetooth_gattc_on_data_available(event, conn_handle, value_handle, data, data_len, n); + + mp_local_free(data_len); + mp_local_free(data); +} + int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; @@ -1235,15 +1278,15 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_ if (uuid) { ble_uuid_any_t nimble_uuid; create_nimble_uuid(uuid, &nimble_uuid); - err = ble_gattc_disc_svc_by_uuid(conn_handle, &nimble_uuid.u, &peripheral_discover_service_cb, NULL); + err = ble_gattc_disc_svc_by_uuid(conn_handle, &nimble_uuid.u, &ble_gattc_service_cb, NULL); } else { - err = ble_gattc_disc_all_svcs(conn_handle, &peripheral_discover_service_cb, NULL); + err = ble_gattc_disc_all_svcs(conn_handle, &ble_gattc_service_cb, NULL); } return ble_hs_err_to_errno(err); } -STATIC int ble_gatt_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) { - DEBUG_printf("ble_gatt_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1); +STATIC int ble_gattc_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) { + DEBUG_printf("ble_gattc_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -1264,15 +1307,15 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s if (uuid) { ble_uuid_any_t nimble_uuid; create_nimble_uuid(uuid, &nimble_uuid); - err = ble_gattc_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, &nimble_uuid.u, &ble_gatt_characteristic_cb, NULL); + err = ble_gattc_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, &nimble_uuid.u, &ble_gattc_characteristic_cb, NULL); } else { - err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gatt_characteristic_cb, NULL); + err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gattc_characteristic_cb, NULL); } return ble_hs_err_to_errno(err); } -STATIC int ble_gatt_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg) { - DEBUG_printf("ble_gatt_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1); +STATIC int ble_gattc_descriptor_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t characteristic_val_handle, const struct ble_gatt_dsc *descriptor, void *arg) { + DEBUG_printf("ble_gattc_descriptor_cb: conn_handle=%d status=%d chr_handle=%d dsc_handle=%d\n", conn_handle, error->status, characteristic_val_handle, descriptor ? descriptor->handle : -1); if (!mp_bluetooth_is_active()) { return 0; } @@ -1289,13 +1332,13 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - int err = ble_gattc_disc_all_dscs(conn_handle, start_handle, end_handle, &ble_gatt_descriptor_cb, NULL); + int err = ble_gattc_disc_all_dscs(conn_handle, start_handle, end_handle, &ble_gattc_descriptor_cb, NULL); return ble_hs_err_to_errno(err); } -STATIC int ble_gatt_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { +STATIC int ble_gattc_attr_read_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { uint16_t handle = attr ? attr->handle : (error ? error->att_handle : 0xffff); - DEBUG_printf("ble_gatt_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); + DEBUG_printf("ble_gattc_attr_read_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); if (!mp_bluetooth_is_active()) { return 0; } @@ -1311,13 +1354,13 @@ int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - int err = ble_gattc_read(conn_handle, value_handle, &ble_gatt_attr_read_cb, NULL); + int err = ble_gattc_read(conn_handle, value_handle, &ble_gattc_attr_read_cb, NULL); return ble_hs_err_to_errno(err); } -STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { +STATIC int ble_gattc_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { uint16_t handle = attr ? attr->handle : (error ? error->att_handle : 0xffff); - DEBUG_printf("ble_gatt_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); + DEBUG_printf("ble_gattc_attr_write_cb: conn_handle=%d status=%d handle=%d\n", conn_handle, error->status, handle); if (!mp_bluetooth_is_active()) { return 0; } @@ -1334,7 +1377,7 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const if (mode == MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE) { err = ble_gattc_write_no_rsp_flat(conn_handle, value_handle, value, *value_len); } else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) { - err = ble_gattc_write_flat(conn_handle, value_handle, value, *value_len, &ble_gatt_attr_write_cb, NULL); + err = ble_gattc_write_flat(conn_handle, value_handle, value, *value_len, &ble_gattc_attr_write_cb, NULL); } else { err = BLE_HS_EINVAL; } @@ -1348,7 +1391,7 @@ int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) { return ble_hs_err_to_errno(ble_gattc_exchange_mtu(conn_handle, NULL, NULL)); } -#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS @@ -1375,6 +1418,11 @@ typedef struct _mp_bluetooth_nimble_l2cap_channel_t { os_membuf_t sdu_mem[]; } mp_bluetooth_nimble_l2cap_channel_t; +STATIC void destroy_l2cap_channel(); +STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg); +STATIC mp_bluetooth_nimble_l2cap_channel_t *get_l2cap_channel_for_conn_cid(uint16_t conn_handle, uint16_t cid); +STATIC int create_l2cap_channel(uint16_t mtu, mp_bluetooth_nimble_l2cap_channel_t **out); + STATIC void destroy_l2cap_channel() { // Only free the l2cap channel if we're the one that initiated the connection. // Listeners continue listening on the same channel. @@ -1395,9 +1443,9 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { ble_l2cap_get_chan_info(event->connect.chan, &info); if (event->connect.status == 0) { - mp_bluetooth_gattc_on_l2cap_connect(event->connect.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); + mp_bluetooth_on_l2cap_connect(event->connect.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); } else { - mp_bluetooth_gattc_on_l2cap_disconnect(event->connect.conn_handle, info.scid, info.psm, event->connect.status); + mp_bluetooth_on_l2cap_disconnect(event->connect.conn_handle, info.scid, info.psm, event->connect.status); destroy_l2cap_channel(); } break; @@ -1405,7 +1453,7 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { case BLE_L2CAP_EVENT_COC_DISCONNECTED: { DEBUG_printf("l2cap_channel_event: disconnect: conn_handle=%d\n", event->disconnect.conn_handle); ble_l2cap_get_chan_info(event->disconnect.chan, &info); - mp_bluetooth_gattc_on_l2cap_disconnect(event->disconnect.conn_handle, info.scid, info.psm, 0); + mp_bluetooth_on_l2cap_disconnect(event->disconnect.conn_handle, info.scid, info.psm, 0); destroy_l2cap_channel(); break; } @@ -1413,7 +1461,7 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { DEBUG_printf("l2cap_channel_event: accept: conn_handle=%d peer_sdu_size=%d\n", event->accept.conn_handle, event->accept.peer_sdu_size); chan->chan = event->accept.chan; ble_l2cap_get_chan_info(event->accept.chan, &info); - int ret = mp_bluetooth_gattc_on_l2cap_accept(event->accept.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); + int ret = mp_bluetooth_on_l2cap_accept(event->accept.conn_handle, info.scid, info.psm, info.our_coc_mtu, info.peer_coc_mtu); if (ret != 0) { return ret; } @@ -1461,7 +1509,7 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { // Don't allow granting more credits until after the IRQ is handled. chan->irq_in_progress = true; - mp_bluetooth_gattc_on_l2cap_recv(event->receive.conn_handle, info.scid); + mp_bluetooth_on_l2cap_recv(event->receive.conn_handle, info.scid); chan->irq_in_progress = false; // If all data has been consumed by the IRQ handler, then now allow @@ -1480,7 +1528,7 @@ STATIC int l2cap_channel_event(struct ble_l2cap_event *event, void *arg) { DEBUG_printf("l2cap_channel_event: tx_unstalled: conn_handle=%d status=%d\n", event->tx_unstalled.conn_handle, event->tx_unstalled.status); ble_l2cap_get_chan_info(event->receive.chan, &info); // Map status to {0,1} (i.e. "sent everything", or "partial send"). - mp_bluetooth_gattc_on_l2cap_send_ready(event->tx_unstalled.conn_handle, info.scid, event->tx_unstalled.status == 0 ? 0 : 1); + mp_bluetooth_on_l2cap_send_ready(event->tx_unstalled.conn_handle, info.scid, event->tx_unstalled.status == 0 ? 0 : 1); break; } case BLE_L2CAP_EVENT_COC_RECONFIG_COMPLETED: { From 1342debb9b419f78566e173b55b67b15f0a89ee4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 19 Feb 2021 16:21:05 +1100 Subject: [PATCH 0648/3533] tests/multi_bluetooth: Add basic performance tests. 1. Exchange GATT notifications. 2. Transmit a stream of data over L2CAP. Signed-off-by: Jim Mussared --- tests/multi_bluetooth/perf_gatt_notify.py | 133 ++++++++++++++++ tests/multi_bluetooth/perf_gatt_notify.py.exp | 0 tests/multi_bluetooth/perf_l2cap.py | 148 ++++++++++++++++++ tests/multi_bluetooth/perf_l2cap.py.exp | 0 4 files changed, 281 insertions(+) create mode 100644 tests/multi_bluetooth/perf_gatt_notify.py create mode 100644 tests/multi_bluetooth/perf_gatt_notify.py.exp create mode 100644 tests/multi_bluetooth/perf_l2cap.py create mode 100644 tests/multi_bluetooth/perf_l2cap.py.exp diff --git a/tests/multi_bluetooth/perf_gatt_notify.py b/tests/multi_bluetooth/perf_gatt_notify.py new file mode 100644 index 0000000000000..88986dda3d9b2 --- /dev/null +++ b/tests/multi_bluetooth/perf_gatt_notify.py @@ -0,0 +1,133 @@ +# Ping-pong GATT notifications between two devices. + +from micropython import const +import time, machine, bluetooth + +TIMEOUT_MS = 2000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_NOTIFY = const(18) + +# How long to run the test for. +_NUM_NOTIFICATIONS = const(50) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = ( + CHAR_UUID, + bluetooth.FLAG_NOTIFY, +) +SERVICE = ( + SERVICE_UUID, + (CHAR,), +) +SERVICES = (SERVICE,) + +is_central = False + +waiting_events = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_CONNECT: + waiting_events[event] = data[0] + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + # conn_handle, def_handle, value_handle, properties, uuid = data + if data[-1] == CHAR_UUID: + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_GATTC_NOTIFY: + if is_central: + conn_handle, value_handle, notify_data = data + ble.gatts_notify(conn_handle, value_handle, b"central" + notify_data) + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services(SERVICES) + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect to us. + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + + # Discover characteristics. + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + # Give the central enough time to discover chars. + time.sleep_ms(500) + + ticks_start = time.ticks_ms() + + for i in range(_NUM_NOTIFICATIONS): + # Send a notification and wait for a response. + ble.gatts_notify(conn_handle, value_handle, "peripheral" + str(i)) + wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS) + + ticks_end = time.ticks_ms() + ticks_total = time.ticks_diff(ticks_end, ticks_start) + print( + "Acknowledged {} notifications in {} ms. {} ms/notification.".format( + _NUM_NOTIFICATIONS, ticks_total, ticks_total // _NUM_NOTIFICATIONS + ) + ) + + # Disconnect the central. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + global is_central + is_central = True + ((char_handle,),) = ble.gatts_register_services(SERVICES) + multitest.next() + try: + # Connect to peripheral and then disconnect. + print("gap_connect") + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + # Discover characteristics. + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + # The IRQ handler will respond to each notification. + + # Wait for the peripheral to disconnect us. + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, 20000) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/perf_gatt_notify.py.exp b/tests/multi_bluetooth/perf_gatt_notify.py.exp new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/multi_bluetooth/perf_l2cap.py b/tests/multi_bluetooth/perf_l2cap.py new file mode 100644 index 0000000000000..9b07bb1dcef2f --- /dev/null +++ b/tests/multi_bluetooth/perf_l2cap.py @@ -0,0 +1,148 @@ +# Send L2CAP data as fast as possible and time it. + +from micropython import const +import time, machine, bluetooth, random + +TIMEOUT_MS = 1000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_L2CAP_ACCEPT = const(22) +_IRQ_L2CAP_CONNECT = const(23) +_IRQ_L2CAP_DISCONNECT = const(24) +_IRQ_L2CAP_RECV = const(25) +_IRQ_L2CAP_SEND_READY = const(26) + +_L2CAP_PSM = const(22) +_L2CAP_MTU = const(512) + +_PAYLOAD_LEN = const(_L2CAP_MTU) +_NUM_PAYLOADS = const(20) + +_RANDOM_SEED = 22 + + +waiting_events = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, addr_type, addr = data + waiting_events[event] = conn_handle + elif event == _IRQ_PERIPHERAL_CONNECT: + conn_handle, addr_type, addr = data + waiting_events[event] = conn_handle + elif event == _IRQ_L2CAP_ACCEPT: + conn_handle, cid, psm, our_mtu, peer_mtu = data + waiting_events[event] = (conn_handle, cid, psm) + elif event == _IRQ_L2CAP_CONNECT: + conn_handle, cid, psm, our_mtu, peer_mtu = data + waiting_events[event] = (conn_handle, cid, psm, our_mtu, peer_mtu) + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +def send_data(ble, conn_handle, cid): + buf = bytearray(_PAYLOAD_LEN) + for i in range(_NUM_PAYLOADS): + for j in range(_PAYLOAD_LEN): + buf[j] = random.randint(0, 255) + if not ble.l2cap_send(conn_handle, cid, buf): + wait_for_event(_IRQ_L2CAP_SEND_READY, TIMEOUT_MS) + + +def recv_data(ble, conn_handle, cid): + buf = bytearray(_PAYLOAD_LEN) + recv_bytes = 0 + recv_correct = 0 + expected_bytes = _PAYLOAD_LEN * _NUM_PAYLOADS + ticks_first_byte = 0 + while recv_bytes < expected_bytes: + wait_for_event(_IRQ_L2CAP_RECV, TIMEOUT_MS) + if not ticks_first_byte: + ticks_first_byte = time.ticks_ms() + while True: + n = ble.l2cap_recvinto(conn_handle, cid, buf) + if n == 0: + break + recv_bytes += n + for i in range(n): + if buf[i] == random.randint(0, 255): + recv_correct += 1 + ticks_end = time.ticks_ms() + return recv_bytes, recv_correct, time.ticks_diff(ticks_end, ticks_first_byte) + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect to us. + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + + ble.l2cap_listen(_L2CAP_PSM, _L2CAP_MTU) + + conn_handle, cid, psm = wait_for_event(_IRQ_L2CAP_ACCEPT, TIMEOUT_MS) + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) + + random.seed(_RANDOM_SEED) + + send_data(ble, conn_handle, cid) + + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) + + # Wait for the central to disconnect. + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + multitest.next() + try: + # Connect to peripheral and then disconnect. + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + + ble.l2cap_connect(conn_handle, _L2CAP_PSM, _L2CAP_MTU) + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) + + random.seed(_RANDOM_SEED) + + recv_bytes, recv_correct, total_ticks = recv_data(ble, conn_handle, cid) + + # Disconnect channel. + ble.l2cap_disconnect(conn_handle, cid) + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) + + print( + "Received {}/{} bytes in {} ms. {} B/s".format( + recv_bytes, recv_correct, total_ticks, recv_bytes * 1000 // total_ticks + ) + ) + + # Disconnect from peripheral. + ble.gap_disconnect(conn_handle) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/perf_l2cap.py.exp b/tests/multi_bluetooth/perf_l2cap.py.exp new file mode 100644 index 0000000000000..e69de29bb2d1d From 9d674cf7ab8570637dfab23811800e3b98babad9 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 17 Feb 2021 11:07:34 +1100 Subject: [PATCH 0649/3533] stm32/uart: Add support for LPUART1 on L0, L4, H7 and WB MCUs. Add LPUART1 as a standard UART. No low power features are supported, yet. LPUART1 is enabled as the next available UART after the standard U(S)ARTs: STM32WB: LPUART1 = UART(2) STM32L0: LPUART1 = UART(6) STM32L4: LPUART1 = UART(6) STM32H7: LPUART1 = UART(9) On all ports: LPUART1 = machine.UART('LP1') LPUART1 is enabled by defining MICROPY_HW_LPUART1_TX and MICROPY_HW_LPUART1_RX in mpconfigboard.h. Signed-off-by: Chris Mason --- ports/stm32/boards/make-pins.py | 2 ++ ports/stm32/machine_uart.c | 29 ++++++++++++++++-- ports/stm32/mpconfigboard_common.h | 9 +++++- ports/stm32/mpconfigport.h | 2 +- ports/stm32/pin_defs_stm32.h | 5 ++++ ports/stm32/stm32_it.c | 18 ++++++++++++ ports/stm32/uart.c | 47 ++++++++++++++++++++++++++++-- ports/stm32/uart.h | 3 ++ 8 files changed, 108 insertions(+), 7 deletions(-) diff --git a/ports/stm32/boards/make-pins.py b/ports/stm32/boards/make-pins.py index a91ed8a2c9150..a195323318112 100755 --- a/ports/stm32/boards/make-pins.py +++ b/ports/stm32/boards/make-pins.py @@ -14,6 +14,7 @@ "I2S": ["CK", "MCK", "SD", "WS", "EXTSD"], "USART": ["RX", "TX", "CTS", "RTS", "CK"], "UART": ["RX", "TX", "CTS", "RTS"], + "LPUART": ["RX", "TX", "CTS", "RTS"], "SPI": ["NSS", "SCK", "MISO", "MOSI"], "SDMMC": ["CK", "CMD", "D0", "D1", "D2", "D3"], "CAN": ["TX", "RX"], @@ -24,6 +25,7 @@ "I2S": "MICROPY_HW_ENABLE_I2S{num}", "SPI": "MICROPY_HW_SPI{num}_SCK", "UART": "MICROPY_HW_UART{num}_TX", + "LPUART": "MICROPY_HW_LPUART{num}_TX", "USART": "MICROPY_HW_UART{num}_TX", "SDMMC": "MICROPY_HW_SDMMC{num}_CK", "CAN": "MICROPY_HW_CAN{num}_TX", diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 31e6a1789e4a3..eceb5b6e7a8b2 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -76,7 +76,14 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); if (!self->is_enabled) { - mp_printf(print, "UART(%u)", self->uart_id); + #ifdef LPUART1 + if (self->uart_id == PYB_LPUART_1) { + mp_printf(print, "UART('LP1')"); + } else + #endif + { + mp_printf(print, "UART(%u)", self->uart_id); + } } else { mp_int_t bits; uint32_t cr1 = self->uartx->CR1; @@ -98,8 +105,16 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k if (cr1 & USART_CR1_PCE) { bits -= 1; } - mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=", - self->uart_id, uart_get_baudrate(self), bits); + #ifdef LPUART1 + if (self->uart_id == PYB_LPUART_1) { + mp_printf(print, "UART('LP1', baudrate=%u, bits=%u, parity=", + uart_get_baudrate(self), bits); + } else + #endif + { + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=", + self->uart_id, uart_get_baudrate(self), bits); + } if (!(cr1 & USART_CR1_PCE)) { mp_print_str(print, "None"); } else if (!(cr1 & USART_CR1_PS)) { @@ -335,6 +350,14 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size } else if (strcmp(port, MICROPY_HW_UART10_NAME) == 0) { uart_id = PYB_UART_10; #endif + #ifdef MICROPY_HW_LPUART1_NAME + } else if (strcmp(port, MICROPY_HW_LPUART1_NAME) == 0) { + uart_id = PYB_LPUART_1; + #endif + #ifdef LPUART1 + } else if (strcmp(port, "LP1") == 0 && uart_exists(PYB_LPUART_1)) { + uart_id = PYB_LPUART_1; + #endif } else { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%s) doesn't exist"), port); } diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 615310e51b771..ed30d17bdcd3b 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -181,6 +181,7 @@ #define MICROPY_HW_MAX_I2C (2) #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (8) +#define MICROPY_HW_MAX_LPUART (0) // Configuration for STM32F4 series #elif defined(STM32F4) @@ -200,6 +201,7 @@ #else #define MICROPY_HW_MAX_UART (6) #endif +#define MICROPY_HW_MAX_LPUART (0) // Configuration for STM32F7 series #elif defined(STM32F7) @@ -214,6 +216,7 @@ #define MICROPY_HW_MAX_I2C (4) #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (8) +#define MICROPY_HW_MAX_LPUART (0) // Configuration for STM32H7 series #elif defined(STM32H7) @@ -223,6 +226,7 @@ #define MICROPY_HW_MAX_I2C (4) #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (8) +#define MICROPY_HW_MAX_LPUART (1) // Configuration for STM32L0 series #elif defined(STM32L0) @@ -232,6 +236,7 @@ #define MICROPY_HW_MAX_I2C (3) #define MICROPY_HW_MAX_TIMER (22) #define MICROPY_HW_MAX_UART (5) +#define MICROPY_HW_MAX_LPUART (1) // Configuration for STM32L4 series #elif defined(STM32L4) @@ -240,7 +245,8 @@ #define PYB_EXTI_NUM_VECTORS (23) #define MICROPY_HW_MAX_I2C (4) #define MICROPY_HW_MAX_TIMER (17) -#define MICROPY_HW_MAX_UART (6) +#define MICROPY_HW_MAX_UART (5) +#define MICROPY_HW_MAX_LPUART (1) // Configuration for STM32WB series #elif defined(STM32WB) @@ -250,6 +256,7 @@ #define MICROPY_HW_MAX_I2C (3) #define MICROPY_HW_MAX_TIMER (17) #define MICROPY_HW_MAX_UART (1) +#define MICROPY_HW_MAX_LPUART (1) #ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION #define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 407a3e6c6a403..5998cd9085367 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -375,7 +375,7 @@ struct _mp_bluetooth_btstack_root_pointers_t; struct _pyb_uart_obj_t *pyb_stdio_uart; \ \ /* pointers to all UART objects (if they have been created) */ \ - struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART]; \ + struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART + MICROPY_HW_MAX_LPUART]; \ \ /* pointers to all CAN objects (if they have been created) */ \ struct _pyb_can_obj_t *pyb_can_obj_all[MICROPY_HW_MAX_CAN]; \ diff --git a/ports/stm32/pin_defs_stm32.h b/ports/stm32/pin_defs_stm32.h index 33a1790807660..9285c190a63e0 100644 --- a/ports/stm32/pin_defs_stm32.h +++ b/ports/stm32/pin_defs_stm32.h @@ -47,6 +47,7 @@ enum { AF_FN_I2C, AF_FN_USART, AF_FN_UART = AF_FN_USART, + AF_FN_LPUART, AF_FN_SPI, AF_FN_I2S, AF_FN_SDMMC, @@ -77,6 +78,10 @@ enum { AF_PIN_TYPE_UART_RX = AF_PIN_TYPE_USART_RX, AF_PIN_TYPE_UART_CTS = AF_PIN_TYPE_USART_CTS, AF_PIN_TYPE_UART_RTS = AF_PIN_TYPE_USART_RTS, + AF_PIN_TYPE_LPUART_TX = AF_PIN_TYPE_USART_TX, + AF_PIN_TYPE_LPUART_RX = AF_PIN_TYPE_USART_RX, + AF_PIN_TYPE_LPUART_CTS = AF_PIN_TYPE_USART_CTS, + AF_PIN_TYPE_LPUART_RTS = AF_PIN_TYPE_USART_RTS, AF_PIN_TYPE_SPI_MOSI = 0, AF_PIN_TYPE_SPI_MISO, diff --git a/ports/stm32/stm32_it.c b/ports/stm32/stm32_it.c index 8e96da177b583..dc96e2dd6cf04 100644 --- a/ports/stm32/stm32_it.c +++ b/ports/stm32/stm32_it.c @@ -742,11 +742,13 @@ void USART1_IRQHandler(void) { IRQ_EXIT(USART1_IRQn); } +#if defined(USART2) void USART2_IRQHandler(void) { IRQ_ENTER(USART2_IRQn); uart_irq_handler(2); IRQ_EXIT(USART2_IRQn); } +#endif #if defined(STM32F0) @@ -772,29 +774,37 @@ void USART4_5_IRQHandler(void) { #else +#if defined(USART3) void USART3_IRQHandler(void) { IRQ_ENTER(USART3_IRQn); uart_irq_handler(3); IRQ_EXIT(USART3_IRQn); } +#endif +#if defined(UART4) void UART4_IRQHandler(void) { IRQ_ENTER(UART4_IRQn); uart_irq_handler(4); IRQ_EXIT(UART4_IRQn); } +#endif +#if defined(UART5) void UART5_IRQHandler(void) { IRQ_ENTER(UART5_IRQn); uart_irq_handler(5); IRQ_EXIT(UART5_IRQn); } +#endif +#if defined(USART6) void USART6_IRQHandler(void) { IRQ_ENTER(USART6_IRQn); uart_irq_handler(6); IRQ_EXIT(USART6_IRQn); } +#endif #if defined(UART7) void UART7_IRQHandler(void) { @@ -830,6 +840,14 @@ void UART10_IRQHandler(void) { #endif +#if defined(LPUART1) +void LPUART1_IRQHandler(void) { + IRQ_ENTER(LPUART1_IRQn); + uart_irq_handler(PYB_LPUART_1); + IRQ_EXIT(LPUART1_IRQn); +} +#endif + #if MICROPY_PY_PYB_LEGACY #if defined(MICROPY_HW_I2C1_SCL) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 8091ba05f99af..d40a883c52b84 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -201,6 +201,11 @@ bool uart_exists(int uart_id) { return true; #endif + #if defined(MICROPY_HW_LPUART1_TX) && defined(MICROPY_HW_LPUART1_RX) + case PYB_LPUART_1: + return true; + #endif + default: return false; } @@ -211,6 +216,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow) { USART_TypeDef *UARTx; IRQn_Type irqn; + uint8_t uart_fn = AF_FN_UART; int uart_unit; const pin_obj_t *pins[4] = {0}; @@ -406,6 +412,28 @@ bool uart_init(pyb_uart_obj_t *uart_obj, break; #endif + #if defined(MICROPY_HW_LPUART1_TX) && defined(MICROPY_HW_LPUART1_RX) + case PYB_LPUART_1: + uart_fn = AF_FN_LPUART; + uart_unit = 1; + UARTx = LPUART1; + irqn = LPUART1_IRQn; + pins[0] = MICROPY_HW_LPUART1_TX; + pins[1] = MICROPY_HW_LPUART1_RX; + #if defined(MICROPY_HW_LPUART1_RTS) + if (flow & UART_HWCONTROL_RTS) { + pins[2] = MICROPY_HW_LPUART1_RTS; + } + #endif + #if defined(MICROPY_HW_LPUART1_CTS) + if (flow & UART_HWCONTROL_CTS) { + pins[3] = MICROPY_HW_LPUART1_CTS; + } + #endif + __HAL_RCC_LPUART1_CLK_ENABLE(); + break; + #endif + default: // UART does not exist or is not configured for this board return false; @@ -416,7 +444,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj, for (uint i = 0; i < 4; i++) { if (pins[i] != NULL) { - bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_UART, uart_unit); + bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, uart_fn, uart_unit); if (!ret) { return false; } @@ -596,6 +624,13 @@ void uart_deinit(pyb_uart_obj_t *self) { __HAL_RCC_UART10_RELEASE_RESET(); __HAL_RCC_UART10_CLK_DISABLE(); #endif + #if defined(LPUART1) + } else if (self->uart_id == PYB_LPUART_1) { + HAL_NVIC_DisableIRQ(LPUART1_IRQn); + __HAL_RCC_LPUART1_FORCE_RESET(); + __HAL_RCC_LPUART1_RELEASE_RESET(); + __HAL_RCC_LPUART1_CLK_DISABLE(); + #endif } } @@ -677,7 +712,15 @@ uint32_t uart_get_source_freq(pyb_uart_obj_t *self) { uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { // This formula assumes UART_OVERSAMPLING_16 - return uart_get_source_freq(self) / self->uartx->BRR; + uint32_t source_freq = uart_get_source_freq(self); + #if defined(LPUART1) + if (self->uart_id == PYB_LPUART_1) { + return source_freq / (self->uartx->BRR >> 8); + } else + #endif + { + return source_freq / self->uartx->BRR; + } } void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) { diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index 570a79c932585..0490a617f67c6 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -40,6 +40,9 @@ typedef enum { PYB_UART_8 = 8, PYB_UART_9 = 9, PYB_UART_10 = 10, + #ifdef LPUART1 + PYB_LPUART_1 = MICROPY_HW_MAX_UART + 1, + #endif } pyb_uart_t; #define CHAR_WIDTH_8BIT (0) From 03a64f20771cd0582af61de31811294456fa695e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Feb 2021 15:24:38 +1100 Subject: [PATCH 0650/3533] stm32/boards/NUCLEO_WB55: Enable LPUART1 on PA2/PA3. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h index 2061349417c64..179369d943cbd 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h @@ -22,6 +22,8 @@ // UART buses #define MICROPY_HW_UART1_TX (pin_B6) #define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_LPUART1_TX (pin_A2) +#define MICROPY_HW_LPUART1_RX (pin_A3) // USART 1 is connected to the virtual com port on the ST-LINK #define MICROPY_HW_UART_REPL PYB_UART_1 #define MICROPY_HW_UART_REPL_BAUD 115200 From d334d781e1ec66dfdd1d165dfb481916639dccaf Mon Sep 17 00:00:00 2001 From: iTitou Date: Sun, 31 Jan 2021 17:56:52 +0100 Subject: [PATCH 0651/3533] tools/verifygitlog.py: Show required format regexp in error message. Signed-off-by: iTitou --- tools/verifygitlog.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/verifygitlog.py b/tools/verifygitlog.py index 0080b96bfaea9..cc4b80f4a97a8 100755 --- a/tools/verifygitlog.py +++ b/tools/verifygitlog.py @@ -57,8 +57,9 @@ def warning(err): # Subject line. subject_line = raw_body[0] very_verbose("subject_line", subject_line) - if not re.match(r"^[^!]+: [A-Z]+.+ .+\.$", subject_line): - error("Subject line should contain ': ' and end in '.': " + subject_line) + subject_line_format = r"^[^!]+: [A-Z]+.+ .+\.$" + if not re.match(subject_line_format, subject_line): + error("Subject line should match " + repr(subject_line_format) + ": " + subject_line) if len(subject_line) >= 73: error("Subject line should be 72 or less characters: " + subject_line) From d867d20d9a7d15b6506b89f5e1e638367f843894 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Feb 2021 11:30:12 +1100 Subject: [PATCH 0652/3533] py/mkrules.cmake: Rename QSTR_DEFS variables to QSTRDEFS. And also MICROPY_PY_QSTRDEFS to MICROPY_QSTRDEFS_PY. These variables are all related. Signed-off-by: Damien George --- py/mkrules.cmake | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/py/mkrules.cmake b/py/mkrules.cmake index e05dcb836a211..304f2be55880d 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -1,14 +1,14 @@ # CMake fragment for MicroPython rules -set(MICROPY_PY_QSTRDEFS "${MICROPY_PY_DIR}/qstrdefs.h") set(MICROPY_GENHDR_DIR "${CMAKE_BINARY_DIR}/genhdr") set(MICROPY_MPVERSION "${MICROPY_GENHDR_DIR}/mpversion.h") set(MICROPY_MODULEDEFS "${MICROPY_GENHDR_DIR}/moduledefs.h") -set(MICROPY_QSTR_DEFS_LAST "${MICROPY_GENHDR_DIR}/qstr.i.last") -set(MICROPY_QSTR_DEFS_SPLIT "${MICROPY_GENHDR_DIR}/qstr.split") -set(MICROPY_QSTR_DEFS_COLLECTED "${MICROPY_GENHDR_DIR}/qstrdefs.collected.h") -set(MICROPY_QSTR_DEFS_PREPROCESSED "${MICROPY_GENHDR_DIR}/qstrdefs.preprocessed.h") -set(MICROPY_QSTR_DEFS_GENERATED "${MICROPY_GENHDR_DIR}/qstrdefs.generated.h") +set(MICROPY_QSTRDEFS_PY "${MICROPY_PY_DIR}/qstrdefs.h") +set(MICROPY_QSTRDEFS_LAST "${MICROPY_GENHDR_DIR}/qstr.i.last") +set(MICROPY_QSTRDEFS_SPLIT "${MICROPY_GENHDR_DIR}/qstr.split") +set(MICROPY_QSTRDEFS_COLLECTED "${MICROPY_GENHDR_DIR}/qstrdefs.collected.h") +set(MICROPY_QSTRDEFS_PREPROCESSED "${MICROPY_GENHDR_DIR}/qstrdefs.preprocessed.h") +set(MICROPY_QSTRDEFS_GENERATED "${MICROPY_GENHDR_DIR}/qstrdefs.generated.h") # Provide defaults for preprocessor flags if not already defined if(NOT MICROPY_CPP_FLAGS) @@ -33,7 +33,7 @@ find_package(Python3 REQUIRED COMPONENTS Interpreter) target_sources(${MICROPY_TARGET} PRIVATE ${MICROPY_MPVERSION} - ${MICROPY_QSTR_DEFS_GENERATED} + ${MICROPY_QSTRDEFS_GENERATED} ) # Command to force the build of another command @@ -68,7 +68,7 @@ add_custom_command( # It only needs to be passed the list of MICROPY_SOURCE_QSTR files that have changed since # it was last run, but it looks like it's not possible to specify that with cmake. add_custom_command( - OUTPUT ${MICROPY_QSTR_DEFS_LAST} + OUTPUT ${MICROPY_QSTRDEFS_LAST} COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py pp ${CMAKE_C_COMPILER} -E output ${MICROPY_GENHDR_DIR}/qstr.i.last cflags ${MICROPY_CPP_FLAGS} -DNO_QSTR sources ${MICROPY_SOURCE_QSTR} DEPENDS ${MICROPY_MODULEDEFS} ${MICROPY_SOURCE_QSTR} @@ -77,34 +77,34 @@ add_custom_command( ) add_custom_command( - OUTPUT ${MICROPY_QSTR_DEFS_SPLIT} + OUTPUT ${MICROPY_QSTRDEFS_SPLIT} COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py split qstr ${MICROPY_GENHDR_DIR}/qstr.i.last ${MICROPY_GENHDR_DIR}/qstr _ - COMMAND touch ${MICROPY_QSTR_DEFS_SPLIT} - DEPENDS ${MICROPY_QSTR_DEFS_LAST} + COMMAND touch ${MICROPY_QSTRDEFS_SPLIT} + DEPENDS ${MICROPY_QSTRDEFS_LAST} VERBATIM COMMAND_EXPAND_LISTS ) add_custom_command( - OUTPUT ${MICROPY_QSTR_DEFS_COLLECTED} - COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py cat qstr _ ${MICROPY_GENHDR_DIR}/qstr ${MICROPY_QSTR_DEFS_COLLECTED} - DEPENDS ${MICROPY_QSTR_DEFS_SPLIT} + OUTPUT ${MICROPY_QSTRDEFS_COLLECTED} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py cat qstr _ ${MICROPY_GENHDR_DIR}/qstr ${MICROPY_QSTRDEFS_COLLECTED} + DEPENDS ${MICROPY_QSTRDEFS_SPLIT} VERBATIM COMMAND_EXPAND_LISTS ) add_custom_command( - OUTPUT ${MICROPY_QSTR_DEFS_PREPROCESSED} - COMMAND cat ${MICROPY_PY_QSTRDEFS} ${MICROPY_QSTR_DEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTR_DEFS_PREPROCESSED} - DEPENDS ${MICROPY_QSTR_DEFS_COLLECTED} + OUTPUT ${MICROPY_QSTRDEFS_PREPROCESSED} + COMMAND cat ${MICROPY_QSTRDEFS_PY} ${MICROPY_QSTRDEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTRDEFS_PREPROCESSED} + DEPENDS ${MICROPY_QSTRDEFS_COLLECTED} VERBATIM COMMAND_EXPAND_LISTS ) add_custom_command( - OUTPUT ${MICROPY_QSTR_DEFS_GENERATED} - COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdata.py ${MICROPY_QSTR_DEFS_PREPROCESSED} > ${MICROPY_QSTR_DEFS_GENERATED} - DEPENDS ${MICROPY_QSTR_DEFS_PREPROCESSED} + OUTPUT ${MICROPY_QSTRDEFS_GENERATED} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdata.py ${MICROPY_QSTRDEFS_PREPROCESSED} > ${MICROPY_QSTRDEFS_GENERATED} + DEPENDS ${MICROPY_QSTRDEFS_PREPROCESSED} VERBATIM COMMAND_EXPAND_LISTS ) @@ -127,7 +127,7 @@ if(MICROPY_FROZEN_MANIFEST) OUTPUT ${MICROPY_FROZEN_CONTENT} COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/tools/makemanifest.py -o ${MICROPY_FROZEN_CONTENT} -v "MPY_DIR=${MICROPY_DIR}" -v "PORT_DIR=${MICROPY_PORT_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MICROPY_CROSS_FLAGS} ${MICROPY_FROZEN_MANIFEST} DEPENDS MICROPY_FORCE_BUILD - ${MICROPY_QSTR_DEFS_GENERATED} + ${MICROPY_QSTRDEFS_GENERATED} VERBATIM ) endif() From 2adf20c5f28e4d36f7832412f4502be664ccdb23 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Feb 2021 11:32:00 +1100 Subject: [PATCH 0653/3533] py/mkrules.cmake: Add MICROPY_QSTRDEFS_PORT to qstr build process. This allows a port to specify a custom qstrdefsport.h file, the same as the QSTR_DEFS variable in a Makefile. Signed-off-by: Damien George --- py/mkrules.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 304f2be55880d..9c4c4afabe03e 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -95,8 +95,10 @@ add_custom_command( add_custom_command( OUTPUT ${MICROPY_QSTRDEFS_PREPROCESSED} - COMMAND cat ${MICROPY_QSTRDEFS_PY} ${MICROPY_QSTRDEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTRDEFS_PREPROCESSED} - DEPENDS ${MICROPY_QSTRDEFS_COLLECTED} + COMMAND cat ${MICROPY_QSTRDEFS_PY} ${MICROPY_QSTRDEFS_PORT} ${MICROPY_QSTRDEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E ${MICROPY_CPP_FLAGS} - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MICROPY_QSTRDEFS_PREPROCESSED} + DEPENDS ${MICROPY_QSTRDEFS_PY} + ${MICROPY_QSTRDEFS_PORT} + ${MICROPY_QSTRDEFS_COLLECTED} VERBATIM COMMAND_EXPAND_LISTS ) From 75db0b907942f4b2e7306d2ce90d2f014cd5a414 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 21 Feb 2021 11:33:15 +1100 Subject: [PATCH 0654/3533] esp32: Define MICROPY_QSTRDEFS_PORT to include special qstrs. Fixes issue #6942. Signed-off-by: Damien George --- ports/esp32/main/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 9fb48a90412ef..bd25d76ee45b0 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -5,6 +5,10 @@ get_filename_component(MICROPY_DIR ${PROJECT_DIR}/../.. ABSOLUTE) include(${MICROPY_DIR}/py/py.cmake) include(${MICROPY_DIR}/extmod/extmod.cmake) +set(MICROPY_QSTRDEFS_PORT + ${PROJECT_DIR}/qstrdefsport.h +) + set(MICROPY_SOURCE_EXTMOD_EXTRA ${MICROPY_DIR}/extmod/modonewire.c ) From 53f5bb05a9cdf59cc2deaf216c25c0c275e6dce7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Feb 2021 09:49:12 +1100 Subject: [PATCH 0655/3533] rp2,stm32: Enable MICROPY_PY_UBINASCII_CRC32 to get ubinascii.crc32(). These ports already have uzlib enabled so this additional ubinascii.crc32 function only costs about 90 bytes of flash. Signed-off-by: Damien George --- ports/rp2/mpconfigport.h | 1 + ports/stm32/mpconfigport.h | 1 + 2 files changed, 2 insertions(+) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 9ba381030b150..b3214acfc2b5d 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -102,6 +102,7 @@ #define MICROPY_PY_URE_SUB (1) #define MICROPY_PY_UHASHLIB (1) #define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_UBINASCII_CRC32 (1) #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 5998cd9085367..174ef22f241c7 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -165,6 +165,7 @@ #define MICROPY_PY_UCRYPTOLIB (MICROPY_PY_USSL) #ifndef MICROPY_PY_UBINASCII #define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_UBINASCII_CRC32 (1) #endif #ifndef MICROPY_PY_UOS #define MICROPY_PY_UOS (1) From cdaec0dcaffbfcb58e190738cf6e9d34541464f0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Feb 2021 14:33:31 +1100 Subject: [PATCH 0656/3533] tools/pydfu.py: Support DFU files with elements of zero size. Instead of raising a ZeroDivisionError, this tool now just skips any elements in the DFU file that have zero size. Signed-off-by: Damien George --- tools/pydfu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index 030f56bf852e3..42b4fa2da6feb 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -521,7 +521,7 @@ def write_elements(elements, mass_erase_used, progress=None): data = elem["data"] elem_size = size elem_addr = addr - if progress: + if progress and elem_size: progress(elem_addr, 0, elem_size) while size > 0: write_size = size From 680ce45323248df288ee8ebd055a4caacb3e46f3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Mar 2021 12:14:54 +1100 Subject: [PATCH 0657/3533] stm32/rfcore: Allow BLE settings to be changed by a board. Two of the defaults have also changed in this commit: - MICROPY_HW_RFCORE_BLE_LSE_SOURCE changed from 1 to 0, which configures the LsSource to be LSE (needed due to errata 2.2.1). - MICROPY_HW_RFCORE_BLE_VITERBI_MODE changed from 0 to 1, which enables Viterbi mode, following all the ST examples. Signed-off-by: Damien George --- ports/stm32/mpconfigboard_common.h | 20 ++++++++++++++++ ports/stm32/rfcore.c | 38 +++++++++++++++--------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index ed30d17bdcd3b..948897b215717 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -262,6 +262,26 @@ #define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1) #endif +// RF core BLE configuration (a board should define +// MICROPY_HW_RFCORE_BLE_NUM_GATT_ATTRIBUTES to override all values) +#ifndef MICROPY_HW_RFCORE_BLE_NUM_GATT_ATTRIBUTES +#define MICROPY_HW_RFCORE_BLE_NUM_GATT_ATTRIBUTES (0) +#define MICROPY_HW_RFCORE_BLE_NUM_GATT_SERVICES (0) +#define MICROPY_HW_RFCORE_BLE_ATT_VALUE_ARRAY_SIZE (0) +#define MICROPY_HW_RFCORE_BLE_NUM_LINK (1) +#define MICROPY_HW_RFCORE_BLE_DATA_LENGTH_EXTENSION (1) +#define MICROPY_HW_RFCORE_BLE_PREPARE_WRITE_LIST_SIZE (0) +#define MICROPY_HW_RFCORE_BLE_MBLOCK_COUNT (0x79) +#define MICROPY_HW_RFCORE_BLE_MAX_ATT_MTU (0) +#define MICROPY_HW_RFCORE_BLE_SLAVE_SCA (0) +#define MICROPY_HW_RFCORE_BLE_MASTER_SCA (0) +#define MICROPY_HW_RFCORE_BLE_LSE_SOURCE (0) // use LSE to clock the rfcore (see errata 2.2.1) +#define MICROPY_HW_RFCORE_BLE_MAX_CONN_EVENT_LENGTH (0xffffffff) +#define MICROPY_HW_RFCORE_BLE_HSE_STARTUP_TIME (0x148) +#define MICROPY_HW_RFCORE_BLE_VITERBI_MODE (1) +#define MICROPY_HW_RFCORE_BLE_LL_ONLY (1) // use LL only, we provide the rest of the BLE stack +#endif + #else #error Unsupported MCU series #endif diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index b11c4f2022733..55fc229dd0c30 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -548,30 +548,30 @@ static const struct { uint16_t AttMtu; uint16_t SlaveSca; uint8_t MasterSca; - uint8_t LsSource; // 0=LSE 1=internal RO + uint8_t LsSource; uint32_t MaxConnEventLength; uint16_t HsStartupTime; uint8_t ViterbiEnable; - uint8_t LlOnly; // 0=LL+Host, 1=LL only + uint8_t LlOnly; uint8_t HwVersion; } ble_init_params = { - 0, - 0, - 0, // NumAttrRecord - 0, // NumAttrServ - 0, // AttrValueArrSize - 1, // NumOfLinks - 1, // ExtendedPacketLengthEnable - 0, // PrWriteListSize - 0x79, // MblockCount - 0, // AttMtu - 0, // SlaveSca - 0, // MasterSca - 1, // LsSource - 0xffffffff, // MaxConnEventLength - 0x148, // HsStartupTime - 0, // ViterbiEnable - 1, // LlOnly + 0, // pBleBufferAddress + 0, // BleBufferSize + MICROPY_HW_RFCORE_BLE_NUM_GATT_ATTRIBUTES, + MICROPY_HW_RFCORE_BLE_NUM_GATT_SERVICES, + MICROPY_HW_RFCORE_BLE_ATT_VALUE_ARRAY_SIZE, + MICROPY_HW_RFCORE_BLE_NUM_LINK, + MICROPY_HW_RFCORE_BLE_DATA_LENGTH_EXTENSION, + MICROPY_HW_RFCORE_BLE_PREPARE_WRITE_LIST_SIZE, + MICROPY_HW_RFCORE_BLE_MBLOCK_COUNT, + MICROPY_HW_RFCORE_BLE_MAX_ATT_MTU, + MICROPY_HW_RFCORE_BLE_SLAVE_SCA, + MICROPY_HW_RFCORE_BLE_MASTER_SCA, + MICROPY_HW_RFCORE_BLE_LSE_SOURCE, + MICROPY_HW_RFCORE_BLE_MAX_CONN_EVENT_LENGTH, + MICROPY_HW_RFCORE_BLE_HSE_STARTUP_TIME, + MICROPY_HW_RFCORE_BLE_VITERBI_MODE, + MICROPY_HW_RFCORE_BLE_LL_ONLY, 0, // HwVersion }; From 59a129f22f096d992496111c498b3ea97e637115 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 5 Mar 2021 09:46:14 +1100 Subject: [PATCH 0658/3533] stm32/storage: Prevent attempts to read/write invalid block addresses. A corrupt filesystem may lead to a request for a block which is out of range of the block device limits. Return an error instead of passing the request down to the lower layer. --- ports/stm32/storage.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 6581860ff3ffb..a71c4a3ea7791 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -340,8 +340,12 @@ STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) { else if (self != &pyb_flash_obj) { // Extended block read on a sub-section of the flash storage uint32_t offset = mp_obj_get_int(args[3]); - block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; - ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) { + ret = -MP_EFAULT; // Bad address + } else { + block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; + ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + } } #endif return MP_OBJ_NEW_SMALL_INT(ret); @@ -363,8 +367,12 @@ STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) { else if (self != &pyb_flash_obj) { // Extended block write on a sub-section of the flash storage uint32_t offset = mp_obj_get_int(args[3]); - block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; - ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) { + ret = -MP_EFAULT; // Bad address + } else { + block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; + ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + } } #endif return MP_OBJ_NEW_SMALL_INT(ret); From 35c602d3b8e1ee2d06715c75fa0b4ca7a353372e Mon Sep 17 00:00:00 2001 From: Herwin Grobben Date: Fri, 5 Mar 2021 10:05:19 +0100 Subject: [PATCH 0659/3533] stm32/make-stmconst.py: Allow "[]" chars when parsing source comments. For STM32WB MCUs, EXTI offset addresses were not parsed due to the appearance of "[31:0]" in a comment in the .h file. --- ports/stm32/make-stmconst.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/make-stmconst.py b/ports/stm32/make-stmconst.py index ac5c56f5c79f5..602bdc6c192d7 100644 --- a/ports/stm32/make-stmconst.py +++ b/ports/stm32/make-stmconst.py @@ -46,7 +46,7 @@ def __init__(self, line): class Lexer: re_io_reg = r"__IO uint(?P8|16|32)_t +(?P[A-Z0-9]+)" - re_comment = r"(?P[A-Za-z0-9 \-/_()&:]+)" + re_comment = r"(?P[A-Za-z0-9 \-/_()&:\[\]]+)" re_addr_offset = r"Address offset: (?P0x[0-9A-Z]{2,3})" regexs = ( ( From 85ea4ac0e56faca680aa03c41eb4009464401f07 Mon Sep 17 00:00:00 2001 From: Braiden Kindt Date: Sun, 14 Feb 2021 23:42:57 -0500 Subject: [PATCH 0660/3533] stm32/main: Fix passing state.reset_mode to init_flash_fs. state.reset_mode is updated by `MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP` but not passed to `init_flash_fs`, and so factory reset is not executed on boards that do not have a bootloader. This bug was introduced by 4c3976bbcaf58266fdcaab264fee5b7a94a682e5 Fixes #6903. --- ports/stm32/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 38710e2651fb3..bdf7328f68e43 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -531,7 +531,7 @@ void stm32_main(uint32_t reset_mode) { // Create it if needed, mount in on /flash, and set it as current dir. bool mounted_flash = false; #if MICROPY_HW_FLASH_MOUNT_AT_BOOT - mounted_flash = init_flash_fs(reset_mode); + mounted_flash = init_flash_fs(state.reset_mode); #endif bool mounted_sdcard = false; From 0facd891e7b4e226e7a905309f917dc271c1860b Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sun, 27 Sep 2020 08:42:52 +0100 Subject: [PATCH 0661/3533] stm32/powerctrl: Save and restore EWUP state when configuring standby. This allows the user to enable wake-up sources using the EWUP bits, on F7 MCUs. Disabling the wake-up sources while clearing the wake-up flags follows the reference manual and ST examples. --- ports/stm32/powerctrl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index bf8be647f4d76..a579713b6f9bf 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -712,10 +712,14 @@ void powerctrl_enter_standby_mode(void) { RTC->ISR &= ~ISR_BITS; #if defined(STM32F7) + // Save EWUP state + uint32_t csr2_ewup = PWR->CSR2 & (PWR_CSR2_EWUP6 | PWR_CSR2_EWUP5 | PWR_CSR2_EWUP4 | PWR_CSR2_EWUP3 | PWR_CSR2_EWUP2 | PWR_CSR2_EWUP1); // disable wake-up flags PWR->CSR2 &= ~(PWR_CSR2_EWUP6 | PWR_CSR2_EWUP5 | PWR_CSR2_EWUP4 | PWR_CSR2_EWUP3 | PWR_CSR2_EWUP2 | PWR_CSR2_EWUP1); // clear global wake-up flag PWR->CR2 |= PWR_CR2_CWUPF6 | PWR_CR2_CWUPF5 | PWR_CR2_CWUPF4 | PWR_CR2_CWUPF3 | PWR_CR2_CWUPF2 | PWR_CR2_CWUPF1; + // Restore EWUP state + PWR->CSR2 |= csr2_ewup; #elif defined(STM32H7) EXTI_D1->PR1 = 0x3fffff; PWR->WKUPCR |= PWR_WAKEUP_FLAG1 | PWR_WAKEUP_FLAG2 | PWR_WAKEUP_FLAG3 | PWR_WAKEUP_FLAG4 | PWR_WAKEUP_FLAG5 | PWR_WAKEUP_FLAG6; From 79c186f5c962b514cde21415d7a262cfdbddbd97 Mon Sep 17 00:00:00 2001 From: Reinhard Feger <47209718+rf-eng@users.noreply.github.com> Date: Sat, 22 Aug 2020 18:48:21 +0200 Subject: [PATCH 0662/3533] stm32/spi: Fix baudrate calculation for H7 series. Fixes issue #6342. --- ports/stm32/spi.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 07374d7a38eef..f8ab791898d3f 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -633,7 +633,11 @@ void spi_print(const mp_print_t *print, const spi_t *spi_obj, bool legacy) { if (spi->State != HAL_SPI_STATE_RESET) { if (spi->Init.Mode == SPI_MODE_MASTER) { // compute baudrate + #if defined(STM32H7) + uint log_prescaler = (spi->Init.BaudRatePrescaler >> 28) + 1; + #else uint log_prescaler = (spi->Init.BaudRatePrescaler >> 3) + 1; + #endif uint baudrate = spi_get_source_freq(spi) >> log_prescaler; if (legacy) { mp_printf(print, ", SPI.MASTER"); From c33c749f64fc664e46529885d84e499ea2c3d461 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Mar 2021 09:04:36 +1100 Subject: [PATCH 0663/3533] stm32/boardctrl: Add MICROPY_BOARD_STARTUP hook. Signed-off-by: Damien George --- ports/stm32/boardctrl.h | 4 ++++ ports/stm32/main.c | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index 6f79dfb890707..f1cbc3b955b3c 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -28,6 +28,10 @@ #include "py/mpconfig.h" +#ifndef MICROPY_BOARD_STARTUP +#define MICROPY_BOARD_STARTUP powerctrl_check_enter_bootloader +#endif + #ifndef MICROPY_BOARD_EARLY_INIT #define MICROPY_BOARD_EARLY_INIT() #endif diff --git a/ports/stm32/main.c b/ports/stm32/main.c index bdf7328f68e43..888b20513c6f9 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -313,8 +313,9 @@ void stm32_main(uint32_t reset_mode) { // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI SCB->CCR |= SCB_CCR_STKALIGN_Msk; - // Check if bootloader should be entered instead of main application - powerctrl_check_enter_bootloader(); + // Hook for a board to run code at start up, for example check if a + // bootloader should be entered instead of the main application. + MICROPY_BOARD_STARTUP(); // Enable caches and prefetch buffers From 7d73b9ff9975261bd2ff3544b09e9ec494775435 Mon Sep 17 00:00:00 2001 From: Mirko Vogt Date: Tue, 9 Feb 2021 18:28:36 +0000 Subject: [PATCH 0664/3533] lib/mbedtls: Switch to currently latest commit of LTS branch v2.16. From a version numbering point of view this is a downgrade (2.17.0 -> 2.16.x). However the latest commit for version 2.17.0 is from March 2019 and no further minor release happened after 2.17.0. This version is EOL. 2.16.x though is still actively maintained as a long term release, hence security and stability fixes are still being backported, including compatibility with upcoming compiler releases. --- lib/mbedtls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mbedtls b/lib/mbedtls index 3f8d78411a26e..1b6a24f759cef 160000 --- a/lib/mbedtls +++ b/lib/mbedtls @@ -1 +1 @@ -Subproject commit 3f8d78411a26e833db18d9fbde0e2f0baeda87f0 +Subproject commit 1b6a24f759cefbb4a69ae9476885d7f42a847e3d From 2d5cece5ac3fc467055dd3e27378ad497adcee00 Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Thu, 18 Feb 2021 01:28:42 +0200 Subject: [PATCH 0665/3533] py/nlr: Implement NLR for AArch64. --- py/nlr.h | 4 +++ py/nlraarch64.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ py/py.mk | 1 + 3 files changed, 79 insertions(+) create mode 100644 py/nlraarch64.c diff --git a/py/nlr.h b/py/nlr.h index f9fbf56e54e60..9f12ede9cd3ac 100644 --- a/py/nlr.h +++ b/py/nlr.h @@ -39,6 +39,7 @@ #define MICROPY_NLR_NUM_REGS_X64_WIN (10) #define MICROPY_NLR_NUM_REGS_ARM_THUMB (10) #define MICROPY_NLR_NUM_REGS_ARM_THUMB_FP (10 + 6) +#define MICROPY_NLR_NUM_REGS_AARCH64 (13) #define MICROPY_NLR_NUM_REGS_XTENSA (10) #define MICROPY_NLR_NUM_REGS_XTENSAWIN (17) @@ -72,6 +73,9 @@ // so only save/restore those as an optimisation. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB_FP) #endif +#elif defined(__aarch64__) + #define MICROPY_NLR_AARCH64 (1) + #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_AARCH64) #elif defined(__xtensa__) #define MICROPY_NLR_XTENSA (1) #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_XTENSA) diff --git a/py/nlraarch64.c b/py/nlraarch64.c new file mode 100644 index 0000000000000..2df0dc9c85021 --- /dev/null +++ b/py/nlraarch64.c @@ -0,0 +1,74 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Yonatan Goldschmidt + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpstate.h" // needed for NLR defs + +#if MICROPY_NLR_AARCH64 + +// AArch64 callee-saved registers are x19-x29. +// https://en.wikipedia.org/wiki/Calling_convention#ARM_(A64) + +// Implemented purely as inline assembly; inside a function, we have to deal with undoing the prologue, restoring +// SP and LR. This way, we don't. +__asm( + "nlr_push: \n" + ".global nlr_push \n" + "mov x9, sp \n" + "stp lr, x9, [x0, #16]\n" // 16 == offsetof(nlr_buf_t, regs) + "stp x19, x20, [x0, #32]\n" + "stp x21, x22, [x0, #48]\n" + "stp x23, x24, [x0, #64]\n" + "stp x25, x26, [x0, #80]\n" + "stp x27, x28, [x0, #96]\n" + "str x29, [x0, #112]\n" + "b nlr_push_tail \n" // do the rest in C + ); + +NORETURN void nlr_jump(void *val) { + MP_NLR_JUMP_HEAD(val, top) + + MP_STATIC_ASSERT(offsetof(nlr_buf_t, regs) == 16); // asm assumes it + + __asm volatile ( + "ldr x29, [%0, #112]\n" + "ldp x27, x28, [%0, #96]\n" + "ldp x25, x26, [%0, #80]\n" + "ldp x23, x24, [%0, #64]\n" + "ldp x21, x22, [%0, #48]\n" + "ldp x19, x20, [%0, #32]\n" + "ldp lr, x9, [%0, #16]\n" // 16 == offsetof(nlr_buf_t, regs) + "mov sp, x9 \n" + "mov x0, #1 \n" // non-local return + "ret \n" + : + : "r" (top) + : + ); + + MP_UNREACHABLE +} + +#endif // MICROPY_NLR_AARCH64 diff --git a/py/py.mk b/py/py.mk index bac38f07412f9..59abc8f50373b 100644 --- a/py/py.mk +++ b/py/py.mk @@ -57,6 +57,7 @@ PY_CORE_O_BASENAME = $(addprefix py/,\ nlrx86.o \ nlrx64.o \ nlrthumb.o \ + nlraarch64.o \ nlrpowerpc.o \ nlrxtensa.o \ nlrsetjmp.o \ From e196cb762e5a946bee0404b043acd673178c162d Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Thu, 18 Feb 2021 01:28:09 +0200 Subject: [PATCH 0666/3533] py/nlrx64: Fix typo in comment. --- py/nlrx64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/nlrx64.c b/py/nlrx64.c index eb39dadce72c1..f5d02afbdb979 100644 --- a/py/nlrx64.c +++ b/py/nlrx64.c @@ -89,7 +89,7 @@ NORETURN void nlr_jump(void *val) { "movq %0, %%rcx \n" // %rcx points to nlr_buf #if MICROPY_NLR_OS_WINDOWS "movq 88(%%rcx), %%rsi \n" // load saved %rsi - "movq 80(%%rcx), %%rdi \n" // load saved %rdr + "movq 80(%%rcx), %%rdi \n" // load saved %rdi #endif "movq 72(%%rcx), %%r15 \n" // load saved %r15 "movq 64(%%rcx), %%r14 \n" // load saved %r14 From 098ac11bb0cb912909af801015ac07bd2bf01ce7 Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Sun, 21 Feb 2021 02:05:40 +0200 Subject: [PATCH 0667/3533] lib/utils/gchelper_generic: Implement AArch64 support. --- lib/utils/gchelper.h | 2 ++ lib/utils/gchelper_generic.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/utils/gchelper.h b/lib/utils/gchelper.h index 4b6ead6ba650f..645ee837f5146 100644 --- a/lib/utils/gchelper.h +++ b/lib/utils/gchelper.h @@ -39,6 +39,8 @@ typedef uintptr_t gc_helper_regs_t[6]; typedef uintptr_t gc_helper_regs_t[4]; #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) typedef uintptr_t gc_helper_regs_t[10]; +#elif defined(__aarch64__) +typedef uintptr_t gc_helper_regs_t[11]; // x19-x29 #endif #endif diff --git a/lib/utils/gchelper_generic.c b/lib/utils/gchelper_generic.c index 9750e8b0c837a..3e7e33ab18ae0 100644 --- a/lib/utils/gchelper_generic.c +++ b/lib/utils/gchelper_generic.c @@ -123,6 +123,33 @@ STATIC void gc_helper_get_regs(gc_helper_regs_t arr) { arr[9] = r13; } +#elif defined(__aarch64__) + +STATIC void gc_helper_get_regs(gc_helper_regs_t arr) { + const register long x19 asm ("x19"); + const register long x20 asm ("x20"); + const register long x21 asm ("x21"); + const register long x22 asm ("x22"); + const register long x23 asm ("x23"); + const register long x24 asm ("x24"); + const register long x25 asm ("x25"); + const register long x26 asm ("x26"); + const register long x27 asm ("x27"); + const register long x28 asm ("x28"); + const register long x29 asm ("x29"); + arr[0] = x19; + arr[1] = x20; + arr[2] = x21; + arr[3] = x22; + arr[4] = x23; + arr[5] = x24; + arr[6] = x25; + arr[7] = x26; + arr[8] = x27; + arr[9] = x28; + arr[10] = x29; +} + #else #error "Architecture not supported for gc_helper_get_regs. Set MICROPY_GCREGS_SETJMP to use the fallback implementation." From 8785acac22a25d376d11c7dda5d0556432818ac2 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 26 Feb 2021 09:36:48 +1100 Subject: [PATCH 0668/3533] esp32/Makefile: Specify port and baud on erase_flash command. --- ports/esp32/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 1249795993614..9a3a15d6ee6c8 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -37,7 +37,7 @@ deploy: idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) flash erase: - idf.py $(IDFPY_FLAGS) erase_flash + idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) erase_flash submodules: git submodule update --init $(addprefix ../../,$(GIT_SUBMODULES)) From 23ce25a7c3175e2251af9af34a6c5eafa871bfcd Mon Sep 17 00:00:00 2001 From: svetelna Date: Mon, 7 Dec 2020 13:00:09 +0100 Subject: [PATCH 0669/3533] mimxrt/boards: Add MIMXRT1050_EVK board, based on MIMXRT1060_EVK. --- .../evkmimxrt1050_flexspi_nor_config.h | 267 ++++++++++++++++++ .../boards/MIMXRT1050_EVK/flash_config.c | 56 ++++ .../boards/MIMXRT1050_EVK/mpconfigboard.h | 9 + .../boards/MIMXRT1050_EVK/mpconfigboard.mk | 7 + ports/mimxrt/boards/MIMXRT1050_EVK/pins.c | 33 +++ ports/mimxrt/boards/MIMXRT1050_EVK/pins.h | 30 ++ ports/mimxrt/boards/MIMXRT1052.ld | 7 + 7 files changed, 409 insertions(+) create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/evkmimxrt1050_flexspi_nor_config.h create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/flash_config.c create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.mk create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1052.ld diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/evkmimxrt1050_flexspi_nor_config.h b/ports/mimxrt/boards/MIMXRT1050_EVK/evkmimxrt1050_flexspi_nor_config.h new file mode 100644 index 0000000000000..d8e842452e0cc --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/evkmimxrt1050_flexspi_nor_config.h @@ -0,0 +1,267 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __EVKMIMXRT1050_FLEXSPI_NOR_CONFIG__ +#define __EVKMIMXRT1050_FLEXSPI_NOR_CONFIG__ + +#include +#include +#include "fsl_common.h" + +/*! @name Driver version */ +/*@{*/ +/*! @brief XIP_BOARD driver version 2.0.0. */ +#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) +/*@}*/ + +/* FLEXSPI memory config block related defintions */ +#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian +#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0 +#define FLEXSPI_CFG_BLK_SIZE (512) + +/* FLEXSPI Feature related definitions */ +#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1 + +/* Lookup table related defintions */ +#define CMD_INDEX_READ 0 +#define CMD_INDEX_READSTATUS 1 +#define CMD_INDEX_WRITEENABLE 2 +#define CMD_INDEX_WRITE 4 + +#define CMD_LUT_SEQ_IDX_READ 0 +#define CMD_LUT_SEQ_IDX_READSTATUS 1 +#define CMD_LUT_SEQ_IDX_WRITEENABLE 3 +#define CMD_LUT_SEQ_IDX_WRITE 9 + +#define CMD_SDR 0x01 +#define CMD_DDR 0x21 +#define RADDR_SDR 0x02 +#define RADDR_DDR 0x22 +#define CADDR_SDR 0x03 +#define CADDR_DDR 0x23 +#define MODE1_SDR 0x04 +#define MODE1_DDR 0x24 +#define MODE2_SDR 0x05 +#define MODE2_DDR 0x25 +#define MODE4_SDR 0x06 +#define MODE4_DDR 0x26 +#define MODE8_SDR 0x07 +#define MODE8_DDR 0x27 +#define WRITE_SDR 0x08 +#define WRITE_DDR 0x28 +#define READ_SDR 0x09 +#define READ_DDR 0x29 +#define LEARN_SDR 0x0A +#define LEARN_DDR 0x2A +#define DATSZ_SDR 0x0B +#define DATSZ_DDR 0x2B +#define DUMMY_SDR 0x0C +#define DUMMY_DDR 0x2C +#define DUMMY_RWDS_SDR 0x0D +#define DUMMY_RWDS_DDR 0x2D +#define JMP_ON_CS 0x1F +#define STOP 0 + +#define FLEXSPI_1PAD 0 +#define FLEXSPI_2PAD 1 +#define FLEXSPI_4PAD 2 +#define FLEXSPI_8PAD 3 + +#define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \ + (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \ + FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1)) + +//!@brief Definitions for FlexSPI Serial Clock Frequency +typedef enum _FlexSpiSerialClockFreq +{ + kFlexSpiSerialClk_30MHz = 1, + kFlexSpiSerialClk_50MHz = 2, + kFlexSpiSerialClk_60MHz = 3, + kFlexSpiSerialClk_75MHz = 4, + kFlexSpiSerialClk_80MHz = 5, + kFlexSpiSerialClk_100MHz = 6, + kFlexSpiSerialClk_133MHz = 7, + kFlexSpiSerialClk_166MHz = 8, +} flexspi_serial_clk_freq_t; + +//!@brief FlexSPI clock configuration type +enum +{ + kFlexSpiClk_SDR, //!< Clock configure for SDR mode + kFlexSpiClk_DDR, //!< Clock configurat for DDR mode +}; + +//!@brief FlexSPI Read Sample Clock Source definition +typedef enum _FlashReadSampleClkSource +{ + kFlexSPIReadSampleClk_LoopbackInternally = 0, + kFlexSPIReadSampleClk_LoopbackFromDqsPad = 1, + kFlexSPIReadSampleClk_LoopbackFromSckPad = 2, + kFlexSPIReadSampleClk_ExternalInputFromDqsPad = 3, +} flexspi_read_sample_clk_t; + +//!@brief Misc feature bit definitions +enum +{ + kFlexSpiMiscOffset_DiffClkEnable = 0, //!< Bit for Differential clock enable + kFlexSpiMiscOffset_Ck2Enable = 1, //!< Bit for CK2 enable + kFlexSpiMiscOffset_ParallelEnable = 2, //!< Bit for Parallel mode enable + kFlexSpiMiscOffset_WordAddressableEnable = 3, //!< Bit for Word Addressable enable + kFlexSpiMiscOffset_SafeConfigFreqEnable = 4, //!< Bit for Safe Configuration Frequency enable + kFlexSpiMiscOffset_PadSettingOverrideEnable = 5, //!< Bit for Pad setting override enable + kFlexSpiMiscOffset_DdrModeEnable = 6, //!< Bit for DDR clock confiuration indication. +}; + +//!@brief Flash Type Definition +enum +{ + kFlexSpiDeviceType_SerialNOR = 1, //!< Flash devices are Serial NOR + kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND + kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH + kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND + kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs +}; + +//!@brief Flash Pad Definitions +enum +{ + kSerialFlash_1Pad = 1, + kSerialFlash_2Pads = 2, + kSerialFlash_4Pads = 4, + kSerialFlash_8Pads = 8, +}; + +//!@brief FlexSPI LUT Sequence structure +typedef struct _lut_sequence +{ + uint8_t seqNum; //!< Sequence Number, valid number: 1-16 + uint8_t seqId; //!< Sequence Index, valid number: 0-15 + uint16_t reserved; +} flexspi_lut_seq_t; + +//!@brief Flash Configuration Command Type +enum +{ + kDeviceConfigCmdType_Generic, //!< Generic command, for example: configure dummy cycles, drive strength, etc + kDeviceConfigCmdType_QuadEnable, //!< Quad Enable command + kDeviceConfigCmdType_Spi2Xpi, //!< Switch from SPI to DPI/QPI/OPI mode + kDeviceConfigCmdType_Xpi2Spi, //!< Switch from DPI/QPI/OPI to SPI mode + kDeviceConfigCmdType_Spi2NoCmd, //!< Switch to 0-4-4/0-8-8 mode + kDeviceConfigCmdType_Reset, //!< Reset device command +}; + +//!@brief FlexSPI Memory Configuration Block +typedef struct _FlexSPIConfig +{ + uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL + uint32_t version; //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix + uint32_t reserved0; //!< [0x008-0x00b] Reserved for future use + uint8_t readSampleClkSrc; //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3 + uint8_t csHoldTime; //!< [0x00d-0x00d] CS hold time, default value: 3 + uint8_t csSetupTime; //!< [0x00e-0x00e] CS setup time, default value: 3 + uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For + //! Serial NAND, need to refer to datasheet + uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable + uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch, + //! Generic configuration, etc. + uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for + //! DPI/QPI/OPI switch or reset command + flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt + //! sequence number, [31:16] Reserved + uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration + uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable + uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe + flexspi_lut_seq_t + configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq + uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use + uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands + uint32_t reserved2; //!< [0x03c-0x03f] Reserved for future use + uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more + //! details + uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details + uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal + uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot + //! Chapter for more details + uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot + //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH + uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use + uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1 + uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2 + uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1 + uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2 + uint32_t csPadSettingOverride; //!< [0x060-0x063] CS pad setting override value + uint32_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value + uint32_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value + uint32_t dqsPadSettingOverride; //!< [0x06c-0x06f] DQS pad setting override value + uint32_t timeoutInMs; //!< [0x070-0x073] Timeout threshold for read status command + uint32_t commandInterval; //!< [0x074-0x077] CS deselect interval between two commands + uint16_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B, in terms of 0.1ns + uint16_t busyOffset; //!< [0x07c-0x07d] Busy offset, valid value: 0-31 + uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 - + //! busy flag is 0 when flash device is busy + uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences + flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences + uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use +} flexspi_mem_config_t; + +/* */ +#define NOR_CMD_INDEX_READ CMD_INDEX_READ //!< 0 +#define NOR_CMD_INDEX_READSTATUS CMD_INDEX_READSTATUS //!< 1 +#define NOR_CMD_INDEX_WRITEENABLE CMD_INDEX_WRITEENABLE //!< 2 +#define NOR_CMD_INDEX_ERASESECTOR 3 //!< 3 +#define NOR_CMD_INDEX_PAGEPROGRAM CMD_INDEX_WRITE //!< 4 +#define NOR_CMD_INDEX_CHIPERASE 5 //!< 5 +#define NOR_CMD_INDEX_DUMMY 6 //!< 6 +#define NOR_CMD_INDEX_ERASEBLOCK 7 //!< 7 + +#define NOR_CMD_LUT_SEQ_IDX_READ CMD_LUT_SEQ_IDX_READ //!< 0 READ LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS \ + CMD_LUT_SEQ_IDX_READSTATUS //!< 1 Read Status LUT sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READSTATUS_XPI \ + 2 //!< 2 Read status DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE \ + CMD_LUT_SEQ_IDX_WRITEENABLE //!< 3 Write Enable sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE_XPI \ + 4 //!< 4 Write Enable DPI/QPI/OPI sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 //!< 5 Erase Sector sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_ERASEBLOCK 8 //!< 8 Erase Block sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM \ + CMD_LUT_SEQ_IDX_WRITE //!< 9 Program sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_CHIPERASE 11 //!< 11 Chip Erase sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_READ_SFDP 13 //!< 13 Read SFDP sequence in lookupTable id stored in config block +#define NOR_CMD_LUT_SEQ_IDX_RESTORE_NOCMD \ + 14 //!< 14 Restore 0-4-4/0-8-8 mode sequence id in lookupTable stored in config block +#define NOR_CMD_LUT_SEQ_IDX_EXIT_NOCMD \ + 15 //!< 15 Exit 0-4-4/0-8-8 mode sequence id in lookupTable stored in config blobk + +/* + * Serial NOR configuration block + */ +typedef struct _flexspi_nor_config +{ + flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI + uint32_t pageSize; //!< Page size of Serial NOR + uint32_t sectorSize; //!< Sector size of Serial NOR + uint8_t ipcmdSerialClkFreq; //!< Clock frequency for IP command + uint8_t isUniformBlockSize; //!< Sector/Block size is the same + uint8_t reserved0[2]; //!< Reserved for future use + uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3 + uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command + uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false + uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution + uint32_t blockSize; //!< Block size + uint32_t reserve2[11]; //!< Reserved for future use +} flexspi_nor_config_t; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif /* __EVKMIMXRT1050_FLEXSPI_NOR_CONFIG__ */ diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/flash_config.c b/ports/mimxrt/boards/MIMXRT1050_EVK/flash_config.c new file mode 100644 index 0000000000000..8203f4811ace1 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/flash_config.c @@ -0,0 +1,56 @@ +/* + * Copyright 2018 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "evkmimxrt1050_flexspi_nor_config.h" + +/* Component ID definition, used by tools. */ +#ifndef FSL_COMPONENT_ID +#define FSL_COMPONENT_ID "platform.drivers.xip_board" +#endif + +/******************************************************************************* + * Code + ******************************************************************************/ +#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1) +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.conf"))) +#elif defined(__ICCARM__) +#pragma location = ".boot_hdr.conf" +#endif + +const flexspi_nor_config_t qspiflash_config = { + .memConfig = + { + .tag = FLEXSPI_CFG_BLK_TAG, + .version = FLEXSPI_CFG_BLK_VERSION, + .readSampleClkSrc = kFlexSPIReadSampleClk_ExternalInputFromDqsPad, + .csHoldTime = 3u, + .csSetupTime = 3u, + .columnAddressWidth = 3u, + // Enable DDR mode, Wordaddassable, Safe configuration, Differential clock + .controllerMiscOption = + (1u << kFlexSpiMiscOffset_DdrModeEnable) | (1u << kFlexSpiMiscOffset_WordAddressableEnable) | + (1u << kFlexSpiMiscOffset_SafeConfigFreqEnable) | (1u << kFlexSpiMiscOffset_DiffClkEnable), + .sflashPadType = kSerialFlash_8Pads, + .serialClkFreq = kFlexSpiSerialClk_133MHz, + .sflashA1Size = 64u * 1024u * 1024u, + .dataValidTime = {16u, 16u}, + .lookupTable = + { + // Read LUTs + FLEXSPI_LUT_SEQ(CMD_DDR, FLEXSPI_8PAD, 0xA0, RADDR_DDR, FLEXSPI_8PAD, 0x18), + FLEXSPI_LUT_SEQ(CADDR_DDR, FLEXSPI_8PAD, 0x10, DUMMY_DDR, FLEXSPI_8PAD, 0x06), + FLEXSPI_LUT_SEQ(READ_DDR, FLEXSPI_8PAD, 0x04, STOP, FLEXSPI_1PAD, 0x0), + }, + }, + .pageSize = 512u, + .sectorSize = 256u * 1024u, + .blockSize = 256u * 1024u, + .isUniformBlockSize = true, +}; + +#endif /* XIP_BOOT_HEADER_ENABLE */ diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h new file mode 100644 index 0000000000000..5a733bbd97bb2 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h @@ -0,0 +1,9 @@ +#define MICROPY_HW_BOARD_NAME "i.MX RT1050 EVK" +#define MICROPY_HW_MCU_NAME "MIMXRT1052DVL6B" + +#define BOARD_FLASH_SIZE (64 * 1024 * 1024) + +// MIMXRT1050_EVK has 1 user LED +#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.mk new file mode 100644 index 0000000000000..61cca4d63f204 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.mk @@ -0,0 +1,7 @@ +MCU_SERIES = MIMXRT1052 +MCU_VARIANT = MIMXRT1052DVL6B + +JLINK_PATH ?= /media/RT1050-EVK/ + +deploy: $(BUILD)/firmware.bin + cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c new file mode 100644 index 0000000000000..d5da9c6f99275 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "pin.h" + +static pin_af_obj_t GPIO_AD_B0_09_af[] = { + PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), +}; + +pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h new file mode 100644 index 0000000000000..baef51c6c8612 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 NXP + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// NOTE: pins.h shall only be included in in pin.h +// hence no include guards are needed since they will be provided by pin.h + +extern pin_obj_t GPIO_AD_B0_09; diff --git a/ports/mimxrt/boards/MIMXRT1052.ld b/ports/mimxrt/boards/MIMXRT1052.ld new file mode 100644 index 0000000000000..836a119e5e471 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1052.ld @@ -0,0 +1,7 @@ +__stack_size__ = 0x6000; +_estack = __StackTop; +_sstack = __StackLimit; + +/* Use second OCRAM bank for GC heap. */ +_gc_heap_start = ORIGIN(m_data2); +_gc_heap_end = ORIGIN(m_data2) + LENGTH(m_data2); From 8610bababed3703f52c57f7e7b575c9d50be38a4 Mon Sep 17 00:00:00 2001 From: StereoRocker Date: Wed, 3 Feb 2021 22:59:36 +0000 Subject: [PATCH 0670/3533] rp2: Enable VfsFat class for FAT filesystem support. Allows interfacing with SD cards, for example. --- ports/rp2/CMakeLists.txt | 1 + ports/rp2/fatfs_port.c | 34 ++++++++++++++++++++++++++++++++++ ports/rp2/mpconfigport.h | 6 ++++++ 3 files changed, 41 insertions(+) create mode 100644 ports/rp2/fatfs_port.c diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index bd58d8f707952..cf20245a3c8ed 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -51,6 +51,7 @@ set(SOURCE_DRIVERS ) set(SOURCE_PORT + fatfs_port.c machine_adc.c machine_i2c.c machine_pin.c diff --git a/ports/rp2/fatfs_port.c b/ports/rp2/fatfs_port.c new file mode 100644 index 0000000000000..3821d6586c13f --- /dev/null +++ b/ports/rp2/fatfs_port.c @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "lib/oofatfs/ff.h" +#include "hardware/rtc.h" + +MP_WEAK DWORD get_fattime(void) { + datetime_t t; + rtc_get_datetime(&t); + return ((2000 + t.year - 1980) << 25) | ((t.month) << 21) | ((t.day) << 16) | ((t.hour) << 11) | ((t.min) << 5) | (t.sec / 2); +} diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index b3214acfc2b5d..69db4449d1a40 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -118,6 +118,12 @@ #define MICROPY_PY_FRAMEBUF (1) #define MICROPY_VFS (1) #define MICROPY_VFS_LFS2 (1) +#define MICROPY_VFS_FAT (1) + +// fatfs configuration +#define MICROPY_FATFS_ENABLE_LFN (1) +#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ +#define MICROPY_FATFS_RPATH (2) // Use VfsLfs2's types for fileio/textio #define mp_type_fileio mp_type_vfs_lfs2_fileio From 8ade163fffe9cca620ebe945dec8d25c54dc877d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 9 Feb 2021 15:50:21 +0100 Subject: [PATCH 0671/3533] rp2/machine_uart: Add timeout/timeout_char to read and write. --- ports/rp2/machine_uart.c | 59 +++++++++++++++++++++++++++++++++++----- ports/rp2/mpconfigport.h | 1 + 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 30446e688de34..6693cf73bf70d 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -54,6 +54,8 @@ typedef struct _machine_uart_obj_t { uint8_t stop; uint8_t tx; uint8_t rx; + uint16_t timeout; // timeout waiting for first char (in ms) + uint16_t timeout_char; // timeout waiting between chars (in ms) } machine_uart_obj_t; STATIC machine_uart_obj_t machine_uart_obj[] = { @@ -68,13 +70,13 @@ STATIC const char *_parity_name[] = {"None", "0", "1"}; STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d)", + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, timeout=%u, timeout_char=%u)", self->uart_id, self->baudrate, self->bits, _parity_name[self->parity], - self->stop, self->tx, self->rx); + self->stop, self->tx, self->rx, self->timeout, self->timeout_char); } STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx }; + enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_timeout, ARG_timeout_char }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, @@ -83,6 +85,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, { MP_QSTR_stop, MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, }; // Parse args. @@ -140,11 +144,28 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->rx = rx; } + // Set timeout if configured. + if (args[ARG_timeout].u_int >= 0) { + self->timeout = args[ARG_timeout].u_int; + } + + // Set timeout_char if configured. + if (args[ARG_timeout_char].u_int >= 0) { + self->timeout_char = args[ARG_timeout_char].u_int; + } + // Initialise the UART peripheral if any arguments given, or it was not initialised previously. if (n_args > 1 || n_kw > 0 || self->baudrate == 0) { if (self->baudrate == 0) { self->baudrate = DEFAULT_UART_BAUDRATE; } + + // Make sure timeout_char is at least as long as a whole character (13 bits to be safe). + uint32_t min_timeout_char = 13000 / self->baudrate + 1; + if (self->timeout_char < min_timeout_char) { + self->timeout_char = min_timeout_char; + } + uart_init(self->uart, self->baudrate); uart_set_format(self->uart, self->bits, self->stop, self->parity); uart_set_fifo_enabled(self->uart, true); @@ -184,26 +205,50 @@ STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_t STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - // TODO support timeout + uint64_t t = time_us_64() + (uint64_t)self->timeout * 1000; + uint64_t timeout_char_us = (uint64_t)self->timeout_char * 1000; uint8_t *dest = buf_in; - for (size_t i = 0; i < size; ++i) { + + for (size_t i = 0; i < size; i++) { + // Wait for the first/next character while (!uart_is_readable(self->uart)) { + if (time_us_64() > t) { // timed out + if (i <= 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } else { + return i; + } + } MICROPY_EVENT_POLL_HOOK } *dest++ = uart_get_hw(self->uart)->dr; + t = time_us_64() + timeout_char_us; } return size; } STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - // TODO support timeout + uint64_t t = time_us_64() + (uint64_t)self->timeout * 1000; + uint64_t timeout_char_us = (uint64_t)self->timeout_char * 1000; const uint8_t *src = buf_in; - for (size_t i = 0; i < size; ++i) { + + for (size_t i = 0; i < size; i++) { + // wait for the first/next character while (!uart_is_writable(self->uart)) { + if (time_us_64() > t) { // timed out + if (i <= 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } else { + return i; + } + } MICROPY_EVENT_POLL_HOOK } uart_get_hw(self->uart)->dr = *src++; + t = time_us_64() + timeout_char_us; } return size; } diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 69db4449d1a40..91d0c29b3ae0f 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -62,6 +62,7 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) From da85cb014afcf843bd0dfe99a1e232c271d3d5e8 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 10 Feb 2021 14:07:18 +0100 Subject: [PATCH 0672/3533] rp2/machine_uart: Add support for inverted TX and RX lines. Usage as in the other ports: keyword "invert" constants: INV_TX and INV_RX Sample: uart = UART(1, invert=UART.INV_TX | UART.INV_RX) --- ports/rp2/machine_uart.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 6693cf73bf70d..936b9a93e9869 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -44,6 +44,10 @@ #define IS_VALID_TX(uart, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(uart, pin)) #define IS_VALID_RX(uart, pin) (((pin) & 3) == 1 && IS_VALID_PERIPH(uart, pin)) +#define UART_INVERT_TX (1) +#define UART_INVERT_RX (2) +#define UART_INVERT_MASK (UART_INVERT_TX | UART_INVERT_RX) + typedef struct _machine_uart_obj_t { mp_obj_base_t base; uart_inst_t *const uart; @@ -56,27 +60,29 @@ typedef struct _machine_uart_obj_t { uint8_t rx; uint16_t timeout; // timeout waiting for first char (in ms) uint16_t timeout_char; // timeout waiting between chars (in ms) + uint8_t invert; } machine_uart_obj_t; STATIC machine_uart_obj_t machine_uart_obj[] = { - {{&machine_uart_type}, uart0, 0, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART0_TX, DEFAULT_UART0_RX}, - {{&machine_uart_type}, uart1, 1, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART1_TX, DEFAULT_UART1_RX}, + {{&machine_uart_type}, uart0, 0, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART0_TX, DEFAULT_UART0_RX, 0, 0, 0}, + {{&machine_uart_type}, uart1, 1, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART1_TX, DEFAULT_UART1_RX, 0, 0, 0}, }; STATIC const char *_parity_name[] = {"None", "0", "1"}; +STATIC const char *_invert_name[] = {"None", "INV_TX", "INV_RX", "INV_TX|INV_RX"}; /******************************************************************************/ // MicroPython bindings for UART STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, timeout=%u, timeout_char=%u)", + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, timeout=%u, timeout_char=%u, invert=%s)", self->uart_id, self->baudrate, self->bits, _parity_name[self->parity], - self->stop, self->tx, self->rx, self->timeout, self->timeout_char); + self->stop, self->tx, self->rx, self->timeout, self->timeout_char, _invert_name[self->invert]); } STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_timeout, ARG_timeout_char }; + enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_timeout, ARG_timeout_char, ARG_invert }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, @@ -87,6 +93,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_invert, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, }; // Parse args. @@ -154,6 +161,14 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->timeout_char = args[ARG_timeout_char].u_int; } + // Set line inversion if configured. + if (args[ARG_invert].u_int >= 0) { + if (args[ARG_invert].u_int & ~UART_INVERT_MASK) { + mp_raise_ValueError(MP_ERROR_TEXT("bad inversion mask")); + } + self->invert = args[ARG_invert].u_int; + } + // Initialise the UART peripheral if any arguments given, or it was not initialised previously. if (n_args > 1 || n_kw > 0 || self->baudrate == 0) { if (self->baudrate == 0) { @@ -171,6 +186,12 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, uart_set_fifo_enabled(self->uart, true); gpio_set_function(self->tx, GPIO_FUNC_UART); gpio_set_function(self->rx, GPIO_FUNC_UART); + if (self->invert & UART_INVERT_RX) { + gpio_set_inover(self->rx, GPIO_OVERRIDE_INVERT); + } + if (self->invert & UART_INVERT_TX) { + gpio_set_outover(self->tx, GPIO_OVERRIDE_INVERT); + } } return MP_OBJ_FROM_PTR(self); @@ -200,6 +221,10 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + + { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERT_TX) }, + { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERT_RX) }, + }; STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); From a075e0b7d87a66a4b2ee9f75bcf70887157c7ae2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 19 Feb 2021 17:11:04 +0100 Subject: [PATCH 0673/3533] rp2/rp2_pio: Allow more than 8 consecutive pins for PIO out/set/sideset. The bitmasks supplied for initialization of out/set/sideset were only 8 bit instead of 32. This resulted in an error, that not more than 8 consecutive pins would get initialized. Fixes issue #6933. --- ports/rp2/rp2_pio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 7d8921d0c3a51..6101164a1572e 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -139,8 +139,8 @@ enum { typedef struct _asm_pio_config_t { int8_t base; uint8_t count; - uint8_t pindirs; - uint8_t pinvals; + uint32_t pindirs; + uint32_t pinvals; } asm_pio_config_t; STATIC void asm_pio_override_shiftctrl(mp_obj_t arg, uint32_t bits, uint32_t lsb, pio_sm_config *config) { From 046164098337cc79dd0f6c710f49bdf6765aa711 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 3 Mar 2021 08:44:38 +0100 Subject: [PATCH 0674/3533] rp2/rp2_pio: Fix sm.get(buf) to not wait after getting last item. sm.get(buf) was waiting for one item more than the length of the supplied buffer. Even if this item was not stored, sm_get would block trying to get an item from the RX fifo. As part of the fix, the edge case for a zero length buffer was moved up to the section where the function arguments are handled. In case of a zero length buffer, sm.get() now returns immediately that buffer. --- ports/rp2/rp2_pio.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 6101164a1572e..9786e569d66c8 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -608,6 +608,9 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { } else { bufinfo.typecode |= 0x20; // make lowercase to support upper and lower } + if (bufinfo.len == 0) { // edge case: buffer of zero length supplied + return args[1]; + } } if (n_args > 2) { shift = mp_obj_get_int(args[2]); @@ -625,9 +628,6 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { if (dest == NULL) { return mp_obj_new_int_from_uint(value); } - if (dest >= dest_top) { - return args[1]; - } if (bufinfo.typecode == 'b') { *(uint8_t *)dest = value; dest += sizeof(uint8_t); @@ -640,6 +640,9 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { } else { mp_raise_ValueError("unsupported buffer type"); } + if (dest >= dest_top) { + return args[1]; + } } } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_get_obj, 1, 3, rp2_state_machine_get); From 11cf742524202fa5fc062f3e6e3040a82f49b190 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 20 Feb 2021 11:11:24 +0100 Subject: [PATCH 0675/3533] rp2/modmachine: Allow changing CPU clock frequency. Using the standard machine.freq(). The safe ranges tested were 10 and 12-270MHz, at which USB REPL still worked. Requested settings can be checked with the script: pico-sdk/src/rp2_common/hardware_clocks/scripts/vcocalc.py. At frequencies like 300MHz the script still signaled OK, but USB did not work any more. --- ports/rp2/modmachine.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index dbaafabe86ede..1393092818789 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -36,6 +36,7 @@ #include "hardware/clocks.h" #include "hardware/watchdog.h" #include "pico/bootrom.h" +#include "pico/stdlib.h" #include "pico/unique_id.h" #define RP2_RESET_PWRON (1) @@ -80,10 +81,18 @@ STATIC mp_obj_t machine_bootloader(void) { } MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader); -STATIC mp_obj_t machine_freq(void) { - return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys)); +STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys)); + } else { + mp_int_t freq = mp_obj_get_int(args[0]); + if (!set_sys_clock_khz(freq / 1000, false)) { + mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency")); + } + return mp_const_none; + } } -MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); STATIC mp_obj_t machine_idle(void) { best_effort_wfe_or_timeout(make_timeout_time_ms(1)); From c675452566ffcae1a49121b84a0233ed164b2152 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 4 Mar 2021 20:57:16 +0100 Subject: [PATCH 0676/3533] rp2/modmachine: Re-init UART for REPL on frequency change. When UART is used for REPL and the MCU frequency is changed, the UART has to be re-initialised. Besides that the UART may have to be recreated after a frequency change, but with USB REPL this is not a problem. Thanks to @HermannSW for spotting and providing the change. --- ports/rp2/modmachine.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 1393092818789..bf5033adbab7a 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -33,6 +33,7 @@ #include "extmod/machine_spi.h" #include "modmachine.h" +#include "uart.h" #include "hardware/clocks.h" #include "hardware/watchdog.h" #include "pico/bootrom.h" @@ -89,6 +90,10 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (!set_sys_clock_khz(freq / 1000, false)) { mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency")); } + #if MICROPY_HW_ENABLE_UART_REPL + setup_default_uart(); + mp_uart_init(); + #endif return mp_const_none; } } From b6489425c631bf4d6551c352dc17836923c9157b Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Thu, 11 Mar 2021 10:31:03 +0000 Subject: [PATCH 0677/3533] rp2/rp2_flash: Prevent MICROPY_HW_FLASH_STORAGE_BASE being set negative. --- ports/rp2/rp2_flash.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index cd1bc65489dfc..b89cb6fd8919a 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -42,6 +42,7 @@ #define MICROPY_HW_FLASH_STORAGE_BASE (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES) #endif +static_assert(MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big"); static_assert(MICROPY_HW_FLASH_STORAGE_BASE + MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big"); typedef struct _rp2_flash_obj_t { From a62e791978a66948637538d1a0a363fe35fa88e4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Mar 2021 18:34:33 +1100 Subject: [PATCH 0678/3533] lib/pico-sdk: Update to latest version 1.1.0. Signed-off-by: Damien George --- lib/pico-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pico-sdk b/lib/pico-sdk index 2d5789eca8965..fc10a97c386f6 160000 --- a/lib/pico-sdk +++ b/lib/pico-sdk @@ -1 +1 @@ -Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72 +Subproject commit fc10a97c386f65c1a44c68684fe52a56aaf50df0 From b24fcd7aec4b34064e9d9016d417d4ca3cc925cc Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Mar 2021 13:32:00 +1100 Subject: [PATCH 0679/3533] esp32/machine_hw_spi: Use default pins when making SPI if none given. The default pins can be optionally configured by a board. Fixes issue #6974. Signed-off-by: Damien George --- docs/esp32/quickref.rst | 1 + ports/esp32/machine_hw_spi.c | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 79e61a10b6dfd..30c9b3b95a9f9 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -302,6 +302,7 @@ has the same methods as software SPI above:: from machine import Pin, SPI + hspi = SPI(1, 10000000) hspi = SPI(1, 10000000, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) vspi = SPI(2, baudrate=80000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 3790b4e0cf142..d59f2c750e666 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -36,9 +36,29 @@ #include "driver/spi_master.h" +// Default pins for SPI(1), can be overridden by a board +#ifndef MICROPY_HW_SPI1_SCK +#define MICROPY_HW_SPI1_SCK (14) +#define MICROPY_HW_SPI1_MOSI (13) +#define MICROPY_HW_SPI1_MISO (12) +#endif + +// Default pins for SPI(2), can be overridden by a board +#ifndef MICROPY_HW_SPI2_SCK +#define MICROPY_HW_SPI2_SCK (18) +#define MICROPY_HW_SPI2_MOSI (23) +#define MICROPY_HW_SPI2_MISO (19) +#endif + #define MP_HW_SPI_MAX_XFER_BYTES (4092) #define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8 +typedef struct _machine_hw_spi_default_pins_t { + int8_t sck; + int8_t mosi; + int8_t miso; +} machine_hw_spi_default_pins_t; + typedef struct _machine_hw_spi_obj_t { mp_obj_base_t base; spi_host_device_t host; @@ -58,6 +78,12 @@ typedef struct _machine_hw_spi_obj_t { } state; } machine_hw_spi_obj_t; +// Default pin mappings for the hardware SPI instances +STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = { + { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO }, + { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO }, +}; + // Static objects mapping to HSPI and VSPI hardware peripherals STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2]; @@ -369,10 +395,13 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_ mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); machine_hw_spi_obj_t *self; + const machine_hw_spi_default_pins_t *default_pins; if (args[ARG_id].u_int == HSPI_HOST) { self = &machine_hw_spi_obj[0]; + default_pins = &machine_hw_spi_default_pins[0]; } else { self = &machine_hw_spi_obj[1]; + default_pins = &machine_hw_spi_default_pins[1]; } self->base.type = &machine_hw_spi_type; @@ -384,9 +413,9 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_ args[ARG_phase].u_int, args[ARG_bits].u_int, args[ARG_firstbit].u_int, - args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj), - args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj), - args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj)); + args[ARG_sck].u_obj == MP_OBJ_NULL ? default_pins->sck : machine_pin_get_id(args[ARG_sck].u_obj), + args[ARG_mosi].u_obj == MP_OBJ_NULL ? default_pins->mosi : machine_pin_get_id(args[ARG_mosi].u_obj), + args[ARG_miso].u_obj == MP_OBJ_NULL ? default_pins->miso : machine_pin_get_id(args[ARG_miso].u_obj)); return MP_OBJ_FROM_PTR(self); } From 6129b8e401c36cc68e0f7ba8180da27a40d17621 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Mar 2021 16:09:27 +1100 Subject: [PATCH 0680/3533] tests: Rename run-tests to run-tests.py for consistency. Signed-off-by: Damien George --- .github/workflows/ports_unix.yml | 22 +++++++++++----------- docs/develop/gettingstarted.rst | 2 +- docs/develop/writingtests.rst | 12 ++++++------ ports/cc3200/tools/smoke.py | 2 +- ports/javascript/Makefile | 4 ++-- ports/qemu-arm/Makefile.test | 2 +- ports/unix/Makefile | 16 ++++++++-------- ports/windows/.appveyor.yml | 14 +++++++------- ports/windows/README.md | 4 ++-- ports/zephyr/make-bin-testsuite | 4 ++-- tests/README | 6 +++--- tests/esp32/partition_ota.py | 2 +- tests/feature_check/README | 2 +- tests/jni/README | 2 +- tests/net_hosted/README | 2 +- tests/net_inet/README | 2 +- tests/run-internalbench.py | 2 +- tests/run-tests-exp.py | 4 ++-- tests/run-tests-exp.sh | 4 ++-- tests/{run-tests => run-tests.py} | 12 ++++++------ tools/ci.sh | 4 ++-- 21 files changed, 62 insertions(+), 62 deletions(-) rename tests/{run-tests => run-tests.py} (98%) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index eb1416045c03a..aa4b8abdc9893 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -24,7 +24,7 @@ jobs: run: source tools/ci.sh && ci_unix_minimal_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures reproducible: runs-on: ubuntu-latest @@ -49,7 +49,7 @@ jobs: run: source tools/ci.sh && ci_unix_standard_run_perfbench - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures coverage: runs-on: ubuntu-latest @@ -76,7 +76,7 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures coverage_32bit: runs-on: ubuntu-latest @@ -94,7 +94,7 @@ jobs: run: source tools/ci.sh && ci_unix_coverage_32bit_run_native_mpy_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures nanbox: runs-on: ubuntu-latest @@ -108,7 +108,7 @@ jobs: run: source tools/ci.sh && ci_unix_nanbox_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures float: runs-on: ubuntu-latest @@ -120,7 +120,7 @@ jobs: run: source tools/ci.sh && ci_unix_float_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures stackless_clang: runs-on: ubuntu-20.04 @@ -134,7 +134,7 @@ jobs: run: source tools/ci.sh && ci_unix_stackless_clang_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures float_clang: runs-on: ubuntu-20.04 @@ -148,7 +148,7 @@ jobs: run: source tools/ci.sh && ci_unix_float_clang_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures settrace: runs-on: ubuntu-latest @@ -160,7 +160,7 @@ jobs: run: source tools/ci.sh && ci_unix_settrace_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures settrace_stackless: runs-on: ubuntu-latest @@ -172,7 +172,7 @@ jobs: run: source tools/ci.sh && ci_unix_settrace_stackless_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures macos: runs-on: macos-11.0 @@ -185,4 +185,4 @@ jobs: run: source tools/ci.sh && ci_unix_macos_run_tests - name: Print failures if: failure() - run: tests/run-tests --print-failures + run: tests/run-tests.py --print-failures diff --git a/docs/develop/gettingstarted.rst b/docs/develop/gettingstarted.rst index 3dd00a579a2b7..32435ebe1c9d6 100644 --- a/docs/develop/gettingstarted.rst +++ b/docs/develop/gettingstarted.rst @@ -273,7 +273,7 @@ To run a selection of tests on a board/device connected over USB use: .. code-block:: bash $ cd tests - $ ./run-tests --target minimal --device /dev/ttyACM0 + $ ./run-tests.py --target minimal --device /dev/ttyACM0 See also :ref:`writingtests`. diff --git a/docs/develop/writingtests.rst b/docs/develop/writingtests.rst index 4bdf4dd7a620e..9bb5178f55f22 100644 --- a/docs/develop/writingtests.rst +++ b/docs/develop/writingtests.rst @@ -4,7 +4,7 @@ Writing tests ============= Tests in MicroPython are located at the path ``tests/``. The following is a listing of -key directories and the run-tests runner script: +key directories and the run-tests.py runner script: .. code-block:: bash @@ -13,7 +13,7 @@ key directories and the run-tests runner script: ├── extmod ├── float ├── micropython - ├── run-tests + ├── run-tests.py ... There are subfolders maintained to categorize the tests. Add a test by creating a new file in one of the @@ -54,17 +54,17 @@ The other way to run tests, which is useful when running on targets other than t .. code-block:: bash $ cd tests - $ ./run-tests + $ ./run-tests.py Then to run on a board: .. code-block:: bash - $ ./run-tests --target minimal --device /dev/ttyACM0 + $ ./run-tests.py --target minimal --device /dev/ttyACM0 And to run only a certain set of tests (eg a directory): .. code-block:: bash - $ ./run-tests -d basics - $ ./run-tests float/builtin*.py + $ ./run-tests.py -d basics + $ ./run-tests.py float/builtin*.py diff --git a/ports/cc3200/tools/smoke.py b/ports/cc3200/tools/smoke.py index 4ccc700c6cd76..463a485c4d4c8 100644 --- a/ports/cc3200/tools/smoke.py +++ b/ports/cc3200/tools/smoke.py @@ -6,7 +6,7 @@ """ Execute it like this: -python3 run-tests --target wipy --device 192.168.1.1 ../cc3200/tools/smoke.py +python3 run-tests.py --target wipy --device 192.168.1.1 ../cc3200/tools/smoke.py """ pin_map = [23, 24, 11, 12, 13, 14, 15, 16, 17, 22, 28, 10, 9, 8, 7, 6, 30, 31, 3, 0, 4, 5] diff --git a/ports/javascript/Makefile b/ports/javascript/Makefile index 4f9dd444befa0..3962e93f5f334 100644 --- a/ports/javascript/Makefile +++ b/ports/javascript/Makefile @@ -63,8 +63,8 @@ $(BUILD)/micropython.js: $(OBJ) library.js wrapper.js min: $(BUILD)/micropython.js uglifyjs $< -c -o $(BUILD)/micropython.min.js -test: $(BUILD)/micropython.js $(TOP)/tests/run-tests +test: $(BUILD)/micropython.js $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests + cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests.py include $(TOP)/py/mkrules.mk diff --git a/ports/qemu-arm/Makefile.test b/ports/qemu-arm/Makefile.test index 340e1c3b90d5e..6b7f1dc7ab46c 100644 --- a/ports/qemu-arm/Makefile.test +++ b/ports/qemu-arm/Makefile.test @@ -10,7 +10,7 @@ CFLAGS += -DTEST $(BUILD)/test_main.o: $(BUILD)/genhdr/tests.h $(BUILD)/genhdr/tests.h: - (cd $(TOP)/tests; ./run-tests --target=qemu-arm --write-exp) + (cd $(TOP)/tests; ./run-tests.py --target=qemu-arm --write-exp) $(Q)echo "Generating $@";(cd $(TOP)/tests; ../tools/tinytest-codegen.py) > $@ $(BUILD)/lib/tinytest/tinytest.o: CFLAGS += -DNO_FORKING diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 6a936a242545c..fe86018aa0ecc 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -292,17 +292,17 @@ include $(TOP)/py/mkrules.mk .PHONY: test test_full -test: $(PROG) $(TOP)/tests/run-tests +test: $(PROG) $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py -test_full: $(PROG) $(TOP)/tests/run-tests +test_full: $(PROG) $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests -d thread - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests --emit native - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) -d basics float micropython - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) --emit native -d basics float micropython + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py -d thread + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --emit native + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) -d basics float micropython + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) --emit native -d basics float micropython cat $(TOP)/tests/basics/0prelim.py | ./$(PROG) | grep -q 'abc' test_gcov: test_full diff --git a/ports/windows/.appveyor.yml b/ports/windows/.appveyor.yml index 4d2d6bd11dc87..1ec72bbb38937 100644 --- a/ports/windows/.appveyor.yml +++ b/ports/windows/.appveyor.yml @@ -37,14 +37,14 @@ build: test_script: - ps: | cd (Join-Path $env:APPVEYOR_BUILD_FOLDER 'tests') - & $env:MICROPY_CPYTHON3 run-tests + & $env:MICROPY_CPYTHON3 run-tests.py if ($LASTEXITCODE -ne 0) { - & $env:MICROPY_CPYTHON3 run-tests --print-failures + & $env:MICROPY_CPYTHON3 run-tests.py --print-failures throw "Test failure" } - & $env:MICROPY_CPYTHON3 run-tests --via-mpy -d basics float micropython + & $env:MICROPY_CPYTHON3 run-tests.py --via-mpy -d basics float micropython if ($LASTEXITCODE -ne 0) { - & $env:MICROPY_CPYTHON3 run-tests --print-failures + & $env:MICROPY_CPYTHON3 run-tests.py --print-failures throw "Test failure" } @@ -71,17 +71,17 @@ after_test: throw "$env:MSYSTEM mpy_cross build exited with code $LASTEXITCODE" } cd (Join-Path $env:APPVEYOR_BUILD_FOLDER 'tests') - $testArgs = @('run-tests') + $testArgs = @('run-tests.py') foreach ($skipTest in @('math_fun', 'float2int_double', 'float_parse', 'math_domain_special')) { $testArgs = $testArgs + '-e' + $skipTest } & $env:MICROPY_CPYTHON3 $testArgs if ($LASTEXITCODE -ne 0) { - & $env:MICROPY_CPYTHON3 run-tests --print-failures + & $env:MICROPY_CPYTHON3 run-tests.py --print-failures throw "Test failure" } & $env:MICROPY_CPYTHON3 ($testArgs + @('--via-mpy', '-d', 'basics', 'float', 'micropython')) if ($LASTEXITCODE -ne 0) { - & $env:MICROPY_CPYTHON3 run-tests --print-failures + & $env:MICROPY_CPYTHON3 run-tests.py --print-failures throw "Test failure" } diff --git a/ports/windows/README.md b/ports/windows/README.md index 1b904f8f5e60e..553e87513e737 100644 --- a/ports/windows/README.md +++ b/ports/windows/README.md @@ -90,11 +90,11 @@ Running the tests This is similar for all ports: cd ../../tests - python ./run-tests + python ./run-tests.py Though when running on Cygwin and using Cygwin's Python installation you'll need: - python3 ./run-tests + python3 ./run-tests.py Depending on the combination of platform and Python version used it might be needed to first set the MICROPY_MICROPYTHON environment variable to diff --git a/ports/zephyr/make-bin-testsuite b/ports/zephyr/make-bin-testsuite index f6aeb83f8a28a..9a8c329772143 100755 --- a/ports/zephyr/make-bin-testsuite +++ b/ports/zephyr/make-bin-testsuite @@ -8,8 +8,8 @@ # ./make-bin-testsuite BOARD=qemu_cortex_m3 run # -(cd ../../tests; ./run-tests --write-exp) -(cd ../../tests; ./run-tests --list-tests --target=minimal \ +(cd ../../tests; ./run-tests.py --write-exp) +(cd ../../tests; ./run-tests.py --list-tests --target=minimal \ -e async -e intbig -e int_big -e builtin_help -e memstats -e bytes_compare3 -e class_reverse_op \ -e /set -e frozenset -e complex -e const -e native -e viper \ -e 'float_divmod\.' -e float_parse_doubleprec -e float/true_value -e float/types \ diff --git a/tests/README b/tests/README index f2cd89bb932a3..8794202e15fde 100644 --- a/tests/README +++ b/tests/README @@ -1,15 +1,15 @@ This directory contains tests for various functionality areas of MicroPython. -To run all stable tests, run "run-tests" script in this directory. +To run all stable tests, run "run-tests.py" script in this directory. Tests of capabilities not supported on all platforms should be written to check for the capability being present. If it is not, the test should merely output 'SKIP' followed by the line terminator, and call sys.exit() to raise SystemExit, instead of attempting to test the -missing capability. The testing framework (run-tests in this +missing capability. The testing framework (run-tests.py in this directory, test_main.c in qemu_arm) recognizes this as a skipped test. There are a few features for which this mechanism cannot be used to -condition a test. The run-tests script uses small scripts in the +condition a test. The run-tests.py script uses small scripts in the feature_check directory to check whether each such feature is present, and skips the relevant tests if not. diff --git a/tests/esp32/partition_ota.py b/tests/esp32/partition_ota.py index 765630c8ce393..212fcf03385e7 100644 --- a/tests/esp32/partition_ota.py +++ b/tests/esp32/partition_ota.py @@ -95,7 +95,7 @@ def copy_partition(src, dest): if sz - addr < 4096: blk = blk[: sz - addr] if addr & 0xFFFF == 0: - # need to show progress to run-tests else it times out + # need to show progress to run-tests.py else it times out print(" ... 0x{:06x}".format(addr)) src.readblocks(addr >> 12, blk) dest.writeblocks(addr >> 12, blk) diff --git a/tests/feature_check/README b/tests/feature_check/README index d062020f7bb32..3b7b6cba41749 100644 --- a/tests/feature_check/README +++ b/tests/feature_check/README @@ -1,4 +1,4 @@ This directory doesn't contain real tests, but code snippets to detect various interpreter features, which can't be/inconvenient to detecte by -other means. Scripts here are executed by run-tests at the beginning of +other means. Scripts here are executed by run-tests.py at the beginning of testsuite to decide what other test groups to run/exclude. diff --git a/tests/jni/README b/tests/jni/README index 8418bb84f442f..f1e1b0eca0beb 100644 --- a/tests/jni/README +++ b/tests/jni/README @@ -8,5 +8,5 @@ of JVM. For example, for OpenJDK 7 on x86_64, following may work: -LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./run-tests jni/*.py +LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./run-tests.py jni/*.py diff --git a/tests/net_hosted/README b/tests/net_hosted/README index 724dd61584a5e..709ed50ce329e 100644 --- a/tests/net_hosted/README +++ b/tests/net_hosted/README @@ -8,4 +8,4 @@ not yet fully correspond to the functional specification above. So far, these tests are not run as part of the main testsuite and need to be run seperately (from the main test/ directory): - ./run-tests net_hosted/*.py + ./run-tests.py net_hosted/*.py diff --git a/tests/net_inet/README b/tests/net_inet/README index 9a5614efa6429..1d6e15b174076 100644 --- a/tests/net_inet/README +++ b/tests/net_inet/README @@ -2,4 +2,4 @@ This directory contains network tests which require Internet connection. Note that these tests are not run as part of the main testsuite and need to be run seperately (from the main test/ directory): - ./run-tests net_inet/*.py + ./run-tests.py net_inet/*.py diff --git a/tests/run-internalbench.py b/tests/run-internalbench.py index 63392814a893f..606fc3b77201e 100755 --- a/tests/run-internalbench.py +++ b/tests/run-internalbench.py @@ -67,7 +67,7 @@ def main(): cmd_parser.add_argument("files", nargs="*", help="input test files") args = cmd_parser.parse_args() - # Note pyboard support is copied over from run-tests, not testes, and likely needs revamping + # Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping if args.pyboard: import pyboard diff --git a/tests/run-tests-exp.py b/tests/run-tests-exp.py index ac32fe9869c36..21b6645336f1f 100644 --- a/tests/run-tests-exp.py +++ b/tests/run-tests-exp.py @@ -1,6 +1,6 @@ # -# This is minimal MicroPython variant of run-tests script, which uses -# .exp files as generated by run-tests --write-exp. It is useful to run +# This is minimal MicroPython variant of run-tests.py script, which uses +# .exp files as generated by run-tests.py --write-exp. It is useful to run # testsuite on systems which have neither CPython3 nor unix shell. # This script is intended to be run by the same interpreter executable # which is to be tested, so should use minimal language functionality. diff --git a/tests/run-tests-exp.sh b/tests/run-tests-exp.sh index 8f81b96d2fe89..177090cd8db93 100755 --- a/tests/run-tests-exp.sh +++ b/tests/run-tests-exp.sh @@ -1,7 +1,7 @@ #!/bin/sh # -# This is plain shell variant of run-tests script, which uses .exp files -# as generated by run-tests --write-exp. It is useful to run testsuite +# This is plain shell variant of run-tests.py script, which uses .exp files +# as generated by run-tests.py --write-exp. It is useful to run testsuite # on embedded systems which don't have CPython3. # diff --git a/tests/run-tests b/tests/run-tests.py similarity index 98% rename from tests/run-tests rename to tests/run-tests.py index b733d8a9fc582..5863b23b41399 100755 --- a/tests/run-tests +++ b/tests/run-tests.py @@ -258,7 +258,7 @@ def run_tests(pyb, tests, args, result_dir): if not (args.list_tests or args.write_exp): # Even if we run completely different tests in a different directory, # we need to access feature_checks from the same directory as the - # run-tests script itself so use base_path. + # run-tests.py script itself so use base_path. # Check if micropython.native is supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, 'native_check.py') @@ -582,19 +582,19 @@ def main(): specified test files. If test files nor directories are specified, the script expects to be ran in the tests directory (where this file is located) and the builtin tests suitable for the target platform are ran. -When running tests, run-tests compares the MicroPython output of the test with the output +When running tests, run-tests.py compares the MicroPython output of the test with the output produced by running the test through CPython unless a .exp file is found, in which case it is used as comparison. -If a test fails, run-tests produces a pair of .out and .exp files in the result +If a test fails, run-tests.py produces a pair of .out and .exp files in the result directory with the MicroPython output and the expectations, respectively. ''', epilog='''\ Options -i and -e can be multiple and processed in the order given. Regex "search" (vs "match") operation is used. An action (include/exclude) of the last matching regex is used: - run-tests -i async - exclude all, then include tests containing "async" anywhere - run-tests -e '/big.+int' - include all, then exclude by regex - run-tests -e async -i async_foo - include all, exclude async, yet still include async_foo + run-tests.py -i async - exclude all, then include tests containing "async" anywhere + run-tests.py -e '/big.+int' - include all, then exclude by regex + run-tests.py -e async -i async_foo - include all, exclude async, yet still include async_foo ''') cmd_parser.add_argument('--target', default='unix', help='the target platform') cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device or the IP address of the pyboard') diff --git a/tools/ci.sh b/tools/ci.sh index a815e9483e58c..53cf7f878b24c 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -306,7 +306,7 @@ function ci_unix_minimal_build { } function ci_unix_minimal_run_tests { - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests -e exception_chain -e self_type_check -e subclass_native_init -d basics) + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests.py -e exception_chain -e self_type_check -e subclass_native_init -d basics) } function ci_unix_standard_build { @@ -441,7 +441,7 @@ function ci_unix_macos_run_tests { # - OSX has poor time resolution and these uasyncio tests do not have correct output # - import_pkg7 has a problem with relative imports # - urandom_basic has a problem with getrandbits(0) - (cd tests && ./run-tests --exclude 'uasyncio_(basic|heaplock|lock|wait_task)' --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') + (cd tests && ./run-tests.py --exclude 'uasyncio_(basic|heaplock|lock|wait_task)' --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') } ######################################################################################## From 2a38d7103672580882fb621a5b76e8d26805d593 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Mar 2021 16:11:27 +1100 Subject: [PATCH 0681/3533] tests/run-tests.py: Reformat with Black. Signed-off-by: Damien George --- tests/run-tests.py | 582 +++++++++++++++++++++++++++------------------ 1 file changed, 352 insertions(+), 230 deletions(-) diff --git a/tests/run-tests.py b/tests/run-tests.py index 5863b23b41399..3ab6194b628fe 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -13,31 +13,34 @@ # are guaranteed to always work, this one should though. BASEPATH = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda: None))) + def base_path(*p): - return os.path.abspath(os.path.join(BASEPATH, *p)).replace('\\', '/') + return os.path.abspath(os.path.join(BASEPATH, *p)).replace("\\", "/") + # Tests require at least CPython 3.3. If your default python3 executable # is of lower version, you can point MICROPY_CPYTHON3 environment var # to the correct executable. -if os.name == 'nt': - CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/windows/micropython.exe')) +if os.name == "nt": + CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", base_path("../ports/windows/micropython.exe")) else: - CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') - MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', base_path('../ports/unix/micropython')) + CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", base_path("../ports/unix/micropython")) # Use CPython options to not save .pyc files, to only access the core standard library # (not site packages which may clash with u-module names), and improve start up time. CPYTHON3_CMD = [CPYTHON3, "-BS"] # mpy-cross is only needed if --via-mpy command-line arg is passed -MPYCROSS = os.getenv('MICROPY_MPYCROSS', base_path('../mpy-cross/mpy-cross')) +MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/mpy-cross")) # For diff'ing test output -DIFF = os.getenv('MICROPY_DIFF', 'diff -u') +DIFF = os.getenv("MICROPY_DIFF", "diff -u") # Set PYTHONIOENCODING so that CPython will use utf-8 on systems which set another encoding in the locale -os.environ['PYTHONIOENCODING'] = 'utf-8' +os.environ["PYTHONIOENCODING"] = "utf-8" + def rm_f(fname): if os.path.exists(fname): @@ -48,57 +51,62 @@ def rm_f(fname): def convert_regex_escapes(line): cs = [] escape = False - for c in str(line, 'utf8'): + for c in str(line, "utf8"): if escape: escape = False cs.append(c) - elif c == '\\': + elif c == "\\": escape = True - elif c in ('(', ')', '[', ']', '{', '}', '.', '*', '+', '^', '$'): - cs.append('\\' + c) + elif c in ("(", ")", "[", "]", "{", "}", ".", "*", "+", "^", "$"): + cs.append("\\" + c) else: cs.append(c) # accept carriage-return(s) before final newline - if cs[-1] == '\n': - cs[-1] = '\r*\n' - return bytes(''.join(cs), 'utf8') + if cs[-1] == "\n": + cs[-1] = "\r*\n" + return bytes("".join(cs), "utf8") def run_micropython(pyb, args, test_file, is_special=False): special_tests = ( - 'micropython/meminfo.py', 'basics/bytes_compare3.py', - 'basics/builtin_help.py', 'thread/thread_exc2.py', - 'esp32/partition_ota.py', + "micropython/meminfo.py", + "basics/bytes_compare3.py", + "basics/builtin_help.py", + "thread/thread_exc2.py", + "esp32/partition_ota.py", ) had_crash = False if pyb is None: # run on PC - if test_file.startswith(('cmdline/', base_path('feature_check/'))) or test_file in special_tests: + if ( + test_file.startswith(("cmdline/", base_path("feature_check/"))) + or test_file in special_tests + ): # special handling for tests of the unix cmdline program is_special = True if is_special: # check for any cmdline options needed for this test args = [MICROPYTHON] - with open(test_file, 'rb') as f: + with open(test_file, "rb") as f: line = f.readline() - if line.startswith(b'# cmdline:'): + if line.startswith(b"# cmdline:"): # subprocess.check_output on Windows only accepts strings, not bytes - args += [str(c, 'utf-8') for c in line[10:].strip().split()] + args += [str(c, "utf-8") for c in line[10:].strip().split()] # run the test, possibly with redirected input try: - if 'repl_' in test_file: + if "repl_" in test_file: # Need to use a PTY to test command line editing try: import pty except ImportError: # in case pty module is not available, like on Windows - return b'SKIP\n' + return b"SKIP\n" import select def get(required=False): - rv = b'' + rv = b"" while True: ready = select.select([master], [], [], 0.02) if ready[0] == [master]: @@ -111,14 +119,15 @@ def send_get(what): os.write(master, what) return get() - with open(test_file, 'rb') as f: + with open(test_file, "rb") as f: # instead of: output_mupy = subprocess.check_output(args, stdin=f) master, slave = pty.openpty() - p = subprocess.Popen(args, stdin=slave, stdout=slave, - stderr=subprocess.STDOUT, bufsize=0) + p = subprocess.Popen( + args, stdin=slave, stdout=slave, stderr=subprocess.STDOUT, bufsize=0 + ) banner = get(True) - output_mupy = banner + b''.join(send_get(line) for line in f) - send_get(b'\x04') # exit the REPL, so coverage info is saved + output_mupy = banner + b"".join(send_get(line) for line in f) + send_get(b"\x04") # exit the REPL, so coverage info is saved # At this point the process might have exited already, but trying to # kill it 'again' normally doesn't result in exceptions as Python and/or # the OS seem to try to handle this nicely. When running Linux on WSL @@ -132,22 +141,28 @@ def send_get(what): os.close(master) os.close(slave) else: - output_mupy = subprocess.check_output(args + [test_file], stderr=subprocess.STDOUT) + output_mupy = subprocess.check_output( + args + [test_file], stderr=subprocess.STDOUT + ) except subprocess.CalledProcessError: - return b'CRASH' + return b"CRASH" else: # a standard test run on PC # create system command - cmdlist = [MICROPYTHON, '-X', 'emit=' + args.emit] + cmdlist = [MICROPYTHON, "-X", "emit=" + args.emit] if args.heapsize is not None: - cmdlist.extend(['-X', 'heapsize=' + args.heapsize]) + cmdlist.extend(["-X", "heapsize=" + args.heapsize]) # if running via .mpy, first compile the .py file if args.via_mpy: - subprocess.check_output([MPYCROSS] + args.mpy_cross_flags.split() + ['-o', 'mpytest.mpy', '-X', 'emit=' + args.emit, test_file]) - cmdlist.extend(['-m', 'mpytest']) + subprocess.check_output( + [MPYCROSS] + + args.mpy_cross_flags.split() + + ["-o", "mpytest.mpy", "-X", "emit=" + args.emit, test_file] + ) + cmdlist.extend(["-m", "mpytest"]) else: cmdlist.append(test_file) @@ -156,11 +171,11 @@ def send_get(what): output_mupy = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as er: had_crash = True - output_mupy = er.output + b'CRASH' + output_mupy = er.output + b"CRASH" # clean up if we had an intermediate .mpy file if args.via_mpy: - rm_f('mpytest.mpy') + rm_f("mpytest.mpy") else: # run on pyboard @@ -169,56 +184,58 @@ def send_get(what): output_mupy = pyb.execfile(test_file) except pyboard.PyboardError as e: had_crash = True - if not is_special and e.args[0] == 'exception': - output_mupy = e.args[1] + e.args[2] + b'CRASH' + if not is_special and e.args[0] == "exception": + output_mupy = e.args[1] + e.args[2] + b"CRASH" else: - output_mupy = b'CRASH' + output_mupy = b"CRASH" # canonical form for all ports/platforms is to use \n for end-of-line - output_mupy = output_mupy.replace(b'\r\n', b'\n') + output_mupy = output_mupy.replace(b"\r\n", b"\n") # don't try to convert the output if we should skip this test - if had_crash or output_mupy in (b'SKIP\n', b'CRASH'): + if had_crash or output_mupy in (b"SKIP\n", b"CRASH"): return output_mupy if is_special or test_file in special_tests: # convert parts of the output that are not stable across runs - with open(test_file + '.exp', 'rb') as f: + with open(test_file + ".exp", "rb") as f: lines_exp = [] for line in f.readlines(): - if line == b'########\n': + if line == b"########\n": line = (line,) else: line = (line, re.compile(convert_regex_escapes(line))) lines_exp.append(line) - lines_mupy = [line + b'\n' for line in output_mupy.split(b'\n')] - if output_mupy.endswith(b'\n'): - lines_mupy = lines_mupy[:-1] # remove erroneous last empty line + lines_mupy = [line + b"\n" for line in output_mupy.split(b"\n")] + if output_mupy.endswith(b"\n"): + lines_mupy = lines_mupy[:-1] # remove erroneous last empty line i_mupy = 0 for i in range(len(lines_exp)): - if lines_exp[i][0] == b'########\n': + if lines_exp[i][0] == b"########\n": # 8x #'s means match 0 or more whole lines line_exp = lines_exp[i + 1] skip = 0 - while i_mupy + skip < len(lines_mupy) and not line_exp[1].match(lines_mupy[i_mupy + skip]): + while i_mupy + skip < len(lines_mupy) and not line_exp[1].match( + lines_mupy[i_mupy + skip] + ): skip += 1 if i_mupy + skip >= len(lines_mupy): - lines_mupy[i_mupy] = b'######## FAIL\n' + lines_mupy[i_mupy] = b"######## FAIL\n" break - del lines_mupy[i_mupy:i_mupy + skip] - lines_mupy.insert(i_mupy, b'########\n') + del lines_mupy[i_mupy : i_mupy + skip] + lines_mupy.insert(i_mupy, b"########\n") i_mupy += 1 else: # a regex if lines_exp[i][1].match(lines_mupy[i_mupy]): lines_mupy[i_mupy] = lines_exp[i][0] else: - #print("don't match: %r %s" % (lines_exp[i][1], lines_mupy[i_mupy])) # DEBUG + # print("don't match: %r %s" % (lines_exp[i][1], lines_mupy[i_mupy])) # DEBUG pass i_mupy += 1 if i_mupy >= len(lines_mupy): break - output_mupy = b''.join(lines_mupy) + output_mupy = b"".join(lines_mupy) return output_mupy @@ -261,193 +278,220 @@ def run_tests(pyb, tests, args, result_dir): # run-tests.py script itself so use base_path. # Check if micropython.native is supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'native_check.py') - if output == b'CRASH': + output = run_feature_check(pyb, args, base_path, "native_check.py") + if output == b"CRASH": skip_native = True # Check if arbitrary-precision integers are supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'int_big.py') - if output != b'1000000000000000000000000000000000000000000000\n': + output = run_feature_check(pyb, args, base_path, "int_big.py") + if output != b"1000000000000000000000000000000000000000000000\n": skip_int_big = True # Check if bytearray is supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'bytearray.py') - if output != b'bytearray\n': + output = run_feature_check(pyb, args, base_path, "bytearray.py") + if output != b"bytearray\n": skip_bytearray = True # Check if set type (and set literals) is supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'set_check.py') - if output == b'CRASH': + output = run_feature_check(pyb, args, base_path, "set_check.py") + if output == b"CRASH": skip_set_type = True # Check if slice is supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'slice.py') - if output != b'slice\n': + output = run_feature_check(pyb, args, base_path, "slice.py") + if output != b"slice\n": skip_slice = True # Check if async/await keywords are supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'async_check.py') - if output == b'CRASH': + output = run_feature_check(pyb, args, base_path, "async_check.py") + if output == b"CRASH": skip_async = True # Check if const keyword (MicroPython extension) is supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'const.py') - if output == b'CRASH': + output = run_feature_check(pyb, args, base_path, "const.py") + if output == b"CRASH": skip_const = True # Check if __rOP__ special methods are supported, and skip such tests if it's not - output = run_feature_check(pyb, args, base_path, 'reverse_ops.py') - if output == b'TypeError\n': + output = run_feature_check(pyb, args, base_path, "reverse_ops.py") + if output == b"TypeError\n": skip_revops = True # Check if uio module exists, and skip such tests if it doesn't - output = run_feature_check(pyb, args, base_path, 'uio_module.py') - if output != b'uio\n': + output = run_feature_check(pyb, args, base_path, "uio_module.py") + if output != b"uio\n": skip_io_module = True # Check if emacs repl is supported, and skip such tests if it's not - t = run_feature_check(pyb, args, base_path, 'repl_emacs_check.py') - if 'True' not in str(t, 'ascii'): - skip_tests.add('cmdline/repl_emacs_keys.py') + t = run_feature_check(pyb, args, base_path, "repl_emacs_check.py") + if "True" not in str(t, "ascii"): + skip_tests.add("cmdline/repl_emacs_keys.py") # Check if words movement in repl is supported, and skip such tests if it's not - t = run_feature_check(pyb, args, base_path, 'repl_words_move_check.py') - if 'True' not in str(t, 'ascii'): - skip_tests.add('cmdline/repl_words_move.py') + t = run_feature_check(pyb, args, base_path, "repl_words_move_check.py") + if "True" not in str(t, "ascii"): + skip_tests.add("cmdline/repl_words_move.py") - upy_byteorder = run_feature_check(pyb, args, base_path, 'byteorder.py') - upy_float_precision = run_feature_check(pyb, args, base_path, 'float.py') - if upy_float_precision == b'CRASH': + upy_byteorder = run_feature_check(pyb, args, base_path, "byteorder.py") + upy_float_precision = run_feature_check(pyb, args, base_path, "float.py") + if upy_float_precision == b"CRASH": upy_float_precision = 0 else: upy_float_precision = int(upy_float_precision) - has_complex = run_feature_check(pyb, args, base_path, 'complex.py') == b'complex\n' - has_coverage = run_feature_check(pyb, args, base_path, 'coverage.py') == b'coverage\n' - cpy_byteorder = subprocess.check_output(CPYTHON3_CMD + [base_path('feature_check/byteorder.py')]) - skip_endian = (upy_byteorder != cpy_byteorder) + has_complex = run_feature_check(pyb, args, base_path, "complex.py") == b"complex\n" + has_coverage = run_feature_check(pyb, args, base_path, "coverage.py") == b"coverage\n" + cpy_byteorder = subprocess.check_output( + CPYTHON3_CMD + [base_path("feature_check/byteorder.py")] + ) + skip_endian = upy_byteorder != cpy_byteorder # These tests don't test slice explicitly but rather use it to perform the test misc_slice_tests = ( - 'builtin_range', - 'class_super', - 'containment', - 'errno1', - 'fun_str', - 'generator1', - 'globals_del', - 'memoryview1', - 'memoryview_gc', - 'object1', - 'python34', - 'struct_endian', + "builtin_range", + "class_super", + "containment", + "errno1", + "fun_str", + "generator1", + "globals_del", + "memoryview1", + "memoryview_gc", + "object1", + "python34", + "struct_endian", ) # Some tests shouldn't be run on GitHub Actions - if os.getenv('GITHUB_ACTIONS') == 'true': - skip_tests.add('thread/stress_schedule.py') # has reliability issues + if os.getenv("GITHUB_ACTIONS") == "true": + skip_tests.add("thread/stress_schedule.py") # has reliability issues if upy_float_precision == 0: - skip_tests.add('extmod/uctypes_le_float.py') - skip_tests.add('extmod/uctypes_native_float.py') - skip_tests.add('extmod/uctypes_sizeof_float.py') - skip_tests.add('extmod/ujson_dumps_float.py') - skip_tests.add('extmod/ujson_loads_float.py') - skip_tests.add('extmod/urandom_extra_float.py') - skip_tests.add('misc/rge_sm.py') + skip_tests.add("extmod/uctypes_le_float.py") + skip_tests.add("extmod/uctypes_native_float.py") + skip_tests.add("extmod/uctypes_sizeof_float.py") + skip_tests.add("extmod/ujson_dumps_float.py") + skip_tests.add("extmod/ujson_loads_float.py") + skip_tests.add("extmod/urandom_extra_float.py") + skip_tests.add("misc/rge_sm.py") if upy_float_precision < 32: - skip_tests.add('float/float2int_intbig.py') # requires fp32, there's float2int_fp30_intbig.py instead - skip_tests.add('float/string_format.py') # requires fp32, there's string_format_fp30.py instead - skip_tests.add('float/bytes_construct.py') # requires fp32 - skip_tests.add('float/bytearray_construct.py') # requires fp32 + skip_tests.add( + "float/float2int_intbig.py" + ) # requires fp32, there's float2int_fp30_intbig.py instead + skip_tests.add( + "float/string_format.py" + ) # requires fp32, there's string_format_fp30.py instead + skip_tests.add("float/bytes_construct.py") # requires fp32 + skip_tests.add("float/bytearray_construct.py") # requires fp32 if upy_float_precision < 64: - skip_tests.add('float/float_divmod.py') # tested by float/float_divmod_relaxed.py instead - skip_tests.add('float/float2int_doubleprec_intbig.py') - skip_tests.add('float/float_parse_doubleprec.py') + skip_tests.add("float/float_divmod.py") # tested by float/float_divmod_relaxed.py instead + skip_tests.add("float/float2int_doubleprec_intbig.py") + skip_tests.add("float/float_parse_doubleprec.py") if not has_complex: - skip_tests.add('float/complex1.py') - skip_tests.add('float/complex1_intbig.py') - skip_tests.add('float/complex_special_methods.py') - skip_tests.add('float/int_big_float.py') - skip_tests.add('float/true_value.py') - skip_tests.add('float/types.py') + skip_tests.add("float/complex1.py") + skip_tests.add("float/complex1_intbig.py") + skip_tests.add("float/complex_special_methods.py") + skip_tests.add("float/int_big_float.py") + skip_tests.add("float/true_value.py") + skip_tests.add("float/types.py") if not has_coverage: - skip_tests.add('cmdline/cmd_parsetree.py') + skip_tests.add("cmdline/cmd_parsetree.py") # Some tests shouldn't be run on a PC - if args.target == 'unix': + if args.target == "unix": # unix build does not have the GIL so can't run thread mutation tests for t in tests: - if t.startswith('thread/mutate_'): + if t.startswith("thread/mutate_"): skip_tests.add(t) # Some tests shouldn't be run on pyboard - if args.target != 'unix': - skip_tests.add('basics/exception_chain.py') # warning is not printed - skip_tests.add('micropython/meminfo.py') # output is very different to PC output - skip_tests.add('extmod/machine_mem.py') # raw memory access not supported - - if args.target == 'wipy': - skip_tests.add('misc/print_exception.py') # requires error reporting full - skip_tests.update({'extmod/uctypes_%s.py' % t for t in 'bytearray le native_le ptr_le ptr_native_le sizeof sizeof_native array_assign_le array_assign_native_le'.split()}) # requires uctypes - skip_tests.add('extmod/zlibd_decompress.py') # requires zlib - skip_tests.add('extmod/uheapq1.py') # uheapq not supported by WiPy - skip_tests.add('extmod/urandom_basic.py') # requires urandom - skip_tests.add('extmod/urandom_extra.py') # requires urandom - elif args.target == 'esp8266': - skip_tests.add('misc/rge_sm.py') # too large - elif args.target == 'minimal': - skip_tests.add('basics/class_inplace_op.py') # all special methods not supported - skip_tests.add('basics/subclass_native_init.py')# native subclassing corner cases not support - skip_tests.add('misc/rge_sm.py') # too large - skip_tests.add('micropython/opt_level.py') # don't assume line numbers are stored - elif args.target == 'nrf': - skip_tests.add('basics/memoryview1.py') # no item assignment for memoryview - skip_tests.add('extmod/urandom_basic.py') # unimplemented: urandom.seed - skip_tests.add('micropython/opt_level.py') # no support for line numbers - skip_tests.add('misc/non_compliant.py') # no item assignment for bytearray + if args.target != "unix": + skip_tests.add("basics/exception_chain.py") # warning is not printed + skip_tests.add("micropython/meminfo.py") # output is very different to PC output + skip_tests.add("extmod/machine_mem.py") # raw memory access not supported + + if args.target == "wipy": + skip_tests.add("misc/print_exception.py") # requires error reporting full + skip_tests.update( + { + "extmod/uctypes_%s.py" % t + for t in "bytearray le native_le ptr_le ptr_native_le sizeof sizeof_native array_assign_le array_assign_native_le".split() + } + ) # requires uctypes + skip_tests.add("extmod/zlibd_decompress.py") # requires zlib + skip_tests.add("extmod/uheapq1.py") # uheapq not supported by WiPy + skip_tests.add("extmod/urandom_basic.py") # requires urandom + skip_tests.add("extmod/urandom_extra.py") # requires urandom + elif args.target == "esp8266": + skip_tests.add("misc/rge_sm.py") # too large + elif args.target == "minimal": + skip_tests.add("basics/class_inplace_op.py") # all special methods not supported + skip_tests.add( + "basics/subclass_native_init.py" + ) # native subclassing corner cases not support + skip_tests.add("misc/rge_sm.py") # too large + skip_tests.add("micropython/opt_level.py") # don't assume line numbers are stored + elif args.target == "nrf": + skip_tests.add("basics/memoryview1.py") # no item assignment for memoryview + skip_tests.add("extmod/urandom_basic.py") # unimplemented: urandom.seed + skip_tests.add("micropython/opt_level.py") # no support for line numbers + skip_tests.add("misc/non_compliant.py") # no item assignment for bytearray for t in tests: - if t.startswith('basics/io_'): + if t.startswith("basics/io_"): skip_tests.add(t) - elif args.target == 'qemu-arm': - skip_tests.add('misc/print_exception.py') # requires sys stdfiles + elif args.target == "qemu-arm": + skip_tests.add("misc/print_exception.py") # requires sys stdfiles # Some tests are known to fail on 64-bit machines - if pyb is None and platform.architecture()[0] == '64bit': + if pyb is None and platform.architecture()[0] == "64bit": pass # Some tests use unsupported features on Windows - if os.name == 'nt': - skip_tests.add('import/import_file.py') # works but CPython prints forward slashes + if os.name == "nt": + skip_tests.add("import/import_file.py") # works but CPython prints forward slashes # Some tests are known to fail with native emitter # Remove them from the below when they work - if args.emit == 'native': - skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from_close generator_name'.split()}) # require raise_varargs, generator name - skip_tests.update({'basics/async_%s.py' % t for t in 'with with2 with_break with_return'.split()}) # require async_with - skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs - skip_tests.add('basics/annotate_var.py') # requires checking for unbound local - skip_tests.add('basics/del_deref.py') # requires checking for unbound local - skip_tests.add('basics/del_local.py') # requires checking for unbound local - skip_tests.add('basics/exception_chain.py') # raise from is not supported - skip_tests.add('basics/scope_implicit.py') # requires checking for unbound local - skip_tests.add('basics/try_finally_return2.py') # requires raise_varargs - skip_tests.add('basics/unboundlocal.py') # requires checking for unbound local - skip_tests.add('extmod/uasyncio_event.py') # unknown issue - skip_tests.add('extmod/uasyncio_lock.py') # requires async with - skip_tests.add('extmod/uasyncio_micropython.py') # unknown issue - skip_tests.add('extmod/uasyncio_wait_for.py') # unknown issue - skip_tests.add('misc/features.py') # requires raise_varargs - skip_tests.add('misc/print_exception.py') # because native doesn't have proper traceback info - skip_tests.add('misc/sys_exc_info.py') # sys.exc_info() is not supported for native - skip_tests.add('micropython/emg_exc.py') # because native doesn't have proper traceback info - skip_tests.add('micropython/heapalloc_traceback.py') # because native doesn't have proper traceback info - skip_tests.add('micropython/opt_level_lineno.py') # native doesn't have proper traceback info - skip_tests.add('micropython/schedule.py') # native code doesn't check pending events + if args.emit == "native": + skip_tests.update( + {"basics/%s.py" % t for t in "gen_yield_from_close generator_name".split()} + ) # require raise_varargs, generator name + skip_tests.update( + {"basics/async_%s.py" % t for t in "with with2 with_break with_return".split()} + ) # require async_with + skip_tests.update( + {"basics/%s.py" % t for t in "try_reraise try_reraise2".split()} + ) # require raise_varargs + skip_tests.add("basics/annotate_var.py") # requires checking for unbound local + skip_tests.add("basics/del_deref.py") # requires checking for unbound local + skip_tests.add("basics/del_local.py") # requires checking for unbound local + skip_tests.add("basics/exception_chain.py") # raise from is not supported + skip_tests.add("basics/scope_implicit.py") # requires checking for unbound local + skip_tests.add("basics/try_finally_return2.py") # requires raise_varargs + skip_tests.add("basics/unboundlocal.py") # requires checking for unbound local + skip_tests.add("extmod/uasyncio_event.py") # unknown issue + skip_tests.add("extmod/uasyncio_lock.py") # requires async with + skip_tests.add("extmod/uasyncio_micropython.py") # unknown issue + skip_tests.add("extmod/uasyncio_wait_for.py") # unknown issue + skip_tests.add("misc/features.py") # requires raise_varargs + skip_tests.add( + "misc/print_exception.py" + ) # because native doesn't have proper traceback info + skip_tests.add("misc/sys_exc_info.py") # sys.exc_info() is not supported for native + skip_tests.add( + "micropython/emg_exc.py" + ) # because native doesn't have proper traceback info + skip_tests.add( + "micropython/heapalloc_traceback.py" + ) # because native doesn't have proper traceback info + skip_tests.add( + "micropython/opt_level_lineno.py" + ) # native doesn't have proper traceback info + skip_tests.add("micropython/schedule.py") # native code doesn't check pending events for test_file in tests: - test_file = test_file.replace('\\', '/') + test_file = test_file.replace("\\", "/") if args.filters: # Default verdict is the opposit of the first action @@ -458,9 +502,13 @@ def run_tests(pyb, tests, args, result_dir): if verdict == "exclude": continue - test_basename = test_file.replace('..', '_').replace('./', '').replace('/', '_') + test_basename = test_file.replace("..", "_").replace("./", "").replace("/", "_") test_name = os.path.splitext(os.path.basename(test_file))[0] - is_native = test_name.startswith("native_") or test_name.startswith("viper_") or args.emit == "native" + is_native = ( + test_name.startswith("native_") + or test_name.startswith("viper_") + or args.emit == "native" + ) is_endian = test_name.endswith("_endian") is_int_big = test_name.startswith("int_big") or test_name.endswith("_intbig") is_bytearray = test_name.startswith("bytearray") or test_name.endswith("_bytearray") @@ -493,23 +541,23 @@ def run_tests(pyb, tests, args, result_dir): continue # get expected output - test_file_expected = test_file + '.exp' + test_file_expected = test_file + ".exp" if os.path.isfile(test_file_expected): # expected output given by a file, so read that in - with open(test_file_expected, 'rb') as f: + with open(test_file_expected, "rb") as f: output_expected = f.read() else: # run CPython to work out expected output try: output_expected = subprocess.check_output(CPYTHON3_CMD + [test_file]) if args.write_exp: - with open(test_file_expected, 'wb') as f: + with open(test_file_expected, "wb") as f: f.write(output_expected) except subprocess.CalledProcessError: - output_expected = b'CPYTHON3 CRASH' + output_expected = b"CPYTHON3 CRASH" # canonical form for all host platforms is to use \n for end-of-line - output_expected = output_expected.replace(b'\r\n', b'\n') + output_expected = output_expected.replace(b"\r\n", b"\n") if args.write_exp: continue @@ -517,7 +565,7 @@ def run_tests(pyb, tests, args, result_dir): # run MicroPython output_mupy = run_micropython(pyb, args, test_file) - if output_mupy == b'SKIP\n': + if output_mupy == b"SKIP\n": print("skip ", test_file) skipped_tests.append(test_name) continue @@ -549,9 +597,9 @@ def run_tests(pyb, tests, args, result_dir): print("{} tests passed".format(passed_count)) if len(skipped_tests) > 0: - print("{} tests skipped: {}".format(len(skipped_tests), ' '.join(skipped_tests))) + print("{} tests skipped: {}".format(len(skipped_tests), " ".join(skipped_tests))) if len(failed_tests) > 0: - print("{} tests failed: {}".format(len(failed_tests), ' '.join(failed_tests))) + print("{} tests failed: {}".format(len(failed_tests), " ".join(failed_tests))) return False # all tests succeeded @@ -559,7 +607,6 @@ def run_tests(pyb, tests, args, result_dir): class append_filter(argparse.Action): - def __init__(self, option_strings, dest, **kwargs): super().__init__(option_strings, dest, default=[], **kwargs) @@ -576,7 +623,7 @@ def __call__(self, parser, args, value, option): def main(): cmd_parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, - description='''Run and manage tests for MicroPython. + description="""Run and manage tests for MicroPython. Tests are discovered by scanning test directories for .py files or using the specified test files. If test files nor directories are specified, the script @@ -587,34 +634,81 @@ def main(): case it is used as comparison. If a test fails, run-tests.py produces a pair of .out and .exp files in the result directory with the MicroPython output and the expectations, respectively. -''', - epilog='''\ +""", + epilog="""\ Options -i and -e can be multiple and processed in the order given. Regex "search" (vs "match") operation is used. An action (include/exclude) of the last matching regex is used: run-tests.py -i async - exclude all, then include tests containing "async" anywhere run-tests.py -e '/big.+int' - include all, then exclude by regex run-tests.py -e async -i async_foo - include all, exclude async, yet still include async_foo -''') - cmd_parser.add_argument('--target', default='unix', help='the target platform') - cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device or the IP address of the pyboard') - cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device') - cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username') - cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password') - cmd_parser.add_argument('-d', '--test-dirs', nargs='*', help='input test directories (if no files given)') - cmd_parser.add_argument('-r', '--result-dir', default=base_path('results'), help='directory for test results') - cmd_parser.add_argument('-e', '--exclude', action=append_filter, metavar='REGEX', dest='filters', help='exclude test by regex on path/name.py') - cmd_parser.add_argument('-i', '--include', action=append_filter, metavar='REGEX', dest='filters', help='include test by regex on path/name.py') - cmd_parser.add_argument('--write-exp', action='store_true', help='use CPython to generate .exp files to run tests w/o CPython') - cmd_parser.add_argument('--list-tests', action='store_true', help='list tests instead of running them') - cmd_parser.add_argument('--emit', default='bytecode', help='MicroPython emitter to use (bytecode or native)') - cmd_parser.add_argument('--heapsize', help='heapsize to use (use default if not specified)') - cmd_parser.add_argument('--via-mpy', action='store_true', help='compile .py files to .mpy first') - cmd_parser.add_argument('--mpy-cross-flags', default='-mcache-lookup-bc', help='flags to pass to mpy-cross') - cmd_parser.add_argument('--keep-path', action='store_true', help='do not clear MICROPYPATH when running tests') - cmd_parser.add_argument('files', nargs='*', help='input test files') - cmd_parser.add_argument('--print-failures', action='store_true', help='print the diff of expected vs. actual output for failed tests and exit') - cmd_parser.add_argument('--clean-failures', action='store_true', help='delete the .exp and .out files from failed tests and exit') +""", + ) + cmd_parser.add_argument("--target", default="unix", help="the target platform") + cmd_parser.add_argument( + "--device", + default="/dev/ttyACM0", + help="the serial device or the IP address of the pyboard", + ) + cmd_parser.add_argument( + "-b", "--baudrate", default=115200, help="the baud rate of the serial device" + ) + cmd_parser.add_argument("-u", "--user", default="micro", help="the telnet login username") + cmd_parser.add_argument("-p", "--password", default="python", help="the telnet login password") + cmd_parser.add_argument( + "-d", "--test-dirs", nargs="*", help="input test directories (if no files given)" + ) + cmd_parser.add_argument( + "-r", "--result-dir", default=base_path("results"), help="directory for test results" + ) + cmd_parser.add_argument( + "-e", + "--exclude", + action=append_filter, + metavar="REGEX", + dest="filters", + help="exclude test by regex on path/name.py", + ) + cmd_parser.add_argument( + "-i", + "--include", + action=append_filter, + metavar="REGEX", + dest="filters", + help="include test by regex on path/name.py", + ) + cmd_parser.add_argument( + "--write-exp", + action="store_true", + help="use CPython to generate .exp files to run tests w/o CPython", + ) + cmd_parser.add_argument( + "--list-tests", action="store_true", help="list tests instead of running them" + ) + cmd_parser.add_argument( + "--emit", default="bytecode", help="MicroPython emitter to use (bytecode or native)" + ) + cmd_parser.add_argument("--heapsize", help="heapsize to use (use default if not specified)") + cmd_parser.add_argument( + "--via-mpy", action="store_true", help="compile .py files to .mpy first" + ) + cmd_parser.add_argument( + "--mpy-cross-flags", default="-mcache-lookup-bc", help="flags to pass to mpy-cross" + ) + cmd_parser.add_argument( + "--keep-path", action="store_true", help="do not clear MICROPYPATH when running tests" + ) + cmd_parser.add_argument("files", nargs="*", help="input test files") + cmd_parser.add_argument( + "--print-failures", + action="store_true", + help="print the diff of expected vs. actual output for failed tests and exit", + ) + cmd_parser.add_argument( + "--clean-failures", + action="store_true", + help="delete the .exp and .out files from failed tests and exit", + ) args = cmd_parser.parse_args() if args.print_failures: @@ -627,55 +721,82 @@ def main(): sys.exit(0) if args.clean_failures: - for f in glob(os.path.join(args.result_dir, "*.exp")) + glob(os.path.join(args.result_dir, "*.out")): + for f in glob(os.path.join(args.result_dir, "*.exp")) + glob( + os.path.join(args.result_dir, "*.out") + ): os.remove(f) sys.exit(0) - LOCAL_TARGETS = ('unix', 'qemu-arm',) - EXTERNAL_TARGETS = ('pyboard', 'wipy', 'esp8266', 'esp32', 'minimal', 'nrf') + LOCAL_TARGETS = ( + "unix", + "qemu-arm", + ) + EXTERNAL_TARGETS = ("pyboard", "wipy", "esp8266", "esp32", "minimal", "nrf") if args.target in LOCAL_TARGETS or args.list_tests: pyb = None elif args.target in EXTERNAL_TARGETS: global pyboard - sys.path.append(base_path('../tools')) + sys.path.append(base_path("../tools")) import pyboard + pyb = pyboard.Pyboard(args.device, args.baudrate, args.user, args.password) pyb.enter_raw_repl() else: - raise ValueError('target must be one of %s' % ", ".join(LOCAL_TARGETS + EXTERNAL_TARGETS)) + raise ValueError("target must be one of %s" % ", ".join(LOCAL_TARGETS + EXTERNAL_TARGETS)) if len(args.files) == 0: if args.test_dirs is None: - test_dirs = ('basics', 'micropython', 'misc', 'extmod',) - if args.target == 'pyboard': + test_dirs = ( + "basics", + "micropython", + "misc", + "extmod", + ) + if args.target == "pyboard": # run pyboard tests - test_dirs += ('float', 'stress', 'pyb', 'pybnative', 'inlineasm') - elif args.target in ('esp8266', 'esp32', 'minimal', 'nrf'): - test_dirs += ('float',) - elif args.target == 'wipy': + test_dirs += ("float", "stress", "pyb", "pybnative", "inlineasm") + elif args.target in ("esp8266", "esp32", "minimal", "nrf"): + test_dirs += ("float",) + elif args.target == "wipy": # run WiPy tests - test_dirs += ('wipy',) - elif args.target == 'unix': + test_dirs += ("wipy",) + elif args.target == "unix": # run PC tests - test_dirs += ('float', 'import', 'io', 'stress', 'unicode', 'unix', 'cmdline',) - elif args.target == 'qemu-arm': + test_dirs += ( + "float", + "import", + "io", + "stress", + "unicode", + "unix", + "cmdline", + ) + elif args.target == "qemu-arm": if not args.write_exp: - raise ValueError('--target=qemu-arm must be used with --write-exp') + raise ValueError("--target=qemu-arm must be used with --write-exp") # Generate expected output files for qemu run. # This list should match the test_dirs tuple in tinytest-codegen.py. - test_dirs += ('float', 'inlineasm', 'qemu-arm',) + test_dirs += ( + "float", + "inlineasm", + "qemu-arm", + ) else: # run tests from these directories test_dirs = args.test_dirs - tests = sorted(test_file for test_files in (glob('{}/*.py'.format(dir)) for dir in test_dirs) for test_file in test_files) + tests = sorted( + test_file + for test_files in (glob("{}/*.py".format(dir)) for dir in test_dirs) + for test_file in test_files + ) else: # tests explicitly given tests = args.files if not args.keep_path: # clear search path to make sure tests use only builtin modules and those in extmod - os.environ['MICROPYPATH'] = os.pathsep + base_path('../extmod') + os.environ["MICROPYPATH"] = os.pathsep + base_path("../extmod") try: os.makedirs(args.result_dir, exist_ok=True) @@ -687,5 +808,6 @@ def main(): if not res: sys.exit(1) + if __name__ == "__main__": main() From e98ff3f08e966595fd84d83296f84962ee3d8d94 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Mar 2021 19:50:13 +1100 Subject: [PATCH 0682/3533] tests/multi_bluetooth: Skip tests when BLE features are unsupported. Signed-off-by: Damien George --- tests/multi_bluetooth/ble_gap_pair.py | 4 ++++ tests/multi_bluetooth/ble_gap_pair_bond.py | 4 ++++ tests/multi_bluetooth/ble_l2cap.py | 4 ++++ tests/multi_bluetooth/perf_l2cap.py | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/tests/multi_bluetooth/ble_gap_pair.py b/tests/multi_bluetooth/ble_gap_pair.py index 728513405ee79..f10fa37074010 100644 --- a/tests/multi_bluetooth/ble_gap_pair.py +++ b/tests/multi_bluetooth/ble_gap_pair.py @@ -4,6 +4,10 @@ from micropython import const import time, machine, bluetooth +if not hasattr(bluetooth.BLE, "gap_pair"): + print("SKIP") + raise SystemExit + TIMEOUT_MS = 4000 _IRQ_CENTRAL_CONNECT = const(1) diff --git a/tests/multi_bluetooth/ble_gap_pair_bond.py b/tests/multi_bluetooth/ble_gap_pair_bond.py index a29c217887b45..d7224cc127b1f 100644 --- a/tests/multi_bluetooth/ble_gap_pair_bond.py +++ b/tests/multi_bluetooth/ble_gap_pair_bond.py @@ -5,6 +5,10 @@ from micropython import const import time, machine, bluetooth +if not hasattr(bluetooth.BLE, "gap_pair"): + print("SKIP") + raise SystemExit + TIMEOUT_MS = 4000 _IRQ_CENTRAL_CONNECT = const(1) diff --git a/tests/multi_bluetooth/ble_l2cap.py b/tests/multi_bluetooth/ble_l2cap.py index a26f59b3ef0f9..b14eeffcc50db 100644 --- a/tests/multi_bluetooth/ble_l2cap.py +++ b/tests/multi_bluetooth/ble_l2cap.py @@ -7,6 +7,10 @@ from micropython import const import time, machine, bluetooth, random +if not hasattr(bluetooth.BLE, "l2cap_connect"): + print("SKIP") + raise SystemExit + TIMEOUT_MS = 1000 _IRQ_CENTRAL_CONNECT = const(1) diff --git a/tests/multi_bluetooth/perf_l2cap.py b/tests/multi_bluetooth/perf_l2cap.py index 9b07bb1dcef2f..0603a02a81dc2 100644 --- a/tests/multi_bluetooth/perf_l2cap.py +++ b/tests/multi_bluetooth/perf_l2cap.py @@ -3,6 +3,10 @@ from micropython import const import time, machine, bluetooth, random +if not hasattr(bluetooth.BLE, "l2cap_connect"): + print("SKIP") + raise SystemExit + TIMEOUT_MS = 1000 _IRQ_CENTRAL_CONNECT = const(1) From af45d511f190454159fe39ba50c477d7bbd6bb5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20K=C3=B6ck?= Date: Sat, 13 Mar 2021 09:17:02 +0100 Subject: [PATCH 0683/3533] rp2: Enable uerrno module. Fixes #6991. --- ports/rp2/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 91d0c29b3ae0f..35d1645522588 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -88,6 +88,7 @@ #define MICROPY_PY_SYS_STDFILES (1) #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_GIL (0) From 8010b15968dece822aa5318888cb6d969f36ef3b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Mar 2021 00:17:22 +1100 Subject: [PATCH 0684/3533] rp2: Enabled more core Python features. This brings the port's configuration closer to the stm32 and esp32 ports. Signed-off-by: Damien George --- ports/rp2/mpconfigport.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 35d1645522588..13db589ec0e3e 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -71,17 +71,33 @@ // Fine control over Python builtins, classes, modules, etc #define MICROPY_PY_FUNCTION_ATTRS (1) +#define MICROPY_PY_DESCRIPTORS (1) +#define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) +#define MICROPY_PY_BUILTINS_STR_CENTER (1) +#define MICROPY_PY_BUILTINS_STR_PARTITION (1) +#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) +#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) +#define MICROPY_PY_BUILTINS_SLICE_INDICES (1) +#define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_ROUND_INT (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) +#define MICROPY_PY_BUILTINS_COMPILE (1) #define MICROPY_PY_BUILTINS_INPUT (1) +#define MICROPY_PY_BUILTINS_POW3 (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT rp2_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (1) -#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) -#define MICROPY_PY___FILE__ (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#define MICROPY_PY_COLLECTIONS_DEQUE (1) +#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1) +#define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1) +#define MICROPY_PY_MATH_FACTORIAL (1) +#define MICROPY_PY_MATH_ISCLOSE (1) +#define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO_IOBASE (1) #define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_SYS_MAXSIZE (1) From dcaf702578045299d5bc007d81669fe1ba640654 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Mar 2021 00:18:18 +1100 Subject: [PATCH 0685/3533] rp2/modmachine: Enable machine.Signal class. Fixes issue #6863. Signed-off-by: Damien George --- ports/rp2/modmachine.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index bf5033adbab7a..cd7dcaade59bd 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -30,6 +30,7 @@ #include "extmod/machine_i2c.h" #include "extmod/machine_mem.h" #include "extmod/machine_pulse.h" +#include "extmod/machine_signal.h" #include "extmod/machine_spi.h" #include "modmachine.h" @@ -163,6 +164,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, + { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, From eccd73a403aef5b41368752b1d11d95e005bdc4b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Mar 2021 15:51:28 +1100 Subject: [PATCH 0686/3533] extmod/extmod.cmake: Add modonewire.c to MICROPY_SOURCE_EXTMOD list. Signed-off-by: Damien George --- extmod/extmod.cmake | 1 + ports/esp32/main/CMakeLists.txt | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 6668a2d8aa3cc..691c3ce9b3fea 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -12,6 +12,7 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/modbluetooth.c ${MICROPY_EXTMOD_DIR}/modbtree.c ${MICROPY_EXTMOD_DIR}/modframebuf.c + ${MICROPY_EXTMOD_DIR}/modonewire.c ${MICROPY_EXTMOD_DIR}/moduasyncio.c ${MICROPY_EXTMOD_DIR}/modubinascii.c ${MICROPY_EXTMOD_DIR}/moducryptolib.c diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index bd25d76ee45b0..b6bccdde3ca9b 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -9,10 +9,6 @@ set(MICROPY_QSTRDEFS_PORT ${PROJECT_DIR}/qstrdefsport.h ) -set(MICROPY_SOURCE_EXTMOD_EXTRA - ${MICROPY_DIR}/extmod/modonewire.c -) - set(MICROPY_SOURCE_LIB ${MICROPY_DIR}/lib/littlefs/lfs1.c ${MICROPY_DIR}/lib/littlefs/lfs1_util.c @@ -75,7 +71,6 @@ set(MICROPY_SOURCE_PORT set(MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_PY} ${MICROPY_SOURCE_EXTMOD} - ${MICROPY_SOURCE_EXTMOD_EXTRA} ${MICROPY_SOURCE_LIB} ${MICROPY_SOURCE_PORT} ) @@ -129,7 +124,6 @@ idf_component_register( SRCS ${MICROPY_SOURCE_PY} ${MICROPY_SOURCE_EXTMOD} - ${MICROPY_SOURCE_EXTMOD_EXTRA} ${MICROPY_SOURCE_LIB} ${MICROPY_SOURCE_DRIVERS} ${MICROPY_SOURCE_PORT} From a9140ab09b7f2b878972a86dabc12d00ddaa463e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 14 Mar 2021 15:52:33 +1100 Subject: [PATCH 0687/3533] rp2: Use core-provided cmake fragments instead of custom ones. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 194 ++++++++++++++++++++------------- ports/rp2/micropy_extmod.cmake | 40 ------- ports/rp2/micropy_py.cmake | 134 ----------------------- ports/rp2/micropy_rules.cmake | 90 --------------- 4 files changed, 121 insertions(+), 337 deletions(-) delete mode 100644 ports/rp2/micropy_extmod.cmake delete mode 100644 ports/rp2/micropy_py.cmake delete mode 100644 ports/rp2/micropy_rules.cmake diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index cf20245a3c8ed..3968194dcbbfe 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -6,8 +6,8 @@ if(NOT CMAKE_BUILD_TYPE) endif() # Set main target and component locations -set(MICROPYTHON_TARGET firmware) -get_filename_component(MPY_DIR "../.." ABSOLUTE) +set(MICROPY_TARGET firmware) +get_filename_component(MICROPY_DIR "../.." ABSOLUTE) if (PICO_SDK_PATH_OVERRIDE) set(PICO_SDK_PATH ${PICO_SDK_PATH_OVERRIDE}) else() @@ -15,42 +15,46 @@ else() endif() # Use the local tinyusb instead of the one in pico-sdk -set(PICO_TINYUSB_PATH ${MPY_DIR}/lib/tinyusb) +set(PICO_TINYUSB_PATH ${MICROPY_DIR}/lib/tinyusb) # Include component cmake fragments -include(micropy_py.cmake) -include(micropy_extmod.cmake) +include(${MICROPY_DIR}/py/py.cmake) +include(${MICROPY_DIR}/extmod/extmod.cmake) include(${PICO_SDK_PATH}/pico_sdk_init.cmake) # Define the top-level project -project(${MICROPYTHON_TARGET}) +project(${MICROPY_TARGET}) pico_sdk_init() -add_executable(${MICROPYTHON_TARGET}) - -set(SOURCE_LIB - ${MPY_DIR}/lib/littlefs/lfs1.c - ${MPY_DIR}/lib/littlefs/lfs1_util.c - ${MPY_DIR}/lib/littlefs/lfs2.c - ${MPY_DIR}/lib/littlefs/lfs2_util.c - ${MPY_DIR}/lib/mp-readline/readline.c - ${MPY_DIR}/lib/oofatfs/ff.c - ${MPY_DIR}/lib/oofatfs/ffunicode.c - ${MPY_DIR}/lib/timeutils/timeutils.c - ${MPY_DIR}/lib/utils/gchelper_m0.s - ${MPY_DIR}/lib/utils/gchelper_native.c - ${MPY_DIR}/lib/utils/mpirq.c - ${MPY_DIR}/lib/utils/stdout_helpers.c - ${MPY_DIR}/lib/utils/sys_stdio_mphal.c - ${MPY_DIR}/lib/utils/pyexec.c +add_executable(${MICROPY_TARGET}) + +set(MICROPY_QSTRDEFS_PORT + ${PROJECT_SOURCE_DIR}/qstrdefsport.h +) + +set(MICROPY_SOURCE_LIB + ${MICROPY_DIR}/lib/littlefs/lfs1.c + ${MICROPY_DIR}/lib/littlefs/lfs1_util.c + ${MICROPY_DIR}/lib/littlefs/lfs2.c + ${MICROPY_DIR}/lib/littlefs/lfs2_util.c + ${MICROPY_DIR}/lib/mp-readline/readline.c + ${MICROPY_DIR}/lib/oofatfs/ff.c + ${MICROPY_DIR}/lib/oofatfs/ffunicode.c + ${MICROPY_DIR}/lib/timeutils/timeutils.c + ${MICROPY_DIR}/lib/utils/gchelper_m0.s + ${MICROPY_DIR}/lib/utils/gchelper_native.c + ${MICROPY_DIR}/lib/utils/mpirq.c + ${MICROPY_DIR}/lib/utils/pyexec.c + ${MICROPY_DIR}/lib/utils/stdout_helpers.c + ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c ) -set(SOURCE_DRIVERS - ${MPY_DIR}/drivers/bus/softspi.c +set(MICROPY_SOURCE_DRIVERS + ${MICROPY_DIR}/drivers/bus/softspi.c ) -set(SOURCE_PORT +set(MICROPY_SOURCE_PORT fatfs_port.c machine_adc.c machine_i2c.c @@ -73,11 +77,11 @@ set(SOURCE_PORT uart.c ) -set(SOURCE_QSTR - ${SOURCE_PY} - ${SOURCE_EXTMOD} - ${MPY_DIR}/lib/utils/mpirq.c - ${MPY_DIR}/lib/utils/sys_stdio_mphal.c +set(MICROPY_SOURCE_QSTR + ${MICROPY_SOURCE_PY} + ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_DIR}/lib/utils/mpirq.c + ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c ${PROJECT_SOURCE_DIR}/machine_adc.c ${PROJECT_SOURCE_DIR}/machine_i2c.c ${PROJECT_SOURCE_DIR}/machine_pin.c @@ -94,37 +98,65 @@ set(SOURCE_QSTR ${PROJECT_SOURCE_DIR}/rp2_pio.c ) -set(MPY_QSTR_DEFS ${PROJECT_SOURCE_DIR}/qstrdefsport.h) +set(PICO_SDK_COMPONENTS + hardware_adc + hardware_base + hardware_clocks + hardware_dma + hardware_flash + hardware_gpio + hardware_i2c + hardware_irq + hardware_pio + hardware_pwm + hardware_regs + hardware_rtc + hardware_spi + hardware_structs + hardware_sync + hardware_timer + hardware_uart + hardware_watchdog + pico_base_headers + pico_binary_info + pico_bootrom + pico_multicore + pico_platform + pico_stdio + pico_stdlib + pico_sync + pico_time + pico_unique_id + tinyusb_device +) # Define mpy-cross flags and frozen manifest -set(MPY_CROSS_FLAGS -march=armv7m) -set(FROZEN_MANIFEST ${PROJECT_SOURCE_DIR}/manifest.py) - -include(micropy_rules.cmake) - -target_sources(${MICROPYTHON_TARGET} PRIVATE - ${SOURCE_PY} - ${SOURCE_EXTMOD} - ${SOURCE_LIB} - ${SOURCE_DRIVERS} - ${SOURCE_PORT} +set(MICROPY_CROSS_FLAGS -march=armv7m) +set(MICROPY_FROZEN_MANIFEST ${PROJECT_SOURCE_DIR}/manifest.py) + +target_sources(${MICROPY_TARGET} PRIVATE + ${MICROPY_SOURCE_PY} + ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_LIB} + ${MICROPY_SOURCE_DRIVERS} + ${MICROPY_SOURCE_PORT} ) -target_include_directories(${MICROPYTHON_TARGET} PRIVATE - "${PROJECT_SOURCE_DIR}" - "${MPY_DIR}" - "${CMAKE_BINARY_DIR}" - ) +target_include_directories(${MICROPY_TARGET} PRIVATE + "${PROJECT_SOURCE_DIR}" + "${MICROPY_DIR}" + "${CMAKE_BINARY_DIR}" +) -target_compile_options(${MICROPYTHON_TARGET} PRIVATE +target_compile_options(${MICROPY_TARGET} PRIVATE -Wall - #-Werror - -DFFCONF_H=\"${MPY_DIR}/lib/oofatfs/ffconf.h\" - -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT - -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT + -Werror ) -target_compile_definitions(${MICROPYTHON_TARGET} PRIVATE +target_compile_definitions(${MICROPY_TARGET} PRIVATE + FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\" + LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT + LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT PICO_FLOAT_PROPAGATE_NANS=1 PICO_STACK_SIZE=0x2000 PICO_CORE1_STACK_SIZE=0 @@ -134,34 +166,50 @@ target_compile_definitions(${MICROPYTHON_TARGET} PRIVATE PICO_NO_BI_STDIO_UART=1 # we call it UART REPL ) -target_link_libraries(${MICROPYTHON_TARGET} - hardware_adc - hardware_dma - hardware_flash - hardware_i2c - hardware_pio - hardware_pwm - hardware_rtc - hardware_spi - hardware_sync - pico_multicore - pico_stdlib_headers - pico_stdlib - pico_unique_id - tinyusb_device +target_link_libraries(${MICROPY_TARGET} + ${PICO_SDK_COMPONENTS} ) # todo this is a bit brittle, but we want to move a few source files into RAM (which requires # a linker script modification) until we explicitly add macro calls around the function # defs to move them into RAM. if (PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM) - pico_set_linker_script(${MICROPYTHON_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp.ld) + pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp.ld) endif() -pico_add_extra_outputs(${MICROPYTHON_TARGET}) +pico_add_extra_outputs(${MICROPY_TARGET}) -add_custom_command(TARGET ${MICROPYTHON_TARGET} +add_custom_command(TARGET ${MICROPY_TARGET} POST_BUILD - COMMAND arm-none-eabi-size --format=berkeley ${PROJECT_BINARY_DIR}/${MICROPYTHON_TARGET}.elf + COMMAND arm-none-eabi-size --format=berkeley ${PROJECT_BINARY_DIR}/${MICROPY_TARGET}.elf VERBATIM ) + +# Collect all the include directories and compile definitions for the pico-sdk components. +macro(_process_target targ) + if(TARGET ${targ}) + get_target_property(type ${targ} TYPE) + set(_inc OFF) + set(_def OFF) + if(${type} STREQUAL STATIC_LIBRARY) + get_target_property(_inc ${targ} INCLUDE_DIRECTORIES) + get_target_property(_def ${targ} COMPILE_DEFINITIONS) + elseif(${type} STREQUAL INTERFACE_LIBRARY) + get_target_property(_inc ${targ} INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(_def ${targ} INTERFACE_COMPILE_DEFINITIONS) + endif() + if(_inc) + list(APPEND MICROPY_CPP_INC_EXTRA ${_inc}) + endif() + if(_def) + list(APPEND MICROPY_CPP_DEF_EXTRA ${_def}) + endif() + endif() +endmacro() +foreach(comp ${PICO_SDK_COMPONENTS}) + _process_target(${comp}) + _process_target(${comp}_headers) +endforeach() + +# Include the main MicroPython cmake rules. +include(${MICROPY_DIR}/py/mkrules.cmake) diff --git a/ports/rp2/micropy_extmod.cmake b/ports/rp2/micropy_extmod.cmake deleted file mode 100644 index 1e968bef637fd..0000000000000 --- a/ports/rp2/micropy_extmod.cmake +++ /dev/null @@ -1,40 +0,0 @@ -# CMake fragment for MicroPython extmod component - -set(SOURCE_EXTMOD - ${MPY_DIR}/extmod/machine_i2c.c - ${MPY_DIR}/extmod/machine_mem.c - ${MPY_DIR}/extmod/machine_pulse.c - ${MPY_DIR}/extmod/machine_signal.c - ${MPY_DIR}/extmod/machine_spi.c - ${MPY_DIR}/extmod/modbtree.c - ${MPY_DIR}/extmod/modframebuf.c - ${MPY_DIR}/extmod/modonewire.c - ${MPY_DIR}/extmod/moduasyncio.c - ${MPY_DIR}/extmod/modubinascii.c - ${MPY_DIR}/extmod/moducryptolib.c - ${MPY_DIR}/extmod/moductypes.c - ${MPY_DIR}/extmod/moduhashlib.c - ${MPY_DIR}/extmod/moduheapq.c - ${MPY_DIR}/extmod/modujson.c - ${MPY_DIR}/extmod/modurandom.c - ${MPY_DIR}/extmod/modure.c - ${MPY_DIR}/extmod/moduselect.c - ${MPY_DIR}/extmod/modussl_axtls.c - ${MPY_DIR}/extmod/modussl_mbedtls.c - ${MPY_DIR}/extmod/modutimeq.c - ${MPY_DIR}/extmod/moduwebsocket.c - ${MPY_DIR}/extmod/moduzlib.c - ${MPY_DIR}/extmod/modwebrepl.c - ${MPY_DIR}/extmod/uos_dupterm.c - ${MPY_DIR}/extmod/utime_mphal.c - ${MPY_DIR}/extmod/vfs.c - ${MPY_DIR}/extmod/vfs_blockdev.c - ${MPY_DIR}/extmod/vfs_fat.c - ${MPY_DIR}/extmod/vfs_fat_diskio.c - ${MPY_DIR}/extmod/vfs_fat_file.c - ${MPY_DIR}/extmod/vfs_lfs.c - ${MPY_DIR}/extmod/vfs_posix.c - ${MPY_DIR}/extmod/vfs_posix_file.c - ${MPY_DIR}/extmod/vfs_reader.c - ${MPY_DIR}/extmod/virtpin.c -) diff --git a/ports/rp2/micropy_py.cmake b/ports/rp2/micropy_py.cmake deleted file mode 100644 index aeb7e2b9b5aa7..0000000000000 --- a/ports/rp2/micropy_py.cmake +++ /dev/null @@ -1,134 +0,0 @@ -# CMake fragment for MicroPython core py component - -set(MPY_PY_DIR "${MPY_DIR}/py") -set(MPY_PY_QSTRDEFS "${MPY_PY_DIR}/qstrdefs.h") -set(MPY_GENHDR_DIR "${CMAKE_BINARY_DIR}/genhdr") -set(MPY_MPVERSION "${MPY_GENHDR_DIR}/mpversion.h") -set(MPY_MODULEDEFS "${MPY_GENHDR_DIR}/moduledefs.h") -set(MPY_QSTR_DEFS_LAST "${MPY_GENHDR_DIR}/qstr.i.last") -set(MPY_QSTR_DEFS_SPLIT "${MPY_GENHDR_DIR}/qstr.split") -set(MPY_QSTR_DEFS_COLLECTED "${MPY_GENHDR_DIR}/qstrdefs.collected.h") -set(MPY_QSTR_DEFS_PREPROCESSED "${MPY_GENHDR_DIR}/qstrdefs.preprocessed.h") -set(MPY_QSTR_DEFS_GENERATED "${MPY_GENHDR_DIR}/qstrdefs.generated.h") -set(MPY_FROZEN_CONTENT "${CMAKE_BINARY_DIR}/frozen_content.c") - -# All py/ source files -set(SOURCE_PY - ${MPY_PY_DIR}/argcheck.c - ${MPY_PY_DIR}/asmarm.c - ${MPY_PY_DIR}/asmbase.c - ${MPY_PY_DIR}/asmthumb.c - ${MPY_PY_DIR}/asmx64.c - ${MPY_PY_DIR}/asmx86.c - ${MPY_PY_DIR}/asmxtensa.c - ${MPY_PY_DIR}/bc.c - ${MPY_PY_DIR}/binary.c - ${MPY_PY_DIR}/builtinevex.c - ${MPY_PY_DIR}/builtinhelp.c - ${MPY_PY_DIR}/builtinimport.c - ${MPY_PY_DIR}/compile.c - ${MPY_PY_DIR}/emitbc.c - ${MPY_PY_DIR}/emitcommon.c - ${MPY_PY_DIR}/emitglue.c - ${MPY_PY_DIR}/emitinlinethumb.c - ${MPY_PY_DIR}/emitinlinextensa.c - ${MPY_PY_DIR}/emitnarm.c - ${MPY_PY_DIR}/emitnthumb.c - ${MPY_PY_DIR}/emitnx64.c - ${MPY_PY_DIR}/emitnx86.c - ${MPY_PY_DIR}/emitnxtensa.c - ${MPY_PY_DIR}/emitnxtensawin.c - ${MPY_PY_DIR}/formatfloat.c - ${MPY_PY_DIR}/frozenmod.c - ${MPY_PY_DIR}/gc.c - ${MPY_PY_DIR}/lexer.c - ${MPY_PY_DIR}/malloc.c - ${MPY_PY_DIR}/map.c - ${MPY_PY_DIR}/modarray.c - ${MPY_PY_DIR}/modbuiltins.c - ${MPY_PY_DIR}/modcmath.c - ${MPY_PY_DIR}/modcollections.c - ${MPY_PY_DIR}/modgc.c - ${MPY_PY_DIR}/modio.c - ${MPY_PY_DIR}/modmath.c - ${MPY_PY_DIR}/modmicropython.c - ${MPY_PY_DIR}/modstruct.c - ${MPY_PY_DIR}/modsys.c - ${MPY_PY_DIR}/modthread.c - ${MPY_PY_DIR}/moduerrno.c - ${MPY_PY_DIR}/mpprint.c - ${MPY_PY_DIR}/mpstate.c - ${MPY_PY_DIR}/mpz.c - ${MPY_PY_DIR}/nativeglue.c - ${MPY_PY_DIR}/nlr.c - ${MPY_PY_DIR}/nlrpowerpc.c - ${MPY_PY_DIR}/nlrsetjmp.c - ${MPY_PY_DIR}/nlrthumb.c - ${MPY_PY_DIR}/nlrx64.c - ${MPY_PY_DIR}/nlrx86.c - ${MPY_PY_DIR}/nlrxtensa.c - ${MPY_PY_DIR}/obj.c - ${MPY_PY_DIR}/objarray.c - ${MPY_PY_DIR}/objattrtuple.c - ${MPY_PY_DIR}/objbool.c - ${MPY_PY_DIR}/objboundmeth.c - ${MPY_PY_DIR}/objcell.c - ${MPY_PY_DIR}/objclosure.c - ${MPY_PY_DIR}/objcomplex.c - ${MPY_PY_DIR}/objdeque.c - ${MPY_PY_DIR}/objdict.c - ${MPY_PY_DIR}/objenumerate.c - ${MPY_PY_DIR}/objexcept.c - ${MPY_PY_DIR}/objfilter.c - ${MPY_PY_DIR}/objfloat.c - ${MPY_PY_DIR}/objfun.c - ${MPY_PY_DIR}/objgenerator.c - ${MPY_PY_DIR}/objgetitemiter.c - ${MPY_PY_DIR}/objint.c - ${MPY_PY_DIR}/objint_longlong.c - ${MPY_PY_DIR}/objint_mpz.c - ${MPY_PY_DIR}/objlist.c - ${MPY_PY_DIR}/objmap.c - ${MPY_PY_DIR}/objmodule.c - ${MPY_PY_DIR}/objnamedtuple.c - ${MPY_PY_DIR}/objnone.c - ${MPY_PY_DIR}/objobject.c - ${MPY_PY_DIR}/objpolyiter.c - ${MPY_PY_DIR}/objproperty.c - ${MPY_PY_DIR}/objrange.c - ${MPY_PY_DIR}/objreversed.c - ${MPY_PY_DIR}/objset.c - ${MPY_PY_DIR}/objsingleton.c - ${MPY_PY_DIR}/objslice.c - ${MPY_PY_DIR}/objstr.c - ${MPY_PY_DIR}/objstringio.c - ${MPY_PY_DIR}/objstrunicode.c - ${MPY_PY_DIR}/objtuple.c - ${MPY_PY_DIR}/objtype.c - ${MPY_PY_DIR}/objzip.c - ${MPY_PY_DIR}/opmethods.c - ${MPY_PY_DIR}/pairheap.c - ${MPY_PY_DIR}/parse.c - ${MPY_PY_DIR}/parsenum.c - ${MPY_PY_DIR}/parsenumbase.c - ${MPY_PY_DIR}/persistentcode.c - ${MPY_PY_DIR}/profile.c - ${MPY_PY_DIR}/pystack.c - ${MPY_PY_DIR}/qstr.c - ${MPY_PY_DIR}/reader.c - ${MPY_PY_DIR}/repl.c - ${MPY_PY_DIR}/ringbuf.c - ${MPY_PY_DIR}/runtime.c - ${MPY_PY_DIR}/runtime_utils.c - ${MPY_PY_DIR}/scheduler.c - ${MPY_PY_DIR}/scope.c - ${MPY_PY_DIR}/sequence.c - ${MPY_PY_DIR}/showbc.c - ${MPY_PY_DIR}/smallint.c - ${MPY_PY_DIR}/stackctrl.c - ${MPY_PY_DIR}/stream.c - ${MPY_PY_DIR}/unicode.c - ${MPY_PY_DIR}/vm.c - ${MPY_PY_DIR}/vstr.c - ${MPY_PY_DIR}/warning.c -) diff --git a/ports/rp2/micropy_rules.cmake b/ports/rp2/micropy_rules.cmake deleted file mode 100644 index 9eee4ac14d1b2..0000000000000 --- a/ports/rp2/micropy_rules.cmake +++ /dev/null @@ -1,90 +0,0 @@ -# CMake fragment for MicroPython rules - -target_sources(${MICROPYTHON_TARGET} PRIVATE - ${MPY_MPVERSION} - ${MPY_QSTR_DEFS_GENERATED} - ${MPY_FROZEN_CONTENT} -) - -# Command to force the build of another command - -add_custom_command( - OUTPUT FORCE_BUILD - COMMENT "" - COMMAND echo -n -) - -# Generate mpversion.h - -add_custom_command( - OUTPUT ${MPY_MPVERSION} - COMMAND ${CMAKE_COMMAND} -E make_directory ${MPY_GENHDR_DIR} - COMMAND python3 ${MPY_DIR}/py/makeversionhdr.py ${MPY_MPVERSION} - DEPENDS FORCE_BUILD -) - -# Generate moduledefs.h - -add_custom_command( - OUTPUT ${MPY_MODULEDEFS} - COMMAND python3 ${MPY_PY_DIR}/makemoduledefs.py --vpath="/" ${SOURCE_QSTR} > ${MPY_MODULEDEFS} - DEPENDS ${MPY_MPVERSION} - ${SOURCE_QSTR} -) - -# Generate qstrs - -# If any of the dependencies in this rule change then the C-preprocessor step must be run. -# It only needs to be passed the list of SOURCE_QSTR files that have changed since it was -# last run, but it looks like it's not possible to specify that with cmake. -add_custom_command( - OUTPUT ${MPY_QSTR_DEFS_LAST} - COMMAND ${CMAKE_C_COMPILER} -E \$\(C_INCLUDES\) \$\(C_FLAGS\) -DNO_QSTR ${SOURCE_QSTR} > ${MPY_GENHDR_DIR}/qstr.i.last - DEPENDS ${MPY_MODULEDEFS} - ${SOURCE_QSTR} - VERBATIM -) - -add_custom_command( - OUTPUT ${MPY_QSTR_DEFS_SPLIT} - COMMAND python3 ${MPY_DIR}/py/makeqstrdefs.py split qstr ${MPY_GENHDR_DIR}/qstr.i.last ${MPY_GENHDR_DIR}/qstr _ - COMMAND touch ${MPY_QSTR_DEFS_SPLIT} - DEPENDS ${MPY_QSTR_DEFS_LAST} - VERBATIM -) - -add_custom_command( - OUTPUT ${MPY_QSTR_DEFS_COLLECTED} - COMMAND python3 ${MPY_DIR}/py/makeqstrdefs.py cat qstr _ ${MPY_GENHDR_DIR}/qstr ${MPY_QSTR_DEFS_COLLECTED} - DEPENDS ${MPY_QSTR_DEFS_SPLIT} - VERBATIM -) - -add_custom_command( - OUTPUT ${MPY_QSTR_DEFS_PREPROCESSED} - COMMAND cat ${MPY_PY_QSTRDEFS} ${MPY_QSTR_DEFS} ${MPY_QSTR_DEFS_COLLECTED} | sed "s/^Q(.*)/\"&\"/" | ${CMAKE_C_COMPILER} -E \$\(C_INCLUDES\) \$\(C_FLAGS\) - | sed "s/^\\\"\\(Q(.*)\\)\\\"/\\1/" > ${MPY_QSTR_DEFS_PREPROCESSED} - DEPENDS ${MPY_PY_QSTRDEFS} ${MPY_QSTR_DEFS} ${MPY_QSTR_DEFS_COLLECTED} - VERBATIM -) - -add_custom_command( - OUTPUT ${MPY_QSTR_DEFS_GENERATED} - COMMAND python3 ${MPY_PY_DIR}/makeqstrdata.py ${MPY_QSTR_DEFS_PREPROCESSED} > ${MPY_QSTR_DEFS_GENERATED} - DEPENDS ${MPY_QSTR_DEFS_PREPROCESSED} - VERBATIM -) - -# Build frozen code - -target_compile_options(${MICROPYTHON_TARGET} PUBLIC - -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool - -DMICROPY_MODULE_FROZEN_MPY=\(1\) -) - -add_custom_command( - OUTPUT ${MPY_FROZEN_CONTENT} - COMMAND python3 ${MPY_DIR}/tools/makemanifest.py -o ${MPY_FROZEN_CONTENT} -v "MPY_DIR=${MPY_DIR}" -v "PORT_DIR=${PROJECT_SOURCE_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MPY_CROSS_FLAGS} ${FROZEN_MANIFEST} - DEPENDS FORCE_BUILD - ${MPY_QSTR_DEFS_GENERATED} - VERBATIM -) From 914380cb89d7cf5e9c186055753ff12bbcf6296a Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 15 Mar 2021 10:29:17 +0100 Subject: [PATCH 0688/3533] all: Add .git-blame-ignore-revs for fixing up git blame output. Add most formatting-only commits to this file so that when used with git blame, these commits are excluded and the output shows only the interesting bits. --- .git-blame-ignore-revs | 32 ++++++++++++++++++++++++++++++++ docs/develop/gettingstarted.rst | 5 +++++ 2 files changed, 37 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000000000..83726922a5280 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,32 @@ +# tests/run-tests.py: Reformat with Black. +2a38d7103672580882fb621a5b76e8d26805d593 + +# all: Update Python code to conform to latest black formatting. +06659077a81b85882254cf0953c33b27614e018e + +# tools/uncrustify: Enable more opts to remove space between func and '('. +77ed6f69ac35c1663a5633a8ee1d8a2446542204 + +# tools/codeformat.py: Include extmod/{btstack,nimble} in code formatting. +026fda605e03113d6e753290d65fed774418bc53 + +# all: Format code to add space after C++-style comment start. +84fa3312cfa7d2237d4b56952f2cd6e3591210c4 + +# tests: Format all Python code with black, except tests in basics subdir. +3dc324d3f1312e40d3a8ed87e7244966bb756f26 + +# all: Remove spaces inside and around parenthesis. +1a3e386c67e03a79eb768cb6e9f6777e002d6660 + +# all: Remove spaces between nested paren and inside function arg paren. +feb25775851ba0c04b8d1013716f442258879d9c + +# all: Reformat C and Python source code with tools/codeformat.py. +69661f3343bedf86e514337cff63d96cc42f8859 + +# stm32/usbdev: Convert files to unix line endings. +abde0fa2267f9062b28c3c015d7662a550125cc6 + +# all: Remove trailing spaces, per coding conventions. +761e4c7ff62896c7d8f8c3dfc3cc98a4cc4f2f6f diff --git a/docs/develop/gettingstarted.rst b/docs/develop/gettingstarted.rst index 32435ebe1c9d6..30b26071ea012 100644 --- a/docs/develop/gettingstarted.rst +++ b/docs/develop/gettingstarted.rst @@ -21,6 +21,11 @@ of Git for your operating system to follow through the rest of the steps. Learn about the basic git commands in this `Git Handbook `_ or any other sources on the internet. +.. note:: + A .git-blame-ignore-revs file is included which avoids the output of git blame getting cluttered + by commits which are only for formatting code but have no functional changes. See `git blame documentation + `_ on how to use this. + Get the code ------------ From d53a6d58b04e524e4ec5ee937a15cc3932f8ee2f Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 15 Mar 2021 09:30:46 +0100 Subject: [PATCH 0689/3533] stm32/Makefile: Fix C++ linker flags when toolchain has spaces in path. The GNU Make dir command uses spaces as item separator so it does not work for e.g building the STM32 port on Cygwin with a default Arm installation in "c:/program files (x86)/GNU Arm Embedded Toolchain". Fix by using POSIX dirname on a quoted path instead. --- ports/stm32/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 6e5fc15363509..9a66d3c10b8cd 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -145,7 +145,8 @@ endif CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS)) CXXFLAGS += $(CXXFLAGS_MOD) ifneq ($(SRC_CXX)$(SRC_MOD_CXX),) -LDFLAGS += -L$(dir $(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)) +LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" +LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif # Options for mpy-cross From 2b888aa2f346b52b16fbefbea6a2d9d8085d8ff1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Mar 2021 12:40:16 +1100 Subject: [PATCH 0690/3533] extmod/modbluetooth: Free temp arrays in gatts register services. This helps to reduce memory fragmentation, by freeing the heap data as soon as it is not needed. It also helps the compiler keeps a reference to the beginning of both arrays, which need to be traceable by the GC (otherwise some compilers may optimise this reference to something else). Signed-off-by: Damien George --- extmod/modbluetooth.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 269493f0a94a3..e379a8c6a304c 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -619,6 +619,11 @@ STATIC mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t } result->items[i] = MP_OBJ_FROM_PTR(service_handles); } + + // Free temporary arrays. + m_del(uint16_t *, handles, len); + m_del(size_t, num_handles, len); + return MP_OBJ_FROM_PTR(result); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, bluetooth_ble_gatts_register_services); From a79d97cb76cf6ec3794c357b706dd24970317c4a Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Mar 2021 12:45:39 +1100 Subject: [PATCH 0691/3533] tests/extmod/vfs_fat_fileio2.py: Close test file at end of test. Otherwise it can lead to inconsistent results running subsequent tests. Signed-off-by: Damien George --- tests/extmod/vfs_fat_fileio2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/extmod/vfs_fat_fileio2.py b/tests/extmod/vfs_fat_fileio2.py index a9cea2bed8346..531dd91f9490a 100644 --- a/tests/extmod/vfs_fat_fileio2.py +++ b/tests/extmod/vfs_fat_fileio2.py @@ -112,3 +112,4 @@ def ioctl(self, op, arg): f.write(bytearray(bsize * free)) except OSError as e: print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC +f.close() From cb68a5741aba5d4935428674234a9d643f97405f Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Mar 2021 12:46:49 +1100 Subject: [PATCH 0692/3533] tests/run-tests.py: Provide more info if script run via pyboard crashes. Signed-off-by: Damien George --- tests/run-tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/run-tests.py b/tests/run-tests.py index 3ab6194b628fe..ae63f9a29a6e2 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -187,7 +187,7 @@ def send_get(what): if not is_special and e.args[0] == "exception": output_mupy = e.args[1] + e.args[2] + b"CRASH" else: - output_mupy = b"CRASH" + output_mupy = bytes(e.args[0], "ascii") + b"\nCRASH" # canonical form for all ports/platforms is to use \n for end-of-line output_mupy = output_mupy.replace(b"\r\n", b"\n") From 6e5aea08a9916c5acc6893e4829bba25fbab32ba Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Mar 2021 10:25:16 +1100 Subject: [PATCH 0693/3533] stm32/Makefile: Allow QSTR_DEFS,QSTR_GLOBAL_DEPENDENCIES to be extended. So a board can provide custom qstr definitions if needed. Signed-off-by: Damien George --- ports/stm32/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 9a66d3c10b8cd..6c71f2757333f 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -18,8 +18,8 @@ include $(BOARD_DIR)/mpconfigboard.mk QSTR_GENERATED_HEADERS = $(BUILD)/pins_qstr.h $(BUILD)/modstm_qstr.h # qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h $(QSTR_GENERATED_HEADERS) -QSTR_GLOBAL_DEPENDENCIES = mpconfigboard_common.h $(BOARD_DIR)/mpconfigboard.h $(QSTR_GENERATED_HEADERS) +QSTR_DEFS += qstrdefsport.h $(QSTR_GENERATED_HEADERS) +QSTR_GLOBAL_DEPENDENCIES += mpconfigboard_common.h $(BOARD_DIR)/mpconfigboard.h $(QSTR_GENERATED_HEADERS) # MicroPython feature configurations MICROPY_ROM_TEXT_COMPRESSION ?= 1 From 42cf77f48bcb24b0515b4ce604736f10267d87bd Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 Mar 2021 10:22:03 +1100 Subject: [PATCH 0694/3533] py/vm: For tracing use mp_printf, and print state when thread enabled. mp_printf should be used to print the prefix because it's also used in mp_bytecode_print2 (otherwise, depending on the system, different output streams may be used). Also print the current thread state when threading is enabled to easily see which thread executes what opcode. Signed-off-by: Damien George --- py/vm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/vm.c b/py/vm.c index 393a2bbbd6513..c97070b78b6ba 100644 --- a/py/vm.c +++ b/py/vm.c @@ -39,7 +39,12 @@ // *FORMAT-OFF* #if 0 -#define TRACE(ip) printf("sp=%d ", (int)(sp - &code_state->state[0] + 1)); mp_bytecode_print2(&mp_plat_print, ip, 1, code_state->fun_bc->const_table); +#if MICROPY_PY_THREAD +#define TRACE_PREFIX mp_printf(&mp_plat_print, "ts=%p sp=%d ", mp_thread_get_state(), (int)(sp - &code_state->state[0] + 1)) +#else +#define TRACE_PREFIX mp_printf(&mp_plat_print, "sp=%d ", (int)(sp - &code_state->state[0] + 1)) +#endif +#define TRACE(ip) TRACE_PREFIX; mp_bytecode_print2(&mp_plat_print, ip, 1, code_state->fun_bc->const_table); #else #define TRACE(ip) #endif From 4fc2866f45692b05829d71b636724e690b786307 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Mar 2021 21:03:03 +1100 Subject: [PATCH 0695/3533] bare-arm: Clean up the code, make it run on an F405, and add a README. This commit simplifies and cleans up the bare-arm port, and adds just enough system and library code to make it execute on an STM32F405 MCU. The mpconfigport.h configuration is simplified to just specify those configuration values that are different from the defaults. And the addition of -fdata-sections and -ffunction-sections means the final firmware is smaller than it previously was, by about 4200 bytes. A README is also added. Signed-off-by: Damien George --- ports/bare-arm/Makefile | 67 ++++++++------- ports/bare-arm/README.md | 21 +++++ ports/bare-arm/lib.c | 128 +++++++++++++++++++++++++++ ports/bare-arm/main.c | 157 +++++++++++----------------------- ports/bare-arm/mpconfigport.h | 118 ++++++++++++------------- ports/bare-arm/qstrdefsport.h | 2 - ports/bare-arm/stm32f405.ld | 93 ++++---------------- ports/bare-arm/system.c | 142 ++++++++++++++++++++++++++++++ 8 files changed, 453 insertions(+), 275 deletions(-) create mode 100644 ports/bare-arm/README.md create mode 100644 ports/bare-arm/lib.c delete mode 100644 ports/bare-arm/qstrdefsport.h create mode 100644 ports/bare-arm/system.c diff --git a/ports/bare-arm/Makefile b/ports/bare-arm/Makefile index 72d5f1a277269..455a9dc5ac137 100644 --- a/ports/bare-arm/Makefile +++ b/ports/bare-arm/Makefile @@ -1,52 +1,59 @@ +# Include the core environment definitions; this will set $(TOP). include ../../py/mkenv.mk -# qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h +# Include py core make definitions. +include $(TOP)/py/py.mk -# MicroPython feature configurations +# Set makefile-level MicroPython feature configurations. MICROPY_ROM_TEXT_COMPRESSION ?= 1 -# include py core make definitions -include $(TOP)/py/py.mk - +# Define toolchain and other tools. CROSS_COMPILE ?= arm-none-eabi- +DFU ?= $(TOP)/tools/dfu.py +PYDFU ?= $(TOP)/tools/pydfu.py -INC += -I. -INC += -I$(TOP) -INC += -I$(BUILD) - -CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) -CSUPEROPT = -Os # save some code space +# Set CFLAGS. +CFLAGS += -I. -I$(TOP) -I$(BUILD) +CFLAGS += -Wall -Werror -std=c99 -nostdlib +CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float +CSUPEROPT = -Os # save some code space for performance-critical code -#Debugging/Optimization +# Select debugging or optimisation build. ifeq ($(DEBUG), 1) -CFLAGS += -O0 -ggdb +CFLAGS += -Og else CFLAGS += -Os -DNDEBUG +CFLAGS += -fdata-sections -ffunction-sections endif -LDFLAGS = -nostdlib -T stm32f405.ld -Map=$@.map --cref -LIBS = +# Set linker flags. +LDFLAGS += -nostdlib -T stm32f405.ld --gc-sections -SRC_C = \ - main.c \ -# printf.c \ - string0.c \ - malloc0.c \ - gccollect.c \ +# Define the required source files. +SRC_C += lib.c main.c system.c -SRC_S = \ -# startup_stm32f40xx.s \ - gchelper.s \ +# Define the required object files. +OBJ += $(PY_CORE_O) +OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o)) - -all: $(BUILD)/firmware.elf +# Define the top-level target, the main firmware. +all: $(BUILD)/firmware.dfu $(BUILD)/firmware.elf: $(OBJ) $(ECHO) "LINK $@" - $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) + $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(Q)$(SIZE) $@ +$(BUILD)/firmware.bin: $(BUILD)/firmware.elf + $(ECHO) "Create $@" + $(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data $^ $@ + +$(BUILD)/firmware.dfu: $(BUILD)/firmware.bin + $(ECHO) "Create $@" + $(Q)$(PYTHON) $(DFU) -b 0x08000000:$^ $@ + +deploy: $(BUILD)/firmware.dfu + $(Q)$(PYTHON) $(PYDFU) -u $^ + +# Include remaining core make rules. include $(TOP)/py/mkrules.mk diff --git a/ports/bare-arm/README.md b/ports/bare-arm/README.md new file mode 100644 index 0000000000000..496ee9c7555de --- /dev/null +++ b/ports/bare-arm/README.md @@ -0,0 +1,21 @@ +The bare-arm port +================= + +This port is intended to be the bare-minimum amount of code and configuration +required to get MicroPython compiling and running on a bare-metal ARM-based +target. No external dependencies or libraries are needed for this build and +it shows exactly what hardware and system functionality MicroPython needs to +run. + +To build, simply run `make` in this directory. The output will be +`build/firmware.elf` (and also corresponding `.bin` and `.dfu` files). This +firmware can run on an STM32F405-based board (eg a PYBv1.x) and `make deploy` +will program it to such an MCU when put in USB DFU mode. The output is a UART +at 115200 baud, with TX on PA0. + +There are some simple demonstration code strings (see `main.c`) which are +compiled and executed when the firmware starts. They produce output on the +system's stdout. + +The size of the firmware (the machine code that is programmed to the +microcontroller's flash/ROM) is currently around 61200 bytes. diff --git a/ports/bare-arm/lib.c b/ports/bare-arm/lib.c new file mode 100644 index 0000000000000..ee7c1d765e9d4 --- /dev/null +++ b/ports/bare-arm/lib.c @@ -0,0 +1,128 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +// These memory functions are needed when the garbage collector is disabled. +// A full implementation should be provided, or the garbage collector enabled. +// The functions here are very simple. + +extern char _heap_start; + +void *malloc(size_t n) { + static char *cur_heap = NULL; + if (cur_heap == NULL) { + cur_heap = &_heap_start; + } + void *ptr = cur_heap; + cur_heap += (n + 7) & ~7; + return ptr; +} + +void *realloc(void *ptr, size_t size) { + void *ptr2 = malloc(size); + if (ptr && size) { + memcpy(ptr2, ptr, size); // size may be greater than ptr's region, do copy anyway + } + return ptr2; +} + +void free(void *p) { +} + +// These standard string functions are needed by the runtime, and can be +// provided either by the system or lib/libc/string0.c. The implementations +// here are very simple. + +int memcmp(const void *s1, const void *s2, size_t n) { + const unsigned char *ss1 = s1, *ss2 = s2; + while (n--) { + int c = *ss1++ - *ss2++; + if (c) { + return c; + } + } + return 0; +} + +void *memcpy(void *dest, const void *src, size_t n) { + return memmove(dest, src, n); +} + +void *memmove(void *dest, const void *src, size_t n) { + unsigned char *d = dest; + const unsigned char *s = src; + if (s < d && d < s + n) { + // Need to copy backwards. + d += n - 1; + s += n - 1; + while (n--) { + *d-- = *s--; + } + } else { + // Can copy forwards. + while (n--) { + *d++ = *s++; + } + } + return dest; +} + +void *memset(void *s, int c, size_t n) { + unsigned char *ss = s; + while (n--) { + *ss++ = c; + } + return s; +} + +char *strchr(const char *s, int c) { + while (*s) { + if (*s == c) { + return (char *)s; + } + ++s; + } + return NULL; +} + +int strcmp(const char *s1, const char *s2) { + while (*s1 && *s2) { + int c = *s1++ - *s2++; + if (c) { + return c; + } + } + return *s1 - *s2; +} + +size_t strlen(const char *s) { + const char *ss = s; + while (*ss) { + ++ss; + } + return ss - s; +} diff --git a/ports/bare-arm/main.c b/ports/bare-arm/main.c index df66c04effd7d..82c27bd097f9c 100644 --- a/ports/bare-arm/main.c +++ b/ports/bare-arm/main.c @@ -1,15 +1,48 @@ -#include -#include -#include +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2014-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include #include "py/compile.h" #include "py/runtime.h" -#include "py/repl.h" -#include "py/mperrno.h" -void do_str(const char *src, mp_parse_input_kind_t input_kind) { +static const char *demo_single_input = + "print('hello world!', list(x + 1 for x in range(10)), end='eol\\n')"; + +static const char *demo_file_input = + "import micropython\n" + "\n" + "print(dir(micropython))\n" + "\n" + "for i in range(10):\n" + " print('iter {:08}'.format(i))"; + +static void do_str(const char *src, mp_parse_input_kind_t input_kind) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { + // Compile, parse and execute the given string. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); @@ -17,121 +50,29 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { mp_call_function_0(module_fun); nlr_pop(); } else { - // uncaught exception + // Uncaught exception: print it out. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); } } -int main(int argc, char **argv) { +// Main entry point: initialise the runtime and execute demo strings. +void bare_main(void) { mp_init(); - do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT); - do_str("for i in range(10):\n print(i)", MP_PARSE_FILE_INPUT); + do_str(demo_single_input, MP_PARSE_SINGLE_INPUT); + do_str(demo_file_input, MP_PARSE_FILE_INPUT); mp_deinit(); - return 0; -} - -mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - mp_raise_OSError(MP_ENOENT); -} - -mp_import_stat_t mp_import_stat(const char *path) { - return MP_IMPORT_STAT_NO_EXIST; -} - -mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); +// Called if an exception is raised outside all C exception-catching handlers. void nlr_jump_fail(void *val) { - while (1) { - ; - } -} - -void NORETURN __fatal_error(const char *msg) { - while (1) { - ; + for (;;) { } } #ifndef NDEBUG +// Used when debugging is enabled. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { - printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); - __fatal_error("Assertion failed"); + for (;;) { + } } #endif - -/* -int _lseek() {return 0;} -int _read() {return 0;} -int _write() {return 0;} -int _close() {return 0;} -void _exit(int x) {for(;;){}} -int _sbrk() {return 0;} -int _kill() {return 0;} -int _getpid() {return 0;} -int _fstat() {return 0;} -int _isatty() {return 0;} -*/ - -void *malloc(size_t n) { - return NULL; -} -void *calloc(size_t nmemb, size_t size) { - return NULL; -} -void *realloc(void *ptr, size_t size) { - return NULL; -} -void free(void *p) { -} -int printf(const char *m, ...) { - return 0; -} -void *memcpy(void *dest, const void *src, size_t n) { - return NULL; -} -int memcmp(const void *s1, const void *s2, size_t n) { - return 0; -} -void *memmove(void *dest, const void *src, size_t n) { - return NULL; -} -void *memset(void *s, int c, size_t n) { - return NULL; -} -int strcmp(const char *s1, const char *s2) { - return 0; -} -int strncmp(const char *s1, const char *s2, size_t n) { - return 0; -} -size_t strlen(const char *s) { - return 0; -} -char *strcat(char *dest, const char *src) { - return NULL; -} -char *strchr(const char *dest, int c) { - return NULL; -} -#include -int vprintf(const char *format, va_list ap) { - return 0; -} -int vsnprintf(char *str, size_t size, const char *format, va_list ap) { - return 0; -} - -#undef putchar -int putchar(int c) { - return 0; -} -int puts(const char *s) { - return 0; -} - -void _start(void) { - main(0, NULL); -} diff --git a/ports/bare-arm/mpconfigport.h b/ports/bare-arm/mpconfigport.h index 7fd236bfba9e4..41b6ee71a3f84 100644 --- a/ports/bare-arm/mpconfigport.h +++ b/ports/bare-arm/mpconfigport.h @@ -1,71 +1,71 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2014-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #include -// options to control how MicroPython is built +// Options to control how MicroPython is built + +// Memory allocation policy +#define MICROPY_QSTR_BYTES_IN_HASH (1) -#define MICROPY_QSTR_BYTES_IN_HASH (1) -#define MICROPY_ALLOC_PATH_MAX (512) -#define MICROPY_EMIT_X64 (0) -#define MICROPY_EMIT_THUMB (0) -#define MICROPY_EMIT_INLINE_THUMB (0) -#define MICROPY_COMP_MODULE_CONST (0) -#define MICROPY_COMP_CONST (0) -#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) -#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0) -#define MICROPY_MEM_STATS (0) -#define MICROPY_DEBUG_PRINTERS (0) -#define MICROPY_ENABLE_GC (0) -#define MICROPY_HELPER_REPL (0) -#define MICROPY_HELPER_LEXER_UNIX (0) -#define MICROPY_ENABLE_SOURCE_LINE (0) -#define MICROPY_ENABLE_DOC_STRING (0) -#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) -#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) -#define MICROPY_PY_ASYNC_AWAIT (0) -#define MICROPY_PY_ASSIGN_EXPR (0) -#define MICROPY_PY_BUILTINS_BYTEARRAY (0) -#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) -#define MICROPY_PY_BUILTINS_MEMORYVIEW (0) -#define MICROPY_PY_BUILTINS_ENUMERATE (0) -#define MICROPY_PY_BUILTINS_FROZENSET (0) -#define MICROPY_PY_BUILTINS_REVERSED (0) -#define MICROPY_PY_BUILTINS_SET (0) -#define MICROPY_PY_BUILTINS_SLICE (0) -#define MICROPY_PY_BUILTINS_PROPERTY (0) -#define MICROPY_PY_BUILTINS_STR_COUNT (0) -#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0) -#define MICROPY_PY___FILE__ (0) -#define MICROPY_PY_GC (0) -#define MICROPY_PY_ARRAY (0) -#define MICROPY_PY_ATTRTUPLE (0) -#define MICROPY_PY_COLLECTIONS (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) -#define MICROPY_PY_IO (0) -#define MICROPY_PY_STRUCT (0) -#define MICROPY_PY_SYS (0) -#define MICROPY_CPYTHON_COMPAT (0) -#define MICROPY_MODULE_GETATTR (0) -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) -#define MICROPY_USE_INTERNAL_PRINTF (0) +// Compiler configuration +#define MICROPY_COMP_CONST (0) +#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) -// type definitions for the specific machine +// Python internal features +#define MICROPY_ENABLE_EXTERNAL_IMPORT (0) +#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) +#define MICROPY_CPYTHON_COMPAT (0) +#define MICROPY_MODULE_GETATTR (0) +#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) -#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) +// Fine control over Python builtins, classes, modules, etc +#define MICROPY_PY_ASYNC_AWAIT (0) +#define MICROPY_PY_ASSIGN_EXPR (0) +#define MICROPY_PY_BUILTINS_STR_COUNT (0) +#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0) +#define MICROPY_PY_BUILTINS_BYTEARRAY (0) +#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) +#define MICROPY_PY_BUILTINS_SET (0) +#define MICROPY_PY_BUILTINS_SLICE (0) +#define MICROPY_PY_BUILTINS_PROPERTY (0) +#define MICROPY_PY_BUILTINS_ENUMERATE (0) +#define MICROPY_PY_BUILTINS_REVERSED (0) +#define MICROPY_PY___FILE__ (0) +#define MICROPY_PY_ARRAY (0) +#define MICROPY_PY_COLLECTIONS (0) +#define MICROPY_PY_IO (0) +#define MICROPY_PY_STRUCT (0) +#define MICROPY_PY_SYS (0) -#define UINT_FMT "%lu" -#define INT_FMT "%ld" +// Type definitions for the specific machine typedef int32_t mp_int_t; // must be pointer size typedef uint32_t mp_uint_t; // must be pointer size typedef long mp_off_t; -// dummy print -#define MP_PLAT_PRINT_STRN(str, len) (void)0 - -// extra built in names to add to the global namespace -#define MICROPY_PORT_BUILTINS \ - { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, - -// We need to provide a declaration/definition of alloca() +// Need to provide a declaration/definition of alloca() #include diff --git a/ports/bare-arm/qstrdefsport.h b/ports/bare-arm/qstrdefsport.h deleted file mode 100644 index 00d3e2ae3c555..0000000000000 --- a/ports/bare-arm/qstrdefsport.h +++ /dev/null @@ -1,2 +0,0 @@ -// qstrs specific to this port -// *FORMAT-OFF* diff --git a/ports/bare-arm/stm32f405.ld b/ports/bare-arm/stm32f405.ld index dd688a02468da..33a5502d60a3e 100644 --- a/ports/bare-arm/stm32f405.ld +++ b/ports/bare-arm/stm32f405.ld @@ -1,117 +1,58 @@ -/* - GNU linker script for STM32F405 -*/ +/* GNU linker script for STM32F405 */ -/* Specify the memory areas */ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000 /* entire flash, 1 MiB */ - FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */ - CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */ } -/* top end of the stack */ _estack = ORIGIN(RAM) + LENGTH(RAM); -/* RAM extents for the garbage collector */ -_ram_end = ORIGIN(RAM) + LENGTH(RAM); -_heap_end = 0x2001c000; /* tunable */ - -/* define output sections */ SECTIONS { - /* The startup code goes first into FLASH */ .isr_vector : { . = ALIGN(4); - KEEP(*(.isr_vector)) /* Startup code */ - + KEEP(*(.isr_vector)) . = ALIGN(4); - } >FLASH_ISR + } >FLASH - /* The program code and other data goes into FLASH */ .text : { . = ALIGN(4); - *(.text) /* .text sections (code) */ - *(.text*) /* .text* sections (code) */ - *(.rodata) /* .rodata sections (constants, strings, etc.) */ - *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ - /* *(.glue_7) */ /* glue arm to thumb code */ - /* *(.glue_7t) */ /* glue thumb to arm code */ - + *(.text) + *(.text*) + *(.rodata) + *(.rodata*) . = ALIGN(4); - _etext = .; /* define a global symbol at end of code */ - _sidata = _etext; /* This is used by the startup in order to initialize the .data secion */ - } >FLASH_TEXT - - /* - .ARM.extab : - { - *(.ARM.extab* .gnu.linkonce.armextab.*) + _etext = .; + _sidata = _etext; } >FLASH - .ARM : - { - __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH - */ - - /* This is the initialized data section - The program executes knowing that the data is in the RAM - but the loader puts the initial values in the FLASH (inidata). - It is one task of the startup to copy the initial values from FLASH to RAM. */ .data : AT ( _sidata ) { . = ALIGN(4); - _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ - _ram_start = .; /* create a global symbol at ram start for garbage collector */ - *(.data) /* .data sections */ - *(.data*) /* .data* sections */ - + _sdata = .; + *(.data) + *(.data*) . = ALIGN(4); - _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ + _edata = .; } >RAM - /* Uninitialized data section */ .bss : { . = ALIGN(4); - _sbss = .; /* define a global symbol at bss start; used by startup code */ + _sbss = .; *(.bss) *(.bss*) *(COMMON) - . = ALIGN(4); - _ebss = .; /* define a global symbol at bss end; used by startup code */ + _ebss = .; } >RAM - /* this is to define the start of the heap, and make sure we have a minimum size */ .heap : { - . = ALIGN(4); - _heap_start = .; /* define a global symbol at heap start */ + . = ALIGN(8); + _heap_start = .; } >RAM - - /* this just checks there is enough RAM for the stack */ - .stack : - { - . = ALIGN(4); - } >RAM - - /* Remove information from the standard libraries */ - /* - /DISCARD/ : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - } - */ - - .ARM.attributes 0 : { *(.ARM.attributes) } } diff --git a/ports/bare-arm/system.c b/ports/bare-arm/system.c new file mode 100644 index 0000000000000..f66dd2d80a608 --- /dev/null +++ b/ports/bare-arm/system.c @@ -0,0 +1,142 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#define RCC ((periph_rcc_t *)0x40023800) +#define GPIOA ((periph_gpio_t *)0x40020000) +#define UART4 ((periph_uart_t *)0x40004C00) + +typedef struct { + volatile uint32_t CR; + volatile uint32_t PLLCFGR; + volatile uint32_t CFGR; + volatile uint32_t CIR; + uint32_t _1[8]; + volatile uint32_t AHB1ENR; + volatile uint32_t AHB2ENR; + volatile uint32_t AHB3ENR; + uint32_t _2; + volatile uint32_t APB1ENR; + volatile uint32_t APB2ENR; +} periph_rcc_t; + +typedef struct { + volatile uint32_t MODER; + volatile uint32_t OTYPER; + volatile uint32_t OSPEEDR; + volatile uint32_t PUPDR; + volatile uint32_t IDR; + volatile uint32_t ODR; + volatile uint16_t BSRRL; + volatile uint16_t BSRRH; + volatile uint32_t LCKR; + volatile uint32_t AFR[2]; +} periph_gpio_t; + +typedef struct { + volatile uint32_t SR; + volatile uint32_t DR; + volatile uint32_t BRR; + volatile uint32_t CR1; +} periph_uart_t; + +extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss; + +void Reset_Handler(void) __attribute__((naked)); +void bare_main(void); + +static void stm32_init(void); +static void gpio_init_alt(periph_gpio_t *gpio, int pin, int alt); + +// Very simple ARM vector table. +const uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = { + (uint32_t)&_estack, + (uint32_t)&Reset_Handler, +}; + +// The CPU runs this function after a reset. +void Reset_Handler(void) { + // Set stack pointer. + __asm volatile ("ldr sp, =_estack"); + + // Copy .data section from flash to RAM. + memcpy(&_sdata, &_sidata, (char *)&_edata - (char *)&_sdata); + + // Zero out .bss section. + memset(&_sbss, 0, (char *)&_ebss - (char *)&_sbss); + + // SCB->CCR: enable 8-byte stack alignment for IRQ handlers, in accord with EABI. + *((volatile uint32_t *)0xe000ed14) |= 1 << 9; + + // Initialise the cpu and peripherals. + stm32_init(); + + // Now that there is a basic system up and running, call the main application code. + bare_main(); + + // This function must not return. + for (;;) { + } +} + +// Set up the STM32 MCU. +static void stm32_init(void) { + // Note: default clock is internal 16MHz. + RCC->AHB1ENR |= 1 << 0; // GPIOAEN + RCC->APB1ENR |= 1 << 19; // UART4EN + gpio_init_alt(GPIOA, 0, 8); + UART4->BRR = (8 << 4) | 11; // 16MHz/(16*8.6875) = 115107 baud + UART4->CR1 = 0x00002008; // USART enable, tx enable, rx enable +} + +// Configure a GPIO pin in alternate-function mode. +static void gpio_init_alt(periph_gpio_t *gpio, int pin, int alt) { + gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | (2 << (2 * pin)); + // OTYPER is left as default push-pull + // OSPEEDR is left as default low speed + // PUPDR is left as default no-pull + gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7))); +} + +// Write a character out to the UART. +static inline void uart_write_char(int c) { + // Wait for TXE, then write the character. + while ((UART4->SR & (1 << 7)) == 0) { + } + UART4->DR = c; +} + +// Send string of given length to stdout, converting \n to \r\n. +void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { + while (len--) { + if (*str == '\n') { + uart_write_char('\r'); + } + uart_write_char(*str++); + } +} From 9fef1c0bde2f9642d383bd56aa112447384a84ba Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Mar 2021 13:48:34 +1100 Subject: [PATCH 0696/3533] py: Rename remaining object types to be of the form mp_type_xxx. For consistency with all other object types in the core. Signed-off-by: Damien George --- py/modio.c | 4 ++-- py/objarray.c | 4 ++-- py/objclosure.c | 4 ++-- py/objdict.c | 20 ++++++++++---------- py/objgetitemiter.c | 4 ++-- py/objrange.c | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/py/modio.c b/py/modio.c index 8a18357e24f12..7f0d13cdfaa0b 100644 --- a/py/modio.c +++ b/py/modio.c @@ -195,7 +195,7 @@ STATIC const mp_stream_p_t bufwriter_stream_p = { .write = bufwriter_write, }; -STATIC const mp_obj_type_t bufwriter_type = { +STATIC const mp_obj_type_t mp_type_bufwriter = { { &mp_type_type }, .name = MP_QSTR_BufferedWriter, .make_new = bufwriter_make_new, @@ -270,7 +270,7 @@ STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) }, #endif #if MICROPY_PY_IO_BUFFEREDWRITER - { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) }, + { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&mp_type_bufwriter) }, #endif }; diff --git a/py/objarray.c b/py/objarray.c index 4947f75905e7f..c27366d7208ef 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -634,7 +634,7 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t array_it_type = { +STATIC const mp_obj_type_t mp_type_array_it = { { &mp_type_type }, .name = MP_QSTR_iterator, .getiter = mp_identity_getiter, @@ -645,7 +645,7 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_bu assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t)); mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in); mp_obj_array_it_t *o = (mp_obj_array_it_t *)iter_buf; - o->base.type = &array_it_type; + o->base.type = &mp_type_array_it; o->array = array; o->offset = 0; o->cur = 0; diff --git a/py/objclosure.c b/py/objclosure.c index 9a8c7631c2bea..054b657896573 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -78,7 +78,7 @@ STATIC void closure_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ } #endif -const mp_obj_type_t closure_type = { +const mp_obj_type_t mp_type_closure = { { &mp_type_type }, .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_closure, @@ -90,7 +90,7 @@ const mp_obj_type_t closure_type = { mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed_over, const mp_obj_t *closed) { mp_obj_closure_t *o = m_new_obj_var(mp_obj_closure_t, mp_obj_t, n_closed_over); - o->base.type = &closure_type; + o->base.type = &mp_type_closure; o->fun = fun; o->n_closed = n_closed_over; memcpy(o->closed, closed, n_closed_over * sizeof(mp_obj_t)); diff --git a/py/objdict.c b/py/objdict.c index 4e51f259e7ff4..aea5952d3ddad 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -408,8 +408,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update); /******************************************************************************/ /* dict views */ -STATIC const mp_obj_type_t dict_view_type; -STATIC const mp_obj_type_t dict_view_it_type; +STATIC const mp_obj_type_t mp_type_dict_view; +STATIC const mp_obj_type_t mp_type_dict_view_it; typedef enum _mp_dict_view_kind_t { MP_DICT_VIEW_ITEMS, @@ -433,7 +433,7 @@ typedef struct _mp_obj_dict_view_t { } mp_obj_dict_view_t; STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) { - mp_check_self(mp_obj_is_type(self_in, &dict_view_it_type)); + mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view_it)); mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in); mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur); @@ -454,7 +454,7 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t dict_view_it_type = { +STATIC const mp_obj_type_t mp_type_dict_view_it = { { &mp_type_type }, .name = MP_QSTR_iterator, .getiter = mp_identity_getiter, @@ -463,10 +463,10 @@ STATIC const mp_obj_type_t dict_view_it_type = { STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t)); - mp_check_self(mp_obj_is_type(view_in, &dict_view_type)); + mp_check_self(mp_obj_is_type(view_in, &mp_type_dict_view)); mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in); mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf; - o->base.type = &dict_view_it_type; + o->base.type = &mp_type_dict_view_it; o->kind = view->kind; o->dict = view->dict; o->cur = 0; @@ -475,7 +475,7 @@ STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; - mp_check_self(mp_obj_is_type(self_in, &dict_view_type)); + mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view)); mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in); bool first = true; mp_print_str(print, mp_dict_view_names[self->kind]); @@ -505,7 +505,7 @@ STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t return dict_binary_op(op, o->dict, rhs_in); } -STATIC const mp_obj_type_t dict_view_type = { +STATIC const mp_obj_type_t mp_type_dict_view = { { &mp_type_type }, .name = MP_QSTR_dict_view, .print = dict_view_print, @@ -515,7 +515,7 @@ STATIC const mp_obj_type_t dict_view_type = { STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) { mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t); - o->base.type = &dict_view_type; + o->base.type = &mp_type_dict_view; o->dict = dict; o->kind = kind; return MP_OBJ_FROM_PTR(o); @@ -548,7 +548,7 @@ STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t)); mp_check_self(mp_obj_is_dict_or_ordereddict(self_in)); mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf; - o->base.type = &dict_view_it_type; + o->base.type = &mp_type_dict_view_it; o->kind = MP_DICT_VIEW_KEYS; o->dict = self_in; o->cur = 0; diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index 54e27b8f11d0b..2670314ab2f32 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -57,7 +57,7 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t it_type = { +STATIC const mp_obj_type_t mp_type_it = { { &mp_type_type }, .name = MP_QSTR_iterator, .getiter = mp_identity_getiter, @@ -68,7 +68,7 @@ STATIC const mp_obj_type_t it_type = { mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_getitem_iter_t) <= sizeof(mp_obj_iter_buf_t)); mp_obj_getitem_iter_t *o = (mp_obj_getitem_iter_t *)iter_buf; - o->base.type = &it_type; + o->base.type = &mp_type_it; o->args[0] = args[0]; o->args[1] = args[1]; o->args[2] = MP_OBJ_NEW_SMALL_INT(0); diff --git a/py/objrange.c b/py/objrange.c index 4eed4b9410550..1f028eb867a91 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -50,7 +50,7 @@ STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) { } } -STATIC const mp_obj_type_t range_it_type = { +STATIC const mp_obj_type_t mp_type_range_it = { { &mp_type_type }, .name = MP_QSTR_iterator, .getiter = mp_identity_getiter, @@ -60,7 +60,7 @@ STATIC const mp_obj_type_t range_it_type = { STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_range_it_t) <= sizeof(mp_obj_iter_buf_t)); mp_obj_range_it_t *o = (mp_obj_range_it_t *)iter_buf; - o->base.type = &range_it_type; + o->base.type = &mp_type_range_it; o->cur = cur; o->stop = stop; o->step = step; From ccc388f157eacfea6b5c44e1b6049a2bbeb44734 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 23 Feb 2021 22:56:44 +0000 Subject: [PATCH 0697/3533] rp2/mpthreadport.h: Cast core_state to _mp_state_thread_t. Required for user C++ code to build successfully against ports/rp2. Signed-off-by: Phil Howard --- ports/rp2/mpthreadport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/mpthreadport.h b/ports/rp2/mpthreadport.h index 4583f6f5391f8..5eb0bff396b98 100644 --- a/ports/rp2/mpthreadport.h +++ b/ports/rp2/mpthreadport.h @@ -41,7 +41,7 @@ static inline void mp_thread_set_state(struct _mp_state_thread_t *state) { } static inline struct _mp_state_thread_t *mp_thread_get_state(void) { - return core_state[get_core_num()]; + return (struct _mp_state_thread_t *)core_state[get_core_num()]; } static inline void mp_thread_mutex_init(mp_thread_mutex_t *m) { From 0cf12dd59c9ddddbd602d4267410033cb5a9d265 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 23 Feb 2021 22:57:14 +0000 Subject: [PATCH 0698/3533] rp2: Add support for USER_C_MODULES to CMake build system. The parts that are generic are added to py/ so they can be used by other ports that use CMake. py/usermod.cmake: * Creates a usermod target to hang user C/CXX modules from. * Gathers sources from user C/CXX modules and libs for QSTR scan. ports/rp2/CMakeLists.txt: * Includes py/usermod.cmake. * Links the resulting usermod library to the MicroPython target. py/mkrules.cmake: Add cxxflags to qstr.i.last custom command for CXX modules: * MICROPY_CPP_FLAGS so CXX modules will find includes. * -DNO_QSTR to fix fatal error missing "genhdr/qstrdefs.generated.h". Usage: The rp2 port can be linked against user C modules by running: make USER_C_MODULES=/path/to/module/micropython.cmake CMake will print a list of included modules. Co-authored-by: Graham Sanderson Co-authored-by: Michael O'Cleirigh Signed-off-by: Phil Howard --- ports/rp2/CMakeLists.txt | 6 +++++ ports/rp2/Makefile | 8 ++++++- py/mkrules.cmake | 2 +- py/usermod.cmake | 52 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 py/usermod.cmake diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 3968194dcbbfe..3db0e6b62a0ee 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -27,6 +27,8 @@ project(${MICROPY_TARGET}) pico_sdk_init() +include(${MICROPY_DIR}/py/usermod.cmake) + add_executable(${MICROPY_TARGET}) set(MICROPY_QSTRDEFS_PORT @@ -80,6 +82,7 @@ set(MICROPY_SOURCE_PORT set(MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_PY} ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_USERMOD} ${MICROPY_DIR}/lib/utils/mpirq.c ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c ${PROJECT_SOURCE_DIR}/machine_adc.c @@ -142,7 +145,10 @@ target_sources(${MICROPY_TARGET} PRIVATE ${MICROPY_SOURCE_PORT} ) +target_link_libraries(${MICROPY_TARGET} usermod) + target_include_directories(${MICROPY_TARGET} PRIVATE + ${MICROPY_INC_USERMOD} "${PROJECT_SOURCE_DIR}" "${MICROPY_DIR}" "${CMAKE_BINARY_DIR}" diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 08cd53dcca68c..3358c4ccae66e 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -6,8 +6,14 @@ BUILD = build $(VERBOSE)MAKESILENT = -s +CMAKE_ARGS = + +ifdef USER_C_MODULES +CMAKE_ARGS += -DUSER_C_MODULES=${USER_C_MODULES} +endif + all: - [ -d $(BUILD) ] || cmake -S . -B $(BUILD) -DPICO_BUILD_DOCS=0 + [ -d $(BUILD) ] || cmake -S . -B $(BUILD) -DPICO_BUILD_DOCS=0 ${CMAKE_ARGS} $(MAKE) $(MAKESILENT) -C $(BUILD) clean: diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 9c4c4afabe03e..f20240c62b299 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -69,7 +69,7 @@ add_custom_command( # it was last run, but it looks like it's not possible to specify that with cmake. add_custom_command( OUTPUT ${MICROPY_QSTRDEFS_LAST} - COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py pp ${CMAKE_C_COMPILER} -E output ${MICROPY_GENHDR_DIR}/qstr.i.last cflags ${MICROPY_CPP_FLAGS} -DNO_QSTR sources ${MICROPY_SOURCE_QSTR} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py pp ${CMAKE_C_COMPILER} -E output ${MICROPY_GENHDR_DIR}/qstr.i.last cflags ${MICROPY_CPP_FLAGS} -DNO_QSTR cxxflags ${MICROPY_CPP_FLAGS} -DNO_QSTR sources ${MICROPY_SOURCE_QSTR} DEPENDS ${MICROPY_MODULEDEFS} ${MICROPY_SOURCE_QSTR} VERBATIM diff --git a/py/usermod.cmake b/py/usermod.cmake new file mode 100644 index 0000000000000..853276283746d --- /dev/null +++ b/py/usermod.cmake @@ -0,0 +1,52 @@ +# Create a target for all user modules to link against. +add_library(usermod INTERFACE) + +function(usermod_gather_sources SOURCES_VARNAME INCLUDE_DIRECTORIES_VARNAME INCLUDED_VARNAME LIB) + if (NOT ${LIB} IN_LIST ${INCLUDED_VARNAME}) + list(APPEND ${INCLUDED_VARNAME} ${LIB}) + + # Gather library sources + get_target_property(lib_sources ${LIB} INTERFACE_SOURCES) + if (lib_sources) + list(APPEND ${SOURCES_VARNAME} ${lib_sources}) + endif() + + # Gather library includes + get_target_property(lib_include_directories ${LIB} INTERFACE_INCLUDE_DIRECTORIES) + if (lib_include_directories) + list(APPEND ${INCLUDE_DIRECTORIES_VARNAME} ${lib_include_directories}) + endif() + + # Recurse linked libraries + get_target_property(trans_depend ${LIB} INTERFACE_LINK_LIBRARIES) + if (trans_depend) + foreach(SUB_LIB ${trans_depend}) + usermod_gather_sources( + ${SOURCES_VARNAME} + ${INCLUDE_DIRECTORIES_VARNAME} + ${INCLUDED_VARNAME} + ${SUB_LIB}) + endforeach() + endif() + + set(${SOURCES_VARNAME} ${${SOURCES_VARNAME}} PARENT_SCOPE) + set(${INCLUDE_DIRECTORIES_VARNAME} ${${INCLUDE_DIRECTORIES_VARNAME}} PARENT_SCOPE) + set(${INCLUDED_VARNAME} ${${INCLUDED_VARNAME}} PARENT_SCOPE) + endif() +endfunction() + +# Include CMake files for user modules. +if (USER_C_MODULES) + foreach(USER_C_MODULE_PATH ${USER_C_MODULES}) + message("Including User C Module(s) from ${USER_C_MODULE_PATH}") + include(${USER_C_MODULE_PATH}) + endforeach() +endif() + +# Recursively gather sources for QSTR scanning - doesn't support generator expressions. +usermod_gather_sources(MICROPY_SOURCE_USERMOD MICROPY_INC_USERMOD found_modules usermod) + +# Report found modules. +list(REMOVE_ITEM found_modules "usermod") +list(JOIN found_modules ", " found_modules) +message("Found User C Module(s): ${found_modules}") From cc497d4c6a7bbf491fbc57b9447632bddff8d566 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 23 Feb 2021 23:06:04 +0000 Subject: [PATCH 0699/3533] examples/usercmodule: Add micropython.cmake to the C and CPP examples. examples/usercmodule/micropython.cmake: Root micropython.cmake file is responsible for including modules. examples/usercmodule/cexample/micropython.cmake: examples/usercmodule/cppexample/micropython.cmake: Module micropython.cmake files define the target and link it to usermod. Signed-off-by: Phil Howard --- .../usercmodule/cexample/micropython.cmake | 21 ++++++++++++++++++ .../usercmodule/cppexample/micropython.cmake | 22 +++++++++++++++++++ examples/usercmodule/micropython.cmake | 11 ++++++++++ 3 files changed, 54 insertions(+) create mode 100644 examples/usercmodule/cexample/micropython.cmake create mode 100644 examples/usercmodule/cppexample/micropython.cmake create mode 100644 examples/usercmodule/micropython.cmake diff --git a/examples/usercmodule/cexample/micropython.cmake b/examples/usercmodule/cexample/micropython.cmake new file mode 100644 index 0000000000000..371a3eefa4726 --- /dev/null +++ b/examples/usercmodule/cexample/micropython.cmake @@ -0,0 +1,21 @@ +# Create an INTERFACE library for our C module. +add_library(usermod_cexample INTERFACE) + +# Add our source files to the lib +target_sources(usermod_cexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/examplemodule.c +) + +# Add the current directory as an include directory. +target_include_directories(usermod_cexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR} +) + +# Enable the module automatically by adding the relevant compile definitions. +target_compile_definitions(usermod_cexample INTERFACE + MODULE_CEXAMPLE_ENABLED=1 +) + +# Link our INTERFACE library to the usermod target. +target_link_libraries(usermod INTERFACE usermod_cexample) + diff --git a/examples/usercmodule/cppexample/micropython.cmake b/examples/usercmodule/cppexample/micropython.cmake new file mode 100644 index 0000000000000..43e8d887f546e --- /dev/null +++ b/examples/usercmodule/cppexample/micropython.cmake @@ -0,0 +1,22 @@ +# Create an INTERFACE library for our CPP module. +add_library(usermod_cppexample INTERFACE) + +# Add our source files to the library. +target_sources(usermod_cppexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/example.cpp + ${CMAKE_CURRENT_LIST_DIR}/examplemodule.c +) + +# Add the current directory as an include directory. +target_include_directories(usermod_cppexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR} +) + +# Enable the module automatically by adding the relevant compile definitions. +target_compile_definitions(usermod_cppexample INTERFACE + MODULE_CPPEXAMPLE_ENABLED=1 +) + +# Link our INTERFACE library to the usermod target. +target_link_libraries(usermod INTERFACE usermod_cppexample) + diff --git a/examples/usercmodule/micropython.cmake b/examples/usercmodule/micropython.cmake new file mode 100644 index 0000000000000..ad88d77806c4e --- /dev/null +++ b/examples/usercmodule/micropython.cmake @@ -0,0 +1,11 @@ +# This top-level micropython.cmake is responsible for listing +# the individual modules we want to include. +# Paths are absolute, and ${CMAKE_CURRENT_LIST_DIR} can be +# used to prefix subdirectories. + +# Add the C example. +include(${CMAKE_CURRENT_LIST_DIR}/cexample/micropython.cmake) + +# Add the CPP example. +include(${CMAKE_CURRENT_LIST_DIR}/cppexample/micropython.cmake) + From 8e5756e2b6d03805bf9f40cd1128c0cd6214e07c Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 24 Feb 2021 12:57:53 +0000 Subject: [PATCH 0700/3533] docs/develop/cmodules.rst: Document C-modules and micropython.cmake. Documents the micropython.cmake file required to make user C modules compatible with the CMake build system. Signed-off-by: Phil Howard --- docs/develop/cmodules.rst | 76 ++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 2db1f65f2e488..dd02f1439c053 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -53,6 +53,30 @@ A MicroPython user C module is a directory with the following files: for header files), these should be added to ``CFLAGS_USERMOD`` for C code and to ``CXXFLAGS_USERMOD`` for C++ code. +* ``micropython.cmake`` contains the CMake configuration for this module. + + In ``micropython.cmake``, you may use ``${CMAKE_CURRENT_LIST_DIR}`` as the path to + the current module. + + Your ``micropython.cmake`` should define an ``INTERFACE`` library and associate + your source files, compile definitions and include directories with it. + The library should then be linked to the ``usermod`` target. + + .. code-block:: cmake + + add_library(usermod_cexample INTERFACE) + + target_sources(usermod_cexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/examplemodule.c + ) + + target_include_directories(usermod_cexample INTERFACE + ${CMAKE_CURRENT_LIST_DIR} + ) + + target_link_libraries(usermod INTERFACE usermod_cexample) + + See below for full usage example. @@ -70,9 +94,11 @@ and has a source file and a Makefile fragment with content as descibed above:: └──usercmodule/ └──cexample/ ├── examplemodule.c - └── micropython.mk + ├── micropython.mk + └── micropython.cmake -Refer to the comments in these 2 files for additional explanation. + +Refer to the comments in these files for additional explanation. Next to the ``cexample`` module there's also ``cppexample`` which works in the same way but shows one way of mixing C and C++ code in MicroPython. @@ -97,10 +123,13 @@ applying 2 modifications: ├── modules/ │ └──example1/ │ ├──example1.c - │ └──micropython.mk + │ ├──micropython.mk + │ └──micropython.cmake │ └──example2/ │ ├──example2.c - │ └──micropython.mk + │ ├──micropython.mk + │ └──micropython.cmake + │ └──micropython.cmake └── micropython/ ├──ports/ ... ├──stm32/ @@ -109,10 +138,21 @@ applying 2 modifications: with ``USER_C_MODULES`` set to the ``my_project/modules`` directory. -- all modules found in this directory will be compiled, but only those - which are explicitly enabled will be availabe for importing. Enabling a - module is done by setting the preprocessor define from its module - registration to 1. For example if the source code defines the module with + A top level ``micropython.cmake`` - found directly in the ``my_project/modules`` + directory - should ``include`` all of your modules. + + .. code-block:: cmake + + include(${CMAKE_CURRENT_LIST_DIR}/example1/micropython.cmake) + include(${CMAKE_CURRENT_LIST_DIR}/example2/micropython.cmake) + + +- all modules found in this directory (or added via ``include`` in the top-level + ``micropython.cmake`` when using CMake) will be compiled, but only those which are + explicitly enabled will be available for importing. Enabling a module is done + by setting the preprocessor define from its module registration to 1. + + For example if the source code defines the module with .. code-block:: c @@ -149,6 +189,26 @@ The build output will show the modules found:: ... +For a CMake-based port such as rp2, this will look a little different: + +.. code-block:: bash + + cd micropython/ports/rp2 + make USER_C_MODULES=../../examples/usercmodule all + + +The CMake build output lists the modules by name:: + + ... + Including User C Module(s) from ../../examples/usercmodule/micropython.cmake + Found User C Module(s): usermod_cexample, usermod_cppexample + ... + + +Note that the ``micropython.cmake`` files define ``DMODULE__ENABLED=1`` automatically. +The top-level ``micropython.cmake`` can be used to control which modules are enabled. + + Or for your own project with a directory structure as shown above, including both modules and building the stm32 port for example: From 5976ea02a50ea76d72ada40db4cda186147fb412 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 23 Mar 2021 12:25:27 +0000 Subject: [PATCH 0701/3533] tools/ci.sh: Add CI for CMake USER_C_MODULE support. Builds the rp2 port against the example C and CXX modules. Signed-off-by: Phil Howard --- tools/ci.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/ci.sh b/tools/ci.sh index 53cf7f878b24c..0450611a10b43 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -181,6 +181,8 @@ function ci_rp2_build { make ${MAKEOPTS} -C mpy-cross git submodule update --init lib/pico-sdk lib/tinyusb make ${MAKEOPTS} -C ports/rp2 + make ${MAKEOPTS} -C ports/rp2 clean + make ${MAKEOPTS} -C ports/rp2 USER_C_MODULES=../../examples/usercmodule/micropython.cmake } ######################################################################################## From ca3d51f12206a88463f2b2a2d27756a1b519cb6a Mon Sep 17 00:00:00 2001 From: Liam Fraser Date: Tue, 30 Mar 2021 11:18:11 +0100 Subject: [PATCH 0702/3533] rp2: Don't advertise remote wakeup for USB serial. This USB feature is currently not supported. With this flag enabled (and the feature not implemented) the USB serial will stop working if there is a delay of more than about 2 seconds between messages, which can occur with USB autosuspend enabled. Fixes issue #6866. --- ports/rp2/tusb_port.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/tusb_port.c b/ports/rp2/tusb_port.c index afb566bdb4a2f..94856ab452702 100644 --- a/ports/rp2/tusb_port.c +++ b/ports/rp2/tusb_port.c @@ -68,7 +68,7 @@ static const tusb_desc_device_t usbd_desc_device = { static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN, - TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA), + 0, USBD_MAX_POWER_MA), TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), From 0ccd9e08aad95f955ded0006cff67e2a50778c84 Mon Sep 17 00:00:00 2001 From: Michael O'Cleirigh Date: Sat, 27 Mar 2021 17:10:39 -0400 Subject: [PATCH 0703/3533] esp32: Restore USER_C_MODULE support with new CMake build system. Support for User C and C++ modules was lost due to upgrading the esp32 to the latest CMake based IDF from the GNUMakefile build process. Restore the support for the esp32 port by integrating with the approach recently added for the rp2 port. Signed-off-by: Michael O'Cleirigh --- ports/esp32/Makefile | 8 +++++++- ports/esp32/main/CMakeLists.txt | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 9a3a15d6ee6c8..39b740ffae074 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -18,7 +18,13 @@ GIT_SUBMODULES = lib/berkeley-db-1.xx .PHONY: all clean deploy erase submodules FORCE -IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -B $(BUILD) +CMAKE_ARGS = + +ifdef USER_C_MODULES + CMAKE_ARGS += -DUSER_C_MODULES=${USER_C_MODULES} +endif + +IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -B $(BUILD) $(CMAKE_ARGS) all: idf.py $(IDFPY_FLAGS) build diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index b6bccdde3ca9b..082410f4c0f5c 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -5,6 +5,10 @@ get_filename_component(MICROPY_DIR ${PROJECT_DIR}/../.. ABSOLUTE) include(${MICROPY_DIR}/py/py.cmake) include(${MICROPY_DIR}/extmod/extmod.cmake) +if(NOT CMAKE_BUILD_EARLY_EXPANSION) + include(${MICROPY_DIR}/py/usermod.cmake) +endif() + set(MICROPY_QSTRDEFS_PORT ${PROJECT_DIR}/qstrdefsport.h ) @@ -71,6 +75,7 @@ set(MICROPY_SOURCE_PORT set(MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_PY} ${MICROPY_SOURCE_EXTMOD} + ${MICROPY_SOURCE_USERMOD} ${MICROPY_SOURCE_LIB} ${MICROPY_SOURCE_PORT} ) @@ -129,6 +134,7 @@ idf_component_register( ${MICROPY_SOURCE_PORT} INCLUDE_DIRS ${MICROPY_DIR} + ${MICROPY_INC_USERMOD} ${MICROPY_PORT_DIR} ${MICROPY_BOARD_DIR} ${CMAKE_BINARY_DIR} @@ -159,6 +165,10 @@ target_compile_options(${MICROPY_TARGET} PUBLIC -Wno-missing-field-initializers ) +# add usermod +target_link_libraries(${MICROPY_TARGET} usermod) + + # Collect all of the include directories and compile definitions for the IDF components. foreach(comp ${IDF_COMPONENTS}) get_target_property(type __idf_${comp} TYPE) From 17b1f8212199ea6344e289e7fdea9585e30765d6 Mon Sep 17 00:00:00 2001 From: Michael O'Cleirigh Date: Sat, 27 Mar 2021 17:13:00 -0400 Subject: [PATCH 0704/3533] tools/ci.sh: Build user C modules for esp32. Builds the esp32 port against the example C and CXX modules. Signed-off-by: Michael O'Cleirigh --- tools/ci.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/ci.sh b/tools/ci.sh index 0450611a10b43..1a410632cdd2d 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -100,6 +100,8 @@ function ci_esp32_build { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/esp32 submodules make ${MAKEOPTS} -C ports/esp32 + make ${MAKEOPTS} -C ports/esp32 clean + make ${MAKEOPTS} -C ports/esp32 USER_C_MODULES=../../../examples/usercmodule/micropython.cmake } ######################################################################################## From ec79e445027857d219d139cd31af578124a8e913 Mon Sep 17 00:00:00 2001 From: Michael O'Cleirigh Date: Tue, 30 Mar 2021 23:03:31 -0400 Subject: [PATCH 0705/3533] esp32: Fix multiple definition errors with mp_hal_stdout_tx functions. It was noticed that the esp32 port didn't build ulab correctly. The problem was a multiple defintion of the 'mp_hal_stdout_tx_str' and 'mp_hal_stdout_tx_strn_cooked' functions. They were defined in stdout_helpers.c but also in the ports/esp32/mphalport.c. Fixed by removing stdout_helpers.c from the build. Signed-off-by: Michael O'Cleirigh --- ports/esp32/main/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 082410f4c0f5c..d7d08ea663df4 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -25,7 +25,6 @@ set(MICROPY_SOURCE_LIB ${MICROPY_DIR}/lib/oofatfs/ffunicode.c ${MICROPY_DIR}/lib/timeutils/timeutils.c ${MICROPY_DIR}/lib/utils/interrupt_char.c - ${MICROPY_DIR}/lib/utils/stdout_helpers.c ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c ${MICROPY_DIR}/lib/utils/pyexec.c ) From d87f42b0e53829052f17955ba1e17f938ae486fb Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 31 Mar 2021 00:45:43 +1100 Subject: [PATCH 0706/3533] examples/usercmodules: Simplify user C module enabling. It's a bit of a pitfall with user C modules that including them in the build does not automatically enable them. This commit changes the docs and examples for user C modules to encourage writers of user C modules to enable them unconditionally. This makes things simpler and covers most use cases. See discussion in issue #6960, and also #7086. Signed-off-by: Damien George --- docs/develop/cmodules.rst | 20 +++++++++++-------- examples/usercmodule/cexample/examplemodule.c | 5 ++++- .../usercmodule/cexample/micropython.cmake | 6 ------ .../usercmodule/cppexample/examplemodule.c | 5 ++++- .../usercmodule/cppexample/micropython.cmake | 6 ------ .../unix/variants/coverage/mpconfigvariant.mk | 3 +-- tools/ci.sh | 2 +- 7 files changed, 22 insertions(+), 25 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index dd02f1439c053..be49485f2cd26 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -149,17 +149,23 @@ applying 2 modifications: - all modules found in this directory (or added via ``include`` in the top-level ``micropython.cmake`` when using CMake) will be compiled, but only those which are - explicitly enabled will be available for importing. Enabling a module is done - by setting the preprocessor define from its module registration to 1. + enabled will be available for importing. If a module is to always be enabled, + which is usually the case for custom modules and custom builds, then it is + enough to supply "1" as the third parameter to the registration macro, like: - For example if the source code defines the module with + .. code-block:: c + + MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1); + + Alternatively, to make the module disabled by default but selectable through + a preprocessor configuration option, use: .. code-block:: c MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); - then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available. + Then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available. This can be done by adding ``CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1`` to the ``make`` command, or editing ``mpconfigport.h`` or ``mpconfigboard.h`` to add @@ -179,7 +185,7 @@ directory can be built for the unix port: .. code-block:: bash cd micropython/ports/unix - make USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1 all + make USER_C_MODULES=../../examples/usercmodule all The build output will show the modules found:: @@ -205,7 +211,6 @@ The CMake build output lists the modules by name:: ... -Note that the ``micropython.cmake`` files define ``DMODULE__ENABLED=1`` automatically. The top-level ``micropython.cmake`` can be used to control which modules are enabled. @@ -215,8 +220,7 @@ including both modules and building the stm32 port for example: .. code-block:: bash cd my_project/micropython/ports/stm32 - make USER_C_MODULES=../../../modules \ - CFLAGS_EXTRA="-DMODULE_EXAMPLE1_ENABLED=1 -DMODULE_EXAMPLE2_ENABLED=1" all + make USER_C_MODULES=../../../modules all Module usage in MicroPython diff --git a/examples/usercmodule/cexample/examplemodule.c b/examples/usercmodule/cexample/examplemodule.c index f608823c9e51c..49ebc7aaaf9d2 100644 --- a/examples/usercmodule/cexample/examplemodule.c +++ b/examples/usercmodule/cexample/examplemodule.c @@ -31,4 +31,7 @@ const mp_obj_module_t example_user_cmodule = { }; // Register the module to make it available in Python. -MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); +// Note: the "1" in the third argument means this module is always enabled. +// This "1" can be optionally replaced with a macro like MODULE_CEXAMPLE_ENABLED +// which can then be used to conditionally enable this module. +MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1); diff --git a/examples/usercmodule/cexample/micropython.cmake b/examples/usercmodule/cexample/micropython.cmake index 371a3eefa4726..ba076a16b246b 100644 --- a/examples/usercmodule/cexample/micropython.cmake +++ b/examples/usercmodule/cexample/micropython.cmake @@ -11,11 +11,5 @@ target_include_directories(usermod_cexample INTERFACE ${CMAKE_CURRENT_LIST_DIR} ) -# Enable the module automatically by adding the relevant compile definitions. -target_compile_definitions(usermod_cexample INTERFACE - MODULE_CEXAMPLE_ENABLED=1 -) - # Link our INTERFACE library to the usermod target. target_link_libraries(usermod INTERFACE usermod_cexample) - diff --git a/examples/usercmodule/cppexample/examplemodule.c b/examples/usercmodule/cppexample/examplemodule.c index ceb588bef6b30..dfb7856837613 100644 --- a/examples/usercmodule/cppexample/examplemodule.c +++ b/examples/usercmodule/cppexample/examplemodule.c @@ -22,4 +22,7 @@ const mp_obj_module_t cppexample_user_cmodule = { }; // Register the module to make it available in Python. -MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, MODULE_CPPEXAMPLE_ENABLED); +// Note: the "1" in the third argument means this module is always enabled. +// This "1" can be optionally replaced with a macro like MODULE_CPPEXAMPLE_ENABLED +// which can then be used to conditionally enable this module. +MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, 1); diff --git a/examples/usercmodule/cppexample/micropython.cmake b/examples/usercmodule/cppexample/micropython.cmake index 43e8d887f546e..6da972c94e3b5 100644 --- a/examples/usercmodule/cppexample/micropython.cmake +++ b/examples/usercmodule/cppexample/micropython.cmake @@ -12,11 +12,5 @@ target_include_directories(usermod_cppexample INTERFACE ${CMAKE_CURRENT_LIST_DIR} ) -# Enable the module automatically by adding the relevant compile definitions. -target_compile_definitions(usermod_cppexample INTERFACE - MODULE_CPPEXAMPLE_ENABLED=1 -) - # Link our INTERFACE library to the usermod target. target_link_libraries(usermod INTERFACE usermod_cppexample) - diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index ef81975d9da8c..fac8c0d2752e0 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -7,8 +7,7 @@ CFLAGS += \ -fprofile-arcs -ftest-coverage \ -Wformat -Wmissing-declarations -Wmissing-prototypes \ -Wold-style-definition -Wpointer-arith -Wshadow -Wuninitialized -Wunused-parameter \ - -DMICROPY_UNIX_COVERAGE \ - -DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1 + -DMICROPY_UNIX_COVERAGE LDFLAGS += -fprofile-arcs -ftest-coverage diff --git a/tools/ci.sh b/tools/ci.sh index 1a410632cdd2d..3ce96a96a9084 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -211,7 +211,7 @@ function ci_stm32_pyb_build { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/stm32 submodules git submodule update --init lib/btstack - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA="-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1" + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' From f541b3673d272fbe9081a96776072a83ac5add9d Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Apr 2021 17:07:20 +1100 Subject: [PATCH 0707/3533] docs/develop: Improve user C modules to properly describe how to build. Make and CMake builds are slightly different and these changes help make it clear what to do in each case. Signed-off-by: Damien George --- docs/develop/cmodules.rst | 176 ++++++++++++++++++++++---------------- 1 file changed, 102 insertions(+), 74 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index be49485f2cd26..346b3e03140de 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -18,7 +18,11 @@ If however you're targeting obscure or proprietary systems it may make more sense to keep this external to the main MicroPython repository. This chapter describes how to compile such external modules into the -MicroPython executable or firmware image. +MicroPython executable or firmware image. Both Make and CMake build +tools are supported, and when writing an external module it's a good idea to +add the build files for both of these tools so the module can be used on all +ports. But when compiling a particular port you will only need to use one +method of building, either Make or CMake. An alternative approach is to use :ref:`natmod` which allows writing custom C code that is placed in a .mpy file, which can be imported dynamically in to @@ -111,116 +115,140 @@ To build such a module, compile MicroPython (see `getting started `_), applying 2 modifications: -- an extra ``make`` flag named ``USER_C_MODULES`` set to the directory - containing all modules you want included (not to the module itself). - For building the example modules which come with MicroPython, - set ``USER_C_MODULES`` to the ``examples/usercmodule`` directory. - For your own projects it's more convenient to keep custom code out of - the main source tree so a typical project directory structure will look - like this:: +1. Set the build-time flag ``USER_C_MODULES`` to point to the modules + you want to include. For ports that use Make this variable should be a + directory which is searched automatically for modules. For ports that + use CMake this variable should be a file which includes the modules to + build. See below for details. - my_project/ - ├── modules/ - │ └──example1/ - │ ├──example1.c - │ ├──micropython.mk - │ └──micropython.cmake - │ └──example2/ - │ ├──example2.c - │ ├──micropython.mk - │ └──micropython.cmake - │ └──micropython.cmake - └── micropython/ - ├──ports/ - ... ├──stm32/ - ... +2. Enable the modules by setting the corresponding C preprocessor macro to + 1. This is only needed if the modules you are building are not + automatically enabled. +For building the example modules which come with MicroPython, +set ``USER_C_MODULES`` to the ``examples/usercmodule`` directory for Make, +or to ``examples/usercmodule/micropython.cmake`` for CMake. - with ``USER_C_MODULES`` set to the ``my_project/modules`` directory. +For example, here's how the to build the unix port with the example modules: - A top level ``micropython.cmake`` - found directly in the ``my_project/modules`` - directory - should ``include`` all of your modules. +.. code-block:: bash - .. code-block:: cmake + cd micropython/ports/unix + make USER_C_MODULES=../../examples/usercmodule - include(${CMAKE_CURRENT_LIST_DIR}/example1/micropython.cmake) - include(${CMAKE_CURRENT_LIST_DIR}/example2/micropython.cmake) +You may need to run ``make clean`` once at the start when including new +user modules in the build. The build output will show the modules found:: + ... + Including User C Module from ../../examples/usercmodule/cexample + Including User C Module from ../../examples/usercmodule/cppexample + ... -- all modules found in this directory (or added via ``include`` in the top-level - ``micropython.cmake`` when using CMake) will be compiled, but only those which are - enabled will be available for importing. If a module is to always be enabled, - which is usually the case for custom modules and custom builds, then it is - enough to supply "1" as the third parameter to the registration macro, like: +For a CMake-based port such as rp2, this will look a little different (note +that CMake is actually invoked by ``make``): - .. code-block:: c +.. code-block:: bash - MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1); + cd micropython/ports/rp2 + make USER_C_MODULES=../../examples/usercmodule/micropython.cmake - Alternatively, to make the module disabled by default but selectable through - a preprocessor configuration option, use: +Again, you may need to run ``make clean`` first for CMake to pick up the +user modules. The CMake build output lists the modules by name:: - .. code-block:: c + ... + Including User C Module(s) from ../../examples/usercmodule/micropython.cmake + Found User C Module(s): usermod_cexample, usermod_cppexample + ... - MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); +The contents of the top-level ``micropython.cmake`` can be used to control which +modules are enabled. +For your own projects it's more convenient to keep custom code out of the main +MicroPython source tree, so a typical project directory structure will look +like this:: - Then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available. - This can be done by adding ``CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1`` to - the ``make`` command, or editing ``mpconfigport.h`` or ``mpconfigboard.h`` - to add + my_project/ + ├── modules/ + │ ├── example1/ + │ │ ├── example1.c + │ │ ├── micropython.mk + │ │ └── micropython.cmake + │ ├── example2/ + │ │ ├── example2.c + │ │ ├── micropython.mk + │ │ └── micropython.cmake + │ └── micropython.cmake + └── micropython/ + ├──ports/ + ... ├──stm32/ + ... - .. code-block:: c +When building with Make set ``USER_C_MODULES`` to the ``my_project/modules`` +directory. For example, building the stm32 port: - #define MODULE_CEXAMPLE_ENABLED (1) +.. code-block:: bash + cd my_project/micropython/ports/stm32 + make USER_C_MODULES=../../../modules - Note that the exact method depends on the port as they have different - structures. If not done correctly it will compile but importing will - fail to find the module. +When building with CMake the top level ``micropython.cmake`` -- found directly +in the ``my_project/modules`` directory -- should ``include`` all of the modules +you want to have available: -To sum up, here's how the ``cexample`` module from the ``examples/usercmodule`` -directory can be built for the unix port: + .. code-block:: cmake -.. code-block:: bash + include(${CMAKE_CURRENT_LIST_DIR}/example1/micropython.cmake) + include(${CMAKE_CURRENT_LIST_DIR}/example2/micropython.cmake) - cd micropython/ports/unix - make USER_C_MODULES=../../examples/usercmodule all +Then build with: -The build output will show the modules found:: +.. code-block:: bash - ... - Including User C Module from ../../examples/usercmodule/cexample - Including User C Module from ../../examples/usercmodule/cppexample - ... + cd my_project/micropython/ports/esp32 + make USER_C_MODULES=../../../../modules/micropython.cmake +Note that the esp32 port needs the extra ``..`` for relative paths due to the +location of its main ``CMakeLists.txt`` file. You can also specify absolute +paths to ``USER_C_MODULES``. -For a CMake-based port such as rp2, this will look a little different: +All modules specified by the ``USER_C_MODULES`` variable (either found in this +directory when using Make, or added via ``include`` when using CMake) will be +compiled, but only those which are enabled will be available for importing. +User modules are usually enabled by default (this is decided by the developer +of the module), in which case there is nothing more to do than set ``USER_C_MODULES`` +as described above. -.. code-block:: bash +If a module is not enabled by default then the corresponding C preprocessor macro +must be enabled. This macro name can be found by searching for the ``MP_REGISTER_MODULE`` +line in the module's source code (it usually appears at the end of the main source file). +The third argument to ``MP_REGISTER_MODULE`` is the macro name, and this must be set +to 1 using ``CFLAGS_EXTRA`` to make the module available. If the third argument is just +the number 1 then the module is enabled by default. - cd micropython/ports/rp2 - make USER_C_MODULES=../../examples/usercmodule all +For example, the ``examples/usercmodule/cexample`` module is enabled by default so +has the following line in its source code: + .. code-block:: c -The CMake build output lists the modules by name:: + MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1); - ... - Including User C Module(s) from ../../examples/usercmodule/micropython.cmake - Found User C Module(s): usermod_cexample, usermod_cppexample - ... +Alternatively, to make this module disabled by default but selectable through +a preprocessor configuration option, it would be: + .. code-block:: c -The top-level ``micropython.cmake`` can be used to control which modules are enabled. + MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED); +In this case the module is enabled by adding ``CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1`` +to the ``make`` command, or editing ``mpconfigport.h`` or ``mpconfigboard.h`` to add -Or for your own project with a directory structure as shown above, -including both modules and building the stm32 port for example: + .. code-block:: c -.. code-block:: bash + #define MODULE_CEXAMPLE_ENABLED (1) - cd my_project/micropython/ports/stm32 - make USER_C_MODULES=../../../modules all +Note that the exact method depends on the port as they have different +structures. If not done correctly it will compile but importing will +fail to find the module. Module usage in MicroPython From 2d8aecd2ad43afeeb14fb8a21fcff29a1000c5c4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 1 Apr 2021 13:36:26 +1100 Subject: [PATCH 0708/3533] rp2/CMakeLists.txt: Enable USB enumeration fix. This is a workaround for errata RP2040-E5, and is needed to make USB more reliable on certain USB ports. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 3db0e6b62a0ee..37b041279f503 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -170,6 +170,7 @@ target_compile_definitions(${MICROPY_TARGET} PRIVATE PICO_NO_PROGRAM_VERSION_STRING=1 # do it ourselves in main.c MICROPY_BUILD_TYPE="${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} ${CMAKE_BUILD_TYPE}" PICO_NO_BI_STDIO_UART=1 # we call it UART REPL + PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1 ) target_link_libraries(${MICROPY_TARGET} From 25ae169e6ec5ada4d80f6632b72b711c0b5e4c98 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Apr 2021 12:11:15 +1000 Subject: [PATCH 0709/3533] stm32: Include .ARM section in firmware for C++ exception handling. Support for C++ was added in 97960dc7deb7a0e691fca5944402cd03386b744b but that commit didn't include the C++ exception handling table in the binary firmware image. This commit fixes that. Signed-off-by: Damien George --- ports/stm32/Makefile | 4 ++-- ports/stm32/boards/PYBD_SF2/mpconfigboard.mk | 2 +- ports/stm32/boards/PYBD_SF3/mpconfigboard.mk | 2 +- ports/stm32/boards/STM32F769DISC/mpconfigboard.mk | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 6c71f2757333f..9c22928a0c7ba 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -643,7 +643,7 @@ TEXT0_ADDR ?= 0x08000000 ifeq ($(TEXT1_ADDR),) # No TEXT1_ADDR given so put all firmware at TEXT0_ADDR location -TEXT0_SECTIONS ?= .isr_vector .text .data +TEXT0_SECTIONS ?= .isr_vector .text .data .ARM deploy-stlink: $(BUILD)/firmware.bin $(call RUN_STLINK,$^,$(TEXT0_ADDR)) @@ -661,7 +661,7 @@ else # TEXT0_ADDR and TEXT1_ADDR are specified so split firmware between these locations TEXT0_SECTIONS ?= .isr_vector -TEXT1_SECTIONS ?= .text .data +TEXT1_SECTIONS ?= .text .data .ARM deploy-stlink: $(BUILD)/firmware0.bin $(BUILD)/firmware1.bin $(call RUN_STLINK,$(word 1,$^),$(TEXT0_ADDR)) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk b/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk index ead8bb8c07cf3..9917aa2c5730a 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk @@ -6,7 +6,7 @@ AF_FILE = boards/stm32f722_af.csv LD_FILES = boards/PYBD_SF2/f722_qspi.ld TEXT0_ADDR = 0x08008000 TEXT1_ADDR = 0x90000000 -TEXT0_SECTIONS = .isr_vector .text .data +TEXT0_SECTIONS = .isr_vector .text .data .ARM TEXT1_SECTIONS = .text_ext # MicroPython settings diff --git a/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk b/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk index abe3dcd862373..6bcc68f4b192e 100644 --- a/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk +++ b/ports/stm32/boards/PYBD_SF3/mpconfigboard.mk @@ -6,7 +6,7 @@ AF_FILE = boards/stm32f722_af.csv LD_FILES = boards/PYBD_SF2/f722_qspi.ld TEXT0_ADDR = 0x08008000 TEXT1_ADDR = 0x90000000 -TEXT0_SECTIONS = .isr_vector .text .data +TEXT0_SECTIONS = .isr_vector .text .data .ARM TEXT1_SECTIONS = .text_ext # MicroPython settings diff --git a/ports/stm32/boards/STM32F769DISC/mpconfigboard.mk b/ports/stm32/boards/STM32F769DISC/mpconfigboard.mk index 81add8c88e772..5d3d11a7958fe 100644 --- a/ports/stm32/boards/STM32F769DISC/mpconfigboard.mk +++ b/ports/stm32/boards/STM32F769DISC/mpconfigboard.mk @@ -22,7 +22,7 @@ ifeq ($(USE_QSPI_XIP),1) LD_FILES = boards/STM32F769DISC/f769_qspi.ld TEXT0_ADDR = 0x08020000 TEXT1_ADDR = 0x90000000 -TEXT0_SECTIONS = .isr_vector .text .data +TEXT0_SECTIONS = .isr_vector .text .data .ARM TEXT1_SECTIONS = .text_qspi else From d35f12f5caf2f6eef08670b61dbe2a3a1b387042 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Apr 2021 12:50:19 +1000 Subject: [PATCH 0710/3533] tools/metrics.py: Fix esp32 output filename due to move to CMake. Signed-off-by: Damien George --- tools/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/metrics.py b/tools/metrics.py index 25acb30f51f01..98291e25a9f06 100755 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -65,7 +65,7 @@ def __init__(self, name, dir, output, make_flags=None): "s": PortData("stm32", "stm32", "build-PYBV10/firmware.elf", "BOARD=PYBV10"), "c": PortData("cc3200", "cc3200", "build/WIPY/release/application.axf", "BTARGET=application"), "8": PortData("esp8266", "esp8266", "build-GENERIC/firmware.elf"), - "3": PortData("esp32", "esp32", "build-GENERIC/application.elf"), + "3": PortData("esp32", "esp32", "build-GENERIC/micropython.elf"), "r": PortData("nrf", "nrf", "build-pca10040/firmware.elf"), "d": PortData("samd", "samd", "build-ADAFRUIT_ITSYBITSY_M4_EXPRESS/firmware.elf"), } From 172fb5230a3943eeb6fbbb4de1dc56b16e2a7637 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 27 Mar 2021 19:19:51 -0500 Subject: [PATCH 0711/3533] extmod/re1.5: Check and report byte overflow errors in _compilecode. The generated regex code is limited in the range of jumps and counts, and this commit checks all cases which can overflow given the right kind of input regex, and returns an error in such a case. This change assumes that the results that overflow an int8_t do not overflow a platform int. Closes: #7078 Signed-off-by: Jeff Epler --- extmod/re1.5/compilecode.c | 31 ++++++++++++++++++++----------- tests/extmod/ure_limit.py | 34 ++++++++++++++++++++++++++++++++++ tests/extmod/ure_limit.py.exp | 7 +++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 tests/extmod/ure_limit.py create mode 100644 tests/extmod/ure_limit.py.exp diff --git a/extmod/re1.5/compilecode.c b/extmod/re1.5/compilecode.c index c4d12af87a32e..add4f6ac20859 100644 --- a/extmod/re1.5/compilecode.c +++ b/extmod/re1.5/compilecode.c @@ -8,11 +8,20 @@ ((code ? memmove(code + at + num, code + at, pc - at) : 0), pc += num) #define REL(at, to) (to - at - 2) #define EMIT(at, byte) (code ? (code[at] = byte) : (at)) +#define EMIT_CHECKED(at, byte) (_emit_checked(at, code, byte, &err)) #define PC (prog->bytelen) +static void _emit_checked(int at, char *code, int val, bool *err) { + *err |= val != (int8_t)val; + if (code) { + code[at] = val; + } +} + static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) { char *code = sizecode ? NULL : prog->insts; + bool err = false; int start = PC; int term = PC; int alt_label = 0; @@ -64,7 +73,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) } EMIT(PC++, *re); } - EMIT(term + 1, cnt); + EMIT_CHECKED(term + 1, cnt); break; } case '(': { @@ -75,7 +84,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) if (capture) { sub = ++prog->sub; EMIT(PC++, Save); - EMIT(PC++, 2 * sub); + EMIT_CHECKED(PC++, 2 * sub); prog->len++; } else { re += 2; @@ -86,7 +95,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) if (capture) { EMIT(PC++, Save); - EMIT(PC++, 2 * sub + 1); + EMIT_CHECKED(PC++, 2 * sub + 1); prog->len++; } @@ -101,7 +110,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) } else { EMIT(term, Split); } - EMIT(term + 1, REL(term, PC)); + EMIT_CHECKED(term + 1, REL(term, PC)); prog->len++; term = PC; break; @@ -109,7 +118,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) if (PC == term) return NULL; // nothing to repeat INSERT_CODE(term, 2, PC); EMIT(PC, Jmp); - EMIT(PC + 1, REL(PC, term)); + EMIT_CHECKED(PC + 1, REL(PC, term)); PC += 2; if (re[1] == '?') { EMIT(term, RSplit); @@ -117,7 +126,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) } else { EMIT(term, Split); } - EMIT(term + 1, REL(term, PC)); + EMIT_CHECKED(term + 1, REL(term, PC)); prog->len += 2; term = PC; break; @@ -129,20 +138,20 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) } else { EMIT(PC, RSplit); } - EMIT(PC + 1, REL(PC, term)); + EMIT_CHECKED(PC + 1, REL(PC, term)); PC += 2; prog->len++; term = PC; break; case '|': if (alt_label) { - EMIT(alt_label, REL(alt_label, PC) + 1); + EMIT_CHECKED(alt_label, REL(alt_label, PC) + 1); } INSERT_CODE(start, 2, PC); EMIT(PC++, Jmp); alt_label = PC++; EMIT(start, Split); - EMIT(start + 1, REL(start, PC)); + EMIT_CHECKED(start + 1, REL(start, PC)); prog->len += 2; term = PC; break; @@ -160,9 +169,9 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode) } if (alt_label) { - EMIT(alt_label, REL(alt_label, PC) + 1); + EMIT_CHECKED(alt_label, REL(alt_label, PC) + 1); } - return re; + return err ? NULL : re; } int re1_5_sizecode(const char *re) diff --git a/tests/extmod/ure_limit.py b/tests/extmod/ure_limit.py new file mode 100644 index 0000000000000..99c6a818e8577 --- /dev/null +++ b/tests/extmod/ure_limit.py @@ -0,0 +1,34 @@ +# Test overflow in ure.compile output code. + +try: + import ure as re +except ImportError: + print("SKIP") + raise SystemExit + + +def test_re(r): + try: + re.compile(r) + except: + print("Error") + + +# too many chars in [] +test_re("[" + "a" * 256 + "]") + +# too many groups +test_re("(a)" * 256) + +# jump too big for ? +test_re("(" + "a" * 62 + ")?") + +# jump too big for * +test_re("(" + "a" * 60 + ".)*") +test_re("(" + "a" * 60 + "..)*") + +# jump too big for + +test_re("(" + "a" * 62 + ")+") + +# jump too big for | +test_re("b" * 63 + "|a") diff --git a/tests/extmod/ure_limit.py.exp b/tests/extmod/ure_limit.py.exp new file mode 100644 index 0000000000000..8353be536c300 --- /dev/null +++ b/tests/extmod/ure_limit.py.exp @@ -0,0 +1,7 @@ +Error +Error +Error +Error +Error +Error +Error From 4f53f462ca9dbec45771e06d2d1a84b61e01e61a Mon Sep 17 00:00:00 2001 From: Tim Radvan Date: Sat, 3 Apr 2021 12:20:48 +0100 Subject: [PATCH 0712/3533] rp2: Import uarray instead of array in rp2 module. Some forum users noticed that `sm.exec()` took longer the more was present on the flash filesystem connected to the RP2040. They traced this back to the `array` import inside `asm_pio()`, which is causing MicroPython to scan the filesystem. uarray is a built-in module, so importing it shouldn't require scanning the filesystem. We avoid moving the import to the top-level in order to keep the namespace clean; we don't want to accidentally expose `rp2.array`. --- ports/rp2/modules/rp2.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/rp2/modules/rp2.py b/ports/rp2/modules/rp2.py index a3adcdc51274d..17e35c73b7ce5 100644 --- a/ports/rp2/modules/rp2.py +++ b/ports/rp2/modules/rp2.py @@ -33,7 +33,9 @@ def __init__( push_thresh=32, pull_thresh=32 ): - from array import array + # uarray is a built-in module so importing it here won't require + # scanning the filesystem. + from uarray import array self.labels = {} execctrl = 0 From a66286f3a0f24985cae2648102e51de64b8a1871 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 6 Apr 2021 20:40:31 +0200 Subject: [PATCH 0713/3533] unix: Improve command line argument processing. Per CPython everything which comes after the command, module or file argument is not an option for the interpreter itself. Hence the processing of options should stop when encountering those, and the remainder be passed as sys.argv. Note the latter was already the case for a module or file but not for a command. This fixes issues like 'micropython myfile.py -h' showing the help and exiting instead of passing '-h' as sys.argv[1], likewise for '-X ' being treated as a special option no matter where it occurs on the command line. --- ports/unix/main.c | 12 ++++++++---- tests/cmdline/repl_inspect.py | 2 +- tests/cmdline/repl_inspect.py.exp | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index 07db8d22c614f..129c6d3bb43a1 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -341,6 +341,9 @@ STATIC int invalid_args(void) { STATIC void pre_process_options(int argc, char **argv) { for (int a = 1; a < argc; a++) { if (argv[a][0] == '-') { + if (strcmp(argv[a], "-c") == 0 || strcmp(argv[a], "-m") == 0) { + break; // Everything after this is a command/module and arguments for it + } if (strcmp(argv[a], "-h") == 0) { print_help(argv); exit(0); @@ -400,6 +403,8 @@ STATIC void pre_process_options(int argc, char **argv) { } a++; } + } else { + break; // Not an option but a file } } } @@ -568,11 +573,10 @@ MP_NOINLINE int main_(int argc, char **argv) { if (a + 1 >= argc) { return invalid_args(); } + set_sys_argv(argv, a + 1, a); // The -c becomes first item of sys.argv, as in CPython + set_sys_argv(argv, argc, a + 2); // Then what comes after the command ret = do_str(argv[a + 1]); - if (ret & FORCED_EXIT) { - break; - } - a += 1; + break; } else if (strcmp(argv[a], "-m") == 0) { if (a + 1 >= argc) { return invalid_args(); diff --git a/tests/cmdline/repl_inspect.py b/tests/cmdline/repl_inspect.py index 5a7564a3c2bbb..8c86f287d1e70 100644 --- a/tests/cmdline/repl_inspect.py +++ b/tests/cmdline/repl_inspect.py @@ -1,2 +1,2 @@ -# cmdline: -c print("test") -i +# cmdline: -i -c print("test") # -c option combined with -i option results in REPL diff --git a/tests/cmdline/repl_inspect.py.exp b/tests/cmdline/repl_inspect.py.exp index 59f734b2f6945..051acfd153a61 100644 --- a/tests/cmdline/repl_inspect.py.exp +++ b/tests/cmdline/repl_inspect.py.exp @@ -1,6 +1,6 @@ test MicroPython \.\+ version Use \.\+ ->>> # cmdline: -c print("test") -i +>>> # cmdline: -i -c print("test") >>> # -c option combined with -i option results in REPL >>> From 00963a4e69207eca9f9ce106f533544774eddd1f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Apr 2021 11:55:07 +1000 Subject: [PATCH 0714/3533] stm32/powerctrl: Allow a board to configure AHB and APB clock dividers. Signed-off-by: Damien George --- ports/stm32/mpconfigboard_common.h | 19 +++++++++++++++++ ports/stm32/powerctrl.c | 4 ++-- ports/stm32/system_stm32.c | 34 +++++++++++++++--------------- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 948897b215717..6fb9289e00dca 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -310,6 +310,25 @@ #endif #endif +// Configure the default bus clock divider values +#ifndef MICROPY_HW_CLK_AHB_DIV +#if defined(STM32H7) +#define MICROPY_HW_CLK_AHB_DIV (RCC_HCLK_DIV2) +#define MICROPY_HW_CLK_APB1_DIV (RCC_APB1_DIV2) +#define MICROPY_HW_CLK_APB2_DIV (RCC_APB2_DIV2) +#define MICROPY_HW_CLK_APB3_DIV (RCC_APB3_DIV2) +#define MICROPY_HW_CLK_APB4_DIV (RCC_APB4_DIV2) +#elif defined(STM32L4) +#define MICROPY_HW_CLK_AHB_DIV (RCC_SYSCLK_DIV1) +#define MICROPY_HW_CLK_APB1_DIV (RCC_HCLK_DIV1) +#define MICROPY_HW_CLK_APB2_DIV (RCC_HCLK_DIV1) +#else +#define MICROPY_HW_CLK_AHB_DIV (RCC_SYSCLK_DIV1) +#define MICROPY_HW_CLK_APB1_DIV (RCC_HCLK_DIV4) +#define MICROPY_HW_CLK_APB2_DIV (RCC_HCLK_DIV2) +#endif +#endif + // If disabled then try normal (non-bypass) LSE first, with fallback to LSI. // If enabled first try LSE in bypass mode. If that fails to start, try non-bypass mode, with fallback to LSI. #ifndef MICROPY_HW_RTC_USE_BYPASS diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index a579713b6f9bf..a34927798a341 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -377,8 +377,8 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t RCC_ClkInitStruct.APB2CLKDivider = calc_apb2_div(ahb / apb2); #if defined(STM32H7) RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; - RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; + RCC_ClkInitStruct.APB3CLKDivider = MICROPY_HW_CLK_APB3_DIV; + RCC_ClkInitStruct.APB4CLKDivider = MICROPY_HW_CLK_APB4_DIV; #endif #if MICROPY_HW_CLK_LAST_FREQ diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index 2160e0ac994c6..72918bbc53578 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -272,9 +272,9 @@ void SystemClock_Config(void) { n = MICROPY_HW_CLK_PLLN; p = MICROPY_HW_CLK_PLLP; q = MICROPY_HW_CLK_PLLQ; - h = RCC_SYSCLK_DIV1; - b1 = RCC_HCLK_DIV4; - b2 = RCC_HCLK_DIV2; + h = MICROPY_HW_CLK_AHB_DIV; + b1 = MICROPY_HW_CLK_APB1_DIV; + b2 = MICROPY_HW_CLK_APB2_DIV; } else { h <<= 4; b1 <<= 10; @@ -285,9 +285,9 @@ void SystemClock_Config(void) { RCC_OscInitStruct.PLL.PLLP = p; // MICROPY_HW_CLK_PLLP; RCC_OscInitStruct.PLL.PLLQ = q; // MICROPY_HW_CLK_PLLQ; - RCC_ClkInitStruct.AHBCLKDivider = h; // RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = b1; // RCC_HCLK_DIV4; - RCC_ClkInitStruct.APB2CLKDivider = b2; // RCC_HCLK_DIV2; + RCC_ClkInitStruct.AHBCLKDivider = h; + RCC_ClkInitStruct.APB1CLKDivider = b1; + RCC_ClkInitStruct.APB2CLKDivider = b2; #else // defined(MICROPY_HW_CLK_LAST_FREQ) && MICROPY_HW_CLK_LAST_FREQ RCC_OscInitStruct.PLL.PLLM = MICROPY_HW_CLK_PLLM; RCC_OscInitStruct.PLL.PLLN = MICROPY_HW_CLK_PLLN; @@ -304,20 +304,20 @@ void SystemClock_Config(void) { #endif #if defined(STM32F4) || defined(STM32F7) - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.AHBCLKDivider = MICROPY_HW_CLK_AHB_DIV; + RCC_ClkInitStruct.APB1CLKDivider = MICROPY_HW_CLK_APB1_DIV; + RCC_ClkInitStruct.APB2CLKDivider = MICROPY_HW_CLK_APB2_DIV; #elif defined(STM32L4) - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.AHBCLKDivider = MICROPY_HW_CLK_AHB_DIV; + RCC_ClkInitStruct.APB1CLKDivider = MICROPY_HW_CLK_APB1_DIV; + RCC_ClkInitStruct.APB2CLKDivider = MICROPY_HW_CLK_APB2_DIV; #elif defined(STM32H7) RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; - RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; - RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; + RCC_ClkInitStruct.AHBCLKDivider = MICROPY_HW_CLK_AHB_DIV; + RCC_ClkInitStruct.APB3CLKDivider = MICROPY_HW_CLK_APB3_DIV; + RCC_ClkInitStruct.APB1CLKDivider = MICROPY_HW_CLK_APB1_DIV; + RCC_ClkInitStruct.APB2CLKDivider = MICROPY_HW_CLK_APB2_DIV; + RCC_ClkInitStruct.APB4CLKDivider = MICROPY_HW_CLK_APB4_DIV; #endif #endif From f4340b7e62e717d31d412f42364fa85a7d4728bc Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Apr 2021 11:58:36 +1000 Subject: [PATCH 0715/3533] stm32/powerctrl: Support using PLLI2C on STM32F413 as USB clock source. So SYSCLK can run at more varied frequencies, eg 100MHz. Signed-off-by: Damien George --- ports/stm32/powerctrl.c | 64 ++++++++++++++++++++++++++++---------- ports/stm32/system_stm32.c | 4 +-- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index a34927798a341..573844063b664 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -50,6 +50,22 @@ #define RCC_SR_RMVF RCC_CSR_RMVF #endif +// Whether this MCU has an independent PLL which can generate 48MHz for USB. +#if defined(STM32F413xx) +// STM32F413 uses PLLI2S as secondary PLL. +#define HAVE_PLL48 1 +#define RCC_CR_PLL48_ON RCC_CR_PLLI2SON +#define RCC_CR_PLL48_RDY RCC_CR_PLLI2SRDY +#elif defined(STM32F7) +// STM32F7 uses PLLSAI as secondary PLL. +#define HAVE_PLL48 1 +#define RCC_CR_PLL48_ON RCC_CR_PLLSAION +#define RCC_CR_PLL48_RDY RCC_CR_PLLSAIRDY +#else +// MCU does not have a secondary PLL. +#define HAVE_PLL48 0 +#endif + // Location in RAM of bootloader state (just after the top of the stack) extern uint32_t _estack[]; #define BL_STATE ((uint32_t *)&_estack) @@ -141,13 +157,24 @@ STATIC int powerctrl_config_vos(uint32_t sysclk_mhz) { } // Assumes that PLL is used as the SYSCLK source -int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk_mhz, bool need_pllsai) { +int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk_mhz, bool need_pll48) { uint32_t flash_latency; - #if defined(STM32F7) - if (need_pllsai) { - // Configure PLLSAI at 48MHz for those peripherals that need this freq - // (calculation assumes it can get an integral value of PLLSAIN) + #if HAVE_PLL48 + if (need_pll48) { + // Configure secondary PLL at 48MHz for those peripherals that need this freq + // (the calculation assumes it can get an integral value of PLL-N). + + #if defined(STM32F413xx) + const uint32_t plli2sm = HSE_VALUE / 1000000; + const uint32_t plli2sq = 2; + const uint32_t plli2sr = 2; + const uint32_t plli2sn = 48 * plli2sq; + RCC->PLLI2SCFGR = plli2sr << RCC_PLLI2SCFGR_PLLI2SR_Pos + | plli2sq << RCC_PLLI2SCFGR_PLLI2SQ_Pos + | plli2sn << RCC_PLLI2SCFGR_PLLI2SN_Pos + | plli2sm << RCC_PLLI2SCFGR_PLLI2SM_Pos; + #else const uint32_t pllm = (RCC->PLLCFGR >> RCC_PLLCFGR_PLLM_Pos) & 0x3f; const uint32_t pllsaip = 4; const uint32_t pllsaiq = 2; @@ -155,13 +182,18 @@ int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk RCC->PLLSAICFGR = pllsaiq << RCC_PLLSAICFGR_PLLSAIQ_Pos | (pllsaip / 2 - 1) << RCC_PLLSAICFGR_PLLSAIP_Pos | pllsain << RCC_PLLSAICFGR_PLLSAIN_Pos; - RCC->CR |= RCC_CR_PLLSAION; + #endif + + // Turn on the PLL and wait for it to be ready. + RCC->CR |= RCC_CR_PLL48_ON; uint32_t ticks = mp_hal_ticks_ms(); - while (!(RCC->CR & RCC_CR_PLLSAIRDY)) { + while (!(RCC->CR & RCC_CR_PLL48_RDY)) { if (mp_hal_ticks_ms() - ticks > 200) { return -MP_ETIMEDOUT; } } + + // Select the alternate 48MHz source. RCC->DCKCFGR2 |= RCC_DCKCFGR2_CK48MSEL; } #endif @@ -317,7 +349,7 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t // Default PLL parameters that give 48MHz on PLL48CK uint32_t m = MICROPY_HW_CLK_VALUE / 1000000, n = 336, p = 2, q = 7; uint32_t sysclk_source; - bool need_pllsai = false; + bool need_pll48 = false; // Search for a valid PLL configuration that keeps USB at 48MHz uint32_t sysclk_mhz = sysclk / 1000000; @@ -338,8 +370,8 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t uint32_t vco_out = sys * p; n = vco_out * m / (MICROPY_HW_CLK_VALUE / 1000000); q = vco_out / 48; - #if defined(STM32F7) - need_pllsai = vco_out % 48 != 0; + #if HAVE_PLL48 + need_pll48 = vco_out % 48 != 0; #endif } goto set_clk; @@ -393,11 +425,11 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t return -MP_EIO; } - #if defined(STM32F7) + #if HAVE_PLL48 // Deselect PLLSAI as 48MHz source if we were using it RCC->DCKCFGR2 &= ~RCC_DCKCFGR2_CK48MSEL; // Turn PLLSAI off because we are changing PLLM (which drives PLLSAI) - RCC->CR &= ~RCC_CR_PLLSAION; + RCC->CR &= ~RCC_CR_PLL48_ON; #endif // Re-configure PLL @@ -440,7 +472,7 @@ int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t // Set PLL as system clock source if wanted if (sysclk_source == RCC_SYSCLKSOURCE_PLLCLK) { RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; - int ret = powerctrl_rcc_clock_config_pll(&RCC_ClkInitStruct, sysclk_mhz, need_pllsai); + int ret = powerctrl_rcc_clock_config_pll(&RCC_ClkInitStruct, sysclk_mhz, need_pll48); if (ret != 0) { return ret; } @@ -607,11 +639,11 @@ void powerctrl_enter_stop_mode(void) { powerctrl_disable_hsi_if_unused(); - #if defined(STM32F7) + #if HAVE_PLL48 if (RCC->DCKCFGR2 & RCC_DCKCFGR2_CK48MSEL) { // Enable PLLSAI if it is selected as 48MHz source - RCC->CR |= RCC_CR_PLLSAION; - while (!(RCC->CR & RCC_CR_PLLSAIRDY)) { + RCC->CR |= RCC_CR_PLL48_ON; + while (!(RCC->CR & RCC_CR_PLL48_RDY)) { } } #endif diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index 72918bbc53578..4e89204bf6c21 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -351,8 +351,8 @@ void SystemClock_Config(void) { uint32_t vco_out = RCC_OscInitStruct.PLL.PLLN * (MICROPY_HW_CLK_VALUE / 1000000) / RCC_OscInitStruct.PLL.PLLM; uint32_t sysclk_mhz = vco_out / RCC_OscInitStruct.PLL.PLLP; - bool need_pllsai = vco_out % 48 != 0; - if (powerctrl_rcc_clock_config_pll(&RCC_ClkInitStruct, sysclk_mhz, need_pllsai) != 0) { + bool need_pll48 = vco_out % 48 != 0; + if (powerctrl_rcc_clock_config_pll(&RCC_ClkInitStruct, sysclk_mhz, need_pll48) != 0) { __fatal_error("HAL_RCC_ClockConfig"); } From cb396827f56f79741efeaa127b9c71c72bba3584 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Apr 2021 12:05:00 +1000 Subject: [PATCH 0716/3533] stm32/boards/pllvalues.py: Relax PLLQ constraints on STM32F413 MCUs. Signed-off-by: Damien George --- ports/stm32/Makefile | 2 +- ports/stm32/boards/pllvalues.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 9c22928a0c7ba..e7a8f0aee62d3 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -750,7 +750,7 @@ CMSIS_MCU_HDR = $(CMSIS_DIR)/$(CMSIS_MCU_LOWER).h modmachine.c: $(GEN_PLLFREQTABLE_HDR) $(GEN_PLLFREQTABLE_HDR): $(PLLVALUES) | $(HEADER_BUILD) $(ECHO) "GEN $@" - $(Q)$(PYTHON) $(PLLVALUES) -c -m $(MCU_SERIES) file:$(BOARD_DIR)/stm32$(MCU_SERIES)xx_hal_conf.h > $@ + $(Q)$(PYTHON) $(PLLVALUES) -c -m $(CMSIS_MCU_LOWER) file:$(BOARD_DIR)/stm32$(MCU_SERIES)xx_hal_conf.h > $@ $(BUILD)/modstm.o: $(GEN_STMCONST_HDR) # Use a pattern rule here so that make will only call make-stmconst.py once to diff --git a/ports/stm32/boards/pllvalues.py b/ports/stm32/boards/pllvalues.py index 59d660364afca..619146cd47fac 100644 --- a/ports/stm32/boards/pllvalues.py +++ b/ports/stm32/boards/pllvalues.py @@ -240,7 +240,7 @@ def main(): argv = sys.argv[1:] c_table = False - mcu_series = "f4" + mcu_series = "stm32f4" hse = None hsi = None @@ -271,13 +271,13 @@ def main(): hse = int(argv[0]) # Select MCU parameters - if mcu_series == "h7": + if mcu_series.startswith("stm32h7"): mcu = mcu_h7 else: mcu = mcu_default - # Relax constraight on PLLQ being 48MHz on F7 and H7 MCUs, which have separate PLLs for 48MHz - relax_pll48 = mcu_series in ("f7", "h7") + # Relax constraint on PLLQ being 48MHz on MCUs which have separate PLLs for 48MHz + relax_pll48 = mcu_series.startswith(("stm32f413", "stm32f7", "stm32h7")) hse_valid_plls = compute_pll_table(hse, relax_pll48) if hsi is not None: From 4d9e657f0ee881f4a41093ab89ec91d03613744d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 17:00:26 +1000 Subject: [PATCH 0717/3533] stm32/mpconfigport.h: Add support for a board to specify root pointers. A board can now define MICROPY_BOARD_ROOT_POINTERS to specify any custom root pointers. Signed-off-by: Damien George --- ports/stm32/mpconfigport.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 174ef22f241c7..2888cac88c43b 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -353,6 +353,10 @@ struct _mp_bluetooth_btstack_root_pointers_t; #define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_BTSTACK #endif +#ifndef MICROPY_BOARD_ROOT_POINTERS +#define MICROPY_BOARD_ROOT_POINTERS +#endif + #define MICROPY_PORT_ROOT_POINTERS \ const char *readline_hist[8]; \ \ @@ -384,9 +388,13 @@ struct _mp_bluetooth_btstack_root_pointers_t; /* list of registered NICs */ \ mp_obj_list_t mod_network_nic_list; \ \ + /* root pointers for sub-systems */ \ MICROPY_PORT_ROOT_POINTER_MBEDTLS \ MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE \ - MICROPY_PORT_ROOT_POINTER_BLUETOOTH_BTSTACK \ + MICROPY_PORT_ROOT_POINTER_BLUETOOTH_BTSTACK \ + \ + /* root pointers defined by a board */ \ + MICROPY_BOARD_ROOT_POINTERS \ // type definitions for the specific machine From 7b41d7f187d0bbfdda66db06b523c8829f198998 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 21:11:24 +1000 Subject: [PATCH 0718/3533] stm32/boardctrl: Give boards control over execution of boot.py,main.py. This commit simplifies the customisation of the main MicroPython execution loop (4 macros are reduced to 2), and allows a board to have full control over the execution (or not) of boot.py and main.py. For boards that use the default start-up code, there is no functional change in this commit. Signed-off-by: Damien George --- ports/stm32/boardctrl.c | 50 +++++++++++++++++++++++++++++++---------- ports/stm32/boardctrl.h | 30 +++++++++---------------- ports/stm32/main.c | 38 ++++++------------------------- 3 files changed, 56 insertions(+), 62 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 188068d705a2a..c82fefa97cf3b 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "py/runtime.h" #include "py/mphal.h" #include "lib/utils/pyexec.h" #include "boardctrl.h" @@ -140,13 +141,21 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { led_state(4, 0); } -void boardctrl_before_boot_py(boardctrl_state_t *state) { - state->run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; -} +int boardctrl_run_boot_py(boardctrl_state_t *state) { + bool run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; + + if (run_boot_py) { + // Run boot.py, if it exists. + const char *boot_py = "boot.py"; + int ret = pyexec_file_if_exists(boot_py); -void boardctrl_after_boot_py(boardctrl_state_t *state) { - if (state->run_boot_py && !state->last_ret) { - flash_error(4); + // Take action based on the execution result. + if (ret & PYEXEC_FORCED_EXIT) { + return BOARDCTRL_GOTO_SOFT_RESET_EXIT; + } + if (!ret) { + flash_error(4); + } } // Turn boot-up LEDs off @@ -160,17 +169,34 @@ void boardctrl_after_boot_py(boardctrl_state_t *state) { led_state(2, 0); led_state(3, 0); led_state(4, 0); + + return BOARDCTRL_CONTINUE; } -void boardctrl_before_main_py(boardctrl_state_t *state) { - state->run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) +int boardctrl_run_main_py(boardctrl_state_t *state) { + bool run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; -} -void boardctrl_after_main_py(boardctrl_state_t *state) { - if (state->run_main_py && !state->last_ret) { - flash_error(3); + if (run_main_py) { + // Run main.py (or what it was configured to be), if it exists. + const char *main_py; + if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) { + main_py = "main.py"; + } else { + main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); + } + int ret = pyexec_file_if_exists(main_py); + + // Take action based on the execution result. + if (ret & PYEXEC_FORCED_EXIT) { + return BOARDCTRL_GOTO_SOFT_RESET_EXIT; + } + if (!ret) { + flash_error(3); + } } + + return BOARDCTRL_CONTINUE; } void boardctrl_start_soft_reset(boardctrl_state_t *state) { diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index f1cbc3b955b3c..33e71b65c9975 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -44,20 +44,12 @@ #define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop #endif -#ifndef MICROPY_BOARD_BEFORE_BOOT_PY -#define MICROPY_BOARD_BEFORE_BOOT_PY boardctrl_before_boot_py +#ifndef MICROPY_BOARD_RUN_BOOT_PY +#define MICROPY_BOARD_RUN_BOOT_PY boardctrl_run_boot_py #endif -#ifndef MICROPY_BOARD_AFTER_BOOT_PY -#define MICROPY_BOARD_AFTER_BOOT_PY boardctrl_after_boot_py -#endif - -#ifndef MICROPY_BOARD_BEFORE_MAIN_PY -#define MICROPY_BOARD_BEFORE_MAIN_PY boardctrl_before_main_py -#endif - -#ifndef MICROPY_BOARD_AFTER_MAIN_PY -#define MICROPY_BOARD_AFTER_MAIN_PY boardctrl_after_main_py +#ifndef MICROPY_BOARD_RUN_MAIN_PY +#define MICROPY_BOARD_RUN_MAIN_PY boardctrl_run_main_py #endif #ifndef MICROPY_BOARD_START_SOFT_RESET @@ -68,20 +60,20 @@ #define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset #endif +enum { + BOARDCTRL_CONTINUE, + BOARDCTRL_GOTO_SOFT_RESET_EXIT, +}; + typedef struct _boardctrl_state_t { uint8_t reset_mode; - bool run_boot_py; - bool run_main_py; bool log_soft_reset; - int last_ret; } boardctrl_state_t; void boardctrl_before_soft_reset_loop(boardctrl_state_t *state); void boardctrl_top_soft_reset_loop(boardctrl_state_t *state); -void boardctrl_before_boot_py(boardctrl_state_t *state); -void boardctrl_after_boot_py(boardctrl_state_t *state); -void boardctrl_before_main_py(boardctrl_state_t *state); -void boardctrl_after_main_py(boardctrl_state_t *state); +int boardctrl_run_boot_py(boardctrl_state_t *state); +int boardctrl_run_main_py(boardctrl_state_t *state); void boardctrl_start_soft_reset(boardctrl_state_t *state); void boardctrl_end_soft_reset(boardctrl_state_t *state); diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 888b20513c6f9..bc8e6f0691eef 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -469,9 +469,7 @@ void stm32_main(uint32_t reset_mode) { boardctrl_state_t state; state.reset_mode = reset_mode; - state.run_boot_py = false; - state.run_main_py = false; - state.last_ret = 0; + state.log_soft_reset = false; MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state); @@ -566,20 +564,11 @@ void stm32_main(uint32_t reset_mode) { // reset config variables; they should be set by boot.py MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL; - MICROPY_BOARD_BEFORE_BOOT_PY(&state); - - // run boot.py, if it exists - // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py - if (state.run_boot_py) { - const char *boot_py = "boot.py"; - state.last_ret = pyexec_file_if_exists(boot_py); - if (state.last_ret & PYEXEC_FORCED_EXIT) { - goto soft_reset_exit; - } + // Run boot.py (or whatever else a board configures at this stage). + if (MICROPY_BOARD_RUN_BOOT_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { + goto soft_reset_exit; } - MICROPY_BOARD_AFTER_BOOT_PY(&state); - // Now we initialise sub-systems that need configuration from boot.py, // or whose initialisation can be safely deferred until after running // boot.py. @@ -613,24 +602,11 @@ void stm32_main(uint32_t reset_mode) { // At this point everything is fully configured and initialised. - MICROPY_BOARD_BEFORE_MAIN_PY(&state); - - // Run the main script from the current directory. - if (state.run_main_py) { - const char *main_py; - if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) { - main_py = "main.py"; - } else { - main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); - } - state.last_ret = pyexec_file_if_exists(main_py); - if (state.last_ret & PYEXEC_FORCED_EXIT) { - goto soft_reset_exit; - } + // Run main.py (or whatever else a board configures at this stage). + if (MICROPY_BOARD_RUN_MAIN_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { + goto soft_reset_exit; } - MICROPY_BOARD_AFTER_MAIN_PY(&state); - #if MICROPY_ENABLE_COMPILER // Main script is finished, so now go into REPL mode. // The REPL mode can change, or it can request a soft reset. From 0fabda31de33b38c6858925d9195731deba6f54a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 23:42:22 +1000 Subject: [PATCH 0719/3533] py/py.cmake: Move qstr helper code to micropy_gather_target_properties. Signed-off-by: Damien George --- ports/esp32/main/CMakeLists.txt | 17 +---------------- ports/rp2/CMakeLists.txt | 24 ++---------------------- py/py.cmake | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 38 deletions(-) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index d7d08ea663df4..1f4bb3175bc16 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -170,22 +170,7 @@ target_link_libraries(${MICROPY_TARGET} usermod) # Collect all of the include directories and compile definitions for the IDF components. foreach(comp ${IDF_COMPONENTS}) - get_target_property(type __idf_${comp} TYPE) - set(_inc OFF) - set(_def OFF) - if(${type} STREQUAL STATIC_LIBRARY) - get_target_property(_inc __idf_${comp} INCLUDE_DIRECTORIES) - get_target_property(_def __idf_${comp} COMPILE_DEFINITIONS) - elseif(${type} STREQUAL INTERFACE_LIBRARY) - get_target_property(_inc __idf_${comp} INTERFACE_INCLUDE_DIRECTORIES) - get_target_property(_def __idf_${comp} INTERFACE_COMPILE_DEFINITIONS) - endif() - if(_inc) - list(APPEND MICROPY_CPP_INC_EXTRA ${_inc}) - endif() - if(_def) - list(APPEND MICROPY_CPP_DEF_EXTRA ${_def}) - endif() + micropy_gather_target_properties(__idf_${comp}) endforeach() if(IDF_VERSION_MINOR GREATER_EQUAL 2) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 37b041279f503..5f3cdebd87060 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -193,29 +193,9 @@ add_custom_command(TARGET ${MICROPY_TARGET} ) # Collect all the include directories and compile definitions for the pico-sdk components. -macro(_process_target targ) - if(TARGET ${targ}) - get_target_property(type ${targ} TYPE) - set(_inc OFF) - set(_def OFF) - if(${type} STREQUAL STATIC_LIBRARY) - get_target_property(_inc ${targ} INCLUDE_DIRECTORIES) - get_target_property(_def ${targ} COMPILE_DEFINITIONS) - elseif(${type} STREQUAL INTERFACE_LIBRARY) - get_target_property(_inc ${targ} INTERFACE_INCLUDE_DIRECTORIES) - get_target_property(_def ${targ} INTERFACE_COMPILE_DEFINITIONS) - endif() - if(_inc) - list(APPEND MICROPY_CPP_INC_EXTRA ${_inc}) - endif() - if(_def) - list(APPEND MICROPY_CPP_DEF_EXTRA ${_def}) - endif() - endif() -endmacro() foreach(comp ${PICO_SDK_COMPONENTS}) - _process_target(${comp}) - _process_target(${comp}_headers) + micropy_gather_target_properties(${comp}) + micropy_gather_target_properties(${comp}_headers) endforeach() # Include the main MicroPython cmake rules. diff --git a/py/py.cmake b/py/py.cmake index 52baaa8efc7fd..be5deca2510da 100644 --- a/py/py.cmake +++ b/py/py.cmake @@ -122,3 +122,25 @@ set(MICROPY_SOURCE_PY ${MICROPY_PY_DIR}/vstr.c ${MICROPY_PY_DIR}/warning.c ) + +# Helper macro to collect include directories and compile definitions for qstr processing. +macro(micropy_gather_target_properties targ) + if(TARGET ${targ}) + get_target_property(type ${targ} TYPE) + set(_inc OFF) + set(_def OFF) + if(${type} STREQUAL STATIC_LIBRARY) + get_target_property(_inc ${targ} INCLUDE_DIRECTORIES) + get_target_property(_def ${targ} COMPILE_DEFINITIONS) + elseif(${type} STREQUAL INTERFACE_LIBRARY) + get_target_property(_inc ${targ} INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(_def ${targ} INTERFACE_COMPILE_DEFINITIONS) + endif() + if(_inc) + list(APPEND MICROPY_CPP_INC_EXTRA ${_inc}) + endif() + if(_def) + list(APPEND MICROPY_CPP_DEF_EXTRA ${_def}) + endif() + endif() +endmacro() From 5dcc9b3b16cf6a27acf01bdf8a7d23ae2aff56e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 9 Apr 2021 00:59:16 +1000 Subject: [PATCH 0720/3533] py/py.cmake: Introduce MICROPY_INC_CORE as a list with core includes. Signed-off-by: Damien George --- ports/esp32/main/CMakeLists.txt | 2 +- ports/rp2/CMakeLists.txt | 2 +- ports/zephyr/CMakeLists.txt | 2 +- py/py.cmake | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 1f4bb3175bc16..2bfc09558e397 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -132,7 +132,7 @@ idf_component_register( ${MICROPY_SOURCE_DRIVERS} ${MICROPY_SOURCE_PORT} INCLUDE_DIRS - ${MICROPY_DIR} + ${MICROPY_INC_CORE} ${MICROPY_INC_USERMOD} ${MICROPY_PORT_DIR} ${MICROPY_BOARD_DIR} diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 5f3cdebd87060..100a83409002e 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -148,9 +148,9 @@ target_sources(${MICROPY_TARGET} PRIVATE target_link_libraries(${MICROPY_TARGET} usermod) target_include_directories(${MICROPY_TARGET} PRIVATE + ${MICROPY_INC_CORE} ${MICROPY_INC_USERMOD} "${PROJECT_SOURCE_DIR}" - "${MICROPY_DIR}" "${CMAKE_BINARY_DIR}" ) diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 08868d716dc9e..716642979a601 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -85,7 +85,7 @@ set(MICROPY_CPP_FLAGS_EXTRA ${includes} ${system_includes} ${definitions} ${opti zephyr_library_named(${MICROPY_TARGET}) zephyr_library_include_directories( - ${MICROPY_DIR} + ${MICROPY_INC_CORE} ${MICROPY_PORT_DIR} ${CMAKE_CURRENT_BINARY_DIR} ) diff --git a/py/py.cmake b/py/py.cmake index be5deca2510da..2b5d437b5762b 100644 --- a/py/py.cmake +++ b/py/py.cmake @@ -2,6 +2,8 @@ set(MICROPY_PY_DIR "${MICROPY_DIR}/py") +list(APPEND MICROPY_INC_CORE "${MICROPY_DIR}") + # All py/ source files set(MICROPY_SOURCE_PY ${MICROPY_PY_DIR}/argcheck.c From 212fe7f33e7427899ee245cda5ab1776b38a79e2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 23:45:28 +1000 Subject: [PATCH 0721/3533] extmod/extmod.cmake: Add support to build btree module with CMake. Signed-off-by: Damien George --- extmod/extmod.cmake | 48 +++++++++++++++++++++++++++++++++ ports/esp32/main/CMakeLists.txt | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 691c3ce9b3fea..a54047519d76e 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -4,6 +4,8 @@ set(MICROPY_EXTMOD_DIR "${MICROPY_DIR}/extmod") set(MICROPY_OOFATFS_DIR "${MICROPY_DIR}/lib/oofatfs") set(MICROPY_SOURCE_EXTMOD + ${MICROPY_DIR}/lib/embed/abort_.c + ${MICROPY_DIR}/lib/utils/printf.c ${MICROPY_EXTMOD_DIR}/machine_i2c.c ${MICROPY_EXTMOD_DIR}/machine_mem.c ${MICROPY_EXTMOD_DIR}/machine_pulse.c @@ -43,3 +45,49 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/virtpin.c ${MICROPY_EXTMOD_DIR}/nimble/modbluetooth_nimble.c ) + +# Library for btree module and associated code + +set(MICROPY_LIB_BERKELEY_DIR "${MICROPY_DIR}/lib/berkeley-db-1.xx") + +if(EXISTS "${MICROPY_LIB_BERKELEY_DIR}/btree/bt_close.c") + add_library(micropy_extmod_btree OBJECT + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_close.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_conv.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_debug.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_delete.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_get.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_open.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_overflow.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_page.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_put.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_search.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_seq.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_split.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_utils.c + ${MICROPY_LIB_BERKELEY_DIR}/mpool/mpool.c + ) + + target_include_directories(micropy_extmod_btree PRIVATE + ${MICROPY_LIB_BERKELEY_DIR}/PORT/include + ) + + target_compile_definitions(micropy_extmod_btree PRIVATE + __DBINTERFACE_PRIVATE=1 + mpool_error=printf + abort=abort_ + "virt_fd_t=void*" + ) + + # The include directories and compile definitions below are needed to build + # modbtree.c and should be added to the main MicroPython target. + + list(APPEND MICROPY_INC_CORE + "${MICROPY_LIB_BERKELEY_DIR}/PORT/include" + ) + + list(APPEND MICROPY_DEF_CORE + __DBINTERFACE_PRIVATE=1 + "virt_fd_t=void*" + ) +endif() diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 2bfc09558e397..2223ecd031d44 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -3,10 +3,10 @@ get_filename_component(MICROPY_DIR ${PROJECT_DIR}/../.. ABSOLUTE) # Include core source components. include(${MICROPY_DIR}/py/py.cmake) -include(${MICROPY_DIR}/extmod/extmod.cmake) if(NOT CMAKE_BUILD_EARLY_EXPANSION) include(${MICROPY_DIR}/py/usermod.cmake) + include(${MICROPY_DIR}/extmod/extmod.cmake) endif() set(MICROPY_QSTRDEFS_PORT From ab9d47e0230768f82a735343fa5b58e2029d8eda Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 23:56:28 +1000 Subject: [PATCH 0722/3533] esp32: Enable btree module. This was disabled with the move to CMake, and this commit reinstates it. Signed-off-by: Damien George --- ports/esp32/main/CMakeLists.txt | 4 +++- ports/esp32/mpconfigport.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 2223ecd031d44..aacdd40d3113d 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -149,6 +149,7 @@ set(MICROPY_CROSS_FLAGS -march=xtensawin) # Set compile options for this port. target_compile_definitions(${MICROPY_TARGET} PUBLIC + ${MICROPY_DEF_CORE} MICROPY_ESP_IDF_4=1 MICROPY_VFS_FAT=1 MICROPY_VFS_LFS2=1 @@ -164,7 +165,8 @@ target_compile_options(${MICROPY_TARGET} PUBLIC -Wno-missing-field-initializers ) -# add usermod +# Add additional extmod and usermod components. +target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree) target_link_libraries(${MICROPY_TARGET} usermod) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 392f8c749c240..c9c4b6268be2f 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -162,6 +162,7 @@ #define MICROPY_PY_UWEBSOCKET (1) #define MICROPY_PY_WEBREPL (1) #define MICROPY_PY_FRAMEBUF (1) +#define MICROPY_PY_BTREE (1) #define MICROPY_PY_USOCKET_EVENTS (MICROPY_PY_WEBREPL) #define MICROPY_PY_BLUETOOTH_RANDOM_ADDR (1) #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME ("ESP32") From 7546d3cf73608b7888583d5bdbcec059954ccfe8 Mon Sep 17 00:00:00 2001 From: aziubin Date: Sun, 21 Mar 2021 18:34:05 +0200 Subject: [PATCH 0723/3533] stm32/boards/NUCLEO_L476RG: Add 5 remaining UARTs. STM32L476RG MCU of NUCLEO_L476RG board has 6 UART/USART units in total (USART1, USART2, USART3, UART4, UART5 and LPUART1), but only UART2, connected to REPL, was defined and available in Python code. Defined are all 5 remaining UART/USART units including LPUART1. Signed-off-by: Alexander Ziubin aziubin@googlemail.com --- .../boards/NUCLEO_L476RG/mpconfigboard.h | 75 ++++++++++++------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h index 00b033006ba9a..2899a97aceeda 100644 --- a/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h @@ -9,45 +9,64 @@ #define MICROPY_HW_ENABLE_DAC (1) // MSI is used and is 4MHz -#define MICROPY_HW_CLK_PLLM (1) -#define MICROPY_HW_CLK_PLLN (40) -#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV7) -#define MICROPY_HW_CLK_PLLQ (RCC_PLLQ_DIV2) -#define MICROPY_HW_CLK_PLLR (RCC_PLLR_DIV2) +#define MICROPY_HW_CLK_PLLM (1) +#define MICROPY_HW_CLK_PLLN (40) +#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV7) +#define MICROPY_HW_CLK_PLLQ (RCC_PLLQ_DIV2) +#define MICROPY_HW_CLK_PLLR (RCC_PLLR_DIV2) + +#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 // The board has an external 32kHz crystal #define MICROPY_HW_RTC_USE_LSE (1) -// UART config -#define MICROPY_HW_UART2_TX (pin_A2) -#define MICROPY_HW_UART2_RX (pin_A3) - +// USART1 config +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +// USART2 config. Connected to ST-Link +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +// USART3 config +#define MICROPY_HW_UART3_TX (pin_C4) +#define MICROPY_HW_UART3_RX (pin_C5) +#define MICROPY_HW_UART3_RTS (pin_B14) +#define MICROPY_HW_UART3_CTS (pin_B13) +// UART4 config +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +// UART5 config +#define MICROPY_HW_UART5_TX (pin_C12) +#define MICROPY_HW_UART5_RX (pin_D2) +// LPUART config +#define MICROPY_HW_LPUART1_TX (pin_C1) +#define MICROPY_HW_LPUART1_RX (pin_C0) +// USART2 is connected to the virtual com port on the ST-Link #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 - // I2C busses -#define MICROPY_HW_I2C1_SCL (pin_B8) -#define MICROPY_HW_I2C1_SDA (pin_B9) -#define MICROPY_HW_I2C2_SCL (pin_B10) -#define MICROPY_HW_I2C2_SDA (pin_B11) -#define MICROPY_HW_I2C3_SCL (pin_C0) -#define MICROPY_HW_I2C3_SDA (pin_C1) +#define MICROPY_HW_I2C1_SCL (pin_B8) +#define MICROPY_HW_I2C1_SDA (pin_B9) +#define MICROPY_HW_I2C2_SCL (pin_B10) +#define MICROPY_HW_I2C2_SDA (pin_B11) +#define MICROPY_HW_I2C3_SCL (pin_C0) +#define MICROPY_HW_I2C3_SDA (pin_C1) // SPI busses -#define MICROPY_HW_SPI1_NSS (pin_A4) -#define MICROPY_HW_SPI1_SCK (pin_B3) -#define MICROPY_HW_SPI1_MISO (pin_B4) -#define MICROPY_HW_SPI1_MOSI (pin_B5) -#define MICROPY_HW_SPI2_NSS (pin_B12) -#define MICROPY_HW_SPI2_SCK (pin_B13) -#define MICROPY_HW_SPI2_MISO (pin_B14) -#define MICROPY_HW_SPI2_MOSI (pin_B15) +#define MICROPY_HW_SPI1_NSS (pin_A4) +#define MICROPY_HW_SPI1_SCK (pin_B3) +#define MICROPY_HW_SPI1_MISO (pin_B4) +#define MICROPY_HW_SPI1_MOSI (pin_B5) +#define MICROPY_HW_SPI2_NSS (pin_B12) +#define MICROPY_HW_SPI2_SCK (pin_B13) +#define MICROPY_HW_SPI2_MISO (pin_B14) +#define MICROPY_HW_SPI2_MOSI (pin_B15) // CAN bus -#define MICROPY_HW_CAN1_TX (pin_A12) -#define MICROPY_HW_CAN1_RX (pin_A11) +#define MICROPY_HW_CAN1_TX (pin_A12) +#define MICROPY_HW_CAN1_RX (pin_A11) // USRSW is pulled low. Pressing the button makes the input go high. #define MICROPY_HW_USRSW_PIN (pin_C13) @@ -61,4 +80,4 @@ #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) // USB config -#define MICROPY_HW_USB_FS (1) +#define MICROPY_HW_USB_FS (1) From 42035e5eded531d41319ffde999fa5a97478c872 Mon Sep 17 00:00:00 2001 From: Santeri Paavolainen Date: Wed, 24 Feb 2021 10:33:26 +0200 Subject: [PATCH 0724/3533] examples/embedding: Fix example so it compiles again. There were a few changes that had broken this example, specifically 2cdf1d25f59409b6130c0e8b6cf50300aed2d7e6 removed file.c from ports/unix. And (at least for MacOS) mp_state_ctx must be placed in the BSS with -fno-common so it is visible to the linker. Signed-off-by: Santeri Paavolainen --- examples/embedding/Makefile.upylib | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index 0e388332e68fa..00def493d650f 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -21,6 +21,9 @@ CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +# Some systems (eg MacOS) need -fno-common so that mp_state_ctx is placed in the BSS. +CFLAGS += -fno-common + # Debugging/Optimization ifdef DEBUG CFLAGS += -g @@ -133,7 +136,6 @@ SRC_C = $(addprefix ports/unix/,\ gccollect.c \ unix_mphal.c \ input.c \ - file.c \ modmachine.c \ modos.c \ moduselect.c \ @@ -146,6 +148,7 @@ SRC_C = $(addprefix ports/unix/,\ LIB_SRC_C = $(addprefix lib/,\ $(LIB_SRC_C_EXTRA) \ utils/printf.c \ + utils/gchelper_generic.c \ timeutils/timeutils.c \ ) From 2c9af1c1d7398bb340f7c78601be6ed2d79ee0b8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 15 Mar 2021 16:42:58 +1100 Subject: [PATCH 0725/3533] rp2/rp2_pio: Validate state machine frequency in constructor. Fixes issue #7025. Signed-off-by: Damien George --- ports/rp2/rp2_pio.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 9786e569d66c8..bd3e10a06c7aa 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -462,13 +462,24 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel } // Compute the clock divider. - float div; + uint16_t clkdiv_int; + uint8_t clkdiv_frac; if (args[ARG_freq].u_int < 0) { - div = 1; + // Default: run at CPU frequency. + clkdiv_int = 1; + clkdiv_frac = 0; } else if (args[ARG_freq].u_int == 0) { - div = 0; + // Special case of 0: set clkdiv to 0. + clkdiv_int = 0; + clkdiv_frac = 0; } else { - div = (float)clock_get_hz(clk_sys) / (float)args[ARG_freq].u_int; + // Frequency given in Hz, compute clkdiv from it. + uint64_t div = (uint64_t)clock_get_hz(clk_sys) * 256ULL / (uint64_t)args[ARG_freq].u_int; + if (!(div >= 1 * 256 && div <= 65536 * 256)) { + mp_raise_ValueError(MP_ERROR_TEXT("freq out of range")); + } + clkdiv_int = div / 256; + clkdiv_frac = div & 0xff; } // Disable and reset the state machine. @@ -476,7 +487,7 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel // Build the state machine config. pio_sm_config config = pio_get_default_sm_config(); - sm_config_set_clkdiv(&config, div); + sm_config_set_clkdiv_int_frac(&config, clkdiv_int, clkdiv_frac); config.execctrl = mp_obj_get_int_truncated(prog[PROG_EXECCTRL]); config.shiftctrl = mp_obj_get_int_truncated(prog[PROG_SHIFTCTRL]); From 6f06dcaee57cc6f36a4121b4797942200b2d7281 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 17 Mar 2021 07:57:27 +0100 Subject: [PATCH 0726/3533] rp2/moduos: Implement uos.urandom(). The implementation samples rosc.randombits at a frequency lower than the oscillator frequency. This gives better random values. In addition, for an 8-bit value 8 samples are taken and fed through a 8-bit CRC, distributing the sampling over the byte. The resulting sampling rate is about 120k/sec. The RNG does not include testing of error conditions, like the ROSC being in sync with the sampling or completely failing. Making the interim value static causes it to perform a little bit better in short sync or drop-out situations. The output of uos.urandom() performs well with the NIST800-22 test suite. In my trial it passed all tests of the sts 2.1.2 test suite. I also ran a test of the random data with the Common Criteria test suite AIS 31, and it passed all tests too. --- ports/rp2/main.c | 16 ++++++++++++++-- ports/rp2/moduos.c | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 64218f97c5aeb..8fddeaa560510 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -30,6 +30,7 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mperrno.h" +#include "py/mphal.h" #include "py/stackctrl.h" #include "lib/mp-readline/readline.h" #include "lib/utils/gchelper.h" @@ -168,10 +169,21 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c } #endif +#define POLY (0xD5) + +uint8_t rosc_random_u8(size_t cycles) { + static uint8_t r; + for (size_t i = 0; i < cycles; ++i) { + r = ((r << 1) | rosc_hw->randombit) ^ (r & 0x80 ? POLY : 0); + mp_hal_delay_us_fast(1); + } + return r; +} + uint32_t rosc_random_u32(void) { uint32_t value = 0; - for (size_t i = 0; i < 32; ++i) { - value = value << 1 | rosc_hw->randombit; + for (size_t i = 0; i < 4; ++i) { + value = value << 8 | rosc_random_u8(32); } return value; } diff --git a/ports/rp2/moduos.c b/ports/rp2/moduos.c index 7ee662b5a4518..02f34eb960d0f 100644 --- a/ports/rp2/moduos.c +++ b/ports/rp2/moduos.c @@ -31,6 +31,8 @@ #include "extmod/vfs_lfs.h" #include "genhdr/mpversion.h" +uint8_t rosc_random_u8(size_t cycles); + STATIC const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine @@ -57,10 +59,22 @@ STATIC mp_obj_t os_uname(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); +STATIC mp_obj_t os_urandom(mp_obj_t num) { + mp_int_t n = mp_obj_get_int(num); + vstr_t vstr; + vstr_init_len(&vstr, n); + for (int i = 0; i < n; i++) { + vstr.buf[i] = rosc_random_u8(8); + } + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); + STATIC const mp_rom_map_elem_t os_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) }, { MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) }, + { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&os_urandom_obj) }, #if MICROPY_VFS { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) }, From 22554cf8e21ab97c60137539536eab05d6a6cdcb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 1 Mar 2021 14:23:45 +0100 Subject: [PATCH 0727/3533] rp2/rp2_pio: Add StateMachine restart,rx_fifo,tx_fifo helper functions. StateMachine.restart: Restarts the state machine StateMachine.rx_fifo: Return the number of RX FIFO items, 0 if empty StateMachine.tx_fifo: Return the number of TX FIFO items, 0 if empty restart() seems to be the most useful one, as it resets the state machine to the initial state without the need to re-initialise/re-create. It also makes PIO code easier, because then stalling as an error state can be unlocked. rx_fifo() is also useful, for MP code to check for data and timeout if no data arrived. Complex logic is easier handled in Python code than in PIO code. tx_fifo() can be useful to check states where data is not processed, and is mostly for symmetry. --- ports/rp2/rp2_pio.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index bd3e10a06c7aa..5f93f10c26794 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -66,6 +66,7 @@ typedef struct _rp2_state_machine_irq_obj_t { } rp2_state_machine_irq_obj_t; STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[8]; +STATIC uint8_t rp2_state_machine_initial_pc[8]; STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); @@ -459,6 +460,7 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel if (offset < 0) { rp2_pio_add_program(&rp2_pio_obj[PIO_NUM(self->pio)], args[ARG_prog].u_obj); offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]); + rp2_state_machine_initial_pc[self->id] = offset; } // Compute the clock divider. @@ -592,6 +594,15 @@ STATIC mp_obj_t rp2_state_machine_active(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_active_obj, 1, 2, rp2_state_machine_active); +// StateMachine.restart() +STATIC mp_obj_t rp2_state_machine_restart(mp_obj_t self_in) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + pio_sm_restart(self->pio, self->sm); + pio_sm_exec(self->pio, self->sm, pio_encode_jmp(rp2_state_machine_initial_pc[self->id])); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2_state_machine_restart_obj, rp2_state_machine_restart); + // StateMachine.exec(instr) STATIC mp_obj_t rp2_state_machine_exec(mp_obj_t self_in, mp_obj_t instr_in) { rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -700,6 +711,20 @@ STATIC mp_obj_t rp2_state_machine_put(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_put_obj, 2, 3, rp2_state_machine_put); +// StateMachine.rx_fifo() +STATIC mp_obj_t rp2_state_machine_rx_fifo(mp_obj_t self_in) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(pio_sm_get_rx_fifo_level(self->pio, self->sm)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2_state_machine_rx_fifo_obj, rp2_state_machine_rx_fifo); + +// StateMachine.tx_fifo() +STATIC mp_obj_t rp2_state_machine_tx_fifo(mp_obj_t self_in) { + rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(pio_sm_get_tx_fifo_level(self->pio, self->sm)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2_state_machine_tx_fifo_obj, rp2_state_machine_tx_fifo); + // StateMachine.irq(handler=None, trigger=0|1, hard=False) STATIC mp_obj_t rp2_state_machine_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_handler, ARG_trigger, ARG_hard }; @@ -759,9 +784,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(rp2_state_machine_irq_obj, 1, rp2_state_machin STATIC const mp_rom_map_elem_t rp2_state_machine_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&rp2_state_machine_init_obj) }, { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&rp2_state_machine_active_obj) }, + { MP_ROM_QSTR(MP_QSTR_restart), MP_ROM_PTR(&rp2_state_machine_restart_obj) }, { MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&rp2_state_machine_exec_obj) }, { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&rp2_state_machine_get_obj) }, { MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&rp2_state_machine_put_obj) }, + { MP_ROM_QSTR(MP_QSTR_rx_fifo), MP_ROM_PTR(&rp2_state_machine_rx_fifo_obj) }, + { MP_ROM_QSTR(MP_QSTR_tx_fifo), MP_ROM_PTR(&rp2_state_machine_tx_fifo_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&rp2_state_machine_irq_obj) }, }; STATIC MP_DEFINE_CONST_DICT(rp2_state_machine_locals_dict, rp2_state_machine_locals_dict_table); From 1be74b94b6f3263b8e360a0a012ae87301539f91 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 11 Mar 2021 08:36:16 +0100 Subject: [PATCH 0728/3533] rp2/machine_uart: Add buffered transfer of data with rxbuf/txbuf kwargs. Instantiation and init now support the rxbuf and txbuf keywords for setting the buffer size. The default size is 256 bytes. The minimum and maximum sizes are 32 and 32766 respectively. uart.write() still includes checks for timeout, even if it is very unlikely to happen due to a) lack of flow control support and b) the minimal timeout values being longer than the time it needs to send a byte. --- ports/rp2/machine_uart.c | 159 +++++++++++++++++++++++++++++++++++---- ports/rp2/mpconfigport.h | 2 + 2 files changed, 147 insertions(+), 14 deletions(-) diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 936b9a93e9869..2da3ab41f86fa 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -28,9 +28,12 @@ #include "py/stream.h" #include "py/mphal.h" #include "py/mperrno.h" +#include "py/ringbuf.h" #include "modmachine.h" +#include "hardware/irq.h" #include "hardware/uart.h" +#include "hardware/regs/uart.h" #define DEFAULT_UART_BAUDRATE (115200) #define DEFAULT_UART_BITS (8) @@ -39,6 +42,9 @@ #define DEFAULT_UART0_RX (1) #define DEFAULT_UART1_TX (4) #define DEFAULT_UART1_RX (5) +#define DEFAULT_BUFFER_SIZE (256) +#define MIN_BUFFER_SIZE (32) +#define MAX_BUFFER_SIZE (32766) #define IS_VALID_PERIPH(uart, pin) (((((pin) + 4) & 8) >> 3) == (uart)) #define IS_VALID_TX(uart, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(uart, pin)) @@ -61,28 +67,81 @@ typedef struct _machine_uart_obj_t { uint16_t timeout; // timeout waiting for first char (in ms) uint16_t timeout_char; // timeout waiting between chars (in ms) uint8_t invert; + ringbuf_t read_buffer; + bool read_lock; + ringbuf_t write_buffer; + bool write_lock; } machine_uart_obj_t; STATIC machine_uart_obj_t machine_uart_obj[] = { - {{&machine_uart_type}, uart0, 0, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART0_TX, DEFAULT_UART0_RX, 0, 0, 0}, - {{&machine_uart_type}, uart1, 1, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, DEFAULT_UART1_TX, DEFAULT_UART1_RX, 0, 0, 0}, + {{&machine_uart_type}, uart0, 0, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, + DEFAULT_UART0_TX, DEFAULT_UART0_RX, 0, 0, 0, {NULL, 1, 0, 0}, 0, {NULL, 1, 0, 0}, 0}, + {{&machine_uart_type}, uart1, 1, 0, DEFAULT_UART_BITS, UART_PARITY_NONE, DEFAULT_UART_STOP, + DEFAULT_UART1_TX, DEFAULT_UART1_RX, 0, 0, 0, {NULL, 1, 0, 0}, 0, {NULL, 1, 0, 0}, 0,}, }; STATIC const char *_parity_name[] = {"None", "0", "1"}; STATIC const char *_invert_name[] = {"None", "INV_TX", "INV_RX", "INV_TX|INV_RX"}; +/******************************************************************************/ +// IRQ and buffer handling + +// take all bytes from the fifo and store them, if possible, in the buffer +STATIC void uart_drain_rx_fifo(machine_uart_obj_t *self) { + while (uart_is_readable(self->uart)) { + // try to write the data, ignore the fail + ringbuf_put(&(self->read_buffer), uart_get_hw(self->uart)->dr); + } +} + +// take bytes from the buffer and put them into the UART FIFO +STATIC void uart_fill_tx_fifo(machine_uart_obj_t *self) { + while (uart_is_writable(self->uart) && ringbuf_avail(&self->write_buffer) > 0) { + // get a byte from the buffer and put it into the uart + uart_get_hw(self->uart)->dr = ringbuf_get(&(self->write_buffer)); + } +} + +STATIC inline void uart_service_interrupt(machine_uart_obj_t *self) { + if (uart_get_hw(self->uart)->mis & UART_UARTMIS_RXMIS_BITS) { // rx interrupt? + // clear all interrupt bits but tx + uart_get_hw(self->uart)->icr = UART_UARTICR_BITS & (~UART_UARTICR_TXIC_BITS); + if (!self->read_lock) { + uart_drain_rx_fifo(self); + } + } + if (uart_get_hw(self->uart)->mis & UART_UARTMIS_TXMIS_BITS) { // tx interrupt? + // clear all interrupt bits but rx + uart_get_hw(self->uart)->icr = UART_UARTICR_BITS & (~UART_UARTICR_RXIC_BITS); + if (!self->write_lock) { + uart_fill_tx_fifo(self); + } + } +} + +STATIC void uart0_irq_handler(void) { + uart_service_interrupt(&machine_uart_obj[0]); +} + +STATIC void uart1_irq_handler(void) { + uart_service_interrupt(&machine_uart_obj[1]); +} + /******************************************************************************/ // MicroPython bindings for UART STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, timeout=%u, timeout_char=%u, invert=%s)", + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, " + "txbuf=%d, rxbuf=%d, timeout=%u, timeout_char=%u, invert=%s)", self->uart_id, self->baudrate, self->bits, _parity_name[self->parity], - self->stop, self->tx, self->rx, self->timeout, self->timeout_char, _invert_name[self->invert]); + self->stop, self->tx, self->rx, self->write_buffer.size - 1, self->read_buffer.size - 1, + self->timeout, self->timeout_char, _invert_name[self->invert]); } STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_timeout, ARG_timeout_char, ARG_invert }; + enum { ARG_id, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, + ARG_timeout, ARG_timeout_char, ARG_invert, ARG_txbuf, ARG_rxbuf}; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, @@ -94,6 +153,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_invert, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_txbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, }; // Parse args. @@ -169,6 +230,30 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->invert = args[ARG_invert].u_int; } + self->read_lock = false; + + // Set the RX buffer size if configured. + size_t rxbuf_len = DEFAULT_BUFFER_SIZE; + if (args[ARG_rxbuf].u_int > 0) { + rxbuf_len = args[ARG_rxbuf].u_int; + if (rxbuf_len < MIN_BUFFER_SIZE) { + rxbuf_len = MIN_BUFFER_SIZE; + } else if (rxbuf_len > MAX_BUFFER_SIZE) { + mp_raise_ValueError(MP_ERROR_TEXT("rxbuf too large")); + } + } + + // Set the TX buffer size if configured. + size_t txbuf_len = DEFAULT_BUFFER_SIZE; + if (args[ARG_txbuf].u_int > 0) { + txbuf_len = args[ARG_txbuf].u_int; + if (txbuf_len < MIN_BUFFER_SIZE) { + txbuf_len = MIN_BUFFER_SIZE; + } else if (txbuf_len > MAX_BUFFER_SIZE) { + mp_raise_ValueError(MP_ERROR_TEXT("txbuf too large")); + } + } + // Initialise the UART peripheral if any arguments given, or it was not initialised previously. if (n_args > 1 || n_kw > 0 || self->baudrate == 0) { if (self->baudrate == 0) { @@ -192,6 +277,25 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, if (self->invert & UART_INVERT_TX) { gpio_set_outover(self->tx, GPIO_OVERRIDE_INVERT); } + + // Allocate the RX/TX buffers. + ringbuf_alloc(&(self->read_buffer), rxbuf_len + 1); + MP_STATE_PORT(rp2_uart_rx_buffer[uart_id]) = self->read_buffer.buf; + + ringbuf_alloc(&(self->write_buffer), txbuf_len + 1); + MP_STATE_PORT(rp2_uart_tx_buffer[uart_id]) = self->write_buffer.buf; + + // Set the irq handler. + if (self->uart_id == 0) { + irq_set_exclusive_handler(UART0_IRQ, uart0_irq_handler); + irq_set_enabled(UART0_IRQ, true); + } else { + irq_set_exclusive_handler(UART1_IRQ, uart1_irq_handler); + irq_set_enabled(UART1_IRQ, true); + } + + // Enable the uart irq; this macro sets the rx irq level to 4. + uart_set_irq_enables(self->uart, true, true); } return MP_OBJ_FROM_PTR(self); @@ -199,7 +303,11 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(uart_is_readable(self->uart)); + // get all bytes from the fifo first + self->read_lock = true; + uart_drain_rx_fifo(self); + self->read_lock = false; + return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->read_buffer)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any); @@ -236,7 +344,7 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz for (size_t i = 0; i < size; i++) { // Wait for the first/next character - while (!uart_is_readable(self->uart)) { + while (ringbuf_avail(&self->read_buffer) == 0) { if (time_us_64() > t) { // timed out if (i <= 0) { *errcode = MP_EAGAIN; @@ -246,8 +354,12 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz } } MICROPY_EVENT_POLL_HOOK + // Force a few incoming bytes to the buffer + self->read_lock = true; + uart_drain_rx_fifo(self); + self->read_lock = false; } - *dest++ = uart_get_hw(self->uart)->dr; + *dest++ = ringbuf_get(&(self->read_buffer)); t = time_us_64() + timeout_char_us; } return size; @@ -258,10 +370,23 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin uint64_t t = time_us_64() + (uint64_t)self->timeout * 1000; uint64_t timeout_char_us = (uint64_t)self->timeout_char * 1000; const uint8_t *src = buf_in; + size_t i = 0; - for (size_t i = 0; i < size; i++) { - // wait for the first/next character - while (!uart_is_writable(self->uart)) { + // Put as many bytes as possible into the transmit buffer. + while (i < size && ringbuf_free(&(self->write_buffer)) > 0) { + ringbuf_put(&(self->write_buffer), *src++); + ++i; + } + + // Kickstart the UART transmit. + self->write_lock = true; + uart_fill_tx_fifo(self); + self->write_lock = false; + + // Send the next characters while busy waiting. + while (i < size) { + // Wait for the first/next character to be sent. + while (ringbuf_free(&(self->write_buffer)) == 0) { if (time_us_64() > t) { // timed out if (i <= 0) { *errcode = MP_EAGAIN; @@ -272,9 +397,15 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin } MICROPY_EVENT_POLL_HOOK } - uart_get_hw(self->uart)->dr = *src++; + ringbuf_put(&(self->write_buffer), *src++); + ++i; t = time_us_64() + timeout_char_us; + self->write_lock = true; + uart_fill_tx_fifo(self); + self->write_lock = false; } + + // Just in case the fifo was drained during refill of the ringbuf. return size; } @@ -284,10 +415,10 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint if (request == MP_STREAM_POLL) { uintptr_t flags = arg; ret = 0; - if ((flags & MP_STREAM_POLL_RD) && uart_is_readable(self->uart)) { + if ((flags & MP_STREAM_POLL_RD) && ringbuf_avail(&self->read_buffer) > 0) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && uart_is_writable(self->uart)) { + if ((flags & MP_STREAM_POLL_WR) && ringbuf_free(&self->write_buffer) > 0) { ret |= MP_STREAM_POLL_WR; } } else { diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 13db589ec0e3e..6c82d18620ea7 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -174,6 +174,8 @@ extern const struct _mp_obj_module_t mp_module_utime; void *machine_pin_irq_obj[30]; \ void *rp2_pio_irq_obj[2]; \ void *rp2_state_machine_irq_obj[8]; \ + void *rp2_uart_rx_buffer[2]; \ + void *rp2_uart_tx_buffer[2]; \ #define MP_STATE_PORT MP_STATE_VM From 7ca686684ea5bffb45764bc3782f54133855a3c9 Mon Sep 17 00:00:00 2001 From: jahr Date: Mon, 5 Apr 2021 22:57:18 +0200 Subject: [PATCH 0729/3533] rp2: Add support for building different board configurations. This change allows to build firmware for different rp2-based boards, following how it is done in other ports like stm32 and esp32. So far only the original Pico and Adafruit Feather RP2040 are added. Board names should match (sans case) those in pico-sdk/src/boards/include/boards/. Usage: Pico firmware can be build either using make as previously (it is the default board) or by `make BOARD=PICO`. Feather is built by `make BOARD=ADAFRUIT_FEATHER_RP2040`. Only the board name and flash drive size is set, pin definition is taken from the appropriate pico-sdk board definition. Firmware is saved in the directory build-BOARD_NAME. --- ports/rp2/CMakeLists.txt | 25 +++++++++++++++++++ ports/rp2/Makefile | 6 +++-- .../mpconfigboard.cmake | 1 + .../ADAFRUIT_FEATHER_RP2040/mpconfigboard.h | 3 +++ ports/rp2/boards/PICO/mpconfigboard.cmake | 1 + ports/rp2/boards/PICO/mpconfigboard.h | 3 +++ ports/rp2/mpconfigport.h | 3 ++- 7 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.cmake create mode 100644 ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h create mode 100644 ports/rp2/boards/PICO/mpconfigboard.cmake create mode 100644 ports/rp2/boards/PICO/mpconfigboard.h diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 100a83409002e..bd3eb74384b0d 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -17,6 +17,30 @@ endif() # Use the local tinyusb instead of the one in pico-sdk set(PICO_TINYUSB_PATH ${MICROPY_DIR}/lib/tinyusb) +# Set the location of this port's directory. +set(MICROPY_PORT_DIR ${CMAKE_SOURCE_DIR}) + +# Set the board if it's not already set. +if(NOT MICROPY_BOARD) + set(MICROPY_BOARD PICO) +endif() + +# Set the PICO_BOARD if it's not already set. +if(NOT PICO_BOARD) + string(TOLOWER ${MICROPY_BOARD} PICO_BOARD) +endif() + +# Set the board directory and check that it exists. +if(NOT MICROPY_BOARD_DIR) + set(MICROPY_BOARD_DIR ${MICROPY_PORT_DIR}/boards/${MICROPY_BOARD}) +endif() +if(NOT EXISTS ${MICROPY_BOARD_DIR}/mpconfigboard.cmake) + message(FATAL_ERROR "Invalid MICROPY_BOARD specified: ${MICROPY_BOARD}") +endif() + +# Include board config +include(${MICROPY_BOARD_DIR}/mpconfigboard.cmake) + # Include component cmake fragments include(${MICROPY_DIR}/py/py.cmake) include(${MICROPY_DIR}/extmod/extmod.cmake) @@ -150,6 +174,7 @@ target_link_libraries(${MICROPY_TARGET} usermod) target_include_directories(${MICROPY_TARGET} PRIVATE ${MICROPY_INC_CORE} ${MICROPY_INC_USERMOD} + ${MICROPY_BOARD_DIR} "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" ) diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 3358c4ccae66e..246f29dd0913e 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -2,11 +2,13 @@ # # This is a simple wrapper around cmake -BUILD = build +BOARD ?= PICO + +BUILD ?= build-$(BOARD) $(VERBOSE)MAKESILENT = -s -CMAKE_ARGS = +CMAKE_ARGS = -DMICROPY_BOARD=$(BOARD) ifdef USER_C_MODULES CMAKE_ARGS += -DUSER_C_MODULES=${USER_C_MODULES} diff --git a/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.cmake b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.cmake new file mode 100644 index 0000000000000..877efa6aba88c --- /dev/null +++ b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.cmake @@ -0,0 +1 @@ +# cmake file for Adafruit Feather RP2040 diff --git a/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h new file mode 100644 index 0000000000000..5068d35541a51 --- /dev/null +++ b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h @@ -0,0 +1,3 @@ +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "Adafruit Feather RP2040" +#define MICROPY_HW_FLASH_STORAGE_BYTES (3072 * 1024) diff --git a/ports/rp2/boards/PICO/mpconfigboard.cmake b/ports/rp2/boards/PICO/mpconfigboard.cmake new file mode 100644 index 0000000000000..3a40ca2871c6b --- /dev/null +++ b/ports/rp2/boards/PICO/mpconfigboard.cmake @@ -0,0 +1 @@ +# cmake file for Raspberry Pi Pico diff --git a/ports/rp2/boards/PICO/mpconfigboard.h b/ports/rp2/boards/PICO/mpconfigboard.h new file mode 100644 index 0000000000000..e6623374d0b44 --- /dev/null +++ b/ports/rp2/boards/PICO/mpconfigboard.h @@ -0,0 +1,3 @@ +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" +#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 6c82d18620ea7..218cd72752628 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -31,8 +31,9 @@ #include "hardware/sync.h" #include "pico/binary_info.h" +#include "mpconfigboard.h" + // Board and hardware specific configuration -#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" #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) From dd62c52a36e224cd0518d0361cc66a2f7e957f93 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 12 Apr 2021 16:21:54 +1000 Subject: [PATCH 0730/3533] stm32/rfcore: Fix race condition with C2 accessing free buffer list. Prior to this commit, if C2 was busy (eg lots of BLE activity) then it may not have had time to respond to the notification on the IPCC_CH_MM channel by the time additional memory was available to put on that buffer. In such a case C1 would modify the free buffer list while C2 was potentially accessing it, and this would eventually lead to lost memory buffers (or a corrupt linked list). If all buffers become lost then ACL packets (asynchronous events) can no longer be delivered from C2 to C1. This commit fixes this issue by waiting for C2 to indicate that it has finished using the free buffer list. Work done in collaboration with Jim Mussared aka @jimmo. Signed-off-by: Damien George --- ports/stm32/rfcore.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 55fc229dd0c30..878d5e1f8558c 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -417,6 +417,12 @@ STATIC void tl_process_msg(volatile tl_list_node_t *head, unsigned int ch, parse // If this node is allocated from the memmgr event pool, then place it into the free buffer. if ((uint8_t *)cur >= ipcc_membuf_memmgr_evt_pool && (uint8_t *)cur < ipcc_membuf_memmgr_evt_pool + sizeof(ipcc_membuf_memmgr_evt_pool)) { + // Wait for C2 to indicate that it has finished using the free buffer, + // so that we can link the newly-freed memory in to this buffer. + // If waiting is needed then it is typically between 5 and 20 microseconds. + while (LL_C1_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_MM)) { + } + // Place memory back in free pool. tl_list_append(&ipcc_mem_memmgr_free_buf_queue, cur); added_to_free_queue = true; From 2668337f36deef7fdd97f35e2efc68a5d2102192 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 12 Apr 2021 16:30:53 +1000 Subject: [PATCH 0731/3533] stm32/rfcore: Intercept addr-resolution HCI cmd to work around BLE bug. The STM32WB has a problem when address resolution is enabled: under certain conditions the MCU can get into a state where it draws an additional 10mA or so and eventually ends up with a broken BLE RX path in the silicon. A simple way to reproduce this is to enable address resolution (which is the default for NimBLE) and start the device advertising. If there is enough BLE activity in the vicinity then the device will at some point enter the bad state and, if left long enough, will have permanent BLE RX damage. STMicroelectronics are aware of this issue. The only known workaround at this stage is to not enable address resolution, which is implemented by this commit. Work done in collaboration with Jim Mussared aka @jimmo. Signed-off-by: Damien George --- ports/stm32/rfcore.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ports/stm32/rfcore.c b/ports/stm32/rfcore.c index 878d5e1f8558c..049f6bdc10c34 100644 --- a/ports/stm32/rfcore.c +++ b/ports/stm32/rfcore.c @@ -610,6 +610,24 @@ void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) { tl_list_node_t *n; uint32_t ch; if (src[0] == HCI_KIND_BT_CMD) { + // The STM32WB has a problem when address resolution is enabled: under certain + // conditions the MCU can get into a state where it draws an additional 10mA + // or so and eventually ends up with a broken BLE RX path in the silicon. A + // simple way to reproduce this is to enable address resolution (which is the + // default for NimBLE) and start the device advertising. If there is enough + // BLE activity in the vicinity then the device will at some point enter the + // bad state and, if left long enough, will have permanent BLE RX damage. + // + // STMicroelectronics are aware of this issue. The only known workaround at + // this stage is to not enable address resolution. We do that here by + // intercepting any command that enables address resolution and convert it + // into a command that disables address resolution. + // + // OGF=0x08 OCF=0x002d HCI_LE_Set_Address_Resolution_Enable + if (len == 5 && memcmp(src + 1, "\x2d\x20\x01\x01", 4) == 0) { + src = (const uint8_t *)"\x01\x2d\x20\x01\x00"; + } + n = (tl_list_node_t *)&ipcc_membuf_ble_cmd_buf[0]; ch = IPCC_CH_BLE; } else if (src[0] == HCI_KIND_BT_ACL) { From b26def0644b7f4c01cf616a24b6fe18f0737ebb6 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 9 Apr 2021 15:28:14 +0200 Subject: [PATCH 0732/3533] py/profile: Resolve name collision with STATIC unset. When building with STATIC undefined (e.g., -DSTATIC=), there are two instances of mp_type_code that collide at link time: in profile.c and in builtinevex.c. This patch resolves the collision by renaming one of them. --- py/profile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/profile.c b/py/profile.c index 863b0068a03e5..9cf8c4b7ba69f 100644 --- a/py/profile.c +++ b/py/profile.c @@ -172,7 +172,7 @@ STATIC void code_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_code = { +const mp_obj_type_t mp_type_settrace_codeobj = { { &mp_type_type }, .name = MP_QSTR_code, .print = code_print, @@ -185,7 +185,7 @@ mp_obj_t mp_obj_new_code(const mp_raw_code_t *rc) { if (o == NULL) { return MP_OBJ_NULL; } - o->base.type = &mp_type_code; + o->base.type = &mp_type_settrace_codeobj; o->rc = rc; o->dict_locals = mp_locals_get(); // this is a wrong! how to do this properly? o->lnotab = MP_OBJ_NULL; From 1a2ffda175bb5842617841362b2df40d7cfa97dd Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 22 Mar 2021 11:20:22 +0100 Subject: [PATCH 0733/3533] py/runtime: Make sys.modules preallocate to a configurable size. This allows configuring the pre-allocated size of sys.modules dict, in order to prevent unwanted reallocations at run-time (3 sys-modules is really not quite enough for a larger project). --- py/mpconfig.h | 5 +++++ py/runtime.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 454fc12b7cacd..b26fa2137a0b4 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -229,6 +229,11 @@ #define MICROPY_MODULE_DICT_SIZE (1) #endif +// Initial size of sys.modules dict +#ifndef MICROPY_LOADED_MODULES_DICT_SIZE +#define MICROPY_LOADED_MODULES_DICT_SIZE (3) +#endif + // Whether realloc/free should be passed allocated memory region size // You must enable this if MICROPY_MEM_STATS is enabled #ifndef MICROPY_MALLOC_USES_ALLOCATED_SIZE diff --git a/py/runtime.c b/py/runtime.c index 5d476a2764df3..0ce6854732822 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -91,7 +91,7 @@ void mp_init(void) { #endif // init global module dict - mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), 3); + mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), MICROPY_LOADED_MODULES_DICT_SIZE); // initialise the __main__ module mp_obj_dict_init(&MP_STATE_VM(dict_main), 1); From 25c029ce9fb0c2c313bfbf933e17648c98a33afc Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Apr 2021 23:22:08 +1000 Subject: [PATCH 0734/3533] stm32/boards: Split UARTx_RTS_DE into UARTx_RTS/UARTx_DE in pin defs. So these alternate functions can be parsed by the build scripts and used in application code. Signed-off-by: Damien George --- ports/stm32/boards/stm32l072_af.csv | 24 ++++++++++++------------ ports/stm32/boards/stm32l452_af.csv | 2 +- ports/stm32/boards/stm32l476_af.csv | 2 +- ports/stm32/boards/stm32wb55_af.csv | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ports/stm32/boards/stm32l072_af.csv b/ports/stm32/boards/stm32l072_af.csv index 0b1115b164a90..c9155bd444976 100644 --- a/ports/stm32/boards/stm32l072_af.csv +++ b/ports/stm32/boards/stm32l072_af.csv @@ -1,7 +1,7 @@ Port,Pin,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,, ,,SPI1/SPI2/I2S2/USART1/2/LPUART1/USB/LPTIM1/TSC/TIM2/21/22/EVENTOUT/SYS_AF,SPI1/SPI2/I2S2/I2C1/TIM2/21,SPI1/SPI2/I2S2/LPUART1/USART5/USB/LPTIM1/TIM2/3/EVENTOUT/SYS_AF,I2C1/TSC/EVENTOUT,I2C1/USART1/2/LPUART1/TIM3/22/EVENTOUT,SPI2/I2S2/I2C2/USART1/TIM2/21/22,I2C1/2/LPUART1/USART4/UASRT5/TIM21/EVENTOUT,I2C3/LPUART1/COMP1/2/TIM3,,ADC PortA,PA0,,,TIM2_CH1,TSC_G1_IO1,USART2_CTS,TIM2_ETR,USART4_TX,COMP1_OUT,,ADC_IN0 -PortA,PA1,EVENTOUT,,TIM2_CH2,TSC_G1_IO2,USART2_RTS_DE,TIM21_ETR,USART4_RX,,,ADC_IN1 +PortA,PA1,EVENTOUT,,TIM2_CH2,TSC_G1_IO2,USART2_RTS/USART2_DE,TIM21_ETR,USART4_RX,,,ADC_IN1 PortA,PA2,TIM21_CH1,,TIM2_CH3,TSC_G1_IO3,USART2_TX,,LPUART1_TX,COMP2_OUT,,ADC_IN2 PortA,PA3,TIM21_CH2,,TIM2_CH4,TSC_G1_IO4,USART2_RX,,LPUART1_RX,,,ADC_IN3 PortA,PA4,SPI1_NSS,,,TSC_G2_IO1,USART2_CK,TIM22_ETR,,,,ADC_IN4 @@ -12,25 +12,25 @@ PortA,PA8,MCO,,USB_CRS_SYNC,EVENTOUT,USART1_CK,,,I2C3_SCL,, PortA,PA9,MCO,,,TSC_G4_IO1,USART1_TX,,I2C1_SCL,I2C3_SMBA,, PortA,PA10,,,,TSC_G4_IO2,USART1_RX,,I2C1_SDA,,, PortA,PA11,SPI1_MISO,,EVENTOUT,TSC_G4_IO3,USART1_CTS,,,COMP1_OUT,, -PortA,PA12,SPI1_MOSI,,EVENTOUT,TSC_G4_IO4,USART1_RTS_DE,,,COMP2_OUT,, +PortA,PA12,SPI1_MOSI,,EVENTOUT,TSC_G4_IO4,USART1_RTS/USART1_DE,,,COMP2_OUT,, PortA,PA13,SWDIO,,USB_NOE,,,,LPUART1_RX,,, PortA,PA14,SWCLK,,,,USART2_TX,,LPUART1_TX,,, -PortA,PA15,SPI1_NSS,,TIM2_ETR,EVENTOUT,USART2_RX,TIM2_CH1,USART4_RTS_DE,,, +PortA,PA15,SPI1_NSS,,TIM2_ETR,EVENTOUT,USART2_RX,TIM2_CH1,USART4_RTS/USART4_DE,,, PortB,PB0,EVENTOUT,,TIM3_CH3,TSC_G3_IO2,,,,,,ADC_IN8 -PortB,PB1,,,TIM3_CH4,TSC_G3_IO3,LPUART1_RTS_DE,,,,,ADC_IN9 +PortB,PB1,,,TIM3_CH4,TSC_G3_IO3,LPUART1_RTS/LPUART1_DE,,,,,ADC_IN9 PortB,PB2,,,LPTIM1_OUT,TSC_G3_IO4,,,,I2C3_SMBA,, -PortB,PB3,SPI1_SCK,,TIM2_CH2,TSC_G5_IO1,EVENTOUT,USART1_RTS_DE,USART5_TX,,, +PortB,PB3,SPI1_SCK,,TIM2_CH2,TSC_G5_IO1,EVENTOUT,USART1_RTS/USART1_DE,USART5_TX,,, PortB,PB4,SPI1_MISO,,TIM3_CH1,TSC_G5_IO2,TIM22_CH1,USART1_CTS,USART5_RX,I2C3_SDA,, -PortB,PB5,SPI1_MOSI,,LPTIM1_IN1,I2C1_SMBA,TIM3_CH2/TIM22_CH2,USART1_CK,USART5_CK/USART5_RTS_DE,,, +PortB,PB5,SPI1_MOSI,,LPTIM1_IN1,I2C1_SMBA,TIM3_CH2/TIM22_CH2,USART1_CK,USART5_CK/USART5_RTS/USART5_DE,,, PortB,PB6,USART1_TX,I2C1_SCL,LPTIM1_ETR,TSC_G5_IO3,,,,,, PortB,PB7,USART1_RX,I2C1_SDA,LPTIM1_IN2,TSC_G5_IO4,,,USART4_CTS,,, PortB,PB8,,,,TSC_SYNC,I2C1_SCL,,,,, PortB,PB9,,,EVENTOUT,,I2C1_SDA,SPI2_NSS/I2S2_WS,,,, PortB,PB10,,,TIM2_CH3,TSC_SYNC,LPUART1_TX,SPI2_SCK,I2C2_SCL,LPUART1_RX,, PortB,PB11,EVENTOUT,,TIM2_CH4,TSC_G6_IO1,LPUART1_RX,,I2C2_SDA,LPUART1_TX,, -PortB,PB12,SPI2_NSS/I2S2_WS,,LPUART1_RTS_DE,TSC_G6_IO2,I2C2_SMBA,,EVENTOUT,,, +PortB,PB12,SPI2_NSS/I2S2_WS,,LPUART1_RTS/LPUART1_DE,TSC_G6_IO2,I2C2_SMBA,,EVENTOUT,,, PortB,PB13,SPI2_SCK/I2S2_CK,,MCO,TSC_G6_IO3,LPUART1_CTS,I2C2_SCL,TIM21_CH1,,, -PortB,PB14,SPI2_MISO/I2S2_MCK,,RTC_OUT,TSC_G6_IO4,LPUART1_RTS_DE,I2C2_SDA,TIM21_CH2,,, +PortB,PB14,SPI2_MISO/I2S2_MCK,,RTC_OUT,TSC_G6_IO4,LPUART1_RTS/LPUART1_DE,I2C2_SDA,TIM21_CH2,,, PortB,PB15,SPI2_MOSI/I2S2_SD,,RTC_REFIN,,,,,,, PortC,PC0,LPTIM1_IN1,,EVENTOUT,TSC_G7_IO1,,,LPUART1_RX,I2C3_SCL,,ADC_IN10 PortC,PC1,LPTIM1_OUT,,EVENTOUT,TSC_G7_IO2,,,LPUART1_TX,I2C3_SDA,,ADC_IN11 @@ -50,9 +50,9 @@ PortC,PC14,,,,,,,,,, PortC,PC15,,,,,,,,,, PortD,PD0,TIM21_CH1,SPI2_NSS/I2S2_WS,,,,,,,, PortD,PD1,,SPI2_SCK/I2S2_CK,,,,,,,, -PortD,PD2,LPUART1_RTS_DE,,TIM3_ETR,,,,USART5_RX,,, +PortD,PD2,LPUART1_RTS/LPUART1_DE,,TIM3_ETR,,,,USART5_RX,,, PortD,PD3,USART2_CTS,,SPI2_MISO/I2S2_MCK,,,,,,, -PortD,PD4,USART2_RTS_DE,SPI2_MOSI/I2S2_SD,,,,,,,, +PortD,PD4,USART2_RTS/USART2_DE,SPI2_MOSI/I2S2_SD,,,,,,,, PortD,PD5,USART2_TX,,,,,,,,, PortD,PD6,USART2_RX,,,,,,,,, PortD,PD7,USART2_CK,TIM21_CH2,,,,,,,, @@ -60,7 +60,7 @@ PortD,PD8,LPUART1_TX,,,,,,,,, PortD,PD9,LPUART1_RX,,,,,,,,, PortD,PD10,,,,,,,,,, PortD,PD11,LPUART1_CTS,,,,,,,,, -PortD,PD12,LPUART1_RTS_DE,,,,,,,,, +PortD,PD12,LPUART1_RTS/LPUART1_DE,,,,,,,,, PortD,PD13,,,,,,,,,, PortD,PD14,,,,,,,,,, PortD,PD15,USB_CRS_SYNC,,,,,,,,, @@ -71,7 +71,7 @@ PortE,PE3,TIM22_CH1,,TIM3_CH1,,,,,,, PortE,PE4,TIM22_CH2,,TIM3_CH2,,,,,,, PortE,PE5,TIM21_CH1,,TIM3_CH3,,,,,,, PortE,PE6,TIM21_CH2,,TIM3_CH4,,,,,,, -PortE,PE7,,,,,,,USART5_CK/USART5_RTS_DE,,, +PortE,PE7,,,,,,,USART5_CK/USART5_RTS/USART5_DE,,, PortE,PE8,,,,,,,USART4_TX,,, PortE,PE9,TIM2_CH1,,TIM2_ETR,,,,USART4_RX,,, PortE,PE10,TIM2_CH2,,,,,,USART5_TX,,, diff --git a/ports/stm32/boards/stm32l452_af.csv b/ports/stm32/boards/stm32l452_af.csv index f7170d1c65db5..1de5c211d2128 100644 --- a/ports/stm32/boards/stm32l452_af.csv +++ b/ports/stm32/boards/stm32l452_af.csv @@ -30,7 +30,7 @@ PortB,PB10,,TIM2_CH3,,I2C4_SCL,I2C2_SCL,SPI2_SCK,,USART3_TX,LPUART1_RX,TSC_SYNC, PortB,PB11,,TIM2_CH4,,I2C4_SDA,I2C2_SDA,,,USART3_RX,LPUART1_TX,,QUADSPI_NCS,,COMP2_OUT,,,EVENTOUT,,, PortB,PB12,,TIM1_BKIN,,TIM1_BKIN_COMP2,I2C2_SMBA,SPI2_NSS,DFSDM_DATIN1,USART3_CK,LPUART1_RTS/LPUART1_DE,TSC_G1_IO1,CAN1_RX,,,SAI1_FS_A,TIM15_BKIN,EVENTOUT,,, PortB,PB13,,TIM1_CH1N,,,I2C2_SCL,SPI2_SCK,DFSDM_CKIN1,USART3_CTS,LPUART1_CTS,TSC_G1_IO2,CAN1_TX,,,SAI1_SCK_A,TIM15_CH1N,EVENTOUT,,, -PortB,PB14,,TIM1_CH2N,,,I2C2_SDA,SPI2_MISO,DFSDM_DATIN2,USART3_RTS_DE,,TSC_G1_IO3,,,,SAI1_MCLK_A,TIM15_CH1,EVENTOUT,,, +PortB,PB14,,TIM1_CH2N,,,I2C2_SDA,SPI2_MISO,DFSDM_DATIN2,USART3_RTS/USART3_DE,,TSC_G1_IO3,,,,SAI1_MCLK_A,TIM15_CH1,EVENTOUT,,, PortB,PB15,RTC_REFIN,TIM1_CH3N,,,,SPI2_MOSI,DFSDM_CKIN2,,,TSC_G1_IO4,,,,SAI1_SD_A,TIM15_CH2,EVENTOUT,,, PortC,PC0,,LPTIM1_IN1,I2C4_SCL,,I2C3_SCL,,,,LPUART1_RX,,,,,,LPTIM2_IN1,EVENTOUT,ADC123_IN1,, PortC,PC1,TRACED0,LPTIM1_OUT,I2C4_SDA,,I2C3_SDA,,,,LPUART1_TX,,,,,,,EVENTOUT,ADC123_IN2,, diff --git a/ports/stm32/boards/stm32l476_af.csv b/ports/stm32/boards/stm32l476_af.csv index 01db895725def..df824ed7080fc 100644 --- a/ports/stm32/boards/stm32l476_af.csv +++ b/ports/stm32/boards/stm32l476_af.csv @@ -30,7 +30,7 @@ PortB,PB10,,TIM2_CH3,,,I2C2_SCL,SPI2_SCK,DFSDM_DATIN7,USART3_TX,LPUART1_RX,,QUAD PortB,PB11,,TIM2_CH4,,,I2C2_SDA,,DFSDM_CKIN7,USART3_RX,LPUART1_TX,,QUADSPI_NCS,LCD_SEG11,COMP2_OUT,,,EVENTOUT,, PortB,PB12,,TIM1_BKIN,,TIM1_BKIN_COMP2,I2C2_SMBA,SPI2_NSS,DFSDM_DATIN1,USART3_CK,LPUART1_RTS/LPUART1_DE,TSC_G1_IO1,,LCD_SEG12,SWPMI1_IO,SAI2_FS_A,TIM15_BKIN,EVENTOUT,, PortB,PB13,,TIM1_CH1N,,,I2C2_SCL,SPI2_SCK,DFSDM_CKIN1,USART3_CTS,LPUART1_CTS,TSC_G1_IO2,,LCD_SEG13,SWPMI1_TX,SAI2_SCK_A,TIM15_CH1N,EVENTOUT,, -PortB,PB14,,TIM1_CH2N,,TIM8_CH2N,I2C2_SDA,SPI2_MISO,DFSDM_DATIN2,USART3_RTS_DE,,TSC_G1_IO3,,LCD_SEG14,SWPMI1_RX,SAI2_MCLK_A,TIM15_CH1,EVENTOUT,, +PortB,PB14,,TIM1_CH2N,,TIM8_CH2N,I2C2_SDA,SPI2_MISO,DFSDM_DATIN2,USART3_RTS/USART3_DE,,TSC_G1_IO3,,LCD_SEG14,SWPMI1_RX,SAI2_MCLK_A,TIM15_CH1,EVENTOUT,, PortB,PB15,RTC_REFIN,TIM1_CH3N,,TIM8_CH3N,,SPI2_MOSI,DFSDM_CKIN2,,,TSC_G1_IO4,,LCD_SEG15,SWPMI1_SUSPEND,SAI2_SD_A,TIM15_CH2,EVENTOUT,, PortC,PC0,,LPTIM1_IN1,,,I2C3_SCL,,DFSDM_DATIN4,,LPUART1_RX,,,LCD_SEG18,,,LPTIM2_IN1,EVENTOUT,ADC123_IN1, PortC,PC1,,LPTIM1_OUT,,,I2C3_SDA,,DFSDM_CKIN4,,LPUART1_TX,,,LCD_SEG19,,,,EVENTOUT,ADC123_IN2, diff --git a/ports/stm32/boards/stm32wb55_af.csv b/ports/stm32/boards/stm32wb55_af.csv index 074c33089261d..d6e1c3f77334c 100644 --- a/ports/stm32/boards/stm32wb55_af.csv +++ b/ports/stm32/boards/stm32wb55_af.csv @@ -12,14 +12,14 @@ PortA,PA8,MCO,TIM1_CH1,,SAI1_PDM_CK2,,,,USART1_CK,,,,LCD_COM0,,SAI1_SCK_A,LPTIM2 PortA,PA9,,TIM1_CH2,,SAI1_PDM_DI2,I2C1_SCL,SPI2_SCK,,USART1_TX,,,,LCD_COM1,,SAI1_FS_A,,EVENTOUT,ADC1_IN16 PortA,PA10,,TIM1_CH3,,SAI1_PDM_DI1,I2C1_SDA,,,USART1_RX,,,USB_CRS_SYNC,LCD_COM2,,SAI1_SD_A,TIM17_BKIN,EVENTOUT, PortA,PA11,,TIM1_CH4,TIM1_BKIN2,,,SPI1_MISO,,USART1_CTS,,,USB_DM,,TIM1_BKIN2,,,EVENTOUT, -PortA,PA12,,TIM1_ETR,,,,SPI1_MOSI,,USART1_RTS_DE,LPUART1_RX,,USB_DP,,,,,EVENTOUT, +PortA,PA12,,TIM1_ETR,,,,SPI1_MOSI,,USART1_RTS/USART1_DE,LPUART1_RX,,USB_DP,,,,,EVENTOUT, PortA,PA13,JTMS/SWDIO,,,,,,,,IR_OUT,,USB_NOE,,,SAI1_SD_B,,EVENTOUT, PortA,PA14,JTCK/SWCLK,LPTIM1_OUT,,,I2C1_SMBA,,,,,,,LCD_SEG5,,SAI1_FS_B,,EVENTOUT, PortA,PA15,JTDI,TIM2_CH1,TIM2_ETR,,,SPI1_NSS,,,,TSC_G3_IO1,,LCD_SEG17,,,,EVENTOUT, PortB,PB0,,,,,,,EXT_PA_TX,,,,,,COMP1_OUT,,,EVENTOUT, -PortB,PB1,,,,,,,,,LPUART1_RTS_DE,,,,,,LPTIM2_IN1,EVENTOUT, +PortB,PB1,,,,,,,,,LPUART1_RTS/LPUART1_DE,,,,,,LPTIM2_IN1,EVENTOUT, PortB,PB2,RTC_OUT,LPTIM1_OUT,,,I2C3_SMBA,SPI1_NSS,,,,,,LCD_VLCD,,SAI1_EXTCLK,,EVENTOUT, -PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,,SPI1_SCK,,USART1_RTS_DE,,,,LCD_SEG7,,SAI1_SCK_B,,EVENTOUT, +PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,,SPI1_SCK,,USART1_RTS/USART1_DE,,,,LCD_SEG7,,SAI1_SCK_B,,EVENTOUT, PortB,PB4,NJTRST,,,,I2C3_SDA,SPI1_MISO,,USART1_CTS,,TSC_G2_IO1,,LCD_SEG8,,SAI1_MCLK_B,TIM17_BKIN,EVENTOUT, PortB,PB5,,LPTIM1_IN1,,,I2C1_SMBA,SPI1_MOSI,,USART1_CK,LPUART1_TX,TSC_G2_IO2,,LCD_SEG9,COMP2_OUT,SAI1_SD_B,TIM16_BKIN,EVENTOUT, PortB,PB6,MCO,LPTIM1_ETR,,,I2C1_SCL,,,USART1_TX,,TSC_G2_IO3,,LCD_SEG6,,SAI1_FS_B,TIM16_CH1N,EVENTOUT, From 2ac09c2694f89b11544f4abf10413c64eeec2b15 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Apr 2021 23:56:37 +1000 Subject: [PATCH 0735/3533] stm32/uart: Use LL_USART_GetBaudRate to compute baudrate. This function includes the UART prescaler in the calculation (if it has one, eg on H7 and WB MCUs). Signed-off-by: Damien George --- ports/stm32/uart.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index d40a883c52b84..36c59cee4ae02 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -711,16 +711,11 @@ uint32_t uart_get_source_freq(pyb_uart_obj_t *self) { } uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { - // This formula assumes UART_OVERSAMPLING_16 - uint32_t source_freq = uart_get_source_freq(self); - #if defined(LPUART1) - if (self->uart_id == PYB_LPUART_1) { - return source_freq / (self->uartx->BRR >> 8); - } else - #endif - { - return source_freq / self->uartx->BRR; - } + return LL_USART_GetBaudRate(self->uartx, uart_get_source_freq(self), + #if defined(STM32H7) || defined(STM32WB) + self->uartx->PRESC, + #endif + LL_USART_OVERSAMPLING_16); } void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) { From 9c9bfe1968d4260924316353bd1c941fe7e8741d Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 30 Mar 2021 20:08:11 +0200 Subject: [PATCH 0736/3533] unix/main: Make static variable that's potentially clobbered by longjmp. This fixes `error: variable 'subpkg_tried' might be clobbered by 'longjmp' or 'vfork' [-Werror=clobbered]` when compiling on ppc64le and aarch64 (and possibly other architectures/toolchains). --- ports/unix/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index 129c6d3bb43a1..e33c1742d8301 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -596,7 +596,12 @@ MP_NOINLINE int main_(int argc, char **argv) { mp_obj_t mod; nlr_buf_t nlr; - bool subpkg_tried = false; + + // Allocating subpkg_tried on the stack can lead to compiler warnings about this + // variable being clobbered when nlr is implemented using setjmp/longjmp. Its + // value must be preserved across calls to setjmp/longjmp. + static bool subpkg_tried; + subpkg_tried = false; reimport: if (nlr_push(&nlr) == 0) { From fc6ea28d00ca733f3ed806cc660f94c606ded4fb Mon Sep 17 00:00:00 2001 From: 8bitgeek Date: Mon, 5 Apr 2021 06:55:39 -0400 Subject: [PATCH 0737/3533] stm32/sdram: Make MICROPY_HW_FMC_BA1,MICROPY_HW_FMC_A11 optional pins. This supports SDRAM having only 2 internal banks (using BA0 only), and only 11 (A0-A10) bits of address, such as IS42S16100H (512K x 16bit x 2bank). --- ports/stm32/sdram.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/sdram.c b/ports/stm32/sdram.c index e8892b57463a4..2791df277afa2 100644 --- a/ports/stm32/sdram.c +++ b/ports/stm32/sdram.c @@ -73,7 +73,9 @@ bool sdram_init(void) { mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNRAS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNRAS); mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNWE, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNWE); mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_BA0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_BA0); + #ifdef MICROPY_HW_FMC_BA1 mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_BA1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_BA1); + #endif mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL0); mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL1); #ifdef MICROPY_HW_FMC_NBL2 @@ -91,7 +93,9 @@ bool sdram_init(void) { mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A8); mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A9); mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A10); + #ifdef MICROPY_HW_FMC_A11 mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A11); + #endif #ifdef MICROPY_HW_FMC_A12 mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A12); #endif From d0e014aa41e32a80a7b591d4de6f2b1ed1298e4d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Apr 2021 12:55:53 +1000 Subject: [PATCH 0738/3533] mimxrt: Enable CPYTHON_COMPAT, PY_ASYNC_AWAIT, PY_ATTRTUPLE options. This change allows running the tests in tests/basics/ without any failures (but some tests are still skipped). Signed-off-by: Damien George --- ports/mimxrt/mpconfigport.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 68c7a894221d8..65b91675a5de6 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -46,11 +46,9 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) -#define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) // Control over Python builtins -#define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_BUILTINS_STR_COUNT (0) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) #define MICROPY_PY_BUILTINS_SET (1) @@ -63,7 +61,6 @@ #define MICROPY_PY___FILE__ (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) -#define MICROPY_PY_ATTRTUPLE (0) #define MICROPY_PY_COLLECTIONS (0) #define MICROPY_PY_SYS_MAXSIZE (1) From 7f366a2190825555c16f57f5dfd4d0d57efd7c1f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Apr 2021 14:55:55 +1000 Subject: [PATCH 0739/3533] esp32/modsocket: Correctly handle poll/read of unconnected TCP socket. For an unconnected TCP socket, poll should return WR|HUP and read should raise ENOTCONN. This is implemented by this commit and now the following tests pass on esp32: extmod/usocket_tcp_basic.py, net_hosted/connect_poll.py. Signed-off-by: Damien George --- ports/esp32/modsocket.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 5135e31631e04..67b1d7d6f0591 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -59,13 +59,19 @@ #define MDNS_QUERY_TIMEOUT_MS (5000) #define MDNS_LOCAL_SUFFIX ".local" +enum { + SOCKET_STATE_NEW, + SOCKET_STATE_CONNECTED, + SOCKET_STATE_PEER_CLOSED, +}; + typedef struct _socket_obj_t { mp_obj_base_t base; int fd; uint8_t domain; uint8_t type; uint8_t proto; - bool peer_closed; + uint8_t state; unsigned int retries; #if MICROPY_PY_USOCKET_EVENTS mp_obj_t events_callback; @@ -254,7 +260,6 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz sock->domain = AF_INET; sock->type = SOCK_STREAM; sock->proto = 0; - sock->peer_closed = false; if (n_args > 0) { sock->domain = mp_obj_get_int(args[0]); if (n_args > 1) { @@ -265,6 +270,8 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz } } + sock->state = sock->type == SOCK_STREAM ? SOCKET_STATE_NEW : SOCKET_STATE_CONNECTED; + sock->fd = lwip_socket(sock->domain, sock->type, sock->proto); if (sock->fd < 0) { mp_raise_OSError(errno); @@ -278,6 +285,7 @@ STATIC mp_obj_t socket_bind(const mp_obj_t arg0, const mp_obj_t arg1) { socket_obj_t *self = MP_OBJ_TO_PTR(arg0); struct addrinfo *res; _socket_getaddrinfo(arg1, &res); + self->state = SOCKET_STATE_CONNECTED; int r = lwip_bind(self->fd, res->ai_addr, res->ai_addrlen); lwip_freeaddrinfo(res); if (r < 0) { @@ -290,6 +298,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind); STATIC mp_obj_t socket_listen(const mp_obj_t arg0, const mp_obj_t arg1) { socket_obj_t *self = MP_OBJ_TO_PTR(arg0); int backlog = mp_obj_get_int(arg1); + self->state = SOCKET_STATE_CONNECTED; int r = lwip_listen(self->fd, backlog); if (r < 0) { mp_raise_OSError(errno); @@ -332,7 +341,7 @@ STATIC mp_obj_t socket_accept(const mp_obj_t arg0) { sock->domain = self->domain; sock->type = self->type; sock->proto = self->proto; - sock->peer_closed = false; + sock->state = SOCKET_STATE_CONNECTED; _socket_settimeout(sock, UINT64_MAX); // make the return value @@ -351,6 +360,7 @@ STATIC mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) { struct addrinfo *res; _socket_getaddrinfo(arg1, &res); MP_THREAD_GIL_EXIT(); + self->state = SOCKET_STATE_CONNECTED; int r = lwip_connect(self->fd, res->ai_addr, res->ai_addrlen); MP_THREAD_GIL_ENTER(); lwip_freeaddrinfo(res); @@ -471,11 +481,17 @@ STATIC mp_uint_t _socket_read_data(mp_obj_t self_in, void *buf, size_t size, struct sockaddr *from, socklen_t *from_len, int *errcode) { socket_obj_t *sock = MP_OBJ_TO_PTR(self_in); + // A new socket cannot be read from. + if (sock->state == SOCKET_STATE_NEW) { + *errcode = MP_ENOTCONN; + return MP_STREAM_ERROR; + } + // If the peer closed the connection then the lwIP socket API will only return "0" once // from lwip_recvfrom and then block on subsequent calls. To emulate POSIX behaviour, // which continues to return "0" for each call on a closed socket, we set a flag when // the peer closed the socket. - if (sock->peer_closed) { + if (sock->state == SOCKET_STATE_PEER_CLOSED) { return 0; } @@ -500,7 +516,7 @@ STATIC mp_uint_t _socket_read_data(mp_obj_t self_in, void *buf, size_t size, MP_THREAD_GIL_ENTER(); } if (r == 0) { - sock->peer_closed = true; + sock->state = SOCKET_STATE_PEER_CLOSED; } if (r >= 0) { return r; @@ -702,6 +718,12 @@ STATIC mp_uint_t socket_stream_ioctl(mp_obj_t self_in, mp_uint_t request, uintpt if (FD_ISSET(socket->fd, &efds)) { ret |= MP_STREAM_POLL_HUP; } + + // New (unconnected) sockets are writable and have HUP set. + if (socket->state == SOCKET_STATE_NEW) { + ret |= (arg & MP_STREAM_POLL_WR) | MP_STREAM_POLL_HUP; + } + return ret; } else if (request == MP_STREAM_CLOSE) { if (socket->fd >= 0) { From 8459f538eb45fd8e1e4d614298449cf18de84d75 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 15 Apr 2021 00:47:24 +1000 Subject: [PATCH 0740/3533] tests/feature_check: Check for lack of pass result rather than failure. Commit cb68a5741aba5d4935428674234a9d643f97405f broke automatic Python feature detection when running tests, because some detection relied on a crash of a feature script returning exactly b"CRASH". This commit fixes this and improves the situation by testing for the lack of a known pass result, rather than an exact failure result. Signed-off-by: Damien George --- tests/feature_check/async_check.py | 3 +++ tests/feature_check/const.py | 1 + tests/feature_check/native_check.py | 4 ++++ tests/feature_check/set_check.py | 2 +- tests/run-tests.py | 14 +++++++------- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/feature_check/async_check.py b/tests/feature_check/async_check.py index 0f6361cd12016..727b7136a5ba6 100644 --- a/tests/feature_check/async_check.py +++ b/tests/feature_check/async_check.py @@ -1,3 +1,6 @@ # check if async/await keywords are supported async def foo(): await 1 + + +print("async") diff --git a/tests/feature_check/const.py b/tests/feature_check/const.py index db32e8c69bd7c..e5928f6d768de 100644 --- a/tests/feature_check/const.py +++ b/tests/feature_check/const.py @@ -1 +1,2 @@ x = const(1) +print(x) diff --git a/tests/feature_check/native_check.py b/tests/feature_check/native_check.py index 3971d1355fbaf..4dc9754d0c12b 100644 --- a/tests/feature_check/native_check.py +++ b/tests/feature_check/native_check.py @@ -2,3 +2,7 @@ @micropython.native def f(): pass + + +f() +print("native") diff --git a/tests/feature_check/set_check.py b/tests/feature_check/set_check.py index ec186cc5b9221..0c7612843a0c9 100644 --- a/tests/feature_check/set_check.py +++ b/tests/feature_check/set_check.py @@ -1,2 +1,2 @@ # check if set literal syntax is supported -{1} +print({1}) diff --git a/tests/run-tests.py b/tests/run-tests.py index ae63f9a29a6e2..a5a9b60637cc3 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -279,7 +279,7 @@ def run_tests(pyb, tests, args, result_dir): # Check if micropython.native is supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, "native_check.py") - if output == b"CRASH": + if output != b"native\n": skip_native = True # Check if arbitrary-precision integers are supported, and skip such tests if it's not @@ -294,7 +294,7 @@ def run_tests(pyb, tests, args, result_dir): # Check if set type (and set literals) is supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, "set_check.py") - if output == b"CRASH": + if output != b"{1}\n": skip_set_type = True # Check if slice is supported, and skip such tests if it's not @@ -304,12 +304,12 @@ def run_tests(pyb, tests, args, result_dir): # Check if async/await keywords are supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, "async_check.py") - if output == b"CRASH": + if output != b"async\n": skip_async = True # Check if const keyword (MicroPython extension) is supported, and skip such tests if it's not output = run_feature_check(pyb, args, base_path, "const.py") - if output == b"CRASH": + if output != b"1\n": skip_const = True # Check if __rOP__ special methods are supported, and skip such tests if it's not @@ -334,10 +334,10 @@ def run_tests(pyb, tests, args, result_dir): upy_byteorder = run_feature_check(pyb, args, base_path, "byteorder.py") upy_float_precision = run_feature_check(pyb, args, base_path, "float.py") - if upy_float_precision == b"CRASH": - upy_float_precision = 0 - else: + try: upy_float_precision = int(upy_float_precision) + except ValueError: + upy_float_precision = 0 has_complex = run_feature_check(pyb, args, base_path, "complex.py") == b"complex\n" has_coverage = run_feature_check(pyb, args, base_path, "coverage.py") == b"coverage\n" cpy_byteorder = subprocess.check_output( From 66a86a061530eeee51191c3c667e9bc3cfcfda40 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Feb 2021 21:24:34 +1100 Subject: [PATCH 0741/3533] esp32: Add initial support for ESP32S2 SoCs. Builds against IDF v4.3-beta2. Signed-off-by: Damien George --- ports/esp32/boards/sdkconfig.base | 1 - ports/esp32/esp32_ulp.c | 4 ++++ ports/esp32/machine_adc.c | 16 +++++++++++++++- ports/esp32/machine_dac.c | 5 +++++ ports/esp32/machine_hw_spi.c | 10 ++++++++-- ports/esp32/machine_i2c.c | 5 +++++ ports/esp32/machine_pin.c | 14 ++++++++++++++ ports/esp32/machine_pwm.c | 4 ++++ ports/esp32/machine_sdcard.c | 6 ++---- ports/esp32/machine_timer.c | 21 ++++++++++++++++++++- ports/esp32/machine_touchpad.c | 13 ++++++------- ports/esp32/machine_uart.c | 2 ++ ports/esp32/main.c | 5 +++++ ports/esp32/main/CMakeLists.txt | 7 ++++++- ports/esp32/modesp32.c | 8 ++++++++ ports/esp32/modmachine.c | 16 ++++++++++++++-- ports/esp32/mpconfigport.h | 2 ++ ports/esp32/uart.c | 4 ++++ 18 files changed, 124 insertions(+), 19 deletions(-) diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index 91e68c7bf6a9c..4d9e38077d908 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -1,7 +1,6 @@ # MicroPython on ESP32, ESP IDF configuration # The following options override the defaults -CONFIG_IDF_TARGET="esp32" CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000 # Compiler options: use -Os to reduce size, but keep full assertions diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 50244cdf25060..8e4ce9c5a41b9 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -26,6 +26,8 @@ #include "py/runtime.h" +#if CONFIG_IDF_TARGET_ESP32 + #include "esp32/ulp.h" #include "esp_err.h" @@ -95,3 +97,5 @@ const mp_obj_type_t esp32_ulp_type = { .make_new = esp32_ulp_make_new, .locals_dict = (mp_obj_t)&esp32_ulp_locals_dict, }; + +#endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 811a208e6d54f..4c19d5992bd8a 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -60,7 +60,11 @@ STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n static int initialized = 0; if (!initialized) { - adc1_config_width(ADC_WIDTH_12Bit); + #if CONFIG_IDF_TARGET_ESP32S2 + adc1_config_width(ADC_WIDTH_BIT_13); + #else + adc1_config_width(ADC_WIDTH_BIT_12); + #endif adc_bit_width = 12; initialized = 1; } @@ -128,6 +132,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { mp_raise_ValueError(MP_ERROR_TEXT("parameter error")); } switch (width) { + #if CONFIG_IDF_TARGET_ESP32 case ADC_WIDTH_9Bit: adc_bit_width = 9; break; @@ -140,6 +145,11 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { case ADC_WIDTH_12Bit: adc_bit_width = 12; break; + #elif CONFIG_IDF_TARGET_ESP32S2 + case ADC_WIDTH_BIT_13: + adc_bit_width = 13; + break; + #endif default: break; } @@ -160,10 +170,14 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_6db) }, { MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_11db) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(ADC_WIDTH_9Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) }, + #elif CONFIG_IDF_TARGET_ESP32S2 + { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table); diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 02855fcf8048e..146ef60aa81d8 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -43,8 +43,13 @@ typedef struct _mdac_obj_t { } mdac_obj_t; STATIC const mdac_obj_t mdac_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, + #else + {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, + {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, + #endif }; STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index d59f2c750e666..a49a58035ae3e 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -184,8 +184,12 @@ STATIC void machine_hw_spi_init_internal( changed = true; } - if (self->host != HSPI_HOST && self->host != VSPI_HOST) { - mp_raise_ValueError(MP_ERROR_TEXT("SPI ID must be either HSPI(1) or VSPI(2)")); + if (self->host != HSPI_HOST + #ifdef VSPI_HOST + && self->host != VSPI_HOST + #endif + ) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host); } if (changed) { @@ -220,8 +224,10 @@ STATIC void machine_hw_spi_init_internal( int dma_chan = 0; if (self->host == HSPI_HOST) { dma_chan = 1; + #ifdef VSPI_HOST } else if (self->host == VSPI_HOST) { dma_chan = 2; + #endif } ret = spi_bus_initialize(self->host, &buscfg, dma_chan); diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 3993c7b52b9bb..fd5180b8d0709 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -34,8 +34,13 @@ #define I2C_0_DEFAULT_SCL (GPIO_NUM_18) #define I2C_0_DEFAULT_SDA (GPIO_NUM_19) +#if CONFIG_IDF_TARGET_ESP32 #define I2C_1_DEFAULT_SCL (GPIO_NUM_25) #define I2C_1_DEFAULT_SDA (GPIO_NUM_26) +#else +#define I2C_1_DEFAULT_SCL (GPIO_NUM_9) +#define I2C_1_DEFAULT_SDA (GPIO_NUM_8) +#endif #define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index dcdd53be92378..8290c77a48263 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -77,10 +77,17 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_21}, + #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_type}, GPIO_NUM_22}, {{&machine_pin_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_25}, + #else + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + #endif {{&machine_pin_type}, GPIO_NUM_26}, {{&machine_pin_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -411,10 +418,17 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_21}, + #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_irq_type}, GPIO_NUM_22}, {{&machine_pin_irq_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_25}, + #else + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + #endif {{&machine_pin_irq_type}, GPIO_NUM_26}, {{&machine_pin_irq_type}, GPIO_NUM_27}, {{NULL}, -1}, diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 7592f243b76f3..a7d7d29df8541 100644 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -50,7 +50,11 @@ STATIC int chan_gpio[LEDC_CHANNEL_MAX]; // 5khz #define PWFREQ (5000) // High speed mode +#if CONFIG_IDF_TARGET_ESP32 #define PWMODE (LEDC_HIGH_SPEED_MODE) +#else +#define PWMODE (LEDC_LOW_SPEED_MODE) +#endif // 10-bit resolution (compatible with esp8266 PWM) #define PWRES (LEDC_TIMER_10_BIT) // Timer 1 diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 40686508a1e20..c9a9face7c242 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -31,6 +31,8 @@ #include "py/mperrno.h" #include "extmod/vfs_fat.h" +#if MICROPY_HW_ENABLE_SDCARD + #include "driver/sdmmc_host.h" #include "driver/sdspi_host.h" #include "sdmmc_cmd.h" @@ -50,10 +52,6 @@ // Hosts are de-inited in __del__. Slots do not need de-initing. // -// Currently the ESP32 Library doesn't support MMC cards, so -// we don't enable on MICROPY_HW_ENABLE_MMCARD. -#if MICROPY_HW_ENABLE_SDCARD - // Forward declaration const mp_obj_type_t machine_sdcard_type; diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 6e5824094ea25..696127af771da 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -30,12 +30,17 @@ #include #include -#include "driver/timer.h" #include "py/obj.h" #include "py/runtime.h" #include "modmachine.h" #include "mphalport.h" +#include "driver/timer.h" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 1) +#include "hal/timer_ll.h" +#define HAVE_TIMER_LL (1) +#endif + #define TIMER_INTR_SEL TIMER_INTR_LEVEL #define TIMER_DIVIDER 8 @@ -127,6 +132,18 @@ STATIC void machine_timer_isr(void *self_in) { machine_timer_obj_t *self = self_in; timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0); + #if HAVE_TIMER_LL + + #if CONFIG_IDF_TARGET_ESP32 + device->hw_timer[self->index].update = 1; + #else + device->hw_timer[self->index].update.update = 1; + #endif + timer_ll_clear_intr_status(device, self->index); + timer_ll_set_alarm_enable(device, self->index, self->repeat); + + #else + device->hw_timer[self->index].update = 1; if (self->index) { device->int_clr_timers.t1 = 1; @@ -135,6 +152,8 @@ STATIC void machine_timer_isr(void *self_in) { } device->hw_timer[self->index].config.alarm_en = self->repeat; + #endif + mp_sched_schedule(self->callback, self); mp_hal_wake_main_task_from_isr(); } diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index 44efac37562c3..335157b154902 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -24,18 +24,15 @@ * THE SOFTWARE. */ +#include "py/runtime.h" +#include "py/mphal.h" +#include "modmachine.h" -#include - -#include "esp_log.h" +#if CONFIG_IDF_TARGET_ESP32 #include "driver/gpio.h" #include "driver/touch_pad.h" -#include "py/runtime.h" -#include "py/mphal.h" -#include "modmachine.h" - typedef struct _mtp_obj_t { mp_obj_base_t base; gpio_num_t gpio_id; @@ -120,3 +117,5 @@ const mp_obj_type_t machine_touchpad_type = { .make_new = mtp_make_new, .locals_dict = (mp_obj_t)&mtp_locals_dict, }; + +#endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 7fce83f2c4c2b..e256b9be439c0 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -307,10 +307,12 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->rx = 9; self->tx = 10; break; + #if SOC_UART_NUM > 2 case UART_NUM_2: self->rx = 16; self->tx = 17; break; + #endif } // Remove any existing configuration diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 7413798d0c302..0c2f56699f4c9 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -37,7 +37,12 @@ #include "esp_task.h" #include "soc/cpu.h" #include "esp_log.h" + +#if CONFIG_IDF_TARGET_ESP32 #include "esp32/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/spiram.h" +#endif #include "py/stackctrl.h" #include "py/nlr.h" diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index aacdd40d3113d..63a221e4d6197 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -84,7 +84,6 @@ set(IDF_COMPONENTS bootloader_support bt driver - esp32 esp_common esp_eth esp_event @@ -123,6 +122,12 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 3) list(APPEND IDF_COMPONENTS hal) endif() +if(IDF_TARGET STREQUAL "esp32") + list(APPEND IDF_COMPONENTS esp32) +elseif(IDF_TARGET STREQUAL "esp32s2") + list(APPEND IDF_COMPONENTS esp32s2) +endif() + # Register the main IDF component. idf_component_register( SRCS diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 53ca7fdc60fcc..3ed5343380015 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -132,6 +132,8 @@ STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_m } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1); +#if CONFIG_IDF_TARGET_ESP32 + STATIC mp_obj_t esp32_raw_temperature(void) { SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S); SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S); @@ -154,6 +156,8 @@ STATIC mp_obj_t esp32_hall_sensor(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_hall_sensor_obj, esp32_hall_sensor); +#endif + STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { mp_int_t cap = mp_obj_get_int(cap_in); multi_heap_info_t info; @@ -182,14 +186,18 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) }, { MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) }, { MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, { MP_ROM_QSTR(MP_QSTR_hall_sensor), MP_ROM_PTR(&esp32_hall_sensor_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, { MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) }, { MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) }, { MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE }, { MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE }, diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 3925bcb64e194..2eb5cd2feed51 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -32,12 +32,18 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp32/rom/rtc.h" -#include "esp32/clk.h" #include "esp_sleep.h" #include "esp_pm.h" #include "driver/touch_pad.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/rtc.h" +#include "esp32/clk.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/rtc.h" +#include "esp32s2/clk.h" +#endif + #include "py/obj.h" #include "py/runtime.h" #include "lib/utils/pyexec.h" @@ -71,7 +77,11 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (freq != 20 && freq != 40 && freq != 80 && freq != 160 && freq != 240) { mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz")); } + #if CONFIG_IDF_TARGET_ESP32 esp_pm_config_esp32_t pm; + #elif CONFIG_IDF_TARGET_ESP32S2 + esp_pm_config_esp32s2_t pm; + #endif pm.max_freq_mhz = freq; pm.min_freq_mhz = freq; pm.light_sleep_enable = false; @@ -260,7 +270,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index c9c4b6268be2f..8788963cb3209 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -153,7 +153,9 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) +#ifndef MICROPY_HW_ENABLE_SDCARD #define MICROPY_HW_ENABLE_SDCARD (1) +#endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly #define MICROPY_PY_USSL (1) diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index c837c8dcfe6a4..bd3eea9f6bed3 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -49,7 +49,11 @@ STATIC void IRAM_ATTR uart_irq_handler(void *arg) { uart->int_clr.frm_err = 1; uart->int_clr.rxfifo_tout = 1; while (uart->status.rxfifo_cnt) { + #if CONFIG_IDF_TARGET_ESP32 uint8_t c = uart->fifo.rw_byte; + #elif CONFIG_IDF_TARGET_ESP32S2 + uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0 + #endif if (c == mp_interrupt_char) { mp_keyboard_interrupt(); } else { From c81d048bb3ad35d62c3c7afe9b770a32f8b49c8b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 19 Feb 2021 12:08:11 +1100 Subject: [PATCH 0742/3533] esp32: Add support for USB with CDC ACM. The REPL will be available on the USB serial port. Signed-off-by: Damien George --- ports/esp32/boards/sdkconfig.usb | 4 ++ ports/esp32/main.c | 5 ++ ports/esp32/main/CMakeLists.txt | 6 +++ ports/esp32/mphalport.c | 7 ++- ports/esp32/usb.c | 92 ++++++++++++++++++++++++++++++++ ports/esp32/usb.h | 32 +++++++++++ 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 ports/esp32/boards/sdkconfig.usb create mode 100644 ports/esp32/usb.c create mode 100644 ports/esp32/usb.h diff --git a/ports/esp32/boards/sdkconfig.usb b/ports/esp32/boards/sdkconfig.usb new file mode 100644 index 0000000000000..657edbc58059b --- /dev/null +++ b/ports/esp32/boards/sdkconfig.usb @@ -0,0 +1,4 @@ +CONFIG_USB_ENABLED=y +CONFIG_USB_CDC_ENABLED=y +CONFIG_USB_CDC_RX_BUFSIZE=256 +CONFIG_USB_CDC_TX_BUFSIZE=256 diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 0c2f56699f4c9..7ca3d84140cf1 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -55,6 +55,7 @@ #include "lib/mp-readline/readline.h" #include "lib/utils/pyexec.h" #include "uart.h" +#include "usb.h" #include "modmachine.h" #include "modnetwork.h" #include "mpthreadport.h" @@ -77,7 +78,11 @@ void mp_task(void *pvParameter) { #if MICROPY_PY_THREAD mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif + #if CONFIG_USB_ENABLED + usb_init(); + #else uart_init(); + #endif machine_init(); // TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0. diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 63a221e4d6197..656045da90a21 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -37,6 +37,7 @@ set(MICROPY_SOURCE_DRIVERS set(MICROPY_SOURCE_PORT ${PROJECT_DIR}/main.c ${PROJECT_DIR}/uart.c + ${PROJECT_DIR}/usb.c ${PROJECT_DIR}/gccollect.c ${PROJECT_DIR}/mphalport.c ${PROJECT_DIR}/fatfs_port.c @@ -126,6 +127,7 @@ if(IDF_TARGET STREQUAL "esp32") list(APPEND IDF_COMPONENTS esp32) elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND IDF_COMPONENTS esp32s2) + list(APPEND IDF_COMPONENTS tinyusb) endif() # Register the main IDF component. @@ -185,6 +187,10 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 2) # so add them explicitly. list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/${IDF_TARGET}/include) list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/include) + if(IDF_VERSION_MINOR GREATER_EQUAL 3) + list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/additions/include) + list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/tinyusb/src) + endif() endif() # Include the main MicroPython cmake rules. diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 65fa88e4d0780..cf668216df9e3 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -43,10 +43,11 @@ #include "lib/timeutils/timeutils.h" #include "lib/utils/pyexec.h" #include "mphalport.h" +#include "usb.h" TaskHandle_t mp_main_task_handle; -STATIC uint8_t stdin_ringbuf_array[256]; +STATIC uint8_t stdin_ringbuf_array[260]; ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0}; // Check the ESP-IDF error code and raise an OSError if it's not ESP_OK. @@ -110,9 +111,13 @@ void mp_hal_stdout_tx_strn(const char *str, uint32_t len) { if (release_gil) { MP_THREAD_GIL_EXIT(); } + #if CONFIG_USB_ENABLED + usb_tx_strn(str, len); + #else for (uint32_t i = 0; i < len; ++i) { uart_tx_one_char(str[i]); } + #endif if (release_gil) { MP_THREAD_GIL_ENTER(); } diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c new file mode 100644 index 0000000000000..aa76321a7e918 --- /dev/null +++ b/ports/esp32/usb.c @@ -0,0 +1,92 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "usb.h" + +#if CONFIG_USB_ENABLED + +#include "tinyusb.h" +#include "tusb_cdc_acm.h" + +#define CDC_ITF TINYUSB_CDC_ACM_0 + +static uint8_t usb_rx_buf[CONFIG_USB_CDC_RX_BUFSIZE]; + +static void usb_callback_rx(int itf, cdcacm_event_t *event) { + // TODO: what happens if more chars come in during this function, are they lost? + for (;;) { + size_t len = 0; + esp_err_t ret = tinyusb_cdcacm_read(itf, usb_rx_buf, sizeof(usb_rx_buf), &len); + if (ret != ESP_OK) { + break; + } + if (len == 0) { + break; + } + for (size_t i = 0; i < len; ++i) { + if (usb_rx_buf[i] == mp_interrupt_char) { + mp_keyboard_interrupt(); + } else { + ringbuf_put(&stdin_ringbuf, usb_rx_buf[i]); + } + } + } +} + +void usb_init(void) { + // Initialise the USB with defaults. + tinyusb_config_t tusb_cfg = {0}; + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + // Initialise the USB serial interface. + tinyusb_config_cdcacm_t amc_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = CDC_ITF, + .rx_unread_buf_sz = 256, + .callback_rx = &usb_callback_rx, + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL + }; + ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg)); +} + +void usb_tx_strn(const char *str, size_t len) { + while (len) { + size_t l = len; + if (l > CONFIG_USB_CDC_TX_BUFSIZE) { + l = CONFIG_USB_CDC_TX_BUFSIZE; + } + tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, l); + tinyusb_cdcacm_write_flush(CDC_ITF, pdMS_TO_TICKS(1000)); + str += l; + len -= l; + } +} + +#endif // CONFIG_USB_ENABLED diff --git a/ports/esp32/usb.h b/ports/esp32/usb.h new file mode 100644 index 0000000000000..a1037803337f8 --- /dev/null +++ b/ports/esp32/usb.h @@ -0,0 +1,32 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_ESP32_USB_H +#define MICROPY_INCLUDED_ESP32_USB_H + +void usb_init(void); +void usb_tx_strn(const char *str, size_t len); + +#endif // MICROPY_INCLUDED_ESP32_USB_H From d97b8daf1a8d3bcb7f205bbffd4f2e122f973f9d Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Apr 2021 11:13:08 +1000 Subject: [PATCH 0743/3533] esp32/boards: Add GENERIC_S2 board definition. Signed-off-by: Damien George --- ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake | 8 ++++++++ ports/esp32/boards/GENERIC_S2/mpconfigboard.h | 5 +++++ 2 files changed, 13 insertions(+) create mode 100644 ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake create mode 100644 ports/esp32/boards/GENERIC_S2/mpconfigboard.h diff --git a/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake new file mode 100644 index 0000000000000..23d52e8b941a0 --- /dev/null +++ b/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake @@ -0,0 +1,8 @@ +set(IDF_TARGET esp32s2) + +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.usb +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/GENERIC_S2/mpconfigboard.h b/ports/esp32/boards/GENERIC_S2/mpconfigboard.h new file mode 100644 index 0000000000000..01ac2ce58ec2d --- /dev/null +++ b/ports/esp32/boards/GENERIC_S2/mpconfigboard.h @@ -0,0 +1,5 @@ +#define MICROPY_HW_BOARD_NAME "ESP32S2 module" +#define MICROPY_HW_MCU_NAME "ESP32S2" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_HW_ENABLE_SDCARD (0) From a9bbf7083ef6b79cf80bdbf34984d847a6c4aae9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Apr 2021 12:33:17 +1000 Subject: [PATCH 0744/3533] tools/ci.sh: Build esp32 using IDF v4.0.2 and v4.3. To test different IDF's, and also test building the GENERIC_S2 board. Signed-off-by: Damien George --- .github/workflows/ports_esp32.yml | 13 +++++++++++-- tools/ci.sh | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ports_esp32.yml b/.github/workflows/ports_esp32.yml index 0ad3f87a25fcb..09817ee126546 100644 --- a/.github/workflows/ports_esp32.yml +++ b/.github/workflows/ports_esp32.yml @@ -13,11 +13,20 @@ on: - 'ports/esp32/**' jobs: - build: + build_idf402: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_esp32_setup + run: source tools/ci.sh && ci_esp32_idf402_setup + - name: Build + run: source tools/ci.sh && ci_esp32_build + + build_idf43: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_esp32_idf43_setup - name: Build run: source tools/ci.sh && ci_esp32_build diff --git a/tools/ci.sh b/tools/ci.sh index 3ce96a96a9084..c018b55002281 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -82,19 +82,27 @@ function ci_cc3200_build { ######################################################################################## # ports/esp32 -function ci_esp32_setup { +function ci_esp32_setup_helper { git clone https://github.com/espressif/esp-idf.git - git -C esp-idf checkout v4.0.2 + git -C esp-idf checkout $1 git -C esp-idf submodule update --init \ components/bt/controller/lib \ components/bt/host/nimble/nimble \ - components/esp_wifi/lib_esp32 \ + components/esp_wifi \ components/esptool_py/esptool \ components/lwip/lwip \ components/mbedtls/mbedtls ./esp-idf/install.sh } +function ci_esp32_idf402_setup { + ci_esp32_setup_helper v4.0.2 +} + +function ci_esp32_idf43_setup { + ci_esp32_setup_helper v4.3-beta2 +} + function ci_esp32_build { source esp-idf/export.sh make ${MAKEOPTS} -C mpy-cross @@ -102,6 +110,9 @@ function ci_esp32_build { make ${MAKEOPTS} -C ports/esp32 make ${MAKEOPTS} -C ports/esp32 clean make ${MAKEOPTS} -C ports/esp32 USER_C_MODULES=../../../examples/usercmodule/micropython.cmake + if [ -d $IDF_PATH/components/esp32s2 ]; then + make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S2 + fi } ######################################################################################## From e5d2ddde25351f1ede7d0d1b00be632301962fb0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 15 Apr 2021 13:02:34 +1000 Subject: [PATCH 0745/3533] esp32/machine_pin: Use rtc_gpio_deinit instead of gpio_reset_pin. Commit 8a917ad2529ea3df5f47e2be5b4edf362d2d03f6 added the gpio_reset_pin() call to make sure that pins that were used as ADC inputs could subsequently be used as digital IO. But calling gpio_reset_pin() will enable the pull-up on the pin and so pull it high for a brief period. Instead use rtc_gpio_deinit() which will just reconfigure the pin as a digital IO and do nothing else. Fixes issue #7079 (see also #5771). Signed-off-by: Damien George --- ports/esp32/machine_pin.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index 8290c77a48263..8dbdd198490b2 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -30,6 +30,7 @@ #include #include "driver/gpio.h" +#include "driver/rtc_io.h" #include "py/runtime.h" #include "py/mphal.h" @@ -157,9 +158,11 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - // reset the pin first if this is a mode-setting init (grab it back from ADC) + // reset the pin to digital if this is a mode-setting init (grab it back from ADC) if (args[ARG_mode].u_obj != mp_const_none) { - gpio_reset_pin(self->id); + if (rtc_gpio_is_valid_gpio(self->id)) { + rtc_gpio_deinit(self->id); + } } // configure the pin for gpio From f842a40df4d26bd74b92a3903096fc6b1709222d Mon Sep 17 00:00:00 2001 From: Tim Radvan Date: Fri, 2 Apr 2021 19:35:18 +0100 Subject: [PATCH 0746/3533] rp2/rp2_pio: Add fifo_join support for PIO. The PIO state machines on the RP2040 have 4 word deep TX and RX FIFOs. If you only need one direction, you can "merge" them into either a single 8 word deep TX or RX FIFO. We simply add constants to the PIO object, and set the appropriate bits in `shiftctrl`. Resolves #6854. Signed-off-by: Tim Radvan --- examples/rp2/pio_uart_rx.py | 1 + ports/rp2/modules/rp2.py | 6 ++++-- ports/rp2/rp2_pio.c | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/rp2/pio_uart_rx.py b/examples/rp2/pio_uart_rx.py index 41dfde3dad433..080b6bd6392bc 100644 --- a/examples/rp2/pio_uart_rx.py +++ b/examples/rp2/pio_uart_rx.py @@ -22,6 +22,7 @@ autopush=True, push_thresh=8, in_shiftdir=rp2.PIO.SHIFT_RIGHT, + fifo_join=PIO.JOIN_RX, ) def uart_rx_mini(): # fmt: off diff --git a/ports/rp2/modules/rp2.py b/ports/rp2/modules/rp2.py index 17e35c73b7ce5..c7e4d1fdd943d 100644 --- a/ports/rp2/modules/rp2.py +++ b/ports/rp2/modules/rp2.py @@ -31,7 +31,8 @@ def __init__( autopush=False, autopull=False, push_thresh=32, - pull_thresh=32 + pull_thresh=32, + fifo_join=0 ): # uarray is a built-in module so importing it here won't require # scanning the filesystem. @@ -40,7 +41,8 @@ def __init__( self.labels = {} execctrl = 0 shiftctrl = ( - (pull_thresh & 0x1F) << 25 + fifo_join << 30 + | (pull_thresh & 0x1F) << 25 | (push_thresh & 0x1F) << 20 | out_shiftdir << 19 | in_shiftdir << 18 diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 5f93f10c26794..44928c0a885a9 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -360,6 +360,10 @@ STATIC const mp_rom_map_elem_t rp2_pio_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SHIFT_LEFT), MP_ROM_INT(0) }, { MP_ROM_QSTR(MP_QSTR_SHIFT_RIGHT), MP_ROM_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_JOIN_NONE), MP_ROM_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_JOIN_TX), MP_ROM_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_JOIN_RX), MP_ROM_INT(2) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_SM0), MP_ROM_INT(0x100) }, { MP_ROM_QSTR(MP_QSTR_IRQ_SM1), MP_ROM_INT(0x200) }, { MP_ROM_QSTR(MP_QSTR_IRQ_SM2), MP_ROM_INT(0x400) }, From 7d911d20698b3b11269f34ecec97d3b4bfabac94 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 18 Apr 2021 23:20:26 +1000 Subject: [PATCH 0747/3533] tests/net_inet: Add 'Strict-Transport-Security' to exp file. Because micropython.org now adds this to the headers. Signed-off-by: Damien George --- tests/net_inet/uasyncio_tcp_read_headers.py.exp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/net_inet/uasyncio_tcp_read_headers.py.exp b/tests/net_inet/uasyncio_tcp_read_headers.py.exp index c200238dc6b83..932d2674cbd1e 100644 --- a/tests/net_inet/uasyncio_tcp_read_headers.py.exp +++ b/tests/net_inet/uasyncio_tcp_read_headers.py.exp @@ -5,6 +5,7 @@ b'Content-Length: 54' b'Connection: close' b'Vary: Accept-Encoding' b'ETag: "54306c85-36"' +b'Strict-Transport-Security: max-age=15768000' b'Accept-Ranges: bytes' close done From 321d1897c34f16243edf2c94913d7cf877a013d1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 19 Apr 2021 00:11:51 +1000 Subject: [PATCH 0748/3533] all: Bump version to 1.15. Signed-off-by: Damien George --- docs/conf.py | 2 +- py/mpconfig.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index dd6cc31fb8736..53cbf9baeddc0 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -74,7 +74,7 @@ # # We don't follow "The short X.Y version" vs "The full version, including alpha/beta/rc tags" # breakdown, so use the same version identifier for both to avoid confusion. -version = release = '1.14' +version = release = '1.15' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/py/mpconfig.h b/py/mpconfig.h index b26fa2137a0b4..3dd83a34abe72 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -28,7 +28,7 @@ // Current version of MicroPython #define MICROPY_VERSION_MAJOR 1 -#define MICROPY_VERSION_MINOR 14 +#define MICROPY_VERSION_MINOR 15 #define MICROPY_VERSION_MICRO 0 // Combined version as a 32-bit number for convenience From b74dc546fc2d7d04853db3acc4dc6ed92cc77f67 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Apr 2021 21:39:53 +1000 Subject: [PATCH 0749/3533] tools/metrics.py: Add rp2 port to table of ports that can be built. Signed-off-by: Damien George --- tools/metrics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/metrics.py b/tools/metrics.py index 98291e25a9f06..c857d0733eef8 100755 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -67,6 +67,7 @@ def __init__(self, name, dir, output, make_flags=None): "8": PortData("esp8266", "esp8266", "build-GENERIC/firmware.elf"), "3": PortData("esp32", "esp32", "build-GENERIC/micropython.elf"), "r": PortData("nrf", "nrf", "build-pca10040/firmware.elf"), + "p": PortData("rp2", "rp2", "build-PICO/firmware.elf"), "d": PortData("samd", "samd", "build-ADAFRUIT_ITSYBITSY_M4_EXPRESS/firmware.elf"), } From 6e0f9b9262a2948391704d53eafec8d2bd2e5ad2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Apr 2021 16:12:32 +1000 Subject: [PATCH 0750/3533] stm32/boards/pllvalues.py: Support wider range of PLL values for F413. Signed-off-by: Damien George --- ports/stm32/boards/pllvalues.py | 38 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/ports/stm32/boards/pllvalues.py b/ports/stm32/boards/pllvalues.py index 619146cd47fac..e0fff40dd3fea 100644 --- a/ports/stm32/boards/pllvalues.py +++ b/ports/stm32/boards/pllvalues.py @@ -31,15 +31,26 @@ def __init__( range_vco_out=range(192, 432 + 1), ) -mcu_h7 = MCU( - range_sysclk=range(2, 400 + 1, 2), # above 400MHz currently unsupported - range_m=range(1, 63 + 1), - range_n=range(4, 512 + 1), - range_p=range(2, 128 + 1, 2), - range_q=range(1, 128 + 1), - range_vco_in=range(1, 16 + 1), - range_vco_out=range(150, 960 + 1), # 150-420=medium, 192-960=wide -) +mcu_table = { + "stm32f413": MCU( + range_sysclk=range(2, 100 + 1, 2), + range_m=range(2, 63 + 1), + range_n=range(50, 432 + 1), + range_p=range(2, 8 + 1, 2), + range_q=range(2, 15 + 1), + range_vco_in=range(1, 2 + 1), + range_vco_out=range(100, 432 + 1), + ), + "stm32h7": MCU( + range_sysclk=range(2, 400 + 1, 2), # above 400MHz currently unsupported + range_m=range(1, 63 + 1), + range_n=range(4, 512 + 1), + range_p=range(2, 128 + 1, 2), + range_q=range(1, 128 + 1), + range_vco_in=range(1, 16 + 1), + range_vco_out=range(150, 960 + 1), # 150-420=medium, 192-960=wide + ), +} def close_int(x): @@ -271,10 +282,11 @@ def main(): hse = int(argv[0]) # Select MCU parameters - if mcu_series.startswith("stm32h7"): - mcu = mcu_h7 - else: - mcu = mcu_default + mcu = mcu_default + for m in mcu_table: + if mcu_series.startswith(m): + mcu = mcu_table[m] + break # Relax constraint on PLLQ being 48MHz on MCUs which have separate PLLs for 48MHz relax_pll48 = mcu_series.startswith(("stm32f413", "stm32f7", "stm32h7")) From 00d6a79b3d5dc80d840dc1d51166e7d95856b3d6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Apr 2021 14:06:14 +1000 Subject: [PATCH 0751/3533] stm32/machine_timer: Improve usability of Timer constructor and init. Improvements are: - Default period is 1000ms with callback disabled. - if period is not specified then it's not updated (previously, if period was not specified then it was set to -1 and running the timer callback as fast as possible, making the REPL unresponsive). - Use uint64_t to compute delta_ms, and raise a ValueError if the period is too large. - If callback is not specified then it's not updated. - Specifying None for the callback will disable the timer. Signed-off-by: Damien George --- ports/stm32/machine_timer.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index e9b16ab72f4b9..c37ac87672ac2 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -42,7 +42,7 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, }; static const mp_arg_t allowed_args[] = { { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} }, - { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, @@ -53,24 +53,35 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); self->mode = args[ARG_mode].u_int; + + uint64_t delta_ms = self->delta_ms; if (args[ARG_freq].u_obj != mp_const_none) { // Frequency specified in Hz #if MICROPY_PY_BUILTINS_FLOAT - self->delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); + delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); #else - self->delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj); + delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj); #endif - } else { + } else if (args[ARG_period].u_int != 0xffffffff) { // Period specified - self->delta_ms = args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int; + delta_ms = (uint64_t)args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int; } - if (self->delta_ms < 1) { - self->delta_ms = 1; + + if (delta_ms < 1) { + delta_ms = 1; + } else if (delta_ms >= 0x40000000) { + mp_raise_ValueError(MP_ERROR_TEXT("period too large")); } + self->delta_ms = (uint32_t)delta_ms; self->expiry_ms = mp_hal_ticks_ms() + self->delta_ms; - self->callback = args[ARG_callback].u_obj; - soft_timer_insert(self); + if (args[ARG_callback].u_obj != MP_OBJ_NULL) { + self->callback = args[ARG_callback].u_obj; + } + + if (self->callback != mp_const_none) { + soft_timer_insert(self); + } return mp_const_none; } @@ -78,6 +89,8 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); self->pairheap.base.type = &machine_timer_type; + self->delta_ms = 1000; + self->callback = mp_const_none; // Get timer id (only soft timer (-1) supported at the moment) mp_int_t id = -1; From 5669a6095444e079af5e38b2b04ca5ff2e7c11f9 Mon Sep 17 00:00:00 2001 From: David Michieli Date: Thu, 22 Apr 2021 13:43:50 +1000 Subject: [PATCH 0752/3533] stm32/mboot: Allow unpacking dfu without secret key. - unpack-dfu command no longer requies a secret key to be present - pack-dfu command raises an exception if no secret key is found --- ports/stm32/mboot/mboot_pack_dfu.py | 13 ++++++++++--- tools/ci.sh | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ports/stm32/mboot/mboot_pack_dfu.py b/ports/stm32/mboot/mboot_pack_dfu.py index 540057e06ee8c..683cdd0a71753 100644 --- a/ports/stm32/mboot/mboot_pack_dfu.py +++ b/ports/stm32/mboot/mboot_pack_dfu.py @@ -86,9 +86,14 @@ def save(self): def load(self): with open(self.filename) as f: - self.sign_sk = self._load_data("mboot_pack_sign_secret_key", f.readline()) - self.sign_pk = self._load_data("mboot_pack_sign_public_key", f.readline()) - self.secretbox = self._load_data("mboot_pack_secretbox_key", f.readline()) + for line in f: + for key, attr in ( + ("mboot_pack_sign_secret_key", "sign_sk"), + ("mboot_pack_sign_public_key", "sign_pk"), + ("mboot_pack_secretbox_key", "secretbox"), + ): + if key in line: + setattr(self, attr, self._load_data(key, line)) def dfu_read(filename): @@ -135,6 +140,8 @@ def encrypt(keys, data): def sign(keys, data): + if not hasattr(keys, "sign_sk"): + raise Exception("packing a dfu requires a secret key") return pyhy.hydro_sign_create(data, MBOOT_PACK_HYDRO_CONTEXT, keys.sign_sk) diff --git a/tools/ci.sh b/tools/ci.sh index c018b55002281..33cf364ddd22d 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -243,6 +243,10 @@ function ci_stm32_nucleo_build { BUILD_WB55=ports/stm32/build-NUCLEO_WB55 python3 ports/stm32/mboot/mboot_pack_dfu.py -k $BOARD_WB55/mboot_keys.h unpack-dfu $BUILD_WB55/firmware.pack.dfu $BUILD_WB55/firmware.unpack.dfu diff $BUILD_WB55/firmware.unpack.dfu $BUILD_WB55/firmware.dfu + # Test unpack-dfu command works without a secret key + tail -n +2 $BOARD_WB55/mboot_keys.h > $BOARD_WB55/mboot_keys_no_sk.h + python3 ports/stm32/mboot/mboot_pack_dfu.py -k $BOARD_WB55/mboot_keys_no_sk.h unpack-dfu $BUILD_WB55/firmware.pack.dfu $BUILD_WB55/firmware.unpack_no_sk.dfu + diff $BUILD_WB55/firmware.unpack.dfu $BUILD_WB55/firmware.unpack_no_sk.dfu } ######################################################################################## From 3c4bfd1dec28ffbefc6379dedeaa24feaa2ef373 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Apr 2021 17:11:13 +1000 Subject: [PATCH 0753/3533] py/objexcept: Support errno attribute on OSError exceptions. This commit adds the errno attribute to exceptions, so code can retrieve errno codes from an OSError using exc.errno. The implementation here simply lets `errno` (and the existing `value`) attributes work on any exception instance (they both alias args[0]). This is for efficiency and to keep code size down. The pros and cons of this are: Pros: - more compatible with CPython, less difference to document and learn - OSError().errno will correctly return None, whereas the current way of doing it via OSError().args[0] will raise an IndexError - it reduces code size on most bare-metal ports (because they already have the errno qstr) - for Python code that uses exc.errno the generated bytecode is 2 bytes smaller and more efficient to execute (compared with exc.args[0]); so bytecode loaded to RAM saves 2 bytes RAM for each use of this attribute, and bytecode that is frozen saves 2 bytes flash/ROM for each use - it's easier/shorter to type, and saves 2 bytes of space in .py files that use it (for each use) Cons: - increases code size by 4-8 bytes on minimal ports that don't already have the `errno` qstr - all exceptions now have .errno and .value attributes (a cpydiff test is added to address this) See also #2407. Signed-off-by: Damien George --- docs/library/builtins.rst | 4 ---- docs/library/uerrno.rst | 4 ++-- py/objexcept.c | 4 +++- tests/basics/exception1.py | 5 +++++ tests/basics/subclass_native3.py | 20 ++++++++++++++++++++ tests/cpydiff/types_exception_attrs.py | 9 +++++++++ 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 tests/cpydiff/types_exception_attrs.py diff --git a/docs/library/builtins.rst b/docs/library/builtins.rst index 365248dc764db..3029cd9e3bd34 100644 --- a/docs/library/builtins.rst +++ b/docs/library/builtins.rst @@ -176,10 +176,6 @@ Exceptions .. exception:: OSError - |see_cpython| `python:OSError`. MicroPython doesn't implement ``errno`` - attribute, instead use the standard way to access exception arguments: - ``exc.args[0]``. - .. exception:: RuntimeError .. exception:: StopIteration diff --git a/docs/library/uerrno.rst b/docs/library/uerrno.rst index def01362f1387..1d60c80e11c27 100644 --- a/docs/library/uerrno.rst +++ b/docs/library/uerrno.rst @@ -16,13 +16,13 @@ Constants Error codes, based on ANSI C/POSIX standard. All error codes start with "E". As mentioned above, inventory of the codes depends on - :term:`MicroPython port`. Errors are usually accessible as ``exc.args[0]`` + :term:`MicroPython port`. Errors are usually accessible as ``exc.errno`` where ``exc`` is an instance of `OSError`. Usage example:: try: uos.mkdir("my_dir") except OSError as exc: - if exc.args[0] == uerrno.EEXIST: + if exc.errno == uerrno.EEXIST: print("Directory already exists") .. data:: errorcode diff --git a/py/objexcept.c b/py/objexcept.c index 885032c3e3eed..f6bffec38353d 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -261,7 +261,9 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (attr == MP_QSTR_args) { decompress_error_text_maybe(self); dest[0] = MP_OBJ_FROM_PTR(self->args); - } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) { + } else if (attr == MP_QSTR_value || attr == MP_QSTR_errno) { + // These are aliases for args[0]: .value for StopIteration and .errno for OSError. + // For efficiency let these attributes apply to all exception instances. dest[0] = mp_obj_exception_get_value(self_in); } } diff --git a/tests/basics/exception1.py b/tests/basics/exception1.py index d83764cb939cc..a0ca8a74e225e 100644 --- a/tests/basics/exception1.py +++ b/tests/basics/exception1.py @@ -1,3 +1,5 @@ +# test basic properties of exceptions + print(repr(IndexError())) print(str(IndexError())) @@ -12,3 +14,6 @@ print(s.value) s = StopIteration(1, 2, 3) print(s.value) + +print(OSError().errno) +print(OSError(1, "msg").errno) diff --git a/tests/basics/subclass_native3.py b/tests/basics/subclass_native3.py index 6745b77bb2836..ac5aabfed7b25 100644 --- a/tests/basics/subclass_native3.py +++ b/tests/basics/subclass_native3.py @@ -1,6 +1,10 @@ +# test subclassing a native exception + + class MyExc(Exception): pass + e = MyExc(100, "Some error") print(e) print(repr(e)) @@ -20,3 +24,19 @@ class MyExc(Exception): raise MyExc("Some error2") except: print("Caught user exception") + + +class MyStopIteration(StopIteration): + pass + + +print(MyStopIteration().value) +print(MyStopIteration(1).value) + + +class MyOSError(OSError): + pass + + +print(MyOSError().errno) +print(MyOSError(1, "msg").errno) diff --git a/tests/cpydiff/types_exception_attrs.py b/tests/cpydiff/types_exception_attrs.py new file mode 100644 index 0000000000000..ad72b62a61a5d --- /dev/null +++ b/tests/cpydiff/types_exception_attrs.py @@ -0,0 +1,9 @@ +""" +categories: Types,Exception +description: All exceptions have readable ``value`` and ``errno`` attributes, not just ``StopIteration`` and ``OSError``. +cause: MicroPython is optimised to reduce code size. +workaround: Only use ``value`` on ``StopIteration`` exceptions, and ``errno`` on ``OSError`` exceptions. Do not use or rely on these attributes on other exceptions. +""" +e = Exception(1) +print(e.value) +print(e.errno) From ac1d01d43ee02d294f5fe762ae608fbb3f72babb Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Apr 2021 17:11:59 +1000 Subject: [PATCH 0754/3533] tools/upip.py: Use .errno instead of .args[0] for OSError exceptions. Signed-off-by: Damien George --- tools/upip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/upip.py b/tools/upip.py index aa8aecedfc23f..728b843c94017 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -60,7 +60,7 @@ def _makedirs(name, mode=0o777): os.mkdir(s) ret = True except OSError as e: - if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR: + if e.errno != errno.EEXIST and e.errno != errno.EISDIR: raise e ret = False return ret From 342d55529d6f3312fc158d7af005f56d5e30adef Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Apr 2021 17:12:08 +1000 Subject: [PATCH 0755/3533] extmod/uasyncio: Use .errno instead of .args[0] for OSError exceptions. Signed-off-by: Damien George --- extmod/uasyncio/stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/uasyncio/stream.py b/extmod/uasyncio/stream.py index b6d787e4f0336..395ff1f6afdf9 100644 --- a/extmod/uasyncio/stream.py +++ b/extmod/uasyncio/stream.py @@ -82,7 +82,7 @@ async def open_connection(host, port): try: s.connect(ai[-1]) except OSError as er: - if er.args[0] != EINPROGRESS: + if er.errno != EINPROGRESS: raise er yield core._io_queue.queue_write(s) return ss, ss From 3123f6918ba18b0a3f7a89500b450f4cb15e1aee Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Apr 2021 19:32:21 +1000 Subject: [PATCH 0756/3533] tests: Use .errno instead of .args[0] for OSError exceptions. Signed-off-by: Damien George --- tests/extmod/btree1.py | 2 +- tests/extmod/btree_error.py | 2 +- tests/extmod/uselect_poll_basic.py | 2 +- tests/extmod/usocket_tcp_basic.py | 2 +- tests/extmod/usocket_udp_nonblock.py | 2 +- tests/extmod/vfs_fat_fileio1.py | 10 +++++----- tests/extmod/vfs_fat_fileio2.py | 12 ++++++------ tests/extmod/vfs_fat_more.py | 2 +- tests/extmod/vfs_fat_ramdisk.py | 6 +++--- tests/extmod/websocket_basic.py | 2 +- tests/multi_net/tcp_accept_recv.py | 2 +- tests/multi_net/tcp_client_rst.py | 2 +- tests/multi_net/uasyncio_tcp_client_rst.py | 2 +- tests/net_hosted/accept_nonblock.py | 2 +- tests/net_hosted/accept_timeout.py | 2 +- tests/net_hosted/connect_nonblock.py | 2 +- tests/net_hosted/connect_nonblock_xfer.py | 10 +++++----- tests/net_inet/ssl_errors.py | 2 +- tests/net_inet/test_tls_nonblock.py | 6 +++--- 19 files changed, 36 insertions(+), 36 deletions(-) diff --git a/tests/extmod/btree1.py b/tests/extmod/btree1.py index 4890d92b4202c..1f4853eaa365e 100644 --- a/tests/extmod/btree1.py +++ b/tests/extmod/btree1.py @@ -65,7 +65,7 @@ try: db.seq(b"foo1") except OSError as e: - print(e.args[0] == uerrno.EINVAL) + print(e.errno == uerrno.EINVAL) print(list(db.keys())) print(list(db.values())) diff --git a/tests/extmod/btree_error.py b/tests/extmod/btree_error.py index 00e07ec8c7ae6..b64769e8840bf 100644 --- a/tests/extmod/btree_error.py +++ b/tests/extmod/btree_error.py @@ -27,7 +27,7 @@ def ioctl(self, cmd, arg): try: db = btree.open(Device(), pagesize=511) except OSError as er: - print("OSError", er.args[0] == uerrno.EINVAL) + print("OSError", er.errno == uerrno.EINVAL) # Valid pagesize, device returns error on read; errno comes from Device.readinto try: diff --git a/tests/extmod/uselect_poll_basic.py b/tests/extmod/uselect_poll_basic.py index 07328365b31da..97fbd6fd158b8 100644 --- a/tests/extmod/uselect_poll_basic.py +++ b/tests/extmod/uselect_poll_basic.py @@ -33,7 +33,7 @@ try: poller.modify(s, select.POLLIN) except OSError as e: - assert e.args[0] == errno.ENOENT + assert e.errno == errno.ENOENT # poll after closing the socket, should return POLLNVAL poller.register(s) diff --git a/tests/extmod/usocket_tcp_basic.py b/tests/extmod/usocket_tcp_basic.py index 368dfe3c98f4a..c2fe8cd14cccd 100644 --- a/tests/extmod/usocket_tcp_basic.py +++ b/tests/extmod/usocket_tcp_basic.py @@ -14,4 +14,4 @@ try: s.recv(1) except OSError as er: - print("ENOTCONN:", er.args[0] == errno.ENOTCONN) + print("ENOTCONN:", er.errno == errno.ENOTCONN) diff --git a/tests/extmod/usocket_udp_nonblock.py b/tests/extmod/usocket_udp_nonblock.py index 7dc0e562a3178..bc560de142e0c 100644 --- a/tests/extmod/usocket_udp_nonblock.py +++ b/tests/extmod/usocket_udp_nonblock.py @@ -17,4 +17,4 @@ try: s.recv(1) except OSError as er: - print("EAGAIN:", er.args[0] == errno.EAGAIN) + print("EAGAIN:", er.errno == errno.EAGAIN) diff --git a/tests/extmod/vfs_fat_fileio1.py b/tests/extmod/vfs_fat_fileio1.py index e42911093f402..7da08b80cf332 100644 --- a/tests/extmod/vfs_fat_fileio1.py +++ b/tests/extmod/vfs_fat_fileio1.py @@ -58,22 +58,22 @@ def ioctl(self, op, arg): try: f.write("world!") except OSError as e: - print(e.args[0] == uerrno.EINVAL) + print(e.errno == uerrno.EINVAL) try: f.read() except OSError as e: - print(e.args[0] == uerrno.EINVAL) + print(e.errno == uerrno.EINVAL) try: f.flush() except OSError as e: - print(e.args[0] == uerrno.EINVAL) + print(e.errno == uerrno.EINVAL) try: open("foo_file.txt", "x") except OSError as e: - print(e.args[0] == uerrno.EEXIST) + print(e.errno == uerrno.EEXIST) with open("foo_file.txt", "a") as f: f.write("world!") @@ -105,7 +105,7 @@ def ioctl(self, op, arg): try: vfs.rmdir("foo_file.txt") except OSError as e: - print(e.args[0] == 20) # uerrno.ENOTDIR + print(e.errno == 20) # uerrno.ENOTDIR vfs.remove("foo_file.txt") print(list(vfs.ilistdir())) diff --git a/tests/extmod/vfs_fat_fileio2.py b/tests/extmod/vfs_fat_fileio2.py index 531dd91f9490a..13978a0af693c 100644 --- a/tests/extmod/vfs_fat_fileio2.py +++ b/tests/extmod/vfs_fat_fileio2.py @@ -51,22 +51,22 @@ def ioctl(self, op, arg): try: vfs.mkdir("foo_dir") except OSError as e: - print(e.args[0] == uerrno.EEXIST) + print(e.errno == uerrno.EEXIST) try: vfs.remove("foo_dir") except OSError as e: - print(e.args[0] == uerrno.EISDIR) + print(e.errno == uerrno.EISDIR) try: vfs.remove("no_file.txt") except OSError as e: - print(e.args[0] == uerrno.ENOENT) + print(e.errno == uerrno.ENOENT) try: vfs.rename("foo_dir", "/null/file") except OSError as e: - print(e.args[0] == uerrno.ENOENT) + print(e.errno == uerrno.ENOENT) # file in dir with open("foo_dir/file-in-dir.txt", "w+t") as f: @@ -82,7 +82,7 @@ def ioctl(self, op, arg): try: vfs.rmdir("foo_dir") except OSError as e: - print(e.args[0] == uerrno.EACCES) + print(e.errno == uerrno.EACCES) # trim full path vfs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt") @@ -111,5 +111,5 @@ def ioctl(self, op, arg): f = open("large_file.txt", "wb") f.write(bytearray(bsize * free)) except OSError as e: - print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC + print("ENOSPC:", e.errno == 28) # uerrno.ENOSPC f.close() diff --git a/tests/extmod/vfs_fat_more.py b/tests/extmod/vfs_fat_more.py index 076802f6ad5eb..f9b54fda00ff8 100644 --- a/tests/extmod/vfs_fat_more.py +++ b/tests/extmod/vfs_fat_more.py @@ -90,7 +90,7 @@ def ioctl(self, op, arg): try: uos.mkdir(exist) except OSError as er: - print("mkdir OSError", er.args[0] == 17) # EEXIST + print("mkdir OSError", er.errno == 17) # EEXIST uos.chdir("/") print(uos.stat("test5.txt")[:-3]) diff --git a/tests/extmod/vfs_fat_ramdisk.py b/tests/extmod/vfs_fat_ramdisk.py index 9a68d94fedefa..4decb5557dd77 100644 --- a/tests/extmod/vfs_fat_ramdisk.py +++ b/tests/extmod/vfs_fat_ramdisk.py @@ -57,7 +57,7 @@ def ioctl(self, op, arg): try: vfs.stat("no_file.txt") except OSError as e: - print(e.args[0] == uerrno.ENOENT) + print(e.errno == uerrno.ENOENT) with vfs.open("foo_file.txt", "w") as f: f.write("hello!") @@ -80,7 +80,7 @@ def ioctl(self, op, arg): try: vfs.chdir("sub_file.txt") except OSError as e: - print(e.args[0] == uerrno.ENOENT) + print(e.errno == uerrno.ENOENT) vfs.chdir("..") print("getcwd:", vfs.getcwd()) @@ -94,4 +94,4 @@ def ioctl(self, op, arg): try: vfs.ilistdir(b"no_exist") except OSError as e: - print("ENOENT:", e.args[0] == uerrno.ENOENT) + print("ENOENT:", e.errno == uerrno.ENOENT) diff --git a/tests/extmod/websocket_basic.py b/tests/extmod/websocket_basic.py index 10396e914d652..a07542f965662 100644 --- a/tests/extmod/websocket_basic.py +++ b/tests/extmod/websocket_basic.py @@ -59,4 +59,4 @@ def ws_write(msg, sz): try: ws.ioctl(-1) except OSError as e: - print("ioctl: EINVAL:", e.args[0] == uerrno.EINVAL) + print("ioctl: EINVAL:", e.errno == uerrno.EINVAL) diff --git a/tests/multi_net/tcp_accept_recv.py b/tests/multi_net/tcp_accept_recv.py index d61108ed1c8bd..f93ede3bf40ad 100644 --- a/tests/multi_net/tcp_accept_recv.py +++ b/tests/multi_net/tcp_accept_recv.py @@ -17,7 +17,7 @@ def instance0(): try: print("recv", s.recv(10)) # should raise Errno 107 ENOTCONN except OSError as er: - print(er.args[0]) + print(er.errno) s.close() diff --git a/tests/multi_net/tcp_client_rst.py b/tests/multi_net/tcp_client_rst.py index 08f262db50810..14da8b1470f22 100644 --- a/tests/multi_net/tcp_client_rst.py +++ b/tests/multi_net/tcp_client_rst.py @@ -33,7 +33,7 @@ def instance0(): print(s2.recv(10)) print(convert_poll_list(poll.poll(1000))) except OSError as er: - print(er.args[0]) + print(er.errno) print(convert_poll_list(poll.poll(1000))) # TODO lwip raises here but apparently it shouldn't print(s2.recv(10)) diff --git a/tests/multi_net/uasyncio_tcp_client_rst.py b/tests/multi_net/uasyncio_tcp_client_rst.py index a3a05490c776b..d81f22748e444 100644 --- a/tests/multi_net/uasyncio_tcp_client_rst.py +++ b/tests/multi_net/uasyncio_tcp_client_rst.py @@ -24,7 +24,7 @@ async def handle_connection(reader, writer): writer.close() await writer.wait_closed() except OSError as er: - print("OSError", er.args[0]) + print("OSError", er.errno) ev.set() diff --git a/tests/net_hosted/accept_nonblock.py b/tests/net_hosted/accept_nonblock.py index 941965e178cfb..d17e287498cd4 100644 --- a/tests/net_hosted/accept_nonblock.py +++ b/tests/net_hosted/accept_nonblock.py @@ -12,5 +12,5 @@ try: s.accept() except OSError as er: - print(er.args[0] == 11) # 11 is EAGAIN + print(er.errno == 11) # 11 is EAGAIN s.close() diff --git a/tests/net_hosted/accept_timeout.py b/tests/net_hosted/accept_timeout.py index 5f528d557d8d3..734fe217cad34 100644 --- a/tests/net_hosted/accept_timeout.py +++ b/tests/net_hosted/accept_timeout.py @@ -18,5 +18,5 @@ try: s.accept() except OSError as er: - print(er.args[0] in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno + print(er.errno in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno s.close() diff --git a/tests/net_hosted/connect_nonblock.py b/tests/net_hosted/connect_nonblock.py index c024b65a0a9a3..4b8055c1615a9 100644 --- a/tests/net_hosted/connect_nonblock.py +++ b/tests/net_hosted/connect_nonblock.py @@ -13,7 +13,7 @@ def test(peer_addr): try: s.connect(peer_addr) except OSError as er: - print(er.args[0] == errno.EINPROGRESS) + print(er.errno == errno.EINPROGRESS) s.close() diff --git a/tests/net_hosted/connect_nonblock_xfer.py b/tests/net_hosted/connect_nonblock_xfer.py index feb648ea0aeff..1a0b2422765c4 100644 --- a/tests/net_hosted/connect_nonblock_xfer.py +++ b/tests/net_hosted/connect_nonblock_xfer.py @@ -25,9 +25,9 @@ def do_connect(peer_addr, tls, handshake): # print("Connecting to", peer_addr) s.connect(peer_addr) except OSError as er: - print("connect:", er.args[0] == errno.EINPROGRESS) - if er.args[0] != errno.EINPROGRESS: - print(" got", er.args[0]) + print("connect:", er.errno == errno.EINPROGRESS) + if er.errno != errno.EINPROGRESS: + print(" got", er.errno) # wrap with ssl/tls if desired if tls: try: @@ -67,7 +67,7 @@ def test(peer_addr, tls=False, handshake=False): except OSError as er: # dp(er) - print("send:", er.args[0] in (errno.EAGAIN, errno.EINPROGRESS)) + print("send:", er.errno in (errno.EAGAIN, errno.EINPROGRESS)) s.close() else: # fake it... print("connect:", True) @@ -103,7 +103,7 @@ def test(peer_addr, tls=False, handshake=False): print("recv:", s.recv(10)) except OSError as er: dp(er) - print("recv:", er.args[0] == errno.EAGAIN) + print("recv:", er.errno == errno.EAGAIN) s.close() else: # fake it... print("connect:", True) diff --git a/tests/net_inet/ssl_errors.py b/tests/net_inet/ssl_errors.py index fd281b1c499c3..ece1f6e253b42 100644 --- a/tests/net_inet/ssl_errors.py +++ b/tests/net_inet/ssl_errors.py @@ -17,7 +17,7 @@ def test(addr, hostname, block=True): s.connect(addr) print("connected") except OSError as e: - if e.args[0] != errno.EINPROGRESS: + if e.errno != errno.EINPROGRESS: raise print("EINPROGRESS") diff --git a/tests/net_inet/test_tls_nonblock.py b/tests/net_inet/test_tls_nonblock.py index c27ead3d50fb7..54abc6966fc05 100644 --- a/tests/net_inet/test_tls_nonblock.py +++ b/tests/net_inet/test_tls_nonblock.py @@ -16,7 +16,7 @@ def test_one(site, opts): s.connect(addr) raise OSError(-1, "connect blocks") except OSError as e: - if e.args[0] != errno.EINPROGRESS: + if e.errno != errno.EINPROGRESS: raise if sys.implementation.name != "micropython": @@ -31,7 +31,7 @@ def test_one(site, opts): else: s = ssl.wrap_socket(s, do_handshake_on_connect=False) except OSError as e: - if e.args[0] != errno.EINPROGRESS: + if e.errno != errno.EINPROGRESS: raise print("wrapped") @@ -69,7 +69,7 @@ def test_one(site, opts): try: b = s.read(128) except OSError as err: - if err.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ: + if err.errno == 2: # 2=ssl.SSL_ERROR_WANT_READ: continue raise if b is None: From 178198a01df51b5f4c5ef9f38ab2fb8f6269d5f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Apr 2021 11:00:43 +1000 Subject: [PATCH 0757/3533] tools/pyboard.py: Support opening serial port in exclusive mode. This is now the default, but can be overridden with CLI `--no-exclusive`, or constructing `Pyboard(..., exclusive=False)`. Signed-off-by: Damien George --- tools/pyboard.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 069f7490d0dd1..29a15f7eae25d 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -4,7 +4,7 @@ # # The MIT License (MIT) # -# Copyright (c) 2014-2019 Damien P. George +# Copyright (c) 2014-2021 Damien P. George # Copyright (c) 2017 Paul Sokolovsky # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -252,7 +252,9 @@ def inWaiting(self): class Pyboard: - def __init__(self, device, baudrate=115200, user="micro", password="python", wait=0): + def __init__( + self, device, baudrate=115200, user="micro", password="python", wait=0, exclusive=True + ): self.use_raw_paste = True if device.startswith("exec:"): self.serial = ProcessToSerial(device[len("exec:") :]) @@ -264,10 +266,15 @@ def __init__(self, device, baudrate=115200, user="micro", password="python", wai else: import serial + # Set options, and exclusive if pyserial supports it + serial_kwargs = {"baudrate": baudrate, "interCharTimeout": 1} + if serial.__version__ >= "3.3": + serial_kwargs["exclusive"] = exclusive + delayed = False for attempt in range(wait + 1): try: - self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1) + self.serial = serial.Serial(device, **serial_kwargs) break except (OSError, IOError): # Py2 and Py3 have different errors if wait == 0: @@ -650,6 +657,11 @@ def main(): action="store_true", help="Do not follow the output after running the scripts.", ) + group.add_argument( + "--no-exclusive", + action="store_true", + help="Do not try to open the serial device for exclusive access.", + ) cmd_parser.add_argument( "-f", "--filesystem", @@ -662,7 +674,9 @@ def main(): # open the connection to the pyboard try: - pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait) + pyb = Pyboard( + args.device, args.baudrate, args.user, args.password, args.wait, not args.no_exclusive + ) except PyboardError as er: print(er) sys.exit(1) From df4e9bdf5c69e36775de313b098651bbd97132a7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Apr 2021 10:26:52 +1000 Subject: [PATCH 0758/3533] esp32/CMakeLists.txt: Require CMake version 3.12. Because "find_package(Python3 ...)" requires at least this version of CMake. And other features like GREATER_EQUAL and COMMAND_EXPAND_LISTS need at least CMake 3.7 and 3.8 respectively. Signed-off-by: Damien George --- ports/esp32/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/CMakeLists.txt b/ports/esp32/CMakeLists.txt index fa419202f90d0..29409adc7414f 100644 --- a/ports/esp32/CMakeLists.txt +++ b/ports/esp32/CMakeLists.txt @@ -1,6 +1,6 @@ # Top-level cmake file for building MicroPython on ESP32. -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.12) # Set the location of this port's directory. set(MICROPY_PORT_DIR ${CMAKE_SOURCE_DIR}) From bb2007b05ccf0e41684cc61f1677c11b7df93f7a Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 22 Apr 2021 18:01:46 +0200 Subject: [PATCH 0759/3533] windows/mpconfigport.h: Enable features also present in unix port. --- ports/windows/mpconfigport.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index faf10752ee203..6421c93bdbc62 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -65,6 +65,7 @@ #define MICROPY_VFS_POSIX_FILE (1) #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_DESCRIPTORS (1) +#define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) @@ -75,12 +76,14 @@ #define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) #define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_POW3 (1) +#define MICROPY_PY_BUILTINS_ROUND_INT (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) #define MICROPY_PY_SYS_EXIT (1) +#define MICROPY_PY_SYS_ATEXIT (1) #define MICROPY_PY_SYS_PLATFORM "win32" #ifndef MICROPY_PY_SYS_PATH_DEFAULT #define MICROPY_PY_SYS_PATH_DEFAULT "~/.micropython/lib" @@ -93,6 +96,7 @@ #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1) #define MICROPY_PY_MATH_ISCLOSE (1) #define MICROPY_PY_CMATH (1) +#define MICROPY_PY_IO_IOBASE (1) #define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_GC_COLLECT_RETVAL (1) #define MICROPY_MODULE_FROZEN_STR (0) From a1bc32d8a8fbb09bc04c2ca07b10475f7ddde8c3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 23 Apr 2021 23:44:37 +1000 Subject: [PATCH 0760/3533] drivers/sdcard: Add sleep_ms(1) delay in SDCard.readinto sync loop. So this driver works on faster MCUs (that run this loop fast) with older, slower SD cards. Fixes issue #7129. Signed-off-by: Damien George --- drivers/sdcard/sdcard.py | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/sdcard/sdcard.py b/drivers/sdcard/sdcard.py index c991fe560892a..0ba3076a3d369 100644 --- a/drivers/sdcard/sdcard.py +++ b/drivers/sdcard/sdcard.py @@ -176,6 +176,7 @@ def readinto(self, buf): self.spi.readinto(self.tokenbuf, 0xFF) if self.tokenbuf[0] == _TOKEN_DATA: break + time.sleep_ms(1) else: self.cs(1) raise OSError("timeout waiting for response") From 530c76f6caee9445f468380f58b98f4e3ceda759 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 24 Apr 2021 00:10:59 +1000 Subject: [PATCH 0761/3533] lib/utils: Remove unused PYEXEC_SWITCH_MODE from pyexec.h. It was made obsolete by commit c98c128fe885e539ecd73843756340f8950115c8. Signed-off-by: Damien George --- lib/utils/pyexec.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils/pyexec.h b/lib/utils/pyexec.h index f69c5ce9a884e..981e7dca9fc6b 100644 --- a/lib/utils/pyexec.h +++ b/lib/utils/pyexec.h @@ -41,7 +41,6 @@ extern pyexec_mode_kind_t pyexec_mode_kind; extern int pyexec_system_exit; #define PYEXEC_FORCED_EXIT (0x100) -#define PYEXEC_SWITCH_MODE (0x200) int pyexec_raw_repl(void); int pyexec_friendly_repl(void); From 65b90cd0f9cb3ee8b5954820cd6d68b782121cd1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Apr 2021 11:04:58 +1000 Subject: [PATCH 0762/3533] teensy: Provide own implementation of gc_collect, to not use stm32. Signed-off-by: Damien George --- ports/teensy/Makefile | 1 - ports/teensy/main.c | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index d3978718e0095..cf9529442a96e 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -95,7 +95,6 @@ SRC_C = \ usb.c \ STM_SRC_C = $(addprefix ports/stm32/,\ - gccollect.c \ irq.c \ pin.c \ pin_named_pins.c \ diff --git a/ports/teensy/main.c b/ports/teensy/main.c index df3fd1ffcf5ac..d4c5f0396faf1 100644 --- a/ports/teensy/main.c +++ b/ports/teensy/main.c @@ -9,6 +9,7 @@ #include "py/gc.h" #include "py/mphal.h" #include "gccollect.h" +#include "lib/utils/gchelper.h" #include "lib/utils/pyexec.h" #include "lib/mp-readline/readline.h" #include "lexermemzip.h" @@ -349,6 +350,12 @@ int main(void) { goto soft_reset; } +void gc_collect(void) { + gc_collect_start(); + gc_helper_collect_regs_and_stack(); + gc_collect_end(); +} + // stub out __libc_init_array. It's called by mk20dx128.c and is used to call // global C++ constructors. Since this is a C-only projects, we don't need to // call constructors. From 0f78c36c5aa458a954eed39a46942209107a553e Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 27 Apr 2021 01:15:43 +0200 Subject: [PATCH 0763/3533] tools/gen-cpydiff.py: Fix formatting of doc strings for new Black. Since version 21.4b0, Black now processes one-line docstrings by stripping leading and trailing spaces, and adding a padding space when needed to break up """"; see https://github.com/psf/black/pull/1740 This commit makes the Python code in this repository conform to this rule. --- tools/gen-cpydiff.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/gen-cpydiff.py b/tools/gen-cpydiff.py index c45bad1058c44..c9f58ec505d3a 100644 --- a/tools/gen-cpydiff.py +++ b/tools/gen-cpydiff.py @@ -74,7 +74,7 @@ def readfiles(): - """ Reads test files """ + """Reads test files""" tests = list(filter(lambda x: x.endswith(".py"), os.listdir(TESTPATH))) tests.sort() files = [] @@ -95,7 +95,7 @@ def readfiles(): def uimports(code): - """ converts CPython module names into MicroPython equivalents """ + """converts CPython module names into MicroPython equivalents""" for uimport in UIMPORTLIST: uimport = bytes(uimport, "utf8") code = code.replace(uimport, b"u" + uimport) @@ -103,7 +103,7 @@ def uimports(code): def run_tests(tests): - """ executes all tests """ + """executes all tests""" results = [] for test in tests: with open(TESTPATH + test.name, "rb") as f: @@ -152,7 +152,7 @@ def run_tests(tests): def indent(block, spaces): - """ indents paragraphs of text for rst formatting """ + """indents paragraphs of text for rst formatting""" new_block = "" for line in block.split("\n"): new_block += spaces + line + "\n" @@ -160,7 +160,7 @@ def indent(block, spaces): def gen_table(contents): - """ creates a table given any set of columns """ + """creates a table given any set of columns""" xlengths = [] ylengths = [] for column in contents: @@ -194,7 +194,7 @@ def gen_table(contents): def gen_rst(results): - """ creates restructured text documents to display tests """ + """creates restructured text documents to display tests""" # make sure the destination directory exists try: @@ -254,7 +254,7 @@ def gen_rst(results): def main(): - """ Main function """ + """Main function""" # set search path so that test scripts find the test modules (and no other ones) os.environ["PYTHONPATH"] = TESTPATH From 30d9f77cc535306eeb9eed6f17e71355fd58995a Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Apr 2021 23:46:46 +1000 Subject: [PATCH 0764/3533] top: Update .git-blame-ignore-revs for latest formatting commit. Signed-off-by: Damien George --- .git-blame-ignore-revs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 83726922a5280..c5039f58b54dc 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,3 +1,6 @@ +# tools/gen-cpydiff.py: Fix formatting of doc strings for new Black. +0f78c36c5aa458a954eed39a46942209107a553e + # tests/run-tests.py: Reformat with Black. 2a38d7103672580882fb621a5b76e8d26805d593 From d4b706c4d01377d42855ff1544ced77536f69caf Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Apr 2021 12:13:58 +1000 Subject: [PATCH 0765/3533] py: Add option to compile without any error messages at all. This introduces a new option, MICROPY_ERROR_REPORTING_NONE, which completely disables all error messages. To be used in cases where MicroPython needs to fit in very limited systems. Signed-off-by: Damien George --- py/argcheck.c | 14 ++++++------ py/bc.c | 4 ++-- py/builtinimport.c | 4 ++-- py/compile.c | 4 ++-- py/misc.h | 4 ++++ py/modbuiltins.c | 2 +- py/mpconfig.h | 2 ++ py/obj.c | 20 ++++++++--------- py/obj.h | 5 +++++ py/objexcept.c | 4 ++++ py/objnamedtuple.c | 6 +++--- py/objstr.c | 38 ++++++++++++++++---------------- py/objtype.c | 8 +++---- py/parsenum.c | 2 +- py/runtime.c | 54 ++++++++++++++++++++++++++++++++-------------- py/runtime.h | 13 +++++++++++ 16 files changed, 117 insertions(+), 67 deletions(-) diff --git a/py/argcheck.c b/py/argcheck.c index c333ead05b64a..ffcca4cb65dc2 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -38,7 +38,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) { size_t n_args_max = (sig >> 1) & 0xffff; if (n_kw && !takes_kw) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_TypeError(MP_ERROR_TEXT("function doesn't take keyword arguments")); @@ -47,7 +47,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) { if (n_args_min == n_args_max) { if (n_args != n_args_min) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -57,7 +57,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) { } } else { if (n_args < n_args_min) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -65,7 +65,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) { n_args_min - n_args); #endif } else if (n_args > n_args_max) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -90,7 +90,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP); if (kw == NULL) { if (allowed[i].flags & MP_ARG_REQUIRED) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%q' argument required"), allowed[i].qst); @@ -114,7 +114,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n } if (pos_found < n_pos) { extra_positional: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else // TODO better error message @@ -122,7 +122,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n #endif } if (kws_found < kws->used) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else // TODO better error message diff --git a/py/bc.c b/py/bc.c index 34bc78fd3a588..58694b97dc24a 100644 --- a/py/bc.c +++ b/py/bc.c @@ -75,7 +75,7 @@ const byte *mp_decode_uint_skip(const byte *ptr) { #endif STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, size_t given) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE // generic message, used also for other argument issues (void)f; (void)expected; @@ -212,7 +212,7 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw } // Didn't find name match with positional args if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("unexpected keyword argument")); #else mp_raise_msg_varg(&mp_type_TypeError, diff --git a/py/builtinimport.c b/py/builtinimport.c index bdc82e77c8f74..874d2dd7f0f5c 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -396,7 +396,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #endif if (module_obj == MP_OBJ_NULL) { // couldn't find the file, so fail - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found")); #else mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), mod_name); @@ -499,7 +499,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #endif // Couldn't find the module, so fail - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found")); #else mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), module_name_qstr); diff --git a/py/compile.c b/py/compile.c index d1a4d65c8bb51..1182b8b1eb7a5 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2597,7 +2597,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t * compile_node(comp, pn_i); if (is_dict) { if (!is_key_value) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax")); #else compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting key:value for dict")); @@ -2607,7 +2607,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t * EMIT(store_map); } else { if (is_key_value) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax")); #else compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting just a value for set")); diff --git a/py/misc.h b/py/misc.h index fe2b3b8afaa75..953809838356d 100644 --- a/py/misc.h +++ b/py/misc.h @@ -264,6 +264,10 @@ typedef union _mp_float_union_t { #if MICROPY_ROM_TEXT_COMPRESSION +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE +#error "MICROPY_ERROR_REPORTING_NONE requires MICROPY_ROM_TEXT_COMPRESSION disabled" +#endif + #ifdef NO_QSTR // Compression enabled but doing QSTR extraction. diff --git a/py/modbuiltins.c b/py/modbuiltins.c index cfbfc5a25683e..ac41942b9ca90 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -372,7 +372,7 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { } } - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("ord expects a character")); #else mp_raise_msg_varg(&mp_type_TypeError, diff --git a/py/mpconfig.h b/py/mpconfig.h index 3dd83a34abe72..5c7212fd1f6bc 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -667,6 +667,8 @@ typedef long long mp_longint_impl_t; #define MICROPY_ENABLE_DOC_STRING (0) #endif +// Exception messages are removed (requires disabling MICROPY_ROM_TEXT_COMPRESSION) +#define MICROPY_ERROR_REPORTING_NONE (0) // Exception messages are short static strings #define MICROPY_ERROR_REPORTING_TERSE (1) // Exception messages provide basic error details diff --git a/py/obj.c b/py/obj.c index ed047acc39f76..f66a9d183c704 100644 --- a/py/obj.c +++ b/py/obj.c @@ -359,7 +359,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) { mp_float_t val; if (!mp_obj_get_float_maybe(arg, &val)) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -399,7 +399,7 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { if (!mp_obj_get_complex_maybe(arg, real, imag)) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -417,7 +417,7 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) { } else if (mp_obj_is_type(o, &mp_type_list)) { mp_obj_list_get(o, len, items); } else { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -431,7 +431,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) { size_t seq_len; mp_obj_get_array(o, &seq_len, items); if (seq_len != len) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length")); #else mp_raise_msg_varg(&mp_type_ValueError, @@ -446,7 +446,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool if (mp_obj_is_small_int(index)) { i = MP_OBJ_SMALL_INT_VALUE(index); } else if (!mp_obj_get_int_maybe(index, &i)) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -466,7 +466,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool } } else { if (i < 0 || (mp_uint_t)i >= len) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range")); #else mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name); @@ -500,7 +500,7 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) { mp_obj_t mp_obj_len(mp_obj_t o_in) { mp_obj_t len = mp_obj_len_maybe(o_in); if (len == MP_OBJ_NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object has no len")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -541,21 +541,21 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { // TODO: call base classes here? } if (value == MP_OBJ_NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion")); #else mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base)); #endif } else if (value == MP_OBJ_SENTINEL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable")); #else mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base)); #endif } else { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment")); #else mp_raise_msg_varg(&mp_type_TypeError, diff --git a/py/obj.h b/py/obj.h index 6a040b77739c2..86e5d83125cb5 100644 --- a/py/obj.h +++ b/py/obj.h @@ -741,8 +741,13 @@ mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag); mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type); mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg); mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args); +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE +#define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type) +#define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type) +#else mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg); mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!) +#endif #ifdef va_start mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, va_list arg); // same fmt restrictions as above #endif diff --git a/py/objexcept.c b/py/objexcept.c index f6bffec38353d..f03bb1b416cfc 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -373,6 +373,8 @@ mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, return mp_obj_exception_make_new(exc_type, n_args, 0, args); } +#if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_NONE + mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) { // Check that the given type is an exception type assert(exc_type->make_new == mp_obj_exception_make_new); @@ -518,6 +520,8 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_er return mp_obj_exception_make_new(exc_type, 1, 0, &arg); } +#endif + // return true if the given object is an exception type bool mp_obj_is_exception_type(mp_obj_t self_in) { if (mp_obj_is_type(self_in, &mp_type_type)) { diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index e4543f5b9b180..214cad2572d7d 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -95,7 +95,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t *)type_in; size_t num_fields = type->n_fields; if (n_args + n_kw != num_fields) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL mp_raise_msg_varg(&mp_type_TypeError, @@ -121,14 +121,14 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, qstr kw = mp_obj_str_get_qstr(args[i]); size_t id = mp_obj_namedtuple_find_field(type, kw); if (id == (size_t)-1) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("unexpected keyword argument '%q'"), kw); #endif } if (tuple->items[id] != MP_OBJ_NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_arg_error_terse_mismatch(); #else mp_raise_msg_varg(&mp_type_TypeError, diff --git a/py/objstr.c b/py/objstr.c index 9f8da873d66c0..98657bd217b93 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -942,7 +942,7 @@ STATIC mp_obj_t arg_as_int(mp_obj_t arg) { } #endif -#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE +#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE STATIC NORETURN void terse_str_format_value_error(void) { mp_raise_ValueError(MP_ERROR_TEXT("bad format string")); } @@ -963,7 +963,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar vstr_add_byte(&vstr, '}'); continue; } - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("single '}' encountered in format string")); @@ -1002,7 +1002,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar if (str < top && (*str == 'r' || *str == 's')) { conversion = *str++; } else { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL mp_raise_ValueError(MP_ERROR_TEXT("bad conversion specifier")); @@ -1040,14 +1040,14 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } } if (str >= top) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("unmatched '{' in format")); #endif } if (*str != '}') { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("expected ':' after format specifier")); @@ -1060,7 +1060,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar int index = 0; if (MP_LIKELY(unichar_isdigit(*field_name))) { if (*arg_i > 0) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError( @@ -1090,7 +1090,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } } else { if (*arg_i < 0) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError( @@ -1183,7 +1183,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar type = *s++; } if (*s) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("invalid format specifier")); @@ -1204,14 +1204,14 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar if (flags & (PF_FLAG_SHOW_SIGN | PF_FLAG_SPACE_SIGN)) { if (type == 's') { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("sign not allowed in string format specifier")); #endif } if (type == 'c') { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError( @@ -1275,7 +1275,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar break; default: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_msg_varg(&mp_type_ValueError, @@ -1347,7 +1347,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar #endif default: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_msg_varg(&mp_type_ValueError, @@ -1359,7 +1359,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar // arg doesn't look like a number if (align == '=') { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError( @@ -1383,7 +1383,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } default: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_msg_varg(&mp_type_ValueError, @@ -1412,7 +1412,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ mp_check_self(mp_obj_is_str_or_bytes(pattern)); GET_STR_DATA_LEN(pattern, str, len); - #if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_TERSE const byte *start_str = str; #endif bool is_bytes = mp_obj_is_type(pattern, &mp_type_bytes); @@ -1444,7 +1444,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ const byte *key = ++str; while (*str != ')') { if (str >= top) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("incomplete format key")); @@ -1508,7 +1508,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ if (str >= top) { incomplete_format: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_ValueError(MP_ERROR_TEXT("incomplete format")); @@ -1594,7 +1594,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ break; default: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE terse_str_format_value_error(); #else mp_raise_msg_varg(&mp_type_ValueError, @@ -2131,7 +2131,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) { } STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't convert to str implicitly")); #else const qstr src_name = mp_obj_get_type(self_in)->name; diff --git a/py/objtype.c b/py/objtype.c index 7f75232941046..508bab99d394a 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -348,7 +348,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size m_del(mp_obj_t, args2, 2 + n_args + 2 * n_kw); } if (init_ret != mp_const_none) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("__init__() should return None")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -860,7 +860,7 @@ mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL}; mp_obj_t call = mp_obj_instance_get_call(self_in, member); if (call == MP_OBJ_NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object not callable")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -985,7 +985,7 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in); if (self->make_new == NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't create instance")); #else mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("can't create '%q' instances"), self->name); @@ -1125,7 +1125,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]); // TODO: Verify with CPy, tested on function type if (t->make_new == NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("type isn't an acceptable base type")); #else mp_raise_msg_varg(&mp_type_TypeError, diff --git a/py/parsenum.c b/py/parsenum.c index e665da7d8cdad..1cfe842577011 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -145,7 +145,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m value_error: { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("invalid syntax for integer")); raise_exc(exc, lex); diff --git a/py/runtime.c b/py/runtime.c index 0ce6854732822..18a7c8565da9b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -189,7 +189,7 @@ mp_obj_t mp_load_global(qstr qst) { #endif elem = mp_map_lookup((mp_map_t *)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP); if (elem == NULL) { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_NameError, MP_ERROR_TEXT("name not defined")); #else mp_raise_msg_varg(&mp_type_NameError, MP_ERROR_TEXT("name '%q' isn't defined"), qst); @@ -289,7 +289,7 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { } // With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int(). // In this case provide a more focused error message to not confuse, e.g. chr(1.0) - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE if (op == MP_UNARY_OP_INT) { mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int")); } else { @@ -610,7 +610,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { } unsupported_op: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -652,7 +652,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons return type->call(fun_in, n_args, n_kw, args); } - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object not callable")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -881,13 +881,13 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) { return; too_short: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack")); #else mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len); #endif too_long: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack")); #else mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("too many values to unpack (expected %d)"), (int)num); @@ -948,7 +948,7 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) { return; too_short: - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack")); #else mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len); @@ -1117,7 +1117,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) { if (dest[0] == MP_OBJ_NULL) { // no attribute/method called attr - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute")); #else // following CPython, we give a more detailed error message for type objects @@ -1161,7 +1161,7 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { return; } } - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute")); #else mp_raise_msg_varg(&mp_type_AttributeError, @@ -1206,7 +1206,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { } // object not iterable - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object not iterable")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -1229,7 +1229,7 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { // __next__ exists, call it and return its result return mp_call_method_n_kw(0, 0, dest); } else { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -1265,7 +1265,7 @@ mp_obj_t mp_iternext(mp_obj_t o_in) { } } } else { - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE + #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator")); #else mp_raise_msg_varg(&mp_type_TypeError, @@ -1512,6 +1512,26 @@ NORETURN void m_malloc_fail(size_t num_bytes) { MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)num_bytes); } +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE + +NORETURN void mp_raise_type(const mp_obj_type_t *exc_type) { + nlr_raise(mp_obj_new_exception(exc_type)); +} + +NORETURN void mp_raise_ValueError_no_msg(void) { + mp_raise_type(&mp_type_ValueError); +} + +NORETURN void mp_raise_TypeError_no_msg(void) { + mp_raise_type(&mp_type_TypeError); +} + +NORETURN void mp_raise_NotImplementedError_no_msg(void) { + mp_raise_type(&mp_type_NotImplementedError); +} + +#else + NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) { if (msg == NULL) { nlr_raise(mp_obj_new_exception(exc_type)); @@ -1536,14 +1556,16 @@ NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg) { mp_raise_msg(&mp_type_TypeError, msg); } -NORETURN void mp_raise_OSError(int errno_) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_))); -} - NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg) { mp_raise_msg(&mp_type_NotImplementedError, msg); } +#endif + +NORETURN void mp_raise_OSError(int errno_) { + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_))); +} + #if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK NORETURN void mp_raise_recursion_depth(void) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, diff --git a/py/runtime.h b/py/runtime.h index 0bf988b905656..0cbbb287ac0f0 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -159,12 +159,25 @@ mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); mp_obj_t mp_import_from(mp_obj_t module, qstr name); void mp_import_all(mp_obj_t module); +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE +NORETURN void mp_raise_type(const mp_obj_type_t *exc_type); +NORETURN void mp_raise_ValueError_no_msg(void); +NORETURN void mp_raise_TypeError_no_msg(void); +NORETURN void mp_raise_NotImplementedError_no_msg(void); +#define mp_raise_msg(exc_type, msg) mp_raise_type(exc_type) +#define mp_raise_msg_varg(exc_type, ...) mp_raise_type(exc_type) +#define mp_raise_ValueError(msg) mp_raise_ValueError_no_msg() +#define mp_raise_TypeError(msg) mp_raise_TypeError_no_msg() +#define mp_raise_NotImplementedError(msg) mp_raise_NotImplementedError_no_msg() +#else #define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL) NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg); NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg); NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg); NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg); +#endif + NORETURN void mp_raise_OSError(int errno_); NORETURN void mp_raise_recursion_depth(void); From 43a8c8178e0b5e2460edde0fdf436509a7f28764 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Apr 2021 23:52:40 +1000 Subject: [PATCH 0766/3533] bare-arm: Switch to use MICROPY_ERROR_REPORTING_NONE to reduce size. Reduces size of this port by about 3300 bytes, and demonstrates how to use this feature. Signed-off-by: Damien George --- ports/bare-arm/Makefile | 2 +- ports/bare-arm/README.md | 2 +- ports/bare-arm/mpconfigport.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/bare-arm/Makefile b/ports/bare-arm/Makefile index 455a9dc5ac137..1a21eb56a867b 100644 --- a/ports/bare-arm/Makefile +++ b/ports/bare-arm/Makefile @@ -5,7 +5,7 @@ include ../../py/mkenv.mk include $(TOP)/py/py.mk # Set makefile-level MicroPython feature configurations. -MICROPY_ROM_TEXT_COMPRESSION ?= 1 +MICROPY_ROM_TEXT_COMPRESSION ?= 0 # Define toolchain and other tools. CROSS_COMPILE ?= arm-none-eabi- diff --git a/ports/bare-arm/README.md b/ports/bare-arm/README.md index 496ee9c7555de..dfa5534d598f9 100644 --- a/ports/bare-arm/README.md +++ b/ports/bare-arm/README.md @@ -18,4 +18,4 @@ compiled and executed when the firmware starts. They produce output on the system's stdout. The size of the firmware (the machine code that is programmed to the -microcontroller's flash/ROM) is currently around 61200 bytes. +microcontroller's flash/ROM) is currently around 57900 bytes. diff --git a/ports/bare-arm/mpconfigport.h b/ports/bare-arm/mpconfigport.h index 41b6ee71a3f84..3bebc31594c27 100644 --- a/ports/bare-arm/mpconfigport.h +++ b/ports/bare-arm/mpconfigport.h @@ -37,7 +37,7 @@ // Python internal features #define MICROPY_ENABLE_EXTERNAL_IMPORT (0) -#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) +#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NONE) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_MODULE_GETATTR (0) #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) From c5cbfd545ab436ef526e7fce7fafab89ac1b69f8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 24 Apr 2021 09:18:09 +1000 Subject: [PATCH 0767/3533] py/dynruntime.h: Add mp_obj_get_array() function. Signed-off-by: Damien George --- py/dynruntime.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/py/dynruntime.h b/py/dynruntime.h index eb8301284e1fc..a8256c19487c4 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -81,6 +81,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) { #define mp_type_type (*mp_fun_table.type_type) #define mp_type_str (*mp_fun_table.type_str) +#define mp_type_tuple (*((mp_obj_base_t *)mp_const_empty_tuple)->type) #define mp_type_list (*mp_fun_table.type_list) #define mp_type_EOFError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_EOFError))) #define mp_type_IndexError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_IndexError))) @@ -121,6 +122,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) { #define mp_obj_len(o) (mp_obj_len_dyn(o)) #define mp_obj_subscr(base, index, val) (mp_fun_table.obj_subscr((base), (index), (val))) +#define mp_obj_get_array(o, len, items) (mp_obj_get_array_dyn((o), (len), (items))) #define mp_obj_list_append(list, item) (mp_fun_table.list_append((list), (item))) static inline mp_obj_t mp_obj_new_str_of_type_dyn(const mp_obj_type_t *type, const byte *data, size_t len) { @@ -251,4 +253,23 @@ static inline void mp_raise_OSError_dyn(int er) { #define mp_obj_get_float(o) (mp_obj_get_float_to_d((o))) #endif +/******************************************************************************/ +// Inline function definitions. + +// *items may point inside a GC block +static inline void mp_obj_get_array_dyn(mp_obj_t o, size_t *len, mp_obj_t **items) { + const mp_obj_type_t *type = mp_obj_get_type(o); + if (type == &mp_type_tuple) { + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(o); + *len = t->len; + *items = &t->items[0]; + } else if (type == &mp_type_list) { + mp_obj_list_t *l = MP_OBJ_TO_PTR(o); + *len = l->len; + *items = l->items; + } else { + mp_raise_TypeError("expected tuple/list"); + } +} + #endif // MICROPY_INCLUDED_PY_DYNRUNTIME_H From f452b9c26569a4e05e026a668dd336f80203f06d Mon Sep 17 00:00:00 2001 From: Daniel Maslowski Date: Sat, 24 Apr 2021 17:41:55 +0200 Subject: [PATCH 0768/3533] pic16bit/Makefile: Make the XC compiler version user-configurable. --- ports/pic16bit/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/pic16bit/Makefile b/ports/pic16bit/Makefile index fa6de38e39274..3c2bd54ea231f 100644 --- a/ports/pic16bit/Makefile +++ b/ports/pic16bit/Makefile @@ -6,7 +6,8 @@ QSTR_DEFS = qstrdefsport.h # include py core make definitions include $(TOP)/py/py.mk -XC16 = /opt/microchip/xc16/v1.35 +XCVERSION ?= 1.35 +XC16 ?= /opt/microchip/xc16/v$(XCVERSION) CROSS_COMPILE ?= $(XC16)/bin/xc16- PARTFAMILY = dsPIC33F From 4dc802400fb81cf674067b9fcb177139ea5ae27e Mon Sep 17 00:00:00 2001 From: plan-do-break-fix Date: Sun, 25 Apr 2021 01:38:45 -0500 Subject: [PATCH 0769/3533] stm32,teensy: Correct typos in project README files. --- ports/stm32/mboot/README.md | 2 +- ports/teensy/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/mboot/README.md b/ports/stm32/mboot/README.md index 59213eb615141..b85bc3e2eedbd 100644 --- a/ports/stm32/mboot/README.md +++ b/ports/stm32/mboot/README.md @@ -159,7 +159,7 @@ Signed and encrypted DFU support -------------------------------- Mboot optionally supports signing and encrypting the binary firmware in the DFU file. -In general this is refered to as a packed DFU file. This requires additional settings +In general this is referred to as a packed DFU file. This requires additional settings in the board config and requires the `pyhy` Python module to be installed for `python3` to be used when building packed firmware, eg: diff --git a/ports/teensy/README.md b/ports/teensy/README.md index c586853b52693..dacc463ccdcd5 100644 --- a/ports/teensy/README.md +++ b/ports/teensy/README.md @@ -5,7 +5,7 @@ Currently the Teensy 3.1 port of MicroPython builds under Linux and not under Wi The tool chain required for the build can be found at . Download the current Linux *.tar.bz2 file. Instructions regarding unpacking the file and moving it to the correct location -as well as adding the extracted folders to the enviroment variable can be found at +as well as adding the extracted folders to the environment variable can be found at In order to download the firmware image to the teensy, you'll need to use the From a708848b0cb0f304f43178140d84d5535e186a38 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 27 Apr 2021 00:54:21 +0200 Subject: [PATCH 0770/3533] stm32/uart: Fix H7 UART clock source configuration. Previously to this commit, Usart16ClockSelection was overwritten and Usart234578ClockSelection was not set. --- ports/stm32/uart.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 36c59cee4ae02..babdf63d96b35 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -118,16 +118,10 @@ STATIC const pyb_uart_irq_map_t mp_uart_irq_map[] = { void uart_init0(void) { #if defined(STM32H7) RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit = {0}; - // Configure USART1/6 clock source - RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART16; + // Configure USART1/6 and USART2/3/4/5/7/8 clock sources + RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART16 | RCC_PERIPHCLK_USART234578; RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2; - if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) { - __fatal_error("HAL_RCCEx_PeriphCLKConfig"); - } - - // Configure USART2/3/4/5/7/8 clock source - RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART234578; - RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1; + RCC_PeriphClkInit.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1; if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) { __fatal_error("HAL_RCCEx_PeriphCLKConfig"); } From 21fee92be6c534d0d44518c0c76f0c7cf5f64b18 Mon Sep 17 00:00:00 2001 From: Steve App Date: Sun, 25 Apr 2021 11:05:47 +0100 Subject: [PATCH 0771/3533] esp32: Restore FROZEN_MANIFEST support with new CMake build system. This commit re-enables the command-line make option "FROZEN_MANIFEST". The boards/*/mpconfigboard.cmake will now use the command-line FROZEN_MANIFEST value if supplied. Usage: make FROZEN_MANIFEST=~/foo/my-manifest.py --- ports/esp32/Makefile | 4 ++++ ports/esp32/boards/GENERIC/mpconfigboard.cmake | 5 +++-- ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake | 4 +++- ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake | 4 +++- ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake | 4 +++- ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake | 4 +++- ports/esp32/boards/TINYPICO/mpconfigboard.cmake | 4 +++- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 39b740ffae074..5a40cd956940e 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -26,6 +26,10 @@ endif IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -B $(BUILD) $(CMAKE_ARGS) +ifdef FROZEN_MANIFEST + IDFPY_FLAGS += -D MICROPY_FROZEN_MANIFEST=$(FROZEN_MANIFEST) +endif + all: idf.py $(IDFPY_FLAGS) build @$(PYTHON) makeimg.py \ diff --git a/ports/esp32/boards/GENERIC/mpconfigboard.cmake b/ports/esp32/boards/GENERIC/mpconfigboard.cmake index f69b05f47b042..4aaa0420d97b0 100644 --- a/ports/esp32/boards/GENERIC/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC/mpconfigboard.cmake @@ -2,5 +2,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base boards/sdkconfig.ble ) - -set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +endif() diff --git a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake index 686c78df97f23..729377a5b4234 100644 --- a/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_D2WD/mpconfigboard.cmake @@ -4,4 +4,6 @@ set(SDKCONFIG_DEFAULTS boards/GENERIC_D2WD/sdkconfig.board ) -set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +endif() diff --git a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake index ca552d470c0a6..88c0efa218ae7 100644 --- a/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_OTA/mpconfigboard.cmake @@ -4,4 +4,6 @@ set(SDKCONFIG_DEFAULTS boards/GENERIC_OTA/sdkconfig.board ) -set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +endif() diff --git a/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake index 23d52e8b941a0..73f2b24e66423 100644 --- a/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_S2/mpconfigboard.cmake @@ -5,4 +5,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.usb ) -set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +endif() diff --git a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake index 2128edb592f2a..bb38f7df2b324 100644 --- a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake @@ -4,4 +4,6 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.spiram ) -set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) +endif() diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake index 5c31f5c60065e..0c65103a85347 100644 --- a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake +++ b/ports/esp32/boards/TINYPICO/mpconfigboard.cmake @@ -6,4 +6,6 @@ set(SDKCONFIG_DEFAULTS boards/TINYPICO/sdkconfig.board ) -set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) +endif() From 326dd7f0dbbadc4a536636d486600ce93dade8c5 Mon Sep 17 00:00:00 2001 From: Steve App Date: Sun, 25 Apr 2021 12:45:06 +0000 Subject: [PATCH 0772/3533] tools/makemanifest.py: Show directory name if there is a FreezeError. --- tools/makemanifest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 117d1536e87d1..51de01dd8a3d5 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -199,7 +199,7 @@ def mkdir(filename): def freeze_internal(kind, path, script, opt): path = convert_path(path) if not os.path.isdir(path): - raise FreezeError("freeze path must be a directory") + raise FreezeError("freeze path must be a directory: {}".format(path)) if script is None and kind == KIND_AS_STR: if any(f[0] == KIND_AS_STR for f in manifest_list): raise FreezeError("can only freeze one str directory") From 89b64478c73d2228419e77a4450c004157ec725a Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Apr 2021 16:30:58 +1000 Subject: [PATCH 0773/3533] stm32/softtimer: Add support for having a C-based callback. Signed-off-by: Damien George --- ports/stm32/machine_timer.c | 7 ++++--- ports/stm32/softtimer.c | 6 +++++- ports/stm32/softtimer.h | 10 ++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index c37ac87672ac2..3f83d5ba61ed9 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -76,10 +76,10 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar self->expiry_ms = mp_hal_ticks_ms() + self->delta_ms; if (args[ARG_callback].u_obj != MP_OBJ_NULL) { - self->callback = args[ARG_callback].u_obj; + self->py_callback = args[ARG_callback].u_obj; } - if (self->callback != mp_const_none) { + if (self->py_callback != mp_const_none) { soft_timer_insert(self); } @@ -89,8 +89,9 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); self->pairheap.base.type = &machine_timer_type; + self->flags = SOFT_TIMER_FLAG_PY_CALLBACK; self->delta_ms = 1000; - self->callback = mp_const_none; + self->py_callback = mp_const_none; // Get timer id (only soft timer (-1) supported at the moment) mp_int_t id = -1; diff --git a/ports/stm32/softtimer.c b/ports/stm32/softtimer.c index d0a186c7d0489..7e439d4e75c66 100644 --- a/ports/stm32/softtimer.c +++ b/ports/stm32/softtimer.c @@ -64,7 +64,11 @@ void soft_timer_handler(void) { while (heap != NULL && TICKS_DIFF(heap->expiry_ms, ticks_ms) <= 0) { soft_timer_entry_t *entry = heap; heap = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap->pairheap); - mp_sched_schedule(entry->callback, MP_OBJ_FROM_PTR(entry)); + if (entry->flags & SOFT_TIMER_FLAG_PY_CALLBACK) { + mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry)); + } else { + entry->c_callback(entry); + } if (entry->mode == SOFT_TIMER_MODE_PERIODIC) { entry->expiry_ms += entry->delta_ms; heap = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap->pairheap, &entry->pairheap); diff --git a/ports/stm32/softtimer.h b/ports/stm32/softtimer.h index a80d9087b57e3..6e06bbcc65dc1 100644 --- a/ports/stm32/softtimer.h +++ b/ports/stm32/softtimer.h @@ -28,15 +28,21 @@ #include "py/pairheap.h" +#define SOFT_TIMER_FLAG_PY_CALLBACK (1) + #define SOFT_TIMER_MODE_ONE_SHOT (1) #define SOFT_TIMER_MODE_PERIODIC (2) typedef struct _soft_timer_entry_t { mp_pairheap_t pairheap; - uint32_t mode; + uint16_t flags; + uint16_t mode; uint32_t expiry_ms; uint32_t delta_ms; // for periodic mode - mp_obj_t callback; + union { + void (*c_callback)(struct _soft_timer_entry_t *); + mp_obj_t py_callback; + }; } soft_timer_entry_t; extern volatile uint32_t soft_timer_next; From 647fa63f9c457b616b1b20fdd98403b681302680 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Apr 2021 15:21:22 +1000 Subject: [PATCH 0774/3533] stm32/softtimer: Support static soft timer instances. This adds support for making static (ie not on the Python GC heap) soft timers. This can be useful for a board to define a custom background handler, or eventually for BLE/network processing to use instead of systick slots; it will be more efficient using soft timer for this. The main issue with using the existing code for static soft timers is that it would combine heap allocated and statically allocated soft_timer_entry_t instances in the same pairing-heap data structure. This would prevent the GC from tracing some of the heap allocated entries (because the GC won't follow pointers outside the heap). This commit makes it so that soft timer entries are explicitly marked, instead of relying on implicit marking by having the root of the pairing heap in the root pointer section. Also, on soft reset only the heap- allocated soft timers are deleted from the pairing heap, leaving the statically allocated ones. Signed-off-by: Damien George --- ports/stm32/gccollect.c | 4 +++ ports/stm32/machine_timer.c | 5 ++- ports/stm32/mpconfigport.h | 2 -- ports/stm32/softtimer.c | 63 +++++++++++++++++++++++++++++++------ ports/stm32/softtimer.h | 6 +++- 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/ports/stm32/gccollect.c b/ports/stm32/gccollect.c index ad5645738ea16..8b47b121ec2c3 100644 --- a/ports/stm32/gccollect.c +++ b/ports/stm32/gccollect.c @@ -32,6 +32,7 @@ #include "py/mpthread.h" #include "lib/utils/gchelper.h" #include "gccollect.h" +#include "softtimer.h" #include "systick.h" void gc_collect(void) { @@ -51,6 +52,9 @@ void gc_collect(void) { mp_thread_gc_others(); #endif + // trace soft timer nodes + soft_timer_gc_mark_all(); + // end the GC gc_collect_end(); diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index 3f83d5ba61ed9..c94532218773f 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -73,14 +73,13 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar mp_raise_ValueError(MP_ERROR_TEXT("period too large")); } self->delta_ms = (uint32_t)delta_ms; - self->expiry_ms = mp_hal_ticks_ms() + self->delta_ms; if (args[ARG_callback].u_obj != MP_OBJ_NULL) { self->py_callback = args[ARG_callback].u_obj; } if (self->py_callback != mp_const_none) { - soft_timer_insert(self); + soft_timer_insert(self, self->delta_ms); } return mp_const_none; @@ -89,7 +88,7 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); self->pairheap.base.type = &machine_timer_type; - self->flags = SOFT_TIMER_FLAG_PY_CALLBACK; + self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED; self->delta_ms = 1000; self->py_callback = mp_const_none; diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 2888cac88c43b..28a2d6a76df79 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -371,8 +371,6 @@ struct _mp_bluetooth_btstack_root_pointers_t; \ mp_obj_t pyb_extint_callback[PYB_EXTI_NUM_VECTORS]; \ \ - struct _soft_timer_entry_t *soft_timer_heap; \ - \ /* pointers to all Timer objects (if they have been created) */ \ struct _pyb_timer_obj_t *pyb_timer_obj_all[MICROPY_HW_MAX_TIMER]; \ \ diff --git a/ports/stm32/softtimer.c b/ports/stm32/softtimer.c index 7e439d4e75c66..7f19eccd2d36d 100644 --- a/ports/stm32/softtimer.c +++ b/ports/stm32/softtimer.c @@ -25,6 +25,8 @@ */ #include +#include "py/gc.h" +#include "py/mphal.h" #include "py/runtime.h" #include "irq.h" #include "softtimer.h" @@ -36,9 +38,10 @@ extern __IO uint32_t uwTick; volatile uint32_t soft_timer_next; -void soft_timer_deinit(void) { - MP_STATE_PORT(soft_timer_heap) = NULL; -} +// Pointer to the pairheap of soft timer objects. +// This may contain bss/data pointers as well as GC-heap pointers, +// and is explicitly GC traced by soft_timer_gc_mark_all(). +STATIC soft_timer_entry_t *soft_timer_heap; STATIC int soft_timer_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) { soft_timer_entry_t *e1 = (soft_timer_entry_t *)n1; @@ -57,10 +60,26 @@ STATIC void soft_timer_schedule_systick(uint32_t ticks_ms) { enable_irq(irq_state); } +void soft_timer_deinit(void) { + // Pop off all the nodes which are allocated on the GC-heap. + uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); + soft_timer_entry_t *heap_from = soft_timer_heap; + soft_timer_entry_t *heap_to = (soft_timer_entry_t *)mp_pairheap_new(soft_timer_lt); + while (heap_from != NULL) { + soft_timer_entry_t *entry = (soft_timer_entry_t *)mp_pairheap_peek(soft_timer_lt, &heap_from->pairheap); + heap_from = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap_from->pairheap); + if (!(entry->flags & SOFT_TIMER_FLAG_GC_ALLOCATED)) { + heap_to = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap_to->pairheap, &entry->pairheap); + } + } + soft_timer_heap = heap_to; + restore_irq_pri(irq_state); +} + // Must be executed at IRQ_PRI_PENDSV void soft_timer_handler(void) { uint32_t ticks_ms = uwTick; - soft_timer_entry_t *heap = MP_STATE_PORT(soft_timer_heap); + soft_timer_entry_t *heap = soft_timer_heap; while (heap != NULL && TICKS_DIFF(heap->expiry_ms, ticks_ms) <= 0) { soft_timer_entry_t *entry = heap; heap = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap->pairheap); @@ -74,7 +93,7 @@ void soft_timer_handler(void) { heap = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap->pairheap, &entry->pairheap); } } - MP_STATE_PORT(soft_timer_heap) = heap; + soft_timer_heap = heap; if (heap == NULL) { // No more timers left, set largest delay possible soft_timer_next = uwTick; @@ -84,11 +103,37 @@ void soft_timer_handler(void) { } } -void soft_timer_insert(soft_timer_entry_t *entry) { +void soft_timer_gc_mark_all(void) { + // Mark all soft timer nodes that are allocated on the GC-heap. + // To avoid deep C recursion, pop and recreate the pairheap as nodes are marked. + uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); + soft_timer_entry_t *heap_from = soft_timer_heap; + soft_timer_entry_t *heap_to = (soft_timer_entry_t *)mp_pairheap_new(soft_timer_lt); + while (heap_from != NULL) { + soft_timer_entry_t *entry = (soft_timer_entry_t *)mp_pairheap_peek(soft_timer_lt, &heap_from->pairheap); + heap_from = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap_from->pairheap); + if (entry->flags & SOFT_TIMER_FLAG_GC_ALLOCATED) { + gc_collect_root((void **)&entry, 1); + } + heap_to = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap_to->pairheap, &entry->pairheap); + } + soft_timer_heap = heap_to; + restore_irq_pri(irq_state); +} + +void soft_timer_static_init(soft_timer_entry_t *entry, uint16_t mode, uint32_t delta_ms, void (*cb)(soft_timer_entry_t *)) { + entry->flags = 0; + entry->mode = mode; + entry->delta_ms = delta_ms; + entry->c_callback = cb; +} + +void soft_timer_insert(soft_timer_entry_t *entry, uint32_t initial_delta_ms) { mp_pairheap_init_node(soft_timer_lt, &entry->pairheap); + entry->expiry_ms = mp_hal_ticks_ms() + initial_delta_ms; uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); - MP_STATE_PORT(soft_timer_heap) = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &MP_STATE_PORT(soft_timer_heap)->pairheap, &entry->pairheap); - if (entry == MP_STATE_PORT(soft_timer_heap)) { + soft_timer_heap = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &soft_timer_heap->pairheap, &entry->pairheap); + if (entry == soft_timer_heap) { // This new timer became the earliest one so set soft_timer_next soft_timer_schedule_systick(entry->expiry_ms); } @@ -97,6 +142,6 @@ void soft_timer_insert(soft_timer_entry_t *entry) { void soft_timer_remove(soft_timer_entry_t *entry) { uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); - MP_STATE_PORT(soft_timer_heap) = (soft_timer_entry_t *)mp_pairheap_delete(soft_timer_lt, &MP_STATE_PORT(soft_timer_heap)->pairheap, &entry->pairheap); + soft_timer_heap = (soft_timer_entry_t *)mp_pairheap_delete(soft_timer_lt, &soft_timer_heap->pairheap, &entry->pairheap); restore_irq_pri(irq_state); } diff --git a/ports/stm32/softtimer.h b/ports/stm32/softtimer.h index 6e06bbcc65dc1..25d828a9fd453 100644 --- a/ports/stm32/softtimer.h +++ b/ports/stm32/softtimer.h @@ -29,6 +29,7 @@ #include "py/pairheap.h" #define SOFT_TIMER_FLAG_PY_CALLBACK (1) +#define SOFT_TIMER_FLAG_GC_ALLOCATED (2) #define SOFT_TIMER_MODE_ONE_SHOT (1) #define SOFT_TIMER_MODE_PERIODIC (2) @@ -49,7 +50,10 @@ extern volatile uint32_t soft_timer_next; void soft_timer_deinit(void); void soft_timer_handler(void); -void soft_timer_insert(soft_timer_entry_t *entry); +void soft_timer_gc_mark_all(void); + +void soft_timer_static_init(soft_timer_entry_t *entry, uint16_t mode, uint32_t delta_ms, void (*cb)(soft_timer_entry_t *)); +void soft_timer_insert(soft_timer_entry_t *entry, uint32_t initial_delta_ms); void soft_timer_remove(soft_timer_entry_t *entry); #endif // MICROPY_INCLUDED_STM32_SOFTTIMER_H From a72b8443cae183ecd5396d3ae963984f4d2e1cdc Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:30:17 +1000 Subject: [PATCH 0775/3533] stm32/boardctrl: Add constants for reset mode values. And use the same boardctrl.h header for both the application and mboot so these constants are consistent. Signed-off-by: Damien George --- ports/stm32/boardctrl.c | 18 ++++++++++-------- ports/stm32/boardctrl.h | 9 +++++++++ ports/stm32/main.c | 5 +++-- ports/stm32/mboot/main.c | 7 ++++--- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index c82fefa97cf3b..ce04914ea907c 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -57,8 +57,8 @@ STATIC uint update_reset_mode(uint reset_mode) { } mp_hal_delay_ms(20); if (i % 30 == 29) { - if (++reset_mode > 3) { - reset_mode = 1; + if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); @@ -97,8 +97,8 @@ STATIC uint update_reset_mode(uint reset_mode) { if (!switch_get()) { break; } - if (++reset_mode > 3) { - reset_mode = 1; + if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } } // Flash the selected reset mode @@ -124,7 +124,7 @@ void boardctrl_before_soft_reset_loop(boardctrl_state_t *state) { #if !MICROPY_HW_USES_BOOTLOADER // Update the reset_mode via the default // method which uses the board switch/button and LEDs. - state->reset_mode = update_reset_mode(1); + state->reset_mode = update_reset_mode(BOARDCTRL_RESET_MODE_NORMAL); #endif } @@ -142,7 +142,8 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { } int boardctrl_run_boot_py(boardctrl_state_t *state) { - bool run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; + bool run_boot_py = state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL + || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM; if (run_boot_py) { // Run boot.py, if it exists. @@ -174,7 +175,8 @@ int boardctrl_run_boot_py(boardctrl_state_t *state) { } int boardctrl_run_main_py(boardctrl_state_t *state) { - bool run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) + bool run_main_py = (state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL + || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; if (run_main_py) { @@ -205,5 +207,5 @@ void boardctrl_start_soft_reset(boardctrl_state_t *state) { void boardctrl_end_soft_reset(boardctrl_state_t *state) { // Set reset_mode to normal boot. - state->reset_mode = 1; + state->reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index 33e71b65c9975..551be3453ee22 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -60,11 +60,20 @@ #define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset #endif +// Constants to return from boardctrl_run_boot_py, boardctrl_run_main_py. enum { BOARDCTRL_CONTINUE, BOARDCTRL_GOTO_SOFT_RESET_EXIT, }; +// Constants for boardctrl_state_t.reset_mode. +enum { + BOARDCTRL_RESET_MODE_NORMAL = 1, + BOARDCTRL_RESET_MODE_SAFE_MODE = 2, + BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM = 3, + BOARDCTRL_RESET_MODE_BOOTLOADER = 4, +}; + typedef struct _boardctrl_state_t { uint8_t reset_mode; bool log_soft_reset; diff --git a/ports/stm32/main.c b/ports/stm32/main.c index bc8e6f0691eef..ca80daaceb5a8 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -156,7 +156,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main); #if MICROPY_HW_FLASH_MOUNT_AT_BOOT // avoid inlining to avoid stack usage within main() MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { - if (reset_mode == 3) { + if (reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { // Asked by user to reset filesystem factory_reset_create_filesystem(); } @@ -210,7 +210,8 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point); - if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) { + if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) + && reset_mode != BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { // No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs), // and didn't already create a filesystem, so try to create a fresh one now. ret = factory_reset_create_filesystem(); diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 4407ca9da9bdf..a77060ab2b47e 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -29,6 +29,7 @@ #include "py/mphal.h" #include "extmod/crypto-algorithms/sha256.c" +#include "boardctrl.h" #include "usbd_core.h" #include "storage.h" #include "flash.h" @@ -1289,7 +1290,7 @@ static int pyb_usbdd_shutdown(void) { static int get_reset_mode(void) { usrbtn_init(); - int reset_mode = 1; + int reset_mode = BOARDCTRL_RESET_MODE_NORMAL; if (usrbtn_state()) { // Cycle through reset modes while USR is held // Timeout is roughly 20s, where reset_mode=1 @@ -1299,7 +1300,7 @@ static int get_reset_mode(void) { for (int i = 0; i < (RESET_MODE_NUM_STATES * RESET_MODE_TIMEOUT_CYCLES + 1) * 32; i++) { if (i % 32 == 0) { if (++reset_mode > RESET_MODE_NUM_STATES) { - reset_mode = 1; + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } uint8_t l = RESET_MODE_LED_STATES >> ((reset_mode - 1) * 4); led_state_all(l); @@ -1396,7 +1397,7 @@ void stm32_main(int initial_r0) { int reset_mode = get_reset_mode(); uint32_t msp = *(volatile uint32_t*)APPLICATION_ADDR; - if (reset_mode != 4 && (msp & APP_VALIDITY_BITS) == 0) { + if (reset_mode != BOARDCTRL_RESET_MODE_BOOTLOADER && (msp & APP_VALIDITY_BITS) == 0) { // not DFU mode so jump to application, passing through reset_mode // undo our DFU settings // TODO probably should disable all IRQ sources first From 885b246ca91592ba48637a8519dd4fbd576ad38a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Apr 2021 23:48:58 +1000 Subject: [PATCH 0776/3533] stm32/boardctrl: Show first reset-mode state on LEDs when selecting. Commit 1e297c88989592258965b69cb740039e26c7636c introduced a bug where the very first reset-mode state on the LEDs was not shown, because prior to that commit the first reset-mode state was the same as the initial LED state (green on, others off) and update_reset_mode() was called after setting this initial LED state. This is fixed in this commit by changing the update_reset_mode() loop so that it displays the current reset mode before doing the delay. Signed-off-by: Damien George --- ports/stm32/boardctrl.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index ce04914ea907c..8cc519d8e9d7d 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -51,20 +51,21 @@ STATIC uint update_reset_mode(uint reset_mode) { // The original method used on the pyboard is appropriate if you have 2 // or more LEDs. #if defined(MICROPY_HW_LED2) - for (uint i = 0; i < 3000; i++) { - if (!switch_get()) { - break; - } - mp_hal_delay_ms(20); - if (i % 30 == 29) { - if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { - reset_mode = BOARDCTRL_RESET_MODE_NORMAL; + for (uint i = 0; i < 100; i++) { + led_state(2, reset_mode & 1); + led_state(3, reset_mode & 2); + led_state(4, reset_mode & 4); + for (uint j = 0; j < 30; ++j) { + mp_hal_delay_ms(20); + if (!switch_get()) { + goto select_mode; } - led_state(2, reset_mode & 1); - led_state(3, reset_mode & 2); - led_state(4, reset_mode & 4); + } + if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } } + select_mode: // flash the selected reset mode for (uint i = 0; i < 6; i++) { led_state(2, 0); From ef2896bdeacdfa3f6dc9faaa3f9c0f3d9bef291b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:10:42 +1000 Subject: [PATCH 0777/3533] stm32/mboot: Allow a board to add source files to the build. A board can now use BUILDING_MBOOT at the Makefile-level to do things conditional on building mboot, for example add source files to SRC_C. Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 0c92442597723..534622c030334 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -23,6 +23,9 @@ ifeq ($(wildcard $(BOARD_DIR)/.),) $(error Invalid BOARD specified: $(BOARD_DIR)) endif +# Enable BUILDING_MBOOT so boards can configure their .mk file accordingly. +BUILDING_MBOOT = 1 + include ../../../py/mkenv.mk include $(BOARD_DIR)/mpconfigboard.mk @@ -78,7 +81,7 @@ CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR) CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT -DLFS2_READONLY -CFLAGS += -DBUILDING_MBOOT=1 +CFLAGS += -DBUILDING_MBOOT=$(BUILDING_MBOOT) CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) @@ -98,7 +101,7 @@ COPT += -Os -DNDEBUG endif $(BUILD)/lib/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN) -LIB_SRC_C = \ +LIB_SRC_C += \ lib/libc/string0.c \ lib/littlefs/lfs1.c \ lib/littlefs/lfs1_util.c \ @@ -111,7 +114,7 @@ LIB_SRC_C = \ extmod/uzlib/tinflate.c \ extmod/uzlib/tinfgzip.c -SRC_C = \ +SRC_C += \ main.c \ elem.c \ fsload.c \ @@ -131,7 +134,7 @@ SRC_C = \ ports/stm32/usbd_conf.c \ $(wildcard $(BOARD_DIR)/*.c) -SRC_O = \ +SRC_O += \ $(STARTUP_FILE) \ $(SYSTEM_FILE) \ ports/stm32/resethandler.o \ @@ -146,7 +149,7 @@ CFLAGS += -DMBOOT_PACK_KEYS_FILE=\"$(MBOOT_PACK_KEYS_FILE)\" endif $(BUILD)/$(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_ll_usb.o: CFLAGS += -Wno-attributes -SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ +SRC_HAL += $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ hal_cortex.c \ hal_flash.c \ hal_flash_ex.c \ @@ -155,13 +158,12 @@ SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\ ll_usb.c \ ) -SRC_USBDEV = $(addprefix ports/stm32/$(USBDEV_DIR)/,\ +SRC_USBDEV += $(addprefix ports/stm32/$(USBDEV_DIR)/,\ core/src/usbd_core.c \ core/src/usbd_ctlreq.c \ core/src/usbd_ioreq.c \ ) -OBJ = OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) From fd01b6c779d39eae1aaa0f038b4ec15d86db44fe Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:12:07 +1000 Subject: [PATCH 0778/3533] stm32/adc: Allow mboot to use basic ADC functions. Signed-off-by: Damien George --- ports/stm32/adc.h | 5 +++++ ports/stm32/machine_adc.c | 8 ++++++-- ports/stm32/mboot/Makefile | 1 + ports/stm32/mboot/mphalport.h | 5 +++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ports/stm32/adc.h b/ports/stm32/adc.h index 6f2e61e099449..64864b19675e4 100644 --- a/ports/stm32/adc.h +++ b/ports/stm32/adc.h @@ -26,8 +26,13 @@ #ifndef MICROPY_INCLUDED_STM32_ADC_H #define MICROPY_INCLUDED_STM32_ADC_H +#if !BUILDING_MBOOT extern const mp_obj_type_t pyb_adc_type; extern const mp_obj_type_t pyb_adc_all_type; +#endif + +void adc_config(ADC_TypeDef *adc, uint32_t bits); +uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint32_t sample_time); #if defined(ADC_CHANNEL_VBAT) diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index d9e5a64da5533..f28bd5b398670 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -96,7 +96,7 @@ STATIC const uint8_t adc_cr_to_bits_table[] = {16, 14, 12, 10, 8, 8, 8, 8}; STATIC const uint8_t adc_cr_to_bits_table[] = {12, 10, 8, 6}; #endif -STATIC void adc_config(ADC_TypeDef *adc, uint32_t bits) { +void adc_config(ADC_TypeDef *adc, uint32_t bits) { // Configure ADC clock source and enable ADC clock #if defined(STM32L4) || defined(STM32WB) __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); @@ -331,7 +331,7 @@ STATIC uint32_t adc_read_channel(ADC_TypeDef *adc) { return value; } -STATIC uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint32_t sample_time) { +uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint32_t sample_time) { if (channel == ADC_CHANNEL_VREF) { return 0xffff; } @@ -357,6 +357,8 @@ STATIC uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint /******************************************************************************/ // MicroPython bindings for machine.ADC +#if !BUILDING_MBOOT + const mp_obj_type_t machine_adc_type; typedef struct _machine_adc_obj_t { @@ -462,3 +464,5 @@ const mp_obj_type_t machine_adc_type = { .make_new = machine_adc_make_new, .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, }; + +#endif diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 534622c030334..d57b9368ee846 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -128,6 +128,7 @@ SRC_C += \ ports/stm32/flash.c \ ports/stm32/flashbdev.c \ ports/stm32/i2cslave.c \ + ports/stm32/machine_adc.c \ ports/stm32/powerctrlboot.c \ ports/stm32/qspi.c \ ports/stm32/spibdev.c \ diff --git a/ports/stm32/mboot/mphalport.h b/ports/stm32/mboot/mphalport.h index 56d18a9b05d1d..ab0bc1587cda9 100644 --- a/ports/stm32/mboot/mphalport.h +++ b/ports/stm32/mboot/mphalport.h @@ -39,6 +39,11 @@ static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) { #define MP_HAL_PIN_MODE_OUTPUT (1) #define MP_HAL_PIN_MODE_ALT (2) #define MP_HAL_PIN_MODE_ANALOG (3) +#if defined(GPIO_ASCR_ASC0) +#define MP_HAL_PIN_MODE_ADC (11) +#else +#define MP_HAL_PIN_MODE_ADC (3) +#endif #define MP_HAL_PIN_MODE_OPEN_DRAIN (5) #define MP_HAL_PIN_MODE_ALT_OPEN_DRAIN (6) #define MP_HAL_PIN_PULL_NONE (GPIO_NOPULL) From 97f09fda3e00b4da11bed3d7fb3d83d89deeeafb Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:21:38 +1000 Subject: [PATCH 0779/3533] stm32/mboot: Fix mp_hal_delay_us() and add mp_hal_ticks_ms(). Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index a77060ab2b47e..c82e460faf642 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -95,6 +95,9 @@ // These bits are used to detect valid application firmware at APPLICATION_ADDR #define APP_VALIDITY_BITS (0x00000003) +// For 1ms system ticker. +static volatile uint32_t systick_ms; + // Global dfu state dfu_context_t dfu_context SECTION_NOZERO_BSS; @@ -104,6 +107,10 @@ uint32_t get_le32(const uint8_t *b) { return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24; } +mp_uint_t mp_hal_ticks_ms(void) { + return systick_ms; +} + void mp_hal_delay_us(mp_uint_t usec) { // use a busy loop for the delay // sys freq is always a multiple of 2MHz, so division here won't lose precision @@ -113,11 +120,10 @@ void mp_hal_delay_us(mp_uint_t usec) { const uint32_t ucount = SystemCoreClock / 2000000 * usec / 2; #endif for (uint32_t count = 0; ++count <= ucount;) { + __NOP(); } } -static volatile uint32_t systick_ms; - void mp_hal_delay_ms(mp_uint_t ms) { if (__get_PRIMASK() == 0) { // IRQs enabled, use systick From 58be5a5aa328e687a3560d4a85fb069400b0b8ca Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:26:20 +1000 Subject: [PATCH 0780/3533] stm32/mboot: Allow a board to customise the linker scripts. A board can now define MBOOT_LD_FILES (at the Makefile-level) to specify custom linker scripts. And stm32_generic.ld has been split into 2 pieces so one or the other can be reused (usually stm32_sections.ld wolud be reused by a board, and stm32_memory.ld redefined). Signed-off-by: Damien George --- ports/stm32/mboot/Makefile | 3 ++- ports/stm32/mboot/stm32_memory.ld | 10 ++++++++++ .../mboot/{stm32_generic.ld => stm32_sections.ld} | 10 ++-------- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 ports/stm32/mboot/stm32_memory.ld rename ports/stm32/mboot/{stm32_generic.ld => stm32_sections.ld} (87%) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index d57b9368ee846..de090da1c6a03 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -85,7 +85,8 @@ CFLAGS += -DBUILDING_MBOOT=$(BUILDING_MBOOT) CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) -LDFLAGS = -nostdlib -L . -T stm32_generic.ld -Map=$(@:.elf=.map) --cref +MBOOT_LD_FILES ?= stm32_memory.ld stm32_sections.ld +LDFLAGS = -nostdlib -L . $(addprefix -T,$(MBOOT_LD_FILES)) -Map=$(@:.elf=.map) --cref LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Remove uncalled code from the final image. diff --git a/ports/stm32/mboot/stm32_memory.ld b/ports/stm32/mboot/stm32_memory.ld new file mode 100644 index 0000000000000..cfcac4096b6d1 --- /dev/null +++ b/ports/stm32/mboot/stm32_memory.ld @@ -0,0 +1,10 @@ +/* + Linker script fragment for mboot on an STM32xxx MCU. + This defines the memory sections for the bootloader to use. +*/ + +MEMORY +{ + FLASH_BL (rx) : ORIGIN = 0x08000000, LENGTH = 32K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 120K +} diff --git a/ports/stm32/mboot/stm32_generic.ld b/ports/stm32/mboot/stm32_sections.ld similarity index 87% rename from ports/stm32/mboot/stm32_generic.ld rename to ports/stm32/mboot/stm32_sections.ld index ade43496745eb..3302c5f97291d 100644 --- a/ports/stm32/mboot/stm32_generic.ld +++ b/ports/stm32/mboot/stm32_sections.ld @@ -1,14 +1,8 @@ /* - GNU linker script for generic STM32xxx MCU + Linker script fragment for mboot on an STM32xxx MCU. + This needs the following MEMORY sections to be defined: FLASH_BL, RAM. */ -/* Specify the memory areas */ -MEMORY -{ - FLASH_BL (rx) : ORIGIN = 0x08000000, LENGTH = 32K /* sector 0 (can be 32K) */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 120K -} - /* produce a link error if there is not this amount of RAM for these sections */ _minimum_stack_size = 8K; From 37494b8c8a3e61612081aa737997201e7b24b536 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:45:51 +1000 Subject: [PATCH 0781/3533] stm32/mboot: Allow mboot to be placed at any location in flash. A board can now define MBOOT_TEXT0_ADDR to place mboot at a location other than 0x08000000. This can be useful if, for example, there is already a different bootloader on the device. Signed-off-by: Damien George --- ports/stm32/Makefile | 5 +++++ ports/stm32/mboot/Makefile | 10 ++++++---- ports/stm32/mboot/main.c | 2 +- ports/stm32/modmachine.c | 6 +++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index e7a8f0aee62d3..e94e7d72f363f 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -27,6 +27,10 @@ MICROPY_ROM_TEXT_COMPRESSION ?= 1 # File containing description of content to be frozen into firmware. FROZEN_MANIFEST ?= boards/manifest.py +# Location of mboot (or other bootloader) if the device has one. +# Used by machine.bootloader(). +MBOOT_TEXT0_ADDR ?= 0x08000000 + # include py core make definitions include $(TOP)/py/py.mk @@ -101,6 +105,7 @@ CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) CFLAGS += -I$(BOARD_DIR) CFLAGS += -DSTM32_HAL_H='' +CFLAGS += -DMBOOT_VTOR=$(MBOOT_TEXT0_ADDR) CFLAGS += -DMICROPY_HW_VTOR=$(TEXT0_ADDR) # Configure for nan-boxing object model if requested diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index de090da1c6a03..17de685a61cea 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -29,6 +29,9 @@ BUILDING_MBOOT = 1 include ../../../py/mkenv.mk include $(BOARD_DIR)/mpconfigboard.mk +# A board can set MBOOT_TEXT0_ADDR to a custom location where mboot should reside. +MBOOT_TEXT0_ADDR ?= 0x08000000 + MCU_SERIES_UPPER = $(shell echo $(MCU_SERIES) | tr '[:lower:]' '[:upper:]') CMSIS_MCU_LOWER = $(shell echo $(CMSIS_MCU) | tr '[:upper:]' '[:lower:]') @@ -77,6 +80,7 @@ CFLAGS += $(COPT) CFLAGS += -I$(BOARD_DIR) CFLAGS += -DSTM32_HAL_H='' CFLAGS += -DBOARD_$(BOARD) +CFLAGS += -DMBOOT_VTOR=$(MBOOT_TEXT0_ADDR) CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR) CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT @@ -181,20 +185,18 @@ $(TOP)/lib/stm32lib/README.md: .PHONY: deploy deploy-stlink -FLASH_ADDR = 0x08000000 - deploy: $(BUILD)/firmware.dfu $(ECHO) "Writing $< to the board" $(Q)$(PYTHON) $(PYDFU) -u $< deploy-stlink: $(BUILD)/firmware.dfu $(ECHO) "Writing $< to the board via ST-LINK" - $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(FLASH_ADDR) + $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(MBOOT_TEXT0_ADDR) $(BUILD)/firmware.dfu: $(BUILD)/firmware.elf $(ECHO) "Create $@" $(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data $^ $(BUILD)/firmware.bin - $(Q)$(PYTHON) $(DFU) -b $(FLASH_ADDR):$(BUILD)/firmware.bin $@ + $(Q)$(PYTHON) $(DFU) -b $(MBOOT_TEXT0_ADDR):$(BUILD)/firmware.bin $@ $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(ECHO) "Create $@" diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index c82e460faf642..36b6890361572 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -1358,7 +1358,7 @@ void stm32_main(int initial_r0) { #endif // Make sure IRQ vector table points to flash where this bootloader lives. - SCB->VTOR = FLASH_BASE; + SCB->VTOR = MBOOT_VTOR; // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI SCB->CCR |= SCB_CCR_STKALIGN_Msk; diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index f9fd1d9a64d77..aee563ee881d1 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -270,16 +270,16 @@ STATIC NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args) #if MICROPY_HW_USES_BOOTLOADER if (n_args == 0 || !mp_obj_is_true(args[0])) { // By default, with no args given, we enter the custom bootloader (mboot) - powerctrl_enter_bootloader(0x70ad0000, 0x08000000); + powerctrl_enter_bootloader(0x70ad0000, MBOOT_VTOR); } if (n_args == 1 && mp_obj_is_str_or_bytes(args[0])) { // With a string/bytes given, pass its data to the custom bootloader size_t len; const char *data = mp_obj_str_get_data(args[0], &len); - void *mboot_region = (void *)*((volatile uint32_t *)0x08000000); + void *mboot_region = (void *)*((volatile uint32_t *)MBOOT_VTOR); memmove(mboot_region, data, len); - powerctrl_enter_bootloader(0x70ad0080, 0x08000000); + powerctrl_enter_bootloader(0x70ad0080, MBOOT_VTOR); } #endif From cf7e71fa4377e30c8612fa19032ae3004670c7e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Apr 2021 12:08:20 +1000 Subject: [PATCH 0782/3533] stm32/sdcard: Allow configuring the SDMMC periph used for SD/MMC card. This can now be selected by setting MICROPY_HW_SDCARD_SDMMC, which defaults to 1, ie SDMMC1. This commit also renames the SD pin configuration macros from MICROPY_HW_SDMMC2_xxx to MICROPY_HW_SDCARD_xxx, as well as renaming MICROPY_HW_SDMMC_BUS_WIDTH to MICROPY_HW_SDCARD_BUS_WIDTH. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 13 +-- .../boards/STM32F769DISC/mpconfigboard.h | 15 +-- ports/stm32/mpconfigboard_common.h | 20 +++- ports/stm32/sdcard.c | 97 ++++++++----------- 4 files changed, 75 insertions(+), 70 deletions(-) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index dc42751fffe87..7d89e17cf46d2 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -165,12 +165,13 @@ extern struct _spi_bdev_t spi_bdev2; #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) // SD card -#define MICROPY_HW_SDMMC2_CK (pyb_pin_SD_CK) -#define MICROPY_HW_SDMMC2_CMD (pyb_pin_SD_CMD) -#define MICROPY_HW_SDMMC2_D0 (pyb_pin_SD_D0) -#define MICROPY_HW_SDMMC2_D1 (pyb_pin_SD_D1) -#define MICROPY_HW_SDMMC2_D2 (pyb_pin_SD_D2) -#define MICROPY_HW_SDMMC2_D3 (pyb_pin_SD_D3) +#define MICROPY_HW_SDCARD_SDMMC (2) +#define MICROPY_HW_SDCARD_CK (pyb_pin_SD_CK) +#define MICROPY_HW_SDCARD_CMD (pyb_pin_SD_CMD) +#define MICROPY_HW_SDCARD_D0 (pyb_pin_SD_D0) +#define MICROPY_HW_SDCARD_D1 (pyb_pin_SD_D1) +#define MICROPY_HW_SDCARD_D2 (pyb_pin_SD_D2) +#define MICROPY_HW_SDCARD_D3 (pyb_pin_SD_D3) #define MICROPY_HW_SDCARD_DETECT_PIN (pyb_pin_SD_SW) #define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) #define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET) diff --git a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h index 843b987cee714..5004a8ecac9d0 100644 --- a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h @@ -90,13 +90,14 @@ extern struct _spi_bdev_t spi_bdev; #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) -// SD card detect switch -#define MICROPY_HW_SDMMC2_CK (pin_D6) -#define MICROPY_HW_SDMMC2_CMD (pin_D7) -#define MICROPY_HW_SDMMC2_D0 (pin_G9) -#define MICROPY_HW_SDMMC2_D1 (pin_G10) -#define MICROPY_HW_SDMMC2_D2 (pin_B3) -#define MICROPY_HW_SDMMC2_D3 (pin_B4) +// SD card +#define MICROPY_HW_SDCARD_SDMMC (2) +#define MICROPY_HW_SDCARD_CK (pin_D6) +#define MICROPY_HW_SDCARD_CMD (pin_D7) +#define MICROPY_HW_SDCARD_D0 (pin_G9) +#define MICROPY_HW_SDCARD_D1 (pin_G10) +#define MICROPY_HW_SDCARD_D2 (pin_B3) +#define MICROPY_HW_SDCARD_D3 (pin_B4) #define MICROPY_HW_SDCARD_DETECT_PIN (pin_I15) #define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) #define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 6fb9289e00dca..37ce8bbf197ef 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -107,9 +107,14 @@ #define MICROPY_HW_ENABLE_MMCARD (0) #endif -// SD/MMC interface bus width (defaults to 4 bits) -#ifndef MICROPY_HW_SDMMC_BUS_WIDTH -#define MICROPY_HW_SDMMC_BUS_WIDTH (4) +// Which SDMMC peripheral to use for the SD/MMC card driver (1 or 2) +#ifndef MICROPY_HW_SDCARD_SDMMC +#define MICROPY_HW_SDCARD_SDMMC (1) +#endif + +// SD/MMC card driver interface bus width (defaults to 4 bits) +#ifndef MICROPY_HW_SDCARD_BUS_WIDTH +#define MICROPY_HW_SDCARD_BUS_WIDTH (4) #endif // Whether to automatically mount (and boot from) the SD card if it's present @@ -383,6 +388,15 @@ #define MICROPY_HW_MAX_CAN (1) #endif +// Define MICROPY_HW_SDMMCx_CK values if that peripheral is used, so that make-pins.py +// generates the relevant AF constants. +#if MICROPY_HW_SDCARD_SDMMC == 1 +#define MICROPY_HW_SDMMC1_CK (1) +#endif +#if MICROPY_HW_SDCARD_SDMMC == 2 +#define MICROPY_HW_SDMMC2_CK (1) +#endif + // Whether the USB peripheral is device-only, or multiple OTG #if defined(STM32L0) || defined(STM32L432xx) || defined(STM32WB) #define MICROPY_HW_USB_IS_MULTI_OTG (0) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index b255ee82ca9be..ea69e7285b38a 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -42,30 +42,39 @@ #if defined(STM32F7) || defined(STM32H7) || defined(STM32L4) -// The F7 has 2 SDMMC units but at the moment we only support using one of them in -// a given build. If a boards config file defines MICROPY_HW_SDMMC2_CK then SDMMC2 -// is used, otherwise SDMMC1 is used. +// The H7/F7/L4 have 2 SDMMC peripherals, but at the moment this driver only supports +// using one of them in a given build, selected by MICROPY_HW_SDCARD_SDMMC. -#if defined(MICROPY_HW_SDMMC2_CK) +#if MICROPY_HW_SDCARD_SDMMC == 2 #define SDIO SDMMC2 #define SDMMC_IRQHandler SDMMC2_IRQHandler #define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC2_CLK_ENABLE() #define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC2_CLK_DISABLE() +#define SDMMC_FORCE_RESET() __HAL_RCC_SDMMC2_FORCE_RESET() +#define SDMMC_RELEASE_RESET() __HAL_RCC_SDMMC2_RELEASE_RESET() #define SDMMC_IRQn SDMMC2_IRQn #define SDMMC_DMA dma_SDMMC_2 +#define STATIC_AF_SDCARD_CK STATIC_AF_SDMMC2_CK +#define STATIC_AF_SDCARD_CMD STATIC_AF_SDMMC2_CMD +#define STATIC_AF_SDCARD_D0 STATIC_AF_SDMMC2_D0 +#define STATIC_AF_SDCARD_D1 STATIC_AF_SDMMC2_D1 +#define STATIC_AF_SDCARD_D2 STATIC_AF_SDMMC2_D2 +#define STATIC_AF_SDCARD_D3 STATIC_AF_SDMMC2_D3 #else #define SDIO SDMMC1 #define SDMMC_IRQHandler SDMMC1_IRQHandler #define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC1_CLK_ENABLE() #define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC1_CLK_DISABLE() +#define SDMMC_FORCE_RESET() __HAL_RCC_SDMMC1_FORCE_RESET() +#define SDMMC_RELEASE_RESET() __HAL_RCC_SDMMC1_RELEASE_RESET() #define SDMMC_IRQn SDMMC1_IRQn #define SDMMC_DMA dma_SDIO_0 -#define STATIC_AF_SDMMC_CK STATIC_AF_SDMMC1_CK -#define STATIC_AF_SDMMC_CMD STATIC_AF_SDMMC1_CMD -#define STATIC_AF_SDMMC_D0 STATIC_AF_SDMMC1_D0 -#define STATIC_AF_SDMMC_D1 STATIC_AF_SDMMC1_D1 -#define STATIC_AF_SDMMC_D2 STATIC_AF_SDMMC1_D2 -#define STATIC_AF_SDMMC_D3 STATIC_AF_SDMMC1_D3 +#define STATIC_AF_SDCARD_CK STATIC_AF_SDMMC1_CK +#define STATIC_AF_SDCARD_CMD STATIC_AF_SDMMC1_CMD +#define STATIC_AF_SDCARD_D0 STATIC_AF_SDMMC1_D0 +#define STATIC_AF_SDCARD_D1 STATIC_AF_SDMMC1_D1 +#define STATIC_AF_SDCARD_D2 STATIC_AF_SDMMC1_D2 +#define STATIC_AF_SDCARD_D3 STATIC_AF_SDMMC1_D3 #endif // The F7 & L4 series calls the peripheral SDMMC rather than SDIO, so provide some @@ -105,25 +114,23 @@ #define SDMMC_IRQHandler SDIO_IRQHandler #define SDMMC_DMA dma_SDIO_0 #define SDIO_USE_GPDMA 1 -#define STATIC_AF_SDMMC_CK STATIC_AF_SDIO_CK -#define STATIC_AF_SDMMC_CMD STATIC_AF_SDIO_CMD -#define STATIC_AF_SDMMC_D0 STATIC_AF_SDIO_D0 -#define STATIC_AF_SDMMC_D1 STATIC_AF_SDIO_D1 -#define STATIC_AF_SDMMC_D2 STATIC_AF_SDIO_D2 -#define STATIC_AF_SDMMC_D3 STATIC_AF_SDIO_D3 +#define STATIC_AF_SDCARD_CK STATIC_AF_SDIO_CK +#define STATIC_AF_SDCARD_CMD STATIC_AF_SDIO_CMD +#define STATIC_AF_SDCARD_D0 STATIC_AF_SDIO_D0 +#define STATIC_AF_SDCARD_D1 STATIC_AF_SDIO_D1 +#define STATIC_AF_SDCARD_D2 STATIC_AF_SDIO_D2 +#define STATIC_AF_SDCARD_D3 STATIC_AF_SDIO_D3 #endif // If no custom SDIO pins defined, use the default ones -#ifndef MICROPY_HW_SDMMC_CK - -#define MICROPY_HW_SDMMC_D0 (pin_C8) -#define MICROPY_HW_SDMMC_D1 (pin_C9) -#define MICROPY_HW_SDMMC_D2 (pin_C10) -#define MICROPY_HW_SDMMC_D3 (pin_C11) -#define MICROPY_HW_SDMMC_CK (pin_C12) -#define MICROPY_HW_SDMMC_CMD (pin_D2) - +#ifndef MICROPY_HW_SDCARD_CK +#define MICROPY_HW_SDCARD_D0 (pin_C8) +#define MICROPY_HW_SDCARD_D1 (pin_C9) +#define MICROPY_HW_SDCARD_D2 (pin_C10) +#define MICROPY_HW_SDCARD_D3 (pin_C11) +#define MICROPY_HW_SDCARD_CK (pin_C12) +#define MICROPY_HW_SDCARD_CMD (pin_D2) #endif #define PYB_SDMMC_FLAG_SD (0x01) @@ -152,26 +159,13 @@ void sdcard_init(void) { // Note: the mp_hal_pin_config function will configure the GPIO in // fast mode which can do up to 50MHz. This should be plenty for SDIO // which clocks up to 25MHz maximum. - #if defined(MICROPY_HW_SDMMC2_CK) - // Use SDMMC2 peripheral with pins provided by the board's config - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_CK); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_CMD); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D0); - #if MICROPY_HW_SDMMC_BUS_WIDTH == 4 - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D1); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D2); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D3); - #endif - #else - // Default SDIO/SDMMC1 config - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CK); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CMD); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D0); - #if MICROPY_HW_SDMMC_BUS_WIDTH == 4 - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D1); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D2); - mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D3); - #endif + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_CK); + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_CMD); + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D0); + #if MICROPY_HW_SDCARD_BUS_WIDTH == 4 + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D1); + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D2); + mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D3); #endif // configure the SD card detect pin @@ -187,13 +181,8 @@ STATIC void sdmmc_msp_init(void) { #if defined(STM32H7) // Reset SDMMC - #if defined(MICROPY_HW_SDMMC2_CK) - __HAL_RCC_SDMMC2_FORCE_RESET(); - __HAL_RCC_SDMMC2_RELEASE_RESET(); - #else - __HAL_RCC_SDMMC1_FORCE_RESET(); - __HAL_RCC_SDMMC1_RELEASE_RESET(); - #endif + SDMMC_FORCE_RESET(); + SDMMC_RELEASE_RESET(); #endif // NVIC configuration for SDIO interrupts @@ -263,7 +252,7 @@ STATIC HAL_StatusTypeDef sdmmc_init_sd(void) { mp_hal_delay_ms(50); } - #if MICROPY_HW_SDMMC_BUS_WIDTH == 4 + #if MICROPY_HW_SDCARD_BUS_WIDTH == 4 // configure the SD bus width for 4-bit wide operation status = HAL_SD_ConfigWideBusOperation(&sdmmc_handle.sd, SDIO_BUS_WIDE_4B); if (status != HAL_OK) { @@ -298,7 +287,7 @@ STATIC HAL_StatusTypeDef sdmmc_init_mmc(void) { // As this is an eMMC card, overwrite LogBlockNbr with actual value sdmmc_handle.mmc.MmcCard.LogBlockNbr = 7469056 + 2048; - #if MICROPY_HW_SDMMC_BUS_WIDTH == 4 + #if MICROPY_HW_SDCARD_BUS_WIDTH == 4 // Configure the SDIO bus width for 4-bit wide operation #ifdef STM32F7 sdmmc_handle.mmc.Init.ClockBypass = SDIO_CLOCK_BYPASS_ENABLE; From 4d967868238e3c359187921edab8e6fc164f357d Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 20:49:29 +0200 Subject: [PATCH 0783/3533] stm32/uart: Enable HW flow control for UART 1/5/7/8. --- ports/stm32/uart.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index babdf63d96b35..a2b69967b6e41 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -223,6 +223,16 @@ bool uart_init(pyb_uart_obj_t *uart_obj, irqn = USART1_IRQn; pins[0] = MICROPY_HW_UART1_TX; pins[1] = MICROPY_HW_UART1_RX; + #if defined(MICROPY_HW_UART1_RTS) + if (flow & UART_HWCONTROL_RTS) { + pins[2] = MICROPY_HW_UART1_RTS; + } + #endif + #if defined(MICROPY_HW_UART1_CTS) + if (flow & UART_HWCONTROL_CTS) { + pins[3] = MICROPY_HW_UART1_CTS; + } + #endif __HAL_RCC_USART1_CLK_ENABLE(); break; #endif @@ -322,6 +332,16 @@ bool uart_init(pyb_uart_obj_t *uart_obj, #endif pins[0] = MICROPY_HW_UART5_TX; pins[1] = MICROPY_HW_UART5_RX; + #if defined(MICROPY_HW_UART5_RTS) + if (flow & UART_HWCONTROL_RTS) { + pins[2] = MICROPY_HW_UART5_RTS; + } + #endif + #if defined(MICROPY_HW_UART5_CTS) + if (flow & UART_HWCONTROL_CTS) { + pins[3] = MICROPY_HW_UART5_CTS; + } + #endif break; #endif @@ -364,6 +384,16 @@ bool uart_init(pyb_uart_obj_t *uart_obj, #endif pins[0] = MICROPY_HW_UART7_TX; pins[1] = MICROPY_HW_UART7_RX; + #if defined(MICROPY_HW_UART7_RTS) + if (flow & UART_HWCONTROL_RTS) { + pins[2] = MICROPY_HW_UART7_RTS; + } + #endif + #if defined(MICROPY_HW_UART7_CTS) + if (flow & UART_HWCONTROL_CTS) { + pins[3] = MICROPY_HW_UART7_CTS; + } + #endif break; #endif @@ -381,6 +411,16 @@ bool uart_init(pyb_uart_obj_t *uart_obj, #endif pins[0] = MICROPY_HW_UART8_TX; pins[1] = MICROPY_HW_UART8_RX; + #if defined(MICROPY_HW_UART8_RTS) + if (flow & UART_HWCONTROL_RTS) { + pins[2] = MICROPY_HW_UART8_RTS; + } + #endif + #if defined(MICROPY_HW_UART8_CTS) + if (flow & UART_HWCONTROL_CTS) { + pins[3] = MICROPY_HW_UART8_CTS; + } + #endif break; #endif From 80788154b31c6097fd152e6e2e971319e8e3da1e Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 21:32:57 +0200 Subject: [PATCH 0784/3533] stm32/sdio: Add functions to re/enable SDIO/SDIOIT. --- ports/stm32/sdio.c | 15 +++++++++++++++ ports/stm32/sdio.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/ports/stm32/sdio.c b/ports/stm32/sdio.c index 87f550e3a6f5d..3ebd4947f448d 100644 --- a/ports/stm32/sdio.c +++ b/ports/stm32/sdio.c @@ -93,6 +93,21 @@ void sdio_deinit(void) { #endif } +void sdio_reenable(void) { + if (__HAL_RCC_SDMMC1_IS_CLK_DISABLED()) { + __HAL_RCC_SDMMC1_CLK_ENABLE(); // enable SDIO peripheral + sdio_enable_high_speed_4bit(); + } +} + +void sdio_enable_irq(bool enable) { + if (enable) { + SDMMC1->MASK |= SDMMC_MASK_SDIOITIE; + } else { + SDMMC1->MASK &= ~SDMMC_MASK_SDIOITIE; + } +} + void sdio_enable_high_speed_4bit(void) { SDMMC_TypeDef *SDIO = SDMMC1; SDIO->POWER = 0; // power off diff --git a/ports/stm32/sdio.h b/ports/stm32/sdio.h index e7115e6f80c65..7cbf024ca8836 100644 --- a/ports/stm32/sdio.h +++ b/ports/stm32/sdio.h @@ -31,6 +31,8 @@ void sdio_init(uint32_t irq_pri); void sdio_deinit(void); +void sdio_reenable(void); +void sdio_enable_irq(bool enable); void sdio_enable_high_speed_4bit(void); int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp); int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf); From d3eb6d68a377c42bd37e609ce667bea815fecd4f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 21:35:10 +0200 Subject: [PATCH 0785/3533] drivers/cyw43/cyw43_ctrl: Use new sdio enable API functions. This removes any references to a specific SDMMC instance, making the driver more generic/portable. --- drivers/cyw43/cyw43_ctrl.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/cyw43/cyw43_ctrl.c b/drivers/cyw43/cyw43_ctrl.c index cc1fbecde8a4e..aaa266b0d8527 100644 --- a/drivers/cyw43/cyw43_ctrl.c +++ b/drivers/cyw43/cyw43_ctrl.c @@ -112,7 +112,7 @@ void cyw43_deinit(cyw43_t *self) { self->itf_state = 0; // Disable async polling - SDMMC1->MASK &= ~SDMMC_MASK_SDIOITIE; + sdio_enable_irq(false); cyw43_poll = NULL; #ifdef pyb_pin_WL_RFSW_VDD @@ -164,7 +164,7 @@ STATIC int cyw43_ensure_up(cyw43_t *self) { cyw43_sleep = CYW43_SLEEP_MAX; cyw43_poll = cyw43_poll_func; #if USE_SDIOIT - SDMMC1->MASK |= SDMMC_MASK_SDIOITIE; + sdio_enable_irq(true); #else extern void extint_set(const pin_obj_t *pin, uint32_t mode); extint_set(pyb_pin_WL_HOST_WAKE, GPIO_MODE_IT_FALLING); @@ -209,7 +209,7 @@ STATIC void cyw43_poll_func(void) { } #if USE_SDIOIT - SDMMC1->MASK |= SDMMC_MASK_SDIOITIE; + sdio_enable_irq(true); #endif } @@ -227,10 +227,7 @@ int cyw43_cb_read_host_interrupt_pin(void *cb_data) { void cyw43_cb_ensure_awake(void *cb_data) { cyw43_sleep = CYW43_SLEEP_MAX; #if !USE_SDIOIT - if (__HAL_RCC_SDMMC1_IS_CLK_DISABLED()) { - __HAL_RCC_SDMMC1_CLK_ENABLE(); // enable SDIO peripheral - sdio_enable_high_speed_4bit(); - } + sdio_reenable(); #endif } From d74e2aca3e30ff1d424e97ac0d37d07694d69b7d Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 20:09:34 +0200 Subject: [PATCH 0786/3533] drivers/cyw43/cywbt: Add compile option for RF switch. --- drivers/cyw43/cywbt.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/cyw43/cywbt.c b/drivers/cyw43/cywbt.c index defe670683ffd..c4fecca81b9ab 100644 --- a/drivers/cyw43/cywbt.c +++ b/drivers/cyw43/cywbt.c @@ -149,10 +149,14 @@ STATIC int cywbt_download_firmware(const uint8_t *firmware) { } // RF switch must select high path during BT patch boot + #if MICROPY_HW_ENABLE_RF_SWITCH mp_hal_pin_config(pyb_pin_WL_GPIO_1, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_UP, 0); + #endif mp_hal_delay_ms(10); // give some time for CTS to go high cywbt_wait_cts_low(); + #if MICROPY_HW_ENABLE_RF_SWITCH mp_hal_pin_config(pyb_pin_WL_GPIO_1, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_DOWN, 0); // Select chip antenna (could also select external) + #endif mp_bluetooth_hci_uart_set_baudrate(115200); cywbt_set_baudrate(3000000); @@ -170,9 +174,11 @@ int mp_bluetooth_hci_controller_init(void) { mp_hal_pin_output(pyb_pin_BT_DEV_WAKE); mp_hal_pin_low(pyb_pin_BT_DEV_WAKE); + #if MICROPY_HW_ENABLE_RF_SWITCH // TODO don't select antenna if wifi is enabled mp_hal_pin_config(pyb_pin_WL_GPIO_4, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0); // RF-switch power mp_hal_pin_high(pyb_pin_WL_GPIO_4); // Turn the RF-switch on + #endif uint8_t buf[256]; From baa712b7f093fdeec3ddd4122be9e2f171d35a33 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 20:10:30 +0200 Subject: [PATCH 0787/3533] stm32/boards/PYBD_SF2: Enable RF switch compile option. --- ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 7d89e17cf46d2..57cc75efc6f92 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -39,6 +39,7 @@ #define MICROPY_HW_ENABLE_USB (1) #define MICROPY_HW_ENABLE_SDCARD (1) #define MICROPY_HW_ENABLE_MMCARD (1) +#define MICROPY_HW_ENABLE_RF_SWITCH (1) #define MICROPY_BOARD_EARLY_INIT board_early_init #define MICROPY_BOARD_ENTER_STOP board_sleep(1); From 0d4eb15392dfeee8c2113e46eb21f27800872524 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 28 Apr 2021 20:11:48 +0200 Subject: [PATCH 0788/3533] drivers/cyw43/cywbt: Remove hard-coded UART6 alternate function setting. --- drivers/cyw43/cywbt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/cyw43/cywbt.c b/drivers/cyw43/cywbt.c index c4fecca81b9ab..64aeb871c38bb 100644 --- a/drivers/cyw43/cywbt.c +++ b/drivers/cyw43/cywbt.c @@ -55,7 +55,7 @@ STATIC void cywbt_wait_cts_low(void) { } mp_hal_delay_ms(1); } - mp_hal_pin_config_alt_static(pyb_pin_BT_CTS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_USART6_CTS); + mp_hal_pin_config_alt(pyb_pin_BT_CTS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, AF_FN_UART, mp_bluetooth_hci_uart_obj.uart_id); } STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) { From a1111b83ed00542b4f5651d3aae9d38ecbbb6dd6 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 29 Apr 2021 23:18:54 +0200 Subject: [PATCH 0789/3533] stm32/sdio: Allow configuring the SDMMC periph used for SDIO. This can now be selected by setting MICROPY_HW_SDIO_SDMMC, which defaults to 1, ie SDMMC1. The pins can also be selected and default to the standard C8/C9/C10/C11/C12/D2. --- ports/stm32/mpconfigboard_common.h | 9 +- ports/stm32/sdio.c | 195 ++++++++++++++++++----------- 2 files changed, 127 insertions(+), 77 deletions(-) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 37ce8bbf197ef..0df35918bef94 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -122,6 +122,11 @@ #define MICROPY_HW_SDCARD_MOUNT_AT_BOOT (MICROPY_HW_ENABLE_SDCARD) #endif +// Which SDMMC peripheral to use for the SDIO driver (1 or 2) +#ifndef MICROPY_HW_SDIO_SDMMC +#define MICROPY_HW_SDIO_SDMMC (1) +#endif + // Whether to enable the MMA7660 driver, exposed as pyb.Accel #ifndef MICROPY_HW_HAS_MMA7660 #define MICROPY_HW_HAS_MMA7660 (0) @@ -390,10 +395,10 @@ // Define MICROPY_HW_SDMMCx_CK values if that peripheral is used, so that make-pins.py // generates the relevant AF constants. -#if MICROPY_HW_SDCARD_SDMMC == 1 +#if MICROPY_HW_SDCARD_SDMMC == 1 || MICROPY_HW_SDIO_SDMMC == 1 #define MICROPY_HW_SDMMC1_CK (1) #endif -#if MICROPY_HW_SDCARD_SDMMC == 2 +#if MICROPY_HW_SDCARD_SDMMC == 2 || MICROPY_HW_SDIO_SDMMC == 2 #define MICROPY_HW_SDMMC2_CK (1) #endif diff --git a/ports/stm32/sdio.c b/ports/stm32/sdio.c index 3ebd4947f448d..dfd409257759a 100644 --- a/ports/stm32/sdio.c +++ b/ports/stm32/sdio.c @@ -28,7 +28,8 @@ #include "py/mperrno.h" #include "py/mphal.h" -#include "genhdr/pins.h" +#include "pin.h" +#include "pin_static_af.h" #include "pendsv.h" #include "sdio.h" @@ -50,18 +51,62 @@ static volatile uint32_t sdmmc_error; static volatile uint8_t *sdmmc_buf_cur; static volatile uint8_t *sdmmc_buf_top; +// The H7/F7/L4 have 2 SDMMC peripherals, but at the moment this driver only supports +// using one of them in a given build, selected by MICROPY_HW_SDIO_SDMMC. + +#if MICROPY_HW_SDIO_SDMMC == 1 +#define SDMMC SDMMC1 +#define SDMMC_IRQn SDMMC1_IRQn +#define SDMMC_IRQHandler SDMMC1_IRQHandler +#define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC1_CLK_ENABLE() +#define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC1_CLK_DISABLE() +#define SDMMC_IS_CLK_DISABLED() __HAL_RCC_SDMMC1_IS_CLK_DISABLED() +#define STATIC_AF_SDMMC_CK STATIC_AF_SDMMC1_CK +#define STATIC_AF_SDMMC_CMD STATIC_AF_SDMMC1_CMD +#define STATIC_AF_SDMMC_D0 STATIC_AF_SDMMC1_D0 +#define STATIC_AF_SDMMC_D1 STATIC_AF_SDMMC1_D1 +#define STATIC_AF_SDMMC_D2 STATIC_AF_SDMMC1_D2 +#define STATIC_AF_SDMMC_D3 STATIC_AF_SDMMC1_D3 +#else +#if defined(STM32F7) +#error Due to DMA configuration, only SDMMC1 is currently supported on F7 +#endif +#define SDMMC SDMMC2 +#define SDMMC_IRQn SDMMC2_IRQn +#define SDMMC_IRQHandler SDMMC2_IRQHandler +#define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC2_CLK_ENABLE() +#define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC2_CLK_DISABLE() +#define SDMMC_IS_CLK_DISABLED() __HAL_RCC_SDMMC2_IS_CLK_DISABLED() +#define STATIC_AF_SDMMC_CK STATIC_AF_SDMMC2_CK +#define STATIC_AF_SDMMC_CMD STATIC_AF_SDMMC2_CMD +#define STATIC_AF_SDMMC_D0 STATIC_AF_SDMMC2_D0 +#define STATIC_AF_SDMMC_D1 STATIC_AF_SDMMC2_D1 +#define STATIC_AF_SDMMC_D2 STATIC_AF_SDMMC2_D2 +#define STATIC_AF_SDMMC_D3 STATIC_AF_SDMMC2_D3 +#endif + +// If no custom SDIO pins defined, use the default ones +#ifndef MICROPY_HW_SDIO_CK +#define MICROPY_HW_SDIO_D0 (pin_C8) +#define MICROPY_HW_SDIO_D1 (pin_C9) +#define MICROPY_HW_SDIO_D2 (pin_C10) +#define MICROPY_HW_SDIO_D3 (pin_C11) +#define MICROPY_HW_SDIO_CK (pin_C12) +#define MICROPY_HW_SDIO_CMD (pin_D2) +#endif + void sdio_init(uint32_t irq_pri) { // configure IO pins - mp_hal_pin_config(pin_C8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, 12); - mp_hal_pin_config(pin_C9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, 12); - mp_hal_pin_config(pin_C10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, 12); - mp_hal_pin_config(pin_C11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, 12); - mp_hal_pin_config(pin_C12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 12); // CLK doesn't need pull-up - mp_hal_pin_config(pin_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, 12); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D0); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D1); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D2); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D3); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_SDMMC_CK); + mp_hal_pin_config_alt_static(MICROPY_HW_SDIO_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CMD); - __HAL_RCC_SDMMC1_CLK_ENABLE(); // enable SDIO peripheral + SDMMC_CLK_ENABLE(); // enable SDIO peripheral - SDMMC_TypeDef *SDIO = SDMMC1; + SDMMC_TypeDef *SDIO = SDMMC; #if defined(STM32F7) SDIO->CLKCR = SDMMC_CLKCR_HWFC_EN | SDMMC_CLKCR_PWRSAV | (120 - 2); // 1-bit, 400kHz #else @@ -80,36 +125,36 @@ void sdio_init(uint32_t irq_pri) { __HAL_RCC_DMA2_CLK_ENABLE(); // enable DMA2 peripheral #endif - NVIC_SetPriority(SDMMC1_IRQn, irq_pri); + NVIC_SetPriority(SDMMC_IRQn, irq_pri); SDIO->MASK = 0; - HAL_NVIC_EnableIRQ(SDMMC1_IRQn); + HAL_NVIC_EnableIRQ(SDMMC_IRQn); } void sdio_deinit(void) { - __HAL_RCC_SDMMC1_CLK_DISABLE(); + SDMMC_CLK_DISABLE(); #if defined(STM32F7) __HAL_RCC_DMA2_CLK_DISABLE(); #endif } void sdio_reenable(void) { - if (__HAL_RCC_SDMMC1_IS_CLK_DISABLED()) { - __HAL_RCC_SDMMC1_CLK_ENABLE(); // enable SDIO peripheral + if (SDMMC_IS_CLK_DISABLED()) { + SDMMC_CLK_ENABLE(); // enable SDIO peripheral sdio_enable_high_speed_4bit(); } } void sdio_enable_irq(bool enable) { if (enable) { - SDMMC1->MASK |= SDMMC_MASK_SDIOITIE; + SDMMC->MASK |= SDMMC_MASK_SDIOITIE; } else { - SDMMC1->MASK &= ~SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= ~SDMMC_MASK_SDIOITIE; } } void sdio_enable_high_speed_4bit(void) { - SDMMC_TypeDef *SDIO = SDMMC1; + SDMMC_TypeDef *SDIO = SDMMC; SDIO->POWER = 0; // power off mp_hal_delay_us(10); #if defined(STM32F7) @@ -128,33 +173,33 @@ void sdio_enable_high_speed_4bit(void) { mp_hal_delay_us(10); } -void SDMMC1_IRQHandler(void) { - if (SDMMC1->STA & SDMMC_STA_CMDREND) { - SDMMC1->ICR = SDMMC_ICR_CMDRENDC; - uint32_t r1 = SDMMC1->RESP1; - if (SDMMC1->RESPCMD == 53 && r1 & 0x800) { - printf("bad RESP1: %lu %lx\n", SDMMC1->RESPCMD, r1); +void SDMMC_IRQHandler(void) { + if (SDMMC->STA & SDMMC_STA_CMDREND) { + SDMMC->ICR = SDMMC_ICR_CMDRENDC; + uint32_t r1 = SDMMC->RESP1; + if (SDMMC->RESPCMD == 53 && r1 & 0x800) { + printf("bad RESP1: %lu %lx\n", SDMMC->RESPCMD, r1); sdmmc_error = 0xffffffff; - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; sdmmc_irq_state = SDMMC_IRQ_STATE_DONE; return; } #if defined(STM32H7) if (!sdmmc_dma) { - while (sdmmc_buf_cur < sdmmc_buf_top && (SDMMC1->STA & SDMMC_STA_DPSMACT) && !(SDMMC1->STA & SDMMC_STA_RXFIFOE)) { - *(uint32_t *)sdmmc_buf_cur = SDMMC1->FIFO; + while (sdmmc_buf_cur < sdmmc_buf_top && (SDMMC->STA & SDMMC_STA_DPSMACT) && !(SDMMC->STA & SDMMC_STA_RXFIFOE)) { + *(uint32_t *)sdmmc_buf_cur = SDMMC->FIFO; sdmmc_buf_cur += 4; } } #endif if (sdmmc_buf_cur >= sdmmc_buf_top) { // data transfer finished, so we are done - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; sdmmc_irq_state = SDMMC_IRQ_STATE_DONE; return; } if (sdmmc_write) { - SDMMC1->DCTRL = + SDMMC->DCTRL = SDMMC_DCTRL_SDIOEN | SDMMC_DCTRL_RWMOD | sdmmc_block_size_log2 << SDMMC_DCTRL_DBLOCKSIZE_Pos @@ -165,51 +210,51 @@ void SDMMC1_IRQHandler(void) { | SDMMC_DCTRL_DTEN ; if (!sdmmc_dma) { - SDMMC1->MASK |= SDMMC_MASK_TXFIFOHEIE; + SDMMC->MASK |= SDMMC_MASK_TXFIFOHEIE; } } sdmmc_irq_state = SDMMC_IRQ_STATE_CMD_DONE; - } else if (SDMMC1->STA & SDMMC_STA_DATAEND) { + } else if (SDMMC->STA & SDMMC_STA_DATAEND) { // data transfer complete // note: it's possible to get DATAEND before CMDREND - SDMMC1->ICR = SDMMC_ICR_DATAENDC; + SDMMC->ICR = SDMMC_ICR_DATAENDC; #if defined(STM32F7) // check if there is some remaining data in RXFIFO if (!sdmmc_dma) { - while (SDMMC1->STA & SDMMC_STA_RXDAVL) { - *(uint32_t *)sdmmc_buf_cur = SDMMC1->FIFO; + while (SDMMC->STA & SDMMC_STA_RXDAVL) { + *(uint32_t *)sdmmc_buf_cur = SDMMC->FIFO; sdmmc_buf_cur += 4; } } #endif if (sdmmc_irq_state == SDMMC_IRQ_STATE_CMD_DONE) { // command and data finished, so we are done - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; sdmmc_irq_state = SDMMC_IRQ_STATE_DONE; } - } else if (SDMMC1->STA & SDMMC_STA_TXFIFOHE) { + } else if (SDMMC->STA & SDMMC_STA_TXFIFOHE) { if (!sdmmc_dma && sdmmc_write) { // write up to 8 words to fifo for (size_t i = 8; i && sdmmc_buf_cur < sdmmc_buf_top; --i) { - SDMMC1->FIFO = *(uint32_t *)sdmmc_buf_cur; + SDMMC->FIFO = *(uint32_t *)sdmmc_buf_cur; sdmmc_buf_cur += 4; } if (sdmmc_buf_cur >= sdmmc_buf_top) { // finished, disable IRQ - SDMMC1->MASK &= ~SDMMC_MASK_TXFIFOHEIE; + SDMMC->MASK &= ~SDMMC_MASK_TXFIFOHEIE; } } - } else if (SDMMC1->STA & SDMMC_STA_RXFIFOHF) { + } else if (SDMMC->STA & SDMMC_STA_RXFIFOHF) { if (!sdmmc_dma && !sdmmc_write) { // read up to 8 words from fifo for (size_t i = 8; i && sdmmc_buf_cur < sdmmc_buf_top; --i) { - *(uint32_t *)sdmmc_buf_cur = SDMMC1->FIFO; + *(uint32_t *)sdmmc_buf_cur = SDMMC->FIFO; sdmmc_buf_cur += 4; } } - } else if (SDMMC1->STA & SDMMC_STA_SDIOIT) { - SDMMC1->MASK &= ~SDMMC_MASK_SDIOITIE; - SDMMC1->ICR = SDMMC_ICR_SDIOITC; + } else if (SDMMC->STA & SDMMC_STA_SDIOIT) { + SDMMC->MASK &= ~SDMMC_MASK_SDIOITIE; + SDMMC->ICR = SDMMC_ICR_SDIOITC; #if MICROPY_PY_NETWORK_CYW43 extern void (*cyw43_poll)(void); @@ -217,11 +262,11 @@ void SDMMC1_IRQHandler(void) { pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll); } #endif - } else if (SDMMC1->STA & 0x3f) { + } else if (SDMMC->STA & 0x3f) { // an error - sdmmc_error = SDMMC1->STA; - SDMMC1->ICR = SDMMC_STATIC_FLAGS; - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + sdmmc_error = SDMMC->STA; + SDMMC->ICR = SDMMC_STATIC_FLAGS; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; sdmmc_irq_state = SDMMC_IRQ_STATE_DONE; } } @@ -229,22 +274,22 @@ void SDMMC1_IRQHandler(void) { int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) { #if defined(STM32F7) // Wait for any outstanding TX to complete - while (SDMMC1->STA & SDMMC_STA_TXACT) { + while (SDMMC->STA & SDMMC_STA_TXACT) { } #endif #if defined(STM32F7) DMA2_Stream3->CR = 0; // ensure DMA is reset #endif - SDMMC1->ICR = SDMMC_STATIC_FLAGS; // clear interrupts - SDMMC1->ARG = arg; - SDMMC1->CMD = cmd | SDMMC_CMD_WAITRESP_0 | SDMMC_CMD_CPSMEN; + SDMMC->ICR = SDMMC_STATIC_FLAGS; // clear interrupts + SDMMC->ARG = arg; + SDMMC->CMD = cmd | SDMMC_CMD_WAITRESP_0 | SDMMC_CMD_CPSMEN; sdmmc_irq_state = SDMMC_IRQ_STATE_CMD_DATA_PENDING; sdmmc_error = 0; sdmmc_buf_cur = NULL; sdmmc_buf_top = NULL; - SDMMC1->MASK = (SDMMC1->MASK & SDMMC_MASK_SDIOITIE) | SDMMC_MASK_CMDRENDIE | 0x3f; + SDMMC->MASK = (SDMMC->MASK & SDMMC_MASK_SDIOITIE) | SDMMC_MASK_CMDRENDIE | 0x3f; uint32_t start = mp_hal_ticks_ms(); for (;;) { @@ -253,13 +298,13 @@ int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) { break; } if (mp_hal_ticks_ms() - start > 1000) { - SDMMC1->MASK = DEFAULT_MASK; - printf("sdio_transfer timeout STA=0x%08x\n", (uint)SDMMC1->STA); + SDMMC->MASK = DEFAULT_MASK; + printf("sdio_transfer timeout STA=0x%08x\n", (uint)SDMMC->STA); return -MP_ETIMEDOUT; } } - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; if (sdmmc_error == SDMMC_STA_CCRCFAIL && cmd == 5) { // Errata: CMD CRC error is incorrectly generated for CMD 5 @@ -268,13 +313,13 @@ int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) { return -(0x1000000 | sdmmc_error); } - uint32_t rcmd = SDMMC1->RESPCMD; + uint32_t rcmd = SDMMC->RESPCMD; if (rcmd != cmd) { printf("sdio_transfer: cmd=%lu rcmd=%lu\n", cmd, rcmd); return -MP_EIO; } if (resp != NULL) { - *resp = SDMMC1->RESP1; + *resp = SDMMC->RESP1; } return 0; } @@ -282,7 +327,7 @@ int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) { int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf) { #if defined(STM32F7) // Wait for any outstanding TX to complete - while (SDMMC1->STA & SDMMC_STA_TXACT) { + while (SDMMC->STA & SDMMC_STA_TXACT) { } #endif @@ -309,11 +354,11 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le bool dma = (len > 16); - SDMMC1->ICR = SDMMC_STATIC_FLAGS; // clear interrupts - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->ICR = SDMMC_STATIC_FLAGS; // clear interrupts + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; - SDMMC1->DTIMER = 0x2000000; // about 700ms running at 48MHz - SDMMC1->DLEN = (len + block_size - 1) & ~(block_size - 1); + SDMMC->DTIMER = 0x2000000; // about 700ms running at 48MHz + SDMMC->DLEN = (len + block_size - 1) & ~(block_size - 1); #if defined(STM32F7) DMA2_Stream3->CR = 0; @@ -339,7 +384,7 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le #if defined(STM32F7) DMA2->LIFCR = 0x3f << 22; DMA2_Stream3->FCR = 0x07; // ? - DMA2_Stream3->PAR = (uint32_t)&SDMMC1->FIFO; + DMA2_Stream3->PAR = (uint32_t)&SDMMC->FIFO; if ((uint32_t)buf & 3) { printf("sdio_transfer_cmd53: buf=%p is not aligned for DMA\n", buf); return -MP_EINVAL; @@ -359,12 +404,12 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le | 1 << 0 // EN ; #else - SDMMC1->IDMABASE0 = (uint32_t)buf; - SDMMC1->IDMACTRL = SDMMC_IDMA_IDMAEN; + SDMMC->IDMABASE0 = (uint32_t)buf; + SDMMC->IDMACTRL = SDMMC_IDMA_IDMAEN; #endif } else { #if defined(STM32H7) - SDMMC1->IDMACTRL = 0; + SDMMC->IDMACTRL = 0; #endif } @@ -373,7 +418,7 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le // (and in case we get a long-running unrelated IRQ here on the host just // after writing to CMD to initiate the command) if (!write) { - SDMMC1->DCTRL = + SDMMC->DCTRL = SDMMC_DCTRL_SDIOEN | SDMMC_DCTRL_RWMOD | block_size_log2 << SDMMC_DCTRL_DBLOCKSIZE_Pos @@ -385,8 +430,8 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le ; } - SDMMC1->ARG = arg; - SDMMC1->CMD = 53 | SDMMC_CMD_WAITRESP_0 | SDMMC_CMD_CPSMEN; + SDMMC->ARG = arg; + SDMMC->CMD = 53 | SDMMC_CMD_WAITRESP_0 | SDMMC_CMD_CPSMEN; sdmmc_irq_state = SDMMC_IRQ_STATE_CMD_DATA_PENDING; sdmmc_block_size_log2 = block_size_log2; @@ -395,7 +440,7 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le sdmmc_error = 0; sdmmc_buf_cur = (uint8_t *)buf; sdmmc_buf_top = (uint8_t *)buf + len; - SDMMC1->MASK = (SDMMC1->MASK & SDMMC_MASK_SDIOITIE) | SDMMC_MASK_CMDRENDIE | SDMMC_MASK_DATAENDIE | SDMMC_MASK_RXFIFOHFIE | 0x3f; + SDMMC->MASK = (SDMMC->MASK & SDMMC_MASK_SDIOITIE) | SDMMC_MASK_CMDRENDIE | SDMMC_MASK_DATAENDIE | SDMMC_MASK_RXFIFOHFIE | 0x3f; // wait to complete transfer uint32_t start = mp_hal_ticks_ms(); @@ -405,23 +450,23 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le break; } if (mp_hal_ticks_ms() - start > 200) { - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; #if defined(STM32F7) - printf("sdio_transfer_cmd53: timeout wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x DMA=%08x:%08x:%08x RCC=%08x\n", write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC1->STA, (uint)SDMMC1->DCOUNT, (uint)SDMMC1->FIFOCNT, (uint)DMA2->LISR, (uint)DMA2->HISR, (uint)DMA2_Stream3->NDTR, (uint)RCC->AHB1ENR); + printf("sdio_transfer_cmd53: timeout wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x DMA=%08x:%08x:%08x RCC=%08x\n", write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->FIFOCNT, (uint)DMA2->LISR, (uint)DMA2->HISR, (uint)DMA2_Stream3->NDTR, (uint)RCC->AHB1ENR); #else - printf("sdio_transfer_cmd53: timeout wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC1->STA, (uint)SDMMC1->DCOUNT, (uint)SDMMC1->DCTRL, (uint)SDMMC1->IDMACTRL); + printf("sdio_transfer_cmd53: timeout wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->DCTRL, (uint)SDMMC->IDMACTRL); #endif return -MP_ETIMEDOUT; } } - SDMMC1->MASK &= SDMMC_MASK_SDIOITIE; + SDMMC->MASK &= SDMMC_MASK_SDIOITIE; if (sdmmc_error) { #if defined(STM32F7) - printf("sdio_transfer_cmd53: error=%08lx wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x DMA=%08x:%08x:%08x RCC=%08x\n", sdmmc_error, write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC1->STA, (uint)SDMMC1->DCOUNT, (uint)SDMMC1->FIFOCNT, (uint)DMA2->LISR, (uint)DMA2->HISR, (uint)DMA2_Stream3->NDTR, (uint)RCC->AHB1ENR); + printf("sdio_transfer_cmd53: error=%08lx wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x DMA=%08x:%08x:%08x RCC=%08x\n", sdmmc_error, write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->FIFOCNT, (uint)DMA2->LISR, (uint)DMA2->HISR, (uint)DMA2_Stream3->NDTR, (uint)RCC->AHB1ENR); #else - printf("sdio_transfer_cmd53: error=%08lx wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", sdmmc_error, write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC1->STA, (uint)SDMMC1->DCOUNT, (uint)SDMMC1->DCTRL, (uint)SDMMC1->IDMACTRL); + printf("sdio_transfer_cmd53: error=%08lx wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", sdmmc_error, write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->DCTRL, (uint)SDMMC->IDMACTRL); #endif return -(0x1000000 | sdmmc_error); } From a41cd150be01adc59828ead26a01a9d9324773e1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:48:04 +1000 Subject: [PATCH 0790/3533] esp8266/modnetwork: Use mp_handle_pending() to raise pending exception. If MICROPY_ENABLE_SCHEDULER is enabled then MP_STATE_VM(sched_state) must be updated after handling the pending exception, which is done by the mp_handle_pending() function. Signed-off-by: Damien George --- ports/esp8266/modnetwork.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index cad53d912cafc..3b8ef4b21fba7 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -239,9 +239,7 @@ STATIC mp_obj_t esp_scan(mp_obj_t self_in) { // esp_scan_list variable to NULL without disabling interrupts if (MP_STATE_VM(mp_pending_exception) != NULL) { esp_scan_list = NULL; - mp_obj_t obj = MP_STATE_VM(mp_pending_exception); - MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; - nlr_raise(obj); + mp_handle_pending(true); } ets_loop_iter(); } From 7e549b6718e80136b03074765f1d4add209a21a5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:50:09 +1000 Subject: [PATCH 0791/3533] py/profile: Use mp_handle_pending() to raise pending exception. If MICROPY_ENABLE_SCHEDULER is enabled then MP_STATE_VM(sched_state) must be updated after handling the pending exception, which is done by the mp_handle_pending() function. Signed-off-by: Damien George --- py/profile.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/py/profile.c b/py/profile.c index 9cf8c4b7ba69f..e5fb35f0ede49 100644 --- a/py/profile.c +++ b/py/profile.c @@ -298,9 +298,7 @@ STATIC mp_obj_t mp_prof_callback_invoke(mp_obj_t callback, prof_callback_args_t mp_prof_is_executing = false; if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { - mp_obj_t obj = MP_STATE_VM(mp_pending_exception); - MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; - nlr_raise(obj); + mp_handle_pending(true); } return top; } From 7cbf826a9575e18ce1b7fe11b0f0997509153260 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:52:19 +1000 Subject: [PATCH 0792/3533] py/scheduler: Add mp_sched_exception() to schedule a pending exception. This helper is added to properly set a pending exception, to mirror mp_sched_schedule(), which schedules a function. Signed-off-by: Damien George --- ports/esp32/mpconfigport.h | 1 + ports/esp8266/mpconfigport.h | 1 + py/mpconfig.h | 4 ++++ py/runtime.h | 1 + py/scheduler.c | 14 +++++++++----- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 8788963cb3209..0c7d42210e084 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -264,6 +264,7 @@ void *esp_native_code_commit(void *, size_t, void *); #endif // Functions that should go in IRAM +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) IRAM_ATTR f #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) IRAM_ATTR f #define UINT_FMT "%u" diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 2c56b4188fd07..52028e833a8f2 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -195,6 +195,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_PY_SYS_PLATFORM "esp8266" #define MP_FASTCODE(n) __attribute__((section(".iram0.text." #n))) n +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_SCHED_SCHEDULE(f) MP_FASTCODE(f) diff --git a/py/mpconfig.h b/py/mpconfig.h index 5c7212fd1f6bc..2934f8ec89ee3 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1504,6 +1504,10 @@ typedef double mp_float_t; /*****************************************************************************/ /* Hooks for a port to wrap functions with attributes */ +#ifndef MICROPY_WRAP_MP_SCHED_EXCEPTION +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) f +#endif + #ifndef MICROPY_WRAP_MP_KEYBOARD_INTERRUPT #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) f #endif diff --git a/py/runtime.h b/py/runtime.h index 0cbbb287ac0f0..9ad4bc0242b0b 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -64,6 +64,7 @@ extern const byte mp_binary_op_method_name[]; void mp_init(void); void mp_deinit(void); +void mp_sched_exception(mp_obj_t exc); void mp_keyboard_interrupt(void); void mp_handle_pending(bool raise_exc); void mp_handle_pending_tail(mp_uint_t atomic_state); diff --git a/py/scheduler.c b/py/scheduler.c index 6b138a631b371..f37f8c3f861e6 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -28,17 +28,21 @@ #include "py/runtime.h" -#if MICROPY_KBD_EXCEPTION -// This function may be called asynchronously at any time so only do the bare minimum. -void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) { - MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; - MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)); +void mp_sched_exception(mp_obj_t exc) { + MP_STATE_VM(mp_pending_exception) = exc; #if MICROPY_ENABLE_SCHEDULER if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { MP_STATE_VM(sched_state) = MP_SCHED_PENDING; } #endif } + +#if MICROPY_KBD_EXCEPTION +// This function may be called asynchronously at any time so only do the bare minimum. +void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) { + MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; + mp_sched_exception(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); +} #endif #if MICROPY_ENABLE_SCHEDULER From bd54eb566f85293279d02d0a663c7e4d9f59e660 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:53:54 +1000 Subject: [PATCH 0793/3533] nrf/boards/microbit: Use mp_sched_exception() where appropriate. Signed-off-by: Damien George --- ports/nrf/boards/microbit/modules/microbitdisplay.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index 979a4ddd08b63..c2eaf4179bcdf 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -305,7 +305,7 @@ static void draw_object(mp_obj_t obj) { async_stop(); } } else { - MP_STATE_VM(mp_pending_exception) = mp_obj_new_exception_msg(&mp_type_TypeError, MP_ERROR_TEXT("not an image.")); + mp_sched_exception(mp_obj_new_exception_msg(&mp_type_TypeError, MP_ERROR_TEXT("not an image."))); async_stop(); } } @@ -341,7 +341,7 @@ static void microbit_display_update(void) { if (mp_obj_get_type(nlr.ret_val) == &mp_type_MemoryError) { mp_printf(&mp_plat_print, "Allocation in interrupt handler"); } - MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(nlr.ret_val); + mp_sched_exception(MP_OBJ_FROM_PTR(nlr.ret_val)); } obj = MP_OBJ_STOP_ITERATION; } From e9e9c76ddf131f8e50b0ae2d44501d3cd88537ef Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:57:34 +1000 Subject: [PATCH 0794/3533] all: Rename mp_keyboard_interrupt to mp_sched_keyboard_interrupt. To match mp_sched_exception() and mp_sched_schedule(). Signed-off-by: Damien George --- extmod/uos_dupterm.c | 2 +- ports/cc3200/mods/pybuart.c | 2 +- ports/cc3200/telnet/telnet.c | 2 +- ports/esp32/mpconfigport.h | 2 +- ports/esp32/uart.c | 2 +- ports/esp32/usb.c | 2 +- ports/esp8266/esp_mphal.h | 2 +- ports/esp8266/mpconfigport.h | 2 +- ports/esp8266/uart.c | 6 +++--- ports/javascript/Makefile | 2 +- ports/javascript/library.js | 2 +- ports/mimxrt/mphalport.c | 2 +- ports/nrf/drivers/bluetooth/ble_uart.c | 2 +- ports/nrf/modules/machine/uart.c | 2 +- ports/rp2/mphalport.c | 2 +- ports/rp2/uart.c | 2 +- ports/samd/mphalport.c | 2 +- ports/stm32/pendsv.c | 2 +- ports/unix/coverage.c | 8 ++++---- ports/unix/unix_mphal.c | 2 +- ports/windows/windows_mphal.c | 2 +- ports/zephyr/src/zephyr_getchar.c | 4 ++-- py/mpconfig.h | 4 ++-- py/runtime.h | 2 +- py/scheduler.c | 2 +- 25 files changed, 32 insertions(+), 32 deletions(-) diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c index ad706d5246f35..b661803875f7f 100644 --- a/extmod/uos_dupterm.c +++ b/extmod/uos_dupterm.c @@ -133,7 +133,7 @@ int mp_uos_dupterm_rx_chr(void) { nlr_pop(); if (buf[0] == mp_interrupt_char) { // Signal keyboard interrupt to be raised as soon as the VM resumes - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); return -2; } return buf[0]; diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index 98adf1c39816f..ac2af7032ee91 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -250,7 +250,7 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) { int data = MAP_UARTCharGetNonBlocking(self->reg); if (MP_STATE_PORT(os_term_dup_obj) && MP_STATE_PORT(os_term_dup_obj)->stream_o == self && data == mp_interrupt_char) { // raise an exception when interrupts are finished - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else { // there's always a read buffer available uint16_t next_head = (self->read_buf_head + 1) % PYBUART_RX_BUFFER_LEN; if (next_head != self->read_buf_tail) { diff --git a/ports/cc3200/telnet/telnet.c b/ports/cc3200/telnet/telnet.c index 401e8a8cc9323..9f51d4cd85959 100644 --- a/ports/cc3200/telnet/telnet.c +++ b/ports/cc3200/telnet/telnet.c @@ -446,7 +446,7 @@ static void telnet_parse_input (uint8_t *str, int16_t *len) { if (*_str <= 127) { if (telnet_data.state == E_TELNET_STE_LOGGED_IN && *_str == mp_interrupt_char) { // raise a keyboard exception - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); (*len)--; _str++; } diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 0c7d42210e084..a06c910470192 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -265,7 +265,7 @@ void *esp_native_code_commit(void *, size_t, void *); // Functions that should go in IRAM #define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) IRAM_ATTR f -#define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) IRAM_ATTR f +#define MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(f) IRAM_ATTR f #define UINT_FMT "%u" #define INT_FMT "%d" diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index bd3eea9f6bed3..c4fe41eaff39c 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -55,7 +55,7 @@ STATIC void IRAM_ATTR uart_irq_handler(void *arg) { uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0 #endif if (c == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else { // this is an inline function so will be in IRAM ringbuf_put(&stdin_ringbuf, c); diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index aa76321a7e918..85a7a11c9ead1 100644 --- a/ports/esp32/usb.c +++ b/ports/esp32/usb.c @@ -50,7 +50,7 @@ static void usb_callback_rx(int itf, cdcacm_event_t *event) { } for (size_t i = 0; i < len; ++i) { if (usb_rx_buf[i] == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else { ringbuf_put(&stdin_ringbuf, usb_rx_buf[i]); } diff --git a/ports/esp8266/esp_mphal.h b/ports/esp8266/esp_mphal.h index 89f4e0ff08544..e9cf7e548d3fa 100644 --- a/ports/esp8266/esp_mphal.h +++ b/ports/esp8266/esp_mphal.h @@ -29,7 +29,7 @@ #include "lib/utils/interrupt_char.h" #include "xtirq.h" -void mp_keyboard_interrupt(void); +void mp_sched_keyboard_interrupt(void); struct _mp_print_t; // Structure for UART-only output via mp_printf() diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 52028e833a8f2..ac47f0aa29059 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -196,7 +196,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MP_FASTCODE(n) __attribute__((section(".iram0.text." #n))) n #define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) MP_FASTCODE(f) -#define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) MP_FASTCODE(f) +#define MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_SCHED_SCHEDULE(f) MP_FASTCODE(f) #define WDEV_HWRNG ((volatile uint32_t *)0x3ff20e44) diff --git a/ports/esp8266/uart.c b/ports/esp8266/uart.c index 7d88555c05ef5..5ac71da07df1a 100644 --- a/ports/esp8266/uart.c +++ b/ports/esp8266/uart.c @@ -42,7 +42,7 @@ static ringbuf_t uart_ringbuf = {uart_ringbuf_array, sizeof(uart_ringbuf_array), static void uart0_rx_intr_handler(void *para); void soft_reset(void); -void mp_keyboard_interrupt(void); +void mp_sched_keyboard_interrupt(void); /****************************************************************************** * FunctionName : uart_config @@ -179,7 +179,7 @@ static void uart0_rx_intr_handler(void *para) { // directly on stdin_ringbuf, rather than going via uart_ringbuf if (uart_attached_to_dupterm) { if (RcvChar == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else { ringbuf_put(&stdin_ringbuf, RcvChar); } @@ -303,7 +303,7 @@ void ICACHE_FLASH_ATTR uart_task_handler(os_event_t *evt) { int c, ret = 0; while ((c = ringbuf_get(&stdin_ringbuf)) >= 0) { if (c == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } ret = pyexec_event_repl_process_char(c); if (ret & PYEXEC_FORCED_EXIT) { diff --git a/ports/javascript/Makefile b/ports/javascript/Makefile index 3962e93f5f334..97236755464cb 100644 --- a/ports/javascript/Makefile +++ b/ports/javascript/Makefile @@ -51,7 +51,7 @@ OBJ = $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -JSFLAGS = -O0 -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_keyboard_interrupt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s "BINARYEN_TRAP_MODE='clamp'" --memory-init-file 0 --js-library library.js +JSFLAGS = -O0 -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_sched_keyboard_interrupt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s "BINARYEN_TRAP_MODE='clamp'" --memory-init-file 0 --js-library library.js all: $(BUILD)/micropython.js diff --git a/ports/javascript/library.js b/ports/javascript/library.js index 9aab3e4320aa1..a8e54aaacae1d 100644 --- a/ports/javascript/library.js +++ b/ports/javascript/library.js @@ -55,7 +55,7 @@ mergeInto(LibraryManager.library, { var n = fs.readSync(process.stdin.fd, buf, 0, 1); if (n > 0) { if (buf[0] == mp_interrupt_char) { - Module.ccall('mp_keyboard_interrupt', 'null', ['null'], ['null']); + Module.ccall('mp_sched_keyboard_interrupt', 'null', ['null'], ['null']); } else { process.stdout.write(String.fromCharCode(buf[0])); } diff --git a/ports/mimxrt/mphalport.c b/ports/mimxrt/mphalport.c index 3032464d398f4..9fee3e85d0082 100644 --- a/ports/mimxrt/mphalport.c +++ b/ports/mimxrt/mphalport.c @@ -37,7 +37,7 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { (void)itf; (void)wanted_char; tud_cdc_read_char(); // discard interrupt char - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } void mp_hal_set_interrupt_char(int c) { diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index 64e3a05f93192..96c2025d0ff71 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -193,7 +193,7 @@ STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t at for (uint16_t i = 0; i < length; i++) { #if MICROPY_KBD_EXCEPTION if (data[i] == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); m_rx_ring_buffer.start = 0; m_rx_ring_buffer.end = 0; } else diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 4f69bfc620b94..af95ae4b8d985 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -128,7 +128,7 @@ STATIC void uart_event_handler(nrfx_uart_event_t const *p_event, void *p_context nrfx_uart_rx(self->p_uart, &self->buf->rx_buf[0], 1); #if !MICROPY_PY_BLE_NUS && MICROPY_KBD_EXCEPTION if (chr == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else #endif { diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c index 1122afcef730e..1d77bc8f5d676 100644 --- a/ports/rp2/mphalport.c +++ b/ports/rp2/mphalport.c @@ -52,7 +52,7 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { (void)itf; (void)wanted_char; tud_cdc_read_char(); // discard interrupt char - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } void mp_hal_set_interrupt_char(int c) { diff --git a/ports/rp2/uart.c b/ports/rp2/uart.c index b7991563af034..bcc41bfaf5198 100644 --- a/ports/rp2/uart.c +++ b/ports/rp2/uart.c @@ -41,7 +41,7 @@ void uart_irq(void) { int c = uart_getc(uart_default); #if MICROPY_KBD_EXCEPTION if (c == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); return; } #endif diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index 357ec93a68143..a87b7d2125a56 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -35,7 +35,7 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { (void)itf; (void)wanted_char; tud_cdc_read_char(); // discard interrupt char - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } void mp_hal_set_interrupt_char(int c) { diff --git a/ports/stm32/pendsv.c b/ports/stm32/pendsv.c index 60b8709be34fd..3181ba616a5a4 100644 --- a/ports/stm32/pendsv.c +++ b/ports/stm32/pendsv.c @@ -61,7 +61,7 @@ void pendsv_init(void) { // thread. void pendsv_kbd_intr(void) { if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_NULL) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); } else { MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; pendsv_object = &MP_STATE_VM(mp_kbd_exception); diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index ef66c4fb59440..220666e381d6b 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -483,7 +483,7 @@ STATIC mp_obj_t extra_coverage(void) { } // setting the keyboard interrupt and raising it during mp_handle_pending - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_handle_pending(true); @@ -493,13 +493,13 @@ STATIC mp_obj_t extra_coverage(void) { } // setting the keyboard interrupt (twice) and cancelling it during mp_handle_pending - mp_keyboard_interrupt(); - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); mp_handle_pending(false); // setting keyboard interrupt and a pending event (intr should be handled first) mp_sched_schedule(MP_OBJ_FROM_PTR(&mp_builtin_print_obj), MP_OBJ_NEW_SMALL_INT(10)); - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); if (nlr_push(&nlr) == 0) { mp_handle_pending(true); nlr_pop(); diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 28a4ca2c4a8e9..d62991d34d04a 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -58,7 +58,7 @@ STATIC void sighandler(int signum) { // this is the second time we are called, so die straight away exit(1); } - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); #endif } } diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 49daa0542868f..dd196f67af36b 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -84,7 +84,7 @@ BOOL WINAPI console_sighandler(DWORD evt) { // this is the second time we are called, so die straight away exit(1); } - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); return TRUE; } return FALSE; diff --git a/ports/zephyr/src/zephyr_getchar.c b/ports/zephyr/src/zephyr_getchar.c index 40dc1c3201243..d75a8a76e3d69 100644 --- a/ports/zephyr/src/zephyr_getchar.c +++ b/ports/zephyr/src/zephyr_getchar.c @@ -21,7 +21,7 @@ #include "zephyr_getchar.h" extern int mp_interrupt_char; -void mp_keyboard_interrupt(void); +void mp_sched_keyboard_interrupt(void); static struct k_sem uart_sem; #define UART_BUFSIZE 256 @@ -36,7 +36,7 @@ static int console_irq_input_hook(uint8_t ch) return 1; } if (ch == mp_interrupt_char) { - mp_keyboard_interrupt(); + mp_sched_keyboard_interrupt(); return 1; } else { uart_ringbuf[i_put] = ch; diff --git a/py/mpconfig.h b/py/mpconfig.h index 2934f8ec89ee3..4445e9aee236b 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1508,8 +1508,8 @@ typedef double mp_float_t; #define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) f #endif -#ifndef MICROPY_WRAP_MP_KEYBOARD_INTERRUPT -#define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) f +#ifndef MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT +#define MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(f) f #endif #ifndef MICROPY_WRAP_MP_SCHED_SCHEDULE diff --git a/py/runtime.h b/py/runtime.h index 9ad4bc0242b0b..7d2cb94e84312 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -65,7 +65,7 @@ void mp_init(void); void mp_deinit(void); void mp_sched_exception(mp_obj_t exc); -void mp_keyboard_interrupt(void); +void mp_sched_keyboard_interrupt(void); void mp_handle_pending(bool raise_exc); void mp_handle_pending_tail(mp_uint_t atomic_state); diff --git a/py/scheduler.c b/py/scheduler.c index f37f8c3f861e6..9ff930007e595 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -39,7 +39,7 @@ void mp_sched_exception(mp_obj_t exc) { #if MICROPY_KBD_EXCEPTION // This function may be called asynchronously at any time so only do the bare minimum. -void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) { +void MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(mp_sched_keyboard_interrupt)(void) { MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; mp_sched_exception(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); } From 916c3fd23fc4b636a8007525c3bd6308d01509d0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 25 Apr 2021 22:24:49 +1000 Subject: [PATCH 0795/3533] py/scheduler: Add optional port hook for when something is scheduled. So that a port can "wake up" when there is work to do. Signed-off-by: Damien George --- py/mpconfig.h | 6 ++++++ py/scheduler.c | 1 + 2 files changed, 7 insertions(+) diff --git a/py/mpconfig.h b/py/mpconfig.h index 4445e9aee236b..7337994a5e85d 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -550,6 +550,12 @@ #define MICROPY_VM_HOOK_RETURN #endif +// Hook for mp_sched_schedule when a function gets scheduled on sched_queue +// (this macro executes within an atomic section) +#ifndef MICROPY_SCHED_HOOK_SCHEDULED +#define MICROPY_SCHED_HOOK_SCHEDULED +#endif + // Whether to include the garbage collector #ifndef MICROPY_ENABLE_GC #define MICROPY_ENABLE_GC (0) diff --git a/py/scheduler.c b/py/scheduler.c index 9ff930007e595..947510c68b1c7 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -134,6 +134,7 @@ bool MICROPY_WRAP_MP_SCHED_SCHEDULE(mp_sched_schedule)(mp_obj_t function, mp_obj uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++); MP_STATE_VM(sched_queue)[iput].func = function; MP_STATE_VM(sched_queue)[iput].arg = arg; + MICROPY_SCHED_HOOK_SCHEDULED; ret = true; } else { // schedule queue is full From d120859857e9e34bded146977f18a2be8c53ce7b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 25 Apr 2021 22:28:55 +1000 Subject: [PATCH 0796/3533] zephyr: Run scheduled callbacks at REPL and during mp_hal_delay_ms. And ctrl-C can now interrupt a time.sleep call. This uses Zephyr's k_poll API to wait efficiently for an event signal, and an optional semaphore. Signed-off-by: Damien George --- ports/zephyr/CMakeLists.txt | 1 + ports/zephyr/main.c | 2 + ports/zephyr/mpconfigport.h | 3 ++ ports/zephyr/mphalport.c | 74 +++++++++++++++++++++++++++++++ ports/zephyr/mphalport.h | 5 ++- ports/zephyr/prj.conf | 1 + ports/zephyr/prj_minimal.conf | 1 + ports/zephyr/src/zephyr_getchar.c | 4 ++ 8 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 ports/zephyr/mphalport.c diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 716642979a601..e30759e064464 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -46,6 +46,7 @@ set(MICROPY_SOURCE_PORT modutime.c modzephyr.c modzsensor.c + mphalport.c uart_core.c zephyr_storage.c ) diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 74785cc830420..f8ec0a0d27e68 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -45,6 +45,7 @@ #include "py/runtime.h" #include "py/repl.h" #include "py/gc.h" +#include "py/mphal.h" #include "py/stackctrl.h" #include "lib/utils/pyexec.h" #include "lib/mp-readline/readline.h" @@ -123,6 +124,7 @@ int real_main(void) { mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512); init_zephyr(); + mp_hal_init(); #ifdef TEST static const char *argv[] = {"test"}; diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 966f7f7e94a1d..162ac8a5766a2 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -96,6 +96,9 @@ #define MICROPY_COMP_CONST (0) #define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) +void mp_hal_signal_event(void); +#define MICROPY_SCHED_HOOK_SCHEDULED mp_hal_signal_event() + #define MICROPY_PY_SYS_PLATFORM "zephyr" #ifdef CONFIG_BOARD diff --git a/ports/zephyr/mphalport.c b/ports/zephyr/mphalport.c new file mode 100644 index 0000000000000..4f00cbd26ca67 --- /dev/null +++ b/ports/zephyr/mphalport.c @@ -0,0 +1,74 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" + +static struct k_poll_signal wait_signal; +static struct k_poll_event wait_events[2] = { + K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, + K_POLL_MODE_NOTIFY_ONLY, + &wait_signal), + K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_SEM_AVAILABLE, + K_POLL_MODE_NOTIFY_ONLY, + NULL, 0), +}; + +void mp_hal_init(void) { + k_poll_signal_init(&wait_signal); +} + +void mp_hal_signal_event(void) { + k_poll_signal_raise(&wait_signal, 0); +} + +void mp_hal_wait_sem(struct k_sem *sem, uint32_t timeout_ms) { + mp_uint_t t0 = mp_hal_ticks_ms(); + if (sem) { + k_poll_event_init(&wait_events[1], K_POLL_TYPE_SEM_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, sem); + } + for (;;) { + k_timeout_t wait; + if (timeout_ms == (uint32_t)-1) { + wait = K_FOREVER; + } else { + uint32_t dt = mp_hal_ticks_ms() - t0; + if (dt >= timeout_ms) { + return; + } + wait = K_MSEC(timeout_ms - dt); + } + k_poll(wait_events, sem ? 2 : 1, wait); + if (wait_events[0].state == K_POLL_STATE_SIGNALED) { + wait_events[0].signal->signaled = 0; + wait_events[0].state = K_POLL_STATE_NOT_READY; + mp_handle_pending(true); + } else if (sem && wait_events[1].state == K_POLL_STATE_SEM_AVAILABLE) { + wait_events[1].state = K_POLL_STATE_NOT_READY; + return; + } + } +} diff --git a/ports/zephyr/mphalport.h b/ports/zephyr/mphalport.h index b3154b649bbda..63a18e138866a 100644 --- a/ports/zephyr/mphalport.h +++ b/ports/zephyr/mphalport.h @@ -1,6 +1,9 @@ #include #include "lib/utils/interrupt_char.h" +void mp_hal_init(void); +void mp_hal_wait_sem(struct k_sem *sem, uint32_t timeout_ms); + static inline mp_uint_t mp_hal_ticks_us(void) { return k_cyc_to_ns_floor64(k_cycle_get_32()) / 1000; } @@ -21,7 +24,7 @@ static inline void mp_hal_delay_us(mp_uint_t delay) { } static inline void mp_hal_delay_ms(mp_uint_t delay) { - k_msleep(delay); + mp_hal_wait_sem(NULL, delay); } static inline uint64_t mp_hal_time_ns(void) { diff --git a/ports/zephyr/prj.conf b/ports/zephyr/prj.conf index c9c2e96da9beb..9e855036d2b84 100644 --- a/ports/zephyr/prj.conf +++ b/ports/zephyr/prj.conf @@ -13,6 +13,7 @@ CONFIG_CONSOLE_PUTCHAR_BUFSIZE=128 CONFIG_NEWLIB_LIBC=y CONFIG_FPU=y CONFIG_MAIN_STACK_SIZE=4736 +CONFIG_POLL=y # Enable sensor subsystem (doesn't add code if not used). # Specific sensors should be enabled per-board. diff --git a/ports/zephyr/prj_minimal.conf b/ports/zephyr/prj_minimal.conf index 8b2104925ab67..806e26af77841 100644 --- a/ports/zephyr/prj_minimal.conf +++ b/ports/zephyr/prj_minimal.conf @@ -1,6 +1,7 @@ CONFIG_NEWLIB_LIBC=y CONFIG_FPU=y CONFIG_MAIN_STACK_SIZE=4096 +CONFIG_POLL=y CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_CONSOLE_SUBSYS=y diff --git a/ports/zephyr/src/zephyr_getchar.c b/ports/zephyr/src/zephyr_getchar.c index d75a8a76e3d69..95d0b49959326 100644 --- a/ports/zephyr/src/zephyr_getchar.c +++ b/ports/zephyr/src/zephyr_getchar.c @@ -22,6 +22,8 @@ extern int mp_interrupt_char; void mp_sched_keyboard_interrupt(void); +void mp_hal_signal_event(void); +void mp_hal_wait_sem(struct k_sem *sem, uint32_t timeout_ms); static struct k_sem uart_sem; #define UART_BUFSIZE 256 @@ -36,6 +38,7 @@ static int console_irq_input_hook(uint8_t ch) return 1; } if (ch == mp_interrupt_char) { + mp_hal_signal_event(); mp_sched_keyboard_interrupt(); return 1; } else { @@ -49,6 +52,7 @@ static int console_irq_input_hook(uint8_t ch) } uint8_t zephyr_getchar(void) { + mp_hal_wait_sem(&uart_sem, -1); k_sem_take(&uart_sem, K_FOREVER); unsigned int key = irq_lock(); uint8_t c = uart_ringbuf[i_get++]; From b46a033e25eef64b8ef52a8408f99b666d32daae Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 26 Apr 2021 00:36:27 +1000 Subject: [PATCH 0797/3533] zephyr/modmachine: Add machine.idle(). Signed-off-by: Damien George --- ports/zephyr/modmachine.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 107895bea0e42..4353083eebd8f 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -53,12 +53,19 @@ STATIC mp_obj_t machine_reset_cause(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); +STATIC mp_obj_t machine_idle(void) { + k_yield(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, #ifdef CONFIG_REBOOT { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, #endif { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, + { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, #if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, From 888664130c6605f333a6901e821589dee43f6ebb Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 11:12:14 +1000 Subject: [PATCH 0798/3533] zephyr/boards: Add config for nucleo_wb55rg board. This board does not work with CONFIG_NETWORKING enabled. And CONFIG_CONSOLE_SUBSYS is enabled so that ctrl-C works. Signed-off-by: Damien George --- ports/zephyr/boards/nucleo_wb55rg.conf | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 ports/zephyr/boards/nucleo_wb55rg.conf diff --git a/ports/zephyr/boards/nucleo_wb55rg.conf b/ports/zephyr/boards/nucleo_wb55rg.conf new file mode 100644 index 0000000000000..369dabddebf20 --- /dev/null +++ b/ports/zephyr/boards/nucleo_wb55rg.conf @@ -0,0 +1,2 @@ +CONFIG_CONSOLE_SUBSYS=n +CONFIG_NETWORKING=n From 76dab3bf31617e707a874a6c0dfc49af97640980 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 30 Apr 2021 10:12:04 +1000 Subject: [PATCH 0799/3533] tests/run-multitests.py: Provide some convenient serial device shorcuts. It's now possible to specify a device serial port using shorcuts like: $ ./run-multitests.py -i pyb:a0 -i pyb:u1 multi_bluetooth/*.py Signed-off-by: Damien George --- tests/run-multitests.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index cdb2730edabbd..3163a48e63d2b 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -160,7 +160,17 @@ def wait_finished(self): class PyInstancePyboard(PyInstance): + @staticmethod + def map_device_shortcut(device): + if device[0] == "a" and device[1:].isdigit(): + return "/dev/ttyACM" + device[1:] + elif device[0] == "u" and device[1:].isdigit(): + return "/dev/ttyUSB" + device[1:] + else: + return device + def __init__(self, device): + device = self.map_device_shortcut(device) self.device = device self.pyb = pyboard.Pyboard(device) self.pyb.enter_raw_repl() From 1d9528210b8a29be7e799be37224706633f7a4d5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Mar 2021 23:32:59 +1100 Subject: [PATCH 0800/3533] tests/multi_bluetooth: Add performance test for gatt char writes. Signed-off-by: Damien George --- tests/multi_bluetooth/perf_gatt_char_write.py | 151 ++++++++++++++++++ .../perf_gatt_char_write.py.exp | 24 +++ 2 files changed, 175 insertions(+) create mode 100644 tests/multi_bluetooth/perf_gatt_char_write.py create mode 100644 tests/multi_bluetooth/perf_gatt_char_write.py.exp diff --git a/tests/multi_bluetooth/perf_gatt_char_write.py b/tests/multi_bluetooth/perf_gatt_char_write.py new file mode 100644 index 0000000000000..00607f0090121 --- /dev/null +++ b/tests/multi_bluetooth/perf_gatt_char_write.py @@ -0,0 +1,151 @@ +# Write characteristic from central to peripheral and time data rate. + +from micropython import const +import time, machine, bluetooth + +TIMEOUT_MS = 2000 + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_PERIPHERAL_CONNECT = const(7) +_IRQ_PERIPHERAL_DISCONNECT = const(8) +_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) +_IRQ_GATTC_CHARACTERISTIC_DONE = const(12) +_IRQ_GATTC_WRITE_DONE = const(17) +_IRQ_MTU_EXCHANGED = const(21) + +# How long to run the test for. +_NUM_NOTIFICATIONS = const(40) +_MTU_SIZE = const(131) +_CHAR_SIZE = const(_MTU_SIZE - 3) + +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") +CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") +CHAR = (CHAR_UUID, bluetooth.FLAG_WRITE | bluetooth.FLAG_WRITE_NO_RESPONSE) +SERVICE = (SERVICE_UUID, (CHAR,)) +SERVICES = (SERVICE,) + +packet_sequence = 0 +waiting_events = {} + + +def irq(event, data): + if event == _IRQ_CENTRAL_CONNECT: + waiting_events[event] = data[0] + elif event == _IRQ_PERIPHERAL_CONNECT: + waiting_events[event] = data[0] + elif event == _IRQ_GATTS_WRITE: + global packet_sequence + conn_handle, attr_handle = data + data = ble.gatts_read(attr_handle) + if not (data[0] == packet_sequence and data[-1] == (256 - packet_sequence) & 0xFF): + print("_IRQ_GATTS_WRITE data invalid:", packet_sequence, data) + elif packet_sequence % 10 == 0: + print("_IRQ_GATTS_WRITE", packet_sequence) + packet_sequence += 1 + elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: + # conn_handle, def_handle, value_handle, properties, uuid = data + if data[-1] == CHAR_UUID: + waiting_events[event] = data[2] + else: + return + elif event == _IRQ_MTU_EXCHANGED: + # ATT MTU exchange complete (either initiated by us or the remote device). + conn_handle, mtu = data + print("_IRQ_MTU_EXCHANGED:", mtu) + + if event not in waiting_events: + waiting_events[event] = None + + +def wait_for_event(event, timeout_ms): + t0 = time.ticks_ms() + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: + if event in waiting_events: + return waiting_events.pop(event) + machine.idle() + raise ValueError("Timeout waiting for {}".format(event)) + + +# Acting in peripheral role. +def instance0(): + multitest.globals(BDADDR=ble.config("mac")) + ((char_handle,),) = ble.gatts_register_services(SERVICES) + ble.gatts_set_buffer(char_handle, _CHAR_SIZE) + print("gap_advertise") + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") + multitest.next() + try: + # Wait for central to connect to us. + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + # Wait for central to disconnect us. + wait_for_event(_IRQ_CENTRAL_DISCONNECT, 30000) + print("final packet_sequence:", packet_sequence) + finally: + ble.active(0) + + +# Acting in central role. +def instance1(): + global packet_sequence + ((char_handle,),) = ble.gatts_register_services(SERVICES) + multitest.next() + try: + # Connect to peripheral and then disconnect. + print("gap_connect") + ble.config(mtu=_MTU_SIZE) + ble.gap_connect(*BDADDR) + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + ble.gattc_exchange_mtu(conn_handle) + + # Discover characteristics. + ble.gattc_discover_characteristics(conn_handle, 1, 65535) + value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS) + wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS) + + # Send data! + data = bytearray(ord("A") + (i % 64) for i in range(_CHAR_SIZE)) + for mode in (0, 1): + ticks_start = time.ticks_ms() + for i in range(_NUM_NOTIFICATIONS): + data[0] = packet_sequence + data[-1] = 256 - packet_sequence + if packet_sequence % 10 == 0: + print("gattc_write", packet_sequence) + if mode == 0: + while True: + try: + ble.gattc_write(conn_handle, value_handle, data, mode) + break + except OSError: + pass + else: + ble.gattc_write(conn_handle, value_handle, data, mode) + wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS) + packet_sequence += 1 + + ticks_end = time.ticks_ms() + ticks_total = time.ticks_diff(ticks_end, ticks_start) + + print( + "Did {} writes in {} ms. {} ms/write, {} bytes/sec".format( + _NUM_NOTIFICATIONS, + ticks_total, + ticks_total / _NUM_NOTIFICATIONS, + _NUM_NOTIFICATIONS * len(data) * 1000 // ticks_total, + ) + ) + + time.sleep_ms(100) + + # DIsconnect the peripheral. + print("gap_disconnect:", ble.gap_disconnect(conn_handle)) + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, 20000) + finally: + ble.active(0) + + +ble = bluetooth.BLE() +ble.active(1) +ble.irq(irq) diff --git a/tests/multi_bluetooth/perf_gatt_char_write.py.exp b/tests/multi_bluetooth/perf_gatt_char_write.py.exp new file mode 100644 index 0000000000000..4dda5e04f0920 --- /dev/null +++ b/tests/multi_bluetooth/perf_gatt_char_write.py.exp @@ -0,0 +1,24 @@ +--- instance0 --- +gap_advertise +_IRQ_MTU_EXCHANGED: 131 +_IRQ_GATTS_WRITE 0 +_IRQ_GATTS_WRITE 10 +_IRQ_GATTS_WRITE 20 +_IRQ_GATTS_WRITE 30 +_IRQ_GATTS_WRITE 40 +_IRQ_GATTS_WRITE 50 +_IRQ_GATTS_WRITE 60 +_IRQ_GATTS_WRITE 70 +final packet_sequence: 80 +--- instance1 --- +gap_connect +_IRQ_MTU_EXCHANGED: 131 +gattc_write 0 +gattc_write 10 +gattc_write 20 +gattc_write 30 +gattc_write 40 +gattc_write 50 +gattc_write 60 +gattc_write 70 +gap_disconnect: True From 6b7c8d3e7283a6c94687ba7dcb8beb94c2dc9eef Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 30 Apr 2021 16:26:42 +1000 Subject: [PATCH 0801/3533] py/runtime: Remove commented-out code from mp_deinit(). These commented-out lines of code have been unused for a long time, so remove them to avoid confusion as to why they are there. mp_obj_dict_free() never existed, this line was converted from mp_map_deinit() and commented out as soon as it was added. The call to mp_map_deinit(mp_loaded_modules_map) was commented in 1a1d11fa32ba043d22995d28cbc039cfa5f3cc46. Fixes issue #3507. Signed-off-by: Damien George --- py/runtime.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/py/runtime.c b/py/runtime.c index 18a7c8565da9b..1ca0702e19fb9 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -155,9 +155,6 @@ void mp_deinit(void) { #ifdef MICROPY_PORT_DEINIT_FUNC MICROPY_PORT_DEINIT_FUNC; #endif - - // mp_obj_dict_free(&dict_main); - // mp_map_deinit(&MP_STATE_VM(mp_loaded_modules_map)); } mp_obj_t mp_load_name(qstr qst) { From 9e1b25a99e9107619c6ed607c730ea2869582799 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 30 Apr 2021 16:42:51 +1000 Subject: [PATCH 0802/3533] docs/library/machine: Specify initial machine.PWM class. This adds an initial specification of the machine.PWM class, to provide a way to generate PWM output that is portable across the different ports. Such functionality may already be available in one way or another (eg through a Timer object), but because configuring PWM via a Timer is very port-specific, and because it's a common thing to do, it's beneficial to have a top-level construct for it. The specification in this commit aims to provide core functionality in a minimal way. It also somewhat matches most existing ad-hoc implementations of machine.PWM. See discussion in #2283 and #4237. Signed-off-by: Damien George --- docs/library/machine.PWM.rst | 79 ++++++++++++++++++++++++++++++++++++ docs/library/machine.rst | 1 + 2 files changed, 80 insertions(+) create mode 100644 docs/library/machine.PWM.rst diff --git a/docs/library/machine.PWM.rst b/docs/library/machine.PWM.rst new file mode 100644 index 0000000000000..f2273d8b453da --- /dev/null +++ b/docs/library/machine.PWM.rst @@ -0,0 +1,79 @@ +.. currentmodule:: machine +.. _machine.PWM: + +class PWM -- pulse width modulation +=================================== + +This class provides pulse width modulation output. + +Example usage:: + + from machine import PWM + + pwm = PWM(pin) # create a PWM object on a pin + pwm.duty_u16(32768) # set duty to 50% + + # reinitialise with a period of 200us, duty of 5us + pwm.init(freq=5000, duty_ns=5000) + + pwm.duty_ns(3000) # set pulse width to 3us + + pwm.deinit() + +Constructors +------------ + +.. class:: PWM(dest, \*, freq, duty_u16, duty_ns) + + Construct and return a new PWM object using the following parameters: + + - *dest* is the entity on which the PWM is output, which is usually a + :ref:`machine.Pin ` object, but a port may allow other values, + like integers. + - *freq* should be an integer which sets the frequency in Hz for the + PWM cycle. + - *duty_u16* sets the duty cycle as a ratio ``duty_u16 / 65535``. + - *duty_ns* sets the pulse width in nanoseconds. + + Setting *freq* may affect other PWM objects if the objects share the same + underlying PWM generator (this is hardware specific). + Only one of *duty_u16* and *duty_ns* should be specified at a time. + +Methods +------- + +.. method:: PWM.init(\*, freq, duty_u16, duty_ns) + + Modify settings for the PWM object. See the above constructor for details + about the parameters. + +.. method:: PWM.deinit() + + Disable the PWM output. + +.. method:: PWM.freq([value]) + + Get or set the current frequency of the PWM output. + + With no arguments the frequency in Hz is returned. + + With a single *value* argument the frequency is set to that value in Hz. The + method may raise a ``ValueError`` if the frequency is outside the valid range. + +.. method:: PWM.duty_u16([value]) + + Get or set the current duty cycle of the PWM output, as an unsigned 16-bit + value in the range 0 to 65535 inclusive. + + With no arguments the duty cycle is returned. + + With a single *value* argument the duty cycle is set to that value, measured + as the ratio ``value / 65535``. + +.. method:: PWM.duty_ns([value]) + + Get or set the current pulse width of the PWM output, as a value in nanoseconds. + + With no arguments the pulse width in nanoseconds is returned. + + With a single *value* argument the pulse width is set to that value. diff --git a/docs/library/machine.rst b/docs/library/machine.rst index 18dc6f2afaa59..5076dc30e059f 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -167,6 +167,7 @@ Classes machine.Pin.rst machine.Signal.rst machine.ADC.rst + machine.PWM.rst machine.UART.rst machine.SPI.rst machine.I2C.rst From dd5c831a0b0c960b638109f1886edc10178f51a1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 30 Apr 2021 16:55:55 +1000 Subject: [PATCH 0803/3533] docs/library/machine: Add machine.bootloader docs. This is provide by a few ports now, and is very useful. Signed-off-by: Damien George --- docs/library/machine.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/library/machine.rst b/docs/library/machine.rst index 5076dc30e059f..f831049f88e92 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -37,6 +37,14 @@ Reset related functions Get the reset cause. See :ref:`constants ` for the possible return values. +.. function:: bootloader([value]) + + Reset the device and enter its bootloader. This is typically used to put the + device into a state where it can be programmed with new firmware. + + Some ports support passing in an optional *value* argument which can control + which bootloader to enter, what to pass to it, or other things. + Interrupt related functions --------------------------- From aa061ae391f2a8fbb16ace31ae0e4ae35b6ad5e9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 30 Apr 2021 16:48:29 +1000 Subject: [PATCH 0804/3533] py/scheduler: Add missing MICROPY_WRAP_MP_SCHED_EXCEPTION usage. This was missed in commit 7cbf826a9575e18ce1b7fe11b0f0997509153260. Signed-off-by: Damien George --- py/scheduler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/scheduler.c b/py/scheduler.c index 947510c68b1c7..0671a34a8cad9 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -28,7 +28,7 @@ #include "py/runtime.h" -void mp_sched_exception(mp_obj_t exc) { +void MICROPY_WRAP_MP_SCHED_EXCEPTION(mp_sched_exception)(mp_obj_t exc) { MP_STATE_VM(mp_pending_exception) = exc; #if MICROPY_ENABLE_SCHEDULER if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { From 1f1a54d0b1371efcfec6ca1b03d21b07a908c70d Mon Sep 17 00:00:00 2001 From: Kathryn Lingel Date: Mon, 6 May 2019 12:16:29 -0700 Subject: [PATCH 0805/3533] py/repl: Filter private methods from tab completion. Anything beginning with "_" will now only be tab-completed if there is already a partial match for such an entry. In other words, entering foo. will no longer complete/list anything beginning with "_". Originally at adafruit#1850 Signed-off-by: Kathryn Lingel --- py/repl.c | 5 +++++ tests/unix/extra_coverage.py.exp | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/py/repl.c b/py/repl.c index c758fc067b2e7..3b1837031b594 100644 --- a/py/repl.c +++ b/py/repl.c @@ -197,6 +197,11 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) { size_t d_len; const char *d_str = (const char *)qstr_data(q, &d_len); + // special case; filter out words that begin with underscore + // unless there's already a partial match + if (s_len == 0 && d_str[0] == '_') { + continue; + } if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { mp_load_method_protected(obj, q, dest, true); if (dest[0] != MP_OBJ_NULL) { diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index d97de2c0ef8b3..ab4d977741122 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -28,11 +28,11 @@ RuntimeError: # repl ame__ -__class__ __name__ argv atexit -byteorder exc_info exit getsizeof -implementation maxsize modules path -platform print_exception stderr -stdin stdout version version_info +argv atexit byteorder exc_info +exit getsizeof implementation maxsize +modules path platform print_exception +stderr stdin stdout version +version_info ementation # attrtuple (start=1, stop=2, step=3) From befbff31b73d5373b5933a3762a3a3c6e871e8da Mon Sep 17 00:00:00 2001 From: scottbelden Date: Tue, 7 May 2019 13:55:20 -0400 Subject: [PATCH 0806/3533] py/repl: Enter four spaces when there are no matches. Originally at adafruit#1859 Signed-off-by: scottbelden --- py/repl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/repl.c b/py/repl.c index 3b1837031b594..a8ae1d0695f81 100644 --- a/py/repl.c +++ b/py/repl.c @@ -228,6 +228,10 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print // nothing found if (q_first == 0) { + if (s_len == 0) { + *compl_str = " "; + return 4; + } // If there're no better alternatives, and if it's first word // in the line, try to complete "import". if (s_start == org_str) { From f85ea8d4fe12251100989287f1cfe14af8650217 Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Wed, 7 Apr 2021 13:09:39 -0400 Subject: [PATCH 0807/3533] py/repl: Refactor autocomplete to reduce nesting. Originally at adafruit#4548 Signed-off-by: Artyom Skrobov --- py/repl.c | 203 +++++++++++++++++++++++++++--------------------------- 1 file changed, 103 insertions(+), 100 deletions(-) diff --git a/py/repl.c b/py/repl.c index a8ae1d0695f81..b5ff145197d18 100644 --- a/py/repl.c +++ b/py/repl.c @@ -161,131 +161,134 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__); mp_obj_t dest[2]; + const char *s_start; + size_t s_len; + for (;;) { // get next word in string to complete - const char *s_start = str; + s_start = str; while (str < top && *str != '.') { ++str; } - size_t s_len = str - s_start; + s_len = str - s_start; - if (str < top) { - // a complete word, lookup in current object - qstr q = qstr_find_strn(s_start, s_len); - if (q == MP_QSTRnull) { - // lookup will fail - return 0; - } - mp_load_method_protected(obj, q, dest, true); - obj = dest[0]; // attribute, method, or MP_OBJ_NULL if nothing found + if (str == top) { + // end of string, do completion on this partial name + break; + } - if (obj == MP_OBJ_NULL) { - // lookup failed - return 0; - } + // a complete word, lookup in current object + qstr q = qstr_find_strn(s_start, s_len); + if (q == MP_QSTRnull) { + // lookup will fail + return 0; + } + mp_load_method_protected(obj, q, dest, true); + obj = dest[0]; // attribute, method, or MP_OBJ_NULL if nothing found - // skip '.' to move to next word - ++str; + if (obj == MP_OBJ_NULL) { + // lookup failed + return 0; + } - } else { - // end of string, do completion on this partial name + // skip '.' to move to next word + ++str; + } - // look for matches - const char *match_str = NULL; - size_t match_len = 0; - qstr q_first = 0, q_last = 0; - for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) { - size_t d_len; - const char *d_str = (const char *)qstr_data(q, &d_len); - // special case; filter out words that begin with underscore - // unless there's already a partial match - if (s_len == 0 && d_str[0] == '_') { - continue; - } - if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { - mp_load_method_protected(obj, q, dest, true); - if (dest[0] != MP_OBJ_NULL) { - if (match_str == NULL) { - match_str = d_str; - match_len = d_len; - } else { - // search for longest common prefix of match_str and d_str - // (assumes these strings are null-terminated) - for (size_t j = s_len; j <= match_len && j <= d_len; ++j) { - if (match_str[j] != d_str[j]) { - match_len = j; - break; - } - } - } - if (q_first == 0) { - q_first = q; + // look for matches + const char *match_str = NULL; + size_t match_len = 0; + qstr q_first = 0, q_last = 0; + for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) { + size_t d_len; + const char *d_str = (const char *)qstr_data(q, &d_len); + // special case; filter out words that begin with underscore + // unless there's already a partial match + if (s_len == 0 && d_str[0] == '_') { + continue; + } + if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { + mp_load_method_protected(obj, q, dest, true); + if (dest[0] != MP_OBJ_NULL) { + if (match_str == NULL) { + match_str = d_str; + match_len = d_len; + } else { + // search for longest common prefix of match_str and d_str + // (assumes these strings are null-terminated) + for (size_t j = s_len; j <= match_len && j <= d_len; ++j) { + if (match_str[j] != d_str[j]) { + match_len = j; + break; } - q_last = q; } } - } - - // nothing found - if (q_first == 0) { - if (s_len == 0) { - *compl_str = " "; - return 4; + if (q_first == 0) { + q_first = q; } - // If there're no better alternatives, and if it's first word - // in the line, try to complete "import". - if (s_start == org_str) { - static const char import_str[] = "import "; - if (memcmp(s_start, import_str, s_len) == 0) { - *compl_str = import_str + s_len; - return sizeof(import_str) - 1 - s_len; - } - } - - return 0; + q_last = q; } + } + } - // 1 match found, or multiple matches with a common prefix - if (q_first == q_last || match_len > s_len) { - *compl_str = match_str + s_len; - return match_len - s_len; + // nothing found + if (q_first == 0) { + if (s_len == 0) { + *compl_str = " "; + return 4; + } + // If there're no better alternatives, and if it's first word + // in the line, try to complete "import". + if (s_start == org_str) { + static const char import_str[] = "import "; + if (memcmp(s_start, import_str, s_len) == 0) { + *compl_str = import_str + s_len; + return sizeof(import_str) - 1 - s_len; } + } - // multiple matches found, print them out + return 0; + } - #define WORD_SLOT_LEN (16) - #define MAX_LINE_LEN (4 * WORD_SLOT_LEN) + // 1 match found, or multiple matches with a common prefix + if (q_first == q_last || match_len > s_len) { + *compl_str = match_str + s_len; + return match_len - s_len; + } - int line_len = MAX_LINE_LEN; // force a newline for first word - for (qstr q = q_first; q <= q_last; ++q) { - size_t d_len; - const char *d_str = (const char *)qstr_data(q, &d_len); - if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { - mp_load_method_protected(obj, q, dest, true); - if (dest[0] != MP_OBJ_NULL) { - int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len; - if (gap < 2) { - gap += WORD_SLOT_LEN; - } - if (line_len + gap + d_len <= MAX_LINE_LEN) { - // TODO optimise printing of gap? - for (int j = 0; j < gap; ++j) { - mp_print_str(print, " "); - } - mp_print_str(print, d_str); - line_len += gap + d_len; - } else { - mp_printf(print, "\n%s", d_str); - line_len = d_len; - } + // multiple matches found, print them out + + #define WORD_SLOT_LEN (16) + #define MAX_LINE_LEN (4 * WORD_SLOT_LEN) + + int line_len = MAX_LINE_LEN; // force a newline for first word + for (qstr q = q_first; q <= q_last; ++q) { + size_t d_len; + const char *d_str = (const char *)qstr_data(q, &d_len); + if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { + mp_load_method_protected(obj, q, dest, true); + if (dest[0] != MP_OBJ_NULL) { + int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len; + if (gap < 2) { + gap += WORD_SLOT_LEN; + } + if (line_len + gap + d_len <= MAX_LINE_LEN) { + // TODO optimise printing of gap? + for (int j = 0; j < gap; ++j) { + mp_print_str(print, " "); } + mp_print_str(print, d_str); + line_len += gap + d_len; + } else { + mp_printf(print, "\n%s", d_str); + line_len = d_len; } } - mp_print_str(print, "\n"); - - return (size_t)(-1); // indicate many matches } } + mp_print_str(print, "\n"); + + return (size_t)(-1); // indicate many matches } #endif // MICROPY_HELPER_REPL From 7556e01f14bb9f0ae32eff1d191985d302f1183e Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Sat, 3 Apr 2021 16:18:41 -0400 Subject: [PATCH 0808/3533] py/repl: Refactor autocomplete, extracting reusable parts. Originally at adafruit#4548 Signed-off-by: Artyom Skrobov --- py/repl.c | 152 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 86 insertions(+), 66 deletions(-) diff --git a/py/repl.c b/py/repl.c index b5ff145197d18..c8bb715e90dfe 100644 --- a/py/repl.c +++ b/py/repl.c @@ -143,6 +143,87 @@ bool mp_repl_continue_with_input(const char *input) { return false; } +STATIC bool test_qstr(mp_obj_t obj, qstr name) { + // try object member + mp_obj_t dest[2]; + mp_load_method_protected(obj, name, dest, true); + return dest[0] != MP_OBJ_NULL; +} + +STATIC const char *find_completions(const char *s_start, size_t s_len, + mp_obj_t obj, size_t *match_len, qstr *q_first, qstr *q_last) { + + const char *match_str = NULL; + *match_len = 0; + *q_first = *q_last = 0; + size_t nqstr = QSTR_TOTAL(); + for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) { + size_t d_len; + const char *d_str = (const char *)qstr_data(q, &d_len); + // special case; filter out words that begin with underscore + // unless there's already a partial match + if (s_len == 0 && d_str[0] == '_') { + continue; + } + if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { + if (test_qstr(obj, q)) { + if (match_str == NULL) { + match_str = d_str; + *match_len = d_len; + } else { + // search for longest common prefix of match_str and d_str + // (assumes these strings are null-terminated) + for (size_t j = s_len; j <= *match_len && j <= d_len; ++j) { + if (match_str[j] != d_str[j]) { + *match_len = j; + break; + } + } + } + if (*q_first == 0) { + *q_first = q; + } + *q_last = q; + } + } + } + return match_str; +} + +STATIC void print_completions(const mp_print_t *print, + const char *s_start, size_t s_len, + mp_obj_t obj, qstr q_first, qstr q_last) { + + #define WORD_SLOT_LEN (16) + #define MAX_LINE_LEN (4 * WORD_SLOT_LEN) + + int line_len = MAX_LINE_LEN; // force a newline for first word + for (qstr q = q_first; q <= q_last; ++q) { + size_t d_len; + const char *d_str = (const char *)qstr_data(q, &d_len); + if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { + if (test_qstr(obj, q)) { + int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len; + if (gap < 2) { + gap += WORD_SLOT_LEN; + } + if (line_len + gap + d_len <= MAX_LINE_LEN) { + // TODO optimise printing of gap? + for (int j = 0; j < gap; ++j) { + mp_print_str(print, " "); + } + mp_print_str(print, d_str); + line_len += gap + d_len; + } else { + mp_printf(print, "\n%s", d_str); + line_len = d_len; + } + } + } + } + mp_print_str(print, "\n"); +} + size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) { // scan backwards to find start of "a.b.c" chain const char *org_str = str; @@ -155,8 +236,6 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print } } - size_t nqstr = QSTR_TOTAL(); - // begin search in outer global dict which is accessed from __main__ mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__); mp_obj_t dest[2]; @@ -196,40 +275,10 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print } // look for matches - const char *match_str = NULL; - size_t match_len = 0; - qstr q_first = 0, q_last = 0; - for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) { - size_t d_len; - const char *d_str = (const char *)qstr_data(q, &d_len); - // special case; filter out words that begin with underscore - // unless there's already a partial match - if (s_len == 0 && d_str[0] == '_') { - continue; - } - if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { - mp_load_method_protected(obj, q, dest, true); - if (dest[0] != MP_OBJ_NULL) { - if (match_str == NULL) { - match_str = d_str; - match_len = d_len; - } else { - // search for longest common prefix of match_str and d_str - // (assumes these strings are null-terminated) - for (size_t j = s_len; j <= match_len && j <= d_len; ++j) { - if (match_str[j] != d_str[j]) { - match_len = j; - break; - } - } - } - if (q_first == 0) { - q_first = q; - } - q_last = q; - } - } - } + size_t match_len; + qstr q_first, q_last; + const char *match_str = + find_completions(s_start, s_len, obj, &match_len, &q_first, &q_last); // nothing found if (q_first == 0) { @@ -257,36 +306,7 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print } // multiple matches found, print them out - - #define WORD_SLOT_LEN (16) - #define MAX_LINE_LEN (4 * WORD_SLOT_LEN) - - int line_len = MAX_LINE_LEN; // force a newline for first word - for (qstr q = q_first; q <= q_last; ++q) { - size_t d_len; - const char *d_str = (const char *)qstr_data(q, &d_len); - if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { - mp_load_method_protected(obj, q, dest, true); - if (dest[0] != MP_OBJ_NULL) { - int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len; - if (gap < 2) { - gap += WORD_SLOT_LEN; - } - if (line_len + gap + d_len <= MAX_LINE_LEN) { - // TODO optimise printing of gap? - for (int j = 0; j < gap; ++j) { - mp_print_str(print, " "); - } - mp_print_str(print, d_str); - line_len += gap + d_len; - } else { - mp_printf(print, "\n%s", d_str); - line_len = d_len; - } - } - } - } - mp_print_str(print, "\n"); + print_completions(print, s_start, s_len, obj, q_first, q_last); return (size_t)(-1); // indicate many matches } From ca35c0059c8895839809f3e11873f96fc8cb4ab5 Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Sat, 3 Apr 2021 16:56:02 -0400 Subject: [PATCH 0809/3533] py/repl: Autocomplete builtin modules. Doing "import " will now complete/list built-in modules. Originally at adafruit#4548 and adafruit#4608 Signed-off-by: Artyom Skrobov --- ports/unix/coverage.c | 7 +++++++ py/repl.c | 34 +++++++++++++++++++++----------- tests/unix/extra_coverage.py.exp | 14 +++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 220666e381d6b..d5b5d8dd767fe 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -268,6 +268,13 @@ STATIC mp_obj_t extra_coverage(void) { size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str); mp_printf(&mp_plat_print, "%.*s\n", (int)len, str); + len = mp_repl_autocomplete("i", 1, &mp_plat_print, &str); + mp_printf(&mp_plat_print, "%.*s\n", (int)len, str); + mp_repl_autocomplete("import ", 7, &mp_plat_print, &str); + len = mp_repl_autocomplete("import ut", 9, &mp_plat_print, &str); + mp_printf(&mp_plat_print, "%.*s\n", (int)len, str); + mp_repl_autocomplete("import utime", 12, &mp_plat_print, &str); + mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0))); mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str); len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str); diff --git a/py/repl.c b/py/repl.c index c8bb715e90dfe..57bc21eff09f6 100644 --- a/py/repl.c +++ b/py/repl.c @@ -26,6 +26,7 @@ #include #include "py/obj.h" +#include "py/objmodule.h" #include "py/runtime.h" #include "py/builtin.h" #include "py/repl.h" @@ -144,10 +145,16 @@ bool mp_repl_continue_with_input(const char *input) { } STATIC bool test_qstr(mp_obj_t obj, qstr name) { - // try object member - mp_obj_t dest[2]; - mp_load_method_protected(obj, name, dest, true); - return dest[0] != MP_OBJ_NULL; + if (obj) { + // try object member + mp_obj_t dest[2]; + mp_load_method_protected(obj, name, dest, true); + return dest[0] != MP_OBJ_NULL; + } else { + // try builtin module + return mp_map_lookup((mp_map_t *)&mp_builtin_module_map, + MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP); + } } STATIC const char *find_completions(const char *s_start, size_t s_len, @@ -274,6 +281,12 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print ++str; } + // after "import", suggest built-in modules + static const char import_str[] = "import "; + if (len >= 7 && !memcmp(org_str, import_str, 7)) { + obj = MP_OBJ_NULL; + } + // look for matches size_t match_len; qstr q_first, q_last; @@ -282,21 +295,18 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print // nothing found if (q_first == 0) { - if (s_len == 0) { - *compl_str = " "; - return 4; - } // If there're no better alternatives, and if it's first word // in the line, try to complete "import". - if (s_start == org_str) { - static const char import_str[] = "import "; + if (s_start == org_str && s_len > 0) { if (memcmp(s_start, import_str, s_len) == 0) { *compl_str = import_str + s_len; return sizeof(import_str) - 1 - s_len; } } - - return 0; + if (q_first == 0) { + *compl_str = " "; + return s_len ? 0 : 4; + } } // 1 match found, or multiple matches with a common prefix diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index ab4d977741122..ea91813fc683f 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -27,6 +27,20 @@ RuntimeError: RuntimeError: # repl ame__ +mport + +builtins micropython _thread _uasyncio +btree cexample cmath cppexample +ffi framebuf gc math +termios uarray ubinascii ucollections +ucryptolib uctypes uerrno uhashlib +uheapq uio ujson umachine +uos urandom ure uselect +usocket ussl ustruct usys +utime utimeq uwebsocket uzlib +ime + +utime utimeq argv atexit byteorder exc_info exit getsizeof implementation maxsize From 3c918d0f5817a32acf28831017b0873771810f06 Mon Sep 17 00:00:00 2001 From: Jan Jurgen Griesfeller Date: Thu, 29 Apr 2021 09:26:38 +0200 Subject: [PATCH 0810/3533] rp2/boards: Add board definition for SparkFun Thing Plus RP2040. --- ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.cmake | 1 + ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.cmake create mode 100644 ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h diff --git a/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.cmake b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.cmake new file mode 100644 index 0000000000000..b9090bbcec433 --- /dev/null +++ b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.cmake @@ -0,0 +1 @@ +# cmake file for SparkFun Thing Plus RP2040 diff --git a/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h new file mode 100644 index 0000000000000..9749acd25adf8 --- /dev/null +++ b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h @@ -0,0 +1,3 @@ +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "SparkFun Thing Plus RP2040" +#define MICROPY_HW_FLASH_STORAGE_BYTES (15 * 1024 * 1024) From d80a037e6bdcc78fbda76ce04e9ded41f335c635 Mon Sep 17 00:00:00 2001 From: Jan Jurgen Griesfeller Date: Thu, 29 Apr 2021 11:26:38 +0200 Subject: [PATCH 0811/3533] rp2/boards: Add board definition for SparkFun Pro Micro board. --- ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.cmake | 1 + ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.cmake create mode 100644 ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h diff --git a/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.cmake b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.cmake new file mode 100644 index 0000000000000..6ac8d7a44640f --- /dev/null +++ b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.cmake @@ -0,0 +1 @@ +# cmake file for SparkFun Pro Micro RP2040 diff --git a/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h new file mode 100644 index 0000000000000..d6c8007ba01e7 --- /dev/null +++ b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h @@ -0,0 +1,3 @@ +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "SparkFun Pro Micro RP2040" +#define MICROPY_HW_FLASH_STORAGE_BYTES (15 * 1024 * 1024) From 1e2f0d2809c5431c9baf1d6f447162013f58baac Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 29 Apr 2021 16:56:28 +0200 Subject: [PATCH 0812/3533] rp2/tusb_port: Add the device unique-id to the USB id. The number shown in the USB id is now the same as that returned by machine.unique_id(). All 8 bytes are inserted as hex into the USB id. A usb id at /dev/serial/by-id then looks like: usb-MicroPython_Board_in_FS_mode_e469b03567342f37-if00 --- ports/rp2/tusb_port.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ports/rp2/tusb_port.c b/ports/rp2/tusb_port.c index 94856ab452702..874d837b9b5e5 100644 --- a/ports/rp2/tusb_port.c +++ b/ports/rp2/tusb_port.c @@ -25,6 +25,7 @@ */ #include "tusb.h" +#include "pico/unique_id.h" #define USBD_VID (0x2E8A) // Raspberry Pi #define USBD_PID (0x0005) // RP2 MicroPython @@ -77,7 +78,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { static const char *const usbd_desc_str[] = { [USBD_STR_MANUF] = "MicroPython", [USBD_STR_PRODUCT] = "Board in FS mode", - [USBD_STR_SERIAL] = "000000000000", // TODO + [USBD_STR_SERIAL] = NULL, // generated dynamically [USBD_STR_CDC] = "Board CDC", }; @@ -102,9 +103,21 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) { return NULL; } - const char *str = usbd_desc_str[index]; - for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) { - desc_str[1 + len] = str[len]; + // check, if serial is requested + if (index == USBD_STR_SERIAL) { + pico_unique_board_id_t id; + pico_get_unique_board_id(&id); + // byte by byte conversion + for (len = 0; len < 16; len += 2) { + const char *hexdig = "0123456789abcdef"; + desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4]; + desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f]; + } + } else { + const char *str = usbd_desc_str[index]; + for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) { + desc_str[1 + len] = str[len]; + } } } From 4154ffbcba48822338ece6bbdb42f4a0498f237e Mon Sep 17 00:00:00 2001 From: Chris Liechti Date: Tue, 8 Nov 2016 21:53:03 +0100 Subject: [PATCH 0813/3533] docs/esp8266: Add note about simultaneous use of STA_IF and AP_IF. See also https://github.com/esp8266/Arduino/issues/1624 --- docs/esp8266/general.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/esp8266/general.rst b/docs/esp8266/general.rst index fbe8fe1c3cabd..ce51f531b9541 100644 --- a/docs/esp8266/general.rst +++ b/docs/esp8266/general.rst @@ -125,6 +125,16 @@ will overflow every 7:45h. If a long-term working RTC time is required then ``time()`` or ``localtime()`` must be called at least once within 7 hours. MicroPython will then handle the overflow. +Simultaneous operation of STA_IF and AP_IF +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Simultaneous operation of STA_IF and AP_IF interfaces is supported. + +However, due to restrictions of the hardware, there may be performance +issues in the AP_IF, if the STA_IF is not connected and searching. +An application should manage these interfaces and for example +deactivate the STA_IF in environments where only the AP_IF is used. + Sockets and WiFi buffers overflow ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 2bf1beef5ce2edde80ba7a24dde781fe1b1692a4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 May 2021 14:24:17 +1000 Subject: [PATCH 0814/3533] docs/esp8266: Add instructions on entering programming mode manually. This adds to the ESP8266 tutorial instructions explaining which pins to pull low to enter programming mode. Commit made originally by @ARF1 in #2910. Signed-off-by: Damien George --- docs/esp8266/tutorial/intro.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/esp8266/tutorial/intro.rst b/docs/esp8266/tutorial/intro.rst index 711db3fcebb72..bbf0457fff5ab 100644 --- a/docs/esp8266/tutorial/intro.rst +++ b/docs/esp8266/tutorial/intro.rst @@ -75,6 +75,10 @@ the DTR and RTS pins wired in a special way then deploying the firmware should be easy as all steps can be done automatically. Boards that have such features include the Adafruit Feather HUZZAH and NodeMCU boards. +If you do not have such a board, you need keep GPIO0 pulled to ground and reset +the device by pulling the reset pin to ground and releasing it again to enter +programming mode. + For best results it is recommended to first erase the entire flash of your device before putting on new MicroPython firmware. @@ -113,6 +117,10 @@ the firmware (note the ``-fm dio`` option):: If the above commands run without error then MicroPython should be installed on your board! +If you pulled GPIO0 manually to ground to enter programming mode, release it +now and reset the device by again pulling the reset pin to ground for a short +duration. + Serial prompt ------------- From d70ab87b2b108aa9d5321c272911f759ad3ee8a9 Mon Sep 17 00:00:00 2001 From: Mordy Ovits Date: Thu, 2 Nov 2017 14:31:17 -0400 Subject: [PATCH 0815/3533] docs/esp8266: Clarify limitations of SSL in esp8266 and fix typos. --- docs/esp8266/general.rst | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/esp8266/general.rst b/docs/esp8266/general.rst index ce51f531b9541..4999bc2e9bb1a 100644 --- a/docs/esp8266/general.rst +++ b/docs/esp8266/general.rst @@ -163,25 +163,26 @@ SSL/TLS limitations ~~~~~~~~~~~~~~~~~~~ ESP8266 uses `axTLS `_ library, which is one -of the smallest TLS libraries with the compatible licensing. However, it +of the smallest TLS libraries with compatible licensing. However, it also has some known issues/limitations: 1. No support for Diffie-Hellman (DH) key exchange and Elliptic-curve - cryptography (ECC). This means it can't work with sites which force - the use of these features (it works ok with classic RSA certificates). + cryptography (ECC). This means it can't work with sites which require + the use of these features (it works ok with the typical sites that use + RSA certificates). 2. Half-duplex communication nature. axTLS uses a single buffer for both sending and receiving, which leads to considerable memory saving and works well with protocols like HTTP. But there may be problems with protocols which don't follow classic request-response model. -Besides axTLS own limitations, the configuration used for MicroPython is +Besides axTLS's own limitations, the configuration used for MicroPython is highly optimized for code size, which leads to additional limitations (these may be lifted in the future): 3. Optimized RSA algorithms are not enabled, which may lead to slow SSL handshakes. -4. Stored sessions are not supported (may allow faster repeated connections - to the same site in some circumstances). +4. Session Reuse is not enabled, which means every connection must undergo + the full, expensive SSL handshake. Besides axTLS specific limitations described above, there's another generic limitation with usage of TLS on the low-memory devices: @@ -195,13 +196,16 @@ limitation with usage of TLS on the low-memory devices: accessing various REST APIs, which usually require much smaller messages. The buffers size is on the order of 5KB, and is adjusted from time to time, taking as a reference being able to access https://google.com . - The smaller buffer hower means that some sites can't be accessed using - it, and it's not possible to stream large amounts of data. + The smaller buffer however means that some sites can't be accessed using + it, and it's not possible to stream large amounts of data. axTLS does + have support for TLS's Max Fragment Size extension, but no HTTPS website + does, so use of the extension is really only effective for local + communication with other devices. There are also some not implemented features specifically in MicroPython's ``ussl`` module based on axTLS: -6. Certificates are not validated (this may make connections susceptible +6. Certificates are not validated (this makes connections susceptible to man-in-the-middle attacks). 7. There is no support for client certificates (scheduled to be fixed in 1.9.4 release). From ca0c75f5042cdf602585b08184f132d7ab4713f3 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 30 Apr 2021 14:41:20 -0500 Subject: [PATCH 0816/3533] stm32/boards: Change default LSI_VALUE to 32000 for F4 MCUs. In the STM32 HAL libraries, the default value for LSI_VALUE for F4 MCUs is 32 kHz. Signed-off-by: David Lechner --- ports/stm32/boards/stm32f4xx_hal_conf_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/boards/stm32f4xx_hal_conf_base.h b/ports/stm32/boards/stm32f4xx_hal_conf_base.h index 134a30018ce36..f09990fd95e59 100644 --- a/ports/stm32/boards/stm32f4xx_hal_conf_base.h +++ b/ports/stm32/boards/stm32f4xx_hal_conf_base.h @@ -88,7 +88,7 @@ // Oscillator values in Hz #define HSI_VALUE (16000000) -#define LSI_VALUE (40000) +#define LSI_VALUE (32000) // SysTick has the highest priority #define TICK_INT_PRIORITY (0x00) From 6e776a671061130d86a300fcd72173d9b93f521a Mon Sep 17 00:00:00 2001 From: Nicko van Someren Date: Sun, 2 May 2021 18:12:58 -0600 Subject: [PATCH 0817/3533] gitignore: Ignore macOS desktop metadata files. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index c52f59eec74e6..7342489927888 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,7 @@ user.props # Generated rst files ###################### genrst/ + +# MacOS desktop metadata files +###################### +.DS_Store From 31ac410a4fbf18b38b6f4557045959140026680e Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 16:53:36 +1000 Subject: [PATCH 0818/3533] docs: Fix some spelling mistakes. --- docs/develop/cmodules.rst | 2 +- docs/esp32/quickref.rst | 2 +- docs/esp8266/quickref.rst | 2 +- docs/library/esp32.rst | 2 +- docs/library/machine.Timer.rst | 2 +- docs/library/machine.TimerWiPy.rst | 4 ++-- docs/library/network.rst | 2 +- docs/library/uctypes.rst | 2 +- docs/library/uio.rst | 8 ++++---- docs/library/uselect.rst | 4 ++-- docs/library/usocket.rst | 2 +- docs/library/utime.rst | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 346b3e03140de..38225e8686a7b 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -91,7 +91,7 @@ This simple module named ``cexample`` provides a single function ``cexample.add_ints(a, b)`` which adds the two integer args together and returns the result. It can be found in the MicroPython source tree `in the examples directory `_ -and has a source file and a Makefile fragment with content as descibed above:: +and has a source file and a Makefile fragment with content as described above:: micropython/ └──examples/ diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 30c9b3b95a9f9..c29688f000289 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -58,7 +58,7 @@ The :mod:`esp32` module:: import esp32 esp32.hall_sensor() # read the internal hall sensor - esp32.raw_temperature() # read the internal temperature of the MCU, in Farenheit + esp32.raw_temperature() # read the internal temperature of the MCU, in Fahrenheit esp32.ULP() # access to the Ultra-Low-Power Co-processor Note that the temperature sensor in the ESP32 will typically read higher than diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index a478b66581d93..b9a46ab112a0c 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -58,7 +58,7 @@ The :mod:`network` module:: wlan.scan() # scan for access points wlan.isconnected() # check if the station is connected to an AP wlan.connect('essid', 'password') # connect to an AP - wlan.config('mac') # get the interface's MAC adddress + wlan.config('mac') # get the interface's MAC address wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses ap = network.WLAN(network.AP_IF) # create access-point interface diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index f179a31ef65e3..1cfb304c14b88 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -162,7 +162,7 @@ used to transmit or receive many other types of digital signals:: The input to the RMT module is an 80MHz clock (in the future it may be able to configure the input clock but, for now, it's fixed). ``clock_div`` *divides* the clock input which determines the resolution of the RMT channel. The -numbers specificed in ``write_pulses`` are multiplied by the resolution to +numbers specified in ``write_pulses`` are multiplied by the resolution to define the pulses. ``clock_div`` is an 8-bit divider (0-255) and each pulse can be defined by diff --git a/docs/library/machine.Timer.rst b/docs/library/machine.Timer.rst index 9991d3aebc2fc..77a549b40b5e9 100644 --- a/docs/library/machine.Timer.rst +++ b/docs/library/machine.Timer.rst @@ -9,7 +9,7 @@ the most flexible and heterogeneous kind of hardware in MCUs and SoCs, differently greatly from a model to a model. MicroPython's Timer class defines a baseline operation of executing a callback with a given period (or once after some delay), and allow specific boards to define more -non-standard behavior (which thus won't be portable to other boards). +non-standard behaviour (which thus won't be portable to other boards). See discussion of :ref:`important constraints ` on Timer callbacks. diff --git a/docs/library/machine.TimerWiPy.rst b/docs/library/machine.TimerWiPy.rst index f5b748c62e1d5..39afc23bc529e 100644 --- a/docs/library/machine.TimerWiPy.rst +++ b/docs/library/machine.TimerWiPy.rst @@ -16,7 +16,7 @@ the most flexible and heterogeneous kind of hardware in MCUs and SoCs, differently greatly from a model to a model. MicroPython's Timer class defines a baseline operation of executing a callback with a given period (or once after some delay), and allow specific boards to define more -non-standard behavior (which thus won't be portable to other boards). +non-standard behaviour (which thus won't be portable to other boards). See discussion of :ref:`important constraints ` on Timer callbacks. @@ -115,7 +115,7 @@ Methods .. method:: timerchannel.irq(*, trigger, priority=1, handler=None) - The behavior of this callback is heavily dependent on the operating + The behaviour of this callback is heavily dependent on the operating mode of the timer channel: - If mode is ``TimerWiPy.PERIODIC`` the callback is executed periodically diff --git a/docs/library/network.rst b/docs/library/network.rst index bd3bc6f34ba81..a20eb2ebf5e0a 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -55,7 +55,7 @@ parameter should be `id`. Activate ("up") or deactivate ("down") the network interface, if a boolean argument is passed. Otherwise, query current state if no argument is provided. Most other methods require an active - interface (behavior of calling them on inactive interface is + interface (behaviour of calling them on inactive interface is undefined). .. method:: AbstractNIC.connect([service_id, key=None, *, ...]) diff --git a/docs/library/uctypes.rst b/docs/library/uctypes.rst index 0fdc40e4840b4..b3343beb7e4a8 100644 --- a/docs/library/uctypes.rst +++ b/docs/library/uctypes.rst @@ -245,7 +245,7 @@ Module contents .. data:: VOID - ``VOID`` is an alias for ``UINT8``, and is provided to conviniently define + ``VOID`` is an alias for ``UINT8``, and is provided to conveniently define C's void pointers: ``(uctypes.PTR, uctypes.VOID)``. .. data:: PTR diff --git a/docs/library/uio.rst b/docs/library/uio.rst index dddb83a170750..adbeef08b7b49 100644 --- a/docs/library/uio.rst +++ b/docs/library/uio.rst @@ -18,7 +18,7 @@ Conceptual hierarchy Conceptual hierarchy of stream base classes is simplified in MicroPython, as described in this section. -(Abstract) base stream classes, which serve as a foundation for behavior +(Abstract) base stream classes, which serve as a foundation for behaviour of all the concrete classes, adhere to few dichotomies (pair-wise classifications) in CPython. In MicroPython, they are somewhat simplified and made implicit to achieve higher efficiencies and save resources. @@ -41,15 +41,15 @@ more concise and efficient programs - something which is highly desirable for MicroPython. So, while MicroPython doesn't support buffered streams, it still provides for no-short-operations streams. Whether there will be short operations or not depends on each particular class' needs, but -developers are strongly advised to favor no-short-operations behavior +developers are strongly advised to favour no-short-operations behaviour for the reasons stated above. For example, MicroPython sockets are guaranteed to avoid short read/writes. Actually, at this time, there is no example of a short-operations stream class in the core, and one would be a port-specific class, where such a need is governed by hardware peculiarities. -The no-short-operations behavior gets tricky in case of non-blocking -streams, blocking vs non-blocking behavior being another CPython dichotomy, +The no-short-operations behaviour gets tricky in case of non-blocking +streams, blocking vs non-blocking behaviour being another CPython dichotomy, fully supported by MicroPython. Non-blocking streams never wait for data either to arrive or be written - they read/write whatever possible, or signal lack of data (or ability to write data). Clearly, this conflicts diff --git a/docs/library/uselect.rst b/docs/library/uselect.rst index 0c3bdfdfd9360..76202739c87e9 100644 --- a/docs/library/uselect.rst +++ b/docs/library/uselect.rst @@ -87,11 +87,11 @@ Methods `callee-owned tuple`. This function provides an efficient, allocation-free way to poll on streams. - If *flags* is 1, one-shot behavior for events is employed: streams for + If *flags* is 1, one-shot behaviour for events is employed: streams for which events happened will have their event masks automatically reset (equivalent to ``poll.modify(obj, 0)``), so new events for such a stream won't be processed until new mask is set with `poll.modify()`. This - behavior is useful for asynchronous I/O schedulers. + behaviour is useful for asynchronous I/O schedulers. .. admonition:: Difference to CPython :class: attention diff --git a/docs/library/usocket.rst b/docs/library/usocket.rst index bc4b4b6d5a952..39b848e593d75 100644 --- a/docs/library/usocket.rst +++ b/docs/library/usocket.rst @@ -222,7 +222,7 @@ Methods Unlike `send()`, this method will try to send all of data, by sending data chunk by chunk consecutively. - The behavior of this method on non-blocking sockets is undefined. Due to this, + The behaviour of this method on non-blocking sockets is undefined. Due to this, on MicroPython, it's recommended to use `write()` method instead, which has the same "no short writes" policy for blocking sockets, and will return number of bytes sent on non-blocking sockets. diff --git a/docs/library/utime.rst b/docs/library/utime.rst index 86fd27b3a5ac8..b7c604dc7b4ec 100644 --- a/docs/library/utime.rst +++ b/docs/library/utime.rst @@ -173,7 +173,7 @@ Functions long sleep), then once you finally look again, it may seem to you that only 1 hour has passed. To avoid this mistake, just look at the clock regularly. Your application should do the same. "Too long sleep" metaphor also maps directly to application - behavior: don't let your application run any single task for too long. Run tasks + behaviour: don't let your application run any single task for too long. Run tasks in steps, and do time-keeping inbetween. `ticks_diff()` is designed to accommodate various usage patterns, among them: From 0054fff840dfda50a32c653774e91b3e19464675 Mon Sep 17 00:00:00 2001 From: Gabriel M Schuyler Date: Sun, 7 Feb 2021 12:35:47 -0600 Subject: [PATCH 0819/3533] docs/pyboard: Fix typo in pyb.Switch tutorial. --- docs/pyboard/tutorial/switch.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pyboard/tutorial/switch.rst b/docs/pyboard/tutorial/switch.rst index 96bb3784e9f8c..059b04d94acc9 100644 --- a/docs/pyboard/tutorial/switch.rst +++ b/docs/pyboard/tutorial/switch.rst @@ -93,7 +93,7 @@ on the pin for any changes, and the following will occur: running Python script. 3. The microcontroller starts executing the special interrupt handler associated with the switch's external trigger. This interrupt handler - get the function that you registered with ``sw.callback()`` and executes + gets the function that you registered with ``sw.callback()`` and executes it. 4. Your callback function is executed until it finishes, returning control to the switch interrupt handler. From 8172c2e9c5d5eec9a6b0a3f6cc4a2383b3d96d26 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 May 2021 23:36:05 +1000 Subject: [PATCH 0820/3533] rp2: Move manifest.py to boards directory. To match other ports. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 2 +- ports/rp2/{ => boards}/manifest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename ports/rp2/{ => boards}/manifest.py (74%) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index bd3eb74384b0d..699520b240849 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -159,7 +159,7 @@ set(PICO_SDK_COMPONENTS # Define mpy-cross flags and frozen manifest set(MICROPY_CROSS_FLAGS -march=armv7m) -set(MICROPY_FROZEN_MANIFEST ${PROJECT_SOURCE_DIR}/manifest.py) +set(MICROPY_FROZEN_MANIFEST ${PROJECT_SOURCE_DIR}/boards/manifest.py) target_sources(${MICROPY_TARGET} PRIVATE ${MICROPY_SOURCE_PY} diff --git a/ports/rp2/manifest.py b/ports/rp2/boards/manifest.py similarity index 74% rename from ports/rp2/manifest.py rename to ports/rp2/boards/manifest.py index a56d4b1b0923b..9df589f126787 100644 --- a/ports/rp2/manifest.py +++ b/ports/rp2/boards/manifest.py @@ -1,3 +1,3 @@ -freeze("modules") +freeze("$(PORT_DIR)/modules") freeze("$(MPY_DIR)/drivers/onewire") include("$(MPY_DIR)/extmod/uasyncio/manifest.py") From 9e29217c73f36802de616c05bee9bf7f9090f722 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Apr 2021 15:37:33 +1000 Subject: [PATCH 0821/3533] unix/modffi: Use a union for passing/returning FFI values. This fixes a bug where double arguments on a 32-bit architecture would not be passed correctly because they only had 4 bytes of storage (not 8). It also fixes a compiler warning/error in return_ffi_value on certian architectures: array subscript 'double[0]' is partly outside array bounds of 'ffi_arg[1]' {aka 'long unsigned int[1]'}. Fixes issue #7064. Signed-off-by: Damien George --- ports/unix/modffi.c | 62 +++++++++++++++---------------------- tests/unix/ffi_float.py | 13 ++++++-- tests/unix/ffi_float.py.exp | 20 ++++++++---- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 598a28cd54a02..5c2595ec5e0ba 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -56,6 +56,13 @@ * but may be later. */ +// This union is large enough to hold any supported argument/return value. +typedef union _ffi_union_t { + ffi_arg ffi; + float flt; + double dbl; +} ffi_union_t; + typedef struct _mp_obj_opaque_t { mp_obj_base_t base; void *val; @@ -151,10 +158,10 @@ STATIC ffi_type *get_ffi_type(mp_obj_t o_in) { mp_raise_TypeError(MP_ERROR_TEXT("unknown type")); } -STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) { +STATIC mp_obj_t return_ffi_value(ffi_union_t *val, char type) { switch (type) { case 's': { - const char *s = (const char *)(intptr_t)val; + const char *s = (const char *)(intptr_t)val->ffi; if (!s) { return mp_const_none; } @@ -164,20 +171,16 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) { return mp_const_none; #if MICROPY_PY_BUILTINS_FLOAT case 'f': { - union { ffi_arg ffi; - float flt; - } val_union = { .ffi = val }; - return mp_obj_new_float_from_f(val_union.flt); + return mp_obj_new_float_from_f(val->flt); } case 'd': { - double *p = (double *)&val; - return mp_obj_new_float_from_d(*p); + return mp_obj_new_float_from_d(val->dbl); } #endif case 'O': - return (mp_obj_t)(intptr_t)val; + return (mp_obj_t)(intptr_t)val->ffi; default: - return mp_obj_new_int(val); + return mp_obj_new_int(val->ffi); } } @@ -368,28 +371,26 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const assert(n_kw == 0); assert(n_args == self->cif.nargs); - ffi_arg values[n_args]; + ffi_union_t values[n_args]; void *valueptrs[n_args]; const char *argtype = self->argtypes; for (uint i = 0; i < n_args; i++, argtype++) { mp_obj_t a = args[i]; if (*argtype == 'O') { - values[i] = (ffi_arg)(intptr_t)a; + values[i].ffi = (ffi_arg)(intptr_t)a; #if MICROPY_PY_BUILTINS_FLOAT } else if (*argtype == 'f') { - float *p = (float *)&values[i]; - *p = mp_obj_get_float_to_f(a); + values[i].flt = mp_obj_get_float_to_f(a); } else if (*argtype == 'd') { - double *p = (double *)&values[i]; - *p = mp_obj_get_float_to_d(a); + values[i].dbl = mp_obj_get_float_to_d(a); #endif } else if (a == mp_const_none) { - values[i] = 0; + values[i].ffi = 0; } else if (mp_obj_is_int(a)) { - values[i] = mp_obj_int_get_truncated(a); + values[i].ffi = mp_obj_int_get_truncated(a); } else if (mp_obj_is_str(a)) { const char *s = mp_obj_str_get_str(a); - values[i] = (ffi_arg)(intptr_t)s; + values[i].ffi = (ffi_arg)(intptr_t)s; } else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer_p.get_buffer != NULL) { mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a); mp_buffer_info_t bufinfo; @@ -397,32 +398,19 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const if (ret != 0) { goto error; } - values[i] = (ffi_arg)(intptr_t)bufinfo.buf; + values[i].ffi = (ffi_arg)(intptr_t)bufinfo.buf; } else if (mp_obj_is_type(a, &fficallback_type)) { mp_obj_fficallback_t *p = MP_OBJ_TO_PTR(a); - values[i] = (ffi_arg)(intptr_t)p->func; + values[i].ffi = (ffi_arg)(intptr_t)p->func; } else { goto error; } valueptrs[i] = &values[i]; } - // If ffi_arg is not big enough to hold a double, then we must pass along a - // pointer to a memory location of the correct size. - // TODO check if this needs to be done for other types which don't fit into - // ffi_arg. - #if MICROPY_PY_BUILTINS_FLOAT - if (sizeof(ffi_arg) == 4 && self->rettype == 'd') { - double retval; - ffi_call(&self->cif, self->func, &retval, valueptrs); - return mp_obj_new_float_from_d(retval); - } else - #endif - { - ffi_arg retval; - ffi_call(&self->cif, self->func, &retval, valueptrs); - return return_ffi_value(retval, self->rettype); - } + ffi_union_t retval; + ffi_call(&self->cif, self->func, &retval, valueptrs); + return return_ffi_value(&retval, self->rettype); error: mp_raise_TypeError(MP_ERROR_TEXT("don't know how to pass object to native function")); diff --git a/tests/unix/ffi_float.py b/tests/unix/ffi_float.py index d0393989651b3..03bd9f7f17b82 100644 --- a/tests/unix/ffi_float.py +++ b/tests/unix/ffi_float.py @@ -35,6 +35,15 @@ def ffi_open(names): # test passing double and float args libm = ffi_open(("libm.so", "libm.so.6", "libc.so.0", "libc.so.6", "libc.dylib")) tgamma = libm.func("d", "tgamma", "d") -for fun in (tgamma,): +for fun_name in ("tgamma",): + fun = globals()[fun_name] for val in (0.5, 1, 1.0, 1.5, 4, 4.0): - print("%.6f" % fun(val)) + print(fun_name, "%.5f" % fun(val)) + +# test passing 2x float/double args +powf = libm.func("f", "powf", "ff") +pow = libm.func("d", "pow", "dd") +for fun_name in ("powf", "pow"): + fun = globals()[fun_name] + for args in ((0, 1), (1, 0), (2, 0.5), (3, 4)): + print(fun_name, "%.5f" % fun(*args)) diff --git a/tests/unix/ffi_float.py.exp b/tests/unix/ffi_float.py.exp index b9d7da2bdb99c..3d90914315901 100644 --- a/tests/unix/ffi_float.py.exp +++ b/tests/unix/ffi_float.py.exp @@ -1,8 +1,16 @@ 1.230000 1.230000 -1.772454 -1.000000 -1.000000 -0.886227 -6.000000 -6.000000 +tgamma 1.77245 +tgamma 1.00000 +tgamma 1.00000 +tgamma 0.88623 +tgamma 6.00000 +tgamma 6.00000 +powf 0.00000 +powf 1.00000 +powf 1.41421 +powf 81.00000 +pow 0.00000 +pow 1.00000 +pow 1.41421 +pow 81.00000 From 4791d290c61e2cc5bcd006b35ef60470920279d6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 12:11:51 +1000 Subject: [PATCH 0822/3533] extmod: Remove old comments used for auto-doc generation. They are no longer used, and the text in the docs is more up to date. Signed-off-by: Damien George --- extmod/moductypes.c | 70 +++++++-------------------------------------- extmod/moduselect.c | 16 ++++------- extmod/vfs_fat.c | 7 ++--- 3 files changed, 18 insertions(+), 75 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 79a49d5c329c2..51626600ccdbd 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -34,38 +34,10 @@ #if MICROPY_PY_UCTYPES -/// \module uctypes - Access data structures in memory -/// -/// The module allows to define layout of raw data structure (using terms -/// of C language), and then access memory buffers using this definition. -/// The module also provides convenience functions to access memory buffers -/// contained in Python objects or wrap memory buffers in Python objects. -/// \constant UINT8_1 - uint8_t value type - -/// \class struct - C-like structure -/// -/// Encapsulalation of in-memory data structure. This class doesn't define -/// any methods, only attribute access (for structure fields) and -/// indexing (for pointer and array fields). -/// -/// Usage: -/// -/// # Define layout of a structure with 2 fields -/// # 0 and 4 are byte offsets of fields from the beginning of struct -/// # they are logically ORed with field type -/// FOO_STRUCT = {"a": 0 | uctypes.UINT32, "b": 4 | uctypes.UINT8} -/// -/// # Example memory buffer to access (contained in bytes object) -/// buf = b"\x64\0\0\0\0x14" -/// -/// # Create structure object referring to address of -/// # the data in the buffer above -/// s = uctypes.struct(FOO_STRUCT, uctypes.addressof(buf)) -/// -/// # Access fields -/// print(s.a, s.b) -/// # Result: -/// # 100, 20 +// The uctypes module allows defining the layout of a raw data structure (using +// terms of the C language), and then access memory buffers using this definition. +// The module also provides convenience functions to access memory buffers +// contained in Python objects or wrap memory buffers in Python objects. #define LAYOUT_LITTLE_ENDIAN (0) #define LAYOUT_BIG_ENDIAN (1) @@ -647,9 +619,8 @@ STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, return 0; } -/// \function addressof() -/// Return address of object's data (applies to object providing buffer -/// interface). +// addressof() +// Return address of object's data (applies to objects providing the buffer interface). STATIC mp_obj_t uctypes_struct_addressof(mp_obj_t buf) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); @@ -657,19 +628,15 @@ STATIC mp_obj_t uctypes_struct_addressof(mp_obj_t buf) { } MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof); -/// \function bytearray_at() -/// Capture memory at given address of given size as bytearray. Memory is -/// captured by reference (and thus memory pointed by bytearray may change -/// or become invalid at later time). Use bytes_at() to capture by value. +// bytearray_at() +// Capture memory at given address of given size as bytearray. STATIC mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) { return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void *)(uintptr_t)mp_obj_int_get_truncated(ptr)); } MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at); -/// \function bytes_at() -/// Capture memory at given address of given size as bytes. Memory is -/// captured by value, i.e. copied. Use bytearray_at() to capture by reference -/// ("zero copy"). +// bytes_at() +// Capture memory at given address of given size as bytes. STATIC mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) { return mp_obj_new_bytes((void *)(uintptr_t)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size)); } @@ -695,36 +662,19 @@ STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_bytes_at), MP_ROM_PTR(&uctypes_struct_bytes_at_obj) }, { MP_ROM_QSTR(MP_QSTR_bytearray_at), MP_ROM_PTR(&uctypes_struct_bytearray_at_obj) }, - /// \moduleref uctypes - - /// \constant NATIVE - Native structure layout - native endianness, - /// platform-specific field alignment { MP_ROM_QSTR(MP_QSTR_NATIVE), MP_ROM_INT(LAYOUT_NATIVE) }, - /// \constant LITTLE_ENDIAN - Little-endian structure layout, tightly packed - /// (no alignment constraints) { MP_ROM_QSTR(MP_QSTR_LITTLE_ENDIAN), MP_ROM_INT(LAYOUT_LITTLE_ENDIAN) }, - /// \constant BIG_ENDIAN - Big-endian structure layout, tightly packed - /// (no alignment constraints) { MP_ROM_QSTR(MP_QSTR_BIG_ENDIAN), MP_ROM_INT(LAYOUT_BIG_ENDIAN) }, - /// \constant VOID - void value type, may be used only as pointer target type. { MP_ROM_QSTR(MP_QSTR_VOID), MP_ROM_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) }, - /// \constant UINT8 - uint8_t value type { MP_ROM_QSTR(MP_QSTR_UINT8), MP_ROM_INT(TYPE2SMALLINT(UINT8, 4)) }, - /// \constant INT8 - int8_t value type { MP_ROM_QSTR(MP_QSTR_INT8), MP_ROM_INT(TYPE2SMALLINT(INT8, 4)) }, - /// \constant UINT16 - uint16_t value type { MP_ROM_QSTR(MP_QSTR_UINT16), MP_ROM_INT(TYPE2SMALLINT(UINT16, 4)) }, - /// \constant INT16 - int16_t value type { MP_ROM_QSTR(MP_QSTR_INT16), MP_ROM_INT(TYPE2SMALLINT(INT16, 4)) }, - /// \constant UINT32 - uint32_t value type { MP_ROM_QSTR(MP_QSTR_UINT32), MP_ROM_INT(TYPE2SMALLINT(UINT32, 4)) }, - /// \constant INT32 - int32_t value type { MP_ROM_QSTR(MP_QSTR_INT32), MP_ROM_INT(TYPE2SMALLINT(INT32, 4)) }, - /// \constant UINT64 - uint64_t value type { MP_ROM_QSTR(MP_QSTR_UINT64), MP_ROM_INT(TYPE2SMALLINT(UINT64, 4)) }, - /// \constant INT64 - int64_t value type { MP_ROM_QSTR(MP_QSTR_INT64), MP_ROM_INT(TYPE2SMALLINT(INT64, 4)) }, { MP_ROM_QSTR(MP_QSTR_BFUINT8), MP_ROM_INT(TYPE2SMALLINT(BFUINT8, 4)) }, diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 6d8249f42758f..fbd51960d5e57 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -40,10 +40,6 @@ // Flags for poll() #define FLAG_ONESHOT (1) -/// \module select - Provides select function to wait for events on a stream -/// -/// This module provides the select function. - typedef struct _poll_obj_t { mp_obj_t obj; mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode); @@ -111,7 +107,7 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, size_t *rwx_num) { return n_ready; } -/// \function select(rlist, wlist, xlist[, timeout]) +// select(rlist, wlist, xlist[, timeout]) STATIC mp_obj_t select_select(size_t n_args, const mp_obj_t *args) { // get array data from tuple/list arguments size_t rwx_len[3]; @@ -178,8 +174,6 @@ STATIC mp_obj_t select_select(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); -/// \class Poll - poll class - typedef struct _mp_obj_poll_t { mp_obj_base_t base; mp_map_t poll_map; @@ -190,7 +184,7 @@ typedef struct _mp_obj_poll_t { mp_obj_t ret_tuple; } mp_obj_poll_t; -/// \method register(obj[, eventmask]) +// register(obj[, eventmask]) STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) { mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); mp_uint_t flags; @@ -204,7 +198,7 @@ STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register); -/// \method unregister(obj) +// unregister(obj) STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP_REMOVE_IF_FOUND); @@ -213,7 +207,7 @@ STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { } MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister); -/// \method modify(obj, eventmask) +// modify(obj, eventmask) STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) { mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP); @@ -348,7 +342,7 @@ STATIC const mp_obj_type_t mp_type_poll = { .locals_dict = (void *)&poll_locals_dict, }; -/// \function poll() +// poll() STATIC mp_obj_t select_poll(void) { mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t); poll->base.type = &mp_type_poll; diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 95b7ad9944116..644be57ae7731 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -256,7 +256,7 @@ STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir); -/// Change current directory. +// Change current directory. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path; @@ -272,7 +272,7 @@ STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir); -/// Get the current directory. +// Get the current directory. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); char buf[MICROPY_ALLOC_PATH_MAX + 1]; @@ -284,8 +284,7 @@ STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd); -/// \function stat(path) -/// Get the status of a file or directory. +// Get the status of a file or directory. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_in); From 02dc1644b6ebf1bfd5bfa2dde4d68d7272526f51 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 12:31:32 +1000 Subject: [PATCH 0823/3533] extmod/moductypes: Remove double blank lines and debugging printf's. Signed-off-by: Damien George --- extmod/moductypes.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 51626600ccdbd..f1108649aa594 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -391,10 +391,8 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(deref); mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS); offset &= VALUE_MASK(VAL_TYPE_BITS); -// printf("scalar type=%d offset=%x\n", val_type, offset); if (val_type <= INT64 || val_type == FLOAT32 || val_type == FLOAT64) { -// printf("size=%d\n", GET_SCALAR_SIZE(val_type)); if (self->flags == LAYOUT_NATIVE) { if (set_val == MP_OBJ_NULL) { return get_aligned(val_type, self->addr + offset, 0); @@ -461,7 +459,6 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(sub->items[0]); mp_uint_t agg_type = GET_TYPE(offset, AGG_TYPE_BITS); offset &= VALUE_MASK(AGG_TYPE_BITS); -// printf("agg type=%d offset=%x\n", agg_type, offset); switch (agg_type) { case STRUCT: { @@ -486,7 +483,6 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set o->desc = MP_OBJ_FROM_PTR(sub); o->addr = self->addr + offset; o->flags = self->flags; -// printf("PTR/ARR base addr=%p\n", o->addr); return MP_OBJ_FROM_PTR(o); } } @@ -642,7 +638,6 @@ STATIC mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) { } MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at); - STATIC const mp_obj_type_t uctypes_struct_type = { { &mp_type_type }, .name = MP_QSTR_struct, @@ -719,7 +714,6 @@ STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PTR), MP_ROM_INT(TYPE2SMALLINT(PTR, AGG_TYPE_BITS)) }, { MP_ROM_QSTR(MP_QSTR_ARRAY), MP_ROM_INT(TYPE2SMALLINT(ARRAY, AGG_TYPE_BITS)) }, }; - STATIC MP_DEFINE_CONST_DICT(mp_module_uctypes_globals, mp_module_uctypes_globals_table); const mp_obj_module_t mp_module_uctypes = { From 350a66a86339a66bfad29e0e8a5a40a34151bfe4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 12:40:53 +1000 Subject: [PATCH 0824/3533] extmod/moductypes: Replace numbers with macro constants. Signed-off-by: Damien George --- extmod/moductypes.c | 67 +++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index f1108649aa594..97e8d7a787928 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -47,6 +47,7 @@ #define BITF_LEN_BITS 5 #define BITF_OFF_BITS 5 #define OFFSET_BITS 17 +#define LEN_BITS (OFFSET_BITS + BITF_OFF_BITS) #if VAL_TYPE_BITS + BITF_LEN_BITS + BITF_OFF_BITS + OFFSET_BITS != 31 #error Invalid encoding field length #endif @@ -409,9 +410,9 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set } } } else if (val_type >= BFUINT8 && val_type <= BFINT32) { - uint bit_offset = (offset >> 17) & 31; - uint bit_len = (offset >> 22) & 31; - offset &= (1 << 17) - 1; + uint bit_offset = (offset >> OFFSET_BITS) & 31; + uint bit_len = (offset >> LEN_BITS) & 31; + offset &= (1 << OFFSET_BITS) - 1; mp_uint_t val; if (self->flags == LAYOUT_NATIVE) { val = get_aligned_basic(val_type & 6, self->addr + offset); @@ -663,51 +664,51 @@ STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_VOID), MP_ROM_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) }, - { MP_ROM_QSTR(MP_QSTR_UINT8), MP_ROM_INT(TYPE2SMALLINT(UINT8, 4)) }, - { MP_ROM_QSTR(MP_QSTR_INT8), MP_ROM_INT(TYPE2SMALLINT(INT8, 4)) }, - { MP_ROM_QSTR(MP_QSTR_UINT16), MP_ROM_INT(TYPE2SMALLINT(UINT16, 4)) }, - { MP_ROM_QSTR(MP_QSTR_INT16), MP_ROM_INT(TYPE2SMALLINT(INT16, 4)) }, - { MP_ROM_QSTR(MP_QSTR_UINT32), MP_ROM_INT(TYPE2SMALLINT(UINT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_INT32), MP_ROM_INT(TYPE2SMALLINT(INT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_UINT64), MP_ROM_INT(TYPE2SMALLINT(UINT64, 4)) }, - { MP_ROM_QSTR(MP_QSTR_INT64), MP_ROM_INT(TYPE2SMALLINT(INT64, 4)) }, - - { MP_ROM_QSTR(MP_QSTR_BFUINT8), MP_ROM_INT(TYPE2SMALLINT(BFUINT8, 4)) }, - { MP_ROM_QSTR(MP_QSTR_BFINT8), MP_ROM_INT(TYPE2SMALLINT(BFINT8, 4)) }, - { MP_ROM_QSTR(MP_QSTR_BFUINT16), MP_ROM_INT(TYPE2SMALLINT(BFUINT16, 4)) }, - { MP_ROM_QSTR(MP_QSTR_BFINT16), MP_ROM_INT(TYPE2SMALLINT(BFINT16, 4)) }, - { MP_ROM_QSTR(MP_QSTR_BFUINT32), MP_ROM_INT(TYPE2SMALLINT(BFUINT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_BFINT32), MP_ROM_INT(TYPE2SMALLINT(BFINT32, 4)) }, - - { MP_ROM_QSTR(MP_QSTR_BF_POS), MP_ROM_INT(17) }, - { MP_ROM_QSTR(MP_QSTR_BF_LEN), MP_ROM_INT(22) }, + { MP_ROM_QSTR(MP_QSTR_UINT8), MP_ROM_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_INT8), MP_ROM_INT(TYPE2SMALLINT(INT8, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_UINT16), MP_ROM_INT(TYPE2SMALLINT(UINT16, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_INT16), MP_ROM_INT(TYPE2SMALLINT(INT16, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_UINT32), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_INT32), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_UINT64), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_INT64), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) }, + + { MP_ROM_QSTR(MP_QSTR_BFUINT8), MP_ROM_INT(TYPE2SMALLINT(BFUINT8, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_BFINT8), MP_ROM_INT(TYPE2SMALLINT(BFINT8, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_BFUINT16), MP_ROM_INT(TYPE2SMALLINT(BFUINT16, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_BFINT16), MP_ROM_INT(TYPE2SMALLINT(BFINT16, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_BFUINT32), MP_ROM_INT(TYPE2SMALLINT(BFUINT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_BFINT32), MP_ROM_INT(TYPE2SMALLINT(BFINT32, VAL_TYPE_BITS)) }, + + { MP_ROM_QSTR(MP_QSTR_BF_POS), MP_ROM_INT(OFFSET_BITS) }, + { MP_ROM_QSTR(MP_QSTR_BF_LEN), MP_ROM_INT(LEN_BITS) }, #if MICROPY_PY_BUILTINS_FLOAT - { MP_ROM_QSTR(MP_QSTR_FLOAT32), MP_ROM_INT(TYPE2SMALLINT(FLOAT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_FLOAT64), MP_ROM_INT(TYPE2SMALLINT(FLOAT64, 4)) }, + { MP_ROM_QSTR(MP_QSTR_FLOAT32), MP_ROM_INT(TYPE2SMALLINT(FLOAT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_FLOAT64), MP_ROM_INT(TYPE2SMALLINT(FLOAT64, VAL_TYPE_BITS)) }, #endif #if MICROPY_PY_UCTYPES_NATIVE_C_TYPES // C native type aliases. These depend on GCC-compatible predefined // preprocessor macros. #if __SIZEOF_SHORT__ == 2 - { MP_ROM_QSTR(MP_QSTR_SHORT), MP_ROM_INT(TYPE2SMALLINT(INT16, 4)) }, - { MP_ROM_QSTR(MP_QSTR_USHORT), MP_ROM_INT(TYPE2SMALLINT(UINT16, 4)) }, + { MP_ROM_QSTR(MP_QSTR_SHORT), MP_ROM_INT(TYPE2SMALLINT(INT16, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_USHORT), MP_ROM_INT(TYPE2SMALLINT(UINT16, VAL_TYPE_BITS)) }, #endif #if __SIZEOF_INT__ == 4 - { MP_ROM_QSTR(MP_QSTR_INT), MP_ROM_INT(TYPE2SMALLINT(INT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_UINT), MP_ROM_INT(TYPE2SMALLINT(UINT32, 4)) }, + { MP_ROM_QSTR(MP_QSTR_INT), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_UINT), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) }, #endif #if __SIZEOF_LONG__ == 4 - { MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT32, 4)) }, - { MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT32, 4)) }, + { MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) }, #elif __SIZEOF_LONG__ == 8 - { MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT64, 4)) }, - { MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, 4)) }, + { MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) }, #endif #if __SIZEOF_LONG_LONG__ == 8 - { MP_ROM_QSTR(MP_QSTR_LONGLONG), MP_ROM_INT(TYPE2SMALLINT(INT64, 4)) }, - { MP_ROM_QSTR(MP_QSTR_ULONGLONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, 4)) }, + { MP_ROM_QSTR(MP_QSTR_LONGLONG), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) }, + { MP_ROM_QSTR(MP_QSTR_ULONGLONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) }, #endif #endif // MICROPY_PY_UCTYPES_NATIVE_C_TYPES From 47583d8cbd251d9cb3b0f4a70c5594d1329ad930 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 13:11:33 +1000 Subject: [PATCH 0825/3533] extmod/moductypes: Fix size and offset calculation for ARRAY of FLOAT32. uctypes.FLOAT32 has a special value representation and uctypes_struct_scalar_size() should be used instead of GET_SCALAR_SIZE(). Signed-off-by: Damien George --- extmod/moductypes.c | 4 ++-- tests/extmod/uctypes_le_float.py | 16 ++++++++++++++++ tests/extmod/uctypes_le_float.py.exp | 2 ++ tests/extmod/uctypes_sizeof_float.py | 2 ++ tests/extmod/uctypes_sizeof_float.py.exp | 2 ++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 97e8d7a787928..58f0adfa4ba34 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -164,7 +164,7 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_ mp_uint_t item_s; if (t->len == 2) { // Elements of array are scalar - item_s = GET_SCALAR_SIZE(val_type); + item_s = uctypes_struct_scalar_size(val_type); if (item_s > *max_field_size) { *max_field_size = item_s; } @@ -541,7 +541,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob return value; // just !MP_OBJ_NULL } } else { - byte *p = self->addr + GET_SCALAR_SIZE(val_type) * index; + byte *p = self->addr + uctypes_struct_scalar_size(val_type) * index; if (value == MP_OBJ_SENTINEL) { return get_unaligned(val_type, p, self->flags); } else { diff --git a/tests/extmod/uctypes_le_float.py b/tests/extmod/uctypes_le_float.py index 89e9a9e0abe48..5255632c912d7 100644 --- a/tests/extmod/uctypes_le_float.py +++ b/tests/extmod/uctypes_le_float.py @@ -22,3 +22,19 @@ S.uf64 = 12.34 print("%.4f" % S.uf64) + +# array of float/double +desc = { + "af32": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2), + "af64": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2), +} +data = bytearray(16) +S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) + +S.af32[0] = 1 +S.af32[1] = 2 +print("%.4f %.4f" % (S.af32[0], S.af32[1]), data) + +S.af64[0] = 1 +S.af64[1] = 2 +print("%.4f %.4f" % (S.af64[0], S.af64[1]), data) diff --git a/tests/extmod/uctypes_le_float.py.exp b/tests/extmod/uctypes_le_float.py.exp index a35a1da2dc134..e93fcb0d97beb 100644 --- a/tests/extmod/uctypes_le_float.py.exp +++ b/tests/extmod/uctypes_le_float.py.exp @@ -1,3 +1,5 @@ 12.3400 12.3400 12.3400 +1.0000 2.0000 bytearray(b'\x00\x00\x80?\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00') +1.0000 2.0000 bytearray(b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@') diff --git a/tests/extmod/uctypes_sizeof_float.py b/tests/extmod/uctypes_sizeof_float.py index 351632d76bbf5..f1a4c88e2d81d 100644 --- a/tests/extmod/uctypes_sizeof_float.py +++ b/tests/extmod/uctypes_sizeof_float.py @@ -6,3 +6,5 @@ print(uctypes.sizeof({"f": uctypes.FLOAT32})) print(uctypes.sizeof({"f": uctypes.FLOAT64})) +print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2)})) +print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2)})) diff --git a/tests/extmod/uctypes_sizeof_float.py.exp b/tests/extmod/uctypes_sizeof_float.py.exp index de78180725a82..82776b54ab5a0 100644 --- a/tests/extmod/uctypes_sizeof_float.py.exp +++ b/tests/extmod/uctypes_sizeof_float.py.exp @@ -1,2 +1,4 @@ 4 8 +8 +16 From b98197f950b572aff2a34934e64d094e5cea6cd5 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:05:33 +1000 Subject: [PATCH 0826/3533] docs/esp32: Add UART to quickref. --- docs/esp32/quickref.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index c29688f000289..2a9c8397c899a 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -171,6 +171,33 @@ Notes: * The pull value of some pins can be set to ``Pin.PULL_HOLD`` to reduce power consumption during deepsleep. +UART (serial bus) +----------------- + +See :ref:`machine.UART `. :: + + from machine import UART + + uart1 = UART(1, baudrate=9600, tx=33, rx=32) + uart1.write('hello') # write 5 bytes + uart1.read(5) # read up to 5 bytes + +The ESP32 has three hardware UARTs: UART0, UART1 and UART2. +They each have default GPIO assigned to them, however depending on your +ESP32 variant and board, these pins may conflict with embedded flash, +onboard PSRAM or peripherals. + +Any GPIO can be used for hardware UARTs using the GPIO matrix, so to avoid +conflicts simply provide ``tx`` and ``rx`` pins when constructing. The default +pins listed below. + +===== ===== ===== ===== +\ UART0 UART1 UART2 +===== ===== ===== ===== +tx 1 10 17 +rx 3 9 16 +===== ===== ===== ===== + PWM (pulse width modulation) ---------------------------- From a65942a41d5bedd80b0098b1578680e72bef384b Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:03:16 +1000 Subject: [PATCH 0827/3533] docs/esp32: Add WDT to quickref. --- docs/esp32/quickref.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 2a9c8397c899a..5e70275830d81 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -384,6 +384,17 @@ See :ref:`machine.RTC ` :: rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time rtc.datetime() # get date and time +WDT (Watchdog timer) +-------------------- + +See :ref:`machine.WDT `. :: + + from machine import WDT + + # enable the WDT with a timeout of 5s (1s is the minimum) + wdt = WDT(timeout=5000) + wdt.feed() + Deep-sleep mode --------------- From a111889705649465b2fd57a1f10bd34aae9b4435 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:04:37 +1000 Subject: [PATCH 0828/3533] docs/esp32: Add SDCard to quickref. --- docs/esp32/quickref.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 5e70275830d81..a569ff0a4e282 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -424,6 +424,21 @@ Notes: p1 = Pin(4, Pin.OUT, None) +SD card +------- + +See :ref:`machine.SDCard `. :: + + import machine, uos + + # Slot 2 uses pins sck=18, cs=5, miso=19, mosi=23 + sd = machine.SDCard(slot=2) + uos.mount(sd, "/sd") # mount + + uos.listdir('/sd') # list directory contents + + uos.umount('/sd') # eject + RMT --- From 64aebed70e58ecc9623231d887d9818475174081 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:02:17 +1000 Subject: [PATCH 0829/3533] docs/esp8266: Add WDT to quickref. --- docs/esp8266/quickref.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index b9a46ab112a0c..af2ef1ca1a9df 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -293,6 +293,17 @@ See :ref:`machine.RTC ` :: (using a custom handler), `RTC.init()` and `RTC.deinit()` are currently not supported. +WDT (Watchdog timer) +-------------------- + +See :ref:`machine.WDT `. :: + + from machine import WDT + + # enable the WDT + wdt = WDT() + wdt.feed() + Deep-sleep mode --------------- From 8ff3520f67bb8f7e5509129f9f561a1217ababa4 Mon Sep 17 00:00:00 2001 From: mishafarms Date: Tue, 4 May 2021 10:50:20 -0700 Subject: [PATCH 0830/3533] esp32/esp32_rmt: Clear config struct before filling it out. Or unset entries will have garbage in them. Signed-off-by: mishafarms --- ports/esp32/esp32_rmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index b547af5539f34..9619b1dd5bd01 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -96,7 +96,7 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz self->carrier_freq = carrier_freq; self->loop_en = false; - rmt_config_t config; + rmt_config_t config = {0}; config.rmt_mode = RMT_MODE_TX; config.channel = (rmt_channel_t)self->channel_id; config.gpio_num = self->pin; From 9eea51b730532cb33fe2531a3aade734d0a0a121 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Tue, 20 Apr 2021 17:07:43 +1000 Subject: [PATCH 0831/3533] drivers/display/ssd1306.py: Add rotate method. And clean up (make more efficient) display set-up commands. --- drivers/display/ssd1306.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index 6359c85eaaa1f..85e2bf0cb528e 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -37,12 +37,12 @@ def __init__(self, width, height, external_vcc): def init_display(self): for cmd in ( - SET_DISP | 0x00, # off + SET_DISP, # display off # address setting SET_MEM_ADDR, 0x00, # horizontal # resolution and layout - SET_DISP_START_LINE | 0x00, + SET_DISP_START_LINE, # start at line 0 SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 SET_MUX_RATIO, self.height - 1, @@ -66,14 +66,14 @@ def init_display(self): # charge pump SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, - SET_DISP | 0x01, + SET_DISP | 0x01, # display on ): # on self.write_cmd(cmd) self.fill(0) self.show() def poweroff(self): - self.write_cmd(SET_DISP | 0x00) + self.write_cmd(SET_DISP) def poweron(self): self.write_cmd(SET_DISP | 0x01) @@ -85,6 +85,10 @@ def contrast(self, contrast): def invert(self, invert): self.write_cmd(SET_NORM_INV | (invert & 1)) + def rotate(self, rotate): + self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3)) + self.write_cmd(SET_SEG_REMAP | (rotate & 1)) + def show(self): x0 = 0 x1 = self.width - 1 From fd24e649fde26bcd484b30025527768d37f5c562 Mon Sep 17 00:00:00 2001 From: Tim Radvan Date: Fri, 16 Apr 2021 12:18:04 +0100 Subject: [PATCH 0832/3533] docs/library: Add initial API reference for rp2 module and its classes. All the method signatures from rp2_pio.c and friends have been taken and converted to RST format, then explanatory notes added for each signature. Signed-off-by: Tim Radvan --- docs/library/index.rst | 11 +++ docs/library/machine.rst | 6 +- docs/library/rp2.Flash.rst | 36 ++++++++ docs/library/rp2.PIO.rst | 94 +++++++++++++++++++++ docs/library/rp2.StateMachine.rst | 131 ++++++++++++++++++++++++++++++ docs/library/rp2.rst | 83 +++++++++++++++++++ 6 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 docs/library/rp2.Flash.rst create mode 100644 docs/library/rp2.PIO.rst create mode 100644 docs/library/rp2.StateMachine.rst create mode 100644 docs/library/rp2.rst diff --git a/docs/library/index.rst b/docs/library/index.rst index 43d9e87f3cda7..2536e8dc96d04 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -165,3 +165,14 @@ The following libraries are specific to the ESP8266 and ESP32. esp.rst esp32.rst + + +Libraries specific to the RP2040 +-------------------------------- + +The following libraries are specific to the RP2040, as used in the Raspberry Pi Pico. + +.. toctree:: + :maxdepth: 2 + + rp2.rst diff --git a/docs/library/machine.rst b/docs/library/machine.rst index f831049f88e92..0a1c1c953005f 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -64,9 +64,11 @@ Interrupt related functions Power related functions ----------------------- -.. function:: freq() +.. function:: freq([hz]) - Returns CPU frequency in hertz. + Returns the CPU frequency in hertz. + + On some ports this can also be used to set the CPU frequency by passing in *hz*. .. function:: idle() diff --git a/docs/library/rp2.Flash.rst b/docs/library/rp2.Flash.rst new file mode 100644 index 0000000000000..3b423adfdd894 --- /dev/null +++ b/docs/library/rp2.Flash.rst @@ -0,0 +1,36 @@ +.. currentmodule:: rp2 +.. _rp2.Flash: + +class Flash -- access to built-in flash storage +=============================================== + +This class gives access to the SPI flash memory. + +In most cases, to store persistent data on the device, you'll want to use a +higher-level abstraction, for example the filesystem via Python's standard file +API, but this interface is useful to :ref:`customise the filesystem +configuration ` or implement a low-level storage system for your +application. + + +Constructors +------------ + +.. class:: Flash() + + Gets the singleton object for accessing the SPI flash memory. + + +Methods +------- + +.. method:: Flash.readblocks(block_num, buf) + Flash.readblocks(block_num, buf, offset) +.. method:: Flash.writeblocks(block_num, buf) + Flash.writeblocks(block_num, buf, offset) +.. method:: Flash.ioctl(cmd, arg) + + These methods implement the simple and extended + :ref:`block protocol ` defined by + :class:`uos.AbstractBlockDev`. + diff --git a/docs/library/rp2.PIO.rst b/docs/library/rp2.PIO.rst new file mode 100644 index 0000000000000..e0675af1e9a2c --- /dev/null +++ b/docs/library/rp2.PIO.rst @@ -0,0 +1,94 @@ +.. currentmodule:: rp2 +.. _rp2.PIO: + +class PIO -- advanced PIO usage +=============================== + +The :class:`PIO` class gives access to an instance of the RP2040's PIO +(programmable I/O) interface. + +The preferred way to interact with PIO is using :class:`rp2.StateMachine`, the +PIO class is for advanced use. + +For assembling PIO programs, see :func:`rp2.asm_pio`. + + +Constructors +------------ + +.. class:: PIO(id) + + Gets the PIO instance numbered *id*. The RP2040 has two PIO instances, + numbered 0 and 1. + + Raises a ``ValueError`` if any other argument is provided. + + +Methods +------- + +.. method:: PIO.add_program(program) + + Add the *program* to the instruction memory of this PIO instance. + + The amount of memory available for programs on each PIO instance is + limited. If there isn't enough space left in the PIO's program memory + this method will raise ``OSError(ENOMEM)``. + +.. method:: PIO.remove_program([program]) + + Remove *program* from the instruction memory of this PIO instance. + + If no program is provided, it removes all programs. + + It is not an error to remove a program which has already been removed. + +.. method:: PIO.state_machine(id, [program, ...]) + + Gets the state machine numbered *id*. On the RP2040, each PIO instance has + four state machines, numbered 0 to 3. + + Optionally initialize it with a *program*: see `StateMachine.init`. + + >>> rp2.PIO(1).state_machine(3) + StateMachine(7) + +.. method:: PIO.irq(handler=None, trigger=IRQ_SM0|IRQ_SM1|IRQ_SM2|IRQ_SM3, hard=False) + + Returns the IRQ object for this PIO instance. + + MicroPython only uses IRQ 0 on each PIO instance. IRQ 1 is not available. + + Optionally configure it. + + +Constants +--------- + +.. data:: PIO.IN_LOW + PIO.IN_HIGH + PIO.OUT_LOW + PIO.OUT_HIGH + + These constants are used for the *out_init*, *set_init*, and *sideset_init* + arguments to `asm_pio`. + +.. data:: PIO.SHIFT_LEFT + PIO.SHIFT_RIGHT + + These constants are used for the *in_shiftdir* and *out_shiftdir* arguments + to `asm_pio` or `StateMachine.init`. + +.. data:: PIO.JOIN_NONE + PIO.JOIN_TX + PIO.JOIN_RX + + These constants are used for the *fifo_join* argument to `asm_pio`. + +.. data:: PIO.IRQ_SM0 + PIO.IRQ_SM1 + PIO.IRQ_SM2 + PIO.IRQ_SM3 + + These constants are used for the *trigger* argument to `PIO.irq`. + diff --git a/docs/library/rp2.StateMachine.rst b/docs/library/rp2.StateMachine.rst new file mode 100644 index 0000000000000..8d73ccf772a7f --- /dev/null +++ b/docs/library/rp2.StateMachine.rst @@ -0,0 +1,131 @@ +.. currentmodule:: rp2 +.. _rp2.StateMachine: + +class StateMachine -- access to the RP2040's programmable I/O interface +======================================================================= + +The :class:`StateMachine` class gives access to the RP2040's PIO (programmable +I/O) interface. + +For assembling PIO programs, see :func:`rp2.asm_pio`. + + +Constructors +------------ + +.. class:: StateMachine(id, [program, ...]) + + Get the state machine numbered *id*. The RP2040 has two identical PIO + instances, each with 4 state machines: so there are 8 state machines in + total, numbered 0 to 7. + + Optionally initialize it with the given program *program*: see + `StateMachine.init`. + + +Methods +------- + +.. method:: StateMachine.init(program, freq=-1, *, in_base=None, out_base=None, set_base=None, jmp_pin=None, sideset_base=None, in_shiftdir=None, out_shiftdir=None, push_thresh=None, pull_thresh=None) + + Configure the state machine instance to run the given *program*. + + The program is added to the instruction memory of this PIO instance. If the + instruction memory already contains this program, then its offset is + re-used so as to save on instruction memory. + + - *freq* is the frequency in Hz to run the state machine at. Defaults to + the system clock frequency. + + The clock divider is computed as ``system clock frequency / freq``, so + there can be slight rounding errors. + + The minimum possible clock divider is one 65536th of the system clock: so + at the default system clock frequency of 125MHz, the minimum value of + *freq* is ``1908``. To run state machines at slower frequencies, you'll + need to reduce the system clock speed with `machine.freq()`. + - *in_base* is the first pin to use for ``in()`` instructions. + - *out_base* is the first pin to use for ``out()`` instructions. + - *set_base* is the first pin to use for ``set()`` instructions. + - *jmp_pin* is the first pin to use for ``jmp(pin, ...)`` instructions. + - *sideset_base* is the first pin to use for side-setting. + - *in_shiftdir* is the direction the ISR will shift, either + `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. + - *out_shiftdir* is the direction the OSR will shift, either + `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. + - *push_thresh* is the threshold in bits before auto-push or conditional + re-pushing is triggered. + - *pull_thresh* is the threshold in bits before auto-push or conditional + re-pushing is triggered. + +.. method:: StateMachine.active([value]) + + Gets or sets whether the state machine is currently running. + + >>> sm.active() + True + >>> sm.active(0) + False + +.. method:: StateMachine.restart() + + Restarts the state machine and jumps to the beginning of the program. + + This method clears the state machine's internal state using the RP2040's + ``SM_RESTART`` register. This includes: + + - input and output shift counters + - the contents of the input shift register + - the delay counter + - the waiting-on-IRQ state + - a stalled instruction run using `StateMachine.exec()` + +.. method:: StateMachine.exec(instr) + + Execute a single PIO instruction. Uses `asm_pio_encode` to encode the + instruction from the given string *instr*. + + >>> sm.exec("set(0, 1)") + +.. method:: StateMachine.get(buf=None, shift=0) + + Pull a word from the state machine's RX FIFO. + + If the FIFO is empty, it blocks until data arrives (i.e. the state machine + pushes a word). + + The value is shifted right by *shift* bits before returning, i.e. the + return value is ``word >> shift``. + +.. method:: StateMachine.put(value, shift=0) + + Push a word onto the state machine's TX FIFO. + + If the FIFO is full, it blocks until there is space (i.e. the state machine + pulls a word). + + The value is first shifted left by *shift* bits, i.e. the state machine + receives ``value << shift``. + +.. method:: StateMachine.rx_fifo() + + Returns the number of words in the state machine's RX FIFO. A value of 0 + indicates the FIFO is empty. + + Useful for checking if data is waiting to be read, before calling + `StateMachine.get()`. + +.. method:: StateMachine.tx_fifo() + + Returns the number of words in the state machine's TX FIFO. A value of 0 + indicates the FIFO is empty. + + Useful for checking if there is space to push another word using + `StateMachine.put()`. + +.. method:: StateMachine.irq(handler=None, trigger=0|1, hard=False) + + Returns the IRQ object for the given StateMachine. + + Optionally configure it. + diff --git a/docs/library/rp2.rst b/docs/library/rp2.rst new file mode 100644 index 0000000000000..b6e7fcf429505 --- /dev/null +++ b/docs/library/rp2.rst @@ -0,0 +1,83 @@ +.. currentmodule:: rp2 + +:mod:`rp2` --- functionality specific to the RP2040 +=================================================== + +.. module:: rp2 + :synopsis: functionality specific to the RP2 + +The ``rp2`` module contains functions and classes specific to the RP2040, as +used in the Raspberry Pi Pico. + +See the `RP2040 Python datasheet +`_ +for more information, and `pico-micropython-examples +`_ +for example code. + + +PIO related functions +--------------------- + +The ``rp2`` module includes functions for assembling PIO programs. + +For running PIO programs, see :class:`rp2.StateMachine`. + +.. function:: asm_pio(*, out_init=None, set_init=None, sideset_init=None, in_shiftdir=0, out_shiftdir=0, autopush=False, autopull=False, push_thresh=32, pull_thresh=32, fifo_join=PIO.JOIN_NONE) + + Assemble a PIO program. + + The following parameters control the initial state of the GPIO pins, as one + of `PIO.IN_LOW`, `PIO.IN_HIGH`, `PIO.OUT_LOW` or `PIO.OUT_HIGH`. If the + program uses more than one pin, provide a tuple, e.g. + ``out_init=(PIO.OUT_LOW, PIO.OUT_LOW)``. + + - *out_init* configures the pins used for ``out()`` instructions. + - *set_init* configures the pins used for ``set()`` instructions. There can + be at most 5. + - *sideset_init* configures the pins used side-setting. There can be at + most 5. + + The following parameters are used by default, but can be overriden in + `StateMachine.init()`: + + - *in_shiftdir* is the default direction the ISR will shift, either + `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. + - *out_shiftdir* is the default direction the OSR will shift, either + `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. + - *push_thresh* is the threshold in bits before auto-push or conditional + re-pushing is triggered. + - *pull_thresh* is the threshold in bits before auto-push or conditional + re-pushing is triggered. + + The remaining parameters are: + + - *autopush* configures whether auto-push is enabled. + - *autopull* configures whether auto-pull is enabled. + - *fifo_join* configures whether the 4-word TX and RX FIFOs should be + combined into a single 8-word FIFO for one direction only. The options + are `PIO.JOIN_NONE`, `PIO.JOIN_RX` and `PIO.JOIN_TX`. + +.. function:: asm_pio_encode(instr, sideset_count) + + Assemble a single PIO instruction. You usually want to use `asm_pio()` + instead. + + >>> rp2.asm_pio_encode("set(0, 1)", 0) + 57345 + +.. class:: PIOASMError + + This exception is raised from `asm_pio()` or `asm_pio_encode()` if there is + an error assembling a PIO program. + + +Classes +------- + +.. toctree:: + :maxdepth: 1 + + rp2.Flash.rst + rp2.PIO.rst + rp2.StateMachine.rst From 31e0b8c71c6e9bed7388439e2441465c58311393 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 May 2021 18:14:13 +1000 Subject: [PATCH 0833/3533] esp32/mpthreadport: Don't explicitly free thread struct in TCB cleanup. Because vPortCleanUpTCB runs on the FreeRTOS idle task and cannot execute any VM or runtime related code like freeing memory. Signed-off-by: Damien George --- ports/esp32/mpthreadport.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index 140a76464fe40..bbfc53d3f52c1 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -166,6 +166,8 @@ void mp_thread_finish(void) { mp_thread_mutex_unlock(&thread_mutex); } +// This is called from the FreeRTOS idle task and is not within Python context, +// so MP_STATE_THREAD is not valid and it does not have the GIL. void vPortCleanUpTCB(void *tcb) { if (thread == NULL) { // threading not yet initialised @@ -182,8 +184,7 @@ void vPortCleanUpTCB(void *tcb) { // move the start pointer thread = th->next; } - // explicitly release all its memory - m_del(thread_t, th, 1); + // The "th" memory will eventually be reclaimed by the GC. break; } } From 864e4ecc47f204e6ca6fbe8342e268932c89acdf Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 May 2021 18:17:27 +1000 Subject: [PATCH 0834/3533] esp32/mpthreadport: Use binary semaphore instead of mutex. So a lock can be acquired on one Python thread and then released on another. A test for this is added. Signed-off-by: Damien George --- ports/esp32/mpthreadport.c | 5 ++++- tests/thread/thread_lock5.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/thread/thread_lock5.py diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index bbfc53d3f52c1..f575d99e6ec65 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -192,7 +192,10 @@ void vPortCleanUpTCB(void *tcb) { } void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { - mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer); + // Need a binary semaphore so a lock can be acquired on one Python thread + // and then released on another. + mutex->handle = xSemaphoreCreateBinaryStatic(&mutex->buffer); + xSemaphoreGive(mutex->handle); } int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { diff --git a/tests/thread/thread_lock5.py b/tests/thread/thread_lock5.py new file mode 100644 index 0000000000000..830b5efa82815 --- /dev/null +++ b/tests/thread/thread_lock5.py @@ -0,0 +1,16 @@ +# test _thread lock objects where a lock is acquired/released by a different thread + +import _thread + + +def thread_entry(): + print("thread about to release lock") + lock.release() + + +lock = _thread.allocate_lock() +lock.acquire() +_thread.start_new_thread(thread_entry, ()) +lock.acquire() +print("main has lock") +lock.release() From 9340cfe77432b548fc6ace6e2959a07a08e8eadc Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 May 2021 18:20:05 +1000 Subject: [PATCH 0835/3533] tests/thread: Make stress_create.py test run on esp32. The esp32 port needs to be idle for finished threads and their resources to be freed up. Signed-off-by: Damien George --- tests/thread/stress_create.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/thread/stress_create.py b/tests/thread/stress_create.py index eda768fa7b5a3..877424cdf50b2 100644 --- a/tests/thread/stress_create.py +++ b/tests/thread/stress_create.py @@ -1,9 +1,13 @@ # stress test for creating many threads try: - import utime as time + import utime + + sleep_ms = utime.sleep_ms except ImportError: import time + + sleep_ms = lambda t: time.sleep(t / 1000) import _thread @@ -16,9 +20,11 @@ def thread_entry(n): try: _thread.start_new_thread(thread_entry, (thread_num,)) thread_num += 1 - except MemoryError: - pass + except (MemoryError, OSError) as er: + # Cannot create a new thead at this stage, yield for a bit to + # let existing threads run to completion and free up resources. + sleep_ms(50) # wait for the last threads to terminate -time.sleep(1) +sleep_ms(500) print("done") From 7b923d6c72be29e2fe57740c9ea3a2135e7db6f7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 May 2021 18:36:27 +1000 Subject: [PATCH 0836/3533] tests/thread: Make stress_aes.py test run on bare-metal ports. This is a long-running test, so make it run in reasonable time on slower, bare-metal ports. Signed-off-by: Damien George --- tests/thread/stress_aes.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/thread/stress_aes.py b/tests/thread/stress_aes.py index f73da557cb00e..1bf252a807f9a 100644 --- a/tests/thread/stress_aes.py +++ b/tests/thread/stress_aes.py @@ -235,7 +235,7 @@ def add(self, val): count = LockedCounter() -def thread_entry(): +def thread_entry(n_loop): global count aes = AES(256) @@ -244,7 +244,7 @@ def thread_entry(): data = bytearray(128) # from now on we don't use the heap - for loop in range(5): + for loop in range(n_loop): # encrypt aes.set_key(key) aes.set_iv(iv) @@ -265,8 +265,20 @@ def thread_entry(): if __name__ == "__main__": - n_thread = 20 + import sys + + if sys.platform == "rp2": + n_thread = 1 + n_loop = 2 + elif sys.platform in ("esp32", "pyboard"): + n_thread = 2 + n_loop = 2 + else: + n_thread = 20 + n_loop = 5 for i in range(n_thread): - _thread.start_new_thread(thread_entry, ()) + _thread.start_new_thread(thread_entry, (n_loop,)) + thread_entry(n_loop) while count.value < n_thread: time.sleep(1) + print("done") From d0de16266f92de180bd34f03aa43f1c489cd883a Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 9 May 2021 00:08:30 +1000 Subject: [PATCH 0837/3533] rp2/mpthreadport: Add mp_thread_deinit to reset core1 on soft reset. Any code running on core1 should be stopped on soft-reset (the GC heap is reset so if code continues to run on core1 it will see corrupt memory). Signed-off-by: Damien George --- ports/rp2/main.c | 3 +++ ports/rp2/mpthreadport.c | 5 +++++ ports/rp2/mpthreadport.h | 1 + 3 files changed, 9 insertions(+) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 8fddeaa560510..7709a478bc80a 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -138,6 +138,9 @@ int main(int argc, char **argv) { mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); rp2_pio_deinit(); machine_pin_deinit(); + #if MICROPY_PY_THREAD + mp_thread_deinit(); + #endif gc_sweep_all(); mp_deinit(); } diff --git a/ports/rp2/mpthreadport.c b/ports/rp2/mpthreadport.c index fb4428772afc1..8a36cfca75a34 100644 --- a/ports/rp2/mpthreadport.c +++ b/ports/rp2/mpthreadport.c @@ -46,6 +46,11 @@ void mp_thread_init(void) { core1_entry = NULL; } +void mp_thread_deinit(void) { + multicore_reset_core1(); + core1_entry = NULL; +} + void mp_thread_gc_others(void) { if (get_core_num() == 0) { // GC running on core0, trace core1's stack, if it's running. diff --git a/ports/rp2/mpthreadport.h b/ports/rp2/mpthreadport.h index 5eb0bff396b98..868f8d14118dd 100644 --- a/ports/rp2/mpthreadport.h +++ b/ports/rp2/mpthreadport.h @@ -34,6 +34,7 @@ typedef struct mutex mp_thread_mutex_t; extern void *core_state[2]; void mp_thread_init(void); +void mp_thread_deinit(void); void mp_thread_gc_others(void); static inline void mp_thread_set_state(struct _mp_state_thread_t *state) { From b6b39bff47b5ff1ffc322a6e795d57451d4215a8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 May 2021 23:56:43 +1000 Subject: [PATCH 0838/3533] py/gc: Make gc_lock_depth have a count per thread. This commit makes gc_lock_depth have one counter per thread, instead of one global counter. This makes threads properly independent with respect to the GC, in particular threads can now independently lock the GC for themselves without locking it for other threads. It also means a given thread can run a hard IRQ without temporarily locking the GC for all other threads and potentially making them have MemoryError exceptions at random locations (this really only occurs on MCUs with multiple cores and no GIL, eg on the rp2 port). The commit also removes protection of the GC lock/unlock functions, which is no longer needed when the counter is per thread (and this also fixes the cas where a hard IRQ calling gc_lock() may stall waiting for the mutex). It also puts the check for `gc_lock_depth > 0` outside the GC mutex in gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from waiting on a mutex if it does attempt to allocate heap memory (and putting the check outside the GC mutex is now safe now that there is a gc_lock_depth per thread). Signed-off-by: Damien George --- lib/utils/pyexec.c | 4 +-- py/gc.c | 45 ++++++++++++++-------------- py/modmicropython.c | 4 +-- py/modthread.c | 3 ++ py/mpstate.h | 4 ++- tests/thread/thread_heap_lock.py | 26 ++++++++++++++++ tests/thread/thread_heap_lock.py.exp | 1 + 7 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 tests/thread/thread_heap_lock.py create mode 100644 tests/thread/thread_heap_lock.py.exp diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index d1e955d65b9a2..4446b36b61b56 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -588,8 +588,8 @@ int pyexec_friendly_repl(void) { // If the GC is locked at this point there is no way out except a reset, // so force the GC to be unlocked to help the user debug what went wrong. - if (MP_STATE_MEM(gc_lock_depth) != 0) { - MP_STATE_MEM(gc_lock_depth) = 0; + if (MP_STATE_THREAD(gc_lock_depth) != 0) { + MP_STATE_THREAD(gc_lock_depth) = 0; } vstr_reset(&line); diff --git a/py/gc.c b/py/gc.c index 53a0d9da4a73c..88adf2045eb42 100644 --- a/py/gc.c +++ b/py/gc.c @@ -150,7 +150,7 @@ void gc_init(void *start, void *end) { MP_STATE_MEM(gc_last_free_atb_index) = 0; // unlock the GC - MP_STATE_MEM(gc_lock_depth) = 0; + MP_STATE_THREAD(gc_lock_depth) = 0; // allow auto collection MP_STATE_MEM(gc_auto_collect_enabled) = 1; @@ -174,19 +174,20 @@ void gc_init(void *start, void *end) { } void gc_lock(void) { - GC_ENTER(); - MP_STATE_MEM(gc_lock_depth)++; - GC_EXIT(); + // This does not need to be atomic or have the GC mutex because: + // - each thread has its own gc_lock_depth so there are no races between threads; + // - a hard interrupt will only change gc_lock_depth during its execution, and + // upon return will restore the value of gc_lock_depth. + MP_STATE_THREAD(gc_lock_depth)++; } void gc_unlock(void) { - GC_ENTER(); - MP_STATE_MEM(gc_lock_depth)--; - GC_EXIT(); + // This does not need to be atomic, See comment above in gc_lock. + MP_STATE_THREAD(gc_lock_depth)--; } bool gc_is_locked(void) { - return MP_STATE_MEM(gc_lock_depth) != 0; + return MP_STATE_THREAD(gc_lock_depth) != 0; } // ptr should be of type void* @@ -320,7 +321,7 @@ STATIC void gc_sweep(void) { void gc_collect_start(void) { GC_ENTER(); - MP_STATE_MEM(gc_lock_depth)++; + MP_STATE_THREAD(gc_lock_depth)++; #if MICROPY_GC_ALLOC_THRESHOLD MP_STATE_MEM(gc_alloc_amount) = 0; #endif @@ -360,13 +361,13 @@ void gc_collect_end(void) { gc_deal_with_stack_overflow(); gc_sweep(); MP_STATE_MEM(gc_last_free_atb_index) = 0; - MP_STATE_MEM(gc_lock_depth)--; + MP_STATE_THREAD(gc_lock_depth)--; GC_EXIT(); } void gc_sweep_all(void) { GC_ENTER(); - MP_STATE_MEM(gc_lock_depth)++; + MP_STATE_THREAD(gc_lock_depth)++; MP_STATE_MEM(gc_stack_overflow) = 0; gc_collect_end(); } @@ -445,14 +446,13 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) { return NULL; } - GC_ENTER(); - // check if GC is locked - if (MP_STATE_MEM(gc_lock_depth) > 0) { - GC_EXIT(); + if (MP_STATE_THREAD(gc_lock_depth) > 0) { return NULL; } + GC_ENTER(); + size_t i; size_t end_block; size_t start_block; @@ -573,13 +573,13 @@ void *gc_alloc_with_finaliser(mp_uint_t n_bytes) { // force the freeing of a piece of memory // TODO: freeing here does not call finaliser void gc_free(void *ptr) { - GC_ENTER(); - if (MP_STATE_MEM(gc_lock_depth) > 0) { + if (MP_STATE_THREAD(gc_lock_depth) > 0) { // TODO how to deal with this error? - GC_EXIT(); return; } + GC_ENTER(); + DEBUG_printf("gc_free(%p)\n", ptr); if (ptr == NULL) { @@ -674,15 +674,14 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) { return NULL; } + if (MP_STATE_THREAD(gc_lock_depth) > 0) { + return NULL; + } + void *ptr = ptr_in; GC_ENTER(); - if (MP_STATE_MEM(gc_lock_depth) > 0) { - GC_EXIT(); - return NULL; - } - // get the GC block number corresponding to this pointer assert(VERIFY_PTR(ptr)); size_t block = BLOCK_FROM_PTR(ptr); diff --git a/py/modmicropython.c b/py/modmicropython.c index f7eadf79bd1d2..180f7f186c69a 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -130,13 +130,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_he STATIC mp_obj_t mp_micropython_heap_unlock(void) { gc_unlock(); - return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth)); + return MP_OBJ_NEW_SMALL_INT(MP_STATE_THREAD(gc_lock_depth)); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock); #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED STATIC mp_obj_t mp_micropython_heap_locked(void) { - return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth)); + return MP_OBJ_NEW_SMALL_INT(MP_STATE_THREAD(gc_lock_depth)); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_locked_obj, mp_micropython_heap_locked); #endif diff --git a/py/modthread.c b/py/modthread.c index 1306dc642b018..64fbb3f198e15 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -171,6 +171,9 @@ STATIC void *thread_entry(void *args_in) { mp_pystack_init(mini_pystack, &mini_pystack[128]); #endif + // The GC starts off unlocked on this thread. + ts.gc_lock_depth = 0; + // set locals and globals from the calling context mp_locals_set(args->dict_locals); mp_globals_set(args->dict_globals); diff --git a/py/mpstate.h b/py/mpstate.h index 2519c77e2d647..a0e3d4f143096 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -80,7 +80,6 @@ typedef struct _mp_state_mem_t { int gc_stack_overflow; MICROPY_GC_STACK_ENTRY_TYPE gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]; - uint16_t gc_lock_depth; // This variable controls auto garbage collection. If set to 0 then the // GC won't automatically run when gc_alloc can't find enough blocks. But @@ -253,6 +252,9 @@ typedef struct _mp_state_thread_t { uint8_t *pystack_cur; #endif + // Locking of the GC is done per thread. + uint16_t gc_lock_depth; + //////////////////////////////////////////////////////////// // START ROOT POINTER SECTION // Everything that needs GC scanning must start here, and diff --git a/tests/thread/thread_heap_lock.py b/tests/thread/thread_heap_lock.py new file mode 100644 index 0000000000000..2837e0f3661fd --- /dev/null +++ b/tests/thread/thread_heap_lock.py @@ -0,0 +1,26 @@ +# test interaction of micropython.heap_lock with threads + +import _thread, micropython + +lock1 = _thread.allocate_lock() +lock2 = _thread.allocate_lock() + + +def thread_entry(): + lock1.acquire() + print([1, 2, 3]) + lock2.release() + + +lock1.acquire() +lock2.acquire() + +_thread.start_new_thread(thread_entry, ()) + +micropython.heap_lock() +lock1.release() +lock2.acquire() +micropython.heap_unlock() + +lock1.release() +lock2.release() diff --git a/tests/thread/thread_heap_lock.py.exp b/tests/thread/thread_heap_lock.py.exp new file mode 100644 index 0000000000000..b5d8bb58d9bc3 --- /dev/null +++ b/tests/thread/thread_heap_lock.py.exp @@ -0,0 +1 @@ +[1, 2, 3] From 4cdcbdb7531b98b2dc9a85737b760f509957e31e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 10 May 2021 12:44:47 +1000 Subject: [PATCH 0839/3533] tests/thread: Make exc1,exit1,exit2,stacksize1,start1 tests run on rp2. The RP2040 has 2 cores and supports running at most 2 Python threads (the main one plus another), and will raise OSError if a thread cannot be created because core1 is already in use. This commit adjusts some thread tests to be robust against such OSError's. These tests now pass on rp2 boards. Signed-off-by: Damien George --- tests/thread/thread_exc1.py | 7 ++++++- tests/thread/thread_exit1.py | 9 +++++++-- tests/thread/thread_exit2.py | 9 +++++++-- tests/thread/thread_stacksize1.py | 7 ++++++- tests/thread/thread_start1.py | 9 +++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/tests/thread/thread_exc1.py b/tests/thread/thread_exc1.py index 16483d7778a06..cd87740929103 100644 --- a/tests/thread/thread_exc1.py +++ b/tests/thread/thread_exc1.py @@ -25,7 +25,12 @@ def thread_entry(): # spawn threads for i in range(n_thread): - _thread.start_new_thread(thread_entry, ()) + while True: + try: + _thread.start_new_thread(thread_entry, ()) + break + except OSError: + pass # busy wait for threads to finish while n_finished < n_thread: diff --git a/tests/thread/thread_exit1.py b/tests/thread/thread_exit1.py index c4a93c45a3df1..186a9be340bd1 100644 --- a/tests/thread/thread_exit1.py +++ b/tests/thread/thread_exit1.py @@ -13,8 +13,13 @@ def thread_entry(): _thread.exit() -_thread.start_new_thread(thread_entry, ()) -_thread.start_new_thread(thread_entry, ()) +for i in range(2): + while True: + try: + _thread.start_new_thread(thread_entry, ()) + break + except OSError: + pass # wait for threads to finish time.sleep(1) diff --git a/tests/thread/thread_exit2.py b/tests/thread/thread_exit2.py index 0cd80e69092a8..5be7945db2205 100644 --- a/tests/thread/thread_exit2.py +++ b/tests/thread/thread_exit2.py @@ -13,8 +13,13 @@ def thread_entry(): raise SystemExit -_thread.start_new_thread(thread_entry, ()) -_thread.start_new_thread(thread_entry, ()) +for i in range(2): + while True: + try: + _thread.start_new_thread(thread_entry, ()) + break + except OSError: + pass # wait for threads to finish time.sleep(1) diff --git a/tests/thread/thread_stacksize1.py b/tests/thread/thread_stacksize1.py index 5d25509b763a0..cf46b73b770e0 100644 --- a/tests/thread/thread_stacksize1.py +++ b/tests/thread/thread_stacksize1.py @@ -41,7 +41,12 @@ def thread_entry(): # set stack size and spawn a few threads _thread.stack_size(sz) for i in range(n_thread): - _thread.start_new_thread(thread_entry, ()) + while True: + try: + _thread.start_new_thread(thread_entry, ()) + break + except OSError: + pass # reset stack size to default (for subsequent scripts on baremetal) _thread.stack_size() diff --git a/tests/thread/thread_start1.py b/tests/thread/thread_start1.py index f0e696840e556..7274633245a71 100644 --- a/tests/thread/thread_start1.py +++ b/tests/thread/thread_start1.py @@ -18,8 +18,13 @@ def thread_entry(n): foo() -_thread.start_new_thread(thread_entry, (10,)) -_thread.start_new_thread(thread_entry, (20,)) +for i in range(2): + while True: + try: + _thread.start_new_thread(thread_entry, ((i + 1) * 10,)) + break + except OSError: + pass # wait for threads to finish time.sleep(1) From 5093d49fae5b59d6440d34b4c05f2950f40a6b9f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 10:58:12 +1000 Subject: [PATCH 0840/3533] esp32: Extend support for S2 series, and S3 where applicable. Improvements made: - PSRAM support for S2 - partition definition for 16MiB flash - correct ADC and DAC pins - correct GPIO and IRQ pins - S3 components in CMakeLists Based on original commit made by Seon Rozenblum aka @UnexpectedMaker. Signed-off-by: Damien George --- ports/esp32/boards/sdkconfig.spiram_sx | 11 +++ ports/esp32/machine_adc.c | 17 +++- ports/esp32/machine_pin.c | 131 ++++++++++++++++++++++--- ports/esp32/machine_touchpad.c | 23 +++++ ports/esp32/main.c | 14 +++ ports/esp32/main/CMakeLists.txt | 3 + ports/esp32/modmachine.c | 3 + ports/esp32/mphalport.c | 7 ++ ports/esp32/partitions-16MiB.csv | 8 ++ 9 files changed, 201 insertions(+), 16 deletions(-) create mode 100644 ports/esp32/boards/sdkconfig.spiram_sx create mode 100644 ports/esp32/partitions-16MiB.csv diff --git a/ports/esp32/boards/sdkconfig.spiram_sx b/ports/esp32/boards/sdkconfig.spiram_sx new file mode 100644 index 0000000000000..18a0712cbf70f --- /dev/null +++ b/ports/esp32/boards/sdkconfig.spiram_sx @@ -0,0 +1,11 @@ +# MicroPython on ESP32-S2 and ESP32-PAD1_subscript_3, ESP IDF configuration with SPIRAM support +CONFIG_ESP32S2_SPIRAM_SUPPORT=y +CONFIG_SPIRAM_TYPE_AUTO=y +CONFIG_DEFAULT_PSRAM_CLK_IO=30 +CONFIG_DEFAULT_PSRAM_CS_IO=26 +CONFIG_SPIRAM_SPEED_80M=y +CONFIG_SPIRAM=y +CONFIG_SPIRAM_BOOT_INIT=y +CONFIG_SPIRAM_IGNORE_NOTFOUND=y +CONFIG_SPIRAM_USE_MEMMAP=y +CONFIG_SPIRAM_MEMTEST=y diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 4c19d5992bd8a..739d47da5072e 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -43,6 +43,7 @@ typedef struct _madc_obj_t { } madc_obj_t; STATIC const madc_obj_t madc_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 {{&machine_adc_type}, GPIO_NUM_36, ADC1_CHANNEL_0}, {{&machine_adc_type}, GPIO_NUM_37, ADC1_CHANNEL_1}, {{&machine_adc_type}, GPIO_NUM_38, ADC1_CHANNEL_2}, @@ -51,6 +52,18 @@ STATIC const madc_obj_t madc_obj[] = { {{&machine_adc_type}, GPIO_NUM_33, ADC1_CHANNEL_5}, {{&machine_adc_type}, GPIO_NUM_34, ADC1_CHANNEL_6}, {{&machine_adc_type}, GPIO_NUM_35, ADC1_CHANNEL_7}, + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + {{&machine_adc_type}, GPIO_NUM_1, ADC1_CHANNEL_0}, + {{&machine_adc_type}, GPIO_NUM_2, ADC1_CHANNEL_1}, + {{&machine_adc_type}, GPIO_NUM_3, ADC1_CHANNEL_2}, + {{&machine_adc_type}, GPIO_NUM_4, ADC1_CHANNEL_3}, + {{&machine_adc_type}, GPIO_NUM_5, ADC1_CHANNEL_4}, + {{&machine_adc_type}, GPIO_NUM_6, ADC1_CHANNEL_5}, + {{&machine_adc_type}, GPIO_NUM_7, ADC1_CHANNEL_6}, + {{&machine_adc_type}, GPIO_NUM_8, ADC1_CHANNEL_7}, + {{&machine_adc_type}, GPIO_NUM_9, ADC1_CHANNEL_8}, + {{&machine_adc_type}, GPIO_NUM_10, ADC1_CHANNEL_9}, + #endif }; STATIC uint8_t adc_bit_width; @@ -145,7 +158,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { case ADC_WIDTH_12Bit: adc_bit_width = 12; break; - #elif CONFIG_IDF_TARGET_ESP32S2 + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 case ADC_WIDTH_BIT_13: adc_bit_width = 13; break; @@ -175,7 +188,7 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) }, - #elif CONFIG_IDF_TARGET_ESP32S2 + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) }, #endif }; diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index 8dbdd198490b2..bd623d0413bd4 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -56,6 +56,8 @@ typedef struct _machine_pin_irq_obj_t { } machine_pin_irq_obj_t; STATIC const machine_pin_obj_t machine_pin_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 + {{&machine_pin_type}, GPIO_NUM_0}, {{&machine_pin_type}, GPIO_NUM_1}, {{&machine_pin_type}, GPIO_NUM_2}, @@ -78,17 +80,10 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_21}, - #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_type}, GPIO_NUM_22}, {{&machine_pin_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_25}, - #else - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - #endif {{&machine_pin_type}, GPIO_NUM_26}, {{&machine_pin_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -103,6 +98,63 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_37}, {{&machine_pin_type}, GPIO_NUM_38}, {{&machine_pin_type}, GPIO_NUM_39}, + + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + + {{&machine_pin_type}, GPIO_NUM_0}, + {{&machine_pin_type}, GPIO_NUM_1}, + {{&machine_pin_type}, GPIO_NUM_2}, + {{&machine_pin_type}, GPIO_NUM_3}, + {{&machine_pin_type}, GPIO_NUM_4}, + {{&machine_pin_type}, GPIO_NUM_5}, + {{&machine_pin_type}, GPIO_NUM_6}, + {{&machine_pin_type}, GPIO_NUM_7}, + {{&machine_pin_type}, GPIO_NUM_8}, + {{&machine_pin_type}, GPIO_NUM_9}, + {{&machine_pin_type}, GPIO_NUM_10}, + {{&machine_pin_type}, GPIO_NUM_11}, + {{&machine_pin_type}, GPIO_NUM_12}, + {{&machine_pin_type}, GPIO_NUM_13}, + {{&machine_pin_type}, GPIO_NUM_14}, + {{&machine_pin_type}, GPIO_NUM_15}, + {{&machine_pin_type}, GPIO_NUM_16}, + {{&machine_pin_type}, GPIO_NUM_17}, + {{&machine_pin_type}, GPIO_NUM_18}, + #if CONFIG_USB_CDC_ENABLED + {{NULL}, -1}, // 19 is for native USB D- + {{NULL}, -1}, // 20 is for native USB D- + #else + {{&machine_pin_type}, GPIO_NUM_19}, + {{&machine_pin_type}, GPIO_NUM_20}, + #endif + {{&machine_pin_type}, GPIO_NUM_21}, + {{NULL}, -1}, // 22 not a pin + {{NULL}, -1}, // 23 not a pin + {{NULL}, -1}, // 24 not a pin + {{NULL}, -1}, // 25 not a pin + {{NULL}, -1}, // 26 FLASH/PSRAM + {{NULL}, -1}, // 27 FLASH/PSRAM + {{NULL}, -1}, // 28 FLASH/PSRAM + {{NULL}, -1}, // 29 FLASH/PSRAM + {{NULL}, -1}, // 30 FLASH/PSRAM + {{NULL}, -1}, // 31 FLASH/PSRAM + {{NULL}, -1}, // 32 FLASH/PSRAM + {{&machine_pin_type}, GPIO_NUM_33}, + {{&machine_pin_type}, GPIO_NUM_34}, + {{&machine_pin_type}, GPIO_NUM_35}, + {{&machine_pin_type}, GPIO_NUM_36}, + {{&machine_pin_type}, GPIO_NUM_37}, + {{&machine_pin_type}, GPIO_NUM_38}, + {{&machine_pin_type}, GPIO_NUM_39}, // MTCLK + {{&machine_pin_type}, GPIO_NUM_40}, // MTDO + {{&machine_pin_type}, GPIO_NUM_41}, // MTDI + {{&machine_pin_type}, GPIO_NUM_42}, // MTMS + {{&machine_pin_type}, GPIO_NUM_43}, // U0TXD + {{&machine_pin_type}, GPIO_NUM_44}, // U0RXD + {{&machine_pin_type}, GPIO_NUM_45}, + {{&machine_pin_type}, GPIO_NUM_46}, + + #endif }; // forward declaration @@ -399,6 +451,8 @@ const mp_obj_type_t machine_pin_type = { STATIC const mp_obj_type_t machine_pin_irq_type; STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { + #if CONFIG_IDF_TARGET_ESP32 + {{&machine_pin_irq_type}, GPIO_NUM_0}, {{&machine_pin_irq_type}, GPIO_NUM_1}, {{&machine_pin_irq_type}, GPIO_NUM_2}, @@ -421,17 +475,10 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_21}, - #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_irq_type}, GPIO_NUM_22}, {{&machine_pin_irq_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_25}, - #else - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - {{NULL}, -1}, - #endif {{&machine_pin_irq_type}, GPIO_NUM_26}, {{&machine_pin_irq_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -446,6 +493,62 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_37}, {{&machine_pin_irq_type}, GPIO_NUM_38}, {{&machine_pin_irq_type}, GPIO_NUM_39}, + + #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + + {{&machine_pin_irq_type}, GPIO_NUM_0}, + {{&machine_pin_irq_type}, GPIO_NUM_1}, + {{&machine_pin_irq_type}, GPIO_NUM_2}, + {{&machine_pin_irq_type}, GPIO_NUM_3}, + {{&machine_pin_irq_type}, GPIO_NUM_4}, + {{&machine_pin_irq_type}, GPIO_NUM_5}, + {{&machine_pin_irq_type}, GPIO_NUM_6}, + {{&machine_pin_irq_type}, GPIO_NUM_7}, + {{&machine_pin_irq_type}, GPIO_NUM_8}, + {{&machine_pin_irq_type}, GPIO_NUM_9}, + {{&machine_pin_irq_type}, GPIO_NUM_10}, + {{&machine_pin_irq_type}, GPIO_NUM_11}, + {{&machine_pin_irq_type}, GPIO_NUM_12}, + {{&machine_pin_irq_type}, GPIO_NUM_13}, + {{&machine_pin_irq_type}, GPIO_NUM_14}, + {{&machine_pin_irq_type}, GPIO_NUM_15}, + {{&machine_pin_irq_type}, GPIO_NUM_16}, + {{&machine_pin_irq_type}, GPIO_NUM_17}, + {{&machine_pin_irq_type}, GPIO_NUM_18}, + #if CONFIG_USB_CDC_ENABLED + {{NULL}, -1}, // 19 is for native USB D- + {{NULL}, -1}, // 20 is for native USB D- + #else + {{&machine_pin_irq_type}, GPIO_NUM_19}, + {{&machine_pin_irq_type}, GPIO_NUM_20}, + #endif + {{&machine_pin_irq_type}, GPIO_NUM_21}, + {{NULL}, -1}, // 22 not a pin + {{NULL}, -1}, // 23 not a pin + {{NULL}, -1}, // 24 not a pin + {{NULL}, -1}, // 25 not a pin + {{NULL}, -1}, // 26 FLASH/PSRAM + {{NULL}, -1}, // 27 FLASH/PSRAM + {{NULL}, -1}, // 28 FLASH/PSRAM + {{NULL}, -1}, // 29 FLASH/PSRAM + {{NULL}, -1}, // 30 FLASH/PSRAM + {{NULL}, -1}, // 31 FLASH/PSRAM + {{NULL}, -1}, // 32 FLASH/PSRAM + {{&machine_pin_irq_type}, GPIO_NUM_33}, + {{&machine_pin_irq_type}, GPIO_NUM_34}, + {{&machine_pin_irq_type}, GPIO_NUM_35}, + {{&machine_pin_irq_type}, GPIO_NUM_36}, + {{&machine_pin_irq_type}, GPIO_NUM_37}, + {{&machine_pin_irq_type}, GPIO_NUM_38}, + {{&machine_pin_irq_type}, GPIO_NUM_39}, + {{&machine_pin_irq_type}, GPIO_NUM_40}, + {{&machine_pin_irq_type}, GPIO_NUM_41}, + {{&machine_pin_irq_type}, GPIO_NUM_42}, + {{&machine_pin_irq_type}, GPIO_NUM_43}, + {{&machine_pin_irq_type}, GPIO_NUM_44}, + {{&machine_pin_irq_type}, GPIO_NUM_45}, + + #endif }; STATIC mp_obj_t machine_pin_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index 335157b154902..168ac16d0eef6 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -31,7 +31,11 @@ #if CONFIG_IDF_TARGET_ESP32 #include "driver/gpio.h" +#if CONFIG_IDF_TARGET_ESP32 #include "driver/touch_pad.h" +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 +#include "driver/touch_sensor.h" +#endif typedef struct _mtp_obj_t { mp_obj_base_t base; @@ -39,6 +43,7 @@ typedef struct _mtp_obj_t { touch_pad_t touchpad_id; } mtp_obj_t; +#if CONFIG_IDF_TARGET_ESP32 STATIC const mtp_obj_t touchpad_obj[] = { {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0}, {{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1}, @@ -51,6 +56,24 @@ STATIC const mtp_obj_t touchpad_obj[] = { {{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8}, {{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9}, }; +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 +STATIC const mtp_obj_t touchpad_obj[] = { + {{&machine_touchpad_type}, GPIO_NUM_1, TOUCH_PAD_NUM1}, + {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2}, + {{&machine_touchpad_type}, GPIO_NUM_3, TOUCH_PAD_NUM3}, + {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM4}, + {{&machine_touchpad_type}, GPIO_NUM_5, TOUCH_PAD_NUM5}, + {{&machine_touchpad_type}, GPIO_NUM_6, TOUCH_PAD_NUM6}, + {{&machine_touchpad_type}, GPIO_NUM_7, TOUCH_PAD_NUM7}, + {{&machine_touchpad_type}, GPIO_NUM_8, TOUCH_PAD_NUM8}, + {{&machine_touchpad_type}, GPIO_NUM_9, TOUCH_PAD_NUM9}, + {{&machine_touchpad_type}, GPIO_NUM_10, TOUCH_PAD_NUM10}, + {{&machine_touchpad_type}, GPIO_NUM_11, TOUCH_PAD_NUM11}, + {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12}, + {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13}, + {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14}, +}; +#endif STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 7ca3d84140cf1..ff6dd69574ba6 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -42,6 +42,8 @@ #include "esp32/spiram.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/spiram.h" #endif #include "py/stackctrl.h" @@ -104,6 +106,18 @@ void mp_task(void *pvParameter) { mp_task_heap = malloc(mp_task_heap_size); break; } + #elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT + // Try to use the entire external SPIRAM directly for the heap + size_t mp_task_heap_size; + size_t esp_spiram_size = esp_spiram_get_size(); + void *mp_task_heap = (void *)0x3ff80000 - esp_spiram_size; + if (esp_spiram_size > 0) { + mp_task_heap_size = esp_spiram_size; + } else { + // No SPIRAM, fallback to normal allocation + mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); + mp_task_heap = malloc(mp_task_heap_size); + } #else // Allocate the uPy heap using malloc and get the largest available region size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 656045da90a21..1cc30b71ec558 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -128,6 +128,9 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND IDF_COMPONENTS esp32s2) list(APPEND IDF_COMPONENTS tinyusb) +elseif(IDF_TARGET STREQUAL "esp32s3") + list(APPEND IDF_COMPONENTS esp32s3) + list(APPEND IDF_COMPONENTS tinyusb) endif() # Register the main IDF component. diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 2eb5cd2feed51..bbe7fae0335e4 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -42,6 +42,9 @@ #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/rtc.h" #include "esp32s2/clk.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/rtc.h" +#include "esp32s3/clk.h" #endif #include "py/obj.h" diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index cf668216df9e3..a0bafb755e781 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -32,7 +32,14 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" + +#if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/uart.h" +#endif #include "py/obj.h" #include "py/objstr.h" diff --git a/ports/esp32/partitions-16MiB.csv b/ports/esp32/partitions-16MiB.csv new file mode 100644 index 0000000000000..5133185c78b2e --- /dev/null +++ b/ports/esp32/partitions-16MiB.csv @@ -0,0 +1,8 @@ +# Notes: the offset of the partition table itself is set in +# $ESPIDF/components/partition_table/Kconfig.projbuild and the +# offset of the factory/ota_0 partition is set in makeimg.py +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 0x180000, +vfs, data, fat, 0x200000, 0xD59F80, From 32ec07a3504d6dcbfc53997197a3c53e8c03497f Mon Sep 17 00:00:00 2001 From: Seon Rozenblum Date: Fri, 19 Mar 2021 18:48:34 +1100 Subject: [PATCH 0841/3533] esp32/boards: Rename TINYPICO board to UM_TINYPICO. And add default hardware SPI 0 pins in mpconfigboard.h, and CONFIG_LWIP_LOCAL_HOSTNAME in sdkconfig.board. --- ports/esp32/boards/TINYPICO/mpconfigboard.h | 2 -- ports/esp32/boards/{TINYPICO => UM_TINYPICO}/manifest.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/modules/dotstar.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/modules/tinypico.py | 0 .../boards/{TINYPICO => UM_TINYPICO}/mpconfigboard.cmake | 0 ports/esp32/boards/UM_TINYPICO/mpconfigboard.h | 6 ++++++ .../esp32/boards/{TINYPICO => UM_TINYPICO}/sdkconfig.board | 1 + 7 files changed, 7 insertions(+), 2 deletions(-) delete mode 100644 ports/esp32/boards/TINYPICO/mpconfigboard.h rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/manifest.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/modules/dotstar.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/modules/tinypico.py (100%) rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/mpconfigboard.cmake (100%) create mode 100644 ports/esp32/boards/UM_TINYPICO/mpconfigboard.h rename ports/esp32/boards/{TINYPICO => UM_TINYPICO}/sdkconfig.board (72%) diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.h b/ports/esp32/boards/TINYPICO/mpconfigboard.h deleted file mode 100644 index e63f43ed25a2f..0000000000000 --- a/ports/esp32/boards/TINYPICO/mpconfigboard.h +++ /dev/null @@ -1,2 +0,0 @@ -#define MICROPY_HW_BOARD_NAME "TinyPICO" -#define MICROPY_HW_MCU_NAME "ESP32-PICO-D4" diff --git a/ports/esp32/boards/TINYPICO/manifest.py b/ports/esp32/boards/UM_TINYPICO/manifest.py similarity index 100% rename from ports/esp32/boards/TINYPICO/manifest.py rename to ports/esp32/boards/UM_TINYPICO/manifest.py diff --git a/ports/esp32/boards/TINYPICO/modules/dotstar.py b/ports/esp32/boards/UM_TINYPICO/modules/dotstar.py similarity index 100% rename from ports/esp32/boards/TINYPICO/modules/dotstar.py rename to ports/esp32/boards/UM_TINYPICO/modules/dotstar.py diff --git a/ports/esp32/boards/TINYPICO/modules/tinypico.py b/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py similarity index 100% rename from ports/esp32/boards/TINYPICO/modules/tinypico.py rename to ports/esp32/boards/UM_TINYPICO/modules/tinypico.py diff --git a/ports/esp32/boards/TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake similarity index 100% rename from ports/esp32/boards/TINYPICO/mpconfigboard.cmake rename to ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake diff --git a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h new file mode 100644 index 0000000000000..05ee5eaf72a5f --- /dev/null +++ b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h @@ -0,0 +1,6 @@ +#define MICROPY_HW_BOARD_NAME "TinyPICO" +#define MICROPY_HW_MCU_NAME "ESP32-PICO-D4" + +#define MICROPY_HW_SPI1_SCK (18) +#define MICROPY_HW_SPI1_MOSI (23) +#define MICROPY_HW_SPI1_MISO (19) diff --git a/ports/esp32/boards/TINYPICO/sdkconfig.board b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board similarity index 72% rename from ports/esp32/boards/TINYPICO/sdkconfig.board rename to ports/esp32/boards/UM_TINYPICO/sdkconfig.board index dc2c23f674e84..766419c7f81a3 100644 --- a/ports/esp32/boards/TINYPICO/sdkconfig.board +++ b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board @@ -2,3 +2,4 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y CONFIG_SPIRAM_SPEED_80M=y CONFIG_ESP32_REV_MIN_1=y +CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyPICO" From 0e87459e2bfd073a084fe4af00c2f75d4c815639 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 11:08:00 +1000 Subject: [PATCH 0842/3533] esp32/boards: Add UM_FEATHERS2 and UM_TINYS2 board definitions. Based on original commit made by Seon Rozenblum aka @UnexpectedMaker. Signed-off-by: Damien George --- ports/esp32/boards/UM_FEATHERS2/manifest.py | 3 + .../boards/UM_FEATHERS2/modules/feathers2.py | 101 ++++++++++++++++++ .../boards/UM_FEATHERS2/mpconfigboard.cmake | 9 ++ .../esp32/boards/UM_FEATHERS2/mpconfigboard.h | 5 + .../esp32/boards/UM_FEATHERS2/sdkconfig.board | 16 +++ ports/esp32/boards/UM_TINYS2/manifest.py | 2 + .../esp32/boards/UM_TINYS2/modules/tinys2.py | 82 ++++++++++++++ .../boards/UM_TINYS2/mpconfigboard.cmake | 8 ++ ports/esp32/boards/UM_TINYS2/mpconfigboard.h | 9 ++ ports/esp32/boards/UM_TINYS2/sdkconfig.board | 6 ++ 10 files changed, 241 insertions(+) create mode 100644 ports/esp32/boards/UM_FEATHERS2/manifest.py create mode 100644 ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py create mode 100644 ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake create mode 100644 ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h create mode 100644 ports/esp32/boards/UM_FEATHERS2/sdkconfig.board create mode 100644 ports/esp32/boards/UM_TINYS2/manifest.py create mode 100644 ports/esp32/boards/UM_TINYS2/modules/tinys2.py create mode 100644 ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake create mode 100644 ports/esp32/boards/UM_TINYS2/mpconfigboard.h create mode 100644 ports/esp32/boards/UM_TINYS2/sdkconfig.board diff --git a/ports/esp32/boards/UM_FEATHERS2/manifest.py b/ports/esp32/boards/UM_FEATHERS2/manifest.py new file mode 100644 index 0000000000000..82ad0c7e49888 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/manifest.py @@ -0,0 +1,3 @@ +include("$(PORT_DIR)/boards/manifest.py") +freeze("$(PORT_DIR)/boards/UM_TINYPICO/modules", "dotstar.py") +freeze("modules") diff --git a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py new file mode 100644 index 0000000000000..32126fc9c9ae7 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py @@ -0,0 +1,101 @@ +# FeatherS2 MicroPython Helper Library +# 2021 Seon Rozenblum - Unexpected Maker +# +# Project home: +# https://feathers2.io +# +# 2021-Mar-21 - v0.1 - Initial implementation + +# Import required libraries +from micropython import const +from machine import Pin, SPI, ADC +import machine, time + +# FeatherS2 Hardware Pin Assignments + +# LDO +LDO2 = const(21) + +# APA102 Dotstar pins +DOTSTAR_CLK = const(45) +DOTSTAR_DATA = const(40) + +# SPI +SPI_MOSI = const(35) +SPI_MISO = const(36) +SPI_CLK = const(37) + +# I2C +I2C_SDA = const(38) +I2C_SCL = const(33) + +# DAC +DAC1 = const(17) +DAC2 = const(18) + +# LED & Ambient Light Sensor +LED = const(13) +AMB_LIGHT = const(4) + +# Helper functions + +# LED & Ambient Light Sensor control +def set_led(state): + l = Pin(LED, Pin.OUT) + l.value(state) + + +def toggle_led(state): + l = Pin(LED, Pin.OUT) + l.value(not l.value()) + + +# Create ADC and set attenuation and return teh ambient light value from the onboard sensor +def get_amb_light(): + adc = ADC(Pin(AMB_LIGHT)) + adc.atten(ADC.ATTN_11DB) + return adc.read() + + +# LDO2 power control +# When we manually turn off the second LDO we also set the DotStar DATA and CLK pins to input to +# prevent parasitic power from lighting the LED even with the LDO off, causing current use. +# The DotStar is a beautiful LED, but parasitic power makes it a terrible choice for battery use :( +def set_ldo2_power(state): + """Set the power for the on-board Dostar to allow no current draw when not needed.""" + # Set the power pin to the inverse of state + ldo2 = Pin(LDO2, Pin.OUT) + ldo2.value(state) + + if state: + Pin(DOTSTAR_CLK, Pin.OUT) + Pin(DOTSTAR_DATA, Pin.OUT) # If power is on, set CLK to be output, otherwise input + else: + Pin(DOTSTAR_CLK, Pin.IN) + Pin(DOTSTAR_DATA, Pin.IN) # If power is on, set CLK to be output, otherwise input + + # A small delay to let the IO change state + time.sleep(0.035) + + +# Dotstar rainbow colour wheel +def dotstar_color_wheel(wheel_pos): + """Color wheel to allow for cycling through the rainbow of RGB colors.""" + wheel_pos = wheel_pos % 255 + + if wheel_pos < 85: + return 255 - wheel_pos * 3, 0, wheel_pos * 3 + elif wheel_pos < 170: + wheel_pos -= 85 + return 0, wheel_pos * 3, 255 - wheel_pos * 3 + else: + wheel_pos -= 170 + return wheel_pos * 3, 255 - wheel_pos * 3, 0 + + +# Go into deep sleep but shut down the APA first to save power +# Use this if you want lowest deep sleep current +def go_deepsleep(t): + """Deep sleep helper that also powers down the on-board Dotstar.""" + set_ldo2_power(False) + machine.deepsleep(t) diff --git a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake new file mode 100644 index 0000000000000..5e570d513bb47 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.cmake @@ -0,0 +1,9 @@ +set(IDF_TARGET esp32s2) +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.spiram_sx + boards/sdkconfig.usb + boards/UM_FEATHERS2/sdkconfig.board +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) \ No newline at end of file diff --git a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h new file mode 100644 index 0000000000000..c045adccd9682 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h @@ -0,0 +1,5 @@ +#define MICROPY_HW_BOARD_NAME "FeatherS2" +#define MICROPY_HW_MCU_NAME "ESP32-S2" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_HW_ENABLE_SDCARD (0) \ No newline at end of file diff --git a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board new file mode 100644 index 0000000000000..ccda7bff68129 --- /dev/null +++ b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board @@ -0,0 +1,16 @@ +CONFIG_FLASHMODE_QIO=y +CONFIG_ESPTOOLPY_FLASHFREQ_80M=y +CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y +CONFIG_ESPTOOLPY_AFTER_NORESET=y + +CONFIG_SPIRAM_MEMTEST= + +CONFIG_ESPTOOLPY_FLASHSIZE_4MB= +CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv" +#CONFIG_USB_AND_UART=y + +# LWIP +CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2" +# end of LWIP diff --git a/ports/esp32/boards/UM_TINYS2/manifest.py b/ports/esp32/boards/UM_TINYS2/manifest.py new file mode 100644 index 0000000000000..7ae2ed15d9169 --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/manifest.py @@ -0,0 +1,2 @@ +include("$(PORT_DIR)/boards/manifest.py") +freeze("modules") diff --git a/ports/esp32/boards/UM_TINYS2/modules/tinys2.py b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py new file mode 100644 index 0000000000000..ca59cb123581d --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py @@ -0,0 +1,82 @@ +# TinyS2 MicroPython Helper Library +# 2021 Seon Rozenblum - Unexpected Maker +# +# Project home: +# https://tinys2.io +# +# 2021-Apr-10 - v0.1 - Initial implementation + +# Import required libraries +from micropython import const +from machine import Pin, SPI, ADC +import machine, time + +# TinyS2 Hardware Pin Assignments + +# Sense Pins +VBUS_SENSE = const(21) +VBAT_SENSE = const(3) + + +# RGB LED Pins +RGB_DATA = const(1) +RGB_PWR = const(2) + +# SPI +SPI_MOSI = const(35) +SPI_MISO = const(36) +SPI_CLK = const(37) + +# I2C +I2C_SDA = const(8) +I2C_SCL = const(9) + +# DAC +DAC1 = const(17) +DAC2 = const(18) + + +# Helper functions +def set_pixel_power(state): + """Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power fro deep sleep.""" + Pin(RGB_PWR, Pin.OUT).value(state) + + +def get_battery_voltage(): + """ + Returns the current battery voltage. If no battery is connected, returns 4.2V which is the charge voltage + This is an approximation only, but useful to detect if the charge state of the battery is getting low. + """ + adc = ADC(Pin(VBAT_SENSE)) # Assign the ADC pin to read + measuredvbat = adc.read() # Read the value + measuredvbat /= 8192 # divide by 8192 as we are using the default ADC voltage range of 0-1V + measuredvbat *= 4.2 # Multiply by 4.2V, our reference voltage + return round(measuredvbat, 2) + + +def get_vbus_present(): + """Detect if VBUS (5V) power source is present""" + return Pin(VBUS_SENSE, Pin.IN).value() == 1 + + +# Dotstar rainbow colour wheel +def rgb_color_wheel(wheel_pos): + """Color wheel to allow for cycling through the rainbow of RGB colors.""" + wheel_pos = wheel_pos % 255 + + if wheel_pos < 85: + return 255 - wheel_pos * 3, 0, wheel_pos * 3 + elif wheel_pos < 170: + wheel_pos -= 85 + return 0, wheel_pos * 3, 255 - wheel_pos * 3 + else: + wheel_pos -= 170 + return wheel_pos * 3, 255 - wheel_pos * 3, 0 + + +# Go into deep sleep but shut down the APA first to save power +# Use this if you want lowest deep sleep current +def go_deepsleep(t): + """Deep sleep helper that also powers down the on-board Dotstar.""" + set_pixel_power(False) + machine.deepsleep(t) diff --git a/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake b/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake new file mode 100644 index 0000000000000..928f9f8fc35d2 --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/mpconfigboard.cmake @@ -0,0 +1,8 @@ +set(IDF_TARGET esp32s2) +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.spiram_sx + boards/sdkconfig.usb +) + +set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py) diff --git a/ports/esp32/boards/UM_TINYS2/mpconfigboard.h b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h new file mode 100644 index 0000000000000..87d266e58c52a --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h @@ -0,0 +1,9 @@ +#define MICROPY_HW_BOARD_NAME "TinyS2" +#define MICROPY_HW_MCU_NAME "ESP32-S2FN4R2" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_HW_ENABLE_SDCARD (0) + +#define MICROPY_HW_SPI1_MOSI (35) +#define MICROPY_HW_SPI1_MISO (36) +#define MICROPY_HW_SPI1_SCK (37) diff --git a/ports/esp32/boards/UM_TINYS2/sdkconfig.board b/ports/esp32/boards/UM_TINYS2/sdkconfig.board new file mode 100644 index 0000000000000..48b6749c720db --- /dev/null +++ b/ports/esp32/boards/UM_TINYS2/sdkconfig.board @@ -0,0 +1,6 @@ +CONFIG_FLASHMODE_QIO=y +CONFIG_ESPTOOLPY_FLASHFREQ_80M=y +CONFIG_USB_AND_UART=y +# LWIP +CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2" +# end of LWIP From cce7096d1a031f43f72c2ea87513301c3db118dc Mon Sep 17 00:00:00 2001 From: Chris Greening Date: Mon, 10 May 2021 18:17:31 +0100 Subject: [PATCH 0843/3533] esp32/boards/UM_TINYPICO: Fix include of sdkconfig fragment. This was broken by 32ec07a3504d6dcbfc53997197a3c53e8c03497f --- ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake index 0c65103a85347..bc2541c69969d 100644 --- a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake +++ b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.cmake @@ -3,7 +3,7 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.ble boards/sdkconfig.240mhz boards/sdkconfig.spiram - boards/TINYPICO/sdkconfig.board + boards/UM_TINYPICO/sdkconfig.board ) if(NOT MICROPY_FROZEN_MANIFEST) From 4404dababb715916262ca9aa45a5732d8454eda0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 11 May 2021 12:46:18 +1000 Subject: [PATCH 0844/3533] rp2/CMakeLists.txt: Include tinyusb_common in PICO_SDK_COMPONENTS. So the TinyUSB headers can be found during qstr processing. Fixes issue #7236. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 699520b240849..50cdba4ba08f8 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -154,6 +154,7 @@ set(PICO_SDK_COMPONENTS pico_sync pico_time pico_unique_id + tinyusb_common tinyusb_device ) From 70f50c46cc8ae0cd6802c7998874b5ea7c410119 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 9 Apr 2018 01:00:01 +0200 Subject: [PATCH 0845/3533] lib/utils: Add ARM semihosting utility functions. This can be a replacement for a UART in custom ports. --- lib/utils/semihosting.c | 132 ++++++++++++++++++++++++++++++++++++++++ lib/utils/semihosting.h | 51 ++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 lib/utils/semihosting.c create mode 100644 lib/utils/semihosting.h diff --git a/lib/utils/semihosting.c b/lib/utils/semihosting.c new file mode 100644 index 0000000000000..18c7f5d57a3f3 --- /dev/null +++ b/lib/utils/semihosting.c @@ -0,0 +1,132 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Ayke van Laethem + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "semihosting.h" + +// Resources: +// http://embed.rs/articles/2016/semi-hosting-rust/ +// https://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22 +// https://github.com/arduino/OpenOCD/blob/master/src/target/arm_semihosting.c + +#define SYS_OPEN 0x01 +#define SYS_WRITEC 0x03 +#define SYS_WRITE 0x05 +#define SYS_READC 0x07 + +// Constants: +#define OPEN_MODE_READ (0) // mode "r" +#define OPEN_MODE_WRITE (4) // mode "w" + +#ifndef __thumb__ +#error Semihosting is only implemented for ARM microcontrollers. +#endif + +static int mp_semihosting_stdout; + +static uint32_t mp_semihosting_call(uint32_t num, const void *arg) { + // A semihosting call works as follows, similar to a SVCall: + // * the call is invoked by a special breakpoint: 0xAB + // * the command is placed in r0 + // * a pointer to the arguments is placed in r1 + // * the return value is placed in r0 + // Note that because it uses the breakpoint instruction, applications + // will hang if they're not connected to a debugger. And they'll be + // stuck in a breakpoint if semihosting is not specifically enabled in + // the debugger. + // Also note that semihosting is extremely slow (sometimes >100ms per + // call). + register uint32_t num_reg __asm__ ("r0") = num; + register const void *args_reg __asm__ ("r1") = arg; + __asm__ __volatile__ ( + "bkpt 0xAB\n" // invoke semihosting call + : "+r" (num_reg) // call number and result + : "r" (args_reg) // arguments + : "memory"); // make sure args aren't optimized away + return num_reg; // r0, which became the result +} + +static int mp_semihosting_open_console(uint32_t mode) { + struct { + char *name; + uint32_t mode; + uint32_t name_len; + } args = { + .name = ":tt", // magic path to console + .mode = mode, // e.g. "r", "w" (see OPEN_MODE_* constants) + .name_len = 3, // strlen(":tt") + }; + return mp_semihosting_call(SYS_OPEN, &args); +} + +void mp_semihosting_init() { + mp_semihosting_stdout = mp_semihosting_open_console(OPEN_MODE_WRITE); +} + +int mp_semihosting_rx_char() { + return mp_semihosting_call(SYS_READC, NULL); +} + +static void mp_semihosting_tx_char(char c) { + mp_semihosting_call(SYS_WRITEC, &c); +} + +uint32_t mp_semihosting_tx_strn(const char *str, size_t len) { + if (len == 0) { + return 0; // nothing to do + } + if (len == 1) { + mp_semihosting_tx_char(*str); // maybe faster? + return 0; + } + + struct { + uint32_t fd; + const char *str; + uint32_t len; + } args = { + .fd = mp_semihosting_stdout, + .str = str, + .len = len, + }; + return mp_semihosting_call(SYS_WRITE, &args); +} + +uint32_t mp_semihosting_tx_strn_cooked(const char *str, size_t len) { + // Write chunks of data until (excluding) the first '\n' character, + // insert a '\r' character, and then continue with the next chunk + // (starting with '\n'). + // Doing byte-by-byte writes would be easier to implement but is far + // too slow. + size_t start = 0; + for (size_t i = 0; i < len; i++) { + if (str[i] == '\n') { + mp_semihosting_tx_strn(str + start, i - start); + mp_semihosting_tx_char('\r'); + start = i; + } + } + return mp_semihosting_tx_strn(str + start, len - start); +} diff --git a/lib/utils/semihosting.h b/lib/utils/semihosting.h new file mode 100644 index 0000000000000..d053a03edaae9 --- /dev/null +++ b/lib/utils/semihosting.h @@ -0,0 +1,51 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Ayke van Laethem + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_LIB_UTILS_SEMIHOSTING_H +#define MICROPY_INCLUDED_LIB_UTILS_SEMIHOSTING_H + +/* + +To use semi-hosting for a replacement UART: +- Add lib/semihosting/semihosting.c to the Makefile sources. +- Call mp_semihosting_init() in main(), around the time UART is initialized. +- Replace mp_hal_stdin_rx_chr and similar in mphalport.c with the semihosting equivalent. +- Include lib/semihosting/semihosting.h in the relevant files. + +Then make sure the debugger is attached and enables semihosting. In OpenOCD this is +done with ARM semihosting enable followed by reset. The terminal will need further +configuration to work with MicroPython (bash: stty raw -echo). + +*/ + +#include +#include + +void mp_semihosting_init(); +int mp_semihosting_rx_char(); +uint32_t mp_semihosting_tx_strn(const char *str, size_t len); +uint32_t mp_semihosting_tx_strn_cooked(const char *str, size_t len); + +#endif // MICROPY_INCLUDED_LIB_UTILS_SEMIHOSTING_H From 18d984c8b2053ddc17f02816cb037f13afee685a Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 11 May 2021 23:45:36 +1000 Subject: [PATCH 0846/3533] tests/run-perfbench.py: Fix native feature check. This was broken by 8459f538eb45fd8e1e4d614298449cf18de84d75 Signed-off-by: Damien George --- tests/run-perfbench.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/run-perfbench.py b/tests/run-perfbench.py index 8b71ae64ce2b2..bcdbe69abbf90 100755 --- a/tests/run-perfbench.py +++ b/tests/run-perfbench.py @@ -85,7 +85,7 @@ def run_benchmark_on_target(target, script): def run_benchmarks(target, param_n, param_m, n_average, test_list): skip_complex = run_feature_test(target, "complex") != "complex" - skip_native = run_feature_test(target, "native_check") != "" + skip_native = run_feature_test(target, "native_check") != "native" for test_file in sorted(test_list): print(test_file + ": ", end="") From 87e38b3cc83be7297957b5ec9b701769ffe2d6e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 12 May 2021 00:05:55 +1000 Subject: [PATCH 0847/3533] docs/library/rp2.rst: Fix typo overriden->overridden. Signed-off-by: Damien George --- docs/library/rp2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/rp2.rst b/docs/library/rp2.rst index b6e7fcf429505..5d168bce20262 100644 --- a/docs/library/rp2.rst +++ b/docs/library/rp2.rst @@ -38,7 +38,7 @@ For running PIO programs, see :class:`rp2.StateMachine`. - *sideset_init* configures the pins used side-setting. There can be at most 5. - The following parameters are used by default, but can be overriden in + The following parameters are used by default, but can be overridden in `StateMachine.init()`: - *in_shiftdir* is the default direction the ISR will shift, either From 300fc842cec1d1ef71df95c917374808ae2cafe9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 12 May 2021 11:18:16 +1000 Subject: [PATCH 0848/3533] py/mkenv.mk: Don't emit info about BUILD_VERBOSE if it's set. If the user sets V or BUILD_VERBOSE then they don't need to see this message. Signed-off-by: Damien George --- py/mkenv.mk | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/py/mkenv.mk b/py/mkenv.mk index 371d32046227d..d54f0a0d3190b 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -20,6 +20,7 @@ ifeq ("$(origin V)", "command line") BUILD_VERBOSE=$(V) endif ifndef BUILD_VERBOSE +$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) BUILD_VERBOSE = 0 endif ifeq ($(BUILD_VERBOSE),0) @@ -27,10 +28,6 @@ Q = @ else Q = endif -# Since this is a new feature, advertise it -ifeq ($(BUILD_VERBOSE),0) -$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) -endif # default settings; can be overridden in main Makefile From ee4ffc1804cc39c80128185e7ea96365fa381d7e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 16:52:01 +1000 Subject: [PATCH 0849/3533] stm32/powerctrl: Add MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET option. When disabled the bootloader is entered via a direct jump. When enabled the bootloader is entered via a system reset then a jump. It's enabled by default to retain the existing behaviour, which is the recommended way. Signed-off-by: Damien George --- ports/stm32/mpconfigboard_common.h | 5 +++++ ports/stm32/powerctrl.c | 34 +++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 0df35918bef94..ce9dbdb0af8c0 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -47,6 +47,11 @@ #define MICROPY_PY_PYB_LEGACY (1) #endif +// Whether machine.bootloader() will enter the bootloader via reset, or direct jump. +#ifndef MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET +#define MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET (1) +#endif + // Whether to enable storage on the internal flash of the MCU #ifndef MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE #define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 573844063b664..253f1056cabe1 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -66,9 +66,11 @@ #define HAVE_PLL48 0 #endif +#if MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET // Location in RAM of bootloader state (just after the top of the stack) extern uint32_t _estack[]; #define BL_STATE ((uint32_t *)&_estack) +#endif static inline void powerctrl_disable_hsi_if_unused(void) { #if !MICROPY_HW_CLK_USE_HSI && (defined(STM32F4) || defined(STM32F7) || defined(STM32H7)) @@ -78,32 +80,47 @@ static inline void powerctrl_disable_hsi_if_unused(void) { } NORETURN void powerctrl_mcu_reset(void) { + #if MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET BL_STATE[1] = 1; // invalidate bootloader address #if __DCACHE_PRESENT == 1 SCB_CleanDCache(); #endif + #endif NVIC_SystemReset(); } +NORETURN static __attribute__((naked)) void branch_to_bootloader(uint32_t r0, uint32_t bl_addr) { + __asm volatile ( + "ldr r2, [r1, #0]\n" // get address of stack pointer + "msr msp, r2\n" // get stack pointer + "ldr r2, [r1, #4]\n" // get address of destination + "bx r2\n" // branch to bootloader + ); + MP_UNREACHABLE; +} + NORETURN void powerctrl_enter_bootloader(uint32_t r0, uint32_t bl_addr) { + #if MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET + + // Enter the bootloader via a reset, so everything is reset (including WDT). + // Upon reset powerctrl_check_enter_bootloader() will jump to the bootloader. BL_STATE[0] = r0; BL_STATE[1] = bl_addr; #if __DCACHE_PRESENT == 1 SCB_CleanDCache(); #endif NVIC_SystemReset(); -} -static __attribute__((naked)) void branch_to_bootloader(uint32_t r0, uint32_t bl_addr) { - __asm volatile ( - "ldr r2, [r1, #0]\n" // get address of stack pointer - "msr msp, r2\n" // get stack pointer - "ldr r2, [r1, #4]\n" // get address of destination - "bx r2\n" // branch to bootloader - ); + #else + + // Enter the bootloader via a direct jump. + branch_to_bootloader(r0, bl_addr); + + #endif } void powerctrl_check_enter_bootloader(void) { + #if MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET uint32_t bl_addr = BL_STATE[1]; BL_STATE[1] = 1; // invalidate bootloader address if ((bl_addr & 0xfff) == 0 && (RCC->RCC_SR & RCC_SR_SFTRSTF)) { @@ -115,6 +132,7 @@ void powerctrl_check_enter_bootloader(void) { uint32_t r0 = BL_STATE[0]; branch_to_bootloader(r0, bl_addr); } + #endif } #if !defined(STM32F0) && !defined(STM32L0) && !defined(STM32WB) From 9ee116c452e6569d70cdebaade5dd25a1555b82b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 May 2021 10:49:43 +1000 Subject: [PATCH 0850/3533] stm32/boardctrl: Adjust logic for running boot.py, main.py. This new logic is equivalent to the old logic when the only possibilities for reset_mode are NORMAL, SAFE_MODE and FILESYSTEM, which is the standard case. But the new logic also allows other reset_mode values (eg BOOTLOADER) to run boot.py and main.py. Signed-off-by: Damien George --- ports/stm32/boardctrl.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 8cc519d8e9d7d..f1b7a4e81cc13 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -143,8 +143,7 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { } int boardctrl_run_boot_py(boardctrl_state_t *state) { - bool run_boot_py = state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL - || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM; + bool run_boot_py = state->reset_mode != BOARDCTRL_RESET_MODE_SAFE_MODE; if (run_boot_py) { // Run boot.py, if it exists. @@ -176,8 +175,7 @@ int boardctrl_run_boot_py(boardctrl_state_t *state) { } int boardctrl_run_main_py(boardctrl_state_t *state) { - bool run_main_py = (state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL - || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) + bool run_main_py = state->reset_mode != BOARDCTRL_RESET_MODE_SAFE_MODE && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; if (run_main_py) { From 6639e282c762b1f878c2c66a1e0153ee8cb61e1a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 12 May 2021 13:29:01 +1000 Subject: [PATCH 0851/3533] stm32/mboot: Add MBOOT_LEAVE_BOOTLOADER_VIA_RESET option. It is enabled by default to get the standard behaviour of doing a reset after it is finished, but can be disabled by a board to jump straight to the application (likely the board needs to use MBOOT_BOARD_CLEANUP to make this work). The application is passed a reset mode of BOARDCTRL_RESET_MODE_BOOTLOADER if the bootloader was active and entered via a jump. Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 64 ++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 36b6890361572..2bee5a666f632 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -40,6 +40,11 @@ #include "dfu.h" #include "pack.h" +// Whether the bootloader will leave via reset, or direct jump to the application. +#ifndef MBOOT_LEAVE_BOOTLOADER_VIA_RESET +#define MBOOT_LEAVE_BOOTLOADER_VIA_RESET (1) +#endif + // This option selects whether to use explicit polling or IRQs for USB events. // In some test cases polling mode can run slightly faster, but it uses more power. // Polling mode will also cause failures with the mass-erase command because USB @@ -1329,6 +1334,45 @@ static int get_reset_mode(void) { return reset_mode; } +NORETURN static __attribute__((naked)) void branch_to_application(uint32_t r0, uint32_t bl_addr) { + __asm volatile ( + "ldr r2, [r1, #0]\n" // get address of stack pointer + "msr msp, r2\n" // set stack pointer + "ldr r2, [r1, #4]\n" // get address of destination + "bx r2\n" // branch to application + ); + MP_UNREACHABLE; +} + +static void try_enter_application(int reset_mode) { + uint32_t msp = *(volatile uint32_t*)APPLICATION_ADDR; + if ((msp & APP_VALIDITY_BITS) != 0) { + // Application is invalid. + return; + } + + // undo our DFU settings + // TODO probably should disable all IRQ sources first + #if defined(MBOOT_BOARD_CLEANUP) + MBOOT_BOARD_CLEANUP(reset_mode); + #endif + #if USE_CACHE && defined(STM32F7) + SCB_DisableICache(); + SCB_DisableDCache(); + #endif + + // Jump to the application. + branch_to_application(reset_mode, APPLICATION_ADDR); +} + +static void leave_bootloader(void) { + #if !MBOOT_LEAVE_BOOTLOADER_VIA_RESET + // Try to enter the application via a jump, if it's valid. + try_enter_application(BOARDCTRL_RESET_MODE_BOOTLOADER); + #endif + NVIC_SystemReset(); +} + static void do_reset(void) { led_state_all(0); mp_hal_delay_ms(50); @@ -1337,7 +1381,7 @@ static void do_reset(void) { i2c_slave_shutdown(MBOOT_I2Cx, I2Cx_EV_IRQn); #endif mp_hal_delay_ms(50); - NVIC_SystemReset(); + leave_bootloader(); } extern PCD_HandleTypeDef pcd_fs_handle; @@ -1402,17 +1446,11 @@ void stm32_main(int initial_r0) { } int reset_mode = get_reset_mode(); - uint32_t msp = *(volatile uint32_t*)APPLICATION_ADDR; - if (reset_mode != BOARDCTRL_RESET_MODE_BOOTLOADER && (msp & APP_VALIDITY_BITS) == 0) { - // not DFU mode so jump to application, passing through reset_mode - // undo our DFU settings - // TODO probably should disable all IRQ sources first - #if USE_CACHE && defined(STM32F7) - SCB_DisableICache(); - SCB_DisableDCache(); - #endif - __set_MSP(msp); - ((void (*)(uint32_t)) *((volatile uint32_t*)(APPLICATION_ADDR + 4)))(reset_mode); + if (reset_mode != BOARDCTRL_RESET_MODE_BOOTLOADER) { + // Bootloader mode was not selected so try to enter the application, + // passing through the reset_mode. This will return if the application + // is invalid. + try_enter_application(reset_mode); } enter_bootloader: @@ -1461,7 +1499,7 @@ void stm32_main(int initial_r0) { } // Always reset because the application is expecting to resume led_state_all(0); - NVIC_SystemReset(); + leave_bootloader(); } #endif From 9a0bca2c2a6aef7b032d9c5bde378058c8258a08 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 12 May 2021 13:33:56 +1000 Subject: [PATCH 0852/3533] stm32/mboot: Make LEDs and reset-mode selection more configurable. A board can now customise mboot with: - MBOOT_LED1, MBOOT_LED2, MBOOT_LED3, MBOOT_LED4: if it needs to have different LEDs for mboot compared to the application - MBOOT_BOARD_LED_INIT: if it needs a fully customised LED init function - MBOOT_BOARD_LED_STATE: if it needs a fully customised LED state-setting function - MBOOT_BOARD_GET_RESET_MODE: if it needs a fully customised function to get the reset mode With full customisation, the only requirement is a single LED to show the status of the bootloader (idle, erasing, flashing, etc), which can be configured to do nothing if needed. Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 71 ++++++++++++++++++++++++++++++--------- ports/stm32/mboot/mboot.h | 2 ++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 2bee5a666f632..3a0242a8d6bf0 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -374,12 +374,27 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) { /******************************************************************************/ // LED +#if defined(MBOOT_LED1) +#define LED0 MBOOT_LED1 +#elif defined(MICROPY_HW_LED1) #define LED0 MICROPY_HW_LED1 +#endif + +#if defined(MBOOT_LED2) +#define LED1 MBOOT_LED2 +#elif defined(MICROPY_HW_LED2) #define LED1 MICROPY_HW_LED2 -#ifdef MICROPY_HW_LED3 +#endif + +#if defined(MBOOT_LED3) +#define LED2 MBOOT_LED3 +#elif defined(MICROPY_HW_LED3) #define LED2 MICROPY_HW_LED3 #endif -#ifdef MICROPY_HW_LED4 + +#if defined(MBOOT_LED4) +#define LED3 MBOOT_LED4 +#elif defined(MICROPY_HW_LED4) #define LED3 MICROPY_HW_LED4 #endif @@ -397,28 +412,45 @@ static uint32_t led0_ms_interval = 0; static int led0_toggle_count = 0; MP_WEAK void led_init(void) { + #if defined(MBOOT_BOARD_LED_INIT) + // Custom LED init function provided by the board. + MBOOT_BOARD_LED_INIT(); + #else + // Init LEDs using GPIO calls. mp_hal_pin_output(LED0); + #ifdef LED1 mp_hal_pin_output(LED1); + #endif #ifdef LED2 mp_hal_pin_output(LED2); #endif #ifdef LED3 mp_hal_pin_output(LED3); #endif + #endif + led0_cur_state = LED0_STATE_OFF; } MP_WEAK void led_state(uint32_t led, int val) { + #if defined(MBOOT_BOARD_LED_STATE) + // Custom LED state function provided by the board. + return MBOOT_BOARD_LED_STATE(led, val); + #else + // Set LEDs using GPIO calls. if (val) { MICROPY_HW_LED_ON(led); } else { MICROPY_HW_LED_OFF(led); } + #endif } void led_state_all(unsigned int mask) { led_state(LED0, mask & 1); + #ifdef LED1 led_state(LED1, mask & 2); + #endif #ifdef LED2 led_state(LED2, mask & 4); #endif @@ -445,17 +477,6 @@ void led0_update() { } } -/******************************************************************************/ -// USR BUTTON - -static void usrbtn_init(void) { - mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, 0); -} - -static int usrbtn_state(void) { - return mp_hal_pin_read(MICROPY_HW_USRSW_PIN) == MICROPY_HW_USRSW_PRESSED; -} - /******************************************************************************/ // FLASH @@ -1287,6 +1308,14 @@ static int pyb_usbdd_shutdown(void) { /******************************************************************************/ // main +#if defined(MBOOT_BOARD_GET_RESET_MODE) + +static inline int mboot_get_reset_mode(void) { + return MBOOT_BOARD_GET_RESET_MODE(); +} + +#else + #define RESET_MODE_NUM_STATES (4) #define RESET_MODE_TIMEOUT_CYCLES (8) #ifdef LED2 @@ -1299,7 +1328,15 @@ static int pyb_usbdd_shutdown(void) { #define RESET_MODE_LED_STATES 0x3210 #endif -static int get_reset_mode(void) { +static void usrbtn_init(void) { + mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, 0); +} + +static int usrbtn_state(void) { + return mp_hal_pin_read(MICROPY_HW_USRSW_PIN) == MICROPY_HW_USRSW_PRESSED; +} + +static int mboot_get_reset_mode(void) { usrbtn_init(); int reset_mode = BOARDCTRL_RESET_MODE_NORMAL; if (usrbtn_state()) { @@ -1334,6 +1371,8 @@ static int get_reset_mode(void) { return reset_mode; } +#endif + NORETURN static __attribute__((naked)) void branch_to_application(uint32_t r0, uint32_t bl_addr) { __asm volatile ( "ldr r2, [r1, #0]\n" // get address of stack pointer @@ -1445,7 +1484,7 @@ void stm32_main(int initial_r0) { goto enter_bootloader; } - int reset_mode = get_reset_mode(); + int reset_mode = mboot_get_reset_mode(); if (reset_mode != BOARDCTRL_RESET_MODE_BOOTLOADER) { // Bootloader mode was not selected so try to enter the application, // passing through the reset_mode. This will return if the application @@ -1455,7 +1494,7 @@ void stm32_main(int initial_r0) { enter_bootloader: - // Init subsystems (get_reset_mode() may call these, calling them again is ok) + // Init subsystems (mboot_get_reset_mode() may call these, calling them again is ok) led_init(); // set the system clock to be HSE diff --git a/ports/stm32/mboot/mboot.h b/ports/stm32/mboot/mboot.h index 8f8a8afdd7c0b..3a27ce08efaf6 100644 --- a/ports/stm32/mboot/mboot.h +++ b/ports/stm32/mboot/mboot.h @@ -85,6 +85,8 @@ enum { extern uint8_t _estack[ELEM_DATA_SIZE]; +void systick_init(void); + uint32_t get_le32(const uint8_t *b); void led_state_all(unsigned int mask); From 6affcb0104336e629390b2bfba997f3446f9ea58 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 13 May 2021 16:26:07 +1000 Subject: [PATCH 0853/3533] tests/run-multitests.py: Flush stdout for each line of trace output. Signed-off-by: Damien George --- tests/run-multitests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index 3163a48e63d2b..66ca799675454 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -243,6 +243,7 @@ def trace_instance_output(instance_idx, line): if cmd_args.trace_output: t_ms = round((time.time() - trace_t0) * 1000) print("{:6} i{} :".format(t_ms, instance_idx), line) + sys.stdout.flush() def run_test_on_instances(test_file, num_instances, instances): From 57365d855734142deb030ebcd00c10efcedf554b Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 12 May 2021 17:02:06 +0200 Subject: [PATCH 0854/3533] py/objarray: Prohibit comparison of mismatching types. Array equality is defined as each element being equal but to keep code size down MicroPython implements a binary comparison. This can only be used correctly for elements with the same binary layout though so turn it into an NotImplementedError when comparing types for which the binary comparison yielded incorrect results: types with different sizes, and floating point numbers because nan != nan. --- py/objarray.c | 25 +++++++++++++++++++++++- tests/basics/array1.py | 9 +++++++++ tests/basics/array_micropython.py | 12 ++++++++++++ tests/cpydiff/module_array_comparison.py | 9 +++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/cpydiff/module_array_comparison.py diff --git a/py/objarray.c b/py/objarray.c index c27366d7208ef..2f1f68d81a40d 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -258,6 +258,16 @@ STATIC mp_obj_t array_unary_op(mp_unary_op_t op, mp_obj_t o_in) { } } +STATIC int typecode_for_comparison(int typecode) { + if (typecode == BYTEARRAY_TYPECODE) { + typecode = 'B'; + } + if (typecode <= 'Z') { + typecode += 32; // to lowercase + } + return typecode; +} + STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in); switch (op) { @@ -319,7 +329,20 @@ STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) { return mp_const_false; } - return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len)); + // mp_seq_cmp_bytes is used so only compatible representations can be correctly compared. + // The type doesn't matter: array/bytearray/str/bytes all have the same buffer layout, so + // just check if the typecodes are compatible; for testing equality the types should have the + // same code except for signedness, and not be floating point because nan never equals nan. + // Note that typecode_for_comparison always returns lowercase letters to save code size. + // No need for (& TYPECODE_MASK) here: xxx_get_buffer already takes care of that. + const int lhs_code = typecode_for_comparison(lhs_bufinfo.typecode); + const int rhs_code = typecode_for_comparison(rhs_bufinfo.typecode); + if (lhs_code == rhs_code && lhs_code != 'f' && lhs_code != 'd') { + return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len)); + } + // mp_obj_equal_not_equal treats returning MP_OBJ_NULL as 'fall back to pointer comparison' + // for MP_BINARY_OP_EQUAL but that is incompatible with CPython. + mp_raise_NotImplementedError(NULL); } default: diff --git a/tests/basics/array1.py b/tests/basics/array1.py index 5b3f475786a28..15789e2c99887 100644 --- a/tests/basics/array1.py +++ b/tests/basics/array1.py @@ -41,14 +41,23 @@ # equality (CPython requires both sides are array) print(bytes(array.array('b', [0x61, 0x62, 0x63])) == b'abc') print(array.array('b', [0x61, 0x62, 0x63]) == b'abc') +print(array.array('B', [0x61, 0x62, 0x63]) == b'abc') print(array.array('b', [0x61, 0x62, 0x63]) != b'abc') print(array.array('b', [0x61, 0x62, 0x63]) == b'xyz') print(array.array('b', [0x61, 0x62, 0x63]) != b'xyz') print(b'abc' == array.array('b', [0x61, 0x62, 0x63])) +print(b'abc' == array.array('B', [0x61, 0x62, 0x63])) print(b'abc' != array.array('b', [0x61, 0x62, 0x63])) print(b'xyz' == array.array('b', [0x61, 0x62, 0x63])) print(b'xyz' != array.array('b', [0x61, 0x62, 0x63])) +compatible_typecodes = [] +for t in ["b", "h", "i", "l", "q"]: + compatible_typecodes.append((t, t)) + compatible_typecodes.append((t, t.upper())) +for a, b in compatible_typecodes: + print(array.array(a, [1, 2]) == array.array(b, [1, 2])) + class X(array.array): pass diff --git a/tests/basics/array_micropython.py b/tests/basics/array_micropython.py index 6b3dc7a93b151..44dc1d83d8efd 100644 --- a/tests/basics/array_micropython.py +++ b/tests/basics/array_micropython.py @@ -17,3 +17,15 @@ a = array.array('P') a.append(1) print(a[0]) + +# comparison between mismatching binary layouts is not implemented +typecodes = ["b", "h", "i", "l", "q", "P", "O", "S", "f", "d"] +for a in typecodes: + for b in typecodes: + if a == b and a not in ["f", "d"]: + continue + try: + array.array(a) == array.array(b) + print('FAIL') + except NotImplementedError: + pass diff --git a/tests/cpydiff/module_array_comparison.py b/tests/cpydiff/module_array_comparison.py new file mode 100644 index 0000000000000..a442af3f5bc96 --- /dev/null +++ b/tests/cpydiff/module_array_comparison.py @@ -0,0 +1,9 @@ +""" +categories: Modules,array +description: Comparison between different typecodes not supported +cause: Code size +workaround: Compare individual elements +""" +import array + +array.array("b", [1, 2]) == array.array("i", [1, 2]) From 09be0c083cb493c70b38079aa6fd785aa9b0b90a Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 10 May 2021 16:40:51 +0200 Subject: [PATCH 0855/3533] py/objarray: Implement more/less comparisons for array. --- py/objarray.c | 17 ++++++++++++----- tests/basics/array1.py | 21 +++++++++++++++++++++ tests/basics/bytearray1.py | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/py/objarray.c b/py/objarray.c index 2f1f68d81a40d..5bd9454716a4f 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -258,12 +258,13 @@ STATIC mp_obj_t array_unary_op(mp_unary_op_t op, mp_obj_t o_in) { } } -STATIC int typecode_for_comparison(int typecode) { +STATIC int typecode_for_comparison(int typecode, bool *is_unsigned) { if (typecode == BYTEARRAY_TYPECODE) { typecode = 'B'; } if (typecode <= 'Z') { typecode += 32; // to lowercase + *is_unsigned = true; } return typecode; } @@ -322,7 +323,11 @@ STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs return mp_const_false; } - case MP_BINARY_OP_EQUAL: { + case MP_BINARY_OP_EQUAL: + case MP_BINARY_OP_LESS: + case MP_BINARY_OP_LESS_EQUAL: + case MP_BINARY_OP_MORE: + case MP_BINARY_OP_MORE_EQUAL: { mp_buffer_info_t lhs_bufinfo; mp_buffer_info_t rhs_bufinfo; array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ); @@ -333,11 +338,13 @@ STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs // The type doesn't matter: array/bytearray/str/bytes all have the same buffer layout, so // just check if the typecodes are compatible; for testing equality the types should have the // same code except for signedness, and not be floating point because nan never equals nan. + // For > and < the types should be the same and unsigned. // Note that typecode_for_comparison always returns lowercase letters to save code size. // No need for (& TYPECODE_MASK) here: xxx_get_buffer already takes care of that. - const int lhs_code = typecode_for_comparison(lhs_bufinfo.typecode); - const int rhs_code = typecode_for_comparison(rhs_bufinfo.typecode); - if (lhs_code == rhs_code && lhs_code != 'f' && lhs_code != 'd') { + bool is_unsigned = false; + const int lhs_code = typecode_for_comparison(lhs_bufinfo.typecode, &is_unsigned); + const int rhs_code = typecode_for_comparison(rhs_bufinfo.typecode, &is_unsigned); + if (lhs_code == rhs_code && lhs_code != 'f' && lhs_code != 'd' && (op == MP_BINARY_OP_EQUAL || is_unsigned)) { return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len)); } // mp_obj_equal_not_equal treats returning MP_OBJ_NULL as 'fall back to pointer comparison' diff --git a/tests/basics/array1.py b/tests/basics/array1.py index 15789e2c99887..f21ad4bd75f90 100644 --- a/tests/basics/array1.py +++ b/tests/basics/array1.py @@ -66,3 +66,24 @@ class X(array.array): print(X('b', [0x61, 0x62, 0x63]) != b'abc') print(X('b', [0x61, 0x62, 0x63]) == array.array('b', [0x61, 0x62, 0x63])) print(X('b', [0x61, 0x62, 0x63]) != array.array('b', [0x61, 0x62, 0x63])) + +# other comparisons +for typecode in ["B", "H", "I", "L", "Q"]: + a = array.array(typecode, [1, 1]) + print(a < a) + print(a <= a) + print(a > a) + print(a >= a) + + al = array.array(typecode, [1, 0]) + ab = array.array(typecode, [1, 2]) + + print(a < al) + print(a <= al) + print(a > al) + print(a >= al) + + print(a < ab) + print(a <= ab) + print(a > ab) + print(a >= ab) diff --git a/tests/basics/bytearray1.py b/tests/basics/bytearray1.py index b598500264fbe..d12292e8798ca 100644 --- a/tests/basics/bytearray1.py +++ b/tests/basics/bytearray1.py @@ -27,6 +27,26 @@ print(b"1" == bytearray([1])) print(bytearray() == bytearray()) +b1 = bytearray([1, 2, 3]) +b2 = bytearray([1, 2, 3]) +b3 = bytearray([1, 3]) +print(b1 == b2) +print(b2 != b3) +print(b1 <= b2) +print(b1 <= b3) +print(b1 < b3) +print(b1 >= b2) +print(b3 >= b2) +print(b3 > b2) +print(b1 != b2) +print(b2 == b3) +print(b1 > b2) +print(b1 > b3) +print(b1 >= b3) +print(b1 < b2) +print(b3 < b2) +print(b3 <= b2) + # comparison with other type should return False print(bytearray() == 1) From 43e7e5f00a601b36b5b39f1fef036d6c25d5562c Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 11 May 2021 12:54:03 +1000 Subject: [PATCH 0856/3533] lib/lwip: Switch to use GitHub mirror repo. It is hopefully more reliable. Signed-off-by: Damien George --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index ceaa5342b412c..faa89c4f5cea1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,7 +7,7 @@ url = https://github.com/atgreen/libffi [submodule "lib/lwip"] path = lib/lwip - url = https://git.savannah.gnu.org/r/lwip.git + url = https://github.com/lwip-tcpip/lwip.git [submodule "lib/berkeley-db-1.xx"] path = lib/berkeley-db-1.xx url = https://github.com/pfalcon/berkeley-db-1.xx From 94a3f8a4b0b29e49b8f83c522b5478c94e4d3643 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 11 May 2021 08:24:36 -0500 Subject: [PATCH 0857/3533] tests/run-tests.py: Parallelize running tests by default. This significantly reduces the time taken to run the test suite (on the unix port). Use `-j1` to disable this feature. Signed-off-by: Jeff Epler --- tests/run-tests.py | 92 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/tests/run-tests.py b/tests/run-tests.py index a5a9b60637cc3..619df5ed361cb 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -8,6 +8,10 @@ import inspect import re from glob import glob +import multiprocessing +from multiprocessing.pool import ThreadPool +import threading +import tempfile # See stackoverflow.com/questions/2632199: __file__ nor sys.argv[0] # are guaranteed to always work, this one should though. @@ -157,12 +161,14 @@ def send_get(what): # if running via .mpy, first compile the .py file if args.via_mpy: + mpy_modname = tempfile.mktemp(dir="") + mpy_filename = mpy_modname + ".mpy" subprocess.check_output( [MPYCROSS] + args.mpy_cross_flags.split() - + ["-o", "mpytest.mpy", "-X", "emit=" + args.emit, test_file] + + ["-o", mpy_filename, "-X", "emit=" + args.emit, test_file] ) - cmdlist.extend(["-m", "mpytest"]) + cmdlist.extend(["-m", mpy_modname]) else: cmdlist.append(test_file) @@ -175,7 +181,7 @@ def send_get(what): # clean up if we had an intermediate .mpy file if args.via_mpy: - rm_f("mpytest.mpy") + rm_f(mpy_filename) else: # run on pyboard @@ -247,12 +253,32 @@ def run_feature_check(pyb, args, base_path, test_file): return run_micropython(pyb, args, base_path("feature_check", test_file), is_special=True) -def run_tests(pyb, tests, args, result_dir): - test_count = 0 - testcase_count = 0 - passed_count = 0 - failed_tests = [] - skipped_tests = [] +class ThreadSafeCounter: + def __init__(self, start=0): + self._value = start + self._lock = threading.Lock() + + def increment(self): + self.add(1) + + def add(self, to_add): + with self._lock: + self._value += to_add + + def append(self, arg): + self.add([arg]) + + @property + def value(self): + return self._value + + +def run_tests(pyb, tests, args, result_dir, num_threads=1): + test_count = ThreadSafeCounter() + testcase_count = ThreadSafeCounter() + passed_count = ThreadSafeCounter() + failed_tests = ThreadSafeCounter([]) + skipped_tests = ThreadSafeCounter([]) skip_tests = set() skip_native = False @@ -490,7 +516,7 @@ def run_tests(pyb, tests, args, result_dir): ) # native doesn't have proper traceback info skip_tests.add("micropython/schedule.py") # native code doesn't check pending events - for test_file in tests: + def run_one_test(test_file): test_file = test_file.replace("\\", "/") if args.filters: @@ -500,7 +526,7 @@ def run_tests(pyb, tests, args, result_dir): if pat.search(test_file): verdict = action if verdict == "exclude": - continue + return test_basename = test_file.replace("..", "_").replace("./", "").replace("/", "_") test_name = os.path.splitext(os.path.basename(test_file))[0] @@ -533,12 +559,12 @@ def run_tests(pyb, tests, args, result_dir): if args.list_tests: if not skip_it: print(test_file) - continue + return if skip_it: print("skip ", test_file) skipped_tests.append(test_name) - continue + return # get expected output test_file_expected = test_file + ".exp" @@ -560,7 +586,7 @@ def run_tests(pyb, tests, args, result_dir): output_expected = output_expected.replace(b"\r\n", b"\n") if args.write_exp: - continue + return # run MicroPython output_mupy = run_micropython(pyb, args, test_file) @@ -568,16 +594,16 @@ def run_tests(pyb, tests, args, result_dir): if output_mupy == b"SKIP\n": print("skip ", test_file) skipped_tests.append(test_name) - continue + return - testcase_count += len(output_expected.splitlines()) + testcase_count.add(len(output_expected.splitlines())) filename_expected = os.path.join(result_dir, test_basename + ".exp") filename_mupy = os.path.join(result_dir, test_basename + ".out") if output_expected == output_mupy: print("pass ", test_file) - passed_count += 1 + passed_count.increment() rm_f(filename_expected) rm_f(filename_mupy) else: @@ -588,16 +614,32 @@ def run_tests(pyb, tests, args, result_dir): print("FAIL ", test_file) failed_tests.append(test_name) - test_count += 1 + test_count.increment() + + if pyb or args.list_tests: + num_threads = 1 + + if num_threads > 1: + pool = ThreadPool(num_threads) + pool.map(run_one_test, tests) + else: + for test in tests: + run_one_test(test) if args.list_tests: return True - print("{} tests performed ({} individual testcases)".format(test_count, testcase_count)) - print("{} tests passed".format(passed_count)) + print( + "{} tests performed ({} individual testcases)".format( + test_count.value, testcase_count.value + ) + ) + print("{} tests passed".format(passed_count.value)) + skipped_tests = sorted(skipped_tests.value) if len(skipped_tests) > 0: print("{} tests skipped: {}".format(len(skipped_tests), " ".join(skipped_tests))) + failed_tests = sorted(failed_tests.value) if len(failed_tests) > 0: print("{} tests failed: {}".format(len(failed_tests), " ".join(failed_tests))) return False @@ -698,6 +740,14 @@ def main(): cmd_parser.add_argument( "--keep-path", action="store_true", help="do not clear MICROPYPATH when running tests" ) + cmd_parser.add_argument( + "-j", + "--jobs", + default=multiprocessing.cpu_count(), + metavar="N", + type=int, + help="Number of tests to run simultaneously", + ) cmd_parser.add_argument("files", nargs="*", help="input test files") cmd_parser.add_argument( "--print-failures", @@ -800,7 +850,7 @@ def main(): try: os.makedirs(args.result_dir, exist_ok=True) - res = run_tests(pyb, tests, args, args.result_dir) + res = run_tests(pyb, tests, args, args.result_dir, args.jobs) finally: if pyb: pyb.close() From 30cbcf881daeecbc100554879e8a92d71185d9f6 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:24:44 +1000 Subject: [PATCH 0858/3533] docs/esp32: Add APA106 to quickref. --- docs/esp32/quickref.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index a569ff0a4e282..68938feedce0d 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -483,10 +483,10 @@ Be sure to put a 4.7k pull-up resistor on the data line. Note that the ``convert_temp()`` method must be called each time you want to sample the temperature. -NeoPixel driver ---------------- +NeoPixel and APA106 driver +-------------------------- -Use the ``neopixel`` module:: +Use the ``neopixel`` and ``apa106`` modules:: from machine import Pin from neopixel import NeoPixel @@ -497,6 +497,13 @@ Use the ``neopixel`` module:: np.write() # write data to all pixels r, g, b = np[0] # get first pixel colour + +The APA106 driver extends NeoPixel, but internally uses a different colour order:: + + from apa106 import APA106 + ap = APA106(pin, 8) + r, g, b = ap[0] + For low-level driving of a NeoPixel:: import esp @@ -508,6 +515,7 @@ For low-level driving of a NeoPixel:: 400kHz) devices by passing ``timing=0`` when constructing the ``NeoPixel`` object. +APA102 (DotStar) uses a different driver as it has an additional clock pin. Capacitive touch ---------------- From 85c51a548fc83d0776029b01b1be3748f82e296a Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 15:57:20 +1000 Subject: [PATCH 0859/3533] docs/esp32: Mention Signal in GPIO section of quickref. --- docs/esp32/quickref.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 68938feedce0d..1709891265d8e 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -171,6 +171,10 @@ Notes: * The pull value of some pins can be set to ``Pin.PULL_HOLD`` to reduce power consumption during deepsleep. +There's a higher-level abstraction :ref:`machine.Signal ` +which can be used to invert a pin. Useful for illuminating active-low LEDs +using ``on()`` or ``value(1)``. + UART (serial bus) ----------------- From d43ed087aee3444a96da059dfdb532c810f7656c Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 15:54:28 +1000 Subject: [PATCH 0860/3533] docs/esp8266: Mention Signal in GPIO section of quickref. --- docs/esp8266/quickref.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index af2ef1ca1a9df..c4fa779c217a5 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -138,6 +138,10 @@ Also note that Pin(16) is a special pin (used for wakeup from deepsleep mode) and may be not available for use with higher-level classes like ``Neopixel``. +There's a higher-level abstraction :ref:`machine.Signal ` +which can be used to invert a pin. Useful for illuminating active-low LEDs +using ``on()`` or ``value(1)``. + UART (serial bus) ----------------- From 97fee47716627b06bfb11dbcc43f6a000d6e7257 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 30 Apr 2021 14:00:13 +1000 Subject: [PATCH 0861/3533] docs/esp8266: Add SSD1306 to quickref and tutorial. --- docs/esp8266/quickref.rst | 14 +++++ docs/esp8266/tutorial/index.rst | 1 + docs/esp8266/tutorial/ssd1306.rst | 93 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 docs/esp8266/tutorial/ssd1306.rst diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index c4fa779c217a5..bc648a348a888 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -424,6 +424,20 @@ The DHT driver is implemented in software and works on all pins:: d.temperature() # eg. 23.6 (°C) d.humidity() # eg. 41.3 (% RH) +SSD1306 driver +-------------- + +Driver for SSD1306 monochrome OLED displays. See tutorial :ref:`ssd1306`. :: + + from machine import Pin, I2C + import ssd1306 + + i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) + display = ssd1306.SSD1306_I2C(128, 64, i2c) + + display.text('Hello World', 0, 0, 1) + display.show() + WebREPL (web browser interactive prompt) ---------------------------------------- diff --git a/docs/esp8266/tutorial/index.rst b/docs/esp8266/tutorial/index.rst index 4ba211a4b2d8b..79ebd92279c55 100644 --- a/docs/esp8266/tutorial/index.rst +++ b/docs/esp8266/tutorial/index.rst @@ -31,4 +31,5 @@ to ``__. neopixel.rst apa102.rst dht.rst + ssd1306.rst nextsteps.rst diff --git a/docs/esp8266/tutorial/ssd1306.rst b/docs/esp8266/tutorial/ssd1306.rst new file mode 100644 index 0000000000000..4dca82afc48f6 --- /dev/null +++ b/docs/esp8266/tutorial/ssd1306.rst @@ -0,0 +1,93 @@ +.. _ssd1306: + +Using a SSD1306 OLED display +============================ + +The SSD1306 OLED display uses either a SPI or I2C interface and comes in a variety of +sizes (128x64, 128x32, 72x40, 64x48) and colours (white, yellow, blue, yellow + blue). + +Hardware SPI interface:: + + from machine import Pin, SPI + import ssd1306 + + hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused) + + dc = Pin(4) # data/command + rst = Pin(5) # reset + cs = Pin(15) # chip select, some modules do not have a pin for this + + display = ssd1306.SSD1306_SPI(128, 64, hspi, dc, rst, cs) + +Software SPI interface:: + + from machine import Pin, SoftSPI + import ssd1306 + + spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) + + dc = Pin(4) # data/command + rst = Pin(5) # reset + cs = Pin(15) # chip select, some modules do not have a pin for this + + display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs) + +I2C interface:: + + from machine import Pin, I2C + import ssd1306 + + # using default address 0x3C + i2c = I2C(sda=Pin(4), scl=Pin(5)) + display = ssd1306.SSD1306_I2C(128, 64, i2c) + +Print Hello World on the first line:: + + display.text('Hello, World!', 0, 0, 1) + display.show() + +Basic functions:: + + display.poweroff() # power off the display, pixels persist in memory + display.poweron() # power on the display, pixels redrawn + display.contrast(0) # dim + display.contrast(255) # bright + display.invert(1) # display inverted + display.invert(0) # display normal + display.rotate(True) # rotate 180 degrees + display.rotate(False) # rotate 0 degrees + display.show() # write the contents of the FrameBuffer to display memory + +Subclassing FrameBuffer provides support for graphics primitives:: + + display.fill(0) # fill entire screen with colour=0 + display.pixel(0, 10) # get pixel at x=0, y=10 + display.pixel(0, 10, 1) # set pixel at x=0, y=10 to colour=1 + display.hline(0, 8, 4, 1) # draw horizontal line x=0, y=8, width=4, colour=1 + display.vline(0, 8, 4, 1) # draw vertical line x=0, y=8, height=4, colour=1 + display.line(0, 0, 127, 63, 1) # draw a line from 0,0 to 127,63 + display.rect(10, 10, 107, 43, 1) # draw a rectangle outline 10,10 to 107,43, colour=1 + display.fill_rect(10, 10, 107, 43, 1) # draw a solid rectangle 10,10 to 107,43, colour=1 + display.text('Hello World', 0, 0, 1) # draw some text at x=0, y=0, colour=1 + display.scroll(20, 0) # scroll 20 pixels to the right + + # draw another FrameBuffer on top of the current one at the given coordinates + import framebuf + fbuf = framebuf.FrameBuffer(bytearray(8 * 8 * 1), 8, 8, framebuf.MONO_VLSB) + fbuf.line(0, 0, 7, 7, 1) + display.blit(fbuf, 10, 10, 0) # draw on top at x=10, y=10, key=0 + display.show() + +Draw the MicroPython logo and print some text:: + + display.fill(0) + display.fill_rect(0, 0, 32, 32, 1) + display.fill_rect(2, 2, 28, 28, 0) + display.vline(9, 8, 22, 1) + display.vline(16, 2, 22, 1) + display.vline(23, 8, 22, 1) + display.fill_rect(26, 24, 2, 4, 1) + display.text('MicroPython', 40, 0, 1) + display.text('SSD1306', 40, 12, 1) + display.text('OLED 128x64', 40, 24, 1) + display.show() From 538b9a9be5e914eae302d31d8ddedec0f049482a Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 14 May 2021 13:23:44 +1000 Subject: [PATCH 0862/3533] esp32/machine_i2c: Allow boards to configure I2C pins using new macros. Following how SPI is configured (and how stm32 does it). --- ports/esp32/machine_i2c.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index fd5180b8d0709..d9003f45b5bed 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -32,14 +32,19 @@ #include "driver/i2c.h" -#define I2C_0_DEFAULT_SCL (GPIO_NUM_18) -#define I2C_0_DEFAULT_SDA (GPIO_NUM_19) +#ifndef MICROPY_HW_I2C0_SCL +#define MICROPY_HW_I2C0_SCL (GPIO_NUM_18) +#define MICROPY_HW_I2C0_SDA (GPIO_NUM_19) +#endif + +#ifndef MICROPY_HW_I2C1_SCL #if CONFIG_IDF_TARGET_ESP32 -#define I2C_1_DEFAULT_SCL (GPIO_NUM_25) -#define I2C_1_DEFAULT_SDA (GPIO_NUM_26) +#define MICROPY_HW_I2C1_SCL (GPIO_NUM_25) +#define MICROPY_HW_I2C1_SDA (GPIO_NUM_26) #else -#define I2C_1_DEFAULT_SCL (GPIO_NUM_9) -#define I2C_1_DEFAULT_SDA (GPIO_NUM_8) +#define MICROPY_HW_I2C1_SCL (GPIO_NUM_9) +#define MICROPY_HW_I2C1_SDA (GPIO_NUM_8) +#endif #endif #define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms @@ -149,11 +154,11 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->base.type = &machine_hw_i2c_type; self->port = i2c_id; if (self->port == I2C_NUM_0) { - self->scl = I2C_0_DEFAULT_SCL; - self->sda = I2C_0_DEFAULT_SDA; + self->scl = MICROPY_HW_I2C0_SCL; + self->sda = MICROPY_HW_I2C0_SDA; } else { - self->scl = I2C_1_DEFAULT_SCL; - self->sda = I2C_1_DEFAULT_SDA; + self->scl = MICROPY_HW_I2C1_SCL; + self->sda = MICROPY_HW_I2C1_SDA; } first_init = true; } From 9e65662a11c37599299ba4ab9c3ddf0510306498 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 14 May 2021 14:00:01 +1000 Subject: [PATCH 0863/3533] esp32/boards: Set default I2C and SPI pins on UM_xxx boards. And fix incorrect I2C and SPI pins in the feathers2 module. --- ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py | 8 ++++---- ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h | 9 ++++++++- ports/esp32/boards/UM_TINYPICO/mpconfigboard.h | 3 +++ ports/esp32/boards/UM_TINYS2/mpconfigboard.h | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py index 32126fc9c9ae7..93c50a18102bb 100644 --- a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py +++ b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py @@ -22,12 +22,12 @@ # SPI SPI_MOSI = const(35) -SPI_MISO = const(36) -SPI_CLK = const(37) +SPI_MISO = const(37) +SPI_CLK = const(36) # I2C -I2C_SDA = const(38) -I2C_SCL = const(33) +I2C_SDA = const(8) +I2C_SCL = const(9) # DAC DAC1 = const(17) diff --git a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h index c045adccd9682..8d0c9f78c83ae 100644 --- a/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h +++ b/ports/esp32/boards/UM_FEATHERS2/mpconfigboard.h @@ -2,4 +2,11 @@ #define MICROPY_HW_MCU_NAME "ESP32-S2" #define MICROPY_PY_BLUETOOTH (0) -#define MICROPY_HW_ENABLE_SDCARD (0) \ No newline at end of file +#define MICROPY_HW_ENABLE_SDCARD (0) + +#define MICROPY_HW_I2C0_SCL (9) +#define MICROPY_HW_I2C0_SDA (8) + +#define MICROPY_HW_SPI1_MOSI (35) // SDO +#define MICROPY_HW_SPI1_MISO (37) // SDI +#define MICROPY_HW_SPI1_SCK (36) diff --git a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h index 05ee5eaf72a5f..6bf70cc13b580 100644 --- a/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h +++ b/ports/esp32/boards/UM_TINYPICO/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "TinyPICO" #define MICROPY_HW_MCU_NAME "ESP32-PICO-D4" +#define MICROPY_HW_I2C0_SCL (22) +#define MICROPY_HW_I2C0_SDA (21) + #define MICROPY_HW_SPI1_SCK (18) #define MICROPY_HW_SPI1_MOSI (23) #define MICROPY_HW_SPI1_MISO (19) diff --git a/ports/esp32/boards/UM_TINYS2/mpconfigboard.h b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h index 87d266e58c52a..1052f6d79ca2b 100644 --- a/ports/esp32/boards/UM_TINYS2/mpconfigboard.h +++ b/ports/esp32/boards/UM_TINYS2/mpconfigboard.h @@ -4,6 +4,9 @@ #define MICROPY_PY_BLUETOOTH (0) #define MICROPY_HW_ENABLE_SDCARD (0) +#define MICROPY_HW_I2C0_SCL (9) +#define MICROPY_HW_I2C0_SDA (8) + #define MICROPY_HW_SPI1_MOSI (35) #define MICROPY_HW_SPI1_MISO (36) #define MICROPY_HW_SPI1_SCK (37) From 605b74f390e1ce9acdbca32d0b3215d37b96852e Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Fri, 14 May 2021 15:36:59 +1000 Subject: [PATCH 0864/3533] esp32/boards: Fix spelling mistakes in comments for UM_xxx boards. --- ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py | 4 ++-- ports/esp32/boards/UM_TINYPICO/modules/tinypico.py | 2 +- ports/esp32/boards/UM_TINYS2/modules/tinys2.py | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py index 93c50a18102bb..95e1f52681606 100644 --- a/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py +++ b/ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py @@ -50,7 +50,7 @@ def toggle_led(state): l.value(not l.value()) -# Create ADC and set attenuation and return teh ambient light value from the onboard sensor +# Create ADC and set attenuation and return the ambient light value from the onboard sensor def get_amb_light(): adc = ADC(Pin(AMB_LIGHT)) adc.atten(ADC.ATTN_11DB) @@ -62,7 +62,7 @@ def get_amb_light(): # prevent parasitic power from lighting the LED even with the LDO off, causing current use. # The DotStar is a beautiful LED, but parasitic power makes it a terrible choice for battery use :( def set_ldo2_power(state): - """Set the power for the on-board Dostar to allow no current draw when not needed.""" + """Set the power for the on-board Dotstar to allow no current draw when not needed.""" # Set the power pin to the inverse of state ldo2 = Pin(LDO2, Pin.OUT) ldo2.value(state) diff --git a/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py b/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py index 846e07960a3f2..04b274bb53b15 100644 --- a/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py +++ b/ports/esp32/boards/UM_TINYPICO/modules/tinypico.py @@ -80,7 +80,7 @@ def get_battery_charging(): # need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use # to minimise unneeded battery drain def set_dotstar_power(state): - """Set the power for the on-board Dostar to allow no current draw when not needed.""" + """Set the power for the on-board Dotstar to allow no current draw when not needed.""" # Set the power pin to the inverse of state if state: Pin(DOTSTAR_PWR, Pin.OUT, None) # Break the PULL_HOLD on the pin diff --git a/ports/esp32/boards/UM_TINYS2/modules/tinys2.py b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py index ca59cb123581d..0a3eaf14d4652 100644 --- a/ports/esp32/boards/UM_TINYS2/modules/tinys2.py +++ b/ports/esp32/boards/UM_TINYS2/modules/tinys2.py @@ -38,7 +38,7 @@ # Helper functions def set_pixel_power(state): - """Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power fro deep sleep.""" + """Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power for deep sleep.""" Pin(RGB_PWR, Pin.OUT).value(state) @@ -59,7 +59,7 @@ def get_vbus_present(): return Pin(VBUS_SENSE, Pin.IN).value() == 1 -# Dotstar rainbow colour wheel +# NeoPixel rainbow colour wheel def rgb_color_wheel(wheel_pos): """Color wheel to allow for cycling through the rainbow of RGB colors.""" wheel_pos = wheel_pos % 255 @@ -74,9 +74,9 @@ def rgb_color_wheel(wheel_pos): return wheel_pos * 3, 255 - wheel_pos * 3, 0 -# Go into deep sleep but shut down the APA first to save power -# Use this if you want lowest deep sleep current +# Go into deep sleep but shut down the RGB LED first to save power +# Use this if you want lowest deep sleep current def go_deepsleep(t): - """Deep sleep helper that also powers down the on-board Dotstar.""" + """Deep sleep helper that also powers down the on-board NeoPixel.""" set_pixel_power(False) machine.deepsleep(t) From 47e6c52f0c2bf058c5d099dd2993192e0978e172 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 May 2021 22:05:03 +1000 Subject: [PATCH 0865/3533] tests/cpydiff: Add test and workaround for function.__module__ attr. MicroPython does not store any reference from a function object to the module it was defined in, but there is a way to use function.__globals__ to indirectly get the module. See issue #7259. Signed-off-by: Damien George --- tests/cpydiff/core_function_moduleattr.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/cpydiff/core_function_moduleattr.py diff --git a/tests/cpydiff/core_function_moduleattr.py b/tests/cpydiff/core_function_moduleattr.py new file mode 100644 index 0000000000000..71747c1f409ee --- /dev/null +++ b/tests/cpydiff/core_function_moduleattr.py @@ -0,0 +1,13 @@ +""" +categories: Core,Functions +description: Function objects do not have the ``__module__`` attribute +cause: MicroPython is optimized for reduced code size and RAM usage. +workaround: Use ``sys.modules[function.__globals__['__name__']]`` for non-builtin modules. +""" + + +def f(): + pass + + +print(f.__module__) From 1446107b4d24915143f8212c2f649a0e628735cc Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 May 2021 00:22:53 +1000 Subject: [PATCH 0866/3533] py/objarray: Use mp_obj_memoryview_init helper in mp_obj_new_memoryview. Signed-off-by: Damien George --- py/objarray.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/py/objarray.c b/py/objarray.c index 5bd9454716a4f..be85674f8712b 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -201,11 +201,7 @@ STATIC mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args, mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) { mp_obj_array_t *self = m_new_obj(mp_obj_array_t); - self->base.type = &mp_type_memoryview; - self->typecode = typecode; - self->memview_offset = 0; - self->len = nitems; - self->items = items; + mp_obj_memoryview_init(self, typecode, 0, nitems, items); return MP_OBJ_FROM_PTR(self); } From 6d2680fa36696fd0b2fd7a595f4a26153cd84e07 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 May 2021 00:23:13 +1000 Subject: [PATCH 0867/3533] py/objarray: Fix constructing a memoryview from a memoryview. Fixes issue #7261. Signed-off-by: Damien George --- py/objarray.c | 8 ++++++++ tests/basics/memoryview_gc.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/py/objarray.c b/py/objarray.c index be85674f8712b..16a4d4aac7e25 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -220,6 +220,14 @@ STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args, bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL), bufinfo.buf)); + // If the input object is a memoryview then need to point the items of the + // new memoryview to the start of the buffer so the GC can trace it. + if (mp_obj_get_type(args[0]) == &mp_type_memoryview) { + mp_obj_array_t *other = MP_OBJ_TO_PTR(args[0]); + self->memview_offset = other->memview_offset; + self->items = other->items; + } + // test if the object can be written to if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) { self->typecode |= MP_OBJ_ARRAY_TYPECODE_FLAG_RW; // indicate writable buffer diff --git a/tests/basics/memoryview_gc.py b/tests/basics/memoryview_gc.py index d366cbbb15405..5cd6124ada612 100644 --- a/tests/basics/memoryview_gc.py +++ b/tests/basics/memoryview_gc.py @@ -21,3 +21,13 @@ # check that the memoryview is still what we want print(list(m)) + +# check that creating a memoryview of a memoryview retains the underlying data +m = None +gc.collect() # cleanup from previous test +m = memoryview(memoryview(bytearray(i for i in range(50)))[5:-5]) +print(sum(m), list(m[:10])) +gc.collect() +for i in range(10): + list(range(10)) # allocate memory to overwrite any reclaimed heap +print(sum(m), list(m[:10])) From 126b1c727118352923703719a2a3d45b9fad3c97 Mon Sep 17 00:00:00 2001 From: Bob Abeles Date: Fri, 14 May 2021 07:57:19 -0700 Subject: [PATCH 0868/3533] py/nlraarch64: Add underscore prefix to function symbols for Darwin ABI. The proper way to do this is to test for __APPLE__ and __MACH__, where __APPLE__ tests for an Apple OS and __MACH__ tests that it is based on CMU Mach. Using both tests ensures that just Darwin (Apple's open source base for MacOS, iOS, etc.) is recognized. __APPLE__ by itself will test for any Apple OS, which can include older OS 7-9 and any future Apple OS. __MACH__ tests for any OS based on CMU Mach, including Darwin and GNU Hurd. Fixes #7232. --- py/nlraarch64.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/py/nlraarch64.c b/py/nlraarch64.c index 2df0dc9c85021..1295351cbe828 100644 --- a/py/nlraarch64.c +++ b/py/nlraarch64.c @@ -34,8 +34,13 @@ // Implemented purely as inline assembly; inside a function, we have to deal with undoing the prologue, restoring // SP and LR. This way, we don't. __asm( + #if defined(__APPLE__) && defined(__MACH__) + "_nlr_push: \n" + ".global _nlr_push \n" + #else "nlr_push: \n" ".global nlr_push \n" + #endif "mov x9, sp \n" "stp lr, x9, [x0, #16]\n" // 16 == offsetof(nlr_buf_t, regs) "stp x19, x20, [x0, #32]\n" @@ -44,7 +49,11 @@ __asm( "stp x25, x26, [x0, #80]\n" "stp x27, x28, [x0, #96]\n" "str x29, [x0, #112]\n" + #if defined(__APPLE__) && defined(__MACH__) + "b _nlr_push_tail \n" // do the rest in C + #else "b nlr_push_tail \n" // do the rest in C + #endif ); NORETURN void nlr_jump(void *val) { From 7ceccad4e2f1e9d073f5781c32e5b377e8391a25 Mon Sep 17 00:00:00 2001 From: Bob Abeles Date: Fri, 14 May 2021 08:05:40 -0700 Subject: [PATCH 0869/3533] py/nlrx64: Correct the detection of Darwin ABI. __APPLE__ tests for an Apple OS and __MACH__ tests that it is based on CMU Mach. Using both tests ensures that just Darwin is recognized. --- py/nlrx64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/nlrx64.c b/py/nlrx64.c index f5d02afbdb979..6f006e755ec7c 100644 --- a/py/nlrx64.c +++ b/py/nlrx64.c @@ -58,7 +58,7 @@ unsigned int nlr_push(nlr_buf_t *nlr) { #else __asm volatile ( - #if defined(__APPLE__) || defined(__MACH__) + #if defined(__APPLE__) && defined(__MACH__) "pop %rbp \n" // undo function's prelude #endif "movq (%rsp), %rax \n" // load return %rip @@ -70,7 +70,7 @@ unsigned int nlr_push(nlr_buf_t *nlr) { "movq %r13, 56(%rdi) \n" // store %r13 into nlr_buf "movq %r14, 64(%rdi) \n" // store %r14 into nlr_buf "movq %r15, 72(%rdi) \n" // store %r15 into nlr_buf - #if defined(__APPLE__) || defined(__MACH__) + #if defined(__APPLE__) && defined(__MACH__) "jmp _nlr_push_tail \n" // do the rest in C #else "jmp nlr_push_tail \n" // do the rest in C From 94fb5e7f5a409e9596657508c6a9ed1c609de267 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Fri, 14 May 2021 08:35:43 -0300 Subject: [PATCH 0870/3533] nrf: Add machine.memXX, and allow boards to customise some features. --- ports/nrf/modules/machine/modmachine.c | 4 ++++ ports/nrf/mpconfigport.h | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index 3330349cd5963..7e45b83df8173 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -201,6 +201,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) }, + { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, + { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, + #if MICROPY_PY_MACHINE_UART { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_hard_uart_type) }, #endif diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index c246c53dd5aff..714b1cc24fce0 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -44,8 +44,6 @@ #endif #define MICROPY_ALLOC_PATH_MAX (512) #define MICROPY_PERSISTENT_CODE_LOAD (0) -#define MICROPY_EMIT_THUMB (0) -#define MICROPY_EMIT_INLINE_THUMB (0) #define MICROPY_COMP_MODULE_CONST (0) #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0) #define MICROPY_READER_VFS (MICROPY_VFS) @@ -123,7 +121,6 @@ #define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (0) #define MICROPY_PY_IO_FILEIO (0) -#define MICROPY_PY_UERRNO (0) #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #define MICROPY_PY_UCTYPES (0) @@ -131,7 +128,6 @@ #define MICROPY_PY_UJSON (0) #define MICROPY_PY_URE (0) #define MICROPY_PY_UHEAPQ (0) -#define MICROPY_PY_UHASHLIB (0) #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PULSE (0) From ea2d83e96175ef1ed260cca917024502e01a5546 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Thu, 13 May 2021 18:18:04 -0300 Subject: [PATCH 0871/3533] nrf/boards: Add support for evk_nina_b3 board. --- ports/nrf/boards/evk_nina_b3/mpconfigboard.h | 85 +++++++++++++++++++ ports/nrf/boards/evk_nina_b3/mpconfigboard.mk | 7 ++ ports/nrf/boards/evk_nina_b3/pins.csv | 48 +++++++++++ 3 files changed, 140 insertions(+) create mode 100644 ports/nrf/boards/evk_nina_b3/mpconfigboard.h create mode 100644 ports/nrf/boards/evk_nina_b3/mpconfigboard.mk create mode 100644 ports/nrf/boards/evk_nina_b3/pins.csv diff --git a/ports/nrf/boards/evk_nina_b3/mpconfigboard.h b/ports/nrf/boards/evk_nina_b3/mpconfigboard.h new file mode 100644 index 0000000000000..eaca796be4d67 --- /dev/null +++ b/ports/nrf/boards/evk_nina_b3/mpconfigboard.h @@ -0,0 +1,85 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Glenn Ruben Bakke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Pin numbering scheme for nrf52840-based boards +// +// Software Pins 0-31 correspond to physical pins +// 0.x and software Pins 32-47 correspond to physical pins 1.x. +// +// Example: Pin(47) would be 1.15 on the PCA10059 + +// Board data +#define MICROPY_HW_BOARD_NAME "EVK_NINA_B3" +#define MICROPY_HW_MCU_NAME "NRF52840" +#define MICROPY_PY_SYS_PLATFORM "nrf52" + +// Enable @viper and @native +#define MICROPY_EMIT_THUMB (1) +#define MICROPY_EMIT_INLINE_THUMB (1) + +// Enable optional modules +#define MICROPY_PY_UERRNO (1) +#define MICROPY_PY_UHASHLIB (1) + +// Peripherals Config +#define MICROPY_PY_MACHINE_UART (1) +#define MICROPY_PY_MACHINE_HW_PWM (1) +#define MICROPY_PY_MACHINE_HW_SPI (1) +#define MICROPY_PY_MACHINE_TIMER (1) +#define MICROPY_PY_MACHINE_RTCOUNTER (1) +#define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_TEMP (1) +#define MICROPY_HW_ENABLE_RNG (1) + +// Configure LEDS +#define MICROPY_HW_HAS_LED (1) +#define MICROPY_HW_LED_COUNT (3) +#define MICROPY_HW_LED_PULLUP (1) +#define MICROPY_HW_LED1 (13) // LED1 RED +#define MICROPY_HW_LED2 (25) // LED2 GREEN +#define MICROPY_HW_LED3 (32) // LED3 BLUE + +// UART config +#define MICROPY_HW_UART1_RX (29) +#define MICROPY_HW_UART1_TX (45) +#define MICROPY_HW_UART1_CTS (44) +#define MICROPY_HW_UART1_RTS (31) +#define MICROPY_HW_UART1_HWFC (1) + +// SPI config +#define MICROPY_HW_SPI0_NAME "SPI0" +#define MICROPY_HW_SPI0_SCK (12) +#define MICROPY_HW_SPI0_MOSI (14) +#define MICROPY_HW_SPI0_MISO (15) + +// PWM Names +#define MICROPY_HW_PWM0_NAME "PWM0" +#define MICROPY_HW_PWM1_NAME "PWM1" +#define MICROPY_HW_PWM2_NAME "PWM2" + +// How Many LED indexes appear in the help() message +#define HELP_TEXT_BOARD_LED "1,2,3" diff --git a/ports/nrf/boards/evk_nina_b3/mpconfigboard.mk b/ports/nrf/boards/evk_nina_b3/mpconfigboard.mk new file mode 100644 index 0000000000000..ca555d3932aa4 --- /dev/null +++ b/ports/nrf/boards/evk_nina_b3/mpconfigboard.mk @@ -0,0 +1,7 @@ +MCU_SERIES = m4 +MCU_VARIANT = nrf52 +MCU_SUB_VARIANT = nrf52840 +SOFTDEV_VERSION = 6.1.1 +LD_FILES += boards/nrf52840_1M_256k.ld + +NRF_DEFINES += -DNRF52840_XXAA diff --git a/ports/nrf/boards/evk_nina_b3/pins.csv b/ports/nrf/boards/evk_nina_b3/pins.csv new file mode 100644 index 0000000000000..e6190bac0a3f7 --- /dev/null +++ b/ports/nrf/boards/evk_nina_b3/pins.csv @@ -0,0 +1,48 @@ +P0,P0 +P1,P1 +P2,P2,ADC0_IN0 +P3,P3,ADC0_IN1 +P4,P4,ADC0_IN2 +P5,P5,ADC0_IN3 +P6,P6 +P7,P7 +P8,P8 +P9,P9 +P10,P10 +P11,P11 +P12,P12 +P13,P13 +P14,P14 +P15,P15 +P16,P16 +P17,P17 +P18,P18 +P19,P19 +P20,P20 +P21,P21 +P22,P22 +P23,P23 +P24,P24 +P25,P25 +P26,P26 +P27,P27 +P28,P28,ADC0_IN4 +P29,P29,ADC0_IN5 +P30,P30,ADC0_IN6 +P31,P31,ADC0_IN7 +P32,P32 +P33,P33 +P34,P34 +P35,P35 +P36,P36 +P37,P37 +P38,P38 +P39,P39 +P40,P40 +P41,P41 +P42,P42 +P43,P43 +P44,P44 +P45,P45 +P46,P46 +P47,P47 From 07528d1f855cf7a970913477b40c664700a0aa4d Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 16 May 2021 20:11:44 -0700 Subject: [PATCH 0872/3533] docs/library: Clarify what type of algorithm is implemented in heapq. --- docs/library/uheapq.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/library/uheapq.rst b/docs/library/uheapq.rst index f822f1e7f32c4..9ae1f61a173dd 100644 --- a/docs/library/uheapq.rst +++ b/docs/library/uheapq.rst @@ -6,9 +6,11 @@ |see_cpython_module| :mod:`python:heapq`. -This module implements the heap queue algorithm. +This module implements the +`min heap queue algorithm `_. -A heap queue is simply a list that has its elements stored in a certain way. +A heap queue is essentially a list that has its elements stored in such a way +that the first item of the list is always the smallest. Functions --------- @@ -19,8 +21,10 @@ Functions .. function:: heappop(heap) - Pop the first item from the ``heap``, and return it. Raises IndexError if - heap is empty. + Pop the first item from the ``heap``, and return it. Raise ``IndexError`` if + ``heap`` is empty. + + The returned item will be the smallest item in the ``heap``. .. function:: heapify(x) From 452fa3f8d4cd8880da06b8a41e191e2acac59c3f Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 16 May 2021 20:21:26 -0700 Subject: [PATCH 0873/3533] docs/library: Add a blank line to fix formatting for ussl docs. --- docs/library/ussl.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/library/ussl.rst b/docs/library/ussl.rst index 14e3f3ad142ce..77d278d41df52 100644 --- a/docs/library/ussl.rst +++ b/docs/library/ussl.rst @@ -13,7 +13,8 @@ facilities for network sockets, both client-side and server-side. Functions --------- -.. function:: ussl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None, do_handshake=True) +.. function:: ussl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None, do_handshake=True) + Takes a `stream` *sock* (usually usocket.socket instance of ``SOCK_STREAM`` type), and returns an instance of ssl.SSLSocket, which wraps the underlying stream in an SSL context. Returned object has the usual `stream` interface methods like From 7408ca1d7857df5ea348da35c9ee12f70a024478 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 16 May 2021 13:27:54 +1000 Subject: [PATCH 0874/3533] mimxrt: Improve ticks and sleep functions using GPT. SysTick cannot wake the CPU from WFI/WFE so a hardware timer is needed to keep track of ticks/delay (similar to the nrf port). Fixes issue #7234. Signed-off-by: Damien George --- ports/mimxrt/Makefile | 2 + ports/mimxrt/board_init.c | 7 -- ports/mimxrt/main.c | 2 + ports/mimxrt/mphalport.c | 17 +---- ports/mimxrt/mphalport.h | 16 +++-- ports/mimxrt/ticks.c | 137 ++++++++++++++++++++++++++++++++++++++ ports/mimxrt/ticks.h | 35 ++++++++++ 7 files changed, 189 insertions(+), 27 deletions(-) create mode 100644 ports/mimxrt/ticks.c create mode 100644 ports/mimxrt/ticks.h diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 85e77b978e9f0..3a001182a036a 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -82,6 +82,7 @@ SRC_TINYUSB_IMX_C += \ $(MCU_DIR)/project_template/clock_config.c \ $(MCU_DIR)/drivers/fsl_clock.c \ $(MCU_DIR)/drivers/fsl_gpio.c \ + $(MCU_DIR)/drivers/fsl_gpt.c \ $(MCU_DIR)/drivers/fsl_common.c \ $(MCU_DIR)/drivers/fsl_lpuart.c \ $(MCU_DIR)/drivers/fsl_flexram.c \ @@ -90,6 +91,7 @@ SRC_C = \ main.c \ led.c \ pin.c \ + ticks.c \ tusb_port.c \ board_init.c \ $(BOARD_DIR)/flash_config.c \ diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c index cd7dc9a7dd08e..9095c880482d3 100644 --- a/ports/mimxrt/board_init.c +++ b/ports/mimxrt/board_init.c @@ -53,9 +53,6 @@ void board_init(void) { // Enable IOCON clock CLOCK_EnableClock(kCLOCK_Iomuxc); - // 1ms tick timer - SysTick_Config(SystemCoreClock / 1000); - // ------------- USB0 ------------- // // Clock @@ -85,10 +82,6 @@ void board_init(void) { // CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U); } -void SysTick_Handler(void) { - systick_ms++; -} - void USB_OTG1_IRQHandler(void) { tud_int_handler(0); tud_task(); diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c index e06283a8ab37b..7832afffcb2b5 100644 --- a/ports/mimxrt/main.c +++ b/ports/mimxrt/main.c @@ -32,6 +32,7 @@ #include "py/stackctrl.h" #include "lib/utils/gchelper.h" #include "lib/utils/pyexec.h" +#include "ticks.h" #include "tusb.h" #include "led.h" @@ -41,6 +42,7 @@ void board_init(void); int main(void) { board_init(); + ticks_init(); tusb_init(); led_init(); diff --git a/ports/mimxrt/mphalport.c b/ports/mimxrt/mphalport.c index 9fee3e85d0082..02a61a4f2a3bb 100644 --- a/ports/mimxrt/mphalport.c +++ b/ports/mimxrt/mphalport.c @@ -27,6 +27,7 @@ #include "py/runtime.h" #include "py/mphal.h" +#include "ticks.h" #include "tusb.h" #include CPU_HEADER_H @@ -46,22 +47,6 @@ void mp_hal_set_interrupt_char(int c) { #endif -void mp_hal_delay_ms(mp_uint_t ms) { - ms += 1; - uint32_t t0 = systick_ms; - while (systick_ms - t0 < ms) { - MICROPY_EVENT_POLL_HOOK - } -} - -void mp_hal_delay_us(mp_uint_t us) { - uint32_t ms = us / 1000 + 1; - uint32_t t0 = systick_ms; - while (systick_ms - t0 < ms) { - __WFI(); - } -} - int mp_hal_stdin_rx_chr(void) { for (;;) { // TODO diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index 2b9e49432cc0f..993e15156618a 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -28,21 +28,29 @@ #define MICROPY_INCLUDED_MIMXRT_MPHALPORT_H #include +#include "ticks.h" #define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U)) #define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U)) #define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin))) -extern volatile uint32_t systick_ms; - void mp_hal_set_interrupt_char(int c); static inline mp_uint_t mp_hal_ticks_ms(void) { - return systick_ms; + return ticks_ms32(); } static inline mp_uint_t mp_hal_ticks_us(void) { - return systick_ms * 1000; + return ticks_us32(); +} + +static inline void mp_hal_delay_ms(mp_uint_t ms) { + uint64_t us = (uint64_t)ms * 1000; + ticks_delay_us64(us); +} + +static inline void mp_hal_delay_us(mp_uint_t us) { + ticks_delay_us64(us); } static inline mp_uint_t mp_hal_ticks_cpu(void) { diff --git a/ports/mimxrt/ticks.c b/ports/mimxrt/ticks.c new file mode 100644 index 0000000000000..676f81b30eb26 --- /dev/null +++ b/ports/mimxrt/ticks.c @@ -0,0 +1,137 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mphal.h" +#include "ticks.h" +#include "fsl_gpt.h" + +// General purpose timer for keeping microsecond and millisecond tick values. +#define GPTx GPT2 +#define GPTx_IRQn GPT2_IRQn +#define GPTx_IRQHandler GPT2_IRQHandler + +static uint32_t ticks_us64_upper; +static uint32_t ticks_ms_upper; + +void ticks_init(void) { + ticks_us64_upper = 0; + ticks_ms_upper = 0; + + gpt_config_t config; + config.clockSource = kGPT_ClockSource_Osc; + config.divider = 24; // XTAL is 24MHz + config.enableFreeRun = true; + config.enableRunInWait = true; + config.enableRunInStop = true; + config.enableRunInDoze = true; + config.enableRunInDbg = false; + config.enableMode = true; + GPT_Init(GPTx, &config); + + GPT_EnableInterrupts(GPTx, kGPT_RollOverFlagInterruptEnable); + NVIC_SetPriority(GPTx_IRQn, 0); // highest priority + NVIC_EnableIRQ(GPTx_IRQn); + + GPT_StartTimer(GPTx); +} + +void GPTx_IRQHandler(void) { + if (GPT_GetStatusFlags(GPTx, kGPT_OutputCompare1Flag)) { + GPT_ClearStatusFlags(GPTx, kGPT_OutputCompare1Flag); + GPT_DisableInterrupts(GPTx, kGPT_OutputCompare1InterruptEnable); + __SEV(); + } + if (GPT_GetStatusFlags(GPTx, kGPT_RollOverFlag)) { + GPT_ClearStatusFlags(GPTx, kGPT_RollOverFlag); + ++ticks_us64_upper; + if (++ticks_ms_upper >= 1000) { + // Wrap upper counter at a multiple of 1000 so that when mp_hal_ticks_ms() + // wraps due to overflow it wraps smoothly. + ticks_ms_upper = 0; + } + } +} + +static void ticks_wake_after_us32(uint32_t us) { + if (us < 2) { + // Delay too short to guarantee that we won't miss it when setting the OCR below. + __SEV(); + } else { + // Disable IRQs so setting the OCR is done without any interruption. + uint32_t irq_state = DisableGlobalIRQ(); + GPT_EnableInterrupts(GPTx, kGPT_OutputCompare1InterruptEnable); + uint32_t oc = GPT_GetCurrentTimerCount(GPTx) + us; + GPT_SetOutputCompareValue(GPTx, kGPT_OutputCompare_Channel1, oc); + EnableGlobalIRQ(irq_state); + } +} + +static uint64_t ticks_us64_with(uint32_t *upper_ptr) { + uint32_t irq_state = DisableGlobalIRQ(); + uint32_t lower = GPT_GetCurrentTimerCount(GPTx); + uint32_t upper = *upper_ptr; + uint32_t overflow = GPT_GetStatusFlags(GPTx, kGPT_RollOverFlag); + EnableGlobalIRQ(irq_state); + if (overflow && lower < 0x80000000) { + // The timer counter overflowed before reading it but the IRQ handler + // has not yet been called, so perform the IRQ arithmetic now. + ++upper; + } + return (uint64_t)upper << 32 | (uint64_t)lower; +} + +uint32_t ticks_us32(void) { + return GPT_GetCurrentTimerCount(GPTx); +} + +uint64_t ticks_us64(void) { + return ticks_us64_with(&ticks_us64_upper); +} + +uint32_t ticks_ms32(void) { + // This will return a value that only has the lower 32-bits valid. + return ticks_us64_with(&ticks_ms_upper) / 1000; +} + +void ticks_delay_us64(uint64_t us) { + uint64_t t0 = ticks_us64(); + for (;;) { + uint64_t dt = ticks_us64() - t0; + if (dt >= us) { + return; + } + dt = us - dt; + if (dt > 0xffffffff) { + dt = 0xffffffff; + } + ticks_wake_after_us32((uint32_t)dt); + if (dt < 50) { + __WFE(); + } else { + MICROPY_EVENT_POLL_HOOK + } + } +} diff --git a/ports/mimxrt/ticks.h b/ports/mimxrt/ticks.h new file mode 100644 index 0000000000000..11127c29cce8a --- /dev/null +++ b/ports/mimxrt/ticks.h @@ -0,0 +1,35 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_MIMXRT_TICKS_H +#define MICROPY_INCLUDED_MIMXRT_TICKS_H + +void ticks_init(void); +uint32_t ticks_us32(void); +uint64_t ticks_us64(void); +uint32_t ticks_ms32(void); +void ticks_delay_us64(uint64_t us); + +#endif // MICROPY_INCLUDED_MIMXRT_TICKS_H From 9d58d46e0a28b7bd3344cae049e8085bcd75cb75 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 28 Jul 2020 16:27:19 +1000 Subject: [PATCH 0875/3533] docs/library/pyb.Pin.rst: Update the arguments for Pin.init(). Add details for Pin.init() value and alt arguments. --- docs/library/pyb.Pin.rst | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/library/pyb.Pin.rst b/docs/library/pyb.Pin.rst index 3d019cd8e0671..6465653f74cba 100644 --- a/docs/library/pyb.Pin.rst +++ b/docs/library/pyb.Pin.rst @@ -98,11 +98,11 @@ Class methods Methods ------- -.. method:: Pin.init(mode, pull=Pin.PULL_NONE, af=-1) +.. method:: Pin.init(mode, pull=Pin.PULL_NONE, \*, value=None, alt=-1) Initialise the pin: - - ``mode`` can be one of: + - *mode* can be one of: - ``Pin.IN`` - configure the pin for input; - ``Pin.OUT_PP`` - configure the pin for output, with push-pull control; @@ -111,14 +111,17 @@ Methods - ``Pin.AF_OD`` - configure the pin for alternate function, open-drain; - ``Pin.ANALOG`` - configure the pin for analog. - - ``pull`` can be one of: + - *pull* can be one of: - ``Pin.PULL_NONE`` - no pull up or down resistors; - ``Pin.PULL_UP`` - enable the pull-up resistor; - ``Pin.PULL_DOWN`` - enable the pull-down resistor. - - when mode is ``Pin.AF_PP`` or ``Pin.AF_OD``, then af can be the index or name - of one of the alternate functions associated with a pin. + - *value* if not None will set the port output value before enabling the pin. + + - *alt* can be used when mode is ``Pin.AF_PP`` or ``Pin.AF_OD`` to set the + index or name of one of the alternate functions associated with a pin. + This arg was previously called *af* which can still be used if needed. Returns: ``None``. From c24003abec78ee443ddc1bdc375c48513e45a54a Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Mon, 13 Jan 2020 17:25:01 +1100 Subject: [PATCH 0876/3533] stm32/boards: Add VCC_GND_F407VE board. --- ports/stm32/boards/VCC_GND_F407VE/bdev.c | 28 +++ .../stm32/boards/VCC_GND_F407VE/board_init.c | 7 + .../boards/VCC_GND_F407VE/mpconfigboard.h | 163 ++++++++++++++++++ .../boards/VCC_GND_F407VE/mpconfigboard.mk | 6 + ports/stm32/boards/VCC_GND_F407VE/pins.csv | 112 ++++++++++++ .../VCC_GND_F407VE/stm32f4xx_hal_conf.h | 15 ++ 6 files changed, 331 insertions(+) create mode 100644 ports/stm32/boards/VCC_GND_F407VE/bdev.c create mode 100644 ports/stm32/boards/VCC_GND_F407VE/board_init.c create mode 100644 ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.h create mode 100644 ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.mk create mode 100644 ports/stm32/boards/VCC_GND_F407VE/pins.csv create mode 100644 ports/stm32/boards/VCC_GND_F407VE/stm32f4xx_hal_conf.h diff --git a/ports/stm32/boards/VCC_GND_F407VE/bdev.c b/ports/stm32/boards/VCC_GND_F407VE/bdev.c new file mode 100644 index 0000000000000..18b5b85b18f8c --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/bdev.c @@ -0,0 +1,28 @@ +#include "storage.h" + +#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE + +// External SPI flash uses standard SPI interface + +STATIC const mp_soft_spi_obj_t soft_spi_bus = { + .delay_half = MICROPY_HW_SOFTSPI_MIN_DELAY, + .polarity = 0, + .phase = 0, + .sck = MICROPY_HW_SPIFLASH_SCK, + .mosi = MICROPY_HW_SPIFLASH_MOSI, + .miso = MICROPY_HW_SPIFLASH_MISO, +}; + +STATIC mp_spiflash_cache_t spi_bdev_cache; + +const mp_spiflash_config_t spiflash_config = { + .bus_kind = MP_SPIFLASH_BUS_SPI, + .bus.u_spi.cs = MICROPY_HW_SPIFLASH_CS, + .bus.u_spi.data = (void*)&soft_spi_bus, + .bus.u_spi.proto = &mp_soft_spi_proto, + .cache = &spi_bdev_cache, +}; + +spi_bdev_t spi_bdev; + +#endif diff --git a/ports/stm32/boards/VCC_GND_F407VE/board_init.c b/ports/stm32/boards/VCC_GND_F407VE/board_init.c new file mode 100644 index 0000000000000..1d5900de55858 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/board_init.c @@ -0,0 +1,7 @@ +#include "py/mphal.h" + +void VCC_GND_F407VE_board_early_init(void) { + // set SPI flash CS pin high + mp_hal_pin_output(pin_A4); + mp_hal_pin_write(pin_A4, 1); +} diff --git a/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.h b/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.h new file mode 100644 index 0000000000000..fd896c6666884 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.h @@ -0,0 +1,163 @@ +/* This file is part of the MicroPython project, http://micropython.org/ + * MIT License; Copyright (c) 2021 Damien P. George + */ + +// STM32F407VET6 Mini by VCC-GND Studio +// http://vcc-gnd.com/ +// https://item.taobao.com/item.htm?ft=t&id=523361737493 +// https://www.aliexpress.com/wholesale?SearchText=STM32F407VET6+Mini + +// DFU mode can be accessed by switching BOOT0 DIP ON (towards USB) + +#define MICROPY_HW_BOARD_NAME "VCC-GND STM32F407VE" +#define MICROPY_HW_MCU_NAME "STM32F407VE" +#define MICROPY_HW_FLASH_FS_LABEL "VCCGNDF407VE" + +// 1 = use internal flash (512 KByte) +// 0 = use external SPI flash +#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1) + +#define MICROPY_HW_HAS_FLASH (1) +#define MICROPY_HW_ENABLE_RNG (1) +#define MICROPY_HW_ENABLE_RTC (1) +#define MICROPY_HW_ENABLE_DAC (1) +#define MICROPY_HW_ENABLE_USB (1) +#define MICROPY_HW_ENABLE_SDCARD (1) + +// HSE is 25MHz +#define MICROPY_HW_CLK_PLLM (25) // divide external clock by this to get 1MHz +#define MICROPY_HW_CLK_PLLN (336) // PLL clock in MHz +#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2) // divide PLL clock by this to get core clock +#define MICROPY_HW_CLK_PLLQ (7) // divide core clock by this to get 48MHz + +// The board has a 32kHz crystal for the RTC +#define MICROPY_HW_RTC_USE_LSE (1) +#define MICROPY_HW_RTC_USE_US (0) +// #define MICROPY_HW_RTC_USE_CALOUT (1) // turn on/off PC13 512Hz output + +// USART1 +#define MICROPY_HW_UART1_TX (pin_A9) // PA9,PB6 +#define MICROPY_HW_UART1_RX (pin_A10) // PA10,PB7 + +// USART2 +#define MICROPY_HW_UART2_TX (pin_A2) // PA2,PD5 +#define MICROPY_HW_UART2_RX (pin_A3) // PA3,PD6 +#define MICROPY_HW_UART2_RTS (pin_A1) // PA1,PD4 +#define MICROPY_HW_UART2_CTS (pin_A0) // PA0,PD3 + +// USART3 +#define MICROPY_HW_UART3_TX (pin_D8) // PB10,PC10,PD8 +#define MICROPY_HW_UART3_RX (pin_D9) // PB11,PC11,PD9 +#define MICROPY_HW_UART3_RTS (pin_D12) // PB14,PD12 +#define MICROPY_HW_UART3_CTS (pin_D11) // PB13,PD11 + +// UART4 +#define MICROPY_HW_UART4_TX (pin_A0) // PA0,PC10 +#define MICROPY_HW_UART4_RX (pin_A1) // PA1,PC11 + +// UART5 +#define MICROPY_HW_UART5_TX (pin_C12) // PC12 +#define MICROPY_HW_UART5_RX (pin_D2) // PD2 + +// USART6 +#define MICROPY_HW_UART6_TX (pin_C6) // PC6,PG14 +#define MICROPY_HW_UART6_RX (pin_C7) // PC7,PG9 + +// I2C buses +#define MICROPY_HW_I2C1_SCL (pin_B6) // PB8,PB6 +#define MICROPY_HW_I2C1_SDA (pin_B7) // PB9,PB7 +#define MICROPY_HW_I2C2_SCL (pin_B10) // PB10 +#define MICROPY_HW_I2C2_SDA (pin_B11) // PB11 +#define MICROPY_HW_I2C3_SCL (pin_A8) // PA8 +#define MICROPY_HW_I2C3_SDA (pin_C9) // PC9 +// AT24C08 EEPROM on I2C1 0x50-0x53 + +// I2S buses +// I2S2_CK PB13 +// I2S2_MCK PC6 +// I2S2_SD PB15 +// I2S2_WS PB12 +// I2S3_CK PB3 +// I2S3_MCK PC7 +// I2S3_SD PB5 +// I2S3_WS PA15 + +// SPI buses +#define MICROPY_HW_SPI1_NSS (pin_A4) // PA4 +#define MICROPY_HW_SPI1_SCK (pin_A5) // PA5,PB3 +#define MICROPY_HW_SPI1_MISO (pin_A6) // PA6,PB4 +#define MICROPY_HW_SPI1_MOSI (pin_A7) // PA7,PB5 + +#define MICROPY_HW_SPI2_NSS (pin_B12) // PB12 +#define MICROPY_HW_SPI2_SCK (pin_B13) // PB13 +#define MICROPY_HW_SPI2_MISO (pin_B14) // PB14 +#define MICROPY_HW_SPI2_MOSI (pin_B15) // PB15 + +#define MICROPY_HW_SPI3_NSS (pin_A15) // PA15 +#define MICROPY_HW_SPI3_SCK (pin_B3) // PB3 +#define MICROPY_HW_SPI3_MISO (pin_B4) // PB4 +#define MICROPY_HW_SPI3_MOSI (pin_B5) // PB5 + +// CAN buses +#define MICROPY_HW_CAN1_TX (pin_B9) // PB9,PD1,PA12 +#define MICROPY_HW_CAN1_RX (pin_B8) // PB8,PD0,PA11 +#define MICROPY_HW_CAN2_TX (pin_B13) // PB13 +#define MICROPY_HW_CAN2_RX (pin_B12) // PB12 + +// DAC +// DAC_OUT1 PA4 +// DAC_OUT2 PA5 + +// LEDs +#define MICROPY_HW_LED1 (pin_B9) // blue +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) + +// If using external SPI flash +#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE + +// The board does not have onboard SPI flash. You need to add an external one. +#define MICROPY_HW_SPIFLASH_SIZE_BITS (4 * 1024 * 1024) // W25X40 - 4 Mbit (512 KByte) +// #define MICROPY_HW_SPIFLASH_SIZE_BITS (32 * 1024 * 1024) // W25Q32 - 32 Mbit (4 MByte) +// #define MICROPY_HW_SPIFLASH_SIZE_BITS (64 * 1024 * 1024) // W25Q64 - 64 Mbit (8 MByte) +// #define MICROPY_HW_SPIFLASH_SIZE_BITS (128 * 1024 * 1024) // W25Q128 - 128 Mbit (16 MByte) + +#define MICROPY_HW_SPIFLASH_CS (pin_A4) // also in board_init.c +#define MICROPY_HW_SPIFLASH_SCK (pin_A5) +#define MICROPY_HW_SPIFLASH_MISO (pin_A6) +#define MICROPY_HW_SPIFLASH_MOSI (pin_A7) + +#define MICROPY_BOARD_EARLY_INIT VCC_GND_F407VE_board_early_init +void VCC_GND_F407VE_board_early_init(void); + +extern const struct _mp_spiflash_config_t spiflash_config; +extern struct _spi_bdev_t spi_bdev; +#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ + (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ + (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ + spi_bdev_ioctl(&spi_bdev, (op), (arg)) \ +) +#define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(&spi_bdev, (dest), (bl), (n)) +#define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(&spi_bdev, (src), (bl), (n)) + +#endif + +// SD card detect switch +#define MICROPY_HW_SDCARD_DETECT_PIN (pin_A8) +#define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) +#define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET) +// 1 - PC10 - DAT2/RES +// 2 - PC11 - CD/DAT3/CS +// 3 - PD2 - CMD/DI +// 4 - VCC - VDD +// 5 - PC12 - CLK/SCLK +// 6 - GND - VSS +// 7 - PC8 - DAT0/D0 +// 8 - PC9 - DAT1/RES +// 9 SW2 - GND +// 10 SW1 - PA8 + +// USB config +#define MICROPY_HW_USB_FS (1) +// #define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9) +// #define MICROPY_HW_USB_OTG_ID_PIN (pin_A10) diff --git a/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.mk b/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.mk new file mode 100644 index 0000000000000..b154dcfbacdc4 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/mpconfigboard.mk @@ -0,0 +1,6 @@ +MCU_SERIES = f4 +CMSIS_MCU = STM32F407xx +AF_FILE = boards/stm32f405_af.csv +LD_FILES = boards/stm32f405.ld boards/common_ifs.ld +TEXT0_ADDR = 0x08000000 +TEXT1_ADDR = 0x08020000 diff --git a/ports/stm32/boards/VCC_GND_F407VE/pins.csv b/ports/stm32/boards/VCC_GND_F407VE/pins.csv new file mode 100644 index 0000000000000..f04d96771b05d --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/pins.csv @@ -0,0 +1,112 @@ +P1_1,GND +P1_2,GND +P1_3,VBAT +P1_4,VCC_3V3 +P1_5,VIN_5V +P1_6,VIN_5V +P1_7,PE0 +P1_8,PE1 +P1_9,PE2 +P1_10,PE3 +P1_11,PE4 +P1_12,PE5 +P1_13,PE6 +P1_14,PC13 +P1_15,PC14 +P1_16,PC15 +P1_17,PC0 +P1_18,PC1 +P1_19,PC2 +P1_20,PC3 +P1_21,PA0 +P1_22,PA2 +P1_23,PA1 +P1_24,PA3 +P1_25,PA4 +P1_26,PA5 +P1_27,PA6 +P1_28,PA7 +P1_29,PC4 +P1_30,PC5 +P1_31,PB0 +P1_32,PB1 +P1_33,PB2 +P1_34,PE7 +P1_35,PE8 +P1_36,PE9 +P2_1,PE11 +P2_2,PE10 +P2_3,PE13 +P2_4,PE12 +P2_5,PE15 +P2_6,PE14 +P2_7,PB11 +P2_8,PB10 +P2_9,PB13 +P2_10,PB12 +P2_11,PB15 +P2_12,PB14 +P2_13,PD9 +P2_14,PD8 +P2_15,PD11 +P2_16,PD10 +P2_17,PD13 +P2_18,PD12 +P2_19,PD15 +P2_20,PD14 +P3_1,PC8 +P3_2,PC6 +P3_3,PC9 +P3_4,PC7 +P3_5,PA9 +P3_6,PA8 +P3_7,PA11 +P3_8,PA10 +P3_9,PA13 +P3_10,PA12 +P3_11,PA15 +P3_12,PA14 +P3_13,PC11 +P3_14,PC10 +P3_15,PC12 +P3_16,PD0 +P3_17,PD1 +P3_18,PD2 +P3_19,PD3 +P3_20,PD4 +P3_21,PD5 +P3_22,PD6 +P3_23,PD7 +P3_24,PB3 +P3_25,PB4 +P3_26,PB5 +P3_27,PB6 +P3_28,PB7 +P3_29,PB8 +P3_30,PB9 +P3_31,GND +P3_32,GND +P3_33,VCC_3V3 +P3_34,VCC_3V3 +P3_35,GND +P3_36,GND +DAC1,PA4 +DAC2,PA5 +BLUE_LED,PB9 +BOOT1,PB2 +USB_VBUS,PA9 +USB_ID,PA10 +USB_DM,PA11 +USB_DP,PA12 +SWCLK,PA14 +SWDIO,PA13 +OSC32_IN,PC14 +OSC32_OUT,PC15 +SD_D0,PC8 +SD_D1,PC9 +SD_D2,PC10 +SD_D3,PC11 +SD_CK,PC12 +SD_CMD,PD2 +SD,PA8 +SD_SW,PA8 diff --git a/ports/stm32/boards/VCC_GND_F407VE/stm32f4xx_hal_conf.h b/ports/stm32/boards/VCC_GND_F407VE/stm32f4xx_hal_conf.h new file mode 100644 index 0000000000000..fe2069fe1418a --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407VE/stm32f4xx_hal_conf.h @@ -0,0 +1,15 @@ +#ifndef MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H +#define MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H + +#include "boards/stm32f4xx_hal_conf_base.h" + +// Oscillator values in Hz +#define HSE_VALUE (25000000) +#define LSE_VALUE (32768) +#define EXTERNAL_CLOCK_VALUE (12288000) + +// Oscillator timeouts in ms +#define HSE_STARTUP_TIMEOUT (100) +#define LSE_STARTUP_TIMEOUT (5000) + +#endif // MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H From 1ca66efbf7be4f57ab31df5ecb81d27da4aefd0d Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Mon, 13 Jan 2020 17:26:01 +1100 Subject: [PATCH 0877/3533] stm32/boards: Add VCC_GND_F407ZG board. --- ports/stm32/boards/VCC_GND_F407ZG/bdev.c | 28 +++ .../stm32/boards/VCC_GND_F407ZG/board_init.c | 7 + .../boards/VCC_GND_F407ZG/mpconfigboard.h | 182 ++++++++++++++++++ .../boards/VCC_GND_F407ZG/mpconfigboard.mk | 10 + ports/stm32/boards/VCC_GND_F407ZG/pins.csv | 157 +++++++++++++++ .../VCC_GND_F407ZG/stm32f4xx_hal_conf.h | 15 ++ 6 files changed, 399 insertions(+) create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/bdev.c create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/board_init.c create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.h create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.mk create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/pins.csv create mode 100644 ports/stm32/boards/VCC_GND_F407ZG/stm32f4xx_hal_conf.h diff --git a/ports/stm32/boards/VCC_GND_F407ZG/bdev.c b/ports/stm32/boards/VCC_GND_F407ZG/bdev.c new file mode 100644 index 0000000000000..18b5b85b18f8c --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/bdev.c @@ -0,0 +1,28 @@ +#include "storage.h" + +#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE + +// External SPI flash uses standard SPI interface + +STATIC const mp_soft_spi_obj_t soft_spi_bus = { + .delay_half = MICROPY_HW_SOFTSPI_MIN_DELAY, + .polarity = 0, + .phase = 0, + .sck = MICROPY_HW_SPIFLASH_SCK, + .mosi = MICROPY_HW_SPIFLASH_MOSI, + .miso = MICROPY_HW_SPIFLASH_MISO, +}; + +STATIC mp_spiflash_cache_t spi_bdev_cache; + +const mp_spiflash_config_t spiflash_config = { + .bus_kind = MP_SPIFLASH_BUS_SPI, + .bus.u_spi.cs = MICROPY_HW_SPIFLASH_CS, + .bus.u_spi.data = (void*)&soft_spi_bus, + .bus.u_spi.proto = &mp_soft_spi_proto, + .cache = &spi_bdev_cache, +}; + +spi_bdev_t spi_bdev; + +#endif diff --git a/ports/stm32/boards/VCC_GND_F407ZG/board_init.c b/ports/stm32/boards/VCC_GND_F407ZG/board_init.c new file mode 100644 index 0000000000000..1f4663fdf9bac --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/board_init.c @@ -0,0 +1,7 @@ +#include "py/mphal.h" + +void VCC_GND_F407ZG_board_early_init(void) { + // set SPI flash CS pin high + mp_hal_pin_output(pin_C4); + mp_hal_pin_write(pin_C4, 1); +} diff --git a/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.h b/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.h new file mode 100644 index 0000000000000..bd17ab410f7c1 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.h @@ -0,0 +1,182 @@ +/* This file is part of the MicroPython project, http://micropython.org/ + * MIT License; Copyright (c) 2021 Damien P. George + */ + +// STM32F407ZGT6 Mini by VCC-GND Studio +// http://vcc-gnd.com/ +// https://item.taobao.com/item.htm?ft=t&id=523383164199 +// https://www.aliexpress.com/wholesale?SearchText=STM32F407ZGT6+Mini + +// DFU mode can be accessed by switching BOOT0 DIP ON (towards USB) + +#define MICROPY_HW_BOARD_NAME "VCC-GND STM32F407ZG" +#define MICROPY_HW_MCU_NAME "STM32F407ZG" +#define MICROPY_HW_FLASH_FS_LABEL "VCCGNDF407ZG" + +// 1 = use internal flash (1 MByte) +// 0 = use onboard SPI flash (512 KByte) Winbond W25X40 +#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) + +#define MICROPY_HW_HAS_FLASH (1) +#define MICROPY_HW_ENABLE_RNG (1) +#define MICROPY_HW_ENABLE_RTC (1) +#define MICROPY_HW_ENABLE_DAC (1) +#define MICROPY_HW_ENABLE_USB (1) +#define MICROPY_HW_ENABLE_SDCARD (1) + +// HSE is 25MHz +#define MICROPY_HW_CLK_PLLM (25) // divide external clock by this to get 1MHz +#define MICROPY_HW_CLK_PLLN (336) // PLL clock in MHz +#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2) // divide PLL clock by this to get core clock +#define MICROPY_HW_CLK_PLLQ (7) // divide core clock by this to get 48MHz + +// The board has a 32kHz crystal for the RTC +#define MICROPY_HW_RTC_USE_LSE (1) +#define MICROPY_HW_RTC_USE_US (0) +// #define MICROPY_HW_RTC_USE_CALOUT (1) // turn on/off PC13 512Hz output + +// USART1 +#define MICROPY_HW_UART1_TX (pin_A9) // PA9,PB6 +#define MICROPY_HW_UART1_RX (pin_A10) // PA10,PB7 +#define MICROPY_HW_UART1_RTS (pin_A12) +#define MICROPY_HW_UART1_CTS (pin_A11) +// USART1_CK PA8 + +// USART2 +#define MICROPY_HW_UART2_TX (pin_A2) // PA2,PD5 +#define MICROPY_HW_UART2_RX (pin_A3) // PA3,PD6 +#define MICROPY_HW_UART2_RTS (pin_A1) // PA1,PD4 +#define MICROPY_HW_UART2_CTS (pin_A0) // PA0,PD3 +// USART2_CK PA4,PD7 + +// USART3 +#define MICROPY_HW_UART3_TX (pin_D8) // PB10,PC10,PD8 +#define MICROPY_HW_UART3_RX (pin_D9) // PB11,PC11,PD9 +#define MICROPY_HW_UART3_RTS (pin_D12) // PB14,PD12 +#define MICROPY_HW_UART3_CTS (pin_D11) // PB13,PD11 +// USART3_CK PB12,PC12,PD10 + +// UART4 +#define MICROPY_HW_UART4_TX (pin_A0) // PA0,PC10 +#define MICROPY_HW_UART4_RX (pin_A1) // PA1,PC11 + +// UART5 +#define MICROPY_HW_UART5_TX (pin_C12) // PC12 +#define MICROPY_HW_UART5_RX (pin_D2) // PD2 + +// USART6 +#define MICROPY_HW_UART6_TX (pin_C6) // PC6,PG14 +#define MICROPY_HW_UART6_RX (pin_C7) // PC7,PG9 +#define MICROPY_HW_UART6_RTS (pin_G8) // PG8,PG12 +#define MICROPY_HW_UART6_CTS (pin_G13) // PG13,PG15 +// USART6_CK PC8,PG7 + +// I2C buses +#define MICROPY_HW_I2C1_SCL (pin_B6) // PB8,PB6 +#define MICROPY_HW_I2C1_SDA (pin_B7) // PB9,PB7 +// I2C1_SMBA PB5 +#define MICROPY_HW_I2C2_SCL (pin_B10) // PB10,PF1 +#define MICROPY_HW_I2C2_SDA (pin_B11) // PB11,PF0 +// I2C2_SMBA PB12,PF2 +#define MICROPY_HW_I2C3_SCL (pin_A8) // PA8 +#define MICROPY_HW_I2C3_SDA (pin_C9) // PC9 +// I2C3_SMBA PA9 + +// AT24C08 EEPROM on I2C1 0x50-0x53 + +// I2S buses - multiplexed with SPI2 and SPI3 +// I2S2_CK PB10,PB13 +// I2S2_MCK PC6 +// I2S2_SD PB15,PC3 +// I2S2_WS PB9,PB12 +// I2S3_CK PB3,PC10 +// I2S3_MCK PC7 +// I2S3_SD PB5,PC12 +// I2S3_WS PA4,PA15 + +// SPI buses +#define MICROPY_HW_SPI1_NSS (pin_A4) // PA4,PA15 +#define MICROPY_HW_SPI1_SCK (pin_A5) // PA5,PB3 +#define MICROPY_HW_SPI1_MISO (pin_A6) // PA6,PB4 +#define MICROPY_HW_SPI1_MOSI (pin_A7) // PA7,PB5 + +#define MICROPY_HW_SPI2_NSS (pin_B12) // PB12,PB9 +#define MICROPY_HW_SPI2_SCK (pin_B13) // PB13,PB10 +#define MICROPY_HW_SPI2_MISO (pin_B14) // PB14,PC2 +#define MICROPY_HW_SPI2_MOSI (pin_B15) // PB15,PC3 + +#define MICROPY_HW_SPI3_NSS (pin_A15) // PA15,PA4 +#define MICROPY_HW_SPI3_SCK (pin_B3) // PB3,PC10 +#define MICROPY_HW_SPI3_MISO (pin_B4) // PB4,PC11 +#define MICROPY_HW_SPI3_MOSI (pin_B5) // PB5,PC12 + +// CAN buses +#define MICROPY_HW_CAN1_TX (pin_B9) // PB9,PD1,PA12 +#define MICROPY_HW_CAN1_RX (pin_B8) // PB8,PD0,PA11 +#define MICROPY_HW_CAN2_TX (pin_B13) // PB13,PB6 +#define MICROPY_HW_CAN2_RX (pin_B12) // PB12,PB5 + +// DAC +// DAC_OUT1 PA4 +// DAC_OUT2 PA5 + +// LEDs +#define MICROPY_HW_LED1 (pin_G15) // blue +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) + +// If using onboard SPI flash +#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE + +// W25X40 SPI Flash = 4 Mbit (512 KByte) +#define MICROPY_HW_SPIFLASH_SIZE_BITS (4 * 1024 * 1024) +#define MICROPY_HW_SPIFLASH_CS (pin_C4) +#define MICROPY_HW_SPIFLASH_SCK (pin_A5) +#define MICROPY_HW_SPIFLASH_MISO (pin_A6) +#define MICROPY_HW_SPIFLASH_MOSI (pin_A7) + +#define MICROPY_BOARD_EARLY_INIT VCC_GND_F407ZG_board_early_init +void VCC_GND_F407ZG_board_early_init(void); + +extern const struct _mp_spiflash_config_t spiflash_config; +extern struct _spi_bdev_t spi_bdev; +#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ + (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ + (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ + spi_bdev_ioctl(&spi_bdev, (op), (arg)) \ +) +#define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(&spi_bdev, (dest), (bl), (n)) +#define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(&spi_bdev, (src), (bl), (n)) + +#endif + +// SD card detect switch +#define MICROPY_HW_SDCARD_DETECT_PIN (pin_F10) +#define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) +#define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET) +// 1 - PC10 - DAT2/RES +// 2 - PC11 - CD/DAT3/CS +// 3 - PD2 - CMD/DI +// 4 - VCC - VDD +// 5 - PC12 - CLK/SCLK +// 6 - GND - VSS +// 7 - PC8 - DAT0/D0 +// 8 - PC9 - DAT1/RES +// 9 SW2 - GND +// 10 SW1 - PF10 + +// USB config +#define MICROPY_HW_USB_FS (1) +// #define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9) +// #define MICROPY_HW_USB_OTG_ID_PIN (pin_A10) + +// Ethernet via RMII +#define MICROPY_HW_ETH_MDC (pin_C1) +#define MICROPY_HW_ETH_MDIO (pin_A2) +#define MICROPY_HW_ETH_RMII_REF_CLK (pin_A1) +#define MICROPY_HW_ETH_RMII_CRS_DV (pin_A7) +#define MICROPY_HW_ETH_RMII_RXD0 (pin_C4) +#define MICROPY_HW_ETH_RMII_RXD1 (pin_C5) +#define MICROPY_HW_ETH_RMII_TX_EN (pin_B11) +#define MICROPY_HW_ETH_RMII_TXD0 (pin_B12) +#define MICROPY_HW_ETH_RMII_TXD1 (pin_B13) diff --git a/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.mk b/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.mk new file mode 100644 index 0000000000000..c67807867ef17 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/mpconfigboard.mk @@ -0,0 +1,10 @@ +MCU_SERIES = f4 +CMSIS_MCU = STM32F407xx +AF_FILE = boards/stm32f405_af.csv +LD_FILES = boards/stm32f405.ld boards/common_ifs.ld +TEXT0_ADDR = 0x08000000 +TEXT1_ADDR = 0x08020000 + +MICROPY_PY_LWIP = 1 +MICROPY_PY_USSL = 1 +MICROPY_SSL_MBEDTLS = 1 diff --git a/ports/stm32/boards/VCC_GND_F407ZG/pins.csv b/ports/stm32/boards/VCC_GND_F407ZG/pins.csv new file mode 100644 index 0000000000000..780523a075f41 --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/pins.csv @@ -0,0 +1,157 @@ +P1_1,GND +P1_2,GND +P1_3,VBAT +P1_4,VCC_3V3 +P1_5,VIN_5V +P1_6,VIN_5V +P1_7,PF2 +P1_8,PF3 +P1_9,PF4 +P1_10,PF5 +P1_11,PF6 +P1_12,PF7 +P1_13,PF8 +P1_14,PF9 +P1_15,PF10 +P1_16,PC0 +P1_17,PC1 +P1_18,PC2 +P1_19,PC3 +P1_20,PA0 +P1_21,PA1 +P1_22,PA2 +P1_23,PA3 +P1_24,PA4 +P1_25,PA5 +P1_26,PA6 +P1_27,PA7 +P1_28,PC4 +P1_29,PC5 +P1_30,PB0 +P1_31,PB1 +P1_32,PB2 +P1_33,PF11 +P1_34,PF12 +P1_35,PF13 +P1_36,PF14 +P1_37,PF15 +P1_38,PG0 +P1_39,PG1 +P1_40,PE7 +P1_41,PE8 +P1_42,PE9 +P1_43,GND +P1_44,GND +P2_1,PG7 +P2_2,PG6 +P2_3,PG5 +P2_4,PG4 +P2_5,PG3 +P2_6,PG2 +P2_7,PD15 +P2_8,PD14 +P2_9,PD13 +P2_10,PD12 +P2_11,PD11 +P2_12,PD10 +P2_13,PD9 +P2_14,PD8 +P2_15,PB15 +P2_16,PB14 +P2_17,PB13 +P2_18,PB12 +P2_19,PB11 +P2_20,PB10 +P2_21,PE15 +P2_22,PE14 +P2_23,PE13 +P2_24,PE12 +P2_25,PE11 +P2_26,PE10 +P3_1,PF1 +P3_2,PF0 +P3_3,PC15 +P3_4,PC14 +P3_5,PC13 +P3_6,PE6 +P3_7,PE5 +P3_8,PE4 +P3_9,PE3 +P3_10,PE2 +P3_11,PE1 +P3_12,PE0 +P4_1,GND +P4_2,GND +P4_3,VCC_3V3 +P4_4,VCC_3V3 +P4_5,PB9 +P4_6,PB8 +P4_7,PB7 +P4_8,PB6 +P4_9,PB5 +P4_10,PB4 +P4_11,PB3 +P4_12,PG15 +P4_13,PG14 +P4_14,PG13 +P4_15,PG12 +P4_16,PG11 +P4_17,PG10 +P4_18,PG9 +P4_19,PD7 +P4_20,PD6 +P4_21,PD5 +P4_22,PD4 +P4_23,PD3 +P4_24,PD2 +P4_25,PD1 +P4_26,PD0 +P4_27,PC12 +P4_28,PC11 +P4_29,PC10 +P4_30,PA15 +P4_31,PA14 +P4_32,PA13 +P4_33,PA12 +P4_34,PA11 +P4_35,PA10 +P4_36,PA9 +P4_37,PA8 +P4_38,PC9 +P4_39,PC8 +P4_40,PC7 +P4_41,PC6 +P4_42,PG8 +P4_43,GND +P4_44,GND +DAC1,PA4 +DAC2,PA5 +SRAM_CS,PG10 +SF_CS,PB12 +BLUE_LED,PG15 +BOOT1,PB2 +USB_VBUS,PA9 +USB_ID,PA10 +USB_DM,PA11 +USB_DP,PA12 +SWCLK,PA14 +SWDIO,PA13 +OSC32_IN,PC14 +OSC32_OUT,PC15 +SD_D0,PC8 +SD_D1,PC9 +SD_D2,PC10 +SD_D3,PC11 +SD_CK,PC12 +SD_CMD,PD2 +SD,PF10 +SD_SW,PF10 +ETH_MDC,PC1 +ETH_MDIO,PA2 +ETH_RMII_REF_CLK,PA1 +ETH_RMII_CRS_DV,PA7 +ETH_RMII_RXD0,PC4 +ETH_RMII_RXD1,PC5 +ETH_RMII_TX_EN,PB11 +ETH_RMII_TXD0,PB12 +ETH_RMII_TXD1,PB13 diff --git a/ports/stm32/boards/VCC_GND_F407ZG/stm32f4xx_hal_conf.h b/ports/stm32/boards/VCC_GND_F407ZG/stm32f4xx_hal_conf.h new file mode 100644 index 0000000000000..fe2069fe1418a --- /dev/null +++ b/ports/stm32/boards/VCC_GND_F407ZG/stm32f4xx_hal_conf.h @@ -0,0 +1,15 @@ +#ifndef MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H +#define MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H + +#include "boards/stm32f4xx_hal_conf_base.h" + +// Oscillator values in Hz +#define HSE_VALUE (25000000) +#define LSE_VALUE (32768) +#define EXTERNAL_CLOCK_VALUE (12288000) + +// Oscillator timeouts in ms +#define HSE_STARTUP_TIMEOUT (100) +#define LSE_STARTUP_TIMEOUT (5000) + +#endif // MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H From 47b778332a0e89740775db544e7c22065df528c2 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Thu, 20 May 2021 16:21:47 +1000 Subject: [PATCH 0878/3533] all: Replace busses with buses. "buses" is the widely accepted plural form of "bus". --- docs/library/network.CC3K.rst | 2 +- docs/library/network.WIZNET5K.rst | 2 +- docs/library/pyb.CAN.rst | 2 +- docs/library/pyb.I2C.rst | 2 +- docs/library/pyb.SPI.rst | 2 +- docs/library/pyb.UART.rst | 2 +- docs/library/pyb.rst | 2 +- ports/stm32/boards/ADAFRUIT_F405_EXPRESS/mpconfigboard.h | 6 +++--- ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h | 4 ++-- ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h | 6 +++--- ports/stm32/boards/CERB40/mpconfigboard.h | 6 +++--- ports/stm32/boards/ESPRUINO_PICO/mpconfigboard.h | 4 ++-- ports/stm32/boards/HYDRABUS/mpconfigboard.h | 4 ++-- ports/stm32/boards/LIMIFROG/mpconfigboard.h | 4 ++-- ports/stm32/boards/NETDUINO_PLUS_2/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F413ZH/mpconfigboard.h | 6 +++--- ports/stm32/boards/NUCLEO_F429ZI/mpconfigboard.h | 6 +++--- ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_F746ZG/mpconfigboard.h | 6 +++--- ports/stm32/boards/NUCLEO_F767ZI/mpconfigboard.h | 6 +++--- ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_L452RE/mpconfigboard.h | 6 +++--- ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h | 6 +++--- ports/stm32/boards/OLIMEX_E407/mpconfigboard.h | 6 +++--- ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 6 +++--- ports/stm32/boards/PYBD_SF6/mpconfigboard.h | 2 +- ports/stm32/boards/PYBLITEV10/mpconfigboard.h | 4 ++-- ports/stm32/boards/PYBV10/mpconfigboard.h | 6 +++--- ports/stm32/boards/PYBV11/mpconfigboard.h | 6 +++--- ports/stm32/boards/PYBV3/mpconfigboard.h | 4 ++-- ports/stm32/boards/PYBV4/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32F411DISC/mpconfigboard.h | 4 ++-- ports/stm32/boards/STM32F429DISC/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32F439/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32F4DISC/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32F769DISC/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32F7DISC/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32L476DISC/mpconfigboard.h | 6 +++--- ports/stm32/boards/STM32L496GDISC/mpconfigboard.h | 4 ++-- ports/stm32/machine_uart.c | 2 +- ports/stm32/pyb_i2c.c | 2 +- ports/stm32/pyb_spi.c | 2 +- ports/stm32/spi.c | 2 +- ports/teensy/uart.c | 2 +- 49 files changed, 105 insertions(+), 105 deletions(-) diff --git a/docs/library/network.CC3K.rst b/docs/library/network.CC3K.rst index 18210e2d26d59..41d3fb437e992 100644 --- a/docs/library/network.CC3K.rst +++ b/docs/library/network.CC3K.rst @@ -25,7 +25,7 @@ For this example to work the CC3000 module must have the following connections: - VBEN connected to Y4 - IRQ connected to Y3 -It is possible to use other SPI busses and other pins for CS, VBEN and IRQ. +It is possible to use other SPI buses and other pins for CS, VBEN and IRQ. Constructors ------------ diff --git a/docs/library/network.WIZNET5K.rst b/docs/library/network.WIZNET5K.rst index e21e3a4978f5f..9e2c40f7f3932 100644 --- a/docs/library/network.WIZNET5K.rst +++ b/docs/library/network.WIZNET5K.rst @@ -26,7 +26,7 @@ For this example to work the WIZnet5x00 module must have the following connectio - nSS connected to X5 - nRESET connected to X4 -It is possible to use other SPI busses and other pins for nSS and nRESET. +It is possible to use other SPI buses and other pins for nSS and nRESET. Constructors ------------ diff --git a/docs/library/pyb.CAN.rst b/docs/library/pyb.CAN.rst index 649bcda108fcc..bcaeeba9df955 100644 --- a/docs/library/pyb.CAN.rst +++ b/docs/library/pyb.CAN.rst @@ -30,7 +30,7 @@ Constructors the bus, if any). If extra arguments are given, the bus is initialised. See :meth:`CAN.init` for parameters of initialisation. - The physical pins of the CAN busses are: + The physical pins of the CAN buses are: - ``CAN(1)`` is on ``YA``: ``(RX, TX) = (Y3, Y4) = (PB8, PB9)`` - ``CAN(2)`` is on ``YB``: ``(RX, TX) = (Y5, Y6) = (PB12, PB13)`` diff --git a/docs/library/pyb.I2C.rst b/docs/library/pyb.I2C.rst index 641dcb8815873..56b036e07447b 100644 --- a/docs/library/pyb.I2C.rst +++ b/docs/library/pyb.I2C.rst @@ -64,7 +64,7 @@ Constructors the bus, if any). If extra arguments are given, the bus is initialised. See ``init`` for parameters of initialisation. - The physical pins of the I2C busses on Pyboards V1.0 and V1.1 are: + The physical pins of the I2C buses on Pyboards V1.0 and V1.1 are: - ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)`` - ``I2C(2)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11)`` diff --git a/docs/library/pyb.SPI.rst b/docs/library/pyb.SPI.rst index 24e2ec5a73d2c..c76b16789ad47 100644 --- a/docs/library/pyb.SPI.rst +++ b/docs/library/pyb.SPI.rst @@ -36,7 +36,7 @@ Constructors the bus, if any). If extra arguments are given, the bus is initialised. See ``init`` for parameters of initialisation. - The physical pins of the SPI busses are: + The physical pins of the SPI buses are: - ``SPI(1)`` is on the X position: ``(NSS, SCK, MISO, MOSI) = (X5, X6, X7, X8) = (PA4, PA5, PA6, PA7)`` - ``SPI(2)`` is on the Y position: ``(NSS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8) = (PB12, PB13, PB14, PB15)`` diff --git a/docs/library/pyb.UART.rst b/docs/library/pyb.UART.rst index a1d6e59002551..e1735403c44a8 100644 --- a/docs/library/pyb.UART.rst +++ b/docs/library/pyb.UART.rst @@ -57,7 +57,7 @@ Constructors the bus, if any). If extra arguments are given, the bus is initialised. See ``init`` for parameters of initialisation. - The physical pins of the UART busses on Pyboard are: + The physical pins of the UART buses on Pyboard are: - ``UART(4)`` is on ``XA``: ``(TX, RX) = (X1, X2) = (PA0, PA1)`` - ``UART(1)`` is on ``XB``: ``(TX, RX) = (X9, X10) = (PB6, PB7)`` diff --git a/docs/library/pyb.rst b/docs/library/pyb.rst index addcd20a91aeb..321be3c52f2fe 100644 --- a/docs/library/pyb.rst +++ b/docs/library/pyb.rst @@ -126,7 +126,7 @@ Power related functions - pclk2: frequency of the APB2 bus If given any arguments then the function sets the frequency of the CPU, - and the busses if additional arguments are given. Frequencies are given in + and the buses if additional arguments are given. Frequencies are given in Hz. Eg freq(120000000) sets sysclk (the CPU frequency) to 120MHz. Note that not all values are supported and the largest supported frequency not greater than the given value will be selected. diff --git a/ports/stm32/boards/ADAFRUIT_F405_EXPRESS/mpconfigboard.h b/ports/stm32/boards/ADAFRUIT_F405_EXPRESS/mpconfigboard.h index 36d1d31315e38..afa81a5ba0938 100644 --- a/ports/stm32/boards/ADAFRUIT_F405_EXPRESS/mpconfigboard.h +++ b/ports/stm32/boards/ADAFRUIT_F405_EXPRESS/mpconfigboard.h @@ -39,7 +39,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) // D6 #define MICROPY_HW_UART6_RX (pin_C7) // D5 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "I2C1" #define MICROPY_HW_I2C1_SCL (pin_B6) // SCL #define MICROPY_HW_I2C1_SDA (pin_B7) // SDA @@ -47,7 +47,7 @@ #define MICROPY_HW_I2C2_SCL (pin_B10) // TX #define MICROPY_HW_I2C2_SDA (pin_B11) // RX -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "SPIFLASH" #define MICROPY_HW_SPI1_NSS (pin_A15) // FLASH CS #define MICROPY_HW_SPI1_SCK (pin_B3) // FLASH CLK @@ -59,7 +59,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) // MISO #define MICROPY_HW_SPI2_MOSI (pin_B15) // MOSI -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_NAME "CAN1" #define MICROPY_HW_CAN1_TX (pin_B9) // D10 #define MICROPY_HW_CAN1_RX (pin_B8) // D9 diff --git a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h index b330e8fdc1f27..0cded1bc866ac 100644 --- a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h +++ b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h @@ -35,11 +35,11 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h index a88bcf675685e..b017edab99313 100644 --- a/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h +++ b/ports/stm32/boards/B_L475E_IOT01A/mpconfigboard.h @@ -28,20 +28,20 @@ // USART3 config for internal use #define MICROPY_HW_UART3_TX (pin_D8) #define MICROPY_HW_UART3_RX (pin_D9) -// USART4 config +// USART4 config #define MICROPY_HW_UART4_TX (pin_A0) #define MICROPY_HW_UART4_RX (pin_A1) // USART 1 is connected to the virtual com port on the ST-LINK #define MICROPY_HW_UART_REPL PYB_UART_1 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/CERB40/mpconfigboard.h b/ports/stm32/boards/CERB40/mpconfigboard.h index 7c166922be674..2f9199d4e02be 100644 --- a/ports/stm32/boards/CERB40/mpconfigboard.h +++ b/ports/stm32/boards/CERB40/mpconfigboard.h @@ -31,7 +31,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) @@ -39,7 +39,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_C9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) @@ -49,7 +49,7 @@ #define MICROPY_HW_SPI3_MISO (pin_B4) #define MICROPY_HW_SPI3_MOSI (pin_B5) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/ESPRUINO_PICO/mpconfigboard.h b/ports/stm32/boards/ESPRUINO_PICO/mpconfigboard.h index 53c7f3cd50934..ed8178d7d4d6e 100644 --- a/ports/stm32/boards/ESPRUINO_PICO/mpconfigboard.h +++ b/ports/stm32/boards/ESPRUINO_PICO/mpconfigboard.h @@ -32,7 +32,7 @@ #define MICROPY_HW_UART6_TX (pin_A11) #define MICROPY_HW_UART6_RX (pin_A12) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) @@ -40,7 +40,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_B4) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/HYDRABUS/mpconfigboard.h b/ports/stm32/boards/HYDRABUS/mpconfigboard.h index d8f1a864b29a7..d894f1606cea6 100644 --- a/ports/stm32/boards/HYDRABUS/mpconfigboard.h +++ b/ports/stm32/boards/HYDRABUS/mpconfigboard.h @@ -30,13 +30,13 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/LIMIFROG/mpconfigboard.h b/ports/stm32/boards/LIMIFROG/mpconfigboard.h index 4b750c17ff4f6..be9b5a8bb878f 100644 --- a/ports/stm32/boards/LIMIFROG/mpconfigboard.h +++ b/ports/stm32/boards/LIMIFROG/mpconfigboard.h @@ -25,13 +25,13 @@ void LIMIFROG_board_early_init(void); #define MICROPY_HW_UART3_TX (pin_C10) #define MICROPY_HW_UART3_RX (pin_C11) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/NETDUINO_PLUS_2/mpconfigboard.h b/ports/stm32/boards/NETDUINO_PLUS_2/mpconfigboard.h index d2075b52bf5f9..38bf8b41e910d 100644 --- a/ports/stm32/boards/NETDUINO_PLUS_2/mpconfigboard.h +++ b/ports/stm32/boards/NETDUINO_PLUS_2/mpconfigboard.h @@ -39,11 +39,11 @@ void NETDUINO_PLUS_2_board_early_init(void); #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) -// SPI busses +// SPI buses #define MICROPY_HW_SPI2_NSS (pin_B12) #define MICROPY_HW_SPI2_SCK (pin_B13) #define MICROPY_HW_SPI2_MISO (pin_B14) diff --git a/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h index fb8de60221e99..7333dcd0c1dca 100644 --- a/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h @@ -49,13 +49,13 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B9) // Arduino D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 #define MICROPY_HW_I2C2_SDA (pin_B11) // pin 18 on CN10 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 diff --git a/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h index babf63c7a1c91..6b874e298c1bb 100644 --- a/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F401RE/mpconfigboard.h @@ -30,7 +30,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B9) // D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 @@ -38,7 +38,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) // Arduino D7, pin 23 on CN10 #define MICROPY_HW_I2C3_SDA (pin_C9) // pin 1 on CN10 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 diff --git a/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h index 26b1e0b619d2e..e37ce889c38b0 100644 --- a/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F411RE/mpconfigboard.h @@ -21,7 +21,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B9) // D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 @@ -29,7 +29,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) // Arduino D7, pin 23 on CN10 #define MICROPY_HW_I2C3_SDA (pin_C9) // pin 1 on CN10 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 diff --git a/ports/stm32/boards/NUCLEO_F413ZH/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F413ZH/mpconfigboard.h index 2f488159a4f00..5297ceda5c4e6 100644 --- a/ports/stm32/boards/NUCLEO_F413ZH/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F413ZH/mpconfigboard.h @@ -41,7 +41,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_3 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_F1) @@ -49,7 +49,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_C9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) // shared with DAC #define MICROPY_HW_SPI1_SCK (pin_A5) // shared with DAC #define MICROPY_HW_SPI1_MISO (pin_A6) @@ -71,7 +71,7 @@ #define MICROPY_HW_SPI5_MISO (pin_E13) #define MICROPY_HW_SPI5_MOSI (pin_E14) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_G1) #define MICROPY_HW_CAN1_RX (pin_G0) #define MICROPY_HW_CAN2_TX (pin_G12) diff --git a/ports/stm32/boards/NUCLEO_F429ZI/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F429ZI/mpconfigboard.h index 05c98e113f7ed..86c02b517879a 100644 --- a/ports/stm32/boards/NUCLEO_F429ZI/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F429ZI/mpconfigboard.h @@ -38,11 +38,11 @@ #define MICROPY_HW_UART_REPL PYB_UART_3 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_C9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_B3) #define MICROPY_HW_SPI1_MISO (pin_B4) @@ -58,7 +58,7 @@ #define MICROPY_HW_SPI5_MISO (pin_F8) #define MICROPY_HW_SPI5_MOSI (pin_F9) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.h index cd4ad36fa0ee9..27845b33aa833 100644 --- a/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.h @@ -24,7 +24,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) // Arduino D10, pin 17 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B7) // pin 21 on CN7 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 @@ -32,7 +32,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) // Arduino D7, pin 23 on CN10 #define MICROPY_HW_I2C3_SDA (pin_C9) // pin 1 on CN10 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 diff --git a/ports/stm32/boards/NUCLEO_F746ZG/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F746ZG/mpconfigboard.h index c9b0c60f274b0..402efcfe31beb 100644 --- a/ports/stm32/boards/NUCLEO_F746ZG/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F746ZG/mpconfigboard.h @@ -40,19 +40,19 @@ #define MICROPY_HW_UART_REPL PYB_UART_3 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// SPI +// SPI buses #define MICROPY_HW_SPI3_NSS (pin_A4) #define MICROPY_HW_SPI3_SCK (pin_B3) #define MICROPY_HW_SPI3_MISO (pin_B4) #define MICROPY_HW_SPI3_MOSI (pin_B5) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/NUCLEO_F767ZI/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F767ZI/mpconfigboard.h index b659223b4d94e..4ed9f41832d85 100644 --- a/ports/stm32/boards/NUCLEO_F767ZI/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F767ZI/mpconfigboard.h @@ -41,7 +41,7 @@ void NUCLEO_F767ZI_board_early_init(void); #define MICROPY_HW_UART_REPL PYB_UART_3 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_F1) @@ -49,13 +49,13 @@ void NUCLEO_F767ZI_board_early_init(void); #define MICROPY_HW_I2C4_SCL (pin_F14) #define MICROPY_HW_I2C4_SDA (pin_F15) -// SPI +// SPI buses #define MICROPY_HW_SPI3_NSS (pin_A4) #define MICROPY_HW_SPI3_SCK (pin_B3) #define MICROPY_HW_SPI3_MISO (pin_B4) #define MICROPY_HW_SPI3_MOSI (pin_B5) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_D1) #define MICROPY_HW_CAN1_RX (pin_D0) diff --git a/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h b/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h index 324f2b03ce0d0..f25263fd2b13c 100644 --- a/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_H743ZI/mpconfigboard.h @@ -49,7 +49,7 @@ void NUCLEO_H743ZI_board_early_init(void); #define MICROPY_HW_UART_REPL PYB_UART_3 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_F1) @@ -57,7 +57,7 @@ void NUCLEO_H743ZI_board_early_init(void); #define MICROPY_HW_I2C4_SCL (pin_F14) #define MICROPY_HW_I2C4_SDA (pin_F15) -// SPI +// SPI buses #define MICROPY_HW_SPI3_NSS (pin_A4) #define MICROPY_HW_SPI3_SCK (pin_B3) #define MICROPY_HW_SPI3_MISO (pin_B4) diff --git a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h index a5c5dc9a0ee7b..b7f7bc4c885ba 100644 --- a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h @@ -34,11 +34,11 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B9) // Arduino D14, pin 5 on CN10 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 diff --git a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h index 14b3bab8a7158..0a3ac1f185a70 100644 --- a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h @@ -42,13 +42,13 @@ #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_A9) #define MICROPY_HW_I2C1_SDA (pin_A10) #define MICROPY_HW_I2C3_SCL (pin_A7) #define MICROPY_HW_I2C3_SDA (pin_B4) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_B0) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/NUCLEO_L452RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L452RE/mpconfigboard.h index 9823ae9b9b74d..1b5827d5aec00 100644 --- a/ports/stm32/boards/NUCLEO_L452RE/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L452RE/mpconfigboard.h @@ -34,7 +34,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 #define MICROPY_HW_I2C1_SDA (pin_B9) // Arduino D14, pin 5 on CN10 #define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 @@ -44,7 +44,7 @@ #define MICROPY_HW_I2C4_SCL (pin_C0) // pin 38 on CN7 #define MICROPY_HW_I2C4_SDA (pin_C1) // pin 36 on CN7 -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 #define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 #define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 @@ -58,7 +58,7 @@ #define MICROPY_HW_SPI3_MISO (pin_C11) // pin 2 on CN7 #define MICROPY_HW_SPI3_MOSI (pin_C12) // pin 3 on CN7 -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) diff --git a/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h index 2899a97aceeda..a7bb0b36bbf01 100644 --- a/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L476RG/mpconfigboard.h @@ -46,7 +46,7 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_B10) @@ -54,7 +54,7 @@ #define MICROPY_HW_I2C3_SCL (pin_C0) #define MICROPY_HW_I2C3_SDA (pin_C1) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_B3) #define MICROPY_HW_SPI1_MISO (pin_B4) @@ -64,7 +64,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) #define MICROPY_HW_SPI2_MOSI (pin_B15) -// CAN bus +// CAN buses #define MICROPY_HW_CAN1_TX (pin_A12) #define MICROPY_HW_CAN1_RX (pin_A11) diff --git a/ports/stm32/boards/OLIMEX_E407/mpconfigboard.h b/ports/stm32/boards/OLIMEX_E407/mpconfigboard.h index d5cd7bb1ec665..999f97a708720 100644 --- a/ports/stm32/boards/OLIMEX_E407/mpconfigboard.h +++ b/ports/stm32/boards/OLIMEX_E407/mpconfigboard.h @@ -36,13 +36,13 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) @@ -52,7 +52,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) #define MICROPY_HW_SPI2_MOSI (pin_B15) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 57cc75efc6f92..48b3ee3eb9092 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -122,7 +122,7 @@ extern struct _spi_bdev_t spi_bdev2; #define MICROPY_HW_UART6_RTS (pyb_pin_BT_RTS) #define MICROPY_HW_UART6_CTS (pyb_pin_BT_CTS) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_HW_I2C1_SCL (pyb_pin_X9) #define MICROPY_HW_I2C1_SDA (pyb_pin_X10) @@ -130,7 +130,7 @@ extern struct _spi_bdev_t spi_bdev2; #define MICROPY_HW_I2C2_SCL (pyb_pin_Y9) #define MICROPY_HW_I2C2_SDA (pyb_pin_Y10) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pyb_pin_X5) #define MICROPY_HW_SPI1_SCK (pyb_pin_X6) @@ -146,7 +146,7 @@ extern struct _spi_bdev_t spi_bdev2; #define MICROPY_HW_SPI3_MISO (pyb_pin_W50) #define MICROPY_HW_SPI3_MOSI (pyb_pin_W46) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_NAME "X" #define MICROPY_HW_CAN1_TX (pyb_pin_X10) #define MICROPY_HW_CAN1_RX (pyb_pin_X9) diff --git a/ports/stm32/boards/PYBD_SF6/mpconfigboard.h b/ports/stm32/boards/PYBD_SF6/mpconfigboard.h index cbfbe39945daf..8c11d7b2eec96 100644 --- a/ports/stm32/boards/PYBD_SF6/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF6/mpconfigboard.h @@ -49,7 +49,7 @@ #define MICROPY_HW_UART7_TX (pyb_pin_W16) #define MICROPY_HW_UART7_RX (pyb_pin_W22B) -// Extra CAN busses +// Extra CAN buses #define MICROPY_HW_CAN2_NAME "Y" #define MICROPY_HW_CAN2_TX (pyb_pin_Y6) #define MICROPY_HW_CAN2_RX (pyb_pin_Y5) diff --git a/ports/stm32/boards/PYBLITEV10/mpconfigboard.h b/ports/stm32/boards/PYBLITEV10/mpconfigboard.h index 77509a6003716..1643e250cb5c9 100644 --- a/ports/stm32/boards/PYBLITEV10/mpconfigboard.h +++ b/ports/stm32/boards/PYBLITEV10/mpconfigboard.h @@ -35,7 +35,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) @@ -43,7 +43,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_B8) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pin_A4) // X5 #define MICROPY_HW_SPI1_SCK (pin_A5) // X6 diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.h b/ports/stm32/boards/PYBV10/mpconfigboard.h index dda98776cd345..52382f44d5c14 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.h +++ b/ports/stm32/boards/PYBV10/mpconfigboard.h @@ -44,7 +44,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) @@ -52,7 +52,7 @@ #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pin_A4) // X5 #define MICROPY_HW_SPI1_SCK (pin_A5) // X6 @@ -64,7 +64,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) // Y7 #define MICROPY_HW_SPI2_MOSI (pin_B15) // Y8 -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_NAME "YA" #define MICROPY_HW_CAN1_TX (pin_B9) // Y4 #define MICROPY_HW_CAN1_RX (pin_B8) // Y3 diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.h b/ports/stm32/boards/PYBV11/mpconfigboard.h index 7d10c13e8fc00..8e84694048201 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.h +++ b/ports/stm32/boards/PYBV11/mpconfigboard.h @@ -44,7 +44,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) @@ -52,7 +52,7 @@ #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pin_A4) // X5 #define MICROPY_HW_SPI1_SCK (pin_A5) // X6 @@ -64,7 +64,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) // Y7 #define MICROPY_HW_SPI2_MOSI (pin_B15) // Y8 -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_NAME "YA" #define MICROPY_HW_CAN1_TX (pin_B9) // Y4 #define MICROPY_HW_CAN1_RX (pin_B8) // Y3 diff --git a/ports/stm32/boards/PYBV3/mpconfigboard.h b/ports/stm32/boards/PYBV3/mpconfigboard.h index 7f1531770f307..9c956efd6d099 100644 --- a/ports/stm32/boards/PYBV3/mpconfigboard.h +++ b/ports/stm32/boards/PYBV3/mpconfigboard.h @@ -44,7 +44,7 @@ #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pin_A4) // X5 #define MICROPY_HW_SPI1_SCK (pin_A5) // X6 @@ -56,7 +56,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) // Y7 #define MICROPY_HW_SPI2_MOSI (pin_B15) // Y8 -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) // Y4 #define MICROPY_HW_CAN1_RX (pin_B8) // Y3 #define MICROPY_HW_CAN2_TX (pin_B13) // Y6 diff --git a/ports/stm32/boards/PYBV4/mpconfigboard.h b/ports/stm32/boards/PYBV4/mpconfigboard.h index c1c60751138a8..2bd569ecdfa9f 100644 --- a/ports/stm32/boards/PYBV4/mpconfigboard.h +++ b/ports/stm32/boards/PYBV4/mpconfigboard.h @@ -41,7 +41,7 @@ #define MICROPY_HW_UART6_PORT (GPIOC) #define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) @@ -49,7 +49,7 @@ #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NAME "X" #define MICROPY_HW_SPI1_NSS (pin_A4) // X5 #define MICROPY_HW_SPI1_SCK (pin_A5) // X6 @@ -61,7 +61,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) // Y7 #define MICROPY_HW_SPI2_MOSI (pin_B15) // Y8 -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_NAME "YA" #define MICROPY_HW_CAN1_TX (pin_B9) // Y4 #define MICROPY_HW_CAN1_RX (pin_B8) // Y3 diff --git a/ports/stm32/boards/STM32F411DISC/mpconfigboard.h b/ports/stm32/boards/STM32F411DISC/mpconfigboard.h index 13172333b7b78..97da20461257f 100644 --- a/ports/stm32/boards/STM32F411DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F411DISC/mpconfigboard.h @@ -26,7 +26,7 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B9) //#define MICROPY_HW_I2C2_SCL (pin_B10) @@ -34,7 +34,7 @@ #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_A9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) diff --git a/ports/stm32/boards/STM32F429DISC/mpconfigboard.h b/ports/stm32/boards/STM32F429DISC/mpconfigboard.h index 9b65f617437a4..95cab08c8fa18 100644 --- a/ports/stm32/boards/STM32F429DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F429DISC/mpconfigboard.h @@ -22,11 +22,11 @@ #define MICROPY_HW_UART2_TX (pin_D8) #define MICROPY_HW_UART2_RX (pin_D9) -// I2C busses +// I2C buses #define MICROPY_HW_I2C3_SCL (pin_A8) #define MICROPY_HW_I2C3_SDA (pin_C9) -// SPI busses +// SPI buses //#define MICROPY_HW_SPI1_NSS (pin_A4) //#define MICROPY_HW_SPI1_SCK (pin_A5) //#define MICROPY_HW_SPI1_MISO (pin_A6) @@ -52,7 +52,7 @@ //#define MICROPY_HW_SPI6_MISO (pin_G12) //#define MICROPY_HW_SPI6_MOSI (pin_G14) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/STM32F439/mpconfigboard.h b/ports/stm32/boards/STM32F439/mpconfigboard.h index a4e10934458f7..702ef265bb323 100644 --- a/ports/stm32/boards/STM32F439/mpconfigboard.h +++ b/ports/stm32/boards/STM32F439/mpconfigboard.h @@ -40,11 +40,11 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_A8) #define MICROPY_HW_I2C1_SDA (pin_C9) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) @@ -74,7 +74,7 @@ //#define MICROPY_HW_SPI6_MISO (pin_G12) //#define MICROPY_HW_SPI6_MOSI (pin_G14) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/STM32F4DISC/mpconfigboard.h b/ports/stm32/boards/STM32F4DISC/mpconfigboard.h index 3e4c8261ccae6..dbe52e6b10e0b 100644 --- a/ports/stm32/boards/STM32F4DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F4DISC/mpconfigboard.h @@ -43,13 +43,13 @@ #define MICROPY_HW_UART6_TX (pin_C6) #define MICROPY_HW_UART6_RX (pin_C7) -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI1_NSS (pin_A4) #define MICROPY_HW_SPI1_SCK (pin_A5) #define MICROPY_HW_SPI1_MISO (pin_A6) @@ -59,7 +59,7 @@ #define MICROPY_HW_SPI2_MISO (pin_B14) #define MICROPY_HW_SPI2_MOSI (pin_B15) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h index 5004a8ecac9d0..87cefb50176ff 100644 --- a/ports/stm32/boards/STM32F769DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F769DISC/mpconfigboard.h @@ -59,19 +59,19 @@ extern struct _spi_bdev_t spi_bdev; #define MICROPY_HW_UART_REPL PYB_UART_1 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// SPI +// SPI buses #define MICROPY_HW_SPI2_NSS (pin_A11) #define MICROPY_HW_SPI2_SCK (pin_A12) #define MICROPY_HW_SPI2_MISO (pin_B14) #define MICROPY_HW_SPI2_MOSI (pin_B15) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/STM32F7DISC/mpconfigboard.h b/ports/stm32/boards/STM32F7DISC/mpconfigboard.h index b07f0a7b8ffc7..cf7061902ee75 100644 --- a/ports/stm32/boards/STM32F7DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32F7DISC/mpconfigboard.h @@ -38,20 +38,20 @@ void STM32F7DISC_board_early_init(void); #define MICROPY_HW_UART_REPL PYB_UART_1 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B8) #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// SPI +// SPI buses #define MICROPY_HW_SPI2_NSS (pin_I0) #define MICROPY_HW_SPI2_SCK (pin_I1) #define MICROPY_HW_SPI2_MISO (pin_B14) #define MICROPY_HW_SPI2_MOSI (pin_B15) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) #define MICROPY_HW_CAN2_TX (pin_B13) diff --git a/ports/stm32/boards/STM32L476DISC/mpconfigboard.h b/ports/stm32/boards/STM32L476DISC/mpconfigboard.h index ad62f761e55f3..58fb8ef1440d2 100644 --- a/ports/stm32/boards/STM32L476DISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32L476DISC/mpconfigboard.h @@ -50,19 +50,19 @@ extern struct _spi_bdev_t spi_bdev; #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_B6) #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// SPI busses +// SPI buses #define MICROPY_HW_SPI2_NSS (pin_D0) #define MICROPY_HW_SPI2_SCK (pin_D1) #define MICROPY_HW_SPI2_MISO (pin_D3) #define MICROPY_HW_SPI2_MOSI (pin_D4) -// CAN busses +// CAN buses #define MICROPY_HW_CAN1_TX (pin_B9) #define MICROPY_HW_CAN1_RX (pin_B8) diff --git a/ports/stm32/boards/STM32L496GDISC/mpconfigboard.h b/ports/stm32/boards/STM32L496GDISC/mpconfigboard.h index 5d9fa25bc3b84..d4d78454af7ee 100644 --- a/ports/stm32/boards/STM32L496GDISC/mpconfigboard.h +++ b/ports/stm32/boards/STM32L496GDISC/mpconfigboard.h @@ -26,13 +26,13 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -// I2C busses +// I2C buses #define MICROPY_HW_I2C1_SCL (pin_G14) #define MICROPY_HW_I2C1_SDA (pin_G13) #define MICROPY_HW_I2C2_SCL (pin_H4) #define MICROPY_HW_I2C2_SDA (pin_B14) -// SPI busses +// SPI buses // -> To the arduino connector #define MICROPY_HW_SPI1_NSS (pin_A15) #define MICROPY_HW_SPI1_SCK (pin_A5) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index eceb5b6e7a8b2..1b8db2e3e633e 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -294,7 +294,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const /// the bus, if any). If extra arguments are given, the bus is initialised. /// See `init` for parameters of initialisation. /// -/// The physical pins of the UART busses are: +/// The physical pins of the UART buses are: /// /// - `UART(4)` is on `XA`: `(TX, RX) = (X1, X2) = (PA0, PA1)` /// - `UART(1)` is on `XB`: `(TX, RX) = (X9, X10) = (PB6, PB7)` diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index ee8b498a1359c..f0bd38333d6c1 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -660,7 +660,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, size_t n_args, co /// the bus, if any). If extra arguments are given, the bus is initialised. /// See `init` for parameters of initialisation. /// -/// The physical pins of the I2C busses are: +/// The physical pins of the I2C buses are: /// /// - `I2C(1)` is on the X position: `(SCL, SDA) = (X9, X10) = (PB6, PB7)` /// - `I2C(2)` is on the Y position: `(SCL, SDA) = (Y9, Y10) = (PB10, PB11)` diff --git a/ports/stm32/pyb_spi.c b/ports/stm32/pyb_spi.c index cc9dd1163623d..99a1cd77b4dbd 100644 --- a/ports/stm32/pyb_spi.c +++ b/ports/stm32/pyb_spi.c @@ -125,7 +125,7 @@ STATIC mp_obj_t pyb_spi_init_helper(const pyb_spi_obj_t *self, size_t n_args, co // the bus, if any). If extra arguments are given, the bus is initialised. // See `init` for parameters of initialisation. // -// The physical pins of the SPI busses are: +// The physical pins of the SPI buses are: // - `SPI(1)` is on the X position: `(NSS, SCK, MISO, MOSI) = (X5, X6, X7, X8) = (PA4, PA5, PA6, PA7)` // - `SPI(2)` is on the Y position: `(NSS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8) = (PB12, PB13, PB14, PB15)` // diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index f8ab791898d3f..d3b3a784e86f9 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -31,7 +31,7 @@ #include "py/mphal.h" #include "spi.h" -// Possible DMA configurations for SPI busses: +// Possible DMA configurations for SPI buses: // SPI1_TX: DMA2_Stream3.CHANNEL_3 or DMA2_Stream5.CHANNEL_3 // SPI1_RX: DMA2_Stream0.CHANNEL_3 or DMA2_Stream2.CHANNEL_3 // SPI2_TX: DMA1_Stream4.CHANNEL_0 diff --git a/ports/teensy/uart.c b/ports/teensy/uart.c index 471b40ee0169d..6373047b004f6 100644 --- a/ports/teensy/uart.c +++ b/ports/teensy/uart.c @@ -302,7 +302,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, uint n_args, const mp /// the bus, if any). If extra arguments are given, the bus is initialised. /// See `init` for parameters of initialisation. /// -/// The physical pins of the UART busses are: +/// The physical pins of the UART buses are: /// /// - `UART(4)` is on `XA`: `(TX, RX) = (X1, X2) = (PA0, PA1)` /// - `UART(1)` is on `XB`: `(TX, RX) = (X9, X10) = (PB6, PB7)` From 04927dfaca06d68a35ff1a5700a5e4eb283b5f2d Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 14 Nov 2020 18:54:26 +1100 Subject: [PATCH 0879/3533] tools/mpy_ld.py: Support R_X86_64_GOTPCREL reloc for x86-64 arch. This can be treated by the linker the same as R_X86_64_REX_GOTPCRELX, according to https://reviews.llvm.org/D18301. Signed-off-by: Damien George --- tools/mpy_ld.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index 774966a7f68a0..852249943887e 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -73,6 +73,7 @@ R_ARM_BASE_PREL = 25 # aka R_ARM_GOTPC R_ARM_GOT_BREL = 26 # aka R_ARM_GOT32 R_ARM_THM_JUMP24 = 30 +R_X86_64_GOTPCREL = 9 R_X86_64_REX_GOTPCRELX = 42 R_386_GOT32X = 43 @@ -132,7 +133,7 @@ def __init__(self, name, mpy_feature, qstr_entry_size, word_size, arch_got, asm_ | MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, 2, 8, - (R_X86_64_REX_GOTPCRELX,), + (R_X86_64_GOTPCREL, R_X86_64_REX_GOTPCRELX), asm_jump_x86, ), "armv7m": ArchData( @@ -536,7 +537,10 @@ def do_relocation_text(env, text_addr, r): # Relcation pointing to GOT reloc = addr = env.got_entries[s.name].offset - elif env.arch.name == "EM_X86_64" and r_info_type == R_X86_64_REX_GOTPCRELX: + elif env.arch.name == "EM_X86_64" and r_info_type in ( + R_X86_64_GOTPCREL, + R_X86_64_REX_GOTPCRELX, + ): # Relcation pointing to GOT got_entry = env.got_entries[s.name] addr = env.got_section.addr + got_entry.offset From f49d47c167ce97b48ff3e9cbbc016b09664390f5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 24 Nov 2020 23:37:05 +1100 Subject: [PATCH 0880/3533] py/asmx64: Support use of top 8 regs in src_r64 argument. Signed-off-by: Damien George --- py/asmx64.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/py/asmx64.c b/py/asmx64.c index fd64eaf98b1c1..62df5c6d4ad79 100644 --- a/py/asmx64.c +++ b/py/asmx64.c @@ -285,31 +285,28 @@ void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest } void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { - assert(src_r64 < 8); - if (dest_r64 < 8) { + if (src_r64 < 8 && dest_r64 < 8) { asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64); } else { - asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM8_TO_R64); + asm_x64_write_byte_3(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), 0x0f, OPCODE_MOVZX_RM8_TO_R64); } asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); } void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { - assert(src_r64 < 8); - if (dest_r64 < 8) { + if (src_r64 < 8 && dest_r64 < 8) { asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64); } else { - asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM16_TO_R64); + asm_x64_write_byte_3(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), 0x0f, OPCODE_MOVZX_RM16_TO_R64); } asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); } void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { - assert(src_r64 < 8); - if (dest_r64 < 8) { + if (src_r64 < 8 && dest_r64 < 8) { asm_x64_write_byte_1(as, OPCODE_MOV_RM64_TO_R64); } else { - asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_RM64_TO_R64); + asm_x64_write_byte_2(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), OPCODE_MOV_RM64_TO_R64); } asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); } From 5176a2d7325d941d8f2f41b31eab87b6e7a83f06 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 24 Nov 2020 23:37:58 +1100 Subject: [PATCH 0881/3533] py/emitnative: Fix x86-64 emitter to generate correct 8/16-bit stores. Fixes issue #6643. Signed-off-by: Damien George --- py/emitnative.c | 4 ++-- tests/micropython/viper_misc2.py | 21 +++++++++++++++++++++ tests/micropython/viper_misc2.py.exp | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/micropython/viper_misc2.py create mode 100644 tests/micropython/viper_misc2.py.exp diff --git a/py/emitnative.c b/py/emitnative.c index 052a505911d2a..425ba2d33e528 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -1769,7 +1769,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) { int reg_index = REG_ARG_2; int reg_value = REG_ARG_3; emit_pre_pop_reg_flexible(emit, &vtype_base, ®_base, reg_index, reg_value); - #if N_X86 + #if N_X64 || N_X86 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX) emit_pre_pop_reg(emit, &vtype_value, reg_value); #else @@ -1856,7 +1856,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) { EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("can't store with '%q' index"), vtype_to_qstr(vtype_index)); } - #if N_X86 + #if N_X64 || N_X86 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX) emit_pre_pop_reg(emit, &vtype_value, reg_value); #else diff --git a/tests/micropython/viper_misc2.py b/tests/micropython/viper_misc2.py new file mode 100644 index 0000000000000..8f0be487d626d --- /dev/null +++ b/tests/micropython/viper_misc2.py @@ -0,0 +1,21 @@ +# Miscellaneous viper tests + +# Test correct use of registers in load and store +@micropython.viper +def expand(dest: ptr8, source: ptr8, length: int): + n = 0 + for x in range(0, length, 2): + c = source[x] + d = source[x + 1] + dest[n] = (c & 0xE0) | ((c & 0x1C) >> 1) + n += 1 + dest[n] = ((c & 3) << 6) | ((d & 0xE0) >> 4) + n += 1 + dest[n] = ((d & 0x1C) << 3) | ((d & 3) << 2) + n += 1 + + +source = b"\xaa\xaa\xff\xff" +dest = bytearray(len(source) // 2 * 3) +expand(dest, source, len(source)) +print(dest) diff --git a/tests/micropython/viper_misc2.py.exp b/tests/micropython/viper_misc2.py.exp new file mode 100644 index 0000000000000..eff2f5e41d244 --- /dev/null +++ b/tests/micropython/viper_misc2.py.exp @@ -0,0 +1 @@ +bytearray(b'\xa4\x8aH\xee\xce\xec') From a96afae90f6e5d693173382561d06e583b0b5fa5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 May 2021 14:10:37 +1000 Subject: [PATCH 0882/3533] stm32/sdio: Fix case of SDIO DMA turning off mid transfer. The DMA driver will turn off DMA if it hasn't been used for an amount of time (to save power). The SDIO driver for cyw43 WLAN was not informing the DMA driver that it was using DMA and there was a chance that the DMA would turn off in the middle of an SDIO DMA transfer. The symptoms of this would be printing of SDIO error messages and a failure to communicate with the cyw43 WLAN module. This commit fixes this issue by changing the SDIO driver to use the dma_nohal_XXX API to initialise and start the DMA. Signed-off-by: Damien George --- ports/stm32/dma.c | 2 +- ports/stm32/sdio.c | 35 +++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index 4320315ba4e5d..d4b41771b0810 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -40,7 +40,7 @@ #define DMA_IDLE_TICK_MAX (8) // 8*8 = 64 msec #define DMA_IDLE_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & DMA_SYSTICK_MASK) == 0) -#define ENABLE_SDIO (MICROPY_HW_ENABLE_SDCARD || MICROPY_HW_ENABLE_MMCARD) +#define ENABLE_SDIO (MICROPY_HW_ENABLE_SDCARD || MICROPY_HW_ENABLE_MMCARD || MICROPY_PY_NETWORK_CYW43) typedef enum { dma_id_not_defined=-1, diff --git a/ports/stm32/sdio.c b/ports/stm32/sdio.c index dfd409257759a..0291e3e81d3c0 100644 --- a/ports/stm32/sdio.c +++ b/ports/stm32/sdio.c @@ -28,6 +28,7 @@ #include "py/mperrno.h" #include "py/mphal.h" +#include "dma.h" #include "pin.h" #include "pin_static_af.h" #include "pendsv.h" @@ -382,27 +383,21 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le } #if defined(STM32F7) - DMA2->LIFCR = 0x3f << 22; - DMA2_Stream3->FCR = 0x07; // ? - DMA2_Stream3->PAR = (uint32_t)&SDMMC->FIFO; if ((uint32_t)buf & 3) { printf("sdio_transfer_cmd53: buf=%p is not aligned for DMA\n", buf); return -MP_EINVAL; } - DMA2_Stream3->M0AR = (uint32_t)buf; - DMA2_Stream3->NDTR = ((len + block_size - 1) & ~(block_size - 1)) / 4; - DMA2_Stream3->CR = 4 << 25 // channel 4 - | 1 << 23 // MBURST INCR4 - | 1 << 21 // PBURST INCR4 - | 3 << 16 // PL very high - | 2 << 13 // MSIZE word - | 2 << 11 // PSIZE word - | 1 << 10 // MINC enabled - | 0 << 9 // PINC disabled - | write << 6 // DIR mem-to-periph - | 1 << 5 // PFCTRL periph is flow controller - | 1 << 0 // EN + uint32_t dma_config = + 2 << DMA_SxCR_MSIZE_Pos // MSIZE word + | 2 << DMA_SxCR_PSIZE_Pos // PSIZE word + | write << DMA_SxCR_DIR_Pos // DIR mem-to-periph + | 1 << DMA_SxCR_PFCTRL_Pos // PFCTRL periph is flow controller ; + uint32_t dma_src = (uint32_t)buf; + uint32_t dma_dest = (uint32_t)&SDMMC->FIFO; + uint32_t dma_len = ((len + block_size - 1) & ~(block_size - 1)) / 4; + dma_nohal_init(&dma_SDIO_0, dma_config); + dma_nohal_start(&dma_SDIO_0, dma_src, dma_dest, dma_len); #else SDMMC->IDMABASE0 = (uint32_t)buf; SDMMC->IDMACTRL = SDMMC_IDMA_IDMAEN; @@ -456,6 +451,9 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le #else printf("sdio_transfer_cmd53: timeout wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->DCTRL, (uint)SDMMC->IDMACTRL); #endif + if (sdmmc_dma) { + dma_nohal_deinit(&dma_SDIO_0); + } return -MP_ETIMEDOUT; } } @@ -468,6 +466,9 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le #else printf("sdio_transfer_cmd53: error=%08lx wr=%d len=%u dma=%u buf_idx=%u STA=%08x SDMMC=%08x:%08x IDMA=%08x\n", sdmmc_error, write, (uint)len, (uint)dma, sdmmc_buf_cur - buf, (uint)SDMMC->STA, (uint)SDMMC->DCOUNT, (uint)SDMMC->DCTRL, (uint)SDMMC->IDMACTRL); #endif + if (sdmmc_dma) { + dma_nohal_deinit(&dma_SDIO_0); + } return -(0x1000000 | sdmmc_error); } @@ -476,6 +477,8 @@ int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t le printf("sdio_transfer_cmd53: didn't transfer correct length: cur=%p top=%p\n", sdmmc_buf_cur, sdmmc_buf_top); return -MP_EIO; } + } else { + dma_nohal_deinit(&dma_SDIO_0); } return 0; From 748339b28126e69fd2dc2778b2a182901d0a4693 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 May 2021 22:17:45 +1000 Subject: [PATCH 0883/3533] stm32/uart: Configure pull-up only on RX and CTS, not TX and RTS. RX and CTS are the input pins and pull-ups are enabled so they don't cause a problem if left unconnected. But the output pins don't need a pull up (they were originally all configured with pull up in commit 8f7491a109a555ca897ae49efe98f4cc2b080311). If needed, the pull-ups can be disabled in Python using machine.Pin after the UART is constructed. See issue #4369. Signed-off-by: Damien George --- ports/stm32/uart.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index a2b69967b6e41..9f74416170f86 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -474,10 +474,11 @@ bool uart_init(pyb_uart_obj_t *uart_obj, } uint32_t mode = MP_HAL_PIN_MODE_ALT; - uint32_t pull = MP_HAL_PIN_PULL_UP; for (uint i = 0; i < 4; i++) { if (pins[i] != NULL) { + // Configure pull-up on RX and CTS (the input pins). + uint32_t pull = (i & 1) ? MP_HAL_PIN_PULL_UP : MP_HAL_PIN_PULL_NONE; bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, uart_fn, uart_unit); if (!ret) { return false; From ea81bcf1c0ddeb8be34192cbededecfa425c039b Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 17 May 2021 23:57:45 +1000 Subject: [PATCH 0884/3533] stm32/mboot: Leave bootloader from thread mode, not from IRQ. Leaving the bootloader from an IRQ (eg USB or I2C IRQ) will not work if MBOOT_LEAVE_BOOTLOADER_VIA_RESET is disabled, ie if mboot jumps directly to the application. This is because the CPU will still be in IRQ state when the application starts and IRQs of lower priority will be blocked. Fix this by setting a flag when the bootloader should finish, and exit the bootloader always from the main (top level) thread. This also improves the USB behaviour of mboot: it no longer abruptly disconnects when the manifest command is sent. Signed-off-by: Damien George --- ports/stm32/mboot/dfu.h | 2 ++ ports/stm32/mboot/main.c | 33 ++++++++++++++++----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/ports/stm32/mboot/dfu.h b/ports/stm32/mboot/dfu.h index 73db3545d8d55..e867a3e22d0c6 100644 --- a/ports/stm32/mboot/dfu.h +++ b/ports/stm32/mboot/dfu.h @@ -26,6 +26,7 @@ #ifndef MICROPY_INCLUDED_STM32_MBOOT_DFU_H #define MICROPY_INCLUDED_STM32_MBOOT_DFU_H +#include #include // DFU spec: https://www.usb.org/sites/default/files/DFU_1.1.pdf @@ -106,6 +107,7 @@ typedef struct _dfu_state_t { dfu_cmd_t cmd; dfu_status_t status; uint8_t error; + bool leave_dfu; uint16_t wBlockNum; uint16_t wLength; uint32_t addr; diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 3a0242a8d6bf0..c1e1d59d235db 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -106,8 +106,6 @@ static volatile uint32_t systick_ms; // Global dfu state dfu_context_t dfu_context SECTION_NOZERO_BSS; -static void do_reset(void); - uint32_t get_le32(const uint8_t *b) { return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24; } @@ -771,7 +769,7 @@ void i2c_slave_process_rx_end(i2c_slave_t *i2c) { memcpy(buf + 12 + sizeof(MICROPY_HW_MCU_NAME), MICROPY_HW_BOARD_NAME, sizeof(MICROPY_HW_BOARD_NAME) - 1); len = 12 + sizeof(MICROPY_HW_MCU_NAME) + sizeof(MICROPY_HW_BOARD_NAME) - 1; } else if (buf[0] == I2C_CMD_RESET && len == 0) { - do_reset(); + dfu_context.leave_dfu = true; } else if (buf[0] == I2C_CMD_GETLAYOUT && len == 0) { len = strlen(FLASH_LAYOUT_STR); memcpy(buf, FLASH_LAYOUT_STR, len); @@ -864,6 +862,7 @@ static void dfu_init(void) { dfu_context.cmd = DFU_CMD_NONE; dfu_context.status = DFU_STATUS_OK; dfu_context.error = 0; + dfu_context.leave_dfu = false; dfu_context.addr = 0x08000000; } @@ -931,7 +930,8 @@ static void dfu_handle_rx(int cmd, int arg, int len, const void *buf) { static void dfu_process(void) { if (dfu_context.state == DFU_STATE_MANIFEST) { - do_reset(); + // Set a flag to leave DFU mode from the main thread (here we are in an IRQ handler). + dfu_context.leave_dfu = true; } if (dfu_context.state == DFU_STATE_BUSY) { @@ -1412,17 +1412,6 @@ static void leave_bootloader(void) { NVIC_SystemReset(); } -static void do_reset(void) { - led_state_all(0); - mp_hal_delay_ms(50); - pyb_usbdd_shutdown(); - #if defined(MBOOT_I2C_SCL) - i2c_slave_shutdown(MBOOT_I2Cx, I2Cx_EV_IRQn); - #endif - mp_hal_delay_ms(50); - leave_bootloader(); -} - extern PCD_HandleTypeDef pcd_fs_handle; extern PCD_HandleTypeDef pcd_hs_handle; @@ -1561,7 +1550,7 @@ void stm32_main(int initial_r0) { #if MBOOT_USB_RESET_ON_DISCONNECT bool has_connected = false; #endif - for (;;) { + while (!dfu_context.leave_dfu) { #if USE_USB_POLLING #if MBOOT_USB_AUTODETECT_PORT || MICROPY_HW_USB_MAIN_DEV == USB_PHY_FS_ID if (USB_OTG_FS->GINTSTS & USB_OTG_FS->GINTMSK) { @@ -1585,10 +1574,20 @@ void stm32_main(int initial_r0) { has_connected = true; } if (has_connected && pyb_usbdd.hUSBDDevice.dev_state == USBD_STATE_SUSPENDED) { - do_reset(); + break; } #endif } + + // Shutdown and leave the bootloader. + led_state_all(0); + mp_hal_delay_ms(50); + pyb_usbdd_shutdown(); + #if defined(MBOOT_I2C_SCL) + i2c_slave_shutdown(MBOOT_I2Cx, I2Cx_EV_IRQn); + #endif + mp_hal_delay_ms(50); + leave_bootloader(); } void NMI_Handler(void) { From 247d7e2e8e7ec262a141b0fc4190a94cce8103ef Mon Sep 17 00:00:00 2001 From: Tobias Thyrrestrup Date: Tue, 18 May 2021 15:38:49 +0200 Subject: [PATCH 0885/3533] tools/pydfu.py: Remove default VID/PID values. As the new default behaviour, this allows PyDFU to be used with all devices, not just the ones matching a specific set of VID/PID values. But it's still possible to specify VID/PID if needed to narrow down the selection of the USB device. Signed-off-by: Tobias Thyrrestrup --- tools/pydfu.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index 42b4fa2da6feb..ea658d300bf38 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -23,10 +23,6 @@ import usb.util import zlib -# VID/PID -__VID = 0x0483 -__PID = 0xDF11 - # USB request __TIMEOUT __TIMEOUT = 4000 @@ -112,10 +108,10 @@ def find_dfu_cfg_descr(descr): return None -def init(): +def init(**kwargs): """Initializes the found DFU device so that we can program it.""" global __dev, __cfg_descr - devices = get_dfu_devices(idVendor=__VID, idProduct=__PID) + devices = get_dfu_devices(**kwargs) if not devices: raise ValueError("No DFU device found") if len(devices) > 1: @@ -565,15 +561,13 @@ def cli_progress(addr, offset, size): def main(): """Test program for verifying this files functionality.""" global __verbose - global __VID - global __PID # Parse CMD args parser = argparse.ArgumentParser(description="DFU Python Util") parser.add_argument( "-l", "--list", help="list available DFU devices", action="store_true", default=False ) - parser.add_argument("--vid", help="USB Vendor ID", type=lambda x: int(x, 0), default=__VID) - parser.add_argument("--pid", help="USB Product ID", type=lambda x: int(x, 0), default=__PID) + parser.add_argument("--vid", help="USB Vendor ID", type=lambda x: int(x, 0), default=None) + parser.add_argument("--pid", help="USB Product ID", type=lambda x: int(x, 0), default=None) parser.add_argument( "-m", "--mass-erase", help="mass erase device", action="store_true", default=False ) @@ -588,14 +582,18 @@ def main(): __verbose = args.verbose - __VID = args.vid - __PID = args.pid + kwargs = {} + if args.vid: + kwargs["idVendor"] = args.vid + + if args.pid: + kwargs["idProduct"] = args.pid if args.list: - list_dfu_devices(idVendor=__VID, idProduct=__PID) + list_dfu_devices(**kwargs) return - init() + init(**kwargs) command_run = False if args.mass_erase: From a7a9f2fe897d2c4e3c18618c996460224df08114 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Thu, 20 May 2021 22:12:35 -0500 Subject: [PATCH 0886/3533] tools/ci.sh: Update zephyr docker image to v0.17.3. Updates the zephyr docker image and SDK to the latest versions. Signed-off-by: Maureen Helm --- tools/ci.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/ci.sh b/tools/ci.sh index 33cf364ddd22d..57421ae242bcd 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -479,14 +479,14 @@ function ci_windows_build { # ports/zephyr function ci_zephyr_setup { - docker pull zephyrprojectrtos/ci:v0.11.13 + docker pull zephyrprojectrtos/ci:v0.17.3 docker run --name zephyr-ci -d -it \ -v "$(pwd)":/micropython \ - -e ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk-0.12.2 \ + -e ZEPHYR_SDK_INSTALL_DIR=/opt/toolchains/zephyr-sdk-0.12.4 \ -e ZEPHYR_TOOLCHAIN_VARIANT=zephyr \ -e ZEPHYR_BASE=/zephyrproject/zephyr \ -w /micropython/ports/zephyr \ - zephyrprojectrtos/ci:v0.11.13 + zephyrprojectrtos/ci:v0.17.3 docker ps -a } From e61ac453dcabaeca2406f1fb6ef113522f341aa2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 22 May 2021 22:09:25 +1000 Subject: [PATCH 0887/3533] py/mkrules.cmake: Add MPY_LIB_DIR and BOARD_DIR to makemanifest call. So that the FROZEN_MANIFEST option in cmake works the same as make. Signed-off-by: Damien George --- py/mkrules.cmake | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/py/mkrules.cmake b/py/mkrules.cmake index f20240c62b299..7589255b2039a 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -125,9 +125,13 @@ if(MICROPY_FROZEN_MANIFEST) MICROPY_MODULE_FROZEN_MPY=\(1\) ) + if(NOT MICROPY_LIB_DIR) + set(MICROPY_LIB_DIR ${MICROPY_DIR}/../micropython-lib) + endif() + add_custom_command( OUTPUT ${MICROPY_FROZEN_CONTENT} - COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/tools/makemanifest.py -o ${MICROPY_FROZEN_CONTENT} -v "MPY_DIR=${MICROPY_DIR}" -v "PORT_DIR=${MICROPY_PORT_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MICROPY_CROSS_FLAGS} ${MICROPY_FROZEN_MANIFEST} + COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/tools/makemanifest.py -o ${MICROPY_FROZEN_CONTENT} -v "MPY_DIR=${MICROPY_DIR}" -v "MPY_LIB_DIR=${MICROPY_LIB_DIR}" -v "PORT_DIR=${MICROPY_PORT_DIR}" -v "BOARD_DIR=${MICROPY_BOARD_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MICROPY_CROSS_FLAGS} ${MICROPY_FROZEN_MANIFEST} DEPENDS MICROPY_FORCE_BUILD ${MICROPY_QSTRDEFS_GENERATED} VERBATIM From 3b950ed2959c603ff30dd6052c157e7981c4d2d6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 22 May 2021 22:10:09 +1000 Subject: [PATCH 0888/3533] tools/ci.sh: Use FROZEN_MANIFEST in an esp32 build to test feature. This tests that FROZEN_MANIFEST works with cmake (on esp32 at least). Signed-off-by: Damien George --- tools/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci.sh b/tools/ci.sh index 57421ae242bcd..7b38ce0df04fc 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -109,7 +109,7 @@ function ci_esp32_build { make ${MAKEOPTS} -C ports/esp32 submodules make ${MAKEOPTS} -C ports/esp32 make ${MAKEOPTS} -C ports/esp32 clean - make ${MAKEOPTS} -C ports/esp32 USER_C_MODULES=../../../examples/usercmodule/micropython.cmake + make ${MAKEOPTS} -C ports/esp32 USER_C_MODULES=../../../examples/usercmodule/micropython.cmake FROZEN_MANIFEST=$(pwd)/ports/esp32/boards/manifest.py if [ -d $IDF_PATH/components/esp32s2 ]; then make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S2 fi From 0aa01b0205adc7544315fed602029e792a235b2e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 00:04:01 +1000 Subject: [PATCH 0889/3533] lib/mbedtls: Switch to currently latest commit of LTS branch v2.16. Signed-off-by: Damien George --- lib/mbedtls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mbedtls b/lib/mbedtls index 1b6a24f759cef..1bc2c9cb8b8fe 160000 --- a/lib/mbedtls +++ b/lib/mbedtls @@ -1 +1 @@ -Subproject commit 1b6a24f759cefbb4a69ae9476885d7f42a847e3d +Subproject commit 1bc2c9cb8b8fe4659bd94b8ebba5a4c02029b7fa From ff5d39529c3bf331dd8171aaa24937b2650f40fd Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Fri, 21 Aug 2020 16:03:21 +0200 Subject: [PATCH 0890/3533] mimxrt: Implement machine.Pin class. - modified pin type from pin_obj_t to machine_pin_obj_t - created machine_pin.c - implemented basic version of make-pins.py to genertate pins.c/.h files automatically; the only alternate function currently supported is GPIO - added af.csv files for all supported MCUs - replaced pins.c/pins.h files with pin.csv for all boards - implemented on/off/high/low/value/init methods - Implemented IN/OUT/OPEN_DRAIN modes - modified LDFLAGS for DEBUG build to get usefull .elf file for debugging Signed-off-by: Philipp Ebensberger --- ports/mimxrt/Makefile | 41 ++- ports/mimxrt/README.md | 5 +- ports/mimxrt/board_init.c | 2 - .../boards/MIMXRT1010_EVK/mpconfigboard.h | 2 +- .../boards/MIMXRT1010_EVK/mpconfigboard.mk | 19 ++ ports/mimxrt/boards/MIMXRT1010_EVK/pins.c | 33 --- ports/mimxrt/boards/MIMXRT1010_EVK/pins.csv | 57 ++++ ports/mimxrt/boards/MIMXRT1010_EVK/pins.h | 30 -- ports/mimxrt/boards/MIMXRT1011.ld | 2 +- ports/mimxrt/boards/MIMXRT1011_af.csv | 44 +++ .../boards/MIMXRT1020_EVK/mpconfigboard.h | 3 +- .../boards/MIMXRT1020_EVK/mpconfigboard.mk | 18 ++ ports/mimxrt/boards/MIMXRT1020_EVK/pins.c | 33 --- ports/mimxrt/boards/MIMXRT1020_EVK/pins.csv | 33 +++ ports/mimxrt/boards/MIMXRT1020_EVK/pins.h | 30 -- ports/mimxrt/boards/MIMXRT1021_af.csv | 94 ++++++ .../boards/MIMXRT1050_EVK/mpconfigboard.h | 2 +- ports/mimxrt/boards/MIMXRT1050_EVK/pins.c | 33 --- ports/mimxrt/boards/MIMXRT1050_EVK/pins.csv | 51 ++++ ports/mimxrt/boards/MIMXRT1050_EVK/pins.h | 30 -- ports/mimxrt/boards/MIMXRT1052_af.csv | 125 ++++++++ .../boards/MIMXRT1060_EVK/mpconfigboard.h | 2 +- .../boards/MIMXRT1060_EVK/mpconfigboard.mk | 19 ++ ports/mimxrt/boards/MIMXRT1060_EVK/pins.c | 33 --- ports/mimxrt/boards/MIMXRT1060_EVK/pins.csv | 51 ++++ ports/mimxrt/boards/MIMXRT1060_EVK/pins.h | 30 -- ports/mimxrt/boards/MIMXRT1062_af.csv | 125 ++++++++ .../boards/MIMXRT1064_EVK/mpconfigboard.h | 2 +- ports/mimxrt/boards/MIMXRT1064_EVK/pins.c | 33 --- ports/mimxrt/boards/MIMXRT1064_EVK/pins.csv | 51 ++++ ports/mimxrt/boards/MIMXRT1064_EVK/pins.h | 30 -- ports/mimxrt/boards/MIMXRT1064_af.csv | 125 ++++++++ ports/mimxrt/boards/TEENSY40/mpconfigboard.h | 2 +- ports/mimxrt/boards/TEENSY40/pins.c | 33 --- ports/mimxrt/boards/TEENSY40/pins.csv | 55 ++++ ports/mimxrt/boards/TEENSY40/pins.h | 30 -- ports/mimxrt/boards/make-pins.py | 259 +++++++++++++++++ ports/mimxrt/boards/mimxrt_prefix.c | 26 ++ ports/mimxrt/led.c | 6 +- ports/mimxrt/led.h | 2 +- ports/mimxrt/machine_pin.c | 273 ++++++++++++++++++ ports/mimxrt/modmachine.c | 2 + ports/mimxrt/mphalport.h | 4 + ports/mimxrt/pin.c | 106 ++++++- ports/mimxrt/pin.h | 112 ++++--- ports/mimxrt/tusb_port.c | 2 +- 46 files changed, 1662 insertions(+), 438 deletions(-) delete mode 100644 ports/mimxrt/boards/MIMXRT1010_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1010_EVK/pins.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1010_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1011_af.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/pins.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1020_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1021_af.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/pins.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1050_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1052_af.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/pins.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1060_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1062_af.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/pins.c create mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/pins.csv delete mode 100644 ports/mimxrt/boards/MIMXRT1064_EVK/pins.h create mode 100644 ports/mimxrt/boards/MIMXRT1064_af.csv delete mode 100644 ports/mimxrt/boards/TEENSY40/pins.c create mode 100644 ports/mimxrt/boards/TEENSY40/pins.csv delete mode 100644 ports/mimxrt/boards/TEENSY40/pins.h create mode 100644 ports/mimxrt/boards/make-pins.py create mode 100644 ports/mimxrt/boards/mimxrt_prefix.c create mode 100644 ports/mimxrt/machine_pin.c diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 3a001182a036a..0c893f9634a8b 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -23,6 +23,16 @@ GIT_SUBMODULES = lib/tinyusb lib/nxp_driver MCU_DIR = lib/nxp_driver/sdk/devices/$(MCU_SERIES) LD_FILES = boards/$(MCU_SERIES).ld $(TOP)/$(MCU_DIR)/gcc/$(MCU_SERIES)xxxxx_flexspi_nor.ld +MAKE_PINS = boards/make-pins.py +BOARD_PINS = $(BOARD_DIR)/pins.csv +AF_FILE = boards/$(MCU_SERIES)_af.csv +PREFIX_FILE = boards/mimxrt_prefix.c +GEN_PINS_SRC = $(BUILD)/pins_gen.c +GEN_PINS_HDR = $(HEADER_BUILD)/pins.h +GEN_PINS_QSTR = $(BUILD)/pins_qstr.h +GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h +GEN_PINS_AF_PY = $(BUILD)/pins_af.py + # mcu driver cause following warnings #CFLAGS += -Wno-error=float-equal -Wno-error=nested-externs CFLAGS += -Wno-error=unused-parameter @@ -49,12 +59,14 @@ CFLAGS += -DXIP_EXTERNAL_FLASH=1 \ -D__START=main \ -DCPU_HEADER_H='<$(MCU_SERIES).h>' -LDFLAGS = $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref +LDFLAGS = $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref --print-memory-usage LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Tune for Debugging or Optimization ifeq ($(DEBUG),1) CFLAGS += -O0 -ggdb +LDFLAGS += --gc-sections +CFLAGS += -fdata-sections -ffunction-sections else CFLAGS += -Os -DNDEBUG LDFLAGS += --gc-sections @@ -95,8 +107,8 @@ SRC_C = \ tusb_port.c \ board_init.c \ $(BOARD_DIR)/flash_config.c \ - $(BOARD_DIR)/pins.c \ machine_led.c \ + machine_pin.c \ modutime.c \ modmachine.c \ mphalport.c \ @@ -116,15 +128,17 @@ SRC_S = lib/utils/gchelper_m3.s \ # List of sources for qstr extraction SRC_QSTR += \ machine_led.c \ + machine_pin.c \ modutime.c \ modmachine.c \ pin.c \ - $(BOARD_DIR)/pins.c \ + $(GEN_PINS_SRC) \ OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SS:.S=.o)) +OBJ += $(BUILD)/pins_gen.o # Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };" $(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces @@ -142,4 +156,25 @@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(Q)$(OBJCOPY) -O ihex -R .eeprom $< $@ +# Making OBJ use an order-only depenedency on the generated pins.h file +# has the side effect of making the pins.h file before we actually compile +# any of the objects. The normal dependency generation will deal with the +# case when pins.h is modified. But when it doesn't exist, we don't know +# which source files might need it. +$(OBJ): | $(GEN_PINS_HDR) + +# With conditional pins, we may need to regenerate qstrdefs.h when config +# options change. +$(HEADER_BUILD)/qstrdefs.generated.h: $(BOARD_DIR)/mpconfigboard.h + +# Use a pattern rule here so that make will only call make-pins.py once to make +# both pins_gen.c and pins.h +$(BUILD)/%_gen.c $(HEADER_BUILD)/%.h: $(BOARD_PINS) $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD) + $(ECHO) "Create $@" + $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE)\ + --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) > $(GEN_PINS_SRC) + +$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c + $(call compile_c) + include $(TOP)/py/mkrules.mk diff --git a/ports/mimxrt/README.md b/ports/mimxrt/README.md index a2b5cf79a16ae..c24939f478791 100644 --- a/ports/mimxrt/README.md +++ b/ports/mimxrt/README.md @@ -5,10 +5,13 @@ Currently supports Teensy 4.0 and the i.MX RT1010 EVK board. Features: - REPL over USB VCP + - machine.Pin Known issues: - pyboard.py doesn't work with files larger than 64 bytes + - machine.Pin class currently does not support GPIOMUX option of + i.MX RT101x variants TODO: - Enable TCM - - Peripherals (pins, LED, Timers, etc) + - Peripherals (LED, Timers, etc) diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c index 9095c880482d3..0336cdf6865d3 100644 --- a/ports/mimxrt/board_init.c +++ b/ports/mimxrt/board_init.c @@ -39,8 +39,6 @@ #include "clock_config.h" -#define LED_STATE_ON (0) - volatile uint32_t systick_ms = 0; const uint8_t dcd_data[] = { 0x00 }; diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h index eeddd0e0213fb..5a89423b50e35 100644 --- a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h @@ -4,6 +4,6 @@ #define BOARD_FLASH_SIZE (16 * 1024 * 1024) // i.MX RT1010 EVK has 1 board LED -#define MICROPY_HW_LED1_PIN (GPIO_11) +#define MICROPY_HW_LED1_PIN (pin_GPIO_11) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk index e178545845b2d..c5b5055324b17 100644 --- a/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk @@ -2,6 +2,25 @@ MCU_SERIES = MIMXRT1011 MCU_VARIANT = MIMXRT1011DAE5A JLINK_PATH = /media/RT1010-EVK/ +JLINK_COMMANDER_SCRIPT = $(BUILD)/script.jlink + + +ifdef JLINK_IP +JLINK_CONNECTION_SETTINGS = -IP $(JLINK_IP) +else +JLINK_CONNECTION_SETTINGS = -USB +endif + + +deploy_jlink: $(BUILD)/firmware.hex + $(Q)$(TOUCH) $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "ExitOnError 1" > $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "speed auto" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "r" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "st" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "loadfile \"$(realpath $(BUILD)/firmware.hex)\"" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "qc" >> $(JLINK_COMMANDER_SCRIPT) + $(JLINK_PATH)JLinkExe -device $(MCU_VARIANT) -if SWD $(JLINK_CONNECTION_SETTINGS) -CommanderScript $(JLINK_COMMANDER_SCRIPT) deploy: $(BUILD)/firmware.bin cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c deleted file mode 100644 index e0397e09edb21..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_11_af[] = { - PIN_AF(GPIOMUX_IO11, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), -}; - -pin_obj_t GPIO_11 = PIN(GPIO_11, GPIO1, 5, GPIO_11_af); diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.csv b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.csv new file mode 100644 index 0000000000000..f7fdf39643eab --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.csv @@ -0,0 +1,57 @@ +D0,GPIO_09 +D1,GPIO_10 +D2,GPIO_AD_05 +D3,GPIO_AD_06 +D4,GPIO_08 +D5,GPIO_01 +D6,GPIO_AD_01 +D7,GPIO_AD_02 +D8,GPIO_SD_02 +D9,GPIO_03 +D10,GPIO_AD_05 +D11,GPIO_AD_04 +D12,GPIO_AD_03 +D13,GPIO_AD_06 +D14,GPIO_01 +D15,GPIO_02 +CUR_A,GPIO_AD_01 +CUR_B,GPIO_AD_02 +CUR_C,GPIO_AD_07 +VOLT_DCB,GPIO_AD_09 +CUR_DCB,GPIO_AD_10 +A0,GPIO_AD_07 +A1,GPIO_AD_09 +A2,GPIO_AD_10 +A3,GPIO_AD_14 +A4,GPIO_AD_01 +A5,GPIO_AD_02 +PWM_AT,GPIO_02 +PWM_AB,GPIO_01 +PWM_BT,GPIO_04 +PWM_BB,GPIO_03 +PWM_CT,GPIO_06 +PWM_CB,GPIO_05 +ENC_A,GPIO_AD_05 +ENC_B,GPIO_AD_06 +LED_GREEN,GPIO_11 +GPIO_01,GPIO_01 +GPIO_02,GPIO_02 +GPIO_03,GPIO_03 +GPIO_04,GPIO_04 +GPIO_05,GPIO_05 +GPIO_06,GPIO_06 +GPIO_08,GPIO_08 +GPIO_09,GPIO_09 +GPIO_10,GPIO_10 +GPIO_11,GPIO_11 +GPIO_AD_01,GPIO_AD_01 +GPIO_AD_02,GPIO_AD_02 +GPIO_AD_03,GPIO_AD_03 +GPIO_AD_04,GPIO_AD_04 +GPIO_AD_05,GPIO_AD_05 +GPIO_AD_06,GPIO_AD_06 +GPIO_AD_07,GPIO_AD_07 +GPIO_AD_09,GPIO_AD_09 +GPIO_AD_10,GPIO_AD_10 +GPIO_AD_14,GPIO_AD_14 +GPIO_SD_02,GPIO_SD_02 diff --git a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h deleted file mode 100644 index 99534932a19d5..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1010_EVK/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_11; diff --git a/ports/mimxrt/boards/MIMXRT1011.ld b/ports/mimxrt/boards/MIMXRT1011.ld index 0512c48a7f2d9..9db30adee8a46 100644 --- a/ports/mimxrt/boards/MIMXRT1011.ld +++ b/ports/mimxrt/boards/MIMXRT1011.ld @@ -1,5 +1,5 @@ /* 24kiB stack. */ -__stack_size__ = 0x6000; +__stack_size__ = 0x4000; _estack = __StackTop; _sstack = __StackLimit; diff --git a/ports/mimxrt/boards/MIMXRT1011_af.csv b/ports/mimxrt/boards/MIMXRT1011_af.csv new file mode 100644 index 0000000000000..11dd08546332b --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1011_af.csv @@ -0,0 +1,44 @@ +Pad,ALT0, ALT1, ALT2, ALT3, ALT4, ALT5, ALT6, ALT7, ALT8, ALT9,ADC,ACMP,Default +GPIO_00,FLEXSPI_B_DQS,SAI3_MCLK,LPSPI2_PCS3,LPSPI1_PCS3,PIT_TRIGGER0,GPIO1_IO00,,,,,,,ALT5 +GPIO_01,SAI1_RX_BCLK,WDOG1_ANY,FLEXPWM1_PWM0_B,LPI2C1_SDA,KPP_ROW3,GPIO1_IO01,,,,,,,ALT5 +GPIO_02,SAI1_RX_SYNC,WDOG2_B,FLEXPWM1_PWM0_A,LPI2C1_SCL,KPP_COL3,GPIO1_IO02,,,,,,,ALT5 +GPIO_03,SAI1_RX_DATA0,GPT1_COMPARE3,FLEXPWM1_PWM1_B,,SPDIF_SR_CLK,GPIO1_IO03,,,,,,,ALT5 +GPIO_04,SAI1_TX_DATA0,GPT1_CAPTURE2,FLEXPWM1_PWM1_A,,SPDIF_IN,GPIO1_IO04,,,,,,,ALT5 +GPIO_05,SAI1_TX_DATA1,GPT1_COMPARE2,FLEXPWM1_PWM2_B,LPUART4_RXD,SPDIF_OUT,GPIO1_IO05,,,,,,,ALT0 +GPIO_06,SAI1_TX_BCLK,GPT1_CAPTURE1,FLEXPWM1_PWM2_A,LPUART4_TXD,SPDIF_EXT_CLK,GPIO1_IO06,,,,,,,ALT5 +GPIO_07,SAI1_TX_SYNC,GPT1_COMPARE1,FLEXPWM1_PWM3_B,LPUART3_RXD,SPDIF_LOCK,GPIO1_IO07,LPUART1_RTS_B,,,,,,ALT5 +GPIO_08,SAI1_MCLK,GPT1_CLK,FLEXPWM1_PWM3_A,LPUART3_TXD,FLEXIO1_IO00,GPIO1_IO08,LPUART1_CTS_B,,,,,,ALT5 +GPIO_09,LPUART1_RXD,WDOG1_B,FLEXSPI_A_SS1_B,LPI2C2_SDA,FLEXIO1_IO01,GPIO1_IO09,SPDIF_SR_CLK,,,,,,ALT5 +GPIO_10,LPUART1_TXD,LPI2C1_HREQ,EWM_OUT_B,LPI2C2_SCL,FLEXIO1_IO02,GPIO1_IO10,SPDIF_IN,,,,,,ALT5 +GPIO_11,LPUART3_RXD,LPI2C1_SDA,KPP_ROW0,FLEXSPI_B_SS1_B,FLEXIO1_IO03,GPIO1_IO11,SPDIF_OUT,ARM_TRACE3,,,,,ALT5 +GPIO_12,LPUART3_TXD,LPI2C1_SCL,KPP_COL0,USB_OTG1_OC,FLEXIO1_IO04,GPIO1_IO12,SPDIF_EXT_CLK,ARM_TRACE2,,,,,ALT5 +GPIO_13,LPUART2_RXD,LPSPI2_PCS2,KPP_ROW3,USB_OTG1_ID,FLEXIO1_IO05,GPIO1_IO13,SPDIF_LOCK,ARM_TRACE1,,,,,ALT5 +GPIO_AD_00,LPUART2_TXD,LPSPI1_PCS2,KPP_COL3,USB_OTG1_PWR,FLEXIO1_IO20,GPIO1_IO14,ARM_NMI,ARM_TRACE0,,,ADC1_IN0,,ALT5 +GPIO_AD_01,LPUART4_RXD,LPSPI2_PCS1,WDOG1_ANY,LPI2C2_SDA,MQS_LEFT,GPIO1_IO15,USB_OTG1_OC,ARM_TRACE_SWO,,,ADC1_IN1,,ALT5 +GPIO_AD_02,LPUART4_TXD,LPSPI1_PCS1,WDOG2_B,LPI2C2_SCL,MQS_RIGHT,GPIO1_IO16,,ARM_TRACE_CLK,,,ADC1_IN2,,ALT5 +GPIO_AD_03,LPSPI1_SDI,PIT_TRIGGER3,FLEXPWM1_PWM2_B,KPP_ROW2,GPT2_CLK,GPIO1_IO17,SNVS_VIO_5_B,JTAG_DE_B,,,ADC1_IN3,,ALT5 +GPIO_AD_04,LPSPI1_SDO,PIT_TRIGGER2,FLEXPWM1_PWM2_A,KPP_COL2,GPT2_COMPARE1,GPIO1_IO18,SNVS_VIO_5_CTL,,,,ADC1_IN4,,ALT5 +GPIO_AD_05,LPSPI1_PCS0,PIT_TRIGGER1,FLEXPWM1_PWM3_B,KPP_ROW1,GPT2_CAPTURE1,GPIO1_IO19,,,,,ADC1_IN5,,ALT5 +GPIO_AD_06,LPSPI1_SCK,PIT_TRIGGER0,FLEXPWM1_PWM3_A,KPP_COL1,GPT2_COMPARE2,GPIO1_IO20,LPI2C1_HREQ,,,,ADC1_IN6,,ALT5 +GPIO_AD_07,LPI2C2_SDA,LPUART3_RXD,ARM_CM7_RXEV,LPUART2_RTS_B,GPT2_CAPTURE2,GPIO1_IO21,OCOTP_FUSE_LATCHED,XBAR1_INOUT03,,,ADC1_IN7,,ALT5 +GPIO_AD_08,LPI2C2_SCL,LPUART3_TXD,ARM_CM7_TXEV,LPUART2_CTS_B,GPT2_COMPARE3,GPIO1_IO22,EWM_OUT_B,JTAG_TRSTB,,,ADC1_IN8,,ALT7 +GPIO_AD_09,LPSPI2_SDI,FLEXPWM1_PWM3_X,KPP_ROW2,ARM_TRACE_SWO,FLEXIO1_IO21,GPIO1_IO23,REF_CLK_32K,JTAG_TDO,,,ADC1_IN9,,ALT7 +GPIO_AD_10,LPSPI2_SDO,FLEXPWM1_PWM2_X,KPP_COL2,PIT_TRIGGER3,FLEXIO1_IO22,GPIO1_IO24,USB_OTG1_ID,JTAG_TDI,,,ADC1_IN10,,ALT7 +GPIO_AD_11,LPSPI2_PCS0,FLEXPWM1_PWM1_X,KPP_ROW1,PIT_TRIGGER2,FLEXIO1_IO23,GPIO1_IO25,WDOG1_B,JTAG_MOD,,,ADC1_IN11,,ALT7 +GPIO_AD_12,LPSPI2_SCK,FLEXPWM1_PWM0_X,KPP_COL1,PIT_TRIGGER1,FLEXIO1_IO24,GPIO1_IO26,USB_OTG1_PWR,JTAG_TCK,,,ADC1_IN12,,ALT7 +GPIO_AD_13,LPI2C1_SDA,LPUART3_RTS_B,KPP_ROW0,LPUART4_RTS_B,FLEXIO1_IO25,GPIO1_IO27,ARM_NMI,JTAG_TMS,,,ADC1_IN13,,ALT7 +GPIO_AD_14,LPI2C1_SCL,LPUART3_CTS_B,KPP_COL0,LPUART4_CTS_B,FLEXIO1_IO26,GPIO1_IO28,REF_CLK_24M,XBAR1_INOUT02,,,ADC1_IN14,,ALT5 +GPIO_SD_00,FLEXSPI_B_SS0_B,SAI3_TX_SYNC,ARM_CM7_RXEV,CCM_STOP,FLEXIO1_IO06,GPIO2_IO00,SRC_BT_CFG2,,,,,,ALT5 +GPIO_SD_01,FLEXSPI_B_DATA1,SAI3_TX_BCLK,FLEXPWM1_PWM0_B,CCM_CLKO2,FLEXIO1_IO07,GPIO2_IO01,SRC_BT_CFG1,,,,,,ALT5 +GPIO_SD_02,FLEXSPI_B_DATA2,SAI3_TX_DATA,FLEXPWM1_PWM0_A,CCM_CLKO1,FLEXIO1_IO08,GPIO2_IO02,SRC_BT_CFG0,,,,,,ALT5 +GPIO_SD_03,FLEXSPI_B_DATA0,SAI3_RX_DATA,FLEXPWM1_PWM1_B,CCM_REF_EN_B,FLEXIO1_IO09,GPIO2_IO03,SRC_BOOT_MODE1,,,,,,ALT6 +GPIO_SD_04,FLEXSPI_B_DATA3,SAI3_RX_SYNC,FLEXPWM1_PWM1_A,CCM_WAIT,FLEXIO1_IO10,GPIO2_IO04,SRC_BOOT_MODE0,,,,,,ALT6 +GPIO_SD_05,FLEXSPI_A_SS1_B,LPI2C1_SDA,LPSPI1_SDI,,FLEXIO1_IO11,GPIO2_IO05,,,,,,,ALT5 +GPIO_SD_06,FLEXSPI_A_SS0_B,LPI2C1_SCL,LPSPI1_SDO,,FLEXIO1_IO12,GPIO2_IO06,,,,,,,ALT5 +GPIO_SD_07,FLEXSPI_A_DATA1,LPI2C2_SDA,LPSPI1_PCS0,,FLEXIO1_IO13,GPIO2_IO07,,,,,,,ALT5 +GPIO_SD_08,FLEXSPI_A_DATA2,LPI2C2_SCL,LPSPI1_SCK,,FLEXIO1_IO14,GPIO2_IO08,,,,,,,ALT5 +GPIO_SD_09,FLEXSPI_A_DATA0,LPSPI2_SDI,LPUART2_RXD,,FLEXIO1_IO15,GPIO2_IO09,,,,,,,ALT5 +GPIO_SD_10,FLEXSPI_A_SCLK,LPSPI2_SDO,LPUART2_TXD,,FLEXIO1_IO16,GPIO2_IO10,,,,,,,ALT5 +GPIO_SD_11,FLEXSPI_A_DATA3,LPSPI2_SCK,LPUART1_RXD,,FLEXIO1_IO17,GPIO2_IO11,WDOG1_RST_B_DEB,,,,,,ALT5 +GPIO_SD_12,FLEXSPI_A_DQS,LPSPI2_PCS0,LPUART1_TXD,,FLEXIO1_IO18,GPIO2_IO12,WDOG2_RST_B_DEB,,,,,,ALT5 +GPIO_SD_13,FLEXSPI_B_SCLK,SAI3_RX_BCLK,ARM_CM7_TXEV,CCM_PMIC_RDY,FLEXIO1_IO19,GPIO2_IO13,SRC_BT_CFG3,,,,,,ALT5 diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h index 8598f76bf1833..d8c0f00e276e8 100644 --- a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h @@ -4,6 +4,7 @@ #define BOARD_FLASH_SIZE (8 * 1024 * 1024) // i.MX RT1020 EVK has 1 board LED -#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_05) +// Todo: think about replacing the define with searching in the generated pins? +#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_05) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk index f3b1689524e29..0f5bb967af318 100644 --- a/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk @@ -2,6 +2,24 @@ MCU_SERIES = MIMXRT1021 MCU_VARIANT = MIMXRT1021DAG5A JLINK_PATH ?= /media/RT1020-EVK/ +JLINK_COMMANDER_SCRIPT = $(BUILD)/script.jlink + + +ifdef JLINK_IP +JLINK_CONNECTION_SETTINGS = -IP $(JLINK_IP) +else +JLINK_CONNECTION_SETTINGS = +endif + + +deploy_jlink: $(BUILD)/firmware.hex + $(ECHO) "ExitOnError 1" > $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "speed auto" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "r" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "st" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "loadfile \"$(realpath $(BUILD)/firmware.hex)\"" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "qc" >> $(JLINK_COMMANDER_SCRIPT) + $(JLINK_PATH)JLinkExe -device $(MCU_VARIANT) -if SWD $(JLINK_CONNECTION_SETTINGS) -CommanderScript $(JLINK_COMMANDER_SCRIPT) deploy: $(BUILD)/firmware.bin cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c deleted file mode 100644 index 946b6efca8b27..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_AD_B0_05_af[] = { - PIN_AF(GPIO1_IO05, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), -}; - -pin_obj_t GPIO_AD_B0_05 = PIN(GPIO_AD_B0_05, GPIO1, 5, GPIO_AD_B0_05_af); diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.csv b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.csv new file mode 100644 index 0000000000000..8420bbd82156b --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.csv @@ -0,0 +1,33 @@ +D0,GPIO_AD_B1_09 +D1,GPIO_AD_B1_08 +D2,GPIO_AD_B0_09 +D3,GPIO_AD_B0_07 +D4,GPIO_AD_B0_05 +D5,GPIO_AD_B0_06 +D6,GPIO_AD_B0_14 +D7,GPIO_AD_B1_06 +D8,GPIO_AD_B1_07 +D9,GPIO_AD_B0_15 +D10,GPIO_AD_B0_11 +D11,GPIO_AD_B0_12 +D12,GPIO_AD_B0_13 +D13,GPIO_AD_B0_10 +D14,GPIO_SD_B1_03 +D15,GPIO_SD_B1_02 +A0,GPIO_AD_B1_10 +A1,GPIO_AD_B1_11 +A2,GPIO_AD_B1_12 +A3,GPIO_AD_B1_13 +A4,GPIO_AD_B1_15 +A5,GPIO_AD_B1_14 +RX,GPIO_AD_B1_09 +TX,GPIO_AD_B1_08 +SDA,GPIO_AD_B1_15 +SCL,GPIO_AD_B1_14 +I2C_SCL,GPIO_SD_B1_02 +I2C_SDA,GPIO_SD_B1_03 +SCK,GPIO_AD_B0_10 +SDI,GPIO_AD_B0_13 +SDO,GPIO_AD_B0_12 +CS,GPIO_AD_B0_11 +LED_GREEN,GPIO_AD_B0_05 \ No newline at end of file diff --git a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h deleted file mode 100644 index 158929911c7a5..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1020_EVK/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_AD_B0_05; diff --git a/ports/mimxrt/boards/MIMXRT1021_af.csv b/ports/mimxrt/boards/MIMXRT1021_af.csv new file mode 100644 index 0000000000000..668b415f0ba7a --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1021_af.csv @@ -0,0 +1,94 @@ +Pad,ALT0, ALT1, ALT2, ALT3, ALT4, ALT5, ALT6, ALT7, ALT8, ALT9,ADC,ACMP,Default +GPIO_AD_B0_00,JTAG_TMS,,,,,GPIO1_IO00,USBPHY1_TSTI_TX_EN,GPT1_COMPARE1,,,,,ALT0 +GPIO_AD_B0_01,JTAG_TCK,,,,,GPIO1_IO01,USBPHY1_TSTI_TX_HIZ,GPT1_CAPTURE2,,,,,ALT0 +GPIO_AD_B0_02,JTAG_MOD,,,,,GPIO1_IO02,USBPHY1_TSTI_TX_LS_MODE,GPT1_CAPTURE1,,,,,ALT0 +GPIO_AD_B0_03,JTAG_TDI,USDHC2_CD_B,WDOG1_B,SAI1_MCLK,USDHC1_WP,GPIO1_IO03,USB_OTG1_OC,CCM_PMIC_RDY,,,,,ALT0 +GPIO_AD_B0_04,JTAG_TDO,FLEXCAN1_TX,USDHC1_WP,TMR2_TIMER0,ENET_MDIO,GPIO1_IO04,USB_OTG1_PWR,EWM_OUT_B,,,,,ALT0 +GPIO_AD_B0_05,JTAG_TRSTB,FLEXCAN1_RX,USDHC1_CD_B,TMR2_TIMER1,ENET_MDC,GPIO1_IO05,USB_OTG1_ID,ARM_NMI,,,,,ALT0 +GPIO_AD_B0_06,PIT_TRIGGER0,MQS_RIGHT,LPUART1_TXD,TMR2_TIMER2,FLEXPWM2_PWMA3,GPIO1_IO06,REF_32K_OUT,,,,,,ALT5 +GPIO_AD_B0_07,PIT_TRIGGER1,MQS_LEFT,LPUART1_RXD,TMR2_TIMER3,FLEXPWM2_PWMB3,GPIO1_IO07,REF_24M_OUT,,,,,,ALT5 +GPIO_AD_B0_08,ENET_TX_CLK,LPI2C3_SCL,LPUART1_CTS_B,KPP_COL0,ENET_REF_CLK,GPIO1_IO08,ARM_CM7_TXEV,,,,,ACMP1_IN4,ALT5 +GPIO_AD_B0_09,ENET_RX_DATA1,LPI2C3_SDA,LPUART1_RTS_B,KPP_ROW0,,GPIO1_IO09,ARM_CM7_RXEV,,,,,ACMP2_IN4,ALT5 +GPIO_AD_B0_10,ENET_RX_DATA0,LPSPI1_SCK,LPUART5_TXD,KPP_COL1,FLEXPWM2_PWMA2,GPIO1_IO10,ARM_TRACE_CLK,,,,,ACMP3_IN4,ALT5 +GPIO_AD_B0_11,ENET_RX_EN,LPSPI1_PCS0,LPUART5_RXD,KPP_ROW1,FLEXPWM2_PWMB2,GPIO1_IO11,ARM_TRACE_SWO,,,,,ACMP4_IN4,ALT5 +GPIO_AD_B0_12,ENET_RX_ER,LPSPI1_SDO,LPUART3_CTS_B,KPP_COL2,FLEXPWM2_PWMA1,GPIO1_IO12,ARM_TRACE0,SNVS_VIO_5_CTL,,,ADC1_IN0,,ALT5 +GPIO_AD_B0_13,ENET_TX_EN,LPSPI1_SDI,LPUART3_RTS_B,KPP_ROW2,FLEXPWM2_PWMB1,GPIO1_IO13,,SNVS_VIO_5_B,,,ADC2_IN0,,ALT5 +GPIO_AD_B0_14,ENET_TX_DATA0,FLEXCAN2_TX,LPUART3_TXD,KPP_COL3,FLEXPWM2_PWMA0,GPIO1_IO14,,WDOG1_ANY,,,"ADC1_IN1,ADC2_IN1","ACMP1_IN0,ACMP2_IN0,ACMP3_IN0,ACMP4_IN0",ALT5 +GPIO_AD_B0_15,ENET_TX_DATA1,FLEXCAN2_RX,LPUART3_RXD,KPP_ROW3,FLEXPWM2_PWMB0,GPIO1_IO15,,,,,"ADC1_IN2,ADC2_IN2","ACMP1_IN1,ACMP2_IN1,ACMP3_IN1,ACMP4_IN1",ALT5 +GPIO_AD_B1_00,SEMC_RDY,FLEXSPI_A_DATA3,FLEXCAN2_TX,SAI1_MCLK,FLEXIO1_D15,GPIO1_IO16,ENET_1588_EVENT2_OUT,KPP_COL4,,,,ACMP1_IN2,ALT5 +GPIO_AD_B1_01,SEMC_CSX0,FLEXSPI_A_SCLK,FLEXCAN2_RX,SAI1_TX_BCLK,FLEXIO1_D14,GPIO1_IO17,ENET_1588_EVENT2_IN,KPP_ROW4,,,ADC1_IN3,ACMP2_IN2,ALT5 +GPIO_AD_B1_02,SEMC_CSX1,FLEXSPI_A_DATA0,LPSPI4_SCK,SAI1_TX_SYNC,FLEXIO1_D13,GPIO1_IO18,ENET_1588_EVENT3_OUT,KPP_COL5,,,ADC2_IN3,ACMP3_IN2,ALT5 +GPIO_AD_B1_03,SEMC_CSX2,FLEXSPI_A_DATA2,LPSPI4_PCS0,SAI1_TX_DATA0,FLEXIO1_D12,GPIO1_IO19,ENET_1588_EVENT3_IN,KPP_ROW5,,,ADC1_IN4,ACMP4_IN2,ALT5 +GPIO_AD_B1_04,SEMC_CSX3,FLEXSPI_A_DATA1,LPSPI4_SDO,SAI1_RX_SYNC,FLEXIO1_D11,GPIO1_IO20,LPSPI1_PCS1,KPP_COL6,,,ADC2_IN4,ACMP1_IN3,ALT5 +GPIO_AD_B1_05,USDHC1_WP,FLEXSPI_A_SS0_B,LPSPI4_SDI,SAI1_RX_DATA0,FLEXIO1_D10,GPIO1_IO21,LPSPI1_PCS2,KPP_ROW6,,,"ADC1_IN5,ADC2_IN5",ACMP2_IN3,ALT5 +GPIO_AD_B1_06,USDHC1_RESET_B,FLEXPWM1_PWMA0,LPUART2_CTS_B,SAI1_RX_BCLK,FLEXIO1_D09,GPIO1_IO22,LPSPI1_PCS3,KPP_COL7,,,"ADC1_IN6,ADC2_IN6",ACMP3_IN3,ALT5 +GPIO_AD_B1_07,USDHC1_VSELECT,FLEXPWM1_PWMB0,LPUART2_RTS_B,SAI1_TX_DATA1,FLEXIO1_D08,GPIO1_IO23,LPSPI3_PCS3,KPP_ROW7,,,"ADC1_IN7,ADC2_IN7",ACMP4_IN3,ALT5 +GPIO_AD_B1_08,LPI2C2_SCL,FLEXPWM1_PWMA1,LPUART2_TXD,SAI1_TX_DATA2,FLEXIO1_D07,GPIO1_IO24,LPSPI3_PCS2,XBAR_INOUT12,,,"ADC1_IN8,ADC2_IN8",ACMP1_IN5,ALT5 +GPIO_AD_B1_09,LPI2C2_SDA,FLEXPWM1_PWMB1,LPUART2_RXD,SAI1_TX_DATA3,FLEXIO1_D06,GPIO1_IO25,LPSPI3_PCS1,XBAR_INOUT13,,,"ADC1_IN9,ADC2_IN9",ACMP2_IN5,ALT5 +GPIO_AD_B1_10,USB_OTG1_PWR,FLEXPWM1_PWMA2,LPUART4_TXD,USDHC1_CD_B,FLEXIO1_D05,GPIO1_IO26,GPT2_CAPTURE1,,,,"ADC1_IN10,ADC2_IN10",ACMP3_IN5,ALT5 +GPIO_AD_B1_11,USB_OTG1_ID,FLEXPWM1_PWMB2,LPUART4_RXD,USDHC1_WP,FLEXIO1_D04,GPIO1_IO27,GPT2_COMPARE1,,,,"ADC1_IN11,ADC2_IN11",ACMP4_IN5,ALT5 +GPIO_AD_B1_12,USB_OTG1_OC,ACMP1_OUT,LPSPI3_SCK,USDHC2_CD_B,FLEXIO1_D03,GPIO1_IO28,FLEXPWM1_PWMA3,,,,"ADC1_IN12,ADC2_IN12","ACMP1_IN6,ACMP1_OUT",ALT5 +GPIO_AD_B1_13,LPI2C1_HREQ,ACMP2_OUT,LPSPI3_PCS0,USDHC2_WP,FLEXIO1_D02,GPIO1_IO29,FLEXPWM1_PWMB3,,,,"ADC1_IN13,ADC2_IN13","ACMP2_IN6,ACMP2_OUT",ALT5 +GPIO_AD_B1_14,LPI2C1_SCL,ACMP3_OUT,LPSPI3_SDO,ENET_1588_EVENT0_OUT,FLEXIO1_D01,GPIO1_IO30,,,,,"ADC1_IN14,ADC2_IN14","ACMP3_IN6,ACMP3_OUT",ALT5 +GPIO_AD_B1_15,LPI2C1_SDA,ACMP4_OUT,LPSPI3_SDI,ENET_1588_EVENT0_IN,FLEXIO1_D00,GPIO1_IO31,,,,,"ADC1_IN15,ADC2_IN15","ACMP4_IN6,ACMP4_OUT",ALT5 +GPIO_EMC_00,SEMC_DA00,TMR2_TIMER0,LPUART4_CTS_B,SPDIF_SR_CLK,LPSPI2_SCK,GPIO2_IO00,FLEXCAN1_TX,PIT_TRIGGER2,,,,,ALT5 +GPIO_EMC_01,SEMC_DA01,TMR2_TIMER1,LPUART4_RTS_B,SPDIF_OUT,LPSPI2_PCS0,GPIO2_IO01,FLEXCAN1_RX,PIT_TRIGGER3,,,,,ALT5 +GPIO_EMC_02,SEMC_DA02,TMR2_TIMER2,LPUART4_TXD,SPDIF_LOCK,LPSPI2_SDO,GPIO2_IO02,LPI2C1_SCL,,,,,,ALT5 +GPIO_EMC_03,SEMC_DA03,TMR2_TIMER3,LPUART4_RXD,SPDIF_EXT_CLK,LPSPI2_SDI,GPIO2_IO03,LPI2C1_SDA,,,,,,ALT5 +GPIO_EMC_04,SEMC_DA04,XBAR_INOUT04,SPDIF_OUT,SAI2_TX_BCLK,FLEXIO1_D16,GPIO2_IO04,USBPHY1_TSTO_PLL_CLK20DIV,,,,,,ALT5 +GPIO_EMC_05,SEMC_DA05,XBAR_INOUT05,SPDIF_IN,SAI2_TX_SYNC,FLEXIO1_D17,GPIO2_IO05,USBPHY1_TSTI_TX_HS_MODE,,,,,,ALT5 +GPIO_EMC_06,SEMC_DA06,XBAR_INOUT06,LPUART3_TXD,SAI2_TX_DATA,FLEXIO1_D18,GPIO2_IO06,USBPHY1_TSTI_TX_DN,,,,,,ALT5 +GPIO_EMC_07,SEMC_DA07,XBAR_INOUT07,LPUART3_RXD,SAI2_RX_SYNC,FLEXIO1_D19,GPIO2_IO07,USBPHY1_TSTO_RX_SQUELCH,,,,,,ALT5 +GPIO_EMC_08,SEMC_DM0,XBAR_INOUT08,FLEXCAN2_TX,SAI2_RX_DATA,FLEXIO1_D20,GPIO2_IO08,USBPHY1_TSTO_RX_DISCON_DET,,,,,,ALT5 +GPIO_EMC_09,SEMC_WE,XBAR_INOUT09,FLEXCAN2_RX,SAI2_RX_BCLK,FLEXIO1_D21,GPIO2_IO09,USBPHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_10,SEMC_CAS,XBAR_INOUT10,LPI2C4_SDA,SAI1_TX_SYNC,LPSPI2_SCK,GPIO2_IO10,FLEXPWM2_PWMX0,,,,,,ALT5 +GPIO_EMC_11,SEMC_RAS,XBAR_INOUT11,LPI2C4_SCL,SAI1_TX_BCLK,LPSPI2_PCS0,GPIO2_IO11,FLEXPWM2_PWMX1,,,,,,ALT5 +GPIO_EMC_12,SEMC_CS0,XBAR_INOUT12,LPUART6_TXD,SAI1_TX_DATA0,LPSPI2_SDO,GPIO2_IO12,FLEXPWM2_PWMX2,,,,,,ALT5 +GPIO_EMC_13,SEMC_BA0,XBAR_INOUT13,LPUART6_RXD,SAI1_RX_DATA0,LPSPI2_SDI,GPIO2_IO13,FLEXPWM2_PWMX3,CCM_PMIC_RDY,,,,,ALT5 +GPIO_EMC_14,SEMC_BA1,XBAR_INOUT14,LPUART6_CTS_B,SAI1_RX_BCLK,LPSPI2_PCS1,GPIO2_IO14,FLEXCAN1_TX,,,,,,ALT5 +GPIO_EMC_15,SEMC_ADDR10,XBAR_INOUT15,LPUART6_RTS_B,SAI1_RX_SYNC,WDOG1_B,GPIO2_IO15,FLEXCAN1_RX,,,,,,ALT5 +GPIO_EMC_16,SEMC_ADDR00,,MQS_RIGHT,SAI2_MCLK,,GPIO2_IO16,SRC_BOOT_MODE0,,,,,,ALT5 +GPIO_EMC_17,SEMC_ADDR01,,MQS_LEFT,SAI3_MCLK,,GPIO2_IO17,SRC_BOOT_MODE1,,,,,,ALT5 +GPIO_EMC_18,SEMC_ADDR02,XBAR_INOUT16,LPI2C2_SDA,SAI1_RX_SYNC,FLEXIO1_D22,GPIO2_IO18,SRC_BT_CFG0,,,,,,ALT5 +GPIO_EMC_19,SEMC_ADDR03,XBAR_INOUT17,LPI2C2_SCL,SAI1_RX_BCLK,FLEXIO1_D23,GPIO2_IO19,SRC_BT_CFG1,,,,,,ALT5 +GPIO_EMC_20,SEMC_ADDR04,FLEXPWM1_PWMA3,LPUART2_CTS_B,SAI1_MCLK,FLEXIO1_D24,GPIO2_IO20,SRC_BT_CFG2,,,,,,ALT5 +GPIO_EMC_21,SEMC_ADDR05,FLEXPWM1_PWMB3,LPUART2_RTS_B,SAI1_RX_DATA0,FLEXIO1_D25,GPIO2_IO21,SRC_BT_CFG3,,,,,,ALT5 +GPIO_EMC_22,SEMC_ADDR06,FLEXPWM1_PWMA2,LPUART2_TXD,SAI1_TX_DATA3,FLEXIO1_D26,GPIO2_IO22,SRC_BT_CFG4,,,,,,ALT5 +GPIO_EMC_23,SEMC_ADDR07,FLEXPWM1_PWMB2,LPUART2_RXD,SAI1_TX_DATA2,FLEXIO1_D27,GPIO2_IO23,SRC_BT_CFG5,,,,,,ALT5 +GPIO_EMC_24,SEMC_ADDR08,FLEXPWM1_PWMA1,LPUART8_CTS_B,SAI1_TX_DATA1,FLEXIO1_D28,GPIO2_IO24,SRC_BT_CFG6,,,,,,ALT5 +GPIO_EMC_25,SEMC_ADDR09,FLEXPWM1_PWMB1,LPUART8_RTS_B,SAI1_TX_DATA0,FLEXIO1_D29,GPIO2_IO25,SRC_BT_CFG7,,,,,,ALT5 +GPIO_EMC_26,SEMC_ADDR11,FLEXPWM1_PWMA0,LPUART8_TXD,SAI1_TX_BCLK,FLEXIO1_D30,GPIO2_IO26,SRC_BT_CFG8,,,,,,ALT5 +GPIO_EMC_27,SEMC_ADDR12,FLEXPWM1_PWMB0,LPUART8_RXD,SAI1_TX_SYNC,FLEXIO1_D31,GPIO2_IO27,SRC_BT_CFG9,,,,,,ALT5 +GPIO_EMC_28,SEMC_DQS,FLEXPWM2_PWMA3,XBAR_INOUT18,SAI3_MCLK,EWM_OUT_B,GPIO2_IO28,GPT2_CAPTURE2,FLEXPWM1_PWMX0,,,,,ALT5 +GPIO_EMC_29,SEMC_CKE,FLEXPWM2_PWMB3,XBAR_INOUT19,SAI3_RX_BCLK,WDOG2_RST_B_DEB,GPIO2_IO29,GPT2_COMPARE2,FLEXPWM1_PWMX1,,,,,ALT5 +GPIO_EMC_30,SEMC_CLK,FLEXPWM2_PWMA2,LPUART4_CTS_B,SAI3_RX_SYNC,WDOG1_RST_B_DEB,GPIO2_IO30,GPT2_COMPARE3,FLEXPWM1_PWMX2,,,,,ALT5 +GPIO_EMC_31,SEMC_DM1,FLEXPWM2_PWMB2,LPUART4_RTS_B,SAI3_RX_DATA,WDOG2_B,GPIO2_IO31,GPT2_CLK,FLEXPWM1_PWMX3,,,,,ALT5 +GPIO_EMC_32,SEMC_DA08,TMR1_TIMER0,LPUART4_TXD,SAI3_TX_DATA,LPSPI4_SCK,GPIO3_IO00,USBPHY1_TSTO_RX_FS_RXD,REF_24M_OUT,,,,,ALT5 +GPIO_EMC_33,SEMC_DA09,TMR1_TIMER1,LPUART4_RXD,SAI3_TX_BCLK,LPSPI4_PCS0,GPIO3_IO01,USBPHY1_TSTI_TX_DP,SRC_TESTER_ACK,,,,,ALT5 +GPIO_EMC_34,SEMC_DA10,TMR1_TIMER2,LPUART7_TXD,SAI3_TX_SYNC,LPSPI4_SDO,GPIO3_IO02,ENET_CRS,,,,,,ALT5 +GPIO_EMC_35,SEMC_DA11,TMR1_TIMER3,LPUART7_RXD,USDHC2_WP,LPSPI4_SDI,GPIO3_IO03,ENET_COL,,,,,,ALT5 +GPIO_EMC_36,SEMC_DA12,FLEXPWM2_PWMA1,LPUART5_CTS_B,CCM_PMIC_RDY,LPSPI4_PCS1,GPIO3_IO04,ENET_RX_CLK,USDHC1_WP,,,,,ALT5 +GPIO_EMC_37,SEMC_DA13,FLEXPWM2_PWMB1,LPUART5_RTS_B,MQS_RIGHT,LPSPI4_PCS2,GPIO3_IO05,ENET_RX_DATA3,USDHC1_VSELECT,,,,,ALT5 +GPIO_EMC_38,SEMC_DA14,FLEXPWM2_PWMA0,LPUART5_TXD,MQS_LEFT,LPSPI4_PCS3,GPIO3_IO06,ENET_RX_DATA2,USDHC1_CD_B,,,,,ALT5 +GPIO_EMC_39,SEMC_DA15,FLEXPWM2_PWMB0,LPUART5_RXD,USB_OTG1_OC,WDOG1_B,GPIO3_IO07,ENET_TX_ER,GPT1_CLK,,,,,ALT5 +GPIO_EMC_40,SEMC_CSX0,XBAR_INOUT18,SPDIF_OUT,USB_OTG1_ID,ENET_MDIO,GPIO3_IO08,ENET_TX_DATA3,GPT1_COMPARE3,,,,,ALT5 +GPIO_EMC_41,SEMC_RDY,XBAR_INOUT19,SPDIF_IN,USB_OTG1_PWR,ENET_MDC,GPIO3_IO09,ENET_TX_DATA2,GPT1_COMPARE2,,,,,ALT5 +GPIO_SD_B0_00,USDHC1_DATA2,TMR1_TIMER0,SAI1_MCLK,SAI2_MCLK,LPI2C3_SCL,GPIO3_IO13,FLEXSPI_A_SS1_B,XBAR_INOUT14,,,,,ALT5 +GPIO_SD_B0_01,USDHC1_DATA3,TMR1_TIMER1,REF_24M_OUT,SAI2_RX_SYNC,LPI2C3_SDA,GPIO3_IO14,FLEXSPI_B_SS1_B,XBAR_INOUT15,,,,,ALT5 +GPIO_SD_B0_02,USDHC1_CMD,TMR1_TIMER2,LPUART7_CTS_B,SAI2_RX_BCLK,LPSPI1_SCK,GPIO3_IO15,ENET_MDIO,XBAR_INOUT16,,,,,ALT5 +GPIO_SD_B0_03,USDHC1_CLK,TMR1_TIMER3,LPUART7_RTS_B,SAI2_RX_DATA,LPSPI1_PCS0,GPIO3_IO16,ENET_MDC,,,,,,ALT5 +GPIO_SD_B0_04,USDHC1_DATA0,FLEXCAN2_TX,LPUART7_TXD,SAI2_TX_DATA,LPSPI1_SDO,GPIO3_IO17,FLEXSPI_B_SS0_B,,,,,,ALT5 +GPIO_SD_B0_05,USDHC1_DATA1,FLEXCAN2_RX,LPUART7_RXD,SAI2_TX_BCLK,LPSPI1_SDI,GPIO3_IO18,FLEXSPI_B_DQS,,,,,,ALT5 +GPIO_SD_B0_06,USDHC1_CD_B,USDHC1_RESET_B,REF_32K_OUT,SAI2_TX_SYNC,WDOG1_B,GPIO3_IO19,XBAR_INOUT17,,,,,,ALT5 +GPIO_SD_B1_00,USDHC2_DATA2,FLEXSPI_B_DATA3,LPUART6_TXD,XBAR_INOUT10,FLEXCAN1_TX,GPIO3_IO20,,,,,,,ALT5 +GPIO_SD_B1_01,USDHC2_DATA3,FLEXSPI_B_SCLK,LPUART6_RXD,FLEXSPI_A_SS1_B,FLEXCAN1_RX,GPIO3_IO21,,,,,,,ALT5 +GPIO_SD_B1_02,USDHC2_CMD,FLEXSPI_B_DATA0,LPUART8_TXD,LPI2C4_SCL,ENET_1588_EVENT1_OUT,GPIO3_IO22,CCM_CLKO1,,,,,,ALT5 +GPIO_SD_B1_03,USDHC2_CLK,FLEXSPI_B_DATA2,LPUART8_RXD,LPI2C4_SDA,ENET_1588_EVENT1_IN,GPIO3_IO23,CCM_CLKO2,,,,,,ALT5 +GPIO_SD_B1_04,USDHC2_DATA0,FLEXSPI_B_DATA1,ENET_TX_CLK,ENET_REF_CLK,EWM_OUT_B,GPIO3_IO24,CCM_WAIT,,,,,,ALT5 +GPIO_SD_B1_05,USDHC2_DATA1,FLEXSPI_A_DQS,ENET_RX_DATA1,SAI3_MCLK,FLEXSPI_B_SS0_B,GPIO3_IO25,CCM_PMIC_RDY,,,,,,ALT5 +GPIO_SD_B1_06,USDHC2_CD_B,FLEXSPI_A_DATA3,ENET_RX_DATA0,SAI3_TX_BCLK,LPSPI2_PCS0,GPIO3_IO26,CCM_STOP,,,,,,ALT5 +GPIO_SD_B1_07,USDHC2_RESET_B,FLEXSPI_A_SCLK,ENET_RX_EN,SAI3_TX_SYNC,LPSPI2_SCK,GPIO3_IO27,,,,,,,ALT5 +GPIO_SD_B1_08,USDHC2_DATA4,FLEXSPI_A_DATA0,ENET_RX_ER,SAI3_TX_DATA,LPSPI2_SDO,GPIO3_IO28,,,,,,,ALT5 +GPIO_SD_B1_09,USDHC2_DATA5,FLEXSPI_A_DATA2,ENET_TX_EN,SAI3_RX_BCLK,LPSPI2_SDI,GPIO3_IO29,CCM_REF_EN_B,,,,,,ALT5 +GPIO_SD_B1_10,USDHC2_DATA6,FLEXSPI_A_DATA1,ENET_TX_DATA0,SAI3_RX_SYNC,LPSPI2_PCS2,GPIO3_IO30,SRC_SYSTEM_RESET,,,,,,ALT5 +GPIO_SD_B1_11,USDHC2_DATA7,FLEXSPI_A_SS0_B,ENET_TX_DATA1,SAI3_RX_DATA,LPSPI2_PCS3,GPIO3_IO31,SRC_EARLY_RESET,,,,,,ALT5 diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h index 5a733bbd97bb2..2337496d2a3be 100644 --- a/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h @@ -4,6 +4,6 @@ #define BOARD_FLASH_SIZE (64 * 1024 * 1024) // MIMXRT1050_EVK has 1 user LED -#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c deleted file mode 100644 index d5da9c6f99275..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_AD_B0_09_af[] = { - PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), -}; - -pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.csv b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.csv new file mode 100644 index 0000000000000..afd63d4f97fe8 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.csv @@ -0,0 +1,51 @@ +D0,GPIO_AD_B1_07 +D1,GPIO_AD_B1_06 +D2,GPIO_AD_B0_11 +D3,GPIO_AD_B1_08 +D4,GPIO_AD_B0_09 +D5,GPIO_AD_B0_10 +D6,GPIO_AD_B1_02 +D7,GPIO_AD_B1_03 +D8,GPIO_AD_B0_03 +D9,GPIO_AD_B0_02 +D10,GPIO_SD_B0_01 +D11,GPIO_SD_B0_02 +D12,GPIO_SD_B0_03 +D13,GPIO_SD_B0_00 +D14,GPIO_AD_B1_01 +D15,GPIO_AD_B1_00 +A0,GPIO_AD_B1_10 +A1,GPIO_AD_B1_11 +A2,GPIO_AD_B1_04 +A3,GPIO_AD_B1_05 +A4,GPIO_AD_B1_01 +A5,GPIO_AD_B1_00 +RX,GPIO_AD_B1_07 +TX,GPIO_AD_B1_06 +SCL,GPIO_AD_B1_00 +SDA,GPIO_AD_B1_01 +SCK,GPIO_SD_B0_00 +SDI,GPIO_SD_B0_03 +SDO,GPIO_SD_B0_02 +CS,GPIO_SD_B0_01 +LED_GREEN,GPIO_AD_B0_09 +GPIO_AD_B1_07,GPIO_AD_B1_07 +GPIO_AD_B1_06,GPIO_AD_B1_06 +GPIO_AD_B0_11,GPIO_AD_B0_11 +GPIO_AD_B1_08,GPIO_AD_B1_08 +GPIO_AD_B0_09,GPIO_AD_B0_09 +GPIO_AD_B0_10,GPIO_AD_B0_10 +GPIO_AD_B1_02,GPIO_AD_B1_02 +GPIO_AD_B1_03,GPIO_AD_B1_03 +GPIO_AD_B0_03,GPIO_AD_B0_03 +GPIO_AD_B0_02,GPIO_AD_B0_02 +GPIO_SD_B0_01,GPIO_SD_B0_01 +GPIO_SD_B0_02,GPIO_SD_B0_02 +GPIO_SD_B0_03,GPIO_SD_B0_03 +GPIO_SD_B0_00,GPIO_SD_B0_00 +GPIO_AD_B1_01,GPIO_AD_B1_01 +GPIO_AD_B1_00,GPIO_AD_B1_00 +GPIO_AD_B1_10,GPIO_AD_B1_10 +GPIO_AD_B1_11,GPIO_AD_B1_11 +GPIO_AD_B1_04,GPIO_AD_B1_04 +GPIO_AD_B1_05,GPIO_AD_B1_05 diff --git a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h deleted file mode 100644 index baef51c6c8612..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1050_EVK/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_AD_B0_09; diff --git a/ports/mimxrt/boards/MIMXRT1052_af.csv b/ports/mimxrt/boards/MIMXRT1052_af.csv new file mode 100644 index 0000000000000..754bf4ab7bf0f --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1052_af.csv @@ -0,0 +1,125 @@ +Pad,ALT0, ALT1, ALT2, ALT3, ALT4, ALT5, ALT6, ALT7, ALT8, ALT9,ADC,ACMP,Default +GPIO_AD_B0_00,FLEXPWM2_PWM3_A,XBAR_INOUT14,REF_CLK_32K,USB_OTG2_ID,LPI2C1_SCLS,GPIO1_IO00,USDHC1_RESET_B,LPSPI3_SCK,,,,ACMP1_IN4,ALT5 +GPIO_AD_B0_01,FLEXPWM2_PWM3_B,XBAR_INOUT15,REF_CLK_24M,USB_OTG1_ID,LPI2C1_SDAS,GPIO1_IO01,EWM_OUT_B,LPSPI3_SOUT,,,,ACMP2_IN4,ALT5 +GPIO_AD_B0_02,FLEXCAN2_TX,XBAR_INOUT16,LPUART6_TXD,USB_OTG1_PWR,FLEXPWM1_PWM0_X,GPIO1_IO02,LPI2C1_HREQ,LPSPI3_SIN,,,,ACMP3_IN4,ALT5 +GPIO_AD_B0_03,FLEXCAN2_RX,XBAR_INOUT17,LPUART6_RXD,USB_OTG1_OC,FLEXPWM1_PWM1_X,GPIO1_IO03,REF_CLK_24M,LPSPI3_PCS0,,,,ACMP4_IN4,ALT5 +GPIO_AD_B0_04,SRC_BOOT_MODE0,MQS_RIGHT,ENET_TX_DATA3,SAI2_TX_SYNC,CSI_DATA09,GPIO1_IO04,PIT_TRIGGER0,LPSPI3_PCS1,,,,,ALT0 +GPIO_AD_B0_05,SRC_BOOT_MODE1,MQS_LEFT,ENET_TX_DATA2,SAI2_TX_BCLK,CSI_DATA08,GPIO1_IO05,XBAR_INOUT17,LPSPI3_PCS2,,,,,ALT0 +GPIO_AD_B0_06,"JTAG_TMS,SWD_DIO",GPT2_COMPARE1,ENET_RX_CLK,SAI2_RX_BCLK,CSI_DATA07,GPIO1_IO06,XBAR_INOUT18,LPSPI3_PCS3,,,,,ALT0 +GPIO_AD_B0_07,"JTAG_TCK,SWD_CLK",GPT2_COMPARE2,ENET_TX_ER,SAI2_RX_SYNC,CSI_DATA06,GPIO1_IO07,XBAR_INOUT19,ENET_1588_EVENT3_OUT,,,,,ALT0 +GPIO_AD_B0_08,JTAG_MOD,GPT2_COMPARE3,ENET_RX_DATA3,SAI2_RX_DATA,CSI_DATA05,GPIO1_IO08,XBAR_INOUT20,ENET_1588_EVENT3_IN,,,,,ALT0 +GPIO_AD_B0_09,JTAG_TDI,FLEXPWM2_PWM3_A,ENET_RX_DATA2,SAI2_TX_DATA,CSI_DATA04,GPIO1_IO09,XBAR_INOUT21,GPT2_CLK,,,,,ALT0 +GPIO_AD_B0_10,JTAG_TDO,FLEXPWM1_PWM3_A,ENET_CRS,SAI2_MCLK,CSI_DATA03,GPIO1_IO10,XBAR_INOUT22,ENET_1588_EVENT0_OUT,,,,,ALT0 +GPIO_AD_B0_11,JTAG_TRSTB,FLEXPWM1_PWM3_B,ENET_COL,WDOG1_B,CSI_DATA02,GPIO1_IO11,XBAR_INOUT23,ENET_1588_EVENT0_IN,,,,,ALT0 +GPIO_AD_B0_12,LPI2C4_SCL,CCM_PMIC_READY,LPUART1_TXD,WDOG2_B,FLEXPWM1_PWM2_X,GPIO1_IO12,ENET_1588_EVENT1_OUT,,,,ADC1_IN1,,ALT5 +GPIO_AD_B0_13,LPI2C4_SDA,GPT1_CLK,LPUART1_RXD,EWM_OUT_B,FLEXPWM1_PWM3_X,GPIO1_IO13,ENET_1588_EVENT1_IN,REF_CLK_24M,,,ADC1_IN2,ACMP1_IN2,ALT5 +GPIO_AD_B0_14,USB_OTG2_OC,XBAR_INOUT24,LPUART1_CTS_B,ENET_1588_EVENT0_OUT,CSI_VSYNC,GPIO1_IO14,FLEXCAN2_TX,WDOG1_ANY,,,ADC1_IN3,ACMP2_IN2,ALT5 +GPIO_AD_B0_15,USB_OTG2_PWR,XBAR_INOUT25,LPUART1_RTS_B,ENET_1588_EVENT0_IN,CSI_HSYNC,GPIO1_IO15,FLEXCAN2_RX,WDOG1_RST_B_DEB,,,ADC1_IN4,ACMP3_IN2,ALT5 +GPIO_AD_B1_00,USB_OTG2_ID,TMR3_TIMER0,LPUART2_CTS_B,LPI2C1_SCL,WDOG1_B,GPIO1_IO16,USDHC1_WP,KPP_ROW7,,,"ADC1_IN5,ADC2_IN5",ACMP4_IN2,ALT5 +GPIO_AD_B1_01,USB_OTG1_PWR,TMR3_TIMER1,LPUART2_RTS_B,LPI2C1_SDA,CCM_PMIC_READY,GPIO1_IO17,USDHC1_VSELECT,KPP_COL7,,,"ADC1_IN6,ADC2_IN6","ACMP1_IN0,ACMP2_IN0,ACMP3_IN0,ACMP4_IN0",ALT5 +GPIO_AD_B1_02,USB_OTG1_ID,TMR3_TIMER2,LPUART2_TXD,SPDIF_OUT,ENET_1588_EVENT2_OUT,GPIO1_IO18,USDHC1_CD_B,KPP_ROW6,,,"ADC1_IN7,ADC2_IN7",ACMP1_IN3,ALT5 +GPIO_AD_B1_03,USB_OTG1_OC,TMR3_TIMER3,LPUART2_RXD,SPDIF_IN,ENET_1588_EVENT2_IN,GPIO1_IO19,USDHC2_CD_B,KPP_COL6,,,"ADC1_IN8,ADC2_IN8",ACMP2_IN3,ALT5 +GPIO_AD_B1_04,FLEXSPI_B_DATA3,ENET_MDC,LPUART3_CTS_B,SPDIF_SR_CLK,CSI_PIXCLK,GPIO1_IO20,USDHC2_DATA0,KPP_ROW5,,,"ADC1_IN9,ADC2_IN9",ACMP3_IN3,ALT5 +GPIO_AD_B1_05,FLEXSPI_B_DATA2,ENET_MDIO,LPUART3_RTS_B,SPDIF_OUT,CSI_MCLK,GPIO1_IO21,USDHC2_DATA1,KPP_COL5,,,"ADC1_IN10,ADC2_IN10",ACMP4_IN3,ALT5 +GPIO_AD_B1_06,FLEXSPI_B_DATA1,LPI2C3_SDA,LPUART3_TXD,SPDIF_LOCK,CSI_VSYNC,GPIO1_IO22,USDHC2_DATA2,KPP_ROW4,,,"ADC1_IN11,ADC2_IN11","ACMP1_IN1,ACMP2_IN1,ACMP3_IN1,ACMP4_IN1",ALT5 +GPIO_AD_B1_07,FLEXSPI_B_DATA0,LPI2C3_SCL,LPUART3_RXD,SPDIF_EXT_CLK,CSI_HSYNC,GPIO1_IO23,USDHC2_DATA3,KPP_COL4,,,"ADC1_IN12,ADC2_IN12",ACMP1_IN5,ALT5 +GPIO_AD_B1_08,FLEXSPI_A_SS1_B,FLEXPWM4_PWM0_A,FLEXCAN1_TX,CCM_PMIC_READY,CSI_DATA09,GPIO1_IO24,USDHC2_CMD,KPP_ROW3,,,"ADC1_IN13,ADC2_IN13",ACMP2_IN5,ALT5 +GPIO_AD_B1_09,FLEXSPI_A_DQS,FLEXPWM4_PWM1_A,FLEXCAN1_RX,SAI1_MCLK,CSI_DATA08,GPIO1_IO25,USDHC2_CLK,KPP_COL3,,,"ADC1_IN14,ADC2_IN14",ACMP3_IN5,ALT5 +GPIO_AD_B1_10,FLEXSPI_A_DATA3,WDOG1_B,LPUART8_TXD,SAI1_RX_SYNC,CSI_DATA07,GPIO1_IO26,USDHC2_WP,KPP_ROW2,,,"ADC1_IN15,ADC2_IN15",ACMP4_IN5,ALT5 +GPIO_AD_B1_11,FLEXSPI_A_DATA2,EWM_OUT_B,LPUART8_RXD,SAI1_RX_BCLK,CSI_DATA06,GPIO1_IO27,USDHC2_RESET_B,KPP_COL2,,,"ADC1_IN0,ADC2_IN0",ACMP1_IN6,ALT5 +GPIO_AD_B1_12,FLEXSPI_A_DATA1,ACMP1_OUT,LPSPI3_PCS0,SAI1_RX_DATA0,CSI_DATA05,GPIO1_IO28,USDHC2_DATA4,KPP_ROW1,,,ADC2_IN1,"ACMP1_OUT,ACMP2_IN6",ALT5 +GPIO_AD_B1_13,FLEXSPI_A_DATA0,ACMP2_OUT,LPSPI3_SIN,SAI1_TX_DATA0,CSI_DATA04,GPIO1_IO29,USDHC2_DATA5,KPP_COL1,,,ADC2_IN2,"ACMP2_OUT,ACMP3_IN6",ALT5 +GPIO_AD_B1_14,FLEXSPI_A_SCLK,ACMP3_OUT,LPSPI3_SOUT,SAI1_TX_BCLK,CSI_DATA03,GPIO1_IO30,USDHC2_DATA6,KPP_ROW0,,,ADC2_IN3,"ACMP3_OUT,ACMP4_IN6",ALT5 +GPIO_AD_B1_15,FLEXSPI_A_SS0_B,ACMP4_OUT,LPSPI3_SCK,SAI1_TX_SYNC,CSI_DATA02,GPIO1_IO31,USDHC2_DATA7,KPP_COL0,,,ADC2_IN4,ACMP4_OUT,ALT5 +GPIO_B0_00,LCD_CLK,TMR1_TIMER0,MQS_RIGHT,LPSPI4_PCS0,FLEXIO2_D00,GPIO2_IO00,SEMC_CSX1,,,,,,ALT5 +GPIO_B0_01,LCD_ENABLE,TMR1_TIMER1,MQS_LEFT,LPSPI4_SIN,FLEXIO2_D01,GPIO2_IO01,SEMC_CSX2,,,,,,ALT5 +GPIO_B0_02,LCD_HSYNC,TMR1_TIMER2,FLEXCAN1_TX,LPSPI4_SOUT,FLEXIO2_D02,GPIO2_IO02,SEMC_CSX3,,,,,,ALT5 +GPIO_B0_03,LCD_VSYNC,TMR2_TIMER0,FLEXCAN1_RX,LPSPI4_SCK,FLEXIO2_D03,GPIO2_IO03,WDOG2_RST_B_DEB,,,,,,ALT5 +GPIO_B0_04,LCD_DATA00,TMR2_TIMER1,LPI2C2_SCL,ARM_TRACE0,FLEXIO2_D04,GPIO2_IO04,SRC_BT_CFG00,,,,,,ALT5 +GPIO_B0_05,LCD_DATA01,TMR2_TIMER2,LPI2C2_SDA,ARM_TRACE1,FLEXIO2_D05,GPIO2_IO05,SRC_BT_CFG01,,,,,,ALT5 +GPIO_B0_06,LCD_DATA02,TMR3_TIMER0,FLEXPWM2_PWM0_A,ARM_TRACE2,FLEXIO2_D06,GPIO2_IO06,SRC_BT_CFG02,,,,,,ALT5 +GPIO_B0_07,LCD_DATA03,TMR3_TIMER1,FLEXPWM2_PWM0_B,ARM_TRACE3,FLEXIO2_D07,GPIO2_IO07,SRC_BT_CFG03,,,,,,ALT5 +GPIO_B0_08,LCD_DATA04,TMR3_TIMER2,FLEXPWM2_PWM1_A,LPUART3_TXD,FLEXIO2_D08,GPIO2_IO08,SRC_BT_CFG04,,,,,,ALT5 +GPIO_B0_09,LCD_DATA05,TMR4_TIMER0,FLEXPWM2_PWM1_B,LPUART3_RXD,FLEXIO2_D09,GPIO2_IO09,SRC_BT_CFG05,,,,,,ALT5 +GPIO_B0_10,LCD_DATA06,TMR4_TIMER1,FLEXPWM2_PWM2_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",FLEXIO2_D10,GPIO2_IO10,SRC_BT_CFG06,,,,,,ALT5 +GPIO_B0_11,LCD_DATA07,TMR4_TIMER2,FLEXPWM2_PWM2_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",FLEXIO2_D11,GPIO2_IO11,SRC_BT_CFG07,,,,,,ALT5 +GPIO_B0_12,LCD_DATA08,XBAR_INOUT10,ARM_TRACE_CLK,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXIO2_D12,GPIO2_IO12,SRC_BT_CFG08,,,,,,ALT5 +GPIO_B0_13,LCD_DATA09,XBAR_INOUT11,ARM_TRACE_SWO,SAI1_MCLK,FLEXIO2_D13,GPIO2_IO13,SRC_BT_CFG09,,,,,,ALT5 +GPIO_B0_14,LCD_DATA10,XBAR_INOUT12,ARM_CM7_EVENT0,SAI1_RX_SYNC,FLEXIO2_D14,GPIO2_IO14,SRC_BT_CFG10,,,,,,ALT5 +GPIO_B0_15,LCD_DATA11,XBAR_INOUT13,ARM_CM7_EVENT1,SAI1_RX_BCLK,FLEXIO2_D15,GPIO2_IO15,SRC_BT_CFG11,,,,,,ALT5 +GPIO_B1_00,LCD_DATA12,XBAR_INOUT14,LPUART4_TXD,SAI1_RX_DATA0,FLEXIO2_D16,GPIO2_IO16,FLEXPWM1_PWM3_A,,,,,,ALT5 +GPIO_B1_01,LCD_DATA13,XBAR_INOUT15,LPUART4_RXD,SAI1_TX_DATA0,FLEXIO2_D17,GPIO2_IO17,FLEXPWM1_PWM3_B,,,,,,ALT5 +GPIO_B1_02,LCD_DATA14,XBAR_INOUT16,LPSPI4_PCS2,SAI1_TX_BCLK,FLEXIO2_D18,GPIO2_IO18,FLEXPWM2_PWM3_A,,,,,,ALT5 +GPIO_B1_03,LCD_DATA15,XBAR_INOUT17,LPSPI4_PCS1,SAI1_TX_SYNC,FLEXIO2_D19,GPIO2_IO19,FLEXPWM2_PWM3_B,,,,,,ALT5 +GPIO_B1_04,LCD_DATA16,LPSPI4_PCS0,CSI_DATA15,ENET_RX_DATA0,FLEXIO2_D20,GPIO2_IO20,,,,,,,ALT5 +GPIO_B1_05,LCD_DATA17,LPSPI4_SIN,CSI_DATA14,ENET_RX_DATA1,FLEXIO2_D21,GPIO2_IO21,,,,,,,ALT5 +GPIO_B1_06,LCD_DATA18,LPSPI4_SOUT,CSI_DATA13,ENET_RX_EN,FLEXIO2_D22,GPIO2_IO22,,,,,,,ALT5 +GPIO_B1_07,LCD_DATA19,LPSPI4_SCK,CSI_DATA12,ENET_TX_DATA0,FLEXIO2_D23,GPIO2_IO23,,,,,,,ALT5 +GPIO_B1_08,LCD_DATA20,TMR1_TIMER3,CSI_DATA11,ENET_TX_DATA1,FLEXIO2_D24,GPIO2_IO24,FLEXCAN2_TX,,,,,,ALT5 +GPIO_B1_09,LCD_DATA21,TMR2_TIMER3,CSI_DATA10,ENET_TX_EN,FLEXIO2_D25,GPIO2_IO25,FLEXCAN2_RX,,,,,,ALT5 +GPIO_B1_10,LCD_DATA22,TMR3_TIMER3,CSI_DATA00,ENET_TX_CLK,FLEXIO2_D26,GPIO2_IO26,ENET_REF_CLK,,,,,,ALT5 +GPIO_B1_11,LCD_DATA23,TMR4_TIMER3,CSI_DATA01,ENET_RX_ER,FLEXIO2_D27,GPIO2_IO27,LPSPI4_PCS3,,,,,,ALT5 +GPIO_B1_12,,LPUART5_TXD,CSI_PIXCLK,ENET_1588_EVENT0_IN,FLEXIO2_D28,GPIO2_IO28,USDHC1_CD_B,,,,,,ALT5 +GPIO_B1_13,WDOG1_B,LPUART5_RXD,CSI_VSYNC,ENET_1588_EVENT0_OUT,FLEXIO2_D29,GPIO2_IO29,USDHC1_WP,,,,,,ALT5 +GPIO_B1_14,ENET_MDC,FLEXPWM4_PWM2_A,CSI_HSYNC,XBAR_INOUT02,FLEXIO2_D30,GPIO2_IO30,USDHC1_VSELECT,,,,,,ALT5 +GPIO_B1_15,ENET_MDIO,FLEXPWM4_PWM3_A,CSI_MCLK,XBAR_INOUT03,FLEXIO2_D31,GPIO2_IO31,USDHC1_RESET_B,,,,,,ALT5 +GPIO_EMC_00,SEMC_DATA00,FLEXPWM4_PWM0_A,LPSPI2_SCK,XBAR_INOUT02,FLEXIO1_D00,GPIO4_IO0,USB_PHY1_TSTI_TX_LS_MODE,,,,,,ALT5 +GPIO_EMC_01,SEMC_DATA01,FLEXPWM4_PWM0_B,LPSPI2_PCS0,XBAR_INOUT03,FLEXIO1_D01,GPIO4_IO1,USB_PHY1_TSTI_TX_HS_MODE,,,,,,ALT5 +GPIO_EMC_02,SEMC_DATA02,FLEXPWM4_PWM1_A,LPSPI2_SOUT,XBAR_INOUT04,FLEXIO1_D02,GPIO4_IO2,USB_PHY1_TSTI_TX_DN,,,,,,ALT5 +GPIO_EMC_03,SEMC_DATA03,FLEXPWM4_PWM1_B,LPSPI2_SIN,XBAR_INOUT05,FLEXIO1_D03,GPIO4_IO3,USB_PHY1_TSTO_RX_SQUELCH,,,,,,ALT5 +GPIO_EMC_04,SEMC_DATA04,FLEXPWM4_PWM2_A,SAI2_TX_DATA,XBAR_INOUT06,FLEXIO1_D04,GPIO4_IO4,USB_PHY1_TSTO_RX_DISCON_DET,,,,,,ALT5 +GPIO_EMC_05,SEMC_DATA05,FLEXPWM4_PWM2_B,SAI2_TX_SYNC,XBAR_INOUT07,FLEXIO1_D05,GPIO4_IO5,USB_PHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_06,SEMC_DATA06,FLEXPWM2_PWM0_A,SAI2_TX_BCLK,XBAR_INOUT08,FLEXIO1_D06,GPIO4_IO6,USB_PHY2_TSTO_RX_FS_RXD,,,,,,ALT5 +GPIO_EMC_07,SEMC_DATA07,FLEXPWM2_PWM0_B,SAI2_MCLK,XBAR_INOUT09,FLEXIO1_D07,GPIO4_IO7,USB_PHY1_TSTO_RX_FS_RXD,,,,,,ALT5 +GPIO_EMC_08,SEMC_DM0,FLEXPWM2_PWM1_A,SAI2_RX_DATA,XBAR_INOUT17,FLEXIO1_D08,GPIO4_IO8,USB_PHY1_TSTI_TX_DP,,,,,,ALT5 +GPIO_EMC_09,SEMC_ADDR00,FLEXPWM2_PWM1_B,SAI2_RX_SYNC,FLEXCAN2_TX,FLEXIO1_D09,GPIO4_IO9,USB_PHY1_TSTI_TX_EN,,,,,,ALT5 +GPIO_EMC_10,SEMC_ADDR01,FLEXPWM2_PWM2_A,SAI2_RX_BCLK,FLEXCAN2_RX,FLEXIO1_D10,GPIO4_IO10,USB_PHY1_TSTI_TX_HIZ,,,,,,ALT5 +GPIO_EMC_11,SEMC_ADDR02,FLEXPWM2_PWM2_B,LPI2C4_SDA,USDHC2_RESET_B,FLEXIO1_D11,GPIO4_IO11,USB_PHY2_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_12,SEMC_ADDR03,XBAR_INOUT24,LPI2C4_SCL,USDHC1_WP,FLEXPWM1_PWM3_A,GPIO4_IO12,USB_PHY1_TSTO_PLL_CLK20DIV,,,,,,ALT5 +GPIO_EMC_13,SEMC_ADDR04,XBAR_INOUT25,LPUART3_TXD,MQS_RIGHT,FLEXPWM1_PWM3_B,GPIO4_IO13,USB_PHY2_TSTO_PLL_CLK20DIV,,,,,,ALT5 +GPIO_EMC_14,SEMC_ADDR05,XBAR_INOUT19,LPUART3_RXD,MQS_LEFT,LPSPI2_PCS1,GPIO4_IO14,USB_PHY2_TSTO_RX_SQUELCH,,,,,,ALT5 +GPIO_EMC_15,SEMC_ADDR06,XBAR_INOUT20,LPUART3_CTS_B,SPDIF_OUT,TMR3_TIMER0,GPIO4_IO15,USB_PHY2_TSTO_RX_DISCON_DET,,,,,,ALT5 +GPIO_EMC_16,SEMC_ADDR07,XBAR_INOUT21,LPUART3_RTS_B,SPDIF_IN,TMR3_TIMER1,GPIO4_IO16,,,,,,,ALT5 +GPIO_EMC_17,SEMC_ADDR08,FLEXPWM4_PWM3_A,LPUART4_CTS_B,FLEXCAN1_TX,TMR3_TIMER2,GPIO4_IO17,,,,,,,ALT5 +GPIO_EMC_18,SEMC_ADDR09,FLEXPWM4_PWM3_B,LPUART4_RTS_B,FLEXCAN1_RX,TMR3_TIMER3,GPIO4_IO18,SNVS_VIO_5_CTL,,,,,,ALT5 +GPIO_EMC_19,SEMC_ADDR11,FLEXPWM2_PWM3_A,LPUART4_TXD,ENET_RX_DATA1,TMR2_TIMER0,GPIO4_IO19,SNVS_VIO_5_B,,,,,,ALT5 +GPIO_EMC_20,SEMC_ADDR12,FLEXPWM2_PWM3_B,LPUART4_RXD,ENET_RX_DATA0,TMR2_TIMER1,GPIO4_IO20,,,,,,,ALT5 +GPIO_EMC_21,SEMC_BA0,FLEXPWM3_PWM3_A,LPI2C3_SDA,ENET_TX_DATA1,TMR2_TIMER2,GPIO4_IO21,,,,,,,ALT5 +GPIO_EMC_22,SEMC_BA1,FLEXPWM3_PWM3_B,LPI2C3_SCL,ENET_TX_DATA0,TMR2_TIMER3,GPIO4_IO22,,,,,,,ALT5 +GPIO_EMC_23,SEMC_ADDR10,FLEXPWM1_PWM0_A,LPUART5_TXD,ENET_RX_EN,GPT1_CAPTURE2,GPIO4_IO23,,,,,,,ALT5 +GPIO_EMC_24,SEMC_CAS,FLEXPWM1_PWM0_B,LPUART5_RXD,ENET_TX_EN,GPT1_CAPTURE1,GPIO4_IO24,,,,,,,ALT5 +GPIO_EMC_25,SEMC_RAS,FLEXPWM1_PWM1_A,LPUART6_TXD,ENET_TX_CLK,ENET_REF_CLK,GPIO4_IO25,,,,,,,ALT5 +GPIO_EMC_26,SEMC_CLK,FLEXPWM1_PWM1_B,LPUART6_RXD,ENET_RX_ER,FLEXIO1_D12,GPIO4_IO26,,,,,,,ALT5 +GPIO_EMC_27,SEMC_CKE,FLEXPWM1_PWM2_A,LPUART5_RTS_B,LPSPI1_SCK,FLEXIO1_D13,GPIO4_IO27,,,,,,,ALT5 +GPIO_EMC_28,SEMC_WE,FLEXPWM1_PWM2_B,LPUART5_CTS_B,LPSPI1_SOUT,FLEXIO1_D14,GPIO4_IO28,,,,,,,ALT5 +GPIO_EMC_29,SEMC_CS0,FLEXPWM3_PWM0_A,LPUART6_RTS_B,LPSPI1_SIN,FLEXIO1_D15,GPIO4_IO29,,,,,,,ALT5 +GPIO_EMC_30,SEMC_DATA08,FLEXPWM3_PWM0_B,LPUART6_CTS_B,LPSPI1_PCS0,CSI_DATA23,GPIO4_IO30,,,,,,,ALT5 +GPIO_EMC_31,SEMC_DATA09,FLEXPWM3_PWM1_A,LPUART7_TXD,LPSPI1_PCS1,CSI_DATA22,GPIO4_IO31,,,,,,,ALT5 +GPIO_EMC_32,SEMC_DATA10,FLEXPWM3_PWM1_B,LPUART7_RXD,CCM_PMIC_READY,CSI_DATA21,GPIO3_IO18,,,,,,,ALT5 +GPIO_EMC_33,SEMC_DATA11,FLEXPWM3_PWM2_A,USDHC1_RESET_B,SAI3_RX_DATA,CSI_DATA20,GPIO3_IO19,,,,,,,ALT5 +GPIO_EMC_34,SEMC_DATA12,FLEXPWM3_PWM2_B,USDHC1_VSELECT,SAI3_RX_SYNC,CSI_DATA19,GPIO3_IO20,,,,,,,ALT5 +GPIO_EMC_35,SEMC_DATA13,XBAR_INOUT18,GPT1_COMPARE1,SAI3_RX_BCLK,CSI_DATA18,GPIO3_IO21,USDHC1_CD_B,,,,,,ALT5 +GPIO_EMC_36,SEMC_DATA14,XBAR_INOUT22,GPT1_COMPARE2,SAI3_TX_DATA,CSI_DATA17,GPIO3_IO22,USDHC1_WP,,,,,,ALT5 +GPIO_EMC_37,SEMC_DATA15,XBAR_INOUT23,GPT1_COMPARE3,SAI3_MCLK,CSI_DATA16,GPIO3_IO23,USDHC2_WP,,,,,,ALT5 +GPIO_EMC_38,SEMC_DM1,FLEXPWM1_PWM3_A,LPUART8_TXD,SAI3_TX_BCLK,CSI_FIELD,GPIO3_IO24,USDHC2_VSELECT,,,,,,ALT5 +GPIO_EMC_39,SEMC_DQS,FLEXPWM1_PWM3_B,LPUART8_RXD,SAI3_TX_SYNC,WDOG1_B,GPIO3_IO25,USDHC2_CD_B,,,,,,ALT5 +GPIO_EMC_40,SEMC_RDY,GPT2_CAPTURE2,LPSPI1_PCS2,USB_OTG2_OC,ENET_MDC,GPIO3_IO26,USDHC2_RESET_B,,,,,,ALT5 +GPIO_EMC_41,SEMC_CSX0,GPT2_CAPTURE1,LPSPI1_PCS3,USB_OTG2_PWR,ENET_MDIO,GPIO3_IO27,USDHC1_VSELECT,,,,,,ALT5 +GPIO_SD_B0_00,USDHC1_CMD,FLEXPWM1_PWM0_A,LPI2C3_SCL,XBAR_INOUT04,LPSPI1_SCK,GPIO3_IO12,FLEXSPI_A_SS1_B,,,,,,ALT5 +GPIO_SD_B0_01,USDHC1_CLK,FLEXPWM1_PWM0_B,LPI2C3_SDA,XBAR_INOUT05,LPSPI1_PCS0,GPIO3_IO13,FLEXSPI_B_SS1_B,,,,,,ALT5 +GPIO_SD_B0_02,USDHC1_DATA0,FLEXPWM1_PWM1_A,LPUART8_CTS_B,XBAR_INOUT06,LPSPI1_SOUT,GPIO3_IO14,,,,,,,ALT5 +GPIO_SD_B0_03,USDHC1_DATA1,FLEXPWM1_PWM1_B,LPUART8_RTS_B,XBAR_INOUT07,LPSPI1_SIN,GPIO3_IO15,,,,,,,ALT5 +GPIO_SD_B0_04,USDHC1_DATA2,FLEXPWM1_PWM2_A,LPUART8_TXD,XBAR_INOUT08,FLEXSPI_B_SS0_B,GPIO3_IO16,CCM_CLKO1,,,,,,ALT5 +GPIO_SD_B0_05,USDHC1_DATA3,FLEXPWM1_PWM2_B,LPUART8_RXD,XBAR_INOUT09,FLEXSPI_B_DQS,GPIO3_IO17,CCM_CLKO2,,,,,,ALT5 +GPIO_SD_B1_00,USDHC2_DATA3,FLEXSPI_B_DATA3,FLEXPWM1_PWM3_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",LPUART4_TXD,GPIO3_IO00,,,,,,,ALT5 +GPIO_SD_B1_01,USDHC2_DATA2,FLEXSPI_B_DATA2,FLEXPWM1_PWM3_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",LPUART4_RXD,GPIO3_IO01,,,,,,,ALT5 +GPIO_SD_B1_02,USDHC2_DATA1,FLEXSPI_B_DATA1,FLEXPWM2_PWM3_A,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXCAN1_TX,GPIO3_IO02,CCM_WAIT,,,,,,ALT5 +GPIO_SD_B1_03,USDHC2_DATA0,FLEXSPI_B_DATA0,FLEXPWM2_PWM3_B,SAI1_MCLK,FLEXCAN1_RX,GPIO3_IO03,CCM_PMIC_READY,,,,,,ALT5 +GPIO_SD_B1_04,USDHC2_CLK,FLEXSPI_B_SCLK,LPI2C1_SCL,SAI1_RX_SYNC,FLEXSPI_A_SS1_B,GPIO3_IO04,CCM_STOP,,,,,,ALT5 +GPIO_SD_B1_05,USDHC2_CMD,FLEXSPI_A_DQS,LPI2C1_SDA,SAI1_RX_BCLK,FLEXSPI_B_SS0_B,GPIO3_IO05,,,,,,,ALT5 +GPIO_SD_B1_06,USDHC2_RESET_B,FLEXSPI_A_SS0_B,LPUART7_CTS_B,SAI1_RX_DATA0,LPSPI2_PCS0,GPIO3_IO06,,,,,,,ALT5 +GPIO_SD_B1_07,SEMC_CSX1,FLEXSPI_A_SCLK,LPUART7_RTS_B,SAI1_TX_DATA0,LPSPI2_SCK,GPIO3_IO07,CCM_REF_EN_B,,,,,,ALT5 +GPIO_SD_B1_08,USDHC2_DATA4,FLEXSPI_A_DATA0,LPUART7_TXD,SAI1_TX_BCLK,LPSPI2_SOUT,GPIO3_IO08,SEMC_CSX2,,,,,,ALT5 +GPIO_SD_B1_09,USDHC2_DATA5,FLEXSPI_A_DATA1,LPUART7_RXD,SAI1_TX_SYNC,LPSPI2_SIN,GPIO3_IO09,,,,,,,ALT5 +GPIO_SD_B1_10,USDHC2_DATA6,FLEXSPI_A_DATA2,LPUART2_RXD,LPI2C2_SDA,LPSPI2_PCS2,GPIO3_IO10,,,,,,,ALT5 +GPIO_SD_B1_11,USDHC2_DATA7,FLEXSPI_A_DATA3,LPUART2_TXD,LPI2C2_SCL,LPSPI2_PCS3,GPIO3_IO11,,,,,,,ALT5 diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h index 8bd2a579174aa..c9e6d8bf2c142 100644 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h @@ -4,6 +4,6 @@ #define BOARD_FLASH_SIZE (8 * 1024 * 1024) // MIMXRT1060_EVK has 1 user LED -#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk index b34169801b900..32f87046b8a5e 100644 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk @@ -2,6 +2,25 @@ MCU_SERIES = MIMXRT1062 MCU_VARIANT = MIMXRT1062DVJ6A JLINK_PATH ?= /media/RT1060-EVK/ +JLINK_COMMANDER_SCRIPT = $(BUILD)/script.jlink + + +ifdef JLINK_IP +JLINK_CONNECTION_SETTINGS = -IP $(JLINK_IP) +else +JLINK_CONNECTION_SETTINGS = -USB +endif + + +deploy_jlink: $(BUILD)/firmware.hex + $(Q)$(TOUCH) $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "ExitOnError 1" > $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "speed auto" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "r" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "st" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "loadfile \"$(realpath $(BUILD)/firmware.hex)\"" >> $(JLINK_COMMANDER_SCRIPT) + $(ECHO) "qc" >> $(JLINK_COMMANDER_SCRIPT) + $(JLINK_PATH)JLinkExe -device $(MCU_VARIANT) -if SWD $(JLINK_CONNECTION_SETTINGS) -CommanderScript $(JLINK_COMMANDER_SCRIPT) deploy: $(BUILD)/firmware.bin cp $< $(JLINK_PATH) diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c deleted file mode 100644 index d5da9c6f99275..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_AD_B0_09_af[] = { - PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), -}; - -pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.csv b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.csv new file mode 100644 index 0000000000000..afd63d4f97fe8 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.csv @@ -0,0 +1,51 @@ +D0,GPIO_AD_B1_07 +D1,GPIO_AD_B1_06 +D2,GPIO_AD_B0_11 +D3,GPIO_AD_B1_08 +D4,GPIO_AD_B0_09 +D5,GPIO_AD_B0_10 +D6,GPIO_AD_B1_02 +D7,GPIO_AD_B1_03 +D8,GPIO_AD_B0_03 +D9,GPIO_AD_B0_02 +D10,GPIO_SD_B0_01 +D11,GPIO_SD_B0_02 +D12,GPIO_SD_B0_03 +D13,GPIO_SD_B0_00 +D14,GPIO_AD_B1_01 +D15,GPIO_AD_B1_00 +A0,GPIO_AD_B1_10 +A1,GPIO_AD_B1_11 +A2,GPIO_AD_B1_04 +A3,GPIO_AD_B1_05 +A4,GPIO_AD_B1_01 +A5,GPIO_AD_B1_00 +RX,GPIO_AD_B1_07 +TX,GPIO_AD_B1_06 +SCL,GPIO_AD_B1_00 +SDA,GPIO_AD_B1_01 +SCK,GPIO_SD_B0_00 +SDI,GPIO_SD_B0_03 +SDO,GPIO_SD_B0_02 +CS,GPIO_SD_B0_01 +LED_GREEN,GPIO_AD_B0_09 +GPIO_AD_B1_07,GPIO_AD_B1_07 +GPIO_AD_B1_06,GPIO_AD_B1_06 +GPIO_AD_B0_11,GPIO_AD_B0_11 +GPIO_AD_B1_08,GPIO_AD_B1_08 +GPIO_AD_B0_09,GPIO_AD_B0_09 +GPIO_AD_B0_10,GPIO_AD_B0_10 +GPIO_AD_B1_02,GPIO_AD_B1_02 +GPIO_AD_B1_03,GPIO_AD_B1_03 +GPIO_AD_B0_03,GPIO_AD_B0_03 +GPIO_AD_B0_02,GPIO_AD_B0_02 +GPIO_SD_B0_01,GPIO_SD_B0_01 +GPIO_SD_B0_02,GPIO_SD_B0_02 +GPIO_SD_B0_03,GPIO_SD_B0_03 +GPIO_SD_B0_00,GPIO_SD_B0_00 +GPIO_AD_B1_01,GPIO_AD_B1_01 +GPIO_AD_B1_00,GPIO_AD_B1_00 +GPIO_AD_B1_10,GPIO_AD_B1_10 +GPIO_AD_B1_11,GPIO_AD_B1_11 +GPIO_AD_B1_04,GPIO_AD_B1_04 +GPIO_AD_B1_05,GPIO_AD_B1_05 diff --git a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h deleted file mode 100644 index baef51c6c8612..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1060_EVK/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_AD_B0_09; diff --git a/ports/mimxrt/boards/MIMXRT1062_af.csv b/ports/mimxrt/boards/MIMXRT1062_af.csv new file mode 100644 index 0000000000000..9b83efeddc169 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1062_af.csv @@ -0,0 +1,125 @@ +Pad,ALT0, ALT1, ALT2, ALT3, ALT4, ALT5, ALT6, ALT7, ALT8, ALT9,ADC,ACMP,Default +GPIO_AD_B0_00,FLEXPWM2_PWM3_A,XBAR_INOUT14,REF_CLK_32K,USB_OTG2_ID,LPI2C1_SCLS,GPIO1_IO00,USDHC1_RESET_B,LPSPI3_SCK,,,,ACMP1_IN4,ALT5 +GPIO_AD_B0_01,FLEXPWM2_PWM3_B,XBAR_INOUT15,REF_CLK_24M,USB_OTG1_ID,LPI2C1_SDAS,GPIO1_IO01,EWM_OUT_B,LPSPI3_SOUT,,,,ACMP2_IN4,ALT5 +GPIO_AD_B0_02,FLEXCAN2_TX,XBAR_INOUT16,LPUART6_TXD,USB_OTG1_PWR,FLEXPWM1_PWM0_X,GPIO1_IO02,LPI2C1_HREQ,LPSPI3_SIN,,,,ACMP3_IN4,ALT5 +GPIO_AD_B0_03,FLEXCAN2_RX,XBAR_INOUT17,LPUART6_RXD,USB_OTG1_OC,FLEXPWM1_PWM1_X,GPIO1_IO03,REF_CLK_24M,LPSPI3_PCS0,,,,ACMP4_IN4,ALT5 +GPIO_AD_B0_04,SRC_BOOT_MODE0,MQS_RIGHT,ENET_TX_DATA3,SAI2_TX_SYNC,CSI_DATA09,GPIO1_IO04,PIT_TRIGGER0,LPSPI3_PCS1,,,,,ALT0 +GPIO_AD_B0_05,SRC_BOOT_MODE1,MQS_LEFT,ENET_TX_DATA2,SAI2_TX_BCLK,CSI_DATA08,GPIO1_IO05,XBAR_INOUT17,LPSPI3_PCS2,,,,,ALT0 +GPIO_AD_B0_06,"JTAG_TMS,SWD_DIO",GPT2_COMPARE1,ENET_RX_CLK,SAI2_RX_BCLK,CSI_DATA07,GPIO1_IO06,XBAR_INOUT18,LPSPI3_PCS3,,,,,ALT0 +GPIO_AD_B0_07,"JTAG_TCK,SWD_CLK",GPT2_COMPARE2,ENET_TX_ER,SAI2_RX_SYNC,CSI_DATA06,GPIO1_IO07,XBAR_INOUT19,ENET_1588_EVENT3_OUT,,,,,ALT0 +GPIO_AD_B0_08,JTAG_MOD,GPT2_COMPARE3,ENET_RX_DATA3,SAI2_RX_DATA,CSI_DATA05,GPIO1_IO08,XBAR_INOUT20,ENET_1588_EVENT3_IN,,,,,ALT0 +GPIO_AD_B0_09,JTAG_TDI,FLEXPWM2_PWM3_A,ENET_RX_DATA2,SAI2_TX_DATA,CSI_DATA04,GPIO1_IO09,XBAR_INOUT21,GPT2_CLK,,,,,ALT0 +GPIO_AD_B0_10,JTAG_TDO,FLEXPWM1_PWM3_A,ENET_CRS,SAI2_MCLK,CSI_DATA03,GPIO1_IO10,XBAR_INOUT22,ENET_1588_EVENT0_OUT,FLEXCAN3_TX,,,,ALT0 +GPIO_AD_B0_11,JTAG_TRSTB,FLEXPWM1_PWM3_B,ENET_COL,WDOG1_B,CSI_DATA02,GPIO1_IO11,XBAR_INOUT23,ENET_1588_EVENT0_IN,FLEXCAN3_RX,,,,ALT0 +GPIO_AD_B0_12,LPI2C4_SCL,CCM_PMIC_READY,LPUART1_TXD,WDOG2_B,FLEXPWM1_PWM2_X,GPIO1_IO12,ENET_1588_EVENT1_OUT,,,,ADC1_IN1,,ALT5 +GPIO_AD_B0_13,LPI2C4_SDA,GPT1_CLK,LPUART1_RXD,EWM_OUT_B,FLEXPWM1_PWM3_X,GPIO1_IO13,ENET_1588_EVENT1_IN,REF_CLK_24M,,,ADC1_IN2,ACMP1_IN2,ALT5 +GPIO_AD_B0_14,USB_OTG2_OC,XBAR_INOUT24,LPUART1_CTS_B,ENET_1588_EVENT0_OUT,CSI_VSYNC,GPIO1_IO14,FLEXCAN2_TX,WDOG1_ANY,FLEXCAN3_TX,,ADC1_IN3,ACMP2_IN2,ALT5 +GPIO_AD_B0_15,USB_OTG2_PWR,XBAR_INOUT25,LPUART1_RTS_B,ENET_1588_EVENT0_IN,CSI_HSYNC,GPIO1_IO15,FLEXCAN2_RX,WDOG1_RESET_B_DEB,FLEXCAN3_RX,,ADC1_IN4,ACMP3_IN2,ALT5 +GPIO_AD_B1_00,USB_OTG2_ID,TMR3_TIMER0,LPUART2_CTS_B,LPI2C1_SCL,WDOG1_B,GPIO1_IO16,USDHC1_WP,KPP_ROW7,ENET2_1588_EVENT0_OUT,FLEXIO3_D00,"ADC1_IN5,ADC2_IN5",ACMP4_IN2,ALT5 +GPIO_AD_B1_01,USB_OTG1_PWR,TMR3_TIMER1,LPUART2_RTS_B,LPI2C1_SDA,CCM_PMIC_READY,GPIO1_IO17,USDHC1_VSELECT,KPP_COL7,ENET2_1588_EVENT0_IN,FLEXIO3_D01,"ADC1_IN6,ADC2_IN6","ACMP1_IN0,ACMP2_IN0,ACMP3_IN0,ACMP4_IN0",ALT5 +GPIO_AD_B1_02,USB_OTG1_ID,TMR3_TIMER2,LPUART2_TXD,SPDIF_OUT,ENET_1588_EVENT2_OUT,GPIO1_IO18,USDHC1_CD_B,KPP_ROW6,GPT2_CLK,FLEXIO3_D02,"ADC1_IN7,ADC2_IN7",ACMP1_IN3,ALT5 +GPIO_AD_B1_03,USB_OTG1_OC,TMR3_TIMER3,LPUART2_RXD,SPDIF_IN,ENET_1588_EVENT2_IN,GPIO1_IO19,USDHC2_CD_B,KPP_COL6,GPT2_CAPTURE1,FLEXIO3_D03,"ADC1_IN8,ADC2_IN8",ACMP2_IN3,ALT5 +GPIO_AD_B1_04,FLEXSPI_B_DATA3,ENET_MDC,LPUART3_CTS_B,SPDIF_SR_CLK,CSI_PIXCLK,GPIO1_IO20,USDHC2_DATA0,KPP_ROW5,GPT2_CAPTURE2,FLEXIO3_D04,"ADC1_IN9,ADC2_IN9",ACMP3_IN3,ALT5 +GPIO_AD_B1_05,FLEXSPI_B_DATA2,ENET_MDIO,LPUART3_RTS_B,SPDIF_OUT,CSI_MCLK,GPIO1_IO21,USDHC2_DATA1,KPP_COL5,GPT2_COMPARE1,FLEXIO3_D05,"ADC1_IN10,ADC2_IN10",ACMP4_IN3,ALT5 +GPIO_AD_B1_06,FLEXSPI_B_DATA1,LPI2C3_SDA,LPUART3_TXD,SPDIF_LOCK,CSI_VSYNC,GPIO1_IO22,USDHC2_DATA2,KPP_ROW4,GPT2_COMPARE2,FLEXIO3_D06,"ADC1_IN11,ADC2_IN11","ACMP1_IN1,ACMP2_IN1,ACMP3_IN1,ACMP4_IN1",ALT5 +GPIO_AD_B1_07,FLEXSPI_B_DATA0,LPI2C3_SCL,LPUART3_RXD,SPDIF_EXT_CLK,CSI_HSYNC,GPIO1_IO23,USDHC2_DATA3,KPP_COL4,GPT2_COMPARE3,FLEXIO3_D07,"ADC1_IN12,ADC2_IN12",ACMP1_IN5,ALT5 +GPIO_AD_B1_08,FLEXSPI_A_SS1_B,FLEXPWM4_PWM0_A,FLEXCAN1_TX,CCM_PMIC_READY,CSI_DATA09,GPIO1_IO24,USDHC2_CMD,KPP_ROW3,,FLEXIO3_D08,"ADC1_IN13,ADC2_IN13",ACMP2_IN5,ALT5 +GPIO_AD_B1_09,FLEXSPI_A_DQS,FLEXPWM4_PWM1_A,FLEXCAN1_RX,SAI1_MCLK,CSI_DATA08,GPIO1_IO25,USDHC2_CLK,KPP_COL3,,FLEXIO3_D09,"ADC1_IN14,ADC2_IN14",ACMP3_IN5,ALT5 +GPIO_AD_B1_10,FLEXSPI_A_DATA3,WDOG1_B,LPUART8_TXD,SAI1_RX_SYNC,CSI_DATA07,GPIO1_IO26,USDHC2_WP,KPP_ROW2,ENET2_1588_EVENT1_OUT,FLEXIO3_D10,"ADC1_IN15,ADC2_IN15",ACMP4_IN5,ALT5 +GPIO_AD_B1_11,FLEXSPI_A_DATA2,EWM_OUT_B,LPUART8_RXD,SAI1_RX_BCLK,CSI_DATA06,GPIO1_IO27,USDHC2_RESET_B,KPP_COL2,ENET2_1588_EVENT1_IN,FLEXIO3_D11,ADC2_IN0,ACMP1_IN6,ALT5 +GPIO_AD_B1_12,FLEXSPI_A_DATA1,ACMP_OUT00,LPSPI3_PCS0,SAI1_RX_DATA0,CSI_DATA05,GPIO1_IO28,USDHC2_DATA4,KPP_ROW1,ENET2_1588_EVENT2_OUT,FLEXIO3_D12,ADC2_IN1,"ACMP1_OUT,ACMP2_IN6",ALT5 +GPIO_AD_B1_13,FLEXSPI_A_DATA0,ACMP_OUT01,LPSPI3_SIN,SAI1_TX_DATA0,CSI_DATA04,GPIO1_IO29,USDHC2_DATA5,KPP_COL1,ENET2_1588_EVENT2_IN,FLEXIO3_D13,ADC2_IN2,"ACMP2_OUT,ACMP3_IN6",ALT5 +GPIO_AD_B1_14,FLEXSPI_A_SCLK,ACMP_OUT02,LPSPI3_SOUT,SAI1_TX_BCLK,CSI_DATA03,GPIO1_IO30,USDHC2_DATA6,KPP_ROW0,ENET2_1588_EVENT3_OUT,FLEXIO3_D14,ADC2_IN3,"ACMP3_OUT,ACMP4_IN6",ALT5 +GPIO_AD_B1_15,FLEXSPI_A_SS0_B,ACMP_OUT03,LPSPI3_SCK,SAI1_TX_SYNC,CSI_DATA02,GPIO1_IO31,USDHC2_DATA7,KPP_COL0,ENET2_1588_EVENT3_IN,FLEXIO3_D15,ADC2_IN4,ACMP4_OUT,ALT5 +GPIO_B0_00,LCD_CLK,TMR1_TIMER0,MQS_RIGHT,LPSPI4_PCS0,FLEXIO2_D00,GPIO2_IO00,SEMC_CSX1,,ENET2_MDC,,,,ALT5 +GPIO_B0_01,LCD_ENABLE,TMR1_TIMER1,MQS_LEFT,LPSPI4_SIN,FLEXIO2_D01,GPIO2_IO01,SEMC_CSX2,,ENET2_MDIO,,,,ALT5 +GPIO_B0_02,LCD_HSYNC,TMR1_TIMER2,FLEXCAN1_TX,LPSPI4_SOUT,FLEXIO2_D02,GPIO2_IO02,SEMC_CSX3,,ENET2_1588_EVENT0_OUT,,,,ALT5 +GPIO_B0_03,LCD_VSYNC,TMR2_TIMER0,FLEXCAN1_RX,LPSPI4_SCK,FLEXIO2_D03,GPIO2_IO03,WDOG2_RESET_B_DEB,,ENET2_1588_EVENT0_IN,,,,ALT5 +GPIO_B0_04,LCD_DATA00,TMR2_TIMER1,LPI2C2_SCL,ARM_TRACE0,FLEXIO2_D04,GPIO2_IO04,SRC_BT_CFG00,,ENET2_TDATA3,,,,ALT5 +GPIO_B0_05,LCD_DATA01,TMR2_TIMER2,LPI2C2_SDA,ARM_TRACE1,FLEXIO2_D05,GPIO2_IO05,SRC_BT_CFG01,,ENET2_TDATA2,,,,ALT5 +GPIO_B0_06,LCD_DATA02,TMR3_TIMER0,FLEXPWM2_PWM0_A,ARM_TRACE2,FLEXIO2_D06,GPIO2_IO06,SRC_BT_CFG02,,ENET2_RX_CLK,,,,ALT5 +GPIO_B0_07,LCD_DATA03,TMR3_TIMER1,FLEXPWM2_PWM0_B,ARM_TRACE3,FLEXIO2_D07,GPIO2_IO07,SRC_BT_CFG03,,ENET2_TX_ER,,,,ALT5 +GPIO_B0_08,LCD_DATA04,TMR3_TIMER2,FLEXPWM2_PWM1_A,LPUART3_TXD,FLEXIO2_D08,GPIO2_IO08,SRC_BT_CFG04,,ENET2_RDATA3,,,,ALT5 +GPIO_B0_09,LCD_DATA05,TMR4_TIMER0,FLEXPWM2_PWM1_B,LPUART3_RXD,FLEXIO2_D09,GPIO2_IO09,SRC_BT_CFG05,,ENET2_RDATA2,,,,ALT5 +GPIO_B0_10,LCD_DATA06,TMR4_TIMER1,FLEXPWM2_PWM2_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",FLEXIO2_D10,GPIO2_IO10,SRC_BT_CFG06,,ENET2_CRS,,,,ALT5 +GPIO_B0_11,LCD_DATA07,TMR4_TIMER2,FLEXPWM2_PWM2_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",FLEXIO2_D11,GPIO2_IO11,SRC_BT_CFG07,,ENET2_COL,,,,ALT5 +GPIO_B0_12,LCD_DATA08,XBAR_INOUT10,ARM_TRACE_CLK,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXIO2_D12,GPIO2_IO12,SRC_BT_CFG08,,ENET2_TDATA0,,,,ALT5 +GPIO_B0_13,LCD_DATA09,XBAR_INOUT11,ARM_TRACE_SWO,SAI1_MCLK,FLEXIO2_D13,GPIO2_IO13,SRC_BT_CFG09,,ENET2_TDATA1,,,,ALT5 +GPIO_B0_14,LCD_DATA10,XBAR_INOUT12,ARM_TXEV,SAI1_RX_SYNC,FLEXIO2_D14,GPIO2_IO14,SRC_BT_CFG10,,ENET2_TX_EN,,,,ALT5 +GPIO_B0_15,LCD_DATA11,XBAR_INOUT13,ARM_EVENT1,SAI1_RX_DATA0,FLEXIO2_D15,GPIO2_IO15,SRC_BT_CFG11,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_B1_00,LCD_DATA12,XBAR_INOUT14,LPUART4_TXD,SAI1_RX_DATA0,FLEXIO2_D16,GPIO2_IO16,FLEXPWM1_PWM3_A,,ENET2_RX_ER,FLEXIO3_D16,,,ALT5 +GPIO_B1_01,LCD_DATA13,XBAR_INOUT15,LPUART4_RXD,SAI1_TX_DATA0,FLEXIO2_D17,GPIO2_IO17,FLEXPWM1_PWM3_B,,ENET2_RDATA0,FLEXIO3_D17,,,ALT5 +GPIO_B1_02,LCD_DATA14,XBAR_INOUT16,LPSPI4_PCS2,SAI1_TX_BCLK,FLEXIO2_D18,GPIO2_IO18,FLEXPWM2_PWM3_A,,ENET2_RDATA1,FLEXIO3_D18,,,ALT5 +GPIO_B1_03,LCD_DATA15,XBAR_INOUT17,LPSPI4_PCS1,SAI1_TX_SYNC,FLEXIO2_D19,GPIO2_IO19,FLEXPWM2_PWM3_B,,ENET2_RX_EN,FLEXIO3_D19,,,ALT5 +GPIO_B1_04,LCD_DATA16,LPSPI4_PCS0,CSI_DATA15,ENET_RX_DATA0,FLEXIO2_D20,GPIO2_IO20,,,GPT1_CLK,FLEXIO3_D20,,,ALT5 +GPIO_B1_05,LCD_DATA17,LPSPI4_SIN,CSI_DATA14,ENET_RX_DATA1,FLEXIO2_D21,GPIO2_IO21,,,GPT1_CAPTURE1,FLEXIO3_D21,,,ALT5 +GPIO_B1_06,LCD_DATA18,LPSPI4_SOUT,CSI_DATA13,ENET_RX_EN,FLEXIO2_D22,GPIO2_IO22,,,GPT1_CAPTURE2,FLEXIO3_D22,,,ALT5 +GPIO_B1_07,LCD_DATA19,LPSPI4_SCK,CSI_DATA12,ENET_TX_DATA0,FLEXIO2_D23,GPIO2_IO23,,,GPT1_COMPARE1,FLEXIO3_D23,,,ALT5 +GPIO_B1_08,LCD_DATA20,TMR1_TIMER3,CSI_DATA11,ENET_TX_DATA1,FLEXIO2_D24,GPIO2_IO24,FLEXCAN2_TX,,GPT1_COMPARE2,FLEXIO3_D24,,,ALT5 +GPIO_B1_09,LCD_DATA21,TMR2_TIMER3,CSI_DATA10,ENET_TX_EN,FLEXIO2_D25,GPIO2_IO25,FLEXCAN2_RX,,GPT1_COMPARE3,FLEXIO3_D25,,,ALT5 +GPIO_B1_10,LCD_DATA22,TMR3_TIMER3,CSI_DATA00,ENET_TX_CLK,FLEXIO2_D26,GPIO2_IO26,ENET_REF_CLK,,,FLEXIO3_D26,,,ALT5 +GPIO_B1_11,LCD_DATA23,TMR4_TIMER3,CSI_DATA01,ENET_RX_ER,FLEXIO2_D27,GPIO2_IO27,LPSPI4_PCS3,,,FLEXIO3_D27,,,ALT5 +GPIO_B1_12,,LPUART5_TXD,CSI_PIXCLK,ENET_1588_EVENT0_IN,FLEXIO2_D28,GPIO2_IO28,USDHC1_CD_B,,,FLEXIO3_D28,,,ALT5 +GPIO_B1_13,WDOG1_B,LPUART5_RXD,CSI_VSYNC,ENET_1588_EVENT0_OUT,FLEXIO2_D29,GPIO2_IO29,USDHC1_WP,,SEMC_DQS4,FLEXIO3_D29,,,ALT5 +GPIO_B1_14,ENET_MDC,FLEXPWM4_PWM2_A,CSI_HSYNC,XBAR_INOUT02,FLEXIO2_D30,GPIO2_IO30,USDHC1_VSELECT,,ENET2_TDATA0,FLEXIO3_D30,,,ALT5 +GPIO_B1_15,ENET_MDIO,FLEXPWM4_PWM3_A,CSI_MCLK,XBAR_INOUT03,FLEXIO2_D31,GPIO2_IO31,USDHC1_RESET_B,,ENET2_TDATA1,FLEXIO3_D31,,,ALT5 +GPIO_EMC_00,SEMC_DATA00,FLEXPWM4_PWM0_A,LPSPI2_SCK,XBAR_INOUT02,FLEXIO1_D00,GPIO4_IO0,USB_PHY1_TSTI_TX_LS_MODE,,,,,,ALT5 +GPIO_EMC_01,SEMC_DATA01,FLEXPWM4_PWM0_B,LPSPI2_PCS0,XBAR_INOUT03,FLEXIO1_D01,GPIO4_IO1,USB_PHY1_TSTI_TX_HS_MODE,JTAG_DE_B,,,,,ALT5 +GPIO_EMC_02,SEMC_DATA02,FLEXPWM4_PWM1_A,LPSPI2_SOUT,XBAR_INOUT04,FLEXIO1_D02,GPIO4_IO2,USB_PHY1_TSTI_TX_DN,,,,,,ALT5 +GPIO_EMC_03,SEMC_DATA03,FLEXPWM4_PWM1_B,LPSPI2_SIN,XBAR_INOUT05,FLEXIO1_D03,GPIO4_IO3,USB_PHY1_TSTO_RX_SQUELCH,,,,,,ALT5 +GPIO_EMC_04,SEMC_DATA04,FLEXPWM4_PWM2_A,SAI2_TX_DATA,XBAR_INOUT06,FLEXIO1_D04,GPIO4_IO4,USB_PHY1_TSTO_RX_DISCON_DET,,,,,,ALT5 +GPIO_EMC_05,SEMC_DATA05,FLEXPWM4_PWM2_B,SAI2_TX_SYNC,XBAR_INOUT07,FLEXIO1_D05,GPIO4_IO5,USB_PHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_06,SEMC_DATA06,FLEXPWM2_PWM0_A,SAI2_TX_BCLK,XBAR_INOUT08,FLEXIO1_D06,GPIO4_IO6,USB_PHY2_TSTO_RX_FS_RXD,,,,,,ALT5 +GPIO_EMC_07,SEMC_DATA07,FLEXPWM2_PWM0_B,SAI2_MCLK,XBAR_INOUT09,FLEXIO1_D07,GPIO4_IO7,USB_PHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_08,SEMC_DM0,FLEXPWM2_PWM1_A,SAI2_RX_DATA,XBAR_INOUT17,FLEXIO1_D08,GPIO4_IO8,USB_PHY1_TSTI_TX_DP,,,,,,ALT5 +GPIO_EMC_09,SEMC_ADDR00,FLEXPWM2_PWM1_B,SAI2_RX_SYNC,FLEXCAN2_TX,FLEXIO1_D09,GPIO4_IO9,USB_PHY1_TSTI_TX_EN,,FLEXSPI2_B_SS1_B,,,,ALT5 +GPIO_EMC_10,SEMC_ADDR01,FLEXPWM2_PWM2_A,SAI2_RX_BCLK,FLEXCAN2_RX,FLEXIO1_D10,GPIO4_IO10,USB_PHY1_TSTI_TX_HIZ,,FLEXSPI2_B_SS0_B,,,,ALT5 +GPIO_EMC_11,SEMC_ADDR02,FLEXPWM2_PWM2_B,LPI2C4_SDA,USDHC2_RESET_B,FLEXIO1_D11,GPIO4_IO11,USB_PHY2_TSTO_RX_FS_RXD,,FLEXSPI2_B_DQS,,,,ALT5 +GPIO_EMC_12,SEMC_ADDR03,XBAR_INOUT24,LPI2C4_SCL,USDHC1_WP,FLEXPWM1_PWM3_A,GPIO4_IO12,USB_PHY1_TSTO_PLL_CLK20DIV,,FLEXSPI2_B_SCLK,,,,ALT5 +GPIO_EMC_13,SEMC_ADDR04,XBAR_INOUT25,LPUART3_TXD,MQS_RIGHT,FLEXPWM1_PWM3_B,GPIO4_IO13,USB_PHY2_TSTO_RX_FS_20DIV,,FLEXSPI2_B_DATA0,,,,ALT5 +GPIO_EMC_14,SEMC_ADDR05,XBAR_INOUT19,LPUART3_RXD,MQS_LEFT,LPSPI2_PCS1,GPIO4_IO14,USB_PHY2_TSTO_RX_FS_ELCH,,FLEXSPI2_B_DATA1,,,,ALT5 +GPIO_EMC_15,SEMC_ADDR06,XBAR_INOUT20,LPUART3_CTS_B,SPDIF_OUT,TMR3_TIMER0,GPIO4_IO15,USB_PHY2_TSTO_RX_FS_ON_DET,,FLEXSPI2_B_DATA2,,,,ALT5 +GPIO_EMC_16,SEMC_ADDR07,XBAR_INOUT21,LPUART3_RTS_B,SPDIF_IN,TMR3_TIMER1,GPIO4_IO16,,,FLEXSPI2_B_DATA3,,,,ALT5 +GPIO_EMC_17,SEMC_ADDR08,FLEXPWM4_PWM3_A,LPUART4_CTS_B,FLEXCAN1_TX,TMR3_TIMER2,GPIO4_IO17,,,,,,,ALT5 +GPIO_EMC_18,SEMC_ADDR09,FLEXPWM4_PWM3_B,LPUART4_RTS_B,FLEXCAN1_RX,TMR3_TIMER3,GPIO4_IO18,SNVS_VIO_5_CTL,,,,,,ALT5 +GPIO_EMC_19,SEMC_ADDR11,FLEXPWM2_PWM3_A,LPUART4_TXD,ENET_RX_DATA1,TMR2_TIMER0,GPIO4_IO19,SNVS_VIO_5_B,,,,,,ALT5 +GPIO_EMC_20,SEMC_ADDR12,FLEXPWM2_PWM3_B,LPUART4_RXD,ENET_RX_DATA0,TMR2_TIMER1,GPIO4_IO20,,,,,,,ALT5 +GPIO_EMC_21,SEMC_BA0,FLEXPWM3_PWM3_A,LPI2C3_SDA,ENET_TX_DATA1,TMR2_TIMER2,GPIO4_IO21,,,,,,,ALT5 +GPIO_EMC_22,SEMC_BA1,FLEXPWM3_PWM3_B,LPI2C3_SCL,ENET_TX_DATA0,TMR2_TIMER3,GPIO4_IO22,,,FLEXSPI2_A_SS1_B,,,,ALT5 +GPIO_EMC_23,SEMC_ADDR10,FLEXPWM1_PWM0_A,LPUART5_TXD,ENET_RX_EN,GPT1_CAPTURE2,GPIO4_IO23,,,FLEXSPI2_A_DQS,,,,ALT5 +GPIO_EMC_24,SEMC_CAS,FLEXPWM1_PWM0_B,LPUART5_RXD,ENET_TX_EN,GPT1_CAPTURE1,GPIO4_IO24,,,FLEXSPI2_A_SS0_B,,,,ALT5 +GPIO_EMC_25,SEMC_RAS,FLEXPWM1_PWM1_A,LPUART6_TXD,ENET_TX_CLK,ENET_REF_CLK,GPIO4_IO25,,,FLEXSPI2_A_SCLK,,,,ALT5 +GPIO_EMC_26,SEMC_CLK,FLEXPWM1_PWM1_B,LPUART6_RXD,ENET_RX_ER,FLEXIO1_D12,GPIO4_IO26,,,FLEXSPI2_A_DATA0,,,,ALT5 +GPIO_EMC_27,SEMC_CKE,FLEXPWM1_PWM2_A,LPUART5_RTS_B,LPSPI1_SCK,FLEXIO1_D13,GPIO4_IO27,,,FLEXSPI2_A_DATA1,,,,ALT5 +GPIO_EMC_28,SEMC_WE,FLEXPWM1_PWM2_B,LPUART5_CTS_B,LPSPI1_SOUT,FLEXIO1_D14,GPIO4_IO28,,,FLEXSPI2_A_DATA2,,,,ALT5 +GPIO_EMC_29,SEMC_CS0,FLEXPWM3_PWM0_A,LPUART6_RTS_B,LPSPI1_SIN,FLEXIO1_D15,GPIO4_IO29,,,FLEXSPI2_A_DATA3,,,,ALT5 +GPIO_EMC_30,SEMC_DATA08,FLEXPWM3_PWM0_B,LPUART6_CTS_B,LPSPI1_PCS0,CSI_DATA23,GPIO4_IO30,,,ENET2_TDATA0,,,,ALT5 +GPIO_EMC_31,SEMC_DATA09,FLEXPWM3_PWM1_A,LPUART7_TXD,LPSPI1_PCS1,CSI_DATA22,GPIO4_IO31,,,ENET2_TDATA1,,,,ALT5 +GPIO_EMC_32,SEMC_DATA10,FLEXPWM3_PWM1_B,LPUART7_RXD,CCM_PMIC_READY,CSI_DATA21,GPIO3_IO18,,,ENET2_TX_EN,,,,ALT5 +GPIO_EMC_33,SEMC_DATA11,FLEXPWM3_PWM2_A,USDHC1_RESET_B,SAI3_RX_DATA,CSI_DATA20,GPIO3_IO19,,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_EMC_34,SEMC_DATA12,FLEXPWM3_PWM2_B,USDHC1_VSELECT,SAI3_RX_SYNC,CSI_DATA19,GPIO3_IO20,,,ENET2_RX_ER,,,,ALT5 +GPIO_EMC_35,SEMC_DATA13,XBAR_INOUT18,GPT1_COMPARE1,SAI3_RX_BCLK,CSI_DATA18,GPIO3_IO21,USDHC1_CD_B,,ENET2_RDATA0,,,,ALT5 +GPIO_EMC_36,SEMC_DATA14,XBAR_INOUT22,GPT1_COMPARE2,SAI3_TX_DATA,CSI_DATA17,GPIO3_IO22,USDHC1_WP,,ENET2_RDATA1,FLEXCAN3_TX,,,ALT5 +GPIO_EMC_37,SEMC_DATA15,XBAR_INOUT23,GPT1_COMPARE3,SAI3_MCLK,CSI_DATA16,GPIO3_IO23,USDHC2_WP,,ENET2_RX_EN,FLEXCAN3_RX,,,ALT5 +GPIO_EMC_38,SEMC_DM1,FLEXPWM1_PWM3_A,LPUART8_TXD,SAI3_TX_BCLK,CSI_FIELD,GPIO3_IO24,USDHC2_VSELECT,,ENET2_MDC,,,,ALT5 +GPIO_EMC_39,SEMC_DQS,FLEXPWM1_PWM3_B,LPUART8_RXD,SAI3_TX_SYNC,WDOG1_B,GPIO3_IO25,USDHC2_CD_B,,ENET2_MDIO,,,,ALT5 +GPIO_EMC_40,SEMC_RDY,GPT2_CAPTURE2,LPSPI1_PCS2,USB_OTG2_OC,ENET_MDC,GPIO3_IO26,USDHC2_RESET_B,,,,,,ALT5 +GPIO_EMC_41,SEMC_CSX0,GPT2_CAPTURE1,LPSPI1_PCS3,USB_OTG2_PWR,ENET_MDIO,GPIO3_IO27,USDHC1_VSELECT,,,,,,ALT5 +GPIO_SD_B0_00,USDHC1_CMD,FLEXPWM1_PWM0_A,LPI2C3_SCL,XBAR_INOUT04,LPSPI1_SCK,GPIO3_IO12,FLEXSPI_A_SS1_B,,ENET2_TX_EN,,,,ALT5 +GPIO_SD_B0_01,USDHC1_CLK,FLEXPWM1_PWM0_B,LPI2C3_SDA,XBAR_INOUT05,LPSPI1_PCS0,GPIO3_IO13,FLEXSPI_B_SS1_B,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_SD_B0_02,USDHC1_DATA0,FLEXPWM1_PWM1_A,LPUART8_CTS_B,XBAR_INOUT06,LPSPI1_SOUT,GPIO3_IO14,,,ENET2_RX_ER,,,,ALT5 +GPIO_SD_B0_03,USDHC1_DATA1,FLEXPWM1_PWM1_B,LPUART8_RTS_B,XBAR_INOUT07,LPSPI1_SIN,GPIO3_IO15,,,ENET2_RDATA0,,,,ALT5 +GPIO_SD_B0_04,USDHC1_DATA2,FLEXPWM1_PWM2_A,LPUART8_TXD,XBAR_INOUT08,FLEXSPI_B_SS0_B,GPIO3_IO16,CCM_CLKO1,,ENET2_RDATA1,,,,ALT5 +GPIO_SD_B0_05,USDHC1_DATA3,FLEXPWM1_PWM2_B,LPUART8_RXD,XBAR_INOUT09,FLEXSPI_B_DQS,GPIO3_IO17,CCM_CLKO2,,ENET2_RX_EN,,,,ALT5 +GPIO_SD_B1_00,USDHC2_DATA3,FLEXSPI_B_DATA3,FLEXPWM1_PWM3_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",LPUART4_TXD,GPIO3_IO00,,,SAI3_RX_DATA,,,,ALT5 +GPIO_SD_B1_01,USDHC2_DATA2,FLEXSPI_B_DATA2,FLEXPWM1_PWM3_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",LPUART4_RXD,GPIO3_IO01,,,SAI3_TX_DATA,,,,ALT5 +GPIO_SD_B1_02,USDHC2_DATA1,FLEXSPI_B_DATA1,FLEXPWM2_PWM3_A,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXCAN1_TX,GPIO3_IO02,CCM_WAIT,,SAI3_TX_SYNC,,,,ALT5 +GPIO_SD_B1_03,USDHC2_DATA0,FLEXSPI_B_DATA0,FLEXPWM2_PWM3_B,SAI1_MCLK,FLEXCAN1_RX,GPIO3_IO03,CCM_PMIC_READY,,SAI3_TX_BCLK,,,,ALT5 +GPIO_SD_B1_04,USDHC2_CLK,FLEXSPI_B_SCLK,LPI2C1_SCL,SAI1_RX_SYNC,FLEXSPI_A_SS1_B,GPIO3_IO04,CCM_STOP,,SAI3_MCLK,,,,ALT5 +GPIO_SD_B1_05,USDHC2_CMD,FLEXSPI_A_DQS,LPI2C1_SDA,SAI1_RX_BCLK,FLEXSPI_B_SS0_B,GPIO3_IO05,,,SAI3_RX_SYNC,,,,ALT5 +GPIO_SD_B1_06,USDHC2_RESET_B,FLEXSPI_A_SS0_B,LPUART7_CTS_B,SAI1_RX_DATA0,LPSPI2_PCS0,GPIO3_IO06,,,SAI3_RX_BCLK,,,,ALT5 +GPIO_SD_B1_07,SEMC_CSX1,FLEXSPI_A_SCLK,LPUART7_RTS_B,SAI1_TX_DATA0,LPSPI2_SCK,GPIO3_IO07,,,,,,,ALT5 +GPIO_SD_B1_08,USDHC2_DATA4,FLEXSPI_A_DATA0,LPUART7_TXD,SAI1_TX_BCLK,LPSPI2_SOUT,GPIO3_IO08,SEMC_CSX2,,,,,,ALT5 +GPIO_SD_B1_09,USDHC2_DATA5,FLEXSPI_A_DATA1,LPUART7_RXD,SAI1_TX_SYNC,LPSPI2_SIN,GPIO3_IO09,,,,,,,ALT5 +GPIO_SD_B1_10,USDHC2_DATA6,FLEXSPI_A_DATA2,LPUART2_RXD,LPI2C2_SDA,LPSPI2_PCS2,GPIO3_IO10,,,,,,,ALT5 +GPIO_SD_B1_11,USDHC2_DATA7,FLEXSPI_A_DATA3,LPUART2_TXD,LPI2C2_SCL,LPSPI2_PCS3,GPIO3_IO11,,,,,,,ALT5 diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h index e05c8824d26ce..3eddb4102142e 100644 --- a/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h @@ -3,6 +3,6 @@ // MIMXRT1064_EVK has 1 user LED -#define MICROPY_HW_LED1_PIN (GPIO_AD_B0_09) +#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_09) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin)) diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c deleted file mode 100644 index d5da9c6f99275..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_AD_B0_09_af[] = { - PIN_AF(GPIO1_IO09, PIN_AF_MODE_ALT5, GPIO1, 0x10B0U), -}; - -pin_obj_t GPIO_AD_B0_09 = PIN(GPIO_AD_B0_09, GPIO1, 9, GPIO_AD_B0_09_af); diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.csv b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.csv new file mode 100644 index 0000000000000..afd63d4f97fe8 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.csv @@ -0,0 +1,51 @@ +D0,GPIO_AD_B1_07 +D1,GPIO_AD_B1_06 +D2,GPIO_AD_B0_11 +D3,GPIO_AD_B1_08 +D4,GPIO_AD_B0_09 +D5,GPIO_AD_B0_10 +D6,GPIO_AD_B1_02 +D7,GPIO_AD_B1_03 +D8,GPIO_AD_B0_03 +D9,GPIO_AD_B0_02 +D10,GPIO_SD_B0_01 +D11,GPIO_SD_B0_02 +D12,GPIO_SD_B0_03 +D13,GPIO_SD_B0_00 +D14,GPIO_AD_B1_01 +D15,GPIO_AD_B1_00 +A0,GPIO_AD_B1_10 +A1,GPIO_AD_B1_11 +A2,GPIO_AD_B1_04 +A3,GPIO_AD_B1_05 +A4,GPIO_AD_B1_01 +A5,GPIO_AD_B1_00 +RX,GPIO_AD_B1_07 +TX,GPIO_AD_B1_06 +SCL,GPIO_AD_B1_00 +SDA,GPIO_AD_B1_01 +SCK,GPIO_SD_B0_00 +SDI,GPIO_SD_B0_03 +SDO,GPIO_SD_B0_02 +CS,GPIO_SD_B0_01 +LED_GREEN,GPIO_AD_B0_09 +GPIO_AD_B1_07,GPIO_AD_B1_07 +GPIO_AD_B1_06,GPIO_AD_B1_06 +GPIO_AD_B0_11,GPIO_AD_B0_11 +GPIO_AD_B1_08,GPIO_AD_B1_08 +GPIO_AD_B0_09,GPIO_AD_B0_09 +GPIO_AD_B0_10,GPIO_AD_B0_10 +GPIO_AD_B1_02,GPIO_AD_B1_02 +GPIO_AD_B1_03,GPIO_AD_B1_03 +GPIO_AD_B0_03,GPIO_AD_B0_03 +GPIO_AD_B0_02,GPIO_AD_B0_02 +GPIO_SD_B0_01,GPIO_SD_B0_01 +GPIO_SD_B0_02,GPIO_SD_B0_02 +GPIO_SD_B0_03,GPIO_SD_B0_03 +GPIO_SD_B0_00,GPIO_SD_B0_00 +GPIO_AD_B1_01,GPIO_AD_B1_01 +GPIO_AD_B1_00,GPIO_AD_B1_00 +GPIO_AD_B1_10,GPIO_AD_B1_10 +GPIO_AD_B1_11,GPIO_AD_B1_11 +GPIO_AD_B1_04,GPIO_AD_B1_04 +GPIO_AD_B1_05,GPIO_AD_B1_05 diff --git a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h b/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h deleted file mode 100644 index baef51c6c8612..0000000000000 --- a/ports/mimxrt/boards/MIMXRT1064_EVK/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 NXP - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_AD_B0_09; diff --git a/ports/mimxrt/boards/MIMXRT1064_af.csv b/ports/mimxrt/boards/MIMXRT1064_af.csv new file mode 100644 index 0000000000000..074a3f69ab3b4 --- /dev/null +++ b/ports/mimxrt/boards/MIMXRT1064_af.csv @@ -0,0 +1,125 @@ +Pad,ALT0, ALT1, ALT2, ALT3, ALT4, ALT5, ALT6, ALT7, ALT8, ALT9,ADC,ACMP,Default +GPIO_AD_B0_00,FLEXPWM2_PWM3_A,XBAR_INOUT14,REF_CLK_32K,USB_OTG2_ID,LPI2C1_SCLS,GPIO1_IO00,USDHC1_RESET_B,LPSPI3_SCK,,,,ACMP1_IN4,ALT5 +GPIO_AD_B0_01,FLEXPWM2_PWM3_B,XBAR_INOUT15,REF_CLK_24M,USB_OTG1_ID,LPI2C1_SDAS,GPIO1_IO01,EWM_OUT_B,LPSPI3_SOUT,,,,ACMP2_IN4,ALT5 +GPIO_AD_B0_02,FLEXCAN2_TX,XBAR_INOUT16,LPUART6_TXD,USB_OTG1_PWR,FLEXPWM1_PWM0_X,GPIO1_IO02,LPI2C1_HREQ,LPSPI3_SIN,,,,ACMP3_IN4,ALT5 +GPIO_AD_B0_03,FLEXCAN2_RX,XBAR_INOUT17,LPUART6_RXD,USB_OTG1_OC,FLEXPWM1_PWM1_X,GPIO1_IO03,REF_CLK_24M,LPSPI3_PCS0,,,,ACMP4_IN4,ALT5 +GPIO_AD_B0_04,SRC_BOOT_MODE0,MQS_RIGHT,ENET_TX_DATA3,SAI2_TX_SYNC,CSI_DATA09,GPIO1_IO04,PIT_TRIGGER0,LPSPI3_PCS1,,,,,ALT0 +GPIO_AD_B0_05,SRC_BOOT_MODE1,MQS_LEFT,ENET_TX_DATA2,SAI2_TX_BCLK,CSI_DATA08,GPIO1_IO05,XBAR_INOUT17,LPSPI3_PCS2,,,,,ALT0 +GPIO_AD_B0_06,"JTAG_TMS,SWD_DIO",GPT2_COMPARE1,ENET_RX_CLK,SAI2_RX_BCLK,CSI_DATA07,GPIO1_IO06,XBAR_INOUT18,LPSPI3_PCS3,,,,,ALT0 +GPIO_AD_B0_07,"JTAG_TCK,SWD_CLK",GPT2_COMPARE2,ENET_TX_ER,SAI2_RX_SYNC,CSI_DATA06,GPIO1_IO07,XBAR_INOUT19,ENET_1588_EVENT3_OUT,,,,,ALT0 +GPIO_AD_B0_08,JTAG_MOD,GPT2_COMPARE3,ENET_RX_DATA3,SAI2_RX_DATA,CSI_DATA05,GPIO1_IO08,XBAR_INOUT20,ENET_1588_EVENT3_IN,,,,,ALT0 +GPIO_AD_B0_09,JTAG_TDI,FLEXPWM2_PWM3_A,ENET_RX_DATA2,SAI2_TX_DATA,CSI_DATA04,GPIO1_IO09,XBAR_INOUT21,GPT2_CLK,,SEMC_DQS4,,,ALT0 +GPIO_AD_B0_10,JTAG_TDO,FLEXPWM1_PWM3_A,ENET_CRS,SAI2_MCLK,CSI_DATA03,GPIO1_IO10,XBAR_INOUT22,ENET_1588_EVENT0_OUT,FLEXCAN3_TX,ARM_TRACE_SWO,,,ALT0 +GPIO_AD_B0_11,JTAG_TRSTB,FLEXPWM1_PWM3_B,ENET_COL,WDOG1_B,CSI_DATA02,GPIO1_IO11,XBAR_INOUT23,ENET_1588_EVENT0_IN,FLEXCAN3_RX,SEMC_CLK6,,,ALT0 +GPIO_AD_B0_12,LPI2C4_SCL,CCM_PMIC_READY,LPUART1_TXD,WDOG2_B,FLEXPWM1_PWM2_X,GPIO1_IO12,ENET_1588_EVENT1_OUT,NMI_GLUE_NMI,,,ADC1_IN1,,ALT5 +GPIO_AD_B0_13,LPI2C4_SDA,GPT1_CLK,LPUART1_RXD,EWM_OUT_B,FLEXPWM1_PWM3_X,GPIO1_IO13,ENET_1588_EVENT1_IN,REF_CLK_24M,,,ADC1_IN2,ACMP1_IN2,ALT5 +GPIO_AD_B0_14,USB_OTG2_OC,XBAR_INOUT24,LPUART1_CTS_B,ENET_1588_EVENT0_OUT,CSI_VSYNC,GPIO1_IO14,FLEXCAN2_TX,WDOG1_ANY,FLEXCAN3_TX,,ADC1_IN3,ACMP2_IN2,ALT5 +GPIO_AD_B0_15,USB_OTG2_PWR,XBAR_INOUT25,LPUART1_RTS_B,ENET_1588_EVENT0_IN,CSI_HSYNC,GPIO1_IO15,FLEXCAN2_RX,WDOG1_RESET_B_DEB,FLEXCAN3_RX,,ADC1_IN4,ACMP3_IN2,ALT5 +GPIO_AD_B1_00,USB_OTG2_ID,TMR3_TIMER0,LPUART2_CTS_B,LPI2C1_SCL,WDOG1_B,GPIO1_IO16,USDHC1_WP,KPP_ROW7,ENET2_1588_EVENT0_OUT,FLEXIO3_D00,"ADC1_IN5,ADC2_IN5",ACMP4_IN2,ALT5 +GPIO_AD_B1_01,USB_OTG1_PWR,TMR3_TIMER1,LPUART2_RTS_B,LPI2C1_SDA,CCM_PMIC_READY,GPIO1_IO17,USDHC1_VSELECT,KPP_COL7,ENET2_1588_EVENT0_IN,FLEXIO3_D01,"ADC1_IN6,ADC2_IN6","ACMP1_IN0,ACMP2_IN0,ACMP3_IN0,ACMP4_IN0",ALT5 +GPIO_AD_B1_02,USB_OTG1_ID,TMR3_TIMER2,LPUART2_TXD,SPDIF_OUT,ENET_1588_EVENT2_OUT,GPIO1_IO18,USDHC1_CD_B,KPP_ROW6,GPT2_CLK,FLEXIO3_D02,"ADC1_IN7,ADC2_IN7",ACMP1_IN3,ALT5 +GPIO_AD_B1_03,USB_OTG1_OC,TMR3_TIMER3,LPUART2_RXD,SPDIF_IN,ENET_1588_EVENT2_IN,GPIO1_IO19,USDHC2_CD_B,KPP_COL6,GPT2_CAPTURE1,FLEXIO3_D03,"ADC1_IN8,ADC2_IN8",ACMP2_IN3,ALT5 +GPIO_AD_B1_04,FLEXSPI_B_DATA3,ENET_MDC,LPUART3_CTS_B,SPDIF_SR_CLK,CSI_PIXCLK,GPIO1_IO20,USDHC2_DATA0,KPP_ROW5,GPT2_CAPTURE2,FLEXIO3_D04,"ADC1_IN9,ADC2_IN9",ACMP3_IN3,ALT5 +GPIO_AD_B1_05,FLEXSPI_B_DATA2,ENET_MDIO,LPUART3_RTS_B,SPDIF_OUT,CSI_MCLK,GPIO1_IO21,USDHC2_DATA1,KPP_COL5,GPT2_COMPARE1,FLEXIO3_D05,"ADC1_IN10,ADC2_IN10",ACMP4_IN3,ALT5 +GPIO_AD_B1_06,FLEXSPI_B_DATA1,LPI2C3_SDA,LPUART3_TXD,SPDIF_LOCK,CSI_VSYNC,GPIO1_IO22,USDHC2_DATA2,KPP_ROW4,GPT2_COMPARE2,FLEXIO3_D06,"ADC1_IN11,ADC2_IN11","ACMP1_IN1,ACMP2_IN1,ACMP3_IN1,ACMP4_IN1",ALT5 +GPIO_AD_B1_07,FLEXSPI_B_DATA0,LPI2C3_SCL,LPUART3_RXD,SPDIF_EXT_CLK,CSI_HSYNC,GPIO1_IO23,USDHC2_DATA3,KPP_COL4,GPT2_COMPARE3,FLEXIO3_D07,"ADC1_IN12,ADC2_IN12",ACMP1_IN5,ALT5 +GPIO_AD_B1_08,FLEXSPI_A_SS1_B,FLEXPWM4_PWM0_A,FLEXCAN1_TX,CCM_PMIC_READY,CSI_DATA09,GPIO1_IO24,USDHC2_CMD,KPP_ROW3,,FLEXIO3_D08,"ADC1_IN13,ADC2_IN13",ACMP2_IN5,ALT5 +GPIO_AD_B1_09,FLEXSPI_A_DQS,FLEXPWM4_PWM1_A,FLEXCAN1_RX,SAI1_MCLK,CSI_DATA08,GPIO1_IO25,USDHC2_CLK,KPP_COL3,,FLEXIO3_D09,"ADC1_IN14,ADC2_IN14",ACMP3_IN5,ALT5 +GPIO_AD_B1_10,FLEXSPI_A_DATA3,WDOG1_B,LPUART8_TXD,SAI1_RX_SYNC,CSI_DATA07,GPIO1_IO26,USDHC2_WP,KPP_ROW2,ENET2_1588_EVENT1_OUT,FLEXIO3_D10,"ADC1_IN15,ADC2_IN15",ACMP4_IN5,ALT5 +GPIO_AD_B1_11,FLEXSPI_A_DATA2,EWM_OUT_B,LPUART8_RXD,SAI1_RX_BCLK,CSI_DATA06,GPIO1_IO27,USDHC2_RESET_B,KPP_COL2,ENET2_1588_EVENT1_IN,FLEXIO3_D11,"ADC1_IN0,ADC2_IN0",ACMP1_IN6,ALT5 +GPIO_AD_B1_12,FLEXSPI_A_DATA1,ACMP_OUT00,LPSPI3_PCS0,SAI1_RX_DATA0,CSI_DATA05,GPIO1_IO28,USDHC2_DATA4,KPP_ROW1,ENET2_1588_EVENT2_OUT,FLEXIO3_D12,ADC2_IN1,"ACMP1_OUT,ACMP2_IN6",ALT5 +GPIO_AD_B1_13,FLEXSPI_A_DATA0,ACMP_OUT01,LPSPI3_SIN,SAI1_TX_DATA0,CSI_DATA04,GPIO1_IO29,USDHC2_DATA5,KPP_COL1,ENET2_1588_EVENT2_IN,FLEXIO3_D13,ADC2_IN2,"ACMP2_OUT,ACMP3_IN6",ALT5 +GPIO_AD_B1_14,FLEXSPI_A_SCLK,ACMP_OUT02,LPSPI3_SOUT,SAI1_TX_BCLK,CSI_DATA03,GPIO1_IO30,USDHC2_DATA6,KPP_ROW0,ENET2_1588_EVENT3_OUT,FLEXIO3_D14,ADC2_IN3,"ACMP3_OUT,ACMP4_IN6",ALT5 +GPIO_AD_B1_15,FLEXSPI_A_SS0_B,AMCP_OUT03,LPSPI3_SCK,SAI1_TX_SYNC,CSI_DATA02,GPIO1_IO31,USDHC2_DATA7,KPP_COL0,ENET2_1588_EVENT3_IN,FLEXIO3_D15,ADC2_IN4,ACMP4_OUT,ALT5 +GPIO_B0_00,LCD_CLK,TMR1_TIMER0,MQS_RIGHT,LPSPI4_PCS0,FLEXIO2_D00,GPIO2_IO00,SEMC_CSX1,,ENET2_MDC,,,,ALT5 +GPIO_B0_01,LCD_ENABLE,TMR1_TIMER1,MQS_LEFT,LPSPI4_SIN,FLEXIO2_D01,GPIO2_IO01,SEMC_CSX2,,ENET2_MDIO,,,,ALT5 +GPIO_B0_02,LCD_HSYNC,TMR1_TIMER2,FLEXCAN1_TX,LPSPI4_SOUT,FLEXIO2_D02,GPIO2_IO02,SEMC_CSX3,,ENET2_1588_EVENT0_OUT,,,,ALT5 +GPIO_B0_03,LCD_VSYNC,TMR2_TIMER0,FLEXCAN1_RX,LPSPI4_SCK,FLEXIO2_D03,GPIO2_IO03,WDOG2_RESET_B_DEB,,ENET2_1588_EVENT0_IN,,,,ALT5 +GPIO_B0_04,LCD_DATA00,TMR2_TIMER1,LPI2C2_SCL,ARM_TRACE0,FLEXIO2_D04,GPIO2_IO04,SRC_BT_CFG00,,ENET2_TDATA3,,,,ALT5 +GPIO_B0_05,LCD_DATA01,TMR2_TIMER2,LPI2C2_SDA,ARM_TRACE1,FLEXIO2_D05,GPIO2_IO05,SRC_BT_CFG01,,ENET2_TDATA2,,,,ALT5 +GPIO_B0_06,LCD_DATA02,TMR3_TIMER0,FLEXPWM2_PWM0_A,ARM_TRACE2,FLEXIO2_D06,GPIO2_IO06,SRC_BT_CFG02,,ENET2_RX_CLK,,,,ALT5 +GPIO_B0_07,LCD_DATA03,TMR3_TIMER1,FLEXPWM2_PWM0_B,ARM_TRACE3,FLEXIO2_D07,GPIO2_IO07,SRC_BT_CFG03,,ENET2_TX_ER,,,,ALT5 +GPIO_B0_08,LCD_DATA04,TMR3_TIMER2,FLEXPWM2_PWM1_A,LPUART3_TXD,FLEXIO2_D08,GPIO2_IO08,SRC_BT_CFG04,,ENET2_RDATA3,,,,ALT5 +GPIO_B0_09,LCD_DATA05,TMR4_TIMER0,FLEXPWM2_PWM1_B,LPUART3_RXD,FLEXIO2_D09,GPIO2_IO09,SRC_BT_CFG05,,ENET2_RDATA2,,,,ALT5 +GPIO_B0_10,LCD_DATA06,TMR4_TIMER1,FLEXPWM2_PWM2_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",FLEXIO2_D10,GPIO2_IO10,SRC_BT_CFG06,,ENET2_CRS,,,,ALT5 +GPIO_B0_11,LCD_DATA07,TMR4_TIMER2,FLEXPWM2_PWM2_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",FLEXIO2_D11,GPIO2_IO11,SRC_BT_CFG07,,ENET2_COL,,,,ALT5 +GPIO_B0_12,LCD_DATA08,XBAR_INOUT10,ARM_TRACE_CLK,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXIO2_D12,GPIO2_IO12,SRC_BT_CFG08,,ENET2_TDATA0,,,,ALT5 +GPIO_B0_13,LCD_DATA09,XBAR_INOUT11,ARM_TRACE_SWO,SAI1_MCLK,FLEXIO2_D13,GPIO2_IO13,SRC_BT_CFG09,,ENET2_TDATA1,,,,ALT5 +GPIO_B0_14,LCD_DATA10,XBAR_INOUT12,ARM_EVENT0,SAI1_RX_SYNC,FLEXIO2_D14,GPIO2_IO14,SRC_BT_CFG10,,ENET2_TX_EN,ENET2_REF_CLK2,,,ALT5 +GPIO_B0_15,LCD_DATA11,XBAR_INOUT13,ARM_EVENT1,SAI1_RX_BCLK,FLEXIO2_D15,GPIO2_IO15,SRC_BT_CFG11,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_B1_00,LCD_DATA12,XBAR_INOUT14,LPUART4_TXD,SAI1_RX_DATA0,FLEXIO2_D16,GPIO2_IO16,FLEXPWM1_PWM3_A,,ENET2_RX_ER,FLEXIO3_D16,,,ALT5 +GPIO_B1_01,LCD_DATA13,XBAR_INOUT15,LPUART4_RXD,SAI1_TX_DATA0,FLEXIO2_D17,GPIO2_IO17,FLEXPWM1_PWM3_B,,ENET2_RDATA0,FLEXIO3_D17,,,ALT5 +GPIO_B1_02,LCD_DATA14,XBAR_INOUT16,LPSPI4_PCS2,SAI1_TX_BCLK,FLEXIO2_D18,GPIO2_IO18,FLEXPWM2_PWM3_A,,ENET2_RDATA1,FLEXIO3_D18,,,ALT5 +GPIO_B1_03,LCD_DATA15,XBAR_INOUT17,LPSPI4_PCS1,SAI1_TX_SYNC,FLEXIO2_D19,GPIO2_IO19,FLEXPWM2_PWM3_B,,ENET2_RX_EN,FLEXIO3_D19,,,ALT5 +GPIO_B1_04,LCD_DATA16,LPSPI4_PCS0,CSI_DATA15,ENET_RX_DATA0,FLEXIO2_D20,GPIO2_IO20,,,GPT1_CLK,FLEXIO3_D20,,,ALT5 +GPIO_B1_05,LCD_DATA17,LPSPI4_SIN,CSI_DATA14,ENET_RX_DATA1,FLEXIO2_D21,GPIO2_IO21,,,GPT1_CAPTURE1,FLEXIO3_D21,,,ALT5 +GPIO_B1_06,LCD_DATA18,LPSPI4_SOUT,CSI_DATA13,ENET_RX_EN,FLEXIO2_D22,GPIO2_IO22,,,GPT1_CAPTURE2,FLEXIO3_D22,,,ALT5 +GPIO_B1_07,LCD_DATA19,LPSPI4_SCK,CSI_DATA12,ENET_TX_DATA0,FLEXIO2_D23,GPIO2_IO23,,,GPT1_COMPARE1,FLEXIO3_D23,,,ALT5 +GPIO_B1_08,LCD_DATA20,TMR1_TIMER3,CSI_DATA11,ENET_TX_DATA1,FLEXIO2_D24,GPIO2_IO24,FLEXCAN2_TX,,GPT1_COMPARE2,FLEXIO3_D24,,,ALT5 +GPIO_B1_09,LCD_DATA21,TMR2_TIMER3,CSI_DATA10,ENET_TX_EN,FLEXIO2_D25,GPIO2_IO25,FLEXCAN2_RX,,GPT1_COMPARE3,FLEXIO3_D25,,,ALT5 +GPIO_B1_10,LCD_DATA22,TMR3_TIMER3,CSI_DATA00,ENET_TX_CLK,FLEXIO2_D26,GPIO2_IO26,ENET_REF_CLK,,,FLEXIO3_D26,,,ALT5 +GPIO_B1_11,LCD_DATA23,TMR4_TIMER3,CSI_DATA01,ENET_RX_ER,FLEXIO2_D27,GPIO2_IO27,LPSPI4_PCS3,,,FLEXIO3_D27,,,ALT5 +GPIO_B1_12,,LPUART5_TXD,CSI_PIXCLK,ENET_1588_EVENT0_IN,FLEXIO2_D28,GPIO2_IO28,USDHC1_CD_B,,,FLEXIO3_D28,,,ALT5 +GPIO_B1_13,WDOG1_B,LPUART5_RXD,CSI_VSYNC,ENET_1588_EVENT0_OUT,FLEXIO2_D29,GPIO2_IO29,USDHC1_WP,,,FLEXIO3_D29,,,ALT5 +GPIO_B1_14,ENET_MDC,FLEXPWM4_PWM2_A,CSI_HSYNC,XBAR_INOUT02,FLEXIO2_D30,GPIO2_IO30,USDHC1_VSELECT,,ENET2_TDATA0,FLEXIO3_D30,,,ALT5 +GPIO_B1_15,ENET_MDIO,FLEXPWM4_PWM3_A,CSI_MCLK,XBAR_INOUT03,FLEXIO2_D31,GPIO2_IO31,USDHC1_RESET_B,,ENET2_TDATA1,FLEXIO3_D31,,,ALT5 +GPIO_EMC_00,SEMC_DATA00,FLEXPWM4_PWM0_A,LPSPI2_SCK,XBAR_INOUT02,FLEXIO1_D00,GPIO4_IO0,USB_PHY1_TSTI_TX_LS_MODE,,,,,,ALT5 +GPIO_EMC_01,SEMC_DATA01,FLEXPWM4_PWM0_B,LPSPI2_PCS0,XBAR_INOUT03,FLEXIO1_D01,GPIO4_IO1,USB_PHY1_TSTI_TX_HS_MODE,JTAG_DE_B,,,,,ALT5 +GPIO_EMC_02,SEMC_DATA02,FLEXPWM4_PWM1_A,LPSPI2_SOUT,XBAR_INOUT04,FLEXIO1_D02,GPIO4_IO2,USB_PHY1_TSTI_TX_DN,,,,,,ALT5 +GPIO_EMC_03,SEMC_DATA03,FLEXPWM4_PWM1_B,LPSPI2_SIN,XBAR_INOUT05,FLEXIO1_D03,GPIO4_IO3,USB_PHY1_TSTO_RX_SQUELCH,,,,,,ALT5 +GPIO_EMC_04,SEMC_DATA04,FLEXPWM4_PWM2_A,SAI2_TX_DATA,XBAR_INOUT06,FLEXIO1_D04,GPIO4_IO4,USB_PHY1_TSTO_RX_DISCON_DET,,,,,,ALT5 +GPIO_EMC_05,SEMC_DATA05,FLEXPWM4_PWM2_B,SAI2_TX_SYNC,XBAR_INOUT07,FLEXIO1_D05,GPIO4_IO5,USB_PHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_06,SEMC_DATA06,FLEXPWM2_PWM0_A,SAI2_TX_BCLK,XBAR_INOUT08,FLEXIO1_D06,GPIO4_IO6,USB_PHY2_TSTO_RX_FS_RXD,,,,,,ALT5 +GPIO_EMC_07,SEMC_DATA07,FLEXPWM2_PWM0_B,SAI2_MCLK,XBAR_INOUT09,FLEXIO1_D07,GPIO4_IO7,USB_PHY1_TSTO_RX_HS_RXD,,,,,,ALT5 +GPIO_EMC_08,SEMC_DM0,FLEXPWM2_PWM1_A,SAI2_RX_DATA,XBAR_INOUT17,FLEXIO1_D08,GPIO4_IO8,USB_PHY1_TSTO_RX_HS_,,,,,,ALT5 +GPIO_EMC_09,SEMC_ADDR00,FLEXPWM2_PWM1_B,SAI2_RX_SYNC,FLEXCAN2_TX,FLEXIO1_D09,GPIO4_IO9,USB_PHY1_TSTI_TX_EN,,FLEXSPI2_B_SS1_B,,,,ALT5 +GPIO_EMC_10,SEMC_ADDR01,FLEXPWM2_PWM2_A,SAI2_RX_BCLK,FLEXCAN2_RX,FLEXIO1_D10,GPIO4_IO10,USB_PHY1_TSTI_TX_HIZ,,FLEXSPI2_B_SS0_B,,,,ALT5 +GPIO_EMC_11,SEMC_ADDR02,FLEXPWM2_PWM2_B,LPI2C4_SDA,USDHC2_RESET_B,FLEXIO1_D11,GPIO4_IO11,USB_PHY2_TSTO_RX_HS_RXD,,FLEXSPI2_B_DQS,,,,ALT5 +GPIO_EMC_12,SEMC_ADDR03,XBAR_INOUT24,LPI2C4_SCL,USDHC1_WP,FLEXPWM1_PWM3_A,GPIO4_IO12,USB_PHY1_TSTO_PLL_CLK20DIV,,FLEXSPI2_B_SCLK,,,,ALT5 +GPIO_EMC_13,SEMC_ADDR04,XBAR_INOUT25,LPUART3_TXD,MQS_RIGHT,FLEXPWM1_PWM3_B,GPIO4_IO13,USB_PHY2_TSTO_PLL_CLK20DIV,,FLEXSPI2_B_DATA0,,,,ALT5 +GPIO_EMC_14,SEMC_ADDR05,XBAR_INOUT19,LPUART3_RXD,MQS_LEFT,LPSPI2_PCS1,GPIO4_IO14,USB_PHY2_TSTO_RX_SQUELCH,,FLEXSPI2_B_DATA1,,,,ALT5 +GPIO_EMC_15,SEMC_ADDR06,XBAR_INOUT20,LPUART3_CTS_B,SPDIF_OUT,TMR3_TIMER0,GPIO4_IO15,USB_PHY2_TSTO_RX_DISCON_DET,,FLEXSPI2_B_DATA2,,,,ALT5 +GPIO_EMC_16,SEMC_ADDR07,XBAR_INOUT21,LPUART3_RTS_B,SPDIF_IN,TMR3_TIMER1,GPIO4_IO16,,,FLEXSPI2_B_DATA3,,,,ALT5 +GPIO_EMC_17,SEMC_ADDR08,FLEXPWM4_PWM3_A,LPUART4_CTS_B,FLEXCAN1_TX,TMR3_TIMER2,GPIO4_IO17,,,,,,,ALT5 +GPIO_EMC_18,SEMC_ADDR09,FLEXPWM4_PWM3_B,LPUART4_RTS_B,FLEXCAN1_RX,TMR3_TIMER3,GPIO4_IO18,SNVS_VIO_5_CTL,,,,,,ALT5 +GPIO_EMC_19,SEMC_ADDR11,FLEXPWM2_PWM3_A,LPUART4_TXD,ENET_RX_DATA1,TMR2_TIMER0,GPIO4_IO19,SNVS_VIO_5_B,,,,,,ALT5 +GPIO_EMC_20,SEMC_ADDR12,FLEXPWM2_PWM3_B,LPUART4_RXD,ENET_RX_DATA0,TMR2_TIMER1,GPIO4_IO20,,,,,,,ALT5 +GPIO_EMC_21,SEMC_BA0,FLEXPWM3_PWM3_A,LPI2C3_SDA,ENET_TX_DATA1,TMR2_TIMER2,GPIO4_IO21,,,,,,,ALT5 +GPIO_EMC_22,SEMC_BA1,FLEXPWM3_PWM3_B,LPI2C3_SCL,ENET_TX_DATA0,TMR2_TIMER3,GPIO4_IO22,,,FLEXSPI2_A_SS1_B,,,,ALT5 +GPIO_EMC_23,SEMC_ADDR10,FLEXPWM1_PWM0_A,LPUART5_TXD,ENET_RX_EN,GPT1_CAPTURE2,GPIO4_IO23,,,FLEXSPI2_A_DQS,,,,ALT5 +GPIO_EMC_24,SEMC_CAS,FLEXPWM1_PWM0_B,LPUART5_RXD,ENET_TX_EN,GPT1_CAPTURE1,GPIO4_IO24,,,FLEXSPI2_A_SS0_B,,,,ALT5 +GPIO_EMC_25,SEMC_RAS,FLEXPWM1_PWM1_A,LPUART6_TXD,ENET_TX_CLK,ENET_REF_CLK,GPIO4_IO25,,,FLEXSPI2_A_SCLK,,,,ALT5 +GPIO_EMC_26,SEMC_CLK,FLEXPWM1_PWM1_B,LPUART6_RXD,ENET_RX_ER,FLEXIO1_D12,GPIO4_IO26,,,FLEXSPI2_A_DATA0,,,,ALT5 +GPIO_EMC_27,SEMC_CKE,FLEXPWM1_PWM2_A,LPUART5_RTS_B,LPSPI1_SCK,FLEXIO1_D13,GPIO4_IO27,,,FLEXSPI2_A_DATA1,,,,ALT5 +GPIO_EMC_28,SEMC_WE,FLEXPWM1_PWM2_B,LPUART5_CTS_B,LPSPI1_SOUT,FLEXIO1_D14,GPIO4_IO28,,,FLEXSPI2_A_DATA2,,,,ALT5 +GPIO_EMC_29,SEMC_CS0,FLEXPWM3_PWM0_A,LPUART6_RTS_B,LPSPI1_SIN,FLEXIO1_D15,GPIO4_IO29,,,FLEXSPI2_A_DATA3,,,,ALT5 +GPIO_EMC_30,SEMC_DATA08,FLEXPWM3_PWM0_B,LPUART6_CTS_B,LPSPI1_PCS0,CSI_DATA23,GPIO4_IO30,,,ENET2_TDATA0,,,,ALT5 +GPIO_EMC_31,SEMC_DATA09,FLEXPWM3_PWM1_A,LPUART7_TXD,LPSPI1_PCS1,CSI_DATA22,GPIO4_IO31,,,ENET2_TDATA1,,,,ALT5 +GPIO_EMC_32,SEMC_DATA10,FLEXPWM3_PWM1_B,LPUART7_RXD,CCM_PMIC_READY,CSI_DATA21,GPIO3_IO18,,,ENET2_TX_EN,,,,ALT5 +GPIO_EMC_33,SEMC_DATA11,FLEXPWM3_PWM2_A,USDHC1_RESET_B,SAI3_RX_DATA,CSI_DATA20,GPIO3_IO19,,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_EMC_34,SEMC_DATA12,FLEXPWM3_PWM2_B,USDHC1_VSELECT,SAI3_RX_SYNC,CSI_DATA19,GPIO3_IO20,,,ENET2_RX_ER,,,,ALT5 +GPIO_EMC_35,SEMC_DATA13,XBAR_INOUT18,GPT1_COMPARE1,SAI3_RX_BCLK,CSI_DATA18,GPIO3_IO21,USDHC1_CD_B,,ENET2_RDATA0,,,,ALT5 +GPIO_EMC_36,SEMC_DATA14,XBAR_INOUT22,GPT1_COMPARE2,SAI3_TX_DATA,CSI_DATA17,GPIO3_IO22,USDHC1_WP,,ENET2_RDATA1,FLEXCAN3_TX,,,ALT5 +GPIO_EMC_37,SEMC_DATA15,XBAR_INOUT23,GPT1_COMPARE3,SAI3_MCLK,CSI_DATA16,GPIO3_IO23,USDHC2_WP,,ENET2_RX_EN,FLEXCAN3_RX,,,ALT5 +GPIO_EMC_38,SEMC_DM1,FLEXPWM1_PWM3_A,LPUART8_TXD,SAI3_TX_BCLK,CSI_FIELD,GPIO3_IO24,USDHC2_VSELECT,,ENET2_MDC,,,,ALT5 +GPIO_EMC_39,SEMC_DQS,FLEXPWM1_PWM3_B,LPUART8_RXD,SAI3_TX_SYNC,WDOG1_B,GPIO3_IO25,USDHC2_CD_B,,ENET2_MDIO,,,,ALT5 +GPIO_EMC_40,SEMC_RDY,GPT2_CAPTURE2,LPSPI1_PCS2,USB_OTG2_OC,ENET_MDC,GPIO3_IO26,USDHC2_RESET_B,,,SEMC_CLK5,,,ALT5 +GPIO_EMC_41,SEMC_CSX0,GPT2_CAPTURE1,LPSPI1_PCS3,USB_OTG2_PWR,ENET_MDIO,GPIO3_IO27,USDHC1_VSELECT,,,,,,ALT5 +GPIO_SD_B0_00,USDHC1_CMD,FLEXPWM1_PWM0_A,LPI2C3_SCL,XBAR_INOUT04,LPSPI1_SCK,GPIO3_IO12,FLEXSPI_A_SS1_B,,ENET2_TX_EN,SEMC_DQS4,,,ALT5 +GPIO_SD_B0_01,USDHC1_CLK,FLEXPWM1_PWM0_B,LPI2C3_SDA,XBAR_INOUT05,LPSPI1_PCS0,GPIO3_IO13,FLEXSPI_B_SS1_B,,ENET2_TX_CLK,ENET2_REF_CLK2,,,ALT5 +GPIO_SD_B0_02,USDHC1_DATA0,FLEXPWM1_PWM1_A,LPUART8_CTS_B,XBAR_INOUT06,LPSPI1_SOUT,GPIO3_IO14,,,ENET2_RX_ER,SEMC_CLK5,,,ALT5 +GPIO_SD_B0_03,USDHC1_DATA1,FLEXPWM1_PWM1_B,LPUART8_RTS_B,XBAR_INOUT07,LPSPI1_SIN,GPIO3_IO15,,,ENET2_RDATA0,SEMC_CLK6,,,ALT5 +GPIO_SD_B0_04,USDHC1_DATA2,FLEXPWM1_PWM2_A,LPUART8_TXD,XBAR_INOUT08,FLEXSPI_B_SS0_B,GPIO3_IO16,CCM_CLKO1,,ENET2_RDATA1,,,,ALT5 +GPIO_SD_B0_05,USDHC1_DATA3,FLEXPWM1_PWM2_B,LPUART8_RXD,XBAR_INOUT09,FLEXSPI_B_DQS,GPIO3_IO17,CCM_CLKO2,,ENET2_RX_EN,,,,ALT5 +GPIO_SD_B1_00,USDHC2_DATA3,FLEXSPI_B_DATA3,FLEXPWM1_PWM3_A,"SAI1_TX_DATA3,SAI1_RX_DATA1",LPUART4_TXD,GPIO3_IO00,,,SAI3_RX_DATA,,,,ALT5 +GPIO_SD_B1_01,USDHC2_DATA2,FLEXSPI_B_DATA2,FLEXPWM1_PWM3_B,"SAI1_TX_DATA2,SAI1_RX_DATA2",LPUART4_RXD,GPIO3_IO01,,,SAI3_TX_DATA,,,,ALT5 +GPIO_SD_B1_02,USDHC2_DATA1,FLEXSPI_B_DATA1,FLEXPWM2_PWM3_A,"SAI1_TX_DATA1,SAI1_RX_DATA3",FLEXCAN1_TX,GPIO3_IO02,CCM_WAIT,,SAI3_TX_SYNC,,,,ALT5 +GPIO_SD_B1_03,USDHC2_DATA0,FLEXSPI_B_DATA0,FLEXPWM2_PWM3_B,SAI1_MCLK,FLEXCAN1_RX,GPIO3_IO03,CCM_PMIC_READY,,SAI3_TX_BCLK,,,,ALT5 +GPIO_SD_B1_04,USDHC2_CLK,FLEXSPI_B_SCLK,LPI2C1_SCL,SAI1_RX_SYNC,FLEXSPI_A_SS1_B,GPIO3_IO04,CCM_STOP,,SAI3_MCLK,,,,ALT5 +GPIO_SD_B1_05,USDHC2_CMD,FLEXSPI_A_DQS,LPI2C1_SDA,SAI1_RX_BCLK,FLEXSPI_B_SS0_B,GPIO3_IO05,,,SAI3_RX_SYNC,,,,ALT5 +GPIO_SD_B1_06,USDHC2_RESET_B,FLEXSPI_A_SS0_B,LPUART7_CTS_B,SAI1_RX_DATA0,LPSPI2_PCS0,GPIO3_IO06,,,SAI3_RX_BCLK,,,,ALT5 +GPIO_SD_B1_07,SEMC_CSX1,FLEXSPI_A_SCLK,LPUART7_RTS_B,SAI1_TX_DATA0,LPSPI2_SCK,GPIO3_IO07,,,,,,,ALT5 +GPIO_SD_B1_08,USDHC2_DATA4,FLEXSPI_A_DATA0,LPUART7_TXD,SAI1_TX_BCLK,LPSPI2_SOUT,GPIO3_IO08,SEMC_CSX2,,,,,,ALT5 +GPIO_SD_B1_09,USDHC2_DATA5,FLEXSPI_A_DATA1,LPUART7_RXD,SAI1_TX_SYNC,LPSPI2_SIN,GPIO3_IO09,,,,,,,ALT5 +GPIO_SD_B1_10,USDHC2_DATA6,FLEXSPI_A_DATA2,LPUART2_RXD,LPI2C2_SDA,LPSPI2_PCS2,GPIO3_IO10,,,,,,,ALT5 +GPIO_SD_B1_11,USDHC2_DATA7,FLEXSPI_A_DATA3,LPUART2_TXD,LPI2C2_SCL,LPSPI2_PCS3,GPIO3_IO11,,,,,,,ALT5 diff --git a/ports/mimxrt/boards/TEENSY40/mpconfigboard.h b/ports/mimxrt/boards/TEENSY40/mpconfigboard.h index 7d564785797ad..e3d7c6a12943f 100644 --- a/ports/mimxrt/boards/TEENSY40/mpconfigboard.h +++ b/ports/mimxrt/boards/TEENSY40/mpconfigboard.h @@ -4,6 +4,6 @@ #define BOARD_FLASH_SIZE (2 * 1024 * 1024) // Teensy 4.0 has 1 board LED -#define MICROPY_HW_LED1_PIN (GPIO_B0_03) +#define MICROPY_HW_LED1_PIN (pin_GPIO_B0_03) #define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) #define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) diff --git a/ports/mimxrt/boards/TEENSY40/pins.c b/ports/mimxrt/boards/TEENSY40/pins.c deleted file mode 100644 index c7bfc102d4350..0000000000000 --- a/ports/mimxrt/boards/TEENSY40/pins.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "pin.h" - -static pin_af_obj_t GPIO_B0_03_af[] = { - PIN_AF(GPIO2_IO03, PIN_AF_MODE_ALT5, GPIO2, 0x10B0U), -}; - -pin_obj_t GPIO_B0_03 = PIN(GPIO_B0_03, GPIO2, 3, GPIO_B0_03_af); diff --git a/ports/mimxrt/boards/TEENSY40/pins.csv b/ports/mimxrt/boards/TEENSY40/pins.csv new file mode 100644 index 0000000000000..c9d7c9856ddce --- /dev/null +++ b/ports/mimxrt/boards/TEENSY40/pins.csv @@ -0,0 +1,55 @@ +D0,GPIO_AD_B0_03 +D1,GPIO_AD_B0_02 +D2,GPIO_EMC_04 +D3,GPIO_EMC_05 +D4,GPIO_EMC_06 +D5,GPIO_EMC_08 +D6,GPIO_B0_10 +D7,GPIO_B1_01 +D8,GPIO_B1_00 +D9,GPIO_B0_11 +D10,GPIO_B0_00 +D11,GPIO_B0_02 +D12,GPIO_B0_01 +D13,GPIO_B0_03 +D14,GPIO_AD_B1_02 +D15,GPIO_AD_B1_03 +D16,GPIO_AD_B1_07 +D17,GPIO_AD_B1_06 +D18,GPIO_AD_B1_01 +D19,GPIO_AD_B1_00 +D20,GPIO_AD_B1_10 +D21,GPIO_AD_B1_11 +D22,GPIO_AD_B1_08 +D23,GPIO_AD_B1_09 +D24,GPIO_AD_B0_12 +D25,GPIO_AD_B0_13 +D26,GPIO_AD_B1_14 +D27,GPIO_AD_B1_15 +D28,GPIO_EMC_32 +D29,GPIO_EMC_31 +D30,GPIO_EMC_37 +D31,GPIO_EMC_36 +D32,GPIO_B0_12 +D33,GPIO_EMC_07 +DAT1,GPIO_AD_B0_03 +DAT0,GPIO_AD_B0_02 +CLK,GPIO_AD_B0_01 +CMD,GPIO_ASD_B0_00 +DAT3,GPIO_SD_B0_05 +DAT2,GPIO_SD_B0_04 +A0,GPIO_AD_B1_02 +A1,GPIO_AD_B1_03 +A2,GPIO_AD_B1_07 +A3,GPIO_AD_B1_06 +A4,GPIO_AD_B1_01 +A5,GPIO_AD_B1_00 +A6,GPIO_AD_B1_10 +A7,GPIO_AD_B1_11 +A8,GPIO_AD_B1_08 +A9,GPIO_AD_B1_09 +A10,GPIO_AD_B0_12 +A11,GPIO_AD_B0_13 +A12,GPIO_AD_B1_14 +A13,GPIO_AD_B1_15 +LED,GPIO_B0_03 \ No newline at end of file diff --git a/ports/mimxrt/boards/TEENSY40/pins.h b/ports/mimxrt/boards/TEENSY40/pins.h deleted file mode 100644 index c0b6dcdfa9576..0000000000000 --- a/ports/mimxrt/boards/TEENSY40/pins.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Philipp Ebensberger - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// NOTE: pins.h shall only be included in in pin.h -// hence no include guards are needed since they will be provided by pin.h - -extern pin_obj_t GPIO_B0_03; diff --git a/ports/mimxrt/boards/make-pins.py b/ports/mimxrt/boards/make-pins.py new file mode 100644 index 0000000000000..5e5b81e31e01a --- /dev/null +++ b/ports/mimxrt/boards/make-pins.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python +"""Creates the pin file for the MIMXRT10xx.""" + +from __future__ import print_function + +import argparse +import sys +import csv + +SUPPORTED_AFS = {"GPIO"} +MAX_AF = 10 # AF0 .. AF9 + + +def parse_pad(pad_str): + """Parses a string and returns a (port, gpio_bit) tuple.""" + if len(pad_str) < 4: + raise ValueError("Expecting pad name to be at least 4 characters") + if pad_str[:4] != "GPIO": + raise ValueError("Expecting pad name to start with GPIO") + return pad_str + + +def af_supported(af_str): + for supported_af in SUPPORTED_AFS: + if af_str.startswith(supported_af): + return True + else: + return False + + +class Pin(object): + """Holds the information associated with a pin.""" + + def __init__(self, pad, gpio, pin, idx=0): + self.idx = idx + self.name = pad + self.pad = pad + self.gpio = gpio + self.pin = pin + self.alt_fn = [] + self.adc_channel = 0 + self.board_pin = False + + def set_is_board_pin(self): + self.board_pin = True + + def is_board_pin(self): + return self.board_pin + + def parse_adc(self, adc_str): + pass + + def parse_af(self, af_idx, af_strs_in): + pass + + def add_af(self, af): + self.alt_fn.append(af) + + def print_pin_af(self): + if self.alt_fn: + print( + "static const machine_pin_af_obj_t pin_{0}_af[{1}] = {{".format( + self.name, len(self.alt_fn) + ) + ) + for af in self.alt_fn: + af.print() + print("};") + else: + raise ValueError("Pin '{}' has no alternative functions".format(self.name)) + + def print(self): + if self.alt_fn: + self.print_pin_af() + print( + "const machine_pin_obj_t pin_{0} = PIN({0}, {1}, {2}, pin_{3});\n".format( + self.name, self.gpio, int(self.pin), self.name + "_af" + ) + ) + else: + raise ValueError("Pin '{}' has no alternative functions".format(self.name)) + + def print_header(self, hdr_file): + pass + + +class AlternateFunction(object): + """Holds the information associated with a pins alternate function.""" + + def __init__(self, idx, af_str): + self.idx = idx + self.af_str = af_str + self.instance = self.af_str.split("_")[0] + + def print(self): + """Prints the C representation of this AF.""" + print( + " PIN_AF({0}, PIN_AF_MODE_ALT{1}, {2}, {3}),".format( + self.af_str, self.idx, self.instance, "0x10B0U" + ) + ) + + +class NamedPin(object): + def __init__(self, name, pad, idx): + self.name = name + self.pad = pad + self.idx = idx + + +class Pins(object): + def __init__(self): + self.cpu_pins = [] + self.board_pins = [] + + def find_pin_by_num(self, pin_num): + for pin in self.cpu_pins: + if pin.pin_num == pin_num: + return pin + + def find_pin_by_name(self, pad): + for pin in self.cpu_pins: + if pin.pad == pad: + return pin + + def parse_board_file(self, filename): + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile) + for row in rows: + pin = self.find_pin_by_name(row[1]) + if pin and row[0]: # Only add board pins that have a name + self.board_pins.append(NamedPin(row[0], pin.pad, pin.idx)) + + def parse_af_file(self, filename, pad_col, af_start_col): + af_end_col = af_start_col + MAX_AF + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile) + header = next(rows) + for idx, row in enumerate(rows): + pad = row[pad_col] + gpio, pin = row[6].split("_") + pin_number = pin.lstrip("IO") + + pin = Pin(pad, gpio, pin_number, idx=idx) + + # Parse alternate functions + af_idx = 0 + for af_idx, af in enumerate(row[af_start_col:af_end_col]): + if af and af_supported(af): + pin.add_af(AlternateFunction(af_idx, af)) + + self.cpu_pins.append(pin) + + @staticmethod + def print_named(label, pins): + print("") + print( + "STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{".format(label) + ) + for pin in pins: + print( + " {{ MP_ROM_QSTR(MP_QSTR_{}), MP_ROM_PTR(&pin_{}) }},".format(pin.name, pin.pad) + ) + print("};") + print( + "MP_DEFINE_CONST_DICT(machine_pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);".format( + label, label + ) + ) + + def print(self): + # Print Pin Object declarations + for pin in self.cpu_pins: + pin.print() + + print("") + print("const machine_pin_obj_t* machine_pin_board_pins [] = {") + for pin in self.board_pins: + print(" &pin_{},".format(pin.pad)) + print("};") + print("const uint32_t num_board_pins = {:d};".format(len(self.board_pins))) + # Print Pin mapping dictionaries + self.print_named("cpu", self.cpu_pins) + self.print_named("board", self.board_pins) + print("") + + def print_header(self, hdr_filename): + with open(hdr_filename, "w") as hdr_file: + for pin in self.cpu_pins: + hdr_file.write("extern const machine_pin_obj_t pin_{};\n".format(pin.name)) + hdr_file.write("extern const machine_pin_obj_t* machine_pin_board_pins[];\n") + hdr_file.write("extern const uint32_t num_board_pins;\n") + hdr_file.write("extern const mp_obj_dict_t machine_pin_cpu_pins_locals_dict;\n") + hdr_file.write("extern const mp_obj_dict_t machine_pin_board_pins_locals_dict;\n") + + +def main(): + parser = argparse.ArgumentParser( + prog="make-pins.py", + usage="%(prog)s [options] [command]", + description="Generate board specific pin file", + ) + parser.add_argument( + "-a", + "--af", + dest="af_filename", + help="Specifies the alternate function file for the chip", + default="mimxrt1021_af.csv", + ) + parser.add_argument( + "-b", + "--board", + dest="board_filename", + help="Specifies the board file", + default="MIMXRT1020_EVK/pins.csv", + ) + parser.add_argument( + "-p", + "--prefix", + dest="prefix_filename", + help="Specifies beginning portion of generated pins file", + default="mimxrt_prefix.c", + ) + parser.add_argument( + "-r", + "--hdr", + dest="hdr_filename", + help="Specifies name of generated pin header file", + default="build/pins.h", + ) + + pins = Pins() + + # test code + args = parser.parse_args() + # + + if args.af_filename: + print("// --af {:s}".format(args.af_filename)) + pins.parse_af_file(args.af_filename, 0, 1) + + if args.board_filename: + print("// --board {:s}".format(args.board_filename)) + pins.parse_board_file(args.board_filename) + + if args.hdr_filename: + print("// --hdr {:s}".format(args.hdr_filename)) + + if args.prefix_filename: + print("// --prefix {:s}".format(args.prefix_filename)) + with open(args.prefix_filename, "r") as prefix_file: + print(prefix_file.read()) + + pins.print() + pins.print_header(args.hdr_filename) + + +if __name__ == "__main__": + main() diff --git a/ports/mimxrt/boards/mimxrt_prefix.c b/ports/mimxrt/boards/mimxrt_prefix.c new file mode 100644 index 0000000000000..59e338976e496 --- /dev/null +++ b/ports/mimxrt/boards/mimxrt_prefix.c @@ -0,0 +1,26 @@ +#include + +#include "py/obj.h" +#include "py/mphal.h" +#include "pin.h" + +#define PIN_AF(_name, _af_mode, _instance, _pad_config) \ + { \ + .base = { &machine_pin_af_type }, \ + .name = MP_QSTR_##_name, \ + .af_mode = (uint32_t)(_af_mode), \ + .instance = (void *)(_instance), \ + .pad_config = (uint32_t)(_pad_config), \ + } \ + +#define PIN(_name, _gpio, _pin, _af_list) \ + { \ + .base = { &machine_pin_type }, \ + .name = MP_QSTR_##_name, \ + .gpio = (_gpio), \ + .pin = (uint32_t)(_pin), \ + .muxRegister = (uint32_t)&(IOMUXC->SW_MUX_CTL_PAD[kIOMUXC_SW_MUX_CTL_PAD_##_name]), \ + .configRegister = (uint32_t)&(IOMUXC->SW_PAD_CTL_PAD[kIOMUXC_SW_PAD_CTL_PAD_##_name]), \ + .af_list_len = (size_t)(sizeof((_af_list)) / sizeof(machine_pin_af_obj_t)), \ + .af_list = (_af_list), \ + } \ diff --git a/ports/mimxrt/led.c b/ports/mimxrt/led.c index bec97dd8f7cc4..cb9f9854646e8 100644 --- a/ports/mimxrt/led.c +++ b/ports/mimxrt/led.c @@ -44,7 +44,7 @@ const machine_led_obj_t machine_led_obj[NUM_LEDS] = { void led_init(void) { // Turn off LEDs and initialize for (mp_int_t led = 0; led < NUM_LEDS; led++) { - const pin_obj_t *led_pin = machine_led_obj[led].led_pin; + const machine_pin_obj_t *led_pin = machine_led_obj[led].led_pin; gpio_pin_config_t pin_config = { .outputLogic = 1U, @@ -67,7 +67,7 @@ void led_state(machine_led_t led, int state) { return; } - const pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; + const machine_pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; if (state == 0) { // turn LED off @@ -83,7 +83,7 @@ void led_toggle(machine_led_t led) { return; } - const pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; + const machine_pin_obj_t *led_pin = machine_led_obj[led - 1].led_pin; mp_hal_pin_toggle(led_pin); } diff --git a/ports/mimxrt/led.h b/ports/mimxrt/led.h index 35418321d173d..a0628e90eddc9 100644 --- a/ports/mimxrt/led.h +++ b/ports/mimxrt/led.h @@ -42,7 +42,7 @@ typedef enum { typedef struct _machine_led_obj_t { mp_obj_base_t base; mp_uint_t led_id; - const pin_obj_t *led_pin; + const machine_pin_obj_t *led_pin; } machine_led_obj_t; void led_init(void); diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c new file mode 100644 index 0000000000000..583ee4a265192 --- /dev/null +++ b/ports/mimxrt/machine_pin.c @@ -0,0 +1,273 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Philipp Ebensberger + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include + +#include "fsl_gpio.h" +#include "fsl_iomuxc.h" + +#include "py/runtime.h" +#include "py/mphal.h" +#include "pin.h" +#include "mphalport.h" + +// Local functions +STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); + +// Class Methods +STATIC void machine_pin_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind); +STATIC mp_obj_t machine_pin_obj_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); +STATIC mp_obj_t machine_pin_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); + +// Instance Methods +STATIC mp_obj_t machine_pin_off(mp_obj_t self_in); +STATIC mp_obj_t machine_pin_on(mp_obj_t self_in); +STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args); +STATIC mp_obj_t machine_pin_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args); + +// Local data +enum { + PIN_INIT_ARG_MODE = 0, + PIN_INIT_ARG_PULL, + PIN_INIT_ARG_VALUE, + PIN_INIT_ARG_DRIVE, +}; + +// Pin mapping dictionaries +const mp_obj_type_t machine_pin_cpu_pins_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_cpu, + .locals_dict = (mp_obj_t)&machine_pin_cpu_pins_locals_dict, +}; + +const mp_obj_type_t machine_pin_board_pins_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_board, + .locals_dict = (mp_obj_t)&machine_pin_board_pins_locals_dict, +}; + +STATIC mp_obj_t machine_pin_obj_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); + machine_pin_obj_t *self = self_in; + + if (n_args == 0) { + return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self)); + } else { + mp_hal_pin_write(self, mp_obj_is_true(args[0])); + return mp_const_none; + } +} + +STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + static const mp_arg_t allowed_args[] = { + [PIN_INIT_ARG_MODE] { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT }, + [PIN_INIT_ARG_PULL] { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + [PIN_INIT_ARG_VALUE] { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + [PIN_INIT_ARG_DRIVE] { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PIN_DRIVE_POWER_3}}, + // TODO: Implement additional arguments + /* + { MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy + { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},*/ + }; + + // Parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get io mode + uint mode = args[PIN_INIT_ARG_MODE].u_int; + if (!IS_GPIO_MODE(mode)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode); + } + + // Handle configuration for GPIO + if ((mode == PIN_MODE_IN) || (mode == PIN_MODE_OUT) || (mode == PIN_MODE_OPEN_DRAIN)) { + gpio_pin_config_t pin_config; + const machine_pin_af_obj_t *af_obj; + uint32_t pad_config = 0UL; + uint8_t pull = PIN_PULL_DISABLED; + + // Generate pin configuration + if ((args[PIN_INIT_ARG_VALUE].u_obj != MP_OBJ_NULL) && (mp_obj_is_true(args[PIN_INIT_ARG_VALUE].u_obj))) { + pin_config.outputLogic = 1U; + } else { + pin_config.outputLogic = 0U; + } + pin_config.direction = mode == PIN_MODE_IN ? kGPIO_DigitalInput : kGPIO_DigitalOutput; + pin_config.interruptMode = kGPIO_NoIntmode; + + af_obj = pin_find_af(self, PIN_AF_MODE_ALT5); // GPIO is always ALT5 + if (af_obj == NULL) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("requested AF %d not available for pin %d"), PIN_AF_MODE_ALT5, mode); + } + + // Generate pad configuration + if (args[PIN_INIT_ARG_PULL].u_obj != mp_const_none) { + pull = (uint8_t)mp_obj_get_int(args[PIN_INIT_ARG_PULL].u_obj); + } + + pad_config |= IOMUXC_SW_PAD_CTL_PAD_SRE(0U); // Slow Slew Rate + pad_config |= IOMUXC_SW_PAD_CTL_PAD_SPEED(0b01); // medium(100MHz) + + if (mode == PIN_MODE_OPEN_DRAIN) { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_ODE(0b1); // Open Drain Enabled + } else { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_ODE(0b0); // Open Drain Disabled + } + + if (pull == PIN_PULL_DISABLED) { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_PKE(0); // Pull/Keeper Disabled + } else if (pull == PIN_PULL_HOLD) { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_PKE(1) | // Pull/Keeper Enabled + IOMUXC_SW_PAD_CTL_PAD_PUE(0); // Keeper selected + } else { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_PKE(1) | // Pull/Keeper Enabled + IOMUXC_SW_PAD_CTL_PAD_PUE(1) | // Pull selected + IOMUXC_SW_PAD_CTL_PAD_PUS(pull); + } + + if (mode == PIN_MODE_IN) { + pad_config |= IOMUXC_SW_PAD_CTL_PAD_DSE(0b000) | // output driver disabled + IOMUXC_SW_PAD_CTL_PAD_HYS(1U); // Hysteresis enabled + } else { + uint drive = args[PIN_INIT_ARG_DRIVE].u_int; + if (!IS_GPIO_DRIVE(drive)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid drive strength: %d"), drive); + } + + pad_config |= IOMUXC_SW_PAD_CTL_PAD_DSE(drive) | + IOMUXC_SW_PAD_CTL_PAD_HYS(0U); // Hysteresis disabled + } + + // Configure PAD as GPIO + IOMUXC_SetPinMux(self->muxRegister, af_obj->af_mode, 0, 0, self->configRegister, 1U); // Software Input On Field: Input Path is determined by functionality + IOMUXC_SetPinConfig(self->muxRegister, af_obj->af_mode, 0, 0, self->configRegister, pad_config); + GPIO_PinInit(self->gpio, self->pin, &pin_config); + } + + return mp_const_none; +} + +STATIC void machine_pin_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { + (void)kind; + const machine_pin_obj_t *self = MP_OBJ_TO_PTR(o); + mp_printf(print, "Pin(%s)", qstr_str(self->name)); +} + +// pin(id, mode, pull, ...) +STATIC mp_obj_t machine_pin_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + const machine_pin_obj_t *pin = pin_find(args[0]); + + if (n_args > 1 || n_kw > 0) { + // pin mode given, so configure this GPIO + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args); + } + + return (mp_obj_t)pin; +} + +// pin.off() +STATIC mp_obj_t machine_pin_off(mp_obj_t self_in) { + machine_pin_obj_t *self = self_in; + mp_hal_pin_low(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_off_obj, machine_pin_off); + +// pin.on() +STATIC mp_obj_t machine_pin_on(mp_obj_t self_in) { + machine_pin_obj_t *self = self_in; + mp_hal_pin_high(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on); + +// pin.value([value]) +STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { + return machine_pin_obj_call(args[0], (n_args - 1), 0, args + 1); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); + +// pin.init(mode, pull, [kwargs]) +STATIC mp_obj_t machine_pin_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args); +} +MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_init); + +STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { + // instance methods + { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) }, + { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) }, + { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_pin_off_obj) }, + { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_pin_on_obj) }, + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) }, + // class attributes + { MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&machine_pin_board_pins_obj_type) }, + { MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&machine_pin_cpu_pins_obj_type) }, + // class constants + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(PIN_MODE_IN) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(PIN_MODE_OUT) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(PIN_MODE_OPEN_DRAIN) }, + + { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(PIN_PULL_UP_100K) }, + { MP_ROM_QSTR(MP_QSTR_PULL_UP_47K), MP_ROM_INT(PIN_PULL_UP_47K) }, + { MP_ROM_QSTR(MP_QSTR_PULL_UP_22K), MP_ROM_INT(PIN_PULL_UP_22K) }, + { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(PIN_PULL_DOWN_100K) }, + { MP_ROM_QSTR(MP_QSTR_PULL_HOLD), MP_ROM_INT(PIN_PULL_HOLD) }, + + { MP_ROM_QSTR(MP_QSTR_DRIVER_OFF), MP_ROM_INT(PIN_DRIVE_OFF) }, + { MP_ROM_QSTR(MP_QSTR_POWER_0), MP_ROM_INT(PIN_DRIVE_POWER_0) }, // R0 (150 Ohm @3.3V / 260 Ohm @ 1.8V) + { MP_ROM_QSTR(MP_QSTR_POWER_1), MP_ROM_INT(PIN_DRIVE_POWER_1) }, // R0/2 + { MP_ROM_QSTR(MP_QSTR_POWER_2), MP_ROM_INT(PIN_DRIVE_POWER_2) }, // R0/3 + { MP_ROM_QSTR(MP_QSTR_POWER_3), MP_ROM_INT(PIN_DRIVE_POWER_3) }, // R0/4 + { MP_ROM_QSTR(MP_QSTR_POWER_4), MP_ROM_INT(PIN_DRIVE_POWER_4) }, // R0/5 + { MP_ROM_QSTR(MP_QSTR_POWER_5), MP_ROM_INT(PIN_DRIVE_POWER_5) }, // R0/6 + { MP_ROM_QSTR(MP_QSTR_POWER_6), MP_ROM_INT(PIN_DRIVE_POWER_6) }, // R0/7 + +}; +STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); + +const mp_obj_type_t machine_pin_type = { + {&mp_type_type}, + .name = MP_QSTR_Pin, + .print = machine_pin_obj_print, + .call = machine_pin_obj_call, + .make_new = machine_pin_obj_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict, +}; + +// FIXME: Create actual pin_af type!!! +const mp_obj_type_t machine_pin_af_type = { + {&mp_type_type}, + .name = MP_QSTR_PinAF, + .print = machine_pin_obj_print, + .make_new = machine_pin_obj_make_new, + .locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict, +}; diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index a6aab7b4aba53..1bb6b6a3520fa 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -28,6 +28,7 @@ #include "py/runtime.h" #include "extmod/machine_mem.h" #include "led.h" +#include "pin.h" #include CPU_HEADER_H @@ -52,6 +53,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if NUM_LEDS { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, #endif + { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index 993e15156618a..a6a28ab4a585c 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -29,10 +29,14 @@ #include #include "ticks.h" +#include "pin.h" +#define mp_hal_pin_obj_t const machine_pin_obj_t * #define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U)) #define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U)) +#define mp_hal_pin_write(p, value) (GPIO_PinWrite(p->gpio, p->pin, value)) #define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin))) +#define mp_hal_pin_read(p) (GPIO_PinRead(p->gpio, p->pin)) void mp_hal_set_interrupt_char(int c); diff --git a/ports/mimxrt/pin.c b/ports/mimxrt/pin.c index b30f4be41ceae..7a437661e3865 100644 --- a/ports/mimxrt/pin.c +++ b/ports/mimxrt/pin.c @@ -24,14 +24,104 @@ * THE SOFTWARE. */ +#include "py/runtime.h" #include "pin.h" -const mp_obj_type_t pin_type = { - .base = {&mp_type_type}, - .name = MP_QSTR_Pin, -}; -const mp_obj_type_t pin_af_type = { - {&mp_type_type}, - .name = MP_QSTR_PinAF, -}; +void pin_init(void) { + return; +} + +uint32_t pin_get_mode(const machine_pin_obj_t *pin) { + uint32_t pin_mode = PIN_MODE_ALT; + uint32_t config_register = *((volatile uint32_t *)pin->configRegister); + uint8_t af_mode = pin_get_af(pin); + + if (af_mode == PIN_AF_MODE_ALT5) { + bool open_drain_enabled = (config_register & IOMUXC_SW_PAD_CTL_PAD_ODE_MASK) >> IOMUXC_SW_PAD_CTL_PAD_ODE_SHIFT; + + if (open_drain_enabled) { + pin_mode = PIN_MODE_OPEN_DRAIN; + } else { + // Check pin direction + if ((pin->gpio->GDIR & (1U << pin->pin)) >> pin->pin) { + pin_mode = PIN_MODE_OUT; + } else { + pin_mode = PIN_MODE_IN; + } + } + } + + return pin_mode; +} + +uint32_t pin_get_af(const machine_pin_obj_t *pin) { + uint32_t mux_register = *((volatile uint32_t *)pin->muxRegister); + + // Read configured AF-Mode of pin + return (uint32_t)(mux_register & IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_MASK) >> IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_SHIFT; +} + +const machine_pin_obj_t *pin_find(mp_obj_t user_obj) { + const machine_pin_obj_t *pin_obj; + + // If a pin was provided, then use it + if (mp_obj_is_type(user_obj, &machine_pin_type)) { + pin_obj = user_obj; + return pin_obj; + } + + // If pin is SMALL_INT + if (mp_obj_is_small_int(user_obj)) { + uint8_t value = MP_OBJ_SMALL_INT_VALUE(user_obj); + if (value < num_board_pins) { + return machine_pin_board_pins[value]; + } + } + + // See if the pin name matches a board pin + pin_obj = pin_find_named_pin(&machine_pin_board_pins_locals_dict, user_obj); + if (pin_obj) { + return pin_obj; + } + + // See if the pin name matches a cpu pin + pin_obj = pin_find_named_pin(&machine_pin_cpu_pins_locals_dict, user_obj); + if (pin_obj) { + return pin_obj; + } + + mp_raise_ValueError(MP_ERROR_TEXT("Pin doesn't exist")); +} + +const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { + mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins); + mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP); + if (named_elem != NULL && named_elem->value != NULL) { + return named_elem->value; + } + return NULL; +} + +const machine_pin_af_obj_t *pin_find_af(const machine_pin_obj_t *pin, uint8_t fn) { + const machine_pin_af_obj_t *af_obj = NULL; + + for (int i = 0; i < pin->af_list_len; ++i) { + af_obj = &pin->af_list[i]; + if (af_obj->af_mode == fn) { + return af_obj; + } + } + + return NULL; +} + +const machine_pin_af_obj_t *pin_find_af_by_index(const machine_pin_obj_t *pin, mp_uint_t af_idx) { + // TODO: Implement pin_find_af_by_index function + return NULL; +} + +const machine_pin_af_obj_t *pin_find_af_by_name(const machine_pin_obj_t *pin, const char *name) { + // TODO: Implement pin_find_af_by_name function + return NULL; +} diff --git a/ports/mimxrt/pin.h b/ports/mimxrt/pin.h index cfb1e6b572502..268b505b3856b 100644 --- a/ports/mimxrt/pin.h +++ b/ports/mimxrt/pin.h @@ -27,12 +27,32 @@ #ifndef MICROPY_INCLUDED_MIMXRT_PIN_H #define MICROPY_INCLUDED_MIMXRT_PIN_H -#include "fsl_gpio.h" +#include #include "py/obj.h" +#include "fsl_gpio.h" + +// ------------------------------------------------------------------------------------------------------------------ // + +#define IS_GPIO_MODE(MODE) (((MODE) == PIN_MODE_IN) || \ + ((MODE) == PIN_MODE_OUT) || \ + ((MODE) == PIN_MODE_OPEN_DRAIN) || \ + ((MODE) == PIN_MODE_ALT)) + +#define IS_GPIO_DRIVE(DRIVE) (((DRIVE) == PIN_DRIVE_OFF) || \ + ((DRIVE) == PIN_DRIVE_POWER_0) || \ + ((DRIVE) == PIN_DRIVE_POWER_1) || \ + ((DRIVE) == PIN_DRIVE_POWER_2) || \ + ((DRIVE) == PIN_DRIVE_POWER_3) || \ + ((DRIVE) == PIN_DRIVE_POWER_4) || \ + ((DRIVE) == PIN_DRIVE_POWER_5) || \ + ((DRIVE) == PIN_DRIVE_POWER_6)) + +// ------------------------------------------------------------------------------------------------------------------ // enum { PIN_MODE_IN = 0, PIN_MODE_OUT, + PIN_MODE_OPEN_DRAIN, PIN_MODE_ALT, }; @@ -47,13 +67,38 @@ enum { PIN_AF_MODE_ALT8, }; +enum { + PIN_PULL_DOWN_100K = 0, + PIN_PULL_UP_47K, + PIN_PULL_UP_100K, + PIN_PULL_UP_22K, + PIN_PULL_DISABLED, + PIN_PULL_HOLD, +}; + +enum { + PIN_DRIVE_OFF = 0b000, + PIN_DRIVE_POWER_0, // R0 (150 Ohm @3.3V / 260 Ohm @ 1.8V) + PIN_DRIVE_POWER_1, // R0/2 + PIN_DRIVE_POWER_2, // R0/3 + PIN_DRIVE_POWER_3, // R0/4 + PIN_DRIVE_POWER_4, // R0/5 + PIN_DRIVE_POWER_5, // R0/6 + PIN_DRIVE_POWER_6, // R0/7 +}; + + + + +// ------------------------------------------------------------------------------------------------------------------ // + typedef struct { mp_obj_base_t base; qstr name; // port name - uint32_t af_mode; // alternate function + uint8_t af_mode; // alternate function void *instance; // pointer to peripheral instance for alternate function uint32_t pad_config; // pad configuration for alternate function -} pin_af_obj_t; +} machine_pin_af_obj_t; typedef struct { mp_obj_base_t base; @@ -62,39 +107,38 @@ typedef struct { uint32_t pin; // pin number uint32_t muxRegister; uint32_t configRegister; - uint32_t mode; // current pin mode - uint32_t af_mode; // current alternate function mode size_t af_list_len; // length of available alternate functions list - const pin_af_obj_t *af_list; // pointer tolist with alternate functions -} pin_obj_t; - -extern const mp_obj_type_t pin_type; -extern const mp_obj_type_t pin_af_type; - -#define PIN_AF(_name, _af_mode, _instance, _pad_config) \ - { \ - .base = { &pin_af_type }, \ - .name = MP_QSTR_##_name, \ - .af_mode = (uint32_t)(_af_mode), \ - .instance = (void *)(_instance), \ - .pad_config = (uint32_t)(_pad_config), \ - } \ - -#define PIN(_name, _gpio, _pin, _af_list) \ - { \ - .base = { &pin_type }, \ - .name = MP_QSTR_##_name, \ - .gpio = (_gpio), \ - .pin = (uint32_t)(_pin), \ - .muxRegister = (uint32_t)&(IOMUXC->SW_MUX_CTL_PAD[kIOMUXC_SW_MUX_CTL_PAD_##_name]), \ - .configRegister = (uint32_t)&(IOMUXC->SW_PAD_CTL_PAD[kIOMUXC_SW_PAD_CTL_PAD_##_name]), \ - .mode = PIN_MODE_IN, \ - .af_mode = PIN_AF_MODE_ALT5, \ - .af_list_len = (size_t)(sizeof((_af_list)) / sizeof(pin_af_obj_t)), \ - .af_list = (_af_list), \ - } \ + const machine_pin_af_obj_t *af_list; // pointer tolist with alternate functions +} machine_pin_obj_t; + +// ------------------------------------------------------------------------------------------------------------------ // + +extern const mp_obj_type_t machine_pin_type; +extern const mp_obj_type_t machine_pin_af_type; + +// ------------------------------------------------------------------------------------------------------------------ // // Include board specific pins -#include "pins.h" +#include "genhdr/pins.h" // pins.h must included at this location + +extern const machine_pin_obj_t *machine_pin_board_pins[]; +extern const uint32_t num_board_pins; + +extern const mp_obj_type_t machine_pin_board_pins_obj_type; +extern const mp_obj_type_t machine_pin_cpu_pins_obj_type; + +extern const mp_obj_dict_t machine_pin_cpu_pins_locals_dict; +extern const mp_obj_dict_t machine_pin_board_pins_locals_dict; + +// ------------------------------------------------------------------------------------------------------------------ // + +void pin_init(void); +uint32_t pin_get_mode(const machine_pin_obj_t *pin); +uint32_t pin_get_af(const machine_pin_obj_t *pin); +const machine_pin_obj_t *pin_find(mp_obj_t user_obj); +const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name); +const machine_pin_af_obj_t *pin_find_af(const machine_pin_obj_t *pin, uint8_t fn); +const machine_pin_af_obj_t *pin_find_af_by_index(const machine_pin_obj_t *pin, mp_uint_t af_idx); +const machine_pin_af_obj_t *pin_find_af_by_name(const machine_pin_obj_t *pin, const char *name); #endif // MICROPY_INCLUDED_MIMXRT_PIN_H diff --git a/ports/mimxrt/tusb_port.c b/ports/mimxrt/tusb_port.c index 0d73b09235b64..2b1ccabbe783a 100644 --- a/ports/mimxrt/tusb_port.c +++ b/ports/mimxrt/tusb_port.c @@ -76,7 +76,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = { static const char *const usbd_desc_str[] = { [USBD_STR_MANUF] = "MicroPython", - [USBD_STR_PRODUCT] = "Board in FS mode", + [USBD_STR_PRODUCT] = "Board in FS mode", // Todo: fix string to indicate that product is running in High Speed mode [USBD_STR_SERIAL] = "000000000000", // TODO [USBD_STR_CDC] = "Board CDC", }; From c326d9a67ba1846bba38f284fa9b8f712dd2d48a Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Sat, 24 Apr 2021 22:15:12 +0200 Subject: [PATCH 0891/3533] mimxrt: Enable built-in help. --- ports/mimxrt/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 65b91675a5de6..e6b15e1b71777 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -58,6 +58,7 @@ #define MICROPY_PY_BUILTINS_FILTER (0) #define MICROPY_PY_BUILTINS_REVERSED (1) #define MICROPY_PY_BUILTINS_MIN_MAX (0) +#define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY___FILE__ (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) From 5f68f0d08ab1effd0fae8a00bdda46dbc788d9db Mon Sep 17 00:00:00 2001 From: Philipp Ebensberger Date: Sat, 24 Apr 2021 21:34:07 +0200 Subject: [PATCH 0892/3533] github/workflows: Add CI workflow for mimxrt port. --- .github/workflows/ports_mimxrt.yml | 23 +++++++++++++++++++++++ tools/ci.sh | 13 +++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 .github/workflows/ports_mimxrt.yml diff --git a/.github/workflows/ports_mimxrt.yml b/.github/workflows/ports_mimxrt.yml new file mode 100644 index 0000000000000..8fbc2209e4139 --- /dev/null +++ b/.github/workflows/ports_mimxrt.yml @@ -0,0 +1,23 @@ +name: mimxrt port + +on: + push: + pull_request: + paths: + - '.github/workflows/*.yml' + - 'tools/**' + - 'py/**' + - 'extmod/**' + - 'lib/**' + - 'drivers/**' + - 'ports/mimxrt/**' + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_mimxrt_setup + - name: Build + run: source tools/ci.sh && ci_mimxrt_build diff --git a/tools/ci.sh b/tools/ci.sh index 7b38ce0df04fc..df812141f5e5f 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -138,6 +138,19 @@ function ci_esp8266_build { make ${MAKEOPTS} -C ports/esp8266 BOARD=GENERIC_1M } +######################################################################################## +# ports/mimxrt + +function ci_mimxrt_setup { + ci_gcc_arm_setup +} + +function ci_mimxrt_build { + make ${MAKEOPTS} -C ports/mimxrt submodules + make ${MAKEOPTS} -C ports/mimxrt BOARD=MIMXRT1020_EVK + make ${MAKEOPTS} -C ports/mimxrt BOARD=TEENSY40 +} + ######################################################################################## # ports/nrf From c732b80f0584156b6ff98e8baa90c011539bf88a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 25 May 2021 14:49:21 +0200 Subject: [PATCH 0893/3533] mimxrt: Extend the Pin module for SoftI2C, SoftSPI support. This change consists mostly of changing and extending the required definitions in mphalport.h. --- ports/mimxrt/machine_pin.c | 14 ++++++++++++++ ports/mimxrt/mphalport.h | 14 +++++++++++++- ports/mimxrt/pin.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c index 583ee4a265192..f63778cbe517a 100644 --- a/ports/mimxrt/machine_pin.c +++ b/ports/mimxrt/machine_pin.c @@ -68,6 +68,20 @@ const mp_obj_type_t machine_pin_board_pins_obj_type = { .locals_dict = (mp_obj_t)&machine_pin_board_pins_locals_dict, }; +// Simplified mode setting used by the extmod modules +void machine_pin_set_mode(const machine_pin_obj_t *self, uint8_t mode) { + gpio_pin_config_t pin_config = {kGPIO_DigitalInput, 1, kGPIO_NoIntmode}; + + pin_config.direction = (mode == PIN_MODE_IN ? kGPIO_DigitalInput : kGPIO_DigitalOutput); + GPIO_PinInit(self->gpio, self->pin, &pin_config); + if (mode == PIN_MODE_OPEN_DRAIN) { + uint32_t pad_config = *(uint32_t *)self->configRegister; + pad_config |= IOMUXC_SW_PAD_CTL_PAD_ODE(0b1) | IOMUXC_SW_PAD_CTL_PAD_DSE(0b110); + IOMUXC_SetPinMux(self->muxRegister, PIN_AF_MODE_ALT5, 0, 0, self->configRegister, 1U); // Software Input On Field: Input Path is determined by functionality + IOMUXC_SetPinConfig(self->muxRegister, PIN_AF_MODE_ALT5, 0, 0, self->configRegister, pad_config); + } +} + STATIC mp_obj_t machine_pin_obj_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); machine_pin_obj_t *self = self_in; diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index a6a28ab4a585c..78b390b4eb578 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -31,12 +31,22 @@ #include "ticks.h" #include "pin.h" +#define MP_HAL_PIN_FMT "%q" + #define mp_hal_pin_obj_t const machine_pin_obj_t * +#define mp_hal_get_pin_obj(o) pin_find(o) +#define mp_hal_pin_name(p) ((p)->name) +#define mp_hal_pin_input(p) machine_pin_set_mode(p, PIN_MODE_IN); +#define mp_hal_pin_output(p) machine_pin_set_mode(p, PIN_MODE_OUT); +#define mp_hal_pin_open_drain(p) machine_pin_set_mode(p, PIN_MODE_OPEN_DRAIN); #define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U)) #define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U)) #define mp_hal_pin_write(p, value) (GPIO_PinWrite(p->gpio, p->pin, value)) #define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin))) -#define mp_hal_pin_read(p) (GPIO_PinRead(p->gpio, p->pin)) +#define mp_hal_pin_read(p) (GPIO_PinReadPadStatus(p->gpio, p->pin)) + +#define mp_hal_pin_od_low(p) mp_hal_pin_low(p) +#define mp_hal_pin_od_high(p) mp_hal_pin_high(p) void mp_hal_set_interrupt_char(int c); @@ -57,6 +67,8 @@ static inline void mp_hal_delay_us(mp_uint_t us) { ticks_delay_us64(us); } +#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us) + static inline mp_uint_t mp_hal_ticks_cpu(void) { return 0; } diff --git a/ports/mimxrt/pin.h b/ports/mimxrt/pin.h index 268b505b3856b..6afedd1f59e1b 100644 --- a/ports/mimxrt/pin.h +++ b/ports/mimxrt/pin.h @@ -140,5 +140,6 @@ const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_ const machine_pin_af_obj_t *pin_find_af(const machine_pin_obj_t *pin, uint8_t fn); const machine_pin_af_obj_t *pin_find_af_by_index(const machine_pin_obj_t *pin, mp_uint_t af_idx); const machine_pin_af_obj_t *pin_find_af_by_name(const machine_pin_obj_t *pin, const char *name); +void machine_pin_set_mode(const machine_pin_obj_t *pin, uint8_t mode); #endif // MICROPY_INCLUDED_MIMXRT_PIN_H From 4ee8ec6931e6a5958d2a5710c6a8f7cd7c0f3862 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 22:15:58 +1000 Subject: [PATCH 0894/3533] py/asmarm: Use builtin func to flush I- and D-cache on ARM 7 archs. The inline assembler code does not work for __ARM_ARCH == 7. Signed-off-by: Damien George --- py/asmarm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/py/asmarm.c b/py/asmarm.c index e91421578b274..5662d75e19527 100644 --- a/py/asmarm.c +++ b/py/asmarm.c @@ -40,7 +40,7 @@ void asm_arm_end_pass(asm_arm_t *as) { if (as->base.pass == MP_ASM_PASS_EMIT) { - #if defined(__linux__) && defined(__GNUC__) + #if (defined(__linux__) && defined(__GNUC__)) || __ARM_ARCH == 7 char *start = mp_asm_base_get_code(&as->base); char *end = start + mp_asm_base_get_code_size(&as->base); __builtin___clear_cache(start, end); @@ -48,10 +48,10 @@ void asm_arm_end_pass(asm_arm_t *as) { // flush I- and D-cache asm volatile ( "0:" - "mrc p15, 0, r15, c7, c10, 3\n" + "mrc p15, 0, r15, c7, c10, 3\n" // test and clean D-cache "bne 0b\n" "mov r0, #0\n" - "mcr p15, 0, r0, c7, c7, 0\n" + "mcr p15, 0, r0, c7, c7, 0\n" // invalidate I-cache and D-cache : : : "r0", "cc"); #endif } From 2c1a6a237d4e68c077c175d16f4d76518c00e4d2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 22:16:06 +1000 Subject: [PATCH 0895/3533] tools/mpy-tool.py: Support relocating ARMv6 arch. Signed-off-by: Damien George --- tools/mpy-tool.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index ea756d3ee2a5c..bfc3cf27e3f46 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -532,6 +532,7 @@ def _link_qstr(self, pc, kind, qst): if config.native_arch in ( MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64, + MP_NATIVE_ARCH_ARMV6, MP_NATIVE_ARCH_XTENSA, MP_NATIVE_ARCH_XTENSAWIN, ): From f5cba77e50c9b3ae73d01cb373ad3226247ccd7b Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 22:16:11 +1000 Subject: [PATCH 0896/3533] tools/tinytest-codegen.py: Add command-line option to exclude tests. Signed-off-by: Damien George --- tools/tinytest-codegen.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index 116a217b4bbc7..cba0b94480ba8 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -108,9 +108,12 @@ def script_to_map(test_file): description="Convert native MicroPython tests to tinytest/upytesthelper C code" ) argparser.add_argument("--stdin", action="store_true", help="read list of tests from stdin") +argparser.add_argument("--exclude", action="append", help="exclude test by name") args = argparser.parse_args() if not args.stdin: + if args.exclude: + exclude_tests += tuple(args.exclude) for group in test_dirs: tests += [test for test in glob("{}/*.py".format(group)) if test not in exclude_tests] else: From b84406f3133a36a703a1506d754fc046dd955922 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 22:16:08 +1000 Subject: [PATCH 0897/3533] qemu-arm: Add support for Cortex-A9 via sabrelite board. Signed-off-by: Damien George --- ports/qemu-arm/Makefile | 21 ++++++++++++---- ports/qemu-arm/Makefile.test | 7 +++++- ports/qemu-arm/imx6.ld | 47 +++++++++++++++++++++++++++++++++++ ports/qemu-arm/mpconfigport.h | 11 +++++--- ports/qemu-arm/startup.c | 27 ++++++++++++++++++++ ports/qemu-arm/uart.c | 27 ++++++++++++++++++++ 6 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 ports/qemu-arm/imx6.ld diff --git a/ports/qemu-arm/Makefile b/ports/qemu-arm/Makefile index aebc5afaeb773..22d0bf39cd961 100644 --- a/ports/qemu-arm/Makefile +++ b/ports/qemu-arm/Makefile @@ -16,7 +16,8 @@ ifeq ($(BOARD),netduino2) CFLAGS += -mthumb -mcpu=cortex-m3 -mfloat-abi=soft CFLAGS += -DQEMU_SOC_STM32 LDSCRIPT = stm32.ld -SRC_BOARD_O = lib/utils/gchelper_m3.o +SRC_BOARD_O = lib/utils/gchelper_native.o lib/utils/gchelper_m3.o +MPY_CROSS_FLAGS += -march=armv7m endif ifeq ($(BOARD),microbit) @@ -24,14 +25,26 @@ CFLAGS += -mthumb -mcpu=cortex-m0 -mfloat-abi=soft CFLAGS += -DQEMU_SOC_NRF51 LDSCRIPT = nrf51.ld QEMU_EXTRA = -global nrf51-soc.flash-size=1048576 -global nrf51-soc.sram-size=262144 -SRC_BOARD_O = lib/utils/gchelper_m0.o +SRC_BOARD_O = lib/utils/gchelper_native.o lib/utils/gchelper_m0.o +MPY_CROSS_FLAGS += -march=armv7m endif ifeq ($(BOARD),mps2-an385) CFLAGS += -mthumb -mcpu=cortex-m3 -mfloat-abi=soft CFLAGS += -DQEMU_SOC_MPS2 LDSCRIPT = mps2.ld -SRC_BOARD_O = lib/utils/gchelper_m3.o +SRC_BOARD_O = lib/utils/gchelper_native.o lib/utils/gchelper_m3.o +MPY_CROSS_FLAGS += -march=armv7m +endif + +ifeq ($(BOARD),sabrelite) +CFLAGS += -mcpu=cortex-a9 +CFLAGS += -DQEMU_SOC_IMX6 +LDSCRIPT = imx6.ld +QEMU_EXTRA = -m 128M +SRC_BOARD_O = lib/utils/gchelper_generic.o +# It's really armv7a but closest supported value is armv6. +MPY_CROSS_FLAGS += -march=armv6 endif CROSS_COMPILE ?= arm-none-eabi- @@ -95,7 +108,6 @@ LIB_SRC_C += $(addprefix lib/,\ libm/atanf.c \ libm/atan2f.c \ libm/roundf.c \ - utils/gchelper_native.c \ utils/sys_stdio_mphal.c \ ) @@ -125,7 +137,6 @@ ifneq ($(FROZEN_MANIFEST),) CFLAGS += -DMICROPY_MODULE_FROZEN_STR CFLAGS += -DMICROPY_MODULE_FROZEN_MPY CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool -MPY_CROSS_FLAGS += -march=armv7m endif all: run diff --git a/ports/qemu-arm/Makefile.test b/ports/qemu-arm/Makefile.test index 6b7f1dc7ab46c..b4ad6114b8046 100644 --- a/ports/qemu-arm/Makefile.test +++ b/ports/qemu-arm/Makefile.test @@ -4,6 +4,11 @@ FROZEN_MANIFEST ?= "freeze('test-frzmpy')" include Makefile +ifeq ($(BOARD),sabrelite) +# These don't work on Cortex-A9. +TESTS_EXCLUDE = inlineasm/asmdiv.py inlineasm/asmspecialregs.py +endif + CFLAGS += -DTEST .PHONY: $(BUILD)/genhdr/tests.h @@ -11,7 +16,7 @@ CFLAGS += -DTEST $(BUILD)/test_main.o: $(BUILD)/genhdr/tests.h $(BUILD)/genhdr/tests.h: (cd $(TOP)/tests; ./run-tests.py --target=qemu-arm --write-exp) - $(Q)echo "Generating $@";(cd $(TOP)/tests; ../tools/tinytest-codegen.py) > $@ + $(Q)echo "Generating $@";(cd $(TOP)/tests; ../tools/tinytest-codegen.py $(addprefix --exclude ,$(TESTS_EXCLUDE))) > $@ $(BUILD)/lib/tinytest/tinytest.o: CFLAGS += -DNO_FORKING diff --git a/ports/qemu-arm/imx6.ld b/ports/qemu-arm/imx6.ld new file mode 100644 index 0000000000000..7155956f15ad5 --- /dev/null +++ b/ports/qemu-arm/imx6.ld @@ -0,0 +1,47 @@ +/* Vector table is at 0x00000000, entry point is 0x10000000. */ + +MEMORY +{ + ROM : ORIGIN = 0x00000000, LENGTH = 96K + RAM : ORIGIN = 0x10000000, LENGTH = 128M +} + +_estack = ORIGIN(RAM) + LENGTH(RAM); + +SECTIONS +{ + .rom : { + . = ALIGN(4); + KEEP(*(.isr_vector)) + . = ALIGN(4); + } > ROM + + .text : { + . = ALIGN(4); + *(.text.Reset_Handler) + *(.text*) + *(.rodata*) + . = ALIGN(4); + _etext = .; + _sidata = _etext; + } > RAM + + .data : AT ( _sidata ) + { + . = ALIGN(4); + _sdata = .; + *(.data*) + . = ALIGN(4); + _edata = .; + } > RAM + + .bss : + { + . = ALIGN(4); + _sbss = .; + *(.bss*) + *(COMMON) + . = ALIGN(4); + _ebss = .; + } > RAM +} diff --git a/ports/qemu-arm/mpconfigport.h b/ports/qemu-arm/mpconfigport.h index 498ab3ed2cfac..1f05719caad9c 100644 --- a/ports/qemu-arm/mpconfigport.h +++ b/ports/qemu-arm/mpconfigport.h @@ -3,9 +3,16 @@ // options to control how MicroPython is built #define MICROPY_ALLOC_PATH_MAX (512) -#define MICROPY_EMIT_X64 (0) + +#if defined(__ARM_ARCH_ISA_ARM) +#define MICROPY_EMIT_ARM (1) +#define MICROPY_EMIT_INLINE_THUMB (1) +#elif defined(__ARM_ARCH_ISA_THUMB) #define MICROPY_EMIT_THUMB (1) #define MICROPY_EMIT_INLINE_THUMB (1) +#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) +#endif + #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) #define MICROPY_MEM_STATS (1) #define MICROPY_DEBUG_PRINTERS (0) @@ -43,8 +50,6 @@ // type definitions for the specific machine -#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) - #define MP_SSIZE_MAX (0x7fffffff) #define UINT_FMT "%lu" diff --git a/ports/qemu-arm/startup.c b/ports/qemu-arm/startup.c index 58bdf7af9e98e..a52b068c332ae 100644 --- a/ports/qemu-arm/startup.c +++ b/ports/qemu-arm/startup.c @@ -28,6 +28,27 @@ void Default_Handler(void) { } } +#if defined(__ARM_ARCH_ISA_ARM) + +// ARM architecture with standard ARM ISA. + +__attribute__((naked, section(".isr_vector"))) void isr_vector(void) { + __asm volatile ( + "b Reset_Handler\n" + "b Default_Handler\n" + "b Default_Handler\n" + "b Default_Handler\n" + "b Default_Handler\n" + "nop\n" + "b Default_Handler\n" + "b Default_Handler\n" + ); +} + +#elif defined(__ARM_ARCH_ISA_THUMB) + +// ARM architecture with Thumb-only ISA. + const uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = { (uint32_t)&_estack, (uint32_t)&Reset_Handler, @@ -47,6 +68,8 @@ const uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = { (uint32_t)&Default_Handler, // SysTick_Handler }; +#endif + void _start(void) { // Enable the UART uart_init(); @@ -68,7 +91,11 @@ __attribute__((naked)) void exit(int status) { "ldr r1, =0x20026\n" // ADP_Stopped_ApplicationExit, a clean exit ".notclean:\n" "movs r0, #0x18\n" // SYS_EXIT + #if defined(__ARM_ARCH_ISA_ARM) + "svc 0x00123456\n" + #elif defined(__ARM_ARCH_ISA_THUMB) "bkpt 0xab\n" + #endif ); for (;;) { } diff --git a/ports/qemu-arm/uart.c b/ports/qemu-arm/uart.c index 8710e9e9f681d..828398cd175a8 100644 --- a/ports/qemu-arm/uart.c +++ b/ports/qemu-arm/uart.c @@ -75,4 +75,31 @@ void uart_tx_strn(const char *buf, size_t len) { } } +#elif defined(QEMU_SOC_IMX6) + +#define UART_UCR1_UARTEN (1 << 0) +#define UART_UCR2_TXEN (1 << 2) + +typedef struct _UART_t { + volatile uint32_t URXD; // 0x00 + volatile uint32_t r0[15]; + volatile uint32_t UTXD; // 0x40 + volatile uint32_t r1[15]; + volatile uint32_t UCR1; // 0x80 + volatile uint32_t UCR2; // 0x84 +} UART_t; + +#define UART1 ((UART_t *)(0x02020000)) + +void uart_init(void) { + UART1->UCR1 = UART_UCR1_UARTEN; + UART1->UCR2 = UART_UCR2_TXEN; +} + +void uart_tx_strn(const char *buf, size_t len) { + for (size_t i = 0; i < len; ++i) { + UART1->UTXD = buf[i]; + } +} + #endif From e7c0a8bca31063c295afdaf8e49c8ab24b70a90d Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 22:14:56 +1000 Subject: [PATCH 0898/3533] tools/ci.sh: Build Cortex-A9 sabrelite board as part of qemu-arm CI. Signed-off-by: Damien George --- tools/ci.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/ci.sh b/tools/ci.sh index df812141f5e5f..c986c9fef80ba 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -194,6 +194,8 @@ function ci_qemu_arm_build { make ${MAKEOPTS} -C ports/qemu-arm CFLAGS_EXTRA=-DMP_ENDIANNESS_BIG=1 make ${MAKEOPTS} -C ports/qemu-arm clean make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test + make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test clean + make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test BOARD=sabrelite test } ######################################################################################## From dc86e044761a132ddd7026f0f9555f958ba8046a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 20 May 2021 10:02:57 +1000 Subject: [PATCH 0899/3533] tests: Make float and framebuf tests skip or run on big-endian archs. Signed-off-by: Damien George --- tests/extmod/framebuf16.py | 7 ++++++- tests/extmod/framebuf_subclass.py | 7 ++++++- ...ytearray_construct.py => bytearray_construct_endian.py} | 0 .../{bytes_construct.py => bytes_construct_endian.py} | 2 +- tests/float/float_array.py | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) rename tests/float/{bytearray_construct.py => bytearray_construct_endian.py} (100%) rename tests/float/{bytes_construct.py => bytes_construct_endian.py} (77%) diff --git a/tests/extmod/framebuf16.py b/tests/extmod/framebuf16.py index e658f1345ad1a..cd7f5ec015836 100644 --- a/tests/extmod/framebuf16.py +++ b/tests/extmod/framebuf16.py @@ -1,9 +1,14 @@ try: - import framebuf + import framebuf, usys except ImportError: print("SKIP") raise SystemExit +# This test and its .exp file is based on a little-endian architecture. +if usys.byteorder != "little": + print("SKIP") + raise SystemExit + def printbuf(): print("--8<--") diff --git a/tests/extmod/framebuf_subclass.py b/tests/extmod/framebuf_subclass.py index aad5d2a1e96b4..a9e3c5efc7912 100644 --- a/tests/extmod/framebuf_subclass.py +++ b/tests/extmod/framebuf_subclass.py @@ -1,11 +1,16 @@ # test subclassing framebuf.FrameBuffer try: - import framebuf + import framebuf, usys except ImportError: print("SKIP") raise SystemExit +# This test and its .exp file is based on a little-endian architecture. +if usys.byteorder != "little": + print("SKIP") + raise SystemExit + class FB(framebuf.FrameBuffer): def __init__(self, n): diff --git a/tests/float/bytearray_construct.py b/tests/float/bytearray_construct_endian.py similarity index 100% rename from tests/float/bytearray_construct.py rename to tests/float/bytearray_construct_endian.py diff --git a/tests/float/bytes_construct.py b/tests/float/bytes_construct_endian.py similarity index 77% rename from tests/float/bytes_construct.py rename to tests/float/bytes_construct_endian.py index 0806087b0ef0e..208f56162f859 100644 --- a/tests/float/bytes_construct.py +++ b/tests/float/bytes_construct_endian.py @@ -1,4 +1,4 @@ -# test construction of bytearray from array with float type +# test construction of bytes from array with float type try: from uarray import array diff --git a/tests/float/float_array.py b/tests/float/float_array.py index 3c2189869b93f..219b6b86aef25 100644 --- a/tests/float/float_array.py +++ b/tests/float/float_array.py @@ -22,4 +22,4 @@ def test(a): test(array("f")) test(array("d")) -print("{:.4f}".format(array("f", b"\xcc\xcc\xcc=")[0])) +print("{:.4f}".format(array("f", bytes(array("I", [0x3DCCCCCC])))[0])) From ef16834887de02cbddf414b560e5a2af9cae4b16 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 May 2021 13:20:02 +1000 Subject: [PATCH 0900/3533] github/workflows: Add workflow to build and run unix port on MIPS. This adds a coverage build and running of the test suite on a MIPS 32-bit big endian architecture. It uses the feature of qemu to execute foreign code as though it were native to the system (using qemu user mode). The code compiled for MIPS will run under the qemu VM, but all syscalls made by this code go to the host (Linux) system. See related #7268 and #7273. Signed-off-by: Damien George --- .github/workflows/ports_unix.yml | 14 ++++++++++++++ tools/ci.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index aa4b8abdc9893..5c4a4f3047a57 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -186,3 +186,17 @@ jobs: - name: Print failures if: failure() run: tests/run-tests.py --print-failures + + qemu_mips: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install packages + run: source tools/ci.sh && ci_unix_qemu_mips_setup + - name: Build + run: source tools/ci.sh && ci_unix_qemu_mips_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_qemu_mips_run_tests + - name: Print failures + if: failure() + run: tests/run-tests.py --print-failures diff --git a/tools/ci.sh b/tools/ci.sh index c986c9fef80ba..23e529e5e1194 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -292,6 +292,13 @@ CI_UNIX_OPTS_SYS_SETTRACE_STACKLESS=( CFLAGS_EXTRA="-DMICROPY_STACKLESS=1 -DMICROPY_STACKLESS_STRICT=1 -DMICROPY_PY_SYS_SETTRACE=1" ) +CI_UNIX_OPTS_QEMU_MIPS=( + CROSS_COMPILE=mips-linux-gnu- + VARIANT=coverage + MICROPY_STANDALONE=1 + LDFLAGS_EXTRA="-static" +) + function ci_unix_build_helper { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/unix "$@" submodules @@ -478,6 +485,26 @@ function ci_unix_macos_run_tests { (cd tests && ./run-tests.py --exclude 'uasyncio_(basic|heaplock|lock|wait_task)' --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') } +function ci_unix_qemu_mips_setup { + sudo apt-get update + sudo apt-get install gcc-mips-linux-gnu g++-mips-linux-gnu + sudo apt-get install qemu-user + qemu-mips --version +} + +function ci_unix_qemu_mips_build { + # qemu-mips on GitHub Actions will seg-fault if not linked statically + ci_unix_build_helper "${CI_UNIX_OPTS_QEMU_MIPS[@]}" +} + +function ci_unix_qemu_mips_run_tests { + # Issues with MIPS tests: + # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) + # - ffi tests do not work + file ./ports/unix/micropython-coverage + (cd tests && MICROPY_MICROPYTHON=../ports/unix/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') +} + ######################################################################################## # ports/windows From 0abf6f830cd390e2e5dbd4a2c68f611f5c9507dc Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 26 May 2021 16:57:39 +1000 Subject: [PATCH 0901/3533] esp8266/boards/GENERIC_512K: Add custom manifest without FS modules. The 512k build does not have a filesystem so there is no reason to include the filesystem-related modules. This commit provides a custom manifest.py for this board which no longer includes: _boot.py, flashbdev.py, inisetup.py, upip.py, upip_utarfile.py. This cuts the build down by about 9k of flash. Signed-off-by: Damien George --- ports/esp8266/boards/GENERIC_512K/manifest.py | 4 ++++ ports/esp8266/boards/GENERIC_512K/mpconfigboard.mk | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 ports/esp8266/boards/GENERIC_512K/manifest.py diff --git a/ports/esp8266/boards/GENERIC_512K/manifest.py b/ports/esp8266/boards/GENERIC_512K/manifest.py new file mode 100644 index 0000000000000..e7ab8ad535b6a --- /dev/null +++ b/ports/esp8266/boards/GENERIC_512K/manifest.py @@ -0,0 +1,4 @@ +freeze("$(PORT_DIR)/modules", ("apa102.py", "neopixel.py", "ntptime.py", "port_diag.py")) +freeze("$(MPY_DIR)/drivers/dht", "dht.py") +freeze("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/extmod/webrepl/manifest.py") diff --git a/ports/esp8266/boards/GENERIC_512K/mpconfigboard.mk b/ports/esp8266/boards/GENERIC_512K/mpconfigboard.mk index a7566f9f19ec9..120351909bc0c 100644 --- a/ports/esp8266/boards/GENERIC_512K/mpconfigboard.mk +++ b/ports/esp8266/boards/GENERIC_512K/mpconfigboard.mk @@ -1 +1,3 @@ LD_FILES = boards/esp8266_512k.ld + +FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py From 6a127810c03d7666f1866f5e48df3c102805dbe8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 25 May 2021 16:48:29 +1000 Subject: [PATCH 0902/3533] extmod/moduhashlib: Put hash obj in final state after digest is called. If digest is called then the hash object is put in a "final" state and calling update() or digest() again will raise a ValueError (instead of silently producing the wrong result). See issue #4119. Signed-off-by: Damien George --- extmod/moduhashlib.c | 33 +++++++++++++++++++++++++++- tests/extmod/uhashlib_final.py | 35 ++++++++++++++++++++++++++++++ tests/extmod/uhashlib_final.py.exp | 1 + 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/extmod/uhashlib_final.py create mode 100644 tests/extmod/uhashlib_final.py.exp diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 195a24334b69f..b170de9e58e13 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -60,9 +60,16 @@ typedef struct _mp_obj_hash_t { mp_obj_base_t base; - char state[0]; + bool final; // if set, update and digest raise an exception + uintptr_t state[0]; // must be aligned to a machine word } mp_obj_hash_t; +static void uhashlib_ensure_not_final(mp_obj_hash_t *self) { + if (self->final) { + mp_raise_ValueError(MP_ERROR_TEXT("hash is final")); + } +} + #if MICROPY_PY_UHASHLIB_SHA256 STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg); @@ -78,6 +85,7 @@ STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_arg mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_sha256_context)); o->base.type = type; + o->final = false; mbedtls_sha256_init((mbedtls_sha256_context *)&o->state); mbedtls_sha256_starts_ret((mbedtls_sha256_context *)&o->state, 0); if (n_args == 1) { @@ -88,6 +96,7 @@ STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_arg STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); mbedtls_sha256_update_ret((mbedtls_sha256_context *)&self->state, bufinfo.buf, bufinfo.len); @@ -96,6 +105,8 @@ STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, 32); mbedtls_sha256_finish_ret((mbedtls_sha256_context *)&self->state, (unsigned char *)vstr.buf); @@ -110,6 +121,7 @@ STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_arg mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(CRYAL_SHA256_CTX)); o->base.type = type; + o->final = false; sha256_init((CRYAL_SHA256_CTX *)o->state); if (n_args == 1) { uhashlib_sha256_update(MP_OBJ_FROM_PTR(o), args[0]); @@ -119,6 +131,7 @@ STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_arg STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); sha256_update((CRYAL_SHA256_CTX *)self->state, bufinfo.buf, bufinfo.len); @@ -127,6 +140,8 @@ STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, SHA256_BLOCK_SIZE); sha256_final((CRYAL_SHA256_CTX *)self->state, (byte *)vstr.buf); @@ -160,6 +175,7 @@ STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX)); o->base.type = type; + o->final = false; SHA1_Init((SHA1_CTX *)o->state); if (n_args == 1) { uhashlib_sha1_update(MP_OBJ_FROM_PTR(o), args[0]); @@ -169,6 +185,7 @@ STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t uhashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); SHA1_Update((SHA1_CTX *)self->state, bufinfo.buf, bufinfo.len); @@ -177,6 +194,8 @@ STATIC mp_obj_t uhashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, SHA1_SIZE); SHA1_Final((byte *)vstr.buf, (SHA1_CTX *)self->state); @@ -196,6 +215,7 @@ STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_sha1_context)); o->base.type = type; + o->final = false; mbedtls_sha1_init((mbedtls_sha1_context *)o->state); mbedtls_sha1_starts_ret((mbedtls_sha1_context *)o->state); if (n_args == 1) { @@ -206,6 +226,7 @@ STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t uhashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); mbedtls_sha1_update_ret((mbedtls_sha1_context *)self->state, bufinfo.buf, bufinfo.len); @@ -214,6 +235,8 @@ STATIC mp_obj_t uhashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, 20); mbedtls_sha1_finish_ret((mbedtls_sha1_context *)self->state, (byte *)vstr.buf); @@ -247,6 +270,7 @@ STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(MD5_CTX)); o->base.type = type; + o->final = false; MD5_Init((MD5_CTX *)o->state); if (n_args == 1) { uhashlib_md5_update(MP_OBJ_FROM_PTR(o), args[0]); @@ -256,6 +280,7 @@ STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t uhashlib_md5_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); MD5_Update((MD5_CTX *)self->state, bufinfo.buf, bufinfo.len); @@ -264,6 +289,8 @@ STATIC mp_obj_t uhashlib_md5_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, MD5_SIZE); MD5_Final((byte *)vstr.buf, (MD5_CTX *)self->state); @@ -283,6 +310,7 @@ STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_md5_context)); o->base.type = type; + o->final = false; mbedtls_md5_init((mbedtls_md5_context *)o->state); mbedtls_md5_starts_ret((mbedtls_md5_context *)o->state); if (n_args == 1) { @@ -293,6 +321,7 @@ STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t uhashlib_md5_update(mp_obj_t self_in, mp_obj_t arg) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); mbedtls_md5_update_ret((mbedtls_md5_context *)self->state, bufinfo.buf, bufinfo.len); @@ -301,6 +330,8 @@ STATIC mp_obj_t uhashlib_md5_update(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); + uhashlib_ensure_not_final(self); + self->final = true; vstr_t vstr; vstr_init_len(&vstr, 16); mbedtls_md5_finish_ret((mbedtls_md5_context *)self->state, (byte *)vstr.buf); diff --git a/tests/extmod/uhashlib_final.py b/tests/extmod/uhashlib_final.py new file mode 100644 index 0000000000000..f562cc17807ec --- /dev/null +++ b/tests/extmod/uhashlib_final.py @@ -0,0 +1,35 @@ +try: + import uhashlib +except ImportError: + print("SKIP") + raise SystemExit + + +for algo_name in ("md5", "sha1", "sha256"): + algo = getattr(uhashlib, algo_name, None) + if not algo: + continue + + # Running .digest() several times in row is not supported. + h = algo(b"123") + h.digest() + try: + h.digest() + print("fail") + except ValueError: + # Expected path, don't print anything so test output is the + # same even if the algorithm is not implemented on the port. + pass + + # Partial digests are not supported. + h = algo(b"123") + h.digest() + try: + h.update(b"456") + print("fail") + except ValueError: + # Expected path, don't print anything so test output is the + # same even if the algorithm is not implemented on the port. + pass + +print("done") diff --git a/tests/extmod/uhashlib_final.py.exp b/tests/extmod/uhashlib_final.py.exp new file mode 100644 index 0000000000000..19f86f493ab11 --- /dev/null +++ b/tests/extmod/uhashlib_final.py.exp @@ -0,0 +1 @@ +done From 62f75376dd6932a694569e2b7a8701b3a6b13a77 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 26 May 2021 22:37:27 +1000 Subject: [PATCH 0903/3533] stm32/boards/NUCLEO_L432KC: Fix FS size and enable LFS1 filesystem. Signed-off-by: Damien George --- ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h | 2 +- ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.mk | 1 + ports/stm32/boards/stm32l432.ld | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h index 0a3ac1f185a70..0f1134491f979 100644 --- a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h @@ -13,13 +13,13 @@ #define MICROPY_PY_UHEAPQ (0) #define MICROPY_PY_UTIMEQ (0) -#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_ADC (1) #define MICROPY_HW_ENABLE_DAC (1) #define MICROPY_HW_ENABLE_USB (0) // requires a custom USB connector on PA11/PA12 #define MICROPY_HW_ENABLE_TIMER (1) #define MICROPY_HW_HAS_SWITCH (0) +#define MICROPY_HW_HAS_FLASH (1) // MSI is used and is 4MHz #define MICROPY_HW_CLK_PLLM (1) diff --git a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.mk index 6e220a4370db3..a87b4710e74ac 100644 --- a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.mk +++ b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.mk @@ -6,6 +6,7 @@ OPENOCD_CONFIG = boards/openocd_stm32l4.cfg # MicroPython settings MICROPY_VFS_FAT = 0 +MICROPY_VFS_LFS1 ?= 1 # Don't include default frozen modules because MCU is tight on flash space FROZEN_MANIFEST ?= diff --git a/ports/stm32/boards/stm32l432.ld b/ports/stm32/boards/stm32l432.ld index 4699543d66912..469e834f91c1f 100644 --- a/ports/stm32/boards/stm32l432.ld +++ b/ports/stm32/boards/stm32l432.ld @@ -6,7 +6,7 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 230K /* sectors 0-114 */ - FLASH_FS (r) : ORIGIN = 0x08060000, LENGTH = 26K /* sectors 115-127 */ + FLASH_FS (r) : ORIGIN = 0x08039800, LENGTH = 26K /* sectors 115-127 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K /* SRAM1, 48K + SRAM2, 16K */ } From 211c3e41f12295f08a883ac326386ecb5bc8d68e Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 27 May 2021 23:57:51 +1000 Subject: [PATCH 0904/3533] stm32/boards/PYBD_SF2: Disable GCC 11 warnings for array bounds. With GCC 11 there is now a warning about array bounds of OTP-mac, due to the OTP being a literal address. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/board_init.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ports/stm32/boards/PYBD_SF2/board_init.c b/ports/stm32/boards/PYBD_SF2/board_init.c index d1a0a09e7a477..c3b54fa2f101a 100644 --- a/ports/stm32/boards/PYBD_SF2/board_init.c +++ b/ports/stm32/boards/PYBD_SF2/board_init.c @@ -64,7 +64,15 @@ void board_sleep(int value) { void mp_hal_get_mac(int idx, uint8_t buf[6]) { // Check if OTP region has a valid MAC address, and use it if it does if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') { + #if __GNUC__ >= 11 + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Warray-bounds" + #pragma GCC diagnostic ignored "-Wstringop-overread" + #endif memcpy(buf, OTP->mac, 6); + #if __GNUC__ >= 11 + #pragma GCC diagnostic pop + #endif buf[5] += idx; return; } From db8704ecbd910e598fba4bc1817acf91d9da79e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 28 May 2021 00:09:34 +1000 Subject: [PATCH 0905/3533] esp8266,esp32: Update manifest to point to new dirs in micropython-lib. Following a refactoring of micropython-lib. Signed-off-by: Damien George --- ports/esp32/boards/manifest_release.py | 9 +++++---- ports/esp8266/boards/GENERIC/manifest.py | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ports/esp32/boards/manifest_release.py b/ports/esp32/boards/manifest_release.py index b5c7cd111b109..8b9bcde6ffa77 100644 --- a/ports/esp32/boards/manifest_release.py +++ b/ports/esp32/boards/manifest_release.py @@ -1,6 +1,7 @@ include("manifest.py") -freeze("$(MPY_LIB_DIR)/upysh", "upysh.py") -freeze("$(MPY_LIB_DIR)/urequests", "urequests.py") -freeze("$(MPY_LIB_DIR)/umqtt.simple", "umqtt/simple.py") -freeze("$(MPY_LIB_DIR)/umqtt.robust", "umqtt/robust.py") +freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") + +freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") +freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") +freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") diff --git a/ports/esp8266/boards/GENERIC/manifest.py b/ports/esp8266/boards/GENERIC/manifest.py index 46f3b837be07f..9ce3ffe3aae15 100644 --- a/ports/esp8266/boards/GENERIC/manifest.py +++ b/ports/esp8266/boards/GENERIC/manifest.py @@ -10,12 +10,12 @@ # Libraries from micropython-lib, include only if the library directory exists if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): # file utilities - freeze("$(MPY_LIB_DIR)/upysh", "upysh.py") + freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") # requests - freeze("$(MPY_LIB_DIR)/urequests", "urequests.py") - freeze("$(MPY_LIB_DIR)/urllib.urequest", "urllib/urequest.py") + freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") + freeze("$(MPY_LIB_DIR)/micropython/urllib.urequest", "urllib/urequest.py") # umqtt - freeze("$(MPY_LIB_DIR)/umqtt.simple", "umqtt/simple.py") - freeze("$(MPY_LIB_DIR)/umqtt.robust", "umqtt/robust.py") + freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") + freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") From 4982d0920ebd247758aa6321bb36a5eede0bd775 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 May 2021 11:04:01 +1000 Subject: [PATCH 0906/3533] tools/pyboard.py: Track raw REPL state via in_raw_repl variable. Signed-off-by: Damien George --- tools/pyboard.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/pyboard.py b/tools/pyboard.py index 29a15f7eae25d..220c010937d73 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -255,6 +255,7 @@ class Pyboard: def __init__( self, device, baudrate=115200, user="micro", password="python", wait=0, exclusive=True ): + self.in_raw_repl = False self.use_raw_paste = True if device.startswith("exec:"): self.serial = ProcessToSerial(device[len("exec:") :]) @@ -348,8 +349,11 @@ def enter_raw_repl(self): print(data) raise PyboardError("could not enter raw repl") + self.in_raw_repl = True + def exit_raw_repl(self): self.serial.write(b"\r\x02") # ctrl-B: enter friendly REPL + self.in_raw_repl = False def follow(self, timeout, data_consumer=None): # wait for normal output From e4ba57c5cd6f68306726891c45f36b5129b633ec Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 May 2021 11:21:19 +1000 Subject: [PATCH 0907/3533] tools/pyboard.py: Add "soft_reset" option to Pyboard.enter_raw_repl(). Signed-off-by: Damien George --- tools/pyboard.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 220c010937d73..7d06aa847d7af 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -322,7 +322,7 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None): time.sleep(0.01) return data - def enter_raw_repl(self): + def enter_raw_repl(self, soft_reset=True): self.serial.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program # flush input (without relying on serial.flushInput()) @@ -332,18 +332,23 @@ def enter_raw_repl(self): n = self.serial.inWaiting() self.serial.write(b"\r\x01") # ctrl-A: enter raw REPL - data = self.read_until(1, b"raw REPL; CTRL-B to exit\r\n>") - if not data.endswith(b"raw REPL; CTRL-B to exit\r\n>"): - print(data) - raise PyboardError("could not enter raw repl") - self.serial.write(b"\x04") # ctrl-D: soft reset - data = self.read_until(1, b"soft reboot\r\n") - if not data.endswith(b"soft reboot\r\n"): - print(data) - raise PyboardError("could not enter raw repl") - # By splitting this into 2 reads, it allows boot.py to print stuff, - # which will show up after the soft reboot and before the raw REPL. + if soft_reset: + data = self.read_until(1, b"raw REPL; CTRL-B to exit\r\n>") + if not data.endswith(b"raw REPL; CTRL-B to exit\r\n>"): + print(data) + raise PyboardError("could not enter raw repl") + + self.serial.write(b"\x04") # ctrl-D: soft reset + + # Waiting for "soft reboot" independently to "raw REPL" (done below) + # allows boot.py to print, which will show up after "soft reboot" + # and before "raw REPL". + data = self.read_until(1, b"soft reboot\r\n") + if not data.endswith(b"soft reboot\r\n"): + print(data) + raise PyboardError("could not enter raw repl") + data = self.read_until(1, b"raw REPL; CTRL-B to exit\r\n") if not data.endswith(b"raw REPL; CTRL-B to exit\r\n"): print(data) From a60ad3364132b9c4a30b20cd91fd5cd1ac965618 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 29 May 2021 17:12:54 +1000 Subject: [PATCH 0908/3533] tools/mpremote: Add new CLI utility to interact with remote device. This has been under development since April 2017. See #3034 and #6375. Signed-off-by: Damien George --- tools/mpremote/LICENSE | 21 + tools/mpremote/README.md | 71 +++ tools/mpremote/mpremote.py | 6 + tools/mpremote/mpremote/__init__.py | 1 + tools/mpremote/mpremote/console.py | 162 ++++++ tools/mpremote/mpremote/main.py | 477 ++++++++++++++++ tools/mpremote/mpremote/pyboardextended.py | 621 +++++++++++++++++++++ tools/mpremote/pyproject.toml | 6 + tools/mpremote/setup.cfg | 25 + 9 files changed, 1390 insertions(+) create mode 100644 tools/mpremote/LICENSE create mode 100644 tools/mpremote/README.md create mode 100755 tools/mpremote/mpremote.py create mode 100644 tools/mpremote/mpremote/__init__.py create mode 100644 tools/mpremote/mpremote/console.py create mode 100644 tools/mpremote/mpremote/main.py create mode 100644 tools/mpremote/mpremote/pyboardextended.py create mode 100644 tools/mpremote/pyproject.toml create mode 100644 tools/mpremote/setup.cfg diff --git a/tools/mpremote/LICENSE b/tools/mpremote/LICENSE new file mode 100644 index 0000000000000..5ec904cca10b1 --- /dev/null +++ b/tools/mpremote/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 Damien P. George + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/tools/mpremote/README.md b/tools/mpremote/README.md new file mode 100644 index 0000000000000..a6aaa1755df5e --- /dev/null +++ b/tools/mpremote/README.md @@ -0,0 +1,71 @@ +# mpremote -- MicroPython remote control + +This CLI tool provides an integrated set of utilities to remotely interact with +and automate a MicroPython device over a serial connection. + +The simplest way to use this tool is: + + mpremote + +This will automatically connect to the device and provide an interactive REPL. + +The full list of supported commands are: + + mpremote connect -- connect to given device + device may be: list, auto, id:x, port:x + or any valid device name/path + mpremote disconnect -- disconnect current device + mpremote mount -- mount local directory on device + mpremote eval -- evaluate and print the string + mpremote exec -- execute the string + mpremote run -- run the given local script + mpremote fs -- execute filesystem commands on the device + command may be: cat, ls, cp, rm, mkdir, rmdir + use ":" as a prefix to specify a file on the device + mpremote repl -- enter REPL + options: + --capture + --inject-code + --inject-file + +Multiple commands can be specified and they will be run sequentially. Connection +and disconnection will be done automatically at the start and end of the execution +of the tool, if such commands are not explicitly given. Automatic connection will +search for the first available serial device. If no action is specified then the +REPL will be entered. + +Shortcuts can be defined using the macro system. Built-in shortcuts are: + +- a0, a1, a2, a3: connect to `/dev/ttyACM?` +- u0, u1, u2, u3: connect to `/dev/ttyUSB?` +- c0, c1, c2, c3: connect to `COM?` +- cat, ls, cp, rm, mkdir, rmdir, df: filesystem commands +- reset: reset the device +- bootloader: make the device enter its bootloader + +Any user configuration, including user-defined shortcuts, can be placed in +.config/mpremote/config.py. For example: + + # Custom macro commands + commands = { + "c33": "connect id:334D335C3138", + "bl": "bootloader", + "double x=4": "eval x*2", + } + +Examples: + + mpremote + mpremote a1 + mpremote connect /dev/ttyUSB0 repl + mpremote ls + mpremote a1 ls + mpremote exec "import micropython; micropython.mem_info()" + mpremote eval 1/2 eval 3/4 + mpremote mount . + mpremote mount . exec "import local_script" + mpremote ls + mpremote cat boot.py + mpremote cp :main.py . + mpremote cp main.py : + mpremote cp -r dir/ : diff --git a/tools/mpremote/mpremote.py b/tools/mpremote/mpremote.py new file mode 100755 index 0000000000000..a91ff67b15da6 --- /dev/null +++ b/tools/mpremote/mpremote.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +import sys +from mpremote import main + +sys.exit(main.main()) diff --git a/tools/mpremote/mpremote/__init__.py b/tools/mpremote/mpremote/__init__.py new file mode 100644 index 0000000000000..1bb8bf6d7fd4c --- /dev/null +++ b/tools/mpremote/mpremote/__init__.py @@ -0,0 +1 @@ +# empty diff --git a/tools/mpremote/mpremote/console.py b/tools/mpremote/mpremote/console.py new file mode 100644 index 0000000000000..646bbfa22e88e --- /dev/null +++ b/tools/mpremote/mpremote/console.py @@ -0,0 +1,162 @@ +import sys + +try: + import select, termios +except ImportError: + termios = None + select = None + import msvcrt + + +class ConsolePosix: + def __init__(self): + self.infd = sys.stdin.fileno() + self.infile = sys.stdin.buffer.raw + self.outfile = sys.stdout.buffer.raw + self.orig_attr = termios.tcgetattr(self.infd) + + def enter(self): + # attr is: [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] + attr = termios.tcgetattr(self.infd) + attr[0] &= ~( + termios.BRKINT | termios.ICRNL | termios.INPCK | termios.ISTRIP | termios.IXON + ) + attr[1] = 0 + attr[2] = attr[2] & ~(termios.CSIZE | termios.PARENB) | termios.CS8 + attr[3] = 0 + attr[6][termios.VMIN] = 1 + attr[6][termios.VTIME] = 0 + termios.tcsetattr(self.infd, termios.TCSANOW, attr) + + def exit(self): + termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr) + + def waitchar(self): + # TODO pyb.serial might not have fd + select.select([console_in.infd, pyb.serial.fd], [], []) + + def readchar(self): + res = select.select([self.infd], [], [], 0) + if res[0]: + return self.infile.read(1) + else: + return None + + def write(self, buf): + self.outfile.write(buf) + + +class ConsoleWindows: + KEY_MAP = { + b"H": b"A", # UP + b"P": b"B", # DOWN + b"M": b"C", # RIGHT + b"K": b"D", # LEFT + b"G": b"H", # POS1 + b"O": b"F", # END + b"Q": b"6~", # PGDN + b"I": b"5~", # PGUP + b"s": b"1;5D", # CTRL-LEFT, + b"t": b"1;5C", # CTRL-RIGHT, + b"\x8d": b"1;5A", # CTRL-UP, + b"\x91": b"1;5B", # CTRL-DOWN, + b"w": b"1;5H", # CTRL-POS1 + b"u": b"1;5F", # CTRL-END + b"\x98": b"1;3A", # ALT-UP, + b"\xa0": b"1;3B", # ALT-DOWN, + b"\x9d": b"1;3C", # ALT-RIGHT, + b"\x9b": b"1;3D", # ALT-LEFT, + b"\x97": b"1;3H", # ALT-POS1, + b"\x9f": b"1;3F", # ALT-END, + b"S": b"3~", # DEL, + b"\x93": b"3;5~", # CTRL-DEL + b"R": b"2~", # INS + b"\x92": b"2;5~", # CTRL-INS + b"\x94": b"Z", # Ctrl-Tab = BACKTAB, + } + + def enter(self): + pass + + def exit(self): + pass + + def inWaiting(self): + return 1 if msvcrt.kbhit() else 0 + + def waitchar(self): + while not (self.inWaiting() or pyb.serial.inWaiting()): + time.sleep(0.01) + + def readchar(self): + if msvcrt.kbhit(): + ch = msvcrt.getch() + while ch in b"\x00\xe0": # arrow or function key prefix? + if not msvcrt.kbhit(): + return None + ch = msvcrt.getch() # second call returns the actual key code + try: + ch = b"\x1b[" + self.KEY_MAP[ch] + except KeyError: + return None + return ch + + def write(self, buf): + buf = buf.decode() if isinstance(buf, bytes) else buf + sys.stdout.write(buf) + sys.stdout.flush() + # for b in buf: + # if isinstance(b, bytes): + # msvcrt.putch(b) + # else: + # msvcrt.putwch(b) + + +if termios: + Console = ConsolePosix + VT_ENABLED = True +else: + Console = ConsoleWindows + + # Windows VT mode ( >= win10 only) + # https://bugs.python.org/msg291732 + import ctypes + from ctypes import wintypes + + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) + + ERROR_INVALID_PARAMETER = 0x0057 + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 + + def _check_bool(result, func, args): + if not result: + raise ctypes.WinError(ctypes.get_last_error()) + return args + + LPDWORD = ctypes.POINTER(wintypes.DWORD) + kernel32.GetConsoleMode.errcheck = _check_bool + kernel32.GetConsoleMode.argtypes = (wintypes.HANDLE, LPDWORD) + kernel32.SetConsoleMode.errcheck = _check_bool + kernel32.SetConsoleMode.argtypes = (wintypes.HANDLE, wintypes.DWORD) + + def set_conout_mode(new_mode, mask=0xFFFFFFFF): + # don't assume StandardOutput is a console. + # open CONOUT$ instead + fdout = os.open("CONOUT$", os.O_RDWR) + try: + hout = msvcrt.get_osfhandle(fdout) + old_mode = wintypes.DWORD() + kernel32.GetConsoleMode(hout, ctypes.byref(old_mode)) + mode = (new_mode & mask) | (old_mode.value & ~mask) + kernel32.SetConsoleMode(hout, mode) + return old_mode.value + finally: + os.close(fdout) + + # def enable_vt_mode(): + mode = mask = ENABLE_VIRTUAL_TERMINAL_PROCESSING + try: + set_conout_mode(mode, mask) + VT_ENABLED = True + except WindowsError as e: + VT_ENABLED = False diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py new file mode 100644 index 0000000000000..1ce00305eb525 --- /dev/null +++ b/tools/mpremote/mpremote/main.py @@ -0,0 +1,477 @@ +""" +MicroPython Remote - Interaction and automation tool for MicroPython +MIT license; Copyright (c) 2019-2021 Damien P. George + +This program provides a set of utilities to interact with and automate a +MicroPython device over a serial connection. Commands supported are: + + mpremote -- auto-detect, connect and enter REPL + mpremote -- connect to given device + mpremote connect -- connect to given device + mpremote disconnect -- disconnect current device + mpremote mount -- mount local directory on device + mpremote eval -- evaluate and print the string + mpremote exec -- execute the string + mpremote run \r +""" + ) + cl.close() + def setup_conn(port, accept_handler): global listen_s @@ -25,34 +104,41 @@ def setup_conn(port, accept_handler): for i in (network.AP_IF, network.STA_IF): iface = network.WLAN(i) if iface.active(): - print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port)) + print("WebREPL server started on http://%s:%d/" % (iface.ifconfig()[0], port)) return listen_s def accept_conn(listen_sock): global client_s cl, remote_addr = listen_sock.accept() - prev = uos.dupterm(None) - uos.dupterm(prev) + + if not server_handshake(cl): + send_html(cl) + return False + + prev = os.dupterm(None) + os.dupterm(prev) if prev: print("\nConcurrent WebREPL connection from", remote_addr, "rejected") cl.close() - return + return False print("\nWebREPL connection from:", remote_addr) client_s = cl - websocket_helper.server_handshake(cl) - ws = uwebsocket.websocket(cl, True) + + ws = websocket.websocket(cl, True) ws = _webrepl._webrepl(ws) cl.setblocking(False) # notify REPL on socket incoming data (ESP32/ESP8266-only) - if hasattr(uos, "dupterm_notify"): - cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify) - uos.dupterm(ws) + if hasattr(os, "dupterm_notify"): + cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify) + os.dupterm(ws) + + return True def stop(): global listen_s, client_s - uos.dupterm(None) + os.dupterm(None) if client_s: client_s.close() if listen_s: @@ -60,6 +146,7 @@ def stop(): def start(port=8266, password=None, accept_handler=accept_conn): + global static_host stop() webrepl_pass = password if webrepl_pass is None: @@ -67,6 +154,8 @@ def start(port=8266, password=None, accept_handler=accept_conn): import webrepl_cfg webrepl_pass = webrepl_cfg.PASS + if hasattr(webrepl_cfg, "BASE"): + static_host = webrepl_cfg.BASE except: print("WebREPL is not configured, run 'import webrepl_setup'") @@ -75,7 +164,9 @@ def start(port=8266, password=None, accept_handler=accept_conn): if accept_handler is None: print("Starting webrepl in foreground mode") - accept_conn(s) + # Run accept_conn to serve HTML until we get a websocket connection. + while not accept_conn(s): + pass elif password is None: print("Started webrepl in normal mode") else: diff --git a/extmod/webrepl/webrepl_setup.py b/extmod/webrepl/webrepl_setup.py index ffc9c77fc0deb..16e5f76e655e4 100644 --- a/extmod/webrepl/webrepl_setup.py +++ b/extmod/webrepl/webrepl_setup.py @@ -1,6 +1,5 @@ import sys -# import uos as os import os import machine diff --git a/extmod/webrepl/websocket_helper.py b/extmod/webrepl/websocket_helper.py deleted file mode 100644 index 3260acc52f377..0000000000000 --- a/extmod/webrepl/websocket_helper.py +++ /dev/null @@ -1,85 +0,0 @@ -try: - import usys as sys -except ImportError: - import sys - -try: - import ubinascii as binascii -except: - import binascii -try: - import uhashlib as hashlib -except: - import hashlib - -DEBUG = 0 - - -def server_handshake(sock): - clr = sock.makefile("rwb", 0) - l = clr.readline() - # sys.stdout.write(repr(l)) - - webkey = None - - while 1: - l = clr.readline() - if not l: - raise OSError("EOF in headers") - if l == b"\r\n": - break - # sys.stdout.write(l) - h, v = [x.strip() for x in l.split(b":", 1)] - if DEBUG: - print((h, v)) - if h == b"Sec-WebSocket-Key": - webkey = v - - if not webkey: - raise OSError("Not a websocket request") - - if DEBUG: - print("Sec-WebSocket-Key:", webkey, len(webkey)) - - d = hashlib.sha1(webkey) - d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") - respkey = d.digest() - respkey = binascii.b2a_base64(respkey)[:-1] - if DEBUG: - print("respkey:", respkey) - - sock.send( - b"""\ -HTTP/1.1 101 Switching Protocols\r -Upgrade: websocket\r -Connection: Upgrade\r -Sec-WebSocket-Accept: """ - ) - sock.send(respkey) - sock.send("\r\n\r\n") - - -# Very simplified client handshake, works for MicroPython's -# websocket server implementation, but probably not for other -# servers. -def client_handshake(sock): - cl = sock.makefile("rwb", 0) - cl.write( - b"""\ -GET / HTTP/1.1\r -Host: echo.websocket.org\r -Connection: Upgrade\r -Upgrade: websocket\r -Sec-WebSocket-Key: foo\r -\r -""" - ) - l = cl.readline() - # print(l) - while 1: - l = cl.readline() - if l == b"\r\n": - break - - -# sys.stdout.write(l) From f736afb577f91cee2a53a22ea17b61755e3f44fc Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Sun, 17 Jul 2022 08:43:33 +1000 Subject: [PATCH 2402/3533] drivers,ports: Fix a few typos in comments. Fixes: - Should read `definitions` rather than `defintions`. - Should read `resolution` rather than `resoultion`. - Should read `inefficient` rather than `inefficent`. - Should read `closed` rather than `closded`. Signed-off-by: Tim Gates --- drivers/cc3000/src/evnt_handler.c | 2 +- ports/mimxrt/hal/flexspi_flash_config.h | 4 ++-- ports/stm32/adc.c | 6 +++--- ports/teensy/core/pins_teensy.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/cc3000/src/evnt_handler.c b/drivers/cc3000/src/evnt_handler.c index 80f34e469b244..d430bbfdad555 100644 --- a/drivers/cc3000/src/evnt_handler.c +++ b/drivers/cc3000/src/evnt_handler.c @@ -604,7 +604,7 @@ INT32 hci_unsol_event_handler(CHAR *event_hdr) { //data[0] represents the socket id, for which FIN was received by remote. //Upon receiving this event, the user can close the socket, or else the - //socket will be closded after inacvitity timeout (by default 60 seconds) + //socket will be closed after inacvitity timeout (by default 60 seconds) tSLInformation.sWlanCB(event_type, data, 1); } } diff --git a/ports/mimxrt/hal/flexspi_flash_config.h b/ports/mimxrt/hal/flexspi_flash_config.h index 3c21eb609a77c..80526880be0b1 100644 --- a/ports/mimxrt/hal/flexspi_flash_config.h +++ b/ports/mimxrt/hal/flexspi_flash_config.h @@ -18,7 +18,7 @@ #define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*@}*/ -/* FLEXSPI memory config block related defintions */ +/* FLEXSPI memory config block related definitions */ #define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian #define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0 #define FLEXSPI_CFG_BLK_SIZE (512) @@ -26,7 +26,7 @@ /* FLEXSPI Feature related definitions */ #define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1 -/* Lookup table related defintions */ +/* Lookup table related definitions */ #define CMD_INDEX_READ 0 #define CMD_INDEX_READSTATUS 1 #define CMD_INDEX_WRITEENABLE 2 diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index fcc7d51e45276..9b7788e59e337 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -50,7 +50,7 @@ /// val = adc.read_core_vbat() # read MCU VBAT /// val = adc.read_core_vref() # read MCU VREF -/* ADC defintions */ +/* ADC definitions */ #define ADCx (ADC1) #define PIN_ADC_MASK PIN_ADC1 #define pin_adc_table pin_adc1 @@ -171,8 +171,8 @@ #define EOC_TIMEOUT (10) /* Core temperature sensor definitions */ -#define CORE_TEMP_V25 (943) /* (0.76v/3.3v)*(2^ADC resoultion) */ -#define CORE_TEMP_AVG_SLOPE (3) /* (2.5mv/3.3v)*(2^ADC resoultion) */ +#define CORE_TEMP_V25 (943) /* (0.76v/3.3v)*(2^ADC resolution) */ +#define CORE_TEMP_AVG_SLOPE (3) /* (2.5mv/3.3v)*(2^ADC resolution) */ // scale and calibration values for VBAT and VREF #define ADC_SCALE (ADC_SCALE_V / ((1 << ADC_CAL_BITS) - 1)) diff --git a/ports/teensy/core/pins_teensy.c b/ports/teensy/core/pins_teensy.c index b28f94a9ecb0c..5817121fad389 100644 --- a/ports/teensy/core/pins_teensy.c +++ b/ports/teensy/core/pins_teensy.c @@ -181,7 +181,7 @@ void portb_isr(void) void portc_isr(void) { - // TODO: these are inefficent. Use CLZ somehow.... + // TODO: these are inefficient. Use CLZ somehow.... uint32_t isfr = PORTC_ISFR; PORTC_ISFR = isfr; if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9](); From fa15aed0f718562871288aa174e91507a134db28 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Fri, 22 Jul 2022 17:28:30 +0100 Subject: [PATCH 2403/3533] docs/library/neopixel: Add note that neopixel is included in rp2 builds. --- docs/library/neopixel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/neopixel.rst b/docs/library/neopixel.rst index 1b37f088baa71..bc3b4e68a40eb 100644 --- a/docs/library/neopixel.rst +++ b/docs/library/neopixel.rst @@ -6,7 +6,7 @@ This module provides a driver for WS2818 / NeoPixel LEDs. -.. note:: This module is only included by default on the ESP8266 and ESP32 +.. note:: This module is only included by default on the ESP8266, ESP32 and RP2 ports. On STM32 / Pyboard, you can `download the module `_ and copy it to the filesystem. From 1e87b56219c69306d77a887cac3d29146180f113 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 4 Jul 2022 17:35:46 +1000 Subject: [PATCH 2404/3533] py/obj: Add support for __float__ and __complex__ functions. --- py/obj.c | 15 +++++-- py/objcomplex.c | 4 ++ py/objtype.c | 6 +++ py/runtime.c | 9 ++++ py/runtime0.h | 2 + .../types_float_implicit_conversion.py | 14 +++++++ tests/float/complex_dunder.py | 40 ++++++++++++++++++ tests/float/float_dunder.py | 42 +++++++++++++++++++ tests/run-tests.py | 1 + 9 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 tests/cpydiff/types_float_implicit_conversion.py create mode 100644 tests/float/complex_dunder.py create mode 100644 tests/float/float_dunder.py diff --git a/py/obj.c b/py/obj.c index 51f6d85defb79..5a05ea58c5ddb 100644 --- a/py/obj.c +++ b/py/obj.c @@ -355,9 +355,13 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) { } else if (mp_obj_is_float(arg)) { val = mp_obj_float_get(arg); } else { - return false; + arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg); + if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) { + val = mp_obj_float_get(arg); + } else { + return false; + } } - *value = val; return true; } @@ -399,7 +403,12 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) } else if (mp_obj_is_type(arg, &mp_type_complex)) { mp_obj_complex_get(arg, real, imag); } else { - return false; + arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg); + if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) { + mp_obj_complex_get(arg, real, imag); + } else { + return false; + } } return true; } diff --git a/py/objcomplex.c b/py/objcomplex.c index 157617e156774..3c4cb66140987 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -88,6 +88,10 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si // a complex, just return it return args[0]; } else { + mp_float_t real, imag; + if (mp_obj_get_complex_maybe(args[0], &real, &imag)) { + return mp_obj_new_complex(real, imag); + } // something else, try to cast it to a complex return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); } diff --git a/py/objtype.c b/py/objtype.c index 37c1e3bd22d30..fe1918bd3731f 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -378,6 +378,12 @@ const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = { [MP_UNARY_OP_INVERT] = MP_QSTR___invert__, [MP_UNARY_OP_ABS] = MP_QSTR___abs__, #endif + #if MICROPY_PY_BUILTINS_FLOAT + [MP_UNARY_OP_FLOAT_MAYBE] = MP_QSTR___float__, + #if MICROPY_PY_BUILTINS_COMPLEX + [MP_UNARY_OP_COMPLEX_MAYBE] = MP_QSTR___complex__, + #endif + #endif #if MICROPY_PY_SYS_GETSIZEOF [MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__, #endif diff --git a/py/runtime.c b/py/runtime.c index e6d8c68070e5c..2c3b3ddde4a4f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -319,6 +319,15 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { // if arg==mp_const_none. return mp_const_true; } + #if MICROPY_PY_BUILTINS_FLOAT + if (op == MP_UNARY_OP_FLOAT_MAYBE + #if MICROPY_PY_BUILTINS_COMPLEX + || op == MP_UNARY_OP_COMPLEX_MAYBE + #endif + ) { + return MP_OBJ_NULL; + } + #endif // With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int(). // In this case provide a more focused error message to not confuse, e.g. chr(1.0) #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE diff --git a/py/runtime0.h b/py/runtime0.h index e6eeff97d6868..c82a4717f4516 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -76,6 +76,8 @@ typedef enum { MP_UNARY_OP_HASH, // __hash__; must return a small int MP_UNARY_OP_ABS, // __abs__ MP_UNARY_OP_INT, // __int__ + MP_UNARY_OP_FLOAT_MAYBE, // __float__ + MP_UNARY_OP_COMPLEX_MAYBE, // __complex__ MP_UNARY_OP_SIZEOF, // for sys.getsizeof() } mp_unary_op_t; diff --git a/tests/cpydiff/types_float_implicit_conversion.py b/tests/cpydiff/types_float_implicit_conversion.py new file mode 100644 index 0000000000000..8d39a7cd41098 --- /dev/null +++ b/tests/cpydiff/types_float_implicit_conversion.py @@ -0,0 +1,14 @@ +""" +categories: Types,float +description: uPy allows implicit conversion of objects in maths operations while CPython does not. +cause: Unknown +workaround: Objects should be wrapped in `float(obj)` for compatibility with CPython. +""" + + +class Test: + def __float__(self): + return 0.5 + + +print(2.0 * Test()) diff --git a/tests/float/complex_dunder.py b/tests/float/complex_dunder.py new file mode 100644 index 0000000000000..128dc69293e28 --- /dev/null +++ b/tests/float/complex_dunder.py @@ -0,0 +1,40 @@ +# test __complex__ function support + + +class TestComplex: + def __complex__(self): + return 1j + 10 + + +class TestStrComplex: + def __complex__(self): + return "a" + + +class TestNonComplex: + def __complex__(self): + return 6 + + +class Test: + pass + + +print(complex(TestComplex())) + +try: + print(complex(TestStrComplex())) +except TypeError: + print("TypeError") + + +try: + print(complex(TestNonComplex())) +except TypeError: + print("TypeError") + + +try: + print(complex(Test())) +except TypeError: + print("TypeError") diff --git a/tests/float/float_dunder.py b/tests/float/float_dunder.py new file mode 100644 index 0000000000000..1cd03db524a74 --- /dev/null +++ b/tests/float/float_dunder.py @@ -0,0 +1,42 @@ +# test __float__ function support + + +class TestFloat: + def __float__(self): + return 10.0 + + +class TestStrFloat: + def __float__(self): + return "a" + + +class TestNonFloat: + def __float__(self): + return 6 + + +class Test: + pass + + +print("%.1f" % float(TestFloat())) +print("%.1f" % TestFloat()) + + +try: + print(float(TestStrFloat())) +except TypeError: + print("TypeError") + + +try: + print(float(TestNonFloat())) +except TypeError: + print("TypeError") + + +try: + print(float(Test())) +except TypeError: + print("TypeError") diff --git a/tests/run-tests.py b/tests/run-tests.py index 2d6dbaaf1cd84..baab7bdd4a90d 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -525,6 +525,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1): skip_tests.add("float/int_big_float.py") skip_tests.add("float/true_value.py") skip_tests.add("float/types.py") + skip_tests.add("float/complex_dunder.py") if not has_coverage: skip_tests.add("cmdline/cmd_parsetree.py") From 4fe3e493b1a62381db15b724f77d565ff2666120 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 25 Jul 2022 15:23:48 +1000 Subject: [PATCH 2405/3533] py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first. This commit simplifies mp_obj_get_complex_maybe() by first calling mp_obj_get_float_maybe() to handle the cases corresponding to floats. Only if that fails does it attempt to extra a full complex number. This reduces code size and also means that mp_obj_get_complex_maybe() now supports user-defined classes defining __float__; in particular this allows user-defined classes to be used as arguments to cmath-module function. Furthermore, complex_make_new() can now be simplified to directly call mp_obj_get_complex(), instead of mp_obj_get_complex_maybe() followed by mp_obj_get_float(). This also improves error messages from complex with an invalid argument, it now raises "can't convert to complex" rather than "can't convert to float". Signed-off-by: Damien George --- py/obj.c | 17 +---------------- py/objcomplex.c | 7 ++----- tests/float/cmath_dunder.py | 21 +++++++++++++++++++++ tests/float/complex_dunder.py | 6 ++++++ tests/float/math_dunder.py | 15 +++++++++++++++ 5 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 tests/float/cmath_dunder.py create mode 100644 tests/float/math_dunder.py diff --git a/py/obj.c b/py/obj.c index 5a05ea58c5ddb..b461fe50aa25d 100644 --- a/py/obj.c +++ b/py/obj.c @@ -383,22 +383,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) { #if MICROPY_PY_BUILTINS_COMPLEX bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { - if (arg == mp_const_false) { - *real = 0; - *imag = 0; - } else if (arg == mp_const_true) { - *real = 1; - *imag = 0; - } else if (mp_obj_is_small_int(arg)) { - *real = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg); - *imag = 0; - #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE - } else if (mp_obj_is_exact_type(arg, &mp_type_int)) { - *real = mp_obj_int_as_float_impl(arg); - *imag = 0; - #endif - } else if (mp_obj_is_float(arg)) { - *real = mp_obj_float_get(arg); + if (mp_obj_get_float_maybe(arg, real)) { *imag = 0; } else if (mp_obj_is_type(arg, &mp_type_complex)) { mp_obj_complex_get(arg, real, imag); diff --git a/py/objcomplex.c b/py/objcomplex.c index 3c4cb66140987..4aa598a0bcb7d 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -89,11 +89,8 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si return args[0]; } else { mp_float_t real, imag; - if (mp_obj_get_complex_maybe(args[0], &real, &imag)) { - return mp_obj_new_complex(real, imag); - } - // something else, try to cast it to a complex - return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); + mp_obj_get_complex(args[0], &real, &imag); + return mp_obj_new_complex(real, imag); } case 2: diff --git a/tests/float/cmath_dunder.py b/tests/float/cmath_dunder.py new file mode 100644 index 0000000000000..3526341510e91 --- /dev/null +++ b/tests/float/cmath_dunder.py @@ -0,0 +1,21 @@ +# test that cmath functions support user classes with __float__ and __complex__ + +try: + import cmath +except ImportError: + print("SKIP") + raise SystemExit + + +class TestFloat: + def __float__(self): + return 1.0 + + +class TestComplex: + def __complex__(self): + return 1j + 10 + + +for clas in TestFloat, TestComplex: + print("%.5g" % cmath.phase(clas())) diff --git a/tests/float/complex_dunder.py b/tests/float/complex_dunder.py index 128dc69293e28..975d829b47f3d 100644 --- a/tests/float/complex_dunder.py +++ b/tests/float/complex_dunder.py @@ -1,6 +1,11 @@ # test __complex__ function support +class TestFloat: + def __float__(self): + return 1.0 + + class TestComplex: def __complex__(self): return 1j + 10 @@ -20,6 +25,7 @@ class Test: pass +print(complex(TestFloat())) print(complex(TestComplex())) try: diff --git a/tests/float/math_dunder.py b/tests/float/math_dunder.py new file mode 100644 index 0000000000000..33ea7f7c1c105 --- /dev/null +++ b/tests/float/math_dunder.py @@ -0,0 +1,15 @@ +# test that math functions support user classes with __float__ + +try: + import math +except ImportError: + print("SKIP") + raise SystemExit + + +class TestFloat: + def __float__(self): + return 1.0 + + +print("%.5g" % math.exp(TestFloat())) From 9fd8250d6962d016942ac48e0d20767124bcd732 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Thu, 21 Jul 2022 12:04:19 +0100 Subject: [PATCH 2406/3533] lib/cyw43-driver: Update driver to latest version. This version of the driver adds an event hook to call during firmware download, and the ability to query the current power mode. --- lib/cyw43-driver | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cyw43-driver b/lib/cyw43-driver index 5614e2750e019..2ab6ca93f9cd0 160000 --- a/lib/cyw43-driver +++ b/lib/cyw43-driver @@ -1 +1 @@ -Subproject commit 5614e2750e019b0ffa14a50e3c92045b952e2634 +Subproject commit 2ab6ca93f9cd044bc6f35c1403b1284e4161294a From 33d6994d4c5855228ae423f2d77fb2549db9f854 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Thu, 14 Jul 2022 18:11:34 +0100 Subject: [PATCH 2407/3533] rp2/cyw43_configport: Set CYW43_EVENT_POLL_HOOK value. This should allow USB to work while we're loading WiFi firmware. Fixes issue #8904. --- ports/rp2/cyw43_configport.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/rp2/cyw43_configport.h b/ports/rp2/cyw43_configport.h index 6173964cb2bef..aeb220b202b1f 100644 --- a/ports/rp2/cyw43_configport.h +++ b/ports/rp2/cyw43_configport.h @@ -95,4 +95,6 @@ static inline void cyw43_delay_ms(uint32_t ms) { } } +#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK_FAST + #endif // MICROPY_INCLUDED_RP2_CYW43_CONFIGPORT_H From f64862a766bf7c22feb1de75dadc69fb9e1aed4a Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Jul 2022 01:37:03 +1000 Subject: [PATCH 2408/3533] rp2/cyw43_configport: Set CYW43_WIFI_NVRAM_INCLUDE_FILE value. Required for latest cyw43-driver. Signed-off-by: Damien George --- ports/rp2/cyw43_configport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/cyw43_configport.h b/ports/rp2/cyw43_configport.h index aeb220b202b1f..b6f5b3cad8a75 100644 --- a/ports/rp2/cyw43_configport.h +++ b/ports/rp2/cyw43_configport.h @@ -32,6 +32,7 @@ #include "py/mphal.h" #include "pendsv.h" +#define CYW43_WIFI_NVRAM_INCLUDE_FILE "wifi_nvram_43439.h" #define CYW43_IOCTL_TIMEOUT_US (1000000) #define CYW43_SLEEP_MAX (10) #define CYW43_NETUTILS (1) From 0c45a28d24e1a7f9ea5912c928d7b5a860a6514a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 24 Jul 2022 09:15:38 +0200 Subject: [PATCH 2409/3533] rp2/rp2_pio: Fix StateMachine.restart when PIO program is shared. The state machines were not properly restarted in the case that the same PIO program was shared among multiple StateMachine instances. This is because only the first StateMachine to use the program would set the rp2_state_machine_initial_pc variable. See https://forum.micropython.org/viewtopic.php?f=21&t=12776&p=69464#p69464 --- ports/rp2/rp2_pio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 072644eba6acc..eb85eb22480d8 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -469,8 +469,8 @@ STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel if (offset < 0) { rp2_pio_add_program(&rp2_pio_obj[PIO_NUM(self->pio)], args[ARG_prog].u_obj); offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]); - rp2_state_machine_initial_pc[self->id] = offset; } + rp2_state_machine_initial_pc[self->id] = offset; // Compute the clock divider. uint16_t clkdiv_int; From c0fa903d6b2ed5131ae60f8faff2c6ad5276b3a2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Jul 2022 12:24:50 +1000 Subject: [PATCH 2410/3533] py/compile: Support large integers in inline-asm data directive. Fixes issue #8956. Signed-off-by: Damien George --- py/compile.c | 5 +++-- tests/inlineasm/asmdata.py | 16 ++++++++++++++++ tests/inlineasm/asmdata.py.exp | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/inlineasm/asmdata.py create mode 100644 tests/inlineasm/asmdata.py.exp diff --git a/py/compile.c b/py/compile.c index ff3e5ffc6db30..8aaf885327497 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3286,12 +3286,13 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind if (pass > MP_PASS_SCOPE) { mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]); for (uint j = 1; j < n_args; j++) { - if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) { + mp_obj_t int_obj; + if (!mp_parse_node_get_int_maybe(pn_arg[j], &int_obj)) { compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("'data' requires integer arguments")); return; } mp_asm_base_data((mp_asm_base_t *)comp->emit_inline_asm, - bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j])); + bytesize, mp_obj_int_get_truncated(int_obj)); } } } else { diff --git a/tests/inlineasm/asmdata.py b/tests/inlineasm/asmdata.py new file mode 100644 index 0000000000000..bbd20c9186a4d --- /dev/null +++ b/tests/inlineasm/asmdata.py @@ -0,0 +1,16 @@ +# test the "data" directive + + +@micropython.asm_thumb +def ret_num(r0) -> uint: + lsl(r0, r0, 2) + mov(r1, pc) + add(r0, r0, r1) + ldr(r0, [r0, 4]) + b(HERE) + data(4, 0x12345678, 0x20000000, 0x40000000, 0x7FFFFFFF + 1, (1 << 32) - 2) + label(HERE) + + +for i in range(5): + print(hex(ret_num(i))) diff --git a/tests/inlineasm/asmdata.py.exp b/tests/inlineasm/asmdata.py.exp new file mode 100644 index 0000000000000..502c04f99314c --- /dev/null +++ b/tests/inlineasm/asmdata.py.exp @@ -0,0 +1,5 @@ +0x12345678 +0x20000000 +0x40000000 +0x80000000 +0xfffffffe From e65d1e69e88268145ff0e7e73240f028885915be Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 22 Jul 2022 13:53:13 +1000 Subject: [PATCH 2411/3533] py/modio: Remove FileIO and TextIOWrapper from io module. On ports with more than one filesystem, the type will be wrong, for example if using LFS but FAT enabled, then the type will be FAT. So it's not possible to use these classes to identify a file object type. Furthermore, constructing an io.FileIO currently crashes on FAT, and make_new isn't supported on LFS. And the io.TextIOWrapper class does not match CPython at all. Signed-off-by: Jim Mussared --- extmod/vfs.c | 2 +- extmod/vfs_fat_file.c | 127 +++++++++++++++----------------------- extmod/vfs_posix.c | 2 +- extmod/vfs_posix_file.c | 19 +----- py/modio.c | 9 --- tests/extmod/vfs_posix.py | 4 +- 6 files changed, 58 insertions(+), 105 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index 2799622b389b2..be1a82d404474 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -312,7 +312,7 @@ mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) #if MICROPY_VFS_POSIX // If the file is an integer then delegate straight to the POSIX handler if (mp_obj_is_small_int(args[ARG_file].u_obj)) { - return mp_vfs_posix_file_open(&mp_type_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj); + return mp_vfs_posix_file_open(&mp_type_vfs_posix_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj); } #endif diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 537101d00f406..ebf36fc3978c4 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -151,72 +151,6 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, } } -// Note: encoding is ignored for now; it's also not a valid kwarg for CPython's FileIO, -// but by adding it here we can use one single mp_arg_t array for open() and FileIO's constructor -STATIC const mp_arg_t file_open_args[] = { - { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, - { MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_r)} }, - { MP_QSTR_encoding, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = MP_ROM_NONE} }, -}; -#define FILE_OPEN_NUM_ARGS MP_ARRAY_SIZE(file_open_args) - -STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_arg_val_t *args) { - int mode = 0; - const char *mode_s = mp_obj_str_get_str(args[1].u_obj); - // TODO make sure only one of r, w, x, a, and b, t are specified - while (*mode_s) { - switch (*mode_s++) { - case 'r': - mode |= FA_READ; - break; - case 'w': - mode |= FA_WRITE | FA_CREATE_ALWAYS; - break; - case 'x': - mode |= FA_WRITE | FA_CREATE_NEW; - break; - case 'a': - mode |= FA_WRITE | FA_OPEN_ALWAYS; - break; - case '+': - mode |= FA_READ | FA_WRITE; - break; - #if MICROPY_PY_IO_FILEIO - case 'b': - type = &mp_type_vfs_fat_fileio; - break; - #endif - case 't': - type = &mp_type_vfs_fat_textio; - break; - } - } - - pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t); - o->base.type = type; - - const char *fname = mp_obj_str_get_str(args[0].u_obj); - assert(vfs != NULL); - FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); - if (res != FR_OK) { - m_del_obj(pyb_file_obj_t, o); - mp_raise_OSError(fresult_to_errno_table[res]); - } - - // for 'a' mode, we must begin at the end of the file - if ((mode & FA_OPEN_ALWAYS) != 0) { - f_lseek(&o->fp, f_size(&o->fp)); - } - - return MP_OBJ_FROM_PTR(o); -} - -STATIC mp_obj_t file_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; - mp_arg_parse_all_kw_array(n_args, n_kw, args, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(NULL, type, arg_vals); -} - // TODO gc hook to close the file if not already closed STATIC const mp_rom_map_elem_t vfs_fat_rawfile_locals_dict_table[] = { @@ -247,7 +181,6 @@ const mp_obj_type_t mp_type_vfs_fat_fileio = { { &mp_type_type }, .name = MP_QSTR_FileIO, .print = file_obj_print, - .make_new = file_obj_make_new, .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &vfs_fat_fileio_stream_p, @@ -266,7 +199,6 @@ const mp_obj_type_t mp_type_vfs_fat_textio = { { &mp_type_type }, .name = MP_QSTR_TextIOWrapper, .print = file_obj_print, - .make_new = file_obj_make_new, .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &vfs_fat_textio_stream_p, @@ -274,15 +206,58 @@ const mp_obj_type_t mp_type_vfs_fat_textio = { }; // Factory function for I/O stream classes -STATIC mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode) { - // TODO: analyze buffering args and instantiate appropriate type +STATIC mp_obj_t fat_vfs_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) { fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); - mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; - arg_vals[0].u_obj = path; - arg_vals[1].u_obj = mode; - arg_vals[2].u_obj = mp_const_none; - return file_open(self, &mp_type_vfs_fat_textio, arg_vals); + + const mp_obj_type_t *type = &mp_type_vfs_fat_textio; + int mode = 0; + const char *mode_s = mp_obj_str_get_str(mode_in); + // TODO make sure only one of r, w, x, a, and b, t are specified + while (*mode_s) { + switch (*mode_s++) { + case 'r': + mode |= FA_READ; + break; + case 'w': + mode |= FA_WRITE | FA_CREATE_ALWAYS; + break; + case 'x': + mode |= FA_WRITE | FA_CREATE_NEW; + break; + case 'a': + mode |= FA_WRITE | FA_OPEN_ALWAYS; + break; + case '+': + mode |= FA_READ | FA_WRITE; + break; + #if MICROPY_PY_IO_FILEIO + case 'b': + type = &mp_type_vfs_fat_fileio; + break; + #endif + case 't': + type = &mp_type_vfs_fat_textio; + break; + } + } + + pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t); + o->base.type = type; + + const char *fname = mp_obj_str_get_str(path_in); + FRESULT res = f_open(&self->fatfs, &o->fp, fname, mode); + if (res != FR_OK) { + m_del_obj(pyb_file_obj_t, o); + mp_raise_OSError(fresult_to_errno_table[res]); + } + + // for 'a' mode, we must begin at the end of the file + if ((mode & FA_OPEN_ALWAYS) != 0) { + f_lseek(&o->fp, f_size(&o->fp)); + } + + return MP_OBJ_FROM_PTR(o); } -MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fatfs_builtin_open_self); +MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fat_vfs_open); #endif // MICROPY_VFS && MICROPY_VFS_FAT diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index 1ada596d137c1..9b00365817a17 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -138,7 +138,7 @@ STATIC mp_obj_t vfs_posix_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode if (!mp_obj_is_small_int(path_in)) { path_in = vfs_posix_get_path_obj(self, path_in); } - return mp_vfs_posix_file_open(&mp_type_textio, path_in, mode_in); + return mp_vfs_posix_file_open(&mp_type_vfs_posix_textio, path_in, mode_in); } STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_open_obj, vfs_posix_open); diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 837c5489b0e14..795ad7bbd9ea4 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -111,17 +111,6 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_ return MP_OBJ_FROM_PTR(o); } -STATIC mp_obj_t vfs_posix_file_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - static const mp_arg_t allowed_args[] = { - { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, - { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} }, - }; - - mp_arg_val_t arg_vals[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, arg_vals); - return mp_vfs_posix_file_open(type, arg_vals[0].u_obj, arg_vals[1].u_obj); -} - STATIC mp_obj_t vfs_posix_file_fileno(mp_obj_t self_in) { mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in); check_fd_is_open(self); @@ -268,7 +257,6 @@ const mp_obj_type_t mp_type_vfs_posix_fileio = { { &mp_type_type }, .name = MP_QSTR_FileIO, .print = vfs_posix_file_print, - .make_new = vfs_posix_file_make_new, .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &vfs_posix_fileio_stream_p, @@ -287,15 +275,14 @@ const mp_obj_type_t mp_type_vfs_posix_textio = { { &mp_type_type }, .name = MP_QSTR_TextIOWrapper, .print = vfs_posix_file_print, - .make_new = vfs_posix_file_make_new, .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &vfs_posix_textio_stream_p, .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, }; -const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_textio}, STDIN_FILENO}; -const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_textio}, STDOUT_FILENO}; -const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_textio}, STDERR_FILENO}; +const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_vfs_posix_textio}, STDIN_FILENO}; +const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_vfs_posix_textio}, STDOUT_FILENO}; +const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_vfs_posix_textio}, STDERR_FILENO}; #endif // MICROPY_VFS_POSIX || MICROPY_VFS_POSIX_FILE diff --git a/py/modio.c b/py/modio.c index 50af0b6a4795e..d44c1948ab27a 100644 --- a/py/modio.c +++ b/py/modio.c @@ -37,9 +37,6 @@ #if MICROPY_PY_IO -extern const mp_obj_type_t mp_type_fileio; -extern const mp_obj_type_t mp_type_textio; - #if MICROPY_PY_IO_IOBASE STATIC const mp_obj_type_t mp_type_iobase; @@ -211,12 +208,6 @@ STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = { #if MICROPY_PY_IO_IOBASE { MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) }, #endif - #if MICROPY_PY_IO_FILEIO - { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) }, - #if MICROPY_CPYTHON_COMPAT - { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) }, - #endif - #endif { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) }, #if MICROPY_PY_IO_BYTESIO { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) }, diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py index 2a14fc20767be..d193236696ea1 100644 --- a/tests/extmod/vfs_posix.py +++ b/tests/extmod/vfs_posix.py @@ -42,8 +42,8 @@ # close on a closed file should succeed f.close() -# construct a file object using the type constructor, with a raw fileno -f = type(f)(2) +# construct a file object with a raw fileno +f = open(2) print(f) # file read From 092784da1948c6bfb224f8f6c593aff255ab2db0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 22 Jul 2022 13:53:40 +1000 Subject: [PATCH 2412/3533] ports: Remove unused mp_type_{fileio/textio} macros in mpconfigport.h. Signed-off-by: Jim Mussared --- ports/cc3200/mpconfigport.h | 4 ---- ports/esp32/mpconfigport.h | 2 -- ports/esp8266/mpconfigport.h | 11 ----------- ports/mimxrt/mpconfigport.h | 4 ---- ports/nrf/mpconfigport.h | 17 ----------------- ports/renesas-ra/mpconfigport.h | 12 ------------ ports/rp2/mpconfigport.h | 9 --------- ports/samd/mpconfigport.h | 4 ---- ports/stm32/mpconfigport.h | 12 ------------ ports/unix/mpconfigport.h | 3 --- ports/unix/variants/minimal/mpconfigvariant.h | 3 --- ports/windows/mpconfigport.h | 3 --- 12 files changed, 84 deletions(-) diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index 8059daeec7c63..e81a2fc29bb3a 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -131,10 +131,6 @@ X(EINVAL) \ X(ETIMEDOUT) \ -// TODO these should be generic, not bound to fatfs -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio - // extra constants #define MICROPY_PORT_CONSTANTS \ { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&mp_module_machine) }, \ diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index dfa577d2bc693..957aa2e93bce0 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -135,8 +135,6 @@ #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MAX_SS (4096) #define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 779767311cdfd..9ff6a34ffd625 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -134,17 +134,6 @@ void *esp_native_code_commit(void *, size_t, void *); // printer for debugging output, goes to UART only extern const struct _mp_print_t mp_debug_print; -#if MICROPY_VFS_FAT -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio -#elif MICROPY_VFS_LFS1 -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio -#elif MICROPY_VFS_LFS2 -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio -#endif - #define MP_STATE_PORT MP_STATE_VM // We need an implementation of the log2 function which is not a macro diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 676e563c7be41..00714ca8d0565 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -184,10 +184,6 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_PENDSV_REENTER atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); #define MICROPY_PY_PENDSV_EXIT restore_irq_pri(atomic_state); -// Use VfsLfs2's types for fileio/textio -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio - // Hooks to add builtins __attribute__((always_inline)) static inline void enable_irq(uint32_t state) { diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 8a2478d27d0f8..a25ca1ec27efe 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -133,23 +133,6 @@ #define MICROPY_FATFS_MAX_SS (4096) #endif -#if MICROPY_VFS -// TODO these should be generic, not bound to a particular FS implementation -#if MICROPY_VFS_FAT -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio -#elif MICROPY_VFS_LFS1 -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio -#elif MICROPY_VFS_LFS2 -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio -#endif -#else // !MICROPY_VFS_FAT -#define mp_type_fileio fatfs_type_fileio -#define mp_type_textio fatfs_type_textio -#endif - // Use port specific uos module rather than extmod variant. #define MICROPY_PY_UOS (0) diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 8ecc17fcb4acd..65b194274a53d 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -137,18 +137,6 @@ #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MULTI_PARTITION (1) -// TODO these should be generic, not bound to a particular FS implementation -#if MICROPY_VFS_FAT -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio -#elif MICROPY_VFS_LFS1 -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio -#elif MICROPY_VFS_LFS2 -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio -#endif - #if MICROPY_PY_MACHINE #define MACHINE_BUILTIN_MODULE_CONSTANTS \ { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&mp_module_machine) }, \ diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index d07c1015e7f59..a3725789f0848 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -122,15 +122,6 @@ #define MICROPY_FATFS_MAX_SS (FLASH_SECTOR_SIZE) #endif -#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio -#elif MICROPY_VFS_LFS2 -// Use VfsLfs2's types for fileio/textio -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio -#endif - #ifndef MICROPY_BOARD_ENTER_BOOTLOADER #define MICROPY_BOARD_ENTER_BOOTLOADER(nargs, args) #endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 96d34bf4da048..24b9cbfe19438 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -95,10 +95,6 @@ #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UASYNCIO (1) -// Use VfsLfs's types for fileio/textio -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio - #define MP_STATE_PORT MP_STATE_VM // Miscellaneous settings diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index e44fa2260cacd..e06e32c0d2173 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -151,18 +151,6 @@ #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MULTI_PARTITION (1) -// TODO these should be generic, not bound to a particular FS implementation -#if MICROPY_VFS_FAT -#define mp_type_fileio mp_type_vfs_fat_fileio -#define mp_type_textio mp_type_vfs_fat_textio -#elif MICROPY_VFS_LFS1 -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio -#elif MICROPY_VFS_LFS2 -#define mp_type_fileio mp_type_vfs_lfs2_fileio -#define mp_type_textio mp_type_vfs_lfs2_textio -#endif - #if MICROPY_PY_PYB extern const struct _mp_obj_module_t pyb_module; #define PYB_BUILTIN_MODULE_CONSTANTS \ diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 4252a406ce8f6..ed4c71097ff16 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -224,9 +224,6 @@ extern const struct _mp_print_t mp_stderr_print; #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) #define MICROPY_ASYNC_KBD_INTR (1) -#define mp_type_fileio mp_type_vfs_posix_fileio -#define mp_type_textio mp_type_vfs_posix_textio - // type definitions for the specific machine // For size_t and ssize_t diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index 1e378e94d7d2b..d6e5fc47262e9 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -107,9 +107,6 @@ #define MICROPY_PY_UHASHLIB (0) #define MICROPY_PY_UBINASCII (0) -#define mp_type_fileio mp_type_vfs_posix_fileio -#define mp_type_textio mp_type_vfs_posix_textio - ////////////////////////////////////////// // Do not change anything beyond this line ////////////////////////////////////////// diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 71814e21476a7..5f8ad983adc4a 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -176,9 +176,6 @@ extern const struct _mp_print_t mp_stderr_print; #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) #define MICROPY_KBD_EXCEPTION (1) -#define mp_type_fileio mp_type_vfs_posix_fileio -#define mp_type_textio mp_type_vfs_posix_textio - #define MICROPY_PORT_INIT_FUNC init() #define MICROPY_PORT_DEINIT_FUNC deinit() From b22abcdbbedb0f7583b19031fd65e19b3883671d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 21 Jul 2022 09:54:02 +1000 Subject: [PATCH 2413/3533] extmod/uasyncio: Handle gather with no awaitables. This previously resulted in gather() yielding but with no way to be resumed. Signed-off-by: Jim Mussared --- extmod/uasyncio/funcs.py | 3 +++ tests/extmod/uasyncio_gather.py | 5 +++++ tests/extmod/uasyncio_gather.py.exp | 2 ++ 3 files changed, 10 insertions(+) diff --git a/extmod/uasyncio/funcs.py b/extmod/uasyncio/funcs.py index a1d38fbcbf975..96883e4fe1061 100644 --- a/extmod/uasyncio/funcs.py +++ b/extmod/uasyncio/funcs.py @@ -62,6 +62,9 @@ def remove(t): async def gather(*aws, return_exceptions=False): + if not aws: + return [] + def done(t, er): # Sub-task "t" has finished, with exception "er". nonlocal state diff --git a/tests/extmod/uasyncio_gather.py b/tests/extmod/uasyncio_gather.py index c081221c9fc68..af6ea555ab132 100644 --- a/tests/extmod/uasyncio_gather.py +++ b/tests/extmod/uasyncio_gather.py @@ -53,6 +53,11 @@ async def main(): print("====") + # Gather with no awaitables + print(await asyncio.gather()) + + print("====") + # Test return_exceptions, where one task is cancelled and the other finishes normally tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))] tasks[0].cancel() diff --git a/tests/extmod/uasyncio_gather.py.exp b/tests/extmod/uasyncio_gather.py.exp index a5ea47ab500d8..371d1ae60dfd2 100644 --- a/tests/extmod/uasyncio_gather.py.exp +++ b/tests/extmod/uasyncio_gather.py.exp @@ -9,6 +9,8 @@ Task C: Compute factorial(4)... Task C: factorial(4) = 24 [2, 6, 24] ==== +[] +==== start 2 end 2 [CancelledError(), 2] From f9cbe6bc47dd4f5b8e85178caecd6f0de22b4c34 Mon Sep 17 00:00:00 2001 From: Dan Ellis Date: Tue, 12 Jul 2022 09:48:38 -0400 Subject: [PATCH 2414/3533] py/formatfloat: Format all whole-number floats exactly. Formerly, py/formatfloat would print whole numbers inaccurately with nonzero digits beyond the decimal place. This resulted from its strategy of successive scaling of the argument by 0.1 which cannot be exactly represented in floating point. The change in this commit avoids scaling until the value is smaller than 1, so all whole numbers print with zero fractional part. Fixes issue #4212. Signed-off-by: Dan Ellis dan.ellis@gmail.com --- py/formatfloat.c | 154 +++++++++++++------- tests/float/float_format_ftoe.py | 4 + tests/float/float_format_ftoe.py.exp | 1 + tests/float/float_format_ints.py | 31 ++++ tests/float/float_format_ints_doubleprec.py | 15 ++ tests/run-tests.py | 1 + tools/tinytest-codegen.py | 1 + 7 files changed, 153 insertions(+), 54 deletions(-) create mode 100644 tests/float/float_format_ftoe.py create mode 100644 tests/float/float_format_ftoe.py.exp create mode 100644 tests/float/float_format_ints.py create mode 100644 tests/float/float_format_ints_doubleprec.py diff --git a/py/formatfloat.c b/py/formatfloat.c index 9d28b2317dc64..357b73ace3ab0 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -25,6 +25,7 @@ */ #include "py/mpconfig.h" +#include "py/misc.h" #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_NONE #include @@ -96,7 +97,16 @@ static inline int fp_isless1(float x) { #define fp_iszero(x) (x == 0) #define fp_isless1(x) (x < 1.0) -#endif +#endif // MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT/DOUBLE + +static inline int fp_ge_eps(FPTYPE x, FPTYPE y) { + mp_float_union_t fb_y = {y}; + // Back off 2 eps. + // This is valid for almost all values, but in practice + // it's only used when y = 1eX for X>=0. + fb_y.i -= 2; + return x >= fb_y.f; +} static const FPTYPE g_pos_pow[] = { #if FPDECEXP > 32 @@ -173,6 +183,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch int num_digits = 0; const FPTYPE *pos_pow = g_pos_pow; const FPTYPE *neg_pow = g_neg_pow; + int signed_e = 0; if (fp_iszero(f)) { e = 0; @@ -192,31 +203,24 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } } } else if (fp_isless1(f)) { - // We need to figure out what an integer digit will be used - // in case 'f' is used (or we revert other format to it below). - // As we just tested number to be <1, this is obviously 0, - // but we can round it up to 1 below. - char first_dig = '0'; - if (f >= FPROUND_TO_ONE) { - first_dig = '1'; - } - + FPTYPE f_mod = f; // Build negative exponent for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) { - if (*neg_pow > f) { + if (*neg_pow > f_mod) { e += e1; - f *= *pos_pow; + f_mod *= *pos_pow; } } + char e_sign_char = '-'; - if (fp_isless1(f) && f >= FPROUND_TO_ONE) { - f = FPCONST(1.0); + if (fp_isless1(f_mod) && f_mod >= FPROUND_TO_ONE) { + f_mod = FPCONST(1.0); if (e == 0) { e_sign_char = '+'; } - } else if (fp_isless1(f)) { + } else if (fp_isless1(f_mod)) { e++; - f *= FPCONST(10.0); + f_mod *= FPCONST(10.0); } // If the user specified 'g' format, and e is <= 4, then we'll switch @@ -224,8 +228,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch if (fmt == 'f' || (fmt == 'g' && e <= 4)) { fmt = 'f'; - dec = -1; - *s++ = first_dig; + dec = 0; if (org_fmt == 'g') { prec += (e - 1); @@ -237,13 +240,8 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } num_digits = prec; - if (num_digits) { - *s++ = '.'; - while (--e && num_digits) { - *s++ = '0'; - num_digits--; - } - } + signed_e = 0; + ++num_digits; } else { // For e & g formats, we'll be printing the exponent, so set the // sign. @@ -256,22 +254,29 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch prec++; } } + signed_e = -e; } } else { - // Build positive exponent - for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) { - if (*pos_pow <= f) { + // Build positive exponent. + // We don't modify f at this point to avoid innaccuracies from + // scaling it. Instead, we find the product of powers of 10 + // that is not greater than it, and use that to start the + // mantissa. + FPTYPE u_base = FPCONST(1.0); + for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++) { + FPTYPE next_u = u_base * *pos_pow; + // fp_ge_eps performs "f >= (next_u - 2eps)" so that if, for + // numerical reasons, f is very close to a power of ten but + // not strictly equal, we still treat it as that power of 10. + // The comparison was failing for maybe 10% of 1eX values, but + // although rounding fixed many of them, there were still some + // rendering as 9.99999998e(X-1). + if (fp_ge_eps(f, next_u)) { + u_base = next_u; e += e1; - f *= *neg_pow; } } - // It can be that f was right on the edge of an entry in pos_pow needs to be reduced - if ((int)f >= 10) { - e += 1; - f *= FPCONST(0.1); - } - // If the user specified fixed format (fmt == 'f') and e makes the // number too big to fit into the available buffer, then we'll // switch to the 'e' format. @@ -310,15 +315,15 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } else { e_sign = '+'; } + signed_e = e; } if (prec < 0) { // This can happen when the prec is trimmed to prevent buffer overflow prec = 0; } - // We now have num.f as a floating point number between >= 1 and < 10 - // (or equal to zero), and e contains the absolute value of the power of - // 10 exponent. and (dec + 1) == the number of dgits before the decimal. + // At this point e contains the absolute value of the power of 10 exponent. + // (dec + 1) == the number of dgits before the decimal. // For e, prec is # digits after the decimal // For f, prec is # digits after the decimal @@ -336,25 +341,63 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch num_digits = prec; } - // Print the digits of the mantissa - for (int i = 0; i < num_digits; ++i, --dec) { - int32_t d = (int32_t)f; - if (d < 0) { - *s++ = '0'; - } else { + if (signed_e < 0) { + // The algorithm below treats numbers smaller than 1 by scaling them + // repeatedly by 10 to bring the new digit to the top. Our input number + // was smaller than 1, so scale it up to be 1 <= f < 10. + FPTYPE u_base = FPCONST(1.0); + const FPTYPE *pow_u = g_pos_pow; + for (int m = FPDECEXP; m; m >>= 1, pow_u++) { + if (m & e) { + u_base *= *pow_u; + } + } + f *= u_base; + } + + int d = 0; + int num_digits_left = num_digits; + for (int digit_index = signed_e; num_digits_left >= 0; --digit_index) { + FPTYPE u_base = FPCONST(1.0); + if (digit_index > 0) { + // Generate 10^digit_index for positive digit_index. + const FPTYPE *pow_u = g_pos_pow; + int target_index = digit_index; + for (int m = FPDECEXP; m; m >>= 1, pow_u++) { + if (m & target_index) { + u_base *= *pow_u; + } + } + } + for (d = 0; d < 9; ++d) { + // This is essentially "if (f < u_base)", but with 2eps margin + // so that if f is just a tiny bit smaller, we treat it as + // equal (and accept the additional digit value). + if (!fp_ge_eps(f, u_base)) { + break; + } + f -= u_base; + } + // We calculate one more digit than we display, to use in rounding + // below. So only emit the digit if it's one that we display. + if (num_digits_left > 0) { + // Emit this number (the leading digit). *s++ = '0' + d; + if (dec == 0 && prec > 0) { + *s++ = '.'; + } } - if (dec == 0 && prec > 0) { - *s++ = '.'; + --dec; + --num_digits_left; + if (digit_index <= 0) { + // Once we get below 1.0, we scale up f instead of calculting + // negative powers of 10 in u_base. This provides better + // renditions of exact decimals like 1/16 etc. + f *= FPCONST(10.0); } - f -= (FPTYPE)d; - f *= FPCONST(10.0); } - - // Round - // If we print non-exponential format (i.e. 'f'), but a digit we're going - // to round by (e) is too far away, then there's nothing to round. - if ((org_fmt != 'f' || e <= num_digits) && f >= FPCONST(5.0)) { + // Rounding. If the next digit to print is >= 5, round up. + if (d >= 5) { char *rs = s; rs--; while (1) { @@ -394,7 +437,10 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } } else { // Need at extra digit at the end to make room for the leading '1' - s++; + // but if we're at the buffer size limit, just drop the final digit. + if ((size_t)(s + 1 - buf) < buf_size) { + s++; + } } char *ss = s; while (ss > rs) { diff --git a/tests/float/float_format_ftoe.py b/tests/float/float_format_ftoe.py new file mode 100644 index 0000000000000..bc4e5a4a53eb1 --- /dev/null +++ b/tests/float/float_format_ftoe.py @@ -0,0 +1,4 @@ +# check a case where rounding was suppressed inappropriately when "f" was +# promoted to "e" for large numbers. +v = 8.888e32 +print("%.2f" % v) # '%.2f' format with e32 becomes '%.2e', expect 8.89e+32. diff --git a/tests/float/float_format_ftoe.py.exp b/tests/float/float_format_ftoe.py.exp new file mode 100644 index 0000000000000..f8b1deb3eca8e --- /dev/null +++ b/tests/float/float_format_ftoe.py.exp @@ -0,0 +1 @@ +8.89e+32 diff --git a/tests/float/float_format_ints.py b/tests/float/float_format_ints.py new file mode 100644 index 0000000000000..0bf4baf12d0a8 --- /dev/null +++ b/tests/float/float_format_ints.py @@ -0,0 +1,31 @@ +# Test that integers format to exact values. + +for b in [13, 123, 457, 23456]: + for r in range(1, 10): + e_fmt = "{:." + str(r) + "e}" + f_fmt = "{:." + str(r) + "f}" + g_fmt = "{:." + str(r) + "g}" + for e in range(0, 5): + f = b * (10**e) + title = str(b) + " x 10^" + str(e) + print(title, "with format", e_fmt, "gives", e_fmt.format(f)) + print(title, "with format", f_fmt, "gives", f_fmt.format(f)) + print(title, "with format", g_fmt, "gives", g_fmt.format(f)) + +# Check that powers of 10 (that fit in float32) format correctly. +for i in range(31): + # It works to 12 digits on all platforms *except* qemu-arm, where + # 10^11 comes out as 10000000820 or something. + print("{:.7g}".format(float("1e" + str(i)))) + +# 16777215 is 2^24 - 1, the largest integer that can be completely held +# in a float32. +print("{:f}".format(16777215)) +# 4294967040 = 16777215 * 128 is the largest integer that is exactly +# represented by a float32 and that will also fit within a (signed) int32. +# The upper bound of our integer-handling code is actually double this, +# but that constant might cause trouble on systems using 32 bit ints. +print("{:f}".format(2147483520)) +# Very large positive integers can be a test for precision and resolution. +# This is a weird way to represent 1e38 (largest power of 10 for float32). +print("{:.6e}".format(float("9" * 30 + "e8"))) diff --git a/tests/float/float_format_ints_doubleprec.py b/tests/float/float_format_ints_doubleprec.py new file mode 100644 index 0000000000000..57899d6d65a85 --- /dev/null +++ b/tests/float/float_format_ints_doubleprec.py @@ -0,0 +1,15 @@ +# Test formatting of very large ints. +# Relies on double-precision floats. + +import array +import sys + +# Challenging way to express 1e200 and 1e100. +print("{:.12e}".format(float("9" * 400 + "e-200"))) +print("{:.12e}".format(float("9" * 400 + "e-300"))) + +# These correspond to the binary representation of 1e200 in float64s: +v1 = 0x54B249AD2594C37D # 1e100 +v2 = 0x6974E718D7D7625A # 1e200 +print("{:.12e}".format(array.array("d", v1.to_bytes(8, sys.byteorder))[0])) +print("{:.12e}".format(array.array("d", v2.to_bytes(8, sys.byteorder))[0])) diff --git a/tests/run-tests.py b/tests/run-tests.py index baab7bdd4a90d..2745ee1393bad 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -516,6 +516,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1): if upy_float_precision < 64: skip_tests.add("float/float_divmod.py") # tested by float/float_divmod_relaxed.py instead skip_tests.add("float/float2int_doubleprec_intbig.py") + skip_tests.add("float/float_format_ints_doubleprec.py") skip_tests.add("float/float_parse_doubleprec.py") if not has_complex: diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index f1169a34d4bdd..feda75f283d3c 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -79,6 +79,7 @@ def script_to_map(test_file): "float/float_divmod.py", # requires double precision floating point to work "float/float2int_doubleprec_intbig.py", + "float/float_format_ints_doubleprec.py", "float/float_parse_doubleprec.py", # inline asm FP tests (require Cortex-M4) "inlineasm/asmfpaddsub.py", From 1230d86dca414baec926b2268156d241346efd66 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 27 Jul 2022 11:37:53 +1000 Subject: [PATCH 2415/3533] py/builtinimport: Remove duplicate static function argument. context==mc in all cases where this function was being called. Signed-off-by: Angus Gratton --- py/builtinimport.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/builtinimport.c b/py/builtinimport.c index cd9636ccdc2d4..36cdac07675e3 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -159,7 +159,7 @@ STATIC void do_load_from_lexer(mp_module_context_t *context, mp_lexer_t *lex) { #endif #if (MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD) || MICROPY_MODULE_FROZEN_MPY -STATIC void do_execute_raw_code(mp_module_context_t *context, const mp_raw_code_t *rc, const mp_module_context_t *mc, const char *source_name) { +STATIC void do_execute_raw_code(const mp_module_context_t *context, const mp_raw_code_t *rc, const char *source_name) { (void)source_name; #if MICROPY_PY___FILE__ @@ -179,7 +179,7 @@ STATIC void do_execute_raw_code(mp_module_context_t *context, const mp_raw_code_ nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { - mp_obj_t module_fun = mp_make_function_from_raw_code(rc, mc, NULL); + mp_obj_t module_fun = mp_make_function_from_raw_code(rc, context, NULL); mp_call_function_0(module_fun); // finish nlr block, restore context @@ -224,7 +224,7 @@ STATIC void do_load(mp_module_context_t *module_obj, vstr_t *file) { if (frozen_type == MP_FROZEN_MPY) { const mp_frozen_module_t *frozen = modref; module_obj->constants = frozen->constants; - do_execute_raw_code(module_obj, frozen->rc, module_obj, file_str + frozen_path_prefix_len); + do_execute_raw_code(module_obj, frozen->rc, file_str + frozen_path_prefix_len); return; } #endif @@ -237,7 +237,7 @@ STATIC void do_load(mp_module_context_t *module_obj, vstr_t *file) { #if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD if (file_str[file->len - 3] == 'm') { mp_compiled_module_t cm = mp_raw_code_load_file(file_str, module_obj); - do_execute_raw_code(module_obj, cm.rc, cm.context, file_str); + do_execute_raw_code(cm.context, cm.rc, file_str); return; } #endif From 45ab801c300d605db96229f6e0626ebe2801f24d Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Tue, 26 Jul 2022 15:56:07 +0100 Subject: [PATCH 2416/3533] rp2/cyw43_configport: Add event hook into cyw43_delay_ms. Still see some USB issues apparently caused by delays loading wifi firmware. cyw43_delay_ms is used to wait in the driver, so we should call the event hook in there. Fixes #8963. --- ports/rp2/cyw43_configport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/cyw43_configport.h b/ports/rp2/cyw43_configport.h index b6f5b3cad8a75..a5ce8a9e74de6 100644 --- a/ports/rp2/cyw43_configport.h +++ b/ports/rp2/cyw43_configport.h @@ -93,6 +93,7 @@ static inline void cyw43_delay_ms(uint32_t ms) { int32_t start = mp_hal_ticks_us(); while (mp_hal_ticks_us() - start < us) { __WFI(); + MICROPY_EVENT_POLL_HOOK_FAST; } } From 3c32ca6e77143704ac08d5694b7260781f127c3d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 15 Apr 2022 12:04:01 -0500 Subject: [PATCH 2417/3533] unix/unix_mphal: Allow overriding hal time functions. This adds #ifdefs around each of the mp_hal_* time functions for the unix port. This allows variants to override individual functions as needed. Signed-off-by: David Lechner --- ports/unix/unix_mphal.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index c224f78700c0a..6f2def890330e 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -199,6 +199,7 @@ void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } +#ifndef mp_hal_ticks_ms mp_uint_t mp_hal_ticks_ms(void) { #if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) struct timespec tv; @@ -210,7 +211,9 @@ mp_uint_t mp_hal_ticks_ms(void) { return tv.tv_sec * 1000 + tv.tv_usec / 1000; #endif } +#endif +#ifndef mp_hal_ticks_us mp_uint_t mp_hal_ticks_us(void) { #if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) struct timespec tv; @@ -222,13 +225,17 @@ mp_uint_t mp_hal_ticks_us(void) { return tv.tv_sec * 1000000 + tv.tv_usec; #endif } +#endif +#ifndef mp_hal_time_ns uint64_t mp_hal_time_ns(void) { struct timeval tv; gettimeofday(&tv, NULL); return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } +#endif +#ifndef mp_hal_delay_ms void mp_hal_delay_ms(mp_uint_t ms) { #ifdef MICROPY_EVENT_POLL_HOOK mp_uint_t start = mp_hal_ticks_ms(); @@ -242,6 +249,7 @@ void mp_hal_delay_ms(mp_uint_t ms) { usleep(ms * 1000); #endif } +#endif void mp_hal_get_random(size_t n, void *buf) { #ifdef _HAVE_GETRANDOM From fdfe4eca745dce5f20fb65a3c197006b9053999a Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 28 Jul 2022 17:56:27 +1000 Subject: [PATCH 2418/3533] ports: Always include debug information in the ELF. For bare metal ARM & xtensa targets, passing -g will make the ELF file larger but doesn't change the binary size. However, this means tools like gdb, addr2line, etc can extract source-level information from the ELF. Also standardise -ggdb to -g, these produce the exact same ELF file on arm-none-eabi-gcc and will use DWARF format for all these ports. --- ports/esp8266/Makefile | 2 +- ports/mimxrt/Makefile | 3 ++- ports/minimal/Makefile | 3 ++- ports/nrf/Makefile | 5 +++-- ports/renesas-ra/Makefile | 3 ++- ports/samd/Makefile | 3 ++- ports/stm32/Makefile | 3 ++- ports/teensy/Makefile | 3 ++- 8 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index f70fbd2284063..52ba18d7d8b0d 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -68,8 +68,8 @@ LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc # Debugging/Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) -CFLAGS += -g COPT = -O0 else CFLAGS += -fdata-sections -ffunction-sections diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index de560bdb66a6b..13b766eb3cc5e 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -297,8 +297,9 @@ SRC_QSTR += \ # Compiler Flags # ============================================================================= +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG),1) -CFLAGS += -Og -ggdb +CFLAGS += -Og else CFLAGS += -Os -DNDEBUG endif diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 4a17df6803e18..169132a1a29f7 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -34,8 +34,9 @@ endif CSUPEROPT = -Os # save some code space # Tune for Debugging or Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) -CFLAGS += -O0 -ggdb +CFLAGS += -O0 else CFLAGS += -Os -DNDEBUG CFLAGS += -fdata-sections -ffunction-sections diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 47653ae716831..0586524ba6141 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -124,7 +124,7 @@ endif CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) -CFLAGS += $(INC) -Wall -Werror -g -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Werror -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -fno-strict-aliasing CFLAGS += -Iboards/$(BOARD) CFLAGS += -DNRF5_HAL_H='<$(MCU_VARIANT)_hal.h>' @@ -138,9 +138,10 @@ LDFLAGS += -Wl,'--defsym=_fs_size=$(FS_SIZE)' endif #Debugging/Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) #ASMFLAGS += -g -gtabs+ -CFLAGS += -O0 -ggdb +CFLAGS += -O0 LDFLAGS += -O0 else CFLAGS += -Os -DNDEBUG diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index d326c81e7858b..92d8be0692af3 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -148,8 +148,9 @@ CFLAGS += -fdata-sections -ffunction-sections LDFLAGS += --gc-sections # Debugging/Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) -CFLAGS += -g -DPENDSV_DEBUG +CFLAGS += -DPENDSV_DEBUG #COPT = -Og COPT = -Os # Disable text compression in debug builds diff --git a/ports/samd/Makefile b/ports/samd/Makefile index b3d6d8b228fda..7091365f4968c 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -57,8 +57,9 @@ LDFLAGS += $(LDFLAGS_MOD) LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Tune for Debugging or Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG),1) -CFLAGS += -O0 -ggdb +CFLAGS += -O0 else CFLAGS += -Os -DNDEBUG LDFLAGS += --gc-sections diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 652dff19fe1da..657524798cf5f 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -114,8 +114,9 @@ $(BUILD)/stm32_it.o $(BUILD)/pendsv.o: CFLAGS += -fno-lto endif # Debugging/Optimization +CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) -CFLAGS += -g -DPENDSV_DEBUG +CFLAGS += -DPENDSV_DEBUG COPT ?= -Og # Disable text compression in debug builds MICROPY_ROM_TEXT_COMPRESSION = 0 diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index 745f530d7a31d..d7161fcbbc3b1 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -105,8 +105,9 @@ LIBS += -L $(dir $(LIBC_FILE_NAME)) -lc LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc #Debugging/Optimization +CFLAGS += -g # always include debug info in the ELF ifdef DEBUG -CFLAGS += -Og -ggdb +CFLAGS += -Og else CFLAGS += -Os #-DNDEBUG endif From fe5598452dd82ab24cf19221777b901a6decccdd Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Wed, 27 Jul 2022 18:19:42 +0100 Subject: [PATCH 2419/3533] docs/library/time: Provide more info about which epoch is used. Some embedded targets use 1970 epoch. --- docs/library/time.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/library/time.rst b/docs/library/time.rst index 6ca172f22128e..3ab5caf248c69 100644 --- a/docs/library/time.rst +++ b/docs/library/time.rst @@ -10,8 +10,8 @@ The ``time`` module provides functions for getting the current time and date, measuring time intervals, and for delays. **Time Epoch**: Unix port uses standard for POSIX systems epoch of -1970-01-01 00:00:00 UTC. However, embedded ports use epoch of -2000-01-01 00:00:00 UTC. +1970-01-01 00:00:00 UTC. However, some embedded ports use epoch of +2000-01-01 00:00:00 UTC. Epoch year may be determined with ``gmtime(0)[0]``. **Maintaining actual calendar date/time**: This requires a Real Time Clock (RTC). On systems with underlying OS (including some From e168d47424eefc15ab74c3c15e363d02deb46fba Mon Sep 17 00:00:00 2001 From: chrismas9 Date: Sun, 26 Jun 2022 22:59:09 +1000 Subject: [PATCH 2420/3533] docs/library/pyb.Pin: Fix out-of-context paragraphs, and AF_PP typo. Remove out of context callback paragraph, it was part of the wipy docs. And move the paragraph about PULL_UP/PULL_DOWN resistor values to within the init() method docs. Also fix pull-pull -> push-pull. Signed-off-by: Chris Mason --- docs/library/pyb.Pin.rst | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/docs/library/pyb.Pin.rst b/docs/library/pyb.Pin.rst index d13cb9741cd22..292758c11cd0b 100644 --- a/docs/library/pyb.Pin.rst +++ b/docs/library/pyb.Pin.rst @@ -58,16 +58,6 @@ an ordinal pin number: You can set ``pyb.Pin.debug(True)`` to get some debug information about how a particular object gets mapped to a pin. -When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled, -that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND -respectively (except pin Y5 which has 11k Ohm resistors). - -Now every time a falling edge is seen on the gpio pin, the callback will be -executed. Caution: mechanical push buttons have "bounce" and pushing or -releasing a switch will often generate multiple edges. -See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed -explanation, along with various techniques for debouncing. - All pin objects go through the pin mapper to come up with one of the gpio pins. @@ -107,7 +97,7 @@ Methods - ``Pin.IN`` - configure the pin for input; - ``Pin.OUT_PP`` - configure the pin for output, with push-pull control; - ``Pin.OUT_OD`` - configure the pin for output, with open-drain control; - - ``Pin.AF_PP`` - configure the pin for alternate function, pull-pull; + - ``Pin.AF_PP`` - configure the pin for alternate function, push-pull; - ``Pin.AF_OD`` - configure the pin for alternate function, open-drain; - ``Pin.ANALOG`` - configure the pin for analog. @@ -117,6 +107,10 @@ Methods - ``Pin.PULL_UP`` - enable the pull-up resistor; - ``Pin.PULL_DOWN`` - enable the pull-down resistor. + When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled, + that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND + respectively (except pin Y5 which has 11k Ohm resistors). + - *value* if not None will set the port output value before enabling the pin. - *alt* can be used when mode is ``Pin.AF_PP`` or ``Pin.AF_OD`` to set the From 33ea400ce849777617bf1e8f44b10928530b64b9 Mon Sep 17 00:00:00 2001 From: chrismas9 Date: Sun, 26 Jun 2022 23:30:37 +1000 Subject: [PATCH 2421/3533] docs/library/pyb.Pin: Add Pin.ALT constant. Some Pin alternate functions are inputs, for example, timer capture and break inputs. In Pyb.Pin the only way to set alt mode is with Pin.AF_PP or Pin.AF_OD. It is not intuitive to use an output mode to configure an input. Pin.ALT is used in the machine.Pin class and works in pyb.Pin. The examples are changed to use Pin.ALT because TIM2_CH3 can be a capture input or pulse output. Signed-off-by: Chris Mason --- docs/library/pyb.Pin.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/library/pyb.Pin.rst b/docs/library/pyb.Pin.rst index 292758c11cd0b..b93924508b943 100644 --- a/docs/library/pyb.Pin.rst +++ b/docs/library/pyb.Pin.rst @@ -97,6 +97,7 @@ Methods - ``Pin.IN`` - configure the pin for input; - ``Pin.OUT_PP`` - configure the pin for output, with push-pull control; - ``Pin.OUT_OD`` - configure the pin for output, with open-drain control; + - ``Pin.ALT`` - configure the pin for alternate function, input or output; - ``Pin.AF_PP`` - configure the pin for alternate function, push-pull; - ``Pin.AF_OD`` - configure the pin for alternate function, open-drain; - ``Pin.ANALOG`` - configure the pin for analog. @@ -113,8 +114,8 @@ Methods - *value* if not None will set the port output value before enabling the pin. - - *alt* can be used when mode is ``Pin.AF_PP`` or ``Pin.AF_OD`` to set the - index or name of one of the alternate functions associated with a pin. + - *alt* can be used when mode is ``Pin.ALT`` , ``Pin.AF_PP`` or ``Pin.AF_OD`` to + set the index or name of one of the alternate functions associated with a pin. This arg was previously called *af* which can still be used if needed. Returns: ``None``. @@ -177,6 +178,10 @@ Methods Constants --------- +.. data:: Pin.ALT + + initialise the pin to alternate-function mode for input or output + .. data:: Pin.AF_OD initialise the pin to alternate-function mode with an open-drain drive @@ -237,11 +242,11 @@ control is desired. To configure X3 to expose TIM2_CH3, you could use:: - pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, alt=pyb.Pin.AF1_TIM2) + pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.ALT, alt=pyb.Pin.AF1_TIM2) or:: - pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, alt=1) + pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.ALT, alt=1) Methods ------- From c038ea0cc68bff18ce9ac338a283d2dd4cf29f1e Mon Sep 17 00:00:00 2001 From: chrismas9 Date: Sun, 26 Jun 2022 23:54:37 +1000 Subject: [PATCH 2422/3533] docs/library/pyb.Timer: Document how to use BKIN pin with example. Document how to connect the Timer block BRK_IN to a physical Pin alternate function. Add an example of PWM Motor drive using complementary outputs with dead time and break input to kill the PWM and generate a callback. Signed-off-by: Chris Mason --- docs/library/pyb.Timer.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/library/pyb.Timer.rst b/docs/library/pyb.Timer.rst index 53213666ba87b..1749efce2d508 100644 --- a/docs/library/pyb.Timer.rst +++ b/docs/library/pyb.Timer.rst @@ -111,7 +111,9 @@ Methods the PWM when the ``BRK_IN`` input is asserted. The value of this argument determines if break is enabled and what the polarity is, and can be one of ``Timer.BRK_OFF``, ``Timer.BRK_LOW`` or - ``Timer.BRK_HIGH``. + ``Timer.BRK_HIGH``. To select the ``BRK_IN`` pin construct a Pin object with + ``mode=Pin.ALT, alt=Pin.AFn_TIMx``. The pin's GPIO input features are + available in alt mode - ``pull=`` , ``value()`` and ``irq()``. You must either specify freq or both of period and prescaler. @@ -204,6 +206,17 @@ Methods ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=8000) ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=16000) + PWM Motor Example with complementary outputs, dead time, break input and break callback:: + + from pyb import Timer + from machine import Pin # machine.Pin supports alt mode and irq on the same pin. + pin_t8_1 = Pin(Pin.board.Y1, mode=Pin.ALT, af=Pin.AF3_TIM8) # Pin PC6, TIM8_CH1 + pin_t8_1n = Pin(Pin.board.X8, mode=Pin.ALT, af=Pin.AF3_TIM8) # Pin PA7, TIM8_CH1N + pin_bkin = Pin(Pin.board.X7, mode=Pin.ALT, af=Pin.AF3_TIM8) # Pin PA6, TIM8_BKIN + pin_bkin.irq(handler=break_callabck, trigger=Pin.IRQ_FALLING) + timer = pyb.Timer(8, freq=1000, deadtime=1008, brk=Timer.BRK_LOW) + ch1 = timer.channel(1, pyb.Timer.PWM, pulse_width_percent=30) + .. method:: Timer.counter([value]) Get or set the timer counter. From 963e599ec0d253534eb835ade7020e8ac3d7919b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 29 Jul 2022 12:06:08 +1000 Subject: [PATCH 2423/3533] tests/cpydiff: Fix formatting of code snippet to use double quotes. Signed-off-by: Damien George --- tests/cpydiff/types_float_implicit_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpydiff/types_float_implicit_conversion.py b/tests/cpydiff/types_float_implicit_conversion.py index 8d39a7cd41098..3726839fac6d1 100644 --- a/tests/cpydiff/types_float_implicit_conversion.py +++ b/tests/cpydiff/types_float_implicit_conversion.py @@ -2,7 +2,7 @@ categories: Types,float description: uPy allows implicit conversion of objects in maths operations while CPython does not. cause: Unknown -workaround: Objects should be wrapped in `float(obj)` for compatibility with CPython. +workaround: Objects should be wrapped in ``float(obj)`` for compatibility with CPython. """ From 9a1b7d8448346b353e3aa7ae11ff2b441b6d5313 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 14:32:12 +1000 Subject: [PATCH 2424/3533] lib/micropython-lib: Add micropython-lib as a submodule. Several boards now depend on libraries from micropython-lib. Rather than expecting micropython-lib to be available as a sibling of the micropython repo, instead make it a submodule. Signed-off-by: Jim Mussared --- .gitmodules | 3 +++ lib/micropython-lib | 1 + 2 files changed, 4 insertions(+) create mode 160000 lib/micropython-lib diff --git a/.gitmodules b/.gitmodules index 3d1e2fea745e7..3c47b5959aad8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -53,3 +53,6 @@ [submodule "lib/cyw43-driver"] path = lib/cyw43-driver url = https://github.com/georgerobotics/cyw43-driver.git +[submodule "lib/micropython-lib"] + path = lib/micropython-lib + url = https://github.com/micropython/micropython-lib.git diff --git a/lib/micropython-lib b/lib/micropython-lib new file mode 160000 index 0000000000000..70e422dc2e885 --- /dev/null +++ b/lib/micropython-lib @@ -0,0 +1 @@ +Subproject commit 70e422dc2e885bbaafe6eb7e3d81118e17d4b555 From be83c08f463c75bb52619ea4f35b5256fc479158 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 14:59:33 +1000 Subject: [PATCH 2425/3533] ports: Always append to GIT_SUBMODULES. Avoids overwriting submodules required by base makefiles. Signed-off-by: Jim Mussared --- ports/esp32/Makefile | 2 +- ports/esp8266/Makefile | 2 +- ports/mimxrt/Makefile | 2 +- ports/nrf/Makefile | 2 +- ports/samd/Makefile | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index c8ca9262c8f16..88f946fa983bc 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -14,7 +14,7 @@ BAUD ?= 460800 PYTHON ?= python3 -GIT_SUBMODULES = lib/berkeley-db-1.xx +GIT_SUBMODULES += lib/berkeley-db-1.xx .PHONY: all clean deploy erase submodules FORCE diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 52ba18d7d8b0d..c027f690fb107 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -32,7 +32,7 @@ FROZEN_MANIFEST ?= boards/manifest.py include $(TOP)/py/py.mk include $(TOP)/extmod/extmod.mk -GIT_SUBMODULES = lib/axtls lib/berkeley-db-1.xx +GIT_SUBMODULES += lib/axtls lib/berkeley-db-1.xx FWBIN = $(BUILD)/firmware-combined.bin PORT ?= /dev/ttyACM0 diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 13b766eb3cc5e..6312ecdd8f3ee 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -7,7 +7,7 @@ BOARD_DIR ?= boards/$(BOARD) BUILD ?= build-$(BOARD) PORT ?= /dev/ttyACM0 CROSS_COMPILE ?= arm-none-eabi- -GIT_SUBMODULES = lib/tinyusb lib/nxp_driver lib/lwip lib/mbedtls +GIT_SUBMODULES += lib/tinyusb lib/nxp_driver lib/lwip lib/mbedtls # MicroPython feature configurations FROZEN_MANIFEST ?= boards/manifest.py diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 0586524ba6141..23be4430f3960 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -58,7 +58,7 @@ FROZEN_MANIFEST ?= modules/manifest.py include ../../py/py.mk include ../../extmod/extmod.mk -GIT_SUBMODULES = lib/nrfx lib/tinyusb +GIT_SUBMODULES += lib/nrfx lib/tinyusb MICROPY_VFS_FAT ?= 0 diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 7091365f4968c..aed8637abcc1f 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -24,7 +24,7 @@ FROZEN_MANIFEST ?= boards/manifest.py include $(TOP)/py/py.mk include $(TOP)/extmod/extmod.mk -GIT_SUBMODULES = lib/asf4 lib/tinyusb +GIT_SUBMODULES += lib/asf4 lib/tinyusb INC += -I. INC += -I$(TOP) From 9a7ac41be61a4d80234ac91c97ee5f099af29626 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 15:26:06 +1000 Subject: [PATCH 2426/3533] rp2/Makefile: Always use cmake to discover submodules. Used to be special-cased for Pico, but now everything depends on micropython-lib if it's using a frozen manifest. Signed-off-by: Jim Mussared --- ports/rp2/CMakeLists.txt | 11 +++++++++-- ports/rp2/Makefile | 15 ++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index f9a9efe62a8c8..f5bd61c44ea84 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -54,6 +54,10 @@ if (MICROPY_PY_NETWORK_CYW43) set(PICO_CYW43_DRIVER_PATH ${MICROPY_DIR}/lib/cyw43-driver) endif() +# Necessary submodules for all boards. +string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/mbedtls) +string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb) + # Include component cmake fragments include(${MICROPY_DIR}/py/py.cmake) include(${MICROPY_DIR}/extmod/extmod.cmake) @@ -278,6 +282,11 @@ if (MICROPY_PY_NETWORK_NINAW10) endif() if (MICROPY_PY_NETWORK_WIZNET5K) + string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/wiznet5k) + if((NOT (${ECHO_SUBMODULES})) AND NOT EXISTS ${MICROPY_DIR}/lib/wiznet5k/README.md) + message(FATAL_ERROR " wiznet5k not initialized.\n Run 'make BOARD=${MICROPY_BOARD} submodules'") + endif() + target_compile_definitions(${MICROPY_TARGET} PRIVATE MICROPY_PY_NETWORK_WIZNET5K=1 WIZCHIP_PREFIXED_EXPORTS=1 @@ -312,8 +321,6 @@ if (MICROPY_PY_NETWORK_WIZNET5K) list(APPEND MICROPY_SOURCE_EXTMOD ${MICROPY_DIR}/extmod/network_wiznet5k.c ) - - string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/wiznet5k) endif() # Add qstr sources for extmod and usermod, in case they are modified by components above. diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 6f8c621c2a4a0..6d2fc009642d8 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -29,17 +29,10 @@ all: clean: $(RM) -rf $(BUILD) -GIT_SUBMODULES += lib/mbedtls lib/tinyusb - +# First ensure that pico-sdk is initialised, then use cmake to pick everything +# else (including board-specific dependencies). submodules: - # lib/pico-sdk is required for the cmake build to function (as used for boards other than PICO below) $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="lib/pico-sdk" submodules -ifeq ($(BOARD),PICO) - # Run the standard submodules target with minimum required submodules above - $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$(GIT_SUBMODULES)" submodules -else - # Run submodules task through cmake interface to pick up any board specific dependencies. - GIT_SUBMODULES=$$(cmake -B $(BUILD)/submodules -DECHO_SUBMODULES=1 -DGIT_SUBMODULES="$(GIT_SUBMODULES)" ${CMAKE_ARGS} -S . 2>&1 | \ - grep 'GIT_SUBMODULES=' | cut -d= -f2); \ + GIT_SUBMODULES=$$(cmake -B $(BUILD)/submodules -DECHO_SUBMODULES=1 ${CMAKE_ARGS} -S . 2>&1 | \ + grep '^GIT_SUBMODULES=' | cut -d= -f2); \ $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$${GIT_SUBMODULES}" submodules -endif From 19f5da9e1bf23821095da2252c2f3c2381f2fe21 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 15:26:52 +1000 Subject: [PATCH 2427/3533] esp32/Makefile: Force micropython-lib as a required submodule. Also use mkrules.mk's submodule target rather than duplicating the call to `submodule sync`. Until we can find a way to use idf.py/cmake to discover submodules we have no way to discover optional or board-specific submodules so need to err on the side of including everything. Signed-off-by: Jim Mussared --- ports/esp32/Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 88f946fa983bc..b7d804d0730e7 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -14,7 +14,11 @@ BAUD ?= 460800 PYTHON ?= python3 -GIT_SUBMODULES += lib/berkeley-db-1.xx +# Would be good to use cmake to discover submodules (see how rp2/Makefile does +# it), but on ESP32 the same trick doesn't work because "idf.py build" fails +# on berkeley-db dependency before printing out the submodule list. +# For now just force the submodule dependencies here. +GIT_SUBMODULES += lib/berkeley-db-1.xx lib/micropython-lib .PHONY: all clean deploy erase submodules FORCE @@ -52,4 +56,4 @@ erase: idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) erase_flash submodules: - git submodule update --init $(addprefix ../../,$(GIT_SUBMODULES)) + $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$(GIT_SUBMODULES)" submodules From 58bed5ec14a2575fd53ca1822be80860f7e3023a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 15:27:23 +1000 Subject: [PATCH 2428/3533] tools/ci.sh: Initialise submodules for more ports. Signed-off-by: Jim Mussared --- tools/ci.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/ci.sh b/tools/ci.sh index 6c3914b1449d2..7e2479e43d388 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -245,8 +245,10 @@ function ci_qemu_arm_setup { function ci_qemu_arm_build { make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/qemu-arm submodules make ${MAKEOPTS} -C ports/qemu-arm CFLAGS_EXTRA=-DMP_ENDIANNESS_BIG=1 make ${MAKEOPTS} -C ports/qemu-arm clean + make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test submodules make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test test make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test clean make ${MAKEOPTS} -C ports/qemu-arm -f Makefile.test BOARD=sabrelite test @@ -354,6 +356,7 @@ function ci_teensy_setup { } function ci_teensy_build { + make ${MAKEOPTS} -C ports/teensy submodules make ${MAKEOPTS} -C ports/teensy } @@ -577,6 +580,7 @@ function ci_unix_float_clang_run_tests { function ci_unix_settrace_build { make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix submodules make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SYS_SETTRACE[@]}" } @@ -586,6 +590,7 @@ function ci_unix_settrace_run_tests { function ci_unix_settrace_stackless_build { make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/unix submodules make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SYS_SETTRACE_STACKLESS[@]}" } @@ -661,6 +666,7 @@ function ci_windows_setup { function ci_windows_build { make ${MAKEOPTS} -C mpy-cross + make ${MAKEOPTS} -C ports/windows submodules make ${MAKEOPTS} -C ports/windows CROSS_COMPILE=i686-w64-mingw32- } From 579f330508e4ba46f4a71193582c36bdf7aa56bd Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 8 Jul 2022 15:00:33 +1000 Subject: [PATCH 2429/3533] py/mkenv.mk: Use micropython-lib from submodule by default. Also adds micropython-lib to 'make submodules' when using a frozen manifest (for make and cmake). Signed-off-by: Jim Mussared --- ports/windows/.appveyor.yml | 4 ++++ ports/windows/msvc/genhdr.targets | 2 +- py/mkenv.mk | 3 ++- py/mkrules.cmake | 7 ++++++- py/mkrules.mk | 7 +++++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ports/windows/.appveyor.yml b/ports/windows/.appveyor.yml index d7192236dfbd4..110511fe22c6b 100644 --- a/ports/windows/.appveyor.yml +++ b/ports/windows/.appveyor.yml @@ -74,6 +74,10 @@ after_test: throw "$env:MSYSTEM mpy_cross build exited with code $LASTEXITCODE" } cd (Join-Path $env:APPVEYOR_BUILD_FOLDER 'ports/windows') + C:\msys64\usr\bin\bash.exe -l -c "make -B VARIANT=$($env:PyVariant) submodules" + if ($LASTEXITCODE -ne 0) { + throw "$env:MSYSTEM build exited with code $LASTEXITCODE" + } C:\msys64\usr\bin\bash.exe -l -c "make -B -j4 V=1 MICROPY_MPYCROSS=../../mpy-cross/mpy-cross.exe VARIANT=$($env:PyVariant)" if ($LASTEXITCODE -ne 0) { throw "$env:MSYSTEM build exited with code $LASTEXITCODE" diff --git a/ports/windows/msvc/genhdr.targets b/ports/windows/msvc/genhdr.targets index aa796088efeca..308a6a1f5fe25 100644 --- a/ports/windows/msvc/genhdr.targets +++ b/ports/windows/msvc/genhdr.targets @@ -170,7 +170,7 @@ using(var outFile = System.IO.File.CreateText(OutputFile)) { MICROPY_MODULE_FROZEN_MPY=1;MICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool;%(PreprocessorDefinitions) - + diff --git a/py/mkenv.mk b/py/mkenv.mk index 2b247974b68e4..cc04a8c0b3071 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -57,7 +57,8 @@ MAKE_MANIFEST = $(PYTHON) $(TOP)/tools/makemanifest.py MAKE_FROZEN = $(PYTHON) $(TOP)/tools/make-frozen.py MPY_TOOL = $(PYTHON) $(TOP)/tools/mpy-tool.py -MPY_LIB_DIR = $(TOP)/../micropython-lib +MPY_LIB_SUBMODULE_DIR = $(TOP)/lib/micropython-lib +MPY_LIB_DIR = $(MPY_LIB_SUBMODULE_DIR) ifeq ($(MICROPY_MPYCROSS),) MICROPY_MPYCROSS = $(TOP)/mpy-cross/mpy-cross diff --git a/py/mkrules.cmake b/py/mkrules.cmake index d7be0f934b158..2f168ede6f2b4 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -180,7 +180,12 @@ if(MICROPY_FROZEN_MANIFEST) # Note: target_compile_definitions already added earlier. if(NOT MICROPY_LIB_DIR) - set(MICROPY_LIB_DIR ${MICROPY_DIR}/../micropython-lib) + string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/micropython-lib) + set(MICROPY_LIB_DIR ${MICROPY_DIR}/lib/micropython-lib) + endif() + + if(NOT (${ECHO_SUBMODULES}) AND NOT EXISTS ${MICROPY_LIB_DIR}/README.md) + message(FATAL_ERROR " micropython-lib not initialized.\n Run 'make BOARD=${MICROPY_BOARD} submodules'") endif() # If MICROPY_MPYCROSS is not explicitly defined in the environment (which diff --git a/py/mkrules.mk b/py/mkrules.mk index a7c437386f8eb..41ec4b0641592 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -174,8 +174,15 @@ $(error Support for FROZEN_MPY_DIR was removed. Please use manifest.py instead, endif ifneq ($(FROZEN_MANIFEST),) +# If we're using the default submodule path for micropython-lib, then make +# sure it's included in "make submodules". +ifeq ($(MPY_LIB_DIR),$(MPY_LIB_SUBMODULE_DIR)) +GIT_SUBMODULES += lib/micropython-lib +endif + # to build frozen_content.c from a manifest $(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h $(BUILD)/genhdr/root_pointers.h | $(MICROPY_MPYCROSS_DEPENDENCY) + $(Q)test -e "$(MPY_LIB_DIR)/README.md" || (echo "Error: micropython-lib not initialized. Run 'make submodules'"; false) $(Q)$(MAKE_MANIFEST) -o $@ -v "MPY_DIR=$(TOP)" -v "MPY_LIB_DIR=$(MPY_LIB_DIR)" -v "PORT_DIR=$(shell pwd)" -v "BOARD_DIR=$(BOARD_DIR)" -b "$(BUILD)" $(if $(MPY_CROSS_FLAGS),-f"$(MPY_CROSS_FLAGS)",) --mpy-tool-flags="$(MPY_TOOL_FLAGS)" $(FROZEN_MANIFEST) endif From 6152bbe3dd82fb7dc1b063e24c19a42af03bf321 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 28 Jul 2022 19:25:59 -0500 Subject: [PATCH 2430/3533] stm32/boards/LEGO_HUB_NO6: Fix typo in README. This fixes a typo in the build directory path. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO6/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/README.md b/ports/stm32/boards/LEGO_HUB_NO6/README.md index 376d0f2b0e445..cc67b6a4bb6b4 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/README.md +++ b/ports/stm32/boards/LEGO_HUB_NO6/README.md @@ -112,8 +112,8 @@ To use this feature, build the firmware (see above for details) then gzip it and copy the resulting file to the Hub (eg using mpremote): $ make BOARD=LEGO_HUB_NO6 - $ gzip boards/LEGO_HUB_NO6/firmware.dfu - $ mpremote cp boards/LEGO_HUB_NO6/firmware.dfu.gz : + $ gzip build-LEGO_HUB_NO6/firmware.dfu + $ mpremote cp build-LEGO_HUB_NO6/firmware.dfu.gz : Then get a REPL on the Hub and execute: From 7cc6df3303f467e218a6ac4f12924eac0ed15045 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 29 Jul 2022 13:58:33 -0500 Subject: [PATCH 2431/3533] stm32/boards/LEGO_HUB_NO6: Use named pins. This changes all uses of pins to use the alias names of the pins. This makes the code easier to understand and will also allow sharing more code with LEGO_HUB_NO7. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO6/appupdate.py | 9 +++- ports/stm32/boards/LEGO_HUB_NO6/board_init.c | 2 +- ports/stm32/boards/LEGO_HUB_NO6/hub_display.c | 30 ++++++------ .../stm32/boards/LEGO_HUB_NO6/mpconfigboard.h | 49 +++++++++---------- ports/stm32/boards/LEGO_HUB_NO6/pins.csv | 8 +-- 5 files changed, 51 insertions(+), 47 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py b/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py index 65954454a5821..0927f16111fec 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py +++ b/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py @@ -23,8 +23,13 @@ def update_app(filename): key = struct.pack("CCR2 = 2; tim->EGR = 1; // UG - mp_hal_pin_config(pin_B15, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 9); + mp_hal_pin_config(pyb_pin_TLC_GS_CLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 9); } static void hub_display_spi_init(void) { @@ -93,9 +93,9 @@ static void hub_display_spi_init(void) { spi->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | 0 << SPI_CR1_BR_Pos | SPI_CR1_MSTR; spi->CR1 |= SPI_CR1_SPE; - mp_hal_pin_config(pin_A5, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); - mp_hal_pin_config(pin_A6, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); - mp_hal_pin_config(pin_A7, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); + mp_hal_pin_config(pyb_pin_TLC_SCLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); + mp_hal_pin_config(pyb_pin_TLC_SOUT, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); + mp_hal_pin_config(pyb_pin_TLC_SIN, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5); } static void hub_display_spi_write(uint8_t value) { @@ -123,9 +123,9 @@ static void hub_display_latch_ctrl(uint8_t dc, uint32_t mc, uint32_t bc, uint8_t for (int i = 0; i < 42; ++i) { hub_display_spi_write(dc); } - mp_hal_pin_high(pin_A15); + mp_hal_pin_high(pyb_pin_TLC_LAT); mp_hal_delay_us(1); - mp_hal_pin_low(pin_A15); + mp_hal_pin_low(pyb_pin_TLC_LAT); } void hub_display_set(uint8_t led, uint16_t value) { @@ -142,17 +142,17 @@ void hub_display_update(void) { for (int i = 0; i < 96; ++i) { hub_display_spi_write(hub_display_gs_state[95 - i]); } - mp_hal_pin_high(pin_A15); + mp_hal_pin_high(pyb_pin_TLC_LAT); mp_hal_delay_us(1); - mp_hal_pin_low(pin_A15); + mp_hal_pin_low(pyb_pin_TLC_LAT); } void hub_display_on(void) { if (hub_display_init) { return; } - mp_hal_pin_output(pin_A15); - mp_hal_pin_low(pin_A15); + mp_hal_pin_output(pyb_pin_TLC_LAT); + mp_hal_pin_low(pyb_pin_TLC_LAT); hub_display_spi_init(); for (int i = 0; i < 2; ++i) { hub_display_latch_ctrl(0xff, 0, 0x1fffff, 0x11); @@ -171,10 +171,10 @@ void hub_display_off(void) { __HAL_RCC_SPI1_CLK_DISABLE(); __HAL_RCC_SPI1_FORCE_RESET(); __HAL_RCC_SPI1_RELEASE_RESET(); - mp_hal_pin_config(pin_A5, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); - mp_hal_pin_config(pin_A6, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); - mp_hal_pin_config(pin_A7, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); - mp_hal_pin_config(pin_A15, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); - mp_hal_pin_config(pin_B15, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_TLC_SCLK, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_TLC_SOUT, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_TLC_SIN, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_TLC_LAT, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_TLC_GS_CLK, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); hub_display_init = false; } diff --git a/ports/stm32/boards/LEGO_HUB_NO6/mpconfigboard.h b/ports/stm32/boards/LEGO_HUB_NO6/mpconfigboard.h index cab46fa02d57c..7f0b1fbe45c9c 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/mpconfigboard.h +++ b/ports/stm32/boards/LEGO_HUB_NO6/mpconfigboard.h @@ -34,41 +34,40 @@ // UART buses // Bluetooth HCI -#define MICROPY_HW_UART2_CTS (pin_D3) -#define MICROPY_HW_UART2_RTS (pin_D4) -#define MICROPY_HW_UART2_TX (pin_D5) -#define MICROPY_HW_UART2_RX (pin_D6) +#define MICROPY_HW_UART2_CTS (pyb_pin_BT_CTS) +#define MICROPY_HW_UART2_RTS (pyb_pin_BT_RTS) +#define MICROPY_HW_UART2_TX (pyb_pin_BT_TX) +#define MICROPY_HW_UART2_RX (pyb_pin_BT_RX) // Port B -#define MICROPY_HW_UART4_TX (pin_D1) -#define MICROPY_HW_UART4_RX (pin_D0) +#define MICROPY_HW_UART4_TX (pyb_pin_PORTB_TX) +#define MICROPY_HW_UART4_RX (pyb_pin_PORTB_RX) // Port D -#define MICROPY_HW_UART5_TX (pin_C12) -#define MICROPY_HW_UART5_RX (pin_D2) +#define MICROPY_HW_UART5_TX (pyb_pin_PORTD_TX) +#define MICROPY_HW_UART5_RX (pyb_pin_PORTD_RX) // Port A -#define MICROPY_HW_UART7_TX (pin_E8) -#define MICROPY_HW_UART7_RX (pin_E7) +#define MICROPY_HW_UART7_TX (pyb_pin_PORTA_TX) +#define MICROPY_HW_UART7_RX (pyb_pin_PORTA_RX) // Port C -#define MICROPY_HW_UART8_TX (pin_E1) -#define MICROPY_HW_UART8_RX (pin_E0) +#define MICROPY_HW_UART8_TX (pyb_pin_PORTC_TX) +#define MICROPY_HW_UART8_RX (pyb_pin_PORTC_RX) // Port F -#define MICROPY_HW_UART9_TX (pin_D15) -#define MICROPY_HW_UART9_RX (pin_D14) +#define MICROPY_HW_UART9_TX (pyb_pin_PORTF_TX) +#define MICROPY_HW_UART9_RX (pyb_pin_PORTF_RX) // Port E -#define MICROPY_HW_UART10_TX (pin_E3) -#define MICROPY_HW_UART10_RX (pin_E2) +#define MICROPY_HW_UART10_TX (pyb_pin_PORTE_TX) +#define MICROPY_HW_UART10_RX (pyb_pin_PORTE_RX) // SPI buses -#define MICROPY_HW_SPI1_NSS (pin_A4) // shared with DAC -#define MICROPY_HW_SPI1_SCK (pin_A5) // shared with DAC -#define MICROPY_HW_SPI1_MISO (pin_A6) -#define MICROPY_HW_SPI1_MOSI (pin_A7) -#define MICROPY_HW_SPI2_NSS (pin_B12) -#define MICROPY_HW_SPI2_SCK (pin_B13) -#define MICROPY_HW_SPI2_MISO (pin_C2) -#define MICROPY_HW_SPI2_MOSI (pin_C3) +#define MICROPY_HW_SPI1_SCK (pyb_pin_TLC_SCLK) +#define MICROPY_HW_SPI1_MISO (pyb_pin_TLC_SOUT) +#define MICROPY_HW_SPI1_MOSI (pyb_pin_TLC_SIN) +#define MICROPY_HW_SPI2_NSS (pyb_pin_FLASH_NSS) +#define MICROPY_HW_SPI2_SCK (pyb_pin_FLASH_SCK) +#define MICROPY_HW_SPI2_MISO (pyb_pin_FLASH_MISO) +#define MICROPY_HW_SPI2_MOSI (pyb_pin_FLASH_MOSI) // USB config -#define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9) +#define MICROPY_HW_USB_VBUS_DETECT_PIN (pyb_pin_USB_VBUS) #define MICROPY_HW_USB_FS (1) #define MICROPY_HW_USB_MSC (1) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/pins.csv b/ports/stm32/boards/LEGO_HUB_NO6/pins.csv index 9e56e2c79de4f..1d0b8f35d8d85 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/pins.csv +++ b/ports/stm32/boards/LEGO_HUB_NO6/pins.csv @@ -26,14 +26,14 @@ PORTD_M1,PB8 PORTD_M2,PB9 LSM6_SCL,PB10 ,PB11 -,PB12 -,PB13 +FLASH_NSS,PB12 +FLASH_SCK,PB13 ,PB14 TLC_GS_CLK,PB15 BAT_IMON_ADC,PC0 BAT_VMON_ADC,PC1 -,PC2 -,PC3 +FLASH_MISO,PC2 +FLASH_MOSI,PC3 CHGOK_CENBTN_3V3OK_ADC,PC4 PORTF_EN,PC5 PORTE_M1,PC6 From 9a51273d96405527e3950441cfebc6c7314a6bcf Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 29 Jul 2022 15:25:10 -0500 Subject: [PATCH 2432/3533] stm32/boards/LEGO_HUB_NO6/appupdate: Detect filesystem size at runtime. This changes appupdate.py to get the filesystem size at runtime. This will allow the code to be shared with LEGO_HUB_NO7 which has a similar flash chip with a different size. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO6/appupdate.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py b/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py index 0927f16111fec..57b24d3f2e605 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py +++ b/ports/stm32/boards/LEGO_HUB_NO6/appupdate.py @@ -2,13 +2,20 @@ # MIT license; Copyright (c) 2022 Damien P. George from micropython import const -import struct, machine, fwupdate, spiflash +import struct, machine, fwupdate, spiflash, pyb + +_IOCTL_BLOCK_COUNT = const(4) +_IOCTL_BLOCK_SIZE = const(5) _SPIFLASH_UPDATE_KEY_ADDR = const(1020 * 1024) _SPIFLASH_UPDATE_KEY_VALUE = const(0x12345678) _FILESYSTEM_ADDR = const(0x8000_0000 + 1024 * 1024) -_FILESYSTEM_LEN = const(31 * 1024 * 1024) + +# Roundabout way to get actual filesystem size from config. +# This takes into account the 1M "reserved" section of the flash memory. +flash = pyb.Flash(start=0) +_FILESYSTEM_LEN = flash.ioctl(_IOCTL_BLOCK_COUNT, None) * flash.ioctl(_IOCTL_BLOCK_SIZE, None) def update_app(filename): @@ -30,6 +37,8 @@ def update_app(filename): baudrate=50_000_000, ) cs = machine.Pin(machine.Pin.board.FLASH_NSS, machine.Pin.OUT, value=1) + + # We can't use pyb.Flash() because we need to write to the "reserved" 1M area. flash = spiflash.SPIFlash(spi, cs) # Write the update key and elements to the SPI flash. From f69af1661923c05149b2a998d5d074d85c51026d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 29 Jul 2022 15:45:13 -0500 Subject: [PATCH 2433/3533] stm32/boards/LEGO_HUB_NO6/spiflash: Pick command type at runtime. This changes spiflash.py to read the flash chip ID at runtime to select the read/write/erase commands. This will allow the code to be shared with LEGO_HUB_NO7 which doesn't use the 32-bit commands. Also remove an unused constant while we are touching this. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO6/spiflash.py | 24 +++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/spiflash.py b/ports/stm32/boards/LEGO_HUB_NO6/spiflash.py index e052f7738b70b..e483ace950842 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/spiflash.py +++ b/ports/stm32/boards/LEGO_HUB_NO6/spiflash.py @@ -4,18 +4,34 @@ from micropython import const _PAGE_SIZE = const(256) # maximum bytes writable in one SPI transfer +_CMD_WRITE = const(0x02) +_CMD_READ = const(0x03) _CMD_RDSR = const(0x05) _CMD_WREN = const(0x06) _CMD_WRITE_32 = const(0x12) _CMD_READ_32 = const(0x13) +_CMD_SEC_ERASE = const(0x20) _CMD_SEC_ERASE_32 = const(0x21) -_CMD_C4READ_32 = const(0xEC) +_CMD_JEDEC_ID = const(0x9F) class SPIFlash: def __init__(self, spi, cs): self.spi = spi self.cs = cs + self.id = self._get_id() + # flash chip on Hub No. 6 uses 32-bit addressing + _32_bit = self.id == b"\xef\x40\x19" + self._READ = _CMD_READ_32 if _32_bit else _CMD_READ + self._WRITE = _CMD_WRITE_32 if _32_bit else _CMD_WRITE + self._ERASE = _CMD_SEC_ERASE_32 if _32_bit else _CMD_SEC_ERASE + + def _get_id(self): + self.cs(0) + self.spi.write(bytearray([_CMD_JEDEC_ID])) + buf = self.spi.read(3) + self.cs(1) + return buf def _wait_wel1(self): # wait WEL=1 @@ -52,11 +68,11 @@ def _flash_modify(self, cmd, addr, buf): self._wait_wip0() def erase_block(self, addr): - self._flash_modify(_CMD_SEC_ERASE_32, addr, None) + self._flash_modify(self._ERASE, addr, None) def readinto(self, addr, buf): self.cs(0) - self.spi.write(bytearray([_CMD_READ_32, addr >> 16, addr >> 8, addr])) + self.spi.write(bytearray([self._READ, addr >> 16, addr >> 8, addr])) self.spi.readinto(buf) self.cs(1) @@ -67,7 +83,7 @@ def write(self, addr, buf): buf_offset = 0 while remain: l = min(_PAGE_SIZE - offset, remain) - self._flash_modify(_CMD_WRITE_32, addr, buf[buf_offset : buf_offset + l]) + self._flash_modify(self._WRITE, addr, buf[buf_offset : buf_offset + l]) remain -= l addr += l buf_offset += l From 0f0f2351bb3c45801250352e72e026e1671fd5d8 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 14 Jun 2022 22:32:44 -0500 Subject: [PATCH 2434/3533] stm32/boards/LEGO_HUB_NO6/cc2564: Make timer configurable. This adds configurable macros to define the timer and channel used to provide the Bluetooth 32768 MHz clock. This will allow code to be shared with LEGO_HUB_NO7. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO6/cc2564.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c b/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c index c54daf3002fe3..50ba81c57ac15 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c +++ b/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c @@ -38,6 +38,12 @@ #define CC2564_PIN_BT_SLOWCLK (pyb_pin_BT_SLOWCLK) #define CC2564_PIN_BT_ENABLE (pyb_pin_BT_ENABLE) +// slight difference between LEGO_HUB_NO6 and LEGO_HUB_NO7 +#ifndef CC2564_TIMER_BT_SLOWCLOCK_TIM +#define CC2564_TIMER_BT_SLOWCLOCK_TIM 8 +#define CC2564_TIMER_BT_SLOWCLOCK_TIM_CH 4 +#endif + STATIC void cc2564_wait_cts_low(mp_hal_pin_obj_t cts, uint32_t timeout_ms) { for (int i = 0; i < timeout_ms; ++i) { if (mp_hal_pin_read(cts) == 0) { @@ -53,18 +59,18 @@ int mp_bluetooth_hci_controller_init(void) { mp_hal_pin_low(CC2564_PIN_BT_ENABLE); // Output a 32768Hz signal on BTSLOWCLK. - // tim8 = pyb.Timer(8, freq=32768) - // tim8_ch4 = tim8.channel(4, pyb.Timer.PWM, pin=btclk) - // tim8_ch4.pulse_width_percent(50) - mp_obj_t args[6] = { MP_OBJ_NEW_SMALL_INT(8), MP_OBJ_NEW_QSTR(MP_QSTR_freq), MP_OBJ_NEW_SMALL_INT(32768), MP_OBJ_NULL }; - mp_obj_t tim8 = pyb_timer_type.make_new(&pyb_timer_type, 1, 1, args); - mp_load_method(tim8, MP_QSTR_channel, args); - args[2] = MP_OBJ_NEW_SMALL_INT(4); + // tim = pyb.Timer(TIM, freq=32768) + // tim_ch = tim.channel(TIM_CH, pyb.Timer.PWM, pin=btclk) + // tim_ch.pulse_width_percent(50) + mp_obj_t args[6] = { MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM), MP_OBJ_NEW_QSTR(MP_QSTR_freq), MP_OBJ_NEW_SMALL_INT(32768), MP_OBJ_NULL }; + mp_obj_t tim = pyb_timer_type.make_new(&pyb_timer_type, 1, 1, args); + mp_load_method(tim, MP_QSTR_channel, args); + args[2] = MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM_CH); args[3] = MP_OBJ_NEW_SMALL_INT(0); // CHANNEL_MODE_PWM_NORMAL args[4] = MP_OBJ_NEW_QSTR(MP_QSTR_pin); args[5] = (mp_obj_t)CC2564_PIN_BT_SLOWCLK; - mp_obj_t tim8_ch4 = mp_call_method_n_kw(2, 1, args); - mp_load_method(tim8_ch4, MP_QSTR_pulse_width_percent, args); + mp_obj_t tim_ch = mp_call_method_n_kw(2, 1, args); + mp_load_method(tim_ch, MP_QSTR_pulse_width_percent, args); args[2] = MP_OBJ_NEW_SMALL_INT(50); mp_call_method_n_kw(1, 0, args); From 10f85fee183404a3cba2795e6cd22c12be742fb3 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 14 Jun 2022 22:32:44 -0500 Subject: [PATCH 2435/3533] stm32/boards/LEGO_HUB_NO7: Add LEGO Hub No. 7 board definition. This adds support for the LEGO Hub No. 7, aka LEGO Technic Small hub, aka LEGO SPIKE Essential hub. This board is largely similar to Hub No. 6: - Same MCU (STM32F413 - different packaging with fewer pins). - Same Bluetooth chip (TI CC2564). - Same IMU chip. - Similar external flash chip - 4MiB instead of 32MiB. - 2 I/O ports instead of 6. - No display - only status and battery LEDs. - Different LED driver chip. - Only 1 button which is also the power button. - No speaker. Signed-off-by: David Lechner --- ports/stm32/boards/LEGO_HUB_NO7/README.md | 135 +++++++++++ ports/stm32/boards/LEGO_HUB_NO7/bdev.c | 3 + .../LEGO_HUB_NO7/bluetooth_init_cc2564C_1.5.c | 3 + ports/stm32/boards/LEGO_HUB_NO7/board.json | 13 ++ ports/stm32/boards/LEGO_HUB_NO7/board_init.c | 212 ++++++++++++++++++ ports/stm32/boards/LEGO_HUB_NO7/cc2564.c | 6 + ports/stm32/boards/LEGO_HUB_NO7/hub_display.c | 190 ++++++++++++++++ ports/stm32/boards/LEGO_HUB_NO7/hub_display.h | 4 + ports/stm32/boards/LEGO_HUB_NO7/manifest.py | 5 + .../stm32/boards/LEGO_HUB_NO7/mpconfigboard.h | 152 +++++++++++++ .../boards/LEGO_HUB_NO7/mpconfigboard.mk | 50 +++++ ports/stm32/boards/LEGO_HUB_NO7/pins.csv | 114 ++++++++++ .../boards/LEGO_HUB_NO7/stm32f4xx_hal_conf.h | 23 ++ 13 files changed, 910 insertions(+) create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/README.md create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/bdev.c create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/bluetooth_init_cc2564C_1.5.c create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/board.json create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/board_init.c create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/cc2564.c create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/hub_display.c create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/hub_display.h create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/manifest.py create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.h create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.mk create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/pins.csv create mode 100644 ports/stm32/boards/LEGO_HUB_NO7/stm32f4xx_hal_conf.h diff --git a/ports/stm32/boards/LEGO_HUB_NO7/README.md b/ports/stm32/boards/LEGO_HUB_NO7/README.md new file mode 100644 index 0000000000000..8159cf3f47130 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/README.md @@ -0,0 +1,135 @@ +LEGO Hub No.7 +============= + +This board definition is for the LEGO Hub No. 7, a LEGO control unit with 1 button, +2 RGB LEDs, 2 Powered Up ports, 6-DOF sensor, Bluetooth, USB, 4MiB external SPI +flash storage, and a rechargeable battery. + +Features that are currently supported: +- standard MicroPython +- machine and bluetooth modules +- filesystem +- USB VCP, MSC and HID + +The Hub has a bootloader preinstalled at 0x08000000 (which is 32kiB in size) which +cannot be erased. This bootloader is entered by holding down the button for 5 seconds, +at which point the USB DFU device appears. If the battery is installed then the +RGB LED will flash purple. If the battery is not installed, the LED will flash orange +briefly and then the hub will turn off (so having the battery installed is required). +When this bootloader is active, the flash from 0x08008000 and up can be erased +and programmed via USB DFU. + +The built-in bootloader has some drawbacks: it cannot be entered programmatically, +and it does not keep the Hub powered up when running from battery (which requires +keeping BAT_PWR_EN high). As such, this board is configured to work with mboot as +a secondary bootloader: mboot is placed at 0x08008000 and the main application +firmware at 0x08010000. When mboot is installed it can be entered programatically +via machine.bootloader(). + +Backing up original Hub firmware +-------------------------------- + +Before installing MicroPython it is advised to backup the original LEGO firmware that +the Hub comes installed with. To do this, enter the built-in bootloader by holding +down the power button for 5 seconds while powering up the Hub via USB (you may +need to take out the battery and disconnect USB to power off the Hub first). Then +run the following command from the root of this repository: + + $ cd ports/stm32 + $ make BOARD=LEGO_HUB_NO7 backup-hub-firmware + +This will create a file called `lego_hub_firmware.dfu`. Put this file in a safe +location. To restore it, enter the built-in bootloader again and run: + + $ make BOARD=LEGO_HUB_NO7 restore-hub-firmware + +This will restore the original firmware but not the filesystem. To recreate the +original filesystem the Hub must be updated using the appropriate LEGO PC +application. + +Installing MicroPython +---------------------- + +You first need to build and install mboot, which only needs to be done once. From +the root of this repository run: + + $ cd ports/stm32/mboot + $ make BOARD=LEGO_HUB_NO7 + +Now enter the built-in bootloader by holding down the power button for 5 +seconds while powering up the Hub via USB (you may need to take out the battery +and disconnect USB to power off the Hub first). Then run: + + $ make BOARD=LEGO_HUB_NO7 deploy + +mboot should now be installed. To enter mboot, remove USB and the battery. +Connect the USB cable (the other end of the USB cable must be connected to +something that provides power). The status light should start cycling through +different colors. Replace the battery (the button will not work without the +battery present). Press the button to activate the desired boot mode: + +- Status light is red - run application (normal boot). +- Status light is green - run application in factory file system mode. +- Status light is blue - run application in safe mode. +- Status light is white - start DFU on the USB port. + + +Now build MicroPython (start at the root of this repository): + + $ cd mpy-cross + $ make + $ cd ../ports/stm32 + $ make submodules + $ make BOARD=LEGO_HUB_NO7 + +And deploy to the Hub (making sure mboot DFU is active, the center button is +blinking red): + + $ make BOARD=LEGO_HUB_NO7 deploy + +If successful, the Hub should now appear as a USB serial and mass storage device. + +Using MicroPython on the Hub +---------------------------- + +Access the MicroPython REPL using mpremote (pip install mpremote), or with any +serial terminal program. + +To scan for BLE devices: + + >>> import bluetooth + >>> ble = bluetooth.BLE() + >>> ble.irq(lambda *x: print(*x)) + >>> ble.active(1) + >>> ble.gap_scan(2000, 625, 625) + +Use help("modules") to see available built-in modules. + +Updating MicroPython from the Hub's filesystem +---------------------------------------------- + +You can update the MicroPython application firmware using the instructions above +for installing the firmware for the first time. The Hub also supports updating +the application firmware from within MicroPython itself, using the on-board +filesystem. + +To use this feature, build the firmware (see above for details) then gzip it and +copy the resulting file to the Hub (eg using mpremote): + + $ make BOARD=LEGO_HUB_NO7 + $ gzip build-LEGO_HUB_NO7/firmware.dfu + $ mpremote cp build-LEGO_HUB_NO7/firmware.dfu.gz : + +Then get a REPL on the Hub and execute: + + >>> import appupdate + >>> appupdate.update_app("firmware.dfu.gz") + +You can alternatively run this REPL command using mpremote: + + $ mpremote exec --no-follow "import appupdate; appupdate.update_app('firmware.dfu.gz')" + +At that point the Hub should restart and the LED on the central button will flash +different colours. Once the update is complete the LED will stop flashing and the +Hub will appear again as a USB device. The application firmware is now updated +and you can remove the firmware.dfu.gz file if desired. diff --git a/ports/stm32/boards/LEGO_HUB_NO7/bdev.c b/ports/stm32/boards/LEGO_HUB_NO7/bdev.c new file mode 100644 index 0000000000000..f104c0f3f4634 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/bdev.c @@ -0,0 +1,3 @@ +// LEGO_HUB_NO7 is identical to LEGO_HUB_NO6 in this regard. + +#include "../LEGO_HUB_NO6/bdev.c" diff --git a/ports/stm32/boards/LEGO_HUB_NO7/bluetooth_init_cc2564C_1.5.c b/ports/stm32/boards/LEGO_HUB_NO7/bluetooth_init_cc2564C_1.5.c new file mode 100644 index 0000000000000..6ea298f68839e --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/bluetooth_init_cc2564C_1.5.c @@ -0,0 +1,3 @@ +// LEGO_HUB_NO7 is identical to LEGO_HUB_NO6 in this regard. + +#include "../LEGO_HUB_NO6/bluetooth_init_cc2564C_1.5.c" diff --git a/ports/stm32/boards/LEGO_HUB_NO7/board.json b/ports/stm32/boards/LEGO_HUB_NO7/board.json new file mode 100644 index 0000000000000..1ef02c17a0636 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/board.json @@ -0,0 +1,13 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [], + "images": [], + "mcu": "stm32f4", + "product": "Hub No.7", + "thumbnail": "", + "url": "", + "vendor": "LEGO" +} diff --git a/ports/stm32/boards/LEGO_HUB_NO7/board_init.c b/ports/stm32/boards/LEGO_HUB_NO7/board_init.c new file mode 100644 index 0000000000000..7b155b1c3cea8 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/board_init.c @@ -0,0 +1,212 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021-2022 Damien P. George + * Copyright (c) 2022 David Lechner + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mphal.h" +#include "irq.h" + +void board_init(void) { + if (query_irq() == IRQ_STATE_DISABLED) { + enable_irq(IRQ_STATE_ENABLED); + } + + // Enable 3V3 for all ports + mp_hal_pin_output(pyb_pin_PORT_3V3_EN); + mp_hal_pin_high(pyb_pin_PORT_3V3_EN); + + // Port A + // Enable RX/TX buffer + mp_hal_pin_output(pyb_pin_PORTA_EN); + mp_hal_pin_low(pyb_pin_PORTA_EN); + + // Port B + // Enable RX/TX buffer + mp_hal_pin_output(pyb_pin_PORTB_EN); + mp_hal_pin_low(pyb_pin_PORTB_EN); +} + +#if BUILDING_MBOOT + +#include "drivers/memory/spiflash.h" +#include "mboot/mboot.h" +#include "boardctrl.h" +#include "adc.h" +#include "hub_display.h" + +#define RESET_MODE_NUM_STATES (4) +#define RESET_MODE_TIMEOUT_CYCLES (8) + +// Location and value for the SPI flash update key. If this key exists at the defined +// location then mboot will attempt to do a filesystem-load update of the main firmware. +// This makes the update robust to power failures: if the update does not complete then +// it will be restarted the next time it powers up. Only when it fully completes will +// this key be erased, and then the application can run. +#define SPIFLASH_UPDATE_KEY_ADDR (1020 * 1024) +#define SPIFLASH_UPDATE_KEY_VALUE (0x12345678) + +static void board_led_pattern(int reset_mode, uint16_t brightness) { + switch (reset_mode) { + case BOARDCTRL_RESET_MODE_NORMAL: + // set status light to red + hub_display_set(3, brightness); + hub_display_set(4, 0); + hub_display_set(5, 0); + break; + case BOARDCTRL_RESET_MODE_SAFE_MODE: + // set status light to green + hub_display_set(3, 0); + hub_display_set(4, brightness); + hub_display_set(5, 0); + break; + case BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM: + // set status light to blue + hub_display_set(3, 0); + hub_display_set(4, 0); + hub_display_set(5, brightness); + break; + case BOARDCTRL_RESET_MODE_BOOTLOADER: + // set status light to white + hub_display_set(3, brightness); + hub_display_set(4, brightness); + hub_display_set(5, brightness); + break; + } + + hub_display_update(); +} + +static void board_battery_init(void) { + mp_hal_pin_config(pyb_pin_BAT_VMON_ADC, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0); + adc_config(ADC1, 12); +} + +// returns true if the battery is pressed, otherwise false +static int board_battery_state(void) { + uint16_t value = adc_config_and_read_u16(ADC1, 6, ADC_SAMPLETIME_15CYCLES); + // If battery voltage is above USB voltage, then we consider the battery + // to be present. + return value > 41100; // 41100 is approx 5.5V +} + +static void board_button_init(void) { + mp_hal_pin_input(pyb_pin_BUTTON); +} + +// returns true if the button is pressed, otherwise false +static int board_button_state(void) { + // button is active low + return !mp_hal_pin_read(pyb_pin_BUTTON); +} + +void board_mboot_cleanup(int reset_mode) { + board_led_pattern(0, 0); + hub_display_off(); +} + +void board_mboot_led_init(void) { + hub_display_on(); +} + +void board_mboot_led_state(int led, int state) { + if (state) { + hub_display_set(3 + led, 0x7fff); + } else { + hub_display_set(3 + led, 0); + } + + hub_display_update(); +} + +int board_mboot_get_reset_mode(uint32_t *initial_r0) { + board_battery_init(); + board_button_init(); + int reset_mode = BOARDCTRL_RESET_MODE_NORMAL; + + if (board_battery_state()) { + // Battery is present, check flash for update key and start an update if the key exists. + // Otherwise, boot normally. + + // Initialise the external SPI flash. + MBOOT_SPIFLASH_SPIFLASH->config = MBOOT_SPIFLASH_CONFIG; + mp_spiflash_init(MBOOT_SPIFLASH_SPIFLASH); + + // Read in the key. + uint32_t buf; + mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR, 4, (uint8_t *)&buf); + + if (buf == SPIFLASH_UPDATE_KEY_VALUE) { + // The key has the correct value, so read in the FS-load elements and enter the bootloader. + mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, ELEM_DATA_SIZE, ELEM_DATA_START); + *initial_r0 = MBOOT_INITIAL_R0_KEY_FSLOAD; + reset_mode = BOARDCTRL_RESET_MODE_BOOTLOADER; + } + } else { + // Battery is not present. Cycle through reset modes until button is pressed. + systick_init(); + hub_display_on(); + reset_mode = 0; + for (int i = 0; i < (RESET_MODE_NUM_STATES * RESET_MODE_TIMEOUT_CYCLES + 1) * 64; i++) { + if (i % 64 == 0) { + if (++reset_mode > RESET_MODE_NUM_STATES) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; + } + board_led_pattern(reset_mode, 0x7fff); + } + + if (board_button_state()) { + break; + } + + mp_hal_delay_ms(19); + } + + // Flash the selected reset mode. + for (int i = 0; i < 6; i++) { + board_led_pattern(reset_mode, 0x0fff); + mp_hal_delay_ms(50); + board_led_pattern(reset_mode, 0x7fff); + mp_hal_delay_ms(50); + } + + mp_hal_delay_ms(300); + } + + board_led_pattern(0, 0); + return reset_mode; +} + +void board_mboot_state_change(int state, uint32_t arg) { + if (state == MBOOT_STATE_FSLOAD_END) { + // The FS-load update completed (either with success or failure), so erase the + // update key and write the result of the FS-load operation into flash. + mp_spiflash_erase_block(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR); + mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, 4, (const uint8_t *)&arg); + } + + mboot_state_change_default(state, arg); +} + +#endif diff --git a/ports/stm32/boards/LEGO_HUB_NO7/cc2564.c b/ports/stm32/boards/LEGO_HUB_NO7/cc2564.c new file mode 100644 index 0000000000000..e5bfe1b4d4ca6 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/cc2564.c @@ -0,0 +1,6 @@ +// LEGO_HUB_NO7 is nearly identical to LEGO_HUB_NO6 in this regard. + +#define CC2564_TIMER_BT_SLOWCLOCK_TIM 2 +#define CC2564_TIMER_BT_SLOWCLOCK_TIM_CH 2 + +#include "../LEGO_HUB_NO6/cc2564.c" diff --git a/ports/stm32/boards/LEGO_HUB_NO7/hub_display.c b/ports/stm32/boards/LEGO_HUB_NO7/hub_display.c new file mode 100644 index 0000000000000..e19485b0646f9 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/hub_display.c @@ -0,0 +1,190 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * Copyright (c) 2022 David Lechner + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mphal.h" +#include "hub_display.h" + +#include STM32_HAL_H + +#define I2C_ADDR 0x28 + +// Registers +#define DEVICE_CONFIG0 0x00 +#define DEVICE_CONFIG1 0x01 +#define LED_CONFIG0 0x02 +#define BANK_BRIGHTNESS 0x03 +#define BANK_A_COLOR 0x04 +#define BANK_B_COLOR 0x05 +#define BANK_C_COLOR 0x06 +#define LED0_BRIGHTNESS 0x07 +#define LED1_BRIGHTNESS 0x08 +#define LED2_BRIGHTNESS 0x09 +#define LED3_BRIGHTNESS 0x0A +#define OUT0_COLOR 0x0B +#define OUT1_COLOR 0x0C +#define OUT2_COLOR 0x0D +#define OUT3_COLOR 0x0E +#define OUT4_COLOR 0x0F +#define OUT5_COLOR 0x10 +#define OUT6_COLOR 0x11 +#define OUT7_COLOR 0x12 +#define OUT8_COLOR 0x13 +#define OUT9_COLOR 0x14 +#define OUT10_COLOR 0x15 +#define OUT11_COLOR 0x16 +#define RESET 0x17 + +// Flags +#define DEVICE_CONFIG0_CHIP_EN (1 << 6) +#define DEVICE_CONFIG1_LOG_SCALE_EN (1 << 5) +#define DEVICE_CONFIG1_POWER_SAVE_EN (1 << 4) +#define DEVICE_CONFIG1_AUTO_INCR_EN (1 << 3) +#define DEVICE_CONFIG1_PWM_DITHERING_EN (1 << 2) +#define DEVICE_CONFIG1_MAX_CURRENT_OPTION (1 << 1) +#define DEVICE_CONFIG1_LED_GLOBAL_OFF (1 << 0) +#define LED_CONFIG0_LED3_BANK_EN (1 << 3) +#define LED_CONFIG0_LED2_BANK_EN (1 << 2) +#define LED_CONFIG0_LED1_BANK_EN (1 << 1) +#define LED_CONFIG0_LED0_BANK_EN (1 << 0) + +#define LP50XX_NUM_CH 6 + +// channel mapping: +// CH 0 = battery LED - red +// CH 1 = battery LED - green +// CH 2 = battery LED - blue +// CH 3 = status LED - red +// CH 4 = status LED - green +// CH 5 = status LED - blue + +#define FMPI2C_CONVERT_TIMINGS(PRESC, SCLDEL, SDADEL, SCLH, SCLL) \ + (((PRESC) << FMPI2C_TIMINGR_PRESC_Pos) | \ + ((SCLDEL) << FMPI2C_TIMINGR_SCLDEL_Pos) | \ + ((SDADEL) << FMPI2C_TIMINGR_SDADEL_Pos) | \ + ((SCLH) << FMPI2C_TIMINGR_SCLH_Pos) | \ + ((SCLL) << FMPI2C_TIMINGR_SCLL_Pos)) + +static FMPI2C_HandleTypeDef hub_display_i2c; +static bool hub_display_init; + +static struct { + uint8_t reg; + uint8_t values[LP50XX_NUM_CH]; +} __attribute__((packed)) hub_display_data = { + .reg = OUT0_COLOR, +}; + +void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c) { + __HAL_RCC_FMPI2C1_CLK_ENABLE(); + mp_hal_pin_config(pyb_pin_LED_SCL, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 4); + mp_hal_pin_config(pyb_pin_LED_SDA, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 4); +} + +void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c) { + __HAL_RCC_FMPI2C1_CLK_DISABLE(); + __HAL_RCC_FMPI2C1_FORCE_RESET(); + __HAL_RCC_FMPI2C1_RELEASE_RESET(); + mp_hal_pin_config(pyb_pin_LED_SCL, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + mp_hal_pin_config(pyb_pin_LED_SDA, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); +} + +static void hub_display_i2c_init(void) { + hub_display_i2c.Instance = FMPI2C1; + hub_display_i2c.Init.Timing = FMPI2C_CONVERT_TIMINGS(0, 4, 0, 19, 28); + hub_display_i2c.Init.OwnAddress1 = 0; + hub_display_i2c.Init.AddressingMode = FMPI2C_ADDRESSINGMODE_7BIT; + hub_display_i2c.Init.DualAddressMode = FMPI2C_DUALADDRESS_DISABLE; + hub_display_i2c.Init.OwnAddress2 = 0; + hub_display_i2c.Init.OwnAddress2Masks = FMPI2C_OA2_NOMASK; + hub_display_i2c.Init.GeneralCallMode = FMPI2C_GENERALCALL_DISABLE; + hub_display_i2c.Init.NoStretchMode = FMPI2C_NOSTRETCH_DISABLE; + HAL_FMPI2C_Init(&hub_display_i2c); +} + +void hub_display_set(uint8_t led, uint16_t value) { + if (led >= LP50XX_NUM_CH) { + return; + } + + hub_display_data.values[led] = value >> 8; +} + +void hub_display_update(void) { + if (!hub_display_init) { + return; + } + + HAL_FMPI2C_Master_Transmit(&hub_display_i2c, I2C_ADDR, (uint8_t *)&hub_display_data, + sizeof(hub_display_data), HAL_MAX_DELAY); +} + +void hub_display_on(void) { + if (hub_display_init) { + return; + } + + hub_display_i2c_init(); + mp_hal_pin_output(pyb_pin_LED_EN); + mp_hal_pin_high(pyb_pin_LED_EN); + + static const struct { + uint8_t reg; + uint8_t values[11]; + } __attribute__((packed)) init_data = { + .reg = DEVICE_CONFIG0, + .values = { + [DEVICE_CONFIG0] = DEVICE_CONFIG0_CHIP_EN, + [DEVICE_CONFIG1] = DEVICE_CONFIG1_POWER_SAVE_EN | DEVICE_CONFIG1_PWM_DITHERING_EN | DEVICE_CONFIG1_AUTO_INCR_EN, + [LED_CONFIG0] = 0, + [BANK_BRIGHTNESS] = 0, + [BANK_A_COLOR] = 0, + [BANK_B_COLOR] = 0, + [BANK_C_COLOR] = 0, + [LED0_BRIGHTNESS] = 51, // battery LED + [LED1_BRIGHTNESS] = 38, // status LED + [LED2_BRIGHTNESS] = 0, + [LED3_BRIGHTNESS] = 0, + } + }; + + HAL_FMPI2C_Master_Transmit(&hub_display_i2c, I2C_ADDR, (uint8_t *)&init_data, + sizeof(init_data), HAL_MAX_DELAY); + + hub_display_init = true; +} + +void hub_display_off(void) { + if (!hub_display_init) { + return; + } + + HAL_FMPI2C_DeInit(&hub_display_i2c); + + mp_hal_pin_config(pyb_pin_LED_EN, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0); + + hub_display_init = false; +} diff --git a/ports/stm32/boards/LEGO_HUB_NO7/hub_display.h b/ports/stm32/boards/LEGO_HUB_NO7/hub_display.h new file mode 100644 index 0000000000000..7623e128a804c --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/hub_display.h @@ -0,0 +1,4 @@ +void hub_display_on(void); +void hub_display_off(void); +void hub_display_update(void); +void hub_display_set(uint8_t led, uint16_t value); diff --git a/ports/stm32/boards/LEGO_HUB_NO7/manifest.py b/ports/stm32/boards/LEGO_HUB_NO7/manifest.py new file mode 100644 index 0000000000000..d746381637d96 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/manifest.py @@ -0,0 +1,5 @@ +include("$(PORT_DIR)/boards/manifest.py") + +# Modules for application firmware update. +freeze("$(PORT_DIR)/mboot", "fwupdate.py", opt=3) +freeze("$(PORT_DIR)/boards/LEGO_HUB_NO6", ("spiflash.py", "appupdate.py"), opt=3) diff --git a/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.h b/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.h new file mode 100644 index 0000000000000..50fb6c0607f89 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.h @@ -0,0 +1,152 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * The MIT License (MIT) + * Copyright (c) 2021 Damien P. George + */ + +#include + +#define MICROPY_HW_BOARD_NAME "LEGO Technic Hub No.7" +#define MICROPY_HW_MCU_NAME "STM32F413" + +#define MICROPY_HW_HAS_SWITCH (0) +#define MICROPY_HW_HAS_FLASH (1) +#define MICROPY_PY_PYB_LEGACY (0) +#define MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET (0) +#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0) +#define MICROPY_HW_ENABLE_RTC (1) +#define MICROPY_HW_ENABLE_RNG (1) +#define MICROPY_HW_ENABLE_DAC (1) +#define MICROPY_HW_ENABLE_USB (1) +#define MICROPY_HW_FLASH_FS_LABEL "HUB_NO7" + +// HSE is 16MHz, CPU freq set to 100MHz, buses at maximum freq +#define MICROPY_HW_CLK_PLLM (16) +#define MICROPY_HW_CLK_PLLN (200) +#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2) +#define MICROPY_HW_CLK_PLLQ (4) +#define MICROPY_HW_CLK_AHB_DIV (RCC_SYSCLK_DIV1) +#define MICROPY_HW_CLK_APB1_DIV (RCC_HCLK_DIV2) +#define MICROPY_HW_CLK_APB2_DIV (RCC_HCLK_DIV1) + +// For 2.7 to 3.6 V, 75 to 100 MHz: 3 wait states. +#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_3 + +// UART buses +// Bluetooth HCI +#define MICROPY_HW_UART2_CTS (pyb_pin_BT_CTS) +#define MICROPY_HW_UART2_RTS (pyb_pin_BT_RTS) +#define MICROPY_HW_UART2_TX (pyb_pin_BT_TX) +#define MICROPY_HW_UART2_RX (pyb_pin_BT_RX) +// Port B +#define MICROPY_HW_UART3_TX (pyb_pin_PORTB_TX) +#define MICROPY_HW_UART3_RX (pyb_pin_PORTB_RX) +// Port A +#define MICROPY_HW_UART5_TX (pyb_pin_PORTA_TX) +#define MICROPY_HW_UART5_RX (pyb_pin_PORTA_RX) + +// SPI buses +#define MICROPY_HW_SPI2_NSS (pyb_pin_FLASH_NSS) +#define MICROPY_HW_SPI2_SCK (pyb_pin_FLASH_SCK) +#define MICROPY_HW_SPI2_MISO (pyb_pin_FLASH_MISO) +#define MICROPY_HW_SPI2_MOSI (pyb_pin_FLASH_MOSI) + +// USB config +#define MICROPY_HW_USB_VBUS_DETECT_PIN (pyb_pin_USB_VBUS) +#define MICROPY_HW_USB_FS (1) +#define MICROPY_HW_USB_MSC (1) + +// Bluetooth config +#define MICROPY_HW_BLE_UART_ID (PYB_UART_2) +#define MICROPY_HW_BLE_UART_BAUDRATE (115200) +#define MICROPY_HW_BLE_UART_BAUDRATE_SECONDARY (921600) +#define MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE btstack_chipset_cc256x_instance() + +// SPI flash, for R/W storage +// The first 1MiB is skipped because it's used by the built-in bootloader +// Note: MICROPY_HW_SPIFLASH_OFFSET_BYTES must be a multiple of MP_SPIFLASH_ERASE_BLOCK_SIZE +#define MICROPY_HW_SPIFLASH_OFFSET_BYTES (1024 * 1024) +#define MICROPY_HW_SPIFLASH_BLOCKMAP(bl) ((bl) + MICROPY_HW_SPIFLASH_OFFSET_BYTES / FLASH_BLOCK_SIZE) +#define MICROPY_HW_SPIFLASH_BLOCKMAP_EXT(bl) ((bl) + MICROPY_HW_SPIFLASH_OFFSET_BYTES / MP_SPIFLASH_ERASE_BLOCK_SIZE) +#define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) +#define MICROPY_HW_SPIFLASH_SIZE_BITS (32 * 1024 * 1024 - MICROPY_HW_SPIFLASH_OFFSET_BYTES * 8) +#define MICROPY_HW_SPIFLASH_CS (MICROPY_HW_SPI2_NSS) +#define MICROPY_HW_SPIFLASH_SCK (MICROPY_HW_SPI2_SCK) +#define MICROPY_HW_SPIFLASH_MISO (MICROPY_HW_SPI2_MISO) +#define MICROPY_HW_SPIFLASH_MOSI (MICROPY_HW_SPI2_MOSI) + +// SPI flash, block device config +#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ + (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \ + (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \ + spi_bdev_ioctl(&spi_bdev, (op), (arg)) \ + ) + +// Configuration for stardard block protocol (block size FLASH_BLOCK_SIZE). +#define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) \ + spi_bdev_readblocks(&spi_bdev, (dest), MICROPY_HW_SPIFLASH_BLOCKMAP(bl), (n)) +#define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) \ + spi_bdev_writeblocks(&spi_bdev, (src), MICROPY_HW_SPIFLASH_BLOCKMAP(bl), (n)) + +// Configuration for extended block protocol (block size MP_SPIFLASH_ERASE_BLOCK_SIZE). +#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (MP_SPIFLASH_ERASE_BLOCK_SIZE) +#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) \ + (spi_bdev_readblocks_raw(&spi_bdev, (dest), MICROPY_HW_SPIFLASH_BLOCKMAP_EXT(bl), (off), (len))) +#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(src, bl, off, len) \ + (spi_bdev_writeblocks_raw(&spi_bdev, (src), MICROPY_HW_SPIFLASH_BLOCKMAP_EXT(bl), (off), (len))) +#define MICROPY_HW_BDEV_ERASEBLOCKS_EXT(bl, len) \ + (spi_bdev_eraseblocks_raw(&spi_bdev, MICROPY_HW_SPIFLASH_BLOCKMAP_EXT(bl), (len))) + +// Board control config +#define MICROPY_BOARD_STARTUP board_init + +/******************************************************************************/ +// Bootloader configuration + +// Configure CPU frequency to 96MHz, to make updates from SPI flash faster +#define MBOOT_CLK_PLLM (MICROPY_HW_CLK_VALUE / 1000000) +#define MBOOT_CLK_PLLN (192) +#define MBOOT_CLK_PLLP (RCC_PLLP_DIV2) +#define MBOOT_CLK_PLLQ (4) +#define MBOOT_CLK_AHB_DIV (RCC_SYSCLK_DIV1) +#define MBOOT_CLK_APB1_DIV (RCC_HCLK_DIV4) +#define MBOOT_CLK_APB2_DIV (RCC_HCLK_DIV2) +#define MBOOT_FLASH_LATENCY FLASH_LATENCY_3 + +#define MBOOT_FSLOAD (1) +#define MBOOT_VFS_FAT (1) +#define MBOOT_LEAVE_BOOTLOADER_VIA_RESET (0) + +#define MBOOT_SPIFLASH_ADDR (0x80000000) +#define MBOOT_SPIFLASH_BYTE_SIZE (4 * 1024 * 1024) +#define MBOOT_SPIFLASH_LAYOUT "/0x80000000/1024*4Kg" +#define MBOOT_SPIFLASH_ERASE_BLOCKS_PER_PAGE (1) +#define MBOOT_SPIFLASH_SPIFLASH (&board_mboot_spiflash) +#define MBOOT_SPIFLASH_CONFIG (&board_mboot_spiflash_config) + +#define MBOOT_LED1 0 +#define MBOOT_LED2 1 +#define MBOOT_LED3 2 +#define MBOOT_BOARD_LED_INIT board_mboot_led_init +#define MBOOT_BOARD_LED_STATE board_mboot_led_state + +#define MBOOT_BOARD_EARLY_INIT(initial_r0) board_init() +#define MBOOT_BOARD_CLEANUP board_mboot_cleanup +#define MBOOT_BOARD_GET_RESET_MODE board_mboot_get_reset_mode +#define MBOOT_BOARD_STATE_CHANGE board_mboot_state_change + +/******************************************************************************/ +// Function declarations + +extern const struct _mp_spiflash_config_t spiflash_config; +extern struct _spi_bdev_t spi_bdev; +extern const struct _mp_spiflash_config_t board_mboot_spiflash_config; +extern struct _mp_spiflash_t board_mboot_spiflash; + +void board_init(void); +void board_mboot_cleanup(int reset_mode); +void board_mboot_led_init(void); +void board_mboot_led_state(int led, int state); +int board_mboot_get_reset_mode(uint32_t *initial_r0); +void board_mboot_state_change(int state, uint32_t arg); +void *btstack_chipset_cc256x_instance(void); diff --git a/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.mk b/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.mk new file mode 100644 index 0000000000000..c06f9a26ad452 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/mpconfigboard.mk @@ -0,0 +1,50 @@ +MCU_SERIES = f4 +CMSIS_MCU = STM32F413xx +AF_FILE = boards/stm32f413_af.csv +LD_FILES = boards/LEGO_HUB_NO6/stm32f413xg.ld boards/common_bl.ld +TEXT0_ADDR = 0x08010000 + +BOOTLOADER_DFU_USB_VID ?= 0x0694 +BOOTLOADER_DFU_USB_PID ?= 0x000C + +# MicroPython settings +MICROPY_PY_BLUETOOTH ?= 1 +MICROPY_BLUETOOTH_NIMBLE ?= 0 +MICROPY_BLUETOOTH_BTSTACK ?= 1 +MICROPY_VFS_LFS2 ?= 1 + +# Board specific frozen modules +FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py + +SRC_HAL += $(STM32LIB_HAL_BASE)/Src/stm32f4xx_hal_fmpi2c.c + +ifneq ($(BUILDING_MBOOT),1) +LIB_SRC_C += lib/btstack/chipset/cc256x/btstack_chipset_cc256x.c +endif + +# Bootloader settings +MBOOT_TEXT0_ADDR = 0x08008000 +MBOOT_LD_FILES = ../boards/LEGO_HUB_NO6/mboot_memory.ld stm32_sections.ld + +# Backup/restore original Hub firmware + +HUB_FIRMWARE = lego_hub_firmware.dfu +HUB_FIRMWARE_ADDR = $(MBOOT_TEXT0_ADDR) +HUB_FIRMWARE_SIZE = 0xf8000 + +backup-hub-firmware: + $(Q)$(DFU_UTIL) -a 0 \ + -d $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) \ + -U $(HUB_FIRMWARE).bin \ + -s $(HUB_FIRMWARE_ADDR):$(HUB_FIRMWARE_SIZE) + $(Q)$(PYTHON) $(DFU) \ + -b $(HUB_FIRMWARE_ADDR):$(HUB_FIRMWARE).bin \ + -D $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) \ + $(HUB_FIRMWARE) + $(Q)$(RM) $(HUB_FIRMWARE).bin + $(ECHO) "Backup created in $(HUB_FIRMWARE)" + +restore-hub-firmware: + $(Q)$(DFU_UTIL) -a 0 \ + -d $(BOOTLOADER_DFU_USB_VID):$(BOOTLOADER_DFU_USB_PID) \ + -D $(HUB_FIRMWARE) diff --git a/ports/stm32/boards/LEGO_HUB_NO7/pins.csv b/ports/stm32/boards/LEGO_HUB_NO7/pins.csv new file mode 100644 index 0000000000000..dd19518b78a9e --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/pins.csv @@ -0,0 +1,114 @@ +BT_CTS,PA0 +BT_RTS,PA1 +BT_TX,PA2 +BT_RX,PA3 +,PA4 +,PA5 +BAT_VMON_ADC,PA6 +BAT_IMON_ADC,PA7 +LSM6_SCL,PA8 +USB_VBUS,PA9 +CHGMODE,PA10 +USB_DM,PA11 +USB_DP,PA12 +,PA13 +,PA14 +,PA15 +BAT_NTC,PB0 +BAT_PWR_EN,PB1 +BUTTON,PB2 +BT_SLOWCLK,PB3 +PORTB_M1,PB4 +PORTB_M2,PB5 +PORTA_M1,PB6 +PORTA_M2,PB7 +PORTB_EN,PB8 +PORTA_EN,PB9 +FLASH_SCK,PB10 +,PB11 +FLASH_NSS,PB12 +LED_EN,PB13 +LED_SDA,PB14 +LED_SCL,PB15 +,PC0 +,PC1 +FLASH_MISO,PC2 +FLASH_MOSI,PC3 +CHG_IMON_ADC,PC4 +,PC5 +CHGOK,PC6 +PORT_3V3_EN,PC7 +BT_ENABLE,PC8 +LSM6_SDA,PC9 +PORTB_TX,PC10 +PORTB_RX,PC11 +PORTA_TX,PC12 +LSM6_INT1,PC13 +,PC14 +,PC15 +,PD0 +,PD1 +PORTA_RX,PD2 +,PD3 +,PD4 +,PD5 +,PD6 +,PD7 +,PD8 +,PD9 +,PD10 +,PD11 +,PD12 +,PD13 +,PD14 +,PD15 +,PE0 +,PE1 +,PE2 +,PE3 +,PE4 +,PE5 +,PE6 +,PE7 +,PE8 +,PE9 +,PE10 +,PE11 +,PE12 +,PE13 +,PE14 +,PE15 +,PF0 +,PF1 +,PF2 +,PF3 +,PF4 +,PF5 +,PF6 +,PF7 +,PF8 +,PF9 +,PF10 +,PF11 +,PF12 +,PF13 +,PF14 +,PF15 +,PG0 +,PG1 +,PG2 +,PG3 +,PG4 +,PG5 +,PG6 +,PG7 +,PG8 +,PG9 +,PG10 +,PG11 +,PG12 +,PG13 +,PG14 +,PG15 +,PH0 +,PH1 diff --git a/ports/stm32/boards/LEGO_HUB_NO7/stm32f4xx_hal_conf.h b/ports/stm32/boards/LEGO_HUB_NO7/stm32f4xx_hal_conf.h new file mode 100644 index 0000000000000..bd70912748a55 --- /dev/null +++ b/ports/stm32/boards/LEGO_HUB_NO7/stm32f4xx_hal_conf.h @@ -0,0 +1,23 @@ +/* This file is part of the MicroPython project, http://micropython.org/ + * The MIT License (MIT) + * Copyright (c) 2019 Damien P. George + */ +#ifndef MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H +#define MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H + +#include "boards/stm32f4xx_hal_conf_base.h" + +#include "stm32f4xx_hal_fmpi2c.h" + +#define HAL_FMPI2C_MODULE_ENABLED + +// Oscillator values in Hz +#define HSE_VALUE (16000000) +#define LSE_VALUE (32768) +#define EXTERNAL_CLOCK_VALUE (12288000) + +// Oscillator timeouts in ms +#define HSE_STARTUP_TIMEOUT (100) +#define LSE_STARTUP_TIMEOUT (5000) + +#endif // MICROPY_INCLUDED_STM32F4XX_HAL_CONF_H From b560b9fe715e293caef80533d61d8c3b179b0339 Mon Sep 17 00:00:00 2001 From: Ian Davies Date: Sun, 3 Jul 2022 18:35:17 +0100 Subject: [PATCH 2436/3533] rp2/mbedtls: Enable certificate validity time validation. --- ports/rp2/mbedtls/mbedtls_config.h | 6 ++++++ ports/rp2/mbedtls/mbedtls_port.c | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/ports/rp2/mbedtls/mbedtls_config.h b/ports/rp2/mbedtls/mbedtls_config.h index 4bf606f5ea23b..743d0a6a8425e 100644 --- a/ports/rp2/mbedtls/mbedtls_config.h +++ b/ports/rp2/mbedtls/mbedtls_config.h @@ -93,6 +93,8 @@ #define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE #define MBEDTLS_X509_CRT_PARSE_C #define MBEDTLS_X509_USE_C +#define MBEDTLS_HAVE_TIME +#define MBEDTLS_HAVE_TIME_DATE // Memory allocation hooks #include @@ -103,6 +105,10 @@ void m_tracked_free(void *ptr); #define MBEDTLS_PLATFORM_STD_FREE m_tracked_free #define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf +// Time hook +time_t rp2_rtctime_seconds(time_t *timer); +#define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds + #include "mbedtls/check_config.h" #endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */ diff --git a/ports/rp2/mbedtls/mbedtls_port.c b/ports/rp2/mbedtls/mbedtls_port.c index aa0f9a36e0eaa..9067eca90ee55 100644 --- a/ports/rp2/mbedtls/mbedtls_port.c +++ b/ports/rp2/mbedtls/mbedtls_port.c @@ -29,6 +29,9 @@ #include "mbedtls_config.h" +#include "hardware/rtc.h" +#include "shared/timeutils/timeutils.h" + extern uint8_t rosc_random_u8(size_t cycles); int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) { @@ -39,4 +42,10 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t return 0; } +time_t rp2_rtctime_seconds(time_t *timer) { + datetime_t t; + rtc_get_datetime(&t); + return timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec); +} + #endif From fbe9417b90474dd1a08749b3a79311a8007a98fb Mon Sep 17 00:00:00 2001 From: Ian Davies Date: Wed, 6 Jul 2022 16:22:57 +0100 Subject: [PATCH 2437/3533] extmod/ntptime: Factor out ntptime module from esp8266 port. The ntptime module was previously only included in the ESP8266 port. This commit factors that module out into the extmod directory, makes it support different epochs, and includes it in the rp2 port. --- {ports/esp8266/modules => extmod}/ntptime.py | 17 +++++++++++++---- ports/esp32/boards/manifest.py | 2 +- ports/esp8266/boards/GENERIC_512K/manifest.py | 1 - ports/esp8266/boards/manifest.py | 1 + ports/rp2/boards/PICO_W/manifest.py | 1 + 5 files changed, 16 insertions(+), 6 deletions(-) rename {ports/esp8266/modules => extmod}/ntptime.py (69%) diff --git a/ports/esp8266/modules/ntptime.py b/extmod/ntptime.py similarity index 69% rename from ports/esp8266/modules/ntptime.py rename to extmod/ntptime.py index dd07e46f1d3b4..05d7e9717d82d 100644 --- a/ports/esp8266/modules/ntptime.py +++ b/extmod/ntptime.py @@ -1,3 +1,5 @@ +import utime + try: import usocket as socket except: @@ -7,9 +9,6 @@ except: import struct -# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 -NTP_DELTA = 3155673600 - # The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org' host = "pool.ntp.org" @@ -26,6 +25,17 @@ def time(): finally: s.close() val = struct.unpack("!I", msg[40:44])[0] + + EPOCH_YEAR = utime.gmtime(0)[0] + if EPOCH_YEAR == 2000: + # (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 + NTP_DELTA = 3155673600 + elif EPOCH_YEAR == 1970: + # (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60 + NTP_DELTA = 2208988800 + else: + raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR)) + return val - NTP_DELTA @@ -33,7 +43,6 @@ def time(): def settime(): t = time() import machine - import utime tm = utime.gmtime(t) machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) diff --git a/ports/esp32/boards/manifest.py b/ports/esp32/boards/manifest.py index 1dc1481a420df..f0ed38b515c71 100644 --- a/ports/esp32/boards/manifest.py +++ b/ports/esp32/boards/manifest.py @@ -1,6 +1,6 @@ freeze("$(PORT_DIR)/modules") freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py")) -freeze("$(MPY_DIR)/ports/esp8266/modules", "ntptime.py") +freeze("$(MPY_DIR)/extmod", "ntptime.py") freeze("$(MPY_DIR)/drivers/dht", "dht.py") freeze("$(MPY_DIR)/drivers/onewire") include("$(MPY_DIR)/extmod/uasyncio/manifest.py") diff --git a/ports/esp8266/boards/GENERIC_512K/manifest.py b/ports/esp8266/boards/GENERIC_512K/manifest.py index ee148c80892d8..e43d94843fdba 100644 --- a/ports/esp8266/boards/GENERIC_512K/manifest.py +++ b/ports/esp8266/boards/GENERIC_512K/manifest.py @@ -1,5 +1,4 @@ freeze("$(BOARD_DIR)", "_boot.py", opt=3) -freeze("$(PORT_DIR)/modules", ("apa102.py", "ntptime.py", "port_diag.py")) freeze("$(MPY_DIR)/drivers/dht", "dht.py") freeze("$(MPY_DIR)/drivers/onewire") include("$(MPY_DIR)/extmod/webrepl/manifest.py") diff --git a/ports/esp8266/boards/manifest.py b/ports/esp8266/boards/manifest.py index 598572d62ad48..c5809717e6927 100644 --- a/ports/esp8266/boards/manifest.py +++ b/ports/esp8266/boards/manifest.py @@ -1,4 +1,5 @@ freeze("$(PORT_DIR)/modules") +freeze("$(MPY_DIR)/extmod", "ntptime.py") freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py")) freeze("$(MPY_DIR)/drivers/dht", "dht.py") freeze("$(MPY_DIR)/drivers/onewire") diff --git a/ports/rp2/boards/PICO_W/manifest.py b/ports/rp2/boards/PICO_W/manifest.py index 1953d5cbd0693..eb748da485c98 100644 --- a/ports/rp2/boards/PICO_W/manifest.py +++ b/ports/rp2/boards/PICO_W/manifest.py @@ -2,6 +2,7 @@ freeze("$(MPY_DIR)/tools", "upip.py") freeze("$(MPY_DIR)/tools", "upip_utarfile.py") +freeze("$(MPY_DIR)/extmod", "ntptime.py") if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") From 1bf2fd0592df999f913dd96be981cdabc0b557ca Mon Sep 17 00:00:00 2001 From: Ian Davies Date: Thu, 21 Jul 2022 21:00:24 +0100 Subject: [PATCH 2438/3533] extmod/modussl_mbedtls: Set a more sensible default debug log level. --- extmod/modussl_mbedtls.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 8365c7a4a4d2e..b14ed9ad0e3d0 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -74,7 +74,7 @@ STATIC const mp_obj_type_t ussl_socket_type; STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) { (void)ctx; (void)level; - printf("DBG:%s:%04d: %s\n", file, line, str); + mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str); } #endif @@ -175,7 +175,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { mbedtls_ctr_drbg_init(&o->ctr_drbg); #ifdef MBEDTLS_DEBUG_C // Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose - mbedtls_debug_set_threshold(0); + mbedtls_debug_set_threshold(3); #endif mbedtls_entropy_init(&o->entropy); From b6c2196fbd27834079d9d306d21c0a0897f03c91 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sun, 17 Jul 2022 12:59:02 +0200 Subject: [PATCH 2439/3533] drivers/cyw43: Allow configuring the netif/mDNS hostname. Allow boards to configure/override the default hostname used for netif and mDNS. --- drivers/cyw43/cyw43.h | 9 +++++++++ drivers/cyw43/cyw43_ctrl.c | 2 ++ drivers/cyw43/cyw43_lwip.c | 7 ++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/cyw43/cyw43.h b/drivers/cyw43/cyw43.h index 7d3e30f5d4eda..5ca4898318a3c 100644 --- a/drivers/cyw43/cyw43.h +++ b/drivers/cyw43/cyw43.h @@ -47,6 +47,14 @@ #define CYW43_LINK_NONET (-2) #define CYW43_LINK_BADAUTH (-3) +#ifndef MICROPY_BOARD_HOSTNAME +#define MICROPY_BOARD_HOSTNAME "PYBD" +#endif + +#ifndef MICROPY_BOARD_HOSTNAME_LENGTH +#define MICROPY_BOARD_HOSTNAME_LENGTH 16 +#endif + typedef struct _cyw43_t { cyw43_ll_t cyw43_ll; @@ -76,6 +84,7 @@ typedef struct _cyw43_t { struct netif netif[2]; struct dhcp dhcp_client; dhcp_server_t dhcp_server; + char hostname[MICROPY_BOARD_HOSTNAME_LENGTH]; } cyw43_t; extern cyw43_t cyw43_state; diff --git a/drivers/cyw43/cyw43_ctrl.c b/drivers/cyw43/cyw43_ctrl.c index 203bc812ac13f..73e6e5895779e 100644 --- a/drivers/cyw43/cyw43_ctrl.c +++ b/drivers/cyw43/cyw43_ctrl.c @@ -101,6 +101,8 @@ void cyw43_init(cyw43_t *self) { self->ap_channel = 3; self->ap_ssid_len = 0; self->ap_key_len = 0; + strncpy(self->hostname, MICROPY_BOARD_HOSTNAME, MICROPY_BOARD_HOSTNAME_LENGTH); + self->hostname[MICROPY_BOARD_HOSTNAME_LENGTH - 1] = 0; cyw43_poll = NULL; } diff --git a/drivers/cyw43/cyw43_lwip.c b/drivers/cyw43/cyw43_lwip.c index 16ae6023755a1..f12a378c5da6d 100644 --- a/drivers/cyw43/cyw43_lwip.c +++ b/drivers/cyw43/cyw43_lwip.c @@ -117,7 +117,7 @@ void cyw43_tcpip_init(cyw43_t *self, int itf) { #else netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, cyw43_netif_init, netif_input); #endif - netif_set_hostname(n, "PYBD"); + netif_set_hostname(n, self->hostname); netif_set_default(n); netif_set_up(n); @@ -132,8 +132,9 @@ void cyw43_tcpip_init(cyw43_t *self, int itf) { #if LWIP_MDNS_RESPONDER // TODO better to call after IP address is set char mdns_hostname[9]; - memcpy(&mdns_hostname[0], "PYBD", 4); - mp_hal_get_mac_ascii(MP_HAL_MAC_WLAN0, 8, 4, &mdns_hostname[4]); + int len = MIN(strlen(self->hostname), 4); + memcpy(&mdns_hostname[0], self->hostname, len); + mp_hal_get_mac_ascii(MP_HAL_MAC_WLAN0, 4 + len, 8 - len, &mdns_hostname[len]); mdns_hostname[8] = '\0'; mdns_resp_add_netif(n, mdns_hostname, 60); #endif From 9dfabcd6d3d080aced888e8474e921f11dc979bb Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sun, 17 Jul 2022 13:05:56 +0200 Subject: [PATCH 2440/3533] extmod/network_cyw43: Add hostname config option. --- extmod/network_cyw43.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index c131fbb109ee5..9df799119fe61 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -381,6 +381,11 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map cyw43_ioctl(self->cyw, CYW43_IOCTL_GET_VAR, 13, buf, self->itf); return MP_OBJ_NEW_SMALL_INT(nw_get_le32(buf) / 4); } + #if !MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER + case MP_QSTR_hostname: { + return mp_obj_new_str(self->cyw->hostname, strlen(self->cyw->hostname)); + } + #endif default: mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); } @@ -453,6 +458,14 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map cyw43_ioctl(self->cyw, CYW43_IOCTL_SET_VAR, 9 + 4, buf, self->itf); break; } + #if !MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER + case MP_QSTR_hostname: { + const char *hostname = mp_obj_str_get_str(e->value); + strncpy(self->cyw->hostname, hostname, MICROPY_BOARD_HOSTNAME_LENGTH); + self->cyw->hostname[MICROPY_BOARD_HOSTNAME_LENGTH - 1] = 0; + break; + } + #endif default: mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); } From 6baeded32236feb9ba57ce78477fd1ee80fb4697 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 5 Aug 2022 18:52:13 -0500 Subject: [PATCH 2441/3533] py/runtime: Fix crash in star arg unpacking. The reallocation trigger for unpacking star args with unknown length did not take into account the number of fixed args remaining. So it was possible that the unpacked iterators could take up exactly the memory allocated then nothing would be left for fixed args after the star args. This causes a segfault crash. This is fixed by taking into account the remaining number of fixed args in the check to decide whether to realloc yet or not. Signed-off-by: David Lechner --- py/runtime.c | 2 +- tests/basics/fun_callstardblstar.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/py/runtime.c b/py/runtime.c index 2c3b3ddde4a4f..ea3553db72275 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -814,7 +814,7 @@ void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_ mp_obj_t iterable = mp_getiter(arg, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - if (args2_len >= args2_alloc) { + if (args2_len + (n_args - i) >= args2_alloc) { args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t), args2_alloc * 2 * sizeof(mp_obj_t)); args2_alloc *= 2; diff --git a/tests/basics/fun_callstardblstar.py b/tests/basics/fun_callstardblstar.py index f395df3333ded..c08e46f668eb7 100644 --- a/tests/basics/fun_callstardblstar.py +++ b/tests/basics/fun_callstardblstar.py @@ -37,3 +37,7 @@ def f2(*args, **kwargs): # case where *args is not a tuple/list and takes up most of the memory allocated for **kwargs f2(*iter(range(100)), **{str(i): i for i in range(100)}) + +# regression test - iterable with unknown len() was exactly using preallocated +# memory causing args 4 and 5 to overflow the allocated arg array +print(1, *iter((1, 2, 3)), *iter((1, 2, 3)), 4, 5, sep=",") From 6804b6f54fbed1f0fdb86668ebb46274e4876fa6 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 5 Apr 2022 08:25:54 +1000 Subject: [PATCH 2442/3533] stm32/boards/NUCLEO_WB55: Add error handling to firmware update scripts. In-the-field use of these FUS/WS firmware update scripts has exposed some weak points, causing corrupted FUS/WS firmware to be flashed to the unit. The problems are mostly caused with the ST GUI application, but sometimes from un-recognised failures during bin file transfer to the WB55 prior to running the rfcore_firmware.py script. Other failures were caused by incorrect load addresses being used, again both from user error copying the address from the HTML release notes to the GUI tool, but also from similarly not updating the address correctly in rfcore_firmware.py To guard against these errors and make it easier to prepare different versions, this commit adds a few features to the rfcore firmware update tools: - When creating the bin file, automatically parse the release note in the folder to get the correct address. - Add a footer to the bin file containing the name, version, CRC, address etc. - Before flashing rfcore, check if the same version is already installed. - Verify the CRC and obfuscation key before flashing bin. - Log the name and version of file being flashed. --- .../boards/NUCLEO_WB55/rfcore_firmware.py | 249 ++++++++++++++---- .../boards/NUCLEO_WB55/rfcore_makefirmware.py | 116 +++++++- 2 files changed, 308 insertions(+), 57 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py index 4085da90fd491..7cc81b4c682a1 100644 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_firmware.py @@ -34,36 +34,54 @@ # # To perform a firmware update: # -# 1. Generate "obfuscated" binary images using rfcore_makefirmware.py -# ./boards/NUCLEO_WB55/rfcore_makefirmware.py ~/src/github.com/STMicroelectronics/STM32CubeWB/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x/ /tmp +# 1. Generate "obfuscated" binary images using rfcore_makefirmware.py, eg. +# $ python3 ./boards/NUCLEO_WB55/rfcore_makefirmware.py ~/src/github.com/STMicroelectronics/STM32CubeWB/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x/ /tmp # This will generate /tmp/{fus_102,fus_110,ws_ble_hci}.bin +# It may warn that stm32wb5x_FUS_fw_1_0_2.bin cannot be found, newer packs don't include this +# which can be ignored unless your currently flashed FUS is older than 1.0.2 # # 2. Copy required files to the device filesystem. -# In general, it's always safe to copy all three files and the updater will -# figure out what needs to be done. This is the recommended option. -# However, if you already have the latest FUS (1.1.0) installed, then just the -# WS firmware is required. -# If a FUS binary is present, then the existing WS will be removed so it's a good -# idea to always include the WS binary if updating FUS. -# Note that a WS binary will not be installed unless FUS 1.1.0 is installed. +# $ mpremote cp /tmp/fus_102.bin : +# $ mpremote cp /tmp/fus_110.bin : +# $ mpremote cp /tmp/ws_ble_hci.bin : +# $ mpremote cp ./boards/NUCLEO_WB55/rfcore_firmware.py : +# In general, it's always safe to copy all three files and the updater will +# figure out what needs to be done. This is the recommended option. +# However, if you already have the latest FUS (1.1.0) installed, then just the +# WS firmware is required. +# If a FUS binary is present, then the existing WS will be removed so it's a good +# idea to always include the WS binary if updating FUS. +# Note that a WS binary will not be installed unless FUS 1.1.0 is installed. # # 3. Ensure boot.py calls `rfcore_firmware.resume()`. -# The WB55 will reset several times during the firmware update process, so this -# script manages the update state using RTC backup registers. -# `rfcore_firmware.resume()` will continue the update operation on startup to -# resume any in-progress update operation, and either trigger another reset, or -# return 0 to indicate that the operation completed successfully, or a reason -# code (see REASON_* below) to indicate failure. +# $ mpremote exec "import rfcore_firmware; rfcore_firmware.install_boot()" +# The WB55 will reset several times during the firmware update process, so this +# script manages the update state using RTC backup registers. +# `rfcore_firmware.resume()` will continue the update operation on startup to +# resume any in-progress update operation, and either trigger another reset, or +# return 0 to indicate that the operation completed successfully, or a reason +# code (see REASON_* below) to indicate failure. # # 4. Call rfcore_firmware.check_for_updates() to start the update process. -# The device will then immediately reboot and when the firmware update completes, -# the status will be returned from rfcore_firmware.resume(). See the REASON_ codes below. -# You can use the built-in stm.rfcore_fw_version() to query the installed version -# from your application code. +# $ mpremote exec "import rfcore_firmware; rfcore_firmware.check_for_updates()" +# The device will then immediately reboot and when the firmware update completes, +# the status will be returned from rfcore_firmware.resume(). See the REASON_ codes below. +# You can use the built-in stm.rfcore_fw_version() to query the installed version +# from your application code. import struct, os -import machine, stm -from micropython import const + +try: + import machine, stm + from ubinascii import crc32 + from micropython import const +except ImportError: + # cpython + from binascii import crc32 + + machine = stm = None + const = lambda x: x + _OGF_VENDOR = const(0x3F) @@ -174,13 +192,6 @@ _PATH_FUS_110 = "fus_110.bin" _PATH_WS_BLE_HCI = "ws_ble_hci.bin" -# This address is correct for versions up to v1.8 (assuming existing firmware deleted). -# Note any address from the end of the filesystem to the SFSA would be fine, but if -# the FUS is fixed in the future to use the specified address then these are the "correct" -# ones. -_ADDR_FUS = 0x080EC000 -_ADDR_WS_BLE_HCI = 0x080DC000 - # When installing the FUS/WS it can take a long time to return to the first # GET_STATE HCI command. # e.g. Installing stm32wb5x_BLE_Stack_full_fw.bin takes 3600ms to respond. @@ -242,10 +253,79 @@ def write(self, addr, buf, sz, key=0): machine.mem32[stm.FLASH + stm.FLASH_CR] = 0 -def _copy_file_to_flash(filename, addr): +def validate_crc(f): + """Should match copy of function in rfcore_makefirmware.py to confirm operation""" + f.seek(0) + file_crc = 0 + chunk = 16 * 1024 + buff = bytearray(chunk) + while True: + read = f.readinto(buff) + if read < chunk: + file_crc = crc32(buff[0:read], file_crc) + break + file_crc = crc32(buff, file_crc) + + file_crc = 0xFFFFFFFF & -file_crc - 1 + f.seek(0) + return file_crc == 0 + + +def check_file_details(filename): + with open(filename, "rb") as f: + if not validate_crc(f): + raise ValueError("file validation failed: incorrect crc") + + # Check the footer on the file + f.seek(-64, 2) + footer = f.read() + details = struct.unpack("<37sIIIIbbbII", footer) + ( + src_filename, + addr_1m, + addr_640k, + addr_512k, + addr_256k, + vers_major, + vers_minor, + vers_patch, + KEY, + crc, + ) = details + src_filename = src_filename.strip(b"\x00").decode() + if KEY != _OBFUSCATION_KEY: + raise ValueError("file validation failed: incorrect key") + + return ( + src_filename, + addr_1m, + addr_640k, + addr_512k, + addr_256k, + (vers_major, vers_minor, vers_patch), + ) + + +def _copy_file_to_flash(filename): flash = _Flash() flash.unlock() + # Reset any previously stored address + _write_target_addr(0) try: + ( + src_filename, + addr_1m, + addr_640k, + addr_512k, + addr_256k, + vers, + ) = check_file_details(filename) + + # TODO add support for querying the correct flash size on chip + addr = load_addr = addr_1m + + log(f"Writing {src_filename} v{vers[0]}.{vers[1]}.{vers[2]} to addr: 0x{addr:x}") + # Erase the entire staging area in flash. erase_addr = STAGING_AREA_START sfr_sfsa = machine.mem32[stm.FLASH + stm.FLASH_SFR] & 0xFF @@ -266,6 +346,9 @@ def _copy_file_to_flash(filename, addr): flash.write(addr, buf, sz, _OBFUSCATION_KEY) addr += 4096 + # Cache the intended target load address + _write_target_addr(load_addr) + finally: flash.lock() @@ -308,17 +391,25 @@ def _fus_fwdelete(): return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_FW_DELETE) -def _fus_run_fwupgrade(addr): +def _fus_run_fwupgrade(): # Note: Address is ignored by the FUS (see comments above). + addr = _read_target_addr() + if not addr: + log(f"Update failed: Invalid load address: 0x{addr:x}") + return False + + log(f"Loading to: 0x{addr:x}") return _run_sys_hci_cmd(_OGF_VENDOR, _OCF_FUS_FW_UPGRADE, struct.pack("= _FUS_VERSION_102 and fus_version < _FUS_VERSION_110: log("FUS 1.0.2 detected") - if _stat_and_start_copy( - _PATH_FUS_110, _ADDR_FUS, _STATE_COPYING_FUS, _STATE_COPIED_FUS - ): + if _stat_and_start_copy(_PATH_FUS_110, _STATE_COPYING_FUS, _STATE_COPIED_FUS): continue else: log("FUS is up-to-date") if fus_version >= _FUS_VERSION_110: if _stat_and_start_copy( - _PATH_WS_BLE_HCI, _ADDR_WS_BLE_HCI, _STATE_COPYING_WS, _STATE_COPIED_WS + _PATH_WS_BLE_HCI, + _STATE_COPYING_WS, + _STATE_COPIED_WS, ): continue else: @@ -465,7 +562,7 @@ def resume(): if fus_is_idle(): log("FUS copy complete, installing") _write_state(_STATE_INSTALLING_FUS) - _fus_run_fwupgrade(_ADDR_FUS) + _fus_run_fwupgrade() else: log("FUS copy bad state") _write_failure_state(REASON_FLASH_FUS_BAD_STATE) @@ -519,7 +616,7 @@ def resume(): if fus_is_idle(): log("WS copy complete, installing") _write_state(_STATE_INSTALLING_WS) - _fus_run_fwupgrade(_ADDR_WS_BLE_HCI) + _fus_run_fwupgrade() else: log("WS copy bad state") _write_failure_state(REASON_FLASH_WS_BAD_STATE) @@ -556,9 +653,59 @@ def resume(): _write_failure_state(REASON_WS_VENDOR + result) +def install_boot(): + boot_py = "/flash/boot.py" + header = "" + mode = "w" + try: + with open(boot_py, "r") as boot: + header = "\n" + mode = "a" + for line in boot: + if "rfcore_firmware.resume()" in line: + print("Already installed.") + return + + print("boot.py exists, adding upgrade handler.") + except OSError: + print("boot.py doesn't exists, adding with upgrade handler.") + + with open(boot_py, mode) as boot: + boot.write(header) + boot.write("# Handle rfcore updates.\n") + boot.write("import rfcore_firmware\n") + boot.write("rfcore_firmware.resume()\n") + + # Start a firmware update. # This will immediately trigger a reset and start the update process on boot. -def check_for_updates(): - log("Starting firmware update") - _write_state(_STATE_WAITING_FOR_FUS) - machine.reset() +def check_for_updates(force=False): + ( + src_filename, + addr_1m, + addr_640k, + addr_512k, + addr_256k, + vers_fus, + ) = check_file_details(_PATH_FUS_110) + ( + src_filename, + addr_1m, + addr_640k, + addr_512k, + addr_256k, + vers_ws, + ) = check_file_details(_PATH_WS_BLE_HCI) + current_version_fus = stm.rfcore_fw_version(_FW_VERSION_FUS) + fus_uptodate = current_version_fus[0:3] == vers_fus + + current_version_ws = stm.rfcore_fw_version(_FW_VERSION_WS) + ws_uptodate = current_version_ws[0:3] == vers_ws + if fus_uptodate and ws_uptodate and not force: + log(f"Already up to date: fus: {current_version_fus}, ws: {current_version_ws}") + else: + log(f"Starting firmware update") + log(f" - fus: {current_version_fus} -> {vers_fus}") + log(f" - ws: {current_version_ws} -> {vers_ws}") + _write_state(_STATE_WAITING_FOR_FUS) + machine.reset() diff --git a/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py b/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py index 23f3d20f0c9ce..6b2fb60892a9b 100755 --- a/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py +++ b/ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py @@ -30,28 +30,70 @@ # rfcore_firmware.py as well as instructions on how to use. import os +import re import struct import sys +from binascii import crc32 +from rfcore_firmware import validate_crc, _OBFUSCATION_KEY -# Must match rfcore_firmware.py. -_OBFUSCATION_KEY = 0x0573B55AA _FIRMWARE_FILES = { "stm32wb5x_FUS_fw_1_0_2.bin": "fus_102.bin", "stm32wb5x_FUS_fw.bin": "fus_110.bin", "stm32wb5x_BLE_HCILayer_fw.bin": "ws_ble_hci.bin", } +_RELEASE_NOTES = "Release_Notes.html" + + +def get_details(release_notes, filename): + if not release_notes: + return None + file_details = re.findall( + rb"%s,(((0x[\d\S]+?),)+[vV][\d\.]+)[<,]" % filename.encode(), + release_notes, + flags=re.DOTALL, + ) + # The release note has all past version details also, but current is at top + latest_details = file_details[0][0].split(b",") + addr_1m, addr_640k, addr_512k, addr_256k, version = latest_details + addr_1m = int(addr_1m, 0) + addr_640k = int(addr_640k, 0) + addr_512k = int(addr_512k, 0) + addr_256k = int(addr_256k, 0) + version = [int(v) for v in version.lower().lstrip(b"v").split(b".")] + return addr_1m, addr_640k, addr_512k, addr_256k, version def main(src_path, dest_path): - for src_file, dest_file in _FIRMWARE_FILES.items(): - src_file = os.path.join(src_path, src_file) + + # Load the release note to parse for important details + with open(os.path.join(src_path, _RELEASE_NOTES), "rb") as f: + release_notes = f.read() + # remove some formatting + release_notes = re.sub(rb"", b"", release_notes) + # flatten tables + release_notes = re.sub( + rb"\W*\n*\W*", + b",", + release_notes.replace(b"", b"").replace(b"", b""), + ) + if ( + b"Wireless Coprocessor Binary,STM32WB5xxG(1M),STM32WB5xxY(640k),STM32WB5xxE(512K),STM32WB5xxC(256K),Version," + not in release_notes + ): + raise SystemExit( + "Cannot determine binary load address, please confirm Coprocessor folder / Release Notes format." + ) + + for src_filename, dest_file in _FIRMWARE_FILES.items(): + src_file = os.path.join(src_path, src_filename) dest_file = os.path.join(dest_path, dest_file) if not os.path.exists(src_file): print("Unable to find: {}".format(src_file)) continue sz = 0 with open(src_file, "rb") as src: + crc = 0 with open(dest_file, "wb") as dest: while True: b = src.read(4) @@ -59,9 +101,71 @@ def main(src_path, dest_path): break (v,) = struct.unpack(" Date: Sat, 23 Jul 2022 13:38:32 +0800 Subject: [PATCH 2443/3533] mpy-cross/Makefile: Respect existing CFLAGS and LDFLAGS. --- mpy-cross/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpy-cross/Makefile b/mpy-cross/Makefile index 2189dff9058ea..4b6e03df8e844 100644 --- a/mpy-cross/Makefile +++ b/mpy-cross/Makefile @@ -19,7 +19,7 @@ INC += -I$(TOP) # compiler settings CWARN = -Wall -Werror CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith -CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS += $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables # Debugging/Optimization @@ -42,7 +42,7 @@ else # Use gcc syntax for map file LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections endif -LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) +LDFLAGS += $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) # source files SRC_C = \ From 736b427220ae0689694fc784bd4426605d556c4c Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 27 Jul 2022 10:45:38 +0200 Subject: [PATCH 2444/3533] extmod/network_wiznet5k: Register NIC when the lwIP stack is used. That was missing, and network.route() returned an empty list. --- extmod/network_wiznet5k.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index f328276a5b269..fecb6ae8c2714 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -224,6 +224,9 @@ STATIC void wiznet5k_init(void) { netif_set_link_up(&wiznet5k_obj.netif); netif_set_up(&wiznet5k_obj.netif); + + // register with network module + mod_network_register_nic(&wiznet5k_obj); } void wiznet5k_deinit(void) { From be2beab71a1a1047299601af70e99053fac508e5 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 27 Jul 2022 10:51:03 +0200 Subject: [PATCH 2445/3533] extmod/network_wiznet5k: Drop obsolete argument count check. Drop an obsolete and wrong argument check, which prevented specifying a pin for the interrupt signal. The proper checks are now done further down in the code. --- extmod/network_wiznet5k.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index fecb6ae8c2714..02bf90244d7e2 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -689,10 +689,8 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size #endif #ifdef MICROPY_HW_WIZNET_SPI_ID - // check arguments - mp_arg_check_num(n_args, n_kw, 0, 3, false); // Allow auto-configuration of SPI if defined for board and no args passed - if (n_args == 0) { + if (n_args == 0 && n_kw == 0) { // Initialize SPI. mp_obj_t spi_obj = MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_SPI_SCK); mp_obj_t miso_obj = MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_SPI_MISO); From 73699a846c031ecde70cf6bd629ef9a8232291e3 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 27 Jul 2022 21:01:29 +0200 Subject: [PATCH 2446/3533] extmod/network_wiznet5k: Deinit the NIC before (re-)initialisation. If nic.active(True) is called several times in a row, the device may lock up. Even if that is bad coding practice, calling wiznet5k_deinit() in wiznet5k_init() prevents the lock. --- extmod/network_wiznet5k.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 02bf90244d7e2..5a26c08952da7 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -198,6 +198,16 @@ STATIC void wiznet5k_config_interrupt(bool enabled) { ); } +void wiznet5k_deinit(void) { + for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) { + if (netif == &wiznet5k_obj.netif) { + netif_remove(netif); + netif->flags = 0; + break; + } + } +} + STATIC void wiznet5k_init(void) { // Configure wiznet for raw ethernet frame usage. @@ -219,6 +229,9 @@ STATIC void wiznet5k_init(void) { wiznet5k_config_interrupt(true); } + // Deinit before a new init to clear the state from a previous activation + wiznet5k_deinit(); + // Hook the Wiznet into lwIP wiznet5k_lwip_init(&wiznet5k_obj); @@ -229,16 +242,6 @@ STATIC void wiznet5k_init(void) { mod_network_register_nic(&wiznet5k_obj); } -void wiznet5k_deinit(void) { - for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) { - if (netif == &wiznet5k_obj.netif) { - netif_remove(netif); - netif->flags = 0; - break; - } - } -} - STATIC void wiznet5k_send_ethernet(wiznet5k_obj_t *self, size_t len, const uint8_t *buf) { uint8_t ip[4] = {1, 1, 1, 1}; // dummy int ret = WIZCHIP_EXPORT(sendto)(0, (byte *)buf, len, ip, 11); // dummy port From 999b66d53108a014194e7a30f20d4a269fdda6b4 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 29 Jul 2022 08:22:42 +0200 Subject: [PATCH 2447/3533] extmod/network_wiznet5k: Schedule clearing of interrupt flags. Avoiding conflicts between the IRQ and an active transfers. Before this change the device could lock up in heavy traffic situations. Fix found and code supplied by @omogenot. --- extmod/network_wiznet5k.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 5a26c08952da7..bdf94993d1f7a 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -177,11 +177,10 @@ STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self); STATIC mp_obj_t mpy_wiznet_read_int(mp_obj_t none_in) { (void)none_in; - wizchip_clrinterrupt(IK_SOCK_0); - setSn_IR(0, Sn_IR_RECV); - - // Handle incoming data - wiznet5k_try_poll(); + // Handle incoming data, unless the SPI bus is busy + if (mp_hal_pin_read(wiznet5k_obj.cs)) { + wiznet5k_try_poll(); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mpy_wiznet_read_int_obj, mpy_wiznet_read_int); @@ -343,6 +342,10 @@ void wiznet5k_poll(void) { } } } + wizchip_clrinterrupt(IK_SOCK_0); + #if _WIZCHIP_ == W5100S + setSn_IR(0, Sn_IR_RECV); // W5100S driver bug: must write to the Sn_IR register to reset the IRQ signal + #endif } #endif // MICROPY_PY_LWIP From f000ac9e824cfa2f2f3e50d902e80b576367b4c1 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 30 Jul 2022 12:30:09 +0200 Subject: [PATCH 2448/3533] extmod/network_wiznet5k: Rearrange the function wiznet5k_poll(). To have just one exit and a more compact flag test. This is just a style change without impact to the functionality. --- extmod/network_wiznet5k.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index bdf94993d1f7a..c862f16c0e7d7 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -325,20 +325,18 @@ STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self) { void wiznet5k_poll(void) { wiznet5k_obj_t *self = &wiznet5k_obj; - if (!(self->netif.flags & NETIF_FLAG_UP) || - !(self->netif.flags & NETIF_FLAG_LINK_UP)) { - return; - } - uint16_t len; - while ((len = wiznet5k_recv_ethernet(self)) > 0) { - if (self->trace_flags & TRACE_ETH_RX) { - netutils_ethernet_trace(MP_PYTHON_PRINTER, len, self->eth_frame, NETUTILS_TRACE_NEWLINE); - } - struct pbuf *p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - if (p != NULL) { - pbuf_take(p, self->eth_frame, len); - if (self->netif.input(p, &self->netif) != ERR_OK) { - pbuf_free(p); + if ((self->netif.flags & (NETIF_FLAG_UP | NETIF_FLAG_LINK_UP)) == (NETIF_FLAG_UP | NETIF_FLAG_LINK_UP)) { + uint16_t len; + while ((len = wiznet5k_recv_ethernet(self)) > 0) { + if (self->trace_flags & TRACE_ETH_RX) { + netutils_ethernet_trace(MP_PYTHON_PRINTER, len, self->eth_frame, NETUTILS_TRACE_NEWLINE); + } + struct pbuf *p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); + if (p != NULL) { + pbuf_take(p, self->eth_frame, len); + if (self->netif.input(p, &self->netif) != ERR_OK) { + pbuf_free(p); + } } } } From af6d2845fad4fde2d36f220efce876a4b1facdaa Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2022 00:53:13 +1000 Subject: [PATCH 2449/3533] extmod/network_wiznet5k: Extract SPI transfer function dynamically. Instead of using the fixed machine_spi_type entity to get the SPI transfer function, this transfer function is now extracted dynamically from the type of the SPI object. This allows the SPI object used to communicate with the WIZNET5K hardware to be SoftSPI or hardware SPI, or anything that has the SPI protocol (at the C level). Signed-off-by: Damien George --- extmod/network_wiznet5k.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index c862f16c0e7d7..89c933c2d4545 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -91,12 +91,11 @@ #endif #endif -extern struct _machine_spi_obj_t *spi_from_mp_obj(mp_obj_t o); - typedef struct _wiznet5k_obj_t { mp_obj_base_t base; mp_uint_t cris_state; - struct _machine_spi_obj_t *spi; + mp_obj_base_t *spi; + void (*spi_transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest); mp_hal_pin_obj_t cs; mp_hal_pin_obj_t rst; #if WIZNET5K_WITH_LWIP_STACK @@ -148,21 +147,21 @@ void mpy_wiznet_yield(void) { } STATIC void wiz_spi_read(uint8_t *buf, uint16_t len) { - ((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer((mp_obj_base_t *)wiznet5k_obj.spi, len, buf, buf); + wiznet5k_obj.spi_transfer(wiznet5k_obj.spi, len, buf, buf); } STATIC void wiz_spi_write(const uint8_t *buf, uint16_t len) { - ((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer((mp_obj_base_t *)wiznet5k_obj.spi, len, buf, NULL); + wiznet5k_obj.spi_transfer(wiznet5k_obj.spi, len, buf, NULL); } STATIC uint8_t wiz_spi_readbyte() { uint8_t buf = 0; - ((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer((mp_obj_base_t *)wiznet5k_obj.spi, 1, &buf, &buf); + wiznet5k_obj.spi_transfer(wiznet5k_obj.spi, 1, &buf, &buf); return buf; } STATIC void wiz_spi_writebyte(const uint8_t buf) { - ((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer((mp_obj_base_t *)wiznet5k_obj.spi, 1, &buf, NULL); + wiznet5k_obj.spi_transfer(wiznet5k_obj.spi, 1, &buf, NULL); } STATIC void wiznet5k_get_mac_address(wiznet5k_obj_t *self, uint8_t mac[6]) { @@ -680,7 +679,7 @@ STATIC void wiznet5k_dhcp_init(wiznet5k_obj_t *self) { // WIZNET5K(spi, pin_cs, pin_rst[, pin_intn]) // Create and return a WIZNET5K object. STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - struct _machine_spi_obj_t *spi; + mp_obj_base_t *spi; mp_hal_pin_obj_t cs; mp_hal_pin_obj_t rst; @@ -706,7 +705,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size MP_ROM_QSTR(MP_QSTR_miso), mp_pin_make_new(NULL, 1, 0, &miso_obj), MP_ROM_QSTR(MP_QSTR_mosi), mp_pin_make_new(NULL, 1, 0, &mosi_obj), }; - spi = machine_spi_type.make_new((mp_obj_t)&machine_spi_type, 2, 3, args); + spi = MP_OBJ_TO_PTR(machine_spi_type.make_new((mp_obj_t)&machine_spi_type, 2, 3, args)); cs = mp_hal_get_pin_obj(mp_pin_make_new(NULL, 1, 0, (mp_obj_t[]) {MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_PIN_CS)})); rst = mp_hal_get_pin_obj(mp_pin_make_new(NULL, 1, 0, (mp_obj_t[]) {MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_PIN_RST)})); @@ -724,7 +723,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size #else mp_arg_check_num(n_args, n_kw, 3, 3, false); #endif - spi = spi_from_mp_obj(args[0]); + spi = mp_hal_get_spi_obj(args[0]); cs = mp_hal_get_pin_obj(args[1]); rst = mp_hal_get_pin_obj(args[2]); #if WIZNET5K_WITH_LWIP_STACK @@ -742,6 +741,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k; wiznet5k_obj.cris_state = 0; wiznet5k_obj.spi = spi; + wiznet5k_obj.spi_transfer = ((mp_machine_spi_p_t *)spi->type->protocol)->transfer; wiznet5k_obj.cs = cs; wiznet5k_obj.rst = rst; #if WIZNET5K_WITH_LWIP_STACK From f72d3cec2338e88b9aa6e28d345f0007042d692b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2022 00:52:03 +1000 Subject: [PATCH 2450/3533] rp2/machine_spi: Add mp_hal_get_spi_obj helper function. And remove the now-obsolete spi_from_mp_obj() function. Signed-off-by: Damien George --- ports/rp2/machine_spi.c | 23 ++++++++++++++--------- ports/rp2/mphalport.h | 2 ++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c index 742a3cfddf44d..42998664a76de 100644 --- a/ports/rp2/machine_spi.c +++ b/ports/rp2/machine_spi.c @@ -269,15 +269,6 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8 } } -machine_spi_obj_t *spi_from_mp_obj(mp_obj_t o) { - if (mp_obj_is_type(o, &machine_spi_type)) { - machine_spi_obj_t *self = MP_OBJ_TO_PTR(o); - return self; - } else { - mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object")); - } -} - STATIC const mp_machine_spi_p_t machine_spi_p = { .init = machine_spi_init, .transfer = machine_spi_transfer, @@ -291,3 +282,17 @@ const mp_obj_type_t machine_spi_type = { .protocol = &machine_spi_p, .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, }; + +mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) { + if (mp_obj_is_type(o, &machine_spi_type)) { + return MP_OBJ_TO_PTR(o); + } + #if MICROPY_PY_MACHINE_SOFTSPI + else if (mp_obj_is_type(o, &mp_machine_soft_spi_type)) { + return MP_OBJ_TO_PTR(o); + } + #endif + else { + mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object")); + } +} diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h index 88683d9360a4e..31bedf3381245 100644 --- a/ports/rp2/mphalport.h +++ b/ports/rp2/mphalport.h @@ -148,6 +148,8 @@ enum mp_hal_pin_interrupt_trigger { void mp_hal_pin_interrupt(mp_hal_pin_obj_t pin, mp_obj_t handler, mp_uint_t trigger, bool hard); +mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t spi_in); + enum { MP_HAL_MAC_WLAN0 = 0, MP_HAL_MAC_BDADDR, From 7d91a9bf5b30c11ea2dcf90e8b3d031e40b6e985 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2022 14:09:02 +1000 Subject: [PATCH 2451/3533] py/mpprint: Fix formatting typo with mp_print_ext_t struct name. Signed-off-by: Damien George --- py/mpprint.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/mpprint.h b/py/mpprint.h index 0dff9a770ada7..8383ea85794c2 100644 --- a/py/mpprint.h +++ b/py/mpprint.h @@ -56,7 +56,7 @@ typedef struct _mp_print_ext_t { mp_print_t base; const char *item_separator; const char *key_separator; -}mp_print_ext_t; +} mp_print_ext_t; #define MP_PRINT_GET_EXT(print) ((mp_print_ext_t *)print) From b5986784e4341d7d34b97b2481567d842453732d Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2022 14:09:22 +1000 Subject: [PATCH 2452/3533] py/objstr: Reformat str access macros to make them readable. Signed-off-by: Damien George --- py/objstr.h | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/py/objstr.h b/py/objstr.h index 8c450baed6e41..8f87c3018a3c9 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -41,24 +41,38 @@ typedef struct _mp_obj_str_t { // use this macro to extract the string hash // warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data #define GET_STR_HASH(str_obj_in, str_hash) \ - mp_uint_t str_hash; if (mp_obj_is_qstr(str_obj_in)) \ - { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->hash; } + mp_uint_t str_hash; \ + if (mp_obj_is_qstr(str_obj_in)) { \ + str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); \ + } else { \ + str_hash = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->hash; \ + } // use this macro to extract the string length #define GET_STR_LEN(str_obj_in, str_len) \ - size_t str_len; if (mp_obj_is_qstr(str_obj_in)) \ - { str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; } + size_t str_len; \ + if (mp_obj_is_qstr(str_obj_in)) { \ + str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); \ + } else { \ + str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \ + } // use this macro to extract the string data and length #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len); #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \ - size_t str_len; const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len); + size_t str_len; \ + const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len); #else #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \ - const byte *str_data; size_t str_len; if (mp_obj_is_qstr(str_obj_in)) \ - { str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \ - else { str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; str_data = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->data; } + const byte *str_data; \ + size_t str_len; \ + if (mp_obj_is_qstr(str_obj_in)) { \ + str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); \ + } else { \ + str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \ + str_data = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->data; \ + } #endif mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args); From 5cfbf18d5fb70a907984781b3ac01aa5c27148be Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2022 14:09:44 +1000 Subject: [PATCH 2453/3533] javascript/Makefile: Remove obsolete disable of array-bounds warning. This was fixed in bb70874111dbb246624a68c013e8f1c3245ca0d8 Signed-off-by: Damien George --- ports/javascript/Makefile | 3 --- 1 file changed, 3 deletions(-) diff --git a/ports/javascript/Makefile b/ports/javascript/Makefile index 046d303336a95..93c7b5609b23b 100644 --- a/ports/javascript/Makefile +++ b/ports/javascript/Makefile @@ -53,7 +53,4 @@ test: $(BUILD)/micropython.js $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests.py -j1 -# Disable errors for array-bounds warnings on "sp[-MP_OBJ_ITER_BUF_NSLOTS + 2]" access. -$(BUILD)/py/vm.o: CFLAGS += -Wno-error=array-bounds - include $(TOP)/py/mkrules.mk From b2e82402688b53829f37475583231b067b9faea7 Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Tue, 2 Aug 2022 21:39:02 +0200 Subject: [PATCH 2454/3533] py/mkrules.mk: Keep all build artefacts inside $(BUILD) directory. The rules for lib (static library with name $(LIBMICROPYTHON)) and the default rule to build a binary (name $(PROG)) produced outputs in the current working directory. Change this to build these files in the build directory. Note: An empty BUILD variable can cause issues (references to the root directory); this is not addressed by this commit due to multiple other places having the same issue. --- py/mkrules.mk | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/py/mkrules.mk b/py/mkrules.mk index 41ec4b0641592..af116786798a9 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -196,9 +196,9 @@ ifneq (,$(findstring mingw,$(COMPILER_TARGET))) PROG := $(PROG).exe endif -all: $(PROG) +all: $(BUILD)/$(PROG) -$(PROG): $(OBJ) +$(BUILD)/$(PROG): $(OBJ) $(ECHO) "LINK $@" # Do not pass COPT here - it's *C* compiler optimizations. For example, # we may want to compile using Thumb, but link with non-Thumb libc. @@ -210,8 +210,8 @@ endif clean: clean-prog clean-prog: - $(RM) -f $(PROG) - $(RM) -f $(PROG).map + $(RM) -f $(BUILD)/$(PROG) + $(RM) -f $(BUILD)/$(PROG).map .PHONY: clean-prog endif @@ -231,8 +231,8 @@ LIBMICROPYTHON = libmicropython.a # with 3rd-party projects which don't have proper dependency # tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some # other file to cause needed effect, e.g. relinking with new lib. -lib $(LIBMICROPYTHON): $(OBJ) - $(Q)$(AR) rcs $(LIBMICROPYTHON) $^ +lib $(BUILD)/$(LIBMICROPYTHON): $(OBJ) + $(Q)$(AR) rcs $(BUILD)/$(LIBMICROPYTHON) $^ $(LIBMICROPYTHON_EXTRA_CMD) clean: From 47c84286e8c8d9873e99f12711a683ecd6b9ca62 Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Tue, 2 Aug 2022 23:48:41 +0200 Subject: [PATCH 2455/3533] all: Fix paths to mpy-cross and micropython binaries. Binaries built using the Make build system now no longer appear in the working directory of the build, but rather in the build directory. Thus some paths had to be adjusted. --- .github/workflows/ports_unix.yml | 2 +- examples/embedding/Makefile | 2 +- ports/unix/Makefile | 22 +++++++++++----------- py/dynruntime.mk | 2 +- py/mkenv.mk | 2 +- py/mkrules.cmake | 2 +- py/mkrules.mk | 2 +- tests/run-natmodtests.py | 2 +- tests/run-tests.py | 6 ++++-- tools/ci.sh | 20 ++++++++++---------- tools/makemanifest.py | 2 +- 11 files changed, 33 insertions(+), 31 deletions(-) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 63cc1c0faa921..ab2406647a8ca 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -35,7 +35,7 @@ jobs: env: SOURCE_DATE_EPOCH: 1234567890 - name: Check reproducible build date - run: echo | ports/unix/micropython-minimal -i | grep 'on 2009-02-13;' + run: echo | ports/unix/build-minimal/micropython-minimal -i | grep 'on 2009-02-13;' standard: runs-on: ubuntu-latest diff --git a/examples/embedding/Makefile b/examples/embedding/Makefile index 99f239a7c5af8..7de1219b26442 100644 --- a/examples/embedding/Makefile +++ b/examples/embedding/Makefile @@ -1,6 +1,6 @@ MPTOP = ../.. CFLAGS = -std=c99 -I. -I$(MPTOP) -DNO_QSTR -LDFLAGS = -L. +LDFLAGS = -L./build hello-embed: hello-embed.o -lmicropython diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 3b339c3d3f79f..709cc79853d57 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -304,18 +304,18 @@ include $(TOP)/py/mkrules.mk .PHONY: test test_full -test: $(PROG) $(TOP)/tests/run-tests.py +test: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py -test_full: $(PROG) $(TOP)/tests/run-tests.py +test_full: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py -d thread - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --emit native - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) -d basics float micropython - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) --emit native -d basics float micropython - cat $(TOP)/tests/basics/0prelim.py | ./$(PROG) | grep -q 'abc' + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py -d thread + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --emit native + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) -d basics float micropython + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) --emit native -d basics float micropython + cat $(TOP)/tests/basics/0prelim.py | ./$(BUILD)/$(PROG) | grep -q 'abc' test_gcov: test_full gcov -o $(BUILD)/py $(TOP)/py/*.c @@ -346,9 +346,9 @@ $(BUILD)/lib/libffi/include/ffi.h: $(TOP)/lib/libffi/configure PREFIX = /usr/local BINDIR = $(DESTDIR)$(PREFIX)/bin -install: $(PROG) +install: $(BUILD)/$(PROG) install -d $(BINDIR) - install $(PROG) $(BINDIR)/$(PROG) + install $(BUILD)/$(PROG) $(BINDIR)/$(PROG) uninstall: -rm $(BINDIR)/$(PROG) diff --git a/py/dynruntime.mk b/py/dynruntime.mk index 09cbb2dd37cbb..10feefc4a7c22 100644 --- a/py/dynruntime.mk +++ b/py/dynruntime.mk @@ -7,7 +7,7 @@ ECHO = @echo RM = /bin/rm MKDIR = /bin/mkdir PYTHON = python3 -MPY_CROSS = $(MPY_DIR)/mpy-cross/mpy-cross +MPY_CROSS = $(MPY_DIR)/mpy-cross/build/mpy-cross MPY_TOOL = $(PYTHON) $(MPY_DIR)/tools/mpy-tool.py MPY_LD = $(PYTHON) $(MPY_DIR)/tools/mpy_ld.py diff --git a/py/mkenv.mk b/py/mkenv.mk index cc04a8c0b3071..ea2e34f3b64de 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -61,7 +61,7 @@ MPY_LIB_SUBMODULE_DIR = $(TOP)/lib/micropython-lib MPY_LIB_DIR = $(MPY_LIB_SUBMODULE_DIR) ifeq ($(MICROPY_MPYCROSS),) -MICROPY_MPYCROSS = $(TOP)/mpy-cross/mpy-cross +MICROPY_MPYCROSS = $(TOP)/mpy-cross/build/mpy-cross MICROPY_MPYCROSS_DEPENDENCY = $(MICROPY_MPYCROSS) endif diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 2f168ede6f2b4..e7c4101ddb72d 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -193,7 +193,7 @@ if(MICROPY_FROZEN_MANIFEST) # to automatically build mpy-cross if needed. set(MICROPY_MPYCROSS $ENV{MICROPY_MPYCROSS}) if(NOT MICROPY_MPYCROSS) - set(MICROPY_MPYCROSS_DEPENDENCY ${MICROPY_DIR}/mpy-cross/mpy-cross) + set(MICROPY_MPYCROSS_DEPENDENCY ${MICROPY_DIR}/mpy-cross/build/mpy-cross) if(NOT MICROPY_MAKE_EXECUTABLE) set(MICROPY_MAKE_EXECUTABLE make) endif() diff --git a/py/mkrules.mk b/py/mkrules.mk index af116786798a9..73c33227c09af 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -162,7 +162,7 @@ $(HEADER_BUILD): ifneq ($(MICROPY_MPYCROSS_DEPENDENCY),) # to automatically build mpy-cross, if needed $(MICROPY_MPYCROSS_DEPENDENCY): - $(MAKE) -C $(dir $@) + $(MAKE) -C $(dir $@).. endif ifneq ($(FROZEN_DIR),) diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index 8eb27169c4ca1..9130e00d6eba1 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -14,7 +14,7 @@ # Paths for host executables CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") -MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython-coverage") +MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-coverage/micropython-coverage") NATMOD_EXAMPLE_DIR = "../examples/natmod/" diff --git a/tests/run-tests.py b/tests/run-tests.py index 2745ee1393bad..8e9bd843134fc 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -33,14 +33,16 @@ def base_path(*p): MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", base_path("../ports/windows/micropython.exe")) else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", base_path("../ports/unix/micropython")) + MICROPYTHON = os.getenv( + "MICROPY_MICROPYTHON", base_path("../ports/unix/build-standard/micropython") + ) # Use CPython options to not save .pyc files, to only access the core standard library # (not site packages which may clash with u-module names), and improve start up time. CPYTHON3_CMD = [CPYTHON3, "-BS"] # mpy-cross is only needed if --via-mpy command-line arg is passed -MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/mpy-cross")) +MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/build/mpy-cross")) # For diff'ing test output DIFF = os.getenv("MICROPY_DIFF", "diff -u") diff --git a/tools/ci.sh b/tools/ci.sh index 7e2479e43d388..20119342cfcf5 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -409,9 +409,9 @@ function ci_unix_run_tests_full_helper { variant=$1 shift if [ $variant = standard ]; then - micropython=micropython + micropython=build-$variant/micropython else - micropython=micropython-$variant + micropython=build-$variant/micropython-$variant fi make -C ports/unix VARIANT=$variant "$@" test_full (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/$micropython ./run-multitests.py multi_net/*.py) @@ -444,7 +444,7 @@ function ci_unix_minimal_build { } function ci_unix_minimal_run_tests { - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-minimal ./run-tests.py -e exception_chain -e self_type_check -e subclass_native_init -d basics) + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/build-minimal/micropython-minimal ./run-tests.py -e exception_chain -e self_type_check -e subclass_native_init -d basics) } function ci_unix_standard_build { @@ -491,21 +491,21 @@ function ci_unix_coverage_run_mpy_merge_tests { test=$(basename $inpy .py) echo $test outmpy=$outdir/$test.mpy - $mptop/mpy-cross/mpy-cross -o $outmpy $inpy - (cd $outdir && $mptop/ports/unix/micropython-coverage -m $test >> out-individual) + $mptop/mpy-cross/build/mpy-cross -o $outmpy $inpy + (cd $outdir && $mptop/ports/unix/build-coverage/micropython-coverage -m $test >> out-individual) allmpy+=($outmpy) done # Merge all the tests into one .mpy file, and then execute it. python3 $mptop/tools/mpy-tool.py --merge -o $outdir/merged.mpy ${allmpy[@]} - (cd $outdir && $mptop/ports/unix/micropython-coverage -m merged > out-merged) + (cd $outdir && $mptop/ports/unix/build-coverage/micropython-coverage -m merged > out-merged) # Make sure the outputs match. diff $outdir/out-individual $outdir/out-merged && /bin/rm -rf $outdir } function ci_unix_coverage_run_native_mpy_tests { - MICROPYPATH=examples/natmod/features2 ./ports/unix/micropython-coverage -m features2 + MICROPYPATH=examples/natmod/features2 ./ports/unix/build-coverage/micropython-coverage -m features2 (cd tests && ./run-natmodtests.py "$@" extmod/{btree*,framebuf*,uheapq*,urandom*,ure*,uzlib*}.py) } @@ -614,7 +614,7 @@ function ci_unix_macos_run_tests { # Issues with macOS tests: # - import_pkg7 has a problem with relative imports # - urandom_basic has a problem with getrandbits(0) - (cd tests && ./run-tests.py --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-standard/micropython ./run-tests.py --exclude 'import_pkg7.py' --exclude 'urandom_basic.py') } function ci_unix_qemu_mips_setup { @@ -634,7 +634,7 @@ function ci_unix_qemu_mips_run_tests { # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) # - ffi tests do not work file ./ports/unix/micropython-coverage - (cd tests && MICROPY_MICROPYTHON=../ports/unix/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') } function ci_unix_qemu_arm_setup { @@ -654,7 +654,7 @@ function ci_unix_qemu_arm_run_tests { # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) export QEMU_LD_PREFIX=/usr/arm-linux-gnueabi file ./ports/unix/micropython-coverage - (cd tests && MICROPY_MICROPYTHON=../ports/unix/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py') + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py') } ######################################################################################## diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 8cdc3eb7741b9..e69698d3f2340 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -329,7 +329,7 @@ def main(): sys.exit(1) # Get paths to tools - MPY_CROSS = VARS["MPY_DIR"] + "/mpy-cross/mpy-cross" + MPY_CROSS = VARS["MPY_DIR"] + "/mpy-cross/build/mpy-cross" if sys.platform == "win32": MPY_CROSS += ".exe" MPY_CROSS = os.getenv("MICROPY_MPYCROSS", MPY_CROSS) From 409995ac6828dc6bb70fcbe0bf9f26fd087ed22b Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Wed, 3 Aug 2022 01:32:51 +0200 Subject: [PATCH 2456/3533] tools/ci.sh: Force mpy-cross build for samd and teensy. --- tools/ci.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/ci.sh b/tools/ci.sh index 20119342cfcf5..cf6fc4170587a 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -293,6 +293,7 @@ function ci_samd_setup { } function ci_samd_build { + make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/samd submodules make ${MAKEOPTS} -C ports/samd } @@ -356,6 +357,7 @@ function ci_teensy_setup { } function ci_teensy_build { + make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/teensy submodules make ${MAKEOPTS} -C ports/teensy } From c7aa6a2c73854af9c0e9561e9213ef20abccf246 Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Wed, 3 Aug 2022 16:29:55 +0200 Subject: [PATCH 2457/3533] tests/run-tests.py: Provide better default MPYCROSS value for Windows. --- tests/run-tests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/run-tests.py b/tests/run-tests.py index 8e9bd843134fc..ca7941f6261bd 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -31,18 +31,20 @@ def base_path(*p): if os.name == "nt": CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python") MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", base_path("../ports/windows/micropython.exe")) + # mpy-cross is only needed if --via-mpy command-line arg is passed + MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/mpy-cross.exe")) else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") MICROPYTHON = os.getenv( "MICROPY_MICROPYTHON", base_path("../ports/unix/build-standard/micropython") ) + # mpy-cross is only needed if --via-mpy command-line arg is passed + MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/build/mpy-cross")) # Use CPython options to not save .pyc files, to only access the core standard library # (not site packages which may clash with u-module names), and improve start up time. CPYTHON3_CMD = [CPYTHON3, "-BS"] -# mpy-cross is only needed if --via-mpy command-line arg is passed -MPYCROSS = os.getenv("MICROPY_MPYCROSS", base_path("../mpy-cross/build/mpy-cross")) # For diff'ing test output DIFF = os.getenv("MICROPY_DIFF", "diff -u") From d53c3b6ade01650f88972ccf7e3002d2b5a84ff6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Aug 2022 12:40:23 +1000 Subject: [PATCH 2458/3533] unix/variants: Remove variant suffix from executable filename. The executable now lives in the build directory, and since the build directory already contains the variant name there is no need to also add it to the executable. Signed-off-by: Damien George --- .github/workflows/ports_unix.yml | 2 +- .../unix/variants/coverage/mpconfigvariant.mk | 2 -- ports/unix/variants/dev/mpconfigvariant.mk | 2 -- ports/unix/variants/fast/mpconfigvariant.mk | 2 -- .../unix/variants/freedos/mpconfigvariant.mk | 2 -- .../unix/variants/minimal/mpconfigvariant.mk | 1 - ports/unix/variants/nanbox/mpconfigvariant.mk | 1 - .../unix/variants/standard/mpconfigvariant.mk | 2 -- ports/windows/variants/dev/mpconfigvariant.mk | 2 -- tests/run-natmodtests.py | 2 +- tools/ci.sh | 26 ++++++++----------- tools/metrics.py | 4 +-- 12 files changed, 15 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index ab2406647a8ca..45cc87e723945 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -35,7 +35,7 @@ jobs: env: SOURCE_DATE_EPOCH: 1234567890 - name: Check reproducible build date - run: echo | ports/unix/build-minimal/micropython-minimal -i | grep 'on 2009-02-13;' + run: echo | ports/unix/build-minimal/micropython -i | grep 'on 2009-02-13;' standard: runs-on: ubuntu-latest diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index fac8c0d2752e0..bbbb0432b5b76 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -1,5 +1,3 @@ -PROG ?= micropython-coverage - # Disable optimisations and enable assert() on coverage builds. DEBUG ?= 1 diff --git a/ports/unix/variants/dev/mpconfigvariant.mk b/ports/unix/variants/dev/mpconfigvariant.mk index 78588d379bcde..058eda2f8d192 100644 --- a/ports/unix/variants/dev/mpconfigvariant.mk +++ b/ports/unix/variants/dev/mpconfigvariant.mk @@ -1,5 +1,3 @@ -PROG ?= micropython-dev - FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py MICROPY_ROM_TEXT_COMPRESSION = 1 diff --git a/ports/unix/variants/fast/mpconfigvariant.mk b/ports/unix/variants/fast/mpconfigvariant.mk index 595e5756457c9..b8fe69e487315 100644 --- a/ports/unix/variants/fast/mpconfigvariant.mk +++ b/ports/unix/variants/fast/mpconfigvariant.mk @@ -2,6 +2,4 @@ COPT += -fno-crossjumping -O2 -PROG = micropython-fast - FROZEN_MANIFEST = diff --git a/ports/unix/variants/freedos/mpconfigvariant.mk b/ports/unix/variants/freedos/mpconfigvariant.mk index a30db3e0c1490..86ab6864f45c1 100644 --- a/ports/unix/variants/freedos/mpconfigvariant.mk +++ b/ports/unix/variants/freedos/mpconfigvariant.mk @@ -10,8 +10,6 @@ CFLAGS += \ -DMICROPY_EMIT_X86=0 \ -DMICROPY_NO_ALLOCA=1 \ -PROG = micropython-freedos - MICROPY_PY_SOCKET = 0 MICROPY_PY_FFI = 0 MICROPY_PY_JNI = 0 diff --git a/ports/unix/variants/minimal/mpconfigvariant.mk b/ports/unix/variants/minimal/mpconfigvariant.mk index ec3b21c0b979c..9d4ddab9563e4 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.mk +++ b/ports/unix/variants/minimal/mpconfigvariant.mk @@ -1,5 +1,4 @@ # build a minimal interpreter -PROG = micropython-minimal FROZEN_MANIFEST = diff --git a/ports/unix/variants/nanbox/mpconfigvariant.mk b/ports/unix/variants/nanbox/mpconfigvariant.mk index 9752b922c1e31..e588e657efc5b 100644 --- a/ports/unix/variants/nanbox/mpconfigvariant.mk +++ b/ports/unix/variants/nanbox/mpconfigvariant.mk @@ -1,4 +1,3 @@ # build interpreter with nan-boxing as object model (object repr D) -PROG = micropython-nanbox MICROPY_FORCE_32BIT = 1 diff --git a/ports/unix/variants/standard/mpconfigvariant.mk b/ports/unix/variants/standard/mpconfigvariant.mk index cf3efab8ae7d9..def7987d8dc80 100644 --- a/ports/unix/variants/standard/mpconfigvariant.mk +++ b/ports/unix/variants/standard/mpconfigvariant.mk @@ -1,3 +1 @@ # This is the default variant when you `make` the Unix port. - -PROG ?= micropython diff --git a/ports/windows/variants/dev/mpconfigvariant.mk b/ports/windows/variants/dev/mpconfigvariant.mk index b2a0b5bb3388f..6d83f5b4dbc41 100644 --- a/ports/windows/variants/dev/mpconfigvariant.mk +++ b/ports/windows/variants/dev/mpconfigvariant.mk @@ -1,5 +1,3 @@ -PROG ?= micropython-dev - FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py MICROPY_ROM_TEXT_COMPRESSION = 1 diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index 9130e00d6eba1..16bb469222569 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -14,7 +14,7 @@ # Paths for host executables CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") -MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-coverage/micropython-coverage") +MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-coverage/micropython") NATMOD_EXAMPLE_DIR = "../examples/natmod/" diff --git a/tools/ci.sh b/tools/ci.sh index cf6fc4170587a..e3203f8bfb9f2 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -410,14 +410,10 @@ function ci_unix_run_tests_helper { function ci_unix_run_tests_full_helper { variant=$1 shift - if [ $variant = standard ]; then - micropython=build-$variant/micropython - else - micropython=build-$variant/micropython-$variant - fi + micropython=../ports/unix/build-$variant/micropython make -C ports/unix VARIANT=$variant "$@" test_full - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/$micropython ./run-multitests.py multi_net/*.py) - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/$micropython ./run-perfbench.py 1000 1000) + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=$micropython ./run-multitests.py multi_net/*.py) + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=$micropython ./run-perfbench.py 1000 1000) } function ci_native_mpy_modules_build { @@ -446,7 +442,7 @@ function ci_unix_minimal_build { } function ci_unix_minimal_run_tests { - (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/build-minimal/micropython-minimal ./run-tests.py -e exception_chain -e self_type_check -e subclass_native_init -d basics) + (cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/build-minimal/micropython ./run-tests.py -e exception_chain -e self_type_check -e subclass_native_init -d basics) } function ci_unix_standard_build { @@ -494,20 +490,20 @@ function ci_unix_coverage_run_mpy_merge_tests { echo $test outmpy=$outdir/$test.mpy $mptop/mpy-cross/build/mpy-cross -o $outmpy $inpy - (cd $outdir && $mptop/ports/unix/build-coverage/micropython-coverage -m $test >> out-individual) + (cd $outdir && $mptop/ports/unix/build-coverage/micropython -m $test >> out-individual) allmpy+=($outmpy) done # Merge all the tests into one .mpy file, and then execute it. python3 $mptop/tools/mpy-tool.py --merge -o $outdir/merged.mpy ${allmpy[@]} - (cd $outdir && $mptop/ports/unix/build-coverage/micropython-coverage -m merged > out-merged) + (cd $outdir && $mptop/ports/unix/build-coverage/micropython -m merged > out-merged) # Make sure the outputs match. diff $outdir/out-individual $outdir/out-merged && /bin/rm -rf $outdir } function ci_unix_coverage_run_native_mpy_tests { - MICROPYPATH=examples/natmod/features2 ./ports/unix/build-coverage/micropython-coverage -m features2 + MICROPYPATH=examples/natmod/features2 ./ports/unix/build-coverage/micropython -m features2 (cd tests && ./run-natmodtests.py "$@" extmod/{btree*,framebuf*,uheapq*,urandom*,ure*,uzlib*}.py) } @@ -635,8 +631,8 @@ function ci_unix_qemu_mips_run_tests { # Issues with MIPS tests: # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) # - ffi tests do not work - file ./ports/unix/micropython-coverage - (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') + file ./ports/unix/build-coverage/micropython + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') } function ci_unix_qemu_arm_setup { @@ -655,8 +651,8 @@ function ci_unix_qemu_arm_run_tests { # Issues with ARM tests: # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) export QEMU_LD_PREFIX=/usr/arm-linux-gnueabi - file ./ports/unix/micropython-coverage - (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython-coverage ./run-tests.py --exclude 'vfs_posix.py') + file ./ports/unix/build-coverage/micropython + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.py') } ######################################################################################## diff --git a/tools/metrics.py b/tools/metrics.py index c79b998e18468..da4d188568fdb 100755 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -60,8 +60,8 @@ def __init__(self, name, dir, output, make_flags=None): port_data = { "b": PortData("bare-arm", "bare-arm", "build/firmware.elf"), "m": PortData("minimal x86", "minimal", "build/firmware.elf"), - "u": PortData("unix x64", "unix", "micropython"), - "n": PortData("unix nanbox", "unix", "micropython-nanbox", "VARIANT=nanbox"), + "u": PortData("unix x64", "unix", "build-standard/micropython"), + "n": PortData("unix nanbox", "unix", "build-nanbox/micropython", "VARIANT=nanbox"), "s": PortData("stm32", "stm32", "build-PYBV10/firmware.elf", "BOARD=PYBV10"), "c": PortData("cc3200", "cc3200", "build/WIPY/release/application.axf", "BTARGET=application"), "8": PortData("esp8266", "esp8266", "build-GENERIC/firmware.elf"), From 2e8816de915244ee5612178711237f4f88d754ed Mon Sep 17 00:00:00 2001 From: Mat Booth Date: Sun, 31 Jul 2022 11:41:54 +0100 Subject: [PATCH 2459/3533] py/dynruntime.mk: Allow building assembly source in natmods. Allow inclusion of assembly source files in dynamic native modules. --- py/dynruntime.mk | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/dynruntime.mk b/py/dynruntime.mk index 10feefc4a7c22..62db43ad149ca 100644 --- a/py/dynruntime.mk +++ b/py/dynruntime.mk @@ -35,7 +35,7 @@ CFLAGS += -U _FORTIFY_SOURCE # prevent use of __*_chk libc functions MPY_CROSS_FLAGS += -march=$(ARCH) -SRC_O += $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(filter %.c,$(SRC)))) +SRC_O += $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(filter %.c,$(SRC))) $(patsubst %.S,%.o,$(filter %.S,$(SRC)))) SRC_MPY += $(addprefix $(BUILD)/, $(patsubst %.py,%.mpy,$(filter %.py,$(SRC)))) ################################################################################ @@ -134,6 +134,11 @@ $(BUILD)/%.o: %.c $(CONFIG_H) Makefile $(ECHO) "CC $<" $(Q)$(CROSS)gcc $(CFLAGS) -o $@ -c $< +# Build .o from .S source files +$(BUILD)/%.o: %.S $(CONFIG_H) Makefile + $(ECHO) "AS $<" + $(Q)$(CROSS)gcc $(CFLAGS) -o $@ -c $< + # Build .mpy from .py source files $(BUILD)/%.mpy: %.py $(ECHO) "MPY $<" From f3285fef07b4baa64cb05ef6dc034496cb0effdc Mon Sep 17 00:00:00 2001 From: Efi Weiss Date: Sun, 31 Jul 2022 22:16:49 +0300 Subject: [PATCH 2460/3533] py/nlrpowerpc: Fix generation of ppc64 code on ppc32 build. Due to inline assembly, wrong instructions were generated. Use corresponding 32 bit instructions and fix the offsets used. Signed-off-by: Efi Weiss --- py/nlrpowerpc.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/py/nlrpowerpc.c b/py/nlrpowerpc.c index 940d8562e120c..448459216b6e8 100644 --- a/py/nlrpowerpc.c +++ b/py/nlrpowerpc.c @@ -32,6 +32,8 @@ // Saving all ABI non-vol registers here +#ifdef __LP64__ + unsigned int nlr_push(nlr_buf_t *nlr) { __asm__ volatile ( @@ -118,4 +120,95 @@ NORETURN void nlr_jump(void *val) { MP_UNREACHABLE; } +#else +// Saving all ABI non-vol registers here + +unsigned int nlr_push(nlr_buf_t *nlr) { + + __asm__ volatile ( + "li 4, 0x4eed ; " // Store canary + "stw 4, 0x00(%0) ;" + "stw 0, 0x04(%0) ;" + "stw 1, 0x08(%0) ;" + "stw 2, 0x0c(%0) ;" + "stw 14, 0x10(%0) ;" + "stw 15, 0x14(%0) ;" + "stw 16, 0x18(%0) ;" + "stw 17, 0x1c(%0) ;" + "stw 18, 0x20(%0) ;" + "stw 19, 0x24(%0) ;" + "stw 20, 0x28(%0) ;" + "stw 21, 0x2c(%0) ;" + "stw 22, 0x30(%0) ;" + "stw 23, 0x34(%0) ;" + "stw 24, 0x38(%0) ;" + "stw 25, 0x3c(%0) ;" + "stw 26, 0x40(%0) ;" + "stw 27, 0x44(%0) ;" + "stw 28, 0x48(%0) ;" + "stw 29, 0x4c(%0) ;" + "stw 30, 0x50(%0) ;" + "stw 31, 0x54(%0) ;" + + "mfcr 4 ; " + "stw 4, 0x58(%0) ;" + "mflr 4 ;" + "stw 4, 0x5c(%0) ;" + "li 4, nlr_push_tail@l ;" + "oris 4, 4, nlr_push_tail@h ;" + "mtctr 4 ;" + "mr 3, %1 ; " + "bctr ;" + : + : "r" (&nlr->regs), "r" (nlr) + : + ); + + return 0; +} + +NORETURN void nlr_jump(void *val) { + MP_NLR_JUMP_HEAD(val, top) + + __asm__ volatile ( + "l 3, 0x0(%0) ;" + "cmpdi 3, 0x4eed ; " // Check canary + "bne . ; " + "l 0, 0x04(%0) ;" + "l 1, 0x08(%0) ;" + "l 2, 0x0c(%0) ;" + "l 14, 0x10(%0) ;" + "l 15, 0x14(%0) ;" + "l 16, 0x18(%0) ;" + "l 17, 0x1c(%0) ;" + "l 18, 0x20(%0) ;" + "l 19, 0x24(%0) ;" + "l 20, 0x28(%0) ;" + "l 21, 0x2c(%0) ;" + "l 22, 0x30(%0) ;" + "l 23, 0x34(%0) ;" + "l 24, 0x38(%0) ;" + "l 25, 0x3c(%0) ;" + "l 26, 0x40(%0) ;" + "l 27, 0x44(%0) ;" + "l 28, 0x48(%0) ;" + "l 29, 0x4c(%0) ;" + "l 30, 0x50(%0) ;" + "l 31, 0x54(%0) ;" + "l 3, 0x58(%0) ;" + "mtcr 3 ;" + "l 3, 0x5c(%0) ;" + "mtlr 3 ; " + "li 3, 1;" + "blr ;" + : + : "r" (&top->regs) + : + ); + + MP_UNREACHABLE; +} + +#endif // __LP64__ + #endif // MICROPY_NLR_POWERPC From 9c6fd974f769ef1e907c1a41f4a5d9b1c1774df7 Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Sun, 31 Jul 2022 18:01:13 +0200 Subject: [PATCH 2461/3533] minimal/Makefile: Avoid terminal reset, use BUILD variable. stty can provide the current terminal settings, so that they can be stored in a shell variable and restored after running the firmware. This avoids the complete "blanking" of the terminal, and thus also removes the need for the sleep call. The run target now references the firmware file using the BUILD variable instead of using the hard coded "build/" path. --- ports/minimal/Makefile | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 169132a1a29f7..5ba6514c9ed43 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -103,12 +103,11 @@ deploy: $(BUILD)/firmware.dfu # Run emulation build on a POSIX system with suitable terminal settings run: - stty raw opost -echo - build/firmware.elf - @echo Resetting terminal... -# This sleep is useful to spot segfaults - sleep 1 - reset + @saved_=`stty -g`; \ + stty raw opost -echo; \ + $(BUILD)/firmware.elf; \ + echo "Exit status: $$?"; \ + stty $$saved_ test: $(BUILD)/firmware.elf $(Q)/bin/echo -e "print('hello world!', list(x+1 for x in range(10)), end='eol\\\\n')\\r\\n\\x04" | $(BUILD)/firmware.elf | tail -n2 | grep "^hello world! \\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\\]eol" From b6651a7a89a666ca76cdc03e065e339dbf3b9298 Mon Sep 17 00:00:00 2001 From: David Yang Date: Mon, 4 Jan 2021 23:22:08 +0800 Subject: [PATCH 2462/3533] unix/modjni: Add missing const qualifier. --- ports/unix/modjni.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index c86f30653c81a..6fa00731fb753 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -376,7 +376,7 @@ STATIC const char *strprev(const char *s, char c) { STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) { const char *arg_type = *jtypesig; - mp_obj_type_t *type = mp_obj_get_type(arg); + const mp_obj_type_t *type = mp_obj_get_type(arg); if (type == &mp_type_str) { if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) { From 6e51dbd95aa051533e2618e2df8f51cb0f06cfed Mon Sep 17 00:00:00 2001 From: omogenot Date: Tue, 26 Jul 2022 16:36:10 +0200 Subject: [PATCH 2463/3533] rp2/boards/W5500_EVB_PICO: Add new board definition for W5500_EVB_PICO. Signed-off-by: github@mymeterinfo.info --- ports/rp2/boards/W5500_EVB_PICO/board.json | 20 +++++++++++++++++++ .../boards/W5500_EVB_PICO/mpconfigboard.cmake | 4 ++++ .../rp2/boards/W5500_EVB_PICO/mpconfigboard.h | 19 ++++++++++++++++++ ports/rp2/boards/W5500_EVB_PICO/readme.md | 18 +++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 ports/rp2/boards/W5500_EVB_PICO/board.json create mode 100644 ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake create mode 100644 ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h create mode 100644 ports/rp2/boards/W5500_EVB_PICO/readme.md diff --git a/ports/rp2/boards/W5500_EVB_PICO/board.json b/ports/rp2/boards/W5500_EVB_PICO/board.json new file mode 100644 index 0000000000000..90b3186cd6b1b --- /dev/null +++ b/ports/rp2/boards/W5500_EVB_PICO/board.json @@ -0,0 +1,20 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [ + "Breadboard Friendly", + "Castellated Pads", + "Ethernet", + "Micro USB" + ], + "images": [ + "W5500-EVB-Pico.jpg" + ], + "mcu": "rp2040", + "product": "Wiznet W5500-EVB-Pico", + "thumbnail": "", + "url": "https://www.wiznet.io/product-item/w5500-evb-pico/", + "vendor": "Wiznet" +} diff --git a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake new file mode 100644 index 0000000000000..875b89f2befe0 --- /dev/null +++ b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake @@ -0,0 +1,4 @@ +# cmake file for Wiznet W5500-EVB-Pico. +set(PICO_BOARD pico) +set(MICROPY_PY_NETWORK_WIZNET5K W5500) +set(MICROPY_PY_LWIP 1) diff --git a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h new file mode 100644 index 0000000000000..1ad7a0e905327 --- /dev/null +++ b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.h @@ -0,0 +1,19 @@ +// Board config for Wiznet W5500-EVB-Pico. + +// Board and hardware specific configuration +#define MICROPY_HW_BOARD_NAME "W5500-EVB-Pico" +#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) + +// Enable networking. +#define MICROPY_PY_NETWORK (1) + +// Wiznet HW config. +#define MICROPY_HW_WIZNET_SPI_ID (0) +#define MICROPY_HW_WIZNET_SPI_BAUDRATE (20 * 1000 * 1000) +#define MICROPY_HW_WIZNET_SPI_SCK (18) +#define MICROPY_HW_WIZNET_SPI_MOSI (19) +#define MICROPY_HW_WIZNET_SPI_MISO (16) +#define MICROPY_HW_WIZNET_PIN_CS (17) +#define MICROPY_HW_WIZNET_PIN_RST (20) +// Connecting the INTN pin enables RECV interrupt handling of incoming data. +#define MICROPY_HW_WIZNET_PIN_INTN (21) diff --git a/ports/rp2/boards/W5500_EVB_PICO/readme.md b/ports/rp2/boards/W5500_EVB_PICO/readme.md new file mode 100644 index 0000000000000..be28286695fa0 --- /dev/null +++ b/ports/rp2/boards/W5500_EVB_PICO/readme.md @@ -0,0 +1,18 @@ +# Wiznet W5500-EVB-Pico + +## Network Example + +To use network / socket based code, connect ethernet port to network with DHCP running: + +``` +>>> import network +>>> nic = network.WIZNET5K() +>>> nic.active(True) +>>> nic.ifconfig() +('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0') +>>> nic.ifconfig("dhcp") +('192.168.0.10', '255.255.255.0', '192.168.0.1', '192.168.0.1') +>>> nic.isconnected() +True +``` +At this point standard network communications libraries should work. From 71f6eb5ac953637e815ec9c5cbc785b4027a8759 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 5 Aug 2022 09:43:22 +0100 Subject: [PATCH 2464/3533] rp2: Mark gc_heap NOLOAD for faster boot. Create a new linker section .unitialized_bss for bss that does not need zero-initialising. Move gc_heap to this section, which saves ~30ms from rising edge of RESET to setting a pin HIGH in MicroPython. Zero fill happens in Pico SDK crt0.S before ROSC is configured. It's very, very slow. Signed-off-by: Phil Howard --- ports/rp2/main.c | 2 +- ports/rp2/memmap_mp.ld | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index d976464de1cc5..1062236a3916b 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -66,7 +66,7 @@ #endif extern uint8_t __StackTop, __StackBottom; -static char gc_heap[MICROPY_GC_HEAP_SIZE]; +__attribute__((section(".uninitialized_bss"))) static char gc_heap[MICROPY_GC_HEAP_SIZE]; // Embed version info in the binary in machine readable form bi_decl(bi_program_version_string(MICROPY_GIT_TAG)); diff --git a/ports/rp2/memmap_mp.ld b/ports/rp2/memmap_mp.ld index 0dc96743ea5cb..82f9cb01c3eff 100644 --- a/ports/rp2/memmap_mp.ld +++ b/ports/rp2/memmap_mp.ld @@ -180,6 +180,12 @@ SECTIONS *(.uninitialized_data*) } > RAM + /* bss without zero init on startup */ + .uninitialized_bss (NOLOAD): { + . = ALIGN(4); + *(.uninitialized_bss*) + } > RAM + /* Start and end symbols must be word-aligned */ .scratch_x : { __scratch_x_start__ = .; From 0546a12238b53f3243cc6ae5dfbb7bcac1d0be9a Mon Sep 17 00:00:00 2001 From: Jacob Siverskog Date: Tue, 9 Aug 2022 14:21:05 +0200 Subject: [PATCH 2465/3533] rp2: Correctly determine path to arm-none-eabi-size. Figure out path to arm-none-eabi-size the same way it's done for the other binaries, instead of assuming it to be in the user's $PATH. Signed-off-by: Jacob Siverskog --- ports/rp2/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index f5bd61c44ea84..37ac78b7339f4 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -412,9 +412,11 @@ endif() pico_add_extra_outputs(${MICROPY_TARGET}) +pico_find_compiler(PICO_COMPILER_SIZE ${PICO_GCC_TRIPLE}-size) + add_custom_command(TARGET ${MICROPY_TARGET} POST_BUILD - COMMAND arm-none-eabi-size --format=berkeley ${PROJECT_BINARY_DIR}/${MICROPY_TARGET}.elf + COMMAND ${PICO_COMPILER_SIZE} --format=berkeley ${PROJECT_BINARY_DIR}/${MICROPY_TARGET}.elf VERBATIM ) From 888e831bf78518c367d40a6b77b29c6aab13a7f0 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 8 Jul 2022 13:47:54 -0500 Subject: [PATCH 2466/3533] docs: Update links for Arm GCC toolchain. The separate A and RM toolchains have been discontinued and replaced by a single toolchain. This updates the links to the RM toolchain to the new toolchain. Signed-off-by: David Lechner --- README.md | 2 +- docs/develop/gettingstarted.rst | 2 +- ports/qemu-arm/README.md | 2 +- ports/renesas-ra/README.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 920f10a5035b8..6941102f0ac48 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ The STM32 version The "stm32" port requires an ARM compiler, arm-none-eabi-gcc, and associated bin-utils. For those using Arch Linux, you need arm-none-eabi-binutils, arm-none-eabi-gcc and arm-none-eabi-newlib packages. Otherwise, try here: -https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm +https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads To build: diff --git a/docs/develop/gettingstarted.rst b/docs/develop/gettingstarted.rst index 36062ddc008bd..000b7d61390cb 100644 --- a/docs/develop/gettingstarted.rst +++ b/docs/develop/gettingstarted.rst @@ -103,7 +103,7 @@ For the stm32 port, the ARM cross-compiler is required: $ sudo apt-get install arm-none-eabi-gcc arm-none-eabi-binutils arm-none-eabi-newlib See the `ARM GCC -toolchain `_ +toolchain `_ for the latest details. Python is also required. Python 2 is supported for now, but we recommend using Python 3. diff --git a/ports/qemu-arm/README.md b/ports/qemu-arm/README.md index 2c815c54b088a..f821c4d1e28fb 100644 --- a/ports/qemu-arm/README.md +++ b/ports/qemu-arm/README.md @@ -16,7 +16,7 @@ The purposes of this port are to enable: process in terms of plugging things together, pressing buttons, etc. This port will only work with the [GNU ARM Embedded Toolchain]( -https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm) +https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads and not with CodeSourcery toolchain. You will need to modify `LDFLAGS` if you want to use CodeSourcery's version of `arm-none-eabi`. The difference is that CodeSourcery needs `-T generic-m-hosted.ld` while diff --git a/ports/renesas-ra/README.md b/ports/renesas-ra/README.md index 482870e1c0368..c75a9e13d1ed0 100644 --- a/ports/renesas-ra/README.md +++ b/ports/renesas-ra/README.md @@ -36,7 +36,7 @@ All other commands below should be executed from the ports/renesas-ra/ directory * Arm compiler An `Arm compiler` is required for the build, along with the associated binary utilities. The default compiler is `arm-none-eabi-gcc`, which is available for -Arch Linux and Windows hosts via https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads. +Linux, Mac and Windows hosts via https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads. The compiler can be changed using the `CROSS_COMPILE` variable when invoking `make`. From 9bcb2c0a209f7970e63757a5f99dd23e010dbc48 Mon Sep 17 00:00:00 2001 From: Nathan Hendler Date: Sun, 31 Jul 2022 11:40:44 -0700 Subject: [PATCH 2467/3533] docs/library/rp2: Fix pull_thresh docs to use pull instead of push. --- docs/library/rp2.StateMachine.rst | 4 ++-- docs/library/rp2.rst | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/library/rp2.StateMachine.rst b/docs/library/rp2.StateMachine.rst index 8d73ccf772a7f..7e38ba8b1a754 100644 --- a/docs/library/rp2.StateMachine.rst +++ b/docs/library/rp2.StateMachine.rst @@ -55,8 +55,8 @@ Methods `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. - *push_thresh* is the threshold in bits before auto-push or conditional re-pushing is triggered. - - *pull_thresh* is the threshold in bits before auto-push or conditional - re-pushing is triggered. + - *pull_thresh* is the threshold in bits before auto-pull or conditional + re-pulling is triggered. .. method:: StateMachine.active([value]) diff --git a/docs/library/rp2.rst b/docs/library/rp2.rst index 06affaae8cac5..ec7666d7cef4b 100644 --- a/docs/library/rp2.rst +++ b/docs/library/rp2.rst @@ -47,8 +47,8 @@ For running PIO programs, see :class:`rp2.StateMachine`. `PIO.SHIFT_LEFT` or `PIO.SHIFT_RIGHT`. - *push_thresh* is the threshold in bits before auto-push or conditional re-pushing is triggered. - - *pull_thresh* is the threshold in bits before auto-push or conditional - re-pushing is triggered. + - *pull_thresh* is the threshold in bits before auto-pull or conditional + re-pulling is triggered. The remaining parameters are: From bfc63a5c844cd9efa25a44b418be201984fa231c Mon Sep 17 00:00:00 2001 From: Kyuchumimo <74131798+Kyuchumimo@users.noreply.github.com> Date: Tue, 9 Aug 2022 18:30:21 -0600 Subject: [PATCH 2468/3533] drivers/sdcard: Add delay in init_card_v1 to make timeout work. This now follows how init_card_v2 works. --- drivers/sdcard/sdcard.py | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/sdcard/sdcard.py b/drivers/sdcard/sdcard.py index f4520acbb5379..df28bd9534482 100644 --- a/drivers/sdcard/sdcard.py +++ b/drivers/sdcard/sdcard.py @@ -118,6 +118,7 @@ def init_card(self, baudrate): def init_card_v1(self): for i in range(_CMD_TIMEOUT): + time.sleep_ms(50) self.cmd(55, 0, 0) if self.cmd(41, 0, 0) == 0: # SDSC card, uses byte addressing in read/write/erase commands From 94a19f10628f9c3a9978daae9f6f4c7ac352e9a2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 11 Aug 2022 16:53:42 +1000 Subject: [PATCH 2469/3533] windows/Makefile: Update test dependency on $(PROG). PR #9012 (b2e82402688b53829f37475583231b067b9faea7) changed the output to $(BUILD)/$(PROG) but the tests are still looking for $(PROG). Also remove the now-unnecessary override of $(PROG) in the standard variant. Signed-off-by: Jim Mussared --- ports/windows/Makefile | 7 +++---- ports/windows/variants/standard/mpconfigvariant.mk | 2 -- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ports/windows/Makefile b/ports/windows/Makefile index 45d61b0b50ac0..1e793800f804b 100644 --- a/ports/windows/Makefile +++ b/ports/windows/Makefile @@ -16,7 +16,6 @@ include $(VARIANT_DIR)/mpconfigvariant.mk FROZEN_MANIFEST ?= variants/manifest.py # Define main target -# This should be configured by the mpconfigvariant.mk PROG ?= micropython # qstr definitions (must come before including py.mk) @@ -98,10 +97,10 @@ include $(TOP)/py/mkrules.mk RUN_TESTS_SKIP += -e math_fun -e float2int_double -e float_parse -e math_domain_special -test: $(PROG) $(TOP)/tests/run-tests.py +test: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) $(PYTHON) ./run-tests.py $(RUN_TESTS_SKIP) + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) $(PYTHON) ./run-tests.py $(RUN_TESTS_SKIP) test_full: test $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) $(PYTHON) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) $(RUN_TESTS_SKIP) -d basics float micropython + cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) $(PYTHON) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) $(RUN_TESTS_SKIP) -d basics float micropython diff --git a/ports/windows/variants/standard/mpconfigvariant.mk b/ports/windows/variants/standard/mpconfigvariant.mk index 9e1f5ae059cef..a0d6712c55312 100644 --- a/ports/windows/variants/standard/mpconfigvariant.mk +++ b/ports/windows/variants/standard/mpconfigvariant.mk @@ -1,3 +1 @@ # This is the default variant when you `make` the Windows port. - -PROG ?= micropython From 82b3500724206f2baa342a3559bbe716e9819426 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Aug 2022 16:34:02 +1000 Subject: [PATCH 2470/3533] py/qstr: Change qstr hash type from mp_uint_t to size_t. The hash is either 8 or 16 bits (depending on MICROPY_QSTR_BYTES_IN_HASH) so will fit in a size_t. This saves 268 bytes on the unix nanbox build. Non-nanbox configurations are unchanged because mp_uint_t is the same size as size_t. Signed-off-by: Damien George --- py/objstr.h | 4 ++-- py/qstr.c | 8 ++++---- py/qstr.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/py/objstr.h b/py/objstr.h index 8f87c3018a3c9..92b065d869b45 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -30,7 +30,7 @@ typedef struct _mp_obj_str_t { mp_obj_base_t base; - mp_uint_t hash; + size_t hash; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte size_t len; const byte *data; @@ -41,7 +41,7 @@ typedef struct _mp_obj_str_t { // use this macro to extract the string hash // warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data #define GET_STR_HASH(str_obj_in, str_hash) \ - mp_uint_t str_hash; \ + size_t str_hash; \ if (mp_obj_is_qstr(str_obj_in)) { \ str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); \ } else { \ diff --git a/py/qstr.c b/py/qstr.c index f9ca106837490..ea700566f4f7d 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -60,9 +60,9 @@ #define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10) // this must match the equivalent function in makeqstrdata.py -mp_uint_t qstr_compute_hash(const byte *data, size_t len) { +size_t qstr_compute_hash(const byte *data, size_t len) { // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html - mp_uint_t hash = 5381; + size_t hash = 5381; for (const byte *top = data + len; data < top; data++) { hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data } @@ -181,7 +181,7 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) { qstr qstr_find_strn(const char *str, size_t str_len) { // work out hash of str - mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len); + size_t str_hash = qstr_compute_hash((const byte *)str, str_len); // search pools for the data for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { @@ -254,7 +254,7 @@ qstr qstr_from_strn(const char *str, size_t len) { MP_STATE_VM(qstr_last_used) += n_bytes; // store the interned strings' data - mp_uint_t hash = qstr_compute_hash((const byte *)str, len); + size_t hash = qstr_compute_hash((const byte *)str, len); memcpy(q_ptr, str, len); q_ptr[len] = '\0'; q = qstr_add(hash, len, q_ptr); diff --git a/py/qstr.h b/py/qstr.h index a463b67a1836c..0ef861f33e8d5 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -78,7 +78,7 @@ typedef struct _qstr_pool_t { void qstr_init(void); -mp_uint_t qstr_compute_hash(const byte *data, size_t len); +size_t qstr_compute_hash(const byte *data, size_t len); qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTRnull if not found qstr qstr_from_str(const char *str); From f7f56d42851aaff2027e23a8ca45c1f1973f1aca Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 10 Aug 2022 14:13:17 +1000 Subject: [PATCH 2471/3533] py/objstr: Consolidate methods for str/bytes/bytearray/array. This commit adds the bytes methods to bytearray, matching CPython. The existing implementations of these methods for str/bytes are reused for bytearray with minor updates to match CPython return types. For details on the CPython behaviour see https://docs.python.org/3/library/stdtypes.html#bytes-and-bytearray-operations The work to merge locals tables for str/bytes/bytearray/array was done by @jimmo. Because of this merging of locals the change in code size for this commit is mostly negative: bare-arm: +0 +0.000% minimal x86: +29 +0.018% unix x64: -792 -0.128% standard[incl -448(data)] unix nanbox: -436 -0.078% nanbox[incl -448(data)] stm32: -40 -0.010% PYBV10 cc3200: -32 -0.017% esp8266: -28 -0.004% GENERIC esp32: -72 -0.005% GENERIC[incl -200(data)] mimxrt: -40 -0.011% TEENSY40 renesas-ra: -40 -0.006% RA6M2_EK nrf: -16 -0.009% pca10040 rp2: -64 -0.013% PICO samd: +148 +0.105% ADAFRUIT_ITSYBITSY_M4_EXPRESS --- py/obj.h | 10 +- py/objarray.c | 22 +-- py/objarray.h | 5 + py/objstr.c | 157 ++++++++++++++++------ py/objstr.h | 19 +++ py/objstrunicode.c | 50 +------ tests/basics/bytearray_byte_operations.py | 35 +++++ tests/basics/bytearray_center.py | 8 ++ tests/basics/bytearray_count.py | 7 + tests/basics/bytearray_partition.py | 8 ++ tests/basics/bytes_center.py | 13 ++ 11 files changed, 225 insertions(+), 109 deletions(-) create mode 100644 tests/basics/bytearray_byte_operations.py create mode 100644 tests/basics/bytearray_center.py create mode 100644 tests/basics/bytearray_count.py create mode 100644 tests/basics/bytearray_partition.py create mode 100644 tests/basics/bytes_center.py diff --git a/py/obj.h b/py/obj.h index 7ba4c0a5f2892..598d6508da737 100644 --- a/py/obj.h +++ b/py/obj.h @@ -394,19 +394,21 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t; .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \ } -#define MP_DEFINE_CONST_DICT(dict_name, table_name) \ +#define MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, n) \ const mp_obj_dict_t dict_name = { \ .base = {&mp_type_dict}, \ .map = { \ .all_keys_are_qstrs = 1, \ .is_fixed = 1, \ .is_ordered = 1, \ - .used = MP_ARRAY_SIZE(table_name), \ - .alloc = MP_ARRAY_SIZE(table_name), \ + .used = n, \ + .alloc = n, \ .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \ }, \ } +#define MP_DEFINE_CONST_DICT(dict_name, table_name) MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, MP_ARRAY_SIZE(table_name)) + // These macros are used to declare and define constant staticmethond and classmethod objects // You can put "static" in front of the definitions to make them local @@ -789,7 +791,7 @@ mp_obj_t mp_obj_new_str(const char *data, size_t len); mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr); mp_obj_t mp_obj_new_bytes(const byte *data, size_t len); -mp_obj_t mp_obj_new_bytearray(size_t n, void *items); +mp_obj_t mp_obj_new_bytearray(size_t n, const void *items); mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items); #if MICROPY_PY_BUILTINS_FLOAT mp_obj_t mp_obj_new_int_from_float(mp_float_t val); diff --git a/py/objarray.c b/py/objarray.c index bff3126a2bc41..11bd7bb15c41c 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -381,7 +381,7 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) { self->free--; return mp_const_none; // return None, as per CPython } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append); +MP_DEFINE_CONST_FUN_OBJ_2(mp_obj_array_append_obj, array_append); STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) { // self is not a memoryview, so we don't need to use (& TYPECODE_MASK) @@ -413,7 +413,7 @@ STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) { return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend); +MP_DEFINE_CONST_FUN_OBJ_2(mp_obj_array_extend_obj, array_extend); #endif STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { @@ -564,18 +564,6 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui return 0; } -#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY -STATIC const mp_rom_map_elem_t array_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) }, - { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) }, - #if MICROPY_CPYTHON_COMPAT - { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) }, - #endif -}; - -STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table); -#endif - #if MICROPY_PY_ARRAY const mp_obj_type_t mp_type_array = { { &mp_type_type }, @@ -587,7 +575,7 @@ const mp_obj_type_t mp_type_array = { .binary_op = array_binary_op, .subscr = array_subscr, .buffer_p = { .get_buffer = array_get_buffer }, - .locals_dict = (mp_obj_dict_t *)&array_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_obj_array_locals_dict, }; #endif @@ -603,7 +591,7 @@ const mp_obj_type_t mp_type_bytearray = { .binary_op = array_binary_op, .subscr = array_subscr, .buffer_p = { .get_buffer = array_get_buffer }, - .locals_dict = (mp_obj_dict_t *)&array_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict, }; #endif @@ -631,7 +619,7 @@ size_t mp_obj_array_len(mp_obj_t self_in) { */ #if MICROPY_PY_BUILTINS_BYTEARRAY -mp_obj_t mp_obj_new_bytearray(size_t n, void *items) { +mp_obj_t mp_obj_new_bytearray(size_t n, const void *items) { mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n); memcpy(o->items, items, n); return MP_OBJ_FROM_PTR(o); diff --git a/py/objarray.h b/py/objarray.h index 94c31c969386b..48a26c3fb301e 100644 --- a/py/objarray.h +++ b/py/objarray.h @@ -59,4 +59,9 @@ static inline void mp_obj_memoryview_init(mp_obj_array_t *self, size_t typecode, } #endif +#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY +MP_DECLARE_CONST_FUN_OBJ_2(mp_obj_array_append_obj); +MP_DECLARE_CONST_FUN_OBJ_2(mp_obj_array_extend_obj); +#endif + #endif // MICROPY_INCLUDED_PY_OBJARRAY_H diff --git a/py/objstr.c b/py/objstr.c index f15a2d974458b..162229c62b2b3 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -41,6 +41,26 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in); +STATIC void str_check_arg_type(const mp_obj_type_t *self_type, const mp_obj_t arg) { + // String operations generally need the args type to match the object they're called on, + // e.g. str.find(str), byte.startswith(byte) + // with the exception that bytes may be used for bytearray and vice versa. + const mp_obj_type_t *arg_type = mp_obj_get_type(arg); + + #if MICROPY_PY_BUILTINS_BYTEARRAY + if (arg_type == &mp_type_bytearray) { + arg_type = &mp_type_bytes; + } + if (self_type == &mp_type_bytearray) { + self_type = &mp_type_bytes; + } + #endif + + if (arg_type != self_type) { + bad_implicit_conversion(arg); + } +} + /******************************************************************************/ /* str */ @@ -452,6 +472,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { mp_check_self(mp_obj_is_str_or_bytes(self_in)); const mp_obj_type_t *self_type = mp_obj_get_type(self_in); + const mp_obj_type_t *ret_type = self_type; // get separation string GET_STR_DATA_LEN(self_in, sep_str, sep_len); @@ -469,8 +490,19 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { // count required length size_t required_len = 0; + #if MICROPY_PY_BUILTINS_BYTEARRAY + if (self_type == &mp_type_bytearray) { + self_type = &mp_type_bytes; + } + #endif for (size_t i = 0; i < seq_len; i++) { - if (mp_obj_get_type(seq_items[i]) != self_type) { + const mp_obj_type_t *seq_type = mp_obj_get_type(seq_items[i]); + #if MICROPY_PY_BUILTINS_BYTEARRAY + if (seq_type == &mp_type_bytearray) { + seq_type = &mp_type_bytes; + } + #endif + if (seq_type != self_type) { mp_raise_TypeError( MP_ERROR_TEXT("join expects a list of str/bytes objects consistent with self object")); } @@ -496,7 +528,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { } // return joined string - return mp_obj_new_str_from_vstr(self_type, &vstr); + return mp_obj_new_str_from_vstr(ret_type, &vstr); } MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join); @@ -545,9 +577,7 @@ mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args) { } else { // sep given - if (mp_obj_get_type(sep) != self_type) { - bad_implicit_conversion(sep); - } + str_check_arg_type(self_type, sep); size_t sep_len; const char *sep_str = mp_obj_str_get_data(sep, &sep_len); @@ -699,9 +729,7 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b mp_check_self(mp_obj_is_str_or_bytes(args[0])); // check argument type - if (mp_obj_get_type(args[1]) != self_type) { - bad_implicit_conversion(args[1]); - } + str_check_arg_type(self_type, args[1]); GET_STR_DATA_LEN(args[0], haystack, haystack_len); GET_STR_DATA_LEN(args[1], needle, needle_len); @@ -805,9 +833,7 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) { chars_to_del = whitespace; chars_to_del_len = sizeof(whitespace) - 1; } else { - if (mp_obj_get_type(args[1]) != self_type) { - bad_implicit_conversion(args[1]); - } + str_check_arg_type(self_type, args[1]); GET_STR_DATA_LEN(args[1], s, l); chars_to_del = s; chars_to_del_len = l; @@ -1633,13 +1659,8 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) { const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); - if (mp_obj_get_type(args[1]) != self_type) { - bad_implicit_conversion(args[1]); - } - - if (mp_obj_get_type(args[2]) != self_type) { - bad_implicit_conversion(args[2]); - } + str_check_arg_type(self_type, args[1]); + str_check_arg_type(self_type, args[2]); // extract string data @@ -1726,9 +1747,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) { mp_check_self(mp_obj_is_str_or_bytes(args[0])); // check argument type - if (mp_obj_get_type(args[1]) != self_type) { - bad_implicit_conversion(args[1]); - } + str_check_arg_type(self_type, args[1]); GET_STR_DATA_LEN(args[0], haystack, haystack_len); GET_STR_DATA_LEN(args[1], needle, needle_len); @@ -1767,9 +1786,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) { mp_check_self(mp_obj_is_str_or_bytes(self_in)); const mp_obj_type_t *self_type = mp_obj_get_type(self_in); - if (self_type != mp_obj_get_type(arg)) { - bad_implicit_conversion(arg); - } + str_check_arg_type(self_type, arg); GET_STR_DATA_LEN(self_in, str, str_len); GET_STR_DATA_LEN(arg, sep, sep_len); @@ -1795,6 +1812,12 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) { result[2] = self_in; } + #if MICROPY_PY_BUILTINS_BYTEARRAY + if (mp_obj_get_type(arg) != self_type) { + arg = mp_obj_new_str_of_type(self_type, sep, sep_len); + } + #endif + const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction); if (position_ptr != NULL) { size_t position = position_ptr - str; @@ -1940,17 +1963,15 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u } } -STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = { +// This locals table is used for the following types: str, bytes, bytearray, array.array. +// Each type takes a different section (start to end offset) of this table. +STATIC const mp_rom_map_elem_t array_bytearray_str_bytes_locals_table[] = { + #if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY + { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&mp_obj_array_append_obj) }, + { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&mp_obj_array_extend_obj) }, + #endif #if MICROPY_CPYTHON_COMPAT { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) }, - #if !MICROPY_PY_BUILTINS_STR_UNICODE - // If we have separate unicode type, then here we have methods only - // for bytes type, and it should not have encode() methods. Otherwise, - // we have non-compliant-but-practical bytestring type, which shares - // method table with bytes, so they both have encode() and decode() - // methods (which should do type checking at runtime). - { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) }, - #endif #endif { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&str_find_obj) }, { MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&str_rfind_obj) }, @@ -1986,9 +2007,46 @@ STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_isdigit), MP_ROM_PTR(&str_isdigit_obj) }, { MP_ROM_QSTR(MP_QSTR_isupper), MP_ROM_PTR(&str_isupper_obj) }, { MP_ROM_QSTR(MP_QSTR_islower), MP_ROM_PTR(&str_islower_obj) }, + #if MICROPY_CPYTHON_COMPAT + { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) }, + #endif }; -STATIC MP_DEFINE_CONST_DICT(str8_locals_dict, str8_locals_dict_table); +#if MICROPY_CPYTHON_COMPAT +#define TABLE_ENTRIES_COMPAT 1 +#else +#define TABLE_ENTRIES_COMPAT 0 +#endif + +#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY +#define TABLE_ENTRIES_ARRAY 2 +#else +#define TABLE_ENTRIES_ARRAY 0 +#endif + +MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_str_locals_dict, + array_bytearray_str_bytes_locals_table + TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_COMPAT, + MP_ARRAY_SIZE(array_bytearray_str_bytes_locals_table) - (TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_COMPAT)); + +#if TABLE_ENTRIES_COMPAT == 0 +#define mp_obj_bytes_locals_dict mp_obj_str_locals_dict +#else +MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_bytes_locals_dict, + array_bytearray_str_bytes_locals_table + TABLE_ENTRIES_ARRAY, + MP_ARRAY_SIZE(array_bytearray_str_bytes_locals_table) - (TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_COMPAT)); +#endif + +#if MICROPY_PY_BUILTINS_BYTEARRAY +MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_bytearray_locals_dict, + array_bytearray_str_bytes_locals_table, + MP_ARRAY_SIZE(array_bytearray_str_bytes_locals_table) - TABLE_ENTRIES_COMPAT); +#endif + +#if MICROPY_PY_ARRAY +MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_array_locals_dict, + array_bytearray_str_bytes_locals_table, + TABLE_ENTRIES_ARRAY); +#endif #if !MICROPY_PY_BUILTINS_STR_UNICODE STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); @@ -2002,9 +2060,9 @@ const mp_obj_type_t mp_type_str = { .subscr = bytes_subscr, .getiter = mp_obj_new_str_iterator, .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, - .locals_dict = (mp_obj_dict_t *)&str8_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, }; -#endif +#endif // !MICROPY_PY_BUILTINS_STR_UNICODE // Reuses most of methods from str const mp_obj_type_t mp_type_bytes = { @@ -2016,7 +2074,7 @@ const mp_obj_type_t mp_type_bytes = { .subscr = bytes_subscr, .getiter = mp_obj_new_bytes_iterator, .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, - .locals_dict = (mp_obj_dict_t *)&str8_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_obj_bytes_locals_dict, }; // The zero-length bytes object, with data that includes a null-terminating byte @@ -2044,6 +2102,10 @@ mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte *data, size_t mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len) { if (type == &mp_type_str) { return mp_obj_new_str((const char *)data, len); + #if MICROPY_PY_BUILTINS_BYTEARRAY + } else if (type == &mp_type_bytearray) { + return mp_obj_new_bytearray(len, data); + #endif } else { return mp_obj_new_bytes(data, len); } @@ -2068,18 +2130,24 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) { } } - // make a new str/bytes object - mp_obj_str_t *o = mp_obj_malloc(mp_obj_str_t, type); - o->len = vstr->len; - o->hash = qstr_compute_hash((byte *)vstr->buf, vstr->len); + byte *data; if (vstr->len + 1 == vstr->alloc) { - o->data = (byte *)vstr->buf; + data = (byte *)vstr->buf; } else { - o->data = (byte *)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1); + data = (byte *)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1); } - ((byte *)o->data)[o->len] = '\0'; // add null byte + data[vstr->len] = '\0'; // add null byte vstr->buf = NULL; vstr->alloc = 0; + #if MICROPY_PY_BUILTINS_BYTEARRAY + if (type == &mp_type_bytearray) { + return mp_obj_new_bytearray_by_ref(vstr->len, data); + } + #endif + mp_obj_str_t *o = mp_obj_malloc(mp_obj_str_t, type); + o->len = vstr->len; + o->hash = qstr_compute_hash(data, vstr->len); + o->data = data; return MP_OBJ_FROM_PTR(o); } @@ -2179,6 +2247,7 @@ const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len) { if (mp_obj_is_qstr(self_in)) { return qstr_data(MP_OBJ_QSTR_VALUE(self_in), len); } else { + MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE; *len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(self_in))->len; return ((mp_obj_str_t *)MP_OBJ_TO_PTR(self_in))->data; } diff --git a/py/objstr.h b/py/objstr.h index 92b065d869b45..7d86ec30baab7 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -27,6 +27,7 @@ #define MICROPY_INCLUDED_PY_OBJSTR_H #include "py/obj.h" +#include "py/objarray.h" typedef struct _mp_obj_str_t { mp_obj_base_t base; @@ -36,6 +37,13 @@ typedef struct _mp_obj_str_t { const byte *data; } mp_obj_str_t; +// This static assert is used to ensure that mp_obj_str_t and mp_obj_array_t are compatible, +// meaning that their len and data/items entries are at the same offsets in the struct. +// This allows the same code to be used for str/bytes and bytearray. +#define MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE \ + MP_STATIC_ASSERT(offsetof(mp_obj_str_t, len) == offsetof(mp_obj_array_t, len) \ + && offsetof(mp_obj_str_t, data) == offsetof(mp_obj_array_t, items)) + #define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte *)str} // use this macro to extract the string hash @@ -70,6 +78,7 @@ const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len); if (mp_obj_is_qstr(str_obj_in)) { \ str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); \ } else { \ + MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE; \ str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \ str_data = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->data; \ } @@ -118,4 +127,14 @@ MP_DECLARE_CONST_FUN_OBJ_1(str_isupper_obj); MP_DECLARE_CONST_FUN_OBJ_1(str_islower_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj); +extern const mp_obj_dict_t mp_obj_str_locals_dict; + +#if MICROPY_PY_BUILTINS_BYTEARRAY +extern const mp_obj_dict_t mp_obj_bytearray_locals_dict; +#endif + +#if MICROPY_PY_ARRAY +extern const mp_obj_dict_t mp_obj_array_locals_dict; +#endif + #endif // MICROPY_INCLUDED_PY_OBJSTR_H diff --git a/py/objstrunicode.c b/py/objstrunicode.c index ed79ad68a93c6..d36dd8b1375a2 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -116,7 +116,11 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s mp_obj_t index, bool is_slice) { // All str functions also handle bytes objects, and they call str_index_to_ptr(), // so it must handle bytes. - if (type == &mp_type_bytes) { + if (type == &mp_type_bytes + #if MICROPY_PY_BUILTINS_BYTEARRAY + || type == &mp_type_bytearray + #endif + ) { // Taken from objstr.c:str_index_to_ptr() size_t index_val = mp_get_index(type, self_len, index, is_slice); return self_data + index_val; @@ -225,48 +229,6 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } } -STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = { - #if MICROPY_CPYTHON_COMPAT - { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&str_encode_obj) }, - #endif - { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&str_find_obj) }, - { MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&str_rfind_obj) }, - { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&str_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_rindex), MP_ROM_PTR(&str_rindex_obj) }, - { MP_ROM_QSTR(MP_QSTR_join), MP_ROM_PTR(&str_join_obj) }, - { MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&str_split_obj) }, - #if MICROPY_PY_BUILTINS_STR_SPLITLINES - { MP_ROM_QSTR(MP_QSTR_splitlines), MP_ROM_PTR(&str_splitlines_obj) }, - #endif - { MP_ROM_QSTR(MP_QSTR_rsplit), MP_ROM_PTR(&str_rsplit_obj) }, - { MP_ROM_QSTR(MP_QSTR_startswith), MP_ROM_PTR(&str_startswith_obj) }, - { MP_ROM_QSTR(MP_QSTR_endswith), MP_ROM_PTR(&str_endswith_obj) }, - { MP_ROM_QSTR(MP_QSTR_strip), MP_ROM_PTR(&str_strip_obj) }, - { MP_ROM_QSTR(MP_QSTR_lstrip), MP_ROM_PTR(&str_lstrip_obj) }, - { MP_ROM_QSTR(MP_QSTR_rstrip), MP_ROM_PTR(&str_rstrip_obj) }, - { MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) }, - { MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) }, - #if MICROPY_PY_BUILTINS_STR_COUNT - { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) }, - #endif - #if MICROPY_PY_BUILTINS_STR_PARTITION - { MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) }, - { MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) }, - #endif - #if MICROPY_PY_BUILTINS_STR_CENTER - { MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) }, - #endif - { MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) }, - { MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) }, - { MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) }, - { MP_ROM_QSTR(MP_QSTR_isalpha), MP_ROM_PTR(&str_isalpha_obj) }, - { MP_ROM_QSTR(MP_QSTR_isdigit), MP_ROM_PTR(&str_isdigit_obj) }, - { MP_ROM_QSTR(MP_QSTR_isupper), MP_ROM_PTR(&str_isupper_obj) }, - { MP_ROM_QSTR(MP_QSTR_islower), MP_ROM_PTR(&str_islower_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(struni_locals_dict, struni_locals_dict_table); - const mp_obj_type_t mp_type_str = { { &mp_type_type }, .name = MP_QSTR_str, @@ -277,7 +239,7 @@ const mp_obj_type_t mp_type_str = { .subscr = str_subscr, .getiter = mp_obj_new_str_iterator, .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, - .locals_dict = (mp_obj_dict_t *)&struni_locals_dict, + .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, }; /******************************************************************************/ diff --git a/tests/basics/bytearray_byte_operations.py b/tests/basics/bytearray_byte_operations.py new file mode 100644 index 0000000000000..7ce53cac535a1 --- /dev/null +++ b/tests/basics/bytearray_byte_operations.py @@ -0,0 +1,35 @@ +# test bytearray with its re-use of byte functions + +print(bytearray(b"hello world").find(b"ll")) +print(bytearray(b"hello\x00world").rfind(b"l")) + +print(bytearray(b"abc efg ").strip(b"g a")) +print(bytearray(b" spacious ").lstrip()) +print(bytearray(b"www.example.com").lstrip(b"cmowz.")) +print(bytearray(b" spacious ").rstrip()) +print(bytearray(b"mississippi").rstrip(b"ipz")) + +print(bytearray(b"abc").split(b"a")) +print(bytearray(b"abcabc").rsplit(b"bc")) + +print(bytearray(b"asdfasdf").replace(b"a", b"b")) + +print("00\x0000".index("0", 0)) +print("00\x0000".index("0", 3)) +print("00\x0000".rindex("0", 0)) +print("00\x0000".rindex("0", 3)) + +print(bytearray(b"foobar").endswith(b"bar")) +print(bytearray(b"1foo").startswith(b"foo", 1)) + +print(bytearray(b" T E \x00 S T").lower()) +print(bytearray(b" te \x00 st").upper()) + +print(bytearray(b" \t\n\r\v\f").isspace()) +print(bytearray(b"this ").isalpha()) +print(bytearray(b"0123456789").isdigit()) +print(bytearray(b"AB").isupper()) +print(bytearray(b"cheese-cake").islower()) + +print(bytearray(b",").join((bytearray(b"abc"), bytearray(b"def")))) +print(type(bytearray(b",").join((b"a", b"b", b"c")))) diff --git a/tests/basics/bytearray_center.py b/tests/basics/bytearray_center.py new file mode 100644 index 0000000000000..c262a8c2097b3 --- /dev/null +++ b/tests/basics/bytearray_center.py @@ -0,0 +1,8 @@ +try: + bytearray.center +except AttributeError: + print("SKIP") + raise SystemExit + +print(bytearray(b"foo").center(6)) +print(type(bytearray(b"foo").center(6))) diff --git a/tests/basics/bytearray_count.py b/tests/basics/bytearray_count.py new file mode 100644 index 0000000000000..a151c1e8183cd --- /dev/null +++ b/tests/basics/bytearray_count.py @@ -0,0 +1,7 @@ +try: + bytearray.count +except AttributeError: + print("SKIP") + raise SystemExit + +print(bytearray(b"aaaa").count(b"a")) diff --git a/tests/basics/bytearray_partition.py b/tests/basics/bytearray_partition.py new file mode 100644 index 0000000000000..b48ec4ed9b9f9 --- /dev/null +++ b/tests/basics/bytearray_partition.py @@ -0,0 +1,8 @@ +try: + bytearray.partition +except AttributeError: + print("SKIP") + raise SystemExit + +print(bytearray(b"asdsf").partition(b"s")) +print(bytearray(b"asdsf").rpartition(b"s")) diff --git a/tests/basics/bytes_center.py b/tests/basics/bytes_center.py new file mode 100644 index 0000000000000..5d8b41d92ff2c --- /dev/null +++ b/tests/basics/bytes_center.py @@ -0,0 +1,13 @@ +try: + bytes.center +except: + print("SKIP") + raise SystemExit + +print(b"foo".center(0)) +print(b"foo".center(1)) +print(b"foo".center(3)) +print(b"foo".center(4)) +print(b"foo".center(5)) +print(b"foo".center(6)) +print(b"foo".center(20)) From 787bd9991903dc736b5eb79826dd34e4c17f72ec Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Aug 2022 21:43:09 +1000 Subject: [PATCH 2472/3533] nrf/modules/ubluepy: Use mp_obj_str_get_data to extract str data. Instead of GET_STR_DATA_LEN, which is intended to be a private macro. Signed-off-by: Damien George --- ports/nrf/modules/ubluepy/ubluepy_peripheral.c | 8 ++++---- ports/nrf/modules/ubluepy/ubluepy_uuid.c | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c index 05a72b1de63d9..acfe316c0cf74 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c +++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c @@ -193,9 +193,8 @@ STATIC mp_obj_t peripheral_advertise(mp_uint_t n_args, const mp_obj_t *pos_args, memset(&adv_data, 0, sizeof(ubluepy_advertise_data_t)); if (device_name_obj != mp_const_none && mp_obj_is_str(device_name_obj)) { - GET_STR_DATA_LEN(device_name_obj, str_data, str_len); - - adv_data.p_device_name = (uint8_t *)str_data; + size_t str_len; + adv_data.p_device_name = (uint8_t *)mp_obj_str_get_data(device_name_obj, &str_len); adv_data.device_name_len = str_len; } @@ -357,7 +356,8 @@ STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, m ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler); if (mp_obj_is_str(dev_addr)) { - GET_STR_DATA_LEN(dev_addr, str_data, str_len); + size_t str_len; + const byte *str_data = (const byte *)mp_obj_str_get_data(dev_addr, &str_len); if (str_len == 17) { // Example "11:22:33:aa:bb:cc" uint8_t * p_addr = m_new(uint8_t, 6); diff --git a/ports/nrf/modules/ubluepy/ubluepy_uuid.c b/ports/nrf/modules/ubluepy/ubluepy_uuid.c index 594d0d9130c3e..aee7b9a1a899c 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_uuid.c +++ b/ports/nrf/modules/ubluepy/ubluepy_uuid.c @@ -70,7 +70,8 @@ STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args, s->value[1] = (((uint16_t)mp_obj_get_int(uuid_obj)) >> 8) & 0xFF; s->value[0] = ((uint8_t)mp_obj_get_int(uuid_obj)) & 0xFF; } else if (mp_obj_is_str(uuid_obj)) { - GET_STR_DATA_LEN(uuid_obj, str_data, str_len); + size_t str_len; + const byte *str_data = (const byte *)mp_obj_str_get_data(uuid_obj, &str_len); if (str_len == 6) { // Assume hex digit prefixed with 0x s->type = UBLUEPY_UUID_16_BIT; s->value[0] = unichar_xdigit_value(str_data[5]); From 6c67fbc280625c59fff7cdf93f16d5e2ad0bad8a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Aug 2022 21:44:53 +1000 Subject: [PATCH 2473/3533] zephyr/machine_uart: Use mp_obj_str_get_str to get device name. This checks that the argument is actually a string. Signed-off-by: Damien George --- ports/zephyr/machine_uart.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 47ca0945a5e6f..3520795c5aad4 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -75,10 +75,9 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - GET_STR_DATA_LEN(args[0], name, name_len); machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type); - self->dev = device_get_binding(name); + self->dev = device_get_binding(mp_obj_str_get_str(args[0])); if (!self->dev) { mp_raise_ValueError(MP_ERROR_TEXT("Bad device name")); } From 28aaab95909aab092cc8c16188fec157142f18a9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Jul 2021 18:01:12 +1000 Subject: [PATCH 2474/3533] py/objstr: Add hex/fromhex to bytes/memoryview/bytearray. These were added in Python 3.5. Enabled via MICROPY_PY_BUILTINS_BYTES_HEX, and enabled by default for all ports that currently have ubinascii. Rework ubinascii to use the implementation of these methods. Signed-off-by: Jim Mussared --- extmod/modubinascii.c | 81 +++++---------------------- ports/cc3200/mpconfigport.h | 1 + ports/javascript/mpconfigport.h | 1 + ports/mimxrt/mpconfigport.h | 1 + ports/qemu-arm/mpconfigport.h | 1 + ports/samd/mpconfigport.h | 1 + ports/unix/mpconfigport.h | 1 + ports/windows/mpconfigport.h | 1 + ports/zephyr/mpconfigport.h | 1 + py/mpconfig.h | 5 ++ py/objarray.c | 9 +++ py/objstr.c | 98 ++++++++++++++++++++++++++++++++- py/objstr.h | 7 +++ 13 files changed, 138 insertions(+), 70 deletions(-) diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index c0e2c587fdd9d..49c8d1fb971e5 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -30,78 +30,21 @@ #include "py/runtime.h" #include "py/binary.h" +#include "py/objstr.h" #if MICROPY_PY_UBINASCII -STATIC mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) { - // First argument is the data to convert. - // Second argument is an optional separator to be used between values. - const char *sep = NULL; - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); - - // Code below assumes non-zero buffer length when computing size with - // separator, so handle the zero-length case here. - if (bufinfo.len == 0) { - return mp_const_empty_bytes; - } - - vstr_t vstr; - size_t out_len = bufinfo.len * 2; - if (n_args > 1) { - // 1-char separator between hex numbers - out_len += bufinfo.len - 1; - sep = mp_obj_str_get_str(args[1]); - } - vstr_init_len(&vstr, out_len); - byte *in = bufinfo.buf, *out = (byte *)vstr.buf; - for (mp_uint_t i = bufinfo.len; i--;) { - byte d = (*in >> 4); - if (d > 9) { - d += 'a' - '9' - 1; - } - *out++ = d + '0'; - d = (*in++ & 0xf); - if (d > 9) { - d += 'a' - '9' - 1; - } - *out++ = d + '0'; - if (sep != NULL && i != 0) { - *out++ = *sep; - } - } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +#if MICROPY_PY_BUILTINS_BYTES_HEX +STATIC mp_obj_t bytes_hex_as_bytes(size_t n_args, const mp_obj_t *args) { + return mp_obj_bytes_hex(n_args, args, &mp_type_bytes); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_hex_as_bytes_obj, 1, 2, bytes_hex_as_bytes); -STATIC mp_obj_t mod_binascii_unhexlify(mp_obj_t data) { - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); - - if ((bufinfo.len & 1) != 0) { - mp_raise_ValueError(MP_ERROR_TEXT("odd-length string")); - } - vstr_t vstr; - vstr_init_len(&vstr, bufinfo.len / 2); - byte *in = bufinfo.buf, *out = (byte *)vstr.buf; - byte hex_byte = 0; - for (mp_uint_t i = bufinfo.len; i--;) { - byte hex_ch = *in++; - if (unichar_isxdigit(hex_ch)) { - hex_byte += unichar_xdigit_value(hex_ch); - } else { - mp_raise_ValueError(MP_ERROR_TEXT("non-hex digit found")); - } - if (i & 1) { - hex_byte <<= 4; - } else { - *out++ = hex_byte; - hex_byte = 0; - } - } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); +STATIC mp_obj_t bytes_fromhex_bytes(mp_obj_t data) { + return mp_obj_bytes_fromhex(MP_OBJ_FROM_PTR(&mp_type_bytes), data); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bytes_fromhex_obj, bytes_fromhex_bytes); +#endif // If ch is a character in the base64 alphabet, and is not a pad character, then // the corresponding integer between 0 and 63, inclusively, is returned. @@ -242,8 +185,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_bin STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) }, - { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) }, - { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) }, + #if MICROPY_PY_BUILTINS_BYTES_HEX + { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&bytes_hex_as_bytes_obj) }, + { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&bytes_fromhex_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) }, { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) }, #if MICROPY_PY_UBINASCII_CRC32 diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index e81a2fc29bb3a..f41c1fe99dbfb 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -78,6 +78,7 @@ #define MICROPY_VFS_FAT (1) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT cc3200_help_text diff --git a/ports/javascript/mpconfigport.h b/ports/javascript/mpconfigport.h index 01a61e391d00b..6c86d816bd4fe 100644 --- a/ports/javascript/mpconfigport.h +++ b/ports/javascript/mpconfigport.h @@ -89,6 +89,7 @@ #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_REPL_AUTO_INDENT (1) #define MICROPY_PY_FUNCTION_ATTRS (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 00714ca8d0565..54d649795df0c 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -74,6 +74,7 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_DESCRIPTORS (1) #define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_FSTRINGS (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) diff --git a/ports/qemu-arm/mpconfigport.h b/ports/qemu-arm/mpconfigport.h index 8809d17154fd0..972dce61b4987 100644 --- a/ports/qemu-arm/mpconfigport.h +++ b/ports/qemu-arm/mpconfigport.h @@ -28,6 +28,7 @@ #define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) #define MICROPY_PY_BUILTINS_POW3 (1) diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 24b9cbfe19438..c02d9316ded30 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -56,6 +56,7 @@ #define MICROPY_MODULE_WEAK_LINKS (1) // Control over Python builtins #define MICROPY_PY_ASYNC_AWAIT (0) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_STR_COUNT (0) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) #define MICROPY_PY_BUILTINS_SET (0) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index ed4c71097ff16..5545a74f57cc5 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -61,6 +61,7 @@ #define MICROPY_PY_DESCRIPTORS (1) #define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_FSTRINGS (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 5f8ad983adc4a..4ed9f316e7bbf 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -84,6 +84,7 @@ #define MICROPY_PY_DESCRIPTORS (1) #define MICROPY_PY_DELATTR_SETATTR (1) #define MICROPY_PY_FSTRINGS (1) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) #define MICROPY_PY_BUILTINS_STR_PARTITION (1) diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 586b0ec916972..4c8096b44107a 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -45,6 +45,7 @@ #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_ATTRTUPLE (0) +#define MICROPY_PY_BUILTINS_BYTES_HEX (1) #define MICROPY_PY_BUILTINS_ENUMERATE (0) #define MICROPY_PY_BUILTINS_FILTER (0) #define MICROPY_PY_BUILTINS_MIN_MAX (0) diff --git a/py/mpconfig.h b/py/mpconfig.h index 25f37ff8b3a2c..1ed34bb6fdae0 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -987,6 +987,11 @@ typedef double mp_float_t; #define MICROPY_PY_STR_BYTES_CMP_WARN (0) #endif +// Add bytes.hex and bytes.fromhex +#ifndef MICROPY_PY_BUILTINS_BYTES_HEX +#define MICROPY_PY_BUILTINS_BYTES_HEX (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) +#endif + // Whether str object is proper unicode #ifndef MICROPY_PY_BUILTINS_STR_UNICODE #define MICROPY_PY_BUILTINS_STR_UNICODE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) diff --git a/py/objarray.c b/py/objarray.c index 11bd7bb15c41c..ecaffeb6a22e7 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -245,6 +245,12 @@ STATIC void memoryview_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in); dest[0] = MP_OBJ_NEW_SMALL_INT(mp_binary_get_size('@', self->typecode & TYPECODE_MASK, NULL)); } + #if MICROPY_PY_BUILTINS_BYTES_HEX + else { + // Need to forward to locals dict. + dest[1] = MP_OBJ_SENTINEL; + } + #endif } #endif @@ -607,6 +613,9 @@ const mp_obj_type_t mp_type_memoryview = { #if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE .attr = memoryview_attr, #endif + #if MICROPY_PY_BUILTINS_BYTES_HEX + .locals_dict = (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, + #endif .subscr = array_subscr, .buffer_p = { .get_buffer = array_get_buffer }, }; diff --git a/py/objstr.c b/py/objstr.c index 162229c62b2b3..45dbb9b3ebde3 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1950,6 +1950,84 @@ STATIC mp_obj_t str_encode(size_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj, 1, 3, str_encode); #endif +#if MICROPY_PY_BUILTINS_BYTES_HEX +mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type) { + // First argument is the data to convert. + // Second argument is an optional separator to be used between values. + const char *sep = NULL; + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); + + // Code below assumes non-zero buffer length when computing size with + // separator, so handle the zero-length case here. + if (bufinfo.len == 0) { + return mp_const_empty_bytes; + } + + vstr_t vstr; + size_t out_len = bufinfo.len * 2; + if (n_args > 1) { + // 1-char separator between hex numbers + out_len += bufinfo.len - 1; + sep = mp_obj_str_get_str(args[1]); + } + vstr_init_len(&vstr, out_len); + byte *in = bufinfo.buf, *out = (byte *)vstr.buf; + for (mp_uint_t i = bufinfo.len; i--;) { + byte d = (*in >> 4); + if (d > 9) { + d += 'a' - '9' - 1; + } + *out++ = d + '0'; + d = (*in++ & 0xf); + if (d > 9) { + d += 'a' - '9' - 1; + } + *out++ = d + '0'; + if (sep != NULL && i != 0) { + *out++ = *sep; + } + } + return mp_obj_new_str_from_vstr(type, &vstr); +} + +mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); + + if ((bufinfo.len & 1) != 0) { + mp_raise_ValueError(MP_ERROR_TEXT("odd-length string")); + } + vstr_t vstr; + vstr_init_len(&vstr, bufinfo.len / 2); + byte *in = bufinfo.buf, *out = (byte *)vstr.buf; + byte hex_byte = 0; + for (mp_uint_t i = bufinfo.len; i--;) { + byte hex_ch = *in++; + if (unichar_isxdigit(hex_ch)) { + hex_byte += unichar_xdigit_value(hex_ch); + } else { + mp_raise_ValueError(MP_ERROR_TEXT("non-hex digit found")); + } + if (i & 1) { + hex_byte <<= 4; + } else { + *out++ = hex_byte; + hex_byte = 0; + } + } + return mp_obj_new_str_from_vstr(MP_OBJ_TO_PTR(type_in), &vstr); +} + +STATIC mp_obj_t bytes_hex_as_str(size_t n_args, const mp_obj_t *args) { + return mp_obj_bytes_hex(n_args, args, &mp_type_str); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_hex_as_str_obj, 1, 2, bytes_hex_as_str); + +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bytes_fromhex_obj, mp_obj_bytes_fromhex); +STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(bytes_fromhex_classmethod_obj, MP_ROM_PTR(&bytes_fromhex_obj)); +#endif // MICROPY_PY_BUILTINS_BYTES_HEX + mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { if (flags == MP_BUFFER_READ) { GET_STR_DATA_LEN(self_in, str_data, str_len); @@ -1970,6 +2048,10 @@ STATIC const mp_rom_map_elem_t array_bytearray_str_bytes_locals_table[] = { { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&mp_obj_array_append_obj) }, { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&mp_obj_array_extend_obj) }, #endif + #if MICROPY_PY_BUILTINS_BYTES_HEX + { MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&bytes_hex_as_str_obj) }, + { MP_ROM_QSTR(MP_QSTR_fromhex), MP_ROM_PTR(&bytes_fromhex_classmethod_obj) }, + #endif #if MICROPY_CPYTHON_COMPAT { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) }, #endif @@ -2018,6 +2100,12 @@ STATIC const mp_rom_map_elem_t array_bytearray_str_bytes_locals_table[] = { #define TABLE_ENTRIES_COMPAT 0 #endif +#if MICROPY_PY_BUILTINS_BYTES_HEX +#define TABLE_ENTRIES_HEX 2 +#else +#define TABLE_ENTRIES_HEX 0 +#endif + #if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY #define TABLE_ENTRIES_ARRAY 2 #else @@ -2025,8 +2113,8 @@ STATIC const mp_rom_map_elem_t array_bytearray_str_bytes_locals_table[] = { #endif MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_str_locals_dict, - array_bytearray_str_bytes_locals_table + TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_COMPAT, - MP_ARRAY_SIZE(array_bytearray_str_bytes_locals_table) - (TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_COMPAT)); + array_bytearray_str_bytes_locals_table + TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_HEX + TABLE_ENTRIES_COMPAT, + MP_ARRAY_SIZE(array_bytearray_str_bytes_locals_table) - (TABLE_ENTRIES_ARRAY + TABLE_ENTRIES_HEX + TABLE_ENTRIES_COMPAT)); #if TABLE_ENTRIES_COMPAT == 0 #define mp_obj_bytes_locals_dict mp_obj_str_locals_dict @@ -2048,6 +2136,12 @@ MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_array_locals_dict, TABLE_ENTRIES_ARRAY); #endif +#if MICROPY_PY_BUILTINS_MEMORYVIEW && MICROPY_PY_BUILTINS_BYTES_HEX +MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_memoryview_locals_dict, + array_bytearray_str_bytes_locals_table + TABLE_ENTRIES_ARRAY, + 1); // Just the "hex" entry. +#endif + #if !MICROPY_PY_BUILTINS_STR_UNICODE STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); diff --git a/py/objstr.h b/py/objstr.h index 7d86ec30baab7..b1217d5872b12 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -127,8 +127,15 @@ MP_DECLARE_CONST_FUN_OBJ_1(str_isupper_obj); MP_DECLARE_CONST_FUN_OBJ_1(str_islower_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj); +mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type); +mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data); + extern const mp_obj_dict_t mp_obj_str_locals_dict; +#if MICROPY_PY_BUILTINS_MEMORYVIEW && MICROPY_PY_BUILTINS_BYTES_HEX +extern const mp_obj_dict_t mp_obj_memoryview_locals_dict; +#endif + #if MICROPY_PY_BUILTINS_BYTEARRAY extern const mp_obj_dict_t mp_obj_bytearray_locals_dict; #endif From f694058f2bce7d6e5ef82c8efd2bcb6cdebfe7a8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Jul 2021 22:49:49 +1000 Subject: [PATCH 2475/3533] tests/extmod/ubinascii: Add tests for bytes.hex etc. Also make the sep test not micropython-specific. Signed-off-by: Jim Mussared --- tests/basics/builtin_str_hex.py | 24 +++++++++++++++++++++++ tests/extmod/ubinascii_hexlify.py | 17 ++++++++++++---- tests/extmod/ubinascii_micropython.py | 15 -------------- tests/extmod/ubinascii_micropython.py.exp | 2 -- tests/extmod/ubinascii_unhexlify.py | 11 +++++++---- 5 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 tests/basics/builtin_str_hex.py delete mode 100644 tests/extmod/ubinascii_micropython.py delete mode 100644 tests/extmod/ubinascii_micropython.py.exp diff --git a/tests/basics/builtin_str_hex.py b/tests/basics/builtin_str_hex.py new file mode 100644 index 0000000000000..7390c8eaee17c --- /dev/null +++ b/tests/basics/builtin_str_hex.py @@ -0,0 +1,24 @@ +if not hasattr(bytes, "fromhex"): + print("SKIP") + raise SystemExit + +for x in ( + b"\x00\x01\x02\x03\x04\x05\x06\x07", + b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", + b"\x7f\x80\xff", + b"1234ABCDabcd", +): + print(x.hex()) + print(bytearray(x).hex()) + print(memoryview(x).hex()) + print(x.hex(":")) + print(bytearray(x).hex(":")) + print(memoryview(x).hex(":")) + +for x in ( + "0001020304050607", + "08090a0b0c0d0e0f", + "7f80ff", + "313233344142434461626364", +): + print(bytes.fromhex(x)) diff --git a/tests/extmod/ubinascii_hexlify.py b/tests/extmod/ubinascii_hexlify.py index 2329f53edd947..3c266fb6cc08c 100644 --- a/tests/extmod/ubinascii_hexlify.py +++ b/tests/extmod/ubinascii_hexlify.py @@ -7,7 +7,16 @@ print("SKIP") raise SystemExit -print(binascii.hexlify(b"\x00\x01\x02\x03\x04\x05\x06\x07")) -print(binascii.hexlify(b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")) -print(binascii.hexlify(b"\x7f\x80\xff")) -print(binascii.hexlify(b"1234ABCDabcd")) +for x in ( + b"\x00\x01\x02\x03\x04\x05\x06\x07", + b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", + b"\x7f\x80\xff", + b"1234ABCDabcd", +): + print(binascii.hexlify(x)) + +# Two-argument version (now supported in CPython) +print(binascii.hexlify(b"123", ":")) + +# zero length buffer +print(binascii.hexlify(b"", b":")) diff --git a/tests/extmod/ubinascii_micropython.py b/tests/extmod/ubinascii_micropython.py deleted file mode 100644 index 94e8daa557b77..0000000000000 --- a/tests/extmod/ubinascii_micropython.py +++ /dev/null @@ -1,15 +0,0 @@ -try: - try: - import ubinascii as binascii - except ImportError: - import binascii -except ImportError: - print("SKIP") - raise SystemExit - -# two arguments supported in uPy but not CPython -a = binascii.hexlify(b"123", ":") -print(a) - -# zero length buffer -print(binascii.hexlify(b"", b":")) diff --git a/tests/extmod/ubinascii_micropython.py.exp b/tests/extmod/ubinascii_micropython.py.exp deleted file mode 100644 index a195d2602c19e..0000000000000 --- a/tests/extmod/ubinascii_micropython.py.exp +++ /dev/null @@ -1,2 +0,0 @@ -b'31:32:33' -b'' diff --git a/tests/extmod/ubinascii_unhexlify.py b/tests/extmod/ubinascii_unhexlify.py index 413eaf1b6fd63..2c3598ab98ec3 100644 --- a/tests/extmod/ubinascii_unhexlify.py +++ b/tests/extmod/ubinascii_unhexlify.py @@ -7,10 +7,13 @@ print("SKIP") raise SystemExit -print(binascii.unhexlify(b"0001020304050607")) -print(binascii.unhexlify(b"08090a0b0c0d0e0f")) -print(binascii.unhexlify(b"7f80ff")) -print(binascii.unhexlify(b"313233344142434461626364")) +for x in ( + b"0001020304050607", + b"08090a0b0c0d0e0f", + b"7f80ff", + b"313233344142434461626364", +): + print(binascii.unhexlify(x)) try: a = binascii.unhexlify(b"0") # odd buffer length From ec24cd1d25a322192be93dc54a02ca4564fc9e7b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Aug 2022 10:12:21 +1000 Subject: [PATCH 2476/3533] mpy-cross,unix: Remove .gitignore file. Now that all build artefacts are placed in a build/ directory the gitignore is no longer needed. Signed-off-by: Damien George --- mpy-cross/.gitignore | 1 - ports/unix/.gitignore | 3 --- 2 files changed, 4 deletions(-) delete mode 100644 mpy-cross/.gitignore delete mode 100644 ports/unix/.gitignore diff --git a/mpy-cross/.gitignore b/mpy-cross/.gitignore deleted file mode 100644 index 82a0a7efaab3d..0000000000000 --- a/mpy-cross/.gitignore +++ /dev/null @@ -1 +0,0 @@ -mpy-cross diff --git a/ports/unix/.gitignore b/ports/unix/.gitignore deleted file mode 100644 index 3ca8f6cb27460..0000000000000 --- a/ports/unix/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -micropython -micropython-* -*.gcov From 945f377b436d3ba6d0ac9962762038133e864604 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Aug 2022 10:15:27 +1000 Subject: [PATCH 2477/3533] py/objstr: Remove str function object declarations from header file. Since f7f56d42851aaff2027e23a8ca45c1f1973f1aca consolidated all uses of these to a single locals dict, they no longer need to be made public. Signed-off-by: Damien George --- py/objstr.h | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/py/objstr.h b/py/objstr.h index b1217d5872b12..78441fedaf2cb 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -98,35 +98,6 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s mp_obj_t index, bool is_slice); const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj); -MP_DECLARE_CONST_FUN_OBJ_2(str_join_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj); -MP_DECLARE_CONST_FUN_OBJ_KW(str_splitlines_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj); -MP_DECLARE_CONST_FUN_OBJ_KW(str_format_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj); -MP_DECLARE_CONST_FUN_OBJ_2(str_partition_obj); -MP_DECLARE_CONST_FUN_OBJ_2(str_rpartition_obj); -MP_DECLARE_CONST_FUN_OBJ_2(str_center_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_lower_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_upper_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_isspace_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_isalpha_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_isdigit_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_isupper_obj); -MP_DECLARE_CONST_FUN_OBJ_1(str_islower_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj); - mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type); mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data); From cf90e24335652462bb7eb4bd105e061683bc316a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Aug 2022 10:32:20 +1000 Subject: [PATCH 2478/3533] py/mkrules: Use abspath to find directory for mpy-cross dependency. Otherwise if the `mpy-cross/build/` directory doesn't exist then `mpy-cross/build/..` won't work. Signed-off-by: Damien George --- py/mkrules.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/mkrules.mk b/py/mkrules.mk index 73c33227c09af..14f1b953cda40 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -162,7 +162,7 @@ $(HEADER_BUILD): ifneq ($(MICROPY_MPYCROSS_DEPENDENCY),) # to automatically build mpy-cross, if needed $(MICROPY_MPYCROSS_DEPENDENCY): - $(MAKE) -C $(dir $@).. + $(MAKE) -C $(abspath $(dir $@)..) endif ifneq ($(FROZEN_DIR),) From 5543b2a9cc7381931431c69d93a77ba5d5d58e2e Mon Sep 17 00:00:00 2001 From: Ned Konz Date: Wed, 1 Sep 2021 10:48:15 -0700 Subject: [PATCH 2479/3533] extmod/uasyncio: Add clear method to ThreadSafeFlag. This is useful in situations where the ThreadSafeFlag is reused and needs to be cleared of any previous, unwanted event. For example, clear the flag at the start of an operation, trigger the operation (eg an I2C write), then (a)wait for an external event to set the flag (eg a pin IRQ). Further events may trigger the flag again but these are unwanted and should be cleared before the next cycle starts. --- docs/library/uasyncio.rst | 7 ++++++- extmod/uasyncio/event.py | 5 ++++- tests/extmod/uasyncio_threadsafeflag.py | 20 ++++++++++++++++++++ tests/extmod/uasyncio_threadsafeflag.py.exp | 11 +++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/library/uasyncio.rst b/docs/library/uasyncio.rst index 0abbf8dd8d7d2..859d505d79a92 100644 --- a/docs/library/uasyncio.rst +++ b/docs/library/uasyncio.rst @@ -153,9 +153,14 @@ class ThreadSafeFlag .. method:: ThreadSafeFlag.set() - Set the flag. If there is a task waiting on the event, it will be scheduled + Set the flag. If there is a task waiting on the flag, it will be scheduled to run. +.. method:: ThreadSafeFlag.clear() + + Clear the flag. This may be used to ensure that a possibly previously-set + flag is clear before waiting for it. + .. method:: ThreadSafeFlag.wait() Wait for the flag to be set. If the flag is already set then it returns diff --git a/extmod/uasyncio/event.py b/extmod/uasyncio/event.py index c48904b9835c7..654ccefa98e00 100644 --- a/extmod/uasyncio/event.py +++ b/extmod/uasyncio/event.py @@ -36,7 +36,7 @@ async def wait(self): # MicroPython-extension: This can be set from outside the asyncio event loop, # such as other threads, IRQs or scheduler context. Implementation is a stream # that asyncio will poll until a flag is set. -# Note: Unlike Event, this is self-clearing. +# Note: Unlike Event, this is self-clearing after a wait(). try: import uio @@ -52,6 +52,9 @@ def ioctl(self, req, flags): def set(self): self._flag = 1 + def clear(self): + self._flag = 0 + async def wait(self): if not self._flag: yield core._io_queue.queue_read(self) diff --git a/tests/extmod/uasyncio_threadsafeflag.py b/tests/extmod/uasyncio_threadsafeflag.py index 4e002a3d2a0b1..a8a08d2e92587 100644 --- a/tests/extmod/uasyncio_threadsafeflag.py +++ b/tests/extmod/uasyncio_threadsafeflag.py @@ -75,5 +75,25 @@ async def main(): print("wait task") await t + # Flag set, cleared, and set again. + print("----") + print("set event") + flag.set() + print("yield") + await asyncio.sleep(0) + print("clear event") + flag.clear() + print("yield") + await asyncio.sleep(0) + t = asyncio.create_task(task(4, flag)) + print("yield") + await asyncio.sleep(0) + print("set event") + flag.set() + print("yield") + await asyncio.sleep(0) + print("wait task") + await t + asyncio.run(main()) diff --git a/tests/extmod/uasyncio_threadsafeflag.py.exp b/tests/extmod/uasyncio_threadsafeflag.py.exp index aef4e479ba447..757115ac4bdd4 100644 --- a/tests/extmod/uasyncio_threadsafeflag.py.exp +++ b/tests/extmod/uasyncio_threadsafeflag.py.exp @@ -19,3 +19,14 @@ yield task 3 task 3 done wait task +---- +set event +yield +clear event +yield +yield +task 4 +set event +yield +wait task +task 4 done From 01514e80c9a150f0506bd6c8c43590ca05f8598f Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 12 Aug 2022 17:03:38 +1000 Subject: [PATCH 2480/3533] extmod/uasyncio: Rename internal _flag to state, to save a qstr. Saves about 16 bytes of flash when uasyncio is frozen in. Signed-off-by: Damien George --- extmod/uasyncio/event.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extmod/uasyncio/event.py b/extmod/uasyncio/event.py index 654ccefa98e00..3b5e79d8f23ea 100644 --- a/extmod/uasyncio/event.py +++ b/extmod/uasyncio/event.py @@ -42,23 +42,23 @@ async def wait(self): class ThreadSafeFlag(uio.IOBase): def __init__(self): - self._flag = 0 + self.state = 0 def ioctl(self, req, flags): if req == 3: # MP_STREAM_POLL - return self._flag * flags + return self.state * flags return None def set(self): - self._flag = 1 + self.state = 1 def clear(self): - self._flag = 0 + self.state = 0 async def wait(self): - if not self._flag: + if not self.state: yield core._io_queue.queue_read(self) - self._flag = 0 + self.state = 0 except ImportError: pass From 69719927f1752b2d6333708c5b60067fe3b7965d Mon Sep 17 00:00:00 2001 From: MrJake222 Date: Thu, 16 Sep 2021 19:49:42 +0200 Subject: [PATCH 2481/3533] extmod/modlwip: Add support for leaving multicast groups. --- extmod/modlwip.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 25669228dffd6..c6ee02132ff21 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -66,6 +66,7 @@ // All socket options should be globally distinct, // because we ignore option levels for efficiency. #define IP_ADD_MEMBERSHIP 0x400 +#define IP_DROP_MEMBERSHIP 0x401 // For compatibilily with older lwIP versions. #ifndef ip_set_option @@ -1376,7 +1377,8 @@ STATIC mp_obj_t lwip_socket_setsockopt(size_t n_args, const mp_obj_t *args) { } // level: IPPROTO_IP - case IP_ADD_MEMBERSHIP: { + case IP_ADD_MEMBERSHIP: + case IP_DROP_MEMBERSHIP: { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); if (bufinfo.len != sizeof(ip_addr_t) * 2) { @@ -1384,7 +1386,12 @@ STATIC mp_obj_t lwip_socket_setsockopt(size_t n_args, const mp_obj_t *args) { } // POSIX setsockopt has order: group addr, if addr, lwIP has it vice-versa - err_t err = igmp_joingroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf); + err_t err; + if (opt == IP_ADD_MEMBERSHIP) { + err = igmp_joingroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf); + } else { + err = igmp_leavegroup((ip_addr_t *)bufinfo.buf + 1, bufinfo.buf); + } if (err != ERR_OK) { mp_raise_OSError(error_lookup_table[-err]); } @@ -1769,6 +1776,7 @@ STATIC const mp_rom_map_elem_t mp_module_lwip_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_IPPROTO_IP), MP_ROM_INT(0) }, { MP_ROM_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_ROM_INT(IP_ADD_MEMBERSHIP) }, + { MP_ROM_QSTR(MP_QSTR_IP_DROP_MEMBERSHIP), MP_ROM_INT(IP_DROP_MEMBERSHIP) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_lwip_globals, mp_module_lwip_globals_table); From 6cd2e4191803e95580bdfc57c06ea818454a25d1 Mon Sep 17 00:00:00 2001 From: Dan Ellis Date: Thu, 28 Jul 2022 23:21:00 -0400 Subject: [PATCH 2482/3533] py/parsenum: Ensure that trailing zeros lead to identical results. Prior to this commit, parsenum would calculate "1e-20" as 1.0*pow(10, -20), and "1.000e-20" as 1000.0*pow(10, -23); in certain cases, this could make seemingly-identical values compare as not equal. This commit watches for trailing zeros as a special case, and ignores them when appropriate, so "1.000e-20" is also calculated as 1.0*pow(10, -20). Fixes issue #5831. --- py/parsenum.c | 70 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/py/parsenum.c b/py/parsenum.c index 5e8e5dfe8c0f9..19cc719201400 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -178,31 +178,51 @@ typedef enum { PARSE_DEC_IN_EXP, } parse_dec_in_t; -#if MICROPY_PY_BUILTINS_COMPLEX -mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) -#else -mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lexer_t *lex) -#endif -{ - #if MICROPY_PY_BUILTINS_FLOAT - +#if MICROPY_PY_BUILTINS_FLOAT // DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing // SMALL_NORMAL_VAL is the smallest power of 10 that is still a normal float // EXACT_POWER_OF_10 is the largest value of x so that 10^x can be stored exactly in a float // Note: EXACT_POWER_OF_10 is at least floor(log_5(2^mantissa_length)). Indeed, 10^n = 2^n * 5^n // so we only have to store the 5^n part in the mantissa (the 2^n part will go into the float's // exponent). - #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT +#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT #define DEC_VAL_MAX 1e20F #define SMALL_NORMAL_VAL (1e-37F) #define SMALL_NORMAL_EXP (-37) #define EXACT_POWER_OF_10 (9) - #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE +#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE #define DEC_VAL_MAX 1e200 #define SMALL_NORMAL_VAL (1e-307) #define SMALL_NORMAL_EXP (-307) #define EXACT_POWER_OF_10 (22) - #endif +#endif + +// Break out inner digit accumulation routine to ease trailing zero deferral. +static void accept_digit(mp_float_t *p_dec_val, int dig, int *p_exp_extra, int in) { + // Core routine to ingest an additional digit. + if (*p_dec_val < DEC_VAL_MAX) { + // dec_val won't overflow so keep accumulating + *p_dec_val = 10 * *p_dec_val + dig; + if (in == PARSE_DEC_IN_FRAC) { + --(*p_exp_extra); + } + } else { + // dec_val might overflow and we anyway can't represent more digits + // of precision, so ignore the digit and just adjust the exponent + if (in == PARSE_DEC_IN_INTG) { + ++(*p_exp_extra); + } + } +} +#endif // MICROPY_BUILTINS_FLOAT + +#if MICROPY_PY_BUILTINS_COMPLEX +mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) +#else +mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lexer_t *lex) +#endif +{ + #if MICROPY_PY_BUILTINS_FLOAT const char *top = str + len; mp_float_t dec_val = 0; @@ -255,6 +275,7 @@ mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lex bool exp_neg = false; int exp_val = 0; int exp_extra = 0; + int trailing_zeros_intg = 0, trailing_zeros_frac = 0; while (str < top) { unsigned int dig = *str++; if ('0' <= dig && dig <= '9') { @@ -267,18 +288,25 @@ mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lex exp_val = 10 * exp_val + dig; } } else { - if (dec_val < DEC_VAL_MAX) { - // dec_val won't overflow so keep accumulating - dec_val = 10 * dec_val + dig; - if (in == PARSE_DEC_IN_FRAC) { - --exp_extra; + if (dig == 0 || dec_val >= DEC_VAL_MAX) { + // Defer treatment of zeros in fractional part. If nothing comes afterwards, ignore them. + // Also, once we reach DEC_VAL_MAX, treat every additional digit as a trailing zero. + if (in == PARSE_DEC_IN_INTG) { + ++trailing_zeros_intg; + } else { + ++trailing_zeros_frac; } } else { - // dec_val might overflow and we anyway can't represent more digits - // of precision, so ignore the digit and just adjust the exponent - if (in == PARSE_DEC_IN_INTG) { - ++exp_extra; + // Time to un-defer any trailing zeros. Intg zeros first. + while (trailing_zeros_intg) { + accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_INTG); + --trailing_zeros_intg; + } + while (trailing_zeros_frac) { + accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_FRAC); + --trailing_zeros_frac; } + accept_digit(&dec_val, dig, &exp_extra, in); } } } else if (in == PARSE_DEC_IN_INTG && dig == '.') { @@ -311,7 +339,7 @@ mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lex } // apply the exponent, making sure it's not a subnormal value - exp_val += exp_extra; + exp_val += exp_extra + trailing_zeros_intg; if (exp_val < SMALL_NORMAL_EXP) { exp_val -= SMALL_NORMAL_EXP; dec_val *= SMALL_NORMAL_VAL; From 6f4d424f461bede1afb69637763f4fce5f27cd90 Mon Sep 17 00:00:00 2001 From: Dan Ellis Date: Thu, 28 Jul 2022 10:49:18 -0400 Subject: [PATCH 2483/3533] py/formatfloat: Use pow(10, e) instead of pos/neg_pow lookup tables. Rework the conversion of floats to decimal strings so it aligns precisely with the conversion of strings to floats in parsenum.c. This is to avoid rendering 1eX as 9.99999eX-1 etc. This is achieved by removing the power- of-10 tables and using pow() to compute the exponent directly, and that's done efficiently by first estimating the power-of-10 exponent from the power-of-2 exponent in the floating-point representation. Code size is reduced by roughly 100 to 200 bytes by this commit. Signed-off-by: Dan Ellis --- py/formatfloat.c | 132 ++++++-------------- py/misc.h | 2 + tests/float/float_format.py | 8 ++ tests/float/float_format_ints_doubleprec.py | 3 + tests/float/string_format_modulo.py | 5 +- 5 files changed, 53 insertions(+), 97 deletions(-) diff --git a/py/formatfloat.c b/py/formatfloat.c index 357b73ace3ab0..fc1b2fe7fc5b4 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -62,26 +62,20 @@ #define FPMIN_BUF_SIZE 6 // +9e+99 #define FLT_SIGN_MASK 0x80000000 -#define FLT_EXP_MASK 0x7F800000 -#define FLT_MAN_MASK 0x007FFFFF -union floatbits { - float f; - uint32_t u; -}; static inline int fp_signbit(float x) { - union floatbits fb = {x}; - return fb.u & FLT_SIGN_MASK; + mp_float_union_t fb = {x}; + return fb.i & FLT_SIGN_MASK; } #define fp_isnan(x) isnan(x) #define fp_isinf(x) isinf(x) static inline int fp_iszero(float x) { - union floatbits fb = {x}; - return fb.u == 0; + mp_float_union_t fb = {x}; + return fb.i == 0; } static inline int fp_isless1(float x) { - union floatbits fb = {x}; - return fb.u < 0x3f800000; + mp_float_union_t fb = {x}; + return fb.i < 0x3f800000; } #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE @@ -99,28 +93,11 @@ static inline int fp_isless1(float x) { #endif // MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT/DOUBLE -static inline int fp_ge_eps(FPTYPE x, FPTYPE y) { - mp_float_union_t fb_y = {y}; - // Back off 2 eps. - // This is valid for almost all values, but in practice - // it's only used when y = 1eX for X>=0. - fb_y.i -= 2; - return x >= fb_y.f; +static inline int fp_expval(FPTYPE x) { + mp_float_union_t fb = {x}; + return (int)((fb.i >> MP_FLOAT_FRAC_BITS) & (~(0xFFFFFFFF << MP_FLOAT_EXP_BITS))) - MP_FLOAT_EXP_OFFSET; } -static const FPTYPE g_pos_pow[] = { - #if FPDECEXP > 32 - MICROPY_FLOAT_CONST(1e256), MICROPY_FLOAT_CONST(1e128), MICROPY_FLOAT_CONST(1e64), - #endif - MICROPY_FLOAT_CONST(1e32), MICROPY_FLOAT_CONST(1e16), MICROPY_FLOAT_CONST(1e8), MICROPY_FLOAT_CONST(1e4), MICROPY_FLOAT_CONST(1e2), MICROPY_FLOAT_CONST(1e1) -}; -static const FPTYPE g_neg_pow[] = { - #if FPDECEXP > 32 - MICROPY_FLOAT_CONST(1e-256), MICROPY_FLOAT_CONST(1e-128), MICROPY_FLOAT_CONST(1e-64), - #endif - MICROPY_FLOAT_CONST(1e-32), MICROPY_FLOAT_CONST(1e-16), MICROPY_FLOAT_CONST(1e-8), MICROPY_FLOAT_CONST(1e-4), MICROPY_FLOAT_CONST(1e-2), MICROPY_FLOAT_CONST(1e-1) -}; - int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, char sign) { char *s = buf; @@ -177,14 +154,15 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch if (fmt == 'g' && prec == 0) { prec = 1; } - int e, e1; + int e; int dec = 0; char e_sign = '\0'; int num_digits = 0; - const FPTYPE *pos_pow = g_pos_pow; - const FPTYPE *neg_pow = g_neg_pow; int signed_e = 0; + // Approximate power of 10 exponent from binary exponent. + // abs(e_guess) is lower bound on abs(power of 10 exponent). + int e_guess = (int)(fp_expval(f) * FPCONST(0.3010299956639812)); // 1/log2(10). if (fp_iszero(f)) { e = 0; if (fmt == 'f') { @@ -203,25 +181,18 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } } } else if (fp_isless1(f)) { - FPTYPE f_mod = f; + FPTYPE f_entry = f; // Save f in case we go to 'f' format. // Build negative exponent - for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) { - if (*neg_pow > f_mod) { - e += e1; - f_mod *= *pos_pow; - } - } - - char e_sign_char = '-'; - if (fp_isless1(f_mod) && f_mod >= FPROUND_TO_ONE) { - f_mod = FPCONST(1.0); - if (e == 0) { - e_sign_char = '+'; - } - } else if (fp_isless1(f_mod)) { - e++; - f_mod *= FPCONST(10.0); + e = -e_guess; + FPTYPE u_base = MICROPY_FLOAT_C_FUN(pow)(10, -e); + while (u_base > f) { + ++e; + u_base = MICROPY_FLOAT_C_FUN(pow)(10, -e); } + // Normalize out the inferred unit. Use divide because + // pow(10, e) * pow(10, -e) is slightly < 1 for some e in float32 + // (e.g. print("%.12f" % ((1e13) * (1e-13)))) + f /= u_base; // If the user specified 'g' format, and e is <= 4, then we'll switch // to the fixed format ('f') @@ -241,11 +212,12 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch num_digits = prec; signed_e = 0; + f = f_entry; ++num_digits; } else { // For e & g formats, we'll be printing the exponent, so set the // sign. - e_sign = e_sign_char; + e_sign = '-'; dec = 0; if (prec > (buf_remaining - FPMIN_BUF_SIZE)) { @@ -262,19 +234,11 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch // scaling it. Instead, we find the product of powers of 10 // that is not greater than it, and use that to start the // mantissa. - FPTYPE u_base = FPCONST(1.0); - for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++) { - FPTYPE next_u = u_base * *pos_pow; - // fp_ge_eps performs "f >= (next_u - 2eps)" so that if, for - // numerical reasons, f is very close to a power of ten but - // not strictly equal, we still treat it as that power of 10. - // The comparison was failing for maybe 10% of 1eX values, but - // although rounding fixed many of them, there were still some - // rendering as 9.99999998e(X-1). - if (fp_ge_eps(f, next_u)) { - u_base = next_u; - e += e1; - } + e = e_guess; + FPTYPE next_u = MICROPY_FLOAT_C_FUN(pow)(10, e + 1); + while (f >= next_u) { + ++e; + next_u = MICROPY_FLOAT_C_FUN(pow)(10, e + 1); } // If the user specified fixed format (fmt == 'f') and e makes the @@ -341,46 +305,22 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch num_digits = prec; } - if (signed_e < 0) { - // The algorithm below treats numbers smaller than 1 by scaling them - // repeatedly by 10 to bring the new digit to the top. Our input number - // was smaller than 1, so scale it up to be 1 <= f < 10. - FPTYPE u_base = FPCONST(1.0); - const FPTYPE *pow_u = g_pos_pow; - for (int m = FPDECEXP; m; m >>= 1, pow_u++) { - if (m & e) { - u_base *= *pow_u; - } - } - f *= u_base; - } - int d = 0; - int num_digits_left = num_digits; - for (int digit_index = signed_e; num_digits_left >= 0; --digit_index) { + for (int digit_index = signed_e; num_digits >= 0; --digit_index) { FPTYPE u_base = FPCONST(1.0); if (digit_index > 0) { // Generate 10^digit_index for positive digit_index. - const FPTYPE *pow_u = g_pos_pow; - int target_index = digit_index; - for (int m = FPDECEXP; m; m >>= 1, pow_u++) { - if (m & target_index) { - u_base *= *pow_u; - } - } + u_base = MICROPY_FLOAT_C_FUN(pow)(10, digit_index); } for (d = 0; d < 9; ++d) { - // This is essentially "if (f < u_base)", but with 2eps margin - // so that if f is just a tiny bit smaller, we treat it as - // equal (and accept the additional digit value). - if (!fp_ge_eps(f, u_base)) { + if (f < u_base) { break; } f -= u_base; } // We calculate one more digit than we display, to use in rounding // below. So only emit the digit if it's one that we display. - if (num_digits_left > 0) { + if (num_digits > 0) { // Emit this number (the leading digit). *s++ = '0' + d; if (dec == 0 && prec > 0) { @@ -388,9 +328,9 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } } --dec; - --num_digits_left; + --num_digits; if (digit_index <= 0) { - // Once we get below 1.0, we scale up f instead of calculting + // Once we get below 1.0, we scale up f instead of calculating // negative powers of 10 in u_base. This provides better // renditions of exact decimals like 1/16 etc. f *= FPCONST(10.0); diff --git a/py/misc.h b/py/misc.h index e642598c56d03..134325f8946ff 100644 --- a/py/misc.h +++ b/py/misc.h @@ -243,10 +243,12 @@ extern mp_uint_t mp_verbose_flag; #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE #define MP_FLOAT_EXP_BITS (11) +#define MP_FLOAT_EXP_OFFSET (1023) #define MP_FLOAT_FRAC_BITS (52) typedef uint64_t mp_float_uint_t; #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT #define MP_FLOAT_EXP_BITS (8) +#define MP_FLOAT_EXP_OFFSET (127) #define MP_FLOAT_FRAC_BITS (23) typedef uint32_t mp_float_uint_t; #endif diff --git a/tests/float/float_format.py b/tests/float/float_format.py index 4c8a217567abf..98ed0eb096fa4 100644 --- a/tests/float/float_format.py +++ b/tests/float/float_format.py @@ -17,3 +17,11 @@ # check a case that would render negative digit values, eg ")" characters # the string is converted back to a float to check for no illegal characters float("%.23e" % 1e-80) + +# Check a problem with malformed "e" format numbers on the edge of 1.0e-X. +for r in range(38): + s = "%.12e" % float("1e-" + str(r)) + # It may format as 1e-r, or 9.999...e-(r+1), both are OK. + # But formatting as 0.999...e-r is NOT ok. + if s[0] == "0": + print("FAIL:", s) diff --git a/tests/float/float_format_ints_doubleprec.py b/tests/float/float_format_ints_doubleprec.py index 57899d6d65a85..67101d3e45037 100644 --- a/tests/float/float_format_ints_doubleprec.py +++ b/tests/float/float_format_ints_doubleprec.py @@ -13,3 +13,6 @@ v2 = 0x6974E718D7D7625A # 1e200 print("{:.12e}".format(array.array("d", v1.to_bytes(8, sys.byteorder))[0])) print("{:.12e}".format(array.array("d", v2.to_bytes(8, sys.byteorder))[0])) + +for i in range(300): + print(float("1e" + str(i))) diff --git a/tests/float/string_format_modulo.py b/tests/float/string_format_modulo.py index 09446153810db..3c206b7393588 100644 --- a/tests/float/string_format_modulo.py +++ b/tests/float/string_format_modulo.py @@ -41,7 +41,10 @@ print(("%.40g" % 1e-1)[:2]) print(("%.40g" % 1e-2)[:2]) print(("%.40g" % 1e-3)[:2]) -print(("%.40g" % 1e-4)[:2]) +# Under Appveyor Release builds, 1e-4 was being formatted as 9.99999...e-5 +# instead of 0.0001. (Interestingly, it formatted correctly for the Debug +# build). Avoid the edge case. +print(("%.40g" % 1.1e-4)[:2]) print("%.0g" % 1) # 0 precision 'g' From a16a330da54afd392252d7ea04139fd4702f48f8 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 15 Aug 2022 16:07:00 +1000 Subject: [PATCH 2484/3533] nrf,stm32: Don't enable debug info by default if LTO is on. It seems sometimes gcc with LTO will generate otherwise valid assembly listings that cause 'as' to error out when generating DWARF debug info; see https://sourceware.org/bugzilla/show_bug.cgi?id=29494 Therefore, don't enable -g by default if LTO is on. Enabling LTO=1 DEBUG=1 is still possible but may result in random errors at link time due to 'as' (the error in this case is "Error: unaligned opcodes detected in executable segment", and the only other easy workaround is CFLAGS+=-fno-jump-tables which may increase code size significantly). Follows on from fdfe4eca745dce5f20fb65a3c197006b9053999a --- ports/nrf/Makefile | 6 ++++-- ports/stm32/Makefile | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 23be4430f3960..bc295cac808e8 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -138,12 +138,14 @@ LDFLAGS += -Wl,'--defsym=_fs_size=$(FS_SIZE)' endif #Debugging/Optimization -CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) #ASMFLAGS += -g -gtabs+ -CFLAGS += -O0 +CFLAGS += -g -O0 LDFLAGS += -O0 else +ifneq ($(LTO), 1) +CFLAGS += -g # always include debug info in the ELF, unless LTO is on +endif CFLAGS += -Os -DNDEBUG LDFLAGS += -Os endif diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 657524798cf5f..14a93a5aa7658 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -114,13 +114,15 @@ $(BUILD)/stm32_it.o $(BUILD)/pendsv.o: CFLAGS += -fno-lto endif # Debugging/Optimization -CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG), 1) -CFLAGS += -DPENDSV_DEBUG +CFLAGS += -g -DPENDSV_DEBUG COPT ?= -Og # Disable text compression in debug builds MICROPY_ROM_TEXT_COMPRESSION = 0 else +ifneq ($(LTO), 1) +CFLAGS += -g # always include debug info in the ELF, unless LTO is on +endif COPT ?= -Os -DNDEBUG endif From 454d969781564567864bb431f416df1fc1852765 Mon Sep 17 00:00:00 2001 From: David Peake Date: Mon, 15 Aug 2022 20:17:41 +1000 Subject: [PATCH 2485/3533] docs/esp32: Fix string quoting consistency in SDCard mount example. It appears that strings in the documentation are typically single quoted. --- docs/esp32/quickref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 3cbb673c04b52..1529c0ef49297 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -589,7 +589,7 @@ See :ref:`machine.SDCard `. :: # Slot 2 uses pins sck=18, cs=5, miso=19, mosi=23 sd = machine.SDCard(slot=2) - os.mount(sd, "/sd") # mount + os.mount(sd, '/sd') # mount os.listdir('/sd') # list directory contents From 98bd7e33b32fb00ad2900e71cbe5cc4938598a7a Mon Sep 17 00:00:00 2001 From: Efi Weiss Date: Sun, 14 Aug 2022 22:12:24 +0300 Subject: [PATCH 2486/3533] unix/modusocket: Support proto and flags arguments to getaddrinfo. Signed-off-by: Efi Weiss --- ports/unix/modusocket.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 03d069cb89ce9..69ae2a78ebbb2 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -564,7 +564,6 @@ STATIC mp_obj_t mod_socket_inet_ntop(mp_obj_t family_in, mp_obj_t binaddr_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_ntop_obj, mod_socket_inet_ntop); STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { - // TODO: Implement 5th and 6th args const char *host = mp_obj_str_get_str(args[0]); const char *serv = NULL; @@ -600,6 +599,12 @@ STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { hints.ai_family = MP_OBJ_SMALL_INT_VALUE(args[2]); if (n_args > 3) { hints.ai_socktype = MP_OBJ_SMALL_INT_VALUE(args[3]); + if (n_args > 4) { + hints.ai_protocol = MP_OBJ_SMALL_INT_VALUE(args[4]); + if (n_args > 5) { + hints.ai_flags = MP_OBJ_SMALL_INT_VALUE(args[5]); + } + } } } @@ -633,7 +638,7 @@ STATIC mp_obj_t mod_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { freeaddrinfo(addr_list); return list; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 4, mod_socket_getaddrinfo); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 6, mod_socket_getaddrinfo); STATIC mp_obj_t mod_socket_sockaddr(mp_obj_t sockaddr_in) { mp_buffer_info_t bufinfo; From cbc9f944c4db6e3aee0bce0584b1eab674f0e5b9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Aug 2022 11:46:53 +1000 Subject: [PATCH 2487/3533] tests,tools: Update path to unix micropython executable. These were missed by 47c84286e8c8d9873e99f12711a683ecd6b9ca62 Signed-off-by: Damien George --- tests/run-internalbench.py | 2 +- tests/run-multitests.py | 2 +- tests/run-perfbench.py | 2 +- tools/codestats.sh | 2 +- tools/gen-cpydiff.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/run-internalbench.py b/tests/run-internalbench.py index 606fc3b77201e..0d21978501101 100755 --- a/tests/run-internalbench.py +++ b/tests/run-internalbench.py @@ -16,7 +16,7 @@ MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe") else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") def run_tests(pyb, test_dict): diff --git a/tests/run-multitests.py b/tests/run-multitests.py index d8a4a48fa755a..bd6cc70f7a516 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -18,7 +18,7 @@ MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe") else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") # For diff'ing test output DIFF = os.getenv("MICROPY_DIFF", "diff -u") diff --git a/tests/run-perfbench.py b/tests/run-perfbench.py index d70b996937373..6f340968b2f90 100755 --- a/tests/run-perfbench.py +++ b/tests/run-perfbench.py @@ -21,7 +21,7 @@ MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe") else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") PYTHON_TRUTH = CPYTHON3 diff --git a/tools/codestats.sh b/tools/codestats.sh index 09284a30de3ac..74430739917b5 100755 --- a/tools/codestats.sh +++ b/tools/codestats.sh @@ -23,7 +23,7 @@ AWK=awk MAKE="make -j2" # these are the binaries that are built; some have 2 or 3 depending on version -bin_unix=ports/unix/micropython +bin_unix=ports/unix/build-standard/micropython bin_stm32=ports/stm32/build-PYBV10/firmware.elf bin_barearm_1=ports/bare-arm/build/flash.elf bin_barearm_2=ports/bare-arm/build/firmware.elf diff --git a/tools/gen-cpydiff.py b/tools/gen-cpydiff.py index d4c8a5736d9ed..46b3079cac2ea 100644 --- a/tools/gen-cpydiff.py +++ b/tools/gen-cpydiff.py @@ -42,7 +42,7 @@ MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe") else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") TESTPATH = "../tests/cpydiff/" DOCPATH = "../docs/genrst/" From 237a393bec51ae564b2b9ded404ef58949a3bc63 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Aug 2022 11:48:45 +1000 Subject: [PATCH 2488/3533] extmod/vfs_posix_file: Remove unused MICROPY_VFS_POSIX_FILE. This was made obsolete by 2b409ef8a46015f8f3bd20bc44e644637dbe9bd3 Signed-off-by: Damien George --- extmod/vfs_posix_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 795ad7bbd9ea4..a758db14c3bd8 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -30,7 +30,7 @@ #include "py/stream.h" #include "extmod/vfs_posix.h" -#if MICROPY_VFS_POSIX || MICROPY_VFS_POSIX_FILE +#if MICROPY_VFS_POSIX #include #include @@ -285,4 +285,4 @@ const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_vfs_posix_textio}, S const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_vfs_posix_textio}, STDOUT_FILENO}; const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_vfs_posix_textio}, STDERR_FILENO}; -#endif // MICROPY_VFS_POSIX || MICROPY_VFS_POSIX_FILE +#endif // MICROPY_VFS_POSIX From 8f4c108025d4590a1b7f9d062ad64f95c45e0bc2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Aug 2022 11:54:17 +1000 Subject: [PATCH 2489/3533] all: Remove MICROPY_PY_IO_FILEIO config option. Since commit e65d1e69e88268145ff0e7e73240f028885915be there is no longer an io.FileIO class, so this option is no longer needed. This option also controlled whether or not files supported being opened in binary mode (eg 'rb'), and could, if disabled, lead to confusion as to why opening a file in binary mode silently did the wrong thing (it would just open in text mode if MICROPY_PY_IO_FILEIO was disabled). The various VFS implementations (POSIX, FAT, LFS) were the only places where enabling this option made a difference, and in almost all cases where one of these filesystems were enabled, MICROPY_PY_IO_FILEIO was also enabled. So it makes sense to just unconditionally enable this feature (ability to open a file in binary mode) in all cases, and so just remove this config option altogether. That makes configuration simpler and means binary file support always exists (and opening a file in binary mode is arguably more fundamental than opening in text mode, so if anything should be configurable then it should be the ability to open in text mode). Signed-off-by: Damien George --- examples/embedding/mpconfigport_minimal.h | 1 - extmod/vfs_fat_file.c | 4 ---- extmod/vfs_lfsx_file.c | 4 ---- extmod/vfs_posix_file.c | 5 ----- ports/cc3200/mpconfigport.h | 1 - ports/esp8266/mpconfigport.h | 1 - ports/mimxrt/mpconfigport.h | 1 - ports/nrf/mpconfigport.h | 1 - ports/renesas-ra/mpconfigport.h | 1 - ports/samd/mpconfigport.h | 1 - ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h | 1 - ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h | 1 - ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h | 1 - ports/unix/mpconfigport.h | 1 - ports/unix/variants/minimal/mpconfigvariant.h | 1 - ports/windows/mpconfigport.h | 1 - py/mpconfig.h | 5 ----- 17 files changed, 31 deletions(-) diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 91b194bc7bc3f..02089c1a62135 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -69,7 +69,6 @@ #define MICROPY_PY_MATH (0) #define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (0) -#define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_STRUCT (0) #define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_EXIT (0) diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index ebf36fc3978c4..874f10c50045c 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -170,7 +170,6 @@ STATIC const mp_rom_map_elem_t vfs_fat_rawfile_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(vfs_fat_rawfile_locals_dict, vfs_fat_rawfile_locals_dict_table); -#if MICROPY_PY_IO_FILEIO STATIC const mp_stream_p_t vfs_fat_fileio_stream_p = { .read = file_obj_read, .write = file_obj_write, @@ -186,7 +185,6 @@ const mp_obj_type_t mp_type_vfs_fat_fileio = { .protocol = &vfs_fat_fileio_stream_p, .locals_dict = (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict, }; -#endif STATIC const mp_stream_p_t vfs_fat_textio_stream_p = { .read = file_obj_read, @@ -230,11 +228,9 @@ STATIC mp_obj_t fat_vfs_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_i case '+': mode |= FA_READ | FA_WRITE; break; - #if MICROPY_PY_IO_FILEIO case 'b': type = &mp_type_vfs_fat_fileio; break; - #endif case 't': type = &mp_type_vfs_fat_textio; break; diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index bc1a37b90bdb8..124361feb99f2 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -68,11 +68,9 @@ mp_obj_t MP_VFS_LFSx(file_open)(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mod case '+': flags |= LFSx_MACRO(_O_RDWR); break; - #if MICROPY_PY_IO_FILEIO case 'b': type = &MP_TYPE_VFS_LFSx_(_fileio); break; - #endif case 't': type = &MP_TYPE_VFS_LFSx_(_textio); break; @@ -216,7 +214,6 @@ STATIC const mp_rom_map_elem_t MP_VFS_LFSx(file_locals_dict_table)[] = { }; STATIC MP_DEFINE_CONST_DICT(MP_VFS_LFSx(file_locals_dict), MP_VFS_LFSx(file_locals_dict_table)); -#if MICROPY_PY_IO_FILEIO STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = { .read = MP_VFS_LFSx(file_read), .write = MP_VFS_LFSx(file_write), @@ -232,7 +229,6 @@ const mp_obj_type_t MP_TYPE_VFS_LFSx_(_fileio) = { .protocol = &MP_VFS_LFSx(fileio_stream_p), .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict), }; -#endif STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = { .read = MP_VFS_LFSx(file_read), diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index a758db14c3bd8..c550842cd4e86 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -83,15 +83,12 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_ case '+': mode_rw = O_RDWR; break; - #if MICROPY_PY_IO_FILEIO - // If we don't have io.FileIO, then files are in text mode implicitly case 'b': type = &mp_type_vfs_posix_fileio; break; case 't': type = &mp_type_vfs_posix_textio; break; - #endif } } @@ -246,7 +243,6 @@ STATIC const mp_rom_map_elem_t vfs_posix_rawfile_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(vfs_posix_rawfile_locals_dict, vfs_posix_rawfile_locals_dict_table); -#if MICROPY_PY_IO_FILEIO STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = { .read = vfs_posix_file_read, .write = vfs_posix_file_write, @@ -262,7 +258,6 @@ const mp_obj_type_t mp_type_vfs_posix_fileio = { .protocol = &vfs_posix_fileio_stream_p, .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, }; -#endif STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { .read = vfs_posix_file_read, diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index f41c1fe99dbfb..93fc291c1a4c2 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -105,7 +105,6 @@ #define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (1) -#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_UERRNO (1) #define MICROPY_PY_UERRNO_ERRORCODE (0) #define MICROPY_PY_THREAD (1) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 9ff6a34ffd625..ded56663b32bf 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -35,7 +35,6 @@ #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (0) #define MICROPY_PY_MATH_FACTORIAL (0) #define MICROPY_PY_MATH_ISCLOSE (0) -#define MICROPY_PY_IO_FILEIO (MICROPY_VFS) #define MICROPY_PY_SYS_PS1_PS2 (0) #define MICROPY_PY_UBINASCII_CRC32 (0) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (0) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 54d649795df0c..8642d53ecc455 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -101,7 +101,6 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_MATH_ISCLOSE (1) #define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO_IOBASE (1) -#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_PLATFORM "mimxrt" #define MICROPY_PY_SYS_STDFILES (1) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index a25ca1ec27efe..ac238057dd427 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -155,7 +155,6 @@ #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_SYS_MAXSIZE (1) -#define MICROPY_PY_IO_FILEIO (MICROPY_VFS_FAT || MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #define MICROPY_PY_UTIME_MP_HAL (1) diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 65b194274a53d..b77a3c492323b 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -83,7 +83,6 @@ #ifndef MICROPY_PY_BUILTINS_HELP_TEXT #define MICROPY_PY_BUILTINS_HELP_TEXT ra_help_text #endif -#define MICROPY_PY_IO_FILEIO (MICROPY_VFS_FAT || MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) #ifndef MICROPY_PY_SYS_PLATFORM // let boards override it if they want #define MICROPY_PY_SYS_PLATFORM "renesas-ra" #endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index c02d9316ded30..0b16b5e817af0 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -77,7 +77,6 @@ #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_PY_SYS_MAXSIZE (1) -#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_IO (1) #define MICROPY_PY_IO_IOBASE (1) diff --git a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h index d4996e6c74d65..dfcca72afb55c 100644 --- a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h +++ b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h @@ -11,7 +11,6 @@ #define MICROPY_PY_BUILTINS_COMPLEX (0) #define MICROPY_PY_GENERATOR_PEND_THROW (0) #define MICROPY_PY_MATH (0) -#define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_FRAMEBUF (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) diff --git a/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h index a68b939939c17..d44bfb0bb663b 100644 --- a/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_F091RC/mpconfigboard.h @@ -5,7 +5,6 @@ #define MICROPY_EMIT_INLINE_THUMB (0) #define MICROPY_OPT_COMPUTED_GOTO (0) #define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) #define MICROPY_PY_STM (0) diff --git a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h index 9af925d45196c..c8c809eb4899e 100644 --- a/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h @@ -11,7 +11,6 @@ #define MICROPY_PY_BUILTINS_COMPLEX (0) #define MICROPY_PY_GENERATOR_PEND_THROW (0) #define MICROPY_PY_MATH (0) -#define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_FRAMEBUF (0) #define MICROPY_PY_USOCKET (0) #define MICROPY_PY_NETWORK (0) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 5545a74f57cc5..03b8ceb1f6f9d 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -87,7 +87,6 @@ #define MICROPY_PY_MATH_ISCLOSE (MICROPY_PY_MATH_SPECIAL_FUNCTIONS) #define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO_IOBASE (1) -#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_PY_UERRNO (1) diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index d6e5fc47262e9..2e2c1de0cc87e 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -86,7 +86,6 @@ #define MICROPY_PY_MATH (0) #define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (0) -#define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_STRUCT (0) #define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_EXIT (0) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 4ed9f316e7bbf..8967242aee5fc 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -125,7 +125,6 @@ #define MICROPY_PY_MATH_ISCLOSE (MICROPY_PY_MATH_SPECIAL_FUNCTIONS) #define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO_IOBASE (1) -#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_GC_COLLECT_RETVAL (1) #ifndef MICROPY_STACKLESS #define MICROPY_STACKLESS (0) diff --git a/py/mpconfig.h b/py/mpconfig.h index 1ed34bb6fdae0..d478c654d021d 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1321,11 +1321,6 @@ typedef double mp_float_t; #define MICROPY_PY_IO_IOBASE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) #endif -// Whether to provide "io.FileIO" class -#ifndef MICROPY_PY_IO_FILEIO -#define MICROPY_PY_IO_FILEIO (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) -#endif - // Whether to provide "io.BytesIO" class #ifndef MICROPY_PY_IO_BYTESIO #define MICROPY_PY_IO_BYTESIO (1) From a311e9e3d419cf447c8a4cf2beed4a341e8387e3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 18 Aug 2022 12:32:10 +1000 Subject: [PATCH 2490/3533] tools/mpremote: Allow + terminator for fs commands. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 5 +++++ tools/mpremote/mpremote/main.py | 34 ++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 2bbed56b89144..80290657f4c35 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -30,6 +30,9 @@ For REPL access, running ``mpremote`` without any arguments is usually all that is needed. ``mpremote`` also supports a set of commands given at the command line which will perform various actions on remote MicroPython devices. +For commands that support multiple arguments (e.g. a list of files), the +argument list can be terminated with ``+``. + The full list of supported commands are: - connect to a specified device via a device-name shortcut: @@ -248,3 +251,5 @@ Examples mpremote cp main.py : mpremote cp -r dir/ : + + mpremote cp a.py b.py : + repl diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index e0266fd71901c..73cd5e153bc73 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -300,6 +300,19 @@ def show_progress_bar(size, total_size): ) +# Get all args up to the terminator ("+"). +# The passed args will be updated with these ones removed. +def get_fs_args(args): + n = 0 + for src in args: + if src == "+": + break + n += 1 + fs_args = args[:n] + args[:] = args[n + 1 :] + return fs_args + + def do_filesystem(pyb, args): def _list_recursive(files, path): if os.path.isdir(path): @@ -308,16 +321,18 @@ def _list_recursive(files, path): else: files.append(os.path.split(path)) + fs_args = get_fs_args(args) + # Don't be verbose when using cat, so output can be redirected to something. - verbose = args[0] != "cat" + verbose = fs_args[0] != "cat" - if args[0] == "cp" and args[1] == "-r": - args.pop(0) - args.pop(0) - assert args[-1] == ":" - args.pop() + if fs_args[0] == "cp" and fs_args[1] == "-r": + fs_args.pop(0) + fs_args.pop(0) + assert fs_args[-1] == ":" + fs_args.pop() src_files = [] - for path in args: + for path in fs_args: _list_recursive(src_files, path) known_dirs = {""} pyb.exec_("import uos") @@ -335,8 +350,9 @@ def _list_recursive(files, path): verbose=verbose, ) else: - pyboard.filesystem_command(pyb, args, progress_callback=show_progress_bar, verbose=verbose) - args.clear() + pyboard.filesystem_command( + pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose + ) def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject): From 263737ecfea6ec1049ef3aff37913866ebd463e5 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 18 Aug 2022 12:34:15 +1000 Subject: [PATCH 2491/3533] tools/pyboard.py: Add "touch" filesystem command. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 5 +++-- docs/reference/pyboard.py.rst | 5 +++-- tools/pyboard.py | 8 ++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 80290657f4c35..3927c9badf1f5 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -131,6 +131,7 @@ The full list of supported commands are: - ``rm `` to remove files on the device - ``mkdir `` to create directories on the device - ``rmdir `` to remove directories on the device + - ``touch `` to create the files (if they don't already exist) - mount the local directory on the remote device: @@ -192,8 +193,8 @@ Shortcuts can be defined using the macro system. Built-in shortcuts are:: - ``c0``, ``c1``, ``c2``, ``c3``: connect to COM? -- ``cat``, ``ls``, ``cp``, ``rm``, ``mkdir``, ``rmdir``, ``df``: filesystem - commands +- ``cat``, ``ls``, ``cp``, ``rm``, ``mkdir``, ``rmdir``, ``touch``, ``df``: + filesystem commands - ``reset``: reset the device diff --git a/docs/reference/pyboard.py.rst b/docs/reference/pyboard.py.rst index 4fedbb7aab9a3..a06ffdcd8fa96 100644 --- a/docs/reference/pyboard.py.rst +++ b/docs/reference/pyboard.py.rst @@ -92,12 +92,13 @@ Filesystem access Using the ``-f`` flag, the following filesystem operations are supported: -* ``cp src [src...] dest`` Copy files to/from the device. * ``cat path`` Print the contents of a file on the device. +* ``cp src [src...] dest`` Copy files to/from the device. * ``ls [path]`` List contents of a directory (defaults to current working directory). -* ``rm path`` Remove a file. * ``mkdir path`` Create a directory. +* ``rm path`` Remove a file. * ``rmdir path`` Remove a directory. +* ``touch path`` Create a file if it doesn't already exist. The ``cp`` command uses a ``ssh``-like convention for referring to local and remote files. Any path starting with a ``:`` will be interpreted as on the diff --git a/tools/pyboard.py b/tools/pyboard.py index 7d0ab6bcd703c..7525049875879 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -543,6 +543,9 @@ def fs_rmdir(self, dir): def fs_rm(self, src): self.exec_("import uos\nuos.remove('%s')" % src) + def fs_touch(self, src): + self.exec_("f=open('%s','a')\nf.close()" % src) + # in Python2 exec is a keyword so one must use "exec_" # but for Python3 we want to provide the nicer version "exec" @@ -595,11 +598,12 @@ def fname_cp_dest(src, dest): op(src, dest2, progress_callback=progress_callback) else: op = { - "ls": pyb.fs_ls, "cat": pyb.fs_cat, + "ls": pyb.fs_ls, "mkdir": pyb.fs_mkdir, - "rmdir": pyb.fs_rmdir, "rm": pyb.fs_rm, + "rmdir": pyb.fs_rmdir, + "touch": pyb.fs_touch, }[cmd] if cmd == "ls" and not args: args = [""] From 59e3348c101efee4071dd2224d01ccce6075b692 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 18 Aug 2022 12:36:42 +1000 Subject: [PATCH 2492/3533] tools/mpremote: Add "edit" command. This allows a remote file to be edited locally by copying it over, running the local editor, then copying it back. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 11 +++++++++++ tools/mpremote/mpremote/main.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 3927c9badf1f5..eb0233bddae48 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -133,6 +133,17 @@ The full list of supported commands are: - ``rmdir `` to remove directories on the device - ``touch `` to create the files (if they don't already exist) +- edit a file on the device: + + .. code-block:: bash + + $ mpremote edit + + The ``edit`` command will copy each file from the device to a local temporary + directory and then launch your editor for each file (defined by the environment + variable ``$EDITOR``). If the editor exits successfully, the updated file will + be copied back to the device. + - mount the local directory on the remote device: .. code-block:: bash diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index 73cd5e153bc73..e614156dbf78c 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -19,6 +19,7 @@ import os, sys from collections.abc import Mapping +import tempfile from textwrap import dedent import serial.tools.list_ports @@ -28,6 +29,7 @@ _PROG = "mpremote" +# (need_raw_repl, is_action, num_args_min, help_text) _COMMANDS = { "connect": ( False, @@ -39,6 +41,7 @@ or any valid device name/path""", ), "disconnect": (False, False, 0, "disconnect current device"), + "edit": (True, True, 1, "edit files on the device"), "resume": (False, False, 0, "resume a previous mpremote session (will not auto soft-reset)"), "soft-reset": (False, True, 0, "perform a soft-reset of the device"), "mount": ( @@ -82,6 +85,7 @@ "ls": "fs ls", "cp": "fs cp", "rm": "fs rm", + "touch": "fs touch", "mkdir": "fs mkdir", "rmdir": "fs rmdir", "df": [ @@ -355,6 +359,23 @@ def _list_recursive(files, path): ) +def do_edit(pyb, args): + if not os.getenv("EDITOR"): + raise pyboard.PyboardError("edit: $EDITOR not set") + for src in get_fs_args(args): + src = src.lstrip(":") + dest_fd, dest = tempfile.mkstemp(suffix=os.path.basename(src)) + try: + print("edit :%s" % (src,)) + os.close(dest_fd) + pyb.fs_touch(src) + pyb.fs_get(src, dest, progress_callback=show_progress_bar) + if os.system("$EDITOR '%s'" % (dest,)) == 0: + pyb.fs_put(dest, src, progress_callback=show_progress_bar) + finally: + os.unlink(dest) + + def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject): while True: console_in.waitchar(pyb.serial) @@ -587,6 +608,8 @@ def main(): return ret elif cmd == "fs": do_filesystem(pyb, args) + elif cmd == "edit": + do_edit(pyb, args) elif cmd == "repl": do_repl(pyb, args) From 470a44bd3ade45fd10d63aabfb14c3696b60e251 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Aug 2022 16:47:02 +1000 Subject: [PATCH 2493/3533] extmod/modframebuf: Optimise argument handling. Several methods extract mp_int_t from adjacent arguments. This reduces code size for the repeated calls to mp_obj_get_int. Signed-off-by: Jim Mussared --- extmod/modframebuf.c | 165 ++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 88 deletions(-) diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 5b6575d5a6503..fe32e44a26261 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -263,21 +263,21 @@ STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, u formats[fb->format].fill_rect(fb, x, y, xend - x, yend - y, col); } -STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) { mp_arg_check_num(n_args, n_kw, 4, 5, false); mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, type); - o->buf_obj = args[0]; + o->buf_obj = args_in[0]; mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE); + mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE); o->buf = bufinfo.buf; - o->width = mp_obj_get_int(args[1]); - o->height = mp_obj_get_int(args[2]); - o->format = mp_obj_get_int(args[3]); + o->width = mp_obj_get_int(args_in[1]); + o->height = mp_obj_get_int(args_in[2]); + o->format = mp_obj_get_int(args_in[3]); if (n_args >= 5) { - o->stride = mp_obj_get_int(args[4]); + o->stride = mp_obj_get_int(args_in[4]); } else { o->stride = o->width; } @@ -305,6 +305,12 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size return MP_OBJ_FROM_PTR(o); } +STATIC void framebuf_args(const mp_obj_t *args_in, mp_int_t *args_out, int n) { + for (int i = 0; i < n; ++i) { + args_out[i] = mp_obj_get_int(args_in[i + 1]); + } +} + STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { (void)flags; mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in); @@ -322,98 +328,71 @@ STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill); -STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) { - (void)n_args; - - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x = mp_obj_get_int(args[1]); - mp_int_t y = mp_obj_get_int(args[2]); - mp_int_t width = mp_obj_get_int(args[3]); - mp_int_t height = mp_obj_get_int(args[4]); - mp_int_t col = mp_obj_get_int(args[5]); - - fill_rect(self, x, y, width, height, col); - +STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[5]; // x, y, w, h, col + framebuf_args(args_in, args, 5); + fill_rect(self, args[0], args[1], args[2], args[3], args[4]); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_fill_rect_obj, 6, 6, framebuf_fill_rect); -STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args) { - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x = mp_obj_get_int(args[1]); - mp_int_t y = mp_obj_get_int(args[2]); +STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t x = mp_obj_get_int(args_in[1]); + mp_int_t y = mp_obj_get_int(args_in[2]); if (0 <= x && x < self->width && 0 <= y && y < self->height) { if (n_args == 3) { // get return MP_OBJ_NEW_SMALL_INT(getpixel(self, x, y)); } else { // set - setpixel(self, x, y, mp_obj_get_int(args[3])); + setpixel(self, x, y, mp_obj_get_int(args_in[3])); } } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pixel); -STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args_in) { (void)n_args; - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x = mp_obj_get_int(args[1]); - mp_int_t y = mp_obj_get_int(args[2]); - mp_int_t w = mp_obj_get_int(args[3]); - mp_int_t col = mp_obj_get_int(args[4]); + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[4]; // x, y, w, col + framebuf_args(args_in, args, 4); - fill_rect(self, x, y, w, 1, col); + fill_rect(self, args[0], args[1], args[2], 1, args[3]); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hline); -STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args_in) { (void)n_args; - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x = mp_obj_get_int(args[1]); - mp_int_t y = mp_obj_get_int(args[2]); - mp_int_t h = mp_obj_get_int(args[3]); - mp_int_t col = mp_obj_get_int(args[4]); + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[4]; // x, y, h, col + framebuf_args(args_in, args, 4); - fill_rect(self, x, y, 1, h, col); + fill_rect(self, args[0], args[1], 1, args[2], args[3]); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vline); -STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args) { - (void)n_args; - - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x = mp_obj_get_int(args[1]); - mp_int_t y = mp_obj_get_int(args[2]); - mp_int_t w = mp_obj_get_int(args[3]); - mp_int_t h = mp_obj_get_int(args[4]); - mp_int_t col = mp_obj_get_int(args[5]); - - fill_rect(self, x, y, w, 1, col); - fill_rect(self, x, y + h - 1, w, 1, col); - fill_rect(self, x, y, 1, h, col); - fill_rect(self, x + w - 1, y, 1, h, col); - +STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[5]; // x, y, w, h, col + framebuf_args(args_in, args, 5); + fill_rect(self, args[0], args[1], args[2], 1, args[4]); + fill_rect(self, args[0], args[1] + args[3] - 1, args[2], 1, args[4]); + fill_rect(self, args[0], args[1], 1, args[3], args[4]); + fill_rect(self, args[0] + args[2] - 1, args[1], 1, args[3], args[4]); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rect); -STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) { - (void)n_args; - - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t x1 = mp_obj_get_int(args[1]); - mp_int_t y1 = mp_obj_get_int(args[2]); - mp_int_t x2 = mp_obj_get_int(args[3]); - mp_int_t y2 = mp_obj_get_int(args[4]); - mp_int_t col = mp_obj_get_int(args[5]); - +STATIC void line(const mp_obj_framebuf_t *fb, mp_int_t x1, mp_int_t y1, mp_int_t x2, mp_int_t y2, mp_int_t col) { mp_int_t dx = x2 - x1; mp_int_t sx; if (dx > 0) { @@ -452,12 +431,12 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) { mp_int_t e = 2 * dy - dx; for (mp_int_t i = 0; i < dx; ++i) { if (steep) { - if (0 <= y1 && y1 < self->width && 0 <= x1 && x1 < self->height) { - setpixel(self, y1, x1, col); + if (0 <= y1 && y1 < fb->width && 0 <= x1 && x1 < fb->height) { + setpixel(fb, y1, x1, col); } } else { - if (0 <= x1 && x1 < self->width && 0 <= y1 && y1 < self->height) { - setpixel(self, x1, y1, col); + if (0 <= x1 && x1 < fb->width && 0 <= y1 && y1 < fb->height) { + setpixel(fb, x1, y1, col); } } while (e >= 0) { @@ -468,31 +447,41 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) { e += 2 * dy; } - if (0 <= x2 && x2 < self->width && 0 <= y2 && y2 < self->height) { - setpixel(self, x2, y2, col); + if (0 <= x2 && x2 < fb->width && 0 <= y2 && y2 < fb->height) { + setpixel(fb, x2, y2, col); } +} + +STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args_in) { + (void)n_args; + + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[5]; // x1, y1, x2, y2, col + framebuf_args(args_in, args, 5); + + line(self, args[0], args[1], args[2], args[3], args[4]); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line); -STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) { - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - mp_obj_t source_in = mp_obj_cast_to_native_base(args[1], MP_OBJ_FROM_PTR(&mp_type_framebuf)); +STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_obj_t source_in = mp_obj_cast_to_native_base(args_in[1], MP_OBJ_FROM_PTR(&mp_type_framebuf)); if (source_in == MP_OBJ_NULL) { mp_raise_TypeError(NULL); } mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(source_in); - mp_int_t x = mp_obj_get_int(args[2]); - mp_int_t y = mp_obj_get_int(args[3]); + mp_int_t x = mp_obj_get_int(args_in[2]); + mp_int_t y = mp_obj_get_int(args_in[3]); mp_int_t key = -1; if (n_args > 4) { - key = mp_obj_get_int(args[4]); + key = mp_obj_get_int(args_in[4]); } mp_obj_framebuf_t *palette = NULL; - if (n_args > 5 && args[5] != mp_const_none) { - palette = MP_OBJ_TO_PTR(mp_obj_cast_to_native_base(args[5], MP_OBJ_FROM_PTR(&mp_type_framebuf))); + if (n_args > 5 && args_in[5] != mp_const_none) { + palette = MP_OBJ_TO_PTR(mp_obj_cast_to_native_base(args_in[5], MP_OBJ_FROM_PTR(&mp_type_framebuf))); } if ( @@ -563,15 +552,15 @@ STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ys } STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll); -STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) { // extract arguments - mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]); - const char *str = mp_obj_str_get_str(args[1]); - mp_int_t x0 = mp_obj_get_int(args[2]); - mp_int_t y0 = mp_obj_get_int(args[3]); + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + const char *str = mp_obj_str_get_str(args_in[1]); + mp_int_t x0 = mp_obj_get_int(args_in[2]); + mp_int_t y0 = mp_obj_get_int(args_in[3]); mp_int_t col = 1; if (n_args >= 5) { - col = mp_obj_get_int(args[4]); + col = mp_obj_get_int(args_in[4]); } // loop over chars @@ -626,18 +615,18 @@ STATIC const mp_obj_type_t mp_type_framebuf = { #endif // this factory function is provided for backwards compatibility with old FrameBuffer1 class -STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args_in) { mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, &mp_type_framebuf); mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE); + mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE); o->buf = bufinfo.buf; - o->width = mp_obj_get_int(args[1]); - o->height = mp_obj_get_int(args[2]); + o->width = mp_obj_get_int(args_in[1]); + o->height = mp_obj_get_int(args_in[2]); o->format = FRAMEBUF_MVLSB; if (n_args >= 4) { - o->stride = mp_obj_get_int(args[3]); + o->stride = mp_obj_get_int(args_in[3]); } else { o->stride = o->width; } From af1f167820e685390d43dd7e250a0ffa078e138d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 13 Aug 2022 00:46:00 +1000 Subject: [PATCH 2494/3533] py/dynruntime: Add mp_obj_is_true. Signed-off-by: Jim Mussared --- py/dynruntime.h | 1 + 1 file changed, 1 insertion(+) diff --git a/py/dynruntime.h b/py/dynruntime.h index e3200a271954e..fb748eb93fe78 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -127,6 +127,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) { #define mp_obj_str_get_data(o, len) (mp_obj_str_get_data_dyn((o), (len))) #define mp_get_buffer_raise(o, bufinfo, fl) (mp_fun_table.get_buffer_raise((o), (bufinfo), (fl))) #define mp_get_stream_raise(s, flags) (mp_fun_table.get_stream_raise((s), (flags))) +#define mp_obj_is_true(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_BOOL)) #define mp_obj_len(o) (mp_obj_len_dyn(o)) #define mp_obj_subscr(base, index, val) (mp_fun_table.obj_subscr((base), (index), (val))) From 127b340438cddd55748e066cacbc1ab64131e232 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Aug 2022 16:34:51 +1000 Subject: [PATCH 2495/3533] extmod/modframebuf: Add fill argument to rect(). We plan to add `ellipse` and `poly` methods, but rather than having to implement a `fill_xyz` version of each, we can make them take an optional fill argument. This commit add this to `rect` as a starting point. Signed-off-by: Jim Mussared --- docs/library/framebuf.rst | 10 +++++----- extmod/modframebuf.c | 14 +++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/library/framebuf.rst b/docs/library/framebuf.rst index 098ada8153847..7024616653422 100644 --- a/docs/library/framebuf.rst +++ b/docs/library/framebuf.rst @@ -77,12 +77,12 @@ The following methods draw shapes onto the FrameBuffer. methods draw horizontal and vertical lines respectively up to a given length. -.. method:: FrameBuffer.rect(x, y, w, h, c) -.. method:: FrameBuffer.fill_rect(x, y, w, h, c) +.. method:: FrameBuffer.rect(x, y, w, h, c[, f]) - Draw a rectangle at the given location, size and color. The `rect` - method draws only a 1 pixel outline whereas the `fill_rect` method - draws both the outline and interior. + Draw a rectangle at the given location, size and color. + + The optional *f* parameter can be set to ``True`` to fill the rectangle. + Otherwise just a one pixel outline is drawn. Drawing text ------------ diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index fe32e44a26261..590bd085459b7 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -384,13 +384,17 @@ STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args_in) { mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); mp_int_t args[5]; // x, y, w, h, col framebuf_args(args_in, args, 5); - fill_rect(self, args[0], args[1], args[2], 1, args[4]); - fill_rect(self, args[0], args[1] + args[3] - 1, args[2], 1, args[4]); - fill_rect(self, args[0], args[1], 1, args[3], args[4]); - fill_rect(self, args[0] + args[2] - 1, args[1], 1, args[3], args[4]); + if (n_args > 6 && mp_obj_is_true(args_in[6])) { + fill_rect(self, args[0], args[1], args[2], args[3], args[4]); + } else { + fill_rect(self, args[0], args[1], args[2], 1, args[4]); + fill_rect(self, args[0], args[1] + args[3] - 1, args[2], 1, args[4]); + fill_rect(self, args[0], args[1], 1, args[3], args[4]); + fill_rect(self, args[0] + args[2] - 1, args[1], 1, args[3], args[4]); + } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rect); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 7, framebuf_rect); STATIC void line(const mp_obj_framebuf_t *fb, mp_int_t x1, mp_int_t y1, mp_int_t x2, mp_int_t y2, mp_int_t col) { mp_int_t dx = x2 - x1; From 42ec9703a07d1d0b55091f5557ff5f81c5134fb8 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Wed, 10 Aug 2022 14:51:19 +0100 Subject: [PATCH 2496/3533] extmod/modframebuf: Add ellipse drawing method. --- docs/library/framebuf.rst | 18 +- examples/natmod/framebuf/framebuf.c | 9 +- extmod/modframebuf.c | 95 ++++ tests/extmod/framebuf_ellipse.py | 65 +++ tests/extmod/framebuf_ellipse.py.exp | 704 +++++++++++++++++++++++++++ 5 files changed, 885 insertions(+), 6 deletions(-) create mode 100644 tests/extmod/framebuf_ellipse.py create mode 100644 tests/extmod/framebuf_ellipse.py.exp diff --git a/docs/library/framebuf.rst b/docs/library/framebuf.rst index 7024616653422..1e23abd0f1b50 100644 --- a/docs/library/framebuf.rst +++ b/docs/library/framebuf.rst @@ -11,8 +11,8 @@ class FrameBuffer ----------------- The FrameBuffer class provides a pixel buffer which can be drawn upon with -pixels, lines, rectangles, text and even other FrameBuffer's. It is useful -when generating output for displays. +pixels, lines, rectangles, ellipses, text and even other FrameBuffers. It is +useful when generating output for displays. For example:: @@ -84,6 +84,20 @@ The following methods draw shapes onto the FrameBuffer. The optional *f* parameter can be set to ``True`` to fill the rectangle. Otherwise just a one pixel outline is drawn. +.. method:: FrameBuffer.ellipse(x, y, xr, yr, c[, f, m]) + + Draw an ellipse at the given location. Radii *xr* and *yr* define the + geometry; equal values cause a circle to be drawn. The *c* parameter + defines the color. + + The optional *f* parameter can be set to ``True`` to fill the ellipse. + Otherwise just a one pixel outline is drawn. + + The optional *m* parameter enables drawing to be restricted to certain + quadrants of the ellipse. The LS four bits determine which quadrants are + to be drawn, with bit 0 specifying Q1, b1 Q2, b2 Q3 and b3 Q4. Quadrants + are numbered counterclockwise with Q1 being top right. + Drawing text ------------ diff --git a/examples/natmod/framebuf/framebuf.c b/examples/natmod/framebuf/framebuf.c index 8d488cffd685a..53fb90c625ada 100644 --- a/examples/natmod/framebuf/framebuf.c +++ b/examples/natmod/framebuf/framebuf.c @@ -12,7 +12,7 @@ mp_obj_type_t mp_type_framebuf; #include "extmod/modframebuf.c" -mp_map_elem_t framebuf_locals_dict_table[10]; +mp_map_elem_t framebuf_locals_dict_table[11]; STATIC MP_DEFINE_CONST_DICT(framebuf_locals_dict, framebuf_locals_dict_table); mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) { @@ -29,9 +29,10 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a framebuf_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_vline), MP_OBJ_FROM_PTR(&framebuf_vline_obj) }; framebuf_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_rect), MP_OBJ_FROM_PTR(&framebuf_rect_obj) }; framebuf_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_line), MP_OBJ_FROM_PTR(&framebuf_line_obj) }; - framebuf_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_blit), MP_OBJ_FROM_PTR(&framebuf_blit_obj) }; - framebuf_locals_dict_table[8] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_scroll), MP_OBJ_FROM_PTR(&framebuf_scroll_obj) }; - framebuf_locals_dict_table[9] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_text), MP_OBJ_FROM_PTR(&framebuf_text_obj) }; + framebuf_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_ellipse), MP_OBJ_FROM_PTR(&framebuf_ellipse_obj) }; + framebuf_locals_dict_table[8] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_blit), MP_OBJ_FROM_PTR(&framebuf_blit_obj) }; + framebuf_locals_dict_table[9] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_scroll), MP_OBJ_FROM_PTR(&framebuf_scroll_obj) }; + framebuf_locals_dict_table[10] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_text), MP_OBJ_FROM_PTR(&framebuf_text_obj) }; mp_type_framebuf.locals_dict = (void*)&framebuf_locals_dict; mp_store_global(MP_QSTR_FrameBuffer, MP_OBJ_FROM_PTR(&mp_type_framebuf)); diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 590bd085459b7..0fa91942c1a79 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -469,6 +469,100 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line); +STATIC void ellipse_pixel(const mp_obj_framebuf_t *fb, mp_int_t x, mp_int_t y, mp_int_t col, mp_int_t mask) { + if (mask && 0 <= x && x < fb->width && 0 <= y && y < fb->height) { + setpixel(fb, x, y, col); + } +} + +// Q2 Q1 +// Q3 Q4 +#define ELLIPSE_MASK_FILL (0x10) +#define ELLIPSE_MASK_ALL (0x0f) +#define ELLIPSE_MASK_Q1 (0x01) +#define ELLIPSE_MASK_Q2 (0x02) +#define ELLIPSE_MASK_Q3 (0x04) +#define ELLIPSE_MASK_Q4 (0x08) + +STATIC void draw_ellipse_points(const mp_obj_framebuf_t *fb, mp_int_t cx, mp_int_t cy, mp_int_t x, mp_int_t y, mp_int_t col, mp_int_t mask) { + if (mask & ELLIPSE_MASK_FILL) { + if (mask & ELLIPSE_MASK_Q1) { + fill_rect(fb, cx, cy - y, x + 1, 1, col); + } + if (mask & ELLIPSE_MASK_Q2) { + fill_rect(fb, cx - x, cy - y, x + 1, 1, col); + } + if (mask & ELLIPSE_MASK_Q3) { + fill_rect(fb, cx - x, cy + y, x + 1, 1, col); + } + if (mask & ELLIPSE_MASK_Q4) { + fill_rect(fb, cx, cy + y, x + 1, 1, col); + } + } else { + ellipse_pixel(fb, cx + x, cy - y, col, mask & ELLIPSE_MASK_Q1); + ellipse_pixel(fb, cx - x, cy - y, col, mask & ELLIPSE_MASK_Q2); + ellipse_pixel(fb, cx - x, cy + y, col, mask & ELLIPSE_MASK_Q3); + ellipse_pixel(fb, cx + x, cy + y, col, mask & ELLIPSE_MASK_Q4); + } +} + +STATIC mp_obj_t framebuf_ellipse(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + mp_int_t args[5]; + framebuf_args(args_in, args, 5); // cx, cy, xradius, yradius, col + mp_int_t mask = (n_args > 6 && mp_obj_is_true(args_in[6])) ? ELLIPSE_MASK_FILL : 0; + if (n_args > 7) { + mask |= mp_obj_get_int(args_in[7]) & ELLIPSE_MASK_ALL; + } else { + mask |= ELLIPSE_MASK_ALL; + } + mp_int_t two_asquare = 2 * args[2] * args[2]; + mp_int_t two_bsquare = 2 * args[3] * args[3]; + mp_int_t x = args[2]; + mp_int_t y = 0; + mp_int_t xchange = args[3] * args[3] * (1 - 2 * args[2]); + mp_int_t ychange = args[2] * args[2]; + mp_int_t ellipse_error = 0; + mp_int_t stoppingx = two_bsquare * args[2]; + mp_int_t stoppingy = 0; + while (stoppingx >= stoppingy) { // 1st set of points, y' > -1 + draw_ellipse_points(self, args[0], args[1], x, y, args[4], mask); + y += 1; + stoppingy += two_asquare; + ellipse_error += ychange; + ychange += two_asquare; + if ((2 * ellipse_error + xchange) > 0) { + x -= 1; + stoppingx -= two_bsquare; + ellipse_error += xchange; + xchange += two_bsquare; + } + } + // 1st point set is done start the 2nd set of points + x = 0; + y = args[3]; + xchange = args[3] * args[3]; + ychange = args[2] * args[2] * (1 - 2 * args[3]); + ellipse_error = 0; + stoppingx = 0; + stoppingy = two_asquare * args[3]; + while (stoppingx <= stoppingy) { // 2nd set of points, y' < -1 + draw_ellipse_points(self, args[0], args[1], x, y, args[4], mask); + x += 1; + stoppingx += two_bsquare; + ellipse_error += xchange; + xchange += two_bsquare; + if ((2 * ellipse_error + ychange) > 0) { + y -= 1; + stoppingy -= two_asquare; + ellipse_error += ychange; + ychange += two_asquare; + } + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_ellipse_obj, 6, 8, framebuf_ellipse); + STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args_in) { mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); mp_obj_t source_in = mp_obj_cast_to_native_base(args_in[1], MP_OBJ_FROM_PTR(&mp_type_framebuf)); @@ -603,6 +697,7 @@ STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_vline), MP_ROM_PTR(&framebuf_vline_obj) }, { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&framebuf_rect_obj) }, { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&framebuf_line_obj) }, + { MP_ROM_QSTR(MP_QSTR_ellipse), MP_ROM_PTR(&framebuf_ellipse_obj) }, { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&framebuf_blit_obj) }, { MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&framebuf_scroll_obj) }, { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&framebuf_text_obj) }, diff --git a/tests/extmod/framebuf_ellipse.py b/tests/extmod/framebuf_ellipse.py new file mode 100644 index 0000000000000..a4c784aff8762 --- /dev/null +++ b/tests/extmod/framebuf_ellipse.py @@ -0,0 +1,65 @@ +try: + import framebuf +except ImportError: + print("SKIP") + raise SystemExit + + +def printbuf(): + print("--8<--") + for y in range(h): + for x in range(w): + print("%02x" % buf[(x + y * w)], end="") + print() + print("-->8--") + + +w = 30 +h = 30 +buf = bytearray(w * h) +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8) + +# Outline +fbuf.fill(0) +fbuf.ellipse(15, 15, 12, 6, 0xFF, False) +printbuf() + +# Fill +fbuf.fill(0) +fbuf.ellipse(15, 15, 6, 12, 0xAA, True) +printbuf() + +# Outline and fill some different quadrant combos. +for m in (0, 0b0001, 0b0010, 0b0100, 0b1000, 0b1010): + fbuf.fill(0) + fbuf.ellipse(15, 15, 6, 12, 0xAA, False, m) + printbuf() + fbuf.fill(0) + fbuf.ellipse(15, 15, 6, 12, 0xAA, True, m) + printbuf() + +# Draw ellipses that will go out of bounds at each of the edges. +for x, y in ( + ( + 4, + 4, + ), + ( + 26, + 4, + ), + ( + 26, + 26, + ), + ( + 4, + 26, + ), +): + fbuf.fill(0) + fbuf.ellipse(x, y, 6, 12, 0xAA, False) + printbuf() + fbuf.fill(0) + fbuf.ellipse(x, y, 6, 12, 0xAA, True) + printbuf() diff --git a/tests/extmod/framebuf_ellipse.py.exp b/tests/extmod/framebuf_ellipse.py.exp new file mode 100644 index 0000000000000..ae6ad1ee7e487 --- /dev/null +++ b/tests/extmod/framebuf_ellipse.py.exp @@ -0,0 +1,704 @@ +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000ffffffffffffffffff00000000000000000000 +0000000000000000ffffff000000000000000000ffffff00000000000000 +000000000000ffff000000000000000000000000000000ffff0000000000 +0000000000ff00000000000000000000000000000000000000ff00000000 +00000000ff000000000000000000000000000000000000000000ff000000 +000000ff0000000000000000000000000000000000000000000000ff0000 +000000ff0000000000000000000000000000000000000000000000ff0000 +000000ff0000000000000000000000000000000000000000000000ff0000 +00000000ff000000000000000000000000000000000000000000ff000000 +0000000000ff00000000000000000000000000000000000000ff00000000 +000000000000ffff000000000000000000000000000000ffff0000000000 +0000000000000000ffffff000000000000000000ffffff00000000000000 +0000000000000000000000ffffffffffffffffff00000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000aaaaaa00000000000000000000000000 +00000000000000000000000000aaaaaaaaaa000000000000000000000000 +000000000000000000000000aaaaaaaaaaaaaa0000000000000000000000 +0000000000000000000000aaaaaaaaaaaaaaaaaa00000000000000000000 +0000000000000000000000aaaaaaaaaaaaaaaaaa00000000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +00000000000000000000aaaaaaaaaaaaaaaaaaaaaa000000000000000000 +0000000000000000000000aaaaaaaaaaaaaaaaaa00000000000000000000 +0000000000000000000000aaaaaaaaaaaaaaaaaa00000000000000000000 +000000000000000000000000aaaaaaaaaaaaaa0000000000000000000000 +00000000000000000000000000aaaaaaaaaa000000000000000000000000 +0000000000000000000000000000aaaaaa00000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +0000000000000000000000000000000000aa000000000000000000000000 +000000000000000000000000000000000000aa0000000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +000000000000000000000000000000aaaaaa000000000000000000000000 +000000000000000000000000000000aaaaaaaa0000000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +00000000000000000000000000aa00000000000000000000000000000000 +000000000000000000000000aa0000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +00000000000000000000000000aaaaaa0000000000000000000000000000 +000000000000000000000000aaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +000000000000000000000000aa0000000000000000000000000000000000 +00000000000000000000000000aa00000000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +000000000000000000000000aaaaaaaa0000000000000000000000000000 +00000000000000000000000000aaaaaa0000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +000000000000000000000000000000000000aa0000000000000000000000 +0000000000000000000000000000000000aa000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaa0000000000000000000000 +000000000000000000000000000000aaaaaa000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +00000000000000000000000000aa00000000000000000000000000000000 +000000000000000000000000aa0000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +0000000000000000000000aa000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +00000000000000000000000000000000000000aa00000000000000000000 +000000000000000000000000000000000000aa0000000000000000000000 +0000000000000000000000000000000000aa000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000aaaa0000000000000000000000000000 +00000000000000000000000000aaaaaa0000000000000000000000000000 +000000000000000000000000aaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +0000000000000000000000aaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +00000000000000000000aaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaa0000000000000000000000000000 +000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaaaa0000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaaaa000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaaaa00000000000000000000 +000000000000000000000000000000aaaaaaaa0000000000000000000000 +000000000000000000000000000000aaaaaa000000000000000000000000 +000000000000000000000000000000aaaa00000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +aa00000000000000aa000000000000000000000000000000000000000000 +aa00000000000000aa000000000000000000000000000000000000000000 +00aa0000000000aa00000000000000000000000000000000000000000000 +0000aa000000aa0000000000000000000000000000000000000000000000 +000000aaaaaa000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaa000000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaa000000000000000000000000000000000000000000 +00aaaaaaaaaaaaaa00000000000000000000000000000000000000000000 +0000aaaaaaaaaa0000000000000000000000000000000000000000000000 +000000aaaaaa000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +00000000000000000000000000000000000000000000aa00000000000000 +00000000000000000000000000000000000000000000aa00000000000000 +0000000000000000000000000000000000000000000000aa0000000000aa +000000000000000000000000000000000000000000000000aa000000aa00 +00000000000000000000000000000000000000000000000000aaaaaa0000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +00000000000000000000000000000000000000000000aaaaaaaaaaaaaaaa +00000000000000000000000000000000000000000000aaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000000000aaaaaaaaaaaaaa +000000000000000000000000000000000000000000000000aaaaaaaaaa00 +00000000000000000000000000000000000000000000000000aaaaaa0000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000aaaaaa0000 +000000000000000000000000000000000000000000000000aa000000aa00 +0000000000000000000000000000000000000000000000aa0000000000aa +00000000000000000000000000000000000000000000aa00000000000000 +00000000000000000000000000000000000000000000aa00000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +000000000000000000000000000000000000000000aa0000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +0000000000000000000000000000000000000000aa000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000aaaaaa0000 +000000000000000000000000000000000000000000000000aaaaaaaaaa00 +0000000000000000000000000000000000000000000000aaaaaaaaaaaaaa +00000000000000000000000000000000000000000000aaaaaaaaaaaaaaaa +00000000000000000000000000000000000000000000aaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +0000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaa +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000aaaaaa000000000000000000000000000000000000000000000000 +0000aa000000aa0000000000000000000000000000000000000000000000 +00aa0000000000aa00000000000000000000000000000000000000000000 +aa00000000000000aa000000000000000000000000000000000000000000 +aa00000000000000aa000000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +000000000000000000aa0000000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +00000000000000000000aa00000000000000000000000000000000000000 +-->8-- +--8<-- +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000 +000000aaaaaa000000000000000000000000000000000000000000000000 +0000aaaaaaaaaa0000000000000000000000000000000000000000000000 +00aaaaaaaaaaaaaa00000000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaa000000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaa000000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +aaaaaaaaaaaaaaaaaaaaaa00000000000000000000000000000000000000 +-->8-- From 04a655c74488128a2c1af9ba8f29fce5e5bbfef9 Mon Sep 17 00:00:00 2001 From: Mat Booth Date: Wed, 13 Jul 2022 21:09:51 +0100 Subject: [PATCH 2497/3533] extmod/modframebuf: Add polygon drawing methods. Add method for drawing polygons. For non-filled polygons, uses the existing line-drawing code to render arbitrary polygons using the given coords list, at the given x,y position, in the given colour. For filled polygons, arbitrary closed polygons are rendered using a fast point-in-polygon algorithm to determine where the edges of the polygon lie on each pixel row. Tests and documentation updates are also included. Signed-off-by: Mat Booth --- docs/library/framebuf.rst | 15 +- extmod/modframebuf.c | 114 ++++++ tests/extmod/framebuf_polygon.py | 222 ++++++++++ tests/extmod/framebuf_polygon.py.exp | 582 +++++++++++++++++++++++++++ 4 files changed, 931 insertions(+), 2 deletions(-) create mode 100644 tests/extmod/framebuf_polygon.py create mode 100644 tests/extmod/framebuf_polygon.py.exp diff --git a/docs/library/framebuf.rst b/docs/library/framebuf.rst index 1e23abd0f1b50..78ae0c1c346b7 100644 --- a/docs/library/framebuf.rst +++ b/docs/library/framebuf.rst @@ -11,8 +11,8 @@ class FrameBuffer ----------------- The FrameBuffer class provides a pixel buffer which can be drawn upon with -pixels, lines, rectangles, ellipses, text and even other FrameBuffers. It is -useful when generating output for displays. +pixels, lines, rectangles, ellipses, polygons, text and even other +FrameBuffers. It is useful when generating output for displays. For example:: @@ -98,6 +98,17 @@ The following methods draw shapes onto the FrameBuffer. to be drawn, with bit 0 specifying Q1, b1 Q2, b2 Q3 and b3 Q4. Quadrants are numbered counterclockwise with Q1 being top right. +.. method:: FrameBuffer.poly(x, y, coords, c[, f]) + + Given a list of coordinates, draw an arbitrary (convex or concave) closed + polygon at the given x, y location using the given color. + + The *coords* must be specified as a :mod:`array` of integers, e.g. + ``array('h', [x0, y0, x1, y1, ... xn, yn])``. + + The optional *f* parameter can be set to ``True`` to fill the polygon. + Otherwise just a one pixel outline is drawn. + Drawing text ------------ diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 0fa91942c1a79..f17bea70b9675 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -28,6 +28,7 @@ #include #include "py/runtime.h" +#include "py/binary.h" #if MICROPY_PY_FRAMEBUF @@ -563,6 +564,116 @@ STATIC mp_obj_t framebuf_ellipse(size_t n_args, const mp_obj_t *args_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_ellipse_obj, 6, 8, framebuf_ellipse); +#if MICROPY_PY_ARRAY && !MICROPY_ENABLE_DYNRUNTIME +// TODO: poly needs mp_binary_get_size & mp_binary_get_val_array which aren't +// available in dynruntime.h yet. + +STATIC mp_int_t poly_int(mp_buffer_info_t *bufinfo, size_t index) { + return mp_obj_get_int(mp_binary_get_val_array(bufinfo->typecode, bufinfo->buf, index)); +} + +STATIC mp_obj_t framebuf_poly(size_t n_args, const mp_obj_t *args_in) { + mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); + + mp_int_t x = mp_obj_get_int(args_in[1]); + mp_int_t y = mp_obj_get_int(args_in[2]); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args_in[3], &bufinfo, MP_BUFFER_READ); + // If an odd number of values was given, this rounds down to multiple of two. + int n_poly = bufinfo.len / (mp_binary_get_size('@', bufinfo.typecode, NULL) * 2); + + if (n_poly == 0) { + return mp_const_none; + } + + mp_int_t col = mp_obj_get_int(args_in[4]); + bool fill = n_args > 5 && mp_obj_is_true(args_in[5]); + + if (fill) { + // This implements an integer version of http://alienryderflex.com/polygon_fill/ + + // The idea is for each scan line, compute the sorted list of x + // coordinates where the scan line intersects the polygon edges, + // then fill between each resulting pair. + + // Restrict just to the scan lines that include the vertical extent of + // this polygon. + mp_int_t y_min = INT_MAX, y_max = INT_MIN; + for (int i = 0; i < n_poly; i++) { + mp_int_t py = poly_int(&bufinfo, i * 2 + 1); + y_min = MIN(y_min, py); + y_max = MAX(y_max, py); + } + + for (mp_int_t row = y_min; row <= y_max; row++) { + // Each node is the x coordinate where an edge crosses this scan line. + mp_int_t nodes[n_poly]; + int n_nodes = 0; + mp_int_t px1 = poly_int(&bufinfo, 0); + mp_int_t py1 = poly_int(&bufinfo, 1); + int i = n_poly * 2 - 1; + do { + mp_int_t py2 = poly_int(&bufinfo, i--); + mp_int_t px2 = poly_int(&bufinfo, i--); + + // Don't include the bottom pixel of a given edge to avoid + // duplicating the node with the start of the next edge. This + // will miss some pixels on the boundary, but we get them at + // the end when we unconditionally draw the outline. + if (py1 != py2 && ((py1 > row && py2 <= row) || (py1 <= row && py2 > row))) { + mp_int_t node = (32 * px1 + 32 * (px2 - px1) * (row - py1) / (py2 - py1) + 16) / 32; + nodes[n_nodes++] = node; + } + + px1 = px2; + py1 = py2; + } while (i >= 0); + + if (!n_nodes) { + continue; + } + + // Sort the nodes left-to-right (bubble-sort for code size). + i = 0; + while (i < n_nodes - 1) { + if (nodes[i] > nodes[i + 1]) { + mp_int_t swap = nodes[i]; + nodes[i] = nodes[i + 1]; + nodes[i + 1] = swap; + if (i) { + i--; + } + } else { + i++; + } + } + + // Fill between each pair of nodes. + for (i = 0; i < n_nodes; i += 2) { + fill_rect(self, x + nodes[i], y + row, (nodes[i + 1] - nodes[i]) + 1, 1, col); + } + } + } + + // Always draw the outline (either because fill=False, or to fix the + // boundary pixels for a fill, see above). + mp_int_t px1 = poly_int(&bufinfo, 0); + mp_int_t py1 = poly_int(&bufinfo, 1); + int i = n_poly * 2 - 1; + do { + mp_int_t py2 = poly_int(&bufinfo, i--); + mp_int_t px2 = poly_int(&bufinfo, i--); + line(self, x + px1, y + py1, x + px2, y + py2, col); + px1 = px2; + py1 = py2; + } while (i >= 0); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_poly_obj, 5, 6, framebuf_poly); +#endif // MICROPY_PY_ARRAY && !MICROPY_ENABLE_DYNRUNTIME + STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args_in) { mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]); mp_obj_t source_in = mp_obj_cast_to_native_base(args_in[1], MP_OBJ_FROM_PTR(&mp_type_framebuf)); @@ -698,6 +809,9 @@ STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&framebuf_rect_obj) }, { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&framebuf_line_obj) }, { MP_ROM_QSTR(MP_QSTR_ellipse), MP_ROM_PTR(&framebuf_ellipse_obj) }, + #if MICROPY_PY_ARRAY + { MP_ROM_QSTR(MP_QSTR_poly), MP_ROM_PTR(&framebuf_poly_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&framebuf_blit_obj) }, { MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&framebuf_scroll_obj) }, { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&framebuf_text_obj) }, diff --git a/tests/extmod/framebuf_polygon.py b/tests/extmod/framebuf_polygon.py new file mode 100644 index 0000000000000..03130b3bf0fab --- /dev/null +++ b/tests/extmod/framebuf_polygon.py @@ -0,0 +1,222 @@ +import sys + +try: + import framebuf + from array import array +except ImportError: + print("SKIP") + raise SystemExit + + +# TODO: poly needs functions that aren't in dynruntime.h yet. +if not hasattr(framebuf.FrameBuffer, "poly"): + print("SKIP") + raise SystemExit + + +def print_buffer(buffer, width, height): + for row in range(height): + for col in range(width): + val = buffer[(row * width) + col] + sys.stdout.write(" {:02x}".format(val) if val else " ··") + sys.stdout.write("\n") + + +buf = bytearray(70 * 70) + +w = 30 +h = 25 +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8) +col = 0xFF +col_fill = 0x99 + +# This describes a arbitrary polygon (this happens to be a concave polygon in +# the shape of an upper-case letter 'M'). +poly = array( + "h", + ( + 0, + 20, + 3, + 20, + 3, + 10, + 6, + 17, + 9, + 10, + 9, + 20, + 12, + 20, + 12, + 3, + 9, + 3, + 6, + 10, + 3, + 3, + 0, + 3, + ), +) +# This describes the same polygon, but the points are in reverse order +# (it shouldn't matter if the polygon has clockwise or anti-clockwise +# winding). Also defined as a bytes instead of array. +poly_reversed = bytes( + ( + 0, + 3, + 3, + 3, + 6, + 10, + 9, + 3, + 12, + 3, + 12, + 20, + 9, + 20, + 9, + 10, + 6, + 17, + 3, + 10, + 3, + 20, + 0, + 20, + ) +) + +# Draw the line polygon (at the origin) and the reversed-order polygon (offset). +fbuf.fill(0) +fbuf.poly(0, 0, poly, col) +fbuf.poly(15, -2, poly_reversed, col) +print_buffer(buf, w, h) +print() + +# Same but filled. +fbuf.fill(0) +fbuf.poly(0, 0, poly, col_fill, True) +fbuf.poly(15, -2, poly_reversed, col_fill, True) +print_buffer(buf, w, h) +print() + +# Draw the fill then the outline to ensure that no fill goes outside the outline. +fbuf.fill(0) +fbuf.poly(0, 0, poly, col_fill, True) +fbuf.poly(0, 0, poly, col) +fbuf.poly(15, -2, poly, col_fill, True) +fbuf.poly(15, -2, poly, col) +print_buffer(buf, w, h) +print() + +# Draw the outline then the fill to ensure the fill completely covers the outline. +fbuf.fill(0) +fbuf.poly(0, 0, poly, col) +fbuf.poly(0, 0, poly, col_fill, True) +fbuf.poly(15, -2, poly, col) +fbuf.poly(15, -2, poly, col_fill, True) +print_buffer(buf, w, h) +print() + +# Draw polygons that will go out of bounds at each of the edges. +for x, y in ( + ( + -8, + -8, + ), + ( + 24, + -6, + ), + ( + 20, + 12, + ), + ( + -2, + 10, + ), +): + fbuf.fill(0) + fbuf.poly(x, y, poly, col) + print_buffer(buf, w, h) + print() + fbuf.fill(0) + fbuf.poly(x, y, poly_reversed, col, True) + print_buffer(buf, w, h) + print() + +# Edge cases: These two lists describe self-intersecting polygons +poly_hourglass = array("h", (0, 0, 9, 0, 0, 19, 9, 19)) +poly_star = array("h", (7, 0, 3, 18, 14, 5, 0, 5, 11, 18)) + +# As before, fill then outline. +fbuf.fill(0) +fbuf.poly(0, 2, poly_hourglass, col_fill, True) +fbuf.poly(0, 2, poly_hourglass, col) +fbuf.poly(12, 2, poly_star, col_fill, True) +fbuf.poly(12, 2, poly_star, col) +print_buffer(buf, w, h) +print() + +# Outline then fill. +fbuf.fill(0) +fbuf.poly(0, 2, poly_hourglass, col) +fbuf.poly(0, 2, poly_hourglass, col_fill, True) +fbuf.poly(12, 2, poly_star, col) +fbuf.poly(12, 2, poly_star, col_fill, True) +print_buffer(buf, w, h) +print() + +# Edge cases: These are "degenerate" polygons. +poly_empty = array("h") # Will draw nothing at all. +poly_one = array("h", (20, 20)) # Will draw a single point. +poly_two = array("h", (10, 10, 5, 5)) # Will draw a single line. +poly_wrong_length = array("h", (2, 2, 4)) # Will round down to one point. + +fbuf.fill(0) +fbuf.poly(0, 0, poly_empty, col) +fbuf.poly(0, 0, poly_one, col) +fbuf.poly(0, 0, poly_two, col) +fbuf.poly(0, 0, poly_wrong_length, col) +print_buffer(buf, w, h) +print() + +# A shape with a horizontal overhang. +poly_overhang = array("h", (0, 0, 0, 5, 5, 5, 5, 10, 10, 10, 10, 0)) + +fbuf.fill(0) +fbuf.poly(0, 0, poly_overhang, col) +fbuf.poly(0, 0, poly_overhang, col_fill, True) +print_buffer(buf, w, h) +print() + +fbuf.fill(0) +fbuf.poly(0, 0, poly_overhang, col_fill, True) +fbuf.poly(0, 0, poly_overhang, col) +print_buffer(buf, w, h) +print() + +# Triangles +w = 70 +h = 70 +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8) +t1 = array("h", [40, 0, 20, 68, 62, 40]) +t2 = array("h", [40, 0, 0, 16, 20, 68]) + +fbuf.fill(0) +fbuf.poly(0, 0, t1, 0xFF, False) +fbuf.poly(0, 0, t2, 0xFF, False) +print_buffer(buf, w, h) + +fbuf.fill(0) +fbuf.poly(0, 0, t1, 0xFF, True) +fbuf.poly(0, 0, t2, 0xFF, True) +print_buffer(buf, w, h) diff --git a/tests/extmod/framebuf_polygon.py.exp b/tests/extmod/framebuf_polygon.py.exp new file mode 100644 index 0000000000000..05cd4264ea8b4 --- /dev/null +++ b/tests/extmod/framebuf_polygon.py.exp @@ -0,0 +1,582 @@ + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· + ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· + ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· + ff ·· ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· + ff ·· ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· + ff ·· ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ff ·· ff ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ff ·· ff ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ff ·· ff ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ff ·· ff ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ff ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· + 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· + ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ff 99 99 99 ff ·· ·· ·· ff 99 99 99 ff ·· ·· + ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· ff 99 99 99 ff ·· ·· ·· ff 99 99 99 ff ·· ·· + ff 99 99 99 ff ·· ·· ·· ff 99 99 99 ff ·· ·· ff 99 99 99 99 ff ·· ff 99 99 99 99 ff ·· ·· + ff 99 99 99 ff ·· ·· ·· ff 99 99 99 ff ·· ·· ff 99 99 99 99 ff ·· ff 99 99 99 99 ff ·· ·· + ff 99 99 99 99 ff ·· ff 99 99 99 99 ff ·· ·· ff 99 99 99 99 99 ff 99 99 99 99 99 ff ·· ·· + ff 99 99 99 99 ff ·· ff 99 99 99 99 ff ·· ·· ff 99 99 ff 99 99 ff 99 99 ff 99 99 ff ·· ·· + ff 99 99 99 99 99 ff 99 99 99 99 99 ff ·· ·· ff 99 99 ff 99 99 99 99 99 ff 99 99 ff ·· ·· + ff 99 99 ff 99 99 ff 99 99 ff 99 99 ff ·· ·· ff 99 99 ff ff 99 99 99 ff ff 99 99 ff ·· ·· + ff 99 99 ff 99 99 99 99 99 ff 99 99 ff ·· ·· ff 99 99 ff ff 99 99 99 ff ff 99 99 ff ·· ·· + ff 99 99 ff ff 99 99 99 ff ff 99 99 ff ·· ·· ff 99 99 ff ·· ff 99 ff ·· ff 99 99 ff ·· ·· + ff 99 99 ff ff 99 99 99 ff ff 99 99 ff ·· ·· ff 99 99 ff ·· ff 99 ff ·· ff 99 99 ff ·· ·· + ff 99 99 ff ·· ff 99 ff ·· ff 99 99 ff ·· ·· ff 99 99 ff ·· ·· ff ·· ·· ff 99 99 ff ·· ·· + ff 99 99 ff ·· ff 99 ff ·· ff 99 99 ff ·· ·· ff 99 99 ff ·· ·· ff ·· ·· ff 99 99 ff ·· ·· + ff 99 99 ff ·· ·· ff ·· ·· ff 99 99 ff ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· + ff 99 99 ff ·· ·· ff ·· ·· ff 99 99 ff ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· + ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ff 99 99 ff ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· + 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 ·· ·· ·· 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· 99 99 99 ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· 99 ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff ·· ff ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ff ·· ·· ·· ff ff + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· ·· ·· ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· ·· ·· ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ·· ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ·· ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ff ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ·· ·· ·· ff ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ff ·· ff ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ·· ·· ·· ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ·· ·· ·· ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ·· ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ·· ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ff ff ff ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· + ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ·· ff 99 99 99 99 ff ·· ff 99 99 99 99 ff ·· ·· ·· ·· + ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ·· ·· ff 99 99 ff ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ·· ·· ff 99 ff ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ·· ·· ff 99 ff ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ff 99 ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff 99 99 ff 99 99 ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ff 99 ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ff 99 ff ·· ff 99 ff ·· ·· ·· ·· ·· ·· ·· + ·· ff 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ff 99 ff ·· ·· ·· ff 99 ff ·· ·· ·· ·· ·· ·· + ·· ff 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· + ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· 99 99 99 99 99 99 ·· ·· ·· ·· + ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· + ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· ·· ·· 99 99 99 ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· ·· ·· 99 99 99 ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· 99 99 99 ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· + ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· 99 99 99 ·· ·· ·· ·· ·· ·· ·· + ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· 99 99 99 ·· 99 99 99 ·· ·· ·· ·· ·· ·· ·· + ·· 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· 99 99 99 ·· ·· ·· 99 99 99 ·· ·· ·· ·· ·· ·· + ·· 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· 99 99 ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· ·· 99 ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + 99 99 99 99 99 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· 99 99 99 99 99 99 ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff 99 99 99 99 99 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff 99 99 99 99 ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ff ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ff ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ff ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ff ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· From c616721b1afe176c8d760afc15a93621d411c1dc Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Aug 2022 09:56:26 +1000 Subject: [PATCH 2498/3533] extmod/modframebuf: Improve poly-fill boundary pixels. Rather than drawing the entire boundary to catch missing pixels, just detect the cases where boundary pixels are skipped during node calculation and pre-emptively draw them then. This adds 72 bytes on PYBV11, but makes filled poly() 20% faster. Signed-off-by: Jim Mussared --- extmod/modframebuf.c | 65 ++++++++++++++++------------ tests/extmod/framebuf_polygon.py.exp | 32 +++++++------- 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index f17bea70b9675..bb1f4f6310774 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -241,11 +241,17 @@ STATIC mp_framebuf_p_t formats[] = { [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect}, }; -static inline void setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { +STATIC inline void setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) { formats[fb->format].setpixel(fb, x, y, col); } -static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { +STATIC void setpixel_checked(const mp_obj_framebuf_t *fb, mp_int_t x, mp_int_t y, mp_int_t col, mp_int_t mask) { + if (mask && 0 <= x && x < fb->width && 0 <= y && y < fb->height) { + setpixel(fb, x, y, col); + } +} + +STATIC inline uint32_t getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) { return formats[fb->format].getpixel(fb, x, y); } @@ -470,12 +476,6 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line); -STATIC void ellipse_pixel(const mp_obj_framebuf_t *fb, mp_int_t x, mp_int_t y, mp_int_t col, mp_int_t mask) { - if (mask && 0 <= x && x < fb->width && 0 <= y && y < fb->height) { - setpixel(fb, x, y, col); - } -} - // Q2 Q1 // Q3 Q4 #define ELLIPSE_MASK_FILL (0x10) @@ -500,10 +500,10 @@ STATIC void draw_ellipse_points(const mp_obj_framebuf_t *fb, mp_int_t cx, mp_int fill_rect(fb, cx, cy + y, x + 1, 1, col); } } else { - ellipse_pixel(fb, cx + x, cy - y, col, mask & ELLIPSE_MASK_Q1); - ellipse_pixel(fb, cx - x, cy - y, col, mask & ELLIPSE_MASK_Q2); - ellipse_pixel(fb, cx - x, cy + y, col, mask & ELLIPSE_MASK_Q3); - ellipse_pixel(fb, cx + x, cy + y, col, mask & ELLIPSE_MASK_Q4); + setpixel_checked(fb, cx + x, cy - y, col, mask & ELLIPSE_MASK_Q1); + setpixel_checked(fb, cx - x, cy - y, col, mask & ELLIPSE_MASK_Q2); + setpixel_checked(fb, cx - x, cy + y, col, mask & ELLIPSE_MASK_Q3); + setpixel_checked(fb, cx + x, cy + y, col, mask & ELLIPSE_MASK_Q4); } } @@ -619,11 +619,23 @@ STATIC mp_obj_t framebuf_poly(size_t n_args, const mp_obj_t *args_in) { // Don't include the bottom pixel of a given edge to avoid // duplicating the node with the start of the next edge. This - // will miss some pixels on the boundary, but we get them at - // the end when we unconditionally draw the outline. + // will miss some pixels on the boundary, and in particular + // at a local minima or inflection point. if (py1 != py2 && ((py1 > row && py2 <= row) || (py1 <= row && py2 > row))) { mp_int_t node = (32 * px1 + 32 * (px2 - px1) * (row - py1) / (py2 - py1) + 16) / 32; nodes[n_nodes++] = node; + } else if (row == MAX(py1, py2)) { + // At local-minima, try and manually fill in the pixels that get missed above. + if (py1 < py2) { + setpixel_checked(self, x + px2, y + py2, col, 1); + } else if (py2 < py1) { + setpixel_checked(self, x + px1, y + py1, col, 1); + } else { + // Even though this is a hline and would be faster to + // use fill_rect, use line() because it handles x2 < + // x1. + line(self, x + px1, y + py1, x + px2, y + py2, col); + } } px1 = px2; @@ -654,21 +666,20 @@ STATIC mp_obj_t framebuf_poly(size_t n_args, const mp_obj_t *args_in) { fill_rect(self, x + nodes[i], y + row, (nodes[i + 1] - nodes[i]) + 1, 1, col); } } + } else { + // Outline only. + mp_int_t px1 = poly_int(&bufinfo, 0); + mp_int_t py1 = poly_int(&bufinfo, 1); + int i = n_poly * 2 - 1; + do { + mp_int_t py2 = poly_int(&bufinfo, i--); + mp_int_t px2 = poly_int(&bufinfo, i--); + line(self, x + px1, y + py1, x + px2, y + py2, col); + px1 = px2; + py1 = py2; + } while (i >= 0); } - // Always draw the outline (either because fill=False, or to fix the - // boundary pixels for a fill, see above). - mp_int_t px1 = poly_int(&bufinfo, 0); - mp_int_t py1 = poly_int(&bufinfo, 1); - int i = n_poly * 2 - 1; - do { - mp_int_t py2 = poly_int(&bufinfo, i--); - mp_int_t px2 = poly_int(&bufinfo, i--); - line(self, x + px1, y + py1, x + px2, y + py2, col); - px1 = px2; - py1 = py2; - } while (i >= 0); - return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_poly_obj, 5, 6, framebuf_poly); diff --git a/tests/extmod/framebuf_polygon.py.exp b/tests/extmod/framebuf_polygon.py.exp index 05cd4264ea8b4..9b4801c788a5c 100644 --- a/tests/extmod/framebuf_polygon.py.exp +++ b/tests/extmod/framebuf_polygon.py.exp @@ -510,22 +510,22 @@ ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· - ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· + ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· From af54d2ce9f923eba06b19bddb784a1c9da228347 Mon Sep 17 00:00:00 2001 From: "Nicholas H.Tollervey" Date: Fri, 19 Aug 2022 14:58:58 +0100 Subject: [PATCH 2499/3533] javascript: Rename this port to 'webassembly'. --- ...ports_javascript.yml => ports_webassembly.yml} | 10 +++++----- ports/{javascript => webassembly}/Makefile | 2 +- ports/{javascript => webassembly}/README.md | 15 ++++++++++----- ports/{javascript => webassembly}/library.h | 0 ports/{javascript => webassembly}/library.js | 0 ports/{javascript => webassembly}/main.c | 0 ports/{javascript => webassembly}/modutime.c | 0 ports/{javascript => webassembly}/mpconfigport.h | 2 +- ports/{javascript => webassembly}/mphalport.c | 0 ports/{javascript => webassembly}/mphalport.h | 0 ports/{javascript => webassembly}/node_run.sh | 0 ports/{javascript => webassembly}/qstrdefsport.h | 0 ports/{javascript => webassembly}/wrapper.js | 0 tools/ci.sh | 12 ++++++------ 14 files changed, 23 insertions(+), 18 deletions(-) rename .github/workflows/{ports_javascript.yml => ports_webassembly.yml} (57%) rename ports/{javascript => webassembly}/Makefile (93%) rename ports/{javascript => webassembly}/README.md (84%) rename ports/{javascript => webassembly}/library.h (100%) rename ports/{javascript => webassembly}/library.js (100%) rename ports/{javascript => webassembly}/main.c (100%) rename ports/{javascript => webassembly}/modutime.c (100%) rename ports/{javascript => webassembly}/mpconfigport.h (99%) rename ports/{javascript => webassembly}/mphalport.c (100%) rename ports/{javascript => webassembly}/mphalport.h (100%) rename ports/{javascript => webassembly}/node_run.sh (100%) rename ports/{javascript => webassembly}/qstrdefsport.h (100%) rename ports/{javascript => webassembly}/wrapper.js (100%) diff --git a/.github/workflows/ports_javascript.yml b/.github/workflows/ports_webassembly.yml similarity index 57% rename from .github/workflows/ports_javascript.yml rename to .github/workflows/ports_webassembly.yml index 244dc966aa6d9..861039c0fcffd 100644 --- a/.github/workflows/ports_javascript.yml +++ b/.github/workflows/ports_webassembly.yml @@ -1,4 +1,4 @@ -name: javascript port +name: webassembly port on: push: @@ -9,7 +9,7 @@ on: - 'py/**' - 'extmod/**' - 'lib/**' - - 'ports/javascript/**' + - 'ports/webassembly/**' jobs: build: @@ -17,8 +17,8 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install packages - run: source tools/ci.sh && ci_javascript_setup + run: source tools/ci.sh && ci_webassembly_setup - name: Build - run: source tools/ci.sh && ci_javascript_build + run: source tools/ci.sh && ci_webassembly_build - name: Run tests - run: source tools/ci.sh && ci_javascript_run_tests + run: source tools/ci.sh && ci_webassembly_run_tests diff --git a/ports/javascript/Makefile b/ports/webassembly/Makefile similarity index 93% rename from ports/javascript/Makefile rename to ports/webassembly/Makefile index 93c7b5609b23b..db3f36ad8eb13 100644 --- a/ports/javascript/Makefile +++ b/ports/webassembly/Makefile @@ -51,6 +51,6 @@ min: $(BUILD)/micropython.js test: $(BUILD)/micropython.js $(TOP)/tests/run-tests.py $(eval DIRNAME=ports/$(notdir $(CURDIR))) - cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests.py -j1 + cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/webassembly/node_run.sh ./run-tests.py -j1 include $(TOP)/py/mkrules.mk diff --git a/ports/javascript/README.md b/ports/webassembly/README.md similarity index 84% rename from ports/javascript/README.md rename to ports/webassembly/README.md index 39808943b1043..3b577233fffe5 100644 --- a/ports/javascript/README.md +++ b/ports/webassembly/README.md @@ -1,13 +1,18 @@ -MicroPython.js -============== +MicroPython WebAssembly +======================= -MicroPython transmuted into Javascript by Emscripten. +MicroPython for [WebAssembly](https://webassembly.org/). Dependencies ------------ -Building micropython.js bears the same requirements as the standard MicroPython -ports with the addition of Emscripten (and uglify-js for the minified file). +Building webassembly port bears the same requirements as the standard +MicroPython ports with the addition of Emscripten (and uglify-js for the +minified file). + +The output includes `micropython.js` (a JavaScript wrapper for the +MicroPython runtime) and `firmware.wasm` (actual MicroPython compiled to +WASM). Build instructions ------------------ diff --git a/ports/javascript/library.h b/ports/webassembly/library.h similarity index 100% rename from ports/javascript/library.h rename to ports/webassembly/library.h diff --git a/ports/javascript/library.js b/ports/webassembly/library.js similarity index 100% rename from ports/javascript/library.js rename to ports/webassembly/library.js diff --git a/ports/javascript/main.c b/ports/webassembly/main.c similarity index 100% rename from ports/javascript/main.c rename to ports/webassembly/main.c diff --git a/ports/javascript/modutime.c b/ports/webassembly/modutime.c similarity index 100% rename from ports/javascript/modutime.c rename to ports/webassembly/modutime.c diff --git a/ports/javascript/mpconfigport.h b/ports/webassembly/mpconfigport.h similarity index 99% rename from ports/javascript/mpconfigport.h rename to ports/webassembly/mpconfigport.h index 6c86d816bd4fe..4fbd922e15642 100644 --- a/ports/javascript/mpconfigport.h +++ b/ports/webassembly/mpconfigport.h @@ -105,7 +105,7 @@ #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1) -#define MICROPY_PY_SYS_PLATFORM "javascript" +#define MICROPY_PY_SYS_PLATFORM "webassembly" #define MICROPY_PY_UERRNO (1) #define MICROPY_PY_UCTYPES (1) #define MICROPY_PY_UZLIB (1) diff --git a/ports/javascript/mphalport.c b/ports/webassembly/mphalport.c similarity index 100% rename from ports/javascript/mphalport.c rename to ports/webassembly/mphalport.c diff --git a/ports/javascript/mphalport.h b/ports/webassembly/mphalport.h similarity index 100% rename from ports/javascript/mphalport.h rename to ports/webassembly/mphalport.h diff --git a/ports/javascript/node_run.sh b/ports/webassembly/node_run.sh similarity index 100% rename from ports/javascript/node_run.sh rename to ports/webassembly/node_run.sh diff --git a/ports/javascript/qstrdefsport.h b/ports/webassembly/qstrdefsport.h similarity index 100% rename from ports/javascript/qstrdefsport.h rename to ports/webassembly/qstrdefsport.h diff --git a/ports/javascript/wrapper.js b/ports/webassembly/wrapper.js similarity index 100% rename from ports/javascript/wrapper.js rename to ports/webassembly/wrapper.js diff --git a/tools/ci.sh b/tools/ci.sh index e3203f8bfb9f2..dc2ccf775b15e 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -172,21 +172,21 @@ function ci_esp8266_build { } ######################################################################################## -# ports/javascript +# ports/webassembly -function ci_javascript_setup { +function ci_webassembly_setup { git clone https://github.com/emscripten-core/emsdk.git (cd emsdk && ./emsdk install latest && ./emsdk activate latest) } -function ci_javascript_build { +function ci_webassembly_build { source emsdk/emsdk_env.sh - make ${MAKEOPTS} -C ports/javascript + make ${MAKEOPTS} -C ports/webassembly } -function ci_javascript_run_tests { +function ci_webassembly_run_tests { # This port is very slow at running, so only run a few of the tests. - (cd tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests.py -j1 basics/builtin_*.py) + (cd tests && MICROPY_MICROPYTHON=../ports/webassembly/node_run.sh ./run-tests.py -j1 basics/builtin_*.py) } ######################################################################################## From 7c8ec85fa34279950e44072b22ac26fe41b91886 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Aug 2022 13:02:40 +1000 Subject: [PATCH 2500/3533] shared/runtime/sys_stdio_mphal: Make func static and remove some TODOs. stdio_obj_print is private to this file so can be made static. The __del__ method does nothing so can be removed (it's only called by the GC if it exists, so if it doesn't exist it won't be called). And FileIO doesn't support a constructor in MicroPython at this stage. Signed-off-by: Damien George --- shared/runtime/sys_stdio_mphal.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/shared/runtime/sys_stdio_mphal.c b/shared/runtime/sys_stdio_mphal.c index e72facb981d9c..24f528b0c4f85 100644 --- a/shared/runtime/sys_stdio_mphal.c +++ b/shared/runtime/sys_stdio_mphal.c @@ -52,7 +52,7 @@ typedef struct _sys_stdio_obj_t { STATIC const sys_stdio_obj_t stdio_buffer_obj; #endif -void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "", self->fd); } @@ -100,8 +100,6 @@ STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__); -// TODO gc hook to close the file if not already closed - STATIC const mp_rom_map_elem_t stdio_locals_dict_table[] = { #if MICROPY_PY_SYS_STDIO_BUFFER { MP_ROM_QSTR(MP_QSTR_buffer), MP_ROM_PTR(&stdio_buffer_obj) }, @@ -112,7 +110,6 @@ STATIC const mp_rom_map_elem_t stdio_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj)}, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_identity_obj) }, - { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_identity_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stdio_obj___exit___obj) }, }; @@ -129,7 +126,6 @@ STATIC const mp_stream_p_t stdio_obj_stream_p = { STATIC const mp_obj_type_t stdio_obj_type = { { &mp_type_type }, .name = MP_QSTR_FileIO, - // TODO .make_new? .print = stdio_obj_print, .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, From 3d65101a8a55550bdbaa4df4e216edba238fa299 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Aug 2022 13:09:57 +1000 Subject: [PATCH 2501/3533] py: Clean up formatting of union definitions. Signed-off-by: Damien George --- py/binary.c | 22 +++++++++++++--------- py/obj.h | 8 +++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/py/binary.c b/py/binary.c index f59e89ca48226..4c8b6ffcdc49a 100644 --- a/py/binary.c +++ b/py/binary.c @@ -241,13 +241,15 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte * return mp_obj_new_str(s_val, strlen(s_val)); #if MICROPY_PY_BUILTINS_FLOAT } else if (val_type == 'f') { - union { uint32_t i; - float f; + union { + uint32_t i; + float f; } fpu = {val}; return mp_obj_new_float_from_f(fpu.f); } else if (val_type == 'd') { - union { uint64_t i; - double f; + union { + uint64_t i; + double f; } fpu = {val}; return mp_obj_new_float_from_d(fpu.f); #endif @@ -308,17 +310,19 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p break; #if MICROPY_PY_BUILTINS_FLOAT case 'f': { - union { uint32_t i; - float f; + union { + uint32_t i; + float f; } fp_sp; fp_sp.f = mp_obj_get_float_to_f(val_in); val = fp_sp.i; break; } case 'd': { - union { uint64_t i64; - uint32_t i32[2]; - double f; + union { + uint64_t i64; + uint32_t i32[2]; + double f; } fp_dp; fp_dp.f = mp_obj_get_float_to_d(val_in); if (MP_BYTES_PER_OBJ_WORD == 8) { diff --git a/py/obj.h b/py/obj.h index 598d6508da737..645fae79f5665 100644 --- a/py/obj.h +++ b/py/obj.h @@ -283,9 +283,11 @@ static inline bool mp_obj_is_obj(mp_const_obj_t o) { #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p))) // rom object storage needs special handling to widen 32-bit pointer to 64-bits -typedef union _mp_rom_obj_t { uint64_t u64; - struct { const void *lo, *hi; - } u32; +typedef union _mp_rom_obj_t { + uint64_t u64; + struct { + const void *lo, *hi; + } u32; } mp_rom_obj_t; #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)} #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)} From d8ad87843ab38dcc74e7e86354849fff585e1ba8 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 19 Aug 2022 16:40:28 +0200 Subject: [PATCH 2502/3533] py/builtinimport: Allow overriding of mp_builtin___import__. This allows ports to override mp_builtin___import__. This can be useful in MicroPython applications where MICROPY_ENABLE_EXTERNAL_IMPORT has to be disabled due to its impact on build size (2% to 2.5% of the minimal port). By overriding the otherwise very minimal mp_builtin___import__, ports can still allow limited forms of application-specific imports. Signed-off-by: Laurens Valk --- py/builtin.h | 6 ++++++ py/builtinimport.c | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/py/builtin.h b/py/builtin.h index fb11627841227..7232142b77bea 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -64,7 +64,13 @@ MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); #endif +// A port can provide its own import handler by defining mp_builtin___import__. +#ifndef mp_builtin___import__ +#define mp_builtin___import__ mp_builtin___import___default +#endif mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args); +mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args); + mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj); diff --git a/py/builtinimport.c b/py/builtinimport.c index 36cdac07675e3..a578d4ad2732e 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -467,7 +467,7 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, return module_obj; } -mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { +mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) { #if DEBUG_PRINT DEBUG_printf("__import__:\n"); for (size_t i = 0; i < n_args; i++) { @@ -566,7 +566,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #else // MICROPY_ENABLE_EXTERNAL_IMPORT -mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { +mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) { // Check that it's not a relative import if (n_args >= 5 && MP_OBJ_SMALL_INT_VALUE(args[4]) != 0) { mp_raise_NotImplementedError(MP_ERROR_TEXT("relative import")); From b1efc1340af122499e5cf1d7214d51e034d45a20 Mon Sep 17 00:00:00 2001 From: Chris Waggoner Date: Sun, 21 Aug 2022 06:57:12 -0400 Subject: [PATCH 2503/3533] mpy-cross,unix: Include alloca.h for NetBSD. --- mpy-cross/mpconfigport.h | 2 +- ports/unix/mpconfigport.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpy-cross/mpconfigport.h b/mpy-cross/mpconfigport.h index 9d455c0af1f3b..4304c552bc45b 100644 --- a/mpy-cross/mpconfigport.h +++ b/mpy-cross/mpconfigport.h @@ -119,7 +119,7 @@ typedef long mp_off_t; #define MP_PLAT_PRINT_STRN(str, len) (void)0 // We need to provide a declaration/definition of alloca() -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__NetBSD__) #include #elif defined(_WIN32) #include diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 03b8ceb1f6f9d..dd73f61e24ba9 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -294,7 +294,7 @@ static inline unsigned long mp_urandom_seed_init(void) { // We need to provide a declaration/definition of alloca() // unless support for it is disabled. #if !defined(MICROPY_NO_ALLOCA) || MICROPY_NO_ALLOCA == 0 -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__NetBSD__) #include #else #include From 717924001d8da40032fd56aa3ba2207059f550d2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 3 Aug 2022 18:01:11 +0200 Subject: [PATCH 2504/3533] stm32: Switch Wiznet to use lib/wiznet5k and extmod/network_wiznet5k. Instead of the old Wiznet driver in drivers/wiznet5k. --- ports/stm32/Makefile | 17 +- ports/stm32/modnwwiznet5k.c | 513 --------------------------------- ports/stm32/network_wiznet5k.c | 465 ------------------------------ 3 files changed, 10 insertions(+), 985 deletions(-) delete mode 100644 ports/stm32/modnwwiznet5k.c delete mode 100644 ports/stm32/network_wiznet5k.c diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 14a93a5aa7658..7c912f4f0b23a 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -460,19 +460,22 @@ LIBS += $(TOP)/drivers/cyw43/libcyw43.a endif ifneq ($(MICROPY_PY_NETWORK_WIZNET5K),0) -WIZNET5K_DIR=drivers/wiznet5k -INC += -I$(TOP)/$(WIZNET5K_DIR) +WIZNET5K_DIR=lib/wiznet5k +GIT_SUBMODULES += lib/wiznet5k +INC += -I$(TOP)/$(WIZNET5K_DIR) -I$(TOP)/$(WIZNET5K_DIR)/Ethernet CFLAGS_MOD += -DMICROPY_PY_NETWORK_WIZNET5K=$(MICROPY_PY_NETWORK_WIZNET5K) -D_WIZCHIP_=$(MICROPY_PY_NETWORK_WIZNET5K) +CFLAGS_MOD += -DWIZCHIP_PREFIXED_EXPORTS=1 ifeq ($(MICROPY_PY_LWIP),1) # When using MACRAW mode (with lwIP), maximum buffer space must be used for the raw socket CFLAGS_MOD += -DWIZCHIP_USE_MAX_BUFFER endif -SRC_MOD += network_wiznet5k.c modnwwiznet5k.c +SRC_MOD += extmod/network_wiznet5k.c SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\ - ethernet/w$(MICROPY_PY_NETWORK_WIZNET5K)/w$(MICROPY_PY_NETWORK_WIZNET5K).c \ - ethernet/wizchip_conf.c \ - ethernet/socket.c \ - internet/dns/dns.c \ + Ethernet/W$(MICROPY_PY_NETWORK_WIZNET5K)/w$(MICROPY_PY_NETWORK_WIZNET5K).c \ + Ethernet/wizchip_conf.c \ + Ethernet/socket.c \ + Internet/DNS/dns.c \ + Internet/DHCP/dhcp.c \ ) endif diff --git a/ports/stm32/modnwwiznet5k.c b/ports/stm32/modnwwiznet5k.c deleted file mode 100644 index 2d5044d5246b1..0000000000000 --- a/ports/stm32/modnwwiznet5k.c +++ /dev/null @@ -1,513 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2014 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include -#include - -#include "py/objlist.h" -#include "py/runtime.h" -#include "py/stream.h" -#include "py/mperrno.h" -#include "py/mphal.h" -#include "shared/netutils/netutils.h" -#include "extmod/modnetwork.h" -#include "pin.h" -#include "spi.h" - -#if MICROPY_PY_NETWORK_WIZNET5K && !MICROPY_PY_LWIP - -#include "ethernet/wizchip_conf.h" -#include "ethernet/socket.h" -#include "internet/dns/dns.h" - -/// \moduleref network - -typedef struct _wiznet5k_obj_t { - mp_obj_base_t base; - mp_uint_t cris_state; - const spi_t *spi; - const pin_obj_t *cs; - const pin_obj_t *rst; - uint8_t socket_used; -} wiznet5k_obj_t; - -STATIC wiznet5k_obj_t wiznet5k_obj; - -STATIC void wiz_cris_enter(void) { - wiznet5k_obj.cris_state = MICROPY_BEGIN_ATOMIC_SECTION(); -} - -STATIC void wiz_cris_exit(void) { - MICROPY_END_ATOMIC_SECTION(wiznet5k_obj.cris_state); -} - -STATIC void wiz_cs_select(void) { - mp_hal_pin_low(wiznet5k_obj.cs); -} - -STATIC void wiz_cs_deselect(void) { - mp_hal_pin_high(wiznet5k_obj.cs); -} - -STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) { - HAL_StatusTypeDef status = HAL_SPI_Receive(wiznet5k_obj.spi->spi, buf, len, 5000); - (void)status; -} - -STATIC void wiz_spi_write(const uint8_t *buf, uint32_t len) { - HAL_StatusTypeDef status = HAL_SPI_Transmit(wiznet5k_obj.spi->spi, (uint8_t *)buf, len, 5000); - (void)status; -} - -STATIC int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) { - uint8_t dns_ip[MOD_NETWORK_IPADDR_BUF_SIZE] = {8, 8, 8, 8}; - uint8_t *buf = m_new(uint8_t, MAX_DNS_BUF_SIZE); - DNS_init(0, buf); - mp_int_t ret = DNS_run(dns_ip, (uint8_t *)name, out_ip); - m_del(uint8_t, buf, MAX_DNS_BUF_SIZE); - if (ret == 1) { - // success - return 0; - } else { - // failure - return -2; - } -} - -STATIC int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) { - if (socket->domain != MOD_NETWORK_AF_INET) { - *_errno = MP_EAFNOSUPPORT; - return -1; - } - - switch (socket->type) { - case MOD_NETWORK_SOCK_STREAM: - socket->type = Sn_MR_TCP; - break; - case MOD_NETWORK_SOCK_DGRAM: - socket->type = Sn_MR_UDP; - break; - default: - *_errno = MP_EINVAL; - return -1; - } - - if (socket->fileno == -1) { - // get first unused socket number - for (mp_uint_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) { - if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) { - wiznet5k_obj.socket_used |= (1 << sn); - socket->fileno = sn; - break; - } - } - if (socket->fileno == -1) { - // too many open sockets - *_errno = MP_EMFILE; - return -1; - } - } - - // WIZNET does not have a concept of pure "open socket". You need to know - // if it's a server or client at the time of creation of the socket. - // So, we defer the open until we know what kind of socket we want. - - // use "domain" to indicate that this socket has not yet been opened - socket->domain = 0; - - return 0; -} - -STATIC void wiznet5k_socket_close(mod_network_socket_obj_t *socket) { - uint8_t sn = (uint8_t)socket->fileno; - if (sn < _WIZCHIP_SOCK_NUM_) { - wiznet5k_obj.socket_used &= ~(1 << sn); - WIZCHIP_EXPORT(close)(sn); - } -} - -STATIC int wiznet5k_socket_bind(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) { - // open the socket in server mode (if port != 0) - mp_int_t ret = WIZCHIP_EXPORT(socket)(socket->fileno, socket->type, port, 0); - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - - // indicate that this socket has been opened - socket->domain = 1; - - // success - return 0; -} - -STATIC int wiznet5k_socket_listen(mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno) { - mp_int_t ret = WIZCHIP_EXPORT(listen)(socket->fileno); - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - return 0; -} - -STATIC int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno) { - for (;;) { - int sr = getSn_SR((uint8_t)socket->fileno); - if (sr == SOCK_ESTABLISHED) { - socket2->domain = socket->domain; - socket2->type = socket->type; - socket2->fileno = socket->fileno; - getSn_DIPR((uint8_t)socket2->fileno, ip); - *port = getSn_PORT(socket2->fileno); - - // WIZnet turns the listening socket into the client socket, so we - // need to re-bind and re-listen on another socket for the server. - // TODO handle errors, especially no-more-sockets error - socket->domain = MOD_NETWORK_AF_INET; - socket->fileno = -1; - int _errno2; - if (wiznet5k_socket_socket(socket, &_errno2) != 0) { - // printf("(bad resocket %d)\n", _errno2); - } else if (wiznet5k_socket_bind(socket, NULL, *port, &_errno2) != 0) { - // printf("(bad rebind %d)\n", _errno2); - } else if (wiznet5k_socket_listen(socket, 0, &_errno2) != 0) { - // printf("(bad relisten %d)\n", _errno2); - } - - return 0; - } - if (sr == SOCK_CLOSED || sr == SOCK_CLOSE_WAIT) { - wiznet5k_socket_close(socket); - *_errno = MP_ENOTCONN; // ?? - return -1; - } - mp_hal_delay_ms(1); - } -} - -STATIC int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) { - // use "bind" function to open the socket in client mode - if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) { - return -1; - } - - // now connect - MP_THREAD_GIL_EXIT(); - mp_int_t ret = WIZCHIP_EXPORT(connect)(socket->fileno, ip, port); - MP_THREAD_GIL_ENTER(); - - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - - // success - return 0; -} - -STATIC mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) { - MP_THREAD_GIL_EXIT(); - mp_int_t ret = WIZCHIP_EXPORT(send)(socket->fileno, (byte *)buf, len); - MP_THREAD_GIL_ENTER(); - - // TODO convert Wiz errno's to POSIX ones - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - return ret; -} - -STATIC mp_uint_t wiznet5k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) { - MP_THREAD_GIL_EXIT(); - mp_int_t ret = WIZCHIP_EXPORT(recv)(socket->fileno, buf, len); - MP_THREAD_GIL_ENTER(); - - // TODO convert Wiz errno's to POSIX ones - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - return ret; -} - -STATIC mp_uint_t wiznet5k_socket_sendto(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) { - if (socket->domain == 0) { - // socket not opened; use "bind" function to open the socket in client mode - if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) { - return -1; - } - } - - MP_THREAD_GIL_EXIT(); - mp_int_t ret = WIZCHIP_EXPORT(sendto)(socket->fileno, (byte *)buf, len, ip, port); - MP_THREAD_GIL_ENTER(); - - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - return ret; -} - -STATIC mp_uint_t wiznet5k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) { - uint16_t port2; - MP_THREAD_GIL_EXIT(); - mp_int_t ret = WIZCHIP_EXPORT(recvfrom)(socket->fileno, buf, len, ip, &port2); - MP_THREAD_GIL_ENTER(); - *port = port2; - if (ret < 0) { - wiznet5k_socket_close(socket); - *_errno = -ret; - return -1; - } - return ret; -} - -STATIC int wiznet5k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) { - // TODO - *_errno = MP_EINVAL; - return -1; -} - -STATIC int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) { - // TODO - *_errno = MP_EINVAL; - return -1; - - /* - if (timeout_ms == 0) { - // set non-blocking mode - uint8_t arg = SOCK_IO_NONBLOCK; - WIZCHIP_EXPORT(ctlsocket)(socket->fileno, CS_SET_IOMODE, &arg); - } - */ -} - -STATIC int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) { - if (request == MP_STREAM_POLL) { - int ret = 0; - if (arg & MP_STREAM_POLL_RD && getSn_RX_RSR(socket->fileno) != 0) { - ret |= MP_STREAM_POLL_RD; - } - if (arg & MP_STREAM_POLL_WR && getSn_TX_FSR(socket->fileno) != 0) { - ret |= MP_STREAM_POLL_WR; - } - return ret; - } else { - *_errno = MP_EINVAL; - return MP_STREAM_ERROR; - } -} - -#if 0 -STATIC void wiznet5k_socket_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { - wiznet5k_socket_obj_t *self = self_in; - print(env, "", self->sn, getSn_MR(self->sn)); -} - -STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) { - mp_int_t ret = WIZCHIP_EXPORT(disconnect)(self->sn); - return 0; -} -#endif - -/******************************************************************************/ -// MicroPython bindings - -/// \classmethod \constructor(spi, pin_cs, pin_rst) -/// Create and return a WIZNET5K object. -STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 3, 3, false); - - // init the wiznet5k object - wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k; - wiznet5k_obj.cris_state = 0; - wiznet5k_obj.spi = spi_from_mp_obj(args[0]); - wiznet5k_obj.cs = pin_find(args[1]); - wiznet5k_obj.rst = pin_find(args[2]); - wiznet5k_obj.socket_used = 0; - - /*!< SPI configuration */ - SPI_InitTypeDef *init = &wiznet5k_obj.spi->spi->Init; - init->Mode = SPI_MODE_MASTER; - init->Direction = SPI_DIRECTION_2LINES; - init->DataSize = SPI_DATASIZE_8BIT; - init->CLKPolarity = SPI_POLARITY_LOW; // clock is low when idle - init->CLKPhase = SPI_PHASE_1EDGE; // data latched on first edge, which is rising edge for low-idle - init->NSS = SPI_NSS_SOFT; - init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz - init->FirstBit = SPI_FIRSTBIT_MSB; - init->TIMode = SPI_TIMODE_DISABLED; - init->CRCCalculation = SPI_CRCCALCULATION_DISABLED; - init->CRCPolynomial = 7; // unused - spi_init(wiznet5k_obj.spi, false); - - mp_hal_pin_output(wiznet5k_obj.cs); - mp_hal_pin_output(wiznet5k_obj.rst); - - mp_hal_pin_low(wiznet5k_obj.rst); - mp_hal_delay_ms(1); // datasheet says 2us - mp_hal_pin_high(wiznet5k_obj.rst); - mp_hal_delay_ms(160); // datasheet says 150ms - - reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit); - reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect); - reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write); - - uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2k buffer for each socket - ctlwizchip(CW_INIT_WIZCHIP, sn_size); - - // set some sensible default values; they are configurable using ifconfig method - wiz_NetInfo netinfo = { - .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, - .ip = {192, 168, 0, 18}, - .sn = {255, 255, 255, 0}, - .gw = {192, 168, 0, 1}, - .dns = {8, 8, 8, 8}, // Google public DNS - .dhcp = NETINFO_STATIC, - }; - ctlnetwork(CN_SET_NETINFO, (void *)&netinfo); - - // seems we need a small delay after init - mp_hal_delay_ms(250); - - // register with network module - mod_network_register_nic(&wiznet5k_obj); - - // return wiznet5k object - return &wiznet5k_obj; -} - -/// \method regs() -/// Dump WIZNET5K registers. -STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) { - // wiznet5k_obj_t *self = self_in; - printf("Wiz CREG:"); - for (int i = 0; i < 0x50; ++i) { - if (i % 16 == 0) { - printf("\n %04x:", i); - } - #if MICROPY_PY_NETWORK_WIZNET5K == 5200 - uint32_t reg = i; - #else - uint32_t reg = _W5500_IO_BASE_ | i << 8; - #endif - printf(" %02x", WIZCHIP_READ(reg)); - } - for (int sn = 0; sn < 4; ++sn) { - printf("\nWiz SREG[%d]:", sn); - for (int i = 0; i < 0x30; ++i) { - if (i % 16 == 0) { - printf("\n %04x:", i); - } - #if MICROPY_PY_NETWORK_WIZNET5K == 5200 - uint32_t reg = WIZCHIP_SREG_ADDR(sn, i); - #else - uint32_t reg = _W5500_IO_BASE_ | i << 8 | WIZCHIP_SREG_BLOCK(sn) << 3; - #endif - printf(" %02x", WIZCHIP_READ(reg)); - } - } - printf("\n"); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_regs_obj, wiznet5k_regs); - -STATIC mp_obj_t wiznet5k_isconnected(mp_obj_t self_in) { - (void)self_in; - return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_isconnected_obj, wiznet5k_isconnected); - -/// \method ifconfig([(ip, subnet, gateway, dns)]) -/// Get/set IP address, subnet mask, gateway and DNS. -STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) { - wiz_NetInfo netinfo; - ctlnetwork(CN_GET_NETINFO, &netinfo); - if (n_args == 1) { - // get - mp_obj_t tuple[4] = { - netutils_format_ipv4_addr(netinfo.ip, NETUTILS_BIG), - netutils_format_ipv4_addr(netinfo.sn, NETUTILS_BIG), - netutils_format_ipv4_addr(netinfo.gw, NETUTILS_BIG), - netutils_format_ipv4_addr(netinfo.dns, NETUTILS_BIG), - }; - return mp_obj_new_tuple(4, tuple); - } else { - // set - mp_obj_t *items; - mp_obj_get_array_fixed_n(args[1], 4, &items); - netutils_parse_ipv4_addr(items[0], netinfo.ip, NETUTILS_BIG); - netutils_parse_ipv4_addr(items[1], netinfo.sn, NETUTILS_BIG); - netutils_parse_ipv4_addr(items[2], netinfo.gw, NETUTILS_BIG); - netutils_parse_ipv4_addr(items[3], netinfo.dns, NETUTILS_BIG); - ctlnetwork(CN_SET_NETINFO, &netinfo); - return mp_const_none; - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k_ifconfig); - -STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&wiznet5k_regs_obj) }, - { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) }, - { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&wiznet5k_isconnected_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table); - -const mod_network_nic_type_t mod_network_nic_type_wiznet5k = { - .base = { - { &mp_type_type }, - .name = MP_QSTR_WIZNET5K, - .make_new = wiznet5k_make_new, - .locals_dict = (mp_obj_dict_t *)&wiznet5k_locals_dict, - }, - .gethostbyname = wiznet5k_gethostbyname, - .socket = wiznet5k_socket_socket, - .close = wiznet5k_socket_close, - .bind = wiznet5k_socket_bind, - .listen = wiznet5k_socket_listen, - .accept = wiznet5k_socket_accept, - .connect = wiznet5k_socket_connect, - .send = wiznet5k_socket_send, - .recv = wiznet5k_socket_recv, - .sendto = wiznet5k_socket_sendto, - .recvfrom = wiznet5k_socket_recvfrom, - .setsockopt = wiznet5k_socket_setsockopt, - .settimeout = wiznet5k_socket_settimeout, - .ioctl = wiznet5k_socket_ioctl, -}; - -#endif // MICROPY_PY_NETWORK_WIZNET5K && !MICROPY_PY_LWIP diff --git a/ports/stm32/network_wiznet5k.c b/ports/stm32/network_wiznet5k.c deleted file mode 100644 index 4675f3c2c2a1e..0000000000000 --- a/ports/stm32/network_wiznet5k.c +++ /dev/null @@ -1,465 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2014-2018 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -#include "py/runtime.h" -#include "py/mphal.h" -#include "extmod/modnetwork.h" -#include "spi.h" - -#if MICROPY_PY_NETWORK_WIZNET5K && MICROPY_PY_LWIP - -#include "shared/netutils/netutils.h" -#include "drivers/wiznet5k/ethernet/socket.h" -#include "lwip/err.h" -#include "lwip/dns.h" -#include "lwip/dhcp.h" -#include "netif/etharp.h" - -#define TRACE_ETH_TX (0x0002) -#define TRACE_ETH_RX (0x0004) - -/*******************************************************************************/ -// Wiznet5k Ethernet driver in MACRAW mode - -typedef struct _wiznet5k_obj_t { - mp_obj_base_t base; - mp_uint_t cris_state; - const spi_t *spi; - mp_hal_pin_obj_t cs; - mp_hal_pin_obj_t rst; - uint8_t eth_frame[1514]; - uint32_t trace_flags; - struct netif netif; - struct dhcp dhcp_struct; -} wiznet5k_obj_t; - -// Global object holding the Wiznet5k state -STATIC wiznet5k_obj_t wiznet5k_obj; - -STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self); - -STATIC void wiz_cris_enter(void) { - wiznet5k_obj.cris_state = MICROPY_BEGIN_ATOMIC_SECTION(); -} - -STATIC void wiz_cris_exit(void) { - MICROPY_END_ATOMIC_SECTION(wiznet5k_obj.cris_state); -} - -STATIC void wiz_cs_select(void) { - mp_hal_pin_low(wiznet5k_obj.cs); -} - -STATIC void wiz_cs_deselect(void) { - mp_hal_pin_high(wiznet5k_obj.cs); -} - -STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) { - HAL_StatusTypeDef status = HAL_SPI_Receive(wiznet5k_obj.spi->spi, buf, len, 5000); - (void)status; -} - -STATIC void wiz_spi_write(const uint8_t *buf, uint32_t len) { - HAL_StatusTypeDef status = HAL_SPI_Transmit(wiznet5k_obj.spi->spi, (uint8_t *)buf, len, 5000); - (void)status; -} - -STATIC void wiznet5k_init(void) { - // SPI configuration - SPI_InitTypeDef *init = &wiznet5k_obj.spi->spi->Init; - init->Mode = SPI_MODE_MASTER; - init->Direction = SPI_DIRECTION_2LINES; - init->DataSize = SPI_DATASIZE_8BIT; - init->CLKPolarity = SPI_POLARITY_LOW; // clock is low when idle - init->CLKPhase = SPI_PHASE_1EDGE; // data latched on first edge, which is rising edge for low-idle - init->NSS = SPI_NSS_SOFT; - init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz - init->FirstBit = SPI_FIRSTBIT_MSB; - init->TIMode = SPI_TIMODE_DISABLED; - init->CRCCalculation = SPI_CRCCALCULATION_DISABLED; - init->CRCPolynomial = 7; // unused - spi_init(wiznet5k_obj.spi, false); - - mp_hal_pin_output(wiznet5k_obj.cs); - mp_hal_pin_output(wiznet5k_obj.rst); - - // Reset the chip - mp_hal_pin_low(wiznet5k_obj.rst); - mp_hal_delay_ms(1); // datasheet says 2us - mp_hal_pin_high(wiznet5k_obj.rst); - mp_hal_delay_ms(150); // datasheet says 150ms - - // Set physical interface callbacks - reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit); - reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect); - reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write); - - // Configure 16k buffers for fast MACRAW - uint8_t sn_size[16] = {16, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0}; - ctlwizchip(CW_INIT_WIZCHIP, sn_size); - - // Seems we need a small delay after init - mp_hal_delay_ms(250); - - // If the device doesn't have a MAC address then set one - uint8_t mac[6]; - getSHAR(mac); - if ((mac[0] | mac[1] | mac[2] | mac[3] | mac[4] | mac[5]) == 0) { - mp_hal_get_mac(MP_HAL_MAC_ETH0, mac); - setSHAR(mac); - } - - // Hook the Wiznet into lwIP - wiznet5k_lwip_init(&wiznet5k_obj); -} - -STATIC void wiznet5k_deinit(void) { - for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) { - if (netif == &wiznet5k_obj.netif) { - netif_remove(netif); - netif->flags = 0; - break; - } - } -} - -STATIC void wiznet5k_get_mac_address(wiznet5k_obj_t *self, uint8_t mac[6]) { - (void)self; - getSHAR(mac); -} - -STATIC void wiznet5k_send_ethernet(wiznet5k_obj_t *self, size_t len, const uint8_t *buf) { - uint8_t ip[4] = {1, 1, 1, 1}; // dummy - int ret = WIZCHIP_EXPORT(sendto)(0, (byte *)buf, len, ip, 11); // dummy port - if (ret != len) { - printf("wiznet5k_send_ethernet: fatal error %d\n", ret); - netif_set_link_down(&self->netif); - netif_set_down(&self->netif); - } -} - -// Stores the frame in self->eth_frame and returns number of bytes in the frame, 0 for no frame -STATIC uint16_t wiznet5k_recv_ethernet(wiznet5k_obj_t *self) { - uint16_t len = getSn_RX_RSR(0); - if (len == 0) { - return 0; - } - - byte ip[4]; - uint16_t port; - int ret = WIZCHIP_EXPORT(recvfrom)(0, self->eth_frame, 1514, ip, &port); - if (ret <= 0) { - printf("wiznet5k_poll: fatal error len=%u ret=%d\n", len, ret); - netif_set_link_down(&self->netif); - netif_set_down(&self->netif); - return 0; - } - - return ret; -} - -/*******************************************************************************/ -// Wiznet5k lwIP interface - -STATIC err_t wiznet5k_netif_output(struct netif *netif, struct pbuf *p) { - wiznet5k_obj_t *self = netif->state; - pbuf_copy_partial(p, self->eth_frame, p->tot_len, 0); - if (self->trace_flags & TRACE_ETH_TX) { - netutils_ethernet_trace(MP_PYTHON_PRINTER, p->tot_len, self->eth_frame, NETUTILS_TRACE_IS_TX | NETUTILS_TRACE_NEWLINE); - } - wiznet5k_send_ethernet(self, p->tot_len, self->eth_frame); - return ERR_OK; -} - -STATIC err_t wiznet5k_netif_init(struct netif *netif) { - netif->linkoutput = wiznet5k_netif_output; - netif->output = etharp_output; - netif->mtu = 1500; - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP; - wiznet5k_get_mac_address(netif->state, netif->hwaddr); - netif->hwaddr_len = sizeof(netif->hwaddr); - int ret = WIZCHIP_EXPORT(socket)(0, Sn_MR_MACRAW, 0, 0); - if (ret != 0) { - printf("WIZNET fatal error in netifinit: %d\n", ret); - return ERR_IF; - } - - // Enable MAC filtering so we only get frames destined for us, to reduce load on lwIP - setSn_MR(0, getSn_MR(0) | Sn_MR_MFEN); - - return ERR_OK; -} - -STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self) { - ip_addr_t ipconfig[4]; - ipconfig[0].addr = 0; - ipconfig[1].addr = 0; - ipconfig[2].addr = 0; - ipconfig[3].addr = 0; - netif_add(&self->netif, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, wiznet5k_netif_init, ethernet_input); - self->netif.name[0] = 'e'; - self->netif.name[1] = '0'; - netif_set_default(&self->netif); - dns_setserver(0, &ipconfig[3]); - dhcp_set_struct(&self->netif, &self->dhcp_struct); - // Setting NETIF_FLAG_UP then clearing it is a workaround for dhcp_start and the - // LWIP_DHCP_CHECK_LINK_UP option, so that the DHCP client schedules itself to - // automatically start when the interface later goes up. - self->netif.flags |= NETIF_FLAG_UP; - dhcp_start(&self->netif); - self->netif.flags &= ~NETIF_FLAG_UP; -} - -void wiznet5k_poll(void) { - wiznet5k_obj_t *self = &wiznet5k_obj; - if (!(self->netif.flags & NETIF_FLAG_LINK_UP)) { - return; - } - uint16_t len; - while ((len = wiznet5k_recv_ethernet(self)) > 0) { - if (self->trace_flags & TRACE_ETH_RX) { - netutils_ethernet_trace(MP_PYTHON_PRINTER, len, self->eth_frame, NETUTILS_TRACE_NEWLINE); - } - struct pbuf *p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - if (p != NULL) { - pbuf_take(p, self->eth_frame, len); - if (self->netif.input(p, &self->netif) != ERR_OK) { - pbuf_free(p); - } - } - } -} - -/*******************************************************************************/ -// MicroPython bindings - -// WIZNET5K([spi, pin_cs, pin_rst]) -STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 3, 3, false); - - const spi_t *spi = spi_from_mp_obj(args[0]); - mp_hal_pin_obj_t cs = pin_find(args[1]); - mp_hal_pin_obj_t rst = pin_find(args[2]); - - // Access the existing object, if it has been constructed with the same hardware interface - if (wiznet5k_obj.base.type == (mp_obj_type_t *)&mod_network_nic_type_wiznet5k) { - if (!(wiznet5k_obj.spi == spi && wiznet5k_obj.cs == cs && wiznet5k_obj.rst == rst - && wiznet5k_obj.netif.flags != 0)) { - wiznet5k_deinit(); - } - } - - // Init the wiznet5k object - wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k; - wiznet5k_obj.cris_state = 0; - wiznet5k_obj.spi = spi; - wiznet5k_obj.cs = cs; - wiznet5k_obj.rst = rst; - wiznet5k_obj.trace_flags = 0; - - // Return wiznet5k object - return MP_OBJ_FROM_PTR(&wiznet5k_obj); -} - -STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) { - (void)self_in; - printf("Wiz CREG:"); - for (int i = 0; i < 0x50; ++i) { - if (i % 16 == 0) { - printf("\n %04x:", i); - } - #if MICROPY_PY_NETWORK_WIZNET5K == 5200 - uint32_t reg = i; - #else - uint32_t reg = _W5500_IO_BASE_ | i << 8; - #endif - printf(" %02x", WIZCHIP_READ(reg)); - } - for (int sn = 0; sn < 4; ++sn) { - printf("\nWiz SREG[%d]:", sn); - for (int i = 0; i < 0x30; ++i) { - if (i % 16 == 0) { - printf("\n %04x:", i); - } - #if MICROPY_PY_NETWORK_WIZNET5K == 5200 - uint32_t reg = WIZCHIP_SREG_ADDR(sn, i); - #else - uint32_t reg = _W5500_IO_BASE_ | i << 8 | WIZCHIP_SREG_BLOCK(sn) << 3; - #endif - printf(" %02x", WIZCHIP_READ(reg)); - } - } - printf("\n"); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_regs_obj, wiznet5k_regs); - -STATIC mp_obj_t wiznet5k_isconnected(mp_obj_t self_in) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool( - wizphy_getphylink() == PHY_LINK_ON - && (self->netif.flags & NETIF_FLAG_UP) - && self->netif.ip_addr.addr != 0 - ); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_isconnected_obj, wiznet5k_isconnected); - -STATIC mp_obj_t wiznet5k_active(size_t n_args, const mp_obj_t *args) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(args[0]); - if (n_args == 1) { - return mp_obj_new_bool(self->netif.flags & NETIF_FLAG_UP); - } else { - if (mp_obj_is_true(args[1])) { - if (!(self->netif.flags & NETIF_FLAG_UP)) { - wiznet5k_init(); - netif_set_link_up(&self->netif); - netif_set_up(&self->netif); - } - } else { - if (self->netif.flags & NETIF_FLAG_UP) { - netif_set_down(&self->netif); - netif_set_link_down(&self->netif); - wiznet5k_deinit(); - } - } - return mp_const_none; - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_active_obj, 1, 2, wiznet5k_active); - -STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(args[0]); - return mod_network_nic_ifconfig(&self->netif, n_args - 1, args + 1); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k_ifconfig); - -STATIC mp_obj_t wiznet5k_status(size_t n_args, const mp_obj_t *args) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(args[0]); - (void)self; - - if (n_args == 1) { - // No arguments: return link status - if (self->netif.flags && wizphy_getphylink() == PHY_LINK_ON) { - if ((self->netif.flags & NETIF_FLAG_UP) && self->netif.ip_addr.addr != 0) { - return MP_OBJ_NEW_SMALL_INT(2); - } else { - return MP_OBJ_NEW_SMALL_INT(1); - } - } else { - return MP_OBJ_NEW_SMALL_INT(0); - } - } - - mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_status_obj, 1, 2, wiznet5k_status); - -STATIC mp_obj_t wiznet5k_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(args[0]); - - if (kwargs->used == 0) { - // Get config value - if (n_args != 2) { - mp_raise_TypeError(MP_ERROR_TEXT("must query one param")); - } - - switch (mp_obj_str_get_qstr(args[1])) { - case MP_QSTR_mac: { - uint8_t buf[6]; - wiznet5k_get_mac_address(self, buf); - return mp_obj_new_bytes(buf, 6); - } - default: - mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); - } - } else { - // Set config value(s) - if (n_args != 1) { - mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args")); - } - - for (size_t i = 0; i < kwargs->alloc; ++i) { - if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) { - mp_map_elem_t *e = &kwargs->table[i]; - switch (mp_obj_str_get_qstr(e->key)) { - case MP_QSTR_mac: { - mp_buffer_info_t buf; - mp_get_buffer_raise(e->value, &buf, MP_BUFFER_READ); - if (buf.len != 6) { - mp_raise_ValueError(NULL); - } - setSHAR(buf.buf); - memcpy(self->netif.hwaddr, buf.buf, 6); - break; - } - case MP_QSTR_trace: { - self->trace_flags = mp_obj_get_int(e->value); - break; - } - default: - mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); - } - } - } - - return mp_const_none; - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wiznet5k_config_obj, 1, wiznet5k_config); - -STATIC mp_obj_t send_ethernet_wrapper(mp_obj_t self_in, mp_obj_t buf_in) { - wiznet5k_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_buffer_info_t buf; - mp_get_buffer_raise(buf_in, &buf, MP_BUFFER_READ); - wiznet5k_send_ethernet(self, buf.len, buf.buf); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(send_ethernet_obj, send_ethernet_wrapper); - -STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&wiznet5k_regs_obj) }, - { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&wiznet5k_isconnected_obj) }, - { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&wiznet5k_active_obj) }, - { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) }, - { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&wiznet5k_status_obj) }, - { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&wiznet5k_config_obj) }, - - { MP_ROM_QSTR(MP_QSTR_send_ethernet), MP_ROM_PTR(&send_ethernet_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table); - -const mp_obj_type_t mod_network_nic_type_wiznet5k = { - { &mp_type_type }, - .name = MP_QSTR_WIZNET5K, - .make_new = wiznet5k_make_new, - .locals_dict = (mp_obj_dict_t *)&wiznet5k_locals_dict, -}; - -#endif // MICROPY_PY_NETWORK_WIZNET5K && MICROPY_PY_LWIP From c3305c49e422369119ab211dc8fb2366aa6d7d53 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 4 Aug 2022 20:19:57 +0200 Subject: [PATCH 2505/3533] stm32: Add definitions required for lwIP version of Wiznet NIC. --- ports/stm32/mphalport.h | 8 ++++++++ ports/stm32/mpnetworkport.c | 5 +++++ ports/stm32/pendsv.h | 3 +++ 3 files changed, 16 insertions(+) diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index 47bb1f8c84417..8028782a90c47 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -96,11 +96,19 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) { #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_read(p) (((p)->gpio->IDR >> (p)->pin) & 1) #define mp_hal_pin_write(p, v) ((v) ? mp_hal_pin_high(p) : mp_hal_pin_low(p)) +#define mp_hal_pin_interrupt(pin, handler, trigger, hard) extint_register_pin(pin, trigger, hard, handler) + +enum mp_hal_pin_interrupt_trigger { + MP_HAL_PIN_TRIGGER_NONE, + MP_HAL_PIN_TRIGGER_FALL = GPIO_MODE_IT_FALLING, + MP_HAL_PIN_TRIGGER_RISE = GPIO_MODE_IT_RISING, +}; void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio); void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt); bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit); void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed); +void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_obj_t callback_obj); enum { MP_HAL_MAC_WLAN0 = 0, diff --git a/ports/stm32/mpnetworkport.c b/ports/stm32/mpnetworkport.c index 2f49328e110ee..1ce758e7dd78a 100644 --- a/ports/stm32/mpnetworkport.c +++ b/ports/stm32/mpnetworkport.c @@ -50,6 +50,11 @@ #if MICROPY_PY_NETWORK_WIZNET5K void wiznet5k_poll(void); +void wiznet5k_deinit(void); + +void wiznet5k_try_poll(void) { + pendsv_schedule_dispatch(PENDSV_DISPATCH_WIZNET, wiznet5k_poll); +} #endif u32_t sys_now(void) { diff --git a/ports/stm32/pendsv.h b/ports/stm32/pendsv.h index 0733d355d3112..f97581e99a308 100644 --- a/ports/stm32/pendsv.h +++ b/ports/stm32/pendsv.h @@ -35,6 +35,9 @@ enum { #if MICROPY_PY_NETWORK_CYW43 PENDSV_DISPATCH_CYW43, #endif + #if MICROPY_PY_NETWORK_WIZNET5K + PENDSV_DISPATCH_WIZNET, + #endif #endif #if MICROPY_PY_BLUETOOTH && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS PENDSV_DISPATCH_BLUETOOTH_HCI, From 54eaa8c8a6c854e13f3c1b646ef44d0888628353 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 10 Aug 2022 11:04:09 +0200 Subject: [PATCH 2506/3533] stm32/mphalport: Add mp_hal_get_spi_obj() helper function. The function spi_from_mp_obj() is kept since it is used by the cc3k driver. --- ports/stm32/mphalport.h | 2 ++ ports/stm32/spi.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index 8028782a90c47..358d9cd25d0da 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -110,6 +110,8 @@ bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, u void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed); void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_obj_t callback_obj); +mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t spi_in); + enum { MP_HAL_MAC_WLAN0 = 0, MP_HAL_MAC_WLAN1, diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 188376ec06d54..9c031cad2e28d 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -30,6 +30,7 @@ #include "py/runtime.h" #include "py/mphal.h" #include "spi.h" +#include "extmod/machine_spi.h" // Possible DMA configurations for SPI buses: // SPI1_TX: DMA2_Stream3.CHANNEL_3 or DMA2_Stream5.CHANNEL_3 @@ -685,6 +686,20 @@ const spi_t *spi_from_mp_obj(mp_obj_t o) { } } +mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) { + if (mp_obj_is_type(o, &machine_hard_spi_type)) { + return MP_OBJ_TO_PTR(o); + } + #if MICROPY_PY_MACHINE_SOFTSPI + else if (mp_obj_is_type(o, &mp_machine_soft_spi_type)) { + return MP_OBJ_TO_PTR(o); + } + #endif + else { + mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object")); + } +} + /******************************************************************************/ // Implementation of low-level SPI C protocol From f6ec01d1da8d316e1cdfeaf20cdc0578f943e154 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 5 Aug 2022 16:49:15 +0200 Subject: [PATCH 2507/3533] tools/ci.sh: Split the stm32 builds for wiznet5k and cc3k. - Add lib/wiznet5k into the 'make submodules' step. - Split the stm32 builds for wiznet5k and cc3k. - Run 'make .... clean' after making the wiznet5k build. --- tools/ci.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/ci.sh b/tools/ci.sh index dc2ccf775b15e..b7b20e3c7d65b 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -309,10 +309,12 @@ function ci_stm32_setup { function ci_stm32_pyb_build { make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/stm32 submodules + make ${MAKEOPTS} -C ports/stm32 MICROPY_PY_NETWORK_WIZNET5K=5200 submodules git submodule update --init lib/btstack git submodule update --init lib/mynewt-nimble - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_NETWORK_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_NETWORK_WIZNET5K=5200 USER_C_MODULES=../../examples/usercmodule + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 clean + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_CC3K=1 make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' From 71dcb21e24064d1150094e66a904877402cdd157 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 10 Aug 2022 11:07:35 +0200 Subject: [PATCH 2508/3533] drivers/wiznet5k: Remove old Wiznet driver. It has been replaced by the submodule lib/wiznet5k. --- drivers/wiznet5k/README.md | 6 - drivers/wiznet5k/ethernet/socket.c | 725 -------- drivers/wiznet5k/ethernet/socket.h | 472 ----- drivers/wiznet5k/ethernet/w5200/w5200.c | 215 --- drivers/wiznet5k/ethernet/w5200/w5200.h | 2092 ---------------------- drivers/wiznet5k/ethernet/w5500/w5500.c | 247 --- drivers/wiznet5k/ethernet/w5500/w5500.h | 2057 --------------------- drivers/wiznet5k/ethernet/wizchip_conf.c | 662 ------- drivers/wiznet5k/ethernet/wizchip_conf.h | 554 ------ drivers/wiznet5k/internet/dhcp/dhcp.c | 978 ---------- drivers/wiznet5k/internet/dhcp/dhcp.h | 150 -- drivers/wiznet5k/internet/dns/dns.c | 566 ------ drivers/wiznet5k/internet/dns/dns.h | 96 - 13 files changed, 8820 deletions(-) delete mode 100644 drivers/wiznet5k/README.md delete mode 100644 drivers/wiznet5k/ethernet/socket.c delete mode 100644 drivers/wiznet5k/ethernet/socket.h delete mode 100644 drivers/wiznet5k/ethernet/w5200/w5200.c delete mode 100644 drivers/wiznet5k/ethernet/w5200/w5200.h delete mode 100644 drivers/wiznet5k/ethernet/w5500/w5500.c delete mode 100644 drivers/wiznet5k/ethernet/w5500/w5500.h delete mode 100644 drivers/wiznet5k/ethernet/wizchip_conf.c delete mode 100644 drivers/wiznet5k/ethernet/wizchip_conf.h delete mode 100644 drivers/wiznet5k/internet/dhcp/dhcp.c delete mode 100644 drivers/wiznet5k/internet/dhcp/dhcp.h delete mode 100644 drivers/wiznet5k/internet/dns/dns.c delete mode 100644 drivers/wiznet5k/internet/dns/dns.h diff --git a/drivers/wiznet5k/README.md b/drivers/wiznet5k/README.md deleted file mode 100644 index 88f25a2b8dbcb..0000000000000 --- a/drivers/wiznet5k/README.md +++ /dev/null @@ -1,6 +0,0 @@ -This is the driver for the WIZnet5x00 series of Ethernet controllers. - -Adapted for MicroPython. - -Original source: https://github.com/Wiznet/W5500_EVB/tree/master/ioLibrary -Taken on: 30 August 2014 diff --git a/drivers/wiznet5k/ethernet/socket.c b/drivers/wiznet5k/ethernet/socket.c deleted file mode 100644 index 3ffda3a722dfa..0000000000000 --- a/drivers/wiznet5k/ethernet/socket.c +++ /dev/null @@ -1,725 +0,0 @@ -//***************************************************************************** -// -//! \file socket.c -//! \brief SOCKET APIs Implements file. -//! \details SOCKET APIs like as Berkeley Socket APIs. -//! \version 1.0.3 -//! \date 2013/10/21 -//! \par Revision history -//! <2014/05/01> V1.0.3. Refer to M20140501 -//! 1. Implicit type casting -> Explicit type casting. -//! 2. replace 0x01 with PACK_REMAINED in recvfrom() -//! 3. Validation a destination ip in connect() & sendto(): -//! It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address. -//! Copy 4 byte addr value into temporary uint32 variable and then compares it. -//! <2013/12/20> V1.0.2 Refer to M20131220 -//! Remove Warning. -//! <2013/11/04> V1.0.1 2nd Release. Refer to "20131104". -//! In sendto(), Add to clear timeout interrupt status (Sn_IR_TIMEOUT) -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#include - -#include "py/mpthread.h" -#include "socket.h" - -#define SOCK_ANY_PORT_NUM 0xC000; - -static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; -static uint16_t sock_io_mode = 0; -static uint16_t sock_is_sending = 0; -static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,}; -static uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,}; - -#if _WIZCHIP_ == 5200 - static uint16_t sock_next_rd[_WIZCHIP_SOCK_NUM_] ={0,}; -#endif - -#define CHECK_SOCKNUM() \ - do{ \ - if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; \ - }while(0); \ - -#define CHECK_SOCKMODE(mode) \ - do{ \ - if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE; \ - }while(0); \ - -#define CHECK_SOCKINIT() \ - do{ \ - if((getSn_SR(sn) != SOCK_INIT)) return SOCKERR_SOCKINIT; \ - }while(0); \ - -#define CHECK_SOCKDATA() \ - do{ \ - if(len == 0) return SOCKERR_DATALEN; \ - }while(0); \ - -void WIZCHIP_EXPORT(socket_reset)(void) { - sock_any_port = SOCK_ANY_PORT_NUM; - sock_io_mode = 0; - sock_is_sending = 0; - /* - memset(sock_remained_size, 0, _WIZCHIP_SOCK_NUM_ * sizeof(uint16_t)); - memset(sock_pack_info, 0, _WIZCHIP_SOCK_NUM_ * sizeof(uint8_t)); - */ - -#if _WIZCHIP_ == 5200 - memset(sock_next_rd, 0, _WIZCHIP_SOCK_NUM_ * sizeof(uint16_t)); -#endif -} - -int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag) -{ - CHECK_SOCKNUM(); - switch(protocol) - { - case Sn_MR_TCP : - case Sn_MR_UDP : - case Sn_MR_MACRAW : - break; - #if ( _WIZCHIP_ < 5200 ) - case Sn_MR_IPRAW : - case Sn_MR_PPPoE : - break; - #endif - default : - return SOCKERR_SOCKMODE; - } - if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG; -#if _WIZCHIP_ == 5200 - if(flag & 0x10) return SOCKERR_SOCKFLAG; -#endif - - if(flag != 0) - { - switch(protocol) - { - case Sn_MR_TCP: - if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK))==0) return SOCKERR_SOCKFLAG; - break; - case Sn_MR_UDP: - if(flag & SF_IGMP_VER2) - { - if((flag & SF_MULTI_ENABLE)==0) return SOCKERR_SOCKFLAG; - } - #if _WIZCHIP_ == 5500 - if(flag & SF_UNI_BLOCK) - { - if((flag & SF_MULTI_ENABLE) == 0) return SOCKERR_SOCKFLAG; - } - #endif - break; - default: - break; - } - } - WIZCHIP_EXPORT(close)(sn); - setSn_MR(sn, (protocol | (flag & 0xF0))); - if(!port) - { - port = sock_any_port++; - if(sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM; - } - setSn_PORT(sn,port); - setSn_CR(sn,Sn_CR_OPEN); - while(getSn_CR(sn)); - sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn); - sock_is_sending &= ~(1< freesize) len = freesize; // check size not to exceed MAX size. - while(1) - { - freesize = getSn_TX_FSR(sn); - tmp = getSn_SR(sn); - if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) - { - WIZCHIP_EXPORT(close)(sn); - return SOCKERR_SOCKSTATUS; - } - if( (sock_io_mode & (1< freesize) ) return SOCK_BUSY; - if(len <= freesize) break; - MICROPY_THREAD_YIELD(); - } - wiz_send_data(sn, buf, len); - #if _WIZCHIP_ == 5200 - sock_next_rd[sn] = getSn_TX_RD(sn) + len; - #endif - setSn_CR(sn,Sn_CR_SEND); - /* wait to process the command... */ - while(getSn_CR(sn)); - sock_is_sending |= (1 << sn); - return len; -} - - -int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len) -{ - uint8_t tmp = 0; - uint16_t recvsize = 0; - CHECK_SOCKNUM(); - CHECK_SOCKMODE(Sn_MR_TCP); - CHECK_SOCKDATA(); - - recvsize = getSn_RxMAX(sn); - if(recvsize < len) len = recvsize; - while(1) - { - recvsize = getSn_RX_RSR(sn); - tmp = getSn_SR(sn); - if (tmp != SOCK_ESTABLISHED) - { - if(tmp == SOCK_CLOSE_WAIT) - { - if(recvsize != 0) break; - else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn)) - { - // dpgeorge: Getting here seems to be an orderly shutdown of the - // socket, and trying to get POSIX behaviour we return 0 because: - // "If no messages are available to be received and the peer has per†- // formed an orderly shutdown, recv() shall return 0". - // TODO this return value clashes with SOCK_BUSY in non-blocking mode. - WIZCHIP_EXPORT(close)(sn); - return 0; - } - } - else - { - WIZCHIP_EXPORT(close)(sn); - return SOCKERR_SOCKSTATUS; - } - } - if((sock_io_mode & (1< freesize) len = freesize; // check size not to exceed MAX size. - while(1) - { - freesize = getSn_TX_FSR(sn); - if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; - if( (sock_io_mode & (1< freesize) ) return SOCK_BUSY; - if(len <= freesize) break; - MICROPY_THREAD_YIELD(); - }; - wiz_send_data(sn, buf, len); - - #if _WIZCHIP_ == 5200 // for W5200 ARP errata - setSUBR(wizchip_getsubn()); - #endif - - setSn_CR(sn,Sn_CR_SEND); - /* wait to process the command... */ - while(getSn_CR(sn)); - while(1) - { - tmp = getSn_IR(sn); - if(tmp & Sn_IR_SENDOK) - { - setSn_IR(sn, Sn_IR_SENDOK); - break; - } - //M:20131104 - //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT; - else if(tmp & Sn_IR_TIMEOUT) - { - setSn_IR(sn, Sn_IR_TIMEOUT); - #if _WIZCHIP_ == 5200 // for W5200 ARP errata - setSUBR((uint8_t*)"\x00\x00\x00\x00"); - #endif - return SOCKERR_TIMEOUT; - } - //////////// - MICROPY_THREAD_YIELD(); - } - #if _WIZCHIP_ == 5200 // for W5200 ARP errata - setSUBR((uint8_t*)"\x00\x00\x00\x00"); - #endif - return len; -} - - - -int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port) -{ - uint8_t mr; - uint8_t head[8]; - uint16_t pack_len=0; - - CHECK_SOCKNUM(); - //CHECK_SOCKMODE(Sn_MR_UDP); - switch((mr=getSn_MR(sn)) & 0x0F) - { - case Sn_MR_UDP: - case Sn_MR_MACRAW: - break; - #if ( _WIZCHIP_ < 5200 ) - case Sn_MR_IPRAW: - case Sn_MR_PPPoE: - break; - #endif - default: - return SOCKERR_SOCKMODE; - } - CHECK_SOCKDATA(); - if(sock_remained_size[sn] == 0) - { - while(1) - { - pack_len = getSn_RX_RSR(sn); - if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; - if( (sock_io_mode & (1< 1514) - { - WIZCHIP_EXPORT(close)(sn); - return SOCKFATAL_PACKLEN; - } - sock_pack_info[sn] = PACK_FIRST; - } - if(len < sock_remained_size[sn]) pack_len = len; - else pack_len = sock_remained_size[sn]; - wiz_recv_data(sn,buf,pack_len); - break; - #if ( _WIZCHIP_ < 5200 ) - case Sn_MR_IPRAW: - if(sock_remained_size[sn] == 0) - { - wiz_recv_data(sn, head, 6); - setSn_CR(sn,Sn_CR_RECV); - while(getSn_CR(sn)); - addr[0] = head[0]; - addr[1] = head[1]; - addr[2] = head[2]; - addr[3] = head[3]; - sock_remained_size[sn] = head[4]; - sock_remaiend_size[sn] = (sock_remained_size[sn] << 8) + head[5]; - sock_pack_info[sn] = PACK_FIRST; - } - // - // Need to packet length check - // - if(len < sock_remained_size[sn]) pack_len = len; - else pack_len = sock_remained_size[sn]; - wiz_recv_data(sn, buf, pack_len); // data copy. - break; - #endif - default: - wiz_recv_ignore(sn, pack_len); // data copy. - sock_remained_size[sn] = pack_len; - break; - } - setSn_CR(sn,Sn_CR_RECV); - /* wait to process the command... */ - while(getSn_CR(sn)) ; - sock_remained_size[sn] -= pack_len; - //M20140501 : replace 0x01 with PACK_REMAINED - //if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01; - if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= PACK_REMAINED; - // - return pack_len; -} - - -int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg) -{ - uint8_t tmp = 0; - CHECK_SOCKNUM(); - switch(cstype) - { - case CS_SET_IOMODE: - tmp = *((uint8_t*)arg); - if(tmp == SOCK_IO_NONBLOCK) sock_io_mode |= (1< explict type casting - //*((uint8_t*)arg) = (sock_io_mode >> sn) & 0x0001; - *((uint8_t*)arg) = (uint8_t)((sock_io_mode >> sn) & 0x0001); - // - break; - case CS_GET_MAXTXBUF: - *((uint16_t*)arg) = getSn_TxMAX(sn); - break; - case CS_GET_MAXRXBUF: - *((uint16_t*)arg) = getSn_RxMAX(sn); - break; - case CS_CLR_INTERRUPT: - if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG; - setSn_IR(sn,*(uint8_t*)arg); - break; - case CS_GET_INTERRUPT: - *((uint8_t*)arg) = getSn_IR(sn); - break; - case CS_SET_INTMASK: - if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG; - setSn_IMR(sn,*(uint8_t*)arg); - break; - case CS_GET_INTMASK: - *((uint8_t*)arg) = getSn_IMR(sn); - default: - return SOCKERR_ARG; - } - return SOCK_OK; -} - -int8_t WIZCHIP_EXPORT(setsockopt)(uint8_t sn, sockopt_type sotype, void* arg) -{ - // M20131220 : Remove warning - //uint8_t tmp; - CHECK_SOCKNUM(); - switch(sotype) - { - case SO_TTL: - setSn_TTL(sn,*(uint8_t*)arg); - break; - case SO_TOS: - setSn_TOS(sn,*(uint8_t*)arg); - break; - case SO_MSS: - setSn_MSSR(sn,*(uint16_t*)arg); - break; - case SO_DESTIP: - setSn_DIPR(sn, (uint8_t*)arg); - break; - case SO_DESTPORT: - setSn_DPORT(sn, *(uint16_t*)arg); - break; -#if _WIZCHIP_ != 5100 - case SO_KEEPALIVESEND: - CHECK_SOCKMODE(Sn_MR_TCP); - #if _WIZCHIP_ > 5200 - if(getSn_KPALVTR(sn) != 0) return SOCKERR_SOCKOPT; - #endif - setSn_CR(sn,Sn_CR_SEND_KEEP); - while(getSn_CR(sn) != 0) - { - // M20131220 - //if ((tmp = getSn_IR(sn)) & Sn_IR_TIMEOUT) - if (getSn_IR(sn) & Sn_IR_TIMEOUT) - { - setSn_IR(sn, Sn_IR_TIMEOUT); - return SOCKERR_TIMEOUT; - } - } - break; - #if _WIZCHIP_ > 5200 - case SO_KEEPALIVEAUTO: - CHECK_SOCKMODE(Sn_MR_TCP); - setSn_KPALVTR(sn,*(uint8_t*)arg); - break; - #endif -#endif - default: - return SOCKERR_ARG; - } - return SOCK_OK; -} - -int8_t WIZCHIP_EXPORT(getsockopt)(uint8_t sn, sockopt_type sotype, void* arg) -{ - CHECK_SOCKNUM(); - switch(sotype) - { - case SO_FLAG: - *(uint8_t*)arg = getSn_MR(sn) & 0xF0; - break; - case SO_TTL: - *(uint8_t*) arg = getSn_TTL(sn); - break; - case SO_TOS: - *(uint8_t*) arg = getSn_TOS(sn); - break; - case SO_MSS: - *(uint8_t*) arg = getSn_MSSR(sn); - case SO_DESTIP: - getSn_DIPR(sn, (uint8_t*)arg); - break; - case SO_DESTPORT: - *(uint16_t*) arg = getSn_DPORT(sn); - break; - #if _WIZCHIP_ > 5200 - case SO_KEEPALIVEAUTO: - CHECK_SOCKMODE(Sn_MR_TCP); - *(uint16_t*) arg = getSn_KPALVTR(sn); - break; - #endif - case SO_SENDBUF: - *(uint16_t*) arg = getSn_TX_FSR(sn); - case SO_RECVBUF: - *(uint16_t*) arg = getSn_RX_RSR(sn); - case SO_STATUS: - *(uint8_t*) arg = getSn_SR(sn); - break; - case SO_REMAINSIZE: - if(getSn_MR(sn) == Sn_MR_TCP) - *(uint16_t*)arg = getSn_RX_RSR(sn); - else - *(uint16_t*)arg = sock_remained_size[sn]; - break; - case SO_PACKINFO: - CHECK_SOCKMODE(Sn_MR_TCP); - *(uint8_t*)arg = sock_pack_info[sn]; - break; - default: - return SOCKERR_SOCKOPT; - } - return SOCK_OK; -} diff --git a/drivers/wiznet5k/ethernet/socket.h b/drivers/wiznet5k/ethernet/socket.h deleted file mode 100644 index 2f03a34eba015..0000000000000 --- a/drivers/wiznet5k/ethernet/socket.h +++ /dev/null @@ -1,472 +0,0 @@ -//***************************************************************************** -// -//! \file socket.h -//! \brief SOCKET APIs Header file. -//! \details SOCKET APIs like as berkeley socket api. -//! \version 1.0.2 -//! \date 2013/10/21 -//! \par Revision history -//! <2014/05/01> V1.0.2. Refer to M20140501 -//! 1. Modify the comment : SO_REMAINED -> PACK_REMAINED -//! 2. Add the comment as zero byte udp data reception in getsockopt(). -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** -/** - * @defgroup WIZnet_socket_APIs 1. WIZnet socket APIs - * @brief WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface. - * But there is a little bit of difference. - * @details - * Comparison between WIZnet and Berkeley SOCKET APIs - * - * - * - * - * - * - * - * - * - * - * - * - *
API WIZnet Berkeley
socket() O O
bind() X O
listen() O O
connect() O O
accept() X O
recv() O O
send() O O
recvfrom() O O
sendto() O O
closesocket() O
close() & disconnect()
O
- * There are @b bind() and @b accept() functions in @b Berkeley SOCKET API but, - * not in @b WIZnet SOCKET API. Because socket() of WIZnet is not only creating a SOCKET but also binding a local port number, - * and listen() of WIZnet is not only listening to connection request from client but also accepting the connection request. \n - * When you program "TCP SERVER" with Berkeley SOCKET API, you can use only one listen port. - * When the listen SOCKET accepts a connection request from a client, it keeps listening. - * After accepting the connection request, a new SOCKET is created and the new SOCKET is used in communication with the client. \n - * Following figure shows network flow diagram by Berkeley SOCKET API. - * @image html Berkeley_SOCKET.jpg "" - * But, When you program "TCP SERVER" with WIZnet SOCKET API, you can use as many as 8 listen SOCKET with same port number. \n - * Because there's no accept() in WIZnet SOCKET APIs, when the listen SOCKET accepts a connection request from a client, - * it is changed in order to communicate with the client. - * And the changed SOCKET is not listening any more and is dedicated for communicating with the client. \n - * If there're many listen SOCKET with same listen port number and a client requests a connection, - * the SOCKET which has the smallest SOCKET number accepts the request and is changed as communication SOCKET. \n - * Following figure shows network flow diagram by WIZnet SOCKET API. - * @image html WIZnet_SOCKET.jpg "" - */ -#ifndef _WIZCHIP_SOCKET_H_ -#define _WIZCHIP_SOCKET_H_ - -// use this macro for exported names to avoid name clashes -#define WIZCHIP_EXPORT(name) wizchip_ ## name - -#include "wizchip_conf.h" - -#define SOCKET uint8_t ///< SOCKET type define for legacy driver - -#define SOCK_OK 1 ///< Result is OK about socket process. -#define SOCK_BUSY 0 ///< Socket is busy on processing the operation. Valid only Non-block IO Mode. -#define SOCK_FATAL -1000 ///< Result is fatal error about socket process. - -#define SOCK_ERROR 0 -#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number -#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option -#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized -#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. -#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. -#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag -#define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) ///< Invalid socket status for socket operation. -#define SOCKERR_ARG (SOCK_ERROR - 10) ///< Invalid argument. -#define SOCKERR_PORTZERO (SOCK_ERROR - 11) ///< Port number is zero -#define SOCKERR_IPINVALID (SOCK_ERROR - 12) ///< Invalid IP address -#define SOCKERR_TIMEOUT (SOCK_ERROR - 13) ///< Timeout occurred -#define SOCKERR_DATALEN (SOCK_ERROR - 14) ///< Data length is zero or greater than buffer max size. -#define SOCKERR_BUFFER (SOCK_ERROR - 15) ///< Socket buffer is not enough for data communication. - -#define SOCKFATAL_PACKLEN (SOCK_FATAL - 1) ///< Invalid packet length. Fatal Error. - -/* - * SOCKET FLAG - */ -#define SF_ETHER_OWN (Sn_MR_MFEN) ///< In \ref Sn_MR_MACRAW, Receive only the packet as broadcast, multicast and own packet -#define SF_IGMP_VER2 (Sn_MR_MC) ///< In \ref Sn_MR_UDP with \ref SF_MULTI_ENABLE, Select IGMP version 2. -#define SF_TCP_NODELAY (Sn_MR_ND) ///< In \ref Sn_MR_TCP, Use to nodelayed ack. -#define SF_MULTI_ENABLE (Sn_MR_MULTI) ///< In \ref Sn_MR_UDP, Enable multicast mode. - -#if _WIZCHIP_ == 5500 - #define SF_BROAD_BLOCK (Sn_MR_BCASTB) ///< In \ref Sn_MR_UDP or \ref Sn_MR_MACRAW, Block broadcast packet. Valid only in W5500 - #define SF_MULTI_BLOCK (Sn_MR_MMB) ///< In \ref Sn_MR_MACRAW, Block multicast packet. Valid only in W5500 - #define SF_IPv6_BLOCK (Sn_MR_MIP6B) ///< In \ref Sn_MR_MACRAW, Block IPv6 packet. Valid only in W5500 - #define SF_UNI_BLOCK (Sn_MR_UCASTB) ///< In \ref Sn_MR_UDP with \ref SF_MULTI_ENABLE. Valid only in W5500 -#endif - -#define SF_IO_NONBLOCK 0x01 ///< Socket nonblock io mode. It used parameter in \ref socket(). - -/* - * UDP & MACRAW Packet Infomation - */ -#define PACK_FIRST 0x80 ///< In Non-TCP packet, It indicates to start receiving a packet. -#define PACK_REMAINED 0x01 ///< In Non-TCP packet, It indicates to remain a packet to be received. -#define PACK_COMPLETED 0x00 ///< In Non-TCP packet, It indicates to complete to receive a packet. - -// resets all global state associated with the socket interface -void WIZCHIP_EXPORT(socket_reset)(void); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Open a socket. - * @details Initializes the socket with 'sn' passed as parameter and open. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param protocol Protocol type to operate such as TCP, UDP and MACRAW. - * @param port Port number to be bined. - * @param flag Socket flags as \ref SF_ETHER_OWN, \ref SF_IGMP_VER2, \ref SF_TCP_NODELAY, \ref SF_MULTI_ENABLE, \ref SF_IO_NONBLOCK and so on.\n - * Valid flags only in W5500 : @ref SF_BROAD_BLOCK, @ref SF_MULTI_BLOCK, @ref SF_IPv6_BLOCK, and @ref SF_UNI_BLOCK. - * @sa Sn_MR - * - * @return @b Success : The socket number @b 'sn' passed as parameter\n - * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number\n - * @ref SOCKERR_SOCKMODE - Not support socket mode as TCP, UDP, and so on. \n - * @ref SOCKERR_SOCKFLAG - Invaild socket flag. - */ -int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Close a socket. - * @details It closes the socket with @b'sn' passed as parameter. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * - * @return @b Success : @ref SOCK_OK \n - * @b Fail : @ref SOCKERR_SOCKNUM - Invalid socket number - */ -int8_t WIZCHIP_EXPORT(close)(uint8_t sn); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Listen to a connection request from a client. - * @details It is listening to a connection request from a client. - * If connection request is accepted successfully, the connection is established. Socket sn is used in passive(server) mode. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @return @b Success : @ref SOCK_OK \n - * @b Fail :\n @ref SOCKERR_SOCKINIT - Socket is not initialized \n - * @ref SOCKERR_SOCKCLOSED - Socket closed unexpectedly. - */ -int8_t WIZCHIP_EXPORT(listen)(uint8_t sn); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Try to connect a server. - * @details It requests connection to the server with destination IP address and port number passed as parameter.\n - * @note It is valid only in TCP client mode. - * In block io mode, it does not return until connection is completed. - * In Non-block io mode, it return @ref SOCK_BUSY immediately. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes. - * @param port Destination port number. - * - * @return @b Success : @ref SOCK_OK \n - * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number\n - * @ref SOCKERR_SOCKMODE - Invalid socket mode\n - * @ref SOCKERR_SOCKINIT - Socket is not initialized\n - * @ref SOCKERR_IPINVALID - Wrong server IP address\n - * @ref SOCKERR_PORTZERO - Server port zero\n - * @ref SOCKERR_TIMEOUT - Timeout occurred during request connection\n - * @ref SOCK_BUSY - In non-block io mode, it returned immediately\n - */ -int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Try to disconnect a connection socket. - * @details It sends request message to disconnect the TCP socket 'sn' passed as parameter to the server or client. - * @note It is valid only in TCP server or client mode. \n - * In block io mode, it does not return until disconnection is completed. \n - * In Non-block io mode, it return @ref SOCK_BUSY immediately. \n - - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @return @b Success : @ref SOCK_OK \n - * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number \n - * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n - * @ref SOCKERR_TIMEOUT - Timeout occurred \n - * @ref SOCK_BUSY - Socket is busy. - */ -int8_t WIZCHIP_EXPORT(disconnect)(uint8_t sn); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Send data to the connected peer in TCP socket. - * @details It is used to send outgoing data to the connected socket. - * @note It is valid only in TCP server or client mode. It can't send data greater than socket buffer size. \n - * In block io mode, It doesn't return until data send is completed - socket buffer size is greater than data. \n - * In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough. \n - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param buf Pointer buffer containing data to be sent. - * @param len The byte length of data in buf. - * @return @b Success : The sent data size \n - * @b Fail : \n @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n - * @ref SOCKERR_TIMEOUT - Timeout occurred \n - * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n - * @ref SOCKERR_SOCKNUM - Invalid socket number \n - * @ref SOCKERR_DATALEN - zero data length \n - * @ref SOCK_BUSY - Socket is busy. - */ -int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Receive data from the connected peer. - * @details It is used to read incoming data from the connected socket.\n - * It waits for data as much as the application wants to receive. - * @note It is valid only in TCP server or client mode. It can't receive data greater than socket buffer size. \n - * In block io mode, it doesn't return until data reception is completed - data is filled as len in socket buffer. \n - * In non-block io mode, it return @ref SOCK_BUSY immediately when len is greater than data size in socket buffer. \n - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param buf Pointer buffer to read incoming data. - * @param len The max data length of data in buf. - * @return @b Success : The real received data size \n - * @b Fail :\n - * @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n - * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n - * @ref SOCKERR_SOCKNUM - Invalid socket number \n - * @ref SOCKERR_DATALEN - zero data length \n - * @ref SOCK_BUSY - Socket is busy. - */ -int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Sends datagram to the peer with destination IP address and port number passed as parameter. - * @details It sends datagram of UDP or MACRAW to the peer with destination IP address and port number passed as parameter.\n - * Even if the connectionless socket has been previously connected to a specific address, - * the address and port number parameters override the destination address for that particular datagram only. - * @note In block io mode, It doesn't return until data send is completed - socket buffer size is greater than len. - * In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param buf Pointer buffer to send outgoing data. - * @param len The byte length of data in buf. - * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes. - * @param port Destination port number. - * - * @return @b Success : The sent data size \n - * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number \n - * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n - * @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n - * @ref SOCKERR_DATALEN - zero data length \n - * @ref SOCKERR_IPINVALID - Wrong server IP address\n - * @ref SOCKERR_PORTZERO - Server port zero\n - * @ref SOCKERR_SOCKCLOSED - Socket unexpectedly closed \n - * @ref SOCKERR_TIMEOUT - Timeout occurred \n - * @ref SOCK_BUSY - Socket is busy. - */ -int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); - -/** - * @ingroup WIZnet_socket_APIs - * @brief Receive datagram of UDP or MACRAW - * @details This function is an application I/F function which is used to receive the data in other then TCP mode. \n - * This function is used to receive UDP and MAC_RAW mode, and handle the header as well. - * This function can divide to received the packet data. - * On the MACRAW SOCKET, the addr and port parameters are ignored. - * @note In block io mode, it doesn't return until data reception is completed - data is filled as len in socket buffer - * In non-block io mode, it return @ref SOCK_BUSY immediately when len is greater than data size in socket buffer. - * - * @param sn Socket number. It should be 0 ~ @ref \_WIZCHIP_SOCK_NUM_. - * @param buf Pointer buffer to read incoming data. - * @param len The max data length of data in buf. - * When the received packet size <= len, receives data as packet sized. - * When others, receives data as len. - * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes. - * It is valid only when the first call recvfrom for receiving the packet. - * When it is valid, @ref packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo). - * @param port Pointer variable of destination port number. - * It is valid only when the first call recvform for receiving the packet. -* When it is valid, @ref packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo). - * - * @return @b Success : This function return real received data size for success.\n - * @b Fail : @ref SOCKERR_DATALEN - zero data length \n - * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n - * @ref SOCKERR_SOCKNUM - Invalid socket number \n - * @ref SOCKBUSY - Socket is busy. - */ -int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); - - -///////////////////////////// -// SOCKET CONTROL & OPTION // -///////////////////////////// -#define SOCK_IO_BLOCK 0 ///< Socket Block IO Mode in @ref setsockopt(). -#define SOCK_IO_NONBLOCK 1 ///< Socket Non-block IO Mode in @ref setsockopt(). - -/** - * @defgroup DATA_TYPE DATA TYPE - */ - -/** - * @ingroup DATA_TYPE - * @brief The kind of Socket Interrupt. - * @sa Sn_IR, Sn_IMR, setSn_IR(), getSn_IR(), setSn_IMR(), getSn_IMR() - */ -typedef enum -{ - SIK_CONNECTED = (1 << 0), ///< connected - SIK_DISCONNECTED = (1 << 1), ///< disconnected - SIK_RECEIVED = (1 << 2), ///< data received - SIK_TIMEOUT = (1 << 3), ///< timeout occurred - SIK_SENT = (1 << 4), ///< send ok - SIK_ALL = 0x1F, ///< all interrupt -}sockint_kind; - -/** - * @ingroup DATA_TYPE - * @brief The type of @ref ctlsocket(). - */ -typedef enum -{ - CS_SET_IOMODE, ///< set socket IO mode with @ref SOCK_IO_BLOCK or @ref SOCK_IO_NONBLOCK - CS_GET_IOMODE, ///< get socket IO mode - CS_GET_MAXTXBUF, ///< get the size of socket buffer allocated in TX memory - CS_GET_MAXRXBUF, ///< get the size of socket buffer allocated in RX memory - CS_CLR_INTERRUPT, ///< clear the interrupt of socket with @ref sockint_kind - CS_GET_INTERRUPT, ///< get the socket interrupt. refer to @ref sockint_kind - CS_SET_INTMASK, ///< set the interrupt mask of socket with @ref sockint_kind - CS_GET_INTMASK ///< get the masked interrupt of socket. refer to @ref sockint_kind -}ctlsock_type; - - -/** - * @ingroup DATA_TYPE - * @brief The type of socket option in @ref setsockopt() or @ref getsockopt() - */ -typedef enum -{ - SO_FLAG, ///< Valid only in getsockopt(), For set flag of socket refer to flag in @ref socket(). - SO_TTL, ///< Set/Get TTL. @ref Sn_TTL ( @ref setSn_TTL(), @ref getSn_TTL() ) - SO_TOS, ///< Set/Get TOS. @ref Sn_TOS ( @ref setSn_TOS(), @ref getSn_TOS() ) - SO_MSS, ///< Set/Get MSS. @ref Sn_MSSR ( @ref setSn_MSSR(), @ref getSn_MSSR() ) - SO_DESTIP, ///< Set/Get the destination IP address. @ref Sn_DIPR ( @ref setSn_DIPR(), @ref getSn_DIPR() ) - SO_DESTPORT, ///< Set/Get the destination Port number. @ref Sn_DPORT ( @ref setSn_DPORT(), @ref getSn_DPORT() ) -#if _WIZCHIP_ != 5100 - SO_KEEPALIVESEND, ///< Valid only in setsockopt. Manually send keep-alive packet in TCP mode - #if _WIZCHIP_ > 5200 - SO_KEEPALIVEAUTO, ///< Set/Get keep-alive auto transmission timer in TCP mode - #endif -#endif - SO_SENDBUF, ///< Valid only in getsockopt. Get the free data size of Socekt TX buffer. @ref Sn_TX_FSR, @ref getSn_TX_FSR() - SO_RECVBUF, ///< Valid only in getsockopt. Get the received data size in socket RX buffer. @ref Sn_RX_RSR, @ref getSn_RX_RSR() - SO_STATUS, ///< Valid only in getsockopt. Get the socket status. @ref Sn_SR, @ref getSn_SR() - SO_REMAINSIZE, ///< Valid only in getsockopt. Get the remained packet size in other then TCP mode. - SO_PACKINFO ///< Valid only in getsockopt. Get the packet information as @ref PACK_FIRST, @ref PACK_REMAINED, and @ref PACK_COMPLETED in other then TCP mode. -}sockopt_type; - -/** - * @ingroup WIZnet_socket_APIs - * @brief Control socket. - * @details Control IO mode, Interrupt & Mask of socket and get the socket buffer information. - * Refer to @ref ctlsock_type. - * @param sn socket number - * @param cstype type of control socket. refer to @ref ctlsock_type. - * @param arg Data type and value is determined according to @ref ctlsock_type. \n - * - * - * - * - * - *
@b cstype @b data type@b value
@ref CS_SET_IOMODE \n @ref CS_GET_IOMODE uint8_t @ref SOCK_IO_BLOCK @ref SOCK_IO_NONBLOCK
@ref CS_GET_MAXTXBUF \n @ref CS_GET_MAXRXBUF uint16_t 0 ~ 16K
@ref CS_CLR_INTERRUPT \n @ref CS_GET_INTERRUPT \n @ref CS_SET_INTMASK \n @ref CS_GET_INTMASK @ref sockint_kind @ref SIK_CONNECTED, etc.
- * @return @b Success @ref SOCK_OK \n - * @b fail @ref SOCKERR_ARG - Invalid argument\n - */ -int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg); - -/** - * @ingroup WIZnet_socket_APIs - * @brief set socket options - * @details Set socket option like as TTL, MSS, TOS, and so on. Refer to @ref sockopt_type. - * - * @param sn socket number - * @param sotype socket option type. refer to @ref sockopt_type - * @param arg Data type and value is determined according to sotype. \n - * - * - * - * - * - * - * - * - * - *
@b sotype @b data type@b value
@ref SO_TTL uint8_t 0 ~ 255
@ref SO_TOS uint8_t 0 ~ 255
@ref SO_MSS uint16_t 0 ~ 65535
@ref SO_DESTIP uint8_t[4]
@ref SO_DESTPORT uint16_t 0 ~ 65535
@ref SO_KEEPALIVESEND null null
@ref SO_KEEPALIVEAUTO uint8_t 0 ~ 255
- * @return - * - @b Success : @ref SOCK_OK \n - * - @b Fail - * - @ref SOCKERR_SOCKNUM - Invalid Socket number \n - * - @ref SOCKERR_SOCKMODE - Invalid socket mode \n - * - @ref SOCKERR_SOCKOPT - Invalid socket option or its value \n - * - @ref SOCKERR_TIMEOUT - Timeout occurred when sending keep-alive packet \n - */ -int8_t WIZCHIP_EXPORT(setsockopt)(uint8_t sn, sockopt_type sotype, void* arg); - -/** - * @ingroup WIZnet_socket_APIs - * @brief get socket options - * @details Get socket option like as FLAG, TTL, MSS, and so on. Refer to @ref sockopt_type - * @param sn socket number - * @param sotype socket option type. refer to @ref sockopt_type - * @param arg Data type and value is determined according to sotype. \n - * - * - * - * - * - * - * - * - * - * - * - * - * - *
@b sotype @b data type@b value
@ref SO_FLAG uint8_t @ref SF_ETHER_OWN, etc...
@ref SO_TOS uint8_t 0 ~ 255
@ref SO_MSS uint16_t 0 ~ 65535
@ref SO_DESTIP uint8_t[4]
@ref SO_DESTPORT uint16_t
@ref SO_KEEPALIVEAUTO uint8_t 0 ~ 255
@ref SO_SENDBUF uint16_t 0 ~ 65535
@ref SO_RECVBUF uint16_t 0 ~ 65535
@ref SO_STATUS uint8_t @ref SOCK_ESTABLISHED, etc..
@ref SO_REMAINSIZE uint16_t 0~ 65535
@ref SO_PACKINFO uint8_t @ref PACK_FIRST, etc...
- * @return - * - @b Success : @ref SOCK_OK \n - * - @b Fail - * - @ref SOCKERR_SOCKNUM - Invalid Socket number \n - * - @ref SOCKERR_SOCKOPT - Invalid socket option or its value \n - * - @ref SOCKERR_SOCKMODE - Invalid socket mode \n - * @note - * The option as PACK_REMAINED and SO_PACKINFO is valid only in NON-TCP mode and after call @ref recvfrom(). \n - * When SO_PACKINFO value is PACK_FIRST and the return value of recvfrom() is zero, - * This means the zero byte UDP data(UDP Header only) received. - */ -int8_t WIZCHIP_EXPORT(getsockopt)(uint8_t sn, sockopt_type sotype, void* arg); - -#endif // _WIZCHIP_SOCKET_H_ diff --git a/drivers/wiznet5k/ethernet/w5200/w5200.c b/drivers/wiznet5k/ethernet/w5200/w5200.c deleted file mode 100644 index a84d3c9fa7bcb..0000000000000 --- a/drivers/wiznet5k/ethernet/w5200/w5200.c +++ /dev/null @@ -1,215 +0,0 @@ -// dpgeorge: this file taken from w5500/w5500.c and adapted to W5200 - -//***************************************************************************** -// -//! \file w5500.c -//! \brief W5500 HAL Interface. -//! \version 1.0.1 -//! \date 2013/10/21 -//! \par Revision history -//! <2014/05/01> V1.0.2 -//! 1. Implicit type casting -> Explicit type casting. Refer to M20140501 -//! Fixed the problem on porting into under 32bit MCU -//! Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh -//! Thank for your interesting and serious advices. -//! <2013/10/21> 1st Release -//! <2013/12/20> V1.0.1 -//! 1. Remove warning -//! 2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_ -//! for loop optimized(removed). refer to M20131220 -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#include "w5200.h" - -#if WIZCHIP_USE_MAX_BUFFER -// This option is intended to be used when MACRAW mode is enabled, to allow -// the single raw socket to use all the available buffer space. -#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 */ -#else -#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 */ -#endif - -#define TXBUF_BASE (0x8000) -#define RXBUF_BASE (0xc000) -#define SBASE(sn) (TXBUF_BASE + SSIZE * (sn)) /* tx buffer base for socket sn */ -#define RBASE(sn) (RXBUF_BASE + RSIZE * (sn)) /* rx buffer base for socket sn */ - -uint8_t WIZCHIP_READ(uint32_t AddrSel) { - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - uint8_t spi_data[4] = { - AddrSel >> 8, - AddrSel, - 0x00, - 0x01, - }; - WIZCHIP.IF.SPI._write_bytes(spi_data, 4); - uint8_t ret; - WIZCHIP.IF.SPI._read_bytes(&ret, 1); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); - - return ret; -} - -void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb) { - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - uint8_t spi_data[5] = { - AddrSel >> 8, - AddrSel, - 0x80, - 0x01, - wb, - }; - WIZCHIP.IF.SPI._write_bytes(spi_data, 5); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - -void WIZCHIP_READ_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) { - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - uint8_t spi_data[4] = { - AddrSel >> 8, - AddrSel, - 0x00 | ((len >> 8) & 0x7f), - len & 0xff, - }; - WIZCHIP.IF.SPI._write_bytes(spi_data, 4); - WIZCHIP.IF.SPI._read_bytes(pBuf, len); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - -void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) { - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - uint8_t spi_data[4] = { - AddrSel >> 8, - AddrSel, - 0x80 | ((len >> 8) & 0x7f), - len & 0xff, - }; - WIZCHIP.IF.SPI._write_bytes(spi_data, 4); - WIZCHIP.IF.SPI._write_bytes(pBuf, len); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - -uint16_t getSn_TX_FSR(uint8_t sn) { - uint16_t val = 0, val1 = 0; - do { - val1 = (WIZCHIP_READ(Sn_TX_FSR(sn)) << 8) | WIZCHIP_READ(Sn_TX_FSR(sn) + 1); - if (val1 != 0) { - val = (WIZCHIP_READ(Sn_TX_FSR(sn)) << 8) | WIZCHIP_READ(Sn_TX_FSR(sn) + 1); - } - } while (val != val1); - return val; -} - -uint16_t getSn_RX_RSR(uint8_t sn) { - uint16_t val = 0, val1 = 0; - do { - val1 = (WIZCHIP_READ(Sn_RX_RSR(sn)) << 8) | WIZCHIP_READ(Sn_RX_RSR(sn) + 1); - if (val1 != 0) { - val = (WIZCHIP_READ(Sn_RX_RSR(sn)) << 8) | WIZCHIP_READ(Sn_RX_RSR(sn) + 1); - } - } while (val != val1); - return val; -} - -void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len) { - if (len == 0) { - return; - } - - uint16_t ptr = getSn_TX_WR(sn); - uint16_t offset = ptr & SMASK; - uint32_t addr = offset + SBASE(sn); - - if (offset + len > SSIZE) { - // implement wrap-around circular buffer - uint16_t size = SSIZE - offset; - WIZCHIP_WRITE_BUF(addr, wizdata, size); - WIZCHIP_WRITE_BUF(SBASE(sn), wizdata + size, len - size); - } else { - WIZCHIP_WRITE_BUF(addr, wizdata, len); - } - - ptr += len; - setSn_TX_WR(sn, ptr); -} - -void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len) { - if (len == 0) { - return; - } - - uint16_t ptr = getSn_RX_RD(sn); - uint16_t offset = ptr & RMASK; - uint16_t addr = RBASE(sn) + offset; - - if (offset + len > RSIZE) { - // implement wrap-around circular buffer - uint16_t size = RSIZE - offset; - WIZCHIP_READ_BUF(addr, wizdata, size); - WIZCHIP_READ_BUF(RBASE(sn), wizdata + size, len - size); - } else { - WIZCHIP_READ_BUF(addr, wizdata, len); - } - - ptr += len; - setSn_RX_RD(sn, ptr); -} - -void wiz_recv_ignore(uint8_t sn, uint16_t len) { - uint16_t ptr = getSn_RX_RD(sn); - ptr += len; - setSn_RX_RD(sn, ptr); -} diff --git a/drivers/wiznet5k/ethernet/w5200/w5200.h b/drivers/wiznet5k/ethernet/w5200/w5200.h deleted file mode 100644 index 63561940f8b55..0000000000000 --- a/drivers/wiznet5k/ethernet/w5200/w5200.h +++ /dev/null @@ -1,2092 +0,0 @@ -// dpgeorge: this file taken from w5500/w5500.h and adapted to W5200 - -//***************************************************************************** -// -//! \file w5500.h -//! \brief W5500 HAL Header File. -//! \version 1.0.0 -//! \date 2013/10/21 -//! \par Revision history -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#ifndef _W5200_H_ -#define _W5200_H_ - -#include -#include "../wizchip_conf.h" -//#include "board.h" - -#define _W5200_IO_BASE_ 0x00000000 - -#define WIZCHIP_CREG_ADDR(addr) (_W5200_IO_BASE_ + (addr)) - -#define WIZCHIP_CH_BASE (0x4000) -#define WIZCHIP_CH_SIZE (0x100) -#define WIZCHIP_SREG_ADDR(sn, addr) (_W5200_IO_BASE_ + WIZCHIP_CH_BASE + (sn) * WIZCHIP_CH_SIZE + (addr)) - -////////////////////////////// -//-------------------------- defgroup --------------------------------- -/** - * @defgroup W5500 W5500 - * - * @brief WHIZCHIP register defines and I/O functions of @b W5500. - * - * - @ref WIZCHIP_register : @ref Common_register_group and @ref Socket_register_group - * - @ref WIZCHIP_IO_Functions : @ref Basic_IO_function, @ref Common_register_access_function and @ref Socket_register_access_function - */ - - -/** - * @defgroup WIZCHIP_register WIZCHIP register - * @ingroup W5500 - * - * @brief WHIZCHIP register defines register group of @b W5500. - * - * - @ref Common_register_group : Common register group - * - @ref Socket_register_group : \c SOCKET n register group - */ - - -/** - * @defgroup WIZCHIP_IO_Functions WIZCHIP I/O functions - * @ingroup W5500 - * - * @brief This supports the basic I/O functions for @ref WIZCHIP_register. - * - * - Basic I/O function \n - * WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() \n\n - * - * - @ref Common_register_group access functions \n - * -# @b Mode \n - * getMR(), setMR() - * -# @b Interrupt \n - * getIR(), setIR(), getIMR(), setIMR(), getSIR(), setSIR(), getSIMR(), setSIMR(), getINTLEVEL(), setINTLEVEL() - * -# Network Information \n - * getSHAR(), setSHAR(), getGAR(), setGAR(), getSUBR(), setSUBR(), getSIPR(), setSIPR() - * -# @b Retransmission \n - * getRCR(), setRCR(), getRTR(), setRTR() - * -# @b PPPoE \n - * getPTIMER(), setPTIMER(), getPMAGIC(), getPMAGIC(), getPSID(), setPSID(), getPHAR(), setPHAR(), getPMRU(), setPMRU() - * -# ICMP packet \n - * getUIPR(), getUPORTR() - * -# @b etc. \n - * getPHYCFGR(), setPHYCFGR(), getVERSIONR() \n\n - * - * - \ref Socket_register_group access functions \n - * -# SOCKET control \n - * getSn_MR(), setSn_MR(), getSn_CR(), setSn_CR(), getSn_IMR(), setSn_IMR(), getSn_IR(), setSn_IR() - * -# SOCKET information \n - * getSn_SR(), getSn_DHAR(), setSn_DHAR(), getSn_PORT(), setSn_PORT(), getSn_DIPR(), setSn_DIPR(), getSn_DPORT(), setSn_DPORT() - * getSn_MSSR(), setSn_MSSR() - * -# SOCKET communication \n - * getSn_RXBUF_SIZE(), setSn_RXBUF_SIZE(), getSn_TXBUF_SIZE(), setSn_TXBUF_SIZE() \n - * getSn_TX_RD(), getSn_TX_WR(), setSn_TX_WR() \n - * getSn_RX_RD(), setSn_RX_RD(), getSn_RX_WR() \n - * getSn_TX_FSR(), getSn_RX_RSR(), getSn_KPALVTR(), setSn_KPALVTR() - * -# IP header field \n - * getSn_FRAG(), setSn_FRAG(), getSn_TOS(), setSn_TOS() \n - * getSn_TTL(), setSn_TTL() - */ - - - -/** - * @defgroup Common_register_group Common register - * @ingroup WIZCHIP_register - * - * @brief Common register group\n - * It set the basic for the networking\n - * It set the configuration such as interrupt, network information, ICMP, etc. - * @details - * @sa MR : Mode register. - * @sa GAR, SUBR, SHAR, SIPR - * @sa INTLEVEL, IR, IMR, SIR, SIMR : Interrupt. - * @sa RTR, RCR : Data retransmission. - * @sa PTIMER, PMAGIC, PHAR, PSID, PMRU : PPPoE. - * @sa UIPR, UPORTR : ICMP message. - * @sa PHYCFGR, VERSIONR : etc. - */ - - - -/** - * @defgroup Socket_register_group Socket register - * @ingroup WIZCHIP_register - * - * @brief Socket register group.\n - * Socket register configures and control SOCKETn which is necessary to data communication. - * @details - * @sa Sn_MR, Sn_CR, Sn_IR, Sn_IMR : SOCKETn Control - * @sa Sn_SR, Sn_PORT, Sn_DHAR, Sn_DIPR, Sn_DPORT : SOCKETn Information - * @sa Sn_MSSR, Sn_TOS, Sn_TTL, Sn_KPALVTR, Sn_FRAG : Internet protocol. - * @sa Sn_RXBUF_SIZE, Sn_TXBUF_SIZE, Sn_TX_FSR, Sn_TX_RD, Sn_TX_WR, Sn_RX_RSR, Sn_RX_RD, Sn_RX_WR : Data communication - */ - - - - /** - * @defgroup Basic_IO_function Basic I/O function - * @ingroup WIZCHIP_IO_Functions - * @brief These are basic input/output functions to read values from register or write values to register. - */ - -/** - * @defgroup Common_register_access_function Common register access functions - * @ingroup WIZCHIP_IO_Functions - * @brief These are functions to access common registers. - */ - -/** - * @defgroup Socket_register_access_function Socket register access functions - * @ingroup WIZCHIP_IO_Functions - * @brief These are functions to access socket registers. - */ - -//------------------------------- defgroup end -------------------------------------------- -//----------------------------- W5500 Common Registers IOMAP ----------------------------- -/** - * @ingroup Common_register_group - * @brief Mode Register address(R/W)\n - * @ref MR is used for S/W reset, ping block mode, PPPoE mode and etc. - * @details Each bit of @ref MR defined as follows. - * - * - * - *
7 6 5 4 3 2 1 0
RST Reserved WOL PB PPPoE Reserved FARP Reserved
- * - \ref MR_RST : Reset - * - \ref MR_WOL : Wake on LAN - * - \ref MR_PB : Ping block - * - \ref MR_PPPOE : PPPoE mode - * - \ref MR_FARP : Force ARP mode - */ -#define MR WIZCHIP_CREG_ADDR(0x0000) - -/** - * @ingroup Common_register_group - * @brief Gateway IP Register address(R/W) - * @details @ref GAR configures the default gateway address. - */ -#define GAR WIZCHIP_CREG_ADDR(0x0001) - -/** - * @ingroup Common_register_group - * @brief Subnet mask Register address(R/W) - * @details @ref SUBR configures the subnet mask address. - */ -#define SUBR WIZCHIP_CREG_ADDR(0x0005) - -/** - * @ingroup Common_register_group - * @brief Source MAC Register address(R/W) - * @details @ref SHAR configures the source hardware address. - */ -#define SHAR WIZCHIP_CREG_ADDR(0x0009) - -/** - * @ingroup Common_register_group - * @brief Source IP Register address(R/W) - * @details @ref SIPR configures the source IP address. - */ -#define SIPR WIZCHIP_CREG_ADDR(0x000f) - -/** - * @ingroup Common_register_group - * @brief Set Interrupt low level timer register address(R/W) - * @details @ref INTLEVEL configures the Interrupt Assert Time. - */ -//#define INTLEVEL (_W5500_IO_BASE_ + (0x0013 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Interrupt Register(R/W) - * @details @ref IR indicates the interrupt status. Each bit of @ref IR will be still until the bit will be written to by the host. - * If @ref IR is not equal to x00 INTn PIN is asserted to low until it is x00\n\n - * Each bit of @ref IR defined as follows. - * - * - * - *
7 6 5 4 3 2 1 0
CONFLICT UNREACH PPPoE MP Reserved Reserved Reserved Reserved
- * - \ref IR_CONFLICT : IP conflict - * - \ref IR_UNREACH : Destination unreachable - * - \ref IR_PPPoE : PPPoE connection close - * - \ref IR_MP : Magic packet - */ -#define IR WIZCHIP_CREG_ADDR(0x0015) - -/** - * @ingroup Common_register_group - * @brief Interrupt mask register(R/W) - * @details @ref IMR is used to mask interrupts. Each bit of @ref IMR corresponds to each bit of @ref IR. - * When a bit of @ref IMR is and the corresponding bit of @ref IR is an interrupt will be issued. In other words, - * if a bit of @ref IMR is an interrupt will not be issued even if the corresponding bit of @ref IR is \n\n - * Each bit of @ref IMR defined as the following. - * - * - * - *
7 6 5 4 3 2 1 0
IM_IR7 IM_IR6 IM_IR5 IM_IR4 Reserved Reserved Reserved Reserved
- * - \ref IM_IR7 : IP Conflict Interrupt Mask - * - \ref IM_IR6 : Destination unreachable Interrupt Mask - * - \ref IM_IR5 : PPPoE Close Interrupt Mask - * - \ref IM_IR4 : Magic Packet Interrupt Mask - */ -#define IMR WIZCHIP_CREG_ADDR(0x0016) - -/** - * @ingroup Common_register_group - * @brief Socket Interrupt Register(R/W) - * @details @ref SIR indicates the interrupt status of Socket.\n - * Each bit of @ref SIR be still until @ref Sn_IR is cleared by the host.\n - * If @ref Sn_IR is not equal to x00 the n-th bit of @ref SIR is and INTn PIN is asserted until @ref SIR is x00 */ -//#define SIR (_W5500_IO_BASE_ + (0x0017 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Socket Interrupt Mask Register(R/W) - * @details Each bit of @ref SIMR corresponds to each bit of @ref SIR. - * When a bit of @ref SIMR is and the corresponding bit of @ref SIR is Interrupt will be issued. - * In other words, if a bit of @ref SIMR is an interrupt will be not issued even if the corresponding bit of @ref SIR is - */ -//#define SIMR (_W5500_IO_BASE_ + (0x0018 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Timeout register address( 1 is 100us )(R/W) - * @details @ref RTR configures the retransmission timeout period. The unit of timeout period is 100us and the default of @ref RTR is x07D0or 000 - * And so the default timeout period is 200ms(100us X 2000). During the time configured by @ref RTR, W5500 waits for the peer response - * to the packet that is transmitted by \ref Sn_CR (CONNECT, DISCON, CLOSE, SEND, SEND_MAC, SEND_KEEP command). - * If the peer does not respond within the @ref RTR time, W5500 retransmits the packet or issues timeout. - */ -#define RTR WIZCHIP_CREG_ADDR(0x0017) - -/** - * @ingroup Common_register_group - * @brief Retry count register(R/W) - * @details @ref RCR configures the number of time of retransmission. - * When retransmission occurs as many as ref RCR+1 Timeout interrupt is issued (@ref Sn_IR[TIMEOUT] = . - */ -#define RCR WIZCHIP_CREG_ADDR(0x0019) - -/** - * @ingroup Common_register_group - * @brief PPP LCP Request Timer register in PPPoE mode(R/W) - * @details @ref PTIMER configures the time for sending LCP echo request. The unit of time is 25ms. - */ -#define PTIMER WIZCHIP_CREG_ADDR(0x0028) - -/** - * @ingroup Common_register_group - * @brief PPP LCP Magic number register in PPPoE mode(R/W) - * @details @ref PMAGIC configures the 4bytes magic number to be used in LCP negotiation. - */ -#define PMAGIC WIZCHIP_CREG_ADDR(0x0029) - -/** - * @ingroup Common_register_group - * @brief PPP Destination MAC Register address(R/W) - * @details @ref PHAR configures the PPPoE server hardware address that is acquired during PPPoE connection process. - */ -//#define PHAR (_W5500_IO_BASE_ + (0x001E << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP Session Identification Register(R/W) - * @details @ref PSID configures the PPPoE sever session ID acquired during PPPoE connection process. - */ -//#define PSID (_W5500_IO_BASE_ + (0x0024 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP Maximum Segment Size(MSS) register(R/W) - * @details @ref PMRU configures the maximum receive unit of PPPoE. - */ -//#define PMRU (_W5500_IO_BASE_ + (0x0026 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Unreachable IP register address in UDP mode(R) - * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number - * which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR indicates - * the destination IP address & port number respectively. - */ -//#define UIPR (_W5500_IO_BASE_ + (0x002a << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Unreachable Port register address in UDP mode(R) - * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number - * which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR - * indicates the destination IP address & port number respectively. - */ -//#define UPORTR (_W5500_IO_BASE_ + (0x002e << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PHY Status Register(R/W) - * @details @ref PHYCFGR configures PHY operation mode and resets PHY. In addition, @ref PHYCFGR indicates the status of PHY such as duplex, Speed, Link. - */ -//#define PHYCFGR (_W5500_IO_BASE_ + (0x002E << 8) + (WIZCHIP_CREG_BLOCK << 3)) -#define PHYSTATUS WIZCHIP_CREG_ADDR(0x0035) - -// Reserved (_W5500_IO_BASE_ + (0x002F << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0030 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0031 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0032 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0033 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0034 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0035 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0036 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0037 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0038 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief chip version register address(R) - * @details @ref VERSIONR always indicates the W5500 version as @b 0x04. - */ -//#define VERSIONR (_W5200_IO_BASE_ + (0x0039 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - - -//----------------------------- W5500 Socket Registers IOMAP ----------------------------- -/** - * @ingroup Socket_register_group - * @brief socket Mode register(R/W) - * @details @ref Sn_MR configures the option or protocol type of Socket n.\n\n - * Each bit of @ref Sn_MR defined as the following. - * - * - * - *
7 6 5 4 3 2 1 0
MULTI/MFEN BCASTB ND/MC/MMB UCASTB/MIP6B Protocol[3] Protocol[2] Protocol[1] Protocol[0]
- * - @ref Sn_MR_MULTI : Support UDP Multicasting - * - @ref Sn_MR_BCASTB : Broadcast block in UDP Multicasting - * - @ref Sn_MR_ND : No Delayed Ack(TCP) flag - * - @ref Sn_MR_MC : IGMP version used in UDP mulitcasting - * - @ref Sn_MR_MMB : Multicast Blocking in @ref Sn_MR_MACRAW mode - * - @ref Sn_MR_UCASTB : Unicast Block in UDP Multicating - * - @ref Sn_MR_MIP6B : IPv6 packet Blocking in @ref Sn_MR_MACRAW mode - * - Protocol - * - * - * - * - * - * - *
Protocol[3] Protocol[2] Protocol[1] Protocol[0] @b Meaning
0 0 0 0 Closed
0 0 0 1 TCP
0 0 1 0 UDP
0 1 0 0 MACRAW
- * - @ref Sn_MR_MACRAW : MAC LAYER RAW SOCK \n - * - @ref Sn_MR_UDP : UDP - * - @ref Sn_MR_TCP : TCP - * - @ref Sn_MR_CLOSE : Unused socket - * @note MACRAW mode should be only used in Socket 0. - */ -#define Sn_MR(N) WIZCHIP_SREG_ADDR(N, 0x0000) - -/** - * @ingroup Socket_register_group - * @brief Socket command register(R/W) - * @details This is used to set the command for Socket n such as OPEN, CLOSE, CONNECT, LISTEN, SEND, and RECEIVE.\n - * After W5500 accepts the command, the @ref Sn_CR register is automatically cleared to 0x00. - * Even though @ref Sn_CR is cleared to 0x00, the command is still being processed.\n - * To check whether the command is completed or not, please check the @ref Sn_IR or @ref Sn_SR. - * - @ref Sn_CR_OPEN : Initialize or open socket. - * - @ref Sn_CR_LISTEN : Wait connection request in TCP mode(Server mode) - * - @ref Sn_CR_CONNECT : Send connection request in TCP mode(Client mode) - * - @ref Sn_CR_DISCON : Send closing request in TCP mode. - * - @ref Sn_CR_CLOSE : Close socket. - * - @ref Sn_CR_SEND : Update TX buffer pointer and send data. - * - @ref Sn_CR_SEND_MAC : Send data with MAC address, so without ARP process. - * - @ref Sn_CR_SEND_KEEP : Send keep alive message. - * - @ref Sn_CR_RECV : Update RX buffer pointer and receive data. - */ -#define Sn_CR(N) WIZCHIP_SREG_ADDR(N, 0x0001) - -/** - * @ingroup Socket_register_group - * @brief Socket interrupt register(R) - * @details @ref Sn_IR indicates the status of Socket Interrupt such as establishment, termination, receiving data, timeout).\n - * When an interrupt occurs and the corresponding bit of @ref Sn_IMR is the corresponding bit of @ref Sn_IR becomes \n - * In order to clear the @ref Sn_IR bit, the host should write the bit to \n - * - * - * - *
7 6 5 4 3 2 1 0
Reserved Reserved Reserved SEND_OK TIMEOUT RECV DISCON CON
- * - \ref Sn_IR_SENDOK : SEND_OK Interrupt - * - \ref Sn_IR_TIMEOUT : TIMEOUT Interrupt - * - \ref Sn_IR_RECV : RECV Interrupt - * - \ref Sn_IR_DISCON : DISCON Interrupt - * - \ref Sn_IR_CON : CON Interrupt - */ -#define Sn_IR(N) WIZCHIP_SREG_ADDR(N, 0x0002) - -/** - * @ingroup Socket_register_group - * @brief Socket status register(R) - * @details @ref Sn_SR indicates the status of Socket n.\n - * The status of Socket n is changed by @ref Sn_CR or some special control packet as SYN, FIN packet in TCP. - * @par Normal status - * - @ref SOCK_CLOSED : Closed - * - @ref SOCK_INIT : Initiate state - * - @ref SOCK_LISTEN : Listen state - * - @ref SOCK_ESTABLISHED : Success to connect - * - @ref SOCK_CLOSE_WAIT : Closing state - * - @ref SOCK_UDP : UDP socket - * - @ref SOCK_MACRAW : MAC raw mode socket - *@par Temporary status during changing the status of Socket n. - * - @ref SOCK_SYNSENT : This indicates Socket n sent the connect-request packet (SYN packet) to a peer. - * - @ref SOCK_SYNRECV : It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer. - * - @ref SOCK_FIN_WAIT : Connection state - * - @ref SOCK_CLOSING : Closing state - * - @ref SOCK_TIME_WAIT : Closing state - * - @ref SOCK_LAST_ACK : Closing state - */ -#define Sn_SR(N) WIZCHIP_SREG_ADDR(N, 0x0003) - -/** - * @ingroup Socket_register_group - * @brief source port register(R/W) - * @details @ref Sn_PORT configures the source port number of Socket n. - * It is valid when Socket n is used in TCP/UPD mode. It should be set before OPEN command is ordered. - */ -#define Sn_PORT(N) WIZCHIP_SREG_ADDR(N, 0x0004) - -/** - * @ingroup Socket_register_group - * @brief Peer MAC register address(R/W) - * @details @ref Sn_DHAR configures the destination hardware address of Socket n when using SEND_MAC command in UDP mode or - * it indicates that it is acquired in ARP-process by CONNECT/SEND command. - */ -#define Sn_DHAR(N) WIZCHIP_SREG_ADDR(N, 0x0006) - -/** - * @ingroup Socket_register_group - * @brief Peer IP register address(R/W) - * @details @ref Sn_DIPR configures or indicates the destination IP address of Socket n. It is valid when Socket n is used in TCP/UDP mode. - * In TCP client mode, it configures an IP address of �TCP serverbefore CONNECT command. - * In TCP server mode, it indicates an IP address of �TCP clientafter successfully establishing connection. - * In UDP mode, it configures an IP address of peer to be received the UDP packet by SEND or SEND_MAC command. - */ -#define Sn_DIPR(N) WIZCHIP_SREG_ADDR(N, 0x000c) - -/** - * @ingroup Socket_register_group - * @brief Peer port register address(R/W) - * @details @ref Sn_DPORT configures or indicates the destination port number of Socket n. It is valid when Socket n is used in TCP/UDP mode. - * In �TCP clientmode, it configures the listen port number of �TCP serverbefore CONNECT command. - * In �TCP Servermode, it indicates the port number of TCP client after successfully establishing connection. - * In UDP mode, it configures the port number of peer to be transmitted the UDP packet by SEND/SEND_MAC command. - */ -#define Sn_DPORT(N) WIZCHIP_SREG_ADDR(N, 0x0010) - -/** - * @ingroup Socket_register_group - * @brief Maximum Segment Size(Sn_MSSR0) register address(R/W) - * @details @ref Sn_MSSR configures or indicates the MTU(Maximum Transfer Unit) of Socket n. - */ -#define Sn_MSSR(N) WIZCHIP_SREG_ADDR(N, 0x0012) - -// Reserved (_W5500_IO_BASE_ + (0x0014 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief IP Type of Service(TOS) Register(R/W) - * @details @ref Sn_TOS configures the TOS(Type Of Service field in IP Header) of Socket n. - * It is set before OPEN command. - */ -#define Sn_TOS(N) WIZCHIP_SREG_ADDR(N, 0x0015) -/** - * @ingroup Socket_register_group - * @brief IP Time to live(TTL) Register(R/W) - * @details @ref Sn_TTL configures the TTL(Time To Live field in IP header) of Socket n. - * It is set before OPEN command. - */ -#define Sn_TTL(N) WIZCHIP_SREG_ADDR(N, 0x0016) -// Reserved (_W5500_IO_BASE_ + (0x0017 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0018 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001A << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001D << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Receive memory size register(R/W) - * @details @ref Sn_RXBUF_SIZE configures the RX buffer block size of Socket n. - * Socket n RX Buffer Block size can be configured with 1,2,4,8, and 16 Kbytes. - * If a different size is configured, the data cannot be normally received from a peer. - * Although Socket n RX Buffer Block size is initially configured to 2Kbytes, - * user can re-configure its size using @ref Sn_RXBUF_SIZE. The total sum of @ref Sn_RXBUF_SIZE can not be exceed 16Kbytes. - * When exceeded, the data reception error is occurred. - */ -#define Sn_RXBUF_SIZE(N) WIZCHIP_SREG_ADDR(N, 0x001e) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory size register(R/W) - * @details @ref Sn_TXBUF_SIZE configures the TX buffer block size of Socket n. Socket n TX Buffer Block size can be configured with 1,2,4,8, and 16 Kbytes. - * If a different size is configured, the data can�t be normally transmitted to a peer. - * Although Socket n TX Buffer Block size is initially configured to 2Kbytes, - * user can be re-configure its size using @ref Sn_TXBUF_SIZE. The total sum of @ref Sn_TXBUF_SIZE can not be exceed 16Kbytes. - * When exceeded, the data transmission error is occurred. - */ -#define Sn_TXBUF_SIZE(N) WIZCHIP_SREG_ADDR(N, 0x001f) - -/** - * @ingroup Socket_register_group - * @brief Transmit free memory size register(R) - * @details @ref Sn_TX_FSR indicates the free size of Socket n TX Buffer Block. It is initialized to the configured size by @ref Sn_TXBUF_SIZE. - * Data bigger than @ref Sn_TX_FSR should not be saved in the Socket n TX Buffer because the bigger data overwrites the previous saved data not yet sent. - * Therefore, check before saving the data to the Socket n TX Buffer, and if data is equal or smaller than its checked size, - * transmit the data with SEND/SEND_MAC command after saving the data in Socket n TX buffer. But, if data is bigger than its checked size, - * transmit the data after dividing into the checked size and saving in the Socket n TX buffer. - */ -#define Sn_TX_FSR(N) WIZCHIP_SREG_ADDR(N, 0x0020) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory read pointer register address(R) - * @details @ref Sn_TX_RD is initialized by OPEN command. However, if Sn_MR(P[3:0]) is TCP mode(001, it is re-initialized while connecting with TCP. - * After its initialization, it is auto-increased by SEND command. - * SEND command transmits the saved data from the current @ref Sn_TX_RD to the @ref Sn_TX_WR in the Socket n TX Buffer. - * After transmitting the saved data, the SEND command increases the @ref Sn_TX_RD as same as the @ref Sn_TX_WR. - * If its increment value exceeds the maximum value 0xFFFF, (greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value. - */ -#define Sn_TX_RD(N) WIZCHIP_SREG_ADDR(N, 0x0022) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory write pointer register address(R/W) - * @details @ref Sn_TX_WR is initialized by OPEN command. However, if Sn_MR(P[3:0]) is TCP mode(001, it is re-initialized while connecting with TCP.\n - * It should be read or be updated like as follows.\n - * 1. Read the starting address for saving the transmitting data.\n - * 2. Save the transmitting data from the starting address of Socket n TX buffer.\n - * 3. After saving the transmitting data, update @ref Sn_TX_WR to the increased value as many as transmitting data size. - * If the increment value exceeds the maximum value 0xFFFF(greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value.\n - * 4. Transmit the saved data in Socket n TX Buffer by using SEND/SEND command - */ -#define Sn_TX_WR(N) WIZCHIP_SREG_ADDR(N, 0x0024) - -/** - * @ingroup Socket_register_group - * @brief Received data size register(R) - * @details @ref Sn_RX_RSR indicates the data size received and saved in Socket n RX Buffer. - * @ref Sn_RX_RSR does not exceed the @ref Sn_RXBUF_SIZE and is calculated as the difference between - * �Socket n RX Write Pointer (@ref Sn_RX_WR)and �Socket n RX Read Pointer (@ref Sn_RX_RD) - */ -#define Sn_RX_RSR(N) WIZCHIP_SREG_ADDR(N, 0x0026) - -/** - * @ingroup Socket_register_group - * @brief Read point of Receive memory(R/W) - * @details @ref Sn_RX_RD is initialized by OPEN command. Make sure to be read or updated as follows.\n - * 1. Read the starting save address of the received data.\n - * 2. Read data from the starting address of Socket n RX Buffer.\n - * 3. After reading the received data, Update @ref Sn_RX_RD to the increased value as many as the reading size. - * If the increment value exceeds the maximum value 0xFFFF, that is, is greater than 0x10000 and the carry bit occurs, - * update with the lower 16bits value ignored the carry bit.\n - * 4. Order RECV command is for notifying the updated @ref Sn_RX_RD to W5500. - */ -#define Sn_RX_RD(N) WIZCHIP_SREG_ADDR(N, 0x0028) - -/** - * @ingroup Socket_register_group - * @brief Write point of Receive memory(R) - * @details @ref Sn_RX_WR is initialized by OPEN command and it is auto-increased by the data reception. - * If the increased value exceeds the maximum value 0xFFFF, (greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value. - */ -#define Sn_RX_WR(N) WIZCHIP_SREG_ADDR(N, 0x002a) - -/** - * @ingroup Socket_register_group - * @brief socket interrupt mask register(R) - * @details @ref Sn_IMR masks the interrupt of Socket n. - * Each bit corresponds to each bit of @ref Sn_IR. When a Socket n Interrupt is occurred and the corresponding bit of @ref Sn_IMR is - * the corresponding bit of @ref Sn_IR becomes When both the corresponding bit of @ref Sn_IMR and @ref Sn_IR are and the n-th bit of @ref IR is - * Host is interrupted by asserted INTn PIN to low. - */ -//#define Sn_IMR(N) (_W5500_IO_BASE_ + (0x002C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Fragment field value in IP header register(R/W) - * @details @ref Sn_FRAG configures the FRAG(Fragment field in IP header). - */ -//#define Sn_FRAG(N) (_W5500_IO_BASE_ + (0x002D << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Keep Alive Timer register(R/W) - * @details @ref Sn_KPALVTR configures the transmitting timer of �KEEP ALIVE(KA)packet of SOCKETn. It is valid only in TCP mode, - * and ignored in other modes. The time unit is 5s. - * KA packet is transmittable after @ref Sn_SR is changed to SOCK_ESTABLISHED and after the data is transmitted or received to/from a peer at least once. - * In case of '@ref Sn_KPALVTR > 0', W5500 automatically transmits KA packet after time-period for checking the TCP connection (Auto-keepalive-process). - * In case of '@ref Sn_KPALVTR = 0', Auto-keep-alive-process will not operate, - * and KA packet can be transmitted by SEND_KEEP command by the host (Manual-keep-alive-process). - * Manual-keep-alive-process is ignored in case of '@ref Sn_KPALVTR > 0'. - */ -//#define Sn_KPALVTR(N) (_W5500_IO_BASE_ + (0x002F << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -//#define Sn_TSR(N) (_W5500_IO_BASE_ + (0x0030 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - - -//----------------------------- W5500 Register values ----------------------------- - -/* MODE register values */ -/** - * @brief Reset - * @details If this bit is All internal registers will be initialized. It will be automatically cleared as after S/W reset. - */ -#define MR_RST 0x80 - -/** - * @brief Wake on LAN - * @details 0 : Disable WOL mode\n - * 1 : Enable WOL mode\n - * If WOL mode is enabled and the received magic packet over UDP has been normally processed, the Interrupt PIN (INTn) asserts to low. - * When using WOL mode, the UDP Socket should be opened with any source port number. (Refer to Socket n Mode Register (@ref Sn_MR) for opening Socket.) - * @note The magic packet over UDP supported by W5500 consists of 6 bytes synchronization stream (xFFFFFFFFFFFF and - * 16 times Target MAC address stream in UDP payload. The options such like password are ignored. You can use any UDP source port number for WOL mode. - */ -#define MR_WOL 0x20 - -/** - * @brief Ping block - * @details 0 : Disable Ping block\n - * 1 : Enable Ping block\n - * If the bit is it blocks the response to a ping request. - */ -#define MR_PB 0x10 - -/** - * @brief Enable PPPoE - * @details 0 : DisablePPPoE mode\n - * 1 : EnablePPPoE mode\n - * If you use ADSL, this bit should be - */ -#define MR_PPPOE 0x08 - -/** - * @brief Enable UDP_FORCE_ARP CHECHK - * @details 0 : Disable Force ARP mode\n - * 1 : Enable Force ARP mode\n - * In Force ARP mode, It forces on sending ARP Request whenever data is sent. - */ -#define MR_FARP 0x02 - -/* IR register values */ -/** - * @brief Check IP conflict. - * @details Bit is set as when own source IP address is same with the sender IP address in the received ARP request. - */ -#define IR_CONFLICT 0x80 - -/** - * @brief Get the destination unreachable message in UDP sending. - * @details When receiving the ICMP (Destination port unreachable) packet, this bit is set as - * When this bit is Destination Information such as IP address and Port number may be checked with the corresponding @ref UIPR & @ref UPORTR. - */ -#define IR_UNREACH 0x40 - -/** - * @brief Get the PPPoE close message. - * @details When PPPoE is disconnected during PPPoE mode, this bit is set. - */ -#define IR_PPPoE 0x20 - -/** - * @brief Get the magic packet interrupt. - * @details When WOL mode is enabled and receives the magic packet over UDP, this bit is set. - */ -#define IR_MP 0x10 - - -/* PHYCFGR register value */ -#define PHYCFGR_RST ~(1<<7) //< For PHY reset, must operate AND mask. -#define PHYCFGR_OPMD (1<<6) // Configre PHY with OPMDC value -#define PHYCFGR_OPMDC_ALLA (7<<3) -#define PHYCFGR_OPMDC_PDOWN (6<<3) -#define PHYCFGR_OPMDC_NA (5<<3) -#define PHYCFGR_OPMDC_100FA (4<<3) -#define PHYCFGR_OPMDC_100F (3<<3) -#define PHYCFGR_OPMDC_100H (2<<3) -#define PHYCFGR_OPMDC_10F (1<<3) -#define PHYCFGR_OPMDC_10H (0<<3) -#define PHYCFGR_DPX_FULL (1<<2) -#define PHYCFGR_DPX_HALF (0<<2) -#define PHYCFGR_SPD_100 (1<<1) -#define PHYCFGR_SPD_10 (0<<1) -#define PHYCFGR_LNK_ON (1<<0) -#define PHYCFGR_LNK_OFF (0<<0) - -// PHYSTATUS register -#define PHYSTATUS_POWERDOWN (0x08) -#define PHYSTATUS_LINK (0x20) - -/* IMR register values */ -/** - * @brief IP Conflict Interrupt Mask. - * @details 0: Disable IP Conflict Interrupt\n - * 1: Enable IP Conflict Interrupt - */ -#define IM_IR7 0x80 - -/** - * @brief Destination unreachable Interrupt Mask. - * @details 0: Disable Destination unreachable Interrupt\n - * 1: Enable Destination unreachable Interrupt - */ -#define IM_IR6 0x40 - -/** - * @brief PPPoE Close Interrupt Mask. - * @details 0: Disable PPPoE Close Interrupt\n - * 1: Enable PPPoE Close Interrupt - */ -#define IM_IR5 0x20 - -/** - * @brief Magic Packet Interrupt Mask. - * @details 0: Disable Magic Packet Interrupt\n - * 1: Enable Magic Packet Interrupt - */ -#define IM_IR4 0x10 - -/* Sn_MR Default values */ -/** - * @brief Support UDP Multicasting - * @details 0 : disable Multicasting\n - * 1 : enable Multicasting\n - * This bit is applied only during UDP mode(P[3:0] = 010.\n - * To use multicasting, @ref Sn_DIPR & @ref Sn_DPORT should be respectively configured with the multicast group IP address & port number - * before Socket n is opened by OPEN command of @ref Sn_CR. - */ -#define Sn_MR_MULTI 0x80 - -/** - * @brief Broadcast block in UDP Multicasting. - * @details 0 : disable Broadcast Blocking\n - * 1 : enable Broadcast Blocking\n - * This bit blocks to receive broadcasting packet during UDP mode(P[3:0] = 010.\m - * In addition, This bit does when MACRAW mode(P[3:0] = 100 - */ -//#define Sn_MR_BCASTB 0x40 - -/** - * @brief No Delayed Ack(TCP), Multicast flag - * @details 0 : Disable No Delayed ACK option\n - * 1 : Enable No Delayed ACK option\n - * This bit is applied only during TCP mode (P[3:0] = 001.\n - * When this bit is It sends the ACK packet without delay as soon as a Data packet is received from a peer.\n - * When this bit is It sends the ACK packet after waiting for the timeout time configured by @ref RTR. - */ -#define Sn_MR_ND 0x20 - -/** - * @brief Unicast Block in UDP Multicasting - * @details 0 : disable Unicast Blocking\n - * 1 : enable Unicast Blocking\n - * This bit blocks receiving the unicast packet during UDP mode(P[3:0] = 010 and MULTI = - */ -//#define Sn_MR_UCASTB 0x10 - -/** - * @brief MAC LAYER RAW SOCK - * @details This configures the protocol mode of Socket n. - * @note MACRAW mode should be only used in Socket 0. - */ -#define Sn_MR_MACRAW 0x04 - -#define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */ - -/** - * @brief UDP - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_UDP 0x02 - -/** - * @brief TCP - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_TCP 0x01 - -/** - * @brief Unused socket - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_CLOSE 0x00 - -/* Sn_MR values used with Sn_MR_MACRAW */ -/** - * @brief MAC filter enable in @ref Sn_MR_MACRAW mode - * @details 0 : disable MAC Filtering\n - * 1 : enable MAC Filtering\n - * This bit is applied only during MACRAW mode(P[3:0] = 100.\n - * When set as W5500 can only receive broadcasting packet or packet sent to itself. - * When this bit is W5500 can receive all packets on Ethernet. - * If user wants to implement Hybrid TCP/IP stack, - * it is recommended that this bit is set as for reducing host overhead to process the all received packets. - */ -#define Sn_MR_MFEN Sn_MR_MULTI - -/** - * @brief Multicast Blocking in @ref Sn_MR_MACRAW mode - * @details 0 : using IGMP version 2\n - * 1 : using IGMP version 1\n - * This bit is applied only during UDP mode(P[3:0] = 010 and MULTI = - * It configures the version for IGMP messages (Join/Leave/Report). - */ -#define Sn_MR_MMB Sn_MR_ND - -/** - * @brief IPv6 packet Blocking in @ref Sn_MR_MACRAW mode - * @details 0 : disable IPv6 Blocking\n - * 1 : enable IPv6 Blocking\n - * This bit is applied only during MACRAW mode (P[3:0] = 100. It blocks to receiving the IPv6 packet. - */ -#define Sn_MR_MIP6B Sn_MR_UCASTB - -/* Sn_MR value used with Sn_MR_UDP & Sn_MR_MULTI */ -/** - * @brief IGMP version used in UDP mulitcasting - * @details 0 : disable Multicast Blocking\n - * 1 : enable Multicast Blocking\n - * This bit is applied only when MACRAW mode(P[3:0] = 100. It blocks to receive the packet with multicast MAC address. - */ -#define Sn_MR_MC Sn_MR_ND - -/* Sn_MR alternate values */ -/** - * @brief For Berkeley Socket API - */ -#define SOCK_STREAM Sn_MR_TCP - -/** - * @brief For Berkeley Socket API - */ -#define SOCK_DGRAM Sn_MR_UDP - - -/* Sn_CR values */ -/** - * @brief Initialize or open socket - * @details Socket n is initialized and opened according to the protocol selected in Sn_MR(P3:P0). - * The table below shows the value of @ref Sn_SR corresponding to @ref Sn_MR.\n - * - * - * - * - * - * - *
\b Sn_MR (P[3:0]) \b Sn_SR
Sn_MR_CLOSE (000
Sn_MR_TCP (001 SOCK_INIT (0x13)
Sn_MR_UDP (010 SOCK_UDP (0x22)
S0_MR_MACRAW (100 SOCK_MACRAW (0x02)
- */ -#define Sn_CR_OPEN 0x01 - -/** - * @brief Wait connection request in TCP mode(Server mode) - * @details This is valid only in TCP mode (Sn_MR(P3:P0) = Sn_MR_TCP). - * In this mode, Socket n operates as a �TCP serverand waits for connection-request (SYN packet) from any �TCP client - * The @ref Sn_SR changes the state from SOCK_INIT to SOCKET_LISTEN. - * When a �TCP clientconnection request is successfully established, - * the @ref Sn_SR changes from SOCK_LISTEN to SOCK_ESTABLISHED and the Sn_IR(0) becomes - * But when a �TCP clientconnection request is failed, Sn_IR(3) becomes and the status of @ref Sn_SR changes to SOCK_CLOSED. - */ -#define Sn_CR_LISTEN 0x02 - -/** - * @brief Send connection request in TCP mode(Client mode) - * @details To connect, a connect-request (SYN packet) is sent to b>TCP serverconfigured by @ref Sn_DIPR & Sn_DPORT(destination address & port). - * If the connect-request is successful, the @ref Sn_SR is changed to @ref SOCK_ESTABLISHED and the Sn_IR(0) becomes \n\n - * The connect-request fails in the following three cases.\n - * 1. When a @b ARPTO occurs (@ref Sn_IR[3] = ) because destination hardware address is not acquired through the ARP-process.\n - * 2. When a @b SYN/ACK packet is not received and @b TCPTO (Sn_IR(3) = )\n - * 3. When a @b RST packet is received instead of a @b SYN/ACK packet. In these cases, @ref Sn_SR is changed to @ref SOCK_CLOSED. - * @note This is valid only in TCP mode and operates when Socket n acts as b>TCP client - */ -#define Sn_CR_CONNECT 0x04 - -/** - * @brief Send closing request in TCP mode - * @details Regardless of b>TCP serveror b>TCP client the DISCON command processes the disconnect-process (b>Active closeor b>Passive close.\n - * @par Active close - * it transmits disconnect-request(FIN packet) to the connected peer\n - * @par Passive close - * When FIN packet is received from peer, a FIN packet is replied back to the peer.\n - * @details When the disconnect-process is successful (that is, FIN/ACK packet is received successfully), @ref Sn_SR is changed to @ref SOCK_CLOSED.\n - * Otherwise, TCPTO occurs (Sn_IR(3)=)= and then @ref Sn_SR is changed to @ref SOCK_CLOSED. - * @note Valid only in TCP mode. - */ -#define Sn_CR_DISCON 0x08 - -/** - * @brief Close socket - * @details Sn_SR is changed to @ref SOCK_CLOSED. - */ -#define Sn_CR_CLOSE 0x10 - -/** - * @brief Update TX buffer pointer and send data - * @details SEND transmits all the data in the Socket n TX buffer.\n - * For more details, please refer to Socket n TX Free Size Register (@ref Sn_TX_FSR), Socket n, - * TX Write Pointer Register(@ref Sn_TX_WR), and Socket n TX Read Pointer Register(@ref Sn_TX_RD). - */ -#define Sn_CR_SEND 0x20 - -/** - * @brief Send data with MAC address, so without ARP process - * @details The basic operation is same as SEND.\n - * Normally SEND transmits data after destination hardware address is acquired by the automatic ARP-process(Address Resolution Protocol).\n - * But SEND_MAC transmits data without the automatic ARP-process.\n - * In this case, the destination hardware address is acquired from @ref Sn_DHAR configured by host, instead of APR-process. - * @note Valid only in UDP mode. - */ -#define Sn_CR_SEND_MAC 0x21 - -/** - * @brief Send keep alive message - * @details It checks the connection status by sending 1byte keep-alive packet.\n - * If the peer can not respond to the keep-alive packet during timeout time, the connection is terminated and the timeout interrupt will occur. - * @note Valid only in TCP mode. - */ -#define Sn_CR_SEND_KEEP 0x22 - -/** - * @brief Update RX buffer pointer and receive data - * @details RECV completes the processing of the received data in Socket n RX Buffer by using a RX read pointer register (@ref Sn_RX_RD).\n - * For more details, refer to Socket n RX Received Size Register (@ref Sn_RX_RSR), Socket n RX Write Pointer Register (@ref Sn_RX_WR), - * and Socket n RX Read Pointer Register (@ref Sn_RX_RD). - */ -#define Sn_CR_RECV 0x40 - -/* Sn_IR values */ -/** - * @brief SEND_OK Interrupt - * @details This is issued when SEND command is completed. - */ -#define Sn_IR_SENDOK 0x10 - -/** - * @brief TIMEOUT Interrupt - * @details This is issued when ARPTO or TCPTO occurs. - */ -#define Sn_IR_TIMEOUT 0x08 - -/** - * @brief RECV Interrupt - * @details This is issued whenever data is received from a peer. - */ -#define Sn_IR_RECV 0x04 - -/** - * @brief DISCON Interrupt - * @details This is issued when FIN or FIN/ACK packet is received from a peer. - */ -#define Sn_IR_DISCON 0x02 - -/** - * @brief CON Interrupt - * @details This is issued one time when the connection with peer is successful and then @ref Sn_SR is changed to @ref SOCK_ESTABLISHED. - */ -#define Sn_IR_CON 0x01 - -/* Sn_SR values */ -/** - * @brief Closed - * @details This indicates that Socket n is released.\N - * When DICON, CLOSE command is ordered, or when a timeout occurs, it is changed to @ref SOCK_CLOSED regardless of previous status. - */ -#define SOCK_CLOSED 0x00 - -/** - * @brief Initiate state - * @details This indicates Socket n is opened with TCP mode.\N - * It is changed to @ref SOCK_INIT when Sn_MR(P[3:0]) = 001and OPEN command is ordered.\N - * After @ref SOCK_INIT, user can use LISTEN /CONNECT command. - */ -#define SOCK_INIT 0x13 - -/** - * @brief Listen state - * @details This indicates Socket n is operating as b>TCP servermode and waiting for connection-request (SYN packet) from a peer (b>TCP client.\n - * It will change to @ref SOCK_ESTALBLISHED when the connection-request is successfully accepted.\n - * Otherwise it will change to @ref SOCK_CLOSED after TCPTO occurred (Sn_IR(TIMEOUT) = . - */ -#define SOCK_LISTEN 0x14 - -/** - * @brief Connection state - * @details This indicates Socket n sent the connect-request packet (SYN packet) to a peer.\n - * It is temporarily shown when @ref Sn_SR is changed from @ref SOCK_INIT to @ref SOCK_ESTABLISHED by CONNECT command.\n - * If connect-accept(SYN/ACK packet) is received from the peer at SOCK_SYNSENT, it changes to @ref SOCK_ESTABLISHED.\n - * Otherwise, it changes to @ref SOCK_CLOSED after TCPTO (@ref Sn_IR[TIMEOUT] = is occurred. - */ -#define SOCK_SYNSENT 0x15 - -/** - * @brief Connection state - * @details It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer.\n - * If socket n sends the response (SYN/ACK packet) to the peer successfully, it changes to @ref SOCK_ESTABLISHED. \n - * If not, it changes to @ref SOCK_CLOSED after timeout occurs (@ref Sn_IR[TIMEOUT] = . - */ -#define SOCK_SYNRECV 0x16 - -/** - * @brief Success to connect - * @details This indicates the status of the connection of Socket n.\n - * It changes to @ref SOCK_ESTABLISHED when the b>TCP SERVERprocessed the SYN packet from the b>TCP CLIENTduring @ref SOCK_LISTEN, or - * when the CONNECT command is successful.\n - * During @ref SOCK_ESTABLISHED, DATA packet can be transferred using SEND or RECV command. - */ -#define SOCK_ESTABLISHED 0x17 - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_FIN_WAIT 0x18 - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_CLOSING 0x1A - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_TIME_WAIT 0x1B - -/** - * @brief Closing state - * @details This indicates Socket n received the disconnect-request (FIN packet) from the connected peer.\n - * This is half-closing status, and data can be transferred.\n - * For full-closing, DISCON command is used. But For just-closing, CLOSE command is used. - */ -#define SOCK_CLOSE_WAIT 0x1C - -/** - * @brief Closing state - * @details This indicates Socket n is waiting for the response (FIN/ACK packet) to the disconnect-request (FIN packet) by passive-close.\n - * It changes to @ref SOCK_CLOSED when Socket n received the response successfully, or when timeout occurs (@ref Sn_IR[TIMEOUT] = . - */ -#define SOCK_LAST_ACK 0x1D - -/** - * @brief UDP socket - * @details This indicates Socket n is opened in UDP mode(Sn_MR(P[3:0]) = 010.\n - * It changes to SOCK_UPD when Sn_MR(P[3:0]) = 010 and OPEN command is ordered.\n - * Unlike TCP mode, data can be transfered without the connection-process. - */ -#define SOCK_UDP 0x22 - -//#define SOCK_IPRAW 0x32 /**< IP raw mode socket */ - -/** - * @brief MAC raw mode socket - * @details This indicates Socket 0 is opened in MACRAW mode (S0_MR(P[3:0]) = 100and is valid only in Socket 0.\n - * It changes to SOCK_MACRAW when S0_MR(P[3:0] = 100and OPEN command is ordered.\n - * Like UDP mode socket, MACRAW mode Socket 0 can transfer a MAC packet (Ethernet frame) without the connection-process. - */ -#define SOCK_MACRAW 0x42 - -//#define SOCK_PPPOE 0x5F - -/* IP PROTOCOL */ -#define IPPROTO_IP 0 //< Dummy for IP -#define IPPROTO_ICMP 1 //< Control message protocol -#define IPPROTO_IGMP 2 //< Internet group management protocol -#define IPPROTO_GGP 3 //< Gateway^2 (deprecated) -#define IPPROTO_TCP 6 //< TCP -#define IPPROTO_PUP 12 //< PUP -#define IPPROTO_UDP 17 //< UDP -#define IPPROTO_IDP 22 //< XNS idp -#define IPPROTO_ND 77 //< UNOFFICIAL net disk protocol -#define IPPROTO_RAW 255 //< Raw IP packet - - -/** - * @brief Enter a critical section - * - * @details It is provided to protect your shared code which are executed without distribution. \n \n - * - * In non-OS environment, It can be just implemented by disabling whole interrupt.\n - * In OS environment, You can replace it to critical section api supported by OS. - * - * \sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() - * \sa WIZCHIP_CRITICAL_EXIT() - */ -#define WIZCHIP_CRITICAL_ENTER() WIZCHIP.CRIS._enter() - -/** - * @brief Exit a critical section - * - * @details It is provided to protect your shared code which are executed without distribution. \n\n - * - * In non-OS environment, It can be just implemented by disabling whole interrupt. \n - * In OS environment, You can replace it to critical section api supported by OS. - * - * @sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() - * @sa WIZCHIP_CRITICAL_ENTER() - */ -#ifdef _exit -#undef _exit -#endif -#define WIZCHIP_CRITICAL_EXIT() WIZCHIP.CRIS._exit() - - - -//////////////////////// -// Basic I/O Function // -//////////////////////// - -/** - * @ingroup Basic_IO_function - * @brief It reads 1 byte value from a register. - * @param AddrSel Register address - * @return The value of register - */ -uint8_t WIZCHIP_READ (uint32_t AddrSel); - -/** - * @ingroup Basic_IO_function - * @brief It writes 1 byte value to a register. - * @param AddrSel Register address - * @param wb Write data - * @return void - */ -void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb ); - -/** - * @ingroup Basic_IO_function - * @brief It reads sequence data from registers. - * @param AddrSel Register address - * @param pBuf Pointer buffer to read data - * @param len Data length - */ -void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It writes sequence data to registers. - * @param AddrSel Register address - * @param pBuf Pointer buffer to write data - * @param len Data length - */ -void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len); - -///////////////////////////////// -// Common Register I/O function // -///////////////////////////////// -/** - * @ingroup Common_register_access_function - * @brief Set Mode Register - * @param (uint8_t)mr The value to be set. - * @sa getMR() - */ -#define setMR(mr) \ - WIZCHIP_WRITE(MR,mr) - - -/** - * @ingroup Common_register_access_function - * @brief Get Mode Register - * @return uint8_t. The value of Mode register. - * @sa setMR() - */ -#define getMR() \ - WIZCHIP_READ(MR) - -/** - * @ingroup Common_register_access_function - * @brief Set gateway IP address - * @param (uint8_t*)gar Pointer variable to set gateway IP address. It should be allocated 4 bytes. - * @sa getGAR() - */ -#define setGAR(gar) \ - WIZCHIP_WRITE_BUF(GAR,gar,4) - -/** - * @ingroup Common_register_access_function - * @brief Get gateway IP address - * @param (uint8_t*)gar Pointer variable to get gateway IP address. It should be allocated 4 bytes. - * @sa setGAR() - */ -#define getGAR(gar) \ - WIZCHIP_READ_BUF(GAR,gar,4) - -/** - * @ingroup Common_register_access_function - * @brief Set subnet mask address - * @param (uint8_t*)subr Pointer variable to set subnet mask address. It should be allocated 4 bytes. - * @sa getSUBR() - */ -#define setSUBR(subr) \ - WIZCHIP_WRITE_BUF(SUBR, subr,4) - - -/** - * @ingroup Common_register_access_function - * @brief Get subnet mask address - * @param (uint8_t*)subr Pointer variable to get subnet mask address. It should be allocated 4 bytes. - * @sa setSUBR() - */ -#define getSUBR(subr) \ - WIZCHIP_READ_BUF(SUBR, subr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Set local MAC address - * @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. - * @sa getSHAR() - */ -#define setSHAR(shar) \ - WIZCHIP_WRITE_BUF(SHAR, shar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Get local MAC address - * @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. - * @sa setSHAR() - */ -#define getSHAR(shar) \ - WIZCHIP_READ_BUF(SHAR, shar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Set local IP address - * @param (uint8_t*)sipr Pointer variable to set local IP address. It should be allocated 4 bytes. - * @sa getSIPR() - */ -#define setSIPR(sipr) \ - WIZCHIP_WRITE_BUF(SIPR, sipr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Get local IP address - * @param (uint8_t*)sipr Pointer variable to get local IP address. It should be allocated 4 bytes. - * @sa setSIPR() - */ -#define getSIPR(sipr) \ - WIZCHIP_READ_BUF(SIPR, sipr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Set INTLEVEL register - * @param (uint16_t)intlevel Value to set @ref INTLEVEL register. - * @sa getINTLEVEL() - */ -// dpgeorge: not yet implemented -#define setINTLEVEL(intlevel) (void)intlevel -#if 0 -#define setINTLEVEL(intlevel) {\ - WIZCHIP_WRITE(INTLEVEL, (uint8_t)(intlevel >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(INTLEVEL,1), (uint8_t) intlevel); \ - } -#endif - - -/** - * @ingroup Common_register_access_function - * @brief Get INTLEVEL register - * @return uint16_t. Value of @ref INTLEVEL register. - * @sa setINTLEVEL() - */ -// dpgeorge: not yet implemented -#define getINTLEVEL() (0) -#if 0 -#define getINTLEVEL() \ - ((WIZCHIP_READ(INTLEVEL) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(INTLEVEL,1))) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Set @ref IR register - * @param (uint8_t)ir Value to set @ref IR register. - * @sa getIR() - */ -#define setIR(ir) \ - WIZCHIP_WRITE(IR, (ir & 0xF0)) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IR register - * @return uint8_t. Value of @ref IR register. - * @sa setIR() - */ -#define getIR() \ - (WIZCHIP_READ(IR) & 0xF0) -/** - * @ingroup Common_register_access_function - * @brief Set @ref IMR register - * @param (uint8_t)imr Value to set @ref IMR register. - * @sa getIMR() - */ -#define setIMR(imr) \ - WIZCHIP_WRITE(IMR, imr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IMR register - * @return uint8_t. Value of @ref IMR register. - * @sa setIMR() - */ -#define getIMR() \ - WIZCHIP_READ(IMR) - - -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIR register - * @param (uint8_t)sir Value to set @ref SIR register. - * @sa getSIR() - */ -// dpgeorge: not yet implemented -#define setSIR(sir) ((void)sir) -#if 0 -#define setSIR(sir) \ - WIZCHIP_WRITE(SIR, sir) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIR register - * @return uint8_t. Value of @ref SIR register. - * @sa setSIR() - */ -// dpgeorge: not yet implemented -#define getSIR() (0) -#if 0 -#define getSIR() \ - WIZCHIP_READ(SIR) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIMR register - * @param (uint8_t)simr Value to set @ref SIMR register. - * @sa getSIMR() - */ -// dpgeorge: not yet implemented -#define setSIMR(simr) ((void)simr) -#if 0 -#define setSIMR(simr) \ - WIZCHIP_WRITE(SIMR, simr) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIMR register - * @return uint8_t. Value of @ref SIMR register. - * @sa setSIMR() - */ -// dpgeorge: not yet implemented -#define getSIMR() (0) -#if 0 -#define getSIMR() \ - WIZCHIP_READ(SIMR) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RTR register - * @param (uint16_t)rtr Value to set @ref RTR register. - * @sa getRTR() - */ -#define setRTR(rtr) {\ - WIZCHIP_WRITE(RTR, (uint8_t)(rtr >> 8)); \ - WIZCHIP_WRITE(RTR + 1, (uint8_t) rtr); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RTR register - * @return uint16_t. Value of @ref RTR register. - * @sa setRTR() - */ -#define getRTR() \ - ((WIZCHIP_READ(RTR) << 8) + WIZCHIP_READ(RTR + 1)) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RCR register - * @param (uint8_t)rcr Value to set @ref RCR register. - * @sa getRCR() - */ -#define setRCR(rcr) \ - WIZCHIP_WRITE(RCR, rcr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RCR register - * @return uint8_t. Value of @ref RCR register. - * @sa setRCR() - */ -#define getRCR() \ - WIZCHIP_READ(RCR) - -//================================================== test done =========================================================== - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PTIMER register - * @param (uint8_t)ptimer Value to set @ref PTIMER register. - * @sa getPTIMER() - */ -#define setPTIMER(ptimer) \ - WIZCHIP_WRITE(PTIMER, ptimer) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PTIMER register - * @return uint8_t. Value of @ref PTIMER register. - * @sa setPTIMER() - */ -#define getPTIMER() \ - WIZCHIP_READ(PTIMER) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PMAGIC register - * @param (uint8_t)pmagic Value to set @ref PMAGIC register. - * @sa getPMAGIC() - */ -#define setPMAGIC(pmagic) \ - WIZCHIP_WRITE(PMAGIC, pmagic) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PMAGIC register - * @return uint8_t. Value of @ref PMAGIC register. - * @sa setPMAGIC() - */ -#define getPMAGIC() \ - WIZCHIP_READ(PMAGIC) - -/** - * @ingroup Common_register_access_function - * @brief Set PHAR address - * @param (uint8_t*)phar Pointer variable to set PPP destination MAC register address. It should be allocated 6 bytes. - * @sa getPHAR() - */ -#if 0 -#define setPHAR(phar) \ - WIZCHIP_WRITE_BUF(PHAR, phar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Get local IP address - * @param (uint8_t*)phar Pointer variable to PPP destination MAC register address. It should be allocated 6 bytes. - * @sa setPHAR() - */ -#define getPHAR(phar) \ - WIZCHIP_READ_BUF(PHAR, phar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PSID register - * @param (uint16_t)psid Value to set @ref PSID register. - * @sa getPSID() - */ -#define setPSID(psid) {\ - WIZCHIP_WRITE(PSID, (uint8_t)(psid >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(PSID,1), (uint8_t) psid); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PSID register - * @return uint16_t. Value of @ref PSID register. - * @sa setPSID() - */ -//uint16_t getPSID(void); -#define getPSID() \ - ((WIZCHIP_READ(PSID) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PSID,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PMRU register - * @param (uint16_t)pmru Value to set @ref PMRU register. - * @sa getPMRU() - */ -#define setPMRU(pmru) { \ - WIZCHIP_WRITE(PMRU, (uint8_t)(pmru>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(PMRU,1), (uint8_t) pmru); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PMRU register - * @return uint16_t. Value of @ref PMRU register. - * @sa setPMRU() - */ -#define getPMRU() \ - ((WIZCHIP_READ(PMRU) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PMRU,1))) - -/** - * @ingroup Common_register_access_function - * @brief Get unreachable IP address - * @param (uint8_t*)uipr Pointer variable to get unreachable IP address. It should be allocated 4 bytes. - */ -#define getUIPR(uipr) \ - WIZCHIP_READ_BUF(UIPR,uipr,6) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref UPORTR register - * @return uint16_t. Value of @ref UPORTR register. - */ -#define getUPORTR() \ - ((WIZCHIP_READ(UPORTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(UPORTR,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PHYCFGR register - * @param (uint8_t)phycfgr Value to set @ref PHYCFGR register. - * @sa getPHYCFGR() - */ -#define setPHYCFGR(phycfgr) \ - WIZCHIP_WRITE(PHYCFGR, phycfgr) -#endif - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PHYCFGR register - * @return uint8_t. Value of @ref PHYCFGR register. - * @sa setPHYCFGR() - */ -#define getPHYSTATUS() \ - WIZCHIP_READ(PHYSTATUS) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref VERSIONR register - * @return uint8_t. Value of @ref VERSIONR register. - */ -/* -#define getVERSIONR() \ - WIZCHIP_READ(VERSIONR) - */ -///////////////////////////////////// - -/////////////////////////////////// -// Socket N register I/O function // -/////////////////////////////////// -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)mr Value to set @ref Sn_MR - * @sa getSn_MR() - */ -#define setSn_MR(sn, mr) \ - WIZCHIP_WRITE(Sn_MR(sn),mr) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_MR. - * @sa setSn_MR() - */ -#define getSn_MR(sn) \ - WIZCHIP_READ(Sn_MR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)cr Value to set @ref Sn_CR - * @sa getSn_CR() - */ -#define setSn_CR(sn, cr) \ - WIZCHIP_WRITE(Sn_CR(sn), cr) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_CR. - * @sa setSn_CR() - */ -#define getSn_CR(sn) \ - WIZCHIP_READ(Sn_CR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)ir Value to set @ref Sn_IR - * @sa getSn_IR() - */ -#define setSn_IR(sn, ir) \ - WIZCHIP_WRITE(Sn_IR(sn), (ir & 0x1F)) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_IR. - * @sa setSn_IR() - */ -#define getSn_IR(sn) \ - (WIZCHIP_READ(Sn_IR(sn)) & 0x1F) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)imr Value to set @ref Sn_IMR - * @sa getSn_IMR() - */ -// dpgeorge: not yet implemented -#define setSn_IMR(sn, imr) (void)sn; (void)imr -#if 0 -#define setSn_IMR(sn, imr) \ - WIZCHIP_WRITE(Sn_IMR(sn), (imr & 0x1F)) -#endif - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_IMR. - * @sa setSn_IMR() - */ -// dpgeorge: not yet implemented -#define getSn_IMR(sn) (0) -#if 0 -#define getSn_IMR(sn) \ - (WIZCHIP_READ(Sn_IMR(sn)) & 0x1F) -#endif - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_SR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_SR. - */ -#define getSn_SR(sn) \ - WIZCHIP_READ(Sn_SR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)port Value to set @ref Sn_PORT. - * @sa getSn_PORT() - */ -#define setSn_PORT(sn, port) { \ - WIZCHIP_WRITE(Sn_PORT(sn), (uint8_t)(port >> 8)); \ - WIZCHIP_WRITE(Sn_PORT(sn) + 1, (uint8_t) port); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_PORT. - * @sa setSn_PORT() - */ -#define getSn_PORT(sn) \ - ((WIZCHIP_READ(Sn_PORT(sn)) << 8) | WIZCHIP_READ(Sn_PORT(sn) + 1)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DHAR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dhar Pointer variable to set socket n destination hardware address. It should be allocated 6 bytes. - * @sa getSn_DHAR() - */ -#define setSn_DHAR(sn, dhar) \ - WIZCHIP_WRITE_BUF(Sn_DHAR(sn), dhar, 6) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dhar Pointer variable to get socket n destination hardware address. It should be allocated 6 bytes. - * @sa setSn_DHAR() - */ -#define getSn_DHAR(sn, dhar) \ - WIZCHIP_READ_BUF(Sn_DHAR(sn), dhar, 6) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dipr Pointer variable to set socket n destination IP address. It should be allocated 4 bytes. - * @sa getSn_DIPR() - */ -#define setSn_DIPR(sn, dipr) \ - WIZCHIP_WRITE_BUF(Sn_DIPR(sn), dipr, 4) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes. - * @sa SetSn_DIPR() - */ -#define getSn_DIPR(sn, dipr) \ - WIZCHIP_READ_BUF(Sn_DIPR(sn), dipr, 4) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)dport Value to set @ref Sn_DPORT - * @sa getSn_DPORT() - */ -#define setSn_DPORT(sn, dport) { \ - WIZCHIP_WRITE(Sn_DPORT(sn), (uint8_t) (dport>>8)); \ - WIZCHIP_WRITE(Sn_DPORT(sn) + 1, (uint8_t) dport); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_DPORT. - * @sa setSn_DPORT() - */ -#define getSn_DPORT(sn) \ - ((WIZCHIP_READ(Sn_DPORT(sn)) << 8) + WIZCHIP_READ((Sn_DPORT(sn)+1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_MSSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)mss Value to set @ref Sn_MSSR - * @sa setSn_MSSR() - */ -#define setSn_MSSR(sn, mss) { \ - WIZCHIP_WRITE(Sn_MSSR(sn), (uint8_t)(mss>>8)); \ - WIZCHIP_WRITE((Sn_MSSR(sn)+1), (uint8_t) mss); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MSSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_MSSR. - * @sa setSn_MSSR() - */ -#define getSn_MSSR(sn) \ - ((WIZCHIP_READ(Sn_MSSR(sn)) << 8) + WIZCHIP_READ((Sn_MSSR(sn)+1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TOS register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)tos Value to set @ref Sn_TOS - * @sa getSn_TOS() - */ -#define setSn_TOS(sn, tos) \ - WIZCHIP_WRITE(Sn_TOS(sn), tos) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TOS register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of Sn_TOS. - * @sa setSn_TOS() - */ -#define getSn_TOS(sn) \ - WIZCHIP_READ(Sn_TOS(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TTL register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)ttl Value to set @ref Sn_TTL - * @sa getSn_TTL() - */ -#define setSn_TTL(sn, ttl) \ - WIZCHIP_WRITE(Sn_TTL(sn), ttl) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TTL register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_TTL. - * @sa setSn_TTL() - */ -#define getSn_TTL(sn) \ - WIZCHIP_READ(Sn_TTL(sn)) - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)rxbufsize Value to set @ref Sn_RXBUF_SIZE - * @sa getSn_RXBUF_SIZE() - */ -#define setSn_RXBUF_SIZE(sn, rxbufsize) \ - WIZCHIP_WRITE(Sn_RXBUF_SIZE(sn),rxbufsize) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_RXBUF_SIZE. - * @sa setSn_RXBUF_SIZE() - */ -#define getSn_RXBUF_SIZE(sn) \ - WIZCHIP_READ(Sn_RXBUF_SIZE(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)txbufsize Value to set @ref Sn_TXBUF_SIZE - * @sa getSn_TXBUF_SIZE() - */ -#define setSn_TXBUF_SIZE(sn, txbufsize) \ - WIZCHIP_WRITE(Sn_TXBUF_SIZE(sn), txbufsize) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_TXBUF_SIZE. - * @sa setSn_TXBUF_SIZE() - */ -#define getSn_TXBUF_SIZE(sn) \ - WIZCHIP_READ(Sn_TXBUF_SIZE(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_FSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_FSR. - */ -uint16_t getSn_TX_FSR(uint8_t sn); - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_RD. - */ -#define getSn_TX_RD(sn) \ - ((WIZCHIP_READ(Sn_TX_RD(sn)) << 8) + WIZCHIP_READ((Sn_TX_RD(sn)+1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)txwr Value to set @ref Sn_TX_WR - * @sa GetSn_TX_WR() - */ -#define setSn_TX_WR(sn, txwr) { \ - WIZCHIP_WRITE(Sn_TX_WR(sn), (uint8_t)(txwr>>8)); \ - WIZCHIP_WRITE((Sn_TX_WR(sn)+1), (uint8_t) txwr); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_WR. - * @sa setSn_TX_WR() - */ -#define getSn_TX_WR(sn) \ - ((WIZCHIP_READ(Sn_TX_WR(sn)) << 8) + WIZCHIP_READ((Sn_TX_WR(sn)+1))) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_RX_RSR. - */ -uint16_t getSn_RX_RSR(uint8_t sn); - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)rxrd Value to set @ref Sn_RX_RD - * @sa getSn_RX_RD() - */ -#define setSn_RX_RD(sn, rxrd) { \ - WIZCHIP_WRITE(Sn_RX_RD(sn), (uint8_t)(rxrd>>8)); \ - WIZCHIP_WRITE((Sn_RX_RD(sn)+1), (uint8_t) rxrd); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @regurn uint16_t. Value of @ref Sn_RX_RD. - * @sa setSn_RX_RD() - */ -#define getSn_RX_RD(sn) \ - ((WIZCHIP_READ(Sn_RX_RD(sn)) << 8) + WIZCHIP_READ((Sn_RX_RD(sn)+1))) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_RX_WR. - */ -#define getSn_RX_WR(sn) \ - ((WIZCHIP_READ(Sn_RX_WR(sn)) << 8) + WIZCHIP_READ((Sn_RX_WR(sn)+1))) - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_FRAG register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)frag Value to set @ref Sn_FRAG - * @sa getSn_FRAD() - */ -#if 0 // dpgeorge -#define setSn_FRAG(sn, frag) { \ - WIZCHIP_WRITE(Sn_FRAG(sn), (uint8_t)(frag >>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1), (uint8_t) frag); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_FRAG register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_FRAG. - * @sa setSn_FRAG() - */ -#define getSn_FRAG(sn) \ - ((WIZCHIP_READ(Sn_FRAG(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_KPALVTR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)kpalvt Value to set @ref Sn_KPALVTR - * @sa getSn_KPALVTR() - */ -#define setSn_KPALVTR(sn, kpalvt) \ - WIZCHIP_WRITE(Sn_KPALVTR(sn), kpalvt) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_KPALVTR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_KPALVTR. - * @sa setSn_KPALVTR() - */ -#define getSn_KPALVTR(sn) \ - WIZCHIP_READ(Sn_KPALVTR(sn)) - -////////////////////////////////////// -#endif - -///////////////////////////////////// -// Sn_TXBUF & Sn_RXBUF IO function // -///////////////////////////////////// -/** - * @brief Gets the max buffer size of socket sn passed as parameter. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of Socket n RX max buffer size. - */ -#define getSn_RxMAX(sn) \ - (getSn_RXBUF_SIZE(sn) << 10) - -/** - * @brief Gets the max buffer size of socket sn passed as parameters. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of Socket n TX max buffer size. - */ -//uint16_t getSn_TxMAX(uint8_t sn); -#define getSn_TxMAX(sn) \ - (getSn_TXBUF_SIZE(sn) << 10) - -void wiz_init(void); - -/** - * @ingroup Basic_IO_function - * @brief It copies data to internal TX memory - * - * @details This function reads the Tx write pointer register and after that, - * it copies the wizdata(pointer buffer) of the length of len(variable) bytes to internal TX memory - * and updates the Tx write pointer register. - * This function is being called by send() and sendto() function also. - * - * @note User should read upper byte first and lower byte later to get proper value. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param wizdata Pointer buffer to write data - * @param len Data length - * @sa wiz_recv_data() - */ -void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It copies data to your buffer from internal RX memory - * - * @details This function read the Rx read pointer register and after that, - * it copies the received data from internal RX memory - * to wizdata(pointer variable) of the length of len(variable) bytes. - * This function is being called by recv() also. - * - * @note User should read upper byte first and lower byte later to get proper value. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param wizdata Pointer buffer to read data - * @param len Data length - * @sa wiz_send_data() - */ -void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It discard the received data in RX memory. - * @details It discards the data of the length of len(variable) bytes in internal RX memory. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param len Data length - */ -void wiz_recv_ignore(uint8_t sn, uint16_t len); - -#endif // _W5500_H_ diff --git a/drivers/wiznet5k/ethernet/w5500/w5500.c b/drivers/wiznet5k/ethernet/w5500/w5500.c deleted file mode 100644 index 3107b1b71aa39..0000000000000 --- a/drivers/wiznet5k/ethernet/w5500/w5500.c +++ /dev/null @@ -1,247 +0,0 @@ -//***************************************************************************** -// -//! \file w5500.c -//! \brief W5500 HAL Interface. -//! \version 1.0.1 -//! \date 2013/10/21 -//! \par Revision history -//! <2014/05/01> V1.0.2 -//! 1. Implicit type casting -> Explicit type casting. Refer to M20140501 -//! Fixed the problem on porting into under 32bit MCU -//! Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh -//! Thank for your interesting and serious advices. -//! <2013/10/21> 1st Release -//! <2013/12/20> V1.0.1 -//! 1. Remove warning -//! 2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_ -//! for loop optimized(removed). refer to M20131220 -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** -//#include -#include "w5500.h" - -#define _W5500_SPI_VDM_OP_ 0x00 -#define _W5500_SPI_FDM_OP_LEN1_ 0x01 -#define _W5500_SPI_FDM_OP_LEN2_ 0x02 -#define _W5500_SPI_FDM_OP_LEN4_ 0x03 - -//////////////////////////////////////////////////// - -#define LPC_SSP0 (0) - -static void Chip_SSP_ReadFrames_Blocking(int dummy, uint8_t *buf, uint32_t len) { - WIZCHIP.IF.SPI._read_bytes(buf, len); -} - -static void Chip_SSP_WriteFrames_Blocking(int dummy, const uint8_t *buf, uint32_t len) { - WIZCHIP.IF.SPI._write_bytes(buf, len); -} - -uint8_t WIZCHIP_READ(uint32_t AddrSel) -{ - uint8_t ret; - uint8_t spi_data[3]; - - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_); - - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); - //ret = WIZCHIP.IF.SPI._read_byte(); - spi_data[0] = (AddrSel & 0x00FF0000) >> 16; - spi_data[1] = (AddrSel & 0x0000FF00) >> 8; - spi_data[2] = (AddrSel & 0x000000FF) >> 0; - Chip_SSP_WriteFrames_Blocking(LPC_SSP0, spi_data, 3); - Chip_SSP_ReadFrames_Blocking(LPC_SSP0, &ret, 1); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); - return ret; -} - -void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb ) -{ - uint8_t spi_data[4]; - - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_); - - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); - //WIZCHIP.IF.SPI._write_byte(wb); - spi_data[0] = (AddrSel & 0x00FF0000) >> 16; - spi_data[1] = (AddrSel & 0x0000FF00) >> 8; - spi_data[2] = (AddrSel & 0x000000FF) >> 0; - spi_data[3] = wb; - Chip_SSP_WriteFrames_Blocking(LPC_SSP0, spi_data, 4); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - -void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len) -{ - uint8_t spi_data[3]; - //uint16_t i; - - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_); - - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); - //for(i = 0; i < len; i++) - // pBuf[i] = WIZCHIP.IF.SPI._read_byte(); - spi_data[0] = (AddrSel & 0x00FF0000) >> 16; - spi_data[1] = (AddrSel & 0x0000FF00) >> 8; - spi_data[2] = (AddrSel & 0x000000FF) >> 0; - Chip_SSP_WriteFrames_Blocking(LPC_SSP0, spi_data, 3); - Chip_SSP_ReadFrames_Blocking(LPC_SSP0, pBuf, len); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - -void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) -{ - uint8_t spi_data[3]; - //uint16_t i; - - WIZCHIP_CRITICAL_ENTER(); - WIZCHIP.CS._select(); - - AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_); - - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); - //WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); - //for(i = 0; i < len; i++) - // WIZCHIP.IF.SPI._write_byte(pBuf[i]); - spi_data[0] = (AddrSel & 0x00FF0000) >> 16; - spi_data[1] = (AddrSel & 0x0000FF00) >> 8; - spi_data[2] = (AddrSel & 0x000000FF) >> 0; - Chip_SSP_WriteFrames_Blocking(LPC_SSP0, spi_data, 3); - Chip_SSP_WriteFrames_Blocking(LPC_SSP0, pBuf, len); - - WIZCHIP.CS._deselect(); - WIZCHIP_CRITICAL_EXIT(); -} - - -uint16_t getSn_TX_FSR(uint8_t sn) -{ - uint16_t val=0,val1=0; - - do - { - val1 = WIZCHIP_READ(Sn_TX_FSR(sn)); - val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1)); - if (val1 != 0) - { - val = WIZCHIP_READ(Sn_TX_FSR(sn)); - val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1)); - } - }while (val != val1); - return val; -} - - -uint16_t getSn_RX_RSR(uint8_t sn) -{ - uint16_t val=0,val1=0; - - do - { - val1 = WIZCHIP_READ(Sn_RX_RSR(sn)); - val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1)); - if (val1 != 0) - { - val = WIZCHIP_READ(Sn_RX_RSR(sn)); - val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1)); - } - }while (val != val1); - return val; -} - -void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len) -{ - uint16_t ptr = 0; - uint32_t addrsel = 0; - - if(len == 0) return; - ptr = getSn_TX_WR(sn); - //M20140501 : implict type casting -> explict type casting - //addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3); - addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3); - // - WIZCHIP_WRITE_BUF(addrsel,wizdata, len); - - ptr += len; - setSn_TX_WR(sn,ptr); -} - -void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len) -{ - uint16_t ptr = 0; - uint32_t addrsel = 0; - - if(len == 0) return; - ptr = getSn_RX_RD(sn); - //M20140501 : implict type casting -> explict type casting - //addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3); - addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3); - // - WIZCHIP_READ_BUF(addrsel, wizdata, len); - ptr += len; - - setSn_RX_RD(sn,ptr); -} - - -void wiz_recv_ignore(uint8_t sn, uint16_t len) -{ - uint16_t ptr = 0; - - ptr = getSn_RX_RD(sn); - ptr += len; - setSn_RX_RD(sn,ptr); -} - diff --git a/drivers/wiznet5k/ethernet/w5500/w5500.h b/drivers/wiznet5k/ethernet/w5500/w5500.h deleted file mode 100644 index c2afb180ebbad..0000000000000 --- a/drivers/wiznet5k/ethernet/w5500/w5500.h +++ /dev/null @@ -1,2057 +0,0 @@ -//***************************************************************************** -// -//! \file w5500.h -//! \brief W5500 HAL Header File. -//! \version 1.0.0 -//! \date 2013/10/21 -//! \par Revision history -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#ifndef _W5500_H_ -#define _W5500_H_ - -#include -#include "../wizchip_conf.h" - -#define _W5500_IO_BASE_ 0x00000000 - -#define _W5500_SPI_READ_ (0x00 << 2) //< SPI interface Read operation in Control Phase -#define _W5500_SPI_WRITE_ (0x01 << 2) //< SPI interface Write operation in Control Phase - -#define WIZCHIP_CREG_BLOCK 0x00 //< Common register block -#define WIZCHIP_SREG_BLOCK(N) (1+4*N) //< Socket N register block -#define WIZCHIP_TXBUF_BLOCK(N) (2+4*N) //< Socket N Tx buffer address block -#define WIZCHIP_RXBUF_BLOCK(N) (3+4*N) //< Socket N Rx buffer address block - -#define WIZCHIP_OFFSET_INC(ADDR, N) (ADDR + (N<<8)) //< Increase offset address - - -/////////////////////////////////////// -// Definition For Legacy Chip Driver // -/////////////////////////////////////// -#define IINCHIP_READ(ADDR) WIZCHIP_READ(ADDR) ///< The defined for legacy chip driver -#define IINCHIP_WRITE(ADDR,VAL) WIZCHIP_WRITE(ADDR,VAL) ///< The defined for legacy chip driver -#define IINCHIP_READ_BUF(ADDR,BUF,LEN) WIZCHIP_READ_BUF(ADDR,BUF,LEN) ///< The defined for legacy chip driver -#define IINCHIP_WRITE_BUF(ADDR,BUF,LEN) WIZCHIP_WRITE(ADDR,BUF,LEN) ///< The defined for legacy chip driver - -////////////////////////////// -//-------------------------- defgroup --------------------------------- -/** - * @defgroup W5500 W5500 - * - * @brief WHIZCHIP register defines and I/O functions of @b W5500. - * - * - @ref WIZCHIP_register : @ref Common_register_group and @ref Socket_register_group - * - @ref WIZCHIP_IO_Functions : @ref Basic_IO_function, @ref Common_register_access_function and @ref Socket_register_access_function - */ - - -/** - * @defgroup WIZCHIP_register WIZCHIP register - * @ingroup W5500 - * - * @brief WHIZCHIP register defines register group of @b W5500. - * - * - @ref Common_register_group : Common register group - * - @ref Socket_register_group : \c SOCKET n register group - */ - - -/** - * @defgroup WIZCHIP_IO_Functions WIZCHIP I/O functions - * @ingroup W5500 - * - * @brief This supports the basic I/O functions for @ref WIZCHIP_register. - * - * - Basic I/O function \n - * WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() \n\n - * - * - @ref Common_register_group access functions \n - * -# @b Mode \n - * getMR(), setMR() - * -# @b Interrupt \n - * getIR(), setIR(), getIMR(), setIMR(), getSIR(), setSIR(), getSIMR(), setSIMR(), getINTLEVEL(), setINTLEVEL() - * -# Network Information \n - * getSHAR(), setSHAR(), getGAR(), setGAR(), getSUBR(), setSUBR(), getSIPR(), setSIPR() - * -# @b Retransmission \n - * getRCR(), setRCR(), getRTR(), setRTR() - * -# @b PPPoE \n - * getPTIMER(), setPTIMER(), getPMAGIC(), getPMAGIC(), getPSID(), setPSID(), getPHAR(), setPHAR(), getPMRU(), setPMRU() - * -# ICMP packet \n - * getUIPR(), getUPORTR() - * -# @b etc. \n - * getPHYCFGR(), setPHYCFGR(), getVERSIONR() \n\n - * - * - \ref Socket_register_group access functions \n - * -# SOCKET control \n - * getSn_MR(), setSn_MR(), getSn_CR(), setSn_CR(), getSn_IMR(), setSn_IMR(), getSn_IR(), setSn_IR() - * -# SOCKET information \n - * getSn_SR(), getSn_DHAR(), setSn_DHAR(), getSn_PORT(), setSn_PORT(), getSn_DIPR(), setSn_DIPR(), getSn_DPORT(), setSn_DPORT() - * getSn_MSSR(), setSn_MSSR() - * -# SOCKET communication \n - * getSn_RXBUF_SIZE(), setSn_RXBUF_SIZE(), getSn_TXBUF_SIZE(), setSn_TXBUF_SIZE() \n - * getSn_TX_RD(), getSn_TX_WR(), setSn_TX_WR() \n - * getSn_RX_RD(), setSn_RX_RD(), getSn_RX_WR() \n - * getSn_TX_FSR(), getSn_RX_RSR(), getSn_KPALVTR(), setSn_KPALVTR() - * -# IP header field \n - * getSn_FRAG(), setSn_FRAG(), getSn_TOS(), setSn_TOS() \n - * getSn_TTL(), setSn_TTL() - */ - - - -/** - * @defgroup Common_register_group Common register - * @ingroup WIZCHIP_register - * - * @brief Common register group\n - * It set the basic for the networking\n - * It set the configuration such as interrupt, network information, ICMP, etc. - * @details - * @sa MR : Mode register. - * @sa GAR, SUBR, SHAR, SIPR - * @sa INTLEVEL, IR, IMR, SIR, SIMR : Interrupt. - * @sa RTR, RCR : Data retransmission. - * @sa PTIMER, PMAGIC, PHAR, PSID, PMRU : PPPoE. - * @sa UIPR, UPORTR : ICMP message. - * @sa PHYCFGR, VERSIONR : etc. - */ - - - -/** - * @defgroup Socket_register_group Socket register - * @ingroup WIZCHIP_register - * - * @brief Socket register group.\n - * Socket register configures and control SOCKETn which is necessary to data communication. - * @details - * @sa Sn_MR, Sn_CR, Sn_IR, Sn_IMR : SOCKETn Control - * @sa Sn_SR, Sn_PORT, Sn_DHAR, Sn_DIPR, Sn_DPORT : SOCKETn Information - * @sa Sn_MSSR, Sn_TOS, Sn_TTL, Sn_KPALVTR, Sn_FRAG : Internet protocol. - * @sa Sn_RXBUF_SIZE, Sn_TXBUF_SIZE, Sn_TX_FSR, Sn_TX_RD, Sn_TX_WR, Sn_RX_RSR, Sn_RX_RD, Sn_RX_WR : Data communication - */ - - - - /** - * @defgroup Basic_IO_function Basic I/O function - * @ingroup WIZCHIP_IO_Functions - * @brief These are basic input/output functions to read values from register or write values to register. - */ - -/** - * @defgroup Common_register_access_function Common register access functions - * @ingroup WIZCHIP_IO_Functions - * @brief These are functions to access common registers. - */ - -/** - * @defgroup Socket_register_access_function Socket register access functions - * @ingroup WIZCHIP_IO_Functions - * @brief These are functions to access socket registers. - */ - -//------------------------------- defgroup end -------------------------------------------- -//----------------------------- W5500 Common Registers IOMAP ----------------------------- -/** - * @ingroup Common_register_group - * @brief Mode Register address(R/W)\n - * @ref MR is used for S/W reset, ping block mode, PPPoE mode and etc. - * @details Each bit of @ref MR defined as follows. - * - * - * - *
7 6 5 4 3 2 1 0
RST Reserved WOL PB PPPoE Reserved FARP Reserved
- * - \ref MR_RST : Reset - * - \ref MR_WOL : Wake on LAN - * - \ref MR_PB : Ping block - * - \ref MR_PPPOE : PPPoE mode - * - \ref MR_FARP : Force ARP mode - */ -#define MR (_W5500_IO_BASE_ + (0x0000 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Gateway IP Register address(R/W) - * @details @ref GAR configures the default gateway address. - */ -#define GAR (_W5500_IO_BASE_ + (0x0001 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Subnet mask Register address(R/W) - * @details @ref SUBR configures the subnet mask address. - */ -#define SUBR (_W5500_IO_BASE_ + (0x0005 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Source MAC Register address(R/W) - * @details @ref SHAR configures the source hardware address. - */ -#define SHAR (_W5500_IO_BASE_ + (0x0009 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Source IP Register address(R/W) - * @details @ref SIPR configures the source IP address. - */ -#define SIPR (_W5500_IO_BASE_ + (0x000F << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Set Interrupt low level timer register address(R/W) - * @details @ref INTLEVEL configures the Interrupt Assert Time. - */ -#define INTLEVEL (_W5500_IO_BASE_ + (0x0013 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Interrupt Register(R/W) - * @details @ref IR indicates the interrupt status. Each bit of @ref IR will be still until the bit will be written to by the host. - * If @ref IR is not equal to x00 INTn PIN is asserted to low until it is x00\n\n - * Each bit of @ref IR defined as follows. - * - * - * - *
7 6 5 4 3 2 1 0
CONFLICT UNREACH PPPoE MP Reserved Reserved Reserved Reserved
- * - \ref IR_CONFLICT : IP conflict - * - \ref IR_UNREACH : Destination unreachable - * - \ref IR_PPPoE : PPPoE connection close - * - \ref IR_MP : Magic packet - */ -#define IR (_W5500_IO_BASE_ + (0x0015 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Interrupt mask register(R/W) - * @details @ref IMR is used to mask interrupts. Each bit of @ref IMR corresponds to each bit of @ref IR. - * When a bit of @ref IMR is and the corresponding bit of @ref IR is an interrupt will be issued. In other words, - * if a bit of @ref IMR is an interrupt will not be issued even if the corresponding bit of @ref IR is \n\n - * Each bit of @ref IMR defined as the following. - * - * - * - *
7 6 5 4 3 2 1 0
IM_IR7 IM_IR6 IM_IR5 IM_IR4 Reserved Reserved Reserved Reserved
- * - \ref IM_IR7 : IP Conflict Interrupt Mask - * - \ref IM_IR6 : Destination unreachable Interrupt Mask - * - \ref IM_IR5 : PPPoE Close Interrupt Mask - * - \ref IM_IR4 : Magic Packet Interrupt Mask - */ -#define IMR (_W5500_IO_BASE_ + (0x0016 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Socket Interrupt Register(R/W) - * @details @ref SIR indicates the interrupt status of Socket.\n - * Each bit of @ref SIR be still until @ref Sn_IR is cleared by the host.\n - * If @ref Sn_IR is not equal to x00 the n-th bit of @ref SIR is and INTn PIN is asserted until @ref SIR is x00 */ -#define SIR (_W5500_IO_BASE_ + (0x0017 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Socket Interrupt Mask Register(R/W) - * @details Each bit of @ref SIMR corresponds to each bit of @ref SIR. - * When a bit of @ref SIMR is and the corresponding bit of @ref SIR is Interrupt will be issued. - * In other words, if a bit of @ref SIMR is an interrupt will be not issued even if the corresponding bit of @ref SIR is - */ -#define SIMR (_W5500_IO_BASE_ + (0x0018 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Timeout register address( 1 is 100us )(R/W) - * @details @ref RTR configures the retransmission timeout period. The unit of timeout period is 100us and the default of @ref RTR is x07D0or 000 - * And so the default timeout period is 200ms(100us X 2000). During the time configured by @ref RTR, W5500 waits for the peer response - * to the packet that is transmitted by \ref Sn_CR (CONNECT, DISCON, CLOSE, SEND, SEND_MAC, SEND_KEEP command). - * If the peer does not respond within the @ref RTR time, W5500 retransmits the packet or issues timeout. - */ -#define RTR (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Retry count register(R/W) - * @details @ref RCR configures the number of time of retransmission. - * When retransmission occurs as many as ref RCR+1 Timeout interrupt is issued (@ref Sn_IR[TIMEOUT] = . - */ -#define RCR (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP LCP Request Timer register in PPPoE mode(R/W) - * @details @ref PTIMER configures the time for sending LCP echo request. The unit of time is 25ms. - */ -#define PTIMER (_W5500_IO_BASE_ + (0x001C << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP LCP Magic number register in PPPoE mode(R/W) - * @details @ref PMAGIC configures the 4bytes magic number to be used in LCP negotiation. - */ -#define PMAGIC (_W5500_IO_BASE_ + (0x001D << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP Destination MAC Register address(R/W) - * @details @ref PHAR configures the PPPoE server hardware address that is acquired during PPPoE connection process. - */ -#define PHAR (_W5500_IO_BASE_ + (0x001E << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP Session Identification Register(R/W) - * @details @ref PSID configures the PPPoE sever session ID acquired during PPPoE connection process. - */ -#define PSID (_W5500_IO_BASE_ + (0x0024 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PPP Maximum Segment Size(MSS) register(R/W) - * @details @ref PMRU configures the maximum receive unit of PPPoE. - */ -#define PMRU (_W5500_IO_BASE_ + (0x0026 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Unreachable IP register address in UDP mode(R) - * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number - * which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR indicates - * the destination IP address & port number respectively. - */ -#define UIPR (_W5500_IO_BASE_ + (0x0028 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief Unreachable Port register address in UDP mode(R) - * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number - * which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR - * indicates the destination IP address & port number respectively. - */ -#define UPORTR (_W5500_IO_BASE_ + (0x002C << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief PHY Status Register(R/W) - * @details @ref PHYCFGR configures PHY operation mode and resets PHY. In addition, @ref PHYCFGR indicates the status of PHY such as duplex, Speed, Link. - */ -#define PHYCFGR (_W5500_IO_BASE_ + (0x002E << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -// Reserved (_W5500_IO_BASE_ + (0x002F << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0030 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0031 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0032 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0033 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0034 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0035 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0036 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0037 << 8) + (WIZCHIP_CREG_BLOCK << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0038 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - -/** - * @ingroup Common_register_group - * @brief chip version register address(R) - * @details @ref VERSIONR always indicates the W5500 version as @b 0x04. - */ -#define VERSIONR (_W5500_IO_BASE_ + (0x0039 << 8) + (WIZCHIP_CREG_BLOCK << 3)) - - -//----------------------------- W5500 Socket Registers IOMAP ----------------------------- -/** - * @ingroup Socket_register_group - * @brief socket Mode register(R/W) - * @details @ref Sn_MR configures the option or protocol type of Socket n.\n\n - * Each bit of @ref Sn_MR defined as the following. - * - * - * - *
7 6 5 4 3 2 1 0
MULTI/MFEN BCASTB ND/MC/MMB UCASTB/MIP6B Protocol[3] Protocol[2] Protocol[1] Protocol[0]
- * - @ref Sn_MR_MULTI : Support UDP Multicasting - * - @ref Sn_MR_BCASTB : Broadcast block in UDP Multicasting - * - @ref Sn_MR_ND : No Delayed Ack(TCP) flag - * - @ref Sn_MR_MC : IGMP version used in UDP mulitcasting - * - @ref Sn_MR_MMB : Multicast Blocking in @ref Sn_MR_MACRAW mode - * - @ref Sn_MR_UCASTB : Unicast Block in UDP Multicating - * - @ref Sn_MR_MIP6B : IPv6 packet Blocking in @ref Sn_MR_MACRAW mode - * - Protocol - * - * - * - * - * - * - *
Protocol[3] Protocol[2] Protocol[1] Protocol[0] @b Meaning
0 0 0 0 Closed
0 0 0 1 TCP
0 0 1 0 UDP
0 1 0 0 MACRAW
- * - @ref Sn_MR_MACRAW : MAC LAYER RAW SOCK \n - * - @ref Sn_MR_UDP : UDP - * - @ref Sn_MR_TCP : TCP - * - @ref Sn_MR_CLOSE : Unused socket - * @note MACRAW mode should be only used in Socket 0. - */ -#define Sn_MR(N) (_W5500_IO_BASE_ + (0x0000 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Socket command register(R/W) - * @details This is used to set the command for Socket n such as OPEN, CLOSE, CONNECT, LISTEN, SEND, and RECEIVE.\n - * After W5500 accepts the command, the @ref Sn_CR register is automatically cleared to 0x00. - * Even though @ref Sn_CR is cleared to 0x00, the command is still being processed.\n - * To check whether the command is completed or not, please check the @ref Sn_IR or @ref Sn_SR. - * - @ref Sn_CR_OPEN : Initialize or open socket. - * - @ref Sn_CR_LISTEN : Wait connection request in TCP mode(Server mode) - * - @ref Sn_CR_CONNECT : Send connection request in TCP mode(Client mode) - * - @ref Sn_CR_DISCON : Send closing request in TCP mode. - * - @ref Sn_CR_CLOSE : Close socket. - * - @ref Sn_CR_SEND : Update TX buffer pointer and send data. - * - @ref Sn_CR_SEND_MAC : Send data with MAC address, so without ARP process. - * - @ref Sn_CR_SEND_KEEP : Send keep alive message. - * - @ref Sn_CR_RECV : Update RX buffer pointer and receive data. - */ -#define Sn_CR(N) (_W5500_IO_BASE_ + (0x0001 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Socket interrupt register(R) - * @details @ref Sn_IR indicates the status of Socket Interrupt such as establishment, termination, receiving data, timeout).\n - * When an interrupt occurs and the corresponding bit of @ref Sn_IMR is the corresponding bit of @ref Sn_IR becomes \n - * In order to clear the @ref Sn_IR bit, the host should write the bit to \n - * - * - * - *
7 6 5 4 3 2 1 0
Reserved Reserved Reserved SEND_OK TIMEOUT RECV DISCON CON
- * - \ref Sn_IR_SENDOK : SEND_OK Interrupt - * - \ref Sn_IR_TIMEOUT : TIMEOUT Interrupt - * - \ref Sn_IR_RECV : RECV Interrupt - * - \ref Sn_IR_DISCON : DISCON Interrupt - * - \ref Sn_IR_CON : CON Interrupt - */ -#define Sn_IR(N) (_W5500_IO_BASE_ + (0x0002 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Socket status register(R) - * @details @ref Sn_SR indicates the status of Socket n.\n - * The status of Socket n is changed by @ref Sn_CR or some special control packet as SYN, FIN packet in TCP. - * @par Normal status - * - @ref SOCK_CLOSED : Closed - * - @ref SOCK_INIT : Initiate state - * - @ref SOCK_LISTEN : Listen state - * - @ref SOCK_ESTABLISHED : Success to connect - * - @ref SOCK_CLOSE_WAIT : Closing state - * - @ref SOCK_UDP : UDP socket - * - @ref SOCK_MACRAW : MAC raw mode socket - *@par Temporary status during changing the status of Socket n. - * - @ref SOCK_SYNSENT : This indicates Socket n sent the connect-request packet (SYN packet) to a peer. - * - @ref SOCK_SYNRECV : It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer. - * - @ref SOCK_FIN_WAIT : Connection state - * - @ref SOCK_CLOSING : Closing state - * - @ref SOCK_TIME_WAIT : Closing state - * - @ref SOCK_LAST_ACK : Closing state - */ -#define Sn_SR(N) (_W5500_IO_BASE_ + (0x0003 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief source port register(R/W) - * @details @ref Sn_PORT configures the source port number of Socket n. - * It is valid when Socket n is used in TCP/UPD mode. It should be set before OPEN command is ordered. - */ -#define Sn_PORT(N) (_W5500_IO_BASE_ + (0x0004 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Peer MAC register address(R/W) - * @details @ref Sn_DHAR configures the destination hardware address of Socket n when using SEND_MAC command in UDP mode or - * it indicates that it is acquired in ARP-process by CONNECT/SEND command. - */ -#define Sn_DHAR(N) (_W5500_IO_BASE_ + (0x0006 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Peer IP register address(R/W) - * @details @ref Sn_DIPR configures or indicates the destination IP address of Socket n. It is valid when Socket n is used in TCP/UDP mode. - * In TCP client mode, it configures an IP address of �TCP serverbefore CONNECT command. - * In TCP server mode, it indicates an IP address of �TCP clientafter successfully establishing connection. - * In UDP mode, it configures an IP address of peer to be received the UDP packet by SEND or SEND_MAC command. - */ -#define Sn_DIPR(N) (_W5500_IO_BASE_ + (0x000C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Peer port register address(R/W) - * @details @ref Sn_DPORT configures or indicates the destination port number of Socket n. It is valid when Socket n is used in TCP/UDP mode. - * In �TCP clientmode, it configures the listen port number of �TCP serverbefore CONNECT command. - * In �TCP Servermode, it indicates the port number of TCP client after successfully establishing connection. - * In UDP mode, it configures the port number of peer to be transmitted the UDP packet by SEND/SEND_MAC command. - */ -#define Sn_DPORT(N) (_W5500_IO_BASE_ + (0x0010 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Maximum Segment Size(Sn_MSSR0) register address(R/W) - * @details @ref Sn_MSSR configures or indicates the MTU(Maximum Transfer Unit) of Socket n. - */ -#define Sn_MSSR(N) (_W5500_IO_BASE_ + (0x0012 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -// Reserved (_W5500_IO_BASE_ + (0x0014 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief IP Type of Service(TOS) Register(R/W) - * @details @ref Sn_TOS configures the TOS(Type Of Service field in IP Header) of Socket n. - * It is set before OPEN command. - */ -#define Sn_TOS(N) (_W5500_IO_BASE_ + (0x0015 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -/** - * @ingroup Socket_register_group - * @brief IP Time to live(TTL) Register(R/W) - * @details @ref Sn_TTL configures the TTL(Time To Live field in IP header) of Socket n. - * It is set before OPEN command. - */ -#define Sn_TTL(N) (_W5500_IO_BASE_ + (0x0016 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0017 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0018 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001A << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) -// Reserved (_W5500_IO_BASE_ + (0x001D << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Receive memory size register(R/W) - * @details @ref Sn_RXBUF_SIZE configures the RX buffer block size of Socket n. - * Socket n RX Buffer Block size can be configured with 1,2,4,8, and 16 Kbytes. - * If a different size is configured, the data cannot be normally received from a peer. - * Although Socket n RX Buffer Block size is initially configured to 2Kbytes, - * user can re-configure its size using @ref Sn_RXBUF_SIZE. The total sum of @ref Sn_RXBUF_SIZE can not be exceed 16Kbytes. - * When exceeded, the data reception error is occurred. - */ -#define Sn_RXBUF_SIZE(N) (_W5500_IO_BASE_ + (0x001E << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory size register(R/W) - * @details @ref Sn_TXBUF_SIZE configures the TX buffer block size of Socket n. Socket n TX Buffer Block size can be configured with 1,2,4,8, and 16 Kbytes. - * If a different size is configured, the data can�t be normally transmitted to a peer. - * Although Socket n TX Buffer Block size is initially configured to 2Kbytes, - * user can be re-configure its size using @ref Sn_TXBUF_SIZE. The total sum of @ref Sn_TXBUF_SIZE can not be exceed 16Kbytes. - * When exceeded, the data transmission error is occurred. - */ -#define Sn_TXBUF_SIZE(N) (_W5500_IO_BASE_ + (0x001F << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Transmit free memory size register(R) - * @details @ref Sn_TX_FSR indicates the free size of Socket n TX Buffer Block. It is initialized to the configured size by @ref Sn_TXBUF_SIZE. - * Data bigger than @ref Sn_TX_FSR should not be saved in the Socket n TX Buffer because the bigger data overwrites the previous saved data not yet sent. - * Therefore, check before saving the data to the Socket n TX Buffer, and if data is equal or smaller than its checked size, - * transmit the data with SEND/SEND_MAC command after saving the data in Socket n TX buffer. But, if data is bigger than its checked size, - * transmit the data after dividing into the checked size and saving in the Socket n TX buffer. - */ -#define Sn_TX_FSR(N) (_W5500_IO_BASE_ + (0x0020 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory read pointer register address(R) - * @details @ref Sn_TX_RD is initialized by OPEN command. However, if Sn_MR(P[3:0]) is TCP mode(001, it is re-initialized while connecting with TCP. - * After its initialization, it is auto-increased by SEND command. - * SEND command transmits the saved data from the current @ref Sn_TX_RD to the @ref Sn_TX_WR in the Socket n TX Buffer. - * After transmitting the saved data, the SEND command increases the @ref Sn_TX_RD as same as the @ref Sn_TX_WR. - * If its increment value exceeds the maximum value 0xFFFF, (greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value. - */ -#define Sn_TX_RD(N) (_W5500_IO_BASE_ + (0x0022 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Transmit memory write pointer register address(R/W) - * @details @ref Sn_TX_WR is initialized by OPEN command. However, if Sn_MR(P[3:0]) is TCP mode(001, it is re-initialized while connecting with TCP.\n - * It should be read or be updated like as follows.\n - * 1. Read the starting address for saving the transmitting data.\n - * 2. Save the transmitting data from the starting address of Socket n TX buffer.\n - * 3. After saving the transmitting data, update @ref Sn_TX_WR to the increased value as many as transmitting data size. - * If the increment value exceeds the maximum value 0xFFFF(greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value.\n - * 4. Transmit the saved data in Socket n TX Buffer by using SEND/SEND command - */ -#define Sn_TX_WR(N) (_W5500_IO_BASE_ + (0x0024 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Received data size register(R) - * @details @ref Sn_RX_RSR indicates the data size received and saved in Socket n RX Buffer. - * @ref Sn_RX_RSR does not exceed the @ref Sn_RXBUF_SIZE and is calculated as the difference between - * �Socket n RX Write Pointer (@ref Sn_RX_WR)and �Socket n RX Read Pointer (@ref Sn_RX_RD) - */ -#define Sn_RX_RSR(N) (_W5500_IO_BASE_ + (0x0026 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Read point of Receive memory(R/W) - * @details @ref Sn_RX_RD is initialized by OPEN command. Make sure to be read or updated as follows.\n - * 1. Read the starting save address of the received data.\n - * 2. Read data from the starting address of Socket n RX Buffer.\n - * 3. After reading the received data, Update @ref Sn_RX_RD to the increased value as many as the reading size. - * If the increment value exceeds the maximum value 0xFFFF, that is, is greater than 0x10000 and the carry bit occurs, - * update with the lower 16bits value ignored the carry bit.\n - * 4. Order RECV command is for notifying the updated @ref Sn_RX_RD to W5500. - */ -#define Sn_RX_RD(N) (_W5500_IO_BASE_ + (0x0028 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Write point of Receive memory(R) - * @details @ref Sn_RX_WR is initialized by OPEN command and it is auto-increased by the data reception. - * If the increased value exceeds the maximum value 0xFFFF, (greater than 0x10000 and the carry bit occurs), - * then the carry bit is ignored and will automatically update with the lower 16bits value. - */ -#define Sn_RX_WR(N) (_W5500_IO_BASE_ + (0x002A << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief socket interrupt mask register(R) - * @details @ref Sn_IMR masks the interrupt of Socket n. - * Each bit corresponds to each bit of @ref Sn_IR. When a Socket n Interrupt is occurred and the corresponding bit of @ref Sn_IMR is - * the corresponding bit of @ref Sn_IR becomes When both the corresponding bit of @ref Sn_IMR and @ref Sn_IR are and the n-th bit of @ref IR is - * Host is interrupted by asserted INTn PIN to low. - */ -#define Sn_IMR(N) (_W5500_IO_BASE_ + (0x002C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Fragment field value in IP header register(R/W) - * @details @ref Sn_FRAG configures the FRAG(Fragment field in IP header). - */ -#define Sn_FRAG(N) (_W5500_IO_BASE_ + (0x002D << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -/** - * @ingroup Socket_register_group - * @brief Keep Alive Timer register(R/W) - * @details @ref Sn_KPALVTR configures the transmitting timer of �KEEP ALIVE(KA)packet of SOCKETn. It is valid only in TCP mode, - * and ignored in other modes. The time unit is 5s. - * KA packet is transmittable after @ref Sn_SR is changed to SOCK_ESTABLISHED and after the data is transmitted or received to/from a peer at least once. - * In case of '@ref Sn_KPALVTR > 0', W5500 automatically transmits KA packet after time-period for checking the TCP connection (Auto-keepalive-process). - * In case of '@ref Sn_KPALVTR = 0', Auto-keep-alive-process will not operate, - * and KA packet can be transmitted by SEND_KEEP command by the host (Manual-keep-alive-process). - * Manual-keep-alive-process is ignored in case of '@ref Sn_KPALVTR > 0'. - */ -#define Sn_KPALVTR(N) (_W5500_IO_BASE_ + (0x002F << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - -//#define Sn_TSR(N) (_W5500_IO_BASE_ + (0x0030 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) - - -//----------------------------- W5500 Register values ----------------------------- - -/* MODE register values */ -/** - * @brief Reset - * @details If this bit is All internal registers will be initialized. It will be automatically cleared as after S/W reset. - */ -#define MR_RST 0x80 - -/** - * @brief Wake on LAN - * @details 0 : Disable WOL mode\n - * 1 : Enable WOL mode\n - * If WOL mode is enabled and the received magic packet over UDP has been normally processed, the Interrupt PIN (INTn) asserts to low. - * When using WOL mode, the UDP Socket should be opened with any source port number. (Refer to Socket n Mode Register (@ref Sn_MR) for opening Socket.) - * @note The magic packet over UDP supported by W5500 consists of 6 bytes synchronization stream (xFFFFFFFFFFFF and - * 16 times Target MAC address stream in UDP payload. The options such like password are ignored. You can use any UDP source port number for WOL mode. - */ -#define MR_WOL 0x20 - -/** - * @brief Ping block - * @details 0 : Disable Ping block\n - * 1 : Enable Ping block\n - * If the bit is it blocks the response to a ping request. - */ -#define MR_PB 0x10 - -/** - * @brief Enable PPPoE - * @details 0 : DisablePPPoE mode\n - * 1 : EnablePPPoE mode\n - * If you use ADSL, this bit should be - */ -#define MR_PPPOE 0x08 - -/** - * @brief Enable UDP_FORCE_ARP CHECHK - * @details 0 : Disable Force ARP mode\n - * 1 : Enable Force ARP mode\n - * In Force ARP mode, It forces on sending ARP Request whenever data is sent. - */ -#define MR_FARP 0x02 - -/* IR register values */ -/** - * @brief Check IP conflict. - * @details Bit is set as when own source IP address is same with the sender IP address in the received ARP request. - */ -#define IR_CONFLICT 0x80 - -/** - * @brief Get the destination unreachable message in UDP sending. - * @details When receiving the ICMP (Destination port unreachable) packet, this bit is set as - * When this bit is Destination Information such as IP address and Port number may be checked with the corresponding @ref UIPR & @ref UPORTR. - */ -#define IR_UNREACH 0x40 - -/** - * @brief Get the PPPoE close message. - * @details When PPPoE is disconnected during PPPoE mode, this bit is set. - */ -#define IR_PPPoE 0x20 - -/** - * @brief Get the magic packet interrupt. - * @details When WOL mode is enabled and receives the magic packet over UDP, this bit is set. - */ -#define IR_MP 0x10 - - -/* PHYCFGR register value */ -#define PHYCFGR_RST ~(1<<7) //< For PHY reset, must operate AND mask. -#define PHYCFGR_OPMD (1<<6) // Configre PHY with OPMDC value -#define PHYCFGR_OPMDC_ALLA (7<<3) -#define PHYCFGR_OPMDC_PDOWN (6<<3) -#define PHYCFGR_OPMDC_NA (5<<3) -#define PHYCFGR_OPMDC_100FA (4<<3) -#define PHYCFGR_OPMDC_100F (3<<3) -#define PHYCFGR_OPMDC_100H (2<<3) -#define PHYCFGR_OPMDC_10F (1<<3) -#define PHYCFGR_OPMDC_10H (0<<3) -#define PHYCFGR_DPX_FULL (1<<2) -#define PHYCFGR_DPX_HALF (0<<2) -#define PHYCFGR_SPD_100 (1<<1) -#define PHYCFGR_SPD_10 (0<<1) -#define PHYCFGR_LNK_ON (1<<0) -#define PHYCFGR_LNK_OFF (0<<0) - -/* IMR register values */ -/** - * @brief IP Conflict Interrupt Mask. - * @details 0: Disable IP Conflict Interrupt\n - * 1: Enable IP Conflict Interrupt - */ -#define IM_IR7 0x80 - -/** - * @brief Destination unreachable Interrupt Mask. - * @details 0: Disable Destination unreachable Interrupt\n - * 1: Enable Destination unreachable Interrupt - */ -#define IM_IR6 0x40 - -/** - * @brief PPPoE Close Interrupt Mask. - * @details 0: Disable PPPoE Close Interrupt\n - * 1: Enable PPPoE Close Interrupt - */ -#define IM_IR5 0x20 - -/** - * @brief Magic Packet Interrupt Mask. - * @details 0: Disable Magic Packet Interrupt\n - * 1: Enable Magic Packet Interrupt - */ -#define IM_IR4 0x10 - -/* Sn_MR Default values */ -/** - * @brief Support UDP Multicasting - * @details 0 : disable Multicasting\n - * 1 : enable Multicasting\n - * This bit is applied only during UDP mode(P[3:0] = 010.\n - * To use multicasting, @ref Sn_DIPR & @ref Sn_DPORT should be respectively configured with the multicast group IP address & port number - * before Socket n is opened by OPEN command of @ref Sn_CR. - */ -#define Sn_MR_MULTI 0x80 - -/** - * @brief Broadcast block in UDP Multicasting. - * @details 0 : disable Broadcast Blocking\n - * 1 : enable Broadcast Blocking\n - * This bit blocks to receive broadcasting packet during UDP mode(P[3:0] = 010.\m - * In addition, This bit does when MACRAW mode(P[3:0] = 100 - */ -#define Sn_MR_BCASTB 0x40 - -/** - * @brief No Delayed Ack(TCP), Multicast flag - * @details 0 : Disable No Delayed ACK option\n - * 1 : Enable No Delayed ACK option\n - * This bit is applied only during TCP mode (P[3:0] = 001.\n - * When this bit is It sends the ACK packet without delay as soon as a Data packet is received from a peer.\n - * When this bit is It sends the ACK packet after waiting for the timeout time configured by @ref RTR. - */ -#define Sn_MR_ND 0x20 - -/** - * @brief Unicast Block in UDP Multicasting - * @details 0 : disable Unicast Blocking\n - * 1 : enable Unicast Blocking\n - * This bit blocks receiving the unicast packet during UDP mode(P[3:0] = 010 and MULTI = - */ -#define Sn_MR_UCASTB 0x10 - -/** - * @brief MAC LAYER RAW SOCK - * @details This configures the protocol mode of Socket n. - * @note MACRAW mode should be only used in Socket 0. - */ -#define Sn_MR_MACRAW 0x04 - -//#define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */ - -/** - * @brief UDP - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_UDP 0x02 - -/** - * @brief TCP - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_TCP 0x01 - -/** - * @brief Unused socket - * @details This configures the protocol mode of Socket n. - */ -#define Sn_MR_CLOSE 0x00 - -/* Sn_MR values used with Sn_MR_MACRAW */ -/** - * @brief MAC filter enable in @ref Sn_MR_MACRAW mode - * @details 0 : disable MAC Filtering\n - * 1 : enable MAC Filtering\n - * This bit is applied only during MACRAW mode(P[3:0] = 100.\n - * When set as W5500 can only receive broadcasting packet or packet sent to itself. - * When this bit is W5500 can receive all packets on Ethernet. - * If user wants to implement Hybrid TCP/IP stack, - * it is recommended that this bit is set as for reducing host overhead to process the all received packets. - */ -#define Sn_MR_MFEN Sn_MR_MULTI - -/** - * @brief Multicast Blocking in @ref Sn_MR_MACRAW mode - * @details 0 : using IGMP version 2\n - * 1 : using IGMP version 1\n - * This bit is applied only during UDP mode(P[3:0] = 010 and MULTI = - * It configures the version for IGMP messages (Join/Leave/Report). - */ -#define Sn_MR_MMB Sn_MR_ND - -/** - * @brief IPv6 packet Blocking in @ref Sn_MR_MACRAW mode - * @details 0 : disable IPv6 Blocking\n - * 1 : enable IPv6 Blocking\n - * This bit is applied only during MACRAW mode (P[3:0] = 100. It blocks to receiving the IPv6 packet. - */ -#define Sn_MR_MIP6B Sn_MR_UCASTB - -/* Sn_MR value used with Sn_MR_UDP & Sn_MR_MULTI */ -/** - * @brief IGMP version used in UDP mulitcasting - * @details 0 : disable Multicast Blocking\n - * 1 : enable Multicast Blocking\n - * This bit is applied only when MACRAW mode(P[3:0] = 100. It blocks to receive the packet with multicast MAC address. - */ -#define Sn_MR_MC Sn_MR_ND - -/* Sn_MR alternate values */ -/** - * @brief For Berkeley Socket API - */ -#define SOCK_STREAM Sn_MR_TCP - -/** - * @brief For Berkeley Socket API - */ -#define SOCK_DGRAM Sn_MR_UDP - - -/* Sn_CR values */ -/** - * @brief Initialize or open socket - * @details Socket n is initialized and opened according to the protocol selected in Sn_MR(P3:P0). - * The table below shows the value of @ref Sn_SR corresponding to @ref Sn_MR.\n - * - * - * - * - * - * - *
\b Sn_MR (P[3:0]) \b Sn_SR
Sn_MR_CLOSE (000
Sn_MR_TCP (001 SOCK_INIT (0x13)
Sn_MR_UDP (010 SOCK_UDP (0x22)
S0_MR_MACRAW (100 SOCK_MACRAW (0x02)
- */ -#define Sn_CR_OPEN 0x01 - -/** - * @brief Wait connection request in TCP mode(Server mode) - * @details This is valid only in TCP mode (Sn_MR(P3:P0) = Sn_MR_TCP). - * In this mode, Socket n operates as a �TCP serverand waits for connection-request (SYN packet) from any �TCP client - * The @ref Sn_SR changes the state from SOCK_INIT to SOCKET_LISTEN. - * When a �TCP clientconnection request is successfully established, - * the @ref Sn_SR changes from SOCK_LISTEN to SOCK_ESTABLISHED and the Sn_IR(0) becomes - * But when a �TCP clientconnection request is failed, Sn_IR(3) becomes and the status of @ref Sn_SR changes to SOCK_CLOSED. - */ -#define Sn_CR_LISTEN 0x02 - -/** - * @brief Send connection request in TCP mode(Client mode) - * @details To connect, a connect-request (SYN packet) is sent to b>TCP serverconfigured by @ref Sn_DIPR & Sn_DPORT(destination address & port). - * If the connect-request is successful, the @ref Sn_SR is changed to @ref SOCK_ESTABLISHED and the Sn_IR(0) becomes \n\n - * The connect-request fails in the following three cases.\n - * 1. When a @b ARPTO occurs (@ref Sn_IR[3] = ) because destination hardware address is not acquired through the ARP-process.\n - * 2. When a @b SYN/ACK packet is not received and @b TCPTO (Sn_IR(3) = )\n - * 3. When a @b RST packet is received instead of a @b SYN/ACK packet. In these cases, @ref Sn_SR is changed to @ref SOCK_CLOSED. - * @note This is valid only in TCP mode and operates when Socket n acts as b>TCP client - */ -#define Sn_CR_CONNECT 0x04 - -/** - * @brief Send closing request in TCP mode - * @details Regardless of b>TCP serveror b>TCP client the DISCON command processes the disconnect-process (b>Active closeor b>Passive close.\n - * @par Active close - * it transmits disconnect-request(FIN packet) to the connected peer\n - * @par Passive close - * When FIN packet is received from peer, a FIN packet is replied back to the peer.\n - * @details When the disconnect-process is successful (that is, FIN/ACK packet is received successfully), @ref Sn_SR is changed to @ref SOCK_CLOSED.\n - * Otherwise, TCPTO occurs (Sn_IR(3)=)= and then @ref Sn_SR is changed to @ref SOCK_CLOSED. - * @note Valid only in TCP mode. - */ -#define Sn_CR_DISCON 0x08 - -/** - * @brief Close socket - * @details Sn_SR is changed to @ref SOCK_CLOSED. - */ -#define Sn_CR_CLOSE 0x10 - -/** - * @brief Update TX buffer pointer and send data - * @details SEND transmits all the data in the Socket n TX buffer.\n - * For more details, please refer to Socket n TX Free Size Register (@ref Sn_TX_FSR), Socket n, - * TX Write Pointer Register(@ref Sn_TX_WR), and Socket n TX Read Pointer Register(@ref Sn_TX_RD). - */ -#define Sn_CR_SEND 0x20 - -/** - * @brief Send data with MAC address, so without ARP process - * @details The basic operation is same as SEND.\n - * Normally SEND transmits data after destination hardware address is acquired by the automatic ARP-process(Address Resolution Protocol).\n - * But SEND_MAC transmits data without the automatic ARP-process.\n - * In this case, the destination hardware address is acquired from @ref Sn_DHAR configured by host, instead of APR-process. - * @note Valid only in UDP mode. - */ -#define Sn_CR_SEND_MAC 0x21 - -/** - * @brief Send keep alive message - * @details It checks the connection status by sending 1byte keep-alive packet.\n - * If the peer can not respond to the keep-alive packet during timeout time, the connection is terminated and the timeout interrupt will occur. - * @note Valid only in TCP mode. - */ -#define Sn_CR_SEND_KEEP 0x22 - -/** - * @brief Update RX buffer pointer and receive data - * @details RECV completes the processing of the received data in Socket n RX Buffer by using a RX read pointer register (@ref Sn_RX_RD).\n - * For more details, refer to Socket n RX Received Size Register (@ref Sn_RX_RSR), Socket n RX Write Pointer Register (@ref Sn_RX_WR), - * and Socket n RX Read Pointer Register (@ref Sn_RX_RD). - */ -#define Sn_CR_RECV 0x40 - -/* Sn_IR values */ -/** - * @brief SEND_OK Interrupt - * @details This is issued when SEND command is completed. - */ -#define Sn_IR_SENDOK 0x10 - -/** - * @brief TIMEOUT Interrupt - * @details This is issued when ARPTO or TCPTO occurs. - */ -#define Sn_IR_TIMEOUT 0x08 - -/** - * @brief RECV Interrupt - * @details This is issued whenever data is received from a peer. - */ -#define Sn_IR_RECV 0x04 - -/** - * @brief DISCON Interrupt - * @details This is issued when FIN or FIN/ACK packet is received from a peer. - */ -#define Sn_IR_DISCON 0x02 - -/** - * @brief CON Interrupt - * @details This is issued one time when the connection with peer is successful and then @ref Sn_SR is changed to @ref SOCK_ESTABLISHED. - */ -#define Sn_IR_CON 0x01 - -/* Sn_SR values */ -/** - * @brief Closed - * @details This indicates that Socket n is released.\N - * When DICON, CLOSE command is ordered, or when a timeout occurs, it is changed to @ref SOCK_CLOSED regardless of previous status. - */ -#define SOCK_CLOSED 0x00 - -/** - * @brief Initiate state - * @details This indicates Socket n is opened with TCP mode.\N - * It is changed to @ref SOCK_INIT when Sn_MR(P[3:0]) = 001and OPEN command is ordered.\N - * After @ref SOCK_INIT, user can use LISTEN /CONNECT command. - */ -#define SOCK_INIT 0x13 - -/** - * @brief Listen state - * @details This indicates Socket n is operating as b>TCP servermode and waiting for connection-request (SYN packet) from a peer (b>TCP client.\n - * It will change to @ref SOCK_ESTALBLISHED when the connection-request is successfully accepted.\n - * Otherwise it will change to @ref SOCK_CLOSED after TCPTO occurred (Sn_IR(TIMEOUT) = . - */ -#define SOCK_LISTEN 0x14 - -/** - * @brief Connection state - * @details This indicates Socket n sent the connect-request packet (SYN packet) to a peer.\n - * It is temporarily shown when @ref Sn_SR is changed from @ref SOCK_INIT to @ref SOCK_ESTABLISHED by CONNECT command.\n - * If connect-accept(SYN/ACK packet) is received from the peer at SOCK_SYNSENT, it changes to @ref SOCK_ESTABLISHED.\n - * Otherwise, it changes to @ref SOCK_CLOSED after TCPTO (@ref Sn_IR[TIMEOUT] = is occurred. - */ -#define SOCK_SYNSENT 0x15 - -/** - * @brief Connection state - * @details It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer.\n - * If socket n sends the response (SYN/ACK packet) to the peer successfully, it changes to @ref SOCK_ESTABLISHED. \n - * If not, it changes to @ref SOCK_CLOSED after timeout occurs (@ref Sn_IR[TIMEOUT] = . - */ -#define SOCK_SYNRECV 0x16 - -/** - * @brief Success to connect - * @details This indicates the status of the connection of Socket n.\n - * It changes to @ref SOCK_ESTABLISHED when the b>TCP SERVERprocessed the SYN packet from the b>TCP CLIENTduring @ref SOCK_LISTEN, or - * when the CONNECT command is successful.\n - * During @ref SOCK_ESTABLISHED, DATA packet can be transferred using SEND or RECV command. - */ -#define SOCK_ESTABLISHED 0x17 - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_FIN_WAIT 0x18 - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_CLOSING 0x1A - -/** - * @brief Closing state - * @details These indicate Socket n is closing.\n - * These are shown in disconnect-process such as active-close and passive-close.\n - * When Disconnect-process is successfully completed, or when timeout occurs, these change to @ref SOCK_CLOSED. - */ -#define SOCK_TIME_WAIT 0x1B - -/** - * @brief Closing state - * @details This indicates Socket n received the disconnect-request (FIN packet) from the connected peer.\n - * This is half-closing status, and data can be transferred.\n - * For full-closing, DISCON command is used. But For just-closing, CLOSE command is used. - */ -#define SOCK_CLOSE_WAIT 0x1C - -/** - * @brief Closing state - * @details This indicates Socket n is waiting for the response (FIN/ACK packet) to the disconnect-request (FIN packet) by passive-close.\n - * It changes to @ref SOCK_CLOSED when Socket n received the response successfully, or when timeout occurs (@ref Sn_IR[TIMEOUT] = . - */ -#define SOCK_LAST_ACK 0x1D - -/** - * @brief UDP socket - * @details This indicates Socket n is opened in UDP mode(Sn_MR(P[3:0]) = 010.\n - * It changes to SOCK_UPD when Sn_MR(P[3:0]) = 010 and OPEN command is ordered.\n - * Unlike TCP mode, data can be transfered without the connection-process. - */ -#define SOCK_UDP 0x22 - -//#define SOCK_IPRAW 0x32 /**< IP raw mode socket */ - -/** - * @brief MAC raw mode socket - * @details This indicates Socket 0 is opened in MACRAW mode (S0_MR(P[3:0]) = 100and is valid only in Socket 0.\n - * It changes to SOCK_MACRAW when S0_MR(P[3:0] = 100and OPEN command is ordered.\n - * Like UDP mode socket, MACRAW mode Socket 0 can transfer a MAC packet (Ethernet frame) without the connection-process. - */ -#define SOCK_MACRAW 0x42 - -//#define SOCK_PPPOE 0x5F - -/* IP PROTOCOL */ -#define IPPROTO_IP 0 //< Dummy for IP -#define IPPROTO_ICMP 1 //< Control message protocol -#define IPPROTO_IGMP 2 //< Internet group management protocol -#define IPPROTO_GGP 3 //< Gateway^2 (deprecated) -#define IPPROTO_TCP 6 //< TCP -#define IPPROTO_PUP 12 //< PUP -#define IPPROTO_UDP 17 //< UDP -#define IPPROTO_IDP 22 //< XNS idp -#define IPPROTO_ND 77 //< UNOFFICIAL net disk protocol -#define IPPROTO_RAW 255 //< Raw IP packet - - -/** - * @brief Enter a critical section - * - * @details It is provided to protect your shared code which are executed without distribution. \n \n - * - * In non-OS environment, It can be just implemented by disabling whole interrupt.\n - * In OS environment, You can replace it to critical section api supported by OS. - * - * \sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() - * \sa WIZCHIP_CRITICAL_EXIT() - */ -#define WIZCHIP_CRITICAL_ENTER() WIZCHIP.CRIS._enter() - -/** - * @brief Exit a critical section - * - * @details It is provided to protect your shared code which are executed without distribution. \n\n - * - * In non-OS environment, It can be just implemented by disabling whole interrupt. \n - * In OS environment, You can replace it to critical section api supported by OS. - * - * @sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() - * @sa WIZCHIP_CRITICAL_ENTER() - */ -#ifdef _exit -#undef _exit -#endif -#define WIZCHIP_CRITICAL_EXIT() WIZCHIP.CRIS._exit() - - - -//////////////////////// -// Basic I/O Function // -//////////////////////// - -/** - * @ingroup Basic_IO_function - * @brief It reads 1 byte value from a register. - * @param AddrSel Register address - * @return The value of register - */ -uint8_t WIZCHIP_READ (uint32_t AddrSel); - -/** - * @ingroup Basic_IO_function - * @brief It writes 1 byte value to a register. - * @param AddrSel Register address - * @param wb Write data - * @return void - */ -void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb ); - -/** - * @ingroup Basic_IO_function - * @brief It reads sequence data from registers. - * @param AddrSel Register address - * @param pBuf Pointer buffer to read data - * @param len Data length - */ -void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It writes sequence data to registers. - * @param AddrSel Register address - * @param pBuf Pointer buffer to write data - * @param len Data length - */ -void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len); - -///////////////////////////////// -// Common Register I/O function // -///////////////////////////////// -/** - * @ingroup Common_register_access_function - * @brief Set Mode Register - * @param (uint8_t)mr The value to be set. - * @sa getMR() - */ -#define setMR(mr) \ - WIZCHIP_WRITE(MR,mr) - - -/** - * @ingroup Common_register_access_function - * @brief Get Mode Register - * @return uint8_t. The value of Mode register. - * @sa setMR() - */ -#define getMR() \ - WIZCHIP_READ(MR) - -/** - * @ingroup Common_register_access_function - * @brief Set gateway IP address - * @param (uint8_t*)gar Pointer variable to set gateway IP address. It should be allocated 4 bytes. - * @sa getGAR() - */ -#define setGAR(gar) \ - WIZCHIP_WRITE_BUF(GAR,gar,4) - -/** - * @ingroup Common_register_access_function - * @brief Get gateway IP address - * @param (uint8_t*)gar Pointer variable to get gateway IP address. It should be allocated 4 bytes. - * @sa setGAR() - */ -#define getGAR(gar) \ - WIZCHIP_READ_BUF(GAR,gar,4) - -/** - * @ingroup Common_register_access_function - * @brief Set subnet mask address - * @param (uint8_t*)subr Pointer variable to set subnet mask address. It should be allocated 4 bytes. - * @sa getSUBR() - */ -#define setSUBR(subr) \ - WIZCHIP_WRITE_BUF(SUBR, subr,4) - - -/** - * @ingroup Common_register_access_function - * @brief Get subnet mask address - * @param (uint8_t*)subr Pointer variable to get subnet mask address. It should be allocated 4 bytes. - * @sa setSUBR() - */ -#define getSUBR(subr) \ - WIZCHIP_READ_BUF(SUBR, subr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Set local MAC address - * @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. - * @sa getSHAR() - */ -#define setSHAR(shar) \ - WIZCHIP_WRITE_BUF(SHAR, shar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Get local MAC address - * @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. - * @sa setSHAR() - */ -#define getSHAR(shar) \ - WIZCHIP_READ_BUF(SHAR, shar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Set local IP address - * @param (uint8_t*)sipr Pointer variable to set local IP address. It should be allocated 4 bytes. - * @sa getSIPR() - */ -#define setSIPR(sipr) \ - WIZCHIP_WRITE_BUF(SIPR, sipr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Get local IP address - * @param (uint8_t*)sipr Pointer variable to get local IP address. It should be allocated 4 bytes. - * @sa setSIPR() - */ -#define getSIPR(sipr) \ - WIZCHIP_READ_BUF(SIPR, sipr, 4) - -/** - * @ingroup Common_register_access_function - * @brief Set INTLEVEL register - * @param (uint16_t)intlevel Value to set @ref INTLEVEL register. - * @sa getINTLEVEL() - */ -#define setINTLEVEL(intlevel) {\ - WIZCHIP_WRITE(INTLEVEL, (uint8_t)(intlevel >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(INTLEVEL,1), (uint8_t) intlevel); \ - } - - -/** - * @ingroup Common_register_access_function - * @brief Get INTLEVEL register - * @return uint16_t. Value of @ref INTLEVEL register. - * @sa setINTLEVEL() - */ -#define getINTLEVEL() \ - ((WIZCHIP_READ(INTLEVEL) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(INTLEVEL,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref IR register - * @param (uint8_t)ir Value to set @ref IR register. - * @sa getIR() - */ -#define setIR(ir) \ - WIZCHIP_WRITE(IR, (ir & 0xF0)) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IR register - * @return uint8_t. Value of @ref IR register. - * @sa setIR() - */ -#define getIR() \ - (WIZCHIP_READ(IR) & 0xF0) -/** - * @ingroup Common_register_access_function - * @brief Set @ref IMR register - * @param (uint8_t)imr Value to set @ref IMR register. - * @sa getIMR() - */ -#define setIMR(imr) \ - WIZCHIP_WRITE(IMR, imr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IMR register - * @return uint8_t. Value of @ref IMR register. - * @sa setIMR() - */ -#define getIMR() \ - WIZCHIP_READ(IMR) - - -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIR register - * @param (uint8_t)sir Value to set @ref SIR register. - * @sa getSIR() - */ -#define setSIR(sir) \ - WIZCHIP_WRITE(SIR, sir) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIR register - * @return uint8_t. Value of @ref SIR register. - * @sa setSIR() - */ -#define getSIR() \ - WIZCHIP_READ(SIR) -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIMR register - * @param (uint8_t)simr Value to set @ref SIMR register. - * @sa getSIMR() - */ -#define setSIMR(simr) \ - WIZCHIP_WRITE(SIMR, simr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIMR register - * @return uint8_t. Value of @ref SIMR register. - * @sa setSIMR() - */ -#define getSIMR() \ - WIZCHIP_READ(SIMR) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RTR register - * @param (uint16_t)rtr Value to set @ref RTR register. - * @sa getRTR() - */ -#define setRTR(rtr) {\ - WIZCHIP_WRITE(RTR, (uint8_t)(rtr >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(RTR,1), (uint8_t) rtr); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RTR register - * @return uint16_t. Value of @ref RTR register. - * @sa setRTR() - */ -#define getRTR() \ - ((WIZCHIP_READ(RTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(RTR,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RCR register - * @param (uint8_t)rcr Value to set @ref RCR register. - * @sa getRCR() - */ -#define setRCR(rcr) \ - WIZCHIP_WRITE(RCR, rcr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RCR register - * @return uint8_t. Value of @ref RCR register. - * @sa setRCR() - */ -#define getRCR() \ - WIZCHIP_READ(RCR) - -//================================================== test done =========================================================== - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PTIMER register - * @param (uint8_t)ptimer Value to set @ref PTIMER register. - * @sa getPTIMER() - */ -#define setPTIMER(ptimer) \ - WIZCHIP_WRITE(PTIMER, ptimer) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PTIMER register - * @return uint8_t. Value of @ref PTIMER register. - * @sa setPTIMER() - */ -#define getPTIMER() \ - WIZCHIP_READ(PTIMER) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PMAGIC register - * @param (uint8_t)pmagic Value to set @ref PMAGIC register. - * @sa getPMAGIC() - */ -#define setPMAGIC(pmagic) \ - WIZCHIP_WRITE(PMAGIC, pmagic) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PMAGIC register - * @return uint8_t. Value of @ref PMAGIC register. - * @sa setPMAGIC() - */ -#define getPMAGIC() \ - WIZCHIP_READ(PMAGIC) - -/** - * @ingroup Common_register_access_function - * @brief Set PHAR address - * @param (uint8_t*)phar Pointer variable to set PPP destination MAC register address. It should be allocated 6 bytes. - * @sa getPHAR() - */ -#define setPHAR(phar) \ - WIZCHIP_WRITE_BUF(PHAR, phar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Get local IP address - * @param (uint8_t*)phar Pointer variable to PPP destination MAC register address. It should be allocated 6 bytes. - * @sa setPHAR() - */ -#define getPHAR(phar) \ - WIZCHIP_READ_BUF(PHAR, phar, 6) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PSID register - * @param (uint16_t)psid Value to set @ref PSID register. - * @sa getPSID() - */ -#define setPSID(psid) {\ - WIZCHIP_WRITE(PSID, (uint8_t)(psid >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(PSID,1), (uint8_t) psid); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PSID register - * @return uint16_t. Value of @ref PSID register. - * @sa setPSID() - */ -//uint16_t getPSID(void); -#define getPSID() \ - ((WIZCHIP_READ(PSID) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PSID,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PMRU register - * @param (uint16_t)pmru Value to set @ref PMRU register. - * @sa getPMRU() - */ -#define setPMRU(pmru) { \ - WIZCHIP_WRITE(PMRU, (uint8_t)(pmru>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(PMRU,1), (uint8_t) pmru); \ - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PMRU register - * @return uint16_t. Value of @ref PMRU register. - * @sa setPMRU() - */ -#define getPMRU() \ - ((WIZCHIP_READ(PMRU) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PMRU,1))) - -/** - * @ingroup Common_register_access_function - * @brief Get unreachable IP address - * @param (uint8_t*)uipr Pointer variable to get unreachable IP address. It should be allocated 4 bytes. - */ -#define getUIPR(uipr) \ - WIZCHIP_READ_BUF(UIPR,uipr,6) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref UPORTR register - * @return uint16_t. Value of @ref UPORTR register. - */ -#define getUPORTR() \ - ((WIZCHIP_READ(UPORTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(UPORTR,1))) - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PHYCFGR register - * @param (uint8_t)phycfgr Value to set @ref PHYCFGR register. - * @sa getPHYCFGR() - */ -#define setPHYCFGR(phycfgr) \ - WIZCHIP_WRITE(PHYCFGR, phycfgr) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PHYCFGR register - * @return uint8_t. Value of @ref PHYCFGR register. - * @sa setPHYCFGR() - */ -#define getPHYCFGR() \ - WIZCHIP_READ(PHYCFGR) - -/** - * @ingroup Common_register_access_function - * @brief Get @ref VERSIONR register - * @return uint8_t. Value of @ref VERSIONR register. - */ -#define getVERSIONR() \ - WIZCHIP_READ(VERSIONR) - -///////////////////////////////////// - -/////////////////////////////////// -// Socket N register I/O function // -/////////////////////////////////// -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)mr Value to set @ref Sn_MR - * @sa getSn_MR() - */ -#define setSn_MR(sn, mr) \ - WIZCHIP_WRITE(Sn_MR(sn),mr) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_MR. - * @sa setSn_MR() - */ -#define getSn_MR(sn) \ - WIZCHIP_READ(Sn_MR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)cr Value to set @ref Sn_CR - * @sa getSn_CR() - */ -#define setSn_CR(sn, cr) \ - WIZCHIP_WRITE(Sn_CR(sn), cr) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_CR. - * @sa setSn_CR() - */ -#define getSn_CR(sn) \ - WIZCHIP_READ(Sn_CR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)ir Value to set @ref Sn_IR - * @sa getSn_IR() - */ -#define setSn_IR(sn, ir) \ - WIZCHIP_WRITE(Sn_IR(sn), (ir & 0x1F)) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_IR. - * @sa setSn_IR() - */ -#define getSn_IR(sn) \ - (WIZCHIP_READ(Sn_IR(sn)) & 0x1F) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)imr Value to set @ref Sn_IMR - * @sa getSn_IMR() - */ -#define setSn_IMR(sn, imr) \ - WIZCHIP_WRITE(Sn_IMR(sn), (imr & 0x1F)) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_IMR. - * @sa setSn_IMR() - */ -#define getSn_IMR(sn) \ - (WIZCHIP_READ(Sn_IMR(sn)) & 0x1F) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_SR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_SR. - */ -#define getSn_SR(sn) \ - WIZCHIP_READ(Sn_SR(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)port Value to set @ref Sn_PORT. - * @sa getSn_PORT() - */ -#define setSn_PORT(sn, port) { \ - WIZCHIP_WRITE(Sn_PORT(sn), (uint8_t)(port >> 8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_PORT(sn),1), (uint8_t) port); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_PORT. - * @sa setSn_PORT() - */ -#define getSn_PORT(sn) \ - ((WIZCHIP_READ(Sn_PORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_PORT(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DHAR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dhar Pointer variable to set socket n destination hardware address. It should be allocated 6 bytes. - * @sa getSn_DHAR() - */ -#define setSn_DHAR(sn, dhar) \ - WIZCHIP_WRITE_BUF(Sn_DHAR(sn), dhar, 6) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dhar Pointer variable to get socket n destination hardware address. It should be allocated 6 bytes. - * @sa setSn_DHAR() - */ -#define getSn_DHAR(sn, dhar) \ - WIZCHIP_READ_BUF(Sn_DHAR(sn), dhar, 6) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dipr Pointer variable to set socket n destination IP address. It should be allocated 4 bytes. - * @sa getSn_DIPR() - */ -#define setSn_DIPR(sn, dipr) \ - WIZCHIP_WRITE_BUF(Sn_DIPR(sn), dipr, 4) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes. - * @sa SetSn_DIPR() - */ -#define getSn_DIPR(sn, dipr) \ - WIZCHIP_READ_BUF(Sn_DIPR(sn), dipr, 4) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)dport Value to set @ref Sn_DPORT - * @sa getSn_DPORT() - */ -#define setSn_DPORT(sn, dport) { \ - WIZCHIP_WRITE(Sn_DPORT(sn), (uint8_t) (dport>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_DPORT(sn),1), (uint8_t) dport); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_DPORT. - * @sa setSn_DPORT() - */ -#define getSn_DPORT(sn) \ - ((WIZCHIP_READ(Sn_DPORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_DPORT(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_MSSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)mss Value to set @ref Sn_MSSR - * @sa setSn_MSSR() - */ -#define setSn_MSSR(sn, mss) { \ - WIZCHIP_WRITE(Sn_MSSR(sn), (uint8_t)(mss>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_MSSR(sn),1), (uint8_t) mss); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MSSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_MSSR. - * @sa setSn_MSSR() - */ -#define getSn_MSSR(sn) \ - ((WIZCHIP_READ(Sn_MSSR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_MSSR(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TOS register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)tos Value to set @ref Sn_TOS - * @sa getSn_TOS() - */ -#define setSn_TOS(sn, tos) \ - WIZCHIP_WRITE(Sn_TOS(sn), tos) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TOS register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of Sn_TOS. - * @sa setSn_TOS() - */ -#define getSn_TOS(sn) \ - WIZCHIP_READ(Sn_TOS(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TTL register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)ttl Value to set @ref Sn_TTL - * @sa getSn_TTL() - */ -#define setSn_TTL(sn, ttl) \ - WIZCHIP_WRITE(Sn_TTL(sn), ttl) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TTL register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_TTL. - * @sa setSn_TTL() - */ -#define getSn_TTL(sn) \ - WIZCHIP_READ(Sn_TTL(sn)) - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)rxbufsize Value to set @ref Sn_RXBUF_SIZE - * @sa getSn_RXBUF_SIZE() - */ -#define setSn_RXBUF_SIZE(sn, rxbufsize) \ - WIZCHIP_WRITE(Sn_RXBUF_SIZE(sn),rxbufsize) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_RXBUF_SIZE. - * @sa setSn_RXBUF_SIZE() - */ -#define getSn_RXBUF_SIZE(sn) \ - WIZCHIP_READ(Sn_RXBUF_SIZE(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)txbufsize Value to set @ref Sn_TXBUF_SIZE - * @sa getSn_TXBUF_SIZE() - */ -#define setSn_TXBUF_SIZE(sn, txbufsize) \ - WIZCHIP_WRITE(Sn_TXBUF_SIZE(sn), txbufsize) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_TXBUF_SIZE. - * @sa setSn_TXBUF_SIZE() - */ -#define getSn_TXBUF_SIZE(sn) \ - WIZCHIP_READ(Sn_TXBUF_SIZE(sn)) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_FSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_FSR. - */ -uint16_t getSn_TX_FSR(uint8_t sn); - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_RD. - */ -#define getSn_TX_RD(sn) \ - ((WIZCHIP_READ(Sn_TX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_RD(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)txwr Value to set @ref Sn_TX_WR - * @sa GetSn_TX_WR() - */ -#define setSn_TX_WR(sn, txwr) { \ - WIZCHIP_WRITE(Sn_TX_WR(sn), (uint8_t)(txwr>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_TX_WR(sn),1), (uint8_t) txwr); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_TX_WR. - * @sa setSn_TX_WR() - */ -#define getSn_TX_WR(sn) \ - ((WIZCHIP_READ(Sn_TX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_WR(sn),1))) - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RSR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_RX_RSR. - */ -uint16_t getSn_RX_RSR(uint8_t sn); - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)rxrd Value to set @ref Sn_RX_RD - * @sa getSn_RX_RD() - */ -#define setSn_RX_RD(sn, rxrd) { \ - WIZCHIP_WRITE(Sn_RX_RD(sn), (uint8_t)(rxrd>>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_RX_RD(sn),1), (uint8_t) rxrd); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @regurn uint16_t. Value of @ref Sn_RX_RD. - * @sa setSn_RX_RD() - */ -#define getSn_RX_RD(sn) \ - ((WIZCHIP_READ(Sn_RX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RD(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_WR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_RX_WR. - */ -#define getSn_RX_WR(sn) \ - ((WIZCHIP_READ(Sn_RX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_WR(sn),1))) - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_FRAG register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint16_t)frag Value to set @ref Sn_FRAG - * @sa getSn_FRAD() - */ -#define setSn_FRAG(sn, frag) { \ - WIZCHIP_WRITE(Sn_FRAG(sn), (uint8_t)(frag >>8)); \ - WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1), (uint8_t) frag); \ - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_FRAG register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of @ref Sn_FRAG. - * @sa setSn_FRAG() - */ -#define getSn_FRAG(sn) \ - ((WIZCHIP_READ(Sn_FRAG(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1))) - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_KPALVTR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param (uint8_t)kpalvt Value to set @ref Sn_KPALVTR - * @sa getSn_KPALVTR() - */ -#define setSn_KPALVTR(sn, kpalvt) \ - WIZCHIP_WRITE(Sn_KPALVTR(sn), kpalvt) - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_KPALVTR register - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint8_t. Value of @ref Sn_KPALVTR. - * @sa setSn_KPALVTR() - */ -#define getSn_KPALVTR(sn) \ - WIZCHIP_READ(Sn_KPALVTR(sn)) - -////////////////////////////////////// - -///////////////////////////////////// -// Sn_TXBUF & Sn_RXBUF IO function // -///////////////////////////////////// -/** - * @brief Gets the max buffer size of socket sn passed as parameter. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of Socket n RX max buffer size. - */ -#define getSn_RxMAX(sn) \ - (getSn_RXBUF_SIZE(sn) << 10) - -/** - * @brief Gets the max buffer size of socket sn passed as parameters. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @return uint16_t. Value of Socket n TX max buffer size. - */ -//uint16_t getSn_TxMAX(uint8_t sn); -#define getSn_TxMAX(sn) \ - (getSn_TXBUF_SIZE(sn) << 10) - -/** - * @ingroup Basic_IO_function - * @brief It copies data to internal TX memory - * - * @details This function reads the Tx write pointer register and after that, - * it copies the wizdata(pointer buffer) of the length of len(variable) bytes to internal TX memory - * and updates the Tx write pointer register. - * This function is being called by send() and sendto() function also. - * - * @note User should read upper byte first and lower byte later to get proper value. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param wizdata Pointer buffer to write data - * @param len Data length - * @sa wiz_recv_data() - */ -void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It copies data to your buffer from internal RX memory - * - * @details This function read the Rx read pointer register and after that, - * it copies the received data from internal RX memory - * to wizdata(pointer variable) of the length of len(variable) bytes. - * This function is being called by recv() also. - * - * @note User should read upper byte first and lower byte later to get proper value. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param wizdata Pointer buffer to read data - * @param len Data length - * @sa wiz_send_data() - */ -void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len); - -/** - * @ingroup Basic_IO_function - * @brief It discard the received data in RX memory. - * @details It discards the data of the length of len(variable) bytes in internal RX memory. - * @param (uint8_t)sn Socket number. It should be 0 ~ 7. - * @param len Data length - */ -void wiz_recv_ignore(uint8_t sn, uint16_t len); - -#endif // _W5500_H_ diff --git a/drivers/wiznet5k/ethernet/wizchip_conf.c b/drivers/wiznet5k/ethernet/wizchip_conf.c deleted file mode 100644 index 3e54d2c90bcd2..0000000000000 --- a/drivers/wiznet5k/ethernet/wizchip_conf.c +++ /dev/null @@ -1,662 +0,0 @@ -//****************************************************************************/ -//! -//! \file wizchip_conf.c -//! \brief WIZCHIP Config Header File. -//! \version 1.0.1 -//! \date 2013/10/21 -//! \par Revision history -//! <2014/05/01> V1.0.1 Refer to M20140501 -//! 1. Explicit type casting in wizchip_bus_readbyte() & wizchip_bus_writebyte() -// Issued by Mathias ClauBen. -//! uint32_t type converts into ptrdiff_t first. And then recoverting it into uint8_t* -//! For remove the warning when pointer type size is not 32bit. -//! If ptrdiff_t doesn't support in your complier, You should must replace ptrdiff_t into your suitable pointer type. -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//*****************************************************************************/ -//A20140501 : for use the type - ptrdiff_t -#include -// - -#include "wizchip_conf.h" -#include "socket.h" - -/** - * @brief Default function to enable interrupt. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_cris_enter(void) {}; -/** - * @brief Default function to disable interrupt. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_cris_exit(void) {}; -/** - * @brief Default function to select chip. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_cs_select(void) {}; -/** - * @brief Default function to deselect chip. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_cs_deselect(void) {}; -/** - * @brief Default function to read in direct or indirect interface. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ - //M20140501 : Explict pointer type casting -//uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *) AddrSel); }; -uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *)((ptrdiff_t) AddrSel)); }; -/** - * @brief Default function to write in direct or indirect interface. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ - -//M20140501 : Explict pointer type casting -//void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*) AddrSel) = wb; }; -void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*)((ptrdiff_t)AddrSel)) = wb; }; - -/** - * @brief Default function to read in SPI interface. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_spi_readbytes(uint8_t *buf, uint32_t len) {} -/** - * @brief Default function to write in SPI interface. - * @note This function help not to access wrong address. If you do not describe this function or register any functions, - * null function is called. - */ -void wizchip_spi_writebytes(const uint8_t *buf, uint32_t len) {} - -/** - * @\ref _WIZCHIP instance - */ -_WIZCHIP WIZCHIP = - { - .id = _WIZCHIP_ID_, - .if_mode = _WIZCHIP_IO_MODE_, - .CRIS._enter = wizchip_cris_enter, - .CRIS._exit = wizchip_cris_exit, - .CS._select = wizchip_cs_select, - .CS._deselect = wizchip_cs_deselect, - .IF.BUS._read_byte = wizchip_bus_readbyte, - .IF.BUS._write_byte = wizchip_bus_writebyte -// .IF.SPI._read_byte = wizchip_spi_readbyte, -// .IF.SPI._write_byte = wizchip_spi_writebyte - }; - -#if _WIZCHIP_ == 5200 // for W5200 ARP errata -static uint8_t _SUBN_[4]; // subnet -#endif -static uint8_t _DNS_[4]; // DNS server ip address -static dhcp_mode _DHCP_; // DHCP mode - -void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void)) -{ - if(!cris_en || !cris_ex) - { - WIZCHIP.CRIS._enter = wizchip_cris_enter; - WIZCHIP.CRIS._exit = wizchip_cris_exit; - } - else - { - WIZCHIP.CRIS._enter = cris_en; - WIZCHIP.CRIS._exit = cris_ex; - } -} - -void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void)) -{ - if(!cs_sel || !cs_desel) - { - WIZCHIP.CS._select = wizchip_cs_select; - WIZCHIP.CS._deselect = wizchip_cs_deselect; - } - else - { - WIZCHIP.CS._select = cs_sel; - WIZCHIP.CS._deselect = cs_desel; - } -} - -void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb)) -{ - while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_)); - - if(!bus_rb || !bus_wb) - { - WIZCHIP.IF.BUS._read_byte = wizchip_bus_readbyte; - WIZCHIP.IF.BUS._write_byte = wizchip_bus_writebyte; - } - else - { - WIZCHIP.IF.BUS._read_byte = bus_rb; - WIZCHIP.IF.BUS._write_byte = bus_wb; - } -} - -void reg_wizchip_spi_cbfunc(void (*spi_rb)(uint8_t *, uint32_t), void (*spi_wb)(const uint8_t *, uint32_t)) -{ - while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_)); - - if(!spi_rb || !spi_wb) - { - WIZCHIP.IF.SPI._read_bytes = wizchip_spi_readbytes; - WIZCHIP.IF.SPI._write_bytes = wizchip_spi_writebytes; - } - else - { - WIZCHIP.IF.SPI._read_bytes = spi_rb; - WIZCHIP.IF.SPI._write_bytes = spi_wb; - } -} - -int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) -{ - uint8_t tmp = 0; - uint8_t* ptmp[2] = {0,0}; - switch(cwtype) - { - case CW_RESET_WIZCHIP: - wizchip_sw_reset(); - break; - case CW_INIT_WIZCHIP: - if(arg != 0) - { - ptmp[0] = (uint8_t*)arg; - ptmp[1] = ptmp[0] + _WIZCHIP_SOCK_NUM_; - } - return wizchip_init(ptmp[0], ptmp[1]); - case CW_CLR_INTERRUPT: - wizchip_clrinterrupt(*((intr_kind*)arg)); - break; - case CW_GET_INTERRUPT: - *((intr_kind*)arg) = wizchip_getinterrupt(); - break; - case CW_SET_INTRMASK: - wizchip_setinterruptmask(*((intr_kind*)arg)); - break; - case CW_GET_INTRMASK: - *((intr_kind*)arg) = wizchip_getinterruptmask(); - break; - #if _WIZCHIP_ > 5100 - case CW_SET_INTRTIME: - setINTLEVEL(*(uint16_t*)arg); - break; - case CW_GET_INTRTIME: - *(uint16_t*)arg = getINTLEVEL(); - break; - #endif - case CW_GET_ID: - ((uint8_t*)arg)[0] = WIZCHIP.id[0]; - ((uint8_t*)arg)[1] = WIZCHIP.id[1]; - ((uint8_t*)arg)[2] = WIZCHIP.id[2]; - ((uint8_t*)arg)[3] = WIZCHIP.id[3]; - ((uint8_t*)arg)[4] = WIZCHIP.id[4]; - ((uint8_t*)arg)[5] = 0; - break; - #if _WIZCHIP_ == 5500 - case CW_RESET_PHY: - wizphy_reset(); - break; - case CW_SET_PHYCONF: - wizphy_setphyconf((wiz_PhyConf*)arg); - break; - case CW_GET_PHYCONF: - wizphy_getphyconf((wiz_PhyConf*)arg); - break; - case CW_GET_PHYSTATUS: - break; - case CW_SET_PHYPOWMODE: - return wizphy_setphypmode(*(uint8_t*)arg); - #endif - case CW_GET_PHYPOWMODE: - tmp = wizphy_getphypmode(); - if((int8_t)tmp == -1) return -1; - *(uint8_t*)arg = tmp; - break; - case CW_GET_PHYLINK: - tmp = wizphy_getphylink(); - if((int8_t)tmp == -1) return -1; - *(uint8_t*)arg = tmp; - break; - default: - return -1; - } - return 0; -} - - -int8_t ctlnetwork(ctlnetwork_type cntype, void* arg) -{ - - switch(cntype) - { - case CN_SET_NETINFO: - wizchip_setnetinfo((wiz_NetInfo*)arg); - break; - case CN_GET_NETINFO: - wizchip_getnetinfo((wiz_NetInfo*)arg); - break; - case CN_SET_NETMODE: - return wizchip_setnetmode(*(netmode_type*)arg); - case CN_GET_NETMODE: - *(netmode_type*)arg = wizchip_getnetmode(); - break; - case CN_SET_TIMEOUT: - wizchip_settimeout((wiz_NetTimeout*)arg); - break; - case CN_GET_TIMEOUT: - wizchip_gettimeout((wiz_NetTimeout*)arg); - break; - default: - return -1; - } - return 0; -} - -void wizchip_sw_reset(void) -{ - uint8_t gw[4], sn[4], sip[4]; - uint8_t mac[6]; - getSHAR(mac); - getGAR(gw); getSUBR(sn); getSIPR(sip); - setMR(MR_RST); - getMR(); // for delay - setSHAR(mac); - setGAR(gw); - setSUBR(sn); - setSIPR(sip); -} - -int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize) -{ - int8_t i; - int8_t tmp = 0; - wizchip_sw_reset(); - if(txsize) - { - tmp = 0; - for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) - tmp += txsize[i]; - if(tmp > 16) return -1; - for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) - setSn_TXBUF_SIZE(i, txsize[i]); - } - if(rxsize) - { - tmp = 0; - for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) - tmp += rxsize[i]; - if(tmp > 16) return -1; - for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) - setSn_RXBUF_SIZE(i, rxsize[i]); - } - - WIZCHIP_EXPORT(socket_reset)(); - - return 0; -} - -void wizchip_clrinterrupt(intr_kind intr) -{ - uint8_t ir = (uint8_t)intr; - uint8_t sir = (uint8_t)((uint16_t)intr >> 8); -#if _WIZCHIP_ < 5500 - ir |= (1<<4); // IK_WOL -#endif -#if _WIZCHIP_ == 5200 - ir |= (1 << 6); -#endif - -#if _WIZCHIP_ < 5200 - sir &= 0x0F; -#endif - -#if _WIZCHIP_ == 5100 - ir |= sir; - setIR(ir); -#else - setIR(ir); - setSIR(sir); -#endif -} - -intr_kind wizchip_getinterrupt(void) -{ - uint8_t ir = 0; - uint8_t sir = 0; - uint16_t ret = 0; -#if _WIZCHIP_ == 5100 - ir = getIR(); - sir = ir 0x0F; -#else - ir = getIR(); - sir = getSIR(); -#endif - -#if _WIZCHIP_ < 5500 - ir &= ~(1<<4); // IK_WOL -#endif -#if _WIZCHIP_ == 5200 - ir &= ~(1 << 6); -#endif - ret = sir; - ret = (ret << 8) + ir; - return (intr_kind)ret; -} - -void wizchip_setinterruptmask(intr_kind intr) -{ - uint8_t imr = (uint8_t)intr; - uint8_t simr = (uint8_t)((uint16_t)intr >> 8); -#if _WIZCHIP_ < 5500 - imr &= ~(1<<4); // IK_WOL -#endif -#if _WIZCHIP_ == 5200 - imr &= ~(1 << 6); -#endif - -#if _WIZCHIP_ < 5200 - simr &= 0x0F; -#endif - -#if _WIZCHIP_ == 5100 - imr |= simr; - setIMR(imr); -#else - setIMR(imr); - setSIMR(simr); -#endif -} - -intr_kind wizchip_getinterruptmask(void) -{ - uint8_t imr = 0; - uint8_t simr = 0; - uint16_t ret = 0; -#if _WIZCHIP_ == 5100 - imr = getIMR(); - simr = imr 0x0F; -#else - imr = getIMR(); - simr = getSIMR(); -#endif - -#if _WIZCHIP_ < 5500 - imr &= ~(1<<4); // IK_WOL -#endif -#if _WIZCHIP_ == 5200 - imr &= ~(1 << 6); // IK_DEST_UNREACH -#endif - ret = simr; - ret = (ret << 8) + imr; - return (intr_kind)ret; -} - -int8_t wizphy_getphylink(void) -{ - int8_t tmp; -#if _WIZCHIP_ == 5200 - if(getPHYSTATUS() & PHYSTATUS_LINK) - tmp = PHY_LINK_ON; - else - tmp = PHY_LINK_OFF; -#elif _WIZCHIP_ == 5500 - if(getPHYCFGR() & PHYCFGR_LNK_ON) - tmp = PHY_LINK_ON; - else - tmp = PHY_LINK_OFF; -#else - tmp = -1; -#endif - return tmp; -} - -#if _WIZCHIP_ > 5100 - -int8_t wizphy_getphypmode(void) -{ - int8_t tmp = 0; - #if _WIZCHIP_ == 5200 - if(getPHYSTATUS() & PHYSTATUS_POWERDOWN) - tmp = PHY_POWER_DOWN; - else - tmp = PHY_POWER_NORM; - #elif _WIZCHIP_ == 5500 - if(getPHYCFGR() & PHYCFGR_OPMDC_PDOWN) - tmp = PHY_POWER_DOWN; - else - tmp = PHY_POWER_NORM; - #else - tmp = -1; - #endif - return tmp; -} -#endif - -#if _WIZCHIP_ == 5500 -void wizphy_reset(void) -{ - uint8_t tmp = getPHYCFGR(); - tmp &= PHYCFGR_RST; - setPHYCFGR(tmp); - tmp = getPHYCFGR(); - tmp |= ~PHYCFGR_RST; - setPHYCFGR(tmp); -} - -void wizphy_setphyconf(wiz_PhyConf* phyconf) -{ - uint8_t tmp = 0; - if(phyconf->by == PHY_CONFBY_SW) - tmp |= PHYCFGR_OPMD; - else - tmp &= ~PHYCFGR_OPMD; - if(phyconf->mode == PHY_MODE_AUTONEGO) - tmp |= PHYCFGR_OPMDC_ALLA; - else - { - if(phyconf->duplex == PHY_DUPLEX_FULL) - { - if(phyconf->speed == PHY_SPEED_100) - tmp |= PHYCFGR_OPMDC_100F; - else - tmp |= PHYCFGR_OPMDC_10F; - } - else - { - if(phyconf->speed == PHY_SPEED_100) - tmp |= PHYCFGR_OPMDC_100H; - else - tmp |= PHYCFGR_OPMDC_10H; - } - } - setPHYCFGR(tmp); - wizphy_reset(); -} - -void wizphy_getphyconf(wiz_PhyConf* phyconf) -{ - uint8_t tmp = 0; - tmp = getPHYCFGR(); - phyconf->by = (tmp & PHYCFGR_OPMD) ? PHY_CONFBY_SW : PHY_CONFBY_HW; - switch(tmp & PHYCFGR_OPMDC_ALLA) - { - case PHYCFGR_OPMDC_ALLA: - case PHYCFGR_OPMDC_100FA: - phyconf->mode = PHY_MODE_AUTONEGO; - break; - default: - phyconf->mode = PHY_MODE_MANUAL; - break; - } - switch(tmp & PHYCFGR_OPMDC_ALLA) - { - case PHYCFGR_OPMDC_100FA: - case PHYCFGR_OPMDC_100F: - case PHYCFGR_OPMDC_100H: - phyconf->speed = PHY_SPEED_100; - break; - default: - phyconf->speed = PHY_SPEED_10; - break; - } - switch(tmp & PHYCFGR_OPMDC_ALLA) - { - case PHYCFGR_OPMDC_100FA: - case PHYCFGR_OPMDC_100F: - case PHYCFGR_OPMDC_10F: - phyconf->duplex = PHY_DUPLEX_FULL; - break; - default: - phyconf->duplex = PHY_DUPLEX_HALF; - break; - } -} - -void wizphy_getphystat(wiz_PhyConf* phyconf) -{ - uint8_t tmp = getPHYCFGR(); - phyconf->duplex = (tmp & PHYCFGR_DPX_FULL) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF; - phyconf->speed = (tmp & PHYCFGR_SPD_100) ? PHY_SPEED_100 : PHY_SPEED_10; -} - -int8_t wizphy_setphypmode(uint8_t pmode) -{ - uint8_t tmp = 0; - tmp = getPHYCFGR(); - if((tmp & PHYCFGR_OPMD)== 0) return -1; - tmp &= ~PHYCFGR_OPMDC_ALLA; - if( pmode == PHY_POWER_DOWN) - tmp |= PHYCFGR_OPMDC_PDOWN; - else - tmp |= PHYCFGR_OPMDC_ALLA; - setPHYCFGR(tmp); - wizphy_reset(); - tmp = getPHYCFGR(); - if( pmode == PHY_POWER_DOWN) - { - if(tmp & PHYCFGR_OPMDC_PDOWN) return 0; - } - else - { - if(tmp & PHYCFGR_OPMDC_ALLA) return 0; - } - return -1; -} -#endif - - -void wizchip_setnetinfo(wiz_NetInfo* pnetinfo) -{ - setSHAR(pnetinfo->mac); - setGAR(pnetinfo->gw); - setSUBR(pnetinfo->sn); - setSIPR(pnetinfo->ip); -#if _WIZCHIP_ == 5200 // for W5200 ARP errata - _SUBN_[0] = pnetinfo->sn[0]; - _SUBN_[1] = pnetinfo->sn[1]; - _SUBN_[2] = pnetinfo->sn[2]; - _SUBN_[3] = pnetinfo->sn[3]; -#endif - _DNS_[0] = pnetinfo->dns[0]; - _DNS_[1] = pnetinfo->dns[1]; - _DNS_[2] = pnetinfo->dns[2]; - _DNS_[3] = pnetinfo->dns[3]; - _DHCP_ = pnetinfo->dhcp; -} - -void wizchip_getnetinfo(wiz_NetInfo* pnetinfo) -{ - getSHAR(pnetinfo->mac); - getGAR(pnetinfo->gw); - getSUBR(pnetinfo->sn); - getSIPR(pnetinfo->ip); -#if _WIZCHIP_ == 5200 // for W5200 ARP errata - pnetinfo->sn[0] = _SUBN_[0]; - pnetinfo->sn[1] = _SUBN_[1]; - pnetinfo->sn[2] = _SUBN_[2]; - pnetinfo->sn[3] = _SUBN_[3]; -#endif - pnetinfo->dns[0]= _DNS_[0]; - pnetinfo->dns[1]= _DNS_[1]; - pnetinfo->dns[2]= _DNS_[2]; - pnetinfo->dns[3]= _DNS_[3]; - pnetinfo->dhcp = _DHCP_; -} - -#if _WIZCHIP_ == 5200 // for W5200 ARP errata -uint8_t *wizchip_getsubn(void) { - return _SUBN_; -} -#endif - -int8_t wizchip_setnetmode(netmode_type netmode) -{ - uint8_t tmp = 0; -#if _WIZCHIP_ != 5500 - if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK)) return -1; -#else - if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK | NM_FORCEARP)) return -1; -#endif - tmp = getMR(); - tmp |= (uint8_t)netmode; - setMR(tmp); - return 0; -} - -netmode_type wizchip_getnetmode(void) -{ - return (netmode_type) getMR(); -} - -void wizchip_settimeout(wiz_NetTimeout* nettime) -{ - setRCR(nettime->retry_cnt); - setRTR(nettime->time_100us); -} - -void wizchip_gettimeout(wiz_NetTimeout* nettime) -{ - nettime->retry_cnt = getRCR(); - nettime->time_100us = getRTR(); -} diff --git a/drivers/wiznet5k/ethernet/wizchip_conf.h b/drivers/wiznet5k/ethernet/wizchip_conf.h deleted file mode 100644 index 4a7a7bd691400..0000000000000 --- a/drivers/wiznet5k/ethernet/wizchip_conf.h +++ /dev/null @@ -1,554 +0,0 @@ -//***************************************************************************** -// -//! \file wizchip_conf.h -//! \brief WIZCHIP Config Header File. -//! \version 1.0.0 -//! \date 2013/10/21 -//! \par Revision history -//! <2013/10/21> 1st Release -//! \author MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -/** - * @defgroup extra_functions 2. WIZnet Extra Functions - * - * @brief These functions is optional function. It could be replaced at WIZCHIP I/O function because they were made by WIZCHIP I/O functions. - * @details There are functions of configuring WIZCHIP, network, interrupt, phy, network information and timer. \n - * - */ - -#ifndef _WIZCHIP_CONF_H_ -#define _WIZCHIP_CONF_H_ - -#include -/** - * @brief Select WIZCHIP. - * @todo You should select one, \b 5100, \b 5200 ,\b 5500 or etc. \n\n - * ex> #define \_WIZCHIP_ 5500 - */ -#ifndef _WIZCHIP_ -#define _WIZCHIP_ 5200 // 5100, 5200, 5500 -#endif - -#define _WIZCHIP_IO_MODE_NONE_ 0x0000 -#define _WIZCHIP_IO_MODE_BUS_ 0x0100 /**< Bus interface mode */ -#define _WIZCHIP_IO_MODE_SPI_ 0x0200 /**< SPI interface mode */ -//#define _WIZCHIP_IO_MODE_IIC_ 0x0400 -//#define _WIZCHIP_IO_MODE_SDIO_ 0x0800 -// Add to -// - -#define _WIZCHIP_IO_MODE_BUS_DIR_ (_WIZCHIP_IO_MODE_BUS_ + 1) /**< BUS interface mode for direct */ -#define _WIZCHIP_IO_MODE_BUS_INDIR_ (_WIZCHIP_IO_MODE_BUS_ + 2) /**< BUS interface mode for indirect */ - -#define _WIZCHIP_IO_MODE_SPI_VDM_ (_WIZCHIP_IO_MODE_SPI_ + 1) /**< SPI interface mode for variable length data*/ -#define _WIZCHIP_IO_MODE_SPI_FDM_ (_WIZCHIP_IO_MODE_SPI_ + 2) /**< SPI interface mode for fixed length data mode*/ - - -#if (_WIZCHIP_ == 5100) - #define _WIZCHIP_ID_ "W5100\0" -/** - * @brief Define interface mode. - * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ - */ - -// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_DIR_ -// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_ - #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_ - -#elif (_WIZCHIP_ == 5200) - #define _WIZCHIP_ID_ "W5200\0" -/** - * @brief Define interface mode. - * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ - */ -// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_ - #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_ - #include "w5200/w5200.h" -#elif (_WIZCHIP_ == 5500) - #define _WIZCHIP_ID_ "W5500\0" - -/** - * @brief Define interface mode. \n - * @todo Should select interface mode as chip. - * - @ref \_WIZCHIP_IO_MODE_SPI_ \n - * -@ref \_WIZCHIP_IO_MODE_SPI_VDM_ : Valid only in @ref \_WIZCHIP_ == 5500 \n - * -@ref \_WIZCHIP_IO_MODE_SPI_FDM_ : Valid only in @ref \_WIZCHIP_ == 5500 \n - * - @ref \_WIZCHIP_IO_MODE_BUS_ \n - * - @ref \_WIZCHIP_IO_MODE_BUS_DIR_ \n - * - @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ \n - * - Others will be defined in future. \n\n - * ex> #define \_WIZCHIP_IO_MODE_ \_WIZCHIP_IO_MODE_SPI_VDM_ - * - */ - //#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_FDM_ - #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_VDM_ - #include "w5500/w5500.h" -#else - #error "Unknown defined _WIZCHIP_. You should define one of 5100, 5200, and 5500 !!!" -#endif - -#ifndef _WIZCHIP_IO_MODE_ - #error "Undefined _WIZCHIP_IO_MODE_. You should define it !!!" -#endif - -/** - * @brief Define I/O base address when BUS IF mode. - * @todo Should re-define it to fit your system when BUS IF Mode (@ref \_WIZCHIP_IO_MODE_BUS_, - * @ref \_WIZCHIP_IO_MODE_BUS_DIR_, @ref \_WIZCHIP_IO_MODE_BUS_INDIR_). \n\n - * ex> #define \_WIZCHIP_IO_BASE_ 0x00008000 - */ -#define _WIZCHIP_IO_BASE_ 0x00000000 // - -#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS - #ifndef _WIZCHIP_IO_BASE_ - #error "You should be define _WIZCHIP_IO_BASE to fit your system memory map." - #endif -#endif - -#if _WIZCHIP_ > 5100 - #define _WIZCHIP_SOCK_NUM_ 8 ///< The count of independant socket of @b WIZCHIP -#else - #define _WIZCHIP_SOCK_NUM_ 4 ///< The count of independant socket of @b WIZCHIP -#endif - - -/******************************************************** -* WIZCHIP BASIC IF functions for SPI, SDIO, I2C , ETC. -*********************************************************/ -/** - * @ingroup DATA_TYPE - * @brief The set of callback functions for W5500:@ref WIZCHIP_IO_Functions W5200:@ref WIZCHIP_IO_Functions_W5200 - */ -typedef struct __WIZCHIP -{ - uint16_t if_mode; ///< host interface mode - uint8_t id[6]; ///< @b WIZCHIP ID such as @b 5100, @b 5200, @b 5500, and so on. - /** - * The set of critical section callback func. - */ - struct _CRIS - { - void (*_enter) (void); ///< crtical section enter - void (*_exit) (void); ///< critial section exit - }CRIS; - /** - * The set of @ref\_WIZCHIP_ select control callback func. - */ - struct _CS - { - void (*_select) (void); ///< @ref \_WIZCHIP_ selected - void (*_deselect)(void); ///< @ref \_WIZCHIP_ deselected - }CS; - /** - * The set of interface IO callback func. - */ - union _IF - { - /** - * For BUS interface IO - */ - struct - { - uint8_t (*_read_byte) (uint32_t AddrSel); - void (*_write_byte) (uint32_t AddrSel, uint8_t wb); - }BUS; - /** - * For SPI interface IO - */ - struct - { - void (*_read_bytes) (uint8_t *buf, uint32_t len); - void (*_write_bytes) (const uint8_t *buf, uint32_t len); - }SPI; - // To be added - // - }IF; -}_WIZCHIP; - -extern _WIZCHIP WIZCHIP; - -/** - * @ingroup DATA_TYPE - * WIZCHIP control type enumration used in @ref ctlwizchip(). - */ -typedef enum -{ - CW_RESET_WIZCHIP, ///< Resets WIZCHIP by softly - CW_INIT_WIZCHIP, ///< Inializes to WIZCHIP with SOCKET buffer size 2 or 1 dimension array typed uint8_t. - CW_GET_INTERRUPT, ///< Get Interrupt status of WIZCHIP - CW_CLR_INTERRUPT, ///< Clears interrupt - CW_SET_INTRMASK, ///< Masks interrupt - CW_GET_INTRMASK, ///< Get interrupt mask - CW_SET_INTRTIME, ///< Set interval time between the current and next interrupt. - CW_GET_INTRTIME, ///< Set interval time between the current and next interrupt. - CW_GET_ID, ///< Gets WIZCHIP name. - -#if _WIZCHIP_ == 5500 - CW_RESET_PHY, ///< Resets internal PHY. Valid Only W5000 - CW_SET_PHYCONF, ///< When PHY configured by interal register, PHY operation mode (Manual/Auto, 10/100, Half/Full). Valid Only W5000 - CW_GET_PHYCONF, ///< Get PHY operation mode in interal register. Valid Only W5000 - CW_GET_PHYSTATUS, ///< Get real PHY status on operating. Valid Only W5000 - CW_SET_PHYPOWMODE, ///< Set PHY power mode as noraml and down when PHYSTATUS.OPMD == 1. Valid Only W5000 -#endif - CW_GET_PHYPOWMODE, ///< Get PHY Power mode as down or normal - CW_GET_PHYLINK ///< Get PHY Link status -}ctlwizchip_type; - -/** - * @ingroup DATA_TYPE - * Network control type enumration used in @ref ctlnetwork(). - */ -typedef enum -{ - CN_SET_NETINFO, ///< Set Network with @ref wiz_NetInfo - CN_GET_NETINFO, ///< Get Network with @ref wiz_NetInfo - CN_SET_NETMODE, ///< Set network mode as WOL, PPPoE, Ping Block, and Force ARP mode - CN_GET_NETMODE, ///< Get network mode as WOL, PPPoE, Ping Block, and Force ARP mode - CN_SET_TIMEOUT, ///< Set network timeout as retry count and time. - CN_GET_TIMEOUT, ///< Get network timeout as retry count and time. -}ctlnetwork_type; - -/** - * @ingroup DATA_TYPE - * Interrupt kind when CW_SET_INTRRUPT, CW_GET_INTERRUPT, CW_SET_INTRMASK - * and CW_GET_INTRMASK is used in @ref ctlnetwork(). - * It can be used with OR operation. - */ -typedef enum -{ -#if _WIZCHIP_ > 5200 - IK_WOL = (1 << 4), ///< Wake On Lan by receiving the magic packet. Valid in W500. -#endif - - IK_PPPOE_TERMINATED = (1 << 5), ///< PPPoE Disconnected - -#if _WIZCHIP_ != 5200 - IK_DEST_UNREACH = (1 << 6), ///< Destination IP & Port Unreable, No use in W5200 -#endif - - IK_IP_CONFLICT = (1 << 7), ///< IP conflict occurred - - IK_SOCK_0 = (1 << 8), ///< Socket 0 interrupt - IK_SOCK_1 = (1 << 9), ///< Socket 1 interrupt - IK_SOCK_2 = (1 << 10), ///< Socket 2 interrupt - IK_SOCK_3 = (1 << 11), ///< Socket 3 interrupt -#if _WIZCHIP_ > 5100 - IK_SOCK_4 = (1 << 12), ///< Socket 4 interrupt, No use in 5100 - IK_SOCK_5 = (1 << 13), ///< Socket 5 interrupt, No use in 5100 - IK_SOCK_6 = (1 << 14), ///< Socket 6 interrupt, No use in 5100 - IK_SOCK_7 = (1 << 15), ///< Socket 7 interrupt, No use in 5100 -#endif - -#if _WIZCHIP_ > 5100 - IK_SOCK_ALL = (0xFF << 8) ///< All Socket interrpt -#else - IK_SOCK_ALL = (0x0F << 8) ///< All Socket interrpt -#endif -}intr_kind; - -#define PHY_CONFBY_HW 0 ///< Configured PHY operation mode by HW pin -#define PHY_CONFBY_SW 1 ///< Configured PHY operation mode by SW register -#define PHY_MODE_MANUAL 0 ///< Configured PHY operation mode with user setting. -#define PHY_MODE_AUTONEGO 1 ///< Configured PHY operation mode with auto-negotiation -#define PHY_SPEED_10 0 ///< Link Speed 10 -#define PHY_SPEED_100 1 ///< Link Speed 100 -#define PHY_DUPLEX_HALF 0 ///< Link Half-Duplex -#define PHY_DUPLEX_FULL 1 ///< Link Full-Duplex -#define PHY_LINK_OFF 0 ///< Link Off -#define PHY_LINK_ON 1 ///< Link On -#define PHY_POWER_NORM 0 ///< PHY power normal mode -#define PHY_POWER_DOWN 1 ///< PHY power down mode - - -#if _WIZCHIP_ == 5500 -/** - * @ingroup DATA_TYPE - * It configures PHY configuration when CW_SET PHYCONF or CW_GET_PHYCONF in W5500, - * and it indicates the real PHY status configured by HW or SW in all WIZCHIP. \n - * Valid only in W5500. - */ -typedef struct wiz_PhyConf_t -{ - uint8_t by; ///< set by @ref PHY_CONFBY_HW or @ref PHY_CONFBY_SW - uint8_t mode; ///< set by @ref PHY_MODE_MANUAL or @ref PHY_MODE_AUTONEGO - uint8_t speed; ///< set by @ref PHY_SPEED_10 or @ref PHY_SPEED_100 - uint8_t duplex; ///< set by @ref PHY_DUPLEX_HALF @ref PHY_DUPLEX_FULL - //uint8_t power; ///< set by @ref PHY_POWER_NORM or @ref PHY_POWER_DOWN - //uint8_t link; ///< Valid only in CW_GET_PHYSTATUS. set by @ref PHY_LINK_ON or PHY_DUPLEX_OFF - }wiz_PhyConf; -#endif - -/** - * @ingroup DATA_TYPE - * It used in setting dhcp_mode of @ref wiz_NetInfo. - */ -typedef enum -{ - NETINFO_STATIC = 1, ///< Static IP configuration by manually. - NETINFO_DHCP ///< Dynamic IP configruation from a DHCP sever -}dhcp_mode; - -/** - * @ingroup DATA_TYPE - * Network Information for WIZCHIP - */ -typedef struct wiz_NetInfo_t -{ - uint8_t mac[6]; ///< Source Mac Address - uint8_t ip[4]; ///< Source IP Address - uint8_t sn[4]; ///< Subnet Mask - uint8_t gw[4]; ///< Gateway IP Address - uint8_t dns[4]; ///< DNS server IP Address - dhcp_mode dhcp; ///< 1 - Static, 2 - DHCP -}wiz_NetInfo; - -/** - * @ingroup DATA_TYPE - * Network mode - */ -typedef enum -{ -#if _WIZCHIP_ == 5500 - NM_FORCEARP = (1<<1), ///< Force to APP send whenever udp data is sent. Valid only in W5500 -#endif - NM_WAKEONLAN = (1<<5), ///< Wake On Lan - NM_PINGBLOCK = (1<<4), ///< Block ping-request - NM_PPPOE = (1<<3), ///< PPPoE mode -}netmode_type; - -/** - * @ingroup DATA_TYPE - * Used in CN_SET_TIMEOUT or CN_GET_TIMEOUT of @ref ctlwizchip() for timeout configruation. - */ -typedef struct wiz_NetTimeout_t -{ - uint8_t retry_cnt; ///< retry count - uint16_t time_100us; ///< time unit 100us -}wiz_NetTimeout; - -/** - *@brief Registers call back function for critical section of I/O functions such as - *\ref WIZCHIP_READ, @ref WIZCHIP_WRITE, @ref WIZCHIP_READ_BUF and @ref WIZCHIP_WRITE_BUF. - *@param cris_en : callback function for critical section enter. - *@param cris_ex : callback function for critical section exit. - *@todo Describe @ref WIZCHIP_CRITICAL_ENTER and @ref WIZCHIP_CRITICAL_EXIT marco or register your functions. - *@note If you do not describe or register, default functions(@ref wizchip_cris_enter & @ref wizchip_cris_exit) is called. - */ -void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void)); - - -/** - *@brief Registers call back function for WIZCHIP select & deselect. - *@param cs_sel : callback function for WIZCHIP select - *@param cs_desel : callback fucntion for WIZCHIP deselect - *@todo Describe @ref wizchip_cs_select and @ref wizchip_cs_deselect function or register your functions. - *@note If you do not describe or register, null function is called. - */ -void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void)); - -/** - *@brief Registers call back function for bus interface. - *@param bus_rb : callback function to read byte data using system bus - *@param bus_wb : callback function to write byte data using system bus - *@todo Describe @ref wizchip_bus_readbyte and @ref wizchip_bus_writebyte function - *or register your functions. - *@note If you do not describe or register, null function is called. - */ -void reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb)); - -/** - *@brief Registers call back function for SPI interface. - *@param spi_rb : callback function to read byte usig SPI - *@param spi_wb : callback function to write byte usig SPI - *@todo Describe \ref wizchip_spi_readbyte and \ref wizchip_spi_writebyte function - *or register your functions. - *@note If you do not describe or register, null function is called. - */ -void reg_wizchip_spi_cbfunc(void (*spi_rb)(uint8_t *, uint32_t), void (*spi_wb)(const uint8_t *, uint32_t)); - -/** - * @ingroup extra_functions - * @brief Controls to the WIZCHIP. - * @details Resets WIZCHIP & internal PHY, Configures PHY mode, Monitor PHY(Link,Speed,Half/Full/Auto), - * controls interrupt & mask and so on. - * @param cwtype : Decides to the control type - * @param arg : arg type is dependent on cwtype. - * @return 0 : Success \n - * -1 : Fail because of invalid \ref ctlwizchip_type or unsupported \ref ctlwizchip_type in WIZCHIP - */ -int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg); - -/** - * @ingroup extra_functions - * @brief Controls to network. - * @details Controls to network environment, mode, timeout and so on. - * @param cntype : Input. Decides to the control type - * @param arg : Inout. arg type is dependent on cntype. - * @return -1 : Fail because of invalid \ref ctlnetwork_type or unsupported \ref ctlnetwork_type in WIZCHIP \n - * 0 : Success - */ -int8_t ctlnetwork(ctlnetwork_type cntype, void* arg); - - -/* - * The following functions are implemented for internal use. - * but You can call these functions for code size reduction instead of ctlwizchip() and ctlnetwork(). - */ - -/** - * @ingroup extra_functions - * @brief Reset WIZCHIP by softly. - */ -void wizchip_sw_reset(void); - -/** - * @ingroup extra_functions - * @brief Initializes WIZCHIP with socket buffer size - * @param txsize Socket tx buffer sizes. If null, initialized the default size 2KB. - * @param rxsize Socket rx buffer sizes. If null, initialized the default size 2KB. - * @return 0 : succcess \n - * -1 : fail. Invalid buffer size - */ -int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize); - -/** - * @ingroup extra_functions - * @brief Clear Interrupt of WIZCHIP. - * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t. - */ -void wizchip_clrinterrupt(intr_kind intr); - -/** - * @ingroup extra_functions - * @brief Get Interrupt of WIZCHIP. - * @return @ref intr_kind value operated OR. It can type-cast to uint16_t. - */ -intr_kind wizchip_getinterrupt(void); - -/** - * @ingroup extra_functions - * @brief Mask or Unmask Interrupt of WIZCHIP. - * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t. - */ -void wizchip_setinterruptmask(intr_kind intr); - -/** - * @ingroup extra_functions - * @brief Get Interrupt mask of WIZCHIP. - * @return : The operated OR vaule of @ref intr_kind. It can type-cast to uint16_t. - */ -intr_kind wizchip_getinterruptmask(void); - -#if _WIZCHIP_ > 5100 - int8_t wizphy_getphylink(void); ///< get the link status of phy in WIZCHIP. No use in W5100 - int8_t wizphy_getphypmode(void); ///< get the power mode of PHY in WIZCHIP. No use in W5100 -#endif - -#if _WIZCHIP_ == 5500 - void wizphy_reset(void); ///< Reset phy. Vailid only in W5500 -/** - * @ingroup extra_functions - * @brief Set the phy information for WIZCHIP without power mode - * @param phyconf : @ref wiz_PhyConf - */ - void wizphy_setphyconf(wiz_PhyConf* phyconf); - /** - * @ingroup extra_functions - * @brief Get phy configuration information. - * @param phyconf : @ref wiz_PhyConf - */ - void wizphy_getphyconf(wiz_PhyConf* phyconf); - /** - * @ingroup extra_functions - * @brief Get phy status. - * @param phyconf : @ref wiz_PhyConf - */ - void wizphy_getphystat(wiz_PhyConf* phyconf); - /** - * @ingroup extra_functions - * @brief set the power mode of phy inside WIZCHIP. Refer to @ref PHYCFGR in W5500, @ref PHYSTATUS in W5200 - * @param pmode Settig value of power down mode. - */ - int8_t wizphy_setphypmode(uint8_t pmode); -#endif - -/** -* @ingroup extra_functions - * @brief Set the network information for WIZCHIP - * @param pnetinfo : @ref wizNetInfo - */ -void wizchip_setnetinfo(wiz_NetInfo* pnetinfo); - -/** - * @ingroup extra_functions - * @brief Get the network information for WIZCHIP - * @param pnetinfo : @ref wizNetInfo - */ -void wizchip_getnetinfo(wiz_NetInfo* pnetinfo); - -#if _WIZCHIP_ == 5200 // for W5200 ARP errata -uint8_t *wizchip_getsubn(void); -#endif - -/** - * @ingroup extra_functions - * @brief Set the network mode such WOL, PPPoE, Ping Block, and etc. - * @param pnetinfo Value of network mode. Refer to @ref netmode_type. - */ -int8_t wizchip_setnetmode(netmode_type netmode); - -/** - * @ingroup extra_functions - * @brief Get the network mode such WOL, PPPoE, Ping Block, and etc. - * @return Value of network mode. Refer to @ref netmode_type. - */ -netmode_type wizchip_getnetmode(void); - -/** - * @ingroup extra_functions - * @brief Set retry time value(@ref RTR) and retry count(@ref RCR). - * @details @ref RTR configures the retransmission timeout period and @ref RCR configures the number of time of retransmission. - * @param nettime @ref RTR value and @ref RCR value. Refer to @ref wiz_NetTimeout. - */ -void wizchip_settimeout(wiz_NetTimeout* nettime); - -/** - * @ingroup extra_functions - * @brief Get retry time value(@ref RTR) and retry count(@ref RCR). - * @details @ref RTR configures the retransmission timeout period and @ref RCR configures the number of time of retransmission. - * @param nettime @ref RTR value and @ref RCR value. Refer to @ref wiz_NetTimeout. - */ -void wizchip_gettimeout(wiz_NetTimeout* nettime); - -#endif // _WIZCHIP_CONF_H_ diff --git a/drivers/wiznet5k/internet/dhcp/dhcp.c b/drivers/wiznet5k/internet/dhcp/dhcp.c deleted file mode 100644 index 5747582599db4..0000000000000 --- a/drivers/wiznet5k/internet/dhcp/dhcp.c +++ /dev/null @@ -1,978 +0,0 @@ -//***************************************************************************** -// -//! \file dhcp.c -//! \brief DHCP APIs implement file. -//! \details Processig DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE. -//! \version 1.1.0 -//! \date 2013/11/18 -//! \par Revision history -//! <2013/11/18> 1st Release -//! <2012/12/20> V1.1.0 -//! 1. Optimize code -//! 2. Add reg_dhcp_cbfunc() -//! 3. Add DHCP_stop() -//! 4. Integrate check_DHCP_state() & DHCP_run() to DHCP_run() -//! 5. Don't care system endian -//! 6. Add comments -//! <2012/12/26> V1.1.1 -//! 1. Modify variable declaration: dhcp_tick_1s is declared volatile for code optimization -//! \author Eric Jung & MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -//#include "Ethernet/socket.h" -//#include "Internet/DHCP/dhcp.h" -#include "../../Ethernet/socket.h" -#include "dhcp.h" - -/* If you want to display debug & processing message, Define _DHCP_DEBUG_ in dhcp.h */ - -#ifdef _DHCP_DEBUG_ - #include -#endif - -/* DHCP state machine. */ -#define STATE_DHCP_INIT 0 ///< Initialize -#define STATE_DHCP_DISCOVER 1 ///< send DISCOVER and wait OFFER -#define STATE_DHCP_REQUEST 2 ///< send REQEUST and wait ACK or NACK -#define STATE_DHCP_LEASED 3 ///< ReceiveD ACK and IP leased -#define STATE_DHCP_REREQUEST 4 ///< send REQUEST for maintaining leased IP -#define STATE_DHCP_RELEASE 5 ///< No use -#define STATE_DHCP_STOP 6 ///< Stop processing DHCP - -#define DHCP_FLAGSBROADCAST 0x8000 ///< The broadcast value of flags in @ref RIP_MSG -#define DHCP_FLAGSUNICAST 0x0000 ///< The unicast value of flags in @ref RIP_MSG - -/* DHCP message OP code */ -#define DHCP_BOOTREQUEST 1 ///< Request Message used in op of @ref RIP_MSG -#define DHCP_BOOTREPLY 2 ///< Reply Message used i op of @ref RIP_MSG - -/* DHCP message type */ -#define DHCP_DISCOVER 1 ///< DISCOVER message in OPT of @ref RIP_MSG -#define DHCP_OFFER 2 ///< OFFER message in OPT of @ref RIP_MSG -#define DHCP_REQUEST 3 ///< REQUEST message in OPT of @ref RIP_MSG -#define DHCP_DECLINE 4 ///< DECLINE message in OPT of @ref RIP_MSG -#define DHCP_ACK 5 ///< ACK message in OPT of @ref RIP_MSG -#define DHCP_NAK 6 ///< NACK message in OPT of @ref RIP_MSG -#define DHCP_RELEASE 7 ///< RELEASE message in OPT of @ref RIP_MSG. No use -#define DHCP_INFORM 8 ///< INFORM message in OPT of @ref RIP_MSG. No use - -#define DHCP_HTYPE10MB 1 ///< Used in type of @ref RIP_MSG -#define DHCP_HTYPE100MB 2 ///< Used in type of @ref RIP_MSG - -#define DHCP_HLENETHERNET 6 ///< Used in hlen of @ref RIP_MSG -#define DHCP_HOPS 0 ///< Used in hops of @ref RIP_MSG -#define DHCP_SECS 0 ///< Used in secs of @ref RIP_MSG - -#define INFINITE_LEASETIME 0xffffffff ///< Infinite lease time - -#define OPT_SIZE 312 /// Max OPT size of @ref RIP_MSG -#define RIP_MSG_SIZE (236+OPT_SIZE) /// Max size of @ref RIP_MSG - -/* - * @brief DHCP option and value (cf. RFC1533) - */ -enum -{ - padOption = 0, - subnetMask = 1, - timerOffset = 2, - routersOnSubnet = 3, - timeServer = 4, - nameServer = 5, - dns = 6, - logServer = 7, - cookieServer = 8, - lprServer = 9, - impressServer = 10, - resourceLocationServer = 11, - hostName = 12, - bootFileSize = 13, - meritDumpFile = 14, - domainName = 15, - swapServer = 16, - rootPath = 17, - extentionsPath = 18, - IPforwarding = 19, - nonLocalSourceRouting = 20, - policyFilter = 21, - maxDgramReasmSize = 22, - defaultIPTTL = 23, - pathMTUagingTimeout = 24, - pathMTUplateauTable = 25, - ifMTU = 26, - allSubnetsLocal = 27, - broadcastAddr = 28, - performMaskDiscovery = 29, - maskSupplier = 30, - performRouterDiscovery = 31, - routerSolicitationAddr = 32, - staticRoute = 33, - trailerEncapsulation = 34, - arpCacheTimeout = 35, - ethernetEncapsulation = 36, - tcpDefaultTTL = 37, - tcpKeepaliveInterval = 38, - tcpKeepaliveGarbage = 39, - nisDomainName = 40, - nisServers = 41, - ntpServers = 42, - vendorSpecificInfo = 43, - netBIOSnameServer = 44, - netBIOSdgramDistServer = 45, - netBIOSnodeType = 46, - netBIOSscope = 47, - xFontServer = 48, - xDisplayManager = 49, - dhcpRequestedIPaddr = 50, - dhcpIPaddrLeaseTime = 51, - dhcpOptionOverload = 52, - dhcpMessageType = 53, - dhcpServerIdentifier = 54, - dhcpParamRequest = 55, - dhcpMsg = 56, - dhcpMaxMsgSize = 57, - dhcpT1value = 58, - dhcpT2value = 59, - dhcpClassIdentifier = 60, - dhcpClientIdentifier = 61, - endOption = 255 -}; - -/* - * @brief DHCP message format - */ -typedef struct { - uint8_t op; ///< @ref DHCP_BOOTREQUEST or @ref DHCP_BOOTREPLY - uint8_t htype; ///< @ref DHCP_HTYPE10MB or @ref DHCP_HTYPE100MB - uint8_t hlen; ///< @ref DHCP_HLENETHERNET - uint8_t hops; ///< @ref DHCP_HOPS - uint32_t xid; ///< @ref DHCP_XID This increase one every DHCP transaction. - uint16_t secs; ///< @ref DHCP_SECS - uint16_t flags; ///< @ref DHCP_FLAGSBROADCAST or @ref DHCP_FLAGSUNICAST - uint8_t ciaddr[4]; ///< @ref Request IP to DHCP sever - uint8_t yiaddr[4]; ///< @ref Offered IP from DHCP server - uint8_t siaddr[4]; ///< No use - uint8_t giaddr[4]; ///< No use - uint8_t chaddr[16]; ///< DHCP client 6bytes MAC address. Others is filled to zero - uint8_t sname[64]; ///< No use - uint8_t file[128]; ///< No use - uint8_t OPT[OPT_SIZE]; ///< Option -} RIP_MSG; - - - -uint8_t DHCP_SOCKET; // Socket number for DHCP - -uint8_t DHCP_SIP[4]; // DHCP Server IP address - -// Network information from DHCP Server -uint8_t OLD_allocated_ip[4] = {0, }; // Previous IP address -uint8_t DHCP_allocated_ip[4] = {0, }; // IP address from DHCP -uint8_t DHCP_allocated_gw[4] = {0, }; // Gateway address from DHCP -uint8_t DHCP_allocated_sn[4] = {0, }; // Subnet mask from DHCP -uint8_t DHCP_allocated_dns[4] = {0, }; // DNS address from DHCP - - -int8_t dhcp_state = STATE_DHCP_INIT; // DHCP state -int8_t dhcp_retry_count = 0; - -uint32_t dhcp_lease_time = INFINITE_LEASETIME; -volatile uint32_t dhcp_tick_1s = 0; // unit 1 second -uint32_t dhcp_tick_next = DHCP_WAIT_TIME ; - -uint32_t DHCP_XID; // Any number - -RIP_MSG* pDHCPMSG; // Buffer pointer for DHCP processing - -uint8_t HOST_NAME[] = DCHP_HOST_NAME; - -uint8_t DHCP_CHADDR[6]; // DHCP Client MAC address. - -/* The default callback function */ -void default_ip_assign(void); -void default_ip_update(void); -void default_ip_conflict(void); - -/* Callback handler */ -void (*dhcp_ip_assign)(void) = default_ip_assign; /* handler to be called when the IP address from DHCP server is first assigned */ -void (*dhcp_ip_update)(void) = default_ip_update; /* handler to be called when the IP address from DHCP server is updated */ -void (*dhcp_ip_conflict)(void) = default_ip_conflict; /* handler to be called when the IP address from DHCP server is conflict */ - -void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void)); - - -/* send DISCOVER message to DHCP server */ -void send_DHCP_DISCOVER(void); - -/* send REQEUST message to DHCP server */ -void send_DHCP_REQUEST(void); - -/* send DECLINE message to DHCP server */ -void send_DHCP_DECLINE(void); - -/* IP conflict check by sending ARP-request to leased IP and wait ARP-response. */ -int8_t check_DHCP_leasedIP(void); - -/* check the timeout in DHCP process */ -uint8_t check_DHCP_timeout(void); - -/* Intialize to timeout process. */ -void reset_DHCP_timeout(void); - -/* Parse message as OFFER and ACK and NACK from DHCP server.*/ -int8_t parseDHCPCMSG(void); - -/* The default handler of ip assign first */ -void default_ip_assign(void) -{ - setSIPR(DHCP_allocated_ip); - setSUBR(DHCP_allocated_sn); - setGAR (DHCP_allocated_gw); -} - -/* The default handler of ip changed */ -void default_ip_update(void) -{ - /* WIZchip Software Reset */ - setMR(MR_RST); - getMR(); // for delay - default_ip_assign(); - setSHAR(DHCP_CHADDR); -} - -/* The default handler of ip changed */ -void default_ip_conflict(void) -{ - // WIZchip Software Reset - setMR(MR_RST); - getMR(); // for delay - setSHAR(DHCP_CHADDR); -} - -/* register the call back func. */ -void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void)) -{ - dhcp_ip_assign = default_ip_assign; - dhcp_ip_update = default_ip_update; - dhcp_ip_conflict = default_ip_conflict; - if(ip_assign) dhcp_ip_assign = ip_assign; - if(ip_update) dhcp_ip_update = ip_update; - if(ip_conflict) dhcp_ip_conflict = ip_conflict; -} - -/* make the common DHCP message */ -void makeDHCPMSG(void) -{ - uint8_t bk_mac[6]; - uint8_t* ptmp; - uint8_t i; - getSHAR(bk_mac); - pDHCPMSG->op = DHCP_BOOTREQUEST; - pDHCPMSG->htype = DHCP_HTYPE10MB; - pDHCPMSG->hlen = DHCP_HLENETHERNET; - pDHCPMSG->hops = DHCP_HOPS; - ptmp = (uint8_t*)(&pDHCPMSG->xid); - *(ptmp+0) = (uint8_t)((DHCP_XID & 0xFF000000) >> 24); - *(ptmp+1) = (uint8_t)((DHCP_XID & 0x00FF0000) >> 16); - *(ptmp+2) = (uint8_t)((DHCP_XID & 0x0000FF00) >> 8); - *(ptmp+3) = (uint8_t)((DHCP_XID & 0x000000FF) >> 0); - pDHCPMSG->secs = DHCP_SECS; - ptmp = (uint8_t*)(&pDHCPMSG->flags); - *(ptmp+0) = (uint8_t)((DHCP_FLAGSBROADCAST & 0xFF00) >> 8); - *(ptmp+1) = (uint8_t)((DHCP_FLAGSBROADCAST & 0x00FF) >> 0); - - pDHCPMSG->ciaddr[0] = 0; - pDHCPMSG->ciaddr[1] = 0; - pDHCPMSG->ciaddr[2] = 0; - pDHCPMSG->ciaddr[3] = 0; - - pDHCPMSG->yiaddr[0] = 0; - pDHCPMSG->yiaddr[1] = 0; - pDHCPMSG->yiaddr[2] = 0; - pDHCPMSG->yiaddr[3] = 0; - - pDHCPMSG->siaddr[0] = 0; - pDHCPMSG->siaddr[1] = 0; - pDHCPMSG->siaddr[2] = 0; - pDHCPMSG->siaddr[3] = 0; - - pDHCPMSG->giaddr[0] = 0; - pDHCPMSG->giaddr[1] = 0; - pDHCPMSG->giaddr[2] = 0; - pDHCPMSG->giaddr[3] = 0; - - pDHCPMSG->chaddr[0] = DHCP_CHADDR[0]; - pDHCPMSG->chaddr[1] = DHCP_CHADDR[1]; - pDHCPMSG->chaddr[2] = DHCP_CHADDR[2]; - pDHCPMSG->chaddr[3] = DHCP_CHADDR[3]; - pDHCPMSG->chaddr[4] = DHCP_CHADDR[4]; - pDHCPMSG->chaddr[5] = DHCP_CHADDR[5]; - - for (i = 6; i < 16; i++) pDHCPMSG->chaddr[i] = 0; - for (i = 0; i < 64; i++) pDHCPMSG->sname[i] = 0; - for (i = 0; i < 128; i++) pDHCPMSG->file[i] = 0; - - // MAGIC_COOKIE - pDHCPMSG->OPT[0] = (uint8_t)((MAGIC_COOKIE & 0xFF000000) >> 24); - pDHCPMSG->OPT[1] = (uint8_t)((MAGIC_COOKIE & 0x00FF0000) >> 16); - pDHCPMSG->OPT[2] = (uint8_t)((MAGIC_COOKIE & 0x0000FF00) >> 8); - pDHCPMSG->OPT[3] = (uint8_t) (MAGIC_COOKIE & 0x000000FF) >> 0; -} - -/* SEND DHCP DISCOVER */ -void send_DHCP_DISCOVER(void) -{ - uint16_t i; - uint8_t ip[4]; - uint16_t k = 0; - - makeDHCPMSG(); - - k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG() - - // Option Request Param - pDHCPMSG->OPT[k++] = dhcpMessageType; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_DISCOVER; - - // Client identifier - pDHCPMSG->OPT[k++] = dhcpClientIdentifier; - pDHCPMSG->OPT[k++] = 0x07; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[0]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[1]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[2]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[3]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[4]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[5]; - - // host name - pDHCPMSG->OPT[k++] = hostName; - pDHCPMSG->OPT[k++] = 0; // fill zero length of hostname - for(i = 0 ; HOST_NAME[i] != 0; i++) - pDHCPMSG->OPT[k++] = HOST_NAME[i]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[3]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[4]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[5]; - pDHCPMSG->OPT[k - (i+3+1)] = i+3; // length of hostname - - pDHCPMSG->OPT[k++] = dhcpParamRequest; - pDHCPMSG->OPT[k++] = 0x06; // length of request - pDHCPMSG->OPT[k++] = subnetMask; - pDHCPMSG->OPT[k++] = routersOnSubnet; - pDHCPMSG->OPT[k++] = dns; - pDHCPMSG->OPT[k++] = domainName; - pDHCPMSG->OPT[k++] = dhcpT1value; - pDHCPMSG->OPT[k++] = dhcpT2value; - pDHCPMSG->OPT[k++] = endOption; - - for (i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0; - - // send broadcasting packet - ip[0] = 255; - ip[1] = 255; - ip[2] = 255; - ip[3] = 255; - -#ifdef _DHCP_DEBUG_ - printf("> Send DHCP_DISCOVER\r\n"); -#endif - - sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT); -} - -/* SEND DHCP REQUEST */ -void send_DHCP_REQUEST(void) -{ - int i; - uint8_t ip[4]; - uint16_t k = 0; - - makeDHCPMSG(); - - if(dhcp_state == STATE_DHCP_LEASED || dhcp_state == STATE_DHCP_REREQUEST) - { - *((uint8_t*)(&pDHCPMSG->flags)) = ((DHCP_FLAGSUNICAST & 0xFF00)>> 8); - *((uint8_t*)(&pDHCPMSG->flags)+1) = (DHCP_FLAGSUNICAST & 0x00FF); - pDHCPMSG->ciaddr[0] = DHCP_allocated_ip[0]; - pDHCPMSG->ciaddr[1] = DHCP_allocated_ip[1]; - pDHCPMSG->ciaddr[2] = DHCP_allocated_ip[2]; - pDHCPMSG->ciaddr[3] = DHCP_allocated_ip[3]; - ip[0] = DHCP_SIP[0]; - ip[1] = DHCP_SIP[1]; - ip[2] = DHCP_SIP[2]; - ip[3] = DHCP_SIP[3]; - } - else - { - ip[0] = 255; - ip[1] = 255; - ip[2] = 255; - ip[3] = 255; - } - - k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG() - - // Option Request Param. - pDHCPMSG->OPT[k++] = dhcpMessageType; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_REQUEST; - - pDHCPMSG->OPT[k++] = dhcpClientIdentifier; - pDHCPMSG->OPT[k++] = 0x07; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[0]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[1]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[2]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[3]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[4]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[5]; - - if(ip[3] == 255) // if(dchp_state == STATE_DHCP_LEASED || dchp_state == DHCP_REREQUEST_STATE) - { - pDHCPMSG->OPT[k++] = dhcpRequestedIPaddr; - pDHCPMSG->OPT[k++] = 0x04; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[0]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[1]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[2]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[3]; - - pDHCPMSG->OPT[k++] = dhcpServerIdentifier; - pDHCPMSG->OPT[k++] = 0x04; - pDHCPMSG->OPT[k++] = DHCP_SIP[0]; - pDHCPMSG->OPT[k++] = DHCP_SIP[1]; - pDHCPMSG->OPT[k++] = DHCP_SIP[2]; - pDHCPMSG->OPT[k++] = DHCP_SIP[3]; - } - - // host name - pDHCPMSG->OPT[k++] = hostName; - pDHCPMSG->OPT[k++] = 0; // length of hostname - for(i = 0 ; HOST_NAME[i] != 0; i++) - pDHCPMSG->OPT[k++] = HOST_NAME[i]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[3]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[4]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[5]; - pDHCPMSG->OPT[k - (i+3+1)] = i+3; // length of hostname - - pDHCPMSG->OPT[k++] = dhcpParamRequest; - pDHCPMSG->OPT[k++] = 0x08; - pDHCPMSG->OPT[k++] = subnetMask; - pDHCPMSG->OPT[k++] = routersOnSubnet; - pDHCPMSG->OPT[k++] = dns; - pDHCPMSG->OPT[k++] = domainName; - pDHCPMSG->OPT[k++] = dhcpT1value; - pDHCPMSG->OPT[k++] = dhcpT2value; - pDHCPMSG->OPT[k++] = performRouterDiscovery; - pDHCPMSG->OPT[k++] = staticRoute; - pDHCPMSG->OPT[k++] = endOption; - - for (i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0; - -#ifdef _DHCP_DEBUG_ - printf("> Send DHCP_REQUEST\r\n"); -#endif - - sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT); - -} - -/* SEND DHCP DHCPDECLINE */ -void send_DHCP_DECLINE(void) -{ - int i; - uint8_t ip[4]; - uint16_t k = 0; - - makeDHCPMSG(); - - k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG() - - *((uint8_t*)(&pDHCPMSG->flags)) = ((DHCP_FLAGSUNICAST & 0xFF00)>> 8); - *((uint8_t*)(&pDHCPMSG->flags)+1) = (DHCP_FLAGSUNICAST & 0x00FF); - - // Option Request Param. - pDHCPMSG->OPT[k++] = dhcpMessageType; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_DECLINE; - - pDHCPMSG->OPT[k++] = dhcpClientIdentifier; - pDHCPMSG->OPT[k++] = 0x07; - pDHCPMSG->OPT[k++] = 0x01; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[0]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[1]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[2]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[3]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[4]; - pDHCPMSG->OPT[k++] = DHCP_CHADDR[5]; - - pDHCPMSG->OPT[k++] = dhcpRequestedIPaddr; - pDHCPMSG->OPT[k++] = 0x04; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[0]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[1]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[2]; - pDHCPMSG->OPT[k++] = DHCP_allocated_ip[3]; - - pDHCPMSG->OPT[k++] = dhcpServerIdentifier; - pDHCPMSG->OPT[k++] = 0x04; - pDHCPMSG->OPT[k++] = DHCP_SIP[0]; - pDHCPMSG->OPT[k++] = DHCP_SIP[1]; - pDHCPMSG->OPT[k++] = DHCP_SIP[2]; - pDHCPMSG->OPT[k++] = DHCP_SIP[3]; - - pDHCPMSG->OPT[k++] = endOption; - - for (i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0; - - //send broadcasting packet - ip[0] = 0xFF; - ip[1] = 0xFF; - ip[2] = 0xFF; - ip[3] = 0xFF; - -#ifdef _DHCP_DEBUG_ - printf("\r\n> Send DHCP_DECLINE\r\n"); -#endif - - sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT); -} - -/* PARSE REPLY pDHCPMSG */ -int8_t parseDHCPMSG(void) -{ - uint8_t svr_addr[6]; - uint16_t svr_port; - uint16_t len; - - uint8_t * p; - uint8_t * e; - uint8_t type; - uint8_t opt_len; - - if((len = getSn_RX_RSR(DHCP_SOCKET)) > 0) - { - len = recvfrom(DHCP_SOCKET, (uint8_t *)pDHCPMSG, len, svr_addr, &svr_port); - #ifdef _DHCP_DEBUG_ - printf("DHCP message : %d.%d.%d.%d(%d) %d received. \r\n",svr_addr[0],svr_addr[1],svr_addr[2], svr_addr[3],svr_port, len); - #endif - } - else return 0; - if (svr_port == DHCP_SERVER_PORT) { - // compare mac address - if ( (pDHCPMSG->chaddr[0] != DHCP_CHADDR[0]) || (pDHCPMSG->chaddr[1] != DHCP_CHADDR[1]) || - (pDHCPMSG->chaddr[2] != DHCP_CHADDR[2]) || (pDHCPMSG->chaddr[3] != DHCP_CHADDR[3]) || - (pDHCPMSG->chaddr[4] != DHCP_CHADDR[4]) || (pDHCPMSG->chaddr[5] != DHCP_CHADDR[5]) ) - return 0; - type = 0; - p = (uint8_t *)(&pDHCPMSG->op); - p = p + 240; // 240 = sizeof(RIP_MSG) + MAGIC_COOKIE size in RIP_MSG.opt - sizeof(RIP_MSG.opt) - e = p + (len - 240); - - while ( p < e ) { - - switch ( *p ) { - - case endOption : - p = e; // for break while(p < e) - break; - case padOption : - p++; - break; - case dhcpMessageType : - p++; - p++; - type = *p++; - break; - case subnetMask : - p++; - p++; - DHCP_allocated_sn[0] = *p++; - DHCP_allocated_sn[1] = *p++; - DHCP_allocated_sn[2] = *p++; - DHCP_allocated_sn[3] = *p++; - break; - case routersOnSubnet : - p++; - opt_len = *p++; - DHCP_allocated_gw[0] = *p++; - DHCP_allocated_gw[1] = *p++; - DHCP_allocated_gw[2] = *p++; - DHCP_allocated_gw[3] = *p++; - p = p + (opt_len - 4); - break; - case dns : - p++; - opt_len = *p++; - DHCP_allocated_dns[0] = *p++; - DHCP_allocated_dns[1] = *p++; - DHCP_allocated_dns[2] = *p++; - DHCP_allocated_dns[3] = *p++; - p = p + (opt_len - 4); - break; - case dhcpIPaddrLeaseTime : - p++; - opt_len = *p++; - dhcp_lease_time = *p++; - dhcp_lease_time = (dhcp_lease_time << 8) + *p++; - dhcp_lease_time = (dhcp_lease_time << 8) + *p++; - dhcp_lease_time = (dhcp_lease_time << 8) + *p++; - #ifdef _DHCP_DEBUG_ - dhcp_lease_time = 10; - #endif - break; - case dhcpServerIdentifier : - p++; - opt_len = *p++; - DHCP_SIP[0] = *p++; - DHCP_SIP[1] = *p++; - DHCP_SIP[2] = *p++; - DHCP_SIP[3] = *p++; - break; - default : - p++; - opt_len = *p++; - p += opt_len; - break; - } // switch - } // while - } // if - return type; -} - -uint8_t DHCP_run(void) -{ - uint8_t type; - uint8_t ret; - - if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED; - - if(getSn_SR(DHCP_SOCKET) != SOCK_UDP) - socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00); - - ret = DHCP_RUNNING; - type = parseDHCPMSG(); - - switch ( dhcp_state ) { - case STATE_DHCP_INIT : - DHCP_allocated_ip[0] = 0; - DHCP_allocated_ip[1] = 0; - DHCP_allocated_ip[2] = 0; - DHCP_allocated_ip[3] = 0; - send_DHCP_DISCOVER(); - dhcp_state = STATE_DHCP_DISCOVER; - break; - case STATE_DHCP_DISCOVER : - if (type == DHCP_OFFER){ -#ifdef _DHCP_DEBUG_ - printf("> Receive DHCP_OFFER\r\n"); -#endif - DHCP_allocated_ip[0] = pDHCPMSG->yiaddr[0]; - DHCP_allocated_ip[1] = pDHCPMSG->yiaddr[1]; - DHCP_allocated_ip[2] = pDHCPMSG->yiaddr[2]; - DHCP_allocated_ip[3] = pDHCPMSG->yiaddr[3]; - - send_DHCP_REQUEST(); - dhcp_state = STATE_DHCP_REQUEST; - } else ret = check_DHCP_timeout(); - break; - - case STATE_DHCP_REQUEST : - if (type == DHCP_ACK) { - -#ifdef _DHCP_DEBUG_ - printf("> Receive DHCP_ACK\r\n"); -#endif - if (check_DHCP_leasedIP()) { - // Network info assignment from DHCP - dhcp_ip_assign(); - reset_DHCP_timeout(); - - dhcp_state = STATE_DHCP_LEASED; - } else { - // IP address conflict occurred - reset_DHCP_timeout(); - dhcp_ip_conflict(); - dhcp_state = STATE_DHCP_INIT; - } - } else if (type == DHCP_NAK) { - -#ifdef _DHCP_DEBUG_ - printf("> Receive DHCP_NACK\r\n"); -#endif - - reset_DHCP_timeout(); - - dhcp_state = STATE_DHCP_DISCOVER; - } else ret = check_DHCP_timeout(); - break; - - case STATE_DHCP_LEASED : - ret = DHCP_IP_LEASED; - if ((dhcp_lease_time != INFINITE_LEASETIME) && ((dhcp_lease_time/2) < dhcp_tick_1s)) { - -#ifdef _DHCP_DEBUG_ - printf("> Maintains the IP address \r\n"); -#endif - - type = 0; - OLD_allocated_ip[0] = DHCP_allocated_ip[0]; - OLD_allocated_ip[1] = DHCP_allocated_ip[1]; - OLD_allocated_ip[2] = DHCP_allocated_ip[2]; - OLD_allocated_ip[3] = DHCP_allocated_ip[3]; - - DHCP_XID++; - - send_DHCP_REQUEST(); - - reset_DHCP_timeout(); - - dhcp_state = STATE_DHCP_REREQUEST; - } - break; - - case STATE_DHCP_REREQUEST : - ret = DHCP_IP_LEASED; - if (type == DHCP_ACK) { - dhcp_retry_count = 0; - if (OLD_allocated_ip[0] != DHCP_allocated_ip[0] || - OLD_allocated_ip[1] != DHCP_allocated_ip[1] || - OLD_allocated_ip[2] != DHCP_allocated_ip[2] || - OLD_allocated_ip[3] != DHCP_allocated_ip[3]) - { - ret = DHCP_IP_CHANGED; - dhcp_ip_update(); - #ifdef _DHCP_DEBUG_ - printf(">IP changed.\r\n"); - #endif - - } - #ifdef _DHCP_DEBUG_ - else printf(">IP is continued.\r\n"); - #endif - reset_DHCP_timeout(); - dhcp_state = STATE_DHCP_LEASED; - } else if (type == DHCP_NAK) { - -#ifdef _DHCP_DEBUG_ - printf("> Receive DHCP_NACK, Failed to maintain ip\r\n"); -#endif - - reset_DHCP_timeout(); - - dhcp_state = STATE_DHCP_DISCOVER; - } else ret = check_DHCP_timeout(); - break; - default : - break; - } - - return ret; -} - -void DHCP_stop(void) -{ - close(DHCP_SOCKET); - dhcp_state = STATE_DHCP_STOP; -} - -uint8_t check_DHCP_timeout(void) -{ - uint8_t ret = DHCP_RUNNING; - - if (dhcp_retry_count < MAX_DHCP_RETRY) { - if (dhcp_tick_next < dhcp_tick_1s) { - - switch ( dhcp_state ) { - case STATE_DHCP_DISCOVER : -// printf("<> state : STATE_DHCP_DISCOVER\r\n"); - send_DHCP_DISCOVER(); - break; - - case STATE_DHCP_REQUEST : -// printf("<> state : STATE_DHCP_REQUEST\r\n"); - - send_DHCP_REQUEST(); - break; - - case STATE_DHCP_REREQUEST : -// printf("<> state : STATE_DHCP_REREQUEST\r\n"); - - send_DHCP_REQUEST(); - break; - - default : - break; - } - - dhcp_tick_1s = 0; - dhcp_tick_next = dhcp_tick_1s + DHCP_WAIT_TIME; - dhcp_retry_count++; - } - } else { // timeout occurred - - switch(dhcp_state) { - case STATE_DHCP_DISCOVER: - dhcp_state = STATE_DHCP_INIT; - ret = DHCP_FAILED; - break; - case STATE_DHCP_REQUEST: - case STATE_DHCP_REREQUEST: - send_DHCP_DISCOVER(); - dhcp_state = STATE_DHCP_DISCOVER; - break; - default : - break; - } - reset_DHCP_timeout(); - } - return ret; -} - -int8_t check_DHCP_leasedIP(void) -{ - uint8_t tmp; - int32_t ret; - - //WIZchip RCR value changed for ARP Timeout count control - tmp = getRCR(); - setRCR(0x03); - - // IP conflict detection : ARP request - ARP reply - // Broadcasting ARP Request for check the IP conflict using UDP sendto() function - ret = sendto(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000); - - // RCR value restore - setRCR(tmp); - - if(ret == SOCKERR_TIMEOUT) { - // UDP send Timeout occurred : allocated IP address is unique, DHCP Success - -#ifdef _DHCP_DEBUG_ - printf("\r\n> Check leased IP - OK\r\n"); -#endif - - return 1; - } else { - // Received ARP reply or etc : IP address conflict occur, DHCP Failed - send_DHCP_DECLINE(); - - ret = dhcp_tick_1s; - while((dhcp_tick_1s - ret) < 2) ; // wait for 1s over; wait to complete to send DECLINE message; - - return 0; - } -} - -void DHCP_init(uint8_t s, uint8_t * buf) -{ - uint8_t zeroip[4] = {0,0,0,0}; - getSHAR(DHCP_CHADDR); - if((DHCP_CHADDR[0] | DHCP_CHADDR[1] | DHCP_CHADDR[2] | DHCP_CHADDR[3] | DHCP_CHADDR[4] | DHCP_CHADDR[5]) == 0x00) - { - // assign temporary mac address, you should be set SHAR before call this function. - DHCP_CHADDR[0] = 0x00; - DHCP_CHADDR[1] = 0x08; - DHCP_CHADDR[2] = 0xdc; - DHCP_CHADDR[3] = 0x00; - DHCP_CHADDR[4] = 0x00; - DHCP_CHADDR[5] = 0x00; - setSHAR(DHCP_CHADDR); - } - - DHCP_SOCKET = s; // SOCK_DHCP - pDHCPMSG = (RIP_MSG*)buf; - DHCP_XID = 0x12345678; - - // WIZchip Netinfo Clear - setSIPR(zeroip); - setSIPR(zeroip); - setGAR(zeroip); - - reset_DHCP_timeout(); - dhcp_state = STATE_DHCP_INIT; -} - - -/* Rset the DHCP timeout count and retry count. */ -void reset_DHCP_timeout(void) -{ - dhcp_tick_1s = 0; - dhcp_tick_next = DHCP_WAIT_TIME; - dhcp_retry_count = 0; -} - -void DHCP_time_handler(void) -{ - dhcp_tick_1s++; -} - -void getIPfromDHCP(uint8_t* ip) -{ - ip[0] = DHCP_allocated_ip[0]; - ip[1] = DHCP_allocated_ip[1]; - ip[2] = DHCP_allocated_ip[2]; - ip[3] = DHCP_allocated_ip[3]; -} - -void getGWfromDHCP(uint8_t* ip) -{ - ip[0] =DHCP_allocated_gw[0]; - ip[1] =DHCP_allocated_gw[1]; - ip[2] =DHCP_allocated_gw[2]; - ip[3] =DHCP_allocated_gw[3]; -} - -void getSNfromDHCP(uint8_t* ip) -{ - ip[0] = DHCP_allocated_sn[0]; - ip[1] = DHCP_allocated_sn[1]; - ip[2] = DHCP_allocated_sn[2]; - ip[3] = DHCP_allocated_sn[3]; -} - -void getDNSfromDHCP(uint8_t* ip) -{ - ip[0] = DHCP_allocated_dns[0]; - ip[1] = DHCP_allocated_dns[1]; - ip[2] = DHCP_allocated_dns[2]; - ip[3] = DHCP_allocated_dns[3]; -} - -uint32_t getDHCPLeasetime(void) -{ - return dhcp_lease_time; -} - - - - diff --git a/drivers/wiznet5k/internet/dhcp/dhcp.h b/drivers/wiznet5k/internet/dhcp/dhcp.h deleted file mode 100644 index ee154d506ac37..0000000000000 --- a/drivers/wiznet5k/internet/dhcp/dhcp.h +++ /dev/null @@ -1,150 +0,0 @@ -//***************************************************************************** -// -//! \file dhcp.h -//! \brief DHCP APIs Header file. -//! \details Processig DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE. -//! \version 1.1.0 -//! \date 2013/11/18 -//! \par Revision history -//! <2013/11/18> 1st Release -//! <2012/12/20> V1.1.0 -//! 1. Move unreferenced DEFINE to dhcp.c -//! <2012/12/26> V1.1.1 -//! \author Eric Jung & MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** -#ifndef _DHCP_H_ -#define _DHCP_H_ - -/* - * @brief - * @details If you want to display debug & processing message, Define _DHCP_DEBUG_ - * @note If defined, it depends on - */ - -//#define _DHCP_DEBUG_ - -/* Retry to processing DHCP */ -#define MAX_DHCP_RETRY 2 ///< Maximum retry count -#define DHCP_WAIT_TIME 10 ///< Wait Time 10s - -/* UDP port numbers for DHCP */ -#define DHCP_SERVER_PORT 67 ///< DHCP server port number -#define DHCP_CLIENT_PORT 68 ///< DHCP client port number - -#define MAGIC_COOKIE 0x63825363 ///< Any number. You can be modified it any number - -#define DCHP_HOST_NAME "WIZnet\0" - -/* - * @brief return value of @ref DHCP_run() - */ -enum -{ - DHCP_FAILED = 0, ///< Processing Fail - DHCP_RUNNING, ///< Processing DHCP protocol - DHCP_IP_ASSIGN, ///< First Occupy IP from DHPC server (if cbfunc == null, act as default default_ip_assign) - DHCP_IP_CHANGED, ///< Change IP address by new IP address from DHCP (if cbfunc == null, act as default default_ip_update) - DHCP_IP_LEASED, ///< Stand by - DHCP_STOPPED ///< Stop processing DHCP protocol -}; - -/* - * @brief DHCP client initialization (outside of the main loop) - * @param s - socket number - * @param buf - buffer for processing DHCP message - */ -void DHCP_init(uint8_t s, uint8_t * buf); - -/* - * @brief DHCP 1s Tick Timer handler - * @note SHOULD BE register to your system 1s Tick timer handler - */ -void DHCP_time_handler(void); - -/* - * @brief Register call back function - * @param ip_assign - callback func when IP is assigned from DHCP server first - * @param ip_update - callback func when IP is changed - * @prarm ip_conflict - callback func when the assigned IP is conflict with others. - */ -void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void)); - -/* - * @brief DHCP client in the main loop - * @return The value is as the follow \n - * @ref DHCP_FAILED \n - * @ref DHCP_RUNNING \n - * @ref DHCP_IP_ASSIGN \n - * @ref DHCP_IP_CHANGED \n - * @ref DHCP_IP_LEASED \n - * @ref DHCP_STOPPED \n - * - * @note This function is always called by you main task. - */ -uint8_t DHCP_run(void); - -/* - * @brief Stop DHCP processing - * @note If you want to restart. call DHCP_init() and DHCP_run() - */ -void DHCP_stop(void); - -/* Get Network information assigned from DHCP server */ -/* - * @brief Get IP address - * @param ip - IP address to be returned - */ -void getIPfromDHCP(uint8_t* ip); -/* - * @brief Get Gateway address - * @param ip - Gateway address to be returned - */ -void getGWfromDHCP(uint8_t* ip); -/* - * @brief Get Subnet mask value - * @param ip - Subnet mask to be returned - */ -void getSNfromDHCP(uint8_t* ip); -/* - * @brief Get DNS address - * @param ip - DNS address to be returned - */ -void getDNSfromDHCP(uint8_t* ip); - -/* - * @brief Get the leased time by DHCP sever - * @return unit 1s - */ -uint32_t getDHCPLeasetime(void); - -#endif /* _DHCP_H_ */ diff --git a/drivers/wiznet5k/internet/dns/dns.c b/drivers/wiznet5k/internet/dns/dns.c deleted file mode 100644 index c0ad570c00970..0000000000000 --- a/drivers/wiznet5k/internet/dns/dns.c +++ /dev/null @@ -1,566 +0,0 @@ -//***************************************************************************** -// -//! \file dns.c -//! \brief DNS APIs Implement file. -//! \details Send DNS query & Receive DNS reponse. \n -//! It depends on stdlib.h & string.h in ansi-c library -//! \version 1.1.0 -//! \date 2013/11/18 -//! \par Revision history -//! <2013/10/21> 1st Release -//! <2013/12/20> V1.1.0 -//! 1. Remove secondary DNS server in DNS_run -//! If 1st DNS_run failed, call DNS_run with 2nd DNS again -//! 2. DNS_timerHandler -> DNS_time_handler -//! 3. Remove the unused define -//! 4. Integrated dns.h dns.c & dns_parse.h dns_parse.c into dns.h & dns.c -//! <2013/12/20> V1.1.0 -//! -//! \author Eric Jung & MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#include -#include - -//#include "Ethernet/socket.h" -//#include "Internet/DNS/dns.h" -#include "../../ethernet/socket.h" -#include "dns.h" - -#ifdef _DNS_DEBUG_ - #include -#endif - -#define INITRTT 2000L /* Initial smoothed response time */ -#define MAXCNAME (MAX_DOMAIN_NAME + (MAX_DOMAIN_NAME>>1)) /* Maximum amount of cname recursion */ - -#define TYPE_A 1 /* Host address */ -#define TYPE_NS 2 /* Name server */ -#define TYPE_MD 3 /* Mail destination (obsolete) */ -#define TYPE_MF 4 /* Mail forwarder (obsolete) */ -#define TYPE_CNAME 5 /* Canonical name */ -#define TYPE_SOA 6 /* Start of Authority */ -#define TYPE_MB 7 /* Mailbox name (experimental) */ -#define TYPE_MG 8 /* Mail group member (experimental) */ -#define TYPE_MR 9 /* Mail rename name (experimental) */ -#define TYPE_NULL 10 /* Null (experimental) */ -#define TYPE_WKS 11 /* Well-known sockets */ -#define TYPE_PTR 12 /* Pointer record */ -#define TYPE_HINFO 13 /* Host information */ -#define TYPE_MINFO 14 /* Mailbox information (experimental)*/ -#define TYPE_MX 15 /* Mail exchanger */ -#define TYPE_TXT 16 /* Text strings */ -#define TYPE_ANY 255 /* Matches any type */ - -#define CLASS_IN 1 /* The ARPA Internet */ - -/* Round trip timing parameters */ -#define AGAIN 8 /* Average RTT gain = 1/8 */ -#define LAGAIN 3 /* Log2(AGAIN) */ -#define DGAIN 4 /* Mean deviation gain = 1/4 */ -#define LDGAIN 2 /* log2(DGAIN) */ - -/* Header for all domain messages */ -struct dhdr -{ - uint16_t id; /* Identification */ - uint8_t qr; /* Query/Response */ -#define QUERY 0 -#define RESPONSE 1 - uint8_t opcode; -#define IQUERY 1 - uint8_t aa; /* Authoratative answer */ - uint8_t tc; /* Truncation */ - uint8_t rd; /* Recursion desired */ - uint8_t ra; /* Recursion available */ - uint8_t rcode; /* Response code */ -#define NO_ERROR 0 -#define FORMAT_ERROR 1 -#define SERVER_FAIL 2 -#define NAME_ERROR 3 -#define NOT_IMPL 4 -#define REFUSED 5 - uint16_t qdcount; /* Question count */ - uint16_t ancount; /* Answer count */ - uint16_t nscount; /* Authority (name server) count */ - uint16_t arcount; /* Additional record count */ -}; - - -uint8_t* pDNSMSG; // DNS message buffer -uint8_t DNS_SOCKET; // SOCKET number for DNS -uint16_t DNS_MSGID; // DNS message ID - -extern uint32_t HAL_GetTick(void); -uint32_t hal_sys_tick; - -/* converts uint16_t from network buffer to a host byte order integer. */ -uint16_t get16(uint8_t * s) -{ - uint16_t i; - i = *s++ << 8; - i = i + *s; - return i; -} - -/* copies uint16_t to the network buffer with network byte order. */ -uint8_t * put16(uint8_t * s, uint16_t i) -{ - *s++ = i >> 8; - *s++ = i; - return s; -} - - -/* - * CONVERT A DOMAIN NAME TO THE HUMAN-READABLE FORM - * - * Description : This function converts a compressed domain name to the human-readable form - * Arguments : msg - is a pointer to the reply message - * compressed - is a pointer to the domain name in reply message. - * buf - is a pointer to the buffer for the human-readable form name. - * len - is the MAX. size of buffer. - * Returns : the length of compressed message - */ -int parse_name(uint8_t * msg, uint8_t * compressed, char * buf, int16_t len) -{ - uint16_t slen; /* Length of current segment */ - uint8_t * cp; - int clen = 0; /* Total length of compressed name */ - int indirect = 0; /* Set if indirection encountered */ - int nseg = 0; /* Total number of segments in name */ - - cp = compressed; - - for (;;) - { - slen = *cp++; /* Length of this segment */ - - if (!indirect) clen++; - - if ((slen & 0xc0) == 0xc0) - { - if (!indirect) - clen++; - indirect = 1; - /* Follow indirection */ - cp = &msg[((slen & 0x3f)<<8) + *cp]; - slen = *cp++; - } - - if (slen == 0) /* zero length == all done */ - break; - - len -= slen + 1; - - if (len < 0) return -1; - - if (!indirect) clen += slen; - - while (slen-- != 0) *buf++ = (char)*cp++; - *buf++ = '.'; - nseg++; - } - - if (nseg == 0) - { - /* Root name; represent as single dot */ - *buf++ = '.'; - len--; - } - - *buf++ = '\0'; - len--; - - return clen; /* Length of compressed message */ -} - -/* - * PARSE QUESTION SECTION - * - * Description : This function parses the question record of the reply message. - * Arguments : msg - is a pointer to the reply message - * cp - is a pointer to the question record. - * Returns : a pointer the to next record. - */ -uint8_t * dns_question(uint8_t * msg, uint8_t * cp) -{ - int len; - char name[MAXCNAME]; - - len = parse_name(msg, cp, name, MAXCNAME); - - - if (len == -1) return 0; - - cp += len; - cp += 2; /* type */ - cp += 2; /* class */ - - return cp; -} - - -/* - * PARSE ANSER SECTION - * - * Description : This function parses the answer record of the reply message. - * Arguments : msg - is a pointer to the reply message - * cp - is a pointer to the answer record. - * Returns : a pointer the to next record. - */ -uint8_t * dns_answer(uint8_t * msg, uint8_t * cp, uint8_t * ip_from_dns) -{ - int len, type; - char name[MAXCNAME]; - - len = parse_name(msg, cp, name, MAXCNAME); - - if (len == -1) return 0; - - cp += len; - type = get16(cp); - cp += 2; /* type */ - cp += 2; /* class */ - cp += 4; /* ttl */ - cp += 2; /* len */ - - - switch (type) - { - case TYPE_A: - /* Just read the address directly into the structure */ - ip_from_dns[0] = *cp++; - ip_from_dns[1] = *cp++; - ip_from_dns[2] = *cp++; - ip_from_dns[3] = *cp++; - break; - case TYPE_CNAME: - case TYPE_MB: - case TYPE_MG: - case TYPE_MR: - case TYPE_NS: - case TYPE_PTR: - /* These types all consist of a single domain name */ - /* convert it to ASCII format */ - len = parse_name(msg, cp, name, MAXCNAME); - if (len == -1) return 0; - - cp += len; - break; - case TYPE_HINFO: - len = *cp++; - cp += len; - - len = *cp++; - cp += len; - break; - case TYPE_MX: - cp += 2; - /* Get domain name of exchanger */ - len = parse_name(msg, cp, name, MAXCNAME); - if (len == -1) return 0; - - cp += len; - break; - case TYPE_SOA: - /* Get domain name of name server */ - len = parse_name(msg, cp, name, MAXCNAME); - if (len == -1) return 0; - - cp += len; - - /* Get domain name of responsible person */ - len = parse_name(msg, cp, name, MAXCNAME); - if (len == -1) return 0; - - cp += len; - - cp += 4; - cp += 4; - cp += 4; - cp += 4; - cp += 4; - break; - case TYPE_TXT: - /* Just stash */ - break; - default: - /* Ignore */ - break; - } - - return cp; -} - -/* - * PARSE THE DNS REPLY - * - * Description : This function parses the reply message from DNS server. - * Arguments : dhdr - is a pointer to the header for DNS message - * buf - is a pointer to the reply message. - * len - is the size of reply message. - * Returns : -1 - Domain name length is too big - * 0 - Fail (Timeout or parse error) - * 1 - Success, - */ -int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns) -{ - uint16_t tmp; - uint16_t i; - uint8_t * msg; - uint8_t * cp; - - msg = pbuf; - memset(pdhdr, 0, sizeof(*pdhdr)); - - pdhdr->id = get16(&msg[0]); - tmp = get16(&msg[2]); - if (tmp & 0x8000) pdhdr->qr = 1; - - pdhdr->opcode = (tmp >> 11) & 0xf; - - if (tmp & 0x0400) pdhdr->aa = 1; - if (tmp & 0x0200) pdhdr->tc = 1; - if (tmp & 0x0100) pdhdr->rd = 1; - if (tmp & 0x0080) pdhdr->ra = 1; - - pdhdr->rcode = tmp & 0xf; - pdhdr->qdcount = get16(&msg[4]); - pdhdr->ancount = get16(&msg[6]); - pdhdr->nscount = get16(&msg[8]); - pdhdr->arcount = get16(&msg[10]); - - - /* Now parse the variable length sections */ - cp = &msg[12]; - - /* Question section */ - for (i = 0; i < pdhdr->qdcount; i++) - { - cp = dns_question(msg, cp); - if(!cp) - { -#ifdef _DNS_DEBUG_ - printf("MAX_DOMAIN_NAME is too small, it should be redefined in dns.h\r\n"); -#endif - return -1; - } - } - - /* Answer section */ - for (i = 0; i < pdhdr->ancount; i++) - { - cp = dns_answer(msg, cp, ip_from_dns); - if(!cp) - { -#ifdef _DNS_DEBUG_ - printf("MAX_DOMAIN_NAME is too small, it should be redefined in dns.h\r\n"); -#endif - return -1; - } - - } - - /* Name server (authority) section */ - for (i = 0; i < pdhdr->nscount; i++) - { - ; - } - - /* Additional section */ - for (i = 0; i < pdhdr->arcount; i++) - { - ; - } - - if(pdhdr->rcode == 0) return 1; // No error - else return 0; -} - - -/* - * MAKE DNS QUERY MESSAGE - * - * Description : This function makes DNS query message. - * Arguments : op - Recursion desired - * name - is a pointer to the domain name. - * buf - is a pointer to the buffer for DNS message. - * len - is the MAX. size of buffer. - * Returns : the pointer to the DNS message. - */ -int16_t dns_makequery(uint16_t op, char * name, uint8_t * buf, uint16_t len) -{ - uint8_t *cp; - char *cp1; - char sname[MAXCNAME]; - char *dname; - uint16_t p; - uint16_t dlen; - - cp = buf; - - DNS_MSGID++; - cp = put16(cp, DNS_MSGID); - p = (op << 11) | 0x0100; /* Recursion desired */ - cp = put16(cp, p); - cp = put16(cp, 1); - cp = put16(cp, 0); - cp = put16(cp, 0); - cp = put16(cp, 0); - - strcpy(sname, name); - dname = sname; - dlen = strlen(dname); - for (;;) - { - /* Look for next dot */ - cp1 = strchr(dname, '.'); - - if (cp1 != NULL) len = cp1 - dname; /* More to come */ - else len = dlen; /* Last component */ - - *cp++ = len; /* Write length of component */ - if (len == 0) break; - - /* Copy component up to (but not including) dot */ - memcpy(cp, dname, len); - cp += len; - if (cp1 == NULL) - { - *cp++ = 0; /* Last one; write null and finish */ - break; - } - dname += len+1; - dlen -= len+1; - } - - cp = put16(cp, 0x0001); /* type */ - cp = put16(cp, 0x0001); /* class */ - - return ((int16_t)((uint32_t)(cp) - (uint32_t)(buf))); -} - -/* - * CHECK DNS TIMEOUT - * - * Description : This function check the DNS timeout - * Arguments : None. - * Returns : -1 - timeout occurred, 0 - timer over, but no timeout, 1 - no timer over, no timeout occur - * Note : timeout : retry count and timer both over. - */ - -int8_t check_DNS_timeout(void) -{ - static uint8_t retry_count; - - uint32_t tick = HAL_GetTick(); - if(tick - hal_sys_tick >= DNS_WAIT_TIME * 1000) - { - hal_sys_tick = tick; - if(retry_count >= MAX_DNS_RETRY) { - retry_count = 0; - return -1; // timeout occurred - } - retry_count++; - return 0; // timer over, but no timeout - } - - return 1; // no timer over, no timeout occur -} - - - -/* DNS CLIENT INIT */ -void DNS_init(uint8_t s, uint8_t * buf) -{ - DNS_SOCKET = s; // SOCK_DNS - pDNSMSG = buf; // User's shared buffer - DNS_MSGID = DNS_MSG_ID; -} - -/* DNS CLIENT RUN */ -int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns) -{ - int8_t ret; - struct dhdr dhp; - uint8_t ip[4]; - uint16_t len, port; - int8_t ret_check_timeout; - - hal_sys_tick = HAL_GetTick(); - - // Socket open - WIZCHIP_EXPORT(socket)(DNS_SOCKET, Sn_MR_UDP, 0, 0); - -#ifdef _DNS_DEBUG_ - printf("> DNS Query to DNS Server : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]); -#endif - - len = dns_makequery(0, (char *)name, pDNSMSG, MAX_DNS_BUF_SIZE); - WIZCHIP_EXPORT(sendto)(DNS_SOCKET, pDNSMSG, len, dns_ip, IPPORT_DOMAIN); - - while (1) - { - if ((len = getSn_RX_RSR(DNS_SOCKET)) > 0) - { - if (len > MAX_DNS_BUF_SIZE) len = MAX_DNS_BUF_SIZE; - len = WIZCHIP_EXPORT(recvfrom)(DNS_SOCKET, pDNSMSG, len, ip, &port); - #ifdef _DNS_DEBUG_ - printf("> Receive DNS message from %d.%d.%d.%d(%d). len = %d\r\n", ip[0], ip[1], ip[2], ip[3],port,len); - #endif - ret = parseDNSMSG(&dhp, pDNSMSG, ip_from_dns); - break; - } - // Check Timeout - ret_check_timeout = check_DNS_timeout(); - if (ret_check_timeout < 0) { - -#ifdef _DNS_DEBUG_ - printf("> DNS Server is not responding : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]); -#endif - return 0; // timeout occurred - } - else if (ret_check_timeout == 0) { - -#ifdef _DNS_DEBUG_ - printf("> DNS Timeout\r\n"); -#endif - WIZCHIP_EXPORT(sendto)(DNS_SOCKET, pDNSMSG, len, dns_ip, IPPORT_DOMAIN); - } - } - WIZCHIP_EXPORT(close)(DNS_SOCKET); - // Return value - // 0 > : failed / 1 - success - return ret; -} diff --git a/drivers/wiznet5k/internet/dns/dns.h b/drivers/wiznet5k/internet/dns/dns.h deleted file mode 100644 index de0039515e28f..0000000000000 --- a/drivers/wiznet5k/internet/dns/dns.h +++ /dev/null @@ -1,96 +0,0 @@ -//***************************************************************************** -// -//! \file dns.h -//! \brief DNS APIs Header file. -//! \details Send DNS query & Receive DNS reponse. -//! \version 1.1.0 -//! \date 2013/11/18 -//! \par Revision history -//! <2013/10/21> 1st Release -//! <2013/12/20> V1.1.0 -//! 1. Remove secondary DNS server in DNS_run -//! If 1st DNS_run failed, call DNS_run with 2nd DNS again -//! 2. DNS_timerHandler -> DNS_time_handler -//! 3. Move the no reference define to dns.c -//! 4. Integrated dns.h dns.c & dns_parse.h dns_parse.c into dns.h & dns.c -//! <2013/12/20> V1.1.0 -//! -//! \author Eric Jung & MidnightCow -//! \copyright -//! -//! Copyright (c) 2013, WIZnet Co., LTD. -//! All rights reserved. -//! -//! Redistribution and use in source and binary forms, with or without -//! modification, are permitted provided that the following conditions -//! are met: -//! -//! * Redistributions of source code must retain the above copyright -//! notice, this list of conditions and the following disclaimer. -//! * 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. -//! * Neither the name of the nor the names of its -//! contributors may be used to endorse or promote products derived -//! from this software without specific prior written permission. -//! -//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. -// -//***************************************************************************** - -#ifndef _DNS_H_ -#define _DNS_H_ - -#include -/* - * @brief Define it for Debug & Monitor DNS processing. - * @note If defined, it depends on - */ - -//#define _DNS_DEBUG_ - -#define MAX_DNS_BUF_SIZE 256 ///< maximum size of DNS buffer. */ -/* - * @brief Maximum length of your queried Domain name - * @todo SHOULD BE defined it equal as or greater than your Domain name length + null character(1) - * @note SHOULD BE careful to stack overflow because it is allocated 1.5 times as MAX_DOMAIN_NAME in stack. - */ -#define MAX_DOMAIN_NAME 32 // for example "www.google.com" - -#define MAX_DNS_RETRY 2 ///< Requery Count -#define DNS_WAIT_TIME 4 ///< Wait response time. unit 1s. - -#define IPPORT_DOMAIN 53 ///< DNS server port number - -#define DNS_MSG_ID 0x1122 ///< ID for DNS message. You can be modified it any number -/* - * @brief DNS process initialize - * @param s : Socket number for DNS - * @param buf : Buffer for DNS message - */ -void DNS_init(uint8_t s, uint8_t * buf); - -/* - * @brief DNS process - * @details Send DNS query and receive DNS response - * @param dns_ip : DNS server ip address - * @param name : Domain name to be queried - * @param ip_from_dns : IP address from DNS server - * @return -1 : failed. @ref MAX_DOMIN_NAME is too small \n - * 0 : failed (Timeout or Parse error)\n - * 1 : success - * @note This function blocks until success or fail. max time = @ref MAX_DNS_RETRY * @ref DNS_WAIT_TIME - */ -int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns); - -#endif /* _DNS_H_ */ From 8308f9c977adf322b264e5fbec6dd84315231eb4 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 20 Aug 2022 11:25:34 +0200 Subject: [PATCH 2509/3533] extmod/network_wiznet5k: Use the configured DNS address if available. Instead of the default 8.8.8.8. The change was suggested by @omogenot. --- extmod/network_wiznet5k.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 89c933c2d4545..95c852d7f7299 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -397,6 +397,9 @@ STATIC int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t dns_ip[MOD_NETWORK_IPADDR_BUF_SIZE] = {8, 8, 8, 8}; uint8_t *buf = m_new(uint8_t, MAX_DNS_BUF_SIZE); DNS_init(2, buf); + if (wiznet5k_obj.netinfo.dns[0]) { + memcpy(dns_ip, wiznet5k_obj.netinfo.dns, MOD_NETWORK_IPADDR_BUF_SIZE); + } mp_int_t ret = DNS_run(dns_ip, (uint8_t *)name, out_ip); m_del(uint8_t, buf, MAX_DNS_BUF_SIZE); if (ret == 1) { From 47c45d0e7fda10f4a62b7017d48ae452203b22f0 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 22 Aug 2022 08:09:02 +0200 Subject: [PATCH 2510/3533] rp2/machine_wdt: Check for the maximum timeout value of watchdog. The value will be checked for timeout <= 8388. Notes were added to the documentation. --- docs/library/machine.WDT.rst | 5 +++-- docs/rp2/quickref.rst | 1 + ports/rp2/machine_wdt.c | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/library/machine.WDT.rst b/docs/library/machine.WDT.rst index 8c81e10ea5817..95893cec36be7 100644 --- a/docs/library/machine.WDT.rst +++ b/docs/library/machine.WDT.rst @@ -15,7 +15,7 @@ Example usage:: wdt = WDT(timeout=2000) # enable it with a timeout of 2s wdt.feed() -Availability of this class: pyboard, WiPy, esp8266, esp32. +Availability of this class: pyboard, WiPy, esp8266, esp32, rp2040, mimxrt. Constructors ------------ @@ -26,7 +26,8 @@ Constructors Once it is running the timeout cannot be changed and the WDT cannot be stopped either. Notes: On the esp32 the minimum timeout is 1 second. On the esp8266 a timeout - cannot be specified, it is determined by the underlying system. + cannot be specified, it is determined by the underlying system. On rp2040 devices, + the maximum timeout is 8388 ms. Methods ------- diff --git a/docs/rp2/quickref.rst b/docs/rp2/quickref.rst index 4824f390ebfbe..430c130c6e865 100644 --- a/docs/rp2/quickref.rst +++ b/docs/rp2/quickref.rst @@ -296,6 +296,7 @@ See :ref:`machine.WDT `. :: wdt = WDT(timeout=5000) wdt.feed() +The maximum value for timeout is 8388 ms. OneWire driver -------------- diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c index 38e0597018d64..d6914a4f2615d 100644 --- a/ports/rp2/machine_wdt.c +++ b/ports/rp2/machine_wdt.c @@ -29,6 +29,9 @@ #include "hardware/watchdog.h" +// The maximum timeout in milliseconds is: 0xffffff / 2 / 1000 +#define WDT_TIMEOUT_MAX 8388 + typedef struct _machine_wdt_obj_t { mp_obj_base_t base; } machine_wdt_obj_t; @@ -53,7 +56,11 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, s } // Start the watchdog (timeout is in milliseconds). - watchdog_enable(args[ARG_timeout].u_int, false); + uint32_t timeout = args[ARG_timeout].u_int; + if (timeout > WDT_TIMEOUT_MAX) { + mp_raise_ValueError(MP_ERROR_TEXT("timeout exceeds " MP_STRINGIFY(WDT_TIMEOUT_MAX))); + } + watchdog_enable(timeout, false); return MP_OBJ_FROM_PTR(&machine_wdt); } From 98d1c50159fe9427d72ec358ba0219eaebb1d991 Mon Sep 17 00:00:00 2001 From: glenn20 Date: Sun, 31 Jul 2022 16:36:10 +1000 Subject: [PATCH 2511/3533] esp32/network_wlan: Use esp_wifi_set/get_channel to config wifi channel. Set the channel with esp_wifi_set_channel(), which adds support for setting the channel of the STA interface Get the channel with esp_wifi_get_channel() which returns the actual wifi channel of the radio, rather than the configured channel. --- ports/esp32/network_wlan.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 9b2200e4ff9ce..501dc40f275a4 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -448,8 +448,22 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ break; } case MP_QSTR_channel: { - req_if = WIFI_IF_AP; - cfg.ap.channel = mp_obj_get_int(kwargs->table[i].value); + uint8_t primary; + wifi_second_chan_t secondary; + // Get the current value of secondary + esp_exceptions(esp_wifi_get_channel(&primary, &secondary)); + primary = mp_obj_get_int(kwargs->table[i].value); + esp_err_t err = esp_wifi_set_channel(primary, secondary); + if (err == ESP_ERR_INVALID_ARG) { + // May need to swap secondary channel above to below or below to above + secondary = ( + (secondary == WIFI_SECOND_CHAN_ABOVE) + ? WIFI_SECOND_CHAN_BELOW + : (secondary == WIFI_SECOND_CHAN_BELOW) + ? WIFI_SECOND_CHAN_ABOVE + : WIFI_SECOND_CHAN_NONE); + esp_exceptions(esp_wifi_set_channel(primary, secondary)); + } break; } case MP_QSTR_hostname: @@ -535,10 +549,13 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ req_if = WIFI_IF_AP; val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode); break; - case MP_QSTR_channel: - req_if = WIFI_IF_AP; - val = MP_OBJ_NEW_SMALL_INT(cfg.ap.channel); + case MP_QSTR_channel: { + uint8_t channel; + wifi_second_chan_t second; + esp_exceptions(esp_wifi_get_channel(&channel, &second)); + val = MP_OBJ_NEW_SMALL_INT(channel); break; + } case MP_QSTR_hostname: case MP_QSTR_dhcp_hostname: { const char *s; From 76f2e3e62bd2bc177a62649bae34b67d9bd2c8b1 Mon Sep 17 00:00:00 2001 From: glenn20 Date: Sun, 31 Jul 2022 18:17:40 +1000 Subject: [PATCH 2512/3533] esp32/network_wlan: Add support to set/get the wifi protocol. Add 'protocol' option to WLAN.config() to support setting/getting the wifi protocol modes: MODE_11G|MODE_11G|MODE_11N. --- ports/esp32/network_wlan.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 501dc40f275a4..4f74262afc381 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -490,6 +490,10 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ esp_exceptions(esp_wifi_set_max_tx_power(power)); break; } + case MP_QSTR_protocol: { + esp_exceptions(esp_wifi_set_protocol(self->if_id, mp_obj_get_int(kwargs->table[i].value))); + break; + } default: goto unknown; } @@ -578,6 +582,12 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ val = mp_obj_new_float(power * 0.25); break; } + case MP_QSTR_protocol: { + uint8_t protocol_bitmap; + esp_exceptions(esp_wifi_get_protocol(self->if_id, &protocol_bitmap)); + val = MP_OBJ_NEW_SMALL_INT(protocol_bitmap); + break; + } default: goto unknown; } From 0507f239e812fa7ae57c1e172c53eae8fc37cd18 Mon Sep 17 00:00:00 2001 From: glenn20 Date: Sun, 31 Jul 2022 18:19:18 +1000 Subject: [PATCH 2513/3533] esp32/modnetwork: Add network.MODE_LR constant. Adds the MODE_LR constant to the network module to support Espressif's long-range communication protocol. --- ports/esp32/modnetwork.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c index 45231c42307c8..7429274c1571d 100644 --- a/ports/esp32/modnetwork.c +++ b/ports/esp32/modnetwork.c @@ -244,6 +244,7 @@ STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_MODE_11B), MP_ROM_INT(WIFI_PROTOCOL_11B) }, { MP_ROM_QSTR(MP_QSTR_MODE_11G), MP_ROM_INT(WIFI_PROTOCOL_11G) }, { MP_ROM_QSTR(MP_QSTR_MODE_11N), MP_ROM_INT(WIFI_PROTOCOL_11N) }, + { MP_ROM_QSTR(MP_QSTR_MODE_LR), MP_ROM_INT(WIFI_PROTOCOL_LR) }, { MP_ROM_QSTR(MP_QSTR_AUTH_OPEN), MP_ROM_INT(WIFI_AUTH_OPEN) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WEP), MP_ROM_INT(WIFI_AUTH_WEP) }, From e6e60f4330c176e1a9443a994ba29ea155d7aca7 Mon Sep 17 00:00:00 2001 From: glenn20 Date: Fri, 12 Aug 2022 16:53:48 +1000 Subject: [PATCH 2514/3533] esp8266/modnetwork: Add support for WLAN.config(protocol=XX) option. Following esp32. This is preferred to using the phy_mode() function. --- ports/esp8266/modnetwork.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index dbc12f74d4ae9..5240d3b320649 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -406,6 +406,10 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs } break; } + case MP_QSTR_protocol: { + wifi_set_phy_mode(mp_obj_get_int(kwargs->table[i].value)); + break; + } default: goto unknown; } @@ -473,6 +477,10 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs } break; } + case MP_QSTR_protocol: { + val = mp_obj_new_int(wifi_get_phy_mode()); + break; + } default: goto unknown; } From 13dceaa4ea4336eddb763985c2274cfa497c8e82 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 12 Jul 2022 18:25:41 +0200 Subject: [PATCH 2515/3533] esp32/machine_uart: Change sendbreak time to be at least 15 bit times. It used to be 10 bit times, which is too short. The break state must be longer than a regular character time, at least 13 bit times. This is now implemented by reducing the baudrate while sending the "0". The break time will now vary with data length and parity setting, but will at least be 15 bit times. Tested with a GENERIC_SPIRAM, GENERIC_C3 and UM_TINYS2 board. --- ports/esp32/machine_uart.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 49ce78b1b24c9..28110e39f3781 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -399,28 +399,19 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); // Save settings - uart_word_length_t word_length; - uart_parity_t parity; - uart_get_word_length(self->uart_num, &word_length); - uart_get_parity(self->uart_num, &parity); + uint32_t baudrate; + uart_get_baudrate(self->uart_num, &baudrate); - // Synthesise the break condition by either a longer word or using even parity + // Synthesise the break condition by reducing the baud rate, + // and cater for the worst case of 5 data bits, no parity. uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000)); - if (word_length != UART_DATA_8_BITS) { - uart_set_word_length(self->uart_num, UART_DATA_8_BITS); - } else if (parity == UART_PARITY_DISABLE) { - uart_set_parity(self->uart_num, UART_PARITY_EVEN); - } else { - // Cannot synthesise break - mp_raise_OSError(MP_EPERM); - } + uart_set_baudrate(self->uart_num, baudrate * 6 / 15); char buf[1] = {0}; uart_write_bytes(self->uart_num, buf, 1); uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000)); - // Restore original settings - uart_set_word_length(self->uart_num, word_length); - uart_set_parity(self->uart_num, parity); + // Restore original setting + uart_set_baudrate(self->uart_num, baudrate); return mp_const_none; } From 8139cbcf6bfad3f9b14865a5315bc83b5c5ecc7a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 17 Aug 2022 08:44:03 +0200 Subject: [PATCH 2516/3533] esp32/machine_timer: Support all init arguments in Timer constructor. Following the usual style of instantiation and init(). --- ports/esp32/machine_timer.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 8541bcbfdb486..66969b3a954f2 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -68,6 +68,7 @@ typedef struct _machine_timer_obj_t { const mp_obj_type_t machine_timer_type; STATIC void machine_timer_disable(machine_timer_obj_t *self); +STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); void machine_timer_deinit_all(void) { // Disable, deallocate and remove all timers from list @@ -94,24 +95,35 @@ STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr } STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, 1, false); + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); mp_uint_t group = (mp_obj_get_int(args[0]) >> 1) & 1; mp_uint_t index = mp_obj_get_int(args[0]) & 1; - // Check whether the timer is already initialized, if so return it + machine_timer_obj_t *self = NULL; + + // Check whether the timer is already initialized, if so use it for (machine_timer_obj_t *t = MP_STATE_PORT(machine_timer_obj_head); t; t = t->next) { if (t->group == group && t->index == index) { - return t; + self = t; + break; } } + // The timer does not exist, create it. + if (self == NULL) { + self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type); + self->group = group; + self->index = index; + + // Add the timer to the linked-list of timers + self->next = MP_STATE_PORT(machine_timer_obj_head); + MP_STATE_PORT(machine_timer_obj_head) = self; + } - machine_timer_obj_t *self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type); - self->group = group; - self->index = index; - - // Add the timer to the linked-list of timers - self->next = MP_STATE_PORT(machine_timer_obj_head); - MP_STATE_PORT(machine_timer_obj_head) = self; + if (n_args > 1 || n_kw > 0) { + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_timer_init_helper(self, n_args - 1, args + 1, &kw_args); + } return self; } From 1fbf0efaebd597207f32a910d635a94ec9f6ef69 Mon Sep 17 00:00:00 2001 From: Damiano Mazzella Date: Tue, 23 Aug 2022 16:50:24 +0200 Subject: [PATCH 2517/3533] stm32/sdram: Enable MPU for unaligned access on H7 MCUs. So that SDRAM can be used as the heap on ARDUINO_PORTENTA_H7, for example. Fixes issue #9087. --- ports/stm32/sdram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/sdram.c b/ports/stm32/sdram.c index a94b802ea49fb..fb0e5a8688d30 100644 --- a/ports/stm32/sdram.c +++ b/ports/stm32/sdram.c @@ -244,7 +244,7 @@ static void sdram_init_seq(SDRAM_HandleTypeDef #define REFRESH_COUNT (MICROPY_HW_SDRAM_REFRESH_RATE * 90000 / 8192 - 20) HAL_SDRAM_ProgramRefreshRate(hsdram, REFRESH_COUNT); - #if defined(STM32F7) + #if defined(STM32F7) || defined(STM32H7) /* Enable MPU for the SDRAM Memory Region to allow non-aligned accesses (hard-fault otherwise) Initially disable all access for the entire SDRAM memory space, From 923375380b29aca68b50b3ec685c6d83465fd63d Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 30 Mar 2022 10:50:18 +1100 Subject: [PATCH 2518/3533] stm32/boards: Increase mboot region to 32k for WB55 boards. If mboot is built with support for packing (signing/encryption) it needs up to 32KiB. So for simplicity increase the mboot region to 32KiB unconditionally for WB55 boards (custom WB55 board configurations can still provide their own linker scripts to override this). --- ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk | 2 +- ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk | 2 +- ports/stm32/boards/stm32wb55xg.ld | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk index 349ce46d79588..2e6ce1fe8f77e 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.mk @@ -6,7 +6,7 @@ STARTUP_FILE = $(STM32LIB_CMSIS_BASE)/Source/Templates/gcc/startup_stm32wb55xx_c ifeq ($(USE_MBOOT),1) # When using Mboot all the text goes together after the bootloader LD_FILES = boards/stm32wb55xg.ld boards/common_bl.ld -TEXT0_ADDR = 0x08004000 +TEXT0_ADDR = 0x08008000 else # When not using Mboot the text goes at the start of flash LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld diff --git a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk index 349ce46d79588..2e6ce1fe8f77e 100644 --- a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk +++ b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.mk @@ -6,7 +6,7 @@ STARTUP_FILE = $(STM32LIB_CMSIS_BASE)/Source/Templates/gcc/startup_stm32wb55xx_c ifeq ($(USE_MBOOT),1) # When using Mboot all the text goes together after the bootloader LD_FILES = boards/stm32wb55xg.ld boards/common_bl.ld -TEXT0_ADDR = 0x08004000 +TEXT0_ADDR = 0x08008000 else # When not using Mboot the text goes at the start of flash LD_FILES = boards/stm32wb55xg.ld boards/common_basic.ld diff --git a/ports/stm32/boards/stm32wb55xg.ld b/ports/stm32/boards/stm32wb55xg.ld index 841c32b8a0fb8..9b1e5bca147d5 100644 --- a/ports/stm32/boards/stm32wb55xg.ld +++ b/ports/stm32/boards/stm32wb55xg.ld @@ -6,7 +6,7 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* sectors 0-127 */ - FLASH_APP (rx) : ORIGIN = 0x08004000, LENGTH = 496K /* sectors 4-127 */ + FLASH_APP (rx) : ORIGIN = 0x08008000, LENGTH = 480K /* sectors 8-127 */ FLASH_FS (r) : ORIGIN = 0x08080000, LENGTH = 256K /* sectors 128-191 */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K /* SRAM1 */ RAM2A (xrw) : ORIGIN = 0x20030000, LENGTH = 10K /* SRAM2A */ From 492ba5eaf2c6e1e9fe8e31fd2e8f47fac6188673 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Aug 2022 16:20:40 +1000 Subject: [PATCH 2519/3533] stm32: Move board variant config to mpconfigboard.mk. Rather than having the autobuild know about the particular variants, have the mpconfigboard.mk describe them and make autobuild discover them automatically. Adds a "query-variants" target to stm32/Makefile to allow the set of possible variants to be queried. Removes pybv3 from the autobuild as this isn't use by the downloads page. Signed-off-by: Jim Mussared --- ports/stm32/Makefile | 3 ++ .../stm32/boards/PYBLITEV10/mpconfigboard.mk | 21 ++++++++++++ ports/stm32/boards/PYBV10/mpconfigboard.mk | 21 ++++++++++++ ports/stm32/boards/PYBV11/mpconfigboard.mk | 21 ++++++++++++ tools/autobuild/build-stm32-extra.sh | 34 +++++++------------ 5 files changed, 79 insertions(+), 21 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 7c912f4f0b23a..f981f8f3eb057 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -37,6 +37,9 @@ include $(TOP)/extmod/extmod.mk GIT_SUBMODULES += lib/libhydrogen lib/lwip lib/mbedtls lib/stm32lib +query-variants: + $(ECHO) "VARIANTS:" $(BOARD_VARIANTS) + LD_DIR=boards USBDEV_DIR=usbdev #USBHOST_DIR=usbhost diff --git a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk index df9506522574f..89977b74ead45 100644 --- a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk @@ -4,3 +4,24 @@ AF_FILE = boards/stm32f411_af.csv LD_FILES = boards/stm32f411.ld boards/common_ifs.ld TEXT0_ADDR = 0x08000000 TEXT1_ADDR = 0x08020000 + +# Provide different variants for the downloads page. +BOARD_VARIANTS += "dp thread dp-thread network" + +ifeq ($(BOARD_VARIANT),"dp") +MICROPY_FLOAT_IMPL=double +endif + +ifeq ($(BOARD_VARIANT),"thread") +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"dp-thread") +MICROPY_FLOAT_IMPL=double +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"network") +MICROPY_PY_NETWORK_WIZNET5K=5200 +MICROPY_PY_CC3K=1 +endif diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.mk b/ports/stm32/boards/PYBV10/mpconfigboard.mk index cb78a7846d933..071ae6eb7c6b5 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV10/mpconfigboard.mk @@ -14,3 +14,24 @@ endif # MicroPython settings MICROPY_VFS_LFS2 = 1 + +# Provide different variants for the downloads page. +BOARD_VARIANTS += "dp thread dp-thread network" + +ifeq ($(BOARD_VARIANT),"dp") +MICROPY_FLOAT_IMPL=double +endif + +ifeq ($(BOARD_VARIANT),"thread") +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"dp-thread") +MICROPY_FLOAT_IMPL=double +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"network") +MICROPY_PY_NETWORK_WIZNET5K=5200 +MICROPY_PY_CC3K=1 +endif diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.mk b/ports/stm32/boards/PYBV11/mpconfigboard.mk index cb78a7846d933..071ae6eb7c6b5 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV11/mpconfigboard.mk @@ -14,3 +14,24 @@ endif # MicroPython settings MICROPY_VFS_LFS2 = 1 + +# Provide different variants for the downloads page. +BOARD_VARIANTS += "dp thread dp-thread network" + +ifeq ($(BOARD_VARIANT),"dp") +MICROPY_FLOAT_IMPL=double +endif + +ifeq ($(BOARD_VARIANT),"thread") +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"dp-thread") +MICROPY_FLOAT_IMPL=double +CFLAGS += -DMICROPY_PY_THREAD=1 +endif + +ifeq ($(BOARD_VARIANT),"network") +MICROPY_PY_NETWORK_WIZNET5K=5200 +MICROPY_PY_CC3K=1 +endif diff --git a/tools/autobuild/build-stm32-extra.sh b/tools/autobuild/build-stm32-extra.sh index 80c6f42f5e6c7..feb269e356049 100755 --- a/tools/autobuild/build-stm32-extra.sh +++ b/tools/autobuild/build-stm32-extra.sh @@ -7,12 +7,15 @@ function do_build() { board=$2 shift shift - echo "building $descr $board" - build_dir=/tmp/stm-build-$board - $MICROPY_AUTOBUILD_MAKE $@ BOARD=$board BUILD=$build_dir || exit 1 - mv $build_dir/firmware.dfu $dest_dir/$descr$fw_tag.dfu - mv $build_dir/firmware.hex $dest_dir/$descr$fw_tag.hex - rm -rf $build_dir + for variant in `$MICROPY_AUTOBUILD_MAKE BOARD=$board query-variants | grep VARIANTS: | cut -d' ' -f2-`; do + target=$descr-$variant + echo "building $target $board" + build_dir=/tmp/stm-build-$board + $MICROPY_AUTOBUILD_MAKE $@ BOARD=$board BOARD_VARIANT=$variant BUILD=$build_dir || exit 1 + mv $build_dir/firmware.dfu $dest_dir/$target$fw_tag.dfu + mv $build_dir/firmware.hex $dest_dir/$target$fw_tag.hex + rm -rf $build_dir + done } # check/get parameters @@ -30,18 +33,7 @@ if [ ! -r modpyb.c ]; then exit 1 fi -# build the versions -do_build pybv3 PYBV3 -do_build pybv3-network PYBV3 MICROPY_PY_NETWORK_WIZNET5K=5200 MICROPY_PY_CC3K=1 -do_build pybv10-dp PYBV10 MICROPY_FLOAT_IMPL=double -do_build pybv10-thread PYBV10 CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pybv10-dp-thread PYBV10 MICROPY_FLOAT_IMPL=double CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pybv10-network PYBV10 MICROPY_PY_NETWORK_WIZNET5K=5200 MICROPY_PY_CC3K=1 -do_build pybv11-dp PYBV11 MICROPY_FLOAT_IMPL=double -do_build pybv11-thread PYBV11 CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pybv11-dp-thread PYBV11 MICROPY_FLOAT_IMPL=double CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pybv11-network PYBV11 MICROPY_PY_NETWORK_WIZNET5K=5200 MICROPY_PY_CC3K=1 -do_build pyblitev10-dp PYBLITEV10 MICROPY_FLOAT_IMPL=double -do_build pyblitev10-thread PYBLITEV10 CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pyblitev10-dp-thread PYBLITEV10 MICROPY_FLOAT_IMPL=double CFLAGS_EXTRA='-DMICROPY_PY_THREAD=1' -do_build pyblitev10-network PYBLITEV10 MICROPY_PY_NETWORK_WIZNET5K=5200 MICROPY_PY_CC3K=1 +# build the variants for each board +do_build pybv10 PYBV10 +do_build pybv11 PYBV11 +do_build pyblitev10 PYBLITEV10 From 986ad6bf1d35cbc6f8073e847aa6b87156eefc4b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 25 Aug 2022 12:27:49 +1000 Subject: [PATCH 2520/3533] stm32/boardctrl: Use HAL_Delay instead of mp_hal_delay_ms. Not safe to use mp_hal_delay_ms before boot if threading is enabled, because threading will not have been initialised, and MICROPY_EVENT_POLL_HOOK assumes threading is initialised. HAL_Delay doesn't call MICROPY_EVENT_POLL_HOOK, but is still power-efficient like mp_hal_delay_ms (unlike mp_hal_delay_us). Fixes #7816. Signed-off-by: Jim Mussared --- ports/stm32/boardctrl.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 406a98381419c..85a42a240ff0d 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -89,6 +89,9 @@ void boardctrl_maybe_enter_mboot(size_t n_args, const void *args_in) { #if !MICROPY_HW_USES_BOOTLOADER STATIC uint update_reset_mode(uint reset_mode) { + // Note: Must use HAL_Delay here as MicroPython is not yet initialised + // and mp_hal_delay_ms will attempt to invoke the scheduler. + #if MICROPY_HW_HAS_SWITCH if (switch_get()) { @@ -100,7 +103,7 @@ STATIC uint update_reset_mode(uint reset_mode) { led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); for (uint j = 0; j < 30; ++j) { - mp_hal_delay_ms(20); + HAL_Delay(20); if (!switch_get()) { goto select_mode; } @@ -115,13 +118,13 @@ STATIC uint update_reset_mode(uint reset_mode) { led_state(2, 0); led_state(3, 0); led_state(4, 0); - mp_hal_delay_ms(50); + HAL_Delay(50); led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); - mp_hal_delay_ms(50); + HAL_Delay(50); } - mp_hal_delay_ms(400); + HAL_Delay(400); #elif defined(MICROPY_HW_LED1) @@ -134,11 +137,11 @@ STATIC uint update_reset_mode(uint reset_mode) { break; } led_state(1, 1); - mp_hal_delay_ms(100); + HAL_Delay(100); led_state(1, 0); - mp_hal_delay_ms(200); + HAL_Delay(200); } - mp_hal_delay_ms(400); + HAL_Delay(400); if (!switch_get()) { break; } @@ -150,11 +153,11 @@ STATIC uint update_reset_mode(uint reset_mode) { for (uint i = 0; i < 2; i++) { for (uint j = 0; j < reset_mode; j++) { led_state(1, 1); - mp_hal_delay_ms(100); + HAL_Delay(100); led_state(1, 0); - mp_hal_delay_ms(200); + HAL_Delay(200); } - mp_hal_delay_ms(400); + HAL_Delay(400); } #else #error Need a reset mode update method From 1855df6361ad4ade62aa46ad08f3202a6a5b1bd1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 24 Aug 2022 10:44:35 +1000 Subject: [PATCH 2521/3533] stm32: Remove support for CC3000 WiFi driver. It has been about 8 years since support for this chip was added. Reasons to remove it are: - It is no longer easy to obtain this part. - There are now many other options for WiFi. - It's not a good use of developer time to maintain it. Signed-off-by: Damien George --- ports/stm32/Makefile | 23 - ports/stm32/boards/PYBLITEV10/board.md | 2 +- .../stm32/boards/PYBLITEV10/mpconfigboard.mk | 1 - ports/stm32/boards/PYBV10/board.md | 2 +- ports/stm32/boards/PYBV10/mpconfigboard.mk | 1 - ports/stm32/boards/PYBV11/board.md | 2 +- ports/stm32/boards/PYBV11/mpconfigboard.mk | 1 - ports/stm32/modnwcc3k.c | 608 ------------------ ports/stm32/mpconfigport.h | 8 - ports/stm32/mpconfigport.mk | 3 - tools/ci.sh | 2 - 11 files changed, 3 insertions(+), 650 deletions(-) delete mode 100644 ports/stm32/modnwcc3k.c diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index f981f8f3eb057..ee00d77173640 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -482,29 +482,6 @@ SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\ ) endif -# for CC3000 module -ifeq ($(MICROPY_PY_CC3K),1) -CC3000_DIR=drivers/cc3000 -INC += -I$(TOP)/$(CC3000_DIR)/inc -CFLAGS_MOD += -DMICROPY_PY_CC3K=1 -SRC_MOD += modnwcc3k.c -SRC_MOD += $(addprefix $(CC3000_DIR)/src/,\ - cc3000_common.c \ - evnt_handler.c \ - hci.c \ - netapp.c \ - nvmem.c \ - security.c \ - socket.c \ - wlan.c \ - ccspi.c \ - inet_ntop.c \ - inet_pton.c \ - patch.c \ - patch_prog.c \ - ) -endif - ifeq ($(MICROPY_SSL_MBEDTLS),1) CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' SRC_MOD += mbedtls/mbedtls_port.c diff --git a/ports/stm32/boards/PYBLITEV10/board.md b/ports/stm32/boards/PYBLITEV10/board.md index 1511ffcf514fa..b490d54046915 100644 --- a/ports/stm32/boards/PYBLITEV10/board.md +++ b/ports/stm32/boards/PYBLITEV10/board.md @@ -1 +1 @@ -The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for CC3000 and WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. +The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. diff --git a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk index 89977b74ead45..8aeaf0e40cfbd 100644 --- a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk @@ -23,5 +23,4 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 -MICROPY_PY_CC3K=1 endif diff --git a/ports/stm32/boards/PYBV10/board.md b/ports/stm32/boards/PYBV10/board.md index 1511ffcf514fa..b490d54046915 100644 --- a/ports/stm32/boards/PYBV10/board.md +++ b/ports/stm32/boards/PYBV10/board.md @@ -1 +1 @@ -The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for CC3000 and WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. +The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.mk b/ports/stm32/boards/PYBV10/mpconfigboard.mk index 071ae6eb7c6b5..97af22b5e19bd 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV10/mpconfigboard.mk @@ -33,5 +33,4 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 -MICROPY_PY_CC3K=1 endif diff --git a/ports/stm32/boards/PYBV11/board.md b/ports/stm32/boards/PYBV11/board.md index 1511ffcf514fa..b490d54046915 100644 --- a/ports/stm32/boards/PYBV11/board.md +++ b/ports/stm32/boards/PYBV11/board.md @@ -1 +1 @@ -The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for CC3000 and WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. +The "standard" build is listed first and is the default firmware that the pyboards are shipped with. Use this firmware if you are uncertain. The "double FP" builds use double-precision floating point instead of the standard single precision. The "threading" builds contain the \_thread module and allow multithreading. The "network" builds have network drivers for WIZ820io included. All these different firmware are completely interchangeable and you can freely change from one to the other without losing the filesystem on your pyboard. diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.mk b/ports/stm32/boards/PYBV11/mpconfigboard.mk index 071ae6eb7c6b5..97af22b5e19bd 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV11/mpconfigboard.mk @@ -33,5 +33,4 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 -MICROPY_PY_CC3K=1 endif diff --git a/ports/stm32/modnwcc3k.c b/ports/stm32/modnwcc3k.c deleted file mode 100644 index 952e535c2dbc6..0000000000000 --- a/ports/stm32/modnwcc3k.c +++ /dev/null @@ -1,608 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2014 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -// CC3000 defines its own ENOBUFS (different to standard one!) -#undef ENOBUFS - -#include "py/objtuple.h" -#include "py/objlist.h" -#include "py/stream.h" -#include "py/runtime.h" -#include "py/mperrno.h" -#include "py/mphal.h" -#include "shared/netutils/netutils.h" -#include "extmod/modnetwork.h" -#include "pin.h" -#include "spi.h" - -#include "hci.h" -#include "socket.h" -#include "inet_ntop.h" -#include "inet_pton.h" -#include "ccspi.h" -#include "wlan.h" -#include "nvmem.h" -#include "netapp.h" -#include "patch_prog.h" - -#define MAX_ADDRSTRLEN (128) -#define MAX_RX_PACKET (CC3000_RX_BUFFER_SIZE - CC3000_MINIMAL_RX_SIZE - 1) -#define MAX_TX_PACKET (CC3000_TX_BUFFER_SIZE - CC3000_MINIMAL_TX_SIZE - 1) - -#define MAKE_SOCKADDR(addr, ip, port) \ - sockaddr addr; \ - addr.sa_family = AF_INET; \ - addr.sa_data[0] = port >> 8; \ - addr.sa_data[1] = port; \ - addr.sa_data[2] = ip[0]; \ - addr.sa_data[3] = ip[1]; \ - addr.sa_data[4] = ip[2]; \ - addr.sa_data[5] = ip[3]; - -#define UNPACK_SOCKADDR(addr, ip, port) \ - port = (addr.sa_data[0] << 8) | addr.sa_data[1]; \ - ip[0] = addr.sa_data[2]; \ - ip[1] = addr.sa_data[3]; \ - ip[2] = addr.sa_data[4]; \ - ip[3] = addr.sa_data[5]; - -STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno); - -int CC3000_EXPORT(errno); // for cc3000 driver - -STATIC volatile uint32_t fd_closed_state = 0; -STATIC volatile bool wlan_connected = false; -STATIC volatile bool ip_obtained = false; - -STATIC int cc3k_get_fd_closed_state(int fd) { - return fd_closed_state & (1 << fd); -} - -STATIC void cc3k_set_fd_closed_state(int fd) { - fd_closed_state |= 1 << fd; -} - -STATIC void cc3k_reset_fd_closed_state(int fd) { - fd_closed_state &= ~(1 << fd); -} - -STATIC void cc3k_callback(long event_type, char *data, unsigned char length) { - switch (event_type) { - case HCI_EVNT_WLAN_UNSOL_CONNECT: - wlan_connected = true; - break; - case HCI_EVNT_WLAN_UNSOL_DISCONNECT: - // link down - wlan_connected = false; - ip_obtained = false; - break; - case HCI_EVNT_WLAN_UNSOL_DHCP: - ip_obtained = true; - break; - case HCI_EVNT_BSD_TCP_CLOSE_WAIT: - // mark socket for closure - cc3k_set_fd_closed_state(data[0]); - break; - } -} - -STATIC int cc3k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) { - uint32_t ip; - // CC3000 gethostbyname is unreliable and usually returns -95 on first call - for (int retry = 5; CC3000_EXPORT(gethostbyname)((char *)name, len, &ip) < 0; retry--) { - if (retry == 0 || CC3000_EXPORT(errno) != -95) { - return CC3000_EXPORT(errno); - } - mp_hal_delay_ms(50); - } - - if (ip == 0) { - // unknown host - return -2; - } - - out_ip[0] = ip >> 24; - out_ip[1] = ip >> 16; - out_ip[2] = ip >> 8; - out_ip[3] = ip; - - return 0; -} - -STATIC int cc3k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) { - if (socket->domain != MOD_NETWORK_AF_INET) { - *_errno = MP_EAFNOSUPPORT; - return -1; - } - - mp_uint_t type; - switch (socket->type) { - case MOD_NETWORK_SOCK_STREAM: - type = SOCK_STREAM; - break; - case MOD_NETWORK_SOCK_DGRAM: - type = SOCK_DGRAM; - break; - case MOD_NETWORK_SOCK_RAW: - type = SOCK_RAW; - break; - default: - *_errno = MP_EINVAL; - return -1; - } - - // open socket - int fd = CC3000_EXPORT(socket)(AF_INET, type, 0); - if (fd < 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - - // clear socket state - cc3k_reset_fd_closed_state(fd); - - // store state of this socket - socket->fileno = fd; - - // make accept blocking by default - int optval = SOCK_OFF; - socklen_t optlen = sizeof(optval); - CC3000_EXPORT(setsockopt)(socket->fileno, SOL_SOCKET, SOCKOPT_ACCEPT_NONBLOCK, &optval, optlen); - - return 0; -} - -STATIC void cc3k_socket_close(mod_network_socket_obj_t *socket) { - CC3000_EXPORT(closesocket)(socket->fileno); -} - -STATIC int cc3k_socket_bind(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = CC3000_EXPORT(bind)(socket->fileno, &addr, sizeof(addr)); - if (ret != 0) { - *_errno = ret; - return -1; - } - return 0; -} - -STATIC int cc3k_socket_listen(mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno) { - int ret = CC3000_EXPORT(listen)(socket->fileno, backlog); - if (ret != 0) { - *_errno = ret; - return -1; - } - return 0; -} - -STATIC int cc3k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno) { - // accept incoming connection - int fd; - sockaddr addr; - socklen_t addr_len = sizeof(addr); - if ((fd = CC3000_EXPORT(accept)(socket->fileno, &addr, &addr_len)) < 0) { - if (fd == SOC_IN_PROGRESS) { - *_errno = MP_EAGAIN; - } else { - *_errno = -fd; - } - return -1; - } - - // clear socket state - cc3k_reset_fd_closed_state(fd); - - // store state in new socket object - socket2->fileno = fd; - - // return ip and port - // it seems CC3000 returns little endian for accept?? - // UNPACK_SOCKADDR(addr, ip, *port); - *port = (addr.sa_data[1] << 8) | addr.sa_data[0]; - ip[3] = addr.sa_data[2]; - ip[2] = addr.sa_data[3]; - ip[1] = addr.sa_data[4]; - ip[0] = addr.sa_data[5]; - - return 0; -} - -STATIC int cc3k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = CC3000_EXPORT(connect)(socket->fileno, &addr, sizeof(addr)); - if (ret != 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - return 0; -} - -STATIC mp_uint_t cc3k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) { - if (cc3k_get_fd_closed_state(socket->fileno)) { - CC3000_EXPORT(closesocket)(socket->fileno); - *_errno = MP_EPIPE; - return -1; - } - - // CC3K does not handle fragmentation, and will overflow, - // split the packet into smaller ones and send them out. - mp_int_t bytes = 0; - while (bytes < len) { - int n = MIN((len - bytes), MAX_TX_PACKET); - n = CC3000_EXPORT(send)(socket->fileno, (uint8_t *)buf + bytes, n, 0); - if (n <= 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - bytes += n; - } - - return bytes; -} - -STATIC mp_uint_t cc3k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) { - // check the socket is open - if (cc3k_get_fd_closed_state(socket->fileno)) { - // socket is closed, but CC3000 may have some data remaining in buffer, so check - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(socket->fileno, &rfds); - cc3000_timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 1; - int nfds = CC3000_EXPORT(select)(socket->fileno + 1, &rfds, NULL, NULL, &tv); - if (nfds == -1 || !FD_ISSET(socket->fileno, &rfds)) { - // no data waiting, so close socket and return 0 data - CC3000_EXPORT(closesocket)(socket->fileno); - return 0; - } - } - - // cap length at MAX_RX_PACKET - len = MIN(len, MAX_RX_PACKET); - - // do the recv - int ret = CC3000_EXPORT(recv)(socket->fileno, buf, len, 0); - if (ret < 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - - return ret; -} - -STATIC mp_uint_t cc3k_socket_sendto(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = CC3000_EXPORT(sendto)(socket->fileno, (byte *)buf, len, 0, (sockaddr *)&addr, sizeof(addr)); - if (ret < 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - return ret; -} - -STATIC mp_uint_t cc3k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) { - sockaddr addr; - socklen_t addr_len = sizeof(addr); - mp_int_t ret = CC3000_EXPORT(recvfrom)(socket->fileno, buf, len, 0, &addr, &addr_len); - if (ret < 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - UNPACK_SOCKADDR(addr, ip, *port); - return ret; -} - -STATIC int cc3k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) { - int ret = CC3000_EXPORT(setsockopt)(socket->fileno, level, opt, optval, optlen); - if (ret < 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - return 0; -} - -STATIC int cc3k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) { - int ret; - if (timeout_ms == 0 || timeout_ms == -1) { - int optval; - socklen_t optlen = sizeof(optval); - if (timeout_ms == 0) { - // set non-blocking mode - optval = SOCK_ON; - } else { - // set blocking mode - optval = SOCK_OFF; - } - ret = CC3000_EXPORT(setsockopt)(socket->fileno, SOL_SOCKET, SOCKOPT_RECV_NONBLOCK, &optval, optlen); - if (ret == 0) { - ret = CC3000_EXPORT(setsockopt)(socket->fileno, SOL_SOCKET, SOCKOPT_ACCEPT_NONBLOCK, &optval, optlen); - } - } else { - // set timeout - socklen_t optlen = sizeof(timeout_ms); - ret = CC3000_EXPORT(setsockopt)(socket->fileno, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT, &timeout_ms, optlen); - } - - if (ret != 0) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - - return 0; -} - -STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) { - mp_uint_t ret; - if (request == MP_STREAM_POLL) { - mp_uint_t flags = arg; - ret = 0; - int fd = socket->fileno; - - // init fds - fd_set rfds, wfds, xfds; - FD_ZERO(&rfds); - FD_ZERO(&wfds); - FD_ZERO(&xfds); - - // set fds if needed - if (flags & MP_STREAM_POLL_RD) { - FD_SET(fd, &rfds); - - // A socked that just closed is available for reading. A call to - // recv() returns 0 which is consistent with BSD. - if (cc3k_get_fd_closed_state(fd)) { - ret |= MP_STREAM_POLL_RD; - } - } - if (flags & MP_STREAM_POLL_WR) { - FD_SET(fd, &wfds); - } - if (flags & MP_STREAM_POLL_HUP) { - FD_SET(fd, &xfds); - } - - // call cc3000 select with minimum timeout - cc3000_timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 1; - int nfds = CC3000_EXPORT(select)(fd + 1, &rfds, &wfds, &xfds, &tv); - - // check for error - if (nfds == -1) { - *_errno = CC3000_EXPORT(errno); - return -1; - } - - // check return of select - if (FD_ISSET(fd, &rfds)) { - ret |= MP_STREAM_POLL_RD; - } - if (FD_ISSET(fd, &wfds)) { - ret |= MP_STREAM_POLL_WR; - } - if (FD_ISSET(fd, &xfds)) { - ret |= MP_STREAM_POLL_HUP; - } - } else { - *_errno = MP_EINVAL; - ret = -1; - } - return ret; -} - -/******************************************************************************/ -// MicroPython bindings; CC3K class - -typedef struct _cc3k_obj_t { - mp_obj_base_t base; -} cc3k_obj_t; - -STATIC const cc3k_obj_t cc3k_obj = {{(mp_obj_type_t *)&mod_network_nic_type_cc3k}}; - -// \classmethod \constructor(spi, pin_cs, pin_en, pin_irq) -// Initialise the CC3000 using the given SPI bus and pins and return a CC3K object. -// -// Note: pins were originally hard-coded to: -// PYBv1.0: init(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3) -// [SPI on Y position; Y6=B13=SCK, Y7=B14=MISO, Y8=B15=MOSI] -// -// STM32F4DISC: init(pyb.SPI(2), pyb.Pin.cpu.A15, pyb.Pin.cpu.B10, pyb.Pin.cpu.B11) -STATIC mp_obj_t cc3k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 4, 4, false); - - // set the pins to use - SpiInit( - spi_from_mp_obj(args[0])->spi, - pin_find(args[1]), - pin_find(args[2]), - pin_find(args[3]) - ); - - // initialize and start the module - wlan_init(cc3k_callback, NULL, NULL, NULL, - ReadWlanInterruptPin, SpiResumeSpi, SpiPauseSpi, WriteWlanPin); - - if (wlan_start(0) != 0) { - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed to init CC3000 module")); - } - - // set connection policy. this should be called explicitly by the user - // wlan_ioctl_set_connection_policy(0, 0, 0); - - // Mask out all non-required events from the CC3000 - wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | - HCI_EVNT_WLAN_UNSOL_INIT | - HCI_EVNT_WLAN_ASYNC_PING_REPORT | - HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE); - - // register with network module - mod_network_register_nic((mp_obj_t)&cc3k_obj); - - return (mp_obj_t)&cc3k_obj; -} - -// method connect(ssid, key=None, *, security=WPA2, bssid=None) -STATIC mp_obj_t cc3k_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - static const mp_arg_t allowed_args[] = { - { MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_key, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WLAN_SEC_WPA2} }, - { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - }; - - // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - // get ssid - size_t ssid_len; - const char *ssid = mp_obj_str_get_data(args[0].u_obj, &ssid_len); - - // get key and sec - size_t key_len = 0; - const char *key = NULL; - mp_uint_t sec = WLAN_SEC_UNSEC; - if (args[1].u_obj != mp_const_none) { - key = mp_obj_str_get_data(args[1].u_obj, &key_len); - sec = args[2].u_int; - } - - // get bssid - const char *bssid = NULL; - if (args[3].u_obj != mp_const_none) { - bssid = mp_obj_str_get_str(args[3].u_obj); - } - - // connect to AP - if (wlan_connect(sec, (char *)ssid, ssid_len, (uint8_t *)bssid, (uint8_t *)key, key_len) != 0) { - mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("could not connect to ssid=%s, sec=%d, key=%s\n"), ssid, sec, key); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(cc3k_connect_obj, 1, cc3k_connect); - -STATIC mp_obj_t cc3k_disconnect(mp_obj_t self_in) { - // should we check return value? - wlan_disconnect(); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_disconnect_obj, cc3k_disconnect); - -STATIC mp_obj_t cc3k_isconnected(mp_obj_t self_in) { - return mp_obj_new_bool(wlan_connected && ip_obtained); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_isconnected_obj, cc3k_isconnected); - -STATIC mp_obj_t cc3k_ifconfig(mp_obj_t self_in) { - tNetappIpconfigRetArgs ipconfig; - netapp_ipconfig(&ipconfig); - - // render MAC address - VSTR_FIXED(mac_vstr, 18); - const uint8_t *mac = ipconfig.uaMacAddr; - vstr_printf(&mac_vstr, "%02x:%02x:%02x:%02x:%02x:%02x", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]); - - // create and return tuple with ifconfig info - mp_obj_t tuple[7] = { - netutils_format_ipv4_addr(ipconfig.aucIP, NETUTILS_LITTLE), - netutils_format_ipv4_addr(ipconfig.aucSubnetMask, NETUTILS_LITTLE), - netutils_format_ipv4_addr(ipconfig.aucDefaultGateway, NETUTILS_LITTLE), - netutils_format_ipv4_addr(ipconfig.aucDNSServer, NETUTILS_LITTLE), - netutils_format_ipv4_addr(ipconfig.aucDHCPServer, NETUTILS_LITTLE), - mp_obj_new_str(mac_vstr.buf, mac_vstr.len), - mp_obj_new_str((const char *)ipconfig.uaSSID, strlen((const char *)ipconfig.uaSSID)), - }; - return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_ifconfig_obj, cc3k_ifconfig); - -STATIC mp_obj_t cc3k_patch_version(mp_obj_t self_in) { - uint8_t pver[2]; - mp_obj_tuple_t *t_pver; - - nvmem_read_sp_version(pver); - t_pver = mp_obj_new_tuple(2, NULL); - t_pver->items[0] = mp_obj_new_int(pver[0]); - t_pver->items[1] = mp_obj_new_int(pver[1]); - return t_pver; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_patch_version_obj, cc3k_patch_version); - -STATIC mp_obj_t cc3k_patch_program(mp_obj_t self_in, mp_obj_t key_in) { - const char *key = mp_obj_str_get_str(key_in); - if (key[0] == 'p' && key[1] == 'g' && key[2] == 'm' && key[3] == '\0') { - patch_prog_start(); - } else { - mp_print_str(&mp_plat_print, "pass 'pgm' as argument in order to program\n"); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(cc3k_patch_program_obj, cc3k_patch_program); - -STATIC const mp_rom_map_elem_t cc3k_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&cc3k_connect_obj) }, - { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&cc3k_disconnect_obj) }, - { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&cc3k_isconnected_obj) }, - { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&cc3k_ifconfig_obj) }, - { MP_ROM_QSTR(MP_QSTR_patch_version), MP_ROM_PTR(&cc3k_patch_version_obj) }, - { MP_ROM_QSTR(MP_QSTR_patch_program), MP_ROM_PTR(&cc3k_patch_program_obj) }, - - // class constants - { MP_ROM_QSTR(MP_QSTR_WEP), MP_ROM_INT(WLAN_SEC_WEP) }, - { MP_ROM_QSTR(MP_QSTR_WPA), MP_ROM_INT(WLAN_SEC_WPA) }, - { MP_ROM_QSTR(MP_QSTR_WPA2), MP_ROM_INT(WLAN_SEC_WPA2) }, -}; - -STATIC MP_DEFINE_CONST_DICT(cc3k_locals_dict, cc3k_locals_dict_table); - -const mod_network_nic_type_t mod_network_nic_type_cc3k = { - .base = { - { &mp_type_type }, - .name = MP_QSTR_CC3K, - .make_new = cc3k_make_new, - .locals_dict = (mp_obj_dict_t *)&cc3k_locals_dict, - }, - .gethostbyname = cc3k_gethostbyname, - .socket = cc3k_socket_socket, - .close = cc3k_socket_close, - .bind = cc3k_socket_bind, - .listen = cc3k_socket_listen, - .accept = cc3k_socket_accept, - .connect = cc3k_socket_connect, - .send = cc3k_socket_send, - .recv = cc3k_socket_recv, - .sendto = cc3k_socket_sendto, - .recvfrom = cc3k_socket_recvfrom, - .setsockopt = cc3k_socket_setsockopt, - .settimeout = cc3k_socket_settimeout, - .ioctl = cc3k_socket_ioctl, -}; diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index e06e32c0d2173..69b29e8ec3932 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -200,13 +200,6 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k; #define MICROPY_HW_NIC_WIZNET5K #endif -#if MICROPY_PY_CC3K -extern const struct _mod_network_nic_type_t mod_network_nic_type_cc3k; -#define MICROPY_HW_NIC_CC3K { MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) }, -#else -#define MICROPY_HW_NIC_CC3K -#endif - // extra constants #define MICROPY_PORT_CONSTANTS \ MACHINE_BUILTIN_MODULE_CONSTANTS \ @@ -221,7 +214,6 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_cc3k; MICROPY_HW_NIC_ETH \ MICROPY_HW_NIC_CYW43 \ MICROPY_HW_NIC_WIZNET5K \ - MICROPY_HW_NIC_CC3K \ MICROPY_BOARD_NETWORK_INTERFACES \ #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/stm32/mpconfigport.mk b/ports/stm32/mpconfigport.mk index 830ccf03123a3..737ff9d3d29ac 100644 --- a/ports/stm32/mpconfigport.mk +++ b/ports/stm32/mpconfigport.mk @@ -6,9 +6,6 @@ # 5500 : support for W5500 module MICROPY_PY_NETWORK_WIZNET5K ?= 0 -# cc3k module for wifi support -MICROPY_PY_CC3K ?= 0 - # VFS FAT FS support MICROPY_VFS_FAT ?= 1 diff --git a/tools/ci.sh b/tools/ci.sh index b7b20e3c7d65b..b4cb7dc2e380a 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -313,8 +313,6 @@ function ci_stm32_pyb_build { git submodule update --init lib/btstack git submodule update --init lib/mynewt-nimble make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_NETWORK_WIZNET5K=5200 USER_C_MODULES=../../examples/usercmodule - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 clean - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_CC3K=1 make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1 make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1' From 7d3f4b23dc26bd26477a2ef6d7b25b3af73453f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 24 Aug 2022 10:48:27 +1000 Subject: [PATCH 2522/3533] drivers/cc3000: Remove CC3000 WiFi driver files. It's no longer used by any port. Signed-off-by: Damien George --- LICENSE | 1 - docs/library/network.CC3K.rst | 89 -- docs/library/network.rst | 1 - drivers/cc3000/inc/cc3000_common.h | 365 ------- drivers/cc3000/inc/ccspi.h | 84 -- drivers/cc3000/inc/data_types.h | 107 -- drivers/cc3000/inc/evnt_handler.h | 166 --- drivers/cc3000/inc/hci.h | 330 ------ drivers/cc3000/inc/host_driver_version.h | 40 - drivers/cc3000/inc/inet_ntop.h | 4 - drivers/cc3000/inc/inet_pton.h | 4 - drivers/cc3000/inc/netapp.h | 343 ------ drivers/cc3000/inc/nvmem.h | 248 ----- drivers/cc3000/inc/patch_prog.h | 11 - drivers/cc3000/inc/security.h | 130 --- drivers/cc3000/inc/socket.h | 676 ------------ drivers/cc3000/inc/wlan.h | 518 --------- drivers/cc3000/src/cc3000_common.c | 164 --- drivers/cc3000/src/ccspi.c | 455 -------- drivers/cc3000/src/evnt_handler.c | 849 --------------- drivers/cc3000/src/hci.c | 225 ---- drivers/cc3000/src/inet_ntop.c | 200 ---- drivers/cc3000/src/inet_pton.c | 216 ---- drivers/cc3000/src/netapp.c | 459 -------- drivers/cc3000/src/nvmem.c | 334 ------ drivers/cc3000/src/patch.c | 117 -- drivers/cc3000/src/patch_prog.c | 414 ------- drivers/cc3000/src/security.c | 530 --------- drivers/cc3000/src/socket.c | 1182 -------------------- drivers/cc3000/src/wlan.c | 1252 ---------------------- 30 files changed, 9514 deletions(-) delete mode 100644 docs/library/network.CC3K.rst delete mode 100644 drivers/cc3000/inc/cc3000_common.h delete mode 100644 drivers/cc3000/inc/ccspi.h delete mode 100644 drivers/cc3000/inc/data_types.h delete mode 100644 drivers/cc3000/inc/evnt_handler.h delete mode 100644 drivers/cc3000/inc/hci.h delete mode 100644 drivers/cc3000/inc/host_driver_version.h delete mode 100644 drivers/cc3000/inc/inet_ntop.h delete mode 100644 drivers/cc3000/inc/inet_pton.h delete mode 100644 drivers/cc3000/inc/netapp.h delete mode 100644 drivers/cc3000/inc/nvmem.h delete mode 100644 drivers/cc3000/inc/patch_prog.h delete mode 100644 drivers/cc3000/inc/security.h delete mode 100644 drivers/cc3000/inc/socket.h delete mode 100644 drivers/cc3000/inc/wlan.h delete mode 100644 drivers/cc3000/src/cc3000_common.c delete mode 100644 drivers/cc3000/src/ccspi.c delete mode 100644 drivers/cc3000/src/evnt_handler.c delete mode 100644 drivers/cc3000/src/hci.c delete mode 100644 drivers/cc3000/src/inet_ntop.c delete mode 100644 drivers/cc3000/src/inet_pton.c delete mode 100644 drivers/cc3000/src/netapp.c delete mode 100644 drivers/cc3000/src/nvmem.c delete mode 100644 drivers/cc3000/src/patch.c delete mode 100644 drivers/cc3000/src/patch_prog.c delete mode 100644 drivers/cc3000/src/security.c delete mode 100644 drivers/cc3000/src/socket.c delete mode 100644 drivers/cc3000/src/wlan.c diff --git a/LICENSE b/LICENSE index 2b9a64b89a723..5a91df195fd4a 100644 --- a/LICENSE +++ b/LICENSE @@ -35,7 +35,6 @@ used during the build process and is not part of the compiled source code. / (MIT) /drivers - /cc3000 (BSD-3-clause) /cc3100 (BSD-3-clause) /wiznet5k (BSD-3-clause) /lib diff --git a/docs/library/network.CC3K.rst b/docs/library/network.CC3K.rst deleted file mode 100644 index 41d3fb437e992..0000000000000 --- a/docs/library/network.CC3K.rst +++ /dev/null @@ -1,89 +0,0 @@ -.. currentmodule:: network -.. _network.CC3K: - -class CC3K -- control CC3000 WiFi modules -========================================= - -This class provides a driver for CC3000 WiFi modules. Example usage:: - - import network - nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3) - nic.connect('your-ssid', 'your-password') - while not nic.isconnected(): - pyb.delay(50) - print(nic.ifconfig()) - - # now use socket as usual - ... - -For this example to work the CC3000 module must have the following connections: - - - MOSI connected to Y8 - - MISO connected to Y7 - - CLK connected to Y6 - - CS connected to Y5 - - VBEN connected to Y4 - - IRQ connected to Y3 - -It is possible to use other SPI buses and other pins for CS, VBEN and IRQ. - -Constructors ------------- - -.. class:: CC3K(spi, pin_cs, pin_en, pin_irq) - - Create a CC3K driver object, initialise the CC3000 module using the given SPI bus - and pins, and return the CC3K object. - - Arguments are: - - - *spi* is an :ref:`SPI object ` which is the SPI bus that the CC3000 is - connected to (the MOSI, MISO and CLK pins). - - *pin_cs* is a :ref:`Pin object ` which is connected to the CC3000 CS pin. - - *pin_en* is a :ref:`Pin object ` which is connected to the CC3000 VBEN pin. - - *pin_irq* is a :ref:`Pin object ` which is connected to the CC3000 IRQ pin. - - All of these objects will be initialised by the driver, so there is no need to - initialise them yourself. For example, you can use:: - - nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3) - -Methods -------- - -.. method:: CC3K.connect(ssid, key=None, *, security=WPA2, bssid=None) - - Connect to a WiFi access point using the given SSID, and other security - parameters. - -.. method:: CC3K.disconnect() - - Disconnect from the WiFi access point. - -.. method:: CC3K.isconnected() - - Returns True if connected to a WiFi access point and has a valid IP address, - False otherwise. - -.. method:: CC3K.ifconfig() - - Returns a 7-tuple with (ip, subnet mask, gateway, DNS server, DHCP server, - MAC address, SSID). - -.. method:: CC3K.patch_version() - - Return the version of the patch program (firmware) on the CC3000. - -.. method:: CC3K.patch_program('pgm') - - Upload the current firmware to the CC3000. You must pass 'pgm' as the first - argument in order for the upload to proceed. - -Constants ---------- - -.. data:: CC3K.WEP -.. data:: CC3K.WPA -.. data:: CC3K.WPA2 - - security type to use diff --git a/docs/library/network.rst b/docs/library/network.rst index 6742a2e0733db..0a6f5506ea411 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -150,7 +150,6 @@ provide a way to control networking interfaces of various kinds. network.WLAN.rst network.WLANWiPy.rst - network.CC3K.rst network.WIZNET5K.rst network.LAN.rst diff --git a/drivers/cc3000/inc/cc3000_common.h b/drivers/cc3000/inc/cc3000_common.h deleted file mode 100644 index d0c4b1d4b92ea..0000000000000 --- a/drivers/cc3000/inc/cc3000_common.h +++ /dev/null @@ -1,365 +0,0 @@ -/***************************************************************************** -* -* cc3000_common.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_COMMON_H__ -#define __CC3000_COMMON_H__ - -#include "data_types.h" - -//****************************************************************************** -// Include files -//****************************************************************************** -#include -#include - -//***************************************************************************** -// Prefix exported names to avoid name clash -//***************************************************************************** -#define CC3000_EXPORT(name) cc3000_ ## name - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -extern int CC3000_EXPORT(errno); - -//***************************************************************************** -// ERROR CODES -//***************************************************************************** -#define ESUCCESS 0 -#define EFAIL -1 -#define EERROR EFAIL - -//***************************************************************************** -// COMMON DEFINES -//***************************************************************************** -#define ERROR_SOCKET_INACTIVE -57 - -#define WLAN_ENABLE (1) -#define WLAN_DISABLE (0) - -#define MAC_ADDR_LEN (6) - -#define SP_PORTION_SIZE (32) - -/*Defines for minimal and maximal RX buffer size. This size includes the spi - header and hci header. - The maximal buffer size derives from: - MTU + HCI header + SPI header + sendto() agrs size - The minimum buffer size derives from: - HCI header + SPI header + max args size - - This buffer is used for receiving events and data. - The packet can not be longer than MTU size and CC3000 does not support - fragmentation. Note that the same buffer is used for reception of the data - and events from CC3000. That is why the minimum is defined. - The calculation for the actual size of buffer for reception is: - Given the maximal data size MAX_DATA that is expected to be received by - application, the required buffer is: - Using recv() or recvfrom(): - - max(CC3000_MINIMAL_RX_SIZE, MAX_DATA + HEADERS_SIZE_DATA + fromlen - + ucArgsize + 1) - - Using gethostbyname() with minimal buffer size will limit the host name - returned to 99 bytes only. - The 1 is used for the overrun detection - - Buffer size increased to 130 following the add_profile() with WEP security - which requires TX buffer size of 130 bytes: - HEADERS_SIZE_EVNT + WLAN_ADD_PROFILE_WEP_PARAM_LEN + MAX SSID LEN + 4 * MAX KEY LEN = 130 - MAX SSID LEN = 32 - MAX SSID LEN = 13 (with add_profile only ascii key setting is supported, - therfore maximum key size is 13) -*/ - -#define CC3000_MINIMAL_RX_SIZE (130 + 1) -#define CC3000_MAXIMAL_RX_SIZE (1519 + 1) - -/*Defines for minimal and maximal TX buffer size. - This buffer is used for sending events and data. - The packet can not be longer than MTU size and CC3000 does not support - fragmentation. Note that the same buffer is used for transmission of the data - and commands. That is why the minimum is defined. - The calculation for the actual size of buffer for transmission is: - Given the maximal data size MAX_DATA, the required buffer is: - Using Sendto(): - - max(CC3000_MINIMAL_TX_SIZE, MAX_DATA + SPI_HEADER_SIZE - + SOCKET_SENDTO_PARAMS_LEN + SIMPLE_LINK_HCI_DATA_HEADER_SIZE + 1) - - Using Send(): - - max(CC3000_MINIMAL_TX_SIZE, MAX_DATA + SPI_HEADER_SIZE - + HCI_CMND_SEND_ARG_LENGTH + SIMPLE_LINK_HCI_DATA_HEADER_SIZE + 1) - - The 1 is used for the overrun detection */ - -#define CC3000_MINIMAL_TX_SIZE (130 + 1) -#define CC3000_MAXIMAL_TX_SIZE (1519 + 1) - -//TX and RX buffer sizes, allow to receive and transmit maximum data at length 8. -#ifdef CC3000_TINY_DRIVER -#define TINY_CC3000_MAXIMAL_RX_SIZE 44 -#define TINY_CC3000_MAXIMAL_TX_SIZE 59 -#endif - -/*In order to determine your preferred buffer size, - change CC3000_MAXIMAL_RX_SIZE and CC3000_MAXIMAL_TX_SIZE to a value between - the minimal and maximal specified above. - Note that the buffers are allocated by SPI. - In case you change the size of those buffers, you might need also to change - the linker file, since for example on MSP430 FRAM devices the buffers are - allocated in the FRAM section that is allocated manually and not by IDE. -*/ - -#ifndef CC3000_TINY_DRIVER - - #define CC3000_RX_BUFFER_SIZE (CC3000_MAXIMAL_RX_SIZE) - #define CC3000_TX_BUFFER_SIZE (CC3000_MAXIMAL_TX_SIZE) - -//if defined TINY DRIVER we use smaller RX and TX buffer in order to minimize RAM consumption -#else - #define CC3000_RX_BUFFER_SIZE (TINY_CC3000_MAXIMAL_RX_SIZE) - #define CC3000_TX_BUFFER_SIZE (TINY_CC3000_MAXIMAL_TX_SIZE) - -#endif - -//***************************************************************************** -// Compound Types -//***************************************************************************** -typedef INT32 cc3000_time_t; -typedef UINT32 clock_t; -typedef INT32 suseconds_t; - -typedef struct cc3000_timeval cc3000_timeval; - -struct cc3000_timeval -{ - cc3000_time_t tv_sec; /* seconds */ - suseconds_t tv_usec; /* microseconds */ -}; - -typedef CHAR *(*tFWPatches)(UINT32 *usLength); - -typedef CHAR *(*tDriverPatches)(UINT32 *usLength); - -typedef CHAR *(*tBootLoaderPatches)(UINT32 *usLength); - -typedef void (*tWlanCB)(INT32 event_type, CHAR * data, UINT8 length ); - -typedef INT32 (*tWlanReadInteruptPin)(void); - -typedef void (*tWlanInterruptEnable)(void); - -typedef void (*tWlanInterruptDisable)(void); - -typedef void (*tWriteWlanPin)(UINT8 val); - -typedef struct -{ - UINT16 usRxEventOpcode; - UINT16 usEventOrDataReceived; - UINT8 *pucReceivedData; - UINT8 *pucTxCommandBuffer; - - tFWPatches sFWPatches; - tDriverPatches sDriverPatches; - tBootLoaderPatches sBootLoaderPatches; - tWlanCB sWlanCB; - tWlanReadInteruptPin ReadWlanInterruptPin; - tWlanInterruptEnable WlanInterruptEnable; - tWlanInterruptDisable WlanInterruptDisable; - tWriteWlanPin WriteWlanPin; - - INT32 slTransmitDataError; - UINT16 usNumberOfFreeBuffers; - UINT16 usSlBufferLength; - UINT16 usBufferSize; - UINT16 usRxDataPending; - - UINT32 NumberOfSentPackets; - UINT32 NumberOfReleasedPackets; - - UINT8 InformHostOnTxComplete; -}sSimplLinkInformation; - -extern volatile sSimplLinkInformation tSLInformation; - - -//***************************************************************************** -// Prototypes for the APIs. -//***************************************************************************** - - - -//***************************************************************************** -// -//! SimpleLinkWaitEvent -//! -//! @param usOpcode command operation code -//! @param pRetParams command return parameters -//! -//! @return none -//! -//! @brief Wait for event, pass it to the hci_event_handler and -//! update the event opcode in a global variable. -// -//***************************************************************************** - -extern void SimpleLinkWaitEvent(UINT16 usOpcode, void *pRetParams); - -//***************************************************************************** -// -//! SimpleLinkWaitData -//! -//! @param pBuf data buffer -//! @param from from information -//! @param fromlen from information length -//! -//! @return none -//! -//! @brief Wait for data, pass it to the hci_event_handler -//! and update in a global variable that there is -//! data to read. -// -//***************************************************************************** - -extern void SimpleLinkWaitData(UINT8 *pBuf, UINT8 *from, UINT8 *fromlen); - -//***************************************************************************** -// -//! UINT32_TO_STREAM_f -//! -//! \param p pointer to the new stream -//! \param u32 pointer to the 32 bit -//! -//! \return pointer to the new stream -//! -//! \brief This function is used for copying 32 bit to stream -//! while converting to little endian format. -// -//***************************************************************************** - -extern UINT8* UINT32_TO_STREAM_f (UINT8 *p, UINT32 u32); - -//***************************************************************************** -// -//! UINT16_TO_STREAM_f -//! -//! \param p pointer to the new stream -//! \param u32 pointer to the 16 bit -//! -//! \return pointer to the new stream -//! -//! \brief This function is used for copying 16 bit to stream -//! while converting to little endian format. -// -//***************************************************************************** - -extern UINT8* UINT16_TO_STREAM_f (UINT8 *p, UINT16 u16); - -//***************************************************************************** -// -//! STREAM_TO_UINT16_f -//! -//! \param p pointer to the stream -//! \param offset offset in the stream -//! -//! \return pointer to the new 16 bit -//! -//! \brief This function is used for copying received stream to -//! 16 bit in little endian format. -// -//***************************************************************************** - -extern UINT16 STREAM_TO_UINT16_f(CHAR* p, UINT16 offset); - -//***************************************************************************** -// -//! STREAM_TO_UINT32_f -//! -//! \param p pointer to the stream -//! \param offset offset in the stream -//! -//! \return pointer to the new 32 bit -//! -//! \brief This function is used for copying received stream to -//! 32 bit in little endian format. -// -//***************************************************************************** - -extern UINT32 STREAM_TO_UINT32_f(CHAR* p, UINT16 offset); - - -//***************************************************************************** -// COMMON MACROs -//***************************************************************************** - - -//This macro is used for copying 8 bit to stream while converting to little endian format. -#define UINT8_TO_STREAM(_p, _val) {*(_p)++ = (_val);} -//This macro is used for copying 16 bit to stream while converting to little endian format. -#define UINT16_TO_STREAM(_p, _u16) (UINT16_TO_STREAM_f(_p, _u16)) -//This macro is used for copying 32 bit to stream while converting to little endian format. -#define UINT32_TO_STREAM(_p, _u32) (UINT32_TO_STREAM_f(_p, _u32)) -//This macro is used for copying a specified value length bits (l) to stream while converting to little endian format. -#define ARRAY_TO_STREAM(p, a, l) {register INT16 _i; for (_i = 0; _i < l; _i++) *(p)++ = ((UINT8 *) a)[_i];} -//This macro is used for copying received stream to 8 bit in little endian format. -#define STREAM_TO_UINT8(_p, _offset, _u8) {_u8 = (UINT8)(*(_p + _offset));} -//This macro is used for copying received stream to 16 bit in little endian format. -#define STREAM_TO_UINT16(_p, _offset, _u16) {_u16 = STREAM_TO_UINT16_f(_p, _offset);} -//This macro is used for copying received stream to 32 bit in little endian format. -#define STREAM_TO_UINT32(_p, _offset, _u32) {_u32 = STREAM_TO_UINT32_f(_p, _offset);} -#define STREAM_TO_STREAM(p, a, l) {register INT16 _i; for (_i = 0; _i < l; _i++) *(a)++= ((UINT8 *) p)[_i];} - - - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_COMMON_H__ diff --git a/drivers/cc3000/inc/ccspi.h b/drivers/cc3000/inc/ccspi.h deleted file mode 100644 index 8fa3ecd10c2fc..0000000000000 --- a/drivers/cc3000/inc/ccspi.h +++ /dev/null @@ -1,84 +0,0 @@ -/***************************************************************************** -* -* spi.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - - -#ifndef __CC3000_SPI_H__ -#define __CC3000_SPI_H__ - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -typedef void (*gcSpiHandleRx)(void *p); -typedef void (*gcSpiHandleTx)(void); -extern unsigned char wlan_tx_buffer[]; - -//***************************************************************************** -// -// Prototypes for the APIs. -// -//***************************************************************************** - -// the arguments must be of type pin_obj_t* and SPI_HandleTypeDef* -extern void SpiInit(void *spi, const void *pin_cs, const void *pin_en, const void *pin_irq); - -extern void SpiOpen(gcSpiHandleRx pfRxHandler); -extern void SpiClose(void); -extern void SpiPauseSpi(void); -extern void SpiResumeSpi(void); -extern long SpiWrite(unsigned char *pUserBuffer, unsigned short usLength); -extern void SpiConfigureHwMapping(void); -extern void SpiCleanGPIOISR(void); -extern void SSIConfigure(unsigned long ulSSIFreq, unsigned long bForceGpioConfiguration, unsigned long uiReconfigureSysClock); -extern int init_spi(void); -extern long ReadWlanInterruptPin(void); -extern void WriteWlanPin(unsigned char val); -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif - diff --git a/drivers/cc3000/inc/data_types.h b/drivers/cc3000/inc/data_types.h deleted file mode 100644 index 0520a9202fb88..0000000000000 --- a/drivers/cc3000/inc/data_types.h +++ /dev/null @@ -1,107 +0,0 @@ -/***************************************************************************** -* -* data_types.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_DATA_TYPES__ -#define __CC3000_DATA_TYPES__ - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - - -#ifndef NULL -#define NULL (0) -#endif - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -#ifndef OK -#define OK (0) -#endif - -#ifndef _INT8 -#define _INT8 -typedef signed char INT8; -#endif - -#ifndef _UINT8 -#define _UINT8 -typedef unsigned char UINT8; -#endif - -#ifndef _INT16 -#define _INT16 -typedef signed short INT16; -#endif - -#ifndef _UINT16 -#define _UINT16 -typedef unsigned short UINT16; -#endif - -#ifndef _BOOLEAN -#define _BOOLEAN -typedef unsigned char BOOLEAN; -#endif - -#ifndef _INT32 -#define _INT32 -typedef signed long INT32; -#endif - -#ifndef _UINT32 -#define _UINT32 -typedef unsigned long UINT32; -#endif - -typedef int INT; -typedef char CHAR; - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* __CC3000_DATA_TYPES__ */ diff --git a/drivers/cc3000/inc/evnt_handler.h b/drivers/cc3000/inc/evnt_handler.h deleted file mode 100644 index d05a442f05fbb..0000000000000 --- a/drivers/cc3000/inc/evnt_handler.h +++ /dev/null @@ -1,166 +0,0 @@ -/***************************************************************************** -* -* evnt_handler.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_EVENT_HANDLER_H__ -#define __CC3000_EVENT_HANDLER_H__ -#include "hci.h" -#include "socket.h" - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -//***************************************************************************** -// -// Prototypes for the APIs. -// -//***************************************************************************** - -//***************************************************************************** -// -//! hci_event_handler -//! -//! @param pRetParams incoming data buffer -//! @param from from information (in case of data received) -//! @param fromlen from information length (in case of data received) -//! -//! @return none -//! -//! @brief Parse the incoming events packets and issues corresponding -//! event handler from global array of handlers pointers -// -//***************************************************************************** -extern UINT8 *hci_event_handler(void *pRetParams, UINT8 *from, UINT8 *fromlen); - -//***************************************************************************** -// -//! hci_unsol_event_handler -//! -//! @param event_hdr event header -//! -//! @return 1 if event supported and handled -//! 0 if event is not supported -//! -//! @brief Handle unsolicited events -// -//***************************************************************************** -extern INT32 hci_unsol_event_handler(CHAR *event_hdr); - -//***************************************************************************** -// -//! hci_unsolicited_event_handler -//! -//! @param None -//! -//! @return ESUCCESS if successful, EFAIL if an error occurred -//! -//! @brief Parse the incoming unsolicited event packets and issues -//! corresponding event handler. -// -//***************************************************************************** -extern INT32 hci_unsolicited_event_handler(void); - -#define M_BSD_RESP_PARAMS_OFFSET(hci_event_hdr)((CHAR *)(hci_event_hdr) + HCI_EVENT_HEADER_SIZE) - -#define SOCKET_STATUS_ACTIVE 0 -#define SOCKET_STATUS_INACTIVE 1 -/* Init socket_active_status = 'all ones': init all sockets with SOCKET_STATUS_INACTIVE. - Will be changed by 'set_socket_active_status' upon 'connect' and 'accept' calls */ -#define SOCKET_STATUS_INIT_VAL 0xFFFF -#define M_IS_VALID_SD(sd) ((0 <= (sd)) && ((sd) <= 7)) -#define M_IS_VALID_STATUS(status) (((status) == SOCKET_STATUS_ACTIVE)||((status) == SOCKET_STATUS_INACTIVE)) - -extern UINT32 socket_active_status; - -extern void set_socket_active_status(INT32 Sd, INT32 Status); -extern INT32 get_socket_active_status(INT32 Sd); - -typedef struct _bsd_accept_return_t -{ - INT32 iSocketDescriptor; - INT32 iStatus; - sockaddr tSocketAddress; - -} tBsdReturnParams; - - -typedef struct _bsd_read_return_t -{ - INT32 iSocketDescriptor; - INT32 iNumberOfBytes; - UINT32 uiFlags; -} tBsdReadReturnParams; - -#define BSD_RECV_FROM_FROMLEN_OFFSET (4) -#define BSD_RECV_FROM_FROM_OFFSET (16) - - -typedef struct _bsd_select_return_t -{ - INT32 iStatus; - UINT32 uiRdfd; - UINT32 uiWrfd; - UINT32 uiExfd; -} tBsdSelectRecvParams; - - -typedef struct _bsd_getsockopt_return_t -{ - UINT8 ucOptValue[4]; - CHAR iStatus; -} tBsdGetSockOptReturnParams; - -typedef struct _bsd_gethostbyname_return_t -{ - INT32 retVal; - INT32 outputAddress; -} tBsdGethostbynameParams; - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_EVENT_HANDLER_H__ - diff --git a/drivers/cc3000/inc/hci.h b/drivers/cc3000/inc/hci.h deleted file mode 100644 index f12b00e918e1b..0000000000000 --- a/drivers/cc3000/inc/hci.h +++ /dev/null @@ -1,330 +0,0 @@ -/***************************************************************************** -* -* hci.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_HCI_H__ -#define __CC3000_HCI_H__ - -#include "cc3000_common.h" - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - - -#define SPI_HEADER_SIZE (5) -#define SIMPLE_LINK_HCI_CMND_HEADER_SIZE (4) -#define HEADERS_SIZE_CMD (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE) -#define SIMPLE_LINK_HCI_DATA_CMND_HEADER_SIZE (5) -#define SIMPLE_LINK_HCI_DATA_HEADER_SIZE (5) -#define SIMPLE_LINK_HCI_PATCH_HEADER_SIZE (2) - - -//***************************************************************************** -// -// Values that can be used as HCI Commands and HCI Packet header defines -// -//***************************************************************************** -#define HCI_TYPE_CMND 0x1 -#define HCI_TYPE_DATA 0x2 -#define HCI_TYPE_PATCH 0x3 -#define HCI_TYPE_EVNT 0x4 - - -#define HCI_EVENT_PATCHES_DRV_REQ (1) -#define HCI_EVENT_PATCHES_FW_REQ (2) -#define HCI_EVENT_PATCHES_BOOTLOAD_REQ (3) - - -#define HCI_CMND_WLAN_BASE (0x0000) -#define HCI_CMND_WLAN_CONNECT 0x0001 -#define HCI_CMND_WLAN_DISCONNECT 0x0002 -#define HCI_CMND_WLAN_IOCTL_SET_SCANPARAM 0x0003 -#define HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY 0x0004 -#define HCI_CMND_WLAN_IOCTL_ADD_PROFILE 0x0005 -#define HCI_CMND_WLAN_IOCTL_DEL_PROFILE 0x0006 -#define HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS 0x0007 -#define HCI_CMND_EVENT_MASK 0x0008 -#define HCI_CMND_WLAN_IOCTL_STATUSGET 0x0009 -#define HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START 0x000A -#define HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP 0x000B -#define HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX 0x000C -#define HCI_CMND_WLAN_CONFIGURE_PATCH 0x000D - - -#define HCI_CMND_SOCKET_BASE 0x1000 -#define HCI_CMND_SOCKET 0x1001 -#define HCI_CMND_BIND 0x1002 -#define HCI_CMND_RECV 0x1004 -#define HCI_CMND_ACCEPT 0x1005 -#define HCI_CMND_LISTEN 0x1006 -#define HCI_CMND_CONNECT 0x1007 -#define HCI_CMND_BSD_SELECT 0x1008 -#define HCI_CMND_SETSOCKOPT 0x1009 -#define HCI_CMND_GETSOCKOPT 0x100A -#define HCI_CMND_CLOSE_SOCKET 0x100B -#define HCI_CMND_RECVFROM 0x100D -#define HCI_CMND_GETHOSTNAME 0x1010 -#define HCI_CMND_MDNS_ADVERTISE 0x1011 -#define HCI_CMND_GETMSSVALUE 0x1012 - - -#define HCI_DATA_BASE 0x80 - -#define HCI_CMND_SEND (0x01 + HCI_DATA_BASE) -#define HCI_CMND_SENDTO (0x03 + HCI_DATA_BASE) -#define HCI_DATA_BSD_RECVFROM (0x04 + HCI_DATA_BASE) -#define HCI_DATA_BSD_RECV (0x05 + HCI_DATA_BASE) - - -#define HCI_CMND_NVMEM_CBASE (0x0200) - - -#define HCI_CMND_NVMEM_CREATE_ENTRY (0x0203) -#define HCI_CMND_NVMEM_SWAP_ENTRY (0x0205) -#define HCI_CMND_NVMEM_READ (0x0201) -#define HCI_CMND_NVMEM_WRITE (0x0090) -#define HCI_CMND_NVMEM_WRITE_PATCH (0x0204) -#define HCI_CMND_READ_SP_VERSION (0x0207) - -#define HCI_CMND_READ_BUFFER_SIZE 0x400B -#define HCI_CMND_SIMPLE_LINK_START 0x4000 - -#define HCI_CMND_NETAPP_BASE 0x2000 - -#define HCI_NETAPP_DHCP (0x0001 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_PING_SEND (0x0002 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_PING_REPORT (0x0003 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_PING_STOP (0x0004 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_IPCONFIG (0x0005 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_ARP_FLUSH (0x0006 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_SET_DEBUG_LEVEL (0x0008 + HCI_CMND_NETAPP_BASE) -#define HCI_NETAPP_SET_TIMERS (0x0009 + HCI_CMND_NETAPP_BASE) - - - - - - -//***************************************************************************** -// -// Values that can be used as HCI Events defines -// -//***************************************************************************** -#define HCI_EVNT_WLAN_BASE 0x0000 -#define HCI_EVNT_WLAN_CONNECT 0x0001 -#define HCI_EVNT_WLAN_DISCONNECT \ - 0x0002 -#define HCI_EVNT_WLAN_IOCTL_ADD_PROFILE \ - 0x0005 - - -#define HCI_EVNT_SOCKET HCI_CMND_SOCKET -#define HCI_EVNT_BIND HCI_CMND_BIND -#define HCI_EVNT_RECV HCI_CMND_RECV -#define HCI_EVNT_ACCEPT HCI_CMND_ACCEPT -#define HCI_EVNT_LISTEN HCI_CMND_LISTEN -#define HCI_EVNT_CONNECT HCI_CMND_CONNECT -#define HCI_EVNT_SELECT HCI_CMND_BSD_SELECT -#define HCI_EVNT_CLOSE_SOCKET HCI_CMND_CLOSE_SOCKET -#define HCI_EVNT_RECVFROM HCI_CMND_RECVFROM -#define HCI_EVNT_SETSOCKOPT HCI_CMND_SETSOCKOPT -#define HCI_EVNT_GETSOCKOPT HCI_CMND_GETSOCKOPT -#define HCI_EVNT_BSD_GETHOSTBYNAME HCI_CMND_GETHOSTNAME -#define HCI_EVNT_MDNS_ADVERTISE HCI_CMND_MDNS_ADVERTISE -#define HCI_EVNT_GETMSSVALUE HCI_CMND_GETMSSVALUE - -#define HCI_EVNT_SEND 0x1003 -#define HCI_EVNT_WRITE 0x100E -#define HCI_EVNT_SENDTO 0x100F - -#define HCI_EVNT_PATCHES_REQ 0x1000 - -#define HCI_EVNT_UNSOL_BASE 0x4000 - -#define HCI_EVNT_WLAN_UNSOL_BASE (0x8000) - -#define HCI_EVNT_WLAN_UNSOL_CONNECT (0x0001 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_UNSOL_DISCONNECT (0x0002 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_UNSOL_INIT (0x0004 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_TX_COMPLETE (0x0008 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_UNSOL_DHCP (0x0010 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_ASYNC_PING_REPORT (0x0040 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE (0x0080 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_WLAN_KEEPALIVE (0x0200 + HCI_EVNT_WLAN_UNSOL_BASE) -#define HCI_EVNT_BSD_TCP_CLOSE_WAIT (0x0800 + HCI_EVNT_WLAN_UNSOL_BASE) - -#define HCI_EVNT_DATA_UNSOL_FREE_BUFF \ - 0x4100 - -#define HCI_EVNT_NVMEM_CREATE_ENTRY \ - HCI_CMND_NVMEM_CREATE_ENTRY -#define HCI_EVNT_NVMEM_SWAP_ENTRY HCI_CMND_NVMEM_SWAP_ENTRY - -#define HCI_EVNT_NVMEM_READ HCI_CMND_NVMEM_READ -#define HCI_EVNT_NVMEM_WRITE (0x0202) - -#define HCI_EVNT_READ_SP_VERSION \ - HCI_CMND_READ_SP_VERSION - -#define HCI_EVNT_INPROGRESS 0xFFFF - - -#define HCI_DATA_RECVFROM 0x84 -#define HCI_DATA_RECV 0x85 -#define HCI_DATA_NVMEM 0x91 - -#define HCI_EVENT_CC3000_CAN_SHUT_DOWN 0x99 - -//***************************************************************************** -// -// Prototypes for the structures for APIs. -// -//***************************************************************************** - -#define HCI_DATA_HEADER_SIZE (5) -#define HCI_EVENT_HEADER_SIZE (5) -#define HCI_DATA_CMD_HEADER_SIZE (5) -#define HCI_PATCH_HEADER_SIZE (6) - -#define HCI_PACKET_TYPE_OFFSET (0) -#define HCI_PACKET_ARGSIZE_OFFSET (2) -#define HCI_PACKET_LENGTH_OFFSET (3) - - -#define HCI_EVENT_OPCODE_OFFSET (1) -#define HCI_EVENT_LENGTH_OFFSET (3) -#define HCI_EVENT_STATUS_OFFSET (4) -#define HCI_DATA_LENGTH_OFFSET (3) - - - - -//***************************************************************************** -// -// Prototypes for the APIs. -// -//***************************************************************************** - -//***************************************************************************** -// -//! hci_command_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the command's arguments buffer -//! @param ucArgsLength length of the arguments -//! -//! @return none -//! -//! @brief Initiate an HCI command. -// -//***************************************************************************** -extern UINT16 hci_command_send(UINT16 usOpcode, - UINT8 *ucArgs, - UINT8 ucArgsLength); - - -//***************************************************************************** -// -//! hci_data_send -//! -//! @param usOpcode command operation code -//! @param ucArgs pointer to the command's arguments buffer -//! @param usArgsLength length of the arguments -//! @param ucTail pointer to the data buffer -//! @param usTailLength buffer length -//! -//! @return none -//! -//! @brief Initiate an HCI data write operation -// -//***************************************************************************** -extern INT32 hci_data_send(UINT8 ucOpcode, - UINT8 *ucArgs, - UINT16 usArgsLength, - UINT16 usDataLength, - const UINT8 *ucTail, - UINT16 usTailLength); - - -//***************************************************************************** -// -//! hci_data_command_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the data buffer -//! @param ucArgsLength arguments length -//! @param ucDataLength data length -//! -//! @return none -//! -//! @brief Prepare HCI header and initiate an HCI data write operation -// -//***************************************************************************** -extern void hci_data_command_send(UINT16 usOpcode, UINT8 *pucBuff, - UINT8 ucArgsLength, UINT16 ucDataLength); - -//***************************************************************************** -// -//! hci_patch_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the command's arguments buffer -//! @param patch pointer to patch content buffer -//! @param usDataLength data length -//! -//! @return none -//! -//! @brief Prepare HCI header and initiate an HCI patch write operation -// -//***************************************************************************** -extern void hci_patch_send(UINT8 ucOpcode, UINT8 *pucBuff, CHAR *patch, UINT16 usDataLength); - - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_HCI_H__ diff --git a/drivers/cc3000/inc/host_driver_version.h b/drivers/cc3000/inc/host_driver_version.h deleted file mode 100644 index a28d21f1beaca..0000000000000 --- a/drivers/cc3000/inc/host_driver_version.h +++ /dev/null @@ -1,40 +0,0 @@ -/***************************************************************************** -* -* host_driver_version.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_HOST_DRIVER_VERSION_H__ -#define __CC3000_HOST_DRIVER_VERSION_H__ - -#define DRIVER_VERSION_NUMBER 15 - -#endif // __CC3000_HOST_DRIVER_VERSION_H__ diff --git a/drivers/cc3000/inc/inet_ntop.h b/drivers/cc3000/inc/inet_ntop.h deleted file mode 100644 index fa70806206ac8..0000000000000 --- a/drivers/cc3000/inc/inet_ntop.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef __INET_NTOP_H -#define __INET_NTOP_H -char *inet_ntop(int af, const void *addr, char *buf, size_t size); -#endif /* __INET_NTOP_H */ diff --git a/drivers/cc3000/inc/inet_pton.h b/drivers/cc3000/inc/inet_pton.h deleted file mode 100644 index 0896d5d29fd7b..0000000000000 --- a/drivers/cc3000/inc/inet_pton.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef __INET_PTON_H -#define __INET_PTON_H -int inet_pton(int, const char *, void *); -#endif /* __INET_PTON_H */ diff --git a/drivers/cc3000/inc/netapp.h b/drivers/cc3000/inc/netapp.h deleted file mode 100644 index 1e4f265896561..0000000000000 --- a/drivers/cc3000/inc/netapp.h +++ /dev/null @@ -1,343 +0,0 @@ -/***************************************************************************** -* -* netapp.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_NETAPP_H__ -#define __CC3000_NETAPP_H__ - -#include "data_types.h" - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -//***************************************************************************** -// -//! \addtogroup netapp_api -//! @{ -// -//***************************************************************************** - -typedef struct _netapp_dhcp_ret_args_t -{ - UINT8 aucIP[4]; - UINT8 aucSubnetMask[4]; - UINT8 aucDefaultGateway[4]; - UINT8 aucDHCPServer[4]; - UINT8 aucDNSServer[4]; -}tNetappDhcpParams; - -typedef struct _netapp_ipconfig_ret_args_t -{ - UINT8 aucIP[4]; - UINT8 aucSubnetMask[4]; - UINT8 aucDefaultGateway[4]; - UINT8 aucDHCPServer[4]; - UINT8 aucDNSServer[4]; - UINT8 uaMacAddr[6]; - UINT8 uaSSID[32]; -}tNetappIpconfigRetArgs; - - -/*Ping send report parameters*/ -typedef struct _netapp_pingreport_args -{ - UINT32 packets_sent; - UINT32 packets_received; - UINT32 min_round_time; - UINT32 max_round_time; - UINT32 avg_round_time; -} netapp_pingreport_args_t; - - -//***************************************************************************** -// -//! netapp_config_mac_adrress -//! -//! @param mac device mac address, 6 bytes. Saved: yes -//! -//! @return return on success 0, otherwise error. -//! -//! @brief Configure device MAC address and store it in NVMEM. -//! The value of the MAC address configured through the API will -//! be stored in CC3000 non volatile memory, thus preserved -//! over resets. -// -//***************************************************************************** -extern INT32 netapp_config_mac_adrress( UINT8 *mac ); - -//***************************************************************************** -// -//! netapp_dhcp -//! -//! @param aucIP device mac address, 6 bytes. Saved: yes -//! @param aucSubnetMask device mac address, 6 bytes. Saved: yes -//! @param aucDefaultGateway device mac address, 6 bytes. Saved: yes -//! @param aucDNSServer device mac address, 6 bytes. Saved: yes -//! -//! @return return on success 0, otherwise error. -//! -//! @brief netapp_dhcp is used to configure the network interface, -//! static or dynamic (DHCP).\n In order to activate DHCP mode, -//! aucIP, aucSubnetMask, aucDefaultGateway must be 0. -//! The default mode of CC3000 is DHCP mode. -//! Note that the configuration is saved in non volatile memory -//! and thus preserved over resets. -//! -//! @note If the mode is altered a reset of CC3000 device is required -//! in order to apply changes.\nAlso note that asynchronous event -//! of DHCP_EVENT, which is generated when an IP address is -//! allocated either by the DHCP server or due to static -//! allocation is generated only upon a connection to the -//! AP was established. -//! -//***************************************************************************** -extern INT32 netapp_dhcp(UINT32 *aucIP, UINT32 *aucSubnetMask,UINT32 *aucDefaultGateway, UINT32 *aucDNSServer); - - - -//***************************************************************************** -// -//! netapp_timeout_values -//! -//! @param aucDHCP DHCP lease time request, also impact -//! the DHCP renew timeout. Range: [0-0xffffffff] seconds, -//! 0 or 0xffffffff == infinity lease timeout. -//! Resolution:10 seconds. Influence: only after -//! reconnecting to the AP. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 seconds. -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 14400 seconds. -//! -//! @param aucARP ARP refresh timeout, if ARP entry is not updated by -//! incoming packet, the ARP entry will be deleted by -//! the end of the timeout. -//! Range: [0-0xffffffff] seconds, 0 == infinity ARP timeout -//! Resolution: 10 seconds. Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 seconds -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 3600 seconds. -//! -//! @param aucKeepalive Keepalive event sent by the end of keepalive timeout -//! Range: [0-0xffffffff] seconds, 0 == infinity timeout -//! Resolution: 10 seconds. -//! Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 sec -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 10 seconds. -//! -//! @param aucInactivity Socket inactivity timeout, socket timeout is -//! refreshed by incoming or outgoing packet, by the -//! end of the socket timeout the socket will be closed -//! Range: [0-0xffffffff] sec, 0 == infinity timeout. -//! Resolution: 10 seconds. Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 sec -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 60 seconds. -//! -//! @return return on success 0, otherwise error. -//! -//! @brief Set new timeout values. Function set new timeout values for: -//! DHCP lease timeout, ARP refresh timeout, keepalive event -//! timeout and socket inactivity timeout -//! -//! @note If a parameter set to non zero value which is less than 10s, -//! it will be set automatically to 10s. -//! -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern INT32 netapp_timeout_values(UINT32 *aucDHCP, UINT32 *aucARP, UINT32 *aucKeepalive, UINT32 *aucInactivity); -#endif - -//***************************************************************************** -// -//! netapp_ping_send -//! -//! @param ip destination IP address -//! @param pingAttempts number of echo requests to send -//! @param pingSize send buffer size which may be up to 1400 bytes -//! @param pingTimeout Time to wait for a response,in milliseconds. -//! -//! @return return on success 0, otherwise error. -//! -//! @brief send ICMP ECHO_REQUEST to network hosts -//! -//! @note If an operation finished successfully asynchronous ping report -//! event will be generated. The report structure is as defined -//! by structure netapp_pingreport_args_t. -//! -//! @warning Calling this function while a previous Ping Requests are in -//! progress will stop the previous ping request. -//***************************************************************************** - - #ifndef CC3000_TINY_DRIVER -extern INT32 netapp_ping_send(UINT32 *ip, UINT32 ulPingAttempts, UINT32 ulPingSize, UINT32 ulPingTimeout); -#endif - -//***************************************************************************** -// -//! netapp_ping_stop -//! -//! @param none -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief Stop any ping request. -//! -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -extern INT32 netapp_ping_stop(); -#endif -//***************************************************************************** -// -//! netapp_ping_report -//! -//! @param none -//! -//! @return none -//! -//! @brief Request for ping status. This API triggers the CC3000 to send -//! asynchronous events: HCI_EVNT_WLAN_ASYNC_PING_REPORT. -//! This event will carry the report structure: -//! netapp_pingreport_args_t. This structure is filled in with ping -//! results up till point of triggering API. -//! netapp_pingreport_args_t:\n packets_sent - echo sent, -//! packets_received - echo reply, min_round_time - minimum -//! round time, max_round_time - max round time, -//! avg_round_time - average round time -//! -//! @note When a ping operation is not active, the returned structure -//! fields are 0. -//! -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern void netapp_ping_report(); -#endif - - -//***************************************************************************** -// -//! netapp_ipconfig -//! -//! @param[out] ipconfig This argument is a pointer to a -//! tNetappIpconfigRetArgs structure. This structure is -//! filled in with the network interface configuration. -//! tNetappIpconfigRetArgs:\n aucIP - ip address, -//! aucSubnetMask - mask, aucDefaultGateway - default -//! gateway address, aucDHCPServer - dhcp server address -//! aucDNSServer - dns server address, uaMacAddr - mac -//! address, uaSSID - connected AP ssid -//! -//! @return none -//! -//! @brief Obtain the CC3000 Network interface information. -//! Note that the information is available only after the WLAN -//! connection was established. Calling this function before -//! associated, will cause non-defined values to be returned. -//! -//! @note The function is useful for figuring out the IP Configuration of -//! the device when DHCP is used and for figuring out the SSID of -//! the Wireless network the device is associated with. -//! -//***************************************************************************** - -extern void netapp_ipconfig( tNetappIpconfigRetArgs * ipconfig ); - - -//***************************************************************************** -// -//! netapp_arp_flush -//! -//! @param none -//! -//! @return none -//! -//! @brief Flushes ARP table -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -extern INT32 netapp_arp_flush(); -#endif - - -//***************************************************************************** -// -//! netapp_set_debug_level -//! -//! @param[in] level debug level. Bitwise [0-8], -//! 0(disable)or 1(enable).\n Bitwise map: 0 - Critical -//! message, 1 information message, 2 - core messages, 3 - -//! HCI messages, 4 - Network stack messages, 5 - wlan -//! messages, 6 - wlan driver messages, 7 - epprom messages, -//! 8 - general messages. Default: 0x13f. Saved: no -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Debug messages sent via the UART debug channel, this function -//! enable/disable the debug level -//! -//***************************************************************************** - - -#ifndef CC3000_TINY_DRIVER -INT32 netapp_set_debug_level(UINT32 ulLevel); -#endif -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** - - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_NETAPP_H__ - diff --git a/drivers/cc3000/inc/nvmem.h b/drivers/cc3000/inc/nvmem.h deleted file mode 100644 index b99a2e7b9ec5b..0000000000000 --- a/drivers/cc3000/inc/nvmem.h +++ /dev/null @@ -1,248 +0,0 @@ -/***************************************************************************** -* -* nvmem.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_NVRAM_H__ -#define __CC3000_NVRAM_H__ - -#include "cc3000_common.h" - - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - - -//***************************************************************************** -// -//! \addtogroup nvmem_api -//! @{ -// -//***************************************************************************** - -/**************************************************************************** -** -** Definitions for File IDs -** -****************************************************************************/ -/* NVMEM file ID - system files*/ -#define NVMEM_NVS_FILEID (0) -#define NVMEM_NVS_SHADOW_FILEID (1) -#define NVMEM_WLAN_CONFIG_FILEID (2) -#define NVMEM_WLAN_CONFIG_SHADOW_FILEID (3) -#define NVMEM_WLAN_DRIVER_SP_FILEID (4) -#define NVMEM_WLAN_FW_SP_FILEID (5) -#define NVMEM_MAC_FILEID (6) -#define NVMEM_FRONTEND_VARS_FILEID (7) -#define NVMEM_IP_CONFIG_FILEID (8) -#define NVMEM_IP_CONFIG_SHADOW_FILEID (9) -#define NVMEM_BOOTLOADER_SP_FILEID (10) -#define NVMEM_RM_FILEID (11) - -/* NVMEM file ID - user files*/ -#define NVMEM_AES128_KEY_FILEID (12) -#define NVMEM_SHARED_MEM_FILEID (13) - -/* max entry in order to invalid nvmem */ -#define NVMEM_MAX_ENTRY (16) - - -//***************************************************************************** -// -//! nvmem_read -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_NVS_FILEID, NVMEM_NVS_SHADOW_FILEID, -//! NVMEM_WLAN_CONFIG_FILEID, NVMEM_WLAN_CONFIG_SHADOW_FILEID, -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! NVMEM_MAC_FILEID, NVMEM_FRONTEND_VARS_FILEID, -//! NVMEM_IP_CONFIG_FILEID, NVMEM_IP_CONFIG_SHADOW_FILEID, -//! NVMEM_BOOTLOADER_SP_FILEID, NVMEM_RM_FILEID, -//! and user files 12-15. -//! @param ulLength number of bytes to read -//! @param ulOffset ulOffset in file from where to read -//! @param buff output buffer pointer -//! -//! @return on success 0, error otherwise. -//! -//! @brief Reads data from the file referred by the ulFileId parameter. -//! Reads data from file ulOffset till length. Err if the file can't -//! be used, is invalid, or if the read is out of bounds. -//! -//***************************************************************************** - -extern INT32 nvmem_read(UINT32 file_id, UINT32 length, UINT32 offset, UINT8 *buff); - -//***************************************************************************** -// -//! nvmem_write -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! NVMEM_MAC_FILEID, NVMEM_BOOTLOADER_SP_FILEID, -//! and user files 12-15. -//! @param ulLength number of bytes to write -//! @param ulEntryOffset offset in file to start write operation from -//! @param buff data to write -//! -//! @return on success 0, error otherwise. -//! -//! @brief Write data to nvmem. -//! writes data to file referred by the ulFileId parameter. -//! Writes data to file ulOffset till ulLength.The file id will be -//! marked invalid till the write is done. The file entry doesn't -//! need to be valid - only allocated. -//! -//***************************************************************************** - -extern INT32 nvmem_write(UINT32 ulFileId, UINT32 ulLength, UINT32 ulEntryOffset, UINT8 *buff); - - -//***************************************************************************** -// -//! nvmem_set_mac_address -//! -//! @param mac mac address to be set -//! -//! @return on success 0, error otherwise. -//! -//! @brief Write MAC address to EEPROM. -//! mac address as appears over the air (OUI first) -//! -//***************************************************************************** -extern UINT8 nvmem_set_mac_address(UINT8 *mac); - - -//***************************************************************************** -// -//! nvmem_get_mac_address -//! -//! @param[out] mac mac address -//! -//! @return on success 0, error otherwise. -//! -//! @brief Read MAC address from EEPROM. -//! mac address as appears over the air (OUI first) -//! -//***************************************************************************** -extern UINT8 nvmem_get_mac_address(UINT8 *mac); - - -//***************************************************************************** -// -//! nvmem_write_patch -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! @param spLength number of bytes to write -//! @param spData SP data to write -//! -//! @return on success 0, error otherwise. -//! -//! @brief program a patch to a specific file ID. -//! The SP data is assumed to be organized in 2-dimensional. -//! Each line is SP_PORTION_SIZE bytes long. Actual programming is -//! applied in SP_PORTION_SIZE bytes portions. -//! -//***************************************************************************** -extern UINT8 nvmem_write_patch(UINT32 ulFileId, UINT32 spLength, const UINT8 *spData); - - -//***************************************************************************** -// -//! nvmem_read_sp_version -//! -//! @param[out] patchVer first number indicates package ID and the second -//! number indicates package build number -//! -//! @return on success 0, error otherwise. -//! -//! @brief Read patch version. read package version (WiFi FW patch, -//! driver-supplicant-NS patch, bootloader patch) -//! -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern UINT8 nvmem_read_sp_version(UINT8* patchVer); -#endif - -//***************************************************************************** -// -//! nvmem_create_entry -//! -//! @param ulFileId nvmem file Id:\n -//! * NVMEM_AES128_KEY_FILEID: 12 -//! * NVMEM_SHARED_MEM_FILEID: 13 -//! * and fileIDs 14 and 15 -//! @param ulNewLen entry ulLength -//! -//! @return on success 0, error otherwise. -//! -//! @brief Create new file entry and allocate space on the NVMEM. -//! Applies only to user files. -//! Modify the size of file. -//! If the entry is unallocated - allocate it to size -//! ulNewLen (marked invalid). -//! If it is allocated then deallocate it first. -//! To just mark the file as invalid without resizing - -//! set ulNewLen=0. -//! -//***************************************************************************** -extern INT32 nvmem_create_entry(UINT32 file_id, UINT32 newlen); - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** - - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** - - -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_NVRAM_H__ diff --git a/drivers/cc3000/inc/patch_prog.h b/drivers/cc3000/inc/patch_prog.h deleted file mode 100644 index 0a141a0cb535c..0000000000000 --- a/drivers/cc3000/inc/patch_prog.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __CC3000_PATCH_PROG_H__ -#define __CC3000_PATCH_PROG_H__ -extern unsigned short fw_length; -extern const unsigned char fw_patch[]; - -extern unsigned short drv_length; -extern const unsigned char wlan_drv_patch[]; -extern const unsigned char cRMdefaultParams[128]; - -void patch_prog_start(); -#endif //__CC3000_PATCH_PROG_H__ diff --git a/drivers/cc3000/inc/security.h b/drivers/cc3000/inc/security.h deleted file mode 100644 index cd1baf55416f5..0000000000000 --- a/drivers/cc3000/inc/security.h +++ /dev/null @@ -1,130 +0,0 @@ -/***************************************************************************** -* -* security.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_SECURITY__ -#define __CC3000_SECURITY__ - -#include "nvmem.h" - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - - -#define AES128_KEY_SIZE 16 - -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG - - -//***************************************************************************** -// -//! aes_encrypt -//! -//! @param[in] key AES128 key of size 16 bytes -//! @param[in\out] state 16 bytes of plain text and cipher text -//! -//! @return none -//! -//! @brief AES128 encryption: -//! Given AES128 key and 16 bytes plain text, cipher text of 16 bytes -//! is computed. The AES implementation is in mode ECB (Electronic -//! Code Book). -//! -//! -//***************************************************************************** -extern void aes_encrypt(UINT8 *state, UINT8 *key); - -//***************************************************************************** -// -//! aes_decrypt -//! -//! @param[in] key AES128 key of size 16 bytes -//! @param[in\out] state 16 bytes of cipher text and plain text -//! -//! @return none -//! -//! @brief AES128 decryption: -//! Given AES128 key and 16 bytes cipher text, plain text of 16 bytes -//! is computed The AES implementation is in mode ECB -//! (Electronic Code Book). -//! -//! -//***************************************************************************** -extern void aes_decrypt(UINT8 *state, UINT8 *key); - - -//***************************************************************************** -// -//! aes_read_key -//! -//! @param[out] key AES128 key of size 16 bytes -//! -//! @return on success 0, error otherwise. -//! -//! @brief Reads AES128 key from EEPROM -//! Reads the AES128 key from fileID #12 in EEPROM -//! returns an error if the key does not exist. -//! -//! -//***************************************************************************** -extern INT32 aes_read_key(UINT8 *key); - -//***************************************************************************** -// -//! aes_write_key -//! -//! @param[out] key AES128 key of size 16 bytes -//! -//! @return on success 0, error otherwise. -//! -//! @brief writes AES128 key from EEPROM -//! Writes the AES128 key to fileID #12 in EEPROM -//! -//! -//***************************************************************************** -extern INT32 aes_write_key(UINT8 *key); - -#endif //CC3000_UNENCRYPTED_SMART_CONFIG - -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif diff --git a/drivers/cc3000/inc/socket.h b/drivers/cc3000/inc/socket.h deleted file mode 100644 index 96c814bf77a08..0000000000000 --- a/drivers/cc3000/inc/socket.h +++ /dev/null @@ -1,676 +0,0 @@ -/***************************************************************************** -* -* socket.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_SOCKET_H__ -#define __CC3000_SOCKET_H__ - -#include "cc3000_common.h" - -//***************************************************************************** -// -//! \addtogroup socket_api -//! @{ -// -//***************************************************************************** - - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -#define HOSTNAME_MAX_LENGTH (230) // 230 bytes + header shouldn't exceed 8 bit value - -//--------- Address Families -------- - -#define AF_INET 2 -#define AF_INET6 23 - -//------------ Socket Types ------------ - -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 -#define SOCK_RAW 3 // Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers -#define SOCK_RDM 4 -#define SOCK_SEQPACKET 5 - -//----------- Socket Protocol ---------- - -#define IPPROTO_IP 0 // dummy for IP -#define IPPROTO_ICMP 1 // control message protocol -#define IPPROTO_IPV4 IPPROTO_IP // IP inside IP -#define IPPROTO_TCP 6 // tcp -#define IPPROTO_UDP 17 // user datagram protocol -#define IPPROTO_IPV6 41 // IPv6 in IPv6 -#define IPPROTO_NONE 59 // No next header -#define IPPROTO_RAW 255 // raw IP packet -#define IPPROTO_MAX 256 - -//----------- Socket retunr codes ----------- - -#define SOC_ERROR (-1) // error -#define SOC_IN_PROGRESS (-2) // socket in progress - -//----------- Socket Options ----------- -#define SOL_SOCKET 0xffff // socket level -#define SOCKOPT_RECV_NONBLOCK 0 // recv non block mode, set SOCK_ON or SOCK_OFF (default block mode) -#define SOCKOPT_RECV_TIMEOUT 1 // optname to configure recv and recvfromtimeout -#define SOCKOPT_ACCEPT_NONBLOCK 2 // accept non block mode, set SOCK_ON or SOCK_OFF (default block mode) -#define SOCK_ON 0 // socket non-blocking mode is enabled -#define SOCK_OFF 1 // socket blocking mode is enabled - -#define MAX_PACKET_SIZE 1500 -#define MAX_LISTEN_QUEUE 4 - -#define IOCTL_SOCKET_EVENTMASK - -#define ENOBUFS 55 // No buffer space available - -#define __FD_SETSIZE 32 - -#define ASIC_ADDR_LEN 8 - -#define NO_QUERY_RECIVED -3 - - -typedef struct _in_addr_t -{ - UINT32 s_addr; // load with inet_aton() -} in_addr; - -typedef struct _sockaddr_t -{ - UINT16 sa_family; - UINT8 sa_data[14]; -} sockaddr; - -typedef struct _sockaddr_in_t -{ - INT16 sin_family; // e.g. AF_INET - UINT16 sin_port; // e.g. htons(3490) - in_addr sin_addr; // see struct in_addr, below - CHAR sin_zero[8]; // zero this if you want to -} sockaddr_in; - -typedef UINT32 socklen_t; - -// The fd_set member is required to be an array of INT32s. -typedef INT32 __fd_mask; - -// It's easier to assume 8-bit bytes than to get CHAR_BIT. -#define __NFDBITS (8 * sizeof (__fd_mask)) -#define __FDELT(d) ((d) / __NFDBITS) -#define __FDMASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS)) - -// fd_set for select and pselect. -typedef struct -{ - __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS]; -#define __FDS_BITS(set) ((set)->fds_bits) -} fd_set; - -// We don't use `memset' because this would require a prototype and -// the array isn't too big. -#define __FD_ZERO(set) \ - do { \ - UINT16 __i; \ - fd_set *__arr = (set); \ - for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \ - __FDS_BITS (__arr)[__i] = 0; \ - } while (0) -#define __FD_SET(d, set) (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d)) -#define __FD_CLR(d, set) (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d)) -#define __FD_ISSET(d, set) (__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d)) - -// Access macros for 'fd_set'. -#define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp) -#define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp) -#define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp) -#define FD_ZERO(fdsetp) __FD_ZERO (fdsetp) - -//Use in case of Big Endian only - -#define htonl(A) ((((UINT32)(A) & 0xff000000) >> 24) | \ - (((UINT32)(A) & 0x00ff0000) >> 8) | \ - (((UINT32)(A) & 0x0000ff00) << 8) | \ - (((UINT32)(A) & 0x000000ff) << 24)) - -#define ntohl htonl - -//Use in case of Big Endian only -#define htons(A) ((((UINT32)(A) & 0xff00) >> 8) | \ - (((UINT32)(A) & 0x00ff) << 8)) - - -#define ntohs htons - -// mDNS port - 5353 mDNS multicast address - 224.0.0.251 -#define SET_mDNS_ADD(sockaddr) sockaddr.sa_data[0] = 0x14; \ - sockaddr.sa_data[1] = 0xe9; \ - sockaddr.sa_data[2] = 0xe0; \ - sockaddr.sa_data[3] = 0x0; \ - sockaddr.sa_data[4] = 0x0; \ - sockaddr.sa_data[5] = 0xfb; - - -//***************************************************************************** -// -// Prototypes for the APIs. -// -//***************************************************************************** - -//***************************************************************************** -// -//! socket -//! -//! @param domain selects the protocol family which will be used for -//! communication. On this version only AF_INET is supported -//! @param type specifies the communication semantics. On this version -//! only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW are supported -//! @param protocol specifies a particular protocol to be used with the -//! socket IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW are -//! supported. -//! -//! @return On success, socket handle that is used for consequent socket -//! operations. On error, -1 is returned. -//! -//! @brief create an endpoint for communication -//! The socket function creates a socket that is bound to a specific -//! transport service provider. This function is called by the -//! application layer to obtain a socket handle. -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(socket)(INT32 domain, INT32 type, INT32 protocol); - -//***************************************************************************** -// -//! closesocket -//! -//! @param sd socket handle. -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief The socket function closes a created socket. -// -//***************************************************************************** -extern INT32 CC3000_EXPORT(closesocket)(INT32 sd); - -//***************************************************************************** -// -//! accept -//! -//! @param[in] sd socket descriptor (handle) -//! @param[out] addr the argument addr is a pointer to a sockaddr structure -//! This structure is filled in with the address of the -//! peer socket, as known to the communications layer. -//! determined. The exact format of the address returned -//! addr is by the socket's address sockaddr. -//! On this version only AF_INET is supported. -//! This argument returns in network order. -//! @param[out] addrlen the addrlen argument is a value-result argument: -//! it should initially contain the size of the structure -//! pointed to by addr. -//! -//! @return For socket in blocking mode: -//! On success, socket handle. on failure negative -//! For socket in non-blocking mode: -//! - On connection establishment, socket handle -//! - On connection pending, SOC_IN_PROGRESS (-2) -//! - On failure, SOC_ERROR (-1) -//! -//! @brief accept a connection on a socket: -//! This function is used with connection-based socket types -//! (SOCK_STREAM). It extracts the first connection request on the -//! queue of pending connections, creates a new connected socket, and -//! returns a new file descriptor referring to that socket. -//! The newly created socket is not in the listening state. -//! The original socket sd is unaffected by this call. -//! The argument sd is a socket that has been created with socket(), -//! bound to a local address with bind(), and is listening for -//! connections after a listen(). The argument addr is a pointer -//! to a sockaddr structure. This structure is filled in with the -//! address of the peer socket, as known to the communications layer. -//! The exact format of the address returned addr is determined by the -//! socket's address family. The addrlen argument is a value-result -//! argument: it should initially contain the size of the structure -//! pointed to by addr, on return it will contain the actual -//! length (in bytes) of the address returned. -//! -//! @sa socket ; bind ; listen -// -//***************************************************************************** -extern INT32 CC3000_EXPORT(accept)(INT32 sd, sockaddr *addr, socklen_t *addrlen); - -//***************************************************************************** -// -//! bind -//! -//! @param[in] sd socket descriptor (handle) -//! @param[out] addr specifies the destination address. On this version -//! only AF_INET is supported. -//! @param[out] addrlen contains the size of the structure pointed to by addr. -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief assign a name to a socket -//! This function gives the socket the local address addr. -//! addr is addrlen bytes long. Traditionally, this is called when a -//! socket is created with socket, it exists in a name space (address -//! family) but has no name assigned. -//! It is necessary to assign a local address before a SOCK_STREAM -//! socket may receive connections. -//! -//! @sa socket ; accept ; listen -// -//***************************************************************************** -extern INT32 CC3000_EXPORT(bind)(INT32 sd, const sockaddr *addr, INT32 addrlen); - -//***************************************************************************** -// -//! listen -//! -//! @param[in] sd socket descriptor (handle) -//! @param[in] backlog specifies the listen queue depth. On this version -//! backlog is not supported. -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief listen for connections on a socket -//! The willingness to accept incoming connections and a queue -//! limit for incoming connections are specified with listen(), -//! and then the connections are accepted with accept. -//! The listen() call applies only to sockets of type SOCK_STREAM -//! The backlog parameter defines the maximum length the queue of -//! pending connections may grow to. -//! -//! @sa socket ; accept ; bind -//! -//! @note On this version, backlog is not supported -// -//***************************************************************************** -extern INT32 CC3000_EXPORT(listen)(INT32 sd, INT32 backlog); - -//***************************************************************************** -// -//! gethostbyname -//! -//! @param[in] hostname host name -//! @param[in] usNameLen name length -//! @param[out] out_ip_addr This parameter is filled in with host IP address. -//! In case that host name is not resolved, -//! out_ip_addr is zero. -//! @return On success, positive is returned. On error, negative is returned -//! -//! @brief Get host IP by name. Obtain the IP Address of machine on network, -//! by its name. -//! -//! @note On this version, only blocking mode is supported. Also note that -//! the function requires DNS server to be configured prior to its usage. -// -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern INT16 CC3000_EXPORT(gethostbyname)(CHAR * hostname, UINT16 usNameLen, UINT32* out_ip_addr); -#endif - - -//***************************************************************************** -// -//! connect -//! -//! @param[in] sd socket descriptor (handle) -//! @param[in] addr specifies the destination addr. On this version -//! only AF_INET is supported. -//! @param[out] addrlen contains the size of the structure pointed to by addr -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief initiate a connection on a socket -//! Function connects the socket referred to by the socket descriptor -//! sd, to the address specified by addr. The addrlen argument -//! specifies the size of addr. The format of the address in addr is -//! determined by the address space of the socket. If it is of type -//! SOCK_DGRAM, this call specifies the peer with which the socket is -//! to be associated; this address is that to which datagrams are to be -//! sent, and the only address from which datagrams are to be received. -//! If the socket is of type SOCK_STREAM, this call attempts to make a -//! connection to another socket. The other socket is specified by -//! address, which is an address in the communications space of the -//! socket. Note that the function implements only blocking behavior -//! thus the caller will be waiting either for the connection -//! establishment or for the connection establishment failure. -//! -//! @sa socket -// -//***************************************************************************** -extern INT32 CC3000_EXPORT(connect)(INT32 sd, const sockaddr *addr, INT32 addrlen); - -//***************************************************************************** -// -//! select -//! -//! @param[in] nfds the highest-numbered file descriptor in any of the -//! three sets, plus 1. -//! @param[out] writesds socket descriptors list for write monitoring -//! @param[out] readsds socket descriptors list for read monitoring -//! @param[out] exceptsds socket descriptors list for exception monitoring -//! @param[in] timeout is an upper bound on the amount of time elapsed -//! before select() returns. Null means infinity -//! timeout. The minimum timeout is 5 milliseconds, -//! less than 5 milliseconds will be set -//! automatically to 5 milliseconds. -//! @return On success, select() returns the number of file descriptors -//! contained in the three returned descriptor sets (that is, the -//! total number of bits that are set in readfds, writefds, -//! exceptfds) which may be zero if the timeout expires before -//! anything interesting happens. -//! On error, -1 is returned. -//! *readsds - return the sockets on which Read request will -//! return without delay with valid data. -//! *writesds - return the sockets on which Write request -//! will return without delay. -//! *exceptsds - return the sockets which closed recently. -//! -//! @brief Monitor socket activity -//! Select allow a program to monitor multiple file descriptors, -//! waiting until one or more of the file descriptors become -//! "ready" for some class of I/O operation -//! -//! @Note If the timeout value set to less than 5ms it will automatically set -//! to 5ms to prevent overload of the system -//! -//! @sa socket -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(select)(INT32 nfds, fd_set *readsds, fd_set *writesds, - fd_set *exceptsds, struct cc3000_timeval *timeout); - -//***************************************************************************** -// -//! setsockopt -//! -//! @param[in] sd socket handle -//! @param[in] level defines the protocol level for this option -//! @param[in] optname defines the option name to Interrogate -//! @param[in] optval specifies a value for the option -//! @param[in] optlen specifies the length of the option value -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief set socket options -//! This function manipulate the options associated with a socket. -//! Options may exist at multiple protocol levels; they are always -//! present at the uppermost socket level. -//! When manipulating socket options the level at which the option -//! resides and the name of the option must be specified. -//! To manipulate options at the socket level, level is specified as -//! SOL_SOCKET. To manipulate options at any other level the protocol -//! number of the appropriate protocol controlling the option is -//! supplied. For example, to indicate that an option is to be -//! interpreted by the TCP protocol, level should be set to the -//! protocol number of TCP; -//! The parameters optval and optlen are used to access optval - -//! use for setsockopt(). For getsockopt() they identify a buffer -//! in which the value for the requested option(s) are to -//! be returned. For getsockopt(), optlen is a value-result -//! parameter, initially containing the size of the buffer -//! pointed to by option_value, and modified on return to -//! indicate the actual size of the value returned. If no option -//! value is to be supplied or returned, option_value may be NULL. -//! -//! @Note On this version the following two socket options are enabled: -//! The only protocol level supported in this version -//! is SOL_SOCKET (level). -//! 1. SOCKOPT_RECV_TIMEOUT (optname) -//! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout -//! in milliseconds. -//! In that case optval should be pointer to UINT32. -//! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on -//! or off. -//! In that case optval should be SOCK_ON or SOCK_OFF (optval). -//! -//! @sa getsockopt -// -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern INT16 CC3000_EXPORT(setsockopt)(INT32 sd, INT32 level, INT32 optname, const void *optval, - socklen_t optlen); -#endif -//***************************************************************************** -// -//! getsockopt -//! -//! @param[in] sd socket handle -//! @param[in] level defines the protocol level for this option -//! @param[in] optname defines the option name to Interrogate -//! @param[out] optval specifies a value for the option -//! @param[out] optlen specifies the length of the option value -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief set socket options -//! This function manipulate the options associated with a socket. -//! Options may exist at multiple protocol levels; they are always -//! present at the uppermost socket level. -//! When manipulating socket options the level at which the option -//! resides and the name of the option must be specified. -//! To manipulate options at the socket level, level is specified as -//! SOL_SOCKET. To manipulate options at any other level the protocol -//! number of the appropriate protocol controlling the option is -//! supplied. For example, to indicate that an option is to be -//! interpreted by the TCP protocol, level should be set to the -//! protocol number of TCP; -//! The parameters optval and optlen are used to access optval - -//! use for setsockopt(). For getsockopt() they identify a buffer -//! in which the value for the requested option(s) are to -//! be returned. For getsockopt(), optlen is a value-result -//! parameter, initially containing the size of the buffer -//! pointed to by option_value, and modified on return to -//! indicate the actual size of the value returned. If no option -//! value is to be supplied or returned, option_value may be NULL. -//! -//! @Note On this version the following two socket options are enabled: -//! The only protocol level supported in this version -//! is SOL_SOCKET (level). -//! 1. SOCKOPT_RECV_TIMEOUT (optname) -//! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout -//! in milliseconds. -//! In that case optval should be pointer to UINT32. -//! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on -//! or off. -//! In that case optval should be SOCK_ON or SOCK_OFF (optval). -//! -//! @sa setsockopt -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(getsockopt)(INT32 sd, INT32 level, INT32 optname, void *optval, - socklen_t *optlen); - -//***************************************************************************** -// -//! recv -//! -//! @param[in] sd socket handle -//! @param[out] buf Points to the buffer where the message should be stored -//! @param[in] len Specifies the length in bytes of the buffer pointed to -//! by the buffer argument. -//! @param[in] flags Specifies the type of message reception. -//! On this version, this parameter is not supported. -//! -//! @return Return the number of bytes received, or -1 if an error -//! occurred -//! -//! @brief function receives a message from a connection-mode socket -//! -//! @sa recvfrom -//! -//! @Note On this version, only blocking mode is supported. -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(recv)(INT32 sd, void *buf, INT32 len, INT32 flags); - -//***************************************************************************** -// -//! recvfrom -//! -//! @param[in] sd socket handle -//! @param[out] buf Points to the buffer where the message should be stored -//! @param[in] len Specifies the length in bytes of the buffer pointed to -//! by the buffer argument. -//! @param[in] flags Specifies the type of message reception. -//! On this version, this parameter is not supported. -//! @param[in] from pointer to an address structure indicating the source -//! address: sockaddr. On this version only AF_INET is -//! supported. -//! @param[in] fromlen source address structure size -//! -//! @return Return the number of bytes received, or -1 if an error -//! occurred -//! -//! @brief read data from socket -//! function receives a message from a connection-mode or -//! connectionless-mode socket. Note that raw sockets are not -//! supported. -//! -//! @sa recv -//! -//! @Note On this version, only blocking mode is supported. -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(recvfrom)(INT32 sd, void *buf, INT32 len, INT32 flags, sockaddr *from, - socklen_t *fromlen); - -//***************************************************************************** -// -//! send -//! -//! @param sd socket handle -//! @param buf Points to a buffer containing the message to be sent -//! @param len message size in bytes -//! @param flags On this version, this parameter is not supported -//! -//! @return Return the number of bytes transmitted, or -1 if an -//! error occurred -//! -//! @brief Write data to TCP socket -//! This function is used to transmit a message to another -//! socket. -//! -//! @Note On this version, only blocking mode is supported. -//! -//! @sa sendto -// -//***************************************************************************** - -extern INT16 CC3000_EXPORT(send)(INT32 sd, const void *buf, INT32 len, INT32 flags); - -//***************************************************************************** -// -//! sendto -//! -//! @param sd socket handle -//! @param buf Points to a buffer containing the message to be sent -//! @param len message size in bytes -//! @param flags On this version, this parameter is not supported -//! @param to pointer to an address structure indicating the destination -//! address: sockaddr. On this version only AF_INET is -//! supported. -//! @param tolen destination address structure size -//! -//! @return Return the number of bytes transmitted, or -1 if an -//! error occurred -//! -//! @brief Write data to TCP socket -//! This function is used to transmit a message to another -//! socket. -//! -//! @Note On this version, only blocking mode is supported. -//! -//! @sa send -// -//***************************************************************************** - -extern INT16 CC3000_EXPORT(sendto)(INT32 sd, const void *buf, INT32 len, INT32 flags, - const sockaddr *to, socklen_t tolen); - -//***************************************************************************** -// -//! mdnsAdvertiser -//! -//! @param[in] mdnsEnabled flag to enable/disable the mDNS feature -//! @param[in] deviceServiceName Service name as part of the published -//! canonical domain name -//! @param[in] deviceServiceNameLength Length of the service name - up to 32 chars -//! -//! -//! @return On success, zero is returned, return SOC_ERROR if socket was not -//! opened successfully, or if an error occurred. -//! -//! @brief Set CC3000 in mDNS advertiser mode in order to advertise itself. -// -//***************************************************************************** -extern INT16 CC3000_EXPORT(mdnsAdvertiser)(UINT16 mdnsEnabled, CHAR * deviceServiceName, UINT16 deviceServiceNameLength); - - -//***************************************************************************** -// -//! getmssvalue -//! -//! @param[in] sd socket descriptor -//! -//! @return On success, returns the MSS value of a TCP connection -//! -//! @brief Returns the MSS value of a TCP connection according to the socket descriptor -// -//***************************************************************************** -extern UINT16 CC3000_EXPORT(getmssvalue) (INT32 sd); - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __SOCKET_H__ diff --git a/drivers/cc3000/inc/wlan.h b/drivers/cc3000/inc/wlan.h deleted file mode 100644 index 48d195b32ae80..0000000000000 --- a/drivers/cc3000/inc/wlan.h +++ /dev/null @@ -1,518 +0,0 @@ -/***************************************************************************** -* -* wlan.h - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -#ifndef __CC3000_WLAN_H__ -#define __CC3000_WLAN_H__ - -#include "cc3000_common.h" - -//***************************************************************************** -// -// If building with a C++ compiler, make all of the definitions in this header -// have a C binding. -// -//***************************************************************************** -#ifdef __cplusplus -extern "C" { -#endif - -#define WLAN_SEC_UNSEC (0) -#define WLAN_SEC_WEP (1) -#define WLAN_SEC_WPA (2) -#define WLAN_SEC_WPA2 (3) -//***************************************************************************** -// -//! \addtogroup wlan_api -//! @{ -// -//***************************************************************************** - - -//***************************************************************************** -// -//! wlan_init -//! -//! @param sWlanCB Asynchronous events callback. -//! 0 no event call back. -//! -call back parameters: -//! 1) event_type: HCI_EVNT_WLAN_UNSOL_CONNECT connect event, -//! HCI_EVNT_WLAN_UNSOL_DISCONNECT disconnect event, -//! HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE config done, -//! HCI_EVNT_WLAN_UNSOL_DHCP dhcp report, -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT ping report OR -//! HCI_EVNT_WLAN_KEEPALIVE keepalive. -//! 2) data: pointer to extra data that received by the event -//! (NULL no data). -//! 3) length: data length. -//! -Events with extra data: -//! HCI_EVNT_WLAN_UNSOL_DHCP: 4 bytes IP, 4 bytes Mask, -//! 4 bytes default gateway, 4 bytes DHCP server and 4 bytes -//! for DNS server. -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT: 4 bytes Packets sent, -//! 4 bytes Packets received, 4 bytes Min round time, -//! 4 bytes Max round time and 4 bytes for Avg round time. -//! -//! @param sFWPatches 0 no patch or pointer to FW patches -//! @param sDriverPatches 0 no patch or pointer to driver patches -//! @param sBootLoaderPatches 0 no patch or pointer to bootloader patches -//! @param sReadWlanInterruptPin init callback. the callback read wlan -//! interrupt status. -//! @param sWlanInterruptEnable init callback. the callback enable wlan -//! interrupt. -//! @param sWlanInterruptDisable init callback. the callback disable wlan -//! interrupt. -//! @param sWriteWlanPin init callback. the callback write value -//! to device pin. -//! -//! @return none -//! -//! @sa wlan_set_event_mask , wlan_start , wlan_stop -//! -//! @brief Initialize wlan driver -//! -//! @warning This function must be called before ANY other wlan driver function -// -//***************************************************************************** -extern void wlan_init( tWlanCB sWlanCB, - tFWPatches sFWPatches, - tDriverPatches sDriverPatches, - tBootLoaderPatches sBootLoaderPatches, - tWlanReadInteruptPin sReadWlanInterruptPin, - tWlanInterruptEnable sWlanInterruptEnable, - tWlanInterruptDisable sWlanInterruptDisable, - tWriteWlanPin sWriteWlanPin); - - - -//***************************************************************************** -// -//! wlan_start -//! -//! @param usPatchesAvailableAtHost - flag to indicate if patches available -//! from host or from EEPROM. Due to the -//! fact the patches are burn to the EEPROM -//! using the patch programmer utility, the -//! patches will be available from the EEPROM -//! and not from the host. -//! -//! @return none -//! -//! @brief Start WLAN device. This function asserts the enable pin of -//! the device (WLAN_EN), starting the HW initialization process. -//! The function blocked until device Initialization is completed. -//! Function also configure patches (FW, driver or bootloader) -//! and calls appropriate device callbacks. -//! -//! @Note Prior calling the function wlan_init shall be called. -//! @Warning This function must be called after wlan_init and before any -//! other wlan API -//! @sa wlan_init , wlan_stop -//! -// -//***************************************************************************** -extern int wlan_start(UINT16 usPatchesAvailableAtHost); - -//***************************************************************************** -// -//! wlan_stop -//! -//! @param none -//! -//! @return none -//! -//! @brief Stop WLAN device by putting it into reset state. -//! -//! @sa wlan_start -// -//***************************************************************************** -extern void wlan_stop(void); - -//***************************************************************************** -// -//! wlan_connect -//! -//! @param sec_type security options: -//! WLAN_SEC_UNSEC, -//! WLAN_SEC_WEP (ASCII support only), -//! WLAN_SEC_WPA or WLAN_SEC_WPA2 -//! @param ssid up to 32 bytes and is ASCII SSID of the AP -//! @param ssid_len length of the SSID -//! @param bssid 6 bytes specified the AP bssid -//! @param key up to 32 bytes specified the AP security key -//! @param key_len key length -//! -//! @return On success, zero is returned. On error, negative is returned. -//! Note that even though a zero is returned on success to trigger -//! connection operation, it does not mean that CCC3000 is already -//! connected. An asynchronous "Connected" event is generated when -//! actual association process finishes and CC3000 is connected to -//! the AP. If DHCP is set, An asynchronous "DHCP" event is -//! generated when DHCP process is finish. -//! -//! -//! @brief Connect to AP -//! @warning Please Note that when connection to AP configured with security -//! type WEP, please confirm that the key is set as ASCII and not -//! as HEX. -//! @sa wlan_disconnect -// -//***************************************************************************** -#ifndef CC3000_TINY_DRIVER -extern INT32 wlan_connect(UINT32 ulSecType, CHAR *ssid, INT32 ssid_len, - UINT8 *bssid, UINT8 *key, INT32 key_len); -#else -extern INT32 wlan_connect(CHAR *ssid, INT32 ssid_len); - -#endif - -//***************************************************************************** -// -//! wlan_disconnect -//! -//! @return 0 disconnected done, other CC3000 already disconnected -//! -//! @brief Disconnect connection from AP. -//! -//! @sa wlan_connect -// -//***************************************************************************** - -extern INT32 wlan_disconnect(void); - -//***************************************************************************** -// -//! wlan_add_profile -//! -//! @param ulSecType WLAN_SEC_UNSEC,WLAN_SEC_WEP,WLAN_SEC_WPA,WLAN_SEC_WPA2 -//! @param ucSsid ssid SSID up to 32 bytes -//! @param ulSsidLen ssid length -//! @param ucBssid bssid 6 bytes -//! @param ulPriority ulPriority profile priority. Lowest priority:0. -//! @param ulPairwiseCipher_Or_TxKeyLen key length for WEP security -//! @param ulGroupCipher_TxKeyIndex key index -//! @param ulKeyMgmt KEY management -//! @param ucPf_OrKey security key -//! @param ulPassPhraseLen security key length for WPA\WPA2 -//! -//! @return On success, index (1-7) of the stored profile is returned. -//! On error, -1 is returned. -//! -//! @brief When auto start is enabled, the device connects to -//! station from the profiles table. Up to 7 profiles are supported. -//! If several profiles configured the device choose the highest -//! priority profile, within each priority group, device will choose -//! profile based on security policy, signal strength, etc -//! parameters. All the profiles are stored in CC3000 NVMEM. -//! -//! @sa wlan_ioctl_del_profile -// -//***************************************************************************** - -extern INT32 wlan_add_profile(UINT32 ulSecType, UINT8* ucSsid, - UINT32 ulSsidLen, - UINT8 *ucBssid, - UINT32 ulPriority, - UINT32 ulPairwiseCipher_Or_Key, - UINT32 ulGroupCipher_TxKeyLen, - UINT32 ulKeyMgmt, - UINT8* ucPf_OrKey, - UINT32 ulPassPhraseLen); - - - -//***************************************************************************** -// -//! wlan_ioctl_del_profile -//! -//! @param index number of profile to delete -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Delete WLAN profile -//! -//! @Note In order to delete all stored profile, set index to 255. -//! -//! @sa wlan_add_profile -// -//***************************************************************************** -extern INT32 wlan_ioctl_del_profile(UINT32 ulIndex); - -//***************************************************************************** -// -//! wlan_set_event_mask -//! -//! @param mask mask option: -//! HCI_EVNT_WLAN_UNSOL_CONNECT connect event -//! HCI_EVNT_WLAN_UNSOL_DISCONNECT disconnect event -//! HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE smart config done -//! HCI_EVNT_WLAN_UNSOL_INIT init done -//! HCI_EVNT_WLAN_UNSOL_DHCP dhcp event report -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT ping report -//! HCI_EVNT_WLAN_KEEPALIVE keepalive -//! HCI_EVNT_WLAN_TX_COMPLETE - disable information on end of transmission -//! Saved: no. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Mask event according to bit mask. In case that event is -//! masked (1), the device will not send the masked event to host. -// -//***************************************************************************** -extern INT32 wlan_set_event_mask(UINT32 ulMask); - -//***************************************************************************** -// -//! wlan_ioctl_statusget -//! -//! @param none -//! -//! @return WLAN_STATUS_DISCONNECTED, WLAN_STATUS_SCANING, -//! STATUS_CONNECTING or WLAN_STATUS_CONNECTED -//! -//! @brief get wlan status: disconnected, scanning, connecting or connected -// -//***************************************************************************** -extern INT32 wlan_ioctl_statusget(void); - - -//***************************************************************************** -// -//! wlan_ioctl_set_connection_policy -//! -//! @param should_connect_to_open_ap enable(1), disable(0) connect to any -//! available AP. This parameter corresponds to the configuration of -//! item # 3 in the brief description. -//! @param should_use_fast_connect enable(1), disable(0). if enabled, tries -//! to connect to the last connected AP. This parameter corresponds -//! to the configuration of item # 1 in the brief description. -//! @param auto_start enable(1), disable(0) auto connect -//! after reset and periodically reconnect if needed. This -//! configuration configures option 2 in the above description. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief When auto is enabled, the device tries to connect according -//! the following policy: -//! 1) If fast connect is enabled and last connection is valid, -//! the device will try to connect to it without the scanning -//! procedure (fast). The last connection will be marked as -//! invalid, due to adding/removing profile. -//! 2) If profile exists, the device will try to connect it -//! (Up to seven profiles). -//! 3) If fast and profiles are not found, and open mode is -//! enabled, the device will try to connect to any AP. -//! * Note that the policy settings are stored in the CC3000 NVMEM. -//! -//! @sa wlan_add_profile , wlan_ioctl_del_profile -// -//***************************************************************************** -extern INT32 wlan_ioctl_set_connection_policy( - UINT32 should_connect_to_open_ap, - UINT32 should_use_fast_connect, - UINT32 ulUseProfiles); - -//***************************************************************************** -// -//! wlan_ioctl_get_scan_results -//! -//! @param[in] scan_timeout parameter not supported -//! @param[out] ucResults scan result (_wlan_full_scan_results_args_t) -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Gets entry from scan result table. -//! The scan results are returned one by one, and each entry -//! represents a single AP found in the area. The following is a -//! format of the scan result: -//! - 4 Bytes: number of networks found -//! - 4 Bytes: The status of the scan: 0 - aged results, -//! 1 - results valid, 2 - no results -//! - 42 bytes: Result entry, where the bytes are arranged as follows: -//! -//! - 1 bit isValid - is result valid or not -//! - 7 bits rssi - RSSI value; -//! - 2 bits: securityMode - security mode of the AP: -//! 0 - Open, 1 - WEP, 2 WPA, 3 WPA2 -//! - 6 bits: SSID name length -//! - 2 bytes: the time at which the entry has entered into -//! scans result table -//! - 32 bytes: SSID name -//! - 6 bytes: BSSID -//! -//! @Note scan_timeout, is not supported on this version. -//! -//! @sa wlan_ioctl_set_scan_params -// -//***************************************************************************** - - -extern INT32 wlan_ioctl_get_scan_results(UINT32 ulScanTimeout, - UINT8 *ucResults); - -//***************************************************************************** -// -//! wlan_ioctl_set_scan_params -//! -//! @param uiEnable - start/stop application scan: -//! 1 = start scan with default interval value of 10 min. -//! in order to set a different scan interval value apply the value -//! in milliseconds. minimum 1 second. 0=stop). Wlan reset -//! (wlan_stop() wlan_start()) is needed when changing scan interval -//! value. Saved: No -//! @param uiMinDwellTime minimum dwell time value to be used for each -//! channel, in milliseconds. Saved: yes -//! Recommended Value: 100 (Default: 20) -//! @param uiMaxDwellTime maximum dwell time value to be used for each -//! channel, in milliseconds. Saved: yes -//! Recommended Value: 100 (Default: 30) -//! @param uiNumOfProbeRequests max probe request between dwell time. -//! Saved: yes. Recommended Value: 5 (Default:2) -//! @param uiChannelMask bitwise, up to 13 channels (0x1fff). -//! Saved: yes. Default: 0x7ff -//! @param uiRSSIThreshold RSSI threshold. Saved: yes (Default: -80) -//! @param uiSNRThreshold NSR threshold. Saved: yes (Default: 0) -//! @param uiDefaultTxPower probe Tx power. Saved: yes (Default: 205) -//! @param aiIntervalList pointer to array with 16 entries (16 channels) -//! each entry (UINT32) holds timeout between periodic scan -//! (connection scan) - in milliseconds. Saved: yes. Default 2000ms. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief start and stop scan procedure. Set scan parameters. -//! -//! @Note uiDefaultTxPower, is not supported on this version. -//! -//! @sa wlan_ioctl_get_scan_results -// -//***************************************************************************** -extern INT32 wlan_ioctl_set_scan_params(UINT32 uiEnable, UINT32 - uiMinDwellTime,UINT32 uiMaxDwellTime, - UINT32 uiNumOfProbeRequests, - UINT32 uiChannelMask, - INT32 iRSSIThreshold,UINT32 uiSNRThreshold, - UINT32 uiDefaultTxPower, - UINT32 *aiIntervalList); - - -//***************************************************************************** -// -//! wlan_smart_config_start -//! -//! @param algoEncryptedFlag indicates whether the information is encrypted -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Start to acquire device profile. The device acquire its own -//! profile, if profile message is found. The acquired AP information -//! is stored in CC3000 EEPROM only in case AES128 encryption is used. -//! In case AES128 encryption is not used, a profile is created by -//! CC3000 internally. -//! -//! @Note An asynchronous event - Smart Config Done will be generated as soon -//! as the process finishes successfully. -//! -//! @sa wlan_smart_config_set_prefix , wlan_smart_config_stop -// -//***************************************************************************** -extern INT32 wlan_smart_config_start(UINT32 algoEncryptedFlag); - - -//***************************************************************************** -// -//! wlan_smart_config_stop -//! -//! @param algoEncryptedFlag indicates whether the information is encrypted -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Stop the acquire profile procedure -//! -//! @sa wlan_smart_config_start , wlan_smart_config_set_prefix -// -//***************************************************************************** -extern INT32 wlan_smart_config_stop(void); - -//***************************************************************************** -// -//! wlan_smart_config_set_prefix -//! -//! @param newPrefix 3 bytes identify the SSID prefix for the Smart Config. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Configure station ssid prefix. The prefix is used internally -//! in CC3000. It should always be TTT. -//! -//! @Note The prefix is stored in CC3000 NVMEM -//! -//! @sa wlan_smart_config_start , wlan_smart_config_stop -// -//***************************************************************************** -extern INT32 wlan_smart_config_set_prefix(CHAR* cNewPrefix); - -//***************************************************************************** -// -//! wlan_smart_config_process -//! -//! @param none -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief process the acquired data and store it as a profile. The acquired -//! AP information is stored in CC3000 EEPROM encrypted. -//! The encrypted data is decrypted and stored as a profile. -//! behavior is as defined by connection policy. -// -//***************************************************************************** -extern INT32 wlan_smart_config_process(void); - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** - - - -//***************************************************************************** -// -// Mark the end of the C bindings section for C++ compilers. -// -//***************************************************************************** -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // __CC3000_WLAN_H__ diff --git a/drivers/cc3000/src/cc3000_common.c b/drivers/cc3000/src/cc3000_common.c deleted file mode 100644 index b4c87848cf476..0000000000000 --- a/drivers/cc3000/src/cc3000_common.c +++ /dev/null @@ -1,164 +0,0 @@ -/***************************************************************************** -* -* cc3000_common.c.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -//***************************************************************************** -// -//! \addtogroup common_api -//! @{ -// -//***************************************************************************** -/****************************************************************************** -* -* Include files -* -*****************************************************************************/ - -#include "cc3000_common.h" -#include "socket.h" -#include "wlan.h" -#include "evnt_handler.h" - -//***************************************************************************** -// -//! __error__ -//! -//! @param pcFilename - file name, where error occurred -//! @param ulLine - line number, where error occurred -//! -//! @return none -//! -//! @brief stub function for ASSERT macro -// -//***************************************************************************** -void __error__(CHAR *pcFilename, UINT32 ulLine) -{ - //TODO full up function -} - - - -//***************************************************************************** -// -//! UINT32_TO_STREAM_f -//! -//! @param p pointer to the new stream -//! @param u32 pointer to the 32 bit -//! -//! @return pointer to the new stream -//! -//! @brief This function is used for copying 32 bit to stream -//! while converting to little endian format. -// -//***************************************************************************** - -UINT8* UINT32_TO_STREAM_f (UINT8 *p, UINT32 u32) -{ - *(p)++ = (UINT8)(u32); - *(p)++ = (UINT8)((u32) >> 8); - *(p)++ = (UINT8)((u32) >> 16); - *(p)++ = (UINT8)((u32) >> 24); - return p; -} - -//***************************************************************************** -// -//! UINT16_TO_STREAM_f -//! -//! @param p pointer to the new stream -//! @param u32 pointer to the 16 bit -//! -//! @return pointer to the new stream -//! -//! @brief This function is used for copying 16 bit to stream -//! while converting to little endian format. -// -//***************************************************************************** - -UINT8* UINT16_TO_STREAM_f (UINT8 *p, UINT16 u16) -{ - *(p)++ = (UINT8)(u16); - *(p)++ = (UINT8)((u16) >> 8); - return p; -} - -//***************************************************************************** -// -//! STREAM_TO_UINT16_f -//! -//! @param p pointer to the stream -//! @param offset offset in the stream -//! -//! @return pointer to the new 16 bit -//! -//! @brief This function is used for copying received stream to -//! 16 bit in little endian format. -// -//***************************************************************************** - -UINT16 STREAM_TO_UINT16_f(CHAR* p, UINT16 offset) -{ - return (UINT16)((UINT16)((UINT16) - (*(p + offset + 1)) << 8) + (UINT16)(*(p + offset))); -} - -//***************************************************************************** -// -//! STREAM_TO_UINT32_f -//! -//! @param p pointer to the stream -//! @param offset offset in the stream -//! -//! @return pointer to the new 32 bit -//! -//! @brief This function is used for copying received stream to -//! 32 bit in little endian format. -// -//***************************************************************************** - -UINT32 STREAM_TO_UINT32_f(CHAR* p, UINT16 offset) -{ - return (UINT32)((UINT32)((UINT32) - (*(p + offset + 3)) << 24) + (UINT32)((UINT32) - (*(p + offset + 2)) << 16) + (UINT32)((UINT32) - (*(p + offset + 1)) << 8) + (UINT32)(*(p + offset))); -} - - - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** diff --git a/drivers/cc3000/src/ccspi.c b/drivers/cc3000/src/ccspi.c deleted file mode 100644 index 1dcd61884086d..0000000000000 --- a/drivers/cc3000/src/ccspi.c +++ /dev/null @@ -1,455 +0,0 @@ -/***************************************************************************** - * - * spi.c - CC3000 Host Driver Implementation. - * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 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. - * - * Neither the name of Texas Instruments Incorporated nor the names of - * its contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "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 COPYRIGHT - * OWNER OR CONTRIBUTORS 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. - * - *****************************************************************************/ - -#include - -#include "py/runtime.h" -#include "pin.h" -#include "led.h" -#include "extint.h" -#include "spi.h" -#include "ccspi.h" -#include "evnt_handler.h" - -#if 0 // print debugging info -#include -#define DEBUG_printf(args...) printf(args) -#else // don't print debugging info -#define DEBUG_printf(args...) (void)0 -#endif - -// these need to be set to valid values before anything in this file will work -STATIC const spi_t *SPI_HANDLE = NULL; -STATIC const pin_obj_t *PIN_CS = NULL; -STATIC const pin_obj_t *PIN_EN = NULL; -STATIC const pin_obj_t *PIN_IRQ = NULL; - -#define CS_LOW() HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_RESET) -#define CS_HIGH() HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET) - -#define READ 3 -#define WRITE 1 - -#define HI(value) (((value) & 0xFF00) >> 8) -#define LO(value) ((value) & 0x00FF) - -#define SPI_TIMEOUT (1000) -#define HEADERS_SIZE_EVNT (SPI_HEADER_SIZE + 5) - -/* SPI bus states */ -#define eSPI_STATE_POWERUP (0) -#define eSPI_STATE_INITIALIZED (1) -#define eSPI_STATE_IDLE (2) -#define eSPI_STATE_WRITE_IRQ (3) -#define eSPI_STATE_WRITE_FIRST_PORTION (4) -#define eSPI_STATE_WRITE_EOT (5) -#define eSPI_STATE_READ_IRQ (6) -#define eSPI_STATE_READ_FIRST_PORTION (7) -#define eSPI_STATE_READ_EOT (8) - -// The magic number that resides at the end of the TX/RX buffer (1 byte after the allocated size) -// for the purpose of detection of the overrun. The location of the memory where the magic number -// resides shall never be written. In case it is written - the overrun occured and either recevie function -// or send function will stuck forever. -#define CC3000_BUFFER_MAGIC_NUMBER (0xDE) - -typedef struct { - gcSpiHandleRx SPIRxHandler; - unsigned short usTxPacketLength; - unsigned short usRxPacketLength; - unsigned long ulSpiState; - unsigned char *pTxPacket; - unsigned char *pRxPacket; -} tSpiInformation; -STATIC tSpiInformation sSpiInformation; - -STATIC char spi_buffer[CC3000_RX_BUFFER_SIZE]; -unsigned char wlan_tx_buffer[CC3000_TX_BUFFER_SIZE]; - -STATIC const mp_obj_fun_builtin_fixed_t irq_callback_obj; - -// set the pins to use to communicate with the CC3000 -// the arguments must be of type pin_obj_t* and SPI_HandleTypeDef* -void SpiInit(void *spi, const void *pin_cs, const void *pin_en, const void *pin_irq) { - SPI_HANDLE = spi; - PIN_CS = pin_cs; - PIN_EN = pin_en; - PIN_IRQ = pin_irq; -} - -void SpiClose(void) -{ - if (sSpiInformation.pRxPacket) { - sSpiInformation.pRxPacket = 0; - } - - tSLInformation.WlanInterruptDisable(); - - //HAL_SPI_DeInit(SPI_HANDLE); -} - -void SpiOpen(gcSpiHandleRx pfRxHandler) -{ - DEBUG_printf("SpiOpen\n"); - - /* initialize SPI state */ - sSpiInformation.ulSpiState = eSPI_STATE_POWERUP; - sSpiInformation.SPIRxHandler = pfRxHandler; - sSpiInformation.usTxPacketLength = 0; - sSpiInformation.pTxPacket = NULL; - sSpiInformation.pRxPacket = (unsigned char *)spi_buffer; - sSpiInformation.usRxPacketLength = 0; - spi_buffer[CC3000_RX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; - wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; - - /* SPI configuration */ - SPI_InitTypeDef *init = &SPI_HANDLE->spi->Init; - init->Mode = SPI_MODE_MASTER; - init->Direction = SPI_DIRECTION_2LINES; - init->DataSize = SPI_DATASIZE_8BIT; - init->CLKPolarity = SPI_POLARITY_LOW; - init->CLKPhase = SPI_PHASE_2EDGE; - init->NSS = SPI_NSS_SOFT; - init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; - init->FirstBit = SPI_FIRSTBIT_MSB; - init->TIMode = SPI_TIMODE_DISABLED; - init->CRCCalculation = SPI_CRCCALCULATION_DISABLED; - init->CRCPolynomial = 7; - spi_init(SPI_HANDLE, false); - - // configure wlan CS and EN pins - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.Speed = GPIO_SPEED_FAST; - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStructure.Pull = GPIO_NOPULL; - GPIO_InitStructure.Alternate = 0; - - GPIO_InitStructure.Pin = PIN_CS->pin_mask; - HAL_GPIO_Init(PIN_CS->gpio, &GPIO_InitStructure); - - GPIO_InitStructure.Pin = PIN_EN->pin_mask; - HAL_GPIO_Init(PIN_EN->gpio, &GPIO_InitStructure); - - HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET); - HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask, GPIO_PIN_RESET); - - /* do a dummy read, this ensures SCLK is low before - actual communications start, it might be required */ - CS_LOW(); - uint8_t buf[1]; - HAL_SPI_Receive(SPI_HANDLE->spi, buf, sizeof(buf), SPI_TIMEOUT); - CS_HIGH(); - - // register EXTI - extint_register((mp_obj_t)PIN_IRQ, GPIO_MODE_IT_FALLING, GPIO_PULLUP, (mp_obj_t)&irq_callback_obj, true); - extint_enable(PIN_IRQ->pin); - - DEBUG_printf("SpiOpen finished; IRQ.pin=%d IRQ_LINE=%d\n", PIN_IRQ->pin, PIN_IRQ->pin); -} - -long ReadWlanInterruptPin(void) -{ - return HAL_GPIO_ReadPin(PIN_IRQ->gpio, PIN_IRQ->pin_mask); -} - -void WriteWlanPin(unsigned char val) -{ - HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask, - (WLAN_ENABLE)? GPIO_PIN_SET:GPIO_PIN_RESET); -} - -STATIC void SpiWriteDataSynchronous(unsigned char *data, unsigned short size) -{ - DEBUG_printf("SpiWriteDataSynchronous(data=%p [%x %x %x %x], size=%u)\n", data, data[0], data[1], data[2], data[3], size); - __disable_irq(); - if (HAL_SPI_TransmitReceive(SPI_HANDLE->spi, data, data, size, SPI_TIMEOUT) != HAL_OK) { - //BREAK(); - } - __enable_irq(); - DEBUG_printf(" - rx data = [%x %x %x %x]\n", data[0], data[1], data[2], data[3]); -} - -STATIC void SpiReadDataSynchronous(unsigned char *data, unsigned short size) -{ - memset(data, READ, size); - __disable_irq(); - if (HAL_SPI_TransmitReceive(SPI_HANDLE->spi, data, data, size, SPI_TIMEOUT) != HAL_OK) { - //BREAK(); - } - __enable_irq(); -} - -STATIC void __delay_cycles(volatile int x) -{ - x *= 6; // for 168 MHz CPU - while (x--); -} - -STATIC long SpiFirstWrite(unsigned char *ucBuf, unsigned short usLength) -{ - DEBUG_printf("SpiFirstWrite %lu\n", sSpiInformation.ulSpiState); - - CS_LOW(); - - // Assuming we are running on 24 MHz ~50 micro delay is 1200 cycles; - __delay_cycles(1200); - - // SPI writes first 4 bytes of data - SpiWriteDataSynchronous(ucBuf, 4); - - __delay_cycles(1200); - - SpiWriteDataSynchronous(ucBuf + 4, usLength - 4); - - // From this point on - operate in a regular way - sSpiInformation.ulSpiState = eSPI_STATE_IDLE; - - CS_HIGH(); - - return(0); -} - -long SpiWrite(unsigned char *pUserBuffer, unsigned short usLength) -{ - DEBUG_printf("SpiWrite %lu\n", sSpiInformation.ulSpiState); - - unsigned char ucPad = 0; - - // Figure out the total length of the packet in order to figure out if there - // is padding or not - if(!(usLength & 0x0001)) { - ucPad++; - } - - pUserBuffer[0] = WRITE; - pUserBuffer[1] = HI(usLength + ucPad); - pUserBuffer[2] = LO(usLength + ucPad); - pUserBuffer[3] = 0; - pUserBuffer[4] = 0; - - usLength += (SPI_HEADER_SIZE + ucPad); - - // The magic number that resides at the end of the TX/RX buffer (1 byte after the allocated size) - // for the purpose of detection of the overrun. If the magic number is overriten - buffer overrun - // occurred - and we will stuck here forever! - if (wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) { - while (1); - } - - if (sSpiInformation.ulSpiState == eSPI_STATE_POWERUP) { - while (sSpiInformation.ulSpiState != eSPI_STATE_INITIALIZED); - } - - if (sSpiInformation.ulSpiState == eSPI_STATE_INITIALIZED) { - // This is time for first TX/RX transactions over SPI: - // the IRQ is down - so need to send read buffer size command - SpiFirstWrite(pUserBuffer, usLength); - } else { - // - // We need to prevent here race that can occur in case 2 back to back packets are sent to the - // device, so the state will move to IDLE and once again to not IDLE due to IRQ - // - tSLInformation.WlanInterruptDisable(); - - while (sSpiInformation.ulSpiState != eSPI_STATE_IDLE); - - sSpiInformation.ulSpiState = eSPI_STATE_WRITE_IRQ; - sSpiInformation.pTxPacket = pUserBuffer; - sSpiInformation.usTxPacketLength = usLength; - - // Assert the CS line and wait till SSI IRQ line is active and then initialize write operation - CS_LOW(); - - // Re-enable IRQ - if it was not disabled - this is not a problem... - tSLInformation.WlanInterruptEnable(); - - // check for a missing interrupt between the CS assertion and enabling back the interrupts - if (tSLInformation.ReadWlanInterruptPin() == 0) { - SpiWriteDataSynchronous(sSpiInformation.pTxPacket, sSpiInformation.usTxPacketLength); - - sSpiInformation.ulSpiState = eSPI_STATE_IDLE; - - CS_HIGH(); - } - } - - // Due to the fact that we are currently implementing a blocking situation - // here we will wait till end of transaction - while (eSPI_STATE_IDLE != sSpiInformation.ulSpiState); - - return(0); -} - -#if 0 -unused -STATIC void SpiReadPacket(void) -{ - int length; - - /* read SPI header */ - SpiReadDataSynchronous(sSpiInformation.pRxPacket, SPI_HEADER_SIZE); - - /* parse data length */ - STREAM_TO_UINT8(sSpiInformation.pRxPacket, SPI_HEADER_SIZE-1, length); - - /* read the remainder of the packet */ - SpiReadDataSynchronous(sSpiInformation.pRxPacket + SPI_HEADER_SIZE, length); - - sSpiInformation.ulSpiState = eSPI_STATE_READ_EOT; -} -#endif - -STATIC void SpiReadHeader(void) -{ - SpiReadDataSynchronous(sSpiInformation.pRxPacket, 10); -} - -STATIC void SpiTriggerRxProcessing(void) -{ - SpiPauseSpi(); - CS_HIGH(); - - // The magic number that resides at the end of the TX/RX buffer (1 byte after the allocated size) - // for the purpose of detection of the overrun. If the magic number is overriten - buffer overrun - // occurred - and we will stuck here forever! - if (sSpiInformation.pRxPacket[CC3000_RX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) { - while (1); - } - - sSpiInformation.ulSpiState = eSPI_STATE_IDLE; - sSpiInformation.SPIRxHandler(sSpiInformation.pRxPacket + SPI_HEADER_SIZE); -} - - -STATIC long SpiReadDataCont(void) -{ - long data_to_recv=0; - unsigned char *evnt_buff, type; - - //determine what type of packet we have - evnt_buff = sSpiInformation.pRxPacket; - STREAM_TO_UINT8((char *)(evnt_buff + SPI_HEADER_SIZE), HCI_PACKET_TYPE_OFFSET, type); - - switch (type) { - case HCI_TYPE_DATA:{ - // We need to read the rest of data.. - STREAM_TO_UINT16((char *)(evnt_buff + SPI_HEADER_SIZE), - HCI_DATA_LENGTH_OFFSET, data_to_recv); - if (!((HEADERS_SIZE_EVNT + data_to_recv) & 1)) { - data_to_recv++; - } - - if (data_to_recv) { - SpiReadDataSynchronous(evnt_buff + 10, data_to_recv); - } - break; - } - case HCI_TYPE_EVNT: { - // Calculate the rest length of the data - STREAM_TO_UINT8((char *)(evnt_buff + SPI_HEADER_SIZE), - HCI_EVENT_LENGTH_OFFSET, data_to_recv); - data_to_recv -= 1; - - // Add padding byte if needed - if ((HEADERS_SIZE_EVNT + data_to_recv) & 1) { - data_to_recv++; - } - - if (data_to_recv) { - SpiReadDataSynchronous(evnt_buff + 10, data_to_recv); - } - - sSpiInformation.ulSpiState = eSPI_STATE_READ_EOT; - break; - } - } - - return 0; -} - -STATIC void SSIContReadOperation(void) -{ - // The header was read - continue with the payload read - if (!SpiReadDataCont()) { - /* All the data was read - finalize handling by switching - to the task and calling from task Event Handler */ - SpiTriggerRxProcessing(); - } -} - -STATIC mp_obj_t irq_callback(mp_obj_t line) { - DEBUG_printf("<< IRQ; state=%lu >>\n", sSpiInformation.ulSpiState); - switch (sSpiInformation.ulSpiState) { - case eSPI_STATE_POWERUP: - /* This means IRQ line was low call a callback of HCI Layer to inform on event */ - DEBUG_printf(" - POWERUP\n"); - sSpiInformation.ulSpiState = eSPI_STATE_INITIALIZED; - break; - case eSPI_STATE_IDLE: - DEBUG_printf(" - IDLE\n"); - sSpiInformation.ulSpiState = eSPI_STATE_READ_IRQ; - - /* IRQ line goes down - we are start reception */ - CS_LOW(); - - // Wait for TX/RX Compete which will come as DMA interrupt - SpiReadHeader(); - - sSpiInformation.ulSpiState = eSPI_STATE_READ_EOT; - - SSIContReadOperation(); - break; - case eSPI_STATE_WRITE_IRQ: - DEBUG_printf(" - WRITE IRQ\n"); - SpiWriteDataSynchronous(sSpiInformation.pTxPacket, sSpiInformation.usTxPacketLength); - - sSpiInformation.ulSpiState = eSPI_STATE_IDLE; - - CS_HIGH(); - break; - } - return mp_const_none; -} - -STATIC MP_DEFINE_CONST_FUN_OBJ_1(irq_callback_obj, irq_callback); - -void SpiPauseSpi(void) { - DEBUG_printf("SpiPauseSpi\n"); - extint_disable(PIN_IRQ->pin); -} - -void SpiResumeSpi(void) { - DEBUG_printf("SpiResumeSpi\n"); - extint_enable(PIN_IRQ->pin); -} diff --git a/drivers/cc3000/src/evnt_handler.c b/drivers/cc3000/src/evnt_handler.c deleted file mode 100644 index d430bbfdad555..0000000000000 --- a/drivers/cc3000/src/evnt_handler.c +++ /dev/null @@ -1,849 +0,0 @@ -/***************************************************************************** -* -* evnt_handler.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ -//***************************************************************************** -// -//! \addtogroup evnt_handler_api -//! @{ -// -//****************************************************************************** - -//****************************************************************************** -// INCLUDE FILES -//****************************************************************************** - -#include "cc3000_common.h" -#include "string.h" -#include "hci.h" -#include "evnt_handler.h" -#include "wlan.h" -#include "socket.h" -#include "netapp.h" -#include "ccspi.h" - - - -//***************************************************************************** -// COMMON DEFINES -//***************************************************************************** - -#define FLOW_CONTROL_EVENT_HANDLE_OFFSET (0) -#define FLOW_CONTROL_EVENT_BLOCK_MODE_OFFSET (1) -#define FLOW_CONTROL_EVENT_FREE_BUFFS_OFFSET (2) -#define FLOW_CONTROL_EVENT_SIZE (4) - -#define BSD_RSP_PARAMS_SOCKET_OFFSET (0) -#define BSD_RSP_PARAMS_STATUS_OFFSET (4) - -#define GET_HOST_BY_NAME_RETVAL_OFFSET (0) -#define GET_HOST_BY_NAME_ADDR_OFFSET (4) - -#define ACCEPT_SD_OFFSET (0) -#define ACCEPT_RETURN_STATUS_OFFSET (4) -#define ACCEPT_ADDRESS__OFFSET (8) - -#define SL_RECEIVE_SD_OFFSET (0) -#define SL_RECEIVE_NUM_BYTES_OFFSET (4) -#define SL_RECEIVE__FLAGS__OFFSET (8) - - -#define SELECT_STATUS_OFFSET (0) -#define SELECT_READFD_OFFSET (4) -#define SELECT_WRITEFD_OFFSET (8) -#define SELECT_EXFD_OFFSET (12) - - -#define NETAPP_IPCONFIG_IP_OFFSET (0) -#define NETAPP_IPCONFIG_SUBNET_OFFSET (4) -#define NETAPP_IPCONFIG_GW_OFFSET (8) -#define NETAPP_IPCONFIG_DHCP_OFFSET (12) -#define NETAPP_IPCONFIG_DNS_OFFSET (16) -#define NETAPP_IPCONFIG_MAC_OFFSET (20) -#define NETAPP_IPCONFIG_SSID_OFFSET (26) - -#define NETAPP_IPCONFIG_IP_LENGTH (4) -#define NETAPP_IPCONFIG_MAC_LENGTH (6) -#define NETAPP_IPCONFIG_SSID_LENGTH (32) - - -#define NETAPP_PING_PACKETS_SENT_OFFSET (0) -#define NETAPP_PING_PACKETS_RCVD_OFFSET (4) -#define NETAPP_PING_MIN_RTT_OFFSET (8) -#define NETAPP_PING_MAX_RTT_OFFSET (12) -#define NETAPP_PING_AVG_RTT_OFFSET (16) - -#define GET_SCAN_RESULTS_TABlE_COUNT_OFFSET (0) -#define GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET (4) -#define GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET (8) -#define GET_SCAN_RESULTS_FRAME_TIME_OFFSET (10) -#define GET_SCAN_RESULTS_SSID_MAC_LENGTH (38) - -#define GET_MSS_VAL_RETVAL_OFFSET (0) - -//***************************************************************************** -// GLOBAL VARAIABLES -//***************************************************************************** - -UINT32 socket_active_status = SOCKET_STATUS_INIT_VAL; - - -//***************************************************************************** -// Prototypes for the static functions -//***************************************************************************** - -static INT32 hci_event_unsol_flowcontrol_handler(CHAR *pEvent); - -static void update_socket_active_status(CHAR *resp_params); - - -//***************************************************************************** -// -//! hci_unsol_handle_patch_request -//! -//! @param event_hdr event header -//! -//! @return none -//! -//! @brief Handle unsolicited event from type patch request -// -//***************************************************************************** -void hci_unsol_handle_patch_request(CHAR *event_hdr) -{ - CHAR *params = (CHAR *)(event_hdr) + HCI_EVENT_HEADER_SIZE; - UINT32 ucLength = 0; - CHAR *patch; - - switch (*params) - { - case HCI_EVENT_PATCHES_DRV_REQ: - - if (tSLInformation.sDriverPatches) - { - patch = tSLInformation.sDriverPatches(&ucLength); - - if (patch) - { - hci_patch_send(HCI_EVENT_PATCHES_DRV_REQ, - tSLInformation.pucTxCommandBuffer, patch, ucLength); - return; - } - } - - // Send 0 length Patches response event - hci_patch_send(HCI_EVENT_PATCHES_DRV_REQ, - tSLInformation.pucTxCommandBuffer, 0, 0); - break; - - case HCI_EVENT_PATCHES_FW_REQ: - - if (tSLInformation.sFWPatches) - { - patch = tSLInformation.sFWPatches(&ucLength); - - // Build and send a patch - if (patch) - { - hci_patch_send(HCI_EVENT_PATCHES_FW_REQ, - tSLInformation.pucTxCommandBuffer, patch, ucLength); - return; - } - } - - // Send 0 length Patches response event - hci_patch_send(HCI_EVENT_PATCHES_FW_REQ, - tSLInformation.pucTxCommandBuffer, 0, 0); - break; - - case HCI_EVENT_PATCHES_BOOTLOAD_REQ: - - if (tSLInformation.sBootLoaderPatches) - { - patch = tSLInformation.sBootLoaderPatches(&ucLength); - - if (patch) - { - hci_patch_send(HCI_EVENT_PATCHES_BOOTLOAD_REQ, - tSLInformation.pucTxCommandBuffer, patch, ucLength); - return; - } - } - - // Send 0 length Patches response event - hci_patch_send(HCI_EVENT_PATCHES_BOOTLOAD_REQ, - tSLInformation.pucTxCommandBuffer, 0, 0); - break; - } -} - - - -//***************************************************************************** -// -//! hci_event_handler -//! -//! @param pRetParams incoming data buffer -//! @param from from information (in case of data received) -//! @param fromlen from information length (in case of data received) -//! -//! @return none -//! -//! @brief Parse the incoming events packets and issues corresponding -//! event handler from global array of handlers pointers -// -//***************************************************************************** - - -UINT8 * hci_event_handler(void *pRetParams, UINT8 *from, UINT8 *fromlen) -{ - UINT8 *pucReceivedData, ucArgsize; - UINT16 usLength; - UINT8 *pucReceivedParams; - UINT16 usReceivedEventOpcode = 0; - UINT32 retValue32; - UINT8 * RecvParams; - UINT8 *RetParams; - - - while (1) - { - if (tSLInformation.usEventOrDataReceived != 0) - { - pucReceivedData = (tSLInformation.pucReceivedData); - - if (*pucReceivedData == HCI_TYPE_EVNT) - { - // Event Received - STREAM_TO_UINT16((CHAR *)pucReceivedData, HCI_EVENT_OPCODE_OFFSET, - usReceivedEventOpcode); - pucReceivedParams = pucReceivedData + HCI_EVENT_HEADER_SIZE; - RecvParams = pucReceivedParams; - RetParams = pRetParams; - - // In case unsolicited event received - here the handling finished - if (hci_unsol_event_handler((CHAR *)pucReceivedData) == 0) - { - STREAM_TO_UINT8(pucReceivedData, HCI_DATA_LENGTH_OFFSET, usLength); - - switch(usReceivedEventOpcode) - { - case HCI_CMND_READ_BUFFER_SIZE: - { - STREAM_TO_UINT8((CHAR *)pucReceivedParams, 0, - tSLInformation.usNumberOfFreeBuffers); - STREAM_TO_UINT16((CHAR *)pucReceivedParams, 1, - tSLInformation.usSlBufferLength); - } - break; - - case HCI_CMND_WLAN_CONFIGURE_PATCH: - case HCI_NETAPP_DHCP: - case HCI_NETAPP_PING_SEND: - case HCI_NETAPP_PING_STOP: - case HCI_NETAPP_ARP_FLUSH: - case HCI_NETAPP_SET_DEBUG_LEVEL: - case HCI_NETAPP_SET_TIMERS: - case HCI_EVNT_NVMEM_READ: - case HCI_EVNT_NVMEM_CREATE_ENTRY: - case HCI_CMND_NVMEM_WRITE_PATCH: - case HCI_NETAPP_PING_REPORT: - case HCI_EVNT_MDNS_ADVERTISE: - - STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET - ,*(UINT8 *)pRetParams); - break; - - case HCI_CMND_SETSOCKOPT: - case HCI_CMND_WLAN_CONNECT: - case HCI_CMND_WLAN_IOCTL_STATUSGET: - case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE: - case HCI_CMND_WLAN_IOCTL_DEL_PROFILE: - case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY: - case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM: - case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START: - case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP: - case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX: - case HCI_CMND_EVENT_MASK: - case HCI_EVNT_WLAN_DISCONNECT: - case HCI_EVNT_SOCKET: - case HCI_EVNT_BIND: - case HCI_CMND_LISTEN: - case HCI_EVNT_CLOSE_SOCKET: - case HCI_EVNT_CONNECT: - case HCI_EVNT_NVMEM_WRITE: - - STREAM_TO_UINT32((CHAR *)pucReceivedParams,0 - ,*(UINT32 *)pRetParams); - break; - - case HCI_EVNT_READ_SP_VERSION: - - STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET - ,*(UINT8 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 1; - STREAM_TO_UINT32((CHAR *)pucReceivedParams, 0, retValue32); - UINT32_TO_STREAM((UINT8 *)pRetParams, retValue32); - break; - - case HCI_EVNT_BSD_GETHOSTBYNAME: - - STREAM_TO_UINT32((CHAR *)pucReceivedParams - ,GET_HOST_BY_NAME_RETVAL_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams - ,GET_HOST_BY_NAME_ADDR_OFFSET,*(UINT32 *)pRetParams); - break; - - case HCI_EVNT_GETMSSVALUE: - - STREAM_TO_UINT16((CHAR *)pucReceivedParams - ,GET_MSS_VAL_RETVAL_OFFSET,*(UINT16 *)pRetParams); - - break; - - case HCI_EVNT_ACCEPT: - { - STREAM_TO_UINT32((CHAR *)pucReceivedParams,ACCEPT_SD_OFFSET - ,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams - ,ACCEPT_RETURN_STATUS_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - - //This argument returns in network order - memcpy((UINT8 *)pRetParams, - pucReceivedParams + ACCEPT_ADDRESS__OFFSET, sizeof(sockaddr)); - break; - } - - case HCI_EVNT_RECV: - case HCI_EVNT_RECVFROM: - { - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SL_RECEIVE__FLAGS__OFFSET,*(UINT32 *)pRetParams); - - if(((tBsdReadReturnParams *)pRetParams)->iNumberOfBytes == ERROR_SOCKET_INACTIVE) - { - set_socket_active_status(((tBsdReadReturnParams *)pRetParams)->iSocketDescriptor,SOCKET_STATUS_INACTIVE); - } - break; - } - - case HCI_EVNT_SEND: - case HCI_EVNT_SENDTO: - { - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - - break; - } - - case HCI_EVNT_SELECT: - { - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SELECT_STATUS_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SELECT_READFD_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SELECT_WRITEFD_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,SELECT_EXFD_OFFSET,*(UINT32 *)pRetParams); - break; - } - - case HCI_CMND_GETSOCKOPT: - - STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET,((tBsdGetSockOptReturnParams *)pRetParams)->iStatus); - //This argument returns in network order - memcpy((UINT8 *)pRetParams, pucReceivedParams, 4); - break; - - case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS: - - STREAM_TO_UINT32((CHAR *)pucReceivedParams,GET_SCAN_RESULTS_TABlE_COUNT_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT32((CHAR *)pucReceivedParams,GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 4; - STREAM_TO_UINT16((CHAR *)pucReceivedParams,GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 2; - STREAM_TO_UINT16((CHAR *)pucReceivedParams,GET_SCAN_RESULTS_FRAME_TIME_OFFSET,*(UINT32 *)pRetParams); - pRetParams = ((CHAR *)pRetParams) + 2; - memcpy((UINT8 *)pRetParams, (CHAR *)(pucReceivedParams + GET_SCAN_RESULTS_FRAME_TIME_OFFSET + 2), GET_SCAN_RESULTS_SSID_MAC_LENGTH); - break; - - case HCI_CMND_SIMPLE_LINK_START: - break; - - case HCI_NETAPP_IPCONFIG: - - //Read IP address - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); - RecvParams += 4; - - //Read subnet - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); - RecvParams += 4; - - //Read default GW - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); - RecvParams += 4; - - //Read DHCP server - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); - RecvParams += 4; - - //Read DNS server - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); - RecvParams += 4; - - //Read Mac address - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_MAC_LENGTH); - RecvParams += 6; - - //Read SSID - STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_SSID_LENGTH); - - } - } - - if (usReceivedEventOpcode == tSLInformation.usRxEventOpcode) - { - tSLInformation.usRxEventOpcode = 0; - } - } - else - { - pucReceivedParams = pucReceivedData; - STREAM_TO_UINT8((CHAR *)pucReceivedData, HCI_PACKET_ARGSIZE_OFFSET, ucArgsize); - - STREAM_TO_UINT16((CHAR *)pucReceivedData, HCI_PACKET_LENGTH_OFFSET, usLength); - - // Data received: note that the only case where from and from length - // are not null is in recv from, so fill the args accordingly - if (from) - { - STREAM_TO_UINT32((CHAR *)(pucReceivedData + HCI_DATA_HEADER_SIZE), BSD_RECV_FROM_FROMLEN_OFFSET, *(UINT32 *)fromlen); - memcpy(from, (pucReceivedData + HCI_DATA_HEADER_SIZE + BSD_RECV_FROM_FROM_OFFSET) ,*fromlen); - } - - memcpy(pRetParams, pucReceivedParams + HCI_DATA_HEADER_SIZE + ucArgsize, - usLength - ucArgsize); - - tSLInformation.usRxDataPending = 0; - } - - tSLInformation.usEventOrDataReceived = 0; - - SpiResumeSpi(); - - // Since we are going to TX - we need to handle this event after the - // ResumeSPi since we need interrupts - if ((*pucReceivedData == HCI_TYPE_EVNT) && - (usReceivedEventOpcode == HCI_EVNT_PATCHES_REQ)) - { - hci_unsol_handle_patch_request((CHAR *)pucReceivedData); - } - - if ((tSLInformation.usRxEventOpcode == 0) && (tSLInformation.usRxDataPending == 0)) - { - return NULL; - } - } - } - -} - -//***************************************************************************** -// -//! hci_unsol_event_handler -//! -//! @param event_hdr event header -//! -//! @return 1 if event supported and handled -//! 0 if event is not supported -//! -//! @brief Handle unsolicited events -// -//***************************************************************************** -INT32 hci_unsol_event_handler(CHAR *event_hdr) -{ - CHAR * data = NULL; - INT32 event_type; - UINT32 NumberOfReleasedPackets; - UINT32 NumberOfSentPackets; - - STREAM_TO_UINT16(event_hdr, HCI_EVENT_OPCODE_OFFSET,event_type); - - if (event_type & HCI_EVNT_UNSOL_BASE) - { - switch(event_type) - { - - case HCI_EVNT_DATA_UNSOL_FREE_BUFF: - { - hci_event_unsol_flowcontrol_handler(event_hdr); - - NumberOfReleasedPackets = tSLInformation.NumberOfReleasedPackets; - NumberOfSentPackets = tSLInformation.NumberOfSentPackets; - - if (NumberOfReleasedPackets == NumberOfSentPackets) - { - if (tSLInformation.InformHostOnTxComplete) - { - tSLInformation.sWlanCB(HCI_EVENT_CC3000_CAN_SHUT_DOWN, NULL, 0); - } - } - return 1; - - } - } - } - - if(event_type & HCI_EVNT_WLAN_UNSOL_BASE) - { - switch(event_type) - { - case HCI_EVNT_WLAN_KEEPALIVE: - case HCI_EVNT_WLAN_UNSOL_CONNECT: - case HCI_EVNT_WLAN_UNSOL_DISCONNECT: - case HCI_EVNT_WLAN_UNSOL_INIT: - case HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE: - - if( tSLInformation.sWlanCB ) - { - tSLInformation.sWlanCB(event_type, 0, 0); - } - break; - - case HCI_EVNT_WLAN_UNSOL_DHCP: - { - UINT8 params[NETAPP_IPCONFIG_MAC_OFFSET + 1]; // extra byte is for the status - UINT8 *recParams = params; - - data = (CHAR*)(event_hdr) + HCI_EVENT_HEADER_SIZE; - - //Read IP address - STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); - data += 4; - //Read subnet - STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); - data += 4; - //Read default GW - STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); - data += 4; - //Read DHCP server - STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); - data += 4; - //Read DNS server - STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); - // read the status - STREAM_TO_UINT8(event_hdr, HCI_EVENT_STATUS_OFFSET, *recParams); - - - if( tSLInformation.sWlanCB ) - { - tSLInformation.sWlanCB(event_type, (CHAR *)params, sizeof(params)); - } - } - break; - - case HCI_EVNT_WLAN_ASYNC_PING_REPORT: - { - netapp_pingreport_args_t params; - data = (CHAR*)(event_hdr) + HCI_EVENT_HEADER_SIZE; - STREAM_TO_UINT32(data, NETAPP_PING_PACKETS_SENT_OFFSET, params.packets_sent); - STREAM_TO_UINT32(data, NETAPP_PING_PACKETS_RCVD_OFFSET, params.packets_received); - STREAM_TO_UINT32(data, NETAPP_PING_MIN_RTT_OFFSET, params.min_round_time); - STREAM_TO_UINT32(data, NETAPP_PING_MAX_RTT_OFFSET, params.max_round_time); - STREAM_TO_UINT32(data, NETAPP_PING_AVG_RTT_OFFSET, params.avg_round_time); - - if( tSLInformation.sWlanCB ) - { - tSLInformation.sWlanCB(event_type, (CHAR *)¶ms, sizeof(params)); - } - } - break; - case HCI_EVNT_BSD_TCP_CLOSE_WAIT: - { - data = (CHAR *)(event_hdr) + HCI_EVENT_HEADER_SIZE; - if( tSLInformation.sWlanCB ) - { - //data[0] represents the socket id, for which FIN was received by remote. - //Upon receiving this event, the user can close the socket, or else the - //socket will be closed after inacvitity timeout (by default 60 seconds) - tSLInformation.sWlanCB(event_type, data, 1); - } - } - break; - - //'default' case which means "event not supported" - default: - return (0); - } - return(1); - } - - if ((event_type == HCI_EVNT_SEND) || (event_type == HCI_EVNT_SENDTO) - || (event_type == HCI_EVNT_WRITE)) - { - CHAR *pArg; - INT32 status; - - pArg = M_BSD_RESP_PARAMS_OFFSET(event_hdr); - STREAM_TO_UINT32(pArg, BSD_RSP_PARAMS_STATUS_OFFSET,status); - - if (ERROR_SOCKET_INACTIVE == status) - { - // The only synchronous event that can come from SL device in form of - // command complete is "Command Complete" on data sent, in case SL device - // was unable to transmit - STREAM_TO_UINT8(event_hdr, HCI_EVENT_STATUS_OFFSET, tSLInformation.slTransmitDataError); - update_socket_active_status(M_BSD_RESP_PARAMS_OFFSET(event_hdr)); - - return (1); - } - else - return (0); - } - - //handle a case where unsolicited event arrived, but was not handled by any of the cases above - if ((event_type != tSLInformation.usRxEventOpcode) && (event_type != HCI_EVNT_PATCHES_REQ)) - { - return(1); - } - - return(0); -} - -//***************************************************************************** -// -//! hci_unsolicited_event_handler -//! -//! @param None -//! -//! @return ESUCCESS if successful, EFAIL if an error occurred -//! -//! @brief Parse the incoming unsolicited event packets and issues -//! corresponding event handler. -// -//***************************************************************************** -INT32 hci_unsolicited_event_handler(void) -{ - UINT32 res = 0; - UINT8 *pucReceivedData; - - if (tSLInformation.usEventOrDataReceived != 0) - { - pucReceivedData = (tSLInformation.pucReceivedData); - - if (*pucReceivedData == HCI_TYPE_EVNT) - { - - // In case unsolicited event received - here the handling finished - if (hci_unsol_event_handler((CHAR *)pucReceivedData) == 1) - { - - // There was an unsolicited event received - we can release the buffer - // and clean the event received - tSLInformation.usEventOrDataReceived = 0; - - res = 1; - SpiResumeSpi(); - } - } - } - - return res; -} - -//***************************************************************************** -// -//! set_socket_active_status -//! -//! @param Sd -//! @param Status -//! @return none -//! -//! @brief Check if the socket ID and status are valid and set -//! accordingly the global socket status -// -//***************************************************************************** -void set_socket_active_status(INT32 Sd, INT32 Status) -{ - if(M_IS_VALID_SD(Sd) && M_IS_VALID_STATUS(Status)) - { - socket_active_status &= ~(1 << Sd); /* clean socket's mask */ - socket_active_status |= (Status << Sd); /* set new socket's mask */ - } -} - - -//***************************************************************************** -// -//! hci_event_unsol_flowcontrol_handler -//! -//! @param pEvent pointer to the string contains parameters for IPERF -//! @return ESUCCESS if successful, EFAIL if an error occurred -//! -//! @brief Called in case unsolicited event from type -//! HCI_EVNT_DATA_UNSOL_FREE_BUFF has received. -//! Keep track on the number of packets transmitted and update the -//! number of free buffer in the SL device. -// -//***************************************************************************** -INT32 hci_event_unsol_flowcontrol_handler(CHAR *pEvent) -{ - - INT32 temp, value; - UINT16 i; - UINT16 pusNumberOfHandles=0; - CHAR *pReadPayload; - - STREAM_TO_UINT16((CHAR *)pEvent,HCI_EVENT_HEADER_SIZE,pusNumberOfHandles); - pReadPayload = ((CHAR *)pEvent + - HCI_EVENT_HEADER_SIZE + sizeof(pusNumberOfHandles)); - temp = 0; - - for(i = 0; i < pusNumberOfHandles ; i++) - { - STREAM_TO_UINT16(pReadPayload, FLOW_CONTROL_EVENT_FREE_BUFFS_OFFSET, value); - temp += value; - pReadPayload += FLOW_CONTROL_EVENT_SIZE; - } - - tSLInformation.usNumberOfFreeBuffers += temp; - tSLInformation.NumberOfReleasedPackets += temp; - - return(ESUCCESS); -} - -//***************************************************************************** -// -//! get_socket_active_status -//! -//! @param Sd Socket IS -//! @return Current status of the socket. -//! -//! @brief Retrieve socket status -// -//***************************************************************************** - -INT32 get_socket_active_status(INT32 Sd) -{ - if(M_IS_VALID_SD(Sd)) - { - return (socket_active_status & (1 << Sd)) ? SOCKET_STATUS_INACTIVE : SOCKET_STATUS_ACTIVE; - } - return SOCKET_STATUS_INACTIVE; -} - -//***************************************************************************** -// -//! update_socket_active_status -//! -//! @param resp_params Socket IS -//! @return Current status of the socket. -//! -//! @brief Retrieve socket status -// -//***************************************************************************** -void update_socket_active_status(CHAR *resp_params) -{ - INT32 status, sd; - - STREAM_TO_UINT32(resp_params, BSD_RSP_PARAMS_SOCKET_OFFSET,sd); - STREAM_TO_UINT32(resp_params, BSD_RSP_PARAMS_STATUS_OFFSET,status); - - if(ERROR_SOCKET_INACTIVE == status) - { - set_socket_active_status(sd, SOCKET_STATUS_INACTIVE); - } -} - - -//***************************************************************************** -// -//! SimpleLinkWaitEvent -//! -//! @param usOpcode command operation code -//! @param pRetParams command return parameters -//! -//! @return none -//! -//! @brief Wait for event, pass it to the hci_event_handler and -//! update the event opcode in a global variable. -// -//***************************************************************************** - -void SimpleLinkWaitEvent(UINT16 usOpcode, void *pRetParams) -{ - // In the blocking implementation the control to caller will be returned only - // after the end of current transaction - tSLInformation.usRxEventOpcode = usOpcode; - hci_event_handler(pRetParams, 0, 0); -} - -//***************************************************************************** -// -//! SimpleLinkWaitData -//! -//! @param pBuf data buffer -//! @param from from information -//! @param fromlen from information length -//! -//! @return none -//! -//! @brief Wait for data, pass it to the hci_event_handler -//! and update in a global variable that there is -//! data to read. -// -//***************************************************************************** - -void SimpleLinkWaitData(UINT8 *pBuf, UINT8 *from, UINT8 *fromlen) -{ - // In the blocking implementation the control to caller will be returned only - // after the end of current transaction, i.e. only after data will be received - tSLInformation.usRxDataPending = 1; - hci_event_handler(pBuf, from, fromlen); -} - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** diff --git a/drivers/cc3000/src/hci.c b/drivers/cc3000/src/hci.c deleted file mode 100644 index 4391692b8bc2b..0000000000000 --- a/drivers/cc3000/src/hci.c +++ /dev/null @@ -1,225 +0,0 @@ -/***************************************************************************** -* -* hci.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -//***************************************************************************** -// -//! \addtogroup hci_app -//! @{ -// -//***************************************************************************** - -#include -#include "cc3000_common.h" -#include "hci.h" -#include "ccspi.h" -#include "evnt_handler.h" -#include "wlan.h" - -#define SL_PATCH_PORTION_SIZE (1000) - - -//***************************************************************************** -// -//! hci_command_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the command's arguments buffer -//! @param ucArgsLength length of the arguments -//! -//! @return none -//! -//! @brief Initiate an HCI command. -// -//***************************************************************************** -UINT16 hci_command_send(UINT16 usOpcode, UINT8 *pucBuff, UINT8 ucArgsLength) -{ - UINT8 *stream; - - stream = (pucBuff + SPI_HEADER_SIZE); - - UINT8_TO_STREAM(stream, HCI_TYPE_CMND); - stream = UINT16_TO_STREAM(stream, usOpcode); - UINT8_TO_STREAM(stream, ucArgsLength); - - //Update the opcode of the event we will be waiting for - SpiWrite(pucBuff, ucArgsLength + SIMPLE_LINK_HCI_CMND_HEADER_SIZE); - - return(0); -} - -//***************************************************************************** -// -//! hci_data_send -//! -//! @param usOpcode command operation code -//! @param ucArgs pointer to the command's arguments buffer -//! @param usArgsLength length of the arguments -//! @param ucTail pointer to the data buffer -//! @param usTailLength buffer length -//! -//! @return none -//! -//! @brief Initiate an HCI data write operation -// -//***************************************************************************** -INT32 hci_data_send(UINT8 ucOpcode, - UINT8 *ucArgs, - UINT16 usArgsLength, - UINT16 usDataLength, - const UINT8 *ucTail, - UINT16 usTailLength) -{ - UINT8 *stream; - - stream = ((ucArgs) + SPI_HEADER_SIZE); - - UINT8_TO_STREAM(stream, HCI_TYPE_DATA); - UINT8_TO_STREAM(stream, ucOpcode); - UINT8_TO_STREAM(stream, usArgsLength); - stream = UINT16_TO_STREAM(stream, usArgsLength + usDataLength + usTailLength); - - // Send the packet over the SPI - SpiWrite(ucArgs, SIMPLE_LINK_HCI_DATA_HEADER_SIZE + usArgsLength + usDataLength + usTailLength); - - return(ESUCCESS); -} - - -//***************************************************************************** -// -//! hci_data_command_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the data buffer -//! @param ucArgsLength arguments length -//! @param ucDataLength data length -//! -//! @return none -//! -//! @brief Prepeare HCI header and initiate an HCI data write operation -// -//***************************************************************************** -void hci_data_command_send(UINT16 usOpcode, UINT8 *pucBuff, UINT8 ucArgsLength,UINT16 ucDataLength) -{ - UINT8 *stream = (pucBuff + SPI_HEADER_SIZE); - - UINT8_TO_STREAM(stream, HCI_TYPE_DATA); - UINT8_TO_STREAM(stream, usOpcode); - UINT8_TO_STREAM(stream, ucArgsLength); - stream = UINT16_TO_STREAM(stream, ucArgsLength + ucDataLength); - - // Send the command over SPI on data channel - SpiWrite(pucBuff, ucArgsLength + ucDataLength + SIMPLE_LINK_HCI_DATA_CMND_HEADER_SIZE); - - return; -} - -//***************************************************************************** -// -//! hci_patch_send -//! -//! @param usOpcode command operation code -//! @param pucBuff pointer to the command's arguments buffer -//! @param patch pointer to patch content buffer -//! @param usDataLength data length -//! -//! @return none -//! -//! @brief Prepeare HCI header and initiate an HCI patch write operation -// -//***************************************************************************** -void hci_patch_send(UINT8 ucOpcode, UINT8 *pucBuff, CHAR *patch, UINT16 usDataLength) -{ - UINT8 *data_ptr = (pucBuff + SPI_HEADER_SIZE); - UINT16 usTransLength; - UINT8 *stream = (pucBuff + SPI_HEADER_SIZE); - - UINT8_TO_STREAM(stream, HCI_TYPE_PATCH); - UINT8_TO_STREAM(stream, ucOpcode); - stream = UINT16_TO_STREAM(stream, usDataLength + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE); - - if (usDataLength <= SL_PATCH_PORTION_SIZE) - { - UINT16_TO_STREAM(stream, usDataLength); - stream = UINT16_TO_STREAM(stream, usDataLength); - memcpy((pucBuff + SPI_HEADER_SIZE) + HCI_PATCH_HEADER_SIZE, patch, usDataLength); - - // Update the opcode of the event we will be waiting for - SpiWrite(pucBuff, usDataLength + HCI_PATCH_HEADER_SIZE); - } - else - { - - usTransLength = (usDataLength/SL_PATCH_PORTION_SIZE); - UINT16_TO_STREAM(stream, usDataLength + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE + usTransLength*SIMPLE_LINK_HCI_PATCH_HEADER_SIZE); - stream = UINT16_TO_STREAM(stream, SL_PATCH_PORTION_SIZE); - memcpy(pucBuff + SPI_HEADER_SIZE + HCI_PATCH_HEADER_SIZE, patch, SL_PATCH_PORTION_SIZE); - usDataLength -= SL_PATCH_PORTION_SIZE; - patch += SL_PATCH_PORTION_SIZE; - - // Update the opcode of the event we will be waiting for - SpiWrite(pucBuff, SL_PATCH_PORTION_SIZE + HCI_PATCH_HEADER_SIZE); - - while (usDataLength) - { - if (usDataLength <= SL_PATCH_PORTION_SIZE) - { - usTransLength = usDataLength; - usDataLength = 0; - - } - else - { - usTransLength = SL_PATCH_PORTION_SIZE; - usDataLength -= usTransLength; - } - - *(UINT16 *)data_ptr = usTransLength; - memcpy(data_ptr + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE, patch, usTransLength); - patch += usTransLength; - - // Update the opcode of the event we will be waiting for - SpiWrite((UINT8 *)data_ptr, usTransLength + sizeof(usTransLength)); - } - } -} - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -// -//***************************************************************************** diff --git a/drivers/cc3000/src/inet_ntop.c b/drivers/cc3000/src/inet_ntop.c deleted file mode 100644 index 83242efa00b07..0000000000000 --- a/drivers/cc3000/src/inet_ntop.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 1996-2001 Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM - * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL - * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING - * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, - * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include "cc3000_common.h" -#include "socket.h" -#include "inet_ntop.h" - -// We can't include stdio.h because it defines _types_fd_set, but we -// need to use the CC3000 version of this type. So we must provide -// our own declaration of snprintf. Grrr. -int snprintf(char *str, size_t size, const char *fmt, ...); - -#define IN6ADDRSZ 16 -#define INADDRSZ 4 -#define INT16SZ 2 - -#define ENOSPC (28) -#define EAFNOSUPPORT (106) -#define SET_ERRNO(err) (CC3000_EXPORT(errno)=-err) - -/* - * Format an IPv4 address, more or less like inet_ntoa(). - * - * Returns `dst' (as a const) - * Note: - * - uses no statics - * - takes a unsigned char* not an in_addr as input - */ -static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size) -{ - size_t len; - char tmp[sizeof "255.255.255.255"]; - - tmp[0] = '\0'; - (void)snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d", - ((int)((unsigned char)src[3])) & 0xff, - ((int)((unsigned char)src[2])) & 0xff, - ((int)((unsigned char)src[1])) & 0xff, - ((int)((unsigned char)src[0])) & 0xff); - - len = strlen(tmp); - if(len == 0 || len >= size) - { - SET_ERRNO(ENOSPC); - return (NULL); - } - strcpy(dst, tmp); - return dst; -} - -#ifdef ENABLE_IPV6 -/* - * Convert IPv6 binary address into presentation (printable) format. - */ -static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size) -{ - /* - * Note that int32_t and int16_t need only be "at least" large enough - * to contain a value of the specified size. On some systems, like - * Crays, there is no such thing as an integer variable with 16 bits. - * Keep this in mind if you think this function should have been coded - * to use pointer overlays. All the world's not a VAX. - */ - char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")]; - char *tp; - struct { - long base; - long len; - } best, cur; - unsigned long words[IN6ADDRSZ / INT16SZ]; - int i; - - /* Preprocess: - * Copy the input (bytewise) array into a wordwise array. - * Find the longest run of 0x00's in src[] for :: shorthanding. - */ - memset(words, '\0', sizeof(words)); - for (i = 0; i < IN6ADDRSZ; i++) - words[i/2] |= (src[i] << ((1 - (i % 2)) << 3)); - - best.base = -1; - cur.base = -1; - best.len = 0; - cur.len = 0; - - for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) - { - if(words[i] == 0) - { - if(cur.base == -1) - cur.base = i, cur.len = 1; - else - cur.len++; - } - else if(cur.base != -1) - { - if(best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - if((cur.base != -1) && (best.base == -1 || cur.len > best.len)) - best = cur; - if(best.base != -1 && best.len < 2) - best.base = -1; - - /* Format the result. - */ - tp = tmp; - for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) - { - /* Are we inside the best run of 0x00's? - */ - if(best.base != -1 && i >= best.base && i < (best.base + best.len)) - { - if(i == best.base) - *tp++ = ':'; - continue; - } - - /* Are we following an initial run of 0x00s or any real hex? - */ - if(i != 0) - *tp++ = ':'; - - /* Is this address an encapsulated IPv4? - */ - if(i == 6 && best.base == 0 && - (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) - { - if(!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp))) - { - SET_ERRNO(ENOSPC); - return (NULL); - } - tp += strlen(tp); - break; - } - tp += snprintf(tp, 5, "%lx", words[i]); - } - - /* Was it a trailing run of 0x00's? - */ - if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) - *tp++ = ':'; - *tp++ = '\0'; - - /* Check for overflow, copy, and we're done. - */ - if((size_t)(tp - tmp) > size) - { - SET_ERRNO(ENOSPC); - return (NULL); - } - strcpy(dst, tmp); - return dst; -} -#endif /* ENABLE_IPV6 */ - -/* - * Convert a network format address to presentation format. - * - * Returns pointer to presentation format address (`buf'). - * Returns NULL on error and errno set with the specific - * error, EAFNOSUPPORT or ENOSPC. - * - * On Windows we store the error in the thread errno, not - * in the winsock error code. This is to avoid loosing the - * actual last winsock error. So use macro ERRNO to fetch the - * errno this funtion sets when returning NULL, not SOCKERRNO. - */ -char *inet_ntop(int af, const void *src, char *buf, size_t size) -{ - switch (af) { - case AF_INET: - return inet_ntop4((const unsigned char*)src, buf, size); -#ifdef ENABLE_IPV6 - case AF_INET6: - return inet_ntop6((const unsigned char*)src, buf, size); -#endif - default: - SET_ERRNO(EAFNOSUPPORT); - return NULL; - } -} diff --git a/drivers/cc3000/src/inet_pton.c b/drivers/cc3000/src/inet_pton.c deleted file mode 100644 index 5f5ae5f9d7f1e..0000000000000 --- a/drivers/cc3000/src/inet_pton.c +++ /dev/null @@ -1,216 +0,0 @@ -/* This is from the BIND 4.9.4 release, modified to compile by itself */ - -/* Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - */ - -#include -#include "cc3000_common.h" -#include "socket.h" -#include "inet_pton.h" - -#define IN6ADDRSZ 16 -#define INADDRSZ 4 -#define INT16SZ 2 - -static int inet_pton4(const char *src, unsigned char *dst); -#ifdef ENABLE_IPV6 -static int inet_pton6(const char *src, unsigned char *dst); -#endif - -#define EAFNOSUPPORT (106) -#define SET_ERRNO(err) (CC3000_EXPORT(errno)=-err) - -/* int - * inet_pton(af, src, dst) - * convert from presentation format (which usually means ASCII printable) - * to network format (which is usually some kind of binary format). - * return: - * 1 if the address was valid for the specified address family - * 0 if the address wasn't valid (`dst' is untouched in this case) - * -1 if some other error occurred (`dst' is untouched in this case, too) - * notice: - * On Windows we store the error in the thread errno, not - * in the winsock error code. This is to avoid loosing the - * actual last winsock error. So use macro ERRNO to fetch the - * errno this funtion sets when returning (-1), not SOCKERRNO. - * author: - * Paul Vixie, 1996. - */ -int inet_pton(int af, const char *src, void *dst) -{ - switch (af) { - case AF_INET: - return (inet_pton4(src, (unsigned char *)dst)); -#ifdef ENABLE_IPV6 - case AF_INET6: - return (inet_pton6(src, (unsigned char *)dst)); -#endif - default: - SET_ERRNO(EAFNOSUPPORT); - return (-1); - } - /* NOTREACHED */ -} - -/* int - * inet_pton4(src, dst) - * like inet_aton() but without all the hexadecimal and shorthand. - * return: - * 1 if `src' is a valid dotted quad, else 0. - * notice: - * does not touch `dst' unless it's returning 1. - * author: - * Paul Vixie, 1996. - */ -static int inet_pton4(const char *src, unsigned char *dst) -{ - static const char digits[] = "0123456789"; - int saw_digit, octets, ch; - unsigned char tmp[INADDRSZ], *tp; - - saw_digit = 0; - octets = 0; - tp = tmp; - *tp = 0; - while((ch = *src++) != '\0') { - const char *pch; - - if((pch = strchr(digits, ch)) != NULL) { - unsigned int val = *tp * 10 + (unsigned int)(pch - digits); - - if(saw_digit && *tp == 0) - return (0); - if(val > 255) - return (0); - *tp = (unsigned char)val; - if(! saw_digit) { - if(++octets > 4) - return (0); - saw_digit = 1; - } - } - else if(ch == '.' && saw_digit) { - if(octets == 4) - return (0); - *++tp = 0; - saw_digit = 0; - } - else - return (0); - } - if(octets < 4) - return (0); - memcpy(dst, tmp, INADDRSZ); - return (1); -} - -#ifdef ENABLE_IPV6 -/* int - * inet_pton6(src, dst) - * convert presentation level address to network order binary form. - * return: - * 1 if `src' is a valid [RFC1884 2.2] address, else 0. - * notice: - * (1) does not touch `dst' unless it's returning 1. - * (2) :: in a full address is silently ignored. - * credit: - * inspired by Mark Andrews. - * author: - * Paul Vixie, 1996. - */ -static int inet_pton6(const char *src, unsigned char *dst) -{ - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; - unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; - const char *xdigits, *curtok; - int ch, saw_xdigit; - unsigned int val; - - memset((tp = tmp), 0, IN6ADDRSZ); - endp = tp + IN6ADDRSZ; - colonp = NULL; - /* Leading :: requires some special handling. */ - if(*src == ':') - if(*++src != ':') - return (0); - curtok = src; - saw_xdigit = 0; - val = 0; - while((ch = *src++) != '\0') { - const char *pch; - - if((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); - if(pch != NULL) { - val <<= 4; - val |= (pch - xdigits); - if(++saw_xdigit > 4) - return (0); - continue; - } - if(ch == ':') { - curtok = src; - if(!saw_xdigit) { - if(colonp) - return (0); - colonp = tp; - continue; - } - if(tp + INT16SZ > endp) - return (0); - *tp++ = (unsigned char) (val >> 8) & 0xff; - *tp++ = (unsigned char) val & 0xff; - saw_xdigit = 0; - val = 0; - continue; - } - if(ch == '.' && ((tp + INADDRSZ) <= endp) && - inet_pton4(curtok, tp) > 0) { - tp += INADDRSZ; - saw_xdigit = 0; - break; /* '\0' was seen by inet_pton4(). */ - } - return (0); - } - if(saw_xdigit) { - if(tp + INT16SZ > endp) - return (0); - *tp++ = (unsigned char) (val >> 8) & 0xff; - *tp++ = (unsigned char) val & 0xff; - } - if(colonp != NULL) { - /* - * Since some memmove()'s erroneously fail to handle - * overlapping regions, we'll do the shift by hand. - */ - const long n = tp - colonp; - long i; - - if(tp == endp) - return (0); - for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; - colonp[n - i] = 0; - } - tp = endp; - } - if(tp != endp) - return (0); - memcpy(dst, tmp, IN6ADDRSZ); - return (1); -} -#endif /* ENABLE_IPV6 */ diff --git a/drivers/cc3000/src/netapp.c b/drivers/cc3000/src/netapp.c deleted file mode 100644 index a6f60feda0d4c..0000000000000 --- a/drivers/cc3000/src/netapp.c +++ /dev/null @@ -1,459 +0,0 @@ -/***************************************************************************** -* -* netapp.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -#include -#include "netapp.h" -#include "hci.h" -#include "socket.h" -#include "evnt_handler.h" -#include "nvmem.h" - -#define MIN_TIMER_VAL_SECONDS 10 -#define MIN_TIMER_SET(t) if ((0 != t) && (t < MIN_TIMER_VAL_SECONDS)) \ - { \ - t = MIN_TIMER_VAL_SECONDS; \ - } - - -#define NETAPP_DHCP_PARAMS_LEN (20) -#define NETAPP_SET_TIMER_PARAMS_LEN (20) -#define NETAPP_SET_DEBUG_LEVEL_PARAMS_LEN (4) -#define NETAPP_PING_SEND_PARAMS_LEN (16) - - -//***************************************************************************** -// -//! netapp_config_mac_adrress -//! -//! @param mac device mac address, 6 bytes. Saved: yes -//! -//! @return return on success 0, otherwise error. -//! -//! @brief Configure device MAC address and store it in NVMEM. -//! The value of the MAC address configured through the API will -//! be stored in CC3000 non volatile memory, thus preserved -//! over resets. -// -//***************************************************************************** -INT32 netapp_config_mac_adrress(UINT8 * mac) -{ - return nvmem_set_mac_address(mac); -} - -//***************************************************************************** -// -//! netapp_dhcp -//! -//! @param aucIP device mac address, 6 bytes. Saved: yes -//! @param aucSubnetMask device mac address, 6 bytes. Saved: yes -//! @param aucDefaultGateway device mac address, 6 bytes. Saved: yes -//! @param aucDNSServer device mac address, 6 bytes. Saved: yes -//! -//! @return return on success 0, otherwise error. -//! -//! @brief netapp_dhcp is used to configure the network interface, -//! static or dynamic (DHCP).\n In order to activate DHCP mode, -//! aucIP, aucSubnetMask, aucDefaultGateway must be 0. -//! The default mode of CC3000 is DHCP mode. -//! Note that the configuration is saved in non volatile memory -//! and thus preserved over resets. -//! -//! @note If the mode is altered a reset of CC3000 device is required -//! in order to apply changes.\nAlso note that asynchronous event -//! of DHCP_EVENT, which is generated when an IP address is -//! allocated either by the DHCP server or due to static -//! allocation is generated only upon a connection to the -//! AP was established. -//! -//***************************************************************************** -INT32 netapp_dhcp(UINT32 *aucIP, UINT32 *aucSubnetMask,UINT32 *aucDefaultGateway, UINT32 *aucDNSServer) -{ - INT8 scRet; - UINT8 *ptr; - UINT8 *args; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - ARRAY_TO_STREAM(args,aucIP,4); - ARRAY_TO_STREAM(args,aucSubnetMask,4); - ARRAY_TO_STREAM(args,aucDefaultGateway,4); - args = UINT32_TO_STREAM(args, 0); - ARRAY_TO_STREAM(args,aucDNSServer,4); - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_DHCP, ptr, NETAPP_DHCP_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_DHCP, &scRet); - - return(scRet); -} - - -//***************************************************************************** -// -//! netapp_timeout_values -//! -//! @param aucDHCP DHCP lease time request, also impact -//! the DHCP renew timeout. Range: [0-0xffffffff] seconds, -//! 0 or 0xffffffff == infinity lease timeout. -//! Resolution:10 seconds. Influence: only after -//! reconnecting to the AP. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 seconds. -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 14400 seconds. -//! -//! @param aucARP ARP refresh timeout, if ARP entry is not updated by -//! incoming packet, the ARP entry will be deleted by -//! the end of the timeout. -//! Range: [0-0xffffffff] seconds, 0 == infinity ARP timeout -//! Resolution: 10 seconds. Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 seconds -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 3600 seconds. -//! -//! @param aucKeepalive Keepalive event sent by the end of keepalive timeout -//! Range: [0-0xffffffff] seconds, 0 == infinity timeout -//! Resolution: 10 seconds. -//! Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 sec -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 10 seconds. -//! -//! @param aucInactivity Socket inactivity timeout, socket timeout is -//! refreshed by incoming or outgoing packet, by the -//! end of the socket timeout the socket will be closed -//! Range: [0-0xffffffff] sec, 0 == infinity timeout. -//! Resolution: 10 seconds. Influence: on runtime. -//! Minimal bound value: MIN_TIMER_VAL_SECONDS - 10 sec -//! The parameter is saved into the CC3000 NVMEM. -//! The default value on CC3000 is 60 seconds. -//! -//! @return return on success 0, otherwise error. -//! -//! @brief Set new timeout values. Function set new timeout values for: -//! DHCP lease timeout, ARP refresh timeout, keepalive event -//! timeout and socket inactivity timeout -//! -//! @note If a parameter set to non zero value which is less than 10s, -//! it will be set automatically to 10s. -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 netapp_timeout_values(UINT32 *aucDHCP, UINT32 *aucARP,UINT32 *aucKeepalive, UINT32 *aucInactivity) -{ - INT8 scRet; - UINT8 *ptr; - UINT8 *args; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Set minimal values of timers - MIN_TIMER_SET(*aucDHCP) - MIN_TIMER_SET(*aucARP) - MIN_TIMER_SET(*aucKeepalive) - MIN_TIMER_SET(*aucInactivity) - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, *aucDHCP); - args = UINT32_TO_STREAM(args, *aucARP); - args = UINT32_TO_STREAM(args, *aucKeepalive); - args = UINT32_TO_STREAM(args, *aucInactivity); - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_SET_TIMERS, ptr, NETAPP_SET_TIMER_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_SET_TIMERS, &scRet); - - return(scRet); -} -#endif - - -//***************************************************************************** -// -//! netapp_ping_send -//! -//! @param ip destination IP address -//! @param pingAttempts number of echo requests to send -//! @param pingSize send buffer size which may be up to 1400 bytes -//! @param pingTimeout Time to wait for a response,in milliseconds. -//! -//! @return return on success 0, otherwise error. -//! -//! @brief send ICMP ECHO_REQUEST to network hosts -//! -//! @note If an operation finished successfully asynchronous ping report -//! event will be generated. The report structure is as defined -//! by structure netapp_pingreport_args_t. -//! -//! @warning Calling this function while a previous Ping Requests are in -//! progress will stop the previous ping request. -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 - netapp_ping_send(UINT32 *ip, UINT32 ulPingAttempts, UINT32 ulPingSize, UINT32 ulPingTimeout) -{ - INT8 scRet; - UINT8 *ptr, *args; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, *ip); - args = UINT32_TO_STREAM(args, ulPingAttempts); - args = UINT32_TO_STREAM(args, ulPingSize); - args = UINT32_TO_STREAM(args, ulPingTimeout); - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_PING_SEND, ptr, NETAPP_PING_SEND_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_PING_SEND, &scRet); - - return(scRet); -} -#endif - -//***************************************************************************** -// -//! netapp_ping_report -//! -//! @param none -//! -//! @return none -//! -//! @brief Request for ping status. This API triggers the CC3000 to send -//! asynchronous events: HCI_EVNT_WLAN_ASYNC_PING_REPORT. -//! This event will carry the report structure: -//! netapp_pingreport_args_t. This structure is filled in with ping -//! results up till point of triggering API. -//! netapp_pingreport_args_t:\n packets_sent - echo sent, -//! packets_received - echo reply, min_round_time - minimum -//! round time, max_round_time - max round time, -//! avg_round_time - average round time -//! -//! @note When a ping operation is not active, the returned structure -//! fields are 0. -//! -//***************************************************************************** - - -#ifndef CC3000_TINY_DRIVER -void netapp_ping_report() -{ - UINT8 *ptr; - ptr = tSLInformation.pucTxCommandBuffer; - INT8 scRet; - - scRet = EFAIL; - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_PING_REPORT, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_PING_REPORT, &scRet); -} -#endif - -//***************************************************************************** -// -//! netapp_ping_stop -//! -//! @param none -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief Stop any ping request. -//! -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 netapp_ping_stop() -{ - INT8 scRet; - UINT8 *ptr; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_PING_STOP, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_PING_STOP, &scRet); - - return(scRet); -} -#endif - -//***************************************************************************** -// -//! netapp_ipconfig -//! -//! @param[out] ipconfig This argument is a pointer to a -//! tNetappIpconfigRetArgs structure. This structure is -//! filled in with the network interface configuration. -//! tNetappIpconfigRetArgs:\n aucIP - ip address, -//! aucSubnetMask - mask, aucDefaultGateway - default -//! gateway address, aucDHCPServer - dhcp server address -//! aucDNSServer - dns server address, uaMacAddr - mac -//! address, uaSSID - connected AP ssid -//! -//! @return none -//! -//! @brief Obtain the CC3000 Network interface information. -//! Note that the information is available only after the WLAN -//! connection was established. Calling this function before -//! associated, will cause non-defined values to be returned. -//! -//! @note The function is useful for figuring out the IP Configuration of -//! the device when DHCP is used and for figuring out the SSID of -//! the Wireless network the device is associated with. -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -void netapp_ipconfig( tNetappIpconfigRetArgs * ipconfig ) -{ - UINT8 *ptr; - - ptr = tSLInformation.pucTxCommandBuffer; - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_IPCONFIG, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_IPCONFIG, ipconfig ); - -} -#else -void netapp_ipconfig( tNetappIpconfigRetArgs * ipconfig ) -{ - -} -#endif - -//***************************************************************************** -// -//! netapp_arp_flush -//! -//! @param none -//! -//! @return none -//! -//! @brief Flushes ARP table -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 netapp_arp_flush(void) -{ - INT8 scRet; - UINT8 *ptr; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - - // Initiate a HCI command - hci_command_send(HCI_NETAPP_ARP_FLUSH, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_NETAPP_ARP_FLUSH, &scRet); - - return(scRet); -} -#endif - -//***************************************************************************** -// -//! netapp_set_debug_level -//! -//! @param[in] level debug level. Bitwise [0-8], -//! 0(disable)or 1(enable).\n Bitwise map: 0 - Critical -//! message, 1 information message, 2 - core messages, 3 - -//! HCI messages, 4 - Network stack messages, 5 - wlan -//! messages, 6 - wlan driver messages, 7 - epprom messages, -//! 8 - general messages. Default: 0x13f. Saved: no -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Debug messages sent via the UART debug channel, this function -//! enable/disable the debug level -//! -//***************************************************************************** - - -#ifndef CC3000_TINY_DRIVER -INT32 netapp_set_debug_level(UINT32 ulLevel) -{ - INT8 scRet; - UINT8 *ptr, *args; - - scRet = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // - // Fill in temporary command buffer - // - args = UINT32_TO_STREAM(args, ulLevel); - - - // - // Initiate a HCI command - // - hci_command_send(HCI_NETAPP_SET_DEBUG_LEVEL, ptr, NETAPP_SET_DEBUG_LEVEL_PARAMS_LEN); - - // - // Wait for command complete event - // - SimpleLinkWaitEvent(HCI_NETAPP_SET_DEBUG_LEVEL, &scRet); - - return(scRet); - -} -#endif diff --git a/drivers/cc3000/src/nvmem.c b/drivers/cc3000/src/nvmem.c deleted file mode 100644 index c6e170a746504..0000000000000 --- a/drivers/cc3000/src/nvmem.c +++ /dev/null @@ -1,334 +0,0 @@ -/***************************************************************************** -* -* nvmem.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -//***************************************************************************** -// -//! \addtogroup nvmem_api -//! @{ -// -//***************************************************************************** - -#include -#include "nvmem.h" -#include "hci.h" -#include "socket.h" -#include "evnt_handler.h" - -//***************************************************************************** -// -// Prototypes for the structures for APIs. -// -//***************************************************************************** - -#define NVMEM_READ_PARAMS_LEN (12) -#define NVMEM_CREATE_PARAMS_LEN (8) -#define NVMEM_WRITE_PARAMS_LEN (16) - -//***************************************************************************** -// -//! nvmem_read -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_NVS_FILEID, NVMEM_NVS_SHADOW_FILEID, -//! NVMEM_WLAN_CONFIG_FILEID, NVMEM_WLAN_CONFIG_SHADOW_FILEID, -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! NVMEM_MAC_FILEID, NVMEM_FRONTEND_VARS_FILEID, -//! NVMEM_IP_CONFIG_FILEID, NVMEM_IP_CONFIG_SHADOW_FILEID, -//! NVMEM_BOOTLOADER_SP_FILEID, NVMEM_RM_FILEID, -//! and user files 12-15. -//! @param ulLength number of bytes to read -//! @param ulOffset ulOffset in file from where to read -//! @param buff output buffer pointer -//! -//! @return on success 0, error otherwise. -//! -//! @brief Reads data from the file referred by the ulFileId parameter. -//! Reads data from file ulOffset till length. Err if the file can't -//! be used, is invalid, or if the read is out of bounds. -//! -//***************************************************************************** - -INT32 nvmem_read(UINT32 ulFileId, UINT32 ulLength, UINT32 ulOffset, UINT8 *buff) -{ - UINT8 ucStatus = 0xFF; - UINT8 *ptr; - UINT8 *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, ulFileId); - args = UINT32_TO_STREAM(args, ulLength); - args = UINT32_TO_STREAM(args, ulOffset); - - // Initiate a HCI command - hci_command_send(HCI_CMND_NVMEM_READ, ptr, NVMEM_READ_PARAMS_LEN); - SimpleLinkWaitEvent(HCI_CMND_NVMEM_READ, &ucStatus); - - // In case there is data - read it - even if an error code is returned - // Note: It is the user responsibility to ignore the data in case of an error code - - // Wait for the data in a synchronous way. Here we assume that the buffer is - // big enough to store also parameters of nvmem - - SimpleLinkWaitData(buff, 0, 0); - - return(ucStatus); -} - -//***************************************************************************** -// -//! nvmem_write -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! NVMEM_MAC_FILEID, NVMEM_BOOTLOADER_SP_FILEID, -//! and user files 12-15. -//! @param ulLength number of bytes to write -//! @param ulEntryOffset offset in file to start write operation from -//! @param buff data to write -//! -//! @return on success 0, error otherwise. -//! -//! @brief Write data to nvmem. -//! writes data to file referred by the ulFileId parameter. -//! Writes data to file ulOffset till ulLength.The file id will be -//! marked invalid till the write is done. The file entry doesn't -//! need to be valid - only allocated. -//! -//***************************************************************************** - -INT32 nvmem_write(UINT32 ulFileId, UINT32 ulLength, UINT32 ulEntryOffset, UINT8 *buff) -{ - INT32 iRes; - UINT8 *ptr; - UINT8 *args; - - iRes = EFAIL; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, ulFileId); - args = UINT32_TO_STREAM(args, 12); - args = UINT32_TO_STREAM(args, ulLength); - args = UINT32_TO_STREAM(args, ulEntryOffset); - - memcpy((ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE + - NVMEM_WRITE_PARAMS_LEN),buff,ulLength); - - // Initiate a HCI command but it will come on data channel - hci_data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN, - ulLength); - - SimpleLinkWaitEvent(HCI_EVNT_NVMEM_WRITE, &iRes); - - return(iRes); -} - - -//***************************************************************************** -// -//! nvmem_set_mac_address -//! -//! @param mac mac address to be set -//! -//! @return on success 0, error otherwise. -//! -//! @brief Write MAC address to EEPROM. -//! mac address as appears over the air (OUI first) -//! -//***************************************************************************** - -UINT8 nvmem_set_mac_address(UINT8 *mac) -{ - return nvmem_write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac); -} - -//***************************************************************************** -// -//! nvmem_get_mac_address -//! -//! @param[out] mac mac address -//! -//! @return on success 0, error otherwise. -//! -//! @brief Read MAC address from EEPROM. -//! mac address as appears over the air (OUI first) -//! -//***************************************************************************** - -UINT8 nvmem_get_mac_address(UINT8 *mac) -{ - return nvmem_read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac); -} - -//***************************************************************************** -// -//! nvmem_write_patch -//! -//! @param ulFileId nvmem file id:\n -//! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, -//! @param spLength number of bytes to write -//! @param spData SP data to write -//! -//! @return on success 0, error otherwise. -//! -//! @brief program a patch to a specific file ID. -//! The SP data is assumed to be organized in 2-dimensional. -//! Each line is SP_PORTION_SIZE bytes long. Actual programming is -//! applied in SP_PORTION_SIZE bytes portions. -//! -//***************************************************************************** - -UINT8 nvmem_write_patch(UINT32 ulFileId, UINT32 spLength, const UINT8 *spData) -{ - UINT8 status = 0; - UINT16 offset = 0; - UINT8* spDataPtr = (UINT8*)spData; - - while ((status == 0) && (spLength >= SP_PORTION_SIZE)) - { - status = nvmem_write(ulFileId, SP_PORTION_SIZE, offset, spDataPtr); - offset += SP_PORTION_SIZE; - spLength -= SP_PORTION_SIZE; - spDataPtr += SP_PORTION_SIZE; - } - - if (status !=0) - { - // NVMEM error occurred - return status; - } - - if (spLength != 0) - { - // if reached here, a reminder is left - status = nvmem_write(ulFileId, spLength, offset, spDataPtr); - } - - return status; -} - -//***************************************************************************** -// -//! nvmem_read_sp_version -//! -//! @param[out] patchVer first number indicates package ID and the second -//! number indicates package build number -//! -//! @return on success 0, error otherwise. -//! -//! @brief Read patch version. read package version (WiFi FW patch, -//! driver-supplicant-NS patch, bootloader patch) -//! -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -UINT8 nvmem_read_sp_version(UINT8* patchVer) -{ - UINT8 *ptr; - // 1st byte is the status and the rest is the SP version - UINT8 retBuf[5]; - - ptr = tSLInformation.pucTxCommandBuffer; - - // Initiate a HCI command, no args are required - hci_command_send(HCI_CMND_READ_SP_VERSION, ptr, 0); - SimpleLinkWaitEvent(HCI_CMND_READ_SP_VERSION, retBuf); - - // package ID - *patchVer = retBuf[3]; - // package build number - *(patchVer+1) = retBuf[4]; - - return(retBuf[0]); -} -#endif - -//***************************************************************************** -// -//! nvmem_create_entry -//! -//! @param ulFileId nvmem file Id:\n -//! * NVMEM_AES128_KEY_FILEID: 12 -//! * NVMEM_SHARED_MEM_FILEID: 13 -//! * and fileIDs 14 and 15 -//! @param ulNewLen entry ulLength -//! -//! @return on success 0, error otherwise. -//! -//! @brief Create new file entry and allocate space on the NVMEM. -//! Applies only to user files. -//! Modify the size of file. -//! If the entry is unallocated - allocate it to size -//! ulNewLen (marked invalid). -//! If it is allocated then deallocate it first. -//! To just mark the file as invalid without resizing - -//! set ulNewLen=0. -//! -//***************************************************************************** - -INT32 nvmem_create_entry(UINT32 ulFileId, UINT32 ulNewLen) -{ - UINT8 *ptr; - UINT8 *args; - UINT8 retval; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, ulFileId); - args = UINT32_TO_STREAM(args, ulNewLen); - - // Initiate a HCI command - hci_command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN); - - SimpleLinkWaitEvent(HCI_CMND_NVMEM_CREATE_ENTRY, &retval); - - return(retval); -} - - - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** diff --git a/drivers/cc3000/src/patch.c b/drivers/cc3000/src/patch.c deleted file mode 100644 index 227be3c9f2a30..0000000000000 --- a/drivers/cc3000/src/patch.c +++ /dev/null @@ -1,117 +0,0 @@ -/***************************************************************************** - * - * {PatchProgrammer_DR_Patch.c} - * - * Burn Patches to EEPROM - * - * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/ - * ALL RIGHTS RESERVED - * - *****************************************************************************/ -// -// Service Pack version P1.13.7.15.28 - Driver patches -// This file contains the CC3K driver and firmware patches -// From CC3000-FRAM-PATCH V:1.13.7 15-MAY-2014 - -unsigned short fw_length = 5700; -unsigned short drv_length = 8024; - -const unsigned char wlan_drv_patch[8024] = { 0x00, 0x01, 0x00, 0x00, 0x50, 0x1F, 0x00, 0x00, 0xF0, 0x03, 0x18, 0x00, 0xE4, 0x62, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7A, 0x63, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x64, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x20, 0x0C, 0x49, 0x08, 0x60, 0x0C, 0x48, 0x19, 0x30, 0xF7, 0x46, 0x30, 0xB5, 0x05, 0x1C, 0xAC, 0x69, 0x68, 0x68, 0x5F, 0x30, 0x09, 0xD1, 0x60, 0x6B, 0x0C, 0x38, 0x01, 0x21, 0x8E, 0x46, 0x06, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x00, 0x20, 0x60, 0x63, 0xAC, 0x69, 0x6C, 0x60, 0x04, 0x48, 0x5B, 0x30, 0x30, 0xBD, 0x40, 0x3B, 0x08, 0x00, 0x49, 0xD0, 0x01, 0x00, 0x09, 0xEA, 0x02, 0x00, 0x91, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA6, 0x64, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x3C, 0x65, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xB5, 0x85, 0xB0, 0x05, 0x1C, 0xA8, 0x88, 0x00, 0x90, 0x28, 0x89, 0x01, 0x90, 0xE9, 0x68, 0x02, 0x91, 0x28, 0x7C, 0x03, 0x90, 0x2A, 0x6A, 0x00, 0x20, 0x17, 0x56, 0x68, 0x60, 0x00, 0x29, 0x4C, 0xD0, 0x00, 0x2F, 0x4A, 0xDC , -0xCA, 0x49, 0x0C, 0x1C, 0x08, 0x26, 0x04, 0x90, 0x21, 0x88, 0x00, 0x98, 0x81, 0x42, 0x0C, 0xD1, 0x62, 0x88, 0x01, 0x98, 0x82, 0x42, 0x08, 0xD1, 0x46, 0x20, 0x02, 0x5D, 0x03, 0x98, 0x82, 0x42, 0x03, 0xD1, 0x0E, 0x20, 0x00, 0x57, 0xB8, 0x42, 0x0A, 0xD0, 0x46, 0x20, 0x00, 0x5D, 0x11, 0x28, 0x22, 0xD1, 0x00, 0x98, 0x81, 0x42, 0x1F, 0xD1, 0x0E, 0x20, 0x00, 0x57, 0xFF, 0xFF, 0xD2, 0x65, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xB8, 0x42, 0x1B, 0xD1, 0x04, 0x20, 0x02, 0x1C, 0x02, 0x98, 0xB9, 0x49, 0x01, 0x23, 0x9E, 0x46, 0xEC, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x17, 0xD0, 0x20, 0x1D, 0x02, 0x99, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0xE7, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x0D, 0xD0, 0x46, 0x20, 0x00, 0x5D, 0x11, 0x28, 0x09, 0xD0, 0xA0, 0x6D, 0x00, 0x28, 0x06, 0xD0, 0xAC, 0x34, 0x04, 0x98, 0x01, 0x30, 0x04, 0x90, 0x01, 0x3E, 0xC1, 0xD1, 0x07, 0xE0, 0x04, 0x98, 0x00, 0x06, 0x00, 0x0E , -0xAC, 0x21, 0x41, 0x43, 0xA6, 0x48, 0x40, 0x18, 0x68, 0x60, 0xA6, 0x48, 0xAD, 0x30, 0x05, 0xB0, 0xF0, 0xBD, 0x00, 0xB5, 0xC2, 0x68, 0x90, 0x69, 0x02, 0x21, 0x01, 0x23, 0x9E, 0x46, 0xA2, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xA2, 0x48, 0x61, 0x30, 0x00, 0xBD, 0x01, 0x79, 0x0B, 0x29, 0x03, 0xD0, 0x89, 0x00, 0x9F, 0x4A, 0x51, 0x5A, 0x01, 0xE0, 0x01, 0x21, 0x49, 0x02, 0x41, 0x60, 0x9D, 0x48, 0x0D, 0x30, 0xF7, 0x46, 0x01, 0x1C, 0xFF, 0xFF, 0x68, 0x66, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x9C, 0x48, 0x02, 0x68, 0x9C, 0x48, 0x01, 0x2A, 0x01, 0xD1, 0x25, 0x30, 0xF7, 0x46, 0x9B, 0x4A, 0x12, 0x68, 0x4A, 0x60, 0x19, 0x30, 0xF7, 0x46, 0x00, 0x21, 0x41, 0x60, 0x99, 0x48, 0x98, 0x49, 0x08, 0x18, 0xF7, 0x46, 0x00, 0x21, 0x41, 0x60, 0x97, 0x48, 0xE7, 0x49, 0x08, 0x18, 0xF7, 0x46, 0xFF, 0xB5, 0x46, 0x69, 0x40, 0x68, 0x01, 0x90, 0x94, 0x49, 0x0A, 0x7C, 0x9A, 0x4F, 0x01, 0x2A, 0x63, 0xD0, 0x09, 0x7C, 0x03, 0x29 , -0x60, 0xD0, 0x91, 0x4A, 0x92, 0x4B, 0x00, 0x21, 0x02, 0x91, 0x59, 0x56, 0x24, 0x23, 0x59, 0x43, 0x53, 0x18, 0x03, 0x93, 0x01, 0x28, 0x17, 0xD1, 0x8A, 0x18, 0x04, 0x23, 0x6C, 0x46, 0x8C, 0x49, 0x15, 0x79, 0x08, 0x78, 0xC0, 0x43, 0x28, 0x43, 0x20, 0x70, 0x01, 0x32, 0x01, 0x31, 0x01, 0x34, 0x01, 0x3B, 0xF5, 0xD1, 0x31, 0x1D, 0x68, 0x46, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0xA9, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x40, 0xD0, 0x35, 0x1D, 0x82, 0x48, 0x00, 0x78, 0xFF, 0x28, 0xFF, 0xFF, 0xFE, 0x66, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x11, 0xD0, 0x00, 0x28, 0x0F, 0xD0, 0x03, 0x98, 0x01, 0x1D, 0x28, 0x1C, 0x01, 0x24, 0xA6, 0x46, 0x7E, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x00, 0x28, 0x05, 0xD1, 0x7A, 0x48, 0xA6, 0x46, 0x7B, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x05, 0xE0, 0x28, 0x1C, 0x01, 0x21, 0x8E, 0x46, 0x78, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x01, 0x99, 0x01, 0x29, 0x01, 0xD1, 0x00, 0x28, 0x1E, 0xD1 , -0x01, 0x99, 0x5F, 0x31, 0x01, 0xD0, 0x00, 0x28, 0x0F, 0xD1, 0xA8, 0x20, 0x81, 0x5D, 0x00, 0x29, 0x08, 0xD0, 0x49, 0x1E, 0x81, 0x55, 0x80, 0x5D, 0x00, 0x28, 0x09, 0xD1, 0x38, 0x1C, 0xFF, 0x30, 0x08, 0x30, 0x11, 0xE0, 0x03, 0x21, 0x02, 0x91, 0x00, 0xE0, 0xA8, 0x20, 0x02, 0x99, 0x81, 0x55, 0x38, 0x1C, 0xFF, 0x30, 0x16, 0x30, 0x07, 0xE0, 0x01, 0x98, 0x01, 0x28, 0x02, 0xD1, 0x38, 0x1C, 0xA3, 0x30, 0x01, 0xE0, 0x38, 0x1C, 0x9F, 0x30, 0x00, 0x90, 0xFF, 0xBD, 0x00, 0xB5, 0x02, 0x1C, 0x10, 0x6A, 0xD1, 0x69, 0x52, 0x69, 0xC3, 0x69, 0x5A, 0x60, 0xFF, 0xFF, 0x94, 0x67, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x01, 0x22, 0x96, 0x46, 0x5E, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x5E, 0x48, 0xFF, 0x30, 0x6E, 0x30, 0x00, 0xBD, 0x10, 0xB5, 0x0A, 0x1C, 0x41, 0x69, 0x00, 0x6A, 0x93, 0x69, 0xDB, 0x69, 0x58, 0x60, 0x90, 0x69, 0x01, 0x24, 0xA6, 0x46, 0x56, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0xA6, 0x46, 0x56, 0x48, 0xFE, 0x44 , -0x00, 0x47, 0x55, 0x48, 0xFF, 0x30, 0xB6, 0x30, 0x10, 0xBD, 0x70, 0xB5, 0x05, 0x1C, 0x6E, 0x69, 0x53, 0x48, 0x02, 0x68, 0x5C, 0x21, 0x88, 0x5D, 0x04, 0x28, 0x15, 0xD1, 0x07, 0x20, 0x88, 0x55, 0x10, 0x0B, 0x11, 0xD2, 0x01, 0x24, 0xA6, 0x46, 0x4E, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x00, 0x28, 0x0A, 0xD0, 0x47, 0x21, 0x89, 0x57, 0xC1, 0x60, 0x11, 0x21, 0xC9, 0x02, 0x00, 0x22, 0x04, 0x23, 0xA6, 0x46, 0xE8, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x01, 0x20, 0x68, 0x60, 0x43, 0x48, 0xED, 0x49, 0x08, 0x18, 0x70, 0xBD, 0x70, 0xB5, 0x05, 0x1C, 0xAE, 0x69, 0x9C, 0x20, 0x80, 0x19, 0x4B, 0x21, 0x89, 0x00, 0x01, 0x24, 0xFF, 0xFF, 0x2A, 0x68, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xA6, 0x46, 0xE8, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x5C, 0x21, 0x88, 0x5D, 0x07, 0x28, 0x01, 0xD1, 0x09, 0x20, 0x00, 0xE0, 0x05, 0x20, 0xAA, 0x69, 0x88, 0x54, 0x30, 0x1C, 0xA6, 0x46, 0xE9, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x68, 0x60, 0xE8, 0x48 , -0x4D, 0x30, 0x70, 0xBD, 0xF0, 0xB5, 0x41, 0x68, 0x82, 0x68, 0x88, 0x23, 0x5E, 0x18, 0x37, 0x88, 0x0B, 0x6F, 0x00, 0x2B, 0x01, 0xD1, 0x00, 0x2F, 0x10, 0xD1, 0x06, 0x2F, 0x02, 0xDD, 0x00, 0x21, 0xC9, 0x43, 0x07, 0xE0, 0x33, 0x88, 0x9B, 0x00, 0xC9, 0x18, 0x0A, 0x67, 0x31, 0x88, 0x01, 0x31, 0x31, 0x80, 0x01, 0x21, 0x81, 0x60, 0xE1, 0x48, 0x1D, 0x30, 0xF0, 0xBD, 0x0B, 0x1C, 0x01, 0x24, 0x5D, 0x6F, 0x1D, 0x67, 0x04, 0x33, 0x01, 0x34, 0x06, 0x2C, 0xE1, 0xDA, 0xF8, 0xE7, 0x00, 0xB5, 0x00, 0x21, 0xC1, 0x60, 0xE9, 0x48, 0x01, 0x68, 0x10, 0x31, 0xE6, 0x48, 0x20, 0x22, 0x01, 0x23, 0x9E, 0x46, 0xE7, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xE5, 0x48, 0xFB, 0x30, 0x00, 0xBD, 0xFF, 0xFF, 0xC0, 0x68, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x14, 0x0D, 0x1B, 0x00, 0x54, 0x19, 0x1B, 0x00, 0x91, 0x1A, 0x01, 0x00, 0x4B, 0xAD, 0x03, 0x00, 0x61, 0x4E, 0x01, 0x00, 0x06, 0x32, 0x08, 0x00, 0x1F, 0x0B, 0x02, 0x00, 0x54, 0x3F , -0x08, 0x00, 0x45, 0xC1, 0x00, 0x00, 0x84, 0x3C, 0x08, 0x00, 0x1B, 0x02, 0x00, 0x00, 0xED, 0x17, 0x00, 0x00, 0xF3, 0xC1, 0x01, 0x00, 0x34, 0x19, 0x1B, 0x00, 0x08, 0x19, 0x1B, 0x00, 0xA6, 0x44, 0x08, 0x00, 0x1C, 0x17, 0x1B, 0x00, 0x18, 0x17, 0x1B, 0x00, 0xCB, 0x67, 0x03, 0x00, 0x0D, 0x47, 0x02, 0x00, 0x39, 0x42, 0x03, 0x00, 0xBD, 0xE7, 0x02, 0x00, 0xB1, 0x40, 0x03, 0x00, 0xB9, 0xEA, 0x01, 0x00, 0x45, 0xDA, 0x00, 0x00, 0x24, 0x41, 0x08, 0x00, 0x71, 0xC0, 0x02, 0x00, 0xF0, 0xB5, 0x88, 0xB0, 0x06, 0x91, 0x07, 0x90, 0x86, 0x69, 0xF0, 0x1C, 0xD9, 0xA1, 0x04, 0x22, 0x01, 0x24, 0xA6, 0x46, 0x14, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x32, 0xD1, 0xF2, 0x79, 0xD7, 0x48, 0x02, 0x70, 0x35, 0x7A, 0x77, 0x7A, 0x78, 0x1B, 0xFF, 0xFF, 0x56, 0x69, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x43, 0x1C, 0x93, 0x42, 0x1D, 0xD0, 0x16, 0x2A, 0x0C, 0xDB, 0x00, 0x2D, 0x0A, 0xD1, 0xD6, 0x48, 0x04, 0x70, 0x0A, 0x20 , -0x81, 0x19, 0xD0, 0x48, 0x1A, 0x1C, 0xA6, 0x46, 0xB8, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x74, 0xE0, 0xD2, 0x48, 0x04, 0x70, 0xCC, 0x48, 0x40, 0x19, 0x0A, 0x21, 0x89, 0x19, 0x7A, 0x1B, 0x52, 0x1C, 0xA6, 0x46, 0xB1, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x67, 0xE0, 0x8F, 0xC6, 0x03, 0x00, 0xC9, 0x48, 0x04, 0x70, 0xC9, 0x48, 0x04, 0x70, 0x0A, 0x20, 0x81, 0x19, 0xC2, 0x48, 0xA6, 0x46, 0xAA, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x59, 0xE0, 0xF0, 0x1C, 0x03, 0x21, 0x0A, 0x1C, 0xBC, 0xA1, 0xA6, 0x46, 0xC6, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x4F, 0xD1, 0xF1, 0x79, 0xBB, 0x48, 0x01, 0x70, 0xBB, 0x4D, 0xB2, 0x79, 0x2A, 0x70, 0x03, 0x20, 0x17, 0x49, 0xF4, 0x31, 0xA6, 0x46, 0xB8, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x28, 0x78, 0x00, 0x28, 0x0B, 0xD1, 0xFF, 0xE7, 0xB8, 0x49, 0x0C, 0x70, 0xB8, 0x49, 0xFF, 0xFF, 0xEC, 0x69, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x0C, 0x70, 0x03, 0x20, 0x0F, 0x49, 0xF5, 0x31, 0xA6, 0x46 , -0xB6, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x20, 0x1C, 0xF2, 0x79, 0x35, 0x7A, 0x77, 0x7A, 0x79, 0x1B, 0x4B, 0x1C, 0x93, 0x42, 0x20, 0xD0, 0xA9, 0x49, 0x09, 0x78, 0x16, 0x29, 0x0F, 0xDB, 0x00, 0x2D, 0x0D, 0xD1, 0xAB, 0x49, 0x08, 0x70, 0x0A, 0x20, 0x81, 0x19, 0xE9, 0x48, 0x1A, 0x1C, 0xA6, 0x46, 0x8B, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x1A, 0xE0, 0xC0, 0x46, 0xC7, 0x04, 0x00, 0x00, 0xA5, 0x49, 0x08, 0x70, 0xE3, 0x48, 0x40, 0x19, 0x0A, 0x21, 0x89, 0x19, 0x7A, 0x1B, 0x52, 0x1C, 0xA6, 0x46, 0x83, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x0A, 0xE0, 0x9E, 0x49, 0x08, 0x70, 0x9E, 0x49, 0x08, 0x70, 0x0A, 0x20, 0x81, 0x19, 0xDB, 0x48, 0xA6, 0x46, 0x7D, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x96, 0x48, 0x00, 0x78, 0x00, 0x28, 0x0A, 0xD0, 0x95, 0x48, 0x00, 0x78, 0x00, 0x28, 0x06, 0xD0, 0x94, 0x48, 0x00, 0x78, 0x00, 0x28, 0x02, 0xD0, 0x93, 0x48, 0x00, 0x78, 0x00, 0x28, 0xFF, 0xFF, 0x82, 0x6A, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00 , -0x00, 0xD1, 0x8C, 0xE0, 0x03, 0x20, 0x17, 0x21, 0x89, 0x01, 0xA6, 0x46, 0x90, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x87, 0x48, 0x01, 0x78, 0x87, 0x48, 0x07, 0x78, 0x84, 0x4E, 0x83, 0x4D, 0xE0, 0x48, 0x00, 0x68, 0x00, 0x28, 0x2E, 0xD1, 0x07, 0x98, 0x41, 0x61, 0x06, 0x62, 0xC6, 0x48, 0x06, 0x9A, 0x10, 0x64, 0x02, 0x2F, 0x01, 0xD0, 0x03, 0x2F, 0x01, 0xD1, 0x03, 0x22, 0x00, 0xE0, 0x3A, 0x1C, 0x06, 0x9B, 0xDA, 0x63, 0x2A, 0x78, 0x9A, 0x63, 0x01, 0x2F, 0x03, 0xD1, 0x05, 0x29, 0x04, 0xD0, 0x0D, 0x29, 0x02, 0xD0, 0xEC, 0x48, 0xDB, 0x30, 0x64, 0xE0, 0x00, 0x25, 0x00, 0x95, 0x01, 0x91, 0x02, 0x96, 0x03, 0x95, 0x04, 0x90, 0x2B, 0x1C, 0x20, 0x1C, 0x01, 0x1C, 0x8E, 0x46, 0xE6, 0x4E, 0xFE, 0x44, 0x30, 0x47, 0xE6, 0x48, 0x04, 0x60, 0x28, 0x1C, 0xA6, 0x46, 0xE5, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x4C, 0xE0, 0x0D, 0x27, 0x00, 0x20, 0x39, 0x1C, 0x05, 0xAA, 0xA6, 0x46, 0xE3, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x68, 0x46 , -0xFF, 0xFF, 0x18, 0x6B, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x80, 0x8A, 0x40, 0x08, 0x05, 0xD2, 0x38, 0x1C, 0x50, 0x21, 0xA6, 0x46, 0xDC, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x38, 0x1C, 0x21, 0x1C, 0x0B, 0x1C, 0x29, 0x1C, 0x00, 0x22, 0xA6, 0x46, 0xD9, 0x4F, 0xFE, 0x44, 0x38, 0x47, 0x2B, 0x78, 0x0D, 0x20, 0x31, 0x1C, 0x22, 0x1C, 0xA6, 0x46, 0xD5, 0x4E, 0xFE, 0x44, 0x30, 0x47, 0x28, 0x78, 0x01, 0x30, 0x02, 0x1C, 0x0D, 0x20, 0x57, 0x49, 0x23, 0x1C, 0xA6, 0x46, 0xD0, 0x4E, 0xFE, 0x44, 0x30, 0x47, 0x55, 0x4E, 0x28, 0x78, 0x02, 0x30, 0x02, 0x1C, 0x0D, 0x20, 0x31, 0x1C, 0x23, 0x1C, 0xA6, 0x46, 0xCA, 0x4F, 0xFE, 0x44, 0x38, 0x47, 0x30, 0x78, 0x00, 0x28, 0x0A, 0xD0, 0x28, 0x78, 0x03, 0x30, 0x02, 0x1C, 0x4C, 0x48, 0x03, 0x78, 0x0D, 0x20, 0x8F, 0x49, 0xA6, 0x46, 0xC3, 0x4D, 0xFE, 0x44, 0x28, 0x47, 0xBF, 0x48, 0x04, 0x60, 0x00, 0x20, 0xA6, 0x46, 0xBE, 0x49, 0xFE, 0x44, 0x08, 0x47, 0xB9, 0x48, 0xEC, 0x49 , -0x08, 0x18, 0x08, 0xB0, 0xF0, 0xBD, 0xC0, 0x46, 0x7B, 0xC0, 0xFF, 0xFF, 0xAE, 0x6B, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x1C, 0xE9, 0x48, 0x02, 0x68, 0xE9, 0x48, 0x00, 0x2A, 0x04, 0xD0, 0x00, 0x22, 0x0A, 0x62, 0x8A, 0x61, 0x1D, 0x30, 0xF7, 0x46, 0xD7, 0x30, 0xF7, 0x46, 0xC0, 0x46, 0x9F, 0x03, 0x00, 0x00, 0x7D, 0xB8, 0x03, 0x00, 0x01, 0x1C, 0xE2, 0x48, 0x02, 0x68, 0xE2, 0x48, 0x00, 0x2A, 0x02, 0xD0, 0xFF, 0x30, 0xB4, 0x30, 0xF7, 0x46, 0x00, 0x22, 0xCA, 0x61, 0x8A, 0x61, 0x4F, 0x30, 0xF7, 0x46, 0xC9, 0x21, 0x01, 0x00, 0x91, 0xE1, 0x00, 0x00, 0xDB, 0x48, 0x01, 0x68, 0x8A, 0x69, 0x01, 0x20, 0xC0, 0x03, 0x10, 0x43, 0x88, 0x61, 0x00, 0x20, 0x08, 0x61, 0xD8, 0x48, 0x23, 0x30, 0xF7, 0x46, 0x89, 0x17, 0x02, 0x00, 0x70, 0xB5, 0x05, 0x1C, 0xD5, 0x4E, 0x29, 0x6A, 0xEA, 0x69, 0x30, 0x1C, 0x01, 0x24, 0xA6, 0x46, 0x0B, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x68, 0x69, 0xA6, 0x46, 0xD1, 0x49 , -0xFE, 0x44, 0x08, 0x47, 0xA8, 0x69, 0x06, 0x30, 0xA8, 0x60, 0xCF, 0x48, 0x00, 0x68, 0x68, 0x60, 0xEE, 0x60, 0xCE, 0x48, 0xFF, 0xFF, 0x44, 0x6C, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x6D, 0x30, 0x70, 0xBD, 0x46, 0x17, 0x1B, 0x00, 0x30, 0x3F, 0x08, 0x00, 0x41, 0xEB, 0x00, 0x00, 0x09, 0xB4, 0x03, 0x00, 0x30, 0xB5, 0xEE, 0x48, 0x01, 0x6D, 0x80, 0x6C, 0x08, 0x43, 0x18, 0xD0, 0xEC, 0x49, 0x08, 0x1C, 0x07, 0x22, 0xFF, 0x23, 0x09, 0x33, 0x1B, 0x58, 0x00, 0x2B, 0x04, 0xD0, 0xFF, 0x30, 0x1D, 0x30, 0x01, 0x3A, 0xF6, 0xD1, 0x0B, 0xE0, 0x02, 0x20, 0xF0, 0x4A, 0xF1, 0x4B, 0x01, 0x24, 0xA6, 0x46, 0xF0, 0x4D, 0xFE, 0x44, 0x28, 0x47, 0xA6, 0x46, 0xEF, 0x48, 0xFE, 0x44, 0x00, 0x47, 0xEF, 0x48, 0xFF, 0x30, 0x5C, 0x30, 0x30, 0xBD, 0xC0, 0x46, 0x53, 0x53, 0x49, 0x44, 0x00, 0xC0, 0x46, 0xC0, 0x4B, 0x45, 0x59, 0x00, 0xEE, 0x62, 0x08, 0x00, 0xF0, 0x62, 0x08, 0x00, 0xEC, 0x62, 0x08, 0x00, 0xED, 0x62, 0x08, 0x00 , -0xE7, 0x7E, 0x03, 0x00, 0xE8, 0x62, 0x08, 0x00, 0xE9, 0x62, 0x08, 0x00, 0xEA, 0x62, 0x08, 0x00, 0xEB, 0x62, 0x08, 0x00, 0xDD, 0x7E, 0x03, 0x00, 0x8F, 0xC6, 0x03, 0x00, 0xF0, 0xB5, 0xFF, 0xFF, 0xDA, 0x6C, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x87, 0xB0, 0x00, 0x91, 0x01, 0x90, 0x6B, 0x48, 0x01, 0x68, 0xCC, 0x4A, 0x14, 0x1C, 0x02, 0x94, 0x07, 0x26, 0x00, 0x25, 0x03, 0x95, 0x04, 0x94, 0x05, 0x95, 0xDF, 0x48, 0x00, 0x68, 0x08, 0x43, 0x27, 0xD0, 0xE0, 0x68, 0x03, 0x99, 0x88, 0x42, 0x0A, 0xDD, 0x03, 0x90, 0x40, 0x1C, 0x00, 0x99, 0x88, 0x60, 0xC2, 0x49, 0xFF, 0x20, 0x1D, 0x30, 0x68, 0x43, 0x0C, 0x18, 0xE0, 0x68, 0x04, 0x91, 0x02, 0x99, 0xC9, 0x68, 0x88, 0x42, 0x10, 0xDB, 0x88, 0x42, 0x10, 0xD1, 0x02, 0x98, 0x01, 0x27, 0xBE, 0x46, 0xCE, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x06, 0x90, 0x20, 0x1C, 0xBE, 0x46, 0xCB, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x06, 0x99, 0x88, 0x42, 0x01, 0xDA, 0x05, 0x95, 0x02, 0x94 , -0x51, 0x48, 0x01, 0x68, 0xFF, 0x27, 0x09, 0x37, 0x38, 0x59, 0x00, 0x28, 0x06, 0xD0, 0xC7, 0x48, 0x00, 0x68, 0x07, 0x2E, 0x00, 0xD1, 0x2E, 0x1C, 0x08, 0x43, 0x04, 0xD0, 0xFF, 0x34, 0x1D, 0x34, 0x01, 0x35, 0x07, 0x2D, 0xC2, 0xDB, 0x07, 0x2E, 0xFF, 0xFF, 0x70, 0x6D, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x0A, 0xD1, 0xC0, 0x48, 0x00, 0x68, 0x08, 0x43, 0x06, 0xD0, 0x05, 0x9E, 0x30, 0x1C, 0x01, 0x21, 0x8E, 0x46, 0xBA, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x01, 0x98, 0x47, 0x61, 0x00, 0x21, 0x81, 0x61, 0xFF, 0x22, 0x1D, 0x32, 0x72, 0x43, 0x04, 0x99, 0x89, 0x18, 0xC1, 0x61, 0x06, 0x62, 0xB4, 0x48, 0x39, 0x30, 0x07, 0xB0, 0xF0, 0xBD, 0x10, 0xB5, 0x04, 0x1C, 0x00, 0x20, 0x01, 0x21, 0x8E, 0x46, 0xB1, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x40, 0x1E, 0x00, 0xD5, 0x00, 0x20, 0x60, 0x60, 0xE0, 0x61, 0xAE, 0x48, 0x21, 0x30, 0x10, 0xBD, 0xC0, 0x46, 0x10, 0x63, 0x08, 0x00, 0x70, 0xB5, 0x04, 0x1C, 0xE5, 0x69, 0x66, 0x69 , -0x00, 0x20, 0x01, 0x21, 0x8E, 0x46, 0xA7, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x60, 0x60, 0xAB, 0x19, 0xA6, 0x4A, 0xA5, 0x49, 0x93, 0x42, 0x02, 0xD3, 0x9A, 0x1A, 0x82, 0x42, 0x02, 0xD9, 0x08, 0x1C, 0x99, 0x30, 0x70, 0xBD, 0x08, 0x1C, 0xAB, 0x30, 0x70, 0xBD, 0x08, 0x6B, 0x41, 0x7D, 0x02, 0x88, 0x92, 0x00, 0xFF, 0xFF, 0x06, 0x6E, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x51, 0x1A, 0x3C, 0x39, 0xFF, 0x22, 0x5F, 0x32, 0x91, 0x42, 0x03, 0xD9, 0x65, 0x21, 0x01, 0x80, 0x02, 0x21, 0x81, 0x73, 0x9B, 0x48, 0x99, 0x49, 0x08, 0x18, 0xF7, 0x46, 0xC0, 0x46, 0x30, 0x63, 0x08, 0x00, 0xF8, 0xB5, 0x40, 0x68, 0x24, 0x21, 0x41, 0x43, 0x97, 0x48, 0x45, 0x18, 0x28, 0x1D, 0x0C, 0x21, 0x49, 0x19, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0x94, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x91, 0x4C, 0x00, 0x28, 0x1B, 0xD0, 0x2B, 0x1C, 0x04, 0x22, 0x6E, 0x46, 0x91, 0x49, 0x1F, 0x7B, 0x08, 0x78, 0xC0, 0x43, 0x38, 0x43, 0x30, 0x70, 0x01, 0x33 , -0x01, 0x31, 0x01, 0x36, 0x01, 0x3A, 0xF5, 0xD1, 0x0C, 0x20, 0x41, 0x19, 0x68, 0x46, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0x87, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x02, 0xD0, 0x20, 0x1C, 0x4A, 0x30, 0xF8, 0xBD, 0x20, 0x1C, 0xF8, 0xBD, 0xB1, 0xBD, 0x00, 0x00, 0x15, 0x95, 0x00, 0x00, 0x58, 0x3F, 0x08, 0x00, 0xF3, 0xF8, 0x00, 0x00, 0xE9, 0x09, 0x02, 0x00, 0xFF, 0xFF, 0x9C, 0x6E, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x2B, 0x09, 0x02, 0x00, 0xDD, 0x0A, 0x02, 0x00, 0xF8, 0xB5, 0x05, 0x1C, 0xAA, 0x69, 0x7D, 0x49, 0x00, 0x20, 0x08, 0x56, 0x24, 0x21, 0x41, 0x43, 0x76, 0x48, 0x43, 0x18, 0x04, 0x24, 0x6E, 0x46, 0x76, 0x49, 0x1F, 0x79, 0x08, 0x78, 0xC0, 0x43, 0x38, 0x43, 0x30, 0x70, 0x01, 0x33, 0x01, 0x31, 0x01, 0x36, 0x01, 0x3C, 0xF5, 0xD1, 0x11, 0x1D, 0x68, 0x46, 0x04, 0x22, 0x01, 0x24, 0xA6, 0x46, 0x6D, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x01, 0x1C, 0x6D, 0x48, 0x04, 0xD0, 0x6E, 0x49, 0x09, 0x78 , -0x69, 0x60, 0x45, 0x30, 0xF8, 0xBD, 0xEC, 0x61, 0xAF, 0x30, 0xF8, 0xBD, 0x70, 0xB5, 0xC5, 0x68, 0x81, 0x68, 0x0A, 0x89, 0x6D, 0x4E, 0x00, 0x2A, 0x22, 0xD0, 0x01, 0x24, 0xC2, 0x6D, 0xA2, 0x18, 0xC2, 0x65, 0x08, 0x89, 0x0D, 0x28, 0x1B, 0xD1, 0x68, 0x6B, 0x21, 0x1C, 0xA6, 0x46, 0x63, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x22, 0x1C, 0x28, 0x1C, 0x00, 0x21, 0xA6, 0x46, 0x60, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x2C, 0x60, 0xE9, 0x6B, 0xFF, 0xFF, 0x32, 0x6F, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x28, 0x1C, 0xA6, 0x46, 0x5E, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x28, 0x1C, 0xA6, 0x46, 0x5C, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x30, 0x1C, 0x3D, 0x30, 0x70, 0xBD, 0x30, 0x1C, 0x23, 0x30, 0x70, 0xBD, 0xC0, 0x46, 0xB1, 0x02, 0x00, 0x00, 0x74, 0x3F, 0x08, 0x00, 0x19, 0xC6, 0x00, 0x00, 0x5C, 0x3F, 0x08, 0x00, 0x1D, 0xC2, 0x00, 0x00, 0x68, 0x44, 0x08, 0x00, 0x43, 0x12, 0x03, 0x00, 0x34, 0x63, 0x08, 0x00, 0x2F, 0x7A, 0x02, 0x00 , -0xB4, 0x36, 0x08, 0x00, 0x8F, 0xF4, 0x01, 0x00, 0xF0, 0xB5, 0x05, 0x1C, 0xAB, 0x69, 0xEC, 0x69, 0x60, 0x68, 0x00, 0x68, 0x2A, 0x30, 0x01, 0x78, 0x0A, 0x06, 0x41, 0x78, 0x09, 0x04, 0x8A, 0x18, 0x81, 0x78, 0x09, 0x02, 0x8A, 0x18, 0xC1, 0x78, 0x8A, 0x18, 0x8E, 0x26, 0xF1, 0x5A, 0x09, 0x0A, 0x01, 0x70, 0xF0, 0x5C, 0x61, 0x68, 0x09, 0x68, 0x2B, 0x26, 0x70, 0x54, 0x8C, 0x20, 0xC0, 0x18, 0x61, 0x68, 0x0E, 0x68, 0x01, 0x88, 0x09, 0x0A, 0x2C, 0x27, 0xB9, 0x55, 0x61, 0x68, 0x09, 0x68, 0xFF, 0xFF, 0xC8, 0x6F, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x2D, 0x26, 0x00, 0x78, 0x70, 0x54, 0x60, 0x68, 0x01, 0x68, 0x32, 0x31, 0x08, 0x78, 0x00, 0x02, 0x49, 0x78, 0x08, 0x18, 0x00, 0x04, 0x00, 0x0C, 0x8C, 0x21, 0xCB, 0x58, 0x35, 0x49, 0x41, 0x40, 0x08, 0x04, 0x00, 0x0C, 0xC0, 0x18, 0x80, 0x1A, 0x01, 0x21, 0x8E, 0x46, 0x32, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x61, 0x68, 0x0A, 0x68, 0x32, 0x23, 0x01, 0x0A, 0x99, 0x54 , -0x61, 0x68, 0x09, 0x68, 0x33, 0x22, 0x50, 0x54, 0x60, 0x68, 0x68, 0x60, 0x2C, 0x48, 0xF0, 0xBD, 0x00, 0x00, 0x1B, 0x00, 0x34, 0x04, 0x1B, 0x00, 0x02, 0x6A, 0x8B, 0x69, 0xDB, 0x69, 0x1B, 0x68, 0x83, 0x60, 0x88, 0x69, 0xC0, 0x69, 0x41, 0x68, 0x8A, 0x42, 0x00, 0xDA, 0x42, 0x60, 0x25, 0x48, 0x79, 0x30, 0xF7, 0x46, 0x24, 0x48, 0x57, 0x30, 0xF7, 0x46, 0x24, 0x48, 0x91, 0x30, 0xF7, 0x46, 0x32, 0x04, 0x00, 0x00, 0xC4, 0x07, 0x00, 0x00, 0x15, 0x09, 0x02, 0x00, 0xC5, 0x93, 0x00, 0x00, 0x0D, 0x91, 0x00, 0x00, 0x40, 0x1E, 0x80, 0x00, 0x1E, 0x4B, 0xFF, 0xFF, 0x5E, 0x70, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x19, 0x50, 0x1C, 0x49, 0x0A, 0x50, 0xF7, 0x46, 0xC0, 0x46, 0xBD, 0xB5, 0x00, 0x00, 0x95, 0x92, 0x00, 0x00, 0xFD, 0x93, 0x00, 0x00, 0x54, 0x3F, 0x08, 0x00, 0x29, 0x4F, 0x03, 0x00, 0xDF, 0xE8, 0x02, 0x00, 0x36, 0x89, 0x41, 0x00, 0x81, 0x06, 0x00, 0x00, 0xB1, 0x78, 0x00, 0x00, 0x94, 0xEC, 0x01, 0x00 , -0x08, 0x19, 0x1B, 0x00, 0x8F, 0xC6, 0x03, 0x00, 0x1C, 0x17, 0x1B, 0x00, 0x67, 0x66, 0x03, 0x00, 0xA6, 0x44, 0x08, 0x00, 0x18, 0x17, 0x1B, 0x00, 0xA7, 0x2F, 0x02, 0x00, 0x91, 0x44, 0x03, 0x00, 0x91, 0x63, 0x03, 0x00, 0x5B, 0x44, 0x03, 0x00, 0xE7, 0x44, 0x03, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x61, 0xA2, 0x03, 0x00, 0x6A, 0x1E, 0x01, 0x00, 0x45, 0xDA, 0x00, 0x00, 0xD7, 0xE8, 0x01, 0x00, 0xE9, 0x78, 0x02, 0x00, 0x04, 0xF3, 0x1A, 0x00, 0x80, 0x7B, 0x08, 0x00, 0xFC, 0xB5, 0x04, 0x1C, 0xA5, 0x69, 0x60, 0x6A, 0x01, 0x90, 0x20, 0x69, 0x00, 0x28, 0x35, 0xD4, 0x08, 0x28, 0x33, 0xDA, 0xE1, 0x69, 0x09, 0x68, 0xFF, 0xFF, 0xF4, 0x70, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x10, 0x29, 0x2F, 0xD1, 0xAC, 0x21, 0x41, 0x43, 0x9E, 0x4A, 0x51, 0x5C, 0x49, 0x08, 0x27, 0xD3, 0x00, 0x06, 0x00, 0x16, 0xA2, 0x68, 0x69, 0x46, 0x01, 0x23, 0x9E, 0x46, 0x9A, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x06, 0x1C, 0x39, 0x30, 0x1F, 0xD0 , -0x00, 0x2E, 0x1D, 0xD0, 0x00, 0x98, 0x07, 0x68, 0x00, 0x99, 0x0C, 0x39, 0x01, 0x98, 0x01, 0x60, 0x28, 0x1D, 0x10, 0x21, 0x79, 0x1A, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0x91, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x02, 0x22, 0x2A, 0x80, 0xA8, 0x1C, 0x08, 0x21, 0x79, 0x1A, 0x01, 0x23, 0x9E, 0x46, 0x8C, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x03, 0xE0, 0x38, 0x20, 0x00, 0xE0, 0x00, 0x20, 0xC6, 0x43, 0x26, 0x60, 0x89, 0x48, 0x7C, 0x30, 0x20, 0x62, 0xFC, 0xBD, 0x30, 0xB5, 0x05, 0x1C, 0x28, 0x69, 0xA9, 0x69, 0x90, 0x29, 0x1D, 0xD1, 0x69, 0x69, 0x09, 0x78, 0x04, 0x29, 0x19, 0xD0, 0x05, 0x29, 0x17, 0xD0, 0x0A, 0x29, 0x15, 0xD0, 0x06, 0x29, 0x13, 0xD0, 0x0C, 0x29, 0x01, 0xDB, 0xFF, 0xFF, 0x8A, 0x71, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x10, 0x29, 0x0F, 0xD1, 0x01, 0x24, 0xA6, 0x46, 0x7D, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x7C, 0x48, 0x7D, 0x49, 0x00, 0x22, 0xA6, 0x46, 0x7C, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x7C, 0x48 , -0xFF, 0x30, 0x5F, 0x30, 0x28, 0x62, 0x30, 0xBD, 0xF0, 0xB5, 0x89, 0xB0, 0x05, 0x90, 0x41, 0x69, 0x02, 0x69, 0x75, 0x48, 0x79, 0x4E, 0x82, 0x42, 0x3A, 0xD0, 0x04, 0x30, 0x82, 0x42, 0x25, 0xD0, 0x74, 0x4D, 0xAA, 0x42, 0x00, 0xD0, 0xCC, 0xE0, 0x08, 0x68, 0x06, 0x1C, 0x0C, 0x22, 0x8F, 0x1A, 0x4A, 0x68, 0x89, 0x18, 0x09, 0x1D, 0x10, 0x22, 0x01, 0x24, 0xA6, 0x46, 0x6E, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x01, 0x1C, 0x09, 0xD1, 0xAC, 0x22, 0x72, 0x43, 0xCB, 0x48, 0x80, 0x18, 0x46, 0x22, 0x12, 0x5C, 0x11, 0x2A, 0x01, 0xD1, 0x44, 0x22, 0x14, 0x54, 0x28, 0x1C, 0x3A, 0x1C, 0xA6, 0x46, 0x62, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x69, 0x48, 0xEC, 0x30, 0xAB, 0xE0, 0x08, 0x68, 0xC0, 0x00, 0x30, 0x18, 0x8A, 0x68, 0x02, 0x2A, 0x02, 0xD0, 0xFF, 0xFF, 0x20, 0x72, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xE7, 0x20, 0x80, 0x00, 0xA2, 0xE0, 0xCA, 0x68, 0x89, 0x18, 0x09, 0x7B, 0x41, 0x60, 0x80, 0x21, 0x01, 0x60, 0x3B, 0x20 , -0x00, 0x01, 0x99, 0xE0, 0x08, 0x68, 0x06, 0x90, 0xC0, 0x00, 0x34, 0x58, 0x10, 0x20, 0x04, 0x90, 0x01, 0x25, 0xAE, 0x46, 0x57, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x07, 0x90, 0x00, 0x28, 0x00, 0xD1, 0x89, 0xE0, 0x0C, 0x21, 0x07, 0x98, 0x0F, 0x18, 0xB0, 0x49, 0xAC, 0x20, 0x06, 0x9A, 0x50, 0x43, 0x08, 0x18, 0x48, 0x22, 0x13, 0x5C, 0x5B, 0x06, 0x5B, 0x0E, 0x13, 0x54, 0x06, 0x9A, 0xD2, 0x00, 0xB6, 0x18, 0x71, 0x68, 0x00, 0x29, 0x47, 0xD1, 0x00, 0x2C, 0x01, 0xDD, 0x08, 0x2C, 0x21, 0xDB, 0x00, 0x22, 0x69, 0x46, 0x0A, 0x70, 0x01, 0x92, 0x0A, 0x81, 0x8A, 0x72, 0xCD, 0x72, 0x0D, 0x73, 0x4D, 0x73, 0x01, 0x88, 0x44, 0x48, 0x06, 0x23, 0xAE, 0x46, 0x44, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x04, 0x1C, 0x02, 0xDC, 0x00, 0x20, 0xC0, 0x43, 0x36, 0xE0, 0x34, 0x60, 0xAC, 0x20, 0x60, 0x43, 0x9A, 0x49, 0xFF, 0xFF, 0xB6, 0x72, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x09, 0x18, 0x5C, 0x22, 0x28, 0x1C, 0x50, 0x54, 0x48, 0x20 , -0x42, 0x5C, 0x52, 0x06, 0x52, 0x0E, 0x42, 0x54, 0xAC, 0x20, 0x60, 0x43, 0x94, 0x49, 0x08, 0x18, 0x5C, 0x21, 0x09, 0x5C, 0x49, 0x1E, 0x02, 0x29, 0x16, 0xD9, 0x03, 0x39, 0x08, 0xD0, 0x00, 0x20, 0x30, 0x56, 0xAE, 0x46, 0xEA, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x80, 0x20, 0x30, 0x60, 0xDB, 0xE7, 0x08, 0x94, 0x80, 0x21, 0x31, 0x60, 0x48, 0x22, 0x13, 0x5C, 0x19, 0x43, 0x11, 0x54, 0xAE, 0x46, 0xE4, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x0A, 0xE0, 0x01, 0x20, 0xCE, 0xE7, 0x06, 0x98, 0x39, 0x1C, 0x04, 0xAA, 0xAE, 0x46, 0xE0, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x04, 0x1C, 0x08, 0x90, 0x06, 0x98, 0x38, 0x60, 0x08, 0x98, 0x78, 0x60, 0x00, 0x28, 0x15, 0xD0, 0xAC, 0x20, 0x60, 0x43, 0x7C, 0x49, 0x09, 0x18, 0x48, 0x88, 0x02, 0x04, 0x02, 0x20, 0x10, 0x43, 0xB8, 0x60, 0x48, 0x68, 0x02, 0x0E, 0x01, 0x02, 0x09, 0x0E, 0x09, 0x02, 0x11, 0x43, 0x02, 0x04, 0x12, 0x0E, 0xFF, 0xFF, 0x4C, 0x73, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00 , -0x12, 0x04, 0x0A, 0x43, 0x00, 0x06, 0x10, 0x43, 0xF8, 0x60, 0x07, 0x98, 0x0D, 0x49, 0x00, 0x22, 0x10, 0x23, 0xAE, 0x46, 0xCE, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x13, 0x48, 0x00, 0xE0, 0x16, 0x20, 0xCC, 0x49, 0x09, 0x18, 0x05, 0x98, 0x01, 0x62, 0x09, 0xB0, 0xF0, 0xBD, 0x5D, 0x0D, 0x1B, 0x00, 0x81, 0xE7, 0x02, 0x00, 0x09, 0xB4, 0x03, 0x00, 0x27, 0x1A, 0x02, 0x00, 0x2F, 0x7A, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x05, 0x10, 0x00, 0x00, 0xCF, 0xF6, 0x00, 0x00, 0xD5, 0xF4, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x91, 0x19, 0x02, 0x00, 0x50, 0x65, 0x08, 0x00, 0x71, 0xC0, 0x02, 0x00, 0x50, 0x19, 0x1B, 0x00, 0x29, 0x16, 0x01, 0x00, 0x2A, 0x03, 0x00, 0x00, 0xF0, 0xB5, 0x8B, 0xB0, 0x07, 0x90, 0x05, 0x69, 0x47, 0x69, 0x0C, 0x20, 0x38, 0x1A, 0x08, 0x90, 0xB6, 0x49, 0x8D, 0x42, 0x16, 0xD1, 0x78, 0x68, 0x0F, 0x30, 0x00, 0x11, 0x00, 0x01, 0x78, 0x60, 0x38, 0x68, 0x0B, 0x28, 0x0E, 0xD8, 0x00, 0x23, 0x68, 0x46 , -0xFF, 0xFF, 0xE2, 0x73, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x03, 0x70, 0x08, 0x98, 0x05, 0x22, 0x01, 0x24, 0xA6, 0x46, 0xAE, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0xAE, 0x48, 0xFF, 0x30, 0x95, 0x30, 0x07, 0x99, 0x08, 0x62, 0xAC, 0x48, 0x85, 0x42, 0x23, 0xD1, 0xAC, 0x48, 0x02, 0x78, 0x6C, 0x46, 0x22, 0x72, 0x43, 0x78, 0x63, 0x72, 0x81, 0x78, 0xA1, 0x72, 0xC0, 0x78, 0xE0, 0x72, 0x00, 0x91, 0x01, 0x90, 0x03, 0x20, 0xA6, 0x49, 0x01, 0x39, 0x01, 0x26, 0xB6, 0x46, 0xF3, 0x4E, 0xFE, 0x44, 0x30, 0x47, 0x04, 0x20, 0x20, 0x70, 0x08, 0x98, 0x9F, 0x49, 0x00, 0x22, 0x02, 0xAB, 0x01, 0x24, 0xA6, 0x46, 0x9B, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x9A, 0x48, 0xFF, 0x30, 0x95, 0x30, 0x07, 0x99, 0x08, 0x62, 0xEB, 0x49, 0x8D, 0x42, 0x14, 0xD1, 0x02, 0x22, 0x68, 0x46, 0x02, 0x70, 0x3A, 0x68, 0xAC, 0x20, 0x42, 0x43, 0x30, 0x48, 0x68, 0x30, 0x83, 0x18, 0x08, 0x98, 0x00, 0x22, 0x01, 0x24, 0xA6, 0x46, 0x8F, 0x4C, 0xFE, 0x44 , -0x20, 0x47, 0x8E, 0x48, 0xFF, 0x30, 0x95, 0x30, 0x07, 0x99, 0xFF, 0xFF, 0x78, 0x74, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x08, 0x62, 0xEF, 0x48, 0x85, 0x42, 0x00, 0xD0, 0x04, 0xE1, 0x03, 0x20, 0x8C, 0x49, 0x01, 0x22, 0x96, 0x46, 0xEC, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x86, 0x48, 0xFF, 0x30, 0x95, 0x30, 0x09, 0x90, 0x38, 0x68, 0xEA, 0x4D, 0x00, 0x28, 0x00, 0xD1, 0xE2, 0xE0, 0x01, 0x24, 0xA6, 0x46, 0xE6, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x00, 0x28, 0x00, 0xD1, 0xD6, 0xE0, 0xE4, 0x48, 0x02, 0x68, 0x03, 0x20, 0x7F, 0x49, 0x01, 0x31, 0xA6, 0x46, 0xE2, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xDF, 0x48, 0x00, 0x68, 0x00, 0x28, 0x5A, 0xD1, 0xDF, 0x4D, 0x2E, 0x20, 0x41, 0x19, 0xB8, 0x68, 0x03, 0x30, 0x08, 0x70, 0x38, 0x7A, 0x48, 0x70, 0x2E, 0x1C, 0x30, 0x36, 0xB8, 0x68, 0x28, 0x18, 0x30, 0x30, 0xDA, 0x49, 0x0A, 0x68, 0x30, 0x3A, 0x31, 0x1C, 0xA6, 0x46, 0xD8, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x78, 0x68, 0x38, 0x18 , -0x01, 0x1D, 0xBA, 0x68, 0x30, 0x1C, 0xA6, 0x46, 0xD9, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xB8, 0x68, 0x41, 0x19, 0x97, 0x22, 0xFF, 0xFF, 0x0E, 0x75, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x09, 0x30, 0x50, 0x54, 0xB8, 0x68, 0x46, 0x19, 0x9E, 0x36, 0x30, 0x70, 0x01, 0x36, 0xB8, 0x68, 0x01, 0xE0, 0x14, 0x0D, 0x1B, 0x00, 0x30, 0x18, 0xCA, 0x49, 0x0A, 0x68, 0x9E, 0x3A, 0x31, 0x1C, 0xA6, 0x46, 0xC8, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x78, 0x68, 0x38, 0x18, 0x01, 0x1D, 0xBA, 0x68, 0x30, 0x1C, 0xA6, 0x46, 0xC9, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xB8, 0x68, 0x40, 0x00, 0xC0, 0x49, 0x09, 0x68, 0x08, 0x18, 0x28, 0x18, 0xC0, 0x49, 0x0A, 0x79, 0x02, 0x70, 0x4A, 0x79, 0x42, 0x70, 0x8A, 0x79, 0x82, 0x70, 0xC9, 0x79, 0xC1, 0x70, 0x04, 0x30, 0x0F, 0x21, 0x41, 0x1A, 0x0A, 0x78, 0xBB, 0x68, 0x9A, 0x18, 0x0A, 0x70, 0x40, 0x1B, 0x00, 0x04, 0x00, 0x0C, 0xB5, 0x49, 0x08, 0x60, 0xB1, 0x48, 0x04, 0x60, 0x02, 0x20, 0x01, 0x1C , -0x11, 0x22, 0xA6, 0x46, 0xB4, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x06, 0x1C, 0x63, 0xD4, 0x03, 0x20, 0xA6, 0x46, 0xB1, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x0A, 0x90, 0x00, 0x25, 0x01, 0x1C, 0xFF, 0xFF, 0xA4, 0x75, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x8D, 0x42, 0x50, 0xD0, 0x29, 0x1C, 0x2B, 0x22, 0xA6, 0x46, 0xAD, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x0C, 0x20, 0x0A, 0x99, 0x40, 0x18, 0xAC, 0x22, 0x72, 0x43, 0xAB, 0x49, 0x8F, 0x18, 0x78, 0x63, 0x29, 0x1C, 0x18, 0x22, 0xA6, 0x46, 0xA6, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x9F, 0x48, 0x02, 0x68, 0x79, 0x6B, 0x4A, 0x60, 0x0A, 0x98, 0xAC, 0x30, 0x08, 0x60, 0x08, 0x68, 0x9A, 0x49, 0xA6, 0x46, 0xA0, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x29, 0x1C, 0x03, 0xA8, 0x10, 0x22, 0xA6, 0x46, 0x9C, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xE0, 0x21, 0x68, 0x46, 0x01, 0x74, 0x45, 0x74, 0x85, 0x74, 0xFB, 0x21, 0xC1, 0x74, 0xEE, 0x49, 0xC1, 0x81, 0x02, 0x21, 0x81, 0x81, 0x79, 0x6B, 0xCA, 0x68 , -0x01, 0x20, 0xC0, 0x03, 0x10, 0x43, 0xC8, 0x60, 0x48, 0x68, 0x00, 0x90, 0x2A, 0x1C, 0x30, 0x1C, 0x0A, 0x9D, 0x29, 0x1C, 0x03, 0xAB, 0xA6, 0x46, 0xE6, 0x4F, 0xFE, 0x44, 0x38, 0x47, 0x07, 0x1C, 0x30, 0x06, 0x00, 0x16, 0xA6, 0x46, 0xEF, 0x49, 0xFF, 0xFF, 0x3A, 0x76, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xFE, 0x44, 0x08, 0x47, 0x28, 0x1C, 0xA6, 0x46, 0xEE, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x11, 0xE0, 0x30, 0x06, 0x00, 0x16, 0xA6, 0x46, 0xE9, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x01, 0x20, 0xC7, 0x43, 0x08, 0xE0, 0x07, 0x1C, 0x06, 0xE0, 0x00, 0x20, 0x00, 0x21, 0xCF, 0x43, 0x01, 0xE0, 0x00, 0x20, 0x07, 0x1C, 0x28, 0x60, 0x00, 0x23, 0x68, 0x46, 0x03, 0x70, 0x3A, 0x06, 0x12, 0x0E, 0x08, 0x98, 0x6F, 0x49, 0x01, 0x24, 0xA6, 0x46, 0x09, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x07, 0x98, 0x09, 0x99, 0x01, 0x62, 0x0B, 0xB0, 0xF0, 0xBD, 0xC1, 0x18, 0x01, 0x00, 0xF1, 0x1B, 0x01, 0x00, 0xF3, 0x1C, 0x02, 0x00, 0x7B, 0xC0 , -0x02, 0x00, 0x91, 0xF0, 0x00, 0x00, 0x03, 0x02, 0x00, 0x00, 0xA3, 0xC0, 0x02, 0x00, 0xA9, 0xE9, 0x00, 0x00, 0x07, 0x02, 0x00, 0x00, 0xE4, 0x62, 0x08, 0x00, 0xAC, 0x05, 0x00, 0x00, 0xFE, 0xB5, 0x07, 0x1C, 0x7D, 0x69, 0xBE, 0x69, 0x0C, 0x20, 0x30, 0x1A, 0x01, 0x90, 0xDF, 0x4C, 0x0A, 0x2D, 0x13, 0xD1, 0xFF, 0xFF, 0xD0, 0x76, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x30, 0x68, 0xDB, 0x49, 0x08, 0x60, 0x01, 0x20, 0x86, 0x46, 0xDA, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x0A, 0x20, 0x00, 0x21, 0x01, 0x9A, 0x01, 0x23, 0x9E, 0x46, 0xE6, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xF5, 0x20, 0x80, 0x00, 0x00, 0x19, 0x38, 0x62, 0x03, 0x2D, 0x79, 0xD1, 0xF5, 0x20, 0x80, 0x00, 0x00, 0x19, 0x02, 0x90, 0x70, 0x68, 0x00, 0x28, 0x0C, 0xD1, 0x00, 0x21, 0xCF, 0x48, 0x01, 0x60, 0xCF, 0x48, 0x00, 0x68, 0x00, 0x28, 0x61, 0xD0, 0x01, 0x21, 0x8E, 0x46, 0xCD, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x5B, 0xE0, 0xCC, 0x48, 0xB1, 0x68, 0x01, 0x60 , -0xF1, 0x68, 0x41, 0x60, 0x31, 0x69, 0x81, 0x60, 0x71, 0x69, 0xC1, 0x60, 0xB1, 0x69, 0xC1, 0x63, 0xF1, 0x69, 0x01, 0x64, 0x31, 0x6A, 0xC1, 0x64, 0x01, 0x24, 0xA6, 0x46, 0xC5, 0x48, 0xFE, 0x44, 0x00, 0x47, 0xA6, 0x46, 0xC4, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x70, 0x68, 0x7D, 0x25, 0xED, 0x00, 0xA8, 0x42, 0x21, 0xD8, 0x01, 0x28, 0x18, 0xD0, 0x02, 0x28, 0x05, 0xD0, 0xFF, 0xFF, 0x66, 0x77, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x28, 0x1C, 0xA6, 0x46, 0xBE, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x1C, 0xE0, 0xB9, 0x48, 0x00, 0x23, 0x19, 0x1C, 0x59, 0x43, 0x3E, 0x22, 0x4A, 0x43, 0x82, 0x83, 0x02, 0x30, 0x01, 0x33, 0x10, 0x2B, 0xF6, 0xDB, 0xA6, 0x46, 0xB4, 0x48, 0xFE, 0x44, 0x00, 0x47, 0xB5, 0x48, 0x0D, 0xE0, 0xB5, 0x4D, 0x28, 0x1C, 0xA6, 0x46, 0xB2, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x04, 0xE0, 0xA6, 0x46, 0xAF, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x75, 0x68, 0xAE, 0x48, 0x05, 0x60, 0x05, 0x1C, 0xA7, 0x4E, 0x30, 0x68 , -0x00, 0x28, 0x06, 0xD1, 0xAD, 0x48, 0x00, 0x68, 0xA6, 0x46, 0xAC, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x30, 0x60, 0x00, 0x94, 0x2B, 0x68, 0xAA, 0x49, 0xAB, 0x4A, 0xA6, 0x46, 0xAB, 0x4D, 0xFE, 0x44, 0x28, 0x47, 0x9C, 0x48, 0x04, 0x60, 0x03, 0x20, 0x00, 0x21, 0x01, 0x9A, 0x01, 0x23, 0x9E, 0x46, 0xA7, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x02, 0x98, 0x38, 0x62, 0xFE, 0xBD, 0xC0, 0x46, 0x13, 0x7F, 0x03, 0x00, 0x12, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0xFC, 0x77, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x30, 0xB5, 0x05, 0x1C, 0x00, 0x20, 0x01, 0x24, 0xA6, 0x46, 0xA0, 0x49, 0xFE, 0x44, 0x08, 0x47, 0xA6, 0x46, 0x9F, 0x49, 0xFE, 0x44, 0x08, 0x47, 0xA6, 0x46, 0x9E, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x7D, 0x21, 0xC9, 0x00, 0xA6, 0x46, 0x9E, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0x9B, 0x49, 0x08, 0x18, 0x99, 0x49, 0x08, 0x80, 0x9B, 0x48, 0x0A, 0x30, 0x28, 0x62, 0x30, 0xBD, 0x11, 0x10, 0x00, 0x00, 0xDD, 0x7E, 0x03, 0x00, 0xB9, 0x90 , -0x00, 0x00, 0x6C, 0x64, 0x08, 0x00, 0xE7, 0x7E, 0x03, 0x00, 0x74, 0x64, 0x08, 0x00, 0x70, 0x64, 0x08, 0x00, 0x6D, 0xC6, 0x03, 0x00, 0x08, 0x19, 0x1B, 0x00, 0x11, 0x18, 0x02, 0x00, 0xD1, 0x78, 0x02, 0x00, 0xC9, 0xBA, 0x03, 0x00, 0x09, 0xB4, 0x03, 0x00, 0x14, 0x0D, 0x1B, 0x00, 0xFE, 0xB5, 0x02, 0x90, 0x45, 0x69, 0x68, 0x46, 0x01, 0x21, 0x8E, 0x46, 0x89, 0x49, 0xFE, 0x44, 0x08, 0x47, 0x89, 0x48, 0x04, 0x1C, 0x68, 0x68, 0x01, 0x69, 0x4A, 0x78, 0x00, 0x27, 0x3E, 0x1C, 0x20, 0x88, 0xFF, 0xFF, 0x92, 0x78, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x40, 0x08, 0x22, 0xD3, 0x20, 0x1D, 0x89, 0x1C, 0x01, 0x23, 0x9E, 0x46, 0x83, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x00, 0x28, 0x07, 0xD1, 0x68, 0x68, 0x00, 0x69, 0x40, 0x78, 0x21, 0x88, 0x09, 0x04, 0x89, 0x16, 0x81, 0x42, 0x3D, 0xD0, 0x02, 0x20, 0x00, 0x5F, 0x00, 0x99, 0x08, 0x1A, 0x00, 0x04, 0x00, 0x14, 0x3C, 0x28, 0x03, 0xDD, 0x20, 0x88, 0x40, 0x10, 0x40, 0x00 , -0x20, 0x80, 0x78, 0x1C, 0x07, 0x06, 0x3F, 0x0E, 0x68, 0x68, 0x01, 0x69, 0x4A, 0x78, 0x2A, 0x34, 0x01, 0x36, 0x14, 0x2E, 0xD5, 0xDB, 0x00, 0x2A, 0x02, 0xD1, 0xAB, 0x20, 0x40, 0x00, 0x50, 0xE0, 0x00, 0x9B, 0x6D, 0x48, 0x14, 0x21, 0x00, 0x22, 0x04, 0x88, 0x64, 0x08, 0x03, 0xD3, 0x2A, 0x30, 0x01, 0x32, 0x01, 0x39, 0xF8, 0xD1, 0x12, 0x06, 0x12, 0x0E, 0x14, 0x2F, 0x16, 0xD1, 0x66, 0x48, 0x00, 0x88, 0x00, 0x06, 0x44, 0x16, 0x64, 0x49, 0x00, 0x22, 0x01, 0x20, 0x4E, 0x8D, 0x36, 0x06, 0x76, 0x16, 0xA6, 0x42, 0x02, 0xDD, 0x34, 0x1C, 0x02, 0x06, 0xFF, 0xFF, 0x28, 0x79, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0x12, 0x0E, 0x2A, 0x31, 0x01, 0x30, 0x14, 0x28, 0xF3, 0xDB, 0x02, 0xE0, 0x32, 0x06, 0x12, 0x0E, 0x00, 0x9B, 0x2A, 0x20, 0x42, 0x43, 0x5A, 0x48, 0x11, 0x18, 0x0C, 0x88, 0x01, 0x20, 0x20, 0x43, 0x00, 0x04, 0x00, 0x0C, 0x08, 0x80, 0xFE, 0x24, 0xA0, 0x43, 0x14, 0x24, 0x64, 0x57, 0x64, 0x06, 0x24, 0x0E , -0x04, 0x43, 0x52, 0x48, 0x14, 0x52, 0x14, 0x18, 0x63, 0x80, 0x08, 0x88, 0x80, 0x05, 0x82, 0x0D, 0x68, 0x68, 0x00, 0x69, 0x40, 0x78, 0x80, 0x02, 0x10, 0x43, 0x08, 0x80, 0x68, 0x68, 0x02, 0x69, 0x20, 0x1D, 0x91, 0x1C, 0x52, 0x78, 0x01, 0x23, 0x9E, 0x46, 0x4B, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x02, 0x98, 0x04, 0x61, 0xFE, 0x20, 0x47, 0x49, 0x09, 0x18, 0x02, 0x98, 0x01, 0x62, 0xFE, 0xBD, 0x82, 0x69, 0x41, 0x69, 0x03, 0x69, 0x02, 0x2B, 0x0A, 0xD1, 0x01, 0x2A, 0x01, 0xD1, 0x00, 0x29, 0x05, 0xD0, 0x02, 0x2A, 0x04, 0xD1, 0x00, 0x29, 0x02, 0xD1, 0x11, 0x21, 0x00, 0xE0, 0x06, 0x21, 0x41, 0x61, 0xF7, 0x46, 0xFF, 0xFF, 0xBE, 0x79, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xC0, 0x46, 0x14, 0xE9, 0x00, 0x00, 0xC9, 0x18, 0x02, 0x00, 0x30, 0xB5, 0x05, 0x1C, 0x3A, 0x48, 0x00, 0x78, 0x00, 0x28, 0x04, 0xD0, 0x01, 0x20, 0x86, 0x46, 0x38, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x01, 0x24, 0xA6, 0x46, 0x37, 0x48, 0xFE, 0x44 , -0x00, 0x47, 0xA6, 0x46, 0x36, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x35, 0x48, 0x0E, 0x30, 0x28, 0x62, 0x30, 0xBD, 0x49, 0x19, 0x01, 0x00, 0x2F, 0x7A, 0x02, 0x00, 0x18, 0xB5, 0x43, 0x69, 0x81, 0x69, 0x31, 0x48, 0x81, 0x29, 0x00, 0xD1, 0x0B, 0x38, 0x00, 0x21, 0x00, 0x91, 0x1A, 0x68, 0x9B, 0x68, 0x01, 0x24, 0xA6, 0x46, 0x2D, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x18, 0xBD, 0x18, 0xB5, 0x40, 0x69, 0x00, 0x21, 0x00, 0x91, 0x02, 0x68, 0x83, 0x68, 0x27, 0x48, 0x01, 0x30, 0x01, 0x24, 0xA6, 0x46, 0x26, 0x4C, 0xFE, 0x44, 0x20, 0x47, 0x18, 0xBD, 0xC0, 0x46, 0x30, 0x63, 0x08, 0x00, 0x19, 0x9E, 0x00, 0x00, 0xB9, 0xEC, 0x00, 0x00, 0x74, 0x3F, 0x08, 0x00, 0x80, 0x3F, 0x08, 0x00, 0xFF, 0xFF, 0x54, 0x7A, 0x08, 0x00, 0x96, 0x00, 0x00, 0x00, 0xB7, 0x5D, 0x03, 0x00, 0x00, 0x00, 0x1B, 0x00, 0xD3, 0x8E, 0x00, 0x00, 0xF7, 0xC6, 0x00, 0x00, 0xE5, 0x90, 0x00, 0x00, 0x68, 0x64, 0x08, 0x00, 0xC0, 0x27, 0x09, 0x00, 0xA0, 0x3B , -0x08, 0x00, 0x3D, 0x5D, 0x03, 0x00, 0x19, 0xC6, 0x00, 0x00, 0xE8, 0x3A, 0x08, 0x00, 0x81, 0x5D, 0x03, 0x00, 0xCF, 0xF6, 0x00, 0x00, 0x4B, 0x4F, 0x03, 0x00, 0x6B, 0xC0, 0x03, 0x00, 0x95, 0x1E, 0x01, 0x00, 0xDA, 0x40, 0x08, 0x00, 0xDC, 0x05, 0x00, 0x00, 0x55, 0xAA, 0x03, 0x00, 0x0D, 0x91, 0x00, 0x00, 0xD7, 0x56, 0x03, 0x00, 0x08, 0x51, 0x08, 0x00, 0x8F, 0xC6, 0x03, 0x00, 0x65, 0xB8, 0x00, 0x00, 0x09, 0xB4, 0x03, 0x00, 0x84, 0x17, 0x1B, 0x00, 0xB5, 0x86, 0x01, 0x00, 0x3D, 0x47, 0x02, 0x00, 0x73, 0x49, 0x02, 0x00, 0x5F, 0x90, 0x00, 0x00, 0x0E, 0x10, 0x00, 0x00, 0xF7, 0xF6, 0x00, 0x00, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x76, 0xFF, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x5A, 0xFF, 0xFF, 0xBD, 0xFF, 0xB5, 0xFF, 0xFF, 0xEA, 0x7A, 0x08, 0x00, 0x58, 0x00, 0x00, 0x00, 0x68, 0x46, 0xFF, 0xF7, 0x99, 0xFF, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x83, 0xFF, 0xFF, 0xBD, 0xFF, 0xB5 , -0x68, 0x46, 0xFF, 0xF7, 0xB6, 0xFE, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x55, 0xFC, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x27, 0xFB, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0xDD, 0xFA, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x68, 0xFE, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0x3D, 0xFB, 0xFF, 0xBD, 0xFF, 0xB5, 0x68, 0x46, 0xFF, 0xF7, 0xBE, 0xFD, 0xFF, 0xBD, 0x78, 0x7B, 0x08, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0xB5, 0xF8, 0xF0, 0x43, 0xFA, 0x00, 0xBD, 0x35, 0x66, 0x08, 0x00, 0x67, 0x66, 0x08, 0x00, 0x71, 0x65, 0x08, 0x00, 0x4D, 0x66, 0x08, 0x00, 0x25, 0x64, 0x08, 0x00, 0x81, 0x66, 0x08, 0x00, 0x8D, 0x66, 0x08, 0x00, 0x99, 0x66, 0x08, 0x00, 0x87, 0x67, 0x08, 0x00, 0xA7, 0x67, 0x08, 0x00, 0xD1, 0x67, 0x08, 0x00, 0x1B, 0x68, 0x08, 0x00, 0x57, 0x68, 0x08, 0x00, 0x2D, 0x69, 0x08, 0x00, 0xB1, 0x6B, 0x08, 0x00, 0xD5, 0x6B, 0x08, 0x00, 0xF9, 0x6B , -0x08, 0x00, 0x15, 0x6C, 0x08, 0x00, 0xA1, 0x68, 0x08, 0x00, 0x59, 0x6C, 0x08, 0x00, 0xA7, 0x6D, 0x08, 0x00, 0xFF, 0x6D, 0x08, 0x00, 0x29, 0x6E, 0x08, 0x00, 0xF9, 0x6E, 0x08, 0x00, 0xD9, 0x6C, 0x08, 0x00, 0xA5, 0x6E, 0x08, 0x00, 0xCD, 0x6D, 0x08, 0x00, 0x81, 0x6F, 0x08, 0x00, 0x1D, 0x70, 0x08, 0x00, 0x31, 0x64, 0x08, 0x00, 0x39, 0x70, 0x08, 0x00, 0x3F, 0x70, 0x08, 0x00, 0x04, 0xF3, 0x1A, 0x00, 0x80, 0x00, 0x00, 0x00, 0xB2, 0x4E, 0x01, 0x00, 0x5A, 0xC1, 0x00, 0x00, 0x96, 0x1A, 0x01, 0x00, 0x22, 0x0B, 0x02, 0x00, 0x60, 0xD0, 0x01, 0x00, 0x06, 0x1A, 0x00, 0x00, 0xB8, 0xC6, 0x01, 0x00, 0xD8, 0x42, 0x03, 0x00, 0x16, 0x42, 0x03, 0x00, 0xEE, 0xDB, 0x00, 0x00, 0x62, 0xDC, 0x00, 0x00, 0xC8, 0xE1, 0x00, 0x00, 0x8A, 0x17, 0x02, 0x00, 0x38, 0xBE, 0x00, 0x00, 0x26, 0xC6, 0x00, 0x00, 0x56, 0xC2, 0x00, 0x00, 0x62, 0x12, 0x03, 0x00, 0xE4, 0xF4, 0x01, 0x00, 0x3A, 0xEC, 0x00, 0x00, 0x5E, 0x92, 0x00, 0x00 , -0xFE, 0xE8, 0x02, 0x00, 0x18, 0x7F, 0x00, 0x00, 0x64, 0xEC, 0x01, 0x00, 0xFE, 0x44, 0x03, 0x00, 0x1C, 0x94, 0x00, 0x00, 0xA8, 0x66, 0x03, 0x00, 0x74, 0xE9, 0x02, 0x00, 0x68, 0x1E, 0x01, 0x00, 0xBC, 0xDA, 0x00, 0x00, 0xEA, 0xE1, 0x00, 0x00, 0x24, 0xE9, 0x01, 0x00, 0x70, 0x79, 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x1C, 0xF0, 0xB5, 0x88, 0xB0, 0xCA, 0x4C, 0x03, 0x20, 0x22, 0x78, 0x63, 0x78, 0xCA, 0x4E, 0x31, 0x1C, 0x01, 0x39, 0x01, 0x25, 0xAE, 0x46, 0xC7, 0x4F, 0xFE, 0x44, 0x38, 0x47, 0xA2, 0x78, 0xE3, 0x78, 0x03, 0x20, 0x31, 0x1C, 0xAE, 0x46, 0xC3, 0x4F, 0xFE, 0x44, 0x38, 0x47, 0x03, 0x20, 0x31, 0x1C, 0x01, 0x31, 0xAE, 0x46, 0xC1, 0x4A, 0xFE, 0x44, 0x10, 0x47, 0xC1, 0x48, 0x21, 0x78, 0x01, 0x70, 0x61, 0x78, 0x41, 0x70, 0xE1, 0x78, 0xC1, 0x70, 0xA1, 0x78, 0x81, 0x70, 0xBF, 0x48, 0x04, 0x1C, 0x33, 0x3C, 0x20, 0x1C, 0x00, 0x21, 0xDC, 0x22, 0xAE, 0x46 , -0xBA, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x84, 0x20, 0xA0, 0x70, 0x05, 0x27, 0xE7, 0x71, 0x0C, 0x26, 0x26, 0x73, 0x0D, 0x20, 0x00, 0x19, 0x98, 0xA1, 0x32, 0x1C, 0xAE, 0x46, 0xB4, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x04, 0x20, 0x60, 0x76, 0x1A, 0x20, 0x00, 0x19, 0x97, 0xA1, 0x06, 0x91, 0x04, 0x22, 0xAE, 0x46, 0xAE, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xFF, 0xFF, 0x96, 0x00, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0xA7, 0x77, 0x1F, 0x34, 0x95, 0xA1, 0x07, 0x91, 0x20, 0x1C, 0x3A, 0x1C, 0xAE, 0x46, 0xA9, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xE6, 0x71, 0x28, 0x1C, 0x60, 0x72, 0x11, 0x20, 0x20, 0x73, 0x94, 0x20, 0x60, 0x73, 0xC0, 0x25, 0x65, 0x74, 0xA6, 0x74, 0x09, 0x22, 0xE2, 0x74, 0xA3, 0x4C, 0x20, 0x1C, 0x8C, 0xA1, 0x01, 0x23, 0x9E, 0x46, 0x9F, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x07, 0x22, 0x62, 0x72, 0x9E, 0x4C, 0x0A, 0x34, 0x20, 0x1C, 0x89, 0xA1, 0x01, 0x23, 0x9E, 0x46, 0x9A, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x04, 0x20 , -0xE0, 0x71, 0x98, 0x4C, 0x12, 0x34, 0x20, 0x1C, 0x06, 0x99, 0x04, 0x22, 0x01, 0x23, 0x9E, 0x46, 0x94, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x27, 0x71, 0x93, 0x4C, 0x17, 0x34, 0x20, 0x1C, 0x07, 0x99, 0x3A, 0x1C, 0x01, 0x23, 0x9E, 0x46, 0x8E, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0xE6, 0x71, 0x01, 0x20, 0x60, 0x72, 0x11, 0x20, 0x20, 0x73, 0x94, 0x20, 0x60, 0x73, 0x02, 0x20, 0xE0, 0x73, 0x25, 0x74, 0x66, 0x74, 0xFF, 0xFF, 0x2C, 0x01, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0xA5, 0x74, 0x2F, 0x27, 0xE7, 0x74, 0x10, 0x20, 0x60, 0x75, 0x80, 0x26, 0xA6, 0x75, 0x01, 0x20, 0xE0, 0x75, 0x11, 0x20, 0xA0, 0x76, 0x94, 0x20, 0xE0, 0x76, 0x24, 0x20, 0x60, 0x77, 0x0A, 0x22, 0xA2, 0x77, 0x80, 0x4C, 0x36, 0x34, 0x20, 0x1C, 0x6D, 0xA1, 0x01, 0x23, 0x9E, 0x46, 0x7C, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x18, 0x22, 0xA2, 0x72, 0x7A, 0x4C, 0x41, 0x34, 0x20, 0x1C, 0x6B, 0xA1, 0x01, 0x23, 0x9E, 0x46, 0x76, 0x4B, 0xFE, 0x44, 0x18, 0x47 , -0x25, 0x76, 0x67, 0x76, 0x21, 0x20, 0xE0, 0x76, 0x26, 0x77, 0x01, 0x20, 0x60, 0x77, 0x72, 0x48, 0x61, 0x30, 0x11, 0x21, 0x01, 0x70, 0x94, 0x21, 0x41, 0x70, 0x04, 0x21, 0x01, 0x72, 0xD2, 0x21, 0x41, 0x72, 0xC5, 0x72, 0x1E, 0x21, 0x01, 0x73, 0x45, 0x73, 0x9E, 0x21, 0x81, 0x73, 0x01, 0x21, 0x01, 0x74, 0x46, 0x74, 0x81, 0x74, 0x11, 0x21, 0x41, 0x75, 0x94, 0x21, 0x81, 0x75, 0x04, 0x21, 0x01, 0x76, 0xAD, 0x20, 0x66, 0x49, 0x08, 0x60, 0x00, 0x24, 0x0F, 0x25, 0xFF, 0xFF, 0xC2, 0x01, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0x29, 0x06, 0x09, 0x0E, 0x00, 0x20, 0x6A, 0x46, 0x01, 0x23, 0x9E, 0x46, 0x62, 0x4B, 0xFE, 0x44, 0x18, 0x47, 0x6B, 0x46, 0x18, 0x88, 0x40, 0x08, 0x0E, 0xD3, 0xA0, 0x00, 0x0F, 0x21, 0xCA, 0x43, 0x69, 0x46, 0x0E, 0x88, 0x16, 0x40, 0x01, 0xAF, 0x3E, 0x52, 0x49, 0x88, 0x11, 0x40, 0x68, 0x44, 0xC1, 0x80, 0x60, 0x1C, 0x04, 0x04, 0x24, 0x0C, 0x01, 0x3D, 0x0B, 0x2D, 0xE0, 0xDA, 0x60, 0x1E , -0x00, 0x04, 0x00, 0x0C, 0x1D, 0xD0, 0x86, 0x46, 0x01, 0x1C, 0x01, 0xAD, 0x01, 0x22, 0xAE, 0x88, 0x90, 0x00, 0xC7, 0x5A, 0xB7, 0x42, 0x0A, 0xDA, 0xBC, 0x46, 0x07, 0x1C, 0x6F, 0x44, 0x7F, 0x88, 0x68, 0x44, 0x06, 0x80, 0xEE, 0x88, 0x46, 0x80, 0x60, 0x46, 0xA8, 0x80, 0xEF, 0x80, 0x04, 0x35, 0x50, 0x1C, 0x02, 0x04, 0x12, 0x0C, 0x01, 0x39, 0xE9, 0xD1, 0x70, 0x46, 0x01, 0x38, 0x86, 0x46, 0xE2, 0xD1, 0x99, 0x88, 0x68, 0x46, 0xC0, 0x88, 0x40, 0x18, 0x00, 0x04, 0x00, 0x0C, 0x42, 0x4B, 0x18, 0x80, 0x42, 0x49, 0x08, 0x1A, 0xFF, 0xFF, 0x58, 0x02, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0x58, 0x80, 0x02, 0x2C, 0x1E, 0xDB, 0x65, 0x1E, 0x01, 0xAA, 0x01, 0x20, 0x01, 0x1C, 0x8C, 0x00, 0x6B, 0x46, 0xE6, 0x5A, 0x93, 0x88, 0xD4, 0x88, 0xE7, 0x18, 0xB7, 0x42, 0x0C, 0xDA, 0x06, 0x24, 0x44, 0x43, 0x38, 0x4B, 0x1B, 0x19, 0x1F, 0x80, 0x1C, 0x88, 0x34, 0x1B, 0x5C, 0x80, 0x01, 0x24, 0x1C, 0x71, 0x40, 0x1C, 0x00, 0x04 , -0x00, 0x0C, 0x04, 0x32, 0x49, 0x1C, 0x09, 0x06, 0x09, 0x0E, 0x01, 0x3D, 0xE4, 0xD1, 0x00, 0x24, 0x31, 0x48, 0x04, 0x70, 0x31, 0x48, 0x04, 0x70, 0x31, 0x48, 0x04, 0x70, 0x31, 0x48, 0x04, 0x70, 0x01, 0x20, 0x86, 0x46, 0x30, 0x48, 0xFE, 0x44, 0x00, 0x47, 0x2F, 0x49, 0x22, 0x1C, 0x08, 0x68, 0x00, 0x28, 0x02, 0xD0, 0x01, 0x20, 0x90, 0x40, 0x04, 0x43, 0x04, 0x31, 0x01, 0x32, 0x20, 0x2A, 0xF5, 0xD3, 0x2A, 0x48, 0x04, 0x60, 0x08, 0xB0, 0xF0, 0xBD, 0x5F, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2D, 0x69, 0x6E, 0x66, 0x6F, 0x00, 0xC0, 0x46, 0xC0, 0x5F, 0x75, 0x64, 0x70, 0x00, 0xC0, 0xFF, 0xFF, 0xEE, 0x02, 0x18, 0x00, 0x96, 0x00, 0x00, 0x00, 0x46, 0xC0, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x00, 0xC0, 0x46, 0x5F, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x00, 0xC0, 0x46, 0x5F, 0x64, 0x6E, 0x73, 0x2D, 0x73, 0x64, 0x00, 0x64, 0x65, 0x76, 0x3D, 0x43, 0x43, 0x33, 0x30, 0x30, 0x30, 0x00, 0xC0, 0x76, 0x65 , -0x6E, 0x64, 0x6F, 0x72, 0x3D, 0x54, 0x65, 0x78, 0x61, 0x73, 0x2D, 0x49, 0x6E, 0x73, 0x74, 0x72, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73, 0x00, 0xC0, 0x46, 0xC0, 0x00, 0x00, 0x18, 0x00, 0xF3, 0x7E, 0x03, 0x00, 0xC6, 0x05, 0x00, 0x00, 0xDD, 0x7E, 0x03, 0x00, 0xE4, 0x62, 0x08, 0x00, 0xC9, 0xBA, 0x03, 0x00, 0x09, 0xB4, 0x03, 0x00, 0xA7, 0x64, 0x08, 0x00, 0x70, 0x64, 0x08, 0x00, 0xDD, 0x0A, 0x02, 0x00, 0x48, 0x32, 0x08, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xE8, 0x62, 0x08, 0x00, 0xE9, 0x62, 0x08, 0x00, 0xEA, 0x62, 0x08, 0x00, 0xEB, 0x62, 0x08, 0x00, 0x81, 0x03, 0x18, 0x00, 0x80, 0x7B, 0x08, 0x00, 0x84, 0xF3, 0x1A, 0x00, 0x0D, 0x49, 0x0E, 0x48, 0xFF, 0xFF, 0x84, 0x03, 0x18, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x88, 0x67, 0x0E, 0x48, 0x88, 0x64, 0x0E, 0x48, 0x48, 0x64, 0x0E, 0x48, 0xC8, 0x64, 0x0E, 0x48, 0x08, 0x65, 0x11, 0x48, 0x02, 0x1C, 0x04, 0x32, 0x0C, 0x4B, 0x13, 0x60, 0x0C, 0x4B, 0x93, 0x62, 0x0C, 0x4A , -0x8A, 0x66, 0x0D, 0x49, 0x01, 0x60, 0x0D, 0x48, 0x0E, 0x49, 0x01, 0x60, 0x0E, 0x49, 0x41, 0x60, 0xF7, 0x46, 0x1C, 0x21, 0x08, 0x00, 0x1B, 0x7B, 0x08, 0x00, 0x11, 0x7B, 0x08, 0x00, 0x07, 0x7B, 0x08, 0x00, 0x39, 0x7B, 0x08, 0x00, 0x2F, 0x7B, 0x08, 0x00, 0x25, 0x7B, 0x08, 0x00, 0xFD, 0x7A, 0x08, 0x00, 0xDF, 0x7A, 0x08, 0x00, 0x50, 0x23, 0x08, 0x00, 0xD5, 0x7A, 0x08, 0x00, 0x58, 0x26, 0x08, 0x00, 0xF3, 0x7A, 0x08, 0x00, 0xE9, 0x7A, 0x08, 0x00 }; - - -const unsigned char cRMdefaultParams[128] = { 0x03, 0x00, 0x01, 0x01, 0x14, 0x14, 0x00, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x23, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x23, 0x23, 0x23, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x01, 0x77, 0x80, 0x1D, 0x1F, 0x22, 0x26, 0x28, 0x29, 0x1A, 0x1F, 0x22, 0x24, 0x26, 0x28, 0x16, 0x1D, 0x1E, 0x20, 0x24, 0x25, 0x1E, 0x2D, 0x01, 0x02, 0x02, 0x02, 0x02, 0x00, 0x15, 0x15, 0x15, 0x11, 0x15, 0x15, 0x0E, 0x00}; - -// -//Service Pack version P1.13.7.15.15 - FW patches -// -const unsigned char fw_patch[5700] = { 0x00, 0x01, 0x00, 0x00, 0x3C, 0x16, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x14, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x68, 0x46, 0x25, 0xF0, 0x95, 0xFB, 0xE0, 0x6B, 0xD0, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x25, 0xF0, 0x38, 0xFB, 0x2C, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x25, 0xF0, 0x0A, 0xFB, 0x04, 0x15, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xB8, 0xF1, 0x90, 0x0F, 0xA4, 0x16, 0x00, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x66, 0xE0, 0x04, 0xA8, 0x35, 0x1D, 0x21, 0xF0, 0x99, 0xFC, 0x68, 0x46, 0x23, 0xF0, 0x28, 0xFB, 0x9A, 0xF8, 0x00, 0x00, 0x01, 0x28, 0x07, 0xD1, 0x28, 0x46, 0x05, 0xF0, 0xC3, 0xFE, 0x01, 0x46, 0x01, 0xAA, 0x00, 0x20, 0x21, 0xF0, 0x50, 0xF9, 0x28, 0x46, 0x05, 0xF0, 0xBB, 0xFE, 0x01, 0xA9, 0x21, 0xF0, 0x2A, 0xFA, 0xE9, 0x79, 0x4F, 0xEA, 0xE0, 0x00, 0x40, 0xB2, 0x11, 0xB1, 0x00, 0xF1, 0x06, 0x00, 0x40, 0xB2, 0xA8, 0x71, 0x1F, 0x38, 0x40, 0x00, 0xE8, 0x71, 0x30, 0x46 , -0x01, 0xF0, 0x0D, 0xFF, 0x10, 0xF1, 0x00, 0x09, 0x4F, 0xF0, 0x00, 0x01, 0x09, 0xD0, 0x28, 0x68, 0x40, 0x0C, 0x09, 0xD3, 0xE8, 0x68, 0xC0, 0x0B, 0x03, 0xD2, 0x48, 0x46, 0xFF, 0xF7, 0xDD, 0xFE, 0x01, 0x21, 0x28, 0x68, 0x40, 0x0C, 0x0A, 0xD2, 0x38, 0x68, 0x40, 0x1C, 0x38, 0x60, 0x20, 0x68, 0x6F, 0xF3, 0x0F, 0x00, 0x20, 0x60, 0x22, 0x68, 0x38, 0x68, 0x10, 0x43, 0x20, 0x60, 0xE8, 0x68, 0xC0, 0x0B, 0x0F, 0xD3, 0xD8, 0xF8, 0x00, 0x00, 0x00, 0xF1, 0x01, 0x00, 0xC8, 0xF8, 0x00, 0x00, 0x20, 0x68, 0x6F, 0xF3, 0x1F, 0x40, 0x20, 0x60, 0xD8, 0xF8, 0x00, 0x20, 0x20, 0x68, 0x40, 0xEA, 0x02, 0x40, 0x20, 0x60, 0x49, 0xB9, 0xB9, 0xF1, 0x00, 0x0F, 0x03, 0xD1, 0x30, 0x46, 0x07, 0xF0, 0xBE, 0xF9, 0x02, 0xE0, 0x48, 0x46, 0x07, 0xF0, 0x06, 0xFA, 0x6C, 0x17, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x68, 0x46, 0x0E, 0xF0, 0x85, 0xFB, 0x00, 0x9E, 0x00, 0x2E, 0x96, 0xD1, 0x05, 0xB0, 0xBD, 0xE8, 0xF0, 0x87, 0xC0, 0x46 , -0x9C, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x23, 0xF0, 0x2A, 0xFE, 0x74, 0x47, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0B, 0x21, 0x21, 0xF0, 0x41, 0xFA, 0x20, 0x68, 0x18, 0x4B, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x21, 0xF0, 0x1C, 0xF9, 0x0A, 0xE0, 0x20, 0x68, 0x00, 0x68, 0x0C, 0x21, 0x40, 0xF0, 0x20, 0x00, 0x21, 0xF0, 0x14, 0xF9, 0x10, 0x57, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0xA8, 0x20, 0xF0, 0x97, 0xF9, 0x01, 0x98, 0x5C, 0x57, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x02, 0xA8, 0x20, 0xF0, 0x71, 0xF9, 0x03, 0x98, 0x00, 0x58, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0xF0, 0x20, 0xF9, 0x1C, 0x58, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0xF0, 0x12, 0xF9, 0x54, 0x58, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0xA8, 0x20, 0xF0, 0xF5, 0xF8, 0x04, 0xAB, 0x04, 0x62, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x79, 0x1F, 0xF0, 0x1B, 0xFD, 0x69, 0xE0, 0x60, 0x93, 0x00, 0x00, 0x40, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0xA6, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x78, 0x1B, 0xF0, 0x39, 0xFB, 0x28, 0x46, 0x90, 0xA8, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x78, 0x1B, 0xF0, 0x69, 0xF9, 0x1E, 0x48, 0x34, 0xAD, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x68, 0xFF, 0x29, 0x98, 0xAD, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10, 0xD4, 0x21, 0xD4, 0xB2, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0xD0, 0x1A, 0xF0, 0x47, 0xFC, 0x20, 0x98, 0xAC, 0xC5, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x78, 0x19, 0xF0 , -0xDB, 0xFA, 0x09, 0x49, 0x28, 0xC6, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x19, 0xF0, 0x9E, 0xFA, 0xDC, 0xD3, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xB5, 0x18, 0xF0, 0xAB, 0xFC, 0x00, 0xF0, 0x03, 0xF8, 0x00, 0xBD, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xDF, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x17, 0xF0, 0xA8, 0xFD, 0xCC, 0xEB, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x38, 0x78, 0x17, 0xF0, 0x77, 0xF8, 0x38, 0x78, 0x16, 0xF0, 0xF0, 0xFF, 0xA8, 0xF7, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0E, 0x49, 0x09, 0x68, 0x23, 0x22, 0x41, 0x61, 0xC8, 0xF7, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x31, 0x2E, 0x31, 0x39, 0x2E, 0x33, 0x31, 0x34, 0x5F, 0x4E, 0x65, 0x77, 0x5F, 0x43, 0x43, 0x41, 0x5F, 0x61, 0x6C, 0x67, 0x6F, 0x72, 0x69, 0x74, 0x68, 0x6D, 0x00, 0xC0, 0x74, 0x56, 0x30, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4C, 0xF9, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x02, 0xA8, 0x16, 0xF0, 0x79, 0xF8 , -0x12, 0xE0, 0x38, 0xFA, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0xA8, 0x16, 0xF0, 0x03, 0xF8, 0x32, 0x20, 0x94, 0xFB, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x0C, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0xA8, 0x14, 0xF0, 0x05, 0xFF, 0x01, 0xF0, 0x10, 0x1B, 0x01, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x1E, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x13, 0xF0, 0x46, 0xBF, 0x18, 0x30, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x30, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x12, 0xF0, 0xD8, 0xFC, 0x9C, 0x31, 0x01, 0x00, 0x08, 0x00 , -0x00, 0x00, 0x68, 0x46, 0x12, 0xF0, 0x51, 0xFC, 0xFE, 0xF7, 0xF0, 0x35, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x68, 0x46, 0x12, 0xF0, 0x27, 0xFA, 0xFE, 0xF7, 0xF0, 0x3D, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0xA9, 0x11, 0xF0, 0x27, 0xFE, 0x20, 0x6F, 0xD0, 0x62, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0F, 0xF0, 0xB8, 0xFB, 0x80, 0x7E, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0xDF, 0xF8, 0x58, 0x82, 0xE0, 0x7E, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0D, 0xF0, 0x6A, 0xFE, 0x0B, 0xF0, 0x9A, 0xF9, 0x03, 0x20, 0xA8, 0xF5, 0x88, 0x71, 0x08, 0x60, 0xF2, 0xF7, 0x16, 0xF9, 0x7A, 0x48, 0x6B, 0x49, 0x9C, 0x7F, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x70, 0x51, 0x4D, 0xC4, 0x7F, 0x01, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x40, 0x49, 0x00, 0xF1, 0x52, 0x00, 0x3A, 0x4A, 0xA8, 0x81, 0x28, 0x83, 0xE8, 0x83, 0xE8, 0x84, 0xA8, 0x85, 0x2A, 0x60, 0x39, 0x48, 0x41, 0xF2, 0x11, 0x12, 0x2A, 0x85, 0x18, 0x90, 0x19, 0x91 , -0x39, 0x49, 0x1A, 0x91, 0x39, 0x49, 0x1B, 0x91, 0x39, 0x49, 0x20, 0x46, 0xDF, 0xF8, 0xC4, 0x90, 0x1C, 0x91, 0x38, 0x49, 0xDF, 0xF8, 0xC0, 0xB0, 0x31, 0x4E, 0x1D, 0x91, 0x48, 0x80, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0D, 0xF0, 0x7C, 0xFA, 0xB4, 0x80, 0x01, 0x00, 0x34, 0x00, 0x00, 0x00, 0x34, 0x12, 0x66, 0x09, 0x84, 0x6B, 0x00, 0x20, 0x94, 0x70, 0x00, 0x20, 0xB4, 0x70, 0x00, 0x20, 0xC4, 0x78, 0x00, 0x20, 0x50, 0x7A, 0x00, 0x20, 0xFE, 0xFF, 0x03, 0x00, 0xA4, 0x70, 0x00, 0x20, 0xB0, 0x70, 0x00, 0x20, 0xB8, 0x70, 0x00, 0x20, 0x60, 0x55, 0x30, 0x80, 0x3C, 0x5C, 0x00, 0x20, 0x04, 0x74, 0x00, 0x20, 0xB8, 0xE4, 0x01, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x20, 0x78, 0x07, 0xF0, 0x01, 0xFC, 0x20, 0x78, 0x07, 0xF0, 0x7A, 0xFB, 0x04, 0x4D, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x2D, 0xE9, 0xF0, 0x4F, 0xAD, 0xF1, 0x8C, 0x0D, 0x04, 0x68, 0x0F, 0x30, 0x03, 0x94, 0xFE, 0xF7, 0xDB, 0xFA, 0x04, 0xF1, 0x14, 0x00 , -0x04, 0x90, 0x21, 0x7D, 0x00, 0x20, 0x21, 0xF0, 0x73, 0x01, 0x88, 0x29, 0x08, 0xBF, 0x01, 0x20, 0x05, 0x90, 0x03, 0x98, 0x00, 0x1D, 0x06, 0x90, 0x5D, 0x48, 0x00, 0x68, 0x01, 0x28, 0x40, 0xF0, 0xFA, 0x83, 0x5B, 0x4D, 0x5C, 0x48, 0xDF, 0xF8, 0x70, 0x91, 0x29, 0x68, 0xDF, 0xF8, 0x70, 0x81, 0xDF, 0xF8, 0x74, 0xA1, 0x88, 0x42, 0x21, 0xD1, 0x10, 0x22, 0x00, 0x21, 0x48, 0x46, 0x4F, 0x46, 0xFB, 0xF7, 0xB7, 0xFE, 0x10, 0x22, 0x00, 0x21, 0x40, 0x46, 0xFB, 0xF7, 0xB2, 0xFE, 0x54, 0x49, 0x08, 0x68, 0x40, 0xF4, 0x80, 0x10, 0x08, 0x60, 0x01, 0x20, 0x51, 0x46, 0x08, 0x60, 0x00, 0x21, 0x29, 0x60, 0x39, 0x78, 0x46, 0x46, 0x00, 0x91, 0x31, 0x78, 0x41, 0xF2, 0x11, 0x13, 0x04, 0x22, 0x01, 0x91, 0x07, 0x21, 0x02, 0x90, 0x03, 0x20, 0xF3, 0xF7, 0x41, 0xFC, 0x51, 0x46, 0x07, 0x91, 0x08, 0x68, 0x56, 0x49, 0xDF, 0xF8, 0x5C, 0xB1, 0x01, 0x28, 0x08, 0x91, 0x56, 0x49, 0xDF, 0xF8, 0x48, 0xA1, 0x09, 0x91, 0x55, 0x49 , -0x0A, 0x91, 0x44, 0x49, 0x0B, 0x91, 0x44, 0x49, 0x0C, 0x91, 0x44, 0x49, 0x0D, 0x91, 0x44, 0x49, 0x0E, 0x91, 0x44, 0x49, 0x0F, 0x91, 0x44, 0x49, 0x10, 0x91, 0x44, 0x49, 0xCC, 0x4D, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x11, 0x91, 0x44, 0x49, 0x12, 0x91, 0x44, 0x49, 0x13, 0x91, 0x44, 0x49, 0x14, 0x91, 0x44, 0x49, 0x15, 0x91, 0x44, 0x49, 0x16, 0x91, 0x9F, 0x49, 0x17, 0x91, 0x9F, 0x49, 0x18, 0x91, 0x9F, 0x49, 0x19, 0x91, 0x9F, 0x49, 0x1A, 0x91, 0x9F, 0x49, 0x1B, 0x91, 0x9F, 0x49, 0x1C, 0x91, 0x9F, 0x49, 0x1D, 0x91, 0x9F, 0x49, 0x1E, 0x91, 0x9F, 0x49, 0x1F, 0x91, 0x40, 0xF0, 0x00, 0x81, 0x04, 0x98, 0x40, 0x78, 0x00, 0xF0, 0x03, 0x01, 0x01, 0x29, 0x40, 0xF0, 0xF9, 0x80, 0x04, 0x99, 0x09, 0x78, 0x01, 0xF0, 0x0C, 0x01, 0x08, 0x29, 0x40, 0xF0, 0xF2, 0x80, 0x06, 0x99, 0x09, 0x88, 0xA1, 0xF1, 0x3C, 0x01, 0x0E, 0xB2, 0x05, 0x99, 0x11, 0xB1, 0xA6, 0xF1, 0x02, 0x06, 0x36, 0xB2, 0xC0, 0x09, 0x4F, 0xF0 , -0x00, 0x07, 0x15, 0xD3, 0x08, 0x3E, 0x36, 0xB2, 0x03, 0x2E, 0x10, 0xD0, 0x17, 0x2E, 0x0E, 0xD0, 0x08, 0x3E, 0x36, 0xB2, 0x03, 0x2E, 0x08, 0xD0, 0x17, 0x2E, 0x06, 0xD0, 0x36, 0x1F, 0x36, 0xB2, 0x03, 0x2E, 0x14, 0xBF, 0x17, 0x2E, 0x02, 0x27, 0x02, 0xE0, 0x03, 0x27, 0x00, 0xE0, 0x01, 0x27, 0x03, 0x2E, 0x18, 0xBF, 0x17, 0x2E, 0x04, 0x9A, 0x40, 0xF0, 0xC8, 0x80, 0x0A, 0x32, 0x12, 0xF8, 0x01, 0x1B, 0x05, 0x24, 0x12, 0xF8, 0x01, 0x3B, 0x4B, 0x40, 0x64, 0x1E, 0xD9, 0xB2, 0xF9, 0xD1, 0x81, 0xEA, 0x21, 0x11, 0x03, 0x2E, 0x94, 0x4E, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x01, 0xF0, 0x0F, 0x01, 0x48, 0x46, 0x35, 0xD0, 0x43, 0x46, 0xCA, 0x5C, 0x52, 0x1C, 0xCA, 0x54, 0x0C, 0x18, 0x33, 0xE0, 0x50, 0x57, 0x02, 0x00, 0x04, 0x74, 0x00, 0x20, 0x34, 0x12, 0x66, 0x09, 0x60, 0x57, 0x02, 0x00, 0x70, 0x57, 0x02, 0x00, 0xC8, 0x48, 0x30, 0x80, 0x4C, 0x57, 0x02, 0x00, 0x88, 0x57, 0x02, 0x00, 0x80, 0x57, 0x02, 0x00 , -0x18, 0x58, 0x02, 0x00, 0x20, 0x58, 0x02, 0x00, 0x28, 0x58, 0x02, 0x00, 0x30, 0x58, 0x02, 0x00, 0x38, 0x58, 0x02, 0x00, 0x40, 0x58, 0x02, 0x00, 0xE4, 0x58, 0x02, 0x00, 0xE2, 0x58, 0x02, 0x00, 0x14, 0x58, 0x02, 0x00, 0xEF, 0x58, 0x02, 0x00, 0xE0, 0x58, 0x02, 0x00, 0xEE, 0x58, 0x02, 0x00, 0x5C, 0x57, 0x02, 0x00, 0xE6, 0x58, 0x02, 0x00, 0xE8, 0x58, 0x02, 0x00, 0x0C, 0x18, 0x20, 0x78, 0x40, 0x1C, 0x20, 0x70, 0x20, 0x78, 0x43, 0x46, 0x03, 0xEB, 0x01, 0x08, 0x0A, 0x28, 0x76, 0xDB, 0x98, 0xF8, 0x00, 0x00, 0x0A, 0x28, 0x72, 0xDB, 0x94, 0x48, 0x00, 0x25, 0x01, 0x60, 0x04, 0x98, 0x00, 0xF1, 0x0A, 0x09, 0x40, 0x78, 0x41, 0x08, 0x24, 0xBF, 0x04, 0x99, 0x09, 0x1D, 0x04, 0xD2, 0x80, 0x08, 0x2E, 0xBF, 0x49, 0x46, 0x04, 0x99, 0x10, 0x31, 0x0B, 0x98, 0x06, 0x22, 0xFA, 0xF7, 0xB9, 0xFC, 0x0C, 0x98, 0x06, 0x22, 0x49, 0x46, 0xFA, 0xF7, 0xB4, 0xFC, 0x9B, 0x48, 0x9A, 0x49, 0x5C, 0x4F, 0x02, 0x00, 0xC8, 0x00 , -0x00, 0x00, 0x00, 0x78, 0x08, 0x22, 0x08, 0x60, 0x58, 0x46, 0x07, 0x60, 0x0D, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0xAF, 0xFD, 0x08, 0x22, 0x0E, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0xAA, 0xFD, 0x08, 0x22, 0x0F, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0xA5, 0xFD, 0x08, 0x22, 0x10, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0xA0, 0xFD, 0x08, 0x22, 0x11, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0x9B, 0xFD, 0x08, 0x22, 0x12, 0x98, 0x29, 0x46, 0xFB, 0xF7, 0x96, 0xFD, 0x07, 0x99, 0x02, 0x20, 0x08, 0x60, 0x13, 0x99, 0x28, 0x46, 0x08, 0x80, 0x08, 0x99, 0x08, 0x70, 0x14, 0x99, 0x08, 0x70, 0x15, 0x99, 0x08, 0x60, 0x16, 0x99, 0x08, 0x70, 0x17, 0x99, 0x08, 0x60, 0x18, 0x99, 0x08, 0x60, 0x19, 0x99, 0x08, 0x60, 0x09, 0x99, 0x08, 0x80, 0x0A, 0x99, 0x08, 0x80, 0x1A, 0x99, 0x08, 0x80, 0x7D, 0x49, 0x0D, 0x60, 0x1B, 0x99, 0x08, 0x70, 0x1C, 0x99, 0x08, 0x70, 0x1D, 0x99, 0x08, 0x70, 0x1E, 0x99, 0x08, 0x80, 0x1F, 0x99, 0x08, 0x80, 0x51, 0x46, 0x08, 0x80 , -0x20, 0x78, 0x00, 0x96, 0x04, 0x22, 0x01, 0x90, 0x98, 0xF8, 0x00, 0x00, 0x41, 0xF2, 0x14, 0x13, 0x07, 0x21, 0x02, 0x90, 0x03, 0x20, 0xF3, 0xF7, 0x07, 0xFB, 0x07, 0x98, 0xA2, 0x49, 0x00, 0x68, 0x20, 0x91, 0xA2, 0x49, 0x05, 0x28, 0x21, 0x91, 0x9E, 0x49, 0x22, 0x91, 0x40, 0xF0, 0x37, 0x82, 0x04, 0x98, 0x40, 0x78, 0x24, 0x50, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x41, 0x08, 0x24, 0xBF, 0x04, 0x99, 0x09, 0x1D, 0x05, 0xD2, 0x80, 0x08, 0x27, 0xBF, 0x04, 0x99, 0x0A, 0x31, 0x04, 0x99, 0x10, 0x31, 0x0B, 0x98, 0xF8, 0xF7, 0x2D, 0xFC, 0x01, 0x28, 0x04, 0x98, 0x40, 0xF0, 0x23, 0x82, 0x40, 0x78, 0x00, 0xF0, 0x03, 0x00, 0x03, 0x28, 0x18, 0xD0, 0x02, 0x28, 0x07, 0xBF, 0x04, 0x99, 0x10, 0x31, 0x04, 0x99, 0x0A, 0x31, 0x13, 0xE0, 0x48, 0x58, 0x02, 0x00, 0x4C, 0x58, 0x02, 0x00, 0x50, 0x58, 0x02, 0x00, 0xF0, 0x58, 0x02, 0x00, 0xF2, 0x58, 0x02, 0x00, 0xF3, 0x58, 0x02, 0x00, 0xF4, 0x58, 0x02, 0x00, 0xEA, 0x58 , -0x02, 0x00, 0xEC, 0x58, 0x02, 0x00, 0x04, 0x99, 0x18, 0x31, 0x0C, 0x98, 0xF8, 0xF7, 0x06, 0xFC, 0x04, 0x99, 0x01, 0x28, 0x40, 0xF0, 0xFC, 0x81, 0x49, 0x78, 0x01, 0xF0, 0x03, 0x01, 0x01, 0x29, 0x40, 0xF0, 0xF6, 0x81, 0x59, 0x46, 0x09, 0x68, 0x04, 0x9A, 0x8E, 0x46, 0x51, 0x46, 0xD2, 0x8A, 0x0B, 0x88, 0x12, 0x11, 0x93, 0x42, 0x00, 0xF0, 0x37, 0x82, 0x0A, 0x80, 0x06, 0x99, 0x09, 0x88, 0x3C, 0x39, 0x0B, 0xB2, 0x71, 0x46, 0x49, 0x1E, 0x09, 0xD0, 0x49, 0x1E, 0x04, 0xD0, 0x49, 0x1E, 0x08, 0xD1, 0xA3, 0xF1, 0x10, 0x03, 0x04, 0xE0, 0xA3, 0xF1, 0x14, 0x03, 0x01, 0xE0, 0xA3, 0xF1, 0x08, 0x03, 0x1B, 0xB2, 0x05, 0x98, 0x10, 0xB1, 0xA3, 0xF1, 0x02, 0x03, 0x1B, 0xB2, 0xEC, 0x50, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x2B, 0x00, 0xF1, 0xCE, 0x81, 0x16, 0x98, 0x40, 0xF2, 0x77, 0x51, 0x99, 0x42, 0x0B, 0x90, 0x14, 0x98, 0x05, 0x90, 0x00, 0xF0, 0xA3, 0x81, 0x08, 0x46, 0x00, 0xF1, 0x3C, 0x00, 0x98, 0x42 , -0x05, 0x9A, 0x00, 0xF0, 0x9A, 0x81, 0xDF, 0xF8, 0x48, 0x82, 0x14, 0x78, 0x0B, 0x9A, 0xBB, 0x2C, 0x12, 0x78, 0x0C, 0x92, 0x63, 0xD1, 0xDD, 0xF8, 0x68, 0xC0, 0xBC, 0xF9, 0x00, 0x20, 0x91, 0x42, 0x18, 0xBF, 0x90, 0x42, 0x5B, 0xD1, 0x1C, 0x2B, 0x4F, 0xF0, 0x00, 0x00, 0xC0, 0xF2, 0x82, 0x81, 0x3D, 0x2B, 0x13, 0x9D, 0x80, 0xF2, 0x7E, 0x81, 0xA3, 0xF1, 0x1C, 0x01, 0x09, 0xB2, 0x29, 0x80, 0x1E, 0x9A, 0xDD, 0xF8, 0x30, 0x90, 0xB2, 0xF9, 0x00, 0x60, 0xB9, 0xF1, 0x01, 0x0F, 0x15, 0xD1, 0xDD, 0xF8, 0x24, 0x90, 0x03, 0x2E, 0x0D, 0xDA, 0xB9, 0xF9, 0x00, 0x70, 0xB9, 0x42, 0x07, 0xD0, 0x01, 0x26, 0x16, 0x80, 0x4A, 0x46, 0x0E, 0x46, 0x03, 0xE0, 0xC0, 0x46, 0x90, 0x57, 0x02, 0x00, 0x76, 0x1C, 0x16, 0x80, 0x1B, 0x9A, 0x16, 0x78, 0x1D, 0x9A, 0x16, 0x70, 0x0C, 0x9A, 0x1F, 0x9E, 0x02, 0x2A, 0x28, 0xD1, 0xB6, 0xF9, 0x00, 0x20, 0xBE, 0xF1, 0x00, 0x0F, 0x1A, 0xD0, 0x03, 0x2A, 0x35, 0x46, 0x0B, 0xDA, 0x0A, 0x9E , -0xB6, 0xF9, 0x00, 0x70, 0xB9, 0x42, 0x04, 0xD0, 0x01, 0x22, 0x2A, 0x80, 0x0A, 0x46, 0x35, 0x46, 0x00, 0xE0, 0xB4, 0x51, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x52, 0x1C, 0x2A, 0x80, 0x1C, 0x99, 0x0A, 0x78, 0x1D, 0x99, 0x0A, 0x70, 0x0F, 0xE0, 0xC0, 0x46, 0x58, 0x57, 0x02, 0x00, 0xA6, 0x64, 0x00, 0x20, 0x54, 0x57, 0x02, 0x00, 0x28, 0x80, 0x05, 0x99, 0x08, 0x70, 0x08, 0x9A, 0x11, 0x78, 0x41, 0xF0, 0x04, 0x01, 0x11, 0x70, 0x04, 0x46, 0x45, 0x46, 0xAC, 0xF8, 0x00, 0x00, 0x28, 0x70, 0xCC, 0x2C, 0x09, 0x9A, 0x40, 0xF0, 0x00, 0x81, 0xDD, 0xF8, 0x7C, 0xC0, 0x0A, 0x99, 0x1E, 0x9F, 0x45, 0x46, 0xB1, 0xF9, 0x00, 0x60, 0xB2, 0xF9, 0x00, 0x90, 0x28, 0x78, 0xCD, 0xF8, 0x50, 0xC0, 0x16, 0x91, 0x0A, 0x97, 0x13, 0x92, 0x09, 0x95, 0x0C, 0x99, 0x01, 0x29, 0x05, 0xD1, 0x05, 0x9A, 0xB0, 0xEB, 0x49, 0x0F, 0x01, 0xD1, 0x00, 0x21, 0x11, 0x70, 0x0C, 0x99, 0x02, 0x29, 0x05, 0xD1, 0x05, 0x9A, 0xB0, 0xEB, 0x46, 0x0F , -0x01, 0xD1, 0x00, 0x21, 0x11, 0x70, 0xB3, 0xF5, 0x14, 0x7F, 0x40, 0xF3, 0xBA, 0x80, 0x40, 0xF2, 0x51, 0x31, 0x99, 0x42, 0x08, 0x99, 0x40, 0xF3, 0xB4, 0x80, 0x09, 0x78, 0x06, 0x29, 0x00, 0xF0, 0xB0, 0x80, 0xA3, 0xF2, 0x51, 0x21, 0x0B, 0xB2, 0x4F, 0xEA, 0xE3, 0x01, 0x03, 0xEB, 0x11, 0x71, 0x4F, 0xEA, 0x21, 0x11, 0xCA, 0xB2, 0x4F, 0xEA, 0xE3, 0x01, 0x03, 0xEB, 0x11, 0x71, 0x15, 0x9D, 0x21, 0xF0, 0x0F, 0x01, 0xA3, 0xEB, 0x01, 0x03, 0x1D, 0x99, 0x2E, 0x68, 0x7C, 0x52, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x91, 0xF8, 0x00, 0xE0, 0x5F, 0xFA, 0x83, 0xF8, 0x60, 0xB1, 0x31, 0x18, 0x11, 0xF8, 0x01, 0x1C, 0x51, 0x40, 0xCB, 0xB2, 0x07, 0xE0, 0xC0, 0x46, 0x58, 0x58, 0x02, 0x00, 0x54, 0x58, 0x02, 0x00, 0xDC, 0x58, 0x02, 0x00, 0x13, 0x46, 0x19, 0x99, 0xDD, 0xF8, 0x6C, 0xA0, 0xDD, 0xF8, 0x70, 0xB0, 0xD1, 0xF8, 0x00, 0xC0, 0x00, 0xF0, 0x0F, 0x01, 0x99, 0x42, 0x22, 0xD0, 0x0A, 0x9B, 0x86, 0x45, 0x9A, 0xF8 , -0x00, 0x10, 0xB3, 0xF9, 0x00, 0x30, 0x9B, 0xF8, 0x00, 0x50, 0x15, 0xDD, 0x37, 0x5C, 0x44, 0x1C, 0x82, 0xEA, 0x07, 0x0C, 0x04, 0xF0, 0x0F, 0x07, 0x67, 0x45, 0x0B, 0xD0, 0xA6, 0x45, 0x06, 0x44, 0x0A, 0xDD, 0x76, 0x78, 0x72, 0x40, 0x86, 0x1C, 0x06, 0xF0, 0x0F, 0x06, 0x96, 0x42, 0x03, 0xD1, 0xC0, 0x1C, 0x4D, 0xE0, 0x80, 0x1C, 0x4B, 0xE0, 0x05, 0x9E, 0x00, 0x22, 0x32, 0x70, 0x4A, 0xE0, 0x00, 0xF0, 0x1F, 0x01, 0x01, 0x22, 0x8A, 0x40, 0x17, 0x99, 0xC4, 0x10, 0x0B, 0x68, 0x24, 0xF0, 0x03, 0x01, 0x5C, 0xF8, 0x01, 0x70, 0x3A, 0x42, 0x32, 0xD1, 0x5F, 0x58, 0x3A, 0x42, 0x06, 0x44, 0x1F, 0xD0, 0x18, 0x9B, 0x37, 0x78, 0x1B, 0x68, 0xB8, 0x45, 0x04, 0xD1, 0x58, 0x58, 0x02, 0x42, 0x19, 0xD0, 0x63, 0x46, 0x15, 0xE0, 0xA1, 0x10, 0x8C, 0x00, 0x19, 0x59, 0x91, 0x43, 0x19, 0x51, 0x00, 0x90, 0x30, 0x78, 0x01, 0x90, 0x44, 0x53, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x03, 0x20, 0x06, 0x21, 0x02, 0x46, 0x4D, 0xF6 , -0x80, 0x53, 0xF3, 0xF7, 0x63, 0xF9, 0x09, 0x99, 0x28, 0x68, 0x0E, 0x78, 0x36, 0x18, 0x06, 0xE0, 0xE3, 0x58, 0x02, 0x00, 0xA0, 0x10, 0x81, 0x00, 0x58, 0x58, 0x02, 0x43, 0x5A, 0x50, 0x86, 0xF8, 0x00, 0x80, 0x0B, 0x98, 0x00, 0x78, 0x0C, 0x90, 0x13, 0x98, 0xB0, 0xF9, 0x00, 0x90, 0x09, 0x98, 0x00, 0x78, 0x0A, 0x9A, 0x40, 0x1C, 0x9B, 0xF8, 0x00, 0x50, 0x9A, 0xF8, 0x00, 0x10, 0xB2, 0xF9, 0x00, 0x30, 0x09, 0x9A, 0xC0, 0xB2, 0x10, 0x70, 0x0C, 0x9A, 0x01, 0x2A, 0x03, 0xD1, 0x88, 0x42, 0xC8, 0xBF, 0x8A, 0xF8, 0x00, 0x00, 0x0C, 0x99, 0x02, 0x29, 0x07, 0xD1, 0xA8, 0x42, 0xC8, 0xBF, 0x8B, 0xF8, 0x00, 0x00, 0x02, 0xE0, 0x0A, 0x98, 0xB0, 0xF9, 0x00, 0x30, 0xB9, 0xF1, 0x00, 0x0F, 0x06, 0xDD, 0x03, 0x2B, 0x04, 0xD1, 0x11, 0x98, 0x01, 0x22, 0x49, 0x46, 0x00, 0xF0, 0x72, 0xFB, 0x16, 0x98, 0xB0, 0xF9, 0x00, 0x10, 0x14, 0x98, 0x00, 0x29, 0xB0, 0xF9, 0x00, 0x00, 0x05, 0xDD, 0x03, 0x28, 0x03, 0xD1, 0x12, 0x98 , -0x02, 0x22, 0x00, 0xF0, 0x64, 0xFB, 0x05, 0x98, 0x04, 0x78, 0x0B, 0x98, 0x00, 0x78, 0x0C, 0x90, 0x08, 0x98, 0xBB, 0x2C, 0x01, 0x78, 0x02, 0xD0, 0xAA, 0x2C, 0x2F, 0xD0, 0x31, 0xE0, 0x0C, 0x98, 0x01, 0x28, 0x0B, 0xD1, 0x0D, 0x98, 0x17, 0x9A, 0x10, 0x60, 0x0C, 0x54, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x0F, 0x98, 0x18, 0x9A, 0x10, 0x60, 0x19, 0x9A, 0x11, 0x98, 0x10, 0x60, 0x47, 0x4A, 0x15, 0x98, 0x02, 0x60, 0x0C, 0x98, 0x02, 0x28, 0x0B, 0xD1, 0x0E, 0x98, 0x17, 0x9A, 0x10, 0x60, 0x10, 0x98, 0x18, 0x9A, 0x10, 0x60, 0x19, 0x9A, 0x12, 0x98, 0x10, 0x60, 0x41, 0x4A, 0x15, 0x98, 0x02, 0x60, 0xCC, 0x20, 0x0F, 0xE0, 0x08, 0x99, 0x09, 0x78, 0x0C, 0xE0, 0x02, 0x20, 0x00, 0xE0, 0x01, 0x20, 0x0B, 0x99, 0x08, 0x70, 0x1A, 0x99, 0x0B, 0x80, 0x13, 0x99, 0x00, 0x20, 0x08, 0x80, 0x08, 0x98, 0x01, 0x78, 0xBB, 0x20, 0x05, 0x9A, 0x10, 0x70, 0x06, 0x29, 0x07, 0x99, 0x11, 0xD1, 0x06, 0x20, 0x08, 0x60, 0x20, 0x98 , -0x00, 0x25, 0x05, 0x70, 0x21, 0x98, 0x29, 0x46, 0x04, 0x22, 0xFB, 0xF7, 0x27, 0xFB, 0x01, 0x20, 0x00, 0xF0, 0x3A, 0xFA, 0x02, 0x20, 0x00, 0xF0, 0x37, 0xFA, 0x22, 0x98, 0x05, 0x60, 0x07, 0x98, 0x04, 0x99, 0x00, 0x68, 0x07, 0x28, 0x44, 0xD1, 0x09, 0x78, 0x03, 0x9C, 0x80, 0x29, 0x40, 0xD1, 0x38, 0x34, 0x21, 0x78, 0x64, 0x1C, 0x22, 0x46, 0x00, 0x29, 0x3A, 0xD1, 0x20, 0x98, 0x90, 0xF8, 0x00, 0xA0, 0x22, 0x98, 0x05, 0x90, 0xD0, 0xF8, 0x00, 0x90, 0x51, 0x46, 0x48, 0x46, 0xFD, 0xF7, 0x79, 0xF9, 0x5F, 0xFA, 0x80, 0xF8, 0x21, 0x9F, 0x15, 0x78, 0x51, 0x46, 0x48, 0x46, 0x17, 0xF8, 0x08, 0x30, 0xFD, 0xF7, 0xD4, 0x54, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x6F, 0xF9, 0x1E, 0x46, 0x1B, 0x1D, 0xC0, 0xB2, 0x38, 0x5C, 0x00, 0x1D, 0xA8, 0x42, 0x28, 0x46, 0x07, 0xDD, 0x80, 0x18, 0x82, 0x1C, 0x10, 0x78, 0x2D, 0x18, 0xAD, 0x1C, 0xAD, 0xB2, 0xAB, 0x42, 0xF7, 0xDC, 0x04, 0xF8, 0x01, 0x6B, 0x11, 0x49, 0x32, 0x46 , -0x20, 0x46, 0x01, 0xEB, 0x48, 0x11, 0xFA, 0xF7, 0xDC, 0xF9, 0xAD, 0x1B, 0xFF, 0x20, 0x34, 0x19, 0xAD, 0x1E, 0x04, 0xF8, 0x01, 0x0B, 0x25, 0x70, 0x05, 0x98, 0x01, 0x68, 0x49, 0x1C, 0x01, 0x60, 0x07, 0x98, 0x00, 0x68, 0x07, 0x28, 0x04, 0xD0, 0x06, 0x99, 0x08, 0x68, 0x20, 0xF4, 0x80, 0x30, 0x08, 0x60, 0x23, 0xB0, 0xBD, 0xE8, 0xF0, 0x8F, 0xC0, 0x46, 0x94, 0x57, 0x02, 0x00, 0xD4, 0x57, 0x02, 0x00, 0x5C, 0x58, 0x02, 0x00, 0x6E, 0x48, 0x2D, 0xE9, 0xF0, 0x47, 0x6E, 0x4D, 0x82, 0x89, 0x4A, 0xF6, 0x55, 0x21, 0xAD, 0xF5, 0xFE, 0x7D, 0x91, 0x42, 0x4F, 0xF0, 0x00, 0x01, 0xAD, 0xF1, 0x38, 0x0D, 0x12, 0xD1, 0x29, 0x60, 0x68, 0x4A, 0x11, 0x70, 0x41, 0xF2, 0x10, 0x41, 0x81, 0x81, 0x4F, 0xF6, 0xFF, 0x70, 0x66, 0x49, 0x08, 0x60, 0x28, 0x68, 0x00, 0x90, 0x02, 0x22, 0x05, 0x21, 0x03, 0x20, 0x41, 0xF2, 0x34, 0x23, 0xF3, 0xF7, 0x48, 0xF8, 0x29, 0x68, 0x61, 0x4C, 0x01, 0x29, 0x20, 0x68, 0x40, 0xF0, 0xAF, 0x80 , -0x5F, 0x4D, 0x02, 0x28, 0x29, 0x68, 0x23, 0xD1, 0x9C, 0x55, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x5E, 0x48, 0x00, 0x68, 0x5F, 0x4A, 0x12, 0x5C, 0x00, 0x92, 0x01, 0x91, 0x5E, 0x49, 0x09, 0x68, 0x02, 0x91, 0x5B, 0x49, 0x0B, 0x5C, 0x03, 0x20, 0x07, 0x21, 0x04, 0x22, 0xF3, 0xF7, 0x2E, 0xF8, 0x5B, 0x49, 0x4A, 0x79, 0x59, 0x48, 0x00, 0x92, 0x02, 0x79, 0x01, 0x92, 0x40, 0x79, 0x04, 0x22, 0x02, 0x90, 0x03, 0x20, 0x0B, 0x79, 0x07, 0x21, 0xF3, 0xF7, 0x20, 0xF8, 0x03, 0x20, 0x20, 0x60, 0xEC, 0xF7, 0x1A, 0xFF, 0x20, 0x68, 0x29, 0x68, 0x04, 0x28, 0x15, 0xD1, 0x0D, 0x20, 0x8D, 0xF8, 0x10, 0x00, 0x00, 0x25, 0x8D, 0xF8, 0x14, 0x50, 0x8D, 0xF8, 0x15, 0x10, 0x28, 0x46, 0x63, 0x21, 0x00, 0xF0, 0xAA, 0xFB, 0x63, 0x21, 0x28, 0x46, 0x00, 0xF0, 0xFA, 0xFA, 0x03, 0xA9, 0x04, 0xA8, 0xE0, 0xF7, 0xCA, 0xFC, 0x05, 0x20, 0x20, 0x60, 0x46, 0x4D, 0x05, 0x28, 0x29, 0x68, 0x01, 0xF1, 0x01, 0x01, 0x29, 0x60, 0x1D, 0xD1 , -0x44, 0x4A, 0x8A, 0x42, 0x1A, 0xD1, 0x43, 0x48, 0x01, 0x68, 0x00, 0x91, 0x40, 0x68, 0x01, 0x90, 0x03, 0x20, 0x02, 0x46, 0x06, 0x21, 0x41, 0x4E, 0x33, 0x46, 0x22, 0x3B, 0xF2, 0xF7, 0xEB, 0xFF, 0x3E, 0x48, 0x01, 0x68, 0x00, 0x91, 0x40, 0x68, 0x33, 0x46, 0x06, 0x21, 0x01, 0x90, 0x03, 0x20, 0x02, 0x46, 0xF2, 0xF7, 0xE0, 0xFF, 0x00, 0x20, 0x28, 0x60, 0x20, 0x68, 0x06, 0x28, 0x47, 0xD1, 0x4F, 0xF0, 0x64, 0x56, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x07, 0x0A, 0xC4, 0xF8, 0x00, 0xA0, 0x03, 0x20, 0x48, 0xF2, 0x70, 0x53, 0x01, 0x22, 0x04, 0x21, 0xF2, 0xF7, 0xD0, 0xFF, 0xDF, 0xF8, 0xCC, 0x80, 0x31, 0x4F, 0x48, 0xF2, 0x70, 0x59, 0x01, 0x25, 0x49, 0xEA, 0x05, 0x03, 0x01, 0x22, 0x04, 0x21, 0x03, 0x20, 0xF2, 0xF7, 0xC3, 0xFF, 0x00, 0x24, 0x17, 0xF8, 0x02, 0x0F, 0x20, 0xE0, 0x69, 0x01, 0x04, 0xEB, 0x08, 0x02, 0x88, 0x18, 0xC6, 0x1C, 0x43, 0x78, 0x00, 0x93, 0x80, 0x78, 0x01, 0x90, 0x30, 0x78, 0x02, 0x90 , -0x53, 0x5C, 0x51, 0x46, 0x04, 0x22, 0x03, 0x20, 0xF2, 0xF7, 0xAE, 0xFF, 0x38, 0x78, 0x01, 0x1B, 0xB1, 0xF1, 0xFF, 0x3F, 0x08, 0xD1, 0xF0, 0x78, 0x00, 0x90, 0xB3, 0x78, 0x03, 0x20, 0x02, 0x22, 0x05, 0x21, 0xF2, 0xF7, 0xA1, 0xFF, 0x38, 0x78, 0x24, 0x1D, 0xE4, 0xB2, 0xA0, 0x42, 0xDC, 0xDC, 0xAD, 0x1C, 0xED, 0xB2, 0x04, 0x2D, 0xCD, 0xDB, 0x63, 0x21, 0x4F, 0xF4, 0x05, 0x70, 0xF1, 0xF7, 0xA3, 0xFC, 0xE1, 0xF7, 0x85, 0xFD, 0x7F, 0xB0, 0x0E, 0xB0, 0xBD, 0xE8, 0xF0, 0x87, 0xC0, 0x46, 0x04, 0x74, 0x00, 0x20, 0x50, 0x57, 0x02, 0x00, 0x6C, 0x5D, 0x02, 0x00, 0x40, 0x00, 0x3D, 0x80, 0x4C, 0x57, 0x02, 0x00, 0x58, 0x57, 0x02, 0x00, 0x90, 0x57, 0x02, 0x00, 0x60, 0x57, 0x02, 0x00, 0x70, 0x57, 0x02, 0x00, 0x5C, 0x57, 0x02, 0x00, 0x80, 0x57, 0x02, 0x00, 0x2C, 0x57, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x88, 0x57, 0x02, 0x00, 0x54, 0x57, 0x02, 0x00, 0xFF, 0xFF, 0x03, 0x00, 0x38, 0x58, 0x02, 0x00, 0x40, 0x58 , -0x02, 0x00, 0x66, 0x55, 0xDD, 0xEE, 0xDB, 0x58, 0x02, 0x00, 0x5C, 0x58, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF4, 0x57, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBC, 0x58, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2D, 0xE9, 0xFE, 0x4F, 0x4A, 0x4A, 0x49, 0x4D, 0x00, 0x90, 0xB2, 0xF9, 0x00, 0x20, 0x43, 0xA3, 0x29, 0x78, 0x01, 0x93, 0x43, 0xA3, 0x01, 0x28, 0x02, 0x93, 0x47, 0x4E, 0xDF, 0xF8, 0x1C, 0x81, 0xDF, 0xF8, 0x1C, 0x91, 0x1E, 0xD0, 0x43, 0x48, 0x04, 0x78, 0x48, 0x01, 0x87, 0x19, 0x30, 0x18, 0x01, 0x99, 0x06, 0x22, 0xF9, 0xF7, 0xC8, 0xFF, 0x40, 0x46, 0x01, 0x78, 0x00, 0x68, 0x07, 0xF1, 0x06, 0x07, 0x07, 0xF8, 0x01, 0x1B, 0x07, 0xF8, 0x01, 0x4B, 0x4F, 0xF0, 0x00, 0x01, 0x07, 0xF8, 0x01, 0x1B, 0x20, 0xB9, 0x39, 0x70, 0x28, 0x78, 0x4E, 0x46, 0x0A, 0x21, 0x58, 0xE0, 0xDF, 0xF8, 0xE4, 0xB0, 0x0F, 0xE0, 0xD4, 0xB2, 0x48, 0x01, 0x87, 0x19, 0x30, 0x18, 0x02, 0x99, 0x07, 0x22, 0xF9, 0xF7, 0xAA, 0xFF, 0xFF, 0x1D, 0x00, 0x20, 0xDF, 0xF8, 0xCC, 0xB0, 0x07, 0xF8, 0x01, 0x4B, 0x07, 0xF8, 0x01, 0x0B, 0x16, 0x2C, 0xB2, 0x46, 0xAE, 0xBF, 0x14, 0x20, 0x84, 0x59 , -0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x60, 0x1E, 0xC0, 0xB2, 0x38, 0x70, 0x4E, 0x46, 0x7F, 0x1C, 0x40, 0x1C, 0xC2, 0xB2, 0x28, 0x78, 0x5B, 0x46, 0x00, 0x21, 0x32, 0x54, 0x38, 0x46, 0x00, 0xF0, 0xBC, 0xF9, 0x28, 0x78, 0x16, 0x2C, 0x31, 0x5C, 0x01, 0xF1, 0x0A, 0x01, 0x31, 0x54, 0x00, 0xF1, 0x01, 0x00, 0xC0, 0xB2, 0x28, 0x70, 0x2B, 0xDB, 0x00, 0x99, 0x0A, 0xEB, 0x40, 0x10, 0x07, 0x46, 0x01, 0x29, 0xA4, 0xF1, 0x01, 0x09, 0x09, 0xD0, 0x01, 0x99, 0x06, 0x22, 0xF9, 0xF7, 0x79, 0xFF, 0x40, 0x46, 0x00, 0x78, 0xBF, 0x1D, 0x07, 0xF8, 0x01, 0x0B, 0x04, 0xE0, 0x02, 0x99, 0x07, 0x22, 0xF9, 0xF7, 0x6F, 0xFF, 0xFF, 0x1D, 0x07, 0xF8, 0x01, 0x4B, 0x15, 0x3C, 0x15, 0x21, 0x28, 0x78, 0xE2, 0xB2, 0x5B, 0x46, 0x07, 0xF8, 0x01, 0x1B, 0x32, 0x54, 0x07, 0xF8, 0x01, 0x9B, 0x38, 0x46, 0x00, 0xF0, 0x8B, 0xF9, 0x28, 0x78, 0x31, 0x5C, 0x0A, 0x31, 0x31, 0x54, 0x40, 0x1C, 0x28, 0x70, 0xBD, 0xE8, 0xFE, 0x8F, 0xC0, 0x46 , -0x54, 0x54, 0x54, 0x4B, 0x45, 0x59, 0x00, 0xC0, 0x54, 0x54, 0x54, 0x53, 0x53, 0x49, 0x44, 0x00, 0x54, 0x58, 0x02, 0x00, 0xE6, 0x58, 0x02, 0x00, 0xE8, 0x58, 0x02, 0x00, 0x5C, 0x58, 0x02, 0x00, 0x5C, 0x57, 0x02, 0x00, 0xDC, 0x58, 0x02, 0x00, 0xD4, 0x57, 0x02, 0x00, 0x94, 0x57, 0x02, 0x00, 0xFF, 0xB5, 0x0C, 0x46, 0x06, 0x46, 0xF7, 0xF7, 0x4C, 0x5A, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0xE3, 0xFE, 0x15, 0x49, 0x15, 0x4A, 0x0D, 0x68, 0x01, 0xA9, 0x92, 0xE8, 0x8C, 0x00, 0x81, 0xE8, 0x8C, 0x00, 0xF7, 0xF7, 0xFD, 0xFE, 0x01, 0x98, 0x11, 0x49, 0x28, 0x1A, 0x03, 0x9D, 0x25, 0x60, 0x02, 0x9A, 0x80, 0x18, 0x82, 0x42, 0x30, 0x60, 0x0E, 0xD8, 0x08, 0x46, 0x00, 0x68, 0x22, 0x68, 0x82, 0x42, 0x0E, 0xD2, 0x68, 0x46, 0xE8, 0xF7, 0xB9, 0xFF, 0x9D, 0xF8, 0x00, 0x00, 0x05, 0xF1, 0x01, 0x05, 0x30, 0xB9, 0x03, 0x95, 0x03, 0xE0, 0x25, 0x68, 0x6D, 0x1C, 0x25, 0x60, 0x0C, 0x46, 0x25, 0x60, 0x00, 0x20, 0x00, 0x90 , -0xFF, 0xBD, 0x60, 0x55, 0x30, 0x80, 0x24, 0x5D, 0x00, 0x20, 0x6C, 0x5D, 0x02, 0x00, 0x70, 0xB5, 0x4D, 0x00, 0x20, 0x2D, 0x4F, 0xF0, 0x00, 0x01, 0x0C, 0x46, 0x4F, 0xEA, 0x55, 0x13, 0x09, 0xD3, 0x50, 0xF8, 0x21, 0x60, 0xB6, 0xF1, 0xFF, 0x3F, 0x04, 0xD1, 0x49, 0x1C, 0xC9, 0xB2, 0x20, 0x3D, 0x5B, 0x1E, 0xF5, 0xD1, 0x4D, 0xB1, 0x20, 0x2D, 0x09, 0xD2, 0x01, 0x23, 0xAB, 0x40, 0x5B, 0x1E, 0x50, 0xF8, 0x21, 0x00, 0x18, 0x40, 0x83, 0x42, 0x01, 0xD1, 0x4F, 0xF0, 0x01, 0x04, 0x07, 0x48, 0x03, 0x78, 0x2C, 0xB9, 0x01, 0x21, 0x91, 0x40, 0xC9, 0x43, 0xC9, 0xB2, 0x0B, 0x40, 0x03, 0xE0, 0x01, 0x21, 0x91, 0x40, 0xC9, 0xB2, 0x0B, 0x43, 0x03, 0x70, 0x70, 0xBD, 0xC0, 0x46, 0xEE, 0x58, 0x02, 0x00, 0x14, 0x5B, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x7E, 0xB5, 0x05, 0x46, 0x95, 0xF8, 0x36, 0x20, 0x0E, 0x46, 0x0F, 0x49, 0x07, 0x2A, 0x12, 0xBF, 0x08, 0x68, 0x01, 0x20, 0x08, 0x60, 0x0D, 0x4C, 0x00, 0x92, 0x21, 0x68 , -0x41, 0xF2, 0x12, 0x13, 0x04, 0x22, 0x01, 0x91, 0x07, 0x21, 0x02, 0x90, 0x03, 0x20, 0xF2, 0xF7, 0x6B, 0xFD, 0x31, 0x46, 0x28, 0x46, 0xE7, 0xF7, 0x21, 0xFC, 0x21, 0x68, 0x00, 0x22, 0x05, 0x29, 0x02, 0xD1, 0x04, 0x49, 0x08, 0x31, 0x0A, 0x60, 0x7E, 0xBD, 0xC0, 0x46, 0x50, 0x57, 0x02, 0x00, 0x4C, 0x57, 0x02, 0x00, 0x00, 0x7C, 0x00, 0x20, 0x0E, 0x49, 0x09, 0x68, 0x49, 0x08, 0x15, 0xD2, 0x0D, 0x49, 0x09, 0x78, 0x91, 0xB1, 0x44, 0xF2, 0xE9, 0x21, 0x68, 0xB9, 0x0B, 0x48, 0x01, 0x78, 0x07, 0x48, 0x01, 0x29, 0x03, 0xD1, 0x0A, 0x49, 0x09, 0x78, 0x0E, 0x29, 0x02, 0xD0, 0x44, 0xF2, 0xDE, 0x21, 0x41, 0x60, 0x01, 0x21, 0x00, 0xE0, 0x06, 0x48, 0x01, 0x60, 0x70, 0x47, 0xC0, 0x46, 0x00, 0x97, 0x3C, 0x80, 0x10, 0x0C, 0x30, 0x80, 0xDD, 0x6A, 0x00, 0x20, 0xE0, 0x7C, 0x00, 0x20, 0xA6, 0x64, 0x00, 0x20, 0x04, 0x97, 0x3C, 0x80, 0x00, 0xB5, 0xAD, 0xF1, 0x18, 0x0D, 0x4F, 0xF0, 0x09, 0x00, 0x00, 0x90, 0x4F, 0xF4 , -0x0C, 0x60, 0x03, 0x90, 0x4F, 0xF0, 0x00, 0x00, 0x01, 0x90, 0x02, 0x90, 0x08, 0x48, 0x08, 0x49, 0x05, 0x90, 0x08, 0x68, 0x20, 0xB9, 0xDC, 0x5B, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0x68, 0x46, 0x03, 0x22, 0xED, 0xF7, 0x3A, 0xFA, 0x03, 0xE0, 0x68, 0x46, 0x03, 0x22, 0xED, 0xF7, 0xC1, 0xFC, 0x06, 0xB0, 0x00, 0xBD, 0xC0, 0x46, 0x81, 0x5C, 0x02, 0x00, 0xDC, 0x7C, 0x00, 0x20, 0x1C, 0xB5, 0x0F, 0x4C, 0x22, 0x68, 0x01, 0x2A, 0x15, 0xD1, 0x0C, 0x48, 0x00, 0x68, 0x07, 0x28, 0x14, 0xBF, 0x40, 0x20, 0x4F, 0xF4, 0x05, 0x70, 0x63, 0x21, 0xF1, 0xF7, 0x0F, 0xFA, 0x20, 0x68, 0x00, 0x90, 0x43, 0xF2, 0x33, 0x33, 0x06, 0x21, 0x40, 0x20, 0x01, 0x90, 0x03, 0x20, 0x02, 0x46, 0xF2, 0xF7, 0xF4, 0xFC, 0x1C, 0xBD, 0xF1, 0xF7, 0x01, 0xFA, 0x1C, 0xBD, 0x4C, 0x57, 0x02, 0x00, 0x50, 0x57, 0x02, 0x00, 0x1E, 0xB5, 0x04, 0x46, 0x0A, 0x49, 0x00, 0x20, 0x0A, 0x4A, 0x08, 0x60, 0x0A, 0x49, 0x00, 0x90, 0x01, 0x90, 0x11, 0x60 , -0x09, 0x4A, 0x45, 0xF2, 0x55, 0x53, 0x02, 0x91, 0x10, 0x60, 0x07, 0x21, 0x04, 0x22, 0x03, 0x20, 0xF2, 0xF7, 0xD8, 0xFC, 0x20, 0x46, 0xE7, 0xF7, 0x59, 0xFD, 0x1E, 0xBD, 0x50, 0x57, 0x02, 0x00, 0x04, 0x74, 0x00, 0x20, 0x34, 0x12, 0x66, 0x09, 0x4C, 0x57, 0x02, 0x00, 0x00, 0xB5, 0x0B, 0x49, 0x00, 0x20, 0x08, 0x60, 0xE0, 0x20, 0xF7, 0xF7, 0xAA, 0xFD, 0x09, 0x49, 0x09, 0x78, 0x49, 0xB1, 0x08, 0x49, 0x0A, 0x78, 0x08, 0x49, 0x00, 0x2A, 0x06, 0xBF, 0x01, 0x22, 0x09, 0x1F, 0x44, 0xF2, 0xA4, 0x5C, 0x02, 0x00, 0xC8, 0x00, 0x00, 0x00, 0xE9, 0x22, 0x0A, 0x60, 0xF7, 0xF7, 0xA1, 0xFD, 0x00, 0xBD, 0xC0, 0x46, 0xDC, 0x7C, 0x00, 0x20, 0xDD, 0x6A, 0x00, 0x20, 0xA7, 0x64, 0x00, 0x20, 0x04, 0x97, 0x3C, 0x80, 0x50, 0xB9, 0x09, 0x48, 0x00, 0x78, 0x01, 0x28, 0x06, 0xD1, 0x08, 0x48, 0x00, 0x78, 0x0E, 0x28, 0x08, 0xBF, 0x44, 0xF2, 0xCB, 0x20, 0x01, 0xD0, 0x44, 0xF2, 0xDE, 0x20, 0x04, 0x49, 0x08, 0x60, 0x01, 0x20 , -0x41, 0xF8, 0x04, 0x0C, 0x70, 0x47, 0xE0, 0x7C, 0x00, 0x20, 0xA6, 0x64, 0x00, 0x20, 0x04, 0x97, 0x3C, 0x80, 0x30, 0xB5, 0x0D, 0x46, 0x84, 0x69, 0xDC, 0xF7, 0x97, 0xFB, 0x24, 0x1D, 0x01, 0x28, 0x04, 0xEB, 0x45, 0x01, 0x06, 0xD1, 0x89, 0x8E, 0x04, 0xEB, 0x85, 0x04, 0x11, 0xB9, 0x21, 0x69, 0x01, 0xB9, 0x00, 0x20, 0x30, 0xBD, 0x30, 0xB5, 0x05, 0x46, 0x5A, 0xB1, 0x03, 0xEB, 0x41, 0x03, 0x1C, 0x78, 0x59, 0x78, 0x41, 0xEA, 0x04, 0x11, 0x52, 0x1E, 0x03, 0xF1, 0x02, 0x03, 0x05, 0xF8, 0x01, 0x1B, 0xF5, 0xD1, 0x30, 0xBD, 0x00, 0xB5, 0x00, 0x20, 0xE7, 0xF7, 0xD6, 0xFE, 0x03, 0x48, 0x01, 0x68, 0x03, 0x29, 0x04, 0xBF, 0x04, 0x21, 0x01, 0x60, 0x00, 0xBD, 0xC0, 0x46, 0x4C, 0x57, 0x02, 0x00, 0x03, 0x4A, 0x12, 0x68, 0x01, 0x2A, 0x04, 0xBF, 0x02, 0x48, 0x63, 0x21, 0xF1, 0xF7, 0x76, 0xB9, 0x50, 0x57, 0x02, 0x00, 0x02, 0x20, 0x01, 0x00, 0x6C, 0x5D, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff --git a/drivers/cc3000/src/patch_prog.c b/drivers/cc3000/src/patch_prog.c deleted file mode 100644 index fd128928fbaff..0000000000000 --- a/drivers/cc3000/src/patch_prog.c +++ /dev/null @@ -1,414 +0,0 @@ -#include -#include -#include "cc3000_common.h" -#include "nvmem.h" -#include "ccspi.h" -#include "hci.h" -#include "wlan.h" -#include "patch_prog.h" -#define BIT0 0x1 -#define BIT1 0x2 -#define BIT2 0x4 -#define BIT3 0x8 -#define BIT4 0x10 -#define BIT5 0x20 -#define BIT6 0x40 -#define BIT7 0x80 - -static unsigned char ucStatus_Dr; -static unsigned char ucStatus_FW; -static unsigned char return_status = 0xFF; - -static signed char mac_status = -1; -static unsigned char counter = 0; - -// Array to store RM parameters from EEPROM. -static unsigned char cRMParamsFromEeprom[128]; -// Array to store MAC address from EEPROM. -static unsigned char cMacFromEeprom[MAC_ADDR_LEN]; -// Smart Config Prefix -static const char aucCC3000_prefix[] = {'T', 'T', 'T'}; - -static void systick_sleep(unsigned long ms) { - extern void HAL_Delay(volatile uint32_t Delay); - HAL_Delay(ms); -} - -// 2 dim array to store address and length of new FAT -static const unsigned short aFATEntries[2][NVMEM_RM_FILEID + 1] = -/* address */ {{0x50, 0x1f0, 0x390, 0x1390, 0x2390, 0x4390, 0x6390, 0x63a0, 0x63b0, 0x63f0, 0x6430, 0x6830}, -/* length */ {0x1a0, 0x1a0, 0x1000, 0x1000, 0x2000, 0x2000, 0x10, 0x10, 0x40, 0x40, 0x400, 0x200}}; -/* 0. NVS */ -/* 1. NVS Shadow */ -/* 2. Wireless Conf */ -/* 3. Wireless Conf Shadow */ -/* 4. BT (WLAN driver) Patches */ -/* 5. WiLink (Firmware) Patches */ -/* 6. MAC addr */ -/* 7. Frontend Vars */ -/* 8. IP config */ -/* 9. IP config Shadow */ -/* 10. Bootloader Patches */ -/* 11. Radio Module params */ -/* 12. AES128 for smart config */ -/* 13. user file */ -/* 14. user file */ -/* 15. user file */ - -//***************************************************************************** -// -//! sendDriverPatch -//! -//! \param pointer to the length -//! -//! \return none -//! -//! \brief The function returns a pointer to the driver patch: -//! since there is no patch yet - it returns 0 -// -//***************************************************************************** - -static char *sendDriverPatch(unsigned long *Length) -{ - *Length = 0; - return NULL; -} - - -//***************************************************************************** -// -//! sendBootLoaderPatch -//! -//! \param pointer to the length -//! -//! \return none -//! -//! \brief The function returns a pointer to the boot loader patch: -//! since there is no patch yet - it returns 0 -// -//***************************************************************************** - -static char *sendBootLoaderPatch(unsigned long *Length) -{ - *Length = 0; - return NULL; -} - -//***************************************************************************** -// -//! sendWLFWPatch -//! -//! \param pointer to the length -//! -//! \return none -//! -//! \brief The function returns a pointer to the FW patch: -//! since there is no patch yet - it returns 0 -// -//***************************************************************************** - -static char *sendWLFWPatch(unsigned long *Length) -{ - *Length = 0; - return NULL; -} - -//***************************************************************************** -// -//! CC3000_UsynchCallback -//! -//! \param Event type -//! -//! \return none -//! -//! \brief The function handles asynchronous events that come from CC3000 -//! device and operates a LED4 to have an on-board indication -// -//***************************************************************************** - -static void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length) -{ - -} - -//***************************************************************************** -// -//! initDriver -//! -//! \param[in] cRequestPatch 0 to load with EEPROM patches -//! and 1 to load with no patches -//! -//! \return none -//! -//! \brief The function initializes a CC3000 device -//! and triggers it to start operation -// -//***************************************************************************** -static int initDriver(unsigned short cRequestPatch) -{ - // WLAN On API Implementation - wlan_init(CC3000_UsynchCallback, sendWLFWPatch, sendDriverPatch, sendBootLoaderPatch, - ReadWlanInterruptPin, SpiResumeSpi, SpiPauseSpi, WriteWlanPin); - - // Trigger a WLAN device - wlan_start(cRequestPatch); - wlan_smart_config_set_prefix((char*)aucCC3000_prefix); - wlan_ioctl_set_connection_policy(0, 0, 0); - wlan_ioctl_del_profile(255); - - // Mask out all non-required events from CC3000 - wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE| - HCI_EVNT_WLAN_UNSOL_INIT| - HCI_EVNT_WLAN_ASYNC_PING_REPORT); - - //unsolicicted_events_timer_init(); - systick_sleep(100); - return(0); -} - - -//***************************************************************************** -// -//! fat_read_content -//! -//! \param[out] is_allocated array of is_allocated in FAT table:\n -//! an allocated entry implies the address and length of the -//! file are valid. -//! 0: not allocated; 1: allocated. -//! \param[out] is_valid array of is_valid in FAT table:\n -//! a valid entry implies the content of the file is relevant. -//! 0: not valid; 1: valid. -//! \param[out] write_protected array of write_protected in FAT table:\n -//! a write protected entry implies it is not possible to write -//! into this entry. -//! 0: not protected; 1: protected. -//! \param[out] file_address array of file address in FAT table:\n -//! this is the absolute address of the file in the EEPROM. -//! \param[out] file_length array of file length in FAT table:\n -//! this is the upper limit of the file size in the EEPROM. -//! -//! \return on succes 0, error otherwise -//! -//! \brief parse the FAT table from eeprom -// -//***************************************************************************** -static unsigned char __attribute__ ((unused)) -fat_read_content(unsigned char *is_allocated, unsigned char *is_valid, - unsigned char *write_protected, unsigned short *file_address, unsigned short *file_length) -{ - unsigned short index; - unsigned char ucStatus; - unsigned char fatTable[48]; - unsigned char* fatTablePtr = fatTable; - - // - // Read in 6 parts to work with tiny driver - // - for (index = 0; index < 6; index++) - { - ucStatus = nvmem_read(16, 8, 4 + 8*index, fatTablePtr); - fatTablePtr += 8; - } - - fatTablePtr = fatTable; - - for (index = 0; index <= NVMEM_RM_FILEID; index++) - { - *is_allocated++ = (*fatTablePtr) & BIT0; - *is_valid++ = ((*fatTablePtr) & BIT1) >> 1; - *write_protected++ = ((*fatTablePtr) & BIT2) >> 2; - *file_address++ = ((*(fatTablePtr+1)<<8) | (*fatTablePtr)) & (BIT4|BIT5|BIT6|BIT7); - *file_length++ = ((*(fatTablePtr+3)<<8) | (*(fatTablePtr+2))) & (BIT4|BIT5|BIT6|BIT7); - - // - // Move to next file ID - // - fatTablePtr += 4; - } - - return ucStatus; -} - -//***************************************************************************** -// -//! fat_write_content -//! -//! \param[in] file_address array of file address in FAT table:\n -//! this is the absolute address of the file in the EEPROM. -//! \param[in] file_length array of file length in FAT table:\n -//! this is the upper limit of the file size in the EEPROM. -//! -//! \return on succes 0, error otherwise -//! -//! \brief parse the FAT table from eeprom -// -//***************************************************************************** -static unsigned char fat_write_content(unsigned short const *file_address, - unsigned short const *file_length) -{ - unsigned short index = 0; - unsigned char ucStatus; - unsigned char fatTable[48]; - unsigned char* fatTablePtr = fatTable; - - // - // First, write the magic number. - // - ucStatus = nvmem_write(16, 2, 0, (unsigned char*)"LS"); - - for (; index <= NVMEM_RM_FILEID; index++) - { - // - // Write address low char and mark as allocated. - // - *fatTablePtr++ = (unsigned char)(file_address[index] & 0xff) | BIT0; - - // - // Write address high char. - // - *fatTablePtr++ = (unsigned char)((file_address[index]>>8) & 0xff); - - // - // Write length low char. - // - *fatTablePtr++ = (unsigned char)(file_length[index] & 0xff); - - // - // Write length high char. - // - *fatTablePtr++ = (unsigned char)((file_length[index]>>8) & 0xff); - } - - // - // Second, write the FAT. - // Write in two parts to work with tiny driver. - // - ucStatus = nvmem_write(16, 24, 4, fatTable); - ucStatus = nvmem_write(16, 24, 24+4, &fatTable[24]); - - // - // Third, we want to erase any user files. - // - memset(fatTable, 0, sizeof(fatTable)); - ucStatus = nvmem_write(16, 16, 52, fatTable); - - return ucStatus; -} - -void patch_prog_start() -{ - unsigned short index; - unsigned char *pRMParams; - - printf("Initializing module...\n"); - - // Init module and request to load with no patches. - // This is in order to overwrite restrictions to - // write to specific places in EEPROM. - initDriver(1); - - // Read MAC address. - mac_status = nvmem_get_mac_address(cMacFromEeprom); - - return_status = 1; - - printf("Reading RM parameters...\n"); - while ((return_status) && (counter < 3)) { - // Read RM parameters. - // Read in 16 parts to work with tiny driver. - return_status = 0; - pRMParams = cRMParamsFromEeprom; - for (index = 0; index < 16; index++) { - return_status |= nvmem_read(NVMEM_RM_FILEID, 8, 8*index, pRMParams); - pRMParams += 8; - } - counter++; - } - - // If RM file is not valid, load the default one. - if (counter == 3) { - printf("RM is not valid, loading default one...\n"); - pRMParams = (unsigned char *)cRMdefaultParams; - } else { - printf("RM is valid.\n"); - pRMParams = cRMParamsFromEeprom; - } - - return_status = 1; - - printf("Writing new FAT\n"); - while (return_status) { - // Write new FAT. - return_status = fat_write_content(aFATEntries[0], aFATEntries[1]); - } - - return_status = 1; - - printf("Writing RM parameters...\n"); - while (return_status) { - // Write RM parameters. - // Write in 4 parts to work with tiny driver. - return_status = 0; - - for (index = 0; index < 4; index++) { - return_status |= nvmem_write(NVMEM_RM_FILEID, - 32, - 32*index, - (pRMParams + 32*index)); - } - } - - return_status = 1; - - // Write back the MAC address, only if exists. - if (mac_status == 0) { - // Zero out MCAST bit if set. - cMacFromEeprom[0] &= 0xfe; - printf("Writing back MAC address..\n"); - while (return_status) { - return_status = nvmem_set_mac_address(cMacFromEeprom); - } - } - - // Update driver - ucStatus_Dr = 1; - printf("Updating driver patch...\n"); - while (ucStatus_Dr) { - // Writing driver patch to EEPRROM - PROTABLE CODE - // Note that the array itself is changing between the - // different Service Packs. - ucStatus_Dr = nvmem_write_patch(NVMEM_WLAN_DRIVER_SP_FILEID, - drv_length, - wlan_drv_patch); - } - - // Update firmware - ucStatus_FW = 1; - printf("Updating firmware patch...\n"); - while (ucStatus_FW) { - // Writing FW patch to EEPRROM - PROTABLE CODE - // Note that the array itself is changing between the - // different Service Packs. - ucStatus_FW = nvmem_write_patch(NVMEM_WLAN_FW_SP_FILEID, - fw_length, - fw_patch); - } - - printf("Update complete, resetting module\n"\ - "If this doesn't work, reset manually...\n"); - - wlan_stop(); - systick_sleep(500); - - // Re-Init module and request to load with patches. - initDriver(0); - - // If MAC does not exist, it is recommended - // that the user will write a valid mac address. - if (mac_status != 0) { - printf("MAC address is not valid, please write a new one\n"); - } - - // Patch update done - printf("All done, call wlan.patch_version()\n"); -} diff --git a/drivers/cc3000/src/security.c b/drivers/cc3000/src/security.c deleted file mode 100644 index 62b4f881343ad..0000000000000 --- a/drivers/cc3000/src/security.c +++ /dev/null @@ -1,530 +0,0 @@ -/***************************************************************************** -* -* security.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -//***************************************************************************** -// -//! \addtogroup security_api -//! @{ -// -//***************************************************************************** - -#include "security.h" - -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG -// foreward sbox -const UINT8 sbox[256] = { -//0 1 2 3 4 5 6 7 8 9 A B C D E F -0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0 -0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1 -0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2 -0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3 -0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4 -0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5 -0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6 -0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7 -0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8 -0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9 -0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A -0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B -0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C -0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D -0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E -0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F -// inverse sbox -const UINT8 rsbox[256] = -{ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb -, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb -, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e -, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25 -, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92 -, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84 -, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06 -, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b -, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73 -, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e -, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b -, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4 -, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f -, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef -, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61 -, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; -// round constant -const UINT8 Rcon[11] = { - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36}; - - -UINT8 expandedKey[176]; - -//***************************************************************************** -// -//! expandKey -//! -//! @param key AES128 key - 16 bytes -//! @param expandedKey expanded AES128 key -//! -//! @return none -//! -//! @brief expend a 16 bytes key for AES128 implementation -//! -//***************************************************************************** - -void expandKey(UINT8 *expandedKey, UINT8 *key) -{ - UINT16 ii, buf1; - for (ii=0;ii<16;ii++) - expandedKey[ii] = key[ii]; - for (ii=1;ii<11;ii++){ - buf1 = expandedKey[ii*16 - 4]; - expandedKey[ii*16 + 0] = sbox[expandedKey[ii*16 - 3]]^expandedKey[(ii-1)*16 + 0]^Rcon[ii]; - expandedKey[ii*16 + 1] = sbox[expandedKey[ii*16 - 2]]^expandedKey[(ii-1)*16 + 1]; - expandedKey[ii*16 + 2] = sbox[expandedKey[ii*16 - 1]]^expandedKey[(ii-1)*16 + 2]; - expandedKey[ii*16 + 3] = sbox[buf1 ]^expandedKey[(ii-1)*16 + 3]; - expandedKey[ii*16 + 4] = expandedKey[(ii-1)*16 + 4]^expandedKey[ii*16 + 0]; - expandedKey[ii*16 + 5] = expandedKey[(ii-1)*16 + 5]^expandedKey[ii*16 + 1]; - expandedKey[ii*16 + 6] = expandedKey[(ii-1)*16 + 6]^expandedKey[ii*16 + 2]; - expandedKey[ii*16 + 7] = expandedKey[(ii-1)*16 + 7]^expandedKey[ii*16 + 3]; - expandedKey[ii*16 + 8] = expandedKey[(ii-1)*16 + 8]^expandedKey[ii*16 + 4]; - expandedKey[ii*16 + 9] = expandedKey[(ii-1)*16 + 9]^expandedKey[ii*16 + 5]; - expandedKey[ii*16 +10] = expandedKey[(ii-1)*16 +10]^expandedKey[ii*16 + 6]; - expandedKey[ii*16 +11] = expandedKey[(ii-1)*16 +11]^expandedKey[ii*16 + 7]; - expandedKey[ii*16 +12] = expandedKey[(ii-1)*16 +12]^expandedKey[ii*16 + 8]; - expandedKey[ii*16 +13] = expandedKey[(ii-1)*16 +13]^expandedKey[ii*16 + 9]; - expandedKey[ii*16 +14] = expandedKey[(ii-1)*16 +14]^expandedKey[ii*16 +10]; - expandedKey[ii*16 +15] = expandedKey[(ii-1)*16 +15]^expandedKey[ii*16 +11]; - } - -} - -//***************************************************************************** -// -//! galois_mul2 -//! -//! @param value argument to multiply -//! -//! @return multiplied argument -//! -//! @brief multiply by 2 in the galois field -//! -//***************************************************************************** - -UINT8 galois_mul2(UINT8 value) -{ - if (value>>7) - { - value = value << 1; - return (value^0x1b); - } else - return value<<1; -} - -//***************************************************************************** -// -//! aes_encr -//! -//! @param[in] expandedKey expanded AES128 key -//! @param[in/out] state 16 bytes of plain text and cipher text -//! -//! @return none -//! -//! @brief internal implementation of AES128 encryption. -//! straight forward aes encryption implementation -//! first the group of operations -//! - addRoundKey -//! - subbytes -//! - shiftrows -//! - mixcolums -//! is executed 9 times, after this addroundkey to finish the 9th -//! round, after that the 10th round without mixcolums -//! no further subfunctions to save cycles for function calls -//! no structuring with "for (....)" to save cycles. -//! -//! -//***************************************************************************** - -void aes_encr(UINT8 *state, UINT8 *expandedKey) -{ - UINT8 buf1, buf2, buf3, round; - - for (round = 0; round < 9; round ++){ - // addroundkey, sbox and shiftrows - // row 0 - state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])]; - state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])]; - state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])]; - state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])]; - // row 1 - buf1 = state[1] ^ expandedKey[(round*16) + 1]; - state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])]; - state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])]; - state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])]; - state[13] = sbox[buf1]; - // row 2 - buf1 = state[2] ^ expandedKey[(round*16) + 2]; - buf2 = state[6] ^ expandedKey[(round*16) + 6]; - state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])]; - state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])]; - state[10] = sbox[buf1]; - state[14] = sbox[buf2]; - // row 3 - buf1 = state[15] ^ expandedKey[(round*16) + 15]; - state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])]; - state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])]; - state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])]; - state[ 3] = sbox[buf1]; - - // mixcolums ////////// - // col1 - buf1 = state[0] ^ state[1] ^ state[2] ^ state[3]; - buf2 = state[0]; - buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1; - buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1; - buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1; - buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1; - // col2 - buf1 = state[4] ^ state[5] ^ state[6] ^ state[7]; - buf2 = state[4]; - buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1; - buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1; - buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1; - buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1; - // col3 - buf1 = state[8] ^ state[9] ^ state[10] ^ state[11]; - buf2 = state[8]; - buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1; - buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1; - buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1; - buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1; - // col4 - buf1 = state[12] ^ state[13] ^ state[14] ^ state[15]; - buf2 = state[12]; - buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1; - buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1; - buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1; - buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1; - - } - // 10th round without mixcols - state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])]; - state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])]; - state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])]; - state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])]; - // row 1 - buf1 = state[1] ^ expandedKey[(round*16) + 1]; - state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])]; - state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])]; - state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])]; - state[13] = sbox[buf1]; - // row 2 - buf1 = state[2] ^ expandedKey[(round*16) + 2]; - buf2 = state[6] ^ expandedKey[(round*16) + 6]; - state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])]; - state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])]; - state[10] = sbox[buf1]; - state[14] = sbox[buf2]; - // row 3 - buf1 = state[15] ^ expandedKey[(round*16) + 15]; - state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])]; - state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])]; - state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])]; - state[ 3] = sbox[buf1]; - // last addroundkey - state[ 0]^=expandedKey[160]; - state[ 1]^=expandedKey[161]; - state[ 2]^=expandedKey[162]; - state[ 3]^=expandedKey[163]; - state[ 4]^=expandedKey[164]; - state[ 5]^=expandedKey[165]; - state[ 6]^=expandedKey[166]; - state[ 7]^=expandedKey[167]; - state[ 8]^=expandedKey[168]; - state[ 9]^=expandedKey[169]; - state[10]^=expandedKey[170]; - state[11]^=expandedKey[171]; - state[12]^=expandedKey[172]; - state[13]^=expandedKey[173]; - state[14]^=expandedKey[174]; - state[15]^=expandedKey[175]; -} - -//***************************************************************************** -// -//! aes_decr -//! -//! @param[in] expandedKey expanded AES128 key -//! @param[in\out] state 16 bytes of cipher text and plain text -//! -//! @return none -//! -//! @brief internal implementation of AES128 decryption. -//! straight forward aes decryption implementation -//! the order of substeps is the exact reverse of decryption -//! inverse functions: -//! - addRoundKey is its own inverse -//! - rsbox is inverse of sbox -//! - rightshift instead of leftshift -//! - invMixColumns = barreto + mixColumns -//! no further subfunctions to save cycles for function calls -//! no structuring with "for (....)" to save cycles -//! -//***************************************************************************** - -void aes_decr(UINT8 *state, UINT8 *expandedKey) -{ - UINT8 buf1, buf2, buf3; - INT8 round; - round = 9; - - // initial addroundkey - state[ 0]^=expandedKey[160]; - state[ 1]^=expandedKey[161]; - state[ 2]^=expandedKey[162]; - state[ 3]^=expandedKey[163]; - state[ 4]^=expandedKey[164]; - state[ 5]^=expandedKey[165]; - state[ 6]^=expandedKey[166]; - state[ 7]^=expandedKey[167]; - state[ 8]^=expandedKey[168]; - state[ 9]^=expandedKey[169]; - state[10]^=expandedKey[170]; - state[11]^=expandedKey[171]; - state[12]^=expandedKey[172]; - state[13]^=expandedKey[173]; - state[14]^=expandedKey[174]; - state[15]^=expandedKey[175]; - - // 10th round without mixcols - state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ]; - state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4]; - state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8]; - state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12]; - // row 1 - buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1]; - state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13]; - state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9]; - state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5]; - state[ 1] = buf1; - // row 2 - buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10]; - buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14]; - state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2]; - state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6]; - state[10] = buf1; - state[14] = buf2; - // row 3 - buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15]; - state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3]; - state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7]; - state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11]; - state[15] = buf1; - - for (round = 8; round >= 0; round--){ - // barreto - //col1 - buf1 = galois_mul2(galois_mul2(state[0]^state[2])); - buf2 = galois_mul2(galois_mul2(state[1]^state[3])); - state[0] ^= buf1; state[1] ^= buf2; state[2] ^= buf1; state[3] ^= buf2; - //col2 - buf1 = galois_mul2(galois_mul2(state[4]^state[6])); - buf2 = galois_mul2(galois_mul2(state[5]^state[7])); - state[4] ^= buf1; state[5] ^= buf2; state[6] ^= buf1; state[7] ^= buf2; - //col3 - buf1 = galois_mul2(galois_mul2(state[8]^state[10])); - buf2 = galois_mul2(galois_mul2(state[9]^state[11])); - state[8] ^= buf1; state[9] ^= buf2; state[10] ^= buf1; state[11] ^= buf2; - //col4 - buf1 = galois_mul2(galois_mul2(state[12]^state[14])); - buf2 = galois_mul2(galois_mul2(state[13]^state[15])); - state[12] ^= buf1; state[13] ^= buf2; state[14] ^= buf1; state[15] ^= buf2; - // mixcolums ////////// - // col1 - buf1 = state[0] ^ state[1] ^ state[2] ^ state[3]; - buf2 = state[0]; - buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1; - buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1; - buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1; - buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1; - // col2 - buf1 = state[4] ^ state[5] ^ state[6] ^ state[7]; - buf2 = state[4]; - buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1; - buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1; - buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1; - buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1; - // col3 - buf1 = state[8] ^ state[9] ^ state[10] ^ state[11]; - buf2 = state[8]; - buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1; - buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1; - buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1; - buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1; - // col4 - buf1 = state[12] ^ state[13] ^ state[14] ^ state[15]; - buf2 = state[12]; - buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1; - buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1; - buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1; - buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1; - - // addroundkey, rsbox and shiftrows - // row 0 - state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ]; - state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4]; - state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8]; - state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12]; - // row 1 - buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1]; - state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13]; - state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9]; - state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5]; - state[ 1] = buf1; - // row 2 - buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10]; - buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14]; - state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2]; - state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6]; - state[10] = buf1; - state[14] = buf2; - // row 3 - buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15]; - state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3]; - state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7]; - state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11]; - state[15] = buf1; - } - -} - -//***************************************************************************** -// -//! aes_encrypt -//! -//! @param[in] key AES128 key of size 16 bytes -//! @param[in\out] state 16 bytes of plain text and cipher text -//! -//! @return none -//! -//! @brief AES128 encryption: -//! Given AES128 key and 16 bytes plain text, cipher text of 16 bytes -//! is computed. The AES implementation is in mode ECB (Electronic -//! Code Book). -//! -//! -//***************************************************************************** - -void aes_encrypt(UINT8 *state, UINT8 *key) -{ - // expand the key into 176 bytes - expandKey(expandedKey, key); - aes_encr(state, expandedKey); -} - -//***************************************************************************** -// -//! aes_decrypt -//! -//! @param[in] key AES128 key of size 16 bytes -//! @param[in\out] state 16 bytes of cipher text and plain text -//! -//! @return none -//! -//! @brief AES128 decryption: -//! Given AES128 key and 16 bytes cipher text, plain text of 16 bytes -//! is computed The AES implementation is in mode ECB -//! (Electronic Code Book). -//! -//! -//***************************************************************************** - -void aes_decrypt(UINT8 *state, UINT8 *key) -{ - expandKey(expandedKey, key); // expand the key into 176 bytes - aes_decr(state, expandedKey); -} - -//***************************************************************************** -// -//! aes_read_key -//! -//! @param[out] key AES128 key of size 16 bytes -//! -//! @return on success 0, error otherwise. -//! -//! @brief Reads AES128 key from EEPROM -//! Reads the AES128 key from fileID #12 in EEPROM -//! returns an error if the key does not exist. -//! -//! -//***************************************************************************** - -INT32 aes_read_key(UINT8 *key) -{ - INT32 returnValue; - - returnValue = nvmem_read(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); - - return returnValue; -} - -//***************************************************************************** -// -//! aes_write_key -//! -//! @param[out] key AES128 key of size 16 bytes -//! -//! @return on success 0, error otherwise. -//! -//! @brief writes AES128 key from EEPROM -//! Writes the AES128 key to fileID #12 in EEPROM -//! -//! -//***************************************************************************** - -INT32 aes_write_key(UINT8 *key) -{ - INT32 returnValue; - - returnValue = nvmem_write(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); - - return returnValue; -} - -#endif //CC3000_UNENCRYPTED_SMART_CONFIG - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** diff --git a/drivers/cc3000/src/socket.c b/drivers/cc3000/src/socket.c deleted file mode 100644 index ddd7e56e80759..0000000000000 --- a/drivers/cc3000/src/socket.c +++ /dev/null @@ -1,1182 +0,0 @@ -/***************************************************************************** -* -* socket.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -//***************************************************************************** -// -//! \addtogroup socket_api -//! @{ -// -//***************************************************************************** - -#include -#include -#include "hci.h" -#include "socket.h" -#include "evnt_handler.h" -#include "netapp.h" - - - -//Enable this flag if and only if you must comply with BSD socket -//close() function -#ifdef _API_USE_BSD_CLOSE -#define close(sd) closesocket(sd) -#endif - -//Enable this flag if and only if you must comply with BSD socket read() and -//write() functions -#ifdef _API_USE_BSD_READ_WRITE -#define read(sd, buf, len, flags) recv(sd, buf, len, flags) -#define write(sd, buf, len, flags) send(sd, buf, len, flags) -#endif - -#define SOCKET_OPEN_PARAMS_LEN (12) -#define SOCKET_CLOSE_PARAMS_LEN (4) -#define SOCKET_ACCEPT_PARAMS_LEN (4) -#define SOCKET_BIND_PARAMS_LEN (20) -#define SOCKET_LISTEN_PARAMS_LEN (8) -#define SOCKET_GET_HOST_BY_NAME_PARAMS_LEN (9) -#define SOCKET_CONNECT_PARAMS_LEN (20) -#define SOCKET_SELECT_PARAMS_LEN (44) -#define SOCKET_SET_SOCK_OPT_PARAMS_LEN (20) -#define SOCKET_GET_SOCK_OPT_PARAMS_LEN (12) -#define SOCKET_RECV_FROM_PARAMS_LEN (12) -#define SOCKET_SENDTO_PARAMS_LEN (24) -#define SOCKET_MDNS_ADVERTISE_PARAMS_LEN (12) -#define SOCKET_GET_MSS_VALUE_PARAMS_LEN (4) - -// The legnth of arguments for the SEND command: sd + buff_offset + len + flags, -// while size of each parameter is 32 bit - so the total length is 16 bytes; - -#define HCI_CMND_SEND_ARG_LENGTH (16) - - -#define SELECT_TIMEOUT_MIN_MICRO_SECONDS 5000 - -#define HEADERS_SIZE_DATA (SPI_HEADER_SIZE + 5) - -#define SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE) - -#define MDNS_DEVICE_SERVICE_MAX_LENGTH (32) - - -//***************************************************************************** -// -//! HostFlowControlConsumeBuff -//! -//! @param sd socket descriptor -//! -//! @return 0 in case there are buffers available, -//! -1 in case of bad socket -//! -2 if there are no free buffers present (only when -//! SEND_NON_BLOCKING is enabled) -//! -//! @brief if SEND_NON_BLOCKING not define - block until have free buffer -//! becomes available, else return immediately with correct status -//! regarding the buffers available. -// -//***************************************************************************** -static INT16 HostFlowControlConsumeBuff(INT16 sd) -{ -#ifndef SEND_NON_BLOCKING - /* wait in busy loop */ - do - { - // In case last transmission failed then we will return the last failure - // reason here. - // Note that the buffer will not be allocated in this case - if (tSLInformation.slTransmitDataError != 0) - { - CC3000_EXPORT(errno) = tSLInformation.slTransmitDataError; - tSLInformation.slTransmitDataError = 0; - return CC3000_EXPORT(errno); - } - - if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd)) - return -1; - } while(0 == tSLInformation.usNumberOfFreeBuffers); - - tSLInformation.usNumberOfFreeBuffers--; - - return 0; -#else - - // In case last transmission failed then we will return the last failure - // reason here. - // Note that the buffer will not be allocated in this case - if (tSLInformation.slTransmitDataError != 0) - { - CC3000_EXPORT(errno) = tSLInformation.slTransmitDataError; - tSLInformation.slTransmitDataError = 0; - return CC3000_EXPORT(errno); - } - if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd)) - return -1; - - //If there are no available buffers, return -2. It is recommended to use - // select or receive to see if there is any buffer occupied with received data - // If so, call receive() to release the buffer. - if(0 == tSLInformation.usNumberOfFreeBuffers) - { - return -2; - } - else - { - tSLInformation.usNumberOfFreeBuffers--; - return 0; - } -#endif -} - -//***************************************************************************** -// -//! socket -//! -//! @param domain selects the protocol family which will be used for -//! communication. On this version only AF_INET is supported -//! @param type specifies the communication semantics. On this version -//! only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW are supported -//! @param protocol specifies a particular protocol to be used with the -//! socket IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW are -//! supported. -//! -//! @return On success, socket handle that is used for consequent socket -//! operations. On error, -1 is returned. -//! -//! @brief create an endpoint for communication -//! The socket function creates a socket that is bound to a specific -//! transport service provider. This function is called by the -//! application layer to obtain a socket handle. -// -//***************************************************************************** - -INT16 CC3000_EXPORT(socket)(INT32 domain, INT32 type, INT32 protocol) -{ - INT32 ret; - UINT8 *ptr, *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, domain); - args = UINT32_TO_STREAM(args, type); - args = UINT32_TO_STREAM(args, protocol); - - // Initiate a HCI command - hci_command_send(HCI_CMND_SOCKET, ptr, SOCKET_OPEN_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_SOCKET, &ret); - - // Process the event - CC3000_EXPORT(errno) = ret; - - set_socket_active_status(ret, SOCKET_STATUS_ACTIVE); - - return(ret); -} - -//***************************************************************************** -// -//! closesocket -//! -//! @param sd socket handle. -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief The socket function closes a created socket. -// -//***************************************************************************** - -INT32 CC3000_EXPORT(closesocket)(INT32 sd) -{ - INT32 ret; - UINT8 *ptr, *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, sd); - - // Initiate a HCI command - hci_command_send(HCI_CMND_CLOSE_SOCKET, - ptr, SOCKET_CLOSE_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_CLOSE_SOCKET, &ret); - CC3000_EXPORT(errno) = ret; - - // since 'close' call may result in either OK (and then it closed) or error - // mark this socket as invalid - set_socket_active_status(sd, SOCKET_STATUS_INACTIVE); - - return(ret); -} - -//***************************************************************************** -// -//! accept -//! -//! @param[in] sd socket descriptor (handle) -//! @param[out] addr the argument addr is a pointer to a sockaddr structure -//! This structure is filled in with the address of the -//! peer socket, as known to the communications layer. -//! determined. The exact format of the address returned -//! addr is by the socket's address sockaddr. -//! On this version only AF_INET is supported. -//! This argument returns in network order. -//! @param[out] addrlen the addrlen argument is a value-result argument: -//! it should initially contain the size of the structure -//! pointed to by addr. -//! -//! @return For socket in blocking mode: -//! On success, socket handle. on failure negative -//! For socket in non-blocking mode: -//! - On connection establishment, socket handle -//! - On connection pending, SOC_IN_PROGRESS (-2) -//! - On failure, SOC_ERROR (-1) -//! -//! @brief accept a connection on a socket: -//! This function is used with connection-based socket types -//! (SOCK_STREAM). It extracts the first connection request on the -//! queue of pending connections, creates a new connected socket, and -//! returns a new file descriptor referring to that socket. -//! The newly created socket is not in the listening state. -//! The original socket sd is unaffected by this call. -//! The argument sd is a socket that has been created with socket(), -//! bound to a local address with bind(), and is listening for -//! connections after a listen(). The argument addr is a pointer -//! to a sockaddr structure. This structure is filled in with the -//! address of the peer socket, as known to the communications layer. -//! The exact format of the address returned addr is determined by the -//! socket's address family. The addrlen argument is a value-result -//! argument: it should initially contain the size of the structure -//! pointed to by addr, on return it will contain the actual -//! length (in bytes) of the address returned. -//! -//! @sa socket ; bind ; listen -// -//***************************************************************************** - -INT32 CC3000_EXPORT(accept)(INT32 sd, sockaddr *addr, socklen_t *addrlen) -{ - INT32 ret; - UINT8 *ptr, *args; - tBsdReturnParams tAcceptReturnArguments; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - - // Initiate a HCI command - hci_command_send(HCI_CMND_ACCEPT, - ptr, SOCKET_ACCEPT_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_ACCEPT, &tAcceptReturnArguments); - - - // need specify return parameters!!! - memcpy(addr, &tAcceptReturnArguments.tSocketAddress, ASIC_ADDR_LEN); - *addrlen = ASIC_ADDR_LEN; - CC3000_EXPORT(errno) = tAcceptReturnArguments.iStatus; - ret = CC3000_EXPORT(errno); - - // if succeeded, iStatus = new socket descriptor. otherwise - error number - if(M_IS_VALID_SD(ret)) - { - set_socket_active_status(ret, SOCKET_STATUS_ACTIVE); - } - else - { - set_socket_active_status(sd, SOCKET_STATUS_INACTIVE); - } - - return(ret); -} - -//***************************************************************************** -// -//! bind -//! -//! @param[in] sd socket descriptor (handle) -//! @param[out] addr specifies the destination address. On this version -//! only AF_INET is supported. -//! @param[out] addrlen contains the size of the structure pointed to by addr. -//! -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief assign a name to a socket -//! This function gives the socket the local address addr. -//! addr is addrlen bytes long. Traditionally, this is called when a -//! socket is created with socket, it exists in a name space (address -//! family) but has no name assigned. -//! It is necessary to assign a local address before a SOCK_STREAM -//! socket may receive connections. -//! -//! @sa socket ; accept ; listen -// -//***************************************************************************** - -INT32 CC3000_EXPORT(bind)(INT32 sd, const sockaddr *addr, INT32 addrlen) -{ - INT32 ret; - UINT8 *ptr, *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - addrlen = ASIC_ADDR_LEN; - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, 0x00000008); - args = UINT32_TO_STREAM(args, addrlen); - ARRAY_TO_STREAM(args, ((UINT8 *)addr), addrlen); - - // Initiate a HCI command - hci_command_send(HCI_CMND_BIND, - ptr, SOCKET_BIND_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_BIND, &ret); - - CC3000_EXPORT(errno) = ret; - - return(ret); -} - -//***************************************************************************** -// -//! listen -//! -//! @param[in] sd socket descriptor (handle) -//! @param[in] backlog specifies the listen queue depth. On this version -//! backlog is not supported. -//! @return On success, zero is returned. On error, -1 is returned. -//! -//! @brief listen for connections on a socket -//! The willingness to accept incoming connections and a queue -//! limit for incoming connections are specified with listen(), -//! and then the connections are accepted with accept. -//! The listen() call applies only to sockets of type SOCK_STREAM -//! The backlog parameter defines the maximum length the queue of -//! pending connections may grow to. -//! -//! @sa socket ; accept ; bind -//! -//! @note On this version, backlog is not supported -// -//***************************************************************************** - -INT32 CC3000_EXPORT(listen)(INT32 sd, INT32 backlog) -{ - INT32 ret; - UINT8 *ptr, *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, backlog); - - // Initiate a HCI command - hci_command_send(HCI_CMND_LISTEN, - ptr, SOCKET_LISTEN_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_LISTEN, &ret); - CC3000_EXPORT(errno) = ret; - - return(ret); -} - -//***************************************************************************** -// -//! gethostbyname -//! -//! @param[in] hostname host name -//! @param[in] usNameLen name length -//! @param[out] out_ip_addr This parameter is filled in with host IP address. -//! In case that host name is not resolved, -//! out_ip_addr is zero. -//! @return On success, positive is returned. On error, negative is returned -//! -//! @brief Get host IP by name. Obtain the IP Address of machine on network, -//! by its name. -//! -//! @note On this version, only blocking mode is supported. Also note that -//! the function requires DNS server to be configured prior to its usage. -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT16 CC3000_EXPORT(gethostbyname)(CHAR * hostname, UINT16 usNameLen, - UINT32* out_ip_addr) -{ - tBsdGethostbynameParams ret; - UINT8 *ptr, *args; - - CC3000_EXPORT(errno) = EFAIL; - - if (usNameLen > HOSTNAME_MAX_LENGTH) - { - return CC3000_EXPORT(errno); - } - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, 8); - args = UINT32_TO_STREAM(args, usNameLen); - ARRAY_TO_STREAM(args, hostname, usNameLen); - - // Initiate a HCI command - hci_command_send(HCI_CMND_GETHOSTNAME, ptr, SOCKET_GET_HOST_BY_NAME_PARAMS_LEN - + usNameLen - 1); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_EVNT_BSD_GETHOSTBYNAME, &ret); - - CC3000_EXPORT(errno) = ret.retVal; - - (*((INT32*)out_ip_addr)) = ret.outputAddress; - - return CC3000_EXPORT(errno); - -} -#endif - -//***************************************************************************** -// -//! connect -//! -//! @param[in] sd socket descriptor (handle) -//! @param[in] addr specifies the destination addr. On this version -//! only AF_INET is supported. -//! @param[out] addrlen contains the size of the structure pointed to by addr -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief initiate a connection on a socket -//! Function connects the socket referred to by the socket descriptor -//! sd, to the address specified by addr. The addrlen argument -//! specifies the size of addr. The format of the address in addr is -//! determined by the address space of the socket. If it is of type -//! SOCK_DGRAM, this call specifies the peer with which the socket is -//! to be associated; this address is that to which datagrams are to be -//! sent, and the only address from which datagrams are to be received. -//! If the socket is of type SOCK_STREAM, this call attempts to make a -//! connection to another socket. The other socket is specified by -//! address, which is an address in the communications space of the -//! socket. Note that the function implements only blocking behavior -//! thus the caller will be waiting either for the connection -//! establishment or for the connection establishment failure. -//! -//! @sa socket -// -//***************************************************************************** - -INT32 CC3000_EXPORT(connect)(INT32 sd, const sockaddr *addr, INT32 addrlen) -{ - INT32 ret; - UINT8 *ptr, *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); - addrlen = 8; - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, 0x00000008); - args = UINT32_TO_STREAM(args, addrlen); - ARRAY_TO_STREAM(args, ((UINT8 *)addr), addrlen); - - // Initiate a HCI command - hci_command_send(HCI_CMND_CONNECT, - ptr, SOCKET_CONNECT_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_CONNECT, &ret); - - CC3000_EXPORT(errno) = ret; - - return((INT32)ret); -} - - -//***************************************************************************** -// -//! select -//! -//! @param[in] nfds the highest-numbered file descriptor in any of the -//! three sets, plus 1. -//! @param[out] writesds socket descriptors list for write monitoring -//! @param[out] readsds socket descriptors list for read monitoring -//! @param[out] exceptsds socket descriptors list for exception monitoring -//! @param[in] timeout is an upper bound on the amount of time elapsed -//! before select() returns. Null means infinity -//! timeout. The minimum timeout is 5 milliseconds, -//! less than 5 milliseconds will be set -//! automatically to 5 milliseconds. -//! @return On success, select() returns the number of file descriptors -//! contained in the three returned descriptor sets (that is, the -//! total number of bits that are set in readfds, writefds, -//! exceptfds) which may be zero if the timeout expires before -//! anything interesting happens. -//! On error, -1 is returned. -//! *readsds - return the sockets on which Read request will -//! return without delay with valid data. -//! *writesds - return the sockets on which Write request -//! will return without delay. -//! *exceptsds - return the sockets which closed recently. -//! -//! @brief Monitor socket activity -//! Select allow a program to monitor multiple file descriptors, -//! waiting until one or more of the file descriptors become -//! "ready" for some class of I/O operation -//! -//! @Note If the timeout value set to less than 5ms it will automatically set -//! to 5ms to prevent overload of the system -//! -//! @sa socket -// -//***************************************************************************** - -INT16 CC3000_EXPORT(select)(INT32 nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, -struct cc3000_timeval *timeout) -{ - UINT8 *ptr, *args; - tBsdSelectRecvParams tParams; - UINT32 is_blocking; - - if( timeout == NULL) - { - is_blocking = 1; /* blocking , infinity timeout */ - } - else - { - is_blocking = 0; /* no blocking, timeout */ - } - - // Fill in HCI packet structure - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, nfds); - args = UINT32_TO_STREAM(args, 0x00000014); - args = UINT32_TO_STREAM(args, 0x00000014); - args = UINT32_TO_STREAM(args, 0x00000014); - args = UINT32_TO_STREAM(args, 0x00000014); - args = UINT32_TO_STREAM(args, is_blocking); - args = UINT32_TO_STREAM(args, ((readsds) ? *(UINT32*)readsds : 0)); - args = UINT32_TO_STREAM(args, ((writesds) ? *(UINT32*)writesds : 0)); - args = UINT32_TO_STREAM(args, ((exceptsds) ? *(UINT32*)exceptsds : 0)); - - if (timeout) - { - if ( 0 == timeout->tv_sec && timeout->tv_usec < - SELECT_TIMEOUT_MIN_MICRO_SECONDS) - { - timeout->tv_usec = SELECT_TIMEOUT_MIN_MICRO_SECONDS; - } - args = UINT32_TO_STREAM(args, timeout->tv_sec); - args = UINT32_TO_STREAM(args, timeout->tv_usec); - } - - // Initiate a HCI command - hci_command_send(HCI_CMND_BSD_SELECT, ptr, SOCKET_SELECT_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_EVNT_SELECT, &tParams); - - // Update actually read FD - if (tParams.iStatus >= 0) - { - if (readsds) - { - memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd)); - } - - if (writesds) - { - memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd)); - } - - if (exceptsds) - { - memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd)); - } - - return(tParams.iStatus); - - } - else - { - CC3000_EXPORT(errno) = tParams.iStatus; - return(-1); - } -} - -//***************************************************************************** -// -//! setsockopt -//! -//! @param[in] sd socket handle -//! @param[in] level defines the protocol level for this option -//! @param[in] optname defines the option name to Interrogate -//! @param[in] optval specifies a value for the option -//! @param[in] optlen specifies the length of the option value -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief set socket options -//! This function manipulate the options associated with a socket. -//! Options may exist at multiple protocol levels; they are always -//! present at the uppermost socket level. -//! When manipulating socket options the level at which the option -//! resides and the name of the option must be specified. -//! To manipulate options at the socket level, level is specified as -//! SOL_SOCKET. To manipulate options at any other level the protocol -//! number of the appropriate protocol controlling the option is -//! supplied. For example, to indicate that an option is to be -//! interpreted by the TCP protocol, level should be set to the -//! protocol number of TCP; -//! The parameters optval and optlen are used to access optval - -//! use for setsockopt(). For getsockopt() they identify a buffer -//! in which the value for the requested option(s) are to -//! be returned. For getsockopt(), optlen is a value-result -//! parameter, initially containing the size of the buffer -//! pointed to by option_value, and modified on return to -//! indicate the actual size of the value returned. If no option -//! value is to be supplied or returned, option_value may be NULL. -//! -//! @Note On this version the following two socket options are enabled: -//! The only protocol level supported in this version -//! is SOL_SOCKET (level). -//! 1. SOCKOPT_RECV_TIMEOUT (optname) -//! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout -//! in milliseconds. -//! In that case optval should be pointer to UINT32. -//! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on -//! or off. -//! In that case optval should be SOCK_ON or SOCK_OFF (optval). -//! -//! @sa getsockopt -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT16 CC3000_EXPORT(setsockopt)(INT32 sd, INT32 level, INT32 optname, const void *optval, - socklen_t optlen) -{ - INT32 ret; - UINT8 *ptr, *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, level); - args = UINT32_TO_STREAM(args, optname); - args = UINT32_TO_STREAM(args, 0x00000008); - args = UINT32_TO_STREAM(args, optlen); - ARRAY_TO_STREAM(args, ((UINT8 *)optval), optlen); - - // Initiate a HCI command - hci_command_send(HCI_CMND_SETSOCKOPT, - ptr, SOCKET_SET_SOCK_OPT_PARAMS_LEN + optlen); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_SETSOCKOPT, &ret); - - if (ret >= 0) - { - return (0); - } - else - { - CC3000_EXPORT(errno) = ret; - return ret; - } -} -#endif - -//***************************************************************************** -// -//! getsockopt -//! -//! @param[in] sd socket handle -//! @param[in] level defines the protocol level for this option -//! @param[in] optname defines the option name to Interrogate -//! @param[out] optval specifies a value for the option -//! @param[out] optlen specifies the length of the option value -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief set socket options -//! This function manipulate the options associated with a socket. -//! Options may exist at multiple protocol levels; they are always -//! present at the uppermost socket level. -//! When manipulating socket options the level at which the option -//! resides and the name of the option must be specified. -//! To manipulate options at the socket level, level is specified as -//! SOL_SOCKET. To manipulate options at any other level the protocol -//! number of the appropriate protocol controlling the option is -//! supplied. For example, to indicate that an option is to be -//! interpreted by the TCP protocol, level should be set to the -//! protocol number of TCP; -//! The parameters optval and optlen are used to access optval - -//! use for setsockopt(). For getsockopt() they identify a buffer -//! in which the value for the requested option(s) are to -//! be returned. For getsockopt(), optlen is a value-result -//! parameter, initially containing the size of the buffer -//! pointed to by option_value, and modified on return to -//! indicate the actual size of the value returned. If no option -//! value is to be supplied or returned, option_value may be NULL. -//! -//! @Note On this version the following two socket options are enabled: -//! The only protocol level supported in this version -//! is SOL_SOCKET (level). -//! 1. SOCKOPT_RECV_TIMEOUT (optname) -//! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout -//! in milliseconds. -//! In that case optval should be pointer to UINT32. -//! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on -//! or off. -//! In that case optval should be SOCK_ON or SOCK_OFF (optval). -//! -//! @sa setsockopt -// -//***************************************************************************** - -INT16 CC3000_EXPORT(getsockopt) (INT32 sd, INT32 level, INT32 optname, void *optval, socklen_t *optlen) -{ - UINT8 *ptr, *args; - tBsdGetSockOptReturnParams tRetParams; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, level); - args = UINT32_TO_STREAM(args, optname); - - // Initiate a HCI command - hci_command_send(HCI_CMND_GETSOCKOPT, - ptr, SOCKET_GET_SOCK_OPT_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_CMND_GETSOCKOPT, &tRetParams); - - if (((INT8)tRetParams.iStatus) >= 0) - { - *optlen = 4; - memcpy(optval, tRetParams.ucOptValue, 4); - return (0); - } - else - { - CC3000_EXPORT(errno) = tRetParams.iStatus; - return CC3000_EXPORT(errno); - } -} - -//***************************************************************************** -// -//! simple_link_recv -//! -//! @param sd socket handle -//! @param buf read buffer -//! @param len buffer length -//! @param flags indicates blocking or non-blocking operation -//! @param from pointer to an address structure indicating source address -//! @param fromlen source address structure size -//! -//! @return Return the number of bytes received, or -1 if an error -//! occurred -//! -//! @brief Read data from socket -//! Return the length of the message on successful completion. -//! If a message is too long to fit in the supplied buffer, -//! excess bytes may be discarded depending on the type of -//! socket the message is received from -// -//***************************************************************************** -static INT16 simple_link_recv(INT32 sd, void *buf, INT32 len, INT32 flags, sockaddr *from, - socklen_t *fromlen, INT32 opcode) -{ - UINT8 *ptr, *args; - tBsdReadReturnParams tSocketReadEvent; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, len); - args = UINT32_TO_STREAM(args, flags); - - // Generate the read command, and wait for the - hci_command_send(opcode, ptr, SOCKET_RECV_FROM_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(opcode, &tSocketReadEvent); - - // In case the number of bytes is more then zero - read data - if (tSocketReadEvent.iNumberOfBytes > 0) - { - // Wait for the data in a synchronous way. Here we assume that the bug is - // big enough to store also parameters of receive from too.... - SimpleLinkWaitData(buf, (UINT8 *)from, (UINT8 *)fromlen); - } - - CC3000_EXPORT(errno) = tSocketReadEvent.iNumberOfBytes; - - return(tSocketReadEvent.iNumberOfBytes); -} - -//***************************************************************************** -// -//! recv -//! -//! @param[in] sd socket handle -//! @param[out] buf Points to the buffer where the message should be stored -//! @param[in] len Specifies the length in bytes of the buffer pointed to -//! by the buffer argument. -//! @param[in] flags Specifies the type of message reception. -//! On this version, this parameter is not supported. -//! -//! @return Return the number of bytes received, or -1 if an error -//! occurred -//! -//! @brief function receives a message from a connection-mode socket -//! -//! @sa recvfrom -//! -//! @Note On this version, only blocking mode is supported. -// -//***************************************************************************** - -INT16 CC3000_EXPORT(recv)(INT32 sd, void *buf, INT32 len, INT32 flags) -{ - return(simple_link_recv(sd, buf, len, flags, NULL, NULL, HCI_CMND_RECV)); -} - -//***************************************************************************** -// -//! recvfrom -//! -//! @param[in] sd socket handle -//! @param[out] buf Points to the buffer where the message should be stored -//! @param[in] len Specifies the length in bytes of the buffer pointed to -//! by the buffer argument. -//! @param[in] flags Specifies the type of message reception. -//! On this version, this parameter is not supported. -//! @param[in] from pointer to an address structure indicating the source -//! address: sockaddr. On this version only AF_INET is -//! supported. -//! @param[in] fromlen source address tructure size -//! -//! @return Return the number of bytes received, or -1 if an error -//! occurred -//! -//! @brief read data from socket -//! function receives a message from a connection-mode or -//! connectionless-mode socket. Note that raw sockets are not -//! supported. -//! -//! @sa recv -//! -//! @Note On this version, only blocking mode is supported. -// -//***************************************************************************** -INT16 CC3000_EXPORT(recvfrom)(INT32 sd, void *buf, INT32 len, INT32 flags, sockaddr *from, - socklen_t *fromlen) -{ - return(simple_link_recv(sd, buf, len, flags, from, fromlen, - HCI_CMND_RECVFROM)); -} - -//***************************************************************************** -// -//! simple_link_send -//! -//! @param sd socket handle -//! @param buf write buffer -//! @param len buffer length -//! @param flags On this version, this parameter is not supported -//! @param to pointer to an address structure indicating destination -//! address -//! @param tolen destination address structure size -//! -//! @return Return the number of bytes transmitted, or -1 if an error -//! occurred, or -2 in case there are no free buffers available -//! (only when SEND_NON_BLOCKING is enabled) -//! -//! @brief This function is used to transmit a message to another -//! socket -// -//***************************************************************************** -static INT16 simple_link_send(INT32 sd, const void *buf, INT32 len, INT32 flags, - const sockaddr *to, INT32 tolen, INT32 opcode) -{ - UINT8 uArgSize=0, addrlen; - UINT8 *ptr, *pDataPtr=0, *args; - UINT32 addr_offset=0; - INT16 res; - tBsdReadReturnParams tSocketSendEvent; - - // Check the bsd_arguments - if (0 != (res = HostFlowControlConsumeBuff(sd))) - { - return res; - } - - //Update the number of sent packets - tSLInformation.NumberOfSentPackets++; - - // Allocate a buffer and construct a packet and send it over spi - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_DATA); - - // Update the offset of data and parameters according to the command - switch(opcode) - { - case HCI_CMND_SENDTO: - { - addr_offset = len + sizeof(len) + sizeof(len); - addrlen = 8; - uArgSize = SOCKET_SENDTO_PARAMS_LEN; - pDataPtr = ptr + HEADERS_SIZE_DATA + SOCKET_SENDTO_PARAMS_LEN; - break; - } - - case HCI_CMND_SEND: - { - tolen = 0; - to = NULL; - uArgSize = HCI_CMND_SEND_ARG_LENGTH; - pDataPtr = ptr + HEADERS_SIZE_DATA + HCI_CMND_SEND_ARG_LENGTH; - break; - } - - default: - { - break; - } - } - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - args = UINT32_TO_STREAM(args, uArgSize - sizeof(sd)); - args = UINT32_TO_STREAM(args, len); - args = UINT32_TO_STREAM(args, flags); - - if (opcode == HCI_CMND_SENDTO) - { - args = UINT32_TO_STREAM(args, addr_offset); - args = UINT32_TO_STREAM(args, addrlen); - } - - // Copy the data received from user into the TX Buffer - ARRAY_TO_STREAM(pDataPtr, ((UINT8 *)buf), len); - - // In case we are using SendTo, copy the to parameters - if (opcode == HCI_CMND_SENDTO) - { - ARRAY_TO_STREAM(pDataPtr, ((UINT8 *)to), tolen); - } - - // Initiate a HCI command - hci_data_send(opcode, ptr, uArgSize, len,(UINT8*)to, tolen); - - if (opcode == HCI_CMND_SENDTO) - SimpleLinkWaitEvent(HCI_EVNT_SENDTO, &tSocketSendEvent); - else - SimpleLinkWaitEvent(HCI_EVNT_SEND, &tSocketSendEvent); - - return (len); -} - - -//***************************************************************************** -// -//! send -//! -//! @param sd socket handle -//! @param buf Points to a buffer containing the message to be sent -//! @param len message size in bytes -//! @param flags On this version, this parameter is not supported -//! -//! @return Return the number of bytes transmitted, or -1 if an -//! error occurred -//! -//! @brief Write data to TCP socket -//! This function is used to transmit a message to another -//! socket. -//! -//! @Note On this version, only blocking mode is supported. -//! -//! @sa sendto -// -//***************************************************************************** - -INT16 CC3000_EXPORT(send)(INT32 sd, const void *buf, INT32 len, INT32 flags) -{ - return(simple_link_send(sd, buf, len, flags, NULL, 0, HCI_CMND_SEND)); -} - -//***************************************************************************** -// -//! sendto -//! -//! @param sd socket handle -//! @param buf Points to a buffer containing the message to be sent -//! @param len message size in bytes -//! @param flags On this version, this parameter is not supported -//! @param to pointer to an address structure indicating the destination -//! address: sockaddr. On this version only AF_INET is -//! supported. -//! @param tolen destination address structure size -//! -//! @return Return the number of bytes transmitted, or -1 if an -//! error occurred -//! -//! @brief Write data to TCP socket -//! This function is used to transmit a message to another -//! socket. -//! -//! @Note On this version, only blocking mode is supported. -//! -//! @sa send -// -//***************************************************************************** - -INT16 CC3000_EXPORT(sendto)(INT32 sd, const void *buf, INT32 len, INT32 flags, const sockaddr *to, - socklen_t tolen) -{ - return(simple_link_send(sd, buf, len, flags, to, tolen, HCI_CMND_SENDTO)); -} - -//***************************************************************************** -// -//! mdnsAdvertiser -//! -//! @param[in] mdnsEnabled flag to enable/disable the mDNS feature -//! @param[in] deviceServiceName Service name as part of the published -//! canonical domain name -//! @param[in] deviceServiceNameLength Length of the service name - up to 32 chars -//! -//! -//! @return On success, zero is returned, return SOC_ERROR if socket was not -//! opened successfully, or if an error occurred. -//! -//! @brief Set CC3000 in mDNS advertiser mode in order to advertise itself. -// -//***************************************************************************** - -INT16 CC3000_EXPORT(mdnsAdvertiser)(UINT16 mdnsEnabled, CHAR * deviceServiceName, UINT16 deviceServiceNameLength) -{ - INT8 ret; - UINT8 *pTxBuffer, *pArgs; - - if (deviceServiceNameLength > MDNS_DEVICE_SERVICE_MAX_LENGTH) - { - return EFAIL; - } - - pTxBuffer = tSLInformation.pucTxCommandBuffer; - pArgs = (pTxBuffer + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); - - // Fill in HCI packet structure - pArgs = UINT32_TO_STREAM(pArgs, mdnsEnabled); - pArgs = UINT32_TO_STREAM(pArgs, 8); - pArgs = UINT32_TO_STREAM(pArgs, deviceServiceNameLength); - ARRAY_TO_STREAM(pArgs, deviceServiceName, deviceServiceNameLength); - - // Initiate a HCI command - hci_command_send(HCI_CMND_MDNS_ADVERTISE, pTxBuffer, SOCKET_MDNS_ADVERTISE_PARAMS_LEN + deviceServiceNameLength); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_EVNT_MDNS_ADVERTISE, &ret); - - return ret; - -} - - -//***************************************************************************** -// -//! getmssvalue -//! -//! @param[in] sd socket descriptor -//! -//! @return On success, returns the MSS value of a TCP connection -//! -//! @brief Returns the MSS value of a TCP connection according to the socket descriptor -// -//***************************************************************************** -UINT16 CC3000_EXPORT(getmssvalue) (INT32 sd) -{ - UINT8 *ptr, *args; - UINT16 ret; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, sd); - - // Initiate a HCI command - hci_command_send(HCI_CMND_GETMSSVALUE, ptr, SOCKET_GET_MSS_VALUE_PARAMS_LEN); - - // Since we are in blocking state - wait for event complete - SimpleLinkWaitEvent(HCI_EVNT_GETMSSVALUE, &ret); - - return ret; -} diff --git a/drivers/cc3000/src/wlan.c b/drivers/cc3000/src/wlan.c deleted file mode 100644 index 6385937778ccc..0000000000000 --- a/drivers/cc3000/src/wlan.c +++ /dev/null @@ -1,1252 +0,0 @@ -/***************************************************************************** -* -* wlan.c - CC3000 Host Driver Implementation. -* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 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. -* -* Neither the name of Texas Instruments Incorporated nor the names of -* its contributors may be used to endorse or promote products derived -* from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "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 COPYRIGHT -* OWNER OR CONTRIBUTORS 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. -* -*****************************************************************************/ - -//***************************************************************************** -// -//! \addtogroup wlan_api -//! @{ -// -//***************************************************************************** - -#include -#include "wlan.h" -#include "hci.h" -#include "ccspi.h" -#include "socket.h" -#include "nvmem.h" -#include "security.h" -#include "evnt_handler.h" - - -volatile sSimplLinkInformation tSLInformation; - -#define SMART_CONFIG_PROFILE_SIZE 67 // 67 = 32 (max ssid) + 32 (max key) + 1 (SSID length) + 1 (security type) + 1 (key length) - -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG -UINT8 key[AES128_KEY_SIZE]; -UINT8 profileArray[SMART_CONFIG_PROFILE_SIZE]; -#endif //CC3000_UNENCRYPTED_SMART_CONFIG - -/* patches type */ -#define PATCHES_HOST_TYPE_WLAN_DRIVER 0x01 -#define PATCHES_HOST_TYPE_WLAN_FW 0x02 -#define PATCHES_HOST_TYPE_BOOTLOADER 0x03 - -#define SL_SET_SCAN_PARAMS_INTERVAL_LIST_SIZE (16) -#define SL_SIMPLE_CONFIG_PREFIX_LENGTH (3) -#define ETH_ALEN (6) -#define MAXIMAL_SSID_LENGTH (32) - -#define SL_PATCHES_REQUEST_DEFAULT (0) -#define SL_PATCHES_REQUEST_FORCE_HOST (1) -#define SL_PATCHES_REQUEST_FORCE_NONE (2) - - -#define WLAN_SEC_UNSEC (0) -#define WLAN_SEC_WEP (1) -#define WLAN_SEC_WPA (2) -#define WLAN_SEC_WPA2 (3) - - -#define WLAN_SL_INIT_START_PARAMS_LEN (1) -#define WLAN_PATCH_PARAMS_LENGTH (8) -#define WLAN_SET_CONNECTION_POLICY_PARAMS_LEN (12) -#define WLAN_DEL_PROFILE_PARAMS_LEN (4) -#define WLAN_SET_MASK_PARAMS_LEN (4) -#define WLAN_SET_SCAN_PARAMS_LEN (100) -#define WLAN_GET_SCAN_RESULTS_PARAMS_LEN (4) -#define WLAN_ADD_PROFILE_NOSEC_PARAM_LEN (24) -#define WLAN_ADD_PROFILE_WEP_PARAM_LEN (36) -#define WLAN_ADD_PROFILE_WPA_PARAM_LEN (44) -#define WLAN_CONNECT_PARAM_LEN (29) -#define WLAN_SMART_CONFIG_START_PARAMS_LEN (4) - - - - -//***************************************************************************** -// -//! SimpleLink_Init_Start -//! -//! @param usPatchesAvailableAtHost flag to indicate if patches available -//! from host or from EEPROM. Due to the -//! fact the patches are burn to the EEPROM -//! using the patch programmer utility, the -//! patches will be available from the EEPROM -//! and not from the host. -//! -//! @return none -//! -//! @brief Send HCI_CMND_SIMPLE_LINK_START to CC3000 -// -//***************************************************************************** -static void SimpleLink_Init_Start(UINT16 usPatchesAvailableAtHost) -{ - UINT8 *ptr; - UINT8 *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (UINT8 *)(ptr + HEADERS_SIZE_CMD); - - UINT8_TO_STREAM(args, ((usPatchesAvailableAtHost) ? SL_PATCHES_REQUEST_FORCE_NONE : SL_PATCHES_REQUEST_DEFAULT)); - - // IRQ Line asserted - send HCI_CMND_SIMPLE_LINK_START to CC3000 - hci_command_send(HCI_CMND_SIMPLE_LINK_START, ptr, WLAN_SL_INIT_START_PARAMS_LEN); - - SimpleLinkWaitEvent(HCI_CMND_SIMPLE_LINK_START, 0); -} - - - -//***************************************************************************** -// -//! wlan_init -//! -//! @param sWlanCB Asynchronous events callback. -//! 0 no event call back. -//! -call back parameters: -//! 1) event_type: HCI_EVNT_WLAN_UNSOL_CONNECT connect event, -//! HCI_EVNT_WLAN_UNSOL_DISCONNECT disconnect event, -//! HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE config done, -//! HCI_EVNT_WLAN_UNSOL_DHCP dhcp report, -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT ping report OR -//! HCI_EVNT_WLAN_KEEPALIVE keepalive. -//! 2) data: pointer to extra data that received by the event -//! (NULL no data). -//! 3) length: data length. -//! -Events with extra data: -//! HCI_EVNT_WLAN_UNSOL_DHCP: 4 bytes IP, 4 bytes Mask, -//! 4 bytes default gateway, 4 bytes DHCP server and 4 bytes -//! for DNS server. -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT: 4 bytes Packets sent, -//! 4 bytes Packets received, 4 bytes Min round time, -//! 4 bytes Max round time and 4 bytes for Avg round time. -//! -//! @param sFWPatches 0 no patch or pointer to FW patches -//! @param sDriverPatches 0 no patch or pointer to driver patches -//! @param sBootLoaderPatches 0 no patch or pointer to bootloader patches -//! @param sReadWlanInterruptPin init callback. the callback read wlan -//! interrupt status. -//! @param sWlanInterruptEnable init callback. the callback enable wlan -//! interrupt. -//! @param sWlanInterruptDisable init callback. the callback disable wlan -//! interrupt. -//! @param sWriteWlanPin init callback. the callback write value -//! to device pin. -//! -//! @return none -//! -//! @sa wlan_set_event_mask , wlan_start , wlan_stop -//! -//! @brief Initialize wlan driver -//! -//! @warning This function must be called before ANY other wlan driver function -// -//***************************************************************************** - -void wlan_init( tWlanCB sWlanCB, - tFWPatches sFWPatches, - tDriverPatches sDriverPatches, - tBootLoaderPatches sBootLoaderPatches, - tWlanReadInteruptPin sReadWlanInterruptPin, - tWlanInterruptEnable sWlanInterruptEnable, - tWlanInterruptDisable sWlanInterruptDisable, - tWriteWlanPin sWriteWlanPin) -{ - - tSLInformation.sFWPatches = sFWPatches; - tSLInformation.sDriverPatches = sDriverPatches; - tSLInformation.sBootLoaderPatches = sBootLoaderPatches; - - // init io callback - tSLInformation.ReadWlanInterruptPin = sReadWlanInterruptPin; - tSLInformation.WlanInterruptEnable = sWlanInterruptEnable; - tSLInformation.WlanInterruptDisable = sWlanInterruptDisable; - tSLInformation.WriteWlanPin = sWriteWlanPin; - - //init asynchronous events callback - tSLInformation.sWlanCB= sWlanCB; - - // By default TX Complete events are routed to host too - tSLInformation.InformHostOnTxComplete = 1; -} - -//***************************************************************************** -// -//! SpiReceiveHandler -//! -//! @param pvBuffer - pointer to the received data buffer -//! The function triggers Received event/data processing -//! -//! @param Pointer to the received data -//! @return none -//! -//! @brief The function triggers Received event/data processing. It is -//! called from the SPI library to receive the data -// -//***************************************************************************** -void SpiReceiveHandler(void *pvBuffer) -{ - tSLInformation.usEventOrDataReceived = 1; - tSLInformation.pucReceivedData = (UINT8 *)pvBuffer; - - hci_unsolicited_event_handler(); -} - - -//***************************************************************************** -// -//! wlan_start -//! -//! @param usPatchesAvailableAtHost - flag to indicate if patches available -//! from host or from EEPROM. Due to the -//! fact the patches are burn to the EEPROM -//! using the patch programmer utility, the -//! patches will be available from the EEPROM -//! and not from the host. -//! -//! @return none -//! -//! @brief Start WLAN device. This function asserts the enable pin of -//! the device (WLAN_EN), starting the HW initialization process. -//! The function blocked until device Initialization is completed. -//! Function also configure patches (FW, driver or bootloader) -//! and calls appropriate device callbacks. -//! -//! @Note Prior calling the function wlan_init shall be called. -//! @Warning This function must be called after wlan_init and before any -//! other wlan API -//! @sa wlan_init , wlan_stop -//! -// -//***************************************************************************** -#define TIMEOUT (500000) - -int wlan_start(UINT16 usPatchesAvailableAtHost) -{ - - UINT32 ulSpiIRQState; - UINT32 wlan_timeout; - - tSLInformation.NumberOfSentPackets = 0; - tSLInformation.NumberOfReleasedPackets = 0; - tSLInformation.usRxEventOpcode = 0; - tSLInformation.usNumberOfFreeBuffers = 0; - tSLInformation.usSlBufferLength = 0; - tSLInformation.usBufferSize = 0; - tSLInformation.usRxDataPending = 0; - tSLInformation.slTransmitDataError = 0; - tSLInformation.usEventOrDataReceived = 0; - tSLInformation.pucReceivedData = 0; - - // Allocate the memory for the RX/TX data transactions - tSLInformation.pucTxCommandBuffer = (UINT8 *)wlan_tx_buffer; - - // init spi - SpiOpen(SpiReceiveHandler); - - // Check the IRQ line - ulSpiIRQState = tSLInformation.ReadWlanInterruptPin(); - - // Chip enable: toggle WLAN EN line - tSLInformation.WriteWlanPin( WLAN_ENABLE ); - - wlan_timeout = TIMEOUT; - if (ulSpiIRQState) { - // wait till the IRQ line goes low - while(tSLInformation.ReadWlanInterruptPin() != 0 && --wlan_timeout) - { - } - } - else - { - // wait till the IRQ line goes high and than low - while(tSLInformation.ReadWlanInterruptPin() == 0 && --wlan_timeout) - { - } - - if (wlan_timeout == 0) { - return -1; - } - - wlan_timeout = TIMEOUT; - while(tSLInformation.ReadWlanInterruptPin() != 0 && --wlan_timeout) - { - } - } - - if (wlan_timeout ==0) { - return -1; - } - - SimpleLink_Init_Start(usPatchesAvailableAtHost); - - // Read Buffer's size and finish - hci_command_send(HCI_CMND_READ_BUFFER_SIZE, tSLInformation.pucTxCommandBuffer, 0); - SimpleLinkWaitEvent(HCI_CMND_READ_BUFFER_SIZE, 0); - - return 0; -} - - -//***************************************************************************** -// -//! wlan_stop -//! -//! @param none -//! -//! @return none -//! -//! @brief Stop WLAN device by putting it into reset state. -//! -//! @sa wlan_start -// -//***************************************************************************** - -void wlan_stop(void) -{ - // Chip disable - tSLInformation.WriteWlanPin( WLAN_DISABLE ); - - // Wait till IRQ line goes high... - while(tSLInformation.ReadWlanInterruptPin() == 0) - { - } - - // Free the used by WLAN Driver memory - if (tSLInformation.pucTxCommandBuffer) - { - tSLInformation.pucTxCommandBuffer = 0; - } - - SpiClose(); -} - - -//***************************************************************************** -// -//! wlan_connect -//! -//! @param sec_type security options: -//! WLAN_SEC_UNSEC, -//! WLAN_SEC_WEP (ASCII support only), -//! WLAN_SEC_WPA or WLAN_SEC_WPA2 -//! @param ssid up to 32 bytes and is ASCII SSID of the AP -//! @param ssid_len length of the SSID -//! @param bssid 6 bytes specified the AP bssid -//! @param key up to 32 bytes specified the AP security key -//! @param key_len key length -//! -//! @return On success, zero is returned. On error, negative is returned. -//! Note that even though a zero is returned on success to trigger -//! connection operation, it does not mean that CCC3000 is already -//! connected. An asynchronous "Connected" event is generated when -//! actual association process finishes and CC3000 is connected to -//! the AP. If DHCP is set, An asynchronous "DHCP" event is -//! generated when DHCP process is finish. -//! -//! -//! @brief Connect to AP -//! @warning Please Note that when connection to AP configured with security -//! type WEP, please confirm that the key is set as ASCII and not -//! as HEX. -//! @sa wlan_disconnect -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 wlan_connect(UINT32 ulSecType, CHAR *ssid, INT32 ssid_len, - UINT8 *bssid, UINT8 *key, INT32 key_len) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - UINT8 bssid_zero[] = {0, 0, 0, 0, 0, 0}; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in command buffer - args = UINT32_TO_STREAM(args, 0x0000001c); - args = UINT32_TO_STREAM(args, ssid_len); - args = UINT32_TO_STREAM(args, ulSecType); - args = UINT32_TO_STREAM(args, 0x00000010 + ssid_len); - args = UINT32_TO_STREAM(args, key_len); - args = UINT16_TO_STREAM(args, 0); - - // padding shall be zeroed - if(bssid) - { - ARRAY_TO_STREAM(args, bssid, ETH_ALEN); - } - else - { - ARRAY_TO_STREAM(args, bssid_zero, ETH_ALEN); - } - - ARRAY_TO_STREAM(args, ssid, ssid_len); - - if(key_len && key) - { - ARRAY_TO_STREAM(args, key, key_len); - } - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_CONNECT, ptr, WLAN_CONNECT_PARAM_LEN + - ssid_len + key_len - 1); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_CONNECT, &ret); - CC3000_EXPORT(errno) = ret; - - return(ret); -} -#else -INT32 wlan_connect(CHAR *ssid, INT32 ssid_len) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - UINT8 bssid_zero[] = {0, 0, 0, 0, 0, 0}; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in command buffer - args = UINT32_TO_STREAM(args, 0x0000001c); - args = UINT32_TO_STREAM(args, ssid_len); - args = UINT32_TO_STREAM(args, 0); - args = UINT32_TO_STREAM(args, 0x00000010 + ssid_len); - args = UINT32_TO_STREAM(args, 0); - args = UINT16_TO_STREAM(args, 0); - - // padding shall be zeroed - ARRAY_TO_STREAM(args, bssid_zero, ETH_ALEN); - ARRAY_TO_STREAM(args, ssid, ssid_len); - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_CONNECT, ptr, WLAN_CONNECT_PARAM_LEN + - ssid_len - 1); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_CONNECT, &ret); - CC3000_EXPORT(errno) = ret; - - return(ret); -} -#endif - -//***************************************************************************** -// -//! wlan_disconnect -//! -//! @return 0 disconnected done, other CC3000 already disconnected -//! -//! @brief Disconnect connection from AP. -//! -//! @sa wlan_connect -// -//***************************************************************************** - -INT32 wlan_disconnect() -{ - INT32 ret; - UINT8 *ptr; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - - hci_command_send(HCI_CMND_WLAN_DISCONNECT, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_DISCONNECT, &ret); - CC3000_EXPORT(errno) = ret; - - return(ret); -} - -//***************************************************************************** -// -//! wlan_ioctl_set_connection_policy -//! -//! @param should_connect_to_open_ap enable(1), disable(0) connect to any -//! available AP. This parameter corresponds to the configuration of -//! item # 3 in the brief description. -//! @param should_use_fast_connect enable(1), disable(0). if enabled, tries -//! to connect to the last connected AP. This parameter corresponds -//! to the configuration of item # 1 in the brief description. -//! @param auto_start enable(1), disable(0) auto connect -//! after reset and periodically reconnect if needed. This -//! configuration configures option 2 in the above description. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief When auto is enabled, the device tries to connect according -//! the following policy: -//! 1) If fast connect is enabled and last connection is valid, -//! the device will try to connect to it without the scanning -//! procedure (fast). The last connection will be marked as -//! invalid, due to adding/removing profile. -//! 2) If profile exists, the device will try to connect it -//! (Up to seven profiles). -//! 3) If fast and profiles are not found, and open mode is -//! enabled, the device will try to connect to any AP. -//! * Note that the policy settings are stored in the CC3000 NVMEM. -//! -//! @sa wlan_add_profile , wlan_ioctl_del_profile -// -//***************************************************************************** - -INT32 wlan_ioctl_set_connection_policy(UINT32 should_connect_to_open_ap, - UINT32 ulShouldUseFastConnect, - UINT32 ulUseProfiles) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (UINT8 *)(ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, should_connect_to_open_ap); - args = UINT32_TO_STREAM(args, ulShouldUseFastConnect); - args = UINT32_TO_STREAM(args, ulUseProfiles); - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY, - ptr, WLAN_SET_CONNECTION_POLICY_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_add_profile -//! -//! @param ulSecType WLAN_SEC_UNSEC,WLAN_SEC_WEP,WLAN_SEC_WPA,WLAN_SEC_WPA2 -//! @param ucSsid ssid SSID up to 32 bytes -//! @param ulSsidLen ssid length -//! @param ucBssid bssid 6 bytes -//! @param ulPriority ulPriority profile priority. Lowest priority:0. -//! Important Note: Smartconfig process (in unencrypted mode) -//! stores the profile internally with priority 1, so changing -//! priorities when adding new profiles should be done with extra care -//! @param ulPairwiseCipher_Or_TxKeyLen key length for WEP security -//! @param ulGroupCipher_TxKeyIndex key index -//! @param ulKeyMgmt KEY management -//! @param ucPf_OrKey security key -//! @param ulPassPhraseLen security key length for WPA\WPA2 -//! -//! @return On success, index (1-7) of the stored profile is returned. -//! On error, -1 is returned. -//! -//! @brief When auto start is enabled, the device connects to -//! station from the profiles table. Up to 7 profiles are supported. -//! If several profiles configured the device choose the highest -//! priority profile, within each priority group, device will choose -//! profile based on security policy, signal strength, etc -//! parameters. All the profiles are stored in CC3000 NVMEM. -//! -//! @sa wlan_ioctl_del_profile -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 wlan_add_profile(UINT32 ulSecType, - UINT8* ucSsid, - UINT32 ulSsidLen, - UINT8 *ucBssid, - UINT32 ulPriority, - UINT32 ulPairwiseCipher_Or_TxKeyLen, - UINT32 ulGroupCipher_TxKeyIndex, - UINT32 ulKeyMgmt, - UINT8* ucPf_OrKey, - UINT32 ulPassPhraseLen) -{ - UINT16 arg_len=0; - INT32 ret; - UINT8 *ptr; - INT32 i = 0; - UINT8 *args; - UINT8 bssid_zero[] = {0, 0, 0, 0, 0, 0}; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - args = UINT32_TO_STREAM(args, ulSecType); - - // Setup arguments in accordance with the security type - switch (ulSecType) - { - //OPEN - case WLAN_SEC_UNSEC: - { - args = UINT32_TO_STREAM(args, 0x00000014); - args = UINT32_TO_STREAM(args, ulSsidLen); - args = UINT16_TO_STREAM(args, 0); - if(ucBssid) - { - ARRAY_TO_STREAM(args, ucBssid, ETH_ALEN); - } - else - { - ARRAY_TO_STREAM(args, bssid_zero, ETH_ALEN); - } - args = UINT32_TO_STREAM(args, ulPriority); - ARRAY_TO_STREAM(args, ucSsid, ulSsidLen); - - arg_len = WLAN_ADD_PROFILE_NOSEC_PARAM_LEN + ulSsidLen; - } - break; - - //WEP - case WLAN_SEC_WEP: - { - args = UINT32_TO_STREAM(args, 0x00000020); - args = UINT32_TO_STREAM(args, ulSsidLen); - args = UINT16_TO_STREAM(args, 0); - if(ucBssid) - { - ARRAY_TO_STREAM(args, ucBssid, ETH_ALEN); - } - else - { - ARRAY_TO_STREAM(args, bssid_zero, ETH_ALEN); - } - args = UINT32_TO_STREAM(args, ulPriority); - args = UINT32_TO_STREAM(args, 0x0000000C + ulSsidLen); - args = UINT32_TO_STREAM(args, ulPairwiseCipher_Or_TxKeyLen); - args = UINT32_TO_STREAM(args, ulGroupCipher_TxKeyIndex); - ARRAY_TO_STREAM(args, ucSsid, ulSsidLen); - - for(i = 0; i < 4; i++) - { - UINT8 *p = &ucPf_OrKey[i * ulPairwiseCipher_Or_TxKeyLen]; - - ARRAY_TO_STREAM(args, p, ulPairwiseCipher_Or_TxKeyLen); - } - - arg_len = WLAN_ADD_PROFILE_WEP_PARAM_LEN + ulSsidLen + - ulPairwiseCipher_Or_TxKeyLen * 4; - - } - break; - - //WPA - //WPA2 - case WLAN_SEC_WPA: - case WLAN_SEC_WPA2: - { - args = UINT32_TO_STREAM(args, 0x00000028); - args = UINT32_TO_STREAM(args, ulSsidLen); - args = UINT16_TO_STREAM(args, 0); - if(ucBssid) - { - ARRAY_TO_STREAM(args, ucBssid, ETH_ALEN); - } - else - { - ARRAY_TO_STREAM(args, bssid_zero, ETH_ALEN); - } - args = UINT32_TO_STREAM(args, ulPriority); - args = UINT32_TO_STREAM(args, ulPairwiseCipher_Or_TxKeyLen); - args = UINT32_TO_STREAM(args, ulGroupCipher_TxKeyIndex); - args = UINT32_TO_STREAM(args, ulKeyMgmt); - args = UINT32_TO_STREAM(args, 0x00000008 + ulSsidLen); - args = UINT32_TO_STREAM(args, ulPassPhraseLen); - ARRAY_TO_STREAM(args, ucSsid, ulSsidLen); - ARRAY_TO_STREAM(args, ucPf_OrKey, ulPassPhraseLen); - - arg_len = WLAN_ADD_PROFILE_WPA_PARAM_LEN + ulSsidLen + ulPassPhraseLen; - } - - break; - } - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_IOCTL_ADD_PROFILE, - ptr, arg_len); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_ADD_PROFILE, &ret); - - return(ret); -} -#else -INT32 wlan_add_profile(UINT32 ulSecType, - UINT8* ucSsid, - UINT32 ulSsidLen, - UINT8 *ucBssid, - UINT32 ulPriority, - UINT32 ulPairwiseCipher_Or_TxKeyLen, - UINT32 ulGroupCipher_TxKeyIndex, - UINT32 ulKeyMgmt, - UINT8* ucPf_OrKey, - UINT32 ulPassPhraseLen) -{ - return -1; -} -#endif - -//***************************************************************************** -// -//! wlan_ioctl_del_profile -//! -//! @param index number of profile to delete -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Delete WLAN profile -//! -//! @Note In order to delete all stored profile, set index to 255. -//! -//! @sa wlan_add_profile -// -//***************************************************************************** - -INT32 wlan_ioctl_del_profile(UINT32 ulIndex) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (UINT8 *)(ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, ulIndex); - ret = EFAIL; - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_IOCTL_DEL_PROFILE, - ptr, WLAN_DEL_PROFILE_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_DEL_PROFILE, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_ioctl_get_scan_results -//! -//! @param[in] scan_timeout parameter not supported -//! @param[out] ucResults scan results (_wlan_full_scan_results_args_t) -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Gets entry from scan result table. -//! The scan results are returned one by one, and each entry -//! represents a single AP found in the area. The following is a -//! format of the scan result: -//! - 4 Bytes: number of networks found -//! - 4 Bytes: The status of the scan: 0 - aged results, -//! 1 - results valid, 2 - no results -//! - 42 bytes: Result entry, where the bytes are arranged as follows: -//! -//! - 1 bit isValid - is result valid or not -//! - 7 bits rssi - RSSI value; -//! - 2 bits: securityMode - security mode of the AP: -//! 0 - Open, 1 - WEP, 2 WPA, 3 WPA2 -//! - 6 bits: SSID name length -//! - 2 bytes: the time at which the entry has entered into -//! scans result table -//! - 32 bytes: SSID name -//! - 6 bytes: BSSID -//! -//! @Note scan_timeout, is not supported on this version. -//! -//! @sa wlan_ioctl_set_scan_params -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 wlan_ioctl_get_scan_results(UINT32 ulScanTimeout, - UINT8 *ucResults) -{ - UINT8 *ptr; - UINT8 *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, ulScanTimeout); - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS, - ptr, WLAN_GET_SCAN_RESULTS_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS, ucResults); - - return(0); -} -#endif - -//***************************************************************************** -// -//! wlan_ioctl_set_scan_params -//! -//! @param uiEnable - start/stop application scan: -//! 1 = start scan with default interval value of 10 min. -//! in order to set a different scan interval value apply the value -//! in milliseconds. minimum 1 second. 0=stop). Wlan reset -//! (wlan_stop() wlan_start()) is needed when changing scan interval -//! value. Saved: No -//! @param uiMinDwellTime minimum dwell time value to be used for each -//! channel, in milliseconds. Saved: yes -//! Recommended Value: 100 (Default: 20) -//! @param uiMaxDwellTime maximum dwell time value to be used for each -//! channel, in milliseconds. Saved: yes -//! Recommended Value: 100 (Default: 30) -//! @param uiNumOfProbeRequests max probe request between dwell time. -//! Saved: yes. Recommended Value: 5 (Default:2) -//! @param uiChannelMask bitwise, up to 13 channels (0x1fff). -//! Saved: yes. Default: 0x7ff -//! @param uiRSSIThreshold RSSI threshold. Saved: yes (Default: -80) -//! @param uiSNRThreshold NSR threshold. Saved: yes (Default: 0) -//! @param uiDefaultTxPower probe Tx power. Saved: yes (Default: 205) -//! @param aiIntervalList pointer to array with 16 entries (16 channels) -//! each entry (UINT32) holds timeout between periodic scan -//! (connection scan) - in millisecond. Saved: yes. Default 2000ms. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief start and stop scan procedure. Set scan parameters. -//! -//! @Note uiDefaultTxPower, is not supported on this version. -//! -//! @sa wlan_ioctl_get_scan_results -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 wlan_ioctl_set_scan_params(UINT32 uiEnable, UINT32 uiMinDwellTime, - UINT32 uiMaxDwellTime, - UINT32 uiNumOfProbeRequests, - UINT32 uiChannelMask,INT32 iRSSIThreshold, - UINT32 uiSNRThreshold, - UINT32 uiDefaultTxPower, - UINT32 *aiIntervalList) -{ - UINT32 uiRes; - UINT8 *ptr; - UINT8 *args; - - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - // Fill in temporary command buffer - args = UINT32_TO_STREAM(args, 36); - args = UINT32_TO_STREAM(args, uiEnable); - args = UINT32_TO_STREAM(args, uiMinDwellTime); - args = UINT32_TO_STREAM(args, uiMaxDwellTime); - args = UINT32_TO_STREAM(args, uiNumOfProbeRequests); - args = UINT32_TO_STREAM(args, uiChannelMask); - args = UINT32_TO_STREAM(args, iRSSIThreshold); - args = UINT32_TO_STREAM(args, uiSNRThreshold); - args = UINT32_TO_STREAM(args, uiDefaultTxPower); - ARRAY_TO_STREAM(args, aiIntervalList, sizeof(UINT32) * - SL_SET_SCAN_PARAMS_INTERVAL_LIST_SIZE); - - // Initiate a HCI command - hci_command_send(HCI_CMND_WLAN_IOCTL_SET_SCANPARAM, - ptr, WLAN_SET_SCAN_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_SET_SCANPARAM, &uiRes); - - return(uiRes); -} -#endif - -//***************************************************************************** -// -//! wlan_set_event_mask -//! -//! @param mask mask option: -//! HCI_EVNT_WLAN_UNSOL_CONNECT connect event -//! HCI_EVNT_WLAN_UNSOL_DISCONNECT disconnect event -//! HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE smart config done -//! HCI_EVNT_WLAN_UNSOL_INIT init done -//! HCI_EVNT_WLAN_UNSOL_DHCP dhcp event report -//! HCI_EVNT_WLAN_ASYNC_PING_REPORT ping report -//! HCI_EVNT_WLAN_KEEPALIVE keepalive -//! HCI_EVNT_WLAN_TX_COMPLETE - disable information on end of transmission -//! Saved: no. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Mask event according to bit mask. In case that event is -//! masked (1), the device will not send the masked event to host. -// -//***************************************************************************** - -INT32 wlan_set_event_mask(UINT32 ulMask) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - - - if ((ulMask & HCI_EVNT_WLAN_TX_COMPLETE) == HCI_EVNT_WLAN_TX_COMPLETE) - { - tSLInformation.InformHostOnTxComplete = 0; - - // Since an event is a virtual event - i.e. it is not coming from CC3000 - // there is no need to send anything to the device if it was an only event - if (ulMask == HCI_EVNT_WLAN_TX_COMPLETE) - { - return 0; - } - - ulMask &= ~HCI_EVNT_WLAN_TX_COMPLETE; - ulMask |= HCI_EVNT_WLAN_UNSOL_BASE; - } - else - { - tSLInformation.InformHostOnTxComplete = 1; - } - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (UINT8 *)(ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, ulMask); - - // Initiate a HCI command - hci_command_send(HCI_CMND_EVENT_MASK, - ptr, WLAN_SET_MASK_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_EVENT_MASK, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_ioctl_statusget -//! -//! @param none -//! -//! @return WLAN_STATUS_DISCONNECTED, WLAN_STATUS_SCANING, -//! STATUS_CONNECTING or WLAN_STATUS_CONNECTED -//! -//! @brief get wlan status: disconnected, scanning, connecting or connected -// -//***************************************************************************** - -#ifndef CC3000_TINY_DRIVER -INT32 wlan_ioctl_statusget(void) -{ - INT32 ret; - UINT8 *ptr; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - - hci_command_send(HCI_CMND_WLAN_IOCTL_STATUSGET, - ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_STATUSGET, &ret); - - return(ret); -} -#endif - -//***************************************************************************** -// -//! wlan_smart_config_start -//! -//! @param algoEncryptedFlag indicates whether the information is encrypted -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Start to acquire device profile. The device acquire its own -//! profile, if profile message is found. The acquired AP information -//! is stored in CC3000 EEPROM only in case AES128 encryption is used. -//! In case AES128 encryption is not used, a profile is created by -//! CC3000 internally. -//! -//! @Note An asynchronous event - Smart Config Done will be generated as soon -//! as the process finishes successfully. -//! -//! @sa wlan_smart_config_set_prefix , wlan_smart_config_stop -// -//***************************************************************************** - -INT32 wlan_smart_config_start(UINT32 algoEncryptedFlag) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (UINT8 *)(ptr + HEADERS_SIZE_CMD); - - // Fill in HCI packet structure - args = UINT32_TO_STREAM(args, algoEncryptedFlag); - ret = EFAIL; - - hci_command_send(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START, ptr, - WLAN_SMART_CONFIG_START_PARAMS_LEN); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_smart_config_stop -//! -//! @param algoEncryptedFlag indicates whether the information is encrypted -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Stop the acquire profile procedure -//! -//! @sa wlan_smart_config_start , wlan_smart_config_set_prefix -// -//***************************************************************************** - -INT32 wlan_smart_config_stop(void) -{ - INT32 ret; - UINT8 *ptr; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - - hci_command_send(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP, ptr, 0); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_smart_config_set_prefix -//! -//! @param newPrefix 3 bytes identify the SSID prefix for the Smart Config. -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief Configure station ssid prefix. The prefix is used internally -//! in CC3000. It should always be TTT. -//! -//! @Note The prefix is stored in CC3000 NVMEM -//! -//! @sa wlan_smart_config_start , wlan_smart_config_stop -// -//***************************************************************************** - -INT32 wlan_smart_config_set_prefix(CHAR* cNewPrefix) -{ - INT32 ret; - UINT8 *ptr; - UINT8 *args; - - ret = EFAIL; - ptr = tSLInformation.pucTxCommandBuffer; - args = (ptr + HEADERS_SIZE_CMD); - - if (cNewPrefix == NULL) - return ret; - else // with the new Smart Config, prefix must be TTT - { - *cNewPrefix = 'T'; - *(cNewPrefix + 1) = 'T'; - *(cNewPrefix + 2) = 'T'; - } - - ARRAY_TO_STREAM(args, cNewPrefix, SL_SIMPLE_CONFIG_PREFIX_LENGTH); - - hci_command_send(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX, ptr, - SL_SIMPLE_CONFIG_PREFIX_LENGTH); - - // Wait for command complete event - SimpleLinkWaitEvent(HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX, &ret); - - return(ret); -} - -//***************************************************************************** -// -//! wlan_smart_config_process -//! -//! @param none -//! -//! @return On success, zero is returned. On error, -1 is returned -//! -//! @brief process the acquired data and store it as a profile. The acquired -//! AP information is stored in CC3000 EEPROM encrypted. -//! The encrypted data is decrypted and stored as a profile. -//! behavior is as defined by connection policy. -// -//***************************************************************************** - - -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG -INT32 wlan_smart_config_process() -{ - INT32 returnValue; - UINT32 ssidLen, keyLen; - UINT8 *decKeyPtr; - UINT8 *ssidPtr; - - // read the key from EEPROM - fileID 12 - returnValue = aes_read_key(key); - - if (returnValue != 0) - return returnValue; - - // read the received data from fileID #13 and parse it according to the followings: - // 1) SSID LEN - not encrypted - // 2) SSID - not encrypted - // 3) KEY LEN - not encrypted. always 32 bytes long - // 4) Security type - not encrypted - // 5) KEY - encrypted together with true key length as the first byte in KEY - // to elaborate, there are two corner cases: - // 1) the KEY is 32 bytes long. In this case, the first byte does not represent KEY length - // 2) the KEY is 31 bytes long. In this case, the first byte represent KEY length and equals 31 - returnValue = nvmem_read(NVMEM_SHARED_MEM_FILEID, SMART_CONFIG_PROFILE_SIZE, 0, profileArray); - - if (returnValue != 0) - return returnValue; - - ssidPtr = &profileArray[1]; - - ssidLen = profileArray[0]; - - decKeyPtr = &profileArray[profileArray[0] + 3]; - - aes_decrypt(decKeyPtr, key); - if (profileArray[profileArray[0] + 1] > 16) - aes_decrypt((UINT8 *)(decKeyPtr + 16), key); - - if (*(UINT8 *)(decKeyPtr +31) != 0) - { - if (*decKeyPtr == 31) - { - keyLen = 31; - decKeyPtr++; - } - else - { - keyLen = 32; - } - } - else - { - keyLen = *decKeyPtr; - decKeyPtr++; - } - - // add a profile - switch (profileArray[profileArray[0] + 2]) - { - case WLAN_SEC_UNSEC://None - { - returnValue = wlan_add_profile(profileArray[profileArray[0] + 2], // security type - ssidPtr, // SSID - ssidLen, // SSID length - NULL, // BSSID - 1, // Priority - 0, 0, 0, 0, 0); - - break; - } - - case WLAN_SEC_WEP://WEP - { - returnValue = wlan_add_profile(profileArray[profileArray[0] + 2], // security type - ssidPtr, // SSID - ssidLen, // SSID length - NULL, // BSSID - 1, // Priority - keyLen, // KEY length - 0, // KEY index - 0, - decKeyPtr, // KEY - 0); - - break; - } - - case WLAN_SEC_WPA://WPA - case WLAN_SEC_WPA2://WPA2 - { - returnValue = wlan_add_profile(WLAN_SEC_WPA2, // security type - ssidPtr, - ssidLen, - NULL, // BSSID - 1, // Priority - 0x18, // PairwiseCipher - 0x1e, // GroupCipher - 2, // KEY management - decKeyPtr, // KEY - keyLen); // KEY length - - break; - } - } - - return returnValue; -} -#endif //CC3000_UNENCRYPTED_SMART_CONFIG - -//***************************************************************************** -// -// Close the Doxygen group. -//! @} -// -//***************************************************************************** From 858707181d907b96fe55e786defa8947e404ab91 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Aug 2022 17:05:30 +1000 Subject: [PATCH 2523/3533] tools/mpremote: Print a nicer error when a filesystem command fails. Signed-off-by: Damien George --- tools/mpremote/mpremote/main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index e614156dbf78c..fa4785b3bf8ff 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -354,9 +354,13 @@ def _list_recursive(files, path): verbose=verbose, ) else: - pyboard.filesystem_command( - pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose - ) + try: + pyboard.filesystem_command( + pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose + ) + except OSError as er: + print(f"{_PROG}: {er}") + sys.exit(1) def do_edit(pyb, args): From 24f1161fe21df146b64a657acadf67e723040636 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 23 Aug 2022 17:25:54 +1000 Subject: [PATCH 2524/3533] tools/pyboard.py: Remove implicit fs_put if source starts with ./. Signed-off-by: Damien George --- tools/pyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 7525049875879..8c4696c4392c3 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -583,7 +583,7 @@ def fname_cp_dest(src, dest): if cmd == "cp": srcs = args[:-1] dest = args[-1] - if srcs[0].startswith("./") or dest.startswith(":"): + if dest.startswith(":"): op = pyb.fs_put fmt = "cp %s :%s" dest = fname_remote(dest) From f5fedf4676274dd26d70142b08753873dc1b99bc Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 24 Aug 2022 10:26:35 +1000 Subject: [PATCH 2525/3533] tools/pyboard.py: Add fs_cp function for direct device-to-device copy. Signed-off-by: Damien George --- tools/pyboard.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 8c4696c4392c3..436bc5ab1be58 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -491,6 +491,20 @@ def fs_cat(self, src, chunk_size=256): ) self.exec_(cmd, data_consumer=stdout_write_bytes) + def fs_cp(self, src, dest, chunk_size=256, progress_callback=None): + if progress_callback: + src_size = int(self.exec_("import os\nprint(os.stat('%s')[6])" % src)) + written = 0 + self.exec_("fr=open('%s','rb')\nr=fr.read\nfw=open('%s','wb')\nw=fw.write" % (src, dest)) + while True: + data_len = int(self.exec_("d=r(%u)\nw(d)\nprint(len(d))" % chunk_size)) + if not data_len: + break + if progress_callback: + written += data_len + progress_callback(written, src_size) + self.exec_("fr.close()\nfw.close()") + def fs_get(self, src, dest, chunk_size=256, progress_callback=None): if progress_callback: src_size = int(self.exec_("import os\nprint(os.stat('%s')[6])" % src)) @@ -584,18 +598,21 @@ def fname_cp_dest(src, dest): srcs = args[:-1] dest = args[-1] if dest.startswith(":"): - op = pyb.fs_put - fmt = "cp %s :%s" - dest = fname_remote(dest) + op_remote_src = pyb.fs_cp + op_local_src = pyb.fs_put else: - op = pyb.fs_get - fmt = "cp :%s %s" + op_remote_src = pyb.fs_get + op_local_src = lambda src, dest, **_: __import__("shutil").copy(src, dest) for src in srcs: - src = fname_remote(src) - dest2 = fname_cp_dest(src, dest) if verbose: - print(fmt % (src, dest2)) - op(src, dest2, progress_callback=progress_callback) + print("cp %s %s" % (src, dest)) + if src.startswith(":"): + op = op_remote_src + else: + op = op_local_src + src2 = fname_remote(src) + dest2 = fname_cp_dest(src2, fname_remote(dest)) + op(src2, dest2, progress_callback=progress_callback) else: op = { "cat": pyb.fs_cat, From 2e386bcf76c38a81a9de93452f9a253c571d2b72 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 24 Aug 2022 10:33:11 +1000 Subject: [PATCH 2526/3533] tools/mpremote: Print nicer errors for unsupported 'cp -r' arguments. Also document support for 'cp :a :b'. Signed-off-by: Damien George --- docs/reference/mpremote.rst | 2 ++ tools/mpremote/mpremote/main.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index eb0233bddae48..a48df9953f503 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -262,6 +262,8 @@ Examples mpremote cp main.py : + mpremote cp :a.py :b.py + mpremote cp -r dir/ : mpremote cp a.py b.py : + repl diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index fa4785b3bf8ff..bd98da88248c1 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -333,10 +333,15 @@ def _list_recursive(files, path): if fs_args[0] == "cp" and fs_args[1] == "-r": fs_args.pop(0) fs_args.pop(0) - assert fs_args[-1] == ":" + if fs_args[-1] != ":": + print(f"{_PROG}: 'cp -r' destination must be ':'") + sys.exit(1) fs_args.pop() src_files = [] for path in fs_args: + if path.startswith(":"): + print(f"{_PROG}: 'cp -r' source files must be local") + sys.exit(1) _list_recursive(src_files, path) known_dirs = {""} pyb.exec_("import uos") From 6b16ce8d38461e9a8f11642a4a9c6e4621c00872 Mon Sep 17 00:00:00 2001 From: Matt Trentini Date: Fri, 19 Aug 2022 01:15:50 +1000 Subject: [PATCH 2527/3533] docs: Update CPython differences and improve the look of table layouts. Updated some of the CPython feature differences: - Updated status of some features. - Added CSS to fix table widths to 100% and word wrap. - Specified explicit table column ratios to improve layout appearance. - Added missing references to anchors. - Better consistency with use of formatting and case. --- docs/conf.py | 4 + docs/differences/python_35.rst | 330 +++++++++++++++++---------------- docs/differences/python_36.rst | 103 +++++----- docs/differences/python_37.rst | 127 +++++++------ docs/differences/python_38.rst | 33 ++-- docs/differences/python_39.rst | 43 +++-- docs/static/custom.css | 5 + 7 files changed, 353 insertions(+), 292 deletions(-) create mode 100644 docs/static/custom.css diff --git a/docs/conf.py b/docs/conf.py index a7a4ab08c5b03..5533bf01918a7 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -166,6 +166,10 @@ # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['static'] +# Add a custom CSS file for HTML generation +html_css_files = [ + 'custom.css', +] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. diff --git a/docs/differences/python_35.rst b/docs/differences/python_35.rst index 94b10b5837374..0fdc6121a1dd0 100644 --- a/docs/differences/python_35.rst +++ b/docs/differences/python_35.rst @@ -5,55 +5,60 @@ Python 3.5 Below is a list of finalised/accepted PEPs for Python 3.5 grouped into their impact to MicroPython. - +----------------------------------------------------------------------------------------------------------+---------------+ - | **Extensions to the syntax:** | **Status** | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 448 `_ | additional unpacking generalizations | Partial | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 465 `_ | a new matrix multiplication operator | Completed | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 492 `_ | coroutines with async and await syntax | Completed | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | **Extensions and changes to runtime:** | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 461 `_ | % formatting for binary strings | Completed | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 475 `_ | retrying system calls that fail with EINTR | Completed | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 479 `_ | change StopIteration handling inside generators | Completed | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | **Standard library changes:** | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 471 `_ | os.scandir() | | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 485 `_ | math.isclose(), a function for testing | Completed | - | | approximate equality | | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | **Miscellaneous changes:** | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 441 `_ | improved Python zip application support | | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 486 `_ | make the Python Launcher aware of virtual | Not relevant | - | | environments | | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 484 `_ | type hints (advisory only) | In Progress | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 488 `_ | elimination of PYO files | Not relevant | - +--------------------------------------------------------+-------------------------------------------------+---------------+ - | `PEP 489 `_ | redesigning extension module loading | | - +--------------------------------------------------------+-------------------------------------------------+---------------+ +.. table:: + :widths: 30 50 20 + +--------------------------------------------------------------------------------------------------------------+--------------------+ + | **Extensions to the syntax** | **Status** | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 448 `_ | Additional unpacking generalizations | Partial | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 465 `_ | A new matrix multiplication operator | Complete | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 492 `_ | Coroutines with ``async`` and ``await`` syntax | Complete | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | **Extensions and changes to runtime** | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 461 `_ | % formatting for binary strings | Complete | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 475 `_ | Retrying system calls that fail with ``EINTR`` | Complete | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 479 `_ | Change ``StopIteration`` handling inside generators | Complete | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | **Standard library changes** | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 471 `_ | ``os.scandir()`` | | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 485 `_ | ``math.isclose()``, a function for testing | Complete | + | | approximate equality | | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | **Miscellaneous changes** | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 441 `_ | Improved Python zip application support | | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 486 `_ | Make the Python Launcher aware of virtual | Not relevant | + | | environments | | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 484 `_ | Type hints (advisory only) | Complete [#fth]_ | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 488 `_ | Elimination of PYO files | Not relevant | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ + | `PEP 489 `_ | Redesigning extension module loading | | + +--------------------------------------------------------+-----------------------------------------------------+--------------------+ Other Language Changes: - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Added the *namereplace* error handlers. The *backslashreplace* error handlers now work with decoding and | | - | translating. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Property docstrings are now writable. This is especially useful for collections.namedtuple() docstrings | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Circular imports involving relative imports are now supported. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ +.. table:: + :widths: 90 10 + + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Added the *namereplace* error handlers. The *backslashreplace* error handlers now work with decoding and | | + | translating. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Property docstrings are now writable. This is especially useful for collections.namedtuple() docstrings | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Circular imports involving relative imports are now supported. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ New Modules: @@ -65,117 +70,124 @@ New Modules: Changes to built-in modules: - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `collections `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *OrderedDict* class is now implemented in C, which makes it 4 to 100 times faster. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | *OrderedDict.items()* , *OrderedDict.keys()* , *OrderedDict.values()* views now support reversed() | | - | iteration. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The deque class now defines *index()*, *insert()*, and *copy()*, and supports the + and * operators. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Docstrings produced by namedtuple() can now be updated. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The UserString class now implements the *__getnewargs__()*, *__rmod__()*, *casefold()*, *format_map()*, | | - | *isprintable()*, and *maketrans()* methods to match the corresponding methods of str. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `heapq `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Element comparison in *merge()* can now be customized by passing a key function in a new optional key | | - | keyword argument, and a new optional *reverse* keyword argument can be used to reverse element comparison | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `io `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new *BufferedIOBase.readinto1()* method, that uses at most one call to the underlying raw stream's | | - | *RawIOBase.read()* or *RawIOBase.readinto()* methods | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `json `_ | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | JSON decoder now raises JSONDecodeError instead of ValueError to provide better context information about | | - | the error. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `math `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Two new constants have been added to the math module: *inf* and *nan*. | Completed | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new function *isclose()* provides a way to test for approximate equality. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new *gcd()* function has been added. The *fractions.gcd()* function is now deprecated. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `os `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The new *scandir()* function returning an iterator of DirEntry objects has been added. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *urandom()* function now uses the *getrandom()* syscall on Linux 3.17 or newer, and *getentropy()* on | | - | OpenBSD 5.6 and newer, removing the need to use /dev/urandom and avoiding failures due to potential file | | - | descriptor exhaustion. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | New *get_blocking()* and *set_blocking()* functions allow getting and setting a file descriptor's blocking| | - | mode (O_NONBLOCK.) | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | There is a new *os.path.commonpath()* function returning the longest common sub-path of each passed | | - | pathname | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `re `_ | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | References and conditional references to groups with fixed length are now allowed in lookbehind assertions| | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The number of capturing groups in regular expressions is no longer limited to 100. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *sub()* and *subn()* functions now replace unmatched groups with empty strings instead of raising an | | - | exception. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *re.error* exceptions have new attributes, msg, pattern, pos, lineno, and colno, that provide better | | - | context information about the error | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `socket `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Functions with timeouts now use a monotonic clock, instead of a system clock. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new *socket.sendfile()* method allows sending a file over a socket by using the high-performance | | - | *os.sendfile()* function on UNIX, resulting in uploads being from 2 to 3 times faster than when using | | - | plain *socket.send()* | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *socket.sendall()* method no longer resets the socket timeout every time bytes are received or sent. | | - | The socket timeout is now the maximum total duration to send all data. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The backlog argument of the *socket.listen()* method is now optional. By default it is set to SOMAXCONN or| Completed | - | to 128, whichever is less. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `ssl `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Memory BIO Support | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | Application-Layer Protocol Negotiation Support | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | There is a new *SSLSocket.version()* method to query the actual protocol version in use. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The SSLSocket class now implements a *SSLSocket.sendfile()* method. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *SSLSocket.send()* method now raises either the *ssl.SSLWantReadError* or *ssl.SSLWantWriteError* | | - | exception on a non-blocking socket if the operation would block. Previously, it would return 0. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *cert_time_to_seconds()* function now interprets the input time as UTC and not as local time, per RFC | | - | 5280. Additionally, the return value is always an int. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | New *SSLObject.shared_ciphers()* and *SSLSocket.shared_ciphers()* methods return the list of ciphers sent | | - | by the client during the handshake. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *SSLSocket.do_handshake()*, *SSLSocket.read()*, *SSLSocket.shutdown()*, and *SSLSocket.write()* | | - | methods of the SSLSocket class no longer reset the socket timeout every time bytes are received or sent. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *match_hostname()* function now supports matching of IP addresses. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `sys `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new *set_coroutine_wrapper()* function allows setting a global hook that will be called whenever a | | - | coroutine object is created by an async def function. A corresponding *get_coroutine_wrapper()* can be | | - | used to obtain a currently set wrapper. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | A new *is_finalizing()* function can be used to check if the Python interpreter is shutting down. | | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | `time `_ | - +-----------------------------------------------------------------------------------------------------------+---------------+ - | The *monotonic()* function is now always available | | - +-----------------------------------------------------------------------------------------------------------+---------------+ +.. table:: + :widths: 90 10 + + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `collections `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *OrderedDict* class is now implemented in C, which makes it 4 to 100 times faster. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | *OrderedDict.items()* , *OrderedDict.keys()* , *OrderedDict.values()* views now support reversed() | | + | iteration. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The deque class now defines *index()*, *insert()*, and *copy()*, and supports the + and * operators. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Docstrings produced by namedtuple() can now be updated. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The UserString class now implements the *__getnewargs__()*, *__rmod__()*, *casefold()*, *format_map()*, | | + | *isprintable()*, and *maketrans()* methods to match the corresponding methods of str. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `heapq `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Element comparison in *merge()* can now be customized by passing a key function in a new optional key | | + | keyword argument, and a new optional *reverse* keyword argument can be used to reverse element comparison | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `io `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new *BufferedIOBase.readinto1()* method, that uses at most one call to the underlying raw stream's | | + | *RawIOBase.read()* or *RawIOBase.readinto()* methods | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `json `_ | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | JSON decoder now raises JSONDecodeError instead of ValueError to provide better context information about | | + | the error. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `math `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Two new constants have been added to the math module: *inf* and *nan*. | Complete | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new function *isclose()* provides a way to test for approximate equality. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new *gcd()* function has been added. The *fractions.gcd()* function is now deprecated. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `os `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The new *scandir()* function returning an iterator of DirEntry objects has been added. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *urandom()* function now uses the *getrandom()* syscall on Linux 3.17 or newer, and *getentropy()* on | | + | OpenBSD 5.6 and newer, removing the need to use /dev/urandom and avoiding failures due to potential file | | + | descriptor exhaustion. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | New *get_blocking()* and *set_blocking()* functions allow getting and setting a file descriptor's blocking| | + | mode (O_NONBLOCK.) | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | There is a new *os.path.commonpath()* function returning the longest common sub-path of each passed | | + | pathname | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `re `_ | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | References and conditional references to groups with fixed length are now allowed in lookbehind assertions| | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The number of capturing groups in regular expressions is no longer limited to 100. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *sub()* and *subn()* functions now replace unmatched groups with empty strings instead of raising an | | + | exception. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *re.error* exceptions have new attributes, msg, pattern, pos, lineno, and colno, that provide better | | + | context information about the error | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `socket `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Functions with timeouts now use a monotonic clock, instead of a system clock. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new *socket.sendfile()* method allows sending a file over a socket by using the high-performance | | + | *os.sendfile()* function on UNIX, resulting in uploads being from 2 to 3 times faster than when using | | + | plain *socket.send()* | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *socket.sendall()* method no longer resets the socket timeout every time bytes are received or sent. | | + | The socket timeout is now the maximum total duration to send all data. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The backlog argument of the *socket.listen()* method is now optional. By default it is set to SOMAXCONN or| Complete | + | to 128, whichever is less. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `ssl `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Memory BIO Support | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | Application-Layer Protocol Negotiation Support | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | There is a new *SSLSocket.version()* method to query the actual protocol version in use. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The SSLSocket class now implements a *SSLSocket.sendfile()* method. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *SSLSocket.send()* method now raises either the *ssl.SSLWantReadError* or *ssl.SSLWantWriteError* | | + | exception on a non-blocking socket if the operation would block. Previously, it would return 0. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *cert_time_to_seconds()* function now interprets the input time as UTC and not as local time, per RFC | | + | 5280. Additionally, the return value is always an int. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | New *SSLObject.shared_ciphers()* and *SSLSocket.shared_ciphers()* methods return the list of ciphers sent | | + | by the client during the handshake. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *SSLSocket.do_handshake()*, *SSLSocket.read()*, *SSLSocket.shutdown()*, and *SSLSocket.write()* | | + | methods of the SSLSocket class no longer reset the socket timeout every time bytes are received or sent. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *match_hostname()* function now supports matching of IP addresses. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `sys `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new *set_coroutine_wrapper()* function allows setting a global hook that will be called whenever a | | + | coroutine object is created by an async def function. A corresponding *get_coroutine_wrapper()* can be | | + | used to obtain a currently set wrapper. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | A new *is_finalizing()* function can be used to check if the Python interpreter is shutting down. | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | `time `_ | + +-----------------------------------------------------------------------------------------------------------+---------------+ + | The *monotonic()* function is now always available | | + +-----------------------------------------------------------------------------------------------------------+---------------+ + +.. rubric:: Notes + +.. [#fth] The MicroPython parser correct ignores all type hints. However, the ``typing`` module is not built-in. diff --git a/docs/differences/python_36.rst b/docs/differences/python_36.rst index edf6bef02300a..3315b0594dafc 100644 --- a/docs/differences/python_36.rst +++ b/docs/differences/python_36.rst @@ -5,53 +5,59 @@ Python 3.6 Python 3.6 beta 1 was released on 12 Sep 2016, and a summary of the new features can be found here: - +-----------------------------------------------------------------------------------------------------------+--------------+ - | **New Syntax Features:** | **Status** | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 498 `_ | Literal String Formatting | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 515 `_ | Underscores in Numeric Literals | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 525 `_ | Asynchronous Generators | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 526 `_ | Syntax for Variable Annotations (provisional) | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 530 `_ | Asynchronous Comprehensions | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | **New Built-in Features:** | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 468 `_ | Preserving the order of *kwargs* in a function | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 487 `_ | Simpler customization of class creation | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 520 `_ | Preserving Class Attribute Definition Order | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | **Standard Library Changes:** | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 495 `_ | Local Time Disambiguation | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 506 `_ | Adding A Secrets Module To The Standard Library | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 519 `_ | Adding a file system path protocol | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | **CPython internals:** | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 509 `_ | Add a private version to dict | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 523 `_ | Adding a frame evaluation API to CPython | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | **Linux/Window Changes** | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 524 `_ | Make os.urandom() blocking on Linux | | - | | (during system startup) | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 528 `_ | Change Windows console encoding to UTF-8 | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ - | `PEP 529 `_ | Change Windows filesystem encoding to UTF-8 | | - +--------------------------------------------------------+--------------------------------------------------+--------------+ +.. table:: + :widths: 30 50 20 + + +-----------------------------------------------------------------------------------------------------------+-----------------+ + | **New Syntax Features** | **Status** | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 498 `_ | Literal String Formatting | Complete | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 515 `_ | Underscores in Numeric Literals | Complete | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 525 `_ | Asynchronous Generators | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 526 `_ | Syntax for Variable Annotations (provisional) | Complete | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 530 `_ | Asynchronous Comprehensions | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | **New Built-in Features** | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 468 `_ | Preserving the order of *kwargs* in a function | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 487 `_ | Simpler customization of class creation | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 520 `_ | Preserving Class Attribute Definition Order | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | **Standard Library Changes** | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 495 `_ | Local Time Disambiguation | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 506 `_ | Adding A Secrets Module To The Standard Library | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 519 `_ | Adding a file system path protocol | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | **CPython Internals** | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 509 `_ | Add a private version to dict | Won't do | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 523 `_ | Adding a frame evaluation API to CPython | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | **Linux/Window Changes** | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 524 `_ | Make ``os.urandom()`` blocking on Linux | | + | | (during system startup) | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 528 `_ | Change Windows console encoding to UTF-8 | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ + | `PEP 529 `_ | Change Windows filesystem encoding to UTF-8 | | + +--------------------------------------------------------+--------------------------------------------------+-----------------+ Other Language Changes: +.. table:: + :widths: 90 10 + +-------------------------------------------------------------------------------------------------------------+---------------+ | A *global* or *nonlocal* statement must now textually appear before the first use of the affected name in | | | the same scope. Previously this was a SyntaxWarning. | | @@ -71,6 +77,9 @@ Other Language Changes: Changes to built-in modules: +.. table:: + :widths: 90 10 + +--------------------------------------------------------------------------------------------------------------+----------------+ | `array `_ | | +--------------------------------------------------------------------------------------------------------------+----------------+ @@ -78,7 +87,7 @@ Changes to built-in modules: +--------------------------------------------------------------------------------------------------------------+----------------+ | `binascii `_ | | +--------------------------------------------------------------------------------------------------------------+----------------+ - | The b2a_base64() function now accepts an optional newline keyword argument to control whether the newline | Completed | + | The b2a_base64() function now accepts an optional newline keyword argument to control whether the newline | Complete | | character is appended to the return value | | +--------------------------------------------------------------------------------------------------------------+----------------+ | `cmath `_ | | @@ -121,7 +130,7 @@ Changes to built-in modules: +--------------------------------------------------------------------------------------------------------------+----------------+ | `math `_ | +--------------------------------------------------------------------------------------------------------------+----------------+ - | The new math.tau (Ï„) constant has been added | Completed | + | The new math.tau (Ï„) constant has been added | Complete | +--------------------------------------------------------------------------------------------------------------+----------------+ | `os `_ | +--------------------------------------------------------------------------------------------------------------+----------------+ @@ -136,7 +145,7 @@ Changes to built-in modules: | `re `_ | +--------------------------------------------------------------------------------------------------------------+----------------+ | Added support of modifier spans in regular expressions. Examples: *'(?i:p)ython'* matches 'python' and | | - | 'Python', but not 'PYTHON'; *'(?i)g(?-i:v)r'* matches *'GvR'* and *'gvr'*, but not *'GVR'* . | | + | 'Python', but not 'PYTHON'; *'(?i)g(?-i:v)r'* matches *'GvR'* and *'gvr'*, but not *'GVR'*. | | +--------------------------------------------------------------------------------------------------------------+----------------+ | Match object groups can be accessed by *__getitem__*, which is equivalent to *group()*. So *mo['name']* is | | | now equivalent to *mo.group('name')*. | | diff --git a/docs/differences/python_37.rst b/docs/differences/python_37.rst index c46678e931dd1..86d1b6e81f96d 100644 --- a/docs/differences/python_37.rst +++ b/docs/differences/python_37.rst @@ -5,72 +5,81 @@ Python 3.7 New Features: - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | **Features:** | **Status** | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 538 `_ | Coercing the legacy C locale to a UTF-8 based | | - | | locale | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 539 `_ | A New C-API for Thread-Local Storage in CPython | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 540 `_ | UTF-8 mode | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 552 `_ | Deterministic pyc | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 553 `_ | Built-in breakpoint() | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 557 `_ | Data Classes | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 560 `_ | Core support for typing module and generic types | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 562 `_ | Module __getattr__ and __dir__ | Partially done | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 563 `_ | Postponed Evaluation of Annotations | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 564 `_ | Time functions with nanosecond resolution | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 565 `_ | Show DeprecationWarning in __main__ | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ - | `PEP 567 `_ | Context Variables | | - +--------------------------------------------------------+--------------------------------------------------+----------------+ +.. table:: + :widths: 20 60 20 + + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | **Feature** | **Status** | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 538 `_ | Coercing the legacy C locale to a UTF-8 based | | + | | locale | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 539 `_ | A New C-API for Thread-Local Storage in CPython | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 540 `_ | UTF-8 mode | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 552 `_ | Deterministic pyc | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 553 `_ | Built-in ``breakpoint()`` | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 557 `_ | Data Classes | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 560 `_ | Core support for typing module and generic types | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 562 `_ | Module ``__getattr__`` and ``__dir__`` | Partial | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 563 `_ | Postponed Evaluation of Annotations | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 564 `_ | Time functions with nanosecond resolution | Partial [#ftimenanosec]_ | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 565 `_ | Show DeprecationWarning in ``__main__`` | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ + | `PEP 567 `_ | Context Variables | | + +--------------------------------------------------------+--------------------------------------------------+--------------------------------------+ Other Language Changes: - +----------------------------------------------------------------------------------------------------------+----------------+ - | async and await are now reserved keywords | Completed | - +----------------------------------------------------------------------------------------------------------+----------------+ - | dict objects must preserve insertion-order | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | More than 255 arguments can now be passed to a function; a function can now have more than 255 parameters| | - +----------------------------------------------------------------------------------------------------------+----------------+ - | bytes.fromhex() and bytearray.fromhex() now ignore all ASCII whitespace, not only spaces | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | str, bytes, and bytearray gained support for the new isascii() method, which can be used to test if a | | - | string or bytes contain only the ASCII characters | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | ImportError now displays module name and module __file__ path whenfrom ... import ... fails | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | Circular imports involving absolute imports with binding a submodule to a name are now supported | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | object.__format__(x, '') is now equivalent to str(x) rather than format(str(self), '') | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | In order to better support dynamic creation of stack traces, types.TracebackType can now be instantiated | | - | from Python code, and the tb_next attribute on tracebacks is now writable | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | When using the -m switch, sys.path[0] is now eagerly expanded to the full starting directory path, rather| | - | than being left as the empty directory (which allows imports from the current working directory at the | | - | time when an import occurs) | | - +----------------------------------------------------------------------------------------------------------+----------------+ - | The new -X importtime option or the PYTHONPROFILEIMPORTTIME environment variable can be used to show the | | - | timing of each module import | | - +----------------------------------------------------------------------------------------------------------+----------------+ +.. table:: + :widths: 90 10 + + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``async`` and ``await`` are now reserved keywords | Complete | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``dict`` objects must preserve insertion-order | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | More than 255 arguments can now be passed to a function; a function can now have more than 255 parameters | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``bytes.fromhex()`` and ``bytearray.fromhex()`` now ignore all ASCII whitespace, not only spaces | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``str``, ``bytes``, and ``bytearray`` gained support for the new ``isascii()`` method, which can be used to | | + | test if a string or bytes contain only the ASCII characters | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``ImportError`` now displays module name and module ``__file__`` path when ``from ... import ...`` fails | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | Circular imports involving absolute imports with binding a submodule to a name are now supported | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather than ``format(str(self), '')`` | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | In order to better support dynamic creation of stack traces, ``types.TracebackType`` can now be | | + | instantiated from Python code, and the ``tb_next`` attribute on tracebacks is now writable | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | When using the ``-m`` switch, ``sys.path[0]`` is now eagerly expanded to the full starting directory path, | | + | rather than being left as the empty directory (which allows imports from the current working directory | | + | at the time when an import occurs) | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ + | The new ``-X importtime`` option or the ``PYTHONPROFILEIMPORTTIME`` environment variable can be used to | | + | show the timing of each module import | | + +-----------------------------------------------------------------------------------------------------------------+----------------+ Changes to built-in modules: +.. table:: + :widths: 90 10 + +------------------------------------------------------------------------------------------------------------+----------------+ | `asyncio `_ | | +------------------------------------------------------------------------------------------------------------+----------------+ - | asyncio (many, may need a separate ticket) | | + | Too many to list | | +------------------------------------------------------------------------------------------------------------+----------------+ | `gc `_ | | +------------------------------------------------------------------------------------------------------------+----------------+ @@ -93,3 +102,7 @@ Changes to built-in modules: +------------------------------------------------------------------------------------------------------------+----------------+ | Mostly updates to support nanosecond resolution in PEP564, see above | | +------------------------------------------------------------------------------------------------------------+----------------+ + +.. rubric:: Notes + +.. [#ftimenanosec] Only :func:`time.time_ns` is implemented. diff --git a/docs/differences/python_38.rst b/docs/differences/python_38.rst index 47840a8b40ec3..32fd68d2caeac 100644 --- a/docs/differences/python_38.rst +++ b/docs/differences/python_38.rst @@ -5,15 +5,18 @@ Python 3.8 Python 3.8.0 (final) was released on the 14 October 2019. The Features for 3.8 are defined in `PEP 569 `_ and -a detailed description of the changes can be found in What's New in `Python +a detailed description of the changes can be found in `What's New in Python 3.8. `_ +.. table:: + :widths: 20 60 20 + +--------------------------------------------------------+---------------------------------------------------+---------------+ - | **Features:** | Status | + | **Features** | **Status** | +--------------------------------------------------------+---------------------------------------------------+---------------+ | `PEP 570 `_ | Positional-only arguments | | +--------------------------------------------------------+---------------------------------------------------+---------------+ - | `PEP 572 `_ | Assignment Expressions | | + | `PEP 572 `_ | Assignment Expressions | Complete | +--------------------------------------------------------+---------------------------------------------------+---------------+ | `PEP 574 `_ | Pickle protocol 5 with out-of-band data | | +--------------------------------------------------------+---------------------------------------------------+---------------+ @@ -25,13 +28,16 @@ a detailed description of the changes can be found in What's New in `Python +--------------------------------------------------------+---------------------------------------------------+---------------+ | **Miscellaneous** | +------------------------------------------------------------------------------------------------------------+---------------+ - | f-strings support = for self-documenting expressions and debugging | Completed | + | f-strings support = for self-documenting expressions and debugging | Complete | +------------------------------------------------------------------------------------------------------------+---------------+ Other Language Changes: +.. table:: + :widths: 90 10 + +------------------------------------------------------------------------------------------------------------+-------------+ - | A *continue* statement was illegal in the *finally* clause due to a problem with the implementation. In | Completed | + | A *continue* statement was illegal in the *finally* clause due to a problem with the implementation. In | Complete | | Python 3.8 this restriction was lifted | | +------------------------------------------------------------------------------------------------------------+-------------+ | The *bool*, *int* , and *fractions.Fraction* types now have an *as_integer_ratio()* method like that found | | @@ -72,14 +78,17 @@ Other Language Changes: Changes to built-in modules: +.. table:: + :widths: 90 10 + +------------------------------------------------------------------------------------------------------------+-------------+ - | `asyncio` | + | `asyncio `_ | +------------------------------------------------------------------------------------------------------------+-------------+ - | *asyncio.run()* has graduated from the provisional to stable API | Completed | + | *asyncio.run()* has graduated from the provisional to stable API | Complete | +------------------------------------------------------------------------------------------------------------+-------------+ | Running *python -m asyncio* launches a natively async REPL | | +------------------------------------------------------------------------------------------------------------+-------------+ - | The exception *asyncio.CancelledError* now inherits from *BaseException* rather than *Exception* and no | Completed | + | The exception *asyncio.CancelledError* now inherits from *BaseException* rather than *Exception* and no | Complete | | longer inherits from *concurrent.futures.CancelledError* | | +------------------------------------------------------------------------------------------------------------+-------------+ | Added *asyncio.Task.get_coro()* for getting the wrapped coroutine within an *asyncio.Task* | | @@ -90,12 +99,12 @@ Changes to built-in modules: | Added support for Happy Eyeballs to *asyncio.loop.create_connection()*. To specify the behavior, two new | | | parameters have been added: *happy_eyeballs_delay* and interleave. | | +------------------------------------------------------------------------------------------------------------+-------------+ - | `gc` | + | `gc `_ | +------------------------------------------------------------------------------------------------------------+-------------+ | *get_objects()* can now receive an optional generation parameter indicating a generation to get objects | | | from. (Note, though, that while *gc* is a built-in, *get_objects()* is not implemented for MicroPython) | | +------------------------------------------------------------------------------------------------------------+-------------+ - | `math` | + | `math `_ | +------------------------------------------------------------------------------------------------------------+-------------+ | Added new function *math.dist()* for computing Euclidean distance between two points | | +------------------------------------------------------------------------------------------------------------+-------------+ @@ -109,9 +118,9 @@ Changes to built-in modules: | Added a new function *math.isqrt()* for computing accurate integer square roots without conversion to | | | floating point | | +------------------------------------------------------------------------------------------------------------+-------------+ - | The function *math.factorial()* no longer accepts arguments that are not int-like | Completed | + | The function *math.factorial()* no longer accepts arguments that are not int-like | Complete | +------------------------------------------------------------------------------------------------------------+-------------+ - | `sys` | + | `sys `_ | +------------------------------------------------------------------------------------------------------------+-------------+ | Add new *sys.unraisablehook()* function which can be overridden to control how "unraisable exceptions" | | | are handled | | diff --git a/docs/differences/python_39.rst b/docs/differences/python_39.rst index 6852dd635ea82..023c6d7c1e398 100644 --- a/docs/differences/python_39.rst +++ b/docs/differences/python_39.rst @@ -8,35 +8,41 @@ defined in `PEP 596 and a detailed description of the changes can be found in `What's New in Python 3.9 `_ +.. table:: + :widths: 20 60 20 + +--------------------------------------------------------+----------------------------------------------------+--------------+ - | **Features:** | | **Status** | + | **Features** | | **Status** | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 573 `_ | fast access to module state from methods of C | | + | `PEP 573 `_ | Fast access to module state from methods of C | | | | extension types | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 584 `_ | union operators added to dict | | + | `PEP 584 `_ | Union operators added to dict | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 585 `_ | type hinting generics in standard collections | | + | `PEP 585 `_ | Type hinting generics in standard collections | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 593 `_ | flexible function and variable annotations | | + | `PEP 593 `_ | Flexible function and variable annotations | | +--------------------------------------------------------+----------------------------------------------------+--------------+ | `PEP 602 `_ | CPython adopts an annual release cycle. Instead of | | | | annual, aiming for two month release cycle | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 614 `_ | relaxed grammar restrictions on decorators | | + | `PEP 614 `_ | Relaxed grammar restrictions on decorators | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 615 `_ | the IANA Time Zone Database is now present in the | | + | `PEP 615 `_ | The IANA Time Zone Database is now present in the | | | | standard library in the zoneinfo module | | +--------------------------------------------------------+----------------------------------------------------+--------------+ - | `PEP 616 `_ | string methods to remove prefixes and suffixes | | + | `PEP 616 `_ | String methods to remove prefixes and suffixes | | +--------------------------------------------------------+----------------------------------------------------+--------------+ | `PEP 617 `_ | CPython now uses a new parser based on PEG | | +--------------------------------------------------------+----------------------------------------------------+--------------+ Other Language Changes: +.. table:: + :widths: 90 10 + +-------------------------------------------------------------------------------------------------------------+---------------+ - | *__import__()* now raises *ImportError* instead of *ValueError* | Completed | + | *__import__()* now raises *ImportError* instead of *ValueError* | Complete | +-------------------------------------------------------------------------------------------------------------+---------------+ | Python now gets the absolute path of the script filename specified on the command line (ex: *python3* | | | *script.py*): the *__file__* attribute of the *__main__* module became an absolute path, rather than a | | @@ -62,8 +68,11 @@ Other Language Changes: Changes to built-in modules: +.. table:: + :widths: 90 10 + +---------------------------------------------------------------------------------------------------------------+---------------+ - | `asyncio` | + | `asyncio `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Due to significant security concerns, the reuse_address parameter of *asyncio.loop.create_datagram_endpoint()*| | | is no longer supported | | @@ -82,13 +91,13 @@ Changes to built-in modules: +---------------------------------------------------------------------------------------------------------------+---------------+ | *asyncio* now raises *TyperError* when calling incompatible methods with an *ssl.SSLSocket* socket | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | `gc` | + | `gc `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Garbage collection does not block on resurrected objects | | +---------------------------------------------------------------------------------------------------------------+---------------+ | Added a new function *gc.is_finalized()* to check if an object has been finalized by the garbage collector | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | `math` | + | `math `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Expanded the *math.gcd()* function to handle multiple arguments. Formerly, it only supported two arguments | | +---------------------------------------------------------------------------------------------------------------+---------------+ @@ -98,21 +107,21 @@ Changes to built-in modules: +---------------------------------------------------------------------------------------------------------------+---------------+ | Added *math.ulp()*: return the value of the least significant bit of a float | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | `os` | + | `os `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Exposed the Linux-specific *os.pidfd_open()* and *os.P_PIDFD* | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | The *os.unsetenv()* function is now also available on Windows | Completed | + | The *os.unsetenv()* function is now also available on Windows | Complete | +---------------------------------------------------------------------------------------------------------------+---------------+ - | The *os.putenv()* and *os.unsetenv()* functions are now always available | Completed | + | The *os.putenv()* and *os.unsetenv()* functions are now always available | Complete | +---------------------------------------------------------------------------------------------------------------+---------------+ | Added *os.waitstatus_to_exitcode()* function: convert a wait status to an exit code | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | `random` | + | `random `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Added a new *random.Random.randbytes* method: generate random bytes | | +---------------------------------------------------------------------------------------------------------------+---------------+ - | `sys` | + | `sys `_ | +---------------------------------------------------------------------------------------------------------------+---------------+ | Added a new *sys.platlibdir* attribute: name of the platform-specific library directory | | +---------------------------------------------------------------------------------------------------------------+---------------+ diff --git a/docs/static/custom.css b/docs/static/custom.css new file mode 100644 index 0000000000000..b0a7f746fcab3 --- /dev/null +++ b/docs/static/custom.css @@ -0,0 +1,5 @@ +/* Workaround to force Sphinx to render tables to 100% and wordwrap */ +/* See https://stackoverflow.com/questions/69359978/grid-table-does-not-word-wrap for more details */ +.wy-table-responsive table td, .wy-table-responsive table th { + white-space: inherit; +} From 769262ef03b4e7198759bcc72c87c7ca5fc7de29 Mon Sep 17 00:00:00 2001 From: Tomasz 'CeDeROM' CEDRO Date: Thu, 18 Aug 2022 05:20:02 +0200 Subject: [PATCH 2528/3533] docs/esp32: Update UART quickref on input-only pins. Signed-off-by: Tomasz 'CeDeROM' CEDRO --- docs/esp32/quickref.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 1529c0ef49297..03ca97c7a7df0 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -216,9 +216,10 @@ They each have default GPIO assigned to them, however depending on your ESP32 variant and board, these pins may conflict with embedded flash, onboard PSRAM or peripherals. -Any GPIO can be used for hardware UARTs using the GPIO matrix, so to avoid -conflicts simply provide ``tx`` and ``rx`` pins when constructing. The default -pins listed below. +Any GPIO can be used for hardware UARTs using the GPIO matrix, except for +input-only pins 34-39 that can be used as ``rx``. To avoid conflicts simply +provide ``tx`` and ``rx`` pins when constructing. The default pins listed +below. ===== ===== ===== ===== \ UART0 UART1 UART2 From 602f9db2f35924a687f8ded2170b2a0b0e13979a Mon Sep 17 00:00:00 2001 From: Tomasz 'CeDeROM' CEDRO Date: Thu, 18 Aug 2022 06:02:53 +0200 Subject: [PATCH 2529/3533] docs/library/machine.UART: Add notes about UART init and deinit. * `init()` can be called multiple times to reconfigure UART. * After `deinit()` it is impossible to call `init()` again. Signed-off-by: Tomasz 'CeDeROM' CEDRO --- docs/library/machine.UART.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index e239525736983..1d54174c99614 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -87,10 +87,22 @@ Methods When no pins are given, then the default set of TX and RX pins is taken, and hardware flow control will be disabled. If *pins* is ``None``, no pin assignment will be made. + .. note:: + It is possible to call ``init()`` multiple times on the same object in + order to reconfigure UART on the fly. That allows using single UART + peripheral to serve different devices attached to different GPIO pins. + Only one device can be served at a time in that case. + Also do not call ``deinit()`` as it will prevent calling ``init()`` + again. + .. method:: UART.deinit() Turn off the UART bus. + .. note:: + You will not be able to call ``init()`` on the object after ``deinit()``. + A new instance needs to be created in that case. + .. method:: UART.any() Returns an integer counting the number of characters that can be read without From da217e83d9a710a03fd8e324fe18d3684628cd69 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 23 Aug 2022 21:35:49 +0200 Subject: [PATCH 2530/3533] docs/library: Fix nested rst styles not rendering. These can't be nested, so apply styling separately. Signed-off-by: Laurens Valk --- docs/library/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/index.rst b/docs/library/index.rst index ffd373a4df25b..59ed1127a7836 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -195,4 +195,4 @@ but also the MicroPython libraries too (e.g. ``machine``, ``bluetooth``, etc). The main exception is the port-specific libraries (``pyb``, ``esp``, etc). *Other than when you specifically want to force the use of the built-in module, -we recommend always using ``import module`` rather than ``import umodule``.* +we recommend always using* ``import module`` *rather than* ``import umodule``. From 6dcfb25ae7cadc298a5aaa09c68205a828939cd9 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 23 Aug 2022 21:37:24 +0200 Subject: [PATCH 2531/3533] docs/library/micropython: Fix spelling of compiler. Signed-off-by: Laurens Valk --- docs/library/micropython.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/micropython.rst b/docs/library/micropython.rst index 7106a1a2ff16d..b17dfa9a75a48 100644 --- a/docs/library/micropython.rst +++ b/docs/library/micropython.rst @@ -9,7 +9,7 @@ Functions .. function:: const(expr) - Used to declare that the expression is a constant so that the compile can + Used to declare that the expression is a constant so that the compiler can optimise it. The use of this function should be as follows:: from micropython import const From 09879f99ca3a6c0e48353eb2aaa1c71ed237f126 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 22 Aug 2022 11:16:16 +1000 Subject: [PATCH 2532/3533] esp8266/README: Update build instructions to match ci.sh. The existing non-Docker instructions are basically impossible to follow because the esp-open-sdk does not compile. Update these instructions to use the exact toolchain that our CI uses. Also split the Docker from non-Docker instructions, to avoid confusion about which commands need to be prefixed. Signed-off-by: Jim Mussared --- ports/esp8266/README.md | 116 +++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 30 deletions(-) diff --git a/ports/esp8266/README.md b/ports/esp8266/README.md index 15ca7f891c263..dd50fc1af943f 100644 --- a/ports/esp8266/README.md +++ b/ports/esp8266/README.md @@ -27,52 +27,106 @@ Documentation is available at http://docs.micropython.org/en/latest/esp8266/quic Build instructions ------------------ -You need the esp-open-sdk toolchain (which provides both the compiler and libraries), which -you can obtain using one of the following two options: - - - Use a Docker image with a pre-built toolchain (**recommended**). - To use this, install Docker, then prepend - `docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk ` to the start - of the mpy-cross and firmware `make` commands below. This will run the commands using the - toolchain inside the container but using the files on your local filesystem. - - - or, install the esp-open-sdk directly on your PC, which can be found at - . Clone this repository and - run `make` in its directory to build and install the SDK locally. Make sure - to add toolchain bin directory to your PATH. Read esp-open-sdk's README for - additional important information on toolchain setup. - If you use this approach, then the command below will work exactly. - -Add the external dependencies to the MicroPython repository checkout: +You need the esp-open-sdk toolchain, which provides both the compiler and libraries. + +There are two ways to do this: + - By running the toolchain in [Docker](https://www.docker.com/) (**recommended**). + - By installing a pre-built toolchain and adding it to your `$PATH`. + +Regardless of which toolchain you use, the first step is to make sure required +submodules are available: + ```bash $ make -C ports/esp8266 submodules ``` + See the README in the repository root for more information about external dependencies. -The MicroPython cross-compiler must be built to pre-compile some of the -built-in scripts to bytecode. This can be done using: +__Building with Docker__ + +Once you have installed Docker, you can run all of the following build +commands inside the Docker container by prefixing them with `docker +run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk ...command...`. +This will automatically download the Docker image provided by @larsks which +contains the full toolchain and SDK. + +Then you need to compile the MicroPython cross-compiler (`mpy-cross`). From +the root of this repository, run: + ```bash -$ make -C mpy-cross +$ docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -C mpy-cross +``` + +**Note:** The `mpy-cross` binary will likely only work inside the Docker +container. This will not be a problem if you're only building ESP8266 +firmware, but if you're also working on other ports then you will need to +recompile for your host when switching between ports. To avoid this, use +the local toolchain instead. + +Then to compile the ESP8266 firmware: + +``` +$ cd ports/esp8266 +$ docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -j BOARD=GENERIC +``` + +This will produce binary images in the `build-GENERIC/` subdirectory. +Substitute the board for whichever board you're using. + +__Building with a local toolchain__ + +First download the pre-built toolchain (thanks to @jepler from Adafruit). You +will need to find somewhere to put it in your filesystem, e.g. `~/espressif`. +Create that directory first if necessary. + +``` +$ cd ~/espressif # Change as necessary +$ wget https://github.com/jepler/esp-open-sdk/releases/download/2018-06-10/xtensa-lx106-elf-standalone.tar.gz +$ tar zxvf xtensa-lx106-elf-standalone.tar.gz +$ rm xtensa-lx106-elf/bin/esptool.py # Use system version of esptool.py instead. +``` + +Then append this to your `$PATH` variable so the compiler binaries can be +found: + +``` +$ export "PATH=$HOME/espressif/xtensa-lx106-elf/bin/:$PATH" ``` -(Prepend the Docker command if using Docker, see above) -Then, to build MicroPython for the ESP8266, just run: +(You will need to do this each time you start a new terminal) + +Then you need to compile the MicroPython cross-compiler (`mpy-cross`). From +the root of this repository, run: + ```bash +$ make -C mpy-cross +``` + +Then to compile the ESP8266 firmware: + +``` $ cd ports/esp8266 -$ make +$ make -j BOARD=GENERIC ``` -(Prepend the Docker command if using Docker, see above) -This will produce binary images in the `build-GENERIC/` subdirectory. If you -install MicroPython to your module for the first time, or after installing any -other firmware, you should erase flash completely: +This will produce binary images in the `build-GENERIC/` subdirectory. +Substitute the board for whichever board you're using. + + +Installing MicroPython +---------------------- + +To communicate with the board you will need to install `esptool.py`. This can +be obtained from your system package manager or from PyPi via `pip`. + +If you install MicroPython to your module for the first time, or after +installing any other firmware, you should erase flash completely: + ```bash $ esptool.py --port /dev/ttyXXX erase_flash ``` -You can install esptool.py either from your system package manager or from PyPi. - Erasing the flash is also useful as a troubleshooting measure, if a module doesn't behave as expected. @@ -80,7 +134,9 @@ To flash MicroPython image to your ESP8266, use: ```bash $ make deploy ``` -(This should not be run inside Docker as it will need access to the serial port.) + +(If using the Docker instructions above, do not run this command via Docker as +it will need access to the serial port. Run it directly instead.) This will use the `esptool.py` script to download the images. You must have your ESP module in the bootloader mode, and connected to a serial port on your PC. From 8a0ee5a5c04e83f04d1c62029ac5ba7c74856507 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 22 Aug 2022 17:08:05 +1000 Subject: [PATCH 2533/3533] py/objstr: Split mp_obj_str_from_vstr into bytes/str versions. Previously the desired output type was specified. Now make the type part of the function name. Because this function is used in a few places this saves code size due to smaller call-site. This makes `mp_obj_new_str_type_from_vstr` a private function of objstr.c (which is almost the only place where the output type isn't a compile-time constant). This saves ~140 bytes on PYBV11. Signed-off-by: Jim Mussared --- extmod/machine_i2c.c | 4 ++-- extmod/machine_spi.c | 2 +- extmod/modlwip.c | 4 ++-- extmod/modubinascii.c | 4 ++-- extmod/moducryptolib.c | 2 +- extmod/moduhashlib.c | 12 +++++----- extmod/modujson.c | 4 ++-- extmod/modure.c | 6 ++++- extmod/modusocket.c | 4 ++-- extmod/vfs.c | 2 +- ports/cc3200/mods/moduos.c | 2 +- ports/cc3200/mods/modusocket.c | 4 ++-- ports/cc3200/mods/pybi2c.c | 4 ++-- ports/cc3200/mods/pybspi.c | 2 +- ports/esp32/modsocket.c | 2 +- ports/esp32/moduos.c | 2 +- ports/esp8266/moduos.c | 2 +- ports/mimxrt/moduos.c | 2 +- ports/nrf/modules/machine/spi.c | 2 +- ports/nrf/modules/uos/moduos.c | 2 +- ports/rp2/moduos.c | 2 +- ports/stm32/moduos.c | 2 +- ports/stm32/pyb_i2c.c | 4 ++-- ports/stm32/pyb_spi.c | 4 ++-- ports/stm32/usb.c | 4 ++-- ports/unix/main.c | 4 ++-- ports/unix/moduos.c | 2 +- ports/unix/modusocket.c | 2 +- ports/zephyr/modusocket.c | 2 +- py/modbuiltins.c | 4 ++-- py/modstruct.c | 2 +- py/obj.h | 3 ++- py/objint.c | 2 +- py/objstr.c | 40 ++++++++++++++++++++------------- py/parsenum.c | 2 +- py/persistentcode.c | 6 ++++- py/stream.c | 22 +++++++++++++----- 37 files changed, 102 insertions(+), 73 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 452f07a2ebdb8..2aa217914bfb5 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -417,7 +417,7 @@ STATIC mp_obj_t machine_i2c_readfrom(size_t n_args, const mp_obj_t *args) { if (ret < 0) { mp_raise_OSError(-ret); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_obj, 3, 4, machine_i2c_readfrom); @@ -581,7 +581,7 @@ STATIC mp_obj_t machine_i2c_readfrom_mem(size_t n_args, const mp_obj_t *pos_args mp_raise_OSError(-ret); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_readfrom_mem_obj, 1, machine_i2c_readfrom_mem); diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index f160f97d04b4f..ba533b2a651a0 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -70,7 +70,7 @@ STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) { vstr_init_len(&vstr, mp_obj_get_int(args[1])); memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len); mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read); diff --git a/extmod/modlwip.c b/extmod/modlwip.c index c6ee02132ff21..f9451a0ce4399 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1198,7 +1198,7 @@ STATIC mp_obj_t lwip_socket_recv(mp_obj_t self_in, mp_obj_t len_in) { return mp_const_empty_bytes; } vstr.len = ret; - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(lwip_socket_recv_obj, lwip_socket_recv); @@ -1271,7 +1271,7 @@ STATIC mp_obj_t lwip_socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) { tuple[0] = mp_const_empty_bytes; } else { vstr.len = ret; - tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + tuple[0] = mp_obj_new_bytes_from_vstr(&vstr); } tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG); return mp_obj_new_tuple(2, tuple); diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index 49c8d1fb971e5..45a39c58d443b 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -104,7 +104,7 @@ STATIC mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) { mp_raise_ValueError(MP_ERROR_TEXT("incorrect padding")); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64); @@ -166,7 +166,7 @@ STATIC mp_obj_t mod_binascii_b2a_base64(size_t n_args, const mp_obj_t *pos_args, if (newline) { *out = '\n'; } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_binascii_b2a_base64_obj, 1, mod_binascii_b2a_base64); diff --git a/extmod/moducryptolib.c b/extmod/moducryptolib.c index c58abaada67e0..dc0ecb9b20f1d 100644 --- a/extmod/moducryptolib.c +++ b/extmod/moducryptolib.c @@ -329,7 +329,7 @@ STATIC mp_obj_t aes_process(size_t n_args, const mp_obj_t *args, bool encrypt) { if (out_buf != MP_OBJ_NULL) { return out_buf; } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC mp_obj_t ucryptolib_aes_encrypt(size_t n_args, const mp_obj_t *args) { diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 39488788f2712..7eae06b77d341 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -109,7 +109,7 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { vstr_t vstr; vstr_init_len(&vstr, 32); mbedtls_sha256_finish_ret((mbedtls_sha256_context *)&self->state, (unsigned char *)vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #else @@ -143,7 +143,7 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { vstr_t vstr; vstr_init_len(&vstr, SHA256_BLOCK_SIZE); sha256_final((CRYAL_SHA256_CTX *)self->state, (byte *)vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #endif @@ -196,7 +196,7 @@ STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) { vstr_t vstr; vstr_init_len(&vstr, SHA1_SIZE); SHA1_Final((byte *)vstr.buf, (SHA1_CTX *)self->state); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #endif @@ -237,7 +237,7 @@ STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) { vstr_init_len(&vstr, 20); mbedtls_sha1_finish_ret((mbedtls_sha1_context *)self->state, (byte *)vstr.buf); mbedtls_sha1_free((mbedtls_sha1_context *)self->state); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #endif @@ -289,7 +289,7 @@ STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) { vstr_t vstr; vstr_init_len(&vstr, MD5_SIZE); MD5_Final((byte *)vstr.buf, (MD5_CTX *)self->state); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #endif // MICROPY_SSL_AXTLS @@ -330,7 +330,7 @@ STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) { vstr_init_len(&vstr, 16); mbedtls_md5_finish_ret((mbedtls_md5_context *)self->state, (byte *)vstr.buf); mbedtls_md5_free((mbedtls_md5_context *)self->state); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } #endif // MICROPY_SSL_MBEDTLS diff --git a/extmod/modujson.c b/extmod/modujson.c index 9a73e4501e7c0..57c50deec028c 100644 --- a/extmod/modujson.c +++ b/extmod/modujson.c @@ -67,7 +67,7 @@ STATIC mp_obj_t mod_ujson_dump_helper(size_t n_args, const mp_obj_t *pos_args, m vstr_t vstr; vstr_init_print(&vstr, 8, &print_ext.base); mp_obj_print_helper(&print_ext.base, pos_args[0], PRINT_JSON); - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } else { // dump(obj, stream) print_ext.base.data = MP_OBJ_TO_PTR(pos_args[1]); @@ -103,7 +103,7 @@ STATIC mp_obj_t mod_ujson_dumps(mp_obj_t obj) { mp_print_t print; vstr_init_print(&vstr, 8, &print); mp_obj_print_helper(&print, obj, PRINT_JSON); - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_dumps_obj, mod_ujson_dumps); diff --git a/extmod/modure.c b/extmod/modure.c index 6bd89898431ff..59fbfddbf24a0 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -372,7 +372,11 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { // Add post-match string vstr_add_strn(&vstr_return, subj.begin, subj.end - subj.begin); - return mp_obj_new_str_from_vstr(mp_obj_get_type(where), &vstr_return); + if (mp_obj_get_type(where) == &mp_type_str) { + return mp_obj_new_str_from_vstr(&vstr_return); + } else { + return mp_obj_new_bytes_from_vstr(&vstr_return); + } } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_sub_obj, 3, 5, re_sub_helper); diff --git a/extmod/modusocket.c b/extmod/modusocket.c index 638ab0cf28c0a..4d72531160e6a 100644 --- a/extmod/modusocket.c +++ b/extmod/modusocket.c @@ -296,7 +296,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { return mp_const_empty_bytes; } vstr.len = ret; - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); @@ -347,7 +347,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) { tuple[0] = mp_const_empty_bytes; } else { vstr.len = ret; - tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + tuple[0] = mp_obj_new_bytes_from_vstr(&vstr); } tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG); return mp_obj_new_tuple(2, tuple); diff --git a/extmod/vfs.c b/extmod/vfs.c index be1a82d404474..0ef20e9281088 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -360,7 +360,7 @@ mp_obj_t mp_vfs_getcwd(void) { if (!(cwd[0] == '/' && cwd[1] == 0)) { vstr_add_str(&vstr, cwd); } - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd); diff --git a/ports/cc3200/mods/moduos.c b/ports/cc3200/mods/moduos.c index cd01b59b35d5c..e284e9eb1ffb4 100644 --- a/ports/cc3200/mods/moduos.c +++ b/ports/cc3200/mods/moduos.c @@ -116,7 +116,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { for (int i = 0; i < n; i++) { vstr.buf[i] = rng_get(); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); diff --git a/ports/cc3200/mods/modusocket.c b/ports/cc3200/mods/modusocket.c index 32849c51d47c6..23982eb96602c 100644 --- a/ports/cc3200/mods/modusocket.c +++ b/ports/cc3200/mods/modusocket.c @@ -581,7 +581,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { } vstr.len = ret; vstr.buf[vstr.len] = '\0'; - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); @@ -625,7 +625,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) { } else { vstr.len = ret; vstr.buf[vstr.len] = '\0'; - tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + tuple[0] = mp_obj_new_bytes_from_vstr(&vstr); } tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE); return mp_obj_new_tuple(2, tuple); diff --git a/ports/cc3200/mods/pybi2c.c b/ports/cc3200/mods/pybi2c.c index 5310176091a96..40b3e2a207146 100644 --- a/ports/cc3200/mods/pybi2c.c +++ b/ports/cc3200/mods/pybi2c.c @@ -392,7 +392,7 @@ STATIC mp_obj_t pyb_i2c_readfrom(size_t n_args, const mp_obj_t *pos_args, mp_map pyb_i2c_read_into(args, &vstr); // return the received data - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_obj, 3, pyb_i2c_readfrom); @@ -456,7 +456,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem(size_t n_args, const mp_obj_t *pos_args, mp vstr_t vstr; pyb_i2c_readmem_into (args, &vstr); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 1, pyb_i2c_readfrom_mem); diff --git a/ports/cc3200/mods/pybspi.c b/ports/cc3200/mods/pybspi.c index 61b1a6bd11eeb..ed03b09060811 100644 --- a/ports/cc3200/mods/pybspi.c +++ b/ports/cc3200/mods/pybspi.c @@ -305,7 +305,7 @@ STATIC mp_obj_t pyb_spi_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t * pybspi_transfer(self, NULL, vstr.buf, vstr.len, &write); // return the received data - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_read_obj, 1, pyb_spi_read); diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 819ea26e98006..a281558241dca 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -552,7 +552,7 @@ mp_obj_t _socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in, } vstr.len = ret; - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { diff --git a/ports/esp32/moduos.c b/ports/esp32/moduos.c index f307a9ffe05b1..bdfd19c5d20ad 100644 --- a/ports/esp32/moduos.c +++ b/ports/esp32/moduos.c @@ -45,7 +45,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { vstr.buf[i] = r; r >>= 8; } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); diff --git a/ports/esp8266/moduos.c b/ports/esp8266/moduos.c index 9a235e61b9b9f..a023796fd3b6c 100644 --- a/ports/esp8266/moduos.c +++ b/ports/esp8266/moduos.c @@ -48,7 +48,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { for (int i = 0; i < n; i++) { vstr.buf[i] = *WDEV_HWRNG; } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); diff --git a/ports/mimxrt/moduos.c b/ports/mimxrt/moduos.c index 34bd2b4248769..ddb4826db9cf3 100644 --- a/ports/mimxrt/moduos.c +++ b/ports/mimxrt/moduos.c @@ -63,7 +63,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { trng_start(); TRNG_GetRandomData(TRNG, vstr.buf, n); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); #endif diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c index 880d946a29f95..81bc151bebd63 100644 --- a/ports/nrf/modules/machine/spi.c +++ b/ports/nrf/modules/machine/spi.c @@ -388,7 +388,7 @@ STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) { vstr_init_len(&vstr, mp_obj_get_int(args[1])); memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len); spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read); diff --git a/ports/nrf/modules/uos/moduos.c b/ports/nrf/modules/uos/moduos.c index 184f833ef5df9..3fdd8eb19ec15 100644 --- a/ports/nrf/modules/uos/moduos.c +++ b/ports/nrf/modules/uos/moduos.c @@ -108,7 +108,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { for (int i = 0; i < n; i++) { vstr.buf[i] = (uint8_t)(rng_generate_random_word() & 0xFF); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); #endif diff --git a/ports/rp2/moduos.c b/ports/rp2/moduos.c index 6288ff5696e4f..398ec28d92107 100644 --- a/ports/rp2/moduos.c +++ b/ports/rp2/moduos.c @@ -35,6 +35,6 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { for (int i = 0; i < n; i++) { vstr.buf[i] = rosc_random_u8(8); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); diff --git a/ports/stm32/moduos.c b/ports/stm32/moduos.c index 1862564b611cd..85d0edeca8a1f 100644 --- a/ports/stm32/moduos.c +++ b/ports/stm32/moduos.c @@ -58,7 +58,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { for (int i = 0; i < n; i++) { vstr.buf[i] = rng_get(); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); #endif diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index 2ca0793c189e6..436b1c9bc2437 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -926,7 +926,7 @@ STATIC mp_obj_t pyb_i2c_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t * if (o_ret != MP_OBJ_NULL) { return o_ret; } else { - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv); @@ -1002,7 +1002,7 @@ STATIC mp_obj_t pyb_i2c_mem_read(size_t n_args, const mp_obj_t *pos_args, mp_map if (o_ret != MP_OBJ_NULL) { return o_ret; } else { - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read); diff --git a/ports/stm32/pyb_spi.c b/ports/stm32/pyb_spi.c index abb7ba41db0d1..f2cdcebf2a72f 100644 --- a/ports/stm32/pyb_spi.c +++ b/ports/stm32/pyb_spi.c @@ -229,7 +229,7 @@ STATIC mp_obj_t pyb_spi_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t * if (o_ret != MP_OBJ_NULL) { return o_ret; } else { - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_recv_obj, 1, pyb_spi_recv); @@ -298,7 +298,7 @@ STATIC mp_obj_t pyb_spi_send_recv(size_t n_args, const mp_obj_t *pos_args, mp_ma if (o_ret != MP_OBJ_NULL) { return o_ret; } else { - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr_recv); + return mp_obj_new_bytes_from_vstr(&vstr_recv); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_send_recv_obj, 1, pyb_spi_send_recv); diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 2a669b2a28a86..c5fdd88acfb8f 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -813,7 +813,7 @@ STATIC mp_obj_t pyb_usb_vcp_recv(size_t n_args, const mp_obj_t *args, mp_map_t * return mp_obj_new_int(ret); // number of bytes read into given buffer } else { vstr.len = ret; // set actual number of bytes read - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); // create a new buffer + return mp_obj_new_bytes_from_vstr(&vstr); // create a new buffer } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_vcp_recv_obj, 1, pyb_usb_vcp_recv); @@ -1007,7 +1007,7 @@ STATIC mp_obj_t pyb_usb_hid_recv(size_t n_args, const mp_obj_t *args, mp_map_t * return mp_obj_new_int(ret); // number of bytes read into given buffer } else { vstr.len = ret; // set actual number of bytes read - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); // create a new buffer + return mp_obj_new_bytes_from_vstr(&vstr); // create a new buffer } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_hid_recv_obj, 1, pyb_usb_hid_recv); diff --git a/ports/unix/main.c b/ports/unix/main.c index 0b27a1f5f5c8e..c388106a645e1 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -554,7 +554,7 @@ MP_NOINLINE int main_(int argc, char **argv) { vstr_init(&vstr, home_l + (p1 - p - 1) + 1); vstr_add_strn(&vstr, home, home_l); vstr_add_strn(&vstr, p + 1, p1 - p - 1); - path_items[i] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + path_items[i] = mp_obj_new_str_from_vstr(&vstr); } else { path_items[i] = mp_obj_new_str_via_qstr(p, p1 - p); } @@ -650,7 +650,7 @@ MP_NOINLINE int main_(int argc, char **argv) { vstr_init(&vstr, len + sizeof(".__main__")); vstr_add_strn(&vstr, argv[a + 1], len); vstr_add_strn(&vstr, ".__main__", sizeof(".__main__") - 1); - import_args[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + import_args[0] = mp_obj_new_str_from_vstr(&vstr); goto reimport; } diff --git a/ports/unix/moduos.c b/ports/unix/moduos.c index b577dbd297f71..854ff335faa47 100644 --- a/ports/unix/moduos.c +++ b/ports/unix/moduos.c @@ -94,7 +94,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { vstr_t vstr; vstr_init_len(&vstr, n); mp_hal_get_random(n, vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 69ae2a78ebbb2..88e7675a9278f 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -559,7 +559,7 @@ STATIC mp_obj_t mod_socket_inet_ntop(mp_obj_t family_in, mp_obj_t binaddr_in) { mp_raise_OSError(errno); } vstr.len = strlen(vstr.buf); - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_ntop_obj, mod_socket_inet_ntop); diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c index 2b6adf4773343..17cf9babd3333 100644 --- a/ports/zephyr/modusocket.c +++ b/ports/zephyr/modusocket.c @@ -289,7 +289,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { } vstr.len = len; - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); diff --git a/py/modbuiltins.c b/py/modbuiltins.c index f3caccbc83e16..2459edda13cd3 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -250,7 +250,7 @@ STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { if (line.len == 0 && ret == CHAR_CTRL_D) { mp_raise_type(&mp_type_EOFError); } - return mp_obj_new_str_from_vstr(&mp_type_str, &line); + return mp_obj_new_str_from_vstr(&line); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); @@ -467,7 +467,7 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) { mp_print_t print; vstr_init_print(&vstr, 16, &print); mp_obj_print_helper(&print, o_in, PRINT_REPR); - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr); diff --git a/py/modstruct.c b/py/modstruct.c index 69c7279e370e6..ec86d4b9c2976 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -220,7 +220,7 @@ STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) { byte *p = (byte *)vstr.buf; memset(p, 0, size); struct_pack_into_internal(args[0], p, n_args - 1, &args[1]); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack); diff --git a/py/obj.h b/py/obj.h index 645fae79f5665..d171e0fe620e3 100644 --- a/py/obj.h +++ b/py/obj.h @@ -791,7 +791,8 @@ mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-prec mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception) mp_obj_t mp_obj_new_str(const char *data, size_t len); mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); -mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr); +mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); +mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr); mp_obj_t mp_obj_new_bytes(const byte *data, size_t len); mp_obj_t mp_obj_new_bytearray(size_t n, const void *items); mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items); diff --git a/py/objint.c b/py/objint.c index 740e7ea948df8..645b26996689c 100644 --- a/py/objint.c +++ b/py/objint.c @@ -446,7 +446,7 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes); diff --git a/py/objstr.c b/py/objstr.c index 45dbb9b3ebde3..69745a2f586ac 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -41,6 +41,8 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in); +STATIC mp_obj_t mp_obj_new_str_type_from_vstr(const mp_obj_type_t *type, vstr_t *vstr); + STATIC void str_check_arg_type(const mp_obj_type_t *self_type, const mp_obj_t arg) { // String operations generally need the args type to match the object they're called on, // e.g. str.find(str), byte.startswith(byte) @@ -170,7 +172,7 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ mp_print_t print; vstr_init_print(&vstr, 16, &print); mp_obj_print_helper(&print, args[0], PRINT_STR); - return mp_obj_new_str_from_vstr(type, &vstr); + return mp_obj_new_str_type_from_vstr(type, &vstr); } default: // 2 or 3 args @@ -256,7 +258,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size vstr_t vstr; vstr_init_len(&vstr, len); memset(vstr.buf, 0, len); - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } // check if argument has the buffer protocol @@ -288,7 +290,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size vstr_add_byte(&vstr, val); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); wrong_args: mp_raise_TypeError(MP_ERROR_TEXT("wrong number of arguments")); @@ -363,7 +365,7 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i vstr_t vstr; vstr_init_len(&vstr, lhs_len * n); mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, vstr.buf); - return mp_obj_new_str_from_vstr(lhs_type, &vstr); + return mp_obj_new_str_type_from_vstr(lhs_type, &vstr); } // From now on all operations allow: @@ -414,7 +416,7 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i vstr_init_len(&vstr, lhs_len + rhs_len); memcpy(vstr.buf, lhs_data, lhs_len); memcpy(vstr.buf + lhs_len, rhs_data, rhs_len); - return mp_obj_new_str_from_vstr(lhs_type, &vstr); + return mp_obj_new_str_type_from_vstr(lhs_type, &vstr); } case MP_BINARY_OP_CONTAINS: @@ -528,7 +530,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { } // return joined string - return mp_obj_new_str_from_vstr(ret_type, &vstr); + return mp_obj_new_str_type_from_vstr(ret_type, &vstr); } MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join); @@ -918,7 +920,7 @@ STATIC mp_obj_t str_center(mp_obj_t str_in, mp_obj_t width_in) { memset(vstr.buf, ' ', width); int left = (width - str_len) / 2; memcpy(vstr.buf + left, str, str_len); - return mp_obj_new_str_from_vstr(mp_obj_get_type(str_in), &vstr); + return mp_obj_new_str_type_from_vstr(mp_obj_get_type(str_in), &vstr); } MP_DEFINE_CONST_FUN_OBJ_2(str_center_obj, str_center); #endif @@ -1144,7 +1146,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar mp_print_t arg_print; vstr_init_print(&arg_vstr, 16, &arg_print); mp_obj_print_helper(&arg_print, arg, print_kind); - arg = mp_obj_new_str_from_vstr(&mp_type_str, &arg_vstr); + arg = mp_obj_new_str_type_from_vstr(&mp_type_str, &arg_vstr); } char fill = '\0'; @@ -1429,7 +1431,7 @@ mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs GET_STR_DATA_LEN(args[0], str, len); int arg_i = 0; vstr_t vstr = mp_obj_str_format_helper((const char *)str, (const char *)str + len, &arg_i, n_args, args, kwargs); - return mp_obj_new_str_from_vstr(mp_obj_get_type(args[0]), &vstr); + return mp_obj_new_str_type_from_vstr(mp_obj_get_type(args[0]), &vstr); } MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format); @@ -1634,7 +1636,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ mp_raise_TypeError(MP_ERROR_TEXT("format string didn't convert all arguments")); } - return mp_obj_new_str_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr); + return mp_obj_new_str_type_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr); } #endif @@ -1737,7 +1739,7 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) { } } - return mp_obj_new_str_from_vstr(self_type, &vstr); + return mp_obj_new_str_type_from_vstr(self_type, &vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); @@ -1849,7 +1851,7 @@ STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) { for (size_t i = 0; i < self_len; i++) { *data++ = op(*self_data++); } - return mp_obj_new_str_from_vstr(mp_obj_get_type(self_in), &vstr); + return mp_obj_new_str_type_from_vstr(mp_obj_get_type(self_in), &vstr); } STATIC mp_obj_t str_lower(mp_obj_t self_in) { @@ -1988,7 +1990,7 @@ mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type *out++ = *sep; } } - return mp_obj_new_str_from_vstr(type, &vstr); + return mp_obj_new_str_type_from_vstr(type, &vstr); } mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data) { @@ -2016,7 +2018,7 @@ mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data) { hex_byte = 0; } } - return mp_obj_new_str_from_vstr(MP_OBJ_TO_PTR(type_in), &vstr); + return mp_obj_new_str_type_from_vstr(MP_OBJ_TO_PTR(type_in), &vstr); } STATIC mp_obj_t bytes_hex_as_str(size_t n_args, const mp_obj_t *args) { @@ -2213,7 +2215,7 @@ mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len) { // Create a str/bytes object from the given vstr. The vstr buffer is resized to // the exact length required and then reused for the str/bytes object. The vstr // is cleared and can safely be passed to vstr_free if it was heap allocated. -mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) { +STATIC mp_obj_t mp_obj_new_str_type_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) { // if not a bytes object, look if a qstr with this data already exists if (type == &mp_type_str) { qstr q = qstr_find_strn(vstr->buf, vstr->len); @@ -2245,6 +2247,14 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) { return MP_OBJ_FROM_PTR(o); } +mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr) { + return mp_obj_new_str_type_from_vstr(&mp_type_str, vstr); +} + +mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr) { + return mp_obj_new_str_type_from_vstr(&mp_type_bytes, vstr); +} + mp_obj_t mp_obj_new_str(const char *data, size_t len) { qstr q = qstr_find_strn(data, len); if (q != MP_QSTRnull) { diff --git a/py/parsenum.c b/py/parsenum.c index 19cc719201400..79765f84f64fb 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -160,7 +160,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m mp_printf(&print, "invalid syntax for integer with base %d: ", base); mp_str_print_quoted(&print, str_val_start, top - str_val_start, true); mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError, - mp_obj_new_str_from_vstr(&mp_type_str, &vstr)); + mp_obj_new_str_from_vstr(&vstr)); raise_exc(exc, lex); #endif } diff --git a/py/persistentcode.c b/py/persistentcode.c index a8e74295504e0..995dedb4569d0 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -202,7 +202,11 @@ STATIC mp_obj_t load_obj(mp_reader_t *reader) { read_bytes(reader, (byte *)vstr.buf, len); if (obj_type == MP_PERSISTENT_OBJ_STR || obj_type == MP_PERSISTENT_OBJ_BYTES) { read_byte(reader); // skip null terminator - return mp_obj_new_str_from_vstr(obj_type == MP_PERSISTENT_OBJ_STR ? &mp_type_str : &mp_type_bytes, &vstr); + if (obj_type == MP_PERSISTENT_OBJ_STR) { + return mp_obj_new_str_from_vstr(&vstr); + } else { + return mp_obj_new_bytes_from_vstr(&vstr); + } } else if (obj_type == MP_PERSISTENT_OBJ_INT) { return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL); } else { diff --git a/py/stream.c b/py/stream.c index dce64a44dbeda..87bed38f6778f 100644 --- a/py/stream.c +++ b/py/stream.c @@ -40,8 +40,6 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in); -#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes) - // Returns error condition in *errcode, if non-zero, return value is number of bytes written // before error condition occurred. If *errcode == 0, returns total bytes written (which will // be equal to input size). @@ -190,7 +188,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl } } - return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + return mp_obj_new_str_from_vstr(&vstr); } #endif @@ -211,7 +209,11 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl mp_raise_OSError(error); } else { vstr.len = out_sz; - return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr); + if (stream_p->is_text) { + return mp_obj_new_str_from_vstr(&vstr); + } else { + return mp_obj_new_bytes_from_vstr(&vstr); + } } } @@ -337,7 +339,11 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) { } vstr.len = total_size; - return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr); + if (stream_p->is_text) { + return mp_obj_new_str_from_vstr(&vstr); + } else { + return mp_obj_new_bytes_from_vstr(&vstr); + } } // Unbuffered, inefficient implementation of readline() for raw I/O files. @@ -390,7 +396,11 @@ STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) } } - return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr); + if (stream_p->is_text) { + return mp_obj_new_str_from_vstr(&vstr); + } else { + return mp_obj_new_bytes_from_vstr(&vstr); + } } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); From 88864587f5af292d7f86aceb6bf40e8331e9a8d6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 26 Aug 2022 12:54:53 +1000 Subject: [PATCH 2534/3533] py/objstr: Always ensure mp_obj_str_from_vstr is unicode-safe. Now that we have `mp_obj_new_str_type_from_vstr` (private helper used by objstr.c) split from the public API (`mp_obj_new_str_from_vstr`), we can enforce a unicode check at the public API without incurring a performance cost on the various objstr.c methods (which are already working on known unicode-safe strings). Signed-off-by: Jim Mussared --- py/objstr.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index 69745a2f586ac..ab1229ad66a6c 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2248,6 +2248,11 @@ STATIC mp_obj_t mp_obj_new_str_type_from_vstr(const mp_obj_type_t *type, vstr_t } mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr) { + #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK + if (!utf8_check((byte *)vstr->buf, vstr->len)) { + mp_raise_msg(&mp_type_UnicodeError, NULL); + } + #endif // MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK return mp_obj_new_str_type_from_vstr(&mp_type_str, vstr); } From 3a910b15650636efc58bce48cc1bfa0debfd375c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 23 Aug 2022 18:35:37 +1000 Subject: [PATCH 2535/3533] py/objstr: Optimise mp_obj_new_str_from_vstr for known-safe strings. The new `mp_obj_new_str_from_utf8_vstr` can be used when you know you already have a unicode-safe string. Signed-off-by: Jim Mussared --- extmod/modujson.c | 4 ++-- extmod/modure.c | 2 +- ports/unix/modusocket.c | 2 +- py/modbuiltins.c | 2 +- py/obj.h | 5 +++++ py/objstr.c | 7 +++++++ py/parsenum.c | 2 +- py/persistentcode.c | 2 +- 8 files changed, 19 insertions(+), 7 deletions(-) diff --git a/extmod/modujson.c b/extmod/modujson.c index 57c50deec028c..4e992e2245d18 100644 --- a/extmod/modujson.c +++ b/extmod/modujson.c @@ -67,7 +67,7 @@ STATIC mp_obj_t mod_ujson_dump_helper(size_t n_args, const mp_obj_t *pos_args, m vstr_t vstr; vstr_init_print(&vstr, 8, &print_ext.base); mp_obj_print_helper(&print_ext.base, pos_args[0], PRINT_JSON); - return mp_obj_new_str_from_vstr(&vstr); + return mp_obj_new_str_from_utf8_vstr(&vstr); } else { // dump(obj, stream) print_ext.base.data = MP_OBJ_TO_PTR(pos_args[1]); @@ -103,7 +103,7 @@ STATIC mp_obj_t mod_ujson_dumps(mp_obj_t obj) { mp_print_t print; vstr_init_print(&vstr, 8, &print); mp_obj_print_helper(&print, obj, PRINT_JSON); - return mp_obj_new_str_from_vstr(&vstr); + return mp_obj_new_str_from_utf8_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_dumps_obj, mod_ujson_dumps); diff --git a/extmod/modure.c b/extmod/modure.c index 59fbfddbf24a0..799fef13b1bdb 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -373,7 +373,7 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { vstr_add_strn(&vstr_return, subj.begin, subj.end - subj.begin); if (mp_obj_get_type(where) == &mp_type_str) { - return mp_obj_new_str_from_vstr(&vstr_return); + return mp_obj_new_str_from_utf8_vstr(&vstr_return); } else { return mp_obj_new_bytes_from_vstr(&vstr_return); } diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 88e7675a9278f..72c70d1750b53 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -559,7 +559,7 @@ STATIC mp_obj_t mod_socket_inet_ntop(mp_obj_t family_in, mp_obj_t binaddr_in) { mp_raise_OSError(errno); } vstr.len = strlen(vstr.buf); - return mp_obj_new_str_from_vstr(&vstr); + return mp_obj_new_str_from_utf8_vstr(&vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_socket_inet_ntop_obj, mod_socket_inet_ntop); diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 2459edda13cd3..f74db95cf5a54 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -467,7 +467,7 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) { mp_print_t print; vstr_init_print(&vstr, 16, &print); mp_obj_print_helper(&print, o_in, PRINT_REPR); - return mp_obj_new_str_from_vstr(&vstr); + return mp_obj_new_str_from_utf8_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr); diff --git a/py/obj.h b/py/obj.h index d171e0fe620e3..0b0a9692ed53a 100644 --- a/py/obj.h +++ b/py/obj.h @@ -792,6 +792,11 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a mp_obj_t mp_obj_new_str(const char *data, size_t len); mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); +#if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK +mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // only use when vstr is already known to be utf-8 encoded +#else +#define mp_obj_new_str_from_utf8_vstr mp_obj_new_str_from_vstr +#endif mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr); mp_obj_t mp_obj_new_bytes(const byte *data, size_t len); mp_obj_t mp_obj_new_bytearray(size_t n, const void *items); diff --git a/py/objstr.c b/py/objstr.c index ab1229ad66a6c..5a62237516c3d 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2256,6 +2256,13 @@ mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr) { return mp_obj_new_str_type_from_vstr(&mp_type_str, vstr); } +#if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK +mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr) { + // bypasses utf8_check. + return mp_obj_new_str_type_from_vstr(&mp_type_str, vstr); +} +#endif // MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK + mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr) { return mp_obj_new_str_type_from_vstr(&mp_type_bytes, vstr); } diff --git a/py/parsenum.c b/py/parsenum.c index 79765f84f64fb..d59715fdc9379 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -160,7 +160,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m mp_printf(&print, "invalid syntax for integer with base %d: ", base); mp_str_print_quoted(&print, str_val_start, top - str_val_start, true); mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError, - mp_obj_new_str_from_vstr(&vstr)); + mp_obj_new_str_from_utf8_vstr(&vstr)); raise_exc(exc, lex); #endif } diff --git a/py/persistentcode.c b/py/persistentcode.c index 995dedb4569d0..82ef0c49953ff 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -203,7 +203,7 @@ STATIC mp_obj_t load_obj(mp_reader_t *reader) { if (obj_type == MP_PERSISTENT_OBJ_STR || obj_type == MP_PERSISTENT_OBJ_BYTES) { read_byte(reader); // skip null terminator if (obj_type == MP_PERSISTENT_OBJ_STR) { - return mp_obj_new_str_from_vstr(&vstr); + return mp_obj_new_str_from_utf8_vstr(&vstr); } else { return mp_obj_new_bytes_from_vstr(&vstr); } From 6c3d8d38bfbe24c3d810f89f546ac947cf0cb66d Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 24 Aug 2022 12:22:57 +1000 Subject: [PATCH 2536/3533] py/objstr: Always validate utf-8 for mp_obj_new_str. All uses of this are either tiny strings or not-known-to-be-safe. Update comments for mp_obj_new_str_copy and mp_obj_new_str_of_type. Signed-off-by: Jim Mussared --- py/obj.h | 8 ++++---- py/objstr.c | 11 ++++++----- py/objstr.h | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/py/obj.h b/py/obj.h index 0b0a9692ed53a..e048cf0c286af 100644 --- a/py/obj.h +++ b/py/obj.h @@ -789,11 +789,11 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value); mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base); mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception) mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception) -mp_obj_t mp_obj_new_str(const char *data, size_t len); -mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); -mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); +mp_obj_t mp_obj_new_str(const char *data, size_t len); // will check utf-8 (raises UnicodeError) +mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8 +mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError) #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK -mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // only use when vstr is already known to be utf-8 encoded +mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // input data must be valid utf-8 #else #define mp_obj_new_str_from_utf8_vstr mp_obj_new_str_from_vstr #endif diff --git a/py/objstr.c b/py/objstr.c index 5a62237516c3d..683d035e44090 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -202,11 +202,7 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ } else { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); - #if MICROPY_PY_BUILTINS_STR_UNICODE_CHECK - if (!utf8_check(bufinfo.buf, bufinfo.len)) { - mp_raise_msg(&mp_type_UnicodeError, NULL); - } - #endif + // This will utf-8 check the input. return mp_obj_new_str(bufinfo.buf, bufinfo.len); } } @@ -2268,6 +2264,11 @@ mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr) { } mp_obj_t mp_obj_new_str(const char *data, size_t len) { + #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK + if (!utf8_check((byte *)data, len)) { + mp_raise_msg(&mp_type_UnicodeError, NULL); + } + #endif qstr q = qstr_find_strn(data, len); if (q != MP_QSTRnull) { // qstr with this data already exists diff --git a/py/objstr.h b/py/objstr.h index 78441fedaf2cb..6c6735bf5efa0 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -88,8 +88,8 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t void mp_str_print_json(const mp_print_t *print, const byte *str_data, size_t str_len); mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args); -mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte *data, size_t len); -mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len); +mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte *data, size_t len); // for type=str, input data must be valid utf-8 +mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len); // for type=str, will check utf-8 (raises UnicodeError) mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); From bd4e45fd68e20af3f965b403e643fa5f71aa1b08 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 24 Aug 2022 12:42:45 +1000 Subject: [PATCH 2537/3533] tests/unicode: Add test for invalid utf-8 file contents. Signed-off-by: Jim Mussared --- tests/unicode/data/utf-8_invalid.txt | 1 + tests/unicode/file_invalid.py | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 tests/unicode/data/utf-8_invalid.txt create mode 100644 tests/unicode/file_invalid.py diff --git a/tests/unicode/data/utf-8_invalid.txt b/tests/unicode/data/utf-8_invalid.txt new file mode 100644 index 0000000000000..50020482bf6e9 --- /dev/null +++ b/tests/unicode/data/utf-8_invalid.txt @@ -0,0 +1 @@ +aaÿbb diff --git a/tests/unicode/file_invalid.py b/tests/unicode/file_invalid.py new file mode 100644 index 0000000000000..3f7f1840629f8 --- /dev/null +++ b/tests/unicode/file_invalid.py @@ -0,0 +1,5 @@ +try: + f = open("unicode/data/utf-8_invalid.txt", encoding="utf-8") + f.read() +except UnicodeError: + print("UnicodeError") From d521899e18c305a4cf3870449e3dd12308f7a806 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 17 Aug 2022 08:43:17 +1000 Subject: [PATCH 2538/3533] py/persistentcode: Clarify ValueError when native emitter disabled. --- py/persistentcode.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/py/persistentcode.c b/py/persistentcode.c index 82ef0c49953ff..1b1741f2614e2 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -402,7 +402,13 @@ mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t * if (MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE) { byte arch = MPY_FEATURE_DECODE_ARCH(header[2]); if (!MPY_FEATURE_ARCH_TEST(arch)) { - mp_raise_ValueError(MP_ERROR_TEXT("incompatible .mpy arch")); + if (MPY_FEATURE_ARCH_TEST(MP_NATIVE_ARCH_NONE)) { + // On supported ports this can be resolved by enabling feature, eg + // mpconfigboard.h: MICROPY_EMIT_THUMB (1) + mp_raise_ValueError(MP_ERROR_TEXT("native code in .mpy unsupported")); + } else { + mp_raise_ValueError(MP_ERROR_TEXT("incompatible .mpy arch")); + } } } From 9a826e0f24add1b7e719e915e0d4dde1c14b416e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Jul 2022 18:48:06 +1000 Subject: [PATCH 2539/3533] lib/lwip: Update lwIP to v2.1.3, tag STABLE-2_1_3_RELEASE. There don't seem to be many changes going from v2.1.2 to v2.1.3 of lwIP. Mostly they are: - IPv6 fixes and improvements - changes to apps and other code that MicroPython doesn't use - comments and tests - minor bug fixes In particular there doesn't look to be any change to the API of any function used by MicroPython. Network multi tests pass on PYBD_SF2 and PYBD_SF6. PYBD_SF2, PYBD_SF6 and PICO_W have unchanged iperf3 performance. Similar results for networking on the mimxrt port. Signed-off-by: Damien George --- lib/lwip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lwip b/lib/lwip index 159e31b689577..6ca936f6b588c 160000 --- a/lib/lwip +++ b/lib/lwip @@ -1 +1 @@ -Subproject commit 159e31b689577dbf69cf0683bbaffbd71fa5ee10 +Subproject commit 6ca936f6b588cee702c638eee75c2436e6cf75de From 730e97509117ea9c8b527857c25f8a2bf04ecd86 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 30 Aug 2022 11:14:32 +1000 Subject: [PATCH 2540/3533] esp32/boards: Merge manifest_release modules into standard manifest. Having two separate manifests is confusing. It's simpler to have the daily builds use the same configuration as the stable, release builds. Signed-off-by: Damien George --- ports/esp32/boards/manifest.py | 6 ++++++ ports/esp32/boards/manifest_release.py | 7 ------- tools/autobuild/build-boards.sh | 7 +------ 3 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 ports/esp32/boards/manifest_release.py diff --git a/ports/esp32/boards/manifest.py b/ports/esp32/boards/manifest.py index f0ed38b515c71..23252442ea460 100644 --- a/ports/esp32/boards/manifest.py +++ b/ports/esp32/boards/manifest.py @@ -6,3 +6,9 @@ include("$(MPY_DIR)/extmod/uasyncio/manifest.py") include("$(MPY_DIR)/extmod/webrepl/manifest.py") include("$(MPY_DIR)/drivers/neopixel/manifest.py") + +# Freeze some micropython-lib modules. +freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") +freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") +freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") +freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") diff --git a/ports/esp32/boards/manifest_release.py b/ports/esp32/boards/manifest_release.py deleted file mode 100644 index 8b9bcde6ffa77..0000000000000 --- a/ports/esp32/boards/manifest_release.py +++ /dev/null @@ -1,7 +0,0 @@ -include("manifest.py") - -freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") - -freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") -freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") -freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") diff --git a/tools/autobuild/build-boards.sh b/tools/autobuild/build-boards.sh index b3d2f8c4c55aa..48c7b81841aa6 100755 --- a/tools/autobuild/build-boards.sh +++ b/tools/autobuild/build-boards.sh @@ -83,12 +83,7 @@ function build_esp32_boards { if idf.py --version | grep -q v4.2; then if [ $mcu = esp32 ]; then # build standard esp32-based boards with IDF v4.2 - if echo $board_json | grep -q GENERIC; then - # traditionally, GENERIC and GENERIC_SPIRAM boards used manifest_release.py - MICROPY_AUTOBUILD_MAKE="$MICROPY_AUTOBUILD_MAKE FROZEN_MANIFEST=$(pwd)/boards/manifest_release.py" build_board $board_json $fw_tag $dest_dir bin elf map - else - build_board $board_json $fw_tag $dest_dir bin elf map - fi + build_board $board_json $fw_tag $dest_dir bin elf map fi else if [ $mcu != esp32 ]; then From d108fc9c4793e1846b2ddfad925fb17bbc44cd51 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 30 Aug 2022 11:18:08 +1000 Subject: [PATCH 2541/3533] esp32/machine_sdcard: Free SPI bus when deiniting SD card. So that everything is reset and the SD card can be created again after calling SDCard.deinit() (and after a soft reset). Fixes issue #8949. Signed-off-by: Damien George --- ports/esp32/machine_sdcard.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 82c2e6cd4e320..3f94356adbb4c 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -283,6 +283,10 @@ STATIC mp_obj_t sd_deinit(mp_obj_t self_in) { { self->host.deinit(); } + if (self->host.flags & SDMMC_HOST_FLAG_SPI) { + // SD card used a (dedicated) SPI bus, so free that SPI bus. + spi_bus_free(self->host.slot); + } self->flags &= ~SDCARD_CARD_FLAGS_HOST_INIT_DONE; } From 64c62f8cf162e56cf4c6049f4a65c3d5e18556ae Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 24 Aug 2022 23:47:58 +1000 Subject: [PATCH 2542/3533] README: Simplify and update, and move unix section to separate file. Changes are: - Remove unix- and stm32-specific sections (move unix to its own README.md), stm32 was duplicated. - Add links to GitHub Discussions and Discord. - Update information about the project. - Add a getting started section. - Explain `make submodules`. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- README.md | 262 ++++++++++++++++++------------------------- ports/unix/README.md | 70 ++++++++++++ 2 files changed, 181 insertions(+), 151 deletions(-) create mode 100644 ports/unix/README.md diff --git a/README.md b/README.md index 6941102f0ac48..6482899b251d9 100644 --- a/README.md +++ b/README.md @@ -15,174 +15,134 @@ code-base, including project-wide name changes and API changes. MicroPython implements the entire Python 3.4 syntax (including exceptions, `with`, `yield from`, etc., and additionally `async`/`await` keywords from -Python 3.5). The following core datatypes are provided: `str` (including -basic Unicode support), `bytes`, `bytearray`, `tuple`, `list`, `dict`, `set`, -`frozenset`, `array.array`, `collections.namedtuple`, classes and instances. -Builtin modules include `sys`, `time`, and `struct`, etc. Select ports have -support for `_thread` module (multithreading). Note that only a subset of -Python 3 functionality is implemented for the data types and modules. - -MicroPython can execute scripts in textual source form or from precompiled -bytecode, in both cases either from an on-device filesystem or "frozen" into -the MicroPython executable. - -See the repository http://github.com/micropython/pyboard for the MicroPython -board (PyBoard), the officially supported reference electronic circuit board. - -Major components in this repository: -- py/ -- the core Python implementation, including compiler, runtime, and +Python 3.5 and some select features from later versions). The following core +datatypes are provided: `str`(including basic Unicode support), `bytes`, +`bytearray`, `tuple`, `list`, `dict`, `set`, `frozenset`, `array.array`, +`collections.namedtuple`, classes and instances. Builtin modules include +`os`, `sys`, `time`, `re`, and `struct`, etc. Select ports have support for +`_thread` module (multithreading), `socket` and `ssl` for networking, and +`asyncio`. Note that only a subset of Python 3 functionality is implemented +for the data types and modules. + +MicroPython can execute scripts in textual source form (.py files) or from +precompiled bytecode (.mpy files), in both cases either from an on-device +filesystem or "frozen" into the MicroPython executable. + +MicroPython also provides a set of MicroPython-specific modules to access +hardware-specific functionality and peripherals such as GPIO, Timers, ADC, +DAC, PWM, SPI, I2C, CAN, Bluetooth, and USB. + +Getting started +--------------- + +See the [online documentation](https://docs.micropython.org/) for API +references and information about using MicroPython and information about how +it is implemented. + +We use [GitHub Discussions](https://github.com/micropython/micropython/discussions) +as our forum, and [Discord](https://discord.gg/RB8HZSAExQ) for chat. These +are great places to ask questions and advice from the community or to discuss your +MicroPython-based projects. + +For bugs and feature requests, please [raise an issue](https://github.com/micropython/micropython/issues/new/choose) +and follow the templates there. + +For information about the [MicroPython pyboard](https://store.micropython.org/pyb-features), +the officially supported board from the +[original Kickstarter campaign](https://www.kickstarter.com/projects/214379695/micro-python-python-for-microcontrollers), +see the [schematics and pinouts](http://github.com/micropython/pyboard) and +[documentation](https://docs.micropython.org/en/latest/pyboard/quickref.html). + +Contributing +------------ + +MicroPython is an open-source project and welcomes contributions. To be +productive, please be sure to follow the +[Contributors' Guidelines](https://github.com/micropython/micropython/wiki/ContributorGuidelines) +and the [Code Conventions](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md). +Note that MicroPython is licenced under the MIT license, and all contributions +should follow this license. + +About this repository +--------------------- + +This repository contains the following components: +- [py/](py/) -- the core Python implementation, including compiler, runtime, and core library. -- mpy-cross/ -- the MicroPython cross-compiler which is used to turn scripts +- [mpy-cross/](mpy-cross/) -- the MicroPython cross-compiler which is used to turn scripts into precompiled bytecode. -- ports/unix/ -- a version of MicroPython that runs on Unix. -- ports/stm32/ -- a version of MicroPython that runs on the PyBoard and similar - STM32 boards (using ST's Cube HAL drivers). -- ports/minimal/ -- a minimal MicroPython port. Start with this if you want - to port MicroPython to another microcontroller. -- tests/ -- test framework and test scripts. -- docs/ -- user documentation in Sphinx reStructuredText format. Rendered - HTML documentation is available at http://docs.micropython.org. - -Additional components: -- ports/bare-arm/ -- a bare minimum version of MicroPython for ARM MCUs. Used - mostly to control code size. -- ports/teensy/ -- a version of MicroPython that runs on the Teensy 3.1 - (preliminary but functional). -- ports/pic16bit/ -- a version of MicroPython for 16-bit PIC microcontrollers. -- ports/cc3200/ -- a version of MicroPython that runs on the CC3200 from TI. -- ports/esp8266/ -- a version of MicroPython that runs on Espressif's ESP8266 SoC. -- ports/esp32/ -- a version of MicroPython that runs on Espressif's ESP32 SoC. -- ports/nrf/ -- a version of MicroPython that runs on Nordic's nRF51 and nRF52 MCUs. -- extmod/ -- additional (non-core) modules implemented in C. -- tools/ -- various tools, including the pyboard.py module. -- examples/ -- a few example Python scripts. - -The subdirectories above may include READMEs with additional info. +- [ports/](ports/) -- platform-specific code for the various ports and architectures that MicroPython runs on. +- [lib/](lib/) -- submodules for external dependencies. +- [tests/](tests/) -- test framework and test scripts. +- [docs/](docs/) -- user documentation in Sphinx reStructuredText format. This is used to generate the [online documentation](http://docs.micropython.org). +- [extmod/](extmod/) -- additional (non-core) modules implemented in C. +- [tools/](tools/) -- various tools, including the pyboard.py module. +- [examples/](examples/) -- a few example Python scripts. "make" is used to build the components, or "gmake" on BSD-based systems. You will also need bash, gcc, and Python 3.3+ available as the command `python3` (if your system only has Python 2.7 then invoke make with the additional option -`PYTHON=python2`). +`PYTHON=python2`). Some ports (rp2 and esp32) additionally use CMake. + +Supported platforms & architectures +----------------------------------- + +MicroPython runs on a wide range of microcontrollers, as well as on Unix-like +(including Linux, BSD, macOS, WSL) and Windows systems. + +Microcontroller targets can be as small as 256kiB flash + 16kiB RAM, although +devices with at least 512kiB flash + 128kiB RAM allow a much more +full-featured experience. + +The [Unix](ports/unix) and [Windows](ports/windows) ports allow both +development and testing of MicroPython itself, as well as providing +lightweight alternative to CPython on these platforms (in particular on +embedded Linux systems). + +The ["minimal"](ports/minimal) port provides an example of a very basic +MicroPython port and can be compiled as both a standalone Linux binary as +well as for ARM Cortex M4. Start with this if you want to port MicroPython to +another microcontroller. Additionally the ["bare-arm"](ports/bare-arm) port +is an example of the absolute minimum configuration, and is used to keep +track of the code size of the core runtime and VM. + +In addition, the following ports are provided in this repository: + - [cc3200](ports/cc3200) -- Texas Instruments CC3200 (including PyCom WiPy). + - [esp32](ports/esp32) -- Espressif ESP32 SoC (including ESP32S2, ESP32S3, ESP32C3). + - [esp8266](ports/esp8266) -- Espressif ESP8266 SoC. + - [mimxrt](ports/mimxrt) -- NXP m.iMX RT (including Teensy 4.x). + - [nrf](ports/nrf) -- Nordic Semiconductor nRF51 and nRF52. + - [pic16bit](ports/pic16bit) -- Microchip PIC 16-bit. + - [powerpc](ports/powerpc) -- IBM PowerPC (including Microwatt) + - [qemu-arm](ports/qemu-arm) -- QEMU-based emulated target, for testing) + - [renesas-ra](ports/renesas-ra) -- Renesas RA family. + - [rp2](ports/rp2) -- Raspberry Pi RP2040 (including Pico and Pico W). + - [samd](ports/samd) -- Microchip (formerly Atmel) SAMD21 and SAMD51. + - [stm32](ports/stm32) -- STMicroelectronics STM32 family (including F0, F4, F7, G0, G4, H7, L0, L4, WB) + - [teensy](ports/teensy) -- Teensy 3.x. + - [webassembly](ports/webassembly) -- Emscripten port targeting browsers and NodeJS. + - [zephyr](ports/zephyr) -- Zephyr RTOS. The MicroPython cross-compiler, mpy-cross ----------------------------------------- -Most ports require the MicroPython cross-compiler to be built first. This -program, called mpy-cross, is used to pre-compile Python scripts to .mpy -files which can then be included (frozen) into the firmware/executable for -a port. To build mpy-cross use: +Most ports require the [MicroPython cross-compiler](mpy-cross) to be built +first. This program, called mpy-cross, is used to pre-compile Python scripts +to .mpy files which can then be included (frozen) into the +firmware/executable for a port. To build mpy-cross use: $ cd mpy-cross $ make -The Unix version ----------------- - -The "unix" port requires a standard Unix environment with gcc and GNU make. -x86 and x64 architectures are supported (i.e. x86 32- and 64-bit), as well -as ARM and MIPS. Making full-featured port to another architecture requires -writing some assembly code for the exception handling and garbage collection. -Alternatively, fallback implementation based on setjmp/longjmp can be used. - -To build (see section below for required dependencies): - - $ cd ports/unix - $ make submodules - $ make - -Then to give it a try: - - $ ./micropython - >>> list(5 * x + y for x in range(10) for y in [4, 2, 1]) - -Use `CTRL-D` (i.e. EOF) to exit the shell. -Learn about command-line options (in particular, how to increase heap size -which may be needed for larger applications): - - $ ./micropython -h - -Run complete testsuite: - - $ make test - -Unix version comes with a builtin package manager called upip, e.g.: - - $ ./micropython -m upip install micropython-pystone - $ ./micropython -m pystone - -Browse available modules on -[PyPI](https://pypi.python.org/pypi?%3Aaction=search&term=micropython). -Standard library modules come from -[micropython-lib](https://github.com/micropython/micropython-lib) project. - External dependencies --------------------- -Building MicroPython ports may require some dependencies installed. - -For Unix port, `libffi` library and `pkg-config` tool are required. On -Debian/Ubuntu/Mint derivative Linux distros, install `build-essential` -(includes toolchain and make), `libffi-dev`, and `pkg-config` packages. - -Other dependencies can be built together with MicroPython. This may -be required to enable extra features or capabilities, and in recent -versions of MicroPython, these may be enabled by default. To build -these additional dependencies, in the port directory you're -interested in (e.g. `ports/unix/`) first execute: +The core MicroPython VM and runtime has no external dependencies, but a given +port might depend on third-party drivers or vendor HALs. This repository +includes [several submodules](lib/) linking to these external dependencies. +Before compiling a given port, use + $ cd ports/name $ make submodules -This will fetch all the relevant git submodules (sub repositories) that -the port needs. Use the same command to get the latest versions of -submodules as they are updated from time to time. After that execute: - - $ make deplibs - -This will build all available dependencies (regardless whether they -are used or not). If you intend to build MicroPython with additional -options (like cross-compiling), the same set of options should be passed -to `make deplibs`. To actually enable/disable use of dependencies, edit -`ports/unix/mpconfigport.mk` file, which has inline descriptions of the options. -For example, to build SSL module (required for `upip` tool described above, -and so enabled by default), `MICROPY_PY_USSL` should be set to 1. - -For some ports, building required dependences is transparent, and happens -automatically. But they still need to be fetched with the `make submodules` -command. - -The STM32 version ------------------ - -The "stm32" port requires an ARM compiler, arm-none-eabi-gcc, and associated -bin-utils. For those using Arch Linux, you need arm-none-eabi-binutils, -arm-none-eabi-gcc and arm-none-eabi-newlib packages. Otherwise, try here: -https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads - -To build: - - $ cd ports/stm32 - $ make submodules - $ make - -You then need to get your board into DFU mode. On the pyboard, connect the -3V3 pin to the P1/DFU pin with a wire (on PYBv1.0 they are next to each other -on the bottom left of the board, second row from the bottom). - -Then to flash the code via USB DFU to your device: - - $ make deploy - -This will use the included `tools/pydfu.py` script. If flashing the firmware -does not work it may be because you don't have the correct permissions, and -need to use `sudo make deploy`. -See the README.md file in the ports/stm32/ directory for further details. - -Contributing ------------- - -MicroPython is an open-source project and welcomes contributions. To be -productive, please be sure to follow the -[Contributors' Guidelines](https://github.com/micropython/micropython/wiki/ContributorGuidelines) -and the [Code Conventions](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md). -Note that MicroPython is licenced under the MIT license, and all contributions -should follow this license. +to ensure that all required submodules are initialised. diff --git a/ports/unix/README.md b/ports/unix/README.md new file mode 100644 index 0000000000000..efc68b2455f90 --- /dev/null +++ b/ports/unix/README.md @@ -0,0 +1,70 @@ +The Unix version +---------------- + +The "unix" port requires a standard Unix-like environment with gcc and GNU +make. This includes Linux, BSD, macOS, and Windows Subsystem for Linux. The +x86 and x64 architectures are supported (i.e. x86 32- and 64-bit), as well as +ARM and MIPS. Making a full-featured port to another architecture requires +writing some assembly code for the exception handling and garbage collection. +Alternatively, a fallback implementation based on setjmp/longjmp can be used. + +To build (see section below for required dependencies): + + $ cd ports/unix + $ make submodules + $ make + +Then to give it a try: + + $ ./build-standard/micropython + >>> list(5 * x + y for x in range(10) for y in [4, 2, 1]) + +Use `CTRL-D` (i.e. EOF) to exit the shell. + +Learn about command-line options (in particular, how to increase heap size +which may be needed for larger applications): + + $ ./micropython -h + +To run the complete testsuite, use: + + $ make test + +The Unix port comes with a builtin package manager called upip, e.g.: + + $ ./micropython -m upip install micropython-pystone + $ ./micropython -m pystone + +Browse available modules on +[PyPI](https://pypi.python.org/pypi?%3Aaction=search&term=micropython). +Standard library modules come from the +[micropython-lib](https://github.com/micropython/micropython-lib) project. + +External dependencies +--------------------- + +The `libffi` library and `pkg-config` tool are required. On Debian/Ubuntu/Mint +derivative Linux distros, install `build-essential`(includes toolchain and +make), `libffi-dev`, and `pkg-config` packages. + +Other dependencies can be built together with MicroPython. This may +be required to enable extra features or capabilities, and in recent +versions of MicroPython, these may be enabled by default. To build +these additional dependencies, in the unix port directory first execute: + + $ make submodules + +This will fetch all the relevant git submodules (sub repositories) that +the port needs. Use the same command to get the latest versions of +submodules as they are updated from time to time. After that execute: + + $ make deplibs + +This will build all available dependencies (regardless whether they are used +or not). If you intend to build MicroPython with additional options +(like cross-compiling), the same set of options should be passed to `make +deplibs`. To actually enable/disable use of dependencies, edit the +`ports/unix/mpconfigport.mk` file, which has inline descriptions of the +options. For example, to build SSL module (required for the `upip` tool +described above, and so enabled by default), `MICROPY_PY_USSL` should be set +to 1. From 316008046a689b59e7a521c811d6e381bd03fb5a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 25 Aug 2022 15:03:38 +1000 Subject: [PATCH 2543/3533] github/ISSUE_TEMPLATE: Replace forum with Discussions. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- .github/ISSUE_TEMPLATE/config.yml | 4 ++-- .github/ISSUE_TEMPLATE/documentation.md | 2 +- .github/ISSUE_TEMPLATE/feature_request.md | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index ac9d97041a505..7bad9562964af 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -7,7 +7,7 @@ assignees: '' --- -* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please see the MicroPython Forum (https://forum.micropython.org/) or raise a documentation request instead. +* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please use the Discussions tab or raise a documentation request instead. * In your issue, please include a clear and concise description of what the bug is, the expected output, and how to replicate it. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 318d9416ee74a..f3662464da337 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,7 +1,7 @@ blank_issues_enabled: false contact_links: - - name: MicroPython Forum - url: https://forum.micropython.org/ + - name: MicroPython GitHub Discussions + url: https://github.com/orgs/micropython/discussions about: Community discussion about all things MicroPython. This is the best place to start if you have questions about using MicroPython or getting started with MicroPython development. - name: MicroPython Documentation url: https://docs.micropython.org/ diff --git a/.github/ISSUE_TEMPLATE/documentation.md b/.github/ISSUE_TEMPLATE/documentation.md index c86c1ec2b8ed6..e36fa62ac29a4 100644 --- a/.github/ISSUE_TEMPLATE/documentation.md +++ b/.github/ISSUE_TEMPLATE/documentation.md @@ -7,7 +7,7 @@ assignees: '' --- -* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please see the MicroPython Forum -- https://forum.micropython.org/ +* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please use the Discussions tab instead. * Describe what was missing from the documentation and/or what was incorrect/incomplete. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 3976699b28ed6..81b55d98e0da8 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -7,7 +7,7 @@ assignees: '' --- -* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please see the MicroPython Forum (https://forum.micropython.org/) or raise a documentation request instead. +* Please search existing issues before raising a new issue. For questions about MicroPython or for help using MicroPython, or any sort of "how do I?" requests, please use the Discussions tab or raise a documentation request instead. * Describe the feature you'd like to see added to MicroPython. In particular, what does this feature enable and why is it useful. MicroPython aims to strike a balance between functionality and code size, so please consider whether this feature can be optionally enabled and whether it can be provided in other ways (e.g. pure-Python library). From 31d7ab327b0da4fe7747aba5590a542b88caa123 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 25 Aug 2022 15:03:56 +1000 Subject: [PATCH 2544/3533] docs/templates/topindex.html: Update forum link. Signed-off-by: Jim Mussared --- docs/templates/topindex.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/templates/topindex.html b/docs/templates/topindex.html index 41bcc740dd919..cfe3ad1516ac6 100644 --- a/docs/templates/topindex.html +++ b/docs/templates/topindex.html @@ -119,7 +119,7 @@

MicroPython documentation

the official MicroPython site

From 53ebbf10e56dd4cc33aec6cc6d0590c66fd1948e Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 27 Aug 2022 10:42:15 +0200 Subject: [PATCH 2545/3533] docs/library/machine.I2C: Add a note about I2C pull-up resistors. Quite regularly users complain about unexpected behavior of I2C, calling it a bug, when in fact the trouble is caused by missing pull-up resistors. So this commit adds a note to the documentation, in the slim hope that people will find and read it. --- docs/library/machine.I2C.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/library/machine.I2C.rst b/docs/library/machine.I2C.rst index 82a88390c3fb3..2a33b1da47a31 100644 --- a/docs/library/machine.I2C.rst +++ b/docs/library/machine.I2C.rst @@ -20,6 +20,17 @@ Software I2C is implemented by bit-banging and can be used on any pin but is not as efficient. These classes have the same methods available and differ primarily in the way they are constructed. +.. Note:: + + The I2C bus requires pull-up circuitry on both SDA and SCL for it's operation. + Usually these are resistors in the range of 1 - 10 kOhm, connected from each SDA/SCL + to Vcc. Without these, the behaviour is undefined and may range from blocking, + unexpected watchdog reset to just wrong values. Often, this pull-up circuitry + is built-in already to the MCU board or sensor breakout boards, but there is + no rule for that. So please check in case of trouble. See also this excellent + `learning guide `_ + by Adafruit about I2C wiring. + Example usage:: from machine import I2C From 2488311dc2f5ef259a03b871620fd6d3f45a1086 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 12:47:34 +0200 Subject: [PATCH 2546/3533] rp2/machine_uart: Implement uart.flush() and uart.txdone(). uart.flush() flush() will wait until all characters have been sent. It may return while the last character is sent. if needed, the calling code has to add one character wait time. To avoid a permanent lock, a timeout applies depending on the size of txbuf and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. It may return True if the last byte of a transfer is sent. ret is False otherwise. --- ports/rp2/machine_uart.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 77ccdb5f50f2a..f7e44b6b256c1 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -435,17 +435,31 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + + if (ringbuf_avail(&self->write_buffer) == 0 && + uart_get_hw(self->uart)->fr & UART_UARTFR_TXFE_BITS) { + return mp_const_true; + } else { + return mp_const_false; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERT_TX) }, { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERT_RX) }, @@ -538,6 +552,19 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint if ((flags & MP_STREAM_POLL_WR) && ringbuf_free(&self->write_buffer) > 0) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is estimated using the buffer size and the baudrate. + // Take the worst case assumptions at 13 bit symbol size times 2. + uint64_t timeout = time_us_64() + + (uint64_t)(33 + self->write_buffer.size) * 13000000ll * 2 / self->baudrate; + do { + if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) { + return 0; + } + MICROPY_EVENT_POLL_HOOK + } while (time_us_64() < timeout); + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From 5466f1b0ea562548cd11cdbe31b5f71c97cf36f9 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 15:21:48 +0200 Subject: [PATCH 2547/3533] esp32/machine_uart: Implement uart.flush() and uart.txdone(). uart.flush() flush() will wait until all characters have been sent.To avoid a permanent lock, a timeout applies depending on the size of txbuf and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. ret is False otherwise. --- ports/esp32/machine_uart.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 28110e39f3781..72445912897f5 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -417,15 +417,28 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + + if (uart_wait_tx_done(self->uart_num, 0) == ESP_OK) { + return mp_const_true; + } else { + return mp_const_false; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INV_TX) }, { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INV_RX) }, @@ -491,6 +504,18 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr if ((flags & MP_STREAM_POLL_WR) && 1) { // FIXME: uart_tx_any_room(self->uart_num) ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is estimated using the buffer size and the baudrate. + // Take the worst case assumptions at 13 bit symbol size times 2. + uint32_t baudrate; + uart_get_baudrate(self->uart_num, &baudrate); + uint32_t timeout = (3 + self->txbuf) * 13000 * 2 / baudrate; + if (uart_wait_tx_done(self->uart_num, timeout) == ESP_OK) { + ret = 0; + } else { + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR; + } } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From 49e17c8bb0c55d8efb3f768ba1dfd5b27c466b26 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 15:49:59 +0200 Subject: [PATCH 2548/3533] mimxrt/machine_uart: Implement uart.flush() and uart.txdone(). uart.flush() flush() will wait until all characters have been sent.To avoid a permanent lock, a timeout applies depending on the size of txbuf and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. ret is False otherwise. --- ports/mimxrt/machine_uart.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 115d9226bf8be..302244f7221f6 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -308,17 +308,30 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + + if (self->tx_status == kStatus_LPUART_TxIdle) { + return mp_const_true; + } else { + return mp_const_false; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERT_TX) }, { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERT_RX) }, @@ -433,6 +446,21 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint if ((flags & MP_STREAM_POLL_WR)) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is estimated using the buffer size and the baudrate. + // Take the worst case assumptions at 13 bit symbol size times 2. + uint64_t timeout = (uint64_t)(3 + self->txbuf_len) * 13000000ll * 2 / + self->config.baudRate_Bps + ticks_us64(); + + do { + if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) { + return 0; + } + MICROPY_EVENT_POLL_HOOK + } while (ticks_us64() < timeout); + + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From 8804993d0f5326fd71f137433a1b52199d65f119 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 16:42:10 +0200 Subject: [PATCH 2549/3533] esp8266/machine_uart: Implement uart.flush() and uart.txdone(). uart.flush() flush() will wait until all characters but the last one have been sent. It returns while the last character is sent. If needed, the calling code has to add one character wait time. To avoid a permanent lock, a timeout applies depending on the size of the FIFO and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. It returns already True when the last byte of a transfer is sent. ret is False otherwise. --- ports/esp8266/machine_uart.c | 24 ++++++++++++++++++++++++ ports/esp8266/uart.c | 9 +++++++++ ports/esp8266/uart.h | 1 + 3 files changed, 34 insertions(+) diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c index 4e88eee162d8b..2fe6516dc7ced 100644 --- a/ports/esp8266/machine_uart.c +++ b/ports/esp8266/machine_uart.c @@ -231,10 +231,20 @@ STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return uart_txdone(self->uart_id) == true ? mp_const_true : mp_const_false; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_uart_any_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, + + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, @@ -305,6 +315,20 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t a if ((flags & MP_STREAM_POLL_WR) && uart_tx_any_room(self->uart_id)) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is estimated using the buffer size and the baudrate. + // Take the worst case assumptions at 13 bit symbol size times 2. + uint64_t timeout = (uint64_t)(3 + 127) * 13000000ll * 2 / self->baudrate + + system_get_time(); + do { + if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) { + return 0; + } + MICROPY_EVENT_POLL_HOOK + } while (system_get_time() < timeout); + + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; diff --git a/ports/esp8266/uart.c b/ports/esp8266/uart.c index 978a7efc38edf..117cd1bf6c938 100644 --- a/ports/esp8266/uart.c +++ b/ports/esp8266/uart.c @@ -111,6 +111,15 @@ void uart_tx_one_char(uint8 uart, uint8 TxChar) { WRITE_PERI_REG(UART_FIFO(uart), TxChar); } +int uart_txdone(uint8 uart) { + uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); + if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) { + return true; + } else { + return false; + } +} + void uart_flush(uint8 uart) { while (true) { uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); diff --git a/ports/esp8266/uart.h b/ports/esp8266/uart.h index de0919bde056d..3c5592ff0aa88 100644 --- a/ports/esp8266/uart.h +++ b/ports/esp8266/uart.h @@ -101,6 +101,7 @@ bool uart_rx_wait(uint32_t timeout_us); int uart_rx_char(void); void uart_tx_one_char(uint8 uart, uint8 TxChar); void uart_flush(uint8 uart); +int uart_txdone(uint8 uart); void uart_os_config(int uart); void uart_setup(uint8 uart); int uart0_get_rxbuf_len(void); From 8ea6fefc6d52ae46199a337064c05431f6dd30cb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 17:04:23 +0200 Subject: [PATCH 2550/3533] stm32/machine_uart: Implement uart.flush() and uart.txdone(). Since uart.write() of the STM32 port waits until all bytes have been sent, uart.flush() and uart.txdone() are implemented as empty functions to provide API consistency. uart.flush() flush() will always return immediately. ret = uart.txdone() uart.txdone() will always return True. --- ports/stm32/machine_uart.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index bf7bcfb9cd354..1bb1d2a1a7844 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -523,6 +523,12 @@ STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_irq_obj, 1, pyb_uart_irq); +// Since uart.write() waits up to the last byte, uart.txdone() always returns True. +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + return mp_const_true; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { // instance methods @@ -538,11 +544,13 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, /// \method write(buf) { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pyb_uart_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_writechar), MP_ROM_PTR(&pyb_uart_writechar_obj) }, { MP_ROM_QSTR(MP_QSTR_readchar), MP_ROM_PTR(&pyb_uart_readchar_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&pyb_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, // class constants { MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(UART_HWCONTROL_RTS) }, @@ -635,6 +643,9 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t a if ((flags & MP_STREAM_POLL_WR) && uart_tx_avail(self)) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // Since uart.write() waits up to the last byte, uart.flush() always succeds. + ret = 0; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From cc0249c936091889c9f2bda0ecafc522e06364a3 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 17:24:30 +0200 Subject: [PATCH 2551/3533] nrf/modules/machine/uart: Implement uart.flush() and uart.txdone(). Since uart.write() of the nrf port waits until all bytes but the last one have been sent, uart.flush() and uart.txdone() are implemented as empty functions to provide API consistency. uart.flush() flush() will always return immediately, even if the last byte may still be sent. ret = uart.txdone() uart.txdone() will always return True, even if the last byte may still be sent. --- ports/nrf/modules/machine/uart.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index e8c82e57dd162..1fd2ccc06cafd 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -292,6 +292,12 @@ STATIC mp_obj_t machine_hard_uart_sendbreak(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_hard_uart_sendbreak_obj, machine_hard_uart_sendbreak); +// Since uart.write() waits up to the last byte, uart.txdone() always returns True. +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + return mp_const_true; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t machine_hard_uart_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, @@ -302,6 +308,8 @@ STATIC const mp_rom_map_elem_t machine_hard_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_writechar), MP_ROM_PTR(&machine_hard_uart_writechar_obj) }, { MP_ROM_QSTR(MP_QSTR_readchar), MP_ROM_PTR(&machine_hard_uart_readchar_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_hard_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, // class constants /* @@ -347,6 +355,11 @@ STATIC mp_uint_t machine_hard_uart_write(mp_obj_t self_in, const void *buf_in, m STATIC mp_uint_t machine_hard_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { machine_hard_uart_obj_t *self = self_in; (void)self; + + if (request == MP_STREAM_FLUSH) { + // Since uart.write() waits up to the last byte, uart.flush() always succeds. + return 0; + } return MP_STREAM_ERROR; } From a39b88f0fbffe802dc8a80980e6fd675c9ef5a65 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 18:08:29 +0200 Subject: [PATCH 2552/3533] cc3200/mods/pybuart: Implement uart.flush() and uart.txdone(). uart.flush() flush() will wait until all characters have been sent. To avoid a permanent lock, a timeout applies depending on the size of FIFO and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. ret is False otherwise. --- ports/cc3200/mods/pybuart.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index 6e774b70d9a9f..a81954abb8b57 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -66,6 +66,7 @@ #define PYBUART_TX_MAX_TIMEOUT_MS (5) #define PYBUART_RX_BUFFER_LEN (256) +#define PYBUART_TX_BUFFER_LEN (17) // interrupt triggers #define UART_TRIGGER_RX_ANY (0x01) @@ -558,6 +559,17 @@ STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_irq_obj, 1, pyb_uart_irq); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + + if (MAP_UARTBusy(self->reg) == false) { + return mp_const_true; + } else { + return mp_const_false; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) }, @@ -565,6 +577,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_uart_any_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&pyb_uart_sendbreak_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pyb_uart_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, /// \method read([nbytes]) { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, @@ -574,6 +587,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, /// \method write(buf) { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, // class constants { MP_ROM_QSTR(MP_QSTR_RX_ANY), MP_ROM_INT(UART_TRIGGER_RX_ANY) }, @@ -635,6 +649,21 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t a if ((flags & MP_STREAM_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is estimated using the buffer size and the baudrate. + // Take the worst case assumptions at 13 bit symbol size times 2. + uint64_t timeout = mp_hal_ticks_ms() + + (PYBUART_TX_BUFFER_LEN) * 13000 * 2 / self->baudrate; + + do { + if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) { + return 0; + } + MICROPY_EVENT_POLL_HOOK + } while (mp_hal_ticks_ms() < timeout); + + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR;; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From b74eeee5e0e2a6c1437072c7343ccf9a5e7b5aad Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 26 Aug 2022 20:37:43 +0200 Subject: [PATCH 2553/3533] docs/library/machine.UART: Add docs for uart.flush() and uart.txdone(). --- docs/library/machine.UART.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index 1d54174c99614..c3eca8f55a693 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -177,6 +177,32 @@ Methods Availability: WiPy. +.. method:: UART.flush() + + Waits until all data has been sent. In case of a timeout, an exception is raised. The timeout + duration depends on the tx buffer size and the baud rate. Unless flow control is enabled, a timeout + should not occur. + + .. note:: + + For the rp2, esp8266 and nrf ports the call returns while the last byte is sent. + If required, a one character wait time has to be added in the calling script. + + Availability: rp2, esp32, esp8266, mimxrt, cc3200, stm32, nrf ports + +.. method:: UART.txdone() + + Tells whether all data has been sent or no data transfer is happening. In this case, + it returns ``True``. If a data transmission is ongoing it returns ``False``. + + .. note:: + + For the rp2, esp8266 and nrf ports the call may return ``True`` even if the last byte + of a transfer is still being sent. If required, a one character wait time has to be + added in the calling script. + + Availability: rp2, esp32, esp8266, mimxrt, cc3200, stm32, nrf ports + Constants --------- From 85a25895ffc17b6034f4348e26c6248c34be8324 Mon Sep 17 00:00:00 2001 From: hoihu Date: Mon, 29 Aug 2022 08:00:37 +0200 Subject: [PATCH 2554/3533] rp2/mbedtls: Fix missing time.h include. --- ports/rp2/mbedtls/mbedtls_config.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/rp2/mbedtls/mbedtls_config.h b/ports/rp2/mbedtls/mbedtls_config.h index 743d0a6a8425e..c80aa5bc18c2a 100644 --- a/ports/rp2/mbedtls/mbedtls_config.h +++ b/ports/rp2/mbedtls/mbedtls_config.h @@ -106,6 +106,7 @@ void m_tracked_free(void *ptr); #define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf // Time hook +#include time_t rp2_rtctime_seconds(time_t *timer); #define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds From 621bff8557c7a7e35cdc9f6c6de0e6c912d94202 Mon Sep 17 00:00:00 2001 From: Takeo Takahashi Date: Thu, 23 Jun 2022 22:27:47 +0900 Subject: [PATCH 2555/3533] renesas-ra/machine_pin: Support drive keyword and fix GPIO setting. Changes are: - Support drive= keyword argument. - Fix trigger keyword check. - Fix GPIO pin setting. Signed-off-by: Takeo Takahashi --- .../boards/RA4M1_CLICKER/mpconfigboard.h | 4 +- .../boards/RA4M1_EK/mpconfigboard.h | 4 +- .../boards/RA4W1_EK/mpconfigboard.h | 4 +- .../boards/RA6M1_EK/mpconfigboard.h | 4 +- .../boards/RA6M2_EK/mpconfigboard.h | 4 +- ports/renesas-ra/extint.c | 30 ++-- ports/renesas-ra/machine_pin.c | 132 +++++++++++------- ports/renesas-ra/mphalport.h | 12 +- ports/renesas-ra/ra/ra_gpio.c | 59 ++++---- ports/renesas-ra/ra/ra_gpio.h | 57 ++++---- ports/renesas-ra/ra/ra_i2c.c | 4 +- ports/renesas-ra/ra/ra_sci.c | 8 +- ports/renesas-ra/ra/ra_spi.c | 4 +- ports/renesas-ra/usrsw.c | 2 +- 14 files changed, 177 insertions(+), 151 deletions(-) diff --git a/ports/renesas-ra/boards/RA4M1_CLICKER/mpconfigboard.h b/ports/renesas-ra/boards/RA4M1_CLICKER/mpconfigboard.h index ec67d68b6584e..f39cf8f80e39c 100644 --- a/ports/renesas-ra/boards/RA4M1_CLICKER/mpconfigboard.h +++ b/ports/renesas-ra/boards/RA4M1_CLICKER/mpconfigboard.h @@ -47,8 +47,8 @@ // Switch #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_USRSW_PIN (pin_P304) -#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) -#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PULL (MP_HAL_PIN_PULL_NONE) +#define MICROPY_HW_USRSW_EXTI_MODE (MP_HAL_PIN_TRIGGER_FALLING) #define MICROPY_HW_USRSW_PRESSED (0) // LEDs diff --git a/ports/renesas-ra/boards/RA4M1_EK/mpconfigboard.h b/ports/renesas-ra/boards/RA4M1_EK/mpconfigboard.h index 500fe78b9e121..2874142890c13 100644 --- a/ports/renesas-ra/boards/RA4M1_EK/mpconfigboard.h +++ b/ports/renesas-ra/boards/RA4M1_EK/mpconfigboard.h @@ -59,8 +59,8 @@ // Switch #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_USRSW_PIN (pin_P105) -#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) -#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PULL (MP_HAL_PIN_PULL_NONE) +#define MICROPY_HW_USRSW_EXTI_MODE (MP_HAL_PIN_TRIGGER_FALLING) #define MICROPY_HW_USRSW_PRESSED (0) // LEDs diff --git a/ports/renesas-ra/boards/RA4W1_EK/mpconfigboard.h b/ports/renesas-ra/boards/RA4W1_EK/mpconfigboard.h index 0e0dff14755cf..0bf907a48c7cc 100644 --- a/ports/renesas-ra/boards/RA4W1_EK/mpconfigboard.h +++ b/ports/renesas-ra/boards/RA4W1_EK/mpconfigboard.h @@ -58,8 +58,8 @@ // Switch #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_USRSW_PIN (pin_P402) -#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) -#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PULL (MP_HAL_PIN_PULL_NONE) +#define MICROPY_HW_USRSW_EXTI_MODE (MP_HAL_PIN_TRIGGER_FALLING) #define MICROPY_HW_USRSW_PRESSED (0) // LEDs diff --git a/ports/renesas-ra/boards/RA6M1_EK/mpconfigboard.h b/ports/renesas-ra/boards/RA6M1_EK/mpconfigboard.h index 0693e404fd8f3..59db4bc218a82 100644 --- a/ports/renesas-ra/boards/RA6M1_EK/mpconfigboard.h +++ b/ports/renesas-ra/boards/RA6M1_EK/mpconfigboard.h @@ -64,8 +64,8 @@ // Switch #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_USRSW_PIN (pin_P415) -#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) -#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PULL (MP_HAL_PIN_PULL_NONE) +#define MICROPY_HW_USRSW_EXTI_MODE (MP_HAL_PIN_TRIGGER_FALLING) #define MICROPY_HW_USRSW_PRESSED (0) // LEDs diff --git a/ports/renesas-ra/boards/RA6M2_EK/mpconfigboard.h b/ports/renesas-ra/boards/RA6M2_EK/mpconfigboard.h index 2d2dc326d3384..9988e0ed25d0c 100644 --- a/ports/renesas-ra/boards/RA6M2_EK/mpconfigboard.h +++ b/ports/renesas-ra/boards/RA6M2_EK/mpconfigboard.h @@ -77,8 +77,8 @@ // Switch #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_USRSW_PIN (pin_P105) -#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) -#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PULL (MP_HAL_PIN_PULL_NONE) +#define MICROPY_HW_USRSW_EXTI_MODE (MP_HAL_PIN_TRIGGER_FALLING) #define MICROPY_HW_USRSW_PRESSED (0) // LEDs diff --git a/ports/renesas-ra/extint.c b/ports/renesas-ra/extint.c index 19caa7a3b3fef..59f9ecfa37ea0 100644 --- a/ports/renesas-ra/extint.c +++ b/ports/renesas-ra/extint.c @@ -138,8 +138,8 @@ uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t ca mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("The Pin object(%d) doesn't have EXTINT feature"), pin_idx); } } - if (pull != GPIO_NOPULL && - pull != GPIO_PULLUP) { + if ((pull != MP_HAL_PIN_PULL_NONE) && + (pull != MP_HAL_PIN_PULL_UP)) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid ExtInt Pull: %d"), pull); } mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[v_line]; @@ -153,7 +153,6 @@ uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t ca extint_disable(v_line); *cb = callback_obj; - // ToDo: mode should be handled pyb_extint_mode[v_line] = mode; pyb_extint_callback_arg[v_line] = MP_OBJ_NEW_SMALL_INT(v_line); if (*cb != mp_const_none) { @@ -200,7 +199,6 @@ void extint_register_pin(const machine_pin_obj_t *pin, uint32_t mode, bool hard_ extint_disable(line); *cb = callback_obj; - // ToDo: mode should be handled pyb_extint_mode[line] = mode; if (*cb != mp_const_none) { @@ -255,16 +253,16 @@ void extint_trigger_mode(uint line, uint32_t mode) { // cond: 0: falling, 1: rising, 2: both edge, 3 low level // Enable or disable the rising detector uint32_t cond = 0; - if ((mode == GPIO_MODE_IT_RISING) || (mode == GPIO_MODE_EVT_RISING)) { - cond = 1; - } else if ((mode == GPIO_MODE_IT_FALLING) || (mode == GPIO_MODE_EVT_FALLING)) { - cond = 0; - } else if ((mode == GPIO_MODE_IT_RISING_FALLING) || (mode == GPIO_MODE_EVT_RISING_FALLING)) { - cond = 2; - } else if (mode == GPIO_IRQ_LOWLEVEL) { + if (mode & MP_HAL_PIN_TRIGGER_LOWLEVEL) { cond = 3; + } else if (mode & MP_HAL_PIN_TRIGGER_FALLING) { + if (mode & MP_HAL_PIN_TRIGGER_RISING) { + cond = 2; + } else { + cond = 0; + } } else { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("The device doesn't have (%d) feature"), (uint32_t)mode); + cond = 1; } ra_icu_trigger_irq_no((uint8_t)line, cond); enable_irq(irq_state); @@ -372,14 +370,6 @@ STATIC const mp_rom_map_elem_t extint_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&extint_obj_disable_obj) }, { MP_ROM_QSTR(MP_QSTR_swint), MP_ROM_PTR(&extint_obj_swint_obj) }, { MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&extint_regs_obj) }, - - // class constants - /// \constant IRQ_RISING - interrupt on a rising edge - /// \constant IRQ_FALLING - interrupt on a falling edge - /// \constant IRQ_RISING_FALLING - interrupt on a rising or falling edge - { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) }, }; STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table); diff --git a/ports/renesas-ra/machine_pin.c b/ports/renesas-ra/machine_pin.c index 6e852e9cbf1d6..f940b55547eba 100644 --- a/ports/renesas-ra/machine_pin.c +++ b/ports/renesas-ra/machine_pin.c @@ -78,7 +78,7 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin uint32_t mode = pin_get_mode(self); - if (mode == GPIO_MODE_ANALOG) { + if (mode == MP_HAL_PIN_MODE_ANALOG) { // analog mp_print_str(print, "ANALOG)"); @@ -86,15 +86,15 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin // IO mode bool af = false; qstr mode_qst; - if (mode == GPIO_MODE_INPUT) { + if (mode == MP_HAL_PIN_MODE_INPUT) { mode_qst = MP_QSTR_IN; - } else if (mode == GPIO_MODE_OUTPUT_PP) { + } else if (mode == MP_HAL_PIN_MODE_OUTPUT) { mode_qst = MP_QSTR_OUT; - } else if (mode == GPIO_MODE_OUTPUT_OD) { + } else if (mode == MP_HAL_PIN_MODE_OPEN_DRAIN) { mode_qst = MP_QSTR_OPEN_DRAIN; } else { af = true; - if (mode == GPIO_MODE_AF_PP) { + if (mode == MP_HAL_PIN_MODE_ALT) { mode_qst = MP_QSTR_ALT; } else { mode_qst = MP_QSTR_ALT_OPEN_DRAIN; @@ -105,9 +105,9 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin // pull mode qstr pull_qst = MP_QSTRnull; uint32_t pull = pin_get_pull(self); - if (pull == GPIO_PULLUP) { + if (pull == MP_HAL_PIN_PULL_UP) { pull_qst = MP_QSTR_PULL_UP; - } else if (pull == GPIO_NOPULL) { + } else if (pull == MP_HAL_PIN_PULL_NONE) { pull_qst = MP_QSTR_PULL_NONE; } if (pull_qst != MP_QSTRnull) { @@ -117,12 +117,14 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin // drive qstr drive_qst = MP_QSTRnull; uint32_t drive = pin_get_drive(self); - if (drive == GPIO_HIGH_POWER) { - drive_qst = MP_QSTR_HIGH_POWER; - } else if (drive == GPIO_MED_POWER) { - drive_qst = MP_QSTR_MED_POWER; - } else if (drive == GPIO_LOW_POWER) { - drive_qst = MP_QSTR_LOW_POWER; + if (drive == MP_HAL_PIN_DRIVE_3) { + drive_qst = MP_QSTR_DRIVE_3; + } else if (drive == MP_HAL_PIN_DRIVE_2) { + drive_qst = MP_QSTR_DRIVE_2; + } else if (drive == MP_HAL_PIN_DRIVE_1) { + drive_qst = MP_QSTR_DRIVE_1; + } else if (drive == MP_HAL_PIN_DRIVE_0) { + drive_qst = MP_QSTR_DRIVE_0; } if (drive_qst != MP_QSTRnull) { mp_printf(print, ", drive=Pin.%q", drive_qst); @@ -143,14 +145,14 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin } } -// pin.init(mode, pull=None, *, value=None, driver=None, alt=FUNC_SIO) +// pin.init(mode=-1, pull=-1, *, value=None, drive=0, alt=-1) STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, - { MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy - { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, - { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = GPIO_LOW_POWER}}, + { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, }; @@ -159,29 +161,50 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // get io mode - uint mode = args[0].u_int; - if (!IS_GPIO_MODE(mode)) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode); + uint32_t mode; + if (args[ARG_mode].u_obj != mp_const_none) { + mode = mp_obj_get_int(args[ARG_mode].u_obj); + if (!IS_GPIO_MODE(mode)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode); + } + } else { + mode = ra_gpio_get_mode(self->pin); } // get pull mode - uint pull = 0; - if (args[1].u_obj != mp_const_none) { - pull = mp_obj_get_int(args[1].u_obj); + uint32_t pull; + if (args[ARG_pull].u_obj != mp_const_none) { + pull = mp_obj_get_int(args[ARG_pull].u_obj); + if (!IS_GPIO_PULL(pull)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pull mode: %d"), pull); + } + if (pull == MP_HAL_PIN_PULL_DOWN) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%q is not supported"), MP_QSTR_PULL_DOWN); + } + } else { + pull = ra_gpio_get_pull(self->pin); } // get drive - uint drive = args[4].u_int; - if (!IS_GPIO_DRIVE(drive)) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin drive: %d"), drive); + uint32_t drive; + if (args[ARG_drive].u_obj != mp_const_none) { + drive = mp_obj_get_int(args[ARG_drive].u_obj); + if (!IS_GPIO_DRIVE(drive)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin drive: %d"), drive); + } + } else { + drive = ra_gpio_get_drive(self->pin); } - mp_hal_pin_config(self, mode, pull, drive, -1); + // get alt + if (args[ARG_alt].u_int != (-1)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("alt is not supported")); + } + mp_hal_pin_config(self, mode, pull, drive, 0); // if given, set the pin value before initialising to prevent glitches - if (args[3].u_obj != MP_OBJ_NULL) { - mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj)); + if (args[ARG_value].u_obj != mp_const_none) { + mp_hal_pin_write(self, mp_obj_is_true(args[ARG_value].u_obj)); } - return mp_const_none; } @@ -249,13 +272,17 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ enum { ARG_handler, ARG_trigger, ARG_hard }; static const mp_arg_t allowed_args[] = { { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, - { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_MODE_IT_RISING | GPIO_MODE_IT_FALLING} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = MP_HAL_PIN_TRIGGER_RISING | MP_HAL_PIN_TRIGGER_FALLING} }, { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, }; machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + if (args[ARG_trigger].u_int & MP_HAL_PIN_TRIGGER_HIGHLEVEL) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%q is not supported"), MP_QSTR_IRQ_HIGH_LEVEL); + } + if (n_args > 1 || kw_args->used != 0) { // configure irq extint_register_pin(self, args[ARG_trigger].u_int, @@ -282,27 +309,23 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&pin_cpu_pins_obj_type) }, // class constants - { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_INPUT) }, - { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) }, - { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) }, - { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_AF_PP) }, - { MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_AF_OD) }, - { MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(GPIO_MODE_ANALOG) }, - { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP) }, - { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULLDOWN) }, - { MP_ROM_QSTR(MP_QSTR_PULL_HOLD), MP_ROM_INT(GPIO_PULLHOLD) }, - { MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) }, - { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_LOW_POWER) }, - { MP_ROM_QSTR(MP_QSTR_MED_POWER), MP_ROM_INT(GPIO_MED_POWER) }, - { MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_HIGH_POWER) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) }, - { MP_ROM_QSTR(MP_QSTR_EVT_RISING), MP_ROM_INT(GPIO_MODE_EVT_RISING) }, - { MP_ROM_QSTR(MP_QSTR_EVT_FALLING), MP_ROM_INT(GPIO_MODE_EVT_FALLING) }, - { MP_ROM_QSTR(MP_QSTR_EVT_RISING_FALLING), MP_ROM_INT(GPIO_MODE_EVT_RISING_FALLING) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_LOWLEVEL), MP_ROM_INT(GPIO_IRQ_LOWLEVEL) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_HIGHLEVEL), MP_ROM_INT(GPIO_IRQ_HIGHLEVEL) }, + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(MP_HAL_PIN_MODE_INPUT) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(MP_HAL_PIN_MODE_OUTPUT) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(MP_HAL_PIN_MODE_OPEN_DRAIN) }, + { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(MP_HAL_PIN_MODE_ALT) }, + { MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(MP_HAL_PIN_MODE_ALT_OPEN_DRAIN) }, + { MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(MP_HAL_PIN_MODE_ANALOG) }, + { MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(MP_HAL_PIN_PULL_NONE) }, + { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(MP_HAL_PIN_PULL_UP) }, + { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(MP_HAL_PIN_PULL_DOWN) }, + { MP_ROM_QSTR(MP_QSTR_DRIVE_0), MP_ROM_INT(MP_HAL_PIN_DRIVE_0) }, + { MP_ROM_QSTR(MP_QSTR_DRIVE_1), MP_ROM_INT(MP_HAL_PIN_DRIVE_1) }, + { MP_ROM_QSTR(MP_QSTR_DRIVE_2), MP_ROM_INT(MP_HAL_PIN_DRIVE_2) }, + { MP_ROM_QSTR(MP_QSTR_DRIVE_3), MP_ROM_INT(MP_HAL_PIN_DRIVE_3) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(MP_HAL_PIN_TRIGGER_FALLING) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(MP_HAL_PIN_TRIGGER_RISING) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_LOW_LEVEL), MP_ROM_INT(MP_HAL_PIN_TRIGGER_LOWLEVEL) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_HIGH_LEVEL), MP_ROM_INT(MP_HAL_PIN_TRIGGER_HIGHLEVEL) }, }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); @@ -352,7 +375,8 @@ uint32_t pin_get_pull(const machine_pin_obj_t *pin) { } // Returns the pin drive. The value returned by this macro should -// be one of GPIO_HIGH_POWER, GPIO_MED_POWER, or GPIO_LOW_POWER. +// be one of GPIO_HIGH_POWER, GPIO_MID_FAST_POWER, GPIO_MID_POWER, +// or GPIO_LOW_POWER. uint32_t pin_get_drive(const machine_pin_obj_t *pin) { return (uint32_t)ra_gpio_get_drive(pin->pin); diff --git a/ports/renesas-ra/mphalport.h b/ports/renesas-ra/mphalport.h index c489d1b966a74..2648e22f994ca 100644 --- a/ports/renesas-ra/mphalport.h +++ b/ports/renesas-ra/mphalport.h @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2018 Damien P. George - * Copyright (c) 2021 Renesas Electronics Corporation + * Copyright (c) 2021-2022 Renesas Electronics Corporation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -69,13 +69,21 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) { #define MP_HAL_PIN_PULL_NONE (GPIO_NOPULL) #define MP_HAL_PIN_PULL_UP (GPIO_PULLUP) #define MP_HAL_PIN_PULL_DOWN (GPIO_PULLDOWN) +#define MP_HAL_PIN_TRIGGER_FALLING (GPIO_IRQ_FALLING) +#define MP_HAL_PIN_TRIGGER_RISING (GPIO_IRQ_RISING) +#define MP_HAL_PIN_TRIGGER_LOWLEVEL (GPIO_IRQ_LOWLEVEL) +#define MP_HAL_PIN_TRIGGER_HIGHLEVEL (GPIO_IRQ_HIGHLEVEL) +#define MP_HAL_PIN_DRIVE_0 (GPIO_LOW_POWER) +#define MP_HAL_PIN_DRIVE_1 (GPIO_MID_POWER) +#define MP_HAL_PIN_DRIVE_2 (GPIO_MID_FAST_POWER) +#define MP_HAL_PIN_DRIVE_3 (GPIO_HIGH_POWER) #define mp_hal_pin_obj_t const machine_pin_obj_t * #define mp_hal_get_pin_obj(o) machine_pin_find(o) #define mp_hal_pin_name(p) ((p)->name) #define mp_hal_pin_input(p) ra_gpio_mode_input((p)->pin) #define mp_hal_pin_output(p) ra_gpio_mode_output((p)->pin) -#define mp_hal_pin_open_drain(p) ra_gpio_config((p)->pin, GPIO_MODE_OUTPUT_OD, 0, 0, 0) +#define mp_hal_pin_open_drain(p) ra_gpio_config((p)->pin, MP_HAL_PIN_MODE_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_DRIVE_0, 0) #define mp_hal_pin_high(p) ra_gpio_write((p)->pin, 1) #define mp_hal_pin_low(p) ra_gpio_write((p)->pin, 0) #define mp_hal_pin_toggle(p) ra_gpio_toggle((p)->pin) diff --git a/ports/renesas-ra/ra/ra_gpio.c b/ports/renesas-ra/ra/ra_gpio.c index 598546cb93493..a01131953c1f3 100644 --- a/ports/renesas-ra/ra/ra_gpio.c +++ b/ports/renesas-ra/ra/ra_gpio.c @@ -31,23 +31,17 @@ void ra_gpio_config(uint32_t pin, uint32_t mode, uint32_t pull, uint32_t drive, uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); pwpr_unprotect(); + _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK | NCODR_MASK | PCR_MASK | PDR_MASK | DSCR1_MASK | DSCR_MASK); switch (mode) { case GPIO_MODE_INPUT: - _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO - _PXXPFS(port, bit) &= ~PDR_MASK; // input - if (pull != 0) { + if (pull == GPIO_PULLUP) { _PXXPFS(port, bit) |= PCR_MASK; // set pullup - } else { - _PXXPFS(port, bit) &= ~PCR_MASK; // clear pullup } break; case GPIO_MODE_OUTPUT_PP: - _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO _PXXPFS(port, bit) |= PDR_MASK; // output - _PXXPFS(port, bit) &= ~PCR_MASK; // pullup clear break; case GPIO_MODE_OUTPUT_OD: - _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO _PXXPFS(port, bit) |= (PDR_MASK | NCODR_MASK); break; case GPIO_MODE_AF_PP: @@ -56,20 +50,23 @@ void ra_gpio_config(uint32_t pin, uint32_t mode, uint32_t pull, uint32_t drive, case GPIO_MODE_AF_OD: _PXXPFS(port, bit) |= (PMR_MASK | PDR_MASK | NCODR_MASK); break; + case GPIO_MODE_ANALOG: + _PXXPFS(port, bit) |= ASEL_MASK; + break; } switch (drive) { case GPIO_HIGH_POWER: _PXXPFS(port, bit) |= (DSCR1_MASK | DSCR_MASK); break; - case GPIO_MED_POWER: - _PXXPFS(port, bit) &= ~DSCR1_MASK; + case GPIO_MID_FAST_POWER: + _PXXPFS(port, bit) |= DSCR1_MASK; + break; + case GPIO_MID_POWER: _PXXPFS(port, bit) |= DSCR_MASK; break; case GPIO_LOW_POWER: - _PXXPFS(port, bit) &= ~(DSCR1_MASK | DSCR_MASK); - break; - default: /* GPIO_NOTOUCH_POWER */ - /* do not modify */ + default: + /* Bits are already cleared */ break; } _PXXPFS(port, bit) &= ~(uint32_t)(0x1f000000); @@ -102,7 +99,6 @@ void ra_gpio_toggle(uint32_t pin) { uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); pwpr_unprotect(); - _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO _PXXPFS(port, bit) ^= 1; pwpr_protect(); } @@ -111,7 +107,6 @@ void ra_gpio_write(uint32_t pin, uint32_t value) { uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); pwpr_unprotect(); - _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO if (value != 0) { _PXXPFS(port, bit) |= 1; } else { @@ -123,15 +118,26 @@ void ra_gpio_write(uint32_t pin, uint32_t value) { uint32_t ra_gpio_read(uint32_t pin) { uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); - return ((_PXXPFS(port, bit) &= PIDR_MASK) != 0) ? 1 : 0; + return ((_PXXPFS(port, bit) & PIDR_MASK) != 0) ? 1 : 0; } uint32_t ra_gpio_get_mode(uint32_t pin) { uint8_t mode = 0; - uint32_t port = GPIO_PORT(pin); - uint32_t bit = GPIO_BIT(pin); - if ((_PXXPFS(port, bit) &= PDR_MASK) != 0) { - mode = GPIO_MODE_OUTPUT_PP; + uint32_t pfs = _PXXPFS(GPIO_PORT(pin), GPIO_BIT(pin)); + if ((pfs & ASEL_MASK) != 0) { + mode = GPIO_MODE_ANALOG; + } else if ((pfs & PMR_MASK) != 0) { + if ((pfs & NCODR_MASK) != 0) { + mode = GPIO_MODE_AF_OD; + } else { + mode = GPIO_MODE_AF_PP; + } + } else if ((pfs & PDR_MASK) != 0) { + if ((pfs & NCODR_MASK) != 0) { + mode = GPIO_MODE_OUTPUT_OD; + } else { + mode = GPIO_MODE_OUTPUT_PP; + } } else { mode = GPIO_MODE_INPUT; } @@ -142,7 +148,7 @@ uint32_t ra_gpio_get_pull(uint32_t pin) { uint8_t pull = 0; uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); - if ((_PXXPFS(port, bit) &= PCR_MASK) != 0) { + if ((_PXXPFS(port, bit) & PCR_MASK) != 0) { pull = GPIO_PULLUP; } else { pull = GPIO_NOPULL; @@ -153,19 +159,22 @@ uint32_t ra_gpio_get_pull(uint32_t pin) { uint32_t ra_gpio_get_af(uint32_t pin) { uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); - return (_PXXPFS(port, bit) &= PMR_MASK) != 0; + return (_PXXPFS(port, bit) & PMR_MASK) != 0; } uint32_t ra_gpio_get_drive(uint32_t pin) { uint8_t drive = 0; uint32_t port = GPIO_PORT(pin); uint32_t bit = GPIO_BIT(pin); - switch (_PXXPFS(port, bit) &= (DSCR1_MASK | DSCR_MASK)) { + switch (_PXXPFS(port, bit) & (DSCR1_MASK | DSCR_MASK)) { case (DSCR1_MASK | DSCR_MASK): drive = GPIO_HIGH_POWER; break; + case DSCR1_MASK: + drive = GPIO_MID_FAST_POWER; + break; case DSCR_MASK: - drive = GPIO_MED_POWER; + drive = GPIO_MID_POWER; break; case 0: default: diff --git a/ports/renesas-ra/ra/ra_gpio.h b/ports/renesas-ra/ra/ra_gpio.h index 7b76016635342..8c16fe3ba18c7 100644 --- a/ports/renesas-ra/ra/ra_gpio.h +++ b/ports/renesas-ra/ra/ra_gpio.h @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2021 Renesas Electronics Corporation + * Copyright (c) 2021,2022 Renesas Electronics Corporation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -69,47 +69,42 @@ enum AF_INDEX { AF_END = 0xff, }; -#define GPIO_MODE_INPUT 1 -#define GPIO_MODE_OUTPUT_PP 2 -#define GPIO_MODE_OUTPUT_OD 3 -#define GPIO_MODE_AF_PP 4 -#define GPIO_MODE_AF_OD 5 -#define GPIO_MODE_ANALOG 6 -#define GPIO_MODE_IT_RISING 7 -#define GPIO_MODE_IT_FALLING 8 -#define GPIO_MODE_IT_RISING_FALLING 9 -#define GPIO_MODE_EVT_RISING 10 -#define GPIO_MODE_EVT_FALLING 11 -#define GPIO_MODE_EVT_RISING_FALLING 12 -#define GPIO_NOPULL 13 -#define GPIO_PULLUP 14 -#define GPIO_PULLDOWN 15 -#define GPIO_PULLHOLD 16 -#define GPIO_LOW_POWER 17 -#define GPIO_MED_POWER 18 -#define GPIO_HIGH_POWER 19 -#define GPIO_NOTOUCH_POWER 20 -#define GPIO_IRQ_LOWLEVEL 21 -#define GPIO_IRQ_HIGHLEVEL 22 +#define GPIO_MODE_INPUT 0 +#define GPIO_MODE_OUTPUT_PP 1 +#define GPIO_MODE_OUTPUT_OD 2 +#define GPIO_MODE_AF_PP 3 +#define GPIO_MODE_AF_OD 4 +#define GPIO_MODE_ANALOG 5 + +#define GPIO_IRQ_FALLING 0x1 +#define GPIO_IRQ_RISING 0x2 +#define GPIO_IRQ_LOWLEVEL 0x4 +#define GPIO_IRQ_HIGHLEVEL 0x8 + +#define GPIO_NOPULL 0 +#define GPIO_PULLUP 1 +#define GPIO_PULLDOWN 2 + +#define GPIO_LOW_POWER 0 +#define GPIO_MID_POWER 1 +#define GPIO_MID_FAST_POWER 2 +#define GPIO_HIGH_POWER 3 #define IS_GPIO_MODE(MODE) (((MODE) == GPIO_MODE_INPUT) || \ ((MODE) == GPIO_MODE_OUTPUT_PP) || \ ((MODE) == GPIO_MODE_OUTPUT_OD) || \ ((MODE) == GPIO_MODE_AF_PP) || \ ((MODE) == GPIO_MODE_AF_OD) || \ - ((MODE) == GPIO_MODE_IT_RISING) || \ - ((MODE) == GPIO_MODE_IT_FALLING) || \ - ((MODE) == GPIO_MODE_IT_RISING_FALLING) || \ - ((MODE) == GPIO_MODE_EVT_RISING) || \ - ((MODE) == GPIO_MODE_EVT_FALLING) || \ - ((MODE) == GPIO_MODE_EVT_RISING_FALLING) || \ ((MODE) == GPIO_MODE_ANALOG)) #define IS_GPIO_DRIVE(DRIVE) (((DRIVE) == GPIO_LOW_POWER) || \ - ((DRIVE) == GPIO_MED_POWER) || \ + ((DRIVE) == GPIO_MID_POWER) || \ + ((DRIVE) == GPIO_MID_FAST_POWER) || \ ((DRIVE) == GPIO_HIGH_POWER)) -#define IS_GPIO_PULL(PULL) (((PULL) == GPIO_NOPULL) || ((PULL) == GPIO_PULLUP)) +#define IS_GPIO_PULL(PULL) (((PULL) == GPIO_NOPULL) || \ + ((PULL) == GPIO_PULLUP) || \ + ((PULL) == GPIO_PULLDOWN)) #define IS_GPIO_AF(AF) ((AF) <= (uint8_t)0x1F) diff --git a/ports/renesas-ra/ra/ra_i2c.c b/ports/renesas-ra/ra/ra_i2c.c index 64bbd6f501b43..ad1e3a74e0de3 100644 --- a/ports/renesas-ra/ra/ra_i2c.c +++ b/ports/renesas-ra/ra/ra_i2c.c @@ -362,8 +362,8 @@ void ra_i2c_set_baudrate(R_IIC0_Type *i2c_inst, uint32_t baudrate) { void ra_i2c_init(R_IIC0_Type *i2c_inst, uint32_t scl, uint32_t sda, uint32_t baudrate) { ra_i2c_module_start(i2c_inst); - ra_gpio_config(scl, GPIO_MODE_AF_OD, 0, GPIO_NOTOUCH_POWER, AF_I2C); - ra_gpio_config(sda, GPIO_MODE_AF_OD, 0, GPIO_NOTOUCH_POWER, AF_I2C); + ra_gpio_config(scl, GPIO_MODE_AF_OD, GPIO_NOPULL, GPIO_LOW_POWER, AF_I2C); + ra_gpio_config(sda, GPIO_MODE_AF_OD, GPIO_NOPULL, GPIO_LOW_POWER, AF_I2C); ra_i2c_priority(i2c_inst, RA_PRI_I2C); i2c_inst->ICCR1_b.ICE = 0; // I2C disable i2c_inst->ICCR1_b.IICRST = 1; // I2C internal reset diff --git a/ports/renesas-ra/ra/ra_sci.c b/ports/renesas-ra/ra/ra_sci.c index 8cf26680cc6e7..2e7c81c4ac477 100644 --- a/ports/renesas-ra/ra/ra_sci.c +++ b/ports/renesas-ra/ra/ra_sci.c @@ -788,7 +788,7 @@ static void ra_sci_tx_set_pin(uint32_t pin) { uint32_t af; find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_tx_pins, SCI_TX_PINS_SIZE, pin, &ch, &af); if (find) { - ra_gpio_config(pin, GPIO_MODE_AF_PP, 0, 0, af); + ra_gpio_config(pin, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_LOW_POWER, af); } } @@ -798,7 +798,7 @@ static void ra_sci_rx_set_pin(uint32_t pin) { uint32_t af; find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_rx_pins, SCI_RX_PINS_SIZE, pin, &ch, &af); if (find) { - ra_gpio_config(pin, GPIO_MODE_INPUT, 1, 0, af); + ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, af); } } @@ -808,7 +808,7 @@ static void ra_sci_cts_set_pin(uint32_t pin) { uint32_t af; find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_cts_pins, SCI_CTS_PINS_SIZE, pin, &ch, &af); if (find) { - ra_gpio_config(pin, GPIO_MODE_INPUT, 1, 0, af); + ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, af); } } @@ -1134,7 +1134,7 @@ void ra_sci_init_with_flow(uint32_t ch, uint32_t tx_pin, uint32_t rx_pin, uint32 } if (rts_pin != (uint32_t)PIN_END) { m_rts_pin[idx] = rts_pin; - ra_gpio_config(rts_pin, GPIO_MODE_OUTPUT_PP, false, 0, 0); + ra_gpio_config(rts_pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_LOW_POWER, 0); ra_gpio_write(rts_pin, 0); } } diff --git a/ports/renesas-ra/ra/ra_spi.c b/ports/renesas-ra/ra/ra_spi.c index b7f78cd6deadd..096519ffd6402 100644 --- a/ports/renesas-ra/ra/ra_spi.c +++ b/ports/renesas-ra/ra/ra_spi.c @@ -196,9 +196,9 @@ static void ra_spi_module_stop(uint32_t ch) { static void ra_spi_set_pin(uint32_t pin, bool miso) { if (miso) { - ra_gpio_config(pin, GPIO_MODE_INPUT, 1, 0, AF_SPI); + ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, AF_SPI); } else { - ra_gpio_config(pin, GPIO_MODE_AF_PP, 0, 0, AF_SPI); + ra_gpio_config(pin, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_LOW_POWER, AF_SPI); } } diff --git a/ports/renesas-ra/usrsw.c b/ports/renesas-ra/usrsw.c index 5e11f48e63929..572510816b1f1 100644 --- a/ports/renesas-ra/usrsw.c +++ b/ports/renesas-ra/usrsw.c @@ -54,7 +54,7 @@ // this function inits the switch GPIO so that it can be used void switch_init0(void) { - mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, GPIO_LOW_POWER, 0); + mp_hal_pin_config(MICROPY_HW_USRSW_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_USRSW_PULL, MP_HAL_PIN_DRIVE_0, 0); } int switch_get(void) { From 2f2fd36713c2c02f56093338e8c1f3cd7b8d403a Mon Sep 17 00:00:00 2001 From: Takeo Takahashi Date: Thu, 23 Jun 2022 22:28:35 +0900 Subject: [PATCH 2556/3533] tests/renesas-ra: Update pin test to support all boards. Signed-off-by: Takeo Takahashi --- tests/renesas-ra/pin.py | 46 ++++++++----------------------------- tests/renesas-ra/pin.py.exp | 12 ++-------- 2 files changed, 11 insertions(+), 47 deletions(-) diff --git a/tests/renesas-ra/pin.py b/tests/renesas-ra/pin.py index 61d76cca7c748..4617cce111101 100644 --- a/tests/renesas-ra/pin.py +++ b/tests/renesas-ra/pin.py @@ -1,43 +1,15 @@ from machine import Pin -import os -n = os.uname().machine -if "RA4W1_EK" in n: - try_pin = "P004" - try_s = "Pin(Pin.cpu.P004, mode=Pin.IN, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)" +p = Pin("SW1", Pin.IN) +if p.value() == 1: + print("pass") else: - try_pin = "P000" - try_s = "Pin(Pin.cpu.P000, mode=Pin.IN, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)" + print("FAIL") -p = Pin(try_pin, Pin.IN) -if str(p) == try_s: - print("OK") +p = Pin("LED1", Pin.OUT) +p.high() +if p.value() == 1: + print("pass") else: - print("NG") - print("exp: " + try_s) - print("out: " + str(p)) - -p.init(p.IN, p.PULL_UP) -p.init(p.IN, pull=p.PULL_UP) -p.init(mode=p.IN, pull=p.PULL_UP) -print(p.value()) - -p.init(p.OUT) -p.init(p.OPEN_DRAIN) + print("FAIL") p.low() -print(p.value()) -p.high() -print(p.value()) -p.value(0) -print(p.value()) -p.value(1) -print(p.value()) -p.value(False) -print(p.value()) -p.value(True) -print(p.value()) -p.off() -print(p.value()) -p.on() -print(p.value()) -p.off() diff --git a/tests/renesas-ra/pin.py.exp b/tests/renesas-ra/pin.py.exp index 4c8cc50b0c06f..0c34cd7a39ca6 100644 --- a/tests/renesas-ra/pin.py.exp +++ b/tests/renesas-ra/pin.py.exp @@ -1,10 +1,2 @@ -OK -1 -0 -1 -0 -1 -0 -1 -0 -1 +pass +pass From af100b70290d20d7e9c3e7d49713e3916b6f05b6 Mon Sep 17 00:00:00 2001 From: Takeo Takahashi Date: Thu, 23 Jun 2022 22:29:00 +0900 Subject: [PATCH 2557/3533] docs/renesas-ra: Add pin drive keyword argument description. Signed-off-by: Takeo Takahashi --- docs/renesas-ra/quickref.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/renesas-ra/quickref.rst b/docs/renesas-ra/quickref.rst index 23ffab50eb80f..47b49575bd5be 100644 --- a/docs/renesas-ra/quickref.rst +++ b/docs/renesas-ra/quickref.rst @@ -110,6 +110,24 @@ Use the :ref:`machine.Pin ` class:: Pin id is available corresponding to the RA MCU's pin name which are Pin.cpu.P106 and 'P106'. The RA MCU has many feature's pins. However, there are some cases that pin feature is fixed or not connected by the board. Please confirm the board manual for the pin mapping. +The following *drive* keyword argument are available if the port drive capability of the Pin is supported by the MCU:: + + Pin.DRIVE_0: Low drive + Pin.DRIVE_1: Middle drive + Pin.DRIVE_2: Middle drive for I2C Fast-mode + Pin.DRIVE_3: High drive + +The *alt* keyword argument is not supported. + +The following functions are not supported:: + + Pin.irq(priority=) # priority keyword argument is not supported + Pin.irq(wake=) # wake keyword argument is not supported + Pin.irq(hard=) # hard keyword argument is ignored because hardware interrupt is used + Pin.mode() + Pin.pull() + Pin.drive() + UART (serial bus) ----------------- From 0b26efe73dd3396bdc2b77651a78d9f2edeb9004 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 28 Aug 2022 18:14:53 +0200 Subject: [PATCH 2558/3533] extmod/machine_i2c: Call MICROPY_PY_EVENT_HOOK during i2c.scan(). Avoiding a watchdog reset during i2c.scan() if the hardware is not properly set up (eg on esp8266), and also allowing to stop the scan with a KeyboardInterrupt. Fixes issue #8876. --- extmod/machine_i2c.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 2aa217914bfb5..ff597b58c5e5c 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -328,6 +328,9 @@ STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) { if (ret == 0) { mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); } + #ifdef MICROPY_EVENT_POLL_HOOK + MICROPY_EVENT_POLL_HOOK + #endif } return list; } From da7f2537a124191371cbc3bc625ed96a43dc29c7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 10 Aug 2022 22:53:39 +1000 Subject: [PATCH 2559/3533] top: Use micropython-lib unconditionally in manifests. micropython-lib is now a submodule, and the manifest compilation process will ensure it is available, so manifests no longer need to check that it is available. Signed-off-by: Jim Mussared --- ports/esp8266/boards/GENERIC/manifest.py | 18 ++++++++---------- .../ARDUINO_NANO_RP2040_CONNECT/manifest.py | 3 +-- ports/rp2/boards/PICO_W/manifest.py | 3 +-- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/ports/esp8266/boards/GENERIC/manifest.py b/ports/esp8266/boards/GENERIC/manifest.py index 9ce3ffe3aae15..ef25040ef8a76 100644 --- a/ports/esp8266/boards/GENERIC/manifest.py +++ b/ports/esp8266/boards/GENERIC/manifest.py @@ -7,15 +7,13 @@ # drivers freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") -# Libraries from micropython-lib, include only if the library directory exists -if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): - # file utilities - freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") +# micropython-lib: file utilities +freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") - # requests - freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") - freeze("$(MPY_LIB_DIR)/micropython/urllib.urequest", "urllib/urequest.py") +# micropython-lib: requests +freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") +freeze("$(MPY_LIB_DIR)/micropython/urllib.urequest", "urllib/urequest.py") - # umqtt - freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") - freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") +# micropython-lib: umqtt +freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") +freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") diff --git a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py index 0448aa89a11e4..7a2f1c9e2738c 100644 --- a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py +++ b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py @@ -8,5 +8,4 @@ l2cap=True, security=True, ) -if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): - freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") +freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") diff --git a/ports/rp2/boards/PICO_W/manifest.py b/ports/rp2/boards/PICO_W/manifest.py index eb748da485c98..d2c76602affe8 100644 --- a/ports/rp2/boards/PICO_W/manifest.py +++ b/ports/rp2/boards/PICO_W/manifest.py @@ -4,5 +4,4 @@ freeze("$(MPY_DIR)/tools", "upip_utarfile.py") freeze("$(MPY_DIR)/extmod", "ntptime.py") -if os.path.isdir(convert_path("$(MPY_LIB_DIR)")): - freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") +freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") From f3cdb052db1784b38e02f043cb72bda5a57b8696 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 13 Jul 2022 23:36:45 +1000 Subject: [PATCH 2560/3533] tools/manifestfile.py: Add library for working with manifests. This splits the manifest file loading logic from makemanifest.py and updates makemanifest.py to use it. This will allow non-freezing uses of manifests, such as defining packages and dependencies in micropython-lib. Also adds additional methods to the manifest "API": - require() - to get a package from micropython-lib. - module() - to define a single-file module - package() - to define a multi-file package module() and package() should replace most uses of freeze() and can also be also used in non-freezing scenarios. Signed-off-by: Jim Mussared --- tools/makemanifest.py | 302 +++++++----------------------- tools/manifestfile.py | 416 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 477 insertions(+), 241 deletions(-) create mode 100644 tools/manifestfile.py diff --git a/tools/makemanifest.py b/tools/makemanifest.py index e69698d3f2340..800a25435eb91 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -29,127 +29,10 @@ import os import subprocess - -########################################################################### -# Public functions to be used in the manifest - - -def include(manifest, **kwargs): - """Include another manifest. - - The manifest argument can be a string (filename) or an iterable of - strings. - - Relative paths are resolved with respect to the current manifest file. - - Optional kwargs can be provided which will be available to the - included script via the `options` variable. - - e.g. include("path.py", extra_features=True) - - in path.py: - options.defaults(standard_features=True) - - # freeze minimal modules. - if options.standard_features: - # freeze standard modules. - if options.extra_features: - # freeze extra modules. - """ - - if not isinstance(manifest, str): - for m in manifest: - include(m) - else: - manifest = convert_path(manifest) - with open(manifest) as f: - # Make paths relative to this manifest file while processing it. - # Applies to includes and input files. - prev_cwd = os.getcwd() - os.chdir(os.path.dirname(manifest)) - exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)}) - os.chdir(prev_cwd) - - -def freeze(path, script=None, opt=0): - """Freeze the input, automatically determining its type. A .py script - will be compiled to a .mpy first then frozen, and a .mpy file will be - frozen directly. - - `path` must be a directory, which is the base directory to search for - files from. When importing the resulting frozen modules, the name of - the module will start after `path`, ie `path` is excluded from the - module name. - - If `path` is relative, it is resolved to the current manifest.py. - Use $(MPY_DIR), $(MPY_LIB_DIR), $(PORT_DIR), $(BOARD_DIR) if you need - to access specific paths. - - If `script` is None all files in `path` will be frozen. - - If `script` is an iterable then freeze() is called on all items of the - iterable (with the same `path` and `opt` passed through). - - If `script` is a string then it specifies the file or directory to - freeze, and can include extra directories before the file or last - directory. The file or directory will be searched for in `path`. If - `script` is a directory then all files in that directory will be frozen. - - `opt` is the optimisation level to pass to mpy-cross when compiling .py - to .mpy. - """ - - freeze_internal(KIND_AUTO, path, script, opt) - - -def freeze_as_str(path): - """Freeze the given `path` and all .py scripts within it as a string, - which will be compiled upon import. - """ - - freeze_internal(KIND_AS_STR, path, None, 0) - - -def freeze_as_mpy(path, script=None, opt=0): - """Freeze the input (see above) by first compiling the .py scripts to - .mpy files, then freezing the resulting .mpy files. - """ - - freeze_internal(KIND_AS_MPY, path, script, opt) - - -def freeze_mpy(path, script=None, opt=0): - """Freeze the input (see above), which must be .mpy files that are - frozen directly. - """ - - freeze_internal(KIND_MPY, path, script, opt) - - -########################################################################### -# Internal implementation - -KIND_AUTO = 0 -KIND_AS_STR = 1 -KIND_AS_MPY = 2 -KIND_MPY = 3 +import manifestfile VARS = {} -manifest_list = [] - - -class IncludeOptions: - def __init__(self, **kwargs): - self._kwargs = kwargs - self._defaults = {} - - def defaults(self, **kwargs): - self._defaults = kwargs - - def __getattr__(self, name): - return self._kwargs.get(name, self._defaults.get(name, None)) - class FreezeError(Exception): pass @@ -163,15 +46,6 @@ def system(cmd): return -1, er.output -def convert_path(path): - # Perform variable substituion. - for name, value in VARS.items(): - path = path.replace("$({})".format(name), value) - # Convert to absolute path (so that future operations don't rely on - # still being chdir'ed). - return os.path.abspath(path) - - def get_timestamp(path, default=None): try: stat = os.stat(path) @@ -182,119 +56,64 @@ def get_timestamp(path, default=None): return default -def get_timestamp_newest(path): - ts_newest = 0 - for dirpath, dirnames, filenames in os.walk(path, followlinks=True): - for f in filenames: - ts_newest = max(ts_newest, get_timestamp(os.path.join(dirpath, f))) - return ts_newest - - def mkdir(filename): path = os.path.dirname(filename) if not os.path.isdir(path): os.makedirs(path) -def freeze_internal(kind, path, script, opt): - path = convert_path(path) - if not os.path.isdir(path): - raise FreezeError("freeze path must be a directory: {}".format(path)) - if script is None and kind == KIND_AS_STR: - manifest_list.append((KIND_AS_STR, path, script, opt)) - elif script is None or isinstance(script, str) and script.find(".") == -1: - # Recursively search `path` for files to freeze, optionally restricted - # to a subdirectory specified by `script` - if script is None: - subdir = "" - else: - subdir = "/" + script - for dirpath, dirnames, filenames in os.walk(path + subdir, followlinks=True): - for f in filenames: - freeze_internal(kind, path, (dirpath + "/" + f)[len(path) + 1 :], opt) - elif not isinstance(script, str): - # `script` is an iterable of items to freeze - for s in script: - freeze_internal(kind, path, s, opt) - else: - # `script` should specify an individual file to be frozen - extension_kind = {KIND_AS_MPY: ".py", KIND_MPY: ".mpy"} - if kind == KIND_AUTO: - for k, ext in extension_kind.items(): - if script.endswith(ext): - kind = k - break - else: - print("warn: unsupported file type, skipped freeze: {}".format(script)) - return - wanted_extension = extension_kind[kind] - if not script.endswith(wanted_extension): - raise FreezeError("expecting a {} file, got {}".format(wanted_extension, script)) - manifest_list.append((kind, path, script, opt)) - - # Formerly make-frozen.py. # This generates: # - MP_FROZEN_STR_NAMES macro # - mp_frozen_str_sizes # - mp_frozen_str_content -def generate_frozen_str_content(paths): - def module_name(f): - return f - - modules = [] - output = [b"#include \n"] - - for path in paths: - root = path.rstrip("/") - root_len = len(root) - - for dirpath, dirnames, filenames in os.walk(root): - for f in filenames: - fullpath = dirpath + "/" + f - st = os.stat(fullpath) - modules.append((path, fullpath[root_len + 1 :], st)) - - output.append(b"#define MP_FROZEN_STR_NAMES \\\n") - for _path, f, st in modules: - m = module_name(f) - output.append(b'"%s\\0" \\\n' % m.encode()) +def generate_frozen_str_content(modules): + output = [ + b"#include \n", + b"#define MP_FROZEN_STR_NAMES \\\n", + ] + + for _, target_path in modules: + print("STR", target_path) + output.append(b'"%s\\0" \\\n' % target_path.encode()) output.append(b"\n") output.append(b"const uint32_t mp_frozen_str_sizes[] = { ") - for _path, f, st in modules: + for full_path, _ in modules: + st = os.stat(full_path) output.append(b"%d, " % st.st_size) output.append(b"0 };\n") output.append(b"const char mp_frozen_str_content[] = {\n") - for path, f, st in modules: - data = open(path + "/" + f, "rb").read() - - # We need to properly escape the script data to create a C string. - # When C parses hex characters of the form \x00 it keeps parsing the hex - # data until it encounters a non-hex character. Thus one must create - # strings of the form "data\x01" "abc" to properly encode this kind of - # data. We could just encode all characters as hex digits but it's nice - # to be able to read the resulting C code as ASCII when possible. - - data = bytearray(data) # so Python2 extracts each byte as an integer - esc_dict = {ord("\n"): b"\\n", ord("\r"): b"\\r", ord('"'): b'\\"', ord("\\"): b"\\\\"} - output.append(b'"') - break_str = False - for c in data: - try: - output.append(esc_dict[c]) - except KeyError: - if 32 <= c <= 126: - if break_str: - output.append(b'" "') - break_str = False - output.append(chr(c).encode()) - else: - output.append(b"\\x%02x" % c) - break_str = True - output.append(b'\\0"\n') + for full_path, _ in modules: + with open(full_path, "rb") as f: + data = f.read() + + # We need to properly escape the script data to create a C string. + # When C parses hex characters of the form \x00 it keeps parsing the hex + # data until it encounters a non-hex character. Thus one must create + # strings of the form "data\x01" "abc" to properly encode this kind of + # data. We could just encode all characters as hex digits but it's nice + # to be able to read the resulting C code as ASCII when possible. + + data = bytearray(data) # so Python2 extracts each byte as an integer + esc_dict = {ord("\n"): b"\\n", ord("\r"): b"\\r", ord('"'): b'\\"', ord("\\"): b"\\\\"} + output.append(b'"') + break_str = False + for c in data: + try: + output.append(esc_dict[c]) + except KeyError: + if 32 <= c <= 126: + if break_str: + output.append(b'" "') + break_str = False + output.append(chr(c).encode()) + else: + output.append(b"\\x%02x" % c) + break_str = True + output.append(b'\\0"\n') output.append(b'"\\0"\n};\n\n') return b"".join(output) @@ -340,14 +159,13 @@ def main(): print("mpy-cross not found at {}, please build it first".format(MPY_CROSS)) sys.exit(1) + manifest = manifestfile.ManifestFile(manifestfile.MODE_FREEZE, VARS) + # Include top-level inputs, to generate the manifest for input_manifest in args.files: try: - if input_manifest.endswith(".py"): - include(input_manifest) - else: - exec(input_manifest) - except FreezeError as er: + manifest.execute(input_manifest) + except manifestfile.ManifestFileError as er: print('freeze error executing "{}": {}'.format(input_manifest, er.args[0])) sys.exit(1) @@ -355,22 +173,25 @@ def main(): str_paths = [] mpy_files = [] ts_newest = 0 - for kind, path, script, opt in manifest_list: - if kind == KIND_AS_STR: - str_paths.append(path) - ts_outfile = get_timestamp_newest(path) - elif kind == KIND_AS_MPY: - infile = "{}/{}".format(path, script) - outfile = "{}/frozen_mpy/{}.mpy".format(args.build_dir, script[:-3]) - ts_infile = get_timestamp(infile) + for full_path, target_path, timestamp, kind, version, opt in manifest.files(): + if kind == manifestfile.KIND_FREEZE_AS_STR: + str_paths.append( + ( + full_path, + target_path, + ) + ) + ts_outfile = timestamp + elif kind == manifestfile.KIND_FREEZE_AS_MPY: + outfile = "{}/frozen_mpy/{}.mpy".format(args.build_dir, target_path[:-3]) ts_outfile = get_timestamp(outfile, 0) - if ts_infile >= ts_outfile: - print("MPY", script) + if timestamp >= ts_outfile: + print("MPY", target_path) mkdir(outfile) res, out = system( [MPY_CROSS] + args.mpy_cross_flags.split() - + ["-o", outfile, "-s", script, "-O{}".format(opt), infile] + + ["-o", outfile, "-s", target_path, "-O{}".format(opt), full_path] ) if res != 0: print("error compiling {}:".format(infile)) @@ -379,10 +200,9 @@ def main(): ts_outfile = get_timestamp(outfile) mpy_files.append(outfile) else: - assert kind == KIND_MPY - infile = "{}/{}".format(path, script) - mpy_files.append(infile) - ts_outfile = get_timestamp(infile) + assert kind == manifestfile.KIND_FREEZE_MPY + mpy_files.append(full_path) + ts_outfile = timestamp ts_newest = max(ts_newest, ts_outfile) # Check if output file needs generating diff --git a/tools/manifestfile.py b/tools/manifestfile.py new file mode 100644 index 0000000000000..cb155da2101cb --- /dev/null +++ b/tools/manifestfile.py @@ -0,0 +1,416 @@ +#!/usr/bin/env python3 +# +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2022 Jim Mussared +# Copyright (c) 2019 Damien P. George +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from __future__ import print_function +import os +import sys +import glob + +__all__ = ["ManifestFileError", "ManifestFile"] + +# Allow freeze*() etc. +MODE_FREEZE = 1 +# Only allow include/require/module/package. +MODE_COMPILE = 2 + + +# In compile mode, .py -> KIND_COMPILE_AS_MPY +# In freeze mode, .py -> KIND_FREEZE_AS_MPY, .mpy->KIND_FREEZE_MPY +KIND_AUTO = 1 +# Freeze-mode only, .py -> KIND_FREEZE_AS_MPY, .mpy->KIND_FREEZE_MPY +KIND_FREEZE_AUTO = 2 + +# Freeze-mode only, The .py file will be frozen as text. +KIND_FREEZE_AS_STR = 3 +# Freeze-mode only, The .py file will be compiled and frozen as bytecode. +KIND_FREEZE_AS_MPY = 4 +# Freeze-mode only, The .mpy file will be frozen directly. +KIND_FREEZE_MPY = 5 +# Compile mode only, the .py file should be compiled to .mpy. +KIND_COMPILE_AS_MPY = 6 + +# File on the local filesystem. +FILE_TYPE_LOCAL = 1 +# URL to file. (TODO) +FILE_TYPE_HTTP = 2 + + +class ManifestFileError(Exception): + pass + + +# Turns a dict of options into a object with attributes used to turn the +# kwargs passed to include() and require into the "options" global in the +# included manifest. +# options = IncludeOptions(foo="bar", blah="stuff") +# options.foo # "bar" +# options.blah # "stuff" +class IncludeOptions: + def __init__(self, **kwargs): + self._kwargs = kwargs + self._defaults = {} + + def defaults(self, **kwargs): + self._defaults = kwargs + + def __getattr__(self, name): + return self._kwargs.get(name, self._defaults.get(name, None)) + + +class ManifestFile: + def __init__(self, mode, path_vars=None): + # Either MODE_FREEZE or MODE_COMPILE. + self._mode = mode + # Path substition variables. + self._path_vars = path_vars or {} + # List of files references by this manifest. + # Tuple of (file_type, full_path, target_path, timestamp, kind, version, opt) + self._manifest_files = [] + # Don't allow including the same file twice. + self._visited = set() + + def _resolve_path(self, path): + # Convert path to an absolute path, applying variable substitutions. + for name, value in self._path_vars.items(): + if value is not None: + path = path.replace("$({})".format(name), value) + return os.path.abspath(path) + + def _manifest_globals(self, kwargs): + # This is the "API" available to a manifest file. + return { + "metadata": self.metadata, + "include": self.include, + "require": self.require, + "package": self.package, + "module": self.module, + "freeze": self.freeze, + "freeze_as_str": self.freeze_as_str, + "freeze_as_mpy": self.freeze_as_mpy, + "freeze_mpy": self.freeze_mpy, + "options": IncludeOptions(**kwargs), + } + + def files(self): + return self._manifest_files + + def execute(self, manifest_file): + if manifest_file.endswith(".py"): + # Execute file from filesystem. + self.include(manifest_file) + else: + # Execute manifest code snippet. + try: + exec(manifest_file, self._manifest_globals({})) + except Exception as er: + raise ManifestFileError("Error in manifest: {}".format(er)) + + def _add_file(self, full_path, target_path, kind=KIND_AUTO, version=None, opt=None): + # Check file exists and get timestamp. + try: + stat = os.stat(full_path) + timestamp = stat.st_mtime + except OSError: + raise ManifestFileError("cannot stat {}".format(full_path)) + + # Map the AUTO kinds to their actual kind based on mode and extension. + _, ext = os.path.splitext(full_path) + if self._mode == MODE_FREEZE: + if kind in ( + KIND_AUTO, + KIND_FREEZE_AUTO, + ): + if ext.lower() == ".py": + kind = KIND_FREEZE_AS_MPY + elif ext.lower() == ".mpy": + kind = KIND_FREEZE_MPY + else: + if kind != KIND_AUTO: + raise ManifestFileError("Not in freeze mode") + if ext.lower() != ".py": + raise ManifestFileError("Expected .py file") + kind = KIND_COMPILE_AS_MPY + + self._manifest_files.append( + (FILE_TYPE_LOCAL, full_path, target_path, timestamp, kind, version, opt) + ) + + def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=False): + base_path = self._resolve_path(base_path) + + if files: + # Use explicit list of files (relative to package_path). + for file in files: + if package_path: + file = os.path.join(package_path, file) + self._add_file( + os.path.join(base_path, file), file, kind=kind, version=None, opt=opt + ) + else: + if base_path: + prev_cwd = os.getcwd() + os.chdir(self._resolve_path(base_path)) + + # Find all candidate files. + for dirpath, _, filenames in os.walk(package_path or ".", followlinks=True): + for file in filenames: + file = os.path.relpath(os.path.join(dirpath, file), ".") + _, ext = os.path.splitext(file) + if ext.lower() in exts: + self._add_file( + os.path.join(base_path, file), + file, + kind=kind, + version=None, + opt=opt, + ) + elif strict: + raise ManifestFileError("Unexpected file type") + + if base_path: + os.chdir(prev_cwd) + + def metadata(self, description=None, version=None): + # TODO + pass + + def include_maybe(self, manifest_path, **kwargs): + """ + Include the manifest file if it exists. See docs for include(). + """ + if os.path.exists(manifest_path): + self.include(manifest_path, **kwargs) + + def include(self, manifest_path, **kwargs): + """ + Include another manifest. + + The manifest argument can be a string (filename) or an iterable of + strings. + + Relative paths are resolved with respect to the current manifest file. + + Optional kwargs can be provided which will be available to the + included script via the `options` variable. + + e.g. include("path.py", extra_features=True) + + in path.py: + options.defaults(standard_features=True) + + # freeze minimal modules. + if options.standard_features: + # freeze standard modules. + if options.extra_features: + # freeze extra modules. + """ + if not isinstance(manifest_path, str): + for m in manifest_path: + self.include(m) + else: + manifest_path = self._resolve_path(manifest_path) + if manifest_path in self._visited: + return + self._visited.add(manifest_path) + with open(manifest_path) as f: + # Make paths relative to this manifest file while processing it. + # Applies to includes and input files. + prev_cwd = os.getcwd() + os.chdir(os.path.dirname(manifest_path)) + try: + exec(f.read(), self._manifest_globals(kwargs)) + except Exception as er: + raise ManifestFileError( + "Error in manifest file: {}: {}".format(manifest_path, er) + ) + os.chdir(prev_cwd) + + def require(self, name, version=None, **kwargs): + """ + Require a module by name from micropython-lib. + + This is a shortcut for + """ + if self._path_vars["MPY_LIB_DIR"]: + for manifest_path in glob.glob( + os.path.join(self._path_vars["MPY_LIB_DIR"], "**", name, "manifest.py"), + recursive=True, + ): + self.include(manifest_path, **kwargs) + return + raise ValueError("Library not found in local micropython-lib: {}".format(name)) + else: + # TODO: HTTP request to obtain URLs from manifest.json. + raise ValueError("micropython-lib not available for require('{}').", name) + + def package(self, package_path, files=None, base_path=".", opt=None): + """ + Define a package, optionally restricting to a set of files. + + Simple case, a package in the current directory: + package("foo") + will include all .py files in foo, and will be stored as foo/bar/baz.py. + + If the package isn't in the current directory, use base_path: + package("foo", base_path="src") + + To restrict to certain files in the package use files (note: paths should be relative to the package): + package("foo", files=["bar/baz.py"]) + """ + # Include "base_path/package_path/**/*.py" --> "package_path/**/*.py" + self._search(base_path, package_path, files, exts=(".py",), kind=KIND_AUTO, opt=opt) + + def module(self, module_path, base_path=".", opt=None): + """ + Include a single Python file as a module. + + If the file is in the current directory: + module("foo.py") + + Otherwise use base_path to locate the file: + module("foo.py", "src/drivers") + """ + # Include "base_path/module_path" --> "module_path" + base_path = self._resolve_path(base_path) + _, ext = os.path.splitext(module_path) + if ext.lower() != ".py": + raise ManifestFileError("module must be .py file") + # TODO: version None + self._add_file(os.path.join(base_path, module_path), module_path, version=None, opt=opt) + + def _freeze_internal(self, path, script, exts, kind, opt): + if script is None: + self._search(path, None, None, exts=exts, kind=kind, opt=opt) + elif isinstance(script, str) and os.path.isdir(os.path.join(path, script)): + self._search(path, script, None, exts=exts, kind=kind, opt=opt) + elif not isinstance(script, str): + self._search(path, None, script, exts=exts, kind=kind, opt=opt) + else: + self._search(path, None, (script,), exts=exts, kind=kind, opt=opt) + + def freeze(self, path, script=None, opt=None): + """ + Freeze the input, automatically determining its type. A .py script + will be compiled to a .mpy first then frozen, and a .mpy file will be + frozen directly. + + `path` must be a directory, which is the base directory to _search for + files from. When importing the resulting frozen modules, the name of + the module will start after `path`, ie `path` is excluded from the + module name. + + If `path` is relative, it is resolved to the current manifest.py. + Use $(MPY_DIR), $(MPY_LIB_DIR), $(PORT_DIR), $(BOARD_DIR) if you need + to access specific paths. + + If `script` is None all files in `path` will be frozen. + + If `script` is an iterable then freeze() is called on all items of the + iterable (with the same `path` and `opt` passed through). + + If `script` is a string then it specifies the file or directory to + freeze, and can include extra directories before the file or last + directory. The file or directory will be _searched for in `path`. If + `script` is a directory then all files in that directory will be frozen. + + `opt` is the optimisation level to pass to mpy-cross when compiling .py + to .mpy. + """ + self._freeze_internal(path, script, exts=(".py", ".mpy"), kind=KIND_FREEZE_AUTO, opt=opt) + + def freeze_as_str(self, path): + """ + Freeze the given `path` and all .py scripts within it as a string, + which will be compiled upon import. + """ + self._search(path, None, None, exts=(".py"), kind=KIND_FREEZE_AS_STR) + + def freeze_as_mpy(self, path, script=None, opt=None): + """ + Freeze the input (see above) by first compiling the .py scripts to + .mpy files, then freezing the resulting .mpy files. + """ + self._freeze_internal(path, script, exts=(".py"), kind=KIND_FREEZE_AS_MPY, opt=opt) + + def freeze_mpy(self, path, script=None, opt=None): + """ + Freeze the input (see above), which must be .mpy files that are + frozen directly. + """ + self._freeze_internal(path, script, exts=(".mpy"), kind=KIND_FREEZE_MPY, opt=opt) + + +def main(): + import argparse + + cmd_parser = argparse.ArgumentParser(description="List the files referenced by a manifest.") + cmd_parser.add_argument("--freeze", action="store_true", help="freeze mode") + cmd_parser.add_argument("--compile", action="store_true", help="compile mode") + cmd_parser.add_argument( + "--lib", + default=os.path.join(os.path.dirname(__file__), "../lib/micropython-lib"), + help="path to micropython-lib repo", + ) + cmd_parser.add_argument("--port", default=None, help="path to port dir") + cmd_parser.add_argument("--board", default=None, help="path to board dir") + cmd_parser.add_argument( + "--top", + default=os.path.join(os.path.dirname(__file__), ".."), + help="path to micropython repo", + ) + cmd_parser.add_argument("files", nargs="+", help="input manifest.py") + args = cmd_parser.parse_args() + + path_vars = { + "MPY_DIR": os.path.abspath(args.top) if args.top else None, + "BOARD_DIR": os.path.abspath(args.board) if args.board else None, + "PORT_DIR": os.path.abspath(args.port) if args.port else None, + "MPY_LIB_DIR": os.path.abspath(args.lib) if args.lib else None, + } + + mode = None + if args.freeze: + mode = MODE_FREEZE + elif args.compile: + mode = MODE_COMPILE + else: + print("Error: No mode specified.", file=sys.stderr) + exit(1) + + m = ManifestFile(mode, path_vars) + for manifest_file in args.files: + try: + m.execute(manifest_file) + except ManifestFileError as er: + print(er, file=sys.stderr) + exit(1) + for f in m.files(): + print(f) + + +if __name__ == "__main__": + main() From e42809531f735b376409d458e51e25f9708ec551 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 15 Jul 2022 23:01:06 +1000 Subject: [PATCH 2561/3533] mpy-cross/mpy_cross: Add Python wrapper for mpy-cross. Rather than invoking mpy-cross directly via system/subprocess in our build tools and other tools, this provides a Python interface for it. Based on https://gitlab.com/alelec/mpy_cross (with the intention of eventually replacing that as the "official" pypi distribution once setup.py etc are added). Signed-off-by: Jim Mussared --- mpy-cross/mpy_cross/__init__.py | 113 ++++++++++++++++++++++++++++++++ mpy-cross/mpy_cross/__main__.py | 38 +++++++++++ 2 files changed, 151 insertions(+) create mode 100644 mpy-cross/mpy_cross/__init__.py create mode 100644 mpy-cross/mpy_cross/__main__.py diff --git a/mpy-cross/mpy_cross/__init__.py b/mpy-cross/mpy_cross/__init__.py new file mode 100644 index 0000000000000..d4c0930bb9198 --- /dev/null +++ b/mpy-cross/mpy_cross/__init__.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 +# +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2022 Andrew Leech +# Copyright (c) 2022 Jim Mussared +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from __future__ import print_function +import os +import stat +import subprocess + +NATIVE_ARCH_X86 = "x86" +NATIVE_ARCH_X64 = "x64" +NATIVE_ARCH_ARMV6 = "armv6" +NATIVE_ARCH_ARMV6M = "armv6m" +NATIVE_ARCH_ARMV7M = "armv7m" +NATIVE_ARCH_ARMV7EM = "armv7em" +NATIVE_ARCH_ARMV7EMSP = "armv7emsp" +NATIVE_ARCH_ARMV7EMDP = "armv7emdp" +NATIVE_ARCH_XTENSA = "xtensa" +NATIVE_ARCH_XTENSAWIN = "xtensawin" + +NATIVE_ARCHS = [ + NATIVE_ARCH_X86, + NATIVE_ARCH_X64, + NATIVE_ARCH_ARMV6, + NATIVE_ARCH_ARMV6M, + NATIVE_ARCH_ARMV7M, + NATIVE_ARCH_ARMV7EM, + NATIVE_ARCH_ARMV7EMSP, + NATIVE_ARCH_ARMV7EMDP, + NATIVE_ARCH_XTENSA, + NATIVE_ARCH_XTENSAWIN, +] + +__all__ = ["compile", "run", "CrossCompileError"] + + +class CrossCompileError(Exception): + pass + + +def find_mpy_cross_binary(mpy_cross): + if mpy_cross: + return mpy_cross + return os.path.abspath(os.path.join(os.path.dirname(__file__), "../mpy-cross")) + + +def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, extra_args=None): + if not src: + raise ValueError("src is required") + if not os.path.exists(src): + raise CrossCompileError("Input .py file not found: {}.".format(src_py)) + + args = [] + + if src_path: + args += ["-s", src_path] + + if dest: + args += ["-o", dest] + + if march: + args += ["-march", march] + + if opt is not None: + args += ["-O{}".format(opt)] + + if extra_args: + args += extra_args + + args += [src] + + run(args, mpy_cross) + + +def run(args, mpy_cross=None): + mpy_cross = find_mpy_cross_binary(mpy_cross) + + if not os.path.exists(mpy_cross): + raise CrossCompileError("mpy-cross binary not found at {}.".format(mpy_cross)) + + try: + st = os.stat(mpy_cross) + os.chmod(mpy_cross, st.st_mode | stat.S_IEXEC) + except OSError: + pass + + try: + subprocess.check_output([mpy_cross] + args, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as er: + raise CrossCompileError(er.output) diff --git a/mpy-cross/mpy_cross/__main__.py b/mpy-cross/mpy_cross/__main__.py new file mode 100644 index 0000000000000..9d957bca025d6 --- /dev/null +++ b/mpy-cross/mpy_cross/__main__.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2022 Andrew Leech +# Copyright (c) 2022 Jim Mussared +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from __future__ import print_function +import argparse +import sys + +from . import run, CrossCompileError + +try: + run(sys.argv[1:]) +except CrossCompileError as er: + print(er.args[0], file=sys.stderr) + raise SystemExit(1) From 6bd0ec7a70496dc51d8dc6c706919ef398b0346e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 15 Jul 2022 23:06:15 +1000 Subject: [PATCH 2562/3533] tools/makemanifest.py: Update to use mpy_cross module. Signed-off-by: Jim Mussared --- tools/makemanifest.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 800a25435eb91..2e7021040dd97 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -29,6 +29,9 @@ import os import subprocess +sys.path.append(os.path.join(os.path.dirname(__file__), "../mpy-cross")) +import mpy_cross + import manifestfile VARS = {} @@ -173,7 +176,7 @@ def main(): str_paths = [] mpy_files = [] ts_newest = 0 - for full_path, target_path, timestamp, kind, version, opt in manifest.files(): + for _file_type, full_path, target_path, timestamp, kind, version, opt in manifest.files(): if kind == manifestfile.KIND_FREEZE_AS_STR: str_paths.append( ( @@ -188,14 +191,18 @@ def main(): if timestamp >= ts_outfile: print("MPY", target_path) mkdir(outfile) - res, out = system( - [MPY_CROSS] - + args.mpy_cross_flags.split() - + ["-o", outfile, "-s", target_path, "-O{}".format(opt), full_path] - ) - if res != 0: - print("error compiling {}:".format(infile)) - sys.stdout.buffer.write(out) + try: + mpy_cross.compile( + full_path, + dest=outfile, + src_path=target_path, + opt=opt, + mpy_cross=MPY_CROSS, + extra_args=args.mpy_cross_flags.split(), + ) + except mpy_cross.CrossCompileError as ex: + print("error compiling {}:".format(target_path)) + print(ex.args[0]) raise SystemExit(1) ts_outfile = get_timestamp(outfile) mpy_files.append(outfile) From e9a28ce312b75a4f2b3b7a8c3e38b7766cb567b3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 15 Jul 2022 23:42:51 +1000 Subject: [PATCH 2563/3533] tools/manifestfile.py: Allow include of directory path. If an include path is a directory, then it implicitly grabs the manifest.py file inside that directory. This simplifies most manifest.py files. Signed-off-by: Jim Mussared --- tools/manifestfile.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tools/manifestfile.py b/tools/manifestfile.py index cb155da2101cb..bf626c22b38d8 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -198,13 +198,6 @@ def metadata(self, description=None, version=None): # TODO pass - def include_maybe(self, manifest_path, **kwargs): - """ - Include the manifest file if it exists. See docs for include(). - """ - if os.path.exists(manifest_path): - self.include(manifest_path, **kwargs) - def include(self, manifest_path, **kwargs): """ Include another manifest. @@ -214,6 +207,9 @@ def include(self, manifest_path, **kwargs): Relative paths are resolved with respect to the current manifest file. + If the path is to a directory, then it implicitly includes the + manifest.py file inside that directory. + Optional kwargs can be provided which will be available to the included script via the `options` variable. @@ -233,6 +229,9 @@ def include(self, manifest_path, **kwargs): self.include(m) else: manifest_path = self._resolve_path(manifest_path) + # Including a directory grabs the manifest.py inside it. + if os.path.isdir(manifest_path): + manifest_path = os.path.join(manifest_path, "manifest.py") if manifest_path in self._visited: return self._visited.add(manifest_path) From bc23f207cefed82172551288ebb8686ee2c512a3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 11 Aug 2022 00:16:48 +1000 Subject: [PATCH 2564/3533] tools/manifestfile.py: Allow require() to specify unix packages. By default, don't include micropython-lib/unix-ffi in the search. If unix_ffi=True is passed to require(), then include unix-ffi and make it take precedence over the other locations (e.g. python-stdlib). This does two things: - Prevents non-unix builds from using unix-only packages. - Allows the unix build to optionally use a more full-featured (e.g. ffi) based package, even with the same name as one from e.g. stdlib. Signed-off-by: Jim Mussared --- tools/manifestfile.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/tools/manifestfile.py b/tools/manifestfile.py index bf626c22b38d8..61560e4565abe 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -248,19 +248,28 @@ def include(self, manifest_path, **kwargs): ) os.chdir(prev_cwd) - def require(self, name, version=None, **kwargs): + def require(self, name, version=None, unix_ffi=False, **kwargs): """ Require a module by name from micropython-lib. - This is a shortcut for + Optionally specify unix_ffi=True to use a module from the unix-ffi directory. """ if self._path_vars["MPY_LIB_DIR"]: - for manifest_path in glob.glob( - os.path.join(self._path_vars["MPY_LIB_DIR"], "**", name, "manifest.py"), - recursive=True, - ): - self.include(manifest_path, **kwargs) - return + lib_dirs = ["micropython", "python-stdlib", "python-ecosys"] + if unix_ffi: + # Search unix-ffi only if unix_ffi=True, and make unix-ffi modules + # take precedence. + lib_dirs = ["unix-ffi"] + lib_dirs + + for lib_dir in lib_dirs: + for manifest_path in glob.glob( + os.path.join( + self._path_vars["MPY_LIB_DIR"], lib_dir, "**", name, "manifest.py" + ), + recursive=True, + ): + self.include(manifest_path, **kwargs) + return raise ValueError("Library not found in local micropython-lib: {}".format(name)) else: # TODO: HTTP request to obtain URLs from manifest.json. From 5852fd7708d02d6ca85e4a5ed01d8263e3962631 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 12 Aug 2022 11:30:56 +1000 Subject: [PATCH 2565/3533] tools/manifestfile.py: Allow manifests to set metadata. The metadata can be version, description, and license. After executing a manifest, the top-level metadata can be queried, and also each file output from the manifest will have the metadata of the containing manifest. Use the version metadata to "tag" files before freezing such that they have __version__ available. --- tools/makemanifest.py | 46 +++++++++++----------- tools/manifestfile.py | 92 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 102 insertions(+), 36 deletions(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 2e7021040dd97..d059d4a2660fe 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -176,34 +176,36 @@ def main(): str_paths = [] mpy_files = [] ts_newest = 0 - for _file_type, full_path, target_path, timestamp, kind, version, opt in manifest.files(): - if kind == manifestfile.KIND_FREEZE_AS_STR: + for result in manifest.files(): + if result.kind == manifestfile.KIND_FREEZE_AS_STR: str_paths.append( ( - full_path, - target_path, + result.full_path, + result.target_path, ) ) - ts_outfile = timestamp - elif kind == manifestfile.KIND_FREEZE_AS_MPY: - outfile = "{}/frozen_mpy/{}.mpy".format(args.build_dir, target_path[:-3]) + ts_outfile = result.timestamp + elif result.kind == manifestfile.KIND_FREEZE_AS_MPY: + outfile = "{}/frozen_mpy/{}.mpy".format(args.build_dir, result.target_path[:-3]) ts_outfile = get_timestamp(outfile, 0) - if timestamp >= ts_outfile: - print("MPY", target_path) + if result.timestamp >= ts_outfile: + print("MPY", result.target_path) mkdir(outfile) - try: - mpy_cross.compile( - full_path, - dest=outfile, - src_path=target_path, - opt=opt, - mpy_cross=MPY_CROSS, - extra_args=args.mpy_cross_flags.split(), - ) - except mpy_cross.CrossCompileError as ex: - print("error compiling {}:".format(target_path)) - print(ex.args[0]) - raise SystemExit(1) + # Add __version__ to the end of the file before compiling. + with manifestfile.tagged_py_file(result.full_path, result.metadata) as tagged_path: + try: + mpy_cross.compile( + tagged_path, + dest=outfile, + src_path=result.target_path, + opt=result.opt, + mpy_cross=MPY_CROSS, + extra_args=args.mpy_cross_flags.split(), + ) + except mpy_cross.CrossCompileError as ex: + print("error compiling {}:".format(target_path)) + print(ex.args[0]) + raise SystemExit(1) ts_outfile = get_timestamp(outfile) mpy_files.append(outfile) else: diff --git a/tools/manifestfile.py b/tools/manifestfile.py index 61560e4565abe..84c79ed82b28f 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -26,9 +26,12 @@ # THE SOFTWARE. from __future__ import print_function +import contextlib import os import sys import glob +import tempfile +from collections import namedtuple __all__ = ["ManifestFileError", "ManifestFile"] @@ -63,6 +66,37 @@ class ManifestFileError(Exception): pass +# The set of files that this manifest references. +ManifestOutput = namedtuple( + "ManifestOutput", + [ + "file_type", # FILE_TYPE_*. + "full_path", # The input file full path. + "target_path", # The target path on the device. + "timestamp", # Last modified date of the input file. + "kind", # KIND_*. + "metadata", # Metadata for the containing package. + "opt", # Optimisation level (or None). + ], +) + + +# Represent the metadata for a package. +class ManifestMetadata: + def __init__(self): + self.version = None + self.description = None + self.license = None + + def update(self, description=None, version=None, license=None): + if description: + self.description = description + if version: + self.version = version + if license: + self.license = version + + # Turns a dict of options into a object with attributes used to turn the # kwargs passed to include() and require into the "options" global in the # included manifest. @@ -87,11 +121,12 @@ def __init__(self, mode, path_vars=None): self._mode = mode # Path substition variables. self._path_vars = path_vars or {} - # List of files references by this manifest. - # Tuple of (file_type, full_path, target_path, timestamp, kind, version, opt) + # List of files (as ManifestFileResult) references by this manifest. self._manifest_files = [] # Don't allow including the same file twice. self._visited = set() + # Stack of metadata for each level. + self._metadata = [ManifestMetadata()] def _resolve_path(self, path): # Convert path to an absolute path, applying variable substitutions. @@ -121,7 +156,7 @@ def files(self): def execute(self, manifest_file): if manifest_file.endswith(".py"): # Execute file from filesystem. - self.include(manifest_file) + self.include(manifest_file, top_level=True) else: # Execute manifest code snippet. try: @@ -129,7 +164,7 @@ def execute(self, manifest_file): except Exception as er: raise ManifestFileError("Error in manifest: {}".format(er)) - def _add_file(self, full_path, target_path, kind=KIND_AUTO, version=None, opt=None): + def _add_file(self, full_path, target_path, kind=KIND_AUTO, opt=None): # Check file exists and get timestamp. try: stat = os.stat(full_path) @@ -156,7 +191,9 @@ def _add_file(self, full_path, target_path, kind=KIND_AUTO, version=None, opt=No kind = KIND_COMPILE_AS_MPY self._manifest_files.append( - (FILE_TYPE_LOCAL, full_path, target_path, timestamp, kind, version, opt) + ManifestOutput( + FILE_TYPE_LOCAL, full_path, target_path, timestamp, kind, self._metadata[-1], opt + ) ) def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=False): @@ -167,9 +204,7 @@ def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=F for file in files: if package_path: file = os.path.join(package_path, file) - self._add_file( - os.path.join(base_path, file), file, kind=kind, version=None, opt=opt - ) + self._add_file(os.path.join(base_path, file), file, kind=kind, opt=opt) else: if base_path: prev_cwd = os.getcwd() @@ -185,7 +220,6 @@ def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=F os.path.join(base_path, file), file, kind=kind, - version=None, opt=opt, ) elif strict: @@ -194,11 +228,19 @@ def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=F if base_path: os.chdir(prev_cwd) - def metadata(self, description=None, version=None): - # TODO - pass + def metadata(self, description=None, version=None, license=None): + """ + From within a manifest file, use this to set the metadata for the + package described by current manifest. + + After executing a manifest file (via execute()), call this + to obtain the metadata for the top-level manifest file. + """ + + self._metadata[-1].update(description, version, license) + return self._metadata[-1] - def include(self, manifest_path, **kwargs): + def include(self, manifest_path, top_level=False, **kwargs): """ Include another manifest. @@ -235,6 +277,8 @@ def include(self, manifest_path, **kwargs): if manifest_path in self._visited: return self._visited.add(manifest_path) + if not top_level: + self._metadata.append(ManifestMetadata()) with open(manifest_path) as f: # Make paths relative to this manifest file while processing it. # Applies to includes and input files. @@ -247,6 +291,8 @@ def include(self, manifest_path, **kwargs): "Error in manifest file: {}: {}".format(manifest_path, er) ) os.chdir(prev_cwd) + if not top_level: + self._metadata.pop() def require(self, name, version=None, unix_ffi=False, **kwargs): """ @@ -308,7 +354,7 @@ def module(self, module_path, base_path=".", opt=None): if ext.lower() != ".py": raise ManifestFileError("module must be .py file") # TODO: version None - self._add_file(os.path.join(base_path, module_path), module_path, version=None, opt=opt) + self._add_file(os.path.join(base_path, module_path), module_path, opt=opt) def _freeze_internal(self, path, script, exts, kind, opt): if script is None: @@ -372,6 +418,24 @@ def freeze_mpy(self, path, script=None, opt=None): self._freeze_internal(path, script, exts=(".mpy"), kind=KIND_FREEZE_MPY, opt=opt) +# Generate a temporary file with a line appended to the end that adds __version__. +@contextlib.contextmanager +def tagged_py_file(path, metadata): + dest_fd, dest_path = tempfile.mkstemp(suffix=".py", text=True) + try: + with os.fdopen(dest_fd, "w") as dest: + with open(path, "r") as src: + contents = src.read() + dest.write(contents) + + # Don't overwrite a version definition if the file already has one in it. + if metadata.version and "__version__ =" not in contents: + dest.write("\n\n__version__ = {}\n".format(repr(metadata.version))) + yield dest_path + finally: + os.unlink(dest_path) + + def main(): import argparse From ccd210984e7a959694b2af4937ce953a822c2ea5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 5 Sep 2022 18:23:11 +1000 Subject: [PATCH 2566/3533] lib/micropython-lib: Update to latest version with manifest changes. Signed-off-by: Damien George --- lib/micropython-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/micropython-lib b/lib/micropython-lib index 70e422dc2e885..f3cfc52ab076f 160000 --- a/lib/micropython-lib +++ b/lib/micropython-lib @@ -1 +1 @@ -Subproject commit 70e422dc2e885bbaafe6eb7e3d81118e17d4b555 +Subproject commit f3cfc52ab076fc913dc6e266fb7370b418c8f542 From 203dae41fbaceeea9a04dc540b22de4ddd0d5a69 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 15 Jul 2022 23:46:28 +1000 Subject: [PATCH 2567/3533] all: Update all manifest.py files to use new features. Changes in this commit: - Manifest include's now use the directory path where possible (no longer necessary to include the manifest.py file explicitly). - Add manifest.py for all drivers and components that are referenced by port/board manifests. - Replace all uses of freeze() with package()/module(), except for port and board modules. - Use opt=3 everywhere, for consistency and to reduce code size. - Use require() instead of include() for all micropython-lib references. - Remove support for optional board-level manifest.py in mimxrt port, to make it behave the same as other ports (the board must set FROZEN_MANIFEST to a custom manifest.py, which can optionally include the default, port-level manifest). - Also reinstates modules that were accidentally removed from the esp8266 512k build in fbe9417b90474dd1a08749b3a79311a8007a98fb. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- drivers/codec/manifest.py | 1 + drivers/dht/manifest.py | 1 + drivers/display/manifest.py | 11 ++++++++ drivers/hts221/manifest.py | 1 + drivers/lps22h/manifest.py | 1 + drivers/lsm6dsox/manifest.py | 1 + drivers/lsm9ds1/manifest.py | 1 + drivers/neopixel/manifest.py | 6 +---- drivers/nrf24l01/manifest.py | 1 + drivers/onewire/manifest.py | 6 +++++ drivers/sdcard/manifest.py | 1 + extmod/uasyncio/manifest.py | 20 ++++++++------- extmod/webrepl/manifest.py | 3 ++- .../boards/LILYGO_TTGO_LORA32/manifest.py | 2 +- ports/esp32/boards/LOLIN_S2_PICO/manifest.py | 2 +- ports/esp32/boards/UM_FEATHERS2/manifest.py | 2 +- ports/esp32/boards/manifest.py | 25 ++++++++++--------- ports/esp8266/boards/GENERIC/manifest.py | 14 +++++------ ports/esp8266/boards/GENERIC_512K/manifest.py | 13 ++++++---- ports/esp8266/boards/manifest.py | 13 +++++----- ports/mimxrt/boards/manifest.py | 10 +++----- .../arduino_nano_33_ble_sense/manifest.py | 6 ++--- ports/nrf/modules/manifest.py | 4 +-- .../boards/RA4M1_CLICKER/manifest.py | 2 +- ports/renesas-ra/boards/RA4M1_EK/manifest.py | 2 +- ports/renesas-ra/boards/manifest.py | 8 +++--- .../ARDUINO_NANO_RP2040_CONNECT/manifest.py | 20 +++++++-------- ports/rp2/boards/PICO_W/manifest.py | 8 +++--- ports/rp2/boards/manifest.py | 8 +++--- .../boards/ARDUINO_PORTENTA_H7/manifest.py | 10 ++------ .../GARATRONIC_PYBSTICK26_F411/manifest.py | 2 +- ports/stm32/boards/LEGO_HUB_NO6/manifest.py | 2 ++ ports/stm32/boards/LEGO_HUB_NO7/manifest.py | 7 ++---- ports/stm32/boards/PYBD_SF2/manifest.py | 2 +- ports/stm32/boards/manifest.py | 9 ++++--- ports/unix/variants/dev/manifest.py | 2 +- ports/unix/variants/manifest.py | 4 +-- ports/windows/variants/dev/manifest.py | 2 +- 38 files changed, 126 insertions(+), 107 deletions(-) create mode 100644 drivers/codec/manifest.py create mode 100644 drivers/dht/manifest.py create mode 100644 drivers/display/manifest.py create mode 100644 drivers/hts221/manifest.py create mode 100644 drivers/lps22h/manifest.py create mode 100644 drivers/lsm6dsox/manifest.py create mode 100644 drivers/lsm9ds1/manifest.py create mode 100644 drivers/nrf24l01/manifest.py create mode 100644 drivers/onewire/manifest.py create mode 100644 drivers/sdcard/manifest.py diff --git a/drivers/codec/manifest.py b/drivers/codec/manifest.py new file mode 100644 index 0000000000000..4ff5d9fc445e2 --- /dev/null +++ b/drivers/codec/manifest.py @@ -0,0 +1 @@ +module("wm8960.py", opt=3) diff --git a/drivers/dht/manifest.py b/drivers/dht/manifest.py new file mode 100644 index 0000000000000..72a4e0d24fcd5 --- /dev/null +++ b/drivers/dht/manifest.py @@ -0,0 +1 @@ +module("dht.py", opt=3) diff --git a/drivers/display/manifest.py b/drivers/display/manifest.py new file mode 100644 index 0000000000000..d1ddff3374aba --- /dev/null +++ b/drivers/display/manifest.py @@ -0,0 +1,11 @@ +# TODO: Split these into separate directories with their own manifests. +options.defaults(lcd160cr=False, ssd1306=False, test=True) + +if options.lcd160cr: + module("lcd160cr.py", opt=3) + + if options.test: + module("lcd160cr_test.py", opt=3) + +if options.ssd1306: + module("ssd1306.py", opt=3) diff --git a/drivers/hts221/manifest.py b/drivers/hts221/manifest.py new file mode 100644 index 0000000000000..5f1792665943a --- /dev/null +++ b/drivers/hts221/manifest.py @@ -0,0 +1 @@ +module("hts221.py", opt=3) diff --git a/drivers/lps22h/manifest.py b/drivers/lps22h/manifest.py new file mode 100644 index 0000000000000..d30108d93dce0 --- /dev/null +++ b/drivers/lps22h/manifest.py @@ -0,0 +1 @@ +module("lps22h.py", opt=3) diff --git a/drivers/lsm6dsox/manifest.py b/drivers/lsm6dsox/manifest.py new file mode 100644 index 0000000000000..28f4b3565e9f7 --- /dev/null +++ b/drivers/lsm6dsox/manifest.py @@ -0,0 +1 @@ +module("lsm6dsox.py", opt=3) diff --git a/drivers/lsm9ds1/manifest.py b/drivers/lsm9ds1/manifest.py new file mode 100644 index 0000000000000..6779362de7ee5 --- /dev/null +++ b/drivers/lsm9ds1/manifest.py @@ -0,0 +1 @@ +module("lsm9ds1.py", opt=3) diff --git a/drivers/neopixel/manifest.py b/drivers/neopixel/manifest.py index 27f610adc0ad7..561d19574af32 100644 --- a/drivers/neopixel/manifest.py +++ b/drivers/neopixel/manifest.py @@ -1,5 +1 @@ -freeze( - ".", - "neopixel.py", - opt=3, -) +module("neopixel.py", opt=3) diff --git a/drivers/nrf24l01/manifest.py b/drivers/nrf24l01/manifest.py new file mode 100644 index 0000000000000..babdb7a52a953 --- /dev/null +++ b/drivers/nrf24l01/manifest.py @@ -0,0 +1 @@ +module("nrf24l01.py", opt=3) diff --git a/drivers/onewire/manifest.py b/drivers/onewire/manifest.py new file mode 100644 index 0000000000000..f500a65d780b8 --- /dev/null +++ b/drivers/onewire/manifest.py @@ -0,0 +1,6 @@ +options.defaults(ds18x20=False) + +module("onewire.py", opt=3) + +if options.ds18x20: + module("ds18x20.py", opt=3) diff --git a/drivers/sdcard/manifest.py b/drivers/sdcard/manifest.py new file mode 100644 index 0000000000000..e584b97d9ca6f --- /dev/null +++ b/drivers/sdcard/manifest.py @@ -0,0 +1 @@ +module("sdcard.py", opt=3) diff --git a/extmod/uasyncio/manifest.py b/extmod/uasyncio/manifest.py index f5fa27bfcaa35..d425a467b3afa 100644 --- a/extmod/uasyncio/manifest.py +++ b/extmod/uasyncio/manifest.py @@ -1,13 +1,15 @@ -# This list of frozen files doesn't include task.py because that's provided by the C module. -freeze( - "..", +# This list of package files doesn't include task.py because that's provided +# by the C module. +package( + "uasyncio", ( - "uasyncio/__init__.py", - "uasyncio/core.py", - "uasyncio/event.py", - "uasyncio/funcs.py", - "uasyncio/lock.py", - "uasyncio/stream.py", + "__init__.py", + "core.py", + "event.py", + "funcs.py", + "lock.py", + "stream.py", ), + base_path="..", opt=3, ) diff --git a/extmod/webrepl/manifest.py b/extmod/webrepl/manifest.py index c504c7305c0fd..6d1a314219caa 100644 --- a/extmod/webrepl/manifest.py +++ b/extmod/webrepl/manifest.py @@ -1 +1,2 @@ -freeze(".", ("webrepl.py", "webrepl_setup.py")) +module("webrepl.py", opt=3) +module("webrepl_setup.py", opt=3) diff --git a/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py b/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py index 0709f8597c07c..6c491c8f66de8 100644 --- a/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py +++ b/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py @@ -1,4 +1,4 @@ include("$(PORT_DIR)/boards/manifest.py") freeze("modules") -freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") +include("$(MPY_DIR)/drivers/display", ssd1306=True) diff --git a/ports/esp32/boards/LOLIN_S2_PICO/manifest.py b/ports/esp32/boards/LOLIN_S2_PICO/manifest.py index 98d4247c607a9..efc37137b2cfd 100644 --- a/ports/esp32/boards/LOLIN_S2_PICO/manifest.py +++ b/ports/esp32/boards/LOLIN_S2_PICO/manifest.py @@ -1,4 +1,4 @@ include("$(PORT_DIR)/boards/manifest.py") freeze("./modules") -freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") +include("$(MPY_DIR)/drivers/display", ssd1306=True) diff --git a/ports/esp32/boards/UM_FEATHERS2/manifest.py b/ports/esp32/boards/UM_FEATHERS2/manifest.py index 82ad0c7e49888..3fda1dd8272cf 100644 --- a/ports/esp32/boards/UM_FEATHERS2/manifest.py +++ b/ports/esp32/boards/UM_FEATHERS2/manifest.py @@ -1,3 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") -freeze("$(PORT_DIR)/boards/UM_TINYPICO/modules", "dotstar.py") +module("dotstar.py", base_path="$(PORT_DIR)/boards/UM_TINYPICO/modules", opt=3) freeze("modules") diff --git a/ports/esp32/boards/manifest.py b/ports/esp32/boards/manifest.py index 23252442ea460..658d11d5855d7 100644 --- a/ports/esp32/boards/manifest.py +++ b/ports/esp32/boards/manifest.py @@ -1,14 +1,15 @@ freeze("$(PORT_DIR)/modules") -freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py")) -freeze("$(MPY_DIR)/extmod", "ntptime.py") -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -freeze("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") -include("$(MPY_DIR)/drivers/neopixel/manifest.py") +module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) +module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) +module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/drivers/neopixel") +include("$(MPY_DIR)/extmod/uasyncio") +include("$(MPY_DIR)/extmod/webrepl") -# Freeze some micropython-lib modules. -freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") -freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") -freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") -freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") +# Require some micropython-lib modules. +require("urequests") +require("upysh") +require("umqtt.simple") +require("umqtt.robust") diff --git a/ports/esp8266/boards/GENERIC/manifest.py b/ports/esp8266/boards/GENERIC/manifest.py index ef25040ef8a76..54b916546c343 100644 --- a/ports/esp8266/boards/GENERIC/manifest.py +++ b/ports/esp8266/boards/GENERIC/manifest.py @@ -2,18 +2,18 @@ include("$(PORT_DIR)/boards/manifest.py") # uasyncio -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") +include("$(MPY_DIR)/extmod/uasyncio") # drivers -freeze("$(MPY_DIR)/drivers/display", "ssd1306.py") +include("$(MPY_DIR)/drivers/display", ssd1306=True) # micropython-lib: file utilities -freeze("$(MPY_LIB_DIR)/micropython/upysh", "upysh.py") +require("upysh") # micropython-lib: requests -freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") -freeze("$(MPY_LIB_DIR)/micropython/urllib.urequest", "urllib/urequest.py") +require("urequests") +require("urllib.urequest") # micropython-lib: umqtt -freeze("$(MPY_LIB_DIR)/micropython/umqtt.simple", "umqtt/simple.py") -freeze("$(MPY_LIB_DIR)/micropython/umqtt.robust", "umqtt/robust.py") +require("umqtt.simple") +require("umqtt.robust") diff --git a/ports/esp8266/boards/GENERIC_512K/manifest.py b/ports/esp8266/boards/GENERIC_512K/manifest.py index e43d94843fdba..57970a6b4dc11 100644 --- a/ports/esp8266/boards/GENERIC_512K/manifest.py +++ b/ports/esp8266/boards/GENERIC_512K/manifest.py @@ -1,5 +1,8 @@ -freeze("$(BOARD_DIR)", "_boot.py", opt=3) -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -freeze("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") -include("$(MPY_DIR)/drivers/neopixel/manifest.py") +module("_boot.py", opt=3) +module("apa102.py", base_path="$(PORT_DIR)/modules", opt=3) +module("port_diag.py", base_path="$(PORT_DIR)/modules", opt=3) +module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/extmod/webrepl") +include("$(MPY_DIR)/drivers/neopixel") diff --git a/ports/esp8266/boards/manifest.py b/ports/esp8266/boards/manifest.py index c5809717e6927..abcee253ffe55 100644 --- a/ports/esp8266/boards/manifest.py +++ b/ports/esp8266/boards/manifest.py @@ -1,7 +1,8 @@ freeze("$(PORT_DIR)/modules") -freeze("$(MPY_DIR)/extmod", "ntptime.py") -freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py")) -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -freeze("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") -include("$(MPY_DIR)/drivers/neopixel/manifest.py") +module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) +module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) +module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/extmod/webrepl") +include("$(MPY_DIR)/drivers/neopixel") diff --git a/ports/mimxrt/boards/manifest.py b/ports/mimxrt/boards/manifest.py index 2e1a1d63d4e79..a273dfa37041a 100644 --- a/ports/mimxrt/boards/manifest.py +++ b/ports/mimxrt/boards/manifest.py @@ -1,8 +1,4 @@ freeze("$(PORT_DIR)/modules") -freeze("$(MPY_DIR)/drivers/onewire") -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") -try: - include("$(BOARD_DIR)/manifest.py") -except FileNotFoundError: - pass +include("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/extmod/uasyncio") diff --git a/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py b/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py index dbc8104dc24fc..2b0cc6c818fbe 100644 --- a/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py +++ b/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py @@ -1,4 +1,4 @@ include("$(PORT_DIR)/modules/manifest.py") -freeze("$(MPY_DIR)/drivers/hts221", "hts221.py") -freeze("$(MPY_DIR)/drivers/lps22h", "lps22h.py") -freeze("$(MPY_DIR)/drivers/lsm9ds1", "lsm9ds1.py") +include("$(MPY_DIR)/drivers/hts221") +include("$(MPY_DIR)/drivers/lps22h") +include("$(MPY_DIR)/drivers/lsm9ds1") diff --git a/ports/nrf/modules/manifest.py b/ports/nrf/modules/manifest.py index b27d4648b7ea2..6efaf62d79d69 100644 --- a/ports/nrf/modules/manifest.py +++ b/ports/nrf/modules/manifest.py @@ -1,2 +1,2 @@ -freeze("$(PORT_DIR)/modules/scripts", "_mkfs.py") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") +module("_mkfs.py", base_path="$(PORT_DIR)/modules/scripts", opt=3) +include("$(MPY_DIR)/extmod/uasyncio") diff --git a/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py b/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py index 4a387915d37e0..c25ae79887e28 100644 --- a/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py +++ b/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py @@ -1,2 +1,2 @@ # We do not want to include default frozen modules, -freeze("$(MPY_DIR)/drivers/sdcard", "sdcard.py") +include("$(MPY_DIR)/drivers/sdcard") diff --git a/ports/renesas-ra/boards/RA4M1_EK/manifest.py b/ports/renesas-ra/boards/RA4M1_EK/manifest.py index 4a387915d37e0..c25ae79887e28 100644 --- a/ports/renesas-ra/boards/RA4M1_EK/manifest.py +++ b/ports/renesas-ra/boards/RA4M1_EK/manifest.py @@ -1,2 +1,2 @@ # We do not want to include default frozen modules, -freeze("$(MPY_DIR)/drivers/sdcard", "sdcard.py") +include("$(MPY_DIR)/drivers/sdcard") diff --git a/ports/renesas-ra/boards/manifest.py b/ports/renesas-ra/boards/manifest.py index 836bf7ccc24dc..c66ec92015dea 100644 --- a/ports/renesas-ra/boards/manifest.py +++ b/ports/renesas-ra/boards/manifest.py @@ -1,4 +1,4 @@ -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -freeze("$(MPY_DIR)/drivers/onewire", "onewire.py") -freeze("$(MPY_DIR)/drivers/sdcard", "sdcard.py") +include("$(MPY_DIR)/extmod/uasyncio") +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/drivers/onewire", ds18x20=False) +include("$(MPY_DIR)/drivers/sdcard") diff --git a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py index 7a2f1c9e2738c..e139c7d60bed7 100644 --- a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py +++ b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py @@ -1,11 +1,11 @@ include("$(PORT_DIR)/boards/manifest.py") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") -freeze("$(MPY_DIR)/drivers/lsm6dsox/", "lsm6dsox.py") -include( - "$(MPY_LIB_DIR)/micropython/bluetooth/aioble/manifest.py", - client=True, - central=True, - l2cap=True, - security=True, -) -freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") + +# Networking +include("$(MPY_DIR)/extmod/webrepl") +require("urequests") + +# Drivers +include("$(MPY_DIR)/drivers/lsm6dsox") + +# Bluetooth +require("aioble", client=True, central=True, l2cap=True, security=True) diff --git a/ports/rp2/boards/PICO_W/manifest.py b/ports/rp2/boards/PICO_W/manifest.py index d2c76602affe8..66ff26b0c8569 100644 --- a/ports/rp2/boards/PICO_W/manifest.py +++ b/ports/rp2/boards/PICO_W/manifest.py @@ -1,7 +1,7 @@ include("../manifest.py") -freeze("$(MPY_DIR)/tools", "upip.py") -freeze("$(MPY_DIR)/tools", "upip_utarfile.py") -freeze("$(MPY_DIR)/extmod", "ntptime.py") +module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) +module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) +module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) -freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py") +require("urequests") diff --git a/ports/rp2/boards/manifest.py b/ports/rp2/boards/manifest.py index b0e5e315544f2..9afcba17e85d2 100644 --- a/ports/rp2/boards/manifest.py +++ b/ports/rp2/boards/manifest.py @@ -1,5 +1,5 @@ freeze("$(PORT_DIR)/modules") -freeze("$(MPY_DIR)/drivers/onewire") -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") -include("$(MPY_DIR)/drivers/neopixel/manifest.py") +include("$(MPY_DIR)/drivers/onewire") +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/extmod/uasyncio") +include("$(MPY_DIR)/drivers/neopixel") diff --git a/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py b/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py index cd721d0ac45d1..0ecdcd21534fe 100644 --- a/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py +++ b/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py @@ -1,9 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") -include( - "$(MPY_LIB_DIR)/micropython/bluetooth/aioble/manifest.py", - client=True, - central=True, - l2cap=True, - security=True, -) +include("$(MPY_DIR)/extmod/webrepl") +require("aioble", client=True, central=True, l2cap=True, security=True) diff --git a/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py b/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py index b09c7ab920308..69da2897ae06f 100644 --- a/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py +++ b/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py @@ -1,2 +1,2 @@ -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") +# Note: Freezes to display.ssd1306, so must use deprecated "freeze" function. freeze("$(MPY_DIR)/drivers/", ("display/ssd1306.py")) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/manifest.py b/ports/stm32/boards/LEGO_HUB_NO6/manifest.py index dc09d04c9f2bf..107001c38eda9 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/manifest.py +++ b/ports/stm32/boards/LEGO_HUB_NO6/manifest.py @@ -1,3 +1,5 @@ +# Shared manifest for LEGO_HUB_NO6 & LEGO_HUB_NO7. + include("$(PORT_DIR)/boards/manifest.py") # Modules for application firmware update. diff --git a/ports/stm32/boards/LEGO_HUB_NO7/manifest.py b/ports/stm32/boards/LEGO_HUB_NO7/manifest.py index d746381637d96..ee79864a982c7 100644 --- a/ports/stm32/boards/LEGO_HUB_NO7/manifest.py +++ b/ports/stm32/boards/LEGO_HUB_NO7/manifest.py @@ -1,5 +1,2 @@ -include("$(PORT_DIR)/boards/manifest.py") - -# Modules for application firmware update. -freeze("$(PORT_DIR)/mboot", "fwupdate.py", opt=3) -freeze("$(PORT_DIR)/boards/LEGO_HUB_NO6", ("spiflash.py", "appupdate.py"), opt=3) +# Use shared manifest. +include("$(PORT_DIR)/boards/LEGO_HUB_NO6") diff --git a/ports/stm32/boards/PYBD_SF2/manifest.py b/ports/stm32/boards/PYBD_SF2/manifest.py index 3819aa01b3dc6..e4736f693a989 100644 --- a/ports/stm32/boards/PYBD_SF2/manifest.py +++ b/ports/stm32/boards/PYBD_SF2/manifest.py @@ -1,2 +1,2 @@ include("$(PORT_DIR)/boards/manifest.py") -include("$(MPY_DIR)/extmod/webrepl/manifest.py") +include("$(MPY_DIR)/extmod/webrepl") diff --git a/ports/stm32/boards/manifest.py b/ports/stm32/boards/manifest.py index 81b85834101de..4bb224e2d866d 100644 --- a/ports/stm32/boards/manifest.py +++ b/ports/stm32/boards/manifest.py @@ -1,4 +1,5 @@ -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") -freeze("$(MPY_DIR)/drivers/dht", "dht.py") -freeze("$(MPY_DIR)/drivers/display", ("lcd160cr.py", "lcd160cr_test.py")) -freeze("$(MPY_DIR)/drivers/onewire", "onewire.py") +include("$(MPY_DIR)/extmod/uasyncio") + +include("$(MPY_DIR)/drivers/dht") +include("$(MPY_DIR)/drivers/display", lcd160cr=True, test=True) +include("$(MPY_DIR)/drivers/onewire", ds18x20=False) diff --git a/ports/unix/variants/dev/manifest.py b/ports/unix/variants/dev/manifest.py index 92a681116a687..dd521258e1ac1 100644 --- a/ports/unix/variants/dev/manifest.py +++ b/ports/unix/variants/dev/manifest.py @@ -1,3 +1,3 @@ include("$(PORT_DIR)/variants/manifest.py") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") +include("$(MPY_DIR)/extmod/uasyncio") diff --git a/ports/unix/variants/manifest.py b/ports/unix/variants/manifest.py index 7708e598dbcfb..bf7ce992ade0d 100644 --- a/ports/unix/variants/manifest.py +++ b/ports/unix/variants/manifest.py @@ -1,2 +1,2 @@ -freeze_as_mpy("$(MPY_DIR)/tools", "upip.py") -freeze_as_mpy("$(MPY_DIR)/tools", "upip_utarfile.py", opt=3) +module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) +module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) diff --git a/ports/windows/variants/dev/manifest.py b/ports/windows/variants/dev/manifest.py index 88a6937b4fb36..f65a3d35e9d15 100644 --- a/ports/windows/variants/dev/manifest.py +++ b/ports/windows/variants/dev/manifest.py @@ -1,2 +1,2 @@ include("$(PORT_DIR)/variants/manifest.py") -include("$(MPY_DIR)/extmod/uasyncio/manifest.py") +include("$(MPY_DIR)/extmod/uasyncio") From fb20dbe4d123c69a7770e70911e827cefef01aa3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 10:57:33 +1000 Subject: [PATCH 2568/3533] stm32/boards/LEGO_HUB_NO6: Update manifest to new format. This was added after 203dae41f and missed in the rebase. Signed-off-by: Jim Mussared --- ports/stm32/boards/LEGO_HUB_NO6/manifest.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/stm32/boards/LEGO_HUB_NO6/manifest.py b/ports/stm32/boards/LEGO_HUB_NO6/manifest.py index 107001c38eda9..1be4246e37882 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/manifest.py +++ b/ports/stm32/boards/LEGO_HUB_NO6/manifest.py @@ -3,5 +3,6 @@ include("$(PORT_DIR)/boards/manifest.py") # Modules for application firmware update. -freeze("$(PORT_DIR)/mboot", "fwupdate.py", opt=3) -freeze("$(BOARD_DIR)", ("spiflash.py", "appupdate.py"), opt=3) +module("fwupdate.py", base_path="$(PORT_DIR)/mboot", opt=3) +module("spiflash.py", opt=3) +module("appupdate.py", opt=3) From 655c29351a7c4ef74612edf6088036b07f39e6c7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Sep 2022 11:52:46 +1000 Subject: [PATCH 2569/3533] drivers/display: Don't include tests by default. The tests can be copied to the board if needed. Also update the docs to reflect this change. Signed-off-by: Damien George --- docs/pyboard/tutorial/lcd160cr_skin.rst | 4 ++-- docs/reference/mpremote.rst | 2 ++ drivers/display/manifest.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/pyboard/tutorial/lcd160cr_skin.rst b/docs/pyboard/tutorial/lcd160cr_skin.rst index fc9d63538261e..fa0debcb1ce3f 100644 --- a/docs/pyboard/tutorial/lcd160cr_skin.rst +++ b/docs/pyboard/tutorial/lcd160cr_skin.rst @@ -40,9 +40,9 @@ Testing the display There is a test program which you can use to test the features of the display, and which also serves as a basis to start creating your own code that uses the -LCD. This test program is included in recent versions of the pyboard firmware -and is also available on GitHub +LCD. This test program is available on GitHub `here `__. +Copy it to the board over USB mass storage, or by using `mpremote`. To run the test from the MicroPython prompt do:: diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index a48df9953f503..e3902f8e5d7e9 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -1,3 +1,5 @@ +.. _mpremote: + MicroPython remote control: mpremote ==================================== diff --git a/drivers/display/manifest.py b/drivers/display/manifest.py index d1ddff3374aba..16f93a7d4e99a 100644 --- a/drivers/display/manifest.py +++ b/drivers/display/manifest.py @@ -1,5 +1,5 @@ # TODO: Split these into separate directories with their own manifests. -options.defaults(lcd160cr=False, ssd1306=False, test=True) +options.defaults(lcd160cr=False, ssd1306=False, test=False) if options.lcd160cr: module("lcd160cr.py", opt=3) From 4e4c28bf27ff4a9e1a2ce5b6fffa641d9a332507 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 6 Sep 2022 11:53:36 +1000 Subject: [PATCH 2570/3533] stm32/boards: Only freeze LCD160CR driver in PYB board firmware. Although this driver and associated hardware can be used on any board, it makes to only freeze it for PYB and PYBD boards. It can be easily copied to any board if needed. Fixes issue #8056. Signed-off-by: Damien George --- ports/stm32/boards/PYBD_SF2/manifest.py | 1 + ports/stm32/boards/PYBLITEV10/mpconfigboard.mk | 3 +++ ports/stm32/boards/PYBV10/manifest.py | 2 ++ ports/stm32/boards/PYBV10/mpconfigboard.mk | 3 +++ ports/stm32/boards/PYBV11/mpconfigboard.mk | 3 +++ ports/stm32/boards/manifest.py | 1 - ports/stm32/boards/manifest_pyboard.py | 1 + 7 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 ports/stm32/boards/PYBV10/manifest.py create mode 100644 ports/stm32/boards/manifest_pyboard.py diff --git a/ports/stm32/boards/PYBD_SF2/manifest.py b/ports/stm32/boards/PYBD_SF2/manifest.py index e4736f693a989..a9e92848d58d5 100644 --- a/ports/stm32/boards/PYBD_SF2/manifest.py +++ b/ports/stm32/boards/PYBD_SF2/manifest.py @@ -1,2 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") +include("$(PORT_DIR)/boards/manifest_pyboard.py") include("$(MPY_DIR)/extmod/webrepl") diff --git a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk index 8aeaf0e40cfbd..74698664760f0 100644 --- a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk @@ -24,3 +24,6 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 endif + +# PYB-specific frozen modules +FROZEN_MANIFEST ?= boards/PYBV10/manifest.py diff --git a/ports/stm32/boards/PYBV10/manifest.py b/ports/stm32/boards/PYBV10/manifest.py new file mode 100644 index 0000000000000..ec0d4a26b7853 --- /dev/null +++ b/ports/stm32/boards/PYBV10/manifest.py @@ -0,0 +1,2 @@ +include("$(PORT_DIR)/boards/manifest.py") +include("$(PORT_DIR)/boards/manifest_pyboard.py") diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.mk b/ports/stm32/boards/PYBV10/mpconfigboard.mk index 97af22b5e19bd..af28782678031 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV10/mpconfigboard.mk @@ -34,3 +34,6 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 endif + +# PYB-specific frozen modules +FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.mk b/ports/stm32/boards/PYBV11/mpconfigboard.mk index 97af22b5e19bd..cf7884cfc67a9 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV11/mpconfigboard.mk @@ -34,3 +34,6 @@ endif ifeq ($(BOARD_VARIANT),"network") MICROPY_PY_NETWORK_WIZNET5K=5200 endif + +# PYB-specific frozen modules +FROZEN_MANIFEST ?= boards/PYBV10/manifest.py diff --git a/ports/stm32/boards/manifest.py b/ports/stm32/boards/manifest.py index 4bb224e2d866d..723caa3eb6d1a 100644 --- a/ports/stm32/boards/manifest.py +++ b/ports/stm32/boards/manifest.py @@ -1,5 +1,4 @@ include("$(MPY_DIR)/extmod/uasyncio") include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/display", lcd160cr=True, test=True) include("$(MPY_DIR)/drivers/onewire", ds18x20=False) diff --git a/ports/stm32/boards/manifest_pyboard.py b/ports/stm32/boards/manifest_pyboard.py new file mode 100644 index 0000000000000..e4fb0a8c4dd1a --- /dev/null +++ b/ports/stm32/boards/manifest_pyboard.py @@ -0,0 +1 @@ +include("$(MPY_DIR)/drivers/display", lcd160cr=True) From 8770cd2f4d24b1ad14c934b4161e42108fe98f84 Mon Sep 17 00:00:00 2001 From: yn386 Date: Sun, 28 Aug 2022 15:40:00 +0900 Subject: [PATCH 2571/3533] stm32/adc: Make ADCAll.read_channel reject invalid channels. pyb.ADC(channel) checks whether specified channel is valid or have ADC capability but pyb.ADCAll().read_channel() does not. This change adds checking whether specified channel is valid and throw ValueError if channel is invalid. This is same as pyb.ADC(). --- ports/stm32/adc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 9b7788e59e337..9d58cf2e7cf02 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -856,6 +856,9 @@ STATIC mp_obj_t adc_all_make_new(const mp_obj_type_t *type, size_t n_args, size_ STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) { pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in); uint32_t chan = adc_get_internal_channel(mp_obj_get_int(channel)); + if (!is_adcx_channel(chan)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("not a valid ADC Channel: %d"), chan); + } uint32_t data = adc_config_and_read_channel(&self->handle, chan); return mp_obj_new_int(data); } From da50827657d6bc3024dd6acea76cb315d1cbbae1 Mon Sep 17 00:00:00 2001 From: yn386 Date: Wed, 31 Aug 2022 19:13:00 +0900 Subject: [PATCH 2572/3533] stm32/pyb_i2c: Fix pyb.I2C to work with dma=True on F4 MCUs. Prior to this commit, excuting this code: i2c = I2C(1, I2C.CONTROLLER, dma=True) i2c.send(data, addr=i2c_addr) the call to i2c.send() does not return and the board needs a reset. This code works when dma=False. According to the specification, I2Cx_EV_IRQHandler should: - Write DR to address when Start condition generated. - Clear ADDR by reading SR2 after reading SR2 when address sent. These processes are included in HAL_I2C_EV_IRQHandler(), however the firmware size increses about 2KB if HAL_I2C_EV_IRQHandler is called. This commit adds above processes to i2c_ev_irq_handler, and increases firmware by less than 100 bytes. Fixes issue #2643. --- ports/stm32/pyb_i2c.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index 436b1c9bc2437..e9877422ca0b4 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -472,7 +472,17 @@ void i2c_ev_irq_handler(mp_uint_t i2c_id) { #if defined(STM32F4) - if (hi2c->Instance->SR1 & I2C_FLAG_BTF && hi2c->State == HAL_I2C_STATE_BUSY_TX) { + if (hi2c->Instance->SR1 & I2C_FLAG_SB) { + if (hi2c->State == HAL_I2C_STATE_BUSY_TX) { + hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress); + } else { + hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress); + } + } else if (hi2c->Instance->SR1 & I2C_FLAG_ADDR) { + __IO uint32_t tmp_sr2; + tmp_sr2 = hi2c->Instance->SR2; + UNUSED(tmp_sr2); + } else if (hi2c->Instance->SR1 & I2C_FLAG_BTF && hi2c->State == HAL_I2C_STATE_BUSY_TX) { if (hi2c->XferCount != 0U) { hi2c->Instance->DR = *hi2c->pBuffPtr++; hi2c->XferCount--; From 989b8c728b296969e6489908b8e94ed23625a1e8 Mon Sep 17 00:00:00 2001 From: yn386 Date: Thu, 1 Sep 2022 18:08:18 +0900 Subject: [PATCH 2573/3533] stm32/timer: Fix use of timer channel callback() method on L4 MCUs. Since L4 HAL version 1.17.0, HAL_TIM_IC_Start_IT() checks whether specified channel of timer is busy or not, which is the case if this function is called more than once without first calling HAL_TIM_IC_Stop_IT(). The fix in this commit is to call the stop function before calling start. The PWM and OC modes have the same issue with the same fix. Fixes issue #8732. --- ports/stm32/timer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 0cef60cb62c87..518a2e23b4730 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -1576,6 +1576,7 @@ STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback) switch (self->mode) { case CHANNEL_MODE_PWM_NORMAL: case CHANNEL_MODE_PWM_INVERTED: + HAL_TIM_PWM_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_PWM_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; case CHANNEL_MODE_OC_TIMING: @@ -1584,9 +1585,11 @@ STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback) case CHANNEL_MODE_OC_TOGGLE: case CHANNEL_MODE_OC_FORCED_ACTIVE: case CHANNEL_MODE_OC_FORCED_INACTIVE: + HAL_TIM_OC_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_OC_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; case CHANNEL_MODE_IC: + HAL_TIM_IC_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_IC_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; } From 719dbbf5639cdeff99bf629c45d66b18007e9958 Mon Sep 17 00:00:00 2001 From: yn386 Date: Fri, 2 Sep 2022 18:46:10 +0900 Subject: [PATCH 2574/3533] stm32/boards: Add alternate function list for STM32F446RE. Signed-off-by: Damien George --- .../boards/NUCLEO_F446RE/mpconfigboard.mk | 2 +- ports/stm32/boards/stm32f446_af.csv | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 ports/stm32/boards/stm32f446_af.csv diff --git a/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.mk index 64a80e992bcec..3a922acebfbd7 100644 --- a/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.mk +++ b/ports/stm32/boards/NUCLEO_F446RE/mpconfigboard.mk @@ -1,6 +1,6 @@ MCU_SERIES = f4 CMSIS_MCU = STM32F446xx -AF_FILE = boards/stm32f429_af.csv +AF_FILE = boards/stm32f446_af.csv LD_FILES = boards/stm32f411.ld boards/common_ifs.ld TEXT0_ADDR = 0x08000000 TEXT1_ADDR = 0x08020000 diff --git a/ports/stm32/boards/stm32f446_af.csv b/ports/stm32/boards/stm32f446_af.csv new file mode 100644 index 0000000000000..0838ed089d8f2 --- /dev/null +++ b/ports/stm32/boards/stm32f446_af.csv @@ -0,0 +1,116 @@ +Port,,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF14,AF15, +,,SYS,TIM1/2,TIM3/4/5,TIM8/9/10/11/CEC,I2C1/2/3/4/CEC,SPI1/2/3/4,SPI2/3/4/SAI1,SPI2/3/USART1/2/3/UART5/SPDIFRX,SAI/USART6/UART4/5/SPDIFRX,CAN1/2/TIM12/13/14/QUADSPI,SAI2/QUADSPI/OTG2_HS/OTG1_FS,OTG1_FS,FMC/SDIO/OTG2_FS,DCMI,,SYS, +PortA,PA0,,TIM2_CH1/TIM2_ETR,TIM5_CH1,TIM8_ETR,,,,USART2_CTS,UART4_TX,,,,,,,EVENTOUT,ADC123_IN0 +PortA,PA1,,TIM2_CH2,TIM5_CH2,,,,,USART2_RTS,UART4_RX,QUADSPI_BK1_IO3,SAI2_MCLK_B,,,,,EVENTOUT,ADC123_IN1 +PortA,PA2,,TIM2_CH3,TIM5_CH3,TIM9_CH1,,,,USART2_TX,SAI2_SCK_B,,,,,,,EVENTOUT,ADC123_IN2 +PortA,PA3,,TIM2_CH4,TIM5_CH4,TIM9_CH2,,,SAI1_FS_A,USART2_RX,,,OTG_HS_ULPI_D0,,,,,EVENTOUT,ADC123_IN3 +PortA,PA4,,,,,,SPI1_NSS/I2S1_WS,SPI3_NSS/I2S3_WS,USART2_CK,,,,,OTG_HS_SOF,DCMI_HSYNC,,EVENTOUT,ADC12_IN4 +PortA,PA5,,TIM2_CH1/TIM2_ETR,,TIM8_CH1N,,SPI1_SCK/I2S1_CK,,,,,OTG_HS_ULPI_CK,,,,,EVENTOUT,ADC12_IN5 +PortA,PA6,,TIM1_BKIN,TIM3_CH1,TIM8_BKIN,,SPI1_MISO,I2S2_MCK,,,TIM13_CH1,,,,DCMI_PIXCLK,,EVENTOUT,ADC12_IN6 +PortA,PA7,,TIM1_CH1N,TIM3_CH2,TIM8_CH1N,,SPI1_MOSI/I2S1_SD,,,,TIM14_CH1,,,FMC_SDNWE,,,EVENTOUT,ADC12_IN7 +PortA,PA8,MCO1,TIM1_CH1,,,I2C3_SCL,,,USART1_CK,,,OTG_FS_SOF,,,,,EVENTOUT, +PortA,PA9,,TIM1_CH2,,,I2C3_SMBA,SPI2_SCK/I2S2_CK,SAI1_SD_B,USART1_TX,,,,,,DCMI_D0,,EVENTOUT, +PortA,PA10,,TIM1_CH3,,,,,,USART1_RX,,,OTG_FS_ID,,,DCMI_D1,,EVENTOUT, +PortA,PA11,,TIM1_CH4,,,,,,USART1_CTS,,CAN1_RX,OTG_FS_DM,,,,,EVENTOUT, +PortA,PA12,,TIM1_ETR,,,,,,USART1_RTS,SAI2_FS_B,CAN1_TX,OTG_FS_DP,,,,,EVENTOUT, +PortA,PA13,JTMS/SWDIO,,,,,,,,,,,,,,,EVENTOUT, +PortA,PA14,JTCK/SWCLK,,,,,,,,,,,,,,,EVENTOUT, +PortA,PA15,JTDI,TIM2_CH1/TIM2_ETR,,,HDMI_CEC,SPI1_NSS/I2S1_WS,SPI3_NSS/I2S3_WS,,UART4_RTS,,,,,,,EVENTOUT, +PortB,PB0,,TIM1_CH2N,TIM3_CH3,TIM8_CH2N,,,,SPI3_MOSI/I2S3_SD,UART4_CTS,,OTG_HS_ULPI_D1,,SDIO_D1,,,EVENTOUT,ADC12_IN8 +PortB,PB1,,TIM1_CH3N,TIM3_CH4,TIM8_CH3N,,,,,,,OTG_HS_ULPI_D2,,SDIO_D2,,,EVENTOUT,ADC12_IN9 +PortB,PB2,,TIM2_CH4,,,,,SAI1_SD_A,SPI3_MOSI/I2S3_SD,,QUADSPI_CLK,OTG_HS_ULPI_D4,,SDIO_CK,,,EVENTOUT, +PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,I2C2_SDA,SPI1_SCK/I2S1_CK,SPI3_SCK/I2S3_CK,,,,,,,,,EVENTOUT, +PortB,PB4,NJTRST,,TIM3_CH1,,I2C3_SDA,SPI1_MISO,SPI3_MISO,SPI2_NSS/I2S2_WS,,,,,,,,EVENTOUT, +PortB,PB5,,,TIM3_CH2,,I2C1_SMBA,SPI1_MOSI/I2S1_SD,SPI3_MOSI/I2S3_SD,,,CAN2_RX,OTG_HS_ULPI_D7,,FMC_SDCKE1,DCMI_D10,,EVENTOUT, +PortB,PB6,,,TIM4_CH1,HDMI_CEC,I2C1_SCL,,,USART1_TX,,CAN2_TX,QUADSPI_BK1_NCS,,FMC_SDNE1,DCMI_D5,,EVENTOUT, +PortB,PB7,,,TIM4_CH2,,I2C1_SDA,,,USART1_RX,SPDIF_RX0,,,,FMC_NL,DCMI_VSYNC,,EVENTOUT, +PortB,PB8,,TIM2_CH1/TIM2_ETR,TIM4_CH3,TIM10_CH1,I2C1_SCL,,,,,CAN1_RX,,,SDIO_D4,DCMI_D6,,EVENTOUT, +PortB,PB9,,TIM2_CH2,TIM4_CH4,TIM11_CH1,I2C1_SDA,SPI2_NSS/I2S2_WS,SAI1_FS_B,,,CAN1_TX,,,SDIO_D5,DCMI_D7,,EVENTOUT, +PortB,PB10,,TIM2_CH3,,,I2C2_SCL,SPI2_SCK/I2S2_CK,SAI1_SCK_A,USART3_TX,,,OTG_HS_ULPI_D3,,,,,EVENTOUT, +PortB,PB11,,TIM2_CH4,,,I2C2_SDA,,,USART3_RX,SAI2_SD_A,,,,,,,EVENTOUT, +PortB,PB12,,TIM1_BKIN,,,I2C2_SMBA,SPI2_NSS/I2S2_WS,SAI1_SCK_B,USART3_CK,,CAN2_RX,OTG_HS_ULPI_D5,,OTG_HS_ID,,,EVENTOUT, +PortB,PB13,,TIM1_CH1N,,,,SPI2_SCK/I2S2_CK,,USART3_CTS,,CAN2_TX,OTG_HS_ULPI_D6,,,,,EVENTOUT, +PortB,PB14,,TIM1_CH2N,,TIM8_CH2N,,SPI2_MISO,,USART3_RTS,,TIM12_CH1,,,OTG_HS_DM,,,EVENTOUT, +PortB,PB15,RTC_REFIN,TIM1_CH3N,,TIM8_CH3N,,SPI2_MOSI/I2S2_SD,,,,TIM12_CH2,,,OTG_HS_DP,,,EVENTOUT, +PortC,PC0,,,,,,,SAI1_MCLK_B,,,,OTG_HS_ULPI_STP,,FMC_SDNWE,,,EVENTOUT,ADC123_IN10 +PortC,PC1,,,,,,SPI3_MOSI/I2S3_SD,SAI1_SD_A,SPI2_MOSI/I2S2_SD,,,,,,,,EVENTOUT,ADC123_IN11 +PortC,PC2,,,,,,SPI2_MISO,,,,,OTG_HS_ULPI_DIR,,FMC_SDNE0,,,EVENTOUT,ADC123_IN12 +PortC,PC3,,,,,,SPI2_MOSI/I2S2_SD,,,,,OTG_HS_ULPI_NXT,,FMC_SDCKE0,,,EVENTOUT,ADC123_IN13 +PortC,PC4,,,,,,I2S1_MCK,,,SPDIF_RX2,,,,FMC_SDNE0,,,EVENTOUT,ADC12_IN14 +PortC,PC5,,,,,,,,USART3_RX,SPDIF_RX3,,,,FMC_SDCKE0,,,EVENTOUT,ADC12_IN15 +PortC,PC6,,,TIM3_CH1,TIM8_CH1,FMPI2C1_SCL,I2S2_MCK,,,USART6_TX,,,,SDIO_D6,DCMI_D0,,EVENTOUT, +PortC,PC7,,,TIM3_CH2,TIM8_CH2,FMPI2C1_SDA,SPI2_SCK/I2S2_CK,I2S3_MCK,SPDIF_RX1,USART6_RX,,,,SDIO_D7,DCMI_D1,,EVENTOUT, +PortC,PC8,TRACED0,,TIM3_CH3,TIM8_CH3,,,,UART5_RTS,USART6_CK,,,,SDIO_D0,DCMI_D2,,EVENTOUT, +PortC,PC9,MCO2,,TIM3_CH4,TIM8_CH4,I2C3_SDA,I2S_CKIN,,UART5_CTS,,QUADSPI_BK1_IO0,,,SDIO_D1,DCMI_D3,,EVENTOUT, +PortC,PC10,,,,,,,SPI3_SCK/I2S3_CK,USART3_TX,UART4_TX,QUADSPI_BK1_IO1,,,SDIO_D2,DCMI_D8,,EVENTOUT, +PortC,PC11,,,,,,,SPI3_MISO,USART3_RX,UART4_RX,QUADSPI_BK2_NCS,,,SDIO_D3,DCMI_D4,,EVENTOUT, +PortC,PC12,,,,,I2C2_SDA,,SPI3_MOSI/I2S3_SD,USART3_CK,UART5_TX,,,,SDIO_CK,DCMI_D9,,EVENTOUT, +PortC,PC13,,,,,,,,,,,,,,,,EVENTOUT, +PortC,PC14,,,,,,,,,,,,,,,,EVENTOUT, +PortC,PC15,,,,,,,,,,,,,,,,EVENTOUT, +PortD,PD0,,,,,,SPI4_MISO,SPI3_MOSI/I2S3_SD,,,CAN1_RX,,,FMC_D2,,,EVENTOUT, +PortD,PD1,,,,,,,,SPI2_NSS/I2S2_WS,,CAN1_TX,,,FMC_D3,,,EVENTOUT, +PortD,PD2,,,TIM3_ETR,,,,,,UART5_RX,,,,SDIO_CMD,DCMI_D11,,EVENTOUT, +PortD,PD3,TRACED1,,,,,SPI2_SCK/I2S2_CK,,USART2_CTS,,QUADSPI_CLK,,,FMC_CLK,DCMI_D5,,EVENTOUT, +PortD,PD4,,,,,,,,USART2_RTS,,,,,FMC_NOE,,,EVENTOUT, +PortD,PD5,,,,,,,,USART2_TX,,,,,FMC_NWE,,,EVENTOUT, +PortD,PD6,,,,,,SPI3_MOSI/I2S3_SD,SAI1_SD_A,USART2_RX,,,,,FMC_NWAIT,DCMI_D10,,EVENTOUT, +PortD,PD7,,,,,,,,USART2_CK,SPDIF_RX0,,,,FMC_NE1,,,EVENTOUT, +PortD,PD8,,,,,,,,USART3_TX,SPDIF_RX1,,,,FMC_D13,,,EVENTOUT, +PortD,PD9,,,,,,,,USART3_RX,,,,,FMC_D14,,,EVENTOUT, +PortD,PD10,,,,,,,,USART3_CK,,,,,FMC_D15,,,EVENTOUT, +PortD,PD11,,,,,FMPI2C1_SMBA,,,USART3_CTS,,QUADSPI_BK1_IO0,SAI2_SD_A,,FMC_A16,,,EVENTOUT, +PortD,PD12,,,TIM4_CH1,,FMPI2C1_SCL,,,USART3_RTS,,QUADSPI_BK1_IO1,SAI2_FS_A,,FMC_A17,,,EVENTOUT, +PortD,PD13,,,TIM4_CH2,,FMPI2C1_SDA,,,,,QUADSPI_BK1_IO3,SAI2_SCK_A,,FMC_A18,,,EVENTOUT, +PortD,PD14,,,TIM4_CH3,,FMPI2C1_SCL,,,,SAI2_SCK_A,,,,FMC_D0,,,EVENTOUT, +PortD,PD15,,,TIM4_CH4,,FMPI2C1_SDA,,,,,,,,FMC_D1,,,EVENTOUT, +PortE,PE0,,,TIM4_ETR,,,,,,,,SAI2_MCLK_A,,FMC_NBL0,DCMI_D2,,EVENTOUT, +PortE,PE1,,,,,,,,,,,,,FMC_NBL1,DCMI_D3,,EVENTOUT, +PortE,PE2,TRACECLK,,,,,SPI4_SCK,SAI1_MCLK_A,,,QUADSPI_BK1_IO2,,,FMC_A23,,,EVENTOUT, +PortE,PE3,TRACED0,,,,,,SAI1_SD_B,,,,,,FMC_A19,,,EVENTOUT, +PortE,PE4,TRACED1,,,,,SPI4_NSS,SAI1_FS_A,,,,,,FMC_A20,DCMI_D4,,EVENTOUT, +PortE,PE5,TRACED2,,,TIM9_CH1,,SPI4_MISO,SAI1_SCK_A,,,,,,FMC_A21,DCMI_D6,,EVENTOUT, +PortE,PE6,TRACED3,,,TIM9_CH2,,SPI4_MOSI,SAI1_SD_A,,,,,,FMC_A22,DCMI_D7,,EVENTOUT, +PortE,PE7,,TIM1_ETR,,,,,,,UART5_RX,,QUADSPI_BK2_IO0,,FMC_D4,,,EVENTOUT, +PortE,PE8,,TIM1_CH1N,,,,,,,UART5_TX,,QUADSPI_BK2_IO1,,FMC_D5,,,EVENTOUT, +PortE,PE9,,TIM1_CH1,,,,,,,,,QUADSPI_BK2_IO2,,FMC_D6,,,EVENTOUT, +PortE,PE10,,TIM1_CH2N,,,,,,,,,QUADSPI_BK2_IO3,,FMC_D7,,,EVENTOUT, +PortE,PE11,,TIM1_CH2,,,,SPI4_NSS,,,,,SAI2_SD_B,,FMC_D8,,,EVENTOUT, +PortE,PE12,,TIM1_CH3N,,,,SPI4_SCK,,,,,SAI2_SCK_B,,FMC_D9,,,EVENTOUT, +PortE,PE13,,TIM1_CH3,,,,SPI4_MISO,,,,,SAI2_FS_B,,FMC_D10,,,EVENTOUT, +PortE,PE14,,TIM1_CH4,,,,SPI4_MOSI,,,,,SAI2_MCLK_B,,FMC_D11,,,EVENTOUT, +PortE,PE15,,TIM1_BKIN,,,,,,,,,,,FMC_D12,,,EVENTOUT, +PortF,PF0,,,,,I2C2_SDA,,,,,,,,FMC_A0,,,EVENTOUT, +PortF,PF1,,,,,I2C2_SCL,,,,,,,,FMC_A1,,,EVENTOUT, +PortF,PF2,,,,,I2C2_SMBA,,,,,,,,FMC_A2,,,EVENTOUT, +PortF,PF3,,,,,,,,,,,,,FMC_A3,,,EVENTOUT,ADC3_IN9 +PortF,PF4,,,,,,,,,,,,,FMC_A4,,,EVENTOUT,ADC3_IN14 +PortF,PF5,,,,,,,,,,,,,FMC_A5,,,EVENTOUT,ADC3_IN15 +PortF,PF6,,,,TIM10_CH1,,,SAI1_SD_B,,,QUADSPI_BK1_IO3,,,,,,EVENTOUT,ADC3_IN4 +PortF,PF7,,,,TIM11_CH1,,,SAI1_MCLK_B,,,QUADSPI_BK1_IO2,,,,,,EVENTOUT,ADC3_IN5 +PortF,PF8,,,,,,,SAI1_SCK_B,,,TIM13_CH1,QUADSPI_BK1_IO0,,,,,EVENTOUT,ADC3_IN6 +PortF,PF9,,,,,,,SAI1_FS_B,,,TIM14_CH1,QUADSPI_BK1_IO1,,,,,EVENTOUT,ADC3_IN7 +PortF,PF10,,,,,,,,,,,,,,DCMI_D11,,EVENTOUT,ADC3_IN8 +PortF,PF11,,,,,,,,,,,SAI2_SD_B,,FMC_SDNRAS,DCMI_D12,,EVENTOUT, +PortF,PF12,,,,,,,,,,,,,FMC_A6,,,EVENTOUT, +PortF,PF13,,,,,FMPI2C1_SMBA,,,,,,,,FMC_A7,,,EVENTOUT, +PortF,PF14,,,,,FMPI2C1_SCL,,,,,,,,FMC_A8,,,EVENTOUT, +PortF,PF15,,,,,FMPI2C1_SDA,,,,,,,,FMC_A9,,,EVENTOUT, +PortG,PG0,,,,,,,,,,,,,FMC_A10,,,EVENTOUT, +PortG,PG1,,,,,,,,,,,,,FMC_A11,,,EVENTOUT, +PortG,PG2,,,,,,,,,,,,,FMC_A12,,,EVENTOUT, +PortG,PG3,,,,,,,,,,,,,FMC_A13,,,EVENTOUT, +PortG,PG4,,,,,,,,,,,,,FMC_A14/FMC_BA0,,,EVENTOUT, +PortG,PG5,,,,,,,,,,,,,FMC_A15/FMC_BA1,,,EVENTOUT, +PortG,PG6,,,,,,,,,,,QUADSPI_BK1_NCS,,,DCMI_D12,,EVENTOUT, +PortG,PG7,,,,,,,,,USART6_CK,,,,FMC_INT,DCMI_D13,,EVENTOUT, +PortG,PG8,,,,,,,,SPDIFRX_IN2,USART6_RTS,,,,FMC_SDCLK,,,EVENTOUT, +PortG,PG9,,,,,,,,SPDIFRX_IN3,USART6_RX,QUADSPI_BK2_IO2,SAI2_FS_B,,FMC_NE2/FMC_NCE3,DCMI_VSYNC,,EVENTOUT, +PortG,PG10,,,,,,,,,,,SAI2_SD_B,,FMC_NE3,DCMI_D2,,EVENTOUT, +PortG,PG11,,,,,,,SPI4_SCK,SPDIFRX_IN0,,,,,,DCMI_D3,,EVENTOUT, +PortG,PG12,,,,,,,SPI4_MISO,SPDIFRX_IN1,USART6_RTS,,,,FMC_NE4,,,EVENTOUT, +PortG,PG13,TRACED2,,,,,,SPI4_MOSI,,USART6_CTS,,,,FMC_A24,,,EVENTOUT, +PortG,PG14,TRACED3,,,,,,SPI4_NSS,,USART6_TX,QUADSPI_BK2_IO3,,,FMC_A25,,,EVENTOUT, +PortG,PG15,,,,,,,,,USART6_CTS,,,,FMC_SDNCAS,DCMI_D13,,EVENTOUT, +PortH,PH0,,,,,,,,,,,,,,,,EVENTOUT, +PortH,PH1,,,,,,,,,,,,,,,,EVENTOUT, From e90b85cc98a24003f2d673bab2c255ab3dce66e7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 5 Sep 2022 07:58:04 -0500 Subject: [PATCH 2575/3533] extmod/modure: Convert byte offsets to unicode indices when necessary. And add a test. Fixes issue #9202. Signed-off-by: Jeff Epler --- extmod/modure.c | 16 ++++++++++++++++ tests/unicode/unicode_ure.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/unicode/unicode_ure.py diff --git a/extmod/modure.c b/extmod/modure.c index 799fef13b1bdb..a674d664999a8 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -33,6 +33,10 @@ #include "py/objstr.h" #include "py/stackctrl.h" +#if MICROPY_PY_BUILTINS_STR_UNICODE +#include "py/unicode.h" +#endif + #if MICROPY_PY_URE #define re1_5_stack_chk() MP_STACK_CHECK() @@ -121,6 +125,18 @@ STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span e = self->caps[no * 2 + 1] - begin; } + #if MICROPY_PY_BUILTINS_STR_UNICODE + if (mp_obj_get_type(self->str) == &mp_type_str) { + const byte *begin = (const byte *)mp_obj_str_get_str(self->str); + if (s != -1) { + s = utf8_ptr_to_index(begin, begin + s); + } + if (e != -1) { + e = utf8_ptr_to_index(begin, begin + e); + } + } + #endif + span[0] = mp_obj_new_int(s); span[1] = mp_obj_new_int(e); } diff --git a/tests/unicode/unicode_ure.py b/tests/unicode/unicode_ure.py new file mode 100644 index 0000000000000..5a5dc600545f1 --- /dev/null +++ b/tests/unicode/unicode_ure.py @@ -0,0 +1,32 @@ +# test match.span() for unicode strings + +try: + import ure as re +except ImportError: + try: + import re + except ImportError: + print("SKIP") + raise SystemExit + +try: + m = re.match(".", "a") + m.span +except AttributeError: + print("SKIP") + raise SystemExit + + +def print_spans(match): + print("----") + try: + i = 0 + while True: + print(match.span(i), match.start(i), match.end(i)) + i += 1 + except IndexError: + pass + + +m = re.match(r"([0-9]*)(([a-z]*)([0-9]*))", "1234\u2764567") +print_spans(m) From aeff6911d750a2d203eeb1cacd74701de5313ee6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 8 Sep 2022 11:24:18 +1000 Subject: [PATCH 2576/3533] lib/micropython-lib: Update submodule to latest. This brings in the drivers and libraries that were previously in this repo. Signed-off-by: Jim Mussared --- lib/micropython-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/micropython-lib b/lib/micropython-lib index f3cfc52ab076f..58f8bec54d5b3 160000 --- a/lib/micropython-lib +++ b/lib/micropython-lib @@ -1 +1 @@ -Subproject commit f3cfc52ab076fc913dc6e266fb7370b418c8f542 +Subproject commit 58f8bec54d5b3b959247b73a6e8f28e8493bd30b From d84c6ef0e8dd363881d80b2d8fb03447cc349830 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 13:49:41 +1000 Subject: [PATCH 2577/3533] ports: Use micropython-lib version of drivers in manifests. --- .../esp32/boards/LILYGO_TTGO_LORA32/manifest.py | 3 +-- ports/esp32/boards/LOLIN_S2_PICO/manifest.py | 3 +-- ports/esp32/boards/manifest.py | 17 +++++++++-------- ports/esp8266/boards/GENERIC/manifest.py | 2 +- ports/esp8266/boards/GENERIC_512K/manifest.py | 11 ++++++----- ports/esp8266/boards/manifest.py | 11 ++++++----- ports/mimxrt/boards/manifest.py | 5 +++-- .../arduino_nano_33_ble_sense/manifest.py | 6 +++--- .../renesas-ra/boards/RA4M1_CLICKER/manifest.py | 2 +- ports/renesas-ra/boards/RA4M1_EK/manifest.py | 2 +- ports/renesas-ra/boards/manifest.py | 6 +++--- .../ARDUINO_NANO_RP2040_CONNECT/manifest.py | 4 ++-- ports/rp2/boards/PICO_W/manifest.py | 2 +- ports/rp2/boards/manifest.py | 7 ++++--- .../boards/ARDUINO_PORTENTA_H7/manifest.py | 2 +- .../GARATRONIC_PYBSTICK26_F411/manifest.py | 3 +-- ports/stm32/boards/PYBD_SF2/manifest.py | 2 +- ports/stm32/boards/manifest.py | 4 ++-- ports/stm32/boards/manifest_pyboard.py | 2 +- 19 files changed, 48 insertions(+), 46 deletions(-) diff --git a/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py b/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py index 6c491c8f66de8..e2cd6b3d01519 100644 --- a/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py +++ b/ports/esp32/boards/LILYGO_TTGO_LORA32/manifest.py @@ -1,4 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") freeze("modules") - -include("$(MPY_DIR)/drivers/display", ssd1306=True) +require("ssd1306") diff --git a/ports/esp32/boards/LOLIN_S2_PICO/manifest.py b/ports/esp32/boards/LOLIN_S2_PICO/manifest.py index efc37137b2cfd..9ac8ade8275bf 100644 --- a/ports/esp32/boards/LOLIN_S2_PICO/manifest.py +++ b/ports/esp32/boards/LOLIN_S2_PICO/manifest.py @@ -1,4 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") freeze("./modules") - -include("$(MPY_DIR)/drivers/display", ssd1306=True) +require("ssd1306") diff --git a/ports/esp32/boards/manifest.py b/ports/esp32/boards/manifest.py index 658d11d5855d7..fcc48d721805a 100644 --- a/ports/esp32/boards/manifest.py +++ b/ports/esp32/boards/manifest.py @@ -1,15 +1,16 @@ freeze("$(PORT_DIR)/modules") module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) -module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) -include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/drivers/neopixel") include("$(MPY_DIR)/extmod/uasyncio") -include("$(MPY_DIR)/extmod/webrepl") # Require some micropython-lib modules. -require("urequests") -require("upysh") -require("umqtt.simple") +require("dht") +require("ds18x20") +require("neopixel") +require("ntptime") +require("onewire") require("umqtt.robust") +require("umqtt.simple") +require("upysh") +require("urequests") +require("webrepl") diff --git a/ports/esp8266/boards/GENERIC/manifest.py b/ports/esp8266/boards/GENERIC/manifest.py index 54b916546c343..ef708846241fa 100644 --- a/ports/esp8266/boards/GENERIC/manifest.py +++ b/ports/esp8266/boards/GENERIC/manifest.py @@ -5,7 +5,7 @@ include("$(MPY_DIR)/extmod/uasyncio") # drivers -include("$(MPY_DIR)/drivers/display", ssd1306=True) +require("ssd1306") # micropython-lib: file utilities require("upysh") diff --git a/ports/esp8266/boards/GENERIC_512K/manifest.py b/ports/esp8266/boards/GENERIC_512K/manifest.py index 57970a6b4dc11..15f6cffc3f94a 100644 --- a/ports/esp8266/boards/GENERIC_512K/manifest.py +++ b/ports/esp8266/boards/GENERIC_512K/manifest.py @@ -1,8 +1,9 @@ module("_boot.py", opt=3) module("apa102.py", base_path="$(PORT_DIR)/modules", opt=3) module("port_diag.py", base_path="$(PORT_DIR)/modules", opt=3) -module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) -include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/extmod/webrepl") -include("$(MPY_DIR)/drivers/neopixel") +require("ntptime") +require("dht") +require("onewire") +require("ds18x20") +require("webrepl") +require("neopixel") diff --git a/ports/esp8266/boards/manifest.py b/ports/esp8266/boards/manifest.py index abcee253ffe55..e7defd0bb70ff 100644 --- a/ports/esp8266/boards/manifest.py +++ b/ports/esp8266/boards/manifest.py @@ -1,8 +1,9 @@ freeze("$(PORT_DIR)/modules") module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) -module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) -include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/extmod/webrepl") -include("$(MPY_DIR)/drivers/neopixel") +require("ntptime") +require("dht") +require("onewire") +require("ds18x20") +require("neopixel") +require("webrepl") diff --git a/ports/mimxrt/boards/manifest.py b/ports/mimxrt/boards/manifest.py index a273dfa37041a..e4e5a236a3578 100644 --- a/ports/mimxrt/boards/manifest.py +++ b/ports/mimxrt/boards/manifest.py @@ -1,4 +1,5 @@ freeze("$(PORT_DIR)/modules") -include("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/drivers/dht") include("$(MPY_DIR)/extmod/uasyncio") +require("onewire") +require("ds18x20") +require("dht") diff --git a/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py b/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py index 2b0cc6c818fbe..8396e0249cbc1 100644 --- a/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py +++ b/ports/nrf/boards/arduino_nano_33_ble_sense/manifest.py @@ -1,4 +1,4 @@ include("$(PORT_DIR)/modules/manifest.py") -include("$(MPY_DIR)/drivers/hts221") -include("$(MPY_DIR)/drivers/lps22h") -include("$(MPY_DIR)/drivers/lsm9ds1") +require("hts221") +require("lps22h") +require("lsm9ds1") diff --git a/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py b/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py index c25ae79887e28..99b9c94051edb 100644 --- a/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py +++ b/ports/renesas-ra/boards/RA4M1_CLICKER/manifest.py @@ -1,2 +1,2 @@ # We do not want to include default frozen modules, -include("$(MPY_DIR)/drivers/sdcard") +require("sdcard") diff --git a/ports/renesas-ra/boards/RA4M1_EK/manifest.py b/ports/renesas-ra/boards/RA4M1_EK/manifest.py index c25ae79887e28..99b9c94051edb 100644 --- a/ports/renesas-ra/boards/RA4M1_EK/manifest.py +++ b/ports/renesas-ra/boards/RA4M1_EK/manifest.py @@ -1,2 +1,2 @@ # We do not want to include default frozen modules, -include("$(MPY_DIR)/drivers/sdcard") +require("sdcard") diff --git a/ports/renesas-ra/boards/manifest.py b/ports/renesas-ra/boards/manifest.py index c66ec92015dea..01699c3d6e6ad 100644 --- a/ports/renesas-ra/boards/manifest.py +++ b/ports/renesas-ra/boards/manifest.py @@ -1,4 +1,4 @@ include("$(MPY_DIR)/extmod/uasyncio") -include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/onewire", ds18x20=False) -include("$(MPY_DIR)/drivers/sdcard") +require("dht") +require("onewire") +require("sdcard") diff --git a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py index e139c7d60bed7..dbd45fa408e63 100644 --- a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py +++ b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/manifest.py @@ -1,11 +1,11 @@ include("$(PORT_DIR)/boards/manifest.py") # Networking -include("$(MPY_DIR)/extmod/webrepl") +require("webrepl") require("urequests") # Drivers -include("$(MPY_DIR)/drivers/lsm6dsox") +require("lsm6dsox") # Bluetooth require("aioble", client=True, central=True, l2cap=True, security=True) diff --git a/ports/rp2/boards/PICO_W/manifest.py b/ports/rp2/boards/PICO_W/manifest.py index 66ff26b0c8569..4d9eb87f20f3c 100644 --- a/ports/rp2/boards/PICO_W/manifest.py +++ b/ports/rp2/boards/PICO_W/manifest.py @@ -2,6 +2,6 @@ module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) -module("ntptime.py", base_path="$(MPY_DIR)/extmod", opt=3) +require("ntptime") require("urequests") diff --git a/ports/rp2/boards/manifest.py b/ports/rp2/boards/manifest.py index 9afcba17e85d2..e62d02f30857c 100644 --- a/ports/rp2/boards/manifest.py +++ b/ports/rp2/boards/manifest.py @@ -1,5 +1,6 @@ freeze("$(PORT_DIR)/modules") -include("$(MPY_DIR)/drivers/onewire") -include("$(MPY_DIR)/drivers/dht") include("$(MPY_DIR)/extmod/uasyncio") -include("$(MPY_DIR)/drivers/neopixel") +require("onewire") +require("ds18x20") +require("dht") +require("neopixel") diff --git a/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py b/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py index 0ecdcd21534fe..4fa92b7205327 100644 --- a/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py +++ b/ports/stm32/boards/ARDUINO_PORTENTA_H7/manifest.py @@ -1,3 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") -include("$(MPY_DIR)/extmod/webrepl") +require("webrepl") require("aioble", client=True, central=True, l2cap=True, security=True) diff --git a/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py b/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py index 69da2897ae06f..2600fc506241e 100644 --- a/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py +++ b/ports/stm32/boards/GARATRONIC_PYBSTICK26_F411/manifest.py @@ -1,2 +1 @@ -# Note: Freezes to display.ssd1306, so must use deprecated "freeze" function. -freeze("$(MPY_DIR)/drivers/", ("display/ssd1306.py")) +require("ssd1306") diff --git a/ports/stm32/boards/PYBD_SF2/manifest.py b/ports/stm32/boards/PYBD_SF2/manifest.py index a9e92848d58d5..0ee11baf4b8e3 100644 --- a/ports/stm32/boards/PYBD_SF2/manifest.py +++ b/ports/stm32/boards/PYBD_SF2/manifest.py @@ -1,3 +1,3 @@ include("$(PORT_DIR)/boards/manifest.py") include("$(PORT_DIR)/boards/manifest_pyboard.py") -include("$(MPY_DIR)/extmod/webrepl") +require("webrepl") diff --git a/ports/stm32/boards/manifest.py b/ports/stm32/boards/manifest.py index 723caa3eb6d1a..e6b77f8ab335d 100644 --- a/ports/stm32/boards/manifest.py +++ b/ports/stm32/boards/manifest.py @@ -1,4 +1,4 @@ include("$(MPY_DIR)/extmod/uasyncio") -include("$(MPY_DIR)/drivers/dht") -include("$(MPY_DIR)/drivers/onewire", ds18x20=False) +require("dht") +require("onewire") diff --git a/ports/stm32/boards/manifest_pyboard.py b/ports/stm32/boards/manifest_pyboard.py index e4fb0a8c4dd1a..789365a433070 100644 --- a/ports/stm32/boards/manifest_pyboard.py +++ b/ports/stm32/boards/manifest_pyboard.py @@ -1 +1 @@ -include("$(MPY_DIR)/drivers/display", lcd160cr=True) +require("lcd160cr") From 24678fe452e9c0d0b96575424f81fb4a5f5f4302 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 13:49:10 +1000 Subject: [PATCH 2578/3533] drivers: Remove drivers that are now in micropython-lib. Signed-off-by: Jim Mussared --- drivers/README.md | 2 +- drivers/codec/manifest.py | 1 - drivers/codec/wm8960.py | 753 ----------------------------- drivers/dht/dht.py | 46 -- drivers/dht/manifest.py | 1 - drivers/display/lcd160cr.py | 482 ------------------ drivers/display/lcd160cr_test.py | 187 ------- drivers/display/manifest.py | 11 - drivers/display/ssd1306.py | 163 ------- drivers/hts221/hts221.py | 91 ---- drivers/hts221/manifest.py | 1 - drivers/lps22h/lps22h.py | 109 ----- drivers/lps22h/manifest.py | 1 - drivers/lsm6dsox/lsm6dsox.py | 271 ----------- drivers/lsm6dsox/lsm6dsox_basic.py | 15 - drivers/lsm6dsox/lsm6dsox_mlc.py | 48 -- drivers/lsm6dsox/manifest.py | 1 - drivers/lsm9ds1/lsm9ds1.py | 189 -------- drivers/lsm9ds1/manifest.py | 1 - drivers/neopixel/manifest.py | 1 - drivers/neopixel/neopixel.py | 50 -- drivers/nrf24l01/manifest.py | 1 - drivers/nrf24l01/nrf24l01.py | 252 ---------- drivers/nrf24l01/nrf24l01test.py | 150 ------ drivers/onewire/ds18x20.py | 52 -- drivers/onewire/manifest.py | 6 - drivers/onewire/onewire.py | 92 ---- drivers/sdcard/manifest.py | 1 - drivers/sdcard/sdcard.py | 299 ------------ drivers/sdcard/sdtest.py | 61 --- extmod/ntptime.py | 48 -- extmod/webrepl/manifest.py | 2 - extmod/webrepl/webrepl.py | 177 ------- extmod/webrepl/webrepl_setup.py | 107 ---- 34 files changed, 1 insertion(+), 3671 deletions(-) delete mode 100644 drivers/codec/manifest.py delete mode 100644 drivers/codec/wm8960.py delete mode 100644 drivers/dht/dht.py delete mode 100644 drivers/dht/manifest.py delete mode 100644 drivers/display/lcd160cr.py delete mode 100644 drivers/display/lcd160cr_test.py delete mode 100644 drivers/display/manifest.py delete mode 100644 drivers/display/ssd1306.py delete mode 100644 drivers/hts221/hts221.py delete mode 100644 drivers/hts221/manifest.py delete mode 100644 drivers/lps22h/lps22h.py delete mode 100644 drivers/lps22h/manifest.py delete mode 100644 drivers/lsm6dsox/lsm6dsox.py delete mode 100644 drivers/lsm6dsox/lsm6dsox_basic.py delete mode 100644 drivers/lsm6dsox/lsm6dsox_mlc.py delete mode 100644 drivers/lsm6dsox/manifest.py delete mode 100644 drivers/lsm9ds1/lsm9ds1.py delete mode 100644 drivers/lsm9ds1/manifest.py delete mode 100644 drivers/neopixel/manifest.py delete mode 100644 drivers/neopixel/neopixel.py delete mode 100644 drivers/nrf24l01/manifest.py delete mode 100644 drivers/nrf24l01/nrf24l01.py delete mode 100644 drivers/nrf24l01/nrf24l01test.py delete mode 100644 drivers/onewire/ds18x20.py delete mode 100644 drivers/onewire/manifest.py delete mode 100644 drivers/onewire/onewire.py delete mode 100644 drivers/sdcard/manifest.py delete mode 100644 drivers/sdcard/sdcard.py delete mode 100644 drivers/sdcard/sdtest.py delete mode 100644 extmod/ntptime.py delete mode 100644 extmod/webrepl/manifest.py delete mode 100644 extmod/webrepl/webrepl.py delete mode 100644 extmod/webrepl/webrepl_setup.py diff --git a/drivers/README.md b/drivers/README.md index 854acc50b62c0..e4fac9fe8d43b 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -1,2 +1,2 @@ -This directory contains drivers for specific hardware. The drivers are +This directory contains C drivers for specific hardware. The drivers are intended to work across multiple ports. diff --git a/drivers/codec/manifest.py b/drivers/codec/manifest.py deleted file mode 100644 index 4ff5d9fc445e2..0000000000000 --- a/drivers/codec/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("wm8960.py", opt=3) diff --git a/drivers/codec/wm8960.py b/drivers/codec/wm8960.py deleted file mode 100644 index ad1cb1cbf872b..0000000000000 --- a/drivers/codec/wm8960.py +++ /dev/null @@ -1,753 +0,0 @@ -# -# Driver class for the WM8960 Codec to be used e.g. with MIMXRT_1xxx Boards. -# Derived from the NXP SDK drivers. -# -# Copyright (c) 2015, Freescale Semiconductor, Inc., (C-Code) -# Copyright 2016-2021 NXP, (C-Code) -# All rights reserved. -# -# Translated to MicroPython by Robert Hammelrath, 2022 -# -# SPDX-License-Identifier: BSD-3-Clause -# - -import array -from micropython import const - -# Define the register addresses of WM8960. -_LINVOL = const(0x0) -_RINVOL = const(0x1) -_LOUT1 = const(0x2) -_ROUT1 = const(0x3) -_CLOCK1 = const(0x4) -_DACCTL1 = const(0x5) -_DACCTL2 = const(0x6) -_IFACE1 = const(0x7) -_CLOCK2 = const(0x8) -_IFACE2 = const(0x9) -_LDAC = const(0xA) -_RDAC = const(0xB) -_RESET = const(0xF) -_3D = const(0x10) -_ALC1 = const(0x11) -_ALC2 = const(0x12) -_ALC3 = const(0x13) -_NOISEG = const(0x14) -_LADC = const(0x15) -_RADC = const(0x16) -_ADDCTL1 = const(0x17) -# Register _ADDCTL2 = const(0x18) -_POWER1 = const(0x19) -_POWER2 = const(0x1A) -_ADDCTL3 = const(0x1B) -# Register _APOP1 = const(0x1C) -# Register _APOP2 = const(0x1D) -_LINPATH = const(0x20) -_RINPATH = const(0x21) -_LOUTMIX = const(0x22) -_ROUTMIX = const(0x25) -_MONOMIX1 = const(0x26) -_MONOMIX2 = const(0x27) -_LOUT2 = const(0x28) -_ROUT2 = const(0x29) -_MONO = const(0x2A) -_INBMIX1 = const(0x2B) -_INBMIX2 = const(0x2C) -_BYPASS1 = const(0x2D) -_BYPASS2 = const(0x2E) -_POWER3 = const(0x2F) -_ADDCTL4 = const(0x30) -_CLASSD1 = const(0x31) -# Register _CLASSD3 = const(0x33) -_PLL1 = const(0x34) -_PLL2 = const(0x35) -_PLL3 = const(0x36) -_PLL4 = const(0x37) - -# WM8960 PLLN range */ -_PLL_N_MIN_VALUE = const(6) -_PLL_N_MAX_VALUE = const(12) - -# WM8960 CLOCK2 bits -_CLOCK2_BCLK_DIV_MASK = const(0x0F) -_CLOCK2_DCLK_DIV_MASK = const(0x1C0) -_CLOCK2_DCLK_DIV_SHIFT = const(0x06) - -# Register _IFACE1 -_IFACE1_FORMAT_MASK = const(0x03) -_IFACE1_WL_MASK = const(0x0C) -_IFACE1_WL_SHIFT = const(0x02) -_IFACE1_LRP_MASK = const(0x10) -_IFACE1_MS_MASK = const(0x40) -_IFACE1_DLRSWAP_MASK = const(0x20) -_IFACE1_ALRSWAP_MASK = const(0x100) - -# Register _POWER1 -_POWER1_VREF_MASK = const(0x40) -_POWER1_VREF_SHIFT = const(0x06) -_POWER1_AINL_MASK = const(0x20) -_POWER1_AINR_MASK = const(0x10) -_POWER1_ADCL_MASK = const(0x08) -_POWER1_ADCR_MASK = const(0x0) -_POWER1_MICB_MASK = const(0x02) -_POWER1_MICB_SHIFT = const(0x01) - -# Register _POWER2 -_POWER2_DACL_MASK = const(0x100) -_POWER2_DACR_MASK = const(0x80) -_POWER2_LOUT1_MASK = const(0x40) -_POWER2_ROUT1_MASK = const(0x20) -_POWER2_SPKL_MASK = const(0x10) -_POWER2_SPKR_MASK = const(0x08) -_POWER3_LMIC_MASK = const(0x20) -_POWER3_RMIC_MASK = const(0x10) -_POWER3_LOMIX_MASK = const(0x08) -_POWER3_ROMIX_MASK = const(0x04) - -# Register _DACCTL1 .. 3 -_DACCTL1_MONOMIX_MASK = const(0x10) -_DACCTL1_MONOMIX_SHIFT = const(0x4) -_DACCTL1_DACMU_MASK = const(0x08) -_DACCTL1_DEEM_MASK = const(0x06) -_DACCTL1_DEEM_SHIFT = const(0x01) -_DACCTL2_DACSMM_MASK = const(0x08) -_DACCTL2_DACMR_MASK = const(0x04) -_DACCTL3_ALCSR_MASK = const(0x07) - -# _WM8060_ALC1 .. 3 -_ALC_CHANNEL_MASK = const(0x180) -_ALC_CHANNEL_SHIFT = const(0x7) -_ALC_MODE_MASK = const(0x100) -_ALC_MODE_SHIFT = const(0x8) -_ALC_GAIN_MASK = const(0x70) -_ALC_GAIN_SHIFT = const(0x4) -_ALC_TARGET_MASK = const(0x0F) -_ALC_ATTACK_MASK = const(0x0F) -_ALC_DECAY_MASK = const(0xF0) -_ALC_DECAY_SHIFT = const(4) -_ALC_HOLD_MASK = const(0xF) - -# Register _NOISEG -_NOISEG_LEVEL_SHIFT = const(3) - -_I2C_ADDR = const(0x1A) - -# WM8960 maximum volume values -_MAX_VOLUME_ADC = const(0xFF) -_MAX_VOLUME_DAC = const(0xFF) -_MAX_VOLUME_HEADPHONE = const(0x7F) -_MAX_VOLUME_LINEIN = const(0x3F) -_MAX_VOLUME_SPEAKER = const(0x7F) - -# Config symbol names -# Modules -MODULE_ADC = const(0) # ADC module in WM8960 -MODULE_DAC = const(1) # DAC module in WM8960 -MODULE_VREF = const(2) # VREF module -MODULE_HEADPHONE = const(3) # Headphone -MODULE_MIC_BIAS = const(4) # Mic bias -MODULE_MIC = const(5) # Input Mic -MODULE_LINE_IN = const(6) # Analog in PGA -MODULE_LINE_OUT = const(7) # Line out module -MODULE_SPEAKER = const(8) # Speaker module -MODULE_OMIX = const(9) # Output mixer -MODULE_MONO_OUT = const(10) # Mono mix - -# Route -ROUTE_BYPASS = const(0) # LINEIN->Headphone. -ROUTE_PLAYBACK = const(1) # I2SIN->DAC->Headphone. -ROUTE_PLAYBACK_RECORD = const(2) # I2SIN->DAC->Headphone, LINEIN->ADC->I2SOUT. -ROUTE_RECORD = const(5) # LINEIN->ADC->I2SOUT. - -# Input -INPUT_CLOSED = const(0) # Input device is closed -INPUT_MIC1 = const(1) # Input as single ended mic, only use L/RINPUT1 -INPUT_MIC2 = const(2) # Input as diff. mic, use L/RINPUT1 and L/RINPUT2 -INPUT_MIC3 = const(3) # Input as diff. mic, use L/RINPUT1 and L/RINPUT3 -INPUT_LINE2 = const(4) # Input as line input, only use L/RINPUT2 -INPUT_LINE3 = const(5) # Input as line input, only use L/RINPUT3 - -# ADC sync input -SYNC_ADC = const(0) # Use ADCLRC pin for ADC sync -SYNC_DAC = const(1) # used DACLRC pin for ADC sync - -# Protocol type -BUS_I2S = const(2) # I2S type -BUS_LEFT_JUSTIFIED = const(1) # Left justified mode -BUS_RIGHT_JUSTIFIED = const(0) # Right justified mode -BUS_PCMA = const(3) # PCM A mode -BUS_PCMB = const(3 | (1 << 4)) # PCM B mode - -# Channel swap -SWAP_NONE = const(0) -SWAP_INPUT = const(1) -SWAP_OUTPUT = const(2) - -# Mute settings -MUTE_FAST = const(0) -MUTE_SLOW = const(1) - -# ALC settings -ALC_OFF = const(0) -ALC_RIGHT = const(1) -ALC_LEFT = const(2) -ALC_STEREO = const(3) -ALC_MODE = const(0) # ALC mode -ALC_LIMITER = const(1) # Limiter mode - -# Clock Source -SYSCLK_MCLK = const(0) # sysclk source from external MCLK -SYSCLK_PLL = const(1) # sysclk source from internal PLL - - -class Regs: - # register cache of 56 register. Since registers cannot be read back, they are - # kept in the table for modification - # fmt: off - cache = array.array("H", ( - 0x0097, 0x0097, 0x0000, 0x0000, 0x0000, 0x0008, 0x0000, - 0x000a, 0x01c0, 0x0000, 0x00ff, 0x00ff, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x007b, 0x0100, 0x0032, 0x0000, - 0x00c3, 0x00c3, 0x01c0, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0100, 0x0100, 0x0050, - 0x0050, 0x0050, 0x0050, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0040, 0x0000, 0x0000, 0x0050, 0x0050, 0x0000, 0x0002, - 0x0037, 0x004d, 0x0080, 0x0008, 0x0031, 0x0026, 0x00e9 - )) - # fmt: on - - def __init__(self, i2c, i2c_address=_I2C_ADDR): - self.value_buffer = bytearray(2) - self.i2c = i2c - self.i2c_address = i2c_address - - def __getitem__(self, reg): - return self.cache[reg] - - def __setitem__(self, reg, value): - if type(reg) is tuple: - if type(value) is tuple: - self[reg[0]] = value[0] - self[reg[1]] = value[1] - else: - self[reg[0]] = value - self[reg[1]] = value - else: - if type(value) is tuple: - val = (self.cache[reg] & (~value[0] & 0xFFFF)) | value[1] - else: - val = value - self.cache[reg] = val - self.value_buffer[0] = (reg << 1) | ((val >> 8) & 0x01) - self.value_buffer[1] = val & 0xFF - self.i2c.writeto(self.i2c_address, self.value_buffer) - - -class WM8960: - - _bit_clock_divider_table = { - 2: 0, - 3: 1, - 4: 2, - 6: 3, - 8: 4, - 11: 5, - 12: 6, - 16: 7, - 22: 8, - 24: 9, - 32: 10, - 44: 11, - 48: 12, - } - - _dac_divider_table = { - 1.0 * 256: 0b000, - 1.5 * 256: 0b001, - 2 * 256: 0b010, - 3 * 256: 0b011, - 4 * 256: 0b100, - 5.5 * 256: 0b101, - 6 * 256: 0b110, - } - - _audio_word_length_table = { - 16: 0b00, - 20: 0b01, - 24: 0b10, - 32: 0b11, - } - - _alc_sample_rate_table = { - 48000: 0, - 44100: 0, - 32000: 1, - 24000: 2, - 22050: 2, - 16000: 3, - 12000: 4, - 11025: 4, - 8000: 5, - } - - _volume_config_table = { - MODULE_ADC: (_MAX_VOLUME_ADC, _LADC, 0x100), - MODULE_DAC: (_MAX_VOLUME_DAC, _LDAC, 0x100), - MODULE_HEADPHONE: (_MAX_VOLUME_HEADPHONE, _LOUT1, 0x180), - MODULE_LINE_IN: (_MAX_VOLUME_LINEIN, _LINVOL, 0x140), - MODULE_SPEAKER: (_MAX_VOLUME_SPEAKER, _LOUT2, 0x180), - } - - _input_config_table = { - INPUT_CLOSED: None, - INPUT_MIC1: (0x138, 0x117), - INPUT_MIC2: (0x178, 0x117), - INPUT_MIC3: (0x1B8, 0x117), - INPUT_LINE2: (0, 0xE), - INPUT_LINE3: (0, 0x70), - } - - def __init__( - self, - i2c, - sample_rate=16000, - bits=16, - swap=SWAP_NONE, - route=ROUTE_PLAYBACK_RECORD, - left_input=INPUT_MIC3, - right_input=INPUT_MIC2, - sysclk_source=SYSCLK_MCLK, - mclk_freq=None, - primary=False, - adc_sync=SYNC_DAC, - protocol=BUS_I2S, - i2c_address=_I2C_ADDR, - ): - self.regs = regs = Regs(i2c, i2c_address) - self.sample_rate = sample_rate - - # check parameter consistency and set the sysclk value - if sysclk_source == SYSCLK_PLL: - if sample_rate in (11025, 22050, 44100): - sysclk = 11289600 - else: - sysclk = 12288000 - if sysclk < sample_rate * 256: - sysclk = sample_rate * 256 - if mclk_freq is None: - mclk_freq = sysclk - else: # sysclk_source == SYSCLK_MCLK - if mclk_freq is None: - mclk_freq = sample_rate * 256 - sysclk = mclk_freq - - regs[_RESET] = 0x00 - # VMID=50K, Enable VREF, AINL, AINR, ADCL and ADCR - # I2S_IN (bit 0), I2S_OUT (bit 1), DAP (bit 4), DAC (bit 5), ADC (bit 6) are powered on - regs[_POWER1] = 0xFE - # Enable DACL, DACR, LOUT1, ROUT1, PLL down, SPKL, SPKR - regs[_POWER2] = 0x1F8 - # Enable left and right channel input PGA, left and right output mixer - regs[_POWER3] = 0x3C - - if adc_sync == SYNC_ADC: - # ADC and DAC use different Frame Clock Pins - regs[_IFACE2] = 0x00 # ADCLRC 0x00:Input 0x40:output. - else: - # ADC and DAC use the same Frame Clock Pin - regs[_IFACE2] = 0x40 # ADCLRC 0x00:Input 0x40:output. - self.set_data_route(route) - self.set_protocol(protocol) - - if sysclk_source == SYSCLK_PLL: - self.set_internal_pll_config(mclk_freq, sysclk) - if primary: - self.set_master_clock(sysclk, sample_rate, bits) - # set master bit. - self.regs[_IFACE1] = (0, _IFACE1_MS_MASK) - - self.set_speaker_clock(sysclk) - - # swap channels - if swap & SWAP_INPUT: - regs[_IFACE1] = (0, _IFACE1_ALRSWAP_MASK) - if swap & SWAP_OUTPUT: - regs[_IFACE1] = (0, _IFACE1_DLRSWAP_MASK) - - self.set_left_input(left_input) - self.set_right_input(right_input) - - regs[_ADDCTL1] = 0x0C0 - regs[_ADDCTL4] = 0x60 # Set GPIO1 to 0. - - regs[_BYPASS1] = regs[_BYPASS2] = 0x0 - # ADC volume, 0dB - regs[_LADC, _RADC] = 0x1C3 - # Digital DAC volume, 0dB - regs[_LDAC, _RDAC] = 0x1FF - # Headphone volume, LOUT1 and ROUT1, 0dB - regs[_LOUT1, _ROUT1] = 0x16F - # speaker volume 6dB - regs[_LOUT2, _ROUT2] = 0x1FF - # enable class D output - regs[_CLASSD1] = 0xF7 - # Unmute DAC. - regs[_DACCTL1] = 0x0000 - # Input PGA volume 0 dB - regs[_LINVOL, _RINVOL] = 0x117 - - self.config_data_format(sysclk, sample_rate, bits) - - def deinit(self): - - self.set_module(MODULE_ADC, False) - self.set_module(MODULE_DAC, False) - self.set_module(MODULE_VREF, False) - self.set_module(MODULE_LINE_IN, False) - self.set_module(MODULE_LINE_OUT, False) - self.set_module(MODULE_SPEAKER, False) - - def set_internal_pll_config(self, input_mclk, output_clk): - regs = self.regs - pllF2 = output_clk * 4 - pll_prescale = 0 - sysclk_div = 1 - frac_mode = 0 - - # disable PLL power - regs[_POWER2] = (1, 0) - regs[_CLOCK1] = (7, 0) - - pllN = pllF2 // input_mclk - if pllN < _PLL_N_MIN_VALUE: - input_mclk //= 2 - pll_prescale = 1 - pllN = pllF2 // input_mclk - if pllN < _PLL_N_MIN_VALUE: - sysclk_div = 2 - pllF2 *= 2 - pllN = pllF2 // input_mclk - - if (pllN < _PLL_N_MIN_VALUE) or (pllN > _PLL_N_MAX_VALUE): - raise ValueError("Invalid MCLK vs. sysclk ratio") - - pllK = ((pllF2 % input_mclk) * (1 << 24)) // input_mclk - if pllK != 0: - frac_mode = 1 - - regs[_PLL1] = (frac_mode << 5) | (pll_prescale << 4) | (pllN & 0x0F) - regs[_PLL2] = (pllK >> 16) & 0xFF - regs[_PLL3] = (pllK >> 8) & 0xFF - regs[_PLL4] = pllK & 0xFF - # enable PLL power - regs[_POWER2] = (1, 1) - regs[_CLOCK1] = (7, ((0 if sysclk_div == 1 else sysclk_div) << 1) | 1) - - def set_master_clock(self, sysclk, sample_rate, bit_width): - bit_clock_divider = (sysclk * 2) // (sample_rate * bit_width * 2) - try: - reg_divider = self._bit_clock_divider_table[bit_clock_divider] - except: - raise ValueError("Invalid ratio of sysclk sample rate and bits") - # configure the master bit clock divider - self.regs[_CLOCK2] = (_CLOCK2_BCLK_DIV_MASK, reg_divider) - - def set_speaker_clock(self, sysclk): - speaker_divider_table = (1.5, 2, 3, 4, 6, 8, 12, 16) - for val in range(8): - divider = speaker_divider_table[val] - f = sysclk / divider - if 500_000 < f < 1_000_000: - break - else: - val = 7 - self.regs[_CLOCK2] = ( - _CLOCK2_DCLK_DIV_MASK, - val << _CLOCK2_DCLK_DIV_SHIFT, - ) - - def set_module(self, module, is_enabled): - - is_enabled = 1 if is_enabled else 0 - regs = self.regs - - if module == MODULE_ADC: - - regs[_POWER1] = ( - _POWER1_ADCL_MASK | _POWER1_ADCR_MASK, - (_POWER1_ADCL_MASK | _POWER1_ADCR_MASK) * is_enabled, - ) - - elif module == MODULE_DAC: - - regs[_POWER2] = ( - _POWER2_DACL_MASK | _POWER2_DACR_MASK, - (_POWER2_DACL_MASK | _POWER2_DACR_MASK) * is_enabled, - ) - - elif module == MODULE_VREF: - - regs[_POWER1] = ( - _POWER1_VREF_MASK, - (is_enabled << _POWER1_VREF_SHIFT), - ) - - elif module == MODULE_LINE_IN: - - regs[_POWER1] = ( - _POWER1_AINL_MASK | _POWER1_AINR_MASK, - (_POWER1_AINL_MASK | _POWER1_AINR_MASK) * is_enabled, - ) - regs[_POWER3] = ( - _POWER3_LMIC_MASK | _POWER3_RMIC_MASK, - (_POWER3_LMIC_MASK | _POWER3_RMIC_MASK) * is_enabled, - ) - - elif module == MODULE_LINE_OUT: - - regs[_POWER2] = ( - _POWER2_LOUT1_MASK | _POWER2_ROUT1_MASK, - (_POWER2_LOUT1_MASK | _POWER2_ROUT1_MASK) * is_enabled, - ) - - elif module == MODULE_MIC_BIAS: - - regs[_POWER1] = ( - _POWER1_MICB_MASK, - (is_enabled << _POWER1_MICB_SHIFT), - ) - - elif module == MODULE_SPEAKER: - - regs[_POWER2] = ( - _POWER2_SPKL_MASK | _POWER2_SPKR_MASK, - (_POWER2_SPKL_MASK | _POWER2_SPKR_MASK) * is_enabled, - ) - regs[_CLASSD1] = 0xF7 - - elif module == MODULE_OMIX: - - regs[_POWER3] = ( - _POWER3_LOMIX_MASK | _POWER3_ROMIX_MASK, - (_POWER3_LOMIX_MASK | _POWER3_ROMIX_MASK) * is_enabled, - ) - - elif module == MODULE_MONO_OUT: - - regs[_MONOMIX1] = regs[_MONOMIX2] = is_enabled << 7 - regs[_MONO] = is_enabled << 6 - - else: - raise ValueError("Invalid module") - - def enable_module(self, module): - self.set_module(module, True) - - def disable_module(self, module): - self.set_module(module, False) - - def set_data_route(self, route): - regs = self.regs - if route == ROUTE_BYPASS: - # Bypass means from line-in to HP - # Left LINPUT3 to left output mixer, LINPUT3 left output mixer volume = 0dB - # Right RINPUT3 to right output mixer, RINPUT3 right output mixer volume = 0dB - regs[_LOUTMIX, _ROUTMIX] = 0x80 - - elif route == ROUTE_PLAYBACK: - # Data route I2S_IN-> DAC-> HP - # - # Left DAC to left output mixer, LINPUT3 left output mixer volume = 0dB - # Right DAC to right output mixer, RINPUT3 right output mixer volume = 0dB - regs[_LOUTMIX, _ROUTMIX] = 0x100 - regs[_POWER3] = 0x0C - # Set power for DAC - self.set_module(MODULE_DAC, True) - self.set_module(MODULE_OMIX, True) - self.set_module(MODULE_LINE_OUT, True) - - elif route == ROUTE_PLAYBACK_RECORD: - # - # Left DAC to left output mixer, LINPUT3 left output mixer volume = 0dB - # Right DAC to right output mixer, RINPUT3 right output mixer volume = 0dB - regs[_LOUTMIX, _ROUTMIX] = 0x100 - regs[_POWER3] = 0x3C - self.set_module(MODULE_DAC, True) - self.set_module(MODULE_ADC, True) - self.set_module(MODULE_LINE_IN, True) - self.set_module(MODULE_OMIX, True) - self.set_module(MODULE_LINE_OUT, True) - - elif route == ROUTE_RECORD: - # LINE_IN->ADC->I2S_OUT - # Left and right input boost, LIN3BOOST and RIN3BOOST = 0dB - regs[_POWER3] = 0x30 - # Power up ADC and AIN - self.set_module(MODULE_LINE_IN, True) - self.set_module(MODULE_ADC, True) - - else: - raise ValueError("Invalid route") - - def set_left_input(self, input): - if not input in self._input_config_table.keys(): - raise ValueError("Invalid input") - - input = self._input_config_table[input] - - regs = self.regs - if input is None: - regs[_POWER1] = (_POWER1_AINL_MASK | _POWER1_ADCL_MASK, 0) - elif input[0] == 0: - regs[_POWER1] = (0, _POWER1_AINL_MASK | _POWER1_ADCL_MASK) - regs[_INBMIX1] = input - else: - regs[_POWER1] = (0, _POWER1_AINL_MASK | _POWER1_ADCL_MASK | _POWER1_MICB_MASK) - regs[_LINPATH] = input[0] - regs[_LINVOL] = input[1] - - def set_right_input(self, input): - if not input in self._input_config_table.keys(): - raise ValueError("Invalid input name") - - input = self._input_config_table[input] - - regs = self.regs - if input is None: - regs[_POWER1] = (_POWER1_AINR_MASK | _POWER1_ADCR_MASK, 0) - elif input[0] == 0: - regs[_POWER1] = (0, _POWER1_AINL_MASK | _POWER1_ADCR_MASK) - regs[_INBMIX2] = input - else: - regs[_POWER1] = (0, _POWER1_AINR_MASK | _POWER1_ADCR_MASK | _POWER1_MICB_MASK) - regs[_RINPATH] = input[0] - regs[_RINVOL] = input[1] - - def set_protocol(self, protocol): - self.regs[_IFACE1] = ( - _IFACE1_FORMAT_MASK | _IFACE1_LRP_MASK, - protocol, - ) - - def config_data_format(self, sysclk, sample_rate, bits): - # Compute sample rate divider, dac and adc are the same sample rate - try: - divider = self._dac_divider_table[sysclk // sample_rate] - wl = self._audio_word_length_table[bits] - except: - raise ValueError("Invalid ratio sysclk/sample_rate or invalid bit length") - - self.regs[_CLOCK1] = (0x1F8, divider << 6 | divider << 3) - self.regs[_IFACE1] = (_IFACE1_WL_MASK, wl << _IFACE1_WL_SHIFT) - - def volume(self, module, volume_l=None, volume_r=None): - if not module in self._volume_config_table.keys(): - raise ValueError("Invalid module") - - if volume_l is None: # get volume - vol_max, regnum, _ = self._volume_config_table[module] - return ( - int((self.regs[regnum] & vol_max) * 100 / vol_max + 0.5), - int((self.regs[regnum + 1] & vol_max) * 100 / vol_max + 0.5), - ) - else: # set volume - if volume_r is None: - volume_r = volume_l - - if not ((0 <= volume_l <= 100) and (0 <= volume_r <= 100)): - raise ValueError("Invalid value for volume") - elif not module in self._volume_config_table.keys(): - raise ValueError("Invalid module") - - vol_max, regnum, flags = self._volume_config_table[module] - self.regs[regnum] = int(volume_l * vol_max / 100 + 0.5) | flags - self.regs[regnum + 1] = int(volume_r * vol_max / 100 + 0.5) | flags - - def mute(self, enable, soft=True, ramp=MUTE_FAST): - enable = _DACCTL1_DACMU_MASK if enable else 0 - soft = _DACCTL2_DACSMM_MASK if soft else 0 - ramp = _DACCTL2_DACMR_MASK if ramp == MUTE_SLOW else 0 - self.regs[_DACCTL1] = (_DACCTL1_DACMU_MASK, enable) - self.regs[_DACCTL2] = ( - _DACCTL2_DACSMM_MASK | _DACCTL2_DACMR_MASK, - soft | ramp, - ) - - def expand_3d(self, depth=0): - depth &= 0x0F - cutoff = 0 if self.sample_rate >= 32000 else 0b1100000 - self.regs[_3D] = cutoff | depth << 1 | (1 if depth > 0 else 0) - - def mono(self, enable): - enable = 1 if enable else 0 - self.regs[_DACCTL1] = ( - _DACCTL1_MONOMIX_MASK, - enable << _DACCTL1_MONOMIX_SHIFT, - ) - - def alc_mode(self, channel, mode=ALC_MODE): - if mode != ALC_MODE: - mode = ALC_LIMITER - channel &= 3 - self.regs[_ALC1] = ( - _ALC_CHANNEL_MASK, - channel << _ALC_CHANNEL_SHIFT, - ) - self.regs[_ALC3] = (_ALC_MODE_MASK, mode << _ALC_MODE_SHIFT) - try: - rate = _alc_sample_rate_table[self.sample_rate] - except: - rate = 0 - self.regs[_ADDCTL3] = (_DACCTL3_ALCSR_MASK, rate) - - def alc_gain(self, target=-12, max_gain=30, min_gain=-17.25, noise_gate=-78): - def limit(value, minval, maxval): - value = int(value) - if value < minval: - value = minval - if value > maxval: - value = maxval - return value - - target = limit((16 + (target * 2) // 3), 0, 15) - max_gain = limit((max_gain + 12) // 6, 0, 7) - min_gain = limit((min_gain * 4 + 69) // 24, 0, 7) - noise_gate = limit((noise_gate * 2 + 153) // 3, -1, 31) - self.regs[_ALC1] = ( - _ALC_GAIN_MASK | _ALC_TARGET_MASK, - (max_gain << _ALC_GAIN_SHIFT) | target, - ) - self.regs[_ALC2] = (_ALC_GAIN_MASK, (min_gain << _ALC_GAIN_SHIFT)) - if noise_gate >= 0: - self.regs[_NOISEG] = noise_gate << _NOISEG_LEVEL_SHIFT | 1 - else: - self.regs[_NOISEG] = 0 - - def alc_time(self, attack=24, decay=192, hold=0): - def logb(value, limit): - value = int(value) - lb = 0 - while value > 1: - value >>= 1 - lb += 1 - if lb > limit: - lb = limit - return lb - - attack = logb(attack / 6, 7) - decay = logb(decay / 24, 7) - hold = logb((hold * 3) / 8, 15) - self.regs[_ALC2] = (_ALC_HOLD_MASK, hold) - self.regs[_ALC3] = ( - _ALC_DECAY_MASK | _ALC_ATTACK_MASK, - (decay << _ALC_DECAY_SHIFT) | attack, - ) - - def deemphasis(self, enable): - deem_table = (32000, 44100, 48000) - enable = not not enable - if enable and self.sample_rate in deem_table: - val = deem_table.index(self.sample_rate) + 1 - else: - val = 0 - self.regs[_DACCTL1] = (_DACCTL1_DEEM_MASK, val << _DACCTL1_DEEM_SHIFT) diff --git a/drivers/dht/dht.py b/drivers/dht/dht.py deleted file mode 100644 index 411e9a8d28a6b..0000000000000 --- a/drivers/dht/dht.py +++ /dev/null @@ -1,46 +0,0 @@ -# DHT11/DHT22 driver for MicroPython on ESP8266 -# MIT license; Copyright (c) 2016 Damien P. George - -import sys - -if sys.platform.startswith("esp"): - from esp import dht_readinto -elif sys.platform == "mimxrt": - from mimxrt import dht_readinto -elif sys.platform == "rp2": - from rp2 import dht_readinto -elif sys.platform == "pyboard": - from pyb import dht_readinto -else: - from machine import dht_readinto - - -class DHTBase: - def __init__(self, pin): - self.pin = pin - self.buf = bytearray(5) - - def measure(self): - buf = self.buf - dht_readinto(self.pin, buf) - if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xFF != buf[4]: - raise Exception("checksum error") - - -class DHT11(DHTBase): - def humidity(self): - return self.buf[0] - - def temperature(self): - return self.buf[2] - - -class DHT22(DHTBase): - def humidity(self): - return (self.buf[0] << 8 | self.buf[1]) * 0.1 - - def temperature(self): - t = ((self.buf[2] & 0x7F) << 8 | self.buf[3]) * 0.1 - if self.buf[2] & 0x80: - t = -t - return t diff --git a/drivers/dht/manifest.py b/drivers/dht/manifest.py deleted file mode 100644 index 72a4e0d24fcd5..0000000000000 --- a/drivers/dht/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("dht.py", opt=3) diff --git a/drivers/display/lcd160cr.py b/drivers/display/lcd160cr.py deleted file mode 100644 index f792418aa2b55..0000000000000 --- a/drivers/display/lcd160cr.py +++ /dev/null @@ -1,482 +0,0 @@ -# Driver for official MicroPython LCD160CR display -# MIT license; Copyright (c) 2017 Damien P. George - -from micropython import const -from utime import sleep_ms -from ustruct import calcsize, pack_into -import uerrno, machine - -# for set_orient -PORTRAIT = const(0) -LANDSCAPE = const(1) -PORTRAIT_UPSIDEDOWN = const(2) -LANDSCAPE_UPSIDEDOWN = const(3) - -# for set_startup_deco; can be or'd -STARTUP_DECO_NONE = const(0) -STARTUP_DECO_MLOGO = const(1) -STARTUP_DECO_INFO = const(2) - -_uart_baud_table = { - 2400: 0, - 4800: 1, - 9600: 2, - 19200: 3, - 38400: 4, - 57600: 5, - 115200: 6, - 230400: 7, - 460800: 8, -} - - -class LCD160CR: - def __init__(self, connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98): - if connect in ("X", "Y", "XY", "YX"): - i = connect[-1] - j = connect[0] - y = j + "4" - elif connect == "C": - i = 2 - j = 2 - y = "A7" - else: - if pwr is None or i2c is None or spi is None: - raise ValueError('must specify valid "connect" or all of "pwr", "i2c" and "spi"') - - if pwr is None: - pwr = machine.Pin(y, machine.Pin.OUT) - if i2c is None: - i2c = machine.I2C(i, freq=1000000) - if spi is None: - spi = machine.SPI(j, baudrate=13500000, polarity=0, phase=0) - - if not pwr.value(): - pwr(1) - sleep_ms(10) - # else: - # alread have power - # lets be optimistic... - - # set connections - self.pwr = pwr - self.i2c = i2c - self.spi = spi - self.i2c_addr = i2c_addr - - # create temp buffers and memoryviews - self.buf16 = bytearray(16) - self.buf19 = bytearray(19) - self.buf = [None] * 10 - for i in range(1, 10): - self.buf[i] = memoryview(self.buf16)[0:i] - self.buf1 = self.buf[1] - self.array4 = [0, 0, 0, 0] - - # set default orientation and window - self.set_orient(PORTRAIT) - self._fcmd2b("= n: - self.i2c.readfrom_into(self.i2c_addr, buf) - return - t -= 1 - sleep_ms(1) - raise OSError(uerrno.ETIMEDOUT) - - def oflush(self, n=255): - t = 5000 - while t: - self.i2c.readfrom_into(self.i2c_addr + 1, self.buf1) - r = self.buf1[0] - if r >= n: - return - t -= 1 - machine.idle() - raise OSError(uerrno.ETIMEDOUT) - - def iflush(self): - t = 5000 - while t: - self.i2c.readfrom_into(self.i2c_addr, self.buf16) - if self.buf16[0] == 0: - return - t -= 1 - sleep_ms(1) - raise OSError(uerrno.ETIMEDOUT) - - #### MISC METHODS #### - - @staticmethod - def rgb(r, g, b): - return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3) - - @staticmethod - def clip_line(c, w, h): - while True: - ca = ce = 0 - if c[1] < 0: - ca |= 8 - elif c[1] > h: - ca |= 4 - if c[0] < 0: - ca |= 1 - elif c[0] > w: - ca |= 2 - if c[3] < 0: - ce |= 8 - elif c[3] > h: - ce |= 4 - if c[2] < 0: - ce |= 1 - elif c[2] > w: - ce |= 2 - if ca & ce: - return False - elif ca | ce: - ca |= ce - if ca & 1: - if c[2] < c[0]: - c[0], c[2] = c[2], c[0] - c[1], c[3] = c[3], c[1] - c[1] += ((-c[0]) * (c[3] - c[1])) // (c[2] - c[0]) - c[0] = 0 - elif ca & 2: - if c[2] < c[0]: - c[0], c[2] = c[2], c[0] - c[1], c[3] = c[3], c[1] - c[3] += ((w - 1 - c[2]) * (c[3] - c[1])) // (c[2] - c[0]) - c[2] = w - 1 - elif ca & 4: - if c[0] == c[2]: - if c[1] >= h: - c[1] = h - 1 - if c[3] >= h: - c[3] = h - 1 - else: - if c[3] < c[1]: - c[0], c[2] = c[2], c[0] - c[1], c[3] = c[3], c[1] - c[2] += ((h - 1 - c[3]) * (c[2] - c[0])) // (c[3] - c[1]) - c[3] = h - 1 - else: - if c[0] == c[2]: - if c[1] < 0: - c[1] = 0 - if c[3] < 0: - c[3] = 0 - else: - if c[3] < c[1]: - c[0], c[2] = c[2], c[0] - c[1], c[3] = c[3], c[1] - c[0] += ((-c[1]) * (c[2] - c[0])) // (c[3] - c[1]) - c[1] = 0 - else: - return True - - #### SETUP COMMANDS #### - - def set_power(self, on): - self.pwr(on) - sleep_ms(15) - - def set_orient(self, orient): - self._fcmd2("= 2: - self.i2c.readfrom_into(self.i2c_addr, self.buf[3]) - return self.buf[3][1] | self.buf[3][2] << 8 - t -= 1 - sleep_ms(1) - raise OSError(uerrno.ETIMEDOUT) - - def get_line(self, x, y, buf): - l = len(buf) // 2 - self._fcmd2b("= l: - self.i2c.readfrom_into(self.i2c_addr, buf) - return - t -= 1 - sleep_ms(1) - raise OSError(uerrno.ETIMEDOUT) - - def screen_dump(self, buf, x=0, y=0, w=None, h=None): - if w is None: - w = self.w - x - if h is None: - h = self.h - y - if w <= 127: - line = bytearray(2 * w + 1) - line2 = None - else: - # split line if more than 254 bytes needed - buflen = (w + 1) // 2 - line = bytearray(2 * buflen + 1) - line2 = memoryview(line)[: 2 * (w - buflen) + 1] - for i in range(min(len(buf) // (2 * w), h)): - ix = i * w * 2 - self.get_line(x, y + i, line) - buf[ix : ix + len(line) - 1] = memoryview(line)[1:] - ix += len(line) - 1 - if line2: - self.get_line(x + buflen, y + i, line2) - buf[ix : ix + len(line2) - 1] = memoryview(line2)[1:] - ix += len(line2) - 1 - - def screen_load(self, buf): - l = self.w * self.h * 2 + 2 - self._fcmd2b("= 0x200: - self._send(ar[n : n + 0x200]) - n += 0x200 - else: - self._send(ar[n:]) - while n < self.w * self.h * 2: - self._send(b"\x00") - n += 1 - - #### TEXT COMMANDS #### - - def set_pos(self, x, y): - self._fcmd2("= self.w or y >= self.h: - return - elif x < 0 or y < 0: - left = top = True - if x < 0: - left = False - w += x - x = 0 - if y < 0: - top = False - h += y - y = 0 - if cmd == 0x51 or cmd == 0x72: - # draw interior - self._fcmd2b("> 7 != 0 - - def get_touch(self): - self._send(b"\x02T") # implicit LCD output flush - b = self.buf[4] - self._waitfor(3, b) - return b[1] >> 7, b[2], b[3] - - #### ADVANCED COMMANDS #### - - def set_spi_win(self, x, y, w, h): - pack_into( - " 32: - raise ValueError("length must be 32 or less") - self._fcmd2(" 0xFFFF: - raise ValueError("length must be 65535 or less") - self.oflush() - self._fcmd2(" 0: - s = "%6.3fV" % data[i] - else: - s = "%5.1f°C" % data[i] - if lcd.h == 160: - lcd.set_font(1, bold=0, scale=1) - else: - lcd.set_font(1, bold=0, scale=1, trans=1) - lcd.set_pos(45, lcd.h - 60 + i * 16) - lcd.write(s) - - -def test_features(lcd, orient=lcd160cr.PORTRAIT): - # if we run on pyboard then use ADC and RTC features - try: - import pyb - - adc = pyb.ADCAll(12, 0xF0000) - rtc = pyb.RTC() - except: - adc = None - rtc = None - - # set orientation and clear screen - lcd = get_lcd(lcd) - lcd.set_orient(orient) - lcd.set_pen(0, 0) - lcd.erase() - - # create M-logo - mlogo = framebuf.FrameBuffer(bytearray(17 * 17 * 2), 17, 17, framebuf.RGB565) - mlogo.fill(0) - mlogo.fill_rect(1, 1, 15, 15, 0xFFFFFF) - mlogo.vline(4, 4, 12, 0) - mlogo.vline(8, 1, 12, 0) - mlogo.vline(12, 4, 12, 0) - mlogo.vline(14, 13, 2, 0) - - # create inline framebuf - offx = 14 - offy = 19 - w = 100 - h = 75 - fbuf = framebuf.FrameBuffer(bytearray(w * h * 2), w, h, framebuf.RGB565) - lcd.set_spi_win(offx, offy, w, h) - - # initialise loop parameters - tx = ty = 0 - t0 = time.ticks_us() - - for i in range(300): - # update position of cross-hair - t, tx2, ty2 = lcd.get_touch() - if t: - tx2 -= offx - ty2 -= offy - if tx2 >= 0 and ty2 >= 0 and tx2 < w and ty2 < h: - tx, ty = tx2, ty2 - else: - tx = (tx + 1) % w - ty = (ty + 1) % h - - # create and show the inline framebuf - fbuf.fill(lcd.rgb(128 + int(64 * math.cos(0.1 * i)), 128, 192)) - fbuf.line( - w // 2, - h // 2, - w // 2 + int(40 * math.cos(0.2 * i)), - h // 2 + int(40 * math.sin(0.2 * i)), - lcd.rgb(128, 255, 64), - ) - fbuf.hline(0, ty, w, lcd.rgb(64, 64, 64)) - fbuf.vline(tx, 0, h, lcd.rgb(64, 64, 64)) - fbuf.rect(tx - 3, ty - 3, 7, 7, lcd.rgb(64, 64, 64)) - for phase in (-0.2, 0, 0.2): - x = w // 2 - 8 + int(50 * math.cos(0.05 * i + phase)) - y = h // 2 - 8 + int(32 * math.sin(0.05 * i + phase)) - fbuf.blit(mlogo, x, y) - for j in range(-3, 3): - fbuf.text( - "MicroPython", - 5, - h // 2 + 9 * j + int(20 * math.sin(0.1 * (i + j))), - lcd.rgb(128 + 10 * j, 0, 128 - 10 * j), - ) - lcd.show_framebuf(fbuf) - - # show results from the ADC - if adc: - show_adc(lcd, adc) - - # show the time - if rtc: - lcd.set_pos(2, 0) - lcd.set_font(1) - t = rtc.datetime() - lcd.write( - "%4d-%02d-%02d %2d:%02d:%02d.%01d" - % (t[0], t[1], t[2], t[4], t[5], t[6], t[7] // 100000) - ) - - # compute the frame rate - t1 = time.ticks_us() - dt = time.ticks_diff(t1, t0) - t0 = t1 - - # show the frame rate - lcd.set_pos(2, 9) - lcd.write("%.2f fps" % (1000000 / dt)) - - -def test_mandel(lcd, orient=lcd160cr.PORTRAIT): - # set orientation and clear screen - lcd = get_lcd(lcd) - lcd.set_orient(orient) - lcd.set_pen(0, 0xFFFF) - lcd.erase() - - # function to compute Mandelbrot pixels - def in_set(c): - z = 0 - for i in range(32): - z = z * z + c - if abs(z) > 100: - return i - return 0 - - # cache width and height of LCD - w = lcd.w - h = lcd.h - - # create the buffer for each line and set SPI parameters - line = bytearray(w * 2) - lcd.set_spi_win(0, 0, w, h) - spi = lcd.fast_spi() - - # draw the Mandelbrot set line-by-line - hh = (h - 1) / 3.2 - ww = (w - 1) / 2.4 - for v in range(h): - for u in range(w): - c = in_set((v / hh - 2.3) + (u / ww - 1.2) * 1j) - if c < 16: - rgb = c << 12 | c << 6 - else: - rgb = 0xF800 | c << 6 - line[2 * u] = rgb - line[2 * u + 1] = rgb >> 8 - spi.write(line) - - -def test_all(lcd, orient=lcd160cr.PORTRAIT): - lcd = get_lcd(lcd) - test_features(lcd, orient) - test_mandel(lcd, orient) - - -print("To run all tests: test_all()") -print("Individual tests are: test_features, test_mandel") -print(' argument should be a connection, eg "X", or an LCD160CR object') diff --git a/drivers/display/manifest.py b/drivers/display/manifest.py deleted file mode 100644 index 16f93a7d4e99a..0000000000000 --- a/drivers/display/manifest.py +++ /dev/null @@ -1,11 +0,0 @@ -# TODO: Split these into separate directories with their own manifests. -options.defaults(lcd160cr=False, ssd1306=False, test=False) - -if options.lcd160cr: - module("lcd160cr.py", opt=3) - - if options.test: - module("lcd160cr_test.py", opt=3) - -if options.ssd1306: - module("ssd1306.py", opt=3) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py deleted file mode 100644 index a504cdadcc94e..0000000000000 --- a/drivers/display/ssd1306.py +++ /dev/null @@ -1,163 +0,0 @@ -# MicroPython SSD1306 OLED driver, I2C and SPI interfaces - -from micropython import const -import framebuf - - -# register definitions -SET_CONTRAST = const(0x81) -SET_ENTIRE_ON = const(0xA4) -SET_NORM_INV = const(0xA6) -SET_DISP = const(0xAE) -SET_MEM_ADDR = const(0x20) -SET_COL_ADDR = const(0x21) -SET_PAGE_ADDR = const(0x22) -SET_DISP_START_LINE = const(0x40) -SET_SEG_REMAP = const(0xA0) -SET_MUX_RATIO = const(0xA8) -SET_IREF_SELECT = const(0xAD) -SET_COM_OUT_DIR = const(0xC0) -SET_DISP_OFFSET = const(0xD3) -SET_COM_PIN_CFG = const(0xDA) -SET_DISP_CLK_DIV = const(0xD5) -SET_PRECHARGE = const(0xD9) -SET_VCOM_DESEL = const(0xDB) -SET_CHARGE_PUMP = const(0x8D) - -# Subclassing FrameBuffer provides support for graphics primitives -# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html -class SSD1306(framebuf.FrameBuffer): - def __init__(self, width, height, external_vcc): - self.width = width - self.height = height - self.external_vcc = external_vcc - self.pages = self.height // 8 - self.buffer = bytearray(self.pages * self.width) - super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) - self.init_display() - - def init_display(self): - for cmd in ( - SET_DISP, # display off - # address setting - SET_MEM_ADDR, - 0x00, # horizontal - # resolution and layout - SET_DISP_START_LINE, # start at line 0 - SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 - SET_MUX_RATIO, - self.height - 1, - SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 - SET_DISP_OFFSET, - 0x00, - SET_COM_PIN_CFG, - 0x02 if self.width > 2 * self.height else 0x12, - # timing and driving scheme - SET_DISP_CLK_DIV, - 0x80, - SET_PRECHARGE, - 0x22 if self.external_vcc else 0xF1, - SET_VCOM_DESEL, - 0x30, # 0.83*Vcc - # display - SET_CONTRAST, - 0xFF, # maximum - SET_ENTIRE_ON, # output follows RAM contents - SET_NORM_INV, # not inverted - SET_IREF_SELECT, - 0x30, # enable internal IREF during display on - # charge pump - SET_CHARGE_PUMP, - 0x10 if self.external_vcc else 0x14, - SET_DISP | 0x01, # display on - ): # on - self.write_cmd(cmd) - self.fill(0) - self.show() - - def poweroff(self): - self.write_cmd(SET_DISP) - - def poweron(self): - self.write_cmd(SET_DISP | 0x01) - - def contrast(self, contrast): - self.write_cmd(SET_CONTRAST) - self.write_cmd(contrast) - - def invert(self, invert): - self.write_cmd(SET_NORM_INV | (invert & 1)) - - def rotate(self, rotate): - self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3)) - self.write_cmd(SET_SEG_REMAP | (rotate & 1)) - - def show(self): - x0 = 0 - x1 = self.width - 1 - if self.width != 128: - # narrow displays use centred columns - col_offset = (128 - self.width) // 2 - x0 += col_offset - x1 += col_offset - self.write_cmd(SET_COL_ADDR) - self.write_cmd(x0) - self.write_cmd(x1) - self.write_cmd(SET_PAGE_ADDR) - self.write_cmd(0) - self.write_cmd(self.pages - 1) - self.write_data(self.buffer) - - -class SSD1306_I2C(SSD1306): - def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): - self.i2c = i2c - self.addr = addr - self.temp = bytearray(2) - self.write_list = [b"\x40", None] # Co=0, D/C#=1 - super().__init__(width, height, external_vcc) - - def write_cmd(self, cmd): - self.temp[0] = 0x80 # Co=1, D/C#=0 - self.temp[1] = cmd - self.i2c.writeto(self.addr, self.temp) - - def write_data(self, buf): - self.write_list[1] = buf - self.i2c.writevto(self.addr, self.write_list) - - -class SSD1306_SPI(SSD1306): - def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): - self.rate = 10 * 1024 * 1024 - dc.init(dc.OUT, value=0) - res.init(res.OUT, value=0) - cs.init(cs.OUT, value=1) - self.spi = spi - self.dc = dc - self.res = res - self.cs = cs - import time - - self.res(1) - time.sleep_ms(1) - self.res(0) - time.sleep_ms(10) - self.res(1) - super().__init__(width, height, external_vcc) - - def write_cmd(self, cmd): - self.spi.init(baudrate=self.rate, polarity=0, phase=0) - self.cs(1) - self.dc(0) - self.cs(0) - self.spi.write(bytearray([cmd])) - self.cs(1) - - def write_data(self, buf): - self.spi.init(baudrate=self.rate, polarity=0, phase=0) - self.cs(1) - self.dc(1) - self.cs(0) - self.spi.write(buf) - self.cs(1) diff --git a/drivers/hts221/hts221.py b/drivers/hts221/hts221.py deleted file mode 100644 index fec52a7389a98..0000000000000 --- a/drivers/hts221/hts221.py +++ /dev/null @@ -1,91 +0,0 @@ -""" -The MIT License (MIT) - -Copyright (c) 2013-2022 Ibrahim Abdelkader -Copyright (c) 2013-2022 Kwabena W. Agyeman - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -HTS221 driver driver for MicroPython. -Original source: https://github.com/ControlEverythingCommunity/HTS221/blob/master/Python/HTS221.py - -Example usage: - -import time -import hts221 -from machine import Pin, I2C - -bus = I2C(1, scl=Pin(15), sda=Pin(14)) -hts = hts221.HTS221(bus) - -while (True): - rH = hts.humidity() - temp = hts.temperature() - print ("rH: %.2f%% T: %.2fC" %(rH, temp)) - time.sleep_ms(100) -""" - -import struct - - -class HTS221: - def __init__(self, i2c, data_rate=1, address=0x5F): - self.bus = i2c - self.odr = data_rate - self.slv_addr = address - - # Set configuration register - # Humidity and temperature average configuration - self.bus.writeto_mem(self.slv_addr, 0x10, b"\x1B") - - # Set control register - # PD | BDU | ODR - cfg = 0x80 | 0x04 | (self.odr & 0x3) - self.bus.writeto_mem(self.slv_addr, 0x20, bytes([cfg])) - - # Read Calibration values from non-volatile memory of the device - # Humidity Calibration values - self.H0 = self._read_reg(0x30, 1) / 2 - self.H1 = self._read_reg(0x31, 1) / 2 - self.H2 = self._read_reg(0x36, 2) - self.H3 = self._read_reg(0x3A, 2) - - # Temperature Calibration values - raw = self._read_reg(0x35, 1) - self.T0 = ((raw & 0x03) * 256) + self._read_reg(0x32, 1) - self.T1 = ((raw & 0x0C) * 64) + self._read_reg(0x33, 1) - self.T2 = self._read_reg(0x3C, 2) - self.T3 = self._read_reg(0x3E, 2) - - def _read_reg(self, reg_addr, size): - fmt = "B" if size == 1 else "H" - reg_addr = reg_addr if size == 1 else reg_addr | 0x80 - return struct.unpack(fmt, self.bus.readfrom_mem(self.slv_addr, reg_addr, size))[0] - - def humidity(self): - rH = self._read_reg(0x28, 2) - return (self.H1 - self.H0) * (rH - self.H2) / (self.H3 - self.H2) + self.H0 - - def temperature(self): - temp = self._read_reg(0x2A, 2) - if temp > 32767: - temp -= 65536 - return ((self.T1 - self.T0) / 8.0) * (temp - self.T2) / (self.T3 - self.T2) + ( - self.T0 / 8.0 - ) diff --git a/drivers/hts221/manifest.py b/drivers/hts221/manifest.py deleted file mode 100644 index 5f1792665943a..0000000000000 --- a/drivers/hts221/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("hts221.py", opt=3) diff --git a/drivers/lps22h/lps22h.py b/drivers/lps22h/lps22h.py deleted file mode 100644 index ca29efce2d6f8..0000000000000 --- a/drivers/lps22h/lps22h.py +++ /dev/null @@ -1,109 +0,0 @@ -""" -The MIT License (MIT) - -Copyright (c) 2016-2019 shaoziyang -Copyright (c) 2022 Ibrahim Abdelkader - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -LPS22HB/HH pressure sensor driver for MicroPython. - -Example usage: - -import time -from lps22h import LPS22H -from machine import Pin, I2C - -bus = I2C(1, scl=Pin(15), sda=Pin(14)) -lps = LPS22H(bus, oneshot=False) - -while (True): - print("Pressure: %.2f hPa Temperature: %.2f C"%(lps.pressure(), lps.temperature())) - time.sleep_ms(10) -""" -import machine - -_LPS22_CTRL_REG1 = const(0x10) -_LPS22_CTRL_REG2 = const(0x11) -_LPS22_STATUS = const(0x27) -_LPS22_TEMP_OUT_L = const(0x2B) -_LPS22_PRESS_OUT_XL = const(0x28) -_LPS22_PRESS_OUT_L = const(0x29) - - -class LPS22H: - def __init__(self, i2c, address=0x5C, oneshot=False): - self.i2c = i2c - self.addr = address - self.oneshot = oneshot - self.buf = bytearray(1) - # ODR=1 EN_LPFP=1 BDU=1 - self._write_reg(_LPS22_CTRL_REG1, 0x1A) - self.set_oneshot_mode(self.oneshot) - - def _int16(self, d): - return d if d < 0x8000 else d - 0x10000 - - def _write_reg(self, reg, dat): - self.buf[0] = dat - self.i2c.writeto_mem(self.addr, reg, self.buf) - - def _read_reg(self, reg, width=8): - self.i2c.readfrom_mem_into(self.addr, reg, self.buf) - val = self.buf[0] - if width == 16: - val |= self._read_reg(reg + 1) << 8 - return val - - def _tigger_oneshot(self, b): - if self.oneshot: - self._write_reg(_LPS22_CTRL_REG2, self._read_reg(_LPS22_CTRL_REG2) | 0x01) - self._read_reg(0x28 + b * 2) - while True: - if self._read_reg(_LPS22_STATUS) & b: - return - machine.idle() - - def set_oneshot_mode(self, oneshot): - self._read_reg(_LPS22_CTRL_REG1) - self.oneshot = oneshot - if oneshot: - self.buf[0] &= 0x0F - else: - self.buf[0] |= 0x10 - self._write_reg(_LPS22_CTRL_REG1, self.buf[0]) - - def pressure(self): - if self.oneshot: - self._tigger_oneshot(1) - return ( - self._read_reg(_LPS22_PRESS_OUT_XL) + self._read_reg(_LPS22_PRESS_OUT_L, 16) * 256 - ) / 4096 - - def temperature(self): - if self.oneshot: - self._tigger_oneshot(2) - return self._int16(self._read_reg(_LPS22_TEMP_OUT_L, 16)) / 100 - - def altitude(self): - return ( - (((1013.25 / self.pressure()) ** (1 / 5.257)) - 1.0) - * (self.temperature() + 273.15) - / 0.0065 - ) diff --git a/drivers/lps22h/manifest.py b/drivers/lps22h/manifest.py deleted file mode 100644 index d30108d93dce0..0000000000000 --- a/drivers/lps22h/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("lps22h.py", opt=3) diff --git a/drivers/lsm6dsox/lsm6dsox.py b/drivers/lsm6dsox/lsm6dsox.py deleted file mode 100644 index 98e19fa4ca7b3..0000000000000 --- a/drivers/lsm6dsox/lsm6dsox.py +++ /dev/null @@ -1,271 +0,0 @@ -""" -LSM6DSOX STMicro driver for MicroPython based on LSM9DS1: -Source repo: https://github.com/hoihu/projects/tree/master/raspi-hat - -The MIT License (MIT) - -Copyright (c) 2021 Damien P. George -Copyright (c) 2021-2022 Ibrahim Abdelkader - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -Basic example usage: - -import time -from lsm6dsox import LSM6DSOX - -from machine import Pin, SPI, I2C -# Init in I2C mode. -lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) - -# Or init in SPI mode. -#lsm = LSM6DSOX(SPI(5), cs_pin=Pin(10)) - -while (True): - print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel())) - print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro())) - print("") - time.sleep_ms(100) -""" - -import array -from micropython import const - - -class LSM6DSOX: - _CTRL3_C = const(0x12) - _CTRL1_XL = const(0x10) - _CTRL8_XL = const(0x17) - _CTRL9_XL = const(0x18) - - _CTRL2_G = const(0x11) - _CTRL7_G = const(0x16) - - _OUTX_L_G = const(0x22) - _OUTX_L_XL = const(0x28) - _MLC_STATUS = const(0x38) - - _DEFAULT_ADDR = const(0x6A) - _WHO_AM_I_REG = const(0x0F) - - _FUNC_CFG_ACCESS = const(0x01) - _FUNC_CFG_BANK_USER = const(0) - _FUNC_CFG_BANK_HUB = const(1) - _FUNC_CFG_BANK_EMBED = const(2) - - _MLC0_SRC = const(0x70) - _MLC_INT1 = const(0x0D) - _TAP_CFG0 = const(0x56) - - _EMB_FUNC_EN_A = const(0x04) - _EMB_FUNC_EN_B = const(0x05) - - def __init__( - self, - bus, - cs_pin=None, - address=_DEFAULT_ADDR, - gyro_odr=104, - accel_odr=104, - gyro_scale=2000, - accel_scale=4, - ucf=None, - ): - """Initalizes Gyro and Accelerator. - accel_odr: (0, 1.6Hz, 3.33Hz, 6.66Hz, 12.5Hz, 26Hz, 52Hz, 104Hz, 208Hz, 416Hz, 888Hz) - gyro_odr: (0, 1.6Hz, 3.33Hz, 6.66Hz, 12.5Hz, 26Hz, 52Hz, 104Hz, 208Hz, 416Hz, 888Hz) - gyro_scale: (245dps, 500dps, 1000dps, 2000dps) - accel_scale: (+/-2g, +/-4g, +/-8g, +-16g) - ucf: MLC program to load. - """ - self.bus = bus - self.cs_pin = cs_pin - self.address = address - self._use_i2c = hasattr(self.bus, "readfrom_mem") - - if not self._use_i2c and cs_pin is None: - raise ValueError("A CS pin must be provided in SPI mode") - - # check the id of the Accelerometer/Gyro - if self.__read_reg(_WHO_AM_I_REG) != 108: - raise OSError("No LSM6DS device was found at address 0x%x" % (self.address)) - - # allocate scratch buffer for efficient conversions and memread op's - self.scratch_int = array.array("h", [0, 0, 0]) - - SCALE_GYRO = {250: 0, 500: 1, 1000: 2, 2000: 3} - SCALE_ACCEL = {2: 0, 4: 2, 8: 3, 16: 1} - # XL_HM_MODE = 0 by default. G_HM_MODE = 0 by default. - ODR = { - 0: 0x00, - 1.6: 0x08, - 3.33: 0x09, - 6.66: 0x0A, - 12.5: 0x01, - 26: 0x02, - 52: 0x03, - 104: 0x04, - 208: 0x05, - 416: 0x06, - 888: 0x07, - } - - gyro_odr = round(gyro_odr, 2) - accel_odr = round(accel_odr, 2) - - # Sanity checks - if not gyro_odr in ODR: - raise ValueError("Invalid sampling rate: %d" % accel_odr) - if not gyro_scale in SCALE_GYRO: - raise ValueError("invalid gyro scaling: %d" % gyro_scale) - if not accel_odr in ODR: - raise ValueError("Invalid sampling rate: %d" % accel_odr) - if not accel_scale in SCALE_ACCEL: - raise ValueError("invalid accelerometer scaling: %d" % accel_scale) - - # Soft-reset the device. - self.reset() - - # Load and configure MLC if UCF file is provided - if ucf != None: - self.load_mlc(ucf) - - # Set Gyroscope datarate and scale. - # Note output from LPF2 second filtering stage is selected. See Figure 18. - self.__write_reg(_CTRL1_XL, (ODR[accel_odr] << 4) | (SCALE_ACCEL[accel_scale] << 2) | 2) - - # Enable LPF2 and HPF fast-settling mode, ODR/4 - self.__write_reg(_CTRL8_XL, 0x09) - - # Set Gyroscope datarate and scale. - self.__write_reg(_CTRL2_G, (ODR[gyro_odr] << 4) | (SCALE_GYRO[gyro_scale] << 2) | 0) - - self.gyro_scale = 32768 / gyro_scale - self.accel_scale = 32768 / accel_scale - - def __read_reg(self, reg, size=1): - if self._use_i2c: - buf = self.bus.readfrom_mem(self.address, reg, size) - else: - try: - self.cs_pin(0) - self.bus.write(bytes([reg | 0x80])) - buf = self.bus.read(size) - finally: - self.cs_pin(1) - if size == 1: - return int(buf[0]) - return [int(x) for x in buf] - - def __write_reg(self, reg, val): - if self._use_i2c: - self.bus.writeto_mem(self.address, reg, bytes([val])) - else: - try: - self.cs_pin(0) - self.bus.write(bytes([reg, val])) - finally: - self.cs_pin(1) - - def __read_reg_into(self, reg, buf): - if self._use_i2c: - self.bus.readfrom_mem_into(self.address, reg, buf) - else: - try: - self.cs_pin(0) - self.bus.write(bytes([reg | 0x80])) - self.bus.readinto(buf) - finally: - self.cs_pin(1) - - def reset(self): - self.__write_reg(_CTRL3_C, self.__read_reg(_CTRL3_C) | 0x1) - for i in range(0, 10): - if (self.__read_reg(_CTRL3_C) & 0x01) == 0: - return - time.sleep_ms(10) - raise OSError("Failed to reset LSM6DS device.") - - def set_mem_bank(self, bank): - cfg = self.__read_reg(_FUNC_CFG_ACCESS) & 0x3F - self.__write_reg(_FUNC_CFG_ACCESS, cfg | (bank << 6)) - - def set_embedded_functions(self, enable, emb_ab=None): - self.set_mem_bank(_FUNC_CFG_BANK_EMBED) - if enable: - self.__write_reg(_EMB_FUNC_EN_A, emb_ab[0]) - self.__write_reg(_EMB_FUNC_EN_B, emb_ab[1]) - else: - emb_a = self.__read_reg(_EMB_FUNC_EN_A) - emb_b = self.__read_reg(_EMB_FUNC_EN_B) - self.__write_reg(_EMB_FUNC_EN_A, (emb_a & 0xC7)) - self.__write_reg(_EMB_FUNC_EN_B, (emb_b & 0xE6)) - emb_ab = (emb_a, emb_b) - - self.set_mem_bank(_FUNC_CFG_BANK_USER) - return emb_ab - - def load_mlc(self, ucf): - # Load MLC config from file - with open(ucf, "r") as ucf_file: - for l in ucf_file: - if l.startswith("Ac"): - v = [int(v, 16) for v in l.strip().split(" ")[1:3]] - self.__write_reg(v[0], v[1]) - - emb_ab = self.set_embedded_functions(False) - - # Disable I3C interface - self.__write_reg(_CTRL9_XL, self.__read_reg(_CTRL9_XL) | 0x01) - - # Enable Block Data Update - self.__write_reg(_CTRL3_C, self.__read_reg(_CTRL3_C) | 0x40) - - # Route signals on interrupt pin 1 - self.set_mem_bank(_FUNC_CFG_BANK_EMBED) - self.__write_reg(_MLC_INT1, self.__read_reg(_MLC_INT1) & 0x01) - self.set_mem_bank(_FUNC_CFG_BANK_USER) - - # Configure interrupt pin mode - self.__write_reg(_TAP_CFG0, self.__read_reg(_TAP_CFG0) | 0x41) - - self.set_embedded_functions(True, emb_ab) - - def read_mlc_output(self): - buf = None - if self.__read_reg(_MLC_STATUS) & 0x1: - self.__read_reg(0x1A, size=12) - self.set_mem_bank(_FUNC_CFG_BANK_EMBED) - buf = self.__read_reg(_MLC0_SRC, 8) - self.set_mem_bank(_FUNC_CFG_BANK_USER) - return buf - - def read_gyro(self): - """Returns gyroscope vector in degrees/sec.""" - mv = memoryview(self.scratch_int) - f = self.gyro_scale - self.__read_reg_into(_OUTX_L_G, mv) - return (mv[0] / f, mv[1] / f, mv[2] / f) - - def read_accel(self): - """Returns acceleration vector in gravity units (9.81m/s^2).""" - mv = memoryview(self.scratch_int) - f = self.accel_scale - self.__read_reg_into(_OUTX_L_XL, mv) - return (mv[0] / f, mv[1] / f, mv[2] / f) diff --git a/drivers/lsm6dsox/lsm6dsox_basic.py b/drivers/lsm6dsox/lsm6dsox_basic.py deleted file mode 100644 index 0ffe9e92b7689..0000000000000 --- a/drivers/lsm6dsox/lsm6dsox_basic.py +++ /dev/null @@ -1,15 +0,0 @@ -# LSM6DSOX Basic Example. -import time -from lsm6dsox import LSM6DSOX - -from machine import Pin, I2C - -lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) -# Or init in SPI mode. -# lsm = LSM6DSOX(SPI(5), cs_pin=Pin(10)) - -while True: - print("Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.read_accel())) - print("Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.read_gyro())) - print("") - time.sleep_ms(100) diff --git a/drivers/lsm6dsox/lsm6dsox_mlc.py b/drivers/lsm6dsox/lsm6dsox_mlc.py deleted file mode 100644 index 866498d0ce13e..0000000000000 --- a/drivers/lsm6dsox/lsm6dsox_mlc.py +++ /dev/null @@ -1,48 +0,0 @@ -# LSM6DSOX IMU MLC (Machine Learning Core) Example. -# Download the raw UCF file, copy to storage and reset. - -# NOTE: The pre-trained models (UCF files) for the examples can be found here: -# https://github.com/STMicroelectronics/STMems_Machine_Learning_Core/tree/master/application_examples/lsm6dsox - -import time -from lsm6dsox import LSM6DSOX -from machine import Pin, I2C - -INT_MODE = True # Run in interrupt mode. -INT_FLAG = False # Set True on interrupt. - - -def imu_int_handler(pin): - global INT_FLAG - INT_FLAG = True - - -if INT_MODE == True: - int_pin = Pin(24) - int_pin.irq(handler=imu_int_handler, trigger=Pin.IRQ_RISING) - -i2c = I2C(0, scl=Pin(13), sda=Pin(12)) - -# Vibration detection example -UCF_FILE = "lsm6dsox_vibration_monitoring.ucf" -UCF_LABELS = {0: "no vibration", 1: "low vibration", 2: "high vibration"} -# NOTE: Selected data rate and scale must match the MLC data rate and scale. -lsm = LSM6DSOX(i2c, gyro_odr=26, accel_odr=26, gyro_scale=2000, accel_scale=4, ucf=UCF_FILE) - -# Head gestures example -# UCF_FILE = "lsm6dsox_head_gestures.ucf" -# UCF_LABELS = {0:"Nod", 1:"Shake", 2:"Stationary", 3:"Swing", 4:"Walk"} -# NOTE: Selected data rate and scale must match the MLC data rate and scale. -# lsm = LSM6DSOX(i2c, gyro_odr=26, accel_odr=26, gyro_scale=250, accel_scale=2, ucf=UCF_FILE) - -print("MLC configured...") - -while True: - if INT_MODE: - if INT_FLAG: - INT_FLAG = False - print(UCF_LABELS[lsm.read_mlc_output()[0]]) - else: - buf = lsm.read_mlc_output() - if buf != None: - print(UCF_LABELS[buf[0]]) diff --git a/drivers/lsm6dsox/manifest.py b/drivers/lsm6dsox/manifest.py deleted file mode 100644 index 28f4b3565e9f7..0000000000000 --- a/drivers/lsm6dsox/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("lsm6dsox.py", opt=3) diff --git a/drivers/lsm9ds1/lsm9ds1.py b/drivers/lsm9ds1/lsm9ds1.py deleted file mode 100644 index 5d9942a7b3469..0000000000000 --- a/drivers/lsm9ds1/lsm9ds1.py +++ /dev/null @@ -1,189 +0,0 @@ -""" -The MIT License (MIT) - -Copyright (c) 2013, 2014 Damien P. George - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - - -LSM9DS1 - 9DOF inertial sensor of STMicro driver for MicroPython. -The sensor contains an accelerometer / gyroscope / magnetometer -Uses the internal FIFO to store up to 16 gyro/accel data, use the iter_accel_gyro generator to access it. - -Example usage: - -import time -from lsm9ds1 import LSM9DS1 -from machine import Pin, I2C - -lsm = LSM9DS1(I2C(1, scl=Pin(15), sda=Pin(14))) - -while (True): - #for g,a in lsm.iter_accel_gyro(): print(g,a) # using fifo - print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.accel())) - print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.magnet())) - print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.gyro())) - print("") - time.sleep_ms(100) -""" -import array - - -_WHO_AM_I = const(0xF) -_CTRL_REG1_G = const(0x10) -_INT_GEN_SRC_G = const(0x14) -_OUT_TEMP = const(0x15) -_OUT_G = const(0x18) -_CTRL_REG4_G = const(0x1E) -_STATUS_REG = const(0x27) -_OUT_XL = const(0x28) -_FIFO_CTRL_REG = const(0x2E) -_FIFO_SRC = const(0x2F) -_OFFSET_REG_X_M = const(0x05) -_CTRL_REG1_M = const(0x20) -_OUT_M = const(0x28) -_SCALE_GYRO = const(((245, 0), (500, 1), (2000, 3))) -_SCALE_ACCEL = const(((2, 0), (4, 2), (8, 3), (16, 1))) - - -class LSM9DS1: - def __init__(self, i2c, address_gyro=0x6B, address_magnet=0x1E): - self.i2c = i2c - self.address_gyro = address_gyro - self.address_magnet = address_magnet - # check id's of accelerometer/gyro and magnetometer - if (self.magent_id() != b"=") or (self.gyro_id() != b"h"): - raise OSError( - "Invalid LSM9DS1 device, using address {}/{}".format(address_gyro, address_magnet) - ) - # allocate scratch buffer for efficient conversions and memread op's - self.scratch = array.array("B", [0, 0, 0, 0, 0, 0]) - self.scratch_int = array.array("h", [0, 0, 0]) - self.init_gyro_accel() - self.init_magnetometer() - - def init_gyro_accel(self, sample_rate=6, scale_gyro=0, scale_accel=0): - """Initalizes Gyro and Accelerator. - sample rate: 0-6 (off, 14.9Hz, 59.5Hz, 119Hz, 238Hz, 476Hz, 952Hz) - scale_gyro: 0-2 (245dps, 500dps, 2000dps ) - scale_accel: 0-3 (+/-2g, +/-4g, +/-8g, +-16g) - """ - assert sample_rate <= 6, "invalid sampling rate: %d" % sample_rate - assert scale_gyro <= 2, "invalid gyro scaling: %d" % scale_gyro - assert scale_accel <= 3, "invalid accelerometer scaling: %d" % scale_accel - - i2c = self.i2c - addr = self.address_gyro - mv = memoryview(self.scratch) - # angular control registers 1-3 / Orientation - mv[0] = ((sample_rate & 0x07) << 5) | ((_SCALE_GYRO[scale_gyro][1] & 0x3) << 3) - mv[1:4] = b"\x00\x00\x00" - i2c.writeto_mem(addr, _CTRL_REG1_G, mv[:5]) - # ctrl4 - enable x,y,z, outputs, no irq latching, no 4D - # ctrl5 - enable all axes, no decimation - # ctrl6 - set scaling and sample rate of accel - # ctrl7,8 - leave at default values - # ctrl9 - FIFO enabled - mv[0] = mv[1] = 0x38 - mv[2] = ((sample_rate & 7) << 5) | ((_SCALE_ACCEL[scale_accel][1] & 0x3) << 3) - mv[3] = 0x00 - mv[4] = 0x4 - mv[5] = 0x2 - i2c.writeto_mem(addr, _CTRL_REG4_G, mv[:6]) - - # fifo: use continous mode (overwrite old data if overflow) - i2c.writeto_mem(addr, _FIFO_CTRL_REG, b"\x00") - i2c.writeto_mem(addr, _FIFO_CTRL_REG, b"\xc0") - - self.scale_gyro = 32768 / _SCALE_GYRO[scale_gyro][0] - self.scale_accel = 32768 / _SCALE_ACCEL[scale_accel][0] - - def init_magnetometer(self, sample_rate=7, scale_magnet=0): - """ - sample rates = 0-7 (0.625, 1.25, 2.5, 5, 10, 20, 40, 80Hz) - scaling = 0-3 (+/-4, +/-8, +/-12, +/-16 Gauss) - """ - assert sample_rate < 8, "invalid sample rate: %d (0-7)" % sample_rate - assert scale_magnet < 4, "invalid scaling: %d (0-3)" % scale_magnet - i2c = self.i2c - addr = self.address_magnet - mv = memoryview(self.scratch) - mv[0] = 0x40 | (sample_rate << 2) # ctrl1: high performance mode - mv[1] = scale_magnet << 5 # ctrl2: scale, normal mode, no reset - mv[2] = 0x00 # ctrl3: continous conversion, no low power, I2C - mv[3] = 0x08 # ctrl4: high performance z-axis - mv[4] = 0x00 # ctr5: no fast read, no block update - i2c.writeto_mem(addr, _CTRL_REG1_M, mv[:5]) - self.scale_factor_magnet = 32768 / ((scale_magnet + 1) * 4) - - def calibrate_magnet(self, offset): - """ - offset is a magnet vecor that will be substracted by the magnetometer - for each measurement. It is written to the magnetometer's offset register - """ - offset = [int(i * self.scale_factor_magnet) for i in offset] - mv = memoryview(self.scratch) - mv[0] = offset[0] & 0xFF - mv[1] = offset[0] >> 8 - mv[2] = offset[1] & 0xFF - mv[3] = offset[1] >> 8 - mv[4] = offset[2] & 0xFF - mv[5] = offset[2] >> 8 - self.i2c.writeto_mem(self.address_magnet, _OFFSET_REG_X_M, mv[:6]) - - def gyro_id(self): - return self.i2c.readfrom_mem(self.address_gyro, _WHO_AM_I, 1) - - def magent_id(self): - return self.i2c.readfrom_mem(self.address_magnet, _WHO_AM_I, 1) - - def magnet(self): - """Returns magnetometer vector in gauss. - raw_values: if True, the non-scaled adc values are returned - """ - mv = memoryview(self.scratch_int) - f = self.scale_factor_magnet - self.i2c.readfrom_mem_into(self.address_magnet, _OUT_M | 0x80, mv) - return (mv[0] / f, mv[1] / f, mv[2] / f) - - def gyro(self): - """Returns gyroscope vector in degrees/sec.""" - mv = memoryview(self.scratch_int) - f = self.scale_gyro - self.i2c.readfrom_mem_into(self.address_gyro, _OUT_G | 0x80, mv) - return (mv[0] / f, mv[1] / f, mv[2] / f) - - def accel(self): - """Returns acceleration vector in gravity units (9.81m/s^2).""" - mv = memoryview(self.scratch_int) - f = self.scale_accel - self.i2c.readfrom_mem_into(self.address_gyro, _OUT_XL | 0x80, mv) - return (mv[0] / f, mv[1] / f, mv[2] / f) - - def iter_accel_gyro(self): - """A generator that returns tuples of (gyro,accelerometer) data from the fifo.""" - while True: - fifo_state = int.from_bytes( - self.i2c.readfrom_mem(self.address_gyro, _FIFO_SRC, 1), "big" - ) - if fifo_state & 0x3F: - # print("Available samples=%d" % (fifo_state & 0x1f)) - yield self.gyro(), self.accel() - else: - break diff --git a/drivers/lsm9ds1/manifest.py b/drivers/lsm9ds1/manifest.py deleted file mode 100644 index 6779362de7ee5..0000000000000 --- a/drivers/lsm9ds1/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("lsm9ds1.py", opt=3) diff --git a/drivers/neopixel/manifest.py b/drivers/neopixel/manifest.py deleted file mode 100644 index 561d19574af32..0000000000000 --- a/drivers/neopixel/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("neopixel.py", opt=3) diff --git a/drivers/neopixel/neopixel.py b/drivers/neopixel/neopixel.py deleted file mode 100644 index caa12dc845361..0000000000000 --- a/drivers/neopixel/neopixel.py +++ /dev/null @@ -1,50 +0,0 @@ -# NeoPixel driver for MicroPython -# MIT license; Copyright (c) 2016 Damien P. George, 2021 Jim Mussared - -from machine import bitstream - - -class NeoPixel: - # G R B W - ORDER = (1, 0, 2, 3) - - def __init__(self, pin, n, bpp=3, timing=1): - self.pin = pin - self.n = n - self.bpp = bpp - self.buf = bytearray(n * bpp) - self.pin.init(pin.OUT) - # Timing arg can either be 1 for 800kHz or 0 for 400kHz, - # or a user-specified timing ns tuple (high_0, low_0, high_1, low_1). - self.timing = ( - ((400, 850, 800, 450) if timing else (800, 1700, 1600, 900)) - if isinstance(timing, int) - else timing - ) - - def __len__(self): - return self.n - - def __setitem__(self, i, v): - offset = i * self.bpp - for i in range(self.bpp): - self.buf[offset + self.ORDER[i]] = v[i] - - def __getitem__(self, i): - offset = i * self.bpp - return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp)) - - def fill(self, v): - b = self.buf - l = len(self.buf) - bpp = self.bpp - for i in range(bpp): - c = v[i] - j = self.ORDER[i] - while j < l: - b[j] = c - j += bpp - - def write(self): - # BITSTREAM_TYPE_HIGH_LOW = 0 - bitstream(self.pin, 0, self.timing, self.buf) diff --git a/drivers/nrf24l01/manifest.py b/drivers/nrf24l01/manifest.py deleted file mode 100644 index babdb7a52a953..0000000000000 --- a/drivers/nrf24l01/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("nrf24l01.py", opt=3) diff --git a/drivers/nrf24l01/nrf24l01.py b/drivers/nrf24l01/nrf24l01.py deleted file mode 100644 index 76d55312f8568..0000000000000 --- a/drivers/nrf24l01/nrf24l01.py +++ /dev/null @@ -1,252 +0,0 @@ -"""NRF24L01 driver for MicroPython -""" - -from micropython import const -import utime - -# nRF24L01+ registers -CONFIG = const(0x00) -EN_RXADDR = const(0x02) -SETUP_AW = const(0x03) -SETUP_RETR = const(0x04) -RF_CH = const(0x05) -RF_SETUP = const(0x06) -STATUS = const(0x07) -RX_ADDR_P0 = const(0x0A) -TX_ADDR = const(0x10) -RX_PW_P0 = const(0x11) -FIFO_STATUS = const(0x17) -DYNPD = const(0x1C) - -# CONFIG register -EN_CRC = const(0x08) # enable CRC -CRCO = const(0x04) # CRC encoding scheme; 0=1 byte, 1=2 bytes -PWR_UP = const(0x02) # 1=power up, 0=power down -PRIM_RX = const(0x01) # RX/TX control; 0=PTX, 1=PRX - -# RF_SETUP register -POWER_0 = const(0x00) # -18 dBm -POWER_1 = const(0x02) # -12 dBm -POWER_2 = const(0x04) # -6 dBm -POWER_3 = const(0x06) # 0 dBm -SPEED_1M = const(0x00) -SPEED_2M = const(0x08) -SPEED_250K = const(0x20) - -# STATUS register -RX_DR = const(0x40) # RX data ready; write 1 to clear -TX_DS = const(0x20) # TX data sent; write 1 to clear -MAX_RT = const(0x10) # max retransmits reached; write 1 to clear - -# FIFO_STATUS register -RX_EMPTY = const(0x01) # 1 if RX FIFO is empty - -# constants for instructions -R_RX_PL_WID = const(0x60) # read RX payload width -R_RX_PAYLOAD = const(0x61) # read RX payload -W_TX_PAYLOAD = const(0xA0) # write TX payload -FLUSH_TX = const(0xE1) # flush TX FIFO -FLUSH_RX = const(0xE2) # flush RX FIFO -NOP = const(0xFF) # use to read STATUS register - - -class NRF24L01: - def __init__(self, spi, cs, ce, channel=46, payload_size=16): - assert payload_size <= 32 - - self.buf = bytearray(1) - - # store the pins - self.spi = spi - self.cs = cs - self.ce = ce - - # init the SPI bus and pins - self.init_spi(4000000) - - # reset everything - ce.init(ce.OUT, value=0) - cs.init(cs.OUT, value=1) - - self.payload_size = payload_size - self.pipe0_read_addr = None - utime.sleep_ms(5) - - # set address width to 5 bytes and check for device present - self.reg_write(SETUP_AW, 0b11) - if self.reg_read(SETUP_AW) != 0b11: - raise OSError("nRF24L01+ Hardware not responding") - - # disable dynamic payloads - self.reg_write(DYNPD, 0) - - # auto retransmit delay: 1750us - # auto retransmit count: 8 - self.reg_write(SETUP_RETR, (6 << 4) | 8) - - # set rf power and speed - self.set_power_speed(POWER_3, SPEED_250K) # Best for point to point links - - # init CRC - self.set_crc(2) - - # clear status flags - self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT) - - # set channel - self.set_channel(channel) - - # flush buffers - self.flush_rx() - self.flush_tx() - - def init_spi(self, baudrate): - try: - master = self.spi.MASTER - except AttributeError: - self.spi.init(baudrate=baudrate, polarity=0, phase=0) - else: - self.spi.init(master, baudrate=baudrate, polarity=0, phase=0) - - def reg_read(self, reg): - self.cs(0) - self.spi.readinto(self.buf, reg) - self.spi.readinto(self.buf) - self.cs(1) - return self.buf[0] - - def reg_write_bytes(self, reg, buf): - self.cs(0) - self.spi.readinto(self.buf, 0x20 | reg) - self.spi.write(buf) - self.cs(1) - return self.buf[0] - - def reg_write(self, reg, value): - self.cs(0) - self.spi.readinto(self.buf, 0x20 | reg) - ret = self.buf[0] - self.spi.readinto(self.buf, value) - self.cs(1) - return ret - - def flush_rx(self): - self.cs(0) - self.spi.readinto(self.buf, FLUSH_RX) - self.cs(1) - - def flush_tx(self): - self.cs(0) - self.spi.readinto(self.buf, FLUSH_TX) - self.cs(1) - - # power is one of POWER_x defines; speed is one of SPEED_x defines - def set_power_speed(self, power, speed): - setup = self.reg_read(RF_SETUP) & 0b11010001 - self.reg_write(RF_SETUP, setup | power | speed) - - # length in bytes: 0, 1 or 2 - def set_crc(self, length): - config = self.reg_read(CONFIG) & ~(CRCO | EN_CRC) - if length == 0: - pass - elif length == 1: - config |= EN_CRC - else: - config |= EN_CRC | CRCO - self.reg_write(CONFIG, config) - - def set_channel(self, channel): - self.reg_write(RF_CH, min(channel, 125)) - - # address should be a bytes object 5 bytes long - def open_tx_pipe(self, address): - assert len(address) == 5 - self.reg_write_bytes(RX_ADDR_P0, address) - self.reg_write_bytes(TX_ADDR, address) - self.reg_write(RX_PW_P0, self.payload_size) - - # address should be a bytes object 5 bytes long - # pipe 0 and 1 have 5 byte address - # pipes 2-5 use same 4 most-significant bytes as pipe 1, plus 1 extra byte - def open_rx_pipe(self, pipe_id, address): - assert len(address) == 5 - assert 0 <= pipe_id <= 5 - if pipe_id == 0: - self.pipe0_read_addr = address - if pipe_id < 2: - self.reg_write_bytes(RX_ADDR_P0 + pipe_id, address) - else: - self.reg_write(RX_ADDR_P0 + pipe_id, address[0]) - self.reg_write(RX_PW_P0 + pipe_id, self.payload_size) - self.reg_write(EN_RXADDR, self.reg_read(EN_RXADDR) | (1 << pipe_id)) - - def start_listening(self): - self.reg_write(CONFIG, self.reg_read(CONFIG) | PWR_UP | PRIM_RX) - self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT) - - if self.pipe0_read_addr is not None: - self.reg_write_bytes(RX_ADDR_P0, self.pipe0_read_addr) - - self.flush_rx() - self.flush_tx() - self.ce(1) - utime.sleep_us(130) - - def stop_listening(self): - self.ce(0) - self.flush_tx() - self.flush_rx() - - # returns True if any data available to recv - def any(self): - return not bool(self.reg_read(FIFO_STATUS) & RX_EMPTY) - - def recv(self): - # get the data - self.cs(0) - self.spi.readinto(self.buf, R_RX_PAYLOAD) - buf = self.spi.read(self.payload_size) - self.cs(1) - # clear RX ready flag - self.reg_write(STATUS, RX_DR) - - return buf - - # blocking wait for tx complete - def send(self, buf, timeout=500): - self.send_start(buf) - start = utime.ticks_ms() - result = None - while result is None and utime.ticks_diff(utime.ticks_ms(), start) < timeout: - result = self.send_done() # 1 == success, 2 == fail - if result == 2: - raise OSError("send failed") - - # non-blocking tx - def send_start(self, buf): - # power up - self.reg_write(CONFIG, (self.reg_read(CONFIG) | PWR_UP) & ~PRIM_RX) - utime.sleep_us(150) - # send the data - self.cs(0) - self.spi.readinto(self.buf, W_TX_PAYLOAD) - self.spi.write(buf) - if len(buf) < self.payload_size: - self.spi.write(b"\x00" * (self.payload_size - len(buf))) # pad out data - self.cs(1) - - # enable the chip so it can send the data - self.ce(1) - utime.sleep_us(15) # needs to be >10us - self.ce(0) - - # returns None if send still in progress, 1 for success, 2 for fail - def send_done(self): - if not (self.reg_read(STATUS) & (TX_DS | MAX_RT)): - return None # tx not finished - - # either finished or failed: get and clear status flags, power down - status = self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT) - self.reg_write(CONFIG, self.reg_read(CONFIG) & ~PWR_UP) - return 1 if status & TX_DS else 2 diff --git a/drivers/nrf24l01/nrf24l01test.py b/drivers/nrf24l01/nrf24l01test.py deleted file mode 100644 index 56bdb6e26eb9c..0000000000000 --- a/drivers/nrf24l01/nrf24l01test.py +++ /dev/null @@ -1,150 +0,0 @@ -"""Test for nrf24l01 module. Portable between MicroPython targets.""" - -import usys -import ustruct as struct -import utime -from machine import Pin, SPI -from nrf24l01 import NRF24L01 -from micropython import const - -# Slave pause between receiving data and checking for further packets. -_RX_POLL_DELAY = const(15) -# Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before -# transmitting to allow the (remote) master time to get into receive mode. The -# master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266. -_SLAVE_SEND_DELAY = const(10) - -if usys.platform == "pyboard": - cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"} -elif usys.platform == "esp8266": # Hardware SPI - cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5} -elif usys.platform == "esp32": # Software SPI - cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27} -else: - raise ValueError("Unsupported platform {}".format(usys.platform)) - -# Addresses are in little-endian format. They correspond to big-endian -# 0xf0f0f0f0e1, 0xf0f0f0f0d2 -pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0") - - -def master(): - csn = Pin(cfg["csn"], mode=Pin.OUT, value=1) - ce = Pin(cfg["ce"], mode=Pin.OUT, value=0) - if cfg["spi"] == -1: - spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"])) - nrf = NRF24L01(spi, csn, ce, payload_size=8) - else: - nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8) - - nrf.open_tx_pipe(pipes[0]) - nrf.open_rx_pipe(1, pipes[1]) - nrf.start_listening() - - num_needed = 16 - num_successes = 0 - num_failures = 0 - led_state = 0 - - print("NRF24L01 master mode, sending %d packets..." % num_needed) - - while num_successes < num_needed and num_failures < num_needed: - # stop listening and send packet - nrf.stop_listening() - millis = utime.ticks_ms() - led_state = max(1, (led_state << 1) & 0x0F) - print("sending:", millis, led_state) - try: - nrf.send(struct.pack("ii", millis, led_state)) - except OSError: - pass - - # start listening again - nrf.start_listening() - - # wait for response, with 250ms timeout - start_time = utime.ticks_ms() - timeout = False - while not nrf.any() and not timeout: - if utime.ticks_diff(utime.ticks_ms(), start_time) > 250: - timeout = True - - if timeout: - print("failed, response timed out") - num_failures += 1 - - else: - # recv packet - (got_millis,) = struct.unpack("i", nrf.recv()) - - # print response and round-trip delay - print( - "got response:", - got_millis, - "(delay", - utime.ticks_diff(utime.ticks_ms(), got_millis), - "ms)", - ) - num_successes += 1 - - # delay then loop - utime.sleep_ms(250) - - print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures)) - - -def slave(): - csn = Pin(cfg["csn"], mode=Pin.OUT, value=1) - ce = Pin(cfg["ce"], mode=Pin.OUT, value=0) - if cfg["spi"] == -1: - spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"])) - nrf = NRF24L01(spi, csn, ce, payload_size=8) - else: - nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8) - - nrf.open_tx_pipe(pipes[1]) - nrf.open_rx_pipe(1, pipes[0]) - nrf.start_listening() - - print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)") - - while True: - if nrf.any(): - while nrf.any(): - buf = nrf.recv() - millis, led_state = struct.unpack("ii", buf) - print("received:", millis, led_state) - for led in leds: - if led_state & 1: - led.on() - else: - led.off() - led_state >>= 1 - utime.sleep_ms(_RX_POLL_DELAY) - - # Give master time to get into receive mode. - utime.sleep_ms(_SLAVE_SEND_DELAY) - nrf.stop_listening() - try: - nrf.send(struct.pack("i", millis)) - except OSError: - pass - print("sent response") - nrf.start_listening() - - -try: - import pyb - - leds = [pyb.LED(i + 1) for i in range(4)] -except: - leds = [] - -print("NRF24L01 test module loaded") -print("NRF24L01 pinout for test:") -print(" CE on", cfg["ce"]) -print(" CSN on", cfg["csn"]) -print(" SCK on", cfg["sck"]) -print(" MISO on", cfg["miso"]) -print(" MOSI on", cfg["mosi"]) -print("run nrf24l01test.slave() on slave, then nrf24l01test.master() on master") diff --git a/drivers/onewire/ds18x20.py b/drivers/onewire/ds18x20.py deleted file mode 100644 index ad2d9f52cdd00..0000000000000 --- a/drivers/onewire/ds18x20.py +++ /dev/null @@ -1,52 +0,0 @@ -# DS18x20 temperature sensor driver for MicroPython. -# MIT license; Copyright (c) 2016 Damien P. George - -from micropython import const - -_CONVERT = const(0x44) -_RD_SCRATCH = const(0xBE) -_WR_SCRATCH = const(0x4E) - - -class DS18X20: - def __init__(self, onewire): - self.ow = onewire - self.buf = bytearray(9) - - def scan(self): - return [rom for rom in self.ow.scan() if rom[0] in (0x10, 0x22, 0x28)] - - def convert_temp(self): - self.ow.reset(True) - self.ow.writebyte(self.ow.SKIP_ROM) - self.ow.writebyte(_CONVERT) - - def read_scratch(self, rom): - self.ow.reset(True) - self.ow.select_rom(rom) - self.ow.writebyte(_RD_SCRATCH) - self.ow.readinto(self.buf) - if self.ow.crc8(self.buf): - raise Exception("CRC error") - return self.buf - - def write_scratch(self, rom, buf): - self.ow.reset(True) - self.ow.select_rom(rom) - self.ow.writebyte(_WR_SCRATCH) - self.ow.write(buf) - - def read_temp(self, rom): - buf = self.read_scratch(rom) - if rom[0] == 0x10: - if buf[1]: - t = buf[0] >> 1 | 0x80 - t = -((~t + 1) & 0xFF) - else: - t = buf[0] >> 1 - return t - 0.25 + (buf[7] - buf[6]) / buf[7] - else: - t = buf[1] << 8 | buf[0] - if t & 0x8000: # sign bit set - t = -((t ^ 0xFFFF) + 1) - return t / 16 diff --git a/drivers/onewire/manifest.py b/drivers/onewire/manifest.py deleted file mode 100644 index f500a65d780b8..0000000000000 --- a/drivers/onewire/manifest.py +++ /dev/null @@ -1,6 +0,0 @@ -options.defaults(ds18x20=False) - -module("onewire.py", opt=3) - -if options.ds18x20: - module("ds18x20.py", opt=3) diff --git a/drivers/onewire/onewire.py b/drivers/onewire/onewire.py deleted file mode 100644 index 4c6da741c74ce..0000000000000 --- a/drivers/onewire/onewire.py +++ /dev/null @@ -1,92 +0,0 @@ -# 1-Wire driver for MicroPython -# MIT license; Copyright (c) 2016 Damien P. George - -import _onewire as _ow - - -class OneWireError(Exception): - pass - - -class OneWire: - SEARCH_ROM = 0xF0 - MATCH_ROM = 0x55 - SKIP_ROM = 0xCC - - def __init__(self, pin): - self.pin = pin - self.pin.init(pin.OPEN_DRAIN, pin.PULL_UP) - - def reset(self, required=False): - reset = _ow.reset(self.pin) - if required and not reset: - raise OneWireError - return reset - - def readbit(self): - return _ow.readbit(self.pin) - - def readbyte(self): - return _ow.readbyte(self.pin) - - def readinto(self, buf): - for i in range(len(buf)): - buf[i] = _ow.readbyte(self.pin) - - def writebit(self, value): - return _ow.writebit(self.pin, value) - - def writebyte(self, value): - return _ow.writebyte(self.pin, value) - - def write(self, buf): - for b in buf: - _ow.writebyte(self.pin, b) - - def select_rom(self, rom): - self.reset() - self.writebyte(self.MATCH_ROM) - self.write(rom) - - def scan(self): - devices = [] - diff = 65 - rom = False - for i in range(0xFF): - rom, diff = self._search_rom(rom, diff) - if rom: - devices += [rom] - if diff == 0: - break - return devices - - def _search_rom(self, l_rom, diff): - if not self.reset(): - return None, 0 - self.writebyte(self.SEARCH_ROM) - if not l_rom: - l_rom = bytearray(8) - rom = bytearray(8) - next_diff = 0 - i = 64 - for byte in range(8): - r_b = 0 - for bit in range(8): - b = self.readbit() - if self.readbit(): - if b: # there are no devices or there is an error on the bus - return None, 0 - else: - if not b: # collision, two devices with different bit meaning - if diff > i or ((l_rom[byte] & (1 << bit)) and diff != i): - b = 1 - next_diff = i - self.writebit(b) - if b: - r_b |= 1 << bit - i -= 1 - rom[byte] = r_b - return rom, next_diff - - def crc8(self, data): - return _ow.crc8(data) diff --git a/drivers/sdcard/manifest.py b/drivers/sdcard/manifest.py deleted file mode 100644 index e584b97d9ca6f..0000000000000 --- a/drivers/sdcard/manifest.py +++ /dev/null @@ -1 +0,0 @@ -module("sdcard.py", opt=3) diff --git a/drivers/sdcard/sdcard.py b/drivers/sdcard/sdcard.py deleted file mode 100644 index df28bd9534482..0000000000000 --- a/drivers/sdcard/sdcard.py +++ /dev/null @@ -1,299 +0,0 @@ -""" -MicroPython driver for SD cards using SPI bus. - -Requires an SPI bus and a CS pin. Provides readblocks and writeblocks -methods so the device can be mounted as a filesystem. - -Example usage on pyboard: - - import pyb, sdcard, os - sd = sdcard.SDCard(pyb.SPI(1), pyb.Pin.board.X5) - pyb.mount(sd, '/sd2') - os.listdir('/') - -Example usage on ESP8266: - - import machine, sdcard, os - sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15)) - os.mount(sd, '/sd') - os.listdir('/') - -""" - -from micropython import const -import time - - -_CMD_TIMEOUT = const(100) - -_R1_IDLE_STATE = const(1 << 0) -# R1_ERASE_RESET = const(1 << 1) -_R1_ILLEGAL_COMMAND = const(1 << 2) -# R1_COM_CRC_ERROR = const(1 << 3) -# R1_ERASE_SEQUENCE_ERROR = const(1 << 4) -# R1_ADDRESS_ERROR = const(1 << 5) -# R1_PARAMETER_ERROR = const(1 << 6) -_TOKEN_CMD25 = const(0xFC) -_TOKEN_STOP_TRAN = const(0xFD) -_TOKEN_DATA = const(0xFE) - - -class SDCard: - def __init__(self, spi, cs, baudrate=1320000): - self.spi = spi - self.cs = cs - - self.cmdbuf = bytearray(6) - self.dummybuf = bytearray(512) - self.tokenbuf = bytearray(1) - for i in range(512): - self.dummybuf[i] = 0xFF - self.dummybuf_memoryview = memoryview(self.dummybuf) - - # initialise the card - self.init_card(baudrate) - - def init_spi(self, baudrate): - try: - master = self.spi.MASTER - except AttributeError: - # on ESP8266 - self.spi.init(baudrate=baudrate, phase=0, polarity=0) - else: - # on pyboard - self.spi.init(master, baudrate=baudrate, phase=0, polarity=0) - - def init_card(self, baudrate): - - # init CS pin - self.cs.init(self.cs.OUT, value=1) - - # init SPI bus; use low data rate for initialisation - self.init_spi(100000) - - # clock card at least 100 cycles with cs high - for i in range(16): - self.spi.write(b"\xff") - - # CMD0: init card; should return _R1_IDLE_STATE (allow 5 attempts) - for _ in range(5): - if self.cmd(0, 0, 0x95) == _R1_IDLE_STATE: - break - else: - raise OSError("no SD card") - - # CMD8: determine card version - r = self.cmd(8, 0x01AA, 0x87, 4) - if r == _R1_IDLE_STATE: - self.init_card_v2() - elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND): - self.init_card_v1() - else: - raise OSError("couldn't determine SD card version") - - # get the number of sectors - # CMD9: response R2 (R1 byte + 16-byte block read) - if self.cmd(9, 0, 0, 0, False) != 0: - raise OSError("no response from SD card") - csd = bytearray(16) - self.readinto(csd) - if csd[0] & 0xC0 == 0x40: # CSD version 2.0 - self.sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024 - elif csd[0] & 0xC0 == 0x00: # CSD version 1.0 (old, <=2GB) - c_size = (csd[6] & 0b11) << 10 | csd[7] << 2 | csd[8] >> 6 - c_size_mult = (csd[9] & 0b11) << 1 | csd[10] >> 7 - read_bl_len = csd[5] & 0b1111 - capacity = (c_size + 1) * (2 ** (c_size_mult + 2)) * (2**read_bl_len) - self.sectors = capacity // 512 - else: - raise OSError("SD card CSD format not supported") - # print('sectors', self.sectors) - - # CMD16: set block length to 512 bytes - if self.cmd(16, 512, 0) != 0: - raise OSError("can't set 512 block size") - - # set to high data rate now that it's initialised - self.init_spi(baudrate) - - def init_card_v1(self): - for i in range(_CMD_TIMEOUT): - time.sleep_ms(50) - self.cmd(55, 0, 0) - if self.cmd(41, 0, 0) == 0: - # SDSC card, uses byte addressing in read/write/erase commands - self.cdv = 512 - # print("[SDCard] v1 card") - return - raise OSError("timeout waiting for v1 card") - - def init_card_v2(self): - for i in range(_CMD_TIMEOUT): - time.sleep_ms(50) - self.cmd(58, 0, 0, 4) - self.cmd(55, 0, 0) - if self.cmd(41, 0x40000000, 0) == 0: - self.cmd(58, 0, 0, -4) # 4-byte response, negative means keep the first byte - ocr = self.tokenbuf[0] # get first byte of response, which is OCR - if not ocr & 0x40: - # SDSC card, uses byte addressing in read/write/erase commands - self.cdv = 512 - else: - # SDHC/SDXC card, uses block addressing in read/write/erase commands - self.cdv = 1 - # print("[SDCard] v2 card") - return - raise OSError("timeout waiting for v2 card") - - def cmd(self, cmd, arg, crc, final=0, release=True, skip1=False): - self.cs(0) - - # create and send the command - buf = self.cmdbuf - buf[0] = 0x40 | cmd - buf[1] = arg >> 24 - buf[2] = arg >> 16 - buf[3] = arg >> 8 - buf[4] = arg - buf[5] = crc - self.spi.write(buf) - - if skip1: - self.spi.readinto(self.tokenbuf, 0xFF) - - # wait for the response (response[7] == 0) - for i in range(_CMD_TIMEOUT): - self.spi.readinto(self.tokenbuf, 0xFF) - response = self.tokenbuf[0] - if not (response & 0x80): - # this could be a big-endian integer that we are getting here - # if final<0 then store the first byte to tokenbuf and discard the rest - if final < 0: - self.spi.readinto(self.tokenbuf, 0xFF) - final = -1 - final - for j in range(final): - self.spi.write(b"\xff") - if release: - self.cs(1) - self.spi.write(b"\xff") - return response - - # timeout - self.cs(1) - self.spi.write(b"\xff") - return -1 - - def readinto(self, buf): - self.cs(0) - - # read until start byte (0xff) - for i in range(_CMD_TIMEOUT): - self.spi.readinto(self.tokenbuf, 0xFF) - if self.tokenbuf[0] == _TOKEN_DATA: - break - time.sleep_ms(1) - else: - self.cs(1) - raise OSError("timeout waiting for response") - - # read data - mv = self.dummybuf_memoryview - if len(buf) != len(mv): - mv = mv[: len(buf)] - self.spi.write_readinto(mv, buf) - - # read checksum - self.spi.write(b"\xff") - self.spi.write(b"\xff") - - self.cs(1) - self.spi.write(b"\xff") - - def write(self, token, buf): - self.cs(0) - - # send: start of block, data, checksum - self.spi.read(1, token) - self.spi.write(buf) - self.spi.write(b"\xff") - self.spi.write(b"\xff") - - # check the response - if (self.spi.read(1, 0xFF)[0] & 0x1F) != 0x05: - self.cs(1) - self.spi.write(b"\xff") - return - - # wait for write to finish - while self.spi.read(1, 0xFF)[0] == 0: - pass - - self.cs(1) - self.spi.write(b"\xff") - - def write_token(self, token): - self.cs(0) - self.spi.read(1, token) - self.spi.write(b"\xff") - # wait for write to finish - while self.spi.read(1, 0xFF)[0] == 0x00: - pass - - self.cs(1) - self.spi.write(b"\xff") - - def readblocks(self, block_num, buf): - nblocks = len(buf) // 512 - assert nblocks and not len(buf) % 512, "Buffer length is invalid" - if nblocks == 1: - # CMD17: set read address for single block - if self.cmd(17, block_num * self.cdv, 0, release=False) != 0: - # release the card - self.cs(1) - raise OSError(5) # EIO - # receive the data and release card - self.readinto(buf) - else: - # CMD18: set read address for multiple blocks - if self.cmd(18, block_num * self.cdv, 0, release=False) != 0: - # release the card - self.cs(1) - raise OSError(5) # EIO - offset = 0 - mv = memoryview(buf) - while nblocks: - # receive the data and release card - self.readinto(mv[offset : offset + 512]) - offset += 512 - nblocks -= 1 - if self.cmd(12, 0, 0xFF, skip1=True): - raise OSError(5) # EIO - - def writeblocks(self, block_num, buf): - nblocks, err = divmod(len(buf), 512) - assert nblocks and not err, "Buffer length is invalid" - if nblocks == 1: - # CMD24: set write address for single block - if self.cmd(24, block_num * self.cdv, 0) != 0: - raise OSError(5) # EIO - - # send the data - self.write(_TOKEN_DATA, buf) - else: - # CMD25: set write address for first block - if self.cmd(25, block_num * self.cdv, 0) != 0: - raise OSError(5) # EIO - # send the data - offset = 0 - mv = memoryview(buf) - while nblocks: - self.write(_TOKEN_CMD25, mv[offset : offset + 512]) - offset += 512 - nblocks -= 1 - self.write_token(_TOKEN_STOP_TRAN) - - def ioctl(self, op, arg): - if op == 4: # get number of blocks - return self.sectors - if op == 5: # get block size in bytes - return 512 diff --git a/drivers/sdcard/sdtest.py b/drivers/sdcard/sdtest.py deleted file mode 100644 index 018ef7c64aa19..0000000000000 --- a/drivers/sdcard/sdtest.py +++ /dev/null @@ -1,61 +0,0 @@ -# Test for sdcard block protocol -# Peter hinch 30th Jan 2016 -import os, sdcard, machine - - -def sdtest(): - spi = machine.SPI(1) - spi.init() # Ensure right baudrate - sd = sdcard.SDCard(spi, machine.Pin.board.X21) # Compatible with PCB - vfs = os.VfsFat(sd) - os.mount(vfs, "/fc") - print("Filesystem check") - print(os.listdir("/fc")) - - line = "abcdefghijklmnopqrstuvwxyz\n" - lines = line * 200 # 5400 chars - short = "1234567890\n" - - fn = "/fc/rats.txt" - print() - print("Multiple block read/write") - with open(fn, "w") as f: - n = f.write(lines) - print(n, "bytes written") - n = f.write(short) - print(n, "bytes written") - n = f.write(lines) - print(n, "bytes written") - - with open(fn, "r") as f: - result1 = f.read() - print(len(result1), "bytes read") - - fn = "/fc/rats1.txt" - print() - print("Single block read/write") - with open(fn, "w") as f: - n = f.write(short) # one block - print(n, "bytes written") - - with open(fn, "r") as f: - result2 = f.read() - print(len(result2), "bytes read") - - os.umount("/fc") - - print() - print("Verifying data read back") - success = True - if result1 == "".join((lines, short, lines)): - print("Large file Pass") - else: - print("Large file Fail") - success = False - if result2 == short: - print("Small file Pass") - else: - print("Small file Fail") - success = False - print() - print("Tests", "passed" if success else "failed") diff --git a/extmod/ntptime.py b/extmod/ntptime.py deleted file mode 100644 index 05d7e9717d82d..0000000000000 --- a/extmod/ntptime.py +++ /dev/null @@ -1,48 +0,0 @@ -import utime - -try: - import usocket as socket -except: - import socket -try: - import ustruct as struct -except: - import struct - -# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org' -host = "pool.ntp.org" - - -def time(): - NTP_QUERY = bytearray(48) - NTP_QUERY[0] = 0x1B - addr = socket.getaddrinfo(host, 123)[0][-1] - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - s.settimeout(1) - res = s.sendto(NTP_QUERY, addr) - msg = s.recv(48) - finally: - s.close() - val = struct.unpack("!I", msg[40:44])[0] - - EPOCH_YEAR = utime.gmtime(0)[0] - if EPOCH_YEAR == 2000: - # (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 - NTP_DELTA = 3155673600 - elif EPOCH_YEAR == 1970: - # (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60 - NTP_DELTA = 2208988800 - else: - raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR)) - - return val - NTP_DELTA - - -# There's currently no timezone support in MicroPython, and the RTC is set in UTC time. -def settime(): - t = time() - import machine - - tm = utime.gmtime(t) - machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) diff --git a/extmod/webrepl/manifest.py b/extmod/webrepl/manifest.py deleted file mode 100644 index 6d1a314219caa..0000000000000 --- a/extmod/webrepl/manifest.py +++ /dev/null @@ -1,2 +0,0 @@ -module("webrepl.py", opt=3) -module("webrepl_setup.py", opt=3) diff --git a/extmod/webrepl/webrepl.py b/extmod/webrepl/webrepl.py deleted file mode 100644 index 56767d8b71f73..0000000000000 --- a/extmod/webrepl/webrepl.py +++ /dev/null @@ -1,177 +0,0 @@ -# This module should be imported from REPL, not run from command line. -import binascii -import hashlib -import network -import os -import socket -import sys -import websocket -import _webrepl - -listen_s = None -client_s = None - -DEBUG = 0 - -_DEFAULT_STATIC_HOST = const("https://micropython.org/webrepl/") -static_host = _DEFAULT_STATIC_HOST - - -def server_handshake(cl): - req = cl.makefile("rwb", 0) - # Skip HTTP GET line. - l = req.readline() - if DEBUG: - sys.stdout.write(repr(l)) - - webkey = None - upgrade = False - websocket = False - - while True: - l = req.readline() - if not l: - # EOF in headers. - return False - if l == b"\r\n": - break - if DEBUG: - sys.stdout.write(l) - h, v = [x.strip() for x in l.split(b":", 1)] - if DEBUG: - print((h, v)) - if h == b"Sec-WebSocket-Key": - webkey = v - elif h == b"Connection" and b"Upgrade" in v: - upgrade = True - elif h == b"Upgrade" and v == b"websocket": - websocket = True - - if not (upgrade and websocket and webkey): - return False - - if DEBUG: - print("Sec-WebSocket-Key:", webkey, len(webkey)) - - d = hashlib.sha1(webkey) - d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") - respkey = d.digest() - respkey = binascii.b2a_base64(respkey)[:-1] - if DEBUG: - print("respkey:", respkey) - - cl.send( - b"""\ -HTTP/1.1 101 Switching Protocols\r -Upgrade: websocket\r -Connection: Upgrade\r -Sec-WebSocket-Accept: """ - ) - cl.send(respkey) - cl.send("\r\n\r\n") - - return True - - -def send_html(cl): - cl.send( - b"""\ -HTTP/1.0 200 OK\r -\r -\r -\r -""" - ) - cl.close() - - -def setup_conn(port, accept_handler): - global listen_s - listen_s = socket.socket() - listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - ai = socket.getaddrinfo("0.0.0.0", port) - addr = ai[0][4] - - listen_s.bind(addr) - listen_s.listen(1) - if accept_handler: - listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler) - for i in (network.AP_IF, network.STA_IF): - iface = network.WLAN(i) - if iface.active(): - print("WebREPL server started on http://%s:%d/" % (iface.ifconfig()[0], port)) - return listen_s - - -def accept_conn(listen_sock): - global client_s - cl, remote_addr = listen_sock.accept() - - if not server_handshake(cl): - send_html(cl) - return False - - prev = os.dupterm(None) - os.dupterm(prev) - if prev: - print("\nConcurrent WebREPL connection from", remote_addr, "rejected") - cl.close() - return False - print("\nWebREPL connection from:", remote_addr) - client_s = cl - - ws = websocket.websocket(cl, True) - ws = _webrepl._webrepl(ws) - cl.setblocking(False) - # notify REPL on socket incoming data (ESP32/ESP8266-only) - if hasattr(os, "dupterm_notify"): - cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify) - os.dupterm(ws) - - return True - - -def stop(): - global listen_s, client_s - os.dupterm(None) - if client_s: - client_s.close() - if listen_s: - listen_s.close() - - -def start(port=8266, password=None, accept_handler=accept_conn): - global static_host - stop() - webrepl_pass = password - if webrepl_pass is None: - try: - import webrepl_cfg - - webrepl_pass = webrepl_cfg.PASS - if hasattr(webrepl_cfg, "BASE"): - static_host = webrepl_cfg.BASE - except: - print("WebREPL is not configured, run 'import webrepl_setup'") - - _webrepl.password(webrepl_pass) - s = setup_conn(port, accept_handler) - - if accept_handler is None: - print("Starting webrepl in foreground mode") - # Run accept_conn to serve HTML until we get a websocket connection. - while not accept_conn(s): - pass - elif password is None: - print("Started webrepl in normal mode") - else: - print("Started webrepl in manual override mode") - - -def start_foreground(port=8266, password=None): - start(port, password, None) diff --git a/extmod/webrepl/webrepl_setup.py b/extmod/webrepl/webrepl_setup.py deleted file mode 100644 index 16e5f76e655e4..0000000000000 --- a/extmod/webrepl/webrepl_setup.py +++ /dev/null @@ -1,107 +0,0 @@ -import sys - -import os -import machine - -RC = "./boot.py" -CONFIG = "./webrepl_cfg.py" - - -def input_choice(prompt, choices): - while 1: - resp = input(prompt) - if resp in choices: - return resp - - -def getpass(prompt): - return input(prompt) - - -def input_pass(): - while 1: - passwd1 = getpass("New password (4-9 chars): ") - if len(passwd1) < 4 or len(passwd1) > 9: - print("Invalid password length") - continue - passwd2 = getpass("Confirm password: ") - if passwd1 == passwd2: - return passwd1 - print("Passwords do not match") - - -def exists(fname): - try: - with open(fname): - pass - return True - except OSError: - return False - - -def get_daemon_status(): - with open(RC) as f: - for l in f: - if "webrepl" in l: - if l.startswith("#"): - return False - return True - return None - - -def change_daemon(action): - LINES = ("import webrepl", "webrepl.start()") - with open(RC) as old_f, open(RC + ".tmp", "w") as new_f: - found = False - for l in old_f: - for patt in LINES: - if patt in l: - found = True - if action and l.startswith("#"): - l = l[1:] - elif not action and not l.startswith("#"): - l = "#" + l - new_f.write(l) - if not found: - new_f.write("import webrepl\nwebrepl.start()\n") - # FatFs rename() is not POSIX compliant, will raise OSError if - # dest file exists. - os.remove(RC) - os.rename(RC + ".tmp", RC) - - -def main(): - status = get_daemon_status() - - print("WebREPL daemon auto-start status:", "enabled" if status else "disabled") - print("\nWould you like to (E)nable or (D)isable it running on boot?") - print("(Empty line to quit)") - resp = input("> ").upper() - - if resp == "E": - if exists(CONFIG): - resp2 = input_choice( - "Would you like to change WebREPL password? (y/n) ", ("y", "n", "") - ) - else: - print("To enable WebREPL, you must set password for it") - resp2 = "y" - - if resp2 == "y": - passwd = input_pass() - with open(CONFIG, "w") as f: - f.write("PASS = %r\n" % passwd) - - if resp not in ("D", "E") or (resp == "D" and not status) or (resp == "E" and status): - print("No further action required") - sys.exit() - - change_daemon(resp == "E") - - print("Changes will be activated after reboot") - resp = input_choice("Would you like to reboot now? (y/n) ", ("y", "n", "")) - if resp == "y": - machine.reset() - - -main() From 6aa3c946347281875165392c09753547d8c77fc3 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 5 Sep 2022 11:20:10 +0100 Subject: [PATCH 2579/3533] rp2/rp2_flash: Add start/len support to rp2.Flash() constructor. This allows support for partitioned flash on rp2 boards. See issue #9208. Signed-off-by: Phil Howard --- ports/rp2/rp2_flash.c | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index db6b9f3bfe757..47c95ea5c822c 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -70,16 +70,45 @@ bi_decl(bi_block_device( BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN)); STATIC mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - // Check args. - mp_arg_check_num(n_args, n_kw, 0, 0, false); + // Parse arguments + enum { ARG_start, ARG_len }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) { + #ifndef NDEBUG + extern char __flash_binary_end; + assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE); + #endif + + // Default singleton object that accesses entire flash + return MP_OBJ_FROM_PTR(&rp2_flash_obj); + } + + rp2_flash_obj_t *self = mp_obj_malloc(rp2_flash_obj_t, &rp2_flash_type); + + mp_int_t start = args[ARG_start].u_int; + if (start == -1) { + start = 0; + } else if (!(0 <= start && start < MICROPY_HW_FLASH_STORAGE_BYTES && start % BLOCK_SIZE_BYTES == 0)) { + mp_raise_ValueError(NULL); + } + + mp_int_t len = args[ARG_len].u_int; + if (len == -1) { + len = MICROPY_HW_FLASH_STORAGE_BYTES - start; + } else if (!(0 < len && start + len <= MICROPY_HW_FLASH_STORAGE_BYTES && len % BLOCK_SIZE_BYTES == 0)) { + mp_raise_ValueError(NULL); + } - #ifndef NDEBUG - extern char __flash_binary_end; - assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE); - #endif + self->flash_base = MICROPY_HW_FLASH_STORAGE_BASE + start; + self->flash_size = len; - // Return singleton object. - return MP_OBJ_FROM_PTR(&rp2_flash_obj); + return MP_OBJ_FROM_PTR(self); } STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) { From c364301817d828eeb6df74095b3ea8083e8fbef6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 23:32:11 +1000 Subject: [PATCH 2580/3533] rp2/boards: Set PICO_BOARD correctly for each board. In most cases, it's calculated automatically from the board name, and so doesn't need to be set at all. Signed-off-by: Jim Mussared --- .../rp2/boards/GARATRONIC_PYBSTICK26_RP2040/mpconfigboard.cmake | 2 -- ports/rp2/boards/PICO_W/mpconfigboard.cmake | 2 -- ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.cmake | 2 +- ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake | 2 +- 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/ports/rp2/boards/GARATRONIC_PYBSTICK26_RP2040/mpconfigboard.cmake b/ports/rp2/boards/GARATRONIC_PYBSTICK26_RP2040/mpconfigboard.cmake index 311e114b2914e..11fcb3d087ec0 100644 --- a/ports/rp2/boards/GARATRONIC_PYBSTICK26_RP2040/mpconfigboard.cmake +++ b/ports/rp2/boards/GARATRONIC_PYBSTICK26_RP2040/mpconfigboard.cmake @@ -1,3 +1 @@ # cmake file - -set(PICO_BOARD garatronic_pybstick26_rp2040) diff --git a/ports/rp2/boards/PICO_W/mpconfigboard.cmake b/ports/rp2/boards/PICO_W/mpconfigboard.cmake index d3fbce6993276..681e0dec44ffd 100644 --- a/ports/rp2/boards/PICO_W/mpconfigboard.cmake +++ b/ports/rp2/boards/PICO_W/mpconfigboard.cmake @@ -1,6 +1,4 @@ # cmake file for Raspberry Pi Pico W -set(MICROPY_BOARD PICO_W) - set(MICROPY_PY_LWIP ON) set(MICROPY_PY_NETWORK_CYW43 ON) diff --git a/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.cmake b/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.cmake index bc5d1ea65e8bf..4746c6e267a19 100644 --- a/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.cmake +++ b/ports/rp2/boards/W5100S_EVB_PICO/mpconfigboard.cmake @@ -1,4 +1,4 @@ # cmake file for Wiznet W5100S-EVB-Pico. -set(PICO_BOARD pico) +set(PICO_BOARD wiznet_w5100s_evb_pico) set(MICROPY_PY_NETWORK_WIZNET5K W5100S) set(MICROPY_PY_LWIP 1) diff --git a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake index 875b89f2befe0..f7f2650bfa0ed 100644 --- a/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake +++ b/ports/rp2/boards/W5500_EVB_PICO/mpconfigboard.cmake @@ -1,4 +1,4 @@ # cmake file for Wiznet W5500-EVB-Pico. -set(PICO_BOARD pico) +set(PICO_BOARD wiznet_w5100s_evb_pico) set(MICROPY_PY_NETWORK_WIZNET5K W5500) set(MICROPY_PY_LWIP 1) From 29437205f29976db71dc12d61ce756732e6f813a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 22:54:40 +1000 Subject: [PATCH 2581/3533] rp2/machine_spi: Use pico-sdk's default pins for SPI. Rather than hardcoding the defaults, use pico-sdk's board definition. Signed-off-by: Jim Mussared --- ports/rp2/machine_spi.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c index 42998664a76de..104bd1fd5338b 100644 --- a/ports/rp2/machine_spi.c +++ b/ports/rp2/machine_spi.c @@ -40,20 +40,36 @@ #define DEFAULT_SPI_FIRSTBIT (SPI_MSB_FIRST) #ifndef MICROPY_HW_SPI0_SCK +#if PICO_DEFAULT_SPI == 0 +#define MICROPY_HW_SPI0_SCK (PICO_DEFAULT_SPI_SCK_PIN) +#define MICROPY_HW_SPI0_MOSI (PICO_DEFAULT_SPI_TX_PIN) +#define MICROPY_HW_SPI0_MISO (PICO_DEFAULT_SPI_RX_PIN) +#else #define MICROPY_HW_SPI0_SCK (6) #define MICROPY_HW_SPI0_MOSI (7) #define MICROPY_HW_SPI0_MISO (4) #endif +#endif #ifndef MICROPY_HW_SPI1_SCK +#if PICO_DEFAULT_SPI == 1 +#define MICROPY_HW_SPI1_SCK (PICO_DEFAULT_SPI_SCK_PIN) +#define MICROPY_HW_SPI1_MOSI (PICO_DEFAULT_SPI_TX_PIN) +#define MICROPY_HW_SPI1_MISO (PICO_DEFAULT_SPI_RX_PIN) +#else #define MICROPY_HW_SPI1_SCK (10) #define MICROPY_HW_SPI1_MOSI (11) #define MICROPY_HW_SPI1_MISO (8) #endif +#endif +// SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}. #define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) +// GP{2,6,10,14,...} #define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) +// GP{3,7,11,15,...} #define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) +// GP{0,4,8,10,...} #define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) typedef struct _machine_spi_obj_t { From 315e74236fb7be7c72f708832038596d668979b7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 23:28:50 +1000 Subject: [PATCH 2582/3533] rp2/machine_i2c: Use pico-sdk's default pins for I2C. Inherits the default values for whichever instance is PICO_DEFAULT_I2C. Signed-off-by: Jim Mussared --- ports/rp2/machine_i2c.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index f785ad7dedf33..3390cc42101d5 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -35,14 +35,24 @@ #define DEFAULT_I2C_FREQ (400000) #ifndef MICROPY_HW_I2C0_SCL +#if PICO_DEFAULT_I2C == 0 +#define MICROPY_HW_I2C0_SCL (PICO_DEFAULT_I2C_SCL_PIN) +#define MICROPY_HW_I2C0_SDA (PICO_DEFAULT_I2C_SDA_PIN) +#else #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) #endif +#endif #ifndef MICROPY_HW_I2C1_SCL +#if PICO_DEFAULT_I2C == 1 +#define MICROPY_HW_I2C1_SCL (PICO_DEFAULT_I2C_SCL_PIN) +#define MICROPY_HW_I2C1_SDA (PICO_DEFAULT_I2C_SDA_PIN) +#else #define MICROPY_HW_I2C1_SCL (7) #define MICROPY_HW_I2C1_SDA (6) #endif +#endif // SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. #define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) From dec0ff7a10efee286037812287d5bfcad611f64f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 6 Sep 2022 23:32:34 +1000 Subject: [PATCH 2583/3533] rp2/boards: Remove all I2C,SPI pin defs that already match pico-sdk. I.e. for whichever SPI/I2C instance is PICO_DEFAULT_I2C, there's no need to set MICROPY_HW_SPIn_SCK. The only ones remaining are for the non-default instance. Signed-off-by: Jim Mussared --- .../rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h | 8 -------- .../boards/ADAFRUIT_ITSYBITSY_RP2040/mpconfigboard.h | 7 ------- ports/rp2/boards/ADAFRUIT_QTPY_RP2040/mpconfigboard.h | 9 +-------- .../boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h | 8 ++------ .../rp2/boards/PIMORONI_PICOLIPO_16MB/mpconfigboard.h | 8 -------- ports/rp2/boards/PIMORONI_PICOLIPO_4MB/mpconfigboard.h | 8 -------- ports/rp2/boards/PIMORONI_TINY2040/mpconfigboard.h | 1 + ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h | 8 -------- ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h | 10 +--------- 9 files changed, 5 insertions(+), 62 deletions(-) diff --git a/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h index 84d2bf20dbc6d..f9cd030d46ce1 100644 --- a/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h +++ b/ports/rp2/boards/ADAFRUIT_FEATHER_RP2040/mpconfigboard.h @@ -7,14 +7,6 @@ #define MICROPY_HW_USB_VID (0x239A) #define MICROPY_HW_USB_PID (0x80F2) -// STEMMA QT / Qwiic on I2C1 -#define MICROPY_HW_I2C1_SCL (3) -#define MICROPY_HW_I2C1_SDA (2) - -#define MICROPY_HW_SPI0_SCK (18) -#define MICROPY_HW_SPI0_MOSI (19) -#define MICROPY_HW_SPI0_MISO (20) - // NeoPixel GPIO16, power not toggleable // Red user LED GPIO13 diff --git a/ports/rp2/boards/ADAFRUIT_ITSYBITSY_RP2040/mpconfigboard.h b/ports/rp2/boards/ADAFRUIT_ITSYBITSY_RP2040/mpconfigboard.h index 8f5551172bf97..be950f4f99d46 100644 --- a/ports/rp2/boards/ADAFRUIT_ITSYBITSY_RP2040/mpconfigboard.h +++ b/ports/rp2/boards/ADAFRUIT_ITSYBITSY_RP2040/mpconfigboard.h @@ -7,13 +7,6 @@ #define MICROPY_HW_USB_VID (0x239A) #define MICROPY_HW_USB_PID (0x80FE) -#define MICROPY_HW_I2C0_SCL (3) -#define MICROPY_HW_I2C0_SDA (2) - -#define MICROPY_HW_SPI0_SCK (18) -#define MICROPY_HW_SPI0_MOSI (19) -#define MICROPY_HW_SPI0_MISO (20) - // NeoPixel data GPIO17, power GPIO16 // Red user LED GPIO11 diff --git a/ports/rp2/boards/ADAFRUIT_QTPY_RP2040/mpconfigboard.h b/ports/rp2/boards/ADAFRUIT_QTPY_RP2040/mpconfigboard.h index ca341cedd8d21..6d834fd4f5d68 100644 --- a/ports/rp2/boards/ADAFRUIT_QTPY_RP2040/mpconfigboard.h +++ b/ports/rp2/boards/ADAFRUIT_QTPY_RP2040/mpconfigboard.h @@ -12,17 +12,10 @@ #define MICROPY_HW_UART1_CTS (10) #define MICROPY_HW_UART1_RTS (7) -#define MICROPY_HW_I2C0_SCL (25) -#define MICROPY_HW_I2C0_SDA (24) - -// STEMMA QT / Qwiic on I2C1 +// STEMMA QT / Qwiic on (non-default) I2C1 #define MICROPY_HW_I2C1_SCL (23) #define MICROPY_HW_I2C1_SDA (22) -#define MICROPY_HW_SPI0_SCK (6) -#define MICROPY_HW_SPI0_MOSI (3) -#define MICROPY_HW_SPI0_MISO (4) - // NeoPixel data GPIO12, power GPIO11 // Boot button GPIO21 diff --git a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h index 87b9187659a42..5ea9b8cdb850f 100644 --- a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h +++ b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h @@ -22,16 +22,12 @@ #define MICROPY_HW_UART1_CTS (10) #define MICROPY_HW_UART1_RTS (11) -// SPI 1 config. +// SPI 1 config (non-default). #define MICROPY_HW_SPI1_SCK (14) #define MICROPY_HW_SPI1_MOSI (11) #define MICROPY_HW_SPI1_MISO (8) -// I2C0 config. -#define MICROPY_HW_I2C0_SCL (13) -#define MICROPY_HW_I2C0_SDA (12) - -// I2C1 config. +// I2C1 config (non-default). #define MICROPY_HW_I2C1_SCL (27) #define MICROPY_HW_I2C1_SDA (26) diff --git a/ports/rp2/boards/PIMORONI_PICOLIPO_16MB/mpconfigboard.h b/ports/rp2/boards/PIMORONI_PICOLIPO_16MB/mpconfigboard.h index 134c2ff784959..a90ef6783d669 100644 --- a/ports/rp2/boards/PIMORONI_PICOLIPO_16MB/mpconfigboard.h +++ b/ports/rp2/boards/PIMORONI_PICOLIPO_16MB/mpconfigboard.h @@ -11,14 +11,6 @@ #define MICROPY_HW_UART1_CTS (10) #define MICROPY_HW_UART1_RTS (11) -// Qwiic on I2C0 -#define MICROPY_HW_I2C0_SCL (4) -#define MICROPY_HW_I2C0_SDA (5) - -#define MICROPY_HW_SPI0_SCK (18) -#define MICROPY_HW_SPI0_MOSI (19) -#define MICROPY_HW_SPI0_MISO (16) - // User LED GPIO25 // VBUS_SENSE GPIO24 diff --git a/ports/rp2/boards/PIMORONI_PICOLIPO_4MB/mpconfigboard.h b/ports/rp2/boards/PIMORONI_PICOLIPO_4MB/mpconfigboard.h index fd45547e1b286..53ade7291532a 100644 --- a/ports/rp2/boards/PIMORONI_PICOLIPO_4MB/mpconfigboard.h +++ b/ports/rp2/boards/PIMORONI_PICOLIPO_4MB/mpconfigboard.h @@ -11,14 +11,6 @@ #define MICROPY_HW_UART1_CTS (10) #define MICROPY_HW_UART1_RTS (11) -// Qwiic on I2C0 -#define MICROPY_HW_I2C0_SCL (4) -#define MICROPY_HW_I2C0_SDA (5) - -#define MICROPY_HW_SPI0_SCK (18) -#define MICROPY_HW_SPI0_MOSI (19) -#define MICROPY_HW_SPI0_MISO (16) - // User LED GPIO25 // VBUS_SENSE GPIO24 diff --git a/ports/rp2/boards/PIMORONI_TINY2040/mpconfigboard.h b/ports/rp2/boards/PIMORONI_TINY2040/mpconfigboard.h index 50cb2bd59936f..ea40a8071b77b 100644 --- a/ports/rp2/boards/PIMORONI_TINY2040/mpconfigboard.h +++ b/ports/rp2/boards/PIMORONI_TINY2040/mpconfigboard.h @@ -6,6 +6,7 @@ #define MICROPY_HW_USB_VID (0x16D0) #define MICROPY_HW_USB_PID (0x08C7) +// I2C0 (non-default) #define MICROPY_HW_I2C0_SCL (4) #define MICROPY_HW_I2C0_SDA (5) diff --git a/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h index 65b29eecd1511..72a9aeb373e44 100644 --- a/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h +++ b/ports/rp2/boards/SPARKFUN_PROMICRO/mpconfigboard.h @@ -11,12 +11,4 @@ #define MICROPY_HW_UART1_CTS (10) #define MICROPY_HW_UART1_RTS (11) -// Qwiic on I2C0 -#define MICROPY_HW_I2C0_SCL (17) -#define MICROPY_HW_I2C0_SDA (16) - -#define MICROPY_HW_SPI0_SCK (22) -#define MICROPY_HW_SPI0_MOSI (23) -#define MICROPY_HW_SPI0_MISO (20) - // NeoPixel data GPIO25, power not toggleable diff --git a/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h index f88ba5dd63c8d..c2ca90b968353 100644 --- a/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h +++ b/ports/rp2/boards/SPARKFUN_THINGPLUS/mpconfigboard.h @@ -9,15 +9,7 @@ #define MICROPY_HW_I2C0_SCL (17) #define MICROPY_HW_I2C0_SDA (16) -// Qwiic on I2C1 -#define MICROPY_HW_I2C1_SCL (7) -#define MICROPY_HW_I2C1_SDA (6) - -#define MICROPY_HW_SPI0_SCK (2) -#define MICROPY_HW_SPI0_MOSI (3) -#define MICROPY_HW_SPI0_MISO (4) - -// MicroSD on SPI1 +// MicroSD on SPI1 (non-default) #define MICROPY_HW_SPI1_SCK (14) #define MICROPY_HW_SPI1_MOSI (15) #define MICROPY_HW_SPI1_MISO (12) From 9070a2494022383586e1f86745ae4c9af52389f6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 8 Sep 2022 13:55:50 +1000 Subject: [PATCH 2584/3533] tools/autobuild: Use distinct directory for building stm32 variants. Previous the build directory just used the board name, now make it use the variant name too. This shouldn't have any change because the existing directory should not exist (all builds run by these scripts remove their build directory after completion), but it makes debugging easier. Signed-off-by: Jim Mussared --- tools/autobuild/build-stm32-extra.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/autobuild/build-stm32-extra.sh b/tools/autobuild/build-stm32-extra.sh index feb269e356049..887e3cd246943 100755 --- a/tools/autobuild/build-stm32-extra.sh +++ b/tools/autobuild/build-stm32-extra.sh @@ -10,7 +10,7 @@ function do_build() { for variant in `$MICROPY_AUTOBUILD_MAKE BOARD=$board query-variants | grep VARIANTS: | cut -d' ' -f2-`; do target=$descr-$variant echo "building $target $board" - build_dir=/tmp/stm-build-$board + build_dir=/tmp/stm-build-$board-$variant $MICROPY_AUTOBUILD_MAKE $@ BOARD=$board BOARD_VARIANT=$variant BUILD=$build_dir || exit 1 mv $build_dir/firmware.dfu $dest_dir/$target$fw_tag.dfu mv $build_dir/firmware.hex $dest_dir/$target$fw_tag.hex From 6e75d177e778caebe791add7eb4cc9eff3a33b6a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 8 Sep 2022 13:58:03 +1000 Subject: [PATCH 2585/3533] stm32/boards/PYB: Fix handling of BOARD_VARIANT selection. The matches should not have been quoted. Signed-off-by: Jim Mussared --- ports/stm32/boards/PYBLITEV10/mpconfigboard.mk | 8 ++++---- ports/stm32/boards/PYBV10/mpconfigboard.mk | 8 ++++---- ports/stm32/boards/PYBV11/mpconfigboard.mk | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk index 74698664760f0..461886b63cab0 100644 --- a/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBLITEV10/mpconfigboard.mk @@ -8,20 +8,20 @@ TEXT1_ADDR = 0x08020000 # Provide different variants for the downloads page. BOARD_VARIANTS += "dp thread dp-thread network" -ifeq ($(BOARD_VARIANT),"dp") +ifeq ($(BOARD_VARIANT),dp) MICROPY_FLOAT_IMPL=double endif -ifeq ($(BOARD_VARIANT),"thread") +ifeq ($(BOARD_VARIANT),thread) CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"dp-thread") +ifeq ($(BOARD_VARIANT),dp-thread) MICROPY_FLOAT_IMPL=double CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"network") +ifeq ($(BOARD_VARIANT),network) MICROPY_PY_NETWORK_WIZNET5K=5200 endif diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.mk b/ports/stm32/boards/PYBV10/mpconfigboard.mk index af28782678031..363f6b4806163 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV10/mpconfigboard.mk @@ -18,20 +18,20 @@ MICROPY_VFS_LFS2 = 1 # Provide different variants for the downloads page. BOARD_VARIANTS += "dp thread dp-thread network" -ifeq ($(BOARD_VARIANT),"dp") +ifeq ($(BOARD_VARIANT),dp) MICROPY_FLOAT_IMPL=double endif -ifeq ($(BOARD_VARIANT),"thread") +ifeq ($(BOARD_VARIANT),thread) CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"dp-thread") +ifeq ($(BOARD_VARIANT),dp-thread) MICROPY_FLOAT_IMPL=double CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"network") +ifeq ($(BOARD_VARIANT),network) MICROPY_PY_NETWORK_WIZNET5K=5200 endif diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.mk b/ports/stm32/boards/PYBV11/mpconfigboard.mk index cf7884cfc67a9..857049f215963 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV11/mpconfigboard.mk @@ -18,20 +18,20 @@ MICROPY_VFS_LFS2 = 1 # Provide different variants for the downloads page. BOARD_VARIANTS += "dp thread dp-thread network" -ifeq ($(BOARD_VARIANT),"dp") +ifeq ($(BOARD_VARIANT),dp) MICROPY_FLOAT_IMPL=double endif -ifeq ($(BOARD_VARIANT),"thread") +ifeq ($(BOARD_VARIANT),thread) CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"dp-thread") +ifeq ($(BOARD_VARIANT),dp-thread) MICROPY_FLOAT_IMPL=double CFLAGS += -DMICROPY_PY_THREAD=1 endif -ifeq ($(BOARD_VARIANT),"network") +ifeq ($(BOARD_VARIANT),network) MICROPY_PY_NETWORK_WIZNET5K=5200 endif From 82fc16f2982b38f6dfd0145b8012b34308d13605 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 9 Aug 2022 16:48:04 +1000 Subject: [PATCH 2586/3533] extmod/modbluetooth: Fix descriptor registration with empty tuple. Incorrect use of "continue" when the tuple was length zero meant it broke the rest of the argument handling. Signed-off-by: Jim Mussared --- extmod/modbluetooth.c | 62 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 4645ae6c98691..c2cbf0e70ad52 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -529,43 +529,41 @@ STATIC int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t character // Optional third element, iterable of descriptors. if (characteristic_len >= 3) { - mp_obj_t descriptors_len_in = mp_obj_len(characteristic_items[2]); - num_descriptors[characteristic_index] = mp_obj_get_int(descriptors_len_in); + mp_int_t n = mp_obj_get_int(mp_obj_len(characteristic_items[2])); + if (n) { + num_descriptors[characteristic_index] = n; + + // Grow the flattened uuids and flags arrays with this many more descriptors. + descriptor_uuids = m_renew(mp_obj_bluetooth_uuid_t *, descriptor_uuids, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); + descriptor_flags = m_renew(uint16_t, descriptor_flags, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); + + // Also grow the handles array. + *handles = m_renew(uint16_t, *handles, *num_handles, *num_handles + num_descriptors[characteristic_index]); + + mp_obj_iter_buf_t iter_buf_desc; + mp_obj_t iterable_desc = mp_getiter(characteristic_items[2], &iter_buf_desc); + mp_obj_t descriptor_obj; + + // Extract out descriptors for this characteristic. + while ((descriptor_obj = mp_iternext(iterable_desc)) != MP_OBJ_STOP_ITERATION) { + // (uuid, flags,) + mp_obj_t *descriptor_items; + mp_obj_get_array_fixed_n(descriptor_obj, 2, &descriptor_items); + mp_obj_t desc_uuid_obj = descriptor_items[0]; + if (!mp_obj_is_type(desc_uuid_obj, &mp_type_bluetooth_uuid)) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid descriptor UUID")); + } - if (num_descriptors[characteristic_index] == 0) { - continue; - } + descriptor_uuids[descriptor_index] = MP_OBJ_TO_PTR(desc_uuid_obj); + descriptor_flags[descriptor_index] = mp_obj_get_int(descriptor_items[1]); + ++descriptor_index; - // Grow the flattened uuids and flags arrays with this many more descriptors. - descriptor_uuids = m_renew(mp_obj_bluetooth_uuid_t *, descriptor_uuids, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); - descriptor_flags = m_renew(uint16_t, descriptor_flags, descriptor_index, descriptor_index + num_descriptors[characteristic_index]); - - // Also grow the handles array. - *handles = m_renew(uint16_t, *handles, *num_handles, *num_handles + num_descriptors[characteristic_index]); - - mp_obj_iter_buf_t iter_buf_desc; - mp_obj_t iterable_desc = mp_getiter(characteristic_items[2], &iter_buf_desc); - mp_obj_t descriptor_obj; - - // Extract out descriptors for this characteristic. - while ((descriptor_obj = mp_iternext(iterable_desc)) != MP_OBJ_STOP_ITERATION) { - // (uuid, flags,) - mp_obj_t *descriptor_items; - mp_obj_get_array_fixed_n(descriptor_obj, 2, &descriptor_items); - mp_obj_t desc_uuid_obj = descriptor_items[0]; - if (!mp_obj_is_type(desc_uuid_obj, &mp_type_bluetooth_uuid)) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid descriptor UUID")); + (*handles)[handle_index++] = 0xffff; } - descriptor_uuids[descriptor_index] = MP_OBJ_TO_PTR(desc_uuid_obj); - descriptor_flags[descriptor_index] = mp_obj_get_int(descriptor_items[1]); - ++descriptor_index; - - (*handles)[handle_index++] = 0xffff; + // Reflect that we've grown the handles array. + *num_handles += num_descriptors[characteristic_index]; } - - // Reflect that we've grown the handles array. - *num_handles += num_descriptors[characteristic_index]; } characteristic_uuids[characteristic_index] = MP_OBJ_TO_PTR(uuid_obj); From cacc96d98c2a70dc7e5194331ea70746c39746ec Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 9 Aug 2022 16:50:58 +1000 Subject: [PATCH 2587/3533] extmod/modbluetooth: Replace def_handle with end_handle in char IRQ. This is technically a breaking change, but: a) We need the end handle to do descriptor discovery properly. b) We have no possible use for the existing definition handle in the characteristic result IRQ. None of the methods can use it, and therefore no existing code should be using it in a way that changing it to a different integer value should break. Unfortunately NimBLE doesn't make it easy to get the end handle, so also implement a mechanism to use the following characteristic to calculate the previous characteristic's end handle. Signed-off-by: Jim Mussared --- docs/library/bluetooth.rst | 2 +- extmod/btstack/modbluetooth_btstack.c | 2 +- extmod/modbluetooth.c | 12 +++-- extmod/modbluetooth.h | 2 +- extmod/nimble/modbluetooth_nimble.c | 70 +++++++++++++++++++++++---- extmod/nimble/modbluetooth_nimble.h | 15 ++++++ 6 files changed, 86 insertions(+), 17 deletions(-) diff --git a/docs/library/bluetooth.rst b/docs/library/bluetooth.rst index 052f7a5c78039..8f7041e8d3fbc 100644 --- a/docs/library/bluetooth.rst +++ b/docs/library/bluetooth.rst @@ -166,7 +166,7 @@ Event Handling conn_handle, status = data elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: # Called for each characteristic found by gattc_discover_services(). - conn_handle, def_handle, value_handle, properties, uuid = data + conn_handle, end_handle, value_handle, properties, uuid = data elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: # Called once service discovery is complete. # Note: Status will be zero on success, implementation-specific value otherwise. diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index d41c671d68a32..b58be78a9942d 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -450,7 +450,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t gatt_client_characteristic_t characteristic; gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(characteristic.uuid16, characteristic.uuid128); - mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.start_handle, characteristic.value_handle, characteristic.properties, &characteristic_uuid); + mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.value_handle, characteristic.end_handle, characteristic.properties, &characteristic_uuid); } else if (event_type == GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT) { DEBUG_printf(" --> gatt descriptor query result\n"); uint16_t conn_handle = gatt_event_all_characteristic_descriptors_query_result_get_handle(packet); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index c2cbf0e70ad52..bd4d9b71790c1 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1101,7 +1101,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { // conn_handle, start_handle, end_handle, uuid ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, &o->irq_data_uuid, NULL); } else if (event == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT) { - // conn_handle, def_handle, value_handle, properties, uuid + // conn_handle, end_handle, value_handle, properties, uuid ringbuf_extract(&o->ringbuf, data_tuple, 3, 1, NULL, 0, &o->irq_data_uuid, NULL); } else if (event == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT) { // conn_handle, handle, uuid @@ -1375,8 +1375,9 @@ void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT, args, 3, 0, NULL_ADDR, service_uuid, NULL_DATA, NULL_DATA_LEN, 0); } -void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { - mp_int_t args[] = {conn_handle, def_handle, value_handle, properties}; +void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t value_handle, uint16_t end_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { + // Note: "end_handle" replaces "def_handle" from the original version of this event. + mp_int_t args[] = {conn_handle, end_handle, value_handle, properties}; invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT, args, 4, 0, NULL_ADDR, characteristic_uuid, NULL_DATA, NULL_DATA_LEN, 0); } @@ -1588,12 +1589,13 @@ void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t schedule_ringbuf(atomic_state); } -void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { +void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t value_handle, uint16_t end_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); if (enqueue_irq(o, 2 + 2 + 2 + 1 + characteristic_uuid->type, MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT)) { ringbuf_put16(&o->ringbuf, conn_handle); - ringbuf_put16(&o->ringbuf, def_handle); + // Note: "end_handle" replaces "def_handle" from the original version of this event. + ringbuf_put16(&o->ringbuf, end_handle); ringbuf_put16(&o->ringbuf, value_handle); ringbuf_put(&o->ringbuf, properties); ringbuf_put_uuid(&o->ringbuf, characteristic_uuid); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 52053045f852c..d490346278203 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -459,7 +459,7 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uin void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid); // Notify modbluetooth that a characteristic was found (either by discover-all-on-service, or discover-by-uuid-on-service). -void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t def_handle, uint16_t value_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid); +void mp_bluetooth_gattc_on_characteristic_result(uint16_t conn_handle, uint16_t value_handle, uint16_t end_handle, uint8_t properties, mp_obj_bluetooth_uuid_t *characteristic_uuid); // Notify modbluetooth that a descriptor was found. void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t handle, mp_obj_bluetooth_uuid_t *descriptor_uuid); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index c26c09e61a2cb..b0194446bd4ba 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -1310,15 +1310,51 @@ int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_ return ble_hs_err_to_errno(err); } +STATIC bool match_char_uuid(const mp_obj_bluetooth_uuid_t *filter_uuid, const ble_uuid_any_t *result_uuid) { + if (!filter_uuid) { + return true; + } + ble_uuid_any_t filter_uuid_nimble; + create_nimble_uuid(filter_uuid, &filter_uuid_nimble); + return ble_uuid_cmp(&result_uuid->u, &filter_uuid_nimble.u) == 0; +} + STATIC int ble_gattc_characteristic_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *characteristic, void *arg) { DEBUG_printf("ble_gattc_characteristic_cb: conn_handle=%d status=%d def_handle=%d val_handle=%d\n", conn_handle, error->status, characteristic ? characteristic->def_handle : -1, characteristic ? characteristic->val_handle : -1); if (!mp_bluetooth_is_active()) { return 0; } + + mp_bluetooth_nimble_pending_characteristic_t *pending = &MP_STATE_PORT(bluetooth_nimble_root_pointers)->pending_char_result; + if (pending->ready) { + // If there's a pending characteristic, we now know what it's end handle is, report it up to modbluetooth. + pending->ready = 0; + + // The end handle will either be the end of the query range (there are + // no more results), or one before the current result's definition + // handle. + uint16_t end_handle = MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_disc_end_handle; + if (error->status == 0) { + end_handle = characteristic->def_handle - 1; + } + + // Assume same conn_handle because we're limiting to a single active discovery. + mp_bluetooth_gattc_on_characteristic_result(conn_handle, pending->value_handle, end_handle, pending->properties, &pending->uuid); + } + if (error->status == 0) { - mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(&characteristic->uuid); - mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic->def_handle, characteristic->val_handle, characteristic->properties, &characteristic_uuid); + // If there's no filter, or the filter matches, then save this result. + if (match_char_uuid(MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_filter_uuid, &characteristic->uuid)) { + pending->value_handle = characteristic->val_handle; + pending->properties = characteristic->properties; + pending->uuid = create_mp_uuid(&characteristic->uuid); + pending->ready = 1; + } } else { + // Finished (or failed). Allow another characteristic discovery to start. + MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_disc_end_handle = 0; + + // Report completion. mp_bluetooth_gattc_on_discover_complete(MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE, conn_handle, error->status == BLE_HS_EDONE ? 0 : error->status); } return 0; @@ -1328,13 +1364,29 @@ int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t s if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } - int err; - if (uuid) { - ble_uuid_any_t nimble_uuid; - create_nimble_uuid(uuid, &nimble_uuid); - err = ble_gattc_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, &nimble_uuid.u, &ble_gattc_characteristic_cb, NULL); - } else { - err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gattc_characteristic_cb, NULL); + + // The implementation of characteristic discovery queries for all + // characteristics, and then UUID filtering is applied by NimBLE on each + // characteristic. Unfortunately, each characteristic result does not + // include its end handle, so you need to know the next characteristic + // before you can raise the previous one to modbluetooth. But if we let + // NimBLE do the filtering, then we don't necessarily see the next one. + // So we make NimBLE return all results and do the filtering here instead. + + if (MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_disc_end_handle) { + // Only allow a single discovery (otherwise we'd need to track a + // pending characteristic per conn handle). + return MP_EBUSY; + } + + // Set the uuid filter (if any). This needs to be a root pointer, + // otherwise we'd use ble_gattc_disc_all_chrs's arg param. + MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_filter_uuid = uuid; + + int err = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, &ble_gattc_characteristic_cb, NULL); + if (!err) { + // Lock out concurrent characteristic discovery. + MP_STATE_PORT(bluetooth_nimble_root_pointers)->char_disc_end_handle = end_handle; } return ble_hs_err_to_errno(err); } diff --git a/extmod/nimble/modbluetooth_nimble.h b/extmod/nimble/modbluetooth_nimble.h index 15648a9959a60..d9bef649201ce 100644 --- a/extmod/nimble/modbluetooth_nimble.h +++ b/extmod/nimble/modbluetooth_nimble.h @@ -31,6 +31,13 @@ #define MP_BLUETOOTH_NIMBLE_MAX_SERVICES (8) +typedef struct _mp_bluetooth_nimble_pending_characteristic_t { + uint16_t value_handle; + uint8_t properties; + mp_obj_bluetooth_uuid_t uuid; + uint8_t ready; +} mp_bluetooth_nimble_pending_characteristic_t; + typedef struct _mp_bluetooth_nimble_root_pointers_t { // Characteristic (and descriptor) value storage. mp_gatts_db_t gatts_db; @@ -44,6 +51,14 @@ typedef struct _mp_bluetooth_nimble_root_pointers_t { struct _mp_bluetooth_nimble_l2cap_channel_t *l2cap_chan; bool l2cap_listening; #endif + + #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + // Workaround to allow us to get the end_handle of each characteristic + // during discovery. See mp_bluetooth_gattc_discover_characteristics(). + uint16_t char_disc_end_handle; + const mp_obj_bluetooth_uuid_t *char_filter_uuid; + mp_bluetooth_nimble_pending_characteristic_t pending_char_result; + #endif } mp_bluetooth_nimble_root_pointers_t; enum { From 1d4228ccc1f4b6760007a8370d75a3ae3339c801 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 9 Sep 2022 14:14:31 +1000 Subject: [PATCH 2588/3533] zephyr/boards: Add config for bbc_microbit_v2. This enables the bluetooth module. GAP scanning and advertising works. Signed-off-by: Damien George --- ports/zephyr/boards/bbc_microbit_v2.conf | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 ports/zephyr/boards/bbc_microbit_v2.conf diff --git a/ports/zephyr/boards/bbc_microbit_v2.conf b/ports/zephyr/boards/bbc_microbit_v2.conf new file mode 100644 index 0000000000000..31872244ca712 --- /dev/null +++ b/ports/zephyr/boards/bbc_microbit_v2.conf @@ -0,0 +1,8 @@ +CONFIG_CONSOLE_SUBSYS=n +CONFIG_NETWORKING=n +CONFIG_BT=y +CONFIG_BT_DEVICE_NAME_DYNAMIC=y +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_CENTRAL=y + +CONFIG_MICROPY_HEAP_SIZE=98304 From 4903e48e340bd9741577f30cacb31605cc70cf20 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 9 Sep 2022 18:12:55 +1000 Subject: [PATCH 2589/3533] tools/makemanifest.py: Force the repo version of the mpy_cross package. In case the version from pypi is installed or some other version is available in sys.path, prepend `$(TOP)/mpy-cross` to sys.path instead. Signed-off-by: Jim Mussared --- tools/makemanifest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/makemanifest.py b/tools/makemanifest.py index d059d4a2660fe..9dd8815aac5f7 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -29,7 +29,8 @@ import os import subprocess -sys.path.append(os.path.join(os.path.dirname(__file__), "../mpy-cross")) +# Always use the mpy-cross from this repo. +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../mpy-cross")) import mpy_cross import manifestfile From 582b3e4e7864809e30eac694251600f61db52a3c Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 9 Sep 2022 09:48:01 +1000 Subject: [PATCH 2590/3533] py/objpolyiter: Add a new polyiter type with finaliser support. --- py/obj.h | 3 +++ py/objpolyiter.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/py/obj.h b/py/obj.h index e048cf0c286af..8949830282256 100644 --- a/py/obj.h +++ b/py/obj.h @@ -677,6 +677,9 @@ extern const mp_obj_type_t mp_type_stringio; extern const mp_obj_type_t mp_type_bytesio; extern const mp_obj_type_t mp_type_reversed; extern const mp_obj_type_t mp_type_polymorph_iter; +#if MICROPY_ENABLE_FINALISER +extern const mp_obj_type_t mp_type_polymorph_iter_with_finaliser; +#endif // Exceptions extern const mp_obj_type_t mp_type_BaseException; diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 01880bff9c1b9..16fd1f486c15f 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -51,3 +51,37 @@ const mp_obj_type_t mp_type_polymorph_iter = { .getiter = mp_identity_getiter, .iternext = polymorph_it_iternext, }; + +#if MICROPY_ENABLE_FINALISER +// mp_type_polymorph_iter_with_finaliser is a variant of the universal iterator +// above which has a finaliser function attached. This function will be run when +// the GC collects the iter object and can be used to close file handles etc. + +// Any instance should have these 3 fields at the beginning +typedef struct _mp_obj_polymorph_iter_with_finaliser_t { + mp_obj_base_t base; + mp_fun_1_t iternext; + mp_fun_1_t finaliser; +} mp_obj_polymorph_with_finaliser_iter_t; + +STATIC mp_obj_t mp_obj_polymorph_iter_del(mp_obj_t self_in) { + mp_obj_polymorph_with_finaliser_iter_t *self = MP_OBJ_TO_PTR(self_in); + // Redirect call to object instance's iternext method + return self->finaliser(self_in); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_obj_polymorph_iter_del_obj, mp_obj_polymorph_iter_del); + +STATIC const mp_rom_map_elem_t mp_obj_polymorph_iter_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_obj_polymorph_iter_del_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mp_obj_polymorph_iter_locals_dict, mp_obj_polymorph_iter_locals_dict_table); + +const mp_obj_type_t mp_type_polymorph_iter_with_finaliser = { + { &mp_type_type }, + .name = MP_QSTR_iterator, + .getiter = mp_identity_getiter, + .iternext = polymorph_it_iternext, + .locals_dict = (mp_obj_dict_t *)&mp_obj_polymorph_iter_locals_dict, +}; + +#endif From 4e0964b59f44b25ebaa5239f9ea4273b804ebe64 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 9 Sep 2022 09:48:01 +1000 Subject: [PATCH 2591/3533] extmod/vfs: Add finaliser to ilistdir to close directory handle. When iterating over filesystem/folders with os.iterdir(), an open file (directory) handle is used internally. Currently this file handle is only closed once the iterator is completely drained, eg. once all entries have been looped over / converted into list etc. If a program opens an iterdir but does not loop over it, or starts to loop over the iterator but breaks out of the loop, then the handle never gets closed. In this state, when the iter object is cleaned up by the garbage collector this open handle can cause corruption of the filesystem. Fixes issues #6568 and #8506. --- extmod/vfs_fat.c | 16 ++++- extmod/vfs_lfsx.c | 25 +++++++- extmod/vfs_posix.c | 19 +++++- tests/extmod/vfs_fat_ilistdir_del.py | 75 ++++++++++++++++++++++ tests/extmod/vfs_fat_ilistdir_del.py.exp | 30 +++++++++ tests/extmod/vfs_lfs_ilistdir_del.py | 75 ++++++++++++++++++++++ tests/extmod/vfs_lfs_ilistdir_del.py.exp | 30 +++++++++ tests/extmod/vfs_posix_ilistdir_del.py | 70 ++++++++++++++++++++ tests/extmod/vfs_posix_ilistdir_del.py.exp | 30 +++++++++ tools/ci.sh | 4 +- 10 files changed, 368 insertions(+), 6 deletions(-) create mode 100644 tests/extmod/vfs_fat_ilistdir_del.py create mode 100644 tests/extmod/vfs_fat_ilistdir_del.py.exp create mode 100644 tests/extmod/vfs_lfs_ilistdir_del.py create mode 100644 tests/extmod/vfs_lfs_ilistdir_del.py.exp create mode 100644 tests/extmod/vfs_posix_ilistdir_del.py create mode 100644 tests/extmod/vfs_posix_ilistdir_del.py.exp diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 27681ca77012a..7d8b51efeb617 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -28,6 +28,10 @@ #include "py/mpconfig.h" #if MICROPY_VFS_FAT +#if !MICROPY_ENABLE_FINALISER +#error "MICROPY_VFS_FAT requires MICROPY_ENABLE_FINALISER" +#endif + #if !MICROPY_VFS #error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS" #endif @@ -118,6 +122,7 @@ STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mk typedef struct _mp_vfs_fat_ilistdir_it_t { mp_obj_base_t base; mp_fun_1_t iternext; + mp_fun_1_t finaliser; bool is_str; FF_DIR dir; } mp_vfs_fat_ilistdir_it_t; @@ -162,6 +167,13 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) { return MP_OBJ_STOP_ITERATION; } +STATIC mp_obj_t mp_vfs_fat_ilistdir_it_del(mp_obj_t self_in) { + mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); + // ignore result / error because we may be closing a second time. + f_closedir(&self->dir); + return mp_const_none; +} + STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]); bool is_str_type = true; @@ -176,8 +188,10 @@ STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) { } // Create a new iterator object to list the dir - mp_vfs_fat_ilistdir_it_t *iter = mp_obj_malloc(mp_vfs_fat_ilistdir_it_t, &mp_type_polymorph_iter); + mp_vfs_fat_ilistdir_it_t *iter = m_new_obj_with_finaliser(mp_vfs_fat_ilistdir_it_t); + iter->base.type = &mp_type_polymorph_iter_with_finaliser; iter->iternext = mp_vfs_fat_ilistdir_it_iternext; + iter->finaliser = mp_vfs_fat_ilistdir_it_del; iter->is_str = is_str_type; FRESULT res = f_opendir(&self->fatfs, &iter->dir, path); if (res != FR_OK) { diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index dbd32338cfd9f..fbfeaa5ccf1a2 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -36,6 +36,10 @@ #include "extmod/vfs.h" #include "shared/timeutils/timeutils.h" +#if !MICROPY_ENABLE_FINALISER +#error "MICROPY_VFS_LFS requires MICROPY_ENABLE_FINALISER" +#endif + STATIC int MP_VFS_LFSx(dev_ioctl)(const struct LFSx_API (config) * c, int cmd, int arg, bool must_return_int) { mp_obj_t ret = mp_vfs_blockdev_ioctl(c->context, cmd, arg); int ret_i = 0; @@ -155,6 +159,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(MP_VFS_LFSx(open_obj), MP_VFS_LFSx(file_open)); typedef struct MP_VFS_LFSx (_ilistdir_it_t) { mp_obj_base_t base; mp_fun_1_t iternext; + mp_fun_1_t finaliser; bool is_str; MP_OBJ_VFS_LFSx *vfs; LFSx_API(dir_t) dir; @@ -163,11 +168,16 @@ typedef struct MP_VFS_LFSx (_ilistdir_it_t) { STATIC mp_obj_t MP_VFS_LFSx(ilistdir_it_iternext)(mp_obj_t self_in) { MP_VFS_LFSx(ilistdir_it_t) * self = MP_OBJ_TO_PTR(self_in); + if (self->vfs == NULL) { + return MP_OBJ_STOP_ITERATION; + } + struct LFSx_API (info) info; for (;;) { int ret = LFSx_API(dir_read)(&self->vfs->lfs, &self->dir, &info); if (ret == 0) { LFSx_API(dir_close)(&self->vfs->lfs, &self->dir); + self->vfs = NULL; return MP_OBJ_STOP_ITERATION; } if (!(info.name[0] == '.' && (info.name[1] == '\0' @@ -190,6 +200,14 @@ STATIC mp_obj_t MP_VFS_LFSx(ilistdir_it_iternext)(mp_obj_t self_in) { return MP_OBJ_FROM_PTR(t); } +STATIC mp_obj_t MP_VFS_LFSx(ilistdir_it_del)(mp_obj_t self_in) { + MP_VFS_LFSx(ilistdir_it_t) * self = MP_OBJ_TO_PTR(self_in); + if (self->vfs != NULL) { + LFSx_API(dir_close)(&self->vfs->lfs, &self->dir); + } + return mp_const_none; +} + STATIC mp_obj_t MP_VFS_LFSx(ilistdir_func)(size_t n_args, const mp_obj_t *args) { MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(args[0]); bool is_str_type = true; @@ -203,14 +221,17 @@ STATIC mp_obj_t MP_VFS_LFSx(ilistdir_func)(size_t n_args, const mp_obj_t *args) path = vstr_null_terminated_str(&self->cur_dir); } - MP_VFS_LFSx(ilistdir_it_t) * iter = mp_obj_malloc(MP_VFS_LFSx(ilistdir_it_t), &mp_type_polymorph_iter); + MP_VFS_LFSx(ilistdir_it_t) * iter = m_new_obj_with_finaliser(MP_VFS_LFSx(ilistdir_it_t)); + iter->base.type = &mp_type_polymorph_iter_with_finaliser; + iter->iternext = MP_VFS_LFSx(ilistdir_it_iternext); + iter->finaliser = MP_VFS_LFSx(ilistdir_it_del); iter->is_str = is_str_type; - iter->vfs = self; int ret = LFSx_API(dir_open)(&self->lfs, &iter->dir, path); if (ret < 0) { mp_raise_OSError(-ret); } + iter->vfs = self; return MP_OBJ_FROM_PTR(iter); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(MP_VFS_LFSx(ilistdir_obj), 1, 2, MP_VFS_LFSx(ilistdir_func)); diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index 9b00365817a17..36b211b84d6b7 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -33,6 +33,10 @@ #if MICROPY_VFS_POSIX +#if !MICROPY_ENABLE_FINALISER +#error "MICROPY_VFS_POSIX requires MICROPY_ENABLE_FINALISER" +#endif + #include #include #include @@ -162,6 +166,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd); typedef struct _vfs_posix_ilistdir_it_t { mp_obj_base_t base; mp_fun_1_t iternext; + mp_fun_1_t finaliser; bool is_str; DIR *dir; } vfs_posix_ilistdir_it_t; @@ -226,10 +231,22 @@ STATIC mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) { } } +STATIC mp_obj_t vfs_posix_ilistdir_it_del(mp_obj_t self_in) { + vfs_posix_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); + if (self->dir != NULL) { + MP_THREAD_GIL_EXIT(); + closedir(self->dir); + MP_THREAD_GIL_ENTER(); + } + return mp_const_none; +} + STATIC mp_obj_t vfs_posix_ilistdir(mp_obj_t self_in, mp_obj_t path_in) { mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in); - vfs_posix_ilistdir_it_t *iter = mp_obj_malloc(vfs_posix_ilistdir_it_t, &mp_type_polymorph_iter); + vfs_posix_ilistdir_it_t *iter = m_new_obj_with_finaliser(vfs_posix_ilistdir_it_t); + iter->base.type = &mp_type_polymorph_iter_with_finaliser; iter->iternext = vfs_posix_ilistdir_it_iternext; + iter->finaliser = vfs_posix_ilistdir_it_del; iter->is_str = mp_obj_get_type(path_in) == &mp_type_str; const char *path = vfs_posix_get_path_str(self, path_in); if (path[0] == '\0') { diff --git a/tests/extmod/vfs_fat_ilistdir_del.py b/tests/extmod/vfs_fat_ilistdir_del.py new file mode 100644 index 0000000000000..a833e9ac128c5 --- /dev/null +++ b/tests/extmod/vfs_fat_ilistdir_del.py @@ -0,0 +1,75 @@ +# Test ilistdir __del__ for VfsFat using a RAM device. +import gc + +try: + import uos + + uos.VfsFat +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMBlockDevice: + ERASE_BLOCK_SIZE = 4096 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) + + def readblocks(self, block, buf, off=0): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + buf[i] = self.data[addr + i] + + def writeblocks(self, block, buf, off=0): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + self.data[addr + i] = buf[i] + + def ioctl(self, op, arg): + if op == 4: # block count + return len(self.data) // self.ERASE_BLOCK_SIZE + if op == 5: # block size + return self.ERASE_BLOCK_SIZE + if op == 6: # erase block + return 0 + + +def test(bdev, vfs_class): + vfs_class.mkfs(bdev) + vfs = vfs_class(bdev) + vfs.mkdir("/test_d1") + vfs.mkdir("/test_d2") + vfs.mkdir("/test_d3") + + for i in range(10): + print(i) + + # We want to partially iterate the ilistdir iterator to leave it in an + # open state, which will then test the finaliser when it's garbage collected. + idir = vfs.ilistdir("/") + print(any(idir)) + + # Alternate way of partially iterating the ilistdir object, modifying the + # filesystem while it's open. + for dname, *_ in vfs.ilistdir("/"): + vfs.rmdir(dname) + break + vfs.mkdir(dname) + + # Also create a fully drained iterator and ensure trying to re-use it + # throws the correct exception. + idir_emptied = vfs.ilistdir("/") + l = list(idir_emptied) + print(len(l)) + try: + next(idir_emptied) + except StopIteration: + pass + + gc.collect() + vfs.open("/test", "w").close() + + +bdev = RAMBlockDevice(30) +test(bdev, uos.VfsFat) diff --git a/tests/extmod/vfs_fat_ilistdir_del.py.exp b/tests/extmod/vfs_fat_ilistdir_del.py.exp new file mode 100644 index 0000000000000..0ab2b019f4b6c --- /dev/null +++ b/tests/extmod/vfs_fat_ilistdir_del.py.exp @@ -0,0 +1,30 @@ +0 +True +3 +1 +True +4 +2 +True +4 +3 +True +4 +4 +True +4 +5 +True +4 +6 +True +4 +7 +True +4 +8 +True +4 +9 +True +4 diff --git a/tests/extmod/vfs_lfs_ilistdir_del.py b/tests/extmod/vfs_lfs_ilistdir_del.py new file mode 100644 index 0000000000000..073576986d3ac --- /dev/null +++ b/tests/extmod/vfs_lfs_ilistdir_del.py @@ -0,0 +1,75 @@ +# Test ilistdir __del__ for VfsLittle using a RAM device. +import gc + +try: + import uos + + uos.VfsLfs2 +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMBlockDevice: + ERASE_BLOCK_SIZE = 1024 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) + + def readblocks(self, block, buf, off): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + buf[i] = self.data[addr + i] + + def writeblocks(self, block, buf, off): + addr = block * self.ERASE_BLOCK_SIZE + off + for i in range(len(buf)): + self.data[addr + i] = buf[i] + + def ioctl(self, op, arg): + if op == 4: # block count + return len(self.data) // self.ERASE_BLOCK_SIZE + if op == 5: # block size + return self.ERASE_BLOCK_SIZE + if op == 6: # erase block + return 0 + + +def test(bdev, vfs_class): + vfs_class.mkfs(bdev) + vfs = vfs_class(bdev) + vfs.mkdir("/test_d1") + vfs.mkdir("/test_d2") + vfs.mkdir("/test_d3") + + for i in range(10): + print(i) + + # We want to partially iterate the ilistdir iterator to leave it in an + # open state, which will then test the finaliser when it's garbage collected. + idir = vfs.ilistdir("/") + print(any(idir)) + + # Alternate way of partially iterating the ilistdir object, modifying the + # filesystem while it's open. + for dname, *_ in vfs.ilistdir("/"): + vfs.rmdir(dname) + break + vfs.mkdir(dname) + + # Also create a fully drained iterator and ensure trying to re-use it + # throws the correct exception. + idir_emptied = vfs.ilistdir("/") + l = list(idir_emptied) + print(len(l)) + try: + next(idir_emptied) + except StopIteration: + pass + + gc.collect() + vfs.open("/test", "w").close() + + +bdev = RAMBlockDevice(30) +test(bdev, uos.VfsLfs2) diff --git a/tests/extmod/vfs_lfs_ilistdir_del.py.exp b/tests/extmod/vfs_lfs_ilistdir_del.py.exp new file mode 100644 index 0000000000000..0ab2b019f4b6c --- /dev/null +++ b/tests/extmod/vfs_lfs_ilistdir_del.py.exp @@ -0,0 +1,30 @@ +0 +True +3 +1 +True +4 +2 +True +4 +3 +True +4 +4 +True +4 +5 +True +4 +6 +True +4 +7 +True +4 +8 +True +4 +9 +True +4 diff --git a/tests/extmod/vfs_posix_ilistdir_del.py b/tests/extmod/vfs_posix_ilistdir_del.py new file mode 100644 index 0000000000000..edb50dfd6229c --- /dev/null +++ b/tests/extmod/vfs_posix_ilistdir_del.py @@ -0,0 +1,70 @@ +# Test ilistdir __del__ for VfsPosix. +import gc + +try: + import os + + os.VfsPosix +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +def test(testdir): + vfs = os.VfsPosix(testdir) + vfs.mkdir("/test_d1") + vfs.mkdir("/test_d2") + vfs.mkdir("/test_d3") + + for i in range(10): + print(i) + + # We want to partially iterate the ilistdir iterator to leave it in an + # open state, which will then test the finaliser when it's garbage collected. + idir = vfs.ilistdir("/") + print(any(idir)) + + # Alternate way of partially iterating the ilistdir object, modifying the + # filesystem while it's open. + for dname, *_ in vfs.ilistdir("/"): + vfs.rmdir(dname) + break + vfs.mkdir(dname) + + # Also create a fully drained iterator and ensure trying to re-use it + # throws the correct exception. + idir_emptied = vfs.ilistdir("/") + l = list(idir_emptied) + print(len(l)) + try: + next(idir_emptied) + except StopIteration: + pass + + gc.collect() + + # Create and delete a file, try to flush out any filesystem + # corruption that may be caused over the loops. + vfs.open("/test", "w").close() + vfs.remove("/test") + + +# We need an empty directory for testing. +# Skip the test if it already exists. +temp_dir = "vfs_posix_ilistdir_del_test_dir" +try: + os.stat(temp_dir) + print("SKIP") + raise SystemExit +except OSError: + pass + +os.mkdir(temp_dir) + +test(temp_dir) + +# Remove tempdir. +for td in os.listdir(temp_dir): + os.rmdir("/".join((temp_dir, td))) + +os.rmdir(temp_dir) diff --git a/tests/extmod/vfs_posix_ilistdir_del.py.exp b/tests/extmod/vfs_posix_ilistdir_del.py.exp new file mode 100644 index 0000000000000..c30ba41326864 --- /dev/null +++ b/tests/extmod/vfs_posix_ilistdir_del.py.exp @@ -0,0 +1,30 @@ +0 +True +3 +1 +True +3 +2 +True +3 +3 +True +3 +4 +True +3 +5 +True +3 +6 +True +3 +7 +True +3 +8 +True +3 +9 +True +3 diff --git a/tools/ci.sh b/tools/ci.sh index b4cb7dc2e380a..8f1d729a13260 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -632,7 +632,7 @@ function ci_unix_qemu_mips_run_tests { # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) # - ffi tests do not work file ./ports/unix/build-coverage/micropython - (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.py' --exclude 'ffi_(callback|float|float2).py') + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.*\.py' --exclude 'ffi_(callback|float|float2).py') } function ci_unix_qemu_arm_setup { @@ -652,7 +652,7 @@ function ci_unix_qemu_arm_run_tests { # - (i)listdir does not work, it always returns the empty list (it's an issue with the underlying C call) export QEMU_LD_PREFIX=/usr/arm-linux-gnueabi file ./ports/unix/build-coverage/micropython - (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.py') + (cd tests && MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-tests.py --exclude 'vfs_posix.*\.py') } ######################################################################################## From 86676a43aa9de47bfdf52c7a71751de1bb512c64 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 12 Sep 2022 08:41:39 +1000 Subject: [PATCH 2592/3533] unix/mpconfigport: Enable MICROPY_ENABLE_FINALISER when VFS is used. --- ports/unix/mpconfigport.h | 2 +- ports/unix/variants/minimal/mpconfigvariant.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index dd73f61e24ba9..cb389d9a53620 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -45,7 +45,6 @@ #ifndef MICROPY_OPT_MAP_LOOKUP_CACHE #define MICROPY_OPT_MAP_LOOKUP_CACHE (1) #endif -#define MICROPY_ENABLE_FINALISER (1) #define MICROPY_STACK_CHECK (1) #define MICROPY_KBD_EXCEPTION (1) #define MICROPY_HELPER_REPL (1) @@ -124,6 +123,7 @@ #ifndef MICROPY_GC_SPLIT_HEAP_N_HEAPS #define MICROPY_GC_SPLIT_HEAP_N_HEAPS (1) #endif +#define MICROPY_ENABLE_FINALISER (1) #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) #define MICROPY_MEM_STATS (1) #define MICROPY_DEBUG_PRINTERS (1) diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index 2e2c1de0cc87e..d9d2a6c48e643 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -37,8 +37,8 @@ #define MICROPY_ALLOC_PARSE_CHUNK_INIT (64) #define MICROPY_ALLOC_PATH_MAX (PATH_MAX) #define MICROPY_ENABLE_GC (1) +#define MICROPY_ENABLE_FINALISER (1) #define MICROPY_GC_ALLOC_THRESHOLD (0) -#define MICROPY_ENABLE_FINALISER (0) #define MICROPY_STACK_CHECK (0) #define MICROPY_COMP_CONST (0) #define MICROPY_MEM_STATS (0) From afc7e1d298add00eacf38be0ae9455f0f09aed2e Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 12 Sep 2022 08:45:56 +1000 Subject: [PATCH 2593/3533] zephyr/mpconfigport: Enable MICROPY_ENABLE_FINALISER when VFS is used. --- ports/zephyr/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index 4c8096b44107a..71836768a44c5 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -39,6 +39,7 @@ #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_STACK_CHECK (1) #define MICROPY_ENABLE_GC (1) +#define MICROPY_ENABLE_FINALISER (MICROPY_VFS) #define MICROPY_HELPER_REPL (1) #define MICROPY_REPL_AUTO_INDENT (1) #define MICROPY_KBD_EXCEPTION (1) From 2d4e7e99bfcfaaed252564f9498176ca3ad88fee Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 12 Sep 2022 08:47:02 +1000 Subject: [PATCH 2594/3533] samd/mpconfigport: Enable MICROPY_ENABLE_FINALISER when VFS is used. --- ports/samd/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 0b16b5e817af0..2c6052149ba30 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -41,6 +41,7 @@ // Python internal features #define MICROPY_ENABLE_GC (1) +#define MICROPY_ENABLE_FINALISER (1) #define MICROPY_KBD_EXCEPTION (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) From 57fd66b80f8352e4859e6b71536b6083f9d7279c Mon Sep 17 00:00:00 2001 From: Wind-stormger Date: Tue, 30 Aug 2022 11:30:12 +0800 Subject: [PATCH 2595/3533] tools/pyboard.py: Support Windows pathname separators. Addresses issue #9132. --- tools/pyboard.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 436bc5ab1be58..60cc06508ebef 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -582,12 +582,12 @@ def fname_remote(src): return src def fname_cp_dest(src, dest): - src = src.rsplit("/", 1)[-1] + _, src = os.path.split(src) if dest is None or dest == "": dest = src elif dest == ".": - dest = "./" + src - elif dest.endswith("/"): + dest = os.path.join(".", src) + elif dest.endswith(os.path.sep): dest += src return dest From 89a0fefb6c5c730a7b740cf31e44a6c76c3993b1 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Sep 2022 14:57:24 +1000 Subject: [PATCH 2596/3533] py/mpconfig: Add LFS1/LFS2 options to match FAT/posix. Also fixes the #ifndef for FAT & posix. Signed-off-by: Jim Mussared --- py/mpconfig.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index d478c654d021d..b8cc61b2adb17 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -920,15 +920,25 @@ typedef double mp_float_t; #endif // Support for VFS POSIX component, to mount a POSIX filesystem within VFS -#ifndef MICROPY_VFS +#ifndef MICROPY_VFS_POSIX #define MICROPY_VFS_POSIX (0) #endif // Support for VFS FAT component, to mount a FAT filesystem within VFS -#ifndef MICROPY_VFS +#ifndef MICROPY_VFS_FAT #define MICROPY_VFS_FAT (0) #endif +// Support for VFS LittleFS v1 component, to mount a LFSv1 filesystem within VFS +#ifndef MICROPY_VFS_LFS1 +#define MICROPY_VFS_LFS1 (0) +#endif + +// Support for VFS LittleFS v2 component, to mount a LFSv2 filesystem within VFS +#ifndef MICROPY_VFS_LFS2 +#define MICROPY_VFS_LFS2 (0) +#endif + /*****************************************************************************/ /* Fine control over Python builtins, classes, modules, etc */ From 605266ee9a1258003032abb2fbfa58ef354ded25 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 16 Aug 2022 01:28:31 +1000 Subject: [PATCH 2597/3533] py/mpconfig: Make feature levels available to mpconfigport.h. Signed-off-by: Jim Mussared --- py/mpconfig.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index b8cc61b2adb17..5da7e453ecac3 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -56,12 +56,6 @@ // release vs debug configs, etc. Note that if you switch from one config // to another, you must rebuild from scratch using "-B" switch to make. -#ifdef MP_CONFIGFILE -#include MP_CONFIGFILE -#else -#include -#endif - // Disable all optional features (i.e. minimal port). #define MICROPY_CONFIG_ROM_LEVEL_MINIMUM (0) // Only enable core features (constrained flash, e.g. STM32L072) @@ -75,6 +69,12 @@ // Enable everything (e.g. coverage) #define MICROPY_CONFIG_ROM_LEVEL_EVERYTHING (50) +#ifdef MP_CONFIGFILE +#include MP_CONFIGFILE +#else +#include +#endif + // Ports/boards should set this, but default to level=core. #ifndef MICROPY_CONFIG_ROM_LEVEL #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES) From f4fed02537e5b1b1a06912c8339c47ce500d18c6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Aug 2022 11:56:03 +1000 Subject: [PATCH 2598/3533] unix/variants: Remove freedos and fast variants. The freedos variant is untested by CI and is difficult to maintain. The fast variant is not a good name for what it does. Signed-off-by: Jim Mussared --- ports/unix/variants/fast/mpconfigvariant.h | 34 ----------------- ports/unix/variants/fast/mpconfigvariant.mk | 5 --- ports/unix/variants/freedos/mpconfigvariant.h | 38 ------------------- .../unix/variants/freedos/mpconfigvariant.mk | 18 --------- 4 files changed, 95 deletions(-) delete mode 100644 ports/unix/variants/fast/mpconfigvariant.h delete mode 100644 ports/unix/variants/fast/mpconfigvariant.mk delete mode 100644 ports/unix/variants/freedos/mpconfigvariant.h delete mode 100644 ports/unix/variants/freedos/mpconfigvariant.mk diff --git a/ports/unix/variants/fast/mpconfigvariant.h b/ports/unix/variants/fast/mpconfigvariant.h deleted file mode 100644 index 8a531b056ab71..0000000000000 --- a/ports/unix/variants/fast/mpconfigvariant.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// This config file is intended to configure artificially fast uPy build for -// synthetic benchmarking, at the expense of features supported and memory -// usage. This config is not intended to be used in production. - -#define MICROPY_PY___FILE__ (0) -// 91 is a magic number proposed by @dpgeorge, which make pystone run ~ at tie -// with CPython 3.4. -#define MICROPY_MODULE_DICT_SIZE (91) diff --git a/ports/unix/variants/fast/mpconfigvariant.mk b/ports/unix/variants/fast/mpconfigvariant.mk deleted file mode 100644 index b8fe69e487315..0000000000000 --- a/ports/unix/variants/fast/mpconfigvariant.mk +++ /dev/null @@ -1,5 +0,0 @@ -# build synthetically fast interpreter for benchmarking - -COPT += -fno-crossjumping -O2 - -FROZEN_MANIFEST = diff --git a/ports/unix/variants/freedos/mpconfigvariant.h b/ports/unix/variants/freedos/mpconfigvariant.h deleted file mode 100644 index 562c783ca35fb..0000000000000 --- a/ports/unix/variants/freedos/mpconfigvariant.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2015 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// options to control how MicroPython is built - -#define MICROPY_PY_USELECT_POSIX (0) - -#define MICROPY_STREAMS_NON_BLOCK (0) - -#define MICROPY_PY_SYS_PLATFORM "freedos" - -// djgpp dirent struct does not have d_ino field -#undef _DIRENT_HAVE_D_INO - -#define MICROPY_USE_INTERNAL_ERRNO (1) diff --git a/ports/unix/variants/freedos/mpconfigvariant.mk b/ports/unix/variants/freedos/mpconfigvariant.mk deleted file mode 100644 index 86ab6864f45c1..0000000000000 --- a/ports/unix/variants/freedos/mpconfigvariant.mk +++ /dev/null @@ -1,18 +0,0 @@ -CC = i586-pc-msdosdjgpp-gcc - -STRIP = i586-pc-msdosdjgpp-strip - -SIZE = i586-pc-msdosdjgpp-size - -CFLAGS += \ - -DMICROPY_NLR_SETJMP \ - -Dtgamma=gamma \ - -DMICROPY_EMIT_X86=0 \ - -DMICROPY_NO_ALLOCA=1 \ - -MICROPY_PY_SOCKET = 0 -MICROPY_PY_FFI = 0 -MICROPY_PY_JNI = 0 -MICROPY_PY_BTREE = 0 -MICROPY_PY_THREAD = 0 -MICROPY_PY_USSL = 0 From c1530a0ce8e83811f88fbd4558ea7aaf27919ed8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 15 Aug 2022 13:58:34 +1000 Subject: [PATCH 2599/3533] unix: Refactor mpconfigport.h and mpconfigvariant.h. This is a no-op for coverage and minimal. The standard and dev variants have been merged and enable the same feature set as a typical bare-metal board. And remove the CI for the dev build. Signed-off-by: Jim Mussared --- .github/workflows/ports_unix.yml | 12 - ports/unix/main.c | 5 + ports/unix/mpconfigport.h | 283 ++++++------------ .../unix/variants/coverage/mpconfigvariant.h | 5 +- ports/unix/variants/dev/mpconfigvariant.h | 40 --- ports/unix/variants/dev/mpconfigvariant.mk | 8 - ports/unix/variants/minimal/mpconfigvariant.h | 135 ++------- ports/unix/variants/mpconfigvariant_common.h | 124 ++++++++ ports/unix/variants/nanbox/mpconfigvariant.h | 5 + .../variants/{dev => standard}/manifest.py | 0 .../unix/variants/standard/mpconfigvariant.h | 17 +- .../unix/variants/standard/mpconfigvariant.mk | 7 + tools/ci.sh | 10 - 13 files changed, 268 insertions(+), 383 deletions(-) delete mode 100644 ports/unix/variants/dev/mpconfigvariant.h delete mode 100644 ports/unix/variants/dev/mpconfigvariant.mk create mode 100644 ports/unix/variants/mpconfigvariant_common.h rename ports/unix/variants/{dev => standard}/manifest.py (100%) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 45cc87e723945..3b738634a67fe 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -49,18 +49,6 @@ jobs: if: failure() run: tests/run-tests.py --print-failures - dev: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Build - run: source tools/ci.sh && ci_unix_dev_build - - name: Run main test suite - run: source tools/ci.sh && ci_unix_dev_run_tests - - name: Print failures - if: failure() - run: tests/run-tests.py --print-failures - coverage: runs-on: ubuntu-latest steps: diff --git a/ports/unix/main.c b/ports/unix/main.c index c388106a645e1..4f019b6c2c30d 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -63,6 +63,11 @@ STATIC uint emit_opt = MP_EMIT_OPT_NONE; long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4); #endif +// Number of heaps to assign by default if MICROPY_GC_SPLIT_HEAP=1 +#ifndef MICROPY_GC_SPLIT_HEAP_N_HEAPS +#define MICROPY_GC_SPLIT_HEAP_N_HEAPS (1) +#endif + STATIC void stderr_print_strn(void *env, const char *str, size_t len) { (void)env; ssize_t ret; diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index cb389d9a53620..08ddd21f63ba1 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -24,85 +24,36 @@ * THE SOFTWARE. */ -// Options to control how MicroPython is built for this port, -// overriding defaults in py/mpconfig.h. +// Options to control how MicroPython is built for this port, overriding +// defaults in py/mpconfig.h. This file is mostly about configuring the +// features to work on Unix-like systems, see mpconfigvariant.h (and +// mpconfigvariant_common.h) for feature enabling. + +// For size_t and ssize_t +#include // Variant-specific definitions. #include "mpconfigvariant.h" -// The minimal variant's config covers everything. -// If we're building the minimal variant, ignore the rest of this file. -#ifndef MICROPY_UNIX_MINIMAL - -// If the variant did not set a feature level then configure a set of features. #ifndef MICROPY_CONFIG_ROM_LEVEL -#define MICROPY_COMP_MODULE_CONST (1) -#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1) -#define MICROPY_COMP_RETURN_IF_EXPR (1) -#ifndef MICROPY_OPT_LOAD_ATTR_FAST_PATH -#define MICROPY_OPT_LOAD_ATTR_FAST_PATH (1) -#endif -#ifndef MICROPY_OPT_MAP_LOOKUP_CACHE -#define MICROPY_OPT_MAP_LOOKUP_CACHE (1) +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES) #endif -#define MICROPY_STACK_CHECK (1) -#define MICROPY_KBD_EXCEPTION (1) -#define MICROPY_HELPER_REPL (1) -#define MICROPY_REPL_EMACS_KEYS (1) -#define MICROPY_REPL_AUTO_INDENT (1) -#define MICROPY_ENABLE_SOURCE_LINE (1) -#ifndef MICROPY_STREAMS_NON_BLOCK -#define MICROPY_STREAMS_NON_BLOCK (1) + +#ifndef MICROPY_PY_SYS_PLATFORM +#if defined(__APPLE__) && defined(__MACH__) + #define MICROPY_PY_SYS_PLATFORM "darwin" +#else + #define MICROPY_PY_SYS_PLATFORM "linux" #endif -#define MICROPY_MODULE_WEAK_LINKS (1) -#define MICROPY_CAN_OVERRIDE_BUILTINS (1) -#define MICROPY_PY_FUNCTION_ATTRS (1) -#define MICROPY_PY_DESCRIPTORS (1) -#define MICROPY_PY_DELATTR_SETATTR (1) -#define MICROPY_PY_FSTRINGS (1) -#define MICROPY_PY_BUILTINS_BYTES_HEX (1) -#define MICROPY_PY_BUILTINS_STR_UNICODE (1) -#define MICROPY_PY_BUILTINS_STR_CENTER (1) -#define MICROPY_PY_BUILTINS_STR_PARTITION (1) -#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1) -#define MICROPY_PY_BUILTINS_MEMORYVIEW (1) -#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) -#define MICROPY_PY_BUILTINS_SLICE_INDICES (1) -#define MICROPY_PY_BUILTINS_FROZENSET (1) -#define MICROPY_PY_BUILTINS_ROUND_INT (1) -#define MICROPY_PY_ALL_SPECIAL_METHODS (1) -#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) -#define MICROPY_PY_BUILTINS_COMPILE (1) -#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) -#define MICROPY_PY_BUILTINS_INPUT (1) -#define MICROPY_PY_BUILTINS_POW3 (1) -#define MICROPY_PY_MICROPYTHON_MEM_INFO (1) -#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) -#define MICROPY_PY_COLLECTIONS_DEQUE (1) -#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1) -#ifndef MICROPY_PY_MATH_SPECIAL_FUNCTIONS -#define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1) #endif -#define MICROPY_PY_MATH_ISCLOSE (MICROPY_PY_MATH_SPECIAL_FUNCTIONS) -#define MICROPY_PY_CMATH (1) -#define MICROPY_PY_IO_IOBASE (1) -#define MICROPY_PY_SYS_MAXSIZE (1) -#define MICROPY_PY_SYS_STDFILES (1) -#define MICROPY_PY_UERRNO (1) -#define MICROPY_PY_UCTYPES (1) -#define MICROPY_PY_UZLIB (1) -#define MICROPY_PY_UJSON (1) -#define MICROPY_PY_UOS (1) -#define MICROPY_PY_URE (1) -#define MICROPY_PY_UHEAPQ (1) -#define MICROPY_PY_UHASHLIB (1) -#define MICROPY_PY_UBINASCII (1) -#define MICROPY_PY_UBINASCII_CRC32 (1) -#define MICROPY_PY_URANDOM (1) + +#ifndef MICROPY_PY_SYS_PATH_DEFAULT +#define MICROPY_PY_SYS_PATH_DEFAULT ".frozen:~/.micropython/lib:/usr/lib/micropython" #endif -#define MICROPY_ALLOC_PATH_MAX (PATH_MAX) -#define MICROPY_PERSISTENT_CODE_LOAD (1) +#define MP_STATE_PORT MP_STATE_VM + +// Configure which emitter to use for this target. #if !defined(MICROPY_EMIT_X64) && defined(__x86_64__) #define MICROPY_EMIT_X64 (1) #endif @@ -118,83 +69,75 @@ #if !defined(MICROPY_EMIT_ARM) && defined(__arm__) && !defined(__thumb2__) #define MICROPY_EMIT_ARM (1) #endif -#define MICROPY_ENABLE_GC (1) -// Number of heaps to assign if MICROPY_GC_SPLIT_HEAP=1 -#ifndef MICROPY_GC_SPLIT_HEAP_N_HEAPS -#define MICROPY_GC_SPLIT_HEAP_N_HEAPS (1) -#endif -#define MICROPY_ENABLE_FINALISER (1) -#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) -#define MICROPY_MEM_STATS (1) -#define MICROPY_DEBUG_PRINTERS (1) -// Printing debug to stderr may give tests which -// check stdout a chance to pass, etc. -#define MICROPY_DEBUG_PRINTER (&mp_stderr_print) -#define MICROPY_READER_POSIX (1) -#define MICROPY_READER_VFS (1) -#define MICROPY_USE_READLINE_HISTORY (1) -#ifndef MICROPY_READLINE_HISTORY_SIZE -#define MICROPY_READLINE_HISTORY_SIZE 50 + +// Type definitions for the specific machine based on the word size. +#ifndef MICROPY_OBJ_REPR +#ifdef __LP64__ +typedef long mp_int_t; // must be pointer size +typedef unsigned long mp_uint_t; // must be pointer size +#else +// These are definitions for machines where sizeof(int) == sizeof(void*), +// regardless of actual size. +typedef int mp_int_t; // must be pointer size +typedef unsigned int mp_uint_t; // must be pointer size #endif -#define MICROPY_HELPER_LEXER_UNIX (1) -#ifndef MICROPY_FLOAT_IMPL -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) +#else +// Assume that if we already defined the obj repr then we also defined types. #endif -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) -#define MICROPY_STREAMS_POSIX_API (1) -#define MICROPY_OPT_COMPUTED_GOTO (1) -#define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (1) -#define MICROPY_VFS (1) -#define MICROPY_VFS_POSIX (1) -#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0) -#define MICROPY_PY_SYS_EXIT (1) -#define MICROPY_PY_SYS_ATEXIT (1) -#if MICROPY_PY_SYS_SETTRACE -#define MICROPY_PERSISTENT_CODE_SAVE (1) -#define MICROPY_COMP_CONST (0) + +// Cannot include , as it may lead to symbol name clashes +#if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) +typedef long long mp_off_t; +#else +typedef long mp_off_t; #endif -#ifndef MICROPY_PY_SYS_PLATFORM -#if defined(__APPLE__) && defined(__MACH__) - #define MICROPY_PY_SYS_PLATFORM "darwin" + +// We need to provide a declaration/definition of alloca() +// unless support for it is disabled. +#if !defined(MICROPY_NO_ALLOCA) || MICROPY_NO_ALLOCA == 0 +#if defined(__FreeBSD__) || defined(__NetBSD__) +#include #else - #define MICROPY_PY_SYS_PLATFORM "linux" +#include #endif #endif -#ifndef MICROPY_PY_SYS_PATH_DEFAULT -#define MICROPY_PY_SYS_PATH_DEFAULT ".frozen:~/.micropython/lib:/usr/lib/micropython" + +// Always enable GC. +#define MICROPY_ENABLE_GC (1) + +#if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__)) +// Fall back to setjmp() implementation for discovery of GC pointers in registers. +#define MICROPY_GCREGS_SETJMP (1) #endif -#define MICROPY_PY_SYS_EXC_INFO (1) -#define MICROPY_PY_GC_COLLECT_RETVAL (1) +// Enable the VFS, and enable the posix "filesystem". +#define MICROPY_ENABLE_FINALISER (1) +#define MICROPY_VFS (1) +#define MICROPY_READER_VFS (1) +#define MICROPY_HELPER_LEXER_UNIX (1) +#define MICROPY_VFS_POSIX (1) +#define MICROPY_READER_POSIX (1) + +// VFS stat functions should return time values relative to 1970/1/1 +#define MICROPY_EPOCH_IS_1970 (1) + +// Assume that select() call, interrupted with a signal, and erroring +// with EINTR, updates remaining timeout value. +#define MICROPY_SELECT_REMAINING_TIME (1) + +// Disable stackless by default. #ifndef MICROPY_STACKLESS #define MICROPY_STACKLESS (0) #define MICROPY_STACKLESS_STRICT (0) #endif -#define MICROPY_PY_UOS_INCLUDEFILE "ports/unix/moduos.c" -#define MICROPY_PY_UOS_ERRNO (1) -#define MICROPY_PY_UOS_GETENV_PUTENV_UNSETENV (1) -#define MICROPY_PY_UOS_SEP (1) -#define MICROPY_PY_UOS_SYSTEM (1) -#define MICROPY_PY_UOS_URANDOM (1) -#define MICROPY_PY_UTIME (1) -#define MICROPY_PY_UTIME_MP_HAL (1) -#define MICROPY_PY_UTIMEQ (1) -#define MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128) -#if MICROPY_PY_USSL -#define MICROPY_PY_UHASHLIB_MD5 (1) -#define MICROPY_PY_UHASHLIB_SHA1 (1) -#define MICROPY_PY_UCRYPTOLIB (1) -#endif -#ifndef MICROPY_PY_USELECT -#define MICROPY_PY_USELECT (0) -#endif -#ifndef MICROPY_PY_USELECT_POSIX -#define MICROPY_PY_USELECT_POSIX (!MICROPY_PY_USELECT) +// If settrace is enabled then we need code saving. +#if MICROPY_PY_SYS_SETTRACE +#define MICROPY_PERSISTENT_CODE_SAVE (1) +#define MICROPY_COMP_CONST (0) #endif -#define MICROPY_PY_UWEBSOCKET (1) -#define MICROPY_PY_MACHINE (1) -#define MICROPY_PY_MACHINE_PULSE (1) + +// Unix-specific configuration of machine.mem*. #define MICROPY_MACHINE_MEM_GET_READ_ADDR mod_machine_mem_get_addr #define MICROPY_MACHINE_MEM_GET_WRITE_ADDR mod_machine_mem_get_addr @@ -203,52 +146,23 @@ #define MICROPY_FATFS_MAX_SS (4096) #define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ -// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc. -// names in exception messages (may require more RAM). -#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) -#define MICROPY_WARNINGS (1) -#define MICROPY_ERROR_PRINTER (&mp_stderr_print) -#define MICROPY_PY_STR_BYTES_CMP_WARN (1) - -// VFS stat functions should return time values relative to 1970/1/1 -#define MICROPY_EPOCH_IS_1970 (1) - -extern const struct _mp_print_t mp_stderr_print; - -#if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__)) -// Fall back to setjmp() implementation for discovery of GC pointers in registers. -#define MICROPY_GCREGS_SETJMP (1) -#endif - -#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) -#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) -#define MICROPY_ASYNC_KBD_INTR (1) +#define MICROPY_ALLOC_PATH_MAX (PATH_MAX) -// type definitions for the specific machine +// Ensure builtinimport.c works with -m. +#define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (1) -// For size_t and ssize_t -#include +// Don't default sys.argv because we do that in main. +#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0) -// assume that if we already defined the obj repr then we also defined types -#ifndef MICROPY_OBJ_REPR -#ifdef __LP64__ -typedef long mp_int_t; // must be pointer size -typedef unsigned long mp_uint_t; // must be pointer size -#else -// These are definitions for machines where sizeof(int) == sizeof(void*), -// regardless of actual size. -typedef int mp_int_t; // must be pointer size -typedef unsigned int mp_uint_t; // must be pointer size -#endif -#endif +#define MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128) -// Cannot include , as it may lead to symbol name clashes -#if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) -typedef long long mp_off_t; -#else -typedef long mp_off_t; -#endif +// Bare-metal ports don't have stderr. Printing debug to stderr may give tests +// which check stdout a chance to pass, etc. +extern const struct _mp_print_t mp_stderr_print; +#define MICROPY_DEBUG_PRINTER (&mp_stderr_print) +#define MICROPY_ERROR_PRINTER (&mp_stderr_print) +// For the native emitter configure how to mark a region as executable. void mp_unix_alloc_exec(size_t min_size, void **ptr, size_t *size); void mp_unix_free_exec(void *ptr, size_t size); void mp_unix_mark_exec(void); @@ -260,8 +174,8 @@ void mp_unix_mark_exec(void); #define MICROPY_FORCE_PLAT_ALLOC_EXEC (1) #endif +// If enabled, configure how to seed random on init. #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC -// Support for seeding the random module on import. #include void mp_hal_get_random(size_t n, void *buf); static inline unsigned long mp_urandom_seed_init(void) { @@ -276,10 +190,6 @@ static inline unsigned long mp_urandom_seed_init(void) { #define MICROPY_PLAT_DEV_MEM (1) #endif -// Assume that select() call, interrupted with a signal, and erroring -// with EINTR, updates remaining timeout value. -#define MICROPY_SELECT_REMAINING_TIME (1) - #ifdef __ANDROID__ #include #if __ANDROID_API__ < 4 @@ -289,18 +199,6 @@ static inline unsigned long mp_urandom_seed_init(void) { #endif #endif -#define MP_STATE_PORT MP_STATE_VM - -// We need to provide a declaration/definition of alloca() -// unless support for it is disabled. -#if !defined(MICROPY_NO_ALLOCA) || MICROPY_NO_ALLOCA == 0 -#if defined(__FreeBSD__) || defined(__NetBSD__) -#include -#else -#include -#endif -#endif - // From "man readdir": "Under glibc, programs can check for the availability // of the fields [in struct dirent] not defined in POSIX.1 by testing whether // the macros [...], _DIRENT_HAVE_D_TYPE are defined." @@ -320,11 +218,13 @@ static inline unsigned long mp_urandom_seed_init(void) { #include #endif +// If threading is enabled, configure the atomic section. #if MICROPY_PY_THREAD #define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0xffffffff) #define MICROPY_END_ATOMIC_SECTION(x) (void)x; mp_thread_unix_end_atomic_section() #endif +// In lieu of a WFI(), slow down polling from being a tight loop. #ifndef MICROPY_EVENT_POLL_HOOK #define MICROPY_EVENT_POLL_HOOK \ do { \ @@ -334,7 +234,6 @@ static inline unsigned long mp_urandom_seed_init(void) { } while (0); #endif +// Configure the implementation of machine.idle(). #include #define MICROPY_UNIX_MACHINE_IDLE sched_yield(); - -#endif // MICROPY_UNIX_MINIMAL diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index fb70d791ac9b3..9857c668748a5 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -30,6 +30,9 @@ // Set base feature level. #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EVERYTHING) +// Enable extra Unix features. +#include "../mpconfigvariant_common.h" + // Enable additional features. #define MICROPY_DEBUG_PARSE_RULE_NAME (1) #define MICROPY_GC_SPLIT_HEAP (1) @@ -45,11 +48,9 @@ #define MICROPY_PY_BUILTINS_RANGE_BINOP (1) #define MICROPY_PY_SYS_GETSIZEOF (1) #define MICROPY_PY_IO_BUFFEREDWRITER (1) -#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (mp_urandom_seed_init()) #define MICROPY_PY_URE_DEBUG (1) #define MICROPY_PY_URE_MATCH_GROUPS (1) #define MICROPY_PY_URE_MATCH_SPAN_START_END (1) #define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1) -#define MICROPY_PY_UCRYPTOLIB (1) #define MICROPY_PY_UCRYPTOLIB_CTR (1) #define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (1) diff --git a/ports/unix/variants/dev/mpconfigvariant.h b/ports/unix/variants/dev/mpconfigvariant.h deleted file mode 100644 index 2522f1861fdc3..0000000000000 --- a/ports/unix/variants/dev/mpconfigvariant.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// Set base feature level. -#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) - -// Enable some additional features. -#ifndef MICROPY_REPL_EMACS_WORDS_MOVE -#define MICROPY_REPL_EMACS_WORDS_MOVE (1) -#endif -#ifndef MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE -#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (1) -#endif -#ifndef MICROPY_PY_SYS_SETTRACE -#define MICROPY_PY_SYS_SETTRACE (1) -#endif -#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (mp_urandom_seed_init()) diff --git a/ports/unix/variants/dev/mpconfigvariant.mk b/ports/unix/variants/dev/mpconfigvariant.mk deleted file mode 100644 index 058eda2f8d192..0000000000000 --- a/ports/unix/variants/dev/mpconfigvariant.mk +++ /dev/null @@ -1,8 +0,0 @@ -FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py - -MICROPY_ROM_TEXT_COMPRESSION = 1 -MICROPY_VFS_FAT ?= 1 -MICROPY_VFS_LFS1 ?= 1 -MICROPY_VFS_LFS2 ?= 1 - -MICROPY_PY_BLUETOOTH ?= 1 diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index d9d2a6c48e643..6b107e77909c3 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -26,117 +26,44 @@ // options to control how MicroPython is built -// Prevent the rest of the default mpconfigport.h being used. -#define MICROPY_UNIX_MINIMAL (1) +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_MINIMUM) +// Disable native emitters. +#define MICROPY_EMIT_X86 (0) +#define MICROPY_EMIT_X64 (0) +#define MICROPY_EMIT_THUMB (0) +#define MICROPY_EMIT_ARM (0) + +// Tune the parser to use less RAM by default. #define MICROPY_ALLOC_QSTR_CHUNK_INIT (64) #define MICROPY_ALLOC_PARSE_RULE_INIT (8) #define MICROPY_ALLOC_PARSE_RULE_INC (8) #define MICROPY_ALLOC_PARSE_RESULT_INIT (8) #define MICROPY_ALLOC_PARSE_RESULT_INC (8) #define MICROPY_ALLOC_PARSE_CHUNK_INIT (64) -#define MICROPY_ALLOC_PATH_MAX (PATH_MAX) -#define MICROPY_ENABLE_GC (1) -#define MICROPY_ENABLE_FINALISER (1) -#define MICROPY_GC_ALLOC_THRESHOLD (0) -#define MICROPY_STACK_CHECK (0) -#define MICROPY_COMP_CONST (0) -#define MICROPY_MEM_STATS (0) -#define MICROPY_DEBUG_PRINTERS (0) -#define MICROPY_READER_POSIX (1) -#define MICROPY_READER_VFS (1) -#define MICROPY_HELPER_REPL (1) -#define MICROPY_HELPER_LEXER_UNIX (1) -#define MICROPY_ENABLE_SOURCE_LINE (0) -#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) -#define MICROPY_WARNINGS (0) -#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (0) -#define MICROPY_KBD_EXCEPTION (1) -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) -#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) -#define MICROPY_STREAMS_NON_BLOCK (0) -#define MICROPY_OPT_COMPUTED_GOTO (0) -#define MICROPY_OPT_LOAD_ATTR_FAST_PATH (0) -#define MICROPY_OPT_MAP_LOOKUP_CACHE (0) -#define MICROPY_CAN_OVERRIDE_BUILTINS (0) -#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) -#define MICROPY_VFS (1) -#define MICROPY_VFS_POSIX (1) -#define MICROPY_CPYTHON_COMPAT (0) -#define MICROPY_PY_BUILTINS_BYTEARRAY (0) -#define MICROPY_PY_BUILTINS_MEMORYVIEW (0) -#define MICROPY_PY_BUILTINS_COMPILE (0) -#define MICROPY_PY_BUILTINS_ENUMERATE (0) -#define MICROPY_PY_BUILTINS_FILTER (0) -#define MICROPY_PY_BUILTINS_FROZENSET (0) -#define MICROPY_PY_BUILTINS_REVERSED (0) -#define MICROPY_PY_BUILTINS_SET (0) -#define MICROPY_PY_BUILTINS_SLICE (0) -#define MICROPY_PY_BUILTINS_STR_COUNT (0) -#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0) -#define MICROPY_PY_BUILTINS_STR_UNICODE (0) -#define MICROPY_PY_BUILTINS_PROPERTY (0) -#define MICROPY_PY_BUILTINS_MIN_MAX (0) -#define MICROPY_PY___FILE__ (0) -#define MICROPY_PY_MICROPYTHON_MEM_INFO (0) -#define MICROPY_PY_GC (0) -#define MICROPY_PY_GC_COLLECT_RETVAL (0) -#define MICROPY_PY_ARRAY (0) -#define MICROPY_PY_COLLECTIONS (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) -#define MICROPY_PY_IO (0) -#define MICROPY_PY_STRUCT (0) -#define MICROPY_PY_SYS (1) -#define MICROPY_PY_SYS_EXIT (0) -#define MICROPY_PY_SYS_PLATFORM "linux" -#ifndef MICROPY_PY_SYS_PATH_DEFAULT -#define MICROPY_PY_SYS_PATH_DEFAULT ".frozen:~/.micropython/lib:/usr/lib/micropython" -#endif -#define MICROPY_PY_SYS_MAXSIZE (0) -#define MICROPY_PY_SYS_STDFILES (0) -#define MICROPY_PY_CMATH (0) -#define MICROPY_PY_UCTYPES (0) -#define MICROPY_PY_UTIME (0) -#define MICROPY_PY_UZLIB (0) -#define MICROPY_PY_UJSON (0) -#define MICROPY_PY_UOS (1) -#define MICROPY_PY_URE (0) -#define MICROPY_PY_UHEAPQ (0) -#define MICROPY_PY_UHASHLIB (0) -#define MICROPY_PY_UBINASCII (0) - -////////////////////////////////////////// -// Do not change anything beyond this line -////////////////////////////////////////// - -#if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__)) -// Fall back to setjmp() implementation for discovery of GC pointers in registers. -#define MICROPY_GCREGS_SETJMP (1) -#endif - -// type definitions for the specific machine -#ifdef __LP64__ -typedef long mp_int_t; // must be pointer size -typedef unsigned long mp_uint_t; // must be pointer size -#else -// These are definitions for machines where sizeof(int) == sizeof(void*), -// regardless for actual size. -typedef int mp_int_t; // must be pointer size -typedef unsigned int mp_uint_t; // must be pointer size -#endif +// Enable features that are not enabled by default with the minimum config. +#define MICROPY_COMP_CONST_FOLDING (1) +#define MICROPY_COMP_CONST_LITERAL (1) +#define MICROPY_COMP_CONST_TUPLE (1) +#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (1) +#define MICROPY_ENABLE_COMPILER (1) +#define MICROPY_ENABLE_EXTERNAL_IMPORT (1) +#define MICROPY_FULL_CHECKS (1) +#define MICROPY_HELPER_REPL (1) +#define MICROPY_KBD_EXCEPTION (1) +#define MICROPY_MODULE_GETATTR (1) +#define MICROPY_MULTIPLE_INHERITANCE (1) +#define MICROPY_PY_ASSIGN_EXPR (1) +#define MICROPY_PY_ASYNC_AWAIT (1) +#define MICROPY_PY_ATTRTUPLE (1) +#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (1) +#define MICROPY_PY_BUILTINS_RANGE_ATTRS (1) +#define MICROPY_PY_GENERATOR_PEND_THROW (1) -// Cannot include , as it may lead to symbol name clashes -#if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) -typedef long long mp_off_t; -#else -typedef long mp_off_t; -#endif +// Enable just the sys and os built-in modules. +#define MICROPY_PY_SYS (1) +#define MICROPY_PY_UOS (1) -// We need to provide a declaration/definition of alloca() -#ifdef __FreeBSD__ -#include -#else -#include -#endif +// The minimum sets this to 1 to save flash. +#define MICROPY_QSTR_BYTES_IN_HASH (2) diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h new file mode 100644 index 0000000000000..1128f21d22ece --- /dev/null +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -0,0 +1,124 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// This file enables and configures features common to all variants +// other than "minimal". + +// Send raise KeyboardInterrupt directly from the signal handler rather than +// scheduling it into the VM. +#define MICROPY_ASYNC_KBD_INTR (1) + +// Enable helpers for printing debugging information. +#ifndef MICROPY_DEBUG_PRINTERS +#define MICROPY_DEBUG_PRINTERS (1) +#endif + +// Enable floating point by default. +#ifndef MICROPY_FLOAT_IMPL +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) +#endif + +// Enable arbritrary precision long-int by default. +#ifndef MICROPY_LONGINT_IMPL +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +#endif + +// Enable use of C libraries that need read/write/lseek/fsync, e.g. axtls. +#define MICROPY_STREAMS_POSIX_API (1) + +// REPL conveniences. +#define MICROPY_REPL_EMACS_WORDS_MOVE (1) +#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (1) +#define MICROPY_USE_READLINE_HISTORY (1) +#ifndef MICROPY_READLINE_HISTORY_SIZE +#define MICROPY_READLINE_HISTORY_SIZE (50) +#endif + +// Seed random on import. +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (mp_urandom_seed_init()) + +// Allow exception details in low-memory conditions. +#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) +#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) + +// Allow loading of .mpy files. +#define MICROPY_PERSISTENT_CODE_LOAD (1) + +// Extra memory debugging. +#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) +#define MICROPY_MEM_STATS (1) + +// Enable a small performance boost for the VM. +#define MICROPY_OPT_COMPUTED_GOTO (1) + +// Return number of collected objects from gc.collect(). +#define MICROPY_PY_GC_COLLECT_RETVAL (1) + +// Enable detailed error messages and warnings. +#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) +#define MICROPY_WARNINGS (1) +#define MICROPY_PY_STR_BYTES_CMP_WARN (1) + +// Configure the "sys" module with features not usually enabled on bare-metal. +#define MICROPY_PY_SYS_ATEXIT (1) +#define MICROPY_PY_SYS_EXC_INFO (1) + +// Configure the "os" module with extra unix features. +#define MICROPY_PY_UOS_INCLUDEFILE "ports/unix/moduos.c" +#define MICROPY_PY_UOS_ERRNO (1) +#define MICROPY_PY_UOS_GETENV_PUTENV_UNSETENV (1) +#define MICROPY_PY_UOS_SEP (1) +#define MICROPY_PY_UOS_SYSTEM (1) +#define MICROPY_PY_UOS_URANDOM (1) + +// Enable the unix-specific "time" module. +#define MICROPY_PY_UTIME (1) +#define MICROPY_PY_UTIME_MP_HAL (1) + +// Enable the utimeq module used by the previous (v2) version of uasyncio. +#define MICROPY_PY_UTIMEQ (1) + +#if MICROPY_PY_USSL +#define MICROPY_PY_UHASHLIB_MD5 (1) +#define MICROPY_PY_UHASHLIB_SHA1 (1) +#define MICROPY_PY_UCRYPTOLIB (1) +#endif + +// Use the posix implementation of the "select" module (unless the variant +// specifically asks for the MicroPython version). +#ifndef MICROPY_PY_USELECT +#define MICROPY_PY_USELECT (0) +#endif +#ifndef MICROPY_PY_USELECT_POSIX +#define MICROPY_PY_USELECT_POSIX (!MICROPY_PY_USELECT) +#endif + +// Enable the "websocket" module. +#define MICROPY_PY_UWEBSOCKET (1) + +// Enable the "machine" module, mostly for machine.mem*. +#define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_PULSE (1) diff --git a/ports/unix/variants/nanbox/mpconfigvariant.h b/ports/unix/variants/nanbox/mpconfigvariant.h index f827158fb7127..7b13b7dc6ce4e 100644 --- a/ports/unix/variants/nanbox/mpconfigvariant.h +++ b/ports/unix/variants/nanbox/mpconfigvariant.h @@ -28,6 +28,11 @@ // continues to build (i.e. catches usage of mp_obj_t that don't work with // this representation). +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) + +// Enable extra Unix features. +#include "../mpconfigvariant_common.h" + // select nan-boxing object model #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_D) diff --git a/ports/unix/variants/dev/manifest.py b/ports/unix/variants/standard/manifest.py similarity index 100% rename from ports/unix/variants/dev/manifest.py rename to ports/unix/variants/standard/manifest.py diff --git a/ports/unix/variants/standard/mpconfigvariant.h b/ports/unix/variants/standard/mpconfigvariant.h index 31618652e961b..75201e9abc8d6 100644 --- a/ports/unix/variants/standard/mpconfigvariant.h +++ b/ports/unix/variants/standard/mpconfigvariant.h @@ -27,18 +27,5 @@ // Set base feature level. #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) -// Disable some features that come enabled by default with the feature level. -#define MICROPY_OPT_MPZ_BITWISE (0) -#define MICROPY_OPT_MATH_FACTORIAL (0) -#define MICROPY_MODULE_ATTR_DELEGATION (0) -#define MICROPY_MODULE_BUILTIN_INIT (0) -#define MICROPY_ENABLE_SCHEDULER (0) -#define MICROPY_PY_BUILTINS_EXECFILE (0) -#define MICROPY_PY_MATH_CONSTANTS (0) -#define MICROPY_PY_MATH_FACTORIAL (0) -#define MICROPY_PY_SYS_PS1_PS2 (0) -#define MICROPY_PY_SYS_STDIO_BUFFER (0) -#define MICROPY_PY_UASYNCIO (0) -#define MICROPY_PY_URE_SUB (0) -#define MICROPY_PY_URANDOM_EXTRA_FUNCS (0) -#define MICROPY_PY_FRAMEBUF (0) +// Enable extra Unix features. +#include "../mpconfigvariant_common.h" diff --git a/ports/unix/variants/standard/mpconfigvariant.mk b/ports/unix/variants/standard/mpconfigvariant.mk index def7987d8dc80..929a1aec117da 100644 --- a/ports/unix/variants/standard/mpconfigvariant.mk +++ b/ports/unix/variants/standard/mpconfigvariant.mk @@ -1 +1,8 @@ # This is the default variant when you `make` the Unix port. + +FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py + +MICROPY_ROM_TEXT_COMPRESSION = 1 +MICROPY_VFS_FAT ?= 1 +MICROPY_VFS_LFS1 ?= 1 +MICROPY_VFS_LFS2 ?= 1 diff --git a/tools/ci.sh b/tools/ci.sh index 8f1d729a13260..9c670b7be5ead 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -454,14 +454,6 @@ function ci_unix_standard_run_tests { ci_unix_run_tests_full_helper standard } -function ci_unix_dev_build { - ci_unix_build_helper VARIANT=dev -} - -function ci_unix_dev_run_tests { - ci_unix_run_tests_helper VARIANT=dev -} - function ci_unix_coverage_setup { sudo pip3 install setuptools sudo pip3 install pyelftools @@ -602,8 +594,6 @@ function ci_unix_macos_build { #make ${MAKEOPTS} -C ports/unix deplibs make ${MAKEOPTS} -C ports/unix # check for additional compiler errors/warnings - make ${MAKEOPTS} -C ports/unix VARIANT=dev submodules - make ${MAKEOPTS} -C ports/unix VARIANT=dev make ${MAKEOPTS} -C ports/unix VARIANT=coverage submodules make ${MAKEOPTS} -C ports/unix VARIANT=coverage } From 3e5b1be8cad9cc38af9af30f5e7fa3582f65ffd9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 16 Aug 2022 01:41:03 +1000 Subject: [PATCH 2600/3533] py/mpconfig: Add "everything" features from unix coverage. Signed-off-by: Jim Mussared --- .../unix/variants/coverage/mpconfigvariant.h | 20 +++---------- py/mpconfig.h | 28 +++++++++---------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 9857c668748a5..6107a4a55625a 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -33,24 +33,12 @@ // Enable extra Unix features. #include "../mpconfigvariant_common.h" -// Enable additional features. -#define MICROPY_DEBUG_PARSE_RULE_NAME (1) +// Enable testing of split heap. #define MICROPY_GC_SPLIT_HEAP (1) #define MICROPY_GC_SPLIT_HEAP_N_HEAPS (4) + +// Enable additional features. +#define MICROPY_DEBUG_PARSE_RULE_NAME (1) #define MICROPY_TRACKED_ALLOC (1) -#define MICROPY_FLOAT_HIGH_QUALITY_HASH (1) -#define MICROPY_REPL_EMACS_WORDS_MOVE (1) -#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (1) #define MICROPY_WARNINGS_CATEGORY (1) -#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (1) -#define MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE (1) -#define MICROPY_PY_BUILTINS_NEXT2 (1) -#define MICROPY_PY_BUILTINS_RANGE_BINOP (1) -#define MICROPY_PY_SYS_GETSIZEOF (1) -#define MICROPY_PY_IO_BUFFEREDWRITER (1) -#define MICROPY_PY_URE_DEBUG (1) -#define MICROPY_PY_URE_MATCH_GROUPS (1) -#define MICROPY_PY_URE_MATCH_SPAN_START_END (1) -#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1) #define MICROPY_PY_UCRYPTOLIB_CTR (1) -#define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (1) diff --git a/py/mpconfig.h b/py/mpconfig.h index 5da7e453ecac3..698d264d2e800 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -683,7 +683,7 @@ // This adds Alt+F, Alt+B, Alt+D and Alt+Backspace for forward-word, backward-word, forward-kill-word // and backward-kill-word, respectively. #ifndef MICROPY_REPL_EMACS_WORDS_MOVE -#define MICROPY_REPL_EMACS_WORDS_MOVE (0) +#define MICROPY_REPL_EMACS_WORDS_MOVE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to include extra convenience keys for word movement/kill in readline REPL. @@ -691,7 +691,7 @@ // respectively. Ctrl+Delete is not implemented because it's a very different escape sequence. // Depends on MICROPY_REPL_EMACS_WORDS_MOVE. #ifndef MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE -#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (0) +#define MICROPY_REPL_EMACS_EXTRA_WORDS_MOVE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to implement auto-indent in REPL @@ -802,7 +802,7 @@ typedef double mp_float_t; // Whether to provide a high-quality hash for float and complex numbers. // Otherwise the default is a very simple but correct hashing function. #ifndef MICROPY_FLOAT_HIGH_QUALITY_HASH -#define MICROPY_FLOAT_HIGH_QUALITY_HASH (0) +#define MICROPY_FLOAT_HIGH_QUALITY_HASH (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Enable features which improve CPython compatibility @@ -1054,7 +1054,7 @@ typedef double mp_float_t; // Whether to support memoryview.itemsize attribute #ifndef MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE -#define MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE (0) +#define MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to support set object @@ -1099,12 +1099,12 @@ typedef double mp_float_t; // the same object will compare as not-equal. With it enabled the semantics // match CPython and ranges are equal if they yield the same sequence of items. #ifndef MICROPY_PY_BUILTINS_RANGE_BINOP -#define MICROPY_PY_BUILTINS_RANGE_BINOP (0) +#define MICROPY_PY_BUILTINS_RANGE_BINOP (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Support for callling next() with second argument #ifndef MICROPY_PY_BUILTINS_NEXT2 -#define MICROPY_PY_BUILTINS_NEXT2 (0) +#define MICROPY_PY_BUILTINS_NEXT2 (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to support rounding of integers (incl bignum); eg round(123,-1)=120 @@ -1124,7 +1124,7 @@ typedef double mp_float_t; // Whether to support all inplace arithmetic operarion methods // (__imul__, etc.) #ifndef MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS -#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (0) +#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to support reverse arithmetic operarion methods @@ -1219,7 +1219,7 @@ typedef double mp_float_t; // Whether to provide the "micropython.heap_locked" function #ifndef MICROPY_PY_MICROPYTHON_HEAP_LOCKED -#define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (0) +#define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to provide "array" module. Note that large chunk of the @@ -1258,7 +1258,7 @@ typedef double mp_float_t; // Whether to provide the _asdict function for namedtuple #ifndef MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT -#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (0) +#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to provide "math" module @@ -1338,7 +1338,7 @@ typedef double mp_float_t; // Whether to provide "io.BufferedWriter" class #ifndef MICROPY_PY_IO_BUFFEREDWRITER -#define MICROPY_PY_IO_BUFFEREDWRITER (0) +#define MICROPY_PY_IO_BUFFEREDWRITER (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to provide "struct" module @@ -1394,7 +1394,7 @@ typedef double mp_float_t; // Whether to provide "sys.getsizeof" function #ifndef MICROPY_PY_SYS_GETSIZEOF -#define MICROPY_PY_SYS_GETSIZEOF (0) +#define MICROPY_PY_SYS_GETSIZEOF (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif // Whether to provide sys.{stdin,stdout,stderr} objects @@ -1516,15 +1516,15 @@ typedef double mp_float_t; #endif #ifndef MICROPY_PY_URE_DEBUG -#define MICROPY_PY_URE_DEBUG (0) +#define MICROPY_PY_URE_DEBUG (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif #ifndef MICROPY_PY_URE_MATCH_GROUPS -#define MICROPY_PY_URE_MATCH_GROUPS (0) +#define MICROPY_PY_URE_MATCH_GROUPS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif #ifndef MICROPY_PY_URE_MATCH_SPAN_START_END -#define MICROPY_PY_URE_MATCH_SPAN_START_END (0) +#define MICROPY_PY_URE_MATCH_SPAN_START_END (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING) #endif #ifndef MICROPY_PY_URE_SUB From 51b054dd66fcd4f26f965910ea25de85a2ea01a3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 16 Aug 2022 01:56:25 +1000 Subject: [PATCH 2601/3533] unix: Refactor mpconfigvariant.mk. All variants (except minimal) enable text compression and fat/lfs, so move them to the common mpconfigport.mk. Signed-off-by: Jim Mussared --- ports/unix/mpconfigport.mk | 6 ++++++ ports/unix/variants/coverage/mpconfigvariant.mk | 5 ----- ports/unix/variants/minimal/mpconfigvariant.mk | 5 ++++- ports/unix/variants/standard/mpconfigvariant.mk | 5 ----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/ports/unix/mpconfigport.mk b/ports/unix/mpconfigport.mk index fe9eec18dc331..ce6183c13397a 100644 --- a/ports/unix/mpconfigport.mk +++ b/ports/unix/mpconfigport.mk @@ -38,3 +38,9 @@ MICROPY_PY_JNI = 0 # Avoid using system libraries, use copies bundled with MicroPython # as submodules (currently affects only libffi). MICROPY_STANDALONE = 0 + +MICROPY_ROM_TEXT_COMPRESSION = 1 + +MICROPY_VFS_FAT = 1 +MICROPY_VFS_LFS1 = 1 +MICROPY_VFS_LFS2 = 1 diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index bbbb0432b5b76..cc37ba1582470 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -12,10 +12,5 @@ LDFLAGS += -fprofile-arcs -ftest-coverage FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py USER_C_MODULES = $(TOP)/examples/usercmodule -MICROPY_ROM_TEXT_COMPRESSION = 1 -MICROPY_VFS_FAT = 1 -MICROPY_VFS_LFS1 = 1 -MICROPY_VFS_LFS2 = 1 - SRC_C += coverage.c SRC_CXX += coveragecpp.cpp diff --git a/ports/unix/variants/minimal/mpconfigvariant.mk b/ports/unix/variants/minimal/mpconfigvariant.mk index 9d4ddab9563e4..d5c2a52e9ae62 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.mk +++ b/ports/unix/variants/minimal/mpconfigvariant.mk @@ -2,7 +2,6 @@ FROZEN_MANIFEST = -MICROPY_ROM_TEXT_COMPRESSION = 1 MICROPY_PY_BTREE = 0 MICROPY_PY_FFI = 0 MICROPY_PY_SOCKET = 0 @@ -10,3 +9,7 @@ MICROPY_PY_THREAD = 0 MICROPY_PY_TERMIOS = 0 MICROPY_PY_USSL = 0 MICROPY_USE_READLINE = 0 + +MICROPY_VFS_FAT = 0 +MICROPY_VFS_LFS1 = 0 +MICROPY_VFS_LFS2 = 0 diff --git a/ports/unix/variants/standard/mpconfigvariant.mk b/ports/unix/variants/standard/mpconfigvariant.mk index 929a1aec117da..c91db1aa10ff9 100644 --- a/ports/unix/variants/standard/mpconfigvariant.mk +++ b/ports/unix/variants/standard/mpconfigvariant.mk @@ -1,8 +1,3 @@ # This is the default variant when you `make` the Unix port. FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py - -MICROPY_ROM_TEXT_COMPRESSION = 1 -MICROPY_VFS_FAT ?= 1 -MICROPY_VFS_LFS1 ?= 1 -MICROPY_VFS_LFS2 ?= 1 From 6c376a93067230b3b9d7d4adc1b804df18b12dcd Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Sep 2022 16:40:34 +1000 Subject: [PATCH 2602/3533] tests/extmod/uasyncio_heaplock.py: Force SKIP on stackless. This is a latent issue that wasn't caught by CI because there was no configuration that had both stackless+uasyncio. The previous check to skip with stackless builds only worked when the bytecode emitter was used by default. Force the check to use the bytecode emitter. Signed-off-by: Jim Mussared --- tests/extmod/uasyncio_heaplock.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/extmod/uasyncio_heaplock.py b/tests/extmod/uasyncio_heaplock.py index a6b4a0819c0e0..9c9487357a2ef 100644 --- a/tests/extmod/uasyncio_heaplock.py +++ b/tests/extmod/uasyncio_heaplock.py @@ -8,11 +8,19 @@ # strict stackless builds can't call functions without allocating a frame on the heap try: - f = lambda: 0 + # force bytecode (in case we're running with emit=native) and verify + # that bytecode-calling-bytecode doesn't allocate + @micropython.bytecode + def f(x): + x and f(x - 1) + micropython.heap_lock() - f() + f(1) micropython.heap_unlock() except RuntimeError: + # RuntimeError (max recursion depth) not MemoryError because effectively + # the recursion depth is at the limit while the heap is locked with + # stackless print("SKIP") raise SystemExit From 65ce7d7ade17f7f9ac2c5fbb75ad5d28078f8490 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 17 Jul 2022 08:43:02 +0200 Subject: [PATCH 2603/3533] mimxrt/machine_uart: Drop a few commented lines about TX ring buffer. --- ports/mimxrt/machine_uart.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 302244f7221f6..1b0c0633345af 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -244,12 +244,6 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args LPUART_EnableTx(self->lpuart, false); self->lpuart->STAT |= 1 << LPUART_STAT_BRK13_SHIFT; LPUART_EnableTx(self->lpuart, true); - - // Allocate the TX ring buffer. Not used yet, but maybe later. - - // ringbuf_alloc(&(self->write_buffer), txbuf_len + 1); - // MP_STATE_PORT(rp2_uart_tx_buffer[uart_id]) = self->write_buffer.buf; - } return MP_OBJ_FROM_PTR(self); From 8e542251405d780f7aa0d6c9abaa30282dd360d5 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 25 Apr 2022 13:56:45 +0200 Subject: [PATCH 2604/3533] mimxrt: Format the firmware image to match the new teensy loader. The new teensy loader keeps the file system under certain conditions: - The file size is properly set in the file header. - The header version is 4.3 These changes are implemented here, requiring a backport of fsl_flexspi_nor_boot.c. There is still a problem with the command line version of the teensy loader, which fails on the first attempt. At the second attempt it works. The GUI version of the teensy loader is fine. --- ports/mimxrt/Makefile | 2 +- ports/mimxrt/boards/common.ld | 7 +- ports/mimxrt/hal/fsl_flexspi_nor_boot.c | 53 +++++++++++ ports/mimxrt/hal/fsl_flexspi_nor_boot.h | 114 ++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 ports/mimxrt/hal/fsl_flexspi_nor_boot.c create mode 100644 ports/mimxrt/hal/fsl_flexspi_nor_boot.h diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 6312ecdd8f3ee..6a18879ac5833 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -128,7 +128,7 @@ SRC_HAL_IMX_C += \ $(MCU_DIR)/drivers/fsl_trng.c \ $(MCU_DIR)/drivers/fsl_wdog.c \ $(MCU_DIR)/system_$(MCU_SERIES).c \ - $(MCU_DIR)/xip/fsl_flexspi_nor_boot.c \ + hal/fsl_flexspi_nor_boot.c \ ifeq ($(MICROPY_HW_SDRAM_AVAIL),1) SRC_HAL_IMX_C += $(MCU_DIR)/drivers/fsl_semc.c diff --git a/ports/mimxrt/boards/common.ld b/ports/mimxrt/boards/common.ld index fbc99da3ddb0e..3aaa14c83bf21 100644 --- a/ports/mimxrt/boards/common.ld +++ b/ports/mimxrt/boards/common.ld @@ -207,7 +207,7 @@ SECTIONS __ram_function_end__ = .; } > m_itcm - __NDATA_ROM = __DATA_ROM + (__ram_function_end__ - __data_start__); + __NDATA_ROM = __RAM_FUNCTIONS_ROM + (__ram_function_end__ - __ram_function_start__); .ncache.init : AT(__NDATA_ROM) { __noncachedata_start__ = .; /* create a global symbol at ncache data start */ @@ -223,7 +223,8 @@ SECTIONS __noncachedata_end__ = .; /* define a global symbol at ncache data end */ } > m_dtcm - __DATA_END = __NDATA_ROM + (__noncachedata_init_end__ - __noncachedata_start__); + __DATA_END = __NDATA_ROM + (__noncachedata_end__ - __noncachedata_start__); + __FLASH_DATA_END = __NDATA_ROM + (__noncachedata_init_end__ - __noncachedata_start__); text_end = ORIGIN(m_text) + LENGTH(m_text); ASSERT(__DATA_END <= text_end, "region m_text overflowed with text and data") @@ -262,6 +263,8 @@ SECTIONS . += STACK_SIZE; } > m_dtcm + _flashimagelen = __FLASH_DATA_END - flash_start; + /* Initializes stack on the end of block */ __StackTop = ORIGIN(m_dtcm) + LENGTH(m_dtcm); __StackLimit = __StackTop - STACK_SIZE; diff --git a/ports/mimxrt/hal/fsl_flexspi_nor_boot.c b/ports/mimxrt/hal/fsl_flexspi_nor_boot.c new file mode 100644 index 0000000000000..5786f37e7ab80 --- /dev/null +++ b/ports/mimxrt/hal/fsl_flexspi_nor_boot.c @@ -0,0 +1,53 @@ +/* + * Copyright 2017 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "fsl_flexspi_nor_boot.h" +extern unsigned long _flashimagelen; +extern unsigned long __etext; + +/* Component ID definition, used by tools. */ +#ifndef FSL_COMPONENT_ID +#define FSL_COMPONENT_ID "platform.drivers.xip_device" +#endif + +#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1) +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.ivt"))) +#elif defined(__ICCARM__) +#pragma location=".boot_hdr.ivt" +#endif + + +/************************************* + * IVT Data + *************************************/ +const ivt image_vector_table = { + IVT_HEADER, /* IVT Header */ + IMAGE_ENTRY_ADDRESS, /* Image Entry Function */ + IVT_RSVD, /* Reserved = 0 */ + (uint32_t)DCD_ADDRESS, /* Address where DCD information is stored */ + (uint32_t)BOOT_DATA_ADDRESS, /* Address where BOOT Data Structure is stored */ + (uint32_t)&image_vector_table, /* Pointer to IVT Self (absolute address */ + (uint32_t)CSF_ADDRESS, /* Address where CSF file is stored */ + IVT_RSVD /* Reserved = 0 */ +}; + +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__) +__attribute__((section(".boot_hdr.boot_data"))) +#elif defined(__ICCARM__) +#pragma location=".boot_hdr.boot_data" +#endif +/************************************* + * Boot Data + *************************************/ +const BOOT_DATA_T boot_data = { + FLASH_BASE, /* boot start location */ + (uint32_t)&_flashimagelen, /* Image size */ + PLUGIN_FLAG, /* Plugin flag*/ + 0xFFFFFFFF /* empty - extra data word */ +}; +#endif diff --git a/ports/mimxrt/hal/fsl_flexspi_nor_boot.h b/ports/mimxrt/hal/fsl_flexspi_nor_boot.h new file mode 100644 index 0000000000000..fbb8a574f3eaa --- /dev/null +++ b/ports/mimxrt/hal/fsl_flexspi_nor_boot.h @@ -0,0 +1,114 @@ +/* + * Copyright 2017 NXP + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __FLEXSPI_NOR_BOOT_H__ +#define __FLEXSPI_NOR_BOOT_H__ + +#include +#include "board.h" + +/*! @name Driver version */ +/*@{*/ +/*! @brief XIP_DEVICE driver version 2.0.0. */ +#define FSL_XIP_DEVICE_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) +/*@}*/ + +/************************************* + * IVT Data + *************************************/ +typedef struct _ivt_ { + /** @ref hdr with tag #HAB_TAG_IVT, length and HAB version fields + * (see @ref data) + */ + uint32_t hdr; + /** Absolute address of the first instruction to execute from the + * image + */ + uint32_t entry; + /** Reserved in this version of HAB: should be NULL. */ + uint32_t reserved1; + /** Absolute address of the image DCD: may be NULL. */ + uint32_t dcd; + /** Absolute address of the Boot Data: may be NULL, but not interpreted + * any further by HAB + */ + uint32_t boot_data; + /** Absolute address of the IVT.*/ + uint32_t self; + /** Absolute address of the image CSF.*/ + uint32_t csf; + /** Reserved in this version of HAB: should be zero. */ + uint32_t reserved2; +} ivt; + +#define IVT_MAJOR_VERSION 0x4 +#define IVT_MAJOR_VERSION_SHIFT 0x4 +#define IVT_MAJOR_VERSION_MASK 0xF +#define IVT_MINOR_VERSION 0x3 +#define IVT_MINOR_VERSION_SHIFT 0x0 +#define IVT_MINOR_VERSION_MASK 0xF + +#define IVT_VERSION(major, minor) \ + ((((major) & IVT_MAJOR_VERSION_MASK) << IVT_MAJOR_VERSION_SHIFT) | \ + (((minor) & IVT_MINOR_VERSION_MASK) << IVT_MINOR_VERSION_SHIFT)) + +/* IVT header */ +#define IVT_TAG_HEADER 0xD1 /**< Image Vector Table */ +#define IVT_SIZE 0x2000 +#define IVT_PAR IVT_VERSION(IVT_MAJOR_VERSION, IVT_MINOR_VERSION) +#define IVT_HEADER (IVT_TAG_HEADER | (IVT_SIZE << 8) | (IVT_PAR << 24)) + +/* Set resume entry */ +#if defined(__CC_ARM) || defined(__ARMCC_VERSION) +extern uint32_t __Vectors[]; +extern uint32_t Image$$RW_m_config_text$$Base[]; +#define IMAGE_ENTRY_ADDRESS ((uint32_t)__Vectors) +#define FLASH_BASE ((uint32_t)Image$$RW_m_config_text$$Base) +#elif defined(__MCUXPRESSO) +extern uint32_t __Vectors[]; +extern uint32_t __boot_hdr_start__[]; +#define IMAGE_ENTRY_ADDRESS ((uint32_t)__Vectors) +#define FLASH_BASE ((uint32_t)__boot_hdr_start__) +#elif defined(__ICCARM__) +extern uint32_t __VECTOR_TABLE[]; +extern uint32_t m_boot_hdr_conf_start[]; +#define IMAGE_ENTRY_ADDRESS ((uint32_t)__VECTOR_TABLE) +#define FLASH_BASE ((uint32_t)m_boot_hdr_conf_start) +#elif defined(__GNUC__) +extern uint32_t __VECTOR_TABLE[]; +extern uint32_t __FLASH_BASE[]; +#define IMAGE_ENTRY_ADDRESS ((uint32_t)__VECTOR_TABLE) +#define FLASH_BASE ((uint32_t)__FLASH_BASE) +#endif + +#define DCD_ADDRESS dcd_data +#define BOOT_DATA_ADDRESS &boot_data +#define CSF_ADDRESS 0 +#define IVT_RSVD (uint32_t)(0x00000000) + +/************************************* + * Boot Data + *************************************/ +typedef struct _boot_data_ { + uint32_t start; /* boot start location */ + uint32_t size; /* size */ + uint32_t plugin; /* plugin flag - 1 if downloaded application is plugin */ + uint32_t placeholder; /* placehoder to make even 0x10 size */ +}BOOT_DATA_T; + +#if defined(BOARD_FLASH_SIZE) +#define FLASH_SIZE BOARD_FLASH_SIZE +#else +#error "Please define macro BOARD_FLASH_SIZE" +#endif +#define PLUGIN_FLAG (uint32_t)0 + +/* External Variables */ +const BOOT_DATA_T boot_data; +extern const uint8_t dcd_data[]; + +#endif /* __FLEXSPI_NOR_BOOT_H__ */ From 0f048a5a2a4cb239784cd152c9cd3fa0deb86db7 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 31 Jul 2022 11:09:57 +0200 Subject: [PATCH 2605/3533] mimxrt/machine_spi: Allow a setting of -1 for cs in the constructor. In that case, no Pin will be configured for the CS signal, even if it is internally still generated. That setting allows to use any pin for CS, which then must be controlled by the Python script. Also make the default cs=-1 to match other ports (software CS). --- docs/mimxrt/pinout.rst | 3 ++- docs/mimxrt/quickref.rst | 8 +++++++- ports/mimxrt/machine_spi.c | 13 ++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/mimxrt/pinout.rst b/docs/mimxrt/pinout.rst index b82c153fc3ea4..ef53fa63bff22 100644 --- a/docs/mimxrt/pinout.rst +++ b/docs/mimxrt/pinout.rst @@ -322,7 +322,8 @@ Olimex RT1010Py - CS0/-/SDO/SDI/SCK SDCARD wi Seeed ARCH MIX J4_12/-/J4_14/J4_13/J4_15 J3_09/J3_05/J3_08_J3_11 ================= ========================= ======================= =============== -Pins denoted with (*) are by default not wired at the board. +Pins denoted with (*) are by default not wired at the board. The CS0 and CS1 signals +are enabled with the keyword option cs=0 or cs=1 of the SPI object constructor. .. _mimxrt_i2c_pinout: diff --git a/docs/mimxrt/quickref.rst b/docs/mimxrt/quickref.rst index 0a14c632aafe5..c75fe60c8d66b 100644 --- a/docs/mimxrt/quickref.rst +++ b/docs/mimxrt/quickref.rst @@ -301,13 +301,19 @@ There are up to four hardware SPI channels that allow faster transmission rates (up to 30Mhz). Hardware SPI is accessed via the :ref:`machine.SPI ` class and has the same methods as software SPI above:: - from machine import SPI + from machine import SPI, Pin spi = SPI(0, 10000000) + cs_pin = Pin(6, Pin.OUT, value=1) + cs_pin(0) spi.write('Hello World') + cs_pin(1) For the assignment of Pins to SPI signals, refer to :ref:`Hardware SPI pinout `. +The keyword option cs=n can be used to enable the cs pin 0 or 1 for an automatic cs signal. The +default is cs=-1. Using cs=-1 the automatic cs signal is not created. +In that case, cs has to be set by the script. Clearing that assignment requires a power cycle. Notes: diff --git a/ports/mimxrt/machine_spi.c b/ports/mimxrt/machine_spi.c index 805ed9a0ac540..32bc77c34ddf6 100644 --- a/ports/mimxrt/machine_spi.c +++ b/ports/mimxrt/machine_spi.c @@ -77,7 +77,7 @@ static const iomux_table_t iomux_table[] = { IOMUX_TABLE_SPI }; -bool lpspi_set_iomux(int8_t spi, uint8_t drive, uint8_t cs) { +bool lpspi_set_iomux(int8_t spi, uint8_t drive, int8_t cs) { int index = (spi - 1) * 5; if (SCK.muxRegister != 0) { @@ -93,7 +93,7 @@ bool lpspi_set_iomux(int8_t spi, uint8_t drive, uint8_t cs) { IOMUXC_SetPinMux(CS1.muxRegister, CS1.muxMode, CS1.inputRegister, CS1.inputDaisy, CS1.configRegister, 0U); IOMUXC_SetPinConfig(CS1.muxRegister, CS1.muxMode, CS1.inputRegister, CS1.inputDaisy, CS1.configRegister, pin_generate_config(PIN_PULL_UP_100K, PIN_MODE_OUT, drive, CS1.configRegister)); - } else { + } else if (cs != -1) { mp_raise_ValueError(MP_ERROR_TEXT("The chosen CS is not available")); } @@ -131,7 +131,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} }, { MP_QSTR_gap_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_DRIVE} }, - { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, }; // Parse the arguments. @@ -173,8 +173,11 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } self->master_config->lastSckToPcsDelayInNanoSec = self->master_config->betweenTransferDelayInNanoSec; self->master_config->pcsToSckDelayInNanoSec = self->master_config->betweenTransferDelayInNanoSec; - uint8_t cs = args[ARG_cs].u_int; - if (cs <= 1) { + int8_t cs = args[ARG_cs].u_int; + // In the SPI master_config for automatic CS the value cs=0 is set already, + // so only cs=1 has to be addressed here. The case cs == -1 for manual CS is handled + // in the function spi_set_iomux() and the value in the master_config can stay at 0. + if (cs == 1) { self->master_config->whichPcs = cs; } LPSPI_MasterInit(self->spi_inst, self->master_config, BOARD_BOOTCLOCKRUN_LPSPI_CLK_ROOT); From 443fbcf81c05de00f35b90d8a443837d62073a63 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 18 Aug 2022 11:36:08 +0200 Subject: [PATCH 2606/3533] mimxrt/machine_uart: Set the UART ioctl write poll flag properly. It was always set to True. The change adds a check to the tx status flag which is set when all data is transferred. --- ports/mimxrt/machine_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 1b0c0633345af..ab17886926453 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -437,7 +437,7 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint ret |= MP_STREAM_POLL_RD; } } - if ((flags & MP_STREAM_POLL_WR)) { + if ((flags & MP_STREAM_POLL_WR) && (self->tx_status == kStatus_LPUART_TxIdle)) { ret |= MP_STREAM_POLL_WR; } } else if (request == MP_STREAM_FLUSH) { From 6472348c1b2559105e1b2f912a58566b9900e9c1 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 8 Sep 2022 14:24:32 +0200 Subject: [PATCH 2607/3533] mimxrt/machine_uart: Fix a bug in UART.write(). Causing an incomplete send if the data size was longer than the buffer size. --- ports/mimxrt/machine_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index ab17886926453..83382a6cf2bc1 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -395,7 +395,7 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin // Wait at least the number of character times for this chunk. t = ticks_us64() + (uint64_t)xfer.dataSize * (13000000 / self->config.baudRate_Bps + 1000); - while (self->handle.txDataSize) { + while (self->tx_status != kStatus_LPUART_TxIdle) { // Wait for the first/next character to be sent. if (ticks_us64() > t) { // timed out if (self->handle.txDataSize >= size) { From 74805435f9459ceae4ea6044a767de4a5479e6c8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Sep 2022 21:14:22 +1000 Subject: [PATCH 2608/3533] py/objpolyiter: Fix comment about finaliser method. Signed-off-by: Damien George --- py/objpolyiter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 16fd1f486c15f..dac6a2545575f 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -66,7 +66,7 @@ typedef struct _mp_obj_polymorph_iter_with_finaliser_t { STATIC mp_obj_t mp_obj_polymorph_iter_del(mp_obj_t self_in) { mp_obj_polymorph_with_finaliser_iter_t *self = MP_OBJ_TO_PTR(self_in); - // Redirect call to object instance's iternext method + // Redirect call to object instance's finaliser method return self->finaliser(self_in); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_obj_polymorph_iter_del_obj, mp_obj_polymorph_iter_del); From 0e8c2204da377e95b5000a3c708891d98cdeb69c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Sep 2022 14:03:37 +1000 Subject: [PATCH 2609/3533] esp32/mphalport: Fix calculation of large sleep by using 64-bit arith. Fixes issue #9304. Signed-off-by: Damien George --- ports/esp32/mphalport.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index aab534937e137..2aaeb9755f972 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -130,7 +130,7 @@ uint32_t mp_hal_ticks_us(void) { } void mp_hal_delay_ms(uint32_t ms) { - uint64_t us = ms * 1000; + uint64_t us = (uint64_t)ms * 1000ULL; uint64_t dt; uint64_t t0 = esp_timer_get_time(); for (;;) { @@ -139,7 +139,7 @@ void mp_hal_delay_ms(uint32_t ms) { MP_THREAD_GIL_EXIT(); uint64_t t1 = esp_timer_get_time(); dt = t1 - t0; - if (dt + portTICK_PERIOD_MS * 1000 >= us) { + if (dt + portTICK_PERIOD_MS * 1000ULL >= us) { // doing a vTaskDelay would take us beyond requested delay time taskYIELD(); MP_THREAD_GIL_ENTER(); From 45972fa5481aeef38d074360b0f72b212968a305 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Sep 2022 09:57:47 +1000 Subject: [PATCH 2610/3533] py/mkrules.mk: Add link to build troubleshooting on failure. Also update the submodules help text to match. Signed-off-by: Jim Mussared --- py/mkrules.mk | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/py/mkrules.mk b/py/mkrules.mk index 14f1b953cda40..a58bd2ee0e4d6 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -4,6 +4,9 @@ THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST)) include $(dir $(THIS_MAKEFILE))mkenv.mk endif +HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" +HELP_MPY_LIB_SUBMODULE ?= "\033[1;31mError: micropython-lib submodule is not initialized.\033[0m Run 'make submodules'" + # Extra deps that need to happen before object compilation. OBJ_EXTRA_ORDER_DEPS = @@ -53,7 +56,7 @@ $(BUILD)/%.o: %.s define compile_c $(ECHO) "CC $<" -$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $< +$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $< || (echo -e $(HELP_BUILD_ERROR); false) @# The following fixes the dependency file. @# See http://make.paulandlesley.org/autodep.html for details. @# Regex adjusted from the above to play better with Windows paths, etc. @@ -65,7 +68,7 @@ endef define compile_cxx $(ECHO) "CXX $<" -$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< +$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< || (echo -e $(HELP_BUILD_ERROR); false) @# The following fixes the dependency file. @# See http://make.paulandlesley.org/autodep.html for details. @# Regex adjusted from the above to play better with Windows paths, etc. @@ -182,7 +185,7 @@ endif # to build frozen_content.c from a manifest $(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h $(BUILD)/genhdr/root_pointers.h | $(MICROPY_MPYCROSS_DEPENDENCY) - $(Q)test -e "$(MPY_LIB_DIR)/README.md" || (echo "Error: micropython-lib not initialized. Run 'make submodules'"; false) + $(Q)test -e "$(MPY_LIB_DIR)/README.md" || (echo -e $(HELP_MPY_LIB_SUBMODULE); false) $(Q)$(MAKE_MANIFEST) -o $@ -v "MPY_DIR=$(TOP)" -v "MPY_LIB_DIR=$(MPY_LIB_DIR)" -v "PORT_DIR=$(shell pwd)" -v "BOARD_DIR=$(BOARD_DIR)" -b "$(BUILD)" $(if $(MPY_CROSS_FLAGS),-f"$(MPY_CROSS_FLAGS)",) --mpy-tool-flags="$(MPY_TOOL_FLAGS)" $(FROZEN_MANIFEST) endif From 18d0e6d0dbdb34c6cac5aeeb9c4424821cd9f16a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Sep 2022 10:08:48 +1000 Subject: [PATCH 2611/3533] esp32/Makefile: Add link to build troubleshooting on failure. Signed-off-by: Jim Mussared --- ports/esp32/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index b7d804d0730e7..e43cad751b216 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -34,8 +34,10 @@ ifdef FROZEN_MANIFEST IDFPY_FLAGS += -D MICROPY_FROZEN_MANIFEST=$(FROZEN_MANIFEST) endif +HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" + all: - idf.py $(IDFPY_FLAGS) build + idf.py $(IDFPY_FLAGS) build || (echo -e $(HELP_BUILD_ERROR); false) @$(PYTHON) makeimg.py \ $(BUILD)/sdkconfig \ $(BUILD)/bootloader/bootloader.bin \ From ca51d63c37e6ca67bec0a645e2aeea71aba91058 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Sep 2022 10:08:59 +1000 Subject: [PATCH 2612/3533] rp2/Makefile: Add link to build troubleshooting on failure. Signed-off-by: Jim Mussared --- ports/rp2/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 6d2fc009642d8..c603f5403fcac 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -22,9 +22,11 @@ ifeq ($(DEBUG),1) CMAKE_ARGS += -DCMAKE_BUILD_TYPE=Debug endif +HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" + all: [ -e $(BUILD)/Makefile ] || cmake -S . -B $(BUILD) -DPICO_BUILD_DOCS=0 ${CMAKE_ARGS} - $(MAKE) $(MAKESILENT) -C $(BUILD) + $(MAKE) $(MAKESILENT) -C $(BUILD) || (echo -e $(HELP_BUILD_ERROR); false) clean: $(RM) -rf $(BUILD) From fb2a57800acffd811d05373dcf63e95b4048d0c6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Jul 2021 14:31:06 +1000 Subject: [PATCH 2613/3533] all: Simplify buffer protocol to just a "get buffer" callback. The buffer protocol type only has a single member, and this existing layout creates problems for the upcoming split/slot-index mp_obj_type_t layout optimisations. If we need to make the buffer protocol more sophisticated in the future either we can rely on the mp_obj_type_t optimisations to just add additional slots to mp_obj_type_t or re-visit the buffer protocol then. This change is a no-op in terms of generated code. Signed-off-by: Jim Mussared --- examples/natmod/framebuf/framebuf.c | 2 +- extmod/modbluetooth.c | 2 +- extmod/modframebuf.c | 2 +- extmod/moductypes.c | 2 +- ports/nrf/boards/microbit/modules/iters.c | 1 - ports/nrf/boards/microbit/modules/microbitdisplay.c | 1 - ports/nrf/boards/microbit/modules/microbitimage.c | 1 - ports/unix/modffi.c | 4 ++-- py/obj.c | 4 ++-- py/obj.h | 6 ++---- py/objarray.c | 6 +++--- py/objstr.c | 4 ++-- py/objstrunicode.c | 2 +- py/objtype.c | 6 +++--- 14 files changed, 19 insertions(+), 24 deletions(-) diff --git a/examples/natmod/framebuf/framebuf.c b/examples/natmod/framebuf/framebuf.c index 53fb90c625ada..2eff61c817d22 100644 --- a/examples/natmod/framebuf/framebuf.c +++ b/examples/natmod/framebuf/framebuf.c @@ -21,7 +21,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a mp_type_framebuf.base.type = (void*)&mp_type_type; mp_type_framebuf.name = MP_QSTR_FrameBuffer; mp_type_framebuf.make_new = framebuf_make_new; - mp_type_framebuf.buffer_p.get_buffer = framebuf_get_buffer; + mp_type_framebuf.buffer = framebuf_get_buffer; framebuf_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill), MP_OBJ_FROM_PTR(&framebuf_fill_obj) }; framebuf_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill_rect), MP_OBJ_FROM_PTR(&framebuf_fill_rect_obj) }; framebuf_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_pixel), MP_OBJ_FROM_PTR(&framebuf_pixel_obj) }; diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index bd4d9b71790c1..4cc57b3d55ab8 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -248,7 +248,7 @@ const mp_obj_type_t mp_type_bluetooth_uuid = { .binary_op = bluetooth_uuid_binary_op, .locals_dict = NULL, .print = bluetooth_uuid_print, - .buffer_p = { .get_buffer = bluetooth_uuid_get_buffer }, + .buffer = bluetooth_uuid_get_buffer, }; // ---------------------------------------------------------------------------- diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index bb1f4f6310774..9542d4303e09d 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -833,7 +833,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = { { &mp_type_type }, .name = MP_QSTR_FrameBuffer, .make_new = framebuf_make_new, - .buffer_p = { .get_buffer = framebuf_get_buffer }, + .buffer = framebuf_get_buffer, .locals_dict = (mp_obj_dict_t *)&framebuf_locals_dict, }; #endif diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 547453c48e9fd..25578dd6b0710 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -642,7 +642,7 @@ STATIC const mp_obj_type_t uctypes_struct_type = { .attr = uctypes_struct_attr, .subscr = uctypes_struct_subscr, .unary_op = uctypes_struct_unary_op, - .buffer_p = { .get_buffer = uctypes_get_buffer }, + .buffer = uctypes_get_buffer, }; STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { diff --git a/ports/nrf/boards/microbit/modules/iters.c b/ports/nrf/boards/microbit/modules/iters.c index 8ae4b698fe92e..e9c4ae3c72bc8 100644 --- a/ports/nrf/boards/microbit/modules/iters.c +++ b/ports/nrf/boards/microbit/modules/iters.c @@ -55,7 +55,6 @@ const mp_obj_type_t microbit_repeat_iterator_type = { .subscr = NULL, .getiter = mp_identity_getiter, .iternext = microbit_repeat_iter_next, - .buffer_p = {NULL}, MP_OBJ_NULL }; diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index b35703ad6f9a7..04b8602879b8d 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -554,7 +554,6 @@ const mp_obj_type_t microbit_display_type = { .subscr = NULL, .getiter = NULL, .iternext = NULL, - .buffer_p = {NULL}, .locals_dict = (mp_obj_dict_t*)µbit_display_locals_dict, }; diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index a028df8282a96..5d2896c38346a 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -690,7 +690,6 @@ const mp_obj_type_t microbit_image_type = { .subscr = NULL, .getiter = NULL, .iternext = NULL, - .buffer_p = {NULL}, .locals_dict = (mp_obj_dict_t*)µbit_image_locals_dict, }; diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index d82d5270fa34f..6417b5d3b395c 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -505,10 +505,10 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const } else if (mp_obj_is_str(a)) { const char *s = mp_obj_str_get_str(a); values[i].ffi = (ffi_arg)(intptr_t)s; - } else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer_p.get_buffer != NULL) { + } else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer != NULL) { mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a); mp_buffer_info_t bufinfo; - int ret = o->type->buffer_p.get_buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ? + int ret = o->type->buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ? if (ret != 0) { goto error; } diff --git a/py/obj.c b/py/obj.c index b461fe50aa25d..eeadd3eedddae 100644 --- a/py/obj.c +++ b/py/obj.c @@ -579,10 +579,10 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) { bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { const mp_obj_type_t *type = mp_obj_get_type(obj); - if (type->buffer_p.get_buffer == NULL) { + if (type->buffer == NULL) { return false; } - int ret = type->buffer_p.get_buffer(obj, bufinfo, flags); + int ret = type->buffer(obj, bufinfo, flags); if (ret != 0) { return false; } diff --git a/py/obj.h b/py/obj.h index 8949830282256..0bf5b58f8155f 100644 --- a/py/obj.h +++ b/py/obj.h @@ -557,9 +557,7 @@ typedef struct _mp_buffer_info_t { #define MP_BUFFER_READ (1) #define MP_BUFFER_WRITE (2) #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE) -typedef struct _mp_buffer_p_t { - mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); -} mp_buffer_p_t; +typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); @@ -618,7 +616,7 @@ struct _mp_obj_type_t { mp_fun_1_t iternext; // Implements the buffer protocol if supported by this type. - mp_buffer_p_t buffer_p; + mp_buffer_fun_t buffer; // One of disjoint protocols (interfaces), like mp_stream_p_t, etc. const void *protocol; diff --git a/py/objarray.c b/py/objarray.c index ecaffeb6a22e7..dca41c2931d66 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -580,7 +580,7 @@ const mp_obj_type_t mp_type_array = { .unary_op = array_unary_op, .binary_op = array_binary_op, .subscr = array_subscr, - .buffer_p = { .get_buffer = array_get_buffer }, + .buffer_p = array_get_buffer, .locals_dict = (mp_obj_dict_t *)&mp_obj_array_locals_dict, }; #endif @@ -596,7 +596,7 @@ const mp_obj_type_t mp_type_bytearray = { .unary_op = array_unary_op, .binary_op = array_binary_op, .subscr = array_subscr, - .buffer_p = { .get_buffer = array_get_buffer }, + .buffer = array_get_buffer, .locals_dict = (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict, }; #endif @@ -617,7 +617,7 @@ const mp_obj_type_t mp_type_memoryview = { .locals_dict = (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, #endif .subscr = array_subscr, - .buffer_p = { .get_buffer = array_get_buffer }, + .buffer = array_get_buffer, }; #endif diff --git a/py/objstr.c b/py/objstr.c index 683d035e44090..9dd7f65e66cae 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2151,7 +2151,7 @@ const mp_obj_type_t mp_type_str = { .binary_op = mp_obj_str_binary_op, .subscr = bytes_subscr, .getiter = mp_obj_new_str_iterator, - .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, + .buffer = mp_obj_str_get_buffer, .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, }; #endif // !MICROPY_PY_BUILTINS_STR_UNICODE @@ -2165,7 +2165,7 @@ const mp_obj_type_t mp_type_bytes = { .binary_op = mp_obj_str_binary_op, .subscr = bytes_subscr, .getiter = mp_obj_new_bytes_iterator, - .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, + .buffer = mp_obj_str_get_buffer, .locals_dict = (mp_obj_dict_t *)&mp_obj_bytes_locals_dict, }; diff --git a/py/objstrunicode.c b/py/objstrunicode.c index d36dd8b1375a2..fef0353683c19 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -238,7 +238,7 @@ const mp_obj_type_t mp_type_str = { .binary_op = mp_obj_str_binary_op, .subscr = str_subscr, .getiter = mp_obj_new_str_iterator, - .buffer_p = { .get_buffer = mp_obj_str_get_buffer }, + .buffer = mp_obj_str_get_buffer, .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, }; diff --git a/py/objtype.c b/py/objtype.c index fe1918bd3731f..c0f68578092dc 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -913,14 +913,14 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, struct class_lookup_data lookup = { .obj = self, .attr = MP_QSTR_, // don't actually look for a method - .meth_offset = offsetof(mp_obj_type_t, buffer_p.get_buffer), + .meth_offset = offsetof(mp_obj_type_t, buffer), .dest = member, .is_type = false, }; mp_obj_class_lookup(&lookup, self->base.type); if (member[0] == MP_OBJ_SENTINEL) { const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]); - return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags); + return type->buffer(self->subobj[0], bufinfo, flags); } else { return 1; // object does not support buffer protocol } @@ -1160,7 +1160,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) o->subscr = instance_subscr; o->getiter = mp_obj_instance_getiter; // o->iternext = ; not implemented - o->buffer_p.get_buffer = instance_get_buffer; + o->buffer = instance_get_buffer; if (bases_len > 0) { // Inherit protocol from a base class. This allows to define an From 42587c78702e0a96ca025e66267201f27389bb17 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Jul 2021 14:06:29 +1000 Subject: [PATCH 2614/3533] all: Standardise mp_obj_type_t initialisation. Remove setting unused slots. Signed-off-by: Jim Mussared --- extmod/vfs_fat.c | 1 - ports/nrf/boards/microbit/modules/iters.c | 8 ---- .../boards/microbit/modules/microbitdisplay.c | 9 ---- .../boards/microbit/modules/microbitimage.c | 42 ------------------- 4 files changed, 60 deletions(-) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 7d8b51efeb617..8d2f13be476d8 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -437,7 +437,6 @@ const mp_obj_type_t mp_fat_vfs_type = { .make_new = fat_vfs_make_new, .protocol = &fat_vfs_proto, .locals_dict = (mp_obj_dict_t *)&fat_vfs_locals_dict, - }; #endif // MICROPY_VFS_FAT diff --git a/ports/nrf/boards/microbit/modules/iters.c b/ports/nrf/boards/microbit/modules/iters.c index e9c4ae3c72bc8..66f9f6c7f3ea0 100644 --- a/ports/nrf/boards/microbit/modules/iters.c +++ b/ports/nrf/boards/microbit/modules/iters.c @@ -46,16 +46,8 @@ static mp_obj_t microbit_repeat_iter_next(mp_obj_t iter_in) { const mp_obj_type_t microbit_repeat_iterator_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .print = NULL, - .make_new = NULL, - .call = NULL, - .unary_op = NULL, - .binary_op = NULL, - .attr = NULL, - .subscr = NULL, .getiter = mp_identity_getiter, .iternext = microbit_repeat_iter_next, - MP_OBJ_NULL }; mp_obj_t microbit_repeat_iterator(mp_obj_t iterable) { diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index 04b8602879b8d..93ba9772fe98c 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -545,15 +545,6 @@ STATIC MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_local const mp_obj_type_t microbit_display_type = { { &mp_type_type }, .name = MP_QSTR_MicroBitDisplay, - .print = NULL, - .make_new = NULL, - .call = NULL, - .unary_op = NULL, - .binary_op = NULL, - .attr = NULL, - .subscr = NULL, - .getiter = NULL, - .iternext = NULL, .locals_dict = (mp_obj_dict_t*)µbit_display_locals_dict, }; diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index 5d2896c38346a..eb44660b974ce 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -683,13 +683,7 @@ const mp_obj_type_t microbit_image_type = { .name = MP_QSTR_MicroBitImage, .print = microbit_image_print, .make_new = microbit_image_make_new, - .call = NULL, - .unary_op = NULL, .binary_op = image_binary_op, - .attr = NULL, - .subscr = NULL, - .getiter = NULL, - .iternext = NULL, .locals_dict = (mp_obj_dict_t*)µbit_image_locals_dict, }; @@ -829,33 +823,14 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { const mp_obj_type_t microbit_scrolling_string_type = { { &mp_type_type }, .name = MP_QSTR_ScrollingString, - .print = NULL, - .make_new = NULL, - .call = NULL, - .unary_op = NULL, - .binary_op = NULL, - .attr = NULL, - .subscr = NULL, .getiter = get_microbit_scrolling_string_iter, - .iternext = NULL, - .buffer_p = {NULL}, - .locals_dict = NULL, }; const mp_obj_type_t microbit_scrolling_string_iterator_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .print = NULL, - .make_new = NULL, - .call = NULL, - .unary_op = NULL, - .binary_op = NULL, - .attr = NULL, - .subscr = NULL, .getiter = mp_identity_getiter, .iternext = microbit_scrolling_string_iter_next, - .buffer_p = {NULL}, - .locals_dict = NULL, }; /** Facade types to present a string as a sequence of images. @@ -897,17 +872,9 @@ static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t const mp_obj_type_t string_image_facade_type = { { &mp_type_type }, .name = MP_QSTR_Facade, - .print = NULL, - .make_new = NULL, - .call = NULL, .unary_op = facade_unary_op, - .binary_op = NULL, - .attr = NULL, .subscr = string_image_facade_subscr, .getiter = microbit_facade_iterator, - .iternext = NULL, - .buffer_p = {NULL}, - NULL }; @@ -940,17 +907,8 @@ static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) { const mp_obj_type_t microbit_facade_iterator_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .print = NULL, - .make_new = NULL, - .call = NULL, - .unary_op = NULL, - .binary_op = NULL, - .attr = NULL, - .subscr = NULL, .getiter = mp_identity_getiter, .iternext = microbit_facade_iter_next, - .buffer_p = {NULL}, - NULL }; mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) { From cdb880789f61ee037cc7905ad75a7a9201d12ba5 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Jul 2021 13:38:27 +1000 Subject: [PATCH 2615/3533] py/obj: Add macro to declare ROM mp_obj_type_t instances. This will allow the structure of mp_obj_type_t to change while keeping the definition code the same. Signed-off-by: Jim Mussared --- py/obj.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/py/obj.h b/py/obj.h index 0bf5b58f8155f..4ab7e0dc09c13 100644 --- a/py/obj.h +++ b/py/obj.h @@ -513,6 +513,7 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); // operator and not the __ne__ operator. If it's set then __ne__ may be implemented. // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg. // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type. +#define MP_TYPE_FLAG_NONE (0x0000) #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001) #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002) #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004) @@ -631,6 +632,40 @@ struct _mp_obj_type_t { struct _mp_obj_dict_t *locals_dict; }; +#define MP_TYPE_NULL_MAKE_NEW (NULL) + +// Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments. +// Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE. +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, _make_new, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11 } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11, .f12 = v12 } + +// Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked +#define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x + +// This macro evaluates to MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N, where N is the value +// of the 30th argument (30 is 13*2 + 4). +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N + +// This macro is used to define a object type in ROM. +// Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*) +// It uses the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_* +// macro to use based on the number of arguments. It works by shifting the +// numeric values 12, 11, ... 0 by the number of arguments, such that the +// 30th argument ends up being the number to use. The _INV values are +// placeholders because the slot arguments come in pairs. +#define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__)) + // Constant types, globally accessible extern const mp_obj_type_t mp_type_type; extern const mp_obj_type_t mp_type_object; From 662b9761b37b054f08fe2f7c00d0fce3a418d0b0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Jul 2021 14:38:38 +1000 Subject: [PATCH 2616/3533] all: Make all mp_obj_type_t defs use MP_DEFINE_CONST_OBJ_TYPE. In preparation for upcoming rework of mp_obj_type_t layout. Signed-off-by: Jim Mussared --- extmod/machine_i2c.c | 17 +-- extmod/machine_mem.c | 14 ++- extmod/machine_pinbase.c | 13 +- extmod/machine_pwm.c | 15 +-- extmod/machine_signal.c | 17 +-- extmod/machine_spi.c | 17 +-- extmod/modbluetooth.c | 33 ++--- extmod/modbtree.c | 22 ++-- extmod/modframebuf.c | 15 +-- extmod/modlwip.c | 30 ++--- extmod/moduasyncio.c | 30 ++--- extmod/moducryptolib.c | 13 +- extmod/moductypes.c | 21 ++-- extmod/moduhashlib.c | 39 +++--- extmod/modure.c | 28 +++-- extmod/moduselect.c | 16 +-- extmod/modusocket.c | 17 +-- extmod/modussl_axtls.c | 20 ++-- extmod/modussl_mbedtls.c | 20 ++-- extmod/modutimeq.c | 15 +-- extmod/moduwebsocket.c | 15 +-- extmod/moduzlib.c | 15 +-- extmod/modwebrepl.c | 15 +-- extmod/network_cyw43.c | 15 +-- extmod/network_ninaw10.c | 15 ++- extmod/network_wiznet5k.c | 28 +++-- extmod/vfs_fat.c | 15 +-- extmod/vfs_fat_file.c | 40 ++++--- extmod/vfs_lfsx.c | 17 +-- extmod/vfs_lfsx_file.c | 40 ++++--- extmod/vfs_posix.c | 15 +-- extmod/vfs_posix_file.c | 40 ++++--- ports/cc3200/misc/mpirq.c | 14 ++- ports/cc3200/mods/modnetwork.c | 13 +- ports/cc3200/mods/moduhashlib.c | 28 +++-- ports/cc3200/mods/modusocket.c | 15 +-- ports/cc3200/mods/modussl.c | 18 +-- ports/cc3200/mods/modwlan.c | 15 ++- ports/cc3200/mods/pybadc.c | 31 ++--- ports/cc3200/mods/pybflash.c | 13 +- ports/cc3200/mods/pybi2c.c | 15 +-- ports/cc3200/mods/pybpin.c | 31 ++--- ports/cc3200/mods/pybrtc.c | 13 +- ports/cc3200/mods/pybsd.c | 13 +- ports/cc3200/mods/pybsleep.c | 10 +- ports/cc3200/mods/pybspi.c | 15 +-- ports/cc3200/mods/pybtimer.c | 29 +++-- ports/cc3200/mods/pybuart.c | 21 ++-- ports/cc3200/mods/pybwdt.c | 13 +- ports/esp32/esp32_nvs.c | 15 +-- ports/esp32/esp32_partition.c | 15 +-- ports/esp32/esp32_rmt.c | 15 +-- ports/esp32/esp32_ulp.c | 13 +- ports/esp32/machine_adc.c | 15 +-- ports/esp32/machine_adcblock.c | 15 +-- ports/esp32/machine_dac.c | 15 +-- ports/esp32/machine_hw_spi.c | 17 +-- ports/esp32/machine_i2c.c | 17 +-- ports/esp32/machine_i2s.c | 21 ++-- ports/esp32/machine_pin.c | 33 ++--- ports/esp32/machine_rtc.c | 13 +- ports/esp32/machine_sdcard.c | 13 +- ports/esp32/machine_timer.c | 15 +-- ports/esp32/machine_touchpad.c | 13 +- ports/esp32/machine_uart.c | 21 ++-- ports/esp32/machine_wdt.c | 13 +- ports/esp32/modsocket.c | 15 +-- ports/esp32/network_lan.c | 12 +- ports/esp32/network_ppp.c | 12 +- ports/esp32/network_wlan.c | 20 ++-- ports/esp8266/machine_adc.c | 15 +-- ports/esp8266/machine_hspi.c | 17 +-- ports/esp8266/machine_pin.c | 33 ++--- ports/esp8266/machine_rtc.c | 13 +- ports/esp8266/machine_uart.c | 21 ++-- ports/esp8266/machine_wdt.c | 13 +- ports/esp8266/modmachine.c | 15 +-- ports/esp8266/modnetwork.c | 12 +- ports/mimxrt/machine_adc.c | 15 +-- ports/mimxrt/machine_i2c.c | 17 +-- ports/mimxrt/machine_i2s.c | 21 ++-- ports/mimxrt/machine_led.c | 15 +-- ports/mimxrt/machine_pin.c | 61 +++++----- ports/mimxrt/machine_rtc.c | 13 +- ports/mimxrt/machine_sdcard.c | 13 +- ports/mimxrt/machine_spi.c | 17 +-- ports/mimxrt/machine_timer.c | 15 +-- ports/mimxrt/machine_uart.c | 21 ++-- ports/mimxrt/machine_wdt.c | 13 +- ports/mimxrt/mimxrt_flash.c | 13 +- ports/mimxrt/network_lan.c | 16 +-- ports/nrf/boards/microbit/modules/iters.c | 14 ++- .../boards/microbit/modules/microbitdisplay.c | 12 +- .../boards/microbit/modules/microbitimage.c | 73 ++++++----- ports/nrf/modules/board/led.c | 15 +-- ports/nrf/modules/machine/adc.c | 15 +-- ports/nrf/modules/machine/i2c.c | 17 +-- ports/nrf/modules/machine/pin.c | 31 ++--- ports/nrf/modules/machine/pwm.c | 15 +-- ports/nrf/modules/machine/rtcounter.c | 15 +-- ports/nrf/modules/machine/spi.c | 17 +-- ports/nrf/modules/machine/temp.c | 15 +-- ports/nrf/modules/machine/timer.c | 15 +-- ports/nrf/modules/machine/uart.c | 21 ++-- ports/nrf/modules/nrf/flashbdev.c | 15 +-- .../modules/ubluepy/ubluepy_characteristic.c | 15 +-- ports/nrf/modules/ubluepy/ubluepy_constants.c | 24 ++-- ports/nrf/modules/ubluepy/ubluepy_delegate.c | 15 +-- .../nrf/modules/ubluepy/ubluepy_descriptor.c | 15 +-- .../nrf/modules/ubluepy/ubluepy_peripheral.c | 15 +-- .../nrf/modules/ubluepy/ubluepy_scan_entry.c | 14 ++- ports/nrf/modules/ubluepy/ubluepy_scanner.c | 15 +-- ports/nrf/modules/ubluepy/ubluepy_service.c | 15 +-- ports/nrf/modules/ubluepy/ubluepy_uuid.c | 15 +-- ports/nrf/modules/uos/microbitfs.c | 28 +++-- ports/nrf/pin_named_pins.c | 28 +++-- ports/pic16bit/modpybled.c | 15 +-- ports/pic16bit/modpybswitch.c | 17 +-- ports/renesas-ra/extint.c | 15 +-- ports/renesas-ra/led.c | 15 +-- ports/renesas-ra/machine_adc.c | 15 +-- ports/renesas-ra/machine_i2c.c | 17 +-- ports/renesas-ra/machine_pin.c | 45 +++---- ports/renesas-ra/machine_rtc.c | 13 +- ports/renesas-ra/machine_spi.c | 17 +-- ports/renesas-ra/machine_timer.c | 15 +-- ports/renesas-ra/machine_uart.c | 21 ++-- ports/renesas-ra/storage.c | 15 +-- ports/renesas-ra/timer.c | 29 +++-- ports/renesas-ra/usrsw.c | 17 +-- ports/rp2/machine_adc.c | 15 +-- ports/rp2/machine_i2c.c | 17 +-- ports/rp2/machine_i2s.c | 21 ++-- ports/rp2/machine_pin.c | 19 +-- ports/rp2/machine_rtc.c | 13 +- ports/rp2/machine_spi.c | 17 +-- ports/rp2/machine_timer.c | 15 +-- ports/rp2/machine_uart.c | 21 ++-- ports/rp2/machine_wdt.c | 13 +- ports/rp2/rp2_flash.c | 13 +- ports/rp2/rp2_pio.c | 30 ++--- ports/samd/machine_led.c | 17 +-- ports/samd/machine_pin.c | 19 +-- ports/samd/samd_flash.c | 13 +- ports/stm32/accel.c | 13 +- ports/stm32/adc.c | 28 +++-- ports/stm32/dac.c | 15 +-- ports/stm32/extint.c | 15 +-- ports/stm32/lcd.c | 13 +- ports/stm32/led.c | 15 +-- ports/stm32/machine_adc.c | 15 +-- ports/stm32/machine_i2c.c | 17 +-- ports/stm32/machine_i2s.c | 21 ++-- ports/stm32/machine_spi.c | 17 +-- ports/stm32/machine_timer.c | 15 +-- ports/stm32/machine_uart.c | 21 ++-- ports/stm32/network_lan.c | 15 +-- ports/stm32/pin.c | 33 ++--- ports/stm32/pin_named_pins.c | 24 ++-- ports/stm32/pyb_can.c | 17 +-- ports/stm32/pyb_i2c.c | 15 +-- ports/stm32/pyb_spi.c | 17 +-- ports/stm32/rtc.c | 13 +- ports/stm32/sdcard.c | 26 ++-- ports/stm32/servo.c | 15 +-- ports/stm32/storage.c | 15 +-- ports/stm32/timer.c | 29 +++-- ports/stm32/usb.c | 36 +++--- ports/stm32/usrsw.c | 17 +-- ports/stm32/wdt.c | 13 +- ports/teensy/led.c | 15 +-- ports/teensy/timer.c | 29 +++-- ports/teensy/uart.c | 15 +-- ports/unix/coverage.c | 26 ++-- ports/unix/modffi.c | 69 ++++++----- ports/unix/modjni.c | 58 +++++---- ports/unix/moduselect.c | 16 +-- ports/unix/modusocket.c | 21 ++-- ports/zephyr/machine_i2c.c | 17 +-- ports/zephyr/machine_pin.c | 19 +-- ports/zephyr/machine_spi.c | 17 +-- ports/zephyr/machine_uart.c | 21 ++-- ports/zephyr/modusocket.c | 17 +-- ports/zephyr/modzsensor.c | 13 +- ports/zephyr/zephyr_storage.c | 30 ++--- py/builtinevex.c | 10 +- py/modio.c | 28 +++-- py/modthread.c | 12 +- py/objarray.c | 87 +++++++------- py/objattrtuple.c | 23 ++-- py/objbool.c | 19 +-- py/objboundmeth.c | 16 +-- py/objcell.c | 10 +- py/objclosure.c | 17 +-- py/objcomplex.c | 17 ++- py/objdeque.c | 15 +-- py/objdict.c | 78 ++++++------ py/objenumerate.c | 15 +-- py/objexcept.c | 15 +-- py/objfilter.c | 15 +-- py/objfloat.c | 15 +-- py/objfun.c | 113 ++++++++---------- py/objgenerator.c | 54 +++++---- py/objgetitemiter.c | 14 ++- py/objint.c | 19 +-- py/objlist.c | 24 ++-- py/objmap.c | 15 +-- py/objmodule.c | 14 ++- py/objnone.c | 14 ++- py/objobject.c | 13 +- py/objpolyiter.c | 31 ++--- py/objproperty.c | 13 +- py/objrange.c | 37 +++--- py/objreversed.c | 15 +-- py/objset.c | 43 +++---- py/objsingleton.c | 11 +- py/objslice.c | 16 +-- py/objstr.c | 48 ++++---- py/objstringio.c | 42 +++---- py/objstrunicode.c | 25 ++-- py/objtuple.c | 23 ++-- py/objtype.c | 58 ++++----- py/objzip.c | 15 +-- py/profile.c | 32 ++--- py/runtime.c | 13 +- shared/runtime/mpirq.c | 14 ++- shared/runtime/sys_stdio_mphal.c | 40 ++++--- 227 files changed, 2547 insertions(+), 2188 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index ff597b58c5e5c..7e597b791029b 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -730,13 +730,14 @@ STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = { .transfer = mp_machine_soft_i2c_transfer, }; -const mp_obj_type_t mp_machine_soft_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_SoftI2C, - .print = mp_machine_soft_i2c_print, - .make_new = mp_machine_soft_i2c_make_new, - .protocol = &mp_machine_soft_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_machine_soft_i2c_type, + MP_QSTR_SoftI2C, + MP_TYPE_FLAG_NONE, + mp_machine_soft_i2c_make_new, + print, mp_machine_soft_i2c_print, + protocol, &mp_machine_soft_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); #endif // MICROPY_PY_MACHINE_SOFTI2C diff --git a/extmod/machine_mem.c b/extmod/machine_mem.c index 73e2f7fd1fcb0..422e99d3ce42a 100644 --- a/extmod/machine_mem.c +++ b/extmod/machine_mem.c @@ -101,12 +101,14 @@ STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t va } } -const mp_obj_type_t machine_mem_type = { - { &mp_type_type }, - .name = MP_QSTR_mem, - .print = machine_mem_print, - .subscr = machine_mem_subscr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_mem_type, + MP_QSTR_mem, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, machine_mem_print, + subscr, machine_mem_subscr + ); const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1}; const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2}; diff --git a/extmod/machine_pinbase.c b/extmod/machine_pinbase.c index 070c5cde9d3ac..617dd1280c784 100644 --- a/extmod/machine_pinbase.c +++ b/extmod/machine_pinbase.c @@ -77,11 +77,12 @@ STATIC const mp_pin_p_t pinbase_pin_p = { .ioctl = pinbase_ioctl, }; -const mp_obj_type_t machine_pinbase_type = { - { &mp_type_type }, - .name = MP_QSTR_PinBase, - .make_new = pinbase_make_new, - .protocol = &pinbase_pin_p, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pinbase_type, + MP_QSTR_PinBase, + MP_TYPE_FLAG_NONE, + pinbase_make_new, + protocol, &pinbase_pin_p + ); #endif // MICROPY_PY_MACHINE diff --git a/extmod/machine_pwm.c b/extmod/machine_pwm.c index ddf49c1358a5d..f12f70a2d14d4 100644 --- a/extmod/machine_pwm.c +++ b/extmod/machine_pwm.c @@ -132,12 +132,13 @@ STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict, machine_pwm_locals_dict_table); -const mp_obj_type_t machine_pwm_type = { - { &mp_type_type }, - .name = MP_QSTR_PWM, - .print = mp_machine_pwm_print, - .make_new = mp_machine_pwm_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_pwm_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pwm_type, + MP_QSTR_PWM, + MP_TYPE_FLAG_NONE, + mp_machine_pwm_make_new, + print, mp_machine_pwm_print, + locals_dict, &machine_pwm_locals_dict + ); #endif // MICROPY_PY_MACHINE_PWM diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index cf7550a2e084a..818bc01c27112 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -172,13 +172,14 @@ STATIC const mp_pin_p_t signal_pin_p = { .ioctl = signal_ioctl, }; -const mp_obj_type_t machine_signal_type = { - { &mp_type_type }, - .name = MP_QSTR_Signal, - .make_new = signal_make_new, - .call = signal_call, - .protocol = &signal_pin_p, - .locals_dict = (void *)&signal_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_signal_type, + MP_QSTR_Signal, + MP_TYPE_FLAG_NONE, + signal_make_new, + call, signal_call, + protocol, &signal_pin_p, + locals_dict, (void *)&signal_locals_dict + ); #endif // MICROPY_PY_MACHINE diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index ba533b2a651a0..43148c9c8d46d 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -251,13 +251,14 @@ const mp_machine_spi_p_t mp_machine_soft_spi_p = { .transfer = mp_machine_soft_spi_transfer, }; -const mp_obj_type_t mp_machine_soft_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SoftSPI, - .print = mp_machine_soft_spi_print, - .make_new = mp_machine_soft_spi_make_new, - .protocol = &mp_machine_soft_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_machine_soft_spi_type, + MP_QSTR_SoftSPI, + MP_TYPE_FLAG_NONE, + mp_machine_soft_spi_make_new, + print, mp_machine_soft_spi_print, + protocol, &mp_machine_soft_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); #endif // MICROPY_PY_MACHINE_SOFTSPI diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 4cc57b3d55ab8..2b7497e1b6551 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -240,16 +240,16 @@ STATIC void ringbuf_get_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) #endif // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS -const mp_obj_type_t mp_type_bluetooth_uuid = { - { &mp_type_type }, - .name = MP_QSTR_UUID, - .make_new = bluetooth_uuid_make_new, - .unary_op = bluetooth_uuid_unary_op, - .binary_op = bluetooth_uuid_binary_op, - .locals_dict = NULL, - .print = bluetooth_uuid_print, - .buffer = bluetooth_uuid_get_buffer, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bluetooth_uuid, + MP_QSTR_UUID, + MP_TYPE_FLAG_NONE, + bluetooth_uuid_make_new, + unary_op, bluetooth_uuid_unary_op, + binary_op, bluetooth_uuid_binary_op, + print, bluetooth_uuid_print, + buffer, bluetooth_uuid_get_buffer + ); // ---------------------------------------------------------------------------- // Bluetooth object: General @@ -976,12 +976,13 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table); -STATIC const mp_obj_type_t mp_type_bluetooth_ble = { - { &mp_type_type }, - .name = MP_QSTR_BLE, - .make_new = bluetooth_ble_make_new, - .locals_dict = (void *)&bluetooth_ble_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bluetooth_ble, + MP_QSTR_BLE, + MP_TYPE_FLAG_NONE, + bluetooth_ble_make_new, + locals_dict, (void *)&bluetooth_ble_locals_dict + ); STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluetooth) }, diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 7a1daacb41669..f115be44fec15 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -319,17 +319,19 @@ STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table); -STATIC const mp_obj_type_t btree_type = { - { &mp_type_type }, +STATIC MP_DEFINE_CONST_OBJ_TYPE( + btree_type, + MP_QSTR_btree, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module - .name = MP_QSTR_btree, - .print = btree_print, - .getiter = btree_getiter, - .iternext = btree_iternext, - .binary_op = btree_binary_op, - .subscr = btree_subscr, - .locals_dict = (void *)&btree_locals_dict, -}; + print, btree_print, + getiter, btree_getiter, + iternext, btree_iternext, + binary_op, btree_binary_op, + subscr, btree_subscr, + locals_dict, (void *)&btree_locals_dict + ); #endif STATIC const FILEVTABLE btree_stream_fvtable = { diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 9542d4303e09d..1d44312cf3040 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -829,13 +829,14 @@ STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(framebuf_locals_dict, framebuf_locals_dict_table); -STATIC const mp_obj_type_t mp_type_framebuf = { - { &mp_type_type }, - .name = MP_QSTR_FrameBuffer, - .make_new = framebuf_make_new, - .buffer = framebuf_get_buffer, - .locals_dict = (mp_obj_dict_t *)&framebuf_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_framebuf, + MP_QSTR_FrameBuffer, + MP_TYPE_FLAG_NONE, + framebuf_make_new, + buffer, framebuf_get_buffer, + locals_dict, (mp_obj_dict_t *)&framebuf_locals_dict + ); #endif // this factory function is provided for backwards compatibility with old FrameBuffer1 class diff --git a/extmod/modlwip.c b/extmod/modlwip.c index f9451a0ce4399..f9d5b76b2c8bb 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -177,12 +177,13 @@ STATIC const mp_rom_map_elem_t lwip_slip_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(lwip_slip_locals_dict, lwip_slip_locals_dict_table); -STATIC const mp_obj_type_t lwip_slip_type = { - { &mp_type_type }, - .name = MP_QSTR_slip, - .make_new = lwip_slip_make_new, - .locals_dict = (mp_obj_dict_t *)&lwip_slip_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + lwip_slip_type, + MP_QSTR_slip, + MP_TYPE_FLAG_NONE, + lwip_slip_make_new, + locals_dict, (mp_obj_dict_t *)&lwip_slip_locals_dict + ); #endif // MICROPY_PY_LWIP_SLIP @@ -1594,14 +1595,15 @@ STATIC const mp_stream_p_t lwip_socket_stream_p = { .ioctl = lwip_socket_ioctl, }; -STATIC const mp_obj_type_t lwip_socket_type = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .print = lwip_socket_print, - .make_new = lwip_socket_make_new, - .protocol = &lwip_socket_stream_p, - .locals_dict = (mp_obj_dict_t *)&lwip_socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + lwip_socket_type, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + lwip_socket_make_new, + print, lwip_socket_print, + protocol, &lwip_socket_stream_p, + locals_dict, (mp_obj_dict_t *)&lwip_socket_locals_dict + ); /******************************************************************************/ // Support functions for memory protection. lwIP has its own memory management diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index 6e3603fa18a47..500d13c5b8595 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -144,12 +144,13 @@ STATIC const mp_rom_map_elem_t task_queue_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(task_queue_locals_dict, task_queue_locals_dict_table); -STATIC const mp_obj_type_t task_queue_type = { - { &mp_type_type }, - .name = MP_QSTR_TaskQueue, - .make_new = task_queue_make_new, - .locals_dict = (mp_obj_dict_t *)&task_queue_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + task_queue_type, + MP_QSTR_TaskQueue, + MP_TYPE_FLAG_NONE, + task_queue_make_new, + locals_dict, (mp_obj_dict_t *)&task_queue_locals_dict + ); /******************************************************************************/ // Task class @@ -286,14 +287,15 @@ STATIC mp_obj_t task_iternext(mp_obj_t self_in) { return mp_const_none; } -STATIC const mp_obj_type_t task_type = { - { &mp_type_type }, - .name = MP_QSTR_Task, - .make_new = task_make_new, - .attr = task_attr, - .getiter = task_getiter, - .iternext = task_iternext, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + task_type, + MP_QSTR_Task, + MP_TYPE_FLAG_NONE, + task_make_new, + attr, task_attr, + getiter, task_getiter, + iternext, task_iternext + ); /******************************************************************************/ // C-level uasyncio module diff --git a/extmod/moducryptolib.c b/extmod/moducryptolib.c index dc0ecb9b20f1d..236b7edfd7aa4 100644 --- a/extmod/moducryptolib.c +++ b/extmod/moducryptolib.c @@ -348,12 +348,13 @@ STATIC const mp_rom_map_elem_t ucryptolib_aes_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(ucryptolib_aes_locals_dict, ucryptolib_aes_locals_dict_table); -STATIC const mp_obj_type_t ucryptolib_aes_type = { - { &mp_type_type }, - .name = MP_QSTR_aes, - .make_new = ucryptolib_aes_make_new, - .locals_dict = (void *)&ucryptolib_aes_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ucryptolib_aes_type, + MP_QSTR_aes, + MP_TYPE_FLAG_NONE, + ucryptolib_aes_make_new, + locals_dict, (void *)&ucryptolib_aes_locals_dict + ); STATIC const mp_rom_map_elem_t mp_module_ucryptolib_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ucryptolib) }, diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 25578dd6b0710..a9ad500c2d1a0 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -634,16 +634,17 @@ STATIC mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) { } MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at); -STATIC const mp_obj_type_t uctypes_struct_type = { - { &mp_type_type }, - .name = MP_QSTR_struct, - .print = uctypes_struct_print, - .make_new = uctypes_struct_make_new, - .attr = uctypes_struct_attr, - .subscr = uctypes_struct_subscr, - .unary_op = uctypes_struct_unary_op, - .buffer = uctypes_get_buffer, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + uctypes_struct_type, + MP_QSTR_struct, + MP_TYPE_FLAG_NONE, + uctypes_struct_make_new, + print, uctypes_struct_print, + attr, uctypes_struct_attr, + subscr, uctypes_struct_subscr, + unary_op, uctypes_struct_unary_op, + buffer, uctypes_get_buffer + ); STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uctypes) }, diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 7eae06b77d341..44625ed6da2ac 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -157,12 +157,13 @@ STATIC const mp_rom_map_elem_t uhashlib_sha256_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(uhashlib_sha256_locals_dict, uhashlib_sha256_locals_dict_table); -STATIC const mp_obj_type_t uhashlib_sha256_type = { - { &mp_type_type }, - .name = MP_QSTR_sha256, - .make_new = uhashlib_sha256_make_new, - .locals_dict = (void *)&uhashlib_sha256_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + uhashlib_sha256_type, + MP_QSTR_sha256, + MP_TYPE_FLAG_NONE, + uhashlib_sha256_make_new, + locals_dict, (void *)&uhashlib_sha256_locals_dict + ); #endif #if MICROPY_PY_UHASHLIB_SHA1 @@ -250,12 +251,13 @@ STATIC const mp_rom_map_elem_t uhashlib_sha1_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(uhashlib_sha1_locals_dict, uhashlib_sha1_locals_dict_table); -STATIC const mp_obj_type_t uhashlib_sha1_type = { - { &mp_type_type }, - .name = MP_QSTR_sha1, - .make_new = uhashlib_sha1_make_new, - .locals_dict = (void *)&uhashlib_sha1_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + uhashlib_sha1_type, + MP_QSTR_sha1, + MP_TYPE_FLAG_NONE, + uhashlib_sha1_make_new, + locals_dict, (void *)&uhashlib_sha1_locals_dict + ); #endif #if MICROPY_PY_UHASHLIB_MD5 @@ -343,12 +345,13 @@ STATIC const mp_rom_map_elem_t uhashlib_md5_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(uhashlib_md5_locals_dict, uhashlib_md5_locals_dict_table); -STATIC const mp_obj_type_t uhashlib_md5_type = { - { &mp_type_type }, - .name = MP_QSTR_md5, - .make_new = uhashlib_md5_make_new, - .locals_dict = (void *)&uhashlib_md5_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + uhashlib_md5_type, + MP_QSTR_md5, + MP_TYPE_FLAG_NONE, + uhashlib_md5_make_new, + locals_dict, (void *)&uhashlib_md5_locals_dict + ); #endif // MICROPY_PY_UHASHLIB_MD5 STATIC const mp_rom_map_elem_t mp_module_uhashlib_globals_table[] = { diff --git a/extmod/modure.c b/extmod/modure.c index a674d664999a8..a27c7ff9fdc57 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -179,12 +179,14 @@ STATIC const mp_rom_map_elem_t match_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(match_locals_dict, match_locals_dict_table); -STATIC const mp_obj_type_t match_type = { - { &mp_type_type }, - .name = MP_QSTR_match, - .print = match_print, - .locals_dict = (void *)&match_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + match_type, + MP_QSTR_match, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, match_print, + locals_dict, (void *)&match_locals_dict + ); #endif STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -411,12 +413,14 @@ STATIC const mp_rom_map_elem_t re_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(re_locals_dict, re_locals_dict_table); -STATIC const mp_obj_type_t re_type = { - { &mp_type_type }, - .name = MP_QSTR_ure, - .print = re_print, - .locals_dict = (void *)&re_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + re_type, + MP_QSTR_ure, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, re_print, + locals_dict, (void *)&re_locals_dict + ); #endif STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) { diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 70a1de2e40998..1a11ee0eab50e 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -336,13 +336,15 @@ STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); -STATIC const mp_obj_type_t mp_type_poll = { - { &mp_type_type }, - .name = MP_QSTR_poll, - .getiter = mp_identity_getiter, - .iternext = poll_iternext, - .locals_dict = (void *)&poll_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_poll, + MP_QSTR_poll, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, poll_iternext, + locals_dict, (void *)&poll_locals_dict + ); // poll() STATIC mp_obj_t select_poll(void) { diff --git a/extmod/modusocket.c b/extmod/modusocket.c index 4d72531160e6a..6008edb11c76f 100644 --- a/extmod/modusocket.c +++ b/extmod/modusocket.c @@ -528,14 +528,15 @@ STATIC const mp_stream_p_t socket_stream_p = { .is_text = false, }; -STATIC const mp_obj_type_t socket_type = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .print = socket_print, - .make_new = socket_make_new, - .protocol = &socket_stream_p, - .locals_dict = (mp_obj_dict_t *)&socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + socket_type, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + socket_make_new, + protocol, &socket_stream_p, + locals_dict, (mp_obj_dict_t *)&socket_locals_dict, + print, socket_print + ); /******************************************************************************/ // usocket module diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 9ba613a3e89e0..72eb0e214fe3f 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -314,16 +314,18 @@ STATIC const mp_stream_p_t ussl_socket_stream_p = { .ioctl = ussl_socket_ioctl, }; -STATIC const mp_obj_type_t ussl_socket_type = { - { &mp_type_type }, +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ussl_socket_type, + MP_QSTR_ussl, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module - .name = MP_QSTR_ussl, - .print = ussl_socket_print, - .getiter = NULL, - .iternext = NULL, - .protocol = &ussl_socket_stream_p, - .locals_dict = (void *)&ussl_socket_locals_dict, -}; + print, ussl_socket_print, + getiter, NULL, + iternext, NULL, + protocol, &ussl_socket_stream_p, + locals_dict, (void *)&ussl_socket_locals_dict + ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // TODO: Implement more args diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index b14ed9ad0e3d0..0fab915f3f06f 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -392,16 +392,18 @@ STATIC const mp_stream_p_t ussl_socket_stream_p = { .ioctl = socket_ioctl, }; -STATIC const mp_obj_type_t ussl_socket_type = { - { &mp_type_type }, +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ussl_socket_type, + MP_QSTR_ussl, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module - .name = MP_QSTR_ussl, - .print = socket_print, - .getiter = NULL, - .iternext = NULL, - .protocol = &ussl_socket_stream_p, - .locals_dict = (void *)&ussl_socket_locals_dict, -}; + print, socket_print, + getiter, NULL, + iternext, NULL, + protocol, &ussl_socket_stream_p, + locals_dict, (void *)&ussl_socket_locals_dict + ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // TODO: Implement more args diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index 1dde90bd1b026..bf4e031895fa6 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -209,13 +209,14 @@ STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(utimeq_locals_dict, utimeq_locals_dict_table); -STATIC const mp_obj_type_t utimeq_type = { - { &mp_type_type }, - .name = MP_QSTR_utimeq, - .make_new = utimeq_make_new, - .unary_op = utimeq_unary_op, - .locals_dict = (void *)&utimeq_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + utimeq_type, + MP_QSTR_utimeq, + MP_TYPE_FLAG_NONE, + utimeq_make_new, + unary_op, utimeq_unary_op, + locals_dict, (void *)&utimeq_locals_dict + ); STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utimeq) }, diff --git a/extmod/moduwebsocket.c b/extmod/moduwebsocket.c index a9ad0c22e3a51..2895978f3d5d2 100644 --- a/extmod/moduwebsocket.c +++ b/extmod/moduwebsocket.c @@ -290,13 +290,14 @@ STATIC const mp_stream_p_t websocket_stream_p = { .ioctl = websocket_ioctl, }; -STATIC const mp_obj_type_t websocket_type = { - { &mp_type_type }, - .name = MP_QSTR_websocket, - .make_new = websocket_make_new, - .protocol = &websocket_stream_p, - .locals_dict = (void *)&websocket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + websocket_type, + MP_QSTR_websocket, + MP_TYPE_FLAG_NONE, + websocket_make_new, + protocol, &websocket_stream_p, + locals_dict, (void *)&websocket_locals_dict + ); STATIC const mp_rom_map_elem_t uwebsocket_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uwebsocket) }, diff --git a/extmod/moduzlib.c b/extmod/moduzlib.c index 0161b9f49cdd8..93c939129eab8 100644 --- a/extmod/moduzlib.c +++ b/extmod/moduzlib.c @@ -140,13 +140,14 @@ STATIC const mp_stream_p_t decompio_stream_p = { }; #if !MICROPY_ENABLE_DYNRUNTIME -STATIC const mp_obj_type_t decompio_type = { - { &mp_type_type }, - .name = MP_QSTR_DecompIO, - .make_new = decompio_make_new, - .protocol = &decompio_stream_p, - .locals_dict = (void *)&decompio_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + decompio_type, + MP_QSTR_DecompIO, + MP_TYPE_FLAG_NONE, + decompio_make_new, + protocol, &decompio_stream_p, + locals_dict, (void *)&decompio_locals_dict + ); #endif STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) { diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index 1a2a718acfab5..cb893b38dcbe6 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -342,13 +342,14 @@ STATIC const mp_stream_p_t webrepl_stream_p = { .ioctl = webrepl_ioctl, }; -STATIC const mp_obj_type_t webrepl_type = { - { &mp_type_type }, - .name = MP_QSTR__webrepl, - .make_new = webrepl_make_new, - .protocol = &webrepl_stream_p, - .locals_dict = (mp_obj_dict_t *)&webrepl_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + webrepl_type, + MP_QSTR__webrepl, + MP_TYPE_FLAG_NONE, + webrepl_make_new, + protocol, &webrepl_stream_p, + locals_dict, (mp_obj_dict_t *)&webrepl_locals_dict + ); STATIC const mp_rom_map_elem_t webrepl_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__webrepl) }, diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index 9df799119fe61..fbd0a750b8e6c 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -496,12 +496,13 @@ STATIC const mp_rom_map_elem_t network_cyw43_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(network_cyw43_locals_dict, network_cyw43_locals_dict_table); -const mp_obj_type_t mp_network_cyw43_type = { - { &mp_type_type }, - .name = MP_QSTR_CYW43, - .print = network_cyw43_print, - .make_new = network_cyw43_make_new, - .locals_dict = (mp_obj_dict_t *)&network_cyw43_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_network_cyw43_type, + MP_QSTR_CYW43, + MP_TYPE_FLAG_NONE, + network_cyw43_make_new, + print, network_cyw43_print, + locals_dict, (mp_obj_dict_t *)&network_cyw43_locals_dict + ); #endif // MICROPY_PY_NETWORK_CYW43 diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 4014a71c1acfa..0906176d20b0b 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -774,13 +774,16 @@ static const mp_rom_map_elem_t nina_locals_dict_table[] = { static MP_DEFINE_CONST_DICT(nina_locals_dict, nina_locals_dict_table); +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mod_network_nic_type_nina_base, + MP_QSTR_nina, + MP_TYPE_FLAG_NONE, + network_ninaw10_make_new, + locals_dict, &nina_locals_dict + ); + const mod_network_nic_type_t mod_network_nic_type_nina = { - .base = { - { &mp_type_type }, - .name = MP_QSTR_nina, - .make_new = network_ninaw10_make_new, - .locals_dict = (mp_obj_t)&nina_locals_dict, - }, + .base = mod_network_nic_type_nina_base, .gethostbyname = network_ninaw10_gethostbyname, .socket = network_ninaw10_socket_socket, .close = network_ninaw10_socket_close, diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 95c852d7f7299..951a2966c93f9 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -1016,20 +1016,24 @@ STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table); #if WIZNET5K_WITH_LWIP_STACK -const mp_obj_type_t mod_network_nic_type_wiznet5k = { - { &mp_type_type }, - .name = MP_QSTR_WIZNET5K, - .make_new = wiznet5k_make_new, - .locals_dict = (mp_obj_dict_t *)&wiznet5k_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mod_network_nic_type_wiznet5k, + MP_QSTR_WIZNET5K, + MP_TYPE_FLAG_NONE, + wiznet5k_make_new, + locals_dict, &wiznet5k_locals_dict + ); #else // WIZNET5K_PROVIDED_STACK +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mod_network_nic_type_wiznet5k_base, + MP_QSTR_WIZNET5K, + MP_TYPE_FLAG_NONE, + wiznet5k_make_new, + locals_dict, &wiznet5k_locals_dict + ); + const mod_network_nic_type_t mod_network_nic_type_wiznet5k = { - .base = { - { &mp_type_type }, - .name = MP_QSTR_WIZNET5K, - .make_new = wiznet5k_make_new, - .locals_dict = (mp_obj_dict_t *)&wiznet5k_locals_dict, - }, + .base = mod_network_nic_type_wiznet5k_base, .gethostbyname = wiznet5k_gethostbyname, .socket = wiznet5k_socket_socket, .close = wiznet5k_socket_close, diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 8d2f13be476d8..4a2ef883c26c1 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -431,12 +431,13 @@ STATIC const mp_vfs_proto_t fat_vfs_proto = { .import_stat = fat_vfs_import_stat, }; -const mp_obj_type_t mp_fat_vfs_type = { - { &mp_type_type }, - .name = MP_QSTR_VfsFat, - .make_new = fat_vfs_make_new, - .protocol = &fat_vfs_proto, - .locals_dict = (mp_obj_dict_t *)&fat_vfs_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_fat_vfs_type, + MP_QSTR_VfsFat, + MP_TYPE_FLAG_NONE, + fat_vfs_make_new, + protocol, &fat_vfs_proto, + locals_dict, (mp_obj_dict_t *)&fat_vfs_locals_dict + ); #endif // MICROPY_VFS_FAT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 874f10c50045c..0d4af09b452cc 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -176,15 +176,17 @@ STATIC const mp_stream_p_t vfs_fat_fileio_stream_p = { .ioctl = file_obj_ioctl, }; -const mp_obj_type_t mp_type_vfs_fat_fileio = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .print = file_obj_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &vfs_fat_fileio_stream_p, - .locals_dict = (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_vfs_fat_fileio, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, file_obj_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &vfs_fat_fileio_stream_p, + locals_dict, (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict + ); STATIC const mp_stream_p_t vfs_fat_textio_stream_p = { .read = file_obj_read, @@ -193,15 +195,17 @@ STATIC const mp_stream_p_t vfs_fat_textio_stream_p = { .is_text = true, }; -const mp_obj_type_t mp_type_vfs_fat_textio = { - { &mp_type_type }, - .name = MP_QSTR_TextIOWrapper, - .print = file_obj_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &vfs_fat_textio_stream_p, - .locals_dict = (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_vfs_fat_textio, + MP_QSTR_TextIOWrapper, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, file_obj_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &vfs_fat_textio_stream_p, + locals_dict, (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict + ); // Factory function for I/O stream classes STATIC mp_obj_t fat_vfs_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) { diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index fbfeaa5ccf1a2..72c6696105d9e 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -502,14 +502,15 @@ STATIC const mp_vfs_proto_t MP_VFS_LFSx(proto) = { .import_stat = MP_VFS_LFSx(import_stat), }; -const mp_obj_type_t MP_TYPE_VFS_LFSx = { - { &mp_type_type }, +MP_DEFINE_CONST_OBJ_TYPE( + MP_TYPE_VFS_LFSx, #if LFS_BUILD_VERSION == 1 - .name = MP_QSTR_VfsLfs1, + MP_QSTR_VfsLfs1, #else - .name = MP_QSTR_VfsLfs2, + MP_QSTR_VfsLfs2, #endif - .make_new = MP_VFS_LFSx(make_new), - .protocol = &MP_VFS_LFSx(proto), - .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(locals_dict), -}; + MP_TYPE_FLAG_NONE, + MP_VFS_LFSx(make_new), + protocol, &MP_VFS_LFSx(proto), + locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(locals_dict) + ); diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index 124361feb99f2..ba90cc6084435 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -220,15 +220,17 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = { .ioctl = MP_VFS_LFSx(file_ioctl), }; -const mp_obj_type_t MP_TYPE_VFS_LFSx_(_fileio) = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .print = MP_VFS_LFSx(file_print), - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &MP_VFS_LFSx(fileio_stream_p), - .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict), -}; +MP_DEFINE_CONST_OBJ_TYPE( + MP_TYPE_VFS_LFSx_(_fileio), + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, MP_VFS_LFSx(file_print), + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &MP_VFS_LFSx(fileio_stream_p), + locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict) + ); STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = { .read = MP_VFS_LFSx(file_read), @@ -237,12 +239,14 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = { .is_text = true, }; -const mp_obj_type_t MP_TYPE_VFS_LFSx_(_textio) = { - { &mp_type_type }, - .name = MP_QSTR_TextIOWrapper, - .print = MP_VFS_LFSx(file_print), - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &MP_VFS_LFSx(textio_stream_p), - .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict), -}; +MP_DEFINE_CONST_OBJ_TYPE( + MP_TYPE_VFS_LFSx_(_textio), + MP_QSTR_TextIOWrapper, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, MP_VFS_LFSx(file_print), + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &MP_VFS_LFSx(textio_stream_p), + locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict) + ); diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index 36b211b84d6b7..79126c007068f 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -398,12 +398,13 @@ STATIC const mp_vfs_proto_t vfs_posix_proto = { .import_stat = mp_vfs_posix_import_stat, }; -const mp_obj_type_t mp_type_vfs_posix = { - { &mp_type_type }, - .name = MP_QSTR_VfsPosix, - .make_new = vfs_posix_make_new, - .protocol = &vfs_posix_proto, - .locals_dict = (mp_obj_dict_t *)&vfs_posix_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_vfs_posix, + MP_QSTR_VfsPosix, + MP_TYPE_FLAG_NONE, + vfs_posix_make_new, + protocol, &vfs_posix_proto, + locals_dict, (mp_obj_dict_t *)&vfs_posix_locals_dict + ); #endif // MICROPY_VFS_POSIX diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index c550842cd4e86..85aef1617b80d 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -249,15 +249,17 @@ STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = { .ioctl = vfs_posix_file_ioctl, }; -const mp_obj_type_t mp_type_vfs_posix_fileio = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .print = vfs_posix_file_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &vfs_posix_fileio_stream_p, - .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_vfs_posix_fileio, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, vfs_posix_file_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &vfs_posix_fileio_stream_p, + locals_dict, (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict + ); STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { .read = vfs_posix_file_read, @@ -266,15 +268,17 @@ STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { .is_text = true, }; -const mp_obj_type_t mp_type_vfs_posix_textio = { - { &mp_type_type }, - .name = MP_QSTR_TextIOWrapper, - .print = vfs_posix_file_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &vfs_posix_textio_stream_p, - .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_vfs_posix_textio, + MP_QSTR_TextIOWrapper, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, vfs_posix_file_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &vfs_posix_textio_stream_p, + locals_dict, (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict + ); const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_vfs_posix_textio}, STDIN_FILENO}; const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_vfs_posix_textio}, STDOUT_FILENO}; diff --git a/ports/cc3200/misc/mpirq.c b/ports/cc3200/misc/mpirq.c index eb93e5eef0cc3..e9cae92a327e6 100644 --- a/ports/cc3200/misc/mpirq.c +++ b/ports/cc3200/misc/mpirq.c @@ -190,11 +190,13 @@ STATIC const mp_rom_map_elem_t mp_irq_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(mp_irq_locals_dict, mp_irq_locals_dict_table); -const mp_obj_type_t mp_irq_type = { - { &mp_type_type }, - .name = MP_QSTR_irq, - .call = mp_irq_call, - .locals_dict = (mp_obj_t)&mp_irq_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_irq_type, + MP_QSTR_irq, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + call, mp_irq_call, + locals_dict, (mp_obj_t)&mp_irq_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_irq_obj_list); diff --git a/ports/cc3200/mods/modnetwork.c b/ports/cc3200/mods/modnetwork.c index d6ccc7c6a04d7..a74189300775b 100644 --- a/ports/cc3200/mods/modnetwork.c +++ b/ports/cc3200/mods/modnetwork.c @@ -171,10 +171,11 @@ STATIC const mp_rom_map_elem_t network_server_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(network_server_locals_dict, network_server_locals_dict_table); -STATIC const mp_obj_type_t network_server_type = { - { &mp_type_type }, - .name = MP_QSTR_Server, - .make_new = network_server_make_new, - .locals_dict = (mp_obj_t)&network_server_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + network_server_type, + MP_QSTR_Server, + MP_TYPE_FLAG_NONE, + network_server_make_new, + locals_dict, (mp_obj_t)&network_server_locals_dict + ); #endif diff --git a/ports/cc3200/mods/moduhashlib.c b/ports/cc3200/mods/moduhashlib.c index fc11569b7f988..5437cfb264e6e 100644 --- a/ports/cc3200/mods/moduhashlib.c +++ b/ports/cc3200/mods/moduhashlib.c @@ -177,19 +177,21 @@ STATIC MP_DEFINE_CONST_DICT(hash_locals_dict, hash_locals_dict_table); // .locals_dict = (mp_obj_t)&hash_locals_dict, //}; -STATIC const mp_obj_type_t sha1_type = { - { &mp_type_type }, - .name = MP_QSTR_sha1, - .make_new = hash_make_new, - .locals_dict = (mp_obj_t)&hash_locals_dict, -}; - -STATIC const mp_obj_type_t sha256_type = { - { &mp_type_type }, - .name = MP_QSTR_sha256, - .make_new = hash_make_new, - .locals_dict = (mp_obj_t)&hash_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + sha1_type, + MP_QSTR_sha1, + MP_TYPE_FLAG_NONE, + hash_make_new, + locals_dict, (mp_obj_t)&hash_locals_dict + ); + +STATIC MP_DEFINE_CONST_OBJ_TYPE( + sha256_type, + MP_QSTR_sha256, + MP_TYPE_FLAG_NONE, + hash_make_new, + locals_dict, (mp_obj_t)&hash_locals_dict + ); STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) }, diff --git a/ports/cc3200/mods/modusocket.c b/ports/cc3200/mods/modusocket.c index 23982eb96602c..11199de14628f 100644 --- a/ports/cc3200/mods/modusocket.c +++ b/ports/cc3200/mods/modusocket.c @@ -759,13 +759,14 @@ const mp_stream_p_t socket_stream_p = { .is_text = false, }; -STATIC const mp_obj_type_t socket_type = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .make_new = socket_make_new, - .protocol = &socket_stream_p, - .locals_dict = (mp_obj_t)&socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + socket_type, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + socket_make_new, + protocol, &socket_stream_p, + locals_dict, (mp_obj_t)&socket_locals_dict + ); /******************************************************************************/ // usocket module diff --git a/ports/cc3200/mods/modussl.c b/ports/cc3200/mods/modussl.c index cd2c62dcb82e3..d0909e7c2909d 100644 --- a/ports/cc3200/mods/modussl.c +++ b/ports/cc3200/mods/modussl.c @@ -60,14 +60,16 @@ STATIC const mp_obj_type_t ssl_socket_type; // ssl sockets inherit from normal socket, so we take its // locals and stream methods -STATIC const mp_obj_type_t ssl_socket_type = { - { &mp_type_type }, - .name = MP_QSTR_ussl, - .getiter = NULL, - .iternext = NULL, - .protocol = &socket_stream_p, - .locals_dict = (mp_obj_t)&socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ssl_socket_type, + MP_QSTR_ussl, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, NULL, + iternext, NULL, + protocol, &socket_stream_p, + locals_dict, (mp_obj_t)&socket_locals_dict + ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC const mp_arg_t allowed_args[] = { diff --git a/ports/cc3200/mods/modwlan.c b/ports/cc3200/mods/modwlan.c index 3b686932e81da..24f5d196d2a30 100644 --- a/ports/cc3200/mods/modwlan.c +++ b/ports/cc3200/mods/modwlan.c @@ -1285,13 +1285,16 @@ STATIC const mp_rom_map_elem_t wlan_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table); +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mod_network_nic_type_wlan_base, + MP_QSTR_WLAN, + MP_TYPE_FLAG_NONE, + wlan_make_new, + locals_dict, (mp_obj_t)&wlan_locals_dict + ); + const mod_network_nic_type_t mod_network_nic_type_wlan = { - .base = { - { &mp_type_type }, - .name = MP_QSTR_WLAN, - .make_new = wlan_make_new, - .locals_dict = (mp_obj_t)&wlan_locals_dict, - }, + .base = mod_network_nic_type_wlan_base, }; STATIC const mp_irq_methods_t wlan_irq_methods = { diff --git a/ports/cc3200/mods/pybadc.c b/ports/cc3200/mods/pybadc.c index fbdd393cd2ecc..a114eeda169a0 100644 --- a/ports/cc3200/mods/pybadc.c +++ b/ports/cc3200/mods/pybadc.c @@ -233,13 +233,14 @@ STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); -const mp_obj_type_t pyb_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = adc_print, - .make_new = adc_make_new, - .locals_dict = (mp_obj_t)&adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + adc_make_new, + print, adc_print, + locals_dict, (mp_obj_t)&adc_locals_dict + ); STATIC void adc_channel_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_adc_channel_obj_t *self = self_in; @@ -300,10 +301,12 @@ STATIC const mp_rom_map_elem_t adc_channel_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(adc_channel_locals_dict, adc_channel_locals_dict_table); -STATIC const mp_obj_type_t pyb_adc_channel_type = { - { &mp_type_type }, - .name = MP_QSTR_ADCChannel, - .print = adc_channel_print, - .call = adc_channel_call, - .locals_dict = (mp_obj_t)&adc_channel_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_adc_channel_type, + MP_QSTR_ADCChannel, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, adc_channel_print, + call, adc_channel_call, + locals_dict, (mp_obj_t)&adc_channel_locals_dict + ); diff --git a/ports/cc3200/mods/pybflash.c b/ports/cc3200/mods/pybflash.c index 9d534608becce..4cfafb7abf642 100644 --- a/ports/cc3200/mods/pybflash.c +++ b/ports/cc3200/mods/pybflash.c @@ -84,12 +84,13 @@ STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); -const mp_obj_type_t pyb_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .make_new = pyb_flash_make_new, - .locals_dict = (mp_obj_t)&pyb_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + pyb_flash_make_new, + locals_dict, (mp_obj_t)&pyb_flash_locals_dict + ); void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; diff --git a/ports/cc3200/mods/pybi2c.c b/ports/cc3200/mods/pybi2c.c index 40b3e2a207146..91a0b9b9f9a99 100644 --- a/ports/cc3200/mods/pybi2c.c +++ b/ports/cc3200/mods/pybi2c.c @@ -521,10 +521,11 @@ STATIC const mp_rom_map_elem_t pyb_i2c_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table); -const mp_obj_type_t pyb_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = pyb_i2c_print, - .make_new = pyb_i2c_make_new, - .locals_dict = (mp_obj_t)&pyb_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + pyb_i2c_make_new, + print, pyb_i2c_print, + locals_dict, (mp_obj_t)&pyb_i2c_locals_dict + ); diff --git a/ports/cc3200/mods/pybpin.c b/ports/cc3200/mods/pybpin.c index fd2f032e8d7ce..948cda70d4ab9 100644 --- a/ports/cc3200/mods/pybpin.c +++ b/ports/cc3200/mods/pybpin.c @@ -931,14 +931,15 @@ STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table); -const mp_obj_type_t pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = pin_print, - .make_new = pin_make_new, - .call = pin_call, - .locals_dict = (mp_obj_t)&pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + pin_make_new, + print, pin_print, + call, pin_call, + locals_dict, (mp_obj_t)&pin_locals_dict + ); STATIC const mp_irq_methods_t pin_irq_methods = { .init = pin_irq, @@ -952,10 +953,12 @@ STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_printf(print, "", self->name); } -const mp_obj_type_t pin_board_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_board, - .print = pin_named_pins_obj_print, - .locals_dict = (mp_obj_t)&pin_board_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_board_pins_obj_type, + MP_QSTR_board, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pin_named_pins_obj_print, + locals_dict, (mp_obj_t)&pin_board_pins_locals_dict + ); diff --git a/ports/cc3200/mods/pybrtc.c b/ports/cc3200/mods/pybrtc.c index 6c1918831c8c2..2761cb3c64f5d 100644 --- a/ports/cc3200/mods/pybrtc.c +++ b/ports/cc3200/mods/pybrtc.c @@ -469,12 +469,13 @@ STATIC const mp_rom_map_elem_t pyb_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); -const mp_obj_type_t pyb_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = pyb_rtc_make_new, - .locals_dict = (mp_obj_t)&pyb_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + pyb_rtc_make_new, + locals_dict, (mp_obj_t)&pyb_rtc_locals_dict + ); STATIC const mp_irq_methods_t pyb_rtc_irq_methods = { .init = pyb_rtc_irq, diff --git a/ports/cc3200/mods/pybsd.c b/ports/cc3200/mods/pybsd.c index 817127634d898..d8834e36f7447 100644 --- a/ports/cc3200/mods/pybsd.c +++ b/ports/cc3200/mods/pybsd.c @@ -212,9 +212,10 @@ STATIC const mp_rom_map_elem_t pyb_sd_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table); -const mp_obj_type_t pyb_sd_type = { - { &mp_type_type }, - .name = MP_QSTR_SD, - .make_new = pyb_sd_make_new, - .locals_dict = (mp_obj_t)&pyb_sd_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_sd_type, + MP_QSTR_SD, + MP_TYPE_FLAG_NONE, + pyb_sd_make_new, + locals_dict, (mp_obj_t)&pyb_sd_locals_dict + ); diff --git a/ports/cc3200/mods/pybsleep.c b/ports/cc3200/mods/pybsleep.c index 76fe0849a4bc2..423099991f8d1 100644 --- a/ports/cc3200/mods/pybsleep.c +++ b/ports/cc3200/mods/pybsleep.c @@ -125,10 +125,12 @@ STATIC pybsleep_data_t pybsleep_data = {NULL, NULL, NULL}; volatile arm_cm4_core_regs_t vault_arm_registers; STATIC pybsleep_reset_cause_t pybsleep_reset_cause = PYB_SLP_PWRON_RESET; STATIC pybsleep_wake_reason_t pybsleep_wake_reason = PYB_SLP_WAKED_PWRON; -STATIC const mp_obj_type_t pyb_sleep_type = { - { &mp_type_type }, - .name = MP_QSTR_sleep, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_sleep_type, + MP_QSTR_sleep, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW + ); /****************************************************************************** DECLARE PRIVATE FUNCTIONS diff --git a/ports/cc3200/mods/pybspi.c b/ports/cc3200/mods/pybspi.c index ed03b09060811..50d897633e557 100644 --- a/ports/cc3200/mods/pybspi.c +++ b/ports/cc3200/mods/pybspi.c @@ -377,10 +377,11 @@ STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table); -const mp_obj_type_t pyb_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = pyb_spi_print, - .make_new = pyb_spi_make_new, - .locals_dict = (mp_obj_t)&pyb_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + pyb_spi_make_new, + print, pyb_spi_print, + locals_dict, (mp_obj_t)&pyb_spi_locals_dict + ); diff --git a/ports/cc3200/mods/pybtimer.c b/ports/cc3200/mods/pybtimer.c index b2725ae166537..a8bc7821e0ce2 100644 --- a/ports/cc3200/mods/pybtimer.c +++ b/ports/cc3200/mods/pybtimer.c @@ -459,13 +459,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table); -const mp_obj_type_t pyb_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = pyb_timer_print, - .make_new = pyb_timer_make_new, - .locals_dict = (mp_obj_t)&pyb_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + pyb_timer_make_new, + print, pyb_timer_print, + locals_dict, (mp_obj_t)&pyb_timer_locals_dict + ); STATIC const mp_irq_methods_t pyb_timer_channel_irq_methods = { .init = pyb_timer_channel_irq, @@ -721,11 +722,13 @@ STATIC const mp_rom_map_elem_t pyb_timer_channel_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table); -STATIC const mp_obj_type_t pyb_timer_channel_type = { - { &mp_type_type }, - .name = MP_QSTR_TimerChannel, - .print = pyb_timer_channel_print, - .locals_dict = (mp_obj_t)&pyb_timer_channel_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_channel_type, + MP_QSTR_TimerChannel, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pyb_timer_channel_print, + locals_dict, (mp_obj_t)&pyb_timer_channel_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_list_t pyb_timer_channel_obj_list); diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index a81954abb8b57..059101f4f205a 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -685,15 +685,16 @@ STATIC const mp_irq_methods_t uart_irq_methods = { .flags = uart_irq_flags }; -const mp_obj_type_t pyb_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = pyb_uart_print, - .make_new = pyb_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + pyb_uart_make_new, + print, pyb_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_t)&pyb_uart_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _pyb_uart_obj_t *pyb_uart_objs[2]); diff --git a/ports/cc3200/mods/pybwdt.c b/ports/cc3200/mods/pybwdt.c index 8db096d6b5fcb..cde1abe59d896 100644 --- a/ports/cc3200/mods/pybwdt.c +++ b/ports/cc3200/mods/pybwdt.c @@ -150,10 +150,11 @@ STATIC const mp_rom_map_elem_t pybwdt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table); -const mp_obj_type_t pyb_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = pyb_wdt_make_new, - .locals_dict = (mp_obj_t)&pybwdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + pyb_wdt_make_new, + locals_dict, (mp_obj_t)&pybwdt_locals_dict + ); diff --git a/ports/esp32/esp32_nvs.c b/ports/esp32/esp32_nvs.c index 42d65be4a63b2..1f96ad129d11a 100644 --- a/ports/esp32/esp32_nvs.c +++ b/ports/esp32/esp32_nvs.c @@ -141,10 +141,11 @@ STATIC const mp_rom_map_elem_t esp32_nvs_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(esp32_nvs_locals_dict, esp32_nvs_locals_dict_table); -const mp_obj_type_t esp32_nvs_type = { - { &mp_type_type }, - .name = MP_QSTR_NVS, - .print = esp32_nvs_print, - .make_new = esp32_nvs_make_new, - .locals_dict = (mp_obj_dict_t *)&esp32_nvs_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp32_nvs_type, + MP_QSTR_NVS, + MP_TYPE_FLAG_NONE, + esp32_nvs_make_new, + print, esp32_nvs_print, + locals_dict, (mp_obj_dict_t *)&esp32_nvs_locals_dict + ); diff --git a/ports/esp32/esp32_partition.c b/ports/esp32/esp32_partition.c index 47e769486c9b4..2e42e7a81903e 100644 --- a/ports/esp32/esp32_partition.c +++ b/ports/esp32/esp32_partition.c @@ -284,10 +284,11 @@ STATIC const mp_rom_map_elem_t esp32_partition_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(esp32_partition_locals_dict, esp32_partition_locals_dict_table); -const mp_obj_type_t esp32_partition_type = { - { &mp_type_type }, - .name = MP_QSTR_Partition, - .print = esp32_partition_print, - .make_new = esp32_partition_make_new, - .locals_dict = (mp_obj_dict_t *)&esp32_partition_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp32_partition_type, + MP_QSTR_Partition, + MP_TYPE_FLAG_NONE, + esp32_partition_make_new, + print, esp32_partition_print, + locals_dict, (mp_obj_dict_t *)&esp32_partition_locals_dict + ); diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index ac897e33368a3..36f33df3f5d44 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -372,10 +372,11 @@ STATIC const mp_rom_map_elem_t esp32_rmt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(esp32_rmt_locals_dict, esp32_rmt_locals_dict_table); -const mp_obj_type_t esp32_rmt_type = { - { &mp_type_type }, - .name = MP_QSTR_RMT, - .print = esp32_rmt_print, - .make_new = esp32_rmt_make_new, - .locals_dict = (mp_obj_dict_t *)&esp32_rmt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp32_rmt_type, + MP_QSTR_RMT, + MP_TYPE_FLAG_NONE, + esp32_rmt_make_new, + print, esp32_rmt_print, + locals_dict, (mp_obj_dict_t *)&esp32_rmt_locals_dict + ); diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 8e4ce9c5a41b9..5eb0e5591e7e0 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -91,11 +91,12 @@ STATIC const mp_rom_map_elem_t esp32_ulp_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(esp32_ulp_locals_dict, esp32_ulp_locals_dict_table); -const mp_obj_type_t esp32_ulp_type = { - { &mp_type_type }, - .name = MP_QSTR_ULP, - .make_new = esp32_ulp_make_new, - .locals_dict = (mp_obj_t)&esp32_ulp_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp32_ulp_type, + MP_QSTR_ULP, + MP_TYPE_FLAG_NONE, + esp32_ulp_make_new, + locals_dict, (mp_obj_t)&esp32_ulp_locals_dict + ); #endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index cb45aab33910c..5cc2d80384915 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -256,10 +256,11 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = madc_print, - .make_new = madc_make_new, - .locals_dict = (mp_obj_t)&madc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + madc_make_new, + print, madc_print, + locals_dict, (mp_obj_t)&madc_locals_dict + ); diff --git a/ports/esp32/machine_adcblock.c b/ports/esp32/machine_adcblock.c index 06c215f8ae73d..770839e93e1b1 100644 --- a/ports/esp32/machine_adcblock.c +++ b/ports/esp32/machine_adcblock.c @@ -194,10 +194,11 @@ STATIC const mp_rom_map_elem_t madcblock_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(madcblock_locals_dict, madcblock_locals_dict_table); -const mp_obj_type_t machine_adcblock_type = { - { &mp_type_type }, - .name = MP_QSTR_ADCBlock, - .print = madcblock_print, - .make_new = madcblock_make_new, - .locals_dict = (mp_obj_t)&madcblock_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adcblock_type, + MP_QSTR_ADCBlock, + MP_TYPE_FLAG_NONE, + madcblock_make_new, + print, madcblock_print, + locals_dict, (mp_obj_dict_t *)&madcblock_locals_dict + ); diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 35826d4a99a8b..c9b9c14e01e77 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -104,12 +104,13 @@ STATIC const mp_rom_map_elem_t mdac_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(mdac_locals_dict, mdac_locals_dict_table); -const mp_obj_type_t machine_dac_type = { - { &mp_type_type }, - .name = MP_QSTR_DAC, - .print = mdac_print, - .make_new = mdac_make_new, - .locals_dict = (mp_obj_t)&mdac_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_dac_type, + MP_QSTR_DAC, + MP_TYPE_FLAG_NONE, + mdac_make_new, + print, mdac_print, + locals_dict, (mp_obj_t)&mdac_locals_dict + ); #endif // MICROPY_PY_MACHINE_DAC diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 3e720adb1a5e2..71583ef60e900 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -545,11 +545,12 @@ STATIC const mp_machine_spi_p_t machine_hw_spi_p = { .transfer = machine_hw_spi_transfer, }; -const mp_obj_type_t machine_hw_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_hw_spi_print, - .make_new = machine_hw_spi_make_new, - .protocol = &machine_hw_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hw_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_hw_spi_make_new, + print, machine_hw_spi_print, + protocol, &machine_hw_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index e25cad0b60a52..9e5be606dbf99 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -192,11 +192,12 @@ STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = { .transfer = machine_hw_i2c_transfer, }; -const mp_obj_type_t machine_hw_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_hw_i2c_print, - .make_new = machine_hw_i2c_make_new, - .protocol = &machine_hw_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hw_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_hw_i2c_make_new, + print, machine_hw_i2c_print, + protocol, &machine_hw_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index 933e68684b9e2..d30fb5d836e7b 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -829,16 +829,17 @@ STATIC const mp_stream_p_t i2s_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_i2s_type = { - { &mp_type_type }, - .name = MP_QSTR_I2S, - .print = machine_i2s_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &i2s_stream_p, - .make_new = machine_i2s_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_i2s_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2s_type, + MP_QSTR_I2S, + MP_TYPE_FLAG_NONE, + machine_i2s_make_new, + print, machine_i2s_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &i2s_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]); diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index b57562633878d..1a1a3a0f8d170 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -529,15 +529,16 @@ STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = machine_pin_print, - .make_new = mp_pin_make_new, - .call = machine_pin_call, - .protocol = &pin_pin_p, - .locals_dict = (mp_obj_t)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_print, + call, machine_pin_call, + protocol, &pin_pin_p, + locals_dict, (mp_obj_t)&machine_pin_locals_dict + ); /******************************************************************************/ // Pin IRQ object @@ -723,11 +724,13 @@ STATIC const mp_rom_map_elem_t machine_pin_irq_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_pin_irq_locals_dict, machine_pin_irq_locals_dict_table); -STATIC const mp_obj_type_t machine_pin_irq_type = { - { &mp_type_type }, - .name = MP_QSTR_IRQ, - .call = machine_pin_irq_call, - .locals_dict = (mp_obj_dict_t *)&machine_pin_irq_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_irq_type, + MP_QSTR_IRQ, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + call, machine_pin_irq_call, + locals_dict, (mp_obj_dict_t *)&machine_pin_irq_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_t machine_pin_irq_handler[40]); diff --git a/ports/esp32/machine_rtc.c b/ports/esp32/machine_rtc.c index 72d7b5c8280aa..6634bf5b03b9b 100644 --- a/ports/esp32/machine_rtc.c +++ b/ports/esp32/machine_rtc.c @@ -173,9 +173,10 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); -const mp_obj_type_t machine_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = machine_rtc_make_new, - .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + machine_rtc_make_new, + locals_dict, (mp_obj_t)&machine_rtc_locals_dict + ); diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 3f94356adbb4c..2d5663d476e8f 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -399,11 +399,12 @@ STATIC const mp_rom_map_elem_t machine_sdcard_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_sdcard_locals_dict, machine_sdcard_locals_dict_table); -const mp_obj_type_t machine_sdcard_type = { - { &mp_type_type }, - .name = MP_QSTR_SDCard, - .make_new = machine_sdcard_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_sdcard_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_sdcard_type, + MP_QSTR_SDCard, + MP_TYPE_FLAG_NONE, + machine_sdcard_make_new, + locals_dict, (mp_obj_dict_t *)&machine_sdcard_locals_dict + ); #endif // MICROPY_HW_ENABLE_SDCARD diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 66969b3a954f2..3b1295095593e 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -277,12 +277,13 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = machine_timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_t)&machine_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + print, machine_timer_print, + locals_dict, (mp_obj_t)&machine_timer_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *machine_timer_obj_head); diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index 168ac16d0eef6..c5e3483b74a08 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -134,11 +134,12 @@ STATIC const mp_rom_map_elem_t mtp_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table); -const mp_obj_type_t machine_touchpad_type = { - { &mp_type_type }, - .name = MP_QSTR_TouchPad, - .make_new = mtp_make_new, - .locals_dict = (mp_obj_t)&mtp_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_touchpad_type, + MP_QSTR_TouchPad, + MP_TYPE_FLAG_NONE, + mtp_make_new, + locals_dict, (mp_obj_t)&mtp_locals_dict + ); #endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 72445912897f5..9df16ae419837 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -530,13 +530,14 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_uart_print, - .make_new = machine_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_uart_make_new, + print, machine_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + ); diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index f0ec6928e05df..c2898c7fe1586 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -83,9 +83,10 @@ STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); -const mp_obj_type_t machine_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = machine_wdt_make_new, - .locals_dict = (mp_obj_t)&machine_wdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + machine_wdt_make_new, + locals_dict, (mp_obj_t)&machine_wdt_locals_dict + ); diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index a281558241dca..a2dcb3946ceaa 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -785,13 +785,14 @@ STATIC const mp_stream_p_t socket_stream_p = { .ioctl = socket_stream_ioctl }; -STATIC const mp_obj_type_t socket_type = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .make_new = socket_make_new, - .protocol = &socket_stream_p, - .locals_dict = (mp_obj_t)&socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + socket_type, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + socket_make_new, + protocol, &socket_stream_p, + locals_dict, (mp_obj_t)&socket_locals_dict + ); STATIC mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { // TODO support additional args beyond the first two diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index f302d70fec1d5..fc50e13c48edc 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -302,10 +302,12 @@ STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table); -const mp_obj_type_t lan_if_type = { - { &mp_type_type }, - .name = MP_QSTR_LAN, - .locals_dict = (mp_obj_dict_t *)&lan_if_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + lan_if_type, + MP_QSTR_LAN, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&lan_if_locals_dict + ); #endif diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index d74283c19cf63..d6368d9f2038b 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -278,8 +278,10 @@ STATIC const mp_rom_map_elem_t ppp_if_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(ppp_if_locals_dict, ppp_if_locals_dict_table); -const mp_obj_type_t ppp_if_type = { - { &mp_type_type }, - .name = MP_QSTR_PPP, - .locals_dict = (mp_obj_dict_t *)&ppp_if_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + ppp_if_type, + MP_QSTR_PPP, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&ppp_if_locals_dict + ); diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 4f74262afc381..6ca5f9a9bafea 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -47,9 +47,8 @@ #error WIFI_MODE_STA and WIFI_MODE_AP are supposed to be bitfields! #endif -STATIC const mp_obj_type_t wlan_if_type; -STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA}; -STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP}; +STATIC const wlan_if_obj_t wlan_sta_obj; +STATIC const wlan_if_obj_t wlan_ap_obj; // Set to "true" if esp_wifi_start() was called static bool wifi_started = false; @@ -616,10 +615,15 @@ STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table); -STATIC const mp_obj_type_t wlan_if_type = { - { &mp_type_type }, - .name = MP_QSTR_WLAN, - .locals_dict = (mp_obj_t)&wlan_if_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + wlan_if_type, + MP_QSTR_WLAN, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&wlan_if_locals_dict + ); + +STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA}; +STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP}; #endif // MICROPY_PY_NETWORK_WLAN diff --git a/ports/esp8266/machine_adc.c b/ports/esp8266/machine_adc.c index 471e14d8dfbed..bface7f7e155c 100644 --- a/ports/esp8266/machine_adc.c +++ b/ports/esp8266/machine_adc.c @@ -89,10 +89,11 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = machine_adc_print, - .make_new = machine_adc_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + machine_adc_make_new, + print, machine_adc_print, + locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + ); diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index 3ff76f5a02e04..c0d4a677e3ef5 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -175,13 +175,14 @@ STATIC const mp_machine_spi_p_t machine_hspi_p = { .transfer = machine_hspi_transfer, }; -const mp_obj_type_t machine_hspi_type = { - { &mp_type_type }, - .name = MP_QSTR_HSPI, - .print = machine_hspi_print, - .make_new = machine_hspi_make_new, - .protocol = &machine_hspi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hspi_type, + MP_QSTR_HSPI, + MP_TYPE_FLAG_NONE, + machine_hspi_make_new, + print, machine_hspi_print, + protocol, &machine_hspi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); #endif // MICROPY_PY_MACHINE_SPI diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c index 8171b8a8a3e35..8b759766cd6f7 100644 --- a/ports/esp8266/machine_pin.c +++ b/ports/esp8266/machine_pin.c @@ -450,15 +450,16 @@ STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, }; -const mp_obj_type_t pyb_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = pyb_pin_print, - .make_new = mp_pin_make_new, - .call = pyb_pin_call, - .protocol = &pin_pin_p, - .locals_dict = (mp_obj_dict_t *)&pyb_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, pyb_pin_print, + call, pyb_pin_call, + protocol, &pin_pin_p, + locals_dict, (mp_obj_dict_t *)&pyb_pin_locals_dict + ); /******************************************************************************/ // Pin IRQ object @@ -509,11 +510,13 @@ STATIC const mp_rom_map_elem_t pin_irq_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pin_irq_locals_dict, pin_irq_locals_dict_table); -STATIC const mp_obj_type_t pin_irq_type = { - { &mp_type_type }, - .name = MP_QSTR_IRQ, - .call = pin_irq_call, - .locals_dict = (mp_obj_dict_t *)&pin_irq_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pin_irq_type, + MP_QSTR_IRQ, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + call, pin_irq_call, + locals_dict, (mp_obj_dict_t *)&pin_irq_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_irq_handler[16]); diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index 38049ce724939..4235b325ef7a8 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -262,9 +262,10 @@ STATIC const mp_rom_map_elem_t pyb_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); -const mp_obj_type_t pyb_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = pyb_rtc_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + pyb_rtc_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_rtc_locals_dict + ); diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c index 2fe6516dc7ced..82f5189388caf 100644 --- a/ports/esp8266/machine_uart.c +++ b/ports/esp8266/machine_uart.c @@ -343,15 +343,16 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t pyb_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = pyb_uart_print, - .make_new = pyb_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&pyb_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + pyb_uart_make_new, + print, pyb_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&pyb_uart_locals_dict + ); MP_REGISTER_ROOT_POINTER(byte * uart0_rxbuf); diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index b06e752277288..d8c32ddd1a1f3 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -69,9 +69,10 @@ STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); -const mp_obj_type_t esp_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = machine_wdt_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_wdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + machine_wdt_make_new, + locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + ); diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index 1dd87b9af8f74..2bb2c7bd7695f 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -337,13 +337,14 @@ STATIC const mp_rom_map_elem_t esp_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(esp_timer_locals_dict, esp_timer_locals_dict_table); -const mp_obj_type_t esp_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = esp_timer_print, - .make_new = esp_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&esp_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + esp_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + esp_timer_make_new, + print, esp_timer_print, + locals_dict, (mp_obj_dict_t *)&esp_timer_locals_dict + ); // this bit is unused in the Xtensa PS register #define ETS_LOOP_ITER_BIT (12) diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index 5240d3b320649..f78bf5da52eb1 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -510,11 +510,13 @@ STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table); -const mp_obj_type_t wlan_if_type = { - { &mp_type_type }, - .name = MP_QSTR_WLAN, - .locals_dict = (mp_obj_dict_t *)&wlan_if_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + wlan_if_type, + MP_QSTR_WLAN, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&wlan_if_locals_dict + ); STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { diff --git a/ports/mimxrt/machine_adc.c b/ports/mimxrt/machine_adc.c index aa4cc831e5613..cbac6b5734d21 100644 --- a/ports/mimxrt/machine_adc.c +++ b/ports/mimxrt/machine_adc.c @@ -117,13 +117,14 @@ STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - {&mp_type_type}, - .name = MP_QSTR_ADC, - .print = adc_obj_print, - .make_new = adc_obj_make_new, - .locals_dict = (mp_obj_dict_t *)&adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + adc_obj_make_new, + print, adc_obj_print, + locals_dict, (mp_obj_dict_t *)&adc_locals_dict + ); void machine_adc_init(void) { for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) { diff --git a/ports/mimxrt/machine_i2c.c b/ports/mimxrt/machine_i2c.c index c10fcaddbe38c..b8b6b7bc63340 100644 --- a/ports/mimxrt/machine_i2c.c +++ b/ports/mimxrt/machine_i2c.c @@ -197,11 +197,12 @@ STATIC const mp_machine_i2c_p_t machine_i2c_p = { .transfer_single = machine_i2c_transfer_single, }; -const mp_obj_type_t machine_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_i2c_print, - .make_new = machine_i2c_make_new, - .protocol = &machine_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_i2c_make_new, + print, machine_i2c_print, + protocol, &machine_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); diff --git a/ports/mimxrt/machine_i2s.c b/ports/mimxrt/machine_i2s.c index a5446c7ac3efa..68bf3a820f59f 100644 --- a/ports/mimxrt/machine_i2s.c +++ b/ports/mimxrt/machine_i2s.c @@ -1213,16 +1213,17 @@ STATIC const mp_stream_p_t i2s_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_i2s_type = { - { &mp_type_type }, - .name = MP_QSTR_I2S, - .print = machine_i2s_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &i2s_stream_p, - .make_new = machine_i2s_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_i2s_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2s_type, + MP_QSTR_I2S, + MP_TYPE_FLAG_NONE, + machine_i2s_make_new, + print, machine_i2s_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &i2s_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_I2S_NUM]); diff --git a/ports/mimxrt/machine_led.c b/ports/mimxrt/machine_led.c index 4082eb34bbbfb..d766c8f3237a2 100644 --- a/ports/mimxrt/machine_led.c +++ b/ports/mimxrt/machine_led.c @@ -80,12 +80,13 @@ STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); -const mp_obj_type_t machine_led_type = { - {&mp_type_type}, - .name = MP_QSTR_LED, - .print = led_obj_print, - .make_new = led_obj_make_new, - .locals_dict = (mp_obj_dict_t *)&led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + led_obj_make_new, + print, led_obj_print, + locals_dict, (mp_obj_dict_t *)&led_locals_dict + ); #endif diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c index 64066af0e16ce..7ec66d0eac95b 100644 --- a/ports/mimxrt/machine_pin.c +++ b/ports/mimxrt/machine_pin.c @@ -58,17 +58,21 @@ enum { }; // Pin mapping dictionaries -const mp_obj_type_t machine_pin_cpu_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_cpu, - .locals_dict = (mp_obj_t)&machine_pin_cpu_pins_locals_dict, -}; - -const mp_obj_type_t machine_pin_board_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_board, - .locals_dict = (mp_obj_t)&machine_pin_board_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_cpu_pins_obj_type, + MP_QSTR_cpu, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_t)&machine_pin_cpu_pins_locals_dict + ); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_board_pins_obj_type, + MP_QSTR_board, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_t)&machine_pin_board_pins_locals_dict + ); STATIC const mp_irq_methods_t machine_pin_irq_methods; @@ -396,7 +400,6 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); - STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { (void)errcode; machine_pin_obj_t *self = self_in; @@ -417,24 +420,26 @@ STATIC const mp_pin_p_t machine_pin_obj_protocol = { .ioctl = machine_pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - {&mp_type_type}, - .name = MP_QSTR_Pin, - .print = machine_pin_obj_print, - .call = machine_pin_obj_call, - .make_new = mp_pin_make_new, - .protocol = &machine_pin_obj_protocol, - .locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_obj_print, + call, machine_pin_obj_call, + protocol, &machine_pin_obj_protocol, + locals_dict, (mp_obj_dict_t *)&machine_pin_locals_dict + ); // FIXME: Create actual pin_af type!!! -const mp_obj_type_t machine_pin_af_type = { - {&mp_type_type}, - .name = MP_QSTR_PinAF, - .print = machine_pin_obj_print, - .make_new = mp_pin_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_af_type, + MP_QSTR_PinAF, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_obj_print, + locals_dict, (mp_obj_dict_t *)&machine_pin_locals_dict + ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/ports/mimxrt/machine_rtc.c b/ports/mimxrt/machine_rtc.c index e0d33554b5594..5211027bdf807 100644 --- a/ports/mimxrt/machine_rtc.c +++ b/ports/mimxrt/machine_rtc.c @@ -166,9 +166,10 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); -const mp_obj_type_t machine_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = machine_rtc_make_new, - .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + machine_rtc_make_new, + locals_dict, (mp_obj_t)&machine_rtc_locals_dict + ); diff --git a/ports/mimxrt/machine_sdcard.c b/ports/mimxrt/machine_sdcard.c index 4a92aae00cefa..b7bdceef47edb 100644 --- a/ports/mimxrt/machine_sdcard.c +++ b/ports/mimxrt/machine_sdcard.c @@ -208,12 +208,13 @@ STATIC const mp_rom_map_elem_t sdcard_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(sdcard_locals_dict, sdcard_locals_dict_table); -const mp_obj_type_t machine_sdcard_type = { - { &mp_type_type }, - .name = MP_QSTR_SDCard, - .make_new = sdcard_obj_make_new, - .locals_dict = (mp_obj_dict_t *)&sdcard_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_sdcard_type, + MP_QSTR_SDCard, + MP_TYPE_FLAG_NONE, + sdcard_obj_make_new, + locals_dict, (mp_obj_dict_t *)&sdcard_locals_dict + ); void machine_sdcard_init0(void) { return; diff --git a/ports/mimxrt/machine_spi.c b/ports/mimxrt/machine_spi.c index 32bc77c34ddf6..ff3cf4fb2551d 100644 --- a/ports/mimxrt/machine_spi.c +++ b/ports/mimxrt/machine_spi.c @@ -251,11 +251,12 @@ STATIC const mp_machine_spi_p_t machine_spi_p = { .transfer = machine_spi_transfer, }; -const mp_obj_type_t machine_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_spi_print, - .make_new = machine_spi_make_new, - .protocol = &machine_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_spi_make_new, + print, machine_spi_print, + protocol, &machine_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); diff --git a/ports/mimxrt/machine_timer.c b/ports/mimxrt/machine_timer.c index 62a560ebbfc4c..9612388486e31 100644 --- a/ports/mimxrt/machine_timer.c +++ b/ports/mimxrt/machine_timer.c @@ -211,12 +211,13 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = machine_timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + print, machine_timer_print, + locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS]); diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 83382a6cf2bc1..4bb518eab25e4 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -469,13 +469,14 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_uart_print, - .make_new = machine_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_uart_make_new, + print, machine_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + ); diff --git a/ports/mimxrt/machine_wdt.c b/ports/mimxrt/machine_wdt.c index d09c464f69a3c..cde80f085b997 100644 --- a/ports/mimxrt/machine_wdt.c +++ b/ports/mimxrt/machine_wdt.c @@ -99,9 +99,10 @@ STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); -const mp_obj_type_t machine_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = machine_wdt_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_wdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + machine_wdt_make_new, + locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + ); diff --git a/ports/mimxrt/mimxrt_flash.c b/ports/mimxrt/mimxrt_flash.c index 27ab987ed17f2..1a7d6cca8da0e 100644 --- a/ports/mimxrt/mimxrt_flash.c +++ b/ports/mimxrt/mimxrt_flash.c @@ -215,9 +215,10 @@ STATIC const mp_rom_map_elem_t mimxrt_flash_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(mimxrt_flash_locals_dict, mimxrt_flash_locals_dict_table); -const mp_obj_type_t mimxrt_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .make_new = mimxrt_flash_make_new, - .locals_dict = (mp_obj_dict_t *)&mimxrt_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mimxrt_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + mimxrt_flash_make_new, + locals_dict, (mp_obj_dict_t *)&mimxrt_flash_locals_dict + ); diff --git a/ports/mimxrt/network_lan.c b/ports/mimxrt/network_lan.c index 5517b54bd02f5..08c3c9e729f50 100644 --- a/ports/mimxrt/network_lan.c +++ b/ports/mimxrt/network_lan.c @@ -220,12 +220,14 @@ STATIC const mp_rom_map_elem_t network_lan_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(network_lan_locals_dict, network_lan_locals_dict_table); -const mp_obj_type_t network_lan_type = { - { &mp_type_type }, - .name = MP_QSTR_LAN, - .print = network_lan_print, - .make_new = network_lan_make_new, - .locals_dict = (mp_obj_dict_t *)&network_lan_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + network_lan_type, + MP_QSTR_LAN, + MP_TYPE_FLAG_NONE, + network_lan_make_new, + print, network_lan_print, + locals_dict, (mp_obj_dict_t *)&network_lan_locals_dict + ); + #endif // defined(MICROPY_HW_ETH_MDC) diff --git a/ports/nrf/boards/microbit/modules/iters.c b/ports/nrf/boards/microbit/modules/iters.c index 66f9f6c7f3ea0..296fc1f51c525 100644 --- a/ports/nrf/boards/microbit/modules/iters.c +++ b/ports/nrf/boards/microbit/modules/iters.c @@ -43,12 +43,14 @@ static mp_obj_t microbit_repeat_iter_next(mp_obj_t iter_in) { return mp_obj_subscr(iter->iterable, MP_OBJ_NEW_SMALL_INT(iter->index), MP_OBJ_SENTINEL); } -const mp_obj_type_t microbit_repeat_iterator_type = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_repeat_iter_next, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_repeat_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, microbit_repeat_iter_next + ); mp_obj_t microbit_repeat_iterator(mp_obj_t iterable) { repeat_iterator_t *result = mp_obj_malloc(repeat_iterator_t, µbit_repeat_iterator_type); diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index 93ba9772fe98c..084cb09524da7 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -542,11 +542,13 @@ STATIC const mp_rom_map_elem_t microbit_display_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table); -const mp_obj_type_t microbit_display_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitDisplay, - .locals_dict = (mp_obj_dict_t*)µbit_display_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_display_type, + MP_QSTR_MicroBitDisplay, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t*)µbit_display_locals_dict + ); microbit_display_obj_t microbit_display_obj = { {µbit_display_type}, diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index eb44660b974ce..95b17bb6d3396 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -678,14 +678,15 @@ STATIC mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs } -const mp_obj_type_t microbit_image_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitImage, - .print = microbit_image_print, - .make_new = microbit_image_make_new, - .binary_op = image_binary_op, - .locals_dict = (mp_obj_dict_t*)µbit_image_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_image_type, + MP_QSTR_MicroBitImage, + MP_TYPE_FLAG_NONE, + microbit_image_make_new, + print, microbit_image_print, + binary_op, image_binary_op, + locals_dict, (mp_obj_dict_t*)µbit_image_locals_dict + ); typedef struct _scrolling_string_t { mp_obj_base_t base; @@ -820,18 +821,22 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { return iter->img; } -const mp_obj_type_t microbit_scrolling_string_type = { - { &mp_type_type }, - .name = MP_QSTR_ScrollingString, - .getiter = get_microbit_scrolling_string_iter, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_scrolling_string_type, + MP_QSTR_ScrollingString, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, get_microbit_scrolling_string_iter + ); -const mp_obj_type_t microbit_scrolling_string_iterator_type = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_scrolling_string_iter_next, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_scrolling_string_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, microbit_scrolling_string_iter_next + ); /** Facade types to present a string as a sequence of images. * These are necessary to avoid allocation during iteration, @@ -869,13 +874,15 @@ static mp_obj_t facade_unary_op(mp_unary_op_t op, mp_obj_t self_in) { static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf); -const mp_obj_type_t string_image_facade_type = { - { &mp_type_type }, - .name = MP_QSTR_Facade, - .unary_op = facade_unary_op, - .subscr = string_image_facade_subscr, - .getiter = microbit_facade_iterator, -}; +MP_DEFINE_CONST_OBJ_TYPE( + string_image_facade_type, + MP_QSTR_Facade, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + unary_op, facade_unary_op, + subscr, string_image_facade_subscr, + getiter, microbit_facade_iterator + ); typedef struct _facade_iterator_t { @@ -904,12 +911,14 @@ static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) { return iter->image; } -const mp_obj_type_t microbit_facade_iterator_type = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_facade_iter_next, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_facade_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, microbit_facade_iter_next + ); mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) { (void)iter_buf; diff --git a/ports/nrf/modules/board/led.c b/ports/nrf/modules/board/led.c index cbfc330d63309..5eef8f0464021 100644 --- a/ports/nrf/modules/board/led.c +++ b/ports/nrf/modules/board/led.c @@ -194,13 +194,14 @@ STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); -const mp_obj_type_t board_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = led_obj_print, - .make_new = led_obj_make_new, - .locals_dict = (mp_obj_dict_t*)&led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + board_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + led_obj_make_new, + print, led_obj_print, + locals_dict, (mp_obj_dict_t*)&led_locals_dict + ); #else // For boards with no LEDs, we leave an empty function here so that we don't diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index 54870d2c18ee8..5814dcaa392cf 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -294,12 +294,13 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .make_new = machine_adc_make_new, - .locals_dict = (mp_obj_dict_t*)&machine_adc_locals_dict, - .print = machine_adc_print, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + machine_adc_make_new, + locals_dict, (mp_obj_dict_t*)&machine_adc_locals_dict, + print, machine_adc_print + ); #endif // MICROPY_PY_MACHINE_ADC diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index aac9320873207..8468684428a7c 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -161,13 +161,14 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer_single = machine_hard_i2c_transfer_single, }; -const mp_obj_type_t machine_hard_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_hard_i2c_print, - .make_new = machine_hard_i2c_make_new, - .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t*)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_hard_i2c_make_new, + print, machine_hard_i2c_print, + protocol, &machine_hard_i2c_p, + locals_dict, (mp_obj_dict_t*)&mp_machine_i2c_locals_dict + ); #endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/nrf/modules/machine/pin.c b/ports/nrf/modules/machine/pin.c index 3a45c22d18ef2..835f6cf2bd32c 100644 --- a/ports/nrf/modules/machine/pin.c +++ b/ports/nrf/modules/machine/pin.c @@ -596,14 +596,15 @@ STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table); -const mp_obj_type_t pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = pin_print, - .make_new = pin_make_new, - .call = pin_call, - .locals_dict = (mp_obj_dict_t*)&pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + pin_make_new, + print, pin_print, + call, pin_call, + locals_dict, (mp_obj_dict_t*)&pin_locals_dict + ); /// \moduleref machine /// \class PinAF - Pin Alternate Functions @@ -671,12 +672,14 @@ STATIC const mp_rom_map_elem_t pin_af_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pin_af_locals_dict, pin_af_locals_dict_table); -const mp_obj_type_t pin_af_type = { - { &mp_type_type }, - .name = MP_QSTR_PinAF, - .print = pin_af_obj_print, - .locals_dict = (mp_obj_dict_t*)&pin_af_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_af_type, + MP_QSTR_PinAF, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pin_af_obj_print, + locals_dict, (mp_obj_dict_t*)&pin_af_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_mapper); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_map_dict); diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index a750285841e24..d0ac0e9450c3c 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -339,12 +339,13 @@ STATIC mp_obj_t machine_hard_pwm_freq(mp_obj_t self_in, mp_arg_val_t *args) { return mp_const_none; } -const mp_obj_type_t machine_hard_pwm_type = { - { &mp_type_type }, - .name = MP_QSTR_PWM, - .print = machine_pwm_print, - .make_new = machine_pwm_make_new, - .locals_dict = (mp_obj_dict_t*)&machine_pwm_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_pwm_type, + MP_QSTR_PWM, + MP_TYPE_FLAG_NONE, + machine_pwm_make_new, + print, machine_pwm_print, + locals_dict, (mp_obj_dict_t*)&machine_pwm_locals_dict + ); #endif // MICROPY_PY_MACHINE_HW_PWM diff --git a/ports/nrf/modules/machine/rtcounter.c b/ports/nrf/modules/machine/rtcounter.c index c9f907774e6a3..3c48c4bb1fb2e 100644 --- a/ports/nrf/modules/machine/rtcounter.c +++ b/ports/nrf/modules/machine/rtcounter.c @@ -262,12 +262,13 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); -const mp_obj_type_t machine_rtcounter_type = { - { &mp_type_type }, - .name = MP_QSTR_RTCounter, - .print = rtc_print, - .make_new = machine_rtc_make_new, - .locals_dict = (mp_obj_dict_t*)&machine_rtc_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtcounter_type, + MP_QSTR_RTCounter, + MP_TYPE_FLAG_NONE, + machine_rtc_make_new, + print, rtc_print, + locals_dict, (mp_obj_dict_t*)&machine_rtc_locals_dict + ); #endif // MICROPY_PY_MACHINE_RTCOUNTER diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c index 81bc151bebd63..22b0ff56e59ae 100644 --- a/ports/nrf/modules/machine/spi.c +++ b/ports/nrf/modules/machine/spi.c @@ -427,13 +427,14 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { .transfer = machine_hard_spi_transfer, }; -const mp_obj_type_t machine_hard_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_hard_spi_print, - .make_new = machine_spi_make_new, - .protocol = &machine_hard_spi_p, - .locals_dict = (mp_obj_dict_t*)&machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_spi_make_new, + print, machine_hard_spi_print, + protocol, &machine_hard_spi_p, + locals_dict, (mp_obj_dict_t*)&machine_spi_locals_dict + ); #endif // MICROPY_PY_MACHINE_HW_SPI diff --git a/ports/nrf/modules/machine/temp.c b/ports/nrf/modules/machine/temp.c index 1eb38c08d0bdc..1e21f11253fd2 100644 --- a/ports/nrf/modules/machine/temp.c +++ b/ports/nrf/modules/machine/temp.c @@ -112,12 +112,13 @@ STATIC const mp_rom_map_elem_t machine_temp_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_temp_locals_dict, machine_temp_locals_dict_table); -const mp_obj_type_t machine_temp_type = { - { &mp_type_type }, - .name = MP_QSTR_Temp, - .make_new = machine_temp_make_new, - .locals_dict = (mp_obj_dict_t*)&machine_temp_locals_dict, - .print = machine_temp_print, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_temp_type, + MP_QSTR_Temp, + MP_TYPE_FLAG_NONE, + machine_temp_make_new, + locals_dict, (mp_obj_dict_t*)&machine_temp_locals_dict, + print, machine_temp_print + ); #endif // MICROPY_PY_MACHINE_TEMP diff --git a/ports/nrf/modules/machine/timer.c b/ports/nrf/modules/machine/timer.c index c99713ef52c5d..3724881aa8131 100644 --- a/ports/nrf/modules/machine/timer.c +++ b/ports/nrf/modules/machine/timer.c @@ -234,12 +234,13 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_dict_t*)&machine_timer_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + print, timer_print, + locals_dict, (mp_obj_dict_t*)&machine_timer_locals_dict + ); #endif // MICROPY_PY_MACHINE_TIMER diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 1fd2ccc06cafd..5c9ba83ab6626 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -370,15 +370,16 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_hard_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_hard_uart_print, - .make_new = machine_hard_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t*)&machine_hard_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_hard_uart_make_new, + print, machine_hard_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t*)&machine_hard_uart_locals_dict + ); #endif // MICROPY_PY_MACHINE_UART diff --git a/ports/nrf/modules/nrf/flashbdev.c b/ports/nrf/modules/nrf/flashbdev.c index f63a9b46b29cf..b67e86d0d0dfd 100644 --- a/ports/nrf/modules/nrf/flashbdev.c +++ b/ports/nrf/modules/nrf/flashbdev.c @@ -183,13 +183,14 @@ STATIC mp_obj_t nrf_flashbdev_make_new(const mp_obj_type_t *type, size_t n_args, return MP_OBJ_FROM_PTR(self); } -const mp_obj_type_t nrf_flashbdev_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .print = nrf_flashbdev_print, - .make_new = nrf_flashbdev_make_new, - .locals_dict = (mp_obj_dict_t *)&nrf_flashbdev_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + nrf_flashbdev_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + nrf_flashbdev_make_new, + print, nrf_flashbdev_print, + locals_dict, (mp_obj_dict_t *)&nrf_flashbdev_locals_dict + ); void flashbdev_init(void) { // Set start to first aligned page from _fs_start. diff --git a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c index a66483f60dad7..5544ac6aefedc 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c +++ b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c @@ -209,12 +209,13 @@ STATIC const mp_rom_map_elem_t ubluepy_characteristic_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_characteristic_locals_dict, ubluepy_characteristic_locals_dict_table); -const mp_obj_type_t ubluepy_characteristic_type = { - { &mp_type_type }, - .name = MP_QSTR_Characteristic, - .print = ubluepy_characteristic_print, - .make_new = ubluepy_characteristic_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_characteristic_type, + MP_QSTR_Characteristic, + MP_TYPE_FLAG_NONE, + ubluepy_characteristic_make_new, + print, ubluepy_characteristic_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict + ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_constants.c b/ports/nrf/modules/ubluepy/ubluepy_constants.c index 14e433e6ebff3..e4637c8cbce2e 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_constants.c +++ b/ports/nrf/modules/ubluepy/ubluepy_constants.c @@ -69,11 +69,13 @@ STATIC const mp_rom_map_elem_t ubluepy_constants_ad_types_locals_dict_table[] = STATIC MP_DEFINE_CONST_DICT(ubluepy_constants_ad_types_locals_dict, ubluepy_constants_ad_types_locals_dict_table); -const mp_obj_type_t ubluepy_constants_ad_types_type = { - { &mp_type_type }, - .name = MP_QSTR_ad_types, - .locals_dict = (mp_obj_dict_t*)&ubluepy_constants_ad_types_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_constants_ad_types_type, + MP_QSTR_ad_types, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t*)&ubluepy_constants_ad_types_locals_dict + ); STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = { // GAP events @@ -90,10 +92,12 @@ STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_constants_locals_dict, ubluepy_constants_locals_dict_table); -const mp_obj_type_t ubluepy_constants_type = { - { &mp_type_type }, - .name = MP_QSTR_constants, - .locals_dict = (mp_obj_dict_t*)&ubluepy_constants_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_constants_type, + MP_QSTR_constants, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t*)&ubluepy_constants_locals_dict + ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_delegate.c b/ports/nrf/modules/ubluepy/ubluepy_delegate.c index 736bfbc9dd236..71648767e61d7 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_delegate.c +++ b/ports/nrf/modules/ubluepy/ubluepy_delegate.c @@ -77,12 +77,13 @@ STATIC const mp_rom_map_elem_t ubluepy_delegate_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_delegate_locals_dict, ubluepy_delegate_locals_dict_table); -const mp_obj_type_t ubluepy_delegate_type = { - { &mp_type_type }, - .name = MP_QSTR_DefaultDelegate, - .print = ubluepy_delegate_print, - .make_new = ubluepy_delegate_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_delegate_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_delegate_type, + MP_QSTR_DefaultDelegate, + MP_TYPE_FLAG_NONE, + ubluepy_delegate_make_new, + print, ubluepy_delegate_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_delegate_locals_dict + ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c index 370188f7fc7a8..07035460ae4d9 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c +++ b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c @@ -70,12 +70,13 @@ STATIC const mp_rom_map_elem_t ubluepy_descriptor_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_descriptor_locals_dict, ubluepy_descriptor_locals_dict_table); -const mp_obj_type_t ubluepy_descriptor_type = { - { &mp_type_type }, - .name = MP_QSTR_Descriptor, - .print = ubluepy_descriptor_print, - .make_new = ubluepy_descriptor_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_descriptor_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_descriptor_type, + MP_QSTR_Descriptor, + MP_TYPE_FLAG_NONE, + ubluepy_descriptor_make_new, + print, ubluepy_descriptor_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_descriptor_locals_dict + ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c index acfe316c0cf74..9c346a885b2cb 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c +++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c @@ -482,12 +482,13 @@ STATIC const mp_rom_map_elem_t ubluepy_peripheral_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_peripheral_locals_dict, ubluepy_peripheral_locals_dict_table); -const mp_obj_type_t ubluepy_peripheral_type = { - { &mp_type_type }, - .name = MP_QSTR_Peripheral, - .print = ubluepy_peripheral_print, - .make_new = ubluepy_peripheral_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_peripheral_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_peripheral_type, + MP_QSTR_Peripheral, + MP_TYPE_FLAG_NONE, + ubluepy_peripheral_make_new, + print, ubluepy_peripheral_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_peripheral_locals_dict + ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c index 773070b08907f..64a81d215d379 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c @@ -136,11 +136,13 @@ STATIC const mp_rom_map_elem_t ubluepy_scan_entry_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_scan_entry_locals_dict, ubluepy_scan_entry_locals_dict_table); -const mp_obj_type_t ubluepy_scan_entry_type = { - { &mp_type_type }, - .name = MP_QSTR_ScanEntry, - .print = ubluepy_scan_entry_print, - .locals_dict = (mp_obj_dict_t*)&ubluepy_scan_entry_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_scan_entry_type, + MP_QSTR_ScanEntry, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, ubluepy_scan_entry_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_scan_entry_locals_dict + ); #endif // MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_scanner.c b/ports/nrf/modules/ubluepy/ubluepy_scanner.c index 60895a3da93fe..c47044cf0c37a 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scanner.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scanner.c @@ -114,12 +114,13 @@ STATIC const mp_rom_map_elem_t ubluepy_scanner_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_scanner_locals_dict, ubluepy_scanner_locals_dict_table); -const mp_obj_type_t ubluepy_scanner_type = { - { &mp_type_type }, - .name = MP_QSTR_Scanner, - .print = ubluepy_scanner_print, - .make_new = ubluepy_scanner_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_scanner_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_scanner_type, + MP_QSTR_Scanner, + MP_TYPE_FLAG_NONE, + ubluepy_scanner_make_new, + print, ubluepy_scanner_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_scanner_locals_dict + ); #endif // MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_service.c b/ports/nrf/modules/ubluepy/ubluepy_service.c index 6dac231779258..9d0d6e5b95f8e 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_service.c +++ b/ports/nrf/modules/ubluepy/ubluepy_service.c @@ -171,12 +171,13 @@ STATIC const mp_rom_map_elem_t ubluepy_service_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_service_locals_dict, ubluepy_service_locals_dict_table); -const mp_obj_type_t ubluepy_service_type = { - { &mp_type_type }, - .name = MP_QSTR_Service, - .print = ubluepy_service_print, - .make_new = ubluepy_service_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_service_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_service_type, + MP_QSTR_Service, + MP_TYPE_FLAG_NONE, + ubluepy_service_make_new, + print, ubluepy_service_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_service_locals_dict + ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_uuid.c b/ports/nrf/modules/ubluepy/ubluepy_uuid.c index aee7b9a1a899c..0414a2a2867f5 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_uuid.c +++ b/ports/nrf/modules/ubluepy/ubluepy_uuid.c @@ -160,12 +160,13 @@ STATIC const mp_rom_map_elem_t ubluepy_uuid_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ubluepy_uuid_locals_dict, ubluepy_uuid_locals_dict_table); -const mp_obj_type_t ubluepy_uuid_type = { - { &mp_type_type }, - .name = MP_QSTR_UUID, - .print = ubluepy_uuid_print, - .make_new = ubluepy_uuid_make_new, - .locals_dict = (mp_obj_dict_t*)&ubluepy_uuid_locals_dict -}; +MP_DEFINE_CONST_OBJ_TYPE( + ubluepy_uuid_type, + MP_QSTR_UUID, + MP_TYPE_FLAG_NONE, + ubluepy_uuid_make_new, + print, ubluepy_uuid_print, + locals_dict, (mp_obj_dict_t*)&ubluepy_uuid_locals_dict + ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/uos/microbitfs.c b/ports/nrf/modules/uos/microbitfs.c index 6c8ffb0925d0e..63ac8c93213f6 100644 --- a/ports/nrf/modules/uos/microbitfs.c +++ b/ports/nrf/modules/uos/microbitfs.c @@ -626,12 +626,14 @@ STATIC const mp_stream_p_t textio_stream_p = { .is_text = true, }; -const mp_obj_type_t uos_mbfs_textio_type = { - { &mp_type_type }, - .name = MP_QSTR_TextIO, - .protocol = &textio_stream_p, - .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + uos_mbfs_textio_type, + MP_QSTR_TextIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + protocol, &textio_stream_p, + locals_dict, (mp_obj_dict_t*)&uos_mbfs_file_locals_dict + ); STATIC const mp_stream_p_t fileio_stream_p = { @@ -639,12 +641,14 @@ STATIC const mp_stream_p_t fileio_stream_p = { .write = microbit_file_write, }; -const mp_obj_type_t uos_mbfs_fileio_type = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .protocol = &fileio_stream_p, - .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + uos_mbfs_fileio_type, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + protocol, &fileio_stream_p, + locals_dict, (mp_obj_dict_t*)&uos_mbfs_file_locals_dict + ); // From micro:bit fileobj.c mp_obj_t uos_mbfs_open(size_t n_args, const mp_obj_t *args) { diff --git a/ports/nrf/pin_named_pins.c b/ports/nrf/pin_named_pins.c index e1d8736b9cf23..87fed746e64ee 100644 --- a/ports/nrf/pin_named_pins.c +++ b/ports/nrf/pin_named_pins.c @@ -36,19 +36,23 @@ STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_printf(print, "", self->name); } -const mp_obj_type_t pin_cpu_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_cpu, - .print = pin_named_pins_obj_print, - .locals_dict = (mp_obj_t)&pin_cpu_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_cpu_pins_obj_type, + MP_QSTR_cpu, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pin_named_pins_obj_print, + locals_dict, (mp_obj_t)&pin_cpu_pins_locals_dict + ); -const mp_obj_type_t pin_board_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_board, - .print = pin_named_pins_obj_print, - .locals_dict = (mp_obj_t)&pin_board_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_board_pins_obj_type, + MP_QSTR_board, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pin_named_pins_obj_print, + locals_dict, (mp_obj_t)&pin_board_pins_locals_dict + ); const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins); diff --git a/ports/pic16bit/modpybled.c b/ports/pic16bit/modpybled.c index 6adb2fda31265..fd4e8cce9c922 100644 --- a/ports/pic16bit/modpybled.c +++ b/ports/pic16bit/modpybled.c @@ -84,10 +84,11 @@ STATIC const mp_rom_map_elem_t pyb_led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_led_locals_dict, pyb_led_locals_dict_table); -const mp_obj_type_t pyb_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = pyb_led_print, - .make_new = pyb_led_make_new, - .locals_dict = (mp_obj_t)&pyb_led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + pyb_led_make_new, + print, pyb_led_print, + locals_dict, (mp_obj_t)&pyb_led_locals_dict + ); diff --git a/ports/pic16bit/modpybswitch.c b/ports/pic16bit/modpybswitch.c index 7b3d0f5f527bb..e1096b1daaadd 100644 --- a/ports/pic16bit/modpybswitch.c +++ b/ports/pic16bit/modpybswitch.c @@ -71,11 +71,12 @@ STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table); -const mp_obj_type_t pyb_switch_type = { - { &mp_type_type }, - .name = MP_QSTR_Switch, - .print = pyb_switch_print, - .make_new = pyb_switch_make_new, - .call = pyb_switch_call, - .locals_dict = (mp_obj_t)&pyb_switch_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_switch_type, + MP_QSTR_Switch, + MP_TYPE_FLAG_NONE, + pyb_switch_make_new, + print, pyb_switch_print, + call, pyb_switch_call, + locals_dict, (mp_obj_t)&pyb_switch_locals_dict + ); diff --git a/ports/renesas-ra/extint.c b/ports/renesas-ra/extint.c index 59f9ecfa37ea0..496a50c3e827a 100644 --- a/ports/renesas-ra/extint.c +++ b/ports/renesas-ra/extint.c @@ -374,13 +374,14 @@ STATIC const mp_rom_map_elem_t extint_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table); -const mp_obj_type_t extint_type = { - { &mp_type_type }, - .name = MP_QSTR_ExtInt, - .print = extint_obj_print, - .make_new = extint_make_new, - .locals_dict = (mp_obj_dict_t *)&extint_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + extint_type, + MP_QSTR_ExtInt, + MP_TYPE_FLAG_NONE, + extint_make_new, + locals_dict, &extint_locals_dict, + print, extint_obj_print + ); void extint_init0(void) { ra_icu_init(); diff --git a/ports/renesas-ra/led.c b/ports/renesas-ra/led.c index efc09d9de1b08..9a3f1d40e9651 100644 --- a/ports/renesas-ra/led.c +++ b/ports/renesas-ra/led.c @@ -170,13 +170,14 @@ STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); -const mp_obj_type_t ra_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = led_obj_print, - .make_new = led_obj_make_new, - .locals_dict = (mp_obj_dict_t *)&led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + ra_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + led_obj_make_new, + locals_dict, &led_locals_dict, + print, led_obj_print + ); #else // For boards with no LEDs, we leave an empty function here so that we don't diff --git a/ports/renesas-ra/machine_adc.c b/ports/renesas-ra/machine_adc.c index adeebeb4c4acd..99e35f48d997f 100644 --- a/ports/renesas-ra/machine_adc.c +++ b/ports/renesas-ra/machine_adc.c @@ -126,10 +126,11 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = machine_adc_print, - .make_new = machine_adc_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + machine_adc_make_new, + locals_dict, &machine_adc_locals_dict, + print, machine_adc_print + ); diff --git a/ports/renesas-ra/machine_i2c.c b/ports/renesas-ra/machine_i2c.c index bc1cea0cd93b8..eaca5ff4a85ea 100644 --- a/ports/renesas-ra/machine_i2c.c +++ b/ports/renesas-ra/machine_i2c.c @@ -156,13 +156,14 @@ STATIC const mp_machine_i2c_p_t machine_i2c_p = { .transfer_single = machine_i2c_transfer_single, }; -const mp_obj_type_t machine_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_i2c_print, - .make_new = machine_i2c_make_new, - .protocol = &machine_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_i2c_make_new, + locals_dict, (void *)&mp_machine_i2c_locals_dict, + print, machine_i2c_print, + protocol, &machine_i2c_p + ); #endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/renesas-ra/machine_pin.c b/ports/renesas-ra/machine_pin.c index f940b55547eba..17ef1e19a4137 100644 --- a/ports/renesas-ra/machine_pin.c +++ b/ports/renesas-ra/machine_pin.c @@ -349,15 +349,16 @@ STATIC const mp_pin_p_t machine_pin_pin_p = { .ioctl = machine_pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = machine_pin_print, - .make_new = mp_pin_make_new, - .call = machine_pin_call, - .protocol = &machine_pin_pin_p, - .locals_dict = (mp_obj_t)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + locals_dict, &machine_pin_locals_dict, + print, machine_pin_print, + call, machine_pin_call, + protocol, &machine_pin_pin_p + ); // Returns the pin mode. This value returned by this macro should be one of: // GPIO_MODE_INPUT, GPIO_MODE_OUTPUT_PP, GPIO_MODE_OUTPUT_OD, @@ -388,17 +389,21 @@ uint32_t pin_get_af(const machine_pin_obj_t *pin) { return (uint32_t)ra_gpio_get_af(pin->pin); } -const mp_obj_type_t pin_cpu_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_cpu, - .locals_dict = (mp_obj_dict_t *)&pin_cpu_pins_locals_dict, -}; - -const mp_obj_type_t pin_board_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_board, - .locals_dict = (mp_obj_dict_t *)&pin_board_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_cpu_pins_obj_type, + MP_QSTR_cpu, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, &pin_cpu_pins_locals_dict + ); + +MP_DEFINE_CONST_OBJ_TYPE( + pin_board_pins_obj_type, + MP_QSTR_board, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, &pin_board_pins_locals_dict + ); const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { const mp_map_t *named_map = &named_pins->map; diff --git a/ports/renesas-ra/machine_rtc.c b/ports/renesas-ra/machine_rtc.c index 9ddc90213774c..c3f64c6971088 100644 --- a/ports/renesas-ra/machine_rtc.c +++ b/ports/renesas-ra/machine_rtc.c @@ -341,9 +341,10 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); -const mp_obj_type_t machine_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = machine_rtc_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + machine_rtc_make_new, + locals_dict, &machine_rtc_locals_dict + ); diff --git a/ports/renesas-ra/machine_spi.c b/ports/renesas-ra/machine_spi.c index 01972ad457610..d0e8b03bd12ea 100644 --- a/ports/renesas-ra/machine_spi.c +++ b/ports/renesas-ra/machine_spi.c @@ -297,14 +297,15 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { .transfer = machine_hard_spi_transfer, }; -const mp_obj_type_t machine_hard_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_hard_spi_print, - .make_new = machine_hard_spi_make_new, - .protocol = &machine_hard_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_hard_spi_make_new, + locals_dict, &mp_machine_spi_locals_dict, + print, machine_hard_spi_print, + protocol, &machine_hard_spi_p + ); void spi_init0(void) { } diff --git a/ports/renesas-ra/machine_timer.c b/ports/renesas-ra/machine_timer.c index c387d35117d82..f3e5aafb7cab2 100644 --- a/ports/renesas-ra/machine_timer.c +++ b/ports/renesas-ra/machine_timer.c @@ -136,10 +136,11 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = machine_timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + locals_dict, &machine_timer_locals_dict, + print, machine_timer_print + ); diff --git a/ports/renesas-ra/machine_uart.c b/ports/renesas-ra/machine_uart.c index 4b4aa3396b622..11f5d6825eb2d 100644 --- a/ports/renesas-ra/machine_uart.c +++ b/ports/renesas-ra/machine_uart.c @@ -571,15 +571,16 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_uart_print, - .make_new = machine_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_uart_make_new, + locals_dict, &machine_uart_locals_dict, + print, machine_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p + ); MP_REGISTER_ROOT_POINTER(struct _machine_uart_obj_t *machine_uart_obj_all[MICROPY_HW_MAX_UART + MICROPY_HW_MAX_LPUART]); diff --git a/ports/renesas-ra/storage.c b/ports/renesas-ra/storage.c index 8d94f6fb9c3b6..f573894a50148 100644 --- a/ports/renesas-ra/storage.c +++ b/ports/renesas-ra/storage.c @@ -400,13 +400,14 @@ STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); -const mp_obj_type_t pyb_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .print = pyb_flash_print, - .make_new = pyb_flash_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + pyb_flash_make_new, + locals_dict, &pyb_flash_locals_dict, + print, pyb_flash_print + ); void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; diff --git a/ports/renesas-ra/timer.c b/ports/renesas-ra/timer.c index 1ae936525eaff..8f7acbcdaa476 100644 --- a/ports/renesas-ra/timer.c +++ b/ports/renesas-ra/timer.c @@ -410,13 +410,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table); -const mp_obj_type_t pyb_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = pyb_timer_print, - .make_new = pyb_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + pyb_timer_make_new, + locals_dict, &pyb_timer_locals_dict, + print, pyb_timer_print + ); #if defined(TIMER_CHANNEL) /* @@ -502,12 +503,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_channel_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table); -STATIC const mp_obj_type_t pyb_timer_channel_type = { - { &mp_type_type }, - .name = MP_QSTR_TimerChannel, - .print = pyb_timer_channel_print, - .locals_dict = (mp_obj_dict_t *)&pyb_timer_channel_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_channel_type, + MP_QSTR_TimerChannel, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, &pyb_timer_channel_locals_dict, + print, pyb_timer_channel_print + ); #endif STATIC void timer_handle_irq_channel(pyb_timer_obj_t *tim, uint8_t channel, mp_obj_t callback) { diff --git a/ports/renesas-ra/usrsw.c b/ports/renesas-ra/usrsw.c index 572510816b1f1..4107d7850d382 100644 --- a/ports/renesas-ra/usrsw.c +++ b/ports/renesas-ra/usrsw.c @@ -135,14 +135,15 @@ STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table); -const mp_obj_type_t pyb_switch_type = { - { &mp_type_type }, - .name = MP_QSTR_Switch, - .print = pyb_switch_print, - .make_new = pyb_switch_make_new, - .call = pyb_switch_call, - .locals_dict = (mp_obj_dict_t *)&pyb_switch_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_switch_type, + MP_QSTR_Switch, + MP_TYPE_FLAG_NONE, + pyb_switch_make_new, + locals_dict, &pyb_switch_locals_dict, + print, pyb_switch_print, + call, pyb_switch_call + ); MP_REGISTER_ROOT_POINTER(mp_obj_t pyb_switch_callback); diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index 5f91392e142df..549f8d5ecdc5a 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -113,10 +113,11 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = machine_adc_print, - .make_new = machine_adc_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + machine_adc_make_new, + print, machine_adc_print, + locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + ); diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index 3390cc42101d5..91d8bb59b76c1 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -176,11 +176,12 @@ STATIC const mp_machine_i2c_p_t machine_i2c_p = { .transfer_single = machine_i2c_transfer_single, }; -const mp_obj_type_t machine_hw_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_i2c_print, - .make_new = machine_i2c_make_new, - .protocol = &machine_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hw_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_i2c_make_new, + print, machine_i2c_print, + protocol, &machine_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); diff --git a/ports/rp2/machine_i2s.c b/ports/rp2/machine_i2s.c index 1015ba10179aa..8446a59781bb9 100644 --- a/ports/rp2/machine_i2s.c +++ b/ports/rp2/machine_i2s.c @@ -1137,15 +1137,16 @@ STATIC const mp_stream_p_t i2s_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_i2s_type = { - { &mp_type_type }, - .name = MP_QSTR_I2S, - .print = machine_i2s_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &i2s_stream_p, - .make_new = machine_i2s_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_i2s_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2s_type, + MP_QSTR_I2S, + MP_TYPE_FLAG_NONE, + machine_i2s_make_new, + print, machine_i2s_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &i2s_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + ); MP_REGISTER_ROOT_POINTER(void *machine_i2s_obj[2]); diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c index 705f61242c259..38670f09abead 100644 --- a/ports/rp2/machine_pin.c +++ b/ports/rp2/machine_pin.c @@ -641,15 +641,16 @@ STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = machine_pin_print, - .make_new = mp_pin_make_new, - .call = machine_pin_call, - .protocol = &pin_pin_p, - .locals_dict = (mp_obj_t)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_print, + call, machine_pin_call, + protocol, &pin_pin_p, + locals_dict, (mp_obj_t)&machine_pin_locals_dict + ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/ports/rp2/machine_rtc.c b/ports/rp2/machine_rtc.c index 9d59124a66e1f..73bdaee6c7606 100644 --- a/ports/rp2/machine_rtc.c +++ b/ports/rp2/machine_rtc.c @@ -115,9 +115,10 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); -const mp_obj_type_t machine_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = machine_rtc_make_new, - .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + machine_rtc_make_new, + locals_dict, (mp_obj_t)&machine_rtc_locals_dict + ); diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c index 104bd1fd5338b..f3ac8d7cf811f 100644 --- a/ports/rp2/machine_spi.c +++ b/ports/rp2/machine_spi.c @@ -290,14 +290,15 @@ STATIC const mp_machine_spi_p_t machine_spi_p = { .transfer = machine_spi_transfer, }; -const mp_obj_type_t machine_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_spi_print, - .make_new = machine_spi_make_new, - .protocol = &machine_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_spi_make_new, + print, machine_spi_print, + protocol, &machine_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) { if (mp_obj_is_type(o, &machine_spi_type)) { diff --git a/ports/rp2/machine_timer.c b/ports/rp2/machine_timer.c index e7e8f02d5517c..d3b60155294f4 100644 --- a/ports/rp2/machine_timer.c +++ b/ports/rp2/machine_timer.c @@ -156,10 +156,11 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = machine_timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + print, machine_timer_print, + locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + ); diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index f7e44b6b256c1..df6228058be7b 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -579,16 +579,17 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_uart_print, - .make_new = machine_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_uart_make_new, + print, machine_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + ); MP_REGISTER_ROOT_POINTER(void *rp2_uart_rx_buffer[2]); MP_REGISTER_ROOT_POINTER(void *rp2_uart_tx_buffer[2]); diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c index d6914a4f2615d..e8c433306922b 100644 --- a/ports/rp2/machine_wdt.c +++ b/ports/rp2/machine_wdt.c @@ -77,9 +77,10 @@ STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); -const mp_obj_type_t machine_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = machine_wdt_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_wdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + machine_wdt_make_new, + locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + ); diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index 47c95ea5c822c..37a3412db5cfe 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -185,9 +185,10 @@ STATIC const mp_rom_map_elem_t rp2_flash_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(rp2_flash_locals_dict, rp2_flash_locals_dict_table); -const mp_obj_type_t rp2_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .make_new = rp2_flash_make_new, - .locals_dict = (mp_obj_dict_t *)&rp2_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + rp2_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + rp2_flash_make_new, + locals_dict, (mp_obj_dict_t *)&rp2_flash_locals_dict + ); diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index eb85eb22480d8..9a195bdd8a64c 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -376,13 +376,14 @@ STATIC const mp_rom_map_elem_t rp2_pio_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(rp2_pio_locals_dict, rp2_pio_locals_dict_table); -const mp_obj_type_t rp2_pio_type = { - { &mp_type_type }, - .name = MP_QSTR_PIO, - .print = rp2_pio_print, - .make_new = rp2_pio_make_new, - .locals_dict = (mp_obj_dict_t *)&rp2_pio_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + rp2_pio_type, + MP_QSTR_PIO, + MP_TYPE_FLAG_NONE, + rp2_pio_make_new, + print, rp2_pio_print, + locals_dict, (mp_obj_dict_t *)&rp2_pio_locals_dict + ); STATIC mp_uint_t rp2_pio_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -806,13 +807,14 @@ STATIC const mp_rom_map_elem_t rp2_state_machine_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(rp2_state_machine_locals_dict, rp2_state_machine_locals_dict_table); -const mp_obj_type_t rp2_state_machine_type = { - { &mp_type_type }, - .name = MP_QSTR_StateMachine, - .print = rp2_state_machine_print, - .make_new = rp2_state_machine_make_new, - .locals_dict = (mp_obj_dict_t *)&rp2_state_machine_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + rp2_state_machine_type, + MP_QSTR_StateMachine, + MP_TYPE_FLAG_NONE, + rp2_state_machine_make_new, + print, rp2_state_machine_print, + locals_dict, (mp_obj_dict_t *)&rp2_state_machine_locals_dict + ); STATIC mp_uint_t rp2_state_machine_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index f4dd1aeb3c65a..76aae8ffc5767 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -162,11 +162,12 @@ STATIC const mp_rom_map_elem_t machine_led_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_led_locals_dict, machine_led_locals_dict_table); -const mp_obj_type_t machine_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = machine_led_print, - .make_new = mp_led_make_new, - .call = machine_led_call, - .locals_dict = (mp_obj_t)&machine_led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + mp_led_make_new, + print, machine_led_print, + call, machine_led_call, + locals_dict, (mp_obj_t)&machine_led_locals_dict + ); diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 161a3ccddd2a4..d47982e0e1c6b 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -287,15 +287,16 @@ STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = machine_pin_print, - .make_new = mp_pin_make_new, - .call = machine_pin_call, - .protocol = &pin_pin_p, - .locals_dict = (mp_obj_t)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_print, + call, machine_pin_call, + protocol, &pin_pin_p, + locals_dict, (mp_obj_t)&machine_pin_locals_dict + ); /* STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { diff --git a/ports/samd/samd_flash.c b/ports/samd/samd_flash.c index 530e3d9c8e5b8..6c74b59a263ad 100644 --- a/ports/samd/samd_flash.c +++ b/ports/samd/samd_flash.c @@ -181,9 +181,10 @@ STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(samd_flash_locals_dict, samd_flash_locals_dict_table); -const mp_obj_type_t samd_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .make_new = samd_flash_make_new, - .locals_dict = (mp_obj_dict_t *)&samd_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + samd_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + samd_flash_make_new, + locals_dict, (mp_obj_dict_t *)&samd_flash_locals_dict + ); diff --git a/ports/stm32/accel.c b/ports/stm32/accel.c index b07791a9a0b5a..276ce37d3ad49 100644 --- a/ports/stm32/accel.c +++ b/ports/stm32/accel.c @@ -281,11 +281,12 @@ STATIC const mp_rom_map_elem_t pyb_accel_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_accel_locals_dict, pyb_accel_locals_dict_table); -const mp_obj_type_t pyb_accel_type = { - { &mp_type_type }, - .name = MP_QSTR_Accel, - .make_new = pyb_accel_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_accel_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_accel_type, + MP_QSTR_Accel, + MP_TYPE_FLAG_NONE, + pyb_accel_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_accel_locals_dict + ); #endif // MICROPY_HW_HAS_MMA7660 || MICROPY_HW_HAS_KXTJ3 diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 9d58cf2e7cf02..7e627d088f744 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -703,13 +703,14 @@ STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); -const mp_obj_type_t pyb_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = adc_print, - .make_new = adc_make_new, - .locals_dict = (mp_obj_dict_t *)&adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + adc_make_new, + print, adc_print, + locals_dict, (mp_obj_dict_t *)&adc_locals_dict + ); /******************************************************************************/ /* adc all object */ @@ -911,11 +912,12 @@ STATIC const mp_rom_map_elem_t adc_all_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(adc_all_locals_dict, adc_all_locals_dict_table); -const mp_obj_type_t pyb_adc_all_type = { - { &mp_type_type }, - .name = MP_QSTR_ADCAll, - .make_new = adc_all_make_new, - .locals_dict = (mp_obj_dict_t *)&adc_all_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_adc_all_type, + MP_QSTR_ADCAll, + MP_TYPE_FLAG_NONE, + adc_all_make_new, + locals_dict, (mp_obj_dict_t *)&adc_all_locals_dict + ); #endif // MICROPY_HW_ENABLE_ADC diff --git a/ports/stm32/dac.c b/ports/stm32/dac.c index c41bf858a7ed4..da50b30fef135 100644 --- a/ports/stm32/dac.c +++ b/ports/stm32/dac.c @@ -502,12 +502,13 @@ STATIC const mp_rom_map_elem_t pyb_dac_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_dac_locals_dict, pyb_dac_locals_dict_table); -const mp_obj_type_t pyb_dac_type = { - { &mp_type_type }, - .name = MP_QSTR_DAC, - .print = pyb_dac_print, - .make_new = pyb_dac_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_dac_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_dac_type, + MP_QSTR_DAC, + MP_TYPE_FLAG_NONE, + pyb_dac_make_new, + print, pyb_dac_print, + locals_dict, (mp_obj_dict_t *)&pyb_dac_locals_dict + ); #endif // MICROPY_HW_ENABLE_DAC diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index d68275bf19c4e..4d2dc5d23be93 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -659,13 +659,14 @@ STATIC const mp_rom_map_elem_t extint_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table); -const mp_obj_type_t extint_type = { - { &mp_type_type }, - .name = MP_QSTR_ExtInt, - .print = extint_obj_print, - .make_new = extint_make_new, - .locals_dict = (mp_obj_dict_t *)&extint_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + extint_type, + MP_QSTR_ExtInt, + MP_TYPE_FLAG_NONE, + extint_make_new, + print, extint_obj_print, + locals_dict, (mp_obj_dict_t *)&extint_locals_dict + ); void extint_init0(void) { for (int i = 0; i < PYB_EXTI_NUM_VECTORS; i++) { diff --git a/ports/stm32/lcd.c b/ports/stm32/lcd.c index a0e001d1fdad2..a951ea7668447 100644 --- a/ports/stm32/lcd.c +++ b/ports/stm32/lcd.c @@ -525,11 +525,12 @@ STATIC const mp_rom_map_elem_t pyb_lcd_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_lcd_locals_dict, pyb_lcd_locals_dict_table); -const mp_obj_type_t pyb_lcd_type = { - { &mp_type_type }, - .name = MP_QSTR_LCD, - .make_new = pyb_lcd_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_lcd_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_lcd_type, + MP_QSTR_LCD, + MP_TYPE_FLAG_NONE, + pyb_lcd_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_lcd_locals_dict + ); #endif // MICROPY_HW_HAS_LCD diff --git a/ports/stm32/led.c b/ports/stm32/led.c index 078327462ae15..6745ef60b8d11 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -381,13 +381,14 @@ STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); -const mp_obj_type_t pyb_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = led_obj_print, - .make_new = led_obj_make_new, - .locals_dict = (mp_obj_dict_t *)&led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + led_obj_make_new, + print, led_obj_print, + locals_dict, (mp_obj_dict_t *)&led_locals_dict + ); #else // For boards with no LEDs, we leave an empty function here so that we don't diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index ad7c994ba111d..682bae3a6d13a 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -492,12 +492,13 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); -const mp_obj_type_t machine_adc_type = { - { &mp_type_type }, - .name = MP_QSTR_ADC, - .print = machine_adc_print, - .make_new = machine_adc_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + machine_adc_make_new, + print, machine_adc_print, + locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + ); #endif diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 262944585aaac..89970f234d190 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -236,13 +236,14 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer = machine_hard_i2c_transfer, }; -const mp_obj_type_t machine_hard_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_hard_i2c_print, - .make_new = machine_hard_i2c_make_new, - .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_hard_i2c_make_new, + print, machine_hard_i2c_print, + protocol, &machine_hard_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); #endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/machine_i2s.c b/ports/stm32/machine_i2s.c index 4f583a53e87bf..7dc6439f25c09 100644 --- a/ports/stm32/machine_i2s.c +++ b/ports/stm32/machine_i2s.c @@ -1114,16 +1114,17 @@ STATIC const mp_stream_p_t i2s_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_i2s_type = { - { &mp_type_type }, - .name = MP_QSTR_I2S, - .print = machine_i2s_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &i2s_stream_p, - .make_new = machine_i2s_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_i2s_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_i2s_type, + MP_QSTR_I2S, + MP_TYPE_FLAG_NONE, + machine_i2s_make_new, + print, machine_i2s_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &i2s_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_MAX_I2S]); diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index 37c026cefc33a..87561c2b7b1dd 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -135,11 +135,12 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { .transfer = machine_hard_spi_transfer, }; -const mp_obj_type_t machine_hard_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_hard_spi_print, - .make_new = machine_hard_spi_make_new, - .protocol = &machine_hard_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_hard_spi_make_new, + print, machine_hard_spi_print, + protocol, &machine_hard_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index c387d35117d82..bd9dbe609849b 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -136,10 +136,11 @@ STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); -const mp_obj_type_t machine_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = machine_timer_print, - .make_new = machine_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&machine_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + machine_timer_make_new, + print, machine_timer_print, + locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + ); diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 1bb1d2a1a7844..4d25a0274cdd3 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -660,13 +660,14 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t pyb_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = pyb_uart_print, - .make_new = pyb_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&pyb_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + pyb_uart_make_new, + print, pyb_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&pyb_uart_locals_dict + ); diff --git a/ports/stm32/network_lan.c b/ports/stm32/network_lan.c index f19916a1df091..f9c7d80b78e95 100644 --- a/ports/stm32/network_lan.c +++ b/ports/stm32/network_lan.c @@ -158,12 +158,13 @@ STATIC const mp_rom_map_elem_t network_lan_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(network_lan_locals_dict, network_lan_locals_dict_table); -const mp_obj_type_t network_lan_type = { - { &mp_type_type }, - .name = MP_QSTR_LAN, - .print = network_lan_print, - .make_new = network_lan_make_new, - .locals_dict = (mp_obj_dict_t *)&network_lan_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + network_lan_type, + MP_QSTR_LAN, + MP_TYPE_FLAG_NONE, + network_lan_make_new, + print, network_lan_print, + locals_dict, (mp_obj_dict_t *)&network_lan_locals_dict + ); #endif // defined(MICROPY_HW_ETH_MDC) diff --git a/ports/stm32/pin.c b/ports/stm32/pin.c index af6bafc43f219..992046cd17e5a 100644 --- a/ports/stm32/pin.c +++ b/ports/stm32/pin.c @@ -592,15 +592,16 @@ STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, }; -const mp_obj_type_t pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = pin_print, - .make_new = mp_pin_make_new, - .call = pin_call, - .protocol = &pin_pin_p, - .locals_dict = (mp_obj_dict_t *)&pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, pin_print, + call, pin_call, + protocol, &pin_pin_p, + locals_dict, (mp_obj_dict_t *)&pin_locals_dict + ); /// \moduleref pyb /// \class PinAF - Pin Alternate Functions @@ -669,12 +670,14 @@ STATIC const mp_rom_map_elem_t pin_af_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pin_af_locals_dict, pin_af_locals_dict_table); -const mp_obj_type_t pin_af_type = { - { &mp_type_type }, - .name = MP_QSTR_PinAF, - .print = pin_af_obj_print, - .locals_dict = (mp_obj_dict_t *)&pin_af_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_af_type, + MP_QSTR_PinAF, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pin_af_obj_print, + locals_dict, (mp_obj_dict_t *)&pin_af_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_mapper); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_map_dict); diff --git a/ports/stm32/pin_named_pins.c b/ports/stm32/pin_named_pins.c index 3a8e0f9fce9da..620888878c4a6 100644 --- a/ports/stm32/pin_named_pins.c +++ b/ports/stm32/pin_named_pins.c @@ -31,17 +31,21 @@ #include "py/mphal.h" #include "pin.h" -const mp_obj_type_t pin_cpu_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_cpu, - .locals_dict = (mp_obj_dict_t *)&pin_cpu_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_cpu_pins_obj_type, + MP_QSTR_cpu, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&pin_cpu_pins_locals_dict + ); -const mp_obj_type_t pin_board_pins_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_board, - .locals_dict = (mp_obj_dict_t *)&pin_board_pins_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pin_board_pins_obj_type, + MP_QSTR_board, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&pin_board_pins_locals_dict + ); const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { const mp_map_t *named_map = &named_pins->map; diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index ff41de31868da..6fdfd2c8549e1 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -1072,14 +1072,15 @@ STATIC const mp_stream_p_t can_stream_p = { .is_text = false, }; -const mp_obj_type_t pyb_can_type = { - { &mp_type_type }, - .name = MP_QSTR_CAN, - .print = pyb_can_print, - .make_new = pyb_can_make_new, - .protocol = &can_stream_p, - .locals_dict = (mp_obj_dict_t *)&pyb_can_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_can_type, + MP_QSTR_CAN, + MP_TYPE_FLAG_NONE, + pyb_can_make_new, + print, pyb_can_print, + protocol, &can_stream_p, + locals_dict, (mp_obj_dict_t *)&pyb_can_locals_dict + ); MP_REGISTER_ROOT_POINTER(struct _pyb_can_obj_t *pyb_can_obj_all[MICROPY_HW_MAX_CAN]); diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index e9877422ca0b4..ee6983a143fd3 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -1104,12 +1104,13 @@ STATIC const mp_rom_map_elem_t pyb_i2c_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table); -const mp_obj_type_t pyb_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = pyb_i2c_print, - .make_new = pyb_i2c_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + pyb_i2c_make_new, + print, pyb_i2c_print, + locals_dict, (mp_obj_dict_t *)&pyb_i2c_locals_dict + ); #endif // MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/pyb_spi.c b/ports/stm32/pyb_spi.c index f2cdcebf2a72f..b1425272feeef 100644 --- a/ports/stm32/pyb_spi.c +++ b/ports/stm32/pyb_spi.c @@ -350,11 +350,12 @@ STATIC const mp_machine_spi_p_t pyb_spi_p = { .transfer = spi_transfer_machine, }; -const mp_obj_type_t pyb_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = pyb_spi_print, - .make_new = pyb_spi_make_new, - .protocol = &pyb_spi_p, - .locals_dict = (mp_obj_dict_t *)&pyb_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + pyb_spi_make_new, + print, pyb_spi_print, + protocol, &pyb_spi_p, + locals_dict, (mp_obj_dict_t *)&pyb_spi_locals_dict + ); diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index 55977791b8c24..c7698db1430a3 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -837,9 +837,10 @@ STATIC const mp_rom_map_elem_t pyb_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); -const mp_obj_type_t pyb_rtc_type = { - { &mp_type_type }, - .name = MP_QSTR_RTC, - .make_new = pyb_rtc_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_rtc_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + pyb_rtc_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_rtc_locals_dict + ); diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 6f5892570baf2..b9cc051e7f13d 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -872,21 +872,23 @@ STATIC const mp_rom_map_elem_t pyb_sdcard_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_sdcard_locals_dict, pyb_sdcard_locals_dict_table); #if MICROPY_HW_ENABLE_SDCARD -const mp_obj_type_t pyb_sdcard_type = { - { &mp_type_type }, - .name = MP_QSTR_SDCard, - .make_new = pyb_sdcard_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_sdcard_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_sdcard_type, + MP_QSTR_SDCard, + MP_TYPE_FLAG_NONE, + pyb_sdcard_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_sdcard_locals_dict + ); #endif #if MICROPY_HW_ENABLE_MMCARD -const mp_obj_type_t pyb_mmcard_type = { - { &mp_type_type }, - .name = MP_QSTR_MMCard, - .make_new = pyb_mmcard_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_sdcard_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_mmcard_type, + MP_QSTR_MMCard, + MP_TYPE_FLAG_NONE, + pyb_mmcard_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_sdcard_locals_dict + ); #endif void sdcard_init_vfs(fs_user_mount_t *vfs, int part) { diff --git a/ports/stm32/servo.c b/ports/stm32/servo.c index 17084224637ec..d552f5e6b5d3a 100644 --- a/ports/stm32/servo.c +++ b/ports/stm32/servo.c @@ -336,12 +336,13 @@ STATIC const mp_rom_map_elem_t pyb_servo_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_servo_locals_dict, pyb_servo_locals_dict_table); -const mp_obj_type_t pyb_servo_type = { - { &mp_type_type }, - .name = MP_QSTR_Servo, - .print = pyb_servo_print, - .make_new = pyb_servo_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_servo_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_servo_type, + MP_QSTR_Servo, + MP_TYPE_FLAG_NONE, + pyb_servo_make_new, + print, pyb_servo_print, + locals_dict, (mp_obj_dict_t *)&pyb_servo_locals_dict + ); #endif // MICROPY_HW_ENABLE_SERVO diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index c33a75f67100c..92f7059493384 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -453,13 +453,14 @@ STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); -const mp_obj_type_t pyb_flash_type = { - { &mp_type_type }, - .name = MP_QSTR_Flash, - .print = pyb_flash_print, - .make_new = pyb_flash_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_flash_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_flash_type, + MP_QSTR_Flash, + MP_TYPE_FLAG_NONE, + pyb_flash_make_new, + print, pyb_flash_print, + locals_dict, (mp_obj_dict_t *)&pyb_flash_locals_dict + ); void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 518a2e23b4730..abf4c1f3fc4ca 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -1471,13 +1471,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table); -const mp_obj_type_t pyb_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = pyb_timer_print, - .make_new = pyb_timer_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + pyb_timer_make_new, + print, pyb_timer_print, + locals_dict, (mp_obj_dict_t *)&pyb_timer_locals_dict + ); /// \moduleref pyb /// \class TimerChannel - setup a channel for a timer. @@ -1610,12 +1611,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_channel_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table); -STATIC const mp_obj_type_t pyb_timer_channel_type = { - { &mp_type_type }, - .name = MP_QSTR_TimerChannel, - .print = pyb_timer_channel_print, - .locals_dict = (mp_obj_dict_t *)&pyb_timer_channel_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_channel_type, + MP_QSTR_TimerChannel, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pyb_timer_channel_print, + locals_dict, (mp_obj_dict_t *)&pyb_timer_channel_locals_dict + ); STATIC void timer_handle_irq_channel(pyb_timer_obj_t *tim, uint8_t channel, mp_obj_t callback) { uint32_t irq_mask = TIMER_IRQ_MASK(channel); diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index c5fdd88acfb8f..7c36765c28c1b 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -936,16 +936,17 @@ STATIC const mp_stream_p_t pyb_usb_vcp_stream_p = { .ioctl = pyb_usb_vcp_ioctl, }; -const mp_obj_type_t pyb_usb_vcp_type = { - { &mp_type_type }, - .name = MP_QSTR_USB_VCP, - .print = pyb_usb_vcp_print, - .make_new = pyb_usb_vcp_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &pyb_usb_vcp_stream_p, - .locals_dict = (mp_obj_dict_t *)&pyb_usb_vcp_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_usb_vcp_type, + MP_QSTR_USB_VCP, + MP_TYPE_FLAG_NONE, + pyb_usb_vcp_make_new, + print, pyb_usb_vcp_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &pyb_usb_vcp_stream_p, + locals_dict, (mp_obj_dict_t *)&pyb_usb_vcp_locals_dict + ); /******************************************************************************/ // MicroPython bindings for USB HID @@ -1077,13 +1078,14 @@ STATIC const mp_stream_p_t pyb_usb_hid_stream_p = { .ioctl = pyb_usb_hid_ioctl, }; -const mp_obj_type_t pyb_usb_hid_type = { - { &mp_type_type }, - .name = MP_QSTR_USB_HID, - .make_new = pyb_usb_hid_make_new, - .protocol = &pyb_usb_hid_stream_p, - .locals_dict = (mp_obj_dict_t *)&pyb_usb_hid_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_usb_hid_type, + MP_QSTR_USB_HID, + MP_TYPE_FLAG_NONE, + pyb_usb_hid_make_new, + protocol, &pyb_usb_hid_stream_p, + locals_dict, (mp_obj_dict_t *)&pyb_usb_hid_locals_dict + ); #endif // MICROPY_HW_USB_HID diff --git a/ports/stm32/usrsw.c b/ports/stm32/usrsw.c index 60aae1c8839da..137f4dabfa5c3 100644 --- a/ports/stm32/usrsw.c +++ b/ports/stm32/usrsw.c @@ -134,14 +134,15 @@ STATIC const mp_rom_map_elem_t pyb_switch_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table); -const mp_obj_type_t pyb_switch_type = { - { &mp_type_type }, - .name = MP_QSTR_Switch, - .print = pyb_switch_print, - .make_new = pyb_switch_make_new, - .call = pyb_switch_call, - .locals_dict = (mp_obj_dict_t *)&pyb_switch_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_switch_type, + MP_QSTR_Switch, + MP_TYPE_FLAG_NONE, + pyb_switch_make_new, + print, pyb_switch_print, + call, pyb_switch_call, + locals_dict, (mp_obj_dict_t *)&pyb_switch_locals_dict + ); MP_REGISTER_ROOT_POINTER(mp_obj_t pyb_switch_callback); diff --git a/ports/stm32/wdt.c b/ports/stm32/wdt.c index d794607bc0a71..e3b8e2e0ae22e 100644 --- a/ports/stm32/wdt.c +++ b/ports/stm32/wdt.c @@ -102,9 +102,10 @@ STATIC const mp_rom_map_elem_t pyb_wdt_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_wdt_locals_dict, pyb_wdt_locals_dict_table); -const mp_obj_type_t pyb_wdt_type = { - { &mp_type_type }, - .name = MP_QSTR_WDT, - .make_new = pyb_wdt_make_new, - .locals_dict = (mp_obj_dict_t *)&pyb_wdt_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + pyb_wdt_make_new, + locals_dict, (mp_obj_dict_t *)&pyb_wdt_locals_dict + ); diff --git a/ports/teensy/led.c b/ports/teensy/led.c index d79e63cf7102a..ca548431fecad 100644 --- a/ports/teensy/led.c +++ b/ports/teensy/led.c @@ -134,10 +134,11 @@ STATIC const mp_rom_map_elem_t led_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table); -const mp_obj_type_t pyb_led_type = { - { &mp_type_type }, - .name = MP_QSTR_LED, - .print = led_obj_print, - .make_new = led_obj_make_new, - .locals_dict = (mp_obj_t)&led_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_led_type, + MP_QSTR_LED, + MP_TYPE_FLAG_NONE, + led_obj_make_new, + print, led_obj_print, + locals_dict, (mp_obj_t)&led_locals_dict + ); diff --git a/ports/teensy/timer.c b/ports/teensy/timer.c index 68dc965eb8bc8..4df24743356cc 100644 --- a/ports/teensy/timer.c +++ b/ports/teensy/timer.c @@ -746,13 +746,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table); -const mp_obj_type_t pyb_timer_type = { - { &mp_type_type }, - .name = MP_QSTR_Timer, - .print = pyb_timer_print, - .make_new = pyb_timer_make_new, - .locals_dict = (mp_obj_t)&pyb_timer_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + pyb_timer_make_new, + print, pyb_timer_print, + locals_dict, (mp_obj_t)&pyb_timer_locals_dict + ); /// \moduleref pyb /// \class TimerChannel - setup a channel for a timer. @@ -889,12 +890,14 @@ STATIC const mp_rom_map_elem_t pyb_timer_channel_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table); -STATIC const mp_obj_type_t pyb_timer_channel_type = { - { &mp_type_type }, - .name = MP_QSTR_TimerChannel, - .print = pyb_timer_channel_print, - .locals_dict = (mp_obj_t)&pyb_timer_channel_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + pyb_timer_channel_type, + MP_QSTR_TimerChannel, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, pyb_timer_channel_print, + locals_dict, (mp_obj_t)&pyb_timer_channel_locals_dict + ); STATIC bool ftm_handle_irq_callback(pyb_timer_obj_t *self, mp_uint_t channel, mp_obj_t callback) { // execute callback if it's set diff --git a/ports/teensy/uart.c b/ports/teensy/uart.c index 5b63fcb3cdd54..8957d92709468 100644 --- a/ports/teensy/uart.c +++ b/ports/teensy/uart.c @@ -483,10 +483,11 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); -const mp_obj_type_t pyb_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = pyb_uart_print, - .make_new = pyb_uart_make_new, - .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + pyb_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + pyb_uart_make_new, + print, pyb_uart_print, + locals_dict, (mp_obj_t)&pyb_uart_locals_dict + ); diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index cf425ac4334f8..b4567417a22a8 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -106,11 +106,14 @@ STATIC const mp_stream_p_t fileio_stream_p = { .ioctl = stest_ioctl, }; -STATIC const mp_obj_type_t mp_type_stest_fileio = { - { &mp_type_type }, - .protocol = &fileio_stream_p, - .locals_dict = (mp_obj_dict_t *)&rawfile_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_stest_fileio, + MP_QSTR_stest_fileio, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + protocol, &fileio_stream_p, + locals_dict, (mp_obj_dict_t *)&rawfile_locals_dict + ); // stream read returns non-blocking error STATIC mp_uint_t stest_read2(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { @@ -133,11 +136,14 @@ STATIC const mp_stream_p_t textio_stream_p2 = { .is_text = true, }; -STATIC const mp_obj_type_t mp_type_stest_textio2 = { - { &mp_type_type }, - .protocol = &textio_stream_p2, - .locals_dict = (mp_obj_dict_t *)&rawfile_locals_dict2, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_stest_textio2, + MP_QSTR_stest_textio2, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + protocol, &textio_stream_p2, + locals_dict, (mp_obj_dict_t *)&rawfile_locals_dict2 + ); // str/bytes objects without a valid hash STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte *)"0123456789"}; diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 6417b5d3b395c..98f0a1aa0ff68 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -421,13 +421,14 @@ STATIC const mp_rom_map_elem_t ffimod_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ffimod_locals_dict, ffimod_locals_dict_table); -STATIC const mp_obj_type_t ffimod_type = { - { &mp_type_type }, - .name = MP_QSTR_ffimod, - .print = ffimod_print, - .make_new = ffimod_make_new, - .locals_dict = (mp_obj_dict_t *)&ffimod_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ffimod_type, + MP_QSTR_ffimod, + MP_TYPE_FLAG_NONE, + ffimod_make_new, + print, ffimod_print, + locals_dict, (mp_obj_dict_t *)&ffimod_locals_dict + ); // FFI function @@ -530,12 +531,14 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_raise_TypeError(MP_ERROR_TEXT("don't know how to pass object to native function")); } -STATIC const mp_obj_type_t ffifunc_type = { - { &mp_type_type }, - .name = MP_QSTR_ffifunc, - .print = ffifunc_print, - .call = ffifunc_call, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ffifunc_type, + MP_QSTR_ffifunc, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, ffifunc_print, + call, ffifunc_call + ); // FFI callback for Python function @@ -556,12 +559,14 @@ STATIC const mp_rom_map_elem_t fficallback_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(fficallback_locals_dict, fficallback_locals_dict_table); -STATIC const mp_obj_type_t fficallback_type = { - { &mp_type_type }, - .name = MP_QSTR_fficallback, - .print = fficallback_print, - .locals_dict = (mp_obj_dict_t *)&fficallback_locals_dict -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + fficallback_type, + MP_QSTR_fficallback, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, fficallback_print, + locals_dict, (mp_obj_dict_t *)&fficallback_locals_dict + ); // FFI variable @@ -592,21 +597,25 @@ STATIC const mp_rom_map_elem_t ffivar_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(ffivar_locals_dict, ffivar_locals_dict_table); -STATIC const mp_obj_type_t ffivar_type = { - { &mp_type_type }, - .name = MP_QSTR_ffivar, - .print = ffivar_print, - .locals_dict = (mp_obj_dict_t *)&ffivar_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + ffivar_type, + MP_QSTR_ffivar, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, ffivar_print, + locals_dict, (mp_obj_dict_t *)&ffivar_locals_dict + ); // Generic opaque storage object (unused) /* -STATIC const mp_obj_type_t opaque_type = { - { &mp_type_type }, - .name = MP_QSTR_opaqueval, -// .print = opaque_print, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + opaque_type, + MP_QSTR_opaqueval, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + // .print = opaque_print, + ); */ STATIC mp_obj_t mod_ffi_open(size_t n_args, const mp_obj_t *args) { diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index 6fa00731fb753..72f95b645b257 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -174,14 +174,16 @@ STATIC const mp_rom_map_elem_t jclass_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table); -STATIC const mp_obj_type_t jclass_type = { - { &mp_type_type }, - .name = MP_QSTR_jclass, - .print = jclass_print, - .attr = jclass_attr, - .call = jclass_call, - .locals_dict = (mp_obj_dict_t *)&jclass_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + jclass_type, + MP_QSTR_jclass, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, jclass_print, + attr, jclass_attr, + call, jclass_call, + locals_dict, (mp_obj_dict_t *)&jclass_locals_dict + ); STATIC mp_obj_t new_jclass(jclass jc) { mp_obj_jclass_t *o = mp_obj_malloc(mp_obj_jclass_t, &jclass_type); @@ -320,16 +322,18 @@ STATIC mp_obj_t subscr_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { return mp_obj_new_getitem_iter(dest, iter_buf); } -STATIC const mp_obj_type_t jobject_type = { - { &mp_type_type }, - .name = MP_QSTR_jobject, - .print = jobject_print, - .unary_op = jobject_unary_op, - .attr = jobject_attr, - .subscr = jobject_subscr, - .getiter = subscr_getiter, -// .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + jobject_type, + MP_QSTR_jobject, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, jobject_print, + unary_op, jobject_unary_op, + attr, jobject_attr, + subscr, jobject_subscr, + getiter, subscr_getiter, + // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, + ); STATIC mp_obj_t new_jobject(jobject jo) { if (jo == NULL) { @@ -567,14 +571,16 @@ STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const return call_method(self->obj, name, methods, false, n_args, args); } -STATIC const mp_obj_type_t jmethod_type = { - { &mp_type_type }, - .name = MP_QSTR_jmethod, - .print = jmethod_print, - .call = jmethod_call, -// .attr = jobject_attr, -// .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + jmethod_type, + MP_QSTR_jmethod, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, jmethod_print, + call, jmethod_call, + // .attr = jobject_attr, + // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, + ); #ifdef __ANDROID__ #define LIBJVM_SO "libdvm.so" diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index 2de4a316edb99..d8a8d1d8c38b7 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -311,13 +311,15 @@ STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); -STATIC const mp_obj_type_t mp_type_poll = { - { &mp_type_type }, - .name = MP_QSTR_poll, - .getiter = mp_identity_getiter, - .iternext = poll_iternext, - .locals_dict = (void *)&poll_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_poll, + MP_QSTR_poll, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, poll_iternext, + locals_dict, (void *)&poll_locals_dict + ); STATIC mp_obj_t select_poll(size_t n_args, const mp_obj_t *args) { int alloc = 4; diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 72c70d1750b53..7e4476cbd0add 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -516,16 +516,17 @@ STATIC const mp_stream_p_t usocket_stream_p = { .ioctl = socket_ioctl, }; -const mp_obj_type_t mp_type_socket = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .print = socket_print, - .make_new = socket_make_new, - .getiter = NULL, - .iternext = NULL, - .protocol = &usocket_stream_p, - .locals_dict = (mp_obj_dict_t *)&usocket_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_socket, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + socket_make_new, + print, socket_print, + getiter, NULL, + iternext, NULL, + protocol, &usocket_stream_p, + locals_dict, (mp_obj_dict_t *)&usocket_locals_dict + ); #define BINADDR_MAX_LEN sizeof(struct in6_addr) STATIC mp_obj_t mod_socket_inet_pton(mp_obj_t family_in, mp_obj_t addr_in) { diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 9bb63229ddb5d..60a1924d803d3 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -126,13 +126,14 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { .transfer_single = machine_hard_i2c_transfer_single, }; -const mp_obj_type_t machine_hard_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_I2C, - .print = machine_hard_i2c_print, - .make_new = machine_hard_i2c_make_new, - .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + machine_hard_i2c_make_new, + print, machine_hard_i2c_print, + protocol, &machine_hard_i2c_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + ); #endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index aa55c04104e46..48303edd8167e 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -285,15 +285,16 @@ STATIC const mp_pin_p_t machine_pin_pin_p = { .ioctl = machine_pin_ioctl, }; -const mp_obj_type_t machine_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_Pin, - .print = machine_pin_print, - .make_new = mp_pin_make_new, - .call = machine_pin_call, - .protocol = &machine_pin_pin_p, - .locals_dict = (mp_obj_t)&machine_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_pin_type, + MP_QSTR_Pin, + MP_TYPE_FLAG_NONE, + mp_pin_make_new, + print, machine_pin_print, + call, machine_pin_call, + protocol, &machine_pin_pin_p, + locals_dict, (mp_obj_t)&machine_pin_locals_dict + ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/ports/zephyr/machine_spi.c b/ports/zephyr/machine_spi.c index 2b0911c5917e5..6d9bf896b06d9 100644 --- a/ports/zephyr/machine_spi.c +++ b/ports/zephyr/machine_spi.c @@ -197,13 +197,14 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { .transfer = machine_hard_spi_transfer, }; -const mp_obj_type_t machine_hard_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_SPI, - .print = machine_hard_spi_print, - .make_new = machine_hard_spi_make_new, - .protocol = &machine_hard_spi_p, - .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_hard_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + machine_hard_spi_make_new, + print, machine_hard_spi_print, + protocol, &machine_hard_spi_p, + locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + ); #endif // MICROPY_PY_MACHINE_SPI diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 3520795c5aad4..3f5df7465791b 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -154,13 +154,14 @@ STATIC const mp_stream_p_t uart_stream_p = { .is_text = false, }; -const mp_obj_type_t machine_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_UART, - .print = machine_uart_print, - .make_new = machine_uart_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_NONE, + machine_uart_make_new, + print, machine_uart_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &uart_stream_p, + locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + ); diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c index 17cf9babd3333..a7bef74ca6deb 100644 --- a/ports/zephyr/modusocket.c +++ b/ports/zephyr/modusocket.c @@ -353,14 +353,15 @@ STATIC const mp_stream_p_t socket_stream_p = { .ioctl = sock_ioctl, }; -STATIC const mp_obj_type_t socket_type = { - { &mp_type_type }, - .name = MP_QSTR_socket, - .print = socket_print, - .make_new = socket_make_new, - .protocol = &socket_stream_p, - .locals_dict = (mp_obj_t)&socket_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + socket_type, + MP_QSTR_socket, + MP_TYPE_FLAG_NONE, + socket_make_new, + print, socket_print, + protocol, &socket_stream_p, + locals_dict, (mp_obj_t)&socket_locals_dict + ); // // getaddrinfo() implementation diff --git a/ports/zephyr/modzsensor.c b/ports/zephyr/modzsensor.c index ba6717046fa42..7c0b0193d3269 100644 --- a/ports/zephyr/modzsensor.c +++ b/ports/zephyr/modzsensor.c @@ -105,12 +105,13 @@ STATIC const mp_rom_map_elem_t sensor_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(sensor_locals_dict, sensor_locals_dict_table); -STATIC const mp_obj_type_t sensor_type = { - { &mp_type_type }, - .name = MP_QSTR_Sensor, - .make_new = sensor_make_new, - .locals_dict = (void *)&sensor_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + sensor_type, + MP_QSTR_Sensor, + MP_TYPE_FLAG_NONE, + sensor_make_new, + locals_dict, (void *)&sensor_locals_dict + ); STATIC const mp_rom_map_elem_t mp_module_zsensor_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zsensor) }, diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c index 1179c3fda809b..ded7caa657632 100644 --- a/ports/zephyr/zephyr_storage.c +++ b/ports/zephyr/zephyr_storage.c @@ -128,13 +128,14 @@ STATIC const mp_rom_map_elem_t zephyr_disk_access_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(zephyr_disk_access_locals_dict, zephyr_disk_access_locals_dict_table); -const mp_obj_type_t zephyr_disk_access_type = { - { &mp_type_type }, - .name = MP_QSTR_DiskAccess, - .print = zephyr_disk_access_print, - .make_new = zephyr_disk_access_make_new, - .locals_dict = (mp_obj_dict_t *)&zephyr_disk_access_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + zephyr_disk_access_type, + MP_QSTR_DiskAccess, + MP_TYPE_FLAG_NONE, + zephyr_disk_access_make_new, + print, zephyr_disk_access_print, + locals_dict, (mp_obj_dict_t *)&zephyr_disk_access_locals_dict + ); #endif // CONFIG_DISK_ACCESS #ifdef CONFIG_FLASH_MAP @@ -249,11 +250,12 @@ STATIC const mp_rom_map_elem_t zephyr_flash_area_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(zephyr_flash_area_locals_dict, zephyr_flash_area_locals_dict_table); -const mp_obj_type_t zephyr_flash_area_type = { - { &mp_type_type }, - .name = MP_QSTR_FlashArea, - .print = zephyr_flash_area_print, - .make_new = zephyr_flash_area_make_new, - .locals_dict = (mp_obj_dict_t *)&zephyr_flash_area_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + zephyr_flash_area_type, + MP_QSTR_FlashArea, + MP_TYPE_FLAG_NONE, + zephyr_flash_area_make_new, + print, zephyr_flash_area_print, + locals_dict, (mp_obj_dict_t *)&zephyr_flash_area_locals_dict + ); #endif // CONFIG_FLASH_MAP diff --git a/py/builtinevex.c b/py/builtinevex.c index 73b77b40b70fd..403cd95a9deef 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -38,10 +38,12 @@ typedef struct _mp_obj_code_t { mp_obj_t module_fun; } mp_obj_code_t; -STATIC const mp_obj_type_t mp_type_code = { - { &mp_type_type }, - .name = MP_QSTR_code, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_code, + MP_QSTR_code, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW + ); STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) { // save context and set new context diff --git a/py/modio.c b/py/modio.c index d44c1948ab27a..093cb1f7e78d2 100644 --- a/py/modio.c +++ b/py/modio.c @@ -97,12 +97,13 @@ STATIC const mp_stream_p_t iobase_p = { .ioctl = iobase_ioctl, }; -STATIC const mp_obj_type_t mp_type_iobase = { - { &mp_type_type }, - .name = MP_QSTR_IOBase, - .make_new = iobase_make_new, - .protocol = &iobase_p, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_iobase, + MP_QSTR_IOBase, + MP_TYPE_FLAG_NONE, + iobase_make_new, + protocol, &iobase_p + ); #endif // MICROPY_PY_IO_IOBASE @@ -191,13 +192,14 @@ STATIC const mp_stream_p_t bufwriter_stream_p = { .write = bufwriter_write, }; -STATIC const mp_obj_type_t mp_type_bufwriter = { - { &mp_type_type }, - .name = MP_QSTR_BufferedWriter, - .make_new = bufwriter_make_new, - .protocol = &bufwriter_stream_p, - .locals_dict = (mp_obj_dict_t *)&bufwriter_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bufwriter, + MP_QSTR_BufferedWriter, + MP_TYPE_FLAG_NONE, + bufwriter_make_new, + protocol, &bufwriter_stream_p, + locals_dict, (mp_obj_dict_t *)&bufwriter_locals_dict + ); #endif // MICROPY_PY_IO_BUFFEREDWRITER STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = { diff --git a/py/modthread.c b/py/modthread.c index bad94fbf2f088..0a154474252d1 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -116,11 +116,13 @@ STATIC const mp_rom_map_elem_t thread_lock_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(thread_lock_locals_dict, thread_lock_locals_dict_table); -STATIC const mp_obj_type_t mp_type_thread_lock = { - { &mp_type_type }, - .name = MP_QSTR_lock, - .locals_dict = (mp_obj_dict_t *)&thread_lock_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_thread_lock, + MP_QSTR_lock, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + locals_dict, (mp_obj_dict_t *)&thread_lock_locals_dict + ); /****************************************************************/ // _thread module diff --git a/py/objarray.c b/py/objarray.c index dca41c2931d66..d93cce29eef5a 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -571,54 +571,55 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui } #if MICROPY_PY_ARRAY -const mp_obj_type_t mp_type_array = { - { &mp_type_type }, - .name = MP_QSTR_array, - .print = array_print, - .make_new = array_make_new, - .getiter = array_iterator_new, - .unary_op = array_unary_op, - .binary_op = array_binary_op, - .subscr = array_subscr, - .buffer_p = array_get_buffer, - .locals_dict = (mp_obj_dict_t *)&mp_obj_array_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_array, + MP_QSTR_array, + MP_TYPE_FLAG_NONE, + array_make_new, + print, array_print, + getiter, array_iterator_new, + unary_op, array_unary_op, + binary_op, array_binary_op, + subscr, array_subscr, + buffer, array_get_buffer, + locals_dict, (mp_obj_dict_t *)&mp_obj_array_locals_dict + ); #endif #if MICROPY_PY_BUILTINS_BYTEARRAY -const mp_obj_type_t mp_type_bytearray = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - .name = MP_QSTR_bytearray, - .print = array_print, - .make_new = bytearray_make_new, - .getiter = array_iterator_new, - .unary_op = array_unary_op, - .binary_op = array_binary_op, - .subscr = array_subscr, - .buffer = array_get_buffer, - .locals_dict = (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bytearray, + MP_QSTR_bytearray, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + bytearray_make_new, + print, array_print, + getiter, array_iterator_new, + unary_op, array_unary_op, + binary_op, array_binary_op, + subscr, array_subscr, + buffer, array_get_buffer, + locals_dict, (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict + ); #endif #if MICROPY_PY_BUILTINS_MEMORYVIEW -const mp_obj_type_t mp_type_memoryview = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - .name = MP_QSTR_memoryview, - .make_new = memoryview_make_new, - .getiter = array_iterator_new, - .unary_op = array_unary_op, - .binary_op = array_binary_op, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_memoryview, + MP_QSTR_memoryview, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + memoryview_make_new, + getiter, array_iterator_new, + unary_op, array_unary_op, + binary_op, array_binary_op, #if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE - .attr = memoryview_attr, + attr, memoryview_attr, #endif #if MICROPY_PY_BUILTINS_BYTES_HEX - .locals_dict = (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, + locals_dict, (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, #endif .subscr = array_subscr, .buffer = array_get_buffer, -}; + ); #endif /* unused @@ -664,12 +665,14 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t mp_type_array_it = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = array_it_iternext, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_array_it, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, array_it_iternext + ); STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t)); diff --git a/py/objattrtuple.c b/py/objattrtuple.c index 13c281aa1c600..0d41ee52354a4 100644 --- a/py/objattrtuple.c +++ b/py/objattrtuple.c @@ -80,15 +80,18 @@ mp_obj_t mp_obj_new_attrtuple(const qstr *fields, size_t n, const mp_obj_t *item return MP_OBJ_FROM_PTR(o); } -const mp_obj_type_t mp_type_attrtuple = { - { &mp_type_type }, - .name = MP_QSTR_tuple, // reuse tuple to save on a qstr - .print = mp_obj_attrtuple_print, - .unary_op = mp_obj_tuple_unary_op, - .binary_op = mp_obj_tuple_binary_op, - .attr = mp_obj_attrtuple_attr, - .subscr = mp_obj_tuple_subscr, - .getiter = mp_obj_tuple_getiter, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_attrtuple, + MP_QSTR_tuple, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + // reuse tuple to save on a qstr + print, mp_obj_attrtuple_print, + unary_op, mp_obj_tuple_unary_op, + binary_op, mp_obj_tuple_binary_op, + attr, mp_obj_attrtuple_attr, + subscr, mp_obj_tuple_subscr, + getiter, mp_obj_tuple_getiter + ); #endif // MICROPY_PY_ATTRTUPLE diff --git a/py/objbool.c b/py/objbool.c index 23e023d8cbc15..5d014bbb8eaf8 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -84,15 +84,16 @@ STATIC mp_obj_t bool_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_ return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT(value), rhs_in); } -const mp_obj_type_t mp_type_bool = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, // can match all numeric types - .name = MP_QSTR_bool, - .print = bool_print, - .make_new = bool_make_new, - .unary_op = bool_unary_op, - .binary_op = bool_binary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + // can match all numeric types + mp_type_bool, + MP_QSTR_bool, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + bool_make_new, + print, bool_print, + unary_op, bool_unary_op, + binary_op, bool_binary_op + ); #if !MICROPY_OBJ_IMMEDIATE_OBJS const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false}; diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 9936c06e494b2..353364cdc70ab 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -95,17 +95,19 @@ STATIC void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #endif -STATIC const mp_obj_type_t mp_type_bound_meth = { - { &mp_type_type }, - .name = MP_QSTR_bound_method, +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bound_meth, + MP_QSTR_bound_method, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - .print = bound_meth_print, + print, bound_meth_print, #endif - .call = bound_meth_call, + call, bound_meth_call #if MICROPY_PY_FUNCTION_ATTRS - .attr = bound_meth_attr, + , attr, bound_meth_attr #endif -}; + ); mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self) { mp_obj_bound_meth_t *o = mp_obj_malloc(mp_obj_bound_meth_t, &mp_type_bound_meth); diff --git a/py/objcell.c b/py/objcell.c index cab0d0b030ee7..a17a94b9b78e9 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -40,13 +40,13 @@ STATIC void cell_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t k } #endif -STATIC const mp_obj_type_t mp_type_cell = { - { &mp_type_type }, - .name = MP_QSTR_, // cell representation is just value in < > +STATIC MP_DEFINE_CONST_OBJ_TYPE( + // cell representation is just value in < > + mp_type_cell, MP_QSTR_, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - .print = cell_print, + , print, cell_print #endif -}; + ); mp_obj_t mp_obj_new_cell(mp_obj_t obj) { mp_obj_cell_t *o = mp_obj_malloc(mp_obj_cell_t, &mp_type_cell); diff --git a/py/objclosure.c b/py/objclosure.c index 5b9923a44bc77..15ed994d37263 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -86,18 +86,19 @@ STATIC void mp_obj_closure_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #endif -const mp_obj_type_t mp_type_closure = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_closure, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_closure, + MP_QSTR_closure, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - .print = closure_print, + print, closure_print, #endif - .call = closure_call, #if MICROPY_PY_FUNCTION_ATTRS - .attr = mp_obj_closure_attr, + attr, mp_obj_closure_attr, #endif -}; + call, closure_call + ); mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed_over, const mp_obj_t *closed) { mp_obj_closure_t *o = mp_obj_malloc_var(mp_obj_closure_t, mp_obj_t, n_closed_over, &mp_type_closure); diff --git a/py/objcomplex.c b/py/objcomplex.c index 4aa598a0bcb7d..cf213d718a793 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -151,16 +151,13 @@ STATIC void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_complex = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - .name = MP_QSTR_complex, - .print = complex_print, - .make_new = complex_make_new, - .unary_op = complex_unary_op, - .binary_op = complex_binary_op, - .attr = complex_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_complex, MP_QSTR_complex, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, complex_make_new, + print, complex_print, + unary_op, complex_unary_op, + binary_op, complex_binary_op, + attr, complex_attr + ); mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { mp_obj_complex_t *o = mp_obj_malloc(mp_obj_complex_t, &mp_type_complex); diff --git a/py/objdeque.c b/py/objdeque.c index b1c59a81e9f3c..22770317ab31a 100644 --- a/py/objdeque.c +++ b/py/objdeque.c @@ -155,12 +155,13 @@ STATIC const mp_rom_map_elem_t deque_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(deque_locals_dict, deque_locals_dict_table); -const mp_obj_type_t mp_type_deque = { - { &mp_type_type }, - .name = MP_QSTR_deque, - .make_new = deque_make_new, - .unary_op = deque_unary_op, - .locals_dict = (mp_obj_dict_t *)&deque_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_deque, + MP_QSTR_deque, + MP_TYPE_FLAG_NONE, + deque_make_new, + unary_op, deque_unary_op, + locals_dict, (mp_obj_dict_t *)&deque_locals_dict + ); #endif // MICROPY_PY_COLLECTIONS_DEQUE diff --git a/py/objdict.c b/py/objdict.c index 1d8e9059a188d..6e217d5c9a716 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -461,12 +461,14 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t mp_type_dict_view_it = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = dict_view_it_iternext, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_dict_view_it, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, dict_view_it_iternext + ); STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t)); @@ -512,13 +514,15 @@ STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t return dict_binary_op(op, o->dict, rhs_in); } -STATIC const mp_obj_type_t mp_type_dict_view = { - { &mp_type_type }, - .name = MP_QSTR_dict_view, - .print = dict_view_print, - .binary_op = dict_view_binary_op, - .getiter = dict_view_getiter, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_dict_view, + MP_QSTR_dict_view, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, dict_view_print, + binary_op, dict_view_binary_op, + getiter, dict_view_getiter + ); STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) { mp_obj_dict_view_t *o = mp_obj_malloc(mp_obj_dict_view_t, &mp_type_dict_view); @@ -585,31 +589,33 @@ STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table); -const mp_obj_type_t mp_type_dict = { - { &mp_type_type }, - .name = MP_QSTR_dict, - .print = dict_print, - .make_new = mp_obj_dict_make_new, - .unary_op = dict_unary_op, - .binary_op = dict_binary_op, - .subscr = dict_subscr, - .getiter = dict_getiter, - .locals_dict = (mp_obj_dict_t *)&dict_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_dict, + MP_QSTR_dict, + MP_TYPE_FLAG_NONE, + mp_obj_dict_make_new, + print, dict_print, + unary_op, dict_unary_op, + binary_op, dict_binary_op, + subscr, dict_subscr, + getiter, dict_getiter, + locals_dict, (mp_obj_dict_t *)&dict_locals_dict + ); #if MICROPY_PY_COLLECTIONS_ORDEREDDICT -const mp_obj_type_t mp_type_ordereddict = { - { &mp_type_type }, - .name = MP_QSTR_OrderedDict, - .print = dict_print, - .make_new = mp_obj_dict_make_new, - .unary_op = dict_unary_op, - .binary_op = dict_binary_op, - .subscr = dict_subscr, - .getiter = dict_getiter, - .parent = &mp_type_dict, - .locals_dict = (mp_obj_dict_t *)&dict_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_ordereddict, + MP_QSTR_OrderedDict, + MP_TYPE_FLAG_NONE, + mp_obj_dict_make_new, + print, dict_print, + unary_op, dict_unary_op, + binary_op, dict_binary_op, + subscr, dict_subscr, + getiter, dict_getiter, + parent, &mp_type_dict, + locals_dict, (mp_obj_dict_t *)&dict_locals_dict + ); #endif void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) { diff --git a/py/objenumerate.c b/py/objenumerate.c index 241aef30236ff..f4f4ff6ae1d93 100644 --- a/py/objenumerate.c +++ b/py/objenumerate.c @@ -67,13 +67,14 @@ STATIC mp_obj_t enumerate_make_new(const mp_obj_type_t *type, size_t n_args, siz return MP_OBJ_FROM_PTR(o); } -const mp_obj_type_t mp_type_enumerate = { - { &mp_type_type }, - .name = MP_QSTR_enumerate, - .make_new = enumerate_make_new, - .iternext = enumerate_iternext, - .getiter = mp_identity_getiter, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_enumerate, + MP_QSTR_enumerate, + MP_TYPE_FLAG_NONE, + enumerate_make_new, + iternext, enumerate_iternext, + getiter, mp_identity_getiter + ); STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) { assert(mp_obj_is_type(self_in, &mp_type_enumerate)); diff --git a/py/objexcept.c b/py/objexcept.c index 028b73fd8b217..190213e12f610 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -284,13 +284,14 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_BaseException = { - { &mp_type_type }, - .name = MP_QSTR_BaseException, - .print = mp_obj_exception_print, - .make_new = mp_obj_exception_make_new, - .attr = mp_obj_exception_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_BaseException, + MP_QSTR_BaseException, + MP_TYPE_FLAG_NONE, + mp_obj_exception_make_new, + print, mp_obj_exception_print, + attr, mp_obj_exception_attr + ); // *FORMAT-OFF* diff --git a/py/objfilter.c b/py/objfilter.c index a402d8c648899..2b57300af3df5 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -60,12 +60,13 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) { return MP_OBJ_STOP_ITERATION; } -const mp_obj_type_t mp_type_filter = { - { &mp_type_type }, - .name = MP_QSTR_filter, - .make_new = filter_make_new, - .getiter = mp_identity_getiter, - .iternext = filter_iternext, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_filter, + MP_QSTR_filter, + MP_TYPE_FLAG_NONE, + filter_make_new, + getiter, mp_identity_getiter, + iternext, filter_iternext + ); #endif // MICROPY_PY_BUILTINS_FILTER diff --git a/py/objfloat.c b/py/objfloat.c index 8e89b3da37beb..9ecbab7a4dede 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -182,15 +182,12 @@ STATIC mp_obj_t float_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs return mp_obj_float_binary_op(op, lhs_val, rhs_in); } -const mp_obj_type_t mp_type_float = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - .name = MP_QSTR_float, - .print = float_print, - .make_new = float_make_new, - .unary_op = float_unary_op, - .binary_op = float_binary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_float, MP_QSTR_float, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, float_make_new, + print, float_print, + unary_op, float_unary_op, + binary_op, float_binary_op + ); #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D diff --git a/py/objfun.c b/py/objfun.c index 5fa9d71ddaf42..30de8670a1de9 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -56,13 +56,11 @@ STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, return self->fun._0(); } -const mp_obj_type_t mp_type_fun_builtin_0 = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, - .name = MP_QSTR_function, - .call = fun_builtin_0_call, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + call, fun_builtin_0_call, + unary_op, mp_generic_unary_op + ); STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_1)); @@ -71,13 +69,11 @@ STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, return self->fun._1(args[0]); } -const mp_obj_type_t mp_type_fun_builtin_1 = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, - .name = MP_QSTR_function, - .call = fun_builtin_1_call, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + call, fun_builtin_1_call, + unary_op, mp_generic_unary_op + ); STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_2)); @@ -86,13 +82,11 @@ STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, return self->fun._2(args[0], args[1]); } -const mp_obj_type_t mp_type_fun_builtin_2 = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, - .name = MP_QSTR_function, - .call = fun_builtin_2_call, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + call, fun_builtin_2_call, + unary_op, mp_generic_unary_op + ); STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_3)); @@ -101,13 +95,11 @@ STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, return self->fun._3(args[0], args[1], args[2]); } -const mp_obj_type_t mp_type_fun_builtin_3 = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, - .name = MP_QSTR_function, - .call = fun_builtin_3_call, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + call, fun_builtin_3_call, + unary_op, mp_generic_unary_op + ); STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_var)); @@ -132,13 +124,11 @@ STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_k } } -const mp_obj_type_t mp_type_fun_builtin_var = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, - .name = MP_QSTR_function, - .call = fun_builtin_var_call, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + call, fun_builtin_var_call, + unary_op, mp_generic_unary_op + ); /******************************************************************************/ /* byte code functions */ @@ -362,19 +352,20 @@ void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #endif -const mp_obj_type_t mp_type_fun_bc = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_function, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_bc, + MP_QSTR_function, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, #if MICROPY_CPYTHON_COMPAT - .print = fun_bc_print, + print, fun_bc_print, #endif - .call = fun_bc_call, - .unary_op = mp_generic_unary_op, + call, fun_bc_call, + unary_op, mp_generic_unary_op #if MICROPY_PY_FUNCTION_ATTRS - .attr = mp_obj_fun_bc_attr, + , attr, mp_obj_fun_bc_attr #endif -}; + ); mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_module_context_t *context, struct _mp_raw_code_t *const *child_table) { size_t n_def_args = 0; @@ -417,19 +408,20 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, co return fun(self_in, n_args, n_kw, args); } -STATIC const mp_obj_type_t mp_type_fun_native = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_function, +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_native, + MP_QSTR_function, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, #if MICROPY_CPYTHON_COMPAT - .print = fun_bc_print, + print, fun_bc_print, #endif - .call = fun_native_call, - .unary_op = mp_generic_unary_op, #if MICROPY_PY_FUNCTION_ATTRS - .attr = mp_obj_fun_bc_attr, + attr, mp_obj_fun_bc_attr, #endif -}; + call, fun_native_call, + unary_op, mp_generic_unary_op + ); mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) { mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(mp_obj_new_fun_bc(def_args, (const byte *)fun_data, mc, child_table)); @@ -531,13 +523,14 @@ STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const return mp_native_to_obj(ret, self->type_sig); } -STATIC const mp_obj_type_t mp_type_fun_asm = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_function, - .call = fun_asm_call, - .unary_op = mp_generic_unary_op, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_asm, + MP_QSTR_function, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, + call, fun_asm_call, + unary_op, mp_generic_unary_op + ); mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig) { mp_obj_fun_asm_t *o = mp_obj_malloc(mp_obj_fun_asm_t, &mp_type_fun_asm); diff --git a/py/objgenerator.c b/py/objgenerator.c index 802fd45bbdf6a..0ab80ca118255 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -70,16 +70,17 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons return MP_OBJ_FROM_PTR(o); } -const mp_obj_type_t mp_type_gen_wrap = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_generator, - .call = gen_wrap_call, - .unary_op = mp_generic_unary_op, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_gen_wrap, + MP_QSTR_generator, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, + call, gen_wrap_call, + unary_op, mp_generic_unary_op #if MICROPY_PY_FUNCTION_ATTRS - .attr = mp_obj_fun_bc_attr, + , attr, mp_obj_fun_bc_attr #endif -}; + ); /******************************************************************************/ // native generator wrapper @@ -131,16 +132,17 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k return MP_OBJ_FROM_PTR(o); } -const mp_obj_type_t mp_type_native_gen_wrap = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_generator, - .call = native_gen_wrap_call, - .unary_op = mp_generic_unary_op, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_native_gen_wrap, + MP_QSTR_generator, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, + call, native_gen_wrap_call, + unary_op, mp_generic_unary_op #if MICROPY_PY_FUNCTION_ATTRS - .attr = mp_obj_fun_bc_attr, + , attr, mp_obj_fun_bc_attr #endif -}; + ); #endif // MICROPY_EMIT_NATIVE @@ -357,12 +359,14 @@ STATIC const mp_rom_map_elem_t gen_instance_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table); -const mp_obj_type_t mp_type_gen_instance = { - { &mp_type_type }, - .name = MP_QSTR_generator, - .print = gen_instance_print, - .unary_op = mp_generic_unary_op, - .getiter = mp_identity_getiter, - .iternext = gen_instance_iternext, - .locals_dict = (mp_obj_dict_t *)&gen_instance_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_gen_instance, + MP_QSTR_generator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, gen_instance_print, + unary_op, mp_generic_unary_op, + getiter, mp_identity_getiter, + iternext, gen_instance_iternext, + locals_dict, (mp_obj_dict_t *)&gen_instance_locals_dict + ); diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index 31ed4a9228ce5..ed2dfbbe1f9e7 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -56,12 +56,14 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) { } } -STATIC const mp_obj_type_t mp_type_it = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = it_iternext, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_it, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, it_iternext + ); // args are those returned from mp_load_method_maybe (ie either an attribute or a method) mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf) { diff --git a/py/objint.c b/py/objint.c index 645b26996689c..d7a3f9eb9d18d 100644 --- a/py/objint.c +++ b/py/objint.c @@ -457,12 +457,13 @@ STATIC const mp_rom_map_elem_t int_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table); -const mp_obj_type_t mp_type_int = { - { &mp_type_type }, - .name = MP_QSTR_int, - .print = mp_obj_int_print, - .make_new = mp_obj_int_make_new, - .unary_op = mp_obj_int_unary_op, - .binary_op = mp_obj_int_binary_op, - .locals_dict = (mp_obj_dict_t *)&int_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_int, + MP_QSTR_int, + MP_TYPE_FLAG_NONE, + mp_obj_int_make_new, + print, mp_obj_int_print, + unary_op, mp_obj_int_unary_op, + binary_op, mp_obj_int_binary_op, + locals_dict, (mp_obj_dict_t *)&int_locals_dict + ); diff --git a/py/objlist.c b/py/objlist.c index f431e273df0af..8c7921b9891b9 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -452,17 +452,19 @@ STATIC const mp_rom_map_elem_t list_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table); -const mp_obj_type_t mp_type_list = { - { &mp_type_type }, - .name = MP_QSTR_list, - .print = list_print, - .make_new = list_make_new, - .unary_op = list_unary_op, - .binary_op = list_binary_op, - .subscr = list_subscr, - .getiter = list_getiter, - .locals_dict = (mp_obj_dict_t *)&list_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_list, + MP_QSTR_list, + MP_TYPE_FLAG_NONE, + list_make_new, + print, list_print, + unary_op, list_unary_op, + binary_op, list_binary_op, + subscr, list_subscr, + getiter, list_getiter, + locals_dict, (mp_obj_dict_t *)&list_locals_dict + ); + void mp_obj_list_init(mp_obj_list_t *o, size_t n) { o->base.type = &mp_type_list; diff --git a/py/objmap.c b/py/objmap.c index 1f9275854f833..dc305e21b5cb7 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -63,10 +63,11 @@ STATIC mp_obj_t map_iternext(mp_obj_t self_in) { return mp_call_function_n_kw(self->fun, self->n_iters, 0, nextses); } -const mp_obj_type_t mp_type_map = { - { &mp_type_type }, - .name = MP_QSTR_map, - .make_new = map_make_new, - .getiter = mp_identity_getiter, - .iternext = map_iternext, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_map, + MP_QSTR_map, + MP_TYPE_FLAG_NONE, + map_make_new, + getiter, mp_identity_getiter, + iternext, map_iternext + ); diff --git a/py/objmodule.c b/py/objmodule.c index 783d6b050868b..6fc3653e6a24d 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -130,12 +130,14 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_module = { - { &mp_type_type }, - .name = MP_QSTR_module, - .print = module_print, - .attr = module_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_module, + MP_QSTR_module, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, module_print, + attr, module_attr + ); mp_obj_t mp_obj_new_module(qstr module_name) { mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map; diff --git a/py/objnone.c b/py/objnone.c index 271a8543f9718..4fffbc997e89e 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -43,12 +43,14 @@ STATIC void none_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_ } } -const mp_obj_type_t mp_type_NoneType = { - { &mp_type_type }, - .name = MP_QSTR_NoneType, - .print = none_print, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_NoneType, + MP_QSTR_NoneType, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, none_print, + unary_op, mp_generic_unary_op + ); #if !MICROPY_OBJ_IMMEDIATE_OBJS const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}}; diff --git a/py/objobject.c b/py/objobject.c index 1652802805bbc..617b40fbbf05e 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -111,11 +111,12 @@ STATIC const mp_rom_map_elem_t object_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(object_locals_dict, object_locals_dict_table); #endif -const mp_obj_type_t mp_type_object = { - { &mp_type_type }, - .name = MP_QSTR_object, - .make_new = object_make_new, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_object, + MP_QSTR_object, + MP_TYPE_FLAG_NONE, + object_make_new #if MICROPY_CPYTHON_COMPAT - .locals_dict = (mp_obj_dict_t *)&object_locals_dict, + , locals_dict, (mp_obj_dict_t *)&object_locals_dict #endif -}; + ); diff --git a/py/objpolyiter.c b/py/objpolyiter.c index dac6a2545575f..326153182bca7 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -45,12 +45,14 @@ STATIC mp_obj_t polymorph_it_iternext(mp_obj_t self_in) { return self->iternext(self_in); } -const mp_obj_type_t mp_type_polymorph_iter = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = polymorph_it_iternext, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_polymorph_iter, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, polymorph_it_iternext + ); #if MICROPY_ENABLE_FINALISER // mp_type_polymorph_iter_with_finaliser is a variant of the universal iterator @@ -76,12 +78,13 @@ STATIC const mp_rom_map_elem_t mp_obj_polymorph_iter_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(mp_obj_polymorph_iter_locals_dict, mp_obj_polymorph_iter_locals_dict_table); -const mp_obj_type_t mp_type_polymorph_iter_with_finaliser = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = polymorph_it_iternext, - .locals_dict = (mp_obj_dict_t *)&mp_obj_polymorph_iter_locals_dict, -}; - +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_polymorph_iter_with_finaliser, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, polymorph_it_iternext, + locals_dict, (mp_obj_dict_t *)&mp_obj_polymorph_iter_locals_dict + ); #endif diff --git a/py/objproperty.c b/py/objproperty.c index 49327c981e0b5..42c357f33050b 100644 --- a/py/objproperty.c +++ b/py/objproperty.c @@ -90,12 +90,13 @@ STATIC const mp_rom_map_elem_t property_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(property_locals_dict, property_locals_dict_table); -const mp_obj_type_t mp_type_property = { - { &mp_type_type }, - .name = MP_QSTR_property, - .make_new = property_make_new, - .locals_dict = (mp_obj_dict_t *)&property_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_property, + MP_QSTR_property, + MP_TYPE_FLAG_NONE, + property_make_new, + locals_dict, (mp_obj_dict_t *)&property_locals_dict + ); const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) { mp_check_self(mp_obj_is_type(self_in, &mp_type_property)); diff --git a/py/objrange.c b/py/objrange.c index 5496021892cb1..adf4b17466ad5 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -50,12 +50,14 @@ STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) { } } -STATIC const mp_obj_type_t mp_type_range_it = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = range_it_iternext, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_range_it, + MP_QSTR_iterator, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + getiter, mp_identity_getiter, + iternext, range_it_iternext + ); STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(mp_obj_range_it_t) <= sizeof(mp_obj_iter_buf_t)); @@ -208,18 +210,19 @@ STATIC void range_attr(mp_obj_t o_in, qstr attr, mp_obj_t *dest) { } #endif -const mp_obj_type_t mp_type_range = { - { &mp_type_type }, - .name = MP_QSTR_range, - .print = range_print, - .make_new = range_make_new, - .unary_op = range_unary_op, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_range, + MP_QSTR_range, + MP_TYPE_FLAG_NONE, + range_make_new, + print, range_print, + unary_op, range_unary_op, #if MICROPY_PY_BUILTINS_RANGE_BINOP - .binary_op = range_binary_op, + binary_op, range_binary_op, #endif - .subscr = range_subscr, - .getiter = range_getiter, + subscr, range_subscr, + getiter, range_getiter #if MICROPY_PY_BUILTINS_RANGE_ATTRS - .attr = range_attr, + , attr, range_attr #endif -}; + ); diff --git a/py/objreversed.c b/py/objreversed.c index 08961c0d2d899..bc1f07ddecc46 100644 --- a/py/objreversed.c +++ b/py/objreversed.c @@ -68,12 +68,13 @@ STATIC mp_obj_t reversed_iternext(mp_obj_t self_in) { return mp_obj_subscr(self->seq, MP_OBJ_NEW_SMALL_INT(self->cur_index), MP_OBJ_SENTINEL); } -const mp_obj_type_t mp_type_reversed = { - { &mp_type_type }, - .name = MP_QSTR_reversed, - .make_new = reversed_make_new, - .getiter = mp_identity_getiter, - .iternext = reversed_iternext, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_reversed, + MP_QSTR_reversed, + MP_TYPE_FLAG_NONE, + reversed_make_new, + getiter, mp_identity_getiter, + iternext, reversed_iternext + ); #endif // MICROPY_PY_BUILTINS_REVERSED diff --git a/py/objset.c b/py/objset.c index 26fd74398b8a9..8fc744a14047e 100644 --- a/py/objset.c +++ b/py/objset.c @@ -539,16 +539,17 @@ STATIC const mp_rom_map_elem_t set_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table); -const mp_obj_type_t mp_type_set = { - { &mp_type_type }, - .name = MP_QSTR_set, - .print = set_print, - .make_new = set_make_new, - .unary_op = set_unary_op, - .binary_op = set_binary_op, - .getiter = set_getiter, - .locals_dict = (mp_obj_dict_t *)&set_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_set, + MP_QSTR_set, + MP_TYPE_FLAG_NONE, + set_make_new, + print, set_print, + unary_op, set_unary_op, + binary_op, set_binary_op, + getiter, set_getiter, + locals_dict, (mp_obj_dict_t *)&set_locals_dict + ); #if MICROPY_PY_BUILTINS_FROZENSET STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = { @@ -564,17 +565,17 @@ STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table); -const mp_obj_type_t mp_type_frozenset = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - .name = MP_QSTR_frozenset, - .print = set_print, - .make_new = set_make_new, - .unary_op = set_unary_op, - .binary_op = set_binary_op, - .getiter = set_getiter, - .locals_dict = (mp_obj_dict_t *)&frozenset_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_frozenset, + MP_QSTR_frozenset, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + set_make_new, + print, set_print, + unary_op, set_unary_op, + binary_op, set_binary_op, + getiter, set_getiter, + locals_dict, (mp_obj_dict_t *)&frozenset_locals_dict + ); #endif mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) { diff --git a/py/objsingleton.c b/py/objsingleton.c index 2b896305cff5b..4a099657d4be4 100644 --- a/py/objsingleton.c +++ b/py/objsingleton.c @@ -43,12 +43,11 @@ STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ mp_printf(print, "%q", self->name); } -const mp_obj_type_t mp_type_singleton = { - { &mp_type_type }, - .name = MP_QSTR_, - .print = singleton_print, - .unary_op = mp_generic_unary_op, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_singleton, MP_QSTR_, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, + print, singleton_print, + unary_op, mp_generic_unary_op + ); const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_Ellipsis}; #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED diff --git a/py/objslice.c b/py/objslice.c index 0b34516c185a7..98c03485ff39e 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -92,16 +92,18 @@ STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table); #endif -const mp_obj_type_t mp_type_slice = { - { &mp_type_type }, - .name = MP_QSTR_slice, - .print = slice_print, +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_slice, + MP_QSTR_slice, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, slice_print #if MICROPY_PY_BUILTINS_SLICE_ATTRS - .attr = slice_attr, + , attr, slice_attr #elif MICROPY_PY_BUILTINS_SLICE_INDICES - .locals_dict = (mp_obj_dict_t *)&slice_locals_dict, + , locals_dict, (mp_obj_dict_t *)&slice_locals_dict #endif -}; + ); mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { mp_obj_slice_t *o = mp_obj_malloc(mp_obj_slice_t, &mp_type_slice); diff --git a/py/objstr.c b/py/objstr.c index 9dd7f65e66cae..77ca269d423bb 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2143,31 +2143,33 @@ MP_DEFINE_CONST_DICT_WITH_SIZE(mp_obj_memoryview_locals_dict, #if !MICROPY_PY_BUILTINS_STR_UNICODE STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); -const mp_obj_type_t mp_type_str = { - { &mp_type_type }, - .name = MP_QSTR_str, - .print = str_print, - .make_new = mp_obj_str_make_new, - .binary_op = mp_obj_str_binary_op, - .subscr = bytes_subscr, - .getiter = mp_obj_new_str_iterator, - .buffer = mp_obj_str_get_buffer, - .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_str, + MP_QSTR_str, + MP_TYPE_FLAG_NONE, + mp_obj_str_make_new, + print, str_print, + binary_op, mp_obj_str_binary_op, + subscr, bytes_subscr, + getiter, mp_obj_new_str_iterator, + buffer, mp_obj_str_get_buffer, + locals_dict, (mp_obj_dict_t *)&mp_obj_str_locals_dict + ); #endif // !MICROPY_PY_BUILTINS_STR_UNICODE -// Reuses most of methods from str -const mp_obj_type_t mp_type_bytes = { - { &mp_type_type }, - .name = MP_QSTR_bytes, - .print = str_print, - .make_new = bytes_make_new, - .binary_op = mp_obj_str_binary_op, - .subscr = bytes_subscr, - .getiter = mp_obj_new_bytes_iterator, - .buffer = mp_obj_str_get_buffer, - .locals_dict = (mp_obj_dict_t *)&mp_obj_bytes_locals_dict, -}; +// Reuses most methods from str +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bytes, + MP_QSTR_bytes, + MP_TYPE_FLAG_NONE, + bytes_make_new, + print, str_print, + binary_op, mp_obj_str_binary_op, + subscr, bytes_subscr, + getiter, mp_obj_new_bytes_iterator, + buffer, mp_obj_str_get_buffer, + locals_dict, (mp_obj_dict_t *)&mp_obj_bytes_locals_dict + ); // The zero-length bytes object, with data that includes a null-terminating byte const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, (const byte *)""}; diff --git a/py/objstringio.c b/py/objstringio.c index 8b6c7531d79de..d781ccc789d20 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -244,16 +244,17 @@ STATIC const mp_stream_p_t stringio_stream_p = { .is_text = true, }; -const mp_obj_type_t mp_type_stringio = { - { &mp_type_type }, - .name = MP_QSTR_StringIO, - .print = stringio_print, - .make_new = stringio_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &stringio_stream_p, - .locals_dict = (mp_obj_dict_t *)&stringio_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_stringio, + MP_QSTR_StringIO, + MP_TYPE_FLAG_NONE, + stringio_make_new, + print, stringio_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &stringio_stream_p, + locals_dict, (mp_obj_dict_t *)&stringio_locals_dict + ); #if MICROPY_PY_IO_BYTESIO STATIC const mp_stream_p_t bytesio_stream_p = { @@ -262,16 +263,17 @@ STATIC const mp_stream_p_t bytesio_stream_p = { .ioctl = stringio_ioctl, }; -const mp_obj_type_t mp_type_bytesio = { - { &mp_type_type }, - .name = MP_QSTR_BytesIO, - .print = stringio_print, - .make_new = stringio_make_new, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &bytesio_stream_p, - .locals_dict = (mp_obj_dict_t *)&stringio_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_bytesio, + MP_QSTR_BytesIO, + MP_TYPE_FLAG_NONE, + stringio_make_new, + print, stringio_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &bytesio_stream_p, + locals_dict, (mp_obj_dict_t *)&stringio_locals_dict + ); #endif #endif diff --git a/py/objstrunicode.c b/py/objstrunicode.c index fef0353683c19..afef1498e98e7 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -229,18 +229,19 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } } -const mp_obj_type_t mp_type_str = { - { &mp_type_type }, - .name = MP_QSTR_str, - .print = uni_print, - .make_new = mp_obj_str_make_new, - .unary_op = uni_unary_op, - .binary_op = mp_obj_str_binary_op, - .subscr = str_subscr, - .getiter = mp_obj_new_str_iterator, - .buffer = mp_obj_str_get_buffer, - .locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_str, + MP_QSTR_str, + MP_TYPE_FLAG_NONE, + mp_obj_str_make_new, + print, uni_print, + unary_op, uni_unary_op, + binary_op, mp_obj_str_binary_op, + subscr, str_subscr, + getiter, mp_obj_new_str_iterator, + buffer, mp_obj_str_get_buffer, + locals_dict, (mp_obj_dict_t *)&mp_obj_str_locals_dict + ); /******************************************************************************/ /* str iterator */ diff --git a/py/objtuple.c b/py/objtuple.c index e0cec84473639..b2ea6e380eb1d 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -224,17 +224,18 @@ STATIC const mp_rom_map_elem_t tuple_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table); -const mp_obj_type_t mp_type_tuple = { - { &mp_type_type }, - .name = MP_QSTR_tuple, - .print = mp_obj_tuple_print, - .make_new = mp_obj_tuple_make_new, - .unary_op = mp_obj_tuple_unary_op, - .binary_op = mp_obj_tuple_binary_op, - .subscr = mp_obj_tuple_subscr, - .getiter = mp_obj_tuple_getiter, - .locals_dict = (mp_obj_dict_t *)&tuple_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_tuple, + MP_QSTR_tuple, + MP_TYPE_FLAG_NONE, + mp_obj_tuple_make_new, + print, mp_obj_tuple_print, + unary_op, mp_obj_tuple_unary_op, + binary_op, mp_obj_tuple_binary_op, + subscr, mp_obj_tuple_subscr, + getiter, mp_obj_tuple_getiter, + locals_dict, (mp_obj_dict_t *)&tuple_locals_dict + ); // the zero-length tuple const mp_obj_tuple_t mp_const_empty_tuple_obj = {{&mp_type_tuple}, 0}; diff --git a/py/objtype.c b/py/objtype.c index c0f68578092dc..77fe8e22e3844 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1098,15 +1098,16 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_type = { - { &mp_type_type }, - .name = MP_QSTR_type, - .print = type_print, - .make_new = type_make_new, - .call = type_call, - .unary_op = mp_generic_unary_op, - .attr = type_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_type, + MP_QSTR_type, + MP_TYPE_FLAG_NONE, + type_make_new, + print, type_print, + call, type_call, + unary_op, mp_generic_unary_op, + attr, type_attr + ); mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) { // Verify input objects have expected type @@ -1314,13 +1315,14 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_obj_class_lookup(&lookup, &mp_type_object); } -const mp_obj_type_t mp_type_super = { - { &mp_type_type }, - .name = MP_QSTR_super, - .print = super_print, - .make_new = super_make_new, - .attr = super_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_super, + MP_QSTR_super, + MP_TYPE_FLAG_NONE, + super_make_new, + print, super_print, + attr, super_attr + ); void mp_load_super_method(qstr attr, mp_obj_t *dest) { mp_obj_super_t super = {{&mp_type_super}, dest[1], dest[2]}; @@ -1436,14 +1438,16 @@ STATIC mp_obj_t static_class_method_make_new(const mp_obj_type_t *self, size_t n return MP_OBJ_FROM_PTR(o); } -const mp_obj_type_t mp_type_staticmethod = { - { &mp_type_type }, - .name = MP_QSTR_staticmethod, - .make_new = static_class_method_make_new, -}; - -const mp_obj_type_t mp_type_classmethod = { - { &mp_type_type }, - .name = MP_QSTR_classmethod, - .make_new = static_class_method_make_new, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_staticmethod, + MP_QSTR_staticmethod, + MP_TYPE_FLAG_NONE, + static_class_method_make_new + ); + +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_classmethod, + MP_QSTR_classmethod, + MP_TYPE_FLAG_NONE, + static_class_method_make_new + ); diff --git a/py/objzip.c b/py/objzip.c index 81fa1d587e37a..0ceafd97f2409 100644 --- a/py/objzip.c +++ b/py/objzip.c @@ -66,10 +66,11 @@ STATIC mp_obj_t zip_iternext(mp_obj_t self_in) { return MP_OBJ_FROM_PTR(tuple); } -const mp_obj_type_t mp_type_zip = { - { &mp_type_type }, - .name = MP_QSTR_zip, - .make_new = zip_make_new, - .getiter = mp_identity_getiter, - .iternext = zip_iternext, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_zip, + MP_QSTR_zip, + MP_TYPE_FLAG_NONE, + zip_make_new, + getiter, mp_identity_getiter, + iternext, zip_iternext + ); diff --git a/py/profile.c b/py/profile.c index 4e23e9eac43b5..2b9531e245ac5 100644 --- a/py/profile.c +++ b/py/profile.c @@ -172,13 +172,15 @@ STATIC void code_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_settrace_codeobj = { - { &mp_type_type }, - .name = MP_QSTR_code, - .print = code_print, - .unary_op = mp_generic_unary_op, - .attr = code_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_settrace_codeobj, + MP_QSTR_code, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, code_print, + unary_op, mp_generic_unary_op, + attr, code_attr + ); mp_obj_t mp_obj_new_code(const mp_module_context_t *context, const mp_raw_code_t *rc) { mp_obj_code_t *o = m_new_obj_maybe(mp_obj_code_t); @@ -241,13 +243,15 @@ STATIC void frame_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -const mp_obj_type_t mp_type_frame = { - { &mp_type_type }, - .name = MP_QSTR_frame, - .print = frame_print, - .unary_op = mp_generic_unary_op, - .attr = frame_attr, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_frame, + MP_QSTR_frame, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, frame_print, + unary_op, mp_generic_unary_op, + attr, frame_attr + ); mp_obj_t mp_obj_new_frame(const mp_code_state_t *code_state) { if (gc_is_locked()) { diff --git a/py/runtime.c b/py/runtime.c index ea3553db72275..27b4f05f0460d 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1070,12 +1070,13 @@ STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c return mp_call_function_n_kw(self->fun, n_args, n_kw, args); } -STATIC const mp_obj_type_t mp_type_checked_fun = { - { &mp_type_type }, - .flags = MP_TYPE_FLAG_BINDS_SELF, - .name = MP_QSTR_function, - .call = checked_fun_call, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + mp_type_checked_fun, + MP_QSTR_function, + MP_TYPE_FLAG_BINDS_SELF, + MP_TYPE_NULL_MAKE_NEW, + call, checked_fun_call + ); STATIC mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) { mp_obj_checked_fun_t *o = mp_obj_malloc(mp_obj_checked_fun_t, &mp_type_checked_fun); diff --git a/shared/runtime/mpirq.c b/shared/runtime/mpirq.c index 8e474bf5a2b13..763da6e0e4449 100644 --- a/shared/runtime/mpirq.c +++ b/shared/runtime/mpirq.c @@ -125,11 +125,13 @@ STATIC const mp_rom_map_elem_t mp_irq_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(mp_irq_locals_dict, mp_irq_locals_dict_table); -const mp_obj_type_t mp_irq_type = { - { &mp_type_type }, - .name = MP_QSTR_irq, - .call = mp_irq_call, - .locals_dict = (mp_obj_dict_t *)&mp_irq_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + mp_irq_type, + MP_QSTR_irq, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + call, mp_irq_call, + locals_dict, (mp_obj_dict_t *)&mp_irq_locals_dict + ); #endif // MICROPY_ENABLE_SCHEDULER diff --git a/shared/runtime/sys_stdio_mphal.c b/shared/runtime/sys_stdio_mphal.c index 24f528b0c4f85..f1290853da734 100644 --- a/shared/runtime/sys_stdio_mphal.c +++ b/shared/runtime/sys_stdio_mphal.c @@ -123,15 +123,17 @@ STATIC const mp_stream_p_t stdio_obj_stream_p = { .is_text = true, }; -STATIC const mp_obj_type_t stdio_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .print = stdio_obj_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &stdio_obj_stream_p, - .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + stdio_obj_type, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, stdio_obj_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &stdio_obj_stream_p, + locals_dict, (mp_obj_dict_t *)&stdio_locals_dict + ); const sys_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN}; const sys_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT}; @@ -157,15 +159,17 @@ STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = { .is_text = false, }; -STATIC const mp_obj_type_t stdio_buffer_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .print = stdio_obj_print, - .getiter = mp_identity_getiter, - .iternext = mp_stream_unbuffered_iter, - .protocol = &stdio_buffer_obj_stream_p, - .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict, -}; +STATIC MP_DEFINE_CONST_OBJ_TYPE( + stdio_buffer_obj_type, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + MP_TYPE_NULL_MAKE_NEW, + print, stdio_obj_print, + getiter, mp_identity_getiter, + iternext, mp_stream_unbuffered_iter, + protocol, &stdio_buffer_obj_stream_p, + locals_dict, (mp_obj_dict_t *)&stdio_locals_dict + ); STATIC const sys_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused #endif From b7d6ee9b75650bd0ac36e89077d5d08a3eed9e3f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 24 Jun 2022 16:22:38 +1000 Subject: [PATCH 2617/3533] all: Fix #if inside MP_DEFINE_CONST_OBJ_TYPE for msvc. Changes: MP_DEFINE_CONST_OBJ_TYPE( ... #if FOO ... #endif ... ); to: MP_DEFINE_CONST_OBJ_TYPE( ... FOO_TYPE_ATTR ... ); Signed-off-by: Jim Mussared --- extmod/vfs_lfsx.c | 14 +++++++++----- py/objarray.c | 26 +++++++++++++++++--------- py/objboundmeth.c | 20 ++++++++++++++------ py/objcell.c | 10 +++++++--- py/objclosure.c | 17 +++++++++++------ py/objfun.c | 39 +++++++++++++++++++++++++++------------ py/objgenerator.c | 20 ++++++++++++++------ py/objobject.c | 10 +++++++--- py/objrange.c | 20 ++++++++++++++------ py/objslice.c | 14 +++++++++----- 10 files changed, 129 insertions(+), 61 deletions(-) diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 72c6696105d9e..3f8d262a4858f 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -502,15 +502,19 @@ STATIC const mp_vfs_proto_t MP_VFS_LFSx(proto) = { .import_stat = MP_VFS_LFSx(import_stat), }; +#if LFS_BUILD_VERSION == 1 +#define VFS_LFSx_QSTR MP_QSTR_VfsLfs1 +#else +#define VFS_LFSx_QSTR MP_QSTR_VfsLfs2 +#endif + MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx, - #if LFS_BUILD_VERSION == 1 - MP_QSTR_VfsLfs1, - #else - MP_QSTR_VfsLfs2, - #endif + VFS_LFSx_QSTR, MP_TYPE_FLAG_NONE, MP_VFS_LFSx(make_new), protocol, &MP_VFS_LFSx(proto), locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(locals_dict) ); + +#undef VFS_LFSx_QSTR diff --git a/py/objarray.c b/py/objarray.c index d93cce29eef5a..8d0fe7f58503c 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -603,6 +603,18 @@ MP_DEFINE_CONST_OBJ_TYPE( #endif #if MICROPY_PY_BUILTINS_MEMORYVIEW +#if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE +#define MEMORYVIEW_TYPE_ATTR attr, memoryview_attr, +#else +#define MEMORYVIEW_TYPE_ATTR +#endif + +#if MICROPY_PY_BUILTINS_BYTES_HEX +#define MEMORYVIEW_TYPE_LOCALS_DICT locals_dict, (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, +#else +#define MEMORYVIEW_TYPE_LOCALS_DICT +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_memoryview, MP_QSTR_memoryview, @@ -611,16 +623,12 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, array_iterator_new, unary_op, array_unary_op, binary_op, array_binary_op, - #if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE - attr, memoryview_attr, - #endif - #if MICROPY_PY_BUILTINS_BYTES_HEX - locals_dict, (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, - #endif - .subscr = array_subscr, - .buffer = array_get_buffer, + MEMORYVIEW_TYPE_LOCALS_DICT + MEMORYVIEW_TYPE_ATTR + subscr, array_subscr, + buffer, array_get_buffer ); -#endif +#endif // MICROPY_PY_BUILTINS_MEMORYVIEW /* unused size_t mp_obj_array_len(mp_obj_t self_in) { diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 353364cdc70ab..f4b3b9b7df9ce 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -95,18 +95,26 @@ STATIC void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #endif +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED +#define BOUND_METH_TYPE_PRINT print, bound_meth_print, +#else +#define BOUND_METH_TYPE_PRINT +#endif + +#if MICROPY_PY_FUNCTION_ATTRS +#define BOUND_METH_TYPE_ATTR attr, bound_meth_attr, +#else +#define BOUND_METH_TYPE_ATTR +#endif + STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_bound_meth, MP_QSTR_bound_method, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - print, bound_meth_print, - #endif + BOUND_METH_TYPE_PRINT + BOUND_METH_TYPE_ATTR call, bound_meth_call - #if MICROPY_PY_FUNCTION_ATTRS - , attr, bound_meth_attr - #endif ); mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self) { diff --git a/py/objcell.c b/py/objcell.c index a17a94b9b78e9..b100fae4fe994 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -40,12 +40,16 @@ STATIC void cell_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t k } #endif +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED +#define CELL_TYPE_PRINT , print, cell_print +#else +#define CELL_TYPE_PRINT +#endif + STATIC MP_DEFINE_CONST_OBJ_TYPE( // cell representation is just value in < > mp_type_cell, MP_QSTR_, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - , print, cell_print - #endif + CELL_TYPE_PRINT ); mp_obj_t mp_obj_new_cell(mp_obj_t obj) { diff --git a/py/objclosure.c b/py/objclosure.c index 15ed994d37263..45a3e83c462d8 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -84,6 +84,15 @@ STATIC void mp_obj_closure_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_obj_closure_t *o = MP_OBJ_TO_PTR(self_in); mp_load_method_maybe(o->fun, attr, dest); } +#define CLOSURE_TYPE_ATTR attr, mp_obj_closure_attr, +#else +#define CLOSURE_TYPE_ATTR +#endif + +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED +#define CLOSURE_TYPE_PRINT print, closure_print, +#else +#define CLOSURE_TYPE_PRINT #endif MP_DEFINE_CONST_OBJ_TYPE( @@ -91,12 +100,8 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_closure, MP_TYPE_FLAG_BINDS_SELF, MP_TYPE_NULL_MAKE_NEW, - #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED - print, closure_print, - #endif - #if MICROPY_PY_FUNCTION_ATTRS - attr, mp_obj_closure_attr, - #endif + CLOSURE_TYPE_ATTR + CLOSURE_TYPE_PRINT call, closure_call ); diff --git a/py/objfun.c b/py/objfun.c index 30de8670a1de9..d6ff354575bf9 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -352,19 +352,27 @@ void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #endif +#if MICROPY_CPYTHON_COMPAT +#define FUN_BC_TYPE_PRINT print, fun_bc_print, +#else +#define FUN_BC_TYPE_PRINT +#endif + +#if MICROPY_PY_FUNCTION_ATTRS +#define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr, +#else +#define FUN_BC_TYPE_ATTR +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_fun_bc, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, MP_TYPE_NULL_MAKE_NEW, - #if MICROPY_CPYTHON_COMPAT - print, fun_bc_print, - #endif + FUN_BC_TYPE_PRINT + FUN_BC_TYPE_ATTR call, fun_bc_call, unary_op, mp_generic_unary_op - #if MICROPY_PY_FUNCTION_ATTRS - , attr, mp_obj_fun_bc_attr - #endif ); mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_module_context_t *context, struct _mp_raw_code_t *const *child_table) { @@ -408,17 +416,24 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, co return fun(self_in, n_args, n_kw, args); } +#if MICROPY_CPYTHON_COMPAT +#define FUN_BC_TYPE_PRINT print, fun_bc_print, +#else +#define FUN_BC_TYPE_PRINT +#endif +#if MICROPY_PY_FUNCTION_ATTRS +#define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr, +#else +#define FUN_BC_TYPE_ATTR +#endif + STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_fun_native, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, MP_TYPE_NULL_MAKE_NEW, - #if MICROPY_CPYTHON_COMPAT - print, fun_bc_print, - #endif - #if MICROPY_PY_FUNCTION_ATTRS - attr, mp_obj_fun_bc_attr, - #endif + FUN_BC_TYPE_PRINT + FUN_BC_TYPE_ATTR call, fun_native_call, unary_op, mp_generic_unary_op ); diff --git a/py/objgenerator.c b/py/objgenerator.c index 0ab80ca118255..299f25e7bb634 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -70,16 +70,20 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons return MP_OBJ_FROM_PTR(o); } +#if MICROPY_PY_FUNCTION_ATTRS +#define GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr, +#else +#define GEN_WRAP_TYPE_ATTR +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_gen_wrap, MP_QSTR_generator, MP_TYPE_FLAG_BINDS_SELF, MP_TYPE_NULL_MAKE_NEW, + GEN_WRAP_TYPE_ATTR call, gen_wrap_call, unary_op, mp_generic_unary_op - #if MICROPY_PY_FUNCTION_ATTRS - , attr, mp_obj_fun_bc_attr - #endif ); /******************************************************************************/ @@ -132,16 +136,20 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k return MP_OBJ_FROM_PTR(o); } +#if MICROPY_PY_FUNCTION_ATTRS +#define NATIVE_GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr, +#else +#define NATIVE_GEN_WRAP_TYPE_ATTR +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_native_gen_wrap, MP_QSTR_generator, MP_TYPE_FLAG_BINDS_SELF, MP_TYPE_NULL_MAKE_NEW, call, native_gen_wrap_call, + NATIVE_GEN_WRAP_TYPE_ATTR unary_op, mp_generic_unary_op - #if MICROPY_PY_FUNCTION_ATTRS - , attr, mp_obj_fun_bc_attr - #endif ); #endif // MICROPY_EMIT_NATIVE diff --git a/py/objobject.c b/py/objobject.c index 617b40fbbf05e..868a85b32ab0c 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -111,12 +111,16 @@ STATIC const mp_rom_map_elem_t object_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(object_locals_dict, object_locals_dict_table); #endif +#if MICROPY_CPYTHON_COMPAT +#define OBJECT_TYPE_LOCALS_DICT , locals_dict, (mp_obj_dict_t *)&object_locals_dict +#else +#define OBJECT_TYPE_LOCALS_DICT +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_object, MP_QSTR_object, MP_TYPE_FLAG_NONE, object_make_new - #if MICROPY_CPYTHON_COMPAT - , locals_dict, (mp_obj_dict_t *)&object_locals_dict - #endif + OBJECT_TYPE_LOCALS_DICT ); diff --git a/py/objrange.c b/py/objrange.c index adf4b17466ad5..3140504b2bb4c 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -210,19 +210,27 @@ STATIC void range_attr(mp_obj_t o_in, qstr attr, mp_obj_t *dest) { } #endif +#if MICROPY_PY_BUILTINS_RANGE_BINOP +#define RANGE_TYPE_BINOP binary_op, range_binary_op, +#else +#define RANGE_TYPE_BINOP +#endif + +#if MICROPY_PY_BUILTINS_RANGE_ATTRS +#define RANGE_TYPE_ATTR attr, range_attr, +#else +#define RANGE_TYPE_ATTR +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_range, MP_QSTR_range, MP_TYPE_FLAG_NONE, range_make_new, + RANGE_TYPE_BINOP + RANGE_TYPE_ATTR print, range_print, unary_op, range_unary_op, - #if MICROPY_PY_BUILTINS_RANGE_BINOP - binary_op, range_binary_op, - #endif subscr, range_subscr, getiter, range_getiter - #if MICROPY_PY_BUILTINS_RANGE_ATTRS - , attr, range_attr - #endif ); diff --git a/py/objslice.c b/py/objslice.c index 98c03485ff39e..7baca1fbe6685 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -92,17 +92,21 @@ STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table); #endif +#if MICROPY_PY_BUILTINS_SLICE_ATTRS +#define SLICE_TYPE_ATTR_OR_LOCALS_DICT attr, slice_attr, +#elif MICROPY_PY_BUILTINS_SLICE_INDICES +#define SLICE_TYPE_ATTR_OR_LOCALS_DICT locals_dict, (mp_obj_dict_t *)&slice_locals_dict, +#else +#define SLICE_TYPE_ATTR_OR_LOCALS_DICT +#endif + MP_DEFINE_CONST_OBJ_TYPE( mp_type_slice, MP_QSTR_slice, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, + SLICE_TYPE_ATTR_OR_LOCALS_DICT print, slice_print - #if MICROPY_PY_BUILTINS_SLICE_ATTRS - , attr, slice_attr - #elif MICROPY_PY_BUILTINS_SLICE_INDICES - , locals_dict, (mp_obj_dict_t *)&slice_locals_dict - #endif ); mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { From 9dce82776db104750283b213095a7aedfb95a1d9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 24 Jun 2022 16:27:46 +1000 Subject: [PATCH 2618/3533] all: Remove unnecessary locals_dict cast. Signed-off-by: Jim Mussared --- extmod/machine_i2c.c | 2 +- extmod/machine_signal.c | 2 +- extmod/machine_spi.c | 2 +- extmod/modbluetooth.c | 2 +- extmod/modbtree.c | 2 +- extmod/modframebuf.c | 2 +- extmod/modlwip.c | 4 ++-- extmod/moduasyncio.c | 2 +- extmod/moducryptolib.c | 2 +- extmod/moduhashlib.c | 6 +++--- extmod/modure.c | 4 ++-- extmod/moduselect.c | 2 +- extmod/modusocket.c | 2 +- extmod/modussl_axtls.c | 2 +- extmod/modussl_mbedtls.c | 2 +- extmod/modutimeq.c | 2 +- extmod/moduwebsocket.c | 2 +- extmod/moduzlib.c | 2 +- extmod/modwebrepl.c | 2 +- extmod/network_cyw43.c | 2 +- extmod/vfs_fat.c | 2 +- extmod/vfs_fat_file.c | 4 ++-- extmod/vfs_lfsx.c | 2 +- extmod/vfs_lfsx_file.c | 4 ++-- extmod/vfs_posix.c | 2 +- extmod/vfs_posix_file.c | 4 ++-- ports/cc3200/misc/mpirq.c | 2 +- ports/cc3200/mods/modnetwork.c | 2 +- ports/cc3200/mods/moduhashlib.c | 4 ++-- ports/cc3200/mods/modusocket.c | 2 +- ports/cc3200/mods/modussl.c | 2 +- ports/cc3200/mods/modwlan.c | 2 +- ports/cc3200/mods/pybadc.c | 4 ++-- ports/cc3200/mods/pybflash.c | 2 +- ports/cc3200/mods/pybi2c.c | 2 +- ports/cc3200/mods/pybpin.c | 4 ++-- ports/cc3200/mods/pybrtc.c | 2 +- ports/cc3200/mods/pybsd.c | 2 +- ports/cc3200/mods/pybspi.c | 2 +- ports/cc3200/mods/pybtimer.c | 4 ++-- ports/cc3200/mods/pybuart.c | 2 +- ports/cc3200/mods/pybwdt.c | 2 +- ports/esp32/esp32_nvs.c | 2 +- ports/esp32/esp32_partition.c | 2 +- ports/esp32/esp32_rmt.c | 2 +- ports/esp32/esp32_ulp.c | 2 +- ports/esp32/machine_adc.c | 2 +- ports/esp32/machine_adcblock.c | 2 +- ports/esp32/machine_dac.c | 2 +- ports/esp32/machine_hw_spi.c | 2 +- ports/esp32/machine_i2c.c | 2 +- ports/esp32/machine_i2s.c | 2 +- ports/esp32/machine_pin.c | 4 ++-- ports/esp32/machine_rtc.c | 2 +- ports/esp32/machine_sdcard.c | 2 +- ports/esp32/machine_timer.c | 2 +- ports/esp32/machine_touchpad.c | 2 +- ports/esp32/machine_uart.c | 2 +- ports/esp32/machine_wdt.c | 2 +- ports/esp32/modsocket.c | 2 +- ports/esp32/network_lan.c | 2 +- ports/esp32/network_ppp.c | 2 +- ports/esp32/network_wlan.c | 2 +- ports/esp8266/machine_adc.c | 2 +- ports/esp8266/machine_hspi.c | 2 +- ports/esp8266/machine_pin.c | 4 ++-- ports/esp8266/machine_rtc.c | 2 +- ports/esp8266/machine_uart.c | 2 +- ports/esp8266/machine_wdt.c | 2 +- ports/esp8266/modmachine.c | 2 +- ports/esp8266/modnetwork.c | 2 +- ports/mimxrt/machine_adc.c | 2 +- ports/mimxrt/machine_i2c.c | 2 +- ports/mimxrt/machine_i2s.c | 2 +- ports/mimxrt/machine_led.c | 2 +- ports/mimxrt/machine_pin.c | 8 ++++---- ports/mimxrt/machine_rtc.c | 2 +- ports/mimxrt/machine_sdcard.c | 2 +- ports/mimxrt/machine_spi.c | 2 +- ports/mimxrt/machine_timer.c | 2 +- ports/mimxrt/machine_uart.c | 2 +- ports/mimxrt/machine_wdt.c | 2 +- ports/mimxrt/mimxrt_flash.c | 2 +- ports/mimxrt/network_lan.c | 2 +- ports/nrf/boards/microbit/modules/microbitdisplay.c | 2 +- ports/nrf/boards/microbit/modules/microbitimage.c | 2 +- ports/nrf/modules/board/led.c | 2 +- ports/nrf/modules/machine/adc.c | 2 +- ports/nrf/modules/machine/i2c.c | 2 +- ports/nrf/modules/machine/pin.c | 4 ++-- ports/nrf/modules/machine/pwm.c | 2 +- ports/nrf/modules/machine/rtcounter.c | 2 +- ports/nrf/modules/machine/spi.c | 2 +- ports/nrf/modules/machine/temp.c | 2 +- ports/nrf/modules/machine/timer.c | 2 +- ports/nrf/modules/machine/uart.c | 2 +- ports/nrf/modules/nrf/flashbdev.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_characteristic.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_constants.c | 4 ++-- ports/nrf/modules/ubluepy/ubluepy_delegate.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_descriptor.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_peripheral.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_scan_entry.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_scanner.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_service.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_uuid.c | 2 +- ports/nrf/modules/uos/microbitfs.c | 4 ++-- ports/nrf/pin_named_pins.c | 4 ++-- ports/pic16bit/modpybled.c | 2 +- ports/pic16bit/modpybswitch.c | 2 +- ports/renesas-ra/machine_i2c.c | 2 +- ports/rp2/machine_adc.c | 2 +- ports/rp2/machine_i2c.c | 2 +- ports/rp2/machine_i2s.c | 2 +- ports/rp2/machine_pin.c | 2 +- ports/rp2/machine_rtc.c | 2 +- ports/rp2/machine_spi.c | 2 +- ports/rp2/machine_timer.c | 2 +- ports/rp2/machine_uart.c | 2 +- ports/rp2/machine_wdt.c | 2 +- ports/rp2/rp2_flash.c | 2 +- ports/rp2/rp2_pio.c | 4 ++-- ports/samd/machine_led.c | 2 +- ports/samd/machine_pin.c | 2 +- ports/samd/samd_flash.c | 2 +- ports/stm32/accel.c | 2 +- ports/stm32/adc.c | 4 ++-- ports/stm32/dac.c | 2 +- ports/stm32/extint.c | 2 +- ports/stm32/lcd.c | 2 +- ports/stm32/led.c | 2 +- ports/stm32/machine_adc.c | 2 +- ports/stm32/machine_i2c.c | 2 +- ports/stm32/machine_i2s.c | 2 +- ports/stm32/machine_spi.c | 2 +- ports/stm32/machine_timer.c | 2 +- ports/stm32/machine_uart.c | 2 +- ports/stm32/network_lan.c | 2 +- ports/stm32/pin.c | 4 ++-- ports/stm32/pin_named_pins.c | 4 ++-- ports/stm32/pyb_can.c | 2 +- ports/stm32/pyb_i2c.c | 2 +- ports/stm32/pyb_spi.c | 2 +- ports/stm32/rtc.c | 2 +- ports/stm32/sdcard.c | 4 ++-- ports/stm32/servo.c | 2 +- ports/stm32/storage.c | 2 +- ports/stm32/timer.c | 4 ++-- ports/stm32/usb.c | 4 ++-- ports/stm32/usrsw.c | 2 +- ports/stm32/wdt.c | 2 +- ports/teensy/led.c | 2 +- ports/teensy/timer.c | 4 ++-- ports/teensy/uart.c | 2 +- ports/unix/coverage.c | 4 ++-- ports/unix/modffi.c | 6 +++--- ports/unix/modjni.c | 6 +++--- ports/unix/moduselect.c | 2 +- ports/unix/modusocket.c | 2 +- ports/zephyr/machine_i2c.c | 2 +- ports/zephyr/machine_pin.c | 2 +- ports/zephyr/machine_spi.c | 2 +- ports/zephyr/machine_uart.c | 2 +- ports/zephyr/modusocket.c | 2 +- ports/zephyr/modzsensor.c | 2 +- ports/zephyr/zephyr_storage.c | 4 ++-- py/modio.c | 2 +- py/modthread.c | 2 +- py/objarray.c | 6 +++--- py/objdeque.c | 2 +- py/objdict.c | 4 ++-- py/objgenerator.c | 2 +- py/objint.c | 2 +- py/objlist.c | 2 +- py/objobject.c | 2 +- py/objpolyiter.c | 2 +- py/objproperty.c | 2 +- py/objset.c | 4 ++-- py/objslice.c | 2 +- py/objstr.c | 4 ++-- py/objstringio.c | 4 ++-- py/objstrunicode.c | 2 +- py/objtuple.c | 2 +- shared/runtime/mpirq.c | 2 +- shared/runtime/sys_stdio_mphal.c | 4 ++-- 185 files changed, 226 insertions(+), 226 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 7e597b791029b..378bf8fc914b7 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -737,7 +737,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_machine_soft_i2c_make_new, print, mp_machine_soft_i2c_print, protocol, &mp_machine_soft_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); #endif // MICROPY_PY_MACHINE_SOFTI2C diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index 818bc01c27112..f665ffaa494e7 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -179,7 +179,7 @@ MP_DEFINE_CONST_OBJ_TYPE( signal_make_new, call, signal_call, protocol, &signal_pin_p, - locals_dict, (void *)&signal_locals_dict + locals_dict, &signal_locals_dict ); #endif // MICROPY_PY_MACHINE diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index 43148c9c8d46d..c7fc5877b1b09 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -258,7 +258,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_machine_soft_spi_make_new, print, mp_machine_soft_spi_print, protocol, &mp_machine_soft_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); #endif // MICROPY_PY_MACHINE_SOFTSPI diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 2b7497e1b6551..2e058fc7deb25 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -981,7 +981,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_BLE, MP_TYPE_FLAG_NONE, bluetooth_ble_make_new, - locals_dict, (void *)&bluetooth_ble_locals_dict + locals_dict, &bluetooth_ble_locals_dict ); STATIC const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = { diff --git a/extmod/modbtree.c b/extmod/modbtree.c index f115be44fec15..15cb6341637a5 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -330,7 +330,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( iternext, btree_iternext, binary_op, btree_binary_op, subscr, btree_subscr, - locals_dict, (void *)&btree_locals_dict + locals_dict, &btree_locals_dict ); #endif diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 1d44312cf3040..f29eab272f0ea 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -835,7 +835,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, framebuf_make_new, buffer, framebuf_get_buffer, - locals_dict, (mp_obj_dict_t *)&framebuf_locals_dict + locals_dict, &framebuf_locals_dict ); #endif diff --git a/extmod/modlwip.c b/extmod/modlwip.c index f9d5b76b2c8bb..2f5da36f40845 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -182,7 +182,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_slip, MP_TYPE_FLAG_NONE, lwip_slip_make_new, - locals_dict, (mp_obj_dict_t *)&lwip_slip_locals_dict + locals_dict, &lwip_slip_locals_dict ); #endif // MICROPY_PY_LWIP_SLIP @@ -1602,7 +1602,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( lwip_socket_make_new, print, lwip_socket_print, protocol, &lwip_socket_stream_p, - locals_dict, (mp_obj_dict_t *)&lwip_socket_locals_dict + locals_dict, &lwip_socket_locals_dict ); /******************************************************************************/ diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index 500d13c5b8595..546764209608a 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -149,7 +149,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_TaskQueue, MP_TYPE_FLAG_NONE, task_queue_make_new, - locals_dict, (mp_obj_dict_t *)&task_queue_locals_dict + locals_dict, &task_queue_locals_dict ); /******************************************************************************/ diff --git a/extmod/moducryptolib.c b/extmod/moducryptolib.c index 236b7edfd7aa4..e4625c21a8786 100644 --- a/extmod/moducryptolib.c +++ b/extmod/moducryptolib.c @@ -353,7 +353,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_aes, MP_TYPE_FLAG_NONE, ucryptolib_aes_make_new, - locals_dict, (void *)&ucryptolib_aes_locals_dict + locals_dict, &ucryptolib_aes_locals_dict ); STATIC const mp_rom_map_elem_t mp_module_ucryptolib_globals_table[] = { diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 44625ed6da2ac..9535e00b406dd 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -162,7 +162,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_sha256, MP_TYPE_FLAG_NONE, uhashlib_sha256_make_new, - locals_dict, (void *)&uhashlib_sha256_locals_dict + locals_dict, &uhashlib_sha256_locals_dict ); #endif @@ -256,7 +256,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_sha1, MP_TYPE_FLAG_NONE, uhashlib_sha1_make_new, - locals_dict, (void *)&uhashlib_sha1_locals_dict + locals_dict, &uhashlib_sha1_locals_dict ); #endif @@ -350,7 +350,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_md5, MP_TYPE_FLAG_NONE, uhashlib_md5_make_new, - locals_dict, (void *)&uhashlib_md5_locals_dict + locals_dict, &uhashlib_md5_locals_dict ); #endif // MICROPY_PY_UHASHLIB_MD5 diff --git a/extmod/modure.c b/extmod/modure.c index a27c7ff9fdc57..43959924053bc 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -185,7 +185,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, match_print, - locals_dict, (void *)&match_locals_dict + locals_dict, &match_locals_dict ); #endif @@ -419,7 +419,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, re_print, - locals_dict, (void *)&re_locals_dict + locals_dict, &re_locals_dict ); #endif diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 1a11ee0eab50e..352b15d983d84 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -343,7 +343,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, getiter, mp_identity_getiter, iternext, poll_iternext, - locals_dict, (void *)&poll_locals_dict + locals_dict, &poll_locals_dict ); // poll() diff --git a/extmod/modusocket.c b/extmod/modusocket.c index 6008edb11c76f..fc16d7e270301 100644 --- a/extmod/modusocket.c +++ b/extmod/modusocket.c @@ -534,7 +534,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, socket_make_new, protocol, &socket_stream_p, - locals_dict, (mp_obj_dict_t *)&socket_locals_dict, + locals_dict, &socket_locals_dict, print, socket_print ); diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 72eb0e214fe3f..78470ea6df046 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -324,7 +324,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( getiter, NULL, iternext, NULL, protocol, &ussl_socket_stream_p, - locals_dict, (void *)&ussl_socket_locals_dict + locals_dict, &ussl_socket_locals_dict ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 0fab915f3f06f..76ca3ac719c91 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -402,7 +402,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( getiter, NULL, iternext, NULL, protocol, &ussl_socket_stream_p, - locals_dict, (void *)&ussl_socket_locals_dict + locals_dict, &ussl_socket_locals_dict ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index bf4e031895fa6..1a7575adc9e58 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -215,7 +215,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, utimeq_make_new, unary_op, utimeq_unary_op, - locals_dict, (void *)&utimeq_locals_dict + locals_dict, &utimeq_locals_dict ); STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = { diff --git a/extmod/moduwebsocket.c b/extmod/moduwebsocket.c index 2895978f3d5d2..c6be50d0e15a3 100644 --- a/extmod/moduwebsocket.c +++ b/extmod/moduwebsocket.c @@ -296,7 +296,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, websocket_make_new, protocol, &websocket_stream_p, - locals_dict, (void *)&websocket_locals_dict + locals_dict, &websocket_locals_dict ); STATIC const mp_rom_map_elem_t uwebsocket_module_globals_table[] = { diff --git a/extmod/moduzlib.c b/extmod/moduzlib.c index 93c939129eab8..533168d0b0543 100644 --- a/extmod/moduzlib.c +++ b/extmod/moduzlib.c @@ -146,7 +146,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, decompio_make_new, protocol, &decompio_stream_p, - locals_dict, (void *)&decompio_locals_dict + locals_dict, &decompio_locals_dict ); #endif diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index cb893b38dcbe6..fc5ca35ea0338 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -348,7 +348,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, webrepl_make_new, protocol, &webrepl_stream_p, - locals_dict, (mp_obj_dict_t *)&webrepl_locals_dict + locals_dict, &webrepl_locals_dict ); STATIC const mp_rom_map_elem_t webrepl_module_globals_table[] = { diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index fbd0a750b8e6c..329ba53ef51a6 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -502,7 +502,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, network_cyw43_make_new, print, network_cyw43_print, - locals_dict, (mp_obj_dict_t *)&network_cyw43_locals_dict + locals_dict, &network_cyw43_locals_dict ); #endif // MICROPY_PY_NETWORK_CYW43 diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 4a2ef883c26c1..7c18a51633746 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -437,7 +437,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, fat_vfs_make_new, protocol, &fat_vfs_proto, - locals_dict, (mp_obj_dict_t *)&fat_vfs_locals_dict + locals_dict, &fat_vfs_locals_dict ); #endif // MICROPY_VFS_FAT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 0d4af09b452cc..00980459db8b9 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -185,7 +185,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &vfs_fat_fileio_stream_p, - locals_dict, (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict + locals_dict, &vfs_fat_rawfile_locals_dict ); STATIC const mp_stream_p_t vfs_fat_textio_stream_p = { @@ -204,7 +204,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &vfs_fat_textio_stream_p, - locals_dict, (mp_obj_dict_t *)&vfs_fat_rawfile_locals_dict + locals_dict, &vfs_fat_rawfile_locals_dict ); // Factory function for I/O stream classes diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 3f8d262a4858f..33e2ef551987a 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -514,7 +514,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_VFS_LFSx(make_new), protocol, &MP_VFS_LFSx(proto), - locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(locals_dict) + locals_dict, &MP_VFS_LFSx(locals_dict) ); #undef VFS_LFSx_QSTR diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index ba90cc6084435..fda1b97b2a13c 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -229,7 +229,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &MP_VFS_LFSx(fileio_stream_p), - locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict) + locals_dict, &MP_VFS_LFSx(file_locals_dict) ); STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = { @@ -248,5 +248,5 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &MP_VFS_LFSx(textio_stream_p), - locals_dict, (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict) + locals_dict, &MP_VFS_LFSx(file_locals_dict) ); diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index 79126c007068f..b02827e8647c9 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -404,7 +404,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, vfs_posix_make_new, protocol, &vfs_posix_proto, - locals_dict, (mp_obj_dict_t *)&vfs_posix_locals_dict + locals_dict, &vfs_posix_locals_dict ); #endif // MICROPY_VFS_POSIX diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 85aef1617b80d..f0b5436fe1ea4 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -258,7 +258,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &vfs_posix_fileio_stream_p, - locals_dict, (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict + locals_dict, &vfs_posix_rawfile_locals_dict ); STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { @@ -277,7 +277,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &vfs_posix_textio_stream_p, - locals_dict, (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict + locals_dict, &vfs_posix_rawfile_locals_dict ); const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_vfs_posix_textio}, STDIN_FILENO}; diff --git a/ports/cc3200/misc/mpirq.c b/ports/cc3200/misc/mpirq.c index e9cae92a327e6..9c3c2f7196721 100644 --- a/ports/cc3200/misc/mpirq.c +++ b/ports/cc3200/misc/mpirq.c @@ -196,7 +196,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, call, mp_irq_call, - locals_dict, (mp_obj_t)&mp_irq_locals_dict + locals_dict, &mp_irq_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_irq_obj_list); diff --git a/ports/cc3200/mods/modnetwork.c b/ports/cc3200/mods/modnetwork.c index a74189300775b..0a72a1ab3256b 100644 --- a/ports/cc3200/mods/modnetwork.c +++ b/ports/cc3200/mods/modnetwork.c @@ -176,6 +176,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Server, MP_TYPE_FLAG_NONE, network_server_make_new, - locals_dict, (mp_obj_t)&network_server_locals_dict + locals_dict, &network_server_locals_dict ); #endif diff --git a/ports/cc3200/mods/moduhashlib.c b/ports/cc3200/mods/moduhashlib.c index 5437cfb264e6e..4a759d8ea5c56 100644 --- a/ports/cc3200/mods/moduhashlib.c +++ b/ports/cc3200/mods/moduhashlib.c @@ -182,7 +182,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_sha1, MP_TYPE_FLAG_NONE, hash_make_new, - locals_dict, (mp_obj_t)&hash_locals_dict + locals_dict, &hash_locals_dict ); STATIC MP_DEFINE_CONST_OBJ_TYPE( @@ -190,7 +190,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_sha256, MP_TYPE_FLAG_NONE, hash_make_new, - locals_dict, (mp_obj_t)&hash_locals_dict + locals_dict, &hash_locals_dict ); STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = { diff --git a/ports/cc3200/mods/modusocket.c b/ports/cc3200/mods/modusocket.c index 11199de14628f..55d504a709726 100644 --- a/ports/cc3200/mods/modusocket.c +++ b/ports/cc3200/mods/modusocket.c @@ -765,7 +765,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, socket_make_new, protocol, &socket_stream_p, - locals_dict, (mp_obj_t)&socket_locals_dict + locals_dict, &socket_locals_dict ); /******************************************************************************/ diff --git a/ports/cc3200/mods/modussl.c b/ports/cc3200/mods/modussl.c index d0909e7c2909d..abc9917c8134c 100644 --- a/ports/cc3200/mods/modussl.c +++ b/ports/cc3200/mods/modussl.c @@ -68,7 +68,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( getiter, NULL, iternext, NULL, protocol, &socket_stream_p, - locals_dict, (mp_obj_t)&socket_locals_dict + locals_dict, &socket_locals_dict ); STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { diff --git a/ports/cc3200/mods/modwlan.c b/ports/cc3200/mods/modwlan.c index 24f5d196d2a30..1c99f075e9020 100644 --- a/ports/cc3200/mods/modwlan.c +++ b/ports/cc3200/mods/modwlan.c @@ -1290,7 +1290,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, wlan_make_new, - locals_dict, (mp_obj_t)&wlan_locals_dict + locals_dict, &wlan_locals_dict ); const mod_network_nic_type_t mod_network_nic_type_wlan = { diff --git a/ports/cc3200/mods/pybadc.c b/ports/cc3200/mods/pybadc.c index a114eeda169a0..a14f9aced2618 100644 --- a/ports/cc3200/mods/pybadc.c +++ b/ports/cc3200/mods/pybadc.c @@ -239,7 +239,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, adc_make_new, print, adc_print, - locals_dict, (mp_obj_t)&adc_locals_dict + locals_dict, &adc_locals_dict ); STATIC void adc_channel_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -308,5 +308,5 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, print, adc_channel_print, call, adc_channel_call, - locals_dict, (mp_obj_t)&adc_channel_locals_dict + locals_dict, &adc_channel_locals_dict ); diff --git a/ports/cc3200/mods/pybflash.c b/ports/cc3200/mods/pybflash.c index 4cfafb7abf642..a6d1e23fbf021 100644 --- a/ports/cc3200/mods/pybflash.c +++ b/ports/cc3200/mods/pybflash.c @@ -89,7 +89,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Flash, MP_TYPE_FLAG_NONE, pyb_flash_make_new, - locals_dict, (mp_obj_t)&pyb_flash_locals_dict + locals_dict, &pyb_flash_locals_dict ); void pyb_flash_init_vfs(fs_user_mount_t *vfs) { diff --git a/ports/cc3200/mods/pybi2c.c b/ports/cc3200/mods/pybi2c.c index 91a0b9b9f9a99..de92cc7c32b30 100644 --- a/ports/cc3200/mods/pybi2c.c +++ b/ports/cc3200/mods/pybi2c.c @@ -527,5 +527,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_i2c_make_new, print, pyb_i2c_print, - locals_dict, (mp_obj_t)&pyb_i2c_locals_dict + locals_dict, &pyb_i2c_locals_dict ); diff --git a/ports/cc3200/mods/pybpin.c b/ports/cc3200/mods/pybpin.c index 948cda70d4ab9..374d09ddfbd77 100644 --- a/ports/cc3200/mods/pybpin.c +++ b/ports/cc3200/mods/pybpin.c @@ -938,7 +938,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_make_new, print, pin_print, call, pin_call, - locals_dict, (mp_obj_t)&pin_locals_dict + locals_dict, &pin_locals_dict ); STATIC const mp_irq_methods_t pin_irq_methods = { @@ -959,6 +959,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, - locals_dict, (mp_obj_t)&pin_board_pins_locals_dict + locals_dict, &pin_board_pins_locals_dict ); diff --git a/ports/cc3200/mods/pybrtc.c b/ports/cc3200/mods/pybrtc.c index 2761cb3c64f5d..ed7a20fecbf6c 100644 --- a/ports/cc3200/mods/pybrtc.c +++ b/ports/cc3200/mods/pybrtc.c @@ -474,7 +474,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, pyb_rtc_make_new, - locals_dict, (mp_obj_t)&pyb_rtc_locals_dict + locals_dict, &pyb_rtc_locals_dict ); STATIC const mp_irq_methods_t pyb_rtc_irq_methods = { diff --git a/ports/cc3200/mods/pybsd.c b/ports/cc3200/mods/pybsd.c index d8834e36f7447..968a6a87ec4d7 100644 --- a/ports/cc3200/mods/pybsd.c +++ b/ports/cc3200/mods/pybsd.c @@ -217,5 +217,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_SD, MP_TYPE_FLAG_NONE, pyb_sd_make_new, - locals_dict, (mp_obj_t)&pyb_sd_locals_dict + locals_dict, &pyb_sd_locals_dict ); diff --git a/ports/cc3200/mods/pybspi.c b/ports/cc3200/mods/pybspi.c index 50d897633e557..7d83fabfde4da 100644 --- a/ports/cc3200/mods/pybspi.c +++ b/ports/cc3200/mods/pybspi.c @@ -383,5 +383,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_spi_make_new, print, pyb_spi_print, - locals_dict, (mp_obj_t)&pyb_spi_locals_dict + locals_dict, &pyb_spi_locals_dict ); diff --git a/ports/cc3200/mods/pybtimer.c b/ports/cc3200/mods/pybtimer.c index a8bc7821e0ce2..14e1deb083ed2 100644 --- a/ports/cc3200/mods/pybtimer.c +++ b/ports/cc3200/mods/pybtimer.c @@ -465,7 +465,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_timer_make_new, print, pyb_timer_print, - locals_dict, (mp_obj_t)&pyb_timer_locals_dict + locals_dict, &pyb_timer_locals_dict ); STATIC const mp_irq_methods_t pyb_timer_channel_irq_methods = { @@ -728,7 +728,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, - locals_dict, (mp_obj_t)&pyb_timer_channel_locals_dict + locals_dict, &pyb_timer_channel_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_list_t pyb_timer_channel_obj_list); diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index 059101f4f205a..e7896c4ca5cfa 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -694,7 +694,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_t)&pyb_uart_locals_dict + locals_dict, &pyb_uart_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _pyb_uart_obj_t *pyb_uart_objs[2]); diff --git a/ports/cc3200/mods/pybwdt.c b/ports/cc3200/mods/pybwdt.c index cde1abe59d896..3c1f9eb195f10 100644 --- a/ports/cc3200/mods/pybwdt.c +++ b/ports/cc3200/mods/pybwdt.c @@ -155,6 +155,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, pyb_wdt_make_new, - locals_dict, (mp_obj_t)&pybwdt_locals_dict + locals_dict, &pybwdt_locals_dict ); diff --git a/ports/esp32/esp32_nvs.c b/ports/esp32/esp32_nvs.c index 1f96ad129d11a..d935a13d6fe5d 100644 --- a/ports/esp32/esp32_nvs.c +++ b/ports/esp32/esp32_nvs.c @@ -147,5 +147,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, esp32_nvs_make_new, print, esp32_nvs_print, - locals_dict, (mp_obj_dict_t *)&esp32_nvs_locals_dict + locals_dict, &esp32_nvs_locals_dict ); diff --git a/ports/esp32/esp32_partition.c b/ports/esp32/esp32_partition.c index 2e42e7a81903e..6ce1e90b4c700 100644 --- a/ports/esp32/esp32_partition.c +++ b/ports/esp32/esp32_partition.c @@ -290,5 +290,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, esp32_partition_make_new, print, esp32_partition_print, - locals_dict, (mp_obj_dict_t *)&esp32_partition_locals_dict + locals_dict, &esp32_partition_locals_dict ); diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 36f33df3f5d44..ee09ac5200611 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -378,5 +378,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, esp32_rmt_make_new, print, esp32_rmt_print, - locals_dict, (mp_obj_dict_t *)&esp32_rmt_locals_dict + locals_dict, &esp32_rmt_locals_dict ); diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 5eb0e5591e7e0..5030f980675ed 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -96,7 +96,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_ULP, MP_TYPE_FLAG_NONE, esp32_ulp_make_new, - locals_dict, (mp_obj_t)&esp32_ulp_locals_dict + locals_dict, &esp32_ulp_locals_dict ); #endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 5cc2d80384915..c4e04159c0b90 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -262,5 +262,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, madc_make_new, print, madc_print, - locals_dict, (mp_obj_t)&madc_locals_dict + locals_dict, &madc_locals_dict ); diff --git a/ports/esp32/machine_adcblock.c b/ports/esp32/machine_adcblock.c index 770839e93e1b1..ae3244f7fdb4a 100644 --- a/ports/esp32/machine_adcblock.c +++ b/ports/esp32/machine_adcblock.c @@ -200,5 +200,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, madcblock_make_new, print, madcblock_print, - locals_dict, (mp_obj_dict_t *)&madcblock_locals_dict + locals_dict, &madcblock_locals_dict ); diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index c9b9c14e01e77..fbe33b0344a47 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -110,7 +110,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, mdac_make_new, print, mdac_print, - locals_dict, (mp_obj_t)&mdac_locals_dict + locals_dict, &mdac_locals_dict ); #endif // MICROPY_PY_MACHINE_DAC diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 71583ef60e900..51ea31ac1b3db 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -552,5 +552,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hw_spi_make_new, print, machine_hw_spi_print, protocol, &machine_hw_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 9e5be606dbf99..895dc3a39874d 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -199,5 +199,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hw_i2c_make_new, print, machine_hw_i2c_print, protocol, &machine_hw_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index d30fb5d836e7b..b853f418ada3c 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -838,7 +838,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + locals_dict, &machine_i2s_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]); diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index 1a1a3a0f8d170..fd523a38edacc 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -537,7 +537,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, - locals_dict, (mp_obj_t)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); /******************************************************************************/ @@ -730,7 +730,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, call, machine_pin_irq_call, - locals_dict, (mp_obj_dict_t *)&machine_pin_irq_locals_dict + locals_dict, &machine_pin_irq_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_t machine_pin_irq_handler[40]); diff --git a/ports/esp32/machine_rtc.c b/ports/esp32/machine_rtc.c index 6634bf5b03b9b..19b83703fd43f 100644 --- a/ports/esp32/machine_rtc.c +++ b/ports/esp32/machine_rtc.c @@ -178,5 +178,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, machine_rtc_make_new, - locals_dict, (mp_obj_t)&machine_rtc_locals_dict + locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 2d5663d476e8f..0b6159157d980 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -404,7 +404,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, machine_sdcard_make_new, - locals_dict, (mp_obj_dict_t *)&machine_sdcard_locals_dict + locals_dict, &machine_sdcard_locals_dict ); #endif // MICROPY_HW_ENABLE_SDCARD diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 3b1295095593e..2fd40fa2afc12 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -283,7 +283,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_timer_make_new, print, machine_timer_print, - locals_dict, (mp_obj_t)&machine_timer_locals_dict + locals_dict, &machine_timer_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *machine_timer_obj_head); diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index c5e3483b74a08..deba818dd1c87 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -139,7 +139,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_TouchPad, MP_TYPE_FLAG_NONE, mtp_make_new, - locals_dict, (mp_obj_t)&mtp_locals_dict + locals_dict, &mtp_locals_dict ); #endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 9df16ae419837..6e091b8838a23 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -539,5 +539,5 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + locals_dict, &machine_uart_locals_dict ); diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index c2898c7fe1586..bda9c6975e152 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -88,5 +88,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, machine_wdt_make_new, - locals_dict, (mp_obj_t)&machine_wdt_locals_dict + locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index a2dcb3946ceaa..e7e6f3c26e287 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -791,7 +791,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, socket_make_new, protocol, &socket_stream_p, - locals_dict, (mp_obj_t)&socket_locals_dict + locals_dict, &socket_locals_dict ); STATIC mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index fc50e13c48edc..3c5aea5fb8a4d 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -307,7 +307,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_LAN, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&lan_if_locals_dict + locals_dict, &lan_if_locals_dict ); #endif diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index d6368d9f2038b..df07515c7d411 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -283,5 +283,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_PPP, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&ppp_if_locals_dict + locals_dict, &ppp_if_locals_dict ); diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 6ca5f9a9bafea..0f1f5de14937a 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -620,7 +620,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&wlan_if_locals_dict + locals_dict, &wlan_if_locals_dict ); STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA}; diff --git a/ports/esp8266/machine_adc.c b/ports/esp8266/machine_adc.c index bface7f7e155c..b1e7b38435276 100644 --- a/ports/esp8266/machine_adc.c +++ b/ports/esp8266/machine_adc.c @@ -95,5 +95,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_adc_make_new, print, machine_adc_print, - locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + locals_dict, &machine_adc_locals_dict ); diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index c0d4a677e3ef5..3f449a1de91e1 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -182,7 +182,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hspi_make_new, print, machine_hspi_print, protocol, &machine_hspi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); #endif // MICROPY_PY_MACHINE_SPI diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c index 8b759766cd6f7..32ffca873dfaf 100644 --- a/ports/esp8266/machine_pin.c +++ b/ports/esp8266/machine_pin.c @@ -458,7 +458,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, pyb_pin_print, call, pyb_pin_call, protocol, &pin_pin_p, - locals_dict, (mp_obj_dict_t *)&pyb_pin_locals_dict + locals_dict, &pyb_pin_locals_dict ); /******************************************************************************/ @@ -516,7 +516,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, call, pin_irq_call, - locals_dict, (mp_obj_dict_t *)&pin_irq_locals_dict + locals_dict, &pin_irq_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_irq_handler[16]); diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index 4235b325ef7a8..cc6b79a031be7 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -267,5 +267,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, pyb_rtc_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_rtc_locals_dict + locals_dict, &pyb_rtc_locals_dict ); diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c index 82f5189388caf..c737f854c34f3 100644 --- a/ports/esp8266/machine_uart.c +++ b/ports/esp8266/machine_uart.c @@ -352,7 +352,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&pyb_uart_locals_dict + locals_dict, &pyb_uart_locals_dict ); MP_REGISTER_ROOT_POINTER(byte * uart0_rxbuf); diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index d8c32ddd1a1f3..26d5c9fa76f13 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -74,5 +74,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, machine_wdt_make_new, - locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index 2bb2c7bd7695f..eb41e30f66b12 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -343,7 +343,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, esp_timer_make_new, print, esp_timer_print, - locals_dict, (mp_obj_dict_t *)&esp_timer_locals_dict + locals_dict, &esp_timer_locals_dict ); // this bit is unused in the Xtensa PS register diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index f78bf5da52eb1..45a5a2be5412a 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -515,7 +515,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&wlan_if_locals_dict + locals_dict, &wlan_if_locals_dict ); STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) { diff --git a/ports/mimxrt/machine_adc.c b/ports/mimxrt/machine_adc.c index cbac6b5734d21..7a19d1225ee73 100644 --- a/ports/mimxrt/machine_adc.c +++ b/ports/mimxrt/machine_adc.c @@ -123,7 +123,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, adc_obj_make_new, print, adc_obj_print, - locals_dict, (mp_obj_dict_t *)&adc_locals_dict + locals_dict, &adc_locals_dict ); void machine_adc_init(void) { diff --git a/ports/mimxrt/machine_i2c.c b/ports/mimxrt/machine_i2c.c index b8b6b7bc63340..62dfd8204c081 100644 --- a/ports/mimxrt/machine_i2c.c +++ b/ports/mimxrt/machine_i2c.c @@ -204,5 +204,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2c_make_new, print, machine_i2c_print, protocol, &machine_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); diff --git a/ports/mimxrt/machine_i2s.c b/ports/mimxrt/machine_i2s.c index 68bf3a820f59f..13380b4ee6b09 100644 --- a/ports/mimxrt/machine_i2s.c +++ b/ports/mimxrt/machine_i2s.c @@ -1222,7 +1222,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + locals_dict, &machine_i2s_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_I2S_NUM]); diff --git a/ports/mimxrt/machine_led.c b/ports/mimxrt/machine_led.c index d766c8f3237a2..9fd98ef71043f 100644 --- a/ports/mimxrt/machine_led.c +++ b/ports/mimxrt/machine_led.c @@ -86,7 +86,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, led_obj_make_new, print, led_obj_print, - locals_dict, (mp_obj_dict_t *)&led_locals_dict + locals_dict, &led_locals_dict ); #endif diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c index 7ec66d0eac95b..261e3e4148034 100644 --- a/ports/mimxrt/machine_pin.c +++ b/ports/mimxrt/machine_pin.c @@ -63,7 +63,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_cpu, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_t)&machine_pin_cpu_pins_locals_dict + locals_dict, &machine_pin_cpu_pins_locals_dict ); MP_DEFINE_CONST_OBJ_TYPE( @@ -71,7 +71,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_board, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_t)&machine_pin_board_pins_locals_dict + locals_dict, &machine_pin_board_pins_locals_dict ); STATIC const mp_irq_methods_t machine_pin_irq_methods; @@ -428,7 +428,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, machine_pin_obj_print, call, machine_pin_obj_call, protocol, &machine_pin_obj_protocol, - locals_dict, (mp_obj_dict_t *)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); // FIXME: Create actual pin_af type!!! @@ -438,7 +438,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, mp_pin_make_new, print, machine_pin_obj_print, - locals_dict, (mp_obj_dict_t *)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { diff --git a/ports/mimxrt/machine_rtc.c b/ports/mimxrt/machine_rtc.c index 5211027bdf807..2e1a09dedbe6f 100644 --- a/ports/mimxrt/machine_rtc.c +++ b/ports/mimxrt/machine_rtc.c @@ -171,5 +171,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, machine_rtc_make_new, - locals_dict, (mp_obj_t)&machine_rtc_locals_dict + locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/mimxrt/machine_sdcard.c b/ports/mimxrt/machine_sdcard.c index b7bdceef47edb..22f7e7c232a8e 100644 --- a/ports/mimxrt/machine_sdcard.c +++ b/ports/mimxrt/machine_sdcard.c @@ -213,7 +213,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, sdcard_obj_make_new, - locals_dict, (mp_obj_dict_t *)&sdcard_locals_dict + locals_dict, &sdcard_locals_dict ); void machine_sdcard_init0(void) { diff --git a/ports/mimxrt/machine_spi.c b/ports/mimxrt/machine_spi.c index ff3cf4fb2551d..93b75e931eb48 100644 --- a/ports/mimxrt/machine_spi.c +++ b/ports/mimxrt/machine_spi.c @@ -258,5 +258,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_spi_make_new, print, machine_spi_print, protocol, &machine_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); diff --git a/ports/mimxrt/machine_timer.c b/ports/mimxrt/machine_timer.c index 9612388486e31..a6b61982f7ec0 100644 --- a/ports/mimxrt/machine_timer.c +++ b/ports/mimxrt/machine_timer.c @@ -217,7 +217,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_timer_make_new, print, machine_timer_print, - locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + locals_dict, &machine_timer_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS]); diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 4bb518eab25e4..9d4873274a8fe 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -478,5 +478,5 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + locals_dict, &machine_uart_locals_dict ); diff --git a/ports/mimxrt/machine_wdt.c b/ports/mimxrt/machine_wdt.c index cde80f085b997..e0b5c74b54268 100644 --- a/ports/mimxrt/machine_wdt.c +++ b/ports/mimxrt/machine_wdt.c @@ -104,5 +104,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, machine_wdt_make_new, - locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/mimxrt/mimxrt_flash.c b/ports/mimxrt/mimxrt_flash.c index 1a7d6cca8da0e..bd03c853d597d 100644 --- a/ports/mimxrt/mimxrt_flash.c +++ b/ports/mimxrt/mimxrt_flash.c @@ -220,5 +220,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Flash, MP_TYPE_FLAG_NONE, mimxrt_flash_make_new, - locals_dict, (mp_obj_dict_t *)&mimxrt_flash_locals_dict + locals_dict, &mimxrt_flash_locals_dict ); diff --git a/ports/mimxrt/network_lan.c b/ports/mimxrt/network_lan.c index 08c3c9e729f50..e15894294b68c 100644 --- a/ports/mimxrt/network_lan.c +++ b/ports/mimxrt/network_lan.c @@ -226,7 +226,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, network_lan_make_new, print, network_lan_print, - locals_dict, (mp_obj_dict_t *)&network_lan_locals_dict + locals_dict, &network_lan_locals_dict ); diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index 084cb09524da7..5cb25ea1ced48 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -547,7 +547,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_MicroBitDisplay, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t*)µbit_display_locals_dict + locals_dict, µbit_display_locals_dict ); microbit_display_obj_t microbit_display_obj = { diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index 95b17bb6d3396..4870b6738f6c0 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -685,7 +685,7 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_image_make_new, print, microbit_image_print, binary_op, image_binary_op, - locals_dict, (mp_obj_dict_t*)µbit_image_locals_dict + locals_dict, µbit_image_locals_dict ); typedef struct _scrolling_string_t { diff --git a/ports/nrf/modules/board/led.c b/ports/nrf/modules/board/led.c index 5eef8f0464021..577c2b62843f4 100644 --- a/ports/nrf/modules/board/led.c +++ b/ports/nrf/modules/board/led.c @@ -200,7 +200,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, led_obj_make_new, print, led_obj_print, - locals_dict, (mp_obj_dict_t*)&led_locals_dict + locals_dict, &led_locals_dict ); #else diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index 5814dcaa392cf..84db8d259f2ac 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -299,7 +299,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_ADC, MP_TYPE_FLAG_NONE, machine_adc_make_new, - locals_dict, (mp_obj_dict_t*)&machine_adc_locals_dict, + locals_dict, &machine_adc_locals_dict, print, machine_adc_print ); diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index 8468684428a7c..7cb55d078821c 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -168,7 +168,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, - locals_dict, (mp_obj_dict_t*)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); #endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/nrf/modules/machine/pin.c b/ports/nrf/modules/machine/pin.c index 835f6cf2bd32c..4f283e5dba149 100644 --- a/ports/nrf/modules/machine/pin.c +++ b/ports/nrf/modules/machine/pin.c @@ -603,7 +603,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_make_new, print, pin_print, call, pin_call, - locals_dict, (mp_obj_dict_t*)&pin_locals_dict + locals_dict, &pin_locals_dict ); /// \moduleref machine @@ -678,7 +678,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pin_af_obj_print, - locals_dict, (mp_obj_dict_t*)&pin_af_locals_dict + locals_dict, &pin_af_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_mapper); diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index d0ac0e9450c3c..54e643ec55ffd 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -345,7 +345,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_pwm_make_new, print, machine_pwm_print, - locals_dict, (mp_obj_dict_t*)&machine_pwm_locals_dict + locals_dict, &machine_pwm_locals_dict ); #endif // MICROPY_PY_MACHINE_HW_PWM diff --git a/ports/nrf/modules/machine/rtcounter.c b/ports/nrf/modules/machine/rtcounter.c index 3c48c4bb1fb2e..cafe08b6c6843 100644 --- a/ports/nrf/modules/machine/rtcounter.c +++ b/ports/nrf/modules/machine/rtcounter.c @@ -268,7 +268,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_rtc_make_new, print, rtc_print, - locals_dict, (mp_obj_dict_t*)&machine_rtc_locals_dict + locals_dict, &machine_rtc_locals_dict ); #endif // MICROPY_PY_MACHINE_RTCOUNTER diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c index 22b0ff56e59ae..d5613a4643246 100644 --- a/ports/nrf/modules/machine/spi.c +++ b/ports/nrf/modules/machine/spi.c @@ -434,7 +434,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, - locals_dict, (mp_obj_dict_t*)&machine_spi_locals_dict + locals_dict, &machine_spi_locals_dict ); #endif // MICROPY_PY_MACHINE_HW_SPI diff --git a/ports/nrf/modules/machine/temp.c b/ports/nrf/modules/machine/temp.c index 1e21f11253fd2..00d6329b6498c 100644 --- a/ports/nrf/modules/machine/temp.c +++ b/ports/nrf/modules/machine/temp.c @@ -117,7 +117,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Temp, MP_TYPE_FLAG_NONE, machine_temp_make_new, - locals_dict, (mp_obj_dict_t*)&machine_temp_locals_dict, + locals_dict, &machine_temp_locals_dict, print, machine_temp_print ); diff --git a/ports/nrf/modules/machine/timer.c b/ports/nrf/modules/machine/timer.c index 3724881aa8131..f7f6101726a6e 100644 --- a/ports/nrf/modules/machine/timer.c +++ b/ports/nrf/modules/machine/timer.c @@ -240,7 +240,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_timer_make_new, print, timer_print, - locals_dict, (mp_obj_dict_t*)&machine_timer_locals_dict + locals_dict, &machine_timer_locals_dict ); #endif // MICROPY_PY_MACHINE_TIMER diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 5c9ba83ab6626..fc0bd682b4b7d 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -379,7 +379,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t*)&machine_hard_uart_locals_dict + locals_dict, &machine_hard_uart_locals_dict ); #endif // MICROPY_PY_MACHINE_UART diff --git a/ports/nrf/modules/nrf/flashbdev.c b/ports/nrf/modules/nrf/flashbdev.c index b67e86d0d0dfd..84a3dd2520b44 100644 --- a/ports/nrf/modules/nrf/flashbdev.c +++ b/ports/nrf/modules/nrf/flashbdev.c @@ -189,7 +189,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, nrf_flashbdev_make_new, print, nrf_flashbdev_print, - locals_dict, (mp_obj_dict_t *)&nrf_flashbdev_locals_dict + locals_dict, &nrf_flashbdev_locals_dict ); void flashbdev_init(void) { diff --git a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c index 5544ac6aefedc..2be7dab9d3f7c 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c +++ b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c @@ -215,7 +215,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_characteristic_make_new, print, ubluepy_characteristic_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict + locals_dict, &ubluepy_characteristic_locals_dict ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_constants.c b/ports/nrf/modules/ubluepy/ubluepy_constants.c index e4637c8cbce2e..c6c399924573d 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_constants.c +++ b/ports/nrf/modules/ubluepy/ubluepy_constants.c @@ -74,7 +74,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_ad_types, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t*)&ubluepy_constants_ad_types_locals_dict + locals_dict, &ubluepy_constants_ad_types_locals_dict ); STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = { @@ -97,7 +97,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_constants, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t*)&ubluepy_constants_locals_dict + locals_dict, &ubluepy_constants_locals_dict ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_delegate.c b/ports/nrf/modules/ubluepy/ubluepy_delegate.c index 71648767e61d7..dd19f70be445b 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_delegate.c +++ b/ports/nrf/modules/ubluepy/ubluepy_delegate.c @@ -83,7 +83,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_delegate_make_new, print, ubluepy_delegate_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_delegate_locals_dict + locals_dict, &ubluepy_delegate_locals_dict ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c index 07035460ae4d9..d942d98223067 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c +++ b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c @@ -76,7 +76,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_descriptor_make_new, print, ubluepy_descriptor_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_descriptor_locals_dict + locals_dict, &ubluepy_descriptor_locals_dict ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c index 9c346a885b2cb..9f638b45d7359 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c +++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c @@ -488,7 +488,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_peripheral_make_new, print, ubluepy_peripheral_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_peripheral_locals_dict + locals_dict, &ubluepy_peripheral_locals_dict ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c index 64a81d215d379..5c74e2e92caf4 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c @@ -142,7 +142,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, ubluepy_scan_entry_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_scan_entry_locals_dict + locals_dict, &ubluepy_scan_entry_locals_dict ); #endif // MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_scanner.c b/ports/nrf/modules/ubluepy/ubluepy_scanner.c index c47044cf0c37a..b56ec349d0430 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scanner.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scanner.c @@ -120,7 +120,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_scanner_make_new, print, ubluepy_scanner_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_scanner_locals_dict + locals_dict, &ubluepy_scanner_locals_dict ); #endif // MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_service.c b/ports/nrf/modules/ubluepy/ubluepy_service.c index 9d0d6e5b95f8e..2bec6befd6894 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_service.c +++ b/ports/nrf/modules/ubluepy/ubluepy_service.c @@ -177,7 +177,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_service_make_new, print, ubluepy_service_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_service_locals_dict + locals_dict, &ubluepy_service_locals_dict ); #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL diff --git a/ports/nrf/modules/ubluepy/ubluepy_uuid.c b/ports/nrf/modules/ubluepy/ubluepy_uuid.c index 0414a2a2867f5..0fd6e75e5b628 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_uuid.c +++ b/ports/nrf/modules/ubluepy/ubluepy_uuid.c @@ -166,7 +166,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ubluepy_uuid_make_new, print, ubluepy_uuid_print, - locals_dict, (mp_obj_dict_t*)&ubluepy_uuid_locals_dict + locals_dict, &ubluepy_uuid_locals_dict ); #endif // MICROPY_PY_UBLUEPY diff --git a/ports/nrf/modules/uos/microbitfs.c b/ports/nrf/modules/uos/microbitfs.c index 63ac8c93213f6..d1b320111607b 100644 --- a/ports/nrf/modules/uos/microbitfs.c +++ b/ports/nrf/modules/uos/microbitfs.c @@ -632,7 +632,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, protocol, &textio_stream_p, - locals_dict, (mp_obj_dict_t*)&uos_mbfs_file_locals_dict + locals_dict, &uos_mbfs_file_locals_dict ); @@ -647,7 +647,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, protocol, &fileio_stream_p, - locals_dict, (mp_obj_dict_t*)&uos_mbfs_file_locals_dict + locals_dict, &uos_mbfs_file_locals_dict ); // From micro:bit fileobj.c diff --git a/ports/nrf/pin_named_pins.c b/ports/nrf/pin_named_pins.c index 87fed746e64ee..84ec89f44aa69 100644 --- a/ports/nrf/pin_named_pins.c +++ b/ports/nrf/pin_named_pins.c @@ -42,7 +42,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, - locals_dict, (mp_obj_t)&pin_cpu_pins_locals_dict + locals_dict, &pin_cpu_pins_locals_dict ); MP_DEFINE_CONST_OBJ_TYPE( @@ -51,7 +51,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, - locals_dict, (mp_obj_t)&pin_board_pins_locals_dict + locals_dict, &pin_board_pins_locals_dict ); const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { diff --git a/ports/pic16bit/modpybled.c b/ports/pic16bit/modpybled.c index fd4e8cce9c922..2e5c2dcca3b1b 100644 --- a/ports/pic16bit/modpybled.c +++ b/ports/pic16bit/modpybled.c @@ -90,5 +90,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_led_make_new, print, pyb_led_print, - locals_dict, (mp_obj_t)&pyb_led_locals_dict + locals_dict, &pyb_led_locals_dict ); diff --git a/ports/pic16bit/modpybswitch.c b/ports/pic16bit/modpybswitch.c index e1096b1daaadd..f27cfb9b093af 100644 --- a/ports/pic16bit/modpybswitch.c +++ b/ports/pic16bit/modpybswitch.c @@ -78,5 +78,5 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_switch_make_new, print, pyb_switch_print, call, pyb_switch_call, - locals_dict, (mp_obj_t)&pyb_switch_locals_dict + locals_dict, &pyb_switch_locals_dict ); diff --git a/ports/renesas-ra/machine_i2c.c b/ports/renesas-ra/machine_i2c.c index eaca5ff4a85ea..563ea8787e523 100644 --- a/ports/renesas-ra/machine_i2c.c +++ b/ports/renesas-ra/machine_i2c.c @@ -161,7 +161,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_I2C, MP_TYPE_FLAG_NONE, machine_i2c_make_new, - locals_dict, (void *)&mp_machine_i2c_locals_dict, + locals_dict, &mp_machine_i2c_locals_dict, print, machine_i2c_print, protocol, &machine_i2c_p ); diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index 549f8d5ecdc5a..85562d5c07583 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -119,5 +119,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_adc_make_new, print, machine_adc_print, - locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + locals_dict, &machine_adc_locals_dict ); diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index 91d8bb59b76c1..5ab93f63581d6 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -183,5 +183,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2c_make_new, print, machine_i2c_print, protocol, &machine_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); diff --git a/ports/rp2/machine_i2s.c b/ports/rp2/machine_i2s.c index 8446a59781bb9..9d70a476f30b1 100644 --- a/ports/rp2/machine_i2s.c +++ b/ports/rp2/machine_i2s.c @@ -1146,7 +1146,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + locals_dict, &machine_i2s_locals_dict ); MP_REGISTER_ROOT_POINTER(void *machine_i2s_obj[2]); diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c index 38670f09abead..8ccfbca3ab426 100644 --- a/ports/rp2/machine_pin.c +++ b/ports/rp2/machine_pin.c @@ -649,7 +649,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, - locals_dict, (mp_obj_t)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { diff --git a/ports/rp2/machine_rtc.c b/ports/rp2/machine_rtc.c index 73bdaee6c7606..6b2c9ac71b65d 100644 --- a/ports/rp2/machine_rtc.c +++ b/ports/rp2/machine_rtc.c @@ -120,5 +120,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, machine_rtc_make_new, - locals_dict, (mp_obj_t)&machine_rtc_locals_dict + locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c index f3ac8d7cf811f..08c79712cf226 100644 --- a/ports/rp2/machine_spi.c +++ b/ports/rp2/machine_spi.c @@ -297,7 +297,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_spi_make_new, print, machine_spi_print, protocol, &machine_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) { diff --git a/ports/rp2/machine_timer.c b/ports/rp2/machine_timer.c index d3b60155294f4..66f632329c723 100644 --- a/ports/rp2/machine_timer.c +++ b/ports/rp2/machine_timer.c @@ -162,5 +162,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_timer_make_new, print, machine_timer_print, - locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + locals_dict, &machine_timer_locals_dict ); diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index df6228058be7b..bb8bf51be6511 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -588,7 +588,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + locals_dict, &machine_uart_locals_dict ); MP_REGISTER_ROOT_POINTER(void *rp2_uart_rx_buffer[2]); diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c index e8c433306922b..c224298d136be 100644 --- a/ports/rp2/machine_wdt.c +++ b/ports/rp2/machine_wdt.c @@ -82,5 +82,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, machine_wdt_make_new, - locals_dict, (mp_obj_dict_t *)&machine_wdt_locals_dict + locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index 37a3412db5cfe..df49c881c4a41 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -190,5 +190,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Flash, MP_TYPE_FLAG_NONE, rp2_flash_make_new, - locals_dict, (mp_obj_dict_t *)&rp2_flash_locals_dict + locals_dict, &rp2_flash_locals_dict ); diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 9a195bdd8a64c..cfba9cce5ae01 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -382,7 +382,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, rp2_pio_make_new, print, rp2_pio_print, - locals_dict, (mp_obj_dict_t *)&rp2_pio_locals_dict + locals_dict, &rp2_pio_locals_dict ); STATIC mp_uint_t rp2_pio_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { @@ -813,7 +813,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, rp2_state_machine_make_new, print, rp2_state_machine_print, - locals_dict, (mp_obj_dict_t *)&rp2_state_machine_locals_dict + locals_dict, &rp2_state_machine_locals_dict ); STATIC mp_uint_t rp2_state_machine_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 76aae8ffc5767..7a9b2299ae3b9 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -169,5 +169,5 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_led_make_new, print, machine_led_print, call, machine_led_call, - locals_dict, (mp_obj_t)&machine_led_locals_dict + locals_dict, &machine_led_locals_dict ); diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index d47982e0e1c6b..75e1a2356c4d2 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -295,7 +295,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, - locals_dict, (mp_obj_t)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); /* diff --git a/ports/samd/samd_flash.c b/ports/samd/samd_flash.c index 6c74b59a263ad..9d64768cfa020 100644 --- a/ports/samd/samd_flash.c +++ b/ports/samd/samd_flash.c @@ -186,5 +186,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Flash, MP_TYPE_FLAG_NONE, samd_flash_make_new, - locals_dict, (mp_obj_dict_t *)&samd_flash_locals_dict + locals_dict, &samd_flash_locals_dict ); diff --git a/ports/stm32/accel.c b/ports/stm32/accel.c index 276ce37d3ad49..05fd1898bc11d 100644 --- a/ports/stm32/accel.c +++ b/ports/stm32/accel.c @@ -286,7 +286,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Accel, MP_TYPE_FLAG_NONE, pyb_accel_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_accel_locals_dict + locals_dict, &pyb_accel_locals_dict ); #endif // MICROPY_HW_HAS_MMA7660 || MICROPY_HW_HAS_KXTJ3 diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 7e627d088f744..e0759439d1ece 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -709,7 +709,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, adc_make_new, print, adc_print, - locals_dict, (mp_obj_dict_t *)&adc_locals_dict + locals_dict, &adc_locals_dict ); /******************************************************************************/ @@ -917,7 +917,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_ADCAll, MP_TYPE_FLAG_NONE, adc_all_make_new, - locals_dict, (mp_obj_dict_t *)&adc_all_locals_dict + locals_dict, &adc_all_locals_dict ); #endif // MICROPY_HW_ENABLE_ADC diff --git a/ports/stm32/dac.c b/ports/stm32/dac.c index da50b30fef135..ac8d76090ee04 100644 --- a/ports/stm32/dac.c +++ b/ports/stm32/dac.c @@ -508,7 +508,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_dac_make_new, print, pyb_dac_print, - locals_dict, (mp_obj_dict_t *)&pyb_dac_locals_dict + locals_dict, &pyb_dac_locals_dict ); #endif // MICROPY_HW_ENABLE_DAC diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index 4d2dc5d23be93..0f28610cc3e7e 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -665,7 +665,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, extint_make_new, print, extint_obj_print, - locals_dict, (mp_obj_dict_t *)&extint_locals_dict + locals_dict, &extint_locals_dict ); void extint_init0(void) { diff --git a/ports/stm32/lcd.c b/ports/stm32/lcd.c index a951ea7668447..5017a68fe5489 100644 --- a/ports/stm32/lcd.c +++ b/ports/stm32/lcd.c @@ -530,7 +530,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_LCD, MP_TYPE_FLAG_NONE, pyb_lcd_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_lcd_locals_dict + locals_dict, &pyb_lcd_locals_dict ); #endif // MICROPY_HW_HAS_LCD diff --git a/ports/stm32/led.c b/ports/stm32/led.c index 6745ef60b8d11..6e3229ab32180 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -387,7 +387,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, led_obj_make_new, print, led_obj_print, - locals_dict, (mp_obj_dict_t *)&led_locals_dict + locals_dict, &led_locals_dict ); #else diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index 682bae3a6d13a..3659073d06bf0 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -498,7 +498,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_adc_make_new, print, machine_adc_print, - locals_dict, (mp_obj_dict_t *)&machine_adc_locals_dict + locals_dict, &machine_adc_locals_dict ); #endif diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 89970f234d190..7927daac1bdd7 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -243,7 +243,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); #endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/machine_i2s.c b/ports/stm32/machine_i2s.c index 7dc6439f25c09..93a465d07ca8a 100644 --- a/ports/stm32/machine_i2s.c +++ b/ports/stm32/machine_i2s.c @@ -1123,7 +1123,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_i2s_locals_dict + locals_dict, &machine_i2s_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_MAX_I2S]); diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index 87561c2b7b1dd..d64ff2af8f761 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -142,5 +142,5 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index bd9dbe609849b..2e0120ea8191d 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -142,5 +142,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, machine_timer_make_new, print, machine_timer_print, - locals_dict, (mp_obj_dict_t *)&machine_timer_locals_dict + locals_dict, &machine_timer_locals_dict ); diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 4d25a0274cdd3..5851d8cf352df 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -669,5 +669,5 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&pyb_uart_locals_dict + locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/stm32/network_lan.c b/ports/stm32/network_lan.c index f9c7d80b78e95..25a955508853a 100644 --- a/ports/stm32/network_lan.c +++ b/ports/stm32/network_lan.c @@ -164,7 +164,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, network_lan_make_new, print, network_lan_print, - locals_dict, (mp_obj_dict_t *)&network_lan_locals_dict + locals_dict, &network_lan_locals_dict ); #endif // defined(MICROPY_HW_ETH_MDC) diff --git a/ports/stm32/pin.c b/ports/stm32/pin.c index 992046cd17e5a..3c3cde026568b 100644 --- a/ports/stm32/pin.c +++ b/ports/stm32/pin.c @@ -600,7 +600,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, pin_print, call, pin_call, protocol, &pin_pin_p, - locals_dict, (mp_obj_dict_t *)&pin_locals_dict + locals_dict, &pin_locals_dict ); /// \moduleref pyb @@ -676,7 +676,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pin_af_obj_print, - locals_dict, (mp_obj_dict_t *)&pin_af_locals_dict + locals_dict, &pin_af_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_t pin_class_mapper); diff --git a/ports/stm32/pin_named_pins.c b/ports/stm32/pin_named_pins.c index 620888878c4a6..1a35ec57875fc 100644 --- a/ports/stm32/pin_named_pins.c +++ b/ports/stm32/pin_named_pins.c @@ -36,7 +36,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_cpu, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&pin_cpu_pins_locals_dict + locals_dict, &pin_cpu_pins_locals_dict ); MP_DEFINE_CONST_OBJ_TYPE( @@ -44,7 +44,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_board, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&pin_board_pins_locals_dict + locals_dict, &pin_board_pins_locals_dict ); const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index 6fdfd2c8549e1..a07fc921649cf 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -1079,7 +1079,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_can_make_new, print, pyb_can_print, protocol, &can_stream_p, - locals_dict, (mp_obj_dict_t *)&pyb_can_locals_dict + locals_dict, &pyb_can_locals_dict ); MP_REGISTER_ROOT_POINTER(struct _pyb_can_obj_t *pyb_can_obj_all[MICROPY_HW_MAX_CAN]); diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index ee6983a143fd3..9071d92944ee4 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -1110,7 +1110,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_i2c_make_new, print, pyb_i2c_print, - locals_dict, (mp_obj_dict_t *)&pyb_i2c_locals_dict + locals_dict, &pyb_i2c_locals_dict ); #endif // MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/pyb_spi.c b/ports/stm32/pyb_spi.c index b1425272feeef..a225bd84e7534 100644 --- a/ports/stm32/pyb_spi.c +++ b/ports/stm32/pyb_spi.c @@ -357,5 +357,5 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_spi_make_new, print, pyb_spi_print, protocol, &pyb_spi_p, - locals_dict, (mp_obj_dict_t *)&pyb_spi_locals_dict + locals_dict, &pyb_spi_locals_dict ); diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index c7698db1430a3..f501ec23b1b31 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -842,5 +842,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_RTC, MP_TYPE_FLAG_NONE, pyb_rtc_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_rtc_locals_dict + locals_dict, &pyb_rtc_locals_dict ); diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index b9cc051e7f13d..cbf1ade5c5b78 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -877,7 +877,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, pyb_sdcard_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_sdcard_locals_dict + locals_dict, &pyb_sdcard_locals_dict ); #endif @@ -887,7 +887,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_MMCard, MP_TYPE_FLAG_NONE, pyb_mmcard_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_sdcard_locals_dict + locals_dict, &pyb_sdcard_locals_dict ); #endif diff --git a/ports/stm32/servo.c b/ports/stm32/servo.c index d552f5e6b5d3a..1b0ca0a88224c 100644 --- a/ports/stm32/servo.c +++ b/ports/stm32/servo.c @@ -342,7 +342,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_servo_make_new, print, pyb_servo_print, - locals_dict, (mp_obj_dict_t *)&pyb_servo_locals_dict + locals_dict, &pyb_servo_locals_dict ); #endif // MICROPY_HW_ENABLE_SERVO diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 92f7059493384..a0154408dd5fb 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -459,7 +459,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_flash_make_new, print, pyb_flash_print, - locals_dict, (mp_obj_dict_t *)&pyb_flash_locals_dict + locals_dict, &pyb_flash_locals_dict ); void pyb_flash_init_vfs(fs_user_mount_t *vfs) { diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index abf4c1f3fc4ca..91c88df1b3726 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -1477,7 +1477,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_timer_make_new, print, pyb_timer_print, - locals_dict, (mp_obj_dict_t *)&pyb_timer_locals_dict + locals_dict, &pyb_timer_locals_dict ); /// \moduleref pyb @@ -1617,7 +1617,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, - locals_dict, (mp_obj_dict_t *)&pyb_timer_channel_locals_dict + locals_dict, &pyb_timer_channel_locals_dict ); STATIC void timer_handle_irq_channel(pyb_timer_obj_t *tim, uint8_t channel, mp_obj_t callback) { diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 7c36765c28c1b..df755fe984c30 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -945,7 +945,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &pyb_usb_vcp_stream_p, - locals_dict, (mp_obj_dict_t *)&pyb_usb_vcp_locals_dict + locals_dict, &pyb_usb_vcp_locals_dict ); /******************************************************************************/ @@ -1084,7 +1084,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_usb_hid_make_new, protocol, &pyb_usb_hid_stream_p, - locals_dict, (mp_obj_dict_t *)&pyb_usb_hid_locals_dict + locals_dict, &pyb_usb_hid_locals_dict ); #endif // MICROPY_HW_USB_HID diff --git a/ports/stm32/usrsw.c b/ports/stm32/usrsw.c index 137f4dabfa5c3..7d406c0ecd717 100644 --- a/ports/stm32/usrsw.c +++ b/ports/stm32/usrsw.c @@ -141,7 +141,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_switch_make_new, print, pyb_switch_print, call, pyb_switch_call, - locals_dict, (mp_obj_dict_t *)&pyb_switch_locals_dict + locals_dict, &pyb_switch_locals_dict ); MP_REGISTER_ROOT_POINTER(mp_obj_t pyb_switch_callback); diff --git a/ports/stm32/wdt.c b/ports/stm32/wdt.c index e3b8e2e0ae22e..09780ea9d3cc9 100644 --- a/ports/stm32/wdt.c +++ b/ports/stm32/wdt.c @@ -107,5 +107,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_WDT, MP_TYPE_FLAG_NONE, pyb_wdt_make_new, - locals_dict, (mp_obj_dict_t *)&pyb_wdt_locals_dict + locals_dict, &pyb_wdt_locals_dict ); diff --git a/ports/teensy/led.c b/ports/teensy/led.c index ca548431fecad..dd4c65da39e1b 100644 --- a/ports/teensy/led.c +++ b/ports/teensy/led.c @@ -140,5 +140,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, led_obj_make_new, print, led_obj_print, - locals_dict, (mp_obj_t)&led_locals_dict + locals_dict, &led_locals_dict ); diff --git a/ports/teensy/timer.c b/ports/teensy/timer.c index 4df24743356cc..a0f490d925fbb 100644 --- a/ports/teensy/timer.c +++ b/ports/teensy/timer.c @@ -752,7 +752,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_timer_make_new, print, pyb_timer_print, - locals_dict, (mp_obj_t)&pyb_timer_locals_dict + locals_dict, &pyb_timer_locals_dict ); /// \moduleref pyb @@ -896,7 +896,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, - locals_dict, (mp_obj_t)&pyb_timer_channel_locals_dict + locals_dict, &pyb_timer_channel_locals_dict ); STATIC bool ftm_handle_irq_callback(pyb_timer_obj_t *self, mp_uint_t channel, mp_obj_t callback) { diff --git a/ports/teensy/uart.c b/ports/teensy/uart.c index 8957d92709468..db5b8e2f44336 100644 --- a/ports/teensy/uart.c +++ b/ports/teensy/uart.c @@ -489,5 +489,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, pyb_uart_make_new, print, pyb_uart_print, - locals_dict, (mp_obj_t)&pyb_uart_locals_dict + locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index b4567417a22a8..7b4c0c0bf48e7 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -112,7 +112,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, protocol, &fileio_stream_p, - locals_dict, (mp_obj_dict_t *)&rawfile_locals_dict + locals_dict, &rawfile_locals_dict ); // stream read returns non-blocking error @@ -142,7 +142,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, protocol, &textio_stream_p2, - locals_dict, (mp_obj_dict_t *)&rawfile_locals_dict2 + locals_dict, &rawfile_locals_dict2 ); // str/bytes objects without a valid hash diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 98f0a1aa0ff68..98fd8936b1aa8 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -427,7 +427,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, ffimod_make_new, print, ffimod_print, - locals_dict, (mp_obj_dict_t *)&ffimod_locals_dict + locals_dict, &ffimod_locals_dict ); // FFI function @@ -565,7 +565,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, fficallback_print, - locals_dict, (mp_obj_dict_t *)&fficallback_locals_dict + locals_dict, &fficallback_locals_dict ); // FFI variable @@ -603,7 +603,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, print, ffivar_print, - locals_dict, (mp_obj_dict_t *)&ffivar_locals_dict + locals_dict, &ffivar_locals_dict ); // Generic opaque storage object (unused) diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index 72f95b645b257..02368e453716b 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -182,7 +182,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( print, jclass_print, attr, jclass_attr, call, jclass_call, - locals_dict, (mp_obj_dict_t *)&jclass_locals_dict + locals_dict, &jclass_locals_dict ); STATIC mp_obj_t new_jclass(jclass jc) { @@ -332,7 +332,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( attr, jobject_attr, subscr, jobject_subscr, getiter, subscr_getiter, - // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, + // .locals_dict = &jobject_locals_dict, ); STATIC mp_obj_t new_jobject(jobject jo) { @@ -579,7 +579,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( print, jmethod_print, call, jmethod_call, // .attr = jobject_attr, - // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict, + // .locals_dict = &jobject_locals_dict, ); #ifdef __ANDROID__ diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index d8a8d1d8c38b7..baed88761c463 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -318,7 +318,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, getiter, mp_identity_getiter, iternext, poll_iternext, - locals_dict, (void *)&poll_locals_dict + locals_dict, &poll_locals_dict ); STATIC mp_obj_t select_poll(size_t n_args, const mp_obj_t *args) { diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 7e4476cbd0add..21260f0b2ada5 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -525,7 +525,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, NULL, iternext, NULL, protocol, &usocket_stream_p, - locals_dict, (mp_obj_dict_t *)&usocket_locals_dict + locals_dict, &usocket_locals_dict ); #define BINADDR_MAX_LEN sizeof(struct in6_addr) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 60a1924d803d3..a844eceb50c14 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -133,7 +133,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_i2c_locals_dict + locals_dict, &mp_machine_i2c_locals_dict ); #endif // MICROPY_PY_MACHINE_I2C diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index 48303edd8167e..3114ac36fd008 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -293,7 +293,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, machine_pin_print, call, machine_pin_call, protocol, &machine_pin_pin_p, - locals_dict, (mp_obj_t)&machine_pin_locals_dict + locals_dict, &machine_pin_locals_dict ); STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { diff --git a/ports/zephyr/machine_spi.c b/ports/zephyr/machine_spi.c index 6d9bf896b06d9..d990ed9c150f5 100644 --- a/ports/zephyr/machine_spi.c +++ b/ports/zephyr/machine_spi.c @@ -204,7 +204,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, - locals_dict, (mp_obj_dict_t *)&mp_machine_spi_locals_dict + locals_dict, &mp_machine_spi_locals_dict ); #endif // MICROPY_PY_MACHINE_SPI diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 3f5df7465791b..9580d37907812 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -163,5 +163,5 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, - locals_dict, (mp_obj_dict_t *)&machine_uart_locals_dict + locals_dict, &machine_uart_locals_dict ); diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c index a7bef74ca6deb..77f839fdd5104 100644 --- a/ports/zephyr/modusocket.c +++ b/ports/zephyr/modusocket.c @@ -360,7 +360,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( socket_make_new, print, socket_print, protocol, &socket_stream_p, - locals_dict, (mp_obj_t)&socket_locals_dict + locals_dict, &socket_locals_dict ); // diff --git a/ports/zephyr/modzsensor.c b/ports/zephyr/modzsensor.c index 7c0b0193d3269..beb4d6ad7903a 100644 --- a/ports/zephyr/modzsensor.c +++ b/ports/zephyr/modzsensor.c @@ -110,7 +110,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_Sensor, MP_TYPE_FLAG_NONE, sensor_make_new, - locals_dict, (void *)&sensor_locals_dict + locals_dict, &sensor_locals_dict ); STATIC const mp_rom_map_elem_t mp_module_zsensor_globals_table[] = { diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c index ded7caa657632..6ab8271d34766 100644 --- a/ports/zephyr/zephyr_storage.c +++ b/ports/zephyr/zephyr_storage.c @@ -134,7 +134,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, zephyr_disk_access_make_new, print, zephyr_disk_access_print, - locals_dict, (mp_obj_dict_t *)&zephyr_disk_access_locals_dict + locals_dict, &zephyr_disk_access_locals_dict ); #endif // CONFIG_DISK_ACCESS @@ -256,6 +256,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, zephyr_flash_area_make_new, print, zephyr_flash_area_print, - locals_dict, (mp_obj_dict_t *)&zephyr_flash_area_locals_dict + locals_dict, &zephyr_flash_area_locals_dict ); #endif // CONFIG_FLASH_MAP diff --git a/py/modio.c b/py/modio.c index 093cb1f7e78d2..a1e04e4cac54a 100644 --- a/py/modio.c +++ b/py/modio.c @@ -198,7 +198,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, bufwriter_make_new, protocol, &bufwriter_stream_p, - locals_dict, (mp_obj_dict_t *)&bufwriter_locals_dict + locals_dict, &bufwriter_locals_dict ); #endif // MICROPY_PY_IO_BUFFEREDWRITER diff --git a/py/modthread.c b/py/modthread.c index 0a154474252d1..3116fe6bd9db5 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -121,7 +121,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_lock, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, - locals_dict, (mp_obj_dict_t *)&thread_lock_locals_dict + locals_dict, &thread_lock_locals_dict ); /****************************************************************/ diff --git a/py/objarray.c b/py/objarray.c index 8d0fe7f58503c..1cb163c4b97aa 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -582,7 +582,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, array_binary_op, subscr, array_subscr, buffer, array_get_buffer, - locals_dict, (mp_obj_dict_t *)&mp_obj_array_locals_dict + locals_dict, &mp_obj_array_locals_dict ); #endif @@ -598,7 +598,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, array_binary_op, subscr, array_subscr, buffer, array_get_buffer, - locals_dict, (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict + locals_dict, &mp_obj_bytearray_locals_dict ); #endif @@ -610,7 +610,7 @@ MP_DEFINE_CONST_OBJ_TYPE( #endif #if MICROPY_PY_BUILTINS_BYTES_HEX -#define MEMORYVIEW_TYPE_LOCALS_DICT locals_dict, (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict, +#define MEMORYVIEW_TYPE_LOCALS_DICT locals_dict, &mp_obj_memoryview_locals_dict, #else #define MEMORYVIEW_TYPE_LOCALS_DICT #endif diff --git a/py/objdeque.c b/py/objdeque.c index 22770317ab31a..1a8f76ca110b1 100644 --- a/py/objdeque.c +++ b/py/objdeque.c @@ -161,7 +161,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, deque_make_new, unary_op, deque_unary_op, - locals_dict, (mp_obj_dict_t *)&deque_locals_dict + locals_dict, &deque_locals_dict ); #endif // MICROPY_PY_COLLECTIONS_DEQUE diff --git a/py/objdict.c b/py/objdict.c index 6e217d5c9a716..c65b14caad6ca 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -599,7 +599,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, dict_binary_op, subscr, dict_subscr, getiter, dict_getiter, - locals_dict, (mp_obj_dict_t *)&dict_locals_dict + locals_dict, &dict_locals_dict ); #if MICROPY_PY_COLLECTIONS_ORDEREDDICT @@ -614,7 +614,7 @@ MP_DEFINE_CONST_OBJ_TYPE( subscr, dict_subscr, getiter, dict_getiter, parent, &mp_type_dict, - locals_dict, (mp_obj_dict_t *)&dict_locals_dict + locals_dict, &dict_locals_dict ); #endif diff --git a/py/objgenerator.c b/py/objgenerator.c index 299f25e7bb634..a960c237002f5 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -376,5 +376,5 @@ MP_DEFINE_CONST_OBJ_TYPE( unary_op, mp_generic_unary_op, getiter, mp_identity_getiter, iternext, gen_instance_iternext, - locals_dict, (mp_obj_dict_t *)&gen_instance_locals_dict + locals_dict, &gen_instance_locals_dict ); diff --git a/py/objint.c b/py/objint.c index d7a3f9eb9d18d..f06bc441f15d5 100644 --- a/py/objint.c +++ b/py/objint.c @@ -465,5 +465,5 @@ MP_DEFINE_CONST_OBJ_TYPE( print, mp_obj_int_print, unary_op, mp_obj_int_unary_op, binary_op, mp_obj_int_binary_op, - locals_dict, (mp_obj_dict_t *)&int_locals_dict + locals_dict, &int_locals_dict ); diff --git a/py/objlist.c b/py/objlist.c index 8c7921b9891b9..5f9e99cc79b26 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -462,7 +462,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, list_binary_op, subscr, list_subscr, getiter, list_getiter, - locals_dict, (mp_obj_dict_t *)&list_locals_dict + locals_dict, &list_locals_dict ); diff --git a/py/objobject.c b/py/objobject.c index 868a85b32ab0c..ffad610707279 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -112,7 +112,7 @@ STATIC MP_DEFINE_CONST_DICT(object_locals_dict, object_locals_dict_table); #endif #if MICROPY_CPYTHON_COMPAT -#define OBJECT_TYPE_LOCALS_DICT , locals_dict, (mp_obj_dict_t *)&object_locals_dict +#define OBJECT_TYPE_LOCALS_DICT , locals_dict, &object_locals_dict #else #define OBJECT_TYPE_LOCALS_DICT #endif diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 326153182bca7..5bc397f6ec3bf 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -85,6 +85,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, getiter, mp_identity_getiter, iternext, polymorph_it_iternext, - locals_dict, (mp_obj_dict_t *)&mp_obj_polymorph_iter_locals_dict + locals_dict, &mp_obj_polymorph_iter_locals_dict ); #endif diff --git a/py/objproperty.c b/py/objproperty.c index 42c357f33050b..ce3b572591d2f 100644 --- a/py/objproperty.c +++ b/py/objproperty.c @@ -95,7 +95,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_property, MP_TYPE_FLAG_NONE, property_make_new, - locals_dict, (mp_obj_dict_t *)&property_locals_dict + locals_dict, &property_locals_dict ); const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) { diff --git a/py/objset.c b/py/objset.c index 8fc744a14047e..6f21bf15dfb58 100644 --- a/py/objset.c +++ b/py/objset.c @@ -548,7 +548,7 @@ MP_DEFINE_CONST_OBJ_TYPE( unary_op, set_unary_op, binary_op, set_binary_op, getiter, set_getiter, - locals_dict, (mp_obj_dict_t *)&set_locals_dict + locals_dict, &set_locals_dict ); #if MICROPY_PY_BUILTINS_FROZENSET @@ -574,7 +574,7 @@ MP_DEFINE_CONST_OBJ_TYPE( unary_op, set_unary_op, binary_op, set_binary_op, getiter, set_getiter, - locals_dict, (mp_obj_dict_t *)&frozenset_locals_dict + locals_dict, &frozenset_locals_dict ); #endif diff --git a/py/objslice.c b/py/objslice.c index 7baca1fbe6685..d1dbb24586d9f 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -95,7 +95,7 @@ STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table); #if MICROPY_PY_BUILTINS_SLICE_ATTRS #define SLICE_TYPE_ATTR_OR_LOCALS_DICT attr, slice_attr, #elif MICROPY_PY_BUILTINS_SLICE_INDICES -#define SLICE_TYPE_ATTR_OR_LOCALS_DICT locals_dict, (mp_obj_dict_t *)&slice_locals_dict, +#define SLICE_TYPE_ATTR_OR_LOCALS_DICT locals_dict, &slice_locals_dict, #else #define SLICE_TYPE_ATTR_OR_LOCALS_DICT #endif diff --git a/py/objstr.c b/py/objstr.c index 77ca269d423bb..d425055559f84 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2153,7 +2153,7 @@ MP_DEFINE_CONST_OBJ_TYPE( subscr, bytes_subscr, getiter, mp_obj_new_str_iterator, buffer, mp_obj_str_get_buffer, - locals_dict, (mp_obj_dict_t *)&mp_obj_str_locals_dict + locals_dict, &mp_obj_str_locals_dict ); #endif // !MICROPY_PY_BUILTINS_STR_UNICODE @@ -2168,7 +2168,7 @@ MP_DEFINE_CONST_OBJ_TYPE( subscr, bytes_subscr, getiter, mp_obj_new_bytes_iterator, buffer, mp_obj_str_get_buffer, - locals_dict, (mp_obj_dict_t *)&mp_obj_bytes_locals_dict + locals_dict, &mp_obj_bytes_locals_dict ); // The zero-length bytes object, with data that includes a null-terminating byte diff --git a/py/objstringio.c b/py/objstringio.c index d781ccc789d20..77547f88cf6c2 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -253,7 +253,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &stringio_stream_p, - locals_dict, (mp_obj_dict_t *)&stringio_locals_dict + locals_dict, &stringio_locals_dict ); #if MICROPY_PY_IO_BYTESIO @@ -272,7 +272,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &bytesio_stream_p, - locals_dict, (mp_obj_dict_t *)&stringio_locals_dict + locals_dict, &stringio_locals_dict ); #endif diff --git a/py/objstrunicode.c b/py/objstrunicode.c index afef1498e98e7..15c59e4e95724 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -240,7 +240,7 @@ MP_DEFINE_CONST_OBJ_TYPE( subscr, str_subscr, getiter, mp_obj_new_str_iterator, buffer, mp_obj_str_get_buffer, - locals_dict, (mp_obj_dict_t *)&mp_obj_str_locals_dict + locals_dict, &mp_obj_str_locals_dict ); /******************************************************************************/ diff --git a/py/objtuple.c b/py/objtuple.c index b2ea6e380eb1d..a684b13e6f254 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -234,7 +234,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, mp_obj_tuple_binary_op, subscr, mp_obj_tuple_subscr, getiter, mp_obj_tuple_getiter, - locals_dict, (mp_obj_dict_t *)&tuple_locals_dict + locals_dict, &tuple_locals_dict ); // the zero-length tuple diff --git a/shared/runtime/mpirq.c b/shared/runtime/mpirq.c index 763da6e0e4449..889fa7a9a82ed 100644 --- a/shared/runtime/mpirq.c +++ b/shared/runtime/mpirq.c @@ -131,7 +131,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, call, mp_irq_call, - locals_dict, (mp_obj_dict_t *)&mp_irq_locals_dict + locals_dict, &mp_irq_locals_dict ); #endif // MICROPY_ENABLE_SCHEDULER diff --git a/shared/runtime/sys_stdio_mphal.c b/shared/runtime/sys_stdio_mphal.c index f1290853da734..6d43425e298fe 100644 --- a/shared/runtime/sys_stdio_mphal.c +++ b/shared/runtime/sys_stdio_mphal.c @@ -132,7 +132,7 @@ MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &stdio_obj_stream_p, - locals_dict, (mp_obj_dict_t *)&stdio_locals_dict + locals_dict, &stdio_locals_dict ); const sys_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN}; @@ -168,7 +168,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( getiter, mp_identity_getiter, iternext, mp_stream_unbuffered_iter, protocol, &stdio_buffer_obj_stream_p, - locals_dict, (mp_obj_dict_t *)&stdio_locals_dict + locals_dict, &stdio_locals_dict ); STATIC const sys_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused From 5ddf671944465411f90bd0968550b719d0dbdb80 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Jul 2021 15:02:15 +1000 Subject: [PATCH 2619/3533] py/objexcept: Make MP_DEFINE_EXCEPTION use MP_DEFINE_CONST_OBJ_TYPE. Signed-off-by: Jim Mussared --- py/objexcept.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/py/objexcept.h b/py/objexcept.h index 384456bb51a94..5d56d67d7314f 100644 --- a/py/objexcept.h +++ b/py/objexcept.h @@ -41,13 +41,10 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); #define MP_DEFINE_EXCEPTION(exc_name, base_name) \ - const mp_obj_type_t mp_type_##exc_name = { \ - { &mp_type_type }, \ - .name = MP_QSTR_##exc_name, \ - .print = mp_obj_exception_print, \ - .make_new = mp_obj_exception_make_new, \ - .attr = mp_obj_exception_attr, \ - .parent = &mp_type_##base_name, \ - }; + MP_DEFINE_CONST_OBJ_TYPE(mp_type_##exc_name, MP_QSTR_##exc_name, MP_TYPE_FLAG_NONE, mp_obj_exception_make_new, \ + print, mp_obj_exception_print, \ + attr, mp_obj_exception_attr, \ + parent, &mp_type_##base_name \ + ); #endif // MICROPY_INCLUDED_PY_OBJEXCEPT_H From e8355eb16357b0bd234a9bcab1c9e8b72fcdbabc Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 22 Apr 2022 13:05:56 +1000 Subject: [PATCH 2620/3533] py/obj: Add "full" and "empty" non-variable-length mp_obj_type_t. This will always have the maximum/minimum size of a mp_obj_type_t representation and can be used as a member in other structs. Signed-off-by: Jim Mussared --- examples/natmod/btree/btree_c.c | 2 +- examples/natmod/framebuf/framebuf.c | 2 +- examples/natmod/ure/ure.c | 4 ++-- examples/natmod/uzlib/uzlib.c | 2 +- extmod/modbtree.c | 2 +- extmod/modframebuf.c | 2 +- extmod/modnetwork.h | 3 ++- extmod/modure.c | 10 +++++----- extmod/network_ninaw10.c | 2 +- extmod/network_wiznet5k.c | 2 +- ports/cc3200/mods/modnetwork.h | 3 ++- ports/cc3200/mods/modwlan.c | 2 +- py/obj.h | 15 +++++++++++++-- py/objnamedtuple.h | 5 ++++- py/objtype.c | 5 ++++- 15 files changed, 40 insertions(+), 21 deletions(-) diff --git a/examples/natmod/btree/btree_c.c b/examples/natmod/btree/btree_c.c index 5e8a34ac40095..e342b7d90320a 100644 --- a/examples/natmod/btree/btree_c.c +++ b/examples/natmod/btree/btree_c.c @@ -94,7 +94,7 @@ int mp_stream_posix_fsync(void *stream) { return res; } -mp_obj_type_t btree_type; +mp_obj_full_type_t btree_type; #include "extmod/modbtree.c" diff --git a/examples/natmod/framebuf/framebuf.c b/examples/natmod/framebuf/framebuf.c index 2eff61c817d22..98b44987b342f 100644 --- a/examples/natmod/framebuf/framebuf.c +++ b/examples/natmod/framebuf/framebuf.c @@ -8,7 +8,7 @@ void *memset(void *s, int c, size_t n) { } #endif -mp_obj_type_t mp_type_framebuf; +mp_obj_full_type_t mp_type_framebuf; #include "extmod/modframebuf.c" diff --git a/examples/natmod/ure/ure.c b/examples/natmod/ure/ure.c index 175b93e395ef9..d9e11760a70c1 100644 --- a/examples/natmod/ure/ure.c +++ b/examples/natmod/ure/ure.c @@ -32,8 +32,8 @@ void *memmove(void *dest, const void *src, size_t n) { return mp_fun_table.memmove_(dest, src, n); } -mp_obj_type_t match_type; -mp_obj_type_t re_type; +mp_obj_full_type_t match_type; +mp_obj_full_type_t re_type; #include "extmod/modure.c" diff --git a/examples/natmod/uzlib/uzlib.c b/examples/natmod/uzlib/uzlib.c index 99b3691761ea4..24ab15d6f1446 100644 --- a/examples/natmod/uzlib/uzlib.c +++ b/examples/natmod/uzlib/uzlib.c @@ -8,7 +8,7 @@ void *memset(void *s, int c, size_t n) { } #endif -mp_obj_type_t decompio_type; +mp_obj_full_type_t decompio_type; #include "extmod/moduzlib.c" diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 15cb6341637a5..60c6885e6b2cc 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -67,7 +67,7 @@ void __dbpanic(DB *db) { } STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) { - mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, &btree_type); + mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, (mp_obj_type_t *)&btree_type); o->stream = stream; o->db = db; o->start_key = mp_const_none; diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index f29eab272f0ea..5347be5643667 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -841,7 +841,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( // this factory function is provided for backwards compatibility with old FrameBuffer1 class STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args_in) { - mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, &mp_type_framebuf); + mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, (mp_obj_type_t *)&mp_type_framebuf); mp_buffer_info_t bufinfo; mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE); diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h index 3481cc6dcc575..55ee4eb4d3e35 100644 --- a/extmod/modnetwork.h +++ b/extmod/modnetwork.h @@ -62,7 +62,8 @@ mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_o struct _mod_network_socket_obj_t; typedef struct _mod_network_nic_type_t { - mp_obj_type_t base; + // Ensure that this struct is big enough to hold any type size. + mp_obj_full_type_t base; // API for non-socket operations int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out); diff --git a/extmod/modure.c b/extmod/modure.c index 43959924053bc..c0114c14fd57f 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -198,7 +198,7 @@ STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { (void)n_args; mp_obj_re_t *self; - if (mp_obj_is_type(args[0], &re_type)) { + if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) { self = MP_OBJ_TO_PTR(args[0]); } else { self = MP_OBJ_TO_PTR(mod_re_compile(1, args)); @@ -217,7 +217,7 @@ STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { return mp_const_none; } - match->base.type = &match_type; + match->base.type = (mp_obj_type_t *)&match_type; match->num_matches = caps_num / 2; // caps_num counts start and end pointers match->str = args[1]; return MP_OBJ_FROM_PTR(match); @@ -282,7 +282,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split); STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { mp_obj_re_t *self; - if (mp_obj_is_type(args[0], &re_type)) { + if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) { self = MP_OBJ_TO_PTR(args[0]); } else { self = MP_OBJ_TO_PTR(mod_re_compile(1, args)); @@ -305,7 +305,7 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) { vstr_t vstr_return; vstr_return.buf = NULL; // We'll init the vstr after the first match mp_obj_match_t *match = mp_local_alloc(sizeof(mp_obj_match_t) + caps_num * sizeof(char *)); - match->base.type = &match_type; + match->base.type = (mp_obj_type_t *)&match_type; match->num_matches = caps_num / 2; // caps_num counts start and end pointers match->str = where; @@ -430,7 +430,7 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) { if (size == -1) { goto error; } - mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, &re_type); + mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, (mp_obj_type_t *)&re_type); #if MICROPY_PY_URE_DEBUG int flags = 0; if (n_args > 1) { diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 0906176d20b0b..806819648d2ee 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -774,7 +774,7 @@ static const mp_rom_map_elem_t nina_locals_dict_table[] = { static MP_DEFINE_CONST_DICT(nina_locals_dict, nina_locals_dict_table); -STATIC MP_DEFINE_CONST_OBJ_TYPE( +STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_nina_base, MP_QSTR_nina, MP_TYPE_FLAG_NONE, diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 951a2966c93f9..1d7318827c895 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -1024,7 +1024,7 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &wiznet5k_locals_dict ); #else // WIZNET5K_PROVIDED_STACK -STATIC MP_DEFINE_CONST_OBJ_TYPE( +STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_wiznet5k_base, MP_QSTR_WIZNET5K, MP_TYPE_FLAG_NONE, diff --git a/ports/cc3200/mods/modnetwork.h b/ports/cc3200/mods/modnetwork.h index 6ec90a2bac189..c15a6467f2788 100644 --- a/ports/cc3200/mods/modnetwork.h +++ b/ports/cc3200/mods/modnetwork.h @@ -36,7 +36,8 @@ DEFINE TYPES ******************************************************************************/ typedef struct _mod_network_nic_type_t { - mp_obj_type_t base; + // Ensure that this struct is big enough to hold any type size. + mp_obj_full_type_t base; } mod_network_nic_type_t; typedef struct _mod_network_socket_base_t { diff --git a/ports/cc3200/mods/modwlan.c b/ports/cc3200/mods/modwlan.c index 1c99f075e9020..1e82b07e0874c 100644 --- a/ports/cc3200/mods/modwlan.c +++ b/ports/cc3200/mods/modwlan.c @@ -1285,7 +1285,7 @@ STATIC const mp_rom_map_elem_t wlan_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table); -STATIC MP_DEFINE_CONST_OBJ_TYPE( +STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_wlan_base, MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, diff --git a/py/obj.h b/py/obj.h index 4ab7e0dc09c13..9c0f41dc3d737 100644 --- a/py/obj.h +++ b/py/obj.h @@ -562,6 +562,9 @@ typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_ bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); +// This struct will be updated to become a variable sized struct. In order to +// use this as a member, or allocate dynamically, use the mp_obj_empty_type_t +// or mp_obj_full_type_t structs below (which must be kept in sync). struct _mp_obj_type_t { // A type is an object so must start with this entry, which points to mp_type_type. mp_obj_base_t base; @@ -632,6 +635,13 @@ struct _mp_obj_type_t { struct _mp_obj_dict_t *locals_dict; }; +// Non-variable sized versions of mp_obj_type_t to be used as a member +// in other structs or for dynamic allocation. The fields are exactly +// as in mp_obj_type_t, but with a fixed size for the flexible array +// members. +typedef mp_obj_type_t mp_obj_empty_type_t; +typedef mp_obj_type_t mp_obj_full_type_t; + #define MP_TYPE_NULL_MAKE_NEW (NULL) // Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments. @@ -657,14 +667,15 @@ struct _mp_obj_type_t { // of the 30th argument (30 is 13*2 + 4). #define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N -// This macro is used to define a object type in ROM. +// These macros are used to define a object type in ROM. // Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*) -// It uses the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_* +// They use the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_* // macro to use based on the number of arguments. It works by shifting the // numeric values 12, 11, ... 0 by the number of arguments, such that the // 30th argument ends up being the number to use. The _INV values are // placeholders because the slot arguments come in pairs. #define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__)) +#define MP_DEFINE_CONST_OBJ_FULL_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_full_type_t, __VA_ARGS__)) // Constant types, globally accessible extern const mp_obj_type_t mp_type_type; diff --git a/py/objnamedtuple.h b/py/objnamedtuple.h index d32af35afec43..9f23351d5a36c 100644 --- a/py/objnamedtuple.h +++ b/py/objnamedtuple.h @@ -29,7 +29,10 @@ #include "py/objtuple.h" typedef struct _mp_obj_namedtuple_type_t { - mp_obj_type_t base; + // Must use the full-size version to avoid this being a variable sized member. + // This means that named tuples use slightly more RAM than necessary, but + // no worse than if we didn't have slots/split representation. + mp_obj_full_type_t base; size_t n_fields; qstr fields[]; } mp_obj_namedtuple_type_t; diff --git a/py/objtype.c b/py/objtype.c index 77fe8e22e3844..cc5a5e580254d 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1148,7 +1148,10 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) #endif } - mp_obj_type_t *o = m_new0(mp_obj_type_t, 1); + // Allocate a full-sized mp_obj_full_type_t instance (i.e. all slots / extended fields). + // Given that Python types use almost all the slots anyway, this doesn't cost anything + // extra. + mp_obj_type_t *o = (mp_obj_type_t *)m_new0(mp_obj_full_type_t, 1); o->base.type = &mp_type_type; o->flags = base_flags; o->name = name; From a52cd5b07d6d6e2502fff2bbfb9e5b96562452a4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Jul 2021 17:14:16 +1000 Subject: [PATCH 2621/3533] py/obj: Add accessors for type slots and use everywhere. This is a no-op, but sets the stage for changing the mp_obj_type_t representation. Signed-off-by: Jim Mussared --- drivers/ninaw10/nina_wifi_bsp.c | 2 +- examples/natmod/btree/btree_c.c | 20 +-- examples/natmod/framebuf/framebuf.c | 4 +- examples/natmod/ure/ure.c | 8 +- examples/natmod/uzlib/uzlib.c | 6 +- extmod/machine_i2c.c | 22 +-- extmod/machine_signal.c | 2 +- extmod/machine_spi.c | 6 +- extmod/network_wiznet5k.c | 2 +- extmod/vfs.c | 5 +- extmod/virtpin.c | 4 +- .../nrf/modules/ubluepy/ubluepy_scan_entry.c | 2 +- ports/rp2/mpbthciport.c | 6 +- ports/unix/modffi.c | 4 +- py/builtinhelp.c | 4 +- py/dynruntime.h | 2 +- py/modbuiltins.c | 2 +- py/obj.c | 28 ++-- py/obj.h | 12 +- py/objarray.c | 2 +- py/objnamedtuple.c | 14 +- py/objtuple.c | 4 +- py/objtype.c | 136 +++++++++--------- py/opmethods.c | 12 +- py/runtime.c | 59 ++++---- py/stream.c | 25 ++-- py/stream.h | 2 +- 27 files changed, 207 insertions(+), 188 deletions(-) diff --git a/drivers/ninaw10/nina_wifi_bsp.c b/drivers/ninaw10/nina_wifi_bsp.c index a65ef7fd86448..d5b1308536af9 100644 --- a/drivers/ninaw10/nina_wifi_bsp.c +++ b/drivers/ninaw10/nina_wifi_bsp.c @@ -137,7 +137,7 @@ int nina_bsp_spi_slave_deselect(void) { int nina_bsp_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t size) { mp_obj_t mp_wifi_spi = MP_STATE_PORT(mp_wifi_spi); - ((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer(mp_wifi_spi, size, tx_buf, rx_buf); + ((mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_spi_type, protocol))->transfer(mp_wifi_spi, size, tx_buf, rx_buf); #if NINA_DEBUG for (int i = 0; i < size; i++) { if (tx_buf) { diff --git a/examples/natmod/btree/btree_c.c b/examples/natmod/btree/btree_c.c index e342b7d90320a..af7535a68f7f7 100644 --- a/examples/natmod/btree/btree_c.c +++ b/examples/natmod/btree/btree_c.c @@ -51,7 +51,7 @@ int *__errno (void) ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { mp_obj_base_t* o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t out_sz = stream_p->write(MP_OBJ_FROM_PTR(stream), buf, len, &native_errno); if (out_sz == MP_STREAM_ERROR) { return -1; @@ -62,7 +62,7 @@ ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) { mp_obj_base_t* o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t out_sz = stream_p->read(MP_OBJ_FROM_PTR(stream), buf, len, &native_errno); if (out_sz == MP_STREAM_ERROR) { return -1; @@ -73,7 +73,7 @@ ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) { off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { const mp_obj_base_t* o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); struct mp_stream_seek_t seek_s; seek_s.offset = offset; seek_s.whence = whence; @@ -86,7 +86,7 @@ off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { int mp_stream_posix_fsync(void *stream) { mp_obj_base_t* o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_FLUSH, 0, &native_errno); if (res == MP_STREAM_ERROR) { return -1; @@ -124,11 +124,11 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a btree_type.base.type = (void*)&mp_fun_table.type_type; btree_type.name = MP_QSTR_btree; - btree_type.print = btree_print; - btree_type.getiter = btree_getiter; - btree_type.iternext = btree_iternext; - btree_type.binary_op = btree_binary_op; - btree_type.subscr = btree_subscr; + MP_OBJ_TYPE_SET_SLOT(&btree_type, print, btree_print, 0); + MP_OBJ_TYPE_SET_SLOT(&btree_type, getiter, btree_getiter, 1); + MP_OBJ_TYPE_SET_SLOT(&btree_type, iternext, btree_iternext, 2); + MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 3); + MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 4); btree_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&btree_close_obj) }; btree_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_flush), MP_OBJ_FROM_PTR(&btree_flush_obj) }; btree_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get), MP_OBJ_FROM_PTR(&btree_get_obj) }; @@ -137,7 +137,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a btree_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_keys), MP_OBJ_FROM_PTR(&btree_keys_obj) }; btree_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_values), MP_OBJ_FROM_PTR(&btree_values_obj) }; btree_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_items), MP_OBJ_FROM_PTR(&btree_items_obj) }; - btree_type.locals_dict = (void*)&btree_locals_dict; + MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 5); mp_store_global(MP_QSTR__open, MP_OBJ_FROM_PTR(&btree_open_obj)); mp_store_global(MP_QSTR_INCL, MP_OBJ_NEW_SMALL_INT(FLAG_END_KEY_INCL)); diff --git a/examples/natmod/framebuf/framebuf.c b/examples/natmod/framebuf/framebuf.c index 98b44987b342f..4c557294719d8 100644 --- a/examples/natmod/framebuf/framebuf.c +++ b/examples/natmod/framebuf/framebuf.c @@ -21,7 +21,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a mp_type_framebuf.base.type = (void*)&mp_type_type; mp_type_framebuf.name = MP_QSTR_FrameBuffer; mp_type_framebuf.make_new = framebuf_make_new; - mp_type_framebuf.buffer = framebuf_get_buffer; + MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, buffer, framebuf_get_buffer, 0); framebuf_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill), MP_OBJ_FROM_PTR(&framebuf_fill_obj) }; framebuf_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill_rect), MP_OBJ_FROM_PTR(&framebuf_fill_rect_obj) }; framebuf_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_pixel), MP_OBJ_FROM_PTR(&framebuf_pixel_obj) }; @@ -33,7 +33,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a framebuf_locals_dict_table[8] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_blit), MP_OBJ_FROM_PTR(&framebuf_blit_obj) }; framebuf_locals_dict_table[9] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_scroll), MP_OBJ_FROM_PTR(&framebuf_scroll_obj) }; framebuf_locals_dict_table[10] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_text), MP_OBJ_FROM_PTR(&framebuf_text_obj) }; - mp_type_framebuf.locals_dict = (void*)&framebuf_locals_dict; + MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, locals_dict, (void*)&framebuf_locals_dict, 1); mp_store_global(MP_QSTR_FrameBuffer, MP_OBJ_FROM_PTR(&mp_type_framebuf)); mp_store_global(MP_QSTR_FrameBuffer1, MP_OBJ_FROM_PTR(&legacy_framebuffer1_obj)); diff --git a/examples/natmod/ure/ure.c b/examples/natmod/ure/ure.c index d9e11760a70c1..d4bd680abd428 100644 --- a/examples/natmod/ure/ure.c +++ b/examples/natmod/ure/ure.c @@ -54,21 +54,21 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a match_type.base.type = (void*)&mp_fun_table.type_type; match_type.name = MP_QSTR_match; - match_type.print = match_print; + MP_OBJ_TYPE_SET_SLOT(&match_type, print, match_print, 0); match_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_group), MP_OBJ_FROM_PTR(&match_group_obj) }; match_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_groups), MP_OBJ_FROM_PTR(&match_groups_obj) }; match_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_span), MP_OBJ_FROM_PTR(&match_span_obj) }; match_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_start), MP_OBJ_FROM_PTR(&match_start_obj) }; match_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_OBJ_FROM_PTR(&match_end_obj) }; - match_type.locals_dict = (void*)&match_locals_dict; + MP_OBJ_TYPE_SET_SLOT(&match_type, locals_dict, (void*)&match_locals_dict, 1); re_type.base.type = (void*)&mp_fun_table.type_type; re_type.name = MP_QSTR_ure; - re_type.print = re_print; + MP_OBJ_TYPE_SET_SLOT(&re_type, print, re_print, 0); re_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_match), MP_OBJ_FROM_PTR(&re_match_obj) }; re_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_search), MP_OBJ_FROM_PTR(&re_search_obj) }; re_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_split), MP_OBJ_FROM_PTR(&re_split_obj) }; - re_type.locals_dict = (void*)&re_locals_dict; + MP_OBJ_TYPE_SET_SLOT(&re_type, locals_dict, (void*)&re_locals_dict, 1); mp_store_global(MP_QSTR_compile, MP_OBJ_FROM_PTR(&mod_re_compile_obj)); mp_store_global(MP_QSTR_match, MP_OBJ_FROM_PTR(&re_match_obj)); diff --git a/examples/natmod/uzlib/uzlib.c b/examples/natmod/uzlib/uzlib.c index 24ab15d6f1446..469452e8f8437 100644 --- a/examples/natmod/uzlib/uzlib.c +++ b/examples/natmod/uzlib/uzlib.c @@ -20,12 +20,12 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a decompio_type.base.type = mp_fun_table.type_type; decompio_type.name = MP_QSTR_DecompIO; - decompio_type.make_new = decompio_make_new; - decompio_type.protocol = &decompio_stream_p; + decompio_type.make_new = &decompio_make_new; + MP_OBJ_TYPE_SET_SLOT(&decompio_type, protocol, &decompio_stream_p, 0); decompio_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_OBJ_FROM_PTR(&mp_stream_read_obj) }; decompio_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_OBJ_FROM_PTR(&mp_stream_readinto_obj) }; decompio_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_OBJ_FROM_PTR(&mp_stream_unbuffered_readline_obj) }; - decompio_type.locals_dict = (void*)&decompio_locals_dict; + MP_OBJ_TYPE_SET_SLOT(&decompio_type, locals_dict, (void*)&decompio_locals_dict, 1); mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_uzlib)); mp_store_global(MP_QSTR_decompress, MP_OBJ_FROM_PTR(&mod_uzlib_decompress_obj)); diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 378bf8fc914b7..bb7ade6fcf574 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -273,7 +273,7 @@ int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n } } - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); int ret = i2c_p->transfer_single(self, addr, len, buf, flags); if (n > 1) { @@ -292,14 +292,14 @@ int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n } STATIC int mp_machine_i2c_readfrom(mp_obj_base_t *self, uint16_t addr, uint8_t *dest, size_t len, bool stop) { - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); mp_machine_i2c_buf_t buf = {.len = len, .buf = dest}; unsigned int flags = MP_MACHINE_I2C_FLAG_READ | (stop ? MP_MACHINE_I2C_FLAG_STOP : 0); return i2c_p->transfer(self, addr, 1, &buf, flags); } STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint8_t *src, size_t len, bool stop) { - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); mp_machine_i2c_buf_t buf = {.len = len, .buf = (uint8_t *)src}; unsigned int flags = stop ? MP_MACHINE_I2C_FLAG_STOP : 0; return i2c_p->transfer(self, addr, 1, &buf, flags); @@ -310,7 +310,7 @@ STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint STATIC mp_obj_t machine_i2c_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->init == NULL) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); } @@ -338,7 +338,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_scan_obj, machine_i2c_scan); STATIC mp_obj_t machine_i2c_start(mp_obj_t self_in) { mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->start == NULL) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); } @@ -352,7 +352,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_start_obj, machine_i2c_start); STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) { mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->stop == NULL) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); } @@ -366,7 +366,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop); STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) { mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->read == NULL) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); } @@ -390,7 +390,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_ STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) { mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in); - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->write == NULL) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported")); } @@ -486,7 +486,7 @@ STATIC mp_obj_t machine_i2c_writevto(size_t n_args, const mp_obj_t *args) { } // Do the I2C transfer - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); int ret = i2c_p->transfer(self, addr, nbufs, bufs, stop ? MP_MACHINE_I2C_FLAG_STOP : 0); mp_local_free(bufs); @@ -519,7 +519,7 @@ STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t a #if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 // The I2C transfer function may support the MP_MACHINE_I2C_FLAG_WRITE1 option - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); if (i2c_p->transfer_supports_write1) { // Create partial write and read buffers mp_machine_i2c_buf_t bufs[2] = { @@ -556,7 +556,7 @@ STATIC int write_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t }; // Do I2C transfer - mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol; + mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol); return i2c_p->transfer(self, addr, 2, bufs, MP_MACHINE_I2C_FLAG_STOP); } diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index f665ffaa494e7..49ee6dfb4abc8 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -51,7 +51,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t if (n_args > 0 && mp_obj_is_obj(args[0])) { mp_obj_base_t *pin_base = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); - pin_p = (mp_pin_p_t *)pin_base->type->protocol; + pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT_OR_NULL(pin_base->type, protocol); } if (pin_p == NULL) { diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index c7fc5877b1b09..54f1964e2193a 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -43,7 +43,7 @@ STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); - mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol; + mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol); spi_p->init(s, n_args - 1, args + 1, kw_args); return mp_const_none; } @@ -51,7 +51,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init); STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self); - mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol; + mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol); if (spi_p->deinit != NULL) { spi_p->deinit(s); } @@ -61,7 +61,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit); STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self); - mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol; + mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol); spi_p->transfer(s, len, src, dest); } diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 1d7318827c895..82f836f691844 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -744,7 +744,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k; wiznet5k_obj.cris_state = 0; wiznet5k_obj.spi = spi; - wiznet5k_obj.spi_transfer = ((mp_machine_spi_p_t *)spi->type->protocol)->transfer; + wiznet5k_obj.spi_transfer = ((mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(spi->type, protocol))->transfer; wiznet5k_obj.cs = cs; wiznet5k_obj.rst = rst; #if WIZNET5K_WITH_LWIP_STACK diff --git a/extmod/vfs.c b/extmod/vfs.c index 0ef20e9281088..00450e1005595 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -132,8 +132,9 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) { } // If the mounted object has the VFS protocol, call its import_stat helper - const mp_vfs_proto_t *proto = mp_obj_get_type(vfs->obj)->protocol; - if (proto != NULL) { + const mp_obj_type_t *type = mp_obj_get_type(vfs->obj); + if (MP_OBJ_TYPE_HAS_SLOT(type, protocol)) { + const mp_vfs_proto_t *proto = MP_OBJ_TYPE_GET_SLOT(type, protocol); return proto->import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out); } diff --git a/extmod/virtpin.c b/extmod/virtpin.c index 71a11232d4339..cd0b9f92f830e 100644 --- a/extmod/virtpin.c +++ b/extmod/virtpin.c @@ -28,12 +28,12 @@ int mp_virtual_pin_read(mp_obj_t pin) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(pin); - mp_pin_p_t *pin_p = (mp_pin_p_t *)s->type->protocol; + mp_pin_p_t *pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol); return pin_p->ioctl(pin, MP_PIN_READ, 0, NULL); } void mp_virtual_pin_write(mp_obj_t pin, int value) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(pin); - mp_pin_p_t *pin_p = (mp_pin_p_t *)s->type->protocol; + mp_pin_p_t *pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol); pin_p->ioctl(pin, MP_PIN_WRITE, value, NULL); } diff --git a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c index 5c74e2e92caf4..89ca65d18c800 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c @@ -91,7 +91,7 @@ STATIC mp_obj_t scan_entry_get_scan_data(mp_obj_t self_in) { mp_obj_t description = mp_const_none; - mp_map_t *constant_map = mp_obj_dict_get_map(ubluepy_constants_ad_types_type.locals_dict); + mp_map_t *constant_map = mp_obj_dict_get_map(MP_OBJ_TYPE_GET_SLOT(&ubluepy_constants_ad_types_type, locals_dict)); mp_map_elem_t *ad_types_table = MP_OBJ_TO_PTR(constant_map->table); uint16_t num_of_elements = constant_map->used; diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index 91c908b164fc8..e37167a974dee 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -128,7 +128,7 @@ int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { int mp_bluetooth_hci_uart_any(void) { int errcode = 0; - const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode); if (errcode != 0) { @@ -142,7 +142,7 @@ int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { debug_printf("mp_bluetooth_hci_uart_write\n"); int errcode = 0; - const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); mp_bluetooth_hci_controller_wakeup(); @@ -159,7 +159,7 @@ int mp_bluetooth_hci_uart_readchar(void) { if (mp_bluetooth_hci_uart_any()) { int errcode = 0; uint8_t buf = 0; - const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) { error_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode); return -1; diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 98fd8936b1aa8..04152f1ad41f2 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -506,10 +506,10 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const } else if (mp_obj_is_str(a)) { const char *s = mp_obj_str_get_str(a); values[i].ffi = (ffi_arg)(intptr_t)s; - } else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer != NULL) { + } else if (MP_OBJ_TYPE_HAS_SLOT(((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type, buffer)) { mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a); mp_buffer_info_t bufinfo; - int ret = o->type->buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ? + int ret = MP_OBJ_TYPE_GET_SLOT(o->type, buffer)(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ? if (ret != 0) { goto error; } diff --git a/py/builtinhelp.c b/py/builtinhelp.c index 94f8beaff23c8..93d94a389d269 100644 --- a/py/builtinhelp.c +++ b/py/builtinhelp.c @@ -143,8 +143,8 @@ STATIC void mp_help_print_obj(const mp_obj_t obj) { if (type == &mp_type_type) { type = MP_OBJ_TO_PTR(obj); } - if (type->locals_dict != NULL) { - map = &type->locals_dict->map; + if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) { + map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map; } } if (map != NULL) { diff --git a/py/dynruntime.h b/py/dynruntime.h index fb748eb93fe78..be573bde2ad4a 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -150,7 +150,7 @@ static inline mp_obj_t mp_obj_cast_to_native_base_dyn(mp_obj_t self_in, mp_const if (MP_OBJ_FROM_PTR(self_type) == native_type) { return self_in; - } else if (self_type->parent != native_type) { + } else if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(self_type, parent) != native_type) { // The self_in object is not a direct descendant of native_type, so fail the cast. // This is a very simple version of mp_obj_is_subclass_fast that could be improved. return MP_OBJ_NULL; diff --git a/py/modbuiltins.c b/py/modbuiltins.c index f74db95cf5a54..152323b5ca963 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -455,7 +455,7 @@ STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { #if MICROPY_CAN_OVERRIDE_BUILTINS // Set "_" special variable mp_obj_t dest[2] = {MP_OBJ_SENTINEL, o}; - mp_type_module.attr(MP_OBJ_FROM_PTR(&mp_module_builtins), MP_QSTR__, dest); + MP_OBJ_TYPE_GET_SLOT(&mp_type_module, attr)(MP_OBJ_FROM_PTR(&mp_module_builtins), MP_QSTR__, dest); #endif } return mp_const_none; diff --git a/py/obj.c b/py/obj.c index eeadd3eedddae..359c73b9c2120 100644 --- a/py/obj.c +++ b/py/obj.c @@ -116,8 +116,8 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t } #endif const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (type->print != NULL) { - type->print((mp_print_t *)print, o_in, kind); + if (MP_OBJ_TYPE_HAS_SLOT(type, print)) { + MP_OBJ_TYPE_GET_SLOT(type, print)((mp_print_t *)print, o_in, kind); } else { mp_printf(print, "<%q>", type->name); } @@ -170,8 +170,8 @@ bool mp_obj_is_true(mp_obj_t arg) { } } else { const mp_obj_type_t *type = mp_obj_get_type(arg); - if (type->unary_op != NULL) { - mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg); + if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) { + mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_BOOL, arg); if (result != MP_OBJ_NULL) { return result == mp_const_true; } @@ -189,7 +189,7 @@ bool mp_obj_is_true(mp_obj_t arg) { } bool mp_obj_is_callable(mp_obj_t o_in) { - const mp_call_fun_t call = mp_obj_get_type(o_in)->call; + const mp_call_fun_t call = MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o_in), call); if (call != mp_obj_instance_call) { return call != NULL; } @@ -256,19 +256,19 @@ mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) { const mp_obj_type_t *type = mp_obj_get_type(o1); // If a full equality test is not needed and the other object is a different // type then we don't need to bother trying the comparison. - if (type->binary_op != NULL && + if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op) && ((type->flags & MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE) || mp_obj_get_type(o2) == type)) { // CPython is asymmetric: it will try __eq__ if there's no __ne__ but not the // other way around. If the class doesn't need a full test we can skip __ne__. if (op == MP_BINARY_OP_NOT_EQUAL && (type->flags & MP_TYPE_FLAG_EQ_HAS_NEQ_TEST)) { - mp_obj_t r = type->binary_op(MP_BINARY_OP_NOT_EQUAL, o1, o2); + mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_NOT_EQUAL, o1, o2); if (r != MP_OBJ_NULL) { return r; } } // Try calling __eq__. - mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2); + mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_EQUAL, o1, o2); if (r != MP_OBJ_NULL) { if (op == MP_BINARY_OP_EQUAL) { return r; @@ -524,8 +524,8 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { return MP_OBJ_NEW_SMALL_INT(l); } else { const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (type->unary_op != NULL) { - return type->unary_op(MP_UNARY_OP_LEN, o_in); + if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) { + return MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_LEN, o_in); } else { return MP_OBJ_NULL; } @@ -534,8 +534,8 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { const mp_obj_type_t *type = mp_obj_get_type(base); - if (type->subscr != NULL) { - mp_obj_t ret = type->subscr(base, index, value); + if (MP_OBJ_TYPE_HAS_SLOT(type, subscr)) { + mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, subscr)(base, index, value); if (ret != MP_OBJ_NULL) { return ret; } @@ -579,10 +579,10 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) { bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { const mp_obj_type_t *type = mp_obj_get_type(obj); - if (type->buffer == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(type, buffer)) { return false; } - int ret = type->buffer(obj, bufinfo, flags); + int ret = MP_OBJ_TYPE_GET_SLOT(type, buffer)(obj, bufinfo, flags); if (ret != 0) { return false; } diff --git a/py/obj.h b/py/obj.h index 9c0f41dc3d737..37e6f0052428c 100644 --- a/py/obj.h +++ b/py/obj.h @@ -660,6 +660,16 @@ typedef mp_obj_type_t mp_obj_full_type_t; #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11 } #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11, .f12 = v12 } +// Always safe, checks if the type can and does have this slot. +#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->f) +// Requires you know that this type can have this slot. +#define MP_OBJ_TYPE_GET_SLOT(t, f) ((t)->f) +// Always safe, returns NULL if the type cannot have this slot. +#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) ((t)->f) +#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->f = v) +#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, f)) +#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(void **)((char *)(t) + (offset)) != NULL) + // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x @@ -822,7 +832,7 @@ void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type); #endif #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int)) #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str)) -#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op)) +#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op)) #define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new) #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function)) diff --git a/py/objarray.c b/py/objarray.c index 1cb163c4b97aa..762a4105c5f56 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -443,7 +443,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value size_t src_len; void *src_items; size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL); - if (mp_obj_is_obj(value) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(value))->type->subscr == array_subscr) { + if (mp_obj_is_obj(value) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(value))->type, subscr) == array_subscr) { // value is array, bytearray or memoryview mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value); if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) { diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index b2129935067c1..cc55aec0b97c0 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -157,14 +157,14 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t o->base.base.type = &mp_type_type; o->base.flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE; // can match tuple o->base.name = name; - o->base.print = namedtuple_print; o->base.make_new = namedtuple_make_new; - o->base.unary_op = mp_obj_tuple_unary_op; - o->base.binary_op = mp_obj_tuple_binary_op; - o->base.attr = namedtuple_attr; - o->base.subscr = mp_obj_tuple_subscr; - o->base.getiter = mp_obj_tuple_getiter; - o->base.parent = &mp_type_tuple; + MP_OBJ_TYPE_SET_SLOT(&o->base, print, namedtuple_print, 0); + MP_OBJ_TYPE_SET_SLOT(&o->base, unary_op, mp_obj_tuple_unary_op, 1); + MP_OBJ_TYPE_SET_SLOT(&o->base, binary_op, mp_obj_tuple_binary_op, 2); + MP_OBJ_TYPE_SET_SLOT(&o->base, attr, namedtuple_attr, 3); + MP_OBJ_TYPE_SET_SLOT(&o->base, subscr, mp_obj_tuple_subscr, 4); + MP_OBJ_TYPE_SET_SLOT(&o->base, getiter, mp_obj_tuple_getiter, 5); + MP_OBJ_TYPE_SET_SLOT(&o->base, parent, &mp_type_tuple, 6); return MP_OBJ_FROM_PTR(o); } diff --git a/py/objtuple.c b/py/objtuple.c index a684b13e6f254..01b2fa14885ac 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -32,7 +32,7 @@ #include "py/runtime.h" // type check is done on getiter method to allow tuple, namedtuple, attrtuple -#define mp_obj_is_tuple_compatible(o) (mp_obj_get_type(o)->getiter == mp_obj_tuple_getiter) +#define mp_obj_is_tuple_compatible(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), getiter) == mp_obj_tuple_getiter) /******************************************************************************/ /* tuple */ @@ -111,7 +111,7 @@ STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t anothe mp_check_self(mp_obj_is_tuple_compatible(self_in)); const mp_obj_type_t *another_type = mp_obj_get_type(another_in); mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in); - if (another_type->getiter != mp_obj_tuple_getiter) { + if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(another_type, getiter) != mp_obj_tuple_getiter) { // Slow path for user subclasses another_in = mp_obj_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple)); if (another_in == MP_OBJ_NULL) { diff --git a/py/objtype.c b/py/objtype.c index cc5a5e580254d..968d6eb3ac32f 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -59,13 +59,13 @@ STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_t // Native types don't have parents (at least not from our perspective) so end. *last_native_base = type; return count + 1; - } else if (type->parent == NULL) { + } else if (!MP_OBJ_TYPE_HAS_SLOT(type, parent)) { // No parents so end search here. return count; #if MICROPY_MULTIPLE_INHERITANCE - } else if (((mp_obj_base_t *)type->parent)->type == &mp_type_tuple) { + } else if (((mp_obj_base_t *)MP_OBJ_TYPE_GET_SLOT(type, parent))->type == &mp_type_tuple) { // Multiple parents, search through them all recursively. - const mp_obj_tuple_t *parent_tuple = type->parent; + const mp_obj_tuple_t *parent_tuple = MP_OBJ_TYPE_GET_SLOT(type, parent); const mp_obj_t *item = parent_tuple->items; const mp_obj_t *top = item + parent_tuple->len; for (; item < top; ++item) { @@ -77,7 +77,7 @@ STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_t #endif } else { // A single parent, use iteration to continue the search. - type = type->parent; + type = MP_OBJ_TYPE_GET_SLOT(type, parent); } } } @@ -118,7 +118,7 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_ // will keep lookup->dest[0]'s value (should be MP_OBJ_NULL on invocation) if attribute // is not found // will set lookup->dest[0] to MP_OBJ_SENTINEL if special method was found in a native -// type base via slot id (as specified by lookup->meth_offset). As there can be only one +// type base via slot id (as specified by lookup->slot_offset). As there can be only one // native base, it's known that it applies to instance->subobj[0]. In most cases, we also // don't need to know which type it was - because instance->subobj[0] is of that type. // The only exception is when object is not yet constructed, then we need to know base @@ -127,7 +127,7 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_ struct class_lookup_data { mp_obj_instance_t *obj; qstr attr; - size_t meth_offset; + size_t slot_offset; mp_obj_t *dest; bool is_type; }; @@ -141,19 +141,19 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_t // This avoids extra method_name => slot lookup. On the other hand, // this should not be applied to class types, as will result in extra // lookup either. - if (lookup->meth_offset != 0 && mp_obj_is_native_type(type)) { - if (*(void **)((char *)type + lookup->meth_offset) != NULL) { + if (lookup->slot_offset != 0 && mp_obj_is_native_type(type)) { + if (MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(type, lookup->slot_offset)) { DEBUG_printf("mp_obj_class_lookup: Matched special meth slot (off=%d) for %s\n", - lookup->meth_offset, qstr_str(lookup->attr)); + lookup->slot_offset, qstr_str(lookup->attr)); lookup->dest[0] = MP_OBJ_SENTINEL; return; } } - if (type->locals_dict != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) { // search locals_dict (the set of methods/attributes) - assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(type->locals_dict))); // MicroPython restriction, for now - mp_map_t *locals_map = &type->locals_dict->map; + assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(MP_OBJ_TYPE_GET_SLOT(type, locals_dict)))); // MicroPython restriction, for now + mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map; mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(lookup->attr), MP_MAP_LOOKUP); if (elem != NULL) { if (lookup->is_type) { @@ -197,12 +197,12 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_t // attribute not found, keep searching base classes - if (type->parent == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(type, parent)) { DEBUG_printf("mp_obj_class_lookup: No more parents\n"); return; #if MICROPY_MULTIPLE_INHERITANCE - } else if (((mp_obj_base_t *)type->parent)->type == &mp_type_tuple) { - const mp_obj_tuple_t *parent_tuple = type->parent; + } else if (((mp_obj_base_t *)MP_OBJ_TYPE_GET_SLOT(type, parent))->type == &mp_type_tuple) { + const mp_obj_tuple_t *parent_tuple = MP_OBJ_TYPE_GET_SLOT(type, parent); const mp_obj_t *item = parent_tuple->items; const mp_obj_t *top = item + parent_tuple->len - 1; for (; item < top; ++item) { @@ -223,7 +223,7 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_t type = (mp_obj_type_t *)MP_OBJ_TO_PTR(*item); #endif } else { - type = type->parent; + type = MP_OBJ_TYPE_GET_SLOT(type, parent); } if (type == &mp_type_object) { // Not a "real" type @@ -239,7 +239,7 @@ STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k struct class_lookup_data lookup = { .obj = self, .attr = meth, - .meth_offset = offsetof(mp_obj_type_t, print), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(print), .dest = member, .is_type = false, }; @@ -247,7 +247,7 @@ STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k if (member[0] == MP_OBJ_NULL && kind == PRINT_STR) { // If there's no __str__, fall back to __repr__ lookup.attr = MP_QSTR___repr__; - lookup.meth_offset = 0; + lookup.slot_offset = 0; mp_obj_class_lookup(&lookup, self->base.type); } @@ -282,7 +282,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size struct class_lookup_data lookup = { .obj = NULL, .attr = MP_QSTR___new__, - .meth_offset = offsetof(mp_obj_type_t, make_new), + .slot_offset = offsetof(mp_obj_type_t, make_new), .dest = init_fn, .is_type = false, }; @@ -332,7 +332,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size init_fn[0] = init_fn[1] = MP_OBJ_NULL; lookup.obj = o; lookup.attr = MP_QSTR___init__; - lookup.meth_offset = 0; + lookup.slot_offset = 0; mp_obj_class_lookup(&lookup, self); if (init_fn[0] != MP_OBJ_NULL) { mp_obj_t init_ret; @@ -414,7 +414,7 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) { struct class_lookup_data lookup = { .obj = self, .attr = op_name, - .meth_offset = offsetof(mp_obj_type_t, unary_op), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(unary_op), .dest = member, .is_type = false, }; @@ -542,7 +542,7 @@ retry:; struct class_lookup_data lookup = { .obj = lhs, .attr = op_name, - .meth_offset = offsetof(mp_obj_type_t, binary_op), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(binary_op), .dest = dest, .is_type = false, }; @@ -608,7 +608,7 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des struct class_lookup_data lookup = { .obj = self, .attr = attr, - .meth_offset = 0, + .slot_offset = 0, .dest = dest, .is_type = false, }; @@ -693,7 +693,7 @@ STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val struct class_lookup_data lookup = { .obj = self, .attr = attr, - .meth_offset = 0, + .slot_offset = 0, .dest = member, .is_type = false, }; @@ -815,7 +815,7 @@ STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value mp_obj_t member[4] = {MP_OBJ_NULL, MP_OBJ_NULL, index, value}; struct class_lookup_data lookup = { .obj = self, - .meth_offset = offsetof(mp_obj_type_t, subscr), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(subscr), .dest = member, .is_type = false, }; @@ -850,7 +850,7 @@ STATIC mp_obj_t mp_obj_instance_get_call(mp_obj_t self_in, mp_obj_t *member) { struct class_lookup_data lookup = { .obj = self, .attr = MP_QSTR___call__, - .meth_offset = offsetof(mp_obj_type_t, call), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(call), .dest = member, .is_type = false, }; @@ -889,7 +889,7 @@ mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) struct class_lookup_data lookup = { .obj = self, .attr = MP_QSTR___iter__, - .meth_offset = offsetof(mp_obj_type_t, getiter), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(getiter), .dest = member, .is_type = false, }; @@ -901,7 +901,7 @@ mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) if (iter_buf == NULL) { iter_buf = m_new_obj(mp_obj_iter_buf_t); } - return type->getiter(self->subobj[0], iter_buf); + return MP_OBJ_TYPE_GET_SLOT(type, getiter)(self->subobj[0], iter_buf); } else { return mp_call_method_n_kw(0, 0, member); } @@ -913,14 +913,14 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, struct class_lookup_data lookup = { .obj = self, .attr = MP_QSTR_, // don't actually look for a method - .meth_offset = offsetof(mp_obj_type_t, buffer), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(buffer), .dest = member, .is_type = false, }; mp_obj_class_lookup(&lookup, self->base.type); if (member[0] == MP_OBJ_SENTINEL) { const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]); - return type->buffer(self->subobj[0], bufinfo, flags); + return MP_OBJ_TYPE_GET_SLOT(type, buffer)(self->subobj[0], bufinfo, flags); } else { return 1; // object does not support buffer protocol } @@ -1021,7 +1021,7 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (attr == MP_QSTR___dict__) { // Returns a read-only dict of the class attributes. // If the internal locals is not fixed, a copy will be created. - const mp_obj_dict_t *dict = self->locals_dict; + const mp_obj_dict_t *dict = MP_OBJ_TYPE_GET_SLOT_OR_NULL(self, locals_dict); if (!dict) { dict = &mp_const_empty_dict_obj; } @@ -1040,7 +1040,7 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { dest[0] = mp_const_empty_tuple; return; } - mp_obj_t parent_obj = self->parent ? MP_OBJ_FROM_PTR(self->parent) : MP_OBJ_FROM_PTR(&mp_type_object); + mp_obj_t parent_obj = MP_OBJ_TYPE_HAS_SLOT(self, parent) ? MP_OBJ_FROM_PTR(MP_OBJ_TYPE_GET_SLOT(self, parent)) : MP_OBJ_FROM_PTR(&mp_type_object); #if MICROPY_MULTIPLE_INHERITANCE if (mp_obj_is_type(parent_obj, &mp_type_tuple)) { dest[0] = parent_obj; @@ -1054,7 +1054,7 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { struct class_lookup_data lookup = { .obj = (mp_obj_instance_t *)self, .attr = attr, - .meth_offset = 0, + .slot_offset = 0, .dest = dest, .is_type = true, }; @@ -1062,9 +1062,9 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } else { // delete/store attribute - if (self->locals_dict != NULL) { - assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(self->locals_dict))); // MicroPython restriction, for now - mp_map_t *locals_map = &self->locals_dict->map; + if (MP_OBJ_TYPE_HAS_SLOT(self, locals_dict)) { + assert(mp_obj_is_dict_or_ordereddict(MP_OBJ_FROM_PTR(MP_OBJ_TYPE_GET_SLOT(self, locals_dict)))); // MicroPython restriction, for now + mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(self, locals_dict)->map; if (locals_map->is_fixed) { // can't apply delete/store to a fixed map return; @@ -1155,43 +1155,44 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) o->base.type = &mp_type_type; o->flags = base_flags; o->name = name; - o->print = instance_print; o->make_new = mp_obj_instance_make_new; - o->call = mp_obj_instance_call; - o->unary_op = instance_unary_op; - o->binary_op = instance_binary_op; - o->attr = mp_obj_instance_attr; - o->subscr = instance_subscr; - o->getiter = mp_obj_instance_getiter; - // o->iternext = ; not implemented - o->buffer = instance_get_buffer; + MP_OBJ_TYPE_SET_SLOT(o, print, instance_print, 0); + MP_OBJ_TYPE_SET_SLOT(o, call, mp_obj_instance_call, 1); + MP_OBJ_TYPE_SET_SLOT(o, unary_op, instance_unary_op, 2); + MP_OBJ_TYPE_SET_SLOT(o, binary_op, instance_binary_op, 3); + MP_OBJ_TYPE_SET_SLOT(o, attr, mp_obj_instance_attr, 4); + MP_OBJ_TYPE_SET_SLOT(o, subscr, instance_subscr, 5); + MP_OBJ_TYPE_SET_SLOT(o, getiter, mp_obj_instance_getiter, 6); + // MP_OBJ_TYPE_SET_SLOT(o, iternext, not implemented) + MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 7); if (bases_len > 0) { // Inherit protocol from a base class. This allows to define an // abstract base class which would translate C-level protocol to // Python method calls, and any subclass inheriting from it will // support this feature. - o->protocol = ((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0]))->protocol; + MP_OBJ_TYPE_SET_SLOT(o, protocol, MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0])), protocol), 8); if (bases_len >= 2) { #if MICROPY_MULTIPLE_INHERITANCE - o->parent = MP_OBJ_TO_PTR(bases_tuple); + MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_tuple), 9); #else mp_raise_NotImplementedError(MP_ERROR_TEXT("multiple inheritance not supported")); #endif } else { - o->parent = MP_OBJ_TO_PTR(bases_items[0]); + MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_items[0]), 9); } } - o->locals_dict = MP_OBJ_TO_PTR(locals_dict); + mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict); + MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 10); #if ENABLE_SPECIAL_ACCESSORS // Check if the class has any special accessor methods if (!(o->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) { - for (size_t i = 0; i < o->locals_dict->map.alloc; i++) { - if (mp_map_slot_is_filled(&o->locals_dict->map, i)) { - const mp_map_elem_t *elem = &o->locals_dict->map.table[i]; + for (size_t i = 0; i < locals_ptr->map.alloc; i++) { + if (mp_map_slot_is_filled(&locals_ptr->map, i)) { + const mp_map_elem_t *elem = &locals_ptr->map.table[i]; if (check_for_special_accessors(elem->key, elem->value)) { o->flags |= MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS; break; @@ -1207,7 +1208,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) mp_raise_TypeError(MP_ERROR_TEXT("multiple bases have instance lay-out conflict")); } - mp_map_t *locals_map = &o->locals_dict->map; + mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(o, locals_dict)->map; mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(MP_QSTR___new__), MP_MAP_LOOKUP); if (elem != NULL) { // __new__ slot exists; check if it is a function @@ -1268,21 +1269,21 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { struct class_lookup_data lookup = { .obj = MP_OBJ_TO_PTR(self->obj), .attr = attr, - .meth_offset = 0, + .slot_offset = 0, .dest = dest, .is_type = false, }; - // Allow a call super().__init__() to reach any native base classes + // Allow a call super().__init__() to reach any native base classes. if (attr == MP_QSTR___init__) { - lookup.meth_offset = offsetof(mp_obj_type_t, make_new); + lookup.slot_offset = offsetof(mp_obj_type_t, make_new); } - if (type->parent == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(type, parent)) { // no parents, do nothing #if MICROPY_MULTIPLE_INHERITANCE - } else if (((mp_obj_base_t *)type->parent)->type == &mp_type_tuple) { - const mp_obj_tuple_t *parent_tuple = type->parent; + } else if (((mp_obj_base_t *)MP_OBJ_TYPE_GET_SLOT(type, parent))->type == &mp_type_tuple) { + const mp_obj_tuple_t *parent_tuple = MP_OBJ_TYPE_GET_SLOT(type, parent); size_t len = parent_tuple->len; const mp_obj_t *items = parent_tuple->items; for (size_t i = 0; i < len; i++) { @@ -1292,14 +1293,15 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // and we don't want to lookup native methods in object. continue; } + mp_obj_class_lookup(&lookup, (mp_obj_type_t *)MP_OBJ_TO_PTR(items[i])); if (dest[0] != MP_OBJ_NULL) { break; } } #endif - } else if (type->parent != &mp_type_object) { - mp_obj_class_lookup(&lookup, type->parent); + } else if (MP_OBJ_TYPE_GET_SLOT(type, parent) != &mp_type_object) { + mp_obj_class_lookup(&lookup, MP_OBJ_TYPE_GET_SLOT(type, parent)); } if (dest[0] != MP_OBJ_NULL) { @@ -1311,9 +1313,9 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { return; } - // Reset meth_offset so we don't look up any native methods in object, + // Reset slot_offset so we don't look up any native methods in object, // because object never takes up the native base-class slot. - lookup.meth_offset = 0; + lookup.slot_offset = 0; mp_obj_class_lookup(&lookup, &mp_type_object); } @@ -1352,13 +1354,13 @@ bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) { const mp_obj_type_t *self = MP_OBJ_TO_PTR(object); - if (self->parent == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(self, parent)) { // type has no parents return false; #if MICROPY_MULTIPLE_INHERITANCE - } else if (((mp_obj_base_t *)self->parent)->type == &mp_type_tuple) { + } else if (((mp_obj_base_t *)MP_OBJ_TYPE_GET_SLOT(self, parent))->type == &mp_type_tuple) { // get the base objects (they should be type objects) - const mp_obj_tuple_t *parent_tuple = self->parent; + const mp_obj_tuple_t *parent_tuple = MP_OBJ_TYPE_GET_SLOT(self, parent); const mp_obj_t *item = parent_tuple->items; const mp_obj_t *top = item + parent_tuple->len - 1; @@ -1374,7 +1376,7 @@ bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) { #endif } else { // type has 1 parent - object = MP_OBJ_FROM_PTR(self->parent); + object = MP_OBJ_FROM_PTR(MP_OBJ_TYPE_GET_SLOT(self, parent)); } } } diff --git a/py/opmethods.c b/py/opmethods.c index 595cc088ba933..c3931fd358466 100644 --- a/py/opmethods.c +++ b/py/opmethods.c @@ -29,24 +29,28 @@ STATIC mp_obj_t op_getitem(mp_obj_t self_in, mp_obj_t key_in) { const mp_obj_type_t *type = mp_obj_get_type(self_in); - return type->subscr(self_in, key_in, MP_OBJ_SENTINEL); + // Note: assumes type must have subscr (only used by dict). + return MP_OBJ_TYPE_GET_SLOT(type, subscr)(self_in, key_in, MP_OBJ_SENTINEL); } MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem); STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { const mp_obj_type_t *type = mp_obj_get_type(self_in); - return type->subscr(self_in, key_in, value_in); + // Note: assumes type must have subscr (only used by dict). + return MP_OBJ_TYPE_GET_SLOT(type, subscr)(self_in, key_in, value_in); } MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem); STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) { const mp_obj_type_t *type = mp_obj_get_type(self_in); - return type->subscr(self_in, key_in, MP_OBJ_NULL); + // Note: assumes type must have subscr (only used by dict). + return MP_OBJ_TYPE_GET_SLOT(type, subscr)(self_in, key_in, MP_OBJ_NULL); } MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem); STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) { const mp_obj_type_t *type = mp_obj_get_type(lhs_in); - return type->binary_op(MP_BINARY_OP_CONTAINS, lhs_in, rhs_in); + // Note: assumes type must have binary_op (only used by set/frozenset). + return MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_CONTAINS, lhs_in, rhs_in); } MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains); diff --git a/py/runtime.c b/py/runtime.c index 27b4f05f0460d..d0e504a3d096f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -307,8 +307,8 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { return MP_OBJ_NEW_SMALL_INT(h); } else { const mp_obj_type_t *type = mp_obj_get_type(arg); - if (type->unary_op != NULL) { - mp_obj_t result = type->unary_op(op, arg); + if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) { + mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(op, arg); if (result != MP_OBJ_NULL) { return result; } @@ -613,8 +613,8 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs const mp_obj_type_t *type; generic_binary_op: type = mp_obj_get_type(lhs); - if (type->binary_op != NULL) { - mp_obj_t result = type->binary_op(op, lhs, rhs); + if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op)) { + mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(op, lhs, rhs); if (result != MP_OBJ_NULL) { return result; } @@ -689,8 +689,8 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons const mp_obj_type_t *type = mp_obj_get_type(fun_in); // do the call - if (type->call != NULL) { - return type->call(fun_in, n_args, n_kw, args); + if (MP_OBJ_TYPE_HAS_SLOT(type, call)) { + return MP_OBJ_TYPE_GET_SLOT(type, call)(fun_in, n_args, n_kw, args); } #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE @@ -1167,14 +1167,14 @@ void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) { } #endif - if (attr == MP_QSTR___next__ && type->iternext != NULL) { + if (attr == MP_QSTR___next__ && MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj); dest[1] = obj; return; } - if (type->attr != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) { // this type can do its own load, so call it - type->attr(obj, attr, dest); + MP_OBJ_TYPE_GET_SLOT(type, attr)(obj, attr, dest); // If type->attr has set dest[1] = MP_OBJ_SENTINEL, we should proceed // with lookups below (i.e. in locals_dict). If not, return right away. if (dest[1] != MP_OBJ_SENTINEL) { @@ -1183,11 +1183,11 @@ void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) { // Clear the fail flag set by type->attr so it's like it never ran. dest[1] = MP_OBJ_NULL; } - if (type->locals_dict != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) { // generic method lookup // this is a lookup in the object (ie not class or type) - assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now - mp_map_t *locals_map = &type->locals_dict->map; + assert(MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->base.type == &mp_type_dict); // MicroPython restriction, for now + mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map; mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); if (elem != NULL) { mp_convert_member_lookup(obj, type, elem->value, dest); @@ -1239,9 +1239,9 @@ void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catc void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value); const mp_obj_type_t *type = mp_obj_get_type(base); - if (type->attr != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) { mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value}; - type->attr(base, attr, dest); + MP_OBJ_TYPE_GET_SLOT(type, attr)(base, attr, dest); if (dest[0] == MP_OBJ_NULL) { // success return; @@ -1260,20 +1260,21 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { assert(o_in); const mp_obj_type_t *type = mp_obj_get_type(o_in); - // Check for native getiter which is the identity. We handle this case explicitly - // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used. - if (type->getiter == mp_identity_getiter) { - return o_in; - } - // check for native getiter (corresponds to __iter__) - if (type->getiter != NULL) { - if (iter_buf == NULL && type->getiter != mp_obj_instance_getiter) { + if (MP_OBJ_TYPE_HAS_SLOT(type, getiter)) { + // Check for native getiter which is the identity. We handle this case explicitly + // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used. + if (MP_OBJ_TYPE_GET_SLOT(type, getiter) == mp_identity_getiter) { + return o_in; + } + + // check for native getiter (corresponds to __iter__) + if (iter_buf == NULL && MP_OBJ_TYPE_GET_SLOT(type, getiter) != mp_obj_instance_getiter) { // if caller did not provide a buffer then allocate one on the heap // mp_obj_instance_getiter is special, it will allocate only if needed iter_buf = m_new_obj(mp_obj_iter_buf_t); } - mp_obj_t iter = type->getiter(o_in, iter_buf); + mp_obj_t iter = MP_OBJ_TYPE_GET_SLOT(type, getiter)(o_in, iter_buf); if (iter != MP_OBJ_NULL) { return iter; } @@ -1305,9 +1306,9 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { // may also raise StopIteration() mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (type->iternext != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - return type->iternext(o_in); + return MP_OBJ_TYPE_GET_SLOT(type, iternext)(o_in); } else { // check for __next__ method mp_obj_t dest[2]; @@ -1331,9 +1332,9 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { mp_obj_t mp_iternext(mp_obj_t o_in) { MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (type->iternext != NULL) { + if (MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - return type->iternext(o_in); + return MP_OBJ_TYPE_GET_SLOT(type, iternext)(o_in); } else { // check for __next__ method mp_obj_t dest[2]; @@ -1371,9 +1372,9 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val); } - if (type->iternext != NULL && send_value == mp_const_none) { + if (MP_OBJ_TYPE_HAS_SLOT(type, iternext) && send_value == mp_const_none) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - mp_obj_t ret = type->iternext(self_in); + mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, iternext)(self_in); *ret_val = ret; if (ret != MP_OBJ_STOP_ITERATION) { return MP_VM_RETURN_YIELD; diff --git a/py/stream.c b/py/stream.c index 87bed38f6778f..36325e7d414a6 100644 --- a/py/stream.c +++ b/py/stream.c @@ -84,15 +84,16 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) { const mp_obj_type_t *type = mp_obj_get_type(self_in); - const mp_stream_p_t *stream_p = type->protocol; - if (stream_p == NULL - || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL) - || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL) - || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) { - // CPython: io.UnsupportedOperation, OSError subclass - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("stream operation not supported")); + if (MP_OBJ_TYPE_HAS_SLOT(type, protocol)) { + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(type, protocol); + if (!((flags & MP_STREAM_OP_READ) && stream_p->read == NULL) + && !((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL) + && !((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) { + return stream_p; + } } - return stream_p; + // CPython: io.UnsupportedOperation, OSError subclass + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("stream operation not supported")); } STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) { @@ -517,7 +518,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl); ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { mp_obj_base_t *o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t out_sz = stream_p->write(MP_OBJ_FROM_PTR(stream), buf, len, &errno); if (out_sz == MP_STREAM_ERROR) { return -1; @@ -528,7 +529,7 @@ ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) { mp_obj_base_t *o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t out_sz = stream_p->read(MP_OBJ_FROM_PTR(stream), buf, len, &errno); if (out_sz == MP_STREAM_ERROR) { return -1; @@ -539,7 +540,7 @@ ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) { off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { const mp_obj_base_t *o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); struct mp_stream_seek_t seek_s; seek_s.offset = offset; seek_s.whence = whence; @@ -552,7 +553,7 @@ off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { int mp_stream_posix_fsync(void *stream) { mp_obj_base_t *o = stream; - const mp_stream_p_t *stream_p = o->type->protocol; + const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol); mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_FLUSH, 0, &errno); if (res == MP_STREAM_ERROR) { return -1; diff --git a/py/stream.h b/py/stream.h index 45f36ef5a6f39..4bc329b3e790d 100644 --- a/py/stream.h +++ b/py/stream.h @@ -95,7 +95,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj); // Object is assumed to have a non-NULL stream protocol with valid r/w/ioctl methods static inline const mp_stream_p_t *mp_get_stream(mp_const_obj_t self) { - return (const mp_stream_p_t *)((const mp_obj_base_t *)MP_OBJ_TO_PTR(self))->type->protocol; + return (const mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(((const mp_obj_base_t *)MP_OBJ_TO_PTR(self))->type, protocol); } const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags); From 3ac8b5851e5f4dade465d52b91ed2ccc17851263 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 15 Jul 2021 15:04:40 +1000 Subject: [PATCH 2622/3533] py/obj: Add slot-index mp_obj_type_t representation. The existings mp_obj_type_t uses a sparse representation for slots for the capability methods of the type (eg print, make_new). This commit adds a compact slot-index representation. The basic idea is that where the mp_obj_type_t struct used to have 12 pointer fields, it now has 12 uint8_t indices, and a variable-length array of pointers. So in the best case (no fields used) it saves 12x4-12=36 bytes (on a 32-bit machine) and in the common case (three fields used) it saves 9x4-12=24 bytes. Overall with all associated changes, this slot-index representation reduces code size by 1000 to 3000 bytes on bare-metal ports. Performance is marginally better on a few tests (eg about 1% better on misc_pystone.py and misc_raytrace.py on PYBv1.1), but overall marginally worse by a percent or so. See issue #7542 for further analysis and discussion. Signed-off-by: Jim Mussared --- py/mpconfig.h | 7 +++ py/obj.h | 121 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 3 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 698d264d2e800..0045fa1985b05 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -145,6 +145,13 @@ #define MICROPY_OBJ_IMMEDIATE_OBJS (MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D) #endif +#define MICROPY_OBJ_TYPE_REPR_FULL (0) +#define MICROPY_OBJ_TYPE_REPR_SLOT_INDEX (1) + +#ifndef MICROPY_OBJ_TYPE_REPR +#define MICROPY_OBJ_TYPE_REPR (MICROPY_OBJ_TYPE_REPR_FULL) +#endif + /*****************************************************************************/ /* Memory allocation policy */ diff --git a/py/obj.h b/py/obj.h index 37e6f0052428c..c17e0e5f18992 100644 --- a/py/obj.h +++ b/py/obj.h @@ -575,12 +575,13 @@ struct _mp_obj_type_t { // The name of this type, a qstr. uint16_t name; - // Corresponds to __repr__ and __str__ special methods. - mp_print_fun_t print; - // Corresponds to __new__ and __init__ special methods, to make an instance of the type. mp_make_new_fun_t make_new; + #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL + // Corresponds to __repr__ and __str__ special methods. + mp_print_fun_t print; + // Corresponds to __call__ special method, ie T(...). mp_call_fun_t call; @@ -633,17 +634,87 @@ struct _mp_obj_type_t { // A dict mapping qstrs to objects local methods/constants/etc. struct _mp_obj_dict_t *locals_dict; + + #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX + + // Ideally these would be only 4 bits, but the extra overhead of + // accessing them adds more code, and we also need to be able to + // take the address of them for mp_obj_class_lookup. + uint8_t slot_index_print; + uint8_t slot_index_call; + uint8_t slot_index_unary_op; + uint8_t slot_index_binary_op; + uint8_t slot_index_attr; + uint8_t slot_index_subscr; + uint8_t slot_index_getiter; + uint8_t slot_index_iternext; + uint8_t slot_index_buffer; + uint8_t slot_index_protocol; + uint8_t slot_index_parent; + uint8_t slot_index_locals_dict; + + const void *slots[]; + + #endif }; // Non-variable sized versions of mp_obj_type_t to be used as a member // in other structs or for dynamic allocation. The fields are exactly // as in mp_obj_type_t, but with a fixed size for the flexible array // members. +#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL typedef mp_obj_type_t mp_obj_empty_type_t; typedef mp_obj_type_t mp_obj_full_type_t; +#else +typedef struct _mp_obj_empty_type_t { + mp_obj_base_t base; + uint16_t flags; + uint16_t name; + mp_make_new_fun_t make_new; + + uint8_t slot_index_print; + uint8_t slot_index_call; + uint8_t slot_index_unary_op; + uint8_t slot_index_binary_op; + uint8_t slot_index_attr; + uint8_t slot_index_subscr; + uint8_t slot_index_getiter; + uint8_t slot_index_iternext; + uint8_t slot_index_buffer; + uint8_t slot_index_protocol; + uint8_t slot_index_parent; + uint8_t slot_index_locals_dict; + + // No slots member. +} mp_obj_empty_type_t; + +typedef struct _mp_obj_full_type_t { + mp_obj_base_t base; + uint16_t flags; + uint16_t name; + mp_make_new_fun_t make_new; + + uint8_t slot_index_print; + uint8_t slot_index_call; + uint8_t slot_index_unary_op; + uint8_t slot_index_binary_op; + uint8_t slot_index_attr; + uint8_t slot_index_subscr; + uint8_t slot_index_getiter; + uint8_t slot_index_iternext; + uint8_t slot_index_buffer; + uint8_t slot_index_protocol; + uint8_t slot_index_parent; + uint8_t slot_index_locals_dict; + + // Explicitly add 12 slots. + const void *slots[12]; +} mp_obj_full_type_t; +#endif #define MP_TYPE_NULL_MAKE_NEW (NULL) +#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL // Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments. // Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new } @@ -670,6 +741,50 @@ typedef mp_obj_type_t mp_obj_full_type_t; #define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, f)) #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(void **)((char *)(t) + (offset)) != NULL) +#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX + +#define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_getiter (mp_getiter_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_iternext (mp_fun_1_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *) +#define _MP_OBJ_TYPE_SLOT_TYPE_parent (const void *) +#define _MP_OBJ_TYPE_SLOT_TYPE_locals_dict (struct _mp_obj_dict_t *) + +// Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments. +// Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE. +// Generated with: +// for i in range(13): +// print(f"#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_{i}(_struct_type, _typename, _name, _flags, _make_new{''.join(f', f{j+1}, v{j+1}' for j in range(i))}) const _struct_type _typename = {{ .base = {{ &mp_type_type }}, .name = _name, .flags = _flags, .make_new = _make_new{''.join(f', .slot_index_##f{j+1} = {j+1}' for j in range(i))}{', .slots = { ' + ''.join(f'v{j+1}, ' for j in range(i)) + '}' if i else '' } }}") +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, _make_new, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slots = { v1, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = { v1, v2, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = { v1, v2, v3, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = { v1, v2, v3, v4, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = { v1, v2, v3, v4, v5, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = { v1, v2, v3, v4, v5, v6, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = { v1, v2, v3, v4, v5, v6, v7, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } } + +#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f) +#define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1]) +#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL)) +#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(t)->slot_index_##f - 1] = (void *)v) +#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f)) +// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer. +#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0 || (offset == offsetof(mp_obj_type_t, make_new) && t->make_new)) + +#endif + // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x From cb0ffdd2bf25dcac3c230bdc1168d492aabaf573 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 22 Jun 2022 11:24:41 +1000 Subject: [PATCH 2623/3533] py/obj: Remove basic mp_obj_type_t sparse representation. This makes the slots-based representation the only option. Signed-off-by: Jim Mussared --- py/mpconfig.h | 7 ----- py/obj.h | 85 +++++++++++---------------------------------------- 2 files changed, 17 insertions(+), 75 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 0045fa1985b05..698d264d2e800 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -145,13 +145,6 @@ #define MICROPY_OBJ_IMMEDIATE_OBJS (MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D) #endif -#define MICROPY_OBJ_TYPE_REPR_FULL (0) -#define MICROPY_OBJ_TYPE_REPR_SLOT_INDEX (1) - -#ifndef MICROPY_OBJ_TYPE_REPR -#define MICROPY_OBJ_TYPE_REPR (MICROPY_OBJ_TYPE_REPR_FULL) -#endif - /*****************************************************************************/ /* Memory allocation policy */ diff --git a/py/obj.h b/py/obj.h index c17e0e5f18992..878fa673487a5 100644 --- a/py/obj.h +++ b/py/obj.h @@ -578,17 +578,22 @@ struct _mp_obj_type_t { // Corresponds to __new__ and __init__ special methods, to make an instance of the type. mp_make_new_fun_t make_new; - #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL + // Slots: For the rest of the fields, the slot index points to the + // relevant function in the variable-length "slots" field. Ideally these + // would be only 4 bits, but the extra overhead of accessing them adds + // more code, and we also need to be able to take the address of them for + // mp_obj_class_lookup. + // Corresponds to __repr__ and __str__ special methods. - mp_print_fun_t print; + uint8_t slot_index_print; // Corresponds to __call__ special method, ie T(...). - mp_call_fun_t call; + uint8_t slot_index_call; // Implements unary and binary operations. // Can return MP_OBJ_NULL if the operation is not supported. - mp_unary_op_fun_t unary_op; - mp_binary_op_fun_t binary_op; + uint8_t slot_index_unary_op; + uint8_t slot_index_binary_op; // Implements load, store and delete attribute. // @@ -602,70 +607,46 @@ struct _mp_obj_type_t { // dest[0,1] = {MP_OBJ_SENTINEL, object} means store // return: for fail, do nothing // for success set dest[0] = MP_OBJ_NULL - mp_attr_fun_t attr; + uint8_t slot_index_attr; // Implements load, store and delete subscripting: // - value = MP_OBJ_SENTINEL means load // - value = MP_OBJ_NULL means delete // - all other values mean store the value // Can return MP_OBJ_NULL if operation not supported. - mp_subscr_fun_t subscr; + uint8_t slot_index_subscr; // Corresponds to __iter__ special method. // Can use the given mp_obj_iter_buf_t to store iterator object, // otherwise can return a pointer to an object on the heap. - mp_getiter_fun_t getiter; + uint8_t slot_index_getiter; // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION // as an optimisation instead of raising StopIteration() with no args. - mp_fun_1_t iternext; + uint8_t slot_index_iternext; // Implements the buffer protocol if supported by this type. - mp_buffer_fun_t buffer; + uint8_t slot_index_buffer; // One of disjoint protocols (interfaces), like mp_stream_p_t, etc. - const void *protocol; + uint8_t slot_index_protocol; // A pointer to the parents of this type: // - 0 parents: pointer is NULL (object is implicitly the single parent) // - 1 parent: a pointer to the type of that parent // - 2 or more parents: pointer to a tuple object containing the parent types - const void *parent; + uint8_t slot_index_parent; // A dict mapping qstrs to objects local methods/constants/etc. - struct _mp_obj_dict_t *locals_dict; - - #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX - - // Ideally these would be only 4 bits, but the extra overhead of - // accessing them adds more code, and we also need to be able to - // take the address of them for mp_obj_class_lookup. - uint8_t slot_index_print; - uint8_t slot_index_call; - uint8_t slot_index_unary_op; - uint8_t slot_index_binary_op; - uint8_t slot_index_attr; - uint8_t slot_index_subscr; - uint8_t slot_index_getiter; - uint8_t slot_index_iternext; - uint8_t slot_index_buffer; - uint8_t slot_index_protocol; - uint8_t slot_index_parent; uint8_t slot_index_locals_dict; const void *slots[]; - - #endif }; // Non-variable sized versions of mp_obj_type_t to be used as a member // in other structs or for dynamic allocation. The fields are exactly // as in mp_obj_type_t, but with a fixed size for the flexible array // members. -#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL -typedef mp_obj_type_t mp_obj_empty_type_t; -typedef mp_obj_type_t mp_obj_full_type_t; -#else typedef struct _mp_obj_empty_type_t { mp_obj_base_t base; uint16_t flags; @@ -710,39 +691,9 @@ typedef struct _mp_obj_full_type_t { // Explicitly add 12 slots. const void *slots[12]; } mp_obj_full_type_t; -#endif #define MP_TYPE_NULL_MAKE_NEW (NULL) -#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL -// Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments. -// Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE. -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, _make_new, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11 } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11, .f12 = v12 } - -// Always safe, checks if the type can and does have this slot. -#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->f) -// Requires you know that this type can have this slot. -#define MP_OBJ_TYPE_GET_SLOT(t, f) ((t)->f) -// Always safe, returns NULL if the type cannot have this slot. -#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) ((t)->f) -#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->f = v) -#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, f)) -#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(void **)((char *)(t) + (offset)) != NULL) - -#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX - #define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t) @@ -783,8 +734,6 @@ typedef struct _mp_obj_full_type_t { // For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer. #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0 || (offset == offsetof(mp_obj_type_t, make_new) && t->make_new)) -#endif - // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x From 165388e4eb5db1207f4d839abe77d417d4c3f7c3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Sep 2022 23:56:52 +1000 Subject: [PATCH 2624/3533] py/objtype: Optimise slot RAM usage for instance types. In all cases other than where you have a native base with a protocol, it now fits into 4 GC blocks (like it did before the slots representation). Signed-off-by: Jim Mussared --- py/misc.h | 1 + py/objnamedtuple.c | 3 +-- py/objtype.c | 33 ++++++++++++++++++++------------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/py/misc.h b/py/misc.h index 134325f8946ff..7350d2f9b0c08 100644 --- a/py/misc.h +++ b/py/misc.h @@ -72,6 +72,7 @@ typedef unsigned int uint; #define m_new_obj(type) (m_new(type, 1)) #define m_new_obj_maybe(type) (m_new_maybe(type, 1)) #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type *)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num))) +#define m_new_obj_var0(obj_type, var_type, var_num) ((obj_type *)m_malloc0(sizeof(obj_type) + sizeof(var_type) * (var_num))) #define m_new_obj_var_maybe(obj_type, var_type, var_num) ((obj_type *)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num))) #if MICROPY_ENABLE_FINALISER #define m_new_obj_with_finaliser(type) ((type *)(m_malloc_with_finaliser(sizeof(type)))) diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index cc55aec0b97c0..5666521617744 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -143,8 +143,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, } mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields) { - mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields); - memset(&o->base, 0, sizeof(o->base)); + mp_obj_namedtuple_type_t *o = m_new_obj_var0(mp_obj_namedtuple_type_t, qstr, n_fields); o->n_fields = n_fields; for (size_t i = 0; i < n_fields; i++) { o->fields[i] = mp_obj_str_get_qstr(fields[i]); diff --git a/py/objtype.c b/py/objtype.c index 968d6eb3ac32f..5b4e375bcc481 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -1148,10 +1148,15 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) #endif } - // Allocate a full-sized mp_obj_full_type_t instance (i.e. all slots / extended fields). - // Given that Python types use almost all the slots anyway, this doesn't cost anything - // extra. - mp_obj_type_t *o = (mp_obj_type_t *)m_new0(mp_obj_full_type_t, 1); + const void *base_protocol = NULL; + if (bases_len > 0) { + base_protocol = MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0])), protocol); + } + + // Allocate a variable-sized mp_obj_type_t with as many slots as we need + // (currently 9, plus 1 for base, plus 1 for base-protocol). + // Note: 11 slots pushes it from 4 to 5 GC blocks. + mp_obj_type_t *o = m_new_obj_var0(mp_obj_type_t, void *, 9 + (bases_len ? 1 : 0) + (base_protocol ? 1 : 0)); o->base.type = &mp_type_type; o->flags = base_flags; o->name = name; @@ -1166,13 +1171,10 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) // MP_OBJ_TYPE_SET_SLOT(o, iternext, not implemented) MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 7); - if (bases_len > 0) { - // Inherit protocol from a base class. This allows to define an - // abstract base class which would translate C-level protocol to - // Python method calls, and any subclass inheriting from it will - // support this feature. - MP_OBJ_TYPE_SET_SLOT(o, protocol, MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0])), protocol), 8); + mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict); + MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 8); + if (bases_len > 0) { if (bases_len >= 2) { #if MICROPY_MULTIPLE_INHERITANCE MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_tuple), 9); @@ -1182,10 +1184,15 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) } else { MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_items[0]), 9); } - } - mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict); - MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 10); + // Inherit protocol from a base class. This allows to define an + // abstract base class which would translate C-level protocol to + // Python method calls, and any subclass inheriting from it will + // support this feature. + if (base_protocol) { + MP_OBJ_TYPE_SET_SLOT(o, protocol, base_protocol, 10); + } + } #if ENABLE_SPECIAL_ACCESSORS // Check if the class has any special accessor methods From 3c6127dfcfc63a2b48c31f751d1ae2c385874c8a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 14 Sep 2022 00:39:48 +1000 Subject: [PATCH 2625/3533] py/objnamedtuple: Optimise slot RAM usage for namedtuple. Rather than reserving a full 12-slot mp_obj_type_t, reserve enough room for seven and cast as necessary. Signed-off-by: Jim Mussared --- py/objnamedtuple.c | 25 +++++++++++++------------ py/objnamedtuple.h | 7 +++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 5666521617744..52536be8b4e97 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -104,7 +104,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("%q() takes %d positional arguments but %d were given"), - type->base.name, num_fields, n_args + n_kw); + ((mp_obj_type_t *)&type->base)->name, num_fields, n_args + n_kw); #endif } @@ -153,17 +153,18 @@ mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t * STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t *fields) { mp_obj_namedtuple_type_t *o = mp_obj_new_namedtuple_base(n_fields, fields); - o->base.base.type = &mp_type_type; - o->base.flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE; // can match tuple - o->base.name = name; - o->base.make_new = namedtuple_make_new; - MP_OBJ_TYPE_SET_SLOT(&o->base, print, namedtuple_print, 0); - MP_OBJ_TYPE_SET_SLOT(&o->base, unary_op, mp_obj_tuple_unary_op, 1); - MP_OBJ_TYPE_SET_SLOT(&o->base, binary_op, mp_obj_tuple_binary_op, 2); - MP_OBJ_TYPE_SET_SLOT(&o->base, attr, namedtuple_attr, 3); - MP_OBJ_TYPE_SET_SLOT(&o->base, subscr, mp_obj_tuple_subscr, 4); - MP_OBJ_TYPE_SET_SLOT(&o->base, getiter, mp_obj_tuple_getiter, 5); - MP_OBJ_TYPE_SET_SLOT(&o->base, parent, &mp_type_tuple, 6); + mp_obj_type_t *type = (mp_obj_type_t *)&o->base; + type->base.type = &mp_type_type; + type->flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE; // can match tuple + type->name = name; + type->make_new = namedtuple_make_new; + MP_OBJ_TYPE_SET_SLOT(type, print, namedtuple_print, 0); + MP_OBJ_TYPE_SET_SLOT(type, unary_op, mp_obj_tuple_unary_op, 1); + MP_OBJ_TYPE_SET_SLOT(type, binary_op, mp_obj_tuple_binary_op, 2); + MP_OBJ_TYPE_SET_SLOT(type, attr, namedtuple_attr, 3); + MP_OBJ_TYPE_SET_SLOT(type, subscr, mp_obj_tuple_subscr, 4); + MP_OBJ_TYPE_SET_SLOT(type, getiter, mp_obj_tuple_getiter, 5); + MP_OBJ_TYPE_SET_SLOT(type, parent, &mp_type_tuple, 6); return MP_OBJ_FROM_PTR(o); } diff --git a/py/objnamedtuple.h b/py/objnamedtuple.h index 9f23351d5a36c..db4a3d87d87fd 100644 --- a/py/objnamedtuple.h +++ b/py/objnamedtuple.h @@ -29,10 +29,9 @@ #include "py/objtuple.h" typedef struct _mp_obj_namedtuple_type_t { - // Must use the full-size version to avoid this being a variable sized member. - // This means that named tuples use slightly more RAM than necessary, but - // no worse than if we didn't have slots/split representation. - mp_obj_full_type_t base; + // This is a mp_obj_type_t with seven slots. + mp_obj_empty_type_t base; + void *slots[7]; size_t n_fields; qstr fields[]; } mp_obj_namedtuple_type_t; From 6da41b59007c9e9a2443ae17278d32210034a63f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 16 Sep 2022 23:57:38 +1000 Subject: [PATCH 2626/3533] py/obj: Merge getiter and iternext mp_obj_type_t slots. The goal here is to remove a slot (making way to turn make_new into a slot) as well as reduce code size by the ~40 references to mp_identity_getiter and mp_stream_unbuffered_iter. This introduces two new type flags: - MP_TYPE_FLAG_ITER_IS_ITERNEXT: This means that the "iter" slot in the type is "iternext", and should use the identity getiter. - MP_TYPE_FLAG_ITER_IS_CUSTOM: This means that the "iter" slot is a pointer to a mp_getiter_iternext_custom_t instance, which then defines both getiter and iternext. And a third flag that is the OR of both, MP_TYPE_FLAG_ITER_IS_STREAM: This means that the type should use the identity getiter, and mp_stream_unbuffered_iter as iternext. Finally, MP_TYPE_FLAG_ITER_IS_GETITER is defined as a no-op flag to give the default case where "iter" is "getiter". Signed-off-by: Jim Mussared --- examples/natmod/btree/btree_c.c | 14 +++-- extmod/modbtree.c | 10 +++- extmod/moduasyncio.c | 10 +++- extmod/moduselect.c | 5 +- extmod/modussl_axtls.c | 2 - extmod/modussl_mbedtls.c | 2 - extmod/vfs_fat_file.c | 8 +-- extmod/vfs_lfsx_file.c | 8 +-- extmod/vfs_posix_file.c | 8 +-- ports/cc3200/mods/modussl.c | 3 - ports/cc3200/mods/pybuart.c | 4 +- ports/esp32/machine_i2s.c | 4 +- ports/esp32/machine_uart.c | 4 +- ports/esp8266/machine_uart.c | 4 +- ports/mimxrt/machine_i2s.c | 4 +- ports/mimxrt/machine_uart.c | 4 +- ports/nrf/boards/microbit/modules/iters.c | 5 +- .../boards/microbit/modules/microbitimage.c | 20 +++---- ports/nrf/modules/machine/uart.c | 4 +- ports/renesas-ra/machine_uart.c | 4 +- ports/rp2/machine_i2s.c | 4 +- ports/rp2/machine_uart.c | 4 +- ports/stm32/machine_i2s.c | 4 +- ports/stm32/machine_uart.c | 4 +- ports/stm32/usb.c | 4 +- ports/unix/modjni.c | 4 +- ports/unix/moduselect.c | 5 +- ports/unix/modusocket.c | 2 - ports/zephyr/machine_uart.c | 4 +- py/obj.c | 8 +-- py/obj.h | 56 ++++++++++++------- py/objarray.c | 17 +++--- py/objattrtuple.c | 4 +- py/objdict.c | 17 +++--- py/objenumerate.c | 5 +- py/objfilter.c | 5 +- py/objgenerator.c | 5 +- py/objgetitemiter.c | 5 +- py/objlist.c | 4 +- py/objmap.c | 5 +- py/objnamedtuple.c | 2 +- py/objpolyiter.c | 10 ++-- py/objrange.c | 7 +-- py/objreversed.c | 5 +- py/objset.c | 8 +-- py/objstr.c | 4 +- py/objstringio.c | 8 +-- py/objstrunicode.c | 4 +- py/objtuple.c | 8 +-- py/objtype.c | 23 +++++--- py/objzip.c | 5 +- py/runtime.c | 52 +++++++++++------ shared/runtime/sys_stdio_mphal.c | 8 +-- tests/basics/io_stringio_base.py | 24 ++++++++ 54 files changed, 234 insertions(+), 227 deletions(-) create mode 100644 tests/basics/io_stringio_base.py diff --git a/examples/natmod/btree/btree_c.c b/examples/natmod/btree/btree_c.c index af7535a68f7f7..c897f2e9a3859 100644 --- a/examples/natmod/btree/btree_c.c +++ b/examples/natmod/btree/btree_c.c @@ -95,6 +95,7 @@ int mp_stream_posix_fsync(void *stream) { } mp_obj_full_type_t btree_type; +mp_getiter_iternext_custom_t btree_getiter_iternext; #include "extmod/modbtree.c" @@ -122,13 +123,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_open_obj, 5, 5, btree_open); mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) { MP_DYNRUNTIME_INIT_ENTRY + btree_getiter_iternext.getiter = btree_getiter; + btree_getiter_iternext.iternext = btree_iternext; + btree_type.base.type = (void*)&mp_fun_table.type_type; + btree_type.flags = MP_TYPE_FLAG_ITER_IS_CUSTOM; btree_type.name = MP_QSTR_btree; MP_OBJ_TYPE_SET_SLOT(&btree_type, print, btree_print, 0); - MP_OBJ_TYPE_SET_SLOT(&btree_type, getiter, btree_getiter, 1); - MP_OBJ_TYPE_SET_SLOT(&btree_type, iternext, btree_iternext, 2); - MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 3); - MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 4); + MP_OBJ_TYPE_SET_SLOT(&btree_type, iter, &btree_getiter_iternext, 1); + MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 2); + MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 3); btree_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&btree_close_obj) }; btree_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_flush), MP_OBJ_FROM_PTR(&btree_flush_obj) }; btree_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get), MP_OBJ_FROM_PTR(&btree_get_obj) }; @@ -137,7 +141,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a btree_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_keys), MP_OBJ_FROM_PTR(&btree_keys_obj) }; btree_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_values), MP_OBJ_FROM_PTR(&btree_values_obj) }; btree_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_items), MP_OBJ_FROM_PTR(&btree_items_obj) }; - MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 5); + MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 4); mp_store_global(MP_QSTR__open, MP_OBJ_FROM_PTR(&btree_open_obj)); mp_store_global(MP_QSTR_INCL, MP_OBJ_NEW_SMALL_INT(FLAG_END_KEY_INCL)); diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 60c6885e6b2cc..f21fe3ff94173 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -319,15 +319,19 @@ STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table); +STATIC const mp_getiter_iternext_custom_t btree_getiter_iternext = { + .getiter = btree_getiter, + .iternext = btree_iternext, +}; + STATIC MP_DEFINE_CONST_OBJ_TYPE( btree_type, MP_QSTR_btree, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_CUSTOM, MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, btree_print, - getiter, btree_getiter, - iternext, btree_iternext, + iter, &btree_getiter_iternext, binary_op, btree_binary_op, subscr, btree_subscr, locals_dict, &btree_locals_dict diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index 546764209608a..b0eb8b6509c82 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -287,14 +287,18 @@ STATIC mp_obj_t task_iternext(mp_obj_t self_in) { return mp_const_none; } +STATIC const mp_getiter_iternext_custom_t task_getiter_iternext = { + .getiter = task_getiter, + .iternext = task_iternext, +}; + STATIC MP_DEFINE_CONST_OBJ_TYPE( task_type, MP_QSTR_Task, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_CUSTOM, task_make_new, attr, task_attr, - getiter, task_getiter, - iternext, task_iternext + iter, &task_getiter_iternext ); /******************************************************************************/ diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 352b15d983d84..58bd1169a945e 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -339,10 +339,9 @@ STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_poll, MP_QSTR_poll, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, poll_iternext, + iter, poll_iternext, locals_dict, &poll_locals_dict ); diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index 78470ea6df046..a6d606d560ebe 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -321,8 +321,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, ussl_socket_print, - getiter, NULL, - iternext, NULL, protocol, &ussl_socket_stream_p, locals_dict, &ussl_socket_locals_dict ); diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 76ca3ac719c91..50712980ba04f 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -399,8 +399,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, socket_print, - getiter, NULL, - iternext, NULL, protocol, &ussl_socket_stream_p, locals_dict, &ussl_socket_locals_dict ); diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 00980459db8b9..ca2e2b446f13d 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -179,11 +179,9 @@ STATIC const mp_stream_p_t vfs_fat_fileio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_fat_fileio, MP_QSTR_FileIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, file_obj_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &vfs_fat_fileio_stream_p, locals_dict, &vfs_fat_rawfile_locals_dict ); @@ -198,11 +196,9 @@ STATIC const mp_stream_p_t vfs_fat_textio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_fat_textio, MP_QSTR_TextIOWrapper, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, file_obj_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &vfs_fat_textio_stream_p, locals_dict, &vfs_fat_rawfile_locals_dict ); diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index fda1b97b2a13c..f97641b7b951a 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -223,11 +223,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = { MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx_(_fileio), MP_QSTR_FileIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, MP_VFS_LFSx(file_print), - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &MP_VFS_LFSx(fileio_stream_p), locals_dict, &MP_VFS_LFSx(file_locals_dict) ); @@ -242,11 +240,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = { MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx_(_textio), MP_QSTR_TextIOWrapper, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, MP_VFS_LFSx(file_print), - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &MP_VFS_LFSx(textio_stream_p), locals_dict, &MP_VFS_LFSx(file_locals_dict) ); diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index f0b5436fe1ea4..729d914d3a11a 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -252,11 +252,9 @@ STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_posix_fileio, MP_QSTR_FileIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, vfs_posix_file_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &vfs_posix_fileio_stream_p, locals_dict, &vfs_posix_rawfile_locals_dict ); @@ -271,11 +269,9 @@ STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_posix_textio, MP_QSTR_TextIOWrapper, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, vfs_posix_file_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &vfs_posix_textio_stream_p, locals_dict, &vfs_posix_rawfile_locals_dict ); diff --git a/ports/cc3200/mods/modussl.c b/ports/cc3200/mods/modussl.c index abc9917c8134c..118cbd06f8fad 100644 --- a/ports/cc3200/mods/modussl.c +++ b/ports/cc3200/mods/modussl.c @@ -64,9 +64,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ssl_socket_type, MP_QSTR_ussl, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, - getiter, NULL, - iternext, NULL, protocol, &socket_stream_p, locals_dict, &socket_locals_dict ); diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index e7896c4ca5cfa..f92f544732209 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -688,11 +688,9 @@ STATIC const mp_irq_methods_t uart_irq_methods = { MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, pyb_uart_make_new, print, pyb_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index b853f418ada3c..6e18394cc5e56 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -832,11 +832,9 @@ STATIC const mp_stream_p_t i2s_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_i2s_make_new, print, machine_i2s_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict ); diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 6e091b8838a23..1f404154fc5ba 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -533,11 +533,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_uart_make_new, print, machine_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict ); diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c index c737f854c34f3..af6231c21903b 100644 --- a/ports/esp8266/machine_uart.c +++ b/ports/esp8266/machine_uart.c @@ -346,11 +346,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, pyb_uart_make_new, print, pyb_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/mimxrt/machine_i2s.c b/ports/mimxrt/machine_i2s.c index 13380b4ee6b09..b6c630138b76b 100644 --- a/ports/mimxrt/machine_i2s.c +++ b/ports/mimxrt/machine_i2s.c @@ -1216,11 +1216,9 @@ STATIC const mp_stream_p_t i2s_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_i2s_make_new, print, machine_i2s_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict ); diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index 9d4873274a8fe..e93d2478f3ff6 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -472,11 +472,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_uart_make_new, print, machine_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict ); diff --git a/ports/nrf/boards/microbit/modules/iters.c b/ports/nrf/boards/microbit/modules/iters.c index 296fc1f51c525..2fe14866914fd 100644 --- a/ports/nrf/boards/microbit/modules/iters.c +++ b/ports/nrf/boards/microbit/modules/iters.c @@ -46,10 +46,9 @@ static mp_obj_t microbit_repeat_iter_next(mp_obj_t iter_in) { MP_DEFINE_CONST_OBJ_TYPE( microbit_repeat_iterator_type, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, microbit_repeat_iter_next + iter, microbit_repeat_iter_next ); mp_obj_t microbit_repeat_iterator(mp_obj_t iterable) { diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index 4870b6738f6c0..b22c2e29acec9 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -824,18 +824,16 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { MP_DEFINE_CONST_OBJ_TYPE( microbit_scrolling_string_type, MP_QSTR_ScrollingString, - MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, - getiter, get_microbit_scrolling_string_iter + MP_TYPE_FLAG_ITER_IS_GETITER, + iter, get_microbit_scrolling_string_iter ); MP_DEFINE_CONST_OBJ_TYPE( microbit_scrolling_string_iterator_type, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, microbit_scrolling_string_iter_next + iter, microbit_scrolling_string_iter_next ); /** Facade types to present a string as a sequence of images. @@ -877,11 +875,10 @@ static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t MP_DEFINE_CONST_OBJ_TYPE( string_image_facade_type, MP_QSTR_Facade, - MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, + MP_TYPE_FLAG_ITER_IS_GETITER, unary_op, facade_unary_op, subscr, string_image_facade_subscr, - getiter, microbit_facade_iterator + iter, microbit_facade_iterator ); @@ -914,10 +911,9 @@ static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) { MP_DEFINE_CONST_OBJ_TYPE( microbit_facade_iterator_type, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, microbit_facade_iter_next + iter, microbit_facade_iter_next ); mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) { diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index fc0bd682b4b7d..ca0fcf859a55a 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -373,11 +373,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_hard_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_hard_uart_make_new, print, machine_hard_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &machine_hard_uart_locals_dict ); diff --git a/ports/renesas-ra/machine_uart.c b/ports/renesas-ra/machine_uart.c index 11f5d6825eb2d..6fa84ca821e4e 100644 --- a/ports/renesas-ra/machine_uart.c +++ b/ports/renesas-ra/machine_uart.c @@ -574,12 +574,10 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_uart_make_new, locals_dict, &machine_uart_locals_dict, print, machine_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p ); diff --git a/ports/rp2/machine_i2s.c b/ports/rp2/machine_i2s.c index 9d70a476f30b1..53ff7417d79a7 100644 --- a/ports/rp2/machine_i2s.c +++ b/ports/rp2/machine_i2s.c @@ -1140,11 +1140,9 @@ STATIC const mp_stream_p_t i2s_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_i2s_make_new, print, machine_i2s_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict ); diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index bb8bf51be6511..06f7e9aaaca2c 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -582,11 +582,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_uart_make_new, print, machine_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict ); diff --git a/ports/stm32/machine_i2s.c b/ports/stm32/machine_i2s.c index 93a465d07ca8a..d68648bc348a5 100644 --- a/ports/stm32/machine_i2s.c +++ b/ports/stm32/machine_i2s.c @@ -1117,11 +1117,9 @@ STATIC const mp_stream_p_t i2s_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_i2s_make_new, print, machine_i2s_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict ); diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 5851d8cf352df..4ccff8c136ada 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -663,11 +663,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, pyb_uart_make_new, print, pyb_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index df755fe984c30..e389ef68f2aae 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -939,11 +939,9 @@ STATIC const mp_stream_p_t pyb_usb_vcp_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( pyb_usb_vcp_type, MP_QSTR_USB_VCP, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, pyb_usb_vcp_make_new, print, pyb_usb_vcp_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &pyb_usb_vcp_stream_p, locals_dict, &pyb_usb_vcp_locals_dict ); diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index 02368e453716b..e6b874b235ecb 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -325,13 +325,13 @@ STATIC mp_obj_t subscr_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { STATIC MP_DEFINE_CONST_OBJ_TYPE( jobject_type, MP_QSTR_jobject, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, MP_TYPE_NULL_MAKE_NEW, print, jobject_print, unary_op, jobject_unary_op, attr, jobject_attr, subscr, jobject_subscr, - getiter, subscr_getiter, + iter, subscr_getiter, // .locals_dict = &jobject_locals_dict, ); diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index baed88761c463..674841bf1871d 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -314,10 +314,9 @@ STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_poll, MP_QSTR_poll, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, poll_iternext, + iter, poll_iternext, locals_dict, &poll_locals_dict ); diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 21260f0b2ada5..dfbf15cd3efd3 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -522,8 +522,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, socket_make_new, print, socket_print, - getiter, NULL, - iternext, NULL, protocol, &usocket_stream_p, locals_dict, &usocket_locals_dict ); diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 9580d37907812..867c5ae8863a0 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -157,11 +157,9 @@ STATIC const mp_stream_p_t uart_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, machine_uart_make_new, print, machine_uart_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict ); diff --git a/py/obj.c b/py/obj.c index 359c73b9c2120..eb17308fed884 100644 --- a/py/obj.c +++ b/py/obj.c @@ -572,10 +572,10 @@ mp_obj_t mp_identity(mp_obj_t self) { } MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity); -mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) { - (void)iter_buf; - return self; -} +// mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) { +// (void)iter_buf; +// return self; +// } bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { const mp_obj_type_t *type = mp_obj_get_type(obj); diff --git a/py/obj.h b/py/obj.h index 878fa673487a5..b1d722080c665 100644 --- a/py/obj.h +++ b/py/obj.h @@ -507,12 +507,20 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); // Flags for type behaviour (mp_obj_type_t.flags) // If MP_TYPE_FLAG_EQ_NOT_REFLEXIVE is clear then __eq__ is reflexive (A==A returns True). // If MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE is clear then the type can't be equal to an -// instance of any different class that also clears this flag. If this flag is set -// then the type may check for equality against a different type. +// instance of any different class that also clears this flag. If this flag is set +// then the type may check for equality against a different type. // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__ -// operator and not the __ne__ operator. If it's set then __ne__ may be implemented. +// operator and not the __ne__ operator. If it's set then __ne__ may be implemented. // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg. // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type. +// MP_TYPE_FLAG_ITER_IS_GETITER is a no-op flag that means the default behaviour for the +// iter slot and it's the getiter function. +// If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set then the "iter" slot is the iternext +// function and getiter will be automatically implemented as "return self". +// If MP_TYPE_FLAG_ITER_IS_CUSTOM is set then the "iter" slot is a pointer to a +// mp_getiter_iternext_custom_t struct instance (with both .getiter and .iternext set). +// If MP_TYPE_FLAG_ITER_IS_STREAM is set then the type implicitly gets a "return self" +// getiter, and mp_stream_unbuffered_iter for iternext. #define MP_TYPE_FLAG_NONE (0x0000) #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001) #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002) @@ -521,6 +529,10 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010) #define MP_TYPE_FLAG_BINDS_SELF (0x0020) #define MP_TYPE_FLAG_BUILTIN_FUN (0x0040) +#define MP_TYPE_FLAG_ITER_IS_GETITER (0x0000) +#define MP_TYPE_FLAG_ITER_IS_ITERNEXT (0x0080) +#define MP_TYPE_FLAG_ITER_IS_CUSTOM (0x0100) +#define MP_TYPE_FLAG_ITER_IS_STREAM (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM) typedef enum { PRINT_STR = 0, @@ -548,6 +560,13 @@ typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t); typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf); +typedef mp_fun_1_t mp_iternext_fun_t; + +// For MP_TYPE_FLAG_ITER_IS_CUSTOM, the "getiter" slot points to an instance of this type. +typedef struct _mp_getiter_iternext_custom_t { + mp_getiter_fun_t getiter; + mp_iternext_fun_t iternext; +} mp_getiter_iternext_custom_t; // Buffer protocol typedef struct _mp_buffer_info_t { @@ -616,14 +635,17 @@ struct _mp_obj_type_t { // Can return MP_OBJ_NULL if operation not supported. uint8_t slot_index_subscr; - // Corresponds to __iter__ special method. - // Can use the given mp_obj_iter_buf_t to store iterator object, - // otherwise can return a pointer to an object on the heap. - uint8_t slot_index_getiter; - - // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION - // as an optimisation instead of raising StopIteration() with no args. - uint8_t slot_index_iternext; + // This slot's behaviour depends on the MP_TYPE_FLAG_ITER_IS_* flags above. + // - If MP_TYPE_FLAG_ITER_IS_GETITER flag is set, then this corresponds to the __iter__ + // special method (of type mp_getiter_fun_t). Can use the given mp_obj_iter_buf_t + // to store the iterator object, otherwise can return a pointer to an object on the heap. + // - If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set, then this corresponds to __next__ special method. + // May return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() + // with no args. The type will implicitly implement getiter as "return self". + // - If MP_TYPE_FLAG_ITER_IS_CUSTOM is set, then this slot must point to an + // mp_getiter_iternext_custom_t instance with both the getiter and iternext fields set. + // - If MP_TYPE_FLAG_ITER_IS_STREAM is set, this this slot should be unset. + uint8_t slot_index_iter; // Implements the buffer protocol if supported by this type. uint8_t slot_index_buffer; @@ -659,8 +681,7 @@ typedef struct _mp_obj_empty_type_t { uint8_t slot_index_binary_op; uint8_t slot_index_attr; uint8_t slot_index_subscr; - uint8_t slot_index_getiter; - uint8_t slot_index_iternext; + uint8_t slot_index_iter; uint8_t slot_index_buffer; uint8_t slot_index_protocol; uint8_t slot_index_parent; @@ -681,15 +702,14 @@ typedef struct _mp_obj_full_type_t { uint8_t slot_index_binary_op; uint8_t slot_index_attr; uint8_t slot_index_subscr; - uint8_t slot_index_getiter; - uint8_t slot_index_iternext; + uint8_t slot_index_iter; uint8_t slot_index_buffer; uint8_t slot_index_protocol; uint8_t slot_index_parent; uint8_t slot_index_locals_dict; // Explicitly add 12 slots. - const void *slots[12]; + const void *slots[11]; } mp_obj_full_type_t; #define MP_TYPE_NULL_MAKE_NEW (NULL) @@ -700,8 +720,7 @@ typedef struct _mp_obj_full_type_t { #define _MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t) -#define _MP_OBJ_TYPE_SLOT_TYPE_getiter (mp_getiter_fun_t) -#define _MP_OBJ_TYPE_SLOT_TYPE_iternext (mp_fun_1_t) +#define _MP_OBJ_TYPE_SLOT_TYPE_iter (const void *) #define _MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *) #define _MP_OBJ_TYPE_SLOT_TYPE_parent (const void *) @@ -1162,7 +1181,6 @@ qstr mp_obj_fun_get_name(mp_const_obj_t fun); mp_obj_t mp_identity(mp_obj_t self); MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj); -mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf); // module typedef struct _mp_obj_module_t { diff --git a/py/objarray.c b/py/objarray.c index 762a4105c5f56..0d1032929f48c 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -574,10 +574,10 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui MP_DEFINE_CONST_OBJ_TYPE( mp_type_array, MP_QSTR_array, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, array_make_new, print, array_print, - getiter, array_iterator_new, + iter, array_iterator_new, unary_op, array_unary_op, binary_op, array_binary_op, subscr, array_subscr, @@ -590,10 +590,10 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_DEFINE_CONST_OBJ_TYPE( mp_type_bytearray, MP_QSTR_bytearray, - MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, bytearray_make_new, print, array_print, - getiter, array_iterator_new, + iter, array_iterator_new, unary_op, array_unary_op, binary_op, array_binary_op, subscr, array_subscr, @@ -618,9 +618,9 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_DEFINE_CONST_OBJ_TYPE( mp_type_memoryview, MP_QSTR_memoryview, - MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, memoryview_make_new, - getiter, array_iterator_new, + iter, array_iterator_new, unary_op, array_unary_op, binary_op, array_binary_op, MEMORYVIEW_TYPE_LOCALS_DICT @@ -676,10 +676,9 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_array_it, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, array_it_iternext + iter, array_it_iternext ); STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) { diff --git a/py/objattrtuple.c b/py/objattrtuple.c index 0d41ee52354a4..2e207f4cf0c46 100644 --- a/py/objattrtuple.c +++ b/py/objattrtuple.c @@ -83,7 +83,7 @@ mp_obj_t mp_obj_new_attrtuple(const qstr *fields, size_t n, const mp_obj_t *item MP_DEFINE_CONST_OBJ_TYPE( mp_type_attrtuple, MP_QSTR_tuple, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, MP_TYPE_NULL_MAKE_NEW, // reuse tuple to save on a qstr print, mp_obj_attrtuple_print, @@ -91,7 +91,7 @@ MP_DEFINE_CONST_OBJ_TYPE( binary_op, mp_obj_tuple_binary_op, attr, mp_obj_attrtuple_attr, subscr, mp_obj_tuple_subscr, - getiter, mp_obj_tuple_getiter + iter, mp_obj_tuple_getiter ); #endif // MICROPY_PY_ATTRTUPLE diff --git a/py/objdict.c b/py/objdict.c index c65b14caad6ca..7755d7b786d4c 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -464,10 +464,9 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict_view_it, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, dict_view_it_iternext + iter, dict_view_it_iternext ); STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) { @@ -517,11 +516,11 @@ STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict_view, MP_QSTR_dict_view, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, MP_TYPE_NULL_MAKE_NEW, print, dict_view_print, binary_op, dict_view_binary_op, - getiter, dict_view_getiter + iter, dict_view_getiter ); STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) { @@ -592,13 +591,13 @@ STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict, MP_QSTR_dict, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, mp_obj_dict_make_new, print, dict_print, unary_op, dict_unary_op, binary_op, dict_binary_op, subscr, dict_subscr, - getiter, dict_getiter, + iter, dict_getiter, locals_dict, &dict_locals_dict ); @@ -606,13 +605,13 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_DEFINE_CONST_OBJ_TYPE( mp_type_ordereddict, MP_QSTR_OrderedDict, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, mp_obj_dict_make_new, print, dict_print, unary_op, dict_unary_op, binary_op, dict_binary_op, subscr, dict_subscr, - getiter, dict_getiter, + iter, dict_getiter, parent, &mp_type_dict, locals_dict, &dict_locals_dict ); diff --git a/py/objenumerate.c b/py/objenumerate.c index f4f4ff6ae1d93..eea9e3e381ed4 100644 --- a/py/objenumerate.c +++ b/py/objenumerate.c @@ -70,10 +70,9 @@ STATIC mp_obj_t enumerate_make_new(const mp_obj_type_t *type, size_t n_args, siz MP_DEFINE_CONST_OBJ_TYPE( mp_type_enumerate, MP_QSTR_enumerate, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, enumerate_make_new, - iternext, enumerate_iternext, - getiter, mp_identity_getiter + iter, enumerate_iternext ); STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) { diff --git a/py/objfilter.c b/py/objfilter.c index 2b57300af3df5..bfe651f40d036 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -63,10 +63,9 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_filter, MP_QSTR_filter, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, filter_make_new, - getiter, mp_identity_getiter, - iternext, filter_iternext + iter, filter_iternext ); #endif // MICROPY_PY_BUILTINS_FILTER diff --git a/py/objgenerator.c b/py/objgenerator.c index a960c237002f5..d8515c13ce257 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -370,11 +370,10 @@ STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_t MP_DEFINE_CONST_OBJ_TYPE( mp_type_gen_instance, MP_QSTR_generator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, print, gen_instance_print, unary_op, mp_generic_unary_op, - getiter, mp_identity_getiter, - iternext, gen_instance_iternext, + iter, gen_instance_iternext, locals_dict, &gen_instance_locals_dict ); diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index ed2dfbbe1f9e7..134cbcd6299d5 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -59,10 +59,9 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_it, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, it_iternext + iter, it_iternext ); // args are those returned from mp_load_method_maybe (ie either an attribute or a method) diff --git a/py/objlist.c b/py/objlist.c index 5f9e99cc79b26..8d18344ea8a71 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -455,13 +455,13 @@ STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( mp_type_list, MP_QSTR_list, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, list_make_new, print, list_print, unary_op, list_unary_op, binary_op, list_binary_op, subscr, list_subscr, - getiter, list_getiter, + iter, list_getiter, locals_dict, &list_locals_dict ); diff --git a/py/objmap.c b/py/objmap.c index dc305e21b5cb7..115832e387e56 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -66,8 +66,7 @@ STATIC mp_obj_t map_iternext(mp_obj_t self_in) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_map, MP_QSTR_map, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, map_make_new, - getiter, mp_identity_getiter, - iternext, map_iternext + iter, map_iternext ); diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 52536be8b4e97..3b45d8f76f29b 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -163,7 +163,7 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t MP_OBJ_TYPE_SET_SLOT(type, binary_op, mp_obj_tuple_binary_op, 2); MP_OBJ_TYPE_SET_SLOT(type, attr, namedtuple_attr, 3); MP_OBJ_TYPE_SET_SLOT(type, subscr, mp_obj_tuple_subscr, 4); - MP_OBJ_TYPE_SET_SLOT(type, getiter, mp_obj_tuple_getiter, 5); + MP_OBJ_TYPE_SET_SLOT(type, iter, mp_obj_tuple_getiter, 5); MP_OBJ_TYPE_SET_SLOT(type, parent, &mp_type_tuple, 6); return MP_OBJ_FROM_PTR(o); } diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 5bc397f6ec3bf..7a45b6b73fe6f 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -48,10 +48,9 @@ STATIC mp_obj_t polymorph_it_iternext(mp_obj_t self_in) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_polymorph_iter, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, polymorph_it_iternext + iter, polymorph_it_iternext ); #if MICROPY_ENABLE_FINALISER @@ -81,10 +80,9 @@ STATIC MP_DEFINE_CONST_DICT(mp_obj_polymorph_iter_locals_dict, mp_obj_polymorph_ MP_DEFINE_CONST_OBJ_TYPE( mp_type_polymorph_iter_with_finaliser, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, polymorph_it_iternext, + iter, polymorph_it_iternext, locals_dict, &mp_obj_polymorph_iter_locals_dict ); #endif diff --git a/py/objrange.c b/py/objrange.c index 3140504b2bb4c..1ad8f6031f292 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -53,10 +53,9 @@ STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) { STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_range_it, MP_QSTR_iterator, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, MP_TYPE_NULL_MAKE_NEW, - getiter, mp_identity_getiter, - iternext, range_it_iternext + iter, range_it_iternext ); STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf) { @@ -232,5 +231,5 @@ MP_DEFINE_CONST_OBJ_TYPE( print, range_print, unary_op, range_unary_op, subscr, range_subscr, - getiter, range_getiter + iter, range_getiter ); diff --git a/py/objreversed.c b/py/objreversed.c index bc1f07ddecc46..e265266d3f127 100644 --- a/py/objreversed.c +++ b/py/objreversed.c @@ -71,10 +71,9 @@ STATIC mp_obj_t reversed_iternext(mp_obj_t self_in) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_reversed, MP_QSTR_reversed, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, reversed_make_new, - getiter, mp_identity_getiter, - iternext, reversed_iternext + iter, reversed_iternext ); #endif // MICROPY_PY_BUILTINS_REVERSED diff --git a/py/objset.c b/py/objset.c index 6f21bf15dfb58..b827f49f40dfe 100644 --- a/py/objset.c +++ b/py/objset.c @@ -542,12 +542,12 @@ STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( mp_type_set, MP_QSTR_set, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, set_make_new, print, set_print, unary_op, set_unary_op, binary_op, set_binary_op, - getiter, set_getiter, + iter, set_getiter, locals_dict, &set_locals_dict ); @@ -568,12 +568,12 @@ STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( mp_type_frozenset, MP_QSTR_frozenset, - MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, set_make_new, print, set_print, unary_op, set_unary_op, binary_op, set_binary_op, - getiter, set_getiter, + iter, set_getiter, locals_dict, &frozenset_locals_dict ); #endif diff --git a/py/objstr.c b/py/objstr.c index d425055559f84..12f6e15d0498c 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2151,7 +2151,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, str_print, binary_op, mp_obj_str_binary_op, subscr, bytes_subscr, - getiter, mp_obj_new_str_iterator, + iter, mp_obj_new_str_iterator, buffer, mp_obj_str_get_buffer, locals_dict, &mp_obj_str_locals_dict ); @@ -2166,7 +2166,7 @@ MP_DEFINE_CONST_OBJ_TYPE( print, str_print, binary_op, mp_obj_str_binary_op, subscr, bytes_subscr, - getiter, mp_obj_new_bytes_iterator, + iter, mp_obj_new_bytes_iterator, buffer, mp_obj_str_get_buffer, locals_dict, &mp_obj_bytes_locals_dict ); diff --git a/py/objstringio.c b/py/objstringio.c index 77547f88cf6c2..4e19b83999aac 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -247,11 +247,9 @@ STATIC const mp_stream_p_t stringio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_stringio, MP_QSTR_StringIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, stringio_make_new, print, stringio_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &stringio_stream_p, locals_dict, &stringio_locals_dict ); @@ -266,11 +264,9 @@ STATIC const mp_stream_p_t bytesio_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( mp_type_bytesio, MP_QSTR_BytesIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, stringio_make_new, print, stringio_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &bytesio_stream_p, locals_dict, &stringio_locals_dict ); diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 15c59e4e95724..9b28841ecd177 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -232,13 +232,13 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_str, MP_QSTR_str, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, mp_obj_str_make_new, print, uni_print, unary_op, uni_unary_op, binary_op, mp_obj_str_binary_op, subscr, str_subscr, - getiter, mp_obj_new_str_iterator, + iter, mp_obj_new_str_iterator, buffer, mp_obj_str_get_buffer, locals_dict, &mp_obj_str_locals_dict ); diff --git a/py/objtuple.c b/py/objtuple.c index 01b2fa14885ac..485d44c52a8d7 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -32,7 +32,7 @@ #include "py/runtime.h" // type check is done on getiter method to allow tuple, namedtuple, attrtuple -#define mp_obj_is_tuple_compatible(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), getiter) == mp_obj_tuple_getiter) +#define mp_obj_is_tuple_compatible(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), iter) == mp_obj_tuple_getiter) /******************************************************************************/ /* tuple */ @@ -111,7 +111,7 @@ STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t anothe mp_check_self(mp_obj_is_tuple_compatible(self_in)); const mp_obj_type_t *another_type = mp_obj_get_type(another_in); mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in); - if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(another_type, getiter) != mp_obj_tuple_getiter) { + if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(another_type, iter) != mp_obj_tuple_getiter) { // Slow path for user subclasses another_in = mp_obj_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple)); if (another_in == MP_OBJ_NULL) { @@ -227,13 +227,13 @@ STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( mp_type_tuple, MP_QSTR_tuple, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_GETITER, mp_obj_tuple_make_new, print, mp_obj_tuple_print, unary_op, mp_obj_tuple_unary_op, binary_op, mp_obj_tuple_binary_op, subscr, mp_obj_tuple_subscr, - getiter, mp_obj_tuple_getiter, + iter, mp_obj_tuple_getiter, locals_dict, &tuple_locals_dict ); diff --git a/py/objtype.c b/py/objtype.c index 5b4e375bcc481..183dce071ee9b 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -142,7 +142,10 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_t // this should not be applied to class types, as will result in extra // lookup either. if (lookup->slot_offset != 0 && mp_obj_is_native_type(type)) { - if (MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(type, lookup->slot_offset)) { + // Check if there is a non-zero value in the specified slot index, + // with a special case for getiter where the slot won't be set + // for MP_TYPE_FLAG_ITER_IS_STREAM. + if (MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(type, lookup->slot_offset) || (lookup->slot_offset == MP_OBJ_TYPE_OFFSETOF_SLOT(iter) && type->flags & MP_TYPE_FLAG_ITER_IS_STREAM)) { DEBUG_printf("mp_obj_class_lookup: Matched special meth slot (off=%d) for %s\n", lookup->slot_offset, qstr_str(lookup->attr)); lookup->dest[0] = MP_OBJ_SENTINEL; @@ -889,7 +892,7 @@ mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) struct class_lookup_data lookup = { .obj = self, .attr = MP_QSTR___iter__, - .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(getiter), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(iter), .dest = member, .is_type = false, }; @@ -898,10 +901,14 @@ mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) return MP_OBJ_NULL; } else if (member[0] == MP_OBJ_SENTINEL) { const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]); - if (iter_buf == NULL) { - iter_buf = m_new_obj(mp_obj_iter_buf_t); + if (type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) { + return self->subobj[0]; + } else { + if (iter_buf == NULL) { + iter_buf = m_new_obj(mp_obj_iter_buf_t); + } + return ((mp_getiter_fun_t)MP_OBJ_TYPE_GET_SLOT(type, iter))(self->subobj[0], iter_buf); } - return MP_OBJ_TYPE_GET_SLOT(type, getiter)(self->subobj[0], iter_buf); } else { return mp_call_method_n_kw(0, 0, member); } @@ -1122,7 +1129,9 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) // Basic validation of base classes uint16_t base_flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE - | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_EQ_HAS_NEQ_TEST; + | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE + | MP_TYPE_FLAG_EQ_HAS_NEQ_TEST + | MP_TYPE_FLAG_ITER_IS_GETITER; size_t bases_len; mp_obj_t *bases_items; mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items); @@ -1167,7 +1176,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) MP_OBJ_TYPE_SET_SLOT(o, binary_op, instance_binary_op, 3); MP_OBJ_TYPE_SET_SLOT(o, attr, mp_obj_instance_attr, 4); MP_OBJ_TYPE_SET_SLOT(o, subscr, instance_subscr, 5); - MP_OBJ_TYPE_SET_SLOT(o, getiter, mp_obj_instance_getiter, 6); + MP_OBJ_TYPE_SET_SLOT(o, iter, mp_obj_instance_getiter, 6); // MP_OBJ_TYPE_SET_SLOT(o, iternext, not implemented) MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 7); diff --git a/py/objzip.c b/py/objzip.c index 0ceafd97f2409..34d73465ef2bd 100644 --- a/py/objzip.c +++ b/py/objzip.c @@ -69,8 +69,7 @@ STATIC mp_obj_t zip_iternext(mp_obj_t self_in) { MP_DEFINE_CONST_OBJ_TYPE( mp_type_zip, MP_QSTR_zip, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, zip_make_new, - getiter, mp_identity_getiter, - iternext, zip_iternext + iter, zip_iternext ); diff --git a/py/runtime.c b/py/runtime.c index d0e504a3d096f..ec628bfe14b4a 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -61,6 +61,8 @@ const mp_obj_module_t mp_module___main__ = { MP_REGISTER_MODULE(MP_QSTR___main__, mp_module___main__); +#define TYPE_HAS_ITERNEXT(type) (type->flags & (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM | MP_TYPE_FLAG_ITER_IS_STREAM)) + void mp_init(void) { qstr_init(); @@ -1167,7 +1169,7 @@ void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) { } #endif - if (attr == MP_QSTR___next__ && MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { + if (attr == MP_QSTR___next__ && TYPE_HAS_ITERNEXT(type)) { dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj); dest[1] = obj; return; @@ -1260,21 +1262,26 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { assert(o_in); const mp_obj_type_t *type = mp_obj_get_type(o_in); + // Most types that use iternext just use the identity getiter. We handle this case explicitly + // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used. + if ((type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) == MP_TYPE_FLAG_ITER_IS_ITERNEXT || (type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) { + return o_in; + } - if (MP_OBJ_TYPE_HAS_SLOT(type, getiter)) { - // Check for native getiter which is the identity. We handle this case explicitly - // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used. - if (MP_OBJ_TYPE_GET_SLOT(type, getiter) == mp_identity_getiter) { - return o_in; - } - + if (MP_OBJ_TYPE_HAS_SLOT(type, iter)) { // check for native getiter (corresponds to __iter__) - if (iter_buf == NULL && MP_OBJ_TYPE_GET_SLOT(type, getiter) != mp_obj_instance_getiter) { + if (iter_buf == NULL && MP_OBJ_TYPE_GET_SLOT(type, iter) != mp_obj_instance_getiter) { // if caller did not provide a buffer then allocate one on the heap // mp_obj_instance_getiter is special, it will allocate only if needed iter_buf = m_new_obj(mp_obj_iter_buf_t); } - mp_obj_t iter = MP_OBJ_TYPE_GET_SLOT(type, getiter)(o_in, iter_buf); + mp_getiter_fun_t getiter; + if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) { + getiter = ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->getiter; + } else { + getiter = (mp_getiter_fun_t)MP_OBJ_TYPE_GET_SLOT(type, iter); + } + mp_obj_t iter = getiter(o_in, iter_buf); if (iter != MP_OBJ_NULL) { return iter; } @@ -1302,13 +1309,26 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { } +STATIC mp_fun_1_t type_get_iternext(const mp_obj_type_t *type) { + if ((type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) { + mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self); + return mp_stream_unbuffered_iter; + } else if (type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) { + return (mp_fun_1_t)MP_OBJ_TYPE_GET_SLOT(type, iter); + } else if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) { + return ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->iternext; + } else { + return NULL; + } +} + // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration() // may also raise StopIteration() mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { + if (TYPE_HAS_ITERNEXT(type)) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - return MP_OBJ_TYPE_GET_SLOT(type, iternext)(o_in); + return type_get_iternext(type)(o_in); } else { // check for __next__ method mp_obj_t dest[2]; @@ -1332,9 +1352,9 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { mp_obj_t mp_iternext(mp_obj_t o_in) { MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext const mp_obj_type_t *type = mp_obj_get_type(o_in); - if (MP_OBJ_TYPE_HAS_SLOT(type, iternext)) { + if (TYPE_HAS_ITERNEXT(type)) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - return MP_OBJ_TYPE_GET_SLOT(type, iternext)(o_in); + return type_get_iternext(type)(o_in); } else { // check for __next__ method mp_obj_t dest[2]; @@ -1372,9 +1392,9 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val); } - if (MP_OBJ_TYPE_HAS_SLOT(type, iternext) && send_value == mp_const_none) { + if (TYPE_HAS_ITERNEXT(type) && send_value == mp_const_none) { MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL; - mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, iternext)(self_in); + mp_obj_t ret = type_get_iternext(type)(self_in); *ret_val = ret; if (ret != MP_OBJ_STOP_ITERATION) { return MP_VM_RETURN_YIELD; diff --git a/shared/runtime/sys_stdio_mphal.c b/shared/runtime/sys_stdio_mphal.c index 6d43425e298fe..325f93dde1d89 100644 --- a/shared/runtime/sys_stdio_mphal.c +++ b/shared/runtime/sys_stdio_mphal.c @@ -126,11 +126,9 @@ STATIC const mp_stream_p_t stdio_obj_stream_p = { MP_DEFINE_CONST_OBJ_TYPE( stdio_obj_type, MP_QSTR_FileIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, stdio_obj_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &stdio_obj_stream_p, locals_dict, &stdio_locals_dict ); @@ -162,11 +160,9 @@ STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = { STATIC MP_DEFINE_CONST_OBJ_TYPE( stdio_buffer_obj_type, MP_QSTR_FileIO, - MP_TYPE_FLAG_NONE, + MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_NULL_MAKE_NEW, print, stdio_obj_print, - getiter, mp_identity_getiter, - iternext, mp_stream_unbuffered_iter, protocol, &stdio_buffer_obj_stream_p, locals_dict, &stdio_locals_dict ); diff --git a/tests/basics/io_stringio_base.py b/tests/basics/io_stringio_base.py new file mode 100644 index 0000000000000..dffc879074a78 --- /dev/null +++ b/tests/basics/io_stringio_base.py @@ -0,0 +1,24 @@ +# Checks that an instance type inheriting from a native base that uses +# MP_TYPE_FLAG_ITER_IS_STREAM will still have a getiter. + +try: + import uio as io +except ImportError: + import io + +a = io.StringIO() +a.write("hello\nworld\nmicro\npython\n") +a.seek(0) + +for line in a: + print(line) + +class X(io.StringIO): + pass + +b = X() +b.write("hello\nworld\nmicro\npython\n") +b.seek(0) + +for line in b: + print(line) From 94beeabd2ee179d587942046555833e022241f24 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 17 Sep 2022 00:31:23 +1000 Subject: [PATCH 2627/3533] py/obj: Convert make_new into a mp_obj_type_t slot. Instead of being an explicit field, it's now a slot like all the other methods. This is a marginal code size improvement because most types have a make_new (100/138 on PYBV11), however it improves consistency in how types are declared, removing the special case for make_new. Signed-off-by: Jim Mussared --- drivers/ninaw10/nina_wifi_bsp.c | 2 +- examples/natmod/framebuf/framebuf.c | 6 +- examples/natmod/uzlib/uzlib.c | 6 +- extmod/machine_i2c.c | 2 +- extmod/machine_i2c.h | 2 +- extmod/machine_mem.c | 1 - extmod/machine_pinbase.c | 2 +- extmod/machine_pwm.c | 2 +- extmod/machine_signal.c | 2 +- extmod/machine_spi.c | 2 +- extmod/machine_spi.h | 2 +- extmod/modbluetooth.c | 4 +- extmod/modbtree.c | 1 - extmod/modframebuf.c | 2 +- extmod/modlwip.c | 4 +- extmod/moduasyncio.c | 4 +- extmod/moducryptolib.c | 2 +- extmod/moductypes.c | 2 +- extmod/moduhashlib.c | 6 +- extmod/modure.c | 2 - extmod/moduselect.c | 1 - extmod/modusocket.c | 2 +- extmod/modussl_axtls.c | 1 - extmod/modussl_mbedtls.c | 1 - extmod/modutimeq.c | 2 +- extmod/moduwebsocket.c | 2 +- extmod/moduzlib.c | 2 +- extmod/modwebrepl.c | 2 +- extmod/network_cyw43.c | 2 +- extmod/network_ninaw10.c | 4 +- extmod/network_wiznet5k.c | 6 +- extmod/vfs.c | 6 +- extmod/vfs_fat.c | 2 +- extmod/vfs_fat_file.c | 2 - extmod/vfs_lfsx.c | 2 +- extmod/vfs_lfsx_file.c | 2 - extmod/vfs_posix.c | 2 +- extmod/vfs_posix_file.c | 2 - ports/cc3200/misc/mpirq.c | 1 - ports/cc3200/mods/modnetwork.c | 2 +- ports/cc3200/mods/moduhashlib.c | 4 +- ports/cc3200/mods/modusocket.c | 2 +- ports/cc3200/mods/modwlan.c | 2 +- ports/cc3200/mods/pybadc.c | 3 +- ports/cc3200/mods/pybflash.c | 2 +- ports/cc3200/mods/pybi2c.c | 2 +- ports/cc3200/mods/pybpin.c | 3 +- ports/cc3200/mods/pybrtc.c | 2 +- ports/cc3200/mods/pybsd.c | 2 +- ports/cc3200/mods/pybsleep.c | 3 +- ports/cc3200/mods/pybspi.c | 2 +- ports/cc3200/mods/pybtimer.c | 3 +- ports/cc3200/mods/pybuart.c | 2 +- ports/cc3200/mods/pybwdt.c | 2 +- ports/esp32/esp32_nvs.c | 2 +- ports/esp32/esp32_partition.c | 2 +- ports/esp32/esp32_rmt.c | 2 +- ports/esp32/esp32_ulp.c | 2 +- ports/esp32/machine_adc.c | 2 +- ports/esp32/machine_adcblock.c | 2 +- ports/esp32/machine_dac.c | 2 +- ports/esp32/machine_hw_spi.c | 2 +- ports/esp32/machine_i2c.c | 2 +- ports/esp32/machine_i2s.c | 2 +- ports/esp32/machine_pin.c | 3 +- ports/esp32/machine_rtc.c | 2 +- ports/esp32/machine_sdcard.c | 2 +- ports/esp32/machine_timer.c | 2 +- ports/esp32/machine_touchpad.c | 2 +- ports/esp32/machine_uart.c | 2 +- ports/esp32/machine_wdt.c | 2 +- ports/esp32/modsocket.c | 2 +- ports/esp32/network_lan.c | 1 - ports/esp32/network_ppp.c | 1 - ports/esp32/network_wlan.c | 1 - ports/esp8266/machine_adc.c | 2 +- ports/esp8266/machine_hspi.c | 2 +- ports/esp8266/machine_pin.c | 3 +- ports/esp8266/machine_rtc.c | 2 +- ports/esp8266/machine_uart.c | 2 +- ports/esp8266/machine_wdt.c | 2 +- ports/esp8266/main.c | 2 +- ports/esp8266/modmachine.c | 2 +- ports/esp8266/modnetwork.c | 1 - ports/mimxrt/machine_adc.c | 2 +- ports/mimxrt/machine_i2c.c | 2 +- ports/mimxrt/machine_i2s.c | 2 +- ports/mimxrt/machine_led.c | 2 +- ports/mimxrt/machine_pin.c | 6 +- ports/mimxrt/machine_rtc.c | 2 +- ports/mimxrt/machine_sdcard.c | 2 +- ports/mimxrt/machine_spi.c | 2 +- ports/mimxrt/machine_timer.c | 2 +- ports/mimxrt/machine_uart.c | 2 +- ports/mimxrt/machine_wdt.c | 2 +- ports/mimxrt/mimxrt_flash.c | 2 +- ports/mimxrt/network_lan.c | 2 +- ports/nrf/boards/microbit/modules/iters.c | 1 - .../boards/microbit/modules/microbitdisplay.c | 1 - .../boards/microbit/modules/microbitimage.c | 4 +- ports/nrf/main.c | 2 +- ports/nrf/modules/board/led.c | 2 +- ports/nrf/modules/machine/adc.c | 2 +- ports/nrf/modules/machine/i2c.c | 2 +- ports/nrf/modules/machine/pin.c | 3 +- ports/nrf/modules/machine/pwm.c | 2 +- ports/nrf/modules/machine/rtcounter.c | 2 +- ports/nrf/modules/machine/spi.c | 2 +- ports/nrf/modules/machine/temp.c | 2 +- ports/nrf/modules/machine/timer.c | 2 +- ports/nrf/modules/machine/uart.c | 2 +- ports/nrf/modules/nrf/flashbdev.c | 2 +- .../modules/ubluepy/ubluepy_characteristic.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_constants.c | 2 - ports/nrf/modules/ubluepy/ubluepy_delegate.c | 2 +- .../nrf/modules/ubluepy/ubluepy_descriptor.c | 2 +- .../nrf/modules/ubluepy/ubluepy_peripheral.c | 2 +- .../nrf/modules/ubluepy/ubluepy_scan_entry.c | 1 - ports/nrf/modules/ubluepy/ubluepy_scanner.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_service.c | 2 +- ports/nrf/modules/ubluepy/ubluepy_uuid.c | 2 +- ports/nrf/modules/uos/microbitfs.c | 2 - ports/nrf/pin_named_pins.c | 2 - ports/pic16bit/modpybled.c | 2 +- ports/pic16bit/modpybswitch.c | 2 +- ports/renesas-ra/extint.c | 2 +- ports/renesas-ra/led.c | 2 +- ports/renesas-ra/machine_adc.c | 2 +- ports/renesas-ra/machine_i2c.c | 2 +- ports/renesas-ra/machine_pin.c | 4 +- ports/renesas-ra/machine_rtc.c | 2 +- ports/renesas-ra/machine_spi.c | 2 +- ports/renesas-ra/machine_timer.c | 2 +- ports/renesas-ra/machine_uart.c | 2 +- ports/renesas-ra/main.c | 2 +- ports/renesas-ra/storage.c | 2 +- ports/renesas-ra/timer.c | 3 +- ports/renesas-ra/usrsw.c | 2 +- ports/rp2/machine_adc.c | 2 +- ports/rp2/machine_i2c.c | 2 +- ports/rp2/machine_i2s.c | 2 +- ports/rp2/machine_pin.c | 2 +- ports/rp2/machine_rtc.c | 2 +- ports/rp2/machine_spi.c | 2 +- ports/rp2/machine_timer.c | 2 +- ports/rp2/machine_uart.c | 2 +- ports/rp2/machine_wdt.c | 2 +- ports/rp2/mpbthciport.c | 2 +- ports/rp2/rp2_flash.c | 2 +- ports/rp2/rp2_pio.c | 4 +- ports/samd/machine_led.c | 2 +- ports/samd/machine_pin.c | 2 +- ports/samd/samd_flash.c | 2 +- ports/stm32/accel.c | 2 +- ports/stm32/adc.c | 4 +- ports/stm32/boards/LEGO_HUB_NO6/cc2564.c | 2 +- ports/stm32/dac.c | 2 +- ports/stm32/extint.c | 2 +- ports/stm32/lcd.c | 2 +- ports/stm32/led.c | 2 +- ports/stm32/machine_adc.c | 2 +- ports/stm32/machine_i2c.c | 2 +- ports/stm32/machine_i2s.c | 2 +- ports/stm32/machine_spi.c | 2 +- ports/stm32/machine_timer.c | 2 +- ports/stm32/machine_uart.c | 2 +- ports/stm32/main.c | 2 +- ports/stm32/network_lan.c | 2 +- ports/stm32/pin.c | 3 +- ports/stm32/pin_named_pins.c | 2 - ports/stm32/pyb_can.c | 2 +- ports/stm32/pyb_i2c.c | 2 +- ports/stm32/pyb_spi.c | 2 +- ports/stm32/rtc.c | 2 +- ports/stm32/sdcard.c | 4 +- ports/stm32/servo.c | 2 +- ports/stm32/storage.c | 2 +- ports/stm32/timer.c | 3 +- ports/stm32/usb.c | 4 +- ports/stm32/usrsw.c | 2 +- ports/stm32/wdt.c | 2 +- ports/teensy/led.c | 2 +- ports/teensy/timer.c | 3 +- ports/teensy/uart.c | 2 +- ports/unix/coverage.c | 2 - ports/unix/main.c | 2 +- ports/unix/modffi.c | 8 +-- ports/unix/modjni.c | 3 - ports/unix/moduselect.c | 1 - ports/unix/modusocket.c | 2 +- ports/zephyr/machine_i2c.c | 2 +- ports/zephyr/machine_pin.c | 2 +- ports/zephyr/machine_spi.c | 2 +- ports/zephyr/machine_uart.c | 2 +- ports/zephyr/main.c | 4 +- ports/zephyr/modusocket.c | 2 +- ports/zephyr/modzsensor.c | 2 +- ports/zephyr/zephyr_storage.c | 4 +- py/builtinevex.c | 3 +- py/dynruntime.h | 2 +- py/modbuiltins.c | 2 +- py/modio.c | 4 +- py/modthread.c | 1 - py/obj.h | 56 +++++++++---------- py/objarray.c | 7 +-- py/objattrtuple.c | 1 - py/objbool.c | 2 +- py/objboundmeth.c | 1 - py/objcell.c | 2 +- py/objclosure.c | 1 - py/objcomplex.c | 3 +- py/objdeque.c | 2 +- py/objdict.c | 6 +- py/objenumerate.c | 2 +- py/objexcept.c | 12 ++-- py/objexcept.h | 3 +- py/objfilter.c | 2 +- py/objfloat.c | 3 +- py/objfun.c | 13 ++--- py/objgenerator.c | 3 - py/objgetitemiter.c | 1 - py/objint.c | 2 +- py/objlist.c | 4 +- py/objlist.h | 1 + py/objmap.c | 2 +- py/objmodule.c | 1 - py/objnamedtuple.c | 16 +++--- py/objnamedtuple.h | 4 +- py/objnone.c | 1 - py/objobject.c | 2 +- py/objpolyiter.c | 2 - py/objproperty.c | 2 +- py/objrange.c | 3 +- py/objreversed.c | 2 +- py/objset.c | 4 +- py/objsingleton.c | 2 +- py/objslice.c | 1 - py/objstr.c | 6 +- py/objstringio.c | 4 +- py/objstrunicode.c | 2 +- py/objtuple.c | 2 +- py/objtype.c | 56 +++++++++---------- py/objtype.h | 4 +- py/objzip.c | 2 +- py/profile.c | 2 - py/runtime.c | 1 - shared/runtime/mpirq.c | 1 - shared/runtime/sys_stdio_mphal.c | 2 - 248 files changed, 316 insertions(+), 397 deletions(-) diff --git a/drivers/ninaw10/nina_wifi_bsp.c b/drivers/ninaw10/nina_wifi_bsp.c index d5b1308536af9..c65fb111fb101 100644 --- a/drivers/ninaw10/nina_wifi_bsp.c +++ b/drivers/ninaw10/nina_wifi_bsp.c @@ -73,7 +73,7 @@ int nina_bsp_init(void) { MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIFI_SPI_BAUDRATE), }; - MP_STATE_PORT(mp_wifi_spi) = machine_spi_type.make_new((mp_obj_t)&machine_spi_type, 2, 0, args); + MP_STATE_PORT(mp_wifi_spi) = MP_OBJ_TYPE_GET_SLOT(&machine_spi_type, make_new)((mp_obj_t)&machine_spi_type, 2, 0, args); return 0; } diff --git a/examples/natmod/framebuf/framebuf.c b/examples/natmod/framebuf/framebuf.c index 4c557294719d8..32b67eabcf84f 100644 --- a/examples/natmod/framebuf/framebuf.c +++ b/examples/natmod/framebuf/framebuf.c @@ -20,8 +20,8 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a mp_type_framebuf.base.type = (void*)&mp_type_type; mp_type_framebuf.name = MP_QSTR_FrameBuffer; - mp_type_framebuf.make_new = framebuf_make_new; - MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, buffer, framebuf_get_buffer, 0); + MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, make_new, framebuf_make_new, 0); + MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, buffer, framebuf_get_buffer, 1); framebuf_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill), MP_OBJ_FROM_PTR(&framebuf_fill_obj) }; framebuf_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill_rect), MP_OBJ_FROM_PTR(&framebuf_fill_rect_obj) }; framebuf_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_pixel), MP_OBJ_FROM_PTR(&framebuf_pixel_obj) }; @@ -33,7 +33,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a framebuf_locals_dict_table[8] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_blit), MP_OBJ_FROM_PTR(&framebuf_blit_obj) }; framebuf_locals_dict_table[9] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_scroll), MP_OBJ_FROM_PTR(&framebuf_scroll_obj) }; framebuf_locals_dict_table[10] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_text), MP_OBJ_FROM_PTR(&framebuf_text_obj) }; - MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, locals_dict, (void*)&framebuf_locals_dict, 1); + MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, locals_dict, (void*)&framebuf_locals_dict, 2); mp_store_global(MP_QSTR_FrameBuffer, MP_OBJ_FROM_PTR(&mp_type_framebuf)); mp_store_global(MP_QSTR_FrameBuffer1, MP_OBJ_FROM_PTR(&legacy_framebuffer1_obj)); diff --git a/examples/natmod/uzlib/uzlib.c b/examples/natmod/uzlib/uzlib.c index 469452e8f8437..9cf58b10e78e2 100644 --- a/examples/natmod/uzlib/uzlib.c +++ b/examples/natmod/uzlib/uzlib.c @@ -20,12 +20,12 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a decompio_type.base.type = mp_fun_table.type_type; decompio_type.name = MP_QSTR_DecompIO; - decompio_type.make_new = &decompio_make_new; - MP_OBJ_TYPE_SET_SLOT(&decompio_type, protocol, &decompio_stream_p, 0); + MP_OBJ_TYPE_SET_SLOT(&decompio_type, make_new, &decompio_make_new, 0); + MP_OBJ_TYPE_SET_SLOT(&decompio_type, protocol, &decompio_stream_p, 1); decompio_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_OBJ_FROM_PTR(&mp_stream_read_obj) }; decompio_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_OBJ_FROM_PTR(&mp_stream_readinto_obj) }; decompio_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_OBJ_FROM_PTR(&mp_stream_unbuffered_readline_obj) }; - MP_OBJ_TYPE_SET_SLOT(&decompio_type, locals_dict, (void*)&decompio_locals_dict, 1); + MP_OBJ_TYPE_SET_SLOT(&decompio_type, locals_dict, (void*)&decompio_locals_dict, 2); mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_uzlib)); mp_store_global(MP_QSTR_decompress, MP_OBJ_FROM_PTR(&mod_uzlib_decompress_obj)); diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index bb7ade6fcf574..3563b3243c36f 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -734,7 +734,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_machine_soft_i2c_type, MP_QSTR_SoftI2C, MP_TYPE_FLAG_NONE, - mp_machine_soft_i2c_make_new, + make_new, mp_machine_soft_i2c_make_new, print, mp_machine_soft_i2c_print, protocol, &mp_machine_soft_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/extmod/machine_i2c.h b/extmod/machine_i2c.h index a3363d4c341c5..600145bfedf4e 100644 --- a/extmod/machine_i2c.h +++ b/extmod/machine_i2c.h @@ -38,7 +38,7 @@ --n_args; \ ++all_args; \ } \ - return mp_machine_soft_i2c_type.make_new(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \ + return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_i2c_type, make_new)(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \ } \ } while (0) diff --git a/extmod/machine_mem.c b/extmod/machine_mem.c index 422e99d3ce42a..f27356e028365 100644 --- a/extmod/machine_mem.c +++ b/extmod/machine_mem.c @@ -105,7 +105,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_mem_type, MP_QSTR_mem, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, machine_mem_print, subscr, machine_mem_subscr ); diff --git a/extmod/machine_pinbase.c b/extmod/machine_pinbase.c index 617dd1280c784..8607e6ed3de99 100644 --- a/extmod/machine_pinbase.c +++ b/extmod/machine_pinbase.c @@ -81,7 +81,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pinbase_type, MP_QSTR_PinBase, MP_TYPE_FLAG_NONE, - pinbase_make_new, + make_new, pinbase_make_new, protocol, &pinbase_pin_p ); diff --git a/extmod/machine_pwm.c b/extmod/machine_pwm.c index f12f70a2d14d4..220d34d7da582 100644 --- a/extmod/machine_pwm.c +++ b/extmod/machine_pwm.c @@ -136,7 +136,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pwm_type, MP_QSTR_PWM, MP_TYPE_FLAG_NONE, - mp_machine_pwm_make_new, + make_new, mp_machine_pwm_make_new, print, mp_machine_pwm_print, locals_dict, &machine_pwm_locals_dict ); diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index 49ee6dfb4abc8..7922ed70771e9 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -176,7 +176,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_signal_type, MP_QSTR_Signal, MP_TYPE_FLAG_NONE, - signal_make_new, + make_new, signal_make_new, call, signal_call, protocol, &signal_pin_p, locals_dict, &signal_locals_dict diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index 54f1964e2193a..bb35cff38e618 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -255,7 +255,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_machine_soft_spi_type, MP_QSTR_SoftSPI, MP_TYPE_FLAG_NONE, - mp_machine_soft_spi_make_new, + make_new, mp_machine_soft_spi_make_new, print, mp_machine_soft_spi_print, protocol, &mp_machine_soft_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/extmod/machine_spi.h b/extmod/machine_spi.h index ca92c719a8b5a..93ee8d00ca32b 100644 --- a/extmod/machine_spi.h +++ b/extmod/machine_spi.h @@ -39,7 +39,7 @@ --n_args; \ ++all_args; \ } \ - return mp_machine_soft_spi_type.make_new(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \ + return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_spi_type, make_new)(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \ } \ } while (0) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 2e058fc7deb25..9c168483ba453 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -244,7 +244,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_bluetooth_uuid, MP_QSTR_UUID, MP_TYPE_FLAG_NONE, - bluetooth_uuid_make_new, + make_new, bluetooth_uuid_make_new, unary_op, bluetooth_uuid_unary_op, binary_op, bluetooth_uuid_binary_op, print, bluetooth_uuid_print, @@ -980,7 +980,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_bluetooth_ble, MP_QSTR_BLE, MP_TYPE_FLAG_NONE, - bluetooth_ble_make_new, + make_new, bluetooth_ble_make_new, locals_dict, &bluetooth_ble_locals_dict ); diff --git a/extmod/modbtree.c b/extmod/modbtree.c index f21fe3ff94173..2da65a2c7affc 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -328,7 +328,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( btree_type, MP_QSTR_btree, MP_TYPE_FLAG_ITER_IS_CUSTOM, - MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, btree_print, iter, &btree_getiter_iternext, diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 5347be5643667..e7825b59128ef 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -833,7 +833,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_framebuf, MP_QSTR_FrameBuffer, MP_TYPE_FLAG_NONE, - framebuf_make_new, + make_new, framebuf_make_new, buffer, framebuf_get_buffer, locals_dict, &framebuf_locals_dict ); diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 2f5da36f40845..f64a5a6250bae 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -181,7 +181,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( lwip_slip_type, MP_QSTR_slip, MP_TYPE_FLAG_NONE, - lwip_slip_make_new, + make_new, lwip_slip_make_new, locals_dict, &lwip_slip_locals_dict ); @@ -1599,7 +1599,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( lwip_socket_type, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - lwip_socket_make_new, + make_new, lwip_socket_make_new, print, lwip_socket_print, protocol, &lwip_socket_stream_p, locals_dict, &lwip_socket_locals_dict diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c index b0eb8b6509c82..021e0da1be2c4 100644 --- a/extmod/moduasyncio.c +++ b/extmod/moduasyncio.c @@ -148,7 +148,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( task_queue_type, MP_QSTR_TaskQueue, MP_TYPE_FLAG_NONE, - task_queue_make_new, + make_new, task_queue_make_new, locals_dict, &task_queue_locals_dict ); @@ -296,7 +296,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( task_type, MP_QSTR_Task, MP_TYPE_FLAG_ITER_IS_CUSTOM, - task_make_new, + make_new, task_make_new, attr, task_attr, iter, &task_getiter_iternext ); diff --git a/extmod/moducryptolib.c b/extmod/moducryptolib.c index e4625c21a8786..fc3fcfd90d904 100644 --- a/extmod/moducryptolib.c +++ b/extmod/moducryptolib.c @@ -352,7 +352,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ucryptolib_aes_type, MP_QSTR_aes, MP_TYPE_FLAG_NONE, - ucryptolib_aes_make_new, + make_new, ucryptolib_aes_make_new, locals_dict, &ucryptolib_aes_locals_dict ); diff --git a/extmod/moductypes.c b/extmod/moductypes.c index a9ad500c2d1a0..15c36290a9f0e 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -638,7 +638,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( uctypes_struct_type, MP_QSTR_struct, MP_TYPE_FLAG_NONE, - uctypes_struct_make_new, + make_new, uctypes_struct_make_new, print, uctypes_struct_print, attr, uctypes_struct_attr, subscr, uctypes_struct_subscr, diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 9535e00b406dd..64e15c444df80 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -161,7 +161,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( uhashlib_sha256_type, MP_QSTR_sha256, MP_TYPE_FLAG_NONE, - uhashlib_sha256_make_new, + make_new, uhashlib_sha256_make_new, locals_dict, &uhashlib_sha256_locals_dict ); #endif @@ -255,7 +255,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( uhashlib_sha1_type, MP_QSTR_sha1, MP_TYPE_FLAG_NONE, - uhashlib_sha1_make_new, + make_new, uhashlib_sha1_make_new, locals_dict, &uhashlib_sha1_locals_dict ); #endif @@ -349,7 +349,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( uhashlib_md5_type, MP_QSTR_md5, MP_TYPE_FLAG_NONE, - uhashlib_md5_make_new, + make_new, uhashlib_md5_make_new, locals_dict, &uhashlib_md5_locals_dict ); #endif // MICROPY_PY_UHASHLIB_MD5 diff --git a/extmod/modure.c b/extmod/modure.c index c0114c14fd57f..801e5df89779e 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -183,7 +183,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( match_type, MP_QSTR_match, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, match_print, locals_dict, &match_locals_dict ); @@ -417,7 +416,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( re_type, MP_QSTR_ure, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, re_print, locals_dict, &re_locals_dict ); diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 58bd1169a945e..128154a4b6d9a 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -340,7 +340,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_poll, MP_QSTR_poll, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, poll_iternext, locals_dict, &poll_locals_dict ); diff --git a/extmod/modusocket.c b/extmod/modusocket.c index fc16d7e270301..194ee6899ede3 100644 --- a/extmod/modusocket.c +++ b/extmod/modusocket.c @@ -532,7 +532,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( socket_type, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - socket_make_new, + make_new, socket_make_new, protocol, &socket_stream_p, locals_dict, &socket_locals_dict, print, socket_print diff --git a/extmod/modussl_axtls.c b/extmod/modussl_axtls.c index a6d606d560ebe..2eae46504835f 100644 --- a/extmod/modussl_axtls.c +++ b/extmod/modussl_axtls.c @@ -318,7 +318,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ussl_socket_type, MP_QSTR_ussl, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, ussl_socket_print, protocol, &ussl_socket_stream_p, diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index 50712980ba04f..95cf12c975af2 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -396,7 +396,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ussl_socket_type, MP_QSTR_ussl, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, // Save on qstr's, reuse same as for module print, socket_print, protocol, &ussl_socket_stream_p, diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index 1a7575adc9e58..1a38104eafcdc 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -213,7 +213,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( utimeq_type, MP_QSTR_utimeq, MP_TYPE_FLAG_NONE, - utimeq_make_new, + make_new, utimeq_make_new, unary_op, utimeq_unary_op, locals_dict, &utimeq_locals_dict ); diff --git a/extmod/moduwebsocket.c b/extmod/moduwebsocket.c index c6be50d0e15a3..9b12fc86358cd 100644 --- a/extmod/moduwebsocket.c +++ b/extmod/moduwebsocket.c @@ -294,7 +294,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( websocket_type, MP_QSTR_websocket, MP_TYPE_FLAG_NONE, - websocket_make_new, + make_new, websocket_make_new, protocol, &websocket_stream_p, locals_dict, &websocket_locals_dict ); diff --git a/extmod/moduzlib.c b/extmod/moduzlib.c index 533168d0b0543..14d15321a3c1e 100644 --- a/extmod/moduzlib.c +++ b/extmod/moduzlib.c @@ -144,7 +144,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( decompio_type, MP_QSTR_DecompIO, MP_TYPE_FLAG_NONE, - decompio_make_new, + make_new, decompio_make_new, protocol, &decompio_stream_p, locals_dict, &decompio_locals_dict ); diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index fc5ca35ea0338..d86f358962348 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -346,7 +346,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( webrepl_type, MP_QSTR__webrepl, MP_TYPE_FLAG_NONE, - webrepl_make_new, + make_new, webrepl_make_new, protocol, &webrepl_stream_p, locals_dict, &webrepl_locals_dict ); diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index 329ba53ef51a6..87d9b9f614282 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -500,7 +500,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_network_cyw43_type, MP_QSTR_CYW43, MP_TYPE_FLAG_NONE, - network_cyw43_make_new, + make_new, network_cyw43_make_new, print, network_cyw43_print, locals_dict, &network_cyw43_locals_dict ); diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 806819648d2ee..f4cc0b22244dd 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -159,7 +159,7 @@ STATIC mp_obj_t network_ninaw10_active(size_t n_args, const mp_obj_t *args) { MP_OBJ_NEW_QSTR(MP_QSTR_freq), MP_OBJ_NEW_SMALL_INT(10), MP_OBJ_NEW_QSTR(MP_QSTR_callback), MP_OBJ_FROM_PTR(&network_ninaw10_timer_callback_obj), }; - MP_STATE_PORT(mp_wifi_timer) = machine_timer_type.make_new((mp_obj_t)&machine_timer_type, 0, 2, timer_args); + MP_STATE_PORT(mp_wifi_timer) = MP_OBJ_TYPE_GET_SLOT(&machine_timer_type, make_new)((mp_obj_t)&machine_timer_type, 0, 2, timer_args); } } else { nina_deinit(); @@ -778,7 +778,7 @@ STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_nina_base, MP_QSTR_nina, MP_TYPE_FLAG_NONE, - network_ninaw10_make_new, + make_new, network_ninaw10_make_new, locals_dict, &nina_locals_dict ); diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index 82f836f691844..af2014508127c 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -708,7 +708,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size MP_ROM_QSTR(MP_QSTR_miso), mp_pin_make_new(NULL, 1, 0, &miso_obj), MP_ROM_QSTR(MP_QSTR_mosi), mp_pin_make_new(NULL, 1, 0, &mosi_obj), }; - spi = MP_OBJ_TO_PTR(machine_spi_type.make_new((mp_obj_t)&machine_spi_type, 2, 3, args)); + spi = MP_OBJ_TO_PTR(MP_OBJ_TYPE_GET_SLOT(&machine_spi_type, make_new)((mp_obj_t)&machine_spi_type, 2, 3, args)); cs = mp_hal_get_pin_obj(mp_pin_make_new(NULL, 1, 0, (mp_obj_t[]) {MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_PIN_CS)})); rst = mp_hal_get_pin_obj(mp_pin_make_new(NULL, 1, 0, (mp_obj_t[]) {MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIZNET_PIN_RST)})); @@ -1020,7 +1020,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mod_network_nic_type_wiznet5k, MP_QSTR_WIZNET5K, MP_TYPE_FLAG_NONE, - wiznet5k_make_new, + make_new, wiznet5k_make_new, locals_dict, &wiznet5k_locals_dict ); #else // WIZNET5K_PROVIDED_STACK @@ -1028,7 +1028,7 @@ STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_wiznet5k_base, MP_QSTR_WIZNET5K, MP_TYPE_FLAG_NONE, - wiznet5k_make_new, + make_new, wiznet5k_make_new, locals_dict, &wiznet5k_locals_dict ); diff --git a/extmod/vfs.c b/extmod/vfs.c index 00450e1005595..af63ceb37eb19 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -173,7 +173,7 @@ STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) { #if MICROPY_VFS_LFS1 if (memcmp(&buf[32], "littlefs", 8) == 0) { // LFS1 - mp_obj_t vfs = mp_type_vfs_lfs1.make_new(&mp_type_vfs_lfs1, 1, 0, &bdev_obj); + mp_obj_t vfs = MP_OBJ_TYPE_GET_SLOT(&mp_type_vfs_lfs1, make_new)(&mp_type_vfs_lfs1, 1, 0, &bdev_obj); nlr_pop(); return vfs; } @@ -181,7 +181,7 @@ STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) { #if MICROPY_VFS_LFS2 if (memcmp(&buf[0], "littlefs", 8) == 0) { // LFS2 - mp_obj_t vfs = mp_type_vfs_lfs2.make_new(&mp_type_vfs_lfs2, 1, 0, &bdev_obj); + mp_obj_t vfs = MP_OBJ_TYPE_GET_SLOT(&mp_type_vfs_lfs2, make_new)(&mp_type_vfs_lfs2, 1, 0, &bdev_obj); nlr_pop(); return vfs; } @@ -194,7 +194,7 @@ STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) { #endif #if MICROPY_VFS_FAT - return mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &bdev_obj); + return MP_OBJ_TYPE_GET_SLOT(&mp_fat_vfs_type, make_new)(&mp_fat_vfs_type, 1, 0, &bdev_obj); #endif // no filesystem found diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 7c18a51633746..efb6bf7e9815d 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -435,7 +435,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_fat_vfs_type, MP_QSTR_VfsFat, MP_TYPE_FLAG_NONE, - fat_vfs_make_new, + make_new, fat_vfs_make_new, protocol, &fat_vfs_proto, locals_dict, &fat_vfs_locals_dict ); diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index ca2e2b446f13d..07e6df9bf9595 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -180,7 +180,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_fat_fileio, MP_QSTR_FileIO, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, file_obj_print, protocol, &vfs_fat_fileio_stream_p, locals_dict, &vfs_fat_rawfile_locals_dict @@ -197,7 +196,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_fat_textio, MP_QSTR_TextIOWrapper, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, file_obj_print, protocol, &vfs_fat_textio_stream_p, locals_dict, &vfs_fat_rawfile_locals_dict diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 33e2ef551987a..d9617817f9565 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -512,7 +512,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx, VFS_LFSx_QSTR, MP_TYPE_FLAG_NONE, - MP_VFS_LFSx(make_new), + make_new, MP_VFS_LFSx(make_new), protocol, &MP_VFS_LFSx(proto), locals_dict, &MP_VFS_LFSx(locals_dict) ); diff --git a/extmod/vfs_lfsx_file.c b/extmod/vfs_lfsx_file.c index f97641b7b951a..2c87fd5b994c6 100644 --- a/extmod/vfs_lfsx_file.c +++ b/extmod/vfs_lfsx_file.c @@ -224,7 +224,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx_(_fileio), MP_QSTR_FileIO, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, MP_VFS_LFSx(file_print), protocol, &MP_VFS_LFSx(fileio_stream_p), locals_dict, &MP_VFS_LFSx(file_locals_dict) @@ -241,7 +240,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_VFS_LFSx_(_textio), MP_QSTR_TextIOWrapper, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, MP_VFS_LFSx(file_print), protocol, &MP_VFS_LFSx(textio_stream_p), locals_dict, &MP_VFS_LFSx(file_locals_dict) diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index b02827e8647c9..3694ebaf99743 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -402,7 +402,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_posix, MP_QSTR_VfsPosix, MP_TYPE_FLAG_NONE, - vfs_posix_make_new, + make_new, vfs_posix_make_new, protocol, &vfs_posix_proto, locals_dict, &vfs_posix_locals_dict ); diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 729d914d3a11a..4a37489686cd9 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -253,7 +253,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_posix_fileio, MP_QSTR_FileIO, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, vfs_posix_file_print, protocol, &vfs_posix_fileio_stream_p, locals_dict, &vfs_posix_rawfile_locals_dict @@ -270,7 +269,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_vfs_posix_textio, MP_QSTR_TextIOWrapper, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, vfs_posix_file_print, protocol, &vfs_posix_textio_stream_p, locals_dict, &vfs_posix_rawfile_locals_dict diff --git a/ports/cc3200/misc/mpirq.c b/ports/cc3200/misc/mpirq.c index 9c3c2f7196721..eb813fa4c6f2a 100644 --- a/ports/cc3200/misc/mpirq.c +++ b/ports/cc3200/misc/mpirq.c @@ -194,7 +194,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_irq_type, MP_QSTR_irq, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, call, mp_irq_call, locals_dict, &mp_irq_locals_dict ); diff --git a/ports/cc3200/mods/modnetwork.c b/ports/cc3200/mods/modnetwork.c index 0a72a1ab3256b..590e872683930 100644 --- a/ports/cc3200/mods/modnetwork.c +++ b/ports/cc3200/mods/modnetwork.c @@ -175,7 +175,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( network_server_type, MP_QSTR_Server, MP_TYPE_FLAG_NONE, - network_server_make_new, + make_new, network_server_make_new, locals_dict, &network_server_locals_dict ); #endif diff --git a/ports/cc3200/mods/moduhashlib.c b/ports/cc3200/mods/moduhashlib.c index 4a759d8ea5c56..302ff335ff862 100644 --- a/ports/cc3200/mods/moduhashlib.c +++ b/ports/cc3200/mods/moduhashlib.c @@ -181,7 +181,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( sha1_type, MP_QSTR_sha1, MP_TYPE_FLAG_NONE, - hash_make_new, + make_new, hash_make_new, locals_dict, &hash_locals_dict ); @@ -189,7 +189,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( sha256_type, MP_QSTR_sha256, MP_TYPE_FLAG_NONE, - hash_make_new, + make_new, hash_make_new, locals_dict, &hash_locals_dict ); diff --git a/ports/cc3200/mods/modusocket.c b/ports/cc3200/mods/modusocket.c index 55d504a709726..cd1489fb4ae5a 100644 --- a/ports/cc3200/mods/modusocket.c +++ b/ports/cc3200/mods/modusocket.c @@ -763,7 +763,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( socket_type, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - socket_make_new, + make_new, socket_make_new, protocol, &socket_stream_p, locals_dict, &socket_locals_dict ); diff --git a/ports/cc3200/mods/modwlan.c b/ports/cc3200/mods/modwlan.c index 1e82b07e0874c..6cf1cbee4e414 100644 --- a/ports/cc3200/mods/modwlan.c +++ b/ports/cc3200/mods/modwlan.c @@ -1289,7 +1289,7 @@ STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE( mod_network_nic_type_wlan_base, MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, - wlan_make_new, + make_new, wlan_make_new, locals_dict, &wlan_locals_dict ); diff --git a/ports/cc3200/mods/pybadc.c b/ports/cc3200/mods/pybadc.c index a14f9aced2618..74664282560d2 100644 --- a/ports/cc3200/mods/pybadc.c +++ b/ports/cc3200/mods/pybadc.c @@ -237,7 +237,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - adc_make_new, + make_new, adc_make_new, print, adc_print, locals_dict, &adc_locals_dict ); @@ -305,7 +305,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_adc_channel_type, MP_QSTR_ADCChannel, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, adc_channel_print, call, adc_channel_call, locals_dict, &adc_channel_locals_dict diff --git a/ports/cc3200/mods/pybflash.c b/ports/cc3200/mods/pybflash.c index a6d1e23fbf021..46b7be234f9cb 100644 --- a/ports/cc3200/mods/pybflash.c +++ b/ports/cc3200/mods/pybflash.c @@ -88,7 +88,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - pyb_flash_make_new, + make_new, pyb_flash_make_new, locals_dict, &pyb_flash_locals_dict ); diff --git a/ports/cc3200/mods/pybi2c.c b/ports/cc3200/mods/pybi2c.c index de92cc7c32b30..5d77dc8367c4a 100644 --- a/ports/cc3200/mods/pybi2c.c +++ b/ports/cc3200/mods/pybi2c.c @@ -525,7 +525,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - pyb_i2c_make_new, + make_new, pyb_i2c_make_new, print, pyb_i2c_print, locals_dict, &pyb_i2c_locals_dict ); diff --git a/ports/cc3200/mods/pybpin.c b/ports/cc3200/mods/pybpin.c index 374d09ddfbd77..f04ca756540e4 100644 --- a/ports/cc3200/mods/pybpin.c +++ b/ports/cc3200/mods/pybpin.c @@ -935,7 +935,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - pin_make_new, + make_new, pin_make_new, print, pin_print, call, pin_call, locals_dict, &pin_locals_dict @@ -957,7 +957,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, locals_dict, &pin_board_pins_locals_dict ); diff --git a/ports/cc3200/mods/pybrtc.c b/ports/cc3200/mods/pybrtc.c index ed7a20fecbf6c..21e729dbf175b 100644 --- a/ports/cc3200/mods/pybrtc.c +++ b/ports/cc3200/mods/pybrtc.c @@ -473,7 +473,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - pyb_rtc_make_new, + make_new, pyb_rtc_make_new, locals_dict, &pyb_rtc_locals_dict ); diff --git a/ports/cc3200/mods/pybsd.c b/ports/cc3200/mods/pybsd.c index 968a6a87ec4d7..209c3b5a85ecb 100644 --- a/ports/cc3200/mods/pybsd.c +++ b/ports/cc3200/mods/pybsd.c @@ -216,6 +216,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_sd_type, MP_QSTR_SD, MP_TYPE_FLAG_NONE, - pyb_sd_make_new, + make_new, pyb_sd_make_new, locals_dict, &pyb_sd_locals_dict ); diff --git a/ports/cc3200/mods/pybsleep.c b/ports/cc3200/mods/pybsleep.c index 423099991f8d1..ffb281e6b13dc 100644 --- a/ports/cc3200/mods/pybsleep.c +++ b/ports/cc3200/mods/pybsleep.c @@ -128,8 +128,7 @@ STATIC pybsleep_wake_reason_t pybsleep_wake_reason = PYB_SLP_WAKED_PWRON; STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_sleep_type, MP_QSTR_sleep, - MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW + MP_TYPE_FLAG_NONE ); /****************************************************************************** diff --git a/ports/cc3200/mods/pybspi.c b/ports/cc3200/mods/pybspi.c index 7d83fabfde4da..61086ec2e1d84 100644 --- a/ports/cc3200/mods/pybspi.c +++ b/ports/cc3200/mods/pybspi.c @@ -381,7 +381,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - pyb_spi_make_new, + make_new, pyb_spi_make_new, print, pyb_spi_print, locals_dict, &pyb_spi_locals_dict ); diff --git a/ports/cc3200/mods/pybtimer.c b/ports/cc3200/mods/pybtimer.c index 14e1deb083ed2..be365f3c92b26 100644 --- a/ports/cc3200/mods/pybtimer.c +++ b/ports/cc3200/mods/pybtimer.c @@ -463,7 +463,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - pyb_timer_make_new, + make_new, pyb_timer_make_new, print, pyb_timer_print, locals_dict, &pyb_timer_locals_dict ); @@ -726,7 +726,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_channel_type, MP_QSTR_TimerChannel, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, locals_dict, &pyb_timer_channel_locals_dict ); diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c index f92f544732209..424ca251ec792 100644 --- a/ports/cc3200/mods/pybuart.c +++ b/ports/cc3200/mods/pybuart.c @@ -689,7 +689,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - pyb_uart_make_new, + make_new, pyb_uart_make_new, print, pyb_uart_print, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict diff --git a/ports/cc3200/mods/pybwdt.c b/ports/cc3200/mods/pybwdt.c index 3c1f9eb195f10..589f53cf1275d 100644 --- a/ports/cc3200/mods/pybwdt.c +++ b/ports/cc3200/mods/pybwdt.c @@ -154,7 +154,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - pyb_wdt_make_new, + make_new, pyb_wdt_make_new, locals_dict, &pybwdt_locals_dict ); diff --git a/ports/esp32/esp32_nvs.c b/ports/esp32/esp32_nvs.c index d935a13d6fe5d..0b3661918c771 100644 --- a/ports/esp32/esp32_nvs.c +++ b/ports/esp32/esp32_nvs.c @@ -145,7 +145,7 @@ MP_DEFINE_CONST_OBJ_TYPE( esp32_nvs_type, MP_QSTR_NVS, MP_TYPE_FLAG_NONE, - esp32_nvs_make_new, + make_new, esp32_nvs_make_new, print, esp32_nvs_print, locals_dict, &esp32_nvs_locals_dict ); diff --git a/ports/esp32/esp32_partition.c b/ports/esp32/esp32_partition.c index 6ce1e90b4c700..17aa34e560e8a 100644 --- a/ports/esp32/esp32_partition.c +++ b/ports/esp32/esp32_partition.c @@ -288,7 +288,7 @@ MP_DEFINE_CONST_OBJ_TYPE( esp32_partition_type, MP_QSTR_Partition, MP_TYPE_FLAG_NONE, - esp32_partition_make_new, + make_new, esp32_partition_make_new, print, esp32_partition_print, locals_dict, &esp32_partition_locals_dict ); diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index ee09ac5200611..78c8c8aceeffb 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -376,7 +376,7 @@ MP_DEFINE_CONST_OBJ_TYPE( esp32_rmt_type, MP_QSTR_RMT, MP_TYPE_FLAG_NONE, - esp32_rmt_make_new, + make_new, esp32_rmt_make_new, print, esp32_rmt_print, locals_dict, &esp32_rmt_locals_dict ); diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 5030f980675ed..843bdb2366df2 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -95,7 +95,7 @@ MP_DEFINE_CONST_OBJ_TYPE( esp32_ulp_type, MP_QSTR_ULP, MP_TYPE_FLAG_NONE, - esp32_ulp_make_new, + make_new, esp32_ulp_make_new, locals_dict, &esp32_ulp_locals_dict ); diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index c4e04159c0b90..1e20186b97be9 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -260,7 +260,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - madc_make_new, + make_new, madc_make_new, print, madc_print, locals_dict, &madc_locals_dict ); diff --git a/ports/esp32/machine_adcblock.c b/ports/esp32/machine_adcblock.c index ae3244f7fdb4a..afe8fdea40b2a 100644 --- a/ports/esp32/machine_adcblock.c +++ b/ports/esp32/machine_adcblock.c @@ -198,7 +198,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adcblock_type, MP_QSTR_ADCBlock, MP_TYPE_FLAG_NONE, - madcblock_make_new, + make_new, madcblock_make_new, print, madcblock_print, locals_dict, &madcblock_locals_dict ); diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index fbe33b0344a47..0e85dc9c9bfb2 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -108,7 +108,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_dac_type, MP_QSTR_DAC, MP_TYPE_FLAG_NONE, - mdac_make_new, + make_new, mdac_make_new, print, mdac_print, locals_dict, &mdac_locals_dict ); diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 51ea31ac1b3db..05b1c871cb3cf 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -549,7 +549,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hw_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_hw_spi_make_new, + make_new, machine_hw_spi_make_new, print, machine_hw_spi_print, protocol, &machine_hw_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 895dc3a39874d..9244343dcf618 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -196,7 +196,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hw_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_hw_i2c_make_new, + make_new, machine_hw_i2c_make_new, print, machine_hw_i2c_print, protocol, &machine_hw_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index 6e18394cc5e56..eecf71549871f 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -833,7 +833,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_i2s_make_new, + make_new, machine_i2s_make_new, print, machine_i2s_print, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index fd523a38edacc..4f6f948d52af5 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -533,7 +533,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, @@ -728,7 +728,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( machine_pin_irq_type, MP_QSTR_IRQ, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, call, machine_pin_irq_call, locals_dict, &machine_pin_irq_locals_dict ); diff --git a/ports/esp32/machine_rtc.c b/ports/esp32/machine_rtc.c index 19b83703fd43f..3d620336c9a91 100644 --- a/ports/esp32/machine_rtc.c +++ b/ports/esp32/machine_rtc.c @@ -177,6 +177,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - machine_rtc_make_new, + make_new, machine_rtc_make_new, locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 0b6159157d980..801a26f378db9 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -403,7 +403,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_sdcard_type, MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, - machine_sdcard_make_new, + make_new, machine_sdcard_make_new, locals_dict, &machine_sdcard_locals_dict ); diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 2fd40fa2afc12..83fe81f6eee34 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -281,7 +281,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, print, machine_timer_print, locals_dict, &machine_timer_locals_dict ); diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index deba818dd1c87..ad1f6c9474cde 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -138,7 +138,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_touchpad_type, MP_QSTR_TouchPad, MP_TYPE_FLAG_NONE, - mtp_make_new, + make_new, mtp_make_new, locals_dict, &mtp_locals_dict ); diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 1f404154fc5ba..4cfa31b71de3b 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -534,7 +534,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_uart_make_new, + make_new, machine_uart_make_new, print, machine_uart_print, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index bda9c6975e152..4ccf417b60999 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -87,6 +87,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - machine_wdt_make_new, + make_new, machine_wdt_make_new, locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index e7e6f3c26e287..334c5bae33c4f 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -789,7 +789,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( socket_type, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - socket_make_new, + make_new, socket_make_new, protocol, &socket_stream_p, locals_dict, &socket_locals_dict ); diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index 3c5aea5fb8a4d..c57d7815d7cab 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -306,7 +306,6 @@ MP_DEFINE_CONST_OBJ_TYPE( lan_if_type, MP_QSTR_LAN, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &lan_if_locals_dict ); diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index df07515c7d411..703cdc7889fdc 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -282,6 +282,5 @@ MP_DEFINE_CONST_OBJ_TYPE( ppp_if_type, MP_QSTR_PPP, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &ppp_if_locals_dict ); diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 0f1f5de14937a..f0b458e6ddfe7 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -619,7 +619,6 @@ MP_DEFINE_CONST_OBJ_TYPE( wlan_if_type, MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &wlan_if_locals_dict ); diff --git a/ports/esp8266/machine_adc.c b/ports/esp8266/machine_adc.c index b1e7b38435276..f4fd32db9c765 100644 --- a/ports/esp8266/machine_adc.c +++ b/ports/esp8266/machine_adc.c @@ -93,7 +93,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - machine_adc_make_new, + make_new, machine_adc_make_new, print, machine_adc_print, locals_dict, &machine_adc_locals_dict ); diff --git a/ports/esp8266/machine_hspi.c b/ports/esp8266/machine_hspi.c index 3f449a1de91e1..2edb294adeff6 100644 --- a/ports/esp8266/machine_hspi.c +++ b/ports/esp8266/machine_hspi.c @@ -179,7 +179,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hspi_type, MP_QSTR_HSPI, MP_TYPE_FLAG_NONE, - machine_hspi_make_new, + make_new, machine_hspi_make_new, print, machine_hspi_print, protocol, &machine_hspi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c index 32ffca873dfaf..ea17728e2359b 100644 --- a/ports/esp8266/machine_pin.c +++ b/ports/esp8266/machine_pin.c @@ -454,7 +454,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, pyb_pin_print, call, pyb_pin_call, protocol, &pin_pin_p, @@ -514,7 +514,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pin_irq_type, MP_QSTR_IRQ, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, call, pin_irq_call, locals_dict, &pin_irq_locals_dict ); diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index cc6b79a031be7..d8cbb8f8247dc 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -266,6 +266,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - pyb_rtc_make_new, + make_new, pyb_rtc_make_new, locals_dict, &pyb_rtc_locals_dict ); diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c index af6231c21903b..873b12b2fab9c 100644 --- a/ports/esp8266/machine_uart.c +++ b/ports/esp8266/machine_uart.c @@ -347,7 +347,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - pyb_uart_make_new, + make_new, pyb_uart_make_new, print, pyb_uart_print, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index 26d5c9fa76f13..39a5d51119efb 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -73,6 +73,6 @@ MP_DEFINE_CONST_OBJ_TYPE( esp_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - machine_wdt_make_new, + make_new, machine_wdt_make_new, locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c index ded5e58abf7ab..583540a81c76b 100644 --- a/ports/esp8266/main.c +++ b/ports/esp8266/main.c @@ -68,7 +68,7 @@ STATIC void mp_reset(void) { mp_obj_t args[2]; args[0] = MP_OBJ_NEW_SMALL_INT(0); args[1] = MP_OBJ_NEW_SMALL_INT(115200); - args[0] = pyb_uart_type.make_new(&pyb_uart_type, 2, 0, args); + args[0] = MP_OBJ_TYPE_GET_SLOT(&pyb_uart_type, make_new)(&pyb_uart_type, 2, 0, args); args[1] = MP_OBJ_NEW_SMALL_INT(1); mp_uos_dupterm_obj.fun.var(2, args); } diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index eb41e30f66b12..af46cbbfe7478 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -341,7 +341,7 @@ MP_DEFINE_CONST_OBJ_TYPE( esp_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - esp_timer_make_new, + make_new, esp_timer_make_new, print, esp_timer_print, locals_dict, &esp_timer_locals_dict ); diff --git a/ports/esp8266/modnetwork.c b/ports/esp8266/modnetwork.c index 45a5a2be5412a..ec62528a08344 100644 --- a/ports/esp8266/modnetwork.c +++ b/ports/esp8266/modnetwork.c @@ -514,7 +514,6 @@ MP_DEFINE_CONST_OBJ_TYPE( wlan_if_type, MP_QSTR_WLAN, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &wlan_if_locals_dict ); diff --git a/ports/mimxrt/machine_adc.c b/ports/mimxrt/machine_adc.c index 7a19d1225ee73..d63157d36efba 100644 --- a/ports/mimxrt/machine_adc.c +++ b/ports/mimxrt/machine_adc.c @@ -121,7 +121,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - adc_obj_make_new, + make_new, adc_obj_make_new, print, adc_obj_print, locals_dict, &adc_locals_dict ); diff --git a/ports/mimxrt/machine_i2c.c b/ports/mimxrt/machine_i2c.c index 62dfd8204c081..f00ba6b1ba768 100644 --- a/ports/mimxrt/machine_i2c.c +++ b/ports/mimxrt/machine_i2c.c @@ -201,7 +201,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_i2c_make_new, + make_new, machine_i2c_make_new, print, machine_i2c_print, protocol, &machine_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/mimxrt/machine_i2s.c b/ports/mimxrt/machine_i2s.c index b6c630138b76b..1733140fb57ee 100644 --- a/ports/mimxrt/machine_i2s.c +++ b/ports/mimxrt/machine_i2s.c @@ -1217,7 +1217,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_i2s_make_new, + make_new, machine_i2s_make_new, print, machine_i2s_print, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict diff --git a/ports/mimxrt/machine_led.c b/ports/mimxrt/machine_led.c index 9fd98ef71043f..8dd74b32ba76d 100644 --- a/ports/mimxrt/machine_led.c +++ b/ports/mimxrt/machine_led.c @@ -84,7 +84,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - led_obj_make_new, + make_new, led_obj_make_new, print, led_obj_print, locals_dict, &led_locals_dict ); diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c index 261e3e4148034..836bd8524b729 100644 --- a/ports/mimxrt/machine_pin.c +++ b/ports/mimxrt/machine_pin.c @@ -62,7 +62,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_cpu_pins_obj_type, MP_QSTR_cpu, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &machine_pin_cpu_pins_locals_dict ); @@ -70,7 +69,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &machine_pin_board_pins_locals_dict ); @@ -424,7 +422,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_obj_print, call, machine_pin_obj_call, protocol, &machine_pin_obj_protocol, @@ -436,7 +434,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_af_type, MP_QSTR_PinAF, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_obj_print, locals_dict, &machine_pin_locals_dict ); diff --git a/ports/mimxrt/machine_rtc.c b/ports/mimxrt/machine_rtc.c index 2e1a09dedbe6f..da7a437491f72 100644 --- a/ports/mimxrt/machine_rtc.c +++ b/ports/mimxrt/machine_rtc.c @@ -170,6 +170,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - machine_rtc_make_new, + make_new, machine_rtc_make_new, locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/mimxrt/machine_sdcard.c b/ports/mimxrt/machine_sdcard.c index 22f7e7c232a8e..9254faf14a441 100644 --- a/ports/mimxrt/machine_sdcard.c +++ b/ports/mimxrt/machine_sdcard.c @@ -212,7 +212,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_sdcard_type, MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, - sdcard_obj_make_new, + make_new, sdcard_obj_make_new, locals_dict, &sdcard_locals_dict ); diff --git a/ports/mimxrt/machine_spi.c b/ports/mimxrt/machine_spi.c index 93b75e931eb48..a3a6bb414edde 100644 --- a/ports/mimxrt/machine_spi.c +++ b/ports/mimxrt/machine_spi.c @@ -255,7 +255,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_spi_make_new, + make_new, machine_spi_make_new, print, machine_spi_print, protocol, &machine_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/mimxrt/machine_timer.c b/ports/mimxrt/machine_timer.c index a6b61982f7ec0..a237272390ce4 100644 --- a/ports/mimxrt/machine_timer.c +++ b/ports/mimxrt/machine_timer.c @@ -215,7 +215,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, print, machine_timer_print, locals_dict, &machine_timer_locals_dict ); diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index e93d2478f3ff6..a0706c8f4fcae 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -473,7 +473,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_uart_make_new, + make_new, machine_uart_make_new, print, machine_uart_print, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict diff --git a/ports/mimxrt/machine_wdt.c b/ports/mimxrt/machine_wdt.c index e0b5c74b54268..f5f14f9ef7b20 100644 --- a/ports/mimxrt/machine_wdt.c +++ b/ports/mimxrt/machine_wdt.c @@ -103,6 +103,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - machine_wdt_make_new, + make_new, machine_wdt_make_new, locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/mimxrt/mimxrt_flash.c b/ports/mimxrt/mimxrt_flash.c index bd03c853d597d..fd2e7558f9f8d 100644 --- a/ports/mimxrt/mimxrt_flash.c +++ b/ports/mimxrt/mimxrt_flash.c @@ -219,6 +219,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mimxrt_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - mimxrt_flash_make_new, + make_new, mimxrt_flash_make_new, locals_dict, &mimxrt_flash_locals_dict ); diff --git a/ports/mimxrt/network_lan.c b/ports/mimxrt/network_lan.c index e15894294b68c..0842cde82d00e 100644 --- a/ports/mimxrt/network_lan.c +++ b/ports/mimxrt/network_lan.c @@ -224,7 +224,7 @@ MP_DEFINE_CONST_OBJ_TYPE( network_lan_type, MP_QSTR_LAN, MP_TYPE_FLAG_NONE, - network_lan_make_new, + make_new, network_lan_make_new, print, network_lan_print, locals_dict, &network_lan_locals_dict ); diff --git a/ports/nrf/boards/microbit/modules/iters.c b/ports/nrf/boards/microbit/modules/iters.c index 2fe14866914fd..e6762421eb579 100644 --- a/ports/nrf/boards/microbit/modules/iters.c +++ b/ports/nrf/boards/microbit/modules/iters.c @@ -47,7 +47,6 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_repeat_iterator_type, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, microbit_repeat_iter_next ); diff --git a/ports/nrf/boards/microbit/modules/microbitdisplay.c b/ports/nrf/boards/microbit/modules/microbitdisplay.c index 5cb25ea1ced48..e4e4d13fe4496 100644 --- a/ports/nrf/boards/microbit/modules/microbitdisplay.c +++ b/ports/nrf/boards/microbit/modules/microbitdisplay.c @@ -546,7 +546,6 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_display_type, MP_QSTR_MicroBitDisplay, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, µbit_display_locals_dict ); diff --git a/ports/nrf/boards/microbit/modules/microbitimage.c b/ports/nrf/boards/microbit/modules/microbitimage.c index b22c2e29acec9..17d737dba5e5d 100644 --- a/ports/nrf/boards/microbit/modules/microbitimage.c +++ b/ports/nrf/boards/microbit/modules/microbitimage.c @@ -682,7 +682,7 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_image_type, MP_QSTR_MicroBitImage, MP_TYPE_FLAG_NONE, - microbit_image_make_new, + make_new, microbit_image_make_new, print, microbit_image_print, binary_op, image_binary_op, locals_dict, µbit_image_locals_dict @@ -832,7 +832,6 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_scrolling_string_iterator_type, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, microbit_scrolling_string_iter_next ); @@ -912,7 +911,6 @@ MP_DEFINE_CONST_OBJ_TYPE( microbit_facade_iterator_type, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, microbit_facade_iter_next ); diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 83466626fca07..bcfaafd39c7e0 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -170,7 +170,7 @@ int main(int argc, char **argv) { MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_NEW_SMALL_INT(115200), }; - MP_STATE_PORT(board_stdio_uart) = machine_hard_uart_type.make_new((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args); + MP_STATE_PORT(board_stdio_uart) = MP_OBJ_TYPE_GET_SLOT(&machine_hard_uart_type, make_new)((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args); } #endif diff --git a/ports/nrf/modules/board/led.c b/ports/nrf/modules/board/led.c index 577c2b62843f4..57065a48517a2 100644 --- a/ports/nrf/modules/board/led.c +++ b/ports/nrf/modules/board/led.c @@ -198,7 +198,7 @@ MP_DEFINE_CONST_OBJ_TYPE( board_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - led_obj_make_new, + make_new, led_obj_make_new, print, led_obj_print, locals_dict, &led_locals_dict ); diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index 84db8d259f2ac..df9d23465a8d3 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -298,7 +298,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - machine_adc_make_new, + make_new, machine_adc_make_new, locals_dict, &machine_adc_locals_dict, print, machine_adc_print ); diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index 7cb55d078821c..c16c3669ec577 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -165,7 +165,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_hard_i2c_make_new, + make_new, machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/nrf/modules/machine/pin.c b/ports/nrf/modules/machine/pin.c index 4f283e5dba149..db5cc9cbb1360 100644 --- a/ports/nrf/modules/machine/pin.c +++ b/ports/nrf/modules/machine/pin.c @@ -600,7 +600,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - pin_make_new, + make_new, pin_make_new, print, pin_print, call, pin_call, locals_dict, &pin_locals_dict @@ -676,7 +676,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_af_type, MP_QSTR_PinAF, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pin_af_obj_print, locals_dict, &pin_af_locals_dict ); diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index 54e643ec55ffd..862e1907cbf3a 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -343,7 +343,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_pwm_type, MP_QSTR_PWM, MP_TYPE_FLAG_NONE, - machine_pwm_make_new, + make_new, machine_pwm_make_new, print, machine_pwm_print, locals_dict, &machine_pwm_locals_dict ); diff --git a/ports/nrf/modules/machine/rtcounter.c b/ports/nrf/modules/machine/rtcounter.c index cafe08b6c6843..d52ca3e9a3366 100644 --- a/ports/nrf/modules/machine/rtcounter.c +++ b/ports/nrf/modules/machine/rtcounter.c @@ -266,7 +266,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_rtcounter_type, MP_QSTR_RTCounter, MP_TYPE_FLAG_NONE, - machine_rtc_make_new, + make_new, machine_rtc_make_new, print, rtc_print, locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c index d5613a4643246..fd7f7bb5bcced 100644 --- a/ports/nrf/modules/machine/spi.c +++ b/ports/nrf/modules/machine/spi.c @@ -431,7 +431,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_spi_make_new, + make_new, machine_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, locals_dict, &machine_spi_locals_dict diff --git a/ports/nrf/modules/machine/temp.c b/ports/nrf/modules/machine/temp.c index 00d6329b6498c..dff15a9d14974 100644 --- a/ports/nrf/modules/machine/temp.c +++ b/ports/nrf/modules/machine/temp.c @@ -116,7 +116,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_temp_type, MP_QSTR_Temp, MP_TYPE_FLAG_NONE, - machine_temp_make_new, + make_new, machine_temp_make_new, locals_dict, &machine_temp_locals_dict, print, machine_temp_print ); diff --git a/ports/nrf/modules/machine/timer.c b/ports/nrf/modules/machine/timer.c index f7f6101726a6e..091e6d0e32ffc 100644 --- a/ports/nrf/modules/machine/timer.c +++ b/ports/nrf/modules/machine/timer.c @@ -238,7 +238,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, print, timer_print, locals_dict, &machine_timer_locals_dict ); diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index ca0fcf859a55a..035a30df9c015 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -374,7 +374,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_hard_uart_make_new, + make_new, machine_hard_uart_make_new, print, machine_hard_uart_print, protocol, &uart_stream_p, locals_dict, &machine_hard_uart_locals_dict diff --git a/ports/nrf/modules/nrf/flashbdev.c b/ports/nrf/modules/nrf/flashbdev.c index 84a3dd2520b44..e92919cac5a22 100644 --- a/ports/nrf/modules/nrf/flashbdev.c +++ b/ports/nrf/modules/nrf/flashbdev.c @@ -187,7 +187,7 @@ MP_DEFINE_CONST_OBJ_TYPE( nrf_flashbdev_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - nrf_flashbdev_make_new, + make_new, nrf_flashbdev_make_new, print, nrf_flashbdev_print, locals_dict, &nrf_flashbdev_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c index 2be7dab9d3f7c..7b9e3af6a3174 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c +++ b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c @@ -213,7 +213,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_characteristic_type, MP_QSTR_Characteristic, MP_TYPE_FLAG_NONE, - ubluepy_characteristic_make_new, + make_new, ubluepy_characteristic_make_new, print, ubluepy_characteristic_print, locals_dict, &ubluepy_characteristic_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_constants.c b/ports/nrf/modules/ubluepy/ubluepy_constants.c index c6c399924573d..cad9adbb03256 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_constants.c +++ b/ports/nrf/modules/ubluepy/ubluepy_constants.c @@ -73,7 +73,6 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_constants_ad_types_type, MP_QSTR_ad_types, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &ubluepy_constants_ad_types_locals_dict ); @@ -96,7 +95,6 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_constants_type, MP_QSTR_constants, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &ubluepy_constants_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_delegate.c b/ports/nrf/modules/ubluepy/ubluepy_delegate.c index dd19f70be445b..43720b4186b0a 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_delegate.c +++ b/ports/nrf/modules/ubluepy/ubluepy_delegate.c @@ -81,7 +81,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_delegate_type, MP_QSTR_DefaultDelegate, MP_TYPE_FLAG_NONE, - ubluepy_delegate_make_new, + make_new, ubluepy_delegate_make_new, print, ubluepy_delegate_print, locals_dict, &ubluepy_delegate_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c index d942d98223067..062b4210941f7 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_descriptor.c +++ b/ports/nrf/modules/ubluepy/ubluepy_descriptor.c @@ -74,7 +74,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_descriptor_type, MP_QSTR_Descriptor, MP_TYPE_FLAG_NONE, - ubluepy_descriptor_make_new, + make_new, ubluepy_descriptor_make_new, print, ubluepy_descriptor_print, locals_dict, &ubluepy_descriptor_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c index 9f638b45d7359..d2a9e0011d124 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c +++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c @@ -486,7 +486,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_peripheral_type, MP_QSTR_Peripheral, MP_TYPE_FLAG_NONE, - ubluepy_peripheral_make_new, + make_new, ubluepy_peripheral_make_new, print, ubluepy_peripheral_print, locals_dict, &ubluepy_peripheral_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c index 89ca65d18c800..2dd4f57860fc9 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scan_entry.c @@ -140,7 +140,6 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_scan_entry_type, MP_QSTR_ScanEntry, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, ubluepy_scan_entry_print, locals_dict, &ubluepy_scan_entry_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_scanner.c b/ports/nrf/modules/ubluepy/ubluepy_scanner.c index b56ec349d0430..ffb7e946717b2 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_scanner.c +++ b/ports/nrf/modules/ubluepy/ubluepy_scanner.c @@ -118,7 +118,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_scanner_type, MP_QSTR_Scanner, MP_TYPE_FLAG_NONE, - ubluepy_scanner_make_new, + make_new, ubluepy_scanner_make_new, print, ubluepy_scanner_print, locals_dict, &ubluepy_scanner_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_service.c b/ports/nrf/modules/ubluepy/ubluepy_service.c index 2bec6befd6894..bf336d04c58ab 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_service.c +++ b/ports/nrf/modules/ubluepy/ubluepy_service.c @@ -175,7 +175,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_service_type, MP_QSTR_Service, MP_TYPE_FLAG_NONE, - ubluepy_service_make_new, + make_new, ubluepy_service_make_new, print, ubluepy_service_print, locals_dict, &ubluepy_service_locals_dict ); diff --git a/ports/nrf/modules/ubluepy/ubluepy_uuid.c b/ports/nrf/modules/ubluepy/ubluepy_uuid.c index 0fd6e75e5b628..abfe3cadd525b 100644 --- a/ports/nrf/modules/ubluepy/ubluepy_uuid.c +++ b/ports/nrf/modules/ubluepy/ubluepy_uuid.c @@ -164,7 +164,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ubluepy_uuid_type, MP_QSTR_UUID, MP_TYPE_FLAG_NONE, - ubluepy_uuid_make_new, + make_new, ubluepy_uuid_make_new, print, ubluepy_uuid_print, locals_dict, &ubluepy_uuid_locals_dict ); diff --git a/ports/nrf/modules/uos/microbitfs.c b/ports/nrf/modules/uos/microbitfs.c index d1b320111607b..6d697e1d1a0d1 100644 --- a/ports/nrf/modules/uos/microbitfs.c +++ b/ports/nrf/modules/uos/microbitfs.c @@ -630,7 +630,6 @@ MP_DEFINE_CONST_OBJ_TYPE( uos_mbfs_textio_type, MP_QSTR_TextIO, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, protocol, &textio_stream_p, locals_dict, &uos_mbfs_file_locals_dict ); @@ -645,7 +644,6 @@ MP_DEFINE_CONST_OBJ_TYPE( uos_mbfs_fileio_type, MP_QSTR_FileIO, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, protocol, &fileio_stream_p, locals_dict, &uos_mbfs_file_locals_dict ); diff --git a/ports/nrf/pin_named_pins.c b/ports/nrf/pin_named_pins.c index 84ec89f44aa69..30e60692f7410 100644 --- a/ports/nrf/pin_named_pins.c +++ b/ports/nrf/pin_named_pins.c @@ -40,7 +40,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_cpu_pins_obj_type, MP_QSTR_cpu, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, locals_dict, &pin_cpu_pins_locals_dict ); @@ -49,7 +48,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pin_named_pins_obj_print, locals_dict, &pin_board_pins_locals_dict ); diff --git a/ports/pic16bit/modpybled.c b/ports/pic16bit/modpybled.c index 2e5c2dcca3b1b..47e83e0409215 100644 --- a/ports/pic16bit/modpybled.c +++ b/ports/pic16bit/modpybled.c @@ -88,7 +88,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - pyb_led_make_new, + make_new, pyb_led_make_new, print, pyb_led_print, locals_dict, &pyb_led_locals_dict ); diff --git a/ports/pic16bit/modpybswitch.c b/ports/pic16bit/modpybswitch.c index f27cfb9b093af..b7192c5bba69b 100644 --- a/ports/pic16bit/modpybswitch.c +++ b/ports/pic16bit/modpybswitch.c @@ -75,7 +75,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_switch_type, MP_QSTR_Switch, MP_TYPE_FLAG_NONE, - pyb_switch_make_new, + make_new, pyb_switch_make_new, print, pyb_switch_print, call, pyb_switch_call, locals_dict, &pyb_switch_locals_dict diff --git a/ports/renesas-ra/extint.c b/ports/renesas-ra/extint.c index 496a50c3e827a..915c23e3cd853 100644 --- a/ports/renesas-ra/extint.c +++ b/ports/renesas-ra/extint.c @@ -378,7 +378,7 @@ MP_DEFINE_CONST_OBJ_TYPE( extint_type, MP_QSTR_ExtInt, MP_TYPE_FLAG_NONE, - extint_make_new, + make_new, extint_make_new, locals_dict, &extint_locals_dict, print, extint_obj_print ); diff --git a/ports/renesas-ra/led.c b/ports/renesas-ra/led.c index 9a3f1d40e9651..a2284c5339b94 100644 --- a/ports/renesas-ra/led.c +++ b/ports/renesas-ra/led.c @@ -174,7 +174,7 @@ MP_DEFINE_CONST_OBJ_TYPE( ra_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - led_obj_make_new, + make_new, led_obj_make_new, locals_dict, &led_locals_dict, print, led_obj_print ); diff --git a/ports/renesas-ra/machine_adc.c b/ports/renesas-ra/machine_adc.c index 99e35f48d997f..b71c3db4b0256 100644 --- a/ports/renesas-ra/machine_adc.c +++ b/ports/renesas-ra/machine_adc.c @@ -130,7 +130,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - machine_adc_make_new, + make_new, machine_adc_make_new, locals_dict, &machine_adc_locals_dict, print, machine_adc_print ); diff --git a/ports/renesas-ra/machine_i2c.c b/ports/renesas-ra/machine_i2c.c index 563ea8787e523..16bd589964983 100644 --- a/ports/renesas-ra/machine_i2c.c +++ b/ports/renesas-ra/machine_i2c.c @@ -160,7 +160,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_i2c_make_new, + make_new, machine_i2c_make_new, locals_dict, &mp_machine_i2c_locals_dict, print, machine_i2c_print, protocol, &machine_i2c_p diff --git a/ports/renesas-ra/machine_pin.c b/ports/renesas-ra/machine_pin.c index 17ef1e19a4137..0e393b64e5543 100644 --- a/ports/renesas-ra/machine_pin.c +++ b/ports/renesas-ra/machine_pin.c @@ -353,7 +353,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, locals_dict, &machine_pin_locals_dict, print, machine_pin_print, call, machine_pin_call, @@ -393,7 +393,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_cpu_pins_obj_type, MP_QSTR_cpu, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &pin_cpu_pins_locals_dict ); @@ -401,7 +400,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &pin_board_pins_locals_dict ); diff --git a/ports/renesas-ra/machine_rtc.c b/ports/renesas-ra/machine_rtc.c index c3f64c6971088..e699bea0bcc08 100644 --- a/ports/renesas-ra/machine_rtc.c +++ b/ports/renesas-ra/machine_rtc.c @@ -345,6 +345,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - machine_rtc_make_new, + make_new, machine_rtc_make_new, locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/renesas-ra/machine_spi.c b/ports/renesas-ra/machine_spi.c index d0e8b03bd12ea..b9f5b1ad1b4eb 100644 --- a/ports/renesas-ra/machine_spi.c +++ b/ports/renesas-ra/machine_spi.c @@ -301,7 +301,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_hard_spi_make_new, + make_new, machine_hard_spi_make_new, locals_dict, &mp_machine_spi_locals_dict, print, machine_hard_spi_print, protocol, &machine_hard_spi_p diff --git a/ports/renesas-ra/machine_timer.c b/ports/renesas-ra/machine_timer.c index f3e5aafb7cab2..0f6ff80d27f46 100644 --- a/ports/renesas-ra/machine_timer.c +++ b/ports/renesas-ra/machine_timer.c @@ -140,7 +140,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, locals_dict, &machine_timer_locals_dict, print, machine_timer_print ); diff --git a/ports/renesas-ra/machine_uart.c b/ports/renesas-ra/machine_uart.c index 6fa84ca821e4e..86505ea0b953c 100644 --- a/ports/renesas-ra/machine_uart.c +++ b/ports/renesas-ra/machine_uart.c @@ -575,7 +575,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_uart_make_new, + make_new, machine_uart_make_new, locals_dict, &machine_uart_locals_dict, print, machine_uart_print, protocol, &uart_stream_p diff --git a/ports/renesas-ra/main.c b/ports/renesas-ra/main.c index a87ddbd868040..d403dbacbe432 100644 --- a/ports/renesas-ra/main.c +++ b/ports/renesas-ra/main.c @@ -182,7 +182,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { if (len != -1) { // Detected a littlefs filesystem so create correct block device for it mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR_len), MP_OBJ_NEW_SMALL_INT(len) }; - bdev = pyb_flash_type.make_new(&pyb_flash_type, 0, 1, args); + bdev = MP_OBJ_TYPE_GET_SLOT(&pyb_flash_type, make_new)(&pyb_flash_type, 0, 1, args); } #endif diff --git a/ports/renesas-ra/storage.c b/ports/renesas-ra/storage.c index f573894a50148..18dff9780078c 100644 --- a/ports/renesas-ra/storage.c +++ b/ports/renesas-ra/storage.c @@ -404,7 +404,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - pyb_flash_make_new, + make_new, pyb_flash_make_new, locals_dict, &pyb_flash_locals_dict, print, pyb_flash_print ); diff --git a/ports/renesas-ra/timer.c b/ports/renesas-ra/timer.c index 8f7acbcdaa476..04bd752b98cc7 100644 --- a/ports/renesas-ra/timer.c +++ b/ports/renesas-ra/timer.c @@ -414,7 +414,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - pyb_timer_make_new, + make_new, pyb_timer_make_new, locals_dict, &pyb_timer_locals_dict, print, pyb_timer_print ); @@ -507,7 +507,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_channel_type, MP_QSTR_TimerChannel, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &pyb_timer_channel_locals_dict, print, pyb_timer_channel_print ); diff --git a/ports/renesas-ra/usrsw.c b/ports/renesas-ra/usrsw.c index 4107d7850d382..edbe519683013 100644 --- a/ports/renesas-ra/usrsw.c +++ b/ports/renesas-ra/usrsw.c @@ -139,7 +139,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_switch_type, MP_QSTR_Switch, MP_TYPE_FLAG_NONE, - pyb_switch_make_new, + make_new, pyb_switch_make_new, locals_dict, &pyb_switch_locals_dict, print, pyb_switch_print, call, pyb_switch_call diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index 85562d5c07583..9e3d7f57a86eb 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -117,7 +117,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - machine_adc_make_new, + make_new, machine_adc_make_new, print, machine_adc_print, locals_dict, &machine_adc_locals_dict ); diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index 5ab93f63581d6..00dc36a4e0cb2 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -180,7 +180,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hw_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_i2c_make_new, + make_new, machine_i2c_make_new, print, machine_i2c_print, protocol, &machine_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/rp2/machine_i2s.c b/ports/rp2/machine_i2s.c index 53ff7417d79a7..d5175471ae809 100644 --- a/ports/rp2/machine_i2s.c +++ b/ports/rp2/machine_i2s.c @@ -1141,7 +1141,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_i2s_make_new, + make_new, machine_i2s_make_new, print, machine_i2s_print, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict diff --git a/ports/rp2/machine_pin.c b/ports/rp2/machine_pin.c index 8ccfbca3ab426..9abcf064e74eb 100644 --- a/ports/rp2/machine_pin.c +++ b/ports/rp2/machine_pin.c @@ -645,7 +645,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, diff --git a/ports/rp2/machine_rtc.c b/ports/rp2/machine_rtc.c index 6b2c9ac71b65d..a9d3426cf8382 100644 --- a/ports/rp2/machine_rtc.c +++ b/ports/rp2/machine_rtc.c @@ -119,6 +119,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - machine_rtc_make_new, + make_new, machine_rtc_make_new, locals_dict, &machine_rtc_locals_dict ); diff --git a/ports/rp2/machine_spi.c b/ports/rp2/machine_spi.c index 08c79712cf226..b2b879c9a8ed1 100644 --- a/ports/rp2/machine_spi.c +++ b/ports/rp2/machine_spi.c @@ -294,7 +294,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_spi_make_new, + make_new, machine_spi_make_new, print, machine_spi_print, protocol, &machine_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/rp2/machine_timer.c b/ports/rp2/machine_timer.c index 66f632329c723..e4fbb03af437c 100644 --- a/ports/rp2/machine_timer.c +++ b/ports/rp2/machine_timer.c @@ -160,7 +160,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, print, machine_timer_print, locals_dict, &machine_timer_locals_dict ); diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 06f7e9aaaca2c..e4881cd4f1173 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -583,7 +583,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_uart_make_new, + make_new, machine_uart_make_new, print, machine_uart_print, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c index c224298d136be..6574a6180f8a2 100644 --- a/ports/rp2/machine_wdt.c +++ b/ports/rp2/machine_wdt.c @@ -81,6 +81,6 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - machine_wdt_make_new, + make_new, machine_wdt_make_new, locals_dict, &machine_wdt_locals_dict ); diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index e37167a974dee..8840fe52f124e 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -103,7 +103,7 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { // This is a statically-allocated UART (see machine_uart.c), and doesn't // contain any heap pointers other than the ringbufs (which are already // root pointers), so no need to track this as a root pointer. - mp_bthci_uart = machine_uart_type.make_new((mp_obj_t)&machine_uart_type, 2, 2, args); + mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 2, 2, args); // Start the HCI polling to process any initial events/packets. mp_bluetooth_hci_start_polling(); diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index df49c881c4a41..0a94b6cc9065e 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -189,6 +189,6 @@ MP_DEFINE_CONST_OBJ_TYPE( rp2_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - rp2_flash_make_new, + make_new, rp2_flash_make_new, locals_dict, &rp2_flash_locals_dict ); diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index cfba9cce5ae01..87deaf7e5f8b2 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -380,7 +380,7 @@ MP_DEFINE_CONST_OBJ_TYPE( rp2_pio_type, MP_QSTR_PIO, MP_TYPE_FLAG_NONE, - rp2_pio_make_new, + make_new, rp2_pio_make_new, print, rp2_pio_print, locals_dict, &rp2_pio_locals_dict ); @@ -811,7 +811,7 @@ MP_DEFINE_CONST_OBJ_TYPE( rp2_state_machine_type, MP_QSTR_StateMachine, MP_TYPE_FLAG_NONE, - rp2_state_machine_make_new, + make_new, rp2_state_machine_make_new, print, rp2_state_machine_print, locals_dict, &rp2_state_machine_locals_dict ); diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 7a9b2299ae3b9..03b47e7cf2aac 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -166,7 +166,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - mp_led_make_new, + make_new, mp_led_make_new, print, machine_led_print, call, machine_led_call, locals_dict, &machine_led_locals_dict diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 75e1a2356c4d2..5f9cbfb99b85e 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -291,7 +291,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_print, call, machine_pin_call, protocol, &pin_pin_p, diff --git a/ports/samd/samd_flash.c b/ports/samd/samd_flash.c index 9d64768cfa020..522522ef84738 100644 --- a/ports/samd/samd_flash.c +++ b/ports/samd/samd_flash.c @@ -185,6 +185,6 @@ MP_DEFINE_CONST_OBJ_TYPE( samd_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - samd_flash_make_new, + make_new, samd_flash_make_new, locals_dict, &samd_flash_locals_dict ); diff --git a/ports/stm32/accel.c b/ports/stm32/accel.c index 05fd1898bc11d..19e16831ead84 100644 --- a/ports/stm32/accel.c +++ b/ports/stm32/accel.c @@ -285,7 +285,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_accel_type, MP_QSTR_Accel, MP_TYPE_FLAG_NONE, - pyb_accel_make_new, + make_new, pyb_accel_make_new, locals_dict, &pyb_accel_locals_dict ); diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index e0759439d1ece..712e9b3adeb2f 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -707,7 +707,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - adc_make_new, + make_new, adc_make_new, print, adc_print, locals_dict, &adc_locals_dict ); @@ -916,7 +916,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_adc_all_type, MP_QSTR_ADCAll, MP_TYPE_FLAG_NONE, - adc_all_make_new, + make_new, adc_all_make_new, locals_dict, &adc_all_locals_dict ); diff --git a/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c b/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c index 50ba81c57ac15..6ec20935f001a 100644 --- a/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c +++ b/ports/stm32/boards/LEGO_HUB_NO6/cc2564.c @@ -63,7 +63,7 @@ int mp_bluetooth_hci_controller_init(void) { // tim_ch = tim.channel(TIM_CH, pyb.Timer.PWM, pin=btclk) // tim_ch.pulse_width_percent(50) mp_obj_t args[6] = { MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM), MP_OBJ_NEW_QSTR(MP_QSTR_freq), MP_OBJ_NEW_SMALL_INT(32768), MP_OBJ_NULL }; - mp_obj_t tim = pyb_timer_type.make_new(&pyb_timer_type, 1, 1, args); + mp_obj_t tim = MP_OBJ_TYPE_GET_SLOT(&pyb_timer_type, make_new)(&pyb_timer_type, 1, 1, args); mp_load_method(tim, MP_QSTR_channel, args); args[2] = MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM_CH); args[3] = MP_OBJ_NEW_SMALL_INT(0); // CHANNEL_MODE_PWM_NORMAL diff --git a/ports/stm32/dac.c b/ports/stm32/dac.c index ac8d76090ee04..36ef9387f8737 100644 --- a/ports/stm32/dac.c +++ b/ports/stm32/dac.c @@ -506,7 +506,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_dac_type, MP_QSTR_DAC, MP_TYPE_FLAG_NONE, - pyb_dac_make_new, + make_new, pyb_dac_make_new, print, pyb_dac_print, locals_dict, &pyb_dac_locals_dict ); diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index 0f28610cc3e7e..ca23261ca736e 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -663,7 +663,7 @@ MP_DEFINE_CONST_OBJ_TYPE( extint_type, MP_QSTR_ExtInt, MP_TYPE_FLAG_NONE, - extint_make_new, + make_new, extint_make_new, print, extint_obj_print, locals_dict, &extint_locals_dict ); diff --git a/ports/stm32/lcd.c b/ports/stm32/lcd.c index 5017a68fe5489..e5d7f30efbd02 100644 --- a/ports/stm32/lcd.c +++ b/ports/stm32/lcd.c @@ -529,7 +529,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_lcd_type, MP_QSTR_LCD, MP_TYPE_FLAG_NONE, - pyb_lcd_make_new, + make_new, pyb_lcd_make_new, locals_dict, &pyb_lcd_locals_dict ); diff --git a/ports/stm32/led.c b/ports/stm32/led.c index 6e3229ab32180..fa211f1e145b4 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -385,7 +385,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - led_obj_make_new, + make_new, led_obj_make_new, print, led_obj_print, locals_dict, &led_locals_dict ); diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index 3659073d06bf0..000b478f86b04 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -496,7 +496,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_adc_type, MP_QSTR_ADC, MP_TYPE_FLAG_NONE, - machine_adc_make_new, + make_new, machine_adc_make_new, print, machine_adc_print, locals_dict, &machine_adc_locals_dict ); diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 7927daac1bdd7..9a2d338f54982 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -240,7 +240,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_hard_i2c_make_new, + make_new, machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/stm32/machine_i2s.c b/ports/stm32/machine_i2s.c index d68648bc348a5..83ac6bc124eb7 100644 --- a/ports/stm32/machine_i2s.c +++ b/ports/stm32/machine_i2s.c @@ -1118,7 +1118,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_i2s_type, MP_QSTR_I2S, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_i2s_make_new, + make_new, machine_i2s_make_new, print, machine_i2s_print, protocol, &i2s_stream_p, locals_dict, &machine_i2s_locals_dict diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index d64ff2af8f761..718ae1af5e5a6 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -139,7 +139,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_hard_spi_make_new, + make_new, machine_hard_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index 2e0120ea8191d..640d1d200fa50 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -140,7 +140,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - machine_timer_make_new, + make_new, machine_timer_make_new, print, machine_timer_print, locals_dict, &machine_timer_locals_dict ); diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 4ccff8c136ada..bb35bac5cc953 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -664,7 +664,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - pyb_uart_make_new, + make_new, pyb_uart_make_new, print, pyb_uart_print, protocol, &uart_stream_p, locals_dict, &pyb_uart_locals_dict diff --git a/ports/stm32/main.c b/ports/stm32/main.c index c001234704482..7f864a018c2ac 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -182,7 +182,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { if (len != -1) { // Detected a littlefs filesystem so create correct block device for it mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR_len), MP_OBJ_NEW_SMALL_INT(len) }; - bdev = pyb_flash_type.make_new(&pyb_flash_type, 0, 1, args); + bdev = MP_OBJ_TYPE_GET_SLOT(&pyb_flash_type, make_new)(&pyb_flash_type, 0, 1, args); } #endif diff --git a/ports/stm32/network_lan.c b/ports/stm32/network_lan.c index 25a955508853a..556adebd8fe95 100644 --- a/ports/stm32/network_lan.c +++ b/ports/stm32/network_lan.c @@ -162,7 +162,7 @@ MP_DEFINE_CONST_OBJ_TYPE( network_lan_type, MP_QSTR_LAN, MP_TYPE_FLAG_NONE, - network_lan_make_new, + make_new, network_lan_make_new, print, network_lan_print, locals_dict, &network_lan_locals_dict ); diff --git a/ports/stm32/pin.c b/ports/stm32/pin.c index 3c3cde026568b..0e4a7e9938611 100644 --- a/ports/stm32/pin.c +++ b/ports/stm32/pin.c @@ -596,7 +596,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, pin_print, call, pin_call, protocol, &pin_pin_p, @@ -674,7 +674,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_af_type, MP_QSTR_PinAF, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pin_af_obj_print, locals_dict, &pin_af_locals_dict ); diff --git a/ports/stm32/pin_named_pins.c b/ports/stm32/pin_named_pins.c index 1a35ec57875fc..8eeb4ed32b118 100644 --- a/ports/stm32/pin_named_pins.c +++ b/ports/stm32/pin_named_pins.c @@ -35,7 +35,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_cpu_pins_obj_type, MP_QSTR_cpu, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &pin_cpu_pins_locals_dict ); @@ -43,7 +42,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &pin_board_pins_locals_dict ); diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c index a07fc921649cf..69e807c79dfd5 100644 --- a/ports/stm32/pyb_can.c +++ b/ports/stm32/pyb_can.c @@ -1076,7 +1076,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_can_type, MP_QSTR_CAN, MP_TYPE_FLAG_NONE, - pyb_can_make_new, + make_new, pyb_can_make_new, print, pyb_can_print, protocol, &can_stream_p, locals_dict, &pyb_can_locals_dict diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index 9071d92944ee4..0cbb640473b82 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -1108,7 +1108,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - pyb_i2c_make_new, + make_new, pyb_i2c_make_new, print, pyb_i2c_print, locals_dict, &pyb_i2c_locals_dict ); diff --git a/ports/stm32/pyb_spi.c b/ports/stm32/pyb_spi.c index a225bd84e7534..54582f8e5414f 100644 --- a/ports/stm32/pyb_spi.c +++ b/ports/stm32/pyb_spi.c @@ -354,7 +354,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - pyb_spi_make_new, + make_new, pyb_spi_make_new, print, pyb_spi_print, protocol, &pyb_spi_p, locals_dict, &pyb_spi_locals_dict diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index f501ec23b1b31..aacfc3805e97c 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -841,6 +841,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_rtc_type, MP_QSTR_RTC, MP_TYPE_FLAG_NONE, - pyb_rtc_make_new, + make_new, pyb_rtc_make_new, locals_dict, &pyb_rtc_locals_dict ); diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index cbf1ade5c5b78..964ed49a67dde 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -876,7 +876,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_sdcard_type, MP_QSTR_SDCard, MP_TYPE_FLAG_NONE, - pyb_sdcard_make_new, + make_new, pyb_sdcard_make_new, locals_dict, &pyb_sdcard_locals_dict ); #endif @@ -886,7 +886,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_mmcard_type, MP_QSTR_MMCard, MP_TYPE_FLAG_NONE, - pyb_mmcard_make_new, + make_new, pyb_mmcard_make_new, locals_dict, &pyb_sdcard_locals_dict ); #endif diff --git a/ports/stm32/servo.c b/ports/stm32/servo.c index 1b0ca0a88224c..e0aa2d6b69f83 100644 --- a/ports/stm32/servo.c +++ b/ports/stm32/servo.c @@ -340,7 +340,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_servo_type, MP_QSTR_Servo, MP_TYPE_FLAG_NONE, - pyb_servo_make_new, + make_new, pyb_servo_make_new, print, pyb_servo_print, locals_dict, &pyb_servo_locals_dict ); diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index a0154408dd5fb..848602a7878e3 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -457,7 +457,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_flash_type, MP_QSTR_Flash, MP_TYPE_FLAG_NONE, - pyb_flash_make_new, + make_new, pyb_flash_make_new, print, pyb_flash_print, locals_dict, &pyb_flash_locals_dict ); diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 91c88df1b3726..7a6ccd4dfd2d9 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -1475,7 +1475,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - pyb_timer_make_new, + make_new, pyb_timer_make_new, print, pyb_timer_print, locals_dict, &pyb_timer_locals_dict ); @@ -1615,7 +1615,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_channel_type, MP_QSTR_TimerChannel, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, locals_dict, &pyb_timer_channel_locals_dict ); diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index e389ef68f2aae..12c5e497de84e 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -940,7 +940,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_usb_vcp_type, MP_QSTR_USB_VCP, MP_TYPE_FLAG_ITER_IS_STREAM, - pyb_usb_vcp_make_new, + make_new, pyb_usb_vcp_make_new, print, pyb_usb_vcp_print, protocol, &pyb_usb_vcp_stream_p, locals_dict, &pyb_usb_vcp_locals_dict @@ -1080,7 +1080,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_usb_hid_type, MP_QSTR_USB_HID, MP_TYPE_FLAG_NONE, - pyb_usb_hid_make_new, + make_new, pyb_usb_hid_make_new, protocol, &pyb_usb_hid_stream_p, locals_dict, &pyb_usb_hid_locals_dict ); diff --git a/ports/stm32/usrsw.c b/ports/stm32/usrsw.c index 7d406c0ecd717..170c01bd6d043 100644 --- a/ports/stm32/usrsw.c +++ b/ports/stm32/usrsw.c @@ -138,7 +138,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_switch_type, MP_QSTR_Switch, MP_TYPE_FLAG_NONE, - pyb_switch_make_new, + make_new, pyb_switch_make_new, print, pyb_switch_print, call, pyb_switch_call, locals_dict, &pyb_switch_locals_dict diff --git a/ports/stm32/wdt.c b/ports/stm32/wdt.c index 09780ea9d3cc9..e541e4d36b4fe 100644 --- a/ports/stm32/wdt.c +++ b/ports/stm32/wdt.c @@ -106,6 +106,6 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_wdt_type, MP_QSTR_WDT, MP_TYPE_FLAG_NONE, - pyb_wdt_make_new, + make_new, pyb_wdt_make_new, locals_dict, &pyb_wdt_locals_dict ); diff --git a/ports/teensy/led.c b/ports/teensy/led.c index dd4c65da39e1b..46c6ae492f851 100644 --- a/ports/teensy/led.c +++ b/ports/teensy/led.c @@ -138,7 +138,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_led_type, MP_QSTR_LED, MP_TYPE_FLAG_NONE, - led_obj_make_new, + make_new, led_obj_make_new, print, led_obj_print, locals_dict, &led_locals_dict ); diff --git a/ports/teensy/timer.c b/ports/teensy/timer.c index a0f490d925fbb..8c7609e4479ec 100644 --- a/ports/teensy/timer.c +++ b/ports/teensy/timer.c @@ -750,7 +750,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_type, MP_QSTR_Timer, MP_TYPE_FLAG_NONE, - pyb_timer_make_new, + make_new, pyb_timer_make_new, print, pyb_timer_print, locals_dict, &pyb_timer_locals_dict ); @@ -894,7 +894,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( pyb_timer_channel_type, MP_QSTR_TimerChannel, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, pyb_timer_channel_print, locals_dict, &pyb_timer_channel_locals_dict ); diff --git a/ports/teensy/uart.c b/ports/teensy/uart.c index db5b8e2f44336..e71f676d0ae18 100644 --- a/ports/teensy/uart.c +++ b/ports/teensy/uart.c @@ -487,7 +487,7 @@ MP_DEFINE_CONST_OBJ_TYPE( pyb_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_NONE, - pyb_uart_make_new, + make_new, pyb_uart_make_new, print, pyb_uart_print, locals_dict, &pyb_uart_locals_dict ); diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 7b4c0c0bf48e7..8b7fc7de67802 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -110,7 +110,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_stest_fileio, MP_QSTR_stest_fileio, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, protocol, &fileio_stream_p, locals_dict, &rawfile_locals_dict ); @@ -140,7 +139,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_stest_textio2, MP_QSTR_stest_textio2, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, protocol, &textio_stream_p2, locals_dict, &rawfile_locals_dict2 ); diff --git a/ports/unix/main.c b/ports/unix/main.c index 4f019b6c2c30d..44823ee174776 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -518,7 +518,7 @@ MP_NOINLINE int main_(int argc, char **argv) { { // Mount the host FS at the root of our internal VFS mp_obj_t args[2] = { - mp_type_vfs_posix.make_new(&mp_type_vfs_posix, 0, 0, NULL), + MP_OBJ_TYPE_GET_SLOT(&mp_type_vfs_posix, make_new)(&mp_type_vfs_posix, 0, 0, NULL), MP_OBJ_NEW_QSTR(MP_QSTR__slash_), }; mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map); diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 04152f1ad41f2..3df748b80d956 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -425,7 +425,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ffimod_type, MP_QSTR_ffimod, MP_TYPE_FLAG_NONE, - ffimod_make_new, + make_new, ffimod_make_new, print, ffimod_print, locals_dict, &ffimod_locals_dict ); @@ -535,7 +535,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ffifunc_type, MP_QSTR_ffifunc, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, ffifunc_print, call, ffifunc_call ); @@ -563,7 +562,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( fficallback_type, MP_QSTR_fficallback, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, fficallback_print, locals_dict, &fficallback_locals_dict ); @@ -601,7 +599,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( ffivar_type, MP_QSTR_ffivar, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, ffivar_print, locals_dict, &ffivar_locals_dict ); @@ -613,8 +610,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( opaque_type, MP_QSTR_opaqueval, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, - // .print = opaque_print, + make_new, // .print = opaque_print, ); */ diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index e6b874b235ecb..5988876f888ab 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -178,7 +178,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( jclass_type, MP_QSTR_jclass, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, jclass_print, attr, jclass_attr, call, jclass_call, @@ -326,7 +325,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( jobject_type, MP_QSTR_jobject, MP_TYPE_FLAG_ITER_IS_GETITER, - MP_TYPE_NULL_MAKE_NEW, print, jobject_print, unary_op, jobject_unary_op, attr, jobject_attr, @@ -575,7 +573,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( jmethod_type, MP_QSTR_jmethod, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, jmethod_print, call, jmethod_call, // .attr = jobject_attr, diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index 674841bf1871d..8f71813e70aec 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -315,7 +315,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_poll, MP_QSTR_poll, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, poll_iternext, locals_dict, &poll_locals_dict ); diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index dfbf15cd3efd3..a32fc60163136 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -520,7 +520,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_socket, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - socket_make_new, + make_new, socket_make_new, print, socket_print, protocol, &usocket_stream_p, locals_dict, &usocket_locals_dict diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index a844eceb50c14..8f0f25257ba54 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -130,7 +130,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, - machine_hard_i2c_make_new, + make_new, machine_hard_i2c_make_new, print, machine_hard_i2c_print, protocol, &machine_hard_i2c_p, locals_dict, &mp_machine_i2c_locals_dict diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c index 3114ac36fd008..be0698651bbf6 100644 --- a/ports/zephyr/machine_pin.c +++ b/ports/zephyr/machine_pin.c @@ -289,7 +289,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, MP_TYPE_FLAG_NONE, - mp_pin_make_new, + make_new, mp_pin_make_new, print, machine_pin_print, call, machine_pin_call, protocol, &machine_pin_pin_p, diff --git a/ports/zephyr/machine_spi.c b/ports/zephyr/machine_spi.c index d990ed9c150f5..19d4ae6dee684 100644 --- a/ports/zephyr/machine_spi.c +++ b/ports/zephyr/machine_spi.c @@ -201,7 +201,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_hard_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, - machine_hard_spi_make_new, + make_new, machine_hard_spi_make_new, print, machine_hard_spi_print, protocol, &machine_hard_spi_p, locals_dict, &mp_machine_spi_locals_dict diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c index 867c5ae8863a0..b989c0f4817c4 100644 --- a/ports/zephyr/machine_uart.c +++ b/ports/zephyr/machine_uart.c @@ -158,7 +158,7 @@ MP_DEFINE_CONST_OBJ_TYPE( machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, - machine_uart_make_new, + make_new, machine_uart_make_new, print, machine_uart_print, protocol, &uart_stream_p, locals_dict, &machine_uart_locals_dict diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index c638f389206e5..869449e7df473 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -104,11 +104,11 @@ STATIC void vfs_init(void) { #ifdef CONFIG_DISK_DRIVER_SDMMC mp_obj_t args[] = { mp_obj_new_str(CONFIG_SDMMC_VOLUME_NAME, strlen(CONFIG_SDMMC_VOLUME_NAME)) }; - bdev = zephyr_disk_access_type.make_new(&zephyr_disk_access_type, ARRAY_SIZE(args), 0, args); + bdev = MP_OBJ_TYPE_GET_SLOT(&zephyr_disk_access_type, make_new)(&zephyr_disk_access_type, ARRAY_SIZE(args), 0, args); mount_point_str = "/sd"; #elif defined(CONFIG_FLASH_MAP) && FLASH_AREA_LABEL_EXISTS(storage) mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(FLASH_AREA_ID(storage)), MP_OBJ_NEW_SMALL_INT(4096) }; - bdev = zephyr_flash_area_type.make_new(&zephyr_flash_area_type, ARRAY_SIZE(args), 0, args); + bdev = MP_OBJ_TYPE_GET_SLOT(&zephyr_flash_area_type, make_new)(&zephyr_flash_area_type, ARRAY_SIZE(args), 0, args); mount_point_str = "/flash"; #endif diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c index 77f839fdd5104..c79f73a9d2759 100644 --- a/ports/zephyr/modusocket.c +++ b/ports/zephyr/modusocket.c @@ -357,7 +357,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( socket_type, MP_QSTR_socket, MP_TYPE_FLAG_NONE, - socket_make_new, + make_new, socket_make_new, print, socket_print, protocol, &socket_stream_p, locals_dict, &socket_locals_dict diff --git a/ports/zephyr/modzsensor.c b/ports/zephyr/modzsensor.c index beb4d6ad7903a..e35a1716f473f 100644 --- a/ports/zephyr/modzsensor.c +++ b/ports/zephyr/modzsensor.c @@ -109,7 +109,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( sensor_type, MP_QSTR_Sensor, MP_TYPE_FLAG_NONE, - sensor_make_new, + make_new, sensor_make_new, locals_dict, &sensor_locals_dict ); diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c index 6ab8271d34766..498fea6fb1c8d 100644 --- a/ports/zephyr/zephyr_storage.c +++ b/ports/zephyr/zephyr_storage.c @@ -132,7 +132,7 @@ MP_DEFINE_CONST_OBJ_TYPE( zephyr_disk_access_type, MP_QSTR_DiskAccess, MP_TYPE_FLAG_NONE, - zephyr_disk_access_make_new, + make_new, zephyr_disk_access_make_new, print, zephyr_disk_access_print, locals_dict, &zephyr_disk_access_locals_dict ); @@ -254,7 +254,7 @@ MP_DEFINE_CONST_OBJ_TYPE( zephyr_flash_area_type, MP_QSTR_FlashArea, MP_TYPE_FLAG_NONE, - zephyr_flash_area_make_new, + make_new, zephyr_flash_area_make_new, print, zephyr_flash_area_print, locals_dict, &zephyr_flash_area_locals_dict ); diff --git a/py/builtinevex.c b/py/builtinevex.c index 403cd95a9deef..173978ef5221d 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -41,8 +41,7 @@ typedef struct _mp_obj_code_t { STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_code, MP_QSTR_code, - MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW + MP_TYPE_FLAG_NONE ); STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) { diff --git a/py/dynruntime.h b/py/dynruntime.h index be573bde2ad4a..8564715c0b59f 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -105,7 +105,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) { #define mp_const_none ((mp_obj_t)mp_fun_table.const_none) #define mp_const_false ((mp_obj_t)mp_fun_table.const_false) #define mp_const_true ((mp_obj_t)mp_fun_table.const_true) -#define mp_const_empty_bytes (mp_type_bytes.make_new(NULL, 0, 0, NULL)) +#define mp_const_empty_bytes (MP_OBJ_TYPE_GET_SLOT(&mp_type_bytes, make_new)(NULL, 0, 0, NULL)) #define mp_const_empty_tuple (mp_fun_table.new_tuple(0, NULL)) #define mp_obj_new_bool(b) ((b) ? (mp_obj_t)mp_fun_table.const_true : (mp_obj_t)mp_fun_table.const_false) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 152323b5ca963..06e9f1acb5e34 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -547,7 +547,7 @@ STATIC mp_obj_t mp_builtin_sorted(size_t n_args, const mp_obj_t *args, mp_map_t if (n_args > 1) { mp_raise_TypeError(MP_ERROR_TEXT("must use keyword argument for key function")); } - mp_obj_t self = mp_type_list.make_new(&mp_type_list, 1, 0, args); + mp_obj_t self = mp_obj_list_make_new(&mp_type_list, 1, 0, args); mp_obj_list_sort(1, &self, kwargs); return self; diff --git a/py/modio.c b/py/modio.c index a1e04e4cac54a..9ec6bbcc4ecd4 100644 --- a/py/modio.c +++ b/py/modio.c @@ -101,7 +101,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_iobase, MP_QSTR_IOBase, MP_TYPE_FLAG_NONE, - iobase_make_new, + make_new, iobase_make_new, protocol, &iobase_p ); @@ -196,7 +196,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_bufwriter, MP_QSTR_BufferedWriter, MP_TYPE_FLAG_NONE, - bufwriter_make_new, + make_new, bufwriter_make_new, protocol, &bufwriter_stream_p, locals_dict, &bufwriter_locals_dict ); diff --git a/py/modthread.c b/py/modthread.c index 3116fe6bd9db5..51d63e4703720 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -120,7 +120,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_thread_lock, MP_QSTR_lock, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, locals_dict, &thread_lock_locals_dict ); diff --git a/py/obj.h b/py/obj.h index b1d722080c665..34adfbd0f0427 100644 --- a/py/obj.h +++ b/py/obj.h @@ -594,15 +594,15 @@ struct _mp_obj_type_t { // The name of this type, a qstr. uint16_t name; - // Corresponds to __new__ and __init__ special methods, to make an instance of the type. - mp_make_new_fun_t make_new; - // Slots: For the rest of the fields, the slot index points to the // relevant function in the variable-length "slots" field. Ideally these // would be only 4 bits, but the extra overhead of accessing them adds // more code, and we also need to be able to take the address of them for // mp_obj_class_lookup. + // Corresponds to __new__ and __init__ special methods, to make an instance of the type. + uint8_t slot_index_make_new; + // Corresponds to __repr__ and __str__ special methods. uint8_t slot_index_print; @@ -673,8 +673,8 @@ typedef struct _mp_obj_empty_type_t { mp_obj_base_t base; uint16_t flags; uint16_t name; - mp_make_new_fun_t make_new; + uint8_t slot_index_make_new; uint8_t slot_index_print; uint8_t slot_index_call; uint8_t slot_index_unary_op; @@ -694,8 +694,8 @@ typedef struct _mp_obj_full_type_t { mp_obj_base_t base; uint16_t flags; uint16_t name; - mp_make_new_fun_t make_new; + uint8_t slot_index_make_new; uint8_t slot_index_print; uint8_t slot_index_call; uint8_t slot_index_unary_op; @@ -712,8 +712,7 @@ typedef struct _mp_obj_full_type_t { const void *slots[11]; } mp_obj_full_type_t; -#define MP_TYPE_NULL_MAKE_NEW (NULL) - +#define _MP_OBJ_TYPE_SLOT_TYPE_make_new (mp_make_new_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t) #define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t) @@ -730,42 +729,41 @@ typedef struct _mp_obj_full_type_t { // Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE. // Generated with: // for i in range(13): -// print(f"#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_{i}(_struct_type, _typename, _name, _flags, _make_new{''.join(f', f{j+1}, v{j+1}' for j in range(i))}) const _struct_type _typename = {{ .base = {{ &mp_type_type }}, .name = _name, .flags = _flags, .make_new = _make_new{''.join(f', .slot_index_##f{j+1} = {j+1}' for j in range(i))}{', .slots = { ' + ''.join(f'v{j+1}, ' for j in range(i)) + '}' if i else '' } }}") -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, _make_new, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slots = { v1, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = { v1, v2, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = { v1, v2, v3, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = { v1, v2, v3, v4, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = { v1, v2, v3, v4, v5, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = { v1, v2, v3, v4, v5, v6, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = { v1, v2, v3, v4, v5, v6, v7, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } } -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } } +// print(f"#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_{i}(_struct_type, _typename, _name, _flags{''.join(f', f{j+1}, v{j+1}' for j in range(i))}) const _struct_type _typename = {{ .base = {{ &mp_type_type }}, .name = _name, .flags = _flags{''.join(f', .slot_index_##f{j+1} = {j+1}' for j in range(i))}{', .slots = { ' + ''.join(f'v{j+1}, ' for j in range(i)) + '}' if i else '' } }}") +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slots = { v1, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = { v1, v2, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = { v1, v2, v3, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = { v1, v2, v3, v4, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = { v1, v2, v3, v4, v5, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = { v1, v2, v3, v4, v5, v6, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = { v1, v2, v3, v4, v5, v6, v7, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } } +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } } #define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f) #define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1]) #define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL)) #define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(t)->slot_index_##f - 1] = (void *)v) #define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f)) -// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer. -#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0 || (offset == offsetof(mp_obj_type_t, make_new) && t->make_new)) +#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0) // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x // This macro evaluates to MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N, where N is the value -// of the 30th argument (30 is 13*2 + 4). -#define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N +// of the 29th argument (29 is 13*2 + 3). +#define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N // These macros are used to define a object type in ROM. // Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*) // They use the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_* // macro to use based on the number of arguments. It works by shifting the // numeric values 12, 11, ... 0 by the number of arguments, such that the -// 30th argument ends up being the number to use. The _INV values are +// 29th argument ends up being the number to use. The _INV values are // placeholders because the slot arguments come in pairs. #define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__)) #define MP_DEFINE_CONST_OBJ_FULL_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_full_type_t, __VA_ARGS__)) @@ -916,7 +914,7 @@ void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type); #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int)) #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str)) #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op)) -#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new) +#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, make_new) == mp_obj_dict_make_new) #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function)) mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict); @@ -1032,7 +1030,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in); mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in); // exception -#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new) +#define mp_obj_is_native_exception_instance(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), make_new) == mp_obj_exception_make_new) bool mp_obj_is_exception_type(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in); bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type); @@ -1044,7 +1042,7 @@ mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type_in, size_t n_args, mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in); void mp_init_emergency_exception_buf(void); static inline mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) { - assert(exc_type->make_new == mp_obj_exception_make_new); + assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new); return mp_obj_exception_make_new(exc_type, 1, 0, &arg); } diff --git a/py/objarray.c b/py/objarray.c index 0d1032929f48c..42fc0749d2b2d 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -575,7 +575,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_array, MP_QSTR_array, MP_TYPE_FLAG_ITER_IS_GETITER, - array_make_new, + make_new, array_make_new, print, array_print, iter, array_iterator_new, unary_op, array_unary_op, @@ -591,7 +591,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_bytearray, MP_QSTR_bytearray, MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, - bytearray_make_new, + make_new, bytearray_make_new, print, array_print, iter, array_iterator_new, unary_op, array_unary_op, @@ -619,7 +619,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_memoryview, MP_QSTR_memoryview, MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, - memoryview_make_new, + make_new, memoryview_make_new, iter, array_iterator_new, unary_op, array_unary_op, binary_op, array_binary_op, @@ -677,7 +677,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_array_it, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, array_it_iternext ); diff --git a/py/objattrtuple.c b/py/objattrtuple.c index 2e207f4cf0c46..fbe04bedb877a 100644 --- a/py/objattrtuple.c +++ b/py/objattrtuple.c @@ -84,7 +84,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_attrtuple, MP_QSTR_tuple, MP_TYPE_FLAG_ITER_IS_GETITER, - MP_TYPE_NULL_MAKE_NEW, // reuse tuple to save on a qstr print, mp_obj_attrtuple_print, unary_op, mp_obj_tuple_unary_op, diff --git a/py/objbool.c b/py/objbool.c index 5d014bbb8eaf8..3267ff98bb8a1 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -89,7 +89,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_bool, MP_QSTR_bool, MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, - bool_make_new, + make_new, bool_make_new, print, bool_print, unary_op, bool_unary_op, binary_op, bool_binary_op diff --git a/py/objboundmeth.c b/py/objboundmeth.c index f4b3b9b7df9ce..8486f876f9a53 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -111,7 +111,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_bound_meth, MP_QSTR_bound_method, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, BOUND_METH_TYPE_PRINT BOUND_METH_TYPE_ATTR call, bound_meth_call diff --git a/py/objcell.c b/py/objcell.c index b100fae4fe994..0a74e29d2064a 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -48,7 +48,7 @@ STATIC void cell_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t k STATIC MP_DEFINE_CONST_OBJ_TYPE( // cell representation is just value in < > - mp_type_cell, MP_QSTR_, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW + mp_type_cell, MP_QSTR_, MP_TYPE_FLAG_NONE CELL_TYPE_PRINT ); diff --git a/py/objclosure.c b/py/objclosure.c index 45a3e83c462d8..6059d18100e03 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -99,7 +99,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_closure, MP_QSTR_closure, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, CLOSURE_TYPE_ATTR CLOSURE_TYPE_PRINT call, closure_call diff --git a/py/objcomplex.c b/py/objcomplex.c index cf213d718a793..ddd103eeb46e3 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -152,7 +152,8 @@ STATIC void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_complex, MP_QSTR_complex, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, complex_make_new, + mp_type_complex, MP_QSTR_complex, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + make_new, complex_make_new, print, complex_print, unary_op, complex_unary_op, binary_op, complex_binary_op, diff --git a/py/objdeque.c b/py/objdeque.c index 1a8f76ca110b1..8b52b8d387303 100644 --- a/py/objdeque.c +++ b/py/objdeque.c @@ -159,7 +159,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_deque, MP_QSTR_deque, MP_TYPE_FLAG_NONE, - deque_make_new, + make_new, deque_make_new, unary_op, deque_unary_op, locals_dict, &deque_locals_dict ); diff --git a/py/objdict.c b/py/objdict.c index 7755d7b786d4c..68c33961c70c0 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -465,7 +465,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict_view_it, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, dict_view_it_iternext ); @@ -517,7 +516,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict_view, MP_QSTR_dict_view, MP_TYPE_FLAG_ITER_IS_GETITER, - MP_TYPE_NULL_MAKE_NEW, print, dict_view_print, binary_op, dict_view_binary_op, iter, dict_view_getiter @@ -592,7 +590,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_dict, MP_QSTR_dict, MP_TYPE_FLAG_ITER_IS_GETITER, - mp_obj_dict_make_new, + make_new, mp_obj_dict_make_new, print, dict_print, unary_op, dict_unary_op, binary_op, dict_binary_op, @@ -606,7 +604,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_ordereddict, MP_QSTR_OrderedDict, MP_TYPE_FLAG_ITER_IS_GETITER, - mp_obj_dict_make_new, + make_new, mp_obj_dict_make_new, print, dict_print, unary_op, dict_unary_op, binary_op, dict_binary_op, diff --git a/py/objenumerate.c b/py/objenumerate.c index eea9e3e381ed4..40bed919ed750 100644 --- a/py/objenumerate.c +++ b/py/objenumerate.c @@ -71,7 +71,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_enumerate, MP_QSTR_enumerate, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - enumerate_make_new, + make_new, enumerate_make_new, iter, enumerate_iternext ); diff --git a/py/objexcept.c b/py/objexcept.c index 190213e12f610..3b76ae62ccc69 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -288,7 +288,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_BaseException, MP_QSTR_BaseException, MP_TYPE_FLAG_NONE, - mp_obj_exception_make_new, + make_new, mp_obj_exception_make_new, print, mp_obj_exception_print, attr, mp_obj_exception_attr ); @@ -375,12 +375,12 @@ MP_DEFINE_EXCEPTION(Exception, BaseException) // *FORMAT-ON* mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) { - assert(exc_type->make_new == mp_obj_exception_make_new); + assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new); return mp_obj_exception_make_new(exc_type, 0, 0, NULL); } mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args) { - assert(exc_type->make_new == mp_obj_exception_make_new); + assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new); return mp_obj_exception_make_new(exc_type, n_args, 0, args); } @@ -388,7 +388,7 @@ mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) { // Check that the given type is an exception type - assert(exc_type->make_new == mp_obj_exception_make_new); + assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new); // Try to allocate memory for the message mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); @@ -467,7 +467,7 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_er assert(fmt != NULL); // Check that the given type is an exception type - assert(exc_type->make_new == mp_obj_exception_make_new); + assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new); // Try to allocate memory for the message mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t); @@ -538,7 +538,7 @@ bool mp_obj_is_exception_type(mp_obj_t self_in) { if (mp_obj_is_type(self_in, &mp_type_type)) { // optimisation when self_in is a builtin exception mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in); - if (self->make_new == mp_obj_exception_make_new) { + if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(self, make_new) == mp_obj_exception_make_new) { return true; } } diff --git a/py/objexcept.h b/py/objexcept.h index 5d56d67d7314f..d532f66609243 100644 --- a/py/objexcept.h +++ b/py/objexcept.h @@ -41,7 +41,8 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); #define MP_DEFINE_EXCEPTION(exc_name, base_name) \ - MP_DEFINE_CONST_OBJ_TYPE(mp_type_##exc_name, MP_QSTR_##exc_name, MP_TYPE_FLAG_NONE, mp_obj_exception_make_new, \ + MP_DEFINE_CONST_OBJ_TYPE(mp_type_##exc_name, MP_QSTR_##exc_name, MP_TYPE_FLAG_NONE, \ + make_new, mp_obj_exception_make_new, \ print, mp_obj_exception_print, \ attr, mp_obj_exception_attr, \ parent, &mp_type_##base_name \ diff --git a/py/objfilter.c b/py/objfilter.c index bfe651f40d036..2a657fde4b71b 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -64,7 +64,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_filter, MP_QSTR_filter, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - filter_make_new, + make_new, filter_make_new, iter, filter_iternext ); diff --git a/py/objfloat.c b/py/objfloat.c index 9ecbab7a4dede..c862b4843b278 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -183,7 +183,8 @@ STATIC mp_obj_t float_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_float, MP_QSTR_float, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, float_make_new, + mp_type_float, MP_QSTR_float, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, + make_new, float_make_new, print, float_print, unary_op, float_unary_op, binary_op, float_binary_op diff --git a/py/objfun.c b/py/objfun.c index d6ff354575bf9..390ddaa2d2821 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -57,7 +57,7 @@ STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, call, fun_builtin_0_call, unary_op, mp_generic_unary_op ); @@ -70,7 +70,7 @@ STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, call, fun_builtin_1_call, unary_op, mp_generic_unary_op ); @@ -83,7 +83,7 @@ STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, call, fun_builtin_2_call, unary_op, mp_generic_unary_op ); @@ -96,7 +96,7 @@ STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, call, fun_builtin_3_call, unary_op, mp_generic_unary_op ); @@ -125,7 +125,7 @@ STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_k } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, MP_TYPE_NULL_MAKE_NEW, + mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN, call, fun_builtin_var_call, unary_op, mp_generic_unary_op ); @@ -368,7 +368,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_fun_bc, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, FUN_BC_TYPE_PRINT FUN_BC_TYPE_ATTR call, fun_bc_call, @@ -431,7 +430,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_fun_native, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, FUN_BC_TYPE_PRINT FUN_BC_TYPE_ATTR call, fun_native_call, @@ -542,7 +540,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_fun_asm, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, call, fun_asm_call, unary_op, mp_generic_unary_op ); diff --git a/py/objgenerator.c b/py/objgenerator.c index d8515c13ce257..8175dbd683db0 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -80,7 +80,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_gen_wrap, MP_QSTR_generator, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, GEN_WRAP_TYPE_ATTR call, gen_wrap_call, unary_op, mp_generic_unary_op @@ -146,7 +145,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_native_gen_wrap, MP_QSTR_generator, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, call, native_gen_wrap_call, NATIVE_GEN_WRAP_TYPE_ATTR unary_op, mp_generic_unary_op @@ -371,7 +369,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_gen_instance, MP_QSTR_generator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, print, gen_instance_print, unary_op, mp_generic_unary_op, iter, gen_instance_iternext, diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index 134cbcd6299d5..c598d1daaca9a 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -60,7 +60,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_it, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, it_iternext ); diff --git a/py/objint.c b/py/objint.c index f06bc441f15d5..1a3ad8694717e 100644 --- a/py/objint.c +++ b/py/objint.c @@ -461,7 +461,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_int, MP_QSTR_int, MP_TYPE_FLAG_NONE, - mp_obj_int_make_new, + make_new, mp_obj_int_make_new, print, mp_obj_int_print, unary_op, mp_obj_int_unary_op, binary_op, mp_obj_int_binary_op, diff --git a/py/objlist.c b/py/objlist.c index 8d18344ea8a71..18da91ba3abc3 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -71,7 +71,7 @@ STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) { return list; } -STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t mp_obj_list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { (void)type_in; mp_arg_check_num(n_args, n_kw, 0, 1, false); @@ -456,7 +456,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_list, MP_QSTR_list, MP_TYPE_FLAG_ITER_IS_GETITER, - list_make_new, + make_new, mp_obj_list_make_new, print, list_print, unary_op, list_unary_op, binary_op, list_binary_op, diff --git a/py/objlist.h b/py/objlist.h index a43663db76f8d..a3bba68028f81 100644 --- a/py/objlist.h +++ b/py/objlist.h @@ -36,5 +36,6 @@ typedef struct _mp_obj_list_t { } mp_obj_list_t; void mp_obj_list_init(mp_obj_list_t *o, size_t n); +mp_obj_t mp_obj_list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args); #endif // MICROPY_INCLUDED_PY_OBJLIST_H diff --git a/py/objmap.c b/py/objmap.c index 115832e387e56..e7e594cd2c54b 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -67,6 +67,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_map, MP_QSTR_map, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - map_make_new, + make_new, map_make_new, iter, map_iternext ); diff --git a/py/objmodule.c b/py/objmodule.c index 6fc3653e6a24d..d14a59b8632c4 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -134,7 +134,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_module, MP_QSTR_module, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, module_print, attr, module_attr ); diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 3b45d8f76f29b..75e21494b4d36 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -157,14 +157,14 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t type->base.type = &mp_type_type; type->flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE; // can match tuple type->name = name; - type->make_new = namedtuple_make_new; - MP_OBJ_TYPE_SET_SLOT(type, print, namedtuple_print, 0); - MP_OBJ_TYPE_SET_SLOT(type, unary_op, mp_obj_tuple_unary_op, 1); - MP_OBJ_TYPE_SET_SLOT(type, binary_op, mp_obj_tuple_binary_op, 2); - MP_OBJ_TYPE_SET_SLOT(type, attr, namedtuple_attr, 3); - MP_OBJ_TYPE_SET_SLOT(type, subscr, mp_obj_tuple_subscr, 4); - MP_OBJ_TYPE_SET_SLOT(type, iter, mp_obj_tuple_getiter, 5); - MP_OBJ_TYPE_SET_SLOT(type, parent, &mp_type_tuple, 6); + MP_OBJ_TYPE_SET_SLOT(type, make_new, namedtuple_make_new, 0); + MP_OBJ_TYPE_SET_SLOT(type, print, namedtuple_print, 1); + MP_OBJ_TYPE_SET_SLOT(type, unary_op, mp_obj_tuple_unary_op, 2); + MP_OBJ_TYPE_SET_SLOT(type, binary_op, mp_obj_tuple_binary_op, 3); + MP_OBJ_TYPE_SET_SLOT(type, attr, namedtuple_attr, 4); + MP_OBJ_TYPE_SET_SLOT(type, subscr, mp_obj_tuple_subscr, 5); + MP_OBJ_TYPE_SET_SLOT(type, iter, mp_obj_tuple_getiter, 6); + MP_OBJ_TYPE_SET_SLOT(type, parent, &mp_type_tuple, 7); return MP_OBJ_FROM_PTR(o); } diff --git a/py/objnamedtuple.h b/py/objnamedtuple.h index db4a3d87d87fd..c4f4149fd606b 100644 --- a/py/objnamedtuple.h +++ b/py/objnamedtuple.h @@ -29,9 +29,9 @@ #include "py/objtuple.h" typedef struct _mp_obj_namedtuple_type_t { - // This is a mp_obj_type_t with seven slots. + // This is a mp_obj_type_t with eight slots. mp_obj_empty_type_t base; - void *slots[7]; + void *slots[8]; size_t n_fields; qstr fields[]; } mp_obj_namedtuple_type_t; diff --git a/py/objnone.c b/py/objnone.c index 4fffbc997e89e..4f8996e897a83 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -47,7 +47,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_NoneType, MP_QSTR_NoneType, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, none_print, unary_op, mp_generic_unary_op ); diff --git a/py/objobject.c b/py/objobject.c index ffad610707279..1acae6d00b189 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -121,6 +121,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_object, MP_QSTR_object, MP_TYPE_FLAG_NONE, - object_make_new + make_new, object_make_new OBJECT_TYPE_LOCALS_DICT ); diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 7a45b6b73fe6f..78b600abacdcd 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -49,7 +49,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_polymorph_iter, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, polymorph_it_iternext ); @@ -81,7 +80,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_polymorph_iter_with_finaliser, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, polymorph_it_iternext, locals_dict, &mp_obj_polymorph_iter_locals_dict ); diff --git a/py/objproperty.c b/py/objproperty.c index ce3b572591d2f..2d3af10e8c70a 100644 --- a/py/objproperty.c +++ b/py/objproperty.c @@ -94,7 +94,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_property, MP_QSTR_property, MP_TYPE_FLAG_NONE, - property_make_new, + make_new, property_make_new, locals_dict, &property_locals_dict ); diff --git a/py/objrange.c b/py/objrange.c index 1ad8f6031f292..f0fe56d9db12e 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -54,7 +54,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_range_it, MP_QSTR_iterator, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - MP_TYPE_NULL_MAKE_NEW, iter, range_it_iternext ); @@ -225,7 +224,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_range, MP_QSTR_range, MP_TYPE_FLAG_NONE, - range_make_new, + make_new, range_make_new, RANGE_TYPE_BINOP RANGE_TYPE_ATTR print, range_print, diff --git a/py/objreversed.c b/py/objreversed.c index e265266d3f127..c66698f028adf 100644 --- a/py/objreversed.c +++ b/py/objreversed.c @@ -72,7 +72,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_reversed, MP_QSTR_reversed, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - reversed_make_new, + make_new, reversed_make_new, iter, reversed_iternext ); diff --git a/py/objset.c b/py/objset.c index b827f49f40dfe..906807889ab36 100644 --- a/py/objset.c +++ b/py/objset.c @@ -543,7 +543,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_set, MP_QSTR_set, MP_TYPE_FLAG_ITER_IS_GETITER, - set_make_new, + make_new, set_make_new, print, set_print, unary_op, set_unary_op, binary_op, set_binary_op, @@ -569,7 +569,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_frozenset, MP_QSTR_frozenset, MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER, - set_make_new, + make_new, set_make_new, print, set_print, unary_op, set_unary_op, binary_op, set_binary_op, diff --git a/py/objsingleton.c b/py/objsingleton.c index 4a099657d4be4..dc73d28c274cd 100644 --- a/py/objsingleton.c +++ b/py/objsingleton.c @@ -44,7 +44,7 @@ STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ } MP_DEFINE_CONST_OBJ_TYPE( - mp_type_singleton, MP_QSTR_, MP_TYPE_FLAG_NONE, MP_TYPE_NULL_MAKE_NEW, + mp_type_singleton, MP_QSTR_, MP_TYPE_FLAG_NONE, print, singleton_print, unary_op, mp_generic_unary_op ); diff --git a/py/objslice.c b/py/objslice.c index d1dbb24586d9f..01d4da0dbda4e 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -104,7 +104,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_slice, MP_QSTR_slice, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, SLICE_TYPE_ATTR_OR_LOCALS_DICT print, slice_print ); diff --git a/py/objstr.c b/py/objstr.c index 12f6e15d0498c..50ab0018ed6e6 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -482,7 +482,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { if (!mp_obj_is_type(arg, &mp_type_list) && !mp_obj_is_type(arg, &mp_type_tuple)) { // arg is not a list nor a tuple, try to convert it to a list // TODO: Try to optimize? - arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg); + arg = mp_obj_list_make_new(&mp_type_list, 1, 0, &arg); } mp_obj_get_array(arg, &seq_len, &seq_items); @@ -2147,7 +2147,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_str, MP_QSTR_str, MP_TYPE_FLAG_NONE, - mp_obj_str_make_new, + make_new, mp_obj_str_make_new, print, str_print, binary_op, mp_obj_str_binary_op, subscr, bytes_subscr, @@ -2162,7 +2162,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_bytes, MP_QSTR_bytes, MP_TYPE_FLAG_NONE, - bytes_make_new, + make_new, bytes_make_new, print, str_print, binary_op, mp_obj_str_binary_op, subscr, bytes_subscr, diff --git a/py/objstringio.c b/py/objstringio.c index 4e19b83999aac..1a083449bafa9 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -248,7 +248,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_stringio, MP_QSTR_StringIO, MP_TYPE_FLAG_ITER_IS_STREAM, - stringio_make_new, + make_new, stringio_make_new, print, stringio_print, protocol, &stringio_stream_p, locals_dict, &stringio_locals_dict @@ -265,7 +265,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_bytesio, MP_QSTR_BytesIO, MP_TYPE_FLAG_ITER_IS_STREAM, - stringio_make_new, + make_new, stringio_make_new, print, stringio_print, protocol, &bytesio_stream_p, locals_dict, &stringio_locals_dict diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 9b28841ecd177..93383b3c19e95 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -233,7 +233,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_str, MP_QSTR_str, MP_TYPE_FLAG_ITER_IS_GETITER, - mp_obj_str_make_new, + make_new, mp_obj_str_make_new, print, uni_print, unary_op, uni_unary_op, binary_op, mp_obj_str_binary_op, diff --git a/py/objtuple.c b/py/objtuple.c index 485d44c52a8d7..9d6797b4ffc3f 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -228,7 +228,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_tuple, MP_QSTR_tuple, MP_TYPE_FLAG_ITER_IS_GETITER, - mp_obj_tuple_make_new, + make_new, mp_obj_tuple_make_new, print, mp_obj_tuple_print, unary_op, mp_obj_tuple_unary_op, binary_op, mp_obj_tuple_binary_op, diff --git a/py/objtype.c b/py/objtype.c index 183dce071ee9b..db364e0e5a5de 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -88,7 +88,7 @@ STATIC mp_obj_t native_base_init_wrapper(size_t n_args, const mp_obj_t *args) { mp_obj_instance_t *self = MP_OBJ_TO_PTR(args[0]); const mp_obj_type_t *native_base = NULL; instance_count_native_bases(self->base.type, &native_base); - self->subobj[0] = native_base->make_new(native_base, n_args - 1, 0, args + 1); + self->subobj[0] = MP_OBJ_TYPE_GET_SLOT(native_base, make_new)(native_base, n_args - 1, 0, args + 1); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(native_base_init_wrapper_obj, 1, MP_OBJ_FUN_ARGS_MAX, native_base_init_wrapper); @@ -285,7 +285,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size struct class_lookup_data lookup = { .obj = NULL, .attr = MP_QSTR___new__, - .slot_offset = offsetof(mp_obj_type_t, make_new), + .slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(make_new), .dest = init_fn, .is_type = false, }; @@ -362,7 +362,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size // If the type had a native base that was not explicitly initialised // (constructed) by the Python __init__() method then construct it now. if (native_base != NULL && o->subobj[0] == MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj)) { - o->subobj[0] = native_base->make_new(native_base, n_args, n_kw, args); + o->subobj[0] = MP_OBJ_TYPE_GET_SLOT(native_base, make_new)(native_base, n_args, n_kw, args); } return MP_OBJ_FROM_PTR(o); @@ -998,7 +998,7 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in); - if (self->make_new == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(self, make_new)) { #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("can't create instance")); #else @@ -1007,7 +1007,7 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp } // make new instance - mp_obj_t o = self->make_new(self, n_args, n_kw, args); + mp_obj_t o = MP_OBJ_TYPE_GET_SLOT(self, make_new)(self, n_args, n_kw, args); // return new instance return o; @@ -1109,7 +1109,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_type, MP_QSTR_type, MP_TYPE_FLAG_NONE, - type_make_new, + make_new, type_make_new, print, type_print, call, type_call, unary_op, mp_generic_unary_op, @@ -1141,7 +1141,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) } mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]); // TODO: Verify with CPy, tested on function type - if (t->make_new == NULL) { + if (!MP_OBJ_TYPE_HAS_SLOT(t, make_new)) { #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE mp_raise_TypeError(MP_ERROR_TEXT("type isn't an acceptable base type")); #else @@ -1163,35 +1163,35 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) } // Allocate a variable-sized mp_obj_type_t with as many slots as we need - // (currently 9, plus 1 for base, plus 1 for base-protocol). - // Note: 11 slots pushes it from 4 to 5 GC blocks. - mp_obj_type_t *o = m_new_obj_var0(mp_obj_type_t, void *, 9 + (bases_len ? 1 : 0) + (base_protocol ? 1 : 0)); + // (currently 10, plus 1 for base, plus 1 for base-protocol). + // Note: mp_obj_type_t is (2 + 3 + #slots) words, so going from 11 to 12 slots + // moves from 4 to 5 gc blocks. + mp_obj_type_t *o = m_new_obj_var0(mp_obj_type_t, void *, 10 + (bases_len ? 1 : 0) + (base_protocol ? 1 : 0)); o->base.type = &mp_type_type; o->flags = base_flags; o->name = name; - o->make_new = mp_obj_instance_make_new; - MP_OBJ_TYPE_SET_SLOT(o, print, instance_print, 0); - MP_OBJ_TYPE_SET_SLOT(o, call, mp_obj_instance_call, 1); - MP_OBJ_TYPE_SET_SLOT(o, unary_op, instance_unary_op, 2); - MP_OBJ_TYPE_SET_SLOT(o, binary_op, instance_binary_op, 3); - MP_OBJ_TYPE_SET_SLOT(o, attr, mp_obj_instance_attr, 4); - MP_OBJ_TYPE_SET_SLOT(o, subscr, instance_subscr, 5); - MP_OBJ_TYPE_SET_SLOT(o, iter, mp_obj_instance_getiter, 6); - // MP_OBJ_TYPE_SET_SLOT(o, iternext, not implemented) - MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 7); + MP_OBJ_TYPE_SET_SLOT(o, make_new, mp_obj_instance_make_new, 0); + MP_OBJ_TYPE_SET_SLOT(o, print, instance_print, 1); + MP_OBJ_TYPE_SET_SLOT(o, call, mp_obj_instance_call, 2); + MP_OBJ_TYPE_SET_SLOT(o, unary_op, instance_unary_op, 3); + MP_OBJ_TYPE_SET_SLOT(o, binary_op, instance_binary_op, 4); + MP_OBJ_TYPE_SET_SLOT(o, attr, mp_obj_instance_attr, 5); + MP_OBJ_TYPE_SET_SLOT(o, subscr, instance_subscr, 6); + MP_OBJ_TYPE_SET_SLOT(o, iter, mp_obj_instance_getiter, 7); + MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 8); mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict); - MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 8); + MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 9); if (bases_len > 0) { if (bases_len >= 2) { #if MICROPY_MULTIPLE_INHERITANCE - MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_tuple), 9); + MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_tuple), 10); #else mp_raise_NotImplementedError(MP_ERROR_TEXT("multiple inheritance not supported")); #endif } else { - MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_items[0]), 9); + MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_items[0]), 10); } // Inherit protocol from a base class. This allows to define an @@ -1199,7 +1199,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) // Python method calls, and any subclass inheriting from it will // support this feature. if (base_protocol) { - MP_OBJ_TYPE_SET_SLOT(o, protocol, base_protocol, 10); + MP_OBJ_TYPE_SET_SLOT(o, protocol, base_protocol, 11); } } @@ -1292,7 +1292,7 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // Allow a call super().__init__() to reach any native base classes. if (attr == MP_QSTR___init__) { - lookup.slot_offset = offsetof(mp_obj_type_t, make_new); + lookup.slot_offset = MP_OBJ_TYPE_OFFSETOF_SLOT(make_new); } if (!MP_OBJ_TYPE_HAS_SLOT(type, parent)) { @@ -1340,7 +1340,7 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_super, MP_QSTR_super, MP_TYPE_FLAG_NONE, - super_make_new, + make_new, super_make_new, print, super_print, attr, super_attr ); @@ -1463,12 +1463,12 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_staticmethod, MP_QSTR_staticmethod, MP_TYPE_FLAG_NONE, - static_class_method_make_new + make_new, static_class_method_make_new ); MP_DEFINE_CONST_OBJ_TYPE( mp_type_classmethod, MP_QSTR_classmethod, MP_TYPE_FLAG_NONE, - static_class_method_make_new + make_new, static_class_method_make_new ); diff --git a/py/objtype.h b/py/objtype.h index 2c613b904580b..76a290760c83f 100644 --- a/py/objtype.h +++ b/py/objtype.h @@ -46,8 +46,8 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *cls, const mp_obj_ty bool mp_obj_instance_is_callable(mp_obj_t self_in); mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); -#define mp_obj_is_instance_type(type) ((type)->make_new == mp_obj_instance_make_new) -#define mp_obj_is_native_type(type) ((type)->make_new != mp_obj_instance_make_new) +#define mp_obj_is_instance_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) == mp_obj_instance_make_new) +#define mp_obj_is_native_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) != mp_obj_instance_make_new) // this needs to be exposed for the above macros to work correctly mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); diff --git a/py/objzip.c b/py/objzip.c index 34d73465ef2bd..3c3138180a28f 100644 --- a/py/objzip.c +++ b/py/objzip.c @@ -70,6 +70,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_zip, MP_QSTR_zip, MP_TYPE_FLAG_ITER_IS_ITERNEXT, - zip_make_new, + make_new, zip_make_new, iter, zip_iternext ); diff --git a/py/profile.c b/py/profile.c index 2b9531e245ac5..fd2d61caa9f87 100644 --- a/py/profile.c +++ b/py/profile.c @@ -176,7 +176,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_settrace_codeobj, MP_QSTR_code, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, code_print, unary_op, mp_generic_unary_op, attr, code_attr @@ -247,7 +246,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_type_frame, MP_QSTR_frame, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, print, frame_print, unary_op, mp_generic_unary_op, attr, frame_attr diff --git a/py/runtime.c b/py/runtime.c index ec628bfe14b4a..da6c86d971546 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1076,7 +1076,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( mp_type_checked_fun, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF, - MP_TYPE_NULL_MAKE_NEW, call, checked_fun_call ); diff --git a/shared/runtime/mpirq.c b/shared/runtime/mpirq.c index 889fa7a9a82ed..f9a85ebbd61bc 100644 --- a/shared/runtime/mpirq.c +++ b/shared/runtime/mpirq.c @@ -129,7 +129,6 @@ MP_DEFINE_CONST_OBJ_TYPE( mp_irq_type, MP_QSTR_irq, MP_TYPE_FLAG_NONE, - MP_TYPE_NULL_MAKE_NEW, call, mp_irq_call, locals_dict, &mp_irq_locals_dict ); diff --git a/shared/runtime/sys_stdio_mphal.c b/shared/runtime/sys_stdio_mphal.c index 325f93dde1d89..ab5bdd34e73c1 100644 --- a/shared/runtime/sys_stdio_mphal.c +++ b/shared/runtime/sys_stdio_mphal.c @@ -127,7 +127,6 @@ MP_DEFINE_CONST_OBJ_TYPE( stdio_obj_type, MP_QSTR_FileIO, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, stdio_obj_print, protocol, &stdio_obj_stream_p, locals_dict, &stdio_locals_dict @@ -161,7 +160,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE( stdio_buffer_obj_type, MP_QSTR_FileIO, MP_TYPE_FLAG_ITER_IS_STREAM, - MP_TYPE_NULL_MAKE_NEW, print, stdio_obj_print, protocol, &stdio_buffer_obj_stream_p, locals_dict, &stdio_locals_dict From b41aaaa8a918a6645ebc6bfa4483bd17286f9263 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 17 Sep 2022 22:22:32 +1000 Subject: [PATCH 2628/3533] py/obj: Optimise code size and performance for make_new as a slot. The check for make_new (i.e. used to determine something's type) is now more complicated due to the slot access. This commit changes the inlining of a few frequently-used helpers to overall improve code size and performance. --- py/obj.h | 6 ++++-- py/objdict.c | 4 ++++ py/objexcept.c | 4 ++++ py/objstr.c | 20 ++++++++++++-------- py/objtype.c | 5 +++-- py/objtype.h | 6 ++---- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/py/obj.h b/py/obj.h index 34adfbd0f0427..8aa5b0a8e667b 100644 --- a/py/obj.h +++ b/py/obj.h @@ -521,6 +521,7 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); // mp_getiter_iternext_custom_t struct instance (with both .getiter and .iternext set). // If MP_TYPE_FLAG_ITER_IS_STREAM is set then the type implicitly gets a "return self" // getiter, and mp_stream_unbuffered_iter for iternext. +// If MP_TYPE_FLAG_INSTANCE_TYPE is set then this is an instance type (i.e. defined in Python). #define MP_TYPE_FLAG_NONE (0x0000) #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001) #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002) @@ -533,6 +534,7 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); #define MP_TYPE_FLAG_ITER_IS_ITERNEXT (0x0080) #define MP_TYPE_FLAG_ITER_IS_CUSTOM (0x0100) #define MP_TYPE_FLAG_ITER_IS_STREAM (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM) +#define MP_TYPE_FLAG_INSTANCE_TYPE (0x0200) typedef enum { PRINT_STR = 0, @@ -914,7 +916,7 @@ void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type); #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int)) #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str)) #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op)) -#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, make_new) == mp_obj_dict_make_new) +bool mp_obj_is_dict_or_ordereddict(mp_obj_t o); #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function)) mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict); @@ -1030,7 +1032,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in); mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in); // exception -#define mp_obj_is_native_exception_instance(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), make_new) == mp_obj_exception_make_new) +bool mp_obj_is_native_exception_instance(mp_obj_t self_in); bool mp_obj_is_exception_type(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in); bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type); diff --git a/py/objdict.c b/py/objdict.c index 68c33961c70c0..7fad5fc8f4ebb 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -33,6 +33,10 @@ #include "py/objtype.h" #include "py/objstr.h" +bool mp_obj_is_dict_or_ordereddict(mp_obj_t o) { + return mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, make_new) == mp_obj_dict_make_new; +} + const mp_obj_dict_t mp_const_empty_dict_obj = { .base = { .type = &mp_type_dict }, .map = { diff --git a/py/objexcept.c b/py/objexcept.c index 3b76ae62ccc69..515cbe7425b03 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -111,6 +111,10 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) { #endif #endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF +bool mp_obj_is_native_exception_instance(mp_obj_t self_in) { + return MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(self_in), make_new) == mp_obj_exception_make_new; +} + STATIC mp_obj_exception_t *get_native_exception(mp_obj_t self_in) { assert(mp_obj_is_exception_instance(self_in)); if (mp_obj_is_native_exception_instance(self_in)) { diff --git a/py/objstr.c b/py/objstr.c index 50ab0018ed6e6..62d7bfb4cc396 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -63,6 +63,10 @@ STATIC void str_check_arg_type(const mp_obj_type_t *self_type, const mp_obj_t ar } } +STATIC void check_is_str_or_bytes(mp_obj_t self_in) { + mp_check_self(mp_obj_is_str_or_bytes(self_in)); +} + /******************************************************************************/ /* str */ @@ -468,7 +472,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { - mp_check_self(mp_obj_is_str_or_bytes(self_in)); + check_is_str_or_bytes(self_in); const mp_obj_type_t *self_type = mp_obj_get_type(self_in); const mp_obj_type_t *ret_type = self_type; @@ -724,7 +728,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit); STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) { const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); - mp_check_self(mp_obj_is_str_or_bytes(args[0])); + check_is_str_or_bytes(args[0]); // check argument type str_check_arg_type(self_type, args[1]); @@ -820,7 +824,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith); enum { LSTRIP, RSTRIP, STRIP }; STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) { - mp_check_self(mp_obj_is_str_or_bytes(args[0])); + check_is_str_or_bytes(args[0]); const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); const byte *chars_to_del; @@ -1422,7 +1426,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - mp_check_self(mp_obj_is_str_or_bytes(args[0])); + check_is_str_or_bytes(args[0]); GET_STR_DATA_LEN(args[0], str, len); int arg_i = 0; @@ -1433,7 +1437,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format); #if MICROPY_PY_BUILTINS_STR_OP_MODULO STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict) { - mp_check_self(mp_obj_is_str_or_bytes(pattern)); + check_is_str_or_bytes(pattern); GET_STR_DATA_LEN(pattern, str, len); #if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_TERSE @@ -1639,7 +1643,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_ // The implementation is optimized, returning the original string if there's // nothing to replace. STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) { - mp_check_self(mp_obj_is_str_or_bytes(args[0])); + check_is_str_or_bytes(args[0]); mp_int_t max_rep = -1; if (n_args == 4) { @@ -1742,7 +1746,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); #if MICROPY_PY_BUILTINS_STR_COUNT STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) { const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); - mp_check_self(mp_obj_is_str_or_bytes(args[0])); + check_is_str_or_bytes(args[0]); // check argument type str_check_arg_type(self_type, args[1]); @@ -1782,7 +1786,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); #if MICROPY_PY_BUILTINS_STR_PARTITION STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) { - mp_check_self(mp_obj_is_str_or_bytes(self_in)); + check_is_str_or_bytes(self_in); const mp_obj_type_t *self_type = mp_obj_get_type(self_in); str_check_arg_type(self_type, arg); diff --git a/py/objtype.c b/py/objtype.c index db364e0e5a5de..909fc833931f6 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -277,7 +277,7 @@ STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k mp_printf(print, "<%s object at %p>", mp_obj_get_type_str(self_in), self); } -mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) { assert(mp_obj_is_instance_type(self)); // look for __new__ function @@ -1131,7 +1131,8 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) uint16_t base_flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_EQ_HAS_NEQ_TEST - | MP_TYPE_FLAG_ITER_IS_GETITER; + | MP_TYPE_FLAG_ITER_IS_GETITER + | MP_TYPE_FLAG_INSTANCE_TYPE; size_t bases_len; mp_obj_t *bases_items; mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items); diff --git a/py/objtype.h b/py/objtype.h index 76a290760c83f..839cc6d146bb1 100644 --- a/py/objtype.h +++ b/py/objtype.h @@ -46,10 +46,8 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *cls, const mp_obj_ty bool mp_obj_instance_is_callable(mp_obj_t self_in); mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); -#define mp_obj_is_instance_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) == mp_obj_instance_make_new) -#define mp_obj_is_native_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) != mp_obj_instance_make_new) -// this needs to be exposed for the above macros to work correctly -mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); +#define mp_obj_is_instance_type(type) ((type)->flags & MP_TYPE_FLAG_INSTANCE_TYPE) +#define mp_obj_is_native_type(type) (!((type)->flags & MP_TYPE_FLAG_INSTANCE_TYPE)) // this needs to be exposed for mp_getiter mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf); From d94141e1473aebae0d3c63aeaa8397651ad6fa01 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 17 Sep 2022 23:57:12 +1000 Subject: [PATCH 2629/3533] py/persistentcode: Introduce .mpy sub-version. The intent is to allow us to make breaking changes to the native ABI (e.g. changes to dynruntime.h) without needing the bytecode version to increment. With this commit the two bits previously used for the feature flags (but now unused as of .mpy version 6) encode a sub-version. A bytecode-only .mpy file can be loaded as long as MPY_VERSION matches, but a native .mpy (i.e. one with an arch set) must also match MPY_SUB_VERSION. This allows 3 additional updates to the native ABI per bytecode revision. The sub-version is set to 1 because the previous commits that changed the layout of mp_obj_type_t have changed the native ABI. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- docs/reference/mpyfiles.rst | 6 ++++++ mpy-cross/main.c | 2 +- py/persistentcode.c | 6 +++--- py/persistentcode.h | 23 +++++++++++------------ tests/micropython/import_mpy_native.py | 7 ++++--- tests/micropython/import_mpy_native_gc.py | 12 +++++++----- tools/mpy-tool.py | 10 ++++++++-- tools/mpy_ld.py | 7 ++++++- 8 files changed, 46 insertions(+), 27 deletions(-) diff --git a/docs/reference/mpyfiles.rst b/docs/reference/mpyfiles.rst index fcb49965652f4..d93a86383e98c 100644 --- a/docs/reference/mpyfiles.rst +++ b/docs/reference/mpyfiles.rst @@ -27,6 +27,11 @@ Compatibility is based on the following: * Version of the .mpy file: the version of the file must match the version supported by the system loading it. +* Sub-version of the .mpy file: if the .mpy file contains native machine code + then the sub-version of the file must match the version support by the + system loading it. Otherwise, if there is no native machine code in the .mpy + file, then the sub-version is ignored when loading. + * Small integer bits: the .mpy file will require a minimum number of bits in a small integer and the system loading it must support at least this many bits. @@ -55,6 +60,7 @@ If importing an .mpy file fails then try the following: 'armv6', 'armv6m', 'armv7m', 'armv7em', 'armv7emsp', 'armv7emdp', 'xtensa', 'xtensawin'][sys_mpy >> 10] print('mpy version:', sys_mpy & 0xff) + print('mpy sub-version:', sys_mpy >> 8 & 3) print('mpy flags:', end='') if arch: print(' -march=' + arch, end='') diff --git a/mpy-cross/main.c b/mpy-cross/main.c index f3ffff61fdf80..4975c8ddb2052 100644 --- a/mpy-cross/main.c +++ b/mpy-cross/main.c @@ -228,7 +228,7 @@ MP_NOINLINE int main_(int argc, char **argv) { a += 1; } else if (strcmp(argv[a], "--version") == 0) { printf("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE - "; mpy-cross emitting mpy v" MP_STRINGIFY(MPY_VERSION) "\n"); + "; mpy-cross emitting mpy v" MP_STRINGIFY(MPY_VERSION) "." MP_STRINGIFY(MPY_SUB_VERSION) "\n"); return 0; } else if (strcmp(argv[a], "-v") == 0) { mp_verbose_flag++; diff --git a/py/persistentcode.c b/py/persistentcode.c index 1b1741f2614e2..de84c17359e3c 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -393,14 +393,14 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *context) { byte header[4]; read_bytes(reader, header, sizeof(header)); + byte arch = MPY_FEATURE_DECODE_ARCH(header[2]); if (header[0] != 'M' || header[1] != MPY_VERSION - || MPY_FEATURE_DECODE_FLAGS(header[2]) != MPY_FEATURE_FLAGS + || (arch != MP_NATIVE_ARCH_NONE && MPY_FEATURE_DECODE_SUB_VERSION(header[2]) != MPY_SUB_VERSION) || header[3] > MP_SMALL_INT_BITS) { mp_raise_ValueError(MP_ERROR_TEXT("incompatible .mpy file")); } if (MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE) { - byte arch = MPY_FEATURE_DECODE_ARCH(header[2]); if (!MPY_FEATURE_ARCH_TEST(arch)) { if (MPY_FEATURE_ARCH_TEST(MP_NATIVE_ARCH_NONE)) { // On supported ports this can be resolved by enabling feature, eg @@ -596,7 +596,7 @@ void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print) { byte header[4] = { 'M', MPY_VERSION, - MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS_DYNAMIC), + MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION), #if MICROPY_DYNAMIC_COMPILER mp_dynamic_compiler.small_int_bits, #else diff --git a/py/persistentcode.h b/py/persistentcode.h index 29ccce4a3d968..e664358737613 100644 --- a/py/persistentcode.h +++ b/py/persistentcode.h @@ -30,24 +30,23 @@ #include "py/reader.h" #include "py/emitglue.h" -// The current version of .mpy files +// The current version of .mpy files. A bytecode-only .mpy file can be loaded +// as long as MPY_VERSION matches, but a native .mpy (i.e. one with an arch +// set) must also match MPY_SUB_VERSION. This allows 3 additional updates to +// the native ABI per bytecode revision. #define MPY_VERSION 6 +#define MPY_SUB_VERSION 1 -// Macros to encode/decode flags to/from the feature byte -#define MPY_FEATURE_ENCODE_FLAGS(flags) (flags) -#define MPY_FEATURE_DECODE_FLAGS(feat) ((feat) & 3) +// Macros to encode/decode sub-version to/from the feature byte. This replaces +// the bits previously used to encode the flags (map caching and unicode) +// which are no longer used starting at .mpy version 6. +#define MPY_FEATURE_ENCODE_SUB_VERSION(version) (version) +#define MPY_FEATURE_DECODE_SUB_VERSION(feat) ((feat) & 3) // Macros to encode/decode native architecture to/from the feature byte #define MPY_FEATURE_ENCODE_ARCH(arch) ((arch) << 2) #define MPY_FEATURE_DECODE_ARCH(feat) ((feat) >> 2) -// The feature flag bits encode the compile-time config options that affect -// the generate bytecode. Note: no longer used. -// (formerly MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE and MICROPY_PY_BUILTINS_STR_UNICODE). -#define MPY_FEATURE_FLAGS (0) -// This is a version of the flags that can be configured at runtime. -#define MPY_FEATURE_FLAGS_DYNAMIC (0) - // Define the host architecture #if MICROPY_EMIT_X86 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86) @@ -82,7 +81,7 @@ // 16-bit little-endian integer with the second and third bytes of supported .mpy files #define MPY_FILE_HEADER_INT (MPY_VERSION \ - | (MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8) + | (MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8) enum { MP_NATIVE_ARCH_NONE = 0, diff --git a/tests/micropython/import_mpy_native.py b/tests/micropython/import_mpy_native.py index 449371ddac06a..73e20694cc27e 100644 --- a/tests/micropython/import_mpy_native.py +++ b/tests/micropython/import_mpy_native.py @@ -11,7 +11,8 @@ raise SystemExit mpy_arch = usys.implementation._mpy >> 8 -if mpy_arch == 0: +if mpy_arch >> 2 == 0: + # This system does not support .mpy files containing native code print("SKIP") raise SystemExit @@ -54,8 +55,8 @@ def open(self, path, mode): valid_header = bytes([77, 6, mpy_arch, 31]) # fmt: off user_files = { - # bad architecture - '/mod0.mpy': b'M\x06\xfc\x1f', + # bad architecture (mpy_arch needed for sub-version) + '/mod0.mpy': bytes([77, 6, 0xfc | mpy_arch, 31]), # test loading of viper and asm '/mod1.mpy': valid_header + ( diff --git a/tests/micropython/import_mpy_native_gc.py b/tests/micropython/import_mpy_native_gc.py index bc8dcafdc7637..e18720fb31473 100644 --- a/tests/micropython/import_mpy_native_gc.py +++ b/tests/micropython/import_mpy_native_gc.py @@ -46,24 +46,26 @@ def open(self, path, mode): # Pre-compiled examples/natmod/features0 example for various architectures, keyed -# by the required value of sys.implementation._mpy. +# by the required value of sys.implementation._mpy (without sub-version). features0_file_contents = { # -march=x64 - 0x806: b'M\x06\x08\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x8a\x02\xe9/\x00\x00\x00SH\x8b\x1d\x83\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dQ\x00\x00\x00H\x8bG\x08L\x8bc(H\x8bx\x08A\xff\xd4H\x8d5+\x00\x00\x00H\x89\xc5H\x8b\x059\x00\x00\x00\x0f\xb7x\x02\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11$\r&\xa3 \x01"\xff', + 0x806: b'M\x06\x09\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x8a\x02\xe9/\x00\x00\x00SH\x8b\x1d\x83\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dQ\x00\x00\x00H\x8bG\x08L\x8bc(H\x8bx\x08A\xff\xd4H\x8d5+\x00\x00\x00H\x89\xc5H\x8b\x059\x00\x00\x00\x0f\xb7x\x02\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11$\r&\xa3 \x01"\xff', # -march=armv6m - 0x1006: b"M\x06\x10\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x88\x02\x18\xe0\x00\x00\x10\xb5\tK\tJ{D\x9cX\x02!\xe3h\x98G\x03\x00\x01 \x00+\x02\xd0XC\x01;\xfa\xe7\x02!#i\x98G\x10\xbd\xc0Fj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\nN\nK~D\xf4XChgiXh\xb8G\x05\x00\x07K\x08I\xf3XyDX\x88ck\x98G(\x00\xb8G h\xf8\xbd\xc0F:\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11<\r>\xa38\x01:\xff", + 0x1006: b"M\x06\x11\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x88\x02\x18\xe0\x00\x00\x10\xb5\tK\tJ{D\x9cX\x02!\xe3h\x98G\x03\x00\x01 \x00+\x02\xd0XC\x01;\xfa\xe7\x02!#i\x98G\x10\xbd\xc0Fj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\nN\nK~D\xf4XChgiXh\xb8G\x05\x00\x07K\x08I\xf3XyDX\x88ck\x98G(\x00\xb8G h\xf8\xbd\xc0F:\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11<\r>\xa38\x01:\xff", } # Populate armv7m-derived archs based on armv6m. for arch in (0x1406, 0x1806, 0x1C06, 0x2006): features0_file_contents[arch] = features0_file_contents[0x1006] -if sys.implementation._mpy not in features0_file_contents: +# Check that a .mpy exists for the target (ignore sub-version in lookup). +sys_implementation_mpy = sys.implementation._mpy & ~(3 << 8) +if sys_implementation_mpy not in features0_file_contents: print("SKIP") raise SystemExit # These are the test .mpy files. -user_files = {"/features0.mpy": features0_file_contents[sys.implementation._mpy]} +user_files = {"/features0.mpy": features0_file_contents[sys_implementation_mpy]} # Create and mount a user filesystem. uos.mount(UserFS(user_files), "/userfs") diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 31212fd5bdda8..8b644c137f4c7 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -88,6 +88,7 @@ def __str__(self): class Config: MPY_VERSION = 6 + MPY_SUB_VERSION = 1 MICROPY_LONGINT_IMPL_NONE = 0 MICROPY_LONGINT_IMPL_LONGLONG = 1 MICROPY_LONGINT_IMPL_MPZ = 2 @@ -1335,6 +1336,9 @@ def read_mpy(filename): feature_byte = header[2] mpy_native_arch = feature_byte >> 2 if mpy_native_arch != MP_NATIVE_ARCH_NONE: + mpy_sub_version = feature_byte & 3 + if mpy_sub_version != config.MPY_SUB_VERSION: + raise MPYReadError(filename, "incompatible .mpy sub-version") if config.native_arch == MP_NATIVE_ARCH_NONE: config.native_arch = mpy_native_arch elif config.native_arch != mpy_native_arch: @@ -1658,7 +1662,9 @@ def merge_mpy(compiled_modules, output_file): else: main_cm_idx = None for idx, cm in enumerate(compiled_modules): - if cm.header[2]: + feature_byte = cm.header[2] + mpy_native_arch = feature_byte >> 2 + if mpy_native_arch: # Must use qstr_table and obj_table from this raw_code if main_cm_idx is not None: raise Exception("can't merge files when more than one contains native code") @@ -1670,7 +1676,7 @@ def merge_mpy(compiled_modules, output_file): header = bytearray(4) header[0] = ord("M") header[1] = config.MPY_VERSION - header[2] = config.native_arch << 2 + header[2] = config.native_arch << 2 | config.MPY_SUB_VERSION header[3] = config.mp_small_int_bits merged_mpy.extend(header) diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index 09ea90dcd1106..05e70709ad8a4 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -36,6 +36,7 @@ # MicroPython constants MPY_VERSION = 6 +MPY_SUB_VERSION = 1 MP_CODE_BYTECODE = 2 MP_CODE_NATIVE_VIPER = 4 MP_NATIVE_ARCH_X86 = 1 @@ -917,7 +918,11 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs): out.open(fmpy) # MPY: header - out.write_bytes(bytearray([ord("M"), MPY_VERSION, env.arch.mpy_feature, MP_SMALL_INT_BITS])) + out.write_bytes( + bytearray( + [ord("M"), MPY_VERSION, env.arch.mpy_feature | MPY_SUB_VERSION, MP_SMALL_INT_BITS] + ) + ) # MPY: n_qstr out.write_uint(1 + len(native_qstr_vals)) From 15d0615d5cad430930d160a8ca3e809d7b82da32 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 13 Sep 2022 14:16:58 +1000 Subject: [PATCH 2630/3533] py/objmodule: Add support for __dict__. This matches class `__dict__`, and is similarly gated on MICROPY_CPYTHON_COMPAT. Unlike class though, because modules's globals are actually dict instances, the result is a mutable dictionary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- py/objmodule.c | 4 ++++ tests/basics/module_dict.py | 13 +++++++++++++ tests/import/module_dict.py | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/basics/module_dict.py create mode 100644 tests/import/module_dict.py diff --git a/py/objmodule.c b/py/objmodule.c index d14a59b8632c4..63e9fa47571b0 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -89,6 +89,10 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); if (elem != NULL) { dest[0] = elem->value; + #if MICROPY_CPYTHON_COMPAT + } else if (attr == MP_QSTR___dict__) { + dest[0] = MP_OBJ_FROM_PTR(self->globals); + #endif #if MICROPY_MODULE_GETATTR } else if (attr != MP_QSTR___getattr__) { elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___getattr__), MP_MAP_LOOKUP); diff --git a/tests/basics/module_dict.py b/tests/basics/module_dict.py new file mode 100644 index 0000000000000..c847294f08916 --- /dev/null +++ b/tests/basics/module_dict.py @@ -0,0 +1,13 @@ +# test __dict__ attribute of a built-in module +# see import/module_dict.py for the equivalent test on user modules + +import sys + +if not hasattr(sys, "__dict__"): + print("SKIP") + raise SystemExit + + +# dict of a built-in module (read-only) +print(type(sys.__dict__)) +print(sys.__dict__["__name__"]) diff --git a/tests/import/module_dict.py b/tests/import/module_dict.py new file mode 100644 index 0000000000000..431d80b3b43b5 --- /dev/null +++ b/tests/import/module_dict.py @@ -0,0 +1,17 @@ +# test __dict__ attribute of a user module + +import sys + +if not hasattr(sys, "__dict__"): + print("SKIP") + raise SystemExit + + +import import1b + +# dict of a user module (read/write) +print(import1b.var) +print(import1b.__dict__["var"]) +import1b.__dict__["var"] = "hello" +print(import1b.var) +print(import1b.__dict__["var"]) From cc588ac3a91a16807242347e8b182d4bc8b7f139 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 19 Sep 2022 10:35:12 +1000 Subject: [PATCH 2631/3533] py/runtime: Add mp_raise_OSError_with_filename helper function. Useful when more detail is needed for an OSError associated with a file. Signed-off-by: Damien George --- py/runtime.c | 9 +++++++++ py/runtime.h | 1 + 2 files changed, 10 insertions(+) diff --git a/py/runtime.c b/py/runtime.c index da6c86d971546..23fae6041dd0f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1692,6 +1692,15 @@ NORETURN void mp_raise_OSError(int errno_) { mp_raise_type_arg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)); } +NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename) { + vstr_t vstr; + vstr_init(&vstr, 32); + vstr_printf(&vstr, "can't open %s", filename); + mp_obj_t o_str = mp_obj_new_str_from_vstr(&vstr); + mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(errno_), MP_OBJ_FROM_PTR(o_str)}; + nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args)); +} + #if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK NORETURN void mp_raise_recursion_depth(void) { mp_raise_type_arg(&mp_type_RuntimeError, MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded)); diff --git a/py/runtime.h b/py/runtime.h index 779b66f2bd25c..36b3caa6c73e6 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -196,6 +196,7 @@ NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg); NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg); NORETURN void mp_raise_StopIteration(mp_obj_t arg); NORETURN void mp_raise_OSError(int errno_); +NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename); NORETURN void mp_raise_recursion_depth(void); #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG From fb77be150636090d69460578ed8458d84db02614 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 19 Sep 2022 10:36:17 +1000 Subject: [PATCH 2632/3533] py: Include filename in errors from loading/saving files via "open". This improves error messages in mpy-cross: - When loading a .py file that doesn't exist (or can't be opened) it now includes the filename in the OSError. - When saving a .mpy file that can't be opened it now raises an exception (prior, it would silently fail), and includes the filename in the OSError. Signed-off-by: Damien George --- py/persistentcode.c | 3 +++ py/reader.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/py/persistentcode.c b/py/persistentcode.c index de84c17359e3c..67c8f327f002c 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -644,6 +644,9 @@ void mp_raw_code_save_file(mp_compiled_module_t *cm, const char *filename) { MP_THREAD_GIL_EXIT(); int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); MP_THREAD_GIL_ENTER(); + if (fd < 0) { + mp_raise_OSError_with_filename(errno, filename); + } mp_print_t fd_print = {(void *)(intptr_t)fd, fd_print_strn}; mp_raw_code_save(cm, &fd_print); MP_THREAD_GIL_EXIT(); diff --git a/py/reader.c b/py/reader.c index d68406b1c68b3..57b12b3b655ca 100644 --- a/py/reader.c +++ b/py/reader.c @@ -139,7 +139,7 @@ void mp_reader_new_file(mp_reader_t *reader, const char *filename) { int fd = open(filename, O_RDONLY, 0644); MP_THREAD_GIL_ENTER(); if (fd < 0) { - mp_raise_OSError(errno); + mp_raise_OSError_with_filename(errno, filename); } mp_reader_new_file_from_fd(reader, fd, true); } From 9ae8d3820474a9525d707b1e19f7703721aec1c8 Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 12 Sep 2022 14:45:39 +0200 Subject: [PATCH 2633/3533] extmod/vfs_posix_file: Implement finaliser for files. Prevent handle leaks when file objects aren't closed explicitly and fix some MICROPY_CPYTHON_COMPAT issues: this wasn't properly adhered to because #ifdef was used so it was always on, and closing files multiple times should be avoided unconditionally. --- extmod/vfs_posix_file.c | 15 ++++++++------- tests/extmod/vfs_posix.py | 29 +++++++++++++++++++++++++++++ tests/extmod/vfs_posix.py.exp | 1 + 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 4a37489686cd9..ea19de7fd04af 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -46,7 +46,7 @@ typedef struct _mp_obj_vfs_posix_file_t { int fd; } mp_obj_vfs_posix_file_t; -#ifdef MICROPY_CPYTHON_COMPAT +#if MICROPY_CPYTHON_COMPAT STATIC void check_fd_is_open(const mp_obj_vfs_posix_file_t *o) { if (o->fd < 0) { mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file")); @@ -63,7 +63,7 @@ STATIC void vfs_posix_file_print(const mp_print_t *print, mp_obj_t self_in, mp_p } mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in) { - mp_obj_vfs_posix_file_t *o = m_new_obj(mp_obj_vfs_posix_file_t); + mp_obj_vfs_posix_file_t *o = m_new_obj_with_finaliser(mp_obj_vfs_posix_file_t); const char *mode_s = mp_obj_str_get_str(mode_in); int mode_rw = 0, mode_x = 0; @@ -185,12 +185,12 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_ return 0; } case MP_STREAM_CLOSE: - MP_THREAD_GIL_EXIT(); - close(o->fd); - MP_THREAD_GIL_ENTER(); - #ifdef MICROPY_CPYTHON_COMPAT + if (o->fd >= 0) { + MP_THREAD_GIL_EXIT(); + close(o->fd); + MP_THREAD_GIL_ENTER(); + } o->fd = -1; - #endif return 0; case MP_STREAM_GET_FILENO: return o->fd; @@ -237,6 +237,7 @@ STATIC const mp_rom_map_elem_t vfs_posix_rawfile_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) }, { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&vfs_posix_file___exit___obj) }, }; diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py index d193236696ea1..3c23140065af8 100644 --- a/tests/extmod/vfs_posix.py +++ b/tests/extmod/vfs_posix.py @@ -1,6 +1,7 @@ # Test for VfsPosix try: + import gc import uos uos.VfsPosix @@ -51,6 +52,34 @@ print(f.read()) f.close() +# file finaliser, also see vfs_fat_finaliser.py +names = [temp_dir + "/x%d" % i for i in range(4)] +basefd = temp_dir + "/nextfd1" +nextfd = temp_dir + "/nextfd2" + +with open(basefd, "w") as f: + base_file_no = f.fileno() + +for i in range(1024): # move GC head forwards by allocating a lot of single blocks + [] + + +def write_files_without_closing(): + for n in names: + open(n, "w").write(n) + sorted(list(range(128)), key=lambda x: x) # use up Python and C stack so f is really gone + + +write_files_without_closing() +gc.collect() + +with open(nextfd, "w") as f: + next_file_no = f.fileno() + print("next_file_no <= base_file_no", next_file_no <= base_file_no) + +for n in names + [basefd, nextfd]: + uos.remove(n) + # rename uos.rename(temp_dir + "/test", temp_dir + "/test2") print(uos.listdir(temp_dir)) diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp index eb9ab43106e1d..99922e621d4c2 100644 --- a/tests/extmod/vfs_posix.py.exp +++ b/tests/extmod/vfs_posix.py.exp @@ -4,6 +4,7 @@ True hello +next_file_no <= base_file_no True ['test2'] ['test2'] From 6ecdf1a240e43bd60c824c7efd575c0a82d02d7e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 19 Sep 2022 12:03:38 +1000 Subject: [PATCH 2634/3533] tests/frozen: Move frozentest.mpy from ports/ to tests/. frozentest.mpy was previously duplicated in ports/minimal and ports/powerpc. This needs to be re-generated on every .mpy version increase, so might as well just have a single copy of it. Signed-off-by: Jim Mussared --- ports/minimal/Makefile | 2 +- ports/powerpc/Makefile | 2 +- ports/powerpc/frozentest.mpy | Bin 196 -> 0 bytes ports/powerpc/frozentest.py | 7 ------- tests/frozen/README.md | 2 ++ {ports/minimal => tests/frozen}/frozentest.mpy | Bin {ports/minimal => tests/frozen}/frozentest.py | 0 tools/ci.sh | 4 ++-- 8 files changed, 6 insertions(+), 11 deletions(-) delete mode 100644 ports/powerpc/frozentest.mpy delete mode 100644 ports/powerpc/frozentest.py create mode 100644 tests/frozen/README.md rename {ports/minimal => tests/frozen}/frozentest.mpy (100%) rename {ports/minimal => tests/frozen}/frozentest.py (100%) diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 5ba6514c9ed43..0e875cc2491a6 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -81,7 +81,7 @@ else all: $(BUILD)/firmware.elf endif -$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h +$(BUILD)/_frozen_mpy.c: $(TOP)/tests/frozen/frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h $(ECHO) "MISC freezing bytecode" $(Q)$(TOP)/tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $< > $@ diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index 12ae485bb8f04..0986fd13eba0f 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -50,7 +50,7 @@ OBJ += $(BUILD)/head.o all: $(BUILD)/firmware.elf $(BUILD)/firmware.map $(BUILD)/firmware.bin -$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h +$(BUILD)/_frozen_mpy.c: $(TOP)/tests/frozen/frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h $(ECHO) "MISC freezing bytecode" $(Q)$(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=mpz $< > $@ diff --git a/ports/powerpc/frozentest.mpy b/ports/powerpc/frozentest.mpy deleted file mode 100644 index 99581617ac3d13e416722a63852da14088ff8db7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 196 zcmeZeW0GfOmP#wiuS(4;Ni8nXE2w1PZ!Bjk4X9*b%4A?wN>s?n&r4S*E-A_cQY9IQ zB?_6v3VHcJ3aFtdFExdMRV@)kJ|nSMp)@ZuIX@*;;oQb^o6c=kNX|$sDrR8gPO1c2 zqF`ubY{JOUVj#fBs15{LYJyAx%qEPHf=tQ`5Q-5(F@Y(iUGHa!0$B}CKH*|4f=q%e QKu+V+@RsY&`jLUj0I@qak^lez diff --git a/ports/powerpc/frozentest.py b/ports/powerpc/frozentest.py deleted file mode 100644 index 78cdd60bf0431..0000000000000 --- a/ports/powerpc/frozentest.py +++ /dev/null @@ -1,7 +0,0 @@ -print("uPy") -print("a long string that is not interned") -print("a string that has unicode αβγ chars") -print(b"bytes 1234\x01") -print(123456789) -for i in range(4): - print(i) diff --git a/tests/frozen/README.md b/tests/frozen/README.md new file mode 100644 index 0000000000000..bd786d5a3c4cc --- /dev/null +++ b/tests/frozen/README.md @@ -0,0 +1,2 @@ +This is a .mpy built against the current .mpy version that can be used to test +freezing without a dependency on mpy-cross. diff --git a/ports/minimal/frozentest.mpy b/tests/frozen/frozentest.mpy similarity index 100% rename from ports/minimal/frozentest.mpy rename to tests/frozen/frozentest.mpy diff --git a/ports/minimal/frozentest.py b/tests/frozen/frozentest.py similarity index 100% rename from ports/minimal/frozentest.py rename to tests/frozen/frozentest.py diff --git a/tools/ci.sh b/tools/ci.sh index 9c670b7be5ead..9963f7796cc06 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -75,8 +75,8 @@ function ci_mpy_format_setup { function ci_mpy_format_test { # Test mpy-tool.py dump feature on bytecode - python2 ./tools/mpy-tool.py -xd ports/minimal/frozentest.mpy - python3 ./tools/mpy-tool.py -xd ports/minimal/frozentest.mpy + python2 ./tools/mpy-tool.py -xd tests/frozen/frozentest.mpy + python3 ./tools/mpy-tool.py -xd tests/frozen/frozentest.mpy # Test mpy-tool.py dump feature on native code make -C examples/natmod/features1 From 920da9c5e30da58d5f77b2c5e28c74e3670e6a3b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 19 Sep 2022 12:05:39 +1000 Subject: [PATCH 2635/3533] unix/variants/coverage: Add test for manifest freeze_mpy(). This uses the frozentest.mpy that is also used by ports/minimal. Also fixes two bugs that these new tests picked up: - File extension matching in manifestfile.py. - Handling of freeze_mpy results in makemanifest. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- ports/unix/variants/coverage/manifest.py | 1 + tests/unix/extra_coverage.py | 5 +++++ tests/unix/extra_coverage.py.exp | 10 ++++++++++ tools/makemanifest.py | 6 +++--- tools/manifestfile.py | 17 +++++++++++++---- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ports/unix/variants/coverage/manifest.py b/ports/unix/variants/coverage/manifest.py index 6111050884222..7c3d9a6b64d69 100644 --- a/ports/unix/variants/coverage/manifest.py +++ b/ports/unix/variants/coverage/manifest.py @@ -1,2 +1,3 @@ freeze_as_str("frzstr") freeze_as_mpy("frzmpy") +freeze_mpy("$(MPY_DIR)/tests/frozen") diff --git a/tests/unix/extra_coverage.py b/tests/unix/extra_coverage.py index bb22485026e89..00226a68e6010 100644 --- a/tests/unix/extra_coverage.py +++ b/tests/unix/extra_coverage.py @@ -96,3 +96,8 @@ from frzqstr import returns_NULL print(returns_NULL()) + +# test for freeze_mpy +import frozentest + +print(frozentest.__file__) diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4d6e1e08567d4..29411545569d3 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -199,3 +199,13 @@ X '\x1b' b'\x00\xff' NULL +uPy +a long string that is not interned +a string that has unicode αβγ chars +b'bytes 1234\x01' +123456789 +0 +1 +2 +3 +frozentest.py diff --git a/tools/makemanifest.py b/tools/makemanifest.py index 9dd8815aac5f7..b2e4f7d8441b6 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -210,9 +210,9 @@ def main(): ts_outfile = get_timestamp(outfile) mpy_files.append(outfile) else: - assert kind == manifestfile.KIND_FREEZE_MPY - mpy_files.append(full_path) - ts_outfile = timestamp + assert result.kind == manifestfile.KIND_FREEZE_MPY + mpy_files.append(result.full_path) + ts_outfile = result.timestamp ts_newest = max(ts_newest, ts_outfile) # Check if output file needs generating diff --git a/tools/manifestfile.py b/tools/manifestfile.py index 84c79ed82b28f..a4d056137ffc9 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -394,28 +394,37 @@ def freeze(self, path, script=None, opt=None): `opt` is the optimisation level to pass to mpy-cross when compiling .py to .mpy. """ - self._freeze_internal(path, script, exts=(".py", ".mpy"), kind=KIND_FREEZE_AUTO, opt=opt) + self._freeze_internal( + path, + script, + exts=( + ".py", + ".mpy", + ), + kind=KIND_FREEZE_AUTO, + opt=opt, + ) def freeze_as_str(self, path): """ Freeze the given `path` and all .py scripts within it as a string, which will be compiled upon import. """ - self._search(path, None, None, exts=(".py"), kind=KIND_FREEZE_AS_STR) + self._search(path, None, None, exts=(".py",), kind=KIND_FREEZE_AS_STR) def freeze_as_mpy(self, path, script=None, opt=None): """ Freeze the input (see above) by first compiling the .py scripts to .mpy files, then freezing the resulting .mpy files. """ - self._freeze_internal(path, script, exts=(".py"), kind=KIND_FREEZE_AS_MPY, opt=opt) + self._freeze_internal(path, script, exts=(".py",), kind=KIND_FREEZE_AS_MPY, opt=opt) def freeze_mpy(self, path, script=None, opt=None): """ Freeze the input (see above), which must be .mpy files that are frozen directly. """ - self._freeze_internal(path, script, exts=(".mpy"), kind=KIND_FREEZE_MPY, opt=opt) + self._freeze_internal(path, script, exts=(".mpy",), kind=KIND_FREEZE_MPY, opt=opt) # Generate a temporary file with a line appended to the end that adds __version__. From 7589d86b6b6ea4232ff5614f3e73565529d4b3bb Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 20 Sep 2022 08:54:53 +1000 Subject: [PATCH 2636/3533] tests/run-multitests: Extend usage information. --- tests/run-multitests.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index bd6cc70f7a516..b5ba83b0ad345 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -3,6 +3,11 @@ # This file is part of the MicroPython project, http://micropython.org/ # The MIT License (MIT) # Copyright (c) 2020 Damien P. George +# +# run-multitests.py +# Runs a test suite that relies on two micropython instances/devices +# interacting in some way. Typically used to test networking / bluetooth etc. + import sys, os, time, re, select import argparse @@ -471,7 +476,10 @@ def run_tests(test_files, instances_truth, instances_test): def main(): global cmd_args - cmd_parser = argparse.ArgumentParser(description="Run network tests for MicroPython") + cmd_parser = argparse.ArgumentParser( + description="Run network tests for MicroPython", + formatter_class=argparse.RawTextHelpFormatter, + ) cmd_parser.add_argument( "-s", "--show-output", action="store_true", help="show test output after running" ) @@ -488,6 +496,14 @@ def main(): default=1, help="repeat the test with this many permutations of the instance order", ) + cmd_parser.epilog = ( + "Supported instance types:\r\n" + " -i pyb: physical device (eg. pyboard) on provided repl port.\n" + " -i micropython unix micropython instance, path customised with MICROPY_MICROPYTHON env.\n" + " -i cpython desktop python3 instance, path customised with MICROPY_CPYTHON3 env.\n" + " -i exec: custom program run on provided path.\n" + "Each instance arg can optionally have custom env provided, eg. ,ENV=VAR,ENV=VAR...\n" + ) cmd_parser.add_argument("files", nargs="+", help="input test files") cmd_args = cmd_parser.parse_args() From 13c4470fd04981fce3fd29771e1d973febbe7d60 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 20 Sep 2022 08:54:58 +1000 Subject: [PATCH 2637/3533] tests/run-multitests: Make paths more deterministic. Allows running from a different directory, etc. This work was funded by Planet Innovation. --- tests/run-multitests.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/run-multitests.py b/tests/run-multitests.py index b5ba83b0ad345..70bae59474b47 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -15,15 +15,24 @@ import subprocess import tempfile -sys.path.append("../tools") +test_dir = os.path.abspath(os.path.dirname(__file__)) + +if os.path.abspath(sys.path[0]) == test_dir: + # remove the micropython/tests dir from path to avoid + # accidentally importing tests like micropython/const.py + sys.path.pop(0) + +sys.path.insert(0, test_dir + "/../tools") import pyboard if os.name == "nt": CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3.exe") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe") + MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", test_dir + "/../ports/windows/micropython.exe") else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") - MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") + MICROPYTHON = os.getenv( + "MICROPY_MICROPYTHON", test_dir + "/../ports/unix/build-standard/micropython" + ) # For diff'ing test output DIFF = os.getenv("MICROPY_DIFF", "diff -u") @@ -508,7 +517,7 @@ def main(): cmd_args = cmd_parser.parse_args() # clear search path to make sure tests use only builtin modules and those in extmod - os.environ["MICROPYPATH"] = os.pathsep + "../extmod" + os.environ["MICROPYPATH"] = os.pathsep.join(("", ".frozen", "../extmod")) test_files = prepare_test_file_list(cmd_args.files) max_instances = max(t[1] for t in test_files) From ed41d517466f329112529a20f9808f1c1724f759 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Sep 2022 11:47:03 +1000 Subject: [PATCH 2638/3533] extmod/modbluetooth: Change data_len type from size_t to uint16_t. For consistency, and to remove the need for additional conversion of types. Signed-off-by: Damien George --- extmod/modbluetooth.c | 16 ++++++++-------- extmod/modbluetooth.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 9c168483ba453..f021308187114 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1139,7 +1139,7 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, const mp_int_t *numeric, size_t n_unsigned, size_t n_signed, const uint8_t *addr, const mp_obj_bluetooth_uuid_t *uuid, - const uint8_t **data, size_t *data_len, size_t n_data) { + const uint8_t **data, uint16_t *data_len, size_t n_data) { mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); if (o->irq_handler == mp_const_none) { return mp_const_none; @@ -1199,7 +1199,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, const mp_int_t *numeric, size_t n_unsigned, size_t n_signed, const uint8_t *addr, const mp_obj_bluetooth_uuid_t *uuid, - const uint8_t **data, size_t *data_len, size_t n_data) { + const uint8_t **data, uint16_t *data_len, size_t n_data) { // This code may run on an existing MicroPython thread, or a non-MicroPython thread // that's not using the mp_thread_get_state() value. In the former case the state @@ -1249,7 +1249,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, const mp_int_t *numeric, size_t n_unsigned, size_t n_signed, const uint8_t *addr, const mp_obj_bluetooth_uuid_t *uuid, - const uint8_t **data, size_t *data_len, size_t n_data) { + const uint8_t **data, uint16_t *data_len, size_t n_data) { return invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); } @@ -1277,7 +1277,7 @@ void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypte invoke_irq_handler(MP_BLUETOOTH_IRQ_ENCRYPTION_UPDATE, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len) { +bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, uint16_t key_len, const uint8_t **value, size_t *value_len) { mp_int_t args[] = {type, index}; mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GET_SECRET, args, 2, 0, NULL_ADDR, NULL_UUID, &key, &key_len, 1); if (result == mp_const_none) { @@ -1293,7 +1293,7 @@ bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t * bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len) { mp_int_t args[] = { type }; const uint8_t *data[] = {key, value}; - size_t data_len[] = {key_len, value_len}; + uint16_t data_len[] = {key_len, value_len}; mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_SET_SECRET, args, 1, 0, NULL_ADDR, NULL_UUID, data, data_len, 2); return mp_obj_is_true(result); } @@ -1364,7 +1364,7 @@ void mp_bluetooth_gap_on_scan_complete(void) { invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_DONE, NULL_NUMERIC, 0, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0); } -void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) { +void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, uint16_t data_len) { mp_int_t args[] = {addr_type, adv_type, rssi}; invoke_irq_handler(MP_BLUETOOTH_IRQ_SCAN_RESULT, args, 1, 2, addr, NULL_UUID, &data, &data_len, 1); } @@ -1394,7 +1394,7 @@ void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { const uint8_t *combined_data; - size_t total_len; + uint16_t total_len; if (num > 1) { // Fragmented buffer, need to combine into a new heap-allocated buffer @@ -1553,7 +1553,7 @@ void mp_bluetooth_gap_on_scan_complete(void) { schedule_ringbuf(atomic_state); } -void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) { +void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, uint16_t data_len) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); data_len = MIN(o->irq_data_data_alloc, data_len); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index d490346278203..ca6436b678d15 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -426,7 +426,7 @@ void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypte // For get, if key is NULL, then the implementation must return the index'th matching key. Otherwise it should return a specific key. // For set, if value is NULL, then delete. // The "type" is stack-specific, but could also be used to implement versioning. -bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, size_t key_len, const uint8_t **value, size_t *value_len); +bool mp_bluetooth_gap_on_get_secret(uint8_t type, uint8_t index, const uint8_t *key, uint16_t key_len, const uint8_t **value, size_t *value_len); bool mp_bluetooth_gap_on_set_secret(uint8_t type, const uint8_t *key, size_t key_len, const uint8_t *value, size_t value_len); // Call this when a passkey verification needs to be processed. @@ -451,7 +451,7 @@ void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value); void mp_bluetooth_gap_on_scan_complete(void); // Notify modbluetooth of a scan result. -void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len); +void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, uint16_t data_len); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT From db668742a5986ab1de9c0474ef268c00fb1879e7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Sep 2022 13:22:27 +1000 Subject: [PATCH 2639/3533] extmod/modbluetooth: Do GATTC reassembly in protected uPy context. The calls to m_new and m_del require an exclusive uPy (really a GC) context. In particular these functions cannot be called directly from a FreeRTOS task on esp32. Fixes issue #9369. Signed-off-by: Damien George --- extmod/modbluetooth.c | 67 +++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index f021308187114..acdbf8d85d02b 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -52,6 +52,9 @@ #error pairing and bonding require synchronous modbluetooth events #endif +// NimBLE can have fragmented data for GATTC events, so requires reassembly. +#define MICROPY_PY_BLUETOOTH_USE_GATTC_EVENT_DATA_REASSEMBLY MICROPY_BLUETOOTH_NIMBLE + #define MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS 2000 #define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN 5 @@ -1168,6 +1171,34 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, data_tuple->items[data_tuple->len++] = MP_OBJ_FROM_PTR(uuid); } #endif + + #if MICROPY_PY_BLUETOOTH_USE_GATTC_EVENT_DATA_REASSEMBLY + void *buf_to_free = NULL; + uint16_t buf_to_free_len = 0; + if (event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE || event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT) { + if (n_data > 1) { + // Fragmented buffer, need to combine into a new heap-allocated buffer + // in order to pass to Python. + // Only gattc_on_data_available calls this code, so data and data_len are writable. + uint16_t total_len = 0; + for (size_t i = 0; i < n_data; ++i) { + total_len += data_len[i]; + } + uint8_t *buf = m_new(uint8_t, total_len); + uint8_t *p = buf; + for (size_t i = 0; i < n_data; ++i) { + memcpy(p, data[i], data_len[i]); + p += data_len[i]; + } + data[0] = buf; + data_len[0] = total_len; + n_data = 1; + buf_to_free = buf; + buf_to_free_len = total_len; + } + } + #endif + for (size_t i = 0; i < n_data; ++i) { if (data[i]) { mp_obj_memoryview_init(&mv_data[i], 'B', 0, data_len[i], (void *)data[i]); @@ -1176,10 +1207,17 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, data_tuple->items[data_tuple->len++] = mp_const_none; } } + assert(data_tuple->len <= MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); mp_obj_t result = mp_call_function_2(o->irq_handler, MP_OBJ_NEW_SMALL_INT(event), MP_OBJ_FROM_PTR(data_tuple)); + #if MICROPY_PY_BLUETOOTH_USE_GATTC_EVENT_DATA_REASSEMBLY + if (buf_to_free != NULL) { + m_del(uint8_t, (uint8_t *)buf_to_free, buf_to_free_len); + } + #endif + mp_local_free(data_tuple); return result; @@ -1393,35 +1431,8 @@ void mp_bluetooth_gattc_on_discover_complete(uint8_t event, uint16_t conn_handle } void mp_bluetooth_gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const uint8_t **data, uint16_t *data_len, size_t num) { - const uint8_t *combined_data; - uint16_t total_len; - - if (num > 1) { - // Fragmented buffer, need to combine into a new heap-allocated buffer - // in order to pass to Python. - total_len = 0; - for (size_t i = 0; i < num; ++i) { - total_len += data_len[i]; - } - uint8_t *buf = m_new(uint8_t, total_len); - uint8_t *p = buf; - for (size_t i = 0; i < num; ++i) { - memcpy(p, data[i], data_len[i]); - p += data_len[i]; - } - combined_data = buf; - } else { - // Single buffer, use directly. - combined_data = *data; - total_len = *data_len; - } - mp_int_t args[] = {conn_handle, value_handle}; - invoke_irq_handler(event, args, 2, 0, NULL_ADDR, NULL_UUID, &combined_data, &total_len, 1); - - if (num > 1) { - m_del(uint8_t, (uint8_t *)combined_data, total_len); - } + invoke_irq_handler(event, args, 2, 0, NULL_ADDR, NULL_UUID, data, data_len, num); } void mp_bluetooth_gattc_on_read_write_status(uint8_t event, uint16_t conn_handle, uint16_t value_handle, uint16_t status) { From f2ad152e7e04608cbf57d756f6a4cebbf9976f6c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 23 Sep 2022 14:20:37 +1000 Subject: [PATCH 2640/3533] extmod/modbluetooth: Run BLE IRQ callback in protected NLR context. The call to invoke_irq_handler_run() always needs to run in a protected NLR context, to catch exceptions from the Python handler, and the m_new's (and also mp_local_alloc when PYSTACK is enabled). With MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS_WITH_INTERLOCK enabled there was already an explicit nlr_push, and that is now used in all cases. Without this change, on stm32 (for example), the callbacks from the BLE stack to invoke_irq_handler() were made via static scheduled nodes which do not have any NLR protection, and hence would lead to a hard fault (uncaught NLR) if an exception was raised in the Python BLE IRQ handler. This was a regression introduced by 8045ac07f599c0ccc447c88a0b778f704b497559, which is fixed by this commit. Signed-off-by: Damien George --- extmod/modbluetooth.c | 51 ++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index acdbf8d85d02b..3a51fb8b2399d 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -1143,10 +1143,6 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, const uint8_t *addr, const mp_obj_bluetooth_uuid_t *uuid, const uint8_t **data, uint16_t *data_len, size_t n_data) { - mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); - if (o->irq_handler == mp_const_none) { - return mp_const_none; - } mp_obj_array_t mv_addr; mp_obj_array_t mv_data[2]; @@ -1210,6 +1206,7 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, assert(data_tuple->len <= MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN); + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); mp_obj_t result = mp_call_function_2(o->irq_handler, MP_OBJ_NEW_SMALL_INT(event), MP_OBJ_FROM_PTR(data_tuple)); #if MICROPY_PY_BLUETOOTH_USE_GATTC_EVENT_DATA_REASSEMBLY @@ -1223,6 +1220,34 @@ STATIC mp_obj_t invoke_irq_handler_run(uint16_t event, return result; } +STATIC mp_obj_t invoke_irq_handler_run_protected(uint16_t event, + const mp_int_t *numeric, size_t n_unsigned, size_t n_signed, + const uint8_t *addr, + const mp_obj_bluetooth_uuid_t *uuid, + const uint8_t **data, uint16_t *data_len, size_t n_data) { + + mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); + if (o->irq_handler == mp_const_none) { + return mp_const_none; + } + + mp_obj_t result = mp_const_none; + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + result = invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); + nlr_pop(); + } else { + // Uncaught exception, print it out. + mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in IRQ callback handler\n"); + mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(nlr.ret_val)); + + // Disable the BLE IRQ handler. + o->irq_handler = mp_const_none; + } + + return result; +} + #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS_WITH_INTERLOCK // On some systems the BLE event callbacks may occur on a system thread which is not @@ -1256,19 +1281,9 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, MP_THREAD_GIL_ENTER(); } - mp_obj_t result = mp_const_none; - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_sched_lock(); - result = invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); - mp_sched_unlock(); - nlr_pop(); - } else { - // Uncaught exception, print it out. - mp_sched_unlock(); - mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in IRQ callback handler\n"); - mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(nlr.ret_val)); - } + mp_sched_lock(); + mp_obj_t result = invoke_irq_handler_run_protected(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); + mp_sched_unlock(); if (ts_orig == NULL) { MP_THREAD_GIL_EXIT(); @@ -1288,7 +1303,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, const uint8_t *addr, const mp_obj_bluetooth_uuid_t *uuid, const uint8_t **data, uint16_t *data_len, size_t n_data) { - return invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); + return invoke_irq_handler_run_protected(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data); } #endif From f91ebf6fa972a593ede25d33740e693d99dbbc3f Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 15 Aug 2022 12:02:23 +1000 Subject: [PATCH 2641/3533] tests: Allow 'special' tests to output "SKIP" on a single line. --- tests/run-tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/run-tests.py b/tests/run-tests.py index ca7941f6261bd..6031b35b53347 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -305,6 +305,10 @@ def send_get(what): if had_crash or output_mupy in (b"SKIP\n", b"CRASH"): return output_mupy + # skipped special tests will output "SKIP" surrounded by other interpreter debug output + if is_special and not had_crash and b"\nSKIP\n" in output_mupy: + return b"SKIP\n" + if is_special or test_file in special_tests: # convert parts of the output that are not stable across runs with open(test_file + ".exp", "rb") as f: From 25ff5b52d9786beff0b90a268af813b223f31f57 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 27 Jul 2022 12:52:48 +1000 Subject: [PATCH 2642/3533] py/parse: Allow const types other than int to optimise as true/false. Allows optimisation of cases like: import micropython _DEBUG = micropython.const(False) if _DEBUG: print('Debugging info') Previously the 'if' statement was only optimised out if the type of the const() argument was integer. The change is implemented in a way that makes the compiler slightly smaller (-16 bytes on PYBV11) but compilation will also be very slightly slower. As a bonus, if const support is enabled then the compiler can now optimise const truthy/falsey expressions of other types, like: while "something": pass ... unclear if that is useful, but perhaps it could be. Signed-off-by: Angus Gratton --- py/parse.c | 28 +++-- tests/cmdline/cmd_showbc_const.py | 69 +++++++++++ tests/cmdline/cmd_showbc_const.py.exp | 160 ++++++++++++++++++++++++++ 3 files changed, 247 insertions(+), 10 deletions(-) create mode 100644 tests/cmdline/cmd_showbc_const.py create mode 100644 tests/cmdline/cmd_showbc_const.py.exp diff --git a/py/parse.c b/py/parse.c index d58098af2e165..62de3282b3443 100644 --- a/py/parse.c +++ b/py/parse.c @@ -334,16 +334,6 @@ STATIC uint8_t peek_rule(parser_t *parser, size_t n) { } #endif -bool mp_parse_node_is_const_false(mp_parse_node_t pn) { - return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE) - || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0); -} - -bool mp_parse_node_is_const_true(mp_parse_node_t pn) { - return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) - || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0); -} - bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) { if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { *o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn)); @@ -427,6 +417,24 @@ STATIC mp_obj_t mp_parse_node_convert_to_obj(mp_parse_node_t pn) { } #endif +STATIC bool parse_node_is_const_bool(mp_parse_node_t pn, bool value) { + // Returns true if 'pn' is a constant whose boolean value is equivalent to 'value' + #if MICROPY_COMP_CONST_TUPLE || MICROPY_COMP_CONST + return mp_parse_node_is_const(pn) && mp_obj_is_true(mp_parse_node_convert_to_obj(pn)) == value; + #else + return MP_PARSE_NODE_IS_TOKEN_KIND(pn, value ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE) + || (MP_PARSE_NODE_IS_SMALL_INT(pn) && !!MP_PARSE_NODE_LEAF_SMALL_INT(pn) == value); + #endif +} + +bool mp_parse_node_is_const_false(mp_parse_node_t pn) { + return parse_node_is_const_bool(pn, false); +} + +bool mp_parse_node_is_const_true(mp_parse_node_t pn) { + return parse_node_is_const_bool(pn, true); +} + size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes) { if (MP_PARSE_NODE_IS_NULL(*pn)) { *nodes = NULL; diff --git a/tests/cmdline/cmd_showbc_const.py b/tests/cmdline/cmd_showbc_const.py new file mode 100644 index 0000000000000..54f9ec23cdbee --- /dev/null +++ b/tests/cmdline/cmd_showbc_const.py @@ -0,0 +1,69 @@ +# cmdline: -v -v +# Test constant-related bytecode optimisations +# (constant folding, compile-time if/while evaluation, etc.) +from micropython import const +import sys + +try: + sys.settrace + # if MICROPY_PY_SYS_SETTRACE is enabled, compile-time const optimizations + # are disabled so the bytecode output is very different + print("SKIP") + raise SystemExit +except AttributeError: + pass + +_STR = const("foo") +_EMPTY_TUPLE = const(()) +_TRUE = const(True) +_FALSE = const(False) +_SMALLINT = const(33) +_ZERO = const(0) + +# Bytecode generated for these if/while statements should contain no JUMP_IF +# and no instances of string 'Eliminated' + +if _STR or _EMPTY_TUPLE: + print("Kept") +if _STR and _EMPTY_TUPLE: + print("Eliminated") +if _TRUE: + print("Kept") +if _SMALLINT: + print("Kept") +if _ZERO and _SMALLINT: + print("Eliminated") +if _FALSE: + print("Eliminated") + +while _SMALLINT: + print("Kept") + break +while _ZERO: + print("Eliminated") +while _FALSE: + print("Eliminated") + +# These values are stored in variables, and therefore bytecode will contain JUMP_IF + +a = _EMPTY_TUPLE or _STR +if a == _STR: + print("Kept") + +b = _SMALLINT and _STR +if b == _STR: + print("Kept") + +# The compiler is also unable to optimise these expressions, even though the arguments are const, +# so these also contain JUMP_IF + +if (_EMPTY_TUPLE or _STR) == _STR: + print("Kept") + +if (_EMPTY_TUPLE and _STR) == _STR: + print("Not Eliminated") + +if (not _STR) == _FALSE: + print("Kept") + +assert True diff --git a/tests/cmdline/cmd_showbc_const.py.exp b/tests/cmdline/cmd_showbc_const.py.exp new file mode 100644 index 0000000000000..6cdc3e9c96373 --- /dev/null +++ b/tests/cmdline/cmd_showbc_const.py.exp @@ -0,0 +1,160 @@ +File cmdline/cmd_showbc_const.py, code block '' (descriptor: \.\+, bytecode @\.\+ 198 bytes) +Raw bytecode (code_info_size=40, bytecode_size=158): + 2c 4c 01 60 2c 46 22 65 27 4a 83 0c 20 27 40 20 + 27 20 27 40 60 20 27 24 40 60 40 24 27 47 24 27 + 67 40 27 47 27 47 26 47 80 10 02 2a 01 1b 03 1c + 02 16 02 59 80 51 1b 04 16 04 48 0f 11 04 13 05 + 59 11 09 10 06 34 01 59 11 0a 65 57 11 0b df 44 + 43 59 4a 01 5d 11 09 10 07 34 01 59 11 09 10 07 + 34 01 59 11 09 10 07 34 01 59 11 09 10 07 34 01 + 59 42 42 42 35 23 00 16 0c 11 0c 23 00 d9 44 47 + 11 09 10 07 34 01 59 23 00 16 0d 11 0d 23 00 d9 + 44 47 11 09 10 07 34 01 59 23 00 23 00 d9 44 47 + 11 09 10 07 34 01 59 23 01 23 00 d9 44 47 11 09 + 23 02 34 01 59 50 23 03 d9 44 47 11 09 10 07 34 + 01 59 42 40 51 63 +arg names: +(N_STATE 6) +(N_EXC_STACK 1) + bc=0 line=1 + bc=0 line=4 + bc=12 line=5 + bc=18 line=7 + bc=20 line=8 + bc=25 line=11 + bc=32 line=12 + bc=42 line=14 + bc=45 line=26 + bc=45 line=27 + bc=52 line=28 + bc=52 line=30 + bc=52 line=31 + bc=59 line=32 + bc=59 line=33 + bc=66 line=34 + bc=66 line=36 + bc=66 line=39 + bc=66 line=40 + bc=73 line=41 + bc=77 line=42 + bc=77 line=44 + bc=77 line=47 + bc=77 line=49 + bc=81 line=50 + bc=88 line=51 + bc=95 line=53 + bc=99 line=54 + bc=106 line=55 + bc=113 line=58 + bc=113 line=60 + bc=120 line=61 + bc=127 line=63 + bc=134 line=64 + bc=141 line=66 + bc=147 line=67 + bc=154 line=69 +00 LOAD_CONST_SMALL_INT 0 +01 LOAD_CONST_STRING 'const' +03 BUILD_TUPLE 1 +05 IMPORT_NAME 'micropython' +07 IMPORT_FROM 'const' +09 STORE_NAME const +11 POP_TOP +12 LOAD_CONST_SMALL_INT 0 +13 LOAD_CONST_NONE +14 IMPORT_NAME 'sys' +16 STORE_NAME sys +18 SETUP_EXCEPT 35 +20 LOAD_NAME sys +22 LOAD_ATTR settrace +24 POP_TOP +25 LOAD_NAME print +27 LOAD_CONST_STRING 'SKIP' +29 CALL_FUNCTION n=1 nkw=0 +31 POP_TOP +32 LOAD_NAME SystemExit +34 RAISE_OBJ +35 DUP_TOP +36 LOAD_NAME AttributeError +38 BINARY_OP 8 +39 POP_JUMP_IF_FALSE 44 +41 POP_TOP +42 POP_EXCEPT_JUMP 45 +44 END_FINALLY +45 LOAD_NAME print +47 LOAD_CONST_STRING 'Kept' +49 CALL_FUNCTION n=1 nkw=0 +51 POP_TOP +52 LOAD_NAME print +54 LOAD_CONST_STRING 'Kept' +56 CALL_FUNCTION n=1 nkw=0 +58 POP_TOP +59 LOAD_NAME print +61 LOAD_CONST_STRING 'Kept' +63 CALL_FUNCTION n=1 nkw=0 +65 POP_TOP +66 LOAD_NAME print +68 LOAD_CONST_STRING 'Kept' +70 CALL_FUNCTION n=1 nkw=0 +72 POP_TOP +73 JUMP 77 +75 JUMP 66 +77 LOAD_CONST_OBJ \.\+='foo' +79 STORE_NAME a +81 LOAD_NAME a +83 LOAD_CONST_OBJ \.\+='foo' +85 BINARY_OP 2 __eq__ +86 POP_JUMP_IF_FALSE 95 +88 LOAD_NAME print +90 LOAD_CONST_STRING 'Kept' +92 CALL_FUNCTION n=1 nkw=0 +94 POP_TOP +95 LOAD_CONST_OBJ \.\+='foo' +97 STORE_NAME b +99 LOAD_NAME b +101 LOAD_CONST_OBJ \.\+='foo' +103 BINARY_OP 2 __eq__ +104 POP_JUMP_IF_FALSE 113 +106 LOAD_NAME print +108 LOAD_CONST_STRING 'Kept' +110 CALL_FUNCTION n=1 nkw=0 +112 POP_TOP +113 LOAD_CONST_OBJ \.\+='foo' +115 LOAD_CONST_OBJ \.\+='foo' +117 BINARY_OP 2 __eq__ +118 POP_JUMP_IF_FALSE 127 +120 LOAD_NAME print +122 LOAD_CONST_STRING 'Kept' +124 CALL_FUNCTION n=1 nkw=0 +126 POP_TOP +127 LOAD_CONST_OBJ \.\+=() +129 LOAD_CONST_OBJ \.\+='foo' +131 BINARY_OP 2 __eq__ +132 POP_JUMP_IF_FALSE 141 +134 LOAD_NAME print +136 LOAD_CONST_OBJ \.\+='Not Eliminated' +138 CALL_FUNCTION n=1 nkw=0 +140 POP_TOP +141 LOAD_CONST_FALSE +142 LOAD_CONST_OBJ \.\+=False +144 BINARY_OP 2 __eq__ +145 POP_JUMP_IF_FALSE 154 +147 LOAD_NAME print +149 LOAD_CONST_STRING 'Kept' +151 CALL_FUNCTION n=1 nkw=0 +153 POP_TOP +154 JUMP 156 +156 LOAD_CONST_NONE +157 RETURN_VALUE +Kept +Kept +Kept +Kept +Kept +Kept +Kept +Kept +mem: total=\\d\+, current=\\d\+, peak=\\d\+ +stack: \\d\+ out of \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+ + No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ From 30e50ab1951680a046c224c12f7cc29aed410e2a Mon Sep 17 00:00:00 2001 From: Jatty_ Date: Tue, 13 Sep 2022 18:38:03 +0700 Subject: [PATCH 2643/3533] stm32/make-stmconst.py: Support TypeDef's with a single char prefix. Update the regex to support parsing files from the STM32CubeU5 library. --- ports/stm32/make-stmconst.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/make-stmconst.py b/ports/stm32/make-stmconst.py index 554d662384454..aab4038e61dfe 100644 --- a/ports/stm32/make-stmconst.py +++ b/ports/stm32/make-stmconst.py @@ -72,7 +72,7 @@ class Lexer: ("}", re.compile(r"}$")), ( "} TypeDef", - re.compile(r"} *(?P[A-Z][A-Za-z0-9_]+)_(?P([A-Za-z0-9_]+)?)TypeDef;$"), + re.compile(r"} *(?P[A-Z][A-Za-z0-9_]*)_(?P([A-Za-z0-9_]+)?)TypeDef;$"), ), ( "IO reg", From ae0b0e701899297d057ab8dc578b3bdbf0144fc4 Mon Sep 17 00:00:00 2001 From: yn386 Date: Mon, 19 Sep 2022 17:55:51 +0900 Subject: [PATCH 2644/3533] lib/stm32lib: Update library to get L1 v1.10.3, and some other fixes. Changes in this new library version are: - Add L1 HAL at v1.10.3. - H7_HAL/rcc_ex: Add SPI45 to HAL_RCCEx_GetPeriphCLKFreq. - L4_HAL/gpio_ex: Add #define for GPIO_AF14_TIM2 on L4P5/L4Q5. - F4_HAL/i2c: Fix I2C frequency calculation macros. - L1_HAL/utils: Fix compile error when USE_HAL_DRIVER is defined. --- .gitmodules | 2 +- lib/stm32lib | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 3c47b5959aad8..992fec3d1836f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,7 +13,7 @@ [submodule "lib/stm32lib"] path = lib/stm32lib url = https://github.com/micropython/stm32lib - branch = work-F0-1.9.0+F4-1.16.0+F7-1.7.0+G4-1.3.0+H7-1.6.0+L0-1.11.2+L4-1.17.0+WB-1.10.0+WL-1.1.0 + branch = work-F0-1.9.0+F4-1.16.0+F7-1.7.0+G0-1.5.1+G4-1.3.0+H7-1.6.0+L0-1.11.2+L1-1.10.3+L4-1.17.0+WB-1.10.0+WL-1.1.0 [submodule "lib/nrfx"] path = lib/nrfx url = https://github.com/NordicSemiconductor/nrfx.git diff --git a/lib/stm32lib b/lib/stm32lib index eb80f0126e506..a9f8fee7bb0cb 160000 --- a/lib/stm32lib +++ b/lib/stm32lib @@ -1 +1 @@ -Subproject commit eb80f0126e50687aac966f4c39a2b5a5deffbe78 +Subproject commit a9f8fee7bb0cb4ac1b4ff719b6b5b285f613f352 From 427d72667f23ef8758fbfd2352dd28ba5565644a Mon Sep 17 00:00:00 2001 From: yn386 Date: Mon, 19 Sep 2022 17:56:31 +0900 Subject: [PATCH 2645/3533] stm32: Add support for STM32L1 MCUs. This change adds STM32L1 support to the STM32 port. --- ports/stm32/Makefile | 7 ++ ports/stm32/adc.c | 55 ++++++++- ports/stm32/boards/stm32l152_af.csv | 117 +++++++++++++++++++ ports/stm32/boards/stm32l152xe.ld | 37 ++++++ ports/stm32/boards/stm32l1xx_hal_conf_base.h | 99 ++++++++++++++++ ports/stm32/dac.c | 2 + ports/stm32/dma.c | 66 ++++++++++- ports/stm32/dma.h | 14 +++ ports/stm32/extint.c | 4 + ports/stm32/extint.h | 2 +- ports/stm32/flash.c | 10 +- ports/stm32/machine_adc.c | 7 +- ports/stm32/modmachine.c | 4 +- ports/stm32/mpconfigboard_common.h | 10 ++ ports/stm32/mphalport.c | 2 +- ports/stm32/mphalport.h | 2 + ports/stm32/powerctrl.c | 6 +- ports/stm32/powerctrlboot.c | 68 +++++++++++ ports/stm32/resethandler_m3.s | 75 ++++++++++++ ports/stm32/rtc.c | 17 +++ ports/stm32/stm32.mk | 3 +- ports/stm32/stm32_it.c | 34 ++++++ ports/stm32/timer.c | 34 ++++-- ports/stm32/uart.c | 17 ++- ports/stm32/uart.h | 2 +- 25 files changed, 657 insertions(+), 37 deletions(-) create mode 100644 ports/stm32/boards/stm32l152_af.csv create mode 100644 ports/stm32/boards/stm32l152xe.ld create mode 100644 ports/stm32/boards/stm32l1xx_hal_conf_base.h create mode 100644 ports/stm32/resethandler_m3.s diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index ee00d77173640..4b4a9f0ce3801 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -347,11 +347,18 @@ SRC_O += \ resethandler_m0.o \ shared/runtime/gchelper_m0.o else +ifeq ($(MCU_SERIES),l1) +CFLAGS += -DUSE_HAL_DRIVER +SRC_O += \ + resethandler_m3.o \ + shared/runtime/gchelper_m3.o +else SRC_O += \ system_stm32.o \ resethandler.o \ shared/runtime/gchelper_m3.o endif +endif HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\ hal.c \ diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 712e9b3adeb2f..02bbbb58db9e5 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -118,6 +118,14 @@ #define ADC_CAL2 ((uint16_t *)(0x1FF1E840)) #define ADC_CAL_BITS (16) +#elif defined(STM32L1) + +#define ADC_SCALE_V (VREFINT_CAL_VREF / 1000.0f) +#define ADC_CAL_ADDRESS (VREFINT_CAL_ADDR) +#define ADC_CAL1 (TEMPSENSOR_CAL1_ADDR) +#define ADC_CAL2 (TEMPSENSOR_CAL2_ADDR) +#define ADC_CAL_BITS (12) + #elif defined(STM32L4) || defined(STM32WB) #define ADC_SCALE_V (VREFINT_CAL_VREF / 1000.0f) @@ -163,6 +171,8 @@ defined(STM32L476xx) || defined(STM32L496xx) || \ defined(STM32WB55xx) #define VBAT_DIV (3) +#elif defined(STM32L152xE) +// STM32L152xE does not have vbat. #else #error Unsupported processor #endif @@ -179,11 +189,17 @@ #define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS) #ifndef __HAL_ADC_IS_CHANNEL_INTERNAL +#if defined(STM32L1) +#define __HAL_ADC_IS_CHANNEL_INTERNAL(channel) \ + (channel == ADC_CHANNEL_VREFINT \ + || channel == ADC_CHANNEL_TEMPSENSOR) +#else #define __HAL_ADC_IS_CHANNEL_INTERNAL(channel) \ (channel == ADC_CHANNEL_VBAT \ || channel == ADC_CHANNEL_VREFINT \ || channel == ADC_CHANNEL_TEMPSENSOR) #endif +#endif typedef struct _pyb_obj_adc_t { mp_obj_base_t base; @@ -210,6 +226,10 @@ STATIC bool is_adcx_channel(int channel) { return IS_ADC_CHANNEL(channel) || channel == ADC_CHANNEL_TEMPSENSOR; #elif defined(STM32F0) || defined(STM32F4) || defined(STM32F7) return IS_ADC_CHANNEL(channel); + #elif defined(STM32L1) + // The HAL of STM32L1 defines some channels those may not be available on package + return __HAL_ADC_IS_CHANNEL_INTERNAL(channel) + || (channel < MP_ARRAY_SIZE(pin_adcall_table) && pin_adcall_table[channel]); #elif defined(STM32G0) || defined(STM32H7) return __HAL_ADC_IS_CHANNEL_INTERNAL(channel) || IS_ADC_CHANNEL(__HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel)); @@ -225,7 +245,7 @@ STATIC bool is_adcx_channel(int channel) { STATIC void adc_wait_for_eoc_or_timeout(ADC_HandleTypeDef *adcHandle, int32_t timeout) { uint32_t tickstart = HAL_GetTick(); - #if defined(STM32F4) || defined(STM32F7) + #if defined(STM32F4) || defined(STM32F7) || defined(STM32L1) while ((adcHandle->Instance->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) { #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) while (READ_BIT(adcHandle->Instance->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) { @@ -239,7 +259,7 @@ STATIC void adc_wait_for_eoc_or_timeout(ADC_HandleTypeDef *adcHandle, int32_t ti } STATIC void adcx_clock_enable(ADC_HandleTypeDef *adch) { - #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) + #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) || defined(STM32L1) ADCx_CLK_ENABLE(); #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ) __HAL_RCC_ADC12_CLK_ENABLE(); @@ -299,6 +319,12 @@ STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) { adch->Init.OversamplingMode = DISABLE; adch->Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; + #elif defined(STM32L1) + adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + adch->Init.ScanConvMode = ADC_SCAN_DISABLE; + adch->Init.LowPowerAutoWait = DISABLE; + adch->Init.DataAlign = ADC_DATAALIGN_RIGHT; + adch->Init.DMAContinuousRequests = DISABLE; #elif defined(STM32G0) || defined(STM32G4) || defined(STM32L4) || defined(STM32WB) adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; adch->Init.ScanConvMode = ADC_SCAN_DISABLE; @@ -367,6 +393,12 @@ STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.OffsetRightShift = DISABLE; sConfig.OffsetSignedSaturation = DISABLE; + #elif defined(STM32L1) + if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) { + sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES; + } else { + sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES; + } #elif defined(STM32G0) if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) { sConfig.SamplingTime = ADC_SAMPLETIME_160CYCLES_5; @@ -555,7 +587,7 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_ HAL_ADC_Start(&self->handle); } else { // for subsequent samples we can just set the "start sample" bit - #if defined(STM32F4) || defined(STM32F7) + #if defined(STM32F4) || defined(STM32F7) || defined(STM32L1) self->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART; #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) SET_BIT(self->handle.Instance->CR, ADC_CR_ADSTART); @@ -665,7 +697,7 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i adc_config_channel(&adc->handle, adc->channel); // for the first sample we need to turn the ADC on // ADC is started: set the "start sample" bit - #if defined(STM32F4) || defined(STM32F7) + #if defined(STM32F4) || defined(STM32F7) || defined(STM32L1) adc->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART; #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) SET_BIT(adc->handle.Instance->CR, ADC_CR_ADSTART); @@ -720,6 +752,8 @@ typedef struct _pyb_adc_all_obj_t { ADC_HandleTypeDef handle; } pyb_adc_all_obj_t; +float adc_read_core_vref(ADC_HandleTypeDef *adcHandle); + void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution, uint32_t en_mask) { switch (resolution) { @@ -762,7 +796,11 @@ void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution, uint32_t en_m } int adc_get_resolution(ADC_HandleTypeDef *adcHandle) { + #if defined(STM32L1) + uint32_t res_reg = adcHandle->Instance->CR1 & ADC_CR1_RES_Msk; + #else uint32_t res_reg = ADC_GET_RESOLUTION(adcHandle); + #endif switch (res_reg) { #if !defined(STM32H7) @@ -814,6 +852,11 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) { return 0; } #else + #if defined(STM32L1) + // Update the reference correction factor before reading tempsensor + // because TS_CAL1 and TS_CAL2 of STM32L1 are at VDDA=3.0V + adc_read_core_vref(adcHandle); + #endif int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR); #endif float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0f; @@ -821,8 +864,12 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) { } float adc_read_core_vbat(ADC_HandleTypeDef *adcHandle) { + #if defined(STM32L152xE) + mp_raise_NotImplementedError(MP_ERROR_TEXT("read_core_vbat not supported")); + #else uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VBAT); return raw_value * VBAT_DIV * ADC_SCALE * adc_refcor; + #endif } float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) { diff --git a/ports/stm32/boards/stm32l152_af.csv b/ports/stm32/boards/stm32l152_af.csv new file mode 100644 index 0000000000000..a6f8e80c3028a --- /dev/null +++ b/ports/stm32/boards/stm32l152_af.csv @@ -0,0 +1,117 @@ +Port,,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF14,AF15, +,,SYS_AF,TIM2,TIM3/TIM4/TIM5,TIM9/TIM10/TIM11,I2C1/I2C2,SPI1/SPI2,SPI3,USART1/USART2/USART3,UART4/UART5,,,,,,,,ADC +PortA,PA0,,TIM2_CH1_ETR,TIM5_CH1,,,,,USART2_CTS,,,,,,,,EVENTOUT,ADC1_IN0 +PortA,PA1,,TIM2_CH2,TIM5_CH2,,,,,USART2_RTS,,,,,,,,EVENTOUT,ADC1_IN1 +PortA,PA2,,TIM2_CH3,TIM5_CH3,TIM9_CH1,,,,USART2_TX,,,,,,,,EVENTOUT,ADC1_IN2 +PortA,PA3,,TIM2_CH4,TIM5_CH4,TIM9_CH2,,,,USART2_RX,,,,,,,,EVENTOUT,ADC1_IN3 +PortA,PA4,,,,,,SPI1_NSS,SPI3_NSS/I2S3_WS,USART2_CK,,,,,,,,EVENTOUT,ADC1_IN4 +PortA,PA5,,TIM2_CH1_ETR,,,,SPI1_SCK,,,,,,,,,,EVENTOUT,ADC1_IN5 +PortA,PA6,,,TIM3_CH1,TIM10_CH1,,SPI1_MISO,,,,,,,,,,EVENTOUT,ADC1_IN6 +PortA,PA7,,,TIM3_CH2,TIM11_CH1,,SPI1_MOSI,,,,,,,,,,EVENTOUT,ADC1_IN7 +PortA,PA8,MCO,,,,,,,USART1_CK,,,,,,,,EVENTOUT, +PortA,PA9,,,,,,,,USART1_TX,,,,,,,,EVENTOUT, +PortA,PA10,,,,,,,,USART1_RX,,,,,,,,EVENTOUT, +PortA,PA11,,,,,,SPI1_MISO,,USART1_CTS,,,,,,,,EVENTOUT, +PortA,PA12,,,,,,SPI1_MOSI,,USART1_RTS,,,,,,,,EVENTOUT, +PortA,PA13,JTMS/SWDIO,,,,,,,,,,,,,,,EVENTOUT, +PortA,PA14,JTCK/SWCLK,,,,,,,,,,,,,,,EVENTOUT, +PortA,PA15,JTDI,TIM2_CH1_ETR,,,,SPI1_NSS,SPI3_NSS/I2S3_WS,,,,,,,,,EVENTOUT, +PortB,PB0,,,TIM3_CH3,,,,,,,,,,,,,EVENTOUT,ADC1_IN8 +PortB,PB1,,,TIM3_CH4,,,,,,,,,,,,,EVENTOUT,ADC1_IN9 +PortB,PB2,BOOT1,,,,,,,,,,,,,,,EVENTOUT, +PortB,PB3,JTDO,TIM2_CH2,,,,SPI1_SCK,SPI3_SCK/I2S3_CK,,,,,,,,,EVENTOUT, +PortB,PB4,NJTRST,,TIM3_CH1,,,SPI1_MISO,SPI3_MISO,,,,,,,,,EVENTOUT, +PortB,PB5,,,TIM3_CH2,,I2C1_SMBA,SPI1_MOSI,SPI3_MOSI/I2S3_SD,,,,,,,,,EVENTOUT, +PortB,PB6,,,TIM4_CH1,,I2C1_SCL,,,USART1_TX,,,,,,,,EVENTOUT, +PortB,PB7,,,TIM4_CH2,,I2C1_SDA,,,USART1_RX,,,,,,,,EVENTOUT, +PortB,PB8,,,TIM4_CH3,TIM10_CH1,I2C1_SCL,,,,,,,,,,,EVENTOUT, +PortB,PB9,,,TIM4_CH4,TIM11_CH1,I2C1_SDA,,,,,,,,,,,EVENTOUT, +PortB,PB10,,TIM2_CH3,,,I2C2_SCL,,,USART3_TX,,,,,,,,EVENTOUT, +PortB,PB11,,TIM2_CH4,,,I2C2_SDA,,,USART3_RX,,,,,,,,EVENTOUT, +PortB,PB12,,,,TIM10_CH1,I2C2_SMBA,SPI2_NSS/I2S2_WS,,USART3_CK,,,,,,,,EVENTOUT,ADC1_IN18 +PortB,PB13,,,,TIM9_CH1,,SPI2_SCK/I2S2_CK,,USART3_CTS,,,,,,,,EVENTOUT,ADC1_IN19 +PortB,PB14,,,,TIM9_CH2,,SPI2_MISO,,USART3_RTS,,,,,,,,EVENTOUT,ADC1_IN20 +PortB,PB15,,,,TIM11_CH1,,SPI2_MOSI/I2S2_SD,,,,,,,,,,EVENTOUT,ADC1_IN21 +PortC,PC0,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN10 +PortC,PC1,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN11 +PortC,PC2,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN12 +PortC,PC3,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN13 +PortC,PC4,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN14 +PortC,PC5,,,,,,,,,,,,,,,,EVENTOUT,ADC1_IN15 +PortC,PC6,,,TIM3_CH1,,,I2S2_MCK,,,,,,,,,,EVENTOUT, +PortC,PC7,,,TIM3_CH2,,,,I2S3_MCK,,,,,,,,,EVENTOUT, +PortC,PC8,,,TIM3_CH3,,,,,,,,,,,,,EVENTOUT, +PortC,PC9,,,TIM3_CH4,,,,,,,,,,,,,EVENTOUT, +PortC,PC10,,,,,,,SPI3_SCK/I2S3_CK,USART3_TX,UART4_TX,,,,,,,EVENTOUT, +PortC,PC11,,,,,,,SPI3_MISO,USART3_RX,UART4_RX,,,,,,,EVENTOUT, +PortC,PC12,,,,,,,SPI3_MOSI/I2S3_SD,USART3_CK,UART5_TX,,,,,,,EVENTOUT, +PortC,PC13,,,,,,,,,,,,,,,,EVENTOUT, +PortC,PC14,,,,,,,,,,,,,,,,EVENTOUT, +PortC,PC15,,,,,,,,,,,,,,,,EVENTOUT, +PortD,PD0,,,,TIM9_CH1,,SPI2_NSS/I2S2_WS,,,,,,,,,,EVENTOUT, +PortD,PD1,,,,,,SPI2_SCK/I2S2_CK,,,,,,,,,,EVENTOUT, +PortD,PD2,,,TIM3_ETR,,,,,,UART5_RX,,,,,,,EVENTOUT, +PortD,PD3,,,,,,SPI2_MISO,,USART2_CTS,,,,,,,,EVENTOUT, +PortD,PD4,,,,,,SPI2_MOSI/I2S2_SD,,USART2_RTS,,,,,,,,EVENTOUT, +PortD,PD5,,,,,,,,USART2_TX,,,,,,,,EVENTOUT, +PortD,PD6,,,,,,,,USART2_RX,,,,,,,,EVENTOUT, +PortD,PD7,,,,TIM9_CH2,,,,USART2_CK,,,,,,,,EVENTOUT, +PortD,PD8,,,,,,,,USART3_TX,,,,,,,,EVENTOUT, +PortD,PD9,,,,,,,,USART3_RX,,,,,,,,EVENTOUT, +PortD,PD10,,,,,,,,USART3_CK,,,,,,,,EVENTOUT, +PortD,PD11,,,,,,,,USART3_CTS,,,,,,,,EVENTOUT, +PortD,PD12,,,TIM4_CH1,,,,,USART3_RTS,,,,,,,,EVENTOUT, +PortD,PD13,,,TIM4_CH2,,,,,,,,,,,,,EVENTOUT, +PortD,PD14,,,TIM4_CH3,,,,,,,,,,,,,EVENTOUT, +PortD,PD15,,,TIM4_CH4,,,,,,,,,,,,,EVENTOUT, +PortE,PE0,,,TIM4_ETR,TIM10_CH1,,,,,,,,,,,,EVENTOUT, +PortE,PE1,,,,TIM11_CH1,,,,,,,,,,,,EVENTOUT, +PortE,PE2,TRACECK,,TIM3_ETR,,,,,,,,,,,,,EVENTOUT, +PortE,PE3,TRACED0,,TIM3_CH1,,,,,,,,,,,,,EVENTOUT, +PortE,PE4,TRACED1,,TIM3_CH2,,,,,,,,,,,,,EVENTOUT, +PortE,PE5,TRACED2,,,TIM9_CH1,,,,,,,,,,,,EVENTOUT, +PortE,PE6,TRACED3,,,TIM9_CH2,,,,,,,,,,,,EVENTOUT, +PortE,PE7,,,,,,,,,,,,,,,,EVENTOUT, +PortE,PE8,,,,,,,,,,,,,,,,EVENTOUT, +PortE,PE9,,TIM2_CH1_ETR,,,,,,,,,,,,,,EVENTOUT, +PortE,PE10,,TIM2_CH2,,,,,,,,,,,,,,EVENTOUT, +PortE,PE11,,TIM2_CH3,,,,,,,,,,,,,,EVENTOUT, +PortE,PE12,,TIM2_CH4,,,,SPI1_NSS,,,,,,,,,,EVENTOUT, +PortE,PE13,,,,,,SPI1_SCK,,,,,,,,,,EVENTOUT, +PortE,PE14,,,,,,SPI1_MISO,,,,,,,,,,EVENTOUT, +PortE,PE15,,,,,,SPI1_MOSI,,,,,,,,,,EVENTOUT, +PortF,PF0,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF1,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF2,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF3,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF4,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF5,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF6,,,TIM5_ETR,,,,,,,,,,,,,EVENTOUT, +PortF,PF7,,,TIM5_CH2,,,,,,,,,,,,,EVENTOUT, +PortF,PF8,,,TIM5_CH3,,,,,,,,,,,,,EVENTOUT, +PortF,PF9,,,TIM5_CH4,,,,,,,,,,,,,EVENTOUT, +PortF,PF10,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF11,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF12,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF13,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF14,,,,,,,,,,,,,,,,EVENTOUT, +PortF,PF15,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG0,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG1,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG2,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG3,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG4,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG5,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG6,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG7,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG8,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG9,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG10,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG11,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG12,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG13,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG14,,,,,,,,,,,,,,,,EVENTOUT, +PortG,PG15,,,,,,,,,,,,,,,,EVENTOUT, +PortH,PH0,,,,,,,,,,,,,,,,, +PortH,PH1,,,,,,,,,,,,,,,,, +PortH,PH2,,,,,,,,,,,,,,,,, diff --git a/ports/stm32/boards/stm32l152xe.ld b/ports/stm32/boards/stm32l152xe.ld new file mode 100644 index 0000000000000..32f8444947e2a --- /dev/null +++ b/ports/stm32/boards/stm32l152xe.ld @@ -0,0 +1,37 @@ +/* + GNU linker script for STM32L152xE +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* entire flash */ + FLASH_FS (rx) : ORIGIN = 0x08064000, LENGTH = 112K /* sectors 100-127 */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 81408 + FS_CACHE (xrw) : ORIGIN = 0x20013e00, LENGTH = 512 +} + +/* produce a link error if there is not this amount of RAM for these sections */ +_minimum_stack_size = 2K; +_minimum_heap_size = 16K; + +/* RAM extents for the garbage collector */ +_ram_start = ORIGIN(RAM); +_ram_end = ORIGIN(RAM) + LENGTH(RAM); + +/* Define the stack. The stack is full descending so begins just above last byte + of RAM. Note that EABI requires the stack to be 8-byte aligned for a call. */ +_estack = ORIGIN(RAM) + LENGTH(RAM) - _estack_reserve; +_sstack = _estack - 16K; /* tunable */ + +/* RAM extents for the garbage collector */ +_ram_start = ORIGIN(RAM); +_ram_end = ORIGIN(RAM) + LENGTH(RAM); +_heap_start = _ebss; /* heap starts just after statically allocated memory */ +_heap_end = _sstack; + +/* Filesystem cache in RAM, and storage in flash */ +_micropy_hw_internal_flash_storage_ram_cache_start = ORIGIN(FS_CACHE); +_micropy_hw_internal_flash_storage_ram_cache_end = ORIGIN(FS_CACHE) + LENGTH(FS_CACHE); +_micropy_hw_internal_flash_storage_start = ORIGIN(FLASH_FS); +_micropy_hw_internal_flash_storage_end = ORIGIN(FLASH_FS) + LENGTH(FLASH_FS); diff --git a/ports/stm32/boards/stm32l1xx_hal_conf_base.h b/ports/stm32/boards/stm32l1xx_hal_conf_base.h new file mode 100644 index 0000000000000..b839fd29f0a3d --- /dev/null +++ b/ports/stm32/boards/stm32l1xx_hal_conf_base.h @@ -0,0 +1,99 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_STM32L1XX_HAL_CONF_BASE_H +#define MICROPY_INCLUDED_STM32L1XX_HAL_CONF_BASE_H + +// Include various HAL modules for convenience +#include "stm32l1xx_hal_rcc.h" +#include "stm32l1xx_hal_gpio.h" +#include "stm32l1xx_hal_dma.h" +#include "stm32l1xx_hal_cortex.h" +#include "stm32l1xx_hal_adc.h" +#include "stm32l1xx_hal_comp.h" +#include "stm32l1xx_hal_crc.h" +#include "stm32l1xx_hal_dac.h" +#include "stm32l1xx_hal_flash.h" +#include "stm32l1xx_hal_i2c.h" +#include "stm32l1xx_hal_iwdg.h" +#include "stm32l1xx_hal_pwr.h" +#include "stm32l1xx_hal_rtc.h" +#include "stm32l1xx_hal_spi.h" +#include "stm32l1xx_hal_tim.h" +#include "stm32l1xx_hal_uart.h" +#include "stm32l1xx_hal_usart.h" +#include "stm32l1xx_hal_wwdg.h" +#include "stm32l1xx_hal_exti.h" +#include "stm32l1xx_ll_adc.h" +#include "stm32l1xx_ll_pwr.h" +#include "stm32l1xx_ll_rtc.h" +#include "stm32l1xx_ll_usart.h" + +// Enable various HAL modules +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_CRC_MODULE_ENABLED +#define HAL_DAC_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +#define HAL_USART_MODULE_ENABLED +#define HAL_WWDG_MODULE_ENABLED + +// Oscillator values in Hz +#define HSE_VALUE (8000000) +#define HSI_VALUE (16000000) +#define HSI48_VALUE (48000000) +#define LSI_VALUE (37000) +#define LSE_VALUE (32768) +#define MSI_VALUE (2097000) + +// Oscillator timeouts in ms +#define HSE_STARTUP_TIMEOUT (100) +#define LSE_STARTUP_TIMEOUT (5000) + +// SysTick has the highest priority +#define TICK_INT_PRIORITY (0x00) + +// Miscellaneous HAL settings +#define DATA_CACHE_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define PREFETCH_ENABLE 1 +#define USE_SPI_CRC 0 +#define USE_RTOS 0 + +// HAL parameter assertions are disabled +#define assert_param(expr) ((void)0) + +#endif // MICROPY_INCLUDED_STM32L1XX_HAL_CONF_BASE_H diff --git a/ports/stm32/dac.c b/ports/stm32/dac.c index 36ef9387f8737..feadbe5c58170 100644 --- a/ports/stm32/dac.c +++ b/ports/stm32/dac.c @@ -261,6 +261,8 @@ STATIC mp_obj_t pyb_dac_init_helper(pyb_dac_obj_t *self, size_t n_args, const mp __HAL_RCC_DAC12_CLK_ENABLE(); #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32L4) __HAL_RCC_DAC1_CLK_ENABLE(); + #elif defined(STM32L1) + __HAL_RCC_DAC_CLK_ENABLE(); #else #error Unsupported Processor #endif diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index 2dc6e8e8b463b..29306f1b271cc 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -80,7 +80,7 @@ typedef union { struct _dma_descr_t { #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) DMA_Stream_TypeDef *instance; - #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL) + #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL) DMA_Channel_TypeDef *instance; #else #error "Unsupported Processor" @@ -398,6 +398,57 @@ static const uint8_t dma_irqn[NSTREAM] = { DMA1_Channel4_5_6_7_IRQn, }; +#elif defined(STM32L1) + +#define NCONTROLLERS (2) +#define NSTREAMS_PER_CONTROLLER (7) +#define NSTREAM (NCONTROLLERS * NSTREAMS_PER_CONTROLLER) + +#define DMA_SUB_INSTANCE_AS_UINT8(dma_request) (dma_request) + +#define DMA1_ENABLE_MASK (0x007f) // Bits in dma_enable_mask corresponding to DMA1 +#define DMA2_ENABLE_MASK (0x0f80) // Bits in dma_enable_mask corresponding to DMA2 + +// These descriptors are ordered by DMAx_Channel number, and within a channel by request +// number. The duplicate streams are ok as long as they aren't used at the same time. + +// DMA1 streams +const dma_descr_t dma_SPI_1_RX = { DMA1_Channel2, 2, dma_id_1, &dma_init_struct_spi_i2c }; +#if MICROPY_HW_ENABLE_DAC +const dma_descr_t dma_DAC_1_TX = { DMA1_Channel2, 2, dma_id_1, &dma_init_struct_dac }; +#endif +const dma_descr_t dma_SPI_1_TX = { DMA1_Channel3, 3, dma_id_2, &dma_init_struct_spi_i2c }; +#if MICROPY_HW_ENABLE_DAC +const dma_descr_t dma_DAC_2_TX = { DMA1_Channel3, 3, dma_id_2, &dma_init_struct_dac }; +#endif +const dma_descr_t dma_SPI_2_RX = { DMA1_Channel4, 4, dma_id_3, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_I2C_2_TX = { DMA1_Channel4, 4, dma_id_3, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_SPI_2_TX = { DMA1_Channel5, 5, dma_id_4, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_I2C_2_RX = { DMA1_Channel5, 5, dma_id_4, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_I2C_1_TX = { DMA1_Channel6, 6, dma_id_5, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_I2C_1_RX = { DMA1_Channel7, 7, dma_id_6, &dma_init_struct_spi_i2c }; + +// DMA2 streams +const dma_descr_t dma_SPI_3_RX = { DMA2_Channel1, 3, dma_id_7, &dma_init_struct_spi_i2c }; +const dma_descr_t dma_SPI_3_TX = { DMA2_Channel2, 3, dma_id_8, &dma_init_struct_spi_i2c }; + +static const uint8_t dma_irqn[NSTREAM] = { + DMA1_Channel1_IRQn, + DMA1_Channel2_IRQn, + DMA1_Channel3_IRQn, + DMA1_Channel4_IRQn, + DMA1_Channel5_IRQn, + DMA1_Channel6_IRQn, + DMA1_Channel7_IRQn, + DMA2_Channel1_IRQn, + DMA2_Channel2_IRQn, + DMA2_Channel3_IRQn, + DMA2_Channel4_IRQn, + DMA2_Channel5_IRQn, + 0, + 0 +}; + #elif defined(STM32L4) #define NCONTROLLERS (2) @@ -705,7 +756,7 @@ volatile dma_idle_count_t dma_idle; #define DMA_INVALID_CHANNEL 0xff // Value stored in dma_last_channel which means invalid -#if defined(STM32F0) || defined(STM32G0) || defined(STM32L0) +#if defined(STM32F0) || defined(STM32G0) || defined(STM32L0) || defined(STM32L1) #define DMA1_IS_CLK_ENABLED() ((RCC->AHBENR & RCC_AHBENR_DMA1EN) != 0) #if defined(DMA2) #define DMA2_IS_CLK_ENABLED() ((RCC->AHBENR & RCC_AHBENR_DMA2EN) != 0) @@ -1080,7 +1131,7 @@ void DMA1_Channel4_5_6_7_IRQHandler(void) { IRQ_EXIT(DMA1_Channel4_5_6_7_IRQn); } -#elif defined(STM32L4) || defined(STM32WB) +#elif defined(STM32L1) || defined(STM32L4) || defined(STM32WB) void DMA1_Channel1_IRQHandler(void) { IRQ_ENTER(DMA1_Channel1_IRQn); @@ -1166,6 +1217,7 @@ void DMA2_Channel5_IRQHandler(void) { } IRQ_EXIT(DMA2_Channel5_IRQn); } +#if !defined(STM32L1) void DMA2_Channel6_IRQHandler(void) { IRQ_ENTER(DMA2_Channel6_IRQn); if (dma_handle[dma_id_12] != NULL) { @@ -1180,6 +1232,7 @@ void DMA2_Channel7_IRQHandler(void) { } IRQ_EXIT(DMA2_Channel7_IRQn); } +#endif #endif @@ -1260,7 +1313,7 @@ void dma_init_handle(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, uint3 #if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL) dma->Init.Request = dma_descr->sub_instance; #else - #if !defined(STM32F0) + #if !defined(STM32F0) && !defined(STM32L1) dma->Init.Channel = dma_descr->sub_instance; #endif #endif @@ -1284,7 +1337,7 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, uint32_t dir dma_enable_clock(dma_id); - #if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL) + #if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32WB) || defined(STM32WL) // Always reset and configure the H7 and G0/G4/H7/L0/L4/WB/WL DMA peripheral // (dma->State is set to HAL_DMA_STATE_RESET by memset above) // TODO: understand how L0/L4 DMA works so this is not needed @@ -1410,7 +1463,7 @@ static void dma_idle_handler(uint32_t tick) { } #endif -#if defined(STM32F0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L4) +#if defined(STM32F0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) void dma_nohal_init(const dma_descr_t *descr, uint32_t config) { DMA_Channel_TypeDef *dma = descr->instance; @@ -1436,6 +1489,7 @@ void dma_nohal_init(const dma_descr_t *descr, uint32_t config) { #elif defined(STM32G4) uint32_t *dmamux_ctrl = (void *)(DMAMUX1_Channel0_BASE + 0x04 * descr->id); *dmamux_ctrl = (*dmamux_ctrl & ~(0x7f)) | descr->sub_instance; + #elif defined(STM32L1) #else DMA_Request_TypeDef *dma_ctrl = (void *)(((uint32_t)dma & ~0xff) + (DMA1_CSELR_BASE - DMA1_BASE)); // DMA1_CSELR or DMA2_CSELR uint32_t channel_number = (((uint32_t)dma & 0xff) - 0x08) / 20; // 0 through 6 diff --git a/ports/stm32/dma.h b/ports/stm32/dma.h index 70c7e6a005238..37b8710c214c1 100644 --- a/ports/stm32/dma.h +++ b/ports/stm32/dma.h @@ -100,6 +100,20 @@ extern const dma_descr_t dma_I2C_2_RX; extern const dma_descr_t dma_I2C_1_TX; extern const dma_descr_t dma_I2C_1_RX; +#elif defined(STM32L1) +extern const dma_descr_t dma_SPI_1_RX; +extern const dma_descr_t dma_SPI_3_TX; +extern const dma_descr_t dma_SPI_1_TX; +extern const dma_descr_t dma_SPI_3_RX; +extern const dma_descr_t dma_DAC_1_TX; +extern const dma_descr_t dma_SPI_2_RX; +extern const dma_descr_t dma_I2C_2_TX; +extern const dma_descr_t dma_DAC_2_TX; +extern const dma_descr_t dma_SPI_2_TX; +extern const dma_descr_t dma_I2C_2_RX; +extern const dma_descr_t dma_I2C_1_TX; +extern const dma_descr_t dma_I2C_1_RX; + #elif defined(STM32L4) || defined(STM32WB) || defined(STM32WL) extern const dma_descr_t dma_ADC_1_RX; diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index ca23261ca736e..fd7950de3e953 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -210,7 +210,11 @@ STATIC const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = { #endif ETH_WKUP_IRQn, OTG_HS_WKUP_IRQn, + #if defined(STM32L1) + TAMPER_STAMP_IRQn, + #else TAMP_STAMP_IRQn, + #endif RTC_WKUP_IRQn, #endif diff --git a/ports/stm32/extint.h b/ports/stm32/extint.h index 95e29c97fdc7e..fddcc2ae7a017 100644 --- a/ports/stm32/extint.h +++ b/ports/stm32/extint.h @@ -43,7 +43,7 @@ #endif #define EXTI_ETH_WAKEUP (19) #define EXTI_USB_OTG_HS_WAKEUP (20) -#if defined(STM32F0) || defined(STM32G4) || defined(STM32L4) || defined(STM32WL) +#if defined(STM32F0) || defined(STM32G4) || defined(STM32L1) || defined(STM32L4) || defined(STM32WL) #define EXTI_RTC_TIMESTAMP (19) #define EXTI_RTC_WAKEUP (20) #elif defined(STM32H7) || defined(STM32WB) diff --git a/ports/stm32/flash.c b/ports/stm32/flash.c index de537aba212de..16af49c3cfb8a 100644 --- a/ports/stm32/flash.c +++ b/ports/stm32/flash.c @@ -117,6 +117,12 @@ static const flash_layout_t flash_layout[] = { { (uint32_t)FLASH_BASE, (uint32_t)FLASH_PAGE_SIZE, 512 }, }; +#elif defined(STM32L1) + +static const flash_layout_t flash_layout[] = { + { (uint32_t)FLASH_BASE, 0x200, 1024 }, +}; + #elif defined(STM32H7) static const flash_layout_t flash_layout[] = { @@ -264,7 +270,7 @@ int flash_erase(uint32_t flash_dest, uint32_t num_word32) { EraseInitStruct.Page = get_page(flash_dest); EraseInitStruct.Banks = get_bank(flash_dest); EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE; - #elif defined(STM32L0) + #elif defined(STM32L0) || defined(STM32L1) __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR); EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.PageAddress = flash_dest; @@ -286,6 +292,8 @@ int flash_erase(uint32_t flash_dest, uint32_t num_word32) { #if defined(STM32H7) __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2); + #elif defined(STM32L1) + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR); #else __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index 000b478f86b04..1a478cd1cffb4 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -42,7 +42,7 @@ #define ADCx_COMMON __LL_ADC_COMMON_INSTANCE(0) #endif -#if defined(STM32F0) || defined(STM32G0) || defined(STM32L0) || defined(STM32WL) +#if defined(STM32F0) || defined(STM32G0) || defined(STM32L0) || defined(STM32L1) || defined(STM32WL) #define ADC_STAB_DELAY_US (1) #define ADC_TEMPSENSOR_DELAY_US (10) #elif defined(STM32G4) @@ -68,6 +68,9 @@ #elif defined(STM32G0) || defined(STM32L0) || defined(STM32WL) #define ADC_SAMPLETIME_DEFAULT ADC_SAMPLETIME_12CYCLES_5 #define ADC_SAMPLETIME_DEFAULT_INT ADC_SAMPLETIME_160CYCLES_5 +#elif defined(STM32L1) +#define ADC_SAMPLETIME_DEFAULT ADC_SAMPLETIME_384CYCLES +#define ADC_SAMPLETIME_DEFAULT_INT ADC_SAMPLETIME_384CYCLES #elif defined(STM32L4) || defined(STM32WB) #define ADC_SAMPLETIME_DEFAULT ADC_SAMPLETIME_12CYCLES_5 #define ADC_SAMPLETIME_DEFAULT_INT ADC_SAMPLETIME_247CYCLES_5 @@ -239,7 +242,7 @@ void adc_config(ADC_TypeDef *adc, uint32_t bits) { STATIC int adc_get_bits(ADC_TypeDef *adc) { #if defined(STM32F0) || defined(STM32G0) || defined(STM32L0) || defined(STM32WL) uint32_t res = (adc->CFGR1 & ADC_CFGR1_RES) >> ADC_CFGR1_RES_Pos; - #elif defined(STM32F4) || defined(STM32F7) + #elif defined(STM32F4) || defined(STM32F7) || defined(STM32L1) uint32_t res = (adc->CR1 & ADC_CR1_RES) >> ADC_CR1_RES_Pos; #elif defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB) uint32_t res = (adc->CFGR & ADC_CFGR_RES) >> ADC_CFGR_RES_Pos; diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index acedac7a599a7..e1796d1cf025a 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -138,7 +138,7 @@ void machine_init(void) { if (state & RCC_SR_IWDGRSTF || state & RCC_SR_WWDGRSTF) { reset_cause = PYB_RESET_WDT; } else if (state & RCC_SR_PORRSTF - #if !defined(STM32F0) && !defined(STM32F412Zx) + #if !defined(STM32F0) && !defined(STM32F412Zx) && !defined(STM32L1) || state & RCC_SR_BORRSTF #endif ) { @@ -309,7 +309,7 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple); } else { // set - #if defined(STM32F0) || defined(STM32L0) || defined(STM32L4) || defined(STM32G0) + #if defined(STM32F0) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32G0) mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); #else mp_int_t sysclk = mp_obj_get_int(args[0]); diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 51bad18dd53ed..b538f78235487 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -370,6 +370,16 @@ #define MICROPY_HW_MAX_UART (5) #define MICROPY_HW_MAX_LPUART (1) +// Configuration for STM32L1 series +#elif defined(STM32L1) +#define MP_HAL_UNIQUE_ID_ADDRESS (UID_BASE) +#define PYB_EXTI_NUM_VECTORS (23) +#define MICROPY_HW_MAX_I2C (2) +// TODO: L1 has 9 timers but tim0 and tim1 don't exist. +#define MICROPY_HW_MAX_TIMER (11) +#define MICROPY_HW_MAX_UART (5) +#define MICROPY_HW_MAX_LPUART (0) + // Configuration for STM32L4 series #elif defined(STM32L4) diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index 619bde69bf064..092d63e1bb19e 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -87,7 +87,7 @@ void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) { // This logic assumes that all the GPIOx_EN bits are adjacent and ordered in one register - #if defined(STM32F0) + #if defined(STM32F0) || defined(STM32L1) #define AHBxENR AHBENR #define AHBxENR_GPIOAEN_Pos RCC_AHBENR_GPIOAEN_Pos #elif defined(STM32F4) || defined(STM32F7) diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index 358d9cd25d0da..5c587c016d041 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -17,6 +17,8 @@ #define MICROPY_PLATFORM_VERSION "HAL1.6.0" #elif defined(STM32L0) #define MICROPY_PLATFORM_VERSION "HAL1.11.2" +#elif defined(STM32L1) +#define MICROPY_PLATFORM_VERSION "HAL1.10.3" #elif defined(STM32L4) #define MICROPY_PLATFORM_VERSION "HAL1.17.0" #elif defined(STM32WB) diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index f3f1837ece651..c0a0595e5d31b 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -143,7 +143,7 @@ void powerctrl_check_enter_bootloader(void) { if (BL_STATE_GET_KEY(bl_state) == BL_STATE_KEY && (RCC->RCC_SR & RCC_SR_SFTRSTF)) { // Reset by NVIC_SystemReset with bootloader data set -> branch to bootloader RCC->RCC_SR = RCC_SR_RMVF; - #if defined(STM32F0) || defined(STM32F4) || defined(STM32G0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L4) || defined(STM32WB) + #if defined(STM32F0) || defined(STM32F4) || defined(STM32G0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32WB) __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); #endif branch_to_bootloader(BL_STATE_GET_REG(bl_state), BL_STATE_GET_ADDR(bl_state)); @@ -286,7 +286,7 @@ int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk #endif -#if !defined(STM32F0) && !defined(STM32G0) && !defined(STM32L0) && !defined(STM32L4) +#if !defined(STM32F0) && !defined(STM32G0) && !defined(STM32L0) && !defined(STM32L1) && !defined(STM32L4) STATIC uint32_t calc_ahb_div(uint32_t wanted_div) { #if defined(STM32H7) @@ -708,7 +708,7 @@ void powerctrl_enter_stop_mode(void) { __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); #endif - #if !defined(STM32F0) && !defined(STM32G0) && !defined(STM32G4) && !defined(STM32L0) && !defined(STM32L4) && !defined(STM32WB) && !defined(STM32WL) + #if !defined(STM32F0) && !defined(STM32G0) && !defined(STM32G4) && !defined(STM32L0) && !defined(STM32L1) && !defined(STM32L4) && !defined(STM32WB) && !defined(STM32WL) // takes longer to wake but reduces stop current HAL_PWREx_EnableFlashPowerDown(); #endif diff --git a/ports/stm32/powerctrlboot.c b/ports/stm32/powerctrlboot.c index 555457c582e2f..61d48ffe5c953 100644 --- a/ports/stm32/powerctrlboot.c +++ b/ports/stm32/powerctrlboot.c @@ -228,6 +228,74 @@ void SystemClock_Config(void) { #endif } +#elif defined(STM32L1) + +void SystemClock_Config(void) { + // Enable power control peripheral + __HAL_RCC_PWR_CLK_ENABLE(); + + // Set power voltage scaling + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + + // Enable the FLASH 64-bit access + FLASH->ACR = FLASH_ACR_ACC64; + // Set flash latency to 1 because SYSCLK > 16MHz + FLASH->ACR |= MICROPY_HW_FLASH_LATENCY; + + #if MICROPY_HW_CLK_USE_HSI + // Enable the 16MHz internal oscillator + RCC->CR |= RCC_CR_HSION; + while (!(RCC->CR & RCC_CR_HSIRDY)) { + } + RCC->CFGR = RCC_CFGR_PLLSRC_HSI; + #else + // Enable the 8MHz external oscillator + RCC->CR |= RCC_CR_HSEBYP; + RCC->CR |= RCC_CR_HSEON; + while (!(RCC->CR & RCC_CR_HSERDY)) { + } + RCC->CFGR = RCC_CFGR_PLLSRC_HSE; + #endif + // Use HSI16 and the PLL to get a 32MHz SYSCLK + RCC->CFGR |= MICROPY_HW_CLK_PLLMUL | MICROPY_HW_CLK_PLLDIV; + RCC->CR |= RCC_CR_PLLON; + while (!(RCC->CR & RCC_CR_PLLRDY)) { + // Wait for PLL to lock + } + RCC->CFGR |= RCC_CFGR_SW_PLL; + + while ((RCC->CFGR & RCC_CFGR_SWS_Msk) != RCC_CFGR_SWS_PLL) { + // Wait for SYSCLK source to change + } + + SystemCoreClockUpdate(); + powerctrl_config_systick(); + + #if MICROPY_HW_ENABLE_USB + // Enable the 48MHz internal oscillator + RCC->CRRCR |= RCC_CRRCR_HSI48ON; + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; + SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48; + while (!(RCC->CRRCR & RCC_CRRCR_HSI48RDY)) { + // Wait for HSI48 to be ready + } + + // Select RC48 as HSI48 for USB and RNG + RCC->CCIPR |= RCC_CCIPR_HSI48SEL; + + // Synchronise HSI48 with 1kHz USB SoF + __HAL_RCC_CRS_CLK_ENABLE(); + CRS->CR = 0x20 << CRS_CR_TRIM_Pos; + CRS->CFGR = 2 << CRS_CFGR_SYNCSRC_Pos | 0x22 << CRS_CFGR_FELIM_Pos + | __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000) << CRS_CFGR_RELOAD_Pos; + #endif + + // Disable the Debug Module in low-power mode due to prevent + // unexpected HardFault after __WFI(). + #if !defined(NDEBUG) + DBGMCU->CR &= ~(DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY); + #endif +} #elif defined(STM32WB) #include "stm32wbxx_ll_hsem.h" diff --git a/ports/stm32/resethandler_m3.s b/ports/stm32/resethandler_m3.s new file mode 100644 index 0000000000000..05a44306e1eb6 --- /dev/null +++ b/ports/stm32/resethandler_m3.s @@ -0,0 +1,75 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + .syntax unified + .cpu cortex-m3 + .fpu softvfp + .thumb + + .section .text.Reset_Handler + .global Reset_Handler + .type Reset_Handler, %function + +Reset_Handler: + /* Save the first argument to pass through to stm32_main */ + mov r4, r0 + + /* Load the stack pointer */ + ldr r0, =_estack + mov sp, r0 + + /* Initialise the data section */ + ldr r1, =_sidata + ldr r2, =_sdata + ldr r3, =_edata + b .data_copy_entry +.data_copy_loop: + ldr r0, [r1] + adds r1, #4 + str r0, [r2] + adds r2, #4 +.data_copy_entry: + cmp r2, r3 + bcc .data_copy_loop + + /* Zero out the BSS section */ + movs r0, #0 + ldr r1, =_sbss + ldr r2, =_ebss + b .bss_zero_entry +.bss_zero_loop: + str r0, [r1] + adds r1, #4 +.bss_zero_entry: + cmp r1, r2 + bcc .bss_zero_loop + + /* Initialise the system and jump to the main code */ + bl SystemInit + mov r0, r4 + bl stm32_main + + .size Reset_Handler, .-Reset_Handler diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index aacfc3805e97c..874e427cffa01 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -91,6 +91,15 @@ STATIC bool rtc_need_init_finalise = false; #define RCC_BDCR_LSEON RCC_CSR_LSEON #define RCC_BDCR_LSERDY RCC_CSR_LSERDY #define RCC_BDCR_LSEBYP RCC_CSR_LSEBYP +#elif defined(STM32L1) +#define BDCR CR +#define RCC_BDCR_RTCEN RCC_CSR_RTCEN +#define RCC_BDCR_RTCSEL RCC_CSR_RTCSEL +#define RCC_BDCR_RTCSEL_0 RCC_CSR_RTCSEL_0 +#define RCC_BDCR_RTCSEL_1 RCC_CSR_RTCSEL_1 +#define RCC_BDCR_LSEON RCC_CSR_LSEON +#define RCC_BDCR_LSERDY RCC_CSR_LSERDY +#define RCC_BDCR_LSEBYP RCC_CSR_LSEBYP #endif void rtc_init_start(bool force_init) { @@ -664,7 +673,15 @@ mp_obj_t pyb_rtc_wakeup(size_t n_args, const mp_obj_t *args) { wucksel -= 1; } if (div <= 16) { + #if defined(STM32L1) + if (rtc_use_lse) { + wut = LSE_VALUE / div * ms / 1000; + } else { + wut = LSI_VALUE / div * ms / 1000; + } + #else wut = 32768 / div * ms / 1000; + #endif } else { // use 1Hz clock wucksel = 4; diff --git a/ports/stm32/stm32.mk b/ports/stm32/stm32.mk index c55b243fe5734..b4f73a67f381d 100644 --- a/ports/stm32/stm32.mk +++ b/ports/stm32/stm32.mk @@ -48,7 +48,7 @@ CFLAGS_CORTEX_M += -mfpu=fpv5-d16 -mfloat-abi=hard SUPPORTS_HARDWARE_FP_SINGLE = 1 SUPPORTS_HARDWARE_FP_DOUBLE = 1 else -ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0 wl)) +ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0 l1 wl)) CFLAGS_CORTEX_M += -msoft-float else CFLAGS_CORTEX_M += -mfpu=fpv4-sp-d16 -mfloat-abi=hard @@ -64,6 +64,7 @@ CFLAGS_MCU_f7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 CFLAGS_MCU_g0 = $(CFLAGS_CORTEX_M) -mtune=cortex-m0plus -mcpu=cortex-m0plus CFLAGS_MCU_g4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_l0 = $(CFLAGS_CORTEX_M) -mtune=cortex-m0plus -mcpu=cortex-m0plus +CFLAGS_MCU_l1 = $(CFLAGS_CORTEX_M) -mtune=cortex-m3 -mcpu=cortex-m3 CFLAGS_MCU_l4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_h7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 CFLAGS_MCU_wb = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 diff --git a/ports/stm32/stm32_it.c b/ports/stm32/stm32_it.c index 2f89f67deb539..c2604992186f9 100644 --- a/ports/stm32/stm32_it.c +++ b/ports/stm32/stm32_it.c @@ -520,11 +520,19 @@ void ETH_WKUP_IRQHandler(void) { } #endif +#if defined(STM32L1) +void TAMPER_STAMP_IRQHandler(void) { + IRQ_ENTER(TAMPER_STAMP_IRQn); + Handle_EXTI_Irq(EXTI_RTC_TIMESTAMP); + IRQ_EXIT(TAMPER_STAMP_IRQn); +} +#else void TAMP_STAMP_IRQHandler(void) { IRQ_ENTER(TAMP_STAMP_IRQn); Handle_EXTI_Irq(EXTI_RTC_TIMESTAMP); IRQ_EXIT(TAMP_STAMP_IRQn); } +#endif void RTC_WKUP_IRQHandler(void) { IRQ_ENTER(RTC_WKUP_IRQn); @@ -698,6 +706,12 @@ void TIM6_DAC_LPTIM1_IRQHandler(void) { timer_irq_handler(6); IRQ_EXIT(TIM6_DAC_LPTIM1_IRQn); } +#elif defined(STM32L1) +void TIM6_IRQHandler(void) { + IRQ_ENTER(TIM6_IRQn); + timer_irq_handler(6); + IRQ_EXIT(TIM6_IRQn); +} #else void TIM6_DAC_IRQHandler(void) { IRQ_ENTER(TIM6_DAC_IRQn); @@ -764,6 +778,26 @@ void TIM8_TRG_COM_TIM14_IRQHandler(void) { } #endif +#if defined(STM32L1) +void TIM9_IRQHandler(void) { + IRQ_ENTER(TIM9_IRQn); + timer_irq_handler(9); + IRQ_EXIT(TIM9_IRQn); +} + +void TIM10_IRQHandler(void) { + IRQ_ENTER(TIM9_IRQn); + timer_irq_handler(10); + IRQ_EXIT(TIM9_IRQn); +} + +void TIM11_IRQHandler(void) { + IRQ_ENTER(TIM9_IRQn); + timer_irq_handler(11); + IRQ_EXIT(TIM9_IRQn); +} +#endif + #if defined(STM32G0) void TIM14_IRQHandler(void) { IRQ_ENTER(TIM14_IRQn); diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 7a6ccd4dfd2d9..8816c218f5e32 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -431,7 +431,7 @@ STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) { #endif } -#if !defined(STM32L0) +#if !defined(STM32L0) && !defined(STM32L1) // Computes the 8-bit value for the DTG field in the BDTR register. // @@ -522,7 +522,7 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ self->tim.Init.ClockDivision == TIM_CLOCKDIVISION_DIV4 ? 4 : self->tim.Init.ClockDivision == TIM_CLOCKDIVISION_DIV2 ? 2 : 1); - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) #if defined(IS_TIM_ADVANCED_INSTANCE) if (IS_TIM_ADVANCED_INSTANCE(self->tim.Instance)) #elif defined(IS_TIM_BREAK_INSTANCE) @@ -640,7 +640,7 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons args[ARG_div].u_int == 4 ? TIM_CLOCKDIVISION_DIV4 : TIM_CLOCKDIVISION_DIV1; - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) init->RepetitionCounter = 0; #endif @@ -772,7 +772,7 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons // init TIM HAL_TIM_Base_Init(&self->tim); - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) #if defined(IS_TIM_ADVANCED_INSTANCE) if (IS_TIM_ADVANCED_INSTANCE(self->tim.Instance)) #elif defined(IS_TIM_BREAK_INSTANCE) @@ -833,7 +833,7 @@ STATIC const uint32_t tim_instance_table[MICROPY_HW_MAX_TIMER] = { TIM_ENTRY(5, TIM5_IRQn), #endif #if defined(TIM6) - #if defined(STM32F412Zx) + #if defined(STM32F412Zx) || defined(STM32L1) TIM_ENTRY(6, TIM6_IRQn), #elif defined(STM32G0) TIM_ENTRY(6, TIM6_DAC_LPTIM1_IRQn), @@ -858,14 +858,26 @@ STATIC const uint32_t tim_instance_table[MICROPY_HW_MAX_TIMER] = { #endif #endif #if defined(TIM9) + #if defined(STM32L1) + TIM_ENTRY(9, TIM9_IRQn), + #else TIM_ENTRY(9, TIM1_BRK_TIM9_IRQn), #endif + #endif #if defined(TIM10) + #if defined(STM32L1) + TIM_ENTRY(10, TIM10_IRQn), + #else TIM_ENTRY(10, TIM1_UP_TIM10_IRQn), #endif + #endif #if defined(TIM11) + #if defined(STM32L1) + TIM_ENTRY(11, TIM11_IRQn), + #else TIM_ENTRY(11, TIM1_TRG_COM_TIM11_IRQn), #endif + #endif #if defined(TIM12) TIM_ENTRY(12, TIM8_BRK_TIM12_IRQn), #endif @@ -936,7 +948,11 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, size_t n_args, siz memset(tim, 0, sizeof(*tim)); tim->base.type = &pyb_timer_type; tim->tim_id = tim_id; + #if defined(STM32L1) + tim->is_32bit = tim_id == 5; + #else tim->is_32bit = tim_id == 2 || tim_id == 5; + #endif tim->callback = mp_const_none; uint32_t ti = tim_instance_table[tim_id - 1]; tim->tim.Instance = (TIM_TypeDef *)(ti & 0xffffff00); @@ -1168,7 +1184,7 @@ STATIC mp_obj_t pyb_timer_channel(size_t n_args, const mp_obj_t *pos_args, mp_ma } oc_config.OCPolarity = TIM_OCPOLARITY_HIGH; oc_config.OCFastMode = TIM_OCFAST_DISABLE; - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) oc_config.OCNPolarity = TIM_OCNPOLARITY_HIGH; oc_config.OCIdleState = TIM_OCIDLESTATE_SET; oc_config.OCNIdleState = TIM_OCNIDLESTATE_SET; @@ -1180,7 +1196,7 @@ STATIC mp_obj_t pyb_timer_channel(size_t n_args, const mp_obj_t *pos_args, mp_ma } else { pyb_timer_channel_callback(MP_OBJ_FROM_PTR(chan), chan->callback); } - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) // Start the complimentary channel too (if its supported) if (IS_TIM_CCXN_INSTANCE(self->tim.Instance, TIMER_CHANNEL(chan))) { HAL_TIMEx_PWMN_Start(&self->tim, TIMER_CHANNEL(chan)); @@ -1203,7 +1219,7 @@ STATIC mp_obj_t pyb_timer_channel(size_t n_args, const mp_obj_t *pos_args, mp_ma oc_config.OCPolarity = TIM_OCPOLARITY_HIGH; } oc_config.OCFastMode = TIM_OCFAST_DISABLE; - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) if (oc_config.OCPolarity == TIM_OCPOLARITY_HIGH) { oc_config.OCNPolarity = TIM_OCNPOLARITY_HIGH; } else { @@ -1222,7 +1238,7 @@ STATIC mp_obj_t pyb_timer_channel(size_t n_args, const mp_obj_t *pos_args, mp_ma } else { pyb_timer_channel_callback(MP_OBJ_FROM_PTR(chan), chan->callback); } - #if !defined(STM32L0) + #if !defined(STM32L0) && !defined(STM32L1) // Start the complimentary channel too (if its supported) if (IS_TIM_CCXN_INSTANCE(self->tim.Instance, TIMER_CHANNEL(chan))) { HAL_TIMEx_OCN_Start(&self->tim, TIMER_CHANNEL(chan)); diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index cea49f4ba1c9b..6d1240cf8d5aa 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -38,7 +38,7 @@ #include "irq.h" #include "pendsv.h" -#if defined(STM32F4) +#if defined(STM32F4) || defined(STM32L1) #define UART_RXNE_IS_SET(uart) ((uart)->SR & USART_SR_RXNE) #else #if defined(STM32G0) || defined(STM32H7) || defined(STM32WL) @@ -101,6 +101,11 @@ #define USART_CR2_IE_ALL (USART_CR2_IE_BASE) #define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_WUFIE) +#elif defined(STM32L1) +#define USART_CR1_IE_ALL (USART_CR1_IE_BASE) +#define USART_CR2_IE_ALL (USART_CR2_IE_BASE) +#define USART_CR3_IE_ALL (USART_CR3_IE_BASE) + #elif defined(STM32L4) || defined(STM32WB) || defined(STM32WL) #define USART_CR1_IE_ALL (USART_CR1_IE_BASE | USART_CR1_EOBIE | USART_CR1_RTOIE | USART_CR1_CMIE) #define USART_CR2_IE_ALL (USART_CR2_IE_BASE) @@ -580,7 +585,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj, huart.FifoMode = UART_FIFOMODE_ENABLE; #endif - #if !defined(STM32F4) + #if !defined(STM32F4) && !defined(STM32L1) huart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; #endif @@ -1016,7 +1021,7 @@ STATIC bool uart_wait_flag_set(pyb_uart_obj_t *self, uint32_t flag, uint32_t tim // an interrupt and the flag can be set quickly if the baudrate is large. uint32_t start = HAL_GetTick(); for (;;) { - #if defined(STM32F4) + #if defined(STM32F4) || defined(STM32L1) if (self->uartx->SR & flag) { return true; } @@ -1071,7 +1076,7 @@ size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_chars, } else { data = *src++; } - #if defined(STM32F4) + #if defined(STM32F4) || defined(STM32L1) uart->DR = data; #else uart->TDR = data; @@ -1109,7 +1114,7 @@ void uart_irq_handler(mp_uint_t uart_id) { } // Capture IRQ status flags. - #if defined(STM32F4) + #if defined(STM32F4) || defined(STM32L1) self->mp_irq_flags = self->uartx->SR; bool rxne_is_set = self->mp_irq_flags & USART_SR_RXNE; bool did_clear_sr = false; @@ -1153,7 +1158,7 @@ void uart_irq_handler(mp_uint_t uart_id) { } // Clear other interrupt flags that can trigger this IRQ handler. - #if defined(STM32F4) + #if defined(STM32F4) || defined(STM32L1) if (did_clear_sr) { // SR was cleared above. Re-enable IDLE if it should be enabled. if (self->mp_irq_trigger & UART_FLAG_IDLE) { diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index ec8a27591c1e5..61d1ac4397903 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -103,7 +103,7 @@ size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_chars, void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); static inline bool uart_tx_avail(pyb_uart_obj_t *self) { - #if defined(STM32F4) + #if defined(STM32F4) || defined(STM32L1) return self->uartx->SR & USART_SR_TXE; #elif defined(STM32G0) || defined(STM32H7) || defined(STM32WL) return self->uartx->ISR & USART_ISR_TXE_TXFNF; From e6d351318746495bf88d2c4bd7cbd81e94a2290e Mon Sep 17 00:00:00 2001 From: yn386 Date: Mon, 19 Sep 2022 17:57:27 +0900 Subject: [PATCH 2646/3533] stm32/boards/NUCLEO_L152RE: Add NUCLEO-L152RE board support. This change adds NUCLEO-L152RE support to the STM32 port. NUCLEO-L152RE: https://www.st.com/en/evaluation-tools/nucleo-l152re.html This board use STM32L152RE: https://www.st.com/en/microcontrollers-microprocessors/stm32l152re.html --- ports/stm32/boards/NUCLEO_L152RE/board.json | 15 ++++ ports/stm32/boards/NUCLEO_L152RE/deploy.md | 31 ++++++++ .../boards/NUCLEO_L152RE/mpconfigboard.h | 74 ++++++++++++++++++ .../boards/NUCLEO_L152RE/mpconfigboard.mk | 4 + ports/stm32/boards/NUCLEO_L152RE/pins.csv | 76 +++++++++++++++++++ .../boards/NUCLEO_L152RE/stm32l1xx_hal_conf.h | 10 +++ 6 files changed, 210 insertions(+) create mode 100644 ports/stm32/boards/NUCLEO_L152RE/board.json create mode 100644 ports/stm32/boards/NUCLEO_L152RE/deploy.md create mode 100644 ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.h create mode 100644 ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.mk create mode 100644 ports/stm32/boards/NUCLEO_L152RE/pins.csv create mode 100644 ports/stm32/boards/NUCLEO_L152RE/stm32l1xx_hal_conf.h diff --git a/ports/stm32/boards/NUCLEO_L152RE/board.json b/ports/stm32/boards/NUCLEO_L152RE/board.json new file mode 100644 index 0000000000000..7b34276937553 --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/board.json @@ -0,0 +1,15 @@ +{ + "deploy": [ + "./deploy.md" + ], + "docs": "", + "features": [], + "images": [ + "nucleo_l152re.jpg" + ], + "mcu": "stm32l1", + "product": "Nucleo L152RE", + "thumbnail": "", + "url": "", + "vendor": "ST Microelectronics" +} diff --git a/ports/stm32/boards/NUCLEO_L152RE/deploy.md b/ports/stm32/boards/NUCLEO_L152RE/deploy.md new file mode 100644 index 0000000000000..0323981d2d664 --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/deploy.md @@ -0,0 +1,31 @@ +### STM32 via ST-Link + +Nucleo and Discovery boards typically include a built-in ST-Link programmer. + +A `.bin` or `.hex` file can be flashed using [st-flash](https://github.com/stlink-org/stlink). + +```bash +# Optional erase to clear existing filesystem. +st-flash erase + +# Flash .bin +st-flash write firmware.bin 0x08000000 +# or, flash .hex +st-flash --format ihex write firmware.hex +``` + +A `.hex` file can be flashed using [STM32 Cube Programmer](https://www.st.com/en/development-tools/stm32cubeprog.html). + +```bash +STM32_Programmer.sh -c port=SWD mode=UR dLPM -d firmware.hex -v -hardRst +``` + +### STM32 via DFU + +Boards with USB support can also be programmed via the ST DFU bootloader, using e.g. [dfu-util](http://dfu-util.sourceforge.net/) or [pydfu.py](https://github.com/micropython/micropython/blob/master/tools/pydfu.py). + +To enter the bootloader the `BOOT0` pin can be connected to `VCC` during reset, or you can use `machine.bootloader()` from the MicroPython REPL. + +```bash +dfu-util --alt 0 -D firmware.dfu +``` diff --git a/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.h new file mode 100644 index 0000000000000..3609955893fe1 --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.h @@ -0,0 +1,74 @@ +#define MICROPY_HW_BOARD_NAME "NUCLEO-L152RE" +#define MICROPY_HW_MCU_NAME "STM32L152xE" + +#define MICROPY_HW_HAS_SWITCH (1) +#define MICROPY_HW_ENABLE_RTC (1) +// This board has an external 32kHz crystal +#define MICROPY_HW_RTC_USE_LSE (1) +#define MICROPY_HW_ENABLE_SERVO (1) +#define MICROPY_HW_ENABLE_DAC (1) + +// HSE is 8MHz, HSI is 16MHz CPU freq set to 32MHz +// Default source for the clock is HSI. +// For revisions of the board greater than C-01, HSE can be used as a +// clock source by removing the #define MICROPY_HW_CLK_USE_HSE line +#define MICROPY_HW_CLK_USE_HSI (1) + +#if MICROPY_HW_CLK_USE_HSI +#define MICROPY_HW_CLK_PLLMUL (RCC_CFGR_PLLMUL6) +#define MICROPY_HW_CLK_PLLDIV (RCC_CFGR_PLLDIV3) +#else +#define MICROPY_HW_CLK_PLLMUL (RCC_CFGR_PLLMUL12) +#define MICROPY_HW_CLK_PLLDIV (RCC_CFGR_PLLDIV3) +#endif + +// UART config +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART3_TX (pin_B10) +#define MICROPY_HW_UART3_RX (pin_B11) +#define MICROPY_HW_UART4_TX (pin_C10) +#define MICROPY_HW_UART4_RX (pin_C11) +#define MICROPY_HW_UART5_TX (pin_C12) +#define MICROPY_HW_UART5_RX (pin_D2) +// UART 2 connects to the STM32F103 (STLINK) on the Nucleo board +// and this is exposed as a USB Serial port. +#define MICROPY_HW_UART_REPL PYB_UART_2 +#define MICROPY_HW_UART_REPL_BAUD 115200 + +// I2C buses +#define MICROPY_HW_I2C1_SCL (pin_B8) // Arduino D15, pin 3 on CN10 +#define MICROPY_HW_I2C1_SDA (pin_B9) // D14, pin 5 on CN10 +#define MICROPY_HW_I2C2_SCL (pin_B10) // Arduino D6, pin 25 on CN10 +#define MICROPY_HW_I2C2_SDA (pin_B11) // Arduino D3, pin 31 on CN10 + +// SPI buses +#define MICROPY_HW_SPI1_NSS (pin_A15) // pin 17 on CN7 +#define MICROPY_HW_SPI1_SCK (pin_A5) // Arduino D13, pin 11 on CN10 +#define MICROPY_HW_SPI1_MISO (pin_A6) // Arduino D12, pin 13 on CN10 +#define MICROPY_HW_SPI1_MOSI (pin_A7) // Arduino D11, pin 15 on CN10 + +#define MICROPY_HW_SPI2_NSS (pin_B12) // pin 16 on CN10 +#define MICROPY_HW_SPI2_SCK (pin_B13) // pin 30 on CN10 +#define MICROPY_HW_SPI2_MISO (pin_B14) // pin 28 on CN10 +#define MICROPY_HW_SPI2_MOSI (pin_B15) // pin 26 on CN10 + +#define MICROPY_HW_SPI3_NSS (pin_A4) // Arduino A2, pin 32 on CN7 +#define MICROPY_HW_SPI3_SCK (pin_C10) // Arduino D3, pin 31 on CN10 +#define MICROPY_HW_SPI3_MISO (pin_C11) // Arduino D5, pin 27 on CN10 +#define MICROPY_HW_SPI3_MOSI (pin_C12) // Arduino D4, pin 29 on CN10 + +// USRSW is pulled low. Pressing the button makes the input go high. +#define MICROPY_HW_USRSW_PIN (pin_C13) +#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) +#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) +#define MICROPY_HW_USRSW_PRESSED (0) + +// LEDs +#define MICROPY_HW_LED1 (pin_A5) // Green LD2 LED on Nucleo +#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin)) +#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin)) + +#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_1 diff --git a/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.mk b/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.mk new file mode 100644 index 0000000000000..a62a775ac8ddb --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/mpconfigboard.mk @@ -0,0 +1,4 @@ +MCU_SERIES = l1 +CMSIS_MCU = STM32L152xE +AF_FILE = boards/stm32l152_af.csv +LD_FILES = boards/stm32l152xe.ld boards/common_basic.ld diff --git a/ports/stm32/boards/NUCLEO_L152RE/pins.csv b/ports/stm32/boards/NUCLEO_L152RE/pins.csv new file mode 100644 index 0000000000000..035d933f5dd45 --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/pins.csv @@ -0,0 +1,76 @@ +D0,PA3 +D1,PA2 +D2,PA10 +D3,PB3 +D4,PB5 +D5,PB4 +D6,PB10 +D7,PA8 +D8,PA9 +D9,PC7 +D10,PB6 +D11,PA7 +D12,PA6 +D13,PA5 +D14,PB9 +D15,PB8 +A0,PA0 +A1,PA1 +A2,PA4 +A3,PB0 +A4,PC1 +A5,PC0 +PA0,PA0 +PA1,PA1 +PA2,PA2 +PA3,PA3 +PA4,PA4 +PA5,PA5 +PA6,PA6 +PA7,PA7 +PA8,PA8 +PA9,PA9 +PA10,PA10 +PA11,PA11 +PA12,PA12 +PA15,PA15 +PB0,PB0 +PB1,PB1 +PB2,PB2 +PB3,PB3 +PB4,PB4 +PB5,PB5 +PB6,PB6 +PB7,PB7 +PB8,PB8 +PB9,PB9 +PB10,PB10 +PB11,PB11 +PB12,PB12 +PB13,PB13 +PB14,PB14 +PB15,PB15 +PC0,PC0 +PC1,PC1 +PC2,PC2 +PC3,PC3 +PC4,PC4 +PC5,PC5 +PC6,PC6 +PC7,PC7 +PC8,PC8 +PC9,PC9 +PC10,PC10 +PC11,PC11 +PC12,PC12 +PC13,PC13 +PC14,PC14 +PC15,PC15 +PD2,PD2 +PH0,PH0 +PH1,PH1 +LED_GREEN,PA5 +LED_ORANGE,PA5 +LED_RED,PA5 +LED_BLUE,PA4 +SW,PC13 diff --git a/ports/stm32/boards/NUCLEO_L152RE/stm32l1xx_hal_conf.h b/ports/stm32/boards/NUCLEO_L152RE/stm32l1xx_hal_conf.h new file mode 100644 index 0000000000000..7ee8204cbf064 --- /dev/null +++ b/ports/stm32/boards/NUCLEO_L152RE/stm32l1xx_hal_conf.h @@ -0,0 +1,10 @@ +/* This file is part of the MicroPython project, http://micropython.org/ + * The MIT License (MIT) + * Copyright (c) 2019 Damien P. George + */ +#ifndef MICROPY_INCLUDED_STM32L1XX_HAL_CONF_H +#define MICROPY_INCLUDED_STM32L1XX_HAL_CONF_H + +#include "boards/stm32l1xx_hal_conf_base.h" + +#endif // MICROPY_INCLUDED_STM32L1XX_HAL_CONF_H From 3abcfb9aecd567c4c74fd3c99703f23c372b7445 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 26 Sep 2022 00:07:58 +1000 Subject: [PATCH 2647/3533] esp32/modsocket: Use mp_obj_is_integer to test port type. Because the value may be a big integer, which is still a valid type to use. Fixes issue #9410. Signed-off-by: Damien George --- ports/esp32/modsocket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 334c5bae33c4f..9812eb347689c 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -214,7 +214,7 @@ static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struc }; mp_obj_t port = portx; - if (mp_obj_is_small_int(port)) { + if (mp_obj_is_integer(port)) { // This is perverse, because lwip_getaddrinfo promptly converts it back to an int, but // that's the API we have to work with ... port = mp_obj_str_binary_op(MP_BINARY_OP_MODULO, mp_obj_new_str_via_qstr("%s", 2), port); From dd9dcb594c577cb818c336db59a884fd329c3840 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 25 Sep 2022 22:12:29 +1000 Subject: [PATCH 2648/3533] esp32/machine_pwm: Don't use LEDC_USE_REF_TICK on ESP32-C3 variants. Because it's not supported by this particular MCU (since IDF v4.4.2). Signed-off-by: Damien George --- ports/esp32/machine_pwm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 0107187a8c98c..2a456c71c7834 100644 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -97,8 +97,10 @@ STATIC ledc_timer_config_t timers[PWM_TIMER_MAX]; // How much to shift from the HIGHEST_PWM_RES duty resolution to the user interface duty resolution UI_RES_16_BIT #define UI_RES_SHIFT (UI_RES_16_BIT - HIGHEST_PWM_RES) // 0 for ESP32, 2 for S2, S3, C3 +#if SOC_LEDC_SUPPORT_REF_TICK // If the PWM frequency is less than EMPIRIC_FREQ, then LEDC_REF_CLK_HZ(1 MHz) source is used, else LEDC_APB_CLK_HZ(80 MHz) source is used #define EMPIRIC_FREQ (10) // Hz +#endif // Config of timer upon which we run all PWM'ed GPIO pins STATIC bool pwm_inited = false; @@ -208,9 +210,11 @@ STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_conf if (freq != timer->freq_hz) { // Find the highest bit resolution for the requested frequency unsigned int i = LEDC_APB_CLK_HZ; // 80 MHz + #if SOC_LEDC_SUPPORT_REF_TICK if (freq < EMPIRIC_FREQ) { i = LEDC_REF_CLK_HZ; // 1 MHz } + #endif #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) // original code @@ -243,9 +247,11 @@ STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_conf timer->duty_resolution = res; timer->freq_hz = freq; timer->clk_cfg = LEDC_USE_APB_CLK; + #if SOC_LEDC_SUPPORT_REF_TICK if (freq < EMPIRIC_FREQ) { timer->clk_cfg = LEDC_USE_REF_TICK; } + #endif // Set frequency esp_err_t err = ledc_timer_config(timer); From 9d6f474ea49fd89b7a1a90b830e6014ef70a89b7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sun, 25 Sep 2022 22:15:45 +1000 Subject: [PATCH 2649/3533] py/objstr: Don't treat bytes as unicode in str.count. `b'\xaa \xaa'.count(b'\xaa')` now (correctly) returns 2 instead of 1. Fixes issue #9404. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- py/objstr.c | 4 +++- tests/basics/bytes_count.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/py/objstr.c b/py/objstr.c index 62d7bfb4cc396..55e737fffc4fc 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1768,6 +1768,8 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) { return MP_OBJ_NEW_SMALL_INT(utf8_charlen(start, end - start) + 1); } + bool is_str = self_type == &mp_type_str; + // count the occurrences mp_int_t num_occurrences = 0; for (const byte *haystack_ptr = start; haystack_ptr + needle_len <= end;) { @@ -1775,7 +1777,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) { num_occurrences++; haystack_ptr += needle_len; } else { - haystack_ptr = utf8_next_char(haystack_ptr); + haystack_ptr = is_str ? utf8_next_char(haystack_ptr) : haystack_ptr + 1; } } diff --git a/tests/basics/bytes_count.py b/tests/basics/bytes_count.py index 5fa0730f5cff0..e71f09db00e29 100644 --- a/tests/basics/bytes_count.py +++ b/tests/basics/bytes_count.py @@ -48,6 +48,13 @@ print(b"aaaa".count(b'a', -1, 5)) print(b"abbabba".count(b"abba")) +print(b'\xaa \xaa'.count(b'\xaa')) +print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa')) +print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 1) +print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 2) +print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 1, 3) +print(b'\xaa \xaa \xaa \xaa'.count(b'\xaa'), 2, 3) + def t(): return True From 0bc1d1055748dd41a2db10a4c6cab32499c488f9 Mon Sep 17 00:00:00 2001 From: Matt Trentini Date: Sun, 11 Sep 2022 11:40:59 +1000 Subject: [PATCH 2650/3533] rp2/Makefile: Add support for BOARD_VARIANTS. Following stm32. This allows a single board definition to define variants of its configuration. --- ports/rp2/Makefile | 9 +++++++++ py/mkrules.cmake | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index c603f5403fcac..7057021bfccbe 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -22,6 +22,10 @@ ifeq ($(DEBUG),1) CMAKE_ARGS += -DCMAKE_BUILD_TYPE=Debug endif +ifdef BOARD_VARIANT +CMAKE_ARGS += -DBOARD_VARIANT=${BOARD_VARIANT} +endif + HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" all: @@ -38,3 +42,8 @@ submodules: GIT_SUBMODULES=$$(cmake -B $(BUILD)/submodules -DECHO_SUBMODULES=1 ${CMAKE_ARGS} -S . 2>&1 | \ grep '^GIT_SUBMODULES=' | cut -d= -f2); \ $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$${GIT_SUBMODULES}" submodules + +query-variants: + @BOARD_VARIANTS=$$(cmake -B $(BUILD)/variants -DECHO_BOARD_VARIANTS=1 ${CMAKE_ARGS} -S . 2>&1 | \ + grep '^BOARD_VARIANTS=' | cut -d= -f2); \ + echo "VARIANTS: $${BOARD_VARIANTS}" diff --git a/py/mkrules.cmake b/py/mkrules.cmake index e7c4101ddb72d..b29986585a798 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -223,3 +223,9 @@ if(ECHO_SUBMODULES) execute_process(COMMAND ${CMAKE_COMMAND} -E echo "GIT_SUBMODULES=${GIT_SUBMODULES}") message(FATAL_ERROR "Done") endif() + +# Display BOARD_VARIANTS +if(ECHO_BOARD_VARIANTS) + execute_process(COMMAND ${CMAKE_COMMAND} -E echo "BOARD_VARIANTS=${BOARD_VARIANTS}") + message(FATAL_ERROR "Done") +endif() From bdbc44474f92db19a40b5f710a140a0bf70fb0ec Mon Sep 17 00:00:00 2001 From: Matt Trentini Date: Thu, 7 Apr 2022 23:33:36 +1000 Subject: [PATCH 2651/3533] rp2/boards/WEACTSTUDIO: Add WEACTSTUDIO with multiple variants. This supports 2, 4, 8 and 16MB flash variants. --- ports/rp2/boards/WEACTSTUDIO/README.md | 34 ++++++++++ ports/rp2/boards/WEACTSTUDIO/board.json | 23 +++++++ ports/rp2/boards/WEACTSTUDIO/deploy.md | 8 +++ ports/rp2/boards/WEACTSTUDIO/manifest.py | 2 + ports/rp2/boards/WEACTSTUDIO/modules/board.py | 4 ++ .../boards/WEACTSTUDIO/mpconfigboard.cmake | 26 ++++++++ ports/rp2/boards/WEACTSTUDIO/mpconfigboard.h | 4 ++ .../rp2/boards/WEACTSTUDIO/weactstudio_16mb.h | 18 ++++++ .../rp2/boards/WEACTSTUDIO/weactstudio_2mb.h | 18 ++++++ .../rp2/boards/WEACTSTUDIO/weactstudio_4mb.h | 18 ++++++ .../rp2/boards/WEACTSTUDIO/weactstudio_8mb.h | 18 ++++++ .../boards/WEACTSTUDIO/weactstudio_common.h | 62 +++++++++++++++++++ 12 files changed, 235 insertions(+) create mode 100644 ports/rp2/boards/WEACTSTUDIO/README.md create mode 100644 ports/rp2/boards/WEACTSTUDIO/board.json create mode 100644 ports/rp2/boards/WEACTSTUDIO/deploy.md create mode 100644 ports/rp2/boards/WEACTSTUDIO/manifest.py create mode 100644 ports/rp2/boards/WEACTSTUDIO/modules/board.py create mode 100644 ports/rp2/boards/WEACTSTUDIO/mpconfigboard.cmake create mode 100644 ports/rp2/boards/WEACTSTUDIO/mpconfigboard.h create mode 100644 ports/rp2/boards/WEACTSTUDIO/weactstudio_16mb.h create mode 100644 ports/rp2/boards/WEACTSTUDIO/weactstudio_2mb.h create mode 100644 ports/rp2/boards/WEACTSTUDIO/weactstudio_4mb.h create mode 100644 ports/rp2/boards/WEACTSTUDIO/weactstudio_8mb.h create mode 100644 ports/rp2/boards/WEACTSTUDIO/weactstudio_common.h diff --git a/ports/rp2/boards/WEACTSTUDIO/README.md b/ports/rp2/boards/WEACTSTUDIO/README.md new file mode 100644 index 0000000000000..ae0acfd2c7b0f --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/README.md @@ -0,0 +1,34 @@ +# WeAct Studio RP2040 + +The WeAct Studio RP2040 Board is based on the Raspberry Pi RP2040 and can be +purchased with 2/4/8/16 MiB of flash. + +These boards are available from a number of resellers and often have the name +"Pico Board RP2040". WeAct maintain the [WeActStudio.RP2040CoreBoard](https://github.com/WeActTC/WeActStudio.RP2040CoreBoard/tree/master) +repository containing information on the board. + +## Build notes + +Builds can be configured with the `BOARD_VARIANT` parameter. Valid variants +can be displayed with the `query-variant` target. An example: + +```bash +> cd ports/rp2 +> make BOARD=WEACTSTUDIO query-variants +VARIANTS: flash_2mb flash_4mb flash_8mb flash_16mb +> make BOARD=WEACTSTUDIO BOARD_VARIANT=flash_8mb submodules all # Build the 8 MiB variant +``` + +`flash_16mb` is the default if `BOARD_VARIANT` is not supplied. + +## Board-specific modules + +The `board` module contains definitions for the onboard LED and user button. + +Example: + +```python +> import board +> board.led.toggle() # Toggles the state of the on-board LED +> board.key.value() # Returns 0 or 1 corresponding to the state of the user key +``` diff --git a/ports/rp2/boards/WEACTSTUDIO/board.json b/ports/rp2/boards/WEACTSTUDIO/board.json new file mode 100644 index 0000000000000..bac5263627ba2 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/board.json @@ -0,0 +1,23 @@ +{ + "deploy": [ + "deploy.md" + ], + "docs": "", + "features": [ + "Breadboard Friendly", + "SPI Flash", + "USB-C" + ], + "images": [ + "weact_rp2040.jpg" + ], + "mcu": "rp2040", + "product": "WeAct Studio RP2040", + "url": "https://github.com/WeActTC/WeActStudio.RP2040CoreBoard", + "variants": { + "flash_2mb": "2 MiB Flash", + "flash_4mb": "4 MiB Flash", + "flash_8mb": "8 MiB Flash" + }, + "vendor": "WeAct" +} diff --git a/ports/rp2/boards/WEACTSTUDIO/deploy.md b/ports/rp2/boards/WEACTSTUDIO/deploy.md new file mode 100644 index 0000000000000..b4db7675ebb5f --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/deploy.md @@ -0,0 +1,8 @@ +### Flashing via UF2 bootloader + +To get the board in bootloader mode ready for the firmware update, execute +`machine.bootloader()` at the MicroPython REPL. Alternatively, hold +down the BOOTSEL button while pressing reset (NRST). The uf2 file below +should then be copied to the USB mass storage device that appears. Once +programming of the new firmware is complete the device will automatically reset +and be ready for use. diff --git a/ports/rp2/boards/WEACTSTUDIO/manifest.py b/ports/rp2/boards/WEACTSTUDIO/manifest.py new file mode 100644 index 0000000000000..f993d4fa6bd29 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/manifest.py @@ -0,0 +1,2 @@ +include("$(PORT_DIR)/boards/manifest.py") +freeze("./modules") diff --git a/ports/rp2/boards/WEACTSTUDIO/modules/board.py b/ports/rp2/boards/WEACTSTUDIO/modules/board.py new file mode 100644 index 0000000000000..dd3b31b805fd5 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/modules/board.py @@ -0,0 +1,4 @@ +from machine import Pin + +led = Pin(25, Pin.OUT, value=0) +key = Pin(23, Pin.IN, Pin.PULL_UP) diff --git a/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.cmake b/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.cmake new file mode 100644 index 0000000000000..5fd3eec897b36 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.cmake @@ -0,0 +1,26 @@ +# CMake file for WeAct Studio RP2040 boards + +# The WeAct Studio boards don't have official pico-sdk support so we define it +# See also: https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards +list(APPEND PICO_BOARD_HEADER_DIRS ${MICROPY_BOARD_DIR}) + +# Freeze board.py +set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) + +# Provide different variants for the downloads page +set(BOARD_VARIANTS "flash_2mb flash_4mb flash_8mb flash_16mb") + +# Select the 16MB variant as the default +set(PICO_BOARD "weactstudio_16mb") + +if("${BOARD_VARIANT}" STREQUAL "flash_2mb") + set(PICO_BOARD "weactstudio_2mb") +endif() + +if("${BOARD_VARIANT}" STREQUAL "flash_4mb") +set(PICO_BOARD "weactstudio_4mb") +endif() + +if("${BOARD_VARIANT}" STREQUAL "flash_8mb") +set(PICO_BOARD "weactstudio_8mb") +endif() diff --git a/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.h b/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.h new file mode 100644 index 0000000000000..3c72ef4fca0b3 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/mpconfigboard.h @@ -0,0 +1,4 @@ +#define MICROPY_HW_BOARD_NAME "WeAct Studio RP2040" + +// Allow 1MB for the firmware image itself, allocate the remainder to the filesystem +#define MICROPY_HW_FLASH_STORAGE_BYTES (PICO_FLASH_SIZE_BYTES - (1 * 1024 * 1024)) diff --git a/ports/rp2/boards/WEACTSTUDIO/weactstudio_16mb.h b/ports/rp2/boards/WEACTSTUDIO/weactstudio_16mb.h new file mode 100644 index 0000000000000..b6208c68972c4 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/weactstudio_16mb.h @@ -0,0 +1,18 @@ +// A pico-sdk board definition is required since the WeAct Studio boards are +// not officially supported. +// +// Officially supported boards: +// https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards + +#ifndef _BOARDS_WEACTSTUDIO_16MB_H +#define _BOARDS_WEACTSTUDIO_16MB_H + +#include "weactstudio_common.h" + +#define WEACTSTUDIO_16MB + +#ifndef PICO_FLASH_SIZE_BYTES +#define PICO_FLASH_SIZE_BYTES (16 * 1024 * 1024) +#endif + +#endif diff --git a/ports/rp2/boards/WEACTSTUDIO/weactstudio_2mb.h b/ports/rp2/boards/WEACTSTUDIO/weactstudio_2mb.h new file mode 100644 index 0000000000000..e6a21ea3bc3fa --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/weactstudio_2mb.h @@ -0,0 +1,18 @@ +// A pico-sdk board definition is required since the WeAct Studio boards are +// not officially supported. +// +// Officially supported boards: +// https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards + +#ifndef _BOARDS_WEACTSTUDIO_2MB_H +#define _BOARDS_WEACTSTUDIO_2MB_H + +#include "weactstudio_common.h" + +#define WEACTSTUDIO_2MB + +#ifndef PICO_FLASH_SIZE_BYTES +#define PICO_FLASH_SIZE_BYTES (2 * 1024 * 1024) +#endif + +#endif diff --git a/ports/rp2/boards/WEACTSTUDIO/weactstudio_4mb.h b/ports/rp2/boards/WEACTSTUDIO/weactstudio_4mb.h new file mode 100644 index 0000000000000..7a2836ab4a3fe --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/weactstudio_4mb.h @@ -0,0 +1,18 @@ +// A pico-sdk board definition is required since the WeAct Studio boards are +// not officially supported. +// +// Officially supported boards: +// https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards + +#ifndef _BOARDS_WEACTSTUDIO_4MB_H +#define _BOARDS_WEACTSTUDIO_4MB_H + +#include "weactstudio_common.h" + +#define WEACTSTUDIO_4MB + +#ifndef PICO_FLASH_SIZE_BYTES +#define PICO_FLASH_SIZE_BYTES (4 * 1024 * 1024) +#endif + +#endif diff --git a/ports/rp2/boards/WEACTSTUDIO/weactstudio_8mb.h b/ports/rp2/boards/WEACTSTUDIO/weactstudio_8mb.h new file mode 100644 index 0000000000000..82baf6688e837 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/weactstudio_8mb.h @@ -0,0 +1,18 @@ +// A pico-sdk board definition is required since the WeAct Studio boards are +// not officially supported. +// +// Officially supported boards: +// https://github.com/raspberrypi/pico-sdk/tree/master/src/boards/include/boards + +#ifndef _BOARDS_WEACTSTUDIO_8MB_H +#define _BOARDS_WEACTSTUDIO_8MB_H + +#include "weactstudio_common.h" + +#define WEACTSTUDIO_8MB + +#ifndef PICO_FLASH_SIZE_BYTES +#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) +#endif + +#endif diff --git a/ports/rp2/boards/WEACTSTUDIO/weactstudio_common.h b/ports/rp2/boards/WEACTSTUDIO/weactstudio_common.h new file mode 100644 index 0000000000000..f1c6903601330 --- /dev/null +++ b/ports/rp2/boards/WEACTSTUDIO/weactstudio_common.h @@ -0,0 +1,62 @@ +// Common configuration to all WeAct Studio boards (only flash size differs) + +#ifndef _BOARDS_WEACTSTUDIO_COMMON_H +#define _BOARDS_WEACTSTUDIO_COMMON_H + +// --- UART --- +#ifndef PICO_DEFAULT_UART +#define PICO_DEFAULT_UART 0 +#endif +#ifndef PICO_DEFAULT_UART_TX_PIN +#define PICO_DEFAULT_UART_TX_PIN 0 +#endif +#ifndef PICO_DEFAULT_UART_RX_PIN +#define PICO_DEFAULT_UART_RX_PIN 1 +#endif + +// --- LED --- +#ifndef PICO_DEFAULT_LED_PIN +#define PICO_DEFAULT_LED_PIN 25 +#endif + +// --- I2C --- +#ifndef PICO_DEFAULT_I2C +#define PICO_DEFAULT_I2C 0 +#endif +#ifndef PICO_DEFAULT_I2C_SDA_PIN +#define PICO_DEFAULT_I2C_SDA_PIN 4 +#endif +#ifndef PICO_DEFAULT_I2C_SCL_PIN +#define PICO_DEFAULT_I2C_SCL_PIN 5 +#endif + +// --- SPI --- +#ifndef PICO_DEFAULT_SPI +#define PICO_DEFAULT_SPI 0 +#endif +#ifndef PICO_DEFAULT_SPI_SCK_PIN +#define PICO_DEFAULT_SPI_SCK_PIN 18 +#endif +#ifndef PICO_DEFAULT_SPI_TX_PIN +#define PICO_DEFAULT_SPI_TX_PIN 19 +#endif +#ifndef PICO_DEFAULT_SPI_RX_PIN +#define PICO_DEFAULT_SPI_RX_PIN 16 +#endif +#ifndef PICO_DEFAULT_SPI_CSN_PIN +#define PICO_DEFAULT_SPI_CSN_PIN 17 +#endif + +// --- FLASH --- +#define PICO_BOOT_STAGE2_CHOOSE_W25Q080 1 + +#ifndef PICO_FLASH_SPI_CLKDIV +#define PICO_FLASH_SPI_CLKDIV 2 +#endif + +// All boards have B1 RP2040 +#ifndef PICO_RP2040_B0_SUPPORTED +#define PICO_RP2040_B0_SUPPORTED 0 +#endif + +#endif From 71050870b81a8dbf3c3fac93bd2d86f0ff9a37d2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Sep 2022 01:04:05 +1000 Subject: [PATCH 2652/3533] mpy-cross/mpy_cross: Fix default path to mpy-cross binary. Needed to be updated to use build/mpy-cross. Also fixes some other issues in the Python wrapper: - Rename find_mpy_cross_binary to _find_mpy_cross_binary - Fix passing of -march arg. - Decode stdout from subprocess. - Print stdout from mpy-cross in __main__.py. Signed-off-by: Jim Mussared --- mpy-cross/mpy_cross/__init__.py | 12 +++++++----- mpy-cross/mpy_cross/__main__.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/mpy-cross/mpy_cross/__init__.py b/mpy-cross/mpy_cross/__init__.py index d4c0930bb9198..235271d2ce172 100644 --- a/mpy-cross/mpy_cross/__init__.py +++ b/mpy-cross/mpy_cross/__init__.py @@ -62,9 +62,11 @@ class CrossCompileError(Exception): def find_mpy_cross_binary(mpy_cross): + +def _find_mpy_cross_binary(mpy_cross): if mpy_cross: return mpy_cross - return os.path.abspath(os.path.join(os.path.dirname(__file__), "../mpy-cross")) + return os.path.abspath(os.path.join(os.path.dirname(__file__), "../build/mpy-cross")) def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, extra_args=None): @@ -82,7 +84,7 @@ def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, args += ["-o", dest] if march: - args += ["-march", march] + args += ["-march=" + march] if opt is not None: args += ["-O{}".format(opt)] @@ -96,7 +98,7 @@ def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, def run(args, mpy_cross=None): - mpy_cross = find_mpy_cross_binary(mpy_cross) + mpy_cross = _find_mpy_cross_binary(mpy_cross) if not os.path.exists(mpy_cross): raise CrossCompileError("mpy-cross binary not found at {}.".format(mpy_cross)) @@ -108,6 +110,6 @@ def run(args, mpy_cross=None): pass try: - subprocess.check_output([mpy_cross] + args, stderr=subprocess.STDOUT) + return subprocess.check_output([mpy_cross] + args, stderr=subprocess.STDOUT).decode() except subprocess.CalledProcessError as er: - raise CrossCompileError(er.output) + raise CrossCompileError(er.output.decode()) diff --git a/mpy-cross/mpy_cross/__main__.py b/mpy-cross/mpy_cross/__main__.py index 9d957bca025d6..2b6b81c333362 100644 --- a/mpy-cross/mpy_cross/__main__.py +++ b/mpy-cross/mpy_cross/__main__.py @@ -32,7 +32,7 @@ from . import run, CrossCompileError try: - run(sys.argv[1:]) + print(run(sys.argv[1:])) except CrossCompileError as er: print(er.args[0], file=sys.stderr) raise SystemExit(1) From ee1b4a2026e79b662142f0d985643dadf7fff87b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Sep 2022 01:05:09 +1000 Subject: [PATCH 2653/3533] mpy-cross/mpy_cross: Add a way to query the mpy version. This returns the mpy version and sub-version for files compiled with this mpy-cross binary. Signed-off-by: Jim Mussared --- mpy-cross/mpy_cross/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/mpy-cross/mpy_cross/__init__.py b/mpy-cross/mpy_cross/__init__.py index 235271d2ce172..01fe550a93cef 100644 --- a/mpy-cross/mpy_cross/__init__.py +++ b/mpy-cross/mpy_cross/__init__.py @@ -27,6 +27,7 @@ from __future__ import print_function import os +import re import stat import subprocess @@ -54,14 +55,15 @@ NATIVE_ARCH_XTENSAWIN, ] -__all__ = ["compile", "run", "CrossCompileError"] +__all__ = ["version", "compile", "run", "CrossCompileError"] class CrossCompileError(Exception): pass -def find_mpy_cross_binary(mpy_cross): +_VERSION_RE = re.compile("mpy-cross emitting mpy v([0-9]+)(?:.([0-9]+))?") + def _find_mpy_cross_binary(mpy_cross): if mpy_cross: @@ -69,6 +71,16 @@ def _find_mpy_cross_binary(mpy_cross): return os.path.abspath(os.path.join(os.path.dirname(__file__), "../build/mpy-cross")) +def mpy_version(mpy_cross=None): + version_info = run(["--version"], mpy_cross=mpy_cross) + match = re.search(_VERSION_RE, version_info) + mpy_version, mpy_sub_version = int(match.group(1)), int(match.group(2) or "0") + return ( + mpy_version, + mpy_sub_version, + ) + + def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, extra_args=None): if not src: raise ValueError("src is required") From 370a87dd12b92722ba8fac5f3562384d31a864ce Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Sep 2022 11:33:24 +1000 Subject: [PATCH 2654/3533] mpy-cross/mpy_cross: Add list of architectures to `__all__`. Signed-off-by: Jim Mussared --- mpy-cross/mpy_cross/__init__.py | 42 +++++++++++++-------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/mpy-cross/mpy_cross/__init__.py b/mpy-cross/mpy_cross/__init__.py index 01fe550a93cef..22d175c89cf3c 100644 --- a/mpy-cross/mpy_cross/__init__.py +++ b/mpy-cross/mpy_cross/__init__.py @@ -31,31 +31,23 @@ import stat import subprocess -NATIVE_ARCH_X86 = "x86" -NATIVE_ARCH_X64 = "x64" -NATIVE_ARCH_ARMV6 = "armv6" -NATIVE_ARCH_ARMV6M = "armv6m" -NATIVE_ARCH_ARMV7M = "armv7m" -NATIVE_ARCH_ARMV7EM = "armv7em" -NATIVE_ARCH_ARMV7EMSP = "armv7emsp" -NATIVE_ARCH_ARMV7EMDP = "armv7emdp" -NATIVE_ARCH_XTENSA = "xtensa" -NATIVE_ARCH_XTENSAWIN = "xtensawin" - -NATIVE_ARCHS = [ - NATIVE_ARCH_X86, - NATIVE_ARCH_X64, - NATIVE_ARCH_ARMV6, - NATIVE_ARCH_ARMV6M, - NATIVE_ARCH_ARMV7M, - NATIVE_ARCH_ARMV7EM, - NATIVE_ARCH_ARMV7EMSP, - NATIVE_ARCH_ARMV7EMDP, - NATIVE_ARCH_XTENSA, - NATIVE_ARCH_XTENSAWIN, -] - -__all__ = ["version", "compile", "run", "CrossCompileError"] +NATIVE_ARCHS = { + "NATIVE_ARCH_NONE": "", + "NATIVE_ARCH_X86": "x86", + "NATIVE_ARCH_X64": "x64", + "NATIVE_ARCH_ARMV6": "armv6", + "NATIVE_ARCH_ARMV6M": "armv6m", + "NATIVE_ARCH_ARMV7M": "armv7m", + "NATIVE_ARCH_ARMV7EM": "armv7em", + "NATIVE_ARCH_ARMV7EMSP": "armv7emsp", + "NATIVE_ARCH_ARMV7EMDP": "armv7emdp", + "NATIVE_ARCH_XTENSA": "xtensa", + "NATIVE_ARCH_XTENSAWIN": "xtensawin", +} + +globals().update(NATIVE_ARCHS) + +__all__ = ["version", "compile", "run", "CrossCompileError"] + list(NATIVE_ARCHS.keys()) class CrossCompileError(Exception): From e4d90be680b0a8933daf1bad03a1fa2632e0136e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 28 Sep 2022 11:46:45 +1000 Subject: [PATCH 2655/3533] mpy-cross/mpy_cross: Add docstrings to public methods. --- mpy-cross/mpy_cross/__init__.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/mpy-cross/mpy_cross/__init__.py b/mpy-cross/mpy_cross/__init__.py index 22d175c89cf3c..8eadbc8352c74 100644 --- a/mpy-cross/mpy_cross/__init__.py +++ b/mpy-cross/mpy_cross/__init__.py @@ -64,6 +64,13 @@ def _find_mpy_cross_binary(mpy_cross): def mpy_version(mpy_cross=None): + """ + Get the version and sub-version of the .mpy file format generated by this version of mpy-cross. + + Returns: A tuple of `(mpy_version, mpy_sub_version)` + Optional keyword arguments: + - mpy_cross: Specific mpy-cross binary to use + """ version_info = run(["--version"], mpy_cross=mpy_cross) match = re.search(_VERSION_RE, version_info) mpy_version, mpy_sub_version = int(match.group(1)), int(match.group(2) or "0") @@ -74,6 +81,22 @@ def mpy_version(mpy_cross=None): def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, extra_args=None): + """ + Compile the specified .py file with mpy-cross. + + Returns: Standard output from mpy-cross as a string. + + Required arguments: + - src: The path to the .py file + + Optional keyword arguments: + - dest: The output .mpy file. Defaults to `src` (with .mpy extension) + - src_path: The path to embed in the .mpy file (defaults to `src`) + - opt: Optimisation level (0-3, default 0) + - march: One of the `NATIVE_ARCH_*` constants (defaults to NATIVE_ARCH_NONE) + - mpy_cross: Specific mpy-cross binary to use + - extra_args: Additional arguments to pass to mpy-cross (e.g. `["-X", "emit=native"]`) + """ if not src: raise ValueError("src is required") if not os.path.exists(src): @@ -102,6 +125,15 @@ def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, def run(args, mpy_cross=None): + """ + Run mpy-cross with the specified command line arguments. + Prefer to use `compile()` instead. + + Returns: Standard output from mpy-cross as a string. + + Optional keyword arguments: + - mpy_cross: Specific mpy-cross binary to use + """ mpy_cross = _find_mpy_cross_binary(mpy_cross) if not os.path.exists(mpy_cross): From fecfbc3f678c8f2f6c450abf840bbefca48a57d0 Mon Sep 17 00:00:00 2001 From: stijn Date: Thu, 29 Sep 2022 14:17:37 +0200 Subject: [PATCH 2656/3533] py/mkenv.mk: Make CPP definition explicit for consistency. --- py/mkenv.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/py/mkenv.mk b/py/mkenv.mk index ea2e34f3b64de..5368279e2908f 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -45,6 +45,7 @@ PYTHON = python3 AS = $(CROSS_COMPILE)as CC = $(CROSS_COMPILE)gcc +CPP = $(CC) -E CXX = $(CROSS_COMPILE)g++ GDB = $(CROSS_COMPILE)gdb LD = $(CROSS_COMPILE)ld From b76ddcbc83b7235ab373e764ae033c1e5cecddd1 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Sep 2022 22:31:35 +1000 Subject: [PATCH 2657/3533] docs/Makefile: Enable parallel compilation for Sphinx. This has a fairly dramatic (nearly 3x on a 6-core machine) speedup for docs compilation, with no impact on correctness. Signed-off-by: Jim Mussared --- docs/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Makefile b/docs/Makefile index 05709617c35b9..766a669a500dc 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -3,7 +3,7 @@ # You can set these variables from the command line. PYTHON = python3 -SPHINXOPTS = -W --keep-going +SPHINXOPTS = -W --keep-going -j auto SPHINXBUILD = sphinx-build PAPER = BUILDDIR = build/$(MICROPY_PORT) From 65ab0ec91cffe5d39ff2e253ac5cc898ecc3c5ce Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Sep 2022 23:13:52 +1000 Subject: [PATCH 2658/3533] tools/manifestfile.py: Add `author` kwarg to metadata(). This allows future micropython-lib packages to specify an author. Signed-off-by: Jim Mussared --- tools/manifestfile.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/manifestfile.py b/tools/manifestfile.py index a4d056137ffc9..f7966ccb8045d 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -87,14 +87,17 @@ def __init__(self): self.version = None self.description = None self.license = None + self.author = None - def update(self, description=None, version=None, license=None): + def update(self, description=None, version=None, license=None, author=None): if description: self.description = description if version: self.version = version if license: self.license = version + if author: + self.author = author # Turns a dict of options into a object with attributes used to turn the @@ -228,7 +231,7 @@ def _search(self, base_path, package_path, files, exts, kind, opt=None, strict=F if base_path: os.chdir(prev_cwd) - def metadata(self, description=None, version=None, license=None): + def metadata(self, description=None, version=None, license=None, author=None): """ From within a manifest file, use this to set the metadata for the package described by current manifest. @@ -237,7 +240,7 @@ def metadata(self, description=None, version=None, license=None): to obtain the metadata for the top-level manifest file. """ - self._metadata[-1].update(description, version, license) + self._metadata[-1].update(description, version, license, author) return self._metadata[-1] def include(self, manifest_path, top_level=False, **kwargs): From ba3652f15d96d9dca0f84522639ea2005b07fcb4 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 30 Sep 2022 00:46:16 +1000 Subject: [PATCH 2659/3533] lib/micropython-lib: Update submodule to latest. This brings in the `mip` tool for installing packages. Signed-off-by: Jim Mussared --- lib/micropython-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/micropython-lib b/lib/micropython-lib index 58f8bec54d5b3..d0f97fc218f07 160000 --- a/lib/micropython-lib +++ b/lib/micropython-lib @@ -1 +1 @@ -Subproject commit 58f8bec54d5b3b959247b73a6e8f28e8493bd30b +Subproject commit d0f97fc218f07c381c835d9f632904c1ae1c9d6b From 924a3e03ec167c4417d89b531794c75ce5a631a3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Sep 2022 17:49:58 +1000 Subject: [PATCH 2660/3533] top: Replace upip with mip everywhere. Updates all README.md and docs, and manifests to `require("mip")`. Also extend and improve the documentation on freezing and packaging. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- docs/develop/gettingstarted.rst | 3 +- docs/develop/optimizations.rst | 2 + docs/esp8266/tutorial/intro.rst | 2 +- docs/pyboard/tutorial/lcd160cr_skin.rst | 2 +- docs/reference/glossary.rst | 30 +- docs/reference/manifest.rst | 271 ++++++++++---- docs/reference/packages.rst | 463 ++++++++---------------- examples/hwapi/README.md | 2 +- ports/esp32/boards/manifest.py | 3 +- ports/esp8266/README.md | 18 +- ports/esp8266/boards/manifest.py | 7 +- ports/rp2/boards/PICO_W/manifest.py | 4 +- ports/unix/README.md | 26 +- ports/unix/variants/manifest.py | 3 +- tools/upip.py | 351 ------------------ tools/upip_utarfile.py | 95 ----- 16 files changed, 404 insertions(+), 878 deletions(-) delete mode 100644 tools/upip.py delete mode 100644 tools/upip_utarfile.py diff --git a/docs/develop/gettingstarted.rst b/docs/develop/gettingstarted.rst index 000b7d61390cb..c2d3816d425a4 100644 --- a/docs/develop/gettingstarted.rst +++ b/docs/develop/gettingstarted.rst @@ -322,7 +322,8 @@ tests tools - Contains helper tools including the ``upip`` and the ``pyboard.py`` module. + Contains scripts used by the build and CI process, as well as user tools such + as ``pyboard.py`` and ``mpremote``. examples diff --git a/docs/develop/optimizations.rst b/docs/develop/optimizations.rst index d972cde66616a..7f2c8cbe7282c 100644 --- a/docs/develop/optimizations.rst +++ b/docs/develop/optimizations.rst @@ -25,6 +25,8 @@ into the firmware image as part of the main firmware compilation process, which the bytecode will be executed from ROM. This can lead to a significant memory saving, and reduce heap fragmentation. +See :ref:`manifest` for more information. + Variables --------- diff --git a/docs/esp8266/tutorial/intro.rst b/docs/esp8266/tutorial/intro.rst index ac46e68b5a5cc..75739bd6f96fd 100644 --- a/docs/esp8266/tutorial/intro.rst +++ b/docs/esp8266/tutorial/intro.rst @@ -23,7 +23,7 @@ convertor to make the UART available to your PC. The minimum requirement for flash size is 1Mbyte. There is also a special build for boards with 512KB, but it is highly limited comparing to the normal build: there is no support for filesystem, and thus features which -depend on it won't work (WebREPL, upip, etc.). As such, 512KB build will +depend on it won't work (WebREPL, mip, etc.). As such, 512KB build will be more interesting for users who build from source and fine-tune parameters for their particular application. diff --git a/docs/pyboard/tutorial/lcd160cr_skin.rst b/docs/pyboard/tutorial/lcd160cr_skin.rst index fa0debcb1ce3f..a0fe88a2e692c 100644 --- a/docs/pyboard/tutorial/lcd160cr_skin.rst +++ b/docs/pyboard/tutorial/lcd160cr_skin.rst @@ -42,7 +42,7 @@ There is a test program which you can use to test the features of the display, and which also serves as a basis to start creating your own code that uses the LCD. This test program is available on GitHub `here `__. -Copy it to the board over USB mass storage, or by using `mpremote`. +Copy it to the board over USB mass storage, or by using :ref:`mpremote`. To run the test from the MicroPython prompt do:: diff --git a/docs/reference/glossary.rst b/docs/reference/glossary.rst index da951189e2045..4c66f70319ba4 100644 --- a/docs/reference/glossary.rst +++ b/docs/reference/glossary.rst @@ -52,7 +52,7 @@ Glossary cross-compiler Also known as ``mpy-cross``. This tool runs on your PC and converts a :term:`.py file` containing MicroPython code into a :term:`.mpy file` - containing MicroPython bytecode. This means it loads faster (the board + containing MicroPython :term:`bytecode`. This means it loads faster (the board doesn't have to compile the code), and uses less space on flash (the bytecode is more space efficient). @@ -128,7 +128,7 @@ Glossary Unlike the :term:`CPython` stdlib, micropython-lib modules are intended to be installed individually - either using manual copying or - using :term:`upip`. + using :term:`mip`. MicroPython port MicroPython supports different :term:`boards `, RTOSes, and @@ -151,16 +151,26 @@ Glossary machine-independent features. It can also function in a similar way to :term:`CPython`'s ``python`` executable. + mip + A package installer for MicroPython (mip - "mip installs packages"). It + installs MicroPython packages either from :term:`micropython-lib`, + GitHub, or arbitrary URLs. mip can be used on-device on + network-capable boards, and internally by tools such + as :term:`mpremote`. + + mpremote + A tool for interacting with a MicroPython device. See :ref:`mpremote`. + .mpy file The output of the :term:`cross-compiler`. A compiled form of a - :term:`.py file` that contains MicroPython bytecode instead of Python - source code. + :term:`.py file` that contains MicroPython :term:`bytecode` instead of + Python source code. native Usually refers to "native code", i.e. machine code for the target microcontroller (such as ARM Thumb, Xtensa, x86/x64). The ``@native`` decorator can be applied to a MicroPython function to generate native - code instead of bytecode for that function, which will likely be + code instead of :term:`bytecode` for that function, which will likely be faster but use more RAM. port @@ -193,8 +203,10 @@ Glossary as a serial port over USB. upip - (Literally, "micro pip"). A package manager for MicroPython, inspired + A now-obsolete package manager for MicroPython, inspired by :term:`CPython`'s pip, but much smaller and with reduced - functionality. - upip runs both on the :term:`Unix port ` and on - :term:`baremetal` ports which offer filesystem and networking support. + functionality. See its replacement, :term:`mip`. + + webrepl + A way of connecting to the REPL (and transferring files) on a device + over the internet from a browser. See https://micropython.org/webrepl diff --git a/docs/reference/manifest.rst b/docs/reference/manifest.rst index b756de47ed032..9bcafd5839cab 100644 --- a/docs/reference/manifest.rst +++ b/docs/reference/manifest.rst @@ -1,35 +1,177 @@ +.. _manifest: + MicroPython manifest files ========================== -When building firmware for a device the following components are included in -the compilation process: +Summary +------- + +MicroPython has a feature that allows Python code to be "frozen" into the +firmware, as an alternative to loading code from the filesystem. + +This has the following benefits: + +- the code is pre-compiled to bytecode, avoiding the need for the Python + source to be compiled at load-time. +- the bytecode can be executed directly from ROM (i.e. flash memory) rather than + being copied into RAM. Similarly any constant objects (strings, tuples, etc) + are loaded from ROM also. This can lead to significantly more memory being + available for your application. +- on devices that do not have a filesystem, this is the only way to + load Python code. + +During development, freezing is generally not recommended as it will +significantly slow down your development cycle, as each update will require +re-flashing the entire firmware. However, it can still be useful to +selectively freeze some rarely-changing dependencies (such as third-party +libraries). + +The way to list the Python files to be be frozen into the firmware is via +a "manifest", which is a Python file that will be interpreted by the build +process. Typically you would write a manifest file as part of a board +definition, but you can also write a stand-alone manifest file and use it with +an existing board definition. + +Manifest files can define dependencies on libraries from :term:`micropython-lib` +as well as Python files on the filesystem, and also on other manifest files. + +Writing manifest files +---------------------- + +A manifest file is a Python file containing a series of function calls. See the +available functions defined below. + +Any paths used in manifest files can include the following variables. These all +resolve to absolute paths. + +- ``$(MPY_DIR)`` -- path to the micropython repo. +- ``$(MPY_LIB_DIR)`` -- path to the micropython-lib submodule. Prefer to use + ``require()``. +- ``$(PORT_DIR)`` -- path to the current port (e.g. ``ports/stm32``) +- ``$(BOARD_DIR)`` -- path to the current board + (e.g. ``ports/stm32/boards/PYBV11``) + +Custom manifest files should not live in the main MicroPython repository. You +should keep them in version control with the rest of your project. + +Typically a manifest used for compiling firmware will need to include the port +manifest, which might include frozen modules that are required for the board to +function. If you just want to add additional modules to an existing board, then +include the board manifest (which will in turn include the port manifest). + +Building with a custom manifest +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Your manifest can be specified on the ``make`` command line with: + +.. code-block:: bash + + $ make BOARD=MYBOARD FROZEN_MANIFEST=/path/to/my/project/manifest.py + +This applies to all ports, including CMake-based ones (e.g. esp32, rp2), as the +Makefile wrapper that will pass this into the CMake build. + +Adding a manifest to a board definition +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you have a custom board definition, you can make it include your custom +manifest automatically. On make-based ports (most ports), in your +``mpconfigboard.mk`` set the ``FROZEN_MANIFEST`` variable. + +.. code-block:: makefile + + FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py + +On CMake-based ports (e.g. esp32, rp2), instead use ``mpconfigboard.cmake`` + +.. code-block:: cmake + + set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) + +High-level functions +~~~~~~~~~~~~~~~~~~~~ + +Note: The ``opt`` keyword argument can be set on the various functions, this controls +the optimisation level used by the cross-compiler. +See :func:`micropython.opt_level`. + +.. function:: package(package_path, files=None, base_path=".", opt=None) + + This is equivalent to copying the "package_path" directory to the device + (except as frozen code). + + In the simplest case, to freeze a package "foo" in the current directory: + + .. code-block:: python3 + + package("foo") + + will recursively include all .py files in foo, and will be frozen as + ``foo/**/*.py``. + + If the package isn't in the same directory as the manifest file, use ``base_path``: + + .. code-block:: python3 + + package("foo", base_path="path/to/libraries") + + You can use the variables above, such as ``$(PORT_DIR)`` in ``base_path``. + + To restrict to certain files in the package use ``files`` (note: paths + should be relative to the package): ``package("foo", files=["bar/baz.py"])``. + +.. function:: module(module_path, base_path=".", opt=None) + + Include a single Python file as a module. + + If the file is in the current directory: + + .. code-block:: python3 + + module("foo.py") + + Otherwise use base_path to locate the file: + + .. code-block:: python3 + + module("foo.py", base_path="src/drivers") + + You can use the variables above, such as ``$(PORT_DIR)`` in ``base_path``. + +.. function:: require(name, unix_ffi=False) + + Require a package by name (and its dependencies) from :term:`micropython-lib`. + + Optionally specify unix_ffi=True to use a module from the unix-ffi directory. + +.. function:: include(manifest_path) -- the core MicroPython virtual machine and runtime -- port-specific system code and drivers to interface with the - microcontroller/device that the firmware is targeting -- standard built-in modules, like ``sys`` -- extended built-in modules, like ``json`` and ``machine`` -- extra modules written in C/C++ -- extra modules written in Python + Include another manifest. + + Typically a manifest used for compiling firmware will need to include the + port manifest, which might include frozen modules that are required for + the board to function. + + The *manifest* argument can be a string (filename) or an iterable of + strings. -All the modules included in the firmware are available via ``import`` from -Python code. The extra modules written in Python that are included in a build -(the last point above) are called *frozen modules*, and are specified by a -``manifest.py`` file. Changing this manifest requires rebuilding the firmware. + Relative paths are resolved with respect to the current manifest file. -It's also possible to add additional modules to the filesystem of the device -once it is up and running. Adding and removing modules to/from the filesystem -does not require rebuilding the firmware so is a simpler process than rebuilding -firmware. The benefit of using a manifest is that frozen modules are more -efficient: they are faster to import and take up less RAM once imported. + If the path is to a directory, then it implicitly includes the + manifest.py file inside that directory. -MicroPython manifest files are Python files and can contain arbitrary Python -code. There are also a set of commands (predefined functions) which are used -to specify the Python source files to include. These commands are described -below. + You can use the variables above, such as ``$(PORT_DIR)`` in ``manifest_path``. -Freezing source code --------------------- +.. function:: metadata(description=None, version=None, license=None, author=None) + + Define metadata for this manifest file. This is useful for manifests for + micropython-lib packages. + +Low-level functions +~~~~~~~~~~~~~~~~~~~ + +These functions are documented for completeness, but with the exception of +``freeze_as_str`` all functionality can be accessed via the high-level functions. .. function:: freeze(path, script=None, opt=0) @@ -42,9 +184,7 @@ Freezing source code module will start after *path*, i.e. *path* is excluded from the module name. - If *path* is relative, it is resolved to the current ``manifest.py``. Use - ``$(MPY_DIR)``, ``$(MPY_LIB_DIR)``, ``$(PORT_DIR)``, ``$(BOARD_DIR)`` if you - need to access specific paths. + If *path* is relative, it is resolved to the current ``manifest.py``. If *script* is None, all files in *path* will be frozen. @@ -75,71 +215,48 @@ Freezing source code Freeze the input, which must be ``.mpy`` files that are frozen directly. See ``freeze()`` for further details on the arguments. - -Including other manifest files ------------------------------- - -.. function:: include(manifest, **kwargs) - - Include another manifest. - - The *manifest* argument can be a string (filename) or an iterable of - strings. - - Relative paths are resolved with respect to the current manifest file. - - Optional *kwargs* can be provided which will be available to the included - script via the *options* variable. - - For example: - - .. code-block:: python3 - - include("path.py", extra_features=True) - - then in path.py: - - .. code-block:: python3 - - options.defaults(standard_features=True) - # freeze minimal modules. - if options.standard_features: - # freeze standard modules. - if options.extra_features: - # freeze extra modules. - - Examples -------- -To freeze a single file which is available as ``import mydriver``, use: +To freeze a single file from the current directory which will be available as +``import mydriver``, use: .. code-block:: python3 - freeze(".", "mydriver.py") + module("mydriver.py") -To freeze a set of files which are available as ``import test1`` and -``import test2``, and which are compiled with optimisation level 3, use: +To freeze a directory of files in a subdirectory "mydriver" of the current +directory which will be available as ``import mydriver``, use: .. code-block:: python3 - freeze("/path/to/tests", ("test1.py", "test2.py"), opt=3) + package("mydriver") -To freeze a module which can be imported as ``import mymodule``, use: +To freeze the "hmac" library from :term:`micropython-lib`, use: .. code-block:: python3 - freeze( - "../relative/path", - ( - "mymodule/__init__.py", - "mymodule/core.py", - "mymodule/extra.py", - ), - ) + require("hmac") -To include a manifest from the MicroPython repository, use: +A more complete example of a custom ``manifest.py`` file for the ``PYBD_SF2`` +board is: .. code-block:: python3 - include("$(MPY_DIR)/extmod/uasyncio/manifest.py") + # Include the board's default manifest. + include("$(BOARD_DIR)/manifest.py") + # Add a custom driver + module("mydriver.py") + # Add aiorepl from micropython-lib + require("aiorepl") + +Then the board can be compiled with + +.. code-block:: bash + + $ cd ports/stm32 + $ make BOARD=PYBD_SF2 FROZEN_MANIFEST=~/src/myproject/manifest.py + +Note that most boards do not have their own ``manifest.py``, rather they use the +port one directly, in which case your manifest should just +``include("$(PORT_DIR)/boards/manifest.py")`` instead. diff --git a/docs/reference/packages.rst b/docs/reference/packages.rst index eb44992ed6324..0c049d1fb2fc2 100644 --- a/docs/reference/packages.rst +++ b/docs/reference/packages.rst @@ -1,314 +1,153 @@ .. _packages: -Distribution packages, package management, and deploying applications -===================================================================== - -Just as the "big" Python, MicroPython supports creation of "third party" -packages, distributing them, and easily installing them in each user's -environment. This chapter discusses how these actions are achieved. -Some familiarity with Python packaging is recommended. - -Overview --------- - -Steps below represent a high-level workflow when creating and consuming -packages: - -1. Python modules and packages are turned into distribution package - archives, and published at the Python Package Index (PyPI). -2. :term:`upip` package manager can be used to install a distribution package - on a :term:`MicroPython port` with networking capabilities (for example, - on the Unix port). -3. For ports without networking capabilities, an "installation image" - can be prepared on the Unix port, and transferred to a device by - suitable means. -4. For low-memory ports, the installation image can be frozen as the - bytecode into MicroPython executable, thus minimizing the memory - storage overheads. - -The sections below describe this process in details. - -Distribution packages ---------------------- - -Python modules and packages can be packaged into archives suitable for -transfer between systems, storing at the well-known location (PyPI), -and downloading on demand for deployment. These archives are known as -*distribution packages* (to differentiate them from Python packages -(means to organize Python source code)). - -The MicroPython distribution package format is a well-known tar.gz -format, with some adaptations however. The Gzip compressor, used as -an external wrapper for TAR archives, by default uses 32KB dictionary -size, which means that to uncompress a compressed stream, 32KB of -contiguous memory needs to be allocated. This requirement may be not -satisfiable on low-memory devices, which may have total memory available -less than that amount, and even if not, a contiguous block like that -may be hard to allocate due to memory fragmentation. To accommodate -these constraints, MicroPython distribution packages use Gzip compression -with the dictionary size of 4K, which should be a suitable compromise -with still achieving some compression while being able to uncompressed -even by the smallest devices. - -Besides the small compression dictionary size, MicroPython distribution -packages also have other optimizations, like removing any files from -the archive which aren't used by the installation process. In particular, -:term:`upip` package manager doesn't execute ``setup.py`` during installation -(see below), and thus that file is not included in the archive. - -At the same time, these optimizations make MicroPython distribution -packages not compatible with :term:`CPython`'s package manager, ``pip``. -This isn't considered a big problem, because: - -1. Packages can be installed with :term:`upip`, and then can be used with - CPython (if they are compatible with it). -2. In the other direction, majority of CPython packages would be - incompatible with MicroPython by various reasons, first of all, - the reliance on features not implemented by MicroPython. - -Summing up, the MicroPython distribution package archives are highly -optimized for MicroPython's target environments, which are highly -resource constrained devices. - - -``upip`` package manager ------------------------- - -MicroPython distribution packages are intended to be installed using -the :term:`upip` package manager. :term:`upip` is a Python application which is -usually distributed (as frozen bytecode) with network-enabled -:term:`MicroPython ports `. At the very least, -:term:`upip` is available in the :term:`MicroPython Unix port`. - -On any :term:`MicroPython port` providing :term:`upip`, it can be accessed as -following:: - - import upip - upip.help() - upip.install(package_or_package_list, [path]) - -Where *package_or_package_list* is the name of a distribution -package to install, or a list of such names to install multiple -packages. Optional *path* parameter specifies filesystem -location to install under and defaults to the standard library -location (see below). - -An example of installing a specific package and then using it:: - - >>> import upip - >>> upip.install("micropython-pystone_lowmem") - [...] - >>> import pystone_lowmem - >>> pystone_lowmem.main() - -Note that the name of Python package and the name of distribution -package for it in general don't have to match, and oftentimes they -don't. This is because PyPI provides a central package repository -for all different Python implementations and versions, and thus -distribution package names may need to be namespaced for a particular -implementation. For example, all packages from `micropython-lib` -follow this naming convention: for a Python module or package named -``foo``, the distribution package name is ``micropython-foo``. - -For the ports which run MicroPython executable from the OS command -prompts (like the Unix port), `upip` can be (and indeed, usually is) -run from the command line instead of MicroPython's own REPL. The -commands which corresponds to the example above are:: - - micropython -m upip -h - micropython -m upip install [-p ] ... - micropython -m upip install micropython-pystone_lowmem - -[TODO: Describe installation path.] - - -Cross-installing packages -------------------------- - -For :term:`MicroPython ports ` without native networking -capabilities, the recommend process is "cross-installing" them into a -"directory image" using the :term:`MicroPython Unix port`, and then -transferring this image to a device by suitable means. - -Installing to a directory image involves using ``-p`` switch to :term:`upip`:: - - micropython -m upip install -p install_dir micropython-pystone_lowmem - -After this command, the package content (and contents of every dependency -packages) will be available in the ``install_dir/`` subdirectory. You -would need to transfer contents of this directory (without the -``install_dir/`` prefix) to the device, at the suitable location, where -it can be found by the Python ``import`` statement (see discussion of -the :term:`upip` installation path above). - - -Cross-installing packages with freezing ---------------------------------------- - -For the low-memory :term:`MicroPython ports `, the process -described in the previous section does not provide the most efficient -resource usage,because the packages are installed in the source form, -so need to be compiled to the bytecome on each import. This compilation -requires RAM, and the resulting bytecode is also stored in RAM, reducing -its amount available for storing application data. Moreover, the process -above requires presence of the filesystem on a device, and the most -resource-constrained devices may not even have it. - -The bytecode freezing is a process which resolves all the issues -mentioned above: - -* The source code is pre-compiled into bytecode and store as such. -* The bytecode is stored in ROM, not RAM. -* Filesystem is not required for frozen packages. - -Using frozen bytecode requires building the executable (firmware) -for a given :term:`MicroPython port` from the C source code. Consequently, -the process is: - -1. Follow the instructions for a particular port on setting up a - toolchain and building the port. For example, for ESP8266 port, - study instructions in ``ports/esp8266/README.md`` and follow them. - Make sure you can build the port and deploy the resulting - executable/firmware successfully before proceeding to the next steps. -2. Build :term:`MicroPython Unix port` and make sure it is in your PATH and - you can execute ``micropython``. -3. Change to port's directory (e.g. ``ports/esp8266/`` for ESP8266). -4. Run ``make clean-frozen``. This step cleans up any previous - modules which were installed for freezing (consequently, you need - to skip this step to add additional modules, instead of starting - from scratch). -5. Run ``micropython -m upip install -p modules ...`` to - install packages you want to freeze. -6. Run ``make clean``. -7. Run ``make``. - -After this, you should have the executable/firmware with modules as -the bytecode inside, which you can deploy the usual way. - -Few notes: - -1. Step 5 in the sequence above assumes that the distribution package - is available from PyPI. If that is not the case, you would need - to copy Python source files manually to ``modules/`` subdirectory - of the port directory. (Note that upip does not support - installing from e.g. version control repositories). -2. The firmware for baremetal devices usually has size restrictions, - so adding too many frozen modules may overflow it. Usually, you - would get a linking error if this happens. However, in some cases, - an image may be produced, which is not runnable on a device. Such - cases are in general bugs, and should be reported and further - investigated. If you face such a situation, as an initial step, - you may want to decrease the amount of frozen modules included. - - -Creating distribution packages ------------------------------- - -Distribution packages for MicroPython are created in the same manner -as for CPython or any other Python implementation, see references at -the end of chapter. Setuptools (instead of distutils) should be used, -because distutils do not support dependencies and other features. "Source -distribution" (``sdist``) format is used for packaging. The post-processing -discussed above, (and pre-processing discussed in the following section) -is achieved by using custom ``sdist`` command for setuptools. Thus, packaging -steps remain the same as for the standard setuptools, the user just -needs to override ``sdist`` command implementation by passing the -appropriate argument to ``setup()`` call:: - - from setuptools import setup - import sdist_upip - - setup( - ..., - cmdclass={'sdist': sdist_upip.sdist} - ) - -The sdist_upip.py module as referenced above can be found in -`micropython-lib`: -https://github.com/micropython/micropython-lib/blob/master/sdist_upip.py - - -Application resources ---------------------- - -A complete application, besides the source code, oftentimes also consists -of data files, e.g. web page templates, game images, etc. It's clear how -to deal with those when application is installed manually - you just put -those data files in the filesystem at some location and use the normal -file access functions. - -The situation is different when deploying applications from packages - this -is more advanced, streamlined and flexible way, but also requires more -advanced approach to accessing data files. This approach is treating -the data files as "resources", and abstracting away access to them. - -Python supports resource access using its "setuptools" library, using -``pkg_resources`` module. MicroPython, following its usual approach, -implements subset of the functionality of that module, specifically -``pkg_resources.resource_stream(package, resource)`` function. -The idea is that an application calls this function, passing a -resource identifier, which is a relative path to data file within -the specified package (usually top-level application package). It -returns a stream object which can be used to access resource contents. -Thus, the ``resource_stream()`` emulates interface of the standard -`open()` function. - -Implementation-wise, ``resource_stream()`` uses file operations -underlyingly, if distribution package is install in the filesystem. -However, it also supports functioning without the underlying filesystem, -e.g. if the package is frozen as the bytecode. This however requires -an extra intermediate step when packaging application - creation of -"Python resource module". - -The idea of this module is to convert binary data to a Python bytes -object, and put it into the dictionary, indexed by the resource name. -This conversion is done automatically using overridden ``sdist`` command -described in the previous section. - -Let's trace the complete process using the following example. Suppose -your application has the following structure:: - - my_app/ - __main__.py - utils.py - data/ - page.html - image.png - -``__main__.py`` and ``utils.py`` should access resources using the -following calls:: - - import pkg_resources - - pkg_resources.resource_stream(__name__, "data/page.html") - pkg_resources.resource_stream(__name__, "data/image.png") - -You can develop and debug using the :term:`MicroPython Unix port` as usual. -When time comes to make a distribution package out of it, just use -overridden "sdist" command from sdist_upip.py module as described in -the previous section. - -This will create a Python resource module named ``R.py``, based on the -files declared in ``MANIFEST`` or ``MANIFEST.in`` files (any non-``.py`` -file will be considered a resource and added to ``R.py``) - before -proceeding with the normal packaging steps. - -Prepared like this, your application will work both when deployed to -filesystem and as frozen bytecode. - -If you would like to debug ``R.py`` creation, you can run:: - - python3 setup.py sdist --manifest-only - -Alternatively, you can use tools/mpy_bin2res.py script from the -MicroPython distribution, in which can you will need to pass paths -to all resource files:: - - mpy_bin2res.py data/page.html data/image.png - -References ----------- - -* Python Packaging User Guide: https://packaging.python.org/ -* Setuptools documentation: https://setuptools.readthedocs.io/ -* Distutils documentation: https://docs.python.org/3/library/distutils.html +Package management +================== + +Installing packages with ``mip`` +-------------------------------- + +Network-capable boards include the ``mip`` module, which can install packages +from :term:`micropython-lib` and from third-party sites (including GitHub). + +``mip`` ("mip installs packages") is similar in concept to Python's ``pip`` tool, +however it does not use the PyPI index, rather it uses :term:`micropython-lib` +as its index by default. ``mip`` will automatically fetch compiled +:term:`.mpy file` when downloading from micropython-lib. + +The most common way to use ``mip`` is from the REPL:: + + >>> import mip + >>> mip.install("pkgname") # Installs the latest version of "pkgname" (and dependencies) + >>> mip.install("pkgname", version="x.y") # Installs version x.y of "pkgname" + >>> mip.install("pkgname", mpy=False) # Installs the source version (i.e. .py rather than .mpy files) + +``mip`` will detect an appropriate location on the filesystem by searching +``sys.path`` for the first entry ending in ``/lib``. You can override the +destination using ``target``, but note that this path must be in ``sys.path`` to be +able to subsequently import it.:: + + >>> mip.install("pkgname", target="third-party") + >>> sys.path.append("third-party") + +As well as downloading packages from the micropython-lib index, ``mip`` can also +install third-party libraries. The simplest way is to download a file directly:: + + >>> mip.install("http://example.com/x/y/foo.py") + >>> mip.install("http://example.com/x/y/foo.mpy") + +When installing a file directly, the ``target`` argument is still supported to set +the destination path, but ``mpy`` and ``version`` are ignored. + +The URL can also start with ``github:`` as a simple way of pointing to content +hosted on GitHub:: + + >>> mip.install("github:org/repo/path/foo.py") # Uses default branch + >>> mip.install("github:org/repo/path/foo.py", version="branch-or-tag") # Optionally specify the branch or tag + +More sophisticated packages (i.e. with more than one file, or with dependencies) +can be downloaded by specifying the path to their ``package.json``. + + >>> mip.install("http://example.com/x/package.json") + >>> mip.install("github:org/user/path/package.json") + +If no json file is specified, then "package.json" is implicitly added:: + + >>> mip.install("http://example.com/x/") + >>> mip.install("github:org/repo") + >>> mip.install("github:org/repo", version="branch-or-tag") + + +Using ``mip`` on the Unix port +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +On the Unix port, ``mip`` can be used at the REPL as above, and also by using ``-m``:: + + $ ./micropython -m mip install pkgname-or-url + $ ./micropython -m mip install pkgname-or-url@version + +The ``--target=path``, ``--no-mpy``, and ``--index`` arguments can be set:: + + $ ./micropython -m mip install --target=third-party pkgname + $ ./micropython -m mip install --no-mpy pkgname + $ ./micropython -m mip install --index https://host/pi pkgname + +Installing packages with ``mpremote`` +------------------------------------- + +The :term:`mpremote` tool also includes the same functionality as ``mip`` and +can be used from a host PC to install packages to a locally connected device +(e.g. via USB or UART):: + + $ mpremote install pkgname + $ mpremote install pkgname@x.y + $ mpremote install http://example.com/x/y/foo.py + $ mpremote install github:org/repo + $ mpremote install github:org/repo@branch-or-tag + +The ``--target=path``, ``--no-mpy``, and ``--index`` arguments can be set:: + + $ mpremote install --target=/flash/third-party pkgname + $ mpremote install --no-mpy pkgname + $ mpremote install --index https://host/pi pkgname + +Installing packages manually +---------------------------- + +Packages can also be installed (in either .py or .mpy form) by manually copying +the files to the device. Depending on the board this might be via USB Mass Storage, +the :term:`mpremote` tool (e.g. ``mpremote fs cp path/to/package.py :package.py``), +:term:`webrepl`, etc. + +Writing & publishing packages +----------------------------- + +Publishing to :term:`micropython-lib` is the easiest way to make your package +broadly accessible to MicroPython users, and automatically available via +``mip`` and ``mpremote`` and compiled to bytecode. See +https://github.com/micropython/micropython-lib for more information. + +To write a "self-hosted" package that can be downloaded by ``mip`` or +``mpremote``, you need a static webserver (or GitHub) to host either a +single .py file, or a package.json file alongside your .py files. + +A typical package.json for an example ``mlx90640`` library looks like:: + + { + "urls": [ + ["mlx90640/__init__.py", "github:org/micropython-mlx90640/mlx90640/__init__.py"], + ["mlx90640/utils.py", "github:org/micropython-mlx90640/mlx90640/utils.py"] + ], + "deps": [ + ["collections-defaultdict", "latest"], + ["os-path", "latest"] + ], + "version": "0.2" + } + +This includes two files, hosted at a GitHub repo named +``org/micropython-mlx90640``, which install into the ``mlx90640`` directory on +the device. It depends on ``collections-defaultdict`` and ``os-path`` which will +be installed automatically. + +Freezing packages +----------------- + +When a Python module or package is imported from the device filesystem, it is +compiled into :term:`bytecode` in RAM, ready to be executed by the VM. For +a :term:`.mpy file`, this conversion has been done already, but the bytecode +still ends up in RAM. + +For low-memory devices, or for large applications, it can be advantageous to +instead run the bytecode from ROM (i.e. flash memory). This can be done +by "freezing" the bytecode into the MicroPython firmware, which is then flashed +to the device. The runtime performance is the same (although importing is +faster), but it can free up significant amounts of RAM for your program to +use. + +The downside of this approach is that it's much slower to develop, because you +have to flash the firmware each time, but it can be still useful to freeze +dependencies that don't change often. + +Freezing is done by writing a manifest file and using it in the build, often as +part of a custom board definition. See the :ref:`manifest` guide for more +information. diff --git a/examples/hwapi/README.md b/examples/hwapi/README.md index 1992eb66094e8..df16b4c86be57 100644 --- a/examples/hwapi/README.md +++ b/examples/hwapi/README.md @@ -116,7 +116,7 @@ For example, one may invent a "configuration manager" helper module which will try to detect current board (among well-known ones), and load appropriate `hwconfig_*.py` - this assumes that a user would lazily deploy them all (or that application will be automatically installed, e.g. using MicroPython's -`upip` package manager). The key point in this case remains the same as +`mip` package manager). The key point in this case remains the same as elaborated above - always assume there can, and will be a custom configuration, and it should be well supported. So, any automatic detection should be overridable by a user, and instructions how to do so are among the most diff --git a/ports/esp32/boards/manifest.py b/ports/esp32/boards/manifest.py index fcc48d721805a..3f6c8cfde5a90 100644 --- a/ports/esp32/boards/manifest.py +++ b/ports/esp32/boards/manifest.py @@ -1,11 +1,10 @@ freeze("$(PORT_DIR)/modules") -module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) -module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) include("$(MPY_DIR)/extmod/uasyncio") # Require some micropython-lib modules. require("dht") require("ds18x20") +require("mip") require("neopixel") require("ntptime") require("onewire") diff --git a/ports/esp8266/README.md b/ports/esp8266/README.md index dd50fc1af943f..b54d8958d77d8 100644 --- a/ports/esp8266/README.md +++ b/ports/esp8266/README.md @@ -200,20 +200,22 @@ Python prompt over WiFi, connecting through a browser. - GitHub repository https://github.com/micropython/webrepl. Please follow the instructions there. -__upip__ +__mip__ -The ESP8266 port comes with builtin `upip` package manager, which can -be used to install additional modules (see the main README for more -information): +The ESP8266 port comes with the built-in `mip` package manager, which can +be used to install additional modules: ``` ->>> import upip ->>> upip.install("micropython-pystone_lowmem") +>>> import mip +>>> mip.install("hmac") [...] ->>> import pystone_lowmem ->>> pystone_lowmem.main() +>>> import hmac +>>> hmac.new(b"1234567890", msg="hello world").hexdigest() ``` +See [Package management](https://docs.micropython.org/en/latest/reference/packages.html) for more +information about `mip`. + Downloading and installing packages may requite a lot of free memory, if you get an error, retry immediately after the hard reset. diff --git a/ports/esp8266/boards/manifest.py b/ports/esp8266/boards/manifest.py index e7defd0bb70ff..53975f6a6baf8 100644 --- a/ports/esp8266/boards/manifest.py +++ b/ports/esp8266/boards/manifest.py @@ -1,9 +1,8 @@ freeze("$(PORT_DIR)/modules") -module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) -module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) -require("ntptime") require("dht") -require("onewire") require("ds18x20") +require("mip") require("neopixel") +require("ntptime") +require("onewire") require("webrepl") diff --git a/ports/rp2/boards/PICO_W/manifest.py b/ports/rp2/boards/PICO_W/manifest.py index 4d9eb87f20f3c..8a74006c64f7e 100644 --- a/ports/rp2/boards/PICO_W/manifest.py +++ b/ports/rp2/boards/PICO_W/manifest.py @@ -1,7 +1,5 @@ include("../manifest.py") -module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) -module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) - +require("mip") require("ntptime") require("urequests") diff --git a/ports/unix/README.md b/ports/unix/README.md index efc68b2455f90..a3a0dba75a131 100644 --- a/ports/unix/README.md +++ b/ports/unix/README.md @@ -24,21 +24,26 @@ Use `CTRL-D` (i.e. EOF) to exit the shell. Learn about command-line options (in particular, how to increase heap size which may be needed for larger applications): - $ ./micropython -h + $ ./build-standard/micropython -h To run the complete testsuite, use: $ make test -The Unix port comes with a builtin package manager called upip, e.g.: +The Unix port comes with a built-in package manager called `mip`, e.g.: - $ ./micropython -m upip install micropython-pystone - $ ./micropython -m pystone + $ ./build-standard/micropython -m mip install hmac -Browse available modules on -[PyPI](https://pypi.python.org/pypi?%3Aaction=search&term=micropython). -Standard library modules come from the -[micropython-lib](https://github.com/micropython/micropython-lib) project. +or + + $ ./build-standard/micropython + >>> import mip + >>> mip.install("hmac") + +Browse available modules at [micropython-lib] +(https://github.com/micropython/micropython-lib). See +[Package management](https://docs.micropython.org/en/latest/reference/packages.html) +for more information about `mip`. External dependencies --------------------- @@ -65,6 +70,5 @@ or not). If you intend to build MicroPython with additional options (like cross-compiling), the same set of options should be passed to `make deplibs`. To actually enable/disable use of dependencies, edit the `ports/unix/mpconfigport.mk` file, which has inline descriptions of the -options. For example, to build SSL module (required for the `upip` tool -described above, and so enabled by default), `MICROPY_PY_USSL` should be set -to 1. +options. For example, to build the SSL module, `MICROPY_PY_USSL` should be +set to 1. diff --git a/ports/unix/variants/manifest.py b/ports/unix/variants/manifest.py index bf7ce992ade0d..e7fe747cb7976 100644 --- a/ports/unix/variants/manifest.py +++ b/ports/unix/variants/manifest.py @@ -1,2 +1 @@ -module("upip.py", base_path="$(MPY_DIR)/tools", opt=3) -module("upip_utarfile.py", base_path="$(MPY_DIR)/tools", opt=3) +require("mip") diff --git a/tools/upip.py b/tools/upip.py deleted file mode 100644 index 2932cca50cc9d..0000000000000 --- a/tools/upip.py +++ /dev/null @@ -1,351 +0,0 @@ -# -# upip - Package manager for MicroPython -# -# Copyright (c) 2015-2018 Paul Sokolovsky -# -# Licensed under the MIT license. -# -import sys -import gc -import uos as os -import uerrno as errno -import ujson as json -import uzlib -import upip_utarfile as tarfile - -gc.collect() - - -debug = False -index_urls = ["https://micropython.org/pi", "https://pypi.org/pypi"] -install_path = None -cleanup_files = [] -gzdict_sz = 16 + 15 - -file_buf = bytearray(512) - - -class NotFoundError(Exception): - pass - - -def op_split(path): - if path == "": - return ("", "") - r = path.rsplit("/", 1) - if len(r) == 1: - return ("", path) - head = r[0] - if not head: - head = "/" - return (head, r[1]) - - -# Expects *file* name -def _makedirs(name, mode=0o777): - ret = False - s = "" - comps = name.rstrip("/").split("/")[:-1] - if comps[0] == "": - s = "/" - for c in comps: - if s and s[-1] != "/": - s += "/" - s += c - try: - os.mkdir(s) - ret = True - except OSError as e: - if e.errno != errno.EEXIST and e.errno != errno.EISDIR: - raise e - ret = False - return ret - - -def save_file(fname, subf): - global file_buf - with open(fname, "wb") as outf: - while True: - sz = subf.readinto(file_buf) - if not sz: - break - outf.write(file_buf, sz) - - -def install_tar(f, prefix): - meta = {} - for info in f: - # print(info) - fname = info.name - try: - fname = fname[fname.index("/") + 1 :] - except ValueError: - fname = "" - - save = True - for p in ("setup.", "PKG-INFO", "README"): - # print(fname, p) - if fname.startswith(p) or ".egg-info" in fname: - if fname.endswith("/requires.txt"): - meta["deps"] = f.extractfile(info).read() - save = False - if debug: - print("Skipping", fname) - break - - if save: - outfname = prefix + fname - if info.type != tarfile.DIRTYPE: - if debug: - print("Extracting " + outfname) - _makedirs(outfname) - subf = f.extractfile(info) - save_file(outfname, subf) - return meta - - -def expandhome(s): - if "~/" in s: - h = os.getenv("HOME") - s = s.replace("~/", h + "/") - return s - - -import ussl -import usocket - -warn_ussl = True - - -def url_open(url): - global warn_ussl - - if debug: - print(url) - - proto, _, host, urlpath = url.split("/", 3) - try: - port = 443 - if ":" in host: - host, port = host.split(":") - port = int(port) - ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) - except OSError as e: - fatal("Unable to resolve %s (no Internet?)" % host, e) - # print("Address infos:", ai) - ai = ai[0] - - s = usocket.socket(ai[0], ai[1], ai[2]) - try: - # print("Connect address:", addr) - s.connect(ai[-1]) - - if proto == "https:": - s = ussl.wrap_socket(s, server_hostname=host) - if warn_ussl: - print("Warning: %s SSL certificate is not validated" % host) - warn_ussl = False - - # MicroPython rawsocket module supports file interface directly - s.write("GET /%s HTTP/1.0\r\nHost: %s:%s\r\n\r\n" % (urlpath, host, port)) - l = s.readline() - protover, status, msg = l.split(None, 2) - if status != b"200": - if status == b"404" or status == b"301": - raise NotFoundError("Package not found") - raise ValueError(status) - while 1: - l = s.readline() - if not l: - raise ValueError("Unexpected EOF in HTTP headers") - if l == b"\r\n": - break - except Exception as e: - s.close() - raise e - - return s - - -def get_pkg_metadata(name): - for url in index_urls: - try: - f = url_open("%s/%s/json" % (url, name)) - except NotFoundError: - continue - try: - return json.load(f) - finally: - f.close() - raise NotFoundError("Package not found") - - -def fatal(msg, exc=None): - print("Error:", msg) - if exc and debug: - raise exc - sys.exit(1) - - -def install_pkg(pkg_spec, install_path): - package = pkg_spec.split("==") - data = get_pkg_metadata(package[0]) - - if len(package) == 1: - latest_ver = data["info"]["version"] - else: - latest_ver = package[1] - packages = data["releases"][latest_ver] - del data - gc.collect() - assert len(packages) == 1 - package_url = packages[0]["url"] - print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url)) - f1 = url_open(package_url) - try: - f2 = uzlib.DecompIO(f1, gzdict_sz) - f3 = tarfile.TarFile(fileobj=f2) - meta = install_tar(f3, install_path) - finally: - f1.close() - del f3 - del f2 - gc.collect() - return meta - - -def install(to_install, install_path=None): - # Calculate gzip dictionary size to use - global gzdict_sz - sz = gc.mem_free() + gc.mem_alloc() - if sz <= 65536: - gzdict_sz = 16 + 12 - - if install_path is None: - install_path = get_install_path() - if install_path[-1] != "/": - install_path += "/" - if not isinstance(to_install, list): - to_install = [to_install] - print("Installing to: " + install_path) - # sets would be perfect here, but don't depend on them - installed = [] - try: - while to_install: - if debug: - print("Queue:", to_install) - pkg_spec = to_install.pop(0) - if pkg_spec in installed: - continue - meta = install_pkg(pkg_spec, install_path) - installed.append(pkg_spec) - if debug: - print(meta) - deps = meta.get("deps", "").rstrip() - if deps: - deps = deps.decode("utf-8").split("\n") - to_install.extend(deps) - except Exception as e: - print( - "Error installing '{}': {}, packages may be partially installed".format(pkg_spec, e), - file=sys.stderr, - ) - - -def get_install_path(): - global install_path - if install_path is None: - # sys.path[0] is current module's path - install_path = sys.path[1] - if install_path == ".frozen": - install_path = sys.path[2] - install_path = expandhome(install_path) - return install_path - - -def cleanup(): - for fname in cleanup_files: - try: - os.unlink(fname) - except OSError: - print("Warning: Cannot delete " + fname) - - -def help(): - print( - """\ -upip - Simple PyPI package manager for MicroPython -Usage: micropython -m upip install [-p ] ... | -r -import upip; upip.install(package_or_list, []) - -If isn't given, packages will be installed to sys.path[1], or -sys.path[2] if the former is .frozen (path can be set from MICROPYPATH -environment variable if supported).""" - ) - print("Default install path:", get_install_path()) - print( - """\ - -Note: only MicroPython packages (usually, named micropython-*) are supported -for installation, upip does not support arbitrary code in setup.py. -""" - ) - - -def main(): - global debug - global index_urls - global install_path - install_path = None - - if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help": - help() - return - - if sys.argv[1] != "install": - fatal("Only 'install' command supported") - - to_install = [] - - i = 2 - while i < len(sys.argv) and sys.argv[i][0] == "-": - opt = sys.argv[i] - i += 1 - if opt == "-h" or opt == "--help": - help() - return - elif opt == "-p": - install_path = sys.argv[i] - i += 1 - elif opt == "-r": - list_file = sys.argv[i] - i += 1 - with open(list_file) as f: - while True: - l = f.readline() - if not l: - break - if l[0] == "#": - continue - to_install.append(l.rstrip()) - elif opt == "-i": - index_urls = [sys.argv[i]] - i += 1 - elif opt == "--debug": - debug = True - else: - fatal("Unknown/unsupported option: " + opt) - - to_install.extend(sys.argv[i:]) - if not to_install: - help() - return - - install(to_install) - - if not debug: - cleanup() - - -if __name__ == "__main__": - main() diff --git a/tools/upip_utarfile.py b/tools/upip_utarfile.py deleted file mode 100644 index 21b899f020a90..0000000000000 --- a/tools/upip_utarfile.py +++ /dev/null @@ -1,95 +0,0 @@ -import uctypes - -# http://www.gnu.org/software/tar/manual/html_node/Standard.html -TAR_HEADER = { - "name": (uctypes.ARRAY | 0, uctypes.UINT8 | 100), - "size": (uctypes.ARRAY | 124, uctypes.UINT8 | 11), -} - -DIRTYPE = "dir" -REGTYPE = "file" - - -def roundup(val, align): - return (val + align - 1) & ~(align - 1) - - -class FileSection: - def __init__(self, f, content_len, aligned_len): - self.f = f - self.content_len = content_len - self.align = aligned_len - content_len - - def read(self, sz=65536): - if self.content_len == 0: - return b"" - if sz > self.content_len: - sz = self.content_len - data = self.f.read(sz) - sz = len(data) - self.content_len -= sz - return data - - def readinto(self, buf): - if self.content_len == 0: - return 0 - if len(buf) > self.content_len: - buf = memoryview(buf)[: self.content_len] - sz = self.f.readinto(buf) - self.content_len -= sz - return sz - - def skip(self): - sz = self.content_len + self.align - if sz: - buf = bytearray(16) - while sz: - s = min(sz, 16) - self.f.readinto(buf, s) - sz -= s - - -class TarInfo: - def __str__(self): - return "TarInfo(%r, %s, %d)" % (self.name, self.type, self.size) - - -class TarFile: - def __init__(self, name=None, fileobj=None): - if fileobj: - self.f = fileobj - else: - self.f = open(name, "rb") - self.subf = None - - def next(self): - if self.subf: - self.subf.skip() - buf = self.f.read(512) - if not buf: - return None - - h = uctypes.struct(uctypes.addressof(buf), TAR_HEADER, uctypes.LITTLE_ENDIAN) - - # Empty block means end of archive - if h.name[0] == 0: - return None - - d = TarInfo() - d.name = str(h.name, "utf-8").rstrip("\0") - d.size = int(bytes(h.size), 8) - d.type = [REGTYPE, DIRTYPE][d.name[-1] == "/"] - self.subf = d.subf = FileSection(self.f, d.size, roundup(d.size, 512)) - return d - - def __iter__(self): - return self - - def __next__(self): - v = self.next() - if v is None: - raise StopIteration - return v - - def extractfile(self, tarinfo): - return tarinfo.subf From 282401da5c6e689e63af7b9858cf9b0d23a24b2b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 30 Sep 2022 15:33:43 +1000 Subject: [PATCH 2661/3533] tools/manifestfile.py: Replace recursive glob with os.walk. Recursive glob isn't supported before Python 3.5. Signed-off-by: Jim Mussared --- tools/manifestfile.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/manifestfile.py b/tools/manifestfile.py index f7966ccb8045d..fb9691d2931ac 100644 --- a/tools/manifestfile.py +++ b/tools/manifestfile.py @@ -311,14 +311,14 @@ def require(self, name, version=None, unix_ffi=False, **kwargs): lib_dirs = ["unix-ffi"] + lib_dirs for lib_dir in lib_dirs: - for manifest_path in glob.glob( - os.path.join( - self._path_vars["MPY_LIB_DIR"], lib_dir, "**", name, "manifest.py" - ), - recursive=True, + # Search for {lib_dir}/**/{name}/manifest.py. + for root, dirnames, filenames in os.walk( + os.path.join(self._path_vars["MPY_LIB_DIR"], lib_dir) ): - self.include(manifest_path, **kwargs) - return + if os.path.basename(root) == name and "manifest.py" in filenames: + self.include(root, **kwargs) + return + raise ValueError("Library not found in local micropython-lib: {}".format(name)) else: # TODO: HTTP request to obtain URLs from manifest.json. From 413a69b94b92d1ae1cdd132e41d460259d22b9b8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 30 Sep 2022 11:46:23 +1000 Subject: [PATCH 2662/3533] tools/mpremote: Simplify dispatch of commands. No functional change. This makes each built-in command defined by just a handler method and simplifies a lot of the logic around tracking the board state. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tools/mpremote/mpremote/commands.py | 261 +++++++++++++ tools/mpremote/mpremote/main.py | 557 ++++++++-------------------- tools/mpremote/mpremote/repl.py | 101 +++++ 3 files changed, 510 insertions(+), 409 deletions(-) create mode 100644 tools/mpremote/mpremote/commands.py create mode 100644 tools/mpremote/mpremote/repl.py diff --git a/tools/mpremote/mpremote/commands.py b/tools/mpremote/mpremote/commands.py new file mode 100644 index 0000000000000..60a625d5e854c --- /dev/null +++ b/tools/mpremote/mpremote/commands.py @@ -0,0 +1,261 @@ +import os +import sys +import tempfile + +import serial.tools.list_ports + +from . import pyboardextended as pyboard + + +class CommandError(Exception): + pass + + do_disconnect(state) + + try: + if dev == "list": + # List attached devices. + for p in sorted(serial.tools.list_ports.comports()): + print( + "{} {} {:04x}:{:04x} {} {}".format( + p.device, + p.serial_number, + p.vid if isinstance(p.vid, int) else 0, + p.pid if isinstance(p.pid, int) else 0, + p.manufacturer, + p.product, + ) + ) + # Don't do implicit REPL command. + state.did_action() + elif dev == "auto": + # Auto-detect and auto-connect to the first available device. + for p in sorted(serial.tools.list_ports.comports()): + try: + state.pyb = pyboard.PyboardExtended(p.device, baudrate=115200) + return + except pyboard.PyboardError as er: + if not er.args[0].startswith("failed to access"): + raise er + raise pyboard.PyboardError("no device found") + elif dev.startswith("id:"): + # Search for a device with the given serial number. + serial_number = dev[len("id:") :] + dev = None + for p in serial.tools.list_ports.comports(): + if p.serial_number == serial_number: + state.pyb = pyboard.PyboardExtended(p.device, baudrate=115200) + return + raise pyboard.PyboardError("no device with serial number {}".format(serial_number)) + else: + # Connect to the given device. + if dev.startswith("port:"): + dev = dev[len("port:") :] + state.pyb = pyboard.PyboardExtended(dev, baudrate=115200) + return + except pyboard.PyboardError as er: + msg = er.args[0] + if msg.startswith("failed to access"): + msg += " (it may be in use by another program)" + print(msg) + sys.exit(1) + + +def do_disconnect(state, _args=None): + if not state.pyb: + return + + try: + if state.pyb.mounted: + if not state.pyb.in_raw_repl: + state.pyb.enter_raw_repl(soft_reset=False) + state.pyb.umount_local() + if state.pyb.in_raw_repl: + state.pyb.exit_raw_repl() + except OSError: + # Ignore any OSError exceptions when shutting down, eg: + # - pyboard.filesystem_command will close the connecton if it had an error + # - umounting will fail if serial port disappeared + pass + state.pyb.close() + state.pyb = None + state._auto_soft_reset = True + + +def show_progress_bar(size, total_size, op="copying"): + if not sys.stdout.isatty(): + return + verbose_size = 2048 + bar_length = 20 + if total_size < verbose_size: + return + elif size >= total_size: + # Clear progress bar when copy completes + print("\r" + " " * (13 + len(op) + bar_length) + "\r", end="") + else: + bar = size * bar_length // total_size + progress = size * 100 // total_size + print( + "\r ... {} {:3d}% [{}{}]".format(op, progress, "#" * bar, "-" * (bar_length - bar)), + end="", + ) + + +# Get all args up to the terminator ("+"). +# The passed args will be updated with these ones removed. +def _get_fs_args(args): + n = 0 + for src in args: + if src == "+": + break + n += 1 + fs_args = args[:n] + args[:] = args[n + 1 :] + return fs_args + + +def do_filesystem(state, args): + state.ensure_raw_repl() + state.did_action() + + def _list_recursive(files, path): + if os.path.isdir(path): + for entry in os.listdir(path): + _list_recursive(files, "/".join((path, entry))) + else: + files.append(os.path.split(path)) + + fs_args = _get_fs_args(args) + + # Don't be verbose when using cat, so output can be redirected to something. + verbose = fs_args[0] != "cat" + + if fs_args[0] == "cp" and fs_args[1] == "-r": + fs_args.pop(0) + fs_args.pop(0) + if fs_args[-1] != ":": + print(f"{_PROG}: 'cp -r' destination must be ':'") + sys.exit(1) + fs_args.pop() + src_files = [] + for path in fs_args: + if path.startswith(":"): + raise CommandError("'cp -r' source files must be local") + _list_recursive(src_files, path) + known_dirs = {""} + state.pyb.exec_("import uos") + for dir, file in src_files: + dir_parts = dir.split("/") + for i in range(len(dir_parts)): + d = "/".join(dir_parts[: i + 1]) + if d not in known_dirs: + state.pyb.exec_("try:\n uos.mkdir('%s')\nexcept OSError as e:\n print(e)" % d) + known_dirs.add(d) + pyboard.filesystem_command( + state.pyb, + ["cp", "/".join((dir, file)), ":" + dir + "/"], + progress_callback=show_progress_bar, + verbose=verbose, + ) + else: + try: + pyboard.filesystem_command( + state.pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose + ) + except OSError as er: + raise CommandError(er) + + +def do_edit(state, args): + state.ensure_raw_repl() + state.did_action() + + if not os.getenv("EDITOR"): + raise pyboard.PyboardError("edit: $EDITOR not set") + for src in _get_fs_args(args): + src = src.lstrip(":") + dest_fd, dest = tempfile.mkstemp(suffix=os.path.basename(src)) + try: + print("edit :%s" % (src,)) + os.close(dest_fd) + state.pyb.fs_touch(src) + state.pyb.fs_get(src, dest, progress_callback=show_progress_bar) + if os.system("$EDITOR '%s'" % (dest,)) == 0: + state.pyb.fs_put(dest, src, progress_callback=show_progress_bar) + finally: + os.unlink(dest) + + +def _get_follow_arg(args): + if args[0] == "--no-follow": + args.pop(0) + return False + else: + return True + + +def _do_execbuffer(state, buf, follow): + state.ensure_raw_repl() + state.did_action() + + try: + state.pyb.exec_raw_no_follow(buf) + if follow: + ret, ret_err = state.pyb.follow(timeout=None, data_consumer=pyboard.stdout_write_bytes) + if ret_err: + pyboard.stdout_write_bytes(ret_err) + sys.exit(1) + except pyboard.PyboardError as er: + print(er) + sys.exit(1) + except KeyboardInterrupt: + sys.exit(1) + + +def do_exec(state, args): + follow = _get_follow_arg(args) + buf = args.pop(0) + _do_execbuffer(state, buf, follow) + + +def do_eval(state, args): + follow = _get_follow_arg(args) + buf = "print(" + args.pop(0) + ")" + _do_execbuffer(state, buf, follow) + + +def do_run(state, args): + follow = _get_follow_arg(args) + filename = args.pop(0) + try: + with open(filename, "rb") as f: + buf = f.read() + except OSError: + raise CommandError(f"could not read file '{filename}'") + sys.exit(1) + _do_execbuffer(state, buf, follow) + + +def do_mount(state, args): + state.ensure_raw_repl() + + unsafe_links = False + if args[0] == "--unsafe-links" or args[0] == "-l": + args.pop(0) + unsafe_links = True + path = args.pop(0) + state.pyb.mount_local(path, unsafe_links=unsafe_links) + print(f"Local directory {path} is mounted at /remote") + + +def do_umount(state, path): + state.ensure_raw_repl() + state.pyb.umount_local() + + +def do_resume(state, _args=None): + state._auto_soft_reset = False + + +def do_soft_reset(state, _args=None): + state.ensure_raw_repl(soft_reset=True) diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index bd98da88248c1..b96e3f46b1bf6 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -19,45 +19,101 @@ import os, sys from collections.abc import Mapping -import tempfile from textwrap import dedent -import serial.tools.list_ports - -from . import pyboardextended as pyboard -from .console import Console, ConsolePosix +from .commands import ( + CommandError, + do_connect, + do_disconnect, + do_edit, + do_filesystem, + do_mount, + do_umount, + do_exec, + do_eval, + do_run, + do_resume, + do_soft_reset, +) +from .repl import do_repl _PROG = "mpremote" -# (need_raw_repl, is_action, num_args_min, help_text) + +def do_help(state, _args=None): + def print_commands_help(cmds, help_idx): + max_command_len = max(len(cmd) for cmd in cmds.keys()) + for cmd in sorted(cmds.keys()): + help_message_lines = dedent(cmds[cmd][help_idx]).split("\n") + help_message = help_message_lines[0] + for line in help_message_lines[1:]: + help_message = "{}\n{}{}".format(help_message, " " * (max_command_len + 4), line) + print(" ", cmd, " " * (max_command_len - len(cmd) + 2), help_message, sep="") + + print(_PROG, "-- MicroPython remote control") + print("See https://docs.micropython.org/en/latest/reference/mpremote.html") + + print("\nList of commands:") + print_commands_help(_COMMANDS, 1) + + print("\nList of shortcuts:") + print_commands_help(_command_expansions, 2) + + sys.exit(0) + + +def do_version(state, _args=None): + from . import __version__ + + print(f"{_PROG} {__version__}") + sys.exit(0) + + +# Map of "command" to tuple of (num_args_min, help_text, handler). _COMMANDS = { "connect": ( - False, - False, 1, """\ connect to given device device may be: list, auto, id:x, port:x or any valid device name/path""", + do_connect, + ), + "disconnect": ( + 0, + "disconnect current device", + do_disconnect, + ), + "edit": ( + 1, + "edit files on the device", + do_edit, + ), + "resume": ( + 0, + "resume a previous mpremote session (will not auto soft-reset)", + do_resume, + ), + "soft-reset": ( + 0, + "perform a soft-reset of the device", + do_soft_reset, ), - "disconnect": (False, False, 0, "disconnect current device"), - "edit": (True, True, 1, "edit files on the device"), - "resume": (False, False, 0, "resume a previous mpremote session (will not auto soft-reset)"), - "soft-reset": (False, True, 0, "perform a soft-reset of the device"), "mount": ( - True, - False, 1, """\ mount local directory on device options: --unsafe-links, -l follow symbolic links pointing outside of local directory""", + do_mount, + ), + "umount": ( + 0, + "unmount the local directory", + do_umount, ), - "umount": (True, False, 0, "unmount the local directory"), "repl": ( - False, - True, 0, """\ enter REPL @@ -65,15 +121,45 @@ --capture --inject-code --inject-file """, + do_repl, + ), + "eval": ( + 1, + "evaluate and print the string", + do_eval, + ), + "exec": ( + 1, + "execute the string", + do_exec, + ), + "run": ( + 1, + "run the given local script", + do_run, + ), + "fs": ( + 1, + "execute filesystem commands on the device", + do_filesystem, + ), + "help": ( + 0, + "print help and exit", + do_help, + ), + "version": ( + 0, + "print version and exit", + do_version, ), - "eval": (True, True, 1, "evaluate and print the string"), - "exec": (True, True, 1, "execute the string"), - "run": (True, True, 1, "run the given local script"), - "fs": (True, True, 1, "execute filesystem commands on the device"), - "help": (False, False, 0, "print help and exit"), - "version": (False, False, 0, "print version and exit"), } +# Additional commands aliases. +# The value can either be: +# - A command string. +# - A list of command strings, each command will be executed sequentially. +# - A dict of command: { [], help: ""} _BUILTIN_COMMAND_EXPANSIONS = { # Device connection shortcuts. "devs": { @@ -117,6 +203,8 @@ "--version": "version", } +# Add "a0", "a1", ..., "u0", "u1", ..., "c0", "c1", ... as aliases +# for "connect /dev/ttyACMn" (and /dev/ttyUSBn, COMn) etc. for port_num in range(4): for prefix, port in [("a", "/dev/ttyACM"), ("u", "/dev/ttyUSB"), ("c", "COM")]: _BUILTIN_COMMAND_EXPANSIONS["{}{}".format(prefix, port_num)] = { @@ -220,307 +308,33 @@ def usage_error(cmd, exp_args, msg): args[0:0] = ["exec", ";".join(pre)] -def do_connect(args): - dev = args.pop(0) - try: - if dev == "list": - # List attached devices. - for p in sorted(serial.tools.list_ports.comports()): - print( - "{} {} {:04x}:{:04x} {} {}".format( - p.device, - p.serial_number, - p.vid if isinstance(p.vid, int) else 0, - p.pid if isinstance(p.pid, int) else 0, - p.manufacturer, - p.product, - ) - ) - return None - elif dev == "auto": - # Auto-detect and auto-connect to the first available device. - for p in sorted(serial.tools.list_ports.comports()): - try: - return pyboard.PyboardExtended(p.device, baudrate=115200) - except pyboard.PyboardError as er: - if not er.args[0].startswith("failed to access"): - raise er - raise pyboard.PyboardError("no device found") - elif dev.startswith("id:"): - # Search for a device with the given serial number. - serial_number = dev[len("id:") :] - dev = None - for p in serial.tools.list_ports.comports(): - if p.serial_number == serial_number: - return pyboard.PyboardExtended(p.device, baudrate=115200) - raise pyboard.PyboardError("no device with serial number {}".format(serial_number)) - else: - # Connect to the given device. - if dev.startswith("port:"): - dev = dev[len("port:") :] - return pyboard.PyboardExtended(dev, baudrate=115200) - except pyboard.PyboardError as er: - msg = er.args[0] - if msg.startswith("failed to access"): - msg += " (it may be in use by another program)" - print(msg) - sys.exit(1) +class State: + def __init__(self): + self.pyb = None + self._did_action = False + self._auto_soft_reset = True + def did_action(self): + self._did_action = True -def do_disconnect(pyb): - try: - if pyb.mounted: - if not pyb.in_raw_repl: - pyb.enter_raw_repl(soft_reset=False) - pyb.umount_local() - if pyb.in_raw_repl: - pyb.exit_raw_repl() - except OSError: - # Ignore any OSError exceptions when shutting down, eg: - # - pyboard.filesystem_command will close the connecton if it had an error - # - umounting will fail if serial port disappeared - pass - pyb.close() - - -def show_progress_bar(size, total_size): - if not sys.stdout.isatty(): - return - verbose_size = 2048 - bar_length = 20 - if total_size < verbose_size: - return - elif size >= total_size: - # Clear progress bar when copy completes - print("\r" + " " * (20 + bar_length) + "\r", end="") - else: - progress = size / total_size - bar = round(progress * bar_length) - print( - "\r ... copying {:3.0f}% [{}{}]".format( - progress * 100, "#" * bar, "-" * (bar_length - bar) - ), - end="", - ) - - -# Get all args up to the terminator ("+"). -# The passed args will be updated with these ones removed. -def get_fs_args(args): - n = 0 - for src in args: - if src == "+": - break - n += 1 - fs_args = args[:n] - args[:] = args[n + 1 :] - return fs_args - - -def do_filesystem(pyb, args): - def _list_recursive(files, path): - if os.path.isdir(path): - for entry in os.listdir(path): - _list_recursive(files, "/".join((path, entry))) - else: - files.append(os.path.split(path)) - - fs_args = get_fs_args(args) - - # Don't be verbose when using cat, so output can be redirected to something. - verbose = fs_args[0] != "cat" - - if fs_args[0] == "cp" and fs_args[1] == "-r": - fs_args.pop(0) - fs_args.pop(0) - if fs_args[-1] != ":": - print(f"{_PROG}: 'cp -r' destination must be ':'") - sys.exit(1) - fs_args.pop() - src_files = [] - for path in fs_args: - if path.startswith(":"): - print(f"{_PROG}: 'cp -r' source files must be local") - sys.exit(1) - _list_recursive(src_files, path) - known_dirs = {""} - pyb.exec_("import uos") - for dir, file in src_files: - dir_parts = dir.split("/") - for i in range(len(dir_parts)): - d = "/".join(dir_parts[: i + 1]) - if d not in known_dirs: - pyb.exec_("try:\n uos.mkdir('%s')\nexcept OSError as e:\n print(e)" % d) - known_dirs.add(d) - pyboard.filesystem_command( - pyb, - ["cp", "/".join((dir, file)), ":" + dir + "/"], - progress_callback=show_progress_bar, - verbose=verbose, - ) - else: - try: - pyboard.filesystem_command( - pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose - ) - except OSError as er: - print(f"{_PROG}: {er}") - sys.exit(1) - - -def do_edit(pyb, args): - if not os.getenv("EDITOR"): - raise pyboard.PyboardError("edit: $EDITOR not set") - for src in get_fs_args(args): - src = src.lstrip(":") - dest_fd, dest = tempfile.mkstemp(suffix=os.path.basename(src)) - try: - print("edit :%s" % (src,)) - os.close(dest_fd) - pyb.fs_touch(src) - pyb.fs_get(src, dest, progress_callback=show_progress_bar) - if os.system("$EDITOR '%s'" % (dest,)) == 0: - pyb.fs_put(dest, src, progress_callback=show_progress_bar) - finally: - os.unlink(dest) - - -def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject): - while True: - console_in.waitchar(pyb.serial) - c = console_in.readchar() - if c: - if c == b"\x1d": # ctrl-], quit - break - elif c == b"\x04": # ctrl-D - # special handling needed for ctrl-D if filesystem is mounted - pyb.write_ctrl_d(console_out_write) - elif c == b"\x0a" and code_to_inject is not None: # ctrl-j, inject code - pyb.serial.write(code_to_inject) - elif c == b"\x0b" and file_to_inject is not None: # ctrl-k, inject script - console_out_write(bytes("Injecting %s\r\n" % file_to_inject, "utf8")) - pyb.enter_raw_repl(soft_reset=False) - with open(file_to_inject, "rb") as f: - pyfile = f.read() - try: - pyb.exec_raw_no_follow(pyfile) - except pyboard.PyboardError as er: - console_out_write(b"Error:\r\n") - console_out_write(er) - pyb.exit_raw_repl() - else: - pyb.serial.write(c) - - try: - n = pyb.serial.inWaiting() - except OSError as er: - if er.args[0] == 5: # IO error, device disappeared - print("device disconnected") - break - - if n > 0: - c = pyb.serial.read(1) - if c is not None: - # pass character through to the console - oc = ord(c) - if oc in (8, 9, 10, 13, 27) or 32 <= oc <= 126: - console_out_write(c) - else: - console_out_write(b"[%02x]" % ord(c)) - - -def do_repl(pyb, args): - capture_file = None - code_to_inject = None - file_to_inject = None - - while len(args): - if args[0] == "--capture": - args.pop(0) - capture_file = args.pop(0) - elif args[0] == "--inject-code": - args.pop(0) - code_to_inject = bytes(args.pop(0).replace("\\n", "\r\n"), "utf8") - elif args[0] == "--inject-file": - args.pop(0) - file_to_inject = args.pop(0) - else: - break - - print("Connected to MicroPython at %s" % pyb.device_name) - print("Use Ctrl-] to exit this shell") - if capture_file is not None: - print('Capturing session to file "%s"' % capture_file) - capture_file = open(capture_file, "wb") - if code_to_inject is not None: - print("Use Ctrl-J to inject", code_to_inject) - if file_to_inject is not None: - print('Use Ctrl-K to inject file "%s"' % file_to_inject) - - console = Console() - console.enter() - - def console_out_write(b): - console.write(b) - if capture_file is not None: - capture_file.write(b) - capture_file.flush() + def run_repl_on_completion(self): + return not self._did_action - try: - do_repl_main_loop( - pyb, - console, - console_out_write, - code_to_inject=code_to_inject, - file_to_inject=file_to_inject, - ) - finally: - console.exit() - if capture_file is not None: - capture_file.close() + def ensure_connected(self): + if self.pyb is None: + do_connect(self, ["auto"]) + def ensure_raw_repl(self, soft_reset=None): + self.ensure_connected() + soft_reset = self._auto_soft_reset if soft_reset is None else soft_reset + if soft_reset or not self.pyb.in_raw_repl: + self.pyb.enter_raw_repl(soft_reset=soft_reset) + self._auto_soft_reset = False -def execbuffer(pyb, buf, follow): - ret_val = 0 - try: - pyb.exec_raw_no_follow(buf) - if follow: - ret, ret_err = pyb.follow(timeout=None, data_consumer=pyboard.stdout_write_bytes) - if ret_err: - pyboard.stdout_write_bytes(ret_err) - ret_val = 1 - except pyboard.PyboardError as er: - print(er) - ret_val = 1 - except KeyboardInterrupt: - ret_val = 1 - return ret_val - - -def print_help(): - def print_commands_help(cmds, help_idx): - max_command_len = max(len(cmd) for cmd in cmds.keys()) - for cmd in sorted(cmds.keys()): - help_message_lines = dedent(cmds[cmd][help_idx]).split("\n") - help_message = help_message_lines[0] - for line in help_message_lines[1:]: - help_message = "{}\n{}{}".format(help_message, " " * (max_command_len + 4), line) - print(" ", cmd, " " * (max_command_len - len(cmd) + 2), help_message, sep="") - - print(_PROG, "-- MicroPython remote control") - print("See https://docs.micropython.org/en/latest/reference/mpremote.html") - - print("\nList of commands:") - print_commands_help(_COMMANDS, 3) - - print("\nList of shortcuts:") - print_commands_help(_command_expansions, 2) - - -def print_version(): - from . import __version__ - - print(f"{_PROG} {__version__}") + def ensure_friendly_repl(self): + self.ensure_connected() + if self.pyb.in_raw_repl: + self.pyb.exit_raw_repl() def main(): @@ -528,106 +342,31 @@ def main(): prepare_command_expansions(config) args = sys.argv[1:] - pyb = None - auto_soft_reset = True - did_action = False + state = State() try: while args: do_command_expansion(args) cmd = args.pop(0) try: - need_raw_repl, is_action, num_args_min, _ = _COMMANDS[cmd] + num_args_min, _help, handler = _COMMANDS[cmd] except KeyError: - print(f"{_PROG}: '{cmd}' is not a command") - return 1 + raise CommandError(f"'{cmd}' is not a command") if len(args) < num_args_min: print(f"{_PROG}: '{cmd}' neads at least {num_args_min} argument(s)") return 1 - if cmd == "connect": - if pyb is not None: - do_disconnect(pyb) - pyb = do_connect(args) - if pyb is None: - did_action = True - continue - elif cmd == "help": - print_help() - sys.exit(0) - elif cmd == "version": - print_version() - sys.exit(0) - elif cmd == "resume": - auto_soft_reset = False - continue - - # The following commands need a connection, and either a raw or friendly REPL. - - if pyb is None: - pyb = do_connect(["auto"]) - - if need_raw_repl: - if not pyb.in_raw_repl: - pyb.enter_raw_repl(soft_reset=auto_soft_reset) - auto_soft_reset = False - else: - if pyb.in_raw_repl: - pyb.exit_raw_repl() - if is_action: - did_action = True - - if cmd == "disconnect": - do_disconnect(pyb) - pyb = None - auto_soft_reset = True - elif cmd == "soft-reset": - pyb.enter_raw_repl(soft_reset=True) - auto_soft_reset = False - elif cmd == "mount": - unsafe_links = False - if args[0] == "--unsafe-links" or args[0] == "-l": - args.pop(0) - unsafe_links = True - path = args.pop(0) - pyb.mount_local(path, unsafe_links=unsafe_links) - print(f"Local directory {path} is mounted at /remote") - elif cmd == "umount": - pyb.umount_local() - elif cmd in ("exec", "eval", "run"): - follow = True - if args[0] == "--no-follow": - args.pop(0) - follow = False - if cmd == "exec": - buf = args.pop(0) - elif cmd == "eval": - buf = "print(" + args.pop(0) + ")" - else: - filename = args.pop(0) - try: - with open(filename, "rb") as f: - buf = f.read() - except OSError: - print(f"{_PROG}: could not read file '{filename}'") - return 1 - ret = execbuffer(pyb, buf, follow) - if ret: - return ret - elif cmd == "fs": - do_filesystem(pyb, args) - elif cmd == "edit": - do_edit(pyb, args) - elif cmd == "repl": - do_repl(pyb, args) - - if not did_action: - if pyb is None: - pyb = do_connect(["auto"]) - if pyb.in_raw_repl: - pyb.exit_raw_repl() - do_repl(pyb, args) + handler(state, args) + + + # If no commands were "actions" then implicitly finish with the REPL. + if state.run_repl_on_completion(): + do_repl(state, args) + + return 0 + except CommandError as e: + print(f"{_PROG}: {e}", file=sys.stderr) + return 1 finally: - if pyb is not None: - do_disconnect(pyb) + do_disconnect(state) diff --git a/tools/mpremote/mpremote/repl.py b/tools/mpremote/mpremote/repl.py new file mode 100644 index 0000000000000..f92d20ae79721 --- /dev/null +++ b/tools/mpremote/mpremote/repl.py @@ -0,0 +1,101 @@ +from .console import Console, ConsolePosix + +from . import pyboardextended as pyboard + + +def do_repl_main_loop(state, console_in, console_out_write, *, code_to_inject, file_to_inject): + while True: + console_in.waitchar(state.pyb.serial) + c = console_in.readchar() + if c: + if c == b"\x1d": # ctrl-], quit + break + elif c == b"\x04": # ctrl-D + # special handling needed for ctrl-D if filesystem is mounted + state.pyb.write_ctrl_d(console_out_write) + elif c == b"\x0a" and code_to_inject is not None: # ctrl-j, inject code + state.pyb.serial.write(code_to_inject) + elif c == b"\x0b" and file_to_inject is not None: # ctrl-k, inject script + console_out_write(bytes("Injecting %s\r\n" % file_to_inject, "utf8")) + state.pyb.enter_raw_repl(soft_reset=False) + with open(file_to_inject, "rb") as f: + pyfile = f.read() + try: + state.pyb.exec_raw_no_follow(pyfile) + except pyboard.PyboardError as er: + console_out_write(b"Error:\r\n") + console_out_write(er) + state.pyb.exit_raw_repl() + else: + state.pyb.serial.write(c) + + try: + n = state.pyb.serial.inWaiting() + except OSError as er: + if er.args[0] == 5: # IO error, device disappeared + print("device disconnected") + break + + if n > 0: + c = state.pyb.serial.read(1) + if c is not None: + # pass character through to the console + oc = ord(c) + if oc in (8, 9, 10, 13, 27) or 32 <= oc <= 126: + console_out_write(c) + else: + console_out_write(b"[%02x]" % ord(c)) + + +def do_repl(state, args): + state.ensure_friendly_repl() + state.did_action() + + capture_file = None + code_to_inject = None + file_to_inject = None + + while len(args): + if args[0] == "--capture": + args.pop(0) + capture_file = args.pop(0) + elif args[0] == "--inject-code": + args.pop(0) + code_to_inject = bytes(args.pop(0).replace("\\n", "\r\n"), "utf8") + elif args[0] == "--inject-file": + args.pop(0) + file_to_inject = args.pop(0) + else: + break + + print("Connected to MicroPython at %s" % state.pyb.device_name) + print("Use Ctrl-] to exit this shell") + if capture_file is not None: + print('Capturing session to file "%s"' % capture_file) + capture_file = open(capture_file, "wb") + if code_to_inject is not None: + print("Use Ctrl-J to inject", code_to_inject) + if file_to_inject is not None: + print('Use Ctrl-K to inject file "%s"' % file_to_inject) + + console = Console() + console.enter() + + def console_out_write(b): + console.write(b) + if capture_file is not None: + capture_file.write(b) + capture_file.flush() + + try: + do_repl_main_loop( + state, + console, + console_out_write, + code_to_inject=code_to_inject, + file_to_inject=file_to_inject, + ) + finally: + console.exit() + if capture_file is not None: + capture_file.close() From 68d094358ec71aa8cdec97e9e6fc3c6d46dedfbf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 30 Sep 2022 14:36:39 +1000 Subject: [PATCH 2663/3533] tools/mpremote: Use argparse for command line parsing. No functional change other than to allow slightly more flexibility in how --foo arguments are specified. This removes all custom handling for --foo args in all commands and replaces it with per-command argparse configs. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tools/mpremote/mpremote/commands.py | 78 ++++------ tools/mpremote/mpremote/main.py | 224 ++++++++++++++++++++-------- tools/mpremote/mpremote/repl.py | 19 +-- 3 files changed, 193 insertions(+), 128 deletions(-) diff --git a/tools/mpremote/mpremote/commands.py b/tools/mpremote/mpremote/commands.py index 60a625d5e854c..bf56df69993a8 100644 --- a/tools/mpremote/mpremote/commands.py +++ b/tools/mpremote/mpremote/commands.py @@ -10,6 +10,9 @@ class CommandError(Exception): pass + +def do_connect(state, args=None): + dev = args.device[0] if args else "auto" do_disconnect(state) try: @@ -101,19 +104,6 @@ def show_progress_bar(size, total_size, op="copying"): ) -# Get all args up to the terminator ("+"). -# The passed args will be updated with these ones removed. -def _get_fs_args(args): - n = 0 - for src in args: - if src == "+": - break - n += 1 - fs_args = args[:n] - args[:] = args[n + 1 :] - return fs_args - - def do_filesystem(state, args): state.ensure_raw_repl() state.did_action() @@ -125,20 +115,22 @@ def _list_recursive(files, path): else: files.append(os.path.split(path)) - fs_args = _get_fs_args(args) + command = args.command[0] + paths = args.path - # Don't be verbose when using cat, so output can be redirected to something. - verbose = fs_args[0] != "cat" + if command == "cat": + # Don't be verbose by default when using cat, so output can be + # redirected to something. + verbose = args.verbose == True + else: + verbose = args.verbose != False - if fs_args[0] == "cp" and fs_args[1] == "-r": - fs_args.pop(0) - fs_args.pop(0) - if fs_args[-1] != ":": - print(f"{_PROG}: 'cp -r' destination must be ':'") - sys.exit(1) - fs_args.pop() + if command == "cp" and args.recursive: + if paths[-1] != ":": + raise CommandError("'cp -r' destination must be ':'") + paths.pop() src_files = [] - for path in fs_args: + for path in paths: if path.startswith(":"): raise CommandError("'cp -r' source files must be local") _list_recursive(src_files, path) @@ -158,9 +150,11 @@ def _list_recursive(files, path): verbose=verbose, ) else: + if args.recursive: + raise CommandError("'-r' only supported for 'cp'") try: pyboard.filesystem_command( - state.pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose + state.pyb, [command] + paths, progress_callback=show_progress_bar, verbose=verbose ) except OSError as er: raise CommandError(er) @@ -172,7 +166,7 @@ def do_edit(state, args): if not os.getenv("EDITOR"): raise pyboard.PyboardError("edit: $EDITOR not set") - for src in _get_fs_args(args): + for src in args.files: src = src.lstrip(":") dest_fd, dest = tempfile.mkstemp(suffix=os.path.basename(src)) try: @@ -186,14 +180,6 @@ def do_edit(state, args): os.unlink(dest) -def _get_follow_arg(args): - if args[0] == "--no-follow": - args.pop(0) - return False - else: - return True - - def _do_execbuffer(state, buf, follow): state.ensure_raw_repl() state.did_action() @@ -213,38 +199,28 @@ def _do_execbuffer(state, buf, follow): def do_exec(state, args): - follow = _get_follow_arg(args) - buf = args.pop(0) - _do_execbuffer(state, buf, follow) + _do_execbuffer(state, args.expr[0], args.follow) def do_eval(state, args): - follow = _get_follow_arg(args) - buf = "print(" + args.pop(0) + ")" - _do_execbuffer(state, buf, follow) + buf = "print(" + args.expr[0] + ")" + _do_execbuffer(state, buf, args.follow) def do_run(state, args): - follow = _get_follow_arg(args) - filename = args.pop(0) + filename = args.path[0] try: with open(filename, "rb") as f: buf = f.read() except OSError: raise CommandError(f"could not read file '{filename}'") - sys.exit(1) - _do_execbuffer(state, buf, follow) + _do_execbuffer(state, buf, args.follow) def do_mount(state, args): state.ensure_raw_repl() - - unsafe_links = False - if args[0] == "--unsafe-links" or args[0] == "-l": - args.pop(0) - unsafe_links = True - path = args.pop(0) - state.pyb.mount_local(path, unsafe_links=unsafe_links) + path = args.path[0] + state.pyb.mount_local(path, unsafe_links=args.unsafe_links) print(f"Local directory {path} is mounted at /remote") diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index b96e3f46b1bf6..17d2b33738344 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -17,6 +17,7 @@ mpremote repl -- enter REPL """ +import argparse import os, sys from collections.abc import Mapping from textwrap import dedent @@ -41,10 +42,10 @@ def do_help(state, _args=None): - def print_commands_help(cmds, help_idx): + def print_commands_help(cmds, help_key): max_command_len = max(len(cmd) for cmd in cmds.keys()) for cmd in sorted(cmds.keys()): - help_message_lines = dedent(cmds[cmd][help_idx]).split("\n") + help_message_lines = dedent(help_key(cmds[cmd])).split("\n") help_message = help_message_lines[0] for line in help_message_lines[1:]: help_message = "{}\n{}{}".format(help_message, " " * (max_command_len + 4), line) @@ -54,10 +55,12 @@ def print_commands_help(cmds, help_idx): print("See https://docs.micropython.org/en/latest/reference/mpremote.html") print("\nList of commands:") - print_commands_help(_COMMANDS, 1) + print_commands_help( + _COMMANDS, lambda x: x[1]().description + ) # extract description from argparse print("\nList of shortcuts:") - print_commands_help(_command_expansions, 2) + print_commands_help(_command_expansions, lambda x: x[2]) # (args, sub, help_message) sys.exit(0) @@ -69,89 +72,157 @@ def do_version(state, _args=None): sys.exit(0) -# Map of "command" to tuple of (num_args_min, help_text, handler). +def _bool_flag(cmd_parser, name, short_name, default, description): + # In Python 3.9+ this can be replaced with argparse.BooleanOptionalAction. + group = cmd_parser.add_mutually_exclusive_group() + group.add_argument( + "--" + name, + "-" + short_name, + action="store_true", + default=default, + help=description, + ) + group.add_argument( + "--no-" + name, + action="store_false", + dest=name, + ) + + +def argparse_connect(): + cmd_parser = argparse.ArgumentParser(description="connect to given device") + cmd_parser.add_argument( + "device", nargs=1, help="Either list, auto, id:x, port:x, or any valid device name/path" + ) + return cmd_parser + + +def argparse_edit(): + cmd_parser = argparse.ArgumentParser(description="edit files on the device") + cmd_parser.add_argument("files", nargs="+", help="list of remote paths") + return cmd_parser + + +def argparse_mount(): + cmd_parser = argparse.ArgumentParser(description="mount local directory on device") + _bool_flag( + cmd_parser, + "unsafe-links", + "l", + False, + "follow symbolic links pointing outside of local directory", + ) + cmd_parser.add_argument("path", nargs=1, help="local path to mount") + return cmd_parser + + +def argparse_repl(): + cmd_parser = argparse.ArgumentParser(description="connect to given device") + cmd_parser.add_argument("--capture", type=str, required=False, help="TODO") + cmd_parser.add_argument("--inject-code", type=str, required=False, help="TODO") + cmd_parser.add_argument("--inject-file", type=str, required=False, help="TODO") + return cmd_parser + + +def argparse_eval(): + cmd_parser = argparse.ArgumentParser(description="evaluate and print the string") + _bool_flag(cmd_parser, "follow", "f", True, "TODO") + cmd_parser.add_argument("expr", nargs=1, help="expression to execute") + return cmd_parser + + +def argparse_exec(): + cmd_parser = argparse.ArgumentParser(description="execute the string") + _bool_flag(cmd_parser, "follow", "f", True, "TODO") + cmd_parser.add_argument("expr", nargs=1, help="expression to execute") + return cmd_parser + + +def argparse_run(): + cmd_parser = argparse.ArgumentParser(description="run the given local script") + _bool_flag(cmd_parser, "follow", "f", False, "TODO") + cmd_parser.add_argument("path", nargs=1, help="expression to execute") + return cmd_parser + + +def argparse_filesystem(): + cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device") + _bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)") + _bool_flag( + cmd_parser, + "verbose", + "v", + None, + "enable verbose output (defaults to True for all commands except cat)", + ) + cmd_parser.add_argument( + "command", nargs=1, help="filesystem command (e.g. cat, cp, ls, rm, touch)" + ) + cmd_parser.add_argument("path", nargs="+", help="local and remote paths") + return cmd_parser + + +def argparse_none(description): + return lambda: argparse.ArgumentParser(description=description) + + +# Map of "command" to tuple of (handler_func, argparse_func). _COMMANDS = { "connect": ( - 1, - """\ - connect to given device - device may be: list, auto, id:x, port:x - or any valid device name/path""", do_connect, + argparse_connect, ), "disconnect": ( - 0, - "disconnect current device", do_disconnect, + argparse_none("disconnect current device"), ), "edit": ( - 1, - "edit files on the device", do_edit, + argparse_edit, ), "resume": ( - 0, - "resume a previous mpremote session (will not auto soft-reset)", do_resume, + argparse_none("resume a previous mpremote session (will not auto soft-reset)"), ), "soft-reset": ( - 0, - "perform a soft-reset of the device", do_soft_reset, + argparse_none("perform a soft-reset of the device"), ), "mount": ( - 1, - """\ - mount local directory on device - options: - --unsafe-links, -l - follow symbolic links pointing outside of local directory""", do_mount, + argparse_mount, ), "umount": ( - 0, - "unmount the local directory", do_umount, + argparse_none("unmount the local directory"), ), "repl": ( - 0, - """\ - enter REPL - options: - --capture - --inject-code - --inject-file """, do_repl, + argparse_repl, ), "eval": ( - 1, - "evaluate and print the string", do_eval, + argparse_eval, ), "exec": ( - 1, - "execute the string", do_exec, + argparse_exec, ), "run": ( - 1, - "run the given local script", do_run, + argparse_run, ), "fs": ( - 1, - "execute filesystem commands on the device", do_filesystem, + argparse_filesystem, ), "help": ( - 0, - "print help and exit", do_help, + argparse_none("print help and exit"), ), "version": ( - 0, - "print version and exit", do_version, + argparse_none("print version and exit"), ), } @@ -301,7 +372,6 @@ def usage_error(cmd, exp_args, msg): # Extra unknown arguments given. arg = args[last_arg_idx].split("=", 1)[0] usage_error(cmd, exp_args, f"given unexpected argument {arg}") - sys.exit(1) # Insert expansion with optional setting of arguments. if pre: @@ -322,7 +392,7 @@ def run_repl_on_completion(self): def ensure_connected(self): if self.pyb is None: - do_connect(self, ["auto"]) + do_connect(self) def ensure_raw_repl(self, soft_reset=None): self.ensure_connected() @@ -341,28 +411,60 @@ def main(): config = load_user_config() prepare_command_expansions(config) - args = sys.argv[1:] + remaining_args = sys.argv[1:] state = State() try: - while args: - do_command_expansion(args) - cmd = args.pop(0) + while remaining_args: + # Skip the terminator. + if remaining_args[0] == "+": + remaining_args.pop(0) + continue + + # Rewrite the front of the list with any matching expansion. + do_command_expansion(remaining_args) + + # The (potentially rewritten) command must now be a base command. + cmd = remaining_args.pop(0) try: - num_args_min, _help, handler = _COMMANDS[cmd] + handler_func, parser_func = _COMMANDS[cmd] except KeyError: raise CommandError(f"'{cmd}' is not a command") - if len(args) < num_args_min: - print(f"{_PROG}: '{cmd}' neads at least {num_args_min} argument(s)") - return 1 - - handler(state, args) - - - # If no commands were "actions" then implicitly finish with the REPL. + # If this command (or any down the chain) has a terminator, then + # limit the arguments passed for this command. They will be added + # back after processing this command. + try: + terminator = remaining_args.index("+") + command_args = remaining_args[:terminator] + extra_args = remaining_args[terminator:] + except ValueError: + command_args = remaining_args + extra_args = [] + + # Special case: "fs ls" allowed have no path specified. + if cmd == "fs" and len(command_args) == 1 and command_args[0] == "ls": + command_args.append("") + + # Use the command-specific argument parser. + cmd_parser = parser_func() + cmd_parser.prog = cmd + # Catch all for unhandled positional arguments (this is the next command). + cmd_parser.add_argument( + "next_command", nargs=argparse.REMAINDER, help=f"Next {_PROG} command" + ) + args = cmd_parser.parse_args(command_args) + + # Execute command. + handler_func(state, args) + + # Get any leftover unprocessed args. + remaining_args = args.next_command + extra_args + + # If no commands were "actions" then implicitly finish with the REPL + # using default args. if state.run_repl_on_completion(): - do_repl(state, args) + do_repl(state, argparse_repl().parse_args([])) return 0 except CommandError as e: diff --git a/tools/mpremote/mpremote/repl.py b/tools/mpremote/mpremote/repl.py index f92d20ae79721..7da00c0fdef0c 100644 --- a/tools/mpremote/mpremote/repl.py +++ b/tools/mpremote/mpremote/repl.py @@ -51,22 +51,9 @@ def do_repl(state, args): state.ensure_friendly_repl() state.did_action() - capture_file = None - code_to_inject = None - file_to_inject = None - - while len(args): - if args[0] == "--capture": - args.pop(0) - capture_file = args.pop(0) - elif args[0] == "--inject-code": - args.pop(0) - code_to_inject = bytes(args.pop(0).replace("\\n", "\r\n"), "utf8") - elif args[0] == "--inject-file": - args.pop(0) - file_to_inject = args.pop(0) - else: - break + capture_file = args.capture + code_to_inject = args.inject_code + file_to_inject = args.inject_file print("Connected to MicroPython at %s" % state.pyb.device_name) print("Use Ctrl-] to exit this shell") From 12ca918eb2ac062f6e6df0772e528eef9d050cb7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 29 Sep 2022 00:45:34 +1000 Subject: [PATCH 2664/3533] tools/mpremote: Add `mpremote mip install` to install packages. This supports the same package sources as the new `mip` tool. - micropython-lib (by name) - http(s) & github packages with json description - directly downloading a .py/.mpy file The version is specified with an optional `@version` on the end of the package name. The target dir, index, and mpy/no-mpy can be set through command line args. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 14 +++ docs/reference/packages.rst | 16 +-- tools/mpremote/README.md | 41 ++++--- tools/mpremote/mpremote/main.py | 28 +++++ tools/mpremote/mpremote/mip.py | 191 ++++++++++++++++++++++++++++++++ tools/pyboard.py | 7 ++ 6 files changed, 272 insertions(+), 25 deletions(-) create mode 100644 tools/mpremote/mpremote/mip.py diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index e3902f8e5d7e9..bb0686237abd4 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -146,6 +146,14 @@ The full list of supported commands are: variable ``$EDITOR``). If the editor exits successfully, the updated file will be copied back to the device. +- install packages from :term:`micropython-lib` (or GitHub) using the ``mip`` tool: + + .. code-block:: bash + + $ mpremote mip install + + See :ref:`packages` for more information. + - mount the local directory on the remote device: .. code-block:: bash @@ -269,3 +277,9 @@ Examples mpremote cp -r dir/ : mpremote cp a.py b.py : + repl + + mpremote mip install aioble + + mpremote mip install github:org/repo@branch + + mpremote mip install --target /flash/third-party functools diff --git a/docs/reference/packages.rst b/docs/reference/packages.rst index 0c049d1fb2fc2..1ddbecb582b4a 100644 --- a/docs/reference/packages.rst +++ b/docs/reference/packages.rst @@ -78,17 +78,17 @@ The :term:`mpremote` tool also includes the same functionality as ``mip`` and can be used from a host PC to install packages to a locally connected device (e.g. via USB or UART):: - $ mpremote install pkgname - $ mpremote install pkgname@x.y - $ mpremote install http://example.com/x/y/foo.py - $ mpremote install github:org/repo - $ mpremote install github:org/repo@branch-or-tag + $ mpremote mip install pkgname + $ mpremote mip install pkgname@x.y + $ mpremote mip install http://example.com/x/y/foo.py + $ mpremote mip install github:org/repo + $ mpremote mip install github:org/repo@branch-or-tag The ``--target=path``, ``--no-mpy``, and ``--index`` arguments can be set:: - $ mpremote install --target=/flash/third-party pkgname - $ mpremote install --no-mpy pkgname - $ mpremote install --index https://host/pi pkgname + $ mpremote mip install --target=/flash/third-party pkgname + $ mpremote mip install --no-mpy pkgname + $ mpremote mip install --index https://host/pi pkgname Installing packages manually ---------------------------- diff --git a/tools/mpremote/README.md b/tools/mpremote/README.md index c294b20811821..7f58788fbe4cb 100644 --- a/tools/mpremote/README.md +++ b/tools/mpremote/README.md @@ -11,23 +11,28 @@ This will automatically connect to the device and provide an interactive REPL. The full list of supported commands are: - mpremote connect -- connect to given device - device may be: list, auto, id:x, port:x - or any valid device name/path - mpremote disconnect -- disconnect current device - mpremote mount -- mount local directory on device - mpremote eval -- evaluate and print the string - mpremote exec -- execute the string - mpremote run -- run the given local script - mpremote fs -- execute filesystem commands on the device - command may be: cat, ls, cp, rm, mkdir, rmdir - use ":" as a prefix to specify a file on the device - mpremote repl -- enter REPL - options: - --capture - --inject-code - --inject-file - mpremote help -- print list of commands and exit + mpremote connect -- connect to given device + device may be: list, auto, id:x, port:x + or any valid device name/path + mpremote disconnect -- disconnect current device + mpremote mount -- mount local directory on device + mpremote eval -- evaluate and print the string + mpremote exec -- execute the string + mpremote run -- run the given local script + mpremote fs -- execute filesystem commands on the device + command may be: cat, ls, cp, rm, mkdir, rmdir + use ":" as a prefix to specify a file on the device + mpremote repl -- enter REPL + options: + --capture + --inject-code + --inject-file + mpremote mip install -- Install packages (from micropython-lib or third-party sources) + options: + --target + --index + --no-mpy + mpremote help -- print list of commands and exit Multiple commands can be specified and they will be run sequentially. Connection and disconnection will be done automatically at the start and end of the execution @@ -73,3 +78,5 @@ Examples: mpremote cp :main.py . mpremote cp main.py : mpremote cp -r dir/ : + mpremote mip install aioble + mpremote mip install github:org/repo@branch diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index 17d2b33738344..4f541685a0cfd 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -36,6 +36,7 @@ do_resume, do_soft_reset, ) +from .mip import do_mip from .repl import do_repl _PROG = "mpremote" @@ -162,6 +163,29 @@ def argparse_filesystem(): return cmd_parser +def argparse_mip(): + cmd_parser = argparse.ArgumentParser( + description="install packages from micropython-lib or third-party sources" + ) + _bool_flag(cmd_parser, "mpy", "m", True, "download as compiled .mpy files (default)") + cmd_parser.add_argument( + "--target", type=str, required=False, help="destination direction on the device" + ) + cmd_parser.add_argument( + "--index", + type=str, + required=False, + help="package index to use (defaults to micropython-lib)", + ) + cmd_parser.add_argument("command", nargs=1, help="mip command (e.g. install)") + cmd_parser.add_argument( + "packages", + nargs="+", + help="list package specifications, e.g. name, name@version, github:org/repo, github:org/repo@branch", + ) + return cmd_parser + + def argparse_none(description): return lambda: argparse.ArgumentParser(description=description) @@ -216,6 +240,10 @@ def argparse_none(description): do_filesystem, argparse_filesystem, ), + "mip": ( + do_mip, + argparse_mip, + ), "help": ( do_help, argparse_none("print help and exit"), diff --git a/tools/mpremote/mpremote/mip.py b/tools/mpremote/mpremote/mip.py new file mode 100644 index 0000000000000..99ca9ff7e3878 --- /dev/null +++ b/tools/mpremote/mpremote/mip.py @@ -0,0 +1,191 @@ +# Micropython package installer +# Ported from micropython-lib/micropython/mip/mip.py. +# MIT license; Copyright (c) 2022 Jim Mussared + +import urllib.error +import urllib.request +import json +import tempfile +import os + +from .commands import CommandError, show_progress_bar + + +_PACKAGE_INDEX = "https://micropython.org/pi/v2" +_CHUNK_SIZE = 128 + + +# This implements os.makedirs(os.dirname(path)) +def _ensure_path_exists(pyb, path): + import os + + split = path.split("/") + + # Handle paths starting with "/". + if not split[0]: + split.pop(0) + split[0] = "/" + split[0] + + prefix = "" + for i in range(len(split) - 1): + prefix += split[i] + if not pyb.fs_exists(prefix): + pyb.fs_mkdir(prefix) + prefix += "/" + + +# Copy from src (stream) to dest (function-taking-bytes) +def _chunk(src, dest, length=None, op="downloading"): + buf = memoryview(bytearray(_CHUNK_SIZE)) + total = 0 + if length: + show_progress_bar(0, length, op) + while True: + n = src.readinto(buf) + if n == 0: + break + dest(buf if n == _CHUNK_SIZE else buf[:n]) + total += n + if length: + show_progress_bar(total, length, op) + + +def _rewrite_url(url, branch=None): + if not branch: + branch = "HEAD" + if url.startswith("github:"): + url = url[7:].split("/") + url = ( + "https://raw.githubusercontent.com/" + + url[0] + + "/" + + url[1] + + "/" + + branch + + "/" + + "/".join(url[2:]) + ) + return url + + +def _download_file(pyb, url, dest): + try: + with urllib.request.urlopen(url) as src: + fd, path = tempfile.mkstemp() + try: + print("Installing:", dest) + with os.fdopen(fd, "wb") as f: + _chunk(src, f.write, src.length) + _ensure_path_exists(pyb, dest) + pyb.fs_put(path, dest, progress_callback=show_progress_bar) + finally: + os.unlink(path) + except urllib.error.HTTPError as e: + if e.status == 404: + raise CommandError(f"File not found: {url}") + else: + raise CommandError(f"Error {e.status} requesting {url}") + except urllib.error.URLError as e: + raise CommandError(f"{e.reason} requesting {url}") + + +def _install_json(pyb, package_json_url, index, target, version, mpy): + try: + with urllib.request.urlopen(_rewrite_url(package_json_url, version)) as response: + package_json = json.load(response) + except urllib.error.HTTPError as e: + if e.status == 404: + raise CommandError(f"Package not found: {package_json_url}") + else: + raise CommandError(f"Error {e.status} requesting {package_json_url}") + except urllib.error.URLError as e: + raise CommandError(f"{e.reason} requesting {package_json_url}") + for target_path, short_hash in package_json.get("hashes", ()): + fs_target_path = target + "/" + target_path + file_url = f"{index}/file/{short_hash[:2]}/{short_hash}" + _download_file(pyb, file_url, fs_target_path) + for target_path, url in package_json.get("urls", ()): + fs_target_path = target + "/" + target_path + _download_file(pyb, _rewrite_url(url, version), fs_target_path) + for dep, dep_version in package_json.get("deps", ()): + _install_package(pyb, dep, index, target, dep_version, mpy) + + +def _install_package(pyb, package, index, target, version, mpy): + if ( + package.startswith("http://") + or package.startswith("https://") + or package.startswith("github:") + ): + if package.endswith(".py") or package.endswith(".mpy"): + print(f"Downloading {package} to {target}") + _download_file( + pyb, _rewrite_url(package, version), target + "/" + package.rsplit("/")[-1] + ) + return + else: + if not package.endswith(".json"): + if not package.endswith("/"): + package += "/" + package += "package.json" + print(f"Installing {package} to {target}") + else: + if not version: + version = "latest" + print(f"Installing {package} ({version}) from {index} to {target}") + + mpy_version = "py" + if mpy: + pyb.exec("import sys") + mpy_version = ( + int(pyb.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF").decode()) or "py" + ) + + package = f"{index}/package/{mpy_version}/{package}/{version}.json" + + _install_json(pyb, package, index, target, version, mpy) + + +def do_mip(state, args): + state.did_action() + + if args.command[0] == "install": + state.ensure_raw_repl() + + for package in args.packages: + version = None + if "@" in package: + package, version = package.split("@") + + print("Install", package) + + if args.index is None: + args.index = _PACKAGE_INDEX + + if args.target is None: + state.pyb.exec("import sys") + lib_paths = ( + state.pyb.eval("'\\n'.join(p for p in sys.path if p.endswith('/lib'))") + .decode() + .split("\n") + ) + if lib_paths and lib_paths[0]: + args.target = lib_paths[0] + else: + raise CommandError( + "Unable to find lib dir in sys.path, use --target to override" + ) + + if args.mpy is None: + args.mpy = True + + try: + _install_package( + state.pyb, package, args.index.rstrip("/"), args.target, version, args.mpy + ) + except CommandError: + print("Package may be partially installed") + raise + print("Done") + else: + raise CommandError(f"mip: '{args.command[0]}' is not a command") diff --git a/tools/pyboard.py b/tools/pyboard.py index 60cc06508ebef..043f4f06fb87f 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -476,6 +476,13 @@ def get_time(self): t = str(self.eval("pyb.RTC().datetime()"), encoding="utf8")[1:-1].split(", ") return int(t[4]) * 3600 + int(t[5]) * 60 + int(t[6]) + def fs_exists(self, src): + try: + self.exec_("import uos\nuos.stat(%s)" % (("'%s'" % src) if src else "")) + return True + except PyboardError: + return False + def fs_ls(self, src): cmd = ( "import uos\nfor f in uos.ilistdir(%s):\n" From 7705b9b9d50b3665de135f314fd1f8cb5d0641f0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 30 Sep 2022 23:43:23 +1000 Subject: [PATCH 2665/3533] tools/pyboard.py: Handle unsupported fs command. Signed-off-by: Jim Mussared --- tools/pyboard.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index 043f4f06fb87f..55c00fbca1944 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -621,23 +621,28 @@ def fname_cp_dest(src, dest): dest2 = fname_cp_dest(src2, fname_remote(dest)) op(src2, dest2, progress_callback=progress_callback) else: - op = { + ops = { "cat": pyb.fs_cat, "ls": pyb.fs_ls, "mkdir": pyb.fs_mkdir, "rm": pyb.fs_rm, "rmdir": pyb.fs_rmdir, "touch": pyb.fs_touch, - }[cmd] + } + if cmd not in ops: + raise PyboardError("'{}' is not a filesystem command".format(cmd)) if cmd == "ls" and not args: args = [""] for src in args: src = fname_remote(src) if verbose: print("%s :%s" % (cmd, src)) - op(src) + ops[cmd](src) except PyboardError as er: - print(str(er.args[2], "ascii")) + if len(er.args) > 1: + print(str(er.args[2], "ascii")) + else: + print(er) pyb.exit_raw_repl() pyb.close() sys.exit(1) From 0ee877a20732039e603b41c63a221bfc2c8fbde9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 Oct 2022 11:47:06 +1100 Subject: [PATCH 2666/3533] esp32/machine_i2s: Add I2S finaliser which calls deinit(). So that the FreeRTOS resources can be freed, eg on soft reset. Fixes issue #9366. Signed-off-by: Damien George --- ports/esp32/machine_i2s.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index eecf71549871f..ce1cb59849dc1 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -533,7 +533,8 @@ STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_arg machine_i2s_obj_t *self; if (MP_STATE_PORT(machine_i2s_obj)[port] == NULL) { - self = mp_obj_malloc(machine_i2s_obj_t, &machine_i2s_type); + self = m_new_obj_with_finaliser(machine_i2s_obj_t); + self->base.type = &machine_i2s_type; MP_STATE_PORT(machine_i2s_obj)[port] = self; self->port = port; } else { @@ -688,6 +689,7 @@ STATIC const mp_rom_map_elem_t machine_i2s_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_i2s_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_i2s_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_i2s_deinit_obj) }, // Static method { MP_ROM_QSTR(MP_QSTR_shift), MP_ROM_PTR(&machine_i2s_shift_obj) }, From bdac8272d8b1a726012bec897dc51dacbe70b295 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 29 Aug 2022 16:50:49 +1000 Subject: [PATCH 2667/3533] tools: Add note about uncrustify versions. Uncrustify versions are not mutually compatible: 1. Version 0.73 or newer produce slightly different formatting. It may be possible to tweak these by adding more config items, but this will cause older versions to error out with 'Unknown option'. 2. Version 0.75 prints a range of deprecation warnings due to config file changes, and returns a non-zero exit code. These are actually fixable as most are the default value, and pp_indent has changed from 'true' to '1' which is backwards compatible. However issue 1 remains, so probably better to have it fail explicitly. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- CODECONVENTIONS.md | 4 ++++ tools/uncrustify.cfg | 3 +++ 2 files changed, 7 insertions(+) diff --git a/CODECONVENTIONS.md b/CODECONVENTIONS.md index 78fb912a6ab75..bceab746182c4 100644 --- a/CODECONVENTIONS.md +++ b/CODECONVENTIONS.md @@ -65,6 +65,10 @@ changes to the correct style. Without arguments this tool will reformat all source code (and may take some time to run). Otherwise pass as arguments to the tool the files that changed and it will only reformat those. +**Important**: Use only [uncrustify](https://github.com/uncrustify/uncrustify) +v0.71 or v0.72 for MicroPython. Different uncrustify versions produce slightly +different formatting, and the configuration file formats are often incompatible. + Python code conventions ======================= diff --git a/tools/uncrustify.cfg b/tools/uncrustify.cfg index 221fb458e9132..28eb49faf7e7a 100644 --- a/tools/uncrustify.cfg +++ b/tools/uncrustify.cfg @@ -1,5 +1,8 @@ # Uncrustify-0.71.0_f +# IMPORTANT: Output is different if using Uncrustify 0.73 or newer, and config file format has changed in newer versions. +# Use version 0.71 or 0.72 to get matching code formatting. + # # General options # From 0e35c4de9b9d55f9288eeb908efd2f7f577dc13b Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 29 Aug 2022 17:30:14 +1000 Subject: [PATCH 2668/3533] tools: Add pre-commit support. Tweak the existing codeformat.py and verifygitlog.py to allow them to be easily called by pre-commit. (This turned out to be easier than using any existing pre-commit hooks, without making subtle changes in the formatting.) This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- .pre-commit-config.yaml | 13 ++++++ CODECONVENTIONS.md | 32 ++++++++++++++ tools/codeformat.py | 15 +++++++ tools/verifygitlog.py | 97 ++++++++++++++++++++++++++--------------- 4 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000..12f3d79c93533 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: + - repo: local + hooks: + - id: codeformat + name: MicroPython codeformat.py for changed files + entry: tools/codeformat.py -v -f + language: python + - id: verifygitlog + name: MicroPython git commit message format checker + entry: tools/verifygitlog.py --check-file --ignore-rebase + language: python + verbose: true + stages: [commit-msg] diff --git a/CODECONVENTIONS.md b/CODECONVENTIONS.md index bceab746182c4..2daea8431f99a 100644 --- a/CODECONVENTIONS.md +++ b/CODECONVENTIONS.md @@ -69,6 +69,38 @@ the tool the files that changed and it will only reformat those. v0.71 or v0.72 for MicroPython. Different uncrustify versions produce slightly different formatting, and the configuration file formats are often incompatible. +Automatic Pre-Commit Hooks +========================== + +To have code formatting and commit message conventions automatically checked +using [pre-commit](https://pre-commit.com/), run the following commands in your +local MicroPython directory: + +``` +$ pip install pre-commit + +$ pre-commit install + +$ pre-commit install --hook-type commit-msg +``` + +pre-commit will now automatically run during `git commit` for both code and +commit message formatting. + +The same formatting checks will be run by CI for any Pull Request submitted to +MicroPython. Pre-commit allows you to see any failure more quickly, and in many +cases will automatically correct it in your local working copy. + +Tips: + +* To skip pre-commit checks on a single commit, use `git commit -n` (for + `--no-verify`). +* To ignore the pre-commit message format check temporarily, start the commit + message subject line with "WIP" (for "Work In Progress"). + +(It is also possible to install pre-commit using Brew or other sources, see +[the docs](https://pre-commit.com/index.html#install) for details.) + Python code conventions ======================= diff --git a/tools/codeformat.py b/tools/codeformat.py index 1c865663a495c..13a699065e30c 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -151,6 +151,11 @@ def main(): cmd_parser.add_argument("-c", action="store_true", help="Format C code only") cmd_parser.add_argument("-p", action="store_true", help="Format Python code only") cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output") + cmd_parser.add_argument( + "-f", + action="store_true", + help="Filter files provided on the command line against the default list of files to check.", + ) cmd_parser.add_argument("files", nargs="*", help="Run on specific globs") args = cmd_parser.parse_args() @@ -162,6 +167,16 @@ def main(): files = [] if args.files: files = list_files(args.files) + if args.f: + # Filter against the default list of files. This is a little fiddly + # because we need to apply both the inclusion globs given in PATHS + # as well as the EXCLUSIONS, and use absolute paths + files = set(os.path.abspath(f) for f in files) + all_files = set(list_files(PATHS, EXCLUSIONS, TOP)) + if args.v: # In verbose mode, log any files we're skipping + for f in files - all_files: + print("Not checking: {}".format(f)) + files = list(files & all_files) else: files = list_files(PATHS, EXCLUSIONS, TOP) diff --git a/tools/verifygitlog.py b/tools/verifygitlog.py index ce3679125617f..f9d98106d6f3c 100755 --- a/tools/verifygitlog.py +++ b/tools/verifygitlog.py @@ -7,6 +7,8 @@ verbosity = 0 # Show what's going on, 0 1 or 2. suggestions = 1 # Set to 0 to not include lengthy suggestions in error messages. +ignore_prefixes = [] + def verbose(*args): if verbosity: @@ -18,6 +20,22 @@ def very_verbose(*args): print(*args) +class ErrorCollection: + # Track errors and warnings as the program runs + def __init__(self): + self.has_errors = False + self.has_warnings = False + self.prefix = "" + + def error(self, text): + print("error: {}{}".format(self.prefix, text)) + self.has_errors = True + + def warning(self, text): + print("warning: {}{}".format(self.prefix, text)) + self.has_warnings = True + + def git_log(pretty_format, *args): # Delete pretty argument from user args so it doesn't interfere with what we do. args = ["git", "log"] + [arg for arg in args if "--pretty" not in args] @@ -28,83 +46,88 @@ def git_log(pretty_format, *args): yield line.decode().rstrip("\r\n") -def verify(sha): +def verify(sha, err): verbose("verify", sha) - errors = [] - warnings = [] - - def error_text(err): - return "commit " + sha + ": " + err - - def error(err): - errors.append(error_text(err)) - - def warning(err): - warnings.append(error_text(err)) + err.prefix = "commit " + sha + ": " # Author and committer email. for line in git_log("%ae%n%ce", sha, "-n1"): very_verbose("email", line) if "noreply" in line: - error("Unwanted email address: " + line) + err.error("Unwanted email address: " + line) # Message body. raw_body = list(git_log("%B", sha, "-n1")) + verify_message_body(raw_body, err) + + +def verify_message_body(raw_body, err): if not raw_body: - error("Message is empty") - return errors, warnings + err.error("Message is empty") + return # Subject line. subject_line = raw_body[0] + for prefix in ignore_prefixes: + if subject_line.startswith(prefix): + verbose("Skipping ignored commit message") + return very_verbose("subject_line", subject_line) subject_line_format = r"^[^!]+: [A-Z]+.+ .+\.$" if not re.match(subject_line_format, subject_line): - error("Subject line should match " + repr(subject_line_format) + ": " + subject_line) + err.error("Subject line should match " + repr(subject_line_format) + ": " + subject_line) if len(subject_line) >= 73: - error("Subject line should be 72 or less characters: " + subject_line) + err.error("Subject line should be 72 or less characters: " + subject_line) # Second one divides subject and body. if len(raw_body) > 1 and raw_body[1]: - error("Second message line should be empty: " + raw_body[1]) + err.error("Second message line should be empty: " + raw_body[1]) # Message body lines. for line in raw_body[2:]: # Long lines with URLs are exempt from the line length rule. if len(line) >= 76 and "://" not in line: - error("Message lines should be 75 or less characters: " + line) + err.error("Message lines should be 75 or less characters: " + line) if not raw_body[-1].startswith("Signed-off-by: ") or "@" not in raw_body[-1]: - warning("Message should be signed-off") - - return errors, warnings + err.warning("Message should be signed-off") def run(args): verbose("run", *args) - has_errors = False - has_warnings = False - for sha in git_log("%h", *args): - errors, warnings = verify(sha) - has_errors |= any(errors) - has_warnings |= any(warnings) - for err in errors: - print("error:", err) - for err in warnings: - print("warning:", err) - if has_errors or has_warnings: + + err = ErrorCollection() + + if "--check-file" in args: + filename = args[-1] + verbose("checking commit message from", filename) + with open(args[-1]) as f: + lines = [line.rstrip("\r\n") for line in f] + verify_message_body(lines, err) + else: # Normal operation, pass arguments to git log + for sha in git_log("%h", *args): + verify(sha, err) + + if err.has_errors or err.has_warnings: if suggestions: print("See https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md") else: print("ok") - if has_errors: + if err.has_errors: sys.exit(1) def show_help(): - print("usage: verifygitlog.py [-v -n -h] ...") + print("usage: verifygitlog.py [-v -n -h --check-file] ...") print("-v : increase verbosity, can be speficied multiple times") print("-n : do not print multi-line suggestions") print("-h : print this help message and exit") + print( + "--check-file : Pass a single argument which is a file containing a candidate commit message" + ) + print( + "--ignore-rebase : Skip checking commits with git rebase autosquash prefixes or WIP as a prefix" + ) print("... : arguments passed to git log to retrieve commits to verify") print(" see https://www.git-scm.com/docs/git-log") print(" passing no arguments at all will verify all commits") @@ -117,6 +140,10 @@ def show_help(): args = sys.argv[1:] verbosity = args.count("-v") suggestions = args.count("-n") == 0 + if "--ignore-rebase" in args: + args.remove("--ignore-rebase") + ignore_prefixes = ["squash!", "fixup!", "amend!", "WIP"] + if "-h" in args: show_help() else: From 95c614e2b60b5c1c83c85aefe88a2aadbf43b9ed Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 Oct 2022 17:43:04 +1100 Subject: [PATCH 2669/3533] esp32/machine_hw_spi: Use auto DMA channel on S2, S3, C3 chips. Auto DMA channel is supported in IDF v4.4, and is required to be used on S3 chips, so use this simpler configuration option where possible. Fixes issue #8634. Signed-off-by: Damien George --- ports/esp32/machine_hw_spi.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 05b1c871cb3cf..647874e17f648 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -267,21 +267,15 @@ STATIC void machine_hw_spi_init_internal( // Select DMA channel based on the hardware SPI host int dma_chan = 0; + #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 + dma_chan = SPI_DMA_CH_AUTO; + #else if (self->host == HSPI_HOST) { - #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 - dma_chan = 3; - #else dma_chan = 1; - #endif - #ifdef FSPI_HOST - } else if (self->host == FSPI_HOST) { - dma_chan = 1; - #endif - #ifdef VSPI_HOST - } else if (self->host == VSPI_HOST) { + } else { dma_chan = 2; - #endif } + #endif ret = spi_bus_initialize(self->host, &buscfg, dma_chan); switch (ret) { From f13134e40308c24bcb2c09084f17fc9d54efe5db Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 5 Oct 2022 22:30:29 +1100 Subject: [PATCH 2670/3533] tools/mpremote: Fix argument handling for follow and add help strings. Fixes in this commit are: - Make --follow the default for "run" (accidentally changed in 68d094358). - Add help strings for "repl": --capture --inject-file --inject-code - Update help strings for "run". - Fix encoding for --inject-code (accidentally broken in 68d094358). - Remove ability to --no-follow for "eval". It was there previously because it shared the same code path with "exec" and "run", but makes no sense for "eval", so might as well remove. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tools/mpremote/mpremote/commands.py | 2 +- tools/mpremote/mpremote/main.py | 29 ++++++++++++++++++++++------- tools/mpremote/mpremote/repl.py | 1 + 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/tools/mpremote/mpremote/commands.py b/tools/mpremote/mpremote/commands.py index bf56df69993a8..558cd82f10800 100644 --- a/tools/mpremote/mpremote/commands.py +++ b/tools/mpremote/mpremote/commands.py @@ -204,7 +204,7 @@ def do_exec(state, args): def do_eval(state, args): buf = "print(" + args.expr[0] + ")" - _do_execbuffer(state, buf, args.follow) + _do_execbuffer(state, buf, True) def do_run(state, args): diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index 4f541685a0cfd..988ffe8f60f1a 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -119,30 +119,45 @@ def argparse_mount(): def argparse_repl(): cmd_parser = argparse.ArgumentParser(description="connect to given device") - cmd_parser.add_argument("--capture", type=str, required=False, help="TODO") - cmd_parser.add_argument("--inject-code", type=str, required=False, help="TODO") - cmd_parser.add_argument("--inject-file", type=str, required=False, help="TODO") + cmd_parser.add_argument( + "--capture", + type=str, + required=False, + help="saves a copy of the REPL session to the specified path", + ) + cmd_parser.add_argument( + "--inject-code", type=str, required=False, help="code to be run when Ctrl-J is pressed" + ) + cmd_parser.add_argument( + "--inject-file", + type=str, + required=False, + help="path to file to be run when Ctrl-K is pressed", + ) return cmd_parser def argparse_eval(): cmd_parser = argparse.ArgumentParser(description="evaluate and print the string") - _bool_flag(cmd_parser, "follow", "f", True, "TODO") cmd_parser.add_argument("expr", nargs=1, help="expression to execute") return cmd_parser def argparse_exec(): cmd_parser = argparse.ArgumentParser(description="execute the string") - _bool_flag(cmd_parser, "follow", "f", True, "TODO") + _bool_flag( + cmd_parser, "follow", "f", True, "follow output until the expression completes (default)" + ) cmd_parser.add_argument("expr", nargs=1, help="expression to execute") return cmd_parser def argparse_run(): cmd_parser = argparse.ArgumentParser(description="run the given local script") - _bool_flag(cmd_parser, "follow", "f", False, "TODO") - cmd_parser.add_argument("path", nargs=1, help="expression to execute") + _bool_flag( + cmd_parser, "follow", "f", True, "follow output until the script completes (default)" + ) + cmd_parser.add_argument("path", nargs=1, help="path to script to execute") return cmd_parser diff --git a/tools/mpremote/mpremote/repl.py b/tools/mpremote/mpremote/repl.py index 7da00c0fdef0c..3d6ca1881b1e7 100644 --- a/tools/mpremote/mpremote/repl.py +++ b/tools/mpremote/mpremote/repl.py @@ -61,6 +61,7 @@ def do_repl(state, args): print('Capturing session to file "%s"' % capture_file) capture_file = open(capture_file, "wb") if code_to_inject is not None: + code_to_inject = bytes(code_to_inject.replace("\\n", "\r\n"), "utf8") print("Use Ctrl-J to inject", code_to_inject) if file_to_inject is not None: print('Use Ctrl-K to inject file "%s"' % file_to_inject) From 46d02c2469ec7947fe3aae2d68e07236baf5c72e Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 Oct 2022 01:02:39 +1100 Subject: [PATCH 2671/3533] tools/mpremote: Bump version to 0.4.0. Signed-off-by: Damien George --- tools/mpremote/mpremote/__init__.py | 2 +- tools/mpremote/setup.cfg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/mpremote/mpremote/__init__.py b/tools/mpremote/mpremote/__init__.py index 493f7415d73d5..6a9beea82f651 100644 --- a/tools/mpremote/mpremote/__init__.py +++ b/tools/mpremote/mpremote/__init__.py @@ -1 +1 @@ -__version__ = "0.3.0" +__version__ = "0.4.0" diff --git a/tools/mpremote/setup.cfg b/tools/mpremote/setup.cfg index 16880d77f895c..7fae3cbcb3906 100644 --- a/tools/mpremote/setup.cfg +++ b/tools/mpremote/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = mpremote -version = 0.3.0 +version = 0.4.0 author = Damien George author_email = damien@micropython.org description = Tool for interacting remotely with MicroPython From 85afed569d3d435b2611856356ff9c78d244f25c Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 27 May 2022 15:43:48 +0200 Subject: [PATCH 2672/3533] samd: Remove the existing provisional support for REPL on UART. It was only partially working and will be rpelaced later by a full machine.UART class implementation. --- .../mpconfigboard.h | 16 ----- .../boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c | 1 + .../mpconfigboard.h | 15 ---- .../ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c | 1 + .../ADAFRUIT_TRINKET_M0/mpconfigboard.h | 15 ---- ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c | 1 + ports/samd/boards/MINISAM_M4/mpconfigboard.h | 15 ---- ports/samd/boards/MINISAM_M4/pins.c | 1 + .../SAMD21_XPLAINED_PRO/mpconfigboard.h | 16 ----- ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c | 1 + .../boards/SEEED_WIO_TERMINAL/mpconfigboard.h | 15 ---- ports/samd/boards/SEEED_WIO_TERMINAL/pins.c | 1 + ports/samd/boards/SEEED_XIAO/mpconfigboard.h | 15 ---- ports/samd/boards/SEEED_XIAO/pins.c | 1 + ports/samd/modmachine.c | 5 -- ports/samd/modmachine.h | 3 - ports/samd/mphalport.c | 8 --- ports/samd/samd_soc.c | 70 ------------------- 18 files changed, 7 insertions(+), 193 deletions(-) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h index 6d3b7987e39de..bdb22d2e0adf5 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h @@ -9,20 +9,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; -// ASF4 MCU package specific Pin definitions -#include "samd21g18a.h" - -// Please consult the SAM_D21 Datasheet, I/O Multiplexing and Considerations. -// On this board (see https://learn.adafruit.com/assets/40553) TX is D1 (PA10) and RX is D0 (PA11) -// USART pin assignments: Tx=PA10=SERCOM0/PAD[2], Rx=PA11==SERCOM0/PAD[3] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 0 // A=0, B=1 -#define MP_TX_PIN 10 // 'n' -#define MP_RX_PIN 11 -#define MP_PERIPHERAL_MUX 5 // 'n'th group of 2 pins -#define USARTx SERCOM0 // SERCOM0: tx/rx -#define MP_PORT_FUNC 0x22 // Sets PMUXE & PMUXO to the Alternative Function.(A-H=0-7) -#define MP_RXPO_PAD 3 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 1 // TXPO- Transmit Data Pinout -#define MP_SERCOMx SERCOM0_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE GCLK_CLKCTRL_ID_SERCOM0_CORE // Generic Clock Control diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c index e0dd752ec900d..fbb5032a40628 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h index c1c4fd8cad4cf..80baf39e5d6db 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h @@ -14,19 +14,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // -// ASF4 MCU package specific Pin definitions -#include "samd51g19a.h" - -// Please consult the SAM_D51 Datasheet, I/O Multiplexing and Considerations. -// USART pin assignments: Tx=TX_D1=PA17=SERCOM3/PAD[0], Rx=RX_D0=PA16=SERCOM3/PAD[1] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 1 // A-D=0-3 -#define MP_TX_PIN 17 -#define MP_RX_PIN 16 // 'n' -#define MP_PERIPHERAL_MUX 8 // 'n'th group of 2 pins -#define USARTx SERCOM3 // -#define MP_PORT_FUNC 0x33 // Sets PMUXE & PMUXO to the Alternative Function.A-N=0-13 -#define MP_RXPO_PAD 1 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 0 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM3_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE SERCOM3_GCLK_ID_CORE diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c index 82948ccbc4f18..0342b0d00c58a 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h index 128689f4f74a7..066c7ee1429e2 100644 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h @@ -9,19 +9,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; -// ASF4 MCU package specific Pin definitions -#include "samd21e18a.h" - -// Please consult the SAM_D21 Datasheet, I/O Multiplexing and Considerations. -// USART pin assignments: Tx=D4=PA06=SERCOM0/PAD[2], Rx=D3=PA07=SERCOM0/PAD[3] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 1 // A=0, B=1 -#define MP_TX_PIN 6 // 'n' -#define MP_RX_PIN 7 -#define MP_PERIPHERAL_MUX 3 // 'n'th group of 2 pins -#define USARTx SERCOM0 // SERCOM0: tx/rx -#define MP_PORT_FUNC 0x33 // Sets PMUXE & PMUXO to the Alternative Function.A-H=0-7 -#define MP_RXPO_PAD 3 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 2 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM0_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE GCLK_CLKCTRL_ID_SERCOM0_CORE // Generic Clock Control diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c index 9fecddb6cd872..676b4794e89b6 100644 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c +++ b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/boards/MINISAM_M4/mpconfigboard.h b/ports/samd/boards/MINISAM_M4/mpconfigboard.h index a65eb54b495f1..a8f1f96242c8a 100644 --- a/ports/samd/boards/MINISAM_M4/mpconfigboard.h +++ b/ports/samd/boards/MINISAM_M4/mpconfigboard.h @@ -14,19 +14,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // -// ASF4 MCU package specific Pin definitions -#include "samd51g19a.h" - -// Please consult the SAM_D51 Datasheet, I/O Multiplexing and Considerations. -// USART pin assignments: Tx=TX_D1=PA17=SERCOM3/PAD[0], Rx=RX_D0=PA16=SERCOM3/PAD[1] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 0 // A-D=0-3 -#define MP_TX_PIN 17 -#define MP_RX_PIN 16 // 'n' -#define MP_PERIPHERAL_MUX 8 // 'n'th group of 2 pins -#define USARTx SERCOM3 // -#define MP_PORT_FUNC 0x33 // Sets PMUXE & PMUXO to the Alternative Function.A-N=0-13 -#define MP_RXPO_PAD 1 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 0 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM3_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE SERCOM3_GCLK_ID_CORE diff --git a/ports/samd/boards/MINISAM_M4/pins.c b/ports/samd/boards/MINISAM_M4/pins.c index 6cdd840b6bc78..17ed58d3c4182 100644 --- a/ports/samd/boards/MINISAM_M4/pins.c +++ b/ports/samd/boards/MINISAM_M4/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h index a7dbf76144bc7..860cb6b977783 100644 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h @@ -9,20 +9,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; -// ASF4 MCU package specific Pin definitions -#include "samd21j18a.h" - -// Please consult the SAM_D21 Datasheet, I/O Multiplexing and Considerations. -// USART pin assignments: (This board has 3 USARTS brought out to the pins. See https://docs.zephyrproject.org/1.14.1/boards/arm/atsamd21_xpro/doc/index.html#serial-port ) -// Tx=PA10=SERCOM0/PAD[2], Rx=PA11=SERCOM0/PAD[3] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 1 // A=0, B=1 -#define MP_TX_PIN 10 // 'n' -#define MP_RX_PIN 11 -#define MP_PERIPHERAL_MUX 5 // 'n'th group of 2 pins -#define USARTx SERCOM0 // SERCOM0: tx/rx -#define MP_PORT_FUNC 0x22 // Sets PMUXE & PMUXO to the Alternative Function.(A-H=0-7) -#define MP_RXPO_PAD 3 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 2 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM0_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE GCLK_CLKCTRL_ID_SERCOM0_CORE // Generic Clock Control diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c index 2a2d50eb48fcc..35718ea7f3d7f 100644 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h index 290bd802b8a0b..bb0f1c828e784 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h @@ -14,19 +14,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; -// ASF4 MCU package specific Pin definitions -#include "samd51p19a.h" - -// Please consult the SAM_D51 Datasheet, I/O Multiplexing and Considerations. -// WIO_Terminal USART pin assignments: Tx=BCM14=PB27=SERCOM2/PAD[0], Rx=BCM15=PB26=SERCOM2/PAD[1] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 1 // A-D=0-3 -#define MP_TX_PIN 27 -#define MP_RX_PIN 26 // 'n' -#define MP_PERIPHERAL_MUX 13 // 'n'th group of 2 pins -#define USARTx SERCOM2 // -#define MP_PORT_FUNC 0x22 // Sets PMUXE & PMUXO to the Alternative Function.A-N=0-13 -#define MP_RXPO_PAD 1 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 0 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM2_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE SERCOM2_GCLK_ID_CORE diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c index 9862552fa3d85..d7833416d3c73 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in "pins.h" reflects # of Pins defined here. diff --git a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h index 6422d7ea0232e..a2f2a9fb0b01c 100644 --- a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h +++ b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h @@ -9,19 +9,4 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; -// ASF4 MCU package specific Pin definitions -#include "samd21g18a.h" - -// Please consult the SAM_D21 Datasheet, I/O Multiplexing and Considerations. -// XIAO_M0 USART pin assignments: Tx=A6=PB8=SERCOM4/PAD[0], Rx=PB9=A7=SERCOM4/PAD[1] #define CPU_FREQ (48000000) // For selecting Baud from clock. -#define MP_PIN_GRP 1 // A=0, B=1 -#define MP_TX_PIN 8 // 'n' -#define MP_RX_PIN 9 -#define MP_PERIPHERAL_MUX 4 // 'n'th group of 2 pins -#define USARTx SERCOM4 // SERCOM4:XIAO_M0 tx/rx -#define MP_PORT_FUNC 0x33 // Sets PMUXE & PMUXO to the Alternative Function.A-H=0-7 -#define MP_RXPO_PAD 1 // RXPO- Receive Data Pinout -#define MP_TXPO_PAD 0 // TXPO- Tranmit Data Pinout -#define MP_SERCOMx SERCOM4_ // APBCMASK -#define MP_SERCOM_GCLK_ID_x_CORE GCLK_CLKCTRL_ID_SERCOM4_CORE // Generic Clock Control diff --git a/ports/samd/boards/SEEED_XIAO/pins.c b/ports/samd/boards/SEEED_XIAO/pins.c index 6043913d2badb..e2f7c264b4bda 100644 --- a/ports/samd/boards/SEEED_XIAO/pins.c +++ b/ports/samd/boards/SEEED_XIAO/pins.c @@ -27,6 +27,7 @@ */ #include "modmachine.h" +#include "sam.h" #include "pins.h" // Ensure Declaration in 'pins.h' reflects # of Pins defined here. diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 74e3571759420..1b78b687e1803 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -45,9 +45,6 @@ #define DBL_TAP_MAGIC_LOADER 0xf01669ef #define DBL_TAP_MAGIC_RESET 0xf02669ef -MP_DEFINE_CONST_FUN_OBJ_0(machine_uart_init_obj, machine_uart_init); -MP_DEFINE_CONST_FUN_OBJ_0(machine_uart_deinit_obj, machine_uart_deinit); - STATIC mp_obj_t machine_reset(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_RESET; NVIC_SystemReset(); @@ -119,8 +116,6 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, - { MP_ROM_QSTR(MP_QSTR_uart_init), MP_ROM_PTR(&machine_uart_init_obj) }, - { MP_ROM_QSTR(MP_QSTR_uart_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, }; diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 61bd2f4d29912..f7ad2b5e5cbc4 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -31,7 +31,4 @@ extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_led_type; -mp_obj_t machine_uart_init(void); -mp_obj_t machine_uart_deinit(void); - #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index c19d542a85566..beade14a6e3da 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -71,9 +71,6 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { int mp_hal_stdin_rx_chr(void) { for (;;) { - if (USARTx->USART.INTFLAG.bit.RXC) { - return USARTx->USART.DATA.bit.DATA; - } if (tud_cdc_connected() && tud_cdc_available()) { uint8_t buf[1]; uint32_t count = tud_cdc_read(buf, sizeof(buf)); @@ -100,9 +97,4 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { i += n2; } } - while (len--) { - while (!(USARTx->USART.INTFLAG.bit.DRE)) { - } - USARTx->USART.DATA.bit.DATA = *str++; - } } diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 7f4df1bb1c756..7a96cbb4800fd 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -35,75 +35,6 @@ #include "samd_soc.h" #include "tusb.h" -// "MP" macros defined in "boards/$(BOARD)/mpconfigboard.h" -mp_obj_t machine_uart_init(void) { - // Firstly, assign alternate function SERCOM PADs to GPIO pins. - PORT->Group[MP_PIN_GRP].PINCFG[MP_TX_PIN].bit.PMUXEN = 1; // Enable - PORT->Group[MP_PIN_GRP].PINCFG[MP_RX_PIN].bit.PMUXEN = 1; // Enable - PORT->Group[MP_PIN_GRP].PMUX[MP_PERIPHERAL_MUX].reg = MP_PORT_FUNC; // Sets PMUXE & PMUXO in 1 hit. - uint32_t rxpo = MP_RXPO_PAD; // 1=Pad1,3=Pad3 Rx data - uint32_t txpo = MP_TXPO_PAD; // 0=pad0,1=Pad2 Tx data - - // Initialise the clocks - #if defined(MCU_SAMD21) - PM->APBCMASK.bit.MP_SERCOMx = 1; // Enable synchronous clock - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | MP_SERCOM_GCLK_ID_x_CORE; // Select multiplexer generic clock source and enable. - // Wait while it updates synchronously. - while (GCLK->STATUS.bit.SYNCBUSY) { - } - #elif defined(MCU_SAMD51) - GCLK->PCHCTRL[MP_SERCOM_GCLK_ID_x_CORE].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0; - MCLK->APBBMASK.bit.MP_SERCOMx = 1; - #endif - - // Setup the Peripheral. - // Reset (clear) the peripheral registers. - while (USARTx->USART.SYNCBUSY.bit.SWRST) { - } - USARTx->USART.CTRLA.bit.SWRST = 1; // Reset all Registers, disable peripheral - while (USARTx->USART.SYNCBUSY.bit.SWRST) { - } - - // Set the register bits as needed - // (CMODE (async),CHSIZE (8),FORM (no parity),SBMODE (1 stop) already 0). - USARTx->USART.CTRLA.reg = // USARTx = SERCOMx set in "boards/$(BOARD)/mpconfigboard.h" - SERCOM_USART_CTRLA_DORD // Data order - | SERCOM_USART_CTRLA_RXPO(rxpo) // Set Pad# - | SERCOM_USART_CTRLA_TXPO(txpo) // Set Pad# - | SERCOM_USART_CTRLA_MODE(1) // USART with internal clock - ; - USARTx->USART.CTRLB.reg = SERCOM_USART_CTRLB_RXEN | SERCOM_USART_CTRLB_TXEN; // Enable Rx & Tx - while (USARTx->USART.SYNCBUSY.bit.CTRLB) { - } - - // Baud rate is clock dependant. - #if CPU_FREQ == 8000000 - uint32_t baud = 50437; // 115200 baud; 65536*(1 - 16 * 115200/8e6) - #elif CPU_FREQ == 48000000 - uint32_t baud = 63019; // 115200 baud; 65536*(1 - 16 * 115200/48e6) - #elif CPU_FREQ == 120000000 - uint32_t baud = 64529; // 115200 baud; 65536*(1 - 16 * 115200/120e6) - #endif - USARTx->USART.BAUD.bit.BAUD = baud; // Set Baud - USARTx->USART.CTRLA.bit.ENABLE = 1; // Enable the peripheral - // Wait for the Registers to update. - while (USARTx->USART.SYNCBUSY.bit.ENABLE) { - } - - return mp_const_none; -} - -// Disconnect SERCOM from GPIO pins. (Can't SWRST, as that will totally kill USART). -mp_obj_t machine_uart_deinit(void) { - // Reset - printf("Disabling the Alt-Funct, releasing the USART pins for GPIO... \n"); - PORT->Group[MP_PIN_GRP].PINCFG[MP_TX_PIN].bit.PMUXEN = 0; // Disable - PORT->Group[MP_PIN_GRP].PINCFG[MP_RX_PIN].bit.PMUXEN = 0; // Disable - - return mp_const_none; -} - - static void usb_init(void) { // Init USB clock #if defined(MCU_SAMD21) @@ -168,6 +99,5 @@ void samd_init(void) { #endif SysTick_Config(CPU_FREQ / 1000); - machine_uart_init(); usb_init(); } From 0420799a847d4bb74c9a92666e2cd35c23b1c12c Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 27 May 2022 21:19:47 +0200 Subject: [PATCH 2673/3533] samd/boards: Replace pins.c and pins.h by pins.csv. The files pins.c and pins.h are generated during the build process from pins.csv, using a make-pins.py script. --- ports/samd/Makefile | 11 +- .../boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c | 59 -------- .../ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv | 37 +++++ .../boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.h | 42 ------ .../ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c | 59 -------- .../ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv | 36 +++++ .../ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.h | 42 ------ ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c | 44 ------ .../samd/boards/ADAFRUIT_TRINKET_M0/pins.csv | 16 +++ ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.h | 42 ------ ports/samd/boards/MINISAM_M4/pins.c | 52 -------- ports/samd/boards/MINISAM_M4/pins.csv | 27 ++++ ports/samd/boards/MINISAM_M4/pins.h | 42 ------ ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c | 92 ------------- .../samd/boards/SAMD21_XPLAINED_PRO/pins.csv | 63 +++++++++ ports/samd/boards/SAMD21_XPLAINED_PRO/pins.h | 42 ------ ports/samd/boards/SEEED_WIO_TERMINAL/pins.c | 61 --------- ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv | 30 +++++ ports/samd/boards/SEEED_WIO_TERMINAL/pins.h | 42 ------ ports/samd/boards/SEEED_XIAO/pins.c | 53 -------- ports/samd/boards/SEEED_XIAO/pins.csv | 22 +++ ports/samd/boards/SEEED_XIAO/pins.h | 42 ------ ports/samd/boards/make-pins.py | 126 ++++++++++++++++++ 23 files changed, 367 insertions(+), 715 deletions(-) delete mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c create mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv delete mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.h delete mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c create mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv delete mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.h delete mode 100644 ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c create mode 100644 ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv delete mode 100644 ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.h delete mode 100644 ports/samd/boards/MINISAM_M4/pins.c create mode 100644 ports/samd/boards/MINISAM_M4/pins.csv delete mode 100644 ports/samd/boards/MINISAM_M4/pins.h delete mode 100644 ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c create mode 100644 ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv delete mode 100644 ports/samd/boards/SAMD21_XPLAINED_PRO/pins.h delete mode 100644 ports/samd/boards/SEEED_WIO_TERMINAL/pins.c create mode 100644 ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv delete mode 100644 ports/samd/boards/SEEED_WIO_TERMINAL/pins.h delete mode 100644 ports/samd/boards/SEEED_XIAO/pins.c create mode 100644 ports/samd/boards/SEEED_XIAO/pins.csv delete mode 100644 ports/samd/boards/SEEED_XIAO/pins.h create mode 100644 ports/samd/boards/make-pins.py diff --git a/ports/samd/Makefile b/ports/samd/Makefile index aed8637abcc1f..1ef35c1a025ea 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -56,6 +56,11 @@ LDFLAGS += $(LDFLAGS_MOD) LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) +MAKE_PINS = boards/make-pins.py +BOARD_PINS = $(BOARD_DIR)/pins.csv +GEN_PINS_SRC = $(BUILD)/pins.c +GEN_PINS_HDR = $(BUILD)/pins.h + # Tune for Debugging or Optimization CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG),1) @@ -79,7 +84,7 @@ SRC_C = \ help.c \ modutime.c \ modmachine.c \ - $(BOARD_DIR)/pins.c \ + $(BUILD)/pins.c \ machine_pin.c \ machine_led.c \ modsamd.c \ @@ -160,4 +165,8 @@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin $(Q)$(PYTHON) $(UF2CONV) -b $(TEXT0) -c -o $@ $< +$(GEN_PINS_SRC): $(BOARD_PINS) + $(ECHO) "Create $@" + $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --pins $(GEN_PINS_SRC) --inc $(GEN_PINS_HDR) + include $(TOP)/py/mkrules.mk diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c deleted file mode 100644 index fbb5032a40628..0000000000000 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PA11}, // D0 - {{&machine_pin_type}, PIN_PA10}, // D1 - {{&machine_pin_type}, PIN_PA14}, // D2 - {{&machine_pin_type}, PIN_PA09}, // D3/ - {{&machine_pin_type}, PIN_PA08}, // D4/ - {{&machine_pin_type}, PIN_PA15}, // D5 - {{&machine_pin_type}, PIN_PA20}, // D6 - {{&machine_pin_type}, PIN_PA21}, // D7 - {{&machine_pin_type}, PIN_PA06}, // D8/ - {{&machine_pin_type}, PIN_PA07}, // D9/ - {{&machine_pin_type}, PIN_PA18}, // D10 - {{&machine_pin_type}, PIN_PA16}, // D11 - {{&machine_pin_type}, PIN_PA19}, // D12 - {{&machine_pin_type}, PIN_PA17}, // D13/ - {{&machine_pin_type}, PIN_PA02}, // A0 - {{&machine_pin_type}, PIN_PB08}, // A1 - {{&machine_pin_type}, PIN_PB09}, // A2 - {{&machine_pin_type}, PIN_PA04}, // A3/ - {{&machine_pin_type}, PIN_PA05}, // A4/ - {{&machine_pin_type}, PIN_PB02}, // A5 -}; - -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PA17}, // D13/ user LED -}; diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv new file mode 100644 index 0000000000000..84e68157acbc3 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv @@ -0,0 +1,37 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PA11,D0 +PIN_PA10,D1 +PIN_PA14,D2 +PIN_PA09,D3 +PIN_PA08,D4 +PIN_PA15,D5 +PIN_PA20,D6 +PIN_PA21,D7 +PIN_PA06,D8 +PIN_PA07,D9 +PIN_PA18,D10 +PIN_PA16,D11 +PIN_PA19,D12 +PIN_PA17,D13 +PIN_PA02,A0 +PIN_PB08,A1 +PIN_PB09,A2 +PIN_PA04,A3 +PIN_PA05,A4 +PIN_PB02,A5 +PIN_PB22,TX +PIN_PB23,RX +PIN_PA23,SCL +PIN_PA22,SDA +PIN_PA06,NEOPIXEL +PIN_PA13,FLASH_CS + +LED_PA17,LED +LED_PA27,LED_TX +LED_PB03,LED_RX diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.h b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.h deleted file mode 100644 index 45bee6167837c..0000000000000 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[20]; -extern const machine_led_obj_t machine_led_obj[1]; diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c deleted file mode 100644 index 0342b0d00c58a..0000000000000 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PA16}, // RX_D0 - {{&machine_pin_type}, PIN_PA17}, // TX_D1 - {{&machine_pin_type}, PIN_PA07}, // D2 - {{&machine_pin_type}, PIN_PB22}, // D3 - {{&machine_pin_type}, PIN_PA14}, // D4 - {{&machine_pin_type}, PIN_PA15}, // D5 - {{NULL}, -1}, // D6- not terminated on breakout. - {{&machine_pin_type}, PIN_PA18}, // D7 - {{NULL}, -1}, // D8- not terminated on breakout. - {{&machine_pin_type}, PIN_PA19}, // D9 - {{&machine_pin_type}, PIN_PA20}, // D10 - {{&machine_pin_type}, PIN_PA21}, // D11 - {{&machine_pin_type}, PIN_PA23}, // D12 - {{&machine_pin_type}, PIN_PA22}, // D13 - {{&machine_pin_type}, PIN_PA02}, // A0 - {{&machine_pin_type}, PIN_PA05}, // A1 - {{&machine_pin_type}, PIN_PB08}, // A2 - {{&machine_pin_type}, PIN_PB09}, // A3 - {{&machine_pin_type}, PIN_PA04}, // A4 - {{&machine_pin_type}, PIN_PA06}, // A5 -}; - -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PA22}, // D13 -}; diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv new file mode 100644 index 0000000000000..90e38761a10ce --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv @@ -0,0 +1,36 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PA16,D0 +PIN_PA17,D1 +PIN_PA07,D2 +PIN_PB22,D3 +PIN_PA14,D4 +PIN_PA15,D5 +- +PIN_PA18,D7 +- +PIN_PA19,D9 +PIN_PA20,D10 +PIN_PA21,D11 +PIN_PA23,D12 +PIN_PA22,D13 +PIN_PA02,A0 +PIN_PA05,A1 +PIN_PB08,A2 +PIN_PB09,A3 +PIN_PA04,A4 +PIN_PA06,A5 +PIN_PA12,SDA +PIN_PA13,SCL +PIN_PA00,MO +PIN_PB23,MI +PIN_PA01,SCK +PIN_PB02,DOTSTAR_CLK +PIN_PB03,DOTSTAR_DATA + +LED_PA22,LED diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.h deleted file mode 100644 index 45bee6167837c..0000000000000 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[20]; -extern const machine_led_obj_t machine_led_obj[1]; diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c deleted file mode 100644 index 676b4794e89b6..0000000000000 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PA08}, // D0 - {{&machine_pin_type}, PIN_PA02}, // D1 - {{&machine_pin_type}, PIN_PA09}, // D2 - {{&machine_pin_type}, PIN_PA07}, // D3/ RxD - {{&machine_pin_type}, PIN_PA06}, // D4/ TxD -}; - -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PA10}, // user LED -}; diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv new file mode 100644 index 0000000000000..797b3f70bb921 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv @@ -0,0 +1,16 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PA08,D0 +PIN_PA02,D1 +PIN_PA09,D2 +PIN_PA07,D3 +PIN_PA06,D4 +PIN_PA00,DOTSTAR_DATA +PIN_PA01,DOTSTAR_CLK + +LED_PA10,LED diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.h b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.h deleted file mode 100644 index 843d69addc0f2..0000000000000 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[5]; -extern const machine_led_obj_t machine_led_obj[1]; diff --git a/ports/samd/boards/MINISAM_M4/pins.c b/ports/samd/boards/MINISAM_M4/pins.c deleted file mode 100644 index 17ed58d3c4182..0000000000000 --- a/ports/samd/boards/MINISAM_M4/pins.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PA02}, // A0,D9 - {{&machine_pin_type}, PIN_PB08}, // A1,D10 - {{&machine_pin_type}, PIN_PB09}, // A2,D11 - {{&machine_pin_type}, PIN_PA04}, // A3,D12 - {{&machine_pin_type}, PIN_PA05}, // A4,D13 - {{&machine_pin_type}, PIN_PA06}, // A5 - {{&machine_pin_type}, PIN_PA16}, // RX_D0 - {{&machine_pin_type}, PIN_PA17}, // TX_D1 - {{&machine_pin_type}, PIN_PA07}, // D2,A6 - {{&machine_pin_type}, PIN_PA19}, // D3 - {{&machine_pin_type}, PIN_PA20}, // D4 - {{&machine_pin_type}, PIN_PA21}, // D5 - {{&machine_pin_type}, PIN_PA00}, // BUTTON -}; - -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PA15}, // LED -}; diff --git a/ports/samd/boards/MINISAM_M4/pins.csv b/ports/samd/boards/MINISAM_M4/pins.csv new file mode 100644 index 0000000000000..7442a028d12a5 --- /dev/null +++ b/ports/samd/boards/MINISAM_M4/pins.csv @@ -0,0 +1,27 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PA02,A0_D9 +PIN_PB08,A1_D10 +PIN_PB09,A2_D11 +PIN_PA04,A3_D12 +PIN_PA05,A4_D13 +PIN_PA06,A5 +PIN_PA16,D0 +PIN_PA17,D1 +PIN_PA07,A6_D2 +PIN_PA19,D3 +PIN_PA20,D4 +PIN_PA21,D5 +PIN_PA00,BUTTON +PIN_PA03,AREF +PIN_PA12,SDA +PIN_PA13,SCL +PIN_PB03,DOTSTAR_DATA +PIN_PB02,DOTSTAR_CLK + +LED_PA15,LED diff --git a/ports/samd/boards/MINISAM_M4/pins.h b/ports/samd/boards/MINISAM_M4/pins.h deleted file mode 100644 index 892b0e7b9763d..0000000000000 --- a/ports/samd/boards/MINISAM_M4/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[13]; -extern const machine_led_obj_t machine_led_obj[1]; diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c deleted file mode 100644 index 35718ea7f3d7f..0000000000000 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - // EXT1 - {{&machine_pin_type}, PIN_PB00}, // PIN3_ADC(+) - {{&machine_pin_type}, PIN_PB01}, // PIN4_ADC(-) - {{&machine_pin_type}, PIN_PB06}, // PIN5_GPIO - {{&machine_pin_type}, PIN_PB07}, // PIN6_GPIO - {{&machine_pin_type}, PIN_PB02}, // PIN7_PWM(+) - {{&machine_pin_type}, PIN_PB03}, // PIN8_PWM(-) - {{&machine_pin_type}, PIN_PB04}, // PIN9_IRQ/GPIO - {{&machine_pin_type}, PIN_PB05}, // PIN10_SPI_SS_B/GPIO - {{&machine_pin_type}, PIN_PA08}, // PIN11_TWI_SDA - {{&machine_pin_type}, PIN_PA09}, // PIN12_TWI_SCL - {{&machine_pin_type}, PIN_PB09}, // PIN13_UART_RX - {{&machine_pin_type}, PIN_PB08}, // PIN14_UART_TX - {{&machine_pin_type}, PIN_PA05}, // PIN15_SPI_SS_A - {{&machine_pin_type}, PIN_PA06}, // PIN16_SPI_MOSI - {{&machine_pin_type}, PIN_PA04}, // PIN17_SPI_MISO - {{&machine_pin_type}, PIN_PA07}, // PIN18_SPI_SCK - - // EXT2 - {{&machine_pin_type}, PIN_PA10}, // PIN3_ADC(+) - {{&machine_pin_type}, PIN_PA11}, // PIN4_ADC(-) - {{&machine_pin_type}, PIN_PA20}, // PIN5_GPIO - {{&machine_pin_type}, PIN_PA21}, // PIN6_GPIO - {{&machine_pin_type}, PIN_PB12}, // PIN7_PWM(+) - {{&machine_pin_type}, PIN_PB13}, // PIN8_PWM(-) - {{&machine_pin_type}, PIN_PB14}, // PIN9_IRQ/GPIO - {{&machine_pin_type}, PIN_PB15}, // PIN10_SPI_SS_B/GPIO - {{NULL}, -1}, // PIN_PA08/ PIN11_TWI_SDA already defined - {{NULL}, -1}, // PIN_PA09/ PIN12_TWI_SCL already defined - {{&machine_pin_type}, PIN_PB11}, // PIN13_UART_RX - {{&machine_pin_type}, PIN_PB10}, // PIN14_UART_TX - {{&machine_pin_type}, PIN_PA17}, // PIN15_SPI_SS_A - {{&machine_pin_type}, PIN_PA18}, // PIN16_SPI_MOSI - {{&machine_pin_type}, PIN_PA16}, // PIN17_SPI_MISO - {{&machine_pin_type}, PIN_PA19}, // PIN18_SPI_SCK - - // EXT3 - {{&machine_pin_type}, PIN_PA02}, // PIN3_ADC(+) - {{&machine_pin_type}, PIN_PA03}, // PIN4_ADC(-) - {{NULL}, -1}, // PIN_PB30/ PIN5_GPIO already defined - {{&machine_pin_type}, PIN_PA15}, // PIN6_GPIO; USER_BUTTON - {{&machine_pin_type}, PIN_PA12}, // PIN7_PWM(+) - {{&machine_pin_type}, PIN_PA13}, // PIN8_PWM(-) - {{&machine_pin_type}, PIN_PA28}, // PIN9_IRQ/GPIO - {{&machine_pin_type}, PIN_PA27}, // PIN10_SPI_SS_B/GPIO - {{NULL}, -1}, // PIN_PA08/ PIN11_TWI_SDA already defined - {{NULL}, -1}, // PIN_PA09/ PIN12_TWI_SCL already defined - {{NULL}, -1}, // PIN_PB11/ PIN13_UART_RX already defined - {{NULL}, -1}, // PIN_PB10/ PIN14_UART_TX already defined - {{&machine_pin_type}, PIN_PB17}, // PIN15_SPI_SS_A - {{&machine_pin_type}, PIN_PB22}, // PIN16_SPI_MOSI - {{&machine_pin_type}, PIN_PB16}, // PIN17_SPI_MISO - {{&machine_pin_type}, PIN_PB23}, // PIN18_SPI_SCK -}; - -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PB30}, // USER_LED -}; diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv new file mode 100644 index 0000000000000..438119d90c1a8 --- /dev/null +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv @@ -0,0 +1,63 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +# EXT1 +PIN_PB00,EXT1_PIN3 +PIN_PB01,EXT1_PIN4 +PIN_PB06,EXT1_PIN5 +PIN_PB07,EXT1_PIN6 +PIN_PB02,EXT1_PIN7 +PIN_PB03,EXT1_PIN8 +PIN_PB04,EXT1_PIN9 +PIN_PB05,EXT1_PIN10 +PIN_PA08,EXT1_PIN11 +PIN_PA09,EXT1_PIN12 +PIN_PB09,EXT1_PIN13 +PIN_PB08,EXT1_PIN14 +PIN_PA05,EXT1_PIN15 +PIN_PA06,EXT1_PIN16 +PIN_PA04,EXT1_PIN17 +PIN_PA07,EXT1_PIN18 + +# EXT2 +PIN_PA10,EXT2_PIN3 +PIN_PA11,EXT2_PIN4 +PIN_PA20,EXT2_PIN5 +PIN_PA21,EXT2_PIN6 +PIN_PB12,EXT2_PIN7 +PIN_PB13,EXT2_PIN8 +PIN_PB14,EXT2_PIN9 +PIN_PB15,EXT2_PIN10 +- +- +PIN_PB11,EXT2_PIN13 +PIN_PB10,EXT2_PIN14 +PIN_PA17,EXT2_PIN15 +PIN_PA18,EXT2_PIN16 +PIN_PA16,EXT2_PIN17 +PIN_PA19,EXT2_PIN18 + +# EXT3 +PIN_PA02,EXT3_PIN3 +PIN_PA03,EXT3_PIN4 +- +PIN_PA15,EXT3_PIN6 +PIN_PA12,EXT3_PIN7 +PIN_PA13,EXT3_PIN8 +PIN_PA28,EXT3_PIN9 +PIN_PA27,EXT3_PIN10 +- +- +- +- +PIN_PB17,EXT3_PIN15 +PIN_PB22,EXT3_PIN16 +PIN_PB16,EXT3_PIN17 +PIN_PB23,EXT3_PIN18 + +LED_PB30,LED + diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.h b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.h deleted file mode 100644 index 0b0e878b43e1f..0000000000000 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[48]; -extern const machine_led_obj_t machine_led_obj[1]; diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c deleted file mode 100644 index d7833416d3c73..0000000000000 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in "pins.h" reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PB08}, // A0/D0 - {{&machine_pin_type}, PIN_PB09}, // A1/D1 - {{&machine_pin_type}, PIN_PA07}, // A2/D2 - {{&machine_pin_type}, PIN_PB04}, // A3/D3 - {{&machine_pin_type}, PIN_PB05}, // A4/D4 - {{&machine_pin_type}, PIN_PB06}, // A5/D5 - {{&machine_pin_type}, PIN_PA04}, // A6/D6 - {{&machine_pin_type}, PIN_PB07}, // A7/D7 - {{&machine_pin_type}, PIN_PA06}, // A8/D8 - {{&machine_pin_type}, PIN_PD08}, // SWITCH_X - {{&machine_pin_type}, PIN_PD09}, // SWITCH_Y - {{&machine_pin_type}, PIN_PD10}, // SWITCH_Z - {{&machine_pin_type}, PIN_PD12}, // SWITCH_B - {{&machine_pin_type}, PIN_PD20}, // SWITCH_U - {{&machine_pin_type}, PIN_PC26}, // BUTTON_1 - {{&machine_pin_type}, PIN_PC27}, // BUTTON_2 - {{&machine_pin_type}, PIN_PC28}, // BUTTON_3 - {{&machine_pin_type}, PIN_PD11}, // BUZZER_CTR - {{&machine_pin_type}, PIN_PC14}, // 5V_OUTPUT_CTR- enable 5V on hdr - {{&machine_pin_type}, PIN_PC15}, // 3V3_OUTPUT_CTR- enable 3V3 on hdr -}; - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_led_obj_t machine_led_obj[] = { - {{&machine_led_type}, PIN_PA15}, // USER_LED (Blue) - {{&machine_led_type}, PIN_PC05}, // LCD_BACKLIGHT_CTR -}; diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv new file mode 100644 index 0000000000000..72da71224a4d4 --- /dev/null +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv @@ -0,0 +1,30 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PB08,A0_D0 +PIN_PB09,A1_D1 +PIN_PA07,A2_D2 +PIN_PB04,A3_D3 +PIN_PB05,A4_D4 +PIN_PB06,A5_D5 +PIN_PA04,A6_D6 +PIN_PB07,A7_D7 +PIN_PA06,A8_D8 +PIN_PD08,SWITCH_X +PIN_PD09,SWITCH_Y +PIN_PD10,SWITCH_Z +PIN_PD12,SWITCH_B +PIN_PD20,SWITCH_U +PIN_PC26,BUTTON_1 +PIN_PC27,BUTTON_2 +PIN_PC28,BUTTON_3 +PIN_PD11,BUZZER_CTR +PIN_PC14,5V_ENABLE +PIN_PC15,3V3_ENABLE + +LED_PA15,LED_BLUE +LED_PC05,LED_LCD diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.h b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.h deleted file mode 100644 index 45ecc254f11ee..0000000000000 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[20]; -extern const machine_led_obj_t machine_led_obj[2]; diff --git a/ports/samd/boards/SEEED_XIAO/pins.c b/ports/samd/boards/SEEED_XIAO/pins.c deleted file mode 100644 index e2f7c264b4bda..0000000000000 --- a/ports/samd/boards/SEEED_XIAO/pins.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c. Holds Board/MCU specific Pin allocations. - */ - -#include "modmachine.h" -#include "sam.h" -#include "pins.h" - -// Ensure Declaration in 'pins.h' reflects # of Pins defined here. -const machine_pin_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_PA02}, // A0/D0 - {{&machine_pin_type}, PIN_PA04}, // A1/D1 - {{&machine_pin_type}, PIN_PA10}, // A2/D2 - {{&machine_pin_type}, PIN_PA11}, // A3/D3 - {{&machine_pin_type}, PIN_PA08}, // A4/D4 - {{&machine_pin_type}, PIN_PA09}, // A5/D5 - {{&machine_pin_type}, PIN_PB08}, // A6/D6 - {{&machine_pin_type}, PIN_PB09}, // A7/D7 - {{&machine_pin_type}, PIN_PA07}, // A8/D8 - {{&machine_pin_type}, PIN_PA05}, // A9/D9 - {{&machine_pin_type}, PIN_PA06}, // A10/D10 -}; - -const machine_led_obj_t machine_led_obj[] = { -// XIAO: Just the available LED Pins: User LED (PA17), Rx & Tx. - {{&machine_led_type}, PIN_PA17}, // W13 - {{&machine_led_type}, PIN_PA18}, // RX_LED - {{&machine_led_type}, PIN_PA19}, // TX_LED -}; diff --git a/ports/samd/boards/SEEED_XIAO/pins.csv b/ports/samd/boards/SEEED_XIAO/pins.csv new file mode 100644 index 0000000000000..8a70a77145639 --- /dev/null +++ b/ports/samd/boards/SEEED_XIAO/pins.csv @@ -0,0 +1,22 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with # are ignored + +PIN_PA02,A0_D0 +PIN_PA04,A1_D1 +PIN_PA10,A2_D2 +PIN_PA11,A3_D3 +PIN_PA08,A4_D4 +PIN_PA09,A5_D5 +PIN_PB08,A6_D6 +PIN_PB09,A7_D7 +PIN_PA07,A8_D8 +PIN_PA05,A9_D9 +PIN_PA06,A10_D10 + +LED_PA17,USER_LED +LED_PA18,RX_LED +LED_PA19,TX_LED diff --git a/ports/samd/boards/SEEED_XIAO/pins.h b/ports/samd/boards/SEEED_XIAO/pins.h deleted file mode 100644 index 226b3f1d7b07a..0000000000000 --- a/ports/samd/boards/SEEED_XIAO/pins.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2021 Peter van der Burg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Used by machine_pin.c & board specific pins.c. Holds Board/MCU specific Pin - * allocations. - */ - -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_pin_obj_t; - -typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; -} machine_led_obj_t; - -// MUST explicitly hold array # of rows, else machine_pin.c wont compile. -extern const machine_pin_obj_t machine_pin_obj[11]; -extern const machine_led_obj_t machine_led_obj[3]; diff --git a/ports/samd/boards/make-pins.py b/ports/samd/boards/make-pins.py new file mode 100644 index 0000000000000..400a9c9b8870f --- /dev/null +++ b/ports/samd/boards/make-pins.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python +"""Generates the pins file for the SAMD port.""" + +from __future__ import print_function + +import argparse +import sys +import csv + +pins_header_prefix = """// This file was automatically generated by make-pins.py +// +typedef struct _machine_pin_obj_t { + mp_obj_base_t base; + uint32_t id; + char *name; +} machine_pin_obj_t; + +""" + +led_header_prefix = """typedef struct _machine_led_obj_t { + mp_obj_base_t base; + uint32_t id; + char *name; +} machine_led_obj_t; + +""" + + +class Pins: + def __init__(self): + self.board_pins = [] # list of pin objects + self.board_leds = [] # list of led objects + + def parse_csv_file(self, filename): + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile) + for row in rows: + # Pin numbers must start with "PIN_" + # LED numbers must start with "LED_" + if len(row) > 0: + if row[0].strip().startswith("PIN_"): + if len(row) == 1: + self.board_pins.append([row[0], row[0][4:]]) + else: + self.board_pins.append([row[0], row[1]]) + elif row[0].strip().startswith("LED_"): + self.board_leds.append(["PIN_" + row[0][4:], row[1]]) + elif row[0].strip().startswith("-"): + self.board_pins.append(["-1", ""]) + + def print_pins(self, pins_filename): + with open(pins_filename, "wt") as pins_file: + pins_file.write("// This file was automatically generated by make-pins.py\n") + pins_file.write("//\n") + pins_file.write('#include "modmachine.h"\n') + pins_file.write('#include "sam.h"\n') + pins_file.write('#include "pins.h"\n\n') + + pins_file.write("const machine_pin_obj_t machine_pin_obj[] = {\n") + for pin in self.board_pins: + pins_file.write(" {{&machine_pin_type}, ") + pins_file.write(pin[0] + ', "' + pin[1]) + pins_file.write('"},\n') + pins_file.write("};\n") + + if self.board_leds: + pins_file.write("\nconst machine_led_obj_t machine_led_obj[] = {\n") + for pin in self.board_leds: + pins_file.write(" {{&machine_pin_type}, ") + pins_file.write(pin[0] + ', "' + pin[1]) + pins_file.write('"},\n') + pins_file.write("};\n") + + def print_header(self, hdr_filename): + with open(hdr_filename, "wt") as hdr_file: + hdr_file.write(pins_header_prefix) + if self.board_leds: + hdr_file.write(led_header_prefix) + hdr_file.write( + "extern const machine_pin_obj_t machine_pin_obj[%d];\n" % len(self.board_pins) + ) + if self.board_leds: + hdr_file.write( + "extern const machine_led_obj_t machine_led_obj[%d];\n" % len(self.board_leds) + ) + + +def main(): + parser = argparse.ArgumentParser( + prog="make-pins.py", + usage="%(prog)s [options] [command]", + description="Generate board specific pin file", + ) + parser.add_argument( + "-b", + "--board", + dest="csv_filename", + help="Specifies the pins.csv filename", + ) + parser.add_argument( + "-p", + "--pins", + dest="pins_filename", + help="Specifies the name of the generated pins.c file", + ) + parser.add_argument( + "-i", + "--inc", + dest="hdr_filename", + help="Specifies name of generated pin header file", + ) + args = parser.parse_args(sys.argv[1:]) + + pins = Pins() + + if args.csv_filename: + pins.parse_csv_file(args.csv_filename) + + if args.pins_filename: + pins.print_pins(args.pins_filename) + + pins.print_header(args.hdr_filename) + + +if __name__ == "__main__": + main() From a5d5ecbf8434e480b42a8a477ef9ba7979bba927 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 13:27:12 +0200 Subject: [PATCH 2674/3533] samd/boards: Move mcu-specific settings into a mpconfig_samdXX.h file. Located at the boards directory. That way, the mpconfigboard.h files are almost empty, just setting the board name and the MCU name. --- ports/samd/Makefile | 3 +++ .../mpconfigboard.h | 10 --------- .../mpconfigboard.h | 15 ------------- .../ADAFRUIT_TRINKET_M0/mpconfigboard.h | 10 --------- ports/samd/boards/MINISAM_M4/mpconfigboard.h | 15 ------------- .../SAMD21_XPLAINED_PRO/mpconfigboard.h | 10 --------- .../boards/SEEED_WIO_TERMINAL/mpconfigboard.h | 15 ------------- ports/samd/boards/SEEED_XIAO/mpconfigboard.h | 10 --------- ports/samd/boards/mpconfig_samd21.h | 9 ++++++++ ports/samd/boards/mpconfig_samd51.h | 21 +++++++++++++++++++ ports/samd/mpconfigport.h | 2 ++ 11 files changed, 35 insertions(+), 85 deletions(-) create mode 100644 ports/samd/boards/mpconfig_samd21.h create mode 100644 ports/samd/boards/mpconfig_samd51.h diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 1ef35c1a025ea..4dd6356c1e0d4 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -50,6 +50,9 @@ CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-a CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += -DMPCONFIG_MCU_H='' + +QSTR_GLOBAL_DEPENDENCIES += boards/mpconfig_$(MCU_SERIES_LOWER).h LDFLAGS = -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref LDFLAGS += $(LDFLAGS_MOD) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h index bdb22d2e0adf5..cec9e9ccdda46 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h @@ -1,12 +1,2 @@ #define MICROPY_HW_BOARD_NAME "Feather M0 Express" #define MICROPY_HW_MCU_NAME "SAMD21G18A" - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 64k Flash storage at top. 256k-64k=196k -// 256*1024=262144 minus 64*1024=65536 = 196608 = 0x30000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h index 80baf39e5d6db..2fffc9c7de28a 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h @@ -1,17 +1,2 @@ #define MICROPY_HW_BOARD_NAME "ItsyBitsy M4 Express" #define MICROPY_HW_MCU_NAME "SAMD51G19A" - -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -#define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 128k Flash storage at top. 512k-128k=384k=0x60000 -// 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h index 066c7ee1429e2..d3a6ba2d8687e 100644 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h @@ -1,12 +1,2 @@ #define MICROPY_HW_BOARD_NAME "Trinket M0" #define MICROPY_HW_MCU_NAME "SAMD21E18A" - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 64k Flash storage at top. 256k-64k=196k -// 256*1024=262144 minus 64*1024=65536 = 196608 = 0x30000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/MINISAM_M4/mpconfigboard.h b/ports/samd/boards/MINISAM_M4/mpconfigboard.h index a8f1f96242c8a..6715a16f01450 100644 --- a/ports/samd/boards/MINISAM_M4/mpconfigboard.h +++ b/ports/samd/boards/MINISAM_M4/mpconfigboard.h @@ -1,17 +1,2 @@ #define MICROPY_HW_BOARD_NAME "Mini SAM M4" #define MICROPY_HW_MCU_NAME "SAMD51G19A" - -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -#define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 128k Flash storage at top. 512k-128k=384k=0x60000 -// 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h index 860cb6b977783..c69b5b4c149de 100644 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h @@ -1,12 +1,2 @@ #define MICROPY_HW_BOARD_NAME "SAMD21-XPLAINED-PRO" #define MICROPY_HW_MCU_NAME "SAMD21J18A" - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 64k Flash storage at top. 256k-64k=196k -// 256*1024=262144 minus 64*1024=65536 = 196608 = 0x30000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h index bb0f1c828e784..cfda18d11c965 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.h @@ -1,17 +1,2 @@ #define MICROPY_HW_BOARD_NAME "Wio Terminal D51R" #define MICROPY_HW_MCU_NAME "SAMD51P19A" - -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -#define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_MATH (0) -#define MICROPY_PY_CMATH (0) - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 128k Flash storage at top. 512k-128k=384k=0x60000 -// 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h index a2f2a9fb0b01c..2026efc6b153b 100644 --- a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h +++ b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h @@ -1,12 +1,2 @@ #define MICROPY_HW_BOARD_NAME "Seeed Xiao" #define MICROPY_HW_MCU_NAME "SAMD21G18A" - -// MicroPython configs -// samd_flash.c flash parameters -// Build a 64k Flash storage at top. 256k-64k=196k -// 256*1024=262144 minus 64*1024=65536 = 196608 = 0x30000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; - -#define CPU_FREQ (48000000) // For selecting Baud from clock. diff --git a/ports/samd/boards/mpconfig_samd21.h b/ports/samd/boards/mpconfig_samd21.h new file mode 100644 index 0000000000000..1924f66f06047 --- /dev/null +++ b/ports/samd/boards/mpconfig_samd21.h @@ -0,0 +1,9 @@ +// Deinitions common to all SAMD21 boards +#include "samd21.h" + +#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) +#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) +#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; + +#define CPU_FREQ (48000000) +#define APB_FREQ (48000000) diff --git a/ports/samd/boards/mpconfig_samd51.h b/ports/samd/boards/mpconfig_samd51.h new file mode 100644 index 0000000000000..1b67b1d02d5c0 --- /dev/null +++ b/ports/samd/boards/mpconfig_samd51.h @@ -0,0 +1,21 @@ +// Deinitions common to all SAMD51 boards +#include "samd51.h" + +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_PY_BUILTINS_COMPLEX (0) +#define MICROPY_PY_MATH (0) +#define MICROPY_PY_CMATH (0) + +// Due to a limitation in the TC counter for us, the ticks period is 2**29 +#define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) +// MicroPython configs +// samd_flash.c flash parameters +// Build a 128k Flash storage at top. 512k-128k=384k=0x60000 +// 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 +#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) +#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) +#define VFS_BLOCK_SIZE_BYTES (1536) // + +#define CPU_FREQ (120000000) +#define APB_FREQ (48000000) +#define DPLLx_REF_FREQ (32768) diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 2c6052149ba30..c29043c5d0a2a 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -28,6 +28,8 @@ // Board specific definitions #include "mpconfigboard.h" +// MCU-Specific definitions +#include MPCONFIG_MCU_H // Memory allocation policies #define MICROPY_GC_STACK_ENTRY_TYPE uint16_t From c4f7c0b8a2504e8a13644ea692c4a66d6f124871 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 16:16:22 +0200 Subject: [PATCH 2675/3533] samd/Makefile: Alphabetically sort the source code files in Makefile. --- ports/samd/Makefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 4dd6356c1e0d4..66e23bae69d0f 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -83,16 +83,16 @@ LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif SRC_C = \ - main.c \ help.c \ + machine_led.c \ + machine_pin.c \ + main.c \ modutime.c \ modmachine.c \ - $(BUILD)/pins.c \ - machine_pin.c \ - machine_led.c \ modsamd.c \ - samd_flash.c \ mphalport.c \ + $(BUILD)/pins.c \ + samd_flash.c \ samd_isr.c \ samd_soc.c \ tusb_port.c \ @@ -130,10 +130,10 @@ endif # List of sources for qstr extraction SRC_QSTR += \ + machine_led.c \ + machine_pin.c \ modutime.c \ modmachine.c \ - machine_pin.c \ - machine_led.c \ modsamd.c \ samd_flash.c \ shared/readline/readline.c \ From 949a808076f10f9f7e25fd9ebe4a03b8cc397e1f Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 15:42:56 +0200 Subject: [PATCH 2676/3533] samd/boards: Add ADAFRUIT_FEATHER_M4_EXPRESS and _ITSYBITSY_M0_EXPRESS. These two boards are used for testing, so it is favorable to have them added early. The full test set is: - ADAFRUIT_FEATHER_M4_EXPRESS: SAMD51 with 32kHz crystal. - ADAFRUIT_ITSYBITSY_M0_EXPRESS: SAMD21 without crystal. - ADAFRUIT_ITSYBITSY_M4_EXPRESS: SAMD51 without crystal. - SEEED_XIAO: SAM21 with 32kHz crystal. --- .../ADAFRUIT_FEATHER_M4_EXPRESS/board.json | 22 ++++++++++ .../mpconfigboard.h | 2 + .../mpconfigboard.mk | 8 ++++ .../ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv | 34 ++++++++++++++++ .../ADAFRUIT_ITSYBITSY_M0_EXPRESS/board.json | 20 ++++++++++ .../mpconfigboard.h | 2 + .../mpconfigboard.mk | 8 ++++ .../ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv | 40 +++++++++++++++++++ ports/samd/boards/samd51j19a.ld | 17 ++++++++ 9 files changed, 153 insertions(+) create mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/board.json create mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h create mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk create mode 100644 ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv create mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/board.json create mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h create mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.mk create mode 100644 ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv create mode 100644 ports/samd/boards/samd51j19a.ld diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/board.json b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/board.json new file mode 100644 index 0000000000000..c8042aa254733 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/board.json @@ -0,0 +1,22 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [ + "Battery Charging", + "Breadboard Friendly", + "Feather", + "Micro USB", + "RGB LED", + "SPI Flash" + ], + "images": [ + "feather_m4_express.jpg" + ], + "mcu": "samd51", + "product": "Feather M4 Express", + "thumbnail": "", + "url": "https://www.adafruit.com/product/3857", + "vendor": "Adafruit" +} diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h new file mode 100644 index 0000000000000..48599eec47297 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h @@ -0,0 +1,2 @@ +#define MICROPY_HW_BOARD_NAME "Feather M4 Express" +#define MICROPY_HW_MCU_NAME "SAMD51J19A" diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk new file mode 100644 index 0000000000000..d29e2b1097b1f --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk @@ -0,0 +1,8 @@ +MCU_SERIES = SAMD51 +CMSIS_MCU = SAMD51J19A +LD_FILES = boards/samd51j19a.ld sections.ld +TEXT0 = 0x4000 + +# The ?='s allow overriding in mpconfigboard.mk. +# MicroPython settings +MICROPY_VFS_LFS1 ?= 1 diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv new file mode 100644 index 0000000000000..5b999c39e0224 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv @@ -0,0 +1,34 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines starting with PIN_ or LED_ are ignored + +PIN_PB17,D0 +PIN_PB16,D1 +PIN_PA14,D4 +PIN_PA16,D5 +PIN_PA18,D6 +- +PIN_PB03,D8 +PIN_PA19,D9 +PIN_PA20,D10 +PIN_PA21,D11 +PIN_PA22,D12 +PIN_PA23,D13 +PIN_PA02,A0 +PIN_PA05,A1 +PIN_PB08,A2 +PIN_PB09,A3 +PIN_PA04,A4 +PIN_PB06,A5 +PIN_PA13,SCL +PIN_PA12,SDA +PIN_PB23,MOSI +PIN_PB22,MISO +PIN_PA17,SCK +PIN_PB01,VDIV +PIN_PA03,AREF + +LED_PA17,LED diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/board.json b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/board.json new file mode 100644 index 0000000000000..f99d19ca87f87 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/board.json @@ -0,0 +1,20 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [ + "Breadboard Friendly", + "Micro USB", + "RGB LED", + "SPI Flash" + ], + "images": [ + "itsybitsy_m0_express.jpg" + ], + "mcu": "samd21", + "product": "ItsyBitsy M0 Express", + "thumbnail": "", + "url": "https://www.adafruit.com/product/3727", + "vendor": "Adafruit" +} diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h new file mode 100644 index 0000000000000..d647af9312eda --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h @@ -0,0 +1,2 @@ +#define MICROPY_HW_BOARD_NAME "ItsyBitsy M0 Express" +#define MICROPY_HW_MCU_NAME "SAMD21G18A" diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.mk b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.mk new file mode 100644 index 0000000000000..a760cf047e629 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.mk @@ -0,0 +1,8 @@ +MCU_SERIES = SAMD21 +CMSIS_MCU = SAMD21G18A +LD_FILES = boards/samd21x18a.ld sections.ld +TEXT0 = 0x2000 + +# The ?='s allow overriding in mpconfigboard.mk. +# MicroPython settings +MICROPY_VFS_LFS1 ?= 1 diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv new file mode 100644 index 0000000000000..50c3c1cf68f51 --- /dev/null +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv @@ -0,0 +1,40 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines not starting with PIN_ or LED_ are ignored + +PIN_PA11,D0 +PIN_PA10,D1 +PIN_PA14,D2 +PIN_PB09,D3 +PIN_PA08,D4 +PIN_PA15,D5 +- +PIN_PA21,D7 +- +PIN_PA07,D9 +PIN_PA18,D10 +PIN_PA16,D11 +PIN_PA19,D12 +PIN_PA17,D13 +PIN_PA02,A0 +PIN_PB08,A1 +PIN_PB09,A2 +PIN_PA04,A3 +PIN_PA05,A4 +PIN_PB02,A5 +PIN_PA22,SDA +PIN_PA23,SCL +PIN_PB10,MO +PIN_PA12,MI +PIN_PB11,SCK +PIN_PA00,DOTSTAR_CLK +PIN_PA01,DOTSTAR_DATA +PIN_PB22,FLASH_MOSI +PIN_PB03,FLASH_MISO +PIN_PB23,FLASH_SCK +PIN_PA27,FLASH_CS + +LED_PA17,LED diff --git a/ports/samd/boards/samd51j19a.ld b/ports/samd/boards/samd51j19a.ld new file mode 100644 index 0000000000000..e0baa9bba0281 --- /dev/null +++ b/ports/samd/boards/samd51j19a.ld @@ -0,0 +1,17 @@ +/* + GNU linker script for SAMD51 +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 512K - 16K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K +} + +/* Top end of the stack, with room for double-tap variable */ +_estack = ORIGIN(RAM) + LENGTH(RAM) - 8; +_sstack = _estack - 16K; + +_sheap = _ebss; +_eheap = _sstack; From b4d29fd47a52526bc9626e5dc28794fa1d95dcf2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 16:31:46 +0200 Subject: [PATCH 2677/3533] samd/clock_config: Set up the clock configuration. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clock settings: - GCLK0: 48 MHz (SAMD21) or 120 MHz(SAMD51). - GCLK1: 32768 Hz for driving the PLL. - GCLK2: 48 MHz for tzhe peripheral clock. - GCLK3: 1 MHz (SAMD21) or 8 MHz (SAMD51) for the µs ticks timer. - GCLK8: 1 kHz for WDT (SAMD21 only). If a 32 kHz crystal is present, it will be used as clock source. Otherwise the DFLL48M in open-loop mode is used. GCLK0 for SAM51 can be changed between 48 MHz and 200 MHz. The specified range is 96 MHz - 120 MHz. --- ports/samd/Makefile | 1 + ports/samd/clock_config.c | 348 ++++++++++++++++++++++++++++++++++++++ ports/samd/clock_config.h | 33 ++++ ports/samd/samd_soc.c | 64 ++++--- ports/samd/samd_soc.h | 11 ++ 5 files changed, 430 insertions(+), 27 deletions(-) create mode 100644 ports/samd/clock_config.c create mode 100644 ports/samd/clock_config.h diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 66e23bae69d0f..e3316a9d6c114 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -83,6 +83,7 @@ LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif SRC_C = \ + clock_config.c \ help.c \ machine_led.c \ machine_pin.c \ diff --git a/ports/samd/clock_config.c b/ports/samd/clock_config.c new file mode 100644 index 0000000000000..0f5634fdb7373 --- /dev/null +++ b/ports/samd/clock_config.c @@ -0,0 +1,348 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * This file provides functions for configuring the clocks. + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "samd_soc.h" + +static uint32_t cpu_freq = CPU_FREQ; +static uint32_t apb_freq = APB_FREQ; + +#if defined(MCU_SAMD21) +int sercom_gclk_id[] = { + GCLK_CLKCTRL_ID_SERCOM0_CORE, GCLK_CLKCTRL_ID_SERCOM1_CORE, + GCLK_CLKCTRL_ID_SERCOM2_CORE, GCLK_CLKCTRL_ID_SERCOM3_CORE, + GCLK_CLKCTRL_ID_SERCOM4_CORE, GCLK_CLKCTRL_ID_SERCOM5_CORE +}; +#elif defined(MCU_SAMD51) +int sercom_gclk_id[] = { + SERCOM0_GCLK_ID_CORE, SERCOM1_GCLK_ID_CORE, + SERCOM2_GCLK_ID_CORE, SERCOM3_GCLK_ID_CORE, + SERCOM4_GCLK_ID_CORE, SERCOM5_GCLK_ID_CORE, + #if defined(SERCOM7_GCLK_ID_CORE) + SERCOM6_GCLK_ID_CORE, SERCOM7_GCLK_ID_CORE, + #endif +}; +#endif + +uint32_t get_cpu_freq(void) { + return cpu_freq; +} + +uint32_t get_apb_freq(void) { + return apb_freq; +} + +#if defined(MCU_SAMD21) +void set_cpu_freq(uint32_t cpu_freq_arg) { + cpu_freq = cpu_freq_arg; +} + +#elif defined(MCU_SAMD51) +void set_cpu_freq(uint32_t cpu_freq_arg) { + cpu_freq = cpu_freq_arg; + + // Setup GCLK0 for 48MHz as default state to keep the MCU running during config change. + GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL0) { + } + + // Setup DPLL0 for 120 MHz + // first: disable DPLL0 in case it is running + OSCCTRL->Dpll[0].DPLLCTRLA.bit.ENABLE = 0; + while (OSCCTRL->Dpll[0].DPLLSYNCBUSY.bit.ENABLE == 1) { + } + // Now configure the registers + OSCCTRL->Dpll[0].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_DIV(1) | OSCCTRL_DPLLCTRLB_LBYPASS | + OSCCTRL_DPLLCTRLB_REFCLK(0) | OSCCTRL_DPLLCTRLB_WUF | OSCCTRL_DPLLCTRLB_FILTER(0x01); + + uint32_t div = cpu_freq / DPLLx_REF_FREQ; + uint32_t frac = (cpu_freq - div * DPLLx_REF_FREQ) / (DPLLx_REF_FREQ / 32); + OSCCTRL->Dpll[0].DPLLRATIO.reg = (frac << 16) + div - 1; + // enable it again + OSCCTRL->Dpll[0].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_ENABLE | OSCCTRL_DPLLCTRLA_RUNSTDBY; + + // Per errata 2.13.1 + while (!(OSCCTRL->Dpll[0].DPLLSTATUS.bit.CLKRDY == 1)) { + } + + // Setup GCLK0 for DPLL0 output (48 or 48-200MHz) + GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DPLL0; + while (GCLK->SYNCBUSY.bit.GENCTRL0) { + } +} +#endif + +void init_clocks(uint32_t cpu_freq) { + #if defined(MCU_SAMD21) + + // SAMD21 Clock settings + // GCLK0: 48MHz from DFLL open loop mode or closed loop mode from 32k Crystal + // GCLK1: 32768 Hz from 32K ULP or 32k Crystal + // GCLK2: 48MHz from DFLL for Peripherals + // GCLK3: 1Mhz for the us-counter (TC3/TC4) + // GCLK8: 1kHz clock for WDT + + NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" + NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz + + #if MICROPY_HW_XOSC32K + // Set up OSC32K according datasheet 17.6.3 + SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x3) | SYSCTRL_XOSC32K_EN32K | + SYSCTRL_XOSC32K_XTALEN; + SYSCTRL->XOSC32K.bit.ENABLE = 1; + while (SYSCTRL->PCLKSR.bit.XOSC32KRDY == 0) { + } + // Set up the DFLL48 according to the data sheet 17.6.7.1.2 + // Step 1: Set up the reference clock + // Connect the OSC32K via GCLK1 to the DFLL input and for further use. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(1); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_CLKEN; + // Enable access to the DFLLCTRL reg acc. to Errata 1.2.1 + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 2: Set the coarse and fine values. + // The coarse setting will be taken from the calibration data. So the value used here + // does not matter. Get the coarse value from the calib data. In case it is not set, + // set a midrange value. + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) + >> FUSES_DFLL48M_COARSE_CAL_Pos; + if (coarse == 0x3f) { + coarse = 0x1f; + } + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 3: Set the multiplication values. The offset of 16384 to the freq is for rounding. + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL((CPU_FREQ + 16384) / 32768) | + SYSCTRL_DFLLMUL_FSTEP(1) | SYSCTRL_DFLLMUL_CSTEP(1); + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 4: Start the DFLL and wait for the PLL lock. We just wait for the fine lock, since + // coarse adjusting is bypassed. + SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | + SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE; + while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) { + } + + #else // MICROPY_HW_XOSC32K + + // Enable DFLL48M + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { + } + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) + | SYSCTRL_DFLLMUL_MUL(48000); + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) + >> FUSES_DFLL48M_COARSE_CAL_Pos; + if (coarse == 0x3f) { + coarse = 0x1f; + } + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM + | SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { + } + // Enable 32768 Hz on GCLK1 for consistency + GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(48016384 / 32768); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(1); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + #endif // MICROPY_HW_XOSC32K + + // Enable GCLK output: 48M on both CCLK0 and GCLK2 + GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + // Enable GCLK output: 1MHz on GCLK3 for TC3 + GCLK->GENDIV.reg = GCLK_GENDIV_ID(3) | GCLK_GENDIV_DIV(48); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(3); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + // Set GCLK8 to 1 kHz. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + #elif defined(MCU_SAMD51) + + // SAMD51 clock settings + // GCLK0: 48MHz from DFLL48M or 48 - 200 MHz from DPLL0 (SAMD51) + // GCLK1: DPLLx_REF_FREQ 32768 Hz from 32KULP or 32k Crystal + // GCLK2: 48MHz from DFLL48M for Peripheral devices + // GCLK3: 16Mhz for the us-counter (TC0/TC1) + // DPLL0: 48 - 200 MHz + + // Steps to set up clocks: + // Reset Clocks + // Switch GCLK0 to DFLL 48MHz + // Setup 32768 Hz source and DFLL48M in closed loop mode, if a crystal is present. + // Setup GCLK1 to the DPLL0 Reference freq. of 32768 Hz + // Setup GCLK1 to drive peripheral channel 1 + // Setup DPLL0 to 120MHz + // Setup GCLK0 to 120MHz + // Setup GCLK2 to 48MHz for Peripherals + // Setup GCLK3 to 8MHz for TC0/TC1 + + // Setup GCLK0 for 48MHz as default state to keep the MCU running during config change. + GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL0) { + } + + #if MICROPY_HW_XOSC32K + // OSCILLATOR CONTROL + // Setup XOSC32K + OSC32KCTRL->INTFLAG.reg = OSC32KCTRL_INTFLAG_XOSC32KRDY | OSC32KCTRL_INTFLAG_XOSC32KFAIL; + OSC32KCTRL->XOSC32K.bit.CGM = OSC32KCTRL_XOSC32K_CGM_HS_Val; + OSC32KCTRL->XOSC32K.bit.XTALEN = 1; // 0: Generator 1: Crystal + OSC32KCTRL->XOSC32K.bit.EN32K = 1; + OSC32KCTRL->XOSC32K.bit.ONDEMAND = 0; + OSC32KCTRL->XOSC32K.bit.RUNSTDBY = 1; + OSC32KCTRL->XOSC32K.bit.STARTUP = 4; + OSC32KCTRL->CFDCTRL.bit.CFDEN = 1; // Fall back to internal Osc on crystal fail + OSC32KCTRL->XOSC32K.bit.ENABLE = 1; + // make sure osc32kcrtl is ready + while (OSC32KCTRL->STATUS.bit.XOSC32KRDY == 0) { + } + + // Setup GCLK1 for 32kHz crystal + GCLK->GENCTRL[1].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K; + while (GCLK->SYNCBUSY.bit.GENCTRL1) { + } + + // Set-up the DFLL48M in closed loop mode with input from the 32kHz crystal + + // Step 1: Peripheral channel 0 is driven by GCLK1 and it feeds DFLL48M + GCLK->PCHCTRL[0].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; + while (GCLK->PCHCTRL[0].bit.CHEN == 0) { + } + // Step 2: Set the multiplication values. The offset of 16384 to the freq is for rounding. + OSCCTRL->DFLLMUL.reg = OSCCTRL_DFLLMUL_MUL((APB_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) | + OSCCTRL_DFLLMUL_FSTEP(1) | OSCCTRL_DFLLMUL_CSTEP(1); + while (OSCCTRL->DFLLSYNC.bit.DFLLMUL == 1) { + } + // Step 3: Set the mode to closed loop + OSCCTRL->DFLLCTRLB.reg = OSCCTRL_DFLLCTRLB_BPLCKC | OSCCTRL_DFLLCTRLB_MODE; + while (OSCCTRL->DFLLSYNC.bit.DFLLCTRLB == 1) { + } + // Wait for lock fine + while (OSCCTRL->STATUS.bit.DFLLLCKF == 0) { + } + // Step 4: Start the DFLL. + OSCCTRL->DFLLCTRLA.reg = OSCCTRL_DFLLCTRLA_RUNSTDBY | OSCCTRL_DFLLCTRLA_ENABLE; + while (OSCCTRL->DFLLSYNC.bit.ENABLE == 1) { + } + + #else // MICROPY_HW_XOSC32K + + // Set GCLK1 to DPLL0_REF_FREQ as defined in mpconfigboard.h (e.g. 32768 Hz) + GCLK->GENCTRL[1].reg = ((APB_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) << GCLK_GENCTRL_DIV_Pos + | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL1) { + } + + #endif // MICROPY_HW_XOSC32K + + // Peripheral channel 1 is driven by GCLK1 and it feeds DPLL0 + GCLK->PCHCTRL[1].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; + while (GCLK->PCHCTRL[1].bit.CHEN == 0) { + } + + set_cpu_freq(cpu_freq); + + apb_freq = APB_FREQ; // To be changed if CPU_FREQ < 48M + + // Setup GCLK2 for DPLL1 output (48 MHz) + GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL2) { + } + + // Setup GCLK3 for 8MHz, Used for TC0/1 counter + GCLK->GENCTRL[3].reg = GCLK_GENCTRL_DIV(6) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL3) { + } + + #endif // defined(MCU_SAMD51) +} + +void enable_sercom_clock(int id) { + // Next: Set up the clocks + #if defined(MCU_SAMD21) + // Enable synchronous clock. The bits are nicely arranged + PM->APBCMASK.reg |= 0x04 << id; + // Select multiplexer generic clock source and enable. + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | sercom_gclk_id[id]; + // Wait while it updates synchronously. + while (GCLK->STATUS.bit.SYNCBUSY) { + } + #elif defined(MCU_SAMD51) + GCLK->PCHCTRL[sercom_gclk_id[id]].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; + // no easy way to set the clocks, except enabling all of them + switch (id) { + case 0: + MCLK->APBAMASK.bit.SERCOM0_ = 1; + break; + case 1: + MCLK->APBAMASK.bit.SERCOM1_ = 1; + break; + case 2: + MCLK->APBBMASK.bit.SERCOM2_ = 1; + break; + case 3: + MCLK->APBBMASK.bit.SERCOM3_ = 1; + break; + case 4: + MCLK->APBDMASK.bit.SERCOM4_ = 1; + break; + case 5: + MCLK->APBDMASK.bit.SERCOM5_ = 1; + break; + #ifdef SERCOM7_GCLK_ID_CORE + case 6: + MCLK->APBDMASK.bit.SERCOM6_ = 1; + break; + case 7: + MCLK->APBDMASK.bit.SERCOM7_ = 1; + break; + #endif + } + #endif +} diff --git a/ports/samd/clock_config.h b/ports/samd/clock_config.h new file mode 100644 index 0000000000000..0e7a9e3280ec3 --- /dev/null +++ b/ports/samd/clock_config.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * This file provides functions for configuring the clocks. + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +void init_clocks(uint32_t cpu_freq); +void set_cpu_freq(uint32_t cpu_freq); +uint32_t get_cpu_freq(void); +uint32_t get_apb_freq(void); +void enable_sercom_clock(int id); diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 7a96cbb4800fd..8d6e808f63904 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -10,6 +10,7 @@ * The MIT License (MIT) * * Copyright (c) 2019 Damien P. George + * Copyright (c) 2022 Robert Hammelrath * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,6 +34,7 @@ #include "py/runtime.h" #include "modmachine.h" #include "samd_soc.h" +#include "sam.h" #include "tusb.h" static void usb_init(void) { @@ -43,7 +45,7 @@ static void usb_init(void) { PM->APBBMASK.bit.USB_ = 1; uint8_t alt = 6; // alt G, USB #elif defined(MCU_SAMD51) - GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK1; + GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; while (GCLK->PCHCTRL[USB_GCLK_ID].bit.CHEN == 0) { } MCLK->AHBMASK.bit.USB_ = 1; @@ -61,43 +63,51 @@ static void usb_init(void) { tusb_init(); } -void samd_init(void) { +// Initialize the microsecond counter on TC 0/1 +void init_us_counter(void) { #if defined(MCU_SAMD21) - NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" - NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz - - // Enable DFLL48M - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; - while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { - } - SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) - | SYSCTRL_DFLLMUL_MUL(48000); - uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) - >> FUSES_DFLL48M_COARSE_CAL_Pos; - if (coarse == 0x3f) { - coarse = 0x1f; - } - uint32_t fine = 512; - SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(fine); - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM - | SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE; - while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { + PM->APBCMASK.bit.TC3_ = 1; // Enable TC3 clock + PM->APBCMASK.bit.TC4_ = 1; // Enable TC4 clock + // Select multiplexer generic clock source and enable. + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_ID_TC4_TC5; + // Wait while it updates synchronously. + while (GCLK->STATUS.bit.SYNCBUSY) { } - GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); - while (GCLK->STATUS.bit.SYNCBUSY) { + // configure the timer + TC4->COUNT32.CTRLA.bit.MODE = TC_CTRLA_MODE_COUNT32_Val; + TC4->COUNT32.CTRLA.bit.RUNSTDBY = 1; + TC4->COUNT32.CTRLA.bit.ENABLE = 1; + while (TC4->COUNT32.STATUS.bit.SYNCBUSY) { + } + TC4->COUNT32.READREQ.reg = TC_READREQ_RREQ | TC_READREQ_RCONT | 0x10; + while (TC4->COUNT32.STATUS.bit.SYNCBUSY) { } #elif defined(MCU_SAMD51) - GCLK->GENCTRL[1].reg = 1 << GCLK_GENCTRL_DIV_Pos | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; - while (GCLK->SYNCBUSY.bit.GENCTRL1) { + MCLK->APBAMASK.bit.TC0_ = 1; // Enable TC0 clock + MCLK->APBAMASK.bit.TC1_ = 1; // Enable TC1 clock + // Peripheral channel 9 is driven by GCLK3, 8 MHz. + GCLK->PCHCTRL[TC0_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK3 | GCLK_PCHCTRL_CHEN; + while (GCLK->PCHCTRL[TC0_GCLK_ID].bit.CHEN == 0) { + } + + // configure the timer + TC0->COUNT32.CTRLA.bit.PRESCALER = 0; + TC0->COUNT32.CTRLA.bit.MODE = TC_CTRLA_MODE_COUNT32_Val; + TC0->COUNT32.CTRLA.bit.RUNSTDBY = 1; + TC0->COUNT32.CTRLA.bit.ENABLE = 1; + while (TC0->COUNT32.SYNCBUSY.bit.ENABLE) { } #endif +} - SysTick_Config(CPU_FREQ / 1000); +void samd_init(void) { + init_clocks(get_cpu_freq()); + SysTick_Config(get_cpu_freq() / 1000); + init_us_counter(); usb_init(); } diff --git a/ports/samd/samd_soc.h b/ports/samd/samd_soc.h index a07e68dbed646..b97159dc7fc5e 100644 --- a/ports/samd/samd_soc.h +++ b/ports/samd/samd_soc.h @@ -28,6 +28,7 @@ #include #include "sam.h" +#include "clock_config.h" void samd_init(void); void samd_main(void); @@ -38,4 +39,14 @@ void USB_1_Handler_wrapper(void); void USB_2_Handler_wrapper(void); void USB_3_Handler_wrapper(void); +void common_uart_irq_handler(int uart_nr); +void common_spi_irq_handler(int spi_nr); +void common_i2c_irq_handler(int i2c_nr); +void sercom_enable(Sercom *spi, int state); +void sercom_register_irq(int sercom_id, int mode); + +#define SERCOM_IRQ_TYPE_UART (0) +#define SERCOM_IRQ_TYPE_SPI (1) +#define SERCOM_IRQ_TYPE_I2C (2) + #endif // MICROPY_INCLUDED_SAMD_SAMD_SOC_H From 5af54ad61feb52f4c1367d6f9a8a60d972f02419 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 17:00:32 +0200 Subject: [PATCH 2678/3533] samd/modmachine: Allow changing the CPU freq with machine.freq(f). SAMD51 only. Accepted values are 48_000_000 to 200_000_000. The range specified by Atmel is 96_000_000 to 120_000_000. --- ports/samd/modmachine.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 1b78b687e1803..99fccf5bf0085 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -59,10 +59,21 @@ STATIC mp_obj_t machine_bootloader(void) { } MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader); -STATIC mp_obj_t machine_freq(void) { - return MP_OBJ_NEW_SMALL_INT(CPU_FREQ); +STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return MP_OBJ_NEW_SMALL_INT(get_cpu_freq()); + } else { + #if defined(MCU_SAMD51) + uint32_t freq = mp_obj_get_int(args[0]); + if (freq >= 48000000 && freq <= 200000000) { + set_cpu_freq(freq); + SysTick_Config(freq / 1000); + } + #endif + return mp_const_none; + } } -MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); STATIC mp_obj_t machine_unique_id(void) { // Each device has a unique 128-bit serial number which is a concatenation of four 32-bit From 3d9940bc28e66464e22386916cb71100d6b24f1d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 18:09:11 +0200 Subject: [PATCH 2679/3533] samd/mphalport: Add pin open-drain funcs, and improve delay and ticks. The changes in this commit are: - Add an interface for pin open-drain mode. - Improve ticks_us() by using the us-counter. - Improve ticks_cpu() by using the CPU's SysTick. --- ports/samd/mphalport.c | 47 ++++++++++++++++++++++++++++-------- ports/samd/mphalport.h | 54 +++++++++++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 19 deletions(-) diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index beade14a6e3da..e51f13ab7e9fc 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2019 Damien P. George + * Copyright (c) 2022 Robert Hammelrath * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,10 +28,13 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/stream.h" +#include "shared/runtime/interrupt_char.h" +#include "extmod/misc.h" #include "samd_soc.h" #include "tusb.h" #if MICROPY_KBD_EXCEPTION +int mp_interrupt_char = -1; void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { (void)itf; @@ -40,24 +44,47 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { } void mp_hal_set_interrupt_char(int c) { + mp_interrupt_char = c; tud_cdc_set_wanted_char(c); } #endif +void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux) { + int pin_grp = pin / 32; + int port_grp = (pin % 32) / 2; + PORT->Group[pin_grp].PINCFG[pin % 32].bit.PMUXEN = 1; // Enable Mux + if (pin & 1) { + PORT->Group[pin_grp].PMUX[port_grp].bit.PMUXO = mux; + } else { + PORT->Group[pin_grp].PMUX[port_grp].bit.PMUXE = mux; + } +} + + void mp_hal_delay_ms(mp_uint_t ms) { - ms += 1; - uint32_t t0 = systick_ms; - while (systick_ms - t0 < ms) { - MICROPY_EVENT_POLL_HOOK + if (ms > 10) { + uint32_t t0 = systick_ms; + while (systick_ms - t0 < ms) { + MICROPY_EVENT_POLL_HOOK + } + } else { + mp_hal_delay_us(ms * 1000); } } void mp_hal_delay_us(mp_uint_t us) { - uint32_t ms = us / 1000 + 1; - uint32_t t0 = systick_ms; - while (systick_ms - t0 < ms) { - __WFI(); + if (us > 0) { + uint32_t start = mp_hal_ticks_us(); + #if defined(MCU_SAMD21) + // SAMD21 counter has effective 32 bit width + while ((mp_hal_ticks_us() - start) < us) { + } + #elif defined(MCU_SAMD51) + // SAMD51 counter has effective 29 bit width + while (((mp_hal_ticks_us() - start) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) < us) { + } + #endif } } @@ -78,7 +105,7 @@ int mp_hal_stdin_rx_chr(void) { return buf[0]; } } - __WFI(); + MICROPY_EVENT_POLL_HOOK } } @@ -90,7 +117,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { n = CFG_TUD_CDC_EP_BUFSIZE; } while (n > tud_cdc_write_available()) { - __WFI(); + MICROPY_EVENT_POLL_HOOK } uint32_t n2 = tud_cdc_write(str + i, n); tud_cdc_write_flush(); diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 2bbde4390e333..b18aa2f9b4355 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2019-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,25 +28,49 @@ #define MICROPY_INCLUDED_SAMD_MPHALPORT_H #include "py/mpconfig.h" -#include "py/ringbuf.h" // ASF4 #include "hal_gpio.h" +#include "hpl_time_measure.h" +#include "sam.h" extern int mp_interrupt_char; -extern ringbuf_t stdin_ringbuf; extern volatile uint32_t systick_ms; +extern volatile uint32_t systick_ms_upper; void mp_hal_set_interrupt_char(int c); +#define mp_hal_delay_us_fast mp_hal_delay_us + static inline mp_uint_t mp_hal_ticks_ms(void) { return systick_ms; } + static inline mp_uint_t mp_hal_ticks_us(void) { + #if defined(MCU_SAMD21) + + return REG_TC4_COUNT32_COUNT; + + #elif defined(MCU_SAMD51) + + TC0->COUNT32.CTRLBSET.reg = TC_CTRLBSET_CMD_READSYNC; + while (TC0->COUNT32.CTRLBSET.reg != 0) { + } + return REG_TC0_COUNT32_COUNT >> 3; + + #else return systick_ms * 1000; + #endif } + +// ticks_cpu is limited to a 1 ms period, since the CPU SysTick counter +// is used for the 1 ms SysTick_Handler interrupt. static inline mp_uint_t mp_hal_ticks_cpu(void) { - return 0; + return (system_time_t)SysTick->VAL; +} + +static inline uint64_t mp_hal_time_ns(void) { + return ((uint64_t)systick_ms + (uint64_t)systick_ms_upper * 0x100000000) * 1000000; } // C-level pin HAL @@ -55,9 +80,10 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) { #define MP_HAL_PIN_FMT "%u" #define mp_hal_pin_obj_t uint -extern uint32_t machine_pin_open_drain_mask; +extern uint32_t machine_pin_open_drain_mask[]; mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in); +void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux); static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) { return pin; @@ -65,18 +91,22 @@ static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); - machine_pin_open_drain_mask &= ~(1 << pin); + machine_pin_open_drain_mask[pin / 32] &= ~(1 << (pin % 32)); } static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); - machine_pin_open_drain_mask &= ~(1 << pin); + machine_pin_open_drain_mask[pin / 32] &= ~(1 << (pin % 32)); } static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); gpio_set_pin_level(pin, 0); - machine_pin_open_drain_mask |= 1 << pin; + machine_pin_open_drain_mask[pin / 32] |= 1 << (pin % 32); +} + +static inline unsigned int mp_hal_get_pin_direction(mp_hal_pin_obj_t pin) { + return (PORT->Group[pin / 32].DIR.reg & (1 << (pin % 32))) >> (pin % 32); } static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) { @@ -87,7 +117,14 @@ static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) { gpio_set_pin_level(pin, v); } -/* +static inline void mp_hal_pin_low(mp_hal_pin_obj_t pin) { + gpio_set_pin_level(pin, 0); +} + +static inline void mp_hal_pin_high(mp_hal_pin_obj_t pin) { + gpio_set_pin_level(pin, 1); +} + static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); } @@ -95,6 +132,5 @@ static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); } -*/ #endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H From 98ae3126402260eab306e846d01d12af317d96a2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 20:23:56 +0200 Subject: [PATCH 2680/3533] samd/samd_isr: Extend systick_ms to 64 bit. By adding a 32 bit overflow counter. This allows it to be used for the time functions. --- ports/samd/samd_isr.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index dcf80d28cab0c..a7956c49305fa 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -32,6 +32,7 @@ extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss; const ISR isr_vector[]; uint32_t systick_ms; +volatile uint32_t systick_ms_upper; void Reset_Handler(void) __attribute__((naked)); void Reset_Handler(void) { @@ -81,7 +82,11 @@ void Default_Handler(void) { } void SysTick_Handler(void) { - systick_ms += 1; + uint32_t next_tick = systick_ms + 1; + systick_ms = next_tick; + if (systick_ms == 0) { + systick_ms_upper += 1; + } } const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { From 33eaf739d2e0502ec77bdd212dfb6a3106db30b8 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 20:33:48 +0200 Subject: [PATCH 2681/3533] samd/machine_pin: Add OPEN_DRAIN mode for pins. Changes in this commit are: - Use mphal_xx functions whenever possible. - Remove obsolete includes. - Clean up traces of a non-functional pin.irq() from earlier builds. Pin.irq() will be added in further commits in a working manner. --- ports/samd/machine_pin.c | 124 +++++++++++++++------------------------ ports/samd/mphalport.h | 4 +- 2 files changed, 50 insertions(+), 78 deletions(-) diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 5f9cbfb99b85e..49aecba011e9b 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -31,35 +31,25 @@ #include "extmod/virtpin.h" #include "modmachine.h" #include "samd_soc.h" -#include "pins.h" // boards// +#include "pins.h" -// ASF4 (MCU package specific pin defs in 'boards') #include "hal_gpio.h" -#include "hpl_gpio.h" -#include "hal_atomic.h" #define GPIO_MODE_IN (0) #define GPIO_MODE_OUT (1) -// #define GPIO_MODE_ALT (3) +#define GPIO_MODE_OPEN_DRAIN (2) #define GPIO_STRENGTH_2MA (0) #define GPIO_STRENGTH_8MA (1) -// asf4 hpl_gpio.h gpio_pull_mode +uint32_t machine_pin_open_drain_mask[4]; -/* -typedef struct _machine_pin_irq_obj_t { - mp_irq_obj_t base; - uint32_t flags; - uint32_t trigger; -} machine_pin_irq_obj_t; - -STATIC const mp_irq_methods_t machine_pin_irq_methods; -*/ +// Open drain behaviour is simulated. +#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask[id / 32] & (1 << (id % 32))) STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pin_obj_t *self = self_in; - mp_printf(print, "Pin(%u)", self->id); + mp_printf(print, "GPIO P%c%02u", "ABCD"[self->id / 32], self->id % 32); } STATIC void pin_validate_drive(bool strength) { @@ -84,24 +74,26 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ // set initial value (do this before configuring mode/pull) if (args[ARG_value].u_obj != mp_const_none) { - gpio_set_pin_level(self->id, mp_obj_is_true(args[ARG_value].u_obj)); + mp_hal_pin_write(self->id, mp_obj_is_true(args[ARG_value].u_obj)); } // configure mode if (args[ARG_mode].u_obj != mp_const_none) { mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); if (mode == GPIO_MODE_IN) { - gpio_set_pin_direction(self->id, GPIO_DIRECTION_IN); + mp_hal_pin_input(self->id); } else if (mode == GPIO_MODE_OUT) { - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); + mp_hal_pin_output(self->id); + } else if (mode == GPIO_MODE_OPEN_DRAIN) { + mp_hal_pin_open_drain(self->id); } else { - gpio_set_pin_direction(self->id, GPIO_DIRECTION_IN); // If no args are given, the Pin is 'input'. + mp_hal_pin_input(self->id); // If no args are given, the Pin is 'input'. } } // configure pull. Only to be used with IN mode. The function sets the pin to INPUT. uint32_t pull = 0; - mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); - if (mode == GPIO_MODE_OUT && args[ARG_pull].u_obj != mp_const_none) { + mp_int_t dir = mp_hal_get_pin_direction(self->id); + if (dir == GPIO_DIRECTION_OUT && args[ARG_pull].u_obj != mp_const_none) { mp_raise_ValueError(MP_ERROR_TEXT("OUT incompatible with pull")); } else if (args[ARG_pull].u_obj != mp_const_none) { pull = mp_obj_get_int(args[ARG_pull].u_obj); @@ -143,17 +135,24 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, } // fast method for getting/setting pin value -STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); machine_pin_obj_t *self = self_in; if (n_args == 0) { // get pin - return MP_OBJ_NEW_SMALL_INT(gpio_get_pin_level(self->id)); + return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self->id)); } else { // set pin bool value = mp_obj_is_true(args[0]); - gpio_set_pin_level(self->id, value); - + if (GPIO_IS_OPEN_DRAIN(self->id)) { + if (value == 0) { + mp_hal_pin_od_low(self->id); + } else { + mp_hal_pin_od_high(self->id); + } + } else { + mp_hal_pin_write(self->id, value); + } return mp_const_none; } } @@ -165,7 +164,7 @@ STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init); // Pin.value([value]) -STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { +mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { return machine_pin_call(args[0], n_args - 1, 0, args + 1); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); @@ -181,9 +180,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_disable_obj, machine_pin_disable); // Pin.low() Totem-pole (push-pull) STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_set_pin_level(self->id, false); - + if (GPIO_IS_OPEN_DRAIN(self->id)) { + mp_hal_pin_od_low(self->id); + } else { + mp_hal_pin_low(self->id); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); @@ -191,9 +192,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); // Pin.high() Totem-pole (push-pull) STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_set_pin_level(self->id, true); - + if (GPIO_IS_OPEN_DRAIN(self->id)) { + mp_hal_pin_od_high(self->id); + } else { + mp_hal_pin_high(self->id); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); @@ -205,18 +208,18 @@ STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { // Determine DIRECTION of PIN. bool pin_dir; - pin_dir = (PORT->Group[(enum gpio_port)GPIO_PORT(self->id)].DIR.reg // Get PORT# - & (1 << GPIO_PIN(self->id))) // Isolate the Pin in question - >> GPIO_PIN(self->id); // Shift to LSB for binary result. - - if (pin_dir) { - // Pin is OUTPUT - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_toggle_pin_level(self->id); + if (GPIO_IS_OPEN_DRAIN(self->id)) { + pin_dir = mp_hal_get_pin_direction(self->id); + if (pin_dir) { + // Pin is output, thus low, switch to high + mp_hal_pin_od_high(self->id); + } else { + mp_hal_pin_od_low(self->id); + } } else { - mp_raise_ValueError(MP_ERROR_TEXT("Cannot TOGGLE INPUT pin!\n")); + gpio_toggle_pin_level(self->id); } - return mp_const_true; + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); @@ -250,20 +253,15 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&machine_pin_disable_obj) }, { MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&machine_pin_drive_obj) }, - - // { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, - // class constants { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) }, { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUT) }, - // { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_ALT) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OPEN_DRAIN) }, { MP_ROM_QSTR(MP_QSTR_PULL_OFF), MP_ROM_INT(GPIO_PULL_OFF) }, { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) }, { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_STRENGTH_2MA) }, { MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_STRENGTH_8MA) }, - // { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_IRQ_EDGE_RISE) }, - // { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_IRQ_EDGE_FALL) }, }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); @@ -298,34 +296,6 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &machine_pin_locals_dict ); -/* -STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { - machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]); - gpio_set_irq_enabled(self->id, GPIO_IRQ_ALL, false); - irq->flags = 0; - irq->trigger = new_trigger; - gpio_set_irq_enabled(self->id, new_trigger, true); - return 0; -} - -STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { - machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]); - if (info_type == MP_IRQ_INFO_FLAGS) { - return irq->flags; - } else if (info_type == MP_IRQ_INFO_TRIGGERS) { - return irq->trigger; - } - return 0; -} - -STATIC const mp_irq_methods_t machine_pin_irq_methods = { - .trigger = machine_pin_irq_trigger, - .info = machine_pin_irq_info, -}; -*/ - mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { if (!mp_obj_is_type(obj, &machine_pin_type)) { mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin")); diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index b18aa2f9b4355..0a1db7df0c5b1 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -101,7 +101,7 @@ static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); - gpio_set_pin_level(pin, 0); + gpio_set_pin_pull_mode(pin, GPIO_PULL_UP); machine_pin_open_drain_mask[pin / 32] |= 1 << (pin % 32); } @@ -127,10 +127,12 @@ static inline void mp_hal_pin_high(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); + gpio_set_pin_level(pin, 0); } static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) { gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(pin, GPIO_PULL_UP); } #endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H From e8615f5813855a3f839714fb62c7d8a5e0082072 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 20:56:18 +0200 Subject: [PATCH 2682/3533] samd/machine_pin: Allow specifying a pin or LED by its name as a string. The names are defined in pins.csv. --- ports/samd/boards/make-pins.py | 10 ++++++---- ports/samd/machine_led.c | 6 ++---- ports/samd/machine_pin.c | 23 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/ports/samd/boards/make-pins.py b/ports/samd/boards/make-pins.py index 400a9c9b8870f..2b62ba7c59593 100644 --- a/ports/samd/boards/make-pins.py +++ b/ports/samd/boards/make-pins.py @@ -15,6 +15,8 @@ char *name; } machine_pin_obj_t; +int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size); + """ led_header_prefix = """typedef struct _machine_led_obj_t { @@ -33,19 +35,19 @@ def __init__(self): def parse_csv_file(self, filename): with open(filename, "r") as csvfile: - rows = csv.reader(csvfile) + rows = csv.reader(csvfile, skipinitialspace=True) for row in rows: # Pin numbers must start with "PIN_" # LED numbers must start with "LED_" if len(row) > 0: - if row[0].strip().startswith("PIN_"): + if row[0].startswith("PIN_"): if len(row) == 1: self.board_pins.append([row[0], row[0][4:]]) else: self.board_pins.append([row[0], row[1]]) - elif row[0].strip().startswith("LED_"): + elif row[0].startswith("LED_"): self.board_leds.append(["PIN_" + row[0][4:], row[1]]) - elif row[0].strip().startswith("-"): + elif row[0].startswith("-"): self.board_pins.append(["-1", ""]) def print_pins(self, pins_filename): diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 03b47e7cf2aac..9c157695818a8 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -30,12 +30,10 @@ #include "py/mphal.h" #include "extmod/virtpin.h" #include "modmachine.h" -#include "pins.h" // boards// +#include "pins.h" // ASF4 (MCU package specific pin defs in 'boards') #include "hal_gpio.h" -#include "hpl_gpio.h" -#include "hal_atomic.h" STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_led_obj_t *self = self_in; @@ -70,7 +68,7 @@ mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted LED object - int wanted_led = mp_obj_get_int(args[0]); + int wanted_led = pin_find(args[0], (const machine_pin_obj_t *)machine_led_obj, MP_ARRAY_SIZE(machine_led_obj)); const machine_led_obj_t *self = NULL; if (0 <= wanted_led && wanted_led < MP_ARRAY_SIZE(machine_led_obj)) { self = (machine_led_obj_t *)&machine_led_obj[wanted_led]; diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 49aecba011e9b..98317abbf26a0 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -26,6 +26,7 @@ * Uses pins.h & pins.c to create board (MCU package) specific 'machine_pin_obj' array. */ +#include "string.h" #include "py/runtime.h" #include "py/mphal.h" #include "extmod/virtpin.h" @@ -58,6 +59,26 @@ STATIC void pin_validate_drive(bool strength) { } } +int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size) { + int wanted_pin = -1; + if (mp_obj_is_small_int(pin)) { + // Pin defined by the index of pin table + wanted_pin = mp_obj_get_int(pin); + } else if (mp_obj_is_str(pin)) { + // Search by name + size_t slen; + const char *s = mp_obj_str_get_data(pin, &slen); + for (wanted_pin = 0; wanted_pin < table_size; wanted_pin++) { + if (slen == strlen(machine_pin_obj[wanted_pin].name) && + strncmp(s, machine_pin_obj[wanted_pin].name, slen) == 0) { + break; + } + } + } + return wanted_pin; +} + + // Pin.init(mode, pull=None, *, value=None, drive=0). No 'alt' yet. STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt }; @@ -112,7 +133,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted pin object - int wanted_pin = mp_obj_get_int(args[0]); + int wanted_pin = pin_find(args[0], machine_pin_obj, MP_ARRAY_SIZE(machine_pin_obj)); const machine_pin_obj_t *self = NULL; if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) { From 4013577af2f530599db80a6696c23be2ed335bee Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 4 Jun 2022 21:26:45 +0200 Subject: [PATCH 2683/3533] samd/modmachine: Enable SoftSPI and SoftI2C. --- ports/samd/Makefile | 1 + ports/samd/modmachine.c | 4 ++++ ports/samd/mpconfigport.h | 2 ++ 3 files changed, 7 insertions(+) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index e3316a9d6c114..ea71973bd7635 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -110,6 +110,7 @@ SRC_C = \ lib/tinyusb/src/device/usbd_control.c \ lib/tinyusb/src/portable/microchip/samd/dcd_samd.c \ lib/tinyusb/src/tusb.c \ + drivers/bus/softspi.c \ shared/libc/printf.c \ shared/libc/string0.c \ shared/readline/readline.c \ diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 99fccf5bf0085..b095a6b39dc96 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -28,6 +28,8 @@ #include "extmod/machine_mem.h" #include "samd_soc.h" #include "modmachine.h" +#include "extmod/machine_i2c.h" +#include "extmod/machine_spi.h" // ASF 4 #include "hal_flash.h" @@ -129,6 +131,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index c29043c5d0a2a..6441818ba2c5f 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -97,6 +97,8 @@ #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UASYNCIO (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #define MP_STATE_PORT MP_STATE_VM From 6c037af086acd90755f93cd323a5114984885449 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 08:55:27 +0200 Subject: [PATCH 2684/3533] samd/boards: Create pin_af_table.c from pin_af_table_SAMDxx.csv files. This step just creates the table. The firmware cannot be built at this commit. The next commit will complete the pin-af mechanism. --- ports/samd/Makefile | 23 ++++- ports/samd/boards/make-pin-af.py | 104 ++++++++++++++++++++ ports/samd/boards/pin-af-table-SAMD21.csv | 65 +++++++++++++ ports/samd/boards/pin-af-table-SAMD51.csv | 113 ++++++++++++++++++++++ 4 files changed, 300 insertions(+), 5 deletions(-) create mode 100644 ports/samd/boards/make-pin-af.py create mode 100644 ports/samd/boards/pin-af-table-SAMD21.csv create mode 100644 ports/samd/boards/pin-af-table-SAMD51.csv diff --git a/ports/samd/Makefile b/ports/samd/Makefile index ea71973bd7635..1556d81617201 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -45,12 +45,22 @@ INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include/pio INC += -I$(TOP)/lib/tinyusb/src +MAKE_PIN_AF = boards/make-pin-af.py +PIN_AF_TABLE_CSV = boards/pin-af-table-$(MCU_SERIES).csv +GEN_PIN_AF = pin_af_table.c + +MAKE_PINS = boards/make-pins.py +BOARD_PINS = $(BOARD_DIR)/pins.csv +GEN_PINS_SRC = $(BUILD)/pins.c +GEN_PINS_HDR = $(BUILD)/pins.h + CFLAGS_MCU_SAMD21 = -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -DMPCONFIG_MCU_H='' +CFLAGS += -DPIN_AF_TABLE_C='<$(BUILD)/$(GEN_PIN_AF)>' QSTR_GLOBAL_DEPENDENCIES += boards/mpconfig_$(MCU_SERIES_LOWER).h @@ -59,11 +69,6 @@ LDFLAGS += $(LDFLAGS_MOD) LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) -MAKE_PINS = boards/make-pins.py -BOARD_PINS = $(BOARD_DIR)/pins.csv -GEN_PINS_SRC = $(BUILD)/pins.c -GEN_PINS_HDR = $(BUILD)/pins.h - # Tune for Debugging or Optimization CFLAGS += -g # always include debug info in the ELF ifeq ($(DEBUG),1) @@ -92,6 +97,7 @@ SRC_C = \ modmachine.c \ modsamd.c \ mphalport.c \ + pin_af.c \ $(BUILD)/pins.c \ samd_flash.c \ samd_isr.c \ @@ -170,6 +176,13 @@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin $(Q)$(PYTHON) $(UF2CONV) -b $(TEXT0) -c -o $@ $< +pin_af.c: $(BUILD)/$(GEN_PIN_AF) + +$(BUILD)/$(GEN_PIN_AF): $(PIN_AF_TABLE_CSV) + $(ECHO) "Create $@" + $(Q)$(PYTHON) $(MAKE_PIN_AF) --csv $(PIN_AF_TABLE_CSV) --table $(BUILD)/$(GEN_PIN_AF) --mcu $(MCU_SERIES) + + $(GEN_PINS_SRC): $(BOARD_PINS) $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --pins $(GEN_PINS_SRC) --inc $(GEN_PINS_HDR) diff --git a/ports/samd/boards/make-pin-af.py b/ports/samd/boards/make-pin-af.py new file mode 100644 index 0000000000000..d895ef9dd99a4 --- /dev/null +++ b/ports/samd/boards/make-pin-af.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +"""Generates the pin_cap table file for the SAMD port.""" + +from __future__ import print_function + +import argparse +import sys +import csv + +table_header = """// This file was automatically generated by make-pin-cap.py +// + +""" + + +class Pins: + def __init__(self): + self.board_pins = [] # list of pin objects + + def parse_csv_file(self, filename): + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile) + for row in rows: + # Pin numbers must start with "PA", "PB", "PC" or "PD" + if len(row) > 0 and row[0].strip().upper()[:2] in ("PA", "PB", "PC", "PD"): + self.board_pins.append(row) + + def print_table(self, table_filename, mcu_name): + with open(table_filename, "wt") as table_file: + table_file.write(table_header) + table_file.write("const pin_af_t pin_af_table[] = {\n") + if mcu_name == "SAMD21": + for row in self.board_pins: + pin = "PIN_" + row[0].upper() + table_file.write(" #ifdef " + pin + "\n") + eic = row[1] if row[1] else "0xff" + adc = row[2] if row[2] else "0xff" + table_file.write(" {%s, %s, %s" % (pin, eic, adc)) + for cell in row[3:]: + if cell: + table_file.write( + ", 0x%s" % cell if len(cell) == 2 else ", 0x0%s" % cell + ) + else: + table_file.write(", 0xff") + table_file.write("},\n") + table_file.write(" #endif\n") + else: + for row in self.board_pins: + pin = "PIN_" + row[0].upper() + table_file.write(" #ifdef " + pin + "\n") + eic = row[1] if row[1] else "0xff" + adc0 = row[2] if row[2] else "0xff" + adc1 = row[3] if row[3] else "0xff" + table_file.write(" {%s, %s, %s, %s" % (pin, eic, adc0, adc1)) + for cell in row[4:]: + if cell: + table_file.write( + ", 0x%s" % cell if len(cell) == 2 else ", 0x0%s" % cell + ) + else: + table_file.write(", 0xff") + table_file.write("},\n") + table_file.write(" #endif\n") + table_file.write("};\n") + + +def main(): + parser = argparse.ArgumentParser( + prog="make-pin-cap.py", + usage="%(prog)s [options] [command]", + description="Generate MCU-specific pin cap table file", + ) + parser.add_argument( + "-c", + "--csv", + dest="csv_filename", + help="Specifies the pin-mux-xxxx.csv filename", + ) + parser.add_argument( + "-t", + "--table", + dest="table_filename", + help="Specifies the name of the generated pin cap table file", + ) + parser.add_argument( + "-m", + "--mcu", + dest="mcu_name", + help="Specifies type of the MCU (SAMD21 or SAMD51)", + ) + args = parser.parse_args(sys.argv[1:]) + + pins = Pins() + + if args.csv_filename: + pins.parse_csv_file(args.csv_filename) + + if args.table_filename: + pins.print_table(args.table_filename, args.mcu_name) + + +if __name__ == "__main__": + main() diff --git a/ports/samd/boards/pin-af-table-SAMD21.csv b/ports/samd/boards/pin-af-table-SAMD21.csv new file mode 100644 index 0000000000000..d8ab903c9e7c4 --- /dev/null +++ b/ports/samd/boards/pin-af-table-SAMD21.csv @@ -0,0 +1,65 @@ +# The pin_cap_tables contain the information anbout pin mux set and pad +# for some of the peripheral devices with many possible assignments. +# The pin_cap_table is a subset from table 7-1 of the data sheet. +# It contain the information about pin mux set and pad +# The eic and adc columns contain the decimal numer for the respecitive +# quantity, the columns for sercom, tc and tcc have in each cell +# the device number in the upper nibble, and the pad number in the lower +# nibble. If a signal is not available, the cell in the csv table is left empty. +# The first column is the pin id, not the number of the board pin. +# Rows not starting with pa, pb, pc or pd are ignored. +# When editing the table with a spread sheet, take care to import the data as text. +# +# Pin,EIC,ADC,SERCOM1,SERCOM2,TC,TCC +pa00,0,,,10,20, +pa01,1,,,11,21, +pa02,2,0,,,, +pa03,3,1,,,, +pb04,4,12,,,, +pb05,5,13,,,, +pb06,6,14,,,, +pb07,7,15,,,, +pb08,8,2,,40,40, +pb09,9,3,,41,41, +pa04,4,4,,00,00, +pa05,5,5,,01,01, +pa06,6,6,,02,10, +pa07,7,7,,03,11, +pa08,,16,00,20,00,12 +pa09,9,17,01,21,01,13 +pa10,10,18,02,22,10,02 +pa11,11,19,03,23,11,03 +pb10,10,,,42,50,04 +pb11,11,,,43,51,05 +pb12,12,,40,,40,06 +pb13,13,,41,,41,07 +pb14,14,,42,,50, +pb15,15,,43,,51, +pa12,12,,20,40,20,06 +pa13,13,,21,41,20,07 +pa14,14,,22,42,30,04 +pa15,15,,23,43,31,05 +pa16,0,,10,30,20,06 +pa17,1,,11,31,21,07 +pa18,2,,12,32,30,02 +pa19,3,,13,33,31,03 +pb16,9,,50,,60,04 +pb17,1,,51,,61,05 +pa20,4,,52,32,70,04 +pa21,5,,53,33,71,07 +pa22,6,,30,50,40,04 +pa23,7,,31,51,41,05 +pa24,12,,32,52,50,12 +pa25,13,,33,53,51,13 +pb22,6,,,52,70, +pb23,7,,,53,71, +pa27,15,,,,, +pa28,8,,,,, +pa30,10,,,12,10, +pa31,11,,,13,11, +pb30,14,,,50,00,12 +pb31,15,,,51,01,13 +pb00,0,,,52,70, +pb01,1,,,53,71, +pb02,2,,,50,60, +pb03,3,,,51,61, diff --git a/ports/samd/boards/pin-af-table-SAMD51.csv b/ports/samd/boards/pin-af-table-SAMD51.csv new file mode 100644 index 0000000000000..70a009ba52231 --- /dev/null +++ b/ports/samd/boards/pin-af-table-SAMD51.csv @@ -0,0 +1,113 @@ +# The pin_cap_table is a subset from table 6-1 of the data sheet. +# It contain the information about pin mux set and pad +# for some of the peripheral devices with many possible assignments. +# The colums represent the peripheral class, as defined in pin_cap_t. The +# column number is equivalent to the mux class. +# The eic and adc columns contain the decimal numer for the respecitive +# quantity, the columns for sercom, tc and tcc have in each cell +# the device number in the first, and the pad number in the second +# digit. If a signal is not available, the cell in the csv table is left empty. +# The first column is the pin id, not the number of the board pin. +# Rows not starting with pa, pb, pc or pd are ignored. +# When editing the table with a spread sheet, take care to import the data as text. +# +# Pin,EIC,ADC0,ADC1,SERCOM1,SERCOM2,TC,TCC1,TCC2 +pb03,9,15,,,51,61,, +pa00,0,,,,10,20,, +pa01,1,,,,11,21,, +pc00,0,,10,,,,, +pc01,1,,11,,,,, +pc02,2,,4,,,,, +pc03,3,,5,,,,, +pa02,2,0,,,,,, +pa03,3,10,,,,,, +pb04,4,,6,,,,, +pb05,5,,7,,,,, +pd00,0,,14,,,,, +pd01,1,,15,,,,, +pb06,6,,8,,,,, +pb07,7,,9,,,,, +pb08,8,2,0,,40,40,, +pb09,9,3,1,,41,41,, +pa04,4,4,,,00,00,, +pa05,5,5,,,01,01,, +pa06,6,6,,,02,10,, +pa07,7,7,,,03,11,, +pc04,4,,,60,,,00, +pc05,5,,,61,,,, +pc06,6,,,62,,,, +pc07,9,,,63,,,, +pa08,,8,2,00,21,00,00,14 +pa09,9,9,3,01,20,01,01,15 +pa10,10,10,,02,22,10,02,16 +pa11,11,11,,03,23,11,03,17 +pb10,10,,,,42,50,04,10 +pb11,12,,,,43,51,05,11 +pb12,12,,,40,,40,30,00 +pb13,13,,,41,,41,31,01 +pb14,14,,,42,,50,40,02 +pb15,15,,,43,,51,41,03 +pd08,3,,,70,61,,01, +pd09,4,,,71,60,,02, +pd10,5,,,72,62,,03, +pd11,6,,,73,63,,04, +pd12,7,,,,,,05, +pc10,10,,,62,72,,00,14 +pc11,11,,,63,73,,01,15 +pc12,12,,,70,61,,02,16 +pc13,13,,,71,60,,03,17 +pc14,14,,,72,62,,04,10 +pc15,15,,,73,63,,05,11 +pa12,12,,,20,41,20,06,12 +pa13,13,,,21,40,21,07,13 +pa14,14,,,22,42,30,20,12 +pa15,15,,,23,43,31,21,13 +pa16,0,,,10,31,20,10,04 +pa17,1,,,11,30,21,11,05 +pa18,2,,,12,32,30,12,06 +pa19,3,,,13,33,31,13,07 +pc16,0,,,60,01,,00, +pc17,1,,,61,00,,01, +pc18,2,,,62,02,,02, +pc19,3,,,63,03,,03, +pc20,4,,,,,,04, +pc21,5,,,,,,05, +pc22,6,,,10,31,,05, +pc23,7,,,11,30,,07, +pd20,10,,,12,32,,10, +pd21,11,,,13,33,,11, +pb16,0,,,50,,60,30,04 +pb17,1,,,51,,61,31,05 +pb18,2,,,52,72,,10, +pb19,3,,,53,73,,11, +pb20,4,,,30,71,,12, +pb21,5,,,31,70,,13, +pa20,4,,,52,32,70,14,00 +pa21,5,,,53,33,71,15,01 +pa22,6,,,30,51,40,16,02 +pa23,7,,,31,50,41,17,03 +pa24,8,,,32,52,50,22, +pa25,9,,,33,53,51,, +pb22,22,,,12,52,70,, +pb23,7,,,13,53,71,, +pb24,8,,,00,21,,, +pb25,9,,,01,20,,, +pb26,12,,,20,41,,12, +pb27,13,,,21,40,,13, +pb28,14,,,22,42,,14, +pb29,15,,,23,43,,15, +pc24,8,,,02,22,,, +pc25,9,,,03,23,,, +pc26,10,,,,,,, +pc27,11,,,10,,,, +pc28,12,,,11,,,, +pa27,11,,,,,,, +pa30,14,,,72,12,60,20, +pa31,15,,,73,13,61,21, +pb30,14,,,70,51,00,40,06 +pb31,15,,,71,50,01,41,07 +pc30,14,,12,,,,, +pc31,15,,13,,,,, +pb00,9,12,,,52,70,, +pb01,1,13,,,53,71,, +pb02,2,14,,,50,60,22, From 6765d4bbd66c19898158504ed8102ebd8de4c645 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 09:26:49 +0200 Subject: [PATCH 2685/3533] samd/pin_af: Add the pin af table and its helper functions. The pin af table is a representation of the MUX table from the data sheet. It provides information for each pin about the supported device functions. That information is needed by pin.irq, machine.ADC, machine.PWM, machine.UART, machine.SPI and machine.I2C. For each of these, the table tells for each pin, which device number, af number and pad number is assigned. Using the table gives a straight, uniform access to the information, where the benefit outweights the size of the table, which is not that large. The tables are MCU-specific. It is not required to tell for each board, which and where each of the above devices is available. That makes addding boards easy. Note: The information for DAC and I2S was not included, since it affects only a few pins. --- ports/samd/pin_af.c | 125 ++++++++++++++++++++++++++++++++++++++++++++ ports/samd/pin_af.h | 90 +++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 ports/samd/pin_af.c create mode 100644 ports/samd/pin_af.h diff --git a/ports/samd/pin_af.c b/ports/samd/pin_af.c new file mode 100644 index 0000000000000..8c152e0d8be02 --- /dev/null +++ b/ports/samd/pin_af.c @@ -0,0 +1,125 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file provides and checks pin capabilities as required + * for USART, I2C, SPI, PWM, ADC + * + */ + +#include + +#include "py/runtime.h" +#include "py/misc.h" +#include "pin_af.h" +#include "sam.h" + + +extern const uint8_t tcc_channel_count[]; + +#include PIN_AF_TABLE_C + +// Just look for an table entry for a given pin and raise an error +// in case of no match (which should not happen). + +const pin_af_t *get_pin_af_info(int pin_id) { + for (int i = 0; i < MP_ARRAY_SIZE(pin_af_table); i++) { + if (pin_af_table[i].pin_id == pin_id) { // Pin match + return &pin_af_table[i]; + } + } + mp_raise_ValueError(MP_ERROR_TEXT("wrong pin")); +} + +// Test, wether the given pin is defined and has signals for sercom. +// If that applies return the alt_fct and pad_nr. +// If not, an error will be raised. + +sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom_nr) { + const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + if ((pct_ptr->sercom1 >> 4) == sercom_nr) { + return (sercom_pad_config_t) {ALT_FCT_SERCOM1, pct_ptr->sercom1 & 0x0f}; + } else if ((pct_ptr->sercom2 >> 4) == sercom_nr) { + return (sercom_pad_config_t) {ALT_FCT_SERCOM2, pct_ptr->sercom2 & 0x0f}; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("wrong serial device")); + } +} + +// Test, wether the given pin is defined as ADC. +// If that applies return the adc instance and channel. +// If not, an error will be raised. + +adc_config_t get_adc_config(int pin_id, int32_t flag) { + const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + if (pct_ptr->adc0 != 0xff && (flag & (1 << pct_ptr->adc0)) == 0) { + return (adc_config_t) {0, pct_ptr->adc0}; + #if defined(MUC_SAMD51) + } else if (pct_ptr->adc1 != 0xff && (flag & (1 << (pct_ptr->adc1 + 16))) == 0) { + return (adc_config_t) {1, pct_ptr->adc1}; + #endif + } else { + mp_raise_ValueError(MP_ERROR_TEXT("ADC pin used")); + } +} + +// Test, wether the given pin is defined and has signals for pwm. +// If that applies return the alt_fct, tcc number and channel number. +// If not, an error will be raised. +// The function either supplies a channel from a wanted device, or +// tries to provide an unused device, if available. + +pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t device_status[]) { + const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + uint8_t tcc1 = pct_ptr->tcc1; + uint8_t tcc2 = pct_ptr->tcc2; + + if (wanted_dev != -1) { + if ((tcc1 >> 4) == wanted_dev) { + return (pwm_config_t) {ALT_FCT_TCC1, tcc1}; + } else if ((tcc2 >> 4) == wanted_dev) { + return (pwm_config_t) {ALT_FCT_TCC2, tcc2}; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("wrong device or channel")); + } + } else { + pwm_config_t ret = {}; + if ((tcc1 >> 4) < TCC_INST_NUM) { + ret = (pwm_config_t) {ALT_FCT_TCC1, tcc1}; + if (tcc2 == 0xff) { + return ret; + } + } + if ((tcc2 >> 4) < TCC_INST_NUM) { + // if a device in slot 1 is not available or already in use, use the one in slot 2 + if (tcc1 == 0xff || device_status[(ret.device_channel >> 4)] != 0) { + return (pwm_config_t) {ALT_FCT_TCC2, tcc2}; + } else { + return ret; + } + } else { + mp_raise_ValueError(MP_ERROR_TEXT("not a PWM pin")); + } + } +} diff --git a/ports/samd/pin_af.h b/ports/samd/pin_af.h new file mode 100644 index 0000000000000..f9dec6b7da052 --- /dev/null +++ b/ports/samd/pin_af.h @@ -0,0 +1,90 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file initialises provides and checks pin capabilities as required + * for USART, I2C, SPI, PWM, ADC + * + */ + +#if defined(MCU_SAMD21) + +typedef struct { + uint8_t pin_id; + uint8_t eic; + uint8_t adc0; + uint8_t sercom1; + uint8_t sercom2; + uint8_t tcc1; + uint8_t tcc2; +} pin_af_t; + +#define ALT_FCT_TC 4 +#define ALT_FCT_TCC1 4 +#define ALT_FCT_TCC2 5 + +#elif defined(MCU_SAMD51) + +typedef struct { + uint8_t pin_id; + uint8_t eic; + uint8_t adc0; + uint8_t adc1; + uint8_t sercom1; + uint8_t sercom2; + uint8_t tc; + uint8_t tcc1; + uint8_t tcc2; +} pin_af_t; + +#define ALT_FCT_TC 4 +#define ALT_FCT_TCC1 5 +#define ALT_FCT_TCC2 6 + +#endif + +typedef struct _sercom_pad_config_t { + uint8_t alt_fct; + uint8_t pad_nr; +} sercom_pad_config_t; + +typedef struct _adc_config_t { + uint8_t device; + uint8_t channel; +} adc_config_t; + +typedef struct _pwm_config_t { + uint8_t alt_fct; + uint8_t device_channel; +} pwm_config_t; + +#define ALT_FCT_EIC 0 +#define ALT_FCT_ADC 1 +#define ALT_FCT_SERCOM1 2 +#define ALT_FCT_SERCOM2 3 + +sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom); +adc_config_t get_adc_config(int pin_id, int32_t flag); +pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t used_dev[]); +const pin_af_t *get_pin_af_info(int pin_id); From 5c7e93ec48b04a6a049cc7f2ec90230cd64ecba2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 10:34:23 +0200 Subject: [PATCH 2686/3533] samd/machine_adc: Add the machine.ADC class. With the method read_u16(). Keyword arguments of the constructor are: - bits=n The resolution; default is 12. - average=n The average of samples, which are taken and cumulated. The default value is 16. Averaging by hw is faster than averaging in code. The ADC runs at a clock freq 1.5 MHz. A single 12 bit conversion takes 8 microseconds. --- ports/samd/Makefile | 2 + ports/samd/machine_adc.c | 238 +++++++++++++++++++++++++++++++++++++++ ports/samd/main.c | 2 + ports/samd/modmachine.c | 4 +- ports/samd/modmachine.h | 3 +- 5 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 ports/samd/machine_adc.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 1556d81617201..910ecdac31035 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -90,6 +90,7 @@ endif SRC_C = \ clock_config.c \ help.c \ + machine_adc.c \ machine_led.c \ machine_pin.c \ main.c \ @@ -138,6 +139,7 @@ endif # List of sources for qstr extraction SRC_QSTR += \ + machine_adc.c \ machine_led.c \ machine_pin.c \ modutime.c \ diff --git a/ports/samd/machine_adc.c b/ports/samd/machine_adc.c new file mode 100644 index 0000000000000..97b6a14f17a49 --- /dev/null +++ b/ports/samd/machine_adc.c @@ -0,0 +1,238 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Philipp Ebensberger + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "sam.h" +#include "pin_af.h" +#include "modmachine.h" + +typedef struct _machine_adc_obj_t { + mp_obj_base_t base; + adc_config_t adc_config; + uint8_t id; + uint8_t avg; + uint8_t bits; +} machine_adc_obj_t; + +#define DEFAULT_ADC_BITS 12 +#define DEFAULT_ADC_AVG 16 + +Adc *const adc_bases[] = ADC_INSTS; +uint32_t busy_flags = 0; +bool init_flags[2] = {false, false}; +static void adc_init(machine_adc_obj_t *self); +static uint8_t resolution[] = { + ADC_CTRLB_RESSEL_8BIT_Val, ADC_CTRLB_RESSEL_10BIT_Val, ADC_CTRLB_RESSEL_12BIT_Val +}; + +// Calculate the floor value of log2(n) +mp_int_t log2i(mp_int_t num) { + mp_int_t res = 0; + for (; num > 1; num >>= 1) { + res += 1; + } + return res; +} + +STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { + (void)kind; + machine_adc_obj_t *self = MP_OBJ_TO_PTR(o); + + mp_printf(print, "ADC(P%c%02u, ADC%u, channel=%u, bits=%u, average=%u)", + "ABCD"[self->id / 32], self->id % 32, self->adc_config.device, + self->adc_config.channel, self->bits, 1 << self->avg); +} + +STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_bits, ARG_average }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_bits, MP_ARG_INT, {.u_int = DEFAULT_ADC_BITS} }, + { MP_QSTR_average, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_ADC_AVG} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Unpack and check, whther the pin has ADC capability + int id = mp_hal_get_pin_obj(args[ARG_id].u_obj); + adc_config_t adc_config = get_adc_config(id, busy_flags); + + // Now that we have a valid device and channel, create and populate the ADC instance + machine_adc_obj_t *self = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type); + self->id = id; + self->adc_config = adc_config; + self->bits = DEFAULT_ADC_BITS; + uint16_t bits = args[ARG_bits].u_int; + if (bits >= 8 && bits <= 12) { + self->bits = bits; + } + uint32_t avg = log2i(args[ARG_average].u_int); + self->avg = (avg <= 10 ? avg : 10); + + // flag the device/channel as being in use. + busy_flags |= (1 << (self->adc_config.device * 16 + self->adc_config.channel)); + + adc_init(self); + + return MP_OBJ_FROM_PTR(self); +} + +// read_u16() +STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + Adc *adc = adc_bases[self->adc_config.device]; + // Set Input channel and resolution + // Select the pin as positive input and gnd as negative input reference, non-diff mode by default + adc->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND | self->adc_config.channel; + // set resolution. Scale 8-16 to 0 - 4 for table access. + adc->CTRLB.bit.RESSEL = resolution[(self->bits - 8) / 2]; + // Measure input voltage + adc->SWTRIG.bit.START = 1; + while (adc->INTFLAG.bit.RESRDY == 0) { + } + // Get and return the result + return MP_OBJ_NEW_SMALL_INT(adc->RESULT.reg * (65536 / (1 << self->bits))); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); + +// deinit() : release the ADC channel +STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + + busy_flags &= ~((1 << (self->adc_config.device * 16 + self->adc_config.channel))); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit); + +void adc_deinit_all(void) { + busy_flags = 0; + init_flags[0] = 0; + init_flags[1] = 0; +} + +STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + make_new, adc_obj_make_new, + print, adc_obj_print, + locals_dict, &adc_locals_dict + ); + +static void adc_init(machine_adc_obj_t *self) { + // ADC & clock init is done only once per ADC + if (init_flags[self->adc_config.device] == false) { + Adc *adc = adc_bases[self->adc_config.device]; + + init_flags[self->adc_config.device] = true; + + #if defined(MCU_SAMD21) + // Configuration SAMD21 + // Enable APBD clocks and PCHCTRL clocks; GCLK2 at 48 MHz + PM->APBCMASK.reg |= PM_APBCMASK_ADC; + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | GCLK_CLKCTRL_ID_ADC; + while (GCLK->STATUS.bit.SYNCBUSY) { + } + // Reset ADC registers + adc->CTRLA.bit.SWRST = 1; + while (adc->CTRLA.bit.SWRST) { + } + // Get the calibration data + uint32_t bias = (*((uint32_t *)ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; + uint32_t linearity = (*((uint32_t *)ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; + linearity |= ((*((uint32_t *)ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5; + /* Write the calibration data. */ + ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity); + // Divide 48MHz clock by 32 to obtain 1.5 MHz clock to adc + adc->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV32; + // Select external AREFA as reference voltage. + adc->REFCTRL.reg = ADC_REFCTRL_REFSEL_AREFA; + // Average: Accumulate samples and scale them down accordingly + adc->AVGCTRL.reg = self->avg | ADC_AVGCTRL_ADJRES(self->avg); + // Enable ADC and wait to be ready + adc->CTRLA.bit.ENABLE = 1; + while (adc->STATUS.bit.SYNCBUSY) { + } + + #elif defined(MCU_SAMD51) + // Configuration SAMD51 + // Enable APBD clocks and PCHCTRL clocks; GCLK2 at 48 MHz + if (self->adc_config.device == 0) { + GCLK->PCHCTRL[ADC0_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK2 | GCLK_PCHCTRL_CHEN; + MCLK->APBDMASK.bit.ADC0_ = 1; + } else { + GCLK->PCHCTRL[ADC1_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK2 | GCLK_PCHCTRL_CHEN; + MCLK->APBDMASK.bit.ADC1_ = 1; + } + // Reset ADC registers + adc->CTRLA.bit.SWRST = 1; + while (adc->CTRLA.bit.SWRST) { + } + // Get the calibration data + uint32_t biascomp; + uint32_t biasr2r; + uint32_t biasrefbuf; + if (self->adc_config.device == 0) { + biascomp = (*((uint32_t *)ADC0_FUSES_BIASCOMP_ADDR) & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; + biasr2r = (*((uint32_t *)ADC0_FUSES_BIASR2R_ADDR) & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; + biasrefbuf = (*((uint32_t *)ADC0_FUSES_BIASREFBUF_ADDR) & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; + } else { + biascomp = (*((uint32_t *)ADC1_FUSES_BIASCOMP_ADDR) & ADC1_FUSES_BIASCOMP_Msk) >> ADC1_FUSES_BIASCOMP_Pos; + biasr2r = (*((uint32_t *)ADC1_FUSES_BIASR2R_ADDR) & ADC1_FUSES_BIASR2R_Msk) >> ADC1_FUSES_BIASR2R_Pos; + biasrefbuf = (*((uint32_t *)ADC1_FUSES_BIASREFBUF_ADDR) & ADC1_FUSES_BIASREFBUF_Msk) >> ADC1_FUSES_BIASREFBUF_Pos; + } + /* Write the calibration data. */ + adc->CALIB.reg = ADC_CALIB_BIASCOMP(biascomp) | ADC_CALIB_BIASR2R(biasr2r) | ADC_CALIB_BIASREFBUF(biasrefbuf); + // Divide 48MHz clock by 32 to obtain 1.5 MHz clock to adc + adc->CTRLA.reg = ADC_CTRLA_PRESCALER_DIV32; + // Select external AREFA as reference voltage. + adc->REFCTRL.reg = ADC_REFCTRL_REFSEL_AREFA; + // Average: Accumulate samples and scale them down accordingly + adc->AVGCTRL.reg = self->avg | ADC_AVGCTRL_ADJRES(self->avg); + // Enable ADC and wait to be ready + adc->CTRLA.bit.ENABLE = 1; + while (adc->SYNCBUSY.bit.ENABLE) { + } + + #endif + } + // Set the port as given in self->id as ADC + mp_hal_set_pin_mux(self->id, ALT_FCT_ADC); +} diff --git a/ports/samd/main.c b/ports/samd/main.c index d8c1c596c5b06..ece86f5889b6a 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -33,6 +33,7 @@ #include "shared/runtime/pyexec.h" extern uint8_t _sstack, _estack, _sheap, _eheap; +extern void adc_deinit_all(void); void samd_main(void) { mp_stack_set_top(&_estack); @@ -62,6 +63,7 @@ void samd_main(void) { } mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); + adc_deinit_all(); gc_sweep_all(); mp_deinit(); } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index b095a6b39dc96..287641cc33aa6 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -129,8 +129,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, - { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, + { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, }; diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index f7ad2b5e5cbc4..53914a10c7fc3 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -28,7 +28,8 @@ #include "py/obj.h" -extern const mp_obj_type_t machine_pin_type; +extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_led_type; +extern const mp_obj_type_t machine_pin_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H From d693758ab28a6b25601f019db78c7f9bc213be38 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 11:03:59 +0200 Subject: [PATCH 2687/3533] samd/machine_pwm: Add the machine.PWM class. Features are: - 3 to 5 different frequency groups. - Freq range of 1Hz - 24 MHz. - Duty rate stays stable on freq change. Keyword options to the PWM constructor: - device=n Select a specific PWM device. If no device is specified, a free device is chosen, if available at that pin. - freq=nnnn - duty_u16=nnnn - duty_ns=nnnn - invert=True/False Allowing two outputs on the same device/channel to have complementary signals. If both freq and duty are provided, PWM output will start immediately. Pins at the same device have the same frequency. If the PWM output number exceeds the number of channels at the PWM device, the effctive channel_no is output_no % channel_count. So with a channel count of 4, output 7 is assigned to channel 3. Pins at a certain channel have the same frequency and duty rate, but may be seperately inverted. --- ports/samd/Makefile | 1 + ports/samd/machine_pwm.c | 362 ++++++++++++++++++++++++++++++++++++++ ports/samd/main.c | 2 + ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/mpconfigport.h | 4 + ports/samd/mphalport.c | 4 + ports/samd/mphalport.h | 1 + 8 files changed, 376 insertions(+) create mode 100644 ports/samd/machine_pwm.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 910ecdac31035..514c92ba1bd6f 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -142,6 +142,7 @@ SRC_QSTR += \ machine_adc.c \ machine_led.c \ machine_pin.c \ + machine_pwm.c \ modutime.c \ modmachine.c \ modsamd.c \ diff --git a/ports/samd/machine_pwm.c b/ports/samd/machine_pwm.c new file mode 100644 index 0000000000000..6f9ca42b1b2aa --- /dev/null +++ b/ports/samd/machine_pwm.c @@ -0,0 +1,362 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "modmachine.h" + +#include "sam.h" +#include "pin_af.h" + +/******************************************************************************/ +// MicroPython bindings for machine.PWM + +typedef struct _machine_pwm_obj_t { + mp_obj_base_t base; + Tcc *instance; + uint8_t pin_id; + uint8_t alt_fct; + uint8_t device; + uint8_t channel; + uint8_t output; + uint16_t prescaler; + uint32_t period; // full period count ticks +} machine_pwm_obj_t; + +#define PWM_NOT_INIT (0) +#define PWM_CLK_READY (1) +#define PWM_TCC_ENABLED (2) +#define PWM_MASTER_CLK (48000000) +#define PWM_FULL_SCALE (65536) + +static Tcc *tcc_instance[] = TCC_INSTS; + +#if defined(MCU_SAMD21) + +static const int tcc_gclk_id[] = { + GCLK_CLKCTRL_ID_TCC0_TCC1, GCLK_CLKCTRL_ID_TCC0_TCC1, GCLK_CLKCTRL_ID_TCC2_TC3 +}; +const uint8_t tcc_channel_count[] = {4, 2, 2}; +const static uint8_t tcc_channel_offset[] = {0, 4, 6}; +static uint32_t pwm_duty_values[8]; + +#define PERBUF PERB +#define CCBUF CCB + +#elif defined(MCU_SAMD51) + +static const int tcc_gclk_id[] = { + TCC0_GCLK_ID, TCC1_GCLK_ID, TCC2_GCLK_ID, + #if TCC_INST_NUM > 3 + TCC3_GCLK_ID, TCC4_GCLK_ID + #endif +}; + +#if TCC_INST_NUM > 3 +const uint8_t tcc_channel_count[] = {6, 4, 3, 2, 2}; +const static uint8_t tcc_channel_offset[] = {0, 6, 10, 13, 15}; +static uint32_t pwm_duty_values[17]; +#else +const uint8_t tcc_channel_count[] = {6, 4, 3}; +const static uint8_t tcc_channel_offset[] = {0, 6, 10}; +static uint32_t pwm_duty_values[13]; +#endif // TCC_INST_NUM > 3 + +#endif // defined(MCU_SAMD51) + +#define put_duty_value(device, channel, duty) \ + pwm_duty_values[tcc_channel_offset[device] + channel] = duty; + +#define get_duty_value(device, channel) \ + pwm_duty_values[tcc_channel_offset[device] + channel] + +static uint8_t duty_type_flags[TCC_INST_NUM]; +static uint8_t device_status[TCC_INST_NUM]; +static uint8_t output_active[TCC_INST_NUM]; +const uint16_t prescaler_table[] = {1, 2, 4, 8, 16, 64, 256, 1024}; + +STATIC void pwm_stop_device(int device); +STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq); +STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u16); +STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns); + +STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "PWM P%c%02u device=%u channel=%u output=%u", + "ABCD"[self->pin_id / 32], self->pin_id % 32, self->device, self->channel, self->output); +} + +// PWM(pin) +STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_freq, ARG_duty_u16, ARG_duty_ns, ARG_invert, ARG_device }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_freq, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_duty_u16, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_duty_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_invert, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_device, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get GPIO and optional device to connect to PWM. + uint32_t pin_id = mp_hal_get_pin_obj(args[ARG_pin].u_obj); + int32_t wanted_dev = args[ARG_device].u_int; // -1 = any + + // Get the peripheral object and populate it + + pwm_config_t config = get_pwm_config(pin_id, wanted_dev, device_status); + uint8_t device = config.device_channel >> 4; + if (device >= TCC_INST_NUM) { + mp_raise_ValueError(MP_ERROR_TEXT("wrong device")); + } + + machine_pwm_obj_t *self = mp_obj_malloc(machine_pwm_obj_t, &machine_pwm_type); + self->instance = tcc_instance[device]; + self->device = device; + self->pin_id = pin_id; + self->alt_fct = config.alt_fct; + self->channel = (config.device_channel & 0x0f) % tcc_channel_count[device]; + self->output = config.device_channel & 0x0f; + self->prescaler = 1; + self->period = 1; // Use an invalid but safe value + put_duty_value(self->device, self->channel, 0); + + Tcc *tcc = self->instance; + + if (device_status[device] == PWM_NOT_INIT) { + // Enable the device clock at first use. + #if defined(MCU_SAMD21) + // Enable synchronous clock. The bits are nicely arranged + PM->APBCMASK.reg |= PM_APBCMASK_TCC0 << device; + // Select multiplexer generic clock source and enable. + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | tcc_gclk_id[device]; + // Wait while it updates synchronously. + while (GCLK->STATUS.bit.SYNCBUSY) { + } + #elif defined(MCU_SAMD51) + // GenClk2 to the tcc + GCLK->PCHCTRL[tcc_gclk_id[device]].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(2); + while (GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_GENCTRL_GCLK2) { + } + // Enable MCLK + switch (device) { + case 0: + MCLK->APBBMASK.reg |= MCLK_APBBMASK_TCC0; + break; + case 1: + MCLK->APBBMASK.reg |= MCLK_APBBMASK_TCC1; + break; + case 2: + MCLK->APBCMASK.reg |= MCLK_APBCMASK_TCC2; + break; + #if TCC_INST_NUM > 3 + case 3: + MCLK->APBCMASK.reg |= MCLK_APBCMASK_TCC3; + break; + case 4: + MCLK->APBDMASK.reg |= MCLK_APBDMASK_TCC4; + break; + #endif + } + #endif + // Reset the device + tcc->CTRLA.reg = TCC_CTRLA_SWRST; + while (tcc->SYNCBUSY.reg & TCC_SYNCBUSY_SWRST) { + } + tcc->CTRLA.reg = TCC_CTRLA_PRESCALER_DIV1; + tcc->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM; + // Flag the clock as initialized, but not the device as enabled. + device_status[device] = PWM_CLK_READY; + } + + if (args[ARG_invert].u_int != -1) { + bool invert = !!args[ARG_invert].u_int; + if (device_status[device] != PWM_CLK_READY) { + pwm_stop_device(device); + } + uint32_t mask = 1 << (self->output + TCC_DRVCTRL_INVEN0_Pos); + if (invert) { + tcc->DRVCTRL.reg |= mask; + } else { + tcc->DRVCTRL.reg &= ~(mask); + } + } + if (args[ARG_duty_u16].u_int != -1) { + mp_machine_pwm_duty_set_u16(self, args[ARG_duty_u16].u_int); + } + if (args[ARG_duty_ns].u_int != -1) { + mp_machine_pwm_duty_set_ns(self, args[ARG_duty_ns].u_int); + } + if (args[ARG_freq].u_int != -1) { + mp_machine_pwm_freq_set(self, args[ARG_freq].u_int); + } + return MP_OBJ_FROM_PTR(self); +} + +STATIC void pwm_stop_device(int device) { + Tcc *tcc = tcc_instance[device]; + tcc->CTRLA.bit.ENABLE = 0; + while (tcc->SYNCBUSY.reg & TCC_SYNCBUSY_ENABLE) { + } + device_status[device] = PWM_CLK_READY; +} + +// Stop all TTC devices +void pwm_deinit_all(void) { + for (int i = 0; i < TCC_INST_NUM; i++) { + Tcc *tcc = tcc_instance[i]; + tcc->CTRLA.reg = TCC_CTRLA_SWRST; + while (tcc->SYNCBUSY.reg & TCC_SYNCBUSY_SWRST) { + } + device_status[i] = PWM_NOT_INIT; + duty_type_flags[i] = 0; + output_active[i] = 0; + } +} + +// Switch off an output. If all outputs of a device are off, +// switch off that device. +// This stops all channels, but keeps the configuration +// Calling pwm.freq(n) will start an instance again. +STATIC void mp_machine_pwm_deinit(machine_pwm_obj_t *self) { + mp_hal_clr_pin_mux(self->pin_id); // Switch the output off + output_active[self->device] &= ~(1 << self->output); // clear output flasg + // Stop the device, if no output is active. + if (output_active[self->device] == 0) { + pwm_stop_device(self->device); + } +} + +STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) { + return MP_OBJ_NEW_SMALL_INT(PWM_MASTER_CLK / self->prescaler / self->period); +} + +STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { + // Set the frequency. The period counter is 24 bit or 16 bit with a pre-scaling + // of up to 1024, allowing a range from 24 MHz down to 1 Hz. + static const uint32_t max_period[5] = {1 << 24, 1 << 24, 1 << 16, 1 << 16, 1 << 16}; + + Tcc *tcc = self->instance; + if (freq < 1) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid freq")); + } + + // Get the actual settings of prescaler & period from the unit + // To be able for cope for changes. + uint32_t prev_period = tcc->PER.reg + 1; + + // Check for the right prescaler + uint8_t index; + for (index = 0; index < 8; index++) { + uint32_t temp = PWM_MASTER_CLK / prescaler_table[index] / freq; + if (temp < max_period[self->device]) { + break; + } + } + self->prescaler = prescaler_table[index]; + + uint32_t period = PWM_MASTER_CLK / self->prescaler / freq; + if (period < 2) { + mp_raise_ValueError(MP_ERROR_TEXT("freq too large")); + } + // Check, if the prescaler has to be changed and stop the device if so. + if (index != tcc->CTRLA.bit.PRESCALER) { + // stop the device + pwm_stop_device(self->device); + // update the prescaler + tcc->CTRLA.bit.PRESCALER = index; + } + // Lock the update to get a glitch-free change of period and duty cycle + tcc->CTRLBSET.reg = TCC_CTRLBSET_LUPD; + tcc->PERBUF.reg = period - 1; + self->period = period; + + // Check if the Duty rate has to be aligned again when freq or prescaler were changed. + // This condition is as well true on first call after instantiation. So (re-)configure + // all channels with a duty_u16 setting. + if (period != prev_period) { + for (uint16_t ch = 0; ch < tcc_channel_count[self->device]; ch++) { + if ((duty_type_flags[self->device] & (1 << ch)) != 0) { // duty_u16 type? + tcc->CCBUF[ch].reg = (uint64_t)get_duty_value(self->device, ch) * period / + PWM_FULL_SCALE; + } + } + } + // If the prescaler was changed, the device is disabled. So this condition is true + // after the instantiation and after a prescaler change. + // (re-)configure all channels with a duty_ns setting. + if (!(tcc->CTRLA.reg & TCC_CTRLA_ENABLE)) { + for (uint16_t ch = 0; ch < tcc_channel_count[self->device]; ch++) { + if ((duty_type_flags[self->device] & (1 << ch)) == 0) { // duty_ns type? + tcc->CCBUF[ch].reg = (uint64_t)get_duty_value(self->device, ch) * PWM_MASTER_CLK / + self->prescaler / 1000000000ULL; + } + } + } + // Remember the output as active. + output_active[self->device] |= 1 << self->output; // set output flag + // (Re-)Select PWM function for given GPIO. + mp_hal_set_pin_mux(self->pin_id, self->alt_fct); + // Enable the device, if required. + if ((device_status[self->device] & PWM_TCC_ENABLED) == 0) { + tcc->CTRLBSET.reg = TCC_CTRLBSET_CMD_UPDATE; + tcc->CTRLA.reg |= TCC_CTRLA_ENABLE; + while (tcc->SYNCBUSY.reg & TCC_SYNCBUSY_ENABLE) { + } + device_status[self->device] |= PWM_TCC_ENABLED; + } + // Unlock the register update, now that the settings are complete + tcc->CTRLBCLR.reg = TCC_CTRLBCLR_LUPD; +} + +STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) { + return MP_OBJ_NEW_SMALL_INT(self->instance->CC[self->channel].reg * PWM_FULL_SCALE / (self->instance->PER.reg + 1)); +} + +STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u16) { + put_duty_value(self->device, self->channel, duty_u16); + // If the device is enabled, than the period is set and we get a reasonable value for + // the duty cycle, set to the CCBUF register. Otherwise, PWM does not start. + if (self->instance->CTRLA.reg & TCC_CTRLA_ENABLE) { + self->instance->CCBUF[self->channel].reg = (uint64_t)duty_u16 * (self->instance->PER.reg + 1) / PWM_FULL_SCALE; + } + duty_type_flags[self->device] |= 1 << self->channel; +} + +STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) { + return MP_OBJ_NEW_SMALL_INT(1000000000ULL * self->instance->CC[self->channel].reg * self->prescaler / PWM_MASTER_CLK); +} + +STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns) { + put_duty_value(self->device, self->channel, duty_ns); + self->instance->CCBUF[self->channel].reg = (uint64_t)duty_ns * PWM_MASTER_CLK / self->prescaler / 1000000000ULL; + duty_type_flags[self->device] &= ~(1 << self->channel); +} diff --git a/ports/samd/main.c b/ports/samd/main.c index ece86f5889b6a..4af4b2b4f8e5e 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -34,6 +34,7 @@ extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); +extern void pwm_deinit_all(void); void samd_main(void) { mp_stack_set_top(&_estack); @@ -64,6 +65,7 @@ void samd_main(void) { mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); adc_deinit_all(); + pwm_deinit_all(); gc_sweep_all(); mp_deinit(); } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 287641cc33aa6..e0a0378461560 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -133,6 +133,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, }; diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 53914a10c7fc3..00e72ede1a7fd 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -31,5 +31,6 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; +extern const mp_obj_type_t machine_pwm_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 6441818ba2c5f..70bd202a313b3 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -99,6 +99,10 @@ #define MICROPY_PY_UASYNCIO (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) +#define MICROPY_PY_MACHINE_PWM (1) +#define MICROPY_PY_MACHINE_PWM_INIT (0) +#define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) +#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/samd/machine_pwm.c" #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index e51f13ab7e9fc..7a3786be8c316 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -61,6 +61,10 @@ void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux) { } } +void mp_hal_clr_pin_mux(mp_hal_pin_obj_t pin) { + int pin_grp = pin / 32; + PORT->Group[pin_grp].PINCFG[pin % 32].bit.PMUXEN = 0; // Disable Mux +} void mp_hal_delay_ms(mp_uint_t ms) { if (ms > 10) { diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 0a1db7df0c5b1..6161d9821ff99 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -84,6 +84,7 @@ extern uint32_t machine_pin_open_drain_mask[]; mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in); void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux); +void mp_hal_clr_pin_mux(mp_hal_pin_obj_t pin); static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) { return pin; From a9eef1b27653b1604d945f95c7ca6d1ce875b9de Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 11:52:33 +0200 Subject: [PATCH 2688/3533] samd/samd_isr: Rework the interrupt tables. Changes are: - Have two separate tables for SAM21 and SAMD51. - Use a short table for SAMD21. - Add a comment to each line telling what it's for, making further use easier. - Add preliminary handlers/entries for PendSV, EIC and Sercom. These will be replaced later when the respecitve modules are added. --- ports/samd/samd_isr.c | 406 ++++++++++++++++++++++++++---------------- 1 file changed, 256 insertions(+), 150 deletions(-) diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index a7956c49305fa..e16018c5348ce 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -24,14 +24,23 @@ * THE SOFTWARE. */ +#include "py/runtime.h" +#include "py/mphal.h" #include "samd_soc.h" +// includes for Softtimer +// #include "pendsv.h" +// #include "softtimer.h" typedef void (*ISR)(void); extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss; +extern void Default_Handler(void); +extern void SysTick_Handler(void); +extern void PendSV_Handler(void); +extern void EIC_Handler(void); const ISR isr_vector[]; -uint32_t systick_ms; +volatile uint32_t systick_ms; volatile uint32_t systick_ms_upper; void Reset_Handler(void) __attribute__((naked)); @@ -87,8 +96,115 @@ void SysTick_Handler(void) { if (systick_ms == 0) { systick_ms_upper += 1; } + + // if (soft_timer_next == next_tick) { + // pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler); + // } +} + +// Temporary Handlers to allow builds. +// Will be removed when the respecitve module is added. +void EIC_Handler(void) { +} + +void PendSV_Handler(void) { +} + + +static uint8_t sercom_irq_type[SERCOM_INST_NUM] = {}; +void (*sercom_irq_handler_table[])(int num) = { + // Temporarily commented until the module is added + NULL, // common_uart_irq_handler, + NULL, // common_spi_irq_handler, + NULL // common_i2c_irq_handler +}; + +void sercom_register_irq(int sercom_id, int mode) { + sercom_irq_type[sercom_id] = mode; +} + +static inline void common_sercom_irq_handler(int sercom_id) { + if (sercom_irq_handler_table[sercom_irq_type[sercom_id]]) { + sercom_irq_handler_table[sercom_irq_type[sercom_id]](sercom_id); + } +} + +void Sercom0_Handler(void) { + common_sercom_irq_handler(0); +} +void Sercom1_Handler(void) { + common_sercom_irq_handler(1); +} +void Sercom2_Handler(void) { + common_sercom_irq_handler(2); +} +void Sercom3_Handler(void) { + common_sercom_irq_handler(3); +} +void Sercom4_Handler(void) { + common_sercom_irq_handler(4); +} +void Sercom5_Handler(void) { + common_sercom_irq_handler(5); +} +#if defined(MCU_SAMD51) +void Sercom6_Handler(void) { + common_sercom_irq_handler(6); } +void Sercom7_Handler(void) { + common_sercom_irq_handler(7); +} +#endif + +#if defined(MCU_SAMD21) +const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { + (ISR)&_estack, + &Reset_Handler, + &Default_Handler, // NMI_Handler + &Default_Handler, // HardFault_Handler + &Default_Handler, // MemManage_Handler + &Default_Handler, // BusFault_Handler + &Default_Handler, // UsageFault_Handler + 0, + 0, + 0, + 0, + &Default_Handler, // SVC_Handler + &Default_Handler, // DebugMon_Handler + 0, + &PendSV_Handler, // PendSV_Handler + &SysTick_Handler, // SysTick_Handler + 0, // 0 Power Manager (PM) + 0, // 1 System Control (SYSCTRL) + 0, // 2 Watchdog Timer (WDT) + 0, // 3 Real-Time Counter (RTC) + &EIC_Handler, // 4 External Interrupt Controller (EIC) + 0, // 5 Non-Volatile Memory Controller (NVMCTRL) + 0, // 6 Direct Memory Access Controller (DMAC) + USB_Handler_wrapper,// 7 Universal Serial Bus (USB) + 0, // 8 Event System Interface (EVSYS) + &Sercom0_Handler, // 9 Serial Communication Interface 0 (SERCOM0) + &Sercom1_Handler, // 10 SAMD21G18A Serial Communication Interface 1 (SERCOM1) + &Sercom2_Handler, // 11 SAMD21G18A Serial Communication Interface 2 (SERCOM2) + &Sercom3_Handler, // 12 SAMD21G18A Serial Communication Interface 3 (SERCOM3) + &Sercom4_Handler, // 13 SAMD21G18A Serial Communication Interface 4 (SERCOM4) + &Sercom5_Handler, // 14 SAMD21G18A Serial Communication Interface 5 (SERCOM5) + 0, // 15 Timer Counter Control 0 (TCC0) + 0, // 16 Timer Counter Control 1 (TCC1) + 0, // 17 Timer Counter Control 2 (TCC2) + 0, // 18 Basic Timer Counter 3 (TC3) + 0, // 19 Basic Timer Counter 4 (TC4) + 0, // 20 Basic Timer Counter 5 (TC5) + 0, // 21 Basic Timer Counter 6 (TC6) + 0, // 22 Basic Timer Counter 7 (TC7) + 0, // 23 Analog Digital Converter (ADC) + 0, // 24 Analog Comparators (AC) + 0, // 25 Digital Analog Converter (DAC) + 0, // 26 Peripheral Touch Controller (PTC) + 0, // 27 Inter-IC Sound Interface (I2S) +}; +#else const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { (ISR)&_estack, &Reset_Handler, @@ -104,154 +220,144 @@ const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { &Default_Handler, // SVC_Handler &Default_Handler, // DebugMon_Handler 0, - &Default_Handler, // PendSV_Handler + &PendSV_Handler, // PendSV_Handler &SysTick_Handler, // SysTick_Handler - 0, // line 0 - 0, - 0, - 0, - 0, - 0, - 0, - #if defined(MCU_SAMD21) - USB_Handler_wrapper, // line 7 - #else - 0, - #endif - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - #if defined(MCU_SAMD51) - &USB_0_Handler_wrapper, // line 80 - &USB_1_Handler_wrapper, - &USB_2_Handler_wrapper, - &USB_3_Handler_wrapper, - #else - 0, - 0, - 0, - 0, - #endif - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, + 0, // 0 Power Manager (PM) + 0, // 1 Main Clock (MCLK) + 0, // 2 Oscillators Control (OSCCTRL): OSCCTRL_XOSCFAIL_0, OSCCTRL_XOSCRDY_0 + 0, // 3 Oscillators Control (OSCCTRL): OSCCTRL_XOSCFAIL_1, OSCCTRL_XOSCRDY_1 + 0, // 4 Oscillators Control (OSCCTRL): OSCCTRL_DFLLLOCKC, OSCCTRL_DFLLLOCKF, OSCCTRL_DFLLOOB, OSCCTRL_DFLLRCS, OSCCTRL_DFLLRDY + 0, // 5 Oscillators Control (OSCCTRL): OSCCTRL_DPLLLCKF_0, OSCCTRL_DPLLLCKR_0, OSCCTRL_DPLLLDRTO_0, OSCCTRL_DPLLLTO_0 + 0, // 6 Oscillators Control (OSCCTRL): OSCCTRL_DPLLLCKF_1, OSCCTRL_DPLLLCKR_1, OSCCTRL_DPLLLDRTO_1, OSCCTRL_DPLLLTO_1 + 0, // 7 32kHz Oscillators Control (OSC32KCTRL) + 0, // 8 Supply Controller (SUPC): SUPC_B12SRDY, SUPC_B33SRDY, SUP + 0, // 9 Supply Controller (SUPC): SUPC_BOD12DET, SUPC_BOD33DET + 0, // 10 Watchdog Timer (WDT) + 0, // 11 Real-Time Counter (RTC) + &EIC_Handler, // 12 External Interrupt Controller (EIC): EIC_EXTINT_0 + &EIC_Handler, // 13 External Interrupt Controller (EIC): EIC_EXTINT_1 + &EIC_Handler, // 14 External Interrupt Controller (EIC): EIC_EXTINT_2 + &EIC_Handler, // 15 External Interrupt Controller (EIC): EIC_EXTINT_3 + &EIC_Handler, // 16 External Interrupt Controller (EIC): EIC_EXTINT_4 + &EIC_Handler, // 17 External Interrupt Controller (EIC): EIC_EXTINT_5 + &EIC_Handler, // 18 External Interrupt Controller (EIC): EIC_EXTINT_6 + &EIC_Handler, // 19 External Interrupt Controller (EIC): EIC_EXTINT_7 + &EIC_Handler, // 20 External Interrupt Controller (EIC): EIC_EXTINT_8 + &EIC_Handler, // 21 External Interrupt Controller (EIC): EIC_EXTINT_9 + &EIC_Handler, // 22 External Interrupt Controller (EIC): EIC_EXTINT_10 + &EIC_Handler, // 23 External Interrupt Controller (EIC): EIC_EXTINT_11 + &EIC_Handler, // 24 External Interrupt Controller (EIC): EIC_EXTINT_12 + &EIC_Handler, // 25 External Interrupt Controller (EIC): EIC_EXTINT_13 + &EIC_Handler, // 26 External Interrupt Controller (EIC): EIC_EXTINT_14 + &EIC_Handler, // 27 External Interrupt Controller (EIC): EIC_EXTINT_15 + 0, // 28 Frequency Meter (FREQM) + 0, // 29 Non-Volatile Memory Controller (NVMCTRL): NVMCTRL_0 - _7 + 0, // 30 Non-Volatile Memory Controller (NVMCTRL): NVMCTRL_8 - _10 + 0, // 31 Direct Memory Access Controller (DMAC): DMAC_SUSP_0, DMAC_TCMPL_0, DMAC_TERR_0 + 0, // 32 Direct Memory Access Controller (DMAC): DMAC_SUSP_1, DMAC_TCMPL_1, DMAC_TERR_1 + 0, // 33 Direct Memory Access Controller (DMAC): DMAC_SUSP_2, DMAC_TCMPL_2, DMAC_TERR_2 + 0, // 34 Direct Memory Access Controller (DMAC): DMAC_SUSP_3, DMAC_TCMPL_3, DMAC_TERR_3 + 0, // 35 Direct Memory Access Controller (DMAC): DMAC_SUSP_4 - _31, DMAC_TCMPL_4 _31, DMAC_TERR_4- _31 + 0, // 36 Event System Interface (EVSYS): EVSYS_EVD_0, EVSYS_OVR_0 + 0, // 37 Event System Interface (EVSYS): EVSYS_EVD_1, EVSYS_OVR_1 + 0, // 38 Event System Interface (EVSYS): EVSYS_EVD_2, EVSYS_OVR_2 + 0, // 39 Event System Interface (EVSYS): EVSYS_EVD_3, EVSYS_OVR_3 + 0, // 40 Event System Interface (EVSYS): EVSYS_EVD_10, EVSYS_EVD_11 + 0, // 41 Peripheral Access Controller (PAC) + 0, // 42 Trigger Allocator (TAL): TAL_BRK + 0, // 43 Trigger Allocator (TAL): TAL_IPS_x + 0, + 0, // 45 RAM ECC (RAMECC) + &Sercom0_Handler, // 46 Serial Communication Interface 0 (SERCOM0): SERCOM0_0 + &Sercom0_Handler, // 47 Serial Communication Interface 0 (SERCOM0): SERCOM0_1 + &Sercom0_Handler, // 48 Serial Communication Interface 0 (SERCOM0): SERCOM0_2 + &Sercom0_Handler, // 49 Serial Communication Interface 0 (SERCOM0): SERCOM0_3 - 6 + &Sercom1_Handler, // 50 Serial Communication Interface 1 (SERCOM1): SERCOM1_0 + &Sercom1_Handler, // 51 Serial Communication Interface 1 (SERCOM1): SERCOM1_1 + &Sercom1_Handler, // 52 Serial Communication Interface 1 (SERCOM1): SERCOM1_2 + &Sercom1_Handler, // 53 Serial Communication Interface 1 (SERCOM1): SERCOM1_3 - 6 + &Sercom2_Handler, // 54 Serial Communication Interface 2 (SERCOM2): SERCOM2_0 + &Sercom2_Handler, // 55 Serial Communication Interface 2 (SERCOM2): SERCOM2_1 + &Sercom2_Handler, // 56 Serial Communication Interface 2 (SERCOM2): SERCOM2_2 + &Sercom2_Handler, // 57 Serial Communication Interface 2 (SERCOM2): SERCOM2_3 - 6 + &Sercom3_Handler, // 58 Serial Communication Interface 3 (SERCOM3): SERCOM3_0 + &Sercom3_Handler, // 59 Serial Communication Interface 3 (SERCOM3): SERCOM3_1 + &Sercom3_Handler, // 60 Serial Communication Interface 3 (SERCOM3): SERCOM3_2 + &Sercom3_Handler, // 61 Serial Communication Interface 3 (SERCOM3): SERCOM3_3 - 6 + &Sercom4_Handler, // 62 Serial Communication Interface 4 (SERCOM4): SERCOM4_0 + &Sercom4_Handler, // 63 Serial Communication Interface 4 (SERCOM4): SERCOM4_1 + &Sercom4_Handler, // 64 Serial Communication Interface 4 (SERCOM4): SERCOM4_2 + &Sercom4_Handler, // 65 Serial Communication Interface 4 (SERCOM4): SERCOM4_3 - 6 + &Sercom5_Handler, // 66 Serial Communication Interface 5 (SERCOM5): SERCOM5_0 + &Sercom5_Handler, // 67 Serial Communication Interface 5 (SERCOM5): SERCOM5_1 + &Sercom5_Handler, // 68 Serial Communication Interface 5 (SERCOM5): SERCOM5_2 + &Sercom5_Handler, // 69 Serial Communication Interface 5 (SERCOM5): SERCOM5_3 - 6 + &Sercom6_Handler, // 70 Serial Communication Interface 6 (SERCOM6): SERCOM6_0 + &Sercom6_Handler, // 71 Serial Communication Interface 6 (SERCOM6): SERCOM6_1 + &Sercom6_Handler, // 72 Serial Communication Interface 6 (SERCOM6): SERCOM6_2 + &Sercom6_Handler, // 73 Serial Communication Interface 6 (SERCOM6): SERCOM6_3 - 6 + &Sercom7_Handler, // 74 Serial Communication Interface 7 (SERCOM7): SERCOM7_0 + &Sercom7_Handler, // 75 Serial Communication Interface 7 (SERCOM7): SERCOM7_1 + &Sercom7_Handler, // 76 Serial Communication Interface 7 (SERCOM7): SERCOM7_2 + &Sercom7_Handler, // 77 Serial Communication Interface 7 (SERCOM7): SERCOM7_3 - 6 + 0, // 78 Control Area Network 0 (CAN0) + 0, // 79 Control Area Network 1 (CAN1) + &USB_0_Handler_wrapper, // 80 Universal Serial Bus (USB): USB_EORSM_DNRS, ... + &USB_1_Handler_wrapper, // 81 Universal Serial Bus (USB): USB_SOF_HSOF + &USB_2_Handler_wrapper, // 82 Universal Serial Bus (USB): USB_TRCPT0_0 - _7 + &USB_3_Handler_wrapper, // 83 Universal Serial Bus (USB): USB_TRCPT1_0 - _7 + 0, // 84 Ethernet MAC (GMAC) + 0, // 85 Timer Counter Control 0 (TCC0): TCC0_CNT_A ... + 0, // 86 Timer Counter Control 0 (TCC0): TCC0_MC_0 + 0, // 87 Timer Counter Control 0 (TCC0): TCC0_MC_1 + 0, // 88 Timer Counter Control 0 (TCC0): TCC0_MC_2 + 0, // 89 Timer Counter Control 0 (TCC0): TCC0_MC_3 + 0, // 90 Timer Counter Control 0 (TCC0): TCC0_MC_4 + 0, // 91 Timer Counter Control 0 (TCC0): TCC0_MC_5 + 0, // 92 Timer Counter Control 1 (TCC1): TCC1_CNT_A ... + 0, // 93 Timer Counter Control 1 (TCC1): TCC1_MC_0 + 0, // 94 Timer Counter Control 1 (TCC1): TCC1_MC_1 + 0, // 95 Timer Counter Control 1 (TCC1): TCC1_MC_2 + 0, // 96 Timer Counter Control 1 (TCC1): TCC1_MC_3 + 0, // 97 Timer Counter Control 2 (TCC2): TCC2_CNT_A ... + 0, // 98 Timer Counter Control 2 (TCC2): TCC2_MC_0 + 0, // 99 Timer Counter Control 2 (TCC2): TCC2_MC_1 + 0, // 100 Timer Counter Control 2 (TCC2): TCC2_MC_2 + 0, // 101 Timer Counter Control 3 (TCC3): TCC3_CNT_A ... + 0, // 102 Timer Counter Control 3 (TCC3): TCC3_MC_0 + 0, // 103 Timer Counter Control 3 (TCC3): TCC3_MC_1 + 0, // 104 Timer Counter Control 4 (TCC4): TCC4_CNT_A ... + 0, // 105 Timer Counter Control 4 (TCC4): TCC4_MC_0 + 0, // 106 Timer Counter Control 4 (TCC4): TCC4_MC_1 + 0, // 107 Basic Timer Counter 0 (TC0) + 0, // 108 Basic Timer Counter 1 (TC1) + 0, // 109 Basic Timer Counter 2 (TC2) + 0, // 110 Basic Timer Counter 3 (TC3) + 0, // 111 Basic Timer Counter 4 (TC4) + 0, // 112 Basic Timer Counter 5 (TC5) + 0, // 113 Basic Timer Counter 6 (TC6) + 0, // 114 Basic Timer Counter 7 (TC7) + 0, // 115 Quadrature Decoder (PDEC): PDEC_DIR_A, PDEC_ERR_A, PDEC_OVF, PDEC_VLC_A + 0, // 116 Quadrature Decoder (PDEC): PDEC_MC_0 + 0, // 117 Quadrature Decoder (PDEC): PDEC_MC_1 + 0, // 118 Analog Digital Converter 0 (ADC0): ADC0_OVERRUN, ADC0_WINMON + 0, // 119 Analog Digital Converter 0 (ADC0): ADC0_RESRDY + 0, // 120 Analog Digital Converter 1 (ADC1): ADC1_OVERRUN, ADC1_WINMON + 0, // 121 Analog Digital Converter 1 (ADC1): ADC1_RESRDY + 0, // 122 Analog Comparators (AC) + 0, // 123 Digital-to-Analog Converter (DAC): DAC_OVERRUN_A_x, DAC_UNDERRUN_A_x + 0, // 124 Digital-to-Analog Converter (DAC): DAC_EMPTY_0 + 0, // 125 Digital-to-Analog Converter (DAC): DAC_EMPTY_1 + 0, // 126 Digital-to-Analog Converter (DAC): DAC_RESRDY_0 + 0, // 127 Digital-to-Analog Converter (DAC): DAC_RESRDY_1 + 0, // 128 Inter-IC Sound Interface (I2S) + 0, // 129 Parallel Capture Controller (PCC) + 0, // 130 Advanced Encryption Standard (AES) + 0, // 131 True Random Generator (TRNG) + 0, // 132 Integrity Check Monitor (ICM) + 0, // 133 PUblic-Key Cryptography Controller (PUKCC) + 0, // 134 Quad SPI interface (QSPI) + 0, // 135 SD/MMC Host Controller 0 (SDHC0) + 0, // 136 SD/MMC Host Controller 1 (SDHC1) }; +#endif From 4b6f6ccf88c652401e39e621b515d06140d35225 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 14:08:44 +0200 Subject: [PATCH 2689/3533] samd/machine_pin: Add pin.irq() to the machine.Pin class. Its API conforms to the docs. There are 16 IRQ channels available, which will be used as assignable to the GPIO numbers. In most cases, the irq channel is GPIO_no % 16. --- ports/samd/Makefile | 2 + ports/samd/machine_pin.c | 200 +++++++++++++++++++++++++++++++++++++- ports/samd/main.c | 2 + ports/samd/mpconfigport.h | 6 +- ports/samd/samd_isr.c | 3 - 5 files changed, 207 insertions(+), 6 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 514c92ba1bd6f..163971cde7344 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -118,6 +118,7 @@ SRC_C = \ lib/tinyusb/src/portable/microchip/samd/dcd_samd.c \ lib/tinyusb/src/tusb.c \ drivers/bus/softspi.c \ + shared/runtime/mpirq.c \ shared/libc/printf.c \ shared/libc/string0.c \ shared/readline/readline.c \ @@ -148,6 +149,7 @@ SRC_QSTR += \ modsamd.c \ samd_flash.c \ shared/readline/readline.c \ + shared/runtime/mpirq.c \ SRC_QSTR += $(SRC_MOD) $(SRC_CXX) diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 98317abbf26a0..a70e9ad0115d7 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2016-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath (pin.irq) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,10 +30,12 @@ #include "string.h" #include "py/runtime.h" #include "py/mphal.h" +#include "shared/runtime/mpirq.h" #include "extmod/virtpin.h" #include "modmachine.h" #include "samd_soc.h" #include "pins.h" +#include "pin_af.h" #include "hal_gpio.h" @@ -42,6 +45,17 @@ #define GPIO_STRENGTH_2MA (0) #define GPIO_STRENGTH_8MA (1) +#define GPIO_IRQ_EDGE_RISE (1) +#define GPIO_IRQ_EDGE_FALL (2) + +typedef struct _machine_pin_irq_obj_t { + mp_irq_obj_t base; + uint32_t flags; + uint32_t trigger; + uint8_t pin_id; +} machine_pin_irq_obj_t; + +STATIC const mp_irq_methods_t machine_pin_irq_methods; uint32_t machine_pin_open_drain_mask[4]; @@ -262,6 +276,141 @@ STATIC mp_obj_t machine_pin_drive(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_drive_obj, 1, 2, machine_pin_drive); +// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False) +STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_handler, ARG_trigger, ARG_hard }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = 3} }, + { MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} }, + }; + machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get the IRQ object. + uint8_t eic_id = get_pin_af_info(self->id)->eic; + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); + if (irq != NULL && irq->pin_id != self->id) { + mp_raise_ValueError(MP_ERROR_TEXT("IRQ already used")); + } + + // Allocate the IRQ object if it doesn't already exist. + if (irq == NULL) { + irq = m_new_obj(machine_pin_irq_obj_t); + irq->base.base.type = &mp_irq_type; + irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods; + irq->base.parent = MP_OBJ_FROM_PTR(self); + irq->base.handler = mp_const_none; + irq->base.ishard = false; + irq->pin_id = 0xff; + MP_STATE_PORT(machine_pin_irq_objects[eic_id]) = irq; + } + // (Re-)configure the irq. + if (n_args > 1 || kw_args->used != 0) { + + // set the mux config of the pin. + mp_hal_set_pin_mux(self->id, ALT_FCT_EIC); + + // Configure IRQ. + #if defined(MCU_SAMD21) + + uint32_t irq_num = 4; + // Disable all IRQs from the affected source while data is updated. + NVIC_DisableIRQ(irq_num); + // Disable EIC + EIC->CTRL.bit.ENABLE = 0; + while (EIC->STATUS.bit.SYNCBUSY != 0) { + } + EIC->INTENCLR.reg = (1 << eic_id); + // Enable the clocks + PM->APBAMASK.bit.EIC_ |= 1; + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | EIC_GCLK_ID; + + #elif defined(MCU_SAMD51) + + uint32_t irq_num = eic_id + 12; + // Disable all IRQs from the affected source while data is updated. + NVIC_DisableIRQ(irq_num); + // Disable EIC + EIC->CTRLA.bit.ENABLE = 0; + while (EIC->SYNCBUSY.bit.ENABLE != 0) { + } + EIC->INTENCLR.reg = (1 << eic_id); + // Enable the clocks + MCLK->APBAMASK.bit.EIC_ |= 1; + GCLK->PCHCTRL[EIC_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; + + #endif + // Clear the pending interrupts flag + EIC->INTENCLR.reg = (1 << eic_id); + + // Update IRQ data. + irq->base.handler = args[ARG_handler].u_obj; + irq->base.ishard = args[ARG_hard].u_bool; + irq->flags = 0; + irq->trigger = args[ARG_trigger].u_int; + irq->pin_id = self->id; + + // Enable IRQ if a handler is given. + if (args[ARG_handler].u_obj != mp_const_none) { + // Set EIC channel mode + EIC->CONFIG[eic_id / 8].reg |= irq->trigger << ((eic_id % 8) * 4); + EIC->INTENSET.reg = (1 << eic_id); + EIC->INTFLAG.reg |= (1 << eic_id); + } + + // Enable EIC (again) + #if defined(MCU_SAMD21) + EIC->CTRL.bit.ENABLE = 1; + while (EIC->STATUS.bit.SYNCBUSY != 0) { + } + #elif defined(MCU_SAMD51) + EIC->CTRLA.bit.ENABLE = 1; + while (EIC->SYNCBUSY.bit.ENABLE != 0) { + } + #endif + // Enable interrupt again + NVIC_EnableIRQ(irq_num); + } + return MP_OBJ_FROM_PTR(irq); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq); + +void pin_irq_deinit_all(void) { + + EIC->INTENCLR.reg = 0xffff; // Disable all interrupts from the EIC. + for (int i = 0; i < 16; i++) { // Clear all irq object pointers + MP_STATE_PORT(machine_pin_irq_objects[i]) = NULL; + } + // Disable all irq's at the NVIC controller + #if defined(MCU_SAMD21) + NVIC_DisableIRQ(4); + #elif defined(MCU_SAMD51) + for (int i = 12; i < 20; i++) { + NVIC_DisableIRQ(i); + } + #endif +} + +// Common EIC handler for all events. +void EIC_Handler() { + uint32_t mask = 1; + uint32_t isr = EIC->INTFLAG.reg; + for (int eic_id = 0; eic_id < 16; eic_id++, mask <<= 1) { + // Did the ISR fire? + if (isr & mask) { + EIC->INTFLAG.reg |= mask; // clear the ISR flag + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); + if (irq != NULL) { + irq->flags = irq->trigger; + mp_irq_handler(&irq->base); + break; + } + } + } +} + STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) }, @@ -273,6 +422,7 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&machine_pin_disable_obj) }, { MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&machine_pin_drive_obj) }, + { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, // class constants { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) }, @@ -283,6 +433,8 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_STRENGTH_2MA) }, { MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_STRENGTH_8MA) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_IRQ_EDGE_RISE) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_IRQ_EDGE_FALL) }, }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); @@ -292,10 +444,10 @@ STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, i switch (request) { case MP_PIN_READ: { - return gpio_get_pin_level(self->id); + return mp_hal_pin_read(self->id); } case MP_PIN_WRITE: { - gpio_set_pin_level(self->id, arg); + mp_hal_pin_write(self->id, arg); return 0; } } @@ -317,6 +469,48 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &machine_pin_locals_dict ); +static uint8_t find_eic_id(int pin) { + for (int eic_id = 0; eic_id < 16; eic_id++) { + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); + if (irq != NULL && irq->pin_id == pin) { + return eic_id; + } + } + return 0xff; +} + +STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint8_t eic_id = find_eic_id(self->id); + if (eic_id != 0xff) { + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); + EIC->INTENCLR.reg |= (1 << eic_id); + irq->flags = 0; + irq->trigger = new_trigger; + EIC->INTENSET.reg |= (1 << eic_id); + } + return 0; +} + +STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { + machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint8_t eic_id = find_eic_id(self->id); + if (eic_id != 0xff) { + machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); + if (info_type == MP_IRQ_INFO_FLAGS) { + return irq->flags; + } else if (info_type == MP_IRQ_INFO_TRIGGERS) { + return irq->trigger; + } + } + return 0; +} + +STATIC const mp_irq_methods_t machine_pin_irq_methods = { + .trigger = machine_pin_irq_trigger, + .info = machine_pin_irq_info, +}; + mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { if (!mp_obj_is_type(obj, &machine_pin_type)) { mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin")); @@ -324,3 +518,5 @@ mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { machine_pin_obj_t *pin = MP_OBJ_TO_PTR(obj); return pin->id; } + +MP_REGISTER_ROOT_POINTER(void *machine_pin_irq_objects[16]); diff --git a/ports/samd/main.c b/ports/samd/main.c index 4af4b2b4f8e5e..520763b5aa4ae 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -34,6 +34,7 @@ extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); +extern void pin_irq_deinit_all(void); extern void pwm_deinit_all(void); void samd_main(void) { @@ -65,6 +66,7 @@ void samd_main(void) { mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); adc_deinit_all(); + pin_irq_deinit_all(); pwm_deinit_all(); gc_sweep_all(); mp_deinit(); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 70bd202a313b3..574d47b035ded 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -54,7 +54,7 @@ #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT samd_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (1) - +#define MICROPY_ENABLE_SCHEDULER (1) // fixes sys/usys import issue #define MICROPY_MODULE_WEAK_LINKS (1) // Control over Python builtins @@ -104,6 +104,10 @@ #define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) #define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/samd/machine_pwm.c" +// Use VfsLfs's types for fileio/textio +#define mp_type_fileio mp_type_vfs_lfs1_fileio +#define mp_type_textio mp_type_vfs_lfs1_textio + #define MP_STATE_PORT MP_STATE_VM // Miscellaneous settings diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index e16018c5348ce..a4e1dd708c41d 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -104,9 +104,6 @@ void SysTick_Handler(void) { // Temporary Handlers to allow builds. // Will be removed when the respecitve module is added. -void EIC_Handler(void) { -} - void PendSV_Handler(void) { } From 7d281f6165d50a75ba21eda4d8d253a8982f3543 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 14:28:08 +0200 Subject: [PATCH 2690/3533] samd/modmachine: Add disable_irq(), enable_irq() and idle() to machine. No specific features. --- ports/samd/modmachine.c | 23 +++++++++++++++++++++++ ports/samd/mpconfigport.h | 12 ++++++++++++ 2 files changed, 35 insertions(+) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index e0a0378461560..eafe9972d065e 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -120,6 +120,25 @@ STATIC mp_obj_t machine_unique_id(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); +STATIC mp_obj_t machine_idle(void) { + MICROPY_EVENT_POLL_HOOK; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); + +STATIC mp_obj_t machine_disable_irq(void) { + uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION(); + return mp_obj_new_int(state); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq); + +STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { + uint32_t state = mp_obj_get_int(state_in); + MICROPY_END_ATOMIC_SECTION(state); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, @@ -136,6 +155,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + + { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 574d47b035ded..fba4ee13d2867 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -111,6 +111,18 @@ #define MP_STATE_PORT MP_STATE_VM // Miscellaneous settings +__attribute__((always_inline)) static inline void enable_irq(uint32_t state) { + __set_PRIMASK(state); +} + +__attribute__((always_inline)) static inline uint32_t disable_irq(void) { + uint32_t state = __get_PRIMASK(); + __disable_irq(); + return state; +} + +#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() +#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) #define MICROPY_EVENT_POLL_HOOK \ do { \ From b33f2045290a379d2cefc1798780aa46f56e29a1 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 15:03:52 +0200 Subject: [PATCH 2691/3533] samd/machine_uart: Add the machine.UART class. All board pins that have UART's assigned can be used. Baud rate range is 75 Baud to ~2 MBaud. No flow control yet, and only RX is buffered. TX buffer and flow control may be added later for SAMD51 with its larger RAM and Flash. --- ports/samd/Makefile | 2 + ports/samd/machine_uart.c | 475 ++++++++++++++++++++++++++++++++++++++ ports/samd/main.c | 2 + ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/mpconfigport.h | 1 + ports/samd/samd_isr.c | 4 +- 7 files changed, 484 insertions(+), 2 deletions(-) create mode 100644 ports/samd/machine_uart.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 163971cde7344..196b7acf11bfa 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -93,6 +93,7 @@ SRC_C = \ machine_adc.c \ machine_led.c \ machine_pin.c \ + machine_uart.c \ main.c \ modutime.c \ modmachine.c \ @@ -144,6 +145,7 @@ SRC_QSTR += \ machine_led.c \ machine_pin.c \ machine_pwm.c \ + machine_uart.c \ modutime.c \ modmachine.c \ modsamd.c \ diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c new file mode 100644 index 0000000000000..9c5cfce839945 --- /dev/null +++ b/ports/samd/machine_uart.c @@ -0,0 +1,475 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/stream.h" +#include "py/ringbuf.h" +#include "modmachine.h" +#include "samd_soc.h" +#include "pin_af.h" +#include "clock_config.h" + +#define DEFAULT_UART_BAUDRATE (115200) +#define DEFAULT_BUFFER_SIZE (256) +#define MIN_BUFFER_SIZE (32) +#define MAX_BUFFER_SIZE (32766) +#define USART_BUFFER_TX (0) + +typedef struct _machine_uart_obj_t { + mp_obj_base_t base; + uint8_t id; + uint32_t baudrate; + uint8_t bits; + uint8_t parity; + uint8_t stop; + uint8_t tx; + sercom_pad_config_t tx_pad_config; + uint8_t rx; + sercom_pad_config_t rx_pad_config; + uint16_t timeout; // timeout waiting for first char (in ms) + uint16_t timeout_char; // timeout waiting between chars (in ms) + bool new; + ringbuf_t read_buffer; + #if USART_BUFFER_TX + ringbuf_t write_buffer; + #endif +} machine_uart_obj_t; + +Sercom *sercom_instance[] = SERCOM_INSTS; +machine_uart_obj_t *uart_table[SERCOM_INST_NUM] = {}; + +STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3 + +// Irq handler + +// take all bytes from the fifo and store them in the buffer +STATIC void uart_drain_rx_fifo(machine_uart_obj_t *self, Sercom *uart) { + while (uart->USART.INTFLAG.bit.RXC != 0) { + if (ringbuf_free(&self->read_buffer) > 0) { + // get a byte from uart and put into the buffer + ringbuf_put(&(self->read_buffer), uart->USART.DATA.bit.DATA); + } else { + // if the buffer is full, discard the data for now + // t.b.d.: flow control + uint32_t temp; + (void)temp; + temp = uart->USART.DATA.bit.DATA; + } + } +} + +void common_uart_irq_handler(int uart_id) { + machine_uart_obj_t *self = uart_table[uart_id]; + // Handle IRQ + if (self != NULL) { + Sercom *uart = sercom_instance[self->id]; + if (uart->USART.INTFLAG.bit.RXC != 0) { + // Now handler the incoming data + uart_drain_rx_fifo(self, uart); + } else if (uart->USART.INTFLAG.bit.DRE != 0) { + // handle the outgoing data + } else { + // Disable the other interrupts, if set by error + uart->USART.INTENCLR.reg = (uint8_t) ~(SERCOM_USART_INTENCLR_DRE | SERCOM_USART_INTENCLR_RXC); + } + } +} + +void sercom_enable(Sercom *uart, int state) { + uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off + // Wait for the Registers to update. + while (uart->USART.SYNCBUSY.bit.ENABLE) { + } +} + +STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, " + "timeout=%u, timeout_char=%u, rxbuf=%d)", + self->id, self->baudrate, self->bits, _parity_name[self->parity], + self->stop + 1, self->timeout, self->timeout_char, self->read_buffer.size - 1); +} + +STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, + ARG_timeout, ARG_timeout_char, ARG_rxbuf, ARG_txbuf}; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_bits, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_parity, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} }, + { MP_QSTR_stop, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + #if USART_BUFFER_TX + { MP_QSTR_txbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + #endif + }; + + // Parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Set baudrate if configured. + if (args[ARG_baudrate].u_int > 0) { + self->baudrate = args[ARG_baudrate].u_int; + } + + // Set bits if configured. + if (args[ARG_bits].u_int > 0) { + self->bits = args[ARG_bits].u_int; + } + + // Set parity if configured. + if (args[ARG_parity].u_obj != MP_OBJ_NEW_SMALL_INT(-1)) { + if (args[ARG_parity].u_obj == mp_const_none) { + self->parity = 0; + } else if (mp_obj_get_int(args[ARG_parity].u_obj) & 1) { + self->parity = 1; // odd + } else { + self->parity = 2; // even + } + } + + // Set stop bits if configured. + if (args[ARG_stop].u_int > 0) { + self->stop = (args[ARG_stop].u_int - 1) & 1; + } + + // Set TX/RX pins if configured. + if (args[ARG_tx].u_obj != mp_const_none) { + self->tx = mp_hal_get_pin_obj(args[ARG_tx].u_obj); + } + if (args[ARG_rx].u_obj != mp_const_none) { + self->rx = mp_hal_get_pin_obj(args[ARG_rx].u_obj); + } + + // Set timeout if configured. + if (args[ARG_timeout].u_int >= 0) { + self->timeout = args[ARG_timeout].u_int; + } + + // Set timeout_char if configured. + if (args[ARG_timeout_char].u_int >= 0) { + self->timeout_char = args[ARG_timeout_char].u_int; + } + + // Set the RX buffer size if configured. + size_t rxbuf_len = DEFAULT_BUFFER_SIZE; + if (args[ARG_rxbuf].u_int > 0) { + rxbuf_len = args[ARG_rxbuf].u_int; + if (rxbuf_len < MIN_BUFFER_SIZE) { + rxbuf_len = MIN_BUFFER_SIZE; + } else if (rxbuf_len > MAX_BUFFER_SIZE) { + mp_raise_ValueError(MP_ERROR_TEXT("rxbuf too large")); + } + } + + #if USART_BUFFER_TX + // Set the TX buffer size if configured. + size_t txbuf_len = DEFAULT_BUFFER_SIZE; + if (args[ARG_txbuf].u_int > 0) { + txbuf_len = args[ARG_txbuf].u_int; + if (txbuf_len < MIN_BUFFER_SIZE) { + txbuf_len = MIN_BUFFER_SIZE; + } else if (txbuf_len > MAX_BUFFER_SIZE) { + mp_raise_ValueError(MP_ERROR_TEXT("txbuf too large")); + } + } + #endif + // Initialise the UART peripheral if any arguments given, or it was not initialised previously. + if (n_args > 0 || kw_args->used > 0 || self->new) { + self->new = false; + + // Check the rx/tx pin assignments + if (self->tx == 0xff || self->rx == 0xff || (self->tx / 4) != (self->rx / 4)) { + mp_raise_ValueError(MP_ERROR_TEXT("Non-matching or missing rx/tx")); + } + self->rx_pad_config = get_sercom_config(self->rx, self->id); + self->tx_pad_config = get_sercom_config(self->tx, self->id); + + // Make sure timeout_char is at least as long as a whole character (13 bits to be safe). + uint32_t min_timeout_char = 13000 / self->baudrate + 1; + if (self->timeout_char < min_timeout_char) { + self->timeout_char = min_timeout_char; + } + + // Allocate the RX/TX buffers. + ringbuf_alloc(&(self->read_buffer), rxbuf_len + 1); + MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = self->read_buffer.buf; + + #if USART_BUFFER_TX + ringbuf_alloc(&(self->write_buffer), txbuf_len + 1); + MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = self->write_buffer.buf; + #endif + + // Step 1: Configure the Pin mux. + mp_hal_set_pin_mux(self->rx, self->rx_pad_config.alt_fct); + mp_hal_set_pin_mux(self->tx, self->tx_pad_config.alt_fct); + + // Next: Set up the clocks + enable_sercom_clock(self->id); + + // Next: Configure the USART + Sercom *uart = sercom_instance[self->id]; + // Reset (clear) the peripheral registers. + while (uart->USART.SYNCBUSY.bit.SWRST) { + } + uart->USART.CTRLA.bit.SWRST = 1; // Reset all Registers, disable peripheral + while (uart->USART.SYNCBUSY.bit.SWRST) { + } + + uint8_t txpo = self->tx_pad_config.pad_nr; + #if defined(MCU_SAMD21) + if (self->tx_pad_config.pad_nr == 2) { // Map pad 2 to TXPO = 1 + txpo = 1; + } + #endif + + uart->USART.CTRLA.reg = + SERCOM_USART_CTRLA_DORD // Data order + | SERCOM_USART_CTRLA_FORM(self->parity != 0 ? 1 : 0) // Enable parity or not + | SERCOM_USART_CTRLA_RXPO(self->rx_pad_config.pad_nr) // Set Pad# + | SERCOM_USART_CTRLA_TXPO(txpo) // Set Pad# + | SERCOM_USART_CTRLA_MODE(1) // USART with internal clock + ; + uart->USART.CTRLB.reg = + SERCOM_USART_CTRLB_RXEN // Enable Rx & Tx + | SERCOM_USART_CTRLB_TXEN + | ((self->parity & 1) << SERCOM_USART_CTRLB_PMODE_Pos) + | (self->stop << SERCOM_USART_CTRLB_SBMODE_Pos) + | SERCOM_USART_CTRLB_CHSIZE((self->bits & 7) | (self->bits & 1)) + ; + while (uart->USART.SYNCBUSY.bit.CTRLB) { + } + + // USART is driven by the clock of GCLK Generator 2, freq by get_apb_freq() + // baud rate; 65536 * (1 - 16 * 115200/bus_freq) + uint32_t baud = 65536 - ((uint64_t)(65536 * 16) * self->baudrate + get_apb_freq() / 2) / get_apb_freq(); + uart->USART.BAUD.bit.BAUD = baud; // Set Baud + + // Enable RXC interrupt + uart->USART.INTENSET.bit.RXC = 1; + #if defined(MCU_SAMD21) + NVIC_EnableIRQ(SERCOM0_IRQn + self->id); + #elif defined(MCU_SAMD51) + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 2); + #endif + sercom_register_irq(self->id, SERCOM_IRQ_TYPE_UART); + + sercom_enable(uart, 1); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + // Get UART bus. + int uart_id = mp_obj_get_int(args[0]); + if (uart_id < 0 || uart_id > SERCOM_INST_NUM) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) doesn't exist"), uart_id); + } + + // Create the UART object and fill it with defaults. + machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type); + self->id = uart_id; + self->baudrate = DEFAULT_UART_BAUDRATE; + self->bits = 8; + self->stop = 0; + self->timeout = 1; + self->timeout_char = 1; + self->tx = 0xff; + self->rx = 0xff; + self->new = true; + uart_table[uart_id] = self; + + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + return machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args); +} + +// uart.init(baud, [kwargs]) +STATIC mp_obj_t machine_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + return machine_uart_init_helper(args[0], n_args - 1, args + 1, kw_args); +} +MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init); + +STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + Sercom *uart = sercom_instance[self->id]; + // clear table entry of uart + uart_table[self->id] = NULL; + // Disable interrupts + uart->USART.INTENCLR.reg = 0xff; + MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = NULL; + #if USART_BUFFER_TX + MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = NULL; + #endif + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_deinit_obj, machine_uart_deinit); + +STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + // get all bytes from the fifo first + uart_drain_rx_fifo(self, sercom_instance[self->id]); + return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->read_buffer)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any); + +STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint32_t break_time_us = 13 * 1000000 / self->baudrate; + + // Wait for the TX queue & register to clear + // Since the flags are not safe, just wait sufficiently long. + // Once tx buffering is implemented, wait as well for the buffer to clear. + mp_hal_delay_us(2 * break_time_us); + // Disable MUX + PORT->Group[self->tx / 32].PINCFG[self->tx % 32].bit.PMUXEN = 0; + // Set TX pin to low for break time + mp_hal_pin_low(self->tx); + mp_hal_delay_us(break_time_us); + mp_hal_pin_high(self->tx); + // Enable Mux again + mp_hal_set_pin_mux(self->tx, self->tx_pad_config.alt_fct); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); + +void uart_deinit_all(void) { + for (int i = 0; i < SERCOM_INST_NUM; i++) { + if (uart_table[i] != NULL) { + machine_uart_deinit((mp_obj_t)uart_table[i]); + } + } +} + +STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) }, + + { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, + { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); + +STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint64_t t = mp_hal_ticks_ms() + self->timeout; + uint64_t timeout_char = self->timeout_char; + uint8_t *dest = buf_in; + Sercom *uart = sercom_instance[self->id]; + + // t.b.d. Cater timeout for timer wrap after 50 days. + for (size_t i = 0; i < size; i++) { + // Wait for the first/next character + while (ringbuf_avail(&self->read_buffer) == 0) { + if (uart->USART.INTFLAG.bit.RXC != 0) { + // Force a few incoming bytes to the buffer + uart_drain_rx_fifo(self, uart); + break; + } + if (mp_hal_ticks_ms() > t) { // timed out + if (i <= 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } else { + return i; + } + } + MICROPY_EVENT_POLL_HOOK + } + *dest++ = ringbuf_get(&(self->read_buffer)); + t = mp_hal_ticks_ms() + timeout_char; + } + return size; +} + +STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + size_t remaining = size; + const uint8_t *src = buf_in; + Sercom *uart = sercom_instance[self->id]; + + while (remaining--) { + while (!(uart->USART.INTFLAG.bit.DRE)) { + } + uart->USART.DATA.bit.DATA = *src; + src += 1; + } + + return size; +} + +STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { + machine_uart_obj_t *self = self_in; + mp_uint_t ret; + Sercom *uart = sercom_instance[self->id]; + if (request == MP_STREAM_POLL) { + uintptr_t flags = arg; + ret = 0; + if ((flags & MP_STREAM_POLL_RD) && (uart->USART.INTFLAG.bit.RXC != 0 || ringbuf_avail(&self->read_buffer) > 0)) { + ret |= MP_STREAM_POLL_RD; + } + if ((flags & MP_STREAM_POLL_WR) && (uart->USART.INTFLAG.bit.DRE != 0)) { + ret |= MP_STREAM_POLL_WR; + } + } else { + *errcode = MP_EINVAL; + ret = MP_STREAM_ERROR; + } + return ret; +} + +STATIC const mp_stream_p_t uart_stream_p = { + .read = machine_uart_read, + .write = machine_uart_write, + .ioctl = machine_uart_ioctl, + .is_text = false, +}; + +MP_DEFINE_CONST_OBJ_TYPE( + machine_uart_type, + MP_QSTR_UART, + MP_TYPE_FLAG_ITER_IS_STREAM, + make_new, machine_uart_make_new, + print, machine_uart_print, + protocol, &uart_stream_p, + locals_dict, &machine_uart_locals_dict + ); + +MP_REGISTER_ROOT_POINTER(void *samd_uart_rx_buffer[SERCOM_INST_NUM]); diff --git a/ports/samd/main.c b/ports/samd/main.c index 520763b5aa4ae..60b8e57faa445 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -36,6 +36,7 @@ extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); extern void pin_irq_deinit_all(void); extern void pwm_deinit_all(void); +extern void uart_deinit_all(void); void samd_main(void) { mp_stack_set_top(&_estack); @@ -68,6 +69,7 @@ void samd_main(void) { adc_deinit_all(); pin_irq_deinit_all(); pwm_deinit_all(); + uart_deinit_all(); gc_sweep_all(); mp_deinit(); } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index eafe9972d065e..8c3033f891bda 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -155,6 +155,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 00e72ede1a7fd..30de339b4a2d6 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -32,5 +32,6 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; +extern const mp_obj_type_t machine_uart_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index fba4ee13d2867..b8cb0f3eb74f2 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -48,6 +48,7 @@ #define MICROPY_HELPER_REPL (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ENABLE_SOURCE_LINE (1) +#define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index a4e1dd708c41d..0e0d84db6906a 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -109,9 +109,9 @@ void PendSV_Handler(void) { static uint8_t sercom_irq_type[SERCOM_INST_NUM] = {}; +// Temporarily commented until the module is added void (*sercom_irq_handler_table[])(int num) = { - // Temporarily commented until the module is added - NULL, // common_uart_irq_handler, + common_uart_irq_handler, NULL, // common_spi_irq_handler, NULL // common_i2c_irq_handler }; From aa870708ac262824ab5829e0f163f8f1e1332ada Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 17:02:46 +0200 Subject: [PATCH 2692/3533] samd/machine_spi: Add the machine.SPI class. Suported by both SAMD21 and SAMD51. It follows the generic API, except for the bits=nn option, which is not implemented (yet). --- ports/samd/Makefile | 2 + ports/samd/machine_spi.c | 330 ++++++++++++++++++++++++++++++++++++++ ports/samd/main.c | 2 + ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/mpconfigport.h | 1 + ports/samd/samd_isr.c | 2 +- 7 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 ports/samd/machine_spi.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 196b7acf11bfa..ca0309fa69935 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -93,6 +93,7 @@ SRC_C = \ machine_adc.c \ machine_led.c \ machine_pin.c \ + machine_spi.c \ machine_uart.c \ main.c \ modutime.c \ @@ -145,6 +146,7 @@ SRC_QSTR += \ machine_led.c \ machine_pin.c \ machine_pwm.c \ + machine_spi.c \ machine_uart.c \ modutime.c \ modmachine.c \ diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c new file mode 100644 index 0000000000000..b3504aeba32ba --- /dev/null +++ b/ports/samd/machine_spi.c @@ -0,0 +1,330 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "py/runtime.h" +#include "py/mphal.h" +#include "extmod/machine_spi.h" +#include "modmachine.h" +#include "samd_soc.h" +#include "pin_af.h" +#include "clock_config.h" + +#define DEFAULT_SPI_BAUDRATE (1000000) +#define DEFAULT_SPI_POLARITY (0) +#define DEFAULT_SPI_PHASE (0) +#define DEFAULT_SPI_BITS (8) +#define DEFAULT_SPI_FIRSTBIT (0) + +typedef struct _machine_spi_obj_t { + mp_obj_base_t base; + uint8_t id; + uint8_t polarity; + uint8_t phase; + uint8_t firstbit; + uint8_t sck; + uint8_t mosi; + uint8_t miso; + uint8_t new; + uint32_t baudrate; + sercom_pad_config_t sck_pad_config; + sercom_pad_config_t mosi_pad_config; + sercom_pad_config_t miso_pad_config; + uint8_t *dest; + size_t rxlen; +} machine_spi_obj_t; + +extern Sercom *sercom_instance[]; +void *sercom_table[SERCOM_INST_NUM] = {}; + +void common_spi_irq_handler(int spi_id) { + // handle Sercom IRQ RXC + machine_spi_obj_t *self = sercom_table[spi_id]; + // Handle IRQ + if (self != NULL) { + Sercom *spi = sercom_instance[self->id]; + if (spi->SPI.INTFLAG.bit.RXC != 0) { + if (self->rxlen > 0) { + *(self->dest)++ = spi->SPI.DATA.bit.DATA; + self->rxlen--; + } else { + // Just in the unlikely case there is data but no space in the buffer + // discard the data and clear the intflag + uint32_t temp; + (void)temp; + temp = spi->SPI.DATA.bit.DATA; + } + } + } +} + +STATIC void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "SPI(%u), baudrate=%u, firstbit=%u, polarity=%u, phase=%u, bits=8", + self->id, self->baudrate, self->firstbit, self->polarity, self->phase); +} + +STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_firstbit, + ARG_sck, ARG_mosi, ARG_miso}; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_firstbit, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + + // Parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Set baudrate if configured. + if (args[ARG_baudrate].u_int >= 0) { + self->baudrate = args[ARG_baudrate].u_int; + } + + // Set polarity if configured. + if (args[ARG_polarity].u_int >= 0) { + self->polarity = args[ARG_polarity].u_int; + } + + // Set phase if configured. + if (args[ARG_phase].u_int >= 0) { + self->phase = args[ARG_phase].u_int; + } + + // Set firstbit if configured. + if (args[ARG_firstbit].u_int >= 0) { + self->firstbit = args[ARG_firstbit].u_int; + } + + // Set SCK/MOSI/MISO pins if configured. + if (args[ARG_sck].u_obj != mp_const_none) { + self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj); + } + if (args[ARG_mosi].u_obj != mp_const_none) { + self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj); + } + if (args[ARG_miso].u_obj != mp_const_none) { + self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj); + } + + // Initialise the SPI peripheral if any arguments given, or it was not initialised previously. + if (n_args > 0 || kw_args->used > 0 || self->new) { + self->new = false; + + // Get the pad and alt-fct numbers. + self->sck_pad_config = get_sercom_config(self->sck, self->id); + self->mosi_pad_config = get_sercom_config(self->mosi, self->id); + + uint8_t dopo = 0; + #if defined(MCU_SAMD21) + if (self->mosi_pad_config.pad_nr == 0 && self->sck_pad_config.pad_nr == 1) { + dopo = 0; + } else if (self->mosi_pad_config.pad_nr == 2 && self->sck_pad_config.pad_nr == 3) { + dopo = 1; + } else if (self->mosi_pad_config.pad_nr == 3 && self->sck_pad_config.pad_nr == 1) { + dopo = 2; + } else if (self->mosi_pad_config.pad_nr == 0 && self->sck_pad_config.pad_nr == 3) { + dopo = 3; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for sck or mosi")); + } + #elif defined(MCU_SAMD51) + if (self->mosi_pad_config.pad_nr == 0 && self->sck_pad_config.pad_nr == 1) { + dopo = 0; + } else if (self->mosi_pad_config.pad_nr == 3 && self->sck_pad_config.pad_nr == 1) { + dopo = 2; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for sck or mosi")); + } + #endif + + if (self->miso != 0xff) { // Miso may be undefined + self->miso_pad_config = get_sercom_config(self->miso, self->id); + mp_hal_set_pin_mux(self->miso, self->miso_pad_config.alt_fct); + } + // Configure the Pin mux. + mp_hal_set_pin_mux(self->sck, self->sck_pad_config.alt_fct); + mp_hal_set_pin_mux(self->mosi, self->mosi_pad_config.alt_fct); + + // Set up the clocks + enable_sercom_clock(self->id); + + // Configure the SPI + Sercom *spi = sercom_instance[self->id]; + // Reset (clear) the peripheral registers. + while (spi->SPI.SYNCBUSY.bit.SWRST) { + } + spi->SPI.CTRLA.bit.SWRST = 1; + while (spi->SPI.SYNCBUSY.bit.SWRST) { + } + + // Set the registers + spi->SPI.CTRLA.bit.MODE = 0x03; // SPI master mode + spi->SPI.CTRLA.bit.CPOL = self->polarity; + spi->SPI.CTRLA.bit.CPHA = self->phase; + spi->SPI.CTRLA.bit.DIPO = self->miso_pad_config.pad_nr; + spi->SPI.CTRLA.bit.DOPO = dopo; + spi->SPI.CTRLA.bit.DORD = self->firstbit; + + // Enable receive only if miso is defined + if (self->miso != 0xff) { + spi->SPI.CTRLB.reg = SERCOM_SPI_CTRLB_RXEN; + while (spi->SPI.SYNCBUSY.bit.CTRLB) { + } + } + + #if defined(MCU_SAMD51) + spi->SPI.CTRLC.reg = 1; // 1 clock cycle character spacing + #endif + + // SPI is driven by the clock of GCLK Generator 2, freq in bus_freq + // baud = bus_freq / (2 * baudrate) - 1 + uint32_t baud = get_apb_freq() / (2 * self->baudrate) - 1; + spi->SPI.BAUD.reg = baud; // Set Baud + + // Enable RXC interrupt only if miso is defined + if (self->miso != 0xff) { + #if defined(MCU_SAMD21) + NVIC_EnableIRQ(SERCOM0_IRQn + self->id); + #elif defined(MCU_SAMD51) + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 2); + #endif + sercom_register_irq(self->id, SERCOM_IRQ_TYPE_SPI); + } + + sercom_enable(spi, 1); + } +} + +STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + // Get SPI bus. + int spi_id = mp_obj_get_int(args[0]); + if (spi_id < 0 || spi_id > SERCOM_INST_NUM) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); + } + + // Create the SPI object and fill it with defaults. + machine_spi_obj_t *self = mp_obj_malloc(machine_spi_obj_t, &machine_spi_type); + self->id = spi_id; + self->baudrate = DEFAULT_SPI_BAUDRATE; + self->polarity = DEFAULT_SPI_POLARITY; + self->phase = DEFAULT_SPI_PHASE; + self->firstbit = DEFAULT_SPI_FIRSTBIT; + self->mosi = 0xff; // 0xff: pin not defined (yet) + self->miso = 0xff; + self->sck = 0xff; + + self->new = true; + sercom_table[spi_id] = self; + + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_spi_init((mp_obj_base_t *)self, n_args - 1, args + 1, &kw_args); + return self; +} + +void sercom_deinit_all(void) { + for (int i = 0; i < SERCOM_INST_NUM; i++) { + if (sercom_table[i] != NULL) { + machine_spi_obj_t *self = sercom_table[i]; + Sercom *spi = sercom_instance[self->id]; + // Disable interrupts (if any) + spi->SPI.INTENCLR.reg = 0xff; + // clear table entry of spi + sercom_table[i] = NULL; + sercom_enable(spi, 0); + } + } +} + +STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; + + Sercom *spi = sercom_instance[self->id]; + size_t txlen = len; + // Clear the input queue, if needed + while (dest && spi->SPI.INTFLAG.bit.RXC) { + uint32_t temp; + (void)temp; + temp = spi->SPI.DATA.bit.DATA; + } + // Set up the irq data pointers and enable IRQ + if (dest) { + if (self->miso == 0xff) { + mp_raise_ValueError(MP_ERROR_TEXT("read is not enabled")); + } + spi->SPI.INTENSET.bit.RXC = 1; + self->dest = dest; + self->rxlen = len; + } + + // Send by polling & receive by IRQ + while (txlen) { + if (spi->SPI.INTFLAG.bit.DRE) { + spi->SPI.DATA.bit.DATA = *src; + src += 1; + txlen--; + } + } + // Receive the remaining data, if any and clear IRQ + // Do no wait forever. + if (dest) { + int32_t timeout = 1000; + while (self->rxlen > 0 && timeout) { + timeout--; + MICROPY_EVENT_POLL_HOOK + } + spi->SPI.INTENCLR.bit.RXC = 1; + } else { + // Wait for the data being shifted out. + while (!spi->SPI.INTFLAG.bit.TXC) { + } + } +} + + +STATIC const mp_machine_spi_p_t machine_spi_p = { + .init = machine_spi_init, + .transfer = machine_spi_transfer, +}; + +MP_DEFINE_CONST_OBJ_TYPE( + machine_spi_type, + MP_QSTR_SPI, + MP_TYPE_FLAG_NONE, + make_new, machine_spi_make_new, + print, machine_spi_print, + protocol, &machine_spi_p, + locals_dict, &mp_machine_spi_locals_dict + ); diff --git a/ports/samd/main.c b/ports/samd/main.c index 60b8e57faa445..1f056083a1f99 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -36,6 +36,7 @@ extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); extern void pin_irq_deinit_all(void); extern void pwm_deinit_all(void); +extern void sercom_deinit_all(void); extern void uart_deinit_all(void); void samd_main(void) { @@ -69,6 +70,7 @@ void samd_main(void) { adc_deinit_all(); pin_irq_deinit_all(); pwm_deinit_all(); + sercom_deinit_all(); uart_deinit_all(); gc_sweep_all(); mp_deinit(); diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 8c3033f891bda..712b520f54fb3 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -155,6 +155,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 30de339b4a2d6..143e3488f098f 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -32,6 +32,7 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; +extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_uart_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index b8cb0f3eb74f2..22fed00b24821 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -99,6 +99,7 @@ #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UASYNCIO (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) +#define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_MACHINE_PWM (1) #define MICROPY_PY_MACHINE_PWM_INIT (0) diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index 0e0d84db6906a..2a5a3e042ccff 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -112,7 +112,7 @@ static uint8_t sercom_irq_type[SERCOM_INST_NUM] = {}; // Temporarily commented until the module is added void (*sercom_irq_handler_table[])(int num) = { common_uart_irq_handler, - NULL, // common_spi_irq_handler, + common_spi_irq_handler, NULL // common_i2c_irq_handler }; From 94d27ae28fd4b815b3ace7dd91643aa0d1b620cd Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 18:11:06 +0200 Subject: [PATCH 2693/3533] samd/machine_i2c: Add the machine.I2C class. Using the common API. Tested with SAMD21 and SAMD51 boards. --- ports/samd/Makefile | 2 + ports/samd/machine_i2c.c | 270 ++++++++++++++++++++++++++++++++++++++ ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/mpconfigport.h | 1 + ports/samd/samd_isr.c | 4 +- 6 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 ports/samd/machine_i2c.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index ca0309fa69935..c5d36adda4b71 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -91,6 +91,7 @@ SRC_C = \ clock_config.c \ help.c \ machine_adc.c \ + machine_i2c.c \ machine_led.c \ machine_pin.c \ machine_spi.c \ @@ -143,6 +144,7 @@ endif # List of sources for qstr extraction SRC_QSTR += \ machine_adc.c \ + machine_i2c.c \ machine_led.c \ machine_pin.c \ machine_pwm.c \ diff --git a/ports/samd/machine_i2c.c b/ports/samd/machine_i2c.c new file mode 100644 index 0000000000000..4d1b03f7d578a --- /dev/null +++ b/ports/samd/machine_i2c.c @@ -0,0 +1,270 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/mperrno.h" +#include "extmod/machine_i2c.h" +#include "modmachine.h" +#include "samd_soc.h" +#include "pin_af.h" +#include "clock_config.h" + +#define DEFAULT_I2C_FREQ (400000) +#define RISETIME_NS (300) +#define I2C_TIMEOUT (100) + +#define IS_BUS_BUSY (i2c->I2CM.STATUS.bit.BUSSTATE == 3) +#define NACK_RECVD (i2c->I2CM.STATUS.bit.RXNACK == 1) +#define IRQ_DATA_SENT (i2c->I2CM.INTFLAG.bit.MB == 1) +#define IRQ_DATA_RECVD (i2c->I2CM.INTFLAG.bit.SB == 1) +#define READ_MODE ((flags & MP_MACHINE_I2C_FLAG_READ) != 0) + +#define PREPARE_ACK i2c->I2CM.CTRLB.bit.ACKACT = 0 +#define PREPARE_NACK i2c->I2CM.CTRLB.bit.ACKACT = 1 +#define SET_STOP_STATE i2c_send_command(i2c, 0x03) + +enum state_t { + state_done = 0, + state_busy, + state_buserr, + state_nack +}; + +typedef struct _machine_i2c_obj_t { + mp_obj_base_t base; + Sercom *instance; + uint8_t id; + uint8_t scl; + uint8_t sda; + uint8_t state; + uint32_t freq; + uint32_t timeout; + size_t len; + uint8_t *buf; +} machine_i2c_obj_t; + +extern Sercom *sercom_instance[]; +extern void *sercom_table[SERCOM_INST_NUM]; + +STATIC void i2c_send_command(Sercom *i2c, uint8_t command) { + i2c->I2CM.CTRLB.bit.CMD = command; + while (i2c->I2CM.SYNCBUSY.bit.SYSOP) { + } +} + +void common_i2c_irq_handler(int i2c_id) { + // handle Sercom I2C IRQ + machine_i2c_obj_t *self = sercom_table[i2c_id]; + // Handle IRQ + if (self != NULL) { + Sercom *i2c = self->instance; + // For now, clear all interrupts + if (IRQ_DATA_RECVD) { + if (self->len > 0) { + *(self->buf)++ = i2c->I2CM.DATA.reg; + self->len--; + self->timeout = I2C_TIMEOUT; + } + if (self->len > 0) { // no ACK at the last byte + PREPARE_ACK; // Send ACK + i2c_send_command(i2c, 0x02); + } else { + PREPARE_NACK; // Send NACK after the last byte + self->state = state_done; + i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_SB; + } + } else if (IRQ_DATA_SENT) { + if (NACK_RECVD) { // e.g. NACK after adress for both read and write. + self->state = state_nack; // force stop of transmission + i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_MB; + } else if (self->len > 0) { // data to be sent + i2c->I2CM.DATA.bit.DATA = *(self->buf)++; + self->len--; + self->timeout = I2C_TIMEOUT; + } else { // No data left, if there was any. + self->state = state_done; + i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_MB; + } + } else { // On any error, e.g. ARBLOST or BUSERROR, stop the transmission + self->len = 0; + self->state = state_buserr; + i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_ERROR; + } + } +} + +STATIC void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "I2C(%u, freq=%u, scl=%u, sda=%u)", + self->id, self->freq, self->scl, self->sda); +} + +mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_freq, ARG_scl, ARG_sda }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} }, + { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse args. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Get I2C bus. + int id = mp_obj_get_int(args[ARG_id].u_obj); + if (id < 0 || id >= SERCOM_INST_NUM) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), id); + } + + // Get the peripheral object. + machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_hw_i2c_type); + self->id = id; + self->instance = sercom_instance[self->id]; + + // Set SCL/SDA pins. + sercom_pad_config_t scl_pad_config; + self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); + scl_pad_config = get_sercom_config(self->scl, self->id); + + sercom_pad_config_t sda_pad_config; + self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); + sda_pad_config = get_sercom_config(self->sda, self->id); + if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for sda or scl")); + } + sercom_table[self->id] = self; + self->freq = args[ARG_freq].u_int; + + // Configure the Pin mux. + mp_hal_set_pin_mux(self->scl, scl_pad_config.alt_fct); + mp_hal_set_pin_mux(self->sda, sda_pad_config.alt_fct); + + // Set up the clocks + enable_sercom_clock(self->id); + + // Initialise the I2C peripheral + Sercom *i2c = self->instance; + + // Reset the device + i2c->I2CM.CTRLA.reg = SERCOM_I2CM_CTRLA_SWRST; + while (i2c->I2CM.SYNCBUSY.bit.SWRST == 1) { + } + // Set to master mode, inactivity timeout of 20 SCL cycles and speed. + i2c->I2CM.CTRLA.reg = SERCOM_I2CM_CTRLA_MODE(0x05) + | SERCOM_I2CM_CTRLA_INACTOUT(3) + | SERCOM_I2CM_CTRLA_SPEED(self->freq > 400000 ? 1 : 0); + + // I2C is driven by the clock of GCLK Generator 2, with it's freq in variable bus_freq + // baud = peripheral_freq / (2 * baudrate) - 5 - (rise_time * peripheral_freq) / 2 + // Just set the minimal configuration for standard and fast mode. + // Set Baud. Assume ~300ns rise time. Maybe set later by a keyword argument. + i2c->I2CM.BAUD.reg = get_apb_freq() / (2 * self->freq) - 5 - (get_apb_freq() / 1000000) * RISETIME_NS / 2000; + + // Enable interrupts + sercom_register_irq(self->id, SERCOM_IRQ_TYPE_SPI); + #if defined(MCU_SAMD21) + NVIC_EnableIRQ(SERCOM0_IRQn + self->id); + #elif defined(MCU_SAMD51) + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id); // MB interrupt + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 1); // SB interrupt + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 3); // ERRROR interrupt + #endif + + // Now enable I2C. + sercom_enable(i2c, 1); + + // Force the bus state to idle + i2c->I2CM.STATUS.bit.BUSSTATE = 1; + + return MP_OBJ_FROM_PTR(self); +} + +STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) { + machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in; + Sercom *i2c = self->instance; + + self->timeout = I2C_TIMEOUT; + self->len = len; + self->buf = buf; + // Wait a while if the bus is busy + while (IS_BUS_BUSY && self->timeout) { + MICROPY_EVENT_POLL_HOOK + if (--self->timeout == 0) { + return -MP_ETIMEDOUT; + } + } + // Enable interrupts and set the state + i2c->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR; + self->state = state_busy; + + // Send the adress, which kicks off the transfer + i2c->I2CM.ADDR.bit.ADDR = (addr << 1) | READ_MODE; + + // Transfer the data + self->timeout = I2C_TIMEOUT; + while (self->state == state_busy && self->timeout) { + self->timeout--; + MICROPY_EVENT_POLL_HOOK + } + i2c->I2CM.INTENCLR.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR; + + // Check the error states after the transfer is stopped + if (self->state == state_nack) { + SET_STOP_STATE; + return self->len == len ? -MP_ENODEV : -MP_EIO; + } else if (self->state == state_buserr) { + SET_STOP_STATE; + return -MP_EIO; + } else if (self->timeout == 0) { + SET_STOP_STATE; + return -MP_ETIMEDOUT; + } + + if (flags & MP_MACHINE_I2C_FLAG_STOP) { + SET_STOP_STATE; + } + + return len; +} + +STATIC const mp_machine_i2c_p_t machine_i2c_p = { + .transfer = mp_machine_i2c_transfer_adaptor, + .transfer_single = machine_i2c_transfer_single, +}; + +MP_DEFINE_CONST_OBJ_TYPE( + machine_hw_i2c_type, + MP_QSTR_I2C, + MP_TYPE_FLAG_NONE, + make_new, machine_i2c_make_new, + print, machine_i2c_print, + protocol, &machine_i2c_p, + locals_dict, &mp_machine_i2c_locals_dict + ); diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 712b520f54fb3..acd672fafef36 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -154,6 +154,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 143e3488f098f..4319867190ccc 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -29,6 +29,7 @@ #include "py/obj.h" extern const mp_obj_type_t machine_adc_type; +extern const mp_obj_type_t machine_hw_i2c_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 22fed00b24821..425ecc59777c8 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -98,6 +98,7 @@ #define MICROPY_PY_URANDOM (1) #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UASYNCIO (1) +#define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index 2a5a3e042ccff..5cb0668c25161 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -109,11 +109,11 @@ void PendSV_Handler(void) { static uint8_t sercom_irq_type[SERCOM_INST_NUM] = {}; -// Temporarily commented until the module is added + void (*sercom_irq_handler_table[])(int num) = { common_uart_irq_handler, common_spi_irq_handler, - NULL // common_i2c_irq_handler + common_i2c_irq_handler }; void sercom_register_irq(int sercom_id, int mode) { From 15212ae8d423dced7698219def0f2d13287a64b8 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 20:34:37 +0200 Subject: [PATCH 2694/3533] samd/moduos: Add uos.dupterm(). --- ports/samd/Makefile | 2 ++ ports/samd/moduos.c | 57 +++++++++++++++++++++++++++++++++++++++ ports/samd/mpconfigport.h | 2 ++ ports/samd/mphalport.c | 12 +++++++++ 4 files changed, 73 insertions(+) create mode 100644 ports/samd/moduos.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index c5d36adda4b71..f31e5ba6ece46 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -121,6 +121,7 @@ SRC_C = \ lib/tinyusb/src/portable/microchip/samd/dcd_samd.c \ lib/tinyusb/src/tusb.c \ drivers/bus/softspi.c \ + extmod/uos_dupterm.c \ shared/runtime/mpirq.c \ shared/libc/printf.c \ shared/libc/string0.c \ @@ -155,6 +156,7 @@ SRC_QSTR += \ modsamd.c \ samd_flash.c \ shared/readline/readline.c \ + extmod/uos_dupterm.c \ shared/runtime/mpirq.c \ SRC_QSTR += $(SRC_MOD) $(SRC_CXX) diff --git a/ports/samd/moduos.c b/ports/samd/moduos.c new file mode 100644 index 0000000000000..b1118f0195b89 --- /dev/null +++ b/ports/samd/moduos.c @@ -0,0 +1,57 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * use of the TRNG by + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2019 Artur Pacholec + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "modmachine.h" +#include "sam.h" + +#if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM +bool mp_uos_dupterm_is_builtin_stream(mp_const_obj_t stream) { + const mp_obj_type_t *type = mp_obj_get_type(stream); + return type == &machine_uart_type; +} +#endif + +#if MICROPY_PY_UOS_DUPTERM_NOTIFY +STATIC mp_obj_t mp_uos_dupterm_notify(mp_obj_t obj_in) { + (void)obj_in; + for (;;) { + int c = mp_uos_dupterm_rx_chr(); + if (c < 0) { + break; + } + ringbuf_put(&stdin_ringbuf, c); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_dupterm_notify_obj, mp_uos_dupterm_notify); +#endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 425ecc59777c8..8238c3d2b55e3 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -88,6 +88,7 @@ #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_UOS (1) +#define MICROPY_PY_UOS_INCLUDEFILE "ports/samd/moduos.c" #define MICROPY_READER_VFS (1) #define MICROPY_VFS (1) #define MICROPY_PY_UJSON (1) @@ -102,6 +103,7 @@ #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) +#define MICROPY_PY_OS_DUPTERM (3) #define MICROPY_PY_MACHINE_PWM (1) #define MICROPY_PY_MACHINE_PWM_INIT (0) #define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index 7a3786be8c316..fc451023903ce 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -97,6 +97,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { if (tud_cdc_connected() && tud_cdc_available()) { ret |= MP_STREAM_POLL_RD; } + #if MICROPY_PY_OS_DUPTERM + ret |= mp_uos_dupterm_poll(poll_flags); + #endif return ret; } @@ -109,6 +112,12 @@ int mp_hal_stdin_rx_chr(void) { return buf[0]; } } + #if MICROPY_PY_OS_DUPTERM + int dupterm_c = mp_uos_dupterm_rx_chr(); + if (dupterm_c >= 0) { + return dupterm_c; + } + #endif MICROPY_EVENT_POLL_HOOK } } @@ -128,4 +137,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { i += n2; } } + #if MICROPY_PY_OS_DUPTERM + mp_uos_dupterm_tx_strn(str, len); + #endif } From 45bf25a0022033474b9fe699267297f882ca9772 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 21:08:37 +0200 Subject: [PATCH 2695/3533] samd/moduos: Add uos.urandom() for SAMD51. Based on the hardware RNG. --- ports/samd/boards/mpconfig_samd51.h | 3 +++ ports/samd/moduos.c | 40 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/ports/samd/boards/mpconfig_samd51.h b/ports/samd/boards/mpconfig_samd51.h index 1b67b1d02d5c0..d963fe1dd295b 100644 --- a/ports/samd/boards/mpconfig_samd51.h +++ b/ports/samd/boards/mpconfig_samd51.h @@ -5,6 +5,9 @@ #define MICROPY_PY_BUILTINS_COMPLEX (0) #define MICROPY_PY_MATH (0) #define MICROPY_PY_CMATH (0) +#define MICROPY_PY_UOS_URANDOM (1) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) +unsigned long trng_random_u32(void); // Due to a limitation in the TC counter for us, the ticks period is 2**29 #define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) diff --git a/ports/samd/moduos.c b/ports/samd/moduos.c index b1118f0195b89..e0237fd88f0c8 100644 --- a/ports/samd/moduos.c +++ b/ports/samd/moduos.c @@ -34,6 +34,46 @@ #include "modmachine.h" #include "sam.h" +#if defined(MCU_SAMD51) +static bool initialized = false; + +STATIC void trng_start(void) { + if (!initialized) { + MCLK->APBCMASK.bit.TRNG_ = 1; + REG_TRNG_CTRLA = TRNG_CTRLA_ENABLE; + initialized = true; + } +} + +uint32_t trng_random_u32(void) { + trng_start(); + while ((REG_TRNG_INTFLAG & TRNG_INTFLAG_DATARDY) == 0) { + } + return REG_TRNG_DATA; +} + +#if MICROPY_PY_UOS_URANDOM +STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { + mp_int_t n = mp_obj_get_int(num); + vstr_t vstr; + vstr_init_len(&vstr, n); + uint32_t rngval = 0; + + trng_start(); + for (int i = 0; i < n; i++) { + if ((i % 4) == 0) { + rngval = trng_random_u32(); + } + vstr.buf[i] = rngval & 0xff; + rngval >>= 8; + } + return mp_obj_new_bytes_from_vstr(&vstr); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); + +#endif // MICROPY_PY_UOS_URANDOM +#endif // defined(MCU_SAMD51) + #if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM bool mp_uos_dupterm_is_builtin_stream(mp_const_obj_t stream) { const mp_obj_type_t *type = mp_obj_get_type(stream); From 4ef2da176f2ca1e7f3cb02488aba61a018033575 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 21:55:30 +0200 Subject: [PATCH 2696/3533] samd/main: Use the common execution mode of boot.py and main.py. Behaviour is: - Do not execute main.py if boot.py failed. - On a forced exit, do a soft reset. --- ports/samd/main.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ports/samd/main.c b/ports/samd/main.c index 1f056083a1f99..5aab39e5099de 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -51,8 +51,17 @@ void samd_main(void) { pyexec_frozen_module("_boot.py"); // Execute user scripts. - pyexec_file_if_exists("boot.py"); - pyexec_file_if_exists("main.py"); + int ret = pyexec_file_if_exists("boot.py"); + if (ret & PYEXEC_FORCED_EXIT) { + goto soft_reset_exit; + } + // Do not execute main.py if boot.py failed + if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL && ret != 0) { + ret = pyexec_file_if_exists("main.py"); + if (ret & PYEXEC_FORCED_EXIT) { + goto soft_reset_exit; + } + } for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { @@ -66,6 +75,7 @@ void samd_main(void) { } } + soft_reset_exit: mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); adc_deinit_all(); pin_irq_deinit_all(); From 009c51c13fc13233a28386d5e78ac79cd773cc79 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 5 Jun 2022 22:03:16 +0200 Subject: [PATCH 2697/3533] samd/mpconfigport: Enable a few more MicroPython features. Additional features are: - Support executing .mpy files. - Allow const(). - Enable auto-indent in REPL. - Enable enumerate, min/max, attrtuple, input. --- ports/samd/mpconfigport.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 8238c3d2b55e3..c2c614e10b462 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -38,14 +38,20 @@ #define MICROPY_ALLOC_PATH_MAX (256) #define MICROPY_QSTR_BYTES_IN_HASH (1) +// MicroPython emitters +#define MICROPY_PERSISTENT_CODE_LOAD (1) +#define MICROPY_EMIT_THUMB (0) +#define MICROPY_EMIT_INLINE_THUMB (0) + // Compiler configuration -#define MICROPY_COMP_CONST (0) +#define MICROPY_COMP_CONST (1) // Python internal features #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_KBD_EXCEPTION (1) #define MICROPY_HELPER_REPL (1) +#define MICROPY_REPL_AUTO_INDENT (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_STREAMS_NON_BLOCK (1) @@ -66,15 +72,16 @@ #define MICROPY_PY_BUILTINS_SET (0) #define MICROPY_PY_BUILTINS_FROZENSET (0) #define MICROPY_PY_BUILTINS_PROPERTY (0) -#define MICROPY_PY_BUILTINS_ENUMERATE (0) +#define MICROPY_PY_BUILTINS_ENUMERATE (1) #define MICROPY_PY_BUILTINS_FILTER (0) #define MICROPY_PY_BUILTINS_REVERSED (0) #define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) -#define MICROPY_PY_BUILTINS_MIN_MAX (0) +#define MICROPY_PY_BUILTINS_MIN_MAX (1) +#define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY___FILE__ (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) -#define MICROPY_PY_ATTRTUPLE (0) +#define MICROPY_PY_ATTRTUPLE (1) #define MICROPY_PY_COLLECTIONS (0) #define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_PLATFORM "samd" From 3625388d8ce028df4bb23077e1a569c987fa91fb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 10:20:44 +0200 Subject: [PATCH 2698/3533] samd/samd_isr: Change the way a Sercom ISR is registered and called. Code size diff: +12 Bytes BSS diff: -12 Bytes RAM usage: +16 Bytes Speed increase: a few clock cycles per call Style improvement: ++ --- ports/samd/machine_i2c.c | 2 +- ports/samd/machine_spi.c | 2 +- ports/samd/machine_uart.c | 2 +- ports/samd/samd_isr.c | 18 +++++++----------- ports/samd/samd_soc.h | 5 +---- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/ports/samd/machine_i2c.c b/ports/samd/machine_i2c.c index 4d1b03f7d578a..90ea5e10ca43b 100644 --- a/ports/samd/machine_i2c.c +++ b/ports/samd/machine_i2c.c @@ -188,7 +188,7 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n i2c->I2CM.BAUD.reg = get_apb_freq() / (2 * self->freq) - 5 - (get_apb_freq() / 1000000) * RISETIME_NS / 2000; // Enable interrupts - sercom_register_irq(self->id, SERCOM_IRQ_TYPE_SPI); + sercom_register_irq(self->id, &common_i2c_irq_handler); #if defined(MCU_SAMD21) NVIC_EnableIRQ(SERCOM0_IRQn + self->id); #elif defined(MCU_SAMD51) diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c index b3504aeba32ba..ddb8756e9a34e 100644 --- a/ports/samd/machine_spi.c +++ b/ports/samd/machine_spi.c @@ -218,7 +218,7 @@ STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj #elif defined(MCU_SAMD51) NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 2); #endif - sercom_register_irq(self->id, SERCOM_IRQ_TYPE_SPI); + sercom_register_irq(self->id, &common_spi_irq_handler); } sercom_enable(spi, 1); diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 9c5cfce839945..0ed7fb95d0c3f 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -281,7 +281,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args #elif defined(MCU_SAMD51) NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 2); #endif - sercom_register_irq(self->id, SERCOM_IRQ_TYPE_UART); + sercom_register_irq(self->id, &common_uart_irq_handler); sercom_enable(uart, 1); } diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index 5cb0668c25161..ed88ca229dc5a 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -108,21 +108,17 @@ void PendSV_Handler(void) { } -static uint8_t sercom_irq_type[SERCOM_INST_NUM] = {}; +void (*sercom_irq_handler_table[SERCOM_INST_NUM])(int num) = {}; -void (*sercom_irq_handler_table[])(int num) = { - common_uart_irq_handler, - common_spi_irq_handler, - common_i2c_irq_handler -}; - -void sercom_register_irq(int sercom_id, int mode) { - sercom_irq_type[sercom_id] = mode; +void sercom_register_irq(int sercom_id, void (*sercom_irq_handler)) { + if (sercom_id < SERCOM_INST_NUM) { + sercom_irq_handler_table[sercom_id] = sercom_irq_handler; + } } static inline void common_sercom_irq_handler(int sercom_id) { - if (sercom_irq_handler_table[sercom_irq_type[sercom_id]]) { - sercom_irq_handler_table[sercom_irq_type[sercom_id]](sercom_id); + if (sercom_irq_handler_table[sercom_id]) { + sercom_irq_handler_table[sercom_id](sercom_id); } } diff --git a/ports/samd/samd_soc.h b/ports/samd/samd_soc.h index b97159dc7fc5e..e8560b50e8461 100644 --- a/ports/samd/samd_soc.h +++ b/ports/samd/samd_soc.h @@ -39,11 +39,8 @@ void USB_1_Handler_wrapper(void); void USB_2_Handler_wrapper(void); void USB_3_Handler_wrapper(void); -void common_uart_irq_handler(int uart_nr); -void common_spi_irq_handler(int spi_nr); -void common_i2c_irq_handler(int i2c_nr); void sercom_enable(Sercom *spi, int state); -void sercom_register_irq(int sercom_id, int mode); +void sercom_register_irq(int sercom_id, void (*sercom_irq_handler)); #define SERCOM_IRQ_TYPE_UART (0) #define SERCOM_IRQ_TYPE_SPI (1) From 32c973d554a31d919ca582d77f4d524760093d4d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 11:13:25 +0200 Subject: [PATCH 2699/3533] samd/machine_timer: Add machine.Timer based on the shared soft-timer. --- ports/samd/Makefile | 4 + ports/samd/boards/mpconfig_samd21.h | 11 +++ ports/samd/boards/mpconfig_samd51.h | 20 ++++ ports/samd/machine_timer.c | 146 ++++++++++++++++++++++++++++ ports/samd/main.c | 2 + ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/mphalport.h | 4 + ports/samd/pendsv.c | 71 ++++++++++++++ ports/samd/pendsv.h | 41 ++++++++ ports/samd/samd_isr.c | 18 ++-- 11 files changed, 307 insertions(+), 12 deletions(-) create mode 100644 ports/samd/machine_timer.c create mode 100644 ports/samd/pendsv.c create mode 100644 ports/samd/pendsv.h diff --git a/ports/samd/Makefile b/ports/samd/Makefile index f31e5ba6ece46..e3d0d28f8f18b 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -95,12 +95,14 @@ SRC_C = \ machine_led.c \ machine_pin.c \ machine_spi.c \ + machine_timer.c \ machine_uart.c \ main.c \ modutime.c \ modmachine.c \ modsamd.c \ mphalport.c \ + pendsv.c \ pin_af.c \ $(BUILD)/pins.c \ samd_flash.c \ @@ -128,6 +130,7 @@ SRC_C = \ shared/readline/readline.c \ shared/runtime/gchelper_native.c \ shared/runtime/pyexec.c \ + shared/runtime/softtimer.c \ shared/runtime/stdout_helpers.c \ shared/runtime/sys_stdio_mphal.c \ shared/timeutils/timeutils.c \ @@ -150,6 +153,7 @@ SRC_QSTR += \ machine_pin.c \ machine_pwm.c \ machine_spi.c \ + machine_timer.c \ machine_uart.c \ modutime.c \ modmachine.c \ diff --git a/ports/samd/boards/mpconfig_samd21.h b/ports/samd/boards/mpconfig_samd21.h index 1924f66f06047..587f3f4b3ad68 100644 --- a/ports/samd/boards/mpconfig_samd21.h +++ b/ports/samd/boards/mpconfig_samd21.h @@ -7,3 +7,14 @@ #define CPU_FREQ (48000000) #define APB_FREQ (48000000) + +#define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1) + +static inline uint32_t raise_irq_pri(uint32_t pri) { + (void)pri; + return 0; +} + +static inline void restore_irq_pri(uint32_t basepri) { + (void)basepri; +} diff --git a/ports/samd/boards/mpconfig_samd51.h b/ports/samd/boards/mpconfig_samd51.h index d963fe1dd295b..95fc635e5e32d 100644 --- a/ports/samd/boards/mpconfig_samd51.h +++ b/ports/samd/boards/mpconfig_samd51.h @@ -22,3 +22,23 @@ unsigned long trng_random_u32(void); #define CPU_FREQ (120000000) #define APB_FREQ (48000000) #define DPLLx_REF_FREQ (32768) + +#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) +#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 7, 0) + +static inline uint32_t raise_irq_pri(uint32_t pri) { + uint32_t basepri = __get_BASEPRI(); + // If non-zero, the processor does not process any exception with a + // priority value greater than or equal to BASEPRI. + // When writing to BASEPRI_MAX the write goes to BASEPRI only if either: + // - Rn is non-zero and the current BASEPRI value is 0 + // - Rn is non-zero and less than the current BASEPRI value + pri <<= (8 - __NVIC_PRIO_BITS); + __ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory"); + return basepri; +} + +// "basepri" should be the value returned from raise_irq_pri +static inline void restore_irq_pri(uint32_t basepri) { + __set_BASEPRI(basepri); +} diff --git a/ports/samd/machine_timer.c b/ports/samd/machine_timer.c new file mode 100644 index 0000000000000..640d1d200fa50 --- /dev/null +++ b/ports/samd/machine_timer.c @@ -0,0 +1,146 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "shared/runtime/softtimer.h" + +typedef soft_timer_entry_t machine_timer_obj_t; + +const mp_obj_type_t machine_timer_type; + +STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); + qstr mode = self->mode == SOFT_TIMER_MODE_ONE_SHOT ? MP_QSTR_ONE_SHOT : MP_QSTR_PERIODIC; + mp_printf(print, "Timer(mode=%q, period=%u)", mode, self->delta_ms); +} + +STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} }, + { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, + { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + // Parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + self->mode = args[ARG_mode].u_int; + + uint64_t delta_ms = self->delta_ms; + if (args[ARG_freq].u_obj != mp_const_none) { + // Frequency specified in Hz + #if MICROPY_PY_BUILTINS_FLOAT + delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); + #else + delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj); + #endif + } else if (args[ARG_period].u_int != 0xffffffff) { + // Period specified + delta_ms = (uint64_t)args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int; + } + + if (delta_ms < 1) { + delta_ms = 1; + } else if (delta_ms >= 0x40000000) { + mp_raise_ValueError(MP_ERROR_TEXT("period too large")); + } + self->delta_ms = (uint32_t)delta_ms; + + if (args[ARG_callback].u_obj != MP_OBJ_NULL) { + self->py_callback = args[ARG_callback].u_obj; + } + + if (self->py_callback != mp_const_none) { + soft_timer_insert(self, self->delta_ms); + } + + return mp_const_none; +} + +STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); + self->pairheap.base.type = &machine_timer_type; + self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED; + self->delta_ms = 1000; + self->py_callback = mp_const_none; + + // Get timer id (only soft timer (-1) supported at the moment) + mp_int_t id = -1; + if (n_args > 0) { + id = mp_obj_get_int(args[0]); + --n_args; + ++args; + } + if (id != -1) { + mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist")); + } + + if (n_args > 0 || n_kw > 0) { + // Start the timer + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); + machine_timer_init_helper(self, n_args, args, &kw_args); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]); + soft_timer_remove(self); + return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init); + +STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) { + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); + soft_timer_remove(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit); + +STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) }, + + { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(SOFT_TIMER_MODE_ONE_SHOT) }, + { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(SOFT_TIMER_MODE_PERIODIC) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_timer_type, + MP_QSTR_Timer, + MP_TYPE_FLAG_NONE, + make_new, machine_timer_make_new, + print, machine_timer_print, + locals_dict, &machine_timer_locals_dict + ); diff --git a/ports/samd/main.c b/ports/samd/main.c index 5aab39e5099de..889af4a1e4736 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -31,6 +31,7 @@ #include "py/stackctrl.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" +#include "shared/runtime/softtimer.h" extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); @@ -82,6 +83,7 @@ void samd_main(void) { pwm_deinit_all(); sercom_deinit_all(); uart_deinit_all(); + soft_timer_deinit(); gc_sweep_all(); mp_deinit(); } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index acd672fafef36..852d10912d7ce 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -157,6 +157,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 4319867190ccc..b6da5e46f5ad6 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -34,6 +34,7 @@ extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; extern const mp_obj_type_t machine_spi_type; +extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_uart_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 6161d9821ff99..2eb4ce7ca680e 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -40,6 +40,10 @@ extern volatile uint32_t systick_ms_upper; void mp_hal_set_interrupt_char(int c); +// Define an alias fo systick_ms, because the shared softtimer.c uses +// the symbol uwTick for the systick ms counter. +#define uwTick systick_ms + #define mp_hal_delay_us_fast mp_hal_delay_us static inline mp_uint_t mp_hal_ticks_ms(void) { diff --git a/ports/samd/pendsv.c b/ports/samd/pendsv.c new file mode 100644 index 0000000000000..00dd6068d1e1c --- /dev/null +++ b/ports/samd/pendsv.c @@ -0,0 +1,71 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "shared/runtime/interrupt_char.h" +#include "sam.h" +#include "pendsv.h" + + +#if defined(PENDSV_DISPATCH_NUM_SLOTS) +uint32_t pendsv_dispatch_active; +pendsv_dispatch_t pendsv_dispatch_table[PENDSV_DISPATCH_NUM_SLOTS]; +#endif + +void pendsv_init(void) { + #if defined(PENDSV_DISPATCH_NUM_SLOTS) + pendsv_dispatch_active = false; + #endif + + // set PendSV interrupt at lowest priority + NVIC_SetPriority(PendSV_IRQn, IRQ_PRI_PENDSV); +} + +#if defined(PENDSV_DISPATCH_NUM_SLOTS) +void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) { + pendsv_dispatch_table[slot] = f; + pendsv_dispatch_active = true; + SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; +} + +void pendsv_dispatch_handler(void) { + for (size_t i = 0; i < PENDSV_DISPATCH_NUM_SLOTS; ++i) { + if (pendsv_dispatch_table[i] != NULL) { + pendsv_dispatch_t f = pendsv_dispatch_table[i]; + pendsv_dispatch_table[i] = NULL; + f(); + } + } +} + +void PendSV_Handler(void) { + if (pendsv_dispatch_active) { + pendsv_dispatch_handler(); + } +} +#endif diff --git a/ports/samd/pendsv.h b/ports/samd/pendsv.h new file mode 100644 index 0000000000000..c21af906b4385 --- /dev/null +++ b/ports/samd/pendsv.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_SAMD_PENDSV_H +#define MICROPY_INCLUDED_SAMD_PENDSV_H + +enum { + PENDSV_DISPATCH_SOFT_TIMER, // For later & for having at least one entry + PENDSV_DISPATCH_MAX +}; + +#define PENDSV_DISPATCH_NUM_SLOTS PENDSV_DISPATCH_MAX + +typedef void (*pendsv_dispatch_t)(void); + +void pendsv_init(void); +void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f); + +#endif // MICROPY_INCLUDED_SAMD_PENDSV_H diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index ed88ca229dc5a..b507d5d1aa737 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -27,9 +27,9 @@ #include "py/runtime.h" #include "py/mphal.h" #include "samd_soc.h" -// includes for Softtimer -// #include "pendsv.h" -// #include "softtimer.h" + +#include "pendsv.h" +#include "shared/runtime/softtimer.h" typedef void (*ISR)(void); @@ -97,17 +97,11 @@ void SysTick_Handler(void) { systick_ms_upper += 1; } - // if (soft_timer_next == next_tick) { - // pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler); - // } -} - -// Temporary Handlers to allow builds. -// Will be removed when the respecitve module is added. -void PendSV_Handler(void) { + if (soft_timer_next == next_tick) { + pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler); + } } - void (*sercom_irq_handler_table[SERCOM_INST_NUM])(int num) = {}; void sercom_register_irq(int sercom_id, void (*sercom_irq_handler)) { From 7a2f2d88f74dcad73acd1f4f7721a3575f68ffc2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 11:23:09 +0200 Subject: [PATCH 2700/3533] samd/machine_wdt: Add the machine.WDT class. --- ports/samd/Makefile | 2 + ports/samd/machine_wdt.c | 141 +++++++++++++++++++++++++++++++++++++++ ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + 4 files changed, 145 insertions(+) create mode 100644 ports/samd/machine_wdt.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index e3d0d28f8f18b..71a902a0a535e 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -97,6 +97,7 @@ SRC_C = \ machine_spi.c \ machine_timer.c \ machine_uart.c \ + machine_wdt.c \ main.c \ modutime.c \ modmachine.c \ @@ -155,6 +156,7 @@ SRC_QSTR += \ machine_spi.c \ machine_timer.c \ machine_uart.c \ + machine_wdt.c \ modutime.c \ modmachine.c \ modsamd.c \ diff --git a/ports/samd/machine_wdt.c b/ports/samd/machine_wdt.c new file mode 100644 index 0000000000000..c0fbfdbfbc7d3 --- /dev/null +++ b/ports/samd/machine_wdt.c @@ -0,0 +1,141 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "modmachine.h" + +#include "sam.h" + +#define MIN_TIMEOUT 512 +#define MAX_TIMEOUT 16384 + +typedef struct _machine_wdt_obj_t { + mp_obj_base_t base; +} machine_wdt_obj_t; + +extern mp_int_t log2i(mp_int_t num); + +STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; + +STATIC void set_timeout(uint32_t timeout) { + // Set new timeout. Have to disable WDT first. + + // Confine to the valid range + if (timeout < MIN_TIMEOUT) { + timeout = MIN_TIMEOUT; + } else if (timeout > MAX_TIMEOUT) { + timeout = MAX_TIMEOUT; + } + + #if defined(MCU_SAMD21) + WDT->CTRL.reg = 0; + while (WDT->STATUS.reg & WDT_STATUS_SYNCBUSY) { + } + WDT->CONFIG.reg = log2i(timeout) - 3; + WDT->CTRL.reg = WDT_CTRL_ENABLE; + while (WDT->STATUS.reg & WDT_STATUS_SYNCBUSY) { + } + #elif defined(MCU_SAMD51) + WDT->CTRLA.reg = 0; + while (WDT->SYNCBUSY.reg & WDT_SYNCBUSY_ENABLE) { + } + WDT->CONFIG.reg = log2i(timeout) - 3; + WDT->CTRLA.reg = WDT_CTRLA_ENABLE; + while (WDT->SYNCBUSY.reg & WDT_SYNCBUSY_ENABLE) { + } + #endif +} + +STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_timeout, ARG_lock }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + #if defined(MCU_SAMD51) + // Verify the WDT id. SAMD51 only, saving a few bytes for SAMD21 + mp_int_t id = args[ARG_id].u_int; + if (id != 0) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); + } + #endif + + // Start the watchdog (timeout is in milliseconds). + uint32_t timeout = args[ARG_timeout].u_int; + // Configure the WDT + #if defined(MCU_SAMD21) + + // Enable APBx clocks and GCLK clock + PM->APBAMASK.reg |= PM_APBAMASK_WDT; + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK8 | GCLK_CLKCTRL_ID_WDT; + + #elif defined(MCU_SAMD51) + + // Enable APBx clocks and 1kHz clock + MCLK->APBAMASK.reg |= MCLK_APBAMASK_WDT; + OSC32KCTRL->OSCULP32K.bit.EN1K = 1; + + #endif + + set_timeout(timeout); + + return MP_OBJ_FROM_PTR(&machine_wdt); +} + +STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { + (void)self_in; + WDT->CLEAR.reg = 0xa5; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); + +STATIC mp_obj_t machine_wdt_timeout_ms(mp_obj_t self_in, mp_obj_t timout_in) { + uint32_t timeout = mp_obj_get_int(timout_in); + + set_timeout(timeout); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_wdt_timeout_ms_obj, machine_wdt_timeout_ms); + +STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, + { MP_ROM_QSTR(MP_QSTR_timeout_ms), MP_ROM_PTR(&machine_wdt_timeout_ms_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + make_new, machine_wdt_make_new, + locals_dict, &machine_wdt_locals_dict + ); diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 852d10912d7ce..cabd6b9c5782d 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -159,6 +159,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index b6da5e46f5ad6..61b26a1fa302b 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -36,5 +36,6 @@ extern const mp_obj_type_t machine_pwm_type; extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_uart_type; +extern const mp_obj_type_t machine_wdt_type; #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H From 6e2dff6baea2a6109fa030a97d2ffdcafd6c3681 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 12:26:13 +0200 Subject: [PATCH 2701/3533] samd/modsamd: Add pininfo() function to the samd module. samd.pininfo() returns the data stored in the pin af table for a pin. Using a small script, a nice representation of the table can be created. --- ports/samd/machine_pin.c | 2 -- ports/samd/modsamd.c | 44 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index a70e9ad0115d7..bef19d177d9a1 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -92,7 +92,6 @@ int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_ return wanted_pin; } - // Pin.init(mode, pull=None, *, value=None, drive=0). No 'alt' yet. STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt }; @@ -157,7 +156,6 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, if (self == NULL || self->base.type == NULL) { mp_raise_ValueError(MP_ERROR_TEXT("invalid pin")); } - self = (machine_pin_obj_t *)&machine_pin_obj[wanted_pin]; if (n_args > 1 || n_kw > 0) { // pin mode given, so configure this GPIO diff --git a/ports/samd/modsamd.c b/ports/samd/modsamd.c index 68fd6b53e56d4..05ed69bad014b 100644 --- a/ports/samd/modsamd.c +++ b/ports/samd/modsamd.c @@ -24,14 +24,56 @@ * THE SOFTWARE. */ +#include "string.h" #include "py/runtime.h" +#include "py/mphal.h" + +#include "sam.h" +#include "pin_af.h" +#include "pins.h" #include "samd_soc.h" extern const mp_obj_type_t samd_flash_type; +STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) { + mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_obj); + const pin_af_t *pin_af = get_pin_af_info(pin); + const char *name = ((machine_pin_obj_t *)MP_OBJ_TO_PTR(pin_obj))->name; + if (pin_af) { + #if defined(MCU_SAMD21) + mp_obj_t tuple[7] = { + tuple[0] = mp_obj_new_str(name, strlen(name)), + tuple[1] = mp_obj_new_int(pin_af->eic), + tuple[2] = mp_obj_new_int(pin_af->adc0), + tuple[3] = mp_obj_new_int(pin_af->sercom1), + tuple[4] = mp_obj_new_int(pin_af->sercom2), + tuple[5] = mp_obj_new_int(pin_af->tcc1), + tuple[6] = mp_obj_new_int(pin_af->tcc2), + }; + return mp_obj_new_tuple(7, tuple); + #elif defined(MCU_SAMD51) + mp_obj_t tuple[9] = { + tuple[0] = mp_obj_new_str(name, strlen(name)), + tuple[1] = mp_obj_new_int(pin_af->eic), + tuple[2] = mp_obj_new_int(pin_af->adc0), + tuple[3] = mp_obj_new_int(pin_af->adc1), + tuple[4] = mp_obj_new_int(pin_af->sercom1), + tuple[5] = mp_obj_new_int(pin_af->sercom2), + tuple[6] = mp_obj_new_int(pin_af->tc), + tuple[7] = mp_obj_new_int(pin_af->tcc1), + tuple[8] = mp_obj_new_int(pin_af->tcc2), + }; + return mp_obj_new_tuple(9, tuple); + #endif + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_pininfo_obj, samd_pininfo); + STATIC const mp_rom_map_elem_t samd_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) }, - { MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) }, + { MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) }, + { MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) }, }; STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table); From aa2d746ef4ef64cbf8093b22facabca2c495be1f Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 13:35:16 +0200 Subject: [PATCH 2702/3533] samd/machine_led: Optimise size of the machine.LED class. By reducing the methods to on(), off(), toggle() and call, and using the method implementation of the machine.Pin class. The code size reduction is 756 byte. --- ports/samd/machine_led.c | 108 ++++----------------------------------- ports/samd/machine_pin.c | 6 +-- 2 files changed, 13 insertions(+), 101 deletions(-) diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 9c157695818a8..48ab869199ade 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -32,37 +32,16 @@ #include "modmachine.h" #include "pins.h" -// ASF4 (MCU package specific pin defs in 'boards') -#include "hal_gpio.h" +extern mp_obj_t machine_pin_low_obj; +extern mp_obj_t machine_pin_high_obj; +extern mp_obj_t machine_pin_toggle_obj; +extern mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_led_obj_t *self = self_in; mp_printf(print, "LED(%u)", self->id); } -// LED.init(mode, *, value=None) -STATIC mp_obj_t machine_led_obj_init_helper(const machine_led_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_mode, ARG_value }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, - { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, - }; - - // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - // set initial value (do this before configuring mode/pull) - if (args[ARG_value].u_obj != mp_const_none) { - gpio_set_pin_level(self->id, mp_obj_is_true(args[ARG_value].u_obj)); - } - - // configure mode - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - - return mp_const_none; -} - // constructor(id, ...) mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); @@ -73,90 +52,23 @@ mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, if (0 <= wanted_led && wanted_led < MP_ARRAY_SIZE(machine_led_obj)) { self = (machine_led_obj_t *)&machine_led_obj[wanted_led]; } - // the array could be padded with 'nulls' (see other Ports). // Will also error if the asked for LED (index) is greater than the array row size. if (self == NULL || self->base.type == NULL) { mp_raise_ValueError(MP_ERROR_TEXT("invalid LED")); } - - if (n_args > 1 || n_kw > 0) { - // mode given, so configure this GPIO - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - machine_led_obj_init_helper(self, n_args - 1, args + 1, &kw_args); - } + mp_hal_pin_output(self->id); + mp_hal_pin_low(self->id); return MP_OBJ_FROM_PTR(self); } -// fast method for getting/setting pin value -STATIC mp_obj_t machine_led_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 1, false); - machine_led_obj_t *self = self_in; - if (n_args == 0) { - // get pin - return MP_OBJ_NEW_SMALL_INT(gpio_get_pin_level(self->id)); - } else { - // set pin - bool value = mp_obj_is_true(args[0]); - gpio_set_pin_level(self->id, value); - - return mp_const_none; - } -} - -// pin.init(mode) -STATIC mp_obj_t machine_led_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { - return machine_led_obj_init_helper(args[0], n_args - 1, args + 1, kw_args); -} -MP_DEFINE_CONST_FUN_OBJ_KW(machine_led_init_obj, 1, machine_led_obj_init); - -// pin.value([value]) -STATIC mp_obj_t machine_led_value(size_t n_args, const mp_obj_t *args) { - return machine_led_call(args[0], n_args - 1, 0, args + 1); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_led_value_obj, 1, 2, machine_led_value); - -// pin.low() -STATIC mp_obj_t machine_led_low(mp_obj_t self_in) { - machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_set_pin_level(self->id, false); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_led_low_obj, machine_led_low); - -// pin.high() -STATIC mp_obj_t machine_led_high(mp_obj_t self_in) { - machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_set_pin_level(self->id, true); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_led_high_obj, machine_led_high); - -// pin.toggle() -STATIC mp_obj_t machine_led_toggle(mp_obj_t self_in) { - machine_led_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); - gpio_toggle_pin_level(self->id); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_led_toggle_obj, machine_led_toggle); STATIC const mp_rom_map_elem_t machine_led_locals_dict_table[] = { // instance methods - { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_led_init_obj) }, - { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_led_value_obj) }, - { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_led_low_obj) }, - { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_led_high_obj) }, - { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_led_low_obj) }, - { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_led_high_obj) }, - { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_led_toggle_obj) }, + { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_low_obj) }, + { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_high_obj) }, + { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_led_locals_dict, machine_led_locals_dict_table); @@ -166,6 +78,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_NONE, make_new, mp_led_make_new, print, machine_led_print, - call, machine_led_call, + call, machine_pin_call, locals_dict, &machine_led_locals_dict ); diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index bef19d177d9a1..e900174a9150f 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -220,7 +220,7 @@ STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); +MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); // Pin.high() Totem-pole (push-pull) STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { @@ -232,7 +232,7 @@ STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); +MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); // Pin.toggle(). Only TOGGLE pins set as OUTPUT. STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { @@ -254,7 +254,7 @@ STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); +MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); // Pin.drive(). Normal (0) is 2mA, High (1) allows 8mA. STATIC mp_obj_t machine_pin_drive(size_t n_args, const mp_obj_t *args) { From f5da77b5ce66a393984fe8f71627f93e95c39edc Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 7 Jun 2022 20:37:44 +0200 Subject: [PATCH 2703/3533] samd/machine_dac: Add the machine.DAC class. It suuports 1 channel @ 10 bit for SAMD21, 2 channels @ 12 bit for SAMD51. Instantiation by: dac = machine.DAC(ch) # 0 or 1 Method write: dac.write(value) The output voltage range is 0..Vdd. --- ports/samd/Makefile | 2 + ports/samd/machine_dac.c | 162 +++++++++++++++++++++++++++++++++++++++ ports/samd/modmachine.c | 1 + ports/samd/modmachine.h | 1 + ports/samd/pin_af.h | 1 + 5 files changed, 167 insertions(+) create mode 100644 ports/samd/machine_dac.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 71a902a0a535e..43b6a1adc3f23 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -91,6 +91,7 @@ SRC_C = \ clock_config.c \ help.c \ machine_adc.c \ + machine_dac.c \ machine_i2c.c \ machine_led.c \ machine_pin.c \ @@ -149,6 +150,7 @@ endif # List of sources for qstr extraction SRC_QSTR += \ machine_adc.c \ + machine_dac.c \ machine_i2c.c \ machine_led.c \ machine_pin.c \ diff --git a/ports/samd/machine_dac.c b/ports/samd/machine_dac.c new file mode 100644 index 0000000000000..41c6784cceb8b --- /dev/null +++ b/ports/samd/machine_dac.c @@ -0,0 +1,162 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Nick Moore + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "sam.h" +#include "pin_af.h" +#include "modmachine.h" + +typedef struct _dac_obj_t { + mp_obj_base_t base; + uint8_t id; + mp_hal_pin_obj_t gpio_id; +} dac_obj_t; + +STATIC const dac_obj_t dac_obj[] = { + #if defined(MCU_SAMD21) + {{&machine_dac_type}, 0, PIN_PA02}, + #elif defined(MCU_SAMD51) + {{&machine_dac_type}, 0, PIN_PA02}, + {{&machine_dac_type}, 1, PIN_PA05}, + #endif +}; +Dac *const dac_bases[] = DAC_INSTS; + +#if defined(MCU_SAMD21) +#define MAX_DAC_VALUE (1023) +#elif defined(MCU_SAMD51) +#define MAX_DAC_VALUE (4095) +static bool dac_init = false; +#endif + + +STATIC mp_obj_t dac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, + const mp_obj_t *args) { + + mp_arg_check_num(n_args, n_kw, 1, 1, true); + uint8_t id = mp_obj_get_int(args[0]); + const dac_obj_t *self = NULL; + if (0 <= id && id <= MP_ARRAY_SIZE(dac_obj)) { + self = &dac_obj[id]; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("invalid Pin for DAC")); + } + + Dac *dac = dac_bases[0]; // Just one DAC + + // Init DAC + #if defined(MCU_SAMD21) + + // Configuration SAMD21 + // Enable APBC clocks and PCHCTRL clocks; GCLK3 at 1 MHz + PM->APBCMASK.reg |= PM_APBCMASK_DAC; + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_ID_DAC; + while (GCLK->STATUS.bit.SYNCBUSY) { + } + // Reset DAC registers + dac->CTRLA.bit.SWRST = 1; + while (dac->CTRLA.bit.SWRST) { + } + dac->CTRLB.reg = DAC_CTRLB_EOEN | DAC_CTRLB_REFSEL(DAC_CTRLB_REFSEL_AVCC_Val); + // Enable DAC and wait to be ready + dac->CTRLA.bit.ENABLE = 1; + while (dac->STATUS.bit.SYNCBUSY) { + } + + #elif defined(MCU_SAMD51) + + // Configuration SAMD51 + // Enable APBD clocks and PCHCTRL clocks; GCLK3 at 8 MHz + if (!dac_init) { + dac_init = true; + MCLK->APBDMASK.reg |= MCLK_APBDMASK_DAC; + GCLK->PCHCTRL[DAC_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK3 | GCLK_PCHCTRL_CHEN; + + // Reset DAC registers + dac->CTRLA.bit.SWRST = 1; + while (dac->CTRLA.bit.SWRST) { + } + dac->CTRLB.reg = DAC_CTRLB_REFSEL(DAC_CTRLB_REFSEL_VDDANA_Val); + } else { + dac->CTRLA.bit.ENABLE = 0; + while (dac->SYNCBUSY.bit.ENABLE) { + } + } + dac->DACCTRL[self->id].reg = DAC_DACCTRL_ENABLE | DAC_DACCTRL_REFRESH(2) | DAC_DACCTRL_CCTRL_CC12M; + + // Enable DAC and wait to be ready + dac->CTRLA.bit.ENABLE = 1; + while (dac->SYNCBUSY.bit.ENABLE) { + } + #endif + + // Set the port as given in self->gpio_id as DAC + mp_hal_set_pin_mux(self->gpio_id, ALT_FCT_DAC); + + return MP_OBJ_FROM_PTR(self); +} + +STATIC void dac_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + dac_obj_t *self = self_in; + mp_printf(print, "DAC(%u) PIN_PA%02u", self->id, self->gpio_id); +} + +STATIC mp_obj_t dac_write(mp_obj_t self_in, mp_obj_t value_in) { + Dac *dac = dac_bases[0]; // Just one DAC + int value = mp_obj_get_int(value_in); + if (value < 0 || value > MAX_DAC_VALUE) { + mp_raise_ValueError(MP_ERROR_TEXT("value out of range")); + } + #if defined(MCU_SAMD21) + dac->DATA.reg = value; + #elif defined(MCU_SAMD51) + dac_obj_t *self = self_in; + dac->DATA[self->id].reg = value; + #endif + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(dac_write_obj, dac_write); + +STATIC const mp_rom_map_elem_t dac_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&dac_write_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(dac_locals_dict, dac_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_dac_type, + MP_QSTR_DAC, + MP_TYPE_FLAG_NONE, + make_new, dac_make_new, + print, dac_print, + locals_dict, &dac_locals_dict + ); diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index cabd6b9c5782d..f7ad47b3c5291 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -150,6 +150,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 61b26a1fa302b..6a745da0675a5 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -29,6 +29,7 @@ #include "py/obj.h" extern const mp_obj_type_t machine_adc_type; +extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_hw_i2c_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; diff --git a/ports/samd/pin_af.h b/ports/samd/pin_af.h index f9dec6b7da052..b75f4ddd02562 100644 --- a/ports/samd/pin_af.h +++ b/ports/samd/pin_af.h @@ -81,6 +81,7 @@ typedef struct _pwm_config_t { #define ALT_FCT_EIC 0 #define ALT_FCT_ADC 1 +#define ALT_FCT_DAC 1 #define ALT_FCT_SERCOM1 2 #define ALT_FCT_SERCOM2 3 From 029e9af4576f3000aabf5e6eac589f6bb644af92 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 7 Jun 2022 21:52:03 +0200 Subject: [PATCH 2704/3533] samd/modmachine: Add machine.time_pulse_us. Software based. Resolution: - +/-2 microseconds on SAMD51. - +/-4 microseconds on SAMD21. --- ports/samd/modmachine.c | 6 ++++-- ports/samd/mpconfigport.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index f7ad47b3c5291..c191afda7910f 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -26,10 +26,11 @@ #include "py/runtime.h" #include "extmod/machine_mem.h" -#include "samd_soc.h" -#include "modmachine.h" +#include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" #include "extmod/machine_spi.h" +#include "modmachine.h" +#include "samd_soc.h" // ASF 4 #include "hal_flash.h" @@ -165,6 +166,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index c2c614e10b462..48631080d27fa 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -111,6 +111,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_OS_DUPTERM (3) +#define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PWM (1) #define MICROPY_PY_MACHINE_PWM_INIT (0) #define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) From aa6dbbcffd4f0a5c7572b8aa1ef256794838f969 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 8 Jun 2022 10:54:02 +0200 Subject: [PATCH 2705/3533] samd/mcu: Factor out MCU policy for SAMD21 and SAMD51. Which contains a mpconfigmcu.h, mpconfigmcu.mk and manifest.py file for each MCU group. That looks better than the previous choice. --- ports/samd/Makefile | 16 +++++----------- .../samd21/mpconfigmcu.h} | 0 ports/samd/mcu/samd21/mpconfigmcu.mk | 1 + .../samd51/mpconfigmcu.h} | 0 ports/samd/mcu/samd51/mpconfigmcu.mk | 1 + ports/samd/mpconfigport.h | 2 +- 6 files changed, 8 insertions(+), 12 deletions(-) rename ports/samd/{boards/mpconfig_samd21.h => mcu/samd21/mpconfigmcu.h} (100%) create mode 100644 ports/samd/mcu/samd21/mpconfigmcu.mk rename ports/samd/{boards/mpconfig_samd51.h => mcu/samd51/mpconfigmcu.h} (100%) create mode 100644 ports/samd/mcu/samd51/mpconfigmcu.mk diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 43b6a1adc3f23..1455e748cc3af 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -9,14 +9,15 @@ ifeq ($(wildcard $(BOARD_DIR)/.),) $(error Invalid BOARD specified: $(BOARD_DIR)) endif +MCU_SERIES_LOWER = $(shell echo $(MCU_SERIES) | tr '[:upper:]' '[:lower:]') + include ../../py/mkenv.mk include $(BOARD_DIR)/mpconfigboard.mk +include mcu/$(MCU_SERIES_LOWER)/mpconfigmcu.mk # Qstr definitions (must come before including py.mk) QSTR_DEFS = qstrdefsport.h -QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h - -MCU_SERIES_LOWER = $(shell echo $(MCU_SERIES) | tr '[:upper:]' '[:lower:]') +QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h mcu/$(MCU_SERIES_LOWER)/mpconfigmcu.h FROZEN_MANIFEST ?= boards/manifest.py @@ -30,6 +31,7 @@ INC += -I. INC += -I$(TOP) INC += -I$(BUILD) INC += -I$(BOARD_DIR) +INC += -Imcu/$(MCU_SERIES_LOWER) INC += -I$(TOP)/lib/cmsis/inc INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hal/include INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hal/utils/include @@ -62,8 +64,6 @@ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -DMPCONFIG_MCU_H='' CFLAGS += -DPIN_AF_TABLE_C='<$(BUILD)/$(GEN_PIN_AF)>' -QSTR_GLOBAL_DEPENDENCIES += boards/mpconfig_$(MCU_SERIES_LOWER).h - LDFLAGS = -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref LDFLAGS += $(LDFLAGS_MOD) @@ -141,12 +141,6 @@ SRC_C += $(SRC_MOD) SRC_CXX += $(SRC_MOD_CXX) -ifeq ($(MCU_SERIES),SAMD21) -SRC_S = shared/runtime/gchelper_m0.s -else -SRC_S = shared/runtime/gchelper_m3.s -endif - # List of sources for qstr extraction SRC_QSTR += \ machine_adc.c \ diff --git a/ports/samd/boards/mpconfig_samd21.h b/ports/samd/mcu/samd21/mpconfigmcu.h similarity index 100% rename from ports/samd/boards/mpconfig_samd21.h rename to ports/samd/mcu/samd21/mpconfigmcu.h diff --git a/ports/samd/mcu/samd21/mpconfigmcu.mk b/ports/samd/mcu/samd21/mpconfigmcu.mk new file mode 100644 index 0000000000000..cc435da8cc913 --- /dev/null +++ b/ports/samd/mcu/samd21/mpconfigmcu.mk @@ -0,0 +1 @@ +SRC_S += shared/runtime/gchelper_m0.s diff --git a/ports/samd/boards/mpconfig_samd51.h b/ports/samd/mcu/samd51/mpconfigmcu.h similarity index 100% rename from ports/samd/boards/mpconfig_samd51.h rename to ports/samd/mcu/samd51/mpconfigmcu.h diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk new file mode 100644 index 0000000000000..461a0182ef019 --- /dev/null +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -0,0 +1 @@ +SRC_S += shared/runtime/gchelper_m3.s diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 48631080d27fa..8a067d3482393 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -29,7 +29,7 @@ // Board specific definitions #include "mpconfigboard.h" // MCU-Specific definitions -#include MPCONFIG_MCU_H +#include "mpconfigmcu.h" // Memory allocation policies #define MICROPY_GC_STACK_ENTRY_TYPE uint16_t From 7da76639027057245cfee043733193b3265e7a54 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 9 Jun 2022 13:44:22 +0200 Subject: [PATCH 2706/3533] samd/mphalport: Add a mp_hal_ticks_ms_64() function. Returning a 64 bit number. This will be used by the utime module and the machine.UART module for timeout avoiding overflow. --- ports/samd/mphalport.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 2eb4ce7ca680e..c2aca8b0e273f 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -50,6 +50,10 @@ static inline mp_uint_t mp_hal_ticks_ms(void) { return systick_ms; } +static inline uint64_t mp_hal_ticks_ms_64(void) { + return ((uint64_t)systick_ms_upper << 32) + systick_ms; +} + static inline mp_uint_t mp_hal_ticks_us(void) { #if defined(MCU_SAMD21) @@ -74,7 +78,7 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) { } static inline uint64_t mp_hal_time_ns(void) { - return ((uint64_t)systick_ms + (uint64_t)systick_ms_upper * 0x100000000) * 1000000; + return mp_hal_ticks_ms_64() * 1000000; } // C-level pin HAL From 37449df821a47698779b48e6c101b084cfbdb6c7 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 9 Jun 2022 14:33:31 +0200 Subject: [PATCH 2707/3533] samd/modutime: Enable time.time() based on systick_ms(). Allowing to set a time and retrieve the time. It is based on systick_ms() with the precision of the MCU clock. Unless that is based on a crystal, the error seen was about 0.5% at room temperature. --- ports/samd/modutime.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ports/samd/modutime.c b/ports/samd/modutime.c index bbeedcfda070f..a54544e62c6ca 100644 --- a/ports/samd/modutime.c +++ b/ports/samd/modutime.c @@ -27,6 +27,9 @@ #include "py/runtime.h" #include "extmod/utime_mphal.h" #include "shared/timeutils/timeutils.h" +#include "mphalport.h" + +static uint32_t time_offset = 0; // localtime([secs]) STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { @@ -34,9 +37,10 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { mp_int_t seconds; if (n_args == 0 || args[0] == mp_const_none) { // seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000; - seconds = mp_obj_get_int(args[0]); + seconds = mp_hal_ticks_ms_64() / 1000 + time_offset; } else { seconds = mp_obj_get_int(args[0]); + time_offset = seconds - mp_hal_ticks_ms_64() / 1000; } timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); mp_obj_t tuple[8] = { @@ -72,7 +76,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); // time() STATIC mp_obj_t time_time(void) { - mp_raise_NotImplementedError("time"); + return mp_obj_new_int_from_uint(mp_hal_ticks_ms_64() / 1000 + time_offset); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); From 9a567b04e7c43024a2a4dce2618b24f822dfe5bc Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 9 Jun 2022 13:46:54 +0200 Subject: [PATCH 2708/3533] samd/machine_uart: Support buffered TX for UART. It can be enabled/disabled by a configuration switch. The code size increase is 308 bytes, but it requires RAM space for buffers, the larger UART object and root pointers. --- ports/samd/machine_uart.c | 78 +++++++++++++++++++++++------ ports/samd/mcu/samd21/mpconfigmcu.h | 2 + ports/samd/mcu/samd51/mpconfigmcu.h | 2 + 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 0ed7fb95d0c3f..92e63ee51e6f2 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -37,7 +37,6 @@ #define DEFAULT_BUFFER_SIZE (256) #define MIN_BUFFER_SIZE (32) #define MAX_BUFFER_SIZE (32766) -#define USART_BUFFER_TX (0) typedef struct _machine_uart_obj_t { mp_obj_base_t base; @@ -54,7 +53,7 @@ typedef struct _machine_uart_obj_t { uint16_t timeout_char; // timeout waiting between chars (in ms) bool new; ringbuf_t read_buffer; - #if USART_BUFFER_TX + #if MICROPY_HW_UART_TXBUF ringbuf_t write_buffer; #endif } machine_uart_obj_t; @@ -91,7 +90,15 @@ void common_uart_irq_handler(int uart_id) { // Now handler the incoming data uart_drain_rx_fifo(self, uart); } else if (uart->USART.INTFLAG.bit.DRE != 0) { + #if MICROPY_HW_UART_TXBUF // handle the outgoing data + if (ringbuf_avail(&self->write_buffer) > 0) { + uart->USART.DATA.bit.DATA = ringbuf_get(&self->write_buffer); + } else { + // Stop the interrupt if there is no more data + uart->USART.INTENCLR.bit.DRE = 1; + } + #endif } else { // Disable the other interrupts, if set by error uart->USART.INTENCLR.reg = (uint8_t) ~(SERCOM_USART_INTENCLR_DRE | SERCOM_USART_INTENCLR_RXC); @@ -127,7 +134,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, - #if USART_BUFFER_TX + #if MICROPY_HW_UART_TXBUF { MP_QSTR_txbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, #endif }; @@ -191,7 +198,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args } } - #if USART_BUFFER_TX + #if MICROPY_HW_UART_TXBUF // Set the TX buffer size if configured. size_t txbuf_len = DEFAULT_BUFFER_SIZE; if (args[ARG_txbuf].u_int > 0) { @@ -224,7 +231,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args ringbuf_alloc(&(self->read_buffer), rxbuf_len + 1); MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = self->read_buffer.buf; - #if USART_BUFFER_TX + #if MICROPY_HW_UART_TXBUF ringbuf_alloc(&(self->write_buffer), txbuf_len + 1); MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = self->write_buffer.buf; #endif @@ -274,6 +281,8 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args uint32_t baud = 65536 - ((uint64_t)(65536 * 16) * self->baudrate + get_apb_freq() / 2) / get_apb_freq(); uart->USART.BAUD.bit.BAUD = baud; // Set Baud + sercom_register_irq(self->id, &common_uart_irq_handler); + // Enable RXC interrupt uart->USART.INTENSET.bit.RXC = 1; #if defined(MCU_SAMD21) @@ -281,7 +290,13 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args #elif defined(MCU_SAMD51) NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 2); #endif - sercom_register_irq(self->id, &common_uart_irq_handler); + #if MICROPY_HW_UART_TXBUF + // Enable DRE interrupt + // SAMD21 has just 1 IRQ for all USART events, so no need for an additional NVIC enable + #if defined(MCU_SAMD51) + NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 0); + #endif + #endif sercom_enable(uart, 1); } @@ -330,7 +345,7 @@ STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) { // Disable interrupts uart->USART.INTENCLR.reg = 0xff; MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = NULL; - #if USART_BUFFER_TX + #if MICROPY_HW_UART_TXBUF MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = NULL; #endif return mp_const_none; @@ -339,7 +354,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_deinit_obj, machine_uart_deinit); STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - // get all bytes from the fifo first + // get all bytes from the fifo first. May be obsolete. uart_drain_rx_fifo(self, sercom_instance[self->id]); return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->read_buffer)); } @@ -349,9 +364,14 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); uint32_t break_time_us = 13 * 1000000 / self->baudrate; + // Wait for the tx buffer to drain. + #if MICROPY_HW_UART_TXBUF + while (ringbuf_avail(&self->write_buffer) > 0) { + MICROPY_EVENT_POLL_HOOK + } + #endif // Wait for the TX queue & register to clear // Since the flags are not safe, just wait sufficiently long. - // Once tx buffering is implemented, wait as well for the buffer to clear. mp_hal_delay_us(2 * break_time_us); // Disable MUX PORT->Group[self->tx / 32].PINCFG[self->tx % 32].bit.PMUXEN = 0; @@ -389,7 +409,7 @@ STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_t STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint64_t t = mp_hal_ticks_ms() + self->timeout; + uint64_t t = mp_hal_ticks_ms_64() + self->timeout; uint64_t timeout_char = self->timeout_char; uint8_t *dest = buf_in; Sercom *uart = sercom_instance[self->id]; @@ -403,7 +423,7 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz uart_drain_rx_fifo(self, uart); break; } - if (mp_hal_ticks_ms() > t) { // timed out + if (mp_hal_ticks_ms_64() > t) { // timed out if (i <= 0) { *errcode = MP_EAGAIN; return MP_STREAM_ERROR; @@ -421,17 +441,40 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - size_t remaining = size; + size_t i = 0; const uint8_t *src = buf_in; Sercom *uart = sercom_instance[self->id]; - while (remaining--) { - while (!(uart->USART.INTFLAG.bit.DRE)) { + #if MICROPY_HW_UART_TXBUF + uint64_t t = mp_hal_ticks_ms_64() + self->timeout; + + while (i < size) { + // Wait for the first/next character to be sent. + while (ringbuf_free(&(self->write_buffer)) == 0) { + if (mp_hal_ticks_ms_64() > t) { // timed out + if (i <= 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } else { + return i; + } + } + MICROPY_EVENT_POLL_HOOK } - uart->USART.DATA.bit.DATA = *src; - src += 1; + ringbuf_put(&(self->write_buffer), *src++); + i++; + uart->USART.INTENSET.bit.DRE = 1; // kick off the IRQ } + #else + + while (i < size) { + while (!(uart->USART.INTFLAG.bit.DRE)) { + } + uart->USART.DATA.bit.DATA = *src++; + i++; + } + #endif return size; } @@ -473,3 +516,6 @@ MP_DEFINE_CONST_OBJ_TYPE( ); MP_REGISTER_ROOT_POINTER(void *samd_uart_rx_buffer[SERCOM_INST_NUM]); +#if MICROPY_HW_UART_TXBUF +MP_REGISTER_ROOT_POINTER(void *samd_uart_tx_buffer[SERCOM_INST_NUM]); +#endif diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index 587f3f4b3ad68..7b91be1430a0d 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -5,6 +5,8 @@ #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; +#define MICROPY_HW_UART_TXBUF (1) + #define CPU_FREQ (48000000) #define APB_FREQ (48000000) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 95fc635e5e32d..089ca48de88bd 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -19,6 +19,8 @@ unsigned long trng_random_u32(void); #define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // +#define MICROPY_HW_UART_TXBUF (1) + #define CPU_FREQ (120000000) #define APB_FREQ (48000000) #define DPLLx_REF_FREQ (32768) From a9304af8fa7165c41be1cad3d55042a5cc14d13c Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 6 Jun 2022 21:06:09 +0200 Subject: [PATCH 2709/3533] samd/boards: Add missing/lost board config and pin definitions. Fixes are: - Pin definitions for ADAFRUIT_FEATHER_Mx_EXPRESS and ADAFRUIT_ITSYBITSY_M4_EXPRESS. - For ADAFRUIT_ITSYBITSY_M0_EXPRESS, change the MISO/MOSI name. - For MINISAM_M4, add the default SPI pins. - For boards with 32k crystal, add the XOSC32K setting. --- .../boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h | 2 ++ ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv | 3 +++ .../boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h | 2 ++ ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv | 9 +++++++++ .../samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv | 6 +++--- .../samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv | 10 ++++++++-- ports/samd/boards/MINISAM_M4/pins.csv | 3 +++ ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h | 2 ++ ports/samd/boards/SEEED_XIAO/mpconfigboard.h | 2 ++ 9 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h index cec9e9ccdda46..815597899c4ae 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "Feather M0 Express" #define MICROPY_HW_MCU_NAME "SAMD21G18A" + +#define MICROPY_HW_XOSC32K (1) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv index 84e68157acbc3..0985ee1646d1f 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv @@ -29,6 +29,9 @@ PIN_PB22,TX PIN_PB23,RX PIN_PA23,SCL PIN_PA22,SDA +PIN_PB10,MOSI +PIN_PA12,MISO +PIN_PB11,SCK PIN_PA06,NEOPIXEL PIN_PA13,FLASH_CS diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h index 48599eec47297..35945395fcc0d 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "Feather M4 Express" #define MICROPY_HW_MCU_NAME "SAMD51J19A" + +#define MICROPY_HW_XOSC32K (1) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv index 5b999c39e0224..32daac3d0f4c7 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv @@ -7,6 +7,8 @@ PIN_PB17,D0 PIN_PB16,D1 +- +- PIN_PA14,D4 PIN_PA16,D5 PIN_PA18,D6 @@ -30,5 +32,12 @@ PIN_PB22,MISO PIN_PA17,SCK PIN_PB01,VDIV PIN_PA03,AREF +PIN_PB03,NEOPIXEL +PIN_PB11,FLASH_CS +PIN_PB10,FLASH_SCK +PIN_PA08,FLASH_MOSI +PIN_PA09,FLASH_MISO +PIN_PA10,FLASH_WP +PIN_PA11,FLASH_HOLD LED_PA17,LED diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv index 50c3c1cf68f51..d7d59c23575da 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv @@ -8,7 +8,7 @@ PIN_PA11,D0 PIN_PA10,D1 PIN_PA14,D2 -PIN_PB09,D3 +PIN_PA09,D3 PIN_PA08,D4 PIN_PA15,D5 - @@ -27,8 +27,8 @@ PIN_PA05,A4 PIN_PB02,A5 PIN_PA22,SDA PIN_PA23,SCL -PIN_PB10,MO -PIN_PA12,MI +PIN_PB10,MOSI +PIN_PA12,MISO PIN_PB11,SCK PIN_PA00,DOTSTAR_CLK PIN_PA01,DOTSTAR_DATA diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv index 90e38761a10ce..ce760a269c243 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv @@ -27,10 +27,16 @@ PIN_PA04,A4 PIN_PA06,A5 PIN_PA12,SDA PIN_PA13,SCL -PIN_PA00,MO -PIN_PB23,MI +PIN_PA00,MOSI +PIN_PB23,MISO PIN_PA01,SCK PIN_PB02,DOTSTAR_CLK PIN_PB03,DOTSTAR_DATA +PIN_PB11,FLASH_CS +PIN_PB10,FLASH_SCK +PIN_PA08,FLASH_MOSI +PIN_PA09,FLASH_MISO +PIN_PA10,FLASH_WP +PIN_PA11,FLASH_HOLD LED_PA22,LED diff --git a/ports/samd/boards/MINISAM_M4/pins.csv b/ports/samd/boards/MINISAM_M4/pins.csv index 7442a028d12a5..cf9a3bd19b214 100644 --- a/ports/samd/boards/MINISAM_M4/pins.csv +++ b/ports/samd/boards/MINISAM_M4/pins.csv @@ -21,6 +21,9 @@ PIN_PA00,BUTTON PIN_PA03,AREF PIN_PA12,SDA PIN_PA13,SCL +PIN_PB22,MOSI +PIN_PB23,MISO +PIN_PA01,SCK PIN_PB03,DOTSTAR_DATA PIN_PB02,DOTSTAR_CLK diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h index c69b5b4c149de..064d1ecc0d030 100644 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "SAMD21-XPLAINED-PRO" #define MICROPY_HW_MCU_NAME "SAMD21J18A" + +#define MICROPY_HW_XOSC32K (1) diff --git a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h index 2026efc6b153b..98994a5bc06bd 100644 --- a/ports/samd/boards/SEEED_XIAO/mpconfigboard.h +++ b/ports/samd/boards/SEEED_XIAO/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "Seeed Xiao" #define MICROPY_HW_MCU_NAME "SAMD21G18A" + +#define MICROPY_HW_XOSC32K (1) From fd7b57dd22ce0105e04c5f9b2d12ca3a3075ca0a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 10 Jun 2022 08:23:09 +0200 Subject: [PATCH 2710/3533] samd/mphalport: Use CYCCNT for SAMD51's mp_hal_ticks_cpu(). And use mp_hal_ticks_us() for SAM21's mp_hal_ticks_cpu(). The SAMD21 has no CYCCNT register, and the SysTick register has only a 1 ms span (== 48000 count range). --- ports/samd/mphalport.h | 16 +++++++++++++--- ports/samd/samd_soc.c | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index c2aca8b0e273f..b7e65da35dc05 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -71,11 +71,21 @@ static inline mp_uint_t mp_hal_ticks_us(void) { #endif } -// ticks_cpu is limited to a 1 ms period, since the CPU SysTick counter -// is used for the 1 ms SysTick_Handler interrupt. +#if defined (MCU_SAMD21) + +#define mp_hal_ticks_cpu mp_hal_ticks_us + +#elif defined (MCU_SAMD51) +static inline void mp_hal_ticks_cpu_enable(void) { + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CYCCNT = 0; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; +} + static inline mp_uint_t mp_hal_ticks_cpu(void) { - return (system_time_t)SysTick->VAL; + return (system_time_t)DWT->CYCCNT; } +#endif static inline uint64_t mp_hal_time_ns(void) { return mp_hal_ticks_ms_64() * 1000000; diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 8d6e808f63904..aca6df0dd88e6 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -36,6 +36,7 @@ #include "samd_soc.h" #include "sam.h" #include "tusb.h" +#include "mphalport.h" static void usb_init(void) { // Init USB clock @@ -110,4 +111,7 @@ void samd_init(void) { SysTick_Config(get_cpu_freq() / 1000); init_us_counter(); usb_init(); + #if defined (MCU_SAMD51) + mp_hal_ticks_cpu_enable(); + #endif } From a4157521731878c7363ff59f15d68eb68b486cb5 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 10 Jun 2022 16:40:50 +0200 Subject: [PATCH 2711/3533] samd/machine_bitstream: Add the machine.bitstream() function. The SAMD21 implementation is an adaption of @jimmo's code for STM32Lxx. The only changes are the addresses and names of the port registers and the timing parameters. SAMD21: The precision is about +/-25ns at 48MHz clock frequency. The first two cycles are about 40-60 ns longer than set. But still good enough to drive a neopixel device. SAMD51: The precision is about +/-30ns at 120MHz clock frequency. Good enough to drive a neopixel device. --- ports/samd/Makefile | 3 +- ports/samd/machine_bitstream.c | 206 +++++++++++++++++++++++++++++++++ ports/samd/modmachine.c | 2 + ports/samd/mpconfigport.h | 1 + ports/samd/mphalport.h | 4 +- ports/samd/samd_soc.c | 2 +- 6 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 ports/samd/machine_bitstream.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 1455e748cc3af..f1bc67e6ee4d6 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -87,10 +87,11 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif -SRC_C = \ +SRC_C += \ clock_config.c \ help.c \ machine_adc.c \ + machine_bitstream.c \ machine_dac.c \ machine_i2c.c \ machine_led.c \ diff --git a/ports/samd/machine_bitstream.c b/ports/samd/machine_bitstream.c new file mode 100644 index 0000000000000..9959d947cb449 --- /dev/null +++ b/ports/samd/machine_bitstream.c @@ -0,0 +1,206 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c. + +#include "py/mpconfig.h" +#include "py/mphal.h" +#include "clock_config.h" + +#if MICROPY_PY_MACHINE_BITSTREAM + +#if __CORTEX_M == 0 + +// No cycle counter on M0, do manual cycle counting instead. + +// STM32F091 @ 48MHz +#define NS_CYCLES_PER_ITER_HIGH (3) +#define NS_CYCLES_PER_ITER_LOW (3) +#define NS_OVERHEAD_CYCLES_HIGH (12) +#define NS_OVERHEAD_CYCLES_LOW (15) + +uint32_t mp_hal_delay_ns_calc(uint32_t ns, bool high) { + uint32_t ncycles = (get_cpu_freq() / 1000000 * ns + 500) / 1000; // + 500 for proper rounding + uint32_t overhead = MIN(ncycles, high ? NS_OVERHEAD_CYCLES_HIGH : NS_OVERHEAD_CYCLES_LOW); + return MAX(1, MP_ROUND_DIVIDE(ncycles - overhead, high ? NS_CYCLES_PER_ITER_HIGH : NS_CYCLES_PER_ITER_LOW)); +} + +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { + volatile const uint32_t mask = 1 << (pin % 32); + volatile uint32_t *outclr = &PORT->Group[pin / 32].OUTCLR.reg; + volatile uint32_t *outset = &PORT->Group[pin / 32].OUTSET.reg; + + // Convert ns to loop iterations [high_time_0, low_time_0, high_time_1, low_time_1]. + for (size_t i = 0; i < 4; ++i) { + timing_ns[i] = mp_hal_delay_ns_calc(timing_ns[i], i % 2 == 0); + } + + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); + + // Measured timing for SAMD21 at 48MHz (cycle=20.83ns) + // timing_ns = (1,1,1,1) + // high: 310 + // low: 375 + // high0: 375 + // low0: 400 + // timing_ns = (500, 500, 500, 500) + // high: 500 + // low: 500 + // high0: 565 + // low0: 540 + // timing_ns = (1000, 1000, 1000, 1000) + // high: 1000 + // low: 1000 + // high0: 1065 + // low0: 1040 + + // --> high is 12 + n*3 cycles + // low is 15 + n*3 cycles + + // NeoPixel timing (400, 850, 800, 450) (+/-150ns) gives timing_ns=(2, 9, 8, 3) which in cycles is + // (12 + 6, 15 + 27, 15 + 24, 12 + 9) = (18, 42, 39, 21) + // --> (375, 875, 812, 437) nanoseconds. + // Measured output on logic analyser is (375, 875, 815, 435) (+/-5ns at 200MHz) + + // Note: the first high/low cycle is longer by 2-3 cycles (40-60ns). + // This is slightly outside spec, but doesn't seem to cause a problem. + + __asm volatile ( + // Force consistent register assignment. + // r6 = len + "ldr r6, %0\n" + // r4 = buf + "ldr r4, %1\n" + // r5 = timing_ms + "ldr r5, %2\n" + + // Must align for consistent timing. + ".align 4\n" + + // Don't increment/decrement before first iteration. + "b .outer2\n" + ".outer:\n" + // ++buf, --len + " add r4, #1\n" + " sub r6, #1\n" + + // len iterations + ".outer2:\n" + " cmp r6, #0\n" + " beq .done\n" + + // r0 = *buf + " ldrb r0, [r4, #0]\n" + + // 8 bits in byte + " mov r7, #8\n" + " .inner:\n" + // *outset = mask + " ldr r2, %3\n" + " ldr r1, %5\n" + " str r1, [r2, #0]\n" + + // r3 = (r0 >> 4) & 8 (r0 is 8 if high bit is 1 else 0) + " mov r8, r6\n" + " lsr r3, r0, #4\n" + " mov r6, #8\n" + " and r3, r6\n" + " mov r6, r8\n" + + // r2 = timing_ns[r2] + " ldr r2, [r5, r3]\n" + " .loop1:\n sub r2, #1\n bne .loop1\n" + + // *outclr = mask + " ldr r2, %4\n" + " str r1, [r2, #0]\n" + + // r2 = timing_ns[r3 + 4] + " add r3, #4\n" + " ldr r2, [r5, r3]\n" + " .loop2:\n sub r2, #1\n bne .loop2\n" + + // b >>= 1 + " lsl r0, r0, #1\n" + + " sub r7, #1\n" + // end of inner loop + " beq .outer\n" + // continue inner loop + " b .inner\n" + + ".done:\n" + : + : "m" (len), "m" (buf), "m" (timing_ns), "m" (outset), "m" (outclr), "m" (mask) + : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8" + ); + + MICROPY_END_ATOMIC_SECTION(atomic_state); +} + +#else // > CORTEX_M0 + +#define NS_TICKS_OVERHEAD (70) + +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { + uint32_t fcpu_mhz = get_cpu_freq() / 1000000; + uint32_t ticks_overhead = fcpu_mhz * NS_TICKS_OVERHEAD / 1000; + // Convert ns to us ticks [high_time_0, period_0, high_time_1, period_1]. + for (size_t i = 0; i < 4; ++i) { + timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; + if (timing_ns[i] > ticks_overhead) { + timing_ns[i] -= ticks_overhead; + } + if (i % 2 == 1) { + // Convert low_time to period (i.e. add high_time). + timing_ns[i] += timing_ns[i - 1] - ticks_overhead; + } + } + + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); + DWT->CYCCNT = 0; + + for (size_t i = 0; i < len; ++i) { + uint8_t b = buf[i]; + for (size_t j = 0; j < 8; ++j) { + uint32_t start_ticks = mp_hal_ticks_cpu(); + uint32_t *t = &timing_ns[b >> 6 & 2]; + mp_hal_pin_high(pin); + while ((mp_hal_ticks_cpu() - start_ticks) < t[0]) { + } + b <<= 1; + mp_hal_pin_low(pin); + while ((mp_hal_ticks_cpu() - start_ticks) < t[1]) { + } + } + } + MICROPY_END_ATOMIC_SECTION(atomic_state); + +} + +#endif // > CORTEX_M0 + +#endif // MICROPY_PY_MACHINE_BITSTREAM diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index c191afda7910f..f6cf7f8155c96 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -25,6 +25,7 @@ */ #include "py/runtime.h" +#include "extmod/machine_bitstream.h" #include "extmod/machine_mem.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" @@ -167,6 +168,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 8a067d3482393..986c42fc7f84d 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -111,6 +111,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_OS_DUPTERM (3) +#define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PWM (1) #define MICROPY_PY_MACHINE_PWM_INIT (0) diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index b7e65da35dc05..f1efd550035af 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -71,11 +71,11 @@ static inline mp_uint_t mp_hal_ticks_us(void) { #endif } -#if defined (MCU_SAMD21) +#if defined(MCU_SAMD21) #define mp_hal_ticks_cpu mp_hal_ticks_us -#elif defined (MCU_SAMD51) +#elif defined(MCU_SAMD51) static inline void mp_hal_ticks_cpu_enable(void) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; DWT->CYCCNT = 0; diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index aca6df0dd88e6..d0df4a7e28cc1 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -111,7 +111,7 @@ void samd_init(void) { SysTick_Config(get_cpu_freq() / 1000); init_us_counter(); usb_init(); - #if defined (MCU_SAMD51) + #if defined(MCU_SAMD51) mp_hal_ticks_cpu_enable(); #endif } From 929dfc66a30c361d2a0bd5b64dec021a49c7b471 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 15 Jun 2022 15:16:07 +0200 Subject: [PATCH 2712/3533] samd/mpconfigport: Restructure to use ROM feature levels. Changes are: - Set the feature level for each MCU: CORE features for SAMD21, and EXTRA features for SAMD51. - Remove all definitions that are included in the core feature level. - Keep the default settings for feature level and float, to make the choice obvious. --- ports/samd/Makefile | 1 + ports/samd/mcu/samd21/mpconfigmcu.h | 22 +++++++++++++++------- ports/samd/mcu/samd51/mpconfigmcu.h | 25 ++++++++++++++++--------- ports/samd/mpconfigport.h | 23 +---------------------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index f1bc67e6ee4d6..5b268286c3a23 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -161,6 +161,7 @@ SRC_QSTR += \ shared/readline/readline.c \ extmod/uos_dupterm.c \ shared/runtime/mpirq.c \ + shared/runtime/sys_stdio_mphal.c \ SRC_QSTR += $(SRC_MOD) $(SRC_CXX) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index 7b91be1430a0d..dd16a6c8affd4 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -1,16 +1,24 @@ // Deinitions common to all SAMD21 boards #include "samd21.h" -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES) -#define MICROPY_HW_UART_TXBUF (1) +// MicroPython emitters +#define MICROPY_EMIT_THUMB (0) +#define MICROPY_EMIT_INLINE_THUMB (0) -#define CPU_FREQ (48000000) -#define APB_FREQ (48000000) +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) -#define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1) +#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) +#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) +#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; + +#define MICROPY_HW_UART_TXBUF (1) + +#define CPU_FREQ (48000000) +#define APB_FREQ (48000000) + +#define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1) static inline uint32_t raise_irq_pri(uint32_t pri) { (void)pri; diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 089ca48de88bd..c3253fcbecb73 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -1,10 +1,17 @@ // Deinitions common to all SAMD51 boards #include "samd51.h" +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_FULL_FEATURES) + +// MicroPython emitters +#define MICROPY_EMIT_THUMB (1) +#define MICROPY_EMIT_INLINE_THUMB (1) + #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_BUILTINS_COMPLEX (0) #define MICROPY_PY_MATH (0) #define MICROPY_PY_CMATH (0) + #define MICROPY_PY_UOS_URANDOM (1) #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); @@ -15,18 +22,18 @@ unsigned long trng_random_u32(void); // samd_flash.c flash parameters // Build a 128k Flash storage at top. 512k-128k=384k=0x60000 // 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) -#define VFS_BLOCK_SIZE_BYTES (1536) // +#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) +#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) +#define VFS_BLOCK_SIZE_BYTES (1536) // -#define MICROPY_HW_UART_TXBUF (1) +#define MICROPY_HW_UART_TXBUF (1) -#define CPU_FREQ (120000000) -#define APB_FREQ (48000000) -#define DPLLx_REF_FREQ (32768) +#define CPU_FREQ (120000000) +#define APB_FREQ (48000000) +#define DPLLx_REF_FREQ (32768) -#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) -#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 7, 0) +#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) +#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 7, 0) static inline uint32_t raise_irq_pri(uint32_t pri) { uint32_t basepri = __get_BASEPRI(); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 986c42fc7f84d..8b8305f0b47e7 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -40,8 +40,6 @@ // MicroPython emitters #define MICROPY_PERSISTENT_CODE_LOAD (1) -#define MICROPY_EMIT_THUMB (0) -#define MICROPY_EMIT_INLINE_THUMB (0) // Compiler configuration #define MICROPY_COMP_CONST (1) @@ -56,33 +54,18 @@ #define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) -#define MICROPY_CPYTHON_COMPAT (0) -#define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT samd_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (1) #define MICROPY_ENABLE_SCHEDULER (1) -// fixes sys/usys import issue #define MICROPY_MODULE_WEAK_LINKS (1) + // Control over Python builtins -#define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_BUILTINS_BYTES_HEX (1) -#define MICROPY_PY_BUILTINS_STR_COUNT (0) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) -#define MICROPY_PY_BUILTINS_SET (0) -#define MICROPY_PY_BUILTINS_FROZENSET (0) -#define MICROPY_PY_BUILTINS_PROPERTY (0) -#define MICROPY_PY_BUILTINS_ENUMERATE (1) -#define MICROPY_PY_BUILTINS_FILTER (0) -#define MICROPY_PY_BUILTINS_REVERSED (0) -#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) -#define MICROPY_PY_BUILTINS_MIN_MAX (1) #define MICROPY_PY_BUILTINS_INPUT (1) -#define MICROPY_PY___FILE__ (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) -#define MICROPY_PY_ATTRTUPLE (1) -#define MICROPY_PY_COLLECTIONS (0) #define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_PLATFORM "samd" #define MICROPY_PY_SYS_EXIT (1) @@ -118,10 +101,6 @@ #define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) #define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/samd/machine_pwm.c" -// Use VfsLfs's types for fileio/textio -#define mp_type_fileio mp_type_vfs_lfs1_fileio -#define mp_type_textio mp_type_vfs_lfs1_textio - #define MP_STATE_PORT MP_STATE_VM // Miscellaneous settings From f00356a486ed7ea9650f59f1259b2732d9a1bb21 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 15 Jun 2022 18:49:24 +0200 Subject: [PATCH 2713/3533] samd/clock_config: Split clock_config.c to separate SAMD21/SAMD51 files. And put the file into the mcu directory. The file got a little bit long and hard to read. --- ports/samd/Makefile | 2 +- ports/samd/mcu/samd21/clock_config.c | 168 +++++++++++++++++++++ ports/samd/{ => mcu/samd51}/clock_config.c | 131 ---------------- 3 files changed, 169 insertions(+), 132 deletions(-) create mode 100644 ports/samd/mcu/samd21/clock_config.c rename ports/samd/{ => mcu/samd51}/clock_config.c (59%) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 5b268286c3a23..8d49778b3bcc8 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -88,7 +88,7 @@ LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif SRC_C += \ - clock_config.c \ + mcu/$(MCU_SERIES_LOWER)/clock_config.c \ help.c \ machine_adc.c \ machine_bitstream.c \ diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c new file mode 100644 index 0000000000000..3d189d67e18ca --- /dev/null +++ b/ports/samd/mcu/samd21/clock_config.c @@ -0,0 +1,168 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * This file provides functions for configuring the clocks. + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/runtime.h" +#include "samd_soc.h" + +static uint32_t cpu_freq = CPU_FREQ; +static uint32_t apb_freq = APB_FREQ; + +int sercom_gclk_id[] = { + GCLK_CLKCTRL_ID_SERCOM0_CORE, GCLK_CLKCTRL_ID_SERCOM1_CORE, + GCLK_CLKCTRL_ID_SERCOM2_CORE, GCLK_CLKCTRL_ID_SERCOM3_CORE, + GCLK_CLKCTRL_ID_SERCOM4_CORE, GCLK_CLKCTRL_ID_SERCOM5_CORE +}; + +uint32_t get_cpu_freq(void) { + return cpu_freq; +} + +uint32_t get_apb_freq(void) { + return apb_freq; +} + +void set_cpu_freq(uint32_t cpu_freq_arg) { + cpu_freq = cpu_freq_arg; +} + + +void init_clocks(uint32_t cpu_freq) { + + // SAMD21 Clock settings + // GCLK0: 48MHz from DFLL open loop mode or closed loop mode from 32k Crystal + // GCLK1: 32768 Hz from 32K ULP or 32k Crystal + // GCLK2: 48MHz from DFLL for Peripherals + // GCLK3: 1Mhz for the us-counter (TC4/TC5) + // GCLK8: 1kHz clock for WDT + + NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" + NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz + + #if MICROPY_HW_XOSC32K + // Set up OSC32K according datasheet 17.6.3 + SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x3) | SYSCTRL_XOSC32K_EN32K | + SYSCTRL_XOSC32K_XTALEN; + SYSCTRL->XOSC32K.bit.ENABLE = 1; + while (SYSCTRL->PCLKSR.bit.XOSC32KRDY == 0) { + } + // Set up the DFLL48 according to the data sheet 17.6.7.1.2 + // Step 1: Set up the reference clock + // Connect the OSC32K via GCLK1 to the DFLL input and for further use. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(1); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_CLKEN; + // Enable access to the DFLLCTRL reg acc. to Errata 1.2.1 + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 2: Set the coarse and fine values. + // The coarse setting will be taken from the calibration data. So the value used here + // does not matter. Get the coarse value from the calib data. In case it is not set, + // set a midrange value. + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) + >> FUSES_DFLL48M_COARSE_CAL_Pos; + if (coarse == 0x3f) { + coarse = 0x1f; + } + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 3: Set the multiplication values. The offset of 16384 to the freq is for rounding. + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL((CPU_FREQ + 16384) / 32768) | + SYSCTRL_DFLLMUL_FSTEP(1) | SYSCTRL_DFLLMUL_CSTEP(1); + while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { + } + // Step 4: Start the DFLL and wait for the PLL lock. We just wait for the fine lock, since + // coarse adjusting is bypassed. + SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | + SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE; + while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) { + } + + #else // MICROPY_HW_XOSC32K + + // Enable DFLL48M + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { + } + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) + | SYSCTRL_DFLLMUL_MUL(48000); + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) + >> FUSES_DFLL48M_COARSE_CAL_Pos; + if (coarse == 0x3f) { + coarse = 0x1f; + } + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM + | SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE; + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { + } + // Enable 32768 Hz on GCLK1 for consistency + GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(48016384 / 32768); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(1); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + #endif // MICROPY_HW_XOSC32K + + // Enable GCLK output: 48M on both CCLK0 and GCLK2 + GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + // Enable GCLK output: 1MHz on GCLK3 for TC4 + GCLK->GENDIV.reg = GCLK_GENDIV_ID(3) | GCLK_GENDIV_DIV(48); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(3); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + // Set GCLK8 to 1 kHz. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); + while (GCLK->STATUS.bit.SYNCBUSY) { + } +} + +void enable_sercom_clock(int id) { + // Next: Set up the clocks + // Enable synchronous clock. The bits are nicely arranged + PM->APBCMASK.reg |= 0x04 << id; + // Select multiplexer generic clock source and enable. + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | sercom_gclk_id[id]; + // Wait while it updates synchronously. + while (GCLK->STATUS.bit.SYNCBUSY) { + } +} diff --git a/ports/samd/clock_config.c b/ports/samd/mcu/samd51/clock_config.c similarity index 59% rename from ports/samd/clock_config.c rename to ports/samd/mcu/samd51/clock_config.c index 0f5634fdb7373..3a3f40385f5d3 100644 --- a/ports/samd/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -34,13 +34,6 @@ static uint32_t cpu_freq = CPU_FREQ; static uint32_t apb_freq = APB_FREQ; -#if defined(MCU_SAMD21) -int sercom_gclk_id[] = { - GCLK_CLKCTRL_ID_SERCOM0_CORE, GCLK_CLKCTRL_ID_SERCOM1_CORE, - GCLK_CLKCTRL_ID_SERCOM2_CORE, GCLK_CLKCTRL_ID_SERCOM3_CORE, - GCLK_CLKCTRL_ID_SERCOM4_CORE, GCLK_CLKCTRL_ID_SERCOM5_CORE -}; -#elif defined(MCU_SAMD51) int sercom_gclk_id[] = { SERCOM0_GCLK_ID_CORE, SERCOM1_GCLK_ID_CORE, SERCOM2_GCLK_ID_CORE, SERCOM3_GCLK_ID_CORE, @@ -49,7 +42,6 @@ int sercom_gclk_id[] = { SERCOM6_GCLK_ID_CORE, SERCOM7_GCLK_ID_CORE, #endif }; -#endif uint32_t get_cpu_freq(void) { return cpu_freq; @@ -59,12 +51,6 @@ uint32_t get_apb_freq(void) { return apb_freq; } -#if defined(MCU_SAMD21) -void set_cpu_freq(uint32_t cpu_freq_arg) { - cpu_freq = cpu_freq_arg; -} - -#elif defined(MCU_SAMD51) void set_cpu_freq(uint32_t cpu_freq_arg) { cpu_freq = cpu_freq_arg; @@ -97,113 +83,8 @@ void set_cpu_freq(uint32_t cpu_freq_arg) { while (GCLK->SYNCBUSY.bit.GENCTRL0) { } } -#endif void init_clocks(uint32_t cpu_freq) { - #if defined(MCU_SAMD21) - - // SAMD21 Clock settings - // GCLK0: 48MHz from DFLL open loop mode or closed loop mode from 32k Crystal - // GCLK1: 32768 Hz from 32K ULP or 32k Crystal - // GCLK2: 48MHz from DFLL for Peripherals - // GCLK3: 1Mhz for the us-counter (TC3/TC4) - // GCLK8: 1kHz clock for WDT - - NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" - NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz - - #if MICROPY_HW_XOSC32K - // Set up OSC32K according datasheet 17.6.3 - SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x3) | SYSCTRL_XOSC32K_EN32K | - SYSCTRL_XOSC32K_XTALEN; - SYSCTRL->XOSC32K.bit.ENABLE = 1; - while (SYSCTRL->PCLKSR.bit.XOSC32KRDY == 0) { - } - // Set up the DFLL48 according to the data sheet 17.6.7.1.2 - // Step 1: Set up the reference clock - // Connect the OSC32K via GCLK1 to the DFLL input and for further use. - GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(1); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_CLKEN; - // Enable access to the DFLLCTRL reg acc. to Errata 1.2.1 - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; - while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { - } - // Step 2: Set the coarse and fine values. - // The coarse setting will be taken from the calibration data. So the value used here - // does not matter. Get the coarse value from the calib data. In case it is not set, - // set a midrange value. - uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) - >> FUSES_DFLL48M_COARSE_CAL_Pos; - if (coarse == 0x3f) { - coarse = 0x1f; - } - SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); - while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { - } - // Step 3: Set the multiplication values. The offset of 16384 to the freq is for rounding. - SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL((CPU_FREQ + 16384) / 32768) | - SYSCTRL_DFLLMUL_FSTEP(1) | SYSCTRL_DFLLMUL_CSTEP(1); - while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { - } - // Step 4: Start the DFLL and wait for the PLL lock. We just wait for the fine lock, since - // coarse adjusting is bypassed. - SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | - SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE; - while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) { - } - - #else // MICROPY_HW_XOSC32K - - // Enable DFLL48M - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; - while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { - } - SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) - | SYSCTRL_DFLLMUL_MUL(48000); - uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) - >> FUSES_DFLL48M_COARSE_CAL_Pos; - if (coarse == 0x3f) { - coarse = 0x1f; - } - SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM - | SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE; - while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { - } - // Enable 32768 Hz on GCLK1 for consistency - GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(48016384 / 32768); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(1); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - - #endif // MICROPY_HW_XOSC32K - - // Enable GCLK output: 48M on both CCLK0 and GCLK2 - GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - - // Enable GCLK output: 1MHz on GCLK3 for TC3 - GCLK->GENDIV.reg = GCLK_GENDIV_ID(3) | GCLK_GENDIV_DIV(48); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(3); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - // Set GCLK8 to 1 kHz. - GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - - #elif defined(MCU_SAMD51) - // SAMD51 clock settings // GCLK0: 48MHz from DFLL48M or 48 - 200 MHz from DPLL0 (SAMD51) // GCLK1: DPLLx_REF_FREQ 32768 Hz from 32KULP or 32k Crystal @@ -299,21 +180,10 @@ void init_clocks(uint32_t cpu_freq) { GCLK->GENCTRL[3].reg = GCLK_GENCTRL_DIV(6) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL3) { } - - #endif // defined(MCU_SAMD51) } void enable_sercom_clock(int id) { // Next: Set up the clocks - #if defined(MCU_SAMD21) - // Enable synchronous clock. The bits are nicely arranged - PM->APBCMASK.reg |= 0x04 << id; - // Select multiplexer generic clock source and enable. - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | sercom_gclk_id[id]; - // Wait while it updates synchronously. - while (GCLK->STATUS.bit.SYNCBUSY) { - } - #elif defined(MCU_SAMD51) GCLK->PCHCTRL[sercom_gclk_id[id]].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; // no easy way to set the clocks, except enabling all of them switch (id) { @@ -344,5 +214,4 @@ void enable_sercom_clock(int id) { break; #endif } - #endif } From 20e7313453b34778df7cddbe3bc6bd72d3b737d7 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 15 Jun 2022 18:58:02 +0200 Subject: [PATCH 2714/3533] samd/clock_config: Add HW_DFLL_USB_SYNC and HW_MCU_OSC32KULP extensions. Two new compile flags are: MICROPY_HW_DFLL_USB_SYNC: Effective only if DFLL48 does not run from the crystal. It will synchronize the DFLL48M clock with the USB's SOF pulse. If no USB is connected, it will fall back to open loop mode. The DFLL48M clock is then pretty precise, but with a higher clock jitter at SAMD51 devices. MICROPY_HW_MCU_OSC32KULP: Effective only if the devics uses a crystal as clock source. Run the MCU clock from the ULP 32kHz oszillator instead of the crystal. This flag was added to cater for a interference problem of the crystal and Neopixel/Debug pins at Adafruit FEATHER Mx boards, which causes the board to crash. Drawback: ticks_ms() and time.time() vs. than ticks_us() and the peripherals like PWM run at not synchronous clocks. --- .../mpconfigboard.h | 3 +- .../mpconfigboard.h | 2 + .../mpconfigboard.h | 2 + ports/samd/clock_config.h | 1 + ports/samd/mcu/samd21/clock_config.c | 68 +++++++++++++--- ports/samd/mcu/samd51/clock_config.c | 78 +++++++++++++++++-- ports/samd/samd_soc.c | 1 + 7 files changed, 137 insertions(+), 18 deletions(-) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h index 35945395fcc0d..b78c003b19a5d 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.h @@ -1,4 +1,5 @@ #define MICROPY_HW_BOARD_NAME "Feather M4 Express" #define MICROPY_HW_MCU_NAME "SAMD51J19A" -#define MICROPY_HW_XOSC32K (1) +#define MICROPY_HW_XOSC32K (1) +#define MICROPY_HW_MCU_OSC32KULP (1) diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h index d647af9312eda..160c61ea2aaae 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "ItsyBitsy M0 Express" #define MICROPY_HW_MCU_NAME "SAMD21G18A" + +#define MICROPY_HW_DFLL_USB_SYNC (1) diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h index 2fffc9c7de28a..f53481d631917 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.h @@ -1,2 +1,4 @@ #define MICROPY_HW_BOARD_NAME "ItsyBitsy M4 Express" #define MICROPY_HW_MCU_NAME "SAMD51G19A" + +#define MICROPY_HW_DFLL_USB_SYNC (1) diff --git a/ports/samd/clock_config.h b/ports/samd/clock_config.h index 0e7a9e3280ec3..6e48b286f4989 100644 --- a/ports/samd/clock_config.h +++ b/ports/samd/clock_config.h @@ -30,4 +30,5 @@ void init_clocks(uint32_t cpu_freq); void set_cpu_freq(uint32_t cpu_freq); uint32_t get_cpu_freq(void); uint32_t get_apb_freq(void); +void check_usb_recovery_mode(void); void enable_sercom_clock(int id); diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index 3d189d67e18ca..e330d7ba6af23 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -29,10 +29,12 @@ #include #include "py/runtime.h" +#include "py/mphal.h" #include "samd_soc.h" static uint32_t cpu_freq = CPU_FREQ; static uint32_t apb_freq = APB_FREQ; +static uint32_t dfll48m_calibration; int sercom_gclk_id[] = { GCLK_CLKCTRL_ID_SERCOM0_CORE, GCLK_CLKCTRL_ID_SERCOM1_CORE, @@ -52,14 +54,28 @@ void set_cpu_freq(uint32_t cpu_freq_arg) { cpu_freq = cpu_freq_arg; } +void check_usb_recovery_mode(void) { + #if !MICROPY_HW_XOSC32K + mp_hal_delay_ms(500); + // Check USB status. If not connected, switch DFLL48M back to open loop + if (USB->DEVICE.DeviceEndpoint[0].EPCFG.reg == 0) { + // Set/keep the open loop mode of the device. + SYSCTRL->DFLLVAL.reg = dfll48m_calibration; + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_ENABLE; + } + #endif // MICROPY_HW_XOSC32K +} void init_clocks(uint32_t cpu_freq) { + dfll48m_calibration = 0; // please the compiler + // SAMD21 Clock settings // GCLK0: 48MHz from DFLL open loop mode or closed loop mode from 32k Crystal - // GCLK1: 32768 Hz from 32K ULP or 32k Crystal + // GCLK1: 32768 Hz from 32K ULP or DFLL48M // GCLK2: 48MHz from DFLL for Peripherals // GCLK3: 1Mhz for the us-counter (TC4/TC5) + // GCLK4: 32kHz from crystal, if present // GCLK8: 1kHz clock for WDT NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" @@ -74,19 +90,34 @@ void init_clocks(uint32_t cpu_freq) { } // Set up the DFLL48 according to the data sheet 17.6.7.1.2 // Step 1: Set up the reference clock - // Connect the OSC32K via GCLK1 to the DFLL input and for further use. + + #if MICROPY_HW_MCU_OSC32KULP + // Connect the GCLK1 to the XOSC32KULP + GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(1); + #else + // Connect the GCLK1 to OSC32K via GCLK1 to the DFLL input and for further use. GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1); GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(1); + #endif + while (GCLK->STATUS.bit.SYNCBUSY) { } - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_CLKEN; + + // Connect the GCLK4 to OSC32K via GCLK1 to the DFLL input and for further use. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(4) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(4); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + + // Connect GCLK4 to the DFLL input and for further use. + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_GEN_GCLK4 | GCLK_CLKCTRL_CLKEN; // Enable access to the DFLLCTRL reg acc. to Errata 1.2.1 SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) { } // Step 2: Set the coarse and fine values. - // The coarse setting will be taken from the calibration data. So the value used here - // does not matter. Get the coarse value from the calib data. In case it is not set, + // Get the coarse value from the calib data. In case it is not set, // set a midrange value. uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) >> FUSES_DFLL48M_COARSE_CAL_Pos; @@ -103,7 +134,7 @@ void init_clocks(uint32_t cpu_freq) { } // Step 4: Start the DFLL and wait for the PLL lock. We just wait for the fine lock, since // coarse adjusting is bypassed. - SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | + SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | SYSCTRL_DFLLCTRL_STABLE | SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE; while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) { } @@ -114,18 +145,33 @@ void init_clocks(uint32_t cpu_freq) { SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE; while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { } - SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) - | SYSCTRL_DFLLMUL_MUL(48000); + uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk) >> FUSES_DFLL48M_COARSE_CAL_Pos; if (coarse == 0x3f) { coarse = 0x1f; } - SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512); - SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_USBCRM + SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(511); + + #if MICROPY_HW_DFLL_USB_SYNC + // Configure the DFLL48M for USB clock recovery. + // Will have to switch back if no USB + SYSCTRL->DFLLSYNC.bit.READREQ = 1; + dfll48m_calibration = SYSCTRL->DFLLVAL.reg; + // Set the Multiplication factor. + SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1) + | SYSCTRL_DFLLMUL_MUL(48000); + // Set the mode to closed loop USB Recovery mode + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_USBCRM | SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE; + #else + // Set/keep the open loop mode of the device. + SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_ENABLE; + #endif + while (!SYSCTRL->PCLKSR.bit.DFLLRDY) { } + // Enable 32768 Hz on GCLK1 for consistency GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(48016384 / 32768); GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(1); @@ -154,10 +200,10 @@ void init_clocks(uint32_t cpu_freq) { GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); while (GCLK->STATUS.bit.SYNCBUSY) { } + } void enable_sercom_clock(int id) { - // Next: Set up the clocks // Enable synchronous clock. The bits are nicely arranged PM->APBCMASK.reg |= 0x04 << id; // Select multiplexer generic clock source and enable. diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index 3a3f40385f5d3..62c05193dd8b6 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -29,10 +29,12 @@ #include #include "py/runtime.h" +#include "py/mphal.h" #include "samd_soc.h" static uint32_t cpu_freq = CPU_FREQ; static uint32_t apb_freq = APB_FREQ; +static uint32_t dfll48m_calibration; int sercom_gclk_id[] = { SERCOM0_GCLK_ID_CORE, SERCOM1_GCLK_ID_CORE, @@ -84,12 +86,43 @@ void set_cpu_freq(uint32_t cpu_freq_arg) { } } +void check_usb_recovery_mode(void) { + #if !MICROPY_HW_XOSC32K + mp_hal_delay_ms(500); + // Check USB status. If not connected, switch DFLL48M back to open loop + if (USB->DEVICE.DeviceEndpoint[0].EPCFG.reg == 0) { + // as per Errata 2.8.3 + OSCCTRL->DFLLMUL.reg = 0; + while (OSCCTRL->DFLLSYNC.bit.DFLLMUL == 1) { + } + // Set the mode to open loop mode + OSCCTRL->DFLLCTRLB.reg = 0; + while (OSCCTRL->DFLLSYNC.bit.DFLLCTRLB == 1) { + } + OSCCTRL->DFLLCTRLA.reg = OSCCTRL_DFLLCTRLA_RUNSTDBY | OSCCTRL_DFLLCTRLA_ENABLE; + while (OSCCTRL->DFLLSYNC.bit.ENABLE == 1) { + } + OSCCTRL->DFLLVAL.reg = dfll48m_calibration; // Reload DFLLVAL register + while (OSCCTRL->DFLLSYNC.bit.DFLLVAL == 1) { + } + // Set the mode to open loop mode + OSCCTRL->DFLLCTRLB.reg = 0; + while (OSCCTRL->DFLLSYNC.bit.DFLLCTRLB == 1) { + } + } + #endif // MICROPY_HW_XOSC32K +} + void init_clocks(uint32_t cpu_freq) { + + dfll48m_calibration = 0; // please the compiler + // SAMD51 clock settings // GCLK0: 48MHz from DFLL48M or 48 - 200 MHz from DPLL0 (SAMD51) - // GCLK1: DPLLx_REF_FREQ 32768 Hz from 32KULP or 32k Crystal + // GCLK1: 32768 Hz from 32KULP or DFLL48M // GCLK2: 48MHz from DFLL48M for Peripheral devices // GCLK3: 16Mhz for the us-counter (TC0/TC1) + // GCLK4: 32kHz from crystal, if present // DPLL0: 48 - 200 MHz // Steps to set up clocks: @@ -102,6 +135,7 @@ void init_clocks(uint32_t cpu_freq) { // Setup GCLK0 to 120MHz // Setup GCLK2 to 48MHz for Peripherals // Setup GCLK3 to 8MHz for TC0/TC1 + // Setup GCLK4 to 32kHz crystal, if present // Setup GCLK0 for 48MHz as default state to keep the MCU running during config change. GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; @@ -124,15 +158,26 @@ void init_clocks(uint32_t cpu_freq) { while (OSC32KCTRL->STATUS.bit.XOSC32KRDY == 0) { } + #if MICROPY_HW_MCU_OSC32KULP + // Setup GCLK1 for 32kHz ULP + GCLK->GENCTRL[1].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K; + #else // Setup GCLK1 for 32kHz crystal GCLK->GENCTRL[1].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K; + #endif + while (GCLK->SYNCBUSY.bit.GENCTRL1) { } + // Setup GCLK4 for 32kHz crystal + GCLK->GENCTRL[4].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K; + while (GCLK->SYNCBUSY.bit.GENCTRL4) { + } + // Set-up the DFLL48M in closed loop mode with input from the 32kHz crystal - // Step 1: Peripheral channel 0 is driven by GCLK1 and it feeds DFLL48M - GCLK->PCHCTRL[0].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; + // Step 1: Peripheral channel 0 is driven by GCLK4 and it feeds DFLL48M + GCLK->PCHCTRL[0].reg = GCLK_PCHCTRL_GEN_GCLK4 | GCLK_PCHCTRL_CHEN; while (GCLK->PCHCTRL[0].bit.CHEN == 0) { } // Step 2: Set the multiplication values. The offset of 16384 to the freq is for rounding. @@ -141,7 +186,7 @@ void init_clocks(uint32_t cpu_freq) { while (OSCCTRL->DFLLSYNC.bit.DFLLMUL == 1) { } // Step 3: Set the mode to closed loop - OSCCTRL->DFLLCTRLB.reg = OSCCTRL_DFLLCTRLB_BPLCKC | OSCCTRL_DFLLCTRLB_MODE; + OSCCTRL->DFLLCTRLB.reg = OSCCTRL_DFLLCTRLB_BPLCKC | OSCCTRL_DFLLCTRLB_STABLE | OSCCTRL_DFLLCTRLB_MODE; while (OSCCTRL->DFLLSYNC.bit.DFLLCTRLB == 1) { } // Wait for lock fine @@ -154,12 +199,34 @@ void init_clocks(uint32_t cpu_freq) { #else // MICROPY_HW_XOSC32K - // Set GCLK1 to DPLL0_REF_FREQ as defined in mpconfigboard.h (e.g. 32768 Hz) + // Derive GCLK1 from DFLL48M at DPLL0_REF_FREQ as defined in mpconfigboard.h (e.g. 32768 Hz) GCLK->GENCTRL[1].reg = ((APB_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) << GCLK_GENCTRL_DIV_Pos | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL1) { } + OSCCTRL->DFLLCTRLA.bit.RUNSTDBY = 1; + OSCCTRL->DFLLCTRLA.bit.ONDEMAND = 0; + + OSCCTRL->DFLLCTRLA.bit.ENABLE = 1; + while (OSCCTRL->DFLLSYNC.bit.ENABLE == 1) { + } + + #if MICROPY_HW_DFLL_USB_SYNC + // Configure the DFLL48M for USB clock recovery. + // Will have to switch back if no USB + dfll48m_calibration = OSCCTRL->DFLLVAL.reg; + // Set the Multiplication factor. + OSCCTRL->DFLLMUL.reg = OSCCTRL_DFLLMUL_MUL(48000) | + OSCCTRL_DFLLMUL_FSTEP(1) | OSCCTRL_DFLLMUL_CSTEP(1); + while (OSCCTRL->DFLLSYNC.bit.DFLLMUL == 1) { + } + // Set the mode to closed loop USB Recovery + OSCCTRL->DFLLCTRLB.reg = OSCCTRL_DFLLCTRLB_USBCRM | OSCCTRL_DFLLCTRLB_CCDIS | OSCCTRL_DFLLCTRLB_MODE; + while (OSCCTRL->DFLLSYNC.bit.DFLLCTRLB == 1) { + } + #endif + #endif // MICROPY_HW_XOSC32K // Peripheral channel 1 is driven by GCLK1 and it feeds DPLL0 @@ -183,7 +250,6 @@ void init_clocks(uint32_t cpu_freq) { } void enable_sercom_clock(int id) { - // Next: Set up the clocks GCLK->PCHCTRL[sercom_gclk_id[id]].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; // no easy way to set the clocks, except enabling all of them switch (id) { diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index d0df4a7e28cc1..f4a27f3df740b 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -111,6 +111,7 @@ void samd_init(void) { SysTick_Config(get_cpu_freq() / 1000); init_us_counter(); usb_init(); + check_usb_recovery_mode(); #if defined(MCU_SAMD51) mp_hal_ticks_cpu_enable(); #endif From 85fb8b8b027d0af560a2b74e5bd4a0554600f6bd Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 16 Jun 2022 11:32:21 +0200 Subject: [PATCH 2715/3533] samd/pin_af: Simplify the pin-af-table handling. Changes are: - The pin-af-table-SAMDxx.csv file are moved to the mcu directories with the name as pin-af-table.csv. - The handling in Makefile and pin_af.c is simplified. --- ports/samd/Makefile | 3 +-- .../samd21/pin-af-table.csv} | 8 ++++---- .../samd51/pin-af-table.csv} | 0 ports/samd/pin_af.c | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) rename ports/samd/{boards/pin-af-table-SAMD21.csv => mcu/samd21/pin-af-table.csv} (95%) rename ports/samd/{boards/pin-af-table-SAMD51.csv => mcu/samd51/pin-af-table.csv} (100%) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 8d49778b3bcc8..9a03502abcdd4 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -48,7 +48,7 @@ INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include/pio INC += -I$(TOP)/lib/tinyusb/src MAKE_PIN_AF = boards/make-pin-af.py -PIN_AF_TABLE_CSV = boards/pin-af-table-$(MCU_SERIES).csv +PIN_AF_TABLE_CSV = mcu/$(MCU_SERIES_LOWER)/pin-af-table.csv GEN_PIN_AF = pin_af_table.c MAKE_PINS = boards/make-pins.py @@ -62,7 +62,6 @@ CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERI CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -DMPCONFIG_MCU_H='' -CFLAGS += -DPIN_AF_TABLE_C='<$(BUILD)/$(GEN_PIN_AF)>' LDFLAGS = -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref LDFLAGS += $(LDFLAGS_MOD) diff --git a/ports/samd/boards/pin-af-table-SAMD21.csv b/ports/samd/mcu/samd21/pin-af-table.csv similarity index 95% rename from ports/samd/boards/pin-af-table-SAMD21.csv rename to ports/samd/mcu/samd21/pin-af-table.csv index d8ab903c9e7c4..6445d148b8fae 100644 --- a/ports/samd/boards/pin-af-table-SAMD21.csv +++ b/ports/samd/mcu/samd21/pin-af-table.csv @@ -59,7 +59,7 @@ pa30,10,,,12,10, pa31,11,,,13,11, pb30,14,,,50,00,12 pb31,15,,,51,01,13 -pb00,0,,,52,70, -pb01,1,,,53,71, -pb02,2,,,50,60, -pb03,3,,,51,61, +pb00,0,8,,52,70, +pb01,1,9,,53,71, +pb02,2,10,,50,60, +pb03,3,11,,51,61, diff --git a/ports/samd/boards/pin-af-table-SAMD51.csv b/ports/samd/mcu/samd51/pin-af-table.csv similarity index 100% rename from ports/samd/boards/pin-af-table-SAMD51.csv rename to ports/samd/mcu/samd51/pin-af-table.csv diff --git a/ports/samd/pin_af.c b/ports/samd/pin_af.c index 8c152e0d8be02..926c4ae0c23aa 100644 --- a/ports/samd/pin_af.c +++ b/ports/samd/pin_af.c @@ -38,7 +38,7 @@ extern const uint8_t tcc_channel_count[]; -#include PIN_AF_TABLE_C +#include "pin_af_table.c" // Just look for an table entry for a given pin and raise an error // in case of no match (which should not happen). From 560170de024353354b4bcd1682392f2bb282c12d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 21 Jun 2022 18:00:51 +0200 Subject: [PATCH 2716/3533] samd/samd_flash: Remove obsolete printf's and return values instead. Returning values is much more useful. --- ports/samd/samd_flash.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ports/samd/samd_flash.c b/ports/samd/samd_flash.c index 522522ef84738..2fffd614f9720 100644 --- a/ports/samd/samd_flash.c +++ b/ports/samd/samd_flash.c @@ -96,21 +96,17 @@ STATIC mp_obj_t eraseblock(uint32_t sector_in) { } STATIC mp_obj_t samd_flash_version(void) { - printf("Flash Driver Version: %lu\n", flash_get_version()); - return mp_const_none; + return MP_OBJ_NEW_SMALL_INT(flash_get_version()); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_version_obj, samd_flash_version); -STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(samd_flash_version_static_obj, MP_ROM_PTR(&samd_flash_version_obj)); STATIC mp_obj_t samd_flash_size(void) { // ASF4 API calls mp_int_t PAGES = flash_get_total_pages(&flash_desc); mp_int_t PAGE_SIZE = flash_get_page_size(&flash_desc); - printf("Flash Size: %u Bytes\n", PAGES * PAGE_SIZE); - return mp_const_none; + return MP_OBJ_NEW_SMALL_INT(PAGES * PAGE_SIZE); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_size_obj, samd_flash_size); -STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(samd_flash_size_static_obj, MP_ROM_PTR(&samd_flash_size_obj)); STATIC mp_obj_t samd_flash_readblocks(size_t n_args, const mp_obj_t *args) { uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base; @@ -172,8 +168,8 @@ STATIC mp_obj_t samd_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl); STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_flash_version), MP_ROM_PTR(&samd_flash_version_static_obj) }, - { MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&samd_flash_size_static_obj) }, + { MP_ROM_QSTR(MP_QSTR_flash_version), MP_ROM_PTR(&samd_flash_version_obj) }, + { MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&samd_flash_size_obj) }, { MP_ROM_QSTR(MP_QSTR_flash_init), MP_ROM_PTR(&samd_flash_init_obj) }, { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&samd_flash_readblocks_obj) }, { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&samd_flash_writeblocks_obj) }, From b0017304625b5d33bea548166b010bd2c3bf465d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 24 Jun 2022 08:27:23 +0200 Subject: [PATCH 2717/3533] samd/Makefile: Fix a dependency problem with "make -j". The build directory was not created before attempting to create the generated files in it. --- ports/samd/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 9a03502abcdd4..d8ca0f8ba6980 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -194,12 +194,13 @@ $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin pin_af.c: $(BUILD)/$(GEN_PIN_AF) -$(BUILD)/$(GEN_PIN_AF): $(PIN_AF_TABLE_CSV) +$(BUILD)/$(GEN_PIN_AF): $(PIN_AF_TABLE_CSV) | $(HEADER_BUILD) $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PIN_AF) --csv $(PIN_AF_TABLE_CSV) --table $(BUILD)/$(GEN_PIN_AF) --mcu $(MCU_SERIES) +machine_led.c machine_pin.c modsamd.c: $(GEN_PINS_HDR) -$(GEN_PINS_SRC): $(BOARD_PINS) +$(GEN_PINS_SRC) $(GEN_PINS_HDR): $(BOARD_PINS) | $(HEADER_BUILD) $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --pins $(GEN_PINS_SRC) --inc $(GEN_PINS_HDR) From e9a76310ec567dd94712a92876ca150bac43f103 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 28 Jun 2022 16:59:01 +0200 Subject: [PATCH 2718/3533] samd/mphalport: Fix USB endpoint handling ignoring Ctrl-C. Porting PR #8040 by @hoihu to SAMD, following the commit 587339022689187a1acbccc1d0e2425a67385ff7. One small addition: before executing keyboard interrupt, the input buffer is cleared. --- ports/samd/Makefile | 1 + ports/samd/mphalport.c | 75 +++++++++++++++++++++++++++++++----------- ports/samd/mphalport.h | 1 + 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index d8ca0f8ba6980..5ff00f21f9266 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -131,6 +131,7 @@ SRC_C += \ shared/libc/string0.c \ shared/readline/readline.c \ shared/runtime/gchelper_native.c \ + shared/runtime/interrupt_char.c \ shared/runtime/pyexec.c \ shared/runtime/softtimer.c \ shared/runtime/stdout_helpers.c \ diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index fc451023903ce..ad3817768f777 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -33,22 +33,54 @@ #include "samd_soc.h" #include "tusb.h" -#if MICROPY_KBD_EXCEPTION -int mp_interrupt_char = -1; - -void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { - (void)itf; - (void)wanted_char; - tud_cdc_read_char(); // discard interrupt char - mp_sched_keyboard_interrupt(); -} +#ifndef MICROPY_HW_STDIN_BUFFER_LEN +#define MICROPY_HW_STDIN_BUFFER_LEN 128 +#endif + +STATIC uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN]; +ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0 }; -void mp_hal_set_interrupt_char(int c) { - mp_interrupt_char = c; - tud_cdc_set_wanted_char(c); +uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll + +void poll_cdc_interfaces(void) { + // any CDC interfaces left to poll? + if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) { + for (uint8_t itf = 0; itf < 8; ++itf) { + if (cdc_itf_pending & (1 << itf)) { + tud_cdc_rx_cb(itf); + if (!cdc_itf_pending) { + break; + } + } + } + } } -#endif +void tud_cdc_rx_cb(uint8_t itf) { + // consume pending USB data immediately to free usb buffer and keep the endpoint from stalling. + // in case the ringbuffer is full, mark the CDC interface that need attention later on for polling + cdc_itf_pending &= ~(1 << itf); + for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) { + if (ringbuf_free(&stdin_ringbuf)) { + int data_char = tud_cdc_read_char(); + #if MICROPY_KBD_EXCEPTION + if (data_char == mp_interrupt_char) { + // Clear the ring buffer + stdin_ringbuf.iget = stdin_ringbuf.iput = 0; + // and stop + mp_sched_keyboard_interrupt(); + } else { + ringbuf_put(&stdin_ringbuf, data_char); + } + #else + ringbuf_put(&stdin_ringbuf, data_char); + #endif + } else { + cdc_itf_pending |= (1 << itf); + return; + } + } +} void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux) { int pin_grp = pin / 32; @@ -94,9 +126,12 @@ void mp_hal_delay_us(mp_uint_t us) { uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { uintptr_t ret = 0; - if (tud_cdc_connected() && tud_cdc_available()) { + + poll_cdc_interfaces(); + if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) { ret |= MP_STREAM_POLL_RD; } + #if MICROPY_PY_OS_DUPTERM ret |= mp_uos_dupterm_poll(poll_flags); #endif @@ -105,13 +140,13 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { int mp_hal_stdin_rx_chr(void) { for (;;) { - if (tud_cdc_connected() && tud_cdc_available()) { - uint8_t buf[1]; - uint32_t count = tud_cdc_read(buf, sizeof(buf)); - if (count) { - return buf[0]; - } + + poll_cdc_interfaces(); + int c = ringbuf_get(&stdin_ringbuf); + if (c != -1) { + return c; } + #if MICROPY_PY_OS_DUPTERM int dupterm_c = mp_uos_dupterm_rx_chr(); if (dupterm_c >= 0) { diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index f1efd550035af..ced0d642b4ed4 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -27,6 +27,7 @@ #ifndef MICROPY_INCLUDED_SAMD_MPHALPORT_H #define MICROPY_INCLUDED_SAMD_MPHALPORT_H +#include "py/ringbuf.h" #include "py/mpconfig.h" // ASF4 From d9338aabc5647979f4a0ec3cb0a7e987a2a2249f Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 30 Jun 2022 16:50:15 +0200 Subject: [PATCH 2719/3533] samd: Change the symbol names for the peripheral clocks. From APB_FREQ to DFLL48M_FREQ, and from apb_freq to peripheral_freq. --- ports/samd/clock_config.h | 2 +- ports/samd/machine_i2c.c | 2 +- ports/samd/machine_pwm.c | 3 ++- ports/samd/machine_spi.c | 4 ++-- ports/samd/machine_uart.c | 4 ++-- ports/samd/mcu/samd21/clock_config.c | 6 +++--- ports/samd/mcu/samd21/mpconfigmcu.h | 2 +- ports/samd/mcu/samd51/clock_config.c | 12 ++++++------ ports/samd/mcu/samd51/mpconfigmcu.h | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/ports/samd/clock_config.h b/ports/samd/clock_config.h index 6e48b286f4989..35b7e532814b4 100644 --- a/ports/samd/clock_config.h +++ b/ports/samd/clock_config.h @@ -29,6 +29,6 @@ void init_clocks(uint32_t cpu_freq); void set_cpu_freq(uint32_t cpu_freq); uint32_t get_cpu_freq(void); -uint32_t get_apb_freq(void); +uint32_t get_peripheral_freq(void); void check_usb_recovery_mode(void); void enable_sercom_clock(int id); diff --git a/ports/samd/machine_i2c.c b/ports/samd/machine_i2c.c index 90ea5e10ca43b..25dfa99340ffe 100644 --- a/ports/samd/machine_i2c.c +++ b/ports/samd/machine_i2c.c @@ -185,7 +185,7 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n // baud = peripheral_freq / (2 * baudrate) - 5 - (rise_time * peripheral_freq) / 2 // Just set the minimal configuration for standard and fast mode. // Set Baud. Assume ~300ns rise time. Maybe set later by a keyword argument. - i2c->I2CM.BAUD.reg = get_apb_freq() / (2 * self->freq) - 5 - (get_apb_freq() / 1000000) * RISETIME_NS / 2000; + i2c->I2CM.BAUD.reg = get_peripheral_freq() / (2 * self->freq) - 5 - (get_peripheral_freq() / 1000000) * RISETIME_NS / 2000; // Enable interrupts sercom_register_irq(self->id, &common_i2c_irq_handler); diff --git a/ports/samd/machine_pwm.c b/ports/samd/machine_pwm.c index 6f9ca42b1b2aa..d987927d310c3 100644 --- a/ports/samd/machine_pwm.c +++ b/ports/samd/machine_pwm.c @@ -28,6 +28,7 @@ #include "py/runtime.h" #include "py/mphal.h" #include "modmachine.h" +#include "clock_config.h" #include "sam.h" #include "pin_af.h" @@ -50,7 +51,7 @@ typedef struct _machine_pwm_obj_t { #define PWM_NOT_INIT (0) #define PWM_CLK_READY (1) #define PWM_TCC_ENABLED (2) -#define PWM_MASTER_CLK (48000000) +#define PWM_MASTER_CLK (get_peripheral_freq()) #define PWM_FULL_SCALE (65536) static Tcc *tcc_instance[] = TCC_INSTS; diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c index ddb8756e9a34e..8321f19cf0dd8 100644 --- a/ports/samd/machine_spi.c +++ b/ports/samd/machine_spi.c @@ -206,9 +206,9 @@ STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj spi->SPI.CTRLC.reg = 1; // 1 clock cycle character spacing #endif - // SPI is driven by the clock of GCLK Generator 2, freq in bus_freq + // SPI is driven by the clock of GCLK Generator 2, freq by get_peripheral_freq() // baud = bus_freq / (2 * baudrate) - 1 - uint32_t baud = get_apb_freq() / (2 * self->baudrate) - 1; + uint32_t baud = get_peripheral_freq() / (2 * self->baudrate) - 1; spi->SPI.BAUD.reg = baud; // Set Baud // Enable RXC interrupt only if miso is defined diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 92e63ee51e6f2..1031f26c26488 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -276,9 +276,9 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args while (uart->USART.SYNCBUSY.bit.CTRLB) { } - // USART is driven by the clock of GCLK Generator 2, freq by get_apb_freq() + // USART is driven by the clock of GCLK Generator 2, freq by get_peripheral_freq() // baud rate; 65536 * (1 - 16 * 115200/bus_freq) - uint32_t baud = 65536 - ((uint64_t)(65536 * 16) * self->baudrate + get_apb_freq() / 2) / get_apb_freq(); + uint32_t baud = 65536 - ((uint64_t)(65536 * 16) * self->baudrate + get_peripheral_freq() / 2) / get_peripheral_freq(); uart->USART.BAUD.bit.BAUD = baud; // Set Baud sercom_register_irq(self->id, &common_uart_irq_handler); diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index e330d7ba6af23..2402ed2e3107a 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -33,7 +33,7 @@ #include "samd_soc.h" static uint32_t cpu_freq = CPU_FREQ; -static uint32_t apb_freq = APB_FREQ; +static uint32_t peripheral_freq = DFLL48M_FREQ; static uint32_t dfll48m_calibration; int sercom_gclk_id[] = { @@ -46,8 +46,8 @@ uint32_t get_cpu_freq(void) { return cpu_freq; } -uint32_t get_apb_freq(void) { - return apb_freq; +uint32_t get_peripheral_freq(void) { + return peripheral_freq; } void set_cpu_freq(uint32_t cpu_freq_arg) { diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index dd16a6c8affd4..ecb125bbdb771 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -16,7 +16,7 @@ #define MICROPY_HW_UART_TXBUF (1) #define CPU_FREQ (48000000) -#define APB_FREQ (48000000) +#define DFLL48M_FREQ (48000000) #define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1) diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index 62c05193dd8b6..31c8f5a8654c2 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -33,7 +33,7 @@ #include "samd_soc.h" static uint32_t cpu_freq = CPU_FREQ; -static uint32_t apb_freq = APB_FREQ; +static uint32_t peripheral_freq = DFLL48M_FREQ; static uint32_t dfll48m_calibration; int sercom_gclk_id[] = { @@ -49,8 +49,8 @@ uint32_t get_cpu_freq(void) { return cpu_freq; } -uint32_t get_apb_freq(void) { - return apb_freq; +uint32_t get_peripheral_freq(void) { + return peripheral_freq; } void set_cpu_freq(uint32_t cpu_freq_arg) { @@ -181,7 +181,7 @@ void init_clocks(uint32_t cpu_freq) { while (GCLK->PCHCTRL[0].bit.CHEN == 0) { } // Step 2: Set the multiplication values. The offset of 16384 to the freq is for rounding. - OSCCTRL->DFLLMUL.reg = OSCCTRL_DFLLMUL_MUL((APB_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) | + OSCCTRL->DFLLMUL.reg = OSCCTRL_DFLLMUL_MUL((DFLL48M_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) | OSCCTRL_DFLLMUL_FSTEP(1) | OSCCTRL_DFLLMUL_CSTEP(1); while (OSCCTRL->DFLLSYNC.bit.DFLLMUL == 1) { } @@ -200,7 +200,7 @@ void init_clocks(uint32_t cpu_freq) { #else // MICROPY_HW_XOSC32K // Derive GCLK1 from DFLL48M at DPLL0_REF_FREQ as defined in mpconfigboard.h (e.g. 32768 Hz) - GCLK->GENCTRL[1].reg = ((APB_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) << GCLK_GENCTRL_DIV_Pos + GCLK->GENCTRL[1].reg = ((DFLL48M_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) << GCLK_GENCTRL_DIV_Pos | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL1) { } @@ -236,7 +236,7 @@ void init_clocks(uint32_t cpu_freq) { set_cpu_freq(cpu_freq); - apb_freq = APB_FREQ; // To be changed if CPU_FREQ < 48M + peripheral_freq = DFLL48M_FREQ; // To be changed if CPU_FREQ < 48M // Setup GCLK2 for DPLL1 output (48 MHz) GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index c3253fcbecb73..2c1ea7fee2574 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -29,7 +29,7 @@ unsigned long trng_random_u32(void); #define MICROPY_HW_UART_TXBUF (1) #define CPU_FREQ (120000000) -#define APB_FREQ (48000000) +#define DFLL48M_FREQ (48000000) #define DPLLx_REF_FREQ (32768) #define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) From 00dcf0464318764066f81f90992d5bfc40acebf1 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 21 Jun 2022 17:32:48 +0200 Subject: [PATCH 2720/3533] samd/mcu: Add floating point suport for SAMD21 devices. For consistency it should be there. --- ports/samd/mcu/samd21/mpconfigmcu.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index ecb125bbdb771..9329a6eb89bc7 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -7,7 +7,10 @@ #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) -#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_PY_BUILTINS_COMPLEX (0) +#define MICROPY_PY_MATH (0) +#define MICROPY_PY_CMATH (0) #define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) #define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) From 387025f5d12ec6313b396e63e770dd9f22f9daeb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 21 Jun 2022 20:31:20 +0200 Subject: [PATCH 2721/3533] samd/mcu: Enable the math module on SAMD51. --- ports/samd/mcu/samd51/mpconfigmcu.h | 3 ++- ports/samd/mcu/samd51/mpconfigmcu.mk | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 2c1ea7fee2574..0e5a8856556e4 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -9,7 +9,8 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_MATH (0) +#define MICROPY_PY_MATH (1) +#define MP_NEED_LOG2 (1) #define MICROPY_PY_CMATH (0) #define MICROPY_PY_UOS_URANDOM (1) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk index 461a0182ef019..0aedbbe28a373 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.mk +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -1 +1,30 @@ SRC_S += shared/runtime/gchelper_m3.s + +SRC_MOD += $(addprefix lib/libm/,\ + acoshf.c \ + asinfacosf.c \ + asinhf.c \ + atan2f.c \ + atanf.c \ + atanhf.c \ + ef_rem_pio2.c \ + erf_lgamma.c \ + fmodf.c \ + kf_cos.c \ + kf_rem_pio2.c \ + kf_sin.c \ + kf_tan.c \ + log1pf.c \ + math.c \ + nearbyintf.c \ + roundf.c \ + sf_cos.c \ + sf_erf.c \ + sf_frexp.c \ + sf_ldexp.c \ + sf_modf.c \ + sf_sin.c \ + sf_tan.c \ + wf_lgamma.c \ + wf_tgamma.c \ + ) From 9f4df86016d8181dc06683d528dbd85473a1dbb9 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 2 Jul 2022 16:26:17 +0200 Subject: [PATCH 2722/3533] samd/boards: Move the flash filesystem definitions to the linker files. They used to be in mpconfigmcu.h, but have to be different for different chip variants, like the SAMD51x20. --- ports/samd/boards/samd21x18a.ld | 3 +++ ports/samd/boards/samd51g19a.ld | 3 +++ ports/samd/boards/samd51j19a.ld | 3 +++ ports/samd/boards/samd51p19a.ld | 3 +++ ports/samd/mcu/samd21/mpconfigmcu.h | 2 -- ports/samd/mcu/samd51/mpconfigmcu.h | 7 +------ ports/samd/samd_flash.c | 6 ++++-- 7 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ports/samd/boards/samd21x18a.ld b/ports/samd/boards/samd21x18a.ld index b7d59c315b643..81a84a15d7d75 100644 --- a/ports/samd/boards/samd21x18a.ld +++ b/ports/samd/boards/samd21x18a.ld @@ -13,5 +13,8 @@ MEMORY _estack = ORIGIN(RAM) + LENGTH(RAM) - 8; _sstack = _estack - 8K; +_oflash_fs = ORIGIN(FLASH) + 192K - 8K; +_sflash_fs = LENGTH(FLASH) - 192K + 8K - 1; + _sheap = _ebss; _eheap = _sstack; diff --git a/ports/samd/boards/samd51g19a.ld b/ports/samd/boards/samd51g19a.ld index e0baa9bba0281..cd03320ba43d7 100644 --- a/ports/samd/boards/samd51g19a.ld +++ b/ports/samd/boards/samd51g19a.ld @@ -13,5 +13,8 @@ MEMORY _estack = ORIGIN(RAM) + LENGTH(RAM) - 8; _sstack = _estack - 16K; +_oflash_fs = ORIGIN(FLASH) + 384K - 16K; +_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; + _sheap = _ebss; _eheap = _sstack; diff --git a/ports/samd/boards/samd51j19a.ld b/ports/samd/boards/samd51j19a.ld index e0baa9bba0281..cd03320ba43d7 100644 --- a/ports/samd/boards/samd51j19a.ld +++ b/ports/samd/boards/samd51j19a.ld @@ -13,5 +13,8 @@ MEMORY _estack = ORIGIN(RAM) + LENGTH(RAM) - 8; _sstack = _estack - 16K; +_oflash_fs = ORIGIN(FLASH) + 384K - 16K; +_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; + _sheap = _ebss; _eheap = _sstack; diff --git a/ports/samd/boards/samd51p19a.ld b/ports/samd/boards/samd51p19a.ld index e0baa9bba0281..cd03320ba43d7 100644 --- a/ports/samd/boards/samd51p19a.ld +++ b/ports/samd/boards/samd51p19a.ld @@ -13,5 +13,8 @@ MEMORY _estack = ORIGIN(RAM) + LENGTH(RAM) - 8; _sstack = _estack - 16K; +_oflash_fs = ORIGIN(FLASH) + 384K - 16K; +_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; + _sheap = _ebss; _eheap = _sstack; diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index 9329a6eb89bc7..7ee23b59b1598 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -12,8 +12,6 @@ #define MICROPY_PY_MATH (0) #define MICROPY_PY_CMATH (0) -#define MICROPY_HW_FLASH_STORAGE_BASE (0x30000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0xFFFF) #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; #define MICROPY_HW_UART_TXBUF (1) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 0e5a8856556e4..19193992f058d 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -19,12 +19,7 @@ unsigned long trng_random_u32(void); // Due to a limitation in the TC counter for us, the ticks period is 2**29 #define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) -// MicroPython configs -// samd_flash.c flash parameters -// Build a 128k Flash storage at top. 512k-128k=384k=0x60000 -// 512*1024= 0x80000 minus 128*1024= 0x20000 = 0x60000 -#define MICROPY_HW_FLASH_STORAGE_BASE (0x60000) -#define MICROPY_HW_FLASH_STORAGE_BYTES (0x1FFFF) + #define VFS_BLOCK_SIZE_BYTES (1536) // #define MICROPY_HW_UART_TXBUF (1) diff --git a/ports/samd/samd_flash.c b/ports/samd/samd_flash.c index 2fffd614f9720..6d9ee96895b01 100644 --- a/ports/samd/samd_flash.c +++ b/ports/samd/samd_flash.c @@ -53,11 +53,13 @@ typedef struct _samd_flash_obj_t { uint32_t flash_size; } samd_flash_obj_t; +extern uint8_t _oflash_fs, _sflash_fs; + // Build a Flash storage at top. STATIC samd_flash_obj_t samd_flash_obj = { .base = { &samd_flash_type }, - .flash_base = MICROPY_HW_FLASH_STORAGE_BASE, // Board specific: mpconfigboard.h - .flash_size = MICROPY_HW_FLASH_STORAGE_BYTES, // Board specific: mpconfigboard.h + .flash_base = (uint32_t)&_oflash_fs, // Get from MCU-Specific loader script. + .flash_size = (uint32_t)&_sflash_fs, // Get from MCU-Specific loader script. }; // FLASH stuff From 65f99e371d3459cdaca4ae01895e706ff93ab285 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 2 Jul 2022 16:36:11 +0200 Subject: [PATCH 2723/3533] samd/boards: Use the same linker file for all SAMD51x19 variants. --- .../mpconfigboard.mk | 2 +- .../mpconfigboard.mk | 2 +- ports/samd/boards/MINISAM_M4/mpconfigboard.mk | 2 +- .../SEEED_WIO_TERMINAL/mpconfigboard.mk | 2 +- ports/samd/boards/samd51j19a.ld | 20 ------------------- ports/samd/boards/samd51p19a.ld | 20 ------------------- .../boards/{samd51g19a.ld => samd51x19a.ld} | 0 7 files changed, 4 insertions(+), 44 deletions(-) delete mode 100644 ports/samd/boards/samd51j19a.ld delete mode 100644 ports/samd/boards/samd51p19a.ld rename ports/samd/boards/{samd51g19a.ld => samd51x19a.ld} (100%) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk index d29e2b1097b1f..781faa24106dd 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/mpconfigboard.mk @@ -1,6 +1,6 @@ MCU_SERIES = SAMD51 CMSIS_MCU = SAMD51J19A -LD_FILES = boards/samd51j19a.ld sections.ld +LD_FILES = boards/samd51x19a.ld sections.ld TEXT0 = 0x4000 # The ?='s allow overriding in mpconfigboard.mk. diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.mk b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.mk index 6b0192c77b9ea..da3e47589ad05 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.mk +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/mpconfigboard.mk @@ -1,6 +1,6 @@ MCU_SERIES = SAMD51 CMSIS_MCU = SAMD51G19A -LD_FILES = boards/samd51g19a.ld sections.ld +LD_FILES = boards/samd51x19a.ld sections.ld TEXT0 = 0x4000 # The ?='s allow overriding in mpconfigboard.mk. diff --git a/ports/samd/boards/MINISAM_M4/mpconfigboard.mk b/ports/samd/boards/MINISAM_M4/mpconfigboard.mk index 6ed0ff552b013..73afb0210446f 100644 --- a/ports/samd/boards/MINISAM_M4/mpconfigboard.mk +++ b/ports/samd/boards/MINISAM_M4/mpconfigboard.mk @@ -1,7 +1,7 @@ # https://www.minifigboards.com/mini-sam-m4/mini-sam-m4-hardware/ MCU_SERIES = SAMD51 CMSIS_MCU = SAMD51G19A -LD_FILES = boards/samd51g19a.ld sections.ld +LD_FILES = boards/samd51x19a.ld sections.ld TEXT0 = 0x4000 # The ?='s allow overriding in mpconfigboard.mk. diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.mk b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.mk index 90fb7f2dd076a..c2c4da6152738 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.mk +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/mpconfigboard.mk @@ -1,6 +1,6 @@ MCU_SERIES = SAMD51 CMSIS_MCU = SAMD51P19A -LD_FILES = boards/samd51p19a.ld sections.ld +LD_FILES = boards/samd51x19a.ld sections.ld TEXT0 = 0x4000 # The ?='s allow overriding in mpconfigboard.mk. diff --git a/ports/samd/boards/samd51j19a.ld b/ports/samd/boards/samd51j19a.ld deleted file mode 100644 index cd03320ba43d7..0000000000000 --- a/ports/samd/boards/samd51j19a.ld +++ /dev/null @@ -1,20 +0,0 @@ -/* - GNU linker script for SAMD51 -*/ - -/* Specify the memory areas */ -MEMORY -{ - FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 512K - 16K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K -} - -/* Top end of the stack, with room for double-tap variable */ -_estack = ORIGIN(RAM) + LENGTH(RAM) - 8; -_sstack = _estack - 16K; - -_oflash_fs = ORIGIN(FLASH) + 384K - 16K; -_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; - -_sheap = _ebss; -_eheap = _sstack; diff --git a/ports/samd/boards/samd51p19a.ld b/ports/samd/boards/samd51p19a.ld deleted file mode 100644 index cd03320ba43d7..0000000000000 --- a/ports/samd/boards/samd51p19a.ld +++ /dev/null @@ -1,20 +0,0 @@ -/* - GNU linker script for SAMD51 -*/ - -/* Specify the memory areas */ -MEMORY -{ - FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 512K - 16K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K -} - -/* Top end of the stack, with room for double-tap variable */ -_estack = ORIGIN(RAM) + LENGTH(RAM) - 8; -_sstack = _estack - 16K; - -_oflash_fs = ORIGIN(FLASH) + 384K - 16K; -_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; - -_sheap = _ebss; -_eheap = _sstack; diff --git a/ports/samd/boards/samd51g19a.ld b/ports/samd/boards/samd51x19a.ld similarity index 100% rename from ports/samd/boards/samd51g19a.ld rename to ports/samd/boards/samd51x19a.ld From 4cf527eb05d9b9a93a92ec427e719bf81a8de305 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 15 Jul 2022 22:44:23 +0200 Subject: [PATCH 2724/3533] samd/main: Initialize readline on start up. Somehow that was forgotten. --- ports/samd/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/samd/main.c b/ports/samd/main.c index 889af4a1e4736..0f24e2382ee89 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -29,6 +29,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/stackctrl.h" +#include "shared/readline/readline.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" @@ -48,6 +49,9 @@ void samd_main(void) { gc_init(&_sheap, &_eheap); mp_init(); + // Initialise sub-systems. + readline_init0(); + // Execute _boot.py to set up the filesystem. pyexec_frozen_module("_boot.py"); From 972212907ddc7f611f41d7e267ceca396f1bb4ba Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 10 Jul 2022 11:55:56 +0200 Subject: [PATCH 2725/3533] samd/mcu: Use lf2s for SAMD51 and lfs1 for SAMD21. Using lfs1 gives a smaller code, but lfs2 has more features. --- ports/samd/mcu/samd21/mpconfigmcu.h | 1 + ports/samd/mcu/samd51/mpconfigmcu.mk | 2 ++ ports/samd/modules/_boot.py | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index 7ee23b59b1598..a84b31276b428 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -6,6 +6,7 @@ // MicroPython emitters #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_BUILTINS_COMPLEX (0) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk index 0aedbbe28a373..e83e8911ddbff 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.mk +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -1,3 +1,5 @@ +MICROPY_VFS_LFS2 ?= 1 + SRC_S += shared/runtime/gchelper_m3.s SRC_MOD += $(addprefix lib/libm/,\ diff --git a/ports/samd/modules/_boot.py b/ports/samd/modules/_boot.py index 84b02480b7456..e183125f2e4bf 100644 --- a/ports/samd/modules/_boot.py +++ b/ports/samd/modules/_boot.py @@ -6,11 +6,13 @@ bdev = samd.Flash() # Try to mount the filesystem, and format the flash if it doesn't exist. +fs_type = uos.VfsLfs2 if hasattr(uos, "VfsLfs2") else uos.VfsLfs1 + try: - vfs = uos.VfsLfs1(bdev) + vfs = fs_type(bdev) except: - uos.VfsLfs1.mkfs(bdev) - vfs = uos.VfsLfs1(bdev) + fs_type.mkfs(bdev) + vfs = fs_type(bdev) uos.mount(vfs, "/") gc.collect() From 366c801b3553191241e329c2f6d7f718f49af105 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 4 Oct 2022 09:17:29 +0200 Subject: [PATCH 2726/3533] samd/machine_pin: Change the printing of Pin and LED objects. It now prints lines like: Pin("D9", mode=IN, pull=PULL_UP, GPIO=PA07) or LED("LED") showing for consistency the names as given in pins.csv. For pins, the GPIO numer is printed as well for a reference. --- ports/samd/boards/make-pins.py | 2 +- ports/samd/machine_led.c | 2 +- ports/samd/machine_pin.c | 14 +++++++++++++- ports/samd/mphalport.h | 12 +++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ports/samd/boards/make-pins.py b/ports/samd/boards/make-pins.py index 2b62ba7c59593..679e2c9d192cb 100644 --- a/ports/samd/boards/make-pins.py +++ b/ports/samd/boards/make-pins.py @@ -68,7 +68,7 @@ def print_pins(self, pins_filename): if self.board_leds: pins_file.write("\nconst machine_led_obj_t machine_led_obj[] = {\n") for pin in self.board_leds: - pins_file.write(" {{&machine_pin_type}, ") + pins_file.write(" {{&machine_led_type}, ") pins_file.write(pin[0] + ', "' + pin[1]) pins_file.write('"},\n') pins_file.write("};\n") diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 48ab869199ade..54c2cb18bc86f 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -39,7 +39,7 @@ extern mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_led_obj_t *self = self_in; - mp_printf(print, "LED(%u)", self->id); + mp_printf(print, "LED(\"%s\")", self->name); } // constructor(id, ...) diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index e900174a9150f..fc72c8f7569fb 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -64,7 +64,19 @@ uint32_t machine_pin_open_drain_mask[4]; STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pin_obj_t *self = self_in; - mp_printf(print, "GPIO P%c%02u", "ABCD"[self->id / 32], self->id % 32); + char *mode_str; + char *pull_str[] = {"PULL_OFF", "PULL_UP", "PULL_DOWN"}; + if (GPIO_IS_OPEN_DRAIN(self->id)) { + mode_str = "OPEN_DRAIN"; + } else { + mode_str = (mp_hal_get_pin_direction(self->id) == GPIO_DIRECTION_OUT) ? "OUT" : "IN"; + } + + mp_printf(print, "Pin(\"%s\", mode=%s, pull=%s, GPIO=P%c%02u)", + self->name, + mode_str, + pull_str[mp_hal_get_pull_mode(self->id)], + "ABCD"[self->id / 32], self->id % 32); } STATIC void pin_validate_drive(bool strength) { diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index ced0d642b4ed4..6f4f838cfba2f 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -126,7 +126,17 @@ static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { } static inline unsigned int mp_hal_get_pin_direction(mp_hal_pin_obj_t pin) { - return (PORT->Group[pin / 32].DIR.reg & (1 << (pin % 32))) >> (pin % 32); + return (PORT->Group[pin / 32].DIR.reg & (1 << (pin % 32))) ? + GPIO_DIRECTION_OUT : GPIO_DIRECTION_IN; +} + +static inline unsigned int mp_hal_get_pull_mode(mp_hal_pin_obj_t pin) { + bool pull_en = (PORT->Group[pin / 32].PINCFG[pin % 32].reg & PORT_PINCFG_PULLEN) != 0; + if (pull_en) { + return gpio_get_pin_level(pin) ? GPIO_PULL_UP : GPIO_PULL_DOWN; + } else { + return GPIO_PULL_OFF; + } } static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) { From f6d06b3ce0f476f9edeaeae855876c83b61d9175 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 6 Oct 2022 10:06:56 +1100 Subject: [PATCH 2727/3533] tools/verifygitlog.py: Ignore comment lines in commit messages. The "signed-off" check assumes that the Signed-off-by: line is the last, but there may me many lines of comments after this. Signed-off-by: Jim Mussared --- tools/verifygitlog.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/verifygitlog.py b/tools/verifygitlog.py index f9d98106d6f3c..5d45e4a4cc615 100755 --- a/tools/verifygitlog.py +++ b/tools/verifygitlog.py @@ -102,7 +102,10 @@ def run(args): filename = args[-1] verbose("checking commit message from", filename) with open(args[-1]) as f: - lines = [line.rstrip("\r\n") for line in f] + # Remove comment lines as well as any empty lines at the end. + lines = [line.rstrip("\r\n") for line in f if not line.startswith("#")] + while not lines[-1]: + lines.pop() verify_message_body(lines, err) else: # Normal operation, pass arguments to git log for sha in git_log("%h", *args): From b8982ec5f90a5aa9d802e9d54912dc4e7ba0916a Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 5 Aug 2022 13:55:56 +1000 Subject: [PATCH 2728/3533] tools/verifygitlog.py: Add additional help for subject line issues. This check used to just show the regular expression that failed to match, but the rules are pretty subtle and hard to interpret from the regular expression alone. Add some basic checks for the main things that go wrong: - Missing capitalisation. - Missing full-stop. - Missing path. - Single-word subject. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tools/verifygitlog.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/verifygitlog.py b/tools/verifygitlog.py index 5d45e4a4cc615..c237cb46beef1 100755 --- a/tools/verifygitlog.py +++ b/tools/verifygitlog.py @@ -46,6 +46,23 @@ def git_log(pretty_format, *args): yield line.decode().rstrip("\r\n") +def diagnose_subject_line(subject_line, subject_line_format, err): + err.error("Subject line: " + subject_line) + if not subject_line.endswith("."): + err.error('* should end with "."') + if not re.match(r"^[^!]+: ", subject_line): + err.error('* should start with "path: "') + if re.match(r"^[^!]+: *$", subject_line): + err.error("* should contain a subject after the path.") + m = re.match(r"^[^!]+: ([a-z][^ ]*)", subject_line) + if m: + err.error('* first word of subject ("{}") should be capitalised.'.format(m.group(1))) + if re.match(r"^[^!]+: [^ ]+$", subject_line): + err.error("* subject should contain more than one word.") + err.error("* should match: " + repr(subject_line_format)) + err.error('* Example: "py/runtime: Add support for foo to bar."') + + def verify(sha, err): verbose("verify", sha) err.prefix = "commit " + sha + ": " @@ -75,9 +92,9 @@ def verify_message_body(raw_body, err): very_verbose("subject_line", subject_line) subject_line_format = r"^[^!]+: [A-Z]+.+ .+\.$" if not re.match(subject_line_format, subject_line): - err.error("Subject line should match " + repr(subject_line_format) + ": " + subject_line) + diagnose_subject_line(subject_line, subject_line_format, err) if len(subject_line) >= 73: - err.error("Subject line should be 72 or less characters: " + subject_line) + err.error("Subject line should be 72 or fewer characters: " + subject_line) # Second one divides subject and body. if len(raw_body) > 1 and raw_body[1]: @@ -90,7 +107,7 @@ def verify_message_body(raw_body, err): err.error("Message lines should be 75 or less characters: " + line) if not raw_body[-1].startswith("Signed-off-by: ") or "@" not in raw_body[-1]: - err.warning("Message should be signed-off") + err.warning('Message should be signed-off. Use "git commit -s".') def run(args): From 4fc543c829add6bb13b111b5e12103893bbc0706 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 6 Oct 2022 00:43:28 +1100 Subject: [PATCH 2729/3533] CODECONVENTIONS.md: Update pre-commit instructions. Signed-off-by: Jim Mussared --- CODECONVENTIONS.md | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/CODECONVENTIONS.md b/CODECONVENTIONS.md index 2daea8431f99a..53b202ea371ef 100644 --- a/CODECONVENTIONS.md +++ b/CODECONVENTIONS.md @@ -72,16 +72,27 @@ different formatting, and the configuration file formats are often incompatible. Automatic Pre-Commit Hooks ========================== -To have code formatting and commit message conventions automatically checked -using [pre-commit](https://pre-commit.com/), run the following commands in your -local MicroPython directory: +To have code formatting and commit message conventions automatically checked, +a configuration file is provided for the [pre-commit] +(https://pre-commit.com/) tool. +First install `pre-commit`, either from your system package manager or via +`pip`. When installing `pre-commit` via pip, it is recommended to use a +virtual environment. Other sources, such as Brew are also available, see +[the docs](https://pre-commit.com/index.html#install) for details. + +``` +$ apt install pre-commit # Ubuntu +$ pacman -Sy python-precommit # Arch Linux +$ brew install pre-commit # Brew +$ pip install pre-commit # PyPI ``` -$ pip install pre-commit -$ pre-commit install +Then inside the MicroPython repository, register the git hooks for pre-commit +by running: -$ pre-commit install --hook-type commit-msg +``` +$ pre-commit install --hook-type pre-commit --hook-type commit-msg ``` pre-commit will now automatically run during `git commit` for both code and @@ -91,6 +102,12 @@ The same formatting checks will be run by CI for any Pull Request submitted to MicroPython. Pre-commit allows you to see any failure more quickly, and in many cases will automatically correct it in your local working copy. +To unregister `pre-commit` from your MicroPython repository, run: + +``` +$ pre-commit uninstall --hook-type pre-commit --hook-type commit-msg +``` + Tips: * To skip pre-commit checks on a single commit, use `git commit -n` (for @@ -98,8 +115,6 @@ Tips: * To ignore the pre-commit message format check temporarily, start the commit message subject line with "WIP" (for "Work In Progress"). -(It is also possible to install pre-commit using Brew or other sources, see -[the docs](https://pre-commit.com/index.html#install) for details.) Python code conventions ======================= From c44b3927b8f464e1e01c07b2431e7db251e6d8c0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 7 Oct 2022 11:06:43 +1100 Subject: [PATCH 2730/3533] py/objstr: Add a helper to set mp_obj_str_t data. Signed-off-by: Jim Mussared --- py/objstr.c | 6 ++++++ py/objstr.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index 55e737fffc4fc..8c639e735493b 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2045,6 +2045,12 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u } } +void mp_obj_str_set_data(mp_obj_str_t *str, const byte *data, size_t len) { + str->data = data; + str->len = len; + str->hash = qstr_compute_hash(data, len); +} + // This locals table is used for the following types: str, bytes, bytearray, array.array. // Each type takes a different section (start to end offset) of this table. STATIC const mp_rom_map_elem_t array_bytearray_str_bytes_locals_table[] = { diff --git a/py/objstr.h b/py/objstr.h index 6c6735bf5efa0..72fe1cfef01a6 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -94,6 +94,8 @@ mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, siz mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); +void mp_obj_str_set_data(mp_obj_str_t *str, const byte *data, size_t len); + const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, mp_obj_t index, bool is_slice); const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction); From 0e8dfaf5384c672fc61bc10926688809db2b2ab2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 7 Oct 2022 02:13:58 +1100 Subject: [PATCH 2731/3533] py/modsys: Add support for sys.executable. Only intended to be used on Unix and other "OS" ports. Matches CPython. This should give the absolute path to the executing binary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- py/modsys.c | 10 ++++++++++ py/mpconfig.h | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/py/modsys.c b/py/modsys.c index 56e83029c465b..35af761a08f12 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -115,6 +115,12 @@ STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = { STATIC const MP_DEFINE_STR_OBJ(mp_sys_platform_obj, MICROPY_PY_SYS_PLATFORM); #endif +#ifdef MICROPY_PY_SYS_EXECUTABLE +// executable - the path to the micropython binary +// This object is non-const and is populated at startup in main() +MP_DEFINE_STR_OBJ(mp_sys_executable_obj, ""); +#endif + // exit([retval]): raise SystemExit, with optional argument given to the exception STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { @@ -262,6 +268,10 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) }, #endif + #if MICROPY_PY_SYS_EXECUTABLE + { MP_ROM_QSTR(MP_QSTR_executable), MP_ROM_PTR(&mp_sys_executable_obj) }, + #endif + /* * Extensions to CPython */ diff --git a/py/mpconfig.h b/py/mpconfig.h index 698d264d2e800..99fa87e5c1536 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1372,6 +1372,13 @@ typedef double mp_float_t; #define MICROPY_PY_SYS_EXC_INFO (0) #endif +// Whether to provide "sys.executable", which is the absolute path to the +// micropython binary +// Intended for use on the "OS" ports (e.g. Unix) +#ifndef MICROPY_PY_SYS_EXECUTABLE +#define MICROPY_PY_SYS_EXECUTABLE (0) +#endif + // Whether to provide "sys.exit" function #ifndef MICROPY_PY_SYS_EXIT #define MICROPY_PY_SYS_EXIT (1) From 8e912a501a926f59f330f8a71c2ac1bd4af88a9e Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 7 Oct 2022 02:13:58 +1100 Subject: [PATCH 2732/3533] unix: Enable sys.executable. Gives the absolute path to the unix micropython binary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared Signed-off-by: Damien George --- ports/unix/main.c | 16 ++++++++++++++++ ports/unix/mpconfigport.h | 3 +++ tests/unix/extra_coverage.py.exp | 9 +++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index 44823ee174776..7920db02f29ae 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -43,6 +43,7 @@ #include "py/builtin.h" #include "py/repl.h" #include "py/gc.h" +#include "py/objstr.h" #include "py/stackctrl.h" #include "py/mphal.h" #include "py/mpthread.h" @@ -435,6 +436,17 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) { } } +#if MICROPY_PY_SYS_EXECUTABLE +extern mp_obj_str_t mp_sys_executable_obj; +STATIC char executable_path[MICROPY_ALLOC_PATH_MAX]; + +STATIC void sys_set_excecutable(char *argv0) { + if (realpath(argv0, executable_path)) { + mp_obj_str_set_data(&mp_sys_executable_obj, (byte *)executable_path, strlen(executable_path)); + } +} +#endif + #ifdef _WIN32 #define PATHLIST_SEP_CHAR ';' #else @@ -598,6 +610,10 @@ MP_NOINLINE int main_(int argc, char **argv) { printf(" peak %d\n", m_get_peak_bytes_allocated()); */ + #if MICROPY_PY_SYS_EXECUTABLE + sys_set_excecutable(argv[0]); + #endif + const int NOTHING_EXECUTED = -2; int ret = NOTHING_EXECUTED; bool inspect = false; diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 08ddd21f63ba1..a67d11b9e7e42 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -154,6 +154,9 @@ typedef long mp_off_t; // Don't default sys.argv because we do that in main. #define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0) +// Enable sys.executable. +#define MICROPY_PY_SYS_EXECUTABLE (1) + #define MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128) // Bare-metal ports don't have stderr. Printing debug to stderr may give tests diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 29411545569d3..2f5409b85b45f 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -62,10 +62,11 @@ ime utime utimeq argv atexit byteorder exc_info -exit getsizeof implementation maxsize -modules path platform print_exception -ps1 ps2 stderr stdin -stdout tracebacklimit version version_info +executable exit getsizeof implementation +maxsize modules path platform +print_exception ps1 ps2 +stderr stdin stdout tracebacklimit +version version_info ementation # attrtuple (start=1, stop=2, step=3) From 17f2783e4aa4534823e2dc33e77389ad498f2d24 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:08:53 +1100 Subject: [PATCH 2733/3533] all: Use += rather than = everywhere for CFLAGS/LDFLAGS/LIBS. This avoids a surprise where an = can cancel out an earlier +=. Signed-off-by: Jim Mussared --- docs/develop/porting.rst | 4 ++-- examples/embedding/Makefile | 4 ++-- examples/embedding/Makefile.upylib | 4 ++-- ports/cc3200/Makefile | 4 ++-- ports/esp8266/Makefile | 6 +++--- ports/mimxrt/Makefile | 2 +- ports/minimal/Makefile | 8 ++++---- ports/nrf/Makefile | 2 +- ports/pic16bit/Makefile | 6 +++--- ports/powerpc/Makefile | 4 ++-- ports/renesas-ra/Makefile | 2 +- ports/samd/Makefile | 6 +++--- ports/stm32/Makefile | 2 +- ports/stm32/mboot/Makefile | 6 +++--- ports/teensy/Makefile | 4 ++-- ports/windows/Makefile | 4 ++-- 16 files changed, 34 insertions(+), 34 deletions(-) diff --git a/docs/develop/porting.rst b/docs/develop/porting.rst index 0511f5d276d80..74974a39e1b4d 100644 --- a/docs/develop/porting.rst +++ b/docs/develop/porting.rst @@ -98,8 +98,8 @@ We also need a Makefile at this point for the port: include $(TOP)/extmod/extmod.mk # Set CFLAGS and libraries. - CFLAGS = -I. -I$(BUILD) -I$(TOP) - LIBS = -lm + CFLAGS += -I. -I$(BUILD) -I$(TOP) + LIBS += -lm # Define the required source files. SRC_C = \ diff --git a/examples/embedding/Makefile b/examples/embedding/Makefile index 7de1219b26442..edaa577f57341 100644 --- a/examples/embedding/Makefile +++ b/examples/embedding/Makefile @@ -1,6 +1,6 @@ MPTOP = ../.. -CFLAGS = -std=c99 -I. -I$(MPTOP) -DNO_QSTR -LDFLAGS = -L./build +CFLAGS += -std=c99 -I. -I$(MPTOP) -DNO_QSTR +LDFLAGS += -L./build hello-embed: hello-embed.o -lmicropython diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index a8e2b91d56105..99ce94b7add00 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -20,7 +20,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized -CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) # Some systems (eg MacOS) need -fno-common so that mp_state_ctx is placed in the BSS. CFLAGS += -fno-common @@ -70,7 +70,7 @@ else # Use gcc syntax for map file LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref endif -LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) +LDFLAGS += $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) ifeq ($(MICROPY_FORCE_32BIT),1) # Note: you may need to install i386 versions of dependency packages, diff --git a/ports/cc3200/Makefile b/ports/cc3200/Makefile index b880ad646bb39..6c034c58637e7 100644 --- a/ports/cc3200/Makefile +++ b/ports/cc3200/Makefile @@ -20,7 +20,7 @@ include ../../py/mkenv.mk CROSS_COMPILE ?= arm-none-eabi- CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -march=armv7e-m -mabi=aapcs -mcpu=cortex-m4 -msoft-float -mfloat-abi=soft -fsingle-precision-constant -Wdouble-promotion -CFLAGS = -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os +CFLAGS += -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os CFLAGS += -g -ffunction-sections -fdata-sections -fno-common -fsigned-char -mno-unaligned-access CFLAGS += -Iboards/$(BOARD) CFLAGS += $(CFLAGS_MOD) @@ -28,7 +28,7 @@ CFLAGS += $(CFLAGS_MOD) # Workaround gcc 12.1 bug. CFLAGS += -Wno-array-bounds -LDFLAGS = -Wl,-nostdlib -Wl,--gc-sections -Wl,-Map=$@.map +LDFLAGS += -Wl,-nostdlib -Wl,--gc-sections -Wl,-Map=$@.map FLASH_SIZE_WIPY = 2M FLASH_SIZE_LAUNCHXL = 1M diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index c027f690fb107..6002dc5e5ab57 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -57,12 +57,12 @@ CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ -Wl,-EL -mlongcalls -mtext-section-literals -mforce-l32 \ -DLWIP_OPEN_SRC -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ +CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ $(CFLAGS_XTENSA) $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) -I$(BOARD_DIR) LD_FILES ?= boards/esp8266_2m.ld -LDFLAGS = -nostdlib -T $(LD_FILES) -Map=$(@:.elf=.map) --cref -LIBS = -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 $(LDFLAGS_MOD) +LDFLAGS += -nostdlib -T $(LD_FILES) -Map=$(@:.elf=.map) --cref +LIBS += -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 $(LDFLAGS_MOD) LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 6a18879ac5833..8493c47a715c1 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -393,7 +393,7 @@ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) # Linker Flags # ============================================================================= -LDFLAGS = \ +LDFLAGS += \ --cref \ --gc-sections \ --print-memory-usage \ diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 0e875cc2491a6..2e85bd912e413 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -23,12 +23,12 @@ ifeq ($(CROSS), 1) DFU = $(TOP)/tools/dfu.py PYDFU = $(TOP)/tools/pydfu.py CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -fsingle-precision-constant -Wdouble-promotion -Wfloat-conversion -CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) -LDFLAGS = -nostdlib -T stm32f405.ld -Map=$@.map --cref --gc-sections +CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) +LDFLAGS += -nostdlib -T stm32f405.ld -Map=$@.map --cref --gc-sections else LD = gcc -CFLAGS = $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT) -LDFLAGS = -Wl,-Map=$@.map,--cref -Wl,--gc-sections +CFLAGS += $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT) +LDFLAGS += -Wl,-Map=$@.map,--cref -Wl,--gc-sections endif CSUPEROPT = -Os # save some code space diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index bc295cac808e8..38d0bd7fce72a 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -129,7 +129,7 @@ CFLAGS += -fno-strict-aliasing CFLAGS += -Iboards/$(BOARD) CFLAGS += -DNRF5_HAL_H='<$(MCU_VARIANT)_hal.h>' -LDFLAGS = $(CFLAGS) +LDFLAGS += $(CFLAGS) LDFLAGS += -Xlinker -Map=$(@:.elf=.map) LDFLAGS += -mthumb -mabi=aapcs $(addprefix -T,$(LD_FILES)) -L boards/ diff --git a/ports/pic16bit/Makefile b/ports/pic16bit/Makefile index 392196cbc2609..d2c8df4707e0d 100644 --- a/ports/pic16bit/Makefile +++ b/ports/pic16bit/Makefile @@ -21,7 +21,7 @@ INC += -I$(XC16)/include INC += -I$(XC16)/support/$(PARTFAMILY)/h CFLAGS_PIC16BIT = -mcpu=$(PART) -mlarge-code -CFLAGS = $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT) +CFLAGS += $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT) #Debugging/Optimization ifeq ($(DEBUG), 1) @@ -30,8 +30,8 @@ else CFLAGS += -O1 -DNDEBUG endif -LDFLAGS = --heap=0 -nostdlib -T $(XC16)/support/$(PARTFAMILY)/gld/p$(PART).gld -Map=$@.map --cref -p$(PART) -LIBS = -L$(XC16)/lib -L$(XC16)/lib/$(PARTFAMILY) -lc -lm -lpic30 +LDFLAGS += --heap=0 -nostdlib -T $(XC16)/support/$(PARTFAMILY)/gld/p$(PART).gld -Map=$@.map --cref -p$(PART) +LIBS += -L$(XC16)/lib -L$(XC16)/lib/$(PARTFAMILY) -lc -lm -lpic30 SRC_C = \ main.c \ diff --git a/ports/powerpc/Makefile b/ports/powerpc/Makefile index 0986fd13eba0f..8fc9b11166e63 100644 --- a/ports/powerpc/Makefile +++ b/ports/powerpc/Makefile @@ -21,14 +21,14 @@ INC += -I. INC += -I$(TOP) INC += -I$(BUILD) -CFLAGS = $(INC) -g -Wall -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT) +CFLAGS += $(INC) -g -Wall -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT) CFLAGS += -mno-string -mno-multiple -mno-vsx -mno-altivec -nostdlib CFLAGS += -mlittle-endian -mstrict-align -msoft-float CFLAGS += -Os CFLAGS += -fdata-sections -ffunction-sections -fno-stack-protector -ffreestanding CFLAGS += -U_FORTIFY_SOURCE -LDFLAGS = -N -T powerpc.lds -nostdlib +LDFLAGS += -N -T powerpc.lds -nostdlib LIBS = diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index 92d8be0692af3..6c37aeb1e198d 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -139,7 +139,7 @@ CFLAGS += -fsingle-precision-constant endif endif -LDFLAGS = -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Map=$(@:.elf=.map) --cref +LDFLAGS += -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Map=$(@:.elf=.map) --cref LDFLAGS += --defsym=_estack_reserve=8 LIBS += "$(shell $(CC) $(CFLAGS) -print-libgcc-file-name)" diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 5ff00f21f9266..512820088a68a 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -58,15 +58,15 @@ GEN_PINS_HDR = $(BUILD)/pins.h CFLAGS_MCU_SAMD21 = -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion +CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -DMPCONFIG_MCU_H='' -LDFLAGS = -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref +LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref LDFLAGS += $(LDFLAGS_MOD) -LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) +LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Tune for Debugging or Optimization CFLAGS += -g # always include debug info in the ELF diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 4b4a9f0ce3801..903730982ea01 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -99,7 +99,7 @@ CFLAGS += -fsingle-precision-constant endif endif -LDFLAGS = -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Wl,-Map=$(@:.elf=.map) -Wl,--cref +LDFLAGS += -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Wl,-Map=$(@:.elf=.map) -Wl,--cref LDFLAGS += -Wl,--defsym=_estack_reserve=8 LIBS += "$(shell $(CC) $(CFLAGS) -print-libgcc-file-name)" diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 4c903b4ff603e..176092f8c0a5f 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -58,7 +58,7 @@ INC += -I../$(USBDEV_DIR)/core/inc -I../$(USBDEV_DIR)/class/inc # the compiler does not optimise these functions in terms of themselves. CFLAGS_BUILTIN ?= -ffreestanding -fno-builtin -fno-lto -CFLAGS = $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) CFLAGS += -D$(CMSIS_MCU) CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) @@ -75,8 +75,8 @@ CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID) MBOOT_LD_FILES ?= stm32_memory.ld stm32_sections.ld -LDFLAGS = -nostdlib -L . $(addprefix -T,$(MBOOT_LD_FILES)) -Map=$(@:.elf=.map) --cref -LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) +LDFLAGS += -nostdlib -L . $(addprefix -T,$(MBOOT_LD_FILES)) -Map=$(@:.elf=.map) --cref +LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Remove uncalled code from the final image. CFLAGS += -fdata-sections -ffunction-sections diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index d7161fcbbc3b1..52fc812ad4e73 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -75,8 +75,8 @@ INC += -I$(TOP)/ports/stm32 INC += -I$(BUILD) INC += -Icore -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) -LDFLAGS = -nostdlib -T mk20dx256.ld -msoft-float -mfloat-abi=soft +CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) +LDFLAGS += -nostdlib -T mk20dx256.ld -msoft-float -mfloat-abi=soft ifeq ($(USE_ARDUINO_TOOLCHAIN),1) diff --git a/ports/windows/Makefile b/ports/windows/Makefile index 1e793800f804b..64334bc18b278 100644 --- a/ports/windows/Makefile +++ b/ports/windows/Makefile @@ -32,8 +32,8 @@ INC += -I$(BUILD) INC += -I$(VARIANT_DIR) # compiler settings -CFLAGS = $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Werror -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) -LDFLAGS = $(LDFLAGS_MOD) -lm -lbcrypt $(LDFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Werror -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +LDFLAGS += -lm -lbcrypt $(LDFLAGS_EXTRA) # Debugging/Optimization ifdef DEBUG From 67d05ed02b26b6ba2dcc638f2deed937a08417d7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:23:52 +1100 Subject: [PATCH 2734/3533] ports: Make generated pin.c handling more consistent across ports. Signed-off-by: Jim Mussared --- ports/mimxrt/Makefile | 7 +++--- ports/nrf/Makefile | 26 ++++++++++---------- ports/samd/Makefile | 5 +++- ports/stm32/Makefile | 57 +++++++++++++++++++++---------------------- ports/teensy/Makefile | 26 ++++++++++---------- 5 files changed, 61 insertions(+), 60 deletions(-) diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 8493c47a715c1..6095f5fd23359 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -15,7 +15,7 @@ MICROPY_VFS_LFS2 ?= 1 MICROPY_VFS_FAT ?= 1 # qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h +QSTR_DEFS += qstrdefsport.h QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h # Generation scripts @@ -49,7 +49,6 @@ GEN_FLEXRAM_CONFIG_SRC = $(BUILD)/flexram_config.s GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h GEN_PINS_AF_PY = $(BUILD)/pins_af.py GEN_PINS_HDR = $(HEADER_BUILD)/pins.h -GEN_PINS_QSTR = $(BUILD)/pins_qstr.h GEN_PINS_SRC = $(BUILD)/pins_gen.c # ============================================================================= @@ -435,7 +434,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SS:.S=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) -OBJ += $(BUILD)/pins_gen.o +OBJ += $(GEN_PINS_SRC:.c=.o) # Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };" $(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces @@ -486,7 +485,7 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h: $(BOARD_PINS) $(MAKE_PINS) $(AF_FILE) $(PR --iomux $(abspath $(TOP)/$(MCU_DIR)/drivers/fsl_iomuxc.h) \ --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) > $(GEN_PINS_SRC) -$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c +$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) $(call compile_c) include $(TOP)/py/mkrules.mk diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 38d0bd7fce72a..9e0354194dd63 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -45,7 +45,7 @@ endif -include boards/$(BOARD)/modules/boardmodules.mk # qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h +QSTR_DEFS = qstrdefsport.h $(GEN_PINS_QSTR) # MicroPython feature configurations ifeq ($(DEBUG), 0) @@ -106,6 +106,16 @@ endif NRF_DEFINES += -D$(MCU_SUB_VARIANT_UPPER) NRF_DEFINES += -DCONFIG_GPIO_AS_PINRESET +MAKE_PINS = boards/make-pins.py +BOARD_PINS = boards/$(BOARD)/pins.csv +AF_FILE = $(MCU_VARIANT)_af.csv +PREFIX_FILE = boards/$(MCU_VARIANT)_prefix.c +GEN_PINS_SRC = $(BUILD)/pins_gen.c +GEN_PINS_HDR = $(HEADER_BUILD)/pins.h +GEN_PINS_QSTR = $(BUILD)/pins_qstr.h +GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h +GEN_PINS_AF_PY = $(BUILD)/pins_af.py + CFLAGS_CORTEX_M = -mthumb -mabi=aapcs -fsingle-precision-constant -Wdouble-promotion CFLAGS_MCU_m33 = $(CFLAGS_CORTEX_M) -mcpu=cortex-m33 -march=armv8-m.main+dsp -mcmse -mfpu=fpv5-sp-d16 -mfloat-abi=hard @@ -354,7 +364,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SYSTEM_C_SRC:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) -OBJ += $(BUILD)/pins_gen.o +OBJ += $(GEN_PINS_SRC:.c=.o) $(BUILD)/$(OOFATFS_DIR)/ff.o: COPT += -Os $(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os @@ -528,19 +538,9 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qst $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) --af-const $(GEN_PINS_AF_CONST) --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC) -$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c +$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) $(call compile_c) -MAKE_PINS = boards/make-pins.py -BOARD_PINS = boards/$(BOARD)/pins.csv -AF_FILE = $(MCU_VARIANT)_af.csv -PREFIX_FILE = boards/$(MCU_VARIANT)_prefix.c -GEN_PINS_SRC = $(BUILD)/pins_gen.c -GEN_PINS_HDR = $(HEADER_BUILD)/pins.h -GEN_PINS_QSTR = $(BUILD)/pins_qstr.h -GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h -GEN_PINS_AF_PY = $(BUILD)/pins_af.py - ifneq ($(FROZEN_MANIFEST),) CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool CFLAGS += -DMICROPY_MODULE_FROZEN_MPY diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 512820088a68a..735d25bf27cbe 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -106,7 +106,6 @@ SRC_C += \ mphalport.c \ pendsv.c \ pin_af.c \ - $(BUILD)/pins.c \ samd_flash.c \ samd_isr.c \ samd_soc.c \ @@ -170,6 +169,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) +OBJ += $(GEN_PINS_SRC:.c=.o) ifneq ($(FROZEN_MANIFEST),) CFLAGS += -DMICROPY_MODULE_FROZEN_MPY @@ -205,4 +205,7 @@ $(GEN_PINS_SRC) $(GEN_PINS_HDR): $(BOARD_PINS) | $(HEADER_BUILD) $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --pins $(GEN_PINS_SRC) --inc $(GEN_PINS_HDR) +$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) + $(call compile_c) + include $(TOP)/py/mkrules.mk diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 903730982ea01..403b68b240a1e 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -15,7 +15,7 @@ include ../../py/mkenv.mk include $(BOARD_DIR)/mpconfigboard.mk # Files that are generated and needed before the QSTR build. -QSTR_GENERATED_HEADERS = $(BUILD)/pins_qstr.h $(BUILD)/modstm_qstr.h +QSTR_GENERATED_HEADERS = $(GEN_PINS_QSTR) $(GEN_STMCONST_QSTR) # qstr definitions (must come before including py.mk) QSTR_DEFS += qstrdefsport.h $(QSTR_GENERATED_HEADERS) @@ -57,6 +57,31 @@ OPENOCD_CONFIG ?= boards/openocd_stm32f4.cfg include stm32.mk +PLLVALUES = boards/pllvalues.py +MAKE_PINS = boards/make-pins.py +BOARD_PINS = $(BOARD_DIR)/pins.csv +PREFIX_FILE = boards/stm32f4xx_prefix.c +GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c +GEN_PINS_HDR = $(HEADER_BUILD)/pins.h +GEN_PINS_QSTR = $(BUILD)/pins_qstr.h +GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h +GEN_PINS_AF_DEFS = $(HEADER_BUILD)/pins_af_defs.h +GEN_PINS_AF_PY = $(BUILD)/pins_af.py + +INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py +FILE2H = $(TOP)/tools/file2h.py + +USB_IDS_FILE = mpconfigboard_common.h +CDCINF_TEMPLATE = pybcdc.inf_template +GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf +GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h + +GEN_PLLFREQTABLE_HDR = $(HEADER_BUILD)/pllfreqtable.h +GEN_STMCONST_HDR = $(HEADER_BUILD)/modstm_const.h +GEN_STMCONST_QSTR = $(BUILD)/modstm_qstr.h +GEN_STMCONST_MPZ = $(HEADER_BUILD)/modstm_mpz.h +CMSIS_MCU_HDR = $(STM32LIB_CMSIS_ABS)/Include/$(CMSIS_MCU_LOWER).h + # Select the cross compile prefix CROSS_COMPILE ?= arm-none-eabi- @@ -537,8 +562,7 @@ OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) -OBJ += $(BUILD)/pins_$(BOARD).o +OBJ += $(GEN_PINS_SRC:.c=.o) # This file contains performance critical functions so turn up the optimisation # level. It doesn't add much to the code size and improves performance a bit. @@ -689,25 +713,6 @@ $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(BUILD)/firmware.elf: $(OBJ) $(call GENERATE_ELF,$@,$^) -PLLVALUES = boards/pllvalues.py -MAKE_PINS = boards/make-pins.py -BOARD_PINS = $(BOARD_DIR)/pins.csv -PREFIX_FILE = boards/stm32f4xx_prefix.c -GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c -GEN_PINS_HDR = $(HEADER_BUILD)/pins.h -GEN_PINS_QSTR = $(BUILD)/pins_qstr.h -GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h -GEN_PINS_AF_DEFS = $(HEADER_BUILD)/pins_af_defs.h -GEN_PINS_AF_PY = $(BUILD)/pins_af.py - -INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py -FILE2H = $(TOP)/tools/file2h.py - -USB_IDS_FILE = mpconfigboard_common.h -CDCINF_TEMPLATE = pybcdc.inf_template -GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf -GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h - # List of sources for qstr extraction SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SRC_MOD) $(SHARED_SRC_C) $(EXTMOD_SRC_C) # Append any auto-generated sources that are needed by sources listed in @@ -738,15 +743,9 @@ $(BUILD)/%_$(BOARD).c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(HEADER_ --af-defs $(GEN_PINS_AF_DEFS) --af-defs-cmp-strings \ --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC) -$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c +$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) $(call compile_c) -GEN_PLLFREQTABLE_HDR = $(HEADER_BUILD)/pllfreqtable.h -GEN_STMCONST_HDR = $(HEADER_BUILD)/modstm_const.h -GEN_STMCONST_QSTR = $(BUILD)/modstm_qstr.h -GEN_STMCONST_MPZ = $(HEADER_BUILD)/modstm_mpz.h -CMSIS_MCU_HDR = $(STM32LIB_CMSIS_ABS)/Include/$(CMSIS_MCU_LOWER).h - modmachine.c: $(GEN_PLLFREQTABLE_HDR) $(GEN_PLLFREQTABLE_HDR): $(PLLVALUES) | $(HEADER_BUILD) $(ECHO) "GEN $@" diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index 52fc812ad4e73..876ddf1c1da35 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -1,7 +1,7 @@ include ../../py/mkenv.mk # qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h +QSTR_DEFS = qstrdefsport.h $(GEN_PINS_QSTR) # MicroPython feature configurations MICROPY_ROM_TEXT_COMPRESSION ?= 1 @@ -66,6 +66,16 @@ $(info Using toolchain from PATH) CROSS_COMPILE ?= arm-none-eabi- endif +MAKE_PINS = make-pins.py +BOARD_PINS = teensy_pins.csv +AF_FILE = mk20dx256_af.csv +PREFIX_FILE = mk20dx256_prefix.c +GEN_PINS_SRC = $(BUILD)/pins_gen.c +GEN_PINS_HDR = $(HEADER_BUILD)/pins.h +GEN_PINS_QSTR = $(BUILD)/pins_qstr.h +GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h +GEN_PINS_AF_PY = $(BUILD)/pins_af.py + CFLAGS_TEENSY = -DF_CPU=96000000 -DUSB_SERIAL -D__MK20DX256__ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -mfloat-abi=soft -fsingle-precision-constant -Wdouble-promotion $(CFLAGS_TEENSY) @@ -157,7 +167,7 @@ SRC_TEENSY = $(addprefix core/,\ OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(SRC_TEENSY:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) OBJ += $(BUILD)/shared/runtime/gchelper_m3.o -OBJ += $(BUILD)/pins_gen.o +OBJ += $(GEN_PINS_SRC:.c=.o) all: hex hex: $(BUILD)/micropython.hex @@ -195,16 +205,6 @@ $(BUILD)/%.hex: $(BUILD)/%.elf $(ECHO) "HEX $<" $(Q)$(OBJCOPY) -O ihex -R .eeprom "$<" "$@" -MAKE_PINS = make-pins.py -BOARD_PINS = teensy_pins.csv -AF_FILE = mk20dx256_af.csv -PREFIX_FILE = mk20dx256_prefix.c -GEN_PINS_SRC = $(BUILD)/pins_gen.c -GEN_PINS_HDR = $(HEADER_BUILD)/pins.h -GEN_PINS_QSTR = $(BUILD)/pins_qstr.h -GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h -GEN_PINS_AF_PY = $(BUILD)/pins_af.py - # List of sources for qstr extraction SRC_QSTR += $(SRC_C) $(STM_SRC_C) $(SHARED_SRC_C) # Append any auto-generated sources that are needed by sources listed in @@ -224,7 +224,7 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qst $(ECHO) "Create $@" $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) --af-const $(GEN_PINS_AF_CONST) --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC) -$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c +$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) $(call compile_c) $(BUILD)/%.pp: $(BUILD)/%.c From 3cc6decfc4d6b459014940ae01f90d4f1fae0da6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:28:24 +1100 Subject: [PATCH 2735/3533] py/py.mk: Make user-C-module handling self-contained in py.mk. Removes the need for the port to add anything to OBJS or SRC_QSTR. Also makes it possible for user-C-modules to differentiate between code that should be processed for QSTR vs other files (e.g. helpers and libraries). Signed-off-by: Jim Mussared --- docs/develop/cmodules.rst | 14 +++++++++++--- ports/samd/Makefile | 3 ++- ports/stm32/Makefile | 3 ++- py/py.mk | 34 ++++++++++++++++++++++++++++------ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst index 143057f7f1e06..1b3ba04da4af4 100644 --- a/docs/develop/cmodules.rst +++ b/docs/develop/cmodules.rst @@ -49,9 +49,17 @@ A MicroPython user C module is a directory with the following files: in your ``micropython.mk`` to a local make variable, eg ``EXAMPLE_MOD_DIR := $(USERMOD_DIR)`` - Your ``micropython.mk`` must add your modules source files relative to your - expanded copy of ``$(USERMOD_DIR)`` to ``SRC_USERMOD``, eg - ``SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c`` + Your ``micropython.mk`` must add your modules source files to the + ``SRC_USERMOD_C`` or ``SRC_USERMOD_LIB_C`` variables. The former will be + processed for ``MP_QSTR_`` and ``MP_REGISTER_MODULE`` definitions, the latter + will not (e.g. helpers and library code that isn't MicroPython-specific). + These paths should include your expaned copy of ``$(USERMOD_DIR)``, e.g.:: + + SRC_USERMOD_C += $(EXAMPLE_MOD_DIR)/modexample.c + SRC_USERMOD_LIB_C += $(EXAMPLE_MOD_DIR)/utils/algorithm.c + + Similarly, use ``SRC_USERMOD_CXX`` and ``SRC_USERMOD_LIB_CXX`` for C++ + source files. If you have custom compiler options (like ``-I`` to add directories to search for header files), these should be added to ``CFLAGS_USERMOD`` for C code diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 735d25bf27cbe..9386a8dacc5de 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -81,7 +81,8 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -std=c99,$(CFLAGS)) CXXFLAGS += $(CXXFLAGS_MOD) -ifneq ($(SRC_CXX)$(SRC_MOD_CXX),) +# TODO make this common -- shouldn't be using these "private" vars from py.mk +ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),) LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 403b68b240a1e..4e594d19ff6b4 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -157,7 +157,8 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS)) CXXFLAGS += $(CXXFLAGS_MOD) -ifneq ($(SRC_CXX)$(SRC_MOD_CXX),) +# TODO make this common -- shouldn't be using these "private" vars from py.mk +ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),) LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif diff --git a/py/py.mk b/py/py.mk index 8aac460b478a0..ec69ca42d9715 100644 --- a/py/py.mk +++ b/py/py.mk @@ -32,22 +32,44 @@ endif ifneq ($(USER_C_MODULES),) # pre-define USERMOD variables as expanded so that variables are immediate # expanded as they're added to them -SRC_USERMOD := + +# C/C++ files that are included in the QSTR/module build +SRC_USERMOD_C := SRC_USERMOD_CXX := +# Other C/C++ files (e.g. libraries or helpers) +SRC_USERMOD_LIB_C := +SRC_USERMOD_LIB_CXX := +# Optionally set flags CFLAGS_USERMOD := CXXFLAGS_USERMOD := LDFLAGS_USERMOD := + +# Backwards compatibility with older user c modules that set SRC_USERMOD +# added to SRC_USERMOD_C below +SRC_USERMOD := + $(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \ $(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\ $(info Including User C Module from $(USERMOD_DIR))\ $(eval include $(module))\ ) -SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD)) -SRC_MOD_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX)) -CFLAGS_MOD += $(CFLAGS_USERMOD) -CXXFLAGS_MOD += $(CXXFLAGS_USERMOD) -LDFLAGS_MOD += $(LDFLAGS_USERMOD) +SRC_USERMOD_C += $(SRC_USERMOD) + +SRC_USERMOD_PATHFIX_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_C)) +SRC_USERMOD_PATHFIX_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX)) +SRC_USERMOD_PATHFIX_LIB_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_LIB_C)) +SRC_USERMOD_PATHFIX_LIB_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_LIB_CXX)) + +CFLAGS += $(CFLAGS_USERMOD) +CXXFLAGS += $(CXXFLAGS_USERMOD) +LDFLAGS += $(LDFLAGS_USERMOD) + +SRC_QSTR += $(SRC_USERMOD_PATHFIX_C) $(SRC_USERMOD_PATHFIX_CXX) +PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_C:.c=.o)) +PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_CXX:.cpp=.o)) +PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_C:.c=.o)) +PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_CXX:.cpp=.o)) endif # py object files From 065af04a4e86aa5bdbdedb89140241e5d0030ffb Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:38:18 +1100 Subject: [PATCH 2736/3533] unix/Makefile: Don't use _MOD variable names. This conflicts with the triple-usage of these variables for user-C-modules and extmod source. For CFLAGS_MOD, just use CFLAGS directly. For SRC, use SRC_C directly as the relevant files are all guarded by the preprocessor anyway. Signed-off-by: Jim Mussared --- ports/unix/Makefile | 46 ++++++++++++++++++++++----------------------- ports/unix/modjni.c | 10 +++++----- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 709cc79853d57..e234621346fa0 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -110,27 +110,25 @@ ifeq ($(MICROPY_FORCE_32BIT),1) # starting with linux-libc-dev:i386 ifeq ($(MICROPY_PY_FFI),1) ifeq ($(UNAME_S),Linux) -CFLAGS_MOD += -I/usr/include/i686-linux-gnu +CFLAGS += -I/usr/include/i686-linux-gnu endif endif endif ifeq ($(MICROPY_USE_READLINE),1) INC += -I$(TOP)/shared/readline -CFLAGS_MOD += -DMICROPY_USE_READLINE=1 +CFLAGS += -DMICROPY_USE_READLINE=1 SHARED_SRC_C_EXTRA += readline/readline.c endif ifeq ($(MICROPY_PY_TERMIOS),1) -CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1 -SRC_MOD += modtermios.c +CFLAGS += -DMICROPY_PY_TERMIOS=1 endif ifeq ($(MICROPY_PY_SOCKET),1) -CFLAGS_MOD += -DMICROPY_PY_SOCKET=1 -SRC_MOD += modusocket.c +CFLAGS += -DMICROPY_PY_SOCKET=1 endif ifeq ($(MICROPY_PY_THREAD),1) -CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0 -LDFLAGS_MOD += $(LIBPTHREAD) +CFLAGS += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0 +LDFLAGS += $(LIBPTHREAD) endif ifeq ($(MICROPY_PY_USSL),1) @@ -139,7 +137,7 @@ GIT_SUBMODULES += lib/axtls endif ifeq ($(MICROPY_SSL_MBEDTLS),1) GIT_SUBMODULES += lib/mbedtls -CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' +CFLAGS += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' endif endif @@ -163,8 +161,8 @@ MICROPY_BLUETOOTH_BTSTACK ?= 1 endif endif -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 +CFLAGS += -DMICROPY_PY_BLUETOOTH=1 +CFLAGS += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) @@ -192,7 +190,7 @@ else # NimBLE is enabled. GIT_SUBMODULES += lib/mynewt-nimble -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 +CFLAGS += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 include $(TOP)/extmod/nimble/nimble.mk endif @@ -205,31 +203,29 @@ ifeq ($(MICROPY_STANDALONE),1) # Build libffi from source. GIT_SUBMODULES += lib/libffi DEPLIBS += libffi -LIBFFI_CFLAGS_MOD := -I$(shell ls -1d $(BUILD)/lib/libffi/out/lib/libffi-*/include) +LIBFFI_CFLAGS := -I$(shell ls -1d $(BUILD)/lib/libffi/out/lib/libffi-*/include) ifeq ($(MICROPY_FORCE_32BIT),1) - LIBFFI_LDFLAGS_MOD = $(BUILD)/lib/libffi/out/lib32/libffi.a + LIBFFI_LDFLAGS = $(BUILD)/lib/libffi/out/lib32/libffi.a else - LIBFFI_LDFLAGS_MOD = $(BUILD)/lib/libffi/out/lib/libffi.a + LIBFFI_LDFLAGS = $(BUILD)/lib/libffi/out/lib/libffi.a endif else # Use system version of libffi. -LIBFFI_CFLAGS_MOD := $(shell pkg-config --cflags libffi) -LIBFFI_LDFLAGS_MOD := $(shell pkg-config --libs libffi) +LIBFFI_CFLAGS := $(shell pkg-config --cflags libffi) +LIBFFI_LDFLAGS := $(shell pkg-config --libs libffi) endif ifeq ($(UNAME_S),Linux) -LIBFFI_LDFLAGS_MOD += -ldl +LIBFFI_LDFLAGS += -ldl endif -CFLAGS_MOD += $(LIBFFI_CFLAGS_MOD) -DMICROPY_PY_FFI=1 -LDFLAGS_MOD += $(LIBFFI_LDFLAGS_MOD) -SRC_MOD += modffi.c +CFLAGS += $(LIBFFI_CFLAGS) -DMICROPY_PY_FFI=1 +LDFLAGS += $(LIBFFI_LDFLAGS) endif ifeq ($(MICROPY_PY_JNI),1) # Path for 64-bit OpenJDK, should be adjusted for other JDKs -CFLAGS_MOD += -I/usr/lib/jvm/java-7-openjdk-amd64/include -DMICROPY_PY_JNI=1 -SRC_MOD += modjni.c +CFLAGS += -I/usr/lib/jvm/java-7-openjdk-amd64/include -DMICROPY_PY_JNI=1 endif # source files @@ -250,6 +246,10 @@ SRC_C += \ mpbtstackport_usb.c \ mpnimbleport.c \ $(SRC_MOD) \ + modtermios.c \ + modusocket.c \ + modffi.c \ + modjni.c \ $(wildcard $(VARIANT_DIR)/*.c) SHARED_SRC_C += $(addprefix shared/,\ diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index 5988876f888ab..10622f588f8aa 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -24,19 +24,19 @@ * THE SOFTWARE. */ +#include "py/runtime.h" +#include "py/binary.h" + +#if MICROPY_PY_JNI + #include #include #include #include #include -#include "py/runtime.h" -#include "py/binary.h" - #include -#if MICROPY_PY_JNI - #define JJ(call, ...) (*env)->call(env, __VA_ARGS__) #define JJ1(call) (*env)->call(env) #define MATCH(s, static) (!strncmp(s, static, sizeof(static) - 1)) From 87011f63530f55411843622af8b1bc2dbf98a6f7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:47:57 +1100 Subject: [PATCH 2737/3533] extmod/extmod.mk: Make extmod.mk handle GIT_SUBMODULES. This applies to nimble, btstack, axtls, mbedtls, lwip. Rather than having the ports individually manage GIT_SUBMODULES for these components, make extmod.mk append them when the relevant feature is enabled. Signed-off-by: Jim Mussared --- extmod/btstack/btstack.mk | 1 + extmod/extmod.mk | 4 +++- extmod/nimble/nimble.mk | 2 ++ ports/mimxrt/Makefile | 2 +- ports/stm32/Makefile | 4 +--- ports/unix/Makefile | 5 +---- tools/ci.sh | 6 ++++-- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index 7ecc230003175..14b8d08bf1d36 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -2,6 +2,7 @@ ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +GIT_SUBMODULES += lib/btstack MICROPY_BLUETOOTH_BTSTACK_USB ?= 0 BTSTACK_EXTMOD_DIR = extmod/btstack diff --git a/extmod/extmod.mk b/extmod/extmod.mk index ae3e45bd55cb7..bd897199178a2 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -100,6 +100,7 @@ CFLAGS_MOD += -DMICROPY_PY_USSL=1 ifeq ($(MICROPY_SSL_AXTLS),1) CFLAGS_MOD += -DMICROPY_SSL_AXTLS=1 -I$(TOP)/lib/axtls/ssl -I$(TOP)/lib/axtls/crypto -I$(TOP)/extmod/axtls-include AXTLS_DIR = lib/axtls +GIT_SUBMODULES += $(AXTLS_DIR) $(BUILD)/$(AXTLS_DIR)/%.o: CFLAGS += -Wno-all -Wno-unused-parameter -Wno-uninitialized -Wno-sign-compare -Wno-old-style-definition -Dmp_stream_errno=errno $(AXTLS_DEFS_EXTRA) SRC_MOD += $(addprefix $(AXTLS_DIR)/,\ ssl/asn1.c \ @@ -118,7 +119,7 @@ SRC_MOD += $(addprefix $(AXTLS_DIR)/,\ ) else ifeq ($(MICROPY_SSL_MBEDTLS),1) MBEDTLS_DIR = lib/mbedtls -CFLAGS_MOD += -DMICROPY_SSL_MBEDTLS=1 -I$(TOP)/$(MBEDTLS_DIR)/include +GIT_SUBMODULES += $(MBEDTLS_DIR) SRC_MOD += lib/mbedtls_errors/mp_mbedtls_errors.c SRC_MOD += $(addprefix $(MBEDTLS_DIR)/library/,\ aes.c \ @@ -199,6 +200,7 @@ endif # lwip ifeq ($(MICROPY_PY_LWIP),1) +GIT_SUBMODULES += lib/lwip # A port should add an include path where lwipopts.h can be found (eg extmod/lwip-include) LWIP_DIR = lib/lwip/src INC += -I$(TOP)/$(LWIP_DIR)/include diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index 806630074eb87..e825261eeadc7 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -17,6 +17,8 @@ CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=$(MICROPY_BLUETOOTH_NIMBL ifeq ($(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY),0) +GIT_SUBMODULES += lib/mynewt-nimble + # On all ports where we provide the full implementation (i.e. not just # bindings like on ESP32), then we don't need to use the ringbuffer. In this # case, all NimBLE events are run by the MicroPython scheduler. On Unix, the diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 6095f5fd23359..b8b08cb011f3a 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -7,7 +7,7 @@ BOARD_DIR ?= boards/$(BOARD) BUILD ?= build-$(BOARD) PORT ?= /dev/ttyACM0 CROSS_COMPILE ?= arm-none-eabi- -GIT_SUBMODULES += lib/tinyusb lib/nxp_driver lib/lwip lib/mbedtls +GIT_SUBMODULES += lib/tinyusb lib/nxp_driver # MicroPython feature configurations FROZEN_MANIFEST ?= boards/manifest.py diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 4e594d19ff6b4..8b503112aef7f 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -35,7 +35,7 @@ MBOOT_TEXT0_ADDR ?= 0x08000000 include $(TOP)/py/py.mk include $(TOP)/extmod/extmod.mk -GIT_SUBMODULES += lib/libhydrogen lib/lwip lib/mbedtls lib/stm32lib +GIT_SUBMODULES += lib/libhydrogen lib/stm32lib query-variants: $(ECHO) "VARIANTS:" $(BOARD_VARIANTS) @@ -531,14 +531,12 @@ endif endif ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) -GIT_SUBMODULES += lib/mynewt-nimble CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 include $(TOP)/extmod/nimble/nimble.mk SRC_C += mpnimbleport.c endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) -GIT_SUBMODULES += lib/btstack MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 include $(TOP)/extmod/btstack/btstack.mk SRC_C += mpbtstackport.c diff --git a/ports/unix/Makefile b/ports/unix/Makefile index e234621346fa0..facdab2e045dd 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -133,10 +133,9 @@ endif ifeq ($(MICROPY_PY_USSL),1) ifeq ($(MICROPY_SSL_AXTLS),1) -GIT_SUBMODULES += lib/axtls + endif ifeq ($(MICROPY_SSL_MBEDTLS),1) -GIT_SUBMODULES += lib/mbedtls CFLAGS += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' endif endif @@ -182,14 +181,12 @@ endif endif # BTstack is enabled. -GIT_SUBMODULES += lib/btstack include $(TOP)/extmod/btstack/btstack.mk SRC_BTSTACK += lib/btstack/platform/embedded/btstack_run_loop_embedded.c else # NimBLE is enabled. -GIT_SUBMODULES += lib/mynewt-nimble CFLAGS += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 include $(TOP)/extmod/nimble/nimble.mk diff --git a/tools/ci.sh b/tools/ci.sh index 9963f7796cc06..86fd44fdfbae6 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -198,8 +198,9 @@ function ci_mimxrt_setup { function ci_mimxrt_build { make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/mimxrt submodules + make ${MAKEOPTS} -C ports/mimxrt BOARD=MIMXRT1020_EVK submodules make ${MAKEOPTS} -C ports/mimxrt BOARD=MIMXRT1020_EVK + make ${MAKEOPTS} -C ports/mimxrt BOARD=TEENSY40 submodules make ${MAKEOPTS} -C ports/mimxrt BOARD=TEENSY40 } @@ -310,6 +311,7 @@ function ci_stm32_setup { function ci_stm32_pyb_build { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/stm32 MICROPY_PY_NETWORK_WIZNET5K=5200 submodules + make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2 submodules git submodule update --init lib/btstack git submodule update --init lib/mynewt-nimble make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_NETWORK_WIZNET5K=5200 USER_C_MODULES=../../examples/usercmodule @@ -326,7 +328,7 @@ function ci_stm32_pyb_build { function ci_stm32_nucleo_build { make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/stm32 submodules + make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_H743ZI submodules git submodule update --init lib/mynewt-nimble # Test building various MCU families, some with additional options. From d6d87225585a9494093d791c807bce652e4c82d8 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 8 Oct 2022 23:59:08 +1100 Subject: [PATCH 2738/3533] extmod: Make extmod.mk self-contained. This makes it so that all a port needs to do is set the relevant variables and "include extmod.mk" and doesn't need to worry about adding anything to OBJ, CFLAGS, SRC_QSTR, etc. Make all extmod variables (src, flags, etc) private to extmod.mk. Also move common/shared, extmod-related fragments (e.g. wiznet, cyw43, bluetooth) into extmod.mk. Now that SRC_MOD, CFLAGS_MOD, CXXFLAGS_MOD are unused by both extmod.mk (and user-C-modules in a previous commit), remove all uses of them from port makefiles. Signed-off-by: Jim Mussared --- extmod/btstack/btstack.mk | 38 +++-- extmod/extmod.mk | 208 ++++++++++++++++++--------- extmod/network_wiznet5k.c | 5 +- extmod/nimble/nimble.mk | 14 +- mpy-cross/Makefile | 2 +- ports/cc3200/Makefile | 1 - ports/esp8266/Makefile | 13 +- ports/mimxrt/Makefile | 17 +-- ports/minimal/Makefile | 13 +- ports/nrf/Makefile | 11 +- ports/renesas-ra/Makefile | 18 +-- ports/samd/Makefile | 23 ++- ports/samd/mcu/samd51/mpconfigmcu.mk | 2 +- ports/stm32/Makefile | 67 +-------- ports/stm32/mboot/Makefile | 2 +- ports/stm32/mpconfigport.h | 8 ++ ports/teensy/Makefile | 5 +- ports/unix/Makefile | 48 +------ ports/unix/mpconfigport.h | 8 ++ ports/windows/Makefile | 13 +- py/py.mk | 2 +- 21 files changed, 235 insertions(+), 283 deletions(-) diff --git a/extmod/btstack/btstack.mk b/extmod/btstack/btstack.mk index 14b8d08bf1d36..ca95931562cde 100644 --- a/extmod/btstack/btstack.mk +++ b/extmod/btstack/btstack.mk @@ -3,17 +3,19 @@ ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) GIT_SUBMODULES += lib/btstack + MICROPY_BLUETOOTH_BTSTACK_USB ?= 0 +MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 BTSTACK_EXTMOD_DIR = extmod/btstack -EXTMOD_SRC_C += extmod/btstack/modbluetooth_btstack.c +SRC_EXTMOD_C += $(BTSTACK_EXTMOD_DIR)/modbluetooth_btstack.c INC += -I$(TOP)/$(BTSTACK_EXTMOD_DIR) -CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 +CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_BTSTACK=1 +CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 +CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 BTSTACK_DIR = $(TOP)/lib/btstack @@ -28,44 +30,50 @@ INC += -I$(BTSTACK_DIR)/3rd-party/bluedroid/encoder/include INC += -I$(BTSTACK_DIR)/3rd-party/md5 INC += -I$(BTSTACK_DIR)/3rd-party/yxml -SRC_BTSTACK = \ +SRC_BTSTACK_C = \ $(addprefix lib/btstack/src/, $(SRC_FILES)) \ $(addprefix lib/btstack/src/ble/, $(filter-out %_tlv.c, $(SRC_BLE_FILES))) \ ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) - $(error Cannot specifiy both MICROPY_BLUETOOTH_BTSTACK_USB and MICROPY_BLUETOOTH_BTSTACK_H4) + $(error Cannot enable both MICROPY_BLUETOOTH_BTSTACK_USB and MICROPY_BLUETOOTH_BTSTACK_H4) +endif +endif + +ifneq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) +ifneq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) + $(error Must enable one of MICROPY_BLUETOOTH_BTSTACK_USB or MICROPY_BLUETOOTH_BTSTACK_H4) endif endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) -SRC_BTSTACK += \ +SRC_BTSTACK_C += \ lib/btstack/platform/libusb/hci_transport_h2_libusb.c -CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK_USB=1 +CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_BTSTACK_USB=1 -CFLAGS += $(shell pkg-config libusb-1.0 --cflags) -LDFLAGS += $(shell pkg-config libusb-1.0 --libs) +CFLAGS_THIRDPARTY += $(shell pkg-config libusb-1.0 --cflags) +LDFLAGS_THIRDPARTY += $(shell pkg-config libusb-1.0 --libs) endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) -SRC_BTSTACK += \ +SRC_BTSTACK_C += \ lib/btstack/src/hci_transport_h4.c \ lib/btstack/chipset/zephyr/btstack_chipset_zephyr.c -EXTMOD_SRC_C += \ +SRC_BTSTACK_C += \ extmod/btstack/btstack_hci_uart.c \ -CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK_H4=1 +CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_BTSTACK_H4=1 endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK_ENABLE_CLASSIC),1) include $(BTSTACK_DIR)/src/classic/Makefile.inc -SRC_BTSTACK += \ +SRC_BTSTACK_C += \ $(addprefix lib/btstack/src/classic/, $(SRC_CLASSIC_FILES)) endif -LIB_SRC_C += $(SRC_BTSTACK) +SRC_THIRDPARTY_C += $(SRC_BTSTACK_C) # Suppress some warnings. BTSTACK_WARNING_CFLAGS = -Wno-old-style-definition -Wno-unused-variable -Wno-unused-parameter -Wno-implicit-fallthrough diff --git a/extmod/extmod.mk b/extmod/extmod.mk index bd897199178a2..3b10fc857c80e 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -1,55 +1,66 @@ # This makefile fragment adds the source code files for the core extmod modules # and provides rules to build 3rd-party components for extmod modules. -PY_EXTMOD_O_BASENAME = \ - extmod/moduasyncio.o \ - extmod/moductypes.o \ - extmod/modujson.o \ - extmod/moduos.o \ - extmod/modure.o \ - extmod/moduzlib.o \ - extmod/moduheapq.o \ - extmod/modutimeq.o \ - extmod/moduhashlib.o \ - extmod/moducryptolib.o \ - extmod/modubinascii.o \ - extmod/virtpin.o \ - extmod/machine_bitstream.o \ - extmod/machine_mem.o \ - extmod/machine_pinbase.o \ - extmod/machine_signal.o \ - extmod/machine_pulse.o \ - extmod/machine_pwm.o \ - extmod/machine_i2c.o \ - extmod/machine_spi.o \ - extmod/modbluetooth.o \ - extmod/modlwip.o \ - extmod/modussl_axtls.o \ - extmod/modussl_mbedtls.o \ - extmod/moduplatform.o\ - extmod/modurandom.o \ - extmod/moduselect.o \ - extmod/moduwebsocket.o \ - extmod/modwebrepl.o \ - extmod/modframebuf.o \ - extmod/vfs.o \ - extmod/vfs_blockdev.o \ - extmod/vfs_reader.o \ - extmod/vfs_posix.o \ - extmod/vfs_posix_file.o \ - extmod/vfs_fat.o \ - extmod/vfs_fat_diskio.o \ - extmod/vfs_fat_file.o \ - extmod/vfs_lfs.o \ - extmod/utime_mphal.o \ - extmod/uos_dupterm.o \ - shared/libc/abort_.o \ - shared/libc/printf.o \ +SRC_EXTMOD_C += \ + extmod/machine_bitstream.c \ + extmod/machine_i2c.c \ + extmod/machine_mem.c \ + extmod/machine_pinbase.c \ + extmod/machine_pulse.c \ + extmod/machine_pwm.c \ + extmod/machine_signal.c \ + extmod/machine_spi.c \ + extmod/modbluetooth.c \ + extmod/modbtree.c \ + extmod/modframebuf.c \ + extmod/modlwip.c \ + extmod/modnetwork.c \ + extmod/modonewire.c \ + extmod/moduasyncio.c \ + extmod/modubinascii.c \ + extmod/moducryptolib.c \ + extmod/moductypes.c \ + extmod/moduhashlib.c \ + extmod/moduheapq.c \ + extmod/modujson.c \ + extmod/moduos.c \ + extmod/moduplatform.c\ + extmod/modurandom.c \ + extmod/modure.c \ + extmod/moduselect.c \ + extmod/modusocket.c \ + extmod/modussl_axtls.c \ + extmod/modussl_mbedtls.c \ + extmod/modutimeq.c \ + extmod/moduwebsocket.c \ + extmod/moduzlib.c \ + extmod/modwebrepl.c \ + extmod/network_cyw43.c \ + extmod/network_ninaw10.c \ + extmod/network_wiznet5k.c \ + extmod/uos_dupterm.c \ + extmod/utime_mphal.c \ + extmod/vfs.c \ + extmod/vfs_blockdev.c \ + extmod/vfs_fat.c \ + extmod/vfs_fat_diskio.c \ + extmod/vfs_fat_file.c \ + extmod/vfs_lfs.c \ + extmod/vfs_posix.c \ + extmod/vfs_posix_file.c \ + extmod/vfs_reader.c \ + extmod/virtpin.c \ + shared/libc/abort_.c \ + shared/libc/printf.c \ -PY_EXTMOD_O = $(addprefix $(BUILD)/, $(PY_EXTMOD_O_BASENAME)) +SRC_THIRDPARTY_C += \ -PY_O += $(PY_EXTMOD_O) -SRC_QSTR += $(PY_EXTMOD_O_BASENAME:.o=.c) +PY_O += $(addprefix $(BUILD)/, $(SRC_EXTMOD_C:.c=.o)) +PY_O += $(addprefix $(BUILD)/, $(SRC_THIRDPARTY_C:.c=.o)) +SRC_QSTR += $(SRC_EXTMOD_C) + +CFLAGS += $(CFLAGS_EXTMOD) $(CFLAGS_THIRDPARTY) +LDFLAGS += $(LDFLAGS_EXTMOD) $(LDFLAGS_THIRDPARTY) ################################################################################ # VFS FAT FS @@ -57,11 +68,11 @@ SRC_QSTR += $(PY_EXTMOD_O_BASENAME:.o=.c) OOFATFS_DIR = lib/oofatfs # this sets the config file for FatFs -CFLAGS_MOD += -DFFCONF_H=\"$(OOFATFS_DIR)/ffconf.h\" +CFLAGS_THIRDPARTY += -DFFCONF_H=\"$(OOFATFS_DIR)/ffconf.h\" ifeq ($(MICROPY_VFS_FAT),1) -CFLAGS_MOD += -DMICROPY_VFS_FAT=1 -SRC_MOD += $(addprefix $(OOFATFS_DIR)/,\ +CFLAGS_EXTMOD += -DMICROPY_VFS_FAT=1 +SRC_THIRDPARTY_C += $(addprefix $(OOFATFS_DIR)/,\ ff.c \ ffunicode.c \ ) @@ -73,18 +84,18 @@ endif LITTLEFS_DIR = lib/littlefs ifeq ($(MICROPY_VFS_LFS1),1) -CFLAGS_MOD += -DMICROPY_VFS_LFS1=1 -CFLAGS_MOD += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT -SRC_MOD += $(addprefix $(LITTLEFS_DIR)/,\ +CFLAGS_EXTMOD += -DMICROPY_VFS_LFS1=1 +CFLAGS_THIRDPARTY += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT +SRC_THIRDPARTY_C += $(addprefix $(LITTLEFS_DIR)/,\ lfs1.c \ lfs1_util.c \ ) endif ifeq ($(MICROPY_VFS_LFS2),1) -CFLAGS_MOD += -DMICROPY_VFS_LFS2=1 -CFLAGS_MOD += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT -SRC_MOD += $(addprefix $(LITTLEFS_DIR)/,\ +CFLAGS_EXTMOD += -DMICROPY_VFS_LFS2=1 +CFLAGS_THIRDPARTY += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT +SRC_THIRDPARTY_C += $(addprefix $(LITTLEFS_DIR)/,\ lfs2.c \ lfs2_util.c \ ) @@ -96,13 +107,13 @@ endif # ussl ifeq ($(MICROPY_PY_USSL),1) -CFLAGS_MOD += -DMICROPY_PY_USSL=1 +CFLAGS_EXTMOD += -DMICROPY_PY_USSL=1 ifeq ($(MICROPY_SSL_AXTLS),1) -CFLAGS_MOD += -DMICROPY_SSL_AXTLS=1 -I$(TOP)/lib/axtls/ssl -I$(TOP)/lib/axtls/crypto -I$(TOP)/extmod/axtls-include AXTLS_DIR = lib/axtls GIT_SUBMODULES += $(AXTLS_DIR) +CFLAGS_EXTMOD += -DMICROPY_SSL_AXTLS=1 -I$(TOP)/lib/axtls/ssl -I$(TOP)/lib/axtls/crypto -I$(TOP)/extmod/axtls-include $(BUILD)/$(AXTLS_DIR)/%.o: CFLAGS += -Wno-all -Wno-unused-parameter -Wno-uninitialized -Wno-sign-compare -Wno-old-style-definition -Dmp_stream_errno=errno $(AXTLS_DEFS_EXTRA) -SRC_MOD += $(addprefix $(AXTLS_DIR)/,\ +SRC_THIRDPARTY_C += $(addprefix $(AXTLS_DIR)/,\ ssl/asn1.c \ ssl/loader.c \ ssl/tls1.c \ @@ -120,8 +131,9 @@ SRC_MOD += $(addprefix $(AXTLS_DIR)/,\ else ifeq ($(MICROPY_SSL_MBEDTLS),1) MBEDTLS_DIR = lib/mbedtls GIT_SUBMODULES += $(MBEDTLS_DIR) -SRC_MOD += lib/mbedtls_errors/mp_mbedtls_errors.c -SRC_MOD += $(addprefix $(MBEDTLS_DIR)/library/,\ +CFLAGS_EXTMOD += -DMICROPY_SSL_MBEDTLS=1 -I$(TOP)/$(MBEDTLS_DIR)/include +SRC_THIRDPARTY_C += lib/mbedtls_errors/mp_mbedtls_errors.c +SRC_THIRDPARTY_C += $(addprefix $(MBEDTLS_DIR)/library/,\ aes.c \ aesni.c \ arc4.c \ @@ -204,10 +216,10 @@ GIT_SUBMODULES += lib/lwip # A port should add an include path where lwipopts.h can be found (eg extmod/lwip-include) LWIP_DIR = lib/lwip/src INC += -I$(TOP)/$(LWIP_DIR)/include -CFLAGS_MOD += -DMICROPY_PY_LWIP=1 -$(BUILD)/$(LWIP_DIR)/core/ipv4/dhcp.o: CFLAGS_MOD += -Wno-address -SRC_MOD += shared/netutils/netutils.c -SRC_MOD += $(addprefix $(LWIP_DIR)/,\ +CFLAGS_EXTMOD += -DMICROPY_PY_LWIP=1 +$(BUILD)/$(LWIP_DIR)/core/ipv4/dhcp.o: CFLAGS += -Wno-address +SRC_THIRDPARTY_C += shared/netutils/netutils.c +SRC_THIRDPARTY_C += $(addprefix $(LWIP_DIR)/,\ apps/mdns/mdns.c \ core/def.c \ core/dns.c \ @@ -246,8 +258,8 @@ SRC_MOD += $(addprefix $(LWIP_DIR)/,\ netif/ethernet.c \ ) ifeq ($(MICROPY_PY_LWIP_SLIP),1) -CFLAGS_MOD += -DMICROPY_PY_LWIP_SLIP=1 -SRC_MOD += $(LWIP_DIR)/netif/slipif.c +CFLAGS_EXTMOD += -DMICROPY_PY_LWIP_SLIP=1 +SRC_THIRDPARTY_C += $(LWIP_DIR)/netif/slipif.c endif endif @@ -258,8 +270,7 @@ ifeq ($(MICROPY_PY_BTREE),1) BTREE_DIR = lib/berkeley-db-1.xx BTREE_DEFS = -D__DBINTERFACE_PRIVATE=1 -Dmpool_error=printf -Dabort=abort_ "-Dvirt_fd_t=void*" $(BTREE_DEFS_EXTRA) INC += -I$(TOP)/$(BTREE_DIR)/PORT/include -SRC_MOD += extmod/modbtree.c -SRC_MOD += $(addprefix $(BTREE_DIR)/,\ +SRC_THIRDPARTY_C += $(addprefix $(BTREE_DIR)/,\ btree/bt_close.c \ btree/bt_conv.c \ btree/bt_debug.c \ @@ -275,10 +286,67 @@ SRC_MOD += $(addprefix $(BTREE_DIR)/,\ btree/bt_utils.c \ mpool/mpool.c \ ) -CFLAGS_MOD += -DMICROPY_PY_BTREE=1 +CFLAGS_EXTMOD += -DMICROPY_PY_BTREE=1 # we need to suppress certain warnings to get berkeley-db to compile cleanly # and we have separate BTREE_DEFS so the definitions don't interfere with other source code $(BUILD)/$(BTREE_DIR)/%.o: CFLAGS += -Wno-old-style-definition -Wno-sign-compare -Wno-unused-parameter $(BTREE_DEFS) $(BUILD)/extmod/modbtree.o: CFLAGS += $(BTREE_DEFS) endif +################################################################################ +# networking + +ifeq ($(MICROPY_PY_NETWORK_CYW43),1) +CFLAGS_EXTMOD += -DMICROPY_PY_NETWORK_CYW43=1 +DRIVERS_SRC_C += drivers/cyw43/cyw43_ctrl.c drivers/cyw43/cyw43_lwip.c +LIBS += $(TOP)/drivers/cyw43/libcyw43.a +endif + +ifneq ($(MICROPY_PY_NETWORK_WIZNET5K),) +ifneq ($(MICROPY_PY_NETWORK_WIZNET5K),0) +WIZNET5K_DIR=lib/wiznet5k +GIT_SUBMODULES += lib/wiznet5k +INC += -I$(TOP)/$(WIZNET5K_DIR) -I$(TOP)/$(WIZNET5K_DIR)/Ethernet +CFLAGS += -DMICROPY_PY_NETWORK_WIZNET5K=$(MICROPY_PY_NETWORK_WIZNET5K) -D_WIZCHIP_=$(MICROPY_PY_NETWORK_WIZNET5K) +CFLAGS_THIRDPARTY += -DWIZCHIP_PREFIXED_EXPORTS=1 +ifeq ($(MICROPY_PY_LWIP),1) +# When using MACRAW mode (with lwIP), maximum buffer space must be used for the raw socket +CFLAGS_THIRDPARTY += -DWIZCHIP_USE_MAX_BUFFER +endif +SRC_THIRDPARTY_C += $(addprefix $(WIZNET5K_DIR)/,\ + Ethernet/W$(MICROPY_PY_NETWORK_WIZNET5K)/w$(MICROPY_PY_NETWORK_WIZNET5K).c \ + Ethernet/wizchip_conf.c \ + Ethernet/socket.c \ + Internet/DNS/dns.c \ + Internet/DHCP/dhcp.c \ + ) +endif +endif + +################################################################################ +# bluetooth + +ifeq ($(MICROPY_PY_BLUETOOTH),1) +CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH=1 + +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +$(error Cannot enable both NimBLE and BTstack at the same time) +endif +endif + +ifneq ($(MICROPY_BLUETOOTH_NIMBLE),1) +ifneq ($(MICROPY_BLUETOOTH_BTSTACK),1) +$(error Must enable one of MICROPY_BLUETOOTH_NIMBLE or MICROPY_BLUETOOTH_BTSTACK) +endif +endif + +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +include $(TOP)/extmod/nimble/nimble.mk +endif + +ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +include $(TOP)/extmod/btstack/btstack.mk +endif + +endif diff --git a/extmod/network_wiznet5k.c b/extmod/network_wiznet5k.c index af2014508127c..78cbff4ce2dfa 100644 --- a/extmod/network_wiznet5k.c +++ b/extmod/network_wiznet5k.c @@ -33,6 +33,9 @@ #include "py/stream.h" #include "py/mperrno.h" #include "py/mphal.h" + +#if MICROPY_PY_NETWORK_WIZNET5K + #include "shared/netutils/netutils.h" #include "extmod/modnetwork.h" #include "extmod/machine_spi.h" @@ -40,8 +43,6 @@ #include "modmachine.h" #include "drivers/bus/spi.h" -#if MICROPY_PY_NETWORK_WIZNET5K - #include "lib/wiznet5k/Ethernet/wizchip_conf.h" // The WIZNET5K module supports two usage modes: diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index e825261eeadc7..fc1709f0e50ab 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -5,15 +5,15 @@ ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) EXTMOD_DIR = extmod NIMBLE_EXTMOD_DIR = $(EXTMOD_DIR)/nimble -EXTMOD_SRC_C += $(NIMBLE_EXTMOD_DIR)/modbluetooth_nimble.c +SRC_EXTMOD_C += $(NIMBLE_EXTMOD_DIR)/modbluetooth_nimble.c -CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1 +CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_NIMBLE=1 # Use NimBLE from the submodule in lib/mynewt-nimble by default, # allowing a port to use their own system version (e.g. ESP32). MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY ?= 0 -CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=$(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY) +CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=$(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY) ifeq ($(MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY),0) @@ -24,16 +24,16 @@ GIT_SUBMODULES += lib/mynewt-nimble # case, all NimBLE events are run by the MicroPython scheduler. On Unix, the # scheduler is also responsible for polling the UART, whereas on STM32 the # UART is also polled by the RX IRQ. -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 +CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1 # Without the ringbuffer, and with the full implementation, we can also # enable pairing and bonding. This requires both synchronous events and # some customisation of the key store. -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 +CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 NIMBLE_LIB_DIR = lib/mynewt-nimble -LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ +SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ $(addprefix ext/tinycrypt/src/, \ aes_encrypt.c \ cmac_mode.c \ @@ -98,7 +98,7 @@ LIB_SRC_C += $(addprefix $(NIMBLE_LIB_DIR)/, \ ) # nimble/host/store/ram/src/ble_store_ram.c \ -EXTMOD_SRC_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \ +SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \ nimble/nimble_npl_os.c \ hal/hal_uart.c \ ) diff --git a/mpy-cross/Makefile b/mpy-cross/Makefile index 4b6e03df8e844..7a71577e2bb1a 100644 --- a/mpy-cross/Makefile +++ b/mpy-cross/Makefile @@ -19,7 +19,7 @@ INC += -I$(TOP) # compiler settings CWARN = -Wall -Werror CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith -CFLAGS += $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS += $(INC) $(CWARN) -std=gnu99 $(COPT) $(CFLAGS_EXTRA) CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables # Debugging/Optimization diff --git a/ports/cc3200/Makefile b/ports/cc3200/Makefile index 6c034c58637e7..61af0bfd98445 100644 --- a/ports/cc3200/Makefile +++ b/ports/cc3200/Makefile @@ -23,7 +23,6 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -march=armv7e-m -mabi=aapcs -mcpu=co CFLAGS += -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os CFLAGS += -g -ffunction-sections -fdata-sections -fno-common -fsigned-char -mno-unaligned-access CFLAGS += -Iboards/$(BOARD) -CFLAGS += $(CFLAGS_MOD) # Workaround gcc 12.1 bug. CFLAGS += -Wno-array-bounds diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 6002dc5e5ab57..168d1d9f6ab66 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -58,11 +58,11 @@ CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ -DLWIP_OPEN_SRC CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ - $(CFLAGS_XTENSA) $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) -I$(BOARD_DIR) + $(CFLAGS_XTENSA) $(COPT) $(CFLAGS_EXTRA) -I$(BOARD_DIR) LD_FILES ?= boards/esp8266_2m.ld LDFLAGS += -nostdlib -T $(LD_FILES) -Map=$(@:.elf=.map) --cref -LIBS += -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 $(LDFLAGS_MOD) +LIBS += -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc @@ -107,11 +107,6 @@ SRC_C = \ posix_helpers.c \ hspi.c \ $(wildcard $(BOARD_DIR)/*.c) \ - $(SRC_MOD) - -EXTMOD_SRC_C = $(addprefix extmod/,\ - modonewire.c \ - ) LIB_SRC_C = $(addprefix lib/,\ libm/math.c \ @@ -163,17 +158,15 @@ DRIVERS_SRC_C = $(addprefix drivers/,\ SRC_S = \ gchelper.s \ -OBJ = OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) -OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(EXTMOD_SRC_C) $(SHARED_SRC_C) $(DRIVERS_SRC_C) +SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) # Append any auto-generated sources that are needed by sources listed in SRC_QSTR SRC_QSTR_AUTO_DEPS += diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index b8b08cb011f3a..640864e582951 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -150,10 +150,6 @@ SRC_C += \ drivers/bus/softspi.c \ drivers/dht/dht.c \ eth.c \ - extmod/modnetwork.c \ - extmod/modonewire.c \ - extmod/modusocket.c \ - extmod/uos_dupterm.c \ fatfs_port.c \ hal/pwm_backport.c \ led.c \ @@ -257,16 +253,6 @@ SRC_S += shared/runtime/gchelper_m3.s \ # QSTR Sources # ============================================================================= -# All settings for Ethernet support are controller by the value of MICROPY_PY_LWIP -ifeq ($(MICROPY_PY_LWIP),1) -SRC_QSTR += \ - extmod/modlwip.c \ - extmod/modnetwork.c \ - extmod/modusocket.c \ - extmod/moduwebsocket.c \ - network_lan.c -endif - # List of sources for qstr extraction SRC_QSTR += \ extmod/modonewire.c \ @@ -386,7 +372,7 @@ CFLAGS += \ -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' endif -CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(CFLAGS_EXTRA) # ============================================================================= # Linker Flags @@ -433,7 +419,6 @@ OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SS:.S=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) # Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };" diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile index 2e85bd912e413..1a20412e0da5d 100644 --- a/ports/minimal/Makefile +++ b/ports/minimal/Makefile @@ -44,11 +44,6 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -std=c99,$(CFLAGS)) -CXXFLAGS += $(CXXFLAGS_MOD) - -# Flags for user C modules -CFLAGS += $(CFLAGS_MOD) -LDFLAGS += $(LDFLAGS_MOD) LIBS = @@ -65,14 +60,10 @@ ifeq ($(CROSS), 1) SRC_C += shared/libc/string0.c endif -SRC_C += $(SRC_MOD) - -SRC_CXX += $(SRC_MOD_CXX) - SRC_QSTR += shared/readline/readline.c shared/runtime/pyexec.c -SRC_QSTR += $(SRC_MOD) $(SRC_MOD_CXX) -OBJ += $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(PY_CORE_O) +OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) ifeq ($(CROSS), 1) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 9e0354194dd63..4128404da4a80 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -132,9 +132,8 @@ CFLAGS += -ffunction-sections -fdata-sections LDFLAGS += -Wl,--gc-sections endif - CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) -CFLAGS += $(INC) -Wall -Werror -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Werror -ansi -std=c11 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_EXTRA) CFLAGS += -fno-strict-aliasing CFLAGS += -Iboards/$(BOARD) CFLAGS += -DNRF5_HAL_H='<$(MCU_VARIANT)_hal.h>' @@ -357,8 +356,8 @@ SRC_C += \ LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc -OBJ += $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) +OBJ += $(PY_O) +OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) @@ -515,11 +514,11 @@ flash: deploy $(BUILD)/$(OUTPUT_FILENAME).elf: $(OBJ) $(ECHO) "LINK $@" - $(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LDFLAGS_MOD) $(LIBS) + $(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIBS) $(Q)$(SIZE) $@ # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_LIB) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) $(SRC_MOD) +SRC_QSTR += $(SRC_C) $(SRC_LIB) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index 6c37aeb1e198d..2b0358b27076a 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -121,7 +121,7 @@ CFLAGS_MCU_RA4W1 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_RA6M1 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 CFLAGS_MCU_RA6M2 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA) #CFLAGS += -D$(CMSIS_MCU) CFLAGS += $(CFLAGS_MCU_$(CMSIS_MCU)) CFLAGS += $(COPT) @@ -161,8 +161,9 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS)) -CXXFLAGS += $(CXXFLAGS_MOD) -ifneq ($(SRC_CXX)$(SRC_MOD_CXX),) + +# TODO make this common -- shouldn't be using these "private" vars from py.mk +ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),) LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif @@ -279,10 +280,6 @@ ifeq ($(MICROPY_FLOAT_IMPL),double) $(LIBM_O): CFLAGS := $(filter-out -Wdouble-promotion -Wfloat-conversion, $(CFLAGS)) endif -EXTMOD_SRC_C += $(addprefix extmod/,\ - modonewire.c \ - ) - DRIVERS_SRC_C += $(addprefix drivers/,\ bus/softspi.c \ bus/softqspi.c \ @@ -334,9 +331,6 @@ SRC_C += $(addprefix $(BOARD_DIR)/ra_gen/,\ vector_data.c \ ) -SRC_CXX += \ - $(SRC_MOD_CXX) - SRC_O += \ $(STARTUP_FILE) \ $(SYSTEM_FILE) @@ -408,13 +402,11 @@ OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) -OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) OBJ += $(BUILD)/pins_$(BOARD).o # This file contains performance critical functions so turn up the optimisation @@ -514,7 +506,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py FILE2H = $(TOP)/tools/file2h.py # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SRC_MOD) $(SHARED_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) # Making OBJ use an order-only depenedency on the generated pins.h file # has the side effect of making the pins.h file before we actually compile diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 9386a8dacc5de..2c5f6a1ab2210 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -60,11 +60,10 @@ CFLAGS_MCU_SAMD21 = -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ -CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(CFLAGS_EXTRA) CFLAGS += -DMPCONFIG_MCU_H='' LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref -LDFLAGS += $(LDFLAGS_MOD) LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) @@ -80,7 +79,7 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -std=c99,$(CFLAGS)) -CXXFLAGS += $(CXXFLAGS_MOD) + # TODO make this common -- shouldn't be using these "private" vars from py.mk ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),) LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" @@ -114,10 +113,6 @@ SRC_C += \ lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_atomic.c \ lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_flash.c \ lib/asf4/$(MCU_SERIES_LOWER)/hpl/nvmctrl/hpl_nvmctrl.c \ - lib/libm/ef_sqrt.c \ - lib/libm/fmodf.c \ - lib/libm/math.c \ - lib/libm/nearbyintf.c \ lib/tinyusb/src/class/cdc/cdc_device.c \ lib/tinyusb/src/common/tusb_fifo.c \ lib/tinyusb/src/device/usbd.c \ @@ -125,7 +120,6 @@ SRC_C += \ lib/tinyusb/src/portable/microchip/samd/dcd_samd.c \ lib/tinyusb/src/tusb.c \ drivers/bus/softspi.c \ - extmod/uos_dupterm.c \ shared/runtime/mpirq.c \ shared/libc/printf.c \ shared/libc/string0.c \ @@ -138,9 +132,11 @@ SRC_C += \ shared/runtime/sys_stdio_mphal.c \ shared/timeutils/timeutils.c \ -SRC_C += $(SRC_MOD) - -SRC_CXX += $(SRC_MOD_CXX) +LIBM_SRC_C += \ + lib/libm/ef_sqrt.c \ + lib/libm/fmodf.c \ + lib/libm/math.c \ + lib/libm/nearbyintf.c \ # List of sources for qstr extraction SRC_QSTR += \ @@ -159,17 +155,16 @@ SRC_QSTR += \ modsamd.c \ samd_flash.c \ shared/readline/readline.c \ - extmod/uos_dupterm.c \ shared/runtime/mpirq.c \ shared/runtime/sys_stdio_mphal.c \ -SRC_QSTR += $(SRC_MOD) $(SRC_CXX) +SRC_QSTR += $(SRC_CXX) OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) ifneq ($(FROZEN_MANIFEST),) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk index e83e8911ddbff..f8ed02f841d9d 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.mk +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -2,7 +2,7 @@ MICROPY_VFS_LFS2 ?= 1 SRC_S += shared/runtime/gchelper_m3.s -SRC_MOD += $(addprefix lib/libm/,\ +LIBM_SRC_C += $(addprefix lib/libm/,\ acoshf.c \ asinfacosf.c \ asinhf.c \ diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 8b503112aef7f..79bf8b54bf2c9 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -95,7 +95,7 @@ INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc #INC += -I$(USBHOST_DIR) INC += -Ilwip_inc -CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 -nostdlib $(CFLAGS_EXTRA) CFLAGS += -D$(CMSIS_MCU) -DUSE_FULL_LL_DRIVER CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) @@ -156,7 +156,7 @@ endif # Flags for optional C++ source code CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS)) -CXXFLAGS += $(CXXFLAGS_MOD) + # TODO make this common -- shouldn't be using these "private" vars from py.mk ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),) LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" @@ -275,12 +275,6 @@ ifeq ($(MICROPY_FLOAT_IMPL),double) $(LIBM_O): CFLAGS := $(filter-out -Wdouble-promotion -Wfloat-conversion, $(CFLAGS)) endif -EXTMOD_SRC_C += $(addprefix extmod/,\ - modonewire.c \ - modnetwork.c \ - modusocket.c \ - ) - DRIVERS_SRC_C += $(addprefix drivers/,\ bus/softspi.c \ bus/softqspi.c \ @@ -358,11 +352,9 @@ SRC_C += \ servo.c \ dac.c \ adc.c \ + sdio.c \ $(wildcard $(BOARD_DIR)/*.c) -SRC_CXX += \ - $(SRC_MOD_CXX) - SRC_O += \ $(STARTUP_FILE) \ $(SYSTEM_FILE) @@ -482,70 +474,26 @@ USBDEV_SRC_C += $(addprefix $(USBDEV_DIR)/,\ class/src/usbd_msc_scsi.c \ ) -ifeq ($(MICROPY_PY_BLUETOOTH),1) -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 -endif - -ifeq ($(MICROPY_PY_NETWORK_CYW43),1) -CFLAGS_MOD += -DMICROPY_PY_NETWORK_CYW43=1 -SRC_C += sdio.c -EXTMOD_SRC_C += extmod/network_cyw43.c -DRIVERS_SRC_C += drivers/cyw43/cyw43_ctrl.c drivers/cyw43/cyw43_lwip.c -LIBS += $(TOP)/drivers/cyw43/libcyw43.a -endif - -ifneq ($(MICROPY_PY_NETWORK_WIZNET5K),0) -WIZNET5K_DIR=lib/wiznet5k -GIT_SUBMODULES += lib/wiznet5k -INC += -I$(TOP)/$(WIZNET5K_DIR) -I$(TOP)/$(WIZNET5K_DIR)/Ethernet -CFLAGS_MOD += -DMICROPY_PY_NETWORK_WIZNET5K=$(MICROPY_PY_NETWORK_WIZNET5K) -D_WIZCHIP_=$(MICROPY_PY_NETWORK_WIZNET5K) -CFLAGS_MOD += -DWIZCHIP_PREFIXED_EXPORTS=1 -ifeq ($(MICROPY_PY_LWIP),1) -# When using MACRAW mode (with lwIP), maximum buffer space must be used for the raw socket -CFLAGS_MOD += -DWIZCHIP_USE_MAX_BUFFER -endif -SRC_MOD += extmod/network_wiznet5k.c -SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\ - Ethernet/W$(MICROPY_PY_NETWORK_WIZNET5K)/w$(MICROPY_PY_NETWORK_WIZNET5K).c \ - Ethernet/wizchip_conf.c \ - Ethernet/socket.c \ - Internet/DNS/dns.c \ - Internet/DHCP/dhcp.c \ - ) -endif - ifeq ($(MICROPY_SSL_MBEDTLS),1) -CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' -SRC_MOD += mbedtls/mbedtls_port.c +CFLAGS += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' +LIB_SRC_C += mbedtls/mbedtls_port.c endif ifeq ($(MICROPY_PY_BLUETOOTH),1) - SRC_C += mpbthciport.c ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) -ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) -$(error Cannot enable both NimBLE and BTstack at the same time) -endif -endif - -ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) -CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 -include $(TOP)/extmod/nimble/nimble.mk SRC_C += mpnimbleport.c endif ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) -MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 -include $(TOP)/extmod/btstack/btstack.mk SRC_C += mpbtstackport.c +MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 endif ifeq ($(MICROPY_PY_NETWORK_CYW43),1) DRIVERS_SRC_C += drivers/cyw43/cywbt.c endif - endif # SRC_O should be placed first to work around this LTO bug with binutils <2.35: @@ -555,7 +503,6 @@ OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(USBDEV_SRC_C:.c=.o)) @@ -713,7 +660,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(call GENERATE_ELF,$@,$^) # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SRC_MOD) $(SHARED_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR SRC_QSTR_AUTO_DEPS += $(GEN_CDCINF_HEADER) diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 176092f8c0a5f..12ffdbc7e4682 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -58,7 +58,7 @@ INC += -I../$(USBDEV_DIR)/core/inc -I../$(USBDEV_DIR)/class/inc # the compiler does not optimise these functions in terms of themselves. CFLAGS_BUILTIN ?= -ffreestanding -fno-builtin -fno-lto -CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_EXTRA) CFLAGS += -D$(CMSIS_MCU) CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 69b29e8ec3932..34b3a6abad7b1 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -309,6 +309,14 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_PY_BLUETOOTH_HCI_READ_MODE MICROPY_PY_BLUETOOTH_HCI_READ_MODE_BYTE #endif +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1) +#endif + +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (MICROPY_BLUETOOTH_NIMBLE) +#endif + // We need an implementation of the log2 function which is not a macro #define MP_NEED_LOG2 (1) diff --git a/ports/teensy/Makefile b/ports/teensy/Makefile index 876ddf1c1da35..89e5cbce9dcca 100644 --- a/ports/teensy/Makefile +++ b/ports/teensy/Makefile @@ -164,7 +164,10 @@ SRC_TEENSY = $(addprefix core/,\ yield.c \ ) -OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(SRC_TEENSY:.c=.o)) +OBJ += $(PY_O) +OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(STM_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_TEENSY:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) OBJ += $(BUILD)/shared/runtime/gchelper_m3.o OBJ += $(GEN_PINS_SRC:.c=.o) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index facdab2e045dd..360cdd6f8ce90 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -39,7 +39,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) -I$(VARIANT_DIR) $(CFLAGS_EXTRA) +CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(COPT) -I$(VARIANT_DIR) $(CFLAGS_EXTRA) # Debugging/Optimization ifdef DEBUG @@ -142,35 +142,10 @@ endif # If the variant enables it, enable modbluetooth. ifeq ($(MICROPY_PY_BLUETOOTH),1) - -HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1') - -# Only one stack can be enabled. -ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) -ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) -$(error Cannot enable both NimBLE and BTstack at the same time) -endif -endif - -# Default to btstack, but a variant (or make command line) can set NimBLE -# explicitly (which is always via H4 UART). -ifneq ($(MICROPY_BLUETOOTH_NIMBLE),1) -ifneq ($(MICROPY_BLUETOOTH_BTSTACK),1) -MICROPY_BLUETOOTH_BTSTACK ?= 1 -endif -endif - -CFLAGS += -DMICROPY_PY_BLUETOOTH=1 -CFLAGS += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 - ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1') # Figure out which BTstack transport to use. -ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1) -ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1) -$(error Cannot enable BTstack support for USB and H4 UART at the same time) -endif -else ifeq ($(HAVE_LIBUSB),1) # Default to btstack-over-usb. MICROPY_BLUETOOTH_BTSTACK_USB ?= 1 @@ -178,20 +153,9 @@ else # Fallback to HCI controller via a H4 UART (e.g. Zephyr on nRF) over a /dev/tty serial port. MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1 endif -endif - -# BTstack is enabled. -include $(TOP)/extmod/btstack/btstack.mk -SRC_BTSTACK += lib/btstack/platform/embedded/btstack_run_loop_embedded.c - -else - -# NimBLE is enabled. -CFLAGS += -DMICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 -include $(TOP)/extmod/nimble/nimble.mk +SRC_BTSTACK_C += lib/btstack/platform/embedded/btstack_run_loop_embedded.c endif - endif ifeq ($(MICROPY_PY_FFI),1) @@ -242,7 +206,6 @@ SRC_C += \ mpbtstackport_h4.c \ mpbtstackport_usb.c \ mpnimbleport.c \ - $(SRC_MOD) \ modtermios.c \ modusocket.c \ modffi.c \ @@ -256,17 +219,14 @@ SHARED_SRC_C += $(addprefix shared/,\ ) SRC_CXX += \ - $(SRC_MOD_CXX) OBJ = $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR SRC_QSTR_AUTO_DEPS += diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index a67d11b9e7e42..a0b9192bfcf27 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -240,3 +240,11 @@ static inline unsigned long mp_urandom_seed_init(void) { // Configure the implementation of machine.idle(). #include #define MICROPY_UNIX_MACHINE_IDLE sched_yield(); + +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE +#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1) +#endif + +#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS +#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (MICROPY_BLUETOOTH_NIMBLE) +#endif diff --git a/ports/windows/Makefile b/ports/windows/Makefile index 64334bc18b278..e7f0f2ff618b3 100644 --- a/ports/windows/Makefile +++ b/ports/windows/Makefile @@ -32,7 +32,7 @@ INC += -I$(BUILD) INC += -I$(VARIANT_DIR) # compiler settings -CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Werror -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Werror -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(COPT) $(CFLAGS_EXTRA) LDFLAGS += -lm -lbcrypt $(LDFLAGS_EXTRA) # Debugging/Optimization @@ -56,31 +56,26 @@ SRC_C = \ realpath.c \ init.c \ fmode.c \ - $(SRC_MOD) \ $(wildcard $(VARIANT_DIR)/*.c) SHARED_SRC_C += $(addprefix shared/,\ $(SHARED_SRC_C_EXTRA) \ ) -SRC_CXX += \ - $(SRC_MOD_CXX) - OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) ifeq ($(MICROPY_USE_READLINE),1) -CFLAGS_MOD += -DMICROPY_USE_READLINE=1 +CFLAGS += -DMICROPY_USE_READLINE=1 SRC_C += shared/readline/readline.c endif LIB += -lws2_32 # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) $(EXTMOD_SRC_C) +SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR SRC_QSTR_AUTO_DEPS += @@ -89,7 +84,7 @@ ifneq ($(FROZEN_MANIFEST),) CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool -DMICROPY_MODULE_FROZEN_MPY=1 -DMPZ_DIG_SIZE=16 endif -CXXFLAGS += $(filter-out -std=gnu99,$(CFLAGS) $(CXXFLAGS_MOD)) +CXXFLAGS += $(filter-out -std=gnu99,$(CFLAGS)) include $(TOP)/py/mkrules.mk diff --git a/py/py.mk b/py/py.mk index ec69ca42d9715..fde612d80465b 100644 --- a/py/py.mk +++ b/py/py.mk @@ -207,7 +207,7 @@ endif # Sources that may contain qstrings SRC_QSTR_IGNORE = py/nlr% -SRC_QSTR += $(SRC_MOD) $(filter-out $(SRC_QSTR_IGNORE),$(PY_CORE_O_BASENAME:.o=.c)) +SRC_QSTR += $(filter-out $(SRC_QSTR_IGNORE),$(PY_CORE_O_BASENAME:.o=.c)) # Anything that depends on FORCE will be considered out-of-date FORCE: From 43bcfb148b21d0f6dfa18d6d565f2c686dec80af Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sun, 9 Oct 2022 00:05:11 +1100 Subject: [PATCH 2739/3533] mimxrt/Makefile: Split up SRC_C variables. This improves clarity a bit, but also ensures that only the required files are added to SRC_QSTR. Signed-off-by: Jim Mussared --- ports/mimxrt/Makefile | 47 +++++++++++++------------------------------ 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 640864e582951..2119e027ef59f 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -178,6 +178,11 @@ SRC_C += \ pendsv.c \ pin.c \ sdcard.c \ + systick.c \ + ticks.c \ + tusb_port.c \ + +SHARED_SRC_C += \ shared/libc/printf.c \ shared/libc/string0.c \ shared/netutils/dhcpserver.c \ @@ -191,23 +196,17 @@ SRC_C += \ shared/runtime/stdout_helpers.c \ shared/runtime/sys_stdio_mphal.c \ shared/timeutils/timeutils.c \ - systick.c \ - ticks.c \ - tusb_port.c \ - $(SRC_TINYUSB_C) \ - $(SRC_HAL_IMX_C) \ - $(SRC_ETH_C) # Add sources for respective board flash type ifeq ($(MICROPY_HW_FLASH_TYPE),$(filter $(MICROPY_HW_FLASH_TYPE),qspi_nor_flash qspi_hyper_flash)) # Add hal/flexspi_nor_flash.c or hal/flashspi_hyper_flash.c respectively - SRC_C += hal/flexspi_$(subst qspi_,,$(MICROPY_HW_FLASH_TYPE)).c + SRC_HAL_C += hal/flexspi_$(subst qspi_,,$(MICROPY_HW_FLASH_TYPE)).c # # Add custom (board specific) or default configuration ifeq ($(MICROPY_HW_BOARD_FLASH_FILES),1) - SRC_C += $(BOARD_DIR)/$(MICROPY_HW_FLASH_TYPE)_config.c + SRC_HAL_C += $(BOARD_DIR)/$(MICROPY_HW_FLASH_TYPE)_config.c else - SRC_C += hal/$(MICROPY_HW_FLASH_TYPE)_config.c + SRC_HAL_C += hal/$(MICROPY_HW_FLASH_TYPE)_config.c endif else $(error Error: Unknown board flash type $(MICROPY_HW_FLASH_TYPE)) @@ -254,29 +253,7 @@ SRC_S += shared/runtime/gchelper_m3.s \ # ============================================================================= # List of sources for qstr extraction -SRC_QSTR += \ - extmod/modonewire.c \ - extmod/uos_dupterm.c \ - machine_adc.c \ - machine_i2s.c \ - machine_led.c \ - machine_pin.c \ - machine_pwm.c \ - machine_rtc.c \ - machine_sdcard.c \ - machine_spi.c \ - machine_timer.c \ - machine_uart.c \ - machine_wdt.c \ - mimxrt_flash.c \ - modmachine.c \ - modmimxrt.c \ - modutime.c \ - pin.c \ - shared/readline/readline.c \ - shared/runtime/mpirq.c \ - shared/runtime/sys_stdio_mphal.c \ - $(GEN_PINS_SRC) +SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(GEN_PINS_SRC) # ============================================================================= # Compiler Flags @@ -413,12 +390,16 @@ ifeq ($(MICROPY_FLOAT_IMPL),double) $(LIBM_O): CFLAGS := $(filter-out -Wdouble-promotion -Wfloat-conversion, $(CFLAGS)) endif - OBJ += $(PY_O) OBJ += $(LIBM_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SS:.S=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_TINYUSB_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_HAL_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_HAL_IMX_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_ETH_C:.c=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) # Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };" From a2aceb50074f625c0d9b08ab8902bef1557c1ec6 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sun, 9 Oct 2022 00:05:17 +1100 Subject: [PATCH 2740/3533] nrf/Makefile: Split up SRC_C variables. This improves clarity a bit, but also ensures that only the required files are added to SRC_QSTR. Signed-off-by: Jim Mussared --- ports/nrf/Makefile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 4128404da4a80..a80a37e1fb464 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -163,7 +163,7 @@ LIBS = \ ifeq ($(MCU_VARIANT), nrf52) -SRC_LIB += $(addprefix lib/,\ +SRC_LIB_C += $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ libm/nearbyintf.c \ @@ -197,7 +197,7 @@ endif ifeq ($(MCU_VARIANT), nrf91) -SRC_LIB += $(addprefix lib/,\ +SRC_LIB_C += $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ libm/nearbyintf.c \ @@ -236,7 +236,7 @@ include drivers/secureboot/secureboot.mk endif -SRC_LIB += $(addprefix shared/,\ +SRC_SHARED_C += $(addprefix shared/,\ libc/string0.c \ readline/readline.c \ runtime/pyexec.c \ @@ -247,7 +247,7 @@ SRC_LIB += $(addprefix shared/,\ ) ifeq ($(MICROPY_FATFS), 1) -SRC_LIB += $(addprefix lib/,\ +SRC_LIB_C += $(addprefix lib/,\ oofatfs/ff.c \ oofatfs/ffunicode.c \ ) @@ -362,7 +362,8 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SYSTEM_C_SRC:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_LIB_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_C:.c=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) $(BUILD)/$(OOFATFS_DIR)/ff.o: COPT += -Os @@ -518,7 +519,7 @@ $(BUILD)/$(OUTPUT_FILENAME).elf: $(OBJ) $(Q)$(SIZE) $@ # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_LIB) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) +SRC_QSTR += $(SRC_C) $(SRC_SHARED_C) $(DRIVERS_SRC_C) $(SRC_BOARD_MODULES) # Append any auto-generated sources that are needed by sources listed in # SRC_QSTR From b25087fc6ff6460bf464dbd402febc86799068a9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sun, 9 Oct 2022 00:05:26 +1100 Subject: [PATCH 2741/3533] samd/Makefile: Split up SRC_C variables. This improves clarity a bit, but also ensures that only the required files are added to SRC_QSTR. Signed-off-by: Jim Mussared --- ports/samd/Makefile | 66 +++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 2c5f6a1ab2210..2f97b1993e99a 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -110,16 +110,8 @@ SRC_C += \ samd_isr.c \ samd_soc.c \ tusb_port.c \ - lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_atomic.c \ - lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_flash.c \ - lib/asf4/$(MCU_SERIES_LOWER)/hpl/nvmctrl/hpl_nvmctrl.c \ - lib/tinyusb/src/class/cdc/cdc_device.c \ - lib/tinyusb/src/common/tusb_fifo.c \ - lib/tinyusb/src/device/usbd.c \ - lib/tinyusb/src/device/usbd_control.c \ - lib/tinyusb/src/portable/microchip/samd/dcd_samd.c \ - lib/tinyusb/src/tusb.c \ - drivers/bus/softspi.c \ + +SHARED_SRC_C += \ shared/runtime/mpirq.c \ shared/libc/printf.c \ shared/libc/string0.c \ @@ -132,39 +124,43 @@ SRC_C += \ shared/runtime/sys_stdio_mphal.c \ shared/timeutils/timeutils.c \ -LIBM_SRC_C += \ - lib/libm/ef_sqrt.c \ - lib/libm/fmodf.c \ - lib/libm/math.c \ - lib/libm/nearbyintf.c \ +ASF4_SRC_C += $(addprefix lib/asf4/$(MCU_SERIES_LOWER)/,\ + hal/src/hal_atomic.c \ + hal/src/hal_flash.c \ + hpl/nvmctrl/hpl_nvmctrl.c \ + ) + +LIBM_SRC_C += $(addprefix lib/libm/,\ + ef_sqrt.c \ + fmodf.c \ + math.c \ + nearbyintf.c \ + ) + +TINYUSB_SRC_C += $(addprefix lib/tinyusb/src/,\ + class/cdc/cdc_device.c \ + common/tusb_fifo.c \ + device/usbd.c \ + device/usbd_control.c \ + portable/microchip/samd/dcd_samd.c \ + tusb.c \ + ) + +DRIVERS_SRC_C += \ + drivers/bus/softspi.c \ # List of sources for qstr extraction -SRC_QSTR += \ - machine_adc.c \ - machine_dac.c \ - machine_i2c.c \ - machine_led.c \ - machine_pin.c \ - machine_pwm.c \ - machine_spi.c \ - machine_timer.c \ - machine_uart.c \ - machine_wdt.c \ - modutime.c \ - modmachine.c \ - modsamd.c \ - samd_flash.c \ - shared/readline/readline.c \ - shared/runtime/mpirq.c \ - shared/runtime/sys_stdio_mphal.c \ - -SRC_QSTR += $(SRC_CXX) +SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(SRC_CXX) $(GEN_PINS_SRC) OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) +OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(ASF4_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o)) OBJ += $(GEN_PINS_SRC:.c=.o) ifneq ($(FROZEN_MANIFEST),) From 92d911803812100fac109b6108ab005e7bbd4c82 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 7 Oct 2022 21:08:08 +0200 Subject: [PATCH 2742/3533] rp2/fatfs_port: Fix the modification date of files. It was off by 2000 % 128 == 80 years. Addresses issue #9535. --- ports/rp2/fatfs_port.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/fatfs_port.c b/ports/rp2/fatfs_port.c index 3821d6586c13f..9706b6fe7dc70 100644 --- a/ports/rp2/fatfs_port.c +++ b/ports/rp2/fatfs_port.c @@ -30,5 +30,5 @@ MP_WEAK DWORD get_fattime(void) { datetime_t t; rtc_get_datetime(&t); - return ((2000 + t.year - 1980) << 25) | ((t.month) << 21) | ((t.day) << 16) | ((t.hour) << 11) | ((t.min) << 5) | (t.sec / 2); + return ((t.year - 1980) << 25) | ((t.month) << 21) | ((t.day) << 16) | ((t.hour) << 11) | ((t.min) << 5) | (t.sec / 2); } From ab317a0d66005ea88f162994130f61d0d488db93 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 8 Oct 2022 17:32:22 -0500 Subject: [PATCH 2743/3533] py/misc: Remove use of bitfield from vstr_t. Since there is only one flag, we don't need to use a bitfield in vstr_t. Compilers emit extra instructions to access a bitfield, so this should reduce the binary size a small amount. Signed-off-by: David Lechner --- py/misc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/misc.h b/py/misc.h index 7350d2f9b0c08..32061a009ed31 100644 --- a/py/misc.h +++ b/py/misc.h @@ -179,7 +179,7 @@ typedef struct _vstr_t { size_t alloc; size_t len; char *buf; - bool fixed_buf : 1; + bool fixed_buf; } vstr_t; // convenience macro to declare a vstr with a fixed size buffer on the stack From 89b320737652829edbab921e86d7ad3962d86d9e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 Oct 2022 14:36:17 +1100 Subject: [PATCH 2744/3533] unix/modffi: Move header includes inside MICROPY_PY_FFI guard. So ffi.h is not needed if this module is disabled. Signed-off-by: Damien George --- ports/unix/modffi.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 3df748b80d956..bc585f8647bc6 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -25,13 +25,6 @@ * THE SOFTWARE. */ -#include -#include -#include -#include -#include -#include - #include "py/runtime.h" #include "py/binary.h" #include "py/mperrno.h" @@ -40,6 +33,12 @@ #if MICROPY_PY_FFI +#include +#include +#include +#include +#include + /* * modffi uses character codes to encode a value type, based on "struct" * module type codes, with some extensions and overridings. From 815920c87f9bda6b3fb7ec24686154210c9e8774 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 Oct 2022 15:49:52 +1100 Subject: [PATCH 2745/3533] extmod/utime_mphal: Make ticks_add check for overflow of delta. Work done in collaboration with @jimmo. Signed-off-by: Damien George --- extmod/utime_mphal.c | 13 +++++++++++ tests/extmod/ticks_add.py | 42 +++++++++++++++++++++++++++++++++++ tests/extmod/ticks_add.py.exp | 36 ++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 tests/extmod/ticks_add.py create mode 100644 tests/extmod/ticks_add.py.exp diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 3d1cdfd820629..cd91c95530cba 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -95,6 +95,19 @@ STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) { // we assume that first argument come from ticks_xx so is small int mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in); mp_uint_t delta = mp_obj_get_int(delta_in); + + // Check that delta does not overflow the range that ticks_diff can handle. + // This ensures the following: + // - ticks_diff(ticks_add(T, delta), T) == delta + // - ticks_diff(T, ticks_add(T, delta)) == -delta + // The latter requires excluding delta=-TICKS_PERIOD/2. + // + // This unsigned comparison is equivalent to a signed comparison of: + // delta <= TICKS_PERIOD/2 || delta >= TICKS_PERIOD/2 + if (delta + MICROPY_PY_UTIME_TICKS_PERIOD / 2 - 1 >= MICROPY_PY_UTIME_TICKS_PERIOD - 1) { + mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ticks interval overflow")); + } + return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add); diff --git a/tests/extmod/ticks_add.py b/tests/extmod/ticks_add.py new file mode 100644 index 0000000000000..2f1ba6c8109de --- /dev/null +++ b/tests/extmod/ticks_add.py @@ -0,0 +1,42 @@ +try: + from utime import ticks_diff, ticks_add +except ImportError: + print("SKIP") + raise SystemExit + +# Maximum value returned from ticks_add, ticks_ms, etc. +TICKS_MAX = ticks_add(0, -1) +# Maximum value returned from ticks_diff. +TICKS_INTERVAL_MAX = TICKS_MAX // 2 + +# Invariants: +# - ticks_diff(ticks_add(T, delta), T) == delta +# - ticks_diff(T, ticks_add(T, delta)) == -delta + +# Check actual values of ticks_add. +print(ticks_add(20, 12)) +print(ticks_add(20, -12)) + +# Check invariant. +print(ticks_diff(ticks_add(100, 123), 100)) +print(ticks_diff(ticks_add(100, -123), 100)) +print(ticks_diff(100, ticks_add(100, 123))) +print(ticks_diff(100, ticks_add(100, -123))) + +# Check limits. +for T in (0, 10, TICKS_MAX): + for delta in ( + -TICKS_INTERVAL_MAX - 1, + -TICKS_INTERVAL_MAX, + 0, + TICKS_INTERVAL_MAX, + TICKS_INTERVAL_MAX + 1, + ): + try: + print(ticks_diff(ticks_add(T, delta), T) == delta) + except OverflowError: + print("OverflowError") + try: + print(ticks_diff(T, ticks_add(T, delta)) == -delta) + except OverflowError: + print("OverflowError") diff --git a/tests/extmod/ticks_add.py.exp b/tests/extmod/ticks_add.py.exp new file mode 100644 index 0000000000000..60dc6f5afda6b --- /dev/null +++ b/tests/extmod/ticks_add.py.exp @@ -0,0 +1,36 @@ +32 +8 +123 +-123 +-123 +123 +OverflowError +OverflowError +True +True +True +True +True +True +OverflowError +OverflowError +OverflowError +OverflowError +True +True +True +True +True +True +OverflowError +OverflowError +OverflowError +OverflowError +True +True +True +True +True +True +OverflowError +OverflowError From 965a87b53cd9da32a546b03cdd01042058080498 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 Oct 2022 16:10:38 +1100 Subject: [PATCH 2746/3533] tests/extmod: Add test for sleep_ms value that overflows ticks. Addresses #9516. Signed-off-by: Damien George --- tests/extmod/uasyncio_micropython.py | 6 ++++++ tests/extmod/uasyncio_micropython.py.exp | 1 + 2 files changed, 7 insertions(+) diff --git a/tests/extmod/uasyncio_micropython.py b/tests/extmod/uasyncio_micropython.py index 69e5fa3224df7..a6b65bb2a84c0 100644 --- a/tests/extmod/uasyncio_micropython.py +++ b/tests/extmod/uasyncio_micropython.py @@ -22,6 +22,12 @@ async def main(): await uasyncio.sleep_ms(1) print(utime.ticks_diff(utime.ticks_ms(), t0) < 100) + try: + # Sleep 1ms beyond maximum allowed sleep value + await uasyncio.sleep_ms(utime.ticks_add(0, -1) // 2 + 1) + except OverflowError: + print("OverflowError") + # When task finished before the timeout print(await uasyncio.wait_for_ms(task(1, 5), 50)) diff --git a/tests/extmod/uasyncio_micropython.py.exp b/tests/extmod/uasyncio_micropython.py.exp index f5be1dc75a254..4d1c6d681f2f6 100644 --- a/tests/extmod/uasyncio_micropython.py.exp +++ b/tests/extmod/uasyncio_micropython.py.exp @@ -1,4 +1,5 @@ True +OverflowError task start 1 task end 1 2 From af4ba6d1b4558e0ce1c81b8acd975f0bcbe83072 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 19:48:07 +0200 Subject: [PATCH 2747/3533] stm32: Rename machine I2C and SPI types consistently across ports. This renames: - machine_hard_i2c_type -> machine_i2c_type - machine_hard_spi_type -> machine_spi_type --- ports/stm32/machine_i2c.c | 18 +++++++++--------- ports/stm32/machine_spi.c | 14 +++++++------- ports/stm32/modmachine.c | 4 ++-- ports/stm32/modmachine.h | 4 +++- ports/stm32/spi.c | 4 ++-- ports/stm32/spi.h | 2 +- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 9a2d338f54982..7718d1bfe0384 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -49,16 +49,16 @@ typedef struct _machine_hard_i2c_obj_t { STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[MICROPY_HW_MAX_I2C] = { #if defined(MICROPY_HW_I2C1_SCL) - [0] = {{&machine_hard_i2c_type}, I2C1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, + [0] = {{&machine_i2c_type}, I2C1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, #endif #if defined(MICROPY_HW_I2C2_SCL) - [1] = {{&machine_hard_i2c_type}, I2C2, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, + [1] = {{&machine_i2c_type}, I2C2, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, #endif #if defined(MICROPY_HW_I2C3_SCL) - [2] = {{&machine_hard_i2c_type}, I2C3, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, + [2] = {{&machine_i2c_type}, I2C3, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, #endif #if defined(MICROPY_HW_I2C4_SCL) - [3] = {{&machine_hard_i2c_type}, I2C4, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA}, + [3] = {{&machine_i2c_type}, I2C4, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA}, #endif }; @@ -140,16 +140,16 @@ typedef mp_machine_soft_i2c_obj_t machine_hard_i2c_obj_t; STATIC machine_hard_i2c_obj_t machine_hard_i2c_obj[MICROPY_HW_MAX_I2C] = { #if defined(MICROPY_HW_I2C1_SCL) - [0] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, + [0] = {{&machine_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, #endif #if defined(MICROPY_HW_I2C2_SCL) - [1] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, + [1] = {{&machine_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, #endif #if defined(MICROPY_HW_I2C3_SCL) - [2] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, + [2] = {{&machine_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, #endif #if defined(MICROPY_HW_I2C4_SCL) - [3] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA}, + [3] = {{&machine_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA}, #endif }; @@ -237,7 +237,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_hard_i2c_make_new, diff --git a/ports/stm32/machine_spi.c b/ports/stm32/machine_spi.c index 718ae1af5e5a6..7ccff75f1ccf1 100644 --- a/ports/stm32/machine_spi.c +++ b/ports/stm32/machine_spi.c @@ -32,12 +32,12 @@ // Implementation of hard SPI for machine module STATIC const machine_hard_spi_obj_t machine_hard_spi_obj[] = { - {{&machine_hard_spi_type}, &spi_obj[0]}, - {{&machine_hard_spi_type}, &spi_obj[1]}, - {{&machine_hard_spi_type}, &spi_obj[2]}, - {{&machine_hard_spi_type}, &spi_obj[3]}, - {{&machine_hard_spi_type}, &spi_obj[4]}, - {{&machine_hard_spi_type}, &spi_obj[5]}, + {{&machine_spi_type}, &spi_obj[0]}, + {{&machine_spi_type}, &spi_obj[1]}, + {{&machine_spi_type}, &spi_obj[2]}, + {{&machine_spi_type}, &spi_obj[3]}, + {{&machine_spi_type}, &spi_obj[4]}, + {{&machine_spi_type}, &spi_obj[5]}, }; STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -136,7 +136,7 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_spi_type, + machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, make_new, machine_hard_spi_make_new, diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index e1796d1cf025a..06fdd0e2c1bd3 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -424,14 +424,14 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, #if MICROPY_PY_MACHINE_I2C #if MICROPY_HW_ENABLE_HW_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, #else { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_SPI - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, #endif #if MICROPY_HW_ENABLE_I2S diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index 9fa7851582e1a..0c776280b416c 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -30,8 +30,10 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_timer_type; -extern const mp_obj_type_t machine_hard_i2c_type; +extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_i2s_type; +extern const mp_obj_type_t machine_spi_type; +extern const mp_obj_type_t machine_timer_type; void machine_init(void); void machine_deinit(void); diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 9c031cad2e28d..e21ebcd02b9da 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -678,7 +678,7 @@ const spi_t *spi_from_mp_obj(mp_obj_t o) { if (mp_obj_is_type(o, &pyb_spi_type)) { pyb_spi_obj_t *self = MP_OBJ_TO_PTR(o); return self->spi; - } else if (mp_obj_is_type(o, &machine_hard_spi_type)) { + } else if (mp_obj_is_type(o, &machine_spi_type)) { machine_hard_spi_obj_t *self = MP_OBJ_TO_PTR(o); return self->spi; } else { @@ -687,7 +687,7 @@ const spi_t *spi_from_mp_obj(mp_obj_t o) { } mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) { - if (mp_obj_is_type(o, &machine_hard_spi_type)) { + if (mp_obj_is_type(o, &machine_spi_type)) { return MP_OBJ_TO_PTR(o); } #if MICROPY_PY_MACHINE_SOFTSPI diff --git a/ports/stm32/spi.h b/ports/stm32/spi.h index 17f1bf6c4ab90..ef3d718cddfd4 100644 --- a/ports/stm32/spi.h +++ b/ports/stm32/spi.h @@ -65,7 +65,7 @@ extern const spi_t spi_obj[6]; extern const mp_spi_proto_t spi_proto; extern const mp_obj_type_t pyb_spi_type; -extern const mp_obj_type_t machine_hard_spi_type; +extern const mp_obj_type_t machine_spi_type; // A transfer of "len" bytes should take len*8*1000/baudrate milliseconds. // To simplify the calculation we assume the baudrate is never less than 8kHz From 427670c21047585fb9295ddf35cdbdaa8265fbe1 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:20:03 +0200 Subject: [PATCH 2748/3533] esp32: Rename machine I2C and SPI types consistently across ports. This renames: - machine_hw_i2c_type -> machine_i2c_type - machine_hw_spi_type -> machine_spi_type --- ports/esp32/machine_hw_spi.c | 4 ++-- ports/esp32/machine_i2c.c | 4 ++-- ports/esp32/modmachine.c | 4 ++-- ports/esp32/modmachine.h | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 647874e17f648..35ecc397b339b 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -490,7 +490,7 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_ self = &machine_hw_spi_obj[1]; default_pins = &machine_hw_spi_default_pins[1]; } - self->base.type = &machine_hw_spi_type; + self->base.type = &machine_spi_type; int8_t sck, mosi, miso; @@ -540,7 +540,7 @@ STATIC const mp_machine_spi_p_t machine_hw_spi_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hw_spi_type, + machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, make_new, machine_hw_spi_make_new, diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 9244343dcf618..17e98ffc5bb4b 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -161,7 +161,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_ bool first_init = false; if (self->base.type == NULL) { // Created for the first time, set default pins - self->base.type = &machine_hw_i2c_type; + self->base.type = &machine_i2c_type; self->port = i2c_id; if (self->port == I2C_NUM_0) { self->scl = MICROPY_HW_I2C0_SCL; @@ -193,7 +193,7 @@ STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hw_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_hw_i2c_make_new, diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index a70f2fbedb59c..e24afd0403502 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -315,14 +315,14 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if MICROPY_PY_MACHINE_DAC { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, #endif - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #if MICROPY_PY_MACHINE_I2S { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, #endif { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hw_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h index 4d2ab9020d16f..138a89e9c33d5 100644 --- a/ports/esp32/modmachine.h +++ b/ports/esp32/modmachine.h @@ -16,8 +16,8 @@ extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_adcblock_type; extern const mp_obj_type_t machine_dac_type; -extern const mp_obj_type_t machine_hw_i2c_type; -extern const mp_obj_type_t machine_hw_spi_type; +extern const mp_obj_type_t machine_i2c_type; +extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_i2s_type; extern const mp_obj_type_t machine_uart_type; extern const mp_obj_type_t machine_rtc_type; From 22ad45fda649b754916883f246139e35ff20e126 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:21:51 +0200 Subject: [PATCH 2749/3533] rp2: Rename machine I2C type consistently across ports. This renames: - machine_hw_i2c_type -> machine_i2c_type --- ports/rp2/machine_i2c.c | 6 +++--- ports/rp2/modmachine.c | 2 +- ports/rp2/modmachine.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index 00dc36a4e0cb2..85d12c7713684 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -68,8 +68,8 @@ typedef struct _machine_i2c_obj_t { } machine_i2c_obj_t; STATIC machine_i2c_obj_t machine_i2c_obj[] = { - {{&machine_hw_i2c_type}, i2c0, 0, MICROPY_HW_I2C0_SCL, MICROPY_HW_I2C0_SDA, 0}, - {{&machine_hw_i2c_type}, i2c1, 1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA, 0}, + {{&machine_i2c_type}, i2c0, 0, MICROPY_HW_I2C0_SCL, MICROPY_HW_I2C0_SDA, 0}, + {{&machine_i2c_type}, i2c1, 1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA, 0}, }; STATIC void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -177,7 +177,7 @@ STATIC const mp_machine_i2c_p_t machine_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hw_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_i2c_make_new, diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 3c8922c417210..8058189a92b63 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -239,7 +239,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, diff --git a/ports/rp2/modmachine.h b/ports/rp2/modmachine.h index 503c1ca86c8ab..6ea4def91d974 100644 --- a/ports/rp2/modmachine.h +++ b/ports/rp2/modmachine.h @@ -4,7 +4,7 @@ #include "py/obj.h" extern const mp_obj_type_t machine_adc_type; -extern const mp_obj_type_t machine_hw_i2c_type; +extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_i2s_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_rtc_type; From e275a58ac12c72d8d8e99f8c558f6e4a1f6637c1 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:22:57 +0200 Subject: [PATCH 2750/3533] samd: Rename machine I2C type consistently across ports. This renames: - machine_hw_i2c_type -> machine_i2c_type --- ports/samd/machine_i2c.c | 4 ++-- ports/samd/modmachine.c | 2 +- ports/samd/modmachine.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/samd/machine_i2c.c b/ports/samd/machine_i2c.c index 25dfa99340ffe..0a8f5b94db081 100644 --- a/ports/samd/machine_i2c.c +++ b/ports/samd/machine_i2c.c @@ -144,7 +144,7 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } // Get the peripheral object. - machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_hw_i2c_type); + machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_i2c_type); self->id = id; self->instance = sercom_instance[self->id]; @@ -260,7 +260,7 @@ STATIC const mp_machine_i2c_p_t machine_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hw_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_i2c_make_new, diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index f6cf7f8155c96..b9de026ed425c 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -157,7 +157,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 6a745da0675a5..e99ca990fbba3 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -30,7 +30,7 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_dac_type; -extern const mp_obj_type_t machine_hw_i2c_type; +extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; From bfa6f34404374bb7c7d892ca6cb32175e8dd19f6 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:25:34 +0200 Subject: [PATCH 2751/3533] renesas-ra: Rename machine SPI type consistently across ports. This renames: - machine_hard_spi_type -> machine_spi_type --- ports/renesas-ra/machine_spi.c | 6 +++--- ports/renesas-ra/modmachine.c | 2 +- ports/renesas-ra/modmachine.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/renesas-ra/machine_spi.c b/ports/renesas-ra/machine_spi.c index b9f5b1ad1b4eb..2c71c83b666c1 100644 --- a/ports/renesas-ra/machine_spi.c +++ b/ports/renesas-ra/machine_spi.c @@ -70,7 +70,7 @@ typedef struct _machine_hard_spi_obj_t { STATIC machine_hard_spi_obj_t machine_hard_spi_obj[] = { #if defined(MICROPY_HW_SPI0_RSPCK) { - {&machine_hard_spi_type}, 0, + {&machine_spi_type}, 0, DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT, DEFAULT_SPI_BAUDRATE, MICROPY_HW_SPI0_RSPCK, MICROPY_HW_SPI0_MOSI, MICROPY_HW_SPI0_MISO, @@ -78,7 +78,7 @@ STATIC machine_hard_spi_obj_t machine_hard_spi_obj[] = { #endif #if defined(MICROPY_HW_SPI1_RSPCK) { - {&machine_hard_spi_type}, 1, + {&machine_spi_type}, 1, DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT, DEFAULT_SPI_BAUDRATE, MICROPY_HW_SPI1_RSPCK, MICROPY_HW_SPI1_MOSI, MICROPY_HW_SPI1_MISO, @@ -298,7 +298,7 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_spi_type, + machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, make_new, machine_hard_spi_make_new, diff --git a/ports/renesas-ra/modmachine.c b/ports/renesas-ra/modmachine.c index e6f78747fdbac..7db36298db4a7 100644 --- a/ports/renesas-ra/modmachine.c +++ b/ports/renesas-ra/modmachine.c @@ -286,7 +286,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_SPI - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, #endif { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, diff --git a/ports/renesas-ra/modmachine.h b/ports/renesas-ra/modmachine.h index de421c5db81ff..5dbda6bda8ae7 100644 --- a/ports/renesas-ra/modmachine.h +++ b/ports/renesas-ra/modmachine.h @@ -37,7 +37,7 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_adcblock_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_i2c_type; -extern const mp_obj_type_t machine_hard_spi_type; +extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_i2s_type; extern const mp_obj_type_t machine_uart_type; extern const mp_obj_type_t machine_rtc_type; From 5e90ec233139e90b5dc022932b15b3bc9fd7865c Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:28:31 +0200 Subject: [PATCH 2752/3533] zephyr: Rename machine I2C and SPI types consistently across ports. This renames: - machine_hard_i2c_type -> machine_i2c_type - machine_hard_spi_type -> machine_spi_type --- ports/zephyr/machine_i2c.c | 4 ++-- ports/zephyr/machine_spi.c | 4 ++-- ports/zephyr/modmachine.c | 4 ++-- ports/zephyr/modmachine.h | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index 8f0f25257ba54..c261ffad0d251 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -84,7 +84,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of timeout is not implemented")); } - machine_hard_i2c_obj_t *self = mp_obj_malloc(machine_hard_i2c_obj_t, &machine_hard_i2c_type); + machine_hard_i2c_obj_t *self = mp_obj_malloc(machine_hard_i2c_obj_t, &machine_i2c_type); self->dev = dev; self->restart = false; @@ -127,7 +127,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_hard_i2c_make_new, diff --git a/ports/zephyr/machine_spi.c b/ports/zephyr/machine_spi.c index 19d4ae6dee684..507d839c66442 100644 --- a/ports/zephyr/machine_spi.c +++ b/ports/zephyr/machine_spi.c @@ -106,7 +106,7 @@ mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz .cs = NULL }; - machine_hard_spi_obj_t *self = mp_obj_malloc(machine_hard_spi_obj_t, &machine_hard_spi_type); + machine_hard_spi_obj_t *self = mp_obj_malloc(machine_hard_spi_obj_t, &machine_spi_type); self->dev = dev; self->config = cfg; @@ -198,7 +198,7 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_spi_type, + machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, make_new, machine_hard_spi_make_new, diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 63bd3369897dd..95a66c51199b1 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -69,10 +69,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, #endif #if MICROPY_PY_MACHINE_SPI - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, #endif { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index a3cdd2b30fe6d..a605ada0de76c 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -4,8 +4,8 @@ #include "py/obj.h" extern const mp_obj_type_t machine_pin_type; -extern const mp_obj_type_t machine_hard_i2c_type; -extern const mp_obj_type_t machine_hard_spi_type; +extern const mp_obj_type_t machine_i2c_type; +extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_uart_type; MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj); From 93f3910fe7e43de5661ff0d6f961c6eccb8537e5 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Oct 2022 20:27:09 +0200 Subject: [PATCH 2753/3533] nrf: Rename machine I2C, PWM, SPI, UART types consistently across ports. This renames: - machine_hard_i2c_type -> machine_i2c_type - machine_hard_pwm_type -> machine_pwm_type - machine_hard_spi_type -> machine_spi_type - machine_hard_uart_type -> machine_uart_type --- ports/nrf/main.c | 2 +- ports/nrf/modules/machine/i2c.c | 6 +++--- ports/nrf/modules/machine/i2c.h | 2 +- ports/nrf/modules/machine/modmachine.c | 8 ++++---- ports/nrf/modules/machine/pwm.c | 16 ++++++++-------- ports/nrf/modules/machine/pwm.h | 2 +- ports/nrf/modules/machine/spi.c | 14 +++++++------- ports/nrf/modules/machine/spi.h | 2 +- ports/nrf/modules/machine/uart.c | 4 ++-- ports/nrf/modules/machine/uart.h | 2 +- ports/nrf/modules/uos/moduos.c | 2 +- 11 files changed, 30 insertions(+), 30 deletions(-) diff --git a/ports/nrf/main.c b/ports/nrf/main.c index bcfaafd39c7e0..989db58b3fa85 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -170,7 +170,7 @@ int main(int argc, char **argv) { MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_NEW_SMALL_INT(115200), }; - MP_STATE_PORT(board_stdio_uart) = MP_OBJ_TYPE_GET_SLOT(&machine_hard_uart_type, make_new)((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args); + MP_STATE_PORT(board_stdio_uart) = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, MP_ARRAY_SIZE(args), 0, args); } #endif diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index c16c3669ec577..ac331b8d7af6a 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -69,8 +69,8 @@ typedef struct _machine_hard_i2c_obj_t { } machine_hard_i2c_obj_t; STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[] = { - {{&machine_hard_i2c_type}, .p_twi = NRFX_TWI_INSTANCE(0)}, - {{&machine_hard_i2c_type}, .p_twi = NRFX_TWI_INSTANCE(1)}, + {{&machine_i2c_type}, .p_twi = NRFX_TWI_INSTANCE(0)}, + {{&machine_i2c_type}, .p_twi = NRFX_TWI_INSTANCE(1)}, }; void i2c_init0(void) { @@ -162,7 +162,7 @@ STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_i2c_type, + machine_i2c_type, MP_QSTR_I2C, MP_TYPE_FLAG_NONE, make_new, machine_hard_i2c_make_new, diff --git a/ports/nrf/modules/machine/i2c.h b/ports/nrf/modules/machine/i2c.h index 1dfb1f077a446..5c5befc285354 100644 --- a/ports/nrf/modules/machine/i2c.h +++ b/ports/nrf/modules/machine/i2c.h @@ -29,7 +29,7 @@ #include "extmod/machine_i2c.h" -extern const mp_obj_type_t machine_hard_i2c_type; +extern const mp_obj_type_t machine_i2c_type; void i2c_init0(void); diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index d322e450671f9..d315582ae6fd5 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -217,13 +217,13 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, #if MICROPY_PY_MACHINE_UART - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_hard_uart_type) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, #endif #if MICROPY_PY_MACHINE_HW_SPI - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, #endif #if MICROPY_PY_MACHINE_I2C - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, #endif #if MICROPY_PY_MACHINE_ADC @@ -236,7 +236,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, #endif #if MICROPY_PY_MACHINE_HW_PWM - { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_hard_pwm_type) }, + { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, #endif #if MICROPY_PY_MACHINE_TEMP { MP_ROM_QSTR(MP_QSTR_Temp), MP_ROM_PTR(&machine_temp_type) }, diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index 862e1907cbf3a..7c84a8c885f90 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -77,11 +77,11 @@ STATIC machine_pwm_config_t hard_configs[MP_ARRAY_SIZE(machine_hard_pwm_instance STATIC const machine_hard_pwm_obj_t machine_hard_pwm_obj[] = { #if defined(NRF52_SERIES) - {{&machine_hard_pwm_type}, .p_pwm = &machine_hard_pwm_instances[0], .p_config = &hard_configs[0]}, - {{&machine_hard_pwm_type}, .p_pwm = &machine_hard_pwm_instances[1], .p_config = &hard_configs[1]}, - {{&machine_hard_pwm_type}, .p_pwm = &machine_hard_pwm_instances[2], .p_config = &hard_configs[2]}, + {{&machine_pwm_type}, .p_pwm = &machine_hard_pwm_instances[0], .p_config = &hard_configs[0]}, + {{&machine_pwm_type}, .p_pwm = &machine_hard_pwm_instances[1], .p_config = &hard_configs[1]}, + {{&machine_pwm_type}, .p_pwm = &machine_hard_pwm_instances[2], .p_config = &hard_configs[2]}, #if NRF52840 - {{&machine_hard_pwm_type}, .p_pwm = &machine_hard_pwm_instances[3], .p_config = &hard_configs[3]}, + {{&machine_pwm_type}, .p_pwm = &machine_hard_pwm_instances[3], .p_config = &hard_configs[3]}, #endif #endif }; @@ -155,7 +155,7 @@ STATIC mp_obj_t machine_pwm_init(size_t n_args, const mp_obj_t *pos_args, mp_map mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // dispatch to specific implementation - if (mp_obj_get_type(self) == &machine_hard_pwm_type) { + if (mp_obj_get_type(self) == &machine_pwm_type) { machine_hard_pwm_init(self, args); } @@ -165,7 +165,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pwm_init_obj, 1, machine_pwm_init); STATIC mp_obj_t machine_pwm_deinit(mp_obj_t self) { // dispatch to specific implementation - if (mp_obj_get_type(self) == &machine_hard_pwm_type) { + if (mp_obj_get_type(self) == &machine_pwm_type) { machine_hard_pwm_deinit(self); } return mp_const_none; @@ -182,7 +182,7 @@ STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *pos_args, mp_map mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - if (mp_obj_get_type(self) == &machine_hard_pwm_type) { + if (mp_obj_get_type(self) == &machine_pwm_type) { machine_hard_pwm_freq(self, args); } else { // soft pwm @@ -340,7 +340,7 @@ STATIC mp_obj_t machine_hard_pwm_freq(mp_obj_t self_in, mp_arg_val_t *args) { } MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_pwm_type, + machine_pwm_type, MP_QSTR_PWM, MP_TYPE_FLAG_NONE, make_new, machine_pwm_make_new, diff --git a/ports/nrf/modules/machine/pwm.h b/ports/nrf/modules/machine/pwm.h index 7a5b72e0e84fa..ab2d927fa4e50 100644 --- a/ports/nrf/modules/machine/pwm.h +++ b/ports/nrf/modules/machine/pwm.h @@ -26,4 +26,4 @@ void pwm_init0(void); -extern const mp_obj_type_t machine_hard_pwm_type; +extern const mp_obj_type_t machine_pwm_type; diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c index fd7f7bb5bcced..0dee20027b2d2 100644 --- a/ports/nrf/modules/machine/spi.c +++ b/ports/nrf/modules/machine/spi.c @@ -117,12 +117,12 @@ STATIC const nrfx_spi_t machine_spi_instances[] = { STATIC nrfx_spi_config_t configs[MP_ARRAY_SIZE(machine_spi_instances)]; STATIC const machine_hard_spi_obj_t machine_hard_spi_obj[] = { - {{&machine_hard_spi_type}, .p_spi = &machine_spi_instances[0], .p_config = &configs[0]}, - {{&machine_hard_spi_type}, .p_spi = &machine_spi_instances[1], .p_config = &configs[1]}, + {{&machine_spi_type}, .p_spi = &machine_spi_instances[0], .p_config = &configs[0]}, + {{&machine_spi_type}, .p_spi = &machine_spi_instances[1], .p_config = &configs[1]}, #if defined(NRF52_SERIES) - {{&machine_hard_spi_type}, .p_spi = &machine_spi_instances[2], .p_config = &configs[2]}, + {{&machine_spi_type}, .p_spi = &machine_spi_instances[2], .p_config = &configs[2]}, #if defined(NRF52840_XXAA) && NRFX_SPIM_ENABLED - {{&machine_hard_spi_type}, .p_spi = &machine_spi_instances[3], .p_config = &configs[3]}, + {{&machine_spi_type}, .p_spi = &machine_spi_instances[3], .p_config = &configs[3]}, #endif // NRF52840_XXAA && NRFX_SPIM_ENABLED #endif // NRF52_SERIES }; @@ -235,7 +235,7 @@ STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *pos_args, mp_map mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // dispatch to specific implementation - if (mp_obj_get_type(self) == &machine_hard_spi_type) { + if (mp_obj_get_type(self) == &machine_spi_type) { machine_hard_spi_init(self, args); } @@ -245,7 +245,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init); STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) { // dispatch to specific implementation - if (mp_obj_get_type(self) == &machine_hard_spi_type) { + if (mp_obj_get_type(self) == &machine_spi_type) { machine_hard_spi_deinit(self); } return mp_const_none; @@ -428,7 +428,7 @@ STATIC const mp_machine_spi_p_t machine_hard_spi_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_spi_type, + machine_spi_type, MP_QSTR_SPI, MP_TYPE_FLAG_NONE, make_new, machine_spi_make_new, diff --git a/ports/nrf/modules/machine/spi.h b/ports/nrf/modules/machine/spi.h index c6f64a19da2a7..e1505781ac38d 100644 --- a/ports/nrf/modules/machine/spi.h +++ b/ports/nrf/modules/machine/spi.h @@ -27,7 +27,7 @@ #include "py/obj.h" typedef struct _machine_hard_spi_obj_t machine_hard_spi_obj_t; -extern const mp_obj_type_t machine_hard_spi_type; +extern const mp_obj_type_t machine_spi_type; void spi_init0(void); void spi_transfer(const machine_hard_spi_obj_t * self, diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 035a30df9c015..eee5b8b79eac9 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -106,7 +106,7 @@ static const nrfx_uart_t instance0 = NRFX_UART_INSTANCE(0); STATIC machine_hard_uart_buf_t machine_hard_uart_buf[1]; STATIC const machine_hard_uart_obj_t machine_hard_uart_obj[] = { - {{&machine_hard_uart_type}, .p_uart = &instance0, .buf = &machine_hard_uart_buf[0]}, + {{&machine_uart_type}, .p_uart = &instance0, .buf = &machine_hard_uart_buf[0]}, }; void uart_init0(void) { @@ -371,7 +371,7 @@ STATIC const mp_stream_p_t uart_stream_p = { }; MP_DEFINE_CONST_OBJ_TYPE( - machine_hard_uart_type, + machine_uart_type, MP_QSTR_UART, MP_TYPE_FLAG_ITER_IS_STREAM, make_new, machine_hard_uart_make_new, diff --git a/ports/nrf/modules/machine/uart.h b/ports/nrf/modules/machine/uart.h index 121f83cd39b39..d3e23645b902d 100644 --- a/ports/nrf/modules/machine/uart.h +++ b/ports/nrf/modules/machine/uart.h @@ -32,7 +32,7 @@ #include "genhdr/pins.h" typedef struct _machine_hard_uart_obj_t machine_hard_uart_obj_t; -extern const mp_obj_type_t machine_hard_uart_type; +extern const mp_obj_type_t machine_uart_type; void uart_init0(void); void uart_deinit(void); diff --git a/ports/nrf/modules/uos/moduos.c b/ports/nrf/modules/uos/moduos.c index 3fdd8eb19ec15..402096aacd789 100644 --- a/ports/nrf/modules/uos/moduos.c +++ b/ports/nrf/modules/uos/moduos.c @@ -126,7 +126,7 @@ STATIC mp_obj_t os_dupterm(mp_uint_t n_args, const mp_obj_t *args) { } else { if (args[0] == mp_const_none) { MP_STATE_PORT(board_stdio_uart) = NULL; - } else if (mp_obj_get_type(args[0]) == &machine_hard_uart_type) { + } else if (mp_obj_get_type(args[0]) == &machine_uart_type) { MP_STATE_PORT(board_stdio_uart) = args[0]; } else { mp_raise_ValueError(MP_ERROR_TEXT("need a UART object")); From 4f946ba963b452cb79524225e0a88134921f2ebb Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Sep 2022 12:51:10 +1000 Subject: [PATCH 2754/3533] lib/btstack: Update to v1.5.3. Signed-off-by: Damien George --- lib/btstack | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/btstack b/lib/btstack index c8b9823f68c6a..87087689bb0c3 160000 --- a/lib/btstack +++ b/lib/btstack @@ -1 +1 @@ -Subproject commit c8b9823f68c6af0fa52e2c4e009aba4dbf257232 +Subproject commit 87087689bb0c37ff4b9b3e3ba670b7019c306ebd From 67f98ba10c3d894e737f275f0a508b7ccf4f1807 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 8 Sep 2022 12:51:19 +1000 Subject: [PATCH 2755/3533] extmod/btstack: Update BTstack bindings to work with latest BTstack. The following multi-tests pass (eg with PYBD_SF6+LEGO_HUB_NO6): ble_gap_advertise.py ble_gap_connect.py ble_gap_device_name.py ble_gattc_discover_services.py ble_gatt_data_transfer.py perf_gatt_char_write.py perf_gatt_notify.py stress_log_filesystem.py These are the same tests that passed prior to this BTstack update. Also tested on the unix port using H4 transport. Signed-off-by: Damien George --- extmod/btstack/btstack_config.h | 2 +- extmod/btstack/btstack_hci_uart.c | 6 ++++++ extmod/btstack/btstack_hci_uart.h | 2 +- extmod/btstack/modbluetooth_btstack.c | 28 ++++++++++++++++++++++----- ports/stm32/mpbtstackport.c | 11 +++-------- ports/unix/mpbtstackport_common.c | 8 +------- ports/unix/mpbtstackport_h4.c | 14 ++++++++------ ports/unix/mpbtstackport_usb.c | 1 + 8 files changed, 44 insertions(+), 28 deletions(-) diff --git a/extmod/btstack/btstack_config.h b/extmod/btstack/btstack_config.h index e56a84f94a16d..7de938cb6f7e1 100644 --- a/extmod/btstack/btstack_config.h +++ b/extmod/btstack/btstack_config.h @@ -6,7 +6,7 @@ #define ENABLE_LE_PERIPHERAL #define ENABLE_LE_CENTRAL // #define ENABLE_CLASSIC -#define ENABLE_LE_DATA_CHANNELS +#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE // #define ENABLE_LOG_INFO // #define ENABLE_LOG_DEBUG #define ENABLE_LOG_ERROR diff --git a/extmod/btstack/btstack_hci_uart.c b/extmod/btstack/btstack_hci_uart.c index 83e865b71d2d3..f945efc762cbf 100644 --- a/extmod/btstack/btstack_hci_uart.c +++ b/extmod/btstack/btstack_hci_uart.c @@ -159,6 +159,12 @@ const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = { &btstack_uart_get_supported_sleep_modes, &btstack_uart_set_sleep, &btstack_uart_set_wakeup_handler, + + // The following are needed for H5 mode only. + NULL, // set_frame_received + NULL, // set_frame_sent, + NULL, // receive_frame, + NULL, // send_frame, }; void mp_bluetooth_btstack_hci_uart_process(void) { diff --git a/extmod/btstack/btstack_hci_uart.h b/extmod/btstack/btstack_hci_uart.h index 8011e587dee3f..74983808ec28d 100644 --- a/extmod/btstack/btstack_hci_uart.h +++ b/extmod/btstack/btstack_hci_uart.h @@ -28,7 +28,7 @@ #ifndef MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H #define MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H -#include "lib/btstack/src/btstack.h" +#include "lib/btstack/src/btstack_uart_block.h" // --- Used by the port to create the HCI transport --------------------------- extern const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block; diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index b58be78a9942d..e9c0037394b21 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -368,7 +368,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t event_type == SM_EVENT_PAIRING_COMPLETE || // event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle event_type == HCI_EVENT_ENCRYPTION_CHANGE) { - DEBUG_printf(" --> enc/auth/pair/bond change\n", ); + DEBUG_printf(" --> enc/auth/pair/bond change\n"); #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING uint16_t conn_handle; switch (event_type) { @@ -420,6 +420,11 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT + } else if (event_type == GATT_EVENT_MTU) { + // This is triggered in client mode. + uint16_t conn_handle = gatt_event_mtu_get_handle(packet); + uint16_t mtu = gatt_event_mtu_get_MTU(packet); + mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu); } else if (event_type == GATT_EVENT_QUERY_COMPLETE) { uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet); @@ -625,6 +630,19 @@ STATIC void set_random_address(void) { DEBUG_printf("set_random_address: Address loaded by controller\n"); } +STATIC void deinit_stack(void) { + mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; + + // Deinitialise BTstack components. + sm_deinit(); + l2cap_deinit(); + hci_deinit(); + btstack_memory_deinit(); + btstack_run_loop_deinit(); + + MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; +} + int mp_bluetooth_init(void) { DEBUG_printf("mp_bluetooth_init\n"); @@ -702,8 +720,8 @@ int mp_bluetooth_init(void) { // Attempt a shutdown (may not do anything). mp_bluetooth_btstack_port_deinit(); - // Clean up. - MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; + // Clean up BTstack. + deinit_stack(); return timeout ? MP_ETIMEDOUT : MP_EINVAL; } @@ -757,8 +775,8 @@ void mp_bluetooth_deinit(void) { } btstack_run_loop_remove_timer(&btstack_init_deinit_timeout); - mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; - MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; + // Clean up BTstack. + deinit_stack(); DEBUG_printf("mp_bluetooth_deinit: complete\n"); } diff --git a/ports/stm32/mpbtstackport.c b/ports/stm32/mpbtstackport.c index 795534042eaec..301ac30e2060d 100644 --- a/ports/stm32/mpbtstackport.c +++ b/ports/stm32/mpbtstackport.c @@ -31,6 +31,7 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK #include "lib/btstack/src/btstack.h" +#include "lib/btstack/src/hci_transport_h4.h" #include "extmod/mpbthci.h" #include "extmod/btstack/btstack_hci_uart.h" #include "extmod/btstack/modbluetooth_btstack.h" @@ -137,16 +138,10 @@ void mp_bluetooth_hci_poll(void) { } void mp_bluetooth_btstack_port_init(void) { - static bool run_loop_init = false; - if (!run_loop_init) { - run_loop_init = true; - btstack_run_loop_init(&mp_btstack_runloop_stm32); - } else { - mp_btstack_runloop_stm32.init(); - } + btstack_run_loop_init(&mp_btstack_runloop_stm32); // hci_dump_open(NULL, HCI_DUMP_STDOUT); - const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); + const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block); hci_init(transport, &hci_transport_config_uart); #ifdef MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c index ec40db65bc3a4..66a3a0536aed3 100644 --- a/ports/unix/mpbtstackport_common.c +++ b/ports/unix/mpbtstackport_common.c @@ -79,13 +79,7 @@ uint32_t hal_time_ms(void) { } void mp_bluetooth_btstack_port_init(void) { - static bool run_loop_init = false; - if (!run_loop_init) { - run_loop_init = true; - btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); - } else { - btstack_run_loop_embedded_get_instance()->init(); - } + btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); // hci_dump_open(NULL, HCI_DUMP_STDOUT); diff --git a/ports/unix/mpbtstackport_h4.c b/ports/unix/mpbtstackport_h4.c index 4fdc20c22b69b..dacfff9a498af 100644 --- a/ports/unix/mpbtstackport_h4.c +++ b/ports/unix/mpbtstackport_h4.c @@ -32,6 +32,7 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4 +#include "lib/btstack/src/hci_transport_h4.h" #include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h" #include "extmod/btstack/btstack_hci_uart.h" @@ -42,11 +43,12 @@ #define DEBUG_printf(...) // printf(__VA_ARGS__) STATIC hci_transport_config_uart_t hci_transport_config_uart = { - HCI_TRANSPORT_CONFIG_UART, - 1000000, // initial baudrate - 0, // main baudrate - 1, // flow control - NULL, // device name + .type = HCI_TRANSPORT_CONFIG_UART, + .baudrate_init = 1000000, + .baudrate_main = 0, + .flowcontrol = 1, + .device_name = NULL, + .parity = BTSTACK_UART_PARITY_OFF, }; void mp_bluetooth_hci_poll_h4(void) { @@ -58,7 +60,7 @@ void mp_bluetooth_hci_poll_h4(void) { void mp_bluetooth_btstack_port_init_h4(void) { DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n"); - const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); + const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block); hci_init(transport, &hci_transport_config_uart); hci_set_chipset(btstack_chipset_zephyr_instance()); diff --git a/ports/unix/mpbtstackport_usb.c b/ports/unix/mpbtstackport_usb.c index 28d2c8c543f71..b8c7b758d93e6 100644 --- a/ports/unix/mpbtstackport_usb.c +++ b/ports/unix/mpbtstackport_usb.c @@ -34,6 +34,7 @@ #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB #include "lib/btstack/src/btstack.h" +#include "lib/btstack/src/hci_transport_usb.h" #include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" #include "lib/btstack/platform/embedded/hal_cpu.h" #include "lib/btstack/platform/embedded/hal_time_ms.h" From b33767896430a36f163c0f9bb7020e688ccb9c5b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 5 Oct 2022 10:46:11 +1100 Subject: [PATCH 2756/3533] extmod/mbedtls: Add common configuration file, and use it in all ports. This is a no-op change. Signed-off-by: Damien George --- extmod/mbedtls/mbedtls_config_common.h | 110 +++++++++++++++++++++++++ ports/mimxrt/mbedtls/mbedtls_config.h | 69 +--------------- ports/rp2/mbedtls/mbedtls_config.h | 73 ++-------------- ports/stm32/mbedtls/mbedtls_config.h | 69 +--------------- ports/unix/mbedtls/mbedtls_config.h | 58 +------------ 5 files changed, 125 insertions(+), 254 deletions(-) create mode 100644 extmod/mbedtls/mbedtls_config_common.h diff --git a/extmod/mbedtls/mbedtls_config_common.h b/extmod/mbedtls/mbedtls_config_common.h new file mode 100644 index 0000000000000..6c9385dea5f79 --- /dev/null +++ b/extmod/mbedtls/mbedtls_config_common.h @@ -0,0 +1,110 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018-2022 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_COMMON_H +#define MICROPY_INCLUDED_MBEDTLS_CONFIG_COMMON_H + +// If you want to debug MBEDTLS uncomment the following and +// pass "3" to mbedtls_debug_set_threshold in socket_new. +// #define MBEDTLS_DEBUG_C + +// Set mbedtls configuration. +#define MBEDTLS_DEPRECATED_REMOVED +#define MBEDTLS_AES_ROM_TABLES +#define MBEDTLS_CIPHER_MODE_CBC +#define MBEDTLS_ECP_DP_SECP192R1_ENABLED +#define MBEDTLS_ECP_DP_SECP224R1_ENABLED +#define MBEDTLS_ECP_DP_SECP256R1_ENABLED +#define MBEDTLS_ECP_DP_SECP384R1_ENABLED +#define MBEDTLS_ECP_DP_SECP521R1_ENABLED +#define MBEDTLS_ECP_DP_SECP192K1_ENABLED +#define MBEDTLS_ECP_DP_SECP224K1_ENABLED +#define MBEDTLS_ECP_DP_SECP256K1_ENABLED +#define MBEDTLS_ECP_DP_BP256R1_ENABLED +#define MBEDTLS_ECP_DP_BP384R1_ENABLED +#define MBEDTLS_ECP_DP_BP512R1_ENABLED +#define MBEDTLS_ECP_DP_CURVE25519_ENABLED +#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED +#define MBEDTLS_NO_PLATFORM_ENTROPY +#define MBEDTLS_PKCS1_V15 +#define MBEDTLS_SHA256_SMALLER +#define MBEDTLS_SSL_PROTO_TLS1 +#define MBEDTLS_SSL_PROTO_TLS1_1 +#define MBEDTLS_SSL_PROTO_TLS1_2 +#define MBEDTLS_SSL_SERVER_NAME_INDICATION + +// Use a smaller output buffer to reduce size of SSL context. +#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384) +#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN) +#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096) + +// Enable mbedtls modules. +#define MBEDTLS_AES_C +#define MBEDTLS_ASN1_PARSE_C +#define MBEDTLS_BIGNUM_C +#define MBEDTLS_CIPHER_C +#define MBEDTLS_CTR_DRBG_C +#define MBEDTLS_ENTROPY_C +#define MBEDTLS_ERROR_C +#define MBEDTLS_MD_C +#define MBEDTLS_MD5_C +#define MBEDTLS_OID_C +#define MBEDTLS_PKCS5_C +#define MBEDTLS_PK_C +#define MBEDTLS_PK_PARSE_C +#define MBEDTLS_PLATFORM_C +#define MBEDTLS_RSA_C +#define MBEDTLS_SHA1_C +#define MBEDTLS_SHA256_C +#define MBEDTLS_SHA512_C +#define MBEDTLS_SSL_CLI_C +#define MBEDTLS_SSL_SRV_C +#define MBEDTLS_SSL_TLS_C +#define MBEDTLS_X509_CRT_PARSE_C +#define MBEDTLS_X509_USE_C + +// A port may enable this option to select additional bare-metal configuration. +#if MICROPY_MBEDTLS_CONFIG_BARE_METAL + +// Bare-metal mbedtls configuration. +#define MBEDTLS_PLATFORM_MEMORY +#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS +#define MBEDTLS_ENTROPY_HARDWARE_ALT + +// Bare-metal memory allocation hooks. +#include +#include +void *m_tracked_calloc(size_t nmemb, size_t size); +void m_tracked_free(void *ptr); +#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc +#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free +#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf + +#endif + +// Include mbedtls configuration checker. +#include "mbedtls/check_config.h" + +#endif // MICROPY_INCLUDED_MBEDTLS_CONFIG_COMMON_H diff --git a/ports/mimxrt/mbedtls/mbedtls_config.h b/ports/mimxrt/mbedtls/mbedtls_config.h index 8e054ed51713a..cc71aa7ec1132 100644 --- a/ports/mimxrt/mbedtls/mbedtls_config.h +++ b/ports/mimxrt/mbedtls/mbedtls_config.h @@ -26,74 +26,13 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// Set mbedtls configuration -#define MBEDTLS_PLATFORM_MEMORY -#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS -#define MBEDTLS_DEPRECATED_REMOVED -#define MBEDTLS_ENTROPY_HARDWARE_ALT -#define MBEDTLS_AES_ROM_TABLES -#define MBEDTLS_CIPHER_MODE_CBC -#define MBEDTLS_ECP_DP_SECP192R1_ENABLED -#define MBEDTLS_ECP_DP_SECP224R1_ENABLED -#define MBEDTLS_ECP_DP_SECP256R1_ENABLED -#define MBEDTLS_ECP_DP_SECP384R1_ENABLED -#define MBEDTLS_ECP_DP_SECP521R1_ENABLED -#define MBEDTLS_ECP_DP_SECP192K1_ENABLED -#define MBEDTLS_ECP_DP_SECP224K1_ENABLED -#define MBEDTLS_ECP_DP_SECP256K1_ENABLED -#define MBEDTLS_ECP_DP_BP256R1_ENABLED -#define MBEDTLS_ECP_DP_BP384R1_ENABLED -#define MBEDTLS_ECP_DP_BP512R1_ENABLED -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED -#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED -#define MBEDTLS_NO_PLATFORM_ENTROPY -#define MBEDTLS_PKCS1_V15 -#define MBEDTLS_SHA256_SMALLER -#define MBEDTLS_SSL_PROTO_TLS1 -#define MBEDTLS_SSL_PROTO_TLS1_1 -#define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_SERVER_NAME_INDICATION - -// Use a smaller output buffer to reduce size of SSL context -#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384) -#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN) -#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096) - // Enable mbedtls modules -#define MBEDTLS_AES_C -#define MBEDTLS_ASN1_PARSE_C -#define MBEDTLS_BIGNUM_C -#define MBEDTLS_CIPHER_C -#define MBEDTLS_CTR_DRBG_C // #define MBEDTLS_ECP_C -#define MBEDTLS_ENTROPY_C -#define MBEDTLS_ERROR_C -#define MBEDTLS_MD_C -#define MBEDTLS_MD5_C -#define MBEDTLS_OID_C -#define MBEDTLS_PKCS5_C -#define MBEDTLS_PK_C -#define MBEDTLS_PK_PARSE_C -#define MBEDTLS_PLATFORM_C -#define MBEDTLS_RSA_C -#define MBEDTLS_SHA1_C -#define MBEDTLS_SHA256_C -#define MBEDTLS_SHA512_C -#define MBEDTLS_SSL_CLI_C -#define MBEDTLS_SSL_SRV_C -#define MBEDTLS_SSL_TLS_C -#define MBEDTLS_X509_CRT_PARSE_C -#define MBEDTLS_X509_USE_C -// Memory allocation hooks -#include -#include -void *m_tracked_calloc(size_t nmemb, size_t size); -void m_tracked_free(void *ptr); -#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc -#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free -#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf +// Set MicroPython-specific options. +#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) -#include "mbedtls/check_config.h" +// Include common mbedtls configuration. +#include "extmod/mbedtls/mbedtls_config_common.h" #endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */ diff --git a/ports/rp2/mbedtls/mbedtls_config.h b/ports/rp2/mbedtls/mbedtls_config.h index c80aa5bc18c2a..61f622295ffa7 100644 --- a/ports/rp2/mbedtls/mbedtls_config.h +++ b/ports/rp2/mbedtls/mbedtls_config.h @@ -26,90 +26,27 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// If you want to debug MBEDTLS uncomment the following and -// Pass 3 to mbedtls_debug_set_threshold in socket_new -// #define MBEDTLS_DEBUG_C - // Set mbedtls configuration -#define MBEDTLS_PLATFORM_MEMORY -#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS -#define MBEDTLS_DEPRECATED_REMOVED -#define MBEDTLS_ENTROPY_HARDWARE_ALT -#define MBEDTLS_AES_ROM_TABLES -#define MBEDTLS_CIPHER_MODE_CBC -#define MBEDTLS_ECP_DP_SECP192R1_ENABLED -#define MBEDTLS_ECP_DP_SECP224R1_ENABLED -#define MBEDTLS_ECP_DP_SECP256R1_ENABLED -#define MBEDTLS_ECP_DP_SECP384R1_ENABLED -#define MBEDTLS_ECP_DP_SECP521R1_ENABLED -#define MBEDTLS_ECP_DP_SECP192K1_ENABLED -#define MBEDTLS_ECP_DP_SECP224K1_ENABLED -#define MBEDTLS_ECP_DP_SECP256K1_ENABLED -#define MBEDTLS_ECP_DP_BP256R1_ENABLED -#define MBEDTLS_ECP_DP_BP384R1_ENABLED -#define MBEDTLS_ECP_DP_BP512R1_ENABLED -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED #define MBEDTLS_ECP_NIST_OPTIM #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED -#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED -#define MBEDTLS_NO_PLATFORM_ENTROPY -#define MBEDTLS_PKCS1_V15 -#define MBEDTLS_SHA256_SMALLER -#define MBEDTLS_SSL_PROTO_TLS1 -#define MBEDTLS_SSL_PROTO_TLS1_1 -#define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_SERVER_NAME_INDICATION - -// Use a smaller output buffer to reduce size of SSL context -#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384) -#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN) -#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096) // Enable mbedtls modules -#define MBEDTLS_AES_C -#define MBEDTLS_ASN1_PARSE_C -#define MBEDTLS_BIGNUM_C -#define MBEDTLS_CIPHER_C -#define MBEDTLS_CTR_DRBG_C #define MBEDTLS_ECDH_C #define MBEDTLS_ECP_C -#define MBEDTLS_ENTROPY_C -#define MBEDTLS_ERROR_C #define MBEDTLS_GCM_C -#define MBEDTLS_MD_C -#define MBEDTLS_MD5_C -#define MBEDTLS_OID_C -#define MBEDTLS_PKCS5_C -#define MBEDTLS_PK_C -#define MBEDTLS_PK_PARSE_C -#define MBEDTLS_PLATFORM_C -#define MBEDTLS_RSA_C -#define MBEDTLS_SHA1_C -#define MBEDTLS_SHA256_C -#define MBEDTLS_SHA512_C -#define MBEDTLS_SSL_CLI_C -#define MBEDTLS_SSL_SRV_C -#define MBEDTLS_SSL_TLS_C #define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE -#define MBEDTLS_X509_CRT_PARSE_C -#define MBEDTLS_X509_USE_C #define MBEDTLS_HAVE_TIME #define MBEDTLS_HAVE_TIME_DATE -// Memory allocation hooks -#include -#include -void *m_tracked_calloc(size_t nmemb, size_t size); -void m_tracked_free(void *ptr); -#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc -#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free -#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf - // Time hook #include time_t rp2_rtctime_seconds(time_t *timer); #define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds -#include "mbedtls/check_config.h" +// Set MicroPython-specific options. +#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) + +// Include common mbedtls configuration. +#include "extmod/mbedtls/mbedtls_config_common.h" #endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */ diff --git a/ports/stm32/mbedtls/mbedtls_config.h b/ports/stm32/mbedtls/mbedtls_config.h index 2e0cb7651a48b..b8548866f1683 100644 --- a/ports/stm32/mbedtls/mbedtls_config.h +++ b/ports/stm32/mbedtls/mbedtls_config.h @@ -26,75 +26,14 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// Set mbedtls configuration -#define MBEDTLS_PLATFORM_MEMORY -#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS -#define MBEDTLS_DEPRECATED_REMOVED -#define MBEDTLS_ENTROPY_HARDWARE_ALT -#define MBEDTLS_AES_ROM_TABLES -#define MBEDTLS_CIPHER_MODE_CBC -#define MBEDTLS_ECP_DP_SECP192R1_ENABLED -#define MBEDTLS_ECP_DP_SECP224R1_ENABLED -#define MBEDTLS_ECP_DP_SECP256R1_ENABLED -#define MBEDTLS_ECP_DP_SECP384R1_ENABLED -#define MBEDTLS_ECP_DP_SECP521R1_ENABLED -#define MBEDTLS_ECP_DP_SECP192K1_ENABLED -#define MBEDTLS_ECP_DP_SECP224K1_ENABLED -#define MBEDTLS_ECP_DP_SECP256K1_ENABLED -#define MBEDTLS_ECP_DP_BP256R1_ENABLED -#define MBEDTLS_ECP_DP_BP384R1_ENABLED -#define MBEDTLS_ECP_DP_BP512R1_ENABLED -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED -#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED -#define MBEDTLS_NO_PLATFORM_ENTROPY -#define MBEDTLS_PKCS1_V15 -#define MBEDTLS_SHA256_SMALLER -#define MBEDTLS_SSL_PROTO_TLS1 -#define MBEDTLS_SSL_PROTO_TLS1_1 -#define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_SERVER_NAME_INDICATION - -// Use a smaller output buffer to reduce size of SSL context -#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384) -#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN) -#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096) - // Enable mbedtls modules -#define MBEDTLS_AES_C -#define MBEDTLS_ASN1_PARSE_C -#define MBEDTLS_BIGNUM_C -#define MBEDTLS_CIPHER_C -#define MBEDTLS_CTR_DRBG_C // #define MBEDTLS_ECP_C -#define MBEDTLS_ENTROPY_C -#define MBEDTLS_ERROR_C -#define MBEDTLS_MD_C -#define MBEDTLS_MD5_C -#define MBEDTLS_OID_C -#define MBEDTLS_PKCS5_C -#define MBEDTLS_PK_C -#define MBEDTLS_PK_PARSE_C -#define MBEDTLS_PLATFORM_C -#define MBEDTLS_RSA_C -#define MBEDTLS_SHA1_C -#define MBEDTLS_SHA256_C -#define MBEDTLS_SHA512_C -#define MBEDTLS_SSL_CLI_C -#define MBEDTLS_SSL_SRV_C -#define MBEDTLS_SSL_TLS_C #define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE -#define MBEDTLS_X509_CRT_PARSE_C -#define MBEDTLS_X509_USE_C -// Memory allocation hooks -#include -#include -void *m_tracked_calloc(size_t nmemb, size_t size); -void m_tracked_free(void *ptr); -#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc -#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free -#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf +// Set MicroPython-specific options. +#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) -#include "mbedtls/check_config.h" +// Include common mbedtls configuration. +#include "extmod/mbedtls/mbedtls_config_common.h" #endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */ diff --git a/ports/unix/mbedtls/mbedtls_config.h b/ports/unix/mbedtls/mbedtls_config.h index b119d09092f0a..c83f1c86f3ddd 100644 --- a/ports/unix/mbedtls/mbedtls_config.h +++ b/ports/unix/mbedtls/mbedtls_config.h @@ -26,69 +26,15 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// If you want to debug MBEDTLS uncomment the following and -// Pass 3 to mbedtls_debug_set_threshold in socket_new -// #define MBEDTLS_DEBUG_C - // Set mbedtls configuration -#define MBEDTLS_DEPRECATED_REMOVED -#define MBEDTLS_AES_ROM_TABLES -#define MBEDTLS_CIPHER_MODE_CBC #define MBEDTLS_CIPHER_MODE_CTR // needed for MICROPY_PY_UCRYPTOLIB_CTR -#define MBEDTLS_ECP_DP_SECP192R1_ENABLED -#define MBEDTLS_ECP_DP_SECP224R1_ENABLED -#define MBEDTLS_ECP_DP_SECP256R1_ENABLED -#define MBEDTLS_ECP_DP_SECP384R1_ENABLED -#define MBEDTLS_ECP_DP_SECP521R1_ENABLED -#define MBEDTLS_ECP_DP_SECP192K1_ENABLED -#define MBEDTLS_ECP_DP_SECP224K1_ENABLED -#define MBEDTLS_ECP_DP_SECP256K1_ENABLED -#define MBEDTLS_ECP_DP_BP256R1_ENABLED -#define MBEDTLS_ECP_DP_BP384R1_ENABLED -#define MBEDTLS_ECP_DP_BP512R1_ENABLED -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED -#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED -#define MBEDTLS_NO_PLATFORM_ENTROPY -#define MBEDTLS_PKCS1_V15 -#define MBEDTLS_SHA256_SMALLER -#define MBEDTLS_SSL_PROTO_TLS1 -#define MBEDTLS_SSL_PROTO_TLS1_1 -#define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_SERVER_NAME_INDICATION - -// Use a smaller output buffer to reduce size of SSL context -#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384) -#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN) -#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096) // Enable mbedtls modules -#define MBEDTLS_AES_C -#define MBEDTLS_ASN1_PARSE_C -#define MBEDTLS_BIGNUM_C -#define MBEDTLS_CIPHER_C -#define MBEDTLS_CTR_DRBG_C -#define MBEDTLS_ENTROPY_C -#define MBEDTLS_ERROR_C #define MBEDTLS_HAVEGE_C -#define MBEDTLS_MD_C -#define MBEDTLS_MD5_C -#define MBEDTLS_OID_C -#define MBEDTLS_PKCS5_C -#define MBEDTLS_PK_C -#define MBEDTLS_PK_PARSE_C -#define MBEDTLS_PLATFORM_C -#define MBEDTLS_RSA_C -#define MBEDTLS_SHA1_C -#define MBEDTLS_SHA256_C -#define MBEDTLS_SHA512_C -#define MBEDTLS_SSL_CLI_C -#define MBEDTLS_SSL_SRV_C -#define MBEDTLS_SSL_TLS_C #define MBEDTLS_TIMING_C #define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE -#define MBEDTLS_X509_CRT_PARSE_C -#define MBEDTLS_X509_USE_C -#include "mbedtls/check_config.h" +// Include common mbedtls configuration. +#include "extmod/mbedtls/mbedtls_config_common.h" #endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */ From 9347545f9ee66210a835cb2f1e9860949feb139f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 5 Oct 2022 10:48:31 +1100 Subject: [PATCH 2757/3533] extmod/mbedtls: Enable MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE. This was already enabled on all ports except mimxrt. Now it's enabled on all of them. Signed-off-by: Damien George --- extmod/mbedtls/mbedtls_config_common.h | 1 + ports/rp2/mbedtls/mbedtls_config.h | 1 - ports/stm32/mbedtls/mbedtls_config.h | 1 - ports/unix/mbedtls/mbedtls_config.h | 1 - 4 files changed, 1 insertion(+), 3 deletions(-) diff --git a/extmod/mbedtls/mbedtls_config_common.h b/extmod/mbedtls/mbedtls_config_common.h index 6c9385dea5f79..4c2be6228af0b 100644 --- a/extmod/mbedtls/mbedtls_config_common.h +++ b/extmod/mbedtls/mbedtls_config_common.h @@ -82,6 +82,7 @@ #define MBEDTLS_SSL_CLI_C #define MBEDTLS_SSL_SRV_C #define MBEDTLS_SSL_TLS_C +#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE #define MBEDTLS_X509_CRT_PARSE_C #define MBEDTLS_X509_USE_C diff --git a/ports/rp2/mbedtls/mbedtls_config.h b/ports/rp2/mbedtls/mbedtls_config.h index 61f622295ffa7..9c930b7d9c490 100644 --- a/ports/rp2/mbedtls/mbedtls_config.h +++ b/ports/rp2/mbedtls/mbedtls_config.h @@ -34,7 +34,6 @@ #define MBEDTLS_ECDH_C #define MBEDTLS_ECP_C #define MBEDTLS_GCM_C -#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE #define MBEDTLS_HAVE_TIME #define MBEDTLS_HAVE_TIME_DATE diff --git a/ports/stm32/mbedtls/mbedtls_config.h b/ports/stm32/mbedtls/mbedtls_config.h index b8548866f1683..cc71aa7ec1132 100644 --- a/ports/stm32/mbedtls/mbedtls_config.h +++ b/ports/stm32/mbedtls/mbedtls_config.h @@ -28,7 +28,6 @@ // Enable mbedtls modules // #define MBEDTLS_ECP_C -#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE // Set MicroPython-specific options. #define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) diff --git a/ports/unix/mbedtls/mbedtls_config.h b/ports/unix/mbedtls/mbedtls_config.h index c83f1c86f3ddd..c8ffab0832a16 100644 --- a/ports/unix/mbedtls/mbedtls_config.h +++ b/ports/unix/mbedtls/mbedtls_config.h @@ -32,7 +32,6 @@ // Enable mbedtls modules #define MBEDTLS_HAVEGE_C #define MBEDTLS_TIMING_C -#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE // Include common mbedtls configuration. #include "extmod/mbedtls/mbedtls_config_common.h" From 8874a09119e74bb0edf73a63495b559d983767b7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 5 Oct 2022 11:02:47 +1100 Subject: [PATCH 2758/3533] extmod/mbedtls: Enable elliptic curve DH and DSA cryptography. This is necessary to access sites that only support these protocols. The rp2 port already has ECDH enabled, so this just adds ECDSA there. The other ports now gain both ECDH and ECDSA. The code size increase is: - rp2 (PICO_W): +2916 bytes flash, +24 bytes BSS - stm32 (PYBD_SF6): +20480 bytes flash, +32 bytes data, +48 bytes BSS - mimxrt (TEENSY41): +20708 bytes flash, +32 bytes data, +48 bytes BSS - unix (standard x86-64): +39344 executable, +1744 bytes data, +96 BSS This is obviously a large increase in code size. But there doesn't seem to be any other option because without elliptic curve cryptography devices are partially cut off from the internet. For use cases that require small firmware size, they'll need to build custom firmware with a custom mbedtls config. Signed-off-by: Damien George --- extmod/mbedtls/mbedtls_config_common.h | 6 ++++++ ports/mimxrt/mbedtls/mbedtls_config.h | 3 --- ports/rp2/mbedtls/mbedtls_config.h | 2 -- ports/stm32/mbedtls/mbedtls_config.h | 3 --- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/extmod/mbedtls/mbedtls_config_common.h b/extmod/mbedtls/mbedtls_config_common.h index 4c2be6228af0b..252f293e03379 100644 --- a/extmod/mbedtls/mbedtls_config_common.h +++ b/extmod/mbedtls/mbedtls_config_common.h @@ -47,6 +47,8 @@ #define MBEDTLS_ECP_DP_BP512R1_ENABLED #define MBEDTLS_ECP_DP_CURVE25519_ENABLED #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED +// #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED // enabling this currently breaks ssl_data.py test +#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED #define MBEDTLS_NO_PLATFORM_ENTROPY #define MBEDTLS_PKCS1_V15 #define MBEDTLS_SHA256_SMALLER @@ -63,9 +65,13 @@ // Enable mbedtls modules. #define MBEDTLS_AES_C #define MBEDTLS_ASN1_PARSE_C +#define MBEDTLS_ASN1_WRITE_C #define MBEDTLS_BIGNUM_C #define MBEDTLS_CIPHER_C #define MBEDTLS_CTR_DRBG_C +#define MBEDTLS_ECDH_C +#define MBEDTLS_ECDSA_C +#define MBEDTLS_ECP_C #define MBEDTLS_ENTROPY_C #define MBEDTLS_ERROR_C #define MBEDTLS_MD_C diff --git a/ports/mimxrt/mbedtls/mbedtls_config.h b/ports/mimxrt/mbedtls/mbedtls_config.h index cc71aa7ec1132..4140bb5145555 100644 --- a/ports/mimxrt/mbedtls/mbedtls_config.h +++ b/ports/mimxrt/mbedtls/mbedtls_config.h @@ -26,9 +26,6 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// Enable mbedtls modules -// #define MBEDTLS_ECP_C - // Set MicroPython-specific options. #define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) diff --git a/ports/rp2/mbedtls/mbedtls_config.h b/ports/rp2/mbedtls/mbedtls_config.h index 9c930b7d9c490..1b6d3dd43a653 100644 --- a/ports/rp2/mbedtls/mbedtls_config.h +++ b/ports/rp2/mbedtls/mbedtls_config.h @@ -31,8 +31,6 @@ #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED // Enable mbedtls modules -#define MBEDTLS_ECDH_C -#define MBEDTLS_ECP_C #define MBEDTLS_GCM_C #define MBEDTLS_HAVE_TIME #define MBEDTLS_HAVE_TIME_DATE diff --git a/ports/stm32/mbedtls/mbedtls_config.h b/ports/stm32/mbedtls/mbedtls_config.h index cc71aa7ec1132..4140bb5145555 100644 --- a/ports/stm32/mbedtls/mbedtls_config.h +++ b/ports/stm32/mbedtls/mbedtls_config.h @@ -26,9 +26,6 @@ #ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H #define MICROPY_INCLUDED_MBEDTLS_CONFIG_H -// Enable mbedtls modules -// #define MBEDTLS_ECP_C - // Set MicroPython-specific options. #define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1) From e24159dec9588d83fc532aa6fcc2f418aae9df85 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 11 Oct 2022 19:22:48 +1100 Subject: [PATCH 2759/3533] extmod/mbedtls: Remove MBEDTLS_ECP_DP_CURVE25519_ENABLED config. Curve25519 arithmetic is supported in mbedtls, but it's not used for TLS. So there's no need to have this option enabled. Reduces rp2 PICO_W firmware by 2440 bytes. Thanks to @Carglglz for the information. Signed-off-by: Damien George --- extmod/mbedtls/mbedtls_config_common.h | 1 - 1 file changed, 1 deletion(-) diff --git a/extmod/mbedtls/mbedtls_config_common.h b/extmod/mbedtls/mbedtls_config_common.h index 252f293e03379..9ec2af85537f9 100644 --- a/extmod/mbedtls/mbedtls_config_common.h +++ b/extmod/mbedtls/mbedtls_config_common.h @@ -45,7 +45,6 @@ #define MBEDTLS_ECP_DP_BP256R1_ENABLED #define MBEDTLS_ECP_DP_BP384R1_ENABLED #define MBEDTLS_ECP_DP_BP512R1_ENABLED -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED // #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED // enabling this currently breaks ssl_data.py test #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED From 68f166dae9ad6dfd94038d5f4394defbb44238af Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 11 Oct 2022 19:25:05 +1100 Subject: [PATCH 2760/3533] extmod/mbedtls: Remove brainpool curves from config. They are much slower than NIST (SECP) curves and shouldn't be needed. Reduces rp2 PICO_W firmware by 1328 bytes. Thanks to @Carglglz for the information. Signed-off-by: Damien George --- extmod/mbedtls/mbedtls_config_common.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/extmod/mbedtls/mbedtls_config_common.h b/extmod/mbedtls/mbedtls_config_common.h index 9ec2af85537f9..bfbc6f7ba298f 100644 --- a/extmod/mbedtls/mbedtls_config_common.h +++ b/extmod/mbedtls/mbedtls_config_common.h @@ -42,9 +42,6 @@ #define MBEDTLS_ECP_DP_SECP192K1_ENABLED #define MBEDTLS_ECP_DP_SECP224K1_ENABLED #define MBEDTLS_ECP_DP_SECP256K1_ENABLED -#define MBEDTLS_ECP_DP_BP256R1_ENABLED -#define MBEDTLS_ECP_DP_BP384R1_ENABLED -#define MBEDTLS_ECP_DP_BP512R1_ENABLED #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED // #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED // enabling this currently breaks ssl_data.py test #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED From b161abc574b8e4fb2b00ef76be2b9c8967e18584 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 18 Oct 2022 00:18:54 +1100 Subject: [PATCH 2761/3533] py/obj: Verify floating point type is correct for repr C. Prevents double-precision floats being enabled on 32-bit architectures where they will not fit into the mp_obj_t encoding. Signed-off-by: Jim Mussared --- py/obj.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/py/obj.h b/py/obj.h index 8aa5b0a8e667b..8d62dd4f3cf01 100644 --- a/py/obj.h +++ b/py/obj.h @@ -173,6 +173,10 @@ static inline bool mp_obj_is_obj(mp_const_obj_t o) { #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C +#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_NONE +#error "MICROPY_OBJ_REPR_C requires float to be enabled." +#endif + static inline bool mp_obj_is_small_int(mp_const_obj_t o) { return (((mp_int_t)(o)) & 1) != 0; } @@ -189,6 +193,9 @@ static inline bool mp_obj_is_small_int(mp_const_obj_t o) { #endif static inline bool mp_obj_is_float(mp_const_obj_t o) { + // Ensure that 32-bit arch can only use single precision. + MP_STATIC_ASSERT(sizeof(mp_float_t) <= sizeof(mp_obj_t)); + return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006; } static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) { From 5ee1cb27711d3747747db3ecd7b761c2a064addf Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 18 Oct 2022 00:22:53 +1100 Subject: [PATCH 2762/3533] stm32/boards/ARDUINO_PORTENTA_H7: Revert to single-precision float. Using repr C is incompatible with double-precision floats on 32-bit arch. Signed-off-by: Jim Mussared --- ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.mk b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.mk index e5b48d0644f0f..7151a5abc5d7a 100644 --- a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.mk +++ b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.mk @@ -10,7 +10,7 @@ BOOTLOADER_DFU_USB_PID = 0x035b # MCU settings MCU_SERIES = h7 CMSIS_MCU = STM32H747xx -MICROPY_FLOAT_IMPL = double +MICROPY_FLOAT_IMPL = single AF_FILE = boards/stm32h743_af.csv LD_FILES = boards/ARDUINO_PORTENTA_H7/stm32h747.ld TEXT0_ADDR = 0x08040000 From 1ba0e8ff96334af986d2b7d90f6d86af27595d28 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 20 Oct 2022 13:14:25 +1100 Subject: [PATCH 2763/3533] py/persistentcode: Only emit sub-version if generated code has native. In order for v1.19.1 to load a .mpy, the formerly-feature-flags which are now used for the sub-version must be zero. The sub-version is only used to indicate a native version change, so it should be zero when emitting bytecode-only .mpy files. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- py/persistentcode.c | 7 ++----- tools/mpy-tool.py | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/py/persistentcode.c b/py/persistentcode.c index 67c8f327f002c..5408f756cf8f9 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -591,21 +591,18 @@ void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print) { // header contains: // byte 'M' // byte version - // byte feature flags + // byte native arch (and sub-version if native) // byte number of bits in a small int byte header[4] = { 'M', MPY_VERSION, - MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION), + cm->has_native ? MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH_DYNAMIC) : 0, #if MICROPY_DYNAMIC_COMPILER mp_dynamic_compiler.small_int_bits, #else MP_SMALL_INT_BITS, #endif }; - if (cm->has_native) { - header[2] |= MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH_DYNAMIC); - } mp_print_bytes(print, header, sizeof(header)); // Number of entries in constant table. diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 8b644c137f4c7..0b8a0403ca286 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -1676,7 +1676,7 @@ def merge_mpy(compiled_modules, output_file): header = bytearray(4) header[0] = ord("M") header[1] = config.MPY_VERSION - header[2] = config.native_arch << 2 | config.MPY_SUB_VERSION + header[2] = config.native_arch << 2 | config.MPY_SUB_VERSION if config.native_arch else 0 header[3] = config.mp_small_int_bits merged_mpy.extend(header) From edc3f3d0d3c4099dc6bce5371df0e1c3c5a4aecb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 29 Jun 2022 15:31:21 +0200 Subject: [PATCH 2764/3533] samd/clock_config: Extend the range of machine.freq(). The value given for machine.freq(f) is extend to the range of 1_000_000 to 200_000_000. Frequencies below 48 MHz will be forced to an integer fraction of 48 MHz. At frequencies below 8 MHz USB is switched off. The power consumption e.g. of ADAFRUIT_ITSYBITSY_M4_EXPRESS drops to about 1.5 mA at 1 MHz. Since the peripheral frequency is dropped as well, timing e.g. of PWM, UART, I2C and SPI is affected and frequency/baud rate has to set again after a frequency change below 48 MHz. --- ports/samd/mcu/samd51/clock_config.c | 72 ++++++++++++++++++++-------- ports/samd/modmachine.c | 4 +- ports/samd/samd_soc.c | 2 +- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index 31c8f5a8654c2..9fb48705adcba 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -54,35 +54,63 @@ uint32_t get_peripheral_freq(void) { } void set_cpu_freq(uint32_t cpu_freq_arg) { - cpu_freq = cpu_freq_arg; // Setup GCLK0 for 48MHz as default state to keep the MCU running during config change. GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL0) { } - // Setup DPLL0 for 120 MHz // first: disable DPLL0 in case it is running OSCCTRL->Dpll[0].DPLLCTRLA.bit.ENABLE = 0; while (OSCCTRL->Dpll[0].DPLLSYNCBUSY.bit.ENABLE == 1) { } - // Now configure the registers - OSCCTRL->Dpll[0].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_DIV(1) | OSCCTRL_DPLLCTRLB_LBYPASS | - OSCCTRL_DPLLCTRLB_REFCLK(0) | OSCCTRL_DPLLCTRLB_WUF | OSCCTRL_DPLLCTRLB_FILTER(0x01); - - uint32_t div = cpu_freq / DPLLx_REF_FREQ; - uint32_t frac = (cpu_freq - div * DPLLx_REF_FREQ) / (DPLLx_REF_FREQ / 32); - OSCCTRL->Dpll[0].DPLLRATIO.reg = (frac << 16) + div - 1; - // enable it again - OSCCTRL->Dpll[0].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_ENABLE | OSCCTRL_DPLLCTRLA_RUNSTDBY; - - // Per errata 2.13.1 - while (!(OSCCTRL->Dpll[0].DPLLSTATUS.bit.CLKRDY == 1)) { + if (cpu_freq_arg > DFLL48M_FREQ) { + + cpu_freq = cpu_freq_arg; + peripheral_freq = DFLL48M_FREQ; + // Now configure the registers + OSCCTRL->Dpll[0].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_DIV(1) | OSCCTRL_DPLLCTRLB_LBYPASS | + OSCCTRL_DPLLCTRLB_REFCLK(0) | OSCCTRL_DPLLCTRLB_WUF | OSCCTRL_DPLLCTRLB_FILTER(0x01); + + uint32_t div = cpu_freq / DPLLx_REF_FREQ; + uint32_t frac = (cpu_freq - div * DPLLx_REF_FREQ) / (DPLLx_REF_FREQ / 32); + OSCCTRL->Dpll[0].DPLLRATIO.reg = (frac << 16) + div - 1; + // enable it again + OSCCTRL->Dpll[0].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_ENABLE | OSCCTRL_DPLLCTRLA_RUNSTDBY; + + // Per errata 2.13.1 + while (!(OSCCTRL->Dpll[0].DPLLSTATUS.bit.CLKRDY == 1)) { + } + // Setup GCLK0 for DPLL0 output (48 or 48-200MHz) + GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DPLL0; + while (GCLK->SYNCBUSY.bit.GENCTRL0) { + } + // Set GCLK 2 back to 48 MHz + GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL2) { + } + } else { + int div = DFLL48M_FREQ / cpu_freq_arg; + // Setup GCLK1 for the low freq + GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(div) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL2) { + } + GCLK->GENCTRL[0].reg = GCLK_GENCTRL_DIV(div) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL0) { + } + peripheral_freq = DFLL48M_FREQ / div; + cpu_freq = DFLL48M_FREQ / div; } - - // Setup GCLK0 for DPLL0 output (48 or 48-200MHz) - GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DPLL0; - while (GCLK->SYNCBUSY.bit.GENCTRL0) { + if (cpu_freq >= 8000000) { + // Setup GCLK5 for DFLL48M output (48 MHz) + GCLK->GENCTRL[5].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + while (GCLK->SYNCBUSY.bit.GENCTRL5) { + } + } else { + // Setup GCLK5 off if CPU Clk < 8 MHz + GCLK->GENCTRL[5].reg = 0; + while (GCLK->SYNCBUSY.bit.GENCTRL5) { + } } } @@ -120,9 +148,10 @@ void init_clocks(uint32_t cpu_freq) { // SAMD51 clock settings // GCLK0: 48MHz from DFLL48M or 48 - 200 MHz from DPLL0 (SAMD51) // GCLK1: 32768 Hz from 32KULP or DFLL48M - // GCLK2: 48MHz from DFLL48M for Peripheral devices - // GCLK3: 16Mhz for the us-counter (TC0/TC1) + // GCLK2: 8-48MHz from DFLL48M for Peripheral devices + // GCLK3: 8Mhz for the us-counter (TC0/TC1) // GCLK4: 32kHz from crystal, if present + // GCLK5: 48MHz from DFLL48M for USB // DPLL0: 48 - 200 MHz // Steps to set up clocks: @@ -136,6 +165,7 @@ void init_clocks(uint32_t cpu_freq) { // Setup GCLK2 to 48MHz for Peripherals // Setup GCLK3 to 8MHz for TC0/TC1 // Setup GCLK4 to 32kHz crystal, if present + // Setup GCLK5 to 48 MHz // Setup GCLK0 for 48MHz as default state to keep the MCU running during config change. GCLK->GENCTRL[0].reg = GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; @@ -238,7 +268,7 @@ void init_clocks(uint32_t cpu_freq) { peripheral_freq = DFLL48M_FREQ; // To be changed if CPU_FREQ < 48M - // Setup GCLK2 for DPLL1 output (48 MHz) + // Setup GCLK2 for DFLL48M output (48 MHz), may be scaled down later by calls to set_cpu_freq GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL2) { } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index b9de026ed425c..d16b660645f3b 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -69,9 +69,9 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { } else { #if defined(MCU_SAMD51) uint32_t freq = mp_obj_get_int(args[0]); - if (freq >= 48000000 && freq <= 200000000) { + if (freq >= 1000000 && freq <= 200000000) { set_cpu_freq(freq); - SysTick_Config(freq / 1000); + SysTick_Config(get_cpu_freq() / 1000); } #endif return mp_const_none; diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index f4a27f3df740b..113529aeea36b 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -46,7 +46,7 @@ static void usb_init(void) { PM->APBBMASK.bit.USB_ = 1; uint8_t alt = 6; // alt G, USB #elif defined(MCU_SAMD51) - GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; + GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK5; while (GCLK->PCHCTRL[USB_GCLK_ID].bit.CHEN == 0) { } MCLK->AHBMASK.bit.USB_ = 1; From 1c32cec7f11864a9bc67f988e39e7dfc7c33188e Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 29 Jun 2022 17:22:20 +0200 Subject: [PATCH 2765/3533] samd/clock_config: Support changing machine.freq() for SAMD21. The range is 1MHz - 48 MHz. Note that below 8 MHz there is no USB support. The frequency will be set to an integer fraction of 48 MHz. And after changing the frequency, the peripherals like PWM, UART, I2C, SPI have to be reconfigured. Current consumption e.g. of the Seeed Xiao board at 1 MHz is about 1.5 mA, mostly caused by the on-board LED (green LED with 1k resistor at 3.3V). --- ports/samd/mcu/samd21/clock_config.c | 43 +++++++++++++++++++++------- ports/samd/mcu/samd21/mpconfigmcu.h | 1 + ports/samd/mcu/samd51/mpconfigmcu.h | 1 + ports/samd/modmachine.c | 4 +-- ports/samd/samd_soc.c | 2 +- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index 2402ed2e3107a..204a5294f2576 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -51,7 +51,36 @@ uint32_t get_peripheral_freq(void) { } void set_cpu_freq(uint32_t cpu_freq_arg) { - cpu_freq = cpu_freq_arg; + + // Set 1 waitstate to be safe + NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_MANW | NVMCTRL_CTRLB_RWS(1); + + int div = DFLL48M_FREQ / cpu_freq_arg; + peripheral_freq = cpu_freq = DFLL48M_FREQ / div; + + // Enable GCLK output: 48M on both CCLK0 and GCLK2 + GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(div); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(div); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + if (cpu_freq >= 8000000) { + // Enable GCLK output: 48MHz on GCLK5 for USB + GCLK->GENDIV.reg = GCLK_GENDIV_ID(5) | GCLK_GENDIV_DIV(1); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(5); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + } else { + // Disable GCLK output on GCLK5 for USB, since USB is not reliable below 8 Mhz. + GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(5); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + } + // Set 0 waitstates for slower CPU clock + NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_MANW | NVMCTRL_CTRLB_RWS(cpu_freq > 24000000 ? 1 : 0); } void check_usb_recovery_mode(void) { @@ -76,6 +105,7 @@ void init_clocks(uint32_t cpu_freq) { // GCLK2: 48MHz from DFLL for Peripherals // GCLK3: 1Mhz for the us-counter (TC4/TC5) // GCLK4: 32kHz from crystal, if present + // GCLK5: 48MHz from DFLL for USB // GCLK8: 1kHz clock for WDT NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" @@ -180,15 +210,7 @@ void init_clocks(uint32_t cpu_freq) { #endif // MICROPY_HW_XOSC32K - // Enable GCLK output: 48M on both CCLK0 and GCLK2 - GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0); - while (GCLK->STATUS.bit.SYNCBUSY) { - } - GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(1); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2); - while (GCLK->STATUS.bit.SYNCBUSY) { - } + set_cpu_freq(cpu_freq); // Enable GCLK output: 1MHz on GCLK3 for TC4 GCLK->GENDIV.reg = GCLK_GENDIV_ID(3) | GCLK_GENDIV_DIV(48); @@ -200,7 +222,6 @@ void init_clocks(uint32_t cpu_freq) { GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); while (GCLK->STATUS.bit.SYNCBUSY) { } - } void enable_sercom_clock(int id) { diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index a84b31276b428..e0af60552b022 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -19,6 +19,7 @@ #define CPU_FREQ (48000000) #define DFLL48M_FREQ (48000000) +#define MAX_CPU_FREQ (48000000) #define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 19193992f058d..819bc1bb11b72 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -26,6 +26,7 @@ unsigned long trng_random_u32(void); #define CPU_FREQ (120000000) #define DFLL48M_FREQ (48000000) +#define MAX_CPU_FREQ (200000000) #define DPLLx_REF_FREQ (32768) #define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index d16b660645f3b..21d700ac31ca5 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -67,13 +67,11 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { return MP_OBJ_NEW_SMALL_INT(get_cpu_freq()); } else { - #if defined(MCU_SAMD51) uint32_t freq = mp_obj_get_int(args[0]); - if (freq >= 1000000 && freq <= 200000000) { + if (freq >= 1000000 && freq <= MAX_CPU_FREQ) { set_cpu_freq(freq); SysTick_Config(get_cpu_freq() / 1000); } - #endif return mp_const_none; } } diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 113529aeea36b..6d8348ebcd833 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -41,7 +41,7 @@ static void usb_init(void) { // Init USB clock #if defined(MCU_SAMD21) - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_USB; + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK5 | GCLK_CLKCTRL_ID_USB; PM->AHBMASK.bit.USB_ = 1; PM->APBBMASK.bit.USB_ = 1; uint8_t alt = 6; // alt G, USB From 64e3c351de0959660d1a6b691ee7dbf2de565fbe Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 29 Jun 2022 14:09:57 +0200 Subject: [PATCH 2766/3533] samd/modmachine: Add machine.reset_cause(). --- ports/samd/modmachine.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 21d700ac31ca5..db3f159cc2c37 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -139,6 +139,17 @@ STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { } MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); +STATIC mp_obj_t machine_reset_cause(void) { + #if defined(MCU_SAMD21) + return MP_OBJ_NEW_SMALL_INT(PM->RCAUSE.reg); + #elif defined(MCU_SAMD51) + return MP_OBJ_NEW_SMALL_INT(RSTC->RCAUSE.reg); + #else + return MP_OBJ_NEW_SMALL_INT(0); + #endif +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, @@ -165,8 +176,18 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) }, + + // Class constants. + // Use numerical constants instead of the symbolic names, + // since the names differ between SAMD21 and SAMD51. + { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(0x01) }, + { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(0x10) }, + { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(0x20) }, + { MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(0x40) }, + { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(0x80) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); From 0d3f0d7470586f9c328a047ac3043420df16d4ab Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 2 Jul 2022 18:12:55 +0200 Subject: [PATCH 2767/3533] samd/boards/SPARKFUN_SAMD51_THING_PLUS: Add board files for Thing Plus. That device uses an SAMD51J20 MCU with 256k RAM and 1024k flash. --- .../SPARKFUN_SAMD51_THING_PLUS/board.json | 21 ++++++++++ .../mpconfigboard.h | 4 ++ .../mpconfigboard.mk | 8 ++++ .../SPARKFUN_SAMD51_THING_PLUS/pins.csv | 42 +++++++++++++++++++ ports/samd/boards/samd51x20a.ld | 20 +++++++++ 5 files changed, 95 insertions(+) create mode 100644 ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/board.json create mode 100644 ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h create mode 100644 ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.mk create mode 100644 ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv create mode 100644 ports/samd/boards/samd51x20a.ld diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/board.json b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/board.json new file mode 100644 index 0000000000000..af643f5c5f3c2 --- /dev/null +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/board.json @@ -0,0 +1,21 @@ +{ + "deploy": [ + "../deploy.md" + ], + "docs": "", + "features": [ + "Battery Charging", + "Breadboard Friendly", + "Micro USB", + "QWIIC", + "SPI Flash" + ], + "images": [ + "sparkfun_samd51_thing_plus.jpg" + ], + "mcu": "samd51", + "product": "Sparkfun SAMD51 Thing Plus", + "thumbnail": "", + "url": "https://www.sparkfun.com/products/14713", + "vendor": "Sparkfun" +} diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h new file mode 100644 index 0000000000000..e797ccca9da11 --- /dev/null +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h @@ -0,0 +1,4 @@ +#define MICROPY_HW_BOARD_NAME "Sparkfun SAMD51 Thing Plus" +#define MICROPY_HW_MCU_NAME "SAMD51J20A" + +#define MICROPY_HW_XOSC32K (1) diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.mk b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.mk new file mode 100644 index 0000000000000..9e5cf887dfce3 --- /dev/null +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.mk @@ -0,0 +1,8 @@ +MCU_SERIES = SAMD51 +CMSIS_MCU = SAMD51J20A +LD_FILES = boards/samd51x20a.ld sections.ld +TEXT0 = 0x4000 + +# The ?='s allow overriding in mpconfigboard.mk. +# MicroPython settings +MICROPY_VFS_LFS1 ?= 1 diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv new file mode 100644 index 0000000000000..b60fb909714e1 --- /dev/null +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv @@ -0,0 +1,42 @@ +# Pin rows contain Pin number and pin name. +# Pin rows start with PIN_ +# LED rows start with LED_ +# If the pin name is omitted, the pin number is added as name. +# Rows for empty entries have to start with '-' +# Empty lines and lines not starting with PIN_ or LED_ are ignored + +PIN_PA13,D0 +PIN_PA12,D1 +PIN_PB23,RXD +PIN_PB22,TXD +PIN_PA06,D4 +PIN_PA15,D5 +PIN_PA20,D6 +PIN_PA21,D7 +- +PIN_PA07,D9 +PIN_PA18,D10 +PIN_PA16,D11 +PIN_PA19,D12 +PIN_PA17,D13 +PIN_PA02,A0 +PIN_PB08,A1 +PIN_PB09,A2 +PIN_PA04,A3 +PIN_PA05,A4 +PIN_PB02,A5 +PIN_PA22,SDA +PIN_PA23,SCL +PIN_PB12,MOSI +PIN_PB11,MISO +PIN_PB13,SCK +PIN_PA08,FLASH_MOSI +PIN_PA09,FLASH_SCK +PIN_PA10,FLASH_CS +PIN_PA11,FLASH_MISO +PIN_PA30,SWDCLK +PIN_PA31,SWDIO + +LED_PA17,LED +LED_PB03,RXLED +LED_PA27,TXLED diff --git a/ports/samd/boards/samd51x20a.ld b/ports/samd/boards/samd51x20a.ld new file mode 100644 index 0000000000000..f0d5e5c6aeb60 --- /dev/null +++ b/ports/samd/boards/samd51x20a.ld @@ -0,0 +1,20 @@ +/* + GNU linker script for SAMD51x20 +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 1024K - 16K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K +} + +/* Top end of the stack, with room for double-tap variable */ +_estack = ORIGIN(RAM) + LENGTH(RAM) - 8; +_sstack = _estack - 16K; + +_oflash_fs = ORIGIN(FLASH) + 384K - 16K; +_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1; + +_sheap = _ebss; +_eheap = _sstack; From a7113e95d78583a47f8074965e17d1e0499e0494 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 7 Jul 2022 09:49:51 +0200 Subject: [PATCH 2768/3533] samd/modmachine: Add machine.dht_readinto and enable on SAMD51. --- ports/samd/mcu/samd51/mpconfigmcu.h | 1 + ports/samd/mcu/samd51/mpconfigmcu.mk | 2 ++ ports/samd/modmachine.c | 4 ++++ ports/samd/mphalport.h | 3 +++ 4 files changed, 10 insertions(+) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 819bc1bb11b72..a266adf93cad8 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -13,6 +13,7 @@ #define MP_NEED_LOG2 (1) #define MICROPY_PY_CMATH (0) +#define MICROPY_PY_MACHINE_DHT_READINTO (1) #define MICROPY_PY_UOS_URANDOM (1) #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk index f8ed02f841d9d..ed4df82c69121 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.mk +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -2,6 +2,8 @@ MICROPY_VFS_LFS2 ?= 1 SRC_S += shared/runtime/gchelper_m3.s +SRC_C += drivers/dht/dht.c \ + LIBM_SRC_C += $(addprefix lib/libm/,\ acoshf.c \ asinfacosf.c \ diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index db3f159cc2c37..14baf279d549c 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -30,6 +30,7 @@ #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" #include "extmod/machine_spi.h" +#include "drivers/dht/dht.h" #include "modmachine.h" #include "samd_soc.h" @@ -179,6 +180,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) }, + #if MICROPY_PY_MACHINE_DHT_READINTO + { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) }, + #endif // Class constants. // Use numerical constants instead of the symbolic names, diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 6f4f838cfba2f..3879240f9a6a1 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -99,6 +99,9 @@ static inline uint64_t mp_hal_time_ns(void) { #define MP_HAL_PIN_FMT "%u" #define mp_hal_pin_obj_t uint +#define mp_hal_quiet_timing_enter() MICROPY_BEGIN_ATOMIC_SECTION() +#define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state) + extern uint32_t machine_pin_open_drain_mask[]; mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in); From fe31fca462011785cf78b4730bf34bb552e9a707 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 21 Jun 2022 14:27:20 +0200 Subject: [PATCH 2769/3533] samd/mcu/samd51: Enable onewire support for SAMD51. --- ports/samd/mcu/samd51/mpconfigmcu.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index a266adf93cad8..5735f512b5a79 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -14,6 +14,7 @@ #define MICROPY_PY_CMATH (0) #define MICROPY_PY_MACHINE_DHT_READINTO (1) +#define MICROPY_PY_ONEWIRE (1) #define MICROPY_PY_UOS_URANDOM (1) #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); From 4c9e4c3310faff5ec05b183454df88d0f33f9e78 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 10 Jul 2022 12:04:14 +0200 Subject: [PATCH 2770/3533] samd/mcu/samd51: Enable FAT support for SAMD51. Tested with a SD card connected to a SAMD51 board. The SEEED WIO terminal has a SD-Card reader built-in. Also a side change to remove a few obsolete lines from Makefile. --- ports/samd/Makefile | 4 --- ports/samd/fatfs_port.c | 41 ++++++++++++++++++++++++++++ ports/samd/mcu/samd51/mpconfigmcu.h | 6 ++++ ports/samd/mcu/samd51/mpconfigmcu.mk | 5 +++- ports/samd/modutime.c | 2 +- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 ports/samd/fatfs_port.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 2f97b1993e99a..abec4e83a5539 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -61,7 +61,6 @@ CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-a CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__ CFLAGS += $(CFLAGS_EXTRA) -CFLAGS += -DMPCONFIG_MCU_H='' LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref @@ -169,9 +168,6 @@ CFLAGS += -DMICROPY_MODULE_FROZEN_STR CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool endif -# Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };" -$(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces - all: $(BUILD)/firmware.uf2 $(BUILD)/firmware.elf: $(OBJ) diff --git a/ports/samd/fatfs_port.c b/ports/samd/fatfs_port.c new file mode 100644 index 0000000000000..9ee1764ebc0a9 --- /dev/null +++ b/ports/samd/fatfs_port.c @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2021 Robert Hammelrath + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "mphalport.h" +#include "py/runtime.h" +#include "shared/timeutils/timeutils.h" +#include "lib/oofatfs/ff.h" + +extern uint32_t time_offset; + +MP_WEAK DWORD get_fattime(void) { + timeutils_struct_time_t tm; + + timeutils_seconds_since_epoch_to_struct_time(mp_hal_ticks_ms_64() / 1000 + time_offset, &tm); + return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) | ((tm.tm_mday) << 16) | + ((tm.tm_hour) << 11) | ((tm.tm_min) << 5) | (tm.tm_sec / 2); +} diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 5735f512b5a79..686cfd6104df0 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -22,6 +22,12 @@ unsigned long trng_random_u32(void); // Due to a limitation in the TC counter for us, the ticks period is 2**29 #define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) +// fatfs configuration used in ffconf.h +#define MICROPY_FATFS_ENABLE_LFN (1) +#define MICROPY_FATFS_RPATH (2) +#define MICROPY_FATFS_MAX_SS (4096) +#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ + #define VFS_BLOCK_SIZE_BYTES (1536) // #define MICROPY_HW_UART_TXBUF (1) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.mk b/ports/samd/mcu/samd51/mpconfigmcu.mk index ed4df82c69121..305e780b8787f 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.mk +++ b/ports/samd/mcu/samd51/mpconfigmcu.mk @@ -1,8 +1,11 @@ MICROPY_VFS_LFS2 ?= 1 +MICROPY_VFS_FAT ?= 1 SRC_S += shared/runtime/gchelper_m3.s -SRC_C += drivers/dht/dht.c \ +SRC_C += \ + fatfs_port.c \ + drivers/dht/dht.c \ LIBM_SRC_C += $(addprefix lib/libm/,\ acoshf.c \ diff --git a/ports/samd/modutime.c b/ports/samd/modutime.c index a54544e62c6ca..4169c15d91172 100644 --- a/ports/samd/modutime.c +++ b/ports/samd/modutime.c @@ -29,7 +29,7 @@ #include "shared/timeutils/timeutils.h" #include "mphalport.h" -static uint32_t time_offset = 0; +uint32_t time_offset = 0; // localtime([secs]) STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { From 03075a68399a6450fd0e238dd75314805f8365b7 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 1 Aug 2022 17:23:11 +0200 Subject: [PATCH 2771/3533] samd/modmachine: Implement machine.lightsleep(). Which just sets the CPU clock to 200kHz and switches the peripheral clock off. There are two modes: machine.lightsleep(duration_ms) and machine.lightsleep() In any mode any configured pin.irq() event will terminate the sleep. Current consumption in lightsleep for some boards: - 1.5 - 2.5 mA when supplied trough an active USB (Seeed XIAO w/o power LED, Adafruit ItsyBitsy) - 0.8 - 2 mA when supplied through Gnd/+5V (Vusb) (Seeed XIAO w/o power LED, Adafruit ItsyBitsy) - < 1 mA for SAMD51 when supplied trough a battery connector (Sparkfun Thing SAMD51 plus) Related change: move the calls to SysTick_Config() into set_cpu_freq(). It is required after each CPU freq change to have ticks_ms run at the proper rate. --- ports/samd/machine_pin.c | 3 ++ ports/samd/mcu/samd21/clock_config.c | 1 + ports/samd/mcu/samd51/clock_config.c | 1 + ports/samd/modmachine.c | 61 +++++++++++++++++++++++++++- ports/samd/samd_soc.c | 1 - 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index fc72c8f7569fb..2dc10f9b46344 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -57,6 +57,8 @@ typedef struct _machine_pin_irq_obj_t { STATIC const mp_irq_methods_t machine_pin_irq_methods; +bool EIC_occured; + uint32_t machine_pin_open_drain_mask[4]; // Open drain behaviour is simulated. @@ -410,6 +412,7 @@ void EIC_Handler() { for (int eic_id = 0; eic_id < 16; eic_id++, mask <<= 1) { // Did the ISR fire? if (isr & mask) { + EIC_occured = true; EIC->INTFLAG.reg |= mask; // clear the ISR flag machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); if (irq != NULL) { diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index 204a5294f2576..a4011c05cbbc7 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -81,6 +81,7 @@ void set_cpu_freq(uint32_t cpu_freq_arg) { } // Set 0 waitstates for slower CPU clock NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_MANW | NVMCTRL_CTRLB_RWS(cpu_freq > 24000000 ? 1 : 0); + SysTick_Config(cpu_freq / 1000); } void check_usb_recovery_mode(void) { diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index 9fb48705adcba..f67d297e8eb1d 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -112,6 +112,7 @@ void set_cpu_freq(uint32_t cpu_freq_arg) { while (GCLK->SYNCBUSY.bit.GENCTRL5) { } } + SysTick_Config(cpu_freq / 1000); } void check_usb_recovery_mode(void) { diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 14baf279d549c..2a8e34a966a8d 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -50,6 +50,10 @@ #define DBL_TAP_MAGIC_LOADER 0xf01669ef #define DBL_TAP_MAGIC_RESET 0xf02669ef +#define LIGHTSLEEP_CPU_FREQ 200000 + +extern bool EIC_occured; + STATIC mp_obj_t machine_reset(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_RESET; NVIC_SystemReset(); @@ -71,7 +75,6 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { uint32_t freq = mp_obj_get_int(args[0]); if (freq >= 1000000 && freq <= MAX_CPU_FREQ) { set_cpu_freq(freq); - SysTick_Config(get_cpu_freq() / 1000); } return mp_const_none; } @@ -151,6 +154,60 @@ STATIC mp_obj_t machine_reset_cause(void) { } MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); +STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { + int32_t duration = -1; + uint32_t freq = get_cpu_freq(); + if (n_args > 0) { + duration = mp_obj_get_int(args[0]); + } + EIC_occured = false; + // Slow down + set_cpu_freq(LIGHTSLEEP_CPU_FREQ); + #if defined(MCU_SAMD21) + // Switch the peripheral clock off + GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(2); + while (GCLK->STATUS.bit.SYNCBUSY) { + } + // Switch the EIC temporarily to GCLK3, since GCLK2 is off + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | EIC_GCLK_ID; + if (duration > 0) { + uint32_t t0 = systick_ms; + while ((systick_ms - t0 < duration) && (EIC_occured == false)) { + __WFI(); + } + } else { + while (EIC_occured == false) { + __WFI(); + } + } + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | EIC_GCLK_ID; + + #elif defined(MCU_SAMD51) + // Switch the peripheral clock off + GCLK->GENCTRL[2].reg = 0; + while (GCLK->SYNCBUSY.bit.GENCTRL2) { + } + // Switch the EIC temporarily to GCLK3, since GCLK2 is off + GCLK->PCHCTRL[EIC_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK3; + if (duration > 0) { + uint32_t t0 = systick_ms; + while ((systick_ms - t0 < duration) && (EIC_occured == false)) { + __WFI(); + } + } else { + while (EIC_occured == false) { + __WFI(); + } + } + GCLK->PCHCTRL[EIC_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK2; + + #endif + // Speed up again + set_cpu_freq(freq); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, @@ -179,6 +236,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) }, #if MICROPY_PY_MACHINE_DHT_READINTO { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) }, diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 6d8348ebcd833..3608306e87194 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -108,7 +108,6 @@ void init_us_counter(void) { void samd_init(void) { init_clocks(get_cpu_freq()); - SysTick_Config(get_cpu_freq() / 1000); init_us_counter(); usb_init(); check_usb_recovery_mode(); From f0399d35e4f7ad4e48b22f02b80a7ee506c9ec64 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 11 Aug 2022 11:22:58 +0200 Subject: [PATCH 2772/3533] samd/modmachine: Get the bootloader magic address from the lib. Instead of being hard-coded, and then it works for all MCUs. That fits except for a Sparkfun SAMD51 Thing Plus (known) bug, which uses 192k - 4 as magic address. Therefore, that address is set as well to avoid a problem when this bug is fixed by Sparkfun. --- .../SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h | 6 ++++++ ports/samd/modmachine.c | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h index e797ccca9da11..a51b71c363e18 100644 --- a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h @@ -2,3 +2,9 @@ #define MICROPY_HW_MCU_NAME "SAMD51J20A" #define MICROPY_HW_XOSC32K (1) + +// There seems to be an inconsistency in the SAMD51 Thing bootloader in that +// the bootloader magic address is at the end of a 192k RAM area, instead of +// 256k. Since the SAMD51x20A has 256k RAM, the loader symbol is at that address +// and so there is a fix here using the previous definition. +#define DBL_TAP_ADDR_ALT ((volatile uint32_t *)(HSRAM_ADDR + HSRAM_SIZE - 0x10000 - 4)) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 2a8e34a966a8d..ce5fef76f753c 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -41,21 +41,26 @@ #include "hpl_pm_base.h" #if MICROPY_PY_MACHINE - #if defined(MCU_SAMD21) -#define DBL_TAP_ADDR ((volatile uint32_t *)(0x20000000 + 32 * 1024 - 4)) +#define DBL_TAP_ADDR ((volatile uint32_t *)(HMCRAMC0_ADDR + HMCRAMC0_SIZE - 4)) #elif defined(MCU_SAMD51) -#define DBL_TAP_ADDR ((volatile uint32_t *)(0x20000000 + 192 * 1024 - 4)) +#define DBL_TAP_ADDR ((volatile uint32_t *)(HSRAM_ADDR + HSRAM_SIZE - 4)) #endif +// A board may define a DPL_TAP_ADDR_ALT, which will be set as well +// Needed at the moment for Sparkfun SAMD51 Thing Plus #define DBL_TAP_MAGIC_LOADER 0xf01669ef #define DBL_TAP_MAGIC_RESET 0xf02669ef #define LIGHTSLEEP_CPU_FREQ 200000 extern bool EIC_occured; +extern uint32_t _dbl_tap_addr; STATIC mp_obj_t machine_reset(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_RESET; + #ifdef DBL_TAP_ADDR_ALT + *DBL_TAP_ADDR_ALT = DBL_TAP_MAGIC_RESET; + #endif NVIC_SystemReset(); return mp_const_none; } @@ -63,6 +68,9 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); STATIC mp_obj_t machine_bootloader(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_LOADER; + #ifdef DBL_TAP_ADDR_ALT + *DBL_TAP_ADDR_ALT = DBL_TAP_MAGIC_LOADER; + #endif NVIC_SystemReset(); return mp_const_none; } From ddd41b8bbf6823ea730e781d508babbecdd41304 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 13 Aug 2022 17:33:04 +0200 Subject: [PATCH 2773/3533] samd/clock_config: Document the #defines use in init_clocks(). Which may be set in the respective mpconfigboard.h files. --- ports/samd/mcu/samd21/clock_config.c | 34 +++++++++++++++++++++++ ports/samd/mcu/samd51/clock_config.c | 40 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index a4011c05cbbc7..a195cb6920536 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -96,6 +96,40 @@ void check_usb_recovery_mode(void) { #endif // MICROPY_HW_XOSC32K } +// Purpose of the #defines for the clock configuration. +// +// Both CPU and periperal devices are clocked by the DFLL48M clock. +// DFLL48M is either free running, or controlled by the 32kHz crystal, or +// Synchronized with the USB clock. +// +// #define MICROPY_HW_XOSC32K (0 | 1) +// +// If MICROPY_HW_XOSC32K = 1, the 32kHz crystal is used as input for GCLK 1, which +// serves as refernce clock source for the DFLL48M oscillator, +// The crystal is used, unless MICROPY_HW_MCU_OSC32KULP is set. +// In that case GCLK1 (and the CPU clock) is driven by the 32K Low power oscillator. +// The reason for offering this option is a design flaw of the Adafruit +// Feather boards, where the RGB Led and Debug signals interfere with the +// crystal, causing the CPU to fail if it is driven by the crystal. +// +// If MICROPY_HW_XOSC32K = 0, the 32kHz signal for GCLK1 (and the CPU) is +// created by dividing the 48MHz clock of DFLL48M, but not used otherwise. +// +// If MICROPY_HW_DFLL_USB_SYNC = 0, the DFLL48M oscillator is free running using +// the pre-configured trim values. In that mode, the peripheral clock is +// not exactly 48Mhz and has a substantional temperature drift. +// +// If MICROPY_HW_DFLL_USB_SYNC = 1, the DFLL48 is synchronized with the 1 kHz USB sync +// signal. If after boot there is no USB sync withing 500ms, the configuratuion falls +// back to a free running 48Mhz oscillator. +// +// In all modes, the 48MHz signal has a substantial jitter, largest when +// MICROPY_HW_DFLL_USB_SYNC is active. That is caused by the repective +// reference frequencies of 32kHz or 1 kHz being low. That affects most +// PWM. Std Dev at 1kHz 0.156Hz (w. Crystal) up to 0.4 Hz (with USB sync). +// +// If none of the mentioned defines is set, the device uses the internal oscillators. + void init_clocks(uint32_t cpu_freq) { dfll48m_calibration = 0; // please the compiler diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index f67d297e8eb1d..3bc4616de9e9d 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -142,6 +142,46 @@ void check_usb_recovery_mode(void) { #endif // MICROPY_HW_XOSC32K } +// Purpose of the #defines for the clock configuration. +// +// The CPU clock is generated by DPLL0, which takes 32768 Hz as reference frequency, +// supplied through GCLK1. +// +// DFLL48M is used for the peripheral clock, e.g. for PWM, UART, SPI, I2C. +// DFLL48M is either free running, or controlled by the 32kHz crystal, or +// Synchronized with the USB clock. +// +// GCLK1 takes it's input either from the 32kHz crystal, the internal low power +// RC oscillator or from DFLL48M. +// +// #define MICROPY_HW_XOSC32K (0 | 1) +// +// If MICROPY_HW_XOSC32K = 1, the 32kHz crystal is used for the DFLL48M oscillator +// and for GCLK1, feeding the CPU, unless MICROPY_HW_MCU_OSC32KULP is set. +// In that case GCLK1 (and the CPU clock) is driven by the 32K Low power oscillator. +// The reason for offering this option is a design flaw of the Adafruit +// Feather boards, where the RGB Led and Debug signals interfere with the +// crystal, causing the CPU to fail if it is driven by the crystal. The +// peripheral devices are affected as well, but continue it's operation. +// +// If MICROPY_HW_XOSC32K = 0, the 32kHz signal for GCLK1 (and the CPU) is +// created by dividing the 48MHz clock of DFLL48M. +// +// If MICROPY_HW_DFLL_USB_SYNC = 0, the DFLL48M oscillator is free running using +// the pre-configured trim values. In that mode, the peripheral clock is +// not exactly 48Mhz and has a substantional temperature drift. +// +// If MICROPY_HW_DFLL_USB_SYNC = 1, the DFLL48 is synchronized with the 1 kHz USB sync +// signal. If after boot there is no USB sync withing 500ms, the configuratuion falls +// back to a free running 48Mhz oscillator. +// +// In all modes, the 48MHz signal has a substantial jitter, largest when +// MICROPY_HW_DFLL_USB_SYNC is active. That is caused by the repective +// reference frequencies of 32kHz or 1 kHz being low. That affects most +// PWM. Std Dev at 1kHz 0.156Hz (w. Crystal) up to 0.4 Hz (with USB sync). +// +// If none of the mentioned defines is set, the device uses the internal oscillators. + void init_clocks(uint32_t cpu_freq) { dfll48m_calibration = 0; // please the compiler From 2251cb774b7b7db12323a32b339903fa27ff7b08 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 18 Aug 2022 11:58:09 +0200 Subject: [PATCH 2774/3533] samd/machine_uart: Implement uart.txdone() and uart.flush(). Using the stream method for uart.flush(). uart.txdone() returns True, if the uart not busy, False otherwise. uart.flush() waits until all bytes have been transmitted or a timeout triggers. The timeout is determined by the buffer size and the baud rate. Also fix two inconsistencies when not using txbuf: - Report in ioctl as being writeable if there is room in the tx buffer, only if it is configured. - Print the txbuf size if configured. --- ports/samd/machine_uart.c | 53 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 1031f26c26488..76b564e874a66 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -116,9 +116,17 @@ void sercom_enable(Sercom *uart, int state) { STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, " - "timeout=%u, timeout_char=%u, rxbuf=%d)", + "timeout=%u, timeout_char=%u, rxbuf=%d" + #if MICROPY_HW_UART_TXBUF + ", txbuf=%d" + #endif + ")", self->id, self->baudrate, self->bits, _parity_name[self->parity], - self->stop + 1, self->timeout, self->timeout_char, self->read_buffer.size - 1); + self->stop + 1, self->timeout, self->timeout_char, self->read_buffer.size - 1 + #if MICROPY_HW_UART_TXBUF + , self->write_buffer.size - 1 + #endif + ); } STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -385,6 +393,22 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak); +STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { + machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + Sercom *uart = sercom_instance[self->id]; + + if (uart->USART.INTFLAG.bit.DRE + #if MICROPY_HW_UART_TXBUF + && ringbuf_avail(&self->write_buffer) == 0 + #endif + && uart->USART.INTFLAG.bit.TXC) { + return mp_const_true; + } else { + return mp_const_false; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); + void uart_deinit_all(void) { for (int i = 0; i < SERCOM_INST_NUM; i++) { if (uart_table[i] != NULL) { @@ -399,7 +423,9 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) }, { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) }, + { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) }, + { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, @@ -414,7 +440,6 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz uint8_t *dest = buf_in; Sercom *uart = sercom_instance[self->id]; - // t.b.d. Cater timeout for timer wrap after 50 days. for (size_t i = 0; i < size; i++) { // Wait for the first/next character while (ringbuf_avail(&self->read_buffer) == 0) { @@ -488,9 +513,29 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint if ((flags & MP_STREAM_POLL_RD) && (uart->USART.INTFLAG.bit.RXC != 0 || ringbuf_avail(&self->read_buffer) > 0)) { ret |= MP_STREAM_POLL_RD; } - if ((flags & MP_STREAM_POLL_WR) && (uart->USART.INTFLAG.bit.DRE != 0)) { + if ((flags & MP_STREAM_POLL_WR) && (uart->USART.INTFLAG.bit.DRE != 0 + #if MICROPY_HW_UART_TXBUF + || ringbuf_avail(&self->write_buffer) > 0 + #endif + )) { ret |= MP_STREAM_POLL_WR; } + } else if (request == MP_STREAM_FLUSH) { + // The timeout is defined by the buffer size and the baudrate. + // Take the worst case assumtions at 13 bit symbol size times 2. + uint64_t timeout = mp_hal_ticks_ms_64() + (3 + #if MICROPY_HW_UART_TXBUF + + self->write_buffer.size + #endif + ) * 13000 * 2 / self->baudrate; + do { + if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) { + return 0; + } + MICROPY_EVENT_POLL_HOOK + } while (mp_hal_ticks_ms_64() < timeout); + *errcode = MP_ETIMEDOUT; + ret = MP_STREAM_ERROR; } else { *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; From be31fde012e0cf522ec9d70f50923a0a899f438c Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 8 Sep 2022 17:55:08 +0200 Subject: [PATCH 2775/3533] samd/mcu: Make some settings in mpconfigmcu.h conditional. And set the default for MICROPY_PY_MATH as 1 for both MCU types. --- ports/samd/mcu/samd21/mpconfigmcu.h | 13 ++++++++++++- ports/samd/mcu/samd21/mpconfigmcu.mk | 29 ++++++++++++++++++++++++++++ ports/samd/mcu/samd51/mpconfigmcu.h | 11 +++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index e0af60552b022..8d155f93ab032 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -9,13 +9,24 @@ #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) + +#ifndef MICROPY_PY_BUILTINS_COMPLEX #define MICROPY_PY_BUILTINS_COMPLEX (0) -#define MICROPY_PY_MATH (0) +#endif + +#ifndef MICROPY_PY_MATH +#define MICROPY_PY_MATH (1) +#endif + +#ifndef MICROPY_PY_CMATH #define MICROPY_PY_CMATH (0) +#endif #define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages; +#ifndef MICROPY_HW_UART_TXBUF #define MICROPY_HW_UART_TXBUF (1) +#endif #define CPU_FREQ (48000000) #define DFLL48M_FREQ (48000000) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.mk b/ports/samd/mcu/samd21/mpconfigmcu.mk index cc435da8cc913..287d6d18be7bd 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.mk +++ b/ports/samd/mcu/samd21/mpconfigmcu.mk @@ -1 +1,30 @@ SRC_S += shared/runtime/gchelper_m0.s + +LIBM_SRC_C += $(addprefix lib/libm/,\ + acoshf.c \ + asinfacosf.c \ + asinhf.c \ + atan2f.c \ + atanf.c \ + atanhf.c \ + ef_rem_pio2.c \ + erf_lgamma.c \ + fmodf.c \ + kf_cos.c \ + kf_rem_pio2.c \ + kf_sin.c \ + kf_tan.c \ + log1pf.c \ + math.c \ + nearbyintf.c \ + roundf.c \ + sf_cos.c \ + sf_erf.c \ + sf_frexp.c \ + sf_ldexp.c \ + sf_modf.c \ + sf_sin.c \ + sf_tan.c \ + wf_lgamma.c \ + wf_tgamma.c \ + ) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 686cfd6104df0..666370c983b72 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -8,10 +8,19 @@ #define MICROPY_EMIT_INLINE_THUMB (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) + +#ifndef MICROPY_PY_BUILTINS_COMPLEX #define MICROPY_PY_BUILTINS_COMPLEX (0) +#endif + +#ifndef MICROPY_PY_MATH #define MICROPY_PY_MATH (1) #define MP_NEED_LOG2 (1) +#endif + +#ifndef MICROPY_PY_CMATH #define MICROPY_PY_CMATH (0) +#endif #define MICROPY_PY_MACHINE_DHT_READINTO (1) #define MICROPY_PY_ONEWIRE (1) @@ -30,7 +39,9 @@ unsigned long trng_random_u32(void); #define VFS_BLOCK_SIZE_BYTES (1536) // +#ifndef MICROPY_HW_UART_TXBUF #define MICROPY_HW_UART_TXBUF (1) +#endif #define CPU_FREQ (120000000) #define DFLL48M_FREQ (48000000) From fc9d66fac64422a6a6ea4a79bfd8b2460cc8e9e8 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 15 Sep 2022 15:58:54 +0200 Subject: [PATCH 2776/3533] samd/machine_rtc: Add the machine.RTC class. Methods implemented are: - rtc.init(date) - rtc.datetime([new_date]) - rtc.calibration(value) The presence of this class can be controlled by MICROPY_PY_MACHINE_RTC. If the RTC module is used, the time module uses the RTC as well. For boards without a 32kHz crystal, using RTC makes no sense, since it will then use the ULP32K oscillator, which is not precise at all. Therefore, it will by default only be enabled for boards using a crystal, but can be enabled in the respective mpconfigboard.h. --- ports/samd/Makefile | 1 + ports/samd/fatfs_port.c | 6 +- ports/samd/machine_rtc.c | 181 +++++++++++++++++++++++++++ ports/samd/mcu/samd21/clock_config.c | 17 ++- ports/samd/mcu/samd21/mpconfigmcu.h | 6 + ports/samd/mcu/samd51/clock_config.c | 20 +-- ports/samd/mcu/samd51/mpconfigmcu.h | 6 + ports/samd/modmachine.c | 3 + ports/samd/modmachine.h | 3 + ports/samd/modutime.c | 30 ++++- ports/samd/samd_soc.c | 7 ++ 11 files changed, 263 insertions(+), 17 deletions(-) create mode 100644 ports/samd/machine_rtc.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index abec4e83a5539..e6e592a0352bf 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -94,6 +94,7 @@ SRC_C += \ machine_i2c.c \ machine_led.c \ machine_pin.c \ + machine_rtc.c \ machine_spi.c \ machine_timer.c \ machine_uart.c \ diff --git a/ports/samd/fatfs_port.c b/ports/samd/fatfs_port.c index 9ee1764ebc0a9..a3e3f1b67b3a9 100644 --- a/ports/samd/fatfs_port.c +++ b/ports/samd/fatfs_port.c @@ -33,9 +33,13 @@ extern uint32_t time_offset; MP_WEAK DWORD get_fattime(void) { + #if MICROPY_PY_MACHINE_RTC + return (RTC->MODE2.CLOCK.reg >> 1) + (20 << 25); + #else + extern void rtc_gettime(timeutils_struct_time_t *tm); timeutils_struct_time_t tm; - timeutils_seconds_since_epoch_to_struct_time(mp_hal_ticks_ms_64() / 1000 + time_offset, &tm); return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) | ((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) | ((tm.tm_min) << 5) | (tm.tm_sec / 2); + #endif } diff --git a/ports/samd/machine_rtc.c b/ports/samd/machine_rtc.c new file mode 100644 index 0000000000000..57bfa998e583b --- /dev/null +++ b/ports/samd/machine_rtc.c @@ -0,0 +1,181 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * Copyright (c) 2022 "Robert Hammelrath" + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "shared/timeutils/timeutils.h" +#include "modmachine.h" +#include "py/mphal.h" +#include "sam.h" + +#if MICROPY_PY_MACHINE_RTC + +typedef struct _machine_rtc_obj_t { + mp_obj_base_t base; + mp_obj_t callback; +} machine_rtc_obj_t; + +// Singleton RTC object. +STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; + +// Start the RTC Timer. +void machine_rtc_start(bool force) { + #if defined(MCU_SAMD21) + + if (RTC->MODE2.CTRL.bit.ENABLE == 0 || force) { + // Enable the 1k Clock + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK8 | GCLK_CLKCTRL_ID_RTC; + + RTC->MODE2.CTRL.reg = RTC_MODE2_CTRL_SWRST; + while (RTC->MODE2.STATUS.bit.SYNCBUSY) { + } + RTC->MODE2.CTRL.reg = + RTC_MODE2_CTRL_MODE_CLOCK | + RTC_MODE2_CTRL_PRESCALER_DIV1024 | + RTC_MODE2_CTRL_ENABLE; + while (RTC->MODE2.STATUS.bit.SYNCBUSY) { + } + } + + #elif defined(MCU_SAMD51) + + if (RTC->MODE2.CTRLA.bit.ENABLE == 0 || force) { + RTC->MODE2.CTRLA.reg = RTC_MODE2_CTRLA_SWRST; + while (RTC->MODE2.SYNCBUSY.bit.SWRST) { + } + RTC->MODE2.CTRLA.reg = + RTC_MODE2_CTRLA_MODE_CLOCK | + RTC_MODE2_CTRLA_CLOCKSYNC | + RTC_MODE2_CTRLA_PRESCALER_DIV1024 | + RTC_MODE2_CTRLA_ENABLE; + while (RTC->MODE2.SYNCBUSY.bit.ENABLE) { + } + } + #endif +} + +// Get the time from the RTC and put it into a tm struct. +void rtc_gettime(timeutils_struct_time_t *tm) { + tm->tm_year = RTC->MODE2.CLOCK.bit.YEAR + 2000; + tm->tm_mon = RTC->MODE2.CLOCK.bit.MONTH; + tm->tm_mday = RTC->MODE2.CLOCK.bit.DAY; + tm->tm_hour = RTC->MODE2.CLOCK.bit.HOUR; + tm->tm_min = RTC->MODE2.CLOCK.bit.MINUTE; + tm->tm_sec = RTC->MODE2.CLOCK.bit.SECOND; +} + +STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + // Check arguments. + mp_arg_check_num(n_args, n_kw, 0, 0, false); + // RTC was already started at boot time. So nothing to do here. + // Return constant object. + return (mp_obj_t)&machine_rtc_obj; +} + +STATIC mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) { + // Rtc *rtc = RTC; + if (n_args == 1) { + // Get date and time. + timeutils_struct_time_t tm; + rtc_gettime(&tm); + + mp_obj_t tuple[8] = { + mp_obj_new_int(tm.tm_year), + mp_obj_new_int(tm.tm_mon), + mp_obj_new_int(tm.tm_mday), + mp_obj_new_int(timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday)), + mp_obj_new_int(tm.tm_hour), + mp_obj_new_int(tm.tm_min), + mp_obj_new_int(tm.tm_sec), + mp_obj_new_int(0), + }; + return mp_obj_new_tuple(8, tuple); + } else { + // Set date and time. + mp_obj_t *items; + mp_obj_get_array_fixed_n(args[1], 8, &items); + + uint32_t date = + RTC_MODE2_CLOCK_YEAR(mp_obj_get_int(items[0]) % 100) | + RTC_MODE2_CLOCK_MONTH(mp_obj_get_int(items[1])) | + RTC_MODE2_CLOCK_DAY(mp_obj_get_int(items[2])) | + RTC_MODE2_CLOCK_HOUR(mp_obj_get_int(items[4])) | + RTC_MODE2_CLOCK_MINUTE(mp_obj_get_int(items[5])) | + RTC_MODE2_CLOCK_SECOND(mp_obj_get_int(items[6])); + + RTC->MODE2.CLOCK.reg = date; + #if defined(MCU_SAMD21) + while (RTC->MODE2.STATUS.bit.SYNCBUSY) { + } + #elif defined(MCU_SAMD51) + while (RTC->MODE2.SYNCBUSY.bit.CLOCKSYNC) { + } + #endif + + return mp_const_none; + } +} + +STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { + return machine_rtc_datetime_helper(n_args, args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime); + +STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) { + mp_obj_t args[2] = {self_in, date}; + machine_rtc_datetime_helper(2, args); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init); + +// calibration(cal) +// When the argument is a number in the range [-16 to 15], set the calibration value. +STATIC mp_obj_t machine_rtc_calibration(mp_obj_t self_in, mp_obj_t cal_in) { + int8_t cal = 0; + // Make it negative for a "natural" behavior: + // value > 0: faster, value < 0: slower + cal = -mp_obj_get_int(cal_in); + RTC->MODE2.FREQCORR.reg = (uint8_t)cal; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_calibration_obj, machine_rtc_calibration); + +STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) }, + { MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&machine_rtc_calibration_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_rtc_type, + MP_QSTR_RTC, + MP_TYPE_FLAG_NONE, + make_new, machine_rtc_make_new, + locals_dict, &machine_rtc_locals_dict + ); + +#endif // MICROPY_PY_MACHINE_RTC diff --git a/ports/samd/mcu/samd21/clock_config.c b/ports/samd/mcu/samd21/clock_config.c index a195cb6920536..00f743cc49d15 100644 --- a/ports/samd/mcu/samd21/clock_config.c +++ b/ports/samd/mcu/samd21/clock_config.c @@ -141,7 +141,7 @@ void init_clocks(uint32_t cpu_freq) { // GCLK3: 1Mhz for the us-counter (TC4/TC5) // GCLK4: 32kHz from crystal, if present // GCLK5: 48MHz from DFLL for USB - // GCLK8: 1kHz clock for WDT + // GCLK8: 1kHz clock for WDT and RTC NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes" NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz @@ -203,6 +203,11 @@ void init_clocks(uint32_t cpu_freq) { SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE; while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) { } + // Set GCLK8 to 1 kHz. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(8); + while (GCLK->STATUS.bit.SYNCBUSY) { + } #else // MICROPY_HW_XOSC32K @@ -242,6 +247,11 @@ void init_clocks(uint32_t cpu_freq) { GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(1); while (GCLK->STATUS.bit.SYNCBUSY) { } + // Set GCLK8 to 1 kHz. + GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); + GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); + while (GCLK->STATUS.bit.SYNCBUSY) { + } #endif // MICROPY_HW_XOSC32K @@ -252,11 +262,6 @@ void init_clocks(uint32_t cpu_freq) { GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(3); while (GCLK->STATUS.bit.SYNCBUSY) { } - // Set GCLK8 to 1 kHz. - GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32); - GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8); - while (GCLK->STATUS.bit.SYNCBUSY) { - } } void enable_sercom_clock(int id) { diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index 8d155f93ab032..331df8e2130a0 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -28,6 +28,12 @@ #define MICROPY_HW_UART_TXBUF (1) #endif +#ifndef MICROPY_PY_MACHINE_RTC +#if MICROPY_HW_XOSC32K +#define MICROPY_PY_MACHINE_RTC (1) +#endif +#endif + #define CPU_FREQ (48000000) #define DFLL48M_FREQ (48000000) #define MAX_CPU_FREQ (48000000) diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index 3bc4616de9e9d..c5f508cae8b6f 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -215,16 +215,19 @@ void init_clocks(uint32_t cpu_freq) { #if MICROPY_HW_XOSC32K // OSCILLATOR CONTROL + // Enable the clock for RTC + OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K; // Setup XOSC32K OSC32KCTRL->INTFLAG.reg = OSC32KCTRL_INTFLAG_XOSC32KRDY | OSC32KCTRL_INTFLAG_XOSC32KFAIL; - OSC32KCTRL->XOSC32K.bit.CGM = OSC32KCTRL_XOSC32K_CGM_HS_Val; - OSC32KCTRL->XOSC32K.bit.XTALEN = 1; // 0: Generator 1: Crystal - OSC32KCTRL->XOSC32K.bit.EN32K = 1; - OSC32KCTRL->XOSC32K.bit.ONDEMAND = 0; - OSC32KCTRL->XOSC32K.bit.RUNSTDBY = 1; - OSC32KCTRL->XOSC32K.bit.STARTUP = 4; OSC32KCTRL->CFDCTRL.bit.CFDEN = 1; // Fall back to internal Osc on crystal fail - OSC32KCTRL->XOSC32K.bit.ENABLE = 1; + OSC32KCTRL->XOSC32K.reg = + OSC32KCTRL_XOSC32K_CGM_HS | + OSC32KCTRL_XOSC32K_XTALEN | + OSC32KCTRL_XOSC32K_EN32K | + OSC32KCTRL_XOSC32K_EN1K | + OSC32KCTRL_XOSC32K_RUNSTDBY | + OSC32KCTRL_XOSC32K_STARTUP(4) | + OSC32KCTRL_XOSC32K_ENABLE; // make sure osc32kcrtl is ready while (OSC32KCTRL->STATUS.bit.XOSC32KRDY == 0) { } @@ -270,6 +273,9 @@ void init_clocks(uint32_t cpu_freq) { #else // MICROPY_HW_XOSC32K + // Enable the clock for RTC + OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K; + // Derive GCLK1 from DFLL48M at DPLL0_REF_FREQ as defined in mpconfigboard.h (e.g. 32768 Hz) GCLK->GENCTRL[1].reg = ((DFLL48M_FREQ + DPLLx_REF_FREQ / 2) / DPLLx_REF_FREQ) << GCLK_GENCTRL_DIV_Pos | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 666370c983b72..541fba9009f3a 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -28,6 +28,12 @@ #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); +#ifndef MICROPY_PY_MACHINE_RTC +#if MICROPY_HW_XOSC32K +#define MICROPY_PY_MACHINE_RTC (1) +#endif +#endif + // Due to a limitation in the TC counter for us, the ticks period is 2**29 #define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index ce5fef76f753c..12e9f7c34134c 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -238,6 +238,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #if MICROPY_PY_MACHINE_RTC + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index e99ca990fbba3..8f85e149896f3 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -38,5 +38,8 @@ extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_uart_type; extern const mp_obj_type_t machine_wdt_type; +#if MICROPY_PY_MACHINE_RTC +extern const mp_obj_type_t machine_rtc_type; +#endif #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/modutime.c b/ports/samd/modutime.c index 4169c15d91172..6b04134497fb8 100644 --- a/ports/samd/modutime.c +++ b/ports/samd/modutime.c @@ -29,20 +29,34 @@ #include "shared/timeutils/timeutils.h" #include "mphalport.h" +#if !MICROPY_PY_MACHINE_RTC uint32_t time_offset = 0; +#endif // !MICROPY_PY_MACHINE_RTC // localtime([secs]) STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { timeutils_struct_time_t tm; mp_int_t seconds; + + #if MICROPY_PY_MACHINE_RTC + extern void rtc_gettime(timeutils_struct_time_t *tm); + if (n_args == 0 || args[0] == mp_const_none) { + rtc_gettime(&tm); + } else { + seconds = mp_obj_get_int(args[0]); + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); + } + + #else if (n_args == 0 || args[0] == mp_const_none) { - // seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000; seconds = mp_hal_ticks_ms_64() / 1000 + time_offset; } else { seconds = mp_obj_get_int(args[0]); time_offset = seconds - mp_hal_ticks_ms_64() / 1000; } timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); + + #endif // MICROPY_PY_MACHINE_RTC mp_obj_t tuple[8] = { tuple[0] = mp_obj_new_int(tm.tm_year), tuple[1] = mp_obj_new_int(tm.tm_mon), @@ -50,8 +64,8 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { tuple[3] = mp_obj_new_int(tm.tm_hour), tuple[4] = mp_obj_new_int(tm.tm_min), tuple[5] = mp_obj_new_int(tm.tm_sec), - tuple[6] = mp_obj_new_int(tm.tm_wday), - tuple[7] = mp_obj_new_int(tm.tm_yday), + tuple[6] = mp_obj_new_int(timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday)), + tuple[7] = mp_obj_new_int(timeutils_year_day(tm.tm_year, tm.tm_mon, tm.tm_mday)), }; return mp_obj_new_tuple(8, tuple); } @@ -76,7 +90,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); // time() STATIC mp_obj_t time_time(void) { + #if MICROPY_PY_MACHINE_RTC + extern void rtc_gettime(timeutils_struct_time_t *tm); + timeutils_struct_time_t tm; + rtc_gettime(&tm); + return mp_obj_new_int_from_uint(timeutils_mktime( + tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)); + + #else return mp_obj_new_int_from_uint(mp_hal_ticks_ms_64() / 1000 + time_offset); + + #endif // MICROPY_PY_MACHINE_RTC } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 3608306e87194..bd3eea536fdbb 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -38,6 +38,10 @@ #include "tusb.h" #include "mphalport.h" +#if MICROPY_PY_MACHINE_RTC +extern void machine_rtc_start(bool force); +#endif + static void usb_init(void) { // Init USB clock #if defined(MCU_SAMD21) @@ -114,4 +118,7 @@ void samd_init(void) { #if defined(MCU_SAMD51) mp_hal_ticks_cpu_enable(); #endif + #if MICROPY_PY_MACHINE_RTC + machine_rtc_start(false); + #endif } From e33db80a5958772095a16b0b3753ed3aa72d07b2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 29 Sep 2022 16:13:23 +0200 Subject: [PATCH 2777/3533] samd/clock_config: Extend the SAMD51 us-counter to 60 bit. This removes the difference in the time.ticks_us() range between SAMD21 and SAMD51. The function mp_hal_ticks_us_64() is added and used for: - SAMD51's mp_hal_ticks_us and mp_hal_delay_us(). For SAMD21, keep the previous methods, which are faster. - mp_hal_ticks_ms() and mp_hal_tick_ms_64(), which saves some bytes and removes a potential race condition every 50 days. Also set the us-counter for SAMD51 to 16 MHz for a faster reading of the microsecond value. Note: With SAMD51, mp_hal_ticks_us_64() has a 60 bit range only, which is still a long time (~36000 years). --- ports/samd/mcu/samd51/clock_config.c | 8 +++--- ports/samd/mcu/samd51/mpconfigmcu.h | 3 --- ports/samd/mphalport.c | 40 ++++++++++++++++++++++++---- ports/samd/mphalport.h | 23 +++++----------- ports/samd/samd_isr.c | 24 ++++++++++++----- ports/samd/samd_soc.c | 8 +++++- 6 files changed, 71 insertions(+), 35 deletions(-) diff --git a/ports/samd/mcu/samd51/clock_config.c b/ports/samd/mcu/samd51/clock_config.c index c5f508cae8b6f..b55419d24b67b 100644 --- a/ports/samd/mcu/samd51/clock_config.c +++ b/ports/samd/mcu/samd51/clock_config.c @@ -190,7 +190,7 @@ void init_clocks(uint32_t cpu_freq) { // GCLK0: 48MHz from DFLL48M or 48 - 200 MHz from DPLL0 (SAMD51) // GCLK1: 32768 Hz from 32KULP or DFLL48M // GCLK2: 8-48MHz from DFLL48M for Peripheral devices - // GCLK3: 8Mhz for the us-counter (TC0/TC1) + // GCLK3: 16Mhz for the us-counter (TC0/TC1) // GCLK4: 32kHz from crystal, if present // GCLK5: 48MHz from DFLL48M for USB // DPLL0: 48 - 200 MHz @@ -204,7 +204,7 @@ void init_clocks(uint32_t cpu_freq) { // Setup DPLL0 to 120MHz // Setup GCLK0 to 120MHz // Setup GCLK2 to 48MHz for Peripherals - // Setup GCLK3 to 8MHz for TC0/TC1 + // Setup GCLK3 to 16MHz for TC0/TC1 // Setup GCLK4 to 32kHz crystal, if present // Setup GCLK5 to 48 MHz @@ -320,8 +320,8 @@ void init_clocks(uint32_t cpu_freq) { while (GCLK->SYNCBUSY.bit.GENCTRL2) { } - // Setup GCLK3 for 8MHz, Used for TC0/1 counter - GCLK->GENCTRL[3].reg = GCLK_GENCTRL_DIV(6) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; + // Setup GCLK3 for 16MHz, Used for TC0/1 counter + GCLK->GENCTRL[3].reg = GCLK_GENCTRL_DIV(3) | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL; while (GCLK->SYNCBUSY.bit.GENCTRL3) { } } diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 541fba9009f3a..16b2c101f02b9 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -34,9 +34,6 @@ unsigned long trng_random_u32(void); #endif #endif -// Due to a limitation in the TC counter for us, the ticks period is 2**29 -#define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000) - // fatfs configuration used in ffconf.h #define MICROPY_FATFS_ENABLE_LFN (1) #define MICROPY_FATFS_RPATH (2) diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c index ad3817768f777..b60bada480b46 100644 --- a/ports/samd/mphalport.c +++ b/ports/samd/mphalport.c @@ -37,6 +37,8 @@ #define MICROPY_HW_STDIN_BUFFER_LEN 128 #endif +extern volatile uint32_t ticks_us64_upper; + STATIC uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN]; ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0 }; @@ -111,19 +113,47 @@ void mp_hal_delay_ms(mp_uint_t ms) { void mp_hal_delay_us(mp_uint_t us) { if (us > 0) { - uint32_t start = mp_hal_ticks_us(); #if defined(MCU_SAMD21) - // SAMD21 counter has effective 32 bit width + uint32_t start = mp_hal_ticks_us(); while ((mp_hal_ticks_us() - start) < us) { } - #elif defined(MCU_SAMD51) - // SAMD51 counter has effective 29 bit width - while (((mp_hal_ticks_us() - start) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) < us) { + #else + uint64_t stop = mp_hal_ticks_us_64() + us; + while (mp_hal_ticks_us_64() < stop) { } #endif } } +uint64_t mp_hal_ticks_us_64(void) { + uint32_t us64_upper = ticks_us64_upper; + uint32_t us64_lower; + uint8_t intflag; + __disable_irq(); + #if defined(MCU_SAMD21) + us64_lower = REG_TC4_COUNT32_COUNT; + intflag = TC4->COUNT32.INTFLAG.reg; + #elif defined(MCU_SAMD51) + TC0->COUNT32.CTRLBSET.reg = TC_CTRLBSET_CMD_READSYNC; + while (TC0->COUNT32.CTRLBSET.reg != 0) { + } + us64_lower = REG_TC0_COUNT32_COUNT; + intflag = TC0->COUNT32.INTFLAG.reg; + #endif + __enable_irq(); + if ((intflag & TC_INTFLAG_OVF) && us64_lower < 0x10000000) { + // The timer counter overflowed before reading it but the IRQ handler + // has not yet been called, so perform the IRQ arithmetic now. + us64_upper++; + } + #if defined(MCU_SAMD21) + return ((uint64_t)us64_upper << 32) | us64_lower; + #elif defined(MCU_SAMD51) + return ((uint64_t)us64_upper << 28) | (us64_lower >> 4); + #endif + +} + uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { uintptr_t ret = 0; diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 3879240f9a6a1..2cbb3333f60f6 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -37,7 +37,7 @@ extern int mp_interrupt_char; extern volatile uint32_t systick_ms; -extern volatile uint32_t systick_ms_upper; +uint64_t mp_hal_ticks_us_64(void); void mp_hal_set_interrupt_char(int c); @@ -47,28 +47,19 @@ void mp_hal_set_interrupt_char(int c); #define mp_hal_delay_us_fast mp_hal_delay_us -static inline mp_uint_t mp_hal_ticks_ms(void) { - return systick_ms; +static inline uint64_t mp_hal_ticks_ms_64(void) { + return mp_hal_ticks_us_64() / 1000; } -static inline uint64_t mp_hal_ticks_ms_64(void) { - return ((uint64_t)systick_ms_upper << 32) + systick_ms; +static inline mp_uint_t mp_hal_ticks_ms(void) { + return (mp_uint_t)mp_hal_ticks_ms_64(); } static inline mp_uint_t mp_hal_ticks_us(void) { #if defined(MCU_SAMD21) - return REG_TC4_COUNT32_COUNT; - - #elif defined(MCU_SAMD51) - - TC0->COUNT32.CTRLBSET.reg = TC_CTRLBSET_CMD_READSYNC; - while (TC0->COUNT32.CTRLBSET.reg != 0) { - } - return REG_TC0_COUNT32_COUNT >> 3; - #else - return systick_ms * 1000; + return (mp_uint_t)mp_hal_ticks_us_64(); #endif } @@ -89,7 +80,7 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) { #endif static inline uint64_t mp_hal_time_ns(void) { - return mp_hal_ticks_ms_64() * 1000000; + return mp_hal_ticks_us_64() * 1000; } // C-level pin HAL diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index b507d5d1aa737..1d6febaaa226b 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -41,7 +41,7 @@ extern void EIC_Handler(void); const ISR isr_vector[]; volatile uint32_t systick_ms; -volatile uint32_t systick_ms_upper; +volatile uint32_t ticks_us64_upper; void Reset_Handler(void) __attribute__((naked)); void Reset_Handler(void) { @@ -93,15 +93,27 @@ void Default_Handler(void) { void SysTick_Handler(void) { uint32_t next_tick = systick_ms + 1; systick_ms = next_tick; - if (systick_ms == 0) { - systick_ms_upper += 1; - } if (soft_timer_next == next_tick) { pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler); } } +void us_timer_IRQ(void) { + #if defined(MCU_SAMD21) + if (TC4->COUNT32.INTFLAG.reg & TC_INTFLAG_OVF) { + ticks_us64_upper++; + } + TC4->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF; + #elif defined(MCU_SAMD51) + if (TC0->COUNT32.INTFLAG.reg & TC_INTFLAG_OVF) { + ticks_us64_upper++; + } + TC0->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF; + #endif +} + +// Sercom IRQ handler support void (*sercom_irq_handler_table[SERCOM_INST_NUM])(int num) = {}; void sercom_register_irq(int sercom_id, void (*sercom_irq_handler)) { @@ -180,7 +192,7 @@ const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { 0, // 16 Timer Counter Control 1 (TCC1) 0, // 17 Timer Counter Control 2 (TCC2) 0, // 18 Basic Timer Counter 3 (TC3) - 0, // 19 Basic Timer Counter 4 (TC4) + &us_timer_IRQ, // 19 Basic Timer Counter 4 (TC4) 0, // 20 Basic Timer Counter 5 (TC5) 0, // 21 Basic Timer Counter 6 (TC6) 0, // 22 Basic Timer Counter 7 (TC7) @@ -316,7 +328,7 @@ const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { 0, // 104 Timer Counter Control 4 (TCC4): TCC4_CNT_A ... 0, // 105 Timer Counter Control 4 (TCC4): TCC4_MC_0 0, // 106 Timer Counter Control 4 (TCC4): TCC4_MC_1 - 0, // 107 Basic Timer Counter 0 (TC0) + &us_timer_IRQ, // 107 Basic Timer Counter 0 (TC0) 0, // 108 Basic Timer Counter 1 (TC1) 0, // 109 Basic Timer Counter 2 (TC2) 0, // 110 Basic Timer Counter 3 (TC3) diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index bd3eea536fdbb..a81e7c68810da 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -68,7 +68,7 @@ static void usb_init(void) { tusb_init(); } -// Initialize the microsecond counter on TC 0/1 +// Initialize the µs counter on TC 0/1 or TC4/5 void init_us_counter(void) { #if defined(MCU_SAMD21) @@ -89,6 +89,9 @@ void init_us_counter(void) { TC4->COUNT32.READREQ.reg = TC_READREQ_RREQ | TC_READREQ_RCONT | 0x10; while (TC4->COUNT32.STATUS.bit.SYNCBUSY) { } + // Enable the IRQ + TC4->COUNT32.INTENSET.reg = TC_INTENSET_OVF; + NVIC_EnableIRQ(TC4_IRQn); #elif defined(MCU_SAMD51) @@ -107,6 +110,9 @@ void init_us_counter(void) { while (TC0->COUNT32.SYNCBUSY.bit.ENABLE) { } + // Enable the IRQ + TC0->COUNT32.INTENSET.reg = TC_INTENSET_OVF; + NVIC_EnableIRQ(TC0_IRQn); #endif } From e7aa9700cad4ace55fb9100c98b2bbfa811870d2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 4 Oct 2022 13:39:43 +0200 Subject: [PATCH 2778/3533] samd/boards/SEEED_WIO_TERMINAL: Declare more pins for SEEED WIO board. Defining all pins from the external 40 Pin connector, and some internal pins like the one for SD and LCD. --- ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv index 72da71224a4d4..9bc57070d1985 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv @@ -22,9 +22,43 @@ PIN_PD20,SWITCH_U PIN_PC26,BUTTON_1 PIN_PC27,BUTTON_2 PIN_PC28,BUTTON_3 -PIN_PD11,BUZZER_CTR PIN_PC14,5V_ENABLE PIN_PC15,3V3_ENABLE +PIN_PB26,TX +PIN_PB27,RX +PIN_PA13,SDA0 +PIN_PA12,SCL0 +PIN_PA17,SDA1 +PIN_PA16,SCL1 +PIN_PB02,MOSI +PIN_PB00,MISO +PIN_PB03,SCK +PIN_PB01,CS +PIN_PB15,GPCLK0 +PIN_PB12,GPCLK1 +PIN_PB13,GPCLK2 +PIN_PC16,SD_MOSI +PIN_PC18,SD_MISO +PIN_PC17,SD_SCK +PIN_PC19,SD_CS +PIN_PD21,SD_DET +PIN_PA20,I2S_LRCLK +PIN_PA21,I2S_SDIN +PIN_PA22,I2S_SDOUT +PIN_PB16,I2C_BCLK +PIN_PB18,LCD_MISO +PIN_PB19,LCD_MOSI +PIN_PB20,LCD_SCK +PIN_PB21,LCD_CS +PIN_PC05,LCD_BACKLIGHT +PIN_PC06,LCD_D/C +PIN_PC07,LCD_RESET +PIN_PC10,LCD_XL +PIN_PC11,LCD_YU +PIN_PC12,LCD_XR +PIN_PC13,LCD_YD +PIN_PC30,MIC +PIN_PD11,BUZZER LED_PA15,LED_BLUE LED_PC05,LED_LCD From e5cf3fab951adf39e7d39c2a8600b61f84e517ce Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 17 Sep 2022 17:27:27 +0200 Subject: [PATCH 2779/3533] samd/machine_pin: Change the pin handling and naming/numbering. Pin numbers are now the MCU port numbers in the range: PA0..PA31: 0..31 PB0..PB31: 32..63 PC0..PC31: 64..95 PD0..PD31: 96..127 Pins can be denoted by the GPIO port number, the name as defined in pins.csv or a string in the form Pxnn, like "PA16" or "PD03". The pins.c and pins.h files are now obsolete. The pin objects are part of the AF table. As result of a simplification, the code now supports using pin names or numbers instead of pin objects for modules like UART, SPI, PWM, I2C, ADC, pininfo. --- ports/samd/Makefile | 26 +--- .../{make-pin-af.py => make-pin-table.py} | 54 +++++++- ports/samd/boards/make-pins.py | 128 ------------------ ports/samd/machine_adc.c | 4 +- ports/samd/machine_led.c | 27 ++-- ports/samd/machine_pin.c | 119 ++++++---------- ports/samd/machine_pwm.c | 4 +- ports/samd/modsamd.c | 60 ++++---- ports/samd/pin_af.c | 69 +++++++++- ports/samd/pin_af.h | 18 ++- 10 files changed, 221 insertions(+), 288 deletions(-) rename ports/samd/boards/{make-pin-af.py => make-pin-table.py} (59%) delete mode 100644 ports/samd/boards/make-pins.py diff --git a/ports/samd/Makefile b/ports/samd/Makefile index e6e592a0352bf..f512fb0b2e339 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -47,14 +47,10 @@ INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include/pio INC += -I$(TOP)/lib/tinyusb/src -MAKE_PIN_AF = boards/make-pin-af.py +MAKE_PIN_AF = boards/make-pin-table.py PIN_AF_TABLE_CSV = mcu/$(MCU_SERIES_LOWER)/pin-af-table.csv -GEN_PIN_AF = pin_af_table.c - -MAKE_PINS = boards/make-pins.py BOARD_PINS = $(BOARD_DIR)/pins.csv -GEN_PINS_SRC = $(BUILD)/pins.c -GEN_PINS_HDR = $(BUILD)/pins.h +GEN_PIN_AF = pin_af_table.c CFLAGS_MCU_SAMD21 = -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard @@ -150,7 +146,7 @@ DRIVERS_SRC_C += \ drivers/bus/softspi.c \ # List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(SRC_CXX) $(GEN_PINS_SRC) +SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(SRC_CXX) OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) @@ -161,7 +157,6 @@ OBJ += $(addprefix $(BUILD)/, $(ASF4_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o)) -OBJ += $(GEN_PINS_SRC:.c=.o) ifneq ($(FROZEN_MANIFEST),) CFLAGS += -DMICROPY_MODULE_FROZEN_MPY @@ -182,19 +177,10 @@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin $(Q)$(PYTHON) $(UF2CONV) -b $(TEXT0) -c -o $@ $< -pin_af.c: $(BUILD)/$(GEN_PIN_AF) +pin_af.c: $(BUILD)/$(GEN_PIN_AF) | $(HEADER_BUILD) -$(BUILD)/$(GEN_PIN_AF): $(PIN_AF_TABLE_CSV) | $(HEADER_BUILD) +$(BUILD)/$(GEN_PIN_AF): $(PIN_AF_TABLE_CSV) $(BOARD_PINS) | $(HEADER_BUILD) $(ECHO) "Create $@" - $(Q)$(PYTHON) $(MAKE_PIN_AF) --csv $(PIN_AF_TABLE_CSV) --table $(BUILD)/$(GEN_PIN_AF) --mcu $(MCU_SERIES) - -machine_led.c machine_pin.c modsamd.c: $(GEN_PINS_HDR) - -$(GEN_PINS_SRC) $(GEN_PINS_HDR): $(BOARD_PINS) | $(HEADER_BUILD) - $(ECHO) "Create $@" - $(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --pins $(GEN_PINS_SRC) --inc $(GEN_PINS_HDR) - -$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) - $(call compile_c) + $(Q)$(PYTHON) $(MAKE_PIN_AF) --csv $(PIN_AF_TABLE_CSV) --board $(BOARD_PINS) --table $(BUILD)/$(GEN_PIN_AF) --mcu $(MCU_SERIES) include $(TOP)/py/mkrules.mk diff --git a/ports/samd/boards/make-pin-af.py b/ports/samd/boards/make-pin-table.py similarity index 59% rename from ports/samd/boards/make-pin-af.py rename to ports/samd/boards/make-pin-table.py index d895ef9dd99a4..327478568afa9 100644 --- a/ports/samd/boards/make-pin-af.py +++ b/ports/samd/boards/make-pin-table.py @@ -16,6 +16,7 @@ class Pins: def __init__(self): self.board_pins = [] # list of pin objects + self.pin_names = {} def parse_csv_file(self, filename): with open(filename, "r") as csvfile: @@ -25,17 +26,43 @@ def parse_csv_file(self, filename): if len(row) > 0 and row[0].strip().upper()[:2] in ("PA", "PB", "PC", "PD"): self.board_pins.append(row) + def parse_pin_file(self, filename): + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile, skipinitialspace=True) + for row in rows: + # Pin numbers must start with "PIN_" + # LED numbers must start with "LED_" + if len(row) > 0: + # for compatibility, map LED_ to PIN_ + if row[0].startswith("LED_"): + row[0] = "PIN_" + row[0][4:] + if len(row) == 1: + self.pin_names[row[0]] = (row[0][4:], "{&machine_led_type}") + else: + self.pin_names[row[0]] = (row[1], "{&machine_led_type}") + elif row[0].startswith("PIN_"): + if len(row) == 1: + self.pin_names[row[0]] = (row[0][4:], "{&machine_pin_type}") + else: + self.pin_names[row[0]] = (row[1], "{&machine_pin_type}") + def print_table(self, table_filename, mcu_name): with open(table_filename, "wt") as table_file: table_file.write(table_header) - table_file.write("const pin_af_t pin_af_table[] = {\n") + table_file.write("const machine_pin_obj_t pin_af_table[] = {\n") if mcu_name == "SAMD21": for row in self.board_pins: pin = "PIN_" + row[0].upper() table_file.write(" #ifdef " + pin + "\n") eic = row[1] if row[1] else "0xff" adc = row[2] if row[2] else "0xff" - table_file.write(" {%s, %s, %s" % (pin, eic, adc)) + if pin in self.pin_names: + name = '"%s"' % self.pin_names[pin][0] + type = self.pin_names[pin][1] + else: + name = '"-"' + type = "{&machine_pin_type}" + table_file.write(" {%s, %s, %s, %s, %s" % (type, pin, name, eic, adc)) for cell in row[3:]: if cell: table_file.write( @@ -52,7 +79,15 @@ def print_table(self, table_filename, mcu_name): eic = row[1] if row[1] else "0xff" adc0 = row[2] if row[2] else "0xff" adc1 = row[3] if row[3] else "0xff" - table_file.write(" {%s, %s, %s, %s" % (pin, eic, adc0, adc1)) + if pin in self.pin_names: + name = '"%s"' % self.pin_names[pin][0] + type = self.pin_names[pin][1] + else: + name = '"-"' + type = "{&machine_pin_type}" + table_file.write( + " {%s, %s, %s, %s, %s, %s" % (type, pin, name, eic, adc0, adc1) + ) for cell in row[4:]: if cell: table_file.write( @@ -67,7 +102,7 @@ def print_table(self, table_filename, mcu_name): def main(): parser = argparse.ArgumentParser( - prog="make-pin-cap.py", + prog="make-pin-af.py", usage="%(prog)s [options] [command]", description="Generate MCU-specific pin cap table file", ) @@ -75,7 +110,13 @@ def main(): "-c", "--csv", dest="csv_filename", - help="Specifies the pin-mux-xxxx.csv filename", + help="Specifies the pin-af-table.csv filename", + ) + parser.add_argument( + "-b", + "--board", + dest="pin_filename", + help="Specifies the pins.csv filename", ) parser.add_argument( "-t", @@ -96,6 +137,9 @@ def main(): if args.csv_filename: pins.parse_csv_file(args.csv_filename) + if args.pin_filename: + pins.parse_pin_file(args.pin_filename) + if args.table_filename: pins.print_table(args.table_filename, args.mcu_name) diff --git a/ports/samd/boards/make-pins.py b/ports/samd/boards/make-pins.py deleted file mode 100644 index 679e2c9d192cb..0000000000000 --- a/ports/samd/boards/make-pins.py +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/env python -"""Generates the pins file for the SAMD port.""" - -from __future__ import print_function - -import argparse -import sys -import csv - -pins_header_prefix = """// This file was automatically generated by make-pins.py -// -typedef struct _machine_pin_obj_t { - mp_obj_base_t base; - uint32_t id; - char *name; -} machine_pin_obj_t; - -int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size); - -""" - -led_header_prefix = """typedef struct _machine_led_obj_t { - mp_obj_base_t base; - uint32_t id; - char *name; -} machine_led_obj_t; - -""" - - -class Pins: - def __init__(self): - self.board_pins = [] # list of pin objects - self.board_leds = [] # list of led objects - - def parse_csv_file(self, filename): - with open(filename, "r") as csvfile: - rows = csv.reader(csvfile, skipinitialspace=True) - for row in rows: - # Pin numbers must start with "PIN_" - # LED numbers must start with "LED_" - if len(row) > 0: - if row[0].startswith("PIN_"): - if len(row) == 1: - self.board_pins.append([row[0], row[0][4:]]) - else: - self.board_pins.append([row[0], row[1]]) - elif row[0].startswith("LED_"): - self.board_leds.append(["PIN_" + row[0][4:], row[1]]) - elif row[0].startswith("-"): - self.board_pins.append(["-1", ""]) - - def print_pins(self, pins_filename): - with open(pins_filename, "wt") as pins_file: - pins_file.write("// This file was automatically generated by make-pins.py\n") - pins_file.write("//\n") - pins_file.write('#include "modmachine.h"\n') - pins_file.write('#include "sam.h"\n') - pins_file.write('#include "pins.h"\n\n') - - pins_file.write("const machine_pin_obj_t machine_pin_obj[] = {\n") - for pin in self.board_pins: - pins_file.write(" {{&machine_pin_type}, ") - pins_file.write(pin[0] + ', "' + pin[1]) - pins_file.write('"},\n') - pins_file.write("};\n") - - if self.board_leds: - pins_file.write("\nconst machine_led_obj_t machine_led_obj[] = {\n") - for pin in self.board_leds: - pins_file.write(" {{&machine_led_type}, ") - pins_file.write(pin[0] + ', "' + pin[1]) - pins_file.write('"},\n') - pins_file.write("};\n") - - def print_header(self, hdr_filename): - with open(hdr_filename, "wt") as hdr_file: - hdr_file.write(pins_header_prefix) - if self.board_leds: - hdr_file.write(led_header_prefix) - hdr_file.write( - "extern const machine_pin_obj_t machine_pin_obj[%d];\n" % len(self.board_pins) - ) - if self.board_leds: - hdr_file.write( - "extern const machine_led_obj_t machine_led_obj[%d];\n" % len(self.board_leds) - ) - - -def main(): - parser = argparse.ArgumentParser( - prog="make-pins.py", - usage="%(prog)s [options] [command]", - description="Generate board specific pin file", - ) - parser.add_argument( - "-b", - "--board", - dest="csv_filename", - help="Specifies the pins.csv filename", - ) - parser.add_argument( - "-p", - "--pins", - dest="pins_filename", - help="Specifies the name of the generated pins.c file", - ) - parser.add_argument( - "-i", - "--inc", - dest="hdr_filename", - help="Specifies name of generated pin header file", - ) - args = parser.parse_args(sys.argv[1:]) - - pins = Pins() - - if args.csv_filename: - pins.parse_csv_file(args.csv_filename) - - if args.pins_filename: - pins.print_pins(args.pins_filename) - - pins.print_header(args.hdr_filename) - - -if __name__ == "__main__": - main() diff --git a/ports/samd/machine_adc.c b/ports/samd/machine_adc.c index 97b6a14f17a49..efe0f041cdfa5 100644 --- a/ports/samd/machine_adc.c +++ b/ports/samd/machine_adc.c @@ -66,8 +66,8 @@ STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t k (void)kind; machine_adc_obj_t *self = MP_OBJ_TO_PTR(o); - mp_printf(print, "ADC(P%c%02u, ADC%u, channel=%u, bits=%u, average=%u)", - "ABCD"[self->id / 32], self->id % 32, self->adc_config.device, + mp_printf(print, "ADC(%s, ADC%u, channel=%u, bits=%u, average=%u)", + pin_name(self->id), self->adc_config.device, self->adc_config.channel, self->bits, 1 << self->avg); } diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c index 54c2cb18bc86f..c18bc052bd2f3 100644 --- a/ports/samd/machine_led.c +++ b/ports/samd/machine_led.c @@ -30,7 +30,7 @@ #include "py/mphal.h" #include "extmod/virtpin.h" #include "modmachine.h" -#include "pins.h" +#include "pin_af.h" extern mp_obj_t machine_pin_low_obj; extern mp_obj_t machine_pin_high_obj; @@ -38,8 +38,10 @@ extern mp_obj_t machine_pin_toggle_obj; extern mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - machine_led_obj_t *self = self_in; - mp_printf(print, "LED(\"%s\")", self->name); + machine_pin_obj_t *self = self_in; + mp_printf(print, "LED(\"%s\", GPIO=P%c%02u)", + pin_name(self->pin_id), + "ABCD"[self->pin_id / 32], self->pin_id % 32); } // constructor(id, ...) @@ -47,23 +49,16 @@ mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted LED object - int wanted_led = pin_find(args[0], (const machine_pin_obj_t *)machine_led_obj, MP_ARRAY_SIZE(machine_led_obj)); - const machine_led_obj_t *self = NULL; - if (0 <= wanted_led && wanted_led < MP_ARRAY_SIZE(machine_led_obj)) { - self = (machine_led_obj_t *)&machine_led_obj[wanted_led]; - } - // the array could be padded with 'nulls' (see other Ports). - // Will also error if the asked for LED (index) is greater than the array row size. - if (self == NULL || self->base.type == NULL) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid LED")); - } - mp_hal_pin_output(self->id); - mp_hal_pin_low(self->id); + const machine_pin_obj_t *self; + + self = pin_find(args[0], &machine_led_type); + + mp_hal_pin_output(self->pin_id); + mp_hal_pin_low(self->pin_id); return MP_OBJ_FROM_PTR(self); } - STATIC const mp_rom_map_elem_t machine_led_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_low_obj) }, diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index 2dc10f9b46344..a7cd86d386ed9 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -34,7 +34,6 @@ #include "extmod/virtpin.h" #include "modmachine.h" #include "samd_soc.h" -#include "pins.h" #include "pin_af.h" #include "hal_gpio.h" @@ -68,17 +67,17 @@ STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin machine_pin_obj_t *self = self_in; char *mode_str; char *pull_str[] = {"PULL_OFF", "PULL_UP", "PULL_DOWN"}; - if (GPIO_IS_OPEN_DRAIN(self->id)) { + if (GPIO_IS_OPEN_DRAIN(self->pin_id)) { mode_str = "OPEN_DRAIN"; } else { - mode_str = (mp_hal_get_pin_direction(self->id) == GPIO_DIRECTION_OUT) ? "OUT" : "IN"; + mode_str = (mp_hal_get_pin_direction(self->pin_id) == GPIO_DIRECTION_OUT) ? "OUT" : "IN"; } mp_printf(print, "Pin(\"%s\", mode=%s, pull=%s, GPIO=P%c%02u)", - self->name, + pin_name(self->pin_id), mode_str, - pull_str[mp_hal_get_pull_mode(self->id)], - "ABCD"[self->id / 32], self->id % 32); + pull_str[mp_hal_get_pull_mode(self->pin_id)], + "ABCD"[self->pin_id / 32], self->pin_id % 32); } STATIC void pin_validate_drive(bool strength) { @@ -87,25 +86,6 @@ STATIC void pin_validate_drive(bool strength) { } } -int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size) { - int wanted_pin = -1; - if (mp_obj_is_small_int(pin)) { - // Pin defined by the index of pin table - wanted_pin = mp_obj_get_int(pin); - } else if (mp_obj_is_str(pin)) { - // Search by name - size_t slen; - const char *s = mp_obj_str_get_data(pin, &slen); - for (wanted_pin = 0; wanted_pin < table_size; wanted_pin++) { - if (slen == strlen(machine_pin_obj[wanted_pin].name) && - strncmp(s, machine_pin_obj[wanted_pin].name, slen) == 0) { - break; - } - } - } - return wanted_pin; -} - // Pin.init(mode, pull=None, *, value=None, drive=0). No 'alt' yet. STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt }; @@ -120,32 +100,34 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + // clear any existing mux setting + mp_hal_clr_pin_mux(self->pin_id); // set initial value (do this before configuring mode/pull) if (args[ARG_value].u_obj != mp_const_none) { - mp_hal_pin_write(self->id, mp_obj_is_true(args[ARG_value].u_obj)); + mp_hal_pin_write(self->pin_id, mp_obj_is_true(args[ARG_value].u_obj)); } // configure mode if (args[ARG_mode].u_obj != mp_const_none) { mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); if (mode == GPIO_MODE_IN) { - mp_hal_pin_input(self->id); + mp_hal_pin_input(self->pin_id); } else if (mode == GPIO_MODE_OUT) { - mp_hal_pin_output(self->id); + mp_hal_pin_output(self->pin_id); } else if (mode == GPIO_MODE_OPEN_DRAIN) { - mp_hal_pin_open_drain(self->id); + mp_hal_pin_open_drain(self->pin_id); } else { - mp_hal_pin_input(self->id); // If no args are given, the Pin is 'input'. + mp_hal_pin_input(self->pin_id); // If no args are given, the Pin is 'input'. } } // configure pull. Only to be used with IN mode. The function sets the pin to INPUT. uint32_t pull = 0; - mp_int_t dir = mp_hal_get_pin_direction(self->id); + mp_int_t dir = mp_hal_get_pin_direction(self->pin_id); if (dir == GPIO_DIRECTION_OUT && args[ARG_pull].u_obj != mp_const_none) { mp_raise_ValueError(MP_ERROR_TEXT("OUT incompatible with pull")); } else if (args[ARG_pull].u_obj != mp_const_none) { pull = mp_obj_get_int(args[ARG_pull].u_obj); - gpio_set_pin_pull_mode(self->id, pull); // hal_gpio.h + gpio_set_pin_pull_mode(self->pin_id, pull); // hal_gpio.h } // get the strength @@ -158,18 +140,10 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ // constructor(id, ...) mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + const machine_pin_obj_t *self; // get the wanted pin object - int wanted_pin = pin_find(args[0], machine_pin_obj, MP_ARRAY_SIZE(machine_pin_obj)); - - const machine_pin_obj_t *self = NULL; - if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) { - self = (machine_pin_obj_t *)&machine_pin_obj[wanted_pin]; - } - - if (self == NULL || self->base.type == NULL) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid pin")); - } + self = pin_find(args[0], &machine_pin_type); if (n_args > 1 || n_kw > 0) { // pin mode given, so configure this GPIO @@ -187,18 +161,18 @@ mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp machine_pin_obj_t *self = self_in; if (n_args == 0) { // get pin - return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self->id)); + return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self->pin_id)); } else { // set pin bool value = mp_obj_is_true(args[0]); - if (GPIO_IS_OPEN_DRAIN(self->id)) { + if (GPIO_IS_OPEN_DRAIN(self->pin_id)) { if (value == 0) { - mp_hal_pin_od_low(self->id); + mp_hal_pin_od_low(self->pin_id); } else { - mp_hal_pin_od_high(self->id); + mp_hal_pin_od_high(self->pin_id); } } else { - mp_hal_pin_write(self->id, value); + mp_hal_pin_write(self->pin_id, value); } return mp_const_none; } @@ -219,7 +193,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_ // Pin.disable(pin) STATIC mp_obj_t machine_pin_disable(mp_obj_t self_in) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - gpio_set_pin_direction(self->id, GPIO_DIRECTION_OFF); // Disables the pin (low power state) + gpio_set_pin_direction(self->pin_id, GPIO_DIRECTION_OFF); // Disables the pin (low power state) return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_disable_obj, machine_pin_disable); @@ -227,10 +201,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_disable_obj, machine_pin_disable); // Pin.low() Totem-pole (push-pull) STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (GPIO_IS_OPEN_DRAIN(self->id)) { - mp_hal_pin_od_low(self->id); + if (GPIO_IS_OPEN_DRAIN(self->pin_id)) { + mp_hal_pin_od_low(self->pin_id); } else { - mp_hal_pin_low(self->id); + mp_hal_pin_low(self->pin_id); } return mp_const_none; } @@ -239,10 +213,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); // Pin.high() Totem-pole (push-pull) STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (GPIO_IS_OPEN_DRAIN(self->id)) { - mp_hal_pin_od_high(self->id); + if (GPIO_IS_OPEN_DRAIN(self->pin_id)) { + mp_hal_pin_od_high(self->pin_id); } else { - mp_hal_pin_high(self->id); + mp_hal_pin_high(self->pin_id); } return mp_const_none; } @@ -255,16 +229,16 @@ STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { // Determine DIRECTION of PIN. bool pin_dir; - if (GPIO_IS_OPEN_DRAIN(self->id)) { - pin_dir = mp_hal_get_pin_direction(self->id); + if (GPIO_IS_OPEN_DRAIN(self->pin_id)) { + pin_dir = mp_hal_get_pin_direction(self->pin_id); if (pin_dir) { // Pin is output, thus low, switch to high - mp_hal_pin_od_high(self->id); + mp_hal_pin_od_high(self->pin_id); } else { - mp_hal_pin_od_low(self->id); + mp_hal_pin_od_low(self->pin_id); } } else { - gpio_toggle_pin_level(self->id); + gpio_toggle_pin_level(self->pin_id); } return mp_const_none; } @@ -280,8 +254,8 @@ STATIC mp_obj_t machine_pin_drive(size_t n_args, const mp_obj_t *args) { pin_validate_drive(strength); // Set the DRVSTR bit (ASF hri/hri_port_dxx.h hri_port_write_PINCFG_DRVSTR_bit(PORT, - (enum gpio_port)GPIO_PORT(self->id), - GPIO_PIN(self->id), + (enum gpio_port)GPIO_PORT(self->pin_id), + GPIO_PIN(self->pin_id), strength); return mp_const_none; } @@ -301,9 +275,9 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // Get the IRQ object. - uint8_t eic_id = get_pin_af_info(self->id)->eic; + uint8_t eic_id = get_pin_obj_ptr(self->pin_id)->eic; machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); - if (irq != NULL && irq->pin_id != self->id) { + if (irq != NULL && irq->pin_id != self->pin_id) { mp_raise_ValueError(MP_ERROR_TEXT("IRQ already used")); } @@ -322,7 +296,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ if (n_args > 1 || kw_args->used != 0) { // set the mux config of the pin. - mp_hal_set_pin_mux(self->id, ALT_FCT_EIC); + mp_hal_set_pin_mux(self->pin_id, ALT_FCT_EIC); // Configure IRQ. #if defined(MCU_SAMD21) @@ -362,7 +336,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ irq->base.ishard = args[ARG_hard].u_bool; irq->flags = 0; irq->trigger = args[ARG_trigger].u_int; - irq->pin_id = self->id; + irq->pin_id = self->pin_id; // Enable IRQ if a handler is given. if (args[ARG_handler].u_obj != mp_const_none) { @@ -457,10 +431,10 @@ STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, i switch (request) { case MP_PIN_READ: { - return mp_hal_pin_read(self->id); + return mp_hal_pin_read(self->pin_id); } case MP_PIN_WRITE: { - mp_hal_pin_write(self->id, arg); + mp_hal_pin_write(self->pin_id, arg); return 0; } } @@ -494,7 +468,7 @@ static uint8_t find_eic_id(int pin) { STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint8_t eic_id = find_eic_id(self->id); + uint8_t eic_id = find_eic_id(self->pin_id); if (eic_id != 0xff) { machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); EIC->INTENCLR.reg |= (1 << eic_id); @@ -507,7 +481,7 @@ STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) { machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint8_t eic_id = find_eic_id(self->id); + uint8_t eic_id = find_eic_id(self->pin_id); if (eic_id != 0xff) { machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[eic_id]); if (info_type == MP_IRQ_INFO_FLAGS) { @@ -525,11 +499,8 @@ STATIC const mp_irq_methods_t machine_pin_irq_methods = { }; mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { - if (!mp_obj_is_type(obj, &machine_pin_type)) { - mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin")); - } - machine_pin_obj_t *pin = MP_OBJ_TO_PTR(obj); - return pin->id; + const machine_pin_obj_t *pin = pin_find(obj, &machine_pin_type); + return pin->pin_id; } MP_REGISTER_ROOT_POINTER(void *machine_pin_irq_objects[16]); diff --git a/ports/samd/machine_pwm.c b/ports/samd/machine_pwm.c index d987927d310c3..91982a6f74a8f 100644 --- a/ports/samd/machine_pwm.c +++ b/ports/samd/machine_pwm.c @@ -107,8 +107,8 @@ STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "PWM P%c%02u device=%u channel=%u output=%u", - "ABCD"[self->pin_id / 32], self->pin_id % 32, self->device, self->channel, self->output); + mp_printf(print, "PWM %s device=%u channel=%u output=%u", + pin_name(self->pin_id), self->device, self->channel, self->output); } // PWM(pin) diff --git a/ports/samd/modsamd.c b/ports/samd/modsamd.c index 05ed69bad014b..79e7b4cc35f1c 100644 --- a/ports/samd/modsamd.c +++ b/ports/samd/modsamd.c @@ -30,42 +30,40 @@ #include "sam.h" #include "pin_af.h" -#include "pins.h" #include "samd_soc.h" extern const mp_obj_type_t samd_flash_type; STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) { - mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_obj); - const pin_af_t *pin_af = get_pin_af_info(pin); - const char *name = ((machine_pin_obj_t *)MP_OBJ_TO_PTR(pin_obj))->name; - if (pin_af) { - #if defined(MCU_SAMD21) - mp_obj_t tuple[7] = { - tuple[0] = mp_obj_new_str(name, strlen(name)), - tuple[1] = mp_obj_new_int(pin_af->eic), - tuple[2] = mp_obj_new_int(pin_af->adc0), - tuple[3] = mp_obj_new_int(pin_af->sercom1), - tuple[4] = mp_obj_new_int(pin_af->sercom2), - tuple[5] = mp_obj_new_int(pin_af->tcc1), - tuple[6] = mp_obj_new_int(pin_af->tcc2), - }; - return mp_obj_new_tuple(7, tuple); - #elif defined(MCU_SAMD51) - mp_obj_t tuple[9] = { - tuple[0] = mp_obj_new_str(name, strlen(name)), - tuple[1] = mp_obj_new_int(pin_af->eic), - tuple[2] = mp_obj_new_int(pin_af->adc0), - tuple[3] = mp_obj_new_int(pin_af->adc1), - tuple[4] = mp_obj_new_int(pin_af->sercom1), - tuple[5] = mp_obj_new_int(pin_af->sercom2), - tuple[6] = mp_obj_new_int(pin_af->tc), - tuple[7] = mp_obj_new_int(pin_af->tcc1), - tuple[8] = mp_obj_new_int(pin_af->tcc2), - }; - return mp_obj_new_tuple(9, tuple); - #endif - } + const machine_pin_obj_t *pin_af = pin_find(pin_obj, NULL); + // Get the name, now that it is not in the pin object + const char *name = pin_af->name; + + #if defined(MCU_SAMD21) + mp_obj_t tuple[7] = { + tuple[0] = mp_obj_new_str(name, strlen(name)), + tuple[1] = mp_obj_new_int(pin_af->eic), + tuple[2] = mp_obj_new_int(pin_af->adc0), + tuple[3] = mp_obj_new_int(pin_af->sercom1), + tuple[4] = mp_obj_new_int(pin_af->sercom2), + tuple[5] = mp_obj_new_int(pin_af->tcc1), + tuple[6] = mp_obj_new_int(pin_af->tcc2), + }; + return mp_obj_new_tuple(7, tuple); + #elif defined(MCU_SAMD51) + mp_obj_t tuple[9] = { + tuple[0] = mp_obj_new_str(name, strlen(name)), + tuple[1] = mp_obj_new_int(pin_af->eic), + tuple[2] = mp_obj_new_int(pin_af->adc0), + tuple[3] = mp_obj_new_int(pin_af->adc1), + tuple[4] = mp_obj_new_int(pin_af->sercom1), + tuple[5] = mp_obj_new_int(pin_af->sercom2), + tuple[6] = mp_obj_new_int(pin_af->tc), + tuple[7] = mp_obj_new_int(pin_af->tcc1), + tuple[8] = mp_obj_new_int(pin_af->tcc2), + }; + return mp_obj_new_tuple(9, tuple); + #endif return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_pininfo_obj, samd_pininfo); diff --git a/ports/samd/pin_af.c b/ports/samd/pin_af.c index 926c4ae0c23aa..fc2a0f3d54d93 100644 --- a/ports/samd/pin_af.c +++ b/ports/samd/pin_af.c @@ -29,7 +29,9 @@ */ #include +#include "string.h" +#include "modmachine.h" #include "py/runtime.h" #include "py/misc.h" #include "pin_af.h" @@ -43,13 +45,65 @@ extern const uint8_t tcc_channel_count[]; // Just look for an table entry for a given pin and raise an error // in case of no match (which should not happen). -const pin_af_t *get_pin_af_info(int pin_id) { +const machine_pin_obj_t *get_pin_obj_ptr(int pin_id) { for (int i = 0; i < MP_ARRAY_SIZE(pin_af_table); i++) { if (pin_af_table[i].pin_id == pin_id) { // Pin match return &pin_af_table[i]; } } - mp_raise_ValueError(MP_ERROR_TEXT("wrong pin")); + mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); +} + +const machine_pin_obj_t *pin_find(mp_obj_t pin, const mp_obj_type_t *type) { + const machine_pin_obj_t *self = NULL; + // Is already a object of the proper type + if (mp_obj_is_type(pin, type)) { + return pin; + } + if (mp_obj_is_small_int(pin)) { + // Pin defined by pin number for PAnn, PBnn, etc. + self = get_pin_obj_ptr(mp_obj_get_int(pin)); + } else if (mp_obj_is_str(pin)) { + // Search by name + size_t slen; + const char *s = mp_obj_str_get_data(pin, &slen); + // Check for a string like PA02 or PD12 + if (slen == 4 && s[0] == 'P' && strchr("ABCD", s[1]) != NULL && + strchr("0123456789", s[2]) != NULL && strchr("0123456789", s[2]) != NULL) { + int num = (s[1] - 'A') * 32 + (s[2] - '0') * 10 + (s[3] - '0'); + self = get_pin_obj_ptr(num); + } else { + for (int i = 0; i < MP_ARRAY_SIZE(pin_af_table); i++) { + if (slen == strlen(pin_af_table[i].name) && + strncmp(s, pin_af_table[i].name, slen) == 0) { + self = &pin_af_table[i]; + } + } + } + } + if (self != NULL && (type == NULL || mp_obj_is_type(self, type))) { + return self; + } else { + mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); + } +} + +const char *pin_name(int id) { + static char board_name[5] = "Pxnn"; + for (int i = 0; i < sizeof(pin_af_table); i++) { + if (pin_af_table[i].pin_id == id) { + if (pin_af_table[i].name[0] != '-') { + return pin_af_table[i].name; + } else { + board_name[1] = "ABCD"[id / 32]; + id %= 32; + board_name[2] = '0' + id / 10; + board_name[3] = '0' + id % 10; + return board_name; + } + } + } + return "-"; } // Test, wether the given pin is defined and has signals for sercom. @@ -57,7 +111,7 @@ const pin_af_t *get_pin_af_info(int pin_id) { // If not, an error will be raised. sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom_nr) { - const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + const machine_pin_obj_t *pct_ptr = get_pin_obj_ptr(pin_id); if ((pct_ptr->sercom1 >> 4) == sercom_nr) { return (sercom_pad_config_t) {ALT_FCT_SERCOM1, pct_ptr->sercom1 & 0x0f}; } else if ((pct_ptr->sercom2 >> 4) == sercom_nr) { @@ -72,7 +126,12 @@ sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom_nr) { // If not, an error will be raised. adc_config_t get_adc_config(int pin_id, int32_t flag) { - const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + const machine_pin_obj_t *pct_ptr = get_pin_obj_ptr(pin_id); + #if defined(MCU_SAMD51) + if (pct_ptr->adc1 != 0xff && (flag & (1 << (pct_ptr->adc1 + 16))) == 0) { + return (adc_config_t) {1, pct_ptr->adc1}; + } else + #endif if (pct_ptr->adc0 != 0xff && (flag & (1 << pct_ptr->adc0)) == 0) { return (adc_config_t) {0, pct_ptr->adc0}; #if defined(MUC_SAMD51) @@ -91,7 +150,7 @@ adc_config_t get_adc_config(int pin_id, int32_t flag) { // tries to provide an unused device, if available. pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t device_status[]) { - const pin_af_t *pct_ptr = get_pin_af_info(pin_id); + const machine_pin_obj_t *pct_ptr = get_pin_obj_ptr(pin_id); uint8_t tcc1 = pct_ptr->tcc1; uint8_t tcc2 = pct_ptr->tcc2; diff --git a/ports/samd/pin_af.h b/ports/samd/pin_af.h index b75f4ddd02562..5bb65a098a65c 100644 --- a/ports/samd/pin_af.h +++ b/ports/samd/pin_af.h @@ -30,15 +30,17 @@ #if defined(MCU_SAMD21) -typedef struct { +typedef struct _machine_pin_obj_t { + mp_obj_base_t base; uint8_t pin_id; + char *name; uint8_t eic; uint8_t adc0; uint8_t sercom1; uint8_t sercom2; uint8_t tcc1; uint8_t tcc2; -} pin_af_t; +} machine_pin_obj_t; #define ALT_FCT_TC 4 #define ALT_FCT_TCC1 4 @@ -46,8 +48,10 @@ typedef struct { #elif defined(MCU_SAMD51) -typedef struct { +typedef struct _machine_pin_obj_t { + mp_obj_base_t base; uint8_t pin_id; + char *name; uint8_t eic; uint8_t adc0; uint8_t adc1; @@ -56,7 +60,7 @@ typedef struct { uint8_t tc; uint8_t tcc1; uint8_t tcc2; -} pin_af_t; +} machine_pin_obj_t; #define ALT_FCT_TC 4 #define ALT_FCT_TCC1 5 @@ -85,7 +89,11 @@ typedef struct _pwm_config_t { #define ALT_FCT_SERCOM1 2 #define ALT_FCT_SERCOM2 3 +extern const machine_pin_obj_t pin_af_table[]; + sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom); adc_config_t get_adc_config(int pin_id, int32_t flag); pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t used_dev[]); -const pin_af_t *get_pin_af_info(int pin_id); +const machine_pin_obj_t *get_pin_obj_ptr(int pin_id); +const char *pin_name(int id); +const machine_pin_obj_t *pin_find(mp_obj_t pin, const mp_obj_type_t *type); From 4d38ab652ea57a60c6f2ff085547f72012ac7df6 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 7 Oct 2022 09:40:45 +0200 Subject: [PATCH 2780/3533] samd: Make ADC, DAC, PWM, SPI objects consistent in how they print out. All of ADC, DAC, Pin, PWM and SPI looked different before this change. --- ports/samd/machine_adc.c | 2 +- ports/samd/machine_dac.c | 2 +- ports/samd/machine_pwm.c | 2 +- ports/samd/machine_spi.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/samd/machine_adc.c b/ports/samd/machine_adc.c index efe0f041cdfa5..8fd3e3bf4e7fa 100644 --- a/ports/samd/machine_adc.c +++ b/ports/samd/machine_adc.c @@ -66,7 +66,7 @@ STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t k (void)kind; machine_adc_obj_t *self = MP_OBJ_TO_PTR(o); - mp_printf(print, "ADC(%s, ADC%u, channel=%u, bits=%u, average=%u)", + mp_printf(print, "ADC(%s, device=%u, channel=%u, bits=%u, average=%u)", pin_name(self->id), self->adc_config.device, self->adc_config.channel, self->bits, 1 << self->avg); } diff --git a/ports/samd/machine_dac.c b/ports/samd/machine_dac.c index 41c6784cceb8b..53407c0d375aa 100644 --- a/ports/samd/machine_dac.c +++ b/ports/samd/machine_dac.c @@ -126,7 +126,7 @@ STATIC mp_obj_t dac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ STATIC void dac_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { dac_obj_t *self = self_in; - mp_printf(print, "DAC(%u) PIN_PA%02u", self->id, self->gpio_id); + mp_printf(print, "DAC(%u, Pin=%s)", self->id, pin_name(self->gpio_id)); } STATIC mp_obj_t dac_write(mp_obj_t self_in, mp_obj_t value_in) { diff --git a/ports/samd/machine_pwm.c b/ports/samd/machine_pwm.c index 91982a6f74a8f..2ae841a9743ba 100644 --- a/ports/samd/machine_pwm.c +++ b/ports/samd/machine_pwm.c @@ -107,7 +107,7 @@ STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "PWM %s device=%u channel=%u output=%u", + mp_printf(print, "PWM(%s, device=%u, channel=%u, output=%u)", pin_name(self->pin_id), self->device, self->channel, self->output); } diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c index 8321f19cf0dd8..43150f67f15a3 100644 --- a/ports/samd/machine_spi.c +++ b/ports/samd/machine_spi.c @@ -82,7 +82,7 @@ void common_spi_irq_handler(int spi_id) { STATIC void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "SPI(%u), baudrate=%u, firstbit=%u, polarity=%u, phase=%u, bits=8", + mp_printf(print, "SPI(%u, baudrate=%u, firstbit=%u, polarity=%u, phase=%u, bits=8)", self->id, self->baudrate, self->firstbit, self->polarity, self->phase); } From a6760bd4efa883ef463795036ff74d678ff65c7b Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 9 Oct 2022 10:56:29 +0200 Subject: [PATCH 2781/3533] samd/modmachine: Replace the LED class by the Signal class. It simplifies and improves the code. The LED_Pxxx lines of the board.csv lines can still be used, but will be taken as Pin definitions. --- ports/samd/Makefile | 1 - ports/samd/boards/make-pin-table.py | 6 +-- ports/samd/machine_led.c | 78 ----------------------------- ports/samd/machine_pin.c | 4 +- ports/samd/modmachine.c | 3 +- ports/samd/modmachine.h | 1 - ports/samd/modsamd.c | 2 +- ports/samd/mpconfigport.h | 1 + ports/samd/pin_af.c | 6 +-- ports/samd/pin_af.h | 2 +- 10 files changed, 11 insertions(+), 93 deletions(-) delete mode 100644 ports/samd/machine_led.c diff --git a/ports/samd/Makefile b/ports/samd/Makefile index f512fb0b2e339..b2e84460ea266 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -88,7 +88,6 @@ SRC_C += \ machine_bitstream.c \ machine_dac.c \ machine_i2c.c \ - machine_led.c \ machine_pin.c \ machine_rtc.c \ machine_spi.c \ diff --git a/ports/samd/boards/make-pin-table.py b/ports/samd/boards/make-pin-table.py index 327478568afa9..40bcd166dd08f 100644 --- a/ports/samd/boards/make-pin-table.py +++ b/ports/samd/boards/make-pin-table.py @@ -36,11 +36,7 @@ def parse_pin_file(self, filename): # for compatibility, map LED_ to PIN_ if row[0].startswith("LED_"): row[0] = "PIN_" + row[0][4:] - if len(row) == 1: - self.pin_names[row[0]] = (row[0][4:], "{&machine_led_type}") - else: - self.pin_names[row[0]] = (row[1], "{&machine_led_type}") - elif row[0].startswith("PIN_"): + if row[0].startswith("PIN_"): if len(row) == 1: self.pin_names[row[0]] = (row[0][4:], "{&machine_pin_type}") else: diff --git a/ports/samd/machine_led.c b/ports/samd/machine_led.c deleted file mode 100644 index c18bc052bd2f3..0000000000000 --- a/ports/samd/machine_led.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * This is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016-2021 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Uses pins.h & pins.c to create board (MCU package) specific 'machine_led_obj' array. - */ - -#include "py/runtime.h" -#include "py/mphal.h" -#include "extmod/virtpin.h" -#include "modmachine.h" -#include "pin_af.h" - -extern mp_obj_t machine_pin_low_obj; -extern mp_obj_t machine_pin_high_obj; -extern mp_obj_t machine_pin_toggle_obj; -extern mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); - -STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - machine_pin_obj_t *self = self_in; - mp_printf(print, "LED(\"%s\", GPIO=P%c%02u)", - pin_name(self->pin_id), - "ABCD"[self->pin_id / 32], self->pin_id % 32); -} - -// constructor(id, ...) -mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - - // get the wanted LED object - const machine_pin_obj_t *self; - - self = pin_find(args[0], &machine_led_type); - - mp_hal_pin_output(self->pin_id); - mp_hal_pin_low(self->pin_id); - - return MP_OBJ_FROM_PTR(self); -} - -STATIC const mp_rom_map_elem_t machine_led_locals_dict_table[] = { - // instance methods - { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_low_obj) }, - { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_high_obj) }, - { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_led_locals_dict, machine_led_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_led_type, - MP_QSTR_LED, - MP_TYPE_FLAG_NONE, - make_new, mp_led_make_new, - print, machine_led_print, - call, machine_pin_call, - locals_dict, &machine_led_locals_dict - ); diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index a7cd86d386ed9..87684858325fb 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -143,7 +143,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const machine_pin_obj_t *self; // get the wanted pin object - self = pin_find(args[0], &machine_pin_type); + self = pin_find(args[0]); if (n_args > 1 || n_kw > 0) { // pin mode given, so configure this GPIO @@ -499,7 +499,7 @@ STATIC const mp_irq_methods_t machine_pin_irq_methods = { }; mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { - const machine_pin_obj_t *pin = pin_find(obj, &machine_pin_type); + const machine_pin_obj_t *pin = pin_find(obj); return pin->pin_id; } diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 12e9f7c34134c..47fc03d824100 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -29,6 +29,7 @@ #include "extmod/machine_mem.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" +#include "extmod/machine_signal.h" #include "extmod/machine_spi.h" #include "drivers/dht/dht.h" #include "modmachine.h" @@ -228,8 +229,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, - { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index 8f85e149896f3..792fca1b92920 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -31,7 +31,6 @@ extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_i2c_type; -extern const mp_obj_type_t machine_led_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_pwm_type; extern const mp_obj_type_t machine_spi_type; diff --git a/ports/samd/modsamd.c b/ports/samd/modsamd.c index 79e7b4cc35f1c..5a2cf4616d1d1 100644 --- a/ports/samd/modsamd.c +++ b/ports/samd/modsamd.c @@ -35,7 +35,7 @@ extern const mp_obj_type_t samd_flash_type; STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) { - const machine_pin_obj_t *pin_af = pin_find(pin_obj, NULL); + const machine_pin_obj_t *pin_af = pin_find(pin_obj); // Get the name, now that it is not in the pin object const char *name = pin_af->name; diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 8b8305f0b47e7..f03f8a11d7649 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -100,6 +100,7 @@ #define MICROPY_PY_MACHINE_PWM_INIT (0) #define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1) #define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/samd/machine_pwm.c" +#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/samd/pin_af.c b/ports/samd/pin_af.c index fc2a0f3d54d93..a7a72ac96abe3 100644 --- a/ports/samd/pin_af.c +++ b/ports/samd/pin_af.c @@ -54,10 +54,10 @@ const machine_pin_obj_t *get_pin_obj_ptr(int pin_id) { mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); } -const machine_pin_obj_t *pin_find(mp_obj_t pin, const mp_obj_type_t *type) { +const machine_pin_obj_t *pin_find(mp_obj_t pin) { const machine_pin_obj_t *self = NULL; // Is already a object of the proper type - if (mp_obj_is_type(pin, type)) { + if (mp_obj_is_type(pin, &machine_pin_type)) { return pin; } if (mp_obj_is_small_int(pin)) { @@ -81,7 +81,7 @@ const machine_pin_obj_t *pin_find(mp_obj_t pin, const mp_obj_type_t *type) { } } } - if (self != NULL && (type == NULL || mp_obj_is_type(self, type))) { + if (self != NULL) { return self; } else { mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); diff --git a/ports/samd/pin_af.h b/ports/samd/pin_af.h index 5bb65a098a65c..4b0c80250cd56 100644 --- a/ports/samd/pin_af.h +++ b/ports/samd/pin_af.h @@ -96,4 +96,4 @@ adc_config_t get_adc_config(int pin_id, int32_t flag); pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t used_dev[]); const machine_pin_obj_t *get_pin_obj_ptr(int pin_id); const char *pin_name(int id); -const machine_pin_obj_t *pin_find(mp_obj_t pin, const mp_obj_type_t *type); +const machine_pin_obj_t *pin_find(mp_obj_t pin); From ac1e31267b0bf21caa31b8136208c116b1612789 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 8 Oct 2022 21:12:50 +0200 Subject: [PATCH 2782/3533] samd/boards: Rework the pins.csv files. Changes are: - Remove the LED_Pxxx definitions from pins.csv, now that the LED class is gone. - Remove the '-' lines. - Add default lines for USB and SWCLK, SWDIO. --- .../boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv | 11 ++++++++--- .../boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv | 10 ++++++---- .../boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv | 8 +++++--- .../boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv | 9 ++++++--- ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv | 8 +++++++- ports/samd/boards/MINISAM_M4/pins.csv | 9 ++++++++- ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv | 15 +++++++-------- ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv | 11 +++++++++-- ports/samd/boards/SEEED_XIAO/pins.csv | 12 +++++++++--- .../boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv | 15 +++++++++------ 10 files changed, 74 insertions(+), 34 deletions(-) diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv index 0985ee1646d1f..45b05c2694f5b 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M0_EXPRESS/pins.csv @@ -5,6 +5,9 @@ # Rows for empty entries have to start with '-' # Empty lines and lines starting with # are ignored +PIN_PA27,LED_TX +PIN_PB03,LED_RX + PIN_PA11,D0 PIN_PA10,D1 PIN_PA14,D2 @@ -35,6 +38,8 @@ PIN_PB11,SCK PIN_PA06,NEOPIXEL PIN_PA13,FLASH_CS -LED_PA17,LED -LED_PA27,LED_TX -LED_PB03,LED_RX +PIN_PA24,USB_DM +PIN_PA25,USB_DP + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv index 32daac3d0f4c7..ca58a4be79f6b 100644 --- a/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_FEATHER_M4_EXPRESS/pins.csv @@ -7,12 +7,9 @@ PIN_PB17,D0 PIN_PB16,D1 -- -- PIN_PA14,D4 PIN_PA16,D5 PIN_PA18,D6 -- PIN_PB03,D8 PIN_PA19,D9 PIN_PA20,D10 @@ -40,4 +37,9 @@ PIN_PA09,FLASH_MISO PIN_PA10,FLASH_WP PIN_PA11,FLASH_HOLD -LED_PA17,LED +PIN_PA24,USB_DM +PIN_PA25,USB_DP +PIN_PA26,USB_SOF + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv index d7d59c23575da..e93e00ab9c944 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M0_EXPRESS/pins.csv @@ -11,9 +11,7 @@ PIN_PA14,D2 PIN_PA09,D3 PIN_PA08,D4 PIN_PA15,D5 -- PIN_PA21,D7 -- PIN_PA07,D9 PIN_PA18,D10 PIN_PA16,D11 @@ -37,4 +35,8 @@ PIN_PB03,FLASH_MISO PIN_PB23,FLASH_SCK PIN_PA27,FLASH_CS -LED_PA17,LED +PIN_PA24,USB_DM +PIN_PA25,USB_DP + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv index ce760a269c243..3dee9bf4c35d9 100644 --- a/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv +++ b/ports/samd/boards/ADAFRUIT_ITSYBITSY_M4_EXPRESS/pins.csv @@ -11,9 +11,7 @@ PIN_PA07,D2 PIN_PB22,D3 PIN_PA14,D4 PIN_PA15,D5 -- PIN_PA18,D7 -- PIN_PA19,D9 PIN_PA20,D10 PIN_PA21,D11 @@ -39,4 +37,9 @@ PIN_PA09,FLASH_MISO PIN_PA10,FLASH_WP PIN_PA11,FLASH_HOLD -LED_PA22,LED +PIN_PA24,USB_DM +PIN_PA25,USB_DP +PIN_PA26,USB_SOF + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv index 797b3f70bb921..95f76815f9808 100644 --- a/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv +++ b/ports/samd/boards/ADAFRUIT_TRINKET_M0/pins.csv @@ -13,4 +13,10 @@ PIN_PA06,D4 PIN_PA00,DOTSTAR_DATA PIN_PA01,DOTSTAR_CLK -LED_PA10,LED +PIN_PA10,LED + +PIN_PA24,USB_DM +PIN_PA25,USB_DP + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/MINISAM_M4/pins.csv b/ports/samd/boards/MINISAM_M4/pins.csv index cf9a3bd19b214..f6d0ab657fefb 100644 --- a/ports/samd/boards/MINISAM_M4/pins.csv +++ b/ports/samd/boards/MINISAM_M4/pins.csv @@ -27,4 +27,11 @@ PIN_PA01,SCK PIN_PB03,DOTSTAR_DATA PIN_PB02,DOTSTAR_CLK -LED_PA15,LED +PIN_PA15,LED + +PIN_PA24,USB_DM +PIN_PA25,USB_DP +PIN_PA26,USB_SOF + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv index 438119d90c1a8..bd0757d5647c0 100644 --- a/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv +++ b/ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv @@ -32,8 +32,6 @@ PIN_PB12,EXT2_PIN7 PIN_PB13,EXT2_PIN8 PIN_PB14,EXT2_PIN9 PIN_PB15,EXT2_PIN10 -- -- PIN_PB11,EXT2_PIN13 PIN_PB10,EXT2_PIN14 PIN_PA17,EXT2_PIN15 @@ -44,20 +42,21 @@ PIN_PA19,EXT2_PIN18 # EXT3 PIN_PA02,EXT3_PIN3 PIN_PA03,EXT3_PIN4 -- PIN_PA15,EXT3_PIN6 PIN_PA12,EXT3_PIN7 PIN_PA13,EXT3_PIN8 PIN_PA28,EXT3_PIN9 PIN_PA27,EXT3_PIN10 -- -- -- -- PIN_PB17,EXT3_PIN15 PIN_PB22,EXT3_PIN16 PIN_PB16,EXT3_PIN17 PIN_PB23,EXT3_PIN18 -LED_PB30,LED +PIN_PB30,LED + +PIN_PA24,USB_DM +PIN_PA25,USB_DP + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv index 9bc57070d1985..4a3d9e21aee0a 100644 --- a/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv +++ b/ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv @@ -60,5 +60,12 @@ PIN_PC13,LCD_YD PIN_PC30,MIC PIN_PD11,BUZZER -LED_PA15,LED_BLUE -LED_PC05,LED_LCD +PIN_PA15,LED_BLUE +PIN_PC05,LED_LCD + +PIN_PA24,USB_DM +PIN_PA25,USB_DP +PIN_PA26,USB_SOF + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/SEEED_XIAO/pins.csv b/ports/samd/boards/SEEED_XIAO/pins.csv index 8a70a77145639..a06d3ac6ac606 100644 --- a/ports/samd/boards/SEEED_XIAO/pins.csv +++ b/ports/samd/boards/SEEED_XIAO/pins.csv @@ -17,6 +17,12 @@ PIN_PA07,A8_D8 PIN_PA05,A9_D9 PIN_PA06,A10_D10 -LED_PA17,USER_LED -LED_PA18,RX_LED -LED_PA19,TX_LED +PIN_PA17,USER_LED +PIN_PA18,RX_LED +PIN_PA19,TX_LED + +PIN_PA24,USB_DM +PIN_PA25,USB_DP + +PIN_PA30,SWCLK +PIN_PA31,SWDIO diff --git a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv index b60fb909714e1..dfd6641333fa3 100644 --- a/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv +++ b/ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/pins.csv @@ -13,7 +13,6 @@ PIN_PA06,D4 PIN_PA15,D5 PIN_PA20,D6 PIN_PA21,D7 -- PIN_PA07,D9 PIN_PA18,D10 PIN_PA16,D11 @@ -34,9 +33,13 @@ PIN_PA08,FLASH_MOSI PIN_PA09,FLASH_SCK PIN_PA10,FLASH_CS PIN_PA11,FLASH_MISO -PIN_PA30,SWDCLK -PIN_PA31,SWDIO -LED_PA17,LED -LED_PB03,RXLED -LED_PA27,TXLED +PIN_PB03,RXLED +PIN_PA27,TXLED + +PIN_PA24,USB_DM +PIN_PA25,USB_DP +PIN_PA26,USB_SOF + +PIN_PA30,SWCLK +PIN_PA31,SWDIO From 474233c250536612a5245231aff1ef3ad74ccf20 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 11 Oct 2022 10:05:01 +0200 Subject: [PATCH 2783/3533] samd/machine_pwm: Serialize fast update of PWM settings. Any update of freq or duty_cycle requires the previous PWM cycle to be finished. Otherwise the new settings are not accepted. Other changes in this commit: - Report the set duty cycles even when the PWM is not yet started. - pwm.freq(0) stops the pwm device, instead of raising an expception. - Clear the duty cycle value cache on soft reset. --- ports/samd/machine_pwm.c | 51 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/ports/samd/machine_pwm.c b/ports/samd/machine_pwm.c index 2ae841a9743ba..f6b417631e1de 100644 --- a/ports/samd/machine_pwm.c +++ b/ports/samd/machine_pwm.c @@ -25,6 +25,7 @@ * THE SOFTWARE. */ +#include #include "py/runtime.h" #include "py/mphal.h" #include "modmachine.h" @@ -46,6 +47,8 @@ typedef struct _machine_pwm_obj_t { uint8_t output; uint16_t prescaler; uint32_t period; // full period count ticks + uint32_t duty_ns; // just for reporting + uint16_t duty_u16; // just for reporting } machine_pwm_obj_t; #define PWM_NOT_INIT (0) @@ -53,6 +56,7 @@ typedef struct _machine_pwm_obj_t { #define PWM_TCC_ENABLED (2) #define PWM_MASTER_CLK (get_peripheral_freq()) #define PWM_FULL_SCALE (65536) +#define PWM_UPDATE_TIMEOUT (2000) static Tcc *tcc_instance[] = TCC_INSTS; @@ -148,6 +152,7 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args self->output = config.device_channel & 0x0f; self->prescaler = 1; self->period = 1; // Use an invalid but safe value + self->duty_u16 = self->duty_ns = 0; put_duty_value(self->device, self->channel, 0); Tcc *tcc = self->instance; @@ -240,6 +245,7 @@ void pwm_deinit_all(void) { device_status[i] = PWM_NOT_INIT; duty_type_flags[i] = 0; output_active[i] = 0; + memset(pwm_duty_values, 0, sizeof(pwm_duty_values)); } } @@ -256,8 +262,25 @@ STATIC void mp_machine_pwm_deinit(machine_pwm_obj_t *self) { } } +STATIC void wait_for_register_update(Tcc *tcc) { + // Wait for a period's end (may be long) to have the change settled + // Each loop cycle takes at least 1 ms, giving an implicit timeout. + for (int i = 0; i < PWM_UPDATE_TIMEOUT; i++) { + if (tcc->INTFLAG.reg & TCC_INTFLAG_OVF) { + break; + } + MICROPY_EVENT_POLL_HOOK + } + // Clear the flag, telling that a cycle has been handled. + tcc->INTFLAG.reg = TCC_INTFLAG_OVF; +} + STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) { - return MP_OBJ_NEW_SMALL_INT(PWM_MASTER_CLK / self->prescaler / self->period); + if (self->instance->CTRLA.reg & TCC_CTRLA_ENABLE) { + return MP_OBJ_NEW_SMALL_INT(PWM_MASTER_CLK / self->prescaler / self->period); + } else { + return MP_OBJ_NEW_SMALL_INT(0); + } } STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { @@ -267,7 +290,8 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { Tcc *tcc = self->instance; if (freq < 1) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid freq")); + pwm_stop_device(self->device); + return; } // Get the actual settings of prescaler & period from the unit @@ -288,6 +312,11 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { if (period < 2) { mp_raise_ValueError(MP_ERROR_TEXT("freq too large")); } + // If the PWM is running, ensure that a cycle has passed since the + // previous setting before setting a new frequency/duty value + if (tcc->CTRLA.reg & TCC_CTRLA_ENABLE) { + wait_for_register_update(tcc); + } // Check, if the prescaler has to be changed and stop the device if so. if (index != tcc->CTRLA.bit.PRESCALER) { // stop the device @@ -339,25 +368,37 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { } STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) { - return MP_OBJ_NEW_SMALL_INT(self->instance->CC[self->channel].reg * PWM_FULL_SCALE / (self->instance->PER.reg + 1)); + return MP_OBJ_NEW_SMALL_INT(self->duty_u16); } STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u16) { + // Remember the values for update & reporting put_duty_value(self->device, self->channel, duty_u16); + self->duty_u16 = duty_u16; + self->duty_ns = 0; // If the device is enabled, than the period is set and we get a reasonable value for // the duty cycle, set to the CCBUF register. Otherwise, PWM does not start. if (self->instance->CTRLA.reg & TCC_CTRLA_ENABLE) { - self->instance->CCBUF[self->channel].reg = (uint64_t)duty_u16 * (self->instance->PER.reg + 1) / PWM_FULL_SCALE; + // Ensure that a cycle has passed updating the registers + // since the previous setting before setting a new duty value + wait_for_register_update(self->instance); + self->instance->CCBUF[self->channel].reg = (uint64_t)duty_u16 * (self->period) / PWM_FULL_SCALE; } duty_type_flags[self->device] |= 1 << self->channel; } STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) { - return MP_OBJ_NEW_SMALL_INT(1000000000ULL * self->instance->CC[self->channel].reg * self->prescaler / PWM_MASTER_CLK); + return MP_OBJ_NEW_SMALL_INT(self->duty_ns); } STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns) { + // Remember the values for update & reporting put_duty_value(self->device, self->channel, duty_ns); + self->duty_ns = duty_ns; + self->duty_u16 = 0; + // Ensure that a cycle has passed updating the registers + // since the previous setting before setting a new duty value + wait_for_register_update(self->instance); self->instance->CCBUF[self->channel].reg = (uint64_t)duty_ns * PWM_MASTER_CLK / self->prescaler / 1000000000ULL; duty_type_flags[self->device] &= ~(1 << self->channel); } From 9c2bc379f183ff9f3280842eb236e8f4b8635835 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 8 Oct 2022 12:32:55 +0200 Subject: [PATCH 2784/3533] samd/machine_uart: Use a finaliser to tidy up UART on soft reset. And use the common sercom_table, saving a few bytes of RAM. --- ports/samd/machine_uart.c | 22 ++++++++-------------- ports/samd/main.c | 2 -- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 76b564e874a66..2526b450931bc 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -59,7 +59,7 @@ typedef struct _machine_uart_obj_t { } machine_uart_obj_t; Sercom *sercom_instance[] = SERCOM_INSTS; -machine_uart_obj_t *uart_table[SERCOM_INST_NUM] = {}; +extern void *sercom_table[SERCOM_INST_NUM]; STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3 @@ -82,7 +82,7 @@ STATIC void uart_drain_rx_fifo(machine_uart_obj_t *self, Sercom *uart) { } void common_uart_irq_handler(int uart_id) { - machine_uart_obj_t *self = uart_table[uart_id]; + machine_uart_obj_t *self = sercom_table[uart_id]; // Handle IRQ if (self != NULL) { Sercom *uart = sercom_instance[self->id]; @@ -322,7 +322,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, } // Create the UART object and fill it with defaults. - machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type); + machine_uart_obj_t *self = m_new_obj_with_finaliser(machine_uart_obj_t); + self->base.type = &machine_uart_type; self->id = uart_id; self->baudrate = DEFAULT_UART_BAUDRATE; self->bits = 8; @@ -332,7 +333,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->tx = 0xff; self->rx = 0xff; self->new = true; - uart_table[uart_id] = self; + sercom_table[uart_id] = self; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); @@ -348,10 +349,10 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init); STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); Sercom *uart = sercom_instance[self->id]; - // clear table entry of uart - uart_table[self->id] = NULL; // Disable interrupts uart->USART.INTENCLR.reg = 0xff; + // clear table entry of uart + sercom_table[self->id] = NULL; MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = NULL; #if MICROPY_HW_UART_TXBUF MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = NULL; @@ -409,14 +410,6 @@ STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone); -void uart_deinit_all(void) { - for (int i = 0; i < SERCOM_INST_NUM; i++) { - if (uart_table[i] != NULL) { - machine_uart_deinit((mp_obj_t)uart_table[i]); - } - } -} - STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) }, @@ -430,6 +423,7 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_uart_deinit_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table); diff --git a/ports/samd/main.c b/ports/samd/main.c index 0f24e2382ee89..725fd651d02c2 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -39,7 +39,6 @@ extern void adc_deinit_all(void); extern void pin_irq_deinit_all(void); extern void pwm_deinit_all(void); extern void sercom_deinit_all(void); -extern void uart_deinit_all(void); void samd_main(void) { mp_stack_set_top(&_estack); @@ -86,7 +85,6 @@ void samd_main(void) { pin_irq_deinit_all(); pwm_deinit_all(); sercom_deinit_all(); - uart_deinit_all(); soft_timer_deinit(); gc_sweep_all(); mp_deinit(); From d74215a3137da5e9001aed92c8c1b06d828ff9d2 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 8 Oct 2022 12:34:45 +0200 Subject: [PATCH 2785/3533] samd/machine_spi: Implement spi.deinit() and simplify sercom_deinit_all. The sercom_deinit_all() function does not need the object pointers. --- ports/samd/machine_spi.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c index 43150f67f15a3..d4c7a05bb60f1 100644 --- a/ports/samd/machine_spi.c +++ b/ports/samd/machine_spi.c @@ -254,17 +254,23 @@ STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, s return self; } +STATIC void machine_sercom_deinit(mp_obj_base_t *self_in) { + machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + Sercom *spi = sercom_instance[self->id]; + // Disable interrupts (if any) + spi->SPI.INTENCLR.reg = 0xff; + sercom_enable(spi, 0); + // clear table entry of spi + sercom_table[self->id] = NULL; +} + void sercom_deinit_all(void) { for (int i = 0; i < SERCOM_INST_NUM; i++) { - if (sercom_table[i] != NULL) { - machine_spi_obj_t *self = sercom_table[i]; - Sercom *spi = sercom_instance[self->id]; - // Disable interrupts (if any) - spi->SPI.INTENCLR.reg = 0xff; - // clear table entry of spi - sercom_table[i] = NULL; - sercom_enable(spi, 0); - } + Sercom *spi = sercom_instance[i]; + spi->SPI.INTENCLR.reg = 0xff; + sercom_register_irq(i, NULL); + sercom_enable(spi, 0); + sercom_table[i] = NULL; } } @@ -316,6 +322,7 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8 STATIC const mp_machine_spi_p_t machine_spi_p = { .init = machine_spi_init, + .deinit = machine_sercom_deinit, .transfer = machine_spi_transfer, }; From a1eebc507eda8f5ec052d0ef0450c52f2320e106 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 13 Oct 2022 19:04:37 +0200 Subject: [PATCH 2786/3533] samd/machine_spi: Register SerCom objects as root pointers. Protect SerCom (UART, SPI, I2C) objects from getting freed by the GC when they go out of scope without being deinitialized. Otherwise the ISR of a Sercom may access an invalid data structure. --- ports/samd/machine_i2c.c | 5 ++--- ports/samd/machine_spi.c | 10 +++++----- ports/samd/machine_uart.c | 7 +++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/ports/samd/machine_i2c.c b/ports/samd/machine_i2c.c index 0a8f5b94db081..d943b6b2f4221 100644 --- a/ports/samd/machine_i2c.c +++ b/ports/samd/machine_i2c.c @@ -69,7 +69,6 @@ typedef struct _machine_i2c_obj_t { } machine_i2c_obj_t; extern Sercom *sercom_instance[]; -extern void *sercom_table[SERCOM_INST_NUM]; STATIC void i2c_send_command(Sercom *i2c, uint8_t command) { i2c->I2CM.CTRLB.bit.CMD = command; @@ -79,7 +78,7 @@ STATIC void i2c_send_command(Sercom *i2c, uint8_t command) { void common_i2c_irq_handler(int i2c_id) { // handle Sercom I2C IRQ - machine_i2c_obj_t *self = sercom_table[i2c_id]; + machine_i2c_obj_t *self = MP_STATE_PORT(sercom_table[i2c_id]); // Handle IRQ if (self != NULL) { Sercom *i2c = self->instance; @@ -159,7 +158,7 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) { mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for sda or scl")); } - sercom_table[self->id] = self; + MP_STATE_PORT(sercom_table[self->id]) = self; self->freq = args[ARG_freq].u_int; // Configure the Pin mux. diff --git a/ports/samd/machine_spi.c b/ports/samd/machine_spi.c index d4c7a05bb60f1..4ffc7095cc2ee 100644 --- a/ports/samd/machine_spi.c +++ b/ports/samd/machine_spi.c @@ -57,11 +57,11 @@ typedef struct _machine_spi_obj_t { } machine_spi_obj_t; extern Sercom *sercom_instance[]; -void *sercom_table[SERCOM_INST_NUM] = {}; +MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]); void common_spi_irq_handler(int spi_id) { // handle Sercom IRQ RXC - machine_spi_obj_t *self = sercom_table[spi_id]; + machine_spi_obj_t *self = MP_STATE_PORT(sercom_table[spi_id]); // Handle IRQ if (self != NULL) { Sercom *spi = sercom_instance[self->id]; @@ -246,7 +246,7 @@ STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, s self->sck = 0xff; self->new = true; - sercom_table[spi_id] = self; + MP_STATE_PORT(sercom_table[spi_id]) = self; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); @@ -261,7 +261,7 @@ STATIC void machine_sercom_deinit(mp_obj_base_t *self_in) { spi->SPI.INTENCLR.reg = 0xff; sercom_enable(spi, 0); // clear table entry of spi - sercom_table[self->id] = NULL; + MP_STATE_PORT(sercom_table[self->id]) = NULL; } void sercom_deinit_all(void) { @@ -270,7 +270,7 @@ void sercom_deinit_all(void) { spi->SPI.INTENCLR.reg = 0xff; sercom_register_irq(i, NULL); sercom_enable(spi, 0); - sercom_table[i] = NULL; + MP_STATE_PORT(sercom_table[i]) = NULL; } } diff --git a/ports/samd/machine_uart.c b/ports/samd/machine_uart.c index 2526b450931bc..4b8fa3b60d8f8 100644 --- a/ports/samd/machine_uart.c +++ b/ports/samd/machine_uart.c @@ -59,7 +59,6 @@ typedef struct _machine_uart_obj_t { } machine_uart_obj_t; Sercom *sercom_instance[] = SERCOM_INSTS; -extern void *sercom_table[SERCOM_INST_NUM]; STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3 @@ -82,7 +81,7 @@ STATIC void uart_drain_rx_fifo(machine_uart_obj_t *self, Sercom *uart) { } void common_uart_irq_handler(int uart_id) { - machine_uart_obj_t *self = sercom_table[uart_id]; + machine_uart_obj_t *self = MP_STATE_PORT(sercom_table[uart_id]); // Handle IRQ if (self != NULL) { Sercom *uart = sercom_instance[self->id]; @@ -333,7 +332,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->tx = 0xff; self->rx = 0xff; self->new = true; - sercom_table[uart_id] = self; + MP_STATE_PORT(sercom_table[uart_id]) = self; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); @@ -352,7 +351,7 @@ STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) { // Disable interrupts uart->USART.INTENCLR.reg = 0xff; // clear table entry of uart - sercom_table[self->id] = NULL; + MP_STATE_PORT(sercom_table[self->id]) = NULL; MP_STATE_PORT(samd_uart_rx_buffer[self->id]) = NULL; #if MICROPY_HW_UART_TXBUF MP_STATE_PORT(samd_uart_tx_buffer[self->id]) = NULL; From fcccfc176b225001b402ce2ced062344990789e0 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 17 Oct 2022 18:05:28 +0200 Subject: [PATCH 2787/3533] samd/modmachine: Add machine.softreset(). For consistency with other ports. --- ports/samd/modmachine.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 47fc03d824100..8adc06f526540 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -32,6 +32,7 @@ #include "extmod/machine_signal.h" #include "extmod/machine_spi.h" #include "drivers/dht/dht.h" +#include "shared/runtime/pyexec.h" #include "modmachine.h" #include "samd_soc.h" @@ -57,6 +58,12 @@ extern bool EIC_occured; extern uint32_t _dbl_tap_addr; +STATIC mp_obj_t machine_soft_reset(void) { + pyexec_system_exit = PYEXEC_FORCED_EXIT; + mp_raise_type(&mp_type_SystemExit); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset); + STATIC mp_obj_t machine_reset(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_RESET; #ifdef DBL_TAP_ADDR_ALT @@ -219,6 +226,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, + { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, From d75c7e822cbfe7014fede1420f996a0af04bb13c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 21 Sep 2022 09:57:10 +1000 Subject: [PATCH 2788/3533] py/obj: Add comments explaining the slot index scheme. Signed-off-by: Jim Mussared --- py/obj.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/py/obj.h b/py/obj.h index 8d62dd4f3cf01..b5b855bad395c 100644 --- a/py/obj.h +++ b/py/obj.h @@ -753,10 +753,16 @@ typedef struct _mp_obj_full_type_t { #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } } #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } } +// Because the mp_obj_type_t instances are in (zero-initialised) ROM, we take +// slot_index_foo=0 to mean that the slot is unset. This also simplifies checking +// if the slot is set. That means that we need to store index+1 in slot_index_foo +// though and then access it as slots[slot_index_foo - 1]. This is an implementation +// detail, the user of these macros doesn't need to be aware of it, and when using +// MP_OBJ_TYPE_OFFSETOF_SLOT you should use zero-based indexing. #define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f) #define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1]) #define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL)) -#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(t)->slot_index_##f - 1] = (void *)v) +#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(n)] = (void *)v) #define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f)) #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0) From 64af916c111b61bce82c00f356a6b1cb81946d87 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 18 Oct 2022 16:24:33 +1100 Subject: [PATCH 2789/3533] docs/templates/layout.html: Indicate latest vs release docs. When looking at latest (the default for docs.micropython.org), make it clear that this isn't the release version. - Changes the version in the top-left to "latest". - Adds a message to the top of each page to explain. For future release versions, add a short message to link to the latest version. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- docs/conf.py | 3 ++- docs/templates/layout.html | 25 +++++++++++++++++++++++++ docs/templates/topindex.html | 3 +-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 5533bf01918a7..ce5c037568adf 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,6 +33,7 @@ 'downloads':[ ('PDF', url_pattern % micropy_version + '/micropython-docs.pdf'), ], + 'is_release': micropy_version != 'latest', } @@ -74,7 +75,7 @@ # # We don't follow "The short X.Y version" vs "The full version, including alpha/beta/rc tags" # breakdown, so use the same version identifier for both to avoid confusion. -version = release = '1.19.1' +version = release = micropy_version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/templates/layout.html b/docs/templates/layout.html index a6caa0bc5a113..8563f02af00a1 100644 --- a/docs/templates/layout.html +++ b/docs/templates/layout.html @@ -4,3 +4,28 @@ {# we change the master_doc variable so that links to the index page are to index.html instead of _index.html #} {% set master_doc = "index" %} + +{% block document %} + {% if is_release %} +
+

+ This is the v{{ release }} version of the MicroPython + documentation. The latest + development version of this page may be more current. +

+
+ {% else %} +
+

+ This is the documentation for the latest development branch of + MicroPython and may refer to features that are not available in released + versions. +

+

+ If you are looking for the documentation for a specific release, use + the drop-down menu on the left and select the desired version. +

+
+ {% endif %} + {{ super() }} +{% endblock %} diff --git a/docs/templates/topindex.html b/docs/templates/topindex.html index cfe3ad1516ac6..4e163b6efe90f 100644 --- a/docs/templates/topindex.html +++ b/docs/templates/topindex.html @@ -5,8 +5,7 @@

MicroPython documentation

- {{ _('Welcome! This is the documentation for MicroPython') }} - v{{ release|e }}{% if last_updated %}, {{ _('last updated') }} {{ last_updated|e }}{% endif %}. + {{ _('Welcome! This is the documentation for MicroPython') }}{% if last_updated %}, {{ _('last updated') }} {{ last_updated|e }}{% endif %}.

From 11910e2fa1c8f5dc76cbc90598ff81526d1f6312 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Mon, 20 Jun 2022 14:02:30 +0200 Subject: [PATCH 2790/3533] docs/samd: Add documentation for the samd port. Includes a general overview, a quickref, pinout tables, and the beginnings of a tutorial. --- docs/index.rst | 1 + docs/samd/general.rst | 88 +++ docs/samd/img/itsybitsy_m4_express.jpg | Bin 0 -> 151296 bytes docs/samd/pinout.rst | 850 +++++++++++++++++++++++++ docs/samd/quickref.rst | 469 ++++++++++++++ docs/samd/tutorial/intro.rst | 84 +++ docs/templates/topindex.html | 4 + 7 files changed, 1496 insertions(+) create mode 100644 docs/samd/general.rst create mode 100644 docs/samd/img/itsybitsy_m4_express.jpg create mode 100644 docs/samd/pinout.rst create mode 100644 docs/samd/quickref.rst create mode 100644 docs/samd/tutorial/intro.rst diff --git a/docs/index.rst b/docs/index.rst index 9a021b3906d46..64b83618da149 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -17,3 +17,4 @@ MicroPython documentation and references unix/quickref.rst zephyr/quickref.rst renesas-ra/quickref.rst + samd/quickref.rst diff --git a/docs/samd/general.rst b/docs/samd/general.rst new file mode 100644 index 0000000000000..66e6d2a31f69d --- /dev/null +++ b/docs/samd/general.rst @@ -0,0 +1,88 @@ +.. _samd_general: + +General information about the SAMD port +======================================= + +The SAMD21/SAMD51 MCU family is a high performance family of devices made by MicroChip. +The SAMD21 devices are based on an ARM M0+ core, the SAMD51 device on ARM Cortex M4 core. +They provide many on-chip I/O units for building small to medium sized devices. + +Multitude of boards +------------------- + +There is a multitude of modules and boards from different sources which carry +an SAMD21/SAMD51 chip. MicroPython aims to provide a generic port which runs on +as many boards/modules as possible, but there may be limitations. The +Adafruit ItsyBitsy M0 Express, Adafruit Feather M4 Express and the Adafruit ItsyBitsy M4 Express +development boards are taken as reference for the port (for example, testing is performed on them). +For any board you are using please make sure you have a data sheet, schematics +and other reference materials so you can look up any board-specific functions. + +The following boards are at the moment supported by the port: + +- ADAFRUIT FEATHER M0 EXPRESS +- ADAFRUIT FEATHER M4 EXPRESS +- ADAFRUIT ITSYBITSY M0 EXPRESS +- ADAFRUIT ITSYBITSY M4 EXPRESS +- ADAFRUIT TRINKET M0 +- MINISAM M4 +- SAMD21 XPLAINED PRO +- SEEED WIO TERMINAL +- SEEED XIAO + +To make a generic SAMD port and support as many boards as possible the +following design and implementation decision were made: + +* GPIO pin numbering is based on the board numbering. + Please have the manual/pin diagram of your board at hand + to find correspondence between your board pins and actual SAMD21/SAMD51 pins. + For the boards listed above, the relation between the board pin number and + the GPIO number can be found at :ref:`samd_pinout`. +* The pins that can be used by MicroPython are limited to those listed + in the board definition files. + +Technical specifications and SoC data sheets +-------------------------------------------- + +The data sheets and other reference material for SAMD21/SAMD51 chip are available +from the vendor site: https://www.microchip.com/en-us/products/microcontrollers-and-microprocessors/32-bit-mcus/sam-32-bit-mcus +They are the primary reference for the chip technical specifications, capabilities, +operating modes, internal functioning, etc. + +For your convenience, a few technical specifications are provided below: + +SAMD21: + +* Architecture: ARM Cortex M0+ +* CPU frequency: up to 48MHz +* Total RAM available: up to 32 kB (see table) +* Internal FlashROM: up to 256 kB + Some boards provide additional external SPI flash. +* GPIO: up to 52 (GPIOs are multiplexed with other functions, including + external FlashROM, UART, etc.) +* UART: up to 6 serial devices, which can used for UART, SPI or I2C. +* I2S: 1 I2S interfaces +* ADC: One 12-bit SAR ADC converter with 16 channels. +* Programming: using BootROM bootloader from USB. + +SAMD51: + +* Architecture: ARM Cortex M4 +* CPU frequency: up to 120MHz +* Total RAM available: up to 256 kB +* Internal FlashROM: up to 1 MB + Some boards provide additional external SPI flash. +* GPIO: up to 99 (GPIOs are multiplexed with other functions, including + external FlashROM, UART, etc.) +* UART: up to 8 serial devices, which can used for UART, SPI or I2C. +* I2S: 1 I2S interfaces +* ADC: Dual 12-bit SAR ADC converter with 16 channels. +* Programming: using BootROM bootloader from USB. + + +For more information see the excellent SAMD21/SAMD51 data sheets or reference manuals. + +At the moment, the SAM21 port of MicroPython requires 256kB flash, of which 64kB is used +for a small file system. The SAM51 port requires 512 kB of flash, of which all flash beyond +384kB is used for a file system. Some boards have additional flash memory, which can be used +as additional file space. diff --git a/docs/samd/img/itsybitsy_m4_express.jpg b/docs/samd/img/itsybitsy_m4_express.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ea18b5c3899dee9a6668451c794d3d50ad0fa390 GIT binary patch literal 151296 zcmd42WmFtNxA!}^6WrY$26uO7a0@cHI{^Yg0>Rye06_;B+$FeMNC+;0;2wfITyoBH z*FEoD_rra^y){!a|LLl|yJ~l@Ro(m7^RoEz8-Sy#sGju34=zncw|Ix~?n*4ugmjCEi zQvfdW!JN>od!~eI>f3}XF5dX`6`K!Kut@e5jzOL3Zd|W^v7cYR9hnH8BM^KcP zhXx1~<>wIv0s+VXKr#vd@wM-3BM>g-zcm~@0sv6(pY1>%9=L-4XCLtY(f{Uv@E`qe z4*v==009o+zee!t_y5;S;Nbsz#PY8m;Qyly$^Xk4;N|}-!Ycr|*U|F0k- zAiyIaAtE6mAtE9oA)_E8A)z26A|j(9qoDpPNXTgDXsGD_e!RRo`RC!E)$1Ge)#|^x z{BM1{^a5~E0hWMJ1UNbXJT4ppF5Jr?fa+fq5dNJI|93%n9SkZO96Sm-;I&;G2LKO; z0RK8f1Y{({*C+#CV~vP}i;M!mqXnYk%jgi$S%vX>{zW4sqE9K5m8Z<&nH{hH~p7@ ze&b~sfQj&59dH3s0BH2jyzp(GvG?B3_$jk*+y-hbCL-8Xb}wS=&bQp+M|QV$_W7QI z+xRB3=OU;ISl+fv>0f=fx*Q3Kh`*Dx5P=&78Hxb>_Ys1>gJ)2}>H12_`!8Bqh2aeU zT5kIY9~_Mmkl?~u|*B9+?obHsqU0EpCChkRcw3J`iI-R(N1 z+^M5o^)*^oxLfawy?i}OQ>)9XJy0Afwn!Zr{VY=o#0!WlF}%tra5`~4wHVU__LcqP zvi>H0*;l4S*%^MU_!ncJkl6kJ!xn@hSn7=kjbt; zOlbdD#os7t(Y1k*+%@j;m!y&PQ+$gU%g6D@=)Z2CG090&*zc06Z60omE!u7U3|CMp zq~=P=UpJ!VlPW3-`N_sMAcCVLrUZIloKE{A_o`kU`g?%{a_{Cv1zYwp`>p1q6F_ea zkDM=U90FdaNi0TKC_h*!D?=_WyFeF8Cr^n%3ci@`>Jp7O?aNm*iK*BP`(0C-qs$D= z6ihUtR547LPUjW2gag4*w9)qLn zn%)ozFD-2piyz;Sf1(x-q^AN?NvM;-5&>Gb9f9%wQnrkd%Gzg2Gzdsp6^|Du3!$0B zT&dYMX&5u8n&b#G>r|iClXURX@{!)Yv$Nig?^dS&+$Nb9`rz3xLYB!nIbMhsc+O6w8>0~9$gh7{n0&gTGS$U*Z^B3AE3RL0^qVkv)L!)wo z-ref2P*U_@nM;HkF`J9VJl3oDP%o_fIuph)W*kBWQ3NP^tj~mXgy5b zDqmiq!U)}_CWu?J?fPWlP?#96J z2$P!u?qFHIA_)olWcOYt^R3ttHPUB&qJ|Y0^bn$e^@FBw5*~V>3THvD;Qu7Y;`H^+@yazwUz5sAd2CR;W#GZHDTZ(>~ z1nG?#>jYO6s!r(BWvhY~%$+icFPt&Hv4m|KZImi+@yN%o*?0yad=yp&fhUx`)p4Lv zFMwOKm4nhwkn8;R@o99|_MC5H&sjp$9m4|S>}Chs=LFY_vC2TFAfwU>mby}Ak#ukR za!{_}bLWqSeMf$gqB)|x4`o-BDYY$uIc%ci#?X?4HIco{a2Uy6EMaDTg@KaJR5HW^ zCiDAC=t=3XJfYY5dLG;eJW4qlowTCIF>N+O11nwcTaXk#X%07SjkX!gDgPjZs!&NP zBsXFH#^z$W*iiH8lyKGioafr;)^c|`az>T+3AFCo5wB+|fHfTWDb%>~@GVm&Y*~_y z#6CQC6Do~%`%cH~ob8j<=y9I7E*3K%DZ+UuvJROO(V?ZjBNr7(+mC<*712qUzQSmH z32c8KmF+0%ih*izME&Zdx00L=(Cx&E0I&-3V_xYg+Zjw=<;-zKbFZBdZ961CjX{5i ztm*jf-fDJ|QCS7*CeDGXss(ezUuNFR;j_)Fe1zduC11$HZDl)uLx2gMYBOcc8$j#V z7(zAF9tYnY6g_*U4xt(@(}@Hr+TTNS0MUxSw+qr4!)X=QYMT`4+m#l=3j^h!W~M-`VN7LPy33J~sAdt~=s z#p#=Vl>q08eQC3U$nnDEjES=u=wOs-1)WojZo1hO$;G1)eYtnY;c>APr&7L(4aDV0 zd0Ob3K(y&Tb$|PLjdOn{NNPiN`HCC+hp~$n|H``FBxB-*A{Pc`v$lW>+V*fLGvqsF zN%0uNC4OmZ_d*4isHt1!2~l3gY_s<4fzxMiIW2x-HNs%oG0#@V&%bo2;n_Y(IvjdD zPK~DVe#PdX?bJ*Fm-!%%9=YEK+05i9=}h-|%=+hug|G74@R`%cPRrR>er=q4KF<_} z#wsLB&b%AHNV~Zu2V+SvoJSd?itDHM^>4tK0V=;sj{qMR_+H&Rj|>tGl&VHsuCNh z-rrbhyEZg3--H&FEHoU<*T2`0tSk7)5Xb2XD|o|Jv1PQSy0L?VlCkCSS8uB@j%Blm2LUmMobP_dpL||ps7o1 zkm=za`0qi90AWblOpZZikZt|)ak1uHpRNW$W(S+7UZh(~n}G98mYl3meCL##fzThE3rxRH~I76B6ex;I|KLI{iap~M~qy2*X?Z@556TV zcHyf9&OXF62li;bt*E|tYpB@}@F*>8*ZabSdSD67EZed=*N|_dkBSwSwe*9{o2(D` zMXhNRuc&cVYecRMmv$NnVDEm3t7xP$npa_A;!sW~xW&8DLRYWY*%8^28;olCGhDl* z(LOIWvW2&wMgFI4`udj$wB>CoEby!AT6iW@<-oaex4g2PP=_T|G_odFeKNx(w)2Q= zz=-&pX@wBksakET$qPWY(k&`hG{_gTSyhrOhVY}+=f|9PQ{+(dB}jIb9cOnYS{3ZD zApbf?bIbIoXzEypKayp_Jb-dbfqg2s>)qS()r94>Uy_7(8&vtLe`=^=~L;4lZ1fe_^ zI8_9vA~LMcP&di|k78o-_`|C}$!F*PGMOoAI-&EtktLujc&%cq4yM;DZ6O+Y&w4|p* zg3d^giX8biAI~W1a7cIe3*fXjMfNuY{MIT|Wrta!=jP^jOqt(IOP-RPxkHa>IXBUr zUgdze)8Q6<;i!bD{d)W5#!z7^xwoL7Xkeh|iA62HzNnIx>ZLz~rI|B4PqjTNYO@9k z%i5-XWJ}fi?Imu5(AN@n_c8L5OB%D>KFa0elZ5b_jXX8p04NOts|1Nqw`gL|$;d}d z2lTcN7$I!@iC^af8-skYkJeg|A2YMdU0YP;%UVorssp3&f6mMPsCLaS>7$eR3)0}E z_^Ys^y9Lbtyi(l&_7Y5TJkb~9^H_RXpBHMcC8pGNnB3!l%Q{WNqcXV)i}D4LMYBmL_(7HILG~z# z8%2)_GS+3y({2iB@+b30rlUd*W=gD_0oX`c{=Z@_fhqloE>gTN!P%j}q=K&?e~2 zn~CqREj0etGDV#7Nx>k9KPZlt167>!D;hX zOP*nG88|p5!xIqa) zuQk`c0%}bc(;0)Okhn}TEdG~V3=EzU)UJ?j(gaW32)qrHQ+KBH^lIJ@?rB-8ZJzUD zq*v0={25UCOO_=;uTe;d1@L}qkqSZ-=e_)^_3uo*>6a4U=UFp>~wKj z4ig;B>?D z2SKV$s8a0}Qd5&AqsbGNa`NU%7(k&V$(Ah82A|Ryzp9)xeRrm)RpfDW#Q#u~bC)g=vrWc4w9>AxUD0rn;iOIX5&YN97je0qy%i+_J@ELkn~{fy^j z{4c3>X)}p>~6))D^2?PGVcnARee*{Y&HvR@qW(9>zEhf3|cd$APjwqz?xe< zaJU&67`kvdY;6cXo)5~HP+2=T_6_VmP9H<^rQoT00ib4hbcUG|6_!*Om-*Ql4LTil zhD_nx{++X3b;r9#PY~FOYT3@v`)--D_~D%{NQ=5(drc-?XTq3+C*3Y)PQKfOKZ(`@ z4qa=t2lM`Zp#A=5oV>%hYwm){)KR%plj6$y<+lEZ!5g-0T9b)6ecR3zNqLlV<)+fWPMW4u0-xP9(c!N*ZN%gnYp+XXacu*-3cCY~BP}ey=0~&> zVe$DcAL>q?RHhCNf9xSc$c?)}%`b`m!ugiI($)pd?@Ia|DT{xa^v?cJ#T6y1xoo#+ z&&&;(>R3iKh;|P<=Hz7ftCn0C1t1uZ z-TAWzWMd~4of;5x#qgRP(6mg{FER6@Nz$41dwyQ^Ct}FaMIdEUcCVnXqhFah7&ah8 z58HWb1_t<#SS_69=Tg{ENA|icaPW#A@*c5Q47vW5!@e0;xZNximu=s6wUx zV~wFw<#8bo{1zJSDFKuyG8Z%a8@4LDTfYg>qDd}o}r$(_#NG{{D*4$T}V|+ z0&=nWNwWxJlkIrjFiUcsy{Wz~%B?=5UNc7=BSTj{2gMm{OPcp(wT6P*3&3>>wp!i) zOH5;2bmA)0nGe?`X&~_3S(Gv}B{#J2v9Sb96YRACuBINqa%b{14 zN0gtFcj!2I`PK?U+<_EH*_oi`%lb~#KT|)3RYI5)*PQZ6}F50-eha;QqyAu3!G*n-iW zKSSWTEYE+Vn(b728~1H_clJ9@O@Y$qd}BNCH`-`h0oc#myp1+u$T&(5)agtk>95i8 z2j(TUuYz~2pMpb|BsPAU@YedA-s6q5zMkXNOnCH&PP0>F>Y~XS`W2VXY@_0uXtfF0i^zlM(R&%>^W^^cT+u9fs!<^v2 zIV)%)-?E}HuDqSGR{?`Tps=cnl!*x)9lq4_3Ho;XlrcLy#u(;EnG%GSA{B)%MIRMB2!a*-ubecp(P^DB=DH z_bTGnc}^87VtXg`)P8vTh)bv(0}$e^(_R-ci}w?j=(@e5VNzbgAnMbj?MO@Y;+vt= zLig+qK&5{(3CCPWwO(6GUMeLfVu``KcNp9Q?85^WUJ$Sqh_{ad0KYo?@9(d#e{)A9 zDjMABmg}U12Kqhse$Xnzh|&C=8!@FBAfh!X)BE@o{E0lf9S&(W5rVi?@B(l{%{kXj zj$|t1){01H6EV9_>^U3oo7O156sTz!D`7n$%W{S71pE<2z9+4^O5D0j11a}>YnkI= z#@9_odvv5Z7*Tb=GmF$WIEkV>mq>yWXunGp9#0ib&31$A^Cjlus)}@)ES-=kX}`hx z%319v_1>3qoPDZvl~YN{Szz92TKUq;aCryH;p}#b*eIRFIwwiWoUc4Hm0lN3^qH|% zbEV=q$B(J3wJ<=^-#)d-&ibvRjkSpiDFjmDrN&3Zkd{waTvD!wJ6uM6ohlj(xDscP zQLnd0YVVIo=223AbMl;PsF`L&8xEhAtCDKV9PLr4A7at3$4ar5_q>+t(lkNNsxuoO zip_WV-R)yYaM0g!9KO`8d_{`eDcK~bl+l)Ng0eKJu^|bfSLPLU?ca@NJ=0G_|(9O;DAIV%yD?~hLI7QJbg!B8r zKo5nNCBQ%Y^?wjUgG+`d_+J^MQyN8FO>GP&tz7(W6?DGx9FQz!W4d<1$L|m#Nkm^y4t(w!N(7E`KGQjWCGm_qPbiK zSCmMBGfdguP>r0lk#tc@29=R|lD^(i>>b>TiQ3Hb#ruFs_p^A+7PqG0flzMr?lYY7 zObsn(Mp~Q$J=K01BPP$LQwfFWnZ-^;h~r|L@Our6G$M|oJ}6hb5$a~II)#j=ou7bu z2t^dweG&b1=T~}9$gK5rmXoPj>UAy+?0$tuo!XiK?HnJOUM%?pAOC)T{C4LBz?kut zn93;Q>;WbhBgbXTT)jO-l<9#U^B6TItB4kL2Z7|*M7H<1gvS{N-s_a$#J3)@ekxB| zjiivxWVac0hnox?30QH6obw#3_lw@)oEcpT4A6M<;c1K)A5o2-JHiG-qVS^bJdCpK z5>CeJ54SF|YNC|RrVEqvZdGc3ACW9=ID%L@MInxjJ>i`@FX!!Ypc<}{U~Ci8;b`s< z5pN238MnWR91GU+wq3S3XA;DRD|YDtiqxA4bX#WA@!)aSAJ#Z4N3(?McU`VzrwM9V z{b=Whmnt01M38#A`Dg1~c%V_IMHji*#5LJ^s1nhgUhEi%1Ggw( zbKbJW_cro85y@`$N3DXL>mn_toYAmbC@QH)_Kp<26fPxa&kfP2m(d}Rgou2@(rOBd3y!}0_Od{*v;hA16=o8Kn*+C9HHcm)k8ESH9j(v})15wMh1*ZpO80W;Kzp zvCBtZqW&`4J~%RbZXiW^V=I5|>};%@p_h^Dxg_@7Jcg~nhPz#mD2Lk=vcUHM*+&>o zt9sj-K1$^cGSm3%Pl5#_Ol=)YG2;Il!NkGP7kNPIk)opcSX^Q_!diKj^R~kmIV9Ia zei}mqVw|j=^ShK?PrAAkUAyB$V>DmZAAdN*FUfY?T8Hj00N-ztqlR)+jpl+A0mStl ztR%X0@EF#lYxW_!-9Kz6DQppas&h3#7bHvI@e3!QGv&S@p)aYBC}(w|WT^$ogDQLL0p-Dny5u zq;$b;P-JoL@*wtZT*>JjQ?NR7O{a%33;P&m=o#?!#)DVkM>|4Kn&<+Is9>fY{CeAf z(?`+Hc;sF$@B*MhOTCttuCf#!bba1}&Q9iws#K6) z!{Z45=%T-gF5en3B5kUFCff4!xF+rW?yS(S3Ovl16blj7BgWRWbs*%WL+s+~jQ`I0 z0^q9Y&}tL)*0LqdEK(lXK0;rR>`x(PBbsH5i(w)s6KLfUlgIr2ke`}JEs`?%+c>`6 zlod>=XReF)NmZsc;Iyxrm4@VbL@0peQC$)23CZ?I=TvBxEXqoN5-ey~_`pD1_2>|W z0XeT!*2SmKf3)RZj~bk0p_$Ebc;oS$k?jMu>Q+_~c$JyeWD=j5b;lfAMV&kT(~bJz zA6?uF>f}MDD&21zhv41Mw1bbajD8FIN?eRQTwio;Ujv7Pi;E)`LWz1-zOC11!4X`Q zR6rN@YbnF2Fj0L`MK;dsjz~Q>{`7d+&Tp9#)@=4NLwZBq^L1D#nK3i{ z*Mc^@(cBxPu&xgODUaF{*sZ!h%}ZY2^&&A(vaNxtp{P$Kx*YB7G^ z*iiE4316EKo*=o!iKn{27|Jhwvq#yX$?a)f?e_)pw4q};)7hoUAAYe8 z!p_TZM79~Qo^Q^psFNq89*scB$eX4Nks*0E7fN7Di+NkvQiQ;9F0Vuh$ zpZbVa2;M76tEGHRAN}p2Iiopj&Epb8d+QQTH*Ma)gO_hTWsmL=L5?k@Mt@&CPlrKg z>(&B#iq$IBY>_)588p*%S~PzTk(7vSAi^{OAI2Dc*`-rx_gsal?HbV2x;firCkQ1} zJC11zG^8`#8JbA1-m!3I;8or7Ds7Wm|FfNDz>+XhD1!>eXh)IScJPYdiKT@YOXp4zwR|8WL#5( z06x-CHb&p6`R$e$YqY=BeY}r;2F^3E)dL!vrh7Gi=y+mttzkE#1UY&)eEjocna+}w zS6fHw$Sr4w1KlfoV_1mtbMW|dQYx9Pn~r|!d$bMKO?a_vwC7C}hui4=+&(MEhVxJK zm=GpVJXIvLDTbkwf> zQ?u3}=VWe7P<2T4c|Q{|V&IpxV1v#m)Y@i>{^>HOMbW|1v)RFVK}%z?neUSSi95#O!^Kom&c$3dgY*P1Z+f0mVSN--0X^$8|z@`{nrQwxt0|^7DQ4CYf zw>tO7h}*>MyA?c}cql*$oA3`-VrNN^WjgGPmBNUxpC@)tw%O8S6-%1^ew=zO++Z6Q zz@=DfjIA=i%cgz3V%X5(y84cmmaNK+!UdTo?nokiGLbC-Ws@UbiX2&vWMxrtK#N2-^B7}Zq zxNnj^>JnBVPcaD*OCd;C(VWJWm>$BWilg9y={Q>TdKSsXo|jIb6O2lNbji>6aHXiJ z$wvW89Yk*HtdlbwmF~Q6eI3-79f8R%VW}xhJT}t8W1j2j&!epm(XBC^T0=(tn)(Lh zV5aejfk=^uC8<_ZEVSzh%ji*5hTlT+TBv!uw!>EQ$DtNvOA{g(A-BaCt&F}JdmLG0 zP~|kfPNGd~KE7w5*yQ|p0)|h;kLKhF%aLDhp5+bSy|w1&RRW#AlX5#Pjv0R$bJHcy zJV*5LxKj(XAP;ZzakieNHs{IG+AJY%~o6i3P!$mN$mRF~k~7{rdEtSqShF1tUR|4?*^qEJ64AJideF zu)2!k7^3h%II`b%z4WBI{D?vd{|IYRAM=~|Ad;vwH_u3IXd+b#MwAlXT&h2nm`4g8 zCDeZ(DU+|A1O3FtB6B!X@`q7F2gw6ZlhE^TfaN@%RulmlnY_ZGJufL`FVw!-=Lcb0 z`q)n8qPBhoYaBYy$fEwc?InSNeq^TE(GpZ}%%CfN_s&Mmaw}wzNOH^3lFJB28(=i^ zNw{71=eYOD8(C!M>|8|YtTC-SEj8!qaW$4iA@yC4yX#U-FW`v*&jOGmWff};N}B9_ z-NGF2k-Z!e2pA{%r~>ZE3{j(a&6clujDn|}w!)%x1lybI#H4d6xRB{21(OmB2DW0C zJv$&%s_2n*nJ)mM!8)Ephiv03*?f*qF%9TM^nX!+_z%&gzTqeohG!6rVlU@!kyUeS zZA;wRbyc3$V`+RxOmC>4%Wsob^N*V>x#p*Yn9~bA*gr5$@Uv^!rgTeEwbPPju@p8k z;NpM=SnhZs@P2}iPs4d#DN{>*dDh`2oz5##4qK2ltk3Z!zi$Z77e%NC-Nj0JGMZ&e zS46{gOliV1Rv$1vVpk8AS}I|Pq%HBi6R9sFU4 zAqv&i)QO~C^b8X(T$ppT#D2pQCwAh@E8sZDL5yK+PjW>cev*asD`OQ)Dn4v9nei-p zOY8)fzDL?ZM94)O|YE6yB-b`?Gr8JOM#F zyCO3Fc;n@8PM_&_jLPA;WkQmj{qBoEUDIhV}J?dxqiegS1aOn&|P`A{_{BkJCd>j$8xj{m* z(HZKZTC`QYPyut?`SrehtDve^(%7oRO=jX6?H29##+#88Os(M`UwMt4_128*3uX>8QF7_+yte|>N@6^qaqLX7O{`H;_|g2 zZV|_+8HV?*>dT4?FIB0^wQUK%5%dK0)W%m>P)&33(gMi`ea+Dm+CDhVZC!}4qgQ=t zXXp0ZF$D0`+TIi~pu#yhqe|I$lTVIF*CbemxRYCNE~OZo2d_8<+b$IPDq&c=@arP9 zZbrhVQf!O3>7ukM*pX(%&AHkf9x~&5etAFBG}K@Eae$m0DH1ij2bb^@X?AErnQIXM zZ%7@=*7Buo??<8P(xSs+)dNP*pUFDQIqc}v*O>AO#;}ScK_Evb>#xveWQ%IN)kQy- z=PyNJ^t?JQE^#UB1%Moc&yg`0Vxz;@Gos;Q4vmjIW;?&3)~qIeYS0ZdIcn9wJw%VS zR1W%x*(yv+ve_-BeEL&8K}CZs%`cIeI8T2?1d*0-?30q?T2p@}sD*-IV2N{;rlcj| z1_H=w#dO)`%^GyHAxu=R&lq!QPr^5DBYqE{2}QRt!ovS$PosmQ)1wV)HH|XW0u5~( z@DVq3uTX|mfk6#kQbaX?U#+Ny^(l8ezgyScVq43AF946ehlNbeL<~V*Qo`fPF^38} zXmt?($7m=K_*NcDM<;wbyPPGMNd+2j^pWGuG^!Qya<)b1wdL|k_(quC$13pgqm-Qs z-WSMv0g@`}{^rSwe!^-W^NKd=gR~|CcH3g43BL@>yZh@zCrHeiB=!*aXfsPR6lVKm zbfnh3;%Xjvnr6iV_J10wBfgdIZ9w~a4&f}ZCgQ1U@e<%PYUZ3r)K~3tc5Mc^Wlu`i zQ0SoZCa2g;ChdH8{E|>GX^=Cap>9iPRViac%#{$Vq0LqkJqNBBosi)p;y zBrWn3pBIJW@G+A}H>pzeY4OaA(y|CSo!0iw!(!8H%ZqNW)16U@n*6(HHRg;)aeQm~ zk2|pdp#@?i2PZ@joyZ!4Iuf9fW|{EVf|kYT59s~C>@X84 z&S*3pJms_C;qqGrl1eh`j@m;^^Bgzw`e_hq8bm*^@L`{FEgvn_X8I z`f=F}i1K!azlmcpOW5D$qOLs6*@lBXCjD%g##)LB0=!!Ggrr=_x)QCCuF8+ZU-j8y0HdZ3%@V{F)V$)df0w@T z;FnNWZrh(cCym5Gg?xyRSoK@;ml$+5Icc8E0*Aj|bi7kfth6I+IQrO>YcD=ph_)pj zrSf?&@rP!Px&F=qXd9DAIdvm4xP-sOh6Uo2#_d9k8iT`mhZsNEJ0k71e5RilKLA$3 zu4<<6puIs}Oev5CM^BjJv56QBhEOj7+fJM0eFar|e^UIRYq-Mluj4YR_f#9 zSS8s*xbob#jcMq#Zw0Q84ejhYOG$;y>uqxRZx<#j=g`8gUb%?AetcbJivm=Tz|@uY z2ZD^$8%yMbptk-thSy9OQ@g1;1n{QDKbn*_m9xcoYMuO4LbyF)0IVO-bXa%v1Oe4F zPgmby(}Znn;8SsEu~_zMZQ+m)y7SHX8cy9>JM5h-3eF=&qF=2Q1-~=30qN2x4tlY; z3lcP7j($+X4v(oj;+pY0W7nI2<`$W4AO~77_jxTQWZ^0C0nd1qs4srJ;WBPECLOfl zj{L^Ll(9`#WQjuk-`IHQo;k0KffN(eYGoLKw~;hM8A34mg$sA`*T~L`@6RbLDWRBeg-thHSv~eO@1L$7mQ{y#VpWO(L!ljOj zW!aaj4Hdp$h1?RrCP*xac;Sl>s_zgu&URNZpSOxTYW>2)4JS`Pd5Egyonj=UaBV=P zO$v{30yWYepRY;t1yKEQEY4-!_HkE?t`#XSw<-$A5ulfIfnD}Qbs+ z92nTjSEk3#tD5Z4=;F`=bm00L$-VMx$fmy(O~|Npu(c>0*V--LE{J|>#k9mS#k3Nb z+T)^y-|6);Mjf1-jF9q-QN6q-O7)n-IY)si&=p(4FEJEa%T(e$q@uw+hG6V@akZ&5 zy9xpWh@(KVvxX00o#VyE@GW@7UXP=hUe!g+iR0?>A-Ht}taF!t@-V#`MZ>3<`klbM zJO(HyGDFRodXXY7L~L+#!KV68? z^#qe{e{N{5URys^7dric+ESHPi$ql>0tLw949U)phCp_HpnDml*zsOf*>XER7Su;3 zxZeL()ZgsxpMdvz-)7!$xl$g! z0DO`<FiWv7a=$2u&jADbR>8^UU3i_4n{f+iVhhPPoi_sE)#?J#LlIq3jH=311Y%slE2+XL?JQ3|HBMJR!EOcohB44DmmKZtxjxgNT#8 zf{hvd`SppVQyon{EUsE2u0C9vlN$N#VG2?NZN70Lju=bzFNaK`a3GkO%@S+~RPdk) zA{XSy=li1^j!v`>X2{mY9kf)oR7DOES~IKvYZFVGVY@=~O?a2<$#-Bl#RW~j*gPhA z3pL|ecT@=f_F8dkwJA3hRx|gIFdA6QE5_Afdj)M7MDi?V7T61)GQ`IjTV`U8_Zgsm z3~JEkF8wlOxj^LnG;lU&i=_qyq-J$?E3X_tgh?`DH?2b?0j*UCFq?BCFSPbg8oRvN z5qg#QnhIJtmmNknXlT~*>P7N7jcY?;CT=4Sdfp*o?=c+Q{O$RmY*u|O?#Q|T;bAMs$+>QF;qk88>I zo~qCm1IfDj)7W6}_?o^}dI(()NKXg<-1LLZWP`bB_G<7)+sOS5v$gw{10C3GVbXS; zN&Lnx4{}mJozeH>aMpw(WWF{%&&*T%ePfU_>ntzPam$<+Q~YFo&zzx4m!vHo*2tV{m#=(70?4`j0RCctvENLA?8G*bFi$_u+ZHwmGh=pU01Y z{;w%a{3L}m?LL0=cgg5T;i|OBbhvrHiL9Dl0OjNq#9Qpi({7C=U%q_4s)VMw%P~{q zv1MD;$%MNyauE;4z6*^H=^#678sFn5mqj8 zAmLBVmD|Lx`3r_NplgZ~0~J!QytPpF)$ocdC81~gm0mxzIkz;2xDqv%b(8#1-u2eg z$CJyN{!U*0^?{!clRmJ`{$@^YV`K5^RU$gV6i}A=SyEGB6>VJ;K{?Y{oPsLn?-Gpk z6W|X_mXWTkrn9$d=g}f~klMF9uYdJJW9<90ireoG7JLj@L;MirgEXy$VmrUfd*!HK zMt})2@NkM^8a`J!Ky{93_83{@%4!X_n*sZV#pXf%w+bTFp8Zd#9^kE9!Z7PE#yFzGBmG?_59Hl|q6v)$}l zV+E;dV%vtn`~4mm->J>345m- zsUYm=d9sOPZX$>?+fo`zmbDBxg`5;@Br(V!Vq#;Zi9)hEV=0L&tYzre_hKxrn~w8x z4{3Vghblec1zdehiJy^^5{Vhxrs#~-;m9{1me$e?bT|?tG+2q@B>L-644UkhC2|ul zh!8FJE|grywKei!LyC33dlZ+q7xw;&r+QXaPDDkaD=GBLVt+De&(9g?3y#}gi>*UD zmsQ#thUP%#t1DX&th%KHJNi~oL|=pqO9o_MLO0lAAsst7+;Xqi&AIflVj~vUZsfMn z2dTs+$Y8@66D9?`OoU8E-!VNdoQs+md-dIy!Qq`t?WT?^E|vye#byXn5i8;%FhsBH zo<#xkyi9d{P|7U*bG>f}f9{kSyET8@xW_ zQfA($M3h>hhtxYuG>nunb|zvyat5jOluV5Bo&NwYiELy7jf}M-xmxC@BD&zACQoEU z6fjUi25~D|dADI1SrbM!+-I(R)*7ZcKV;TaOKP2GaT5!9Z)Eyoi9r-X&QefoiGe5` zus3lYluwg)tk%K!)WpBNNN@5b5};Rbss1tWnbXX6oOvAw^Q5Y@r8OuWHL=%V-fe z8I6p{XXaz272T2$x1FPR8a5lf)Jqfv6I{k%8)-sNEMbd>k?*{ee8DWr_*2iDPT61A$0#b5n7L3G{oNv5O#Qrh+o^#MqUqkM(yeRks+r0pRb-^N%t*k# zUBuo-TagorCw@H#KWn8^Ljd4yTs@{AvEg!W+RB?b7%aH3Oau&Buz!f|Qy(QgFbm~h z3|Qn_zof8ewIaJ0T-qqd*d!O?liA+O0=P*_BaazAF%idkq^(ppl{{8$Rq?c}zRHt~ ztabrBBt_QkSTF-)B0U=jd~spz@8WvknYpG*L7AGl%x9?dn4mLr|5)bm3;$tx}yj6L5 z9BPj%I9qu7tFiaUNYrvd%!slHHQ^B~fg))Tvv~YY7q-ies;5&?+NJ2c$tF_$q#w)zePTRH1KQb%{=<%dj?Ej`x_Z3o>Gn=>ZmC&r$+9atzzJ{>reya# zmOt4%&SZ{4fAN9pXXDbWWz3a~vvOKlkz*Zv-&K_3Afg#2R~v&PxR<3z>jlY~ZzM$$ zFe15@boehJp1+dP=r&e;S4F44945c+1=p~(?1hLjN7PZsxl9zu@ra-P>DSv$d2LON zy^E_B(svtc-nnBWV9M0UgBvp~;;4;*W;<=lW1uO1M0FSOMUKv7KE*%-aj>44F3Q^PVqX>I~~CH4cx$Bz?_+n1Q1Nd@iDag(ycj8ir8$^nJl$X z$b=%0H7lY<$HIZc8x_A~6pYPPOaTgnrZ<`B2(IIBwBL9#C6jXMvVWHewPjAnwTy*U zVOektSSBc7*dr4g&+mQqk(fqhEvCL2mF#X3&A?bbrH@n?R5RjHY1@nxiEcv?5h6qX z05jD%huFh1m3#Q?;_RZ}+qR~TK*pVhL5RA;B{2}TLt%i4BI6JfKNA}}sV3~Y*)N=m z>^2V4mL&fGT}DX2kyrRw?knHoIYbuo`{)aL)vJS@sZz0yrKCh}xU6NOxAw+u<-gi| zEFyg&&KIyBDBdP!dV3ZsXAaG46|5dQqe|25*SwV2bt980TNMN+SOKsr6?}w%?Y7ak zR?`l{F=$qP#x*o7l`*SbqY#i&%midkQe-ebktsfLAKNjse9XJWEdgekRJdN{)%Lb( zG!> z3dv*rD-eO(=cjs!_b+30C1zK`VjX>zC{6r#{kXUUS}Gq|DhbmfL?pp35W(f}Qd2y` zzcq6og~n;jeoHft3bBwyOI9MZW9rG9j>IZSQgQvVo%WNGQBM0zZEa(MwLq(=Dr8ls zz-m5fs-Ej^DaPoKh+~%GASqmt0GP++6UH~*K!1+976xytF`Qlkr3(D56?Kb62Jqk8 zBFQN*PbnwF$M1INvHVl573_9$(;BfvuFj%~SY9F7)|4&chLEj_h*-n`hZrB2 zE|p^j=0eSPS*fLLSo^W|O~g#AA7IF&T(uc+G7%Y=hz>iLZSp?_6WHJ3bao!S7)rnH zD`TpvwmFJg>_HJ)VM4*vGSEL-qF8cF5-Es?fB{x!=1$(GqMbdMU$Q@tJa!(h@)mE> zG8tJBG@h24$#Cc;NU?mQ;sisYiw~Ncvx&~eQ#F!orYu>xWWt)0EeCrXm|n&ZDv>5H zU?v=l#E=xqJvxe?d0BUvZUt3hQ%cgFi3x)kf9J`gS_%$$Fd`WMBOeeW5cILh$mB0# zt>ISUdu?KIxcru2oRT>u!cMu`;8u_Vz;raCMqADzcM01mWE8r}tklL(jD{wq*Q;Tx zV=~JvqSdNe=mzl<7+|zFIsCbD8JXb`3$Kc)I4S$#Jd`pE6Pi(rBB|&rE4klB7Ms;J^ui*2;d`^ zIh#Kzma#_T%NviSR^BR`@zqM*^$fK{P$weOIARkqB_O%njRrH$s}z1MAwFxs`ISH>B%2O6udVY2}QB!05~xsG$xWWThrh{(GeUY)F!He495 zkCn+Vqz~E$WJicEi4-KjwplKDY;Yqu>b*6%*Gkn}8Wn|=4;M;SHZ0aCkp~B}`v!fJ z!Th3kSHwpMo{n92o7qnvU70ZTHoi))@q4O>zELwZ+_syX<|%L_L~#=z6CV@4BL=w& zUIAp+3Ru^xO5*aC;@29rpu*vak&Ty`lF?CeLL(rs`PwIW<+N2&op(8F$l{vUUYK!R zs$%x8VZ$VmQ41LOmNkG9CNQWOkcgR?n(?$M6dCF4HPqlvuoc!tQJNneDtWn{LJ z@# zf_l|IhO1)@WmKwUY||zprXeNny49=-NA7G45J%%A)Evy22QBA#+SkR`%n9alH5rXn z&sjU!L97Q8-6IAJhfJ3SMX_nMbN%xszyAP*)7662NN(vhVlU~)x|O+SGg_O(l%azv z^DeIr5IEa-g9A2B(aU+GQyzCqWN{3O*engY&oWzO2;PGteQKX^5JiBMM$<64_3+*c z=V$^)<7wru<<6UKnYHs4u+*wU@+;uTlC-sni&SOm30Xc&l%b9@128cii+I~ZlEE!` z+MxUAldEQpwwj(l5>K>o0Ma=rA(-AmMPau806iC99YZsvlaNoPnpp-*``D=~$72NI zRrfWHCz}FBxeP8p6DF6&#qu6U2;7dTt%%6|g{+j=#?bZI#>x!|c!UE@V!;U&sH+IX zM#~_bwwU+>=1V_}Oi*vqw!wDmW&cP=eO62zZw~}pTuhqn4 zX-2BaqaYS;*sPRWUP(~dXz+)9xjTspWhKPQ4mtj!CMHQW=WJL-yoN&xxqDXHwsM}9 zrWg?bwYEvKVo9;jW-O!prY3)VE2p2N<%+VF?^nyX?!C6C){tO|*f9#=qLxey!AGu3 zoD2zX`A0xZI5pbTDyHk2bvc^>73o>9R$Hik~l zDq0x|*GZPEBCOL&Yb9Z~gJ$|Oh`>WG)P*x&hurHnGmNdeu9XY=V`r?hQr#up zERH!C=hup;Fsd|N$e`uW$d=e#iE{n%k~8?;N@nO2r-lm%UZJg+w5Qiafqi*-VnnVq zWe}gnR#EuOj}ndNp7YD#LbCjZKR1uSXY*F_Rci&h)ddxw7F;l(VOU_SA)_K~DO))d zNI^``#=r_%dTf2Nms0?E62~s3+g0l_w-Shw2HI%>EYl`J9=)bxZT9@5p%I*|oCC<_ zUhI`h;M(<_ROOEQmWZy%wn;FaHM%;RPlGBJnvH2*+v-x9HhE~U~-0Rs0 ztk5L-%hpLgaw#KJluxXq{S}hw_PB#P76D^UG~c z8!!B}X4@%deIYSY!y3$$4oUg+DL_agDGB}lM=(ZP^WI~;0v6~_XmS}#PGka->V}nL zn8D=kPKhw$KwKuNPx!|vkL>xUB->@0(^Qd|$JGipjX>rSD*;_0{*9b&o^Sx=glrra z1iiN3Zh%#aRz99D5g&1W+`VEKau%K#=U!hKF>4G!Vp^z`04r(Q0KRsex_H_d3T}(I z+E+1JnJ;C#!H{Syh*-7pC)!x@4`h>B1fz~nX&H~kI&EcjahOTeSlhWO42ujvTddVq zLCFwvtUJdPSQv8$?o|+6M5C9?^$Kj)O0oG7z}W<)o|Qo&Z6xX{$a^lVgmykYs^b#b z0B5%^h@JM^p=O197HT^ihQ;Hu7ONI5 zCwVG(F{l^;2a;M0SVY*8FB432W&&RW;kJ7nAR-#qgc4s2_ zNo5$pO}5-gOveSw$%SE%78D}p&m_u!9z0{9U7SWkCXDti85CseDc29}h*mgU zy9FW(*x{sMC=n9$m#SxI;JN<*L$Nj2r(qeotj7uK1^vrrzB&n)oo+yXCIFoKTH^?k zU=Yjze17L}7L9z)XBBF$Ml!inS|?MQ>U)T9@Hh}+7Auc{;vgnd`<6t1zNeLXb+AY9 z(OXq2CdFfF4AWB6Vk9Tls`2*2T`dUQ4maZw{{W11$|*kHqP0rMB>@7}Tf|g3uZ^>1 zPm?Jck}|@D6@Z>R_c?%x_z~&~13HG4-6fNprMflRR4r4>T=csg0&pWRvIke0nU+te z%b0-%@ew1?2`}7s2HlM)tj!yFdX?T6ap{3`QB=w}*p6+6v)wTELpGD0w%_rMp8$m$ zv1+wVIU`}Qkrh&)+KmcSGOr#$j9S5bu8(unPn-j8;Qex;`ch!l1tRdr7d7K!1|3} z{bs=-t+x&lw12pO0?N}>dihM#F;p{nlNxC-vsyrs))c@f!U$G#AWUMl`7Ve? zEkxUOv1O*sm)vZ~9vRE_+CsSsYra6uD~M@(i}YnA1wZ)DMOJe%8LF8~HcJ(ev?p~{ z$nk-WTM8j^*8*PB-ngu0eQVjSP-ADUUrnMvGobr%de3{h8!OxmQ=H9M~~&!+nx5BQX&tx9)lp zu0pM>=~!Jh&Q3B0s{@^|!z$71#IfEbd%#EfjiATlbln@c``K&n#_B(L z9Yk7p+|xnTjw)z|>yaj#rpWmR^XDP%2Vt5*>Uj9dLU#`>ZdZ1t{WuJJxliy9qfCMB#Pf~;jC8a`l{7gSdX?#9PUCWC>OATV|~s_f6JyhoPKK=Ofg2c&P72r zD+prR$V4@#1tu$DL3-rIMf=9m;)saacV0Kn`>E2UO9*8*8tTf1Fd>Qs{{a3uICkU9 zYccDc;9x)Cx&f)vHYWB=v-nLrj|*nUxGC0a3CUn6)Z67F?Ng+H=W&uDA&HrY7>im? z=AdR7Ss>C8WLH|LKB|zKn~E)SJ20n7>RmcyLGKd<^AwWkVF>?lIlFBAFnBVNzc4%`Ws`2;lTq|J8 zjwjbiR#^d_v1U6rntoD`OA zhDgi-#c+-*6*di1EI>m^qRvM##v!Y!vf;%H@^cI*IYaGRtaPk%O@d%ULDDf|ISeeM z=6OcbGt$f@{{UURKbNPeGdBr=Ug9cE8C{dA;FYo$S`G+Sb6PnmQ5#5e{g+c>a6nmh zxbiJ8&)AZv#}MO%jDQq8tw@uUWakm7vQq>ro#$cL1HdwP3rV1=v88W=X7z$hBBk3F z1?M**(ATkMC(mhM%yW;&Z^xyWg1vH%7O+~cnaV_i>x*W^hgS!$*(UK4E-*Z1WCX+t z2!9{k%=I&MsbetIvDs6+ZQSyfT(~v1C5FKj4U0@C42(&ecG^#u#^b+2+^usZe#`O( zN^`DIsd;QAC9SI*5&{Oa5m~lEu`T70K$4B7AihAIRIq1MFgkYi#^*)VEb+FrSQCRL zk-#UPx0m(F2}#EhF^Guc)pK3is_jcRG|p{#D(Y@RjyoDHE0V>Dp5nn_jBEJW@xcj z>#GXE_T#H#rDVo0oD@ZrHce|OSxE7Z%Z~B-o`7Rg#!)vPk;mh373wo%w)I$#SP?#1 z66Mude?nFF0!x$R$v6@oA{|kcv1l>493ER6Lr-UC9EW{|P`yO}W+sLcB<4%p3zm_P z?-0&2`rE40Qm5VLbsk?jSZ7tJR%+ZDqPTpygey9f;Ko5i$JHzdf!~~e&pa!Lc#>m55Od;fOLjbji$$yFQ{YKpYRsbtTLiSG_)vTIBF1e4YR}j)e z50LuD*9!oZJdJ!N4kSusx7sVKp)|H2(PD7=Ck3bJ+%=~u_DD5d$Q5Rqz}XO}tj7C= zkiiLH$$Xs07!&^h>bG31j4CVYOd!;3I{0MlNm#1lSu1wIl*>rqfN=J+`i`CNky15v2KPQo~eC`~zKoXc3Lf0e2S@_2~O*iUT= zs!_m)tQsNiTTWKPk6Ol{4REhzi)e!BTB9ijW4S;uCCh-8k2L}M z)*H!=^E0$;%F3$cFJP$Mb?bM%w&bBQ@V}K%7&NVmfs(|qWip_HIGBhnqB&0Gp;`2X zR)#L@;Is1P*x_d*0##P+y|u%6nM-GnBI7?YTh8+{&I<)siMsQH`R}7(%96TUY^zDBdG|w&)E=o4i@6S zIhxj`fYGo-?oku-F){Ny?H;Yq{{W8WkD=V0)-P3e!?{|Mu(3)qSRg`ajqH9>95D#F zT!L3^8+w^0aU?`j6So+iv=QasgFXK74^nVaji3TBKyrS0{|J<;7ZG#d5~_CIE!pW_Dr=oJXvJe<_vB%zjZa zteH}%=!^v~>vhxPSCb6@WoaL+($*ia<>y}3niIaiV-n{xp{V}_t zD_HZoZsa-`JQ<*yYu_$A+RBjba>4r4BfR*|0Ga#jOpbMns|@KT-bx z_v_H7dpyYSKfvz?Gk;J20Hm9fh8BaLgH~4|; z(b6!hN)=^uEK+YUa+mY&d3DKBoTC~s{zaEk?iV|Cky^ca7WSq!sjaO{D20iG^bpJ_ zzZS9vSrZH3Qaf)w4yvB34Q-r7tes0XN~hjg#w@+b3HWjz!j*!!h!og>{>CWw{DXe8 zV_SVC{N>Ubb()7xcnSbAi5#78LFesbF@iToXy!8NiLo$o5m*+oIo>2nA|fI;xVdJo z*IjD&?G=^PWtk!}rqC9NIj~X^LZ4F^1S8H4PG{rdC#4QXD!xE&I-o`nm^T+&?%9u3 zxmt-}>b0dp=`mqX7esI=8JPUJPh4&`mOxg#s^apKHCd5kNhy!IQsx1s#v~WcWQTYm zBZC<^DU$feDpT>ZU2F?^-@ihbrDE$;2~#B&C4P$VErqzE2@NE=Hjz2_>ZUcg@*}Kl z7bdl^vYJ)GPO6tC#~S$&3b`<5A{8;hCSoE#+}eh7mYa*j7DkL=ODcx4jAUV~$=M)} zYu|AzkzvOnncwoC`Tcc5OH1U_R;E_9RAZ=HQDf+<6y!oD-bu6|4G38&p(8&VM3|Xv zyv)c1-@QPxxt7MAZ7jPkuCt}i6R+q9{?Oe;7C*@MIUU5NA}4ttg*5D2zgf8~ek)Mw zJTb}BUDlh;PU6AYfmno2XjdP3`e345!9t{Cz5DAKc+U7kcnw=Or| z+6!fIh6_I8!9He7h;bf8B|l+qipaB*JwKMKHE88|eVGNxsM|ob`k2;4aI|LD#K`eb z#Lm&Y^a9J06zbl>+rCT4Rk*ropel-Fg)eg}uK=+I4q#SPii#wHSW~{|JsWowm%2Er zH}s~x9AJu`RU(m%S|VW)E-PUK)TU-&SRh+E3yH+UbX`hW3+_5|doJut*Ynw=-8B6^7d&+~Zx!pphS67OY`n2nmA`u6ytOaX$hv3loleZ(^w{ z9fNjBZTnyqRU@$Il^iW^uC|zr!Zw2Zpae-##y)qV&NydT0VBJ*#1`tnjnq=lk0^}L(i8G&EK+vRDv}?17x0NGc!8+m* zT%}d(kRg?-D9AShy|Xj(gT@<(Da4;_pRvg0(lrw#oCKt}Nq8}GeWav1!6m{vnn&U)r7 z-o|Acp00G!_YSYIO_oW^+(Wvjf=7WwkNb((Tbg^8o61|u*4n%4HEDHqB&vc?z6!Hh z1th{%PIW-UBuGu3$cg>+UCL}uO?s8tD5Am{sxy#}yt1@R@nEU|#X~Ee5V?Vm#Qy+$ z$#esol`2pR6|>gqVV+-eBUYWaE6}J>$%2;K48MR&q5`h5fv!31t=4KG`Wx0gyPIzIUIQj+0PoahELJUPjqwBJyHv<7-N-{F5M$ zXof<#@;HzZh}@2L+{$E%m>ag+cRTl1@?y(O2x6opdN%IwS0N>olSMOL^X06lG71A-vA5yVf%-BUvWjD3uKWqUG6Kvt zfp9Q&*vR%sK@kMNaUaKMkBQt=DsesdAN;T3bs+|>+) z%oD7oAj_6v%gF#RA+1u+nM4dQKeP;awjDcfBt;!cf|$l-u4XG`ZB8UeB%9b3+!BSH^LyB zLbTn4DT%L;bGJ3$rT|{W`q2r68#}voauGYzyE*B8d|( zY;QYEbOd%Cl^ni59J{T>VeZx+v!hy(MbAJ8Po}iM{j5pzuMCFIIfYL1F$1(2TCPpp zJ|i|Wi6Co`WQs7PS5z9gFOtZT66Z0nF9?I)CMV!Wqb{bl0lC_^>w3cifLic_TOjlM zPrDLff|XsSB=Rw7k+7m-XX0jLW+IwaDrpK>&wuk+oiTj|N>LhCYOWC+!RwaD8dy>! z8%M!J?fQ;@<>%(*rA1jas_lBIv}m?g7&V%u#?Df`W(4%XbNonRWwKEaL~=pJo9fFm zA$t{+b~fbJb(;+1$@d_F5^DowK2;;k%!^>1`0e;~%%Cy$Ka#6~#5VCMVM$G^5eju-5yk?WF-!Vz`Ny6s*A4+tn@nFqfDXutPK50Q7n!({G*o96BD=2@+?tTvoVOvY8_LO!s9X2&fRK4HYS8g zn1YXh!6_sVfe{1zBxYtKmhvlU)Wy=Zl((s@;4IRKuqd^9QC_l&L>#g|WFcf?36WC@ zWY2Qr5?rJLG1`$gmsajXXl1BNCbLRTNpzABEB^qvgDuF3#v~*mF%nTRJMXa7(O)Fo zWl4jaoF!##^|EU*RY(*#{cUIngnMq3Nkn@O`vzuUVj#S&D_;ZPfo2}J2mIuSdA7ex+7s4&RFv` z*e`Nj4ie@Q%6Ey0=ml%3jK*WKj=>wW^jNF*`Fd|tM~g~TCh2T6WxR+XzVVIrARYez z2-y5KV7JGjf^%_zix7Cq*4Fo_6{p&XK4QXjK{2hE+@e1%_W7BZhM4+5@wf}O#@`DS zEl|jwJdjD|siLs5Q4dQMC^?XkDUjYjQPVn8cQQ6JH8q|gp4#>-lb?+mHC`)}GSgi8zj)iz6o+fTcCaWnu*f2gybL zqB<1fy=yE*+L$oCO5^j?(Od$-++%A*8|%1cMjZvlC9j?K1Bm|s-1O~O8ko^V?ACKK z=yjTyn*}UgRiOU>A8@8%%2+e)jpvW!6Z?*)%6f|dQk{zq#L&EChRYTU#^qu+nOf`f zf@2^VACbhzOuTe0i+rLr^ov|ocD#SyBiLG!mBF~TC zS8Z8tYVC}68wH8g8E~y~w{X8lrk1mQJlZ zxlBcuS7WVX8T@u3Bmmj4*qJqyE@D3zpYvo++x*Gla<)SO7Yi2pdVNBc6D_qa$62s2 zEE)7H2KOAz&kqb&rh^qy!he=D zeX@igy^;z{G_1plw+LBsi3y$ldMxZ)y4+4GDC6xSohxT*%+Wu!N{4A6H7LePCLjP{ zN7u+CqB%)*9Sl}SBm79*u@$o|=KN)3t;Jm>ArfwdECNZeB(KcMXW}D&j(`s#Tq(Zg zCx(4D6PA1l6f{_*J9uP|q89EG1WSdwOdwq#5$Tfy8&tn$VqQ&QmL1Q386+k8Yl*XUD`GD>- z9Jk(jk;z^notn8nZB(PxS$1)`FSAzf04#UfA%rosZ@=7N%WH5~|RR#sIFAME4@_&>QH~#EDjZ*XOY1IjAW;H;u9a^ z6VQ0}2lAKcRn2N`Eo!NK!f{om$=STtawdyxSRnzjGhmoZADM?o&+niUMug^$Bh}%y08IigA$C>8c}*)mHSrMTzGO8G^J5 zk6^#nQe#~_ibIf)%*x6(jiP0C9=2x-n$G0Q7CNTc%7$N$#wjhOU0@6R+zvO*frygk zGXpRqv~Lr<(`L>V-Ke*a$mOyJaa8hk*F_3#)sd8{%$%%Cav2z&Tg;G=OwTTWj+mn@ zj<$<+Fn23n`+>+N$UU_k1z~Fy?I6?>PA88dm@$Zn_=1U&XyZi&5}GvED@07wePvfH zbfz{7B`~=sgs_ZeV4_GquLcEaUANyrE$Rcgn1f5lwPW#I%?L_|MF7 z8UFx|y;OZLevS93Uc~u7=AMzm7FnhibjNSlxs+2NVG?ZRAb+i=$ALmjN&wwh&DK$3^np*WAGnnI4mUr_2zc?|*5zuL z%`Jnggwt4>I&%v}3!_Gr5EQC~b^ic@l(5)Ea6EX3#6%3lf3Aviq}UdW#gE*GP9yv$ zd5wmDaUUCh&q-dAdsKeb2mtUAiTID1pNRhe`}7A{*24MIw#3}P20?`X0kZU;AAvjtxb{3Y5R6cWPZuk5;6g}&lv{~>-RGg z5ddyy$&aKze0v1jJ=^+N_kT0k{ZgCF&9_aBgI$IZ2=b`6OGYIV1MyO0)I7>!H(_RBa<#z`Vnw`ASTB5Q;7otKohU6|qAz&>P75$LDU)d(Gf&5})QT}H6Ti{o2bT)?i zb9rTk*56SM>geq*#Oi+IW06L_OHXH5xNs70E&!_9KWOq4mg1vVIb_KXSaaU*8a}HZ zO5Wf1Lr(a^-)`h;zZr8CSxu|N^0Sc9wS#UaMQtfM=L04EspCJF{Eu1;!RjnVZiwJD zg?f{YZRT7?TCye@R#{`%=H$1CXf{cN!kzXl%4d0!p0MMdLgTUCH9JMwj75Ii*|v<1 zn$^`Z$+dFPQ4+}|$j|t|u04F`_EKdN`|Eq!*UFydcp>yW(q2IL^;bt^`kB(K>T~>#vguX;A_;|wFO0-a(bN9`PmiYdfY%+9(|%w1{{Wwy z_l_5>*TwetMy|+COSl$YJO)=}32qwhV!}~bOcs=>#LdHmb)A~8$d1l89pAAZbggqcB>tCGrXqE5LA1WHK#yF+ybpDXKiNp-3D?i*XsAqh&FL}I;)mpJ{KrW2RQ};@zP@DZ&{1tJ6Qb(K#`piBg z`E9>dJs+eiLhco+M`y#%Uu>_)v1?sPMyhI(SbwR7lyxLyRFCA@R^4VXPxmbSaxPFv z$FXw~aXT9$X#935{{WKTxv_a{UHe&k78Q-6%4l7+2_7~};;{O1OU9ZyJ#PgfTj@)={i_Q}M&kCyXm(5y&5-B^j>?@owgMm*0^?`2@ zw83?6YtN(Kj{W`Y_Nwo9)4R&eGvDht<@?JSX6-vN>EQ%uf>y$}NJx|ku1oX(04a%x ziw*Z&X|b;9)0Cclxc+1v9-g_KFN=;1DAviTw;6WDBCUtWAxpA?D&EVk1Fu<3mq7bM zCO$o=?7k0F zWN|rb)o@yd)26J_x_+QT0noE_C1MjMEs%}Bz>EI?a9vz$&)90KuC7l#Nww&NzaFay z2Gy=19K~XD=!)=X?374JY$f~JA}mmvlvJb2WJbpsH7@68VO4&zbgpXcTx)R^pD||B zD_O3`s$DIWBFHCmFLbF!8L&g_WS1O?X%XhAf(bDmTMR@ht6v)PtB|E-i^(x#kg3AOYPkk4d3$Hr z=1J95`a-d!i<2a$mhf>MGqYOWpU2Xtm3rGmZMjwxmQUCN+R1_Yt>oCxagd4bMX|ty z{{DK4H=f612U4?8UL3VKLK^uxsB%&km@-*R@3+7dT*lG4m!#8W zO&yQ8ue`+AdIRqBLsOE74hR~!k6f%EcZiY$`1zTZrpoHnxnaqjjPkCgS4jT=jD>qj z013@uIDK2FWqSw?+wUXuze{s1JUvONnaGYiC2|VFTasF0V#ypAs>)_r$!+Fr1TJ7> z;&zGNdODF@ub0eVsNNMy>>_KWllJpke-JAGu3S~bEH&~xW+&DKyv%$6)fcYjU8sqe z)m*7QyI#7G!TW&NzE^H$45M+Dl*I2OWgqLSNkcEWivbeK;aFndZLyK7ReUB(k+Nju zNVkaz{A8kMZRTgDkgK^SWX6B#{YO%z)vmqLyhLQ)V zSQXaK>J2_v+*OLo5dKfr)ZbsEL2|(~6dVbx_hRBAWTt=0N@NCsQ!+O$>O8#_E$o;| z7|V)6aw|2jnj_mVD;P@3WaDWem>KvOj-$s8HCP<3CnnLLt2H97GP1GrX0Q?+vf`{& zX+e7wO^;;EaoFc*<-Chy8IHj=&HTf%c691hUKNuob~Y8f*0W@rgpLJlY@{X!jIK?4 zZ4(`Gg`B>evvt@!J`*2r9|ZNS5zf`$YRKE@_d=A#@N7hY52TgBalEXC05c+Dh8{XH zjXYW#0cBOtwkU}^gb}pnBiS+jOW%FADWA`$831-xmMaf$4tWe>D<~CNbt}?c7@H74 z)IqtiSOAI&T!;c9`2PULC#y{>c45f>0H#YQ2UzOS^i{o54Tiy+5({6lJuxv1g5$Id ze15uw>foP|$o~KVPgB##Hn`g+uq*P&t#blGa&7*x_9(=pf64$Rf7d`#f=%JW!h!jjU*hRN0LWliA&}z+D}4%UT*-*~mn227%p43x;XMH)Gcu{k!G=FE zZo6)-#NEcGx@--ChKoUCB+-b7IEF!g`FwunSY@$zE-lk|=*NlKLdk3lywzBVkQ(5u z!9Yqt⋘EL=t5?>~Rq;L}|@jSXo`_AeupO*Rppe$h5>gvUV&meacYqYJhj&^M(Ha zh{Q+3s8+_k$~a-gV^vR+6K?VrF9(7ID&MgnMqp;KAq}QRQ9J&lU?j0^89P}$Lt`JC z$yT)QU1zz=G)sw$_lku;1f_&1HXw-Q``@UUl)rl3H0#bi*-I`Pva2|pEazFAtOKI5-lDv^R&bzvsWE; zVI;hsQ#D*r*&CIb`d9f6t+x27q5T82K&)fgJZ2_%z(fE=R9LLa1D%G1)PHHm-a$wgI$QZ&F42TFS zW*`6;5fKp`D!!`KG8$tR!N%dP-MR@ybwbmQ!^&X2PmF8>0vO3F5i-f=#hXvW{M<2? zrj1FKDtN|Wv+i^S>&}HbQ7n@_9)>PC3Bt&PL-B||kC~o;M`2V`Z4|QkFZzzv=04Ig z2*?te;$ujp!D0~r=@}9^=MsCy{{ZtZY0PQ(EI1giOprvb5OFi^AZGhczycv7w8Zo& zMmn)(a&>U3#A5MpCAzxW*jpkE_0P9OnwmoaXD6s%Mk-_zU5Th`qkEaC*;OPU5K^*CN9D9gg5&fN zwZzfNwacvR)yj=*mNK=>lrE^Iq3+U|SGrcoSFFDokIGy>`sf8YIeYBbW`4Bf*f_(@ zpb9}NWUdl29>`xWeTV|&Q4{|F)f2qW=p3SKUBueTzjped@=cR~c03jEZ$ z=kpu>Gray=%#`gqtJeM(7ke4^F{aX)F{ew2J0`J`DF!{HVOxMOW^(Ow_6We4vgtT ztX8LQB0E>B%s|B^iTLBjyTGJ2oxM@M&@mSKX}+FJa!++zSryTj4U}V;wn8MJ1WTOD zA|hf()>Avka{mC!sjb5OH>+fI4oYrKGfriRxL4@rtG&ONwir-*?L2*{CR*1TaM3R<|64XTnbIVLldoD@j@F){+hB43h=xI@ZkG`cJi zF)g8BT-PV^6A)O>v7W;?{HJY(K05lVO&hvaj1EsqsWRbu!jl4hL`3I$%|HT%!7*hc zzcC(r%)_QSX2ZS~sm5nbvp(vj_ZOP1vgM$7svP4@FM5NaUTtv(q#Qrl8)eREFP_b1GeuvYz&K2oJRl{viGn27oj15nOgz$j{ ztRagQ7v~UB5zAP`{q|cj%{P#1zS_PwA{STA#&uyL!iW{L3ag5E3^;!%g%I!Yw?#E} zF|NI*SB%GoaR`Zf>Y$RUX_6ptF}}hUL=hh3LQI({DVc(ffcWZJF}0ee{VkF~1FYSa zQ5zRhx`4naD6J$r3k>+5uz19(Cyw#dZ?8F9R;XuDjm2A4=xsP_cXi^h3=tKQObiny z2hH>OK}-(+0D$z*EF+V+%hc*-mqws)_acL?)>8KgF;LGElA zdoaVqB8ZiX!FMHOofgvusgke@f@zqN;y@V%JIDT`U@eRmpTg=2Q+rN#Ben5|70f{lQJS?q}f87EKxSVZ;AMb=>Gs#e&$(}lZ_l6 zl2Yv=08SBF`kZWHofce@Fj$v3h5q71r~d#hk{(8`0;~o*A%e)}j}*YHV`+xlEFj$3 zO-7y{;sXHBkIYYQfVf#kwS1*aOy=?8>glhPtwFZ!!|B#w66OB$Oj#X@S;V=@_mYjb z=ePiG({8ZJmMLRvGQvXrteaN1U#skh1~>45AW@48b;K~{#6cA88&^{n2apQ&>)v}Q zJY2YX5mahdV}w|?*pzUS00922zfwQ*(AdpIQmgFcPDU!sPw~J@iY?3&MnOm?r4a(B zF>^8@0&-Qrigtk52`YNW2!XXuu1^|+RjQbbo-gef#4ta&!||VFiOQTZWxqY=Xqexs ze$~}3F$u~(`{}NvF7QZ^-4!ZE1cZo%W-$>Hw%@3ox<4U5^!jqqk<(#TQjT|6{mFc;C*osnhr;9WwO^CV zswG> zQmxwiRhc0M(j|z%2*}6zMD9h=wd3t4DU8R^3-(&Cy=~yKvG6>cH|?>B4g$v zBxB+_0w_bB8BFY1W*w@$MtHi*GaITTyNf2l^86RFqi~KBGs|v7Q#bA}7pb)#tPD1h z!XrJX^UkYPRB5d4--q#l4ohq*M~T74@G}4~GX?XsO`!Wvt9zNOFukzt#*n?-ZCRL2 zVyCBJWX0M|m>G~Tk}wEd$0DDI^#c7*w`q-CV$Ff zc>e&7kyyCWINyFdelb7lKlRWRzQG$LjEKsAb3Z%vAFyC1c*K1F0LMWY$;?c|N5y<{; zOR3+H15!aF`<_ud@4U~){KU5zHX;n5f80d;bjN6kOCll|B?Kk@#((A~pe+vGcH6g| zsnaxcFKVwW>0QpBuDSHR{2#9I=v^oq!DEl)%7DOve)a%tUgT{{VBoCHZINmW=Tqx0UmG zJZ3|<;F>S*l=hhR4&sV^`j09FO~f_MCw;*J9YJ5MN>kTPZ1^kkO$76EodlxHAe z4TVPANlN6$c$oZuUpsZ8e?9()-HP&B)^A>RL3BGw-nJ?)SXCm5xNM-oL>q5aB9Pi3 z$|R%xOvc~6_G|S1@6Ua^`{L#!y53oKb4vCvxdVFjd$`PIRd*_~no1%p%SIwYa`vo( zf7)cfMICDg>QUzonC@-6o;hev;^y?GLuA5kvheLgiNBkA|&zm2_~^1)r0JYGvh*;f>fgU!SN9{$4)A4shVE9T$?5*;!c|h1GuaD90~V>73Rv^tFlmCc z*&v^WF|)q&9b~^_ygl!i)z`^CA$-r$I;B1`c)zH1qlXvV9JY;4ZzG(kUP858CYi&( z`s=E-WFL3emuwDoF+2V(6@IH9`PxKy{&FegxSyQ+Dp;ZyMdWzG) z1v76RDGOeGT24 zdbXA|{{UdsuIiwyQ<3{hYqPz0G5IBt#?iFI>2V*42MInIh_wflk2610AEpPHUSoNO zuKeWi%Kj6}4$C91a`0ve~*? znvG!bVI)8m1i9QDCoo(5?a~^*{{WH=jRSY@d};7!#*Gi-Wdo01BPT{_X7vU&X`XVw zSpB*g0%Ux6@*yaXpVv})hfv1PZM_I@Yw7!7@M(HITgNKBlJaX~xw=;|+HHNRJ87BK z{iyCB!&Z>Y=p9R2CxpsVH!dq&qi`gpkW%9x&SF&~BM}j{@nAK};ngVIRz}n6x=HK2 zr&zpcX`mA(06~P7nNu{~ zeN|?1c?xt;a}|d4gAIksMq(j`L;(vQ@d4^jr}c| z8saWtEZ(t%#{G<;$_N^Q{{Xp3)PXq-<-sFl6ibdZ;zf&+DJkJA>Wn^18+Mv;EXdf) zwbV++Ewut$0&bR$g6GH+rxfASV1%YH`fSH({06`$8dNNI+}Ik<9L{ zC%^VgM9Cf%V&oP>Pvf!IX(kM8fK{VdSPK!8hDy09gy>%F7?v^>yu=FNhDfdaJ*w82 z;>0mn>`Lj&g1zglsa3R%%k@-UHOyDfqQNGzvtdHL&({|oR-;`9? zuaP4v+b+Rcfi4PUv2bJYC;C1A066MvUw8Z~Gov<@i;gvyRZcHO&tv5a20Y{-1R;k1 z09<_Xj$7|P011*mutq-pHP;-%tnp>M)gl`h^$^0=s_uCRsgo=MAIEsyOP9{ja4jEp zc>=A7({_PEioX29Ui1MmTPThh$ewZxnUS5ikVpESp~hnIqghcyIb-bp_Qgw?YwKs) zvYLuTHERC=+?2pTK)hx+#F-StZxb>tmIVscomseFzprEtt=29<6Hm~{U6PZrJXl_$ z{RcQ9lHcc`CBi=uM<-g)ZRd{B%2?Y73}psbCa@PN3a_Z- z(Xma4_PHwGj>jCA#0n-O**j0=G19Dz3i2vOXWwTRkxe()q{-azKyWpeQ2zk1W(FYG z!2$S*Nl*It0hixt$gOu7a}}1-cV@a)$+e7B#h0n7*sQQvFysin1d1YH;vjxtHifu+ zE;|#HxqAq`4T;LiS@(}Ly@P!+xgxS=Y?Ukpkuofql9?$9{6KoD&NB~f>$nOK+P zF_-CIb`e!=D#J1Wx7r_Dn%g4+!5~ZRGK!b}7}Uxg<8L(`NCsxFthg6lsx1CKT|HnDvm!L`0VJ75iud zYf@RbMjr4(#wxAwcR0YiWw40cFd+jhCCtk|MFk%c<`;|&MOpaAzf0J1P%5i_wQA*U zX5jw#<+|bD)jO1okrwkMJM{>w7iP1wcQGi;P**6LGE;GYZzcO|k|0HbMX=1m{{XM! zm-!J-KpMnT%GtFWt*@YTXBVY%GX%m(Vq^2YUK)**FC%UBjZ0Gf5 z5=mK9iIWTDc4W-VOl=!+>he05=uV-`>A<>q&~kr~uAM-{6^h1j>=13Jh+^Q2etSt2 z{yY9L5CyG_&-*o9tw!90P}7W1&~5uwi}^dLkRtW9!bE#5p3xcQ5yA&(js9oAR?cHF zm9a*oX}Sxl#EdxK_FU|96q6#c?@OpeZtJDT_>k*Bs^|Wnr}P zFrUcI26*id0@^sq##gxv)cGu~E~{)tUH9f#@LU8j#|>myM<+=(R%#(G0zZsxyt@_Q z@%JYWf&T!b>7*jYJvVE?+O$oJONgI$Fp@7&)Q(Xf1Wft)o}vtu3|Z;yc4i%Yhy^7T zab}g@V>pZjk||iF3q)}lTt_ck84)`i$5JyOR<2;PkIkyCMh3q&KP<4W;cP>iblOwy zXFCAMh+ISt(GfiX7&7LO$K`3wn)UT8Dp_yS2{5A8O3GWgzDUG~%ukF@$d>;A@2axD z$6+!jiNnq$vQLgIe5-nv+_DD5*SEJ62JG4w8%z+C@AJ@0ubBHcQJTubGPR_-u2m$g zX%8y^r^P^QLkh_*&>vArRf2NathRU!cS@RUTzjAa^CZ?wCyB6!jDo>~F&w5NXxrt1R*$mA z*0W8qk4YP5qmo~I0`F9(gLV>OE7$Nl>@y`Xw8?c1X5?|1k@j2~b?a6K%Nap-SvICZ zWUbhi%os35Q|pc7WmKr& z$p=Y`;bn1f#BRwax#Ce3T^vOVQ9#7c<0ca`GFwbUIJB(JLm!B`b!nb* zgiU%OW7~C8=vdFE?i(^DW9*oTnA?m5N5pkid*IQ%iDj^T>UL#K3|JX9nx1e)HGxo5 zBvKNx_Lj>T-Ud0$$QO|Ev5&VEKFqAK^rO*M>d@a=ls?rp_MX>LO6P z&j!7>&NvHe6a?;5=@bVjo?gLZl>S{W2Sy($Q!!@|R^o8Zgx-C-1m>p8B>jjw;vz87 zlack57ZoTAeUE5%+(md@JZ-FYC~6IRG@0YKs;h z6K!q8GRhFpDVY4CsQSsGLSSd{BtTO?^E~JaK~&R;3_f;^qedSj_g6)l?Sqe6{j#fb zRl^c*mXc+1nS+?RWZxQvXzW=YSpq=Ode`JG3~oxz|{_w*qQW4z-<%p zB|UPt@wgheO;wK5tH%SCIU05Azk0Qnd{+yYv$KJQLnjLX_3T42^Ci&~#)VRaWikhS z#?UJhIFO&RnPavDN$>+HC@tbfeq(*-{Q*7vb}|juZU?9oHT^?k;{Msyl2zcjT!ljY zhD;_&&*WU=d7qA$V<-!2)hudiV=T6_Et1m^%Q;jic$&WIN>#rHOvdpAJb&YlNq{OU zDpkNW=%J8QQBhaBTIu9eUx`;Veo9Pj?>cVZ`_&>D{UFfd0G@E%kA*8 zDk9ooUnTj>>;VxYG7z>DPRGz6Y+-O1r3-NQ-B)svFZk@Mj;vOO0nhaj+Z7S*`sV_0 zGqg_I%*?vRB;8*3-DWA&!zGg@!B~}wibM?ZSuRFcGEDd~891N!v<}kPp+_5Tj#>3J zg0of4gHs_V?^78Jh+6*K<^pO28%hj9VE+JH&j^THlC2rF<+I4YZ!3;D$Wll0X)A~p zz(=s9fdFO71!O8M#v_bJ>u3Pmn2HN+NUNR6Y5SKfFVPhkoznsl1g8~{{ks1Et^?H^w~yTQ0Dv;Z z#?r}Czd(^{(5(2avPy{&KU{{&n2?c$Nkqg1PT!PIQ(U@NOYUR-_fXrdRajiBKP~nf2rrW z#POejj;P<=$Kw+vB!^C&%zj5C zbyU?>7_ut+XhQFR2i zyPwHk##05l6~YclQ({>J$%cUP9ik*=Hu#y4Hk1DV$NRf|fzci2)t$WVMjd#MA+=JI zPe)`~k6AXoTI=Ha%Z2ia>>)6dX7MI65MKa0dh~LNoC?!bi4j@Mp)f4r>te{VMw};eU4Z$8)sbbah90b+d75z+hXJO;J5FOr{FaRfUL*J={_R$k^aO zx0!>O>uVL_wWA8~0)hh=a#lQnq)m=oCO;t&GAQNx{r>>2fWe<7e6H=Eh}t%vcD#}9 zHC+*=a<@LiO=*mNIIQJ$9np!HO5{dP8eGSg^TGxPd5!*WuNi!}?BA96Jy&zK8Dpod zG{MxTt8|^#t(hG!YQjaTO_3Qbia>>tKk$G0+pKBwGwH70_W3c9^2WBb9Z9d}4^s<^ z#A@f8sOh@vS;-D26FgDuHp!WBgEA@RQye<-Yvs>zywdtp{Xz8?k{T-$sXG_0wQ_Ow zbrvviGm+E%%}sA&ld)G4gLLg6xS#BTiJjsmIs@N}gNYcXR@KA~#w9b$afzSb>*i)Q z{{YuO%82G<ut-#`C<)MoPn}wa}G3jOICi$3PAhVonc~j^F#}*FZw1MCW)C{{Vmg04|hj9R4Fo z;WPS^QaI@8%w9gmO6#FSQ|MT7J}^I#96;{^KQlA)@z$hp{{UFN!85+}J3HOJ+xH{K z96qEGXKy=!sck9RcT%*L5Eb%JsoxV|iK*EQ9f(+><@s^w4F3S~H@6+n?pB<`;yYE` zja{HK2*~@KtZ>$Ce9#g&eZD{)5_3`@*hDLs*rW|aGe4vKaerX~lOi0YgM91SD0j0klG(Xihy4`~B=Z`(G*8c#;_E%hES(Fa@ zYut^RV~?`eDqK2ZnhF>=m=Ks)F`!UqU|=C)xt9Ht`b1zcTC*+PKIr{GGF|1;f*i86 z^uKCqY5FWRM1c$P7cV|r&uNG#7~UdxkD1@OKC8c3KOg*L@#hn)bC^=qdgoNtKmPzt z`&&~dILw8WsLe5z%810BTzDO3a43P6h`?>cW3M_MCwWu#0P+UsXgq`Zk@u6!{@DFC zGI`vZ>9auL=MSduf|8~&j|zeo10JZfzx$`xY8fzN21K@O2e&^QHAii7_*v0?napPO z?jK4*@i@$lY&AS~KkU-6F~`&tEooa-2F_3;D}ats^26&Mexn~yZqa=;yHlqwXG)H| z@>f%nr?(mwjgnjP`30_EGI;_fY>aXw9&*ki4cm-F%uL3$e1-bPcp2gEZZTRfz3NyMj*NaxH}7?JGI`9+V^M6 z9`Sbq-;Lf6*3@?LTBo|RrN=Ll?hdE7WtK{wp2gbYrb!!C1h^Ri6fESXXJF_&_j0?f z-aP||?RRr~soyPe*lbHq#*xuFk1plIku5VEg^X(4bpp|Z%m_&O(*w9f5uDCO`K$Fi z@`K4P-1Z-`{lDxNj$S2pzqfh7&gfkmrs-L=Zlebu_D50fxMG*1*2cO*6}ZGN@+k<2 z!F@B`ZpZhBEpr=rIA0I@&QC4tv6dO5@{a!i@r~!%h$23Fao)b%V{h`^fMoAwyp!^? z#f>T5JrUxcfA!{s?e?q3v3cDsjIzr(>xyB48^*j0{e$2Gf!(XX%wdE2-+cJ`X=k)Q zkzQDK2eRG9(>L^OZ1wB&c8C2~+*Ut4k_I!`i?uK?a!hd)DZ|Z@J8wIV+Sbjhx;hUb zQxn|0GqF0d7&hv)#+IV-%Kc$=EKx0XM3%BxO4|z|35GA?WRmHe3j3LDi&3RV3^7fkpG^t{kzzP0EQEGQGh^%l+5IAVFj-7j<^xFCm_Dj2+ z#?YPX@)yl+(H;um7DKeztbKxo$#G~@EQLIC&c$|#gsq9NV=zS?&)i2*eKLIB@vp*e z-T7lzRQ9K|z5DNep0AD8U6jdcZ6brrBjydLpqv|5L?zA(* z#uY7Dox}C+*Se8=M zD$ngyzwRCqEv{1Nu~gVo44AC~QzH=_L)~rCt)oLvic+a3^)=_#OV`l!(388WqgRX_ z^WIOV$9?d=Heowqs=k*VHzPTwwC$Q$ZCl!2=DxP8r)2$bc5+H>d7Q%7!FMIGlQBEQ zM1Q>~hhqGNZ(?ls-&SZ%Z>YOAS-U!KC!4EsTsk);TZ}%QUb;u_RSwK@)=EE zid-_vX)JP8>AI+j;1>j>m2e}2ju8ZLGr0c%l4=j}3$M*U^+@vY(Ilk5aF@FS%!_rF-v|OUU6;^`ci|;6iOy3g*C=C5-JeADjy# z^Z6+KM092@!rPIrlcQrxUnH;s!);l!#%h!j;m|8+8I(sM+INATQ~u|r_;=x|7p0h* z4>wx-xy8`N*^$SaVmM!8h!^(X*td*xgEQeWMKLiD(rrp8R8~aUdbKvT)5VY?0EjMZ zY1>-Mn|G9KsQgF@Mq~HB4T-8d8g=U8A5$Q@3dxvJxeTx=Mc!%=Vw)bo_j=nS`Tl$l zX`VA3EPdj~Db2N6sJDw-gle=7IU820<#k}V0vRQ!6AlmY^vp!{bK0oPRXQhj=2>`5g+|=`EPV+^17hDuw*)C}N}2K^}k48hcDE;Dn&qG6c})`q+=-Bq9$ zU6)BIqXffLj9uBGXMb3Tflko_6UH}*k@2+IYW2NAmccr;7SSKOYhD$ZX=)J&8M7e* z1t;+aG0I^0@3+T5PkmLXyuXIP9xh1LQl&s8G*7sN_FPI?gt zw{GP&RKKZk#Mwe9O;Lso!vGQ$LK^v#mHR1BlXc~xr|`rt=PulhD#MhW7tao zOqxF=BH;f3;ROZAAbv}f&iuOI)zmDbmG$XbW`ea+u}x{2Uc&&%d`%NrnVoi6bEqlrSKO`4NnA{toa9jo=R_pXRQ84HfOEMkuh_>Xm`5FrT<;KsX;L++%Z%x0%{wW_e0@i*3c~yD>)6 z0?Ltbb-vQ#$uct}h=)2zTQb;akcQB4ynazVDKVvldh@ob*rAsDyEav4@7--CVXzq? z%xn(B5fQnMwsx43k>e1|G3^ zRqKd-W&>tMMODDXClaBO4lxB2&{$VwacIP@oojhIHy2YUMfOP)O3cb+`MvE8>=1*N zQUMXn8E17J7RAe2F5wTVI|B3 zM(@*_QzEjM+9UP=wdh@=kkhql;_YMcT*;_FW5v&@5$q3GDR2^PI|V=3UjQDhr~Vcz zIg_VGu1(f7mBmp86Oxn)+)QD&oY-@cTzE}`T(8*^5HmY$TgaSgIxyGU)jHYoW#{OgL zCVy%I2xEAd{qz8}*|iLf&102buBuz)&9)8&5co?LB%)hc2NASKDcGmxCL$s?4$w|U z#InA1mowzVG!>2-svL4_pTIyA46$Uj7{ozu{blj-)MG~n_jyd1(#GHymY=ug_8fXs zVW47hGrUV8B*$@$h9-aJdZ9MmF^jI|=WeNxPEgpkb|Rzg&L1HRi$wl6iJ2eM7LjWdYz&SC3x-)NbJAd?rgy4N zNpY~p%)RVXqb)vPraN<_#1${O$6Y4;um<*MI!Hrp{m; z(Z>7Cqv~IfN|tPzE=mA68mtVoL$f+BWaLz#4VE( zz{y3(n-dVwb0hLHnfRJDZN833+o?4LV&3pwo07P+u#;In889$p8%Uf)Y{-a@n2vzE zv1U}MsOe|%*d#nb(G@|ytDk_6+z6?g1e^>>48#{MQ!(?r^Q&tMrZ8C)+sb8oZBt!X zwP4;k3c&j?VLNRFHONI}@xQKPly4vEW3F@uTI4Rmarm5%-2VVEh&vG5mKTY<-*7Mb z!N+1rtM@O}UP1?9*oHQZzF%Xq+M0#S(~84gx15I#>bEt$b(qc)R(XjlgX5V=Dgt16 zMDm$3h!am?#~LS(3zt zbJb@XNG;4nwh7L#2_WDzESQ-h-F#$XBY2*G4w`NP&Ym|7BBRI_CZSkKF-WZ>WZ|*% zv@e-^1#$SnPbm4Bo`lHLxp{~JU~}Y6eLTNX^#A$Mp5w*w*5zuvyr=ChW@3>)v;;k zNOFh}$B3-Rq8gBaU_`=X6*D^-bN>L`ZUxPVO`5Kg(@b76gyki70ShI*5=2545OXIX z3tXp&1OmtBh{VZmsnl?n@}ZKwQ~5dxIHuUkHGmdMCD9feU3G-OMpgt;CA7;o{mjgy zdM@R$>BrcNrao6oA9Q}l8dr+8FxM>v%SBLjl2TK=9Lx(nk+r*ki;i-1xi{YF$aDGgKwdoR*k7Rq_;si2**Q2&`58oVf|l4g)h0 z`AY;C-R$NoJg%Vg=+`z+RWz5Hq2 zxhf;BO>52SPxLF)UeaQwNdEu{FIkj?Nd7y>9)~(%0RF`(xdcmbg8}9YZqvoE5%4kBsLc`H0n}A7Zp(< zAHsG356C|m+x~a!^O@4!l+~U_TZ&m&+aH+ zfBm3k#F)_q$ixJUaUmlqi$}*ltEbdk>2sFU{ww)6PrG`@8;PwLmT3)Q!~X!Pv@M}2 zbx@&k&{1NmVRN)Ab&}js9~JEojy|5z1O9+TAB@q)mN+M<>{{UNU8-MCQ&69u4i!}nVIJsOh*3z z^AjKIpc&b=zD9Q#Uu6X{<2dEd{vZZ_^51>u{x;|@wOqIE9~7!Iq)T%d{3N~xA_MZ; zcKv>%=6~N$Z7!XMteyw=QTYr<_>BJm%t!j@Tfo|DJ6`mtrVNU9nJ6Dw4?Vx~+E^ct z{!(1y+PnX8BmZ{dcYSt@N)0mqAOF!cqzVEipwh4)t zSHTzVHuJyTKGOI904?I5c6&9c`{kpxZ7Z}YVGmGhtZL6E6jKEh11WWX7TmYj1|m>1 zyCygMf@iNszF1}GJ3tP#?q06PXsjb4X~AQ`v;2v{>M$4|_bI)0j`KuXwd5Z2qL3kjdw5>OSD;cIR@KeBMsAaCpdBWqOSY%R{UB=9tV6 z+pQmr3nQv~-`sxY`H9>`_m8`p`?T1lyB?fwlhRs$5qR)=8&AUH?eMtD%UVN#1p5r) z1WbvW$HeuSr;5jYIJF0mo;Uq6yur=uPZxF4gCVI8BYM6*YUNAemFZqKEUT2F5VK;} z5@RqkGsYq{x2*L40C`iE(mHcPc<PpC{lvNX zEw`BXfHtl4y70%rk05k!k{$c5N+sAhUR^T;$hRbMeMH){&6mnT=!~|6AF@&eC(beH1 zV9)`4HRJ8viX&IGGgo^_uj=IcKis^=12%{!FC$MF+73a-$t~!Tj%z98GO{C%5F$U; zAR-5FBDiG1pz9dMGuC9#X_T?Z*o0IO`iP%t1dMGHFjFzPPWJAr?QW6RDU5ci!&iKb zeW|z`5oKQxJBY{jJ+NDVswM_`#Gdd2w`*yB=Js>Db`bqkdUvtC(4BvR7|j)>?Nn+y2&Kl~&$wSX!qiiUms+B2_J@ zxMVCoc|yP=c1*e*Bs)i0QpX^{%^$9Nwd2kxbe?Zd2asMfRxz`b!`-Pi+ox2=LRmF! z56w61iCmiGR73?5Gx<#A_PEv>GrS{$?T3jN{e7f+MU<_L%PSLZ{{Y-)^$uoH0}XKm z7qr#}wJb|;P{fe!EG^CNhNY!|?M`>Ly`s`qv|ndkj&to-%U)RI^z z3ns#0@U6rPW+=GG%yoqy-UoMT-=nvjz8v_$qPzEJLH4t``cpHeS5H~s4uH`cNmG8-`ro-r|Ki7ydvx_qh5;DS_e`2lWo}CFK#r`vsCKY$>#Dc8cJ=1 zF_9%%P))MYkoCwUcb|>p8hl9btF%5M_QMb4r7(>;@N-J& z8lA`QACg{1>#XsA7egDTvKFv9Zt;{l(=m0;aIjEIn!^0Hu<2x!Ov!KC-VZyr`lDI* zPf2(?tbFs)ZL>+*&sAr;p`e(a-Q}~cu`jb)Y2Qj2PTZ4J!(KE-v?zi^$&&86XS#Zy zTK8+Zc`oKoQ(froO-9Jn!&b&swMBYhP$e~MrpiZvHes%FN0dQB2Y8;X9{SMwy!m_D zTi8y@_p>8+waZ7?-ox0TTLX7l3TZfT)m(h7?y3 zf`W-a`%9Yc8aERND|Hr(C8m$e9>5$jc*mh>kl?&T%ud{jc(`#iQ?kfL>pB7tW5w zVbXaF*J8WXng0N(nfCSKTWY>G?V>A~EZ8JqtFR15^4Mp2Ih|a24fGW9L&?7PcMHl- zr&oWxdGkZFoyp8QRt8emtsHhs23A;46y1Pf!3ueZ#D}co{C#1@H`>>S{e$gC{{S8D z6Z_%Ze9mTXDi(xh^uNWa%Xycr?A`Z)dv+*&Drr)m}e&0zKXB*1Xe{dxMP8 zxnl-r^a`L3LcT@HlUo=qCn^k%8wRtPkI|P+VeMn}0FEA=n-A3{#$BsK%33!}MQ1h8 z!8v0YJfF%r4;h%4+DdoC%tb3HJGncxG4!)G>>an)ssqBSh@_8~0Es9AIoOEzoumCk zb)P;VcDuxm>S@m7Pm`WoYM&kQlf7*tR-bP&S&DZ}VkX>%Ledb#Dy*`29eFWi`iYb9 zLBH4()G5ZLStv?N%jfb%J;O6}$8h?jDvF^X0$!L?$za)|wg zP!^oB|fhtg+eGK@nHMSC&b|by8#MdjF%uW-(|!_ z@7$i#cOS7;sCQQ-tS}wh@!u(Kak_uGUADbnGo@n8Pcc!HkGB(VbB4-nhaiUp^O!h@ z>p{uTu$viHWv=0dl`=Knan>sIAz?gOVW{8(zWV@=_7^Eb>WVY8J5GUFn<{k{I;Fsq9w{Dm2S{E!|D}UxWQf8 z_)uJMVK!y}hnXn~14>F5F~&JEVj@VUN;P4|EZ5Vt%C;qDzqux|t)Yblr4s4Z6_J9F zP;4?EiH=_}@f|j+fLU5hUeWb%**c`(N~+nII&y@H%v|#sQ@yK!I7v?55j%YKlLG0x zIyS8vj2Oq?xoMW^%jeJ+GMuIl*dH=zo=g}p*<}hHqj8y^l^c?&XAzwoWn7i2b!~y% zYV6v$LTR-E9Dt%sB%r1_Z?NtDqB@fekb%qOY}BoZoW@306i z;#mr@$%z((!=e@-BfvI+lG;CeOhh%0wYKY&hbxB5&`ABf*yjpBANC2fMix*;rnEl; znV-*p_tJLTsZbKUjqG@5Dr4G*rBJujs>zsVt_TT44k#tg>j~o=VhiJ80-G+mZ_#nN ziKUgckgoiIS0=Q=NXkgfyYt)tHlOwWx{|$IRF*W7?%&E~m2Ejkkp@)&vH79L&KK&-`>+!x3CEdRm?_ zk5wX@ill{zCdW2B7h$TBVx$(xY{c?{h?x9hHkklhbTRW+BU2YR^-)SvU_QP8vl%-ztgBG!wVSAR?AvKx-N@N@$<$KIQL`?58KkjR-82$0X=gtz`km1OU`PE5A+vCQ8XP!U^pWGWm_jf7d`!mBf1mmA=ZN-gfz|lp(7fjX{kTuXz=wwQ(g$#2CV)_uGBrV(D-4 zvxcui+cRNexGgCA<`E^L!WbmB0M-$?5ttf4eSd@&`;m0vb2xjbp?3!Ru$L;sW>p}w zCd|Pf89#=MLkVqI{vSMMCTE}kxmk6aht1>iOSkun`{iCbgzEJM3RVLuSP+SC%V9X? zBpko|dN$5eEV7}PyOho3T9q>&TCJ-jqfJ#B=04kPn_=ccMoQtd2?W7R%)~9Lj>cok z7mC8U)UNw%)3o|c$12#Z^9wkSRIUO>fBOnKu>F%WJI74LYsnr!GP-XTR*_^Y9!}L# zhuxzOTUr3~AR^o4VH5HB94Ijma~?p5oOri1}__K!wR#?{e)V^3~p=P7LXF5 znQ_E|wl)-mNlXa9jgoEacxc2U6F3Zd%C*Zig25qFE6H<6ngq$7<_jl^;&=xGo-rhI zD(`18I$DadSMU}w*$4jsNJA8Xu;#4PWU`YYHWF9FQT2|kkM5feVaFH zpV&7fD}qHBV^}9U&4M>rHOk~z*^^(~97Mzy$OmMyF1=~FuYNw1pRnn-D%8_$=7~a- zEo#ViD1bSI98T*c&i??G(HvG?%v`>?f7A75P2bE^Sf=y1l*9i3wg|YyZ2~6~AB=6c z$5Ag6ZZZv>H(vg6q`}3y2BU8bs&mk9fZnING{!r^l}wk{9J)9Ld+Xh zP$wfXAJ-hF1} zlTKEev9wxpu}b(fNyN|!=94AQ9y^plPV*D+0aWW;Wvf;WrZ+!IrD?FbnI(!+q#qa_ zRhC3d8Cfc1H{;Kjzx3BR7V$T zh_f8ABDCv>J1|$RU$!!GyG|Ct2}-?7Ul){w19ljV%X4xV`*x~^Un7L2kir!;z}I!h zSzNU2l9g<$B<31099tt10TIF=V~0l89W_R6TD6+P0*e>+4(Hh5XbW-ysdf+1Eh4FsRQ>MDjV7bgOsd5Fu?>-pop(X@ZZzyam%u5#G# zeOq};tFXg0RhK%;R%TEU3k|2zOBI;&J>#5?(E}YUt!Ld@*wnLs4@~5fL@Xo;wFnkv z=>)Dxvt@s7qgj;f8;p+9JM_~XIM{YG6+-Ts3|gx_Vhds>^@st5i`qm@mvNQJ3GLZUbBwDP#y*$VlUn1SF-thn~X6tZW1=47P5+)q$&`>NEXRqnxAy48K+6CXVh zDhE1+Y8bVi4zduIRtpDN4zV#3wpU;rnTSI@&JvxNWwgY|@6ZcrN8VYO0{7laA4>EI$#Xm|~!dJ`EU3gM4h^<9tmzgAeW_-j% zE6kk70vY)ZUpvP@SjS#opstLQbsdIMl})N=aQ14>%tt?5l$ps2g`6gMaWVMbXL{)1 zGMbxEVzfqwz~*(9ztp#@N7!RRr}AvbxPrZTA}Ik7AT&gK4`7P&QMB#Tp9WO1$!s=X zOsL~>v}t7%khmDp8U{+o@rOou0HEXxtP2w=omj3|tOI5QJzTIPM+bOD=Se&|=R)MAKD6&k5ZzmK?$V%dKA)UyqtFfXzMJMO1l&w%vjSJGEB1hO{;JZ zC|YC)P$V!j8_32W_p<}s4A-1U^JldE^!jvo;oREV+1jriqxHR?yvM;Y3a-^;(M3%J zWM*r_PV`8+9M(qh#7ccB_6zC_qIMOLLb zY}S%vp)%DfAXn@I4Y=+hw*LU{tO)RH%g(~+uITw8;AXh)h8IkCOSf9?^y97E%a!X6 zr+YT#*Q@H7;E1gNP)G^lAZnS2gZbY?J0nvUWHU5vBQZ6my?mVtK&Qe8W@3KXts6>+y3HN64jF?9Ynyr@K*)w99!bT6SzsTiBVT@()FJCqriYp{??oivE^U)~a~?ZZ2=$w{A!T7hMaF?P)S(LU{_J z0EnaUlIzBI)EDavp8@;D+TR;{D8Aln>-e?P9{6f1Qrirq2FVh|C6)et)s|smNI6FC zf&Gl_nYx3^9^Cy!@ZJ{qXW@@>bT^Ql(#~od+Vi-2qLH|0Xe}R)uIjUEAhxY%7m1fE zg3~ZujtY1F$)14h9~3nvqx1XDjd9%$`qECj?ayi@`4h%_M$SBMw#x$@PmswA?F&KP z&9+cNcZi81BF2xZ-_@g2XPc}1Y4Dr9ou{$no>NqxZ#5>W4Cn)Nt(S)gr-Z4znZZeL zo1wUYiETW9y6rEkzPi&NMGvMlb{EH7mZsFcL3b-9lf{;y9DM!JS(?jT{Lx5!A+;(^ z6jqJkSM1n!*t+x0+7AdjXW&ke)7ooVVefa#>glRcM#hW5X)RXj)ckN4uu6tb{zjW+ zS2OP?AQPS>p&=H;y=b_aGSrLb*sg~Z-db4XVsf??rCe3x?SIGY1xhha;Ikc&SL_~=W#WNE#B^^Jxh<+~3 zdzzaah|)RyimRDmk!PA~Ta-k$SI?XZ34+OvXA%N%F%ciW=kjm88fP1)ZRj0grLnr> z%#3cPw%qq{0Zgd;Rxv~?99`H+i00nV^;u=t5uHDC045?f>f!811U!*zdlo?$|QPBdsDWaQ258KJ4KZ3caR!B&5s+j?kg8#7o+uFMw#ktS?t>O-=gw#D#b@*s({xo zw|}kC9HWfy6F4$?x7U6)+|hV!&b92GV&8K+XH{osP*Qam9OjlUXDMa8f50}b;9|S= zW6=;t%r_a{2eQppk&u6k&uyG zPSePvtTXcm=+&+>z1#19eHB9Aj{U8@rf+wHP8V*pHKb{ubuu`9F(+DmSca`&OVk|OoNhMIz1x&`m7mRQD z*M9NIj@(;2h|KX=b*SH}htm7Q?-zTQqw?Mo_|xQ0sn;62Jg{wyUF(P2p4keFUgj|? z0vB9=={06$Bx59Tyze_r@%zYcEHeHi;Ox7{g7MR~x}_CQ0jR8GV=FxdTx{{X3ushWpX#-01MgkF7oGH%O9obBCo^MlCOf|`TH1M+9cM)`}Zay^Ic z-nYZQAKH%iWL5m`KObt!CatQuB->LNUnariE5FPbn1Wakf`_j`SZ}FzXI=I;unMKv zc4%(wU=y6tmIp4~f`5+{S51UB^Y$&0hLBGvor`FQi221&B)*TBOs<&nlUVlCQRQ_v zd^J9MQac~piumaMJ(rE>`#QEP*mv94T{^_`TF{|%&4?a+cnIlhx_#IBfp*JDFSmIP z{de<3U^M1fvlaC2V%XS$G zT~W5Yg=*=vpJUVK)BgaGjEv0eW)r^u z0FTG&dfp#!`^~5ON0!jtj_ih*!rr}U^{{A77Cklb3m|DFNF|K(n2?e0`%TW1{;dukrf6j=0y2$6&PHHnuz2Wc2<=_{nRl|tp=T?tX#42U*aTsqg(mQxm2gbACE)L&aLe?hgy&7*XBN?`il1}9op>+rF8`A z3}&3l*L`E|m7t?ptpz$_BK0(iRzGX83;D=Q6b8eR{kHE=zGZZ5lcLtMgOn@0ExSK6Nwm&?0LakMoj%>{FL!0RO*}E zmGA?wJ)Y7y3ObH68uz+=x=`tywxq}e7&4wU`1PmnE3Qf*uVW=lYK&qgRU}#kJu#KU z=5hLlH9fB4QOFTh44vDJW@5OQ%x`s$_LY>Ete}nxB7aQ$?vDX2+Wt1xH=LhM5WbFKqoq>Pks4d-w1+pCh#{{Sg}CA7WW^p}U8GU&d? z8tJmOj*FbEs+jR7HXRHEOv>PPRH|kQN_YIWjj8zW*!C+}cjs96E8-4_$$cMnrUe>X zRN#A;+_p&_q>z@B#L1+-CSKL{3Lh@b+7*N_y_a=@pHYlaehhE`igd^ zTO|p~$6_){x09&qyvRgo*{Ftn-8eL~A+vp|XRP82;$l8~)K*n%Urtw1b6jDioGfy< z>|As^yBJYwuQKuXGq1h<#qo=`J)ZHy>jBwb)M7NRX8V~qdHT4r~FJ`)ZRZhi4 zxAiQuxj>SOkdLT%1^|ziYay%LGKG%+#E42OK`epbn-rm`1JX|a|D%iJP z7m;z#H=ScR2NNbHCT4olM|1p1&uN_w?;(2~Z&i6C;-()U+SfG^U>%IrS_1l$66|KD z!|wrAfel+BwZu~~u|#r{nU~YGt4qB6tyMI=4-I=-l%6LX9V1^emC9AaUzTJoV5;9> zCfv5UJLEoPDmfw{Siy zlG|pyaa)e6*Jx%b0v^IF+T+{+7J5rYYUCi00OpO~GxVUWt@$ZOkfQl@xUkc%#idK$TP zlHd^$_U25U1TCLqS<7}F(E%efCL<+M=(NC{*p79%x{{YwsrkW~O$l0$9{nf8C;B z;w4Vw3V?#1B;ws>dDSlIsj5t&Dg1ZY3;; zF*WT;%a?4?PZ&TCZ+od#GpgKRhr)tJHib{5fO)FQuh4&nei zPx|O=g2N>1K7nGWsHG^2mftF`O`H$h;FuCF1jY|Pn359Ppevv9i=0=$0ESf1TLusQSD&#R6jL+|u z{{X%Ex^crtBPgqnoQ>fzw_P@|wSj^hKtV50+i+!Yh*5+KCSc+wCMG&e>NeL^xa>YR zF?TIx)ki9%6C&NXiHTpaY2vs5fFdyxfgm6qw+ZMEnB*C|?$c1{wyl*W_wf{kSt(BO z5}M=dnS4(;iDZ_;5%ILd^n4sx$7BaNNA0letz=kFwsgzh4WOb(z%$3-+3oFU3F|*u6FT?)0f(+) zo%iXjX5}xp6>`W|t1)Rr;zeY*auN&LS?w8N9Em`aRm^1liJ6V|nI^TJLrgAT#8*zA z$yFseizPyZ3~MGn=hrMN4SDS4$Fq4{iEXzj+yzS(U(jb)pzVT3mgOoUaRSWRt|B5; z#KnXJGaJC1PbrevLYai6s8z)84B7H1y*LlVJ(+GBpM z!NVP~$Xr!h`7G3ue&DCtm$fj;^8&S3A^ww?WRMrZ@-5Vdo{R!=cgCyPzG6^2Nb z*_jw=3<}~PpwfW>;L4*Cz7R7z@|frYa+!-L`&#eC*s_X9S}UbiAm*Hr$EMi2%RXiy zU?*C4iHYCzdqV7*LACAG$uk|D$i=$~vI@luZQF4!8^NFbkoi}$@!RkLC=rO`Jszug z>QozwT9J~$z8z)@AuU`XrqXBqko~-Xi4lG=ylwmFU%a(zFHR`rkwwc&>9VoF-)^3R zddkN&Dn$PPwPz6p5w_-k@?8NYYH}3HaQ4w@R%>NM8aMs*(^X~dg^Xj;Ff4O2*v2 zIIA>qE;e6%u}MQ}3?;s`kNBVDD;bda_?X^jueB#TfJ9a!p^T(!`)RfJPWIa)XgrKm z*d@=YGw_p`h}&_9nVx`GZMAW?u29qk+{!r>5RAZJzT}%626?oKXJ$;y?lC)jZ~K|+ zon6?BY7ZQoeww4*+L&rAeMHVNv`Kb0ah$z{Wo|iRB>j`IK>l&C#P2iCfaNELFv(+auW2&O)%P z5W*OW;-&<}L)nH>Nr5AYWKYLHw#zEn#9RJ2G1+=ohP?oJFcnz>qcgm!s6RlIPdOlT zOiZ+AZTbeArS!4ZF^-Kxt1Z$jp$)(%#ygMY3|}wDoGpxrf!pNx#BUt{(N(bvJe*mv zhYWhJy{*MWwMxpS7_s+r<@lU%4nK^j*Bfo{T~!XuWW6`gjYmwtBO<4&@U4`1S5Xjs zz^qgQJMte^+X;+s@zj|nQNiW0v0ApM$K)8FWoA`Xkc2GP#eSOu644zJ8cz-#hVvvW$`t}#p4L8%G@rdMzF47h7jO@KZuSXLpw}N#QgOPebsjz<_Lwy zB5E0(B?_)EQLqfKR!}TT*s~KIq9^sV@7B8BInswVm*?78L4bmsb?3073yVThe4Z ze%f$dzv2(-qCH}m$~@I>ivnHp0JJ0Swc?+*=u&M>K)bpDF?AP?LrLZ{mNR+W9Y_-V7iRh(?$;koO2>&Bk@qC)|fp9^#Ssg z-I%wgYw5b$rpT3gmr}&pCYX`dQkMjuS+;D4=3EyMGXoM+GYO>X+LhhZtSY_vhVA%K z$Foa^_av^Yt*w-$6^t~o_x%rTy|(aw#opNH+(wMX_8+x9#;5jZZJf4W0vSuW_7nii z;f5q&4d)^w1;7Dev`1b@{zylQ+EWji-ATF?+S@BY z&mjY*UvA;E$C9SW#1w5kb?zJLpXR2O&{j0okj?iSAEWWfVi8*roVzvXE>1Y0UbFlZ zKZrr3f(9aD1|m0|?D!ktmtZuPd9)@6&fPnvyod8+xE-^~>y103?&;-ZG*+C!dr%f> z)^p*4%$p+Gy_TM1$!!6T@cg)Fy&tJix^?TzgEu;byQQ)BI{U*PCqAyfPK*9G_DkwC zU8wSt#T{deQQ*6*I^loFX(!;2nyNA`AK^=7MC4|u0E|_QheWh*{wWW z@LOFTpVQe)aZak#HL?R=DXQvwWSfe^*&T+GVoq4oiH1M!2qWSrebL4*!>-BplHZL! zQ})z6X7T?3R|{8<&|3b*Jf@hbYw{ybBKVzzSCN4CDx7eDA(8-zXI>&Z>6Yz}YijrV zG1y#=U%36B)2hqmv_4Nbgru_rTxKh=paTTOi8P>jf3!hu9~0Npw>FYxM=Ntnv4Hrm z2D{bd-;g>>#6KVVbD7t@>xdQ}4eC1U(DjDEkzLGh@l`M`A{B|r7x~O?$cgs3^ga4g zm(x$Xz1z=YwMIX=xAGc3h7&WYZ71z(WuNz=*FK{6_Z?_8nxYOTazRe}PTg$l>5t=2 zYCBi-{{Zoi51_IX^_CAYrEz+*P--!7+HSGR+(O*hbNLZC1)B#GnUVC2Ohk`a->C9> zSGJmOEvz-|yjCwrV{0<)`|nH|Xs9+JGl-6H^T}WVytKa!z9W1a7?a*#9(zl#Eo#kG z-%jDLIj6DsOqz3+uB{(qgT%JApL|1z1PTeSLp@~#Qb7eXI}Y9^{+eDA_kX?lEeC<| z`$puomu+!6hZ%ab@e^qYueNFq#sr*9i z*KIqa$^2MAy&L78i=(0ZXz@q79yNETDLC@(CvkQ5pQ(w?U;?2OhANbUi0!=Pg|P{G`0+3G z9BvP%_tpzL+gyf&?eBW@-;cej@l&^&XHaQGSg)l;RWpNE4BK~WKPbW|Je`|Mq}vNu z^)_CqE=MOlc~z_`Pv(2z^~&kehrq*4ng_+b%w$OlGV$`s)_aVePK#wZOjpvk}ep*twrsZ$F6yj?gjj6CG>w zw;p8c?Lnw>m9>_e$M+XZaeOj!zQUk>@sQ#>Cm3{#hIE{`!({DDmLWKHqs~+P>o-X*9RgOUM0J z-{aC%arjL?+-yEp-F;b#!K%v~IQ*KL-LqKUOmvVz`!<;p+yo*oKR5hK?aqHr_M^J~ zG3NW*<#v?r0nSf1t@MtNt%}isL=aUIfqDgua&t*=dy6LWKv!H=n3fjeI@rhFxu|nk z?H{MGx2)#w*o3B;l##`OB7r4;s_Tf6J$yzlmp$6@u(r>En_PR-fx zJ;1vIiq;9QNsu_WLkcUrz1$=3q)C7z&s@aB^~lM8{W|-}t~_1t?u+sV${cTC`}N~f zGI#r3ceu*dwL?}@vqGB=7nsBQ7*{L=goJxxWYC4W7ZAWR$$vk7qh0m-sOEIvhCT!7 zJyoZ>7pRu1uE^vm9VchY@tA#rAI7}}Zla3Cx7-jiHkcTQ)1Q(Z-_~8;)VOaQ^gfL2 z7B96rPdMD}oYB3m$5{*>CjFK2esbmOYKwV1q<}?VylF~+6#}@4_DOc%fgWa3`fhm> zpYELZBejM6QkJjKm1yJNUq5z{$yT{uEReM?s7*Rltk|Z)_C%1HI>Qh zSI92>d0*mRg}vPGUXn(a)%lGV+lL*T4x_5YTCJF^PyjM@=1ykC6ZN^Uoug>@h}t^x zTlDwsA9*#`a5WFqOUV4^k<`2Ws4_ugKxa!P+*KE0NC+?4wxO7`#YX5=JbK7N1!pB3 zN+Yex`LFem@H4&LlEmNbH(@mnt?%xm$=9ov$7;t2v`6_NBE3HM$r+~Yg*AN zTvKtP_R02S=7*`PR;MMDjYf6@x;@tH{;bI5G`f?l5u0pIrp8=*UuRKSo+TD6Gx9Y$ zBtQFyc^DB~PxbZNo@4gk!k;4hMXfD)58B;*rMtx#_T63lEy4a;g#jS3X3aa^ft#M8 z5-F)>3QT!N&nTGh?)iA_p!{eGdY?mOt!q5R!}e9utJ+R^e~p^Q6YSs-8%O2G;G!e< z){8u#^6w$r?5BK3vf67?c{k;*vBl%C_3*lX-fYr2Hr`cQD7Rd9l1rfOgsdib%yO9g z_nHq(UFtS>io&~cURz(>y=(T`Rpqn2wp)M6tPbe-gGa{AecImsc2m7Nhd-e;em1^u zw_VvCBXdmu062#!jf!?Hy?<-`kVR4>s{sQb2ru^=Y>%@409hY3J6G79bJ)!1b-Mwt z`?Y&0HT17?4+^|>igl!AOgj|Tp|6m=5IaLkAZAW?-*1dc^WVx}D7)*1@%vQy6XO?i zd?N7`L6F4uhe2d9&bKEji~N!9lW0_UxjY&c29lncRya_ z^BS92Yx2nVmi4-M432kF$Tr4W+C5;K7D-XYWJN&&xtZWG8*RTJIqbz}m{;bl#s# zd%fDe-uBNh>8g3XYhaGPotaOvMhz#h^PUkK$m~*6J8!>R(Co*9IbWv!s_o_ruo_W5 zPyIrBtBtplcX{rs)uOvcq3f^;{hFCZ!Ga~JIDlsvYYov8JYoc}Wb+5A^bWt#nzJ{c z^*3#G1`l0g?2+emKA^2 zx!Oxjc(LI&h0)QIF3{s`q_N%3)Ya@L7f&$gN_NMrEFGA}NW!eRNSsdMAUgN&zj5z< zT6=e=^Bsch2CB|^W7>^*V@c=q?Y!lUbFDRYloY{M%MNB%h0({wmtHWg$UalZgr*KU z)IPadbmqFqcH6wE)IGfJ=A5*;k^H3#Sd7M>f2)@51_+si#LPoribh*I7Sj<2&v1Kq z?zRITmen5nCTe))H={Q2_dU3c3xe>dAkH57FIp6$-9lmao>M8Po&iz|;>?J>42wn}&27$RTe z)-GyYL#uUuTNiJ-Izj0~i9jxjOwzgyw@pm%4!-a36P{+V;&N0Faa&2sZq@qA=KF!c!e~}X64rT;**gw+0>HVX;c=3z6eol95 z#a&h951blDKYVnjZ1Yr-8GRig3RNnIMb0%=%P`NoW=?m3Qv-o>QO8}k=8t;2r}U}u zFSi}?ua4~(ZoIcwTg$_ZfBsLlg+5fleg zY3}!AqO#-b+5O3V6H#`xJ|pgVMfxT8ht5qw=hu%tv++Y)cc)W!>dvOeSl75~IXkb@ zzx4O8qTT$3X0+4yDM-ht<6@SE@ep4nwy&%wlDf0Ber|bx*^e9htCG$Ll~+b;h2o85*T1R)N=KfGyT}>AlOul$b2z-7!vtJ&ih8w^ByjJG5(y^bgnn*8|Bw| zvVKrphznY8v{o-xZkJP6Ji&#JNmwMrq`x9$4F3S2_}+6L3)HkfsPD%IooXe3B+$Ezw|g52csnP1?*W$Yi}|)r?1Mm9>1;$n2oy|lk|`-J zoL02FMXVM*)+dR$^eu89a6` z8=ES=h^brdjZz-TC>_m0hzKH*fc4CYNf!SAKNfXUdvfsm5Uwprrd*ifv1%+08?|YK zTNT}$g~gj}+B%`@3f|R{=12O0zd=>1S8*^8&@N1JDJQ-xD^}kt!YPce%)3U z;aEn>*^DUSsDXC!G7&$nJ62w`+r37zvl>q)yesbPI%Mh(s74(4G|HGH69dE}@faBY z09`h(S?bAHN*AcDSvNMS-IK?>M>Hl2V=TJc5LxN$nZHSb!uDh3E*ebN3?GrT|?U3X3DINOD&^Xxlb z);VfhzU0S*O|D2Jz%n8YP%{&>0e)YU#8p`|Cr(~V5r?pxVkN!sQiq-B=UP9iUPF9?P=qL&~{rk6pT2hky6#)Xm zCd5RX#`F5=wm#MAsgK8|uCl9c)8igBe$uPmQN)H31;DEyNdkabZ?ut5XF zs}oIO?=VZZA!H?s0fB-j8Dz+U_LUTKmlWI@@%i^;j^j>cpcaVjGQy9YyOo#fu-OD| zX4w*0x>!bkrA7~g%PU^oYgyPCt!U24ivuH?RLHs~_C z+Oi++g{4y~O5N>LiGF1p@3_eI_O055S=BW@r6y%2xALI3EhG|$ek?k^*oL}5$L1RZ zz8jSy<;TYe4^%S4C|3f%aoe)Sf!PSK2+00t%>+t&!hFkhJJZ` zToxbvj%4X%&aI728_(I-x}j+R%##rgBlRTa3IwCoClVheF)*UH!mV0uuVuo)a96BS z*UtqH6PT6Al)wyrfOdk??cuKEis!1)(4vGlaesd#)YAaP_rb!Q>$IrX+fj16lO zxVg|ji4x#O3P>aTW@bwl;}(}@GEc~R?pKz5v!m=UTyVhx>^iV#DP-%trqJhR_AUSGVcZj=!F@ht|t|#xBpV7xxaXpfs$4 z1fkP0JR~MyzG7x$ZMN&3eaGXn7i)hfRXAsjrZL3xze;}Qd64rMu-R-5!x0c)@z**7 zuEgr)Q;CQ+W?M4O6yk~5ganBCL5qNgFg`+e`3NDIkC>j5<{~Q4xjK&XZU?DqSq@*= zkjMkX+SiFD8}A6=FjEB~yli{?L&~_9KOL4&>Pl!~~hh8RDD zZ?^M0{PY!T56xw6{{SC{##zTo`9(_$QkOLf85RnV3c!PYDK0aUU?4oNX+X>U7nKuU3sn3V`bC zl+u})l32MgFwh<3cH7|LgZhZ;;>6CbHJFoo;i4w$HCiCr01HsVIF7?#_P`+xkKm?f zVvefI4mR`bShY_}tqRC4y6Y~zs+G}KXc=M4g`VJvo#IDff{Bpt ztwxZ!maK`BM#k(hfQYbMLM9?2I7}AN{{S0w4NOgW$P4V(t!P6Y%d9F7Y_sj+83L89 zp7o3u;7ZZYul*m*h!d!&!BwOsskN487 zgyHDrb7f+!}b4((wtE+AqGf~OF|=oDkeT-dGu4`r<`6geM5VDlhf6$WM?-45uDLl zqP1%oOe&n5Dv-cx{_D{sd{cyRkrE?#LS}IH2>qexa^B03!{#pHTex;rD z#pio_l)t6CW77hH2d_k9%@-sNc931?LrxONA`;+nynf^7dAI(X-%o7kl%3GT;c<8@ zMpISTtj$Al`M6!`S~7&SllSgJq5~mr!$IH+coM^alzho`-M&V6OgaMfoS$c|J6UKz zCQ9*^p0&FiX9dIf>#T{F0Z4I_JLF_Oe;zw_C(|>@PSSSAxE@OQ&)*EzsqA)+?=FlD z6&nTYGVXFdC!=$#S}wG{ zlRP`ahWaXat*YsEN6J4SJNJ~=jHIUB%LZ2K@z_sr6dIlUU`1Lihz?$#n1D6!@wZg> z>%+d#c2^bKZ#%nxuXXBt#nr=SX)j*iMoIcs*5HbN%v5NVyd2s9Ced!{_7CJA zjBTjgZ~p)r@2P$dR34M<;=FF|Uv3TV9A9veto?MHt_2IY`tSteV2qUT6NsJTedO|UQF+fpu|5`fk>c*P(Or?z*%xFq z)`zd2wVu6h)EP{7-hV$5Ai988Q+voUWU9S`kv?x-T1?+yDjEVk)7C1rnix^mZz(1W=|+N znyhHUu@tYI4AhbkAM7~buzB&AnJ&J^I%_WyPF|T`-Jd(elgv9E9gXvIPW?N3$KL+V zcn?F!M)Tudkg>60Pmqo6L=pZE{{SJF ziSnpTEHilFa{HN=^wz`UypOltZzFr*-JNZzbge6Nu4(Ai*;?5O*&0i(Ufb4w*hEHE z2vj5yvXYNLw^6NzRgI)Mdt+b`?(OkkS50~s5;;Q2HS8TCCT*B5d7{($c$x~op zTC(8pbN(cxLNaW*R`*q7|^oqF6_H>Jv&KCR~KK3J_|cpc;VacC-eC2F_-P@^F77J(^_LwV(k9_ z7m&tNGl7-cy|?s%1T;j>NvZ>gT#qihkLsC@>@S0!D0uDIzZ1K4g6)5EGl6b&MxM`h z)mpa?Qo2RukmQSiN%mShfO6A`Hb?_Y{{R{5Xx+B0*uN5V4Rfohmz&SUf4+*DE-1(y zs5l2$wj~qnkHAEBo;wcFAHKZteQP|A$@f1^c;Auk0(F%gIU3VYV&^7>#d@@+)mqS> z?>&SVP`!j^VkcqSsm_(r>)BeyH6@nFuBg^1-dyjc93*O6wjL{Z1Kpn0=ua9v)T^t$ zp5Aw|+m)w$fUlM;rUPH(EU>J~^#bMJ_O?CKE06INgf3vCX^yuu^n?0K_Qy%-{{T9* zXVfn_+79??Zr&K+akFwP9*5d4A*__oi7nJTscROg6V=sBYF<=`NrXUiU{8GB;8G#ZmGqF!rYpMJ=sZHS! zG@#`qk>fWP-Ok2$LqThP>gnKDW&0^#Cv}#y)_ESJ%Hd}q@uiML4)AZXBC-44B&_%dIqrlRkR(^BJY|jyXY{?H`Vh^&jx{=U`TQRMm}^`^@?EDT|Oy zE0k<YwXV|Z$ZXd|r?RWdaUmA5Ub9{@y(6f~7w_~*~ulsE;V^E-om46oOfC53s z1YWhiW>p~)90;Jg+Aq;Fy8cFYuPgNs%6V~@)Y>!8td66qfXC`QbksdNf}C7M3b@)c zF+XEg+pWtH^{J&n3!UWPj!2J@?E)i;UDo*J_}i7@qO#p9l?EyIXbHQ_sgA%dlpPmn;clCNRmaS@{iAy_0m2W`7PyNo4woc z{{X`R!)p%sSL{!B4_WGOBF8v+DmluS#kva{IRUCku-hY$Qs!Z+I5k_rIvKn}mTVbjiZ!txqYh0Y>y!Alz?uaSn5 zx7Z+h^<1wB^)Hgz7g^;s4kx-jf$y#-Qe^Gm?dCyMs%7b-_oki4Cal7jHYry~kZr#x zVNbzNUUvS!{oT@fzv`dcp7!|N+>YI7Zu$0e7lgpl)f9C)U2gVZEwdnW3W_X49)v3L z*5%Ab@nNAcv`lrl52vrz559hG*30%6%U`GOa5YX^l`55W7P9St{{SPV-(ki-ZSCge zvXcyf^DCJmQ1G4hK8{!h7=3Fz!N+(LA&zl~9c!8{YyQl2xJa2HcXLYm{ROI`ojk8^- zn3b}%fMrcM14x-Qw&rYQxiOJq`p>*S)7!L~m%EkzslQWwABXKGzRo7@pV6JN)EaS@ z^p|YYL2X7n^Xr3W+)G78W?;5d!-78<{ac?uPjwaSLPq9L{BT8&MP&tbu57VDBD6Wi3}G#C#yLmK%*^%L z{%-lz+@CwO_6ptX9wW5ETbVG&KB#x0ZY>^p?$VLF5Mo3~z=kkc1{{Y|4Q|AYrj1dXRo9JGrOrCOdq3A?J z)gC)ZM)Dw2BQqOJ{=fcxW)F;BZg*qFVbyvjt|LSFLD@X+ogFOgA>2m)04q&+h-~{Q z)=9mJsF5a{j-WuT)5DO=j7(zI4?*Ph0p#uIy-TLGbu3%zCnlO!9kb%l%Q4gY4(rQ>&D2OBvFX|BzAIQkQ;Gjx(jkoI{_P^R6H#|A= zCqe4HSE4(|*iOUtYR7Mn72NqBevH?8a#LK_Ld6$XXpmD_0g6O+fZ%cyJcuI)bDg{F z7uDy*jXmC58pFKa$A2_ls$BkjKB@B)?S5$H`3f zr4JH(=k8aBQ{5~beH+`p^LArg)QnEJ(mC5xMl+-9a#l;pM9EfxO8`9}JR|a$`8n$` z^#1_WBfVYq)V-zgLj~Qr`eFEOV-Va#YA)8`X4`bH5D}(`$yyj-2{bHoa<={NzWekC zR^Ivtd52fY>)nQq;`Ue7XG&ys9-P(57hIvKEY+})Z5Gv624k&BIHFr-PO(5pEJ%Q& zbq(CF7W}*MC&*dtex%ctdnw$Eb&PJWW^*}!MWDvor*TfLdl8r&rpzCY6OHos=PMDX**4O`@Yja}Wuq( zkcdPVE&5Np-Zl8!^&#>1P59A6xZX&IivB%!954l}?p{`7Q}>S0<93XG4?k&~=4H2( z62ae`*HnB!M;1Y^Y;V_Jx7zbFj`Ckr_%Dgn9!y{~wS6xg*1fKqS5I81vTG;TjR76i zEwJ;N@-zN>@3igKlRSy?L%Kd+_ai0cCzX25wvp|MxPeOKN9~7s8rZ~4{{TfHpIONa zM7Bw=Tk-3%{L=8}UU-Mx4Lb2o2zGAWFYgLa@Qe414 zsJXb*@HmkH>khI%$S&SDxqZZ=Sh5?$X`M3__yXM|d~H}j{0hP${EeK8RQzWTWFSH-^d`Ay&L zE!#~8O9he)>u53Mr{9&2f`N=UiH^4d67r%t3DHnEd1 zmdCjmE9S@1AIQ%%{FCnQnBDrDtLNWKFRTX}sI-IFfP=^6QvPr&RB!zzjcx~HEqThQ@ zTDVD5?Ox=U96S8ycCpLS#o(^-MpIGX>+gj zmJf0}ORxI{r|`YO)ftT;-|oY=FC(VAkK63P<#fe}-BTzV110<$xRS{0FaV8b5>i5O z5n@hKxXrB9d$?rl0i|6VR8)*3ir4OWTK@p+aFLW0K*r=FG5Q$mexa=UO|QIC@xSX2 z;`fhq`+Y}UX0ZBuyd9RSQ8R?@t}f14u??);bc|MgwAke8_8PMx2L2`S)`0wi$Z6~^ zke%n!_}?9McZs-EXFlT>p)+qQ7WAkuiq^pPC1*KYPkxQMil(|YWg4n}hm>3w3 z!g{saG<|PeXH2hTl60-S#7actoJ@W}1;ipz{(Hp1#Bctim>m8y8Vnq3bh&5jwl7($ zR+p~18kzwDV+AntBRBGio%n+OHW=#Wh{v*qI@NlbwsAR?s2=_LzK)D1Waua(JrfJCQDm%=pix>u19|zW%j|~ zK>`O{rfad%B$yU_KmhJLOqTOhn|3DdTLiY`bEoYFDOH9N#{r7i*+CIRknq@l+(3@e z5w_qdXujT8DV(EUBbsAt)g;D5CKQ{8C{nj#)dWB%7H64m&Mf0I#v(eY?8~tQiTmo= za&nB;x}ZT)ZHz<|St5}#s|O5`kRD`YaWgRy5k^G?PGRb@+@_Pq+lqB9WYqPNSu+Mp zXXPX#CkYi0`~>@U+m!F|QCkOszk$b&GKZ$XjqH2WN_ny(2HB9#>Uw3H!1CYxcmDu= z08N^;^7N-THj#unz!ia{n_Tfu4ie9{s7zZGCC}q;#ztf2cbI{xR2a?M_2lw4EFqyY z?X@+?5^}}^SPPaBoRwn)hBpc8K_x+ir_K%?Q8&%CCCP|jfR8^`HTda?Ip{2 znfxPZOCN}={GDZE!x~mBv8`&>o+?ubg1KupDJAT=f-nh)g4_K}bOjahR0_>(P_FT2 zM(k7j2??~-t_)RaW<>sWQ!6EeaxYnNA=~15gzGEq>Lr&)H<2MAu~N->uJ^tmR-_`B ziyCN+-Y3FmDsiEJ`O5&r;e!sqVx!N44HkMo%w zhwq{f5Rq-1a3Th|HoNW>pTFY^1qy;#Ek{35`v|61b2A0M&p@inY$cE~;jy3i~+>BtokJa43Tx$z=n=LN?wfds?Du zYbeX!vl^kGwGF$`_W@;z4pHcfKJbQuec-u*{D*iy_@02cav0qVGgBjw?beyqcW!Pw8Td`sk9ijq4HjTRHKv=ad&9cT*RjRL`yFq2uXyUEWe+Y?-YyLrFC|EJD6EHCnQgai$ zZP6J>ER`41y^G0v24%M7@5+~nf|4(YKU};976ZQfavAB<+UqK-OmH?aY*8+?y0!>Q z_@VX-9B-}*YFcnd7CuYhTpL74?>4>Aq&T`XZ0B*A`(z?)wsw_fEfj#V7H?usSkr;1Xc+H4sBr(#8oH(i7A<{DG$T8ja<%ZlwG=&O)#?4 zHSrntjFQ-rQkIp0j3^*RcMYTCqcV%RQH*70w&vkjTa;>?B%RENWMP{~&+Lt;fn;)# z-}9L01r{yf={CNoQ=WQ#b!Al_S#R}17|4&h18DldT`MEmbG(d?&f7#4l&aU@?qlrP z%W51|_;YPU33J@(~4P-YqE9+BdKxO2ANQ6!zJtra!pD>CYs6FmKZYGgJDU4%zmS7 z!0OEIl}@LtZ!edZ;`JF@DvU}BVKvmLh5Hg>N^2mIDw31k5-<@FxEEMi+}_G%@Y&op zv+c&A$!R@zfOJ{K+0$8t6|6$Vhr(;xTSV4!J3E&ACR-yBxX)V`p-^pMvLjPb)XRV^ z*00-(r3{77Ax8zFa}4z-j7r(O%#h6g0IrKIB8kD*TvZXLD5z{jFq;QE+EXphGYcdJ_^eJ06G0n z`0qP6?yrnpqVi8dVf9`j_MO!D3-ti;daF*}uE@Tx%Zfmq1@{6|Cj^#kaTCAlW3NB` z*X(61qOz3TR>oBofWfis0W*AXh4NfR)3nLSpX{PwW@n6b(Y=oEXOi9mYjewXBT>#{ zw@t}&&1o!>8tfiju@Z1cUT69wq)mR+#C!n!{B?xXc?Q9IjoRM!c1>9wCxhIjO&O5G>P&!Z%!2dSn~zg+GMiFGk^qQj z$(Q6~u*`2W{>DGoTT|>uoBlfak%#_29Jh9#ypE=GkgoPTXQFj^X&>V;^HKp4Yb>mBUHBWr(tI2(iEIR9|8wmQltxT>k*XM0|1EYFid| zq@S^^Otpq=#QT-wZ_=B?j~+bU@-wp4{D$&-x=BOy9+fCkak^TAP`6X6l{82Z0FzOh z6#_&|nEwFAI_iHL{{ZwUA{8!L_PQR-MZoAYPOGn*V=sx6WFd56>ghF^F@ZvU~ ztO;x8wa9vbK^yw^i`TF{w*LU*&EU6j^uK>FUDWb_&W`hAUaX#y#$@!n=Dr&uke&?t zNvBr3Eeyn;z2<(hg^+ARzh5oB>H4#+C3go_PEQ!&f7N?q%VplvP_l)e!)xm{EA=(b#Jh6N$y(zWOXL3x)>CsV#Yq($=x;bL2OZHfA#o4c;%}6vedng$Z8)iyN8E7KWy~&12bz0 zO9zTjkE0`c!iXVLCp(e9mdfC@M18|~VDFGP-E;Kf*PbNy>q_^_wz`M8di3%XdxxC7 zR?A2;JxBPot|;YlaM@LY+?>dQ6^y%xz{G9V$$RtUho3!`)19xpdOfZVo|t z-2VV%Eec<%>;i--BPnT5T;lPfTqWL+*oGr{nA#&@{%LA&r~Zua>lxh-F*{q?Uh?QW zdSI|RcShrF#$O9@a@j9V8B-FZykpfYyr35R3`y*@#Cqx0qjmLL&N7PhM=3^hp4PBp zw|n#5#maW0w7ye!-$&}v?X^l`8j6o^sMXZDeP>}7+O$>2SEk70Rnrh`E#eP(XTx22_IZcPSYJl z=Z}|t)$?myBe~t^v5~Q+ai#&IG+sIzZyBe9gu|a*V6!`y-z4aZ6A;*d7~UX9Z$#kQ zNgPwdM`PLW{-4yt%I@)bD~jx1it)$6y*FmD-Nb3Sg_pIHvsZ&Fv`~VT*UVW{)BS3Q z5KPxE^I^>TckQ=%>1$0RjlJ8Pu0J#w%E3|kOH!1W=_=LcA|oSc;#ixKCp)Wo8G;St z_tusEn%-mfN5bCSYEH-Z`qz8>FYX?o)|o9)qSdanCPN>jvz66eve8XfTrv!@!4WxT z!!gKqoyTssF{trz-5%d#^j?4ts`8zr+Eq%$+wc*f1%l^&Td&)I(y9DtiFis#=bt&`I+?o?5~#kj;1>u zl*Z;r5Sc|QK`m0Oih^R`wHF_6xKg9#`(RsqL4M`fs>2 zb2{-Fr%YO={I(XY_e+BnX8f64zfm!~Cd-ykyC-Z!q6cW#6^lKR)5+9_+xtD0!|P$B z`z*udwG*xCNLUGx&mf4bla;K7Mj{y-!T$i|9e$dm-o)qlkCE1=7{8;jbm@I%jnx|I zNzk39(cSd#9!oB)ap%{HuFSSuGiJh-xTb7KS#cZ0*zJpXou+0(uU9?@cC+dc^wGq3 zvtQ@@Tg3Stqjd>m3qRZb-)lT`GMG%l$WCsuq^Lw4D{_RA1v^5-xBQ}dQQXZ9^wRBT zjNeR7;`uk&>G4ln1w2ly?T>YPQ;KD({j{(%R?63y%X68!wi}78i%VjpjNoIONp-Vs z^7%UN4SaCz&wc!l?sF$EI-!}0Z0%JHbwzjPELAh8R+hk$Lj7e}p3vB$0AxyIXpN3X zUH59vAH=;|to;4%r;}az?f(Fd`p%V@)%}O=7929J)VUd|0Ge3PGcl^T85PU`$nhB& zou}i;*OcB%>OUxZ#c#U0pGxQs*y!4{=NG0l=9tAQm0Tti2a;+wQn7~G-OPzS{sVyg zKty#1l|EH>m(C9L=6l`VYx4Wmt+O46rKjth*3%WGto2i%OrKel%M6)d$Jxk=d2J)s zHCD-_{>7fElEj>_MiF3#)>ed|Rxl)>-`yc45fd{7{(xByh|GyYOhx5rmzneC>w*Urjf^jB}0oXw)GaJOo%>QE(wqGY>rfCD)Z zfr8v;)?XjKj>7i8SnAy=8r^p^)m<}>QyS^Dm<2`^>*cx}qfnbn;KZOJCMIS+f8!%{ zK4(T%(RA?`jUSBE9y;D;IgGoctTMKCwsKG__u2_DlCdWN0TTqCA;uyI&u}kX27=PK zOddZMOGfCr)t6lti!*k88N5Y^^cF)SYbh=U0|o_PHPIfnti*nID2!XyUFg*MpCNA< zs5KTm^qIJFb-#$ITqh#H5`? zmrEWa>vWl#Qn?WriGlR*w-(RF#q#wUS1)Akgc?5A*X(3Z zBPf`jBM}1;96AHLJYn)9yZ$5V9@zf?`ah_39v`xLdd1the$_hbs%gDYqFk;~cBr^P z#C)1WMg%9sB6s7~{X0$OH;W!)X-XZ!(Kfq7;m)A#jui^*-)MWAk)uaPVsm9^rTVJA zCmuYq7DL;@k{J?0^*F#nTP%+`T8Ba0(>YAe8ir>Zn8gBO%v7sd!cK%pkJ~ozY$mlF zLxHVo*qD{b?-LU< zJpt=Sy`C74b8$J{J@ppv{&VV8tF$Rq);beFX}oqnM{25gD@2r7%;hUVEZ9nH+SxE( zp;8n0^D>UKVdX#57hZNOyQSpUh}yRy=XZ~EG7iE}%;A7+UC=KA-PJEu36rzv_6I8q z+judF5s}-h9{gJImqB?GuKVHNO#sJgom-=OM}E1ZSLUrg8Fjjo zGAlZWtdjePDzI?VfLzG@k3e}$##y(E&S5cHHz{{fciz*A%;zcEl-Sd=5;AjIz!1Kt z9$dfCZzc2kh|KR8{5=@TxP3mL@pOt zK}wHoDgd0;mpPr|tUm3|sq()WOTw`E*S#K+{*AjMv$?(w(fkY+@qC z7~U0z1i`{RQOfo=Z(GG!ms|Q@#hh#ry%?`oM1cDwPfwFlVaGz+J+{`02vgI zvLFYJ5L^0O{{Zp*)V=$}X-V$qmtC&zwY^G=Gy4f{$EAgLm@;uY-KNHhJ_T}Wx$7|v zgtm^mcd(u_V}70mqJE@)S@vSiu<~J{)u6C~>WtZ_;wz35^^1hEq{jGp71Lw>+ z9>ygxBi1W+`?Q_Z`rQ3MyOpnpxY{GPb#l~aRON1piS{&>3Y4auA)x`mcAdnP5>q>a zIZr5=->AF2B9&RXu1#mY4|S#edNJDudPML33$y)|?VoNti{vJl$9T)(k8AQ+eM5lJ zbfp#UYRu&$hGOa}m2)JjqFilkrX~%qpJ9v$f%WCLzrEq}C+lP6@#TAE*({yN!{eDt zPhjg*gdkW}3nBQgo8$@jr*K>VHJYnwU6UIKk`B{te<52nY;+CJi zV?}A)j-tnwFC(I@d)$d^1ByxKHfhabVF$m~06e8)y@~QuxP9UAQ|LkBhP?0+)$Bei zxY?UlJJZ~aFIvpyv5k_IoW+Eny1a~s+iwyx3rNI3#@$2THKtIEEaIN$-d&S^S=QPz zx~!DsX7qR)-JKufj*INidp?gHyVhDi4B)oyVcMSTEW3&p&0=;hguc8L*H0gs&1`|h z2<<P-Un8k_unt<&WjSD#kV4X!_K zTkit}44J?$rPq_bb!i_jyE&lzJ?>|Cb@nGy>nu&JS)~<~Jf#e-ENz5Zv3S@=_IoKz zB7Y%qK}gTQ#{QOmvc5rg6T&|4rBZq&Il` zy`tiV(L^=}c!`LgnEgMfZe83cTqw09`(-rOs_lYPRMBbMtf!ZQM>=~sjm%ocuNzHy zRo=&jO0+?|YuFZ3Nyo+vfEyp&{nBK_M#aJYTJYOKRW$jR^ zuoHv?7ZWq=mGYS~jrNF$$9+F{DUxmX0O%H^}ZvwzD#JY>2_yP-9TPW8r}tUzml&56X~Z3n3u4*GkvBu zB^LY4Pgl1nq>^3==5rLt?$@U4Id|mfe+CuO%0^KOuj~}?CW9)iMIOC^0178~BW~Zh z-K`YS&ZeS1%7mD+WYvLXh1WzXedbdt_Cb#n3_2%&jvx4R9x>NOYvn6t0?m6C%vRIL ztzhJvSGd232?M1t&DcNW&xx6lo{ME$bnnc+3~j~w)b2R~TUfg5i3pNVjLY0+D`O5% zb{jbqNkq)x)Q3)ED(Wi~kt$W|Mj;_mx+Gg;b|yE5@QH=Y!+@4iGFu+|!EMy0yK`Xi z*_&A>Vyf6{fQRl7nQ2TJ1R^WIt#c3}L^Jtun4i~Q8k`)CKE6Jct5_epmm+ex6unGR zECqjbp5{>IelFT)lu5@)@f7P-qLtOTcA=uE)T+W^y_N-SXEJG~MaBL_6Uerhh>n0~ zVL054yS0jf=O=MxoM5LCRwe}{AB{p7l@Kl(w!pk*CSdlDj*;bZ40^0iTK0X`f+r!2 zviS!Ig4nnw6hJS5yz7y{aFA4_2xpW|*JbQNLM(9pCY~XI7Hwr6hi{{GaW%q$Wd^<@ z4;ftl0GF_L-+qa`TFokhXDLMK;T&sC2tGHxrc6rbl>Y$yHu^pxOo=7_Q@+KwKwD3v zcq&$5zdKynOsmziSR61g1p@ZP@5h`F&;I~y#`3~+?>o;`i*aTS$BIFQ50@Cj?ElZPNL6zlbZ>wmRnxtoGiJg!4 z^V1{4=j~1%rG&)|V=y|k#f1@3Cb>8`m@U)8efN#0efj}M+1z73!MKcCa@<_kSoAZC zp4%cawN|jL zTMB*(C#RHX(9P2nQonO5x0I#PiS)?DIB=n>fMBp}gqvVw}#ejFZF;s42UaxH$HU8Q? zEorP+-bAKLK*~l4B|Jt4*Z$^uyK)zD4$R9+&2}P;u#{Go+;y~s89*VD;FVWIOiVy= zmKM@dQw0$PqOmHIiu}>QV6v21Mn=2?APP+jU3IzxoTdN^dEzijWJevoI$wVDQ>S{J zikeR$r|eYSa>(@6u*I%8rI6~|M9WUJC>AhVn?Eg&pgQY0kJ9;Z!#@2jABVOS;YTm3 z=o?!w<`}7OtCHd-0tg3&>zIl7n2vSan0+;F&1P4}BQ21rRg+v96lOovAIBaW$M$a% zF*C+v=lw@I1FJFAYh&Ky#KtCt$FJ4tve{jlbfDN#3o&4UnD5Vo#K-3!@$pSrOs^!7 z$S)U-4@G7@HrCjLeY0oPe31?Q;$)&hkiTN!ra8n!L^j4_*H)@sq+-gb6UQA=)o1{v zNvs&*Xc+_T2###rcK-mvC;jvb2DUkv!^pQLwO6TWB{;Op8Ipyv6xXQaR$^ragWHV5 z221jvn1DWDzSA1IMUh^X(Pe#7yt=H{KX$^-2H#_h_h2z=fRO=`Sxjva`=MCr%-YCR zW3S>Ty~L18ARyso0#a+< zW(W^`_xA7mnVBq842D}L{Cl6B#Gve%jf-G1z=N1*tZJHxL>|5&?BpCEXa4{x=mwv4 zV;f@}7)!^FkF^0s++twC>e@L~8wpvg9xy8@11LBWT+hUhOJx^tVro;UL1Ng}u`Z`> zpr|WBIvT@cVUWUj$f>2kBw6hrnV6W0s@Qfps>?=Ftqn}20|5fsrUONcfSUfV$#O<5 z35Jj5xF7!jj{QrFE&l-N>e*`9#?w-2yPb>T~O!mEetqg&&d3NG5Qi=;Zc7$3(m1nd-iti5oJD8mCF_?|we{($ntd8n-3WD$B z9f^Jq6{($Cs*LhhT~2Gtpht@$QaUb{gs}$K@QRI=|f%%}iMf)Iix_2!`?#J3x#? zbC~}CbI~AgKV4}a%6UQC94@}o7qr%}(-|~N@H&$STiLGBkA=F(PxO}_6DT zwWk?wK(Axs37E|MOm1NM{{RaG?Z!pCdBn+l{<_|ujXzMH!g*Cfk8bte0*7aN?E1vI zcnrg(HL}i+f{J-U{82qJ3pPss0226_0iX39D*DoWK)m3y=Wl*{8{A&x_kYL!+i47= zaoP__>FmYa1zS{i*fK@-LZuqk3O*)HR0z*b5I zAV-`V7R581EM{K4y_;=6BQw=+sWhthH6vO$l8W==+@H`jUz5z|9HTEk`0aPE%ReQ& zaq=q{=AKJb_ix4I%}h>^)!P040CG93b4Bx(qT7y+SsK-q_T>(2NUY6*Gx%*9%<*Ty zUn)GqU0Lz}0JnMU{{Uz4t;pl>8m~}f+l|5F@yBhk{{Zdw4hgbS5(ZGQ*x=%Rck6FH zBl$Drf7Tzo7Cd~`+GDop+cY?z0s06Ttp6z^|q7xK2Yy9eKFq;tNy?4Czfk$ z;*V3C8-9u{GGYp--t;~KSvw4e+>B2cKCf8Ib6OzmM6!|0|9y`O$~xsQ)I!!WiBQ+ z72c5CLSi^2V7U#5&w1HXl#`6oe}C$E9Od-Ro9FAQZlzVc>e;%92^!H}%(IY$ZeEdzSU9Cm5omxkX}A0V~wYdo{; ze|r0&+56ZWQ}J{=g{!PD(cC;*C5KZTmV$|~n6553Qr%(>7Cy)rh>71^9p>-Ge_MCY zxVq1}x|heE;`bLZs4^7tI`$7 zFI0Q$>Q(ATWS<91WAuiIr-H@dGzPU4mR_`@FVIW;}UfL0F#~z_{lx{?S7h04_GHBpp}0mX8~PN%OQxrSs!!3townKsho+D z*(lkO6Xk-&snc4j<8j&A_=Cuh#082qRgbc%jiN#s06xL|W8!D^5g(|IvV+6!@9U2k zyphoz=umR zUD5JGT(ih7`D;wiclFjyCe&MFMRh`~85C2cN7hXW=q611B%^tq;y+W>?b?h-j@B`m zi+$y}o?XS+l;YER)xFtBk^RR=YlJpz6MaH4Zb}sZTz2I>0gu-GN`6KwgHxzXDw!s^ znX<~k69&2WG7e-RV1C%1GrUB6g!Fb6&{otq%>5+e+?C|icCAYmURY$0D9< z7gH@$9g9LN8C$n`5H}-fOfc6RnIH{_DEW!~d`B3bfXxkc++=B<<7Cy9*4a%?Kgqq7 za@uyuD*gl62E^B8Q(%rjW0%MjPXQ>}f7EnUZDX&yr;)LzuP;jyaiRQ+>FNADRGQ&C z`&bCCFl@5PS`C9GJ4h_2^%K&qNtnp$&0C+;nuKurXD(i*sL)UqO`rs&EioY_U^WqH z$rjTM5!>+Vv2@n9{{W%S0L)@24fl#1TXTUZ8RG#l_;dn* z&uRQFHlblg?uc28(`?eWYNeuh2fAw-M52AACoVQ``Fr-6nThMGbq1@->I%v&MvTpy zkxh=PR25;6p=>X4I3tiKk=kUZ=6BkET^Xx&7NFHxq-AwwgQsYbEUk`1mW*Z{kP!_~ zizL9efhqlR*#7|c)sITPJQr%Ro}5lNm^D!|TWMgfHIs*2_ z==|nrYgNNzk(6T=^wTPDQWro(Mq4hEF&X0%UIB13v>P^`%44qZO0(E3Y}T)hR*%h0 zhEC;&9a`mzwmZ$7imk2)?N@BJMT&DcL=15a`AqedTDbiio71_?F`dI26}`NDi!~t+ zL$;A9_><&Y#K?~E$CmLCzVh!6k;kqoWRZc=`hrnp^42hFN7y>)Zzn8sY!O9<#Hh*! z20}O7eEXp|R`*lBUU749Dn+tT6^4)Flwbr+DEL69O_@&8La6 zbW?WC%b7;u?b{w=U}zB%EzOhoku%w_42*0)hWl^4&uRB==8M0nE??7_{Y|HV4whus zmwT5q8pfCg#T;b05EKvx;l>7k-1XC!H2U9lc5#@fc>ze*$Z>Azj%)Tvn`UIT39wMX z*p&mg%x};PZsU_^{{Xigjm~|s@hV`1fMdjF2nMh_p0LPiy#tfKjLlq@87~t2vOlL9oBbV;aZhJ?O)wzJv^)KoGYVq@J!Z1{lYEikq!xCILwkRTJu;e&IWS@ET z=bkVW4QLq9?gw#nF1=0sw{zD@-O0pbG|Q2R=bN5} zM6E#*2UmSbbdHSe7j)=$>$V-1*PXuc{{UO#XEM!N?9cM~-E}oDGMcO|!-gePkk88A zIa>o}6xoU9J7I<$7>iac>blyEIjkEDNl&QrZF|gc`=UvcyeeXMVFMygIYdnHfQZ@` zk+{8WqU!jA*srTkcy+&u-TKF9`#H|ocLta6+07e@v3oDItg?KBMn8ghp2#b4KP<6ma@7);7$P(oqRSOVwG@<*H%>lm7r6q{@29 zKc}B%d>`{a1N8>co!-}Yy*uSjgkT;QQDdw)V?}BI0JK?ExsIeI2y6wfXd0p*OOVFV zGxO0O{A;^A*iR?)51DMcTw{nSrp9P&|W9Mnv zyx{lv09qaV1>@Si308;OxqWoFb-b82g*Zn!JGZl13x5sB^DML~%XI~_x->$O; zPol+>;p{2lAQC%FM_sqr-t2dWz<)M80qn1wRJ(2CJn5Y?sjxNk+7AO=VcZ-e%^7QPak(76oYPRHSFd9|6xFqQu_sZjCZ<95ohJn)u=UoOn_Ot08oW)@zfum~cke~` z%UfG%bmaBcgVbHRx}7!L?NDif{{XYo{{V2C*zApl^8;;TAvghJR9l&rmb_8_0OV2R zcVM;tgwlRXUDN&B@!J@L;xf%$`u6p9BA=~eUAAm0Al}gdQ8SrIn;_(i<|nJS>V=!` zN1i@R_B&Vj%jI`(eB{vj)y$!yJ5xCl|Us#zp@C$OF5YA5G-Z*&hUaR_wmB$$?nQW$C?+OI;$k87g$^RU(Kf zJ<07TncN6w1CeQ8x?SAWec{&n(fVqUrxUs`)}dDdbQ_tJTKB7QF)7{^;59g>(r^c1U_eaE>bF|-a2esD%+cVi|uaK7H zD&lHch&BKrYIN7KT%e4_akmKSJLD+Prz(`And5`AtvR%+b}`?zKTh7%c(0(eN0Yx% z&p8?4HlqZmJ*PCbmbo=`UJxW_)hA>|EF#P-rZ_Gp26;~Vkw;qy#`boHmEDcWWc)Yt z`w{h4?I4n^G<_J+$mrgz~eqoy^G6>}GpXJ4og?Qp*>%-a{oqWJ5#h2jE8%So2=Rs^PS*}ac zYvidd$Vk{TGaJV#%Ff638@hi~FQ_ki`{US56@J}zy3cSf>$}>=QBqs6T5B0LA)C2O zGFi63B3Z_EjszRTK=0OFd7s>m1ANQ#BMaK@$Lh}>d`r_>TODb7)4f@+j71%7d^o~W zQy7p6%z<&sBGHr-^5g#i@W+3nSXF-l5~#Mz%JH9ORBFOInu)7rA(b-inNymiZVmlhn7{{Xao$O8y`%3Q%44=9k75G6|>--_k{q9c@b zA-XMab5ihI_XdnuU8`Y0Eu6a4CnW?Eki85cK-Ym;?-4d4dCt+Ymm5c}IyT-i&}Ao4 zS*P}mvR`pv_=mDA5lnjm)K}zgl7cxIFVRH*0Od0z1MSchUBy+@ly5q}+0C@USW65@ zWWcQ-vcbS_#$C=iapSk)yp4DGYBs23veRJkRBc2y+-cUSb=MGLuu%~K!eq*0oDb}0 z=i}o*E`5r1Xpip?ig1ZL#YnuN6uy`?YBkNg=f&4^4{{Zi4j;o_b3c_pg7_4{l z7xD#NSt3M&?w-XgG6lrM@c|fuk=uBkzu!;kV;<_Z=L|Uex6q_lTp@~o;Br&$B*{s$ zX(Rstx#i9$@&5qAI{1t|6AXebrkt;m@wJAH=_{*ndl@T#u_i=6oSAOV>;^zgY!T1^ z1k|?~`;_is@02PlO+?wW>MRBbgsUSKfK*7Bm^nlN2H*wrKcv|DH3ir)w~x7$surzF zL+vhBR#E(6a&Yj87MVcC(tr5vAFiggh{DyJb~4^3{lZ;Eoy>I=v4egMp~XuTHGgao zuyG)W;U3tHx1O7n*~{dzc}!9)=e268%O6FQh(QH5tXEP}p=_DJgg1*zc^L)FeutnT z$;D;5+V3w9lCracSUqxoe{G~3k|wl&m;!^D7x_etO#E#hGI;8=n5?yIUE3JDga|9S z7Dyt48E!u{l<<#o;cyc>d?|q&?K^bm4MAvIZfQ4W@mNqW8a3&Q5Nxhq!gRbzezSlQ z2{fEckx3uSpGzPyKX0cQg{6q7!>2T^+^Oh}v=eR7lX@*ZEwBiY^|*xMTG(lGm5gwhf`-7z>^~-rv}1EuYgrtX7B11;unV^e zyKCVTDBh>YLggCfUvSoGN}NY948v-Xu%+Chc|b%)(J{9`L-vS70*0+= z#y|vP%{p?^0P8@+2HWw7+i4krjysIc`<}Veyk+Z@XjdE%WGuruGUbaEUedE`l);<~ z@g+yZ53IMCk(u9Oh>mmzS;{g^vy#kKPx7j=ixA1r)%&(q{$vbhXqaF*KJ3d*?z*J)1G#Z8Um&)**5Ap;IEw(wIT zt=Wu)``2t|v5U2_4Ty_%0INV$zp2JLpnx(y{QaabFeLq$&SewBgbc>>_R4FfY+Szl zYw8lt6@6DhV3~vBYxce;+NFC6PPxR$`+(Q6OwUabcHL|BKH6ODEnjEYsI8S-HPEpU zHdPlkETrN9b|@{}xhdXYraAx?-l>>|*|f>jw3?O?qQ!y+sW1RFi+Fdk7w9F=B}@zX zloWgyUoTBY(Onz4Csfmi6K(a(Vw_6VxX47*E^-qD5eSLqdu&^b4^*Qwi_>jIY@TNI zHq>J>MJO4H>X6_c*Cs~AvQLrn34w_?+Be>1o8;YQi|u1?P)&na0F*)KO(Tv zD3{L`PQ^Qn98dmT0G24Cjvp9{C=N9B;vtU-=kh@t0V!OT`zY*P_11IkPD`Iw1a{8XIBc?mh?w3dsm^HQ#*MoR)0Wp3iO++mxdkd%3*UPwkqDilHi_l< z@4w7+0Fv5Np)kgMwE#`F-E>N3y$FU0JdY1h+1-nAt78y|hX{_-$Ts+h-=G1Qs*mA~ zH!EW>^ehya=7sGXQ1Xd$0#X7FIEzXiCG#P?PsDA|HuTjy>9}<=nOcQzs@rjxl9*C` zV4`b%9HdGcBD0A90LB}(-5ZXm!LByJH#F^lvNV;CqE49}5Fo=YKt9Qxlyk_o+D~tp zo|}`V`zz3Xpu1C3jekNARpAH}@?!U3RcGsXQ98zoJa#YXX2sKl#wS3D2O1stK&R-kb z?;^3fi^z`B(*qUfYeSDw=|u0w0oDozZ`FYzm=^Lcf*tk{h}||o{C4vrJ+s#d* z`-`C83K+=af+$HqphY$~gAVhMQ{ zIfgoY{6-id3-y!^`^4;D@rhM*{{VRWc-L8e;P~;X^SB*m$Qw*$Rs@czqXazDW~7v@ z=NwF?XJU?XG5g=FVH+&I4?~@X@28gk089M0b+spAbjNNrew^18>SLc{H;!wWii(qB zQu_Y40kQ^c6zoGI1v5Xnp1U{cgPgD5ZySE3z7tUIMjy0ZPxo0L&S||%V#s6u`qKts zhy#d&0+x&8*Syab%>45C!LJg&sGX?pZTx>|{Di<)?H6{O=1@xSu>Syn%S#F;jTSka zYrkeMl1?BunA(3Sh|G@ocpauSy$RR)2KGxw>dbk=hAAMI37Pqa`q0Ug%*P-5n#4b^ z)J#mzmitfnntuy+4X9O>396Cj2B@^h{{S8T0IRlNS5IYcc*TIiQ%KD#8p9*0sKx|p z@|Zg?BG_|ChCllF$o?Zgxt_gKC&ygxXS`(bW9YxzO=CmIp6h2R(W~5@MRu&+si*XK zUuCE!ZDFdw&@wB~O`c|lB!Lqv?qMt%k-uSQ+#@(agL0=}1h!ScJre%Ii1b`vwFD&{oG ze~9CMQDmROy-S&AK2F7_fC|lywI6w~h{%Z?PUA8$zJokP z?ibP*$HU9_uelN3-yl2Fjnug;Rwqnd!30sYj4MzTavx$l*MI%Ef%}=eJ)YQ@{ZCdu zceTHaKTFQi_NU0dIQv_f^N;Eq-dc>+7`z6iZ{1@t$}>q5FEO6kV2qh?Y>7m#g5n|< z9Zli))qCmh=ihL9f8t-4Ur&C`_D8sy!Lf_N>ODABryr5awc83|)vG4mCKwoO!Xh4H zJI?bxd36q^QAtLVlp^1^-wVmD4GOHODR}igBzYC(){F7y$BVvcr(J6t-Xk}qsbhOr zpjSw7&2~o8(br7 z(HC`}kNM42s8LiqXN$IGyH>7Uum>doY6=r-cWMwz6bwt51*8u=dxPb^wDQxq#pVsF z`p>&Ktb#Pr=;5pPk*5$sED9L9h?Ya^8mv~a?NCeK4+b~(Jy`56<8w+is4PX173n%M z*(Z}4t#?l+m)3bs-FIINp>5Nw(ri|vs6#Cn(Ld~6c3nY}1-_|T{n0%AfX(M^y4{b` z%gQeWJZ{7Hd$gVM?PZ?J>n`Eav4<}1w%v@iXX7m{6d3CxY6$V@f?<(Lb_Ps|6z?3O z9~gd)8m|T04A*%4jH9H!pT0Iy^jOMMuY!vjhd*58V3gX#O*(+@V1TZDZd~qOk>>V>MH(JYMg?nN9pV(=iF|k#;??NU6)gse4VBx7zp zdy`nllY(APJYD1LTl;;kyr;|f>30FH{4ecYyjGsh_YwK@7MRo*;VNX}>D+raw`?ZL ziV}_5KQqsX4&57y&Qa8r^A|%Ij>aysmloG)AvIP_DdS>gjh4ZFi`TsJf(f7BS%KXT z6#MDh-7DCS6+EczpMEP~AFp%EWh*>E>vN$LQgp9`+ z+oL;}oi*Ja+xeUI3houH8RHj?I%`=EXZtDHy&sOP{O(I7Q7D?#3{|i&6;}0o7Hqeh zT;{SQqE;OYmc^#&>esCTBOJB?Ap&n^(z%jcak9cA2FoP(gvR0{A@S7p8AZ-pZv~5Y zZ9XNhxm}UMeNhL`)DmqOG35s{#%4C#ao?&nrj_pxj=HZwX&(M*JYI#(WVGHdR$y^& zyqDd=V(-B=t6f;w;jHm$nX)bJ$Re5iMpxBe%GbzNX;c-elo=x_aZIr-bK4Nf6XHKwLLGn0jildvJAOdd^`{Ru?>0KTn+T#EupDpQriSO?gwk*vd1 z?y-j$AX*X-sUSERiRCl?_UXuGtsXA3u2rJOopP4hpjG3QW8OfNz01`+Vs{<)i8B8H zpO6`48=K4`&3vv3lCZ0Fd1gihiaN3EV&v*ATbT@$5EO($%s_r8=XH4uUMi7AwM}%; zO3U40myOSwLPD~XEs(Fba~J>+BqSgra})WLbsSv2M}n`6b1Pi9(6L>05H=^%5yWb} zWU>K+h#8Xq0Ky_BI=T-Zhqe~(BK}T=fd-P;-cG3H2$&pKDOj*u4KN*wV?V2~Ut#x;W5pS%PTRym35psbvzZ}K#7C?8qvLs=t=(aPsiv9(i?%9Rs!Gb( z^uSV>-}>9?nFD76f|DJhVVh6PPWSG2ESyc-l<`>~}@i)Huyo3?`~X8&*dcx4Lh*pHKe)E;NsBe9Y}9bM$qN z<#?stuH56n`h!a58pn+|h5-$~XDV<{uBSKzQ^sV#xQ_RJlU^cr15M;HzEAfbzdg3| zlgr$oVQMnv{LtlHr9ny+S4uJ$LVcO^&1n9Xv1L{S)UaGkn=oU+j0TDNC3!`oJ16BY zZaWmr{md;qhCf`ZR9m#(++rGsebzk~=}DWdv5Vn05Xggu4)H&iEv{X zVf!E5?$!A1q`&DcL6NVC$ zrwfs9il0;N$m*PLYIS3+KSXQW8208dFKmPpn_BOxY~eC1IA`=k&jH~eat0$FQG2i5 zZ422u`jfH$080!`W4S&A1`6H_ROoK!WiRRqTJIpyWV29WYFh?IF3JV<#2r`C))jf@ z8wk%?E9J-3-^iaUJ73uj&1!v3qI+MZbiNB3TA9B4w=z1vHl-vY?W~(ph_6|YY9dDx zBPl5(C@2xup;*Kn^)oj~nj=@jk?FrrVW*RP3``ScdkM!WBaQz6P*Knu5dQ$z z+Z@-H-Hh(ozhPn-OC2Kr2|K#%2TrZ z%T(#RT93USO1kguhnTvlH8RJF(dny_c)eySi-Co)CL*8{_7g4=!PuhDB#VW?;H`?Q|6 z)7XsWpT3{5xr)oBcBooWPv$KNi35*_pP1Wu`HlDKehV3f(sb~;BT4C8ewx!5dR*#K zt5Ha)K_XKEq=@i^{<%bs(>wnF>!3e9533K;%g4{9$8kFWV+D-Q`B&cV>eI04!D0xo z@3on(Yk*>gxqGIgO@uHJ6B8*dbH~1pM zoKChpB|NGx-?-2>i>k!AlU5X(>k}x66J?E}B&4Su{9XF4`1$6)Yx}+957Z|>_ov5B z@Z85){#3MvS?a@Ubj{JT_BgVJjH%qEU;f-t=&%IE%ueMU0h!)1eO0>~*uNolwvE?) zvDTf}?bfZdtZt$5_dxd2Yhks{tHmt|$QD5~QpNqPSvtxaz%scH^~61swap*cF8KG~ z$t*9HzDVlrGfzZl>vxse9SPh0S8^IQ=J4*)P3EZz}pLg4!sn6}jA7s8%jF{(8)^16E+ zc2u*Fw_ccis$L@H8A8>#3j^nEqBhItXVi z=5)LDZ*VZ(x5{2B+M=Q?DGFVeSS6@K*ujZ(544AHnc^c6b?d*a57WcNyTRHY1iqKO zitl%9aCRpH8rNRtDjls&Dk)Q2MMk>~^&hBAUF;Y#BG@Bs%YArX5j&aKPV{)G^$_qo z!(5J$)Y{`tT1R{PGgneo$I_>Zwv<@sKx{(RoPu8he{L`!*pG-3f+^A7ulhZGAv}=R z+M8SW>Ge?3UMcCzSsN3P?WU=ziu=7cbXrkjkj0y6a4lpcAQGm)Z7>HR0wYV-=*iUm zs``DsKO%oh`BcAZXRl-RH;?{gcv;-9sBhD|witgK`_ZGm={?Wv)vRy7M(fnnP+hr% z%390HzPmnn5r<9*hzSw7UZ;DyG1u(?oyOEZP#i??Z zo$RrW(-mw7w#_IQ1cL0$LA7t%BxGiLCsB9f>BIEi@TVvA`|z8(nuouZ^;Pz(xEjX} zp|vz=+=M4Dlx3NTuG>XTYpj)TCb1IYQ3vyxn&-;zr=BZW=&vKSCy_dL%(O9bE77d{kkc%}Qf?vFO38dW5n zGSy<2P^@ zp39^{wInVUye$>S6x2%B13NA^+@>UrlEayvU12?~Dcv0fggani3_#dTX`p5|KHg7#53~jU0A-M6Ux;7|mY-V+PFW&A&FAsnW_OLQBW-N4TDLTAQWbj2@&$ZN zlM0Ab{xU&agWEhx3JiCj)NQ}ff7!04YSiSiRv|*=GSy#51L0g53P8^jiI;zYMDjIH z?~H7)EUO!NxOHN2@XG@v`ojZWM{h&m2-Oi1ZBYb|?}rvdZM^j$aZfy>;&Fu?dDK35$u?aw;1VV*4VKPZO7ERiyVh5N&6r{#Hoe?F)3=@Vh(0L4hScV7tGA{ zRZJEnY0}1)BRO;1U|Do2s6erU_W?5xU>r;t$cV&oA_$rJ{PySrs^gUGi|Jmj+rf^C zOvo)+IoVj=EFfi-`e19gSjBEXEg_i_nHU&~Dpku3%1W*(P31TPD?;tUIO7o{78mMR z2}HJk$A5uZqyGRA+L+9vA^d$?HLl%INxH)f6=5~bW=bR~H-kzQ0MVG8!U_d{mESCS(X!7;KU6AgnXFUxu3KLRlTLZ=l|u|mD;)Q%#ZaYf^r zmB^TKuxKDiiGlo8mhGpwRlIGeeime9#$ubcYi_}oKOvFJ(@=l-qb#7zv%sQ0u zc|2=ZsVQ-7i%KQF&M8dfQe|XfAy$cUgw2`U!Xt+JM%@4Y>EhyzT0g-bhD}VXO{mpAA1DF;B zOz@eKf|)7bXy-sD3KNtn_GqEjs->zHQU*l6#LGW5iZ_XZGi0(X48%YgM11nzoA}B! zYmvC_SGQ*I)W_9WHOFmNQ zHe;Y2(yfxSmyxS-?b5AgCexngHh`&QR0<}4?k!su zEFjECg<>3`ez~8S=qv2Bwv`oT^K^46uBEEOE^x*zk|J;mw94=%Mlk~`5#hW{{>pj* z(ye4OCN?oMgTIYA`wDohVr-D94q);jut|bZ@kvhWIE{{CBlo&2g}|+ru*`LbWiHk> zJ*#uT5mY_X#DYkKAxQv9Lgamfa(_E<*vk@(qH}7@{eV`Y#b!2iyCmADxa}!m$SB`4 zB?7-(@QtP?^4c9@*=6}f*9Pi~^y7O<_g6Nic$~~I<{$-Z7+}N((<3py{{WZ30UgRz za!Xc2F{Cqkg8h%bY0g)qa|=kB%%WN|6FhfOPVvSjM`DSasTS%AY}2^7!Q?@f`7Zj@GA-u>3Q7lwEH(g zTVaYQC`DL0fEFc^g{DJ{n$|-QdlU=>VQis$Bb>XO&p54DTuoL|6*2aP0v&CJCQKCw z408OxBAAJf$Dr$0pfQk)eOsmXjgp8!mQuA=k;H*lkX%PLuh;E{G9`YXDgDIs0d{QF zU7XGjCUcIY7dssG9Bm!-)-+W1J z>;9RY;si<}AfjXlNU1fbrtUj24rI%%@?+O47O-IDDHW8qBxE9gLLfVSx&gf*s(8#> zvKEt--+$K-5OoR&h$ z#EGk-y1~Y5E<|VBAAujUoH954&i!2%@@%C~5k|P#!dEodZ??^&tka)Uwg_ZQkgWI= zOz+BLai55eplsz!{_3_NxSFvf+-lJ#xLM6|g?+SGQzezvCPWhkAqGw2BRq(J!T#g+ zFA1u!?2HaaN$Tu2M!k8(RdvFus|zNm4A;2)gqXXB>LYm=h}-9|d}#CM%0C41*i9R# z>*o96*vfpYr%&V?Gn&)!G70C6y)!1jA79ORm|Vu+AC&c$`8YpK!L~Z5DrWGEnOUUE z%gVxH1`Gu@0%`0~8b2%W3uG?ew!d{{G@RRMT8L%b&^%VR z494Un&r=uanI3%RTaxZ)YkV7Z)1A`shtAz6sQZPXH6|ZBZd1=KO4U%Qk;d3jPbFGN z#1(eTkntt%6Y)R3T{H3XxjKWxN}bQ`M{~P*s63MKCs}FRI$tG-qmw4*^mZ1?**0fS zuERdXh7!PeDgY$qQ?&2DUTkdWJxSZGbEf;T+Kp+S?WVk}`xa?sXtYoJM95Mm!8bs` zOBhC&Fc^p*!arrS9~M5S`d;Qo726*xa2U?hYL=UEi`3P{hl9`L1|Vdti4nzX4uj-m z96*GEcHT;7XE@7@C-*g0?>sykSnf}a9y5Ja{AcWz1HC=g?jDEM-Jiw(0MXh<$SoPB zo2Yc|r@*dY5*d0yKFU{;Dp)$TgAxY8x(kEDtCPY%rnl2az`pz7d%3N=#npaxd1-+^ zztOr=NNA?D_=hsP7Q3T@pcfd+yKpr+(-M^wLHtNbO|z-{RhR9?wxQ%_iT&5g_G7%w zRd$wNOp|y-iF#8}cEh-xvF#4A?v2c@ zQzlyLQw`zBW!%>OEx3xqAabOV5U82pDN0l^0v1UuPn8;e)?InIF z5?JpXPp>(CKkC1zw{g6#?mwPBF?M6T57(K!L6XWk+O(SPV#bYwg~m0Eu?s}*w54Cv z$ZQJ0zr=&9%e5MJ%0H;rm)-~KkEniQS>?MIueAZH4~;M_({|r}d8U zi^?AreBAB!jP6E*)99&$Zn;)Ay+!I*+FPV%P(08%I^Lx)n5l`_mU~3cBGq=4`keV2 zqx@m+hl5?)$@aU*UfxLE(VBlN+!YPnrkP@GO)+(6u;v$?< zc{y8LMMk_idsoua?9=f5QRCEfg>3gJen#Uw=kqhLYVeo19v5k!DRpPm1Gze;`mgE@ zdzG}0x53QYNs!Yz!V9en6#gkxY!JzqFVAT6af|Lgi^%s^v>LToe23WST~mwH8Y>)( zJ8st0d8&4n;izgI##ZSWHtZX-f6hua+b;YT@GHJPb(O2#sJWx`uVC@HFW~h~yUXUP znsAB$1~GN%je%pV3ar(cHjJ)He|yC3>aW5-q<@OO1ZVqy+)lyhYaQe74oacX$aiB} zWk&6cE+tbWOU73sBvTV3Wd8t2!Eq)*5k$a^iE>3oSv*?nzVl}^COhgq-#+H{i&XcE z>Tlir<~K^Y^&fdWR%dB^n8A2u z+6_sl{F(9}O!n@Uf4liS79m4{(>V#KPqKvC-W3Q1MOkB+GLs|V0xc8Y(Fd2EtL@j- zJIGG+_hzSh{891W$Bh$WG!OFG`qJF*a4Z3X8*fa*cr6B~SOwD0pfUlp-1Pq~a7bE;(6sh_q{ z1b}e}mBQnZ0>q+sBMpe=N+rZ2iGndSO+8&fp;hAh3oA^@|PdCU;Nx-P#{2r&VI|J%`nm(Q8E2&eCNp zhIQj$Q+Bm+B7!ri5ClwI5@eWMN`86**2X@{L!Mgy0P>GIj2mTPFSxdBqksy)0!0!f zv~ih%jsrV&K2sr3#>E;~ym{iQUm9fmi-ro+ixP6RETRoX$`5#fnD~#GpNO>S1${w` zTkzL&*@~fHjkUHRe$yZ@3|TANx$IxsGqA+(`!CZQceE2nOPaZG|0A%$^v&O z+i1P}Y1!+ZS!=|2`PmI$pzyupW|YY0v3hS*0`x_%Vfh_#elN|LKD0x?p|q{cQV0fJ=h z9FAuvwfAWJgYI@N_tNve{e{!|yXrOCZA+)E_8Yw&tTf{va{^?$9-CPdv2B{dmvaWp zZi6C$m>`+x1)Zn8VO_)QpK*LV>`!z2Ec)4Y!&Xx5hj9CIg;d+acS9VSye`{ZNH&-p zF?3?!^MDy*-L#uv=YIXH?3ZXhkoBtfce_2V?$>j>*WN+Y*==F2yK#&xly=p;Me~co z)~=pY4m&nMj)@j~6$QM&FOF{ae_!?owtCyOzAfc@tK1(A{K(YWP%)Zcy7hF&^4Pog z@-|T0b!+8vYp{!V-s%gMwDxK6RFTgBh}YMC{;2)u`epVjR(OTqk0|tCe?Fr3a(1#9 z?DbU@v}o)Ffe5Q_GLu-QZAY!;6+$F7CCo?9$QvQo9kB6>$bSU^{rAwzi#Ms0lCZPXIUYh^caX=V;VhUV_=19w4l(|sI-|3F&F{~Ln)f5!j}yC#f$iR> z%HLi_>?;}<^xm@}mMTX%jUg#Wk1~<4BP0aFnA&%l19qN9_DjIOr8kV)lS(tS*v~Qi z%I@wk9}U&E-D4G|=?urOApDeUR7^lcV&ssygpdL*3K;85SPI%dxHxL~ZrxsdYH?9N zDcY?^ZC}i%fD9eHBaNP%ok4+y5GnJJ2$>^PgsMw?U8MS@_|N4=gR3kDj|1updrO0z78#Li~ry>h*brZb=!g z(P-;9V|6T9?zvW4)XYlZswPNBG3F8zF+Z2U9(1%VjcaQ&r|=!5%AT`~&OsQ=ektx> zy>^(vO36lH5Nei5M3DgS^~`O`B#YfY)3@ofg7C{tR@FVgZ65G>A(8pJ1YtYG?Y~(+n!S#~WAl0aK)QHpbfE0svt(MquC$4AvnFE!!Ub_L z0~-$Wx6IF80e+mHR1W!grRGhX*1e^vyQ|wwr62lxRaLEyD|t5rnL4lW@_(DJIlk4Y z5W!sR(r({@b+qiq)d$G`048f|4JX`vci7I(>Fl0n&ANI6M{2YyJ69Rs>jK&)x3yp0B-|Q{Zl@k+CxtcyUBTT*@k0n0oIAe*k(oc7*Hj`YAAP)fti@w z{m=aM;_cd%MGn%XpdF+@_X`DYETdmAj0JSd5Lyo>g5(64r|tP)7M=@P$a6 zL`Nt-yIafe33TQM#+^;xj>KxR(Ov%l()MmP_L0u%M`d*8u%hcT73Ujbu#JopD=46g znaCKB%KrogO z`4n~RQ|Tq2&Kih~LTRbqtc9(X0rr5&mXidAOo+ZBS@G6x{UEf@Vle*z zQ|`}qUtHvLH)p$#r}3E_t|tpu0dpag)b<`Fk5zJAc=S>N`=Vf+#1Rl}KLR^-sl_~v z`*}T0YySYJr(`^X?w+6WU&wXnzTNlB8>){#p|AGn*`u%Hxq^Tdk;v9@KQWRt)#6Sv z6`**CEH)x6PmiWQkbWrV{GswM!%re|_>TA$o4Ms))sXDf)?$mz%pMmJb153;?Rv$F)09$htFELG;!(B~tGS*`{ZTwl?`CVjuKadO$6mqejBZOwX>CW4z+~U7G5G{qYF^83`+==h zHWBvBOu$PZ90k1c7>KJ%Rc74ZW6NuO8qQJk9uGD@QV*$qs``N7yHDHgM~K(jTAqZ% zzY(vncuh~W9Nwr^s>5>d*rY(KRp11;7TPp5B|J_+`NRpIrAjXlbZ z0i=*s^IFT(Ud9^JED!@PzS8$H7BRqE$#R09pY=1iY%kW8wX}cI`JKPSc?E~c_PFWd z`-z3V)ls3`Y+>NpMT((VEE6xV9~7RZJ$nhs%ug-nuWNDr>@0a_vJh{GqcS`b@OSBh z=2P)@wAYVV4Kb^G39qvHM_FOx4{daH(~<12jZgDJ=?N=(7qk zM9S2D)hHI&ApwYk)os=F2~wcrC+WM;_1gZ*YIP&QtlA%6KdSDL*O^}+hs%d*Yb~u0 zc9d>DrL8>`%EI_o3Z8%zmmW%oE+L$tqn6PfdVllrxGyGtpS~w~v#q;jW3#>a)|jkr zH!F>yXCFfS+-693w~zbmfU$C^6djWGN47zHBlbOOYeV_#^fJAxt9Oq~{Tn&=%?ym6 z*w?K=ntvaAesg98h7~|YU`w3UR}%Pz#E&A|XN_7fzn@p_XNU4r$_yr-?6-*Z_Q&YX z*RK{CCF;&ov$D6WxMmh$r3G}jA|_yn26+5+S6T(*sZMH^#TjQQuHKw-SnqXfdWmgV z;PYdIEXF(dyryFws%k7%eN6SfPMk4V?OncZV0+^{%c55v26K zo6f4ChpMIMrC^0%KC^-f=`45>)i7N7349E7uj^R~mNNPE;j#Y!$KSsR$F+e>t{cb= zLd9BGlD%BwTqIKkKBEAVkr){IJgRP+Y^LpMPmTiZ-+7aA6w|0GHMW#X`zBO44TM(- zstk9m*$NOTYl43vou_#CUg|2TW~IA`wUK3id!pYO%dpi8M|Mew?CTr)j#x7T%050u zXQCHA`d9Kf2j9U~w^5hi!RD0X-VB9$%FF{}+8*#|EQ_28PSFuDyvwFp3c0uA5NyWH z(n*+II}{ol4<;0d$2GJ?W$KNfXXkI8hQ%&^M$5GxKPH^9<)vQBHY{1Jy9Z$5qJ#Nt zL7R9C1T;-v#T>@R%6fLS*~(fg*yJ|;k7dYm)m++5tl4wkxa=ff;S(-xy>O%?uc6NG{5xee*EEE^t#5GZVBd&Hn%c=%rgRozFnzDU}q0 z6<(_|m7?rSl9lX~E;t+r&luc(KU;JK=Z>MAH90Y>ZdGcULcq6KFKiUgKgjDDkHj2C z#XR6qq}Cgdq4v~k>a50@6s2pLYqyR_svu*I%Q!I^;lqDiz)5nQx00FXKw2!j zr?QV6b|+K=uG8F~?W|XARA{io-(+E>z?@KGU!N_4CS&F%SYX$3m|Mo_+BI&n>lf+A z>DM6=*#ag>wY#xm10yplDE54O{Bi4)Uy@cj^^B!ExqU+oQP_(XJvHgDzw+h&$|sRb zN5;hy{^y`>4jV0qy%+OI$Wv1vVm7$8g>x1DM_p+ja6xY)$bKeZzux@-PN35s=KY>x zwpE94L=#zcH+DqKM7Ytd2oZr8+CAaMVqmtOg|+GksLXw=F`+aVy0kKu+zPAd9t5;1 zE6=ZfaS;Ywq{PegQ9TouuFB4R?2etn;B@mIc-0GT!fmpn2u1_c@Dde_M(|J*Ka|HQ z+h{A6e@s%HIF;YQ(`oZrT!hw!R8^IaI+qSce<&yjA&uvR#{U2tKnae3!gUpj*NXmS zmFlT;3T@(f3@&(A1uA|Ey1(Gq+IYKKszOCT-fZzF+pfqbMrM zfMgcow(|ua`1MUP#~sX)DdVg%hCuJH&pv60Q#oE`Z0k z2MltZd1sHiWajN%`G5Y(j!ESre*|$GZ99C-$OT&|tzMODR-2Gg+BPcWT){PDnI!~m zP`EwIYT!5vWA-eN-)-Ziu%_gq&I=cc%w1>OTgSeoTMeMeZ#1*lKB4t43RN*G9in)_ zOm18B-Jr$d>{+tv)-@b+uodbZYWGJVy#y7Vt#A;J$xQK?`t>SUL~!ErE4E%zub$fin5edD3B z>C9HGm9LM^;<0{(vm}@-pW6!wwZof9W9}&75FzUl78wCSM$j`SWE(Us&OWJFeH4IE znvqo3V}~M@fEft-cHfkLu@V^HZK42Qk$v{o3V8%3l};-mIueb+j2c@^Kcj|!)1@V{uTt|!F3 z2p};unG}RLz5+m`MkjfY5b5?B>|3pk1=n>eZ(T~2D$SzxNEu1BRhcNU%kD@;zaQfx z5wJ$|WN}mgXkzkM)m?R}ED`=v$MfnjxFaOJH@o@--Fh-3tq61B?WLt>KYt}riPt69h6GK#@dXEws94q_iQGTARL88~C2E10I36Pt zGbR53Jr{2@oSR*UTC#lFaMjZ&v0>Dwi1IS80Hv9cJdPS-K?EF!@`>uD{{T6YJJ{u@ z;-MK^i}tG(%C`fM%HTnZg{=o3s4ETJK#P)~Obn(QH%`6Q&ZVrX2t|=ctn#OmV;wTM zT(Q+p+X+m8tXyR*C<34Hk?0Dtxc1%4)V9p7I>mcH%HnLb_iVRZN+pYeOP1m&TaskR zkHeq&Ps~Ok_j9lzs4;qq@z8*Eg(Bfimeycfl+ckC{irX|CHW}EciqMR0NJ?){bluP zv~F9;+r;0~*(w&K)0A~eO0p+iJj{JcHid|;GAbCEiJn~glIn2zYPSi%Ml_HW7$CVU}c@Y+vP)b1B&7UV_!8I#Jj$Bd2; z5;ZM}l+!Lk$&|#x_c)Noo_y$Qiql!ij^Y@wa`-I8M2=R8%BNwqULrl! zhF3Ed74tE)ZMbjNvo)7&boHD~i;u5_yLq+`IJmuHs11VJAc*R=eb(Q!2J7FB&@&&G zF0gmFy@%F#9es|}UA@j~4F}y^V!i3k<8oyA$FgP}61IWKEM;wgBoRpn%#8jLG0~xe z(NA%-cbmPB?L_vcwB5rUN7|}+>N=-LV)BcNR&+*AozJ)&j+e;0^Qp{bwb+AX0j-82 zAJ}}eefs0WKREj|epdPQ;(vo(;qKmz!Yc!(R!;A`oXxPA&z}z*uJybP?jZ( zu4YU~#CnUZ8F-ELoylnHxbE2b)HrYnv{mWr%wUTtkn#xOYNQPCA><`2kC=iV*4=Fr z$bT8T7vzqb!1pV+Ry4)^Uj}aso-$$4c@W-nC}a-IDdh#iNLdpzKkhp6-q_Qx#F5^G zdX(O2yfVb^pVmt=k?i~X6W||=xDThlXzip7GE~=AmSH}{E+y3JW1v+^-x!eg#QUT; z?=ce{Ya_=$9=oaKCw6LfU$FhJ(0b>%wpQDAuG+$5`PE4}dELs|V91b6nH&j3nVg>U zJvP*Rj@0;VccpZWtJHWN-RT^rckJD(ER$1+`xjx7W|LZ2)^Yy;BQXvB1bW~9 z0AM^`?q7@jsQRk-uj013PNee7J%Pt-ZqMrmf@`l|X_sQQRN~D7pQd4adFz3 zH14>>*vQeTM&OwH@DFR^RE?Ix1T-eJ$E<`@GCa@o|nX!tMu~U7_(m4cR+{>VRrRK)si(A6>Uc88m{%s&#%TOY35+Hi zn+%t|ry|MXGc71s%ulFxz`Wi^QC-Kec`U}U%}zNpYZSYdtlWy5ffD#Bt%yv-5k$)5 zM0o8H6Tbe6exKDohF&%Hx3S&N?#F$xK23LDH<7Dk#{L!d)ueJTpd)3xYCVe+Q(3p& zJ9C(j+x#Q1sC`O4FyH6=id&_Owb;QO74&@X@0k6Bt#h=!y{|*J+0)1?FPhf4Mq+Q| zqPW7Vx|v)UoA#SBVu*x_6YGf+gCdBuvfbb9E~4_gMt2kHVcBh6se3=;k8CaI{@C_& zPE}=kmGuo4s3yKjvf6XIE1(U`nBd*k1hND`m#q8U;`*KGIv!l)Y<4@e8vg*gnY~o3 zZ;tfz)}v$bnGHJw{h3_PwqaF4KJs*&oJ-iySFdEO%U)ff`Z4$0UiZCS=kEUik=?oM zk8x?GIR;bh;|DF9tC8a%+qSG;AGcW?lNM`O6nfSJ$hYGfnx202XM!F0)qRWctI3Oa zu*vu9y;;2GuG4xCOwpH8?aqOfd&ei0Vq@Bn9hQIqPp^QiKVm{Qzh(PxOIX~-_eaQl zELQN#x7v#lhVK4F8jtwVz^LRGWzb52eTT{a0AQs<2QeiCJ5SGYc_ux>?Pr>rr#+)Q zvdU|I=jd#raJqB3x?rOpHPN*5#|jbwRcZMFb!5yW;sbb|uH|ZPHNLAJNa)_pYP{~A z*123&lgq6XG{|E#<^;|pEoZYUOv*~}$Gkn;bC8jU_?@H)G^cI*iJ0yWbpDTCGxv|O ze^Ac@JJPzj&fjG<$UTqkP1|ZaHykWTLn>OPy3=55u2%#OS-^`L!Rv5d7Ljl5N7G|c9tv(pE=1_EXaj6lJ4v(FOy38#D* z(Vdv>M{I8PyF06HU~)RoTH`U*3nQp|hhP$<1F-j>Z3;*Tkx%v!M8OmQ$0IJa!}WCf zk)y}H>_=t01=ze+z3*K<-(-URsdjaTTeqc{Yd-qBFm(2;?2i#sNw88=EgnTbJAqG| zA6A+k9`^4~c5|@V{@CijDf9G$HJ72Jvq;q1OS8zX*;HCg5Xd5KKtwL7ekzzV;xK!k znT#%k@~_A)=<06b;yaPs#po)IZmsV2v+e>h)@rdYbgB;}kYJOmw8&9LnkoMPX-gI9 z;p;MvzwK|1z4Fz$%^9dWapes5FU5Tly0<%-sQJj$!Bcgq37dGRnH;WB@b*c7sOc`%&XAnzmU?9+-2v<#i5EnF}=reakuiOETVELS|;R zHAGa9A?R$F7_peYcSdO~Fxu8L8kGyj@>jmAQn%P?NdOYgA&J~cL~EV+l*~-b#Px4# z%$8FOj9q+gS3=;9h0r{uoW@GE3M_M#ESg9+7xpydV`vD1iP~hoV{Z9cZHyJ^vtvD<@1Wjh=Yi3>NM{7d)q z)@*p0^(*po!)e{Y|d3)m$@#(|ma9Iq@t3RZ1L2Yg*t7<+RfaYxVmh5bi0ZfR5 zMiRz99bV=mMd^GlcMXqSW##NYGbIly^ApO+ZwmW^DJ}UYaO1=M^6Ni7nOYaKJ<$4u z_A|1b*~aPa<}RK$EvuWS?d24Tb|#&Dt4*nbD3CTXCKTwNM6dlvKyMSs&pvK><)^UP zJF+?tUTHr9GNn@1rZ!u%rFA!ZX_TxRimJ-O7BLuk2n4q15W3``>I6W0IpAN_Yrt=( zhig1w(SBZb63uSHcY6Lq2O590bu8rQ&v5;2QX@~rRN05)1J$^jl>)ISi7kkTi)VgN zXMAe${{UY0&sg}v{dcUqjMRB!#!>A?2ODDI>?KwdyVq`31+BQ+wE_x#t07x`P%#Cu zPg>FKFJ`-!OAVQ`gI0^9nzbs!8>;=o5_f%nac|PLEi%(0 zGL=sA@f{tI)0$F?@d(rS3{DSBfmfB9F^pAqaflfZW0NNg_56Fk+!o*JddxpIyDy#b z_sp$lrM!YuG!~HVC967Y>HY~`-;QqyuJ0?WJHJ>oCyU_DATe$NIzP0% z=bcNadn=jC*urD8=bm%3x>mHZ3wQ|Q{DlY$8xmt;ts6EcX^p)NtLgLN=X71`?q_Im zc}A}_XDa%bEm3m<+c~DE(l0x8IEAYSSSx@qr{KAM*i$}9^*?Cy9$9%m*`Eu%LZzD4 z*^_llZb`Q&s2rHKnEpp2krhW21XdL&250>r{{Woz`cY{v)ArNHwI?N=%ck0KHK`#F zu(IBwlf?r2MH4=-<_-19NP)=|O!Nnh?>qjQUF-1gO{2){j;C*sJ&?r0>gMxU3|vlE zw@xw{N8V$bEc?{N8ojR{@{*oW@e?@p^uh1WtMOa9`h<1nn$~^W$#)-GFFT{Vr=+yO zYAb56g+wnKZkQ>S!l)cA(4n7Lk?tcBGd+8L#rXx8cBb!sE51Fx)7o#c*sLx($z-#6 zs_hKYY$*0q(I8c(!=zWbfrJ2}Ud71BW6U3gK4|z;*gvVqo&EXAXq`#g&mP}{tERL) z+;$pk(!gFvvAHOxR!S#(4YBJ@jsi0(F^z|P`cr)Jaju(O9mDm(@^cNSdq1VTLeo8_ z??#uz>z!B`&2^}C4ua0jIRW9Cb&OeDS|9C`fB;yj|+RF z-7eL6I?&YKF83OkBRiPVMUiXtZb?(>L!45THbILLS(><*N|I6W$E~{dBhNn5cdx@N zFKzqN*#7`3{-ODtUZJ;lTGTxaH*IM=bX>IBR_`{l#deqrk=xNE@{u_$q9SKoe_Vde zXn!Aj)7m^gVe4tE&Xv^qU7728Gg@kUXdN!)*i;KfqucD+;8GD?k@%Wf{{S=AYIefx zZBCBqEsHF?JkI+2=sxpM@?*puHRAR!xLz~Z(J#X3T-L3~wYPHFO6v%(a^*V%Gd2B& zlL&6-CZA-^k_UKTZE7^jF%acpN-~b zcVV6KQ^238SBQO(@iXdO+?`?B9{uQaDz2XI9yb`)(7eK`w6%u97?n##x`nk*@i|Z# zPUZ1b>P;@C#W>0lZa-pYORY+cY*tuE?JpC*`84MHtKRK7*>5R4O~zsLHm=N6!KXi` zvvzVx#}%h3NDKJeYHU4Z&uH@kh#1>pjizM})(7cyP4C}tu$~z9=Dn;jJ zgz3x(S;6UilNM5o*OyT&6&hk8fHo{CFkDFE;tF?L*)G@XZy~kLTgyJ{R@Hr>&~DQ< zG?t$2&+?f3g{#yPl{eYRW%6}Q$83d^02&lhEL3{Wki^GO8a9gIbsCB;IP=*O+Rfz& z<}{acL%&N;qiy|fjniG%@4ssEx^uHEU3`WwPh`XG@*3YFCA3`5{$&fJ5Q11%oy7fP zGCNF9S#9-;`q*OpD#m2|KJbb(#;5YXA&yyRGRlCnitSww(qQE=!I>x(gf6)634vL} z2^{a$Us|uN#SX&b@_r-uNvts$uO)$VM^tM>HFJ#Z;EMfZuyjPJ_sB>EY6zG}dS*76 zn2$AHJb3QPknLTo@HX`}p=L!_8G9#p*>5Ld@h0ofXZ}v+Qc_d-L_o&>03Ce}FiK6L zqvXAN8$(hurJUZR&+5G1r@C&+b{5}|@NETqB;cuWk4!E7_W@e4B;+IdKw zVU&#dyAzLO(XE!g%yaR!rJMp+X@pQO+9&ZJh#4KfbJca~D&ad1jkl%Z)TM!CV`u;j z#(1M}l#u%8@%-X<-aqvn5r|$o)nkfS+Ba8H)K|$S%cCh=>@U+C8pGmb62}z5a43!c z0Dl`m!A(S4_nUJ01Xy*hb3)xaZ2N>ngu%Vf0F~HqALGV%l7oo-b@r^@xrnBe)2UNW zaP-p>%6*}z-@}TF-5}b*>-UJlIH-03jiw4ZAMA!}_()#`xXAm z=h|^J*#(Nm5Rnzfu4ZNn+U9H`uYrxXl@~G7X4PzUL|ZJ4eUO?~Yu8-K{sL+>@648B zBl#vGCvCs+=-DbM5Xzx5-)BC1l?d8WCe*QtLQpR{d+V&HPt6^7k!qo;K zaS;Nh=ZA)F~0KYE(!aZ zl<(v#A(Bm9%Qfw`E)tmo#cw3ZK30xWJjw(>wiu6{)+trUGT7?*6J9W)xay?LFrpzU72sI~ z)%CWRepJjwUA&JMS^m?6Kvs?4w*ioBmA|Wgm3qs8!oFZ;K4Jh}OAUW9U9Ku9%4V#r;OsK{P`_9xiW-l)kz)i#7*It&n@4Z| z0EZhze;*m|TPfxjmR3gK+*(;;s@_x>!UV*`69^kB`)KyT2}A*o(YMJ}=M7Cb%NDV= zB&sY0HEvg)5Nj3?7)rz)XoQ|bE}4;;nHc#A>YK$G72YqhHVetv`*6u+GE{@h?d8gA zcq|EpBM}3NQ7eTbkIxHg_Hm=tm3a0+L(CSoF4J3#(Cc+5x4^)qYa z#nhPL}gJBJMkVr5gjt8dgf;_e-~?1>H1b&^y9(! z&QUh3scTzJprZb}L6eW6Y??R)BJx+uQ+sd6$UA$6{IIrWL|AVck# zfSCMZci;2q39f%`Q7{O#HwPs7CtIuPs(ilfbG=267K+KnawR+@;Zw(OiRz4=cD>|g z+gX(tjHFYKnyt$rtf&DbOKq8x<6arcOwVa&=K~u|bs!@w>-HaE6G*|VWDY)4?3D5; zc&R8v!7?Y(-)}gE6Qp+BCT6rt+Le|?_psRI$i-}~MpC;JY>7jm&S5lvw02+;xJ<~m zjLgLGfEDL!VB+>a4VB1fd&-DVDoHk6IPdFQ=U`0Frf1*rxx~oGl*i}Lbtu=OwXzeY zviNW|J;|{3OMMdJn1F&JFYJ^Eeo-Y5F~7veOmNod<0#?c3n6+L_R!h6yc?kF!Lgix zb`<+0<^~EsTWz#QP4SFl5Jx!t)}&of3&~_rST5l6fhZ!EarEwC{&C7=cb|?u0K4ug z4fDM;v3TllvV}EwE3K`{TbKbOe=ZR2Br>5E2|&uEqJJ-leaCvd=C9;4&(*I$TFNv9 z7aUDjuVmi z4l^{WUG!(#77Z?`Li~FJ(u)NmV6TeH<5Z{E1~Q?Mn2+wBRMt(`*)gYk8s&k57W7qs znU7SEvKXduV8F-(O2#4#88aN@=4xdOZR)phR-C-1pZj#J^%IWw)=e_rMj>$UX)pm$GYsYleCoZh=A;>#$5(!8aCNoA!4&G zWSi}V9Fjrx>n^zrqw-^kaM3222RN7znPF+QtXeDp<8jrr{4A}my>yBc1qA?eX1VS_ z=K&KZ=17WU{{ZvpIx1bi{I*F;EOlUO1!+t)0TtIGuzQ=!osa8MqBq`0gl)Fb&<+B? zry-S!LkzEMYeOyDVpSGBWe}vUv>&dOiHY_~=3wR-6EpcoNwc~=?zmR-T%U}rEF#5? zP+mfJd2HpdnAnywN>wr>F(R1!W9DSrc$`pTax}4}pULRL^_2QCqSCQkWC&PIGJ;_H zN!xB*hvt4a>8%X4ikA_Np+h2Ai#JjsM-(X~nfqturf-))3~W>RKw1^uM&(mr@j2KwNh-xu9e9f7GBO^(;xpnINKP}6yF6n(e+(EHl?Q3cbjzJ?Ts>H0u%DF+ABo)FzN|4w| z@KGGgZ`||-et66E9_(+MH8Z`r?nV&P-0L!R^Ey)XaGZnBj6pFhAceJ2$fNTwX50S& zu03}(uNAdNW_6cud&Ag^daJowb4y%yYZauIta3P&(nIHKVTH9(vdndx1OPvt$YT}x z1pLhTx9?Alxg6!DYg~1GMTORMEL;04=whq64c$h$MYuH?LgR56<1rgdL40i)t>Qn` z$GJW>W~pfpCh=L_ccM~ZU3$q9r(LG9*kCdhNN3nFGs<1_Gq}d{#x~2J4K7Mtx3m%@0(-R~vw{r>eQN3pmPq2Li(pT1 zi76=}F0>QgZs+%tzuNhI+42*(n$Nj<{iF6Hn951QC{z~_vbH@Ezg0?$IezI}K+m)0 zKQ6aHtF#AVJl4}V9d)C%4jMCeYsyiVq{TDb!jjpn{{YEzJmh45_5T3AvFEX!n(@1` zJ<{$*gZheg!&7Sh-RcYltsSOyy>!k`4WX{vgHtjVv7q<>5ihC>KFN!^=@gGfj<@Gw(+NVNxYfy~c(Ap~*~0vrhL0x~rzD zvN+d(ayquvQ7%)q+*r#A+9g}fnc?UE02(g#_M^SJ1HayN_p`A5x9nzs&f~R@uSw+W zy4HH_Qx^~1+HeADS5p|!nH|_s6aJtkK3{bz^Z1pns!kO$@RH$rV~%oMEu(1~S#Rnx zksfIMMSMc~oc5o_Uo5=F(D!sIDbu4GlUCzvBL%B;CmYFw zXZ`OudDvYYV^??2wp114*1PhHO<-)lIiAqC7-8AV;Smuw7qW&uYnWum5;zud<0CtK z{ToP?$H+px;!vNSS1ZGB!+wQU=VYeTIbYoEe-^w~@?*?Bcc!~{;}30Y>TE_kD;Uhy zrD@8r`ZavbjhCBm*V z)lQ`U0Hv>?qrwiQigo>dt1e^$L{w1G^suI89UJ#wmmS~n)3!ct_|w}@H#qI#}%)s&wROs(p<`IYGtT5hj}0*pnkrFd55n z_>h6)I#$x!Q!A_UI@h|o&$`*vX#`wd-F>Sv>@1$9O}h{2~fr66RARVNg z`0(q0pIMISRPF9o_m2K(c9XYWOKKfSi)NBfc~WjYPer7}xz2!GKVdw2*FEX(9S+R) z!y8*Q-J3|OkviE7n84$)cFsn{>n7&M+a)RpWRBY(#76V+74~Ds9`t!3lXi;iT`f6~ z*3iZyBb3QFDB^p6LKV&VT#bep?g*~#lx?I=@LX1$jgGL{V!UO;NWz#RLE0%4Ly23&ndJhkn1zwxiJo+Eg_de4!T zdo51IMfX1SRU|boVyzMop^FJ(&9&j541%9ieQS@(0w=EB`WE<&;(wLBxy$PQ$9Ze6 zvYpPSt2F)I^yzI?pzC6@>_8b=wTn9muuoziX(>QkhRoMe3_wHJP+A=A)*^dNrw;zP z)|zipYF5;&i)_Oq*;<&a7aW%{_&LnpVn=PFB!B0w>e4y=ZKAW0-+d3>F6nsV+$JK- zSIcQ^h*ZiRFl5|ul7mrKAPGnUK~n#(&ozID7O@^7`Vq&wB7 zyH(ts8>-w*t)TUVy2pOesj#@nV)taZmRAR`wGuB>FrrKSb*W5m{{S4%rJlI8hRoH$ zd2goh^Tm$SSnlFleOn)~B`#i~e8(b@lJ4XtoKLgTdNkJs|- zORL~CR%;|zDCzv8ZhhNw_}2S`it9NbWC)?KDPG$|4_n8>cDqz*tUkHadS?lztLj>H z*=G`rW!n~ikd&@rRfe^ofu$uNzEC{MOr)igyg=@jR!tmm%zN>Z!M_SVn%X}lQ^yX( zXimmmy^iIS%MzycRn&kWd0)u7C8Q!-%pyuv%%r%%5lr0L+f8==0J^#>C*7YSimhKA zu=VFrT*PY%ri4o?49MKb2E-3piiqIkNGFYDs9@ofA!tomb0LoIe}4R^!7sEur^C&A z7`mB>B6FJL1)CN)8JxOFtW8C#1M~ZG2!i+?vDZ`UIv#L#X5&Wm{w@!UT z&$u3GcqiW7SKOZGYOwD~)g`ac0)mNH@!k$(=dIdyueLhd zQc~1@Q29C7KOH=L^3I-^w}r;{9}@Ix8@SACjijQp0+oopKZLMBtWiw10h|biJj(fj zceQ>TyfK}>@*`aLx9Vw=%qLQ7-tTE$OJ^BYk{_{u9xJZtLqMUBQC#OMo zFiJiEKi>Q7E59Ag`a*pnHBk2Rx!tkV-g|kom`rMIOLqP;uAs`;J0wl}%V&Ff^<>y5 zFcUu5ji!DGXRQ|a;ot1vjNVjskIFH`Th_X#PwE)WX6s}ME#Ati-DWD|C5O2>(Tm($ zjPDZxJMY%i{O|fm=#Q)a0CaO6e|sxZR)O}jwftGr=eP)VP zSWPDrJcdt^dwQYuowVxBL!X4#zkhsqEL}Y-Wi+lH5t=?a>p!EX)}yz&%MII}&*S?G z-%U428`EW zcQaovUn@s3Ui%Hhu9Z;1r!Gtpl|HyrWB$=QabtNhXOu)o^sNfiujLvqG@~oZ zBgEwTvwpUJU28g9SLggK@F%%zUvt~X9j?2viIm%3jS~Zd$Ks@R_T>md1&xW_5JLh` zP*Was{ATQm=vPK(Az}8iPnob6F96#U6ZSOhHRlp+B4do&B{R&+=Y6BErImqijGjJe zs)}BFIj7f^%gxxxoy?c9>{*M_PRJ9bf+5a%M)MMUp_4N(QPy*tD2^zyv|5>MCx0HG zY)coIywVVnCMcwa3|G9k=aBN5nf$(1W1C&rtBuF~nb%zumDrl1aWT$AgcD8#m}IFx zRE)$7ujlm;AUGlUdspk`FfH_*W;m-@Td$$YX9=;wSh86(ePl5^Yy-Idra&Uw8jxKC zUDCo0T?j1B+XZn*A`e1QL@f=fa3!j zK<(5O?_Mi5!x+nUk!q7BnM;fwuj~^M;>1LO3`Bs;Z!r-Qzt2E8Wyjwvc2euWYwj|P zYKpR!L{AW3^NHjUuA0)@XbH*w>Iv2P=F?OXU& zg(=o4nR?pStfZsVLcUWaGDjp^{^NM4jn!gl3mfX9G@WKL$5e%N)-W82h^iaO9JU}? zGs}3N_>QJkXT7bqtY}rn(y?4^I@N=;mQ*-csMcsSnE~*w0r5V$7LNOTL~_zvvv|D4 zC1vjW$o4wN?r--%>;k??6p1cCM2nvlj`6o1fFjf59~k@u-lux$umx$j5-YBI{{TzB z30v5?!#jVPg?2z5uPD)ra1GpdTx;nl~ zD6E*SogA=$laA=ttrjJ&BjfP3oRn}Kr+xnb@+jN)y%%zrrQfXkO?XqQ40^p zDG8Eu*%4AVbU{}b+{H07yloHzDp#9^Zc#Y4>SZam*Uzbvb8{SRXNfKuKl2A_Ff2bez(H4>6{0q~VT3AG_)(0%bOc1rQ|-+GYuD-{You%rmRB zot>>}l2(UVw9X2FeYFSX*}8^IEO^5TC1bmbk1h6&fEX=fF?nma<zQlcJP0}F`Hxok>fb~S#IF)yEIBPp3Fo~}!lE8{YT zWH~%*kec@Fr5M?Js=0G8xi86JQ8P0F$Qd!%qn6PT03fww8Z@XGVzM@fQ(#lTB!>4)w6e1R|)+v2ak5uo80|mF+FI=R2S}_+zPujBDLd zAuJ!b`=Y$RQyU_YF;rJ$ASPD@1q3ieQ@rmn02x~|TOW!}P0tJX7aD6&q*%tJ43=Rl zPXUsNIb@qA7+*4QTYT>`>ZBr#*E;n~ecXkH6aeJVsI5}bxWCrMA?la=VirPYGI2i< z6Vc8FH&IenLm`dH!}iS6k^KoeNh~yc~Aab6p6}(czvBl$)|8u^``DX ztQvsY0+-0xuOMbZvKSbSAUi~D^Uwj+6<*15$WyPIsaaJf#fxs3iuk2Ui0!#ZAu5Tk z_XNTmCk&IB800fO1x%=Dm_aAKaHNU>3sI;$kLDC1o=+KN70& z-DQzHJ}Whs6ROt{doD6>pAoCS+j^8P*jW)IvXu!$^4Ru@*f`rOqft2OnTFL~FxQ8s zYq;I_A7I#AtUyQ$f0=?VFdvlsL;?3HUX6^sXl|WFo0Tja*?rocp}l9>B-u+?fKsL+ zS1{QA$tWo0u*5*^vZhT8quFBpwb*5P7sbk~vbHnqS`OOgTFz44r1yxKkC^`el8Vdb zqMcarS=#9`Tdqt!wXx<3TZpWQ2`*$}%S+-nBa#0AV;xsXcN3RkS1m96MI2kC2?6hm zY@RGxegLu=VhCiQxAqhemhd}33*#QYv}PpF(tI~4w*&O>y> zuQa^nEUnLa)Awtx`jFC7K#Cb)C!t`2`Gndt`z2UE;6Lm&AxE+G0ma2!Ka%Y0|z4%P682F1X0+1nVAR&I}9$eH@zWepP ze-FN>y_@nh3$@+b)7Y-x_oTqS=LMd=DXiwJ*1?K*8MwH4L=+2mK4CvU1GWq5y$OoV zvRzd;?9ryq)uQ^1wp?LhWh)jF1%iktHeh$*xgW;zU15)N{CC7!jCr#eiqsjYE$ihN z{{Xf+?<)&G>$a(qQ3OoDiB9pn&*-|^oUrl_RP?(Iu6sR_$#+M^3cbG8dJj6T%={Zi+$ttBi8ycFYzPl2ixGxX>86DQRVGHSFe<<9-Gw~APj|j zpm?ChOZ+Bd;v@cJt*Xy=j!mN{lxaV(U6joE9pBE(`1_|UMxMQ_bzMy_V=?O~ua4q_ zeTf$c!24BQRzI#LW(op-)NQq2H2$sH2k9x@y<6b-(^JQ8#8%E}*;0L5L`mB7x_|A~?)-c4T{#-VYOeVZin)Tz26(>_2Vuzy6{2XSsR{9+hOY z-lj6 zoX&{GhN9D2jp}!--gYV~z#+3V-2_eqFvs&So_yIu>a9AZYKl{Xl{|P^=k3Nxy%MFg zCnZ=@;P;t6I)Cy5%y(PF4>0^*(fSj~3>KZ8M}gM5RAOF~3lM{~PWeaNR5*2&l&lCw zVh8s#J6rv(`qX#B#rF?mJb>|Yv;IW+P1=hW`*Ekcp>rz;vlZ(f+RV6OlU}klED5X| z0q&BRA>4TrPSkk=^o;sRcI&xl@DBE-^4H64K|#3ddXFPg!y3j;#HC%nH+|^pMEtA!nrrn)s+#LQ$@7}ba3wCqa2E3SFpcv-enasqe^#>hv z=}kJl3bLx!$}Mpgu=DEI?*4{#yP8v$+B~yz`e(NN@bcQGH|rJU=XLa+x$(mT+-zkX zMWXTweTJ45Srw&~@atb78er@E{@8*f%4`uv#~b?O&1!D%+&c``fw0B`wT27WqmbxT-Ak(0=P^V&$DHmK+J6uVz<>Ts?=~(TfINtwo?%* zpG@-}ONk~CYA;N9rR5&2?)~2!>2?D%+J7anr(xQ>`;&rVbZ2k0+J&I~MBI=^+h0i- zm)kO|h%lG5gbrw!RLZ^#cLI|q^N-lXui#9F$ORCXM)absI& zaB!+wF8AhvfUrAtY7ez|br_H;gd9Vsmo@D%C9ZENalQ2H*K>P`*^M#U4H>9BfTIdmdbtiz?nCrv+YbII7Y8xRK2+zw@fF+3-$?*ZUvl)8(s+Iz}B zEwNZV8R2C-)-@WgbnZo!@;ctFtctPqEu~fpAyfCac`j3;GqmI!e;O(y|RfoOq>x?x;kUG^)q8S1G^Y+yV4r_CuW#}?LYCBt0`VdLtf_73Ehwq zGHl<^WcXX(?-ftB{nh$$>g{WU*1DAN8+KSha6UW0`H4JRUx}VM7O! zguY4+N+DjjpIjeK?=bQ{Uv~Q|rM1UsHD`Quz zroOqkWr+^nXiSxn!r^ZyprLsQGT7RqA`u<9!l6?iGdt|#>eclG@*~5a9`bn~8#Jy1 zJ>G;}$7uIt^#zmrWaMt2uDRebCOhgJy0(*=OEHBBrLBSji0c|V$>$~u=qLP|Ge#EcF4F>4;> zZfSQtEcn~lzUWY?gYIXQ`U@|B?CuK}Pd|#GsWm6_*~RBL6YS=7ay@>wTUnBsTB=Ek zq$?j>0m*IDJ=yFRlf94Z{%bwsCvg0+^54jg@arUGtn0icqx%g%r?JFp;>n{ni8YfI z#8C|pmTnXI3<$?vf#R>3o-X+bs5`5!`ww$Sc=h!P);N{av4qxHSC%_8`u_m^O+`i6 z6vk*bI-;S zju_O5GDWGZlT|XFU_hpyw~wx8@sR0P(BHnDuI)dOo!{+_x5?Ji`u_l4;wqy~%U1$RDA9)gQCH?$UYe5zZ-3;w8j+g71nk3VY73H0G;=lp1oWBGdm@o`YwGib?kY&uCR3V zc4Jgz^&)hoT!`MBWs8(6)lLS2VoDoe*pFjGBaY4)o#qRqA4or?CA}$|(Vk%WLsqc& zWtQKY)0!pX45_0zw3rrcn8XV$LfAZDqjbQG#K!$`=!>g~RI6_}%5>w|%Rj5`dG(r9 zbC~kco4-mA9XyiJ9fi{g@?jcl8<=VW)*lF*FfTh5$T=x`k0HyA=1iQk*6}$Ah0gx~ z5fQ21t+&?uBZtfCU#EY7db-`t=D_~|1>BD1;|N4>Mq#@#SZ2t_w;5sO|7jHEt4si8A%VjVkQAF_aZTD?=dmfZJOCp&Rn|{6lsXD zk4TRYV1WpSI3ilYct*lMluq;h)Wn2Uw?YUo-4{iJLZV`wsIFw$sr(Fu5#>Qz*)6y6VXdYX+NTZZR`HvL>}7 zu}nlne`fMuGaLRrNCSGEdhE#D$g%LRu&66>QbX*)2NPR(lp#OZ-(W`2TqAg%g10a^ z8ddT$UZxNR%SA59>?XA~1d34A7qL=v1|Fe_XEsO7M?h0sCYwbI7G>n}Bt-V09-uBj z44X#Hy@IlsUjG1|^E{@1d&@T>+ybp5CxOG!t1Pxxg0kUGXx8~sJ?vn}PS70qa+wK_ z(8orIYNuZG9u%>aqTu7w^trPXmLVr8dOd3eqIjsGQ@ldhxczMrZz&3=Svd+=#bOx3 zuCi5ragMM=O(!MO5Wuf7Bt$$S3-gWsHs}iIXNB{8vH4b_@dNDe_bIYzS-~<6O=5h4 z3OGQ15xnvFlIXE^Pb3(u)ISktFm`LYnNXnX_(u7nq_QW zLz|Mvj-$j2D1FY^33(-F?NCaIpAyI|;$!1!;|w@upaC0Jo{ z8TNegnB-F%f15r8W;oA4eVlzJ&AXOt{iZ>7AqbJQOH~IM%$ieGEHgQf^&FWN-`No{ z`0p$TV=-376c>_CMw)@~kG>ymT_+Ypec$YX5a9$+sEL*nb}65TkBGHtL5QWOFnQEC z?ER&t*$-0Lz{7g=1#KT(%mBnhL_~`uN@Th24A*_CAIjsL@{wXLMOwbF&FaoDN+6-4P7=0sZ+S(3}Gxoo9C5*B87y2loW)I=kqbV z?;_?a)BZ-GTy8qfNi|Vuitz9G6jp2Dt zLD*GRRtm=RFduT|fGnF0CQ0l(c7U1ro}Q+zK(9Ksp5Zw;CeijTlJh>R?^A>sSlW3F@#Lsa1K zcArHUJx8dQ3~chn<^3wFFv1y81!Cg`xXJ!smlGfLw_NB2-iq-2b!=y+6L>1GDDO>Y zgDN3bLH`^J@LWIvmIB?YzAO8(fE4CeyI0qwl6`j`2h=O!dmKQTRhy3)0A`8Pe^ z%KNG)o|PM}t7tF6ZSWzkOx;@^`@{)v@jU>M>BQj~jXK}-&}C~|?rVvwTdFJZ8RDseB8Zup z9iTV6DT=vkEQDn2@H1eh$u5|!+(2b8w#XvGB)zA|L)QZUWBCzvH5gu1b@FbzR*G6h zj~kSUYCaNT$uJQH6G-J_2M|ybD-zp(nF6{NzV4kH%RGW{n2;+2RweaE*kVH@$%T-% zumD+-1(X?;k`j(F8_@OE%AHWE`t))*>8{$<6;cI)P#hG#A+!hxe{87#04a$r{x*(^ zW+L{WLh^Ph4pGRw*B#Z3iE>nBCS-1r6>i>J665FNdEb77en~}-$=A7ttek`HMf8N# z^{TXlGGKt^g$HQh+Azr1Y)$7X<(1#0yD&~;EG3xn=hyoE zbj5cy4J>8KtFuiATbvdyR=7u78()wVp$6fP&URPz2<^yspWnjoc6%mY^&VED&buPQ zYMw+#*G>bA3cwk}z_Vr<0?Ijr#LQ33b@l{qzPd41F{JkcVwJk|EEAAK5^J>&Sqj6D z1352lMnw}l?;QZ{%~e>dnZZ_h1Jw09Ab?;d!@!r#5+N#wvxqFGkgy z78yuIOiky1T=A@h39t!k^vJRZNAj46iHVt=6^jxymySBk8Z}L+YY;E3d+~5ggaCX= z7W)wt6zvoL029>qpHT$r`kM`r(=2^k3teT_Gi87Y^_^%VhWfXL35V+r&X0kX|o!* zuuG~z3R0@0oE3tDYJ|ojA|)nK^E1-4W9?kAT-k`y+1iXHxfPC|1~dNvD#}~l9>z;y zNfTiFM2E-D{Zo2{Govymi>HB6So>!t-J`0ROMOvbz(a+=fjN-_C}B8cr{V&701J5K z(X(q2l4bmz%ymaCiME8}tEK9ZVu1@_^WGlNh=xIg0m#Ju;w~#FbBYr@7gkVUyNL6F_o& zCy2TX*itPa2qB+bPgM``)k1NY2GCGFh~bDUCl&+Ja8m^l_zQ|mrWjk9Z!x^YM^{qv z{;QU9RchL+dZmbU7K}^nD#IHqg^D9-GbrN_b2|s5rZ)NL3s)zSikRpvOO?JCn|`~6 z%(iebjgMm53^Tg$CDhl_)N9Lax_k#YY3!a4$D$7Ev7tS0nQ`xh9iY!q8{ zBQ`@u4YF&Mf}&6V00@p}!+887rT0-YNlnV+GDyXj9Mt1$-dsfrya|&p6a?(X!eP81ig*NUE!+NMpe4u>n`3N~BKny&ifLnAR4loo z&^Qo5Ghh;7#6*V^0Z|lB<0K1 zLbTl~=wiW~NMfs?xqZ6VSjfZIJf_(M%)~@M9S5yDOM}&T`-oNp_#0Lk47yjGiVjGf zgY-&SD}Y4$;d}}sgcP6qlIswtXzuiQ-P|U32gfaUsdRRud5AK{^$29Pu-KpEYpx*< z2o+2V0CwI;w%m$mXSVIO!YC;K>Od90m9FHiigs-WR5?`cGJ1}a{Dlxi-s}Tf{5WCZ)Br@Nk=V|TSn9W0R45CTK@pY4(#@|ea+2y zU*273*+JD+imsx@U~ zyPYr?wB%SUYSPXdG5{Er#1zTK#S^)HqvB@r>=)Em$$tm(G&DzX^O>&uc=DDIO>5Ft za=4)rkXWtX{{RpNjK>yy{{H|`w_7L1A7HY(9lxsd9&<>i0wYZ zDKP=B&P*Cif*2$CiIFdljpudWPp=X?kK#XhJjU)vd^;_O*4|b2qq0={kK0`rZqnLU zS#R(7+DR@X2-aBjSCAi-lQxZ|olB#8nc}aWy{E_L^lq@zSxX7@Lkg|3R!)VIk(k_7 zBGols$oP-MVq#LU&OuDH`$Wst+OhQCf& z5vZ{xD?`OiV7(_KVtc~)#p-smySJ4Za>S*^eZ3CW^|nn)Ybf%=t^TK;cXxld+1~u? zuQB|9(f!M1nOT($Q*#}UzzochRSZgu239aInO6_WV5VYub?kdx>fY#ing0OC@~@IU zYG{mSWxMaEFk!Crc54*a#@xo;sb~Wmk*gyD$}h7pd#Q3WFfl**^>z1)R{c6OXKk(h zLG}Ltf?rP$Dy_dS>Y<#_?y9WbrOA_Lb36`8n9X}(GrNKBdq;7YVGjWvHvK}rsJ-#? zn-7ldz0DDa@&5qFL|UAPX?hl!X6~hJbf2nnb{GT7wRw=3sIE&R5ZY&qK+|7N+*Yik zwbI)p^ZKRi%VwdjTU3fvpa`}e1~+7`Y82dwXEFk^JT zs=bJc(6>y&s!Q!L(;=}NB4oGXGZ|U*weTOaxC}>+-cxxYIL%n?Tary))b^hrV6Rrx z7ly=%WilH{e?m{%att3-L~lPc0$RZE)4^{3dE>10E|%>DO);l5#vA)JMy%9~YUyDP ze>mPEWaaV^21^);4ttUQQxUq47JityUeEU{QELAH0RE~ygtO#k9}jCEnhJVf9hhD@ zF4{;Aq+4^7xhglASNOme@2h67)Rwl=aITuhIf`;$nOLJ|Qf;-^+=rQ-17SS&?+0$N zz4FyHw6|*X9TjAZ^rhp)YRtZEkX$GEWXwMwjaLa|0*WPvGrUZ3p0RJgH}l=L?pAM5 z`5WTSu<{4O8Y4k^TfJ0HGTMU>E34{tZB>m#4cqvSa4_QW^Oea-ltghT55iw2JD-8_ zI>&$>XJ}0y+dk-{O8)@W3Dk+y(DZlC&)j-K#cL-%#;jJPuhP=wzf7(I075#0UiW7+ z;fAJvpPy8oIJiHL zVF4=F%rZT*5gcM-VkSts@+dx2Y8?SbR``$Q{{U}$JLOk^v@-d89UVOOsC8bN#rru^ zEIpcM#TG$acSvv^97ZNL`0Bj_uY9iZTf2D<%=qnZxJ+)f)Ry%qXgxccu$?QTRemSh zRAeGbhB;#s4F$*p8xP!Pd70;z{v~%$>GRwjFWSEG-qCIxj>#rQ{>b+rCBa5R{HDPKfqqWMm7Y9zGfQj!{r4hq_^o57H7=DcMW^)b zjD1FmHBFLPOR>$WE3IC#f;VZKL-bP}YY*wQ+OH-x-;q=)_ygR{UD!Qa6xVxF>PlbV zrfB15kjpLN+DA#=(g}fF2QF)a+9o1;-6w4OZR5vv{-@0BJA8rfhkx;UClOayWU=^s zYAE1o>hxL?WajUzL++W%2EtP_JSB$%KaTQC9gSz4aCx5c8?)Y1Wby~z?(cUGU19sh ztkE~AcN@9by9j=F@q(`GWp@GBrBPwIs&>T>$K9o#HVEGHDjSLM|VHQsl4E$7^~8_WbzNnU1Xgz+TA&a%J&|W zCo8EnP5Xsk7obGuVNh1Uh~nP>8G%7Tj{E#X?>l=(&?~r&$49Tre$r&Sgkt98aN(t% z)SH_;q~gHZ_L&yYMA;5gJIwF5R_9@JUG2bs@!Rf}XX(*m{hutt-g)`I;Na&ePb68g zP2XHZK@HHyF07%EJfLDECL$xR68dvj_m@)lx-;FHj^Wz#3iZ@~5bZv^ja^MSf|RTp z_9&p{+>mbWb1N@o6z)WO{Qm$@)UN7QsY(@gsQ&;C79Xomx-I%HhZ^*qNjx>{$-U1q zd@S#`i5jk^k?)=d*M`X0A;)H@SvKSH(-2V$_R4or%&&e2GEuP3{{S)7-`YQ^SJZR0 zb#WcN?SGYB>(r58QHsT3FJkIyi?YH25L#Pqvkam>2jrpvMmC7wt^0jX8LqOwNUqgs z&i!dEMXY?z%GT02x;WYZg1p^q`pWyEkb(7r;vl7^5wiV)T(_RC@1$SSQyb$0Yxxh~ ze1dfskn@d%20EAoM$}YLva64XIl7E;O?)zUsw^h2i%_MW6Oc*=zDg4GX(<8nRkqFZH1gb z0mKZ%@}84@Z~nS@jbWSA-U59-wT-E|1pHTWJDq@%SmMG?d1ElFndc4SW9+!H{N=OR)^L>c#79wp?qNnGpyV5~p!8X7dv<)vlZ%pVF{K zlX&OY99E6RW12aQIV;9Xjd$tWi1iGMjFM9j-(*bsSWrb1E#*9ZAg4A|>a>xND$RCA z$jRF)sX2F{iwN~g26#vjPqbj92qHiJa~&_5>#j?yl&6VpDr~Xt#bn1p%5b9F&_Qs=YzmN8Uql%5}Ann3yf{B zKz@g4pqh+^YNbSAEZS|jRj^_!B!`4cWMI$%5*2s*n3-|UGYH&&2%dni608im;$2WF zE2@@w6+kU2r&sBMVC;ORZolV*b&WV;&R7h(jvGgs6nYr@Y7K8x(#K zJqvNgX45gL3Raq=zaI;#+bv<7t|VUJA^f+HIT$D@9p-oZx&X{>BNdRO>~=!UT-t%= z54s9nrwDB(9@$`|y>T%k!UUk;VkTw=AXsBlmOGfoR80$|Rc3Jv0Tw#J2uO&4Hbi1Y zqy1ZPh>`yQ-%?((YR~<(U*$3O3gm!7n#VpKsVN>SFG;{mGDZw{AA%U4iHKZP{Wy%0 zOAcd=cu2H~LtVJ&7Qs*b#IU%@6zwqsCA198?Y6)`>?<*BsZAW~oIB0pU3qJ_&BGg$F$YVU33yz%wiGIFr?`rHJn&}uW2l9jQOF%uI%<BlbnMPG47EJdozhCNVCwP8T8_WIWa_z(D)#{U4ggH#!7IJn2-X;S_-C14Ve zWkNOD0+ug&;zzWP=O@L&K@t#A8}HBp->Z8fG*M>Vxv(ix+m0Ru>U2Rw#}(}Z8!QTD zKPjKz>N+m97|B?}hFLVSxUgzlQ~4Zgms%oKAi?=59y=^{f{%&Zzu!aDxrATwI7+!9 z%zdoHVdA>d&w4?z&T1r95^i4NWKsA?u%bR-`Zj4|ot|Zb!qyzA6{E_**{=aq$jfIO z$qNZ)ArS-f#y=SNjk*F1XymbJWlUZZU7Dcejd;wAEhsIcVB7|I&mI%Xes&>`(8pZr z)5?YxBO4cOVko=q_l4fb)l@)9oR6d0L`0pYA`5-Df{ni(xzG_AshAX@xrn2RzSLBy zTCMud8Zj?c!RnD=OwQy5%l$-0`swc+<;T-q%yxFB8m*;6BBMRTjf?^&!`KTEX+#*$ z0hxsmM=hslS9>Oj#7(KSQRuut%1uT&s;6aw(w3@<_uf>q`;JSPfin@Er)Zvv^-){t zwpmjLeXk10EQ6-2PP~3VDNro`0K9@%Jf>sjWsS#G5UsZL$dt&;M?jsXRm{;V*;0&)19idOrCaUC*oY%rW(sS}r04$L zCOvZo5%C>S_OH6ic&ivVaa=F#$L6>So}?b|wR;vyH<3kn4j~2-Coo@@(BSSVnNPRN zMfy2lY%$g{aIsvnN(q2w`B_XL%;SUs2?-9xJM16qHo~K1gB#wg?9!$mZo^BXLhfa%ohx7B|i{54%-h(7mx;5yy&dBeJBP! zbX^Q$9%4<7JBQn(VhoFq{{UnWvSxND=n3lGx_`&7AC|#Vx;3iSuY|?}S!&wYKEvYW zNd$yTB;aNsMh|F+i0P_o>I*0u6#?s%Fy`obp0-ywn1p|46^kQzX>o`siH-N(XJ!>L zSnQD5#$w$3eAu@6Qt|6eBZP$UU048~4g%UnV77x85t!Ixr7C_t4cVRScvrk0M zn-)|+da?&ar+Aj|koh3?gBcO>sV|s_35fWOx;pytwttcgmNVGkw51h#+-N5g24G^@ zYZzv601+OTiGh!p{{SiI1(>D@p3=cL6j%zaX17)HfD$TklZ91w{vBaF)DefloP+-6;d)cO1~vTEmD z(BP^w&7u(19dU67y!IiOnTcM)$%*C>wZu)%s~Kv|dl>cPUQ7dn$qi)FS#fU^u%EW( z1bak>D1#OR!I95EDjL{;4CO4AqNXIqrp1Y*145|*%!$^te%+RQqPzosV1GFH`RYd0 z%LZb_x_JziM^@4}Haw`o*aI1YI|*>UBi6Hn2!8Ml5i#Sqmi)?M{{W9Lt#>7L&%@MN z4nHAyE1QBg+hO%Hm5GQr*`!QlC|xr?*yv}{!>u;2_%_9Qsu>1Anu((@N9q;~`s4Oc zWAaR^lFQGvnOmtdfZAdF`BUR z36VHoqDnbH?IJsVJpn3HV5w1XTO?!ZrPo6pZB>_F8scFHL?%qsa03iKfZU1EAL=>< zl~qbdA!8HwxLRhm*0yHQ{zMIf3=)6fsAk7n2jg(yaT6Qu3~_jZ(hN(nR`M-M)%sA0!)p7MsGWj%w+Ko$y#dcS27CgoPk4HNQg`mWR{WOD`zN(;rNVg zyp@Z?SEWYY7dndTn@U^Cg>g#M*d2)6)3B%kl9mid2OAt>EA+%fk+d5*n%KN*DP%F1 zZCg`=?~0*2)};UevJ$m{9IRv(AaB@U;(dDt@*YmrN_B!AW{w>^>)K!OhRcNbM`@;ndR(>ncJWxWnZ{s>j^5=Dl({IRyID?*^7Xb1exH~I3`&JCmXEf#5P#2 zI)@!ubgE=d%TbvI60EZP=23OnNHHTP63lr*0$IieW=x5!g818@vKYpjB-E`jj$%C- zI@fWS=zP(tqAlE*_rsSmMIc_Nh?!sOefpZ|qa?d^arkh0Ez)a()YW2BY@jwGcyKA0 zI9Nv$nJM*-{{Rp@0V*-JD~2Mn@YV6ePn|tOCH5pBtId>+YAN0c&SMk;*<5~y zjJ$?_&SQ)~1lY%3aWf5GELOUTj8(8e`Yl1r58O?Az`$8BYbXhsFP*>Rs+(6oK*o)f zcpk)(t3dqEV99>w%5hJpavgTBaz}G`0XD* z@BqkhWMOx3FQJ~ShF7$cML4k10GSp+dq|`Yft14nVrP!g{yh+;Y5N0im8*4%kz7qQ zuxm13WoF(YMlR(I-aTSqU+4yB{d5L)xAK-Q(;HIk)OxKzQ+n8VxgW=CHa3lv22ry< zeq3V_@zbhB_kN4CsZO8DT!!Cmd_Ax(HVDRL19a3t-cY4Xm&C&3gnS0w02Vo|5OLkZ zok?YYwUtFw*r-jl1y~5@VJ=QM05fFIuZa%P%aq9$i!wOMWr#XyyIqqDt@ou0tGUO^Y4gxOuXyzL=23d*JylOh3{@FriJ2LAwz z4*Wo31; zS+kD(KBOf1F---W@CB4DQ#0%524yopF}F@@4%1N1; zCi>yi$%}l=f+-08xQN^*uHyb=DA*je?1om! z9pYwL+(tf6BaLMR*j8$ePuXb2!8( zi+R~g0f3o3_LwRE0L!9+d@+-L-bHv_qcz4h-*Pl7@wK%LlUog`Qub@5Vp7($dDos` zODs{H@S^jPK8PbjOX_CoArA^7q69^$LS%8dPi{g&Z^VRAwo@Xt3 z*V%t1JV5%8=^U0n8aRB8t!Ta33_e`Kz3g+K2b5SuL=(~S!3GZTAD+=0PhAzxAMGU4 z_3jco>%5(e@^YWi2fW@Rc!ghC=|3ntY1}{@jFd*!IQZYic+fBcQL9)!rBgI%_> zsWk0Q_R`H{GZ`C7h$|ReO^QH=zCdGi!)%POBjR}?OZO2yc{kSGkn#sJuJ0=bofoh4 zfU@AHSmbjBa{MW5WDCmOlO`c55L^D)akDoVo&Gk+{^IIQU8=Qyt?wsub-t|D`rjsV zV>O-1wq1Jct!1*wr7eOGhBSjVDo+!RV*k36CZ9x zYbZ84$Fp?AXlKT*-F+FpsH@gm1H{tu8^qrGVyoA@SiHtg^A6yH;%i)HChMGxrDZZC ziVR4Dnaqqt?civ@)Bd-Tow@I}iKVQ5s!3z??M~_DYoQucbdXo)Qtl$LLWOIF zlrX9#GPXZ*Y?l}q_G!A^E17NCDsuiAWBNRrUTJk`tfrsjUj9ILD*ph{$Glo=CFAdU zG=-lawZ>d^4|6*cjHo4|aP*i2Xo_YDn-$ow0Rc8P4EFXwEEogU%RF=QqsJdTyFcDu z(|Ez$oip_I^M_gEM&4nI!c+cQAM2Pbnwi8#;LLmA9@+N^!$06=a4WD%`c!JJ;rPke zp3QcDM15BLnC@lOPkMFcQz36XYfjhub{*YAe$!HDtrF=4Kp>UMVnQNhId!RzsppCQ zKlr)lPQL8FjCbqdG>%Sm?WU}*71e0lS462kf^9NMp1)YC&cs`+ta1GS^)ptiV4SDf zA~`q0$0p*>x?Rc89z0k2WBnlWzC!se+$}kgWlrLEov%5ejIv_|X1#o!!X;O*e#qD= z{wacj8QjWeQ+>|*PklnWL8okL9RC1~%*^R(aU}86+4?zKHJ33wpw^>XSbI$JC>e?0 z_dhWk5&FXYLA#IidedGteLs8`?S2!qx@zW@n@QKs#RGoQ65l~8fDn=kAj4U?jcCu} z^-uo*%LKlV{{URysFtYncgIf@`{}Ohc{$syeU>ac?Pal*)0x1O*ESoKkfxO_%U1y+ zh76bX6mr@JZ=YQ2+Jy5`+Hu<+e+T!;Yo(#Z(fIROUYRjl#Qy*`yp8a~xLJKk*v(1* z08PHrlUq`0{QViZLnKO`EBm^zP}JE_E(RR~G9kcVwvD54I#=tb>AW&+$shW3u-D0Po3$vWb-{x7`Y(a55=x3Eywn%=Oh@U2hz*SkE;(5T76PQ}Vsh z?lzsY>)1R+VFR&gE%k-QrE$SZWEk=|h=P$RpP8Pw1@z!S)V0>Y-ti6%5=IYDvx>jx82A*7@MIVee8;2pDUh|U}ZLuij5@QoPO!Z)W zXTG`FuKeV49t{0AbxqAN+4|0YGgfy`5ORiR-z|c594iN7Qe$~p*cpjzZZo)yZ>m1T zc=?U(eiKaQ>td>v^t7v12VCb$*qM_CFD@h5xEo$uFKIY2iTIAJR4u8~jw$eu4CdtW z#2z_#{{W}^OQ*3J8o4{ob)z*!Qmh|rWCUE+<25m+KGn8jS1?6C`iP#hd5229ia8sq z1OQVwOA63bWrwsxfzk|I6p)J?u`}GDca8R)y1TU9JXQ-WhJ9@}Y*9`7MSMnRRb+yG z8yoSHuN;mznQq<;fSKb15oP6W@JUa#$gcr8UN5^{zPB1@Mq z%l$`VQoBOCc%qyh3eD1MY`Fg2h_h8AB)QzdO7#)JL7v4DQOke#9a}f-Q9`PLGTp3m zh=RX5szyCbhDibaqEK9jN^GCX_9Bp(FUB@$k%rP`EQI31trAJidrx!{YpnPg&!hhU z@*IDk2qK<4jKoIqJspb%)2&I^fU}UbkNd0D-+`ut*-Qz5`@A|@tiW;V~*Vydqha;UaL zFEUT{%M4c@)z7J$gH#Z<3t)y1-9H<_Op}ktQODTD)om(N$ZRn+>~RO$M#RE%IPL3! zbcr6}v`Cys`s$iaeABSJStAnnp>Tpipqh2LM&8Pzx!@`vkvN~2J~1DUKpT#2e6q0U zj71t$&K}^aVd=CnUO>63O0}P2$&nC$oNpyFzWqY_$+I+Q<4*QN8j6c{D!($82*}Jf zYy<%?mjlEDkxYz(f>)5H4b{jBL>`SZJ7l@#kQgJPx$pHfZaSNDf z%Q%$Ch{RWq!O_cDtb@w0GK4B^RvWFX>cS4>C95R9fGW`1YcKl9Ss@=j7{Td$Bf zB(lwV{MAg{!&6fftBsdH6p4tme-0`b+-4?cU>H<%TAVq*8;yMyE=t8FnG@`|oC}4t zAbs=g3EmeBjiBd$gPqd2DZnccZy=>=O7>T1s9K=ivCsKXylH!8RA4OuDCZu*N~UA+ zj+rrJ_kFW2>H1gZ!N7g7#fI`AZ1E{3h{bt?tcpNnOK6?vXxrqP7U~#NB(KnIpiAlG93T6uC*Sc(d5ibl0U z?(!I15Fs=a*ivh<6huD6i=pu`1W8m8NXbCVPbuCa2{F`JGb?!GxA!S8yN}^(2iTi^ zw#@!N5o=6V5ry~Wu3xJx*D1z2jC9c7fRizZ+epl_{d0+4j`s#Cme z2S5Fh&^K?_!d1i_T-G~IUwQ%oE_f}*md9H6pOix zx+=YP1`m-WSjc3Il_tP(Os-`@L_;zA+J1VGIn-Dh*08oRD?1x4Sp}Tc3ycX9D)Ppl z0?l{{^}$4OnGzWP03LvOi`1I;0aj6ojXyTMokzQM7*d z=Usf%S;+$L(Yso$R~yql(PM zj&$OkD(;bQ>5ysyHY8#f%i0Dz@|ffJ%*yID6hi6y{DI*n;pE1>G(BUHAeoYhX%{6x z@4o*4zn+N@$mEvHc|QGGWm&b=bMA=S__8EyjL400GM{fFU`*^&^VL={HFG&+j!LdL z-0Ax&mj32pPJx4%Dy{G}jGJn|rhmDT(U3`srK2cgv`>TEL+n{X-$2n zhzY7D4RQ>f#HB|c_|HImw|T<$U%!5vF*3ll54k+qW$ICg?#YNS5-AqIkj%uKmnr4( za^%A8T6tQRlo*S*g48u&mM$6uK?WYj4DJ&H6Ff&^{e0~`F2)Njg)-80oMzAzT(^z5 zpL4)h0>|1XD3J(vklW@bpa`m}xh#kAcd_;@IA0H;NUJg|<@rpE!oBF?y7MtSihrskN@6)lOwVv> z{mu;~PO1*UMANRudn%lAG@Qc%RRSPNA(;_@D39Fqw3#r-R}|#<02R7eR;*}+V=qz1 zrXpPfEms&OGoSSw%yE&KF~N|>uP_%Mda)@FMndg&n4EEtkpMuMSD*5j^+@l>iBG}D zKrb9^x71B*)1*o($x^u#Di%JSx+5?HR%9$eEpSAFQviR=TM_!`n|aK2l65>!-C4)F zz#X>{*$Y_~H=3cs3uqXC&dFP5XNddeCOxOQHRj8$T^h9lu1fKtbwbj?H7dnvmO!~d zvp+v<>-1oy5*xx_1d;s;XY3u8h+1!;!Lu zEAA$zy|0g1HHMWXv#|*aBWR8!ZQpI?dTm)1^0(b*P7<<|YN{g5!@f2~W7+U?=@JGu zOPov${6^m&JIefv>g4FPwvyavos+gob+=c5um>W)vi2aC?3sv}gPswv?GZc7fDJ2H z%wgk^{6)MaD{wY#RjJK3L`W3RU73LyLkcV-^}LLWBY&Cd=Uemt0DE<<)EIipaM-MV z+RQq)q)IkwSp&L(UvAilf{t=UGaDB3U2Lha>4VX6((~hVY|CrccgGVkWo2z)#B)EG z5X_LwZSlO0)?|%oUQV?@%GDyJLP(5Q5O6>cQYj^ZrhgJ9m)u7p|RDA9bwUEqmj;OGAhT{V!jB2P2nvbwF6Kc(Sme&vm%^Q562+U{KBkkLX zM8L;NSC6k-9dh`!83+TbalXWuijFHt%oR|EAF#NDeY*-oKovY=@z^~WMT_lfDr=V)c7)o5;1Xjr#kKFxPgFJTEu5yf~F0701?6gFl*5BcicsYZ)v z4m!f#c~#X4)QtrNE8Xm2#LQTg9864%ltg}{cGzQ}4WiKdoK^D3Co3$qwP)HNZJQD^ zGD~w}83`uI?5s5STE!lWWKQETx8y&N?Z!R^BcKIvsN!*Ny5~}`YCp5NRaGuP7_WA+B*n%n zXsRH$>AQ5thy%+CJaJ_Oqdd$Loo*v`0p|E6DC|; z5@jrwO}cL82iYrhrroO_bt^Jw5%x^Xkuzx`9fo8F{f|}qPTpd+MYQYW9l9K*S+5H; zsG?*}yF>Ff48tIXXY(0|o#HwIEQ2Q=%u_OFT3L~@Su-zK5K}VP;mU%ETLFAvrVM#= zmGAx|ut3LE%h#^T8N$Z8UUcXa4VQocGQ)W}vnq=P&1Jbq02Z^3(nq;w+xV0^z*5tC zW`w!l$dW{9%#%oDhz*f4Wh)BuNsqu{0(ReisJWA`L3ukvtu;oO$H6S%onb`&5>Q^# zW8jw0@nXig#Hqh^9}yul&j+q2mQH1u|pKP8bq0KM@1f%}!wzSB(lSt&p#6 zY`8fpOqj)0h7rh+tO=5EN5qN?Zej5e0zCw!h2$MP9Yw_jOIyiucGs~nl4LM&Bn2fB zL{k!Tzs%3Zz%P_!@zrI3$i)^se(stkf|pSiCyc~^zQoKe8@^27KRIk|yu|cF*F~p| z94>E5Jgr&uuEQN-!Yvl!atdPCGsZp8M6;c_6#oG0uZUDy$mFX#5;st5MB29$1)NpD zC1fioB6l+q9f~F*SHJE$`IE`I^`thIPcNq|%Zb94Dekvyf?%4v3vxGDm*M_Dhs1xl zo`9y!+LrF*u`y{4mrH9TtXEQ`y(cj(dgPa3t1^RFuZb=paWV5H*TAPl(h3=1ZnjNT z;X(3AE-)o7T830gDV@Dc+`kW>jy-%lhDA>PNYRW8n*wZA7r4~_0CAqsD>>qDL(7!R zM;U_{-gcOU;rt#Um9sW4FCAXFGQHLirinevmQ0k{X#+{T*&D!&jEIE6Oz#i}S?bQ* zQO(qYO=HJ}po%qaBkcxnttJ+U;YZwRShSoxIdJy*J#I zzwYQ|G?_(zv_vE&CTxZroNp#AToeTUq>ns?{7%tX%lWP&Bbt64E+XX_l@FbezOSk!HIIPZ||}GIe3m;sPFG#Jb|;4&>myz%5~S< zjc=%67o{;b(`(yP6;{Zz{@TrlEJgMmuoA4QBjsfi+}~8sr*C+^t$obUo=JG+s`Q4K z@{dJn-~Rwk`;pkaTbmjeFu4@I!)_Kf%o{5d8Cm5Hu28osP;)$tqpMTKUod=U^S=V# z-U!g8+@q>7o6(hEgEGNr%3xZl2`G|O&*EHX^*v=a0DFPsMk@h@M{YD1m;N=lgLY+`hMyv|bYJ53J5175PMu!cZJ1i6evNfB-O>FmT@zqpY+#Hq-g)@@`O1 z3kPfMTCKzSyq_G`=z;L~1PRBCKqk1XC-5p9p+q_Uk< z6inAdDMBZ>i@nT`$%Q>=qs<)+<0hKcI!FHi>7&bCdFCa(P>UH2ZJgA&mQcwjGwx9f zB-)gTJ+R4`TLg$Wu6fS!1G5h&ezN{IeJ*>stGqS(eE4(P{Zp#6PDMD~ajtbUzRWIP z5|4TziAg%cVj(-WS;i!`+i~7feM<<^*{F3K{J7RCTE z#Rz(@q(s|ZOk-s-WT1j)^&`|RPO+^+E#gD6(j2TZS@tSNFjXSLHRppuLX@LOtG(n+lcQ~nV*P!58AUp0+GuM82yWd}< zw}`#Ps2@wOr~d$dyR+rSqpr*zCQliVE5a;-f`?@C*BLZX{h!+8K$!$_l9-5y(1jk$ z_Y*7LyPi3A^8o(g_f1-!-j>DFsm&?9W-JnA%=y`A7zraO`1qOq%=PVi>7n%B?Pt^X zNB0A{TJKn5H7>lhg8OY-tgJ-R$6~80Il83=$n3i6Ic$@6f^3w*ZvZ__QPAx!Qmqfd z`Qs^b>_*D!;*xZnvlF;G1LOT`H!XQ(q+&z)xLFH&G-l4B<8W6GJhhRMyuDAEEP*y;D*y6>d=rO zn8p_vf@JOO-GebBw)Rifm+Moh{PD|bZw!1X&Dhd@6k;w#9Z{(@~OK~HEBSeP$B?74b03qaY z6C3T-f|WXS(uFAD5z}r@a|ZGEu{e(3cK-kj?1Wc^R<5?+VI~$e*8~wNO`>!}NfQ=4 zg>q0JUzXDm8J&{5POFn`x`A~HN*IR4xq4wsIRa|q{6rRGw)=d+f5i1pDx~49SimH4 zI9irW>Z0K=))=BG;~t~MebGaK!+b#Pd% zgOJB#2P=>qy^FQdHzGs9slKIYtCx_V!M#qi zGDel%WTqe;03i}0q9BQ{B_T1#{{VdpT4>{Pw35u`MQ?hUCPYA#+Er$Py|xwXBa(j+ zV3bG6PyEEsKuLcyV&AmqC2X2&WJ?)QBs3#!;6EU0rVogTn)V_fnV9&A>y=mc_7TdX zg;475Y}hTUsm3dxWIDkCGyF^N7~vbv-!fegRS7K9tglu70L2bISzK?JP_0osBiO`= zz%lO`l9-vDx0&1Le!1$x-_2swXNSwyU8Jvm04|kG43&hbWYYIULWT=?kI23^l9>Pt zmulpRAJ4H@8(|El+lu=njmNPmJ>Y=%G2<(US$u9YKkurtRhM$kMKXs<1nXY`u~Y2j zOBKq077?5roXcRsCIE(GarvF)HXa`30kNM1U1IB#?6Tz4NvG5@bzVZXilj$}5)qSv zlH9hKh#KW!cdFxVH|&_pb4nbz8o5K7<~+?pJc(<9SwzV|F^ZDo$|ht3aqboc+BnOo zv4kZGYOsb{%x4wCXMhIF zF*IsW)5_$F1y;+wax3Kt2W;^5@L>tZv2g{+Oa3FE0sH#dhpF}S##29eMO6f+RGG1Q zw(2&GH!fkW^?zA189Yp8V`<|(MU(rIbmEnSvKGo0WX9$_c43VbEluJf{jh8_nf_(- z1Tp@)WC^)J)iQZJ1@Nwqv{hE!B6U?Yi{eCXlY)9$MsWu-xR4zDZ`3ne4N|sg0gkPZ zzSW~6Qp2)&geyu{HoC~ME)aGZL=eJKSmI>aIVOxQMqpwJL}E6b z=(B$5*umv;)iM|x)t>bfL>WclUf~DpnVo>SZxQ;9{{UO>(vcv>WW9%hZk**PtBt!{ zR!pe@F)9k{nf!J*GJt{DWTd0!XP_Qq>DbFtMVd{~*(7m@_t=(GVU))y=8>|I5{W4i z;yX!1{O`Wp=nS!9hbMe>7B@>XFYO|6GccHGnNK1njKbm=L_}q@PsvR0Gc&h30c*dL zG*>s%C*OQUs#Rtom6;H5Fw%;|wUBQHJfdO>K&E4T=3=;L7`-Qr9gH{Z;AV&wTN*Tj6v|>5HK?l0Wf@`CT0g{0cA|p3`*(( z2M~j&X*H`>fQxo4TG~V0O@aY71W(T>nVt6DcG@Y5Ox4jjk*UPi^t<)!m(JUT>G4Dni%kBJ57UY1|GrO3RNHD zJPLkx-~IG-FwgmQI<(@k5mkecg>$$DhTTpK!~VobBv@hqB+{89iIAVe z;wNqQ>5efaQi~4_sBy4nwk4OcbXPUtLO@C21S$_?mobIN6pzVso_j#{8Df(U_{?5o zCw|QkSrs&MV`d5j)ydHig$ODN4rAn?zvHN~7GkTfULQzc4e-4 z1Vqr0;6cEp$Pyy>DO7Lzj)BNxn;6S^jDmFGWbHN0yxVqA61D*>gkKP$frW^`2#jwM zJfeRtfNaY=qHKCGLWCw+T{m&8I`me_=CX2>uqyIGGLe~{At|4T-gjH4>tj;7I9z*h zwwjw?Z3zmH$t$2T!xIgOWXa|-16l#zUS$CM6NXL^cwMIQc2+yV*Nbbo1^gueB>1DGH%s zUX`(q7&G_n^DKc}&foAe(f2GxCBEO;QcYX)F0uCyR1M0(0AW_M_7@XeG842%A>F^1 zHu9~7OJxCC|+X_5Th3@ z5o}yV0!JLJj1&+-7?d1N@>|B=jCI0JdmjOF9-Q$A!3E4XqNt~H{zDo{WDpuR5JWM&3uoNd1?z$)`{7pYbECX3w0khLI|I~II>D4*)uzQM99B+b-Iu#TCI?iU3I0 ziQZ*7@sY}qD#pw2SMC9FAm z(3vt~XWr;AXv;|b+^pig^kr2IH7ZG22ABBrumuHKf(CrJ z#_$b~nS$G|m8!5xs^W%H>MK{PWBBScv=#(J9|`v#Qi z0-%h0Yy+WFMr~MP*UTV*1b7o9!Ut~0CF}|=)5Sq#K)MHzVOUPkP^U0? ziwNOB0uwVlb|{bc&=pbCudav`($iRH7p-v2J-}Nm$;_CkRr#=xU)ds4B?r7nmhv+) z^l<M1hXslW!#5LJ!hfxnon!4R3^RfKxXckBHWU%W?Jy%B`1G&tE99(Mr(V_x zmR$PQk#eSc?d~grclE6!1R}VNr{v(Ir1y{1%ot?*(Tuf_t(CcrM9UIQY&yD|1qH;V zMLV3yZ^XVLOPB1XdIBmHt>dzI)2(L=?7T&VSBr^SJyQ!AXAfyf!7!wTLSyCxerNR) zmAq}rPR(1hsBh%H(tXuyb6LQf0TL#gaWcq{Z~p-6?GXSQe2-MVv)+%q%jw)zX93t{ zsJ07Jl3v0&_gG>70DSOT!~$Y?L6*}Tr>Nd%hGL?nRB5WMtPeg3HX7%T=k^Fd7;_>B z5_A~M$V5oT2$=x&U&jtl7mvo-vt2+{+rpe*3nOkqOcWzrCx3h^n;6sOJijlEx(2Fo zkjl9erpp$IzPlu!Z?!KEn-e6d7751N5RAmkOiax3>6;NQCehY9 z;B_Em8(=0QVHpCIM)R@%0K~^YQASOR#JbAyT8Il8VM^Lzm7~+SErM1VGd`k)`~+>e zODAv2cL6pyJcb2kn()pdCdP zt2ZI#XJ(5^MY?Wlzky3|xX>XMOhL{{XIlR|k%% zjiqkoe4Equ!H{V2Q(ELjgsIVFp+wFDDiMw5Sk5iwxb4)L44xrYF7v|HE4D7IVM(y3 zQnR{ZXZj3ARw5pqghPmr2#NTZf{@b_!{zQ|ve>6aMzf10833C#38JH8BDy16?o(EH_O}@JUKleH1~OJNB?#sP9@sJ7B4cfaCP4U#$qa@X z_FEBOCy_${<|#1jnR}Q3V5ICawTQEvkp3kuWPJWzb>6Db!>MsaiyW4e#oMGMRWG?j z60}(o7*T);1ap7`4T>juk@=mXBeBwl;g0b`ar6mQmqOIk^)9tJ{-_4RUJi)@gWhH) z3zFh?{9<}4`sTw*aSx+xLQ0(jxnAYVFG@~e-fm#R+#@@+OD~0)nG8gn%z>@`;`sfP z(^}q+cLifLr8SK=GcAK{9J1-&#KzmgmvL=@Ognk_ncIG2G1kKTT>8uR3$`!D_UFxv zGBb2iwW_AQqf)$$K=~OQwRLhCksX2_jklPY+kNJKx0x(Q+}@~mF?mcSsIgwaZ!#?0=1iA7q;@};At>AR z))@Bp2jmCStICek_uEG4%s#4C11PHVW^8e*kj7e%O`6w8_6o+?WJ26V0y3iBc8;|H z+uc*-SA@CeWIGv=WnBq3Q0uyK_^k1{0yT_5wd>w%3t&cK$Pl&-CMPf?Gd`7k7W3n= z9&JtsL29a49XH=3;}0F0(79t@-CK?a2HNGye(7Zg$|R5EON__I&pOFUE;8)QWv)}f z^UV*VuYkT|ci&j~0i=AayY&3-_HxwaHTICzn7WxfY-9-p=2)p`?h{&I)B@vTp5-%W zDUMQ6)@}GX*q;x+mHn}H7s5?jK2iBQ-Mpyj>fOT6D~2B@kgr|utNoRR7h4jaQ&@7I ze%4qBCm#@ieOKx8;0SBEXY5`XMU?o zrER4x)h9N+9yb2~l3upGa;d6Pd5LLw3E)3$d=2d$W550T%xN6|0FfEG*0pB4?iP6j zjD8V}{^wP!v!80v63Ll8_i+@#Z?|4zey`tI-s1g9vUv{_{6NWSJZFW~vIScyomje*S4O`Adx6I&K0I|XTp=0ImL`Nr7$+78Cx^opul3~h#0N414z4Pg*3fRCnm0WFO- zEpzkowE#?Q@zt80I&g}OHH3(%RBiHFor>}APWIOqj=oszg@qRX0LNwj0FG^H*%e98 z6a0k8f&^j*hZ&JvjQ%klWwtJ&FwrmLnWGjH$i!);&1Adscs@BN^1Awq-m~e{3f(B*=M5JOipPO4HI1~OeAsEW` zbk;y>(XEnLGOFw%Q6gV(xL__e+l&tmJ4AoaTjaq_$7HO#Bv$g;uH^*{o7N7pq?<(G zfJu+769psgmYjUdn3?|LqOvo2pLsR^0FS#Z;&yDE1#x`EFwN5rH4(=sg%Q`A5P_2QtsLQS<1R^QoUs> z2~0y4Vh&uACU_i3@4rAaH`#3BF`1#Oa@@!Z7K^@uYd|1(QOOkWpTuTjVmU0QZ=K~G z^f;*6%i%DTH5G$!jhQU8P*jMJsg{OfV|fqonBy{BCU>4)9deyk>RO4#h47{(X|C1P zSr`#O^H~hX^+v)wNA4qN{{T_b3#}S9Os(X`<|XiT>8%E0YD{e}_ZUGOl!*S zQv%sgRS9d5(Sd|5RbXN!07ZsE(v|P)C>~KW(bW<+*^Qi9GC3L+**00S?O0uK4d4Om zF<24{enlK+WVe}__}VdMO2Ahva#Nj!NErp1<6OmH*@LhRw>(E__kz!sH^IA98W ze+iH&V-99Mcb67ef;*F2Ct|h7RW-tHRWubuGGo&`@haq8`$OUU-XAn)X z0lw*luXd3`eMSaDvcv#p2YD%$mmR1UPWxA3u-MhxNMkCL^Psa2hrH#Gs0mpP`*4$) z{{YT@AZUn0s{7S#*+<22LTl5jWSGe+%!w7uZ7wf}9&&Iq!e?*X%$7p!Ow}`e3{{P3 zUd(b;3dm%Dz>G>7xa49`F6Zom+Dmxbahd1@Ft1i6IYYMClF((ltg&(RZ9dy476$34Ng*jOWmXTZaTCgSk=vB=`JRNqB!f3> z6$k$SW_DA3_~DmB21Z~k8bo47Cujyt7W2rr{rsOnu=UmoD#5-^(aD%C+%uWe;qSpdX< zut`(w1N4k6;%DMiaWNZ2&+a;nG%TK10Yd62kQ4N9JfL%^fDWB*T@ltCoz*2qb1pZ`RSi@z**7B)pbQ{{YA) zt~$6jOr}{?td^Ispo%%FNMn=~K+ODY{{X(Z&;wSYdCaNKA0>A&Nh0Y3c}dN7G9W+i z51gu0JCuMn3PMI_Xx=)K{n>=yiB&v?SNaON3X<6eQYsFD}mg|x$#sK5QgMa zD^9fpWJ0pfJ98;blWF4sZ1k`~3U~;rCxpwk*k_E$&&K=oDGhu~R|@uR4kfBtkk`k! zp^Y8IY`3%ks8_~BRUbYg?OMg^TupNjBd2bf7WJ=>x<+hsHSA>7{y!5kxZXY^sGfIf+q~R0YMEQ|MsBHo z{K5N30ESFBh`vA0IE-=Ic#rqhIC}U+<#H0dMqAxt<~_EKl|UNR(IxrHfs!JGjGo)7 zVt0w?2`@gvV!oL%$63q}rSol*DXm@5zkpP%gbA?*Q8V!oCSU$j)O7QVRK*apicUrx zE;Q0rc0KkNyprt7h?xTdKv4d1Gs}+OhVw=@*@dxerv5u6V#QUWgJ^|#gA(2u!IDg5 zw`daPMH|Ua$L?lajl5%NW+zc(aW(5>6c%jKvtCtIYl4CGGA3mXEr?D>)>ATD?>qbe zk+X$^{v$T(ISS0N?5~Vk1P#`py|611iE|Dl;$vcs{{a4b^vv43wewE5l8p97eRr+^ ztOgR`J|JvC2I?s113MqE<|miLM@%SQ#9pu#DvR4mwvNIXTXTzIppDrO&OV?_Z7~8I zb}z4KSOA26`Ax(${T%#XG~vCopu7%{vN7RC7d04TyLf@ zEq%ZOf&sSFx`m=(`pkq5VX-g=^S|H(!P+8caqSga3Q$?c;%&tzj5@=x<|Aq9nFET- z`SqwszXBbj_c7Bh8GM#Sm~>gLnkrG;wBcyd$K%?;g_5A!=_gLo(=m^OD<3<~{T*I@ z!prF<CT-X4S^scf_)U|DvZlaQny>k(?M8^F923^a>Qu`c%+UCr(T&lT|IiS+I~r?WtD3P*V+`WLgvaY%(CDiAWb2nV6rRikf3)?J5aEZY*t- zwyrhqV;qtudfW+xf;p1~e1?8jFjK$h&=p&%H|V;qJyr7c?xF2**Q@BGV$bzNy3K<5 zh#y=CjK?j4Hu#w6i%Y=!loOw={LP%cQN36^n(pS%Ov z`~0Wp=cb2WHCs+rH{mjPx7S)s=){-~5>5ss3^kDnvPO5>3M284kB$1RHrbJ6*It~= zzc^@$54q?<;t66rN&HMEMCLdW{$u*afBsWrMGmaIPCEvNi(Z&wT$8Q*YXVwu!7qRJ z{{UgQwx5lPKljiJt7I%!%w4wCuPdm9dv6t`XLhQ~h#_hhN3b?zoWcGiMq9@F?sh$N zsztr(FYY$`qQ3e{kG(Y$Kkb^xKV4D{qwtC6v^7VY zjB-W_4Ua*$X^A0@GCwh}&-#v-Ry1ZZ81-bg$v)2Bb#djB*hmjs7EIttT}$Q(m5KwR|&`Cpgt(lr?f8?Lw*; z%*VJ55iB`jOs$ON@yZHhYK=RVf`cwwwRN*$DXn*ni#a;Y4n;Ozn1zW*BnwBDlPTb>8&;Wxnr)U33m!tJwE!Yu2)-Wd#D6iu#tTF zPjZ?08Hn5Cq;xVYJXL)`S!vPuB&3*{(=t(y_Ob>_$E*T!8iL!jl?s&FC+Cpykhc9OlmCqQ6`H(Bhpv5+( z`m83&*|J`xE-TbY!A~T^NpUKjrUYgtAfRLZXQgAZuT zB9XgYhDmU$ObMr0WW?qoAXx>!BZ!~ZKuA+|fmyWpuB;+o2x11@8>@38D)lb41uPGA z54K%1BmJ;5ABR#qA)98NJk(=z!|6d;O4nkoxPt(Xw@Gj?oq|cbPT%4SpY`$bz8TE2 znszCKN^2w9#VBh&nKfY_hRJ~f5^4fid(Zy>XZw=qn-OaLkfK$vSf?_>EHfbJU89yj zuy?3_k3&{44G--ZiH-viun#~=%2>OhZvGO^d3frq3Yi+1>(%T{arcoRn8}$XK(J)x zF+YrOExu!<`DMgKWPsc$Kj}mZ0Gnaba}r7sDBYg}v5@lP&NtiRsVJuIWb5Q<)^s_RRscTh{60u#0B7-ti3yn{#fi64g?q85kydOr-NC;f*=*od9?&MV zm@Ekrkxb7X25}Mj7T^n$$3kum8#J;3n}s5%S9NBBX$Z*?Otw=a%6&URptwK^_K%*F z)O_UbEjYVbJf+ESmFJB`xk*dLBu>oCM6{qoge&F*U}hpYPggxGCN(BsYlFj~Dq%20 z$wq^6qERe;Wws@1ou)z~;9_=&>WFn)$AwlyAgqH%N~qSRD{{_#auEY1Bx(h~#KA=J zjpz08& z6uVYeVat0JS{ej~!xvTUjxn@CoA4WKLmTcpLm_4-48gXFgJ^11Z{5kNRk4sLW-GJ& zn#lF0VCVgrNBW+P9DVvXvK7@z4=W@d(WZ+wlq_^f5L!^2DRHx4OtzVwqkZ;`fJ%eJ z1>{7vcZQL$>MGswV3*GxEMx$R*E}O}2;x#Z{LfDET7+_VtS`K%1NJDc;#yT!!&J9Q z)`NlNz-6Q(Gy0gGx%Vlri$AqZ+VZO)0X6cda*$!nSq({#seuzPLJWd2lHYx#dacD~ z*1kT&c%#m8W zWUb^-tIDu#AO*u3WU9{>DNC6}NRcol$;A66PGDjRIKcE6ItR0VQ(vlQjcUbdT`}ZN zobN`MtI4>;Yt1GZ*Wwf^l}7ht*5EEEDBpRQiQj#vZmizr`E}?20G%3Yk8b>u@>@Hp zJ2`VAyuIBCr|_%CW1h$;^@WL2&G%~V@hMx|E8Sx}B1e~5a5`U2YZ z&9K3P^|HeNAPs&b3K7rr- zk-TrlCS&sGFm$eh?d}6W1$b)lb!}RaQxf4-auG=v_5&w#Qw&a!gp6(=1ra}#$6e)B zPhiR1Vy3EipqqxqNkq7zp6MjGmU5DkCCP+vE)zd9%c*MRa?^#iIOJEytimfY)kT~S ztB9^ZA|_ynM8k+d2t=wT1}EliOSfrPhcxSC%?f3PzR^-W+|h-?T_!NXh8ozoAc=^b zqIUeIXE_5x%*{IjD6fLXFzGde7Hl=4>>z~*z*jjUVF^zNWX~BH+irnlMHfse~@0>5SRF0M=~?Pll6Bu&LW&Hn(f!DOiB9A-I0PSN(&ojAv1>ix=a^%#i8p@JW76j-&a z;0co}7=buAfiWVTprogiL{WaUWGF~Rp~%|9RAH4D*&3lJc!-M)#IgwdP7H;7b}muB zOb)Hf0(9`#R`cU3%7LX~=^{j!v4vc}rMpKch@bHx{{Y`WL3-nyabG)}c0%W`+{KUp zt{77?2Lz_U7#9Bk@9~+5`2F-AIh63%8<(9|PL?SRQk2CFX*UxX11K0=OoHH!XJVcA zBH#DZmkC9NB`dLzrp%ILSxw_DY%QORF^~}|0f~r_<;r9C@iEjDQIp6G7w4(g%ao~B zOzVGA<|AVmkyhpF68z-3#F0$?KO4vp<7njH6~e{Z7qF5r4FJeJ28ZySj>gmI^u&x5 z@ECInW(cH2A_vTJ5W>`9>DJ^?Ddg5#*_fBi$qNZAi6#Nh0Q41O$SNxWdWV1qph=R5qxz?9q(px}Kk*R&EU6h_ z#9}@5(UxQ>DYS`nk)3%VSydJK%ktU`_uu|LJ~|f71Ep6VbVjuxMl+5zRI$>#E-B(W zL%5F_kmDwQvXY(VdSODnq+d$~4ii;y%CU>J_U8%-l}mj_xRY*cZwG_q8k{7yg6q7kZ_hUFzwvps82Y{{{?Z^mbge1EnHQ9*$R#V-hsf0%lkBfN}DKjtT*o);=(omQgaExR##^Lqg^G0 z!5o*aA%a{R6R=LjBmV#+JzQ-=-;q`$6^N&HiPslH$xB%-21*pziTt>>K4xe0-byFp zBcK@3UZTb$?eyBQS7=P~Hnnb7H|-G4K89>#2L%vk`Ig^59D3(KTw+esqV5kO?1*s- z5_WFz6M>J#MPG3>#Bqp{{I}jeQ5|!jEY@RRc@i$s)mw;*MyhnvZSPGw>)6AJ$;&tr zY>yrxCOb3om@T1w+_SH_8JC6puPFxE$k(|h=2+xVLH6!h%2W*Y1PM&Hh(zzkL#ph} zl&>3d%T-NoTXL4{otR=wV1h@|r4&5LqPYQri4Y+&kCQFS^x3gxY^$1nKB>9N234p3 z0C{n<3))SD9D>NeNJ|+7#6~73a-M*#&b4l`@n&^0DpMo?ty-~5?&XYB6q^hp2soL9 z8sul3Z9fqm1zRC*pj-_)87zyTW}8ud(6tGOh4CvYQA?7|xsLE$nKL9?@}16m=+^~U zxSH`++`Yt1P1VKp14o@{Uf^@0x`kv&5;`58e(K~-^@cDa!1)Tb$chY< z3tS&5n4iLCN7d?&)NKF@X;(M5h)q?l)ZP@6QVok2!BTQbCJRDqq)Zqw$Kqlq{(7ls zwPAHxbc)SiTO7J+x$G1Kz?3C1S_@*{AiuD=O#c2h>O8FDfa>7=g&5_R+&*b2PzGsi z&lj#y%2>cLjx#g=06+OpR9�K~p)tGQFE0a77rZW(b2%siLIDzzvdFa4u0iLLy=| zo?QSIMjM^Tx+3;^$=O0e^^?e=L|_bvT1l7^1;mtzL=4QxOw7*vM0e3Al%mUVjL&5n z!);8Z_DGa019=gd2xiginF9X+d(1@qbVV$>te7UPjSr{jQWe+r9Ybr_aKZx$%82y{ zv8r@>$~T|$o#5lHHTCP=w8Bw=PpLjXcgyUsQ8g99oT53e1b5`zsla za?HyZHBEJOBF{Zy#)d}Os$iEYbK2aqlJfmPB%DfHL(NONS=Cp#c z2$#10OieKaAdqy(Fs4#M@&5n_gNWOV^x7}vF1seYyK@#Z0I{(YocV-T6Brov)}9dE z{lzj9GY7zCciZ7*rqRXHsc$kk>{UvZYpJPXKF}5I5#q8jVKt>c+&;zrUm!C(M07Ik ztf(#3881WuwKCcbN~uk9U?RHBqaKE>Ca;K%{{Rs#Uq1jCGl;(2@+hkrjI*d*1;5v7vd!Y08l(*Y2TJ6t8Xge^P0mqkNc%mr%sI}2)|B2eo5*5r)xuYEL=g0{W7Z_qfudq3+H<^5`9S`@KXWw6s;x{8Ce}dl z&3#3oh{#t=22Fd<t@oaQRS`ywFt2ANYNF6J$?XL;YEYg}6sksFtmnsYD`6ydCzYlDe5ij@nMh=GsDe&nV%o;yU#`^s9J z@yN**R>+XTN}o3!1k7!~!c3IEm@VLT_}k;5Lba=PGL-DjJ}fBW)H27l?5vcW$#4dj ztQQhuQlXd3O8(nFXMSA}9_P9WeIMD?@VCA@qb$;1ec=tr(lU8E&Q ztP96IRihv_X|veYFh_?ut0EBwALRrQ5gh(Lcj(iDx-UH7R(Grh&@=89!&5fe2za>t zT(<&%h>fOyT}5b|)z`Us)v^|*y@4Eh#E&nLWUq{gxRtmrI0+QY{$CM0?HvFOM5?lW z?BAMR7NP>fxZ{f&ggAHsJ`J-XL$|JYjBN!3QSwvKqiu}0jKX89IBv~?3UzAQTj_CF zKp9vT#@iCfQM`UXj6{B?pzl;FcQSbDGkV?77gVYjtD&D{aNb2;0lXlQ#hc7Tah@^% z0ORo{O;pXc9{k2~Fhqt`dTQTE#k7*hNLZMHl^YCHOE5T*;$IQI@&MJenbEU+6H6as z9FvzNc*Qj=Nd!c&Sz-oL5fTB$A~}9jAE}O{ZR%(D7OL6IH8$&vCc+97_qJe3zitq* z=L!)8gv`do2e#c&7}{$pk9LyCX0BEzuvVfvV_k?j;D}88T~(ap5viFmh5`{{XI~wQ}xWEfptQOu~k`Xk+S({t#rbi1pa#2mnw_n9M}P z@#FaX)swcQrlm`{CJ7GppYoG4;8PHUBwEN|pCJ82NXYN;ymcmKa!OtG<%Gs7)-E-` z87!_p*YF850EU+m5{VMtA|^^=c7PRy$djdInM?NEc7-r=PT@(hak%}l%Lrg#gm+o~ zJ0AZ4`E_vQ^I1zzO_$zodnDsq@->>ewPuOUgp(lSki?iw#Kc5QM&q>azd;S__M+2r z8JqA;iD4;up;^>HMDwR2o^mk@^|RJLkL%;(s6-K&TtKfWoXJ^49TKpX$^~(>1JuVr zEh(5#kpQk_nFdl*CA0umKE6%tzipb5${Bn<)9=$^34M4oiG>?VN+qlqAtV0)F$o`? zraFSU!G~>FstjJ92TwD{S5xb%C9oR_#6^Y>`i4E@JAR@+$EFre#k`$`<#F?xE&*g= zwWSM+ygfy0p2Nuff*>=t`wVSAu9u~Zvu$q~SKU%5t`}kd0BkkbD~?eB?bgWWFa^AR zU#b0c1X&DqIKno~>~<=xAh#`fWa_MFV%6_IUehr$!((|IGihWV^E3MCkd28^SF?u6 zWUVm6huctM9eIHgD>xevNNlQP%VCizl37R2)6%EYqxTq#&b3G}R*8`guEAxcoIsHz zfTMz-oi2@oVVHsV>4l`^9bU>WWb-w~!;raH5i}+Yq=b2dafX2>meNrJxX;G@0UhM! zb9XG#rH0g%=J7>^Rw@Z(Ve82Wxs4|omgWQ-&iuLajlLtJ;~seG%33|D{zE3giDT z%*XrarK+1+%%A@N<=2^5KFPz3y%QUX&PpeddnKEUmeCtd-#q{r(YtB(7<6gnZ&^Mm z3t&fcC`(%5rC}THpr2%roJ?{xlhSTxJc7o?I5i>I&3o$|$jZNBiD=_QARFzW}m)q?pC+k74bBPV$ zi}f)dKOHAtI|vQ0l33fws?@@d?yNPj592I@z5T>tJ6IEyZ7_Q;efEu>RkbzhIx_e3 z6^hGMjH_)HQC)KrGTnwE9Ja(lAVM+{we7e`5k&rf65}gWaTwEGnNq-cL>$`| zv|3`-;Q|u(5xJJYfM(CI+md^41i^(cD}$0aYaxdu z5+13Yq81azH{NaoVsAQa{{S+DlP-WPoBgj$%YqA<`#%Fl{ z08`P&X3*AIjV$(OAPAF%*}E=^1`-<*gMHSRjf2-8B?GkY5fRW8K{x{)k+DkFa{5zP zDY5hkks^QCspr4spT(0dZv0A5Xy1L~S<E#v~a(bYz&`GsUV+^JY zmQqVu?X=7k!Hkykw(&C(VQEvioMYCnXj(;^LerOW-XP^uJdff}=jH{2{@w&-?a;t<)4csbtrCKXKG;F;SDpl2E~`5MW~! z_K#GFexg5LzLF}YCm~+x9eY_yRlq7a4WDSeVGl7C!9>8!j1+A@JriQ0>`499BGg0#E~^1zI21_; z!GnmG#5Nd+35nZjo&I)*U&<=xLkAYIwcLhSSt7JE>tZj{o19YTOx6;a#of51i;G7-G&aS^x7#O?FEu&_-UmhYo8lc9()V-)J_ z6t_yOE$3-MQ*6k$s%$9>cE@qLlYJgXOx7x1O_?8M+uGhi7Dv1%A=dz zH9eLwwG#;RF-$UBR>FdjcKe{rM*j{HUwh{wMj3@DpUgL^#~B8{y0kJb|_es z@5`VBJbt00aTq%&TIMXR^=$E4%CbaAnJp$9Ea7{^{{TqA9F)w=&q8DGq^FH}r-&=d z+t9{H?xC44aKw?96Oxkoo%flIz9XV(9qe-7TOXdui4qNUbP`u_-o$T8RR5lsTn=_Ol)&8_{T*zCktLz8II4}%;syEw}paDMuEVOu*zIf z6EPbIgm1SQ+J1VMDw_+dluKJymDbACm0M$LJCKx+Pv8*(&si*_V4!Dz`I+gP3gJ;E z->A}~MI|u6ldMMp9EKKA>_Cj04{+KDzGh=-jlfB5RIMu>IRS23*Ss57+=bS!$xsIn zgW}PaN4WNw;}bG_M)NZ=jtv&r+sVZ_trN9z*6b;j`EVfN>mSA77p@jX&O zUW2Kf_Z!&e(q|;TDxKh#$-);f0=XEIFTe&4Cwbpvj-azs5(&rTTsVcS5T>YYl&YPz zl*th}a#Jgga#1nHWTYhid2|GeWgGxSY>q!s)rU!0K-PgU^^s5eX7dxu3O0|Ai2nfJ zT?BZFYCTrERz#1}7@I9+V@#BkNDu&T^CcfKGw~lgZ4u6Zw{CHu=h=Q)xeM0L zD}C(IvF&q{&0dff0CC;|CzM3Q!AH#Pv`Kcg)LkXBPg-RNO4$?ZBe24dCb7iSK$H}a zK>kO({KWqHsGX9bkc^8lcd~CKDnt+&GC+OXY@rC4jwW2U-~CK<0i%#zjK)(c+{P0(oV0Y>yLji-tm|ad z&hln$ID@`(QcDT&{GqhKv*PDY}!*KMO78KtJVQ_Q0hWhz{I@T;sV0<%$W}clLWa$vSMTL-=;YfDrCnXy3x6p!WVX!4D{aSE7-_~?5xle2daR57UupKtM(TU4IfOXP57 zm=G^xAa?dK^E~EeHl95ILSW*hEN)*c`ZUyp*pNguO2;A~CR+}O7CqDcFjKt7+i%j< z+qs#fy{Of*jUm2kH&A^@LGy_8S1~R9tz2$w{{V(eO#IJGswR=*ad_-;2111yZJQ+t zc_0L5{{Z4NsFjuI(C*lY!vL2l?M zE#S9^+9p6@65Doa*}scx5ozRb7~39`mPo+DIV>fLXaIx*pren@+f2^p(ptwltaLJ^ z>pyDsjbF;!mj*tUDTX2hD}@+AflT|P&zCM!)Gn=6tykJ+DBQ=|thzF_AG(*dMzQ2_ z#T0JO*Qm;74iy08sBIXEp0<@;RH87Te9S z?Y=T8xYfWI38(~PehVa%jtF)S-^}&)-bVddb23&@kEv+Fqe$z5O58-Oib4=m96;u0 zns}26OMmf+>I=0hQd-w@8hGcDE+DIYyN%TOlmx=$%4IUeaGBbEMB*p@Isyq7WbUSm zGZ`#Wf;4at^SpZbpUHWMHj0j7(GjQ#@Dp z$m118e<3H&#O<{Hx-A1@u2GZ&8x{C- zuPt2eRDj5SD;xCMN~z3(KG=dpc*~?oa2stLB4_-%0avbqAT43%D^YS^bjQ-Y0>4(` z3~(%V_JEY}+y4M9x9PJmc9O?XMSPTA3qNsgtl9fxt1}1dGvP4V1C@am)3(fknEZO> zsI`>TUwJlBIY?ZnE4V!eg{}o6PI_f#5h3e=7zzBKM|k+_<@p*pIY8xU)yZ55xNo?W zM6CHl5nO)}WRrk54^Z+6ke**VKp$(bH+>)(8N&uL!|9t-d;yivxgh2M(UB>iVYFdv zo#G-t)W<_r1Y7|c+1SUf%TO4_MK-l!E13oip-wm{6c+5y|AMRcXrsL4N4 zOXQBhT!)XnLVSfxFhm@-IoE+V2q$THYo{$j%{5vin7v>zZa;-;@{l>|#N5|7XF`JJXZn${h+ zjaPD1DAcQ$v-a?D;&s7S zO>Hgq0``vrCdh`-2Q%;`$KoTS#?64oR=nxR!iKRuHmW92MGTSkwQD0TJYY?5Q87D- zZMV+#);j91l5K3^T4S6}OtH;^7!A3uOMXUd7Q{k=V|h^T{)6ZkWIF? z8lZq;$jkAWXjzB}9v}fQ6B|U#%zo#um8;dpTz$i{OCg9=#FVUKl%tp!5gN)QPQgbA zD43Yz3!nGUj;Dr7)GTws)pa-Z9`(=6a6YP&gZQNH_3&+mWS&rPda%FpsyeC21GB6635(3+tvjU2MZYCFcCYavwE}8fgnhZ=vGk@* zVoyt$W>O*^!YJF-F){{R!b{`!V$Vygwp^vfBPwQ|Z=bunVPxhIeIlr~9#uo-Or zwlDRz+jT|DOvQ0^#!$SI32N)YmJwOp!b$b48N$6H&U=p-mcYjQ#17TD^{gF;P}|Gn z=GazSR4LkdoCpN2un}z5u%nkKfrW>}Z4)32G~*tytr-hA+Q~4djGtessR$Iuv6i5A zE!L&!2UzK1C+$h-y7}m71O1Wwsq{jnsMq{pzE@!6|$`d zGGqc3-3iX}wic(6xsk>+1(0r5M=)A?=x039f0YvWgD1-6ozEmEsNR-~aQ zI2nkEK@5x#mku-g{{Si9Xg{hy#%A12gdgG5Uz;nwIhJxmmU-*(`uR?e1SSy)0~ikZtzC&PR-G z9>^#e-eYfxnRvXFy17iR-C20tTGXrcc$?E+uNY~x)&dNGAr*{I%*2x?{{SiI0N05& z+h4R-ji&}tbycgBxOXTNivIvVfr)}C*ncl%$IjhbD@vGzV6WEAXDy9LDW_CiR&nDf z+H9s-#7@f?Nr|755ebN&^$xN*W(GRy?UgE71xT{WQH^igRcLS}SUVH_k_r+(#%5=k zbsW^MdCkUD3c83Y%B-&+KH7i_DUu6Fe2)BvCMSMSLR`OJxdNlZ#gS;XzB$xsKHd4O zIJXMuvJQEnhXO#=u#N;Gca_a{Z4n!EEV9)_)EhZ~tTQVDHBTfEk*+xa2nhg8^ND8L z%x%2PZLu~mnEwE<9D#*f&8SUKC7jx%w4!7lFZcWq$sZ$#<+Dq9{HLZSnicHP%6}t_ ziwsh#4VfTQE61Z|g6xaovs`C@h|EkxhyK0*wD9QkRIN^h3iokh9M$ap#8a*SJUzq= z2N**ovM0Rr<-&d!5!DybU2{hlbd5YsBf<35T2R?JI6Cn%bGPNZ9{j#`{{Y9PwW~{4 zi41VH?5k896spueP?|*GNt;Xo@BUwuO#cAA{{USRQT3(t6pI%!w&1FmFe@$VONdP7 zH*`!aM2=*JK0j0b`T+~^)fHjtYELk`1lRFv{&mQ~^pgDgDXUwDp8Ni%bW`r~cJVkCY#0OPDEL#Hh)rIemi zW*XuPNhvb*m!Nz^?oBED z4C8N^-bIsQW;Tcdfwvl=ae6bD{HZtX>sqY#O-aW7Tb#*A2_Ez7E| zIs%M^tGB=j?zrlP{{Ur*?H^jJ0?|Ca@rE%4GdwmCiTnj$KM@c^)f1S>I&EV^9M=-m zF zf}hmJ(H$wsqZf9rqJ0MBsWFXH9eO_9fWnY6CbAc~F`*$b8_wgjPsaQ71;=1)ttI@m zJ5x+V(`PS~AQ(beQbu7UM#CcbEw}#wf3AUP#!751q+|1fihjzJxU1f2j(l#s1&eGz zdgexUowl8)qO*C7SKQrOFXAi0E)BJXr_6hoCCAwk=q-q2a)2G6r)Zz?=!d**BqvkqZm&KX0k!37(hkDA~O?EwJBqtCmfC+*9f2jWeQxW=(jI&|6 zaZ_!EXC@K&0|{|4%x7^j4k8l2)I>~7@)Di?W2w|x6QWICS(LA&B1OLOm7s=xRl*~f z6pr7B{yTC10N+3dZM5Z$taq5V+9pHmzFIMS1ym&E2hFfAR5M}9=kxJ?V3#pe4M@v3k=brP!*)9CP z_x}K{k>sj#%gMb(%%zEJBVmSOy5sjn&0Pj5jMGSLRm|YswcZU^Q z)KZ|cWdpQ8%Zv>tBO81~2}FOX{{T=`#w#I^Mlm=|LzHYxqDo%0OhV!acIETxnIeeg zGdsa_ZN3?cBzjpf@%YzI7BRmZx0slYQ9JLyKo?x{RfpO&Yo;KHh6~9HFDN$-W$iDXQvt_}Pw0R5 z)fLUS$Py97Wxu)4HPU*;12YBbn@6`Tpf~{pd~C7ngEb;?lV}a^3&imY3V?PgWAP+H zP#oHEw!l*{AHDu(sZ279MZ0xrAg%)0O+z--O{x!Br5K==X5=9)y#wLHz#DA{2(APfU)(CBCfBF*2gb;K`9b(MP Xh{Ssd+t0FRX#At&<8SrXIs^aN+iI5( literal 0 HcmV?d00001 diff --git a/docs/samd/pinout.rst b/docs/samd/pinout.rst new file mode 100644 index 0000000000000..7e8d7ad9014eb --- /dev/null +++ b/docs/samd/pinout.rst @@ -0,0 +1,850 @@ +.. _samd_pinout: + +Pinout for the SAMD machine modules +=================================== + +The assignment of device functions to pins is very flexible. The same function may be used +at different pins. The representation of the assignment choices are given by a table, +which is a subset of the MCU's Pin MUX table and is specific to each board, as the +available pin set varies. The structure of the table is the same for each board, but +the set of rows is different. + +.. _samd21_pinout_table: + +Adafruit ItsyBitsy M0 Express pin assignment table +-------------------------------------------------- + +=== ==== ============ ==== ==== ====== ====== ====== ====== +Pin GPIO Pin name IRQ ADC Serial Serial TCC/TC TCC/TC +=== ==== ============ ==== ==== ====== ====== ====== ====== + 0 PA11 D0 11 19 0/3 2/3 1/1 0/3 + 1 PA10 D1 10 18 0/2 2/2 1/0 0/2 + 2 PA14 D2 14 - 2/2 4/2 3/0 0/4 + 3 PA09 D3 9 17 0/1 2/1 0/1 1/3 + 4 PA08 D4 - 16 0/0 2/0 0/0 1/2 + 5 PA15 D5 15 - 2/3 4/3 3/1 0/5 + 7 PA21 D7 5 - 5/3 3/3 7/1 0/7 + 9 PA07 D9 7 7 - 0/3 1/1 - + 10 PA18 D10 2 - 1/2 3/2 3/0 0/2 + 11 PA16 D11 0 - 1/0 3/0 2/0 0/6 + 12 PA19 D12 3 - 1/3 3/3 3/1 0/3 + 13 PA17 D13 1 - 1/1 3/1 2/1 0/7 + 14 PA02 A0 2 0 - - - - + 15 PB08 A1 8 2 - 4/0 4/0 - + 16 PB09 A2 9 3 - 4/1 4/1 - + 17 PA04 A3 4 4 - 0/0 0/0 - + 18 PA05 A4 5 5 - 0/1 0/1 - + 19 PB02 A5 2 - - 5/0 6/0 - + 20 PA22 SDA 6 - 3/0 5/0 4/0 0/4 + 21 PA23 SCL 7 - 3/1 5/1 4/1 0/5 + 22 PB10 MOSI 10 - - 4/2 5/0 0/4 + 23 PA12 MISO 12 - 2/0 4/0 2/0 0/6 + 24 PB11 SCK 11 - - 4/3 5/1 0/5 + 25 PA00 DOTSTAR_CLK 0 - - 1/0 2/0 - + 26 PA01 DOTSTAR_DATA 1 - - 1/1 2/1 - + 27 PB22 FLASH_MOSI 6 - - 5/2 7/0 - + 28 PB03 FLASH_MISO 3 - - 5/1 6/1 - + 29 PB23 FLASH_SCK 7 - - 5/3 7/1 - +=== ==== ============ ==== ==== ====== ====== ====== ====== + + +Description of the columns: + +- *Pin* - The number that is expected at ``machine.Pin(n)``, if the pin is given + as a number. This is NOT the GPIO number, but the board pin number, as + given in the board specific definition file. +- *GPIO* - The GPIO number. +- *Pin Name* - The name of a Pin which is expected argument to ``machine.Pin("name")``. +- *IRQ* - The IRQ number assigned to that GPIO, used internally by ``Pin.irq()``. When + using ``Pin.irq()``, different pins must use different IRQs +- *ADC* - The ADC channel assigned to the pin. When using ADC, different pins must + not use the same ADC channel. +- *Serial* - Two columns with optional Serial signal assignments. Both may be used. + The cell content is device #/pad #. The pad # is the respective internal + signal of that serial device. Details below. +- *TCC/TC* - Two columns with assignments of the TCC modules for PWM. + The cell content is device #/output #. For PWM, devices 0, 1, and 2 + are used. The TC device pair 3/4 is used for ``ticks_us()``. + +SAMD21 UART assignments +``````````````````````` +The UART devices and signals must be chosen according to the following rules: + +- The TX signal must be at a Pin with pad numbers 2 or 0, like Pin 1 with serial + device 0 or 2. +- The RX pin may be assigned to one of the other pads. + +Examples for Adafruit ItsyBitsy M0 Express: + +- uart 0 at pins 0/1 This is the default UART at the RX/TX labelled pins +- uart 1 at pins 12/10 +- uart 2 at pins 0/1 +- uart 3 at pins 11/13 +- uart 4 at pins 2/5 +- uart 5 at pins SCL/SDA + +or other combinations. + +SAMD21 I2C assignments +`````````````````````` +The I2C devices and signals must be chosen according to the following rules: + +- The SDA signal must be at a Pin with pad numbers 0. +- The SCL signal must be at a Pin with pad numbers 1. + +Examples for Adafruit ItsyBitsy M0 Express: + +- I2C 0 at Pin A3/A4 +- I2C 1 at pins 11/13 +- I2C 2 at the pins 4/3 +- I2C 3 at the pins SDA/SCL This is the default I2C device at the SDA/SCl labelled pin +- I2C 4 at the pins A1/A2 +- I2C 5 at the pins SDA/SCL, + +or other combinations. + +SAMD21 SPI assignments +`````````````````````` +The I2C devices and signals must be chosen according to the following rules: + +- The following pad number pairs are suitable for MOSI/SCK: 0/1, 2/3, 3/1, and 0/3. +- The MISO signal must be at a Pin with a different pad number than MOSI or SCK. + +Examples for Adafruit ItsyBitsy M0 Express: + +- SPI 0 at pins 0/4/1 +- SPI 1 at pins 11/12/13 +- SPI 2 at pins 0/4/1 +- SPI 3 at pins 11/12/13 +- SPI 4 at Pin MOSI/MISO/SCK This is the default SPI device at the MOSI/MISO/SCK labelled pins. + +or other combinations. + + +SAMD21 PWM assignments +`````````````````````` + +The TCC/TC device numbers 0, 1 and 2 can be used for PWM. Device 0 has four +channels, device 1 and 2 have two channels. So in total 3 different PWM +frequencies can be used, and 8 different duty cycle values. + +The DAC output for the Adafruit ItsyBitsy M0 Express board is available at the pin A0. + +.. _samd51_pinout_table: + +Adafruit ItsyBitsy M4 Express pin assignment table +-------------------------------------------------- + +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== +Pin GPIO Pin name IRQ ADC ADC Serial Serial TC PWM PWM +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + 2 PA02 A0 2 0 - - - - - - + 5 PA05 A1 5 5 - - 0/1 0/1 - - + 40 PB08 A2 8 2 0 - 4/0 4/0 - - + 41 PB09 A3 9 3 1 - 4/1 4/1 - - + 4 PA04 A4 4 4 - - 0/0 0/0 - - + 6 PA06 A5 6 6 - - 0/2 1/0 - - + 16 PA16 D0 0 - - 1/0 3/1 2/0 1/0 0/4 + 17 PA17 D1 1 - - 1/1 3/0 2/1 1/1 0/5 + 7 PA07 D2 7 7 - - 0/3 1/1 - - + 54 PB22 D3 22 - - 1/2 5/2 7/0 - - + 14 PA14 D4 14 - - 2/2 4/2 3/0 2/0 1/2 + 15 PA15 D5 15 - - 2/3 4/3 3/1 2/1 1/3 + 18 PA18 D7 2 - - 1/2 3/2 3/0 1/2 0/6 + 19 PA19 D9 3 - - 1/3 3/3 3/1 1/3 0/7 + 20 PA20 D10 4 - - 5/2 3/2 7/0 1/4 0/0 + 21 PA21 D11 5 - - 5/3 3/3 7/1 1/5 0/1 + 23 PA23 D12 7 - - 3/1 5/0 4/1 1/7 0/3 + 22 PA22 D13 6 - - 3/0 5/1 4/0 1/6 0/2 + 34 PB02 DOTSTAR_CLK 2 14 - - 5/0 6/0 2/2 - + 35 PB03 DOTSTAR_DATA 9 15 - - 5/1 6/1 - - + 43 PB11 FLASH_CS 12 - - - 4/3 5/1 0/5 1/1 + 11 PA11 FLASH_HOLD 11 11 - 0/3 2/3 1/1 0/3 1/7 + 9 PA09 FLASH_MISO 9 9 3 0/1 2/0 0/1 0/1 1/5 + 8 PA08 FLASH_MOSI - 8 2 0/0 2/1 0/0 0/0 1/4 + 42 PB10 FLASH_SCK 10 - - - 4/2 5/0 0/4 1/0 + 10 PA10 FLASH_WP 10 10 - 0/2 2/2 1/0 0/2 1/6 + 55 PB23 MISO 7 - - 1/3 5/3 7/1 - - + 0 PA00 MOSI 0 - - - 1/0 2/0 - - + 1 PA01 SCK 1 - - - 1/1 2/1 - - + 13 PA13 SCL 13 - - 2/1 4/0 2/1 0/7 1/3 + 12 PA12 SDA 12 - - 2/0 4/1 2/0 0/6 1/2 + 30 PA30 SWCLK 14 - - 7/2 1/2 6/0 2/0 - + 31 PA31 SWDIO 15 - - 7/3 1/3 6/1 2/1 - + 24 PA24 USB_DM 8 - - 3/2 5/2 5/0 2/2 - + 25 PA25 USB_DP 9 - - 3/3 5/3 5/1 - - + 3 PA03 - 3 10 - - - - - - + 27 PA27 - 11 - - - - - - - +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + + +Description of the columns: + +- *Pin* - The number that is expected at ``machine.Pin(n)``, if the pin is given + as a number. This is NOT the GPIO number, but the board pin number, as + given in the board specific definition file. +- *GPIO* - The GPIO number. +- *Pin Name* The name of a Pin which is expected argument to ``machine.Pin("name")``. +- *IRQ* - The IRQ number assigned to that GPIO, used internally by ``Pin.irq()``. When + using ``Pin.irq()``, different pins must use different IRQs +- *ADC* - The ADC0/1 channel assigned to the pin. When using ADC, different pins must + not use the same ADC device and channel. +- *Serial* - Two columns with optional Serial signal assignments. Both may be used. + The cell content is device #/pad #. The pad # is the respective internal + signal of that serial device. Details below. +- *TC* - These device are currently not assigned to Pin. the TC device pair 0/1 + is used for ``ticks_us()``. +- *PWM* - Two columns with assignments of the TCC modules for PWM + The cell content is device #/output #. Details below. + +SAMD51 UART assignments +``````````````````````` +The UART devices and signals must be chosen according to the following rules: + +- The TX signal must be at a Pin with pad numbers 0, like Pin 1 with serial + device 3. +- The RX pin may be assigned to one of the other pads. + +Examples for Adafruit ItsyBitsy 4 Express: + +- uart 0 at pins A4/A1 +- uart 1 at pins 1/0 This is the default UART at the RX/TX labelled pins +- uart 2 at pins SCL/SDA This is the default I2C device at the SDA/SCl labelled pin +- uart 3 at pins 0/1 +- uart 4 at pins SDA/SCL +- uart 5 at pins D12/D13 + +or other combinations. + +SAMD51 I2C assignments +`````````````````````` +The I2C devices and signals must be chosen according to the following rules: + +- The SDA signal must be at a Pin with pad numbers 0. +- The SCL signal must be at a Pin with pad numbers 1. + +Examples for Adafruit ItsyBitsy M0 Express: + +- I2C 0 at pins A3/A4 +- I2C 1 at pins 0/1 +- I2C 2 at the pins SDA/SCL +- I2C 3 at the pins 1/0 +- I2C 4 at the pins A2/A3 +- I2C 5 at the pins 12/13 + +or other combinations. + +SAMD51 SPI assignments +`````````````````````` +The SPI devices and signals must be chosen according to the following rules: + +- The following pad number pairs are suitable for MOSI/SCK: 0/1 and 3/1. +- The MISO signal must be at a Pin with a different pad number than MOSI or SCK. + +Examples for Adafruit ItsyBitsy M0 Express: + +- SPI 1 at Pin MOSI/MISO/SCK This is the default SPI device at the MOSI/MISO/SCK labelled pins. +- SPI 3 at pins 13/11/12 +- SPI 5 at pins 12/3/13 + +or other combinations. + + +SAMD51 PWM assignments +`````````````````````` + +The TCC/PWM device numbers 0 through 4 can be used for PWM. Device 0 has six +channels, device 1 has four channels, device 2 has three channels and devices +3 and 4 have two channels. So in total up to 5 different PWM frequencies +can be used, and up to 17 different duty cycle values. Note that these numbers +do not apply to every board. + +The DAC outputs for the Adafruit ItsyBitsy M4 Express board are available at the pins A0 and A1. + +Adafruit Feather M4 Express pin assignment table +------------------------------------------------ + +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== +Pin GPIO Pin name IRQ ADC ADC Serial Serial TC PWM PWM +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + 2 PA02 A0 2 0 - - - - - - + 5 PA05 A1 5 5 - - 0/1 0/1 - - + 40 PB08 A2 8 2 0 - 4/0 4/0 - - + 41 PB09 A3 9 3 1 - 4/1 4/1 - - + 4 PA04 A4 4 4 - - 0/0 0/0 - - + 38 PB06 A5 6 - 8 - - - - - + 49 PB17 D0 1 - - 5/1 - 6/1 3/1 0/5 + 48 PB16 D1 0 - - 5/0 - 6/0 3/0 0/4 + 14 PA14 D4 14 - - 2/2 4/2 3/0 2/0 1/2 + 16 PA16 D5 0 - - 1/0 3/1 2/0 1/0 0/4 + 18 PA18 D6 2 - - 1/2 3/2 3/0 1/2 0/6 + 19 PA19 D9 3 - - 1/3 3/3 3/1 1/3 0/7 + 3 PA03 AREF 3 10 - - - - - - + 20 PA20 D10 4 - - 5/2 3/2 7/0 1/4 0/0 + 21 PA21 D11 5 - - 5/3 3/3 7/1 1/5 0/1 + 22 PA22 D12 6 - - 3/0 5/1 4/0 1/6 0/2 + 23 PA23 D13 7 - - 3/1 5/0 4/1 1/7 0/3 + 43 PB11 FLASH_CS 12 - - - 4/3 5/1 0/5 1/1 + 11 PA11 FLASH_HOLD 11 11 - 0/3 2/3 1/1 0/3 1/7 + 9 PA09 FLASH_MISO 9 9 3 0/1 2/0 0/1 0/1 1/5 + 8 PA08 FLASH_MOSI - 8 2 0/0 2/1 0/0 0/0 1/4 + 42 PB10 FLASH_SCK 10 - - - 4/2 5/0 0/4 1/0 + 10 PA10 FLASH_WP 10 10 - 0/2 2/2 1/0 0/2 1/6 + 54 PB22 MISO 22 - - 1/2 5/2 7/0 - - + 55 PB23 MOSI 7 - - 1/3 5/3 7/1 - - + 35 PB03 NEOPIXEL 9 15 - - 5/1 6/1 - - + 17 PA17 SCK 1 - - 1/1 3/0 2/1 1/1 0/5 + 13 PA13 SCL 13 - - 2/1 4/0 2/1 0/7 1/3 + 12 PA12 SDA 12 - - 2/0 4/1 2/0 0/6 1/2 + 30 PA30 SWCLK 14 - - 7/2 1/2 6/0 2/0 - + 31 PA31 SWDIO 15 - - 7/3 1/3 6/1 2/1 - + 24 PA24 USB_DM 8 - - 3/2 5/2 5/0 2/2 - + 25 PA25 USB_DP 9 - - 3/3 5/3 5/1 - - + 33 PB01 VDIV 1 13 - - 5/3 7/1 - - + 0 PA00 - 0 - - - 1/0 2/0 - - + 1 PA01 - 1 - - - 1/1 2/1 - - + 6 PA06 - 6 6 - - 0/2 1/0 - - + 7 PA07 - 7 7 - - 0/3 1/1 - - + 15 PA15 - 15 - - 2/3 4/3 3/1 2/1 1/3 + 27 PA27 - 11 - - - - - - - + 32 PB00 - 9 12 - - 5/2 7/0 - - + 34 PB02 - 2 14 - - 5/0 6/0 2/2 - + 36 PB04 - 4 - 6 - - - - - + 37 PB05 - 5 - 7 - - - - - + 39 PB07 - 7 - 9 - - - - - + 44 PB12 - 12 - - 4/0 - 4/0 3/0 0/0 + 45 PB13 - 13 - - 4/1 - 4/1 3/1 0/1 + 46 PB14 - 14 - - 4/2 - 5/0 4/0 0/2 + 47 PB15 - 15 - - 4/3 - 5/1 4/1 0/3 + 62 PB30 - 14 - - 7/0 5/1 0/0 4/0 0/6 + 63 PB31 - 15 - - 7/1 5/0 0/1 4/1 0/7 +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M4 Express :ref:`samd51_pinout_table`. + +The default devices at the board are: + +- UART 5 at pins 0/1, labelled RX/TX +- I2C 2 at pins 21/20, labelled SDA/SCL +- SPI 1 at pins 22/23/24, labelled MOSI, MISO and SCK +- DAC output on pins 14 and 15, labelled A0 and A1 + +SEEED XIAO pin assignment table +------------------------------- + +=== ==== ============ ==== ==== ====== ====== ====== ====== +Pin GPIO Pin name IRQ ADC Serial Serial TCC/TC TCC/TC +=== ==== ============ ==== ==== ====== ====== ====== ====== + 2 PA02 A0_D0 2 0 - - - - + 4 PA04 A1_D1 4 4 - 0/0 0/0 - + 10 PA10 A2_D2 10 18 0/2 2/2 1/0 0/2 + 11 PA11 A3_D3 11 19 0/3 2/3 1/1 0/3 + 8 PA08 A4_D4 - 16 0/0 2/0 0/0 1/2 + 9 PA09 A5_D5 9 17 0/1 2/1 0/1 1/3 + 40 PB08 A6_D6 8 2 - 4/0 4/0 - + 41 PB09 A7_D7 9 3 - 4/1 4/1 - + 7 PA07 A8_D8 7 7 - 0/3 1/1 - + 5 PA05 A9_D9 5 5 - 0/1 0/1 - + 6 PA06 A10_D10 6 6 - 0/2 1/0 - + 18 PA18 RX_LED 2 - 1/2 3/2 3/0 0/2 + 30 PA30 SWCLK 10 - - 1/2 1/0 - + 31 PA31 SWDIO 11 - - 1/3 1/1 - + 19 PA19 TX_LED 3 - 1/3 3/3 3/1 0/3 + 24 PA24 USB_DM 12 - 3/2 5/2 5/0 1/2 + 25 PA25 USB_DP 13 - 3/3 5/3 5/1 1/3 + 17 PA17 USER_LED 1 - 1/1 3/1 2/1 0/7 + 0 PA00 - 0 - - 1/0 2/0 - + 1 PA01 - 1 - - 1/1 2/1 - + 3 PA03 - 3 1 - - - - + 12 PA12 - 12 - 2/0 4/0 2/0 0/6 + 13 PA13 - 13 - 2/1 4/1 2/0 0/7 + 14 PA14 - 14 - 2/2 4/2 3/0 0/4 + 15 PA15 - 15 - 2/3 4/3 3/1 0/5 + 16 PA16 - 0 - 1/0 3/0 2/0 0/6 + 20 PA20 - 4 - 5/2 3/2 7/0 0/4 + 21 PA21 - 5 - 5/3 3/3 7/1 0/7 + 22 PA22 - 6 - 3/0 5/0 4/0 0/4 + 23 PA23 - 7 - 3/1 5/1 4/1 0/5 + 27 PA27 - 15 - - - - - + 28 PA28 - 8 - - - - - + 34 PB02 - 2 10 - 5/0 6/0 - + 35 PB03 - 3 11 - 5/1 6/1 - + 42 PB10 - 10 - - 4/2 5/0 0/4 + 43 PB11 - 11 - - 4/3 5/1 0/5 + 54 PB22 - 6 - - 5/2 7/0 - + 55 PB23 - 7 - - 5/3 7/1 - +=== ==== ============ ==== ==== ====== ====== ====== ====== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M0 Express :ref:`samd21_pinout_table`. + +The default devices at the board are: + +- UART 4 at pins 7/6, labelled A6_D6/A7_D7 +- I2C 2 at pins 4/5, labelled A4_D4/A5_D5 +- SPI 0 at pins 10/9/8, labelled A10_D10, A9_D9 and A8_D8 +- DAC output on pin 0, labelled A0_D0 + +Adafruit Feather M0 Express pin assignment table +------------------------------------------------ + +=== ==== ============ ==== ==== ====== ====== ====== ====== +Pin GPIO Pin name IRQ ADC Serial Serial TCC/TC TCC/TC +=== ==== ============ ==== ==== ====== ====== ====== ====== + 2 PA02 A0 2 0 - - - - + 40 PB08 A1 8 2 - 4/0 4/0 - + 41 PB09 A2 9 3 - 4/1 4/1 - + 4 PA04 A3 4 4 - 0/0 0/0 - + 5 PA05 A4 5 5 - 0/1 0/1 - + 34 PB02 A5 2 10 - 5/0 6/0 - + 11 PA11 D0 11 19 0/3 2/3 1/1 0/3 + 10 PA10 D1 10 18 0/2 2/2 1/0 0/2 + 14 PA14 D2 14 - 2/2 4/2 3/0 0/4 + 9 PA09 D3 9 17 0/1 2/1 0/1 1/3 + 8 PA08 D4 - 16 0/0 2/0 0/0 1/2 + 15 PA15 D5 15 - 2/3 4/3 3/1 0/5 + 20 PA20 D6 4 - 5/2 3/2 7/0 0/4 + 21 PA21 D7 5 - 5/3 3/3 7/1 0/7 + 7 PA07 D9 7 7 - 0/3 1/1 - + 55 PB23 RX 7 - - 5/3 7/1 - + 54 PB22 TX 6 - - 5/2 7/0 - + 18 PA18 D10 2 - 1/2 3/2 3/0 0/2 + 16 PA16 D11 0 - 1/0 3/0 2/0 0/6 + 19 PA19 D12 3 - 1/3 3/3 3/1 0/3 + 17 PA17 D13 1 - 1/1 3/1 2/1 0/7 + 13 PA13 FLASH_CS 13 - 2/1 4/1 2/0 0/7 + 35 PB03 LED_RX 3 11 - 5/1 6/1 - + 27 PA27 LED_TX 15 - - - - - + 12 PA12 MISO 12 - 2/0 4/0 2/0 0/6 + 42 PB10 MOSI 10 - - 4/2 5/0 0/4 + 6 PA06 NEOPIXEL 6 6 - 0/2 1/0 - + 43 PB11 SCK 11 - - 4/3 5/1 0/5 + 23 PA23 SCL 7 - 3/1 5/1 4/1 0/5 + 22 PA22 SDA 6 - 3/0 5/0 4/0 0/4 + 30 PA30 SWCLK 10 - - 1/2 1/0 - + 31 PA31 SWDIO 11 - - 1/3 1/1 - + 24 PA24 USB_DM 12 - 3/2 5/2 5/0 1/2 + 25 PA25 USB_DP 13 - 3/3 5/3 5/1 1/3 + 0 PA00 - 0 - - 1/0 2/0 - + 1 PA01 - 1 - - 1/1 2/1 - + 3 PA03 - 3 1 - - - - + 28 PA28 - 8 - - - - - +=== ==== ============ ==== ==== ====== ====== ====== ====== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M0 Express :ref:`samd21_pinout_table`. + +The default devices at the board are: + +- UART 5 at pins 21/20, labelled RX/TX +- I2C 3 at pins 22/23, labelled SDA/SCL +- SPI 4 at pins 24/25/26, labelled MOSI, MISO and SCK +- DAC output on pin 14, labelled A0 + +Adafruit Trinket M0 pin assignment table +------------------------------------------------ + +=== ==== ============ ==== ==== ====== ====== ====== ====== +Pin GPIO Pin name IRQ ADC Serial Serial TCC/TC TCC/TC +=== ==== ============ ==== ==== ====== ====== ====== ====== + 8 PA08 D0 - 16 0/0 2/0 0/0 1/2 + 2 PA02 D1 2 0 - - - - + 9 PA09 D2 9 17 0/1 2/1 0/1 1/3 + 7 PA07 D3 7 7 - 0/3 1/1 - + 6 PA06 D4 6 6 - 0/2 1/0 - + 1 PA01 DOTSTAR_CLK 1 - - 1/1 2/1 - + 0 PA00 DOTSTAR_DATA 0 - - 1/0 2/0 - + 10 PA10 LED 10 18 0/2 2/2 1/0 0/2 + 30 PA30 SWCLK 10 - - 1/2 1/0 - + 31 PA31 SWDIO 11 - - 1/3 1/1 - + 24 PA24 USB_DM 12 - 3/2 5/2 5/0 1/2 + 25 PA25 USB_DP 13 - 3/3 5/3 5/1 1/3 + 3 PA03 - 3 1 - - - - + 4 PA04 - 4 4 - 0/0 0/0 - + 5 PA05 - 5 5 - 0/1 0/1 - + 11 PA11 - 11 19 0/3 2/3 1/1 0/3 + 14 PA14 - 14 - 2/2 4/2 3/0 0/4 + 15 PA15 - 15 - 2/3 4/3 3/1 0/5 + 16 PA16 - 0 - 1/0 3/0 2/0 0/6 + 17 PA17 - 1 - 1/1 3/1 2/1 0/7 + 18 PA18 - 2 - 1/2 3/2 3/0 0/2 + 19 PA19 - 3 - 1/3 3/3 3/1 0/3 + 22 PA22 - 6 - 3/0 5/0 4/0 0/4 + 23 PA23 - 7 - 3/1 5/1 4/1 0/5 + 27 PA27 - 15 - - - - - + 28 PA28 - 8 - - - - - +=== ==== ============ ==== ==== ====== ====== ====== ====== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M0 Express :ref:`samd21_pinout_table`. + +The default devices at the board are: + +- UART 0 at pins 3/4, labelled D3/D4 +- I2C 2 at pins 0/2, labelled D0/D2 +- SPI 0 at pins 4/2/3, labelled D4, D2 and D0 +- DAC output on pin 1, labelled D1 + +SAMD21 Xplained PRO pin assignment table +---------------------------------------- + +=== ==== ============ ==== ==== ====== ====== ====== ====== +Pin GPIO Pin name IRQ ADC Serial Serial TCC/TC TCC/TC +=== ==== ============ ==== ==== ====== ====== ====== ====== + 32 PB00 EXT1_PIN3 0 8 - 5/2 7/0 - + 33 PB01 EXT1_PIN4 1 9 - 5/3 7/1 - + 38 PB06 EXT1_PIN5 6 14 - - - - + 39 PB07 EXT1_PIN6 7 15 - - - - + 34 PB02 EXT1_PIN7 2 10 - 5/0 6/0 - + 35 PB03 EXT1_PIN8 3 11 - 5/1 6/1 - + 36 PB04 EXT1_PIN9 4 12 - - - - + 37 PB05 EXT1_PIN10 5 13 - - - - + 8 PA08 EXT1_PIN11 - 16 0/0 2/0 0/0 1/2 + 9 PA09 EXT1_PIN12 9 17 0/1 2/1 0/1 1/3 + 41 PB09 EXT1_PIN13 9 3 - 4/1 4/1 - + 40 PB08 EXT1_PIN14 8 2 - 4/0 4/0 - + 5 PA05 EXT1_PIN15 5 5 - 0/1 0/1 - + 6 PA06 EXT1_PIN16 6 6 - 0/2 1/0 - + 4 PA04 EXT1_PIN17 4 4 - 0/0 0/0 - + 7 PA07 EXT1_PIN18 7 7 - 0/3 1/1 - + 10 PA10 EXT2_PIN3 10 18 0/2 2/2 1/0 0/2 + 11 PA11 EXT2_PIN4 11 19 0/3 2/3 1/1 0/3 + 20 PA20 EXT2_PIN5 4 - 5/2 3/2 7/0 0/4 + 21 PA21 EXT2_PIN6 5 - 5/3 3/3 7/1 0/7 + 44 PB12 EXT2_PIN7 12 - 4/0 - 4/0 0/6 + 45 PB13 EXT2_PIN8 13 - 4/1 - 4/1 0/7 + 46 PB14 EXT2_PIN9 14 - 4/2 - 5/0 - + 47 PB15 EXT2_PIN10 15 - 4/3 - 5/1 - + 43 PB11 EXT2_PIN13 11 - - 4/3 5/1 0/5 + 42 PB10 EXT2_PIN14 10 - - 4/2 5/0 0/4 + 17 PA17 EXT2_PIN15 1 - 1/1 3/1 2/1 0/7 + 18 PA18 EXT2_PIN16 2 - 1/2 3/2 3/0 0/2 + 16 PA16 EXT2_PIN17 0 - 1/0 3/0 2/0 0/6 + 19 PA19 EXT2_PIN18 3 - 1/3 3/3 3/1 0/3 + 2 PA02 EXT3_PIN3 2 0 - - - - + 3 PA03 EXT3_PIN4 3 1 - - - - + 15 PA15 EXT3_PIN6 15 - 2/3 4/3 3/1 0/5 + 12 PA12 EXT3_PIN7 12 - 2/0 4/0 2/0 0/6 + 13 PA13 EXT3_PIN8 13 - 2/1 4/1 2/0 0/7 + 28 PA28 EXT3_PIN9 8 - - - - - + 27 PA27 EXT3_PIN10 15 - - - - - + 49 PB17 EXT3_PIN15 1 - 5/1 - 6/1 0/5 + 54 PB22 EXT3_PIN16 6 - - 5/2 7/0 - + 48 PB16 EXT3_PIN17 9 - 5/0 - 6/0 0/4 + 55 PB23 EXT3_PIN18 7 - - 5/3 7/1 - + 62 PB30 LED 14 - - 5/0 0/0 1/2 + 30 PA30 SWCLK 10 - - 1/2 1/0 - + 31 PA31 SWDIO 11 - - 1/3 1/1 - + 24 PA24 USB_DM 12 - 3/2 5/2 5/0 1/2 + 25 PA25 USB_DP 13 - 3/3 5/3 5/1 1/3 + 0 PA00 - 0 - - 1/0 2/0 - + 1 PA01 - 1 - - 1/1 2/1 - + 14 PA14 - 14 - 2/2 4/2 3/0 0/4 + 22 PA22 - 6 - 3/0 5/0 4/0 0/4 + 23 PA23 - 7 - 3/1 5/1 4/1 0/5 + 63 PB31 - 15 - - 5/1 0/1 1/3 +=== ==== ============ ==== ==== ====== ====== ====== ====== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M0 Express :ref:`samd21_pinout_table`. + +There are no pins labelled for default devices on this board. DAC output +is on pin 32, labelled EXT3_PIN3 + +Minisam M4 pin assignment table +------------------------------- + +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== +Pin GPIO Pin name IRQ ADC ADC Serial Serial TC PWM PWM +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + 16 PA16 D0 0 - - 1/0 3/1 2/0 1/0 0/4 + 17 PA17 D1 1 - - 1/1 3/0 2/1 1/1 0/5 + 19 PA19 D3 3 - - 1/3 3/3 3/1 1/3 0/7 + 20 PA20 D4 4 - - 5/2 3/2 7/0 1/4 0/0 + 21 PA21 D5 5 - - 5/3 3/3 7/1 1/5 0/1 + 2 PA02 A0_D9 2 0 - - - - - - + 40 PB08 A1_D10 8 2 0 - 4/0 4/0 - - + 41 PB09 A2_D11 9 3 1 - 4/1 4/1 - - + 4 PA04 A3_D12 4 4 - - 0/0 0/0 - - + 5 PA05 A4_D13 5 5 - - 0/1 0/1 - - + 6 PA06 A5 6 6 - - 0/2 1/0 - - + 7 PA07 A6_D2 7 7 - - 0/3 1/1 - - + 3 PA03 AREF 3 10 - - - - - - + 0 PA00 BUTTON 0 - - - 1/0 2/0 - - + 34 PB02 DOTSTAR_CLK 2 14 - - 5/0 6/0 2/2 - + 35 PB03 DOTSTAR_DATA 9 15 - - 5/1 6/1 - - + 15 PA15 LED 15 - - 2/3 4/3 3/1 2/1 1/3 + 55 PB23 MISO 7 - - 1/3 5/3 7/1 - - + 54 PB22 MOSI 22 - - 1/2 5/2 7/0 - - + 1 PA01 SCK 1 - - - 1/1 2/1 - - + 13 PA13 SCL 13 - - 2/1 4/0 2/1 0/7 1/3 + 12 PA12 SDA 12 - - 2/0 4/1 2/0 0/6 1/2 + 30 PA30 SWCLK 14 - - 7/2 1/2 6/0 2/0 - + 31 PA31 SWDIO 15 - - 7/3 1/3 6/1 2/1 - + 24 PA24 USB_DM 8 - - 3/2 5/2 5/0 2/2 - + 25 PA25 USB_DP 9 - - 3/3 5/3 5/1 - - + 8 PA08 - - 8 2 0/0 2/1 0/0 0/0 1/4 + 9 PA09 - 9 9 3 0/1 2/0 0/1 0/1 1/5 + 10 PA10 - 10 10 - 0/2 2/2 1/0 0/2 1/6 + 11 PA11 - 11 11 - 0/3 2/3 1/1 0/3 1/7 + 14 PA14 - 14 - - 2/2 4/2 3/0 2/0 1/2 + 18 PA18 - 2 - - 1/2 3/2 3/0 1/2 0/6 + 22 PA22 - 6 - - 3/0 5/1 4/0 1/6 0/2 + 23 PA23 - 7 - - 3/1 5/0 4/1 1/7 0/3 + 27 PA27 - 11 - - - - - - - + 42 PB10 - 10 - - - 4/2 5/0 0/4 1/0 + 43 PB11 - 12 - - - 4/3 5/1 0/5 1/1 +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M4 Express :ref:`samd51_pinout_table`. + +The default devices at the board are: + +- UART 1 at pins 6/7, labelled D0/D1 +- I2C 2 at pins 14/15, labelled SDA/SCL +- SPI 1 at pins 16/17/18, labelled MOSI, MISO and SCK +- DAC output on pins 0 and 4, labelled A0_D9 and A4_D13 + +Seeed WIO Terminal pin assignment table +--------------------------------------- + +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== +Pin GPIO Pin name IRQ ADC ADC Serial Serial TC PWM PWM +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + 33 PB01 CS 1 13 - - 5/3 7/1 - - + 59 PB27 RX 13 - - 2/1 4/0 - 1/3 - + 58 PB26 TX 12 - - 2/0 4/1 - 1/2 - + 79 PC15 3V3_ENABLE 15 - - 7/3 6/3 - 0/5 1/1 + 78 PC14 5V_ENABLE 14 - - 7/2 6/2 - 0/4 1/0 + 40 PB08 A0_D0 8 2 0 - 4/0 4/0 - - + 41 PB09 A1_D1 9 3 1 - 4/1 4/1 - - + 7 PA07 A2_D2 7 7 - - 0/3 1/1 - - + 36 PB04 A3_D3 4 - 6 - - - - - + 37 PB05 A4_D4 5 - 7 - - - - - + 38 PB06 A5_D5 6 - 8 - - - - - + 4 PA04 A6_D6 4 4 - - 0/0 0/0 - - + 39 PB07 A7_D7 7 - 9 - - - - - + 6 PA06 A8_D8 6 6 - - 0/2 1/0 - - + 90 PC26 BUTTON_1 10 - - - - - - - + 91 PC27 BUTTON_2 11 - - 1/0 - - - - + 92 PC28 BUTTON_3 12 - - 1/1 - - - - +107 PD11 BUZZER 6 - - 7/3 6/3 - 0/4 - + 47 PB15 GPCLK0 15 - - 4/3 - 5/1 4/1 0/3 + 44 PB12 GPCLK1 12 - - 4/0 - 4/0 3/0 0/0 + 45 PB13 GPCLK2 13 - - 4/1 - 4/1 3/1 0/1 + 48 PB16 I2C_BCLK 0 - - 5/0 - 6/0 3/0 0/4 + 20 PA20 I2S_LRCLK 4 - - 5/2 3/2 7/0 1/4 0/0 + 21 PA21 I2S_SDIN 5 - - 5/3 3/3 7/1 1/5 0/1 + 22 PA22 I2S_SDOUT 6 - - 3/0 5/1 4/0 1/6 0/2 + 50 PB18 LCD_MISO 2 - - 5/2 7/2 - 1/0 - + 51 PB19 LCD_MOSI 3 - - 5/3 7/3 - 1/1 - + 52 PB20 LCD_SCK 4 - - 3/0 7/1 - 1/2 - + 53 PB21 LCD_CS 5 - - 3/1 7/0 - 1/3 - + 70 PC06 LCD_D/C 6 - - 6/2 - - - - + 71 PC07 LCD_RESET 9 - - 6/3 - - - - + 74 PC10 LCD_XL 10 - - 6/2 7/2 - 0/0 1/4 + 76 PC12 LCD_XR 12 - - 7/0 6/1 - 0/2 1/6 + 77 PC13 LCD_YD 13 - - 7/1 6/0 - 0/3 1/7 + 75 PC11 LCD_YU 11 - - 6/3 7/3 - 0/1 1/5 + 15 PA15 LED_BLUE 15 - - 2/3 4/3 3/1 2/1 1/3 + 69 PC05 LED_LCD 5 - - 6/1 - - - - + 94 PC30 MIC 14 - 12 - - - - - + 32 PB00 MISO 9 12 - - 5/2 7/0 - - + 34 PB02 MOSI 2 14 - - 5/0 6/0 2/2 - + 35 PB03 SCK 9 15 - - 5/1 6/1 - - + 12 PA12 SCL0 12 - - 2/0 4/1 2/0 0/6 1/2 + 13 PA13 SDA0 13 - - 2/1 4/0 2/1 0/7 1/3 + 16 PA16 SCL1 0 - - 1/0 3/1 2/0 1/0 0/4 + 17 PA17 SDA1 1 - - 1/1 3/0 2/1 1/1 0/5 +117 PD21 SD_DET 11 - - 1/3 3/3 - 1/1 - + 83 PC19 SD_CS 3 - - 6/3 0/3 - 0/3 - + 82 PC18 SD_MISO 2 - - 6/2 0/2 - 0/2 - + 80 PC16 SD_MOSI 0 - - 6/0 0/1 - 0/0 - + 81 PC17 SD_SCK 1 - - 6/1 0/0 - 0/1 - + 30 PA30 SWCLK 14 - - 7/2 1/2 6/0 2/0 - + 31 PA31 SWDIO 15 - - 7/3 1/3 6/1 2/1 - +108 PD12 SWITCH_B 7 - - - - - 0/5 - +116 PD20 SWITCH_U 10 - - 1/2 3/2 - 1/0 - +104 PD08 SWITCH_X 3 - - 7/0 6/1 - 0/1 - +105 PD09 SWITCH_Y 4 - - 7/1 6/0 - 0/2 - +106 PD10 SWITCH_Z 5 - - 7/2 6/2 - 0/3 - + 24 PA24 USB_DM 8 - - 3/2 5/2 5/0 2/2 - + 25 PA25 USB_DP 9 - - 3/3 5/3 5/1 - - + 0 PA00 - 0 - - - 1/0 2/0 - - + 1 PA01 - 1 - - - 1/1 2/1 - - + 2 PA02 - 2 0 - - - - - - + 3 PA03 - 3 10 - - - - - - + 5 PA05 - 5 5 - - 0/1 0/1 - - + 8 PA08 - - 8 2 0/0 2/1 0/0 0/0 1/4 + 9 PA09 - 9 9 3 0/1 2/0 0/1 0/1 1/5 + 10 PA10 - 10 10 - 0/2 2/2 1/0 0/2 1/6 + 11 PA11 - 11 11 - 0/3 2/3 1/1 0/3 1/7 + 14 PA14 - 14 - - 2/2 4/2 3/0 2/0 1/2 + 18 PA18 - 2 - - 1/2 3/2 3/0 1/2 0/6 + 19 PA19 - 3 - - 1/3 3/3 3/1 1/3 0/7 + 23 PA23 - 7 - - 3/1 5/0 4/1 1/7 0/3 + 27 PA27 - 11 - - - - - - - + 42 PB10 - 10 - - - 4/2 5/0 0/4 1/0 + 43 PB11 - 12 - - - 4/3 5/1 0/5 1/1 + 46 PB14 - 14 - - 4/2 - 5/0 4/0 0/2 + 49 PB17 - 1 - - 5/1 - 6/1 3/1 0/5 + 54 PB22 - 22 - - 1/2 5/2 7/0 - - + 55 PB23 - 7 - - 1/3 5/3 7/1 - - + 56 PB24 - 8 - - 0/0 2/1 - - - + 57 PB25 - 9 - - 0/1 2/0 - - - + 60 PB28 - 14 - - 2/2 4/2 - 1/4 - + 61 PB29 - 15 - - 2/3 4/3 - 1/5 - + 62 PB30 - 14 - - 7/0 5/1 0/0 4/0 0/6 + 63 PB31 - 15 - - 7/1 5/0 0/1 4/1 0/7 + 64 PC00 - 0 - 10 - - - - - + 65 PC01 - 1 - 11 - - - - - + 66 PC02 - 2 - 4 - - - - - + 67 PC03 - 3 - 5 - - - - - + 68 PC04 - 4 - - 6/0 - - 0/0 - + 84 PC20 - 4 - - - - - 0/4 - + 85 PC21 - 5 - - - - - 0/5 - + 86 PC22 - 6 - - 1/0 3/1 - 0/5 - + 87 PC23 - 7 - - 1/1 3/0 - 0/7 - + 88 PC24 - 8 - - 0/2 2/2 - - - + 89 PC25 - 9 - - 0/3 2/3 - - - + 95 PC31 - 15 - 13 - - - - - + 96 PD00 - 0 - 14 - - - - - + 97 PD01 - 1 - 15 - - - - - +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M4 Express :ref:`samd51_pinout_table`. + +There seems to be no default pin assignment for this board. + +Sparkfun SAMD51 Thing Plus pin assignment table +------------------------------------------------ + +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== +Pin GPIO Pin name IRQ ADC ADC Serial Serial TC PWM PWM +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + 2 PA02 A0 2 0 - - - - - - + 40 PB08 A1 8 2 0 - 4/0 4/0 - - + 41 PB09 A2 9 3 1 - 4/1 4/1 - - + 4 PA04 A3 4 4 - - 0/0 0/0 - - + 5 PA05 A4 5 5 - - 0/1 0/1 - - + 34 PB02 A5 2 14 - - 5/0 6/0 2/2 - + 13 PA13 D0 13 - - 2/1 4/0 2/1 0/7 1/3 + 12 PA12 D1 12 - - 2/0 4/1 2/0 0/6 1/2 + 6 PA06 D4 6 6 - - 0/2 1/0 - - + 15 PA15 D5 15 - - 2/3 4/3 3/1 2/1 1/3 + 20 PA20 D6 4 - - 5/2 3/2 7/0 1/4 0/0 + 21 PA21 D7 5 - - 5/3 3/3 7/1 1/5 0/1 + 7 PA07 D9 7 7 - - 0/3 1/1 - - + 18 PA18 D10 2 - - 1/2 3/2 3/0 1/2 0/6 + 16 PA16 D11 0 - - 1/0 3/1 2/0 1/0 0/4 + 19 PA19 D12 3 - - 1/3 3/3 3/1 1/3 0/7 + 17 PA17 D13 1 - - 1/1 3/0 2/1 1/1 0/5 + 10 PA10 FLASH_CS 10 10 - 0/2 2/2 1/0 0/2 1/6 + 11 PA11 FLASH_MISO 11 11 - 0/3 2/3 1/1 0/3 1/7 + 8 PA08 FLASH_MOSI - 8 2 0/0 2/1 0/0 0/0 1/4 + 9 PA09 FLASH_SCK 9 9 3 0/1 2/0 0/1 0/1 1/5 + 43 PB11 MISO 12 - - - 4/3 5/1 0/5 1/1 + 44 PB12 MOSI 12 - - 4/0 - 4/0 3/0 0/0 + 55 PB23 RXD 7 - - 1/3 5/3 7/1 - - + 35 PB03 RXLED 9 15 - - 5/1 6/1 - - + 45 PB13 SCK 13 - - 4/1 - 4/1 3/1 0/1 + 23 PA23 SCL 7 - - 3/1 5/0 4/1 1/7 0/3 + 22 PA22 SDA 6 - - 3/0 5/1 4/0 1/6 0/2 + 30 PA30 SWCLK 14 - - 7/2 1/2 6/0 2/0 - + 31 PA31 SWDIO 15 - - 7/3 1/3 6/1 2/1 - + 54 PB22 TXD 22 - - 1/2 5/2 7/0 - - + 27 PA27 TXLED 11 - - - - - - - + 24 PA24 USB_DM 8 - - 3/2 5/2 5/0 2/2 - + 25 PA25 USB_DP 9 - - 3/3 5/3 5/1 - - + 0 PA00 - 0 - - - 1/0 2/0 - - + 1 PA01 - 1 - - - 1/1 2/1 - - + 3 PA03 - 3 10 - - - - - - + 14 PA14 - 14 - - 2/2 4/2 3/0 2/0 1/2 + 32 PB00 - 9 12 - - 5/2 7/0 - - + 33 PB01 - 1 13 - - 5/3 7/1 - - + 36 PB04 - 4 - 6 - - - - - + 37 PB05 - 5 - 7 - - - - - + 38 PB06 - 6 - 8 - - - - - + 39 PB07 - 7 - 9 - - - - - + 42 PB10 - 10 - - - 4/2 5/0 0/4 1/0 + 46 PB14 - 14 - - 4/2 - 5/0 4/0 0/2 + 47 PB15 - 15 - - 4/3 - 5/1 4/1 0/3 + 48 PB16 - 0 - - 5/0 - 6/0 3/0 0/4 + 49 PB17 - 1 - - 5/1 - 6/1 3/1 0/5 + 62 PB30 - 14 - - 7/0 5/1 0/0 4/0 0/6 + 63 PB31 - 15 - - 7/1 5/0 0/1 4/1 0/7 +=== ==== ============ ==== ==== ==== ====== ====== ===== ===== ===== + +For the definition of the table columns see the explanation at the table for +Adafruit ItsyBitsy M4 Express :ref:`samd51_pinout_table`. + +The default devices at the board are: + +- UART 1 at pins 2/3, labelled RXD/TXD +- I2C 5 at pins 20/21, labelled SDA/SCL +- SPI 4 at pins 22/23/24, labelled MOSI, MISO and SCK +- DAC output on pins 14 and 18, labelled A0 and A4 + +Scripts for creating the pin assignment tables +---------------------------------------------- + +The tables shown above were created with small a Python script running on the target board:: + + from samd import pininfo + from machine import Pin + import os + + def print_entry(e, txt): + print(txt, end=": ") + if e == 255: + print(" - ", end="") + else: + print("%d/%d" % (e >> 4, e & 0x0f), end="") + + def print_pininfo(pin, info): + print("%3d" % pin, end=" ") + print("P%c%02d" % ("ABCD"[pin // 32], pin % 32), end="") + print(" %12s" % info[0], end="") + print(" IRQ:%2s" % (info[1] if info[1] != 255 else "-"), end="") + print(" ADC0:%2s" % (info[2] if info[2] != 255 else "-"), end="") + if len(info) == 7: + print_entry(info[3], " Serial1") + print_entry(info[4], " Serial2") + print_entry(info[5], " PWM1" if (info[5] >> 4) < 3 else " TC") + print_entry(info[6], " PWM2") + else: + print(" ADC1:%2s" % (info[3] if info[3] != 255 else "-"), end="") + print_entry(info[4], " Serial1") + print_entry(info[5], " Serial2") + print_entry(info[6], " TC") + print_entry(info[7], " PWM1") + print_entry(info[8], " PWM2") + print() + + def tblkey(i): + name = i[1][0] + if name != "-": + if len(name) < 3: + return " " + name + else: + return name + else: + return "zzzzzzz%03d" % i[0] + + def table(num = 127): + pintbl = [] + for i in range(num): + try: + pintbl.append((i, pininfo(i))) + except: + pass + # print("not defined") + + pintbl.sort(key=tblkey) + for item in pintbl: + print_pininfo(item[0], item[1]) + + table() diff --git a/docs/samd/quickref.rst b/docs/samd/quickref.rst new file mode 100644 index 0000000000000..5e8298d1b366f --- /dev/null +++ b/docs/samd/quickref.rst @@ -0,0 +1,469 @@ +.. _samd_quickref: + +Quick reference for the SAMD21/SAMD51 family +============================================ + +.. image:: img/itsybitsy_m4_express.jpg + :alt: Adafruit ItsyBitsy M4 Express board + :width: 640px + +The Adafruit ItsyBitsy M4 Express board. + +Below is a quick reference for SAMD21/SAMD51-based boards. If it is your first time +working with this board it may be useful to get an overview of the microcontroller: + +.. toctree:: + :maxdepth: 1 + + general.rst + tutorial/intro.rst + pinout.rst + + +Installing MicroPython +---------------------- + +See the corresponding section of tutorial: :ref:`samd_intro`. It also includes +a troubleshooting subsection. + +General board control +--------------------- + +The MicroPython REPL is on the USB port, configured in VCP mode. +Tab-completion is useful to find out what methods an object has. +Paste mode (Ctrl-E) is useful to paste a large slab of Python code into +the REPL. + +The :mod:`machine` module:: + + import machine + + machine.freq() # get the current frequency of the CPU + machine.freq(96_000_000) # set the CPU frequency to 96 MHz + +The range accepted by the function call is 1_000_000 to 200_000_000 (1 MHz to 200 MHz) +for SAMD51 and 1_000_000 to 48_000_000 (1 MHz to 48 MHz) for SAMD21. The safe +range for SAMD51 according to the data sheet is 96 MHz to 120 MHz. +At frequencies below 8 MHz USB will be disabled. Changing the frequency below 48 MHz +impacts the baud rates of UART, I2C and SPI. These have to be set again after +changing the CPU frequency. The ms and µs timers are not affected by the frequency +change. + + +Delay and timing +---------------- + +Use the :mod:`time

MicroPython documentation

Quick reference for the Renesas RA
general information for Renesas RA based boards, snippets of useful code, and a tutorial

+ From c138e10fbb60981ca9e9b7459e7b9ae0c2528c94 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 27 Oct 2022 12:57:10 +1100 Subject: [PATCH 2791/3533] py/makeversionhdr: Fall back to py/mpconfig.h instead of docs/conf.py. Commit 64af916c111b61bce82c00f356a6b1cb81946d87 removed the version string from docs/conf.py. py/mpconfig.h is a better place to get the version from, so use that (when there is no git repository). Signed-off-by: Damien George --- py/makeversionhdr.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/py/makeversionhdr.py b/py/makeversionhdr.py index 54b7fa9ab7d2a..d1b41e63b0845 100644 --- a/py/makeversionhdr.py +++ b/py/makeversionhdr.py @@ -62,21 +62,27 @@ def get_version_info_from_git(): return git_tag, git_hash -def get_version_info_from_docs_conf(): - with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "conf.py")) as f: +def get_version_info_from_mpconfig(): + with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "py", "mpconfig.h")) as f: for line in f: - if line.startswith("version = release = '"): - ver = line.strip().split(" = ")[2].strip("'") - git_tag = "v" + ver + if line.startswith("#define MICROPY_VERSION_MAJOR "): + ver_major = int(line.strip().split()[2]) + elif line.startswith("#define MICROPY_VERSION_MINOR "): + ver_minor = int(line.strip().split()[2]) + elif line.startswith("#define MICROPY_VERSION_MICRO "): + ver_micro = int(line.strip().split()[2]) + git_tag = "v%d.%d" % (ver_major, ver_minor) + if ver_micro != 0: + git_tag += ".%d" % (ver_micro,) return git_tag, "" return None def make_version_header(filename): - # Get version info using git, with fallback to docs/conf.py + # Get version info using git, with fallback to py/mpconfig.h info = get_version_info_from_git() if info is None: - info = get_version_info_from_docs_conf() + info = get_version_info_from_mpconfig() git_tag, git_hash = info From e20bb98392c988a410ba04d01d3f573e6c15803d Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 15 Oct 2022 21:37:51 +0200 Subject: [PATCH 2792/3533] mimxrt/machine_pin: Add the Pin.toggle() method. For consistency with other ports, even if this method is undocumented at the moment. --- ports/mimxrt/machine_pin.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ports/mimxrt/machine_pin.c b/ports/mimxrt/machine_pin.c index 836bd8524b729..bb2c820100be5 100644 --- a/ports/mimxrt/machine_pin.c +++ b/ports/mimxrt/machine_pin.c @@ -285,6 +285,14 @@ STATIC mp_obj_t machine_pin_on(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on); +// pin.toggle() +STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { + machine_pin_obj_t *self = self_in; + mp_hal_pin_toggle(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); + // pin.value([value]) STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { return machine_pin_obj_call(args[0], (n_args - 1), 0, args + 1); @@ -366,6 +374,7 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) }, { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_pin_off_obj) }, { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_pin_on_obj) }, + { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, From 9d2e179fa565d8af96fb6ce3643e00f999c9210a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 22 Oct 2022 21:28:42 +0200 Subject: [PATCH 2793/3533] mimxrt: Fix CPU freeze when calling __WFE() in MICROPY_EVENT_POLL_HOOK. This issue affected i.MX RT 1052, 1062 and 1064. It seems to be addressed by Errata ERR006223, which also mentions i.MX RT101x and 102x, but these devices worked well even without the change. As a side effect, the current consumption at an idle REPL drops significantly with this fix. Fixes issue #7235. --- ports/mimxrt/boards/MIMXRT1052_clock_config.c | 1 + ports/mimxrt/boards/MIMXRT1062_clock_config.c | 1 + ports/mimxrt/boards/MIMXRT1064_clock_config.c | 1 + ports/mimxrt/mpconfigport.h | 1 + 4 files changed, 4 insertions(+) diff --git a/ports/mimxrt/boards/MIMXRT1052_clock_config.c b/ports/mimxrt/boards/MIMXRT1052_clock_config.c index 93492812977ab..fa7450d487a63 100644 --- a/ports/mimxrt/boards/MIMXRT1052_clock_config.c +++ b/ports/mimxrt/boards/MIMXRT1052_clock_config.c @@ -465,4 +465,5 @@ void BOARD_BootClockRUN(void) { IOMUXC_GPR->GPR5 &= ~IOMUXC_GPR_GPR5_VREF_1M_CLK_GPT2_MASK; /* Set SystemCoreClock variable. */ SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK; + CLOCK_SetMode(kCLOCK_ModeRun); } diff --git a/ports/mimxrt/boards/MIMXRT1062_clock_config.c b/ports/mimxrt/boards/MIMXRT1062_clock_config.c index 05474167bd85b..589ffb0b58311 100644 --- a/ports/mimxrt/boards/MIMXRT1062_clock_config.c +++ b/ports/mimxrt/boards/MIMXRT1062_clock_config.c @@ -487,4 +487,5 @@ void BOARD_BootClockRUN(void) { IOMUXC_GPR->GPR5 &= ~IOMUXC_GPR_GPR5_VREF_1M_CLK_GPT2_MASK; /* Set SystemCoreClock variable. */ SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK; + CLOCK_SetMode(kCLOCK_ModeRun); } diff --git a/ports/mimxrt/boards/MIMXRT1064_clock_config.c b/ports/mimxrt/boards/MIMXRT1064_clock_config.c index 5e49a2fff80e3..56dd75d7fbf6f 100644 --- a/ports/mimxrt/boards/MIMXRT1064_clock_config.c +++ b/ports/mimxrt/boards/MIMXRT1064_clock_config.c @@ -487,4 +487,5 @@ void BOARD_BootClockRUN(void) { IOMUXC_GPR->GPR5 &= ~IOMUXC_GPR_GPR5_VREF_1M_CLK_GPT2_MASK; /* Set SystemCoreClock variable. */ SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK; + CLOCK_SetMode(kCLOCK_ModeRun); } diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 8642d53ecc455..3f87800e3056d 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -241,6 +241,7 @@ extern const struct _mp_obj_type_t network_lan_type; do { \ extern void mp_handle_pending(bool); \ mp_handle_pending(true); \ + __WFE(); \ } while (0); #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) From 65fa7fd8bbaf570be3f4654bcc597a46c70e4b75 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 15 Oct 2022 11:40:22 +0200 Subject: [PATCH 2794/3533] mimxrt/machine_timer: Use soft-timer implementation for machine.Timer. This releases the hardware timers for other tasks, which need a higher resolution and faster response. And it is less port-specific code. --- docs/mimxrt/quickref.rst | 9 +- ports/mimxrt/Makefile | 1 + ports/mimxrt/board_init.c | 3 - ports/mimxrt/machine_timer.c | 158 +++++++++-------------------------- ports/mimxrt/main.c | 2 + ports/mimxrt/mphalport.h | 4 + ports/mimxrt/systick.c | 7 ++ 7 files changed, 58 insertions(+), 126 deletions(-) diff --git a/docs/mimxrt/quickref.rst b/docs/mimxrt/quickref.rst index c75fe60c8d66b..06f91f7f50312 100644 --- a/docs/mimxrt/quickref.rst +++ b/docs/mimxrt/quickref.rst @@ -56,21 +56,18 @@ Use the :mod:`time